Increase loop alignment on Cortex cores to 8 and set function alignment to 16.
[official-gcc.git] / gcc / tree-vrp.c
blob4333d60672fe7bedd8fdbfcabb3d60ee0511b258
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 && compare_values (vr1.max, zero) != 0)
2943 min = int_const_binop (code, vr0.min, vr1.max);
2944 else
2945 min = zero;
2947 else if (cmp == 0 || cmp == -1)
2948 min = vr0.min;
2949 else
2950 type = VR_VARYING;
2952 else
2954 /* Otherwise the range is -max .. max or min .. -min
2955 depending on which bound is bigger in absolute value,
2956 as the division can change the sign. */
2957 abs_extent_range (vr, vr0.min, vr0.max);
2958 return;
2960 if (type == VR_VARYING)
2962 set_value_range_to_varying (vr);
2963 return;
2966 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1))
2968 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2969 return;
2972 else if (code == TRUNC_MOD_EXPR)
2974 if (range_is_null (&vr1))
2976 set_value_range_to_undefined (vr);
2977 return;
2979 /* ABS (A % B) < ABS (B) and either
2980 0 <= A % B <= A or A <= A % B <= 0. */
2981 type = VR_RANGE;
2982 signop sgn = TYPE_SIGN (expr_type);
2983 unsigned int prec = TYPE_PRECISION (expr_type);
2984 wide_int wmin, wmax, tmp;
2985 wide_int zero = wi::zero (prec);
2986 wide_int one = wi::one (prec);
2987 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
2989 wmax = wi::sub (vr1.max, one);
2990 if (sgn == SIGNED)
2992 tmp = wi::sub (wi::minus_one (prec), vr1.min);
2993 wmax = wi::smax (wmax, tmp);
2996 else
2998 wmax = wi::max_value (prec, sgn);
2999 /* X % INT_MIN may be INT_MAX. */
3000 if (sgn == UNSIGNED)
3001 wmax = wmax - one;
3004 if (sgn == UNSIGNED)
3005 wmin = zero;
3006 else
3008 wmin = -wmax;
3009 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3011 tmp = vr0.min;
3012 if (wi::gts_p (tmp, zero))
3013 tmp = zero;
3014 wmin = wi::smax (wmin, tmp);
3018 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3020 tmp = vr0.max;
3021 if (sgn == SIGNED && wi::neg_p (tmp))
3022 tmp = zero;
3023 wmax = wi::min (wmax, tmp, sgn);
3026 min = wide_int_to_tree (expr_type, wmin);
3027 max = wide_int_to_tree (expr_type, wmax);
3029 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3031 bool int_cst_range0, int_cst_range1;
3032 wide_int may_be_nonzero0, may_be_nonzero1;
3033 wide_int must_be_nonzero0, must_be_nonzero1;
3035 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3036 &may_be_nonzero0,
3037 &must_be_nonzero0);
3038 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3039 &may_be_nonzero1,
3040 &must_be_nonzero1);
3042 type = VR_RANGE;
3043 if (code == BIT_AND_EXPR)
3045 min = wide_int_to_tree (expr_type,
3046 must_be_nonzero0 & must_be_nonzero1);
3047 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3048 /* If both input ranges contain only negative values we can
3049 truncate the result range maximum to the minimum of the
3050 input range maxima. */
3051 if (int_cst_range0 && int_cst_range1
3052 && tree_int_cst_sgn (vr0.max) < 0
3053 && tree_int_cst_sgn (vr1.max) < 0)
3055 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3056 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3058 /* If either input range contains only non-negative values
3059 we can truncate the result range maximum to the respective
3060 maximum of the input range. */
3061 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3062 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3063 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3064 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3065 max = wide_int_to_tree (expr_type, wmax);
3067 else if (code == BIT_IOR_EXPR)
3069 max = wide_int_to_tree (expr_type,
3070 may_be_nonzero0 | may_be_nonzero1);
3071 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3072 /* If the input ranges contain only positive values we can
3073 truncate the minimum of the result range to the maximum
3074 of the input range minima. */
3075 if (int_cst_range0 && int_cst_range1
3076 && tree_int_cst_sgn (vr0.min) >= 0
3077 && tree_int_cst_sgn (vr1.min) >= 0)
3079 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3080 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3082 /* If either input range contains only negative values
3083 we can truncate the minimum of the result range to the
3084 respective minimum range. */
3085 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3086 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3087 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3088 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3089 min = wide_int_to_tree (expr_type, wmin);
3091 else if (code == BIT_XOR_EXPR)
3093 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3094 | ~(may_be_nonzero0 | may_be_nonzero1));
3095 wide_int result_one_bits
3096 = (must_be_nonzero0.and_not (may_be_nonzero1)
3097 | must_be_nonzero1.and_not (may_be_nonzero0));
3098 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3099 min = wide_int_to_tree (expr_type, result_one_bits);
3100 /* If the range has all positive or all negative values the
3101 result is better than VARYING. */
3102 if (tree_int_cst_sgn (min) < 0
3103 || tree_int_cst_sgn (max) >= 0)
3105 else
3106 max = min = NULL_TREE;
3109 else
3110 gcc_unreachable ();
3112 /* If either MIN or MAX overflowed, then set the resulting range to
3113 VARYING. But we do accept an overflow infinity representation. */
3114 if (min == NULL_TREE
3115 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3116 || max == NULL_TREE
3117 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3119 set_value_range_to_varying (vr);
3120 return;
3123 /* We punt if:
3124 1) [-INF, +INF]
3125 2) [-INF, +-INF(OVF)]
3126 3) [+-INF(OVF), +INF]
3127 4) [+-INF(OVF), +-INF(OVF)]
3128 We learn nothing when we have INF and INF(OVF) on both sides.
3129 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3130 overflow. */
3131 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3132 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3134 set_value_range_to_varying (vr);
3135 return;
3138 cmp = compare_values (min, max);
3139 if (cmp == -2 || cmp == 1)
3141 /* If the new range has its limits swapped around (MIN > MAX),
3142 then the operation caused one of them to wrap around, mark
3143 the new range VARYING. */
3144 set_value_range_to_varying (vr);
3146 else
3147 set_value_range (vr, type, min, max, NULL);
3150 /* Extract range information from a binary expression OP0 CODE OP1 based on
3151 the ranges of each of its operands with resulting type EXPR_TYPE.
3152 The resulting range is stored in *VR. */
3154 static void
3155 extract_range_from_binary_expr (value_range *vr,
3156 enum tree_code code,
3157 tree expr_type, tree op0, tree op1)
3159 value_range vr0 = VR_INITIALIZER;
3160 value_range vr1 = VR_INITIALIZER;
3162 /* Get value ranges for each operand. For constant operands, create
3163 a new value range with the operand to simplify processing. */
3164 if (TREE_CODE (op0) == SSA_NAME)
3165 vr0 = *(get_value_range (op0));
3166 else if (is_gimple_min_invariant (op0))
3167 set_value_range_to_value (&vr0, op0, NULL);
3168 else
3169 set_value_range_to_varying (&vr0);
3171 if (TREE_CODE (op1) == SSA_NAME)
3172 vr1 = *(get_value_range (op1));
3173 else if (is_gimple_min_invariant (op1))
3174 set_value_range_to_value (&vr1, op1, NULL);
3175 else
3176 set_value_range_to_varying (&vr1);
3178 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3180 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3181 and based on the other operand, for example if it was deduced from a
3182 symbolic comparison. When a bound of the range of the first operand
3183 is invariant, we set the corresponding bound of the new range to INF
3184 in order to avoid recursing on the range of the second operand. */
3185 if (vr->type == VR_VARYING
3186 && (code == PLUS_EXPR || code == MINUS_EXPR)
3187 && TREE_CODE (op1) == SSA_NAME
3188 && vr0.type == VR_RANGE
3189 && symbolic_range_based_on_p (&vr0, op1))
3191 const bool minus_p = (code == MINUS_EXPR);
3192 value_range n_vr1 = VR_INITIALIZER;
3194 /* Try with VR0 and [-INF, OP1]. */
3195 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3196 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3198 /* Try with VR0 and [OP1, +INF]. */
3199 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3200 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3202 /* Try with VR0 and [OP1, OP1]. */
3203 else
3204 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3206 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3209 if (vr->type == VR_VARYING
3210 && (code == PLUS_EXPR || code == MINUS_EXPR)
3211 && TREE_CODE (op0) == SSA_NAME
3212 && vr1.type == VR_RANGE
3213 && symbolic_range_based_on_p (&vr1, op0))
3215 const bool minus_p = (code == MINUS_EXPR);
3216 value_range n_vr0 = VR_INITIALIZER;
3218 /* Try with [-INF, OP0] and VR1. */
3219 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3220 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3222 /* Try with [OP0, +INF] and VR1. */
3223 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3224 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3226 /* Try with [OP0, OP0] and VR1. */
3227 else
3228 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3230 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3234 /* Extract range information from a unary operation CODE based on
3235 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3236 The resulting range is stored in *VR. */
3238 static void
3239 extract_range_from_unary_expr_1 (value_range *vr,
3240 enum tree_code code, tree type,
3241 value_range *vr0_, tree op0_type)
3243 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3245 /* VRP only operates on integral and pointer types. */
3246 if (!(INTEGRAL_TYPE_P (op0_type)
3247 || POINTER_TYPE_P (op0_type))
3248 || !(INTEGRAL_TYPE_P (type)
3249 || POINTER_TYPE_P (type)))
3251 set_value_range_to_varying (vr);
3252 return;
3255 /* If VR0 is UNDEFINED, so is the result. */
3256 if (vr0.type == VR_UNDEFINED)
3258 set_value_range_to_undefined (vr);
3259 return;
3262 /* Handle operations that we express in terms of others. */
3263 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3265 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3266 copy_value_range (vr, &vr0);
3267 return;
3269 else if (code == NEGATE_EXPR)
3271 /* -X is simply 0 - X, so re-use existing code that also handles
3272 anti-ranges fine. */
3273 value_range zero = VR_INITIALIZER;
3274 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3275 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3276 return;
3278 else if (code == BIT_NOT_EXPR)
3280 /* ~X is simply -1 - X, so re-use existing code that also handles
3281 anti-ranges fine. */
3282 value_range minusone = VR_INITIALIZER;
3283 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3284 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3285 type, &minusone, &vr0);
3286 return;
3289 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3290 and express op ~[] as (op []') U (op []''). */
3291 if (vr0.type == VR_ANTI_RANGE
3292 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3294 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3295 if (vrtem1.type != VR_UNDEFINED)
3297 value_range vrres = VR_INITIALIZER;
3298 extract_range_from_unary_expr_1 (&vrres, code, type,
3299 &vrtem1, op0_type);
3300 vrp_meet (vr, &vrres);
3302 return;
3305 if (CONVERT_EXPR_CODE_P (code))
3307 tree inner_type = op0_type;
3308 tree outer_type = type;
3310 /* If the expression evaluates to a pointer, we are only interested in
3311 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3312 if (POINTER_TYPE_P (type))
3314 if (range_is_nonnull (&vr0))
3315 set_value_range_to_nonnull (vr, type);
3316 else if (range_is_null (&vr0))
3317 set_value_range_to_null (vr, type);
3318 else
3319 set_value_range_to_varying (vr);
3320 return;
3323 /* If VR0 is varying and we increase the type precision, assume
3324 a full range for the following transformation. */
3325 if (vr0.type == VR_VARYING
3326 && INTEGRAL_TYPE_P (inner_type)
3327 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3329 vr0.type = VR_RANGE;
3330 vr0.min = TYPE_MIN_VALUE (inner_type);
3331 vr0.max = TYPE_MAX_VALUE (inner_type);
3334 /* If VR0 is a constant range or anti-range and the conversion is
3335 not truncating we can convert the min and max values and
3336 canonicalize the resulting range. Otherwise we can do the
3337 conversion if the size of the range is less than what the
3338 precision of the target type can represent and the range is
3339 not an anti-range. */
3340 if ((vr0.type == VR_RANGE
3341 || vr0.type == VR_ANTI_RANGE)
3342 && TREE_CODE (vr0.min) == INTEGER_CST
3343 && TREE_CODE (vr0.max) == INTEGER_CST
3344 && (!is_overflow_infinity (vr0.min)
3345 || (vr0.type == VR_RANGE
3346 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3347 && needs_overflow_infinity (outer_type)
3348 && supports_overflow_infinity (outer_type)))
3349 && (!is_overflow_infinity (vr0.max)
3350 || (vr0.type == VR_RANGE
3351 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3352 && needs_overflow_infinity (outer_type)
3353 && supports_overflow_infinity (outer_type)))
3354 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3355 || (vr0.type == VR_RANGE
3356 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3357 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3358 size_int (TYPE_PRECISION (outer_type)))))))
3360 tree new_min, new_max;
3361 if (is_overflow_infinity (vr0.min))
3362 new_min = negative_overflow_infinity (outer_type);
3363 else
3364 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3365 0, false);
3366 if (is_overflow_infinity (vr0.max))
3367 new_max = positive_overflow_infinity (outer_type);
3368 else
3369 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3370 0, false);
3371 set_and_canonicalize_value_range (vr, vr0.type,
3372 new_min, new_max, NULL);
3373 return;
3376 set_value_range_to_varying (vr);
3377 return;
3379 else if (code == ABS_EXPR)
3381 tree min, max;
3382 int cmp;
3384 /* Pass through vr0 in the easy cases. */
3385 if (TYPE_UNSIGNED (type)
3386 || value_range_nonnegative_p (&vr0))
3388 copy_value_range (vr, &vr0);
3389 return;
3392 /* For the remaining varying or symbolic ranges we can't do anything
3393 useful. */
3394 if (vr0.type == VR_VARYING
3395 || symbolic_range_p (&vr0))
3397 set_value_range_to_varying (vr);
3398 return;
3401 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3402 useful range. */
3403 if (!TYPE_OVERFLOW_UNDEFINED (type)
3404 && ((vr0.type == VR_RANGE
3405 && vrp_val_is_min (vr0.min))
3406 || (vr0.type == VR_ANTI_RANGE
3407 && !vrp_val_is_min (vr0.min))))
3409 set_value_range_to_varying (vr);
3410 return;
3413 /* ABS_EXPR may flip the range around, if the original range
3414 included negative values. */
3415 if (is_overflow_infinity (vr0.min))
3416 min = positive_overflow_infinity (type);
3417 else if (!vrp_val_is_min (vr0.min))
3418 min = fold_unary_to_constant (code, type, vr0.min);
3419 else if (!needs_overflow_infinity (type))
3420 min = TYPE_MAX_VALUE (type);
3421 else if (supports_overflow_infinity (type))
3422 min = positive_overflow_infinity (type);
3423 else
3425 set_value_range_to_varying (vr);
3426 return;
3429 if (is_overflow_infinity (vr0.max))
3430 max = positive_overflow_infinity (type);
3431 else if (!vrp_val_is_min (vr0.max))
3432 max = fold_unary_to_constant (code, type, vr0.max);
3433 else if (!needs_overflow_infinity (type))
3434 max = TYPE_MAX_VALUE (type);
3435 else if (supports_overflow_infinity (type)
3436 /* We shouldn't generate [+INF, +INF] as set_value_range
3437 doesn't like this and ICEs. */
3438 && !is_positive_overflow_infinity (min))
3439 max = positive_overflow_infinity (type);
3440 else
3442 set_value_range_to_varying (vr);
3443 return;
3446 cmp = compare_values (min, max);
3448 /* If a VR_ANTI_RANGEs contains zero, then we have
3449 ~[-INF, min(MIN, MAX)]. */
3450 if (vr0.type == VR_ANTI_RANGE)
3452 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3454 /* Take the lower of the two values. */
3455 if (cmp != 1)
3456 max = min;
3458 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3459 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3460 flag_wrapv is set and the original anti-range doesn't include
3461 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3462 if (TYPE_OVERFLOW_WRAPS (type))
3464 tree type_min_value = TYPE_MIN_VALUE (type);
3466 min = (vr0.min != type_min_value
3467 ? int_const_binop (PLUS_EXPR, type_min_value,
3468 build_int_cst (TREE_TYPE (type_min_value), 1))
3469 : type_min_value);
3471 else
3473 if (overflow_infinity_range_p (&vr0))
3474 min = negative_overflow_infinity (type);
3475 else
3476 min = TYPE_MIN_VALUE (type);
3479 else
3481 /* All else has failed, so create the range [0, INF], even for
3482 flag_wrapv since TYPE_MIN_VALUE is in the original
3483 anti-range. */
3484 vr0.type = VR_RANGE;
3485 min = build_int_cst (type, 0);
3486 if (needs_overflow_infinity (type))
3488 if (supports_overflow_infinity (type))
3489 max = positive_overflow_infinity (type);
3490 else
3492 set_value_range_to_varying (vr);
3493 return;
3496 else
3497 max = TYPE_MAX_VALUE (type);
3501 /* If the range contains zero then we know that the minimum value in the
3502 range will be zero. */
3503 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3505 if (cmp == 1)
3506 max = min;
3507 min = build_int_cst (type, 0);
3509 else
3511 /* If the range was reversed, swap MIN and MAX. */
3512 if (cmp == 1)
3513 std::swap (min, max);
3516 cmp = compare_values (min, max);
3517 if (cmp == -2 || cmp == 1)
3519 /* If the new range has its limits swapped around (MIN > MAX),
3520 then the operation caused one of them to wrap around, mark
3521 the new range VARYING. */
3522 set_value_range_to_varying (vr);
3524 else
3525 set_value_range (vr, vr0.type, min, max, NULL);
3526 return;
3529 /* For unhandled operations fall back to varying. */
3530 set_value_range_to_varying (vr);
3531 return;
3535 /* Extract range information from a unary expression CODE OP0 based on
3536 the range of its operand with resulting type TYPE.
3537 The resulting range is stored in *VR. */
3539 static void
3540 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3541 tree type, tree op0)
3543 value_range vr0 = VR_INITIALIZER;
3545 /* Get value ranges for the operand. For constant operands, create
3546 a new value range with the operand to simplify processing. */
3547 if (TREE_CODE (op0) == SSA_NAME)
3548 vr0 = *(get_value_range (op0));
3549 else if (is_gimple_min_invariant (op0))
3550 set_value_range_to_value (&vr0, op0, NULL);
3551 else
3552 set_value_range_to_varying (&vr0);
3554 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3558 /* Extract range information from a conditional expression STMT based on
3559 the ranges of each of its operands and the expression code. */
3561 static void
3562 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3564 tree op0, op1;
3565 value_range vr0 = VR_INITIALIZER;
3566 value_range vr1 = VR_INITIALIZER;
3568 /* Get value ranges for each operand. For constant operands, create
3569 a new value range with the operand to simplify processing. */
3570 op0 = gimple_assign_rhs2 (stmt);
3571 if (TREE_CODE (op0) == SSA_NAME)
3572 vr0 = *(get_value_range (op0));
3573 else if (is_gimple_min_invariant (op0))
3574 set_value_range_to_value (&vr0, op0, NULL);
3575 else
3576 set_value_range_to_varying (&vr0);
3578 op1 = gimple_assign_rhs3 (stmt);
3579 if (TREE_CODE (op1) == SSA_NAME)
3580 vr1 = *(get_value_range (op1));
3581 else if (is_gimple_min_invariant (op1))
3582 set_value_range_to_value (&vr1, op1, NULL);
3583 else
3584 set_value_range_to_varying (&vr1);
3586 /* The resulting value range is the union of the operand ranges */
3587 copy_value_range (vr, &vr0);
3588 vrp_meet (vr, &vr1);
3592 /* Extract range information from a comparison expression EXPR based
3593 on the range of its operand and the expression code. */
3595 static void
3596 extract_range_from_comparison (value_range *vr, enum tree_code code,
3597 tree type, tree op0, tree op1)
3599 bool sop = false;
3600 tree val;
3602 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3603 NULL);
3605 /* A disadvantage of using a special infinity as an overflow
3606 representation is that we lose the ability to record overflow
3607 when we don't have an infinity. So we have to ignore a result
3608 which relies on overflow. */
3610 if (val && !is_overflow_infinity (val) && !sop)
3612 /* Since this expression was found on the RHS of an assignment,
3613 its type may be different from _Bool. Convert VAL to EXPR's
3614 type. */
3615 val = fold_convert (type, val);
3616 if (is_gimple_min_invariant (val))
3617 set_value_range_to_value (vr, val, vr->equiv);
3618 else
3619 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3621 else
3622 /* The result of a comparison is always true or false. */
3623 set_value_range_to_truthvalue (vr, type);
3626 /* Helper function for simplify_internal_call_using_ranges and
3627 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3628 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3629 always overflow. Set *OVF to true if it is known to always
3630 overflow. */
3632 static bool
3633 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3634 tree op0, tree op1, bool *ovf)
3636 value_range vr0 = VR_INITIALIZER;
3637 value_range vr1 = VR_INITIALIZER;
3638 if (TREE_CODE (op0) == SSA_NAME)
3639 vr0 = *get_value_range (op0);
3640 else if (TREE_CODE (op0) == INTEGER_CST)
3641 set_value_range_to_value (&vr0, op0, NULL);
3642 else
3643 set_value_range_to_varying (&vr0);
3645 if (TREE_CODE (op1) == SSA_NAME)
3646 vr1 = *get_value_range (op1);
3647 else if (TREE_CODE (op1) == INTEGER_CST)
3648 set_value_range_to_value (&vr1, op1, NULL);
3649 else
3650 set_value_range_to_varying (&vr1);
3652 if (!range_int_cst_p (&vr0)
3653 || TREE_OVERFLOW (vr0.min)
3654 || TREE_OVERFLOW (vr0.max))
3656 vr0.min = vrp_val_min (TREE_TYPE (op0));
3657 vr0.max = vrp_val_max (TREE_TYPE (op0));
3659 if (!range_int_cst_p (&vr1)
3660 || TREE_OVERFLOW (vr1.min)
3661 || TREE_OVERFLOW (vr1.max))
3663 vr1.min = vrp_val_min (TREE_TYPE (op1));
3664 vr1.max = vrp_val_max (TREE_TYPE (op1));
3666 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3667 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3668 if (arith_overflowed_p (subcode, type, vr0.max,
3669 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3670 return false;
3671 if (subcode == MULT_EXPR)
3673 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3674 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3675 return false;
3677 if (*ovf)
3679 /* So far we found that there is an overflow on the boundaries.
3680 That doesn't prove that there is an overflow even for all values
3681 in between the boundaries. For that compute widest_int range
3682 of the result and see if it doesn't overlap the range of
3683 type. */
3684 widest_int wmin, wmax;
3685 widest_int w[4];
3686 int i;
3687 w[0] = wi::to_widest (vr0.min);
3688 w[1] = wi::to_widest (vr0.max);
3689 w[2] = wi::to_widest (vr1.min);
3690 w[3] = wi::to_widest (vr1.max);
3691 for (i = 0; i < 4; i++)
3693 widest_int wt;
3694 switch (subcode)
3696 case PLUS_EXPR:
3697 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3698 break;
3699 case MINUS_EXPR:
3700 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3701 break;
3702 case MULT_EXPR:
3703 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3704 break;
3705 default:
3706 gcc_unreachable ();
3708 if (i == 0)
3710 wmin = wt;
3711 wmax = wt;
3713 else
3715 wmin = wi::smin (wmin, wt);
3716 wmax = wi::smax (wmax, wt);
3719 /* The result of op0 CODE op1 is known to be in range
3720 [wmin, wmax]. */
3721 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3722 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3723 /* If all values in [wmin, wmax] are smaller than
3724 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3725 the arithmetic operation will always overflow. */
3726 if (wmax < wtmin || wmin > wtmax)
3727 return true;
3728 return false;
3730 return true;
3733 /* Try to derive a nonnegative or nonzero range out of STMT relying
3734 primarily on generic routines in fold in conjunction with range data.
3735 Store the result in *VR */
3737 static void
3738 extract_range_basic (value_range *vr, gimple *stmt)
3740 bool sop = false;
3741 tree type = gimple_expr_type (stmt);
3743 if (is_gimple_call (stmt))
3745 tree arg;
3746 int mini, maxi, zerov = 0, prec;
3747 enum tree_code subcode = ERROR_MARK;
3748 combined_fn cfn = gimple_call_combined_fn (stmt);
3750 switch (cfn)
3752 case CFN_BUILT_IN_CONSTANT_P:
3753 /* If the call is __builtin_constant_p and the argument is a
3754 function parameter resolve it to false. This avoids bogus
3755 array bound warnings.
3756 ??? We could do this as early as inlining is finished. */
3757 arg = gimple_call_arg (stmt, 0);
3758 if (TREE_CODE (arg) == SSA_NAME
3759 && SSA_NAME_IS_DEFAULT_DEF (arg)
3760 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3762 set_value_range_to_null (vr, type);
3763 return;
3765 break;
3766 /* Both __builtin_ffs* and __builtin_popcount return
3767 [0, prec]. */
3768 CASE_CFN_FFS:
3769 CASE_CFN_POPCOUNT:
3770 arg = gimple_call_arg (stmt, 0);
3771 prec = TYPE_PRECISION (TREE_TYPE (arg));
3772 mini = 0;
3773 maxi = prec;
3774 if (TREE_CODE (arg) == SSA_NAME)
3776 value_range *vr0 = get_value_range (arg);
3777 /* If arg is non-zero, then ffs or popcount
3778 are non-zero. */
3779 if (((vr0->type == VR_RANGE
3780 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3781 || (vr0->type == VR_ANTI_RANGE
3782 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3783 && !is_overflow_infinity (vr0->min)
3784 && !is_overflow_infinity (vr0->max))
3785 mini = 1;
3786 /* If some high bits are known to be zero,
3787 we can decrease the maximum. */
3788 if (vr0->type == VR_RANGE
3789 && TREE_CODE (vr0->max) == INTEGER_CST
3790 && !operand_less_p (vr0->min,
3791 build_zero_cst (TREE_TYPE (vr0->min)))
3792 && !is_overflow_infinity (vr0->max))
3793 maxi = tree_floor_log2 (vr0->max) + 1;
3795 goto bitop_builtin;
3796 /* __builtin_parity* returns [0, 1]. */
3797 CASE_CFN_PARITY:
3798 mini = 0;
3799 maxi = 1;
3800 goto bitop_builtin;
3801 /* __builtin_c[lt]z* return [0, prec-1], except for
3802 when the argument is 0, but that is undefined behavior.
3803 On many targets where the CLZ RTL or optab value is defined
3804 for 0 the value is prec, so include that in the range
3805 by default. */
3806 CASE_CFN_CLZ:
3807 arg = gimple_call_arg (stmt, 0);
3808 prec = TYPE_PRECISION (TREE_TYPE (arg));
3809 mini = 0;
3810 maxi = prec;
3811 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3812 != CODE_FOR_nothing
3813 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3814 zerov)
3815 /* Handle only the single common value. */
3816 && zerov != prec)
3817 /* Magic value to give up, unless vr0 proves
3818 arg is non-zero. */
3819 mini = -2;
3820 if (TREE_CODE (arg) == SSA_NAME)
3822 value_range *vr0 = get_value_range (arg);
3823 /* From clz of VR_RANGE minimum we can compute
3824 result maximum. */
3825 if (vr0->type == VR_RANGE
3826 && TREE_CODE (vr0->min) == INTEGER_CST
3827 && !is_overflow_infinity (vr0->min))
3829 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3830 if (maxi != prec)
3831 mini = 0;
3833 else if (vr0->type == VR_ANTI_RANGE
3834 && integer_zerop (vr0->min)
3835 && !is_overflow_infinity (vr0->min))
3837 maxi = prec - 1;
3838 mini = 0;
3840 if (mini == -2)
3841 break;
3842 /* From clz of VR_RANGE maximum we can compute
3843 result minimum. */
3844 if (vr0->type == VR_RANGE
3845 && TREE_CODE (vr0->max) == INTEGER_CST
3846 && !is_overflow_infinity (vr0->max))
3848 mini = prec - 1 - tree_floor_log2 (vr0->max);
3849 if (mini == prec)
3850 break;
3853 if (mini == -2)
3854 break;
3855 goto bitop_builtin;
3856 /* __builtin_ctz* return [0, prec-1], except for
3857 when the argument is 0, but that is undefined behavior.
3858 If there is a ctz optab for this mode and
3859 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3860 otherwise just assume 0 won't be seen. */
3861 CASE_CFN_CTZ:
3862 arg = gimple_call_arg (stmt, 0);
3863 prec = TYPE_PRECISION (TREE_TYPE (arg));
3864 mini = 0;
3865 maxi = prec - 1;
3866 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3867 != CODE_FOR_nothing
3868 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3869 zerov))
3871 /* Handle only the two common values. */
3872 if (zerov == -1)
3873 mini = -1;
3874 else if (zerov == prec)
3875 maxi = prec;
3876 else
3877 /* Magic value to give up, unless vr0 proves
3878 arg is non-zero. */
3879 mini = -2;
3881 if (TREE_CODE (arg) == SSA_NAME)
3883 value_range *vr0 = get_value_range (arg);
3884 /* If arg is non-zero, then use [0, prec - 1]. */
3885 if (((vr0->type == VR_RANGE
3886 && integer_nonzerop (vr0->min))
3887 || (vr0->type == VR_ANTI_RANGE
3888 && integer_zerop (vr0->min)))
3889 && !is_overflow_infinity (vr0->min))
3891 mini = 0;
3892 maxi = prec - 1;
3894 /* If some high bits are known to be zero,
3895 we can decrease the result maximum. */
3896 if (vr0->type == VR_RANGE
3897 && TREE_CODE (vr0->max) == INTEGER_CST
3898 && !is_overflow_infinity (vr0->max))
3900 maxi = tree_floor_log2 (vr0->max);
3901 /* For vr0 [0, 0] give up. */
3902 if (maxi == -1)
3903 break;
3906 if (mini == -2)
3907 break;
3908 goto bitop_builtin;
3909 /* __builtin_clrsb* returns [0, prec-1]. */
3910 CASE_CFN_CLRSB:
3911 arg = gimple_call_arg (stmt, 0);
3912 prec = TYPE_PRECISION (TREE_TYPE (arg));
3913 mini = 0;
3914 maxi = prec - 1;
3915 goto bitop_builtin;
3916 bitop_builtin:
3917 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3918 build_int_cst (type, maxi), NULL);
3919 return;
3920 case CFN_UBSAN_CHECK_ADD:
3921 subcode = PLUS_EXPR;
3922 break;
3923 case CFN_UBSAN_CHECK_SUB:
3924 subcode = MINUS_EXPR;
3925 break;
3926 case CFN_UBSAN_CHECK_MUL:
3927 subcode = MULT_EXPR;
3928 break;
3929 case CFN_GOACC_DIM_SIZE:
3930 case CFN_GOACC_DIM_POS:
3931 /* Optimizing these two internal functions helps the loop
3932 optimizer eliminate outer comparisons. Size is [1,N]
3933 and pos is [0,N-1]. */
3935 bool is_pos = cfn == CFN_GOACC_DIM_POS;
3936 int axis = get_oacc_ifn_dim_arg (stmt);
3937 int size = get_oacc_fn_dim_size (current_function_decl, axis);
3939 if (!size)
3940 /* If it's dynamic, the backend might know a hardware
3941 limitation. */
3942 size = targetm.goacc.dim_limit (axis);
3944 tree type = TREE_TYPE (gimple_call_lhs (stmt));
3945 set_value_range (vr, VR_RANGE,
3946 build_int_cst (type, is_pos ? 0 : 1),
3947 size ? build_int_cst (type, size - is_pos)
3948 : vrp_val_max (type), NULL);
3950 return;
3951 default:
3952 break;
3954 if (subcode != ERROR_MARK)
3956 bool saved_flag_wrapv = flag_wrapv;
3957 /* Pretend the arithmetics is wrapping. If there is
3958 any overflow, we'll complain, but will actually do
3959 wrapping operation. */
3960 flag_wrapv = 1;
3961 extract_range_from_binary_expr (vr, subcode, type,
3962 gimple_call_arg (stmt, 0),
3963 gimple_call_arg (stmt, 1));
3964 flag_wrapv = saved_flag_wrapv;
3966 /* If for both arguments vrp_valueize returned non-NULL,
3967 this should have been already folded and if not, it
3968 wasn't folded because of overflow. Avoid removing the
3969 UBSAN_CHECK_* calls in that case. */
3970 if (vr->type == VR_RANGE
3971 && (vr->min == vr->max
3972 || operand_equal_p (vr->min, vr->max, 0)))
3973 set_value_range_to_varying (vr);
3974 return;
3977 /* Handle extraction of the two results (result of arithmetics and
3978 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
3979 internal function. */
3980 else if (is_gimple_assign (stmt)
3981 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
3982 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
3983 && INTEGRAL_TYPE_P (type))
3985 enum tree_code code = gimple_assign_rhs_code (stmt);
3986 tree op = gimple_assign_rhs1 (stmt);
3987 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
3989 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
3990 if (is_gimple_call (g) && gimple_call_internal_p (g))
3992 enum tree_code subcode = ERROR_MARK;
3993 switch (gimple_call_internal_fn (g))
3995 case IFN_ADD_OVERFLOW:
3996 subcode = PLUS_EXPR;
3997 break;
3998 case IFN_SUB_OVERFLOW:
3999 subcode = MINUS_EXPR;
4000 break;
4001 case IFN_MUL_OVERFLOW:
4002 subcode = MULT_EXPR;
4003 break;
4004 default:
4005 break;
4007 if (subcode != ERROR_MARK)
4009 tree op0 = gimple_call_arg (g, 0);
4010 tree op1 = gimple_call_arg (g, 1);
4011 if (code == IMAGPART_EXPR)
4013 bool ovf = false;
4014 if (check_for_binary_op_overflow (subcode, type,
4015 op0, op1, &ovf))
4016 set_value_range_to_value (vr,
4017 build_int_cst (type, ovf),
4018 NULL);
4019 else if (TYPE_PRECISION (type) == 1
4020 && !TYPE_UNSIGNED (type))
4021 set_value_range_to_varying (vr);
4022 else
4023 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4024 build_int_cst (type, 1), NULL);
4026 else if (types_compatible_p (type, TREE_TYPE (op0))
4027 && types_compatible_p (type, TREE_TYPE (op1)))
4029 bool saved_flag_wrapv = flag_wrapv;
4030 /* Pretend the arithmetics is wrapping. If there is
4031 any overflow, IMAGPART_EXPR will be set. */
4032 flag_wrapv = 1;
4033 extract_range_from_binary_expr (vr, subcode, type,
4034 op0, op1);
4035 flag_wrapv = saved_flag_wrapv;
4037 else
4039 value_range vr0 = VR_INITIALIZER;
4040 value_range vr1 = VR_INITIALIZER;
4041 bool saved_flag_wrapv = flag_wrapv;
4042 /* Pretend the arithmetics is wrapping. If there is
4043 any overflow, IMAGPART_EXPR will be set. */
4044 flag_wrapv = 1;
4045 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4046 type, op0);
4047 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4048 type, op1);
4049 extract_range_from_binary_expr_1 (vr, subcode, type,
4050 &vr0, &vr1);
4051 flag_wrapv = saved_flag_wrapv;
4053 return;
4058 if (INTEGRAL_TYPE_P (type)
4059 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4060 set_value_range_to_nonnegative (vr, type,
4061 sop || stmt_overflow_infinity (stmt));
4062 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4063 && !sop)
4064 set_value_range_to_nonnull (vr, type);
4065 else
4066 set_value_range_to_varying (vr);
4070 /* Try to compute a useful range out of assignment STMT and store it
4071 in *VR. */
4073 static void
4074 extract_range_from_assignment (value_range *vr, gassign *stmt)
4076 enum tree_code code = gimple_assign_rhs_code (stmt);
4078 if (code == ASSERT_EXPR)
4079 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4080 else if (code == SSA_NAME)
4081 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4082 else if (TREE_CODE_CLASS (code) == tcc_binary)
4083 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4084 gimple_expr_type (stmt),
4085 gimple_assign_rhs1 (stmt),
4086 gimple_assign_rhs2 (stmt));
4087 else if (TREE_CODE_CLASS (code) == tcc_unary)
4088 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4089 gimple_expr_type (stmt),
4090 gimple_assign_rhs1 (stmt));
4091 else if (code == COND_EXPR)
4092 extract_range_from_cond_expr (vr, stmt);
4093 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4094 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4095 gimple_expr_type (stmt),
4096 gimple_assign_rhs1 (stmt),
4097 gimple_assign_rhs2 (stmt));
4098 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4099 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4100 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4101 else
4102 set_value_range_to_varying (vr);
4104 if (vr->type == VR_VARYING)
4105 extract_range_basic (vr, stmt);
4108 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4109 would be profitable to adjust VR using scalar evolution information
4110 for VAR. If so, update VR with the new limits. */
4112 static void
4113 adjust_range_with_scev (value_range *vr, struct loop *loop,
4114 gimple *stmt, tree var)
4116 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4117 enum ev_direction dir;
4119 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4120 better opportunities than a regular range, but I'm not sure. */
4121 if (vr->type == VR_ANTI_RANGE)
4122 return;
4124 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4126 /* Like in PR19590, scev can return a constant function. */
4127 if (is_gimple_min_invariant (chrec))
4129 set_value_range_to_value (vr, chrec, vr->equiv);
4130 return;
4133 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4134 return;
4136 init = initial_condition_in_loop_num (chrec, loop->num);
4137 tem = op_with_constant_singleton_value_range (init);
4138 if (tem)
4139 init = tem;
4140 step = evolution_part_in_loop_num (chrec, loop->num);
4141 tem = op_with_constant_singleton_value_range (step);
4142 if (tem)
4143 step = tem;
4145 /* If STEP is symbolic, we can't know whether INIT will be the
4146 minimum or maximum value in the range. Also, unless INIT is
4147 a simple expression, compare_values and possibly other functions
4148 in tree-vrp won't be able to handle it. */
4149 if (step == NULL_TREE
4150 || !is_gimple_min_invariant (step)
4151 || !valid_value_p (init))
4152 return;
4154 dir = scev_direction (chrec);
4155 if (/* Do not adjust ranges if we do not know whether the iv increases
4156 or decreases, ... */
4157 dir == EV_DIR_UNKNOWN
4158 /* ... or if it may wrap. */
4159 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4160 true))
4161 return;
4163 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4164 negative_overflow_infinity and positive_overflow_infinity,
4165 because we have concluded that the loop probably does not
4166 wrap. */
4168 type = TREE_TYPE (var);
4169 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4170 tmin = lower_bound_in_type (type, type);
4171 else
4172 tmin = TYPE_MIN_VALUE (type);
4173 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4174 tmax = upper_bound_in_type (type, type);
4175 else
4176 tmax = TYPE_MAX_VALUE (type);
4178 /* Try to use estimated number of iterations for the loop to constrain the
4179 final value in the evolution. */
4180 if (TREE_CODE (step) == INTEGER_CST
4181 && is_gimple_val (init)
4182 && (TREE_CODE (init) != SSA_NAME
4183 || get_value_range (init)->type == VR_RANGE))
4185 widest_int nit;
4187 /* We are only entering here for loop header PHI nodes, so using
4188 the number of latch executions is the correct thing to use. */
4189 if (max_loop_iterations (loop, &nit))
4191 value_range maxvr = VR_INITIALIZER;
4192 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4193 bool overflow;
4195 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4196 &overflow);
4197 /* If the multiplication overflowed we can't do a meaningful
4198 adjustment. Likewise if the result doesn't fit in the type
4199 of the induction variable. For a signed type we have to
4200 check whether the result has the expected signedness which
4201 is that of the step as number of iterations is unsigned. */
4202 if (!overflow
4203 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4204 && (sgn == UNSIGNED
4205 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4207 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4208 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4209 TREE_TYPE (init), init, tem);
4210 /* Likewise if the addition did. */
4211 if (maxvr.type == VR_RANGE)
4213 value_range initvr = VR_INITIALIZER;
4215 if (TREE_CODE (init) == SSA_NAME)
4216 initvr = *(get_value_range (init));
4217 else if (is_gimple_min_invariant (init))
4218 set_value_range_to_value (&initvr, init, NULL);
4219 else
4220 return;
4222 /* Check if init + nit * step overflows. Though we checked
4223 scev {init, step}_loop doesn't wrap, it is not enough
4224 because the loop may exit immediately. Overflow could
4225 happen in the plus expression in this case. */
4226 if ((dir == EV_DIR_DECREASES
4227 && (is_negative_overflow_infinity (maxvr.min)
4228 || compare_values (maxvr.min, initvr.min) != -1))
4229 || (dir == EV_DIR_GROWS
4230 && (is_positive_overflow_infinity (maxvr.max)
4231 || compare_values (maxvr.max, initvr.max) != 1)))
4232 return;
4234 tmin = maxvr.min;
4235 tmax = maxvr.max;
4241 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4243 min = tmin;
4244 max = tmax;
4246 /* For VARYING or UNDEFINED ranges, just about anything we get
4247 from scalar evolutions should be better. */
4249 if (dir == EV_DIR_DECREASES)
4250 max = init;
4251 else
4252 min = init;
4254 else if (vr->type == VR_RANGE)
4256 min = vr->min;
4257 max = vr->max;
4259 if (dir == EV_DIR_DECREASES)
4261 /* INIT is the maximum value. If INIT is lower than VR->MAX
4262 but no smaller than VR->MIN, set VR->MAX to INIT. */
4263 if (compare_values (init, max) == -1)
4264 max = init;
4266 /* According to the loop information, the variable does not
4267 overflow. If we think it does, probably because of an
4268 overflow due to arithmetic on a different INF value,
4269 reset now. */
4270 if (is_negative_overflow_infinity (min)
4271 || compare_values (min, tmin) == -1)
4272 min = tmin;
4275 else
4277 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4278 if (compare_values (init, min) == 1)
4279 min = init;
4281 if (is_positive_overflow_infinity (max)
4282 || compare_values (tmax, max) == -1)
4283 max = tmax;
4286 else
4287 return;
4289 /* If we just created an invalid range with the minimum
4290 greater than the maximum, we fail conservatively.
4291 This should happen only in unreachable
4292 parts of code, or for invalid programs. */
4293 if (compare_values (min, max) == 1
4294 || (is_negative_overflow_infinity (min)
4295 && is_positive_overflow_infinity (max)))
4296 return;
4298 /* Even for valid range info, sometimes overflow flag will leak in.
4299 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4300 drop them except for +-overflow_infinity which still need special
4301 handling in vrp pass. */
4302 if (TREE_OVERFLOW_P (min)
4303 && ! is_negative_overflow_infinity (min))
4304 min = drop_tree_overflow (min);
4305 if (TREE_OVERFLOW_P (max)
4306 && ! is_positive_overflow_infinity (max))
4307 max = drop_tree_overflow (max);
4309 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4313 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4315 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4316 all the values in the ranges.
4318 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4320 - Return NULL_TREE if it is not always possible to determine the
4321 value of the comparison.
4323 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4324 overflow infinity was used in the test. */
4327 static tree
4328 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4329 bool *strict_overflow_p)
4331 /* VARYING or UNDEFINED ranges cannot be compared. */
4332 if (vr0->type == VR_VARYING
4333 || vr0->type == VR_UNDEFINED
4334 || vr1->type == VR_VARYING
4335 || vr1->type == VR_UNDEFINED)
4336 return NULL_TREE;
4338 /* Anti-ranges need to be handled separately. */
4339 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4341 /* If both are anti-ranges, then we cannot compute any
4342 comparison. */
4343 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4344 return NULL_TREE;
4346 /* These comparisons are never statically computable. */
4347 if (comp == GT_EXPR
4348 || comp == GE_EXPR
4349 || comp == LT_EXPR
4350 || comp == LE_EXPR)
4351 return NULL_TREE;
4353 /* Equality can be computed only between a range and an
4354 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4355 if (vr0->type == VR_RANGE)
4357 /* To simplify processing, make VR0 the anti-range. */
4358 value_range *tmp = vr0;
4359 vr0 = vr1;
4360 vr1 = tmp;
4363 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4365 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4366 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4367 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4369 return NULL_TREE;
4372 if (!usable_range_p (vr0, strict_overflow_p)
4373 || !usable_range_p (vr1, strict_overflow_p))
4374 return NULL_TREE;
4376 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4377 operands around and change the comparison code. */
4378 if (comp == GT_EXPR || comp == GE_EXPR)
4380 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4381 std::swap (vr0, vr1);
4384 if (comp == EQ_EXPR)
4386 /* Equality may only be computed if both ranges represent
4387 exactly one value. */
4388 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4389 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4391 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4392 strict_overflow_p);
4393 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4394 strict_overflow_p);
4395 if (cmp_min == 0 && cmp_max == 0)
4396 return boolean_true_node;
4397 else if (cmp_min != -2 && cmp_max != -2)
4398 return boolean_false_node;
4400 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4401 else if (compare_values_warnv (vr0->min, vr1->max,
4402 strict_overflow_p) == 1
4403 || compare_values_warnv (vr1->min, vr0->max,
4404 strict_overflow_p) == 1)
4405 return boolean_false_node;
4407 return NULL_TREE;
4409 else if (comp == NE_EXPR)
4411 int cmp1, cmp2;
4413 /* If VR0 is completely to the left or completely to the right
4414 of VR1, they are always different. Notice that we need to
4415 make sure that both comparisons yield similar results to
4416 avoid comparing values that cannot be compared at
4417 compile-time. */
4418 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4419 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4420 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4421 return boolean_true_node;
4423 /* If VR0 and VR1 represent a single value and are identical,
4424 return false. */
4425 else if (compare_values_warnv (vr0->min, vr0->max,
4426 strict_overflow_p) == 0
4427 && compare_values_warnv (vr1->min, vr1->max,
4428 strict_overflow_p) == 0
4429 && compare_values_warnv (vr0->min, vr1->min,
4430 strict_overflow_p) == 0
4431 && compare_values_warnv (vr0->max, vr1->max,
4432 strict_overflow_p) == 0)
4433 return boolean_false_node;
4435 /* Otherwise, they may or may not be different. */
4436 else
4437 return NULL_TREE;
4439 else if (comp == LT_EXPR || comp == LE_EXPR)
4441 int tst;
4443 /* If VR0 is to the left of VR1, return true. */
4444 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4445 if ((comp == LT_EXPR && tst == -1)
4446 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4448 if (overflow_infinity_range_p (vr0)
4449 || overflow_infinity_range_p (vr1))
4450 *strict_overflow_p = true;
4451 return boolean_true_node;
4454 /* If VR0 is to the right of VR1, return false. */
4455 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4456 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4457 || (comp == LE_EXPR && tst == 1))
4459 if (overflow_infinity_range_p (vr0)
4460 || overflow_infinity_range_p (vr1))
4461 *strict_overflow_p = true;
4462 return boolean_false_node;
4465 /* Otherwise, we don't know. */
4466 return NULL_TREE;
4469 gcc_unreachable ();
4473 /* Given a value range VR, a value VAL and a comparison code COMP, return
4474 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4475 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4476 always returns false. Return NULL_TREE if it is not always
4477 possible to determine the value of the comparison. Also set
4478 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4479 infinity was used in the test. */
4481 static tree
4482 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4483 bool *strict_overflow_p)
4485 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4486 return NULL_TREE;
4488 /* Anti-ranges need to be handled separately. */
4489 if (vr->type == VR_ANTI_RANGE)
4491 /* For anti-ranges, the only predicates that we can compute at
4492 compile time are equality and inequality. */
4493 if (comp == GT_EXPR
4494 || comp == GE_EXPR
4495 || comp == LT_EXPR
4496 || comp == LE_EXPR)
4497 return NULL_TREE;
4499 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4500 if (value_inside_range (val, vr->min, vr->max) == 1)
4501 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4503 return NULL_TREE;
4506 if (!usable_range_p (vr, strict_overflow_p))
4507 return NULL_TREE;
4509 if (comp == EQ_EXPR)
4511 /* EQ_EXPR may only be computed if VR represents exactly
4512 one value. */
4513 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4515 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4516 if (cmp == 0)
4517 return boolean_true_node;
4518 else if (cmp == -1 || cmp == 1 || cmp == 2)
4519 return boolean_false_node;
4521 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4522 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4523 return boolean_false_node;
4525 return NULL_TREE;
4527 else if (comp == NE_EXPR)
4529 /* If VAL is not inside VR, then they are always different. */
4530 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4531 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4532 return boolean_true_node;
4534 /* If VR represents exactly one value equal to VAL, then return
4535 false. */
4536 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4537 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4538 return boolean_false_node;
4540 /* Otherwise, they may or may not be different. */
4541 return NULL_TREE;
4543 else if (comp == LT_EXPR || comp == LE_EXPR)
4545 int tst;
4547 /* If VR is to the left of VAL, return true. */
4548 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4549 if ((comp == LT_EXPR && tst == -1)
4550 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4552 if (overflow_infinity_range_p (vr))
4553 *strict_overflow_p = true;
4554 return boolean_true_node;
4557 /* If VR is to the right of VAL, return false. */
4558 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4559 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4560 || (comp == LE_EXPR && tst == 1))
4562 if (overflow_infinity_range_p (vr))
4563 *strict_overflow_p = true;
4564 return boolean_false_node;
4567 /* Otherwise, we don't know. */
4568 return NULL_TREE;
4570 else if (comp == GT_EXPR || comp == GE_EXPR)
4572 int tst;
4574 /* If VR is to the right of VAL, return true. */
4575 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4576 if ((comp == GT_EXPR && tst == 1)
4577 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4579 if (overflow_infinity_range_p (vr))
4580 *strict_overflow_p = true;
4581 return boolean_true_node;
4584 /* If VR is to the left of VAL, return false. */
4585 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4586 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4587 || (comp == GE_EXPR && tst == -1))
4589 if (overflow_infinity_range_p (vr))
4590 *strict_overflow_p = true;
4591 return boolean_false_node;
4594 /* Otherwise, we don't know. */
4595 return NULL_TREE;
4598 gcc_unreachable ();
4602 /* Debugging dumps. */
4604 void dump_value_range (FILE *, value_range *);
4605 void debug_value_range (value_range *);
4606 void dump_all_value_ranges (FILE *);
4607 void debug_all_value_ranges (void);
4608 void dump_vr_equiv (FILE *, bitmap);
4609 void debug_vr_equiv (bitmap);
4612 /* Dump value range VR to FILE. */
4614 void
4615 dump_value_range (FILE *file, value_range *vr)
4617 if (vr == NULL)
4618 fprintf (file, "[]");
4619 else if (vr->type == VR_UNDEFINED)
4620 fprintf (file, "UNDEFINED");
4621 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4623 tree type = TREE_TYPE (vr->min);
4625 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4627 if (is_negative_overflow_infinity (vr->min))
4628 fprintf (file, "-INF(OVF)");
4629 else if (INTEGRAL_TYPE_P (type)
4630 && !TYPE_UNSIGNED (type)
4631 && vrp_val_is_min (vr->min))
4632 fprintf (file, "-INF");
4633 else
4634 print_generic_expr (file, vr->min, 0);
4636 fprintf (file, ", ");
4638 if (is_positive_overflow_infinity (vr->max))
4639 fprintf (file, "+INF(OVF)");
4640 else if (INTEGRAL_TYPE_P (type)
4641 && vrp_val_is_max (vr->max))
4642 fprintf (file, "+INF");
4643 else
4644 print_generic_expr (file, vr->max, 0);
4646 fprintf (file, "]");
4648 if (vr->equiv)
4650 bitmap_iterator bi;
4651 unsigned i, c = 0;
4653 fprintf (file, " EQUIVALENCES: { ");
4655 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4657 print_generic_expr (file, ssa_name (i), 0);
4658 fprintf (file, " ");
4659 c++;
4662 fprintf (file, "} (%u elements)", c);
4665 else if (vr->type == VR_VARYING)
4666 fprintf (file, "VARYING");
4667 else
4668 fprintf (file, "INVALID RANGE");
4672 /* Dump value range VR to stderr. */
4674 DEBUG_FUNCTION void
4675 debug_value_range (value_range *vr)
4677 dump_value_range (stderr, vr);
4678 fprintf (stderr, "\n");
4682 /* Dump value ranges of all SSA_NAMEs to FILE. */
4684 void
4685 dump_all_value_ranges (FILE *file)
4687 size_t i;
4689 for (i = 0; i < num_vr_values; i++)
4691 if (vr_value[i])
4693 print_generic_expr (file, ssa_name (i), 0);
4694 fprintf (file, ": ");
4695 dump_value_range (file, vr_value[i]);
4696 fprintf (file, "\n");
4700 fprintf (file, "\n");
4704 /* Dump all value ranges to stderr. */
4706 DEBUG_FUNCTION void
4707 debug_all_value_ranges (void)
4709 dump_all_value_ranges (stderr);
4713 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4714 create a new SSA name N and return the assertion assignment
4715 'N = ASSERT_EXPR <V, V OP W>'. */
4717 static gimple *
4718 build_assert_expr_for (tree cond, tree v)
4720 tree a;
4721 gassign *assertion;
4723 gcc_assert (TREE_CODE (v) == SSA_NAME
4724 && COMPARISON_CLASS_P (cond));
4726 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4727 assertion = gimple_build_assign (NULL_TREE, a);
4729 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4730 operand of the ASSERT_EXPR. Create it so the new name and the old one
4731 are registered in the replacement table so that we can fix the SSA web
4732 after adding all the ASSERT_EXPRs. */
4733 create_new_def_for (v, assertion, NULL);
4735 return assertion;
4739 /* Return false if EXPR is a predicate expression involving floating
4740 point values. */
4742 static inline bool
4743 fp_predicate (gimple *stmt)
4745 GIMPLE_CHECK (stmt, GIMPLE_COND);
4747 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4750 /* If the range of values taken by OP can be inferred after STMT executes,
4751 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4752 describes the inferred range. Return true if a range could be
4753 inferred. */
4755 static bool
4756 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4758 *val_p = NULL_TREE;
4759 *comp_code_p = ERROR_MARK;
4761 /* Do not attempt to infer anything in names that flow through
4762 abnormal edges. */
4763 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4764 return false;
4766 /* Similarly, don't infer anything from statements that may throw
4767 exceptions. ??? Relax this requirement? */
4768 if (stmt_could_throw_p (stmt))
4769 return false;
4771 /* If STMT is the last statement of a basic block with no normal
4772 successors, there is no point inferring anything about any of its
4773 operands. We would not be able to find a proper insertion point
4774 for the assertion, anyway. */
4775 if (stmt_ends_bb_p (stmt))
4777 edge_iterator ei;
4778 edge e;
4780 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4781 if (!(e->flags & EDGE_ABNORMAL))
4782 break;
4783 if (e == NULL)
4784 return false;
4787 if (infer_nonnull_range (stmt, op))
4789 *val_p = build_int_cst (TREE_TYPE (op), 0);
4790 *comp_code_p = NE_EXPR;
4791 return true;
4794 return false;
4798 void dump_asserts_for (FILE *, tree);
4799 void debug_asserts_for (tree);
4800 void dump_all_asserts (FILE *);
4801 void debug_all_asserts (void);
4803 /* Dump all the registered assertions for NAME to FILE. */
4805 void
4806 dump_asserts_for (FILE *file, tree name)
4808 assert_locus *loc;
4810 fprintf (file, "Assertions to be inserted for ");
4811 print_generic_expr (file, name, 0);
4812 fprintf (file, "\n");
4814 loc = asserts_for[SSA_NAME_VERSION (name)];
4815 while (loc)
4817 fprintf (file, "\t");
4818 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4819 fprintf (file, "\n\tBB #%d", loc->bb->index);
4820 if (loc->e)
4822 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4823 loc->e->dest->index);
4824 dump_edge_info (file, loc->e, dump_flags, 0);
4826 fprintf (file, "\n\tPREDICATE: ");
4827 print_generic_expr (file, name, 0);
4828 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4829 print_generic_expr (file, loc->val, 0);
4830 fprintf (file, "\n\n");
4831 loc = loc->next;
4834 fprintf (file, "\n");
4838 /* Dump all the registered assertions for NAME to stderr. */
4840 DEBUG_FUNCTION void
4841 debug_asserts_for (tree name)
4843 dump_asserts_for (stderr, name);
4847 /* Dump all the registered assertions for all the names to FILE. */
4849 void
4850 dump_all_asserts (FILE *file)
4852 unsigned i;
4853 bitmap_iterator bi;
4855 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4856 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4857 dump_asserts_for (file, ssa_name (i));
4858 fprintf (file, "\n");
4862 /* Dump all the registered assertions for all the names to stderr. */
4864 DEBUG_FUNCTION void
4865 debug_all_asserts (void)
4867 dump_all_asserts (stderr);
4871 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4872 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4873 E->DEST, then register this location as a possible insertion point
4874 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4876 BB, E and SI provide the exact insertion point for the new
4877 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4878 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4879 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4880 must not be NULL. */
4882 static void
4883 register_new_assert_for (tree name, tree expr,
4884 enum tree_code comp_code,
4885 tree val,
4886 basic_block bb,
4887 edge e,
4888 gimple_stmt_iterator si)
4890 assert_locus *n, *loc, *last_loc;
4891 basic_block dest_bb;
4893 gcc_checking_assert (bb == NULL || e == NULL);
4895 if (e == NULL)
4896 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4897 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4899 /* Never build an assert comparing against an integer constant with
4900 TREE_OVERFLOW set. This confuses our undefined overflow warning
4901 machinery. */
4902 if (TREE_OVERFLOW_P (val))
4903 val = drop_tree_overflow (val);
4905 /* The new assertion A will be inserted at BB or E. We need to
4906 determine if the new location is dominated by a previously
4907 registered location for A. If we are doing an edge insertion,
4908 assume that A will be inserted at E->DEST. Note that this is not
4909 necessarily true.
4911 If E is a critical edge, it will be split. But even if E is
4912 split, the new block will dominate the same set of blocks that
4913 E->DEST dominates.
4915 The reverse, however, is not true, blocks dominated by E->DEST
4916 will not be dominated by the new block created to split E. So,
4917 if the insertion location is on a critical edge, we will not use
4918 the new location to move another assertion previously registered
4919 at a block dominated by E->DEST. */
4920 dest_bb = (bb) ? bb : e->dest;
4922 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4923 VAL at a block dominating DEST_BB, then we don't need to insert a new
4924 one. Similarly, if the same assertion already exists at a block
4925 dominated by DEST_BB and the new location is not on a critical
4926 edge, then update the existing location for the assertion (i.e.,
4927 move the assertion up in the dominance tree).
4929 Note, this is implemented as a simple linked list because there
4930 should not be more than a handful of assertions registered per
4931 name. If this becomes a performance problem, a table hashed by
4932 COMP_CODE and VAL could be implemented. */
4933 loc = asserts_for[SSA_NAME_VERSION (name)];
4934 last_loc = loc;
4935 while (loc)
4937 if (loc->comp_code == comp_code
4938 && (loc->val == val
4939 || operand_equal_p (loc->val, val, 0))
4940 && (loc->expr == expr
4941 || operand_equal_p (loc->expr, expr, 0)))
4943 /* If E is not a critical edge and DEST_BB
4944 dominates the existing location for the assertion, move
4945 the assertion up in the dominance tree by updating its
4946 location information. */
4947 if ((e == NULL || !EDGE_CRITICAL_P (e))
4948 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4950 loc->bb = dest_bb;
4951 loc->e = e;
4952 loc->si = si;
4953 return;
4957 /* Update the last node of the list and move to the next one. */
4958 last_loc = loc;
4959 loc = loc->next;
4962 /* If we didn't find an assertion already registered for
4963 NAME COMP_CODE VAL, add a new one at the end of the list of
4964 assertions associated with NAME. */
4965 n = XNEW (struct assert_locus);
4966 n->bb = dest_bb;
4967 n->e = e;
4968 n->si = si;
4969 n->comp_code = comp_code;
4970 n->val = val;
4971 n->expr = expr;
4972 n->next = NULL;
4974 if (last_loc)
4975 last_loc->next = n;
4976 else
4977 asserts_for[SSA_NAME_VERSION (name)] = n;
4979 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4982 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4983 Extract a suitable test code and value and store them into *CODE_P and
4984 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4986 If no extraction was possible, return FALSE, otherwise return TRUE.
4988 If INVERT is true, then we invert the result stored into *CODE_P. */
4990 static bool
4991 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4992 tree cond_op0, tree cond_op1,
4993 bool invert, enum tree_code *code_p,
4994 tree *val_p)
4996 enum tree_code comp_code;
4997 tree val;
4999 /* Otherwise, we have a comparison of the form NAME COMP VAL
5000 or VAL COMP NAME. */
5001 if (name == cond_op1)
5003 /* If the predicate is of the form VAL COMP NAME, flip
5004 COMP around because we need to register NAME as the
5005 first operand in the predicate. */
5006 comp_code = swap_tree_comparison (cond_code);
5007 val = cond_op0;
5009 else
5011 /* The comparison is of the form NAME COMP VAL, so the
5012 comparison code remains unchanged. */
5013 comp_code = cond_code;
5014 val = cond_op1;
5017 /* Invert the comparison code as necessary. */
5018 if (invert)
5019 comp_code = invert_tree_comparison (comp_code, 0);
5021 /* VRP only handles integral and pointer types. */
5022 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
5023 && ! POINTER_TYPE_P (TREE_TYPE (val)))
5024 return false;
5026 /* Do not register always-false predicates.
5027 FIXME: this works around a limitation in fold() when dealing with
5028 enumerations. Given 'enum { N1, N2 } x;', fold will not
5029 fold 'if (x > N2)' to 'if (0)'. */
5030 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5031 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5033 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5034 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5036 if (comp_code == GT_EXPR
5037 && (!max
5038 || compare_values (val, max) == 0))
5039 return false;
5041 if (comp_code == LT_EXPR
5042 && (!min
5043 || compare_values (val, min) == 0))
5044 return false;
5046 *code_p = comp_code;
5047 *val_p = val;
5048 return true;
5051 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5052 (otherwise return VAL). VAL and MASK must be zero-extended for
5053 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5054 (to transform signed values into unsigned) and at the end xor
5055 SGNBIT back. */
5057 static wide_int
5058 masked_increment (const wide_int &val_in, const wide_int &mask,
5059 const wide_int &sgnbit, unsigned int prec)
5061 wide_int bit = wi::one (prec), res;
5062 unsigned int i;
5064 wide_int val = val_in ^ sgnbit;
5065 for (i = 0; i < prec; i++, bit += bit)
5067 res = mask;
5068 if ((res & bit) == 0)
5069 continue;
5070 res = bit - 1;
5071 res = (val + bit).and_not (res);
5072 res &= mask;
5073 if (wi::gtu_p (res, val))
5074 return res ^ sgnbit;
5076 return val ^ sgnbit;
5079 /* Try to register an edge assertion for SSA name NAME on edge E for
5080 the condition COND contributing to the conditional jump pointed to by BSI.
5081 Invert the condition COND if INVERT is true. */
5083 static void
5084 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5085 enum tree_code cond_code,
5086 tree cond_op0, tree cond_op1, bool invert)
5088 tree val;
5089 enum tree_code comp_code;
5091 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5092 cond_op0,
5093 cond_op1,
5094 invert, &comp_code, &val))
5095 return;
5097 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5098 reachable from E. */
5099 if (live_on_edge (e, name))
5100 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5102 /* In the case of NAME <= CST and NAME being defined as
5103 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5104 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5105 This catches range and anti-range tests. */
5106 if ((comp_code == LE_EXPR
5107 || comp_code == GT_EXPR)
5108 && TREE_CODE (val) == INTEGER_CST
5109 && TYPE_UNSIGNED (TREE_TYPE (val)))
5111 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5112 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5114 /* Extract CST2 from the (optional) addition. */
5115 if (is_gimple_assign (def_stmt)
5116 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5118 name2 = gimple_assign_rhs1 (def_stmt);
5119 cst2 = gimple_assign_rhs2 (def_stmt);
5120 if (TREE_CODE (name2) == SSA_NAME
5121 && TREE_CODE (cst2) == INTEGER_CST)
5122 def_stmt = SSA_NAME_DEF_STMT (name2);
5125 /* Extract NAME2 from the (optional) sign-changing cast. */
5126 if (gimple_assign_cast_p (def_stmt))
5128 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5129 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5130 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5131 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5132 name3 = gimple_assign_rhs1 (def_stmt);
5135 /* If name3 is used later, create an ASSERT_EXPR for it. */
5136 if (name3 != NULL_TREE
5137 && TREE_CODE (name3) == SSA_NAME
5138 && (cst2 == NULL_TREE
5139 || TREE_CODE (cst2) == INTEGER_CST)
5140 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5141 && live_on_edge (e, name3))
5143 tree tmp;
5145 /* Build an expression for the range test. */
5146 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5147 if (cst2 != NULL_TREE)
5148 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5150 if (dump_file)
5152 fprintf (dump_file, "Adding assert for ");
5153 print_generic_expr (dump_file, name3, 0);
5154 fprintf (dump_file, " from ");
5155 print_generic_expr (dump_file, tmp, 0);
5156 fprintf (dump_file, "\n");
5159 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5162 /* If name2 is used later, create an ASSERT_EXPR for it. */
5163 if (name2 != NULL_TREE
5164 && TREE_CODE (name2) == SSA_NAME
5165 && TREE_CODE (cst2) == INTEGER_CST
5166 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5167 && live_on_edge (e, name2))
5169 tree tmp;
5171 /* Build an expression for the range test. */
5172 tmp = name2;
5173 if (TREE_TYPE (name) != TREE_TYPE (name2))
5174 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5175 if (cst2 != NULL_TREE)
5176 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5178 if (dump_file)
5180 fprintf (dump_file, "Adding assert for ");
5181 print_generic_expr (dump_file, name2, 0);
5182 fprintf (dump_file, " from ");
5183 print_generic_expr (dump_file, tmp, 0);
5184 fprintf (dump_file, "\n");
5187 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5191 /* In the case of post-in/decrement tests like if (i++) ... and uses
5192 of the in/decremented value on the edge the extra name we want to
5193 assert for is not on the def chain of the name compared. Instead
5194 it is in the set of use stmts.
5195 Similar cases happen for conversions that were simplified through
5196 fold_{sign_changed,widened}_comparison. */
5197 if ((comp_code == NE_EXPR
5198 || comp_code == EQ_EXPR)
5199 && TREE_CODE (val) == INTEGER_CST)
5201 imm_use_iterator ui;
5202 gimple *use_stmt;
5203 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5205 if (!is_gimple_assign (use_stmt))
5206 continue;
5208 /* Cut off to use-stmts that are dominating the predecessor. */
5209 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5210 continue;
5212 tree name2 = gimple_assign_lhs (use_stmt);
5213 if (TREE_CODE (name2) != SSA_NAME
5214 || !live_on_edge (e, name2))
5215 continue;
5217 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5218 tree cst;
5219 if (code == PLUS_EXPR
5220 || code == MINUS_EXPR)
5222 cst = gimple_assign_rhs2 (use_stmt);
5223 if (TREE_CODE (cst) != INTEGER_CST)
5224 continue;
5225 cst = int_const_binop (code, val, cst);
5227 else if (CONVERT_EXPR_CODE_P (code))
5229 /* For truncating conversions we cannot record
5230 an inequality. */
5231 if (comp_code == NE_EXPR
5232 && (TYPE_PRECISION (TREE_TYPE (name2))
5233 < TYPE_PRECISION (TREE_TYPE (name))))
5234 continue;
5235 cst = fold_convert (TREE_TYPE (name2), val);
5237 else
5238 continue;
5240 if (TREE_OVERFLOW_P (cst))
5241 cst = drop_tree_overflow (cst);
5242 register_new_assert_for (name2, name2, comp_code, cst,
5243 NULL, e, bsi);
5247 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5248 && TREE_CODE (val) == INTEGER_CST)
5250 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5251 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5252 tree val2 = NULL_TREE;
5253 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5254 wide_int mask = wi::zero (prec);
5255 unsigned int nprec = prec;
5256 enum tree_code rhs_code = ERROR_MARK;
5258 if (is_gimple_assign (def_stmt))
5259 rhs_code = gimple_assign_rhs_code (def_stmt);
5261 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5262 assert that A != CST1 -+ CST2. */
5263 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5264 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
5266 tree op0 = gimple_assign_rhs1 (def_stmt);
5267 tree op1 = gimple_assign_rhs2 (def_stmt);
5268 if (TREE_CODE (op0) == SSA_NAME
5269 && TREE_CODE (op1) == INTEGER_CST
5270 && live_on_edge (e, op0))
5272 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
5273 ? MINUS_EXPR : PLUS_EXPR);
5274 op1 = int_const_binop (reverse_op, val, op1);
5275 if (TREE_OVERFLOW (op1))
5276 op1 = drop_tree_overflow (op1);
5277 register_new_assert_for (op0, op0, comp_code, op1, NULL, e, bsi);
5281 /* Add asserts for NAME cmp CST and NAME being defined
5282 as NAME = (int) NAME2. */
5283 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5284 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5285 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5286 && gimple_assign_cast_p (def_stmt))
5288 name2 = gimple_assign_rhs1 (def_stmt);
5289 if (CONVERT_EXPR_CODE_P (rhs_code)
5290 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5291 && TYPE_UNSIGNED (TREE_TYPE (name2))
5292 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5293 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5294 || !tree_int_cst_equal (val,
5295 TYPE_MIN_VALUE (TREE_TYPE (val))))
5296 && live_on_edge (e, name2))
5298 tree tmp, cst;
5299 enum tree_code new_comp_code = comp_code;
5301 cst = fold_convert (TREE_TYPE (name2),
5302 TYPE_MIN_VALUE (TREE_TYPE (val)));
5303 /* Build an expression for the range test. */
5304 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5305 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5306 fold_convert (TREE_TYPE (name2), val));
5307 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5309 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5310 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5311 build_int_cst (TREE_TYPE (name2), 1));
5314 if (dump_file)
5316 fprintf (dump_file, "Adding assert for ");
5317 print_generic_expr (dump_file, name2, 0);
5318 fprintf (dump_file, " from ");
5319 print_generic_expr (dump_file, tmp, 0);
5320 fprintf (dump_file, "\n");
5323 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5324 e, bsi);
5328 /* Add asserts for NAME cmp CST and NAME being defined as
5329 NAME = NAME2 >> CST2.
5331 Extract CST2 from the right shift. */
5332 if (rhs_code == RSHIFT_EXPR)
5334 name2 = gimple_assign_rhs1 (def_stmt);
5335 cst2 = gimple_assign_rhs2 (def_stmt);
5336 if (TREE_CODE (name2) == SSA_NAME
5337 && tree_fits_uhwi_p (cst2)
5338 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5339 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5340 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5341 && live_on_edge (e, name2))
5343 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5344 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5347 if (val2 != NULL_TREE
5348 && TREE_CODE (val2) == INTEGER_CST
5349 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5350 TREE_TYPE (val),
5351 val2, cst2), val))
5353 enum tree_code new_comp_code = comp_code;
5354 tree tmp, new_val;
5356 tmp = name2;
5357 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5359 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5361 tree type = build_nonstandard_integer_type (prec, 1);
5362 tmp = build1 (NOP_EXPR, type, name2);
5363 val2 = fold_convert (type, val2);
5365 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5366 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5367 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5369 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5371 wide_int minval
5372 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5373 new_val = val2;
5374 if (minval == new_val)
5375 new_val = NULL_TREE;
5377 else
5379 wide_int maxval
5380 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5381 mask |= val2;
5382 if (mask == maxval)
5383 new_val = NULL_TREE;
5384 else
5385 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5388 if (new_val)
5390 if (dump_file)
5392 fprintf (dump_file, "Adding assert for ");
5393 print_generic_expr (dump_file, name2, 0);
5394 fprintf (dump_file, " from ");
5395 print_generic_expr (dump_file, tmp, 0);
5396 fprintf (dump_file, "\n");
5399 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5400 NULL, e, bsi);
5404 /* Add asserts for NAME cmp CST and NAME being defined as
5405 NAME = NAME2 & CST2.
5407 Extract CST2 from the and.
5409 Also handle
5410 NAME = (unsigned) NAME2;
5411 casts where NAME's type is unsigned and has smaller precision
5412 than NAME2's type as if it was NAME = NAME2 & MASK. */
5413 names[0] = NULL_TREE;
5414 names[1] = NULL_TREE;
5415 cst2 = NULL_TREE;
5416 if (rhs_code == BIT_AND_EXPR
5417 || (CONVERT_EXPR_CODE_P (rhs_code)
5418 && INTEGRAL_TYPE_P (TREE_TYPE (val))
5419 && TYPE_UNSIGNED (TREE_TYPE (val))
5420 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5421 > prec))
5423 name2 = gimple_assign_rhs1 (def_stmt);
5424 if (rhs_code == BIT_AND_EXPR)
5425 cst2 = gimple_assign_rhs2 (def_stmt);
5426 else
5428 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5429 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5431 if (TREE_CODE (name2) == SSA_NAME
5432 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5433 && TREE_CODE (cst2) == INTEGER_CST
5434 && !integer_zerop (cst2)
5435 && (nprec > 1
5436 || TYPE_UNSIGNED (TREE_TYPE (val))))
5438 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5439 if (gimple_assign_cast_p (def_stmt2))
5441 names[1] = gimple_assign_rhs1 (def_stmt2);
5442 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5443 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5444 || (TYPE_PRECISION (TREE_TYPE (name2))
5445 != TYPE_PRECISION (TREE_TYPE (names[1])))
5446 || !live_on_edge (e, names[1]))
5447 names[1] = NULL_TREE;
5449 if (live_on_edge (e, name2))
5450 names[0] = name2;
5453 if (names[0] || names[1])
5455 wide_int minv, maxv, valv, cst2v;
5456 wide_int tem, sgnbit;
5457 bool valid_p = false, valn, cst2n;
5458 enum tree_code ccode = comp_code;
5460 valv = wide_int::from (val, nprec, UNSIGNED);
5461 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5462 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5463 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5464 /* If CST2 doesn't have most significant bit set,
5465 but VAL is negative, we have comparison like
5466 if ((x & 0x123) > -4) (always true). Just give up. */
5467 if (!cst2n && valn)
5468 ccode = ERROR_MARK;
5469 if (cst2n)
5470 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5471 else
5472 sgnbit = wi::zero (nprec);
5473 minv = valv & cst2v;
5474 switch (ccode)
5476 case EQ_EXPR:
5477 /* Minimum unsigned value for equality is VAL & CST2
5478 (should be equal to VAL, otherwise we probably should
5479 have folded the comparison into false) and
5480 maximum unsigned value is VAL | ~CST2. */
5481 maxv = valv | ~cst2v;
5482 valid_p = true;
5483 break;
5485 case NE_EXPR:
5486 tem = valv | ~cst2v;
5487 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5488 if (valv == 0)
5490 cst2n = false;
5491 sgnbit = wi::zero (nprec);
5492 goto gt_expr;
5494 /* If (VAL | ~CST2) is all ones, handle it as
5495 (X & CST2) < VAL. */
5496 if (tem == -1)
5498 cst2n = false;
5499 valn = false;
5500 sgnbit = wi::zero (nprec);
5501 goto lt_expr;
5503 if (!cst2n && wi::neg_p (cst2v))
5504 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5505 if (sgnbit != 0)
5507 if (valv == sgnbit)
5509 cst2n = true;
5510 valn = true;
5511 goto gt_expr;
5513 if (tem == wi::mask (nprec - 1, false, nprec))
5515 cst2n = true;
5516 goto lt_expr;
5518 if (!cst2n)
5519 sgnbit = wi::zero (nprec);
5521 break;
5523 case GE_EXPR:
5524 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5525 is VAL and maximum unsigned value is ~0. For signed
5526 comparison, if CST2 doesn't have most significant bit
5527 set, handle it similarly. If CST2 has MSB set,
5528 the minimum is the same, and maximum is ~0U/2. */
5529 if (minv != valv)
5531 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5532 VAL. */
5533 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5534 if (minv == valv)
5535 break;
5537 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5538 valid_p = true;
5539 break;
5541 case GT_EXPR:
5542 gt_expr:
5543 /* Find out smallest MINV where MINV > VAL
5544 && (MINV & CST2) == MINV, if any. If VAL is signed and
5545 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5546 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5547 if (minv == valv)
5548 break;
5549 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5550 valid_p = true;
5551 break;
5553 case LE_EXPR:
5554 /* Minimum unsigned value for <= is 0 and maximum
5555 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5556 Otherwise, find smallest VAL2 where VAL2 > VAL
5557 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5558 as maximum.
5559 For signed comparison, if CST2 doesn't have most
5560 significant bit set, handle it similarly. If CST2 has
5561 MSB set, the maximum is the same and minimum is INT_MIN. */
5562 if (minv == valv)
5563 maxv = valv;
5564 else
5566 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5567 if (maxv == valv)
5568 break;
5569 maxv -= 1;
5571 maxv |= ~cst2v;
5572 minv = sgnbit;
5573 valid_p = true;
5574 break;
5576 case LT_EXPR:
5577 lt_expr:
5578 /* Minimum unsigned value for < is 0 and maximum
5579 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5580 Otherwise, find smallest VAL2 where VAL2 > VAL
5581 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5582 as maximum.
5583 For signed comparison, if CST2 doesn't have most
5584 significant bit set, handle it similarly. If CST2 has
5585 MSB set, the maximum is the same and minimum is INT_MIN. */
5586 if (minv == valv)
5588 if (valv == sgnbit)
5589 break;
5590 maxv = valv;
5592 else
5594 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5595 if (maxv == valv)
5596 break;
5598 maxv -= 1;
5599 maxv |= ~cst2v;
5600 minv = sgnbit;
5601 valid_p = true;
5602 break;
5604 default:
5605 break;
5607 if (valid_p
5608 && (maxv - minv) != -1)
5610 tree tmp, new_val, type;
5611 int i;
5613 for (i = 0; i < 2; i++)
5614 if (names[i])
5616 wide_int maxv2 = maxv;
5617 tmp = names[i];
5618 type = TREE_TYPE (names[i]);
5619 if (!TYPE_UNSIGNED (type))
5621 type = build_nonstandard_integer_type (nprec, 1);
5622 tmp = build1 (NOP_EXPR, type, names[i]);
5624 if (minv != 0)
5626 tmp = build2 (PLUS_EXPR, type, tmp,
5627 wide_int_to_tree (type, -minv));
5628 maxv2 = maxv - minv;
5630 new_val = wide_int_to_tree (type, maxv2);
5632 if (dump_file)
5634 fprintf (dump_file, "Adding assert for ");
5635 print_generic_expr (dump_file, names[i], 0);
5636 fprintf (dump_file, " from ");
5637 print_generic_expr (dump_file, tmp, 0);
5638 fprintf (dump_file, "\n");
5641 register_new_assert_for (names[i], tmp, LE_EXPR,
5642 new_val, NULL, e, bsi);
5649 /* OP is an operand of a truth value expression which is known to have
5650 a particular value. Register any asserts for OP and for any
5651 operands in OP's defining statement.
5653 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5654 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5656 static void
5657 register_edge_assert_for_1 (tree op, enum tree_code code,
5658 edge e, gimple_stmt_iterator bsi)
5660 gimple *op_def;
5661 tree val;
5662 enum tree_code rhs_code;
5664 /* We only care about SSA_NAMEs. */
5665 if (TREE_CODE (op) != SSA_NAME)
5666 return;
5668 /* We know that OP will have a zero or nonzero value. If OP is used
5669 more than once go ahead and register an assert for OP. */
5670 if (live_on_edge (e, op))
5672 val = build_int_cst (TREE_TYPE (op), 0);
5673 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5676 /* Now look at how OP is set. If it's set from a comparison,
5677 a truth operation or some bit operations, then we may be able
5678 to register information about the operands of that assignment. */
5679 op_def = SSA_NAME_DEF_STMT (op);
5680 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5681 return;
5683 rhs_code = gimple_assign_rhs_code (op_def);
5685 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5687 bool invert = (code == EQ_EXPR ? true : false);
5688 tree op0 = gimple_assign_rhs1 (op_def);
5689 tree op1 = gimple_assign_rhs2 (op_def);
5691 if (TREE_CODE (op0) == SSA_NAME)
5692 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5693 if (TREE_CODE (op1) == SSA_NAME)
5694 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5696 else if ((code == NE_EXPR
5697 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5698 || (code == EQ_EXPR
5699 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5701 /* Recurse on each operand. */
5702 tree op0 = gimple_assign_rhs1 (op_def);
5703 tree op1 = gimple_assign_rhs2 (op_def);
5704 if (TREE_CODE (op0) == SSA_NAME
5705 && has_single_use (op0))
5706 register_edge_assert_for_1 (op0, code, e, bsi);
5707 if (TREE_CODE (op1) == SSA_NAME
5708 && has_single_use (op1))
5709 register_edge_assert_for_1 (op1, code, e, bsi);
5711 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5712 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5714 /* Recurse, flipping CODE. */
5715 code = invert_tree_comparison (code, false);
5716 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5718 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5720 /* Recurse through the copy. */
5721 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5723 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5725 /* Recurse through the type conversion, unless it is a narrowing
5726 conversion or conversion from non-integral type. */
5727 tree rhs = gimple_assign_rhs1 (op_def);
5728 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5729 && (TYPE_PRECISION (TREE_TYPE (rhs))
5730 <= TYPE_PRECISION (TREE_TYPE (op))))
5731 register_edge_assert_for_1 (rhs, code, e, bsi);
5735 /* Try to register an edge assertion for SSA name NAME on edge E for
5736 the condition COND contributing to the conditional jump pointed to by
5737 SI. */
5739 static void
5740 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5741 enum tree_code cond_code, tree cond_op0,
5742 tree cond_op1)
5744 tree val;
5745 enum tree_code comp_code;
5746 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5748 /* Do not attempt to infer anything in names that flow through
5749 abnormal edges. */
5750 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5751 return;
5753 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5754 cond_op0, cond_op1,
5755 is_else_edge,
5756 &comp_code, &val))
5757 return;
5759 /* Register ASSERT_EXPRs for name. */
5760 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5761 cond_op1, is_else_edge);
5764 /* If COND is effectively an equality test of an SSA_NAME against
5765 the value zero or one, then we may be able to assert values
5766 for SSA_NAMEs which flow into COND. */
5768 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5769 statement of NAME we can assert both operands of the BIT_AND_EXPR
5770 have nonzero value. */
5771 if (((comp_code == EQ_EXPR && integer_onep (val))
5772 || (comp_code == NE_EXPR && integer_zerop (val))))
5774 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5776 if (is_gimple_assign (def_stmt)
5777 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5779 tree op0 = gimple_assign_rhs1 (def_stmt);
5780 tree op1 = gimple_assign_rhs2 (def_stmt);
5781 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5782 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5786 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5787 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5788 have zero value. */
5789 if (((comp_code == EQ_EXPR && integer_zerop (val))
5790 || (comp_code == NE_EXPR && integer_onep (val))))
5792 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5794 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5795 necessarily zero value, or if type-precision is one. */
5796 if (is_gimple_assign (def_stmt)
5797 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5798 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5799 || comp_code == EQ_EXPR)))
5801 tree op0 = gimple_assign_rhs1 (def_stmt);
5802 tree op1 = gimple_assign_rhs2 (def_stmt);
5803 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5804 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5810 /* Determine whether the outgoing edges of BB should receive an
5811 ASSERT_EXPR for each of the operands of BB's LAST statement.
5812 The last statement of BB must be a COND_EXPR.
5814 If any of the sub-graphs rooted at BB have an interesting use of
5815 the predicate operands, an assert location node is added to the
5816 list of assertions for the corresponding operands. */
5818 static void
5819 find_conditional_asserts (basic_block bb, gcond *last)
5821 gimple_stmt_iterator bsi;
5822 tree op;
5823 edge_iterator ei;
5824 edge e;
5825 ssa_op_iter iter;
5827 bsi = gsi_for_stmt (last);
5829 /* Look for uses of the operands in each of the sub-graphs
5830 rooted at BB. We need to check each of the outgoing edges
5831 separately, so that we know what kind of ASSERT_EXPR to
5832 insert. */
5833 FOR_EACH_EDGE (e, ei, bb->succs)
5835 if (e->dest == bb)
5836 continue;
5838 /* Register the necessary assertions for each operand in the
5839 conditional predicate. */
5840 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5841 register_edge_assert_for (op, e, bsi,
5842 gimple_cond_code (last),
5843 gimple_cond_lhs (last),
5844 gimple_cond_rhs (last));
5848 struct case_info
5850 tree expr;
5851 basic_block bb;
5854 /* Compare two case labels sorting first by the destination bb index
5855 and then by the case value. */
5857 static int
5858 compare_case_labels (const void *p1, const void *p2)
5860 const struct case_info *ci1 = (const struct case_info *) p1;
5861 const struct case_info *ci2 = (const struct case_info *) p2;
5862 int idx1 = ci1->bb->index;
5863 int idx2 = ci2->bb->index;
5865 if (idx1 < idx2)
5866 return -1;
5867 else if (idx1 == idx2)
5869 /* Make sure the default label is first in a group. */
5870 if (!CASE_LOW (ci1->expr))
5871 return -1;
5872 else if (!CASE_LOW (ci2->expr))
5873 return 1;
5874 else
5875 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5876 CASE_LOW (ci2->expr));
5878 else
5879 return 1;
5882 /* Determine whether the outgoing edges of BB should receive an
5883 ASSERT_EXPR for each of the operands of BB's LAST statement.
5884 The last statement of BB must be a SWITCH_EXPR.
5886 If any of the sub-graphs rooted at BB have an interesting use of
5887 the predicate operands, an assert location node is added to the
5888 list of assertions for the corresponding operands. */
5890 static void
5891 find_switch_asserts (basic_block bb, gswitch *last)
5893 gimple_stmt_iterator bsi;
5894 tree op;
5895 edge e;
5896 struct case_info *ci;
5897 size_t n = gimple_switch_num_labels (last);
5898 #if GCC_VERSION >= 4000
5899 unsigned int idx;
5900 #else
5901 /* Work around GCC 3.4 bug (PR 37086). */
5902 volatile unsigned int idx;
5903 #endif
5905 bsi = gsi_for_stmt (last);
5906 op = gimple_switch_index (last);
5907 if (TREE_CODE (op) != SSA_NAME)
5908 return;
5910 /* Build a vector of case labels sorted by destination label. */
5911 ci = XNEWVEC (struct case_info, n);
5912 for (idx = 0; idx < n; ++idx)
5914 ci[idx].expr = gimple_switch_label (last, idx);
5915 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5917 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5919 for (idx = 0; idx < n; ++idx)
5921 tree min, max;
5922 tree cl = ci[idx].expr;
5923 basic_block cbb = ci[idx].bb;
5925 min = CASE_LOW (cl);
5926 max = CASE_HIGH (cl);
5928 /* If there are multiple case labels with the same destination
5929 we need to combine them to a single value range for the edge. */
5930 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5932 /* Skip labels until the last of the group. */
5933 do {
5934 ++idx;
5935 } while (idx < n && cbb == ci[idx].bb);
5936 --idx;
5938 /* Pick up the maximum of the case label range. */
5939 if (CASE_HIGH (ci[idx].expr))
5940 max = CASE_HIGH (ci[idx].expr);
5941 else
5942 max = CASE_LOW (ci[idx].expr);
5945 /* Nothing to do if the range includes the default label until we
5946 can register anti-ranges. */
5947 if (min == NULL_TREE)
5948 continue;
5950 /* Find the edge to register the assert expr on. */
5951 e = find_edge (bb, cbb);
5953 /* Register the necessary assertions for the operand in the
5954 SWITCH_EXPR. */
5955 register_edge_assert_for (op, e, bsi,
5956 max ? GE_EXPR : EQ_EXPR,
5957 op, fold_convert (TREE_TYPE (op), min));
5958 if (max)
5959 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
5960 fold_convert (TREE_TYPE (op), max));
5963 XDELETEVEC (ci);
5967 /* Traverse all the statements in block BB looking for statements that
5968 may generate useful assertions for the SSA names in their operand.
5969 If a statement produces a useful assertion A for name N_i, then the
5970 list of assertions already generated for N_i is scanned to
5971 determine if A is actually needed.
5973 If N_i already had the assertion A at a location dominating the
5974 current location, then nothing needs to be done. Otherwise, the
5975 new location for A is recorded instead.
5977 1- For every statement S in BB, all the variables used by S are
5978 added to bitmap FOUND_IN_SUBGRAPH.
5980 2- If statement S uses an operand N in a way that exposes a known
5981 value range for N, then if N was not already generated by an
5982 ASSERT_EXPR, create a new assert location for N. For instance,
5983 if N is a pointer and the statement dereferences it, we can
5984 assume that N is not NULL.
5986 3- COND_EXPRs are a special case of #2. We can derive range
5987 information from the predicate but need to insert different
5988 ASSERT_EXPRs for each of the sub-graphs rooted at the
5989 conditional block. If the last statement of BB is a conditional
5990 expression of the form 'X op Y', then
5992 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5994 b) If the conditional is the only entry point to the sub-graph
5995 corresponding to the THEN_CLAUSE, recurse into it. On
5996 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5997 an ASSERT_EXPR is added for the corresponding variable.
5999 c) Repeat step (b) on the ELSE_CLAUSE.
6001 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6003 For instance,
6005 if (a == 9)
6006 b = a;
6007 else
6008 b = c + 1;
6010 In this case, an assertion on the THEN clause is useful to
6011 determine that 'a' is always 9 on that edge. However, an assertion
6012 on the ELSE clause would be unnecessary.
6014 4- If BB does not end in a conditional expression, then we recurse
6015 into BB's dominator children.
6017 At the end of the recursive traversal, every SSA name will have a
6018 list of locations where ASSERT_EXPRs should be added. When a new
6019 location for name N is found, it is registered by calling
6020 register_new_assert_for. That function keeps track of all the
6021 registered assertions to prevent adding unnecessary assertions.
6022 For instance, if a pointer P_4 is dereferenced more than once in a
6023 dominator tree, only the location dominating all the dereference of
6024 P_4 will receive an ASSERT_EXPR. */
6026 static void
6027 find_assert_locations_1 (basic_block bb, sbitmap live)
6029 gimple *last;
6031 last = last_stmt (bb);
6033 /* If BB's last statement is a conditional statement involving integer
6034 operands, determine if we need to add ASSERT_EXPRs. */
6035 if (last
6036 && gimple_code (last) == GIMPLE_COND
6037 && !fp_predicate (last)
6038 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6039 find_conditional_asserts (bb, as_a <gcond *> (last));
6041 /* If BB's last statement is a switch statement involving integer
6042 operands, determine if we need to add ASSERT_EXPRs. */
6043 if (last
6044 && gimple_code (last) == GIMPLE_SWITCH
6045 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6046 find_switch_asserts (bb, as_a <gswitch *> (last));
6048 /* Traverse all the statements in BB marking used names and looking
6049 for statements that may infer assertions for their used operands. */
6050 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6051 gsi_prev (&si))
6053 gimple *stmt;
6054 tree op;
6055 ssa_op_iter i;
6057 stmt = gsi_stmt (si);
6059 if (is_gimple_debug (stmt))
6060 continue;
6062 /* See if we can derive an assertion for any of STMT's operands. */
6063 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6065 tree value;
6066 enum tree_code comp_code;
6068 /* If op is not live beyond this stmt, do not bother to insert
6069 asserts for it. */
6070 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6071 continue;
6073 /* If OP is used in such a way that we can infer a value
6074 range for it, and we don't find a previous assertion for
6075 it, create a new assertion location node for OP. */
6076 if (infer_value_range (stmt, op, &comp_code, &value))
6078 /* If we are able to infer a nonzero value range for OP,
6079 then walk backwards through the use-def chain to see if OP
6080 was set via a typecast.
6082 If so, then we can also infer a nonzero value range
6083 for the operand of the NOP_EXPR. */
6084 if (comp_code == NE_EXPR && integer_zerop (value))
6086 tree t = op;
6087 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6089 while (is_gimple_assign (def_stmt)
6090 && CONVERT_EXPR_CODE_P
6091 (gimple_assign_rhs_code (def_stmt))
6092 && TREE_CODE
6093 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6094 && POINTER_TYPE_P
6095 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6097 t = gimple_assign_rhs1 (def_stmt);
6098 def_stmt = SSA_NAME_DEF_STMT (t);
6100 /* Note we want to register the assert for the
6101 operand of the NOP_EXPR after SI, not after the
6102 conversion. */
6103 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
6104 register_new_assert_for (t, t, comp_code, value,
6105 bb, NULL, si);
6109 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6113 /* Update live. */
6114 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6115 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6116 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6117 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6120 /* Traverse all PHI nodes in BB, updating live. */
6121 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6122 gsi_next (&si))
6124 use_operand_p arg_p;
6125 ssa_op_iter i;
6126 gphi *phi = si.phi ();
6127 tree res = gimple_phi_result (phi);
6129 if (virtual_operand_p (res))
6130 continue;
6132 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6134 tree arg = USE_FROM_PTR (arg_p);
6135 if (TREE_CODE (arg) == SSA_NAME)
6136 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6139 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6143 /* Do an RPO walk over the function computing SSA name liveness
6144 on-the-fly and deciding on assert expressions to insert. */
6146 static void
6147 find_assert_locations (void)
6149 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6150 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6151 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6152 int rpo_cnt, i;
6154 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6155 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6156 for (i = 0; i < rpo_cnt; ++i)
6157 bb_rpo[rpo[i]] = i;
6159 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6160 the order we compute liveness and insert asserts we otherwise
6161 fail to insert asserts into the loop latch. */
6162 loop_p loop;
6163 FOR_EACH_LOOP (loop, 0)
6165 i = loop->latch->index;
6166 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6167 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6168 !gsi_end_p (gsi); gsi_next (&gsi))
6170 gphi *phi = gsi.phi ();
6171 if (virtual_operand_p (gimple_phi_result (phi)))
6172 continue;
6173 tree arg = gimple_phi_arg_def (phi, j);
6174 if (TREE_CODE (arg) == SSA_NAME)
6176 if (live[i] == NULL)
6178 live[i] = sbitmap_alloc (num_ssa_names);
6179 bitmap_clear (live[i]);
6181 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6186 for (i = rpo_cnt - 1; i >= 0; --i)
6188 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6189 edge e;
6190 edge_iterator ei;
6192 if (!live[rpo[i]])
6194 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6195 bitmap_clear (live[rpo[i]]);
6198 /* Process BB and update the live information with uses in
6199 this block. */
6200 find_assert_locations_1 (bb, live[rpo[i]]);
6202 /* Merge liveness into the predecessor blocks and free it. */
6203 if (!bitmap_empty_p (live[rpo[i]]))
6205 int pred_rpo = i;
6206 FOR_EACH_EDGE (e, ei, bb->preds)
6208 int pred = e->src->index;
6209 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6210 continue;
6212 if (!live[pred])
6214 live[pred] = sbitmap_alloc (num_ssa_names);
6215 bitmap_clear (live[pred]);
6217 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6219 if (bb_rpo[pred] < pred_rpo)
6220 pred_rpo = bb_rpo[pred];
6223 /* Record the RPO number of the last visited block that needs
6224 live information from this block. */
6225 last_rpo[rpo[i]] = pred_rpo;
6227 else
6229 sbitmap_free (live[rpo[i]]);
6230 live[rpo[i]] = NULL;
6233 /* We can free all successors live bitmaps if all their
6234 predecessors have been visited already. */
6235 FOR_EACH_EDGE (e, ei, bb->succs)
6236 if (last_rpo[e->dest->index] == i
6237 && live[e->dest->index])
6239 sbitmap_free (live[e->dest->index]);
6240 live[e->dest->index] = NULL;
6244 XDELETEVEC (rpo);
6245 XDELETEVEC (bb_rpo);
6246 XDELETEVEC (last_rpo);
6247 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6248 if (live[i])
6249 sbitmap_free (live[i]);
6250 XDELETEVEC (live);
6253 /* Create an ASSERT_EXPR for NAME and insert it in the location
6254 indicated by LOC. Return true if we made any edge insertions. */
6256 static bool
6257 process_assert_insertions_for (tree name, assert_locus *loc)
6259 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6260 gimple *stmt;
6261 tree cond;
6262 gimple *assert_stmt;
6263 edge_iterator ei;
6264 edge e;
6266 /* If we have X <=> X do not insert an assert expr for that. */
6267 if (loc->expr == loc->val)
6268 return false;
6270 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6271 assert_stmt = build_assert_expr_for (cond, name);
6272 if (loc->e)
6274 /* We have been asked to insert the assertion on an edge. This
6275 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6276 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6277 || (gimple_code (gsi_stmt (loc->si))
6278 == GIMPLE_SWITCH));
6280 gsi_insert_on_edge (loc->e, assert_stmt);
6281 return true;
6284 /* Otherwise, we can insert right after LOC->SI iff the
6285 statement must not be the last statement in the block. */
6286 stmt = gsi_stmt (loc->si);
6287 if (!stmt_ends_bb_p (stmt))
6289 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6290 return false;
6293 /* If STMT must be the last statement in BB, we can only insert new
6294 assertions on the non-abnormal edge out of BB. Note that since
6295 STMT is not control flow, there may only be one non-abnormal edge
6296 out of BB. */
6297 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6298 if (!(e->flags & EDGE_ABNORMAL))
6300 gsi_insert_on_edge (e, assert_stmt);
6301 return true;
6304 gcc_unreachable ();
6308 /* Process all the insertions registered for every name N_i registered
6309 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6310 found in ASSERTS_FOR[i]. */
6312 static void
6313 process_assert_insertions (void)
6315 unsigned i;
6316 bitmap_iterator bi;
6317 bool update_edges_p = false;
6318 int num_asserts = 0;
6320 if (dump_file && (dump_flags & TDF_DETAILS))
6321 dump_all_asserts (dump_file);
6323 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6325 assert_locus *loc = asserts_for[i];
6326 gcc_assert (loc);
6328 while (loc)
6330 assert_locus *next = loc->next;
6331 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6332 free (loc);
6333 loc = next;
6334 num_asserts++;
6338 if (update_edges_p)
6339 gsi_commit_edge_inserts ();
6341 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6342 num_asserts);
6346 /* Traverse the flowgraph looking for conditional jumps to insert range
6347 expressions. These range expressions are meant to provide information
6348 to optimizations that need to reason in terms of value ranges. They
6349 will not be expanded into RTL. For instance, given:
6351 x = ...
6352 y = ...
6353 if (x < y)
6354 y = x - 2;
6355 else
6356 x = y + 3;
6358 this pass will transform the code into:
6360 x = ...
6361 y = ...
6362 if (x < y)
6364 x = ASSERT_EXPR <x, x < y>
6365 y = x - 2
6367 else
6369 y = ASSERT_EXPR <y, x >= y>
6370 x = y + 3
6373 The idea is that once copy and constant propagation have run, other
6374 optimizations will be able to determine what ranges of values can 'x'
6375 take in different paths of the code, simply by checking the reaching
6376 definition of 'x'. */
6378 static void
6379 insert_range_assertions (void)
6381 need_assert_for = BITMAP_ALLOC (NULL);
6382 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6384 calculate_dominance_info (CDI_DOMINATORS);
6386 find_assert_locations ();
6387 if (!bitmap_empty_p (need_assert_for))
6389 process_assert_insertions ();
6390 update_ssa (TODO_update_ssa_no_phi);
6393 if (dump_file && (dump_flags & TDF_DETAILS))
6395 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6396 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6399 free (asserts_for);
6400 BITMAP_FREE (need_assert_for);
6403 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6404 and "struct" hacks. If VRP can determine that the
6405 array subscript is a constant, check if it is outside valid
6406 range. If the array subscript is a RANGE, warn if it is
6407 non-overlapping with valid range.
6408 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6410 static void
6411 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6413 value_range *vr = NULL;
6414 tree low_sub, up_sub;
6415 tree low_bound, up_bound, up_bound_p1;
6417 if (TREE_NO_WARNING (ref))
6418 return;
6420 low_sub = up_sub = TREE_OPERAND (ref, 1);
6421 up_bound = array_ref_up_bound (ref);
6423 /* Can not check flexible arrays. */
6424 if (!up_bound
6425 || TREE_CODE (up_bound) != INTEGER_CST)
6426 return;
6428 /* Accesses to trailing arrays via pointers may access storage
6429 beyond the types array bounds. */
6430 if (warn_array_bounds < 2
6431 && array_at_struct_end_p (ref))
6432 return;
6434 low_bound = array_ref_low_bound (ref);
6435 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6436 build_int_cst (TREE_TYPE (up_bound), 1));
6438 /* Empty array. */
6439 if (tree_int_cst_equal (low_bound, up_bound_p1))
6441 warning_at (location, OPT_Warray_bounds,
6442 "array subscript is above array bounds");
6443 TREE_NO_WARNING (ref) = 1;
6446 if (TREE_CODE (low_sub) == SSA_NAME)
6448 vr = get_value_range (low_sub);
6449 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6451 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6452 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6456 if (vr && vr->type == VR_ANTI_RANGE)
6458 if (TREE_CODE (up_sub) == INTEGER_CST
6459 && (ignore_off_by_one
6460 ? tree_int_cst_lt (up_bound, up_sub)
6461 : tree_int_cst_le (up_bound, up_sub))
6462 && TREE_CODE (low_sub) == INTEGER_CST
6463 && tree_int_cst_le (low_sub, low_bound))
6465 warning_at (location, OPT_Warray_bounds,
6466 "array subscript is outside array bounds");
6467 TREE_NO_WARNING (ref) = 1;
6470 else if (TREE_CODE (up_sub) == INTEGER_CST
6471 && (ignore_off_by_one
6472 ? !tree_int_cst_le (up_sub, up_bound_p1)
6473 : !tree_int_cst_le (up_sub, up_bound)))
6475 if (dump_file && (dump_flags & TDF_DETAILS))
6477 fprintf (dump_file, "Array bound warning for ");
6478 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6479 fprintf (dump_file, "\n");
6481 warning_at (location, OPT_Warray_bounds,
6482 "array subscript is above array bounds");
6483 TREE_NO_WARNING (ref) = 1;
6485 else if (TREE_CODE (low_sub) == INTEGER_CST
6486 && tree_int_cst_lt (low_sub, low_bound))
6488 if (dump_file && (dump_flags & TDF_DETAILS))
6490 fprintf (dump_file, "Array bound warning for ");
6491 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6492 fprintf (dump_file, "\n");
6494 warning_at (location, OPT_Warray_bounds,
6495 "array subscript is below array bounds");
6496 TREE_NO_WARNING (ref) = 1;
6500 /* Searches if the expr T, located at LOCATION computes
6501 address of an ARRAY_REF, and call check_array_ref on it. */
6503 static void
6504 search_for_addr_array (tree t, location_t location)
6506 /* Check each ARRAY_REFs in the reference chain. */
6509 if (TREE_CODE (t) == ARRAY_REF)
6510 check_array_ref (location, t, true /*ignore_off_by_one*/);
6512 t = TREE_OPERAND (t, 0);
6514 while (handled_component_p (t));
6516 if (TREE_CODE (t) == MEM_REF
6517 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6518 && !TREE_NO_WARNING (t))
6520 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6521 tree low_bound, up_bound, el_sz;
6522 offset_int idx;
6523 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6524 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6525 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6526 return;
6528 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6529 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6530 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6531 if (!low_bound
6532 || TREE_CODE (low_bound) != INTEGER_CST
6533 || !up_bound
6534 || TREE_CODE (up_bound) != INTEGER_CST
6535 || !el_sz
6536 || TREE_CODE (el_sz) != INTEGER_CST)
6537 return;
6539 idx = mem_ref_offset (t);
6540 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6541 if (idx < 0)
6543 if (dump_file && (dump_flags & TDF_DETAILS))
6545 fprintf (dump_file, "Array bound warning for ");
6546 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6547 fprintf (dump_file, "\n");
6549 warning_at (location, OPT_Warray_bounds,
6550 "array subscript is below array bounds");
6551 TREE_NO_WARNING (t) = 1;
6553 else if (idx > (wi::to_offset (up_bound)
6554 - wi::to_offset (low_bound) + 1))
6556 if (dump_file && (dump_flags & TDF_DETAILS))
6558 fprintf (dump_file, "Array bound warning for ");
6559 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6560 fprintf (dump_file, "\n");
6562 warning_at (location, OPT_Warray_bounds,
6563 "array subscript is above array bounds");
6564 TREE_NO_WARNING (t) = 1;
6569 /* walk_tree() callback that checks if *TP is
6570 an ARRAY_REF inside an ADDR_EXPR (in which an array
6571 subscript one outside the valid range is allowed). Call
6572 check_array_ref for each ARRAY_REF found. The location is
6573 passed in DATA. */
6575 static tree
6576 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6578 tree t = *tp;
6579 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6580 location_t location;
6582 if (EXPR_HAS_LOCATION (t))
6583 location = EXPR_LOCATION (t);
6584 else
6586 location_t *locp = (location_t *) wi->info;
6587 location = *locp;
6590 *walk_subtree = TRUE;
6592 if (TREE_CODE (t) == ARRAY_REF)
6593 check_array_ref (location, t, false /*ignore_off_by_one*/);
6595 else if (TREE_CODE (t) == ADDR_EXPR)
6597 search_for_addr_array (t, location);
6598 *walk_subtree = FALSE;
6601 return NULL_TREE;
6604 /* Walk over all statements of all reachable BBs and call check_array_bounds
6605 on them. */
6607 static void
6608 check_all_array_refs (void)
6610 basic_block bb;
6611 gimple_stmt_iterator si;
6613 FOR_EACH_BB_FN (bb, cfun)
6615 edge_iterator ei;
6616 edge e;
6617 bool executable = false;
6619 /* Skip blocks that were found to be unreachable. */
6620 FOR_EACH_EDGE (e, ei, bb->preds)
6621 executable |= !!(e->flags & EDGE_EXECUTABLE);
6622 if (!executable)
6623 continue;
6625 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6627 gimple *stmt = gsi_stmt (si);
6628 struct walk_stmt_info wi;
6629 if (!gimple_has_location (stmt)
6630 || is_gimple_debug (stmt))
6631 continue;
6633 memset (&wi, 0, sizeof (wi));
6635 location_t loc = gimple_location (stmt);
6636 wi.info = &loc;
6638 walk_gimple_op (gsi_stmt (si),
6639 check_array_bounds,
6640 &wi);
6645 /* Return true if all imm uses of VAR are either in STMT, or
6646 feed (optionally through a chain of single imm uses) GIMPLE_COND
6647 in basic block COND_BB. */
6649 static bool
6650 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6652 use_operand_p use_p, use2_p;
6653 imm_use_iterator iter;
6655 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6656 if (USE_STMT (use_p) != stmt)
6658 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6659 if (is_gimple_debug (use_stmt))
6660 continue;
6661 while (is_gimple_assign (use_stmt)
6662 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6663 && single_imm_use (gimple_assign_lhs (use_stmt),
6664 &use2_p, &use_stmt2))
6665 use_stmt = use_stmt2;
6666 if (gimple_code (use_stmt) != GIMPLE_COND
6667 || gimple_bb (use_stmt) != cond_bb)
6668 return false;
6670 return true;
6673 /* Handle
6674 _4 = x_3 & 31;
6675 if (_4 != 0)
6676 goto <bb 6>;
6677 else
6678 goto <bb 7>;
6679 <bb 6>:
6680 __builtin_unreachable ();
6681 <bb 7>:
6682 x_5 = ASSERT_EXPR <x_3, ...>;
6683 If x_3 has no other immediate uses (checked by caller),
6684 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6685 from the non-zero bitmask. */
6687 static void
6688 maybe_set_nonzero_bits (basic_block bb, tree var)
6690 edge e = single_pred_edge (bb);
6691 basic_block cond_bb = e->src;
6692 gimple *stmt = last_stmt (cond_bb);
6693 tree cst;
6695 if (stmt == NULL
6696 || gimple_code (stmt) != GIMPLE_COND
6697 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6698 ? EQ_EXPR : NE_EXPR)
6699 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6700 || !integer_zerop (gimple_cond_rhs (stmt)))
6701 return;
6703 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6704 if (!is_gimple_assign (stmt)
6705 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6706 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6707 return;
6708 if (gimple_assign_rhs1 (stmt) != var)
6710 gimple *stmt2;
6712 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6713 return;
6714 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6715 if (!gimple_assign_cast_p (stmt2)
6716 || gimple_assign_rhs1 (stmt2) != var
6717 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6718 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6719 != TYPE_PRECISION (TREE_TYPE (var))))
6720 return;
6722 cst = gimple_assign_rhs2 (stmt);
6723 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6726 /* Convert range assertion expressions into the implied copies and
6727 copy propagate away the copies. Doing the trivial copy propagation
6728 here avoids the need to run the full copy propagation pass after
6729 VRP.
6731 FIXME, this will eventually lead to copy propagation removing the
6732 names that had useful range information attached to them. For
6733 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6734 then N_i will have the range [3, +INF].
6736 However, by converting the assertion into the implied copy
6737 operation N_i = N_j, we will then copy-propagate N_j into the uses
6738 of N_i and lose the range information. We may want to hold on to
6739 ASSERT_EXPRs a little while longer as the ranges could be used in
6740 things like jump threading.
6742 The problem with keeping ASSERT_EXPRs around is that passes after
6743 VRP need to handle them appropriately.
6745 Another approach would be to make the range information a first
6746 class property of the SSA_NAME so that it can be queried from
6747 any pass. This is made somewhat more complex by the need for
6748 multiple ranges to be associated with one SSA_NAME. */
6750 static void
6751 remove_range_assertions (void)
6753 basic_block bb;
6754 gimple_stmt_iterator si;
6755 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6756 a basic block preceeded by GIMPLE_COND branching to it and
6757 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6758 int is_unreachable;
6760 /* Note that the BSI iterator bump happens at the bottom of the
6761 loop and no bump is necessary if we're removing the statement
6762 referenced by the current BSI. */
6763 FOR_EACH_BB_FN (bb, cfun)
6764 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6766 gimple *stmt = gsi_stmt (si);
6767 gimple *use_stmt;
6769 if (is_gimple_assign (stmt)
6770 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6772 tree lhs = gimple_assign_lhs (stmt);
6773 tree rhs = gimple_assign_rhs1 (stmt);
6774 tree var;
6775 use_operand_p use_p;
6776 imm_use_iterator iter;
6778 var = ASSERT_EXPR_VAR (rhs);
6779 gcc_assert (TREE_CODE (var) == SSA_NAME);
6781 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6782 && SSA_NAME_RANGE_INFO (lhs))
6784 if (is_unreachable == -1)
6786 is_unreachable = 0;
6787 if (single_pred_p (bb)
6788 && assert_unreachable_fallthru_edge_p
6789 (single_pred_edge (bb)))
6790 is_unreachable = 1;
6792 /* Handle
6793 if (x_7 >= 10 && x_7 < 20)
6794 __builtin_unreachable ();
6795 x_8 = ASSERT_EXPR <x_7, ...>;
6796 if the only uses of x_7 are in the ASSERT_EXPR and
6797 in the condition. In that case, we can copy the
6798 range info from x_8 computed in this pass also
6799 for x_7. */
6800 if (is_unreachable
6801 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6802 single_pred (bb)))
6804 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6805 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6806 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6807 maybe_set_nonzero_bits (bb, var);
6811 /* Propagate the RHS into every use of the LHS. */
6812 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6813 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6814 SET_USE (use_p, var);
6816 /* And finally, remove the copy, it is not needed. */
6817 gsi_remove (&si, true);
6818 release_defs (stmt);
6820 else
6822 if (!is_gimple_debug (gsi_stmt (si)))
6823 is_unreachable = 0;
6824 gsi_next (&si);
6830 /* Return true if STMT is interesting for VRP. */
6832 static bool
6833 stmt_interesting_for_vrp (gimple *stmt)
6835 if (gimple_code (stmt) == GIMPLE_PHI)
6837 tree res = gimple_phi_result (stmt);
6838 return (!virtual_operand_p (res)
6839 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6840 || POINTER_TYPE_P (TREE_TYPE (res))));
6842 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6844 tree lhs = gimple_get_lhs (stmt);
6846 /* In general, assignments with virtual operands are not useful
6847 for deriving ranges, with the obvious exception of calls to
6848 builtin functions. */
6849 if (lhs && TREE_CODE (lhs) == SSA_NAME
6850 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6851 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6852 && (is_gimple_call (stmt)
6853 || !gimple_vuse (stmt)))
6854 return true;
6855 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6856 switch (gimple_call_internal_fn (stmt))
6858 case IFN_ADD_OVERFLOW:
6859 case IFN_SUB_OVERFLOW:
6860 case IFN_MUL_OVERFLOW:
6861 /* These internal calls return _Complex integer type,
6862 but are interesting to VRP nevertheless. */
6863 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6864 return true;
6865 break;
6866 default:
6867 break;
6870 else if (gimple_code (stmt) == GIMPLE_COND
6871 || gimple_code (stmt) == GIMPLE_SWITCH)
6872 return true;
6874 return false;
6878 /* Initialize local data structures for VRP. */
6880 static void
6881 vrp_initialize (void)
6883 basic_block bb;
6885 values_propagated = false;
6886 num_vr_values = num_ssa_names;
6887 vr_value = XCNEWVEC (value_range *, num_vr_values);
6888 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6890 FOR_EACH_BB_FN (bb, cfun)
6892 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6893 gsi_next (&si))
6895 gphi *phi = si.phi ();
6896 if (!stmt_interesting_for_vrp (phi))
6898 tree lhs = PHI_RESULT (phi);
6899 set_value_range_to_varying (get_value_range (lhs));
6900 prop_set_simulate_again (phi, false);
6902 else
6903 prop_set_simulate_again (phi, true);
6906 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
6907 gsi_next (&si))
6909 gimple *stmt = gsi_stmt (si);
6911 /* If the statement is a control insn, then we do not
6912 want to avoid simulating the statement once. Failure
6913 to do so means that those edges will never get added. */
6914 if (stmt_ends_bb_p (stmt))
6915 prop_set_simulate_again (stmt, true);
6916 else if (!stmt_interesting_for_vrp (stmt))
6918 ssa_op_iter i;
6919 tree def;
6920 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6921 set_value_range_to_varying (get_value_range (def));
6922 prop_set_simulate_again (stmt, false);
6924 else
6925 prop_set_simulate_again (stmt, true);
6930 /* Return the singleton value-range for NAME or NAME. */
6932 static inline tree
6933 vrp_valueize (tree name)
6935 if (TREE_CODE (name) == SSA_NAME)
6937 value_range *vr = get_value_range (name);
6938 if (vr->type == VR_RANGE
6939 && (vr->min == vr->max
6940 || operand_equal_p (vr->min, vr->max, 0)))
6941 return vr->min;
6943 return name;
6946 /* Return the singleton value-range for NAME if that is a constant
6947 but signal to not follow SSA edges. */
6949 static inline tree
6950 vrp_valueize_1 (tree name)
6952 if (TREE_CODE (name) == SSA_NAME)
6954 /* If the definition may be simulated again we cannot follow
6955 this SSA edge as the SSA propagator does not necessarily
6956 re-visit the use. */
6957 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
6958 if (!gimple_nop_p (def_stmt)
6959 && prop_simulate_again_p (def_stmt))
6960 return NULL_TREE;
6961 value_range *vr = get_value_range (name);
6962 if (range_int_cst_singleton_p (vr))
6963 return vr->min;
6965 return name;
6968 /* Visit assignment STMT. If it produces an interesting range, record
6969 the SSA name in *OUTPUT_P. */
6971 static enum ssa_prop_result
6972 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p)
6974 tree def, lhs;
6975 ssa_op_iter iter;
6976 enum gimple_code code = gimple_code (stmt);
6977 lhs = gimple_get_lhs (stmt);
6979 /* We only keep track of ranges in integral and pointer types. */
6980 if (TREE_CODE (lhs) == SSA_NAME
6981 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6982 /* It is valid to have NULL MIN/MAX values on a type. See
6983 build_range_type. */
6984 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6985 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6986 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6988 value_range new_vr = VR_INITIALIZER;
6990 /* Try folding the statement to a constant first. */
6991 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
6992 vrp_valueize_1);
6993 if (tem && is_gimple_min_invariant (tem))
6994 set_value_range_to_value (&new_vr, tem, NULL);
6995 /* Then dispatch to value-range extracting functions. */
6996 else if (code == GIMPLE_CALL)
6997 extract_range_basic (&new_vr, stmt);
6998 else
6999 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7001 if (update_value_range (lhs, &new_vr))
7003 *output_p = lhs;
7005 if (dump_file && (dump_flags & TDF_DETAILS))
7007 fprintf (dump_file, "Found new range for ");
7008 print_generic_expr (dump_file, lhs, 0);
7009 fprintf (dump_file, ": ");
7010 dump_value_range (dump_file, &new_vr);
7011 fprintf (dump_file, "\n");
7014 if (new_vr.type == VR_VARYING)
7015 return SSA_PROP_VARYING;
7017 return SSA_PROP_INTERESTING;
7020 return SSA_PROP_NOT_INTERESTING;
7022 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7023 switch (gimple_call_internal_fn (stmt))
7025 case IFN_ADD_OVERFLOW:
7026 case IFN_SUB_OVERFLOW:
7027 case IFN_MUL_OVERFLOW:
7028 /* These internal calls return _Complex integer type,
7029 which VRP does not track, but the immediate uses
7030 thereof might be interesting. */
7031 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7033 imm_use_iterator iter;
7034 use_operand_p use_p;
7035 enum ssa_prop_result res = SSA_PROP_VARYING;
7037 set_value_range_to_varying (get_value_range (lhs));
7039 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7041 gimple *use_stmt = USE_STMT (use_p);
7042 if (!is_gimple_assign (use_stmt))
7043 continue;
7044 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7045 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7046 continue;
7047 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7048 tree use_lhs = gimple_assign_lhs (use_stmt);
7049 if (TREE_CODE (rhs1) != rhs_code
7050 || TREE_OPERAND (rhs1, 0) != lhs
7051 || TREE_CODE (use_lhs) != SSA_NAME
7052 || !stmt_interesting_for_vrp (use_stmt)
7053 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7054 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7055 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7056 continue;
7058 /* If there is a change in the value range for any of the
7059 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7060 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7061 or IMAGPART_EXPR immediate uses, but none of them have
7062 a change in their value ranges, return
7063 SSA_PROP_NOT_INTERESTING. If there are no
7064 {REAL,IMAG}PART_EXPR uses at all,
7065 return SSA_PROP_VARYING. */
7066 value_range new_vr = VR_INITIALIZER;
7067 extract_range_basic (&new_vr, use_stmt);
7068 value_range *old_vr = get_value_range (use_lhs);
7069 if (old_vr->type != new_vr.type
7070 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7071 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7072 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7073 res = SSA_PROP_INTERESTING;
7074 else
7075 res = SSA_PROP_NOT_INTERESTING;
7076 BITMAP_FREE (new_vr.equiv);
7077 if (res == SSA_PROP_INTERESTING)
7079 *output_p = lhs;
7080 return res;
7084 return res;
7086 break;
7087 default:
7088 break;
7091 /* Every other statement produces no useful ranges. */
7092 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7093 set_value_range_to_varying (get_value_range (def));
7095 return SSA_PROP_VARYING;
7098 /* Helper that gets the value range of the SSA_NAME with version I
7099 or a symbolic range containing the SSA_NAME only if the value range
7100 is varying or undefined. */
7102 static inline value_range
7103 get_vr_for_comparison (int i)
7105 value_range vr = *get_value_range (ssa_name (i));
7107 /* If name N_i does not have a valid range, use N_i as its own
7108 range. This allows us to compare against names that may
7109 have N_i in their ranges. */
7110 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7112 vr.type = VR_RANGE;
7113 vr.min = ssa_name (i);
7114 vr.max = ssa_name (i);
7117 return vr;
7120 /* Compare all the value ranges for names equivalent to VAR with VAL
7121 using comparison code COMP. Return the same value returned by
7122 compare_range_with_value, including the setting of
7123 *STRICT_OVERFLOW_P. */
7125 static tree
7126 compare_name_with_value (enum tree_code comp, tree var, tree val,
7127 bool *strict_overflow_p, bool use_equiv_p)
7129 bitmap_iterator bi;
7130 unsigned i;
7131 bitmap e;
7132 tree retval, t;
7133 int used_strict_overflow;
7134 bool sop;
7135 value_range equiv_vr;
7137 /* Get the set of equivalences for VAR. */
7138 e = get_value_range (var)->equiv;
7140 /* Start at -1. Set it to 0 if we do a comparison without relying
7141 on overflow, or 1 if all comparisons rely on overflow. */
7142 used_strict_overflow = -1;
7144 /* Compare vars' value range with val. */
7145 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7146 sop = false;
7147 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7148 if (retval)
7149 used_strict_overflow = sop ? 1 : 0;
7151 /* If the equiv set is empty we have done all work we need to do. */
7152 if (e == NULL)
7154 if (retval
7155 && used_strict_overflow > 0)
7156 *strict_overflow_p = true;
7157 return retval;
7160 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7162 if (! use_equiv_p
7163 && ! SSA_NAME_IS_DEFAULT_DEF (ssa_name (i))
7164 && prop_simulate_again_p (SSA_NAME_DEF_STMT (ssa_name (i))))
7165 continue;
7167 equiv_vr = get_vr_for_comparison (i);
7168 sop = false;
7169 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7170 if (t)
7172 /* If we get different answers from different members
7173 of the equivalence set this check must be in a dead
7174 code region. Folding it to a trap representation
7175 would be correct here. For now just return don't-know. */
7176 if (retval != NULL
7177 && t != retval)
7179 retval = NULL_TREE;
7180 break;
7182 retval = t;
7184 if (!sop)
7185 used_strict_overflow = 0;
7186 else if (used_strict_overflow < 0)
7187 used_strict_overflow = 1;
7191 if (retval
7192 && used_strict_overflow > 0)
7193 *strict_overflow_p = true;
7195 return retval;
7199 /* Given a comparison code COMP and names N1 and N2, compare all the
7200 ranges equivalent to N1 against all the ranges equivalent to N2
7201 to determine the value of N1 COMP N2. Return the same value
7202 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7203 whether we relied on an overflow infinity in the comparison. */
7206 static tree
7207 compare_names (enum tree_code comp, tree n1, tree n2,
7208 bool *strict_overflow_p)
7210 tree t, retval;
7211 bitmap e1, e2;
7212 bitmap_iterator bi1, bi2;
7213 unsigned i1, i2;
7214 int used_strict_overflow;
7215 static bitmap_obstack *s_obstack = NULL;
7216 static bitmap s_e1 = NULL, s_e2 = NULL;
7218 /* Compare the ranges of every name equivalent to N1 against the
7219 ranges of every name equivalent to N2. */
7220 e1 = get_value_range (n1)->equiv;
7221 e2 = get_value_range (n2)->equiv;
7223 /* Use the fake bitmaps if e1 or e2 are not available. */
7224 if (s_obstack == NULL)
7226 s_obstack = XNEW (bitmap_obstack);
7227 bitmap_obstack_initialize (s_obstack);
7228 s_e1 = BITMAP_ALLOC (s_obstack);
7229 s_e2 = BITMAP_ALLOC (s_obstack);
7231 if (e1 == NULL)
7232 e1 = s_e1;
7233 if (e2 == NULL)
7234 e2 = s_e2;
7236 /* Add N1 and N2 to their own set of equivalences to avoid
7237 duplicating the body of the loop just to check N1 and N2
7238 ranges. */
7239 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7240 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7242 /* If the equivalence sets have a common intersection, then the two
7243 names can be compared without checking their ranges. */
7244 if (bitmap_intersect_p (e1, e2))
7246 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7247 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7249 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7250 ? boolean_true_node
7251 : boolean_false_node;
7254 /* Start at -1. Set it to 0 if we do a comparison without relying
7255 on overflow, or 1 if all comparisons rely on overflow. */
7256 used_strict_overflow = -1;
7258 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7259 N2 to their own set of equivalences to avoid duplicating the body
7260 of the loop just to check N1 and N2 ranges. */
7261 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7263 value_range vr1 = get_vr_for_comparison (i1);
7265 t = retval = NULL_TREE;
7266 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7268 bool sop = false;
7270 value_range vr2 = get_vr_for_comparison (i2);
7272 t = compare_ranges (comp, &vr1, &vr2, &sop);
7273 if (t)
7275 /* If we get different answers from different members
7276 of the equivalence set this check must be in a dead
7277 code region. Folding it to a trap representation
7278 would be correct here. For now just return don't-know. */
7279 if (retval != NULL
7280 && t != retval)
7282 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7283 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7284 return NULL_TREE;
7286 retval = t;
7288 if (!sop)
7289 used_strict_overflow = 0;
7290 else if (used_strict_overflow < 0)
7291 used_strict_overflow = 1;
7295 if (retval)
7297 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7298 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7299 if (used_strict_overflow > 0)
7300 *strict_overflow_p = true;
7301 return retval;
7305 /* None of the equivalent ranges are useful in computing this
7306 comparison. */
7307 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7308 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7309 return NULL_TREE;
7312 /* Helper function for vrp_evaluate_conditional_warnv & other
7313 optimizers. */
7315 static tree
7316 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7317 tree op0, tree op1,
7318 bool * strict_overflow_p)
7320 value_range *vr0, *vr1;
7322 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7323 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7325 tree res = NULL_TREE;
7326 if (vr0 && vr1)
7327 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7328 if (!res && vr0)
7329 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7330 if (!res && vr1)
7331 res = (compare_range_with_value
7332 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7333 return res;
7336 /* Helper function for vrp_evaluate_conditional_warnv. */
7338 static tree
7339 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7340 tree op1, bool use_equiv_p,
7341 bool *strict_overflow_p, bool *only_ranges)
7343 tree ret;
7344 if (only_ranges)
7345 *only_ranges = true;
7347 /* We only deal with integral and pointer types. */
7348 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7349 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7350 return NULL_TREE;
7352 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7353 (code, op0, op1, strict_overflow_p)))
7354 return ret;
7355 if (only_ranges)
7356 *only_ranges = false;
7357 /* Do not use compare_names during propagation, it's quadratic. */
7358 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
7359 && use_equiv_p)
7360 return compare_names (code, op0, op1, strict_overflow_p);
7361 else if (TREE_CODE (op0) == SSA_NAME)
7362 return compare_name_with_value (code, op0, op1,
7363 strict_overflow_p, use_equiv_p);
7364 else if (TREE_CODE (op1) == SSA_NAME)
7365 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
7366 strict_overflow_p, use_equiv_p);
7367 return NULL_TREE;
7370 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7371 information. Return NULL if the conditional can not be evaluated.
7372 The ranges of all the names equivalent with the operands in COND
7373 will be used when trying to compute the value. If the result is
7374 based on undefined signed overflow, issue a warning if
7375 appropriate. */
7377 static tree
7378 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7380 bool sop;
7381 tree ret;
7382 bool only_ranges;
7384 /* Some passes and foldings leak constants with overflow flag set
7385 into the IL. Avoid doing wrong things with these and bail out. */
7386 if ((TREE_CODE (op0) == INTEGER_CST
7387 && TREE_OVERFLOW (op0))
7388 || (TREE_CODE (op1) == INTEGER_CST
7389 && TREE_OVERFLOW (op1)))
7390 return NULL_TREE;
7392 sop = false;
7393 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7394 &only_ranges);
7396 if (ret && sop)
7398 enum warn_strict_overflow_code wc;
7399 const char* warnmsg;
7401 if (is_gimple_min_invariant (ret))
7403 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7404 warnmsg = G_("assuming signed overflow does not occur when "
7405 "simplifying conditional to constant");
7407 else
7409 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7410 warnmsg = G_("assuming signed overflow does not occur when "
7411 "simplifying conditional");
7414 if (issue_strict_overflow_warning (wc))
7416 location_t location;
7418 if (!gimple_has_location (stmt))
7419 location = input_location;
7420 else
7421 location = gimple_location (stmt);
7422 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7426 if (warn_type_limits
7427 && ret && only_ranges
7428 && TREE_CODE_CLASS (code) == tcc_comparison
7429 && TREE_CODE (op0) == SSA_NAME)
7431 /* If the comparison is being folded and the operand on the LHS
7432 is being compared against a constant value that is outside of
7433 the natural range of OP0's type, then the predicate will
7434 always fold regardless of the value of OP0. If -Wtype-limits
7435 was specified, emit a warning. */
7436 tree type = TREE_TYPE (op0);
7437 value_range *vr0 = get_value_range (op0);
7439 if (vr0->type == VR_RANGE
7440 && INTEGRAL_TYPE_P (type)
7441 && vrp_val_is_min (vr0->min)
7442 && vrp_val_is_max (vr0->max)
7443 && is_gimple_min_invariant (op1))
7445 location_t location;
7447 if (!gimple_has_location (stmt))
7448 location = input_location;
7449 else
7450 location = gimple_location (stmt);
7452 warning_at (location, OPT_Wtype_limits,
7453 integer_zerop (ret)
7454 ? G_("comparison always false "
7455 "due to limited range of data type")
7456 : G_("comparison always true "
7457 "due to limited range of data type"));
7461 return ret;
7465 /* Visit conditional statement STMT. If we can determine which edge
7466 will be taken out of STMT's basic block, record it in
7467 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7468 SSA_PROP_VARYING. */
7470 static enum ssa_prop_result
7471 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7473 tree val;
7474 bool sop;
7476 *taken_edge_p = NULL;
7478 if (dump_file && (dump_flags & TDF_DETAILS))
7480 tree use;
7481 ssa_op_iter i;
7483 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7484 print_gimple_stmt (dump_file, stmt, 0, 0);
7485 fprintf (dump_file, "\nWith known ranges\n");
7487 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7489 fprintf (dump_file, "\t");
7490 print_generic_expr (dump_file, use, 0);
7491 fprintf (dump_file, ": ");
7492 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7495 fprintf (dump_file, "\n");
7498 /* Compute the value of the predicate COND by checking the known
7499 ranges of each of its operands.
7501 Note that we cannot evaluate all the equivalent ranges here
7502 because those ranges may not yet be final and with the current
7503 propagation strategy, we cannot determine when the value ranges
7504 of the names in the equivalence set have changed.
7506 For instance, given the following code fragment
7508 i_5 = PHI <8, i_13>
7510 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7511 if (i_14 == 1)
7514 Assume that on the first visit to i_14, i_5 has the temporary
7515 range [8, 8] because the second argument to the PHI function is
7516 not yet executable. We derive the range ~[0, 0] for i_14 and the
7517 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7518 the first time, since i_14 is equivalent to the range [8, 8], we
7519 determine that the predicate is always false.
7521 On the next round of propagation, i_13 is determined to be
7522 VARYING, which causes i_5 to drop down to VARYING. So, another
7523 visit to i_14 is scheduled. In this second visit, we compute the
7524 exact same range and equivalence set for i_14, namely ~[0, 0] and
7525 { i_5 }. But we did not have the previous range for i_5
7526 registered, so vrp_visit_assignment thinks that the range for
7527 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7528 is not visited again, which stops propagation from visiting
7529 statements in the THEN clause of that if().
7531 To properly fix this we would need to keep the previous range
7532 value for the names in the equivalence set. This way we would've
7533 discovered that from one visit to the other i_5 changed from
7534 range [8, 8] to VR_VARYING.
7536 However, fixing this apparent limitation may not be worth the
7537 additional checking. Testing on several code bases (GCC, DLV,
7538 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7539 4 more predicates folded in SPEC. */
7540 sop = false;
7542 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7543 gimple_cond_lhs (stmt),
7544 gimple_cond_rhs (stmt),
7545 false, &sop, NULL);
7546 if (val)
7548 if (!sop)
7549 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7550 else
7552 if (dump_file && (dump_flags & TDF_DETAILS))
7553 fprintf (dump_file,
7554 "\nIgnoring predicate evaluation because "
7555 "it assumes that signed overflow is undefined");
7556 val = NULL_TREE;
7560 if (dump_file && (dump_flags & TDF_DETAILS))
7562 fprintf (dump_file, "\nPredicate evaluates to: ");
7563 if (val == NULL_TREE)
7564 fprintf (dump_file, "DON'T KNOW\n");
7565 else
7566 print_generic_stmt (dump_file, val, 0);
7569 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7572 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7573 that includes the value VAL. The search is restricted to the range
7574 [START_IDX, n - 1] where n is the size of VEC.
7576 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7577 returned.
7579 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7580 it is placed in IDX and false is returned.
7582 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7583 returned. */
7585 static bool
7586 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7588 size_t n = gimple_switch_num_labels (stmt);
7589 size_t low, high;
7591 /* Find case label for minimum of the value range or the next one.
7592 At each iteration we are searching in [low, high - 1]. */
7594 for (low = start_idx, high = n; high != low; )
7596 tree t;
7597 int cmp;
7598 /* Note that i != high, so we never ask for n. */
7599 size_t i = (high + low) / 2;
7600 t = gimple_switch_label (stmt, i);
7602 /* Cache the result of comparing CASE_LOW and val. */
7603 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7605 if (cmp == 0)
7607 /* Ranges cannot be empty. */
7608 *idx = i;
7609 return true;
7611 else if (cmp > 0)
7612 high = i;
7613 else
7615 low = i + 1;
7616 if (CASE_HIGH (t) != NULL
7617 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7619 *idx = i;
7620 return true;
7625 *idx = high;
7626 return false;
7629 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7630 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7631 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7632 then MAX_IDX < MIN_IDX.
7633 Returns true if the default label is not needed. */
7635 static bool
7636 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7637 size_t *max_idx)
7639 size_t i, j;
7640 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7641 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7643 if (i == j
7644 && min_take_default
7645 && max_take_default)
7647 /* Only the default case label reached.
7648 Return an empty range. */
7649 *min_idx = 1;
7650 *max_idx = 0;
7651 return false;
7653 else
7655 bool take_default = min_take_default || max_take_default;
7656 tree low, high;
7657 size_t k;
7659 if (max_take_default)
7660 j--;
7662 /* If the case label range is continuous, we do not need
7663 the default case label. Verify that. */
7664 high = CASE_LOW (gimple_switch_label (stmt, i));
7665 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7666 high = CASE_HIGH (gimple_switch_label (stmt, i));
7667 for (k = i + 1; k <= j; ++k)
7669 low = CASE_LOW (gimple_switch_label (stmt, k));
7670 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7672 take_default = true;
7673 break;
7675 high = low;
7676 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7677 high = CASE_HIGH (gimple_switch_label (stmt, k));
7680 *min_idx = i;
7681 *max_idx = j;
7682 return !take_default;
7686 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7687 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7688 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7689 Returns true if the default label is not needed. */
7691 static bool
7692 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7693 size_t *max_idx1, size_t *min_idx2,
7694 size_t *max_idx2)
7696 size_t i, j, k, l;
7697 unsigned int n = gimple_switch_num_labels (stmt);
7698 bool take_default;
7699 tree case_low, case_high;
7700 tree min = vr->min, max = vr->max;
7702 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7704 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7706 /* Set second range to emtpy. */
7707 *min_idx2 = 1;
7708 *max_idx2 = 0;
7710 if (vr->type == VR_RANGE)
7712 *min_idx1 = i;
7713 *max_idx1 = j;
7714 return !take_default;
7717 /* Set first range to all case labels. */
7718 *min_idx1 = 1;
7719 *max_idx1 = n - 1;
7721 if (i > j)
7722 return false;
7724 /* Make sure all the values of case labels [i , j] are contained in
7725 range [MIN, MAX]. */
7726 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7727 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7728 if (tree_int_cst_compare (case_low, min) < 0)
7729 i += 1;
7730 if (case_high != NULL_TREE
7731 && tree_int_cst_compare (max, case_high) < 0)
7732 j -= 1;
7734 if (i > j)
7735 return false;
7737 /* If the range spans case labels [i, j], the corresponding anti-range spans
7738 the labels [1, i - 1] and [j + 1, n - 1]. */
7739 k = j + 1;
7740 l = n - 1;
7741 if (k > l)
7743 k = 1;
7744 l = 0;
7747 j = i - 1;
7748 i = 1;
7749 if (i > j)
7751 i = k;
7752 j = l;
7753 k = 1;
7754 l = 0;
7757 *min_idx1 = i;
7758 *max_idx1 = j;
7759 *min_idx2 = k;
7760 *max_idx2 = l;
7761 return false;
7764 /* Visit switch statement STMT. If we can determine which edge
7765 will be taken out of STMT's basic block, record it in
7766 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7767 SSA_PROP_VARYING. */
7769 static enum ssa_prop_result
7770 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7772 tree op, val;
7773 value_range *vr;
7774 size_t i = 0, j = 0, k, l;
7775 bool take_default;
7777 *taken_edge_p = NULL;
7778 op = gimple_switch_index (stmt);
7779 if (TREE_CODE (op) != SSA_NAME)
7780 return SSA_PROP_VARYING;
7782 vr = get_value_range (op);
7783 if (dump_file && (dump_flags & TDF_DETAILS))
7785 fprintf (dump_file, "\nVisiting switch expression with operand ");
7786 print_generic_expr (dump_file, op, 0);
7787 fprintf (dump_file, " with known range ");
7788 dump_value_range (dump_file, vr);
7789 fprintf (dump_file, "\n");
7792 if ((vr->type != VR_RANGE
7793 && vr->type != VR_ANTI_RANGE)
7794 || symbolic_range_p (vr))
7795 return SSA_PROP_VARYING;
7797 /* Find the single edge that is taken from the switch expression. */
7798 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7800 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7801 label */
7802 if (j < i)
7804 gcc_assert (take_default);
7805 val = gimple_switch_default_label (stmt);
7807 else
7809 /* Check if labels with index i to j and maybe the default label
7810 are all reaching the same label. */
7812 val = gimple_switch_label (stmt, i);
7813 if (take_default
7814 && CASE_LABEL (gimple_switch_default_label (stmt))
7815 != CASE_LABEL (val))
7817 if (dump_file && (dump_flags & TDF_DETAILS))
7818 fprintf (dump_file, " not a single destination for this "
7819 "range\n");
7820 return SSA_PROP_VARYING;
7822 for (++i; i <= j; ++i)
7824 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7826 if (dump_file && (dump_flags & TDF_DETAILS))
7827 fprintf (dump_file, " not a single destination for this "
7828 "range\n");
7829 return SSA_PROP_VARYING;
7832 for (; k <= l; ++k)
7834 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7836 if (dump_file && (dump_flags & TDF_DETAILS))
7837 fprintf (dump_file, " not a single destination for this "
7838 "range\n");
7839 return SSA_PROP_VARYING;
7844 *taken_edge_p = find_edge (gimple_bb (stmt),
7845 label_to_block (CASE_LABEL (val)));
7847 if (dump_file && (dump_flags & TDF_DETAILS))
7849 fprintf (dump_file, " will take edge to ");
7850 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7853 return SSA_PROP_INTERESTING;
7857 /* Evaluate statement STMT. If the statement produces a useful range,
7858 return SSA_PROP_INTERESTING and record the SSA name with the
7859 interesting range into *OUTPUT_P.
7861 If STMT is a conditional branch and we can determine its truth
7862 value, the taken edge is recorded in *TAKEN_EDGE_P.
7864 If STMT produces a varying value, return SSA_PROP_VARYING. */
7866 static enum ssa_prop_result
7867 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
7869 tree def;
7870 ssa_op_iter iter;
7872 if (dump_file && (dump_flags & TDF_DETAILS))
7874 fprintf (dump_file, "\nVisiting statement:\n");
7875 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7878 if (!stmt_interesting_for_vrp (stmt))
7879 gcc_assert (stmt_ends_bb_p (stmt));
7880 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7881 return vrp_visit_assignment_or_call (stmt, output_p);
7882 else if (gimple_code (stmt) == GIMPLE_COND)
7883 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7884 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7885 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7887 /* All other statements produce nothing of interest for VRP, so mark
7888 their outputs varying and prevent further simulation. */
7889 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7890 set_value_range_to_varying (get_value_range (def));
7892 return SSA_PROP_VARYING;
7895 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7896 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7897 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7898 possible such range. The resulting range is not canonicalized. */
7900 static void
7901 union_ranges (enum value_range_type *vr0type,
7902 tree *vr0min, tree *vr0max,
7903 enum value_range_type vr1type,
7904 tree vr1min, tree vr1max)
7906 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7907 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7909 /* [] is vr0, () is vr1 in the following classification comments. */
7910 if (mineq && maxeq)
7912 /* [( )] */
7913 if (*vr0type == vr1type)
7914 /* Nothing to do for equal ranges. */
7916 else if ((*vr0type == VR_RANGE
7917 && vr1type == VR_ANTI_RANGE)
7918 || (*vr0type == VR_ANTI_RANGE
7919 && vr1type == VR_RANGE))
7921 /* For anti-range with range union the result is varying. */
7922 goto give_up;
7924 else
7925 gcc_unreachable ();
7927 else if (operand_less_p (*vr0max, vr1min) == 1
7928 || operand_less_p (vr1max, *vr0min) == 1)
7930 /* [ ] ( ) or ( ) [ ]
7931 If the ranges have an empty intersection, result of the union
7932 operation is the anti-range or if both are anti-ranges
7933 it covers all. */
7934 if (*vr0type == VR_ANTI_RANGE
7935 && vr1type == VR_ANTI_RANGE)
7936 goto give_up;
7937 else if (*vr0type == VR_ANTI_RANGE
7938 && vr1type == VR_RANGE)
7940 else if (*vr0type == VR_RANGE
7941 && vr1type == VR_ANTI_RANGE)
7943 *vr0type = vr1type;
7944 *vr0min = vr1min;
7945 *vr0max = vr1max;
7947 else if (*vr0type == VR_RANGE
7948 && vr1type == VR_RANGE)
7950 /* The result is the convex hull of both ranges. */
7951 if (operand_less_p (*vr0max, vr1min) == 1)
7953 /* If the result can be an anti-range, create one. */
7954 if (TREE_CODE (*vr0max) == INTEGER_CST
7955 && TREE_CODE (vr1min) == INTEGER_CST
7956 && vrp_val_is_min (*vr0min)
7957 && vrp_val_is_max (vr1max))
7959 tree min = int_const_binop (PLUS_EXPR,
7960 *vr0max,
7961 build_int_cst (TREE_TYPE (*vr0max), 1));
7962 tree max = int_const_binop (MINUS_EXPR,
7963 vr1min,
7964 build_int_cst (TREE_TYPE (vr1min), 1));
7965 if (!operand_less_p (max, min))
7967 *vr0type = VR_ANTI_RANGE;
7968 *vr0min = min;
7969 *vr0max = max;
7971 else
7972 *vr0max = vr1max;
7974 else
7975 *vr0max = vr1max;
7977 else
7979 /* If the result can be an anti-range, create one. */
7980 if (TREE_CODE (vr1max) == INTEGER_CST
7981 && TREE_CODE (*vr0min) == INTEGER_CST
7982 && vrp_val_is_min (vr1min)
7983 && vrp_val_is_max (*vr0max))
7985 tree min = int_const_binop (PLUS_EXPR,
7986 vr1max,
7987 build_int_cst (TREE_TYPE (vr1max), 1));
7988 tree max = int_const_binop (MINUS_EXPR,
7989 *vr0min,
7990 build_int_cst (TREE_TYPE (*vr0min), 1));
7991 if (!operand_less_p (max, min))
7993 *vr0type = VR_ANTI_RANGE;
7994 *vr0min = min;
7995 *vr0max = max;
7997 else
7998 *vr0min = vr1min;
8000 else
8001 *vr0min = vr1min;
8004 else
8005 gcc_unreachable ();
8007 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8008 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8010 /* [ ( ) ] or [( ) ] or [ ( )] */
8011 if (*vr0type == VR_RANGE
8012 && vr1type == VR_RANGE)
8014 else if (*vr0type == VR_ANTI_RANGE
8015 && vr1type == VR_ANTI_RANGE)
8017 *vr0type = vr1type;
8018 *vr0min = vr1min;
8019 *vr0max = vr1max;
8021 else if (*vr0type == VR_ANTI_RANGE
8022 && vr1type == VR_RANGE)
8024 /* Arbitrarily choose the right or left gap. */
8025 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8026 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8027 build_int_cst (TREE_TYPE (vr1min), 1));
8028 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8029 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8030 build_int_cst (TREE_TYPE (vr1max), 1));
8031 else
8032 goto give_up;
8034 else if (*vr0type == VR_RANGE
8035 && vr1type == VR_ANTI_RANGE)
8036 /* The result covers everything. */
8037 goto give_up;
8038 else
8039 gcc_unreachable ();
8041 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8042 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8044 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8045 if (*vr0type == VR_RANGE
8046 && vr1type == VR_RANGE)
8048 *vr0type = vr1type;
8049 *vr0min = vr1min;
8050 *vr0max = vr1max;
8052 else if (*vr0type == VR_ANTI_RANGE
8053 && vr1type == VR_ANTI_RANGE)
8055 else if (*vr0type == VR_RANGE
8056 && vr1type == VR_ANTI_RANGE)
8058 *vr0type = VR_ANTI_RANGE;
8059 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8061 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8062 build_int_cst (TREE_TYPE (*vr0min), 1));
8063 *vr0min = vr1min;
8065 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8067 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8068 build_int_cst (TREE_TYPE (*vr0max), 1));
8069 *vr0max = vr1max;
8071 else
8072 goto give_up;
8074 else if (*vr0type == VR_ANTI_RANGE
8075 && vr1type == VR_RANGE)
8076 /* The result covers everything. */
8077 goto give_up;
8078 else
8079 gcc_unreachable ();
8081 else if ((operand_less_p (vr1min, *vr0max) == 1
8082 || operand_equal_p (vr1min, *vr0max, 0))
8083 && operand_less_p (*vr0min, vr1min) == 1
8084 && operand_less_p (*vr0max, vr1max) == 1)
8086 /* [ ( ] ) or [ ]( ) */
8087 if (*vr0type == VR_RANGE
8088 && vr1type == VR_RANGE)
8089 *vr0max = vr1max;
8090 else if (*vr0type == VR_ANTI_RANGE
8091 && vr1type == VR_ANTI_RANGE)
8092 *vr0min = vr1min;
8093 else if (*vr0type == VR_ANTI_RANGE
8094 && vr1type == VR_RANGE)
8096 if (TREE_CODE (vr1min) == INTEGER_CST)
8097 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8098 build_int_cst (TREE_TYPE (vr1min), 1));
8099 else
8100 goto give_up;
8102 else if (*vr0type == VR_RANGE
8103 && vr1type == VR_ANTI_RANGE)
8105 if (TREE_CODE (*vr0max) == INTEGER_CST)
8107 *vr0type = vr1type;
8108 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8109 build_int_cst (TREE_TYPE (*vr0max), 1));
8110 *vr0max = vr1max;
8112 else
8113 goto give_up;
8115 else
8116 gcc_unreachable ();
8118 else if ((operand_less_p (*vr0min, vr1max) == 1
8119 || operand_equal_p (*vr0min, vr1max, 0))
8120 && operand_less_p (vr1min, *vr0min) == 1
8121 && operand_less_p (vr1max, *vr0max) == 1)
8123 /* ( [ ) ] or ( )[ ] */
8124 if (*vr0type == VR_RANGE
8125 && vr1type == VR_RANGE)
8126 *vr0min = vr1min;
8127 else if (*vr0type == VR_ANTI_RANGE
8128 && vr1type == VR_ANTI_RANGE)
8129 *vr0max = vr1max;
8130 else if (*vr0type == VR_ANTI_RANGE
8131 && vr1type == VR_RANGE)
8133 if (TREE_CODE (vr1max) == INTEGER_CST)
8134 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8135 build_int_cst (TREE_TYPE (vr1max), 1));
8136 else
8137 goto give_up;
8139 else if (*vr0type == VR_RANGE
8140 && vr1type == VR_ANTI_RANGE)
8142 if (TREE_CODE (*vr0min) == INTEGER_CST)
8144 *vr0type = vr1type;
8145 *vr0min = vr1min;
8146 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8147 build_int_cst (TREE_TYPE (*vr0min), 1));
8149 else
8150 goto give_up;
8152 else
8153 gcc_unreachable ();
8155 else
8156 goto give_up;
8158 return;
8160 give_up:
8161 *vr0type = VR_VARYING;
8162 *vr0min = NULL_TREE;
8163 *vr0max = NULL_TREE;
8166 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8167 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8168 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8169 possible such range. The resulting range is not canonicalized. */
8171 static void
8172 intersect_ranges (enum value_range_type *vr0type,
8173 tree *vr0min, tree *vr0max,
8174 enum value_range_type vr1type,
8175 tree vr1min, tree vr1max)
8177 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8178 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8180 /* [] is vr0, () is vr1 in the following classification comments. */
8181 if (mineq && maxeq)
8183 /* [( )] */
8184 if (*vr0type == vr1type)
8185 /* Nothing to do for equal ranges. */
8187 else if ((*vr0type == VR_RANGE
8188 && vr1type == VR_ANTI_RANGE)
8189 || (*vr0type == VR_ANTI_RANGE
8190 && vr1type == VR_RANGE))
8192 /* For anti-range with range intersection the result is empty. */
8193 *vr0type = VR_UNDEFINED;
8194 *vr0min = NULL_TREE;
8195 *vr0max = NULL_TREE;
8197 else
8198 gcc_unreachable ();
8200 else if (operand_less_p (*vr0max, vr1min) == 1
8201 || operand_less_p (vr1max, *vr0min) == 1)
8203 /* [ ] ( ) or ( ) [ ]
8204 If the ranges have an empty intersection, the result of the
8205 intersect operation is the range for intersecting an
8206 anti-range with a range or empty when intersecting two ranges. */
8207 if (*vr0type == VR_RANGE
8208 && vr1type == VR_ANTI_RANGE)
8210 else if (*vr0type == VR_ANTI_RANGE
8211 && vr1type == VR_RANGE)
8213 *vr0type = vr1type;
8214 *vr0min = vr1min;
8215 *vr0max = vr1max;
8217 else if (*vr0type == VR_RANGE
8218 && vr1type == VR_RANGE)
8220 *vr0type = VR_UNDEFINED;
8221 *vr0min = NULL_TREE;
8222 *vr0max = NULL_TREE;
8224 else if (*vr0type == VR_ANTI_RANGE
8225 && vr1type == VR_ANTI_RANGE)
8227 /* If the anti-ranges are adjacent to each other merge them. */
8228 if (TREE_CODE (*vr0max) == INTEGER_CST
8229 && TREE_CODE (vr1min) == INTEGER_CST
8230 && operand_less_p (*vr0max, vr1min) == 1
8231 && integer_onep (int_const_binop (MINUS_EXPR,
8232 vr1min, *vr0max)))
8233 *vr0max = vr1max;
8234 else if (TREE_CODE (vr1max) == INTEGER_CST
8235 && TREE_CODE (*vr0min) == INTEGER_CST
8236 && operand_less_p (vr1max, *vr0min) == 1
8237 && integer_onep (int_const_binop (MINUS_EXPR,
8238 *vr0min, vr1max)))
8239 *vr0min = vr1min;
8240 /* Else arbitrarily take VR0. */
8243 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8244 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8246 /* [ ( ) ] or [( ) ] or [ ( )] */
8247 if (*vr0type == VR_RANGE
8248 && vr1type == VR_RANGE)
8250 /* If both are ranges the result is the inner one. */
8251 *vr0type = vr1type;
8252 *vr0min = vr1min;
8253 *vr0max = vr1max;
8255 else if (*vr0type == VR_RANGE
8256 && vr1type == VR_ANTI_RANGE)
8258 /* Choose the right gap if the left one is empty. */
8259 if (mineq)
8261 if (TREE_CODE (vr1max) == INTEGER_CST)
8262 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8263 build_int_cst (TREE_TYPE (vr1max), 1));
8264 else
8265 *vr0min = vr1max;
8267 /* Choose the left gap if the right one is empty. */
8268 else if (maxeq)
8270 if (TREE_CODE (vr1min) == INTEGER_CST)
8271 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8272 build_int_cst (TREE_TYPE (vr1min), 1));
8273 else
8274 *vr0max = vr1min;
8276 /* Choose the anti-range if the range is effectively varying. */
8277 else if (vrp_val_is_min (*vr0min)
8278 && vrp_val_is_max (*vr0max))
8280 *vr0type = vr1type;
8281 *vr0min = vr1min;
8282 *vr0max = vr1max;
8284 /* Else choose the range. */
8286 else if (*vr0type == VR_ANTI_RANGE
8287 && vr1type == VR_ANTI_RANGE)
8288 /* If both are anti-ranges the result is the outer one. */
8290 else if (*vr0type == VR_ANTI_RANGE
8291 && vr1type == VR_RANGE)
8293 /* The intersection is empty. */
8294 *vr0type = VR_UNDEFINED;
8295 *vr0min = NULL_TREE;
8296 *vr0max = NULL_TREE;
8298 else
8299 gcc_unreachable ();
8301 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8302 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8304 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8305 if (*vr0type == VR_RANGE
8306 && vr1type == VR_RANGE)
8307 /* Choose the inner range. */
8309 else if (*vr0type == VR_ANTI_RANGE
8310 && vr1type == VR_RANGE)
8312 /* Choose the right gap if the left is empty. */
8313 if (mineq)
8315 *vr0type = VR_RANGE;
8316 if (TREE_CODE (*vr0max) == INTEGER_CST)
8317 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8318 build_int_cst (TREE_TYPE (*vr0max), 1));
8319 else
8320 *vr0min = *vr0max;
8321 *vr0max = vr1max;
8323 /* Choose the left gap if the right is empty. */
8324 else if (maxeq)
8326 *vr0type = VR_RANGE;
8327 if (TREE_CODE (*vr0min) == INTEGER_CST)
8328 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8329 build_int_cst (TREE_TYPE (*vr0min), 1));
8330 else
8331 *vr0max = *vr0min;
8332 *vr0min = vr1min;
8334 /* Choose the anti-range if the range is effectively varying. */
8335 else if (vrp_val_is_min (vr1min)
8336 && vrp_val_is_max (vr1max))
8338 /* Else choose the range. */
8339 else
8341 *vr0type = vr1type;
8342 *vr0min = vr1min;
8343 *vr0max = vr1max;
8346 else if (*vr0type == VR_ANTI_RANGE
8347 && vr1type == VR_ANTI_RANGE)
8349 /* If both are anti-ranges the result is the outer one. */
8350 *vr0type = vr1type;
8351 *vr0min = vr1min;
8352 *vr0max = vr1max;
8354 else if (vr1type == VR_ANTI_RANGE
8355 && *vr0type == VR_RANGE)
8357 /* The intersection is empty. */
8358 *vr0type = VR_UNDEFINED;
8359 *vr0min = NULL_TREE;
8360 *vr0max = NULL_TREE;
8362 else
8363 gcc_unreachable ();
8365 else if ((operand_less_p (vr1min, *vr0max) == 1
8366 || operand_equal_p (vr1min, *vr0max, 0))
8367 && operand_less_p (*vr0min, vr1min) == 1)
8369 /* [ ( ] ) or [ ]( ) */
8370 if (*vr0type == VR_ANTI_RANGE
8371 && vr1type == VR_ANTI_RANGE)
8372 *vr0max = vr1max;
8373 else if (*vr0type == VR_RANGE
8374 && vr1type == VR_RANGE)
8375 *vr0min = vr1min;
8376 else if (*vr0type == VR_RANGE
8377 && vr1type == VR_ANTI_RANGE)
8379 if (TREE_CODE (vr1min) == INTEGER_CST)
8380 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8381 build_int_cst (TREE_TYPE (vr1min), 1));
8382 else
8383 *vr0max = vr1min;
8385 else if (*vr0type == VR_ANTI_RANGE
8386 && vr1type == VR_RANGE)
8388 *vr0type = VR_RANGE;
8389 if (TREE_CODE (*vr0max) == INTEGER_CST)
8390 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8391 build_int_cst (TREE_TYPE (*vr0max), 1));
8392 else
8393 *vr0min = *vr0max;
8394 *vr0max = vr1max;
8396 else
8397 gcc_unreachable ();
8399 else if ((operand_less_p (*vr0min, vr1max) == 1
8400 || operand_equal_p (*vr0min, vr1max, 0))
8401 && operand_less_p (vr1min, *vr0min) == 1)
8403 /* ( [ ) ] or ( )[ ] */
8404 if (*vr0type == VR_ANTI_RANGE
8405 && vr1type == VR_ANTI_RANGE)
8406 *vr0min = vr1min;
8407 else if (*vr0type == VR_RANGE
8408 && vr1type == VR_RANGE)
8409 *vr0max = vr1max;
8410 else if (*vr0type == VR_RANGE
8411 && vr1type == VR_ANTI_RANGE)
8413 if (TREE_CODE (vr1max) == INTEGER_CST)
8414 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8415 build_int_cst (TREE_TYPE (vr1max), 1));
8416 else
8417 *vr0min = vr1max;
8419 else if (*vr0type == VR_ANTI_RANGE
8420 && vr1type == VR_RANGE)
8422 *vr0type = VR_RANGE;
8423 if (TREE_CODE (*vr0min) == INTEGER_CST)
8424 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8425 build_int_cst (TREE_TYPE (*vr0min), 1));
8426 else
8427 *vr0max = *vr0min;
8428 *vr0min = vr1min;
8430 else
8431 gcc_unreachable ();
8434 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8435 result for the intersection. That's always a conservative
8436 correct estimate. */
8438 return;
8442 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8443 in *VR0. This may not be the smallest possible such range. */
8445 static void
8446 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8448 value_range saved;
8450 /* If either range is VR_VARYING the other one wins. */
8451 if (vr1->type == VR_VARYING)
8452 return;
8453 if (vr0->type == VR_VARYING)
8455 copy_value_range (vr0, vr1);
8456 return;
8459 /* When either range is VR_UNDEFINED the resulting range is
8460 VR_UNDEFINED, too. */
8461 if (vr0->type == VR_UNDEFINED)
8462 return;
8463 if (vr1->type == VR_UNDEFINED)
8465 set_value_range_to_undefined (vr0);
8466 return;
8469 /* Save the original vr0 so we can return it as conservative intersection
8470 result when our worker turns things to varying. */
8471 saved = *vr0;
8472 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8473 vr1->type, vr1->min, vr1->max);
8474 /* Make sure to canonicalize the result though as the inversion of a
8475 VR_RANGE can still be a VR_RANGE. */
8476 set_and_canonicalize_value_range (vr0, vr0->type,
8477 vr0->min, vr0->max, vr0->equiv);
8478 /* If that failed, use the saved original VR0. */
8479 if (vr0->type == VR_VARYING)
8481 *vr0 = saved;
8482 return;
8484 /* If the result is VR_UNDEFINED there is no need to mess with
8485 the equivalencies. */
8486 if (vr0->type == VR_UNDEFINED)
8487 return;
8489 /* The resulting set of equivalences for range intersection is the union of
8490 the two sets. */
8491 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8492 bitmap_ior_into (vr0->equiv, vr1->equiv);
8493 else if (vr1->equiv && !vr0->equiv)
8494 bitmap_copy (vr0->equiv, vr1->equiv);
8497 static void
8498 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8500 if (dump_file && (dump_flags & TDF_DETAILS))
8502 fprintf (dump_file, "Intersecting\n ");
8503 dump_value_range (dump_file, vr0);
8504 fprintf (dump_file, "\nand\n ");
8505 dump_value_range (dump_file, vr1);
8506 fprintf (dump_file, "\n");
8508 vrp_intersect_ranges_1 (vr0, vr1);
8509 if (dump_file && (dump_flags & TDF_DETAILS))
8511 fprintf (dump_file, "to\n ");
8512 dump_value_range (dump_file, vr0);
8513 fprintf (dump_file, "\n");
8517 /* Meet operation for value ranges. Given two value ranges VR0 and
8518 VR1, store in VR0 a range that contains both VR0 and VR1. This
8519 may not be the smallest possible such range. */
8521 static void
8522 vrp_meet_1 (value_range *vr0, value_range *vr1)
8524 value_range saved;
8526 if (vr0->type == VR_UNDEFINED)
8528 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8529 return;
8532 if (vr1->type == VR_UNDEFINED)
8534 /* VR0 already has the resulting range. */
8535 return;
8538 if (vr0->type == VR_VARYING)
8540 /* Nothing to do. VR0 already has the resulting range. */
8541 return;
8544 if (vr1->type == VR_VARYING)
8546 set_value_range_to_varying (vr0);
8547 return;
8550 saved = *vr0;
8551 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8552 vr1->type, vr1->min, vr1->max);
8553 if (vr0->type == VR_VARYING)
8555 /* Failed to find an efficient meet. Before giving up and setting
8556 the result to VARYING, see if we can at least derive a useful
8557 anti-range. FIXME, all this nonsense about distinguishing
8558 anti-ranges from ranges is necessary because of the odd
8559 semantics of range_includes_zero_p and friends. */
8560 if (((saved.type == VR_RANGE
8561 && range_includes_zero_p (saved.min, saved.max) == 0)
8562 || (saved.type == VR_ANTI_RANGE
8563 && range_includes_zero_p (saved.min, saved.max) == 1))
8564 && ((vr1->type == VR_RANGE
8565 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8566 || (vr1->type == VR_ANTI_RANGE
8567 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8569 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8571 /* Since this meet operation did not result from the meeting of
8572 two equivalent names, VR0 cannot have any equivalences. */
8573 if (vr0->equiv)
8574 bitmap_clear (vr0->equiv);
8575 return;
8578 set_value_range_to_varying (vr0);
8579 return;
8581 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8582 vr0->equiv);
8583 if (vr0->type == VR_VARYING)
8584 return;
8586 /* The resulting set of equivalences is always the intersection of
8587 the two sets. */
8588 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8589 bitmap_and_into (vr0->equiv, vr1->equiv);
8590 else if (vr0->equiv && !vr1->equiv)
8591 bitmap_clear (vr0->equiv);
8594 static void
8595 vrp_meet (value_range *vr0, value_range *vr1)
8597 if (dump_file && (dump_flags & TDF_DETAILS))
8599 fprintf (dump_file, "Meeting\n ");
8600 dump_value_range (dump_file, vr0);
8601 fprintf (dump_file, "\nand\n ");
8602 dump_value_range (dump_file, vr1);
8603 fprintf (dump_file, "\n");
8605 vrp_meet_1 (vr0, vr1);
8606 if (dump_file && (dump_flags & TDF_DETAILS))
8608 fprintf (dump_file, "to\n ");
8609 dump_value_range (dump_file, vr0);
8610 fprintf (dump_file, "\n");
8615 /* Visit all arguments for PHI node PHI that flow through executable
8616 edges. If a valid value range can be derived from all the incoming
8617 value ranges, set a new range for the LHS of PHI. */
8619 static enum ssa_prop_result
8620 vrp_visit_phi_node (gphi *phi)
8622 size_t i;
8623 tree lhs = PHI_RESULT (phi);
8624 value_range *lhs_vr = get_value_range (lhs);
8625 value_range vr_result = VR_INITIALIZER;
8626 bool first = true;
8627 int edges, old_edges;
8628 struct loop *l;
8630 if (dump_file && (dump_flags & TDF_DETAILS))
8632 fprintf (dump_file, "\nVisiting PHI node: ");
8633 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8636 edges = 0;
8637 for (i = 0; i < gimple_phi_num_args (phi); i++)
8639 edge e = gimple_phi_arg_edge (phi, i);
8641 if (dump_file && (dump_flags & TDF_DETAILS))
8643 fprintf (dump_file,
8644 " Argument #%d (%d -> %d %sexecutable)\n",
8645 (int) i, e->src->index, e->dest->index,
8646 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8649 if (e->flags & EDGE_EXECUTABLE)
8651 tree arg = PHI_ARG_DEF (phi, i);
8652 value_range vr_arg;
8654 ++edges;
8656 if (TREE_CODE (arg) == SSA_NAME)
8658 vr_arg = *(get_value_range (arg));
8659 /* Do not allow equivalences or symbolic ranges to leak in from
8660 backedges. That creates invalid equivalencies.
8661 See PR53465 and PR54767. */
8662 if (e->flags & EDGE_DFS_BACK)
8664 if (vr_arg.type == VR_RANGE
8665 || vr_arg.type == VR_ANTI_RANGE)
8667 vr_arg.equiv = NULL;
8668 if (symbolic_range_p (&vr_arg))
8670 vr_arg.type = VR_VARYING;
8671 vr_arg.min = NULL_TREE;
8672 vr_arg.max = NULL_TREE;
8676 else
8678 /* If the non-backedge arguments range is VR_VARYING then
8679 we can still try recording a simple equivalence. */
8680 if (vr_arg.type == VR_VARYING)
8682 vr_arg.type = VR_RANGE;
8683 vr_arg.min = arg;
8684 vr_arg.max = arg;
8685 vr_arg.equiv = NULL;
8689 else
8691 if (TREE_OVERFLOW_P (arg))
8692 arg = drop_tree_overflow (arg);
8694 vr_arg.type = VR_RANGE;
8695 vr_arg.min = arg;
8696 vr_arg.max = arg;
8697 vr_arg.equiv = NULL;
8700 if (dump_file && (dump_flags & TDF_DETAILS))
8702 fprintf (dump_file, "\t");
8703 print_generic_expr (dump_file, arg, dump_flags);
8704 fprintf (dump_file, ": ");
8705 dump_value_range (dump_file, &vr_arg);
8706 fprintf (dump_file, "\n");
8709 if (first)
8710 copy_value_range (&vr_result, &vr_arg);
8711 else
8712 vrp_meet (&vr_result, &vr_arg);
8713 first = false;
8715 if (vr_result.type == VR_VARYING)
8716 break;
8720 if (vr_result.type == VR_VARYING)
8721 goto varying;
8722 else if (vr_result.type == VR_UNDEFINED)
8723 goto update_range;
8725 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8726 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8728 /* To prevent infinite iterations in the algorithm, derive ranges
8729 when the new value is slightly bigger or smaller than the
8730 previous one. We don't do this if we have seen a new executable
8731 edge; this helps us avoid an overflow infinity for conditionals
8732 which are not in a loop. If the old value-range was VR_UNDEFINED
8733 use the updated range and iterate one more time. */
8734 if (edges > 0
8735 && gimple_phi_num_args (phi) > 1
8736 && edges == old_edges
8737 && lhs_vr->type != VR_UNDEFINED)
8739 /* Compare old and new ranges, fall back to varying if the
8740 values are not comparable. */
8741 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8742 if (cmp_min == -2)
8743 goto varying;
8744 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8745 if (cmp_max == -2)
8746 goto varying;
8748 /* For non VR_RANGE or for pointers fall back to varying if
8749 the range changed. */
8750 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8751 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8752 && (cmp_min != 0 || cmp_max != 0))
8753 goto varying;
8755 /* If the new minimum is larger than the previous one
8756 retain the old value. If the new minimum value is smaller
8757 than the previous one and not -INF go all the way to -INF + 1.
8758 In the first case, to avoid infinite bouncing between different
8759 minimums, and in the other case to avoid iterating millions of
8760 times to reach -INF. Going to -INF + 1 also lets the following
8761 iteration compute whether there will be any overflow, at the
8762 expense of one additional iteration. */
8763 if (cmp_min < 0)
8764 vr_result.min = lhs_vr->min;
8765 else if (cmp_min > 0
8766 && !vrp_val_is_min (vr_result.min))
8767 vr_result.min
8768 = int_const_binop (PLUS_EXPR,
8769 vrp_val_min (TREE_TYPE (vr_result.min)),
8770 build_int_cst (TREE_TYPE (vr_result.min), 1));
8772 /* Similarly for the maximum value. */
8773 if (cmp_max > 0)
8774 vr_result.max = lhs_vr->max;
8775 else if (cmp_max < 0
8776 && !vrp_val_is_max (vr_result.max))
8777 vr_result.max
8778 = int_const_binop (MINUS_EXPR,
8779 vrp_val_max (TREE_TYPE (vr_result.min)),
8780 build_int_cst (TREE_TYPE (vr_result.min), 1));
8782 /* If we dropped either bound to +-INF then if this is a loop
8783 PHI node SCEV may known more about its value-range. */
8784 if (cmp_min > 0 || cmp_min < 0
8785 || cmp_max < 0 || cmp_max > 0)
8786 goto scev_check;
8788 goto infinite_check;
8791 /* If the new range is different than the previous value, keep
8792 iterating. */
8793 update_range:
8794 if (update_value_range (lhs, &vr_result))
8796 if (dump_file && (dump_flags & TDF_DETAILS))
8798 fprintf (dump_file, "Found new range for ");
8799 print_generic_expr (dump_file, lhs, 0);
8800 fprintf (dump_file, ": ");
8801 dump_value_range (dump_file, &vr_result);
8802 fprintf (dump_file, "\n");
8805 if (vr_result.type == VR_VARYING)
8806 return SSA_PROP_VARYING;
8808 return SSA_PROP_INTERESTING;
8811 /* Nothing changed, don't add outgoing edges. */
8812 return SSA_PROP_NOT_INTERESTING;
8814 varying:
8815 set_value_range_to_varying (&vr_result);
8817 scev_check:
8818 /* If this is a loop PHI node SCEV may known more about its value-range.
8819 scev_check can be reached from two paths, one is a fall through from above
8820 "varying" label, the other is direct goto from code block which tries to
8821 avoid infinite simulation. */
8822 if ((l = loop_containing_stmt (phi))
8823 && l->header == gimple_bb (phi))
8824 adjust_range_with_scev (&vr_result, l, phi, lhs);
8826 infinite_check:
8827 /* If we will end up with a (-INF, +INF) range, set it to
8828 VARYING. Same if the previous max value was invalid for
8829 the type and we end up with vr_result.min > vr_result.max. */
8830 if ((vr_result.type == VR_RANGE || vr_result.type == VR_ANTI_RANGE)
8831 && !((vrp_val_is_max (vr_result.max) && vrp_val_is_min (vr_result.min))
8832 || compare_values (vr_result.min, vr_result.max) > 0))
8833 goto update_range;
8835 /* No match found. Set the LHS to VARYING. */
8836 set_value_range_to_varying (lhs_vr);
8837 return SSA_PROP_VARYING;
8840 /* Simplify boolean operations if the source is known
8841 to be already a boolean. */
8842 static bool
8843 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
8845 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8846 tree lhs, op0, op1;
8847 bool need_conversion;
8849 /* We handle only !=/== case here. */
8850 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8852 op0 = gimple_assign_rhs1 (stmt);
8853 if (!op_with_boolean_value_range_p (op0))
8854 return false;
8856 op1 = gimple_assign_rhs2 (stmt);
8857 if (!op_with_boolean_value_range_p (op1))
8858 return false;
8860 /* Reduce number of cases to handle to NE_EXPR. As there is no
8861 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8862 if (rhs_code == EQ_EXPR)
8864 if (TREE_CODE (op1) == INTEGER_CST)
8865 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8866 build_int_cst (TREE_TYPE (op1), 1));
8867 else
8868 return false;
8871 lhs = gimple_assign_lhs (stmt);
8872 need_conversion
8873 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8875 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8876 if (need_conversion
8877 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8878 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8879 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8880 return false;
8882 /* For A != 0 we can substitute A itself. */
8883 if (integer_zerop (op1))
8884 gimple_assign_set_rhs_with_ops (gsi,
8885 need_conversion
8886 ? NOP_EXPR : TREE_CODE (op0), op0);
8887 /* For A != B we substitute A ^ B. Either with conversion. */
8888 else if (need_conversion)
8890 tree tem = make_ssa_name (TREE_TYPE (op0));
8891 gassign *newop
8892 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
8893 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8894 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8895 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
8896 set_range_info (tem, VR_RANGE,
8897 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
8898 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
8899 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
8901 /* Or without. */
8902 else
8903 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8904 update_stmt (gsi_stmt (*gsi));
8906 return true;
8909 /* Simplify a division or modulo operator to a right shift or
8910 bitwise and if the first operand is unsigned or is greater
8911 than zero and the second operand is an exact power of two.
8912 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
8913 into just op0 if op0's range is known to be a subset of
8914 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
8915 modulo. */
8917 static bool
8918 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
8920 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8921 tree val = NULL;
8922 tree op0 = gimple_assign_rhs1 (stmt);
8923 tree op1 = gimple_assign_rhs2 (stmt);
8924 value_range *vr = get_value_range (op0);
8926 if (rhs_code == TRUNC_MOD_EXPR
8927 && TREE_CODE (op1) == INTEGER_CST
8928 && tree_int_cst_sgn (op1) == 1
8929 && range_int_cst_p (vr)
8930 && tree_int_cst_lt (vr->max, op1))
8932 if (TYPE_UNSIGNED (TREE_TYPE (op0))
8933 || tree_int_cst_sgn (vr->min) >= 0
8934 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
8935 vr->min))
8937 /* If op0 already has the range op0 % op1 has,
8938 then TRUNC_MOD_EXPR won't change anything. */
8939 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
8940 gimple_assign_set_rhs_from_tree (&gsi, op0);
8941 update_stmt (stmt);
8942 return true;
8946 if (!integer_pow2p (op1))
8948 /* X % -Y can be only optimized into X % Y either if
8949 X is not INT_MIN, or Y is not -1. Fold it now, as after
8950 remove_range_assertions the range info might be not available
8951 anymore. */
8952 if (rhs_code == TRUNC_MOD_EXPR
8953 && fold_stmt (gsi, follow_single_use_edges))
8954 return true;
8955 return false;
8958 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8959 val = integer_one_node;
8960 else
8962 bool sop = false;
8964 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8966 if (val
8967 && sop
8968 && integer_onep (val)
8969 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8971 location_t location;
8973 if (!gimple_has_location (stmt))
8974 location = input_location;
8975 else
8976 location = gimple_location (stmt);
8977 warning_at (location, OPT_Wstrict_overflow,
8978 "assuming signed overflow does not occur when "
8979 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8983 if (val && integer_onep (val))
8985 tree t;
8987 if (rhs_code == TRUNC_DIV_EXPR)
8989 t = build_int_cst (integer_type_node, tree_log2 (op1));
8990 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8991 gimple_assign_set_rhs1 (stmt, op0);
8992 gimple_assign_set_rhs2 (stmt, t);
8994 else
8996 t = build_int_cst (TREE_TYPE (op1), 1);
8997 t = int_const_binop (MINUS_EXPR, op1, t);
8998 t = fold_convert (TREE_TYPE (op0), t);
9000 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9001 gimple_assign_set_rhs1 (stmt, op0);
9002 gimple_assign_set_rhs2 (stmt, t);
9005 update_stmt (stmt);
9006 return true;
9009 return false;
9012 /* Simplify a min or max if the ranges of the two operands are
9013 disjoint. Return true if we do simplify. */
9015 static bool
9016 simplify_min_or_max_using_ranges (gimple *stmt)
9018 tree op0 = gimple_assign_rhs1 (stmt);
9019 tree op1 = gimple_assign_rhs2 (stmt);
9020 bool sop = false;
9021 tree val;
9023 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9024 (LE_EXPR, op0, op1, &sop));
9025 if (!val)
9027 sop = false;
9028 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9029 (LT_EXPR, op0, op1, &sop));
9032 if (val)
9034 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9036 location_t location;
9038 if (!gimple_has_location (stmt))
9039 location = input_location;
9040 else
9041 location = gimple_location (stmt);
9042 warning_at (location, OPT_Wstrict_overflow,
9043 "assuming signed overflow does not occur when "
9044 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9047 /* VAL == TRUE -> OP0 < or <= op1
9048 VAL == FALSE -> OP0 > or >= op1. */
9049 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9050 == integer_zerop (val)) ? op0 : op1;
9051 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9052 gimple_assign_set_rhs_from_tree (&gsi, res);
9053 update_stmt (stmt);
9054 return true;
9057 return false;
9060 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9061 ABS_EXPR. If the operand is <= 0, then simplify the
9062 ABS_EXPR into a NEGATE_EXPR. */
9064 static bool
9065 simplify_abs_using_ranges (gimple *stmt)
9067 tree op = gimple_assign_rhs1 (stmt);
9068 value_range *vr = get_value_range (op);
9070 if (vr)
9072 tree val = NULL;
9073 bool sop = false;
9075 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9076 if (!val)
9078 /* The range is neither <= 0 nor > 0. Now see if it is
9079 either < 0 or >= 0. */
9080 sop = false;
9081 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9082 &sop);
9085 if (val)
9087 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9089 location_t location;
9091 if (!gimple_has_location (stmt))
9092 location = input_location;
9093 else
9094 location = gimple_location (stmt);
9095 warning_at (location, OPT_Wstrict_overflow,
9096 "assuming signed overflow does not occur when "
9097 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9100 gimple_assign_set_rhs1 (stmt, op);
9101 if (integer_zerop (val))
9102 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9103 else
9104 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9105 update_stmt (stmt);
9106 return true;
9110 return false;
9113 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9114 If all the bits that are being cleared by & are already
9115 known to be zero from VR, or all the bits that are being
9116 set by | are already known to be one from VR, the bit
9117 operation is redundant. */
9119 static bool
9120 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9122 tree op0 = gimple_assign_rhs1 (stmt);
9123 tree op1 = gimple_assign_rhs2 (stmt);
9124 tree op = NULL_TREE;
9125 value_range vr0 = VR_INITIALIZER;
9126 value_range vr1 = VR_INITIALIZER;
9127 wide_int may_be_nonzero0, may_be_nonzero1;
9128 wide_int must_be_nonzero0, must_be_nonzero1;
9129 wide_int mask;
9131 if (TREE_CODE (op0) == SSA_NAME)
9132 vr0 = *(get_value_range (op0));
9133 else if (is_gimple_min_invariant (op0))
9134 set_value_range_to_value (&vr0, op0, NULL);
9135 else
9136 return false;
9138 if (TREE_CODE (op1) == SSA_NAME)
9139 vr1 = *(get_value_range (op1));
9140 else if (is_gimple_min_invariant (op1))
9141 set_value_range_to_value (&vr1, op1, NULL);
9142 else
9143 return false;
9145 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9146 &must_be_nonzero0))
9147 return false;
9148 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9149 &must_be_nonzero1))
9150 return false;
9152 switch (gimple_assign_rhs_code (stmt))
9154 case BIT_AND_EXPR:
9155 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9156 if (mask == 0)
9158 op = op0;
9159 break;
9161 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9162 if (mask == 0)
9164 op = op1;
9165 break;
9167 break;
9168 case BIT_IOR_EXPR:
9169 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9170 if (mask == 0)
9172 op = op1;
9173 break;
9175 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9176 if (mask == 0)
9178 op = op0;
9179 break;
9181 break;
9182 default:
9183 gcc_unreachable ();
9186 if (op == NULL_TREE)
9187 return false;
9189 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9190 update_stmt (gsi_stmt (*gsi));
9191 return true;
9194 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9195 a known value range VR.
9197 If there is one and only one value which will satisfy the
9198 conditional, then return that value. Else return NULL.
9200 If signed overflow must be undefined for the value to satisfy
9201 the conditional, then set *STRICT_OVERFLOW_P to true. */
9203 static tree
9204 test_for_singularity (enum tree_code cond_code, tree op0,
9205 tree op1, value_range *vr,
9206 bool *strict_overflow_p)
9208 tree min = NULL;
9209 tree max = NULL;
9211 /* Extract minimum/maximum values which satisfy the conditional as it was
9212 written. */
9213 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9215 /* This should not be negative infinity; there is no overflow
9216 here. */
9217 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9219 max = op1;
9220 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9222 tree one = build_int_cst (TREE_TYPE (op0), 1);
9223 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9224 if (EXPR_P (max))
9225 TREE_NO_WARNING (max) = 1;
9228 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9230 /* This should not be positive infinity; there is no overflow
9231 here. */
9232 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9234 min = op1;
9235 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9237 tree one = build_int_cst (TREE_TYPE (op0), 1);
9238 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9239 if (EXPR_P (min))
9240 TREE_NO_WARNING (min) = 1;
9244 /* Now refine the minimum and maximum values using any
9245 value range information we have for op0. */
9246 if (min && max)
9248 if (compare_values (vr->min, min) == 1)
9249 min = vr->min;
9250 if (compare_values (vr->max, max) == -1)
9251 max = vr->max;
9253 /* If the new min/max values have converged to a single value,
9254 then there is only one value which can satisfy the condition,
9255 return that value. */
9256 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9258 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9259 && is_overflow_infinity (vr->max))
9260 *strict_overflow_p = true;
9261 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9262 && is_overflow_infinity (vr->min))
9263 *strict_overflow_p = true;
9265 return min;
9268 return NULL;
9271 /* Return whether the value range *VR fits in an integer type specified
9272 by PRECISION and UNSIGNED_P. */
9274 static bool
9275 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9277 tree src_type;
9278 unsigned src_precision;
9279 widest_int tem;
9280 signop src_sgn;
9282 /* We can only handle integral and pointer types. */
9283 src_type = TREE_TYPE (vr->min);
9284 if (!INTEGRAL_TYPE_P (src_type)
9285 && !POINTER_TYPE_P (src_type))
9286 return false;
9288 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9289 and so is an identity transform. */
9290 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9291 src_sgn = TYPE_SIGN (src_type);
9292 if ((src_precision < dest_precision
9293 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9294 || (src_precision == dest_precision && src_sgn == dest_sgn))
9295 return true;
9297 /* Now we can only handle ranges with constant bounds. */
9298 if (vr->type != VR_RANGE
9299 || TREE_CODE (vr->min) != INTEGER_CST
9300 || TREE_CODE (vr->max) != INTEGER_CST)
9301 return false;
9303 /* For sign changes, the MSB of the wide_int has to be clear.
9304 An unsigned value with its MSB set cannot be represented by
9305 a signed wide_int, while a negative value cannot be represented
9306 by an unsigned wide_int. */
9307 if (src_sgn != dest_sgn
9308 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9309 return false;
9311 /* Then we can perform the conversion on both ends and compare
9312 the result for equality. */
9313 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9314 if (tem != wi::to_widest (vr->min))
9315 return false;
9316 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9317 if (tem != wi::to_widest (vr->max))
9318 return false;
9320 return true;
9323 /* Simplify a conditional using a relational operator to an equality
9324 test if the range information indicates only one value can satisfy
9325 the original conditional. */
9327 static bool
9328 simplify_cond_using_ranges (gcond *stmt)
9330 tree op0 = gimple_cond_lhs (stmt);
9331 tree op1 = gimple_cond_rhs (stmt);
9332 enum tree_code cond_code = gimple_cond_code (stmt);
9334 if (cond_code != NE_EXPR
9335 && cond_code != EQ_EXPR
9336 && TREE_CODE (op0) == SSA_NAME
9337 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9338 && is_gimple_min_invariant (op1))
9340 value_range *vr = get_value_range (op0);
9342 /* If we have range information for OP0, then we might be
9343 able to simplify this conditional. */
9344 if (vr->type == VR_RANGE)
9346 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9347 bool sop = false;
9348 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9350 if (new_tree
9351 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9353 if (dump_file)
9355 fprintf (dump_file, "Simplified relational ");
9356 print_gimple_stmt (dump_file, stmt, 0, 0);
9357 fprintf (dump_file, " into ");
9360 gimple_cond_set_code (stmt, EQ_EXPR);
9361 gimple_cond_set_lhs (stmt, op0);
9362 gimple_cond_set_rhs (stmt, new_tree);
9364 update_stmt (stmt);
9366 if (dump_file)
9368 print_gimple_stmt (dump_file, stmt, 0, 0);
9369 fprintf (dump_file, "\n");
9372 if (sop && issue_strict_overflow_warning (wc))
9374 location_t location = input_location;
9375 if (gimple_has_location (stmt))
9376 location = gimple_location (stmt);
9378 warning_at (location, OPT_Wstrict_overflow,
9379 "assuming signed overflow does not occur when "
9380 "simplifying conditional");
9383 return true;
9386 /* Try again after inverting the condition. We only deal
9387 with integral types here, so no need to worry about
9388 issues with inverting FP comparisons. */
9389 sop = false;
9390 new_tree = test_for_singularity
9391 (invert_tree_comparison (cond_code, false),
9392 op0, op1, vr, &sop);
9394 if (new_tree
9395 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9397 if (dump_file)
9399 fprintf (dump_file, "Simplified relational ");
9400 print_gimple_stmt (dump_file, stmt, 0, 0);
9401 fprintf (dump_file, " into ");
9404 gimple_cond_set_code (stmt, NE_EXPR);
9405 gimple_cond_set_lhs (stmt, op0);
9406 gimple_cond_set_rhs (stmt, new_tree);
9408 update_stmt (stmt);
9410 if (dump_file)
9412 print_gimple_stmt (dump_file, stmt, 0, 0);
9413 fprintf (dump_file, "\n");
9416 if (sop && issue_strict_overflow_warning (wc))
9418 location_t location = input_location;
9419 if (gimple_has_location (stmt))
9420 location = gimple_location (stmt);
9422 warning_at (location, OPT_Wstrict_overflow,
9423 "assuming signed overflow does not occur when "
9424 "simplifying conditional");
9427 return true;
9432 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9433 see if OP0 was set by a type conversion where the source of
9434 the conversion is another SSA_NAME with a range that fits
9435 into the range of OP0's type.
9437 If so, the conversion is redundant as the earlier SSA_NAME can be
9438 used for the comparison directly if we just massage the constant in the
9439 comparison. */
9440 if (TREE_CODE (op0) == SSA_NAME
9441 && TREE_CODE (op1) == INTEGER_CST)
9443 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9444 tree innerop;
9446 if (!is_gimple_assign (def_stmt)
9447 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9448 return false;
9450 innerop = gimple_assign_rhs1 (def_stmt);
9452 if (TREE_CODE (innerop) == SSA_NAME
9453 && !POINTER_TYPE_P (TREE_TYPE (innerop))
9454 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
9455 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
9457 value_range *vr = get_value_range (innerop);
9459 if (range_int_cst_p (vr)
9460 && range_fits_type_p (vr,
9461 TYPE_PRECISION (TREE_TYPE (op0)),
9462 TYPE_SIGN (TREE_TYPE (op0)))
9463 && int_fits_type_p (op1, TREE_TYPE (innerop))
9464 /* The range must not have overflowed, or if it did overflow
9465 we must not be wrapping/trapping overflow and optimizing
9466 with strict overflow semantics. */
9467 && ((!is_negative_overflow_infinity (vr->min)
9468 && !is_positive_overflow_infinity (vr->max))
9469 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9471 /* If the range overflowed and the user has asked for warnings
9472 when strict overflow semantics were used to optimize code,
9473 issue an appropriate warning. */
9474 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9475 && (is_negative_overflow_infinity (vr->min)
9476 || is_positive_overflow_infinity (vr->max))
9477 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9479 location_t location;
9481 if (!gimple_has_location (stmt))
9482 location = input_location;
9483 else
9484 location = gimple_location (stmt);
9485 warning_at (location, OPT_Wstrict_overflow,
9486 "assuming signed overflow does not occur when "
9487 "simplifying conditional");
9490 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9491 gimple_cond_set_lhs (stmt, innerop);
9492 gimple_cond_set_rhs (stmt, newconst);
9493 return true;
9498 return false;
9501 /* Simplify a switch statement using the value range of the switch
9502 argument. */
9504 static bool
9505 simplify_switch_using_ranges (gswitch *stmt)
9507 tree op = gimple_switch_index (stmt);
9508 value_range *vr;
9509 bool take_default;
9510 edge e;
9511 edge_iterator ei;
9512 size_t i = 0, j = 0, n, n2;
9513 tree vec2;
9514 switch_update su;
9515 size_t k = 1, l = 0;
9517 if (TREE_CODE (op) == SSA_NAME)
9519 vr = get_value_range (op);
9521 /* We can only handle integer ranges. */
9522 if ((vr->type != VR_RANGE
9523 && vr->type != VR_ANTI_RANGE)
9524 || symbolic_range_p (vr))
9525 return false;
9527 /* Find case label for min/max of the value range. */
9528 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9530 else if (TREE_CODE (op) == INTEGER_CST)
9532 take_default = !find_case_label_index (stmt, 1, op, &i);
9533 if (take_default)
9535 i = 1;
9536 j = 0;
9538 else
9540 j = i;
9543 else
9544 return false;
9546 n = gimple_switch_num_labels (stmt);
9548 /* Bail out if this is just all edges taken. */
9549 if (i == 1
9550 && j == n - 1
9551 && take_default)
9552 return false;
9554 /* Build a new vector of taken case labels. */
9555 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9556 n2 = 0;
9558 /* Add the default edge, if necessary. */
9559 if (take_default)
9560 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9562 for (; i <= j; ++i, ++n2)
9563 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9565 for (; k <= l; ++k, ++n2)
9566 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9568 /* Mark needed edges. */
9569 for (i = 0; i < n2; ++i)
9571 e = find_edge (gimple_bb (stmt),
9572 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9573 e->aux = (void *)-1;
9576 /* Queue not needed edges for later removal. */
9577 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9579 if (e->aux == (void *)-1)
9581 e->aux = NULL;
9582 continue;
9585 if (dump_file && (dump_flags & TDF_DETAILS))
9587 fprintf (dump_file, "removing unreachable case label\n");
9589 to_remove_edges.safe_push (e);
9590 e->flags &= ~EDGE_EXECUTABLE;
9593 /* And queue an update for the stmt. */
9594 su.stmt = stmt;
9595 su.vec = vec2;
9596 to_update_switch_stmts.safe_push (su);
9597 return false;
9600 /* Simplify an integral conversion from an SSA name in STMT. */
9602 static bool
9603 simplify_conversion_using_ranges (gimple *stmt)
9605 tree innerop, middleop, finaltype;
9606 gimple *def_stmt;
9607 signop inner_sgn, middle_sgn, final_sgn;
9608 unsigned inner_prec, middle_prec, final_prec;
9609 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9611 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9612 if (!INTEGRAL_TYPE_P (finaltype))
9613 return false;
9614 middleop = gimple_assign_rhs1 (stmt);
9615 def_stmt = SSA_NAME_DEF_STMT (middleop);
9616 if (!is_gimple_assign (def_stmt)
9617 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9618 return false;
9619 innerop = gimple_assign_rhs1 (def_stmt);
9620 if (TREE_CODE (innerop) != SSA_NAME
9621 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9622 return false;
9624 /* Get the value-range of the inner operand. Use get_range_info in
9625 case innerop was created during substitute-and-fold. */
9626 wide_int imin, imax;
9627 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
9628 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
9629 return false;
9630 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
9631 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
9633 /* Simulate the conversion chain to check if the result is equal if
9634 the middle conversion is removed. */
9635 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9636 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9637 final_prec = TYPE_PRECISION (finaltype);
9639 /* If the first conversion is not injective, the second must not
9640 be widening. */
9641 if (wi::gtu_p (innermax - innermin,
9642 wi::mask <widest_int> (middle_prec, false))
9643 && middle_prec < final_prec)
9644 return false;
9645 /* We also want a medium value so that we can track the effect that
9646 narrowing conversions with sign change have. */
9647 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9648 if (inner_sgn == UNSIGNED)
9649 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9650 else
9651 innermed = 0;
9652 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9653 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9654 innermed = innermin;
9656 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9657 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9658 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9659 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9661 /* Require that the final conversion applied to both the original
9662 and the intermediate range produces the same result. */
9663 final_sgn = TYPE_SIGN (finaltype);
9664 if (wi::ext (middlemin, final_prec, final_sgn)
9665 != wi::ext (innermin, final_prec, final_sgn)
9666 || wi::ext (middlemed, final_prec, final_sgn)
9667 != wi::ext (innermed, final_prec, final_sgn)
9668 || wi::ext (middlemax, final_prec, final_sgn)
9669 != wi::ext (innermax, final_prec, final_sgn))
9670 return false;
9672 gimple_assign_set_rhs1 (stmt, innerop);
9673 update_stmt (stmt);
9674 return true;
9677 /* Simplify a conversion from integral SSA name to float in STMT. */
9679 static bool
9680 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
9681 gimple *stmt)
9683 tree rhs1 = gimple_assign_rhs1 (stmt);
9684 value_range *vr = get_value_range (rhs1);
9685 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9686 machine_mode mode;
9687 tree tem;
9688 gassign *conv;
9690 /* We can only handle constant ranges. */
9691 if (vr->type != VR_RANGE
9692 || TREE_CODE (vr->min) != INTEGER_CST
9693 || TREE_CODE (vr->max) != INTEGER_CST)
9694 return false;
9696 /* First check if we can use a signed type in place of an unsigned. */
9697 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9698 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9699 != CODE_FOR_nothing)
9700 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9701 mode = TYPE_MODE (TREE_TYPE (rhs1));
9702 /* If we can do the conversion in the current input mode do nothing. */
9703 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9704 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9705 return false;
9706 /* Otherwise search for a mode we can use, starting from the narrowest
9707 integer mode available. */
9708 else
9710 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9713 /* If we cannot do a signed conversion to float from mode
9714 or if the value-range does not fit in the signed type
9715 try with a wider mode. */
9716 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9717 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9718 break;
9720 mode = GET_MODE_WIDER_MODE (mode);
9721 /* But do not widen the input. Instead leave that to the
9722 optabs expansion code. */
9723 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9724 return false;
9726 while (mode != VOIDmode);
9727 if (mode == VOIDmode)
9728 return false;
9731 /* It works, insert a truncation or sign-change before the
9732 float conversion. */
9733 tem = make_ssa_name (build_nonstandard_integer_type
9734 (GET_MODE_PRECISION (mode), 0));
9735 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9736 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9737 gimple_assign_set_rhs1 (stmt, tem);
9738 update_stmt (stmt);
9740 return true;
9743 /* Simplify an internal fn call using ranges if possible. */
9745 static bool
9746 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9748 enum tree_code subcode;
9749 bool is_ubsan = false;
9750 bool ovf = false;
9751 switch (gimple_call_internal_fn (stmt))
9753 case IFN_UBSAN_CHECK_ADD:
9754 subcode = PLUS_EXPR;
9755 is_ubsan = true;
9756 break;
9757 case IFN_UBSAN_CHECK_SUB:
9758 subcode = MINUS_EXPR;
9759 is_ubsan = true;
9760 break;
9761 case IFN_UBSAN_CHECK_MUL:
9762 subcode = MULT_EXPR;
9763 is_ubsan = true;
9764 break;
9765 case IFN_ADD_OVERFLOW:
9766 subcode = PLUS_EXPR;
9767 break;
9768 case IFN_SUB_OVERFLOW:
9769 subcode = MINUS_EXPR;
9770 break;
9771 case IFN_MUL_OVERFLOW:
9772 subcode = MULT_EXPR;
9773 break;
9774 default:
9775 return false;
9778 tree op0 = gimple_call_arg (stmt, 0);
9779 tree op1 = gimple_call_arg (stmt, 1);
9780 tree type;
9781 if (is_ubsan)
9782 type = TREE_TYPE (op0);
9783 else if (gimple_call_lhs (stmt) == NULL_TREE)
9784 return false;
9785 else
9786 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9787 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9788 || (is_ubsan && ovf))
9789 return false;
9791 gimple *g;
9792 location_t loc = gimple_location (stmt);
9793 if (is_ubsan)
9794 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9795 else
9797 int prec = TYPE_PRECISION (type);
9798 tree utype = type;
9799 if (ovf
9800 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9801 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9802 utype = build_nonstandard_integer_type (prec, 1);
9803 if (TREE_CODE (op0) == INTEGER_CST)
9804 op0 = fold_convert (utype, op0);
9805 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9807 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9808 gimple_set_location (g, loc);
9809 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9810 op0 = gimple_assign_lhs (g);
9812 if (TREE_CODE (op1) == INTEGER_CST)
9813 op1 = fold_convert (utype, op1);
9814 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9816 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9817 gimple_set_location (g, loc);
9818 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9819 op1 = gimple_assign_lhs (g);
9821 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9822 gimple_set_location (g, loc);
9823 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9824 if (utype != type)
9826 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9827 gimple_assign_lhs (g));
9828 gimple_set_location (g, loc);
9829 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9831 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9832 gimple_assign_lhs (g),
9833 build_int_cst (type, ovf));
9835 gimple_set_location (g, loc);
9836 gsi_replace (gsi, g, false);
9837 return true;
9840 /* Simplify STMT using ranges if possible. */
9842 static bool
9843 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9845 gimple *stmt = gsi_stmt (*gsi);
9846 if (is_gimple_assign (stmt))
9848 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9849 tree rhs1 = gimple_assign_rhs1 (stmt);
9851 switch (rhs_code)
9853 case EQ_EXPR:
9854 case NE_EXPR:
9855 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9856 if the RHS is zero or one, and the LHS are known to be boolean
9857 values. */
9858 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9859 return simplify_truth_ops_using_ranges (gsi, stmt);
9860 break;
9862 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9863 and BIT_AND_EXPR respectively if the first operand is greater
9864 than zero and the second operand is an exact power of two.
9865 Also optimize TRUNC_MOD_EXPR away if the second operand is
9866 constant and the first operand already has the right value
9867 range. */
9868 case TRUNC_DIV_EXPR:
9869 case TRUNC_MOD_EXPR:
9870 if (TREE_CODE (rhs1) == SSA_NAME
9871 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9872 return simplify_div_or_mod_using_ranges (gsi, stmt);
9873 break;
9875 /* Transform ABS (X) into X or -X as appropriate. */
9876 case ABS_EXPR:
9877 if (TREE_CODE (rhs1) == SSA_NAME
9878 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9879 return simplify_abs_using_ranges (stmt);
9880 break;
9882 case BIT_AND_EXPR:
9883 case BIT_IOR_EXPR:
9884 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9885 if all the bits being cleared are already cleared or
9886 all the bits being set are already set. */
9887 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9888 return simplify_bit_ops_using_ranges (gsi, stmt);
9889 break;
9891 CASE_CONVERT:
9892 if (TREE_CODE (rhs1) == SSA_NAME
9893 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9894 return simplify_conversion_using_ranges (stmt);
9895 break;
9897 case FLOAT_EXPR:
9898 if (TREE_CODE (rhs1) == SSA_NAME
9899 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9900 return simplify_float_conversion_using_ranges (gsi, stmt);
9901 break;
9903 case MIN_EXPR:
9904 case MAX_EXPR:
9905 return simplify_min_or_max_using_ranges (stmt);
9906 break;
9908 default:
9909 break;
9912 else if (gimple_code (stmt) == GIMPLE_COND)
9913 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9914 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9915 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
9916 else if (is_gimple_call (stmt)
9917 && gimple_call_internal_p (stmt))
9918 return simplify_internal_call_using_ranges (gsi, stmt);
9920 return false;
9923 /* If the statement pointed by SI has a predicate whose value can be
9924 computed using the value range information computed by VRP, compute
9925 its value and return true. Otherwise, return false. */
9927 static bool
9928 fold_predicate_in (gimple_stmt_iterator *si)
9930 bool assignment_p = false;
9931 tree val;
9932 gimple *stmt = gsi_stmt (*si);
9934 if (is_gimple_assign (stmt)
9935 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9937 assignment_p = true;
9938 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9939 gimple_assign_rhs1 (stmt),
9940 gimple_assign_rhs2 (stmt),
9941 stmt);
9943 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
9944 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
9945 gimple_cond_lhs (cond_stmt),
9946 gimple_cond_rhs (cond_stmt),
9947 stmt);
9948 else
9949 return false;
9951 if (val)
9953 if (assignment_p)
9954 val = fold_convert (gimple_expr_type (stmt), val);
9956 if (dump_file)
9958 fprintf (dump_file, "Folding predicate ");
9959 print_gimple_expr (dump_file, stmt, 0, 0);
9960 fprintf (dump_file, " to ");
9961 print_generic_expr (dump_file, val, 0);
9962 fprintf (dump_file, "\n");
9965 if (is_gimple_assign (stmt))
9966 gimple_assign_set_rhs_from_tree (si, val);
9967 else
9969 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9970 gcond *cond_stmt = as_a <gcond *> (stmt);
9971 if (integer_zerop (val))
9972 gimple_cond_make_false (cond_stmt);
9973 else if (integer_onep (val))
9974 gimple_cond_make_true (cond_stmt);
9975 else
9976 gcc_unreachable ();
9979 return true;
9982 return false;
9985 /* Callback for substitute_and_fold folding the stmt at *SI. */
9987 static bool
9988 vrp_fold_stmt (gimple_stmt_iterator *si)
9990 if (fold_predicate_in (si))
9991 return true;
9993 return simplify_stmt_using_ranges (si);
9996 /* Unwindable const/copy equivalences. */
9997 const_and_copies *equiv_stack;
9999 /* A trivial wrapper so that we can present the generic jump threading
10000 code with a simple API for simplifying statements. STMT is the
10001 statement we want to simplify, WITHIN_STMT provides the location
10002 for any overflow warnings. */
10004 static tree
10005 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10006 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED)
10008 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10009 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10010 gimple_cond_lhs (cond_stmt),
10011 gimple_cond_rhs (cond_stmt),
10012 within_stmt);
10014 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10016 value_range new_vr = VR_INITIALIZER;
10017 tree lhs = gimple_assign_lhs (assign_stmt);
10019 if (TREE_CODE (lhs) == SSA_NAME
10020 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10021 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10023 extract_range_from_assignment (&new_vr, assign_stmt);
10024 if (range_int_cst_singleton_p (&new_vr))
10025 return new_vr.min;
10029 return NULL_TREE;
10032 /* Blocks which have more than one predecessor and more than
10033 one successor present jump threading opportunities, i.e.,
10034 when the block is reached from a specific predecessor, we
10035 may be able to determine which of the outgoing edges will
10036 be traversed. When this optimization applies, we are able
10037 to avoid conditionals at runtime and we may expose secondary
10038 optimization opportunities.
10040 This routine is effectively a driver for the generic jump
10041 threading code. It basically just presents the generic code
10042 with edges that may be suitable for jump threading.
10044 Unlike DOM, we do not iterate VRP if jump threading was successful.
10045 While iterating may expose new opportunities for VRP, it is expected
10046 those opportunities would be very limited and the compile time cost
10047 to expose those opportunities would be significant.
10049 As jump threading opportunities are discovered, they are registered
10050 for later realization. */
10052 static void
10053 identify_jump_threads (void)
10055 basic_block bb;
10056 gcond *dummy;
10057 int i;
10058 edge e;
10060 /* Ugh. When substituting values earlier in this pass we can
10061 wipe the dominance information. So rebuild the dominator
10062 information as we need it within the jump threading code. */
10063 calculate_dominance_info (CDI_DOMINATORS);
10065 /* We do not allow VRP information to be used for jump threading
10066 across a back edge in the CFG. Otherwise it becomes too
10067 difficult to avoid eliminating loop exit tests. Of course
10068 EDGE_DFS_BACK is not accurate at this time so we have to
10069 recompute it. */
10070 mark_dfs_back_edges ();
10072 /* Do not thread across edges we are about to remove. Just marking
10073 them as EDGE_IGNORE will do. */
10074 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10075 e->flags |= EDGE_IGNORE;
10077 /* Allocate our unwinder stack to unwind any temporary equivalences
10078 that might be recorded. */
10079 equiv_stack = new const_and_copies ();
10081 /* To avoid lots of silly node creation, we create a single
10082 conditional and just modify it in-place when attempting to
10083 thread jumps. */
10084 dummy = gimple_build_cond (EQ_EXPR,
10085 integer_zero_node, integer_zero_node,
10086 NULL, NULL);
10088 /* Walk through all the blocks finding those which present a
10089 potential jump threading opportunity. We could set this up
10090 as a dominator walker and record data during the walk, but
10091 I doubt it's worth the effort for the classes of jump
10092 threading opportunities we are trying to identify at this
10093 point in compilation. */
10094 FOR_EACH_BB_FN (bb, cfun)
10096 gimple *last;
10098 /* If the generic jump threading code does not find this block
10099 interesting, then there is nothing to do. */
10100 if (! potentially_threadable_block (bb))
10101 continue;
10103 last = last_stmt (bb);
10105 /* We're basically looking for a switch or any kind of conditional with
10106 integral or pointer type arguments. Note the type of the second
10107 argument will be the same as the first argument, so no need to
10108 check it explicitly.
10110 We also handle the case where there are no statements in the
10111 block. This come up with forwarder blocks that are not
10112 optimized away because they lead to a loop header. But we do
10113 want to thread through them as we can sometimes thread to the
10114 loop exit which is obviously profitable. */
10115 if (!last
10116 || gimple_code (last) == GIMPLE_SWITCH
10117 || (gimple_code (last) == GIMPLE_COND
10118 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10119 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10120 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10121 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10122 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10124 edge_iterator ei;
10126 /* We've got a block with multiple predecessors and multiple
10127 successors which also ends in a suitable conditional or
10128 switch statement. For each predecessor, see if we can thread
10129 it to a specific successor. */
10130 FOR_EACH_EDGE (e, ei, bb->preds)
10132 /* Do not thread across edges marked to ignoreor abnormal
10133 edges in the CFG. */
10134 if (e->flags & (EDGE_IGNORE | EDGE_COMPLEX))
10135 continue;
10137 thread_across_edge (dummy, e, true, equiv_stack, NULL,
10138 simplify_stmt_for_jump_threading);
10143 /* Clear EDGE_IGNORE. */
10144 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10145 e->flags &= ~EDGE_IGNORE;
10147 /* We do not actually update the CFG or SSA graphs at this point as
10148 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10149 handle ASSERT_EXPRs gracefully. */
10152 /* We identified all the jump threading opportunities earlier, but could
10153 not transform the CFG at that time. This routine transforms the
10154 CFG and arranges for the dominator tree to be rebuilt if necessary.
10156 Note the SSA graph update will occur during the normal TODO
10157 processing by the pass manager. */
10158 static void
10159 finalize_jump_threads (void)
10161 thread_through_all_blocks (false);
10162 delete equiv_stack;
10166 /* Traverse all the blocks folding conditionals with known ranges. */
10168 static void
10169 vrp_finalize (bool warn_array_bounds_p)
10171 size_t i;
10173 values_propagated = true;
10175 if (dump_file)
10177 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10178 dump_all_value_ranges (dump_file);
10179 fprintf (dump_file, "\n");
10182 /* Set value range to non pointer SSA_NAMEs. */
10183 for (i = 0; i < num_vr_values; i++)
10184 if (vr_value[i])
10186 tree name = ssa_name (i);
10188 if (!name
10189 || POINTER_TYPE_P (TREE_TYPE (name))
10190 || (vr_value[i]->type == VR_VARYING)
10191 || (vr_value[i]->type == VR_UNDEFINED))
10192 continue;
10194 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10195 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10196 && (vr_value[i]->type == VR_RANGE
10197 || vr_value[i]->type == VR_ANTI_RANGE))
10198 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10199 vr_value[i]->max);
10202 substitute_and_fold (op_with_constant_singleton_value_range,
10203 vrp_fold_stmt, false);
10205 if (warn_array_bounds && warn_array_bounds_p)
10206 check_all_array_refs ();
10208 /* We must identify jump threading opportunities before we release
10209 the datastructures built by VRP. */
10210 identify_jump_threads ();
10212 /* Free allocated memory. */
10213 for (i = 0; i < num_vr_values; i++)
10214 if (vr_value[i])
10216 BITMAP_FREE (vr_value[i]->equiv);
10217 free (vr_value[i]);
10220 free (vr_value);
10221 free (vr_phi_edge_counts);
10223 /* So that we can distinguish between VRP data being available
10224 and not available. */
10225 vr_value = NULL;
10226 vr_phi_edge_counts = NULL;
10230 /* Main entry point to VRP (Value Range Propagation). This pass is
10231 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10232 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10233 Programming Language Design and Implementation, pp. 67-78, 1995.
10234 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10236 This is essentially an SSA-CCP pass modified to deal with ranges
10237 instead of constants.
10239 While propagating ranges, we may find that two or more SSA name
10240 have equivalent, though distinct ranges. For instance,
10242 1 x_9 = p_3->a;
10243 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10244 3 if (p_4 == q_2)
10245 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10246 5 endif
10247 6 if (q_2)
10249 In the code above, pointer p_5 has range [q_2, q_2], but from the
10250 code we can also determine that p_5 cannot be NULL and, if q_2 had
10251 a non-varying range, p_5's range should also be compatible with it.
10253 These equivalences are created by two expressions: ASSERT_EXPR and
10254 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10255 result of another assertion, then we can use the fact that p_5 and
10256 p_4 are equivalent when evaluating p_5's range.
10258 Together with value ranges, we also propagate these equivalences
10259 between names so that we can take advantage of information from
10260 multiple ranges when doing final replacement. Note that this
10261 equivalency relation is transitive but not symmetric.
10263 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10264 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10265 in contexts where that assertion does not hold (e.g., in line 6).
10267 TODO, the main difference between this pass and Patterson's is that
10268 we do not propagate edge probabilities. We only compute whether
10269 edges can be taken or not. That is, instead of having a spectrum
10270 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10271 DON'T KNOW. In the future, it may be worthwhile to propagate
10272 probabilities to aid branch prediction. */
10274 static unsigned int
10275 execute_vrp (bool warn_array_bounds_p)
10277 int i;
10278 edge e;
10279 switch_update *su;
10281 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10282 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10283 scev_initialize ();
10285 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10286 Inserting assertions may split edges which will invalidate
10287 EDGE_DFS_BACK. */
10288 insert_range_assertions ();
10290 to_remove_edges.create (10);
10291 to_update_switch_stmts.create (5);
10292 threadedge_initialize_values ();
10294 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10295 mark_dfs_back_edges ();
10297 vrp_initialize ();
10298 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10299 vrp_finalize (warn_array_bounds_p);
10301 free_numbers_of_iterations_estimates (cfun);
10303 /* ASSERT_EXPRs must be removed before finalizing jump threads
10304 as finalizing jump threads calls the CFG cleanup code which
10305 does not properly handle ASSERT_EXPRs. */
10306 remove_range_assertions ();
10308 /* If we exposed any new variables, go ahead and put them into
10309 SSA form now, before we handle jump threading. This simplifies
10310 interactions between rewriting of _DECL nodes into SSA form
10311 and rewriting SSA_NAME nodes into SSA form after block
10312 duplication and CFG manipulation. */
10313 update_ssa (TODO_update_ssa);
10315 finalize_jump_threads ();
10317 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10318 CFG in a broken state and requires a cfg_cleanup run. */
10319 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10320 remove_edge (e);
10321 /* Update SWITCH_EXPR case label vector. */
10322 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10324 size_t j;
10325 size_t n = TREE_VEC_LENGTH (su->vec);
10326 tree label;
10327 gimple_switch_set_num_labels (su->stmt, n);
10328 for (j = 0; j < n; j++)
10329 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10330 /* As we may have replaced the default label with a regular one
10331 make sure to make it a real default label again. This ensures
10332 optimal expansion. */
10333 label = gimple_switch_label (su->stmt, 0);
10334 CASE_LOW (label) = NULL_TREE;
10335 CASE_HIGH (label) = NULL_TREE;
10338 if (to_remove_edges.length () > 0)
10340 free_dominance_info (CDI_DOMINATORS);
10341 loops_state_set (LOOPS_NEED_FIXUP);
10344 to_remove_edges.release ();
10345 to_update_switch_stmts.release ();
10346 threadedge_finalize_values ();
10348 scev_finalize ();
10349 loop_optimizer_finalize ();
10350 return 0;
10353 namespace {
10355 const pass_data pass_data_vrp =
10357 GIMPLE_PASS, /* type */
10358 "vrp", /* name */
10359 OPTGROUP_NONE, /* optinfo_flags */
10360 TV_TREE_VRP, /* tv_id */
10361 PROP_ssa, /* properties_required */
10362 0, /* properties_provided */
10363 0, /* properties_destroyed */
10364 0, /* todo_flags_start */
10365 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10368 class pass_vrp : public gimple_opt_pass
10370 public:
10371 pass_vrp (gcc::context *ctxt)
10372 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
10375 /* opt_pass methods: */
10376 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10377 void set_pass_param (unsigned int n, bool param)
10379 gcc_assert (n == 0);
10380 warn_array_bounds_p = param;
10382 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10383 virtual unsigned int execute (function *)
10384 { return execute_vrp (warn_array_bounds_p); }
10386 private:
10387 bool warn_array_bounds_p;
10388 }; // class pass_vrp
10390 } // anon namespace
10392 gimple_opt_pass *
10393 make_pass_vrp (gcc::context *ctxt)
10395 return new pass_vrp (ctxt);