Update ChangeLog and version files for release
[official-gcc.git] / gcc / tree-vrp.c
blob154df21a3e65df04b57d5fe1e92d074759fca411
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 /* Set value-ranges of all SSA names defined by STMT to varying. */
722 static void
723 set_defs_to_varying (gimple *stmt)
725 ssa_op_iter i;
726 tree def;
727 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
729 value_range *vr = get_value_range (def);
730 /* Avoid writing to vr_const_varying get_value_range may return. */
731 if (vr->type != VR_VARYING)
732 set_value_range_to_varying (vr);
737 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
739 static inline bool
740 vrp_operand_equal_p (const_tree val1, const_tree val2)
742 if (val1 == val2)
743 return true;
744 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
745 return false;
746 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
749 /* Return true, if the bitmaps B1 and B2 are equal. */
751 static inline bool
752 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
754 return (b1 == b2
755 || ((!b1 || bitmap_empty_p (b1))
756 && (!b2 || bitmap_empty_p (b2)))
757 || (b1 && b2
758 && bitmap_equal_p (b1, b2)));
761 /* Update the value range and equivalence set for variable VAR to
762 NEW_VR. Return true if NEW_VR is different from VAR's previous
763 value.
765 NOTE: This function assumes that NEW_VR is a temporary value range
766 object created for the sole purpose of updating VAR's range. The
767 storage used by the equivalence set from NEW_VR will be freed by
768 this function. Do not call update_value_range when NEW_VR
769 is the range object associated with another SSA name. */
771 static inline bool
772 update_value_range (const_tree var, value_range *new_vr)
774 value_range *old_vr;
775 bool is_new;
777 /* If there is a value-range on the SSA name from earlier analysis
778 factor that in. */
779 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
781 wide_int min, max;
782 value_range_type rtype = get_range_info (var, &min, &max);
783 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
785 value_range nr;
786 nr.type = rtype;
787 /* Range info on SSA names doesn't carry overflow information
788 so make sure to preserve the overflow bit on the lattice. */
789 if (new_vr->type == VR_RANGE
790 && is_negative_overflow_infinity (new_vr->min)
791 && wi::eq_p (new_vr->min, min))
792 nr.min = new_vr->min;
793 else
794 nr.min = wide_int_to_tree (TREE_TYPE (var), min);
795 if (new_vr->type == VR_RANGE
796 && is_positive_overflow_infinity (new_vr->max)
797 && wi::eq_p (new_vr->max, max))
798 nr.max = new_vr->max;
799 else
800 nr.max = wide_int_to_tree (TREE_TYPE (var), max);
801 nr.equiv = NULL;
802 vrp_intersect_ranges (new_vr, &nr);
806 /* Update the value range, if necessary. */
807 old_vr = get_value_range (var);
808 is_new = old_vr->type != new_vr->type
809 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
810 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
811 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
813 if (is_new)
815 /* Do not allow transitions up the lattice. The following
816 is slightly more awkward than just new_vr->type < old_vr->type
817 because VR_RANGE and VR_ANTI_RANGE need to be considered
818 the same. We may not have is_new when transitioning to
819 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
820 called. */
821 if (new_vr->type == VR_UNDEFINED)
823 BITMAP_FREE (new_vr->equiv);
824 set_value_range_to_varying (old_vr);
825 set_value_range_to_varying (new_vr);
826 return true;
828 else
829 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
830 new_vr->equiv);
833 BITMAP_FREE (new_vr->equiv);
835 return is_new;
839 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
840 point where equivalence processing can be turned on/off. */
842 static void
843 add_equivalence (bitmap *equiv, const_tree var)
845 unsigned ver = SSA_NAME_VERSION (var);
846 value_range *vr = vr_value[ver];
848 if (*equiv == NULL)
849 *equiv = BITMAP_ALLOC (NULL);
850 bitmap_set_bit (*equiv, ver);
851 if (vr && vr->equiv)
852 bitmap_ior_into (*equiv, vr->equiv);
856 /* Return true if VR is ~[0, 0]. */
858 static inline bool
859 range_is_nonnull (value_range *vr)
861 return vr->type == VR_ANTI_RANGE
862 && integer_zerop (vr->min)
863 && integer_zerop (vr->max);
867 /* Return true if VR is [0, 0]. */
869 static inline bool
870 range_is_null (value_range *vr)
872 return vr->type == VR_RANGE
873 && integer_zerop (vr->min)
874 && integer_zerop (vr->max);
877 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
878 a singleton. */
880 static inline bool
881 range_int_cst_p (value_range *vr)
883 return (vr->type == VR_RANGE
884 && TREE_CODE (vr->max) == INTEGER_CST
885 && TREE_CODE (vr->min) == INTEGER_CST);
888 /* Return true if VR is a INTEGER_CST singleton. */
890 static inline bool
891 range_int_cst_singleton_p (value_range *vr)
893 return (range_int_cst_p (vr)
894 && !is_overflow_infinity (vr->min)
895 && !is_overflow_infinity (vr->max)
896 && tree_int_cst_equal (vr->min, vr->max));
899 /* Return true if value range VR involves at least one symbol. */
901 static inline bool
902 symbolic_range_p (value_range *vr)
904 return (!is_gimple_min_invariant (vr->min)
905 || !is_gimple_min_invariant (vr->max));
908 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
909 otherwise. We only handle additive operations and set NEG to true if the
910 symbol is negated and INV to the invariant part, if any. */
912 static tree
913 get_single_symbol (tree t, bool *neg, tree *inv)
915 bool neg_;
916 tree inv_;
918 if (TREE_CODE (t) == PLUS_EXPR
919 || TREE_CODE (t) == POINTER_PLUS_EXPR
920 || TREE_CODE (t) == MINUS_EXPR)
922 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
924 neg_ = (TREE_CODE (t) == MINUS_EXPR);
925 inv_ = TREE_OPERAND (t, 0);
926 t = TREE_OPERAND (t, 1);
928 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
930 neg_ = false;
931 inv_ = TREE_OPERAND (t, 1);
932 t = TREE_OPERAND (t, 0);
934 else
935 return NULL_TREE;
937 else
939 neg_ = false;
940 inv_ = NULL_TREE;
943 if (TREE_CODE (t) == NEGATE_EXPR)
945 t = TREE_OPERAND (t, 0);
946 neg_ = !neg_;
949 if (TREE_CODE (t) != SSA_NAME)
950 return NULL_TREE;
952 *neg = neg_;
953 *inv = inv_;
954 return t;
957 /* The reverse operation: build a symbolic expression with TYPE
958 from symbol SYM, negated according to NEG, and invariant INV. */
960 static tree
961 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
963 const bool pointer_p = POINTER_TYPE_P (type);
964 tree t = sym;
966 if (neg)
967 t = build1 (NEGATE_EXPR, type, t);
969 if (integer_zerop (inv))
970 return t;
972 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
975 /* Return true if value range VR involves exactly one symbol SYM. */
977 static bool
978 symbolic_range_based_on_p (value_range *vr, const_tree sym)
980 bool neg, min_has_symbol, max_has_symbol;
981 tree inv;
983 if (is_gimple_min_invariant (vr->min))
984 min_has_symbol = false;
985 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
986 min_has_symbol = true;
987 else
988 return false;
990 if (is_gimple_min_invariant (vr->max))
991 max_has_symbol = false;
992 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
993 max_has_symbol = true;
994 else
995 return false;
997 return (min_has_symbol || max_has_symbol);
1000 /* Return true if value range VR uses an overflow infinity. */
1002 static inline bool
1003 overflow_infinity_range_p (value_range *vr)
1005 return (vr->type == VR_RANGE
1006 && (is_overflow_infinity (vr->min)
1007 || is_overflow_infinity (vr->max)));
1010 /* Return false if we can not make a valid comparison based on VR;
1011 this will be the case if it uses an overflow infinity and overflow
1012 is not undefined (i.e., -fno-strict-overflow is in effect).
1013 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1014 uses an overflow infinity. */
1016 static bool
1017 usable_range_p (value_range *vr, bool *strict_overflow_p)
1019 gcc_assert (vr->type == VR_RANGE);
1020 if (is_overflow_infinity (vr->min))
1022 *strict_overflow_p = true;
1023 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1024 return false;
1026 if (is_overflow_infinity (vr->max))
1028 *strict_overflow_p = true;
1029 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1030 return false;
1032 return true;
1035 /* Return true if the result of assignment STMT is know to be non-zero.
1036 If the return value is based on the assumption that signed overflow is
1037 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1038 *STRICT_OVERFLOW_P.*/
1040 static bool
1041 gimple_assign_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1043 enum tree_code code = gimple_assign_rhs_code (stmt);
1044 switch (get_gimple_rhs_class (code))
1046 case GIMPLE_UNARY_RHS:
1047 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1048 gimple_expr_type (stmt),
1049 gimple_assign_rhs1 (stmt),
1050 strict_overflow_p);
1051 case GIMPLE_BINARY_RHS:
1052 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1053 gimple_expr_type (stmt),
1054 gimple_assign_rhs1 (stmt),
1055 gimple_assign_rhs2 (stmt),
1056 strict_overflow_p);
1057 case GIMPLE_TERNARY_RHS:
1058 return false;
1059 case GIMPLE_SINGLE_RHS:
1060 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1061 strict_overflow_p);
1062 case GIMPLE_INVALID_RHS:
1063 gcc_unreachable ();
1064 default:
1065 gcc_unreachable ();
1069 /* Return true if STMT is known to compute a non-zero value.
1070 If the return value is based on the assumption that signed overflow is
1071 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1072 *STRICT_OVERFLOW_P.*/
1074 static bool
1075 gimple_stmt_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1077 switch (gimple_code (stmt))
1079 case GIMPLE_ASSIGN:
1080 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1081 case GIMPLE_CALL:
1083 tree fndecl = gimple_call_fndecl (stmt);
1084 if (!fndecl) return false;
1085 if (flag_delete_null_pointer_checks && !flag_check_new
1086 && DECL_IS_OPERATOR_NEW (fndecl)
1087 && !TREE_NOTHROW (fndecl))
1088 return true;
1089 /* References are always non-NULL. */
1090 if (flag_delete_null_pointer_checks
1091 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1092 return true;
1093 if (flag_delete_null_pointer_checks &&
1094 lookup_attribute ("returns_nonnull",
1095 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1096 return true;
1097 return gimple_alloca_call_p (stmt);
1099 default:
1100 gcc_unreachable ();
1104 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1105 obtained so far. */
1107 static bool
1108 vrp_stmt_computes_nonzero (gimple *stmt, bool *strict_overflow_p)
1110 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1111 return true;
1113 /* If we have an expression of the form &X->a, then the expression
1114 is nonnull if X is nonnull. */
1115 if (is_gimple_assign (stmt)
1116 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1118 tree expr = gimple_assign_rhs1 (stmt);
1119 tree base = get_base_address (TREE_OPERAND (expr, 0));
1121 if (base != NULL_TREE
1122 && TREE_CODE (base) == MEM_REF
1123 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1125 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
1126 if (range_is_nonnull (vr))
1127 return true;
1131 return false;
1134 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1135 a gimple invariant, or SSA_NAME +- CST. */
1137 static bool
1138 valid_value_p (tree expr)
1140 if (TREE_CODE (expr) == SSA_NAME)
1141 return true;
1143 if (TREE_CODE (expr) == PLUS_EXPR
1144 || TREE_CODE (expr) == MINUS_EXPR)
1145 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1146 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1148 return is_gimple_min_invariant (expr);
1151 /* Return
1152 1 if VAL < VAL2
1153 0 if !(VAL < VAL2)
1154 -2 if those are incomparable. */
1155 static inline int
1156 operand_less_p (tree val, tree val2)
1158 /* LT is folded faster than GE and others. Inline the common case. */
1159 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1161 if (! is_positive_overflow_infinity (val2))
1162 return tree_int_cst_lt (val, val2);
1164 else
1166 tree tcmp;
1168 fold_defer_overflow_warnings ();
1170 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1172 fold_undefer_and_ignore_overflow_warnings ();
1174 if (!tcmp
1175 || TREE_CODE (tcmp) != INTEGER_CST)
1176 return -2;
1178 if (!integer_zerop (tcmp))
1179 return 1;
1182 /* val >= val2, not considering overflow infinity. */
1183 if (is_negative_overflow_infinity (val))
1184 return is_negative_overflow_infinity (val2) ? 0 : 1;
1185 else if (is_positive_overflow_infinity (val2))
1186 return is_positive_overflow_infinity (val) ? 0 : 1;
1188 return 0;
1191 /* Compare two values VAL1 and VAL2. Return
1193 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1194 -1 if VAL1 < VAL2,
1195 0 if VAL1 == VAL2,
1196 +1 if VAL1 > VAL2, and
1197 +2 if VAL1 != VAL2
1199 This is similar to tree_int_cst_compare but supports pointer values
1200 and values that cannot be compared at compile time.
1202 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1203 true if the return value is only valid if we assume that signed
1204 overflow is undefined. */
1206 static int
1207 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1209 if (val1 == val2)
1210 return 0;
1212 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1213 both integers. */
1214 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1215 == POINTER_TYPE_P (TREE_TYPE (val2)));
1217 /* Convert the two values into the same type. This is needed because
1218 sizetype causes sign extension even for unsigned types. */
1219 val2 = fold_convert (TREE_TYPE (val1), val2);
1220 STRIP_USELESS_TYPE_CONVERSION (val2);
1222 if ((TREE_CODE (val1) == SSA_NAME
1223 || (TREE_CODE (val1) == NEGATE_EXPR
1224 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1225 || TREE_CODE (val1) == PLUS_EXPR
1226 || TREE_CODE (val1) == MINUS_EXPR)
1227 && (TREE_CODE (val2) == SSA_NAME
1228 || (TREE_CODE (val2) == NEGATE_EXPR
1229 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1230 || TREE_CODE (val2) == PLUS_EXPR
1231 || TREE_CODE (val2) == MINUS_EXPR))
1233 tree n1, c1, n2, c2;
1234 enum tree_code code1, code2;
1236 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1237 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1238 same name, return -2. */
1239 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1241 code1 = SSA_NAME;
1242 n1 = val1;
1243 c1 = NULL_TREE;
1245 else
1247 code1 = TREE_CODE (val1);
1248 n1 = TREE_OPERAND (val1, 0);
1249 c1 = TREE_OPERAND (val1, 1);
1250 if (tree_int_cst_sgn (c1) == -1)
1252 if (is_negative_overflow_infinity (c1))
1253 return -2;
1254 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1255 if (!c1)
1256 return -2;
1257 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1261 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1263 code2 = SSA_NAME;
1264 n2 = val2;
1265 c2 = NULL_TREE;
1267 else
1269 code2 = TREE_CODE (val2);
1270 n2 = TREE_OPERAND (val2, 0);
1271 c2 = TREE_OPERAND (val2, 1);
1272 if (tree_int_cst_sgn (c2) == -1)
1274 if (is_negative_overflow_infinity (c2))
1275 return -2;
1276 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1277 if (!c2)
1278 return -2;
1279 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1283 /* Both values must use the same name. */
1284 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1286 n1 = TREE_OPERAND (n1, 0);
1287 n2 = TREE_OPERAND (n2, 0);
1289 if (n1 != n2)
1290 return -2;
1292 if (code1 == SSA_NAME && code2 == SSA_NAME)
1293 /* NAME == NAME */
1294 return 0;
1296 /* If overflow is defined we cannot simplify more. */
1297 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1298 return -2;
1300 if (strict_overflow_p != NULL
1301 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1302 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1303 *strict_overflow_p = true;
1305 if (code1 == SSA_NAME)
1307 if (code2 == PLUS_EXPR)
1308 /* NAME < NAME + CST */
1309 return -1;
1310 else if (code2 == MINUS_EXPR)
1311 /* NAME > NAME - CST */
1312 return 1;
1314 else if (code1 == PLUS_EXPR)
1316 if (code2 == SSA_NAME)
1317 /* NAME + CST > NAME */
1318 return 1;
1319 else if (code2 == PLUS_EXPR)
1320 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1321 return compare_values_warnv (c1, c2, strict_overflow_p);
1322 else if (code2 == MINUS_EXPR)
1323 /* NAME + CST1 > NAME - CST2 */
1324 return 1;
1326 else if (code1 == MINUS_EXPR)
1328 if (code2 == SSA_NAME)
1329 /* NAME - CST < NAME */
1330 return -1;
1331 else if (code2 == PLUS_EXPR)
1332 /* NAME - CST1 < NAME + CST2 */
1333 return -1;
1334 else if (code2 == MINUS_EXPR)
1335 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1336 C1 and C2 are swapped in the call to compare_values. */
1337 return compare_values_warnv (c2, c1, strict_overflow_p);
1340 gcc_unreachable ();
1343 /* We cannot compare non-constants. */
1344 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1345 return -2;
1347 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1349 /* We cannot compare overflowed values, except for overflow
1350 infinities. */
1351 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1353 if (strict_overflow_p != NULL)
1354 *strict_overflow_p = true;
1355 if (is_negative_overflow_infinity (val1))
1356 return is_negative_overflow_infinity (val2) ? 0 : -1;
1357 else if (is_negative_overflow_infinity (val2))
1358 return 1;
1359 else if (is_positive_overflow_infinity (val1))
1360 return is_positive_overflow_infinity (val2) ? 0 : 1;
1361 else if (is_positive_overflow_infinity (val2))
1362 return -1;
1363 return -2;
1366 return tree_int_cst_compare (val1, val2);
1368 else
1370 tree t;
1372 /* First see if VAL1 and VAL2 are not the same. */
1373 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1374 return 0;
1376 /* If VAL1 is a lower address than VAL2, return -1. */
1377 if (operand_less_p (val1, val2) == 1)
1378 return -1;
1380 /* If VAL1 is a higher address than VAL2, return +1. */
1381 if (operand_less_p (val2, val1) == 1)
1382 return 1;
1384 /* If VAL1 is different than VAL2, return +2.
1385 For integer constants we either have already returned -1 or 1
1386 or they are equivalent. We still might succeed in proving
1387 something about non-trivial operands. */
1388 if (TREE_CODE (val1) != INTEGER_CST
1389 || TREE_CODE (val2) != INTEGER_CST)
1391 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1392 if (t && integer_onep (t))
1393 return 2;
1396 return -2;
1400 /* Compare values like compare_values_warnv, but treat comparisons of
1401 nonconstants which rely on undefined overflow as incomparable. */
1403 static int
1404 compare_values (tree val1, tree val2)
1406 bool sop;
1407 int ret;
1409 sop = false;
1410 ret = compare_values_warnv (val1, val2, &sop);
1411 if (sop
1412 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1413 ret = -2;
1414 return ret;
1418 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1419 0 if VAL is not inside [MIN, MAX],
1420 -2 if we cannot tell either way.
1422 Benchmark compile/20001226-1.c compilation time after changing this
1423 function. */
1425 static inline int
1426 value_inside_range (tree val, tree min, tree max)
1428 int cmp1, cmp2;
1430 cmp1 = operand_less_p (val, min);
1431 if (cmp1 == -2)
1432 return -2;
1433 if (cmp1 == 1)
1434 return 0;
1436 cmp2 = operand_less_p (max, val);
1437 if (cmp2 == -2)
1438 return -2;
1440 return !cmp2;
1444 /* Return true if value ranges VR0 and VR1 have a non-empty
1445 intersection.
1447 Benchmark compile/20001226-1.c compilation time after changing this
1448 function.
1451 static inline bool
1452 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
1454 /* The value ranges do not intersect if the maximum of the first range is
1455 less than the minimum of the second range or vice versa.
1456 When those relations are unknown, we can't do any better. */
1457 if (operand_less_p (vr0->max, vr1->min) != 0)
1458 return false;
1459 if (operand_less_p (vr1->max, vr0->min) != 0)
1460 return false;
1461 return true;
1465 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1466 include the value zero, -2 if we cannot tell. */
1468 static inline int
1469 range_includes_zero_p (tree min, tree max)
1471 tree zero = build_int_cst (TREE_TYPE (min), 0);
1472 return value_inside_range (zero, min, max);
1475 /* Return true if *VR is know to only contain nonnegative values. */
1477 static inline bool
1478 value_range_nonnegative_p (value_range *vr)
1480 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1481 which would return a useful value should be encoded as a
1482 VR_RANGE. */
1483 if (vr->type == VR_RANGE)
1485 int result = compare_values (vr->min, integer_zero_node);
1486 return (result == 0 || result == 1);
1489 return false;
1492 /* If *VR has a value rante that is a single constant value return that,
1493 otherwise return NULL_TREE. */
1495 static tree
1496 value_range_constant_singleton (value_range *vr)
1498 if (vr->type == VR_RANGE
1499 && vrp_operand_equal_p (vr->min, vr->max)
1500 && is_gimple_min_invariant (vr->min))
1501 return vr->min;
1503 return NULL_TREE;
1506 /* If OP has a value range with a single constant value return that,
1507 otherwise return NULL_TREE. This returns OP itself if OP is a
1508 constant. */
1510 static tree
1511 op_with_constant_singleton_value_range (tree op)
1513 if (is_gimple_min_invariant (op))
1514 return op;
1516 if (TREE_CODE (op) != SSA_NAME)
1517 return NULL_TREE;
1519 return value_range_constant_singleton (get_value_range (op));
1522 /* Return true if op is in a boolean [0, 1] value-range. */
1524 static bool
1525 op_with_boolean_value_range_p (tree op)
1527 value_range *vr;
1529 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1530 return true;
1532 if (integer_zerop (op)
1533 || integer_onep (op))
1534 return true;
1536 if (TREE_CODE (op) != SSA_NAME)
1537 return false;
1539 vr = get_value_range (op);
1540 return (vr->type == VR_RANGE
1541 && integer_zerop (vr->min)
1542 && integer_onep (vr->max));
1545 /* Extract value range information from an ASSERT_EXPR EXPR and store
1546 it in *VR_P. */
1548 static void
1549 extract_range_from_assert (value_range *vr_p, tree expr)
1551 tree var, cond, limit, min, max, type;
1552 value_range *limit_vr;
1553 enum tree_code cond_code;
1555 var = ASSERT_EXPR_VAR (expr);
1556 cond = ASSERT_EXPR_COND (expr);
1558 gcc_assert (COMPARISON_CLASS_P (cond));
1560 /* Find VAR in the ASSERT_EXPR conditional. */
1561 if (var == TREE_OPERAND (cond, 0)
1562 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1563 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1565 /* If the predicate is of the form VAR COMP LIMIT, then we just
1566 take LIMIT from the RHS and use the same comparison code. */
1567 cond_code = TREE_CODE (cond);
1568 limit = TREE_OPERAND (cond, 1);
1569 cond = TREE_OPERAND (cond, 0);
1571 else
1573 /* If the predicate is of the form LIMIT COMP VAR, then we need
1574 to flip around the comparison code to create the proper range
1575 for VAR. */
1576 cond_code = swap_tree_comparison (TREE_CODE (cond));
1577 limit = TREE_OPERAND (cond, 0);
1578 cond = TREE_OPERAND (cond, 1);
1581 limit = avoid_overflow_infinity (limit);
1583 type = TREE_TYPE (var);
1584 gcc_assert (limit != var);
1586 /* For pointer arithmetic, we only keep track of pointer equality
1587 and inequality. */
1588 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1590 set_value_range_to_varying (vr_p);
1591 return;
1594 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1595 try to use LIMIT's range to avoid creating symbolic ranges
1596 unnecessarily. */
1597 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1599 /* LIMIT's range is only interesting if it has any useful information. */
1600 if (limit_vr
1601 && (limit_vr->type == VR_UNDEFINED
1602 || limit_vr->type == VR_VARYING
1603 || symbolic_range_p (limit_vr)))
1604 limit_vr = NULL;
1606 /* Initially, the new range has the same set of equivalences of
1607 VAR's range. This will be revised before returning the final
1608 value. Since assertions may be chained via mutually exclusive
1609 predicates, we will need to trim the set of equivalences before
1610 we are done. */
1611 gcc_assert (vr_p->equiv == NULL);
1612 add_equivalence (&vr_p->equiv, var);
1614 /* Extract a new range based on the asserted comparison for VAR and
1615 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1616 will only use it for equality comparisons (EQ_EXPR). For any
1617 other kind of assertion, we cannot derive a range from LIMIT's
1618 anti-range that can be used to describe the new range. For
1619 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1620 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1621 no single range for x_2 that could describe LE_EXPR, so we might
1622 as well build the range [b_4, +INF] for it.
1623 One special case we handle is extracting a range from a
1624 range test encoded as (unsigned)var + CST <= limit. */
1625 if (TREE_CODE (cond) == NOP_EXPR
1626 || TREE_CODE (cond) == PLUS_EXPR)
1628 if (TREE_CODE (cond) == PLUS_EXPR)
1630 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1631 TREE_OPERAND (cond, 1));
1632 max = int_const_binop (PLUS_EXPR, limit, min);
1633 cond = TREE_OPERAND (cond, 0);
1635 else
1637 min = build_int_cst (TREE_TYPE (var), 0);
1638 max = limit;
1641 /* Make sure to not set TREE_OVERFLOW on the final type
1642 conversion. We are willingly interpreting large positive
1643 unsigned values as negative signed values here. */
1644 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1645 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1647 /* We can transform a max, min range to an anti-range or
1648 vice-versa. Use set_and_canonicalize_value_range which does
1649 this for us. */
1650 if (cond_code == LE_EXPR)
1651 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1652 min, max, vr_p->equiv);
1653 else if (cond_code == GT_EXPR)
1654 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1655 min, max, vr_p->equiv);
1656 else
1657 gcc_unreachable ();
1659 else if (cond_code == EQ_EXPR)
1661 enum value_range_type range_type;
1663 if (limit_vr)
1665 range_type = limit_vr->type;
1666 min = limit_vr->min;
1667 max = limit_vr->max;
1669 else
1671 range_type = VR_RANGE;
1672 min = limit;
1673 max = limit;
1676 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1678 /* When asserting the equality VAR == LIMIT and LIMIT is another
1679 SSA name, the new range will also inherit the equivalence set
1680 from LIMIT. */
1681 if (TREE_CODE (limit) == SSA_NAME)
1682 add_equivalence (&vr_p->equiv, limit);
1684 else if (cond_code == NE_EXPR)
1686 /* As described above, when LIMIT's range is an anti-range and
1687 this assertion is an inequality (NE_EXPR), then we cannot
1688 derive anything from the anti-range. For instance, if
1689 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1690 not imply that VAR's range is [0, 0]. So, in the case of
1691 anti-ranges, we just assert the inequality using LIMIT and
1692 not its anti-range.
1694 If LIMIT_VR is a range, we can only use it to build a new
1695 anti-range if LIMIT_VR is a single-valued range. For
1696 instance, if LIMIT_VR is [0, 1], the predicate
1697 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1698 Rather, it means that for value 0 VAR should be ~[0, 0]
1699 and for value 1, VAR should be ~[1, 1]. We cannot
1700 represent these ranges.
1702 The only situation in which we can build a valid
1703 anti-range is when LIMIT_VR is a single-valued range
1704 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1705 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1706 if (limit_vr
1707 && limit_vr->type == VR_RANGE
1708 && compare_values (limit_vr->min, limit_vr->max) == 0)
1710 min = limit_vr->min;
1711 max = limit_vr->max;
1713 else
1715 /* In any other case, we cannot use LIMIT's range to build a
1716 valid anti-range. */
1717 min = max = limit;
1720 /* If MIN and MAX cover the whole range for their type, then
1721 just use the original LIMIT. */
1722 if (INTEGRAL_TYPE_P (type)
1723 && vrp_val_is_min (min)
1724 && vrp_val_is_max (max))
1725 min = max = limit;
1727 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1728 min, max, vr_p->equiv);
1730 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1732 min = TYPE_MIN_VALUE (type);
1734 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1735 max = limit;
1736 else
1738 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1739 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1740 LT_EXPR. */
1741 max = limit_vr->max;
1744 /* If the maximum value forces us to be out of bounds, simply punt.
1745 It would be pointless to try and do anything more since this
1746 all should be optimized away above us. */
1747 if ((cond_code == LT_EXPR
1748 && compare_values (max, min) == 0)
1749 || is_overflow_infinity (max))
1750 set_value_range_to_varying (vr_p);
1751 else
1753 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1754 if (cond_code == LT_EXPR)
1756 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1757 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1758 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1759 build_int_cst (TREE_TYPE (max), -1));
1760 else
1761 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1762 build_int_cst (TREE_TYPE (max), 1));
1763 if (EXPR_P (max))
1764 TREE_NO_WARNING (max) = 1;
1767 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1770 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1772 max = TYPE_MAX_VALUE (type);
1774 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1775 min = limit;
1776 else
1778 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1779 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1780 GT_EXPR. */
1781 min = limit_vr->min;
1784 /* If the minimum value forces us to be out of bounds, simply punt.
1785 It would be pointless to try and do anything more since this
1786 all should be optimized away above us. */
1787 if ((cond_code == GT_EXPR
1788 && compare_values (min, max) == 0)
1789 || is_overflow_infinity (min))
1790 set_value_range_to_varying (vr_p);
1791 else
1793 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1794 if (cond_code == GT_EXPR)
1796 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1797 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1798 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1799 build_int_cst (TREE_TYPE (min), -1));
1800 else
1801 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1802 build_int_cst (TREE_TYPE (min), 1));
1803 if (EXPR_P (min))
1804 TREE_NO_WARNING (min) = 1;
1807 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1810 else
1811 gcc_unreachable ();
1813 /* Finally intersect the new range with what we already know about var. */
1814 vrp_intersect_ranges (vr_p, get_value_range (var));
1818 /* Extract range information from SSA name VAR and store it in VR. If
1819 VAR has an interesting range, use it. Otherwise, create the
1820 range [VAR, VAR] and return it. This is useful in situations where
1821 we may have conditionals testing values of VARYING names. For
1822 instance,
1824 x_3 = y_5;
1825 if (x_3 > y_5)
1828 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1829 always false. */
1831 static void
1832 extract_range_from_ssa_name (value_range *vr, tree var)
1834 value_range *var_vr = get_value_range (var);
1836 if (var_vr->type != VR_VARYING)
1837 copy_value_range (vr, var_vr);
1838 else
1839 set_value_range (vr, VR_RANGE, var, var, NULL);
1841 add_equivalence (&vr->equiv, var);
1845 /* Wrapper around int_const_binop. If the operation overflows and we
1846 are not using wrapping arithmetic, then adjust the result to be
1847 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1848 NULL_TREE if we need to use an overflow infinity representation but
1849 the type does not support it. */
1851 static tree
1852 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1854 tree res;
1856 res = int_const_binop (code, val1, val2);
1858 /* If we are using unsigned arithmetic, operate symbolically
1859 on -INF and +INF as int_const_binop only handles signed overflow. */
1860 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1862 int checkz = compare_values (res, val1);
1863 bool overflow = false;
1865 /* Ensure that res = val1 [+*] val2 >= val1
1866 or that res = val1 - val2 <= val1. */
1867 if ((code == PLUS_EXPR
1868 && !(checkz == 1 || checkz == 0))
1869 || (code == MINUS_EXPR
1870 && !(checkz == 0 || checkz == -1)))
1872 overflow = true;
1874 /* Checking for multiplication overflow is done by dividing the
1875 output of the multiplication by the first input of the
1876 multiplication. If the result of that division operation is
1877 not equal to the second input of the multiplication, then the
1878 multiplication overflowed. */
1879 else if (code == MULT_EXPR && !integer_zerop (val1))
1881 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1882 res,
1883 val1);
1884 int check = compare_values (tmp, val2);
1886 if (check != 0)
1887 overflow = true;
1890 if (overflow)
1892 res = copy_node (res);
1893 TREE_OVERFLOW (res) = 1;
1897 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1898 /* If the singed operation wraps then int_const_binop has done
1899 everything we want. */
1901 /* Signed division of -1/0 overflows and by the time it gets here
1902 returns NULL_TREE. */
1903 else if (!res)
1904 return NULL_TREE;
1905 else if ((TREE_OVERFLOW (res)
1906 && !TREE_OVERFLOW (val1)
1907 && !TREE_OVERFLOW (val2))
1908 || is_overflow_infinity (val1)
1909 || is_overflow_infinity (val2))
1911 /* If the operation overflowed but neither VAL1 nor VAL2 are
1912 overflown, return -INF or +INF depending on the operation
1913 and the combination of signs of the operands. */
1914 int sgn1 = tree_int_cst_sgn (val1);
1915 int sgn2 = tree_int_cst_sgn (val2);
1917 if (needs_overflow_infinity (TREE_TYPE (res))
1918 && !supports_overflow_infinity (TREE_TYPE (res)))
1919 return NULL_TREE;
1921 /* We have to punt on adding infinities of different signs,
1922 since we can't tell what the sign of the result should be.
1923 Likewise for subtracting infinities of the same sign. */
1924 if (((code == PLUS_EXPR && sgn1 != sgn2)
1925 || (code == MINUS_EXPR && sgn1 == sgn2))
1926 && is_overflow_infinity (val1)
1927 && is_overflow_infinity (val2))
1928 return NULL_TREE;
1930 /* Don't try to handle division or shifting of infinities. */
1931 if ((code == TRUNC_DIV_EXPR
1932 || code == FLOOR_DIV_EXPR
1933 || code == CEIL_DIV_EXPR
1934 || code == EXACT_DIV_EXPR
1935 || code == ROUND_DIV_EXPR
1936 || code == RSHIFT_EXPR)
1937 && (is_overflow_infinity (val1)
1938 || is_overflow_infinity (val2)))
1939 return NULL_TREE;
1941 /* Notice that we only need to handle the restricted set of
1942 operations handled by extract_range_from_binary_expr.
1943 Among them, only multiplication, addition and subtraction
1944 can yield overflow without overflown operands because we
1945 are working with integral types only... except in the
1946 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1947 for division too. */
1949 /* For multiplication, the sign of the overflow is given
1950 by the comparison of the signs of the operands. */
1951 if ((code == MULT_EXPR && sgn1 == sgn2)
1952 /* For addition, the operands must be of the same sign
1953 to yield an overflow. Its sign is therefore that
1954 of one of the operands, for example the first. For
1955 infinite operands X + -INF is negative, not positive. */
1956 || (code == PLUS_EXPR
1957 && (sgn1 >= 0
1958 ? !is_negative_overflow_infinity (val2)
1959 : is_positive_overflow_infinity (val2)))
1960 /* For subtraction, non-infinite operands must be of
1961 different signs to yield an overflow. Its sign is
1962 therefore that of the first operand or the opposite of
1963 that of the second operand. A first operand of 0 counts
1964 as positive here, for the corner case 0 - (-INF), which
1965 overflows, but must yield +INF. For infinite operands 0
1966 - INF is negative, not positive. */
1967 || (code == MINUS_EXPR
1968 && (sgn1 >= 0
1969 ? !is_positive_overflow_infinity (val2)
1970 : is_negative_overflow_infinity (val2)))
1971 /* We only get in here with positive shift count, so the
1972 overflow direction is the same as the sign of val1.
1973 Actually rshift does not overflow at all, but we only
1974 handle the case of shifting overflowed -INF and +INF. */
1975 || (code == RSHIFT_EXPR
1976 && sgn1 >= 0)
1977 /* For division, the only case is -INF / -1 = +INF. */
1978 || code == TRUNC_DIV_EXPR
1979 || code == FLOOR_DIV_EXPR
1980 || code == CEIL_DIV_EXPR
1981 || code == EXACT_DIV_EXPR
1982 || code == ROUND_DIV_EXPR)
1983 return (needs_overflow_infinity (TREE_TYPE (res))
1984 ? positive_overflow_infinity (TREE_TYPE (res))
1985 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1986 else
1987 return (needs_overflow_infinity (TREE_TYPE (res))
1988 ? negative_overflow_infinity (TREE_TYPE (res))
1989 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1992 return res;
1996 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1997 bitmask if some bit is unset, it means for all numbers in the range
1998 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1999 bitmask if some bit is set, it means for all numbers in the range
2000 the bit is 1, otherwise it might be 0 or 1. */
2002 static bool
2003 zero_nonzero_bits_from_vr (const tree expr_type,
2004 value_range *vr,
2005 wide_int *may_be_nonzero,
2006 wide_int *must_be_nonzero)
2008 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2009 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2010 if (!range_int_cst_p (vr)
2011 || is_overflow_infinity (vr->min)
2012 || is_overflow_infinity (vr->max))
2013 return false;
2015 if (range_int_cst_singleton_p (vr))
2017 *may_be_nonzero = vr->min;
2018 *must_be_nonzero = *may_be_nonzero;
2020 else if (tree_int_cst_sgn (vr->min) >= 0
2021 || tree_int_cst_sgn (vr->max) < 0)
2023 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2024 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2025 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2026 if (xor_mask != 0)
2028 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2029 may_be_nonzero->get_precision ());
2030 *may_be_nonzero = *may_be_nonzero | mask;
2031 *must_be_nonzero = must_be_nonzero->and_not (mask);
2035 return true;
2038 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2039 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2040 false otherwise. If *AR can be represented with a single range
2041 *VR1 will be VR_UNDEFINED. */
2043 static bool
2044 ranges_from_anti_range (value_range *ar,
2045 value_range *vr0, value_range *vr1)
2047 tree type = TREE_TYPE (ar->min);
2049 vr0->type = VR_UNDEFINED;
2050 vr1->type = VR_UNDEFINED;
2052 if (ar->type != VR_ANTI_RANGE
2053 || TREE_CODE (ar->min) != INTEGER_CST
2054 || TREE_CODE (ar->max) != INTEGER_CST
2055 || !vrp_val_min (type)
2056 || !vrp_val_max (type))
2057 return false;
2059 if (!vrp_val_is_min (ar->min))
2061 vr0->type = VR_RANGE;
2062 vr0->min = vrp_val_min (type);
2063 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2065 if (!vrp_val_is_max (ar->max))
2067 vr1->type = VR_RANGE;
2068 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2069 vr1->max = vrp_val_max (type);
2071 if (vr0->type == VR_UNDEFINED)
2073 *vr0 = *vr1;
2074 vr1->type = VR_UNDEFINED;
2077 return vr0->type != VR_UNDEFINED;
2080 /* Helper to extract a value-range *VR for a multiplicative operation
2081 *VR0 CODE *VR1. */
2083 static void
2084 extract_range_from_multiplicative_op_1 (value_range *vr,
2085 enum tree_code code,
2086 value_range *vr0, value_range *vr1)
2088 enum value_range_type type;
2089 tree val[4];
2090 size_t i;
2091 tree min, max;
2092 bool sop;
2093 int cmp;
2095 /* Multiplications, divisions and shifts are a bit tricky to handle,
2096 depending on the mix of signs we have in the two ranges, we
2097 need to operate on different values to get the minimum and
2098 maximum values for the new range. One approach is to figure
2099 out all the variations of range combinations and do the
2100 operations.
2102 However, this involves several calls to compare_values and it
2103 is pretty convoluted. It's simpler to do the 4 operations
2104 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2105 MAX1) and then figure the smallest and largest values to form
2106 the new range. */
2107 gcc_assert (code == MULT_EXPR
2108 || code == TRUNC_DIV_EXPR
2109 || code == FLOOR_DIV_EXPR
2110 || code == CEIL_DIV_EXPR
2111 || code == EXACT_DIV_EXPR
2112 || code == ROUND_DIV_EXPR
2113 || code == RSHIFT_EXPR
2114 || code == LSHIFT_EXPR);
2115 gcc_assert ((vr0->type == VR_RANGE
2116 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2117 && vr0->type == vr1->type);
2119 type = vr0->type;
2121 /* Compute the 4 cross operations. */
2122 sop = false;
2123 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2124 if (val[0] == NULL_TREE)
2125 sop = true;
2127 if (vr1->max == vr1->min)
2128 val[1] = NULL_TREE;
2129 else
2131 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2132 if (val[1] == NULL_TREE)
2133 sop = true;
2136 if (vr0->max == vr0->min)
2137 val[2] = NULL_TREE;
2138 else
2140 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2141 if (val[2] == NULL_TREE)
2142 sop = true;
2145 if (vr0->min == vr0->max || vr1->min == vr1->max)
2146 val[3] = NULL_TREE;
2147 else
2149 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2150 if (val[3] == NULL_TREE)
2151 sop = true;
2154 if (sop)
2156 set_value_range_to_varying (vr);
2157 return;
2160 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2161 of VAL[i]. */
2162 min = val[0];
2163 max = val[0];
2164 for (i = 1; i < 4; i++)
2166 if (!is_gimple_min_invariant (min)
2167 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2168 || !is_gimple_min_invariant (max)
2169 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2170 break;
2172 if (val[i])
2174 if (!is_gimple_min_invariant (val[i])
2175 || (TREE_OVERFLOW (val[i])
2176 && !is_overflow_infinity (val[i])))
2178 /* If we found an overflowed value, set MIN and MAX
2179 to it so that we set the resulting range to
2180 VARYING. */
2181 min = max = val[i];
2182 break;
2185 if (compare_values (val[i], min) == -1)
2186 min = val[i];
2188 if (compare_values (val[i], max) == 1)
2189 max = val[i];
2193 /* If either MIN or MAX overflowed, then set the resulting range to
2194 VARYING. But we do accept an overflow infinity
2195 representation. */
2196 if (min == NULL_TREE
2197 || !is_gimple_min_invariant (min)
2198 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2199 || max == NULL_TREE
2200 || !is_gimple_min_invariant (max)
2201 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2203 set_value_range_to_varying (vr);
2204 return;
2207 /* We punt if:
2208 1) [-INF, +INF]
2209 2) [-INF, +-INF(OVF)]
2210 3) [+-INF(OVF), +INF]
2211 4) [+-INF(OVF), +-INF(OVF)]
2212 We learn nothing when we have INF and INF(OVF) on both sides.
2213 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2214 overflow. */
2215 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2216 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2218 set_value_range_to_varying (vr);
2219 return;
2222 cmp = compare_values (min, max);
2223 if (cmp == -2 || cmp == 1)
2225 /* If the new range has its limits swapped around (MIN > MAX),
2226 then the operation caused one of them to wrap around, mark
2227 the new range VARYING. */
2228 set_value_range_to_varying (vr);
2230 else
2231 set_value_range (vr, type, min, max, NULL);
2234 /* Extract range information from a binary operation CODE based on
2235 the ranges of each of its operands *VR0 and *VR1 with resulting
2236 type EXPR_TYPE. The resulting range is stored in *VR. */
2238 static void
2239 extract_range_from_binary_expr_1 (value_range *vr,
2240 enum tree_code code, tree expr_type,
2241 value_range *vr0_, value_range *vr1_)
2243 value_range vr0 = *vr0_, vr1 = *vr1_;
2244 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2245 enum value_range_type type;
2246 tree min = NULL_TREE, max = NULL_TREE;
2247 int cmp;
2249 if (!INTEGRAL_TYPE_P (expr_type)
2250 && !POINTER_TYPE_P (expr_type))
2252 set_value_range_to_varying (vr);
2253 return;
2256 /* Not all binary expressions can be applied to ranges in a
2257 meaningful way. Handle only arithmetic operations. */
2258 if (code != PLUS_EXPR
2259 && code != MINUS_EXPR
2260 && code != POINTER_PLUS_EXPR
2261 && code != MULT_EXPR
2262 && code != TRUNC_DIV_EXPR
2263 && code != FLOOR_DIV_EXPR
2264 && code != CEIL_DIV_EXPR
2265 && code != EXACT_DIV_EXPR
2266 && code != ROUND_DIV_EXPR
2267 && code != TRUNC_MOD_EXPR
2268 && code != RSHIFT_EXPR
2269 && code != LSHIFT_EXPR
2270 && code != MIN_EXPR
2271 && code != MAX_EXPR
2272 && code != BIT_AND_EXPR
2273 && code != BIT_IOR_EXPR
2274 && code != BIT_XOR_EXPR)
2276 set_value_range_to_varying (vr);
2277 return;
2280 /* If both ranges are UNDEFINED, so is the result. */
2281 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2283 set_value_range_to_undefined (vr);
2284 return;
2286 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2287 code. At some point we may want to special-case operations that
2288 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2289 operand. */
2290 else if (vr0.type == VR_UNDEFINED)
2291 set_value_range_to_varying (&vr0);
2292 else if (vr1.type == VR_UNDEFINED)
2293 set_value_range_to_varying (&vr1);
2295 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2296 and express ~[] op X as ([]' op X) U ([]'' op X). */
2297 if (vr0.type == VR_ANTI_RANGE
2298 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2300 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2301 if (vrtem1.type != VR_UNDEFINED)
2303 value_range vrres = VR_INITIALIZER;
2304 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2305 &vrtem1, vr1_);
2306 vrp_meet (vr, &vrres);
2308 return;
2310 /* Likewise for X op ~[]. */
2311 if (vr1.type == VR_ANTI_RANGE
2312 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2314 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2315 if (vrtem1.type != VR_UNDEFINED)
2317 value_range vrres = VR_INITIALIZER;
2318 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2319 vr0_, &vrtem1);
2320 vrp_meet (vr, &vrres);
2322 return;
2325 /* The type of the resulting value range defaults to VR0.TYPE. */
2326 type = vr0.type;
2328 /* Refuse to operate on VARYING ranges, ranges of different kinds
2329 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2330 because we may be able to derive a useful range even if one of
2331 the operands is VR_VARYING or symbolic range. Similarly for
2332 divisions, MIN/MAX and PLUS/MINUS.
2334 TODO, we may be able to derive anti-ranges in some cases. */
2335 if (code != BIT_AND_EXPR
2336 && code != BIT_IOR_EXPR
2337 && code != TRUNC_DIV_EXPR
2338 && code != FLOOR_DIV_EXPR
2339 && code != CEIL_DIV_EXPR
2340 && code != EXACT_DIV_EXPR
2341 && code != ROUND_DIV_EXPR
2342 && code != TRUNC_MOD_EXPR
2343 && code != MIN_EXPR
2344 && code != MAX_EXPR
2345 && code != PLUS_EXPR
2346 && code != MINUS_EXPR
2347 && code != RSHIFT_EXPR
2348 && (vr0.type == VR_VARYING
2349 || vr1.type == VR_VARYING
2350 || vr0.type != vr1.type
2351 || symbolic_range_p (&vr0)
2352 || symbolic_range_p (&vr1)))
2354 set_value_range_to_varying (vr);
2355 return;
2358 /* Now evaluate the expression to determine the new range. */
2359 if (POINTER_TYPE_P (expr_type))
2361 if (code == MIN_EXPR || code == MAX_EXPR)
2363 /* For MIN/MAX expressions with pointers, we only care about
2364 nullness, if both are non null, then the result is nonnull.
2365 If both are null, then the result is null. Otherwise they
2366 are varying. */
2367 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2368 set_value_range_to_nonnull (vr, expr_type);
2369 else if (range_is_null (&vr0) && range_is_null (&vr1))
2370 set_value_range_to_null (vr, expr_type);
2371 else
2372 set_value_range_to_varying (vr);
2374 else if (code == POINTER_PLUS_EXPR)
2376 /* For pointer types, we are really only interested in asserting
2377 whether the expression evaluates to non-NULL. */
2378 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2379 set_value_range_to_nonnull (vr, expr_type);
2380 else if (range_is_null (&vr0) && range_is_null (&vr1))
2381 set_value_range_to_null (vr, expr_type);
2382 else
2383 set_value_range_to_varying (vr);
2385 else if (code == BIT_AND_EXPR)
2387 /* For pointer types, we are really only interested in asserting
2388 whether the expression evaluates to non-NULL. */
2389 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2390 set_value_range_to_nonnull (vr, expr_type);
2391 else if (range_is_null (&vr0) || range_is_null (&vr1))
2392 set_value_range_to_null (vr, expr_type);
2393 else
2394 set_value_range_to_varying (vr);
2396 else
2397 set_value_range_to_varying (vr);
2399 return;
2402 /* For integer ranges, apply the operation to each end of the
2403 range and see what we end up with. */
2404 if (code == PLUS_EXPR || code == MINUS_EXPR)
2406 const bool minus_p = (code == MINUS_EXPR);
2407 tree min_op0 = vr0.min;
2408 tree min_op1 = minus_p ? vr1.max : vr1.min;
2409 tree max_op0 = vr0.max;
2410 tree max_op1 = minus_p ? vr1.min : vr1.max;
2411 tree sym_min_op0 = NULL_TREE;
2412 tree sym_min_op1 = NULL_TREE;
2413 tree sym_max_op0 = NULL_TREE;
2414 tree sym_max_op1 = NULL_TREE;
2415 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2417 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2418 single-symbolic ranges, try to compute the precise resulting range,
2419 but only if we know that this resulting range will also be constant
2420 or single-symbolic. */
2421 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2422 && (TREE_CODE (min_op0) == INTEGER_CST
2423 || (sym_min_op0
2424 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2425 && (TREE_CODE (min_op1) == INTEGER_CST
2426 || (sym_min_op1
2427 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2428 && (!(sym_min_op0 && sym_min_op1)
2429 || (sym_min_op0 == sym_min_op1
2430 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2431 && (TREE_CODE (max_op0) == INTEGER_CST
2432 || (sym_max_op0
2433 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2434 && (TREE_CODE (max_op1) == INTEGER_CST
2435 || (sym_max_op1
2436 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2437 && (!(sym_max_op0 && sym_max_op1)
2438 || (sym_max_op0 == sym_max_op1
2439 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2441 const signop sgn = TYPE_SIGN (expr_type);
2442 const unsigned int prec = TYPE_PRECISION (expr_type);
2443 wide_int type_min, type_max, wmin, wmax;
2444 int min_ovf = 0;
2445 int max_ovf = 0;
2447 /* Get the lower and upper bounds of the type. */
2448 if (TYPE_OVERFLOW_WRAPS (expr_type))
2450 type_min = wi::min_value (prec, sgn);
2451 type_max = wi::max_value (prec, sgn);
2453 else
2455 type_min = vrp_val_min (expr_type);
2456 type_max = vrp_val_max (expr_type);
2459 /* Combine the lower bounds, if any. */
2460 if (min_op0 && min_op1)
2462 if (minus_p)
2464 wmin = wi::sub (min_op0, min_op1);
2466 /* Check for overflow. */
2467 if (wi::cmp (0, min_op1, sgn)
2468 != wi::cmp (wmin, min_op0, sgn))
2469 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2471 else
2473 wmin = wi::add (min_op0, min_op1);
2475 /* Check for overflow. */
2476 if (wi::cmp (min_op1, 0, sgn)
2477 != wi::cmp (wmin, min_op0, sgn))
2478 min_ovf = wi::cmp (min_op0, wmin, sgn);
2481 else if (min_op0)
2482 wmin = min_op0;
2483 else if (min_op1)
2484 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2485 else
2486 wmin = wi::shwi (0, prec);
2488 /* Combine the upper bounds, if any. */
2489 if (max_op0 && max_op1)
2491 if (minus_p)
2493 wmax = wi::sub (max_op0, max_op1);
2495 /* Check for overflow. */
2496 if (wi::cmp (0, max_op1, sgn)
2497 != wi::cmp (wmax, max_op0, sgn))
2498 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2500 else
2502 wmax = wi::add (max_op0, max_op1);
2504 if (wi::cmp (max_op1, 0, sgn)
2505 != wi::cmp (wmax, max_op0, sgn))
2506 max_ovf = wi::cmp (max_op0, wmax, sgn);
2509 else if (max_op0)
2510 wmax = max_op0;
2511 else if (max_op1)
2512 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2513 else
2514 wmax = wi::shwi (0, prec);
2516 /* Check for type overflow. */
2517 if (min_ovf == 0)
2519 if (wi::cmp (wmin, type_min, sgn) == -1)
2520 min_ovf = -1;
2521 else if (wi::cmp (wmin, type_max, sgn) == 1)
2522 min_ovf = 1;
2524 if (max_ovf == 0)
2526 if (wi::cmp (wmax, type_min, sgn) == -1)
2527 max_ovf = -1;
2528 else if (wi::cmp (wmax, type_max, sgn) == 1)
2529 max_ovf = 1;
2532 /* If we have overflow for the constant part and the resulting
2533 range will be symbolic, drop to VR_VARYING. */
2534 if ((min_ovf && sym_min_op0 != sym_min_op1)
2535 || (max_ovf && sym_max_op0 != sym_max_op1))
2537 set_value_range_to_varying (vr);
2538 return;
2541 if (TYPE_OVERFLOW_WRAPS (expr_type))
2543 /* If overflow wraps, truncate the values and adjust the
2544 range kind and bounds appropriately. */
2545 wide_int tmin = wide_int::from (wmin, prec, sgn);
2546 wide_int tmax = wide_int::from (wmax, prec, sgn);
2547 if (min_ovf == max_ovf)
2549 /* No overflow or both overflow or underflow. The
2550 range kind stays VR_RANGE. */
2551 min = wide_int_to_tree (expr_type, tmin);
2552 max = wide_int_to_tree (expr_type, tmax);
2554 else if ((min_ovf == -1 && max_ovf == 0)
2555 || (max_ovf == 1 && min_ovf == 0))
2557 /* Min underflow or max overflow. The range kind
2558 changes to VR_ANTI_RANGE. */
2559 bool covers = false;
2560 wide_int tem = tmin;
2561 type = VR_ANTI_RANGE;
2562 tmin = tmax + 1;
2563 if (wi::cmp (tmin, tmax, sgn) < 0)
2564 covers = true;
2565 tmax = tem - 1;
2566 if (wi::cmp (tmax, tem, sgn) > 0)
2567 covers = true;
2568 /* If the anti-range would cover nothing, drop to varying.
2569 Likewise if the anti-range bounds are outside of the
2570 types values. */
2571 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2573 set_value_range_to_varying (vr);
2574 return;
2576 min = wide_int_to_tree (expr_type, tmin);
2577 max = wide_int_to_tree (expr_type, tmax);
2579 else
2581 /* Other underflow and/or overflow, drop to VR_VARYING. */
2582 set_value_range_to_varying (vr);
2583 return;
2586 else
2588 /* If overflow does not wrap, saturate to the types min/max
2589 value. */
2590 if (min_ovf == -1)
2592 if (needs_overflow_infinity (expr_type)
2593 && supports_overflow_infinity (expr_type))
2594 min = negative_overflow_infinity (expr_type);
2595 else
2596 min = wide_int_to_tree (expr_type, type_min);
2598 else if (min_ovf == 1)
2600 if (needs_overflow_infinity (expr_type)
2601 && supports_overflow_infinity (expr_type))
2602 min = positive_overflow_infinity (expr_type);
2603 else
2604 min = wide_int_to_tree (expr_type, type_max);
2606 else
2607 min = wide_int_to_tree (expr_type, wmin);
2609 if (max_ovf == -1)
2611 if (needs_overflow_infinity (expr_type)
2612 && supports_overflow_infinity (expr_type))
2613 max = negative_overflow_infinity (expr_type);
2614 else
2615 max = wide_int_to_tree (expr_type, type_min);
2617 else if (max_ovf == 1)
2619 if (needs_overflow_infinity (expr_type)
2620 && supports_overflow_infinity (expr_type))
2621 max = positive_overflow_infinity (expr_type);
2622 else
2623 max = wide_int_to_tree (expr_type, type_max);
2625 else
2626 max = wide_int_to_tree (expr_type, wmax);
2629 if (needs_overflow_infinity (expr_type)
2630 && supports_overflow_infinity (expr_type))
2632 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2633 || (min_op1
2634 && (minus_p
2635 ? is_positive_overflow_infinity (min_op1)
2636 : is_negative_overflow_infinity (min_op1))))
2637 min = negative_overflow_infinity (expr_type);
2638 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2639 || (max_op1
2640 && (minus_p
2641 ? is_negative_overflow_infinity (max_op1)
2642 : is_positive_overflow_infinity (max_op1))))
2643 max = positive_overflow_infinity (expr_type);
2646 /* If the result lower bound is constant, we're done;
2647 otherwise, build the symbolic lower bound. */
2648 if (sym_min_op0 == sym_min_op1)
2650 else if (sym_min_op0)
2651 min = build_symbolic_expr (expr_type, sym_min_op0,
2652 neg_min_op0, min);
2653 else if (sym_min_op1)
2654 min = build_symbolic_expr (expr_type, sym_min_op1,
2655 neg_min_op1 ^ minus_p, min);
2657 /* Likewise for the upper bound. */
2658 if (sym_max_op0 == sym_max_op1)
2660 else if (sym_max_op0)
2661 max = build_symbolic_expr (expr_type, sym_max_op0,
2662 neg_max_op0, max);
2663 else if (sym_max_op1)
2664 max = build_symbolic_expr (expr_type, sym_max_op1,
2665 neg_max_op1 ^ minus_p, max);
2667 else
2669 /* For other cases, for example if we have a PLUS_EXPR with two
2670 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2671 to compute a precise range for such a case.
2672 ??? General even mixed range kind operations can be expressed
2673 by for example transforming ~[3, 5] + [1, 2] to range-only
2674 operations and a union primitive:
2675 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2676 [-INF+1, 4] U [6, +INF(OVF)]
2677 though usually the union is not exactly representable with
2678 a single range or anti-range as the above is
2679 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2680 but one could use a scheme similar to equivalences for this. */
2681 set_value_range_to_varying (vr);
2682 return;
2685 else if (code == MIN_EXPR
2686 || code == MAX_EXPR)
2688 if (vr0.type == VR_RANGE
2689 && !symbolic_range_p (&vr0))
2691 type = VR_RANGE;
2692 if (vr1.type == VR_RANGE
2693 && !symbolic_range_p (&vr1))
2695 /* For operations that make the resulting range directly
2696 proportional to the original ranges, apply the operation to
2697 the same end of each range. */
2698 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2699 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2701 else if (code == MIN_EXPR)
2703 min = vrp_val_min (expr_type);
2704 max = vr0.max;
2706 else if (code == MAX_EXPR)
2708 min = vr0.min;
2709 max = vrp_val_max (expr_type);
2712 else if (vr1.type == VR_RANGE
2713 && !symbolic_range_p (&vr1))
2715 type = VR_RANGE;
2716 if (code == MIN_EXPR)
2718 min = vrp_val_min (expr_type);
2719 max = vr1.max;
2721 else if (code == MAX_EXPR)
2723 min = vr1.min;
2724 max = vrp_val_max (expr_type);
2727 else
2729 set_value_range_to_varying (vr);
2730 return;
2733 else if (code == MULT_EXPR)
2735 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2736 drop to varying. This test requires 2*prec bits if both
2737 operands are signed and 2*prec + 2 bits if either is not. */
2739 signop sign = TYPE_SIGN (expr_type);
2740 unsigned int prec = TYPE_PRECISION (expr_type);
2742 if (range_int_cst_p (&vr0)
2743 && range_int_cst_p (&vr1)
2744 && TYPE_OVERFLOW_WRAPS (expr_type))
2746 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2747 typedef generic_wide_int
2748 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2749 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2750 vrp_int size = sizem1 + 1;
2752 /* Extend the values using the sign of the result to PREC2.
2753 From here on out, everthing is just signed math no matter
2754 what the input types were. */
2755 vrp_int min0 = vrp_int_cst (vr0.min);
2756 vrp_int max0 = vrp_int_cst (vr0.max);
2757 vrp_int min1 = vrp_int_cst (vr1.min);
2758 vrp_int max1 = vrp_int_cst (vr1.max);
2759 /* Canonicalize the intervals. */
2760 if (sign == UNSIGNED)
2762 if (wi::ltu_p (size, min0 + max0))
2764 min0 -= size;
2765 max0 -= size;
2768 if (wi::ltu_p (size, min1 + max1))
2770 min1 -= size;
2771 max1 -= size;
2775 vrp_int prod0 = min0 * min1;
2776 vrp_int prod1 = min0 * max1;
2777 vrp_int prod2 = max0 * min1;
2778 vrp_int prod3 = max0 * max1;
2780 /* Sort the 4 products so that min is in prod0 and max is in
2781 prod3. */
2782 /* min0min1 > max0max1 */
2783 if (wi::gts_p (prod0, prod3))
2784 std::swap (prod0, prod3);
2786 /* min0max1 > max0min1 */
2787 if (wi::gts_p (prod1, prod2))
2788 std::swap (prod1, prod2);
2790 if (wi::gts_p (prod0, prod1))
2791 std::swap (prod0, prod1);
2793 if (wi::gts_p (prod2, prod3))
2794 std::swap (prod2, prod3);
2796 /* diff = max - min. */
2797 prod2 = prod3 - prod0;
2798 if (wi::geu_p (prod2, sizem1))
2800 /* the range covers all values. */
2801 set_value_range_to_varying (vr);
2802 return;
2805 /* The following should handle the wrapping and selecting
2806 VR_ANTI_RANGE for us. */
2807 min = wide_int_to_tree (expr_type, prod0);
2808 max = wide_int_to_tree (expr_type, prod3);
2809 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2810 return;
2813 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2814 drop to VR_VARYING. It would take more effort to compute a
2815 precise range for such a case. For example, if we have
2816 op0 == 65536 and op1 == 65536 with their ranges both being
2817 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2818 we cannot claim that the product is in ~[0,0]. Note that we
2819 are guaranteed to have vr0.type == vr1.type at this
2820 point. */
2821 if (vr0.type == VR_ANTI_RANGE
2822 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2824 set_value_range_to_varying (vr);
2825 return;
2828 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2829 return;
2831 else if (code == RSHIFT_EXPR
2832 || code == LSHIFT_EXPR)
2834 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2835 then drop to VR_VARYING. Outside of this range we get undefined
2836 behavior from the shift operation. We cannot even trust
2837 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2838 shifts, and the operation at the tree level may be widened. */
2839 if (range_int_cst_p (&vr1)
2840 && compare_tree_int (vr1.min, 0) >= 0
2841 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2843 if (code == RSHIFT_EXPR)
2845 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2846 useful ranges just from the shift count. E.g.
2847 x >> 63 for signed 64-bit x is always [-1, 0]. */
2848 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2850 vr0.type = type = VR_RANGE;
2851 vr0.min = vrp_val_min (expr_type);
2852 vr0.max = vrp_val_max (expr_type);
2854 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2855 return;
2857 /* We can map lshifts by constants to MULT_EXPR handling. */
2858 else if (code == LSHIFT_EXPR
2859 && range_int_cst_singleton_p (&vr1))
2861 bool saved_flag_wrapv;
2862 value_range vr1p = VR_INITIALIZER;
2863 vr1p.type = VR_RANGE;
2864 vr1p.min = (wide_int_to_tree
2865 (expr_type,
2866 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2867 TYPE_PRECISION (expr_type))));
2868 vr1p.max = vr1p.min;
2869 /* We have to use a wrapping multiply though as signed overflow
2870 on lshifts is implementation defined in C89. */
2871 saved_flag_wrapv = flag_wrapv;
2872 flag_wrapv = 1;
2873 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2874 &vr0, &vr1p);
2875 flag_wrapv = saved_flag_wrapv;
2876 return;
2878 else if (code == LSHIFT_EXPR
2879 && range_int_cst_p (&vr0))
2881 int prec = TYPE_PRECISION (expr_type);
2882 int overflow_pos = prec;
2883 int bound_shift;
2884 wide_int low_bound, high_bound;
2885 bool uns = TYPE_UNSIGNED (expr_type);
2886 bool in_bounds = false;
2888 if (!uns)
2889 overflow_pos -= 1;
2891 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2892 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2893 overflow. However, for that to happen, vr1.max needs to be
2894 zero, which means vr1 is a singleton range of zero, which
2895 means it should be handled by the previous LSHIFT_EXPR
2896 if-clause. */
2897 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2898 wide_int complement = ~(bound - 1);
2900 if (uns)
2902 low_bound = bound;
2903 high_bound = complement;
2904 if (wi::ltu_p (vr0.max, low_bound))
2906 /* [5, 6] << [1, 2] == [10, 24]. */
2907 /* We're shifting out only zeroes, the value increases
2908 monotonically. */
2909 in_bounds = true;
2911 else if (wi::ltu_p (high_bound, vr0.min))
2913 /* [0xffffff00, 0xffffffff] << [1, 2]
2914 == [0xfffffc00, 0xfffffffe]. */
2915 /* We're shifting out only ones, the value decreases
2916 monotonically. */
2917 in_bounds = true;
2920 else
2922 /* [-1, 1] << [1, 2] == [-4, 4]. */
2923 low_bound = complement;
2924 high_bound = bound;
2925 if (wi::lts_p (vr0.max, high_bound)
2926 && wi::lts_p (low_bound, vr0.min))
2928 /* For non-negative numbers, we're shifting out only
2929 zeroes, the value increases monotonically.
2930 For negative numbers, we're shifting out only ones, the
2931 value decreases monotomically. */
2932 in_bounds = true;
2936 if (in_bounds)
2938 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2939 return;
2943 set_value_range_to_varying (vr);
2944 return;
2946 else if (code == TRUNC_DIV_EXPR
2947 || code == FLOOR_DIV_EXPR
2948 || code == CEIL_DIV_EXPR
2949 || code == EXACT_DIV_EXPR
2950 || code == ROUND_DIV_EXPR)
2952 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2954 /* For division, if op1 has VR_RANGE but op0 does not, something
2955 can be deduced just from that range. Say [min, max] / [4, max]
2956 gives [min / 4, max / 4] range. */
2957 if (vr1.type == VR_RANGE
2958 && !symbolic_range_p (&vr1)
2959 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2961 vr0.type = type = VR_RANGE;
2962 vr0.min = vrp_val_min (expr_type);
2963 vr0.max = vrp_val_max (expr_type);
2965 else
2967 set_value_range_to_varying (vr);
2968 return;
2972 /* For divisions, if flag_non_call_exceptions is true, we must
2973 not eliminate a division by zero. */
2974 if (cfun->can_throw_non_call_exceptions
2975 && (vr1.type != VR_RANGE
2976 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2978 set_value_range_to_varying (vr);
2979 return;
2982 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2983 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2984 include 0. */
2985 if (vr0.type == VR_RANGE
2986 && (vr1.type != VR_RANGE
2987 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2989 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2990 int cmp;
2992 min = NULL_TREE;
2993 max = NULL_TREE;
2994 if (TYPE_UNSIGNED (expr_type)
2995 || value_range_nonnegative_p (&vr1))
2997 /* For unsigned division or when divisor is known
2998 to be non-negative, the range has to cover
2999 all numbers from 0 to max for positive max
3000 and all numbers from min to 0 for negative min. */
3001 cmp = compare_values (vr0.max, zero);
3002 if (cmp == -1)
3004 /* When vr0.max < 0, vr1.min != 0 and value
3005 ranges for dividend and divisor are available. */
3006 if (vr1.type == VR_RANGE
3007 && !symbolic_range_p (&vr0)
3008 && !symbolic_range_p (&vr1)
3009 && compare_values (vr1.min, zero) != 0)
3010 max = int_const_binop (code, vr0.max, vr1.min);
3011 else
3012 max = zero;
3014 else if (cmp == 0 || cmp == 1)
3015 max = vr0.max;
3016 else
3017 type = VR_VARYING;
3018 cmp = compare_values (vr0.min, zero);
3019 if (cmp == 1)
3021 /* For unsigned division when value ranges for dividend
3022 and divisor are available. */
3023 if (vr1.type == VR_RANGE
3024 && !symbolic_range_p (&vr0)
3025 && !symbolic_range_p (&vr1)
3026 && compare_values (vr1.max, zero) != 0)
3027 min = int_const_binop (code, vr0.min, vr1.max);
3028 else
3029 min = zero;
3031 else if (cmp == 0 || cmp == -1)
3032 min = vr0.min;
3033 else
3034 type = VR_VARYING;
3036 else
3038 /* Otherwise the range is -max .. max or min .. -min
3039 depending on which bound is bigger in absolute value,
3040 as the division can change the sign. */
3041 abs_extent_range (vr, vr0.min, vr0.max);
3042 return;
3044 if (type == VR_VARYING)
3046 set_value_range_to_varying (vr);
3047 return;
3050 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1))
3052 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3053 return;
3056 else if (code == TRUNC_MOD_EXPR)
3058 if (range_is_null (&vr1))
3060 set_value_range_to_undefined (vr);
3061 return;
3063 /* ABS (A % B) < ABS (B) and either
3064 0 <= A % B <= A or A <= A % B <= 0. */
3065 type = VR_RANGE;
3066 signop sgn = TYPE_SIGN (expr_type);
3067 unsigned int prec = TYPE_PRECISION (expr_type);
3068 wide_int wmin, wmax, tmp;
3069 wide_int zero = wi::zero (prec);
3070 wide_int one = wi::one (prec);
3071 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
3073 wmax = wi::sub (vr1.max, one);
3074 if (sgn == SIGNED)
3076 tmp = wi::sub (wi::minus_one (prec), vr1.min);
3077 wmax = wi::smax (wmax, tmp);
3080 else
3082 wmax = wi::max_value (prec, sgn);
3083 /* X % INT_MIN may be INT_MAX. */
3084 if (sgn == UNSIGNED)
3085 wmax = wmax - one;
3088 if (sgn == UNSIGNED)
3089 wmin = zero;
3090 else
3092 wmin = -wmax;
3093 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3095 tmp = vr0.min;
3096 if (wi::gts_p (tmp, zero))
3097 tmp = zero;
3098 wmin = wi::smax (wmin, tmp);
3102 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3104 tmp = vr0.max;
3105 if (sgn == SIGNED && wi::neg_p (tmp))
3106 tmp = zero;
3107 wmax = wi::min (wmax, tmp, sgn);
3110 min = wide_int_to_tree (expr_type, wmin);
3111 max = wide_int_to_tree (expr_type, wmax);
3113 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3115 bool int_cst_range0, int_cst_range1;
3116 wide_int may_be_nonzero0, may_be_nonzero1;
3117 wide_int must_be_nonzero0, must_be_nonzero1;
3119 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3120 &may_be_nonzero0,
3121 &must_be_nonzero0);
3122 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3123 &may_be_nonzero1,
3124 &must_be_nonzero1);
3126 type = VR_RANGE;
3127 if (code == BIT_AND_EXPR)
3129 min = wide_int_to_tree (expr_type,
3130 must_be_nonzero0 & must_be_nonzero1);
3131 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3132 /* If both input ranges contain only negative values we can
3133 truncate the result range maximum to the minimum of the
3134 input range maxima. */
3135 if (int_cst_range0 && int_cst_range1
3136 && tree_int_cst_sgn (vr0.max) < 0
3137 && tree_int_cst_sgn (vr1.max) < 0)
3139 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3140 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3142 /* If either input range contains only non-negative values
3143 we can truncate the result range maximum to the respective
3144 maximum of the input range. */
3145 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3146 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3147 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3148 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3149 max = wide_int_to_tree (expr_type, wmax);
3151 else if (code == BIT_IOR_EXPR)
3153 max = wide_int_to_tree (expr_type,
3154 may_be_nonzero0 | may_be_nonzero1);
3155 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3156 /* If the input ranges contain only positive values we can
3157 truncate the minimum of the result range to the maximum
3158 of the input range minima. */
3159 if (int_cst_range0 && int_cst_range1
3160 && tree_int_cst_sgn (vr0.min) >= 0
3161 && tree_int_cst_sgn (vr1.min) >= 0)
3163 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3164 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3166 /* If either input range contains only negative values
3167 we can truncate the minimum of the result range to the
3168 respective minimum range. */
3169 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3170 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3171 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3172 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3173 min = wide_int_to_tree (expr_type, wmin);
3175 else if (code == BIT_XOR_EXPR)
3177 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3178 | ~(may_be_nonzero0 | may_be_nonzero1));
3179 wide_int result_one_bits
3180 = (must_be_nonzero0.and_not (may_be_nonzero1)
3181 | must_be_nonzero1.and_not (may_be_nonzero0));
3182 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3183 min = wide_int_to_tree (expr_type, result_one_bits);
3184 /* If the range has all positive or all negative values the
3185 result is better than VARYING. */
3186 if (tree_int_cst_sgn (min) < 0
3187 || tree_int_cst_sgn (max) >= 0)
3189 else
3190 max = min = NULL_TREE;
3193 else
3194 gcc_unreachable ();
3196 /* If either MIN or MAX overflowed, then set the resulting range to
3197 VARYING. But we do accept an overflow infinity representation. */
3198 if (min == NULL_TREE
3199 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3200 || max == NULL_TREE
3201 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3203 set_value_range_to_varying (vr);
3204 return;
3207 /* We punt if:
3208 1) [-INF, +INF]
3209 2) [-INF, +-INF(OVF)]
3210 3) [+-INF(OVF), +INF]
3211 4) [+-INF(OVF), +-INF(OVF)]
3212 We learn nothing when we have INF and INF(OVF) on both sides.
3213 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3214 overflow. */
3215 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3216 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3218 set_value_range_to_varying (vr);
3219 return;
3222 cmp = compare_values (min, max);
3223 if (cmp == -2 || cmp == 1)
3225 /* If the new range has its limits swapped around (MIN > MAX),
3226 then the operation caused one of them to wrap around, mark
3227 the new range VARYING. */
3228 set_value_range_to_varying (vr);
3230 else
3231 set_value_range (vr, type, min, max, NULL);
3234 /* Extract range information from a binary expression OP0 CODE OP1 based on
3235 the ranges of each of its operands with resulting type EXPR_TYPE.
3236 The resulting range is stored in *VR. */
3238 static void
3239 extract_range_from_binary_expr (value_range *vr,
3240 enum tree_code code,
3241 tree expr_type, tree op0, tree op1)
3243 value_range vr0 = VR_INITIALIZER;
3244 value_range vr1 = VR_INITIALIZER;
3246 /* Get value ranges for each operand. For constant operands, create
3247 a new value range with the operand to simplify processing. */
3248 if (TREE_CODE (op0) == SSA_NAME)
3249 vr0 = *(get_value_range (op0));
3250 else if (is_gimple_min_invariant (op0))
3251 set_value_range_to_value (&vr0, op0, NULL);
3252 else
3253 set_value_range_to_varying (&vr0);
3255 if (TREE_CODE (op1) == SSA_NAME)
3256 vr1 = *(get_value_range (op1));
3257 else if (is_gimple_min_invariant (op1))
3258 set_value_range_to_value (&vr1, op1, NULL);
3259 else
3260 set_value_range_to_varying (&vr1);
3262 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3264 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3265 and based on the other operand, for example if it was deduced from a
3266 symbolic comparison. When a bound of the range of the first operand
3267 is invariant, we set the corresponding bound of the new range to INF
3268 in order to avoid recursing on the range of the second operand. */
3269 if (vr->type == VR_VARYING
3270 && (code == PLUS_EXPR || code == MINUS_EXPR)
3271 && TREE_CODE (op1) == SSA_NAME
3272 && vr0.type == VR_RANGE
3273 && symbolic_range_based_on_p (&vr0, op1))
3275 const bool minus_p = (code == MINUS_EXPR);
3276 value_range n_vr1 = VR_INITIALIZER;
3278 /* Try with VR0 and [-INF, OP1]. */
3279 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3280 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3282 /* Try with VR0 and [OP1, +INF]. */
3283 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3284 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3286 /* Try with VR0 and [OP1, OP1]. */
3287 else
3288 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3290 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3293 if (vr->type == VR_VARYING
3294 && (code == PLUS_EXPR || code == MINUS_EXPR)
3295 && TREE_CODE (op0) == SSA_NAME
3296 && vr1.type == VR_RANGE
3297 && symbolic_range_based_on_p (&vr1, op0))
3299 const bool minus_p = (code == MINUS_EXPR);
3300 value_range n_vr0 = VR_INITIALIZER;
3302 /* Try with [-INF, OP0] and VR1. */
3303 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3304 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3306 /* Try with [OP0, +INF] and VR1. */
3307 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3308 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3310 /* Try with [OP0, OP0] and VR1. */
3311 else
3312 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3314 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3318 /* Extract range information from a unary operation CODE based on
3319 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3320 The resulting range is stored in *VR. */
3322 static void
3323 extract_range_from_unary_expr_1 (value_range *vr,
3324 enum tree_code code, tree type,
3325 value_range *vr0_, tree op0_type)
3327 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3329 /* VRP only operates on integral and pointer types. */
3330 if (!(INTEGRAL_TYPE_P (op0_type)
3331 || POINTER_TYPE_P (op0_type))
3332 || !(INTEGRAL_TYPE_P (type)
3333 || POINTER_TYPE_P (type)))
3335 set_value_range_to_varying (vr);
3336 return;
3339 /* If VR0 is UNDEFINED, so is the result. */
3340 if (vr0.type == VR_UNDEFINED)
3342 set_value_range_to_undefined (vr);
3343 return;
3346 /* Handle operations that we express in terms of others. */
3347 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3349 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3350 copy_value_range (vr, &vr0);
3351 return;
3353 else if (code == NEGATE_EXPR)
3355 /* -X is simply 0 - X, so re-use existing code that also handles
3356 anti-ranges fine. */
3357 value_range zero = VR_INITIALIZER;
3358 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3359 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3360 return;
3362 else if (code == BIT_NOT_EXPR)
3364 /* ~X is simply -1 - X, so re-use existing code that also handles
3365 anti-ranges fine. */
3366 value_range minusone = VR_INITIALIZER;
3367 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3368 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3369 type, &minusone, &vr0);
3370 return;
3373 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3374 and express op ~[] as (op []') U (op []''). */
3375 if (vr0.type == VR_ANTI_RANGE
3376 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3378 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3379 if (vrtem1.type != VR_UNDEFINED)
3381 value_range vrres = VR_INITIALIZER;
3382 extract_range_from_unary_expr_1 (&vrres, code, type,
3383 &vrtem1, op0_type);
3384 vrp_meet (vr, &vrres);
3386 return;
3389 if (CONVERT_EXPR_CODE_P (code))
3391 tree inner_type = op0_type;
3392 tree outer_type = type;
3394 /* If the expression evaluates to a pointer, we are only interested in
3395 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3396 if (POINTER_TYPE_P (type))
3398 if (range_is_nonnull (&vr0))
3399 set_value_range_to_nonnull (vr, type);
3400 else if (range_is_null (&vr0))
3401 set_value_range_to_null (vr, type);
3402 else
3403 set_value_range_to_varying (vr);
3404 return;
3407 /* If VR0 is varying and we increase the type precision, assume
3408 a full range for the following transformation. */
3409 if (vr0.type == VR_VARYING
3410 && INTEGRAL_TYPE_P (inner_type)
3411 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3413 vr0.type = VR_RANGE;
3414 vr0.min = TYPE_MIN_VALUE (inner_type);
3415 vr0.max = TYPE_MAX_VALUE (inner_type);
3418 /* If VR0 is a constant range or anti-range and the conversion is
3419 not truncating we can convert the min and max values and
3420 canonicalize the resulting range. Otherwise we can do the
3421 conversion if the size of the range is less than what the
3422 precision of the target type can represent and the range is
3423 not an anti-range. */
3424 if ((vr0.type == VR_RANGE
3425 || vr0.type == VR_ANTI_RANGE)
3426 && TREE_CODE (vr0.min) == INTEGER_CST
3427 && TREE_CODE (vr0.max) == INTEGER_CST
3428 && (!is_overflow_infinity (vr0.min)
3429 || (vr0.type == VR_RANGE
3430 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3431 && needs_overflow_infinity (outer_type)
3432 && supports_overflow_infinity (outer_type)))
3433 && (!is_overflow_infinity (vr0.max)
3434 || (vr0.type == VR_RANGE
3435 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3436 && needs_overflow_infinity (outer_type)
3437 && supports_overflow_infinity (outer_type)))
3438 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3439 || (vr0.type == VR_RANGE
3440 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3441 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3442 size_int (TYPE_PRECISION (outer_type)))))))
3444 tree new_min, new_max;
3445 if (is_overflow_infinity (vr0.min))
3446 new_min = negative_overflow_infinity (outer_type);
3447 else
3448 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3449 0, false);
3450 if (is_overflow_infinity (vr0.max))
3451 new_max = positive_overflow_infinity (outer_type);
3452 else
3453 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3454 0, false);
3455 set_and_canonicalize_value_range (vr, vr0.type,
3456 new_min, new_max, NULL);
3457 return;
3460 set_value_range_to_varying (vr);
3461 return;
3463 else if (code == ABS_EXPR)
3465 tree min, max;
3466 int cmp;
3468 /* Pass through vr0 in the easy cases. */
3469 if (TYPE_UNSIGNED (type)
3470 || value_range_nonnegative_p (&vr0))
3472 copy_value_range (vr, &vr0);
3473 return;
3476 /* For the remaining varying or symbolic ranges we can't do anything
3477 useful. */
3478 if (vr0.type == VR_VARYING
3479 || symbolic_range_p (&vr0))
3481 set_value_range_to_varying (vr);
3482 return;
3485 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3486 useful range. */
3487 if (!TYPE_OVERFLOW_UNDEFINED (type)
3488 && ((vr0.type == VR_RANGE
3489 && vrp_val_is_min (vr0.min))
3490 || (vr0.type == VR_ANTI_RANGE
3491 && !vrp_val_is_min (vr0.min))))
3493 set_value_range_to_varying (vr);
3494 return;
3497 /* ABS_EXPR may flip the range around, if the original range
3498 included negative values. */
3499 if (is_overflow_infinity (vr0.min))
3500 min = positive_overflow_infinity (type);
3501 else if (!vrp_val_is_min (vr0.min))
3502 min = fold_unary_to_constant (code, type, vr0.min);
3503 else if (!needs_overflow_infinity (type))
3504 min = TYPE_MAX_VALUE (type);
3505 else if (supports_overflow_infinity (type))
3506 min = positive_overflow_infinity (type);
3507 else
3509 set_value_range_to_varying (vr);
3510 return;
3513 if (is_overflow_infinity (vr0.max))
3514 max = positive_overflow_infinity (type);
3515 else if (!vrp_val_is_min (vr0.max))
3516 max = fold_unary_to_constant (code, type, vr0.max);
3517 else if (!needs_overflow_infinity (type))
3518 max = TYPE_MAX_VALUE (type);
3519 else if (supports_overflow_infinity (type)
3520 /* We shouldn't generate [+INF, +INF] as set_value_range
3521 doesn't like this and ICEs. */
3522 && !is_positive_overflow_infinity (min))
3523 max = positive_overflow_infinity (type);
3524 else
3526 set_value_range_to_varying (vr);
3527 return;
3530 cmp = compare_values (min, max);
3532 /* If a VR_ANTI_RANGEs contains zero, then we have
3533 ~[-INF, min(MIN, MAX)]. */
3534 if (vr0.type == VR_ANTI_RANGE)
3536 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3538 /* Take the lower of the two values. */
3539 if (cmp != 1)
3540 max = min;
3542 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3543 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3544 flag_wrapv is set and the original anti-range doesn't include
3545 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3546 if (TYPE_OVERFLOW_WRAPS (type))
3548 tree type_min_value = TYPE_MIN_VALUE (type);
3550 min = (vr0.min != type_min_value
3551 ? int_const_binop (PLUS_EXPR, type_min_value,
3552 build_int_cst (TREE_TYPE (type_min_value), 1))
3553 : type_min_value);
3555 else
3557 if (overflow_infinity_range_p (&vr0))
3558 min = negative_overflow_infinity (type);
3559 else
3560 min = TYPE_MIN_VALUE (type);
3563 else
3565 /* All else has failed, so create the range [0, INF], even for
3566 flag_wrapv since TYPE_MIN_VALUE is in the original
3567 anti-range. */
3568 vr0.type = VR_RANGE;
3569 min = build_int_cst (type, 0);
3570 if (needs_overflow_infinity (type))
3572 if (supports_overflow_infinity (type))
3573 max = positive_overflow_infinity (type);
3574 else
3576 set_value_range_to_varying (vr);
3577 return;
3580 else
3581 max = TYPE_MAX_VALUE (type);
3585 /* If the range contains zero then we know that the minimum value in the
3586 range will be zero. */
3587 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3589 if (cmp == 1)
3590 max = min;
3591 min = build_int_cst (type, 0);
3593 else
3595 /* If the range was reversed, swap MIN and MAX. */
3596 if (cmp == 1)
3597 std::swap (min, max);
3600 cmp = compare_values (min, max);
3601 if (cmp == -2 || cmp == 1)
3603 /* If the new range has its limits swapped around (MIN > MAX),
3604 then the operation caused one of them to wrap around, mark
3605 the new range VARYING. */
3606 set_value_range_to_varying (vr);
3608 else
3609 set_value_range (vr, vr0.type, min, max, NULL);
3610 return;
3613 /* For unhandled operations fall back to varying. */
3614 set_value_range_to_varying (vr);
3615 return;
3619 /* Extract range information from a unary expression CODE OP0 based on
3620 the range of its operand with resulting type TYPE.
3621 The resulting range is stored in *VR. */
3623 static void
3624 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3625 tree type, tree op0)
3627 value_range vr0 = VR_INITIALIZER;
3629 /* Get value ranges for the operand. For constant operands, create
3630 a new value range with the operand to simplify processing. */
3631 if (TREE_CODE (op0) == SSA_NAME)
3632 vr0 = *(get_value_range (op0));
3633 else if (is_gimple_min_invariant (op0))
3634 set_value_range_to_value (&vr0, op0, NULL);
3635 else
3636 set_value_range_to_varying (&vr0);
3638 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3642 /* Extract range information from a conditional expression STMT based on
3643 the ranges of each of its operands and the expression code. */
3645 static void
3646 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3648 tree op0, op1;
3649 value_range vr0 = VR_INITIALIZER;
3650 value_range vr1 = VR_INITIALIZER;
3652 /* Get value ranges for each operand. For constant operands, create
3653 a new value range with the operand to simplify processing. */
3654 op0 = gimple_assign_rhs2 (stmt);
3655 if (TREE_CODE (op0) == SSA_NAME)
3656 vr0 = *(get_value_range (op0));
3657 else if (is_gimple_min_invariant (op0))
3658 set_value_range_to_value (&vr0, op0, NULL);
3659 else
3660 set_value_range_to_varying (&vr0);
3662 op1 = gimple_assign_rhs3 (stmt);
3663 if (TREE_CODE (op1) == SSA_NAME)
3664 vr1 = *(get_value_range (op1));
3665 else if (is_gimple_min_invariant (op1))
3666 set_value_range_to_value (&vr1, op1, NULL);
3667 else
3668 set_value_range_to_varying (&vr1);
3670 /* The resulting value range is the union of the operand ranges */
3671 copy_value_range (vr, &vr0);
3672 vrp_meet (vr, &vr1);
3676 /* Extract range information from a comparison expression EXPR based
3677 on the range of its operand and the expression code. */
3679 static void
3680 extract_range_from_comparison (value_range *vr, enum tree_code code,
3681 tree type, tree op0, tree op1)
3683 bool sop = false;
3684 tree val;
3686 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3687 NULL);
3689 /* A disadvantage of using a special infinity as an overflow
3690 representation is that we lose the ability to record overflow
3691 when we don't have an infinity. So we have to ignore a result
3692 which relies on overflow. */
3694 if (val && !is_overflow_infinity (val) && !sop)
3696 /* Since this expression was found on the RHS of an assignment,
3697 its type may be different from _Bool. Convert VAL to EXPR's
3698 type. */
3699 val = fold_convert (type, val);
3700 if (is_gimple_min_invariant (val))
3701 set_value_range_to_value (vr, val, vr->equiv);
3702 else
3703 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3705 else
3706 /* The result of a comparison is always true or false. */
3707 set_value_range_to_truthvalue (vr, type);
3710 /* Helper function for simplify_internal_call_using_ranges and
3711 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3712 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3713 always overflow. Set *OVF to true if it is known to always
3714 overflow. */
3716 static bool
3717 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3718 tree op0, tree op1, bool *ovf)
3720 value_range vr0 = VR_INITIALIZER;
3721 value_range vr1 = VR_INITIALIZER;
3722 if (TREE_CODE (op0) == SSA_NAME)
3723 vr0 = *get_value_range (op0);
3724 else if (TREE_CODE (op0) == INTEGER_CST)
3725 set_value_range_to_value (&vr0, op0, NULL);
3726 else
3727 set_value_range_to_varying (&vr0);
3729 if (TREE_CODE (op1) == SSA_NAME)
3730 vr1 = *get_value_range (op1);
3731 else if (TREE_CODE (op1) == INTEGER_CST)
3732 set_value_range_to_value (&vr1, op1, NULL);
3733 else
3734 set_value_range_to_varying (&vr1);
3736 if (!range_int_cst_p (&vr0)
3737 || TREE_OVERFLOW (vr0.min)
3738 || TREE_OVERFLOW (vr0.max))
3740 vr0.min = vrp_val_min (TREE_TYPE (op0));
3741 vr0.max = vrp_val_max (TREE_TYPE (op0));
3743 if (!range_int_cst_p (&vr1)
3744 || TREE_OVERFLOW (vr1.min)
3745 || TREE_OVERFLOW (vr1.max))
3747 vr1.min = vrp_val_min (TREE_TYPE (op1));
3748 vr1.max = vrp_val_max (TREE_TYPE (op1));
3750 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3751 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3752 if (arith_overflowed_p (subcode, type, vr0.max,
3753 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3754 return false;
3755 if (subcode == MULT_EXPR)
3757 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3758 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3759 return false;
3761 if (*ovf)
3763 /* So far we found that there is an overflow on the boundaries.
3764 That doesn't prove that there is an overflow even for all values
3765 in between the boundaries. For that compute widest_int range
3766 of the result and see if it doesn't overlap the range of
3767 type. */
3768 widest_int wmin, wmax;
3769 widest_int w[4];
3770 int i;
3771 w[0] = wi::to_widest (vr0.min);
3772 w[1] = wi::to_widest (vr0.max);
3773 w[2] = wi::to_widest (vr1.min);
3774 w[3] = wi::to_widest (vr1.max);
3775 for (i = 0; i < 4; i++)
3777 widest_int wt;
3778 switch (subcode)
3780 case PLUS_EXPR:
3781 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3782 break;
3783 case MINUS_EXPR:
3784 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3785 break;
3786 case MULT_EXPR:
3787 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3788 break;
3789 default:
3790 gcc_unreachable ();
3792 if (i == 0)
3794 wmin = wt;
3795 wmax = wt;
3797 else
3799 wmin = wi::smin (wmin, wt);
3800 wmax = wi::smax (wmax, wt);
3803 /* The result of op0 CODE op1 is known to be in range
3804 [wmin, wmax]. */
3805 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3806 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3807 /* If all values in [wmin, wmax] are smaller than
3808 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3809 the arithmetic operation will always overflow. */
3810 if (wi::lts_p (wmax, wtmin) || wi::gts_p (wmin, wtmax))
3811 return true;
3812 return false;
3814 return true;
3817 /* Try to derive a nonnegative or nonzero range out of STMT relying
3818 primarily on generic routines in fold in conjunction with range data.
3819 Store the result in *VR */
3821 static void
3822 extract_range_basic (value_range *vr, gimple *stmt)
3824 bool sop = false;
3825 tree type = gimple_expr_type (stmt);
3827 if (is_gimple_call (stmt))
3829 tree arg;
3830 int mini, maxi, zerov = 0, prec;
3831 enum tree_code subcode = ERROR_MARK;
3832 combined_fn cfn = gimple_call_combined_fn (stmt);
3834 switch (cfn)
3836 case CFN_BUILT_IN_CONSTANT_P:
3837 /* If the call is __builtin_constant_p and the argument is a
3838 function parameter resolve it to false. This avoids bogus
3839 array bound warnings.
3840 ??? We could do this as early as inlining is finished. */
3841 arg = gimple_call_arg (stmt, 0);
3842 if (TREE_CODE (arg) == SSA_NAME
3843 && SSA_NAME_IS_DEFAULT_DEF (arg)
3844 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3846 set_value_range_to_null (vr, type);
3847 return;
3849 break;
3850 /* Both __builtin_ffs* and __builtin_popcount return
3851 [0, prec]. */
3852 CASE_CFN_FFS:
3853 CASE_CFN_POPCOUNT:
3854 arg = gimple_call_arg (stmt, 0);
3855 prec = TYPE_PRECISION (TREE_TYPE (arg));
3856 mini = 0;
3857 maxi = prec;
3858 if (TREE_CODE (arg) == SSA_NAME)
3860 value_range *vr0 = get_value_range (arg);
3861 /* If arg is non-zero, then ffs or popcount
3862 are non-zero. */
3863 if (((vr0->type == VR_RANGE
3864 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3865 || (vr0->type == VR_ANTI_RANGE
3866 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3867 && !is_overflow_infinity (vr0->min)
3868 && !is_overflow_infinity (vr0->max))
3869 mini = 1;
3870 /* If some high bits are known to be zero,
3871 we can decrease the maximum. */
3872 if (vr0->type == VR_RANGE
3873 && TREE_CODE (vr0->max) == INTEGER_CST
3874 && !operand_less_p (vr0->min,
3875 build_zero_cst (TREE_TYPE (vr0->min)))
3876 && !is_overflow_infinity (vr0->max))
3877 maxi = tree_floor_log2 (vr0->max) + 1;
3879 goto bitop_builtin;
3880 /* __builtin_parity* returns [0, 1]. */
3881 CASE_CFN_PARITY:
3882 mini = 0;
3883 maxi = 1;
3884 goto bitop_builtin;
3885 /* __builtin_c[lt]z* return [0, prec-1], except for
3886 when the argument is 0, but that is undefined behavior.
3887 On many targets where the CLZ RTL or optab value is defined
3888 for 0 the value is prec, so include that in the range
3889 by default. */
3890 CASE_CFN_CLZ:
3891 arg = gimple_call_arg (stmt, 0);
3892 prec = TYPE_PRECISION (TREE_TYPE (arg));
3893 mini = 0;
3894 maxi = prec;
3895 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3896 != CODE_FOR_nothing
3897 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3898 zerov)
3899 /* Handle only the single common value. */
3900 && zerov != prec)
3901 /* Magic value to give up, unless vr0 proves
3902 arg is non-zero. */
3903 mini = -2;
3904 if (TREE_CODE (arg) == SSA_NAME)
3906 value_range *vr0 = get_value_range (arg);
3907 /* From clz of VR_RANGE minimum we can compute
3908 result maximum. */
3909 if (vr0->type == VR_RANGE
3910 && TREE_CODE (vr0->min) == INTEGER_CST
3911 && !is_overflow_infinity (vr0->min))
3913 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3914 if (maxi != prec)
3915 mini = 0;
3917 else if (vr0->type == VR_ANTI_RANGE
3918 && integer_zerop (vr0->min)
3919 && !is_overflow_infinity (vr0->min))
3921 maxi = prec - 1;
3922 mini = 0;
3924 if (mini == -2)
3925 break;
3926 /* From clz of VR_RANGE maximum we can compute
3927 result minimum. */
3928 if (vr0->type == VR_RANGE
3929 && TREE_CODE (vr0->max) == INTEGER_CST
3930 && !is_overflow_infinity (vr0->max))
3932 mini = prec - 1 - tree_floor_log2 (vr0->max);
3933 if (mini == prec)
3934 break;
3937 if (mini == -2)
3938 break;
3939 goto bitop_builtin;
3940 /* __builtin_ctz* return [0, prec-1], except for
3941 when the argument is 0, but that is undefined behavior.
3942 If there is a ctz optab for this mode and
3943 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3944 otherwise just assume 0 won't be seen. */
3945 CASE_CFN_CTZ:
3946 arg = gimple_call_arg (stmt, 0);
3947 prec = TYPE_PRECISION (TREE_TYPE (arg));
3948 mini = 0;
3949 maxi = prec - 1;
3950 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3951 != CODE_FOR_nothing
3952 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3953 zerov))
3955 /* Handle only the two common values. */
3956 if (zerov == -1)
3957 mini = -1;
3958 else if (zerov == prec)
3959 maxi = prec;
3960 else
3961 /* Magic value to give up, unless vr0 proves
3962 arg is non-zero. */
3963 mini = -2;
3965 if (TREE_CODE (arg) == SSA_NAME)
3967 value_range *vr0 = get_value_range (arg);
3968 /* If arg is non-zero, then use [0, prec - 1]. */
3969 if (((vr0->type == VR_RANGE
3970 && integer_nonzerop (vr0->min))
3971 || (vr0->type == VR_ANTI_RANGE
3972 && integer_zerop (vr0->min)))
3973 && !is_overflow_infinity (vr0->min))
3975 mini = 0;
3976 maxi = prec - 1;
3978 /* If some high bits are known to be zero,
3979 we can decrease the result maximum. */
3980 if (vr0->type == VR_RANGE
3981 && TREE_CODE (vr0->max) == INTEGER_CST
3982 && !is_overflow_infinity (vr0->max))
3984 maxi = tree_floor_log2 (vr0->max);
3985 /* For vr0 [0, 0] give up. */
3986 if (maxi == -1)
3987 break;
3990 if (mini == -2)
3991 break;
3992 goto bitop_builtin;
3993 /* __builtin_clrsb* returns [0, prec-1]. */
3994 CASE_CFN_CLRSB:
3995 arg = gimple_call_arg (stmt, 0);
3996 prec = TYPE_PRECISION (TREE_TYPE (arg));
3997 mini = 0;
3998 maxi = prec - 1;
3999 goto bitop_builtin;
4000 bitop_builtin:
4001 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
4002 build_int_cst (type, maxi), NULL);
4003 return;
4004 case CFN_UBSAN_CHECK_ADD:
4005 subcode = PLUS_EXPR;
4006 break;
4007 case CFN_UBSAN_CHECK_SUB:
4008 subcode = MINUS_EXPR;
4009 break;
4010 case CFN_UBSAN_CHECK_MUL:
4011 subcode = MULT_EXPR;
4012 break;
4013 case CFN_GOACC_DIM_SIZE:
4014 case CFN_GOACC_DIM_POS:
4015 /* Optimizing these two internal functions helps the loop
4016 optimizer eliminate outer comparisons. Size is [1,N]
4017 and pos is [0,N-1]. */
4019 bool is_pos = cfn == CFN_GOACC_DIM_POS;
4020 int axis = get_oacc_ifn_dim_arg (stmt);
4021 int size = get_oacc_fn_dim_size (current_function_decl, axis);
4023 if (!size)
4024 /* If it's dynamic, the backend might know a hardware
4025 limitation. */
4026 size = targetm.goacc.dim_limit (axis);
4028 tree type = TREE_TYPE (gimple_call_lhs (stmt));
4029 set_value_range (vr, VR_RANGE,
4030 build_int_cst (type, is_pos ? 0 : 1),
4031 size ? build_int_cst (type, size - is_pos)
4032 : vrp_val_max (type), NULL);
4034 return;
4035 default:
4036 break;
4038 if (subcode != ERROR_MARK)
4040 bool saved_flag_wrapv = flag_wrapv;
4041 /* Pretend the arithmetics is wrapping. If there is
4042 any overflow, we'll complain, but will actually do
4043 wrapping operation. */
4044 flag_wrapv = 1;
4045 extract_range_from_binary_expr (vr, subcode, type,
4046 gimple_call_arg (stmt, 0),
4047 gimple_call_arg (stmt, 1));
4048 flag_wrapv = saved_flag_wrapv;
4050 /* If for both arguments vrp_valueize returned non-NULL,
4051 this should have been already folded and if not, it
4052 wasn't folded because of overflow. Avoid removing the
4053 UBSAN_CHECK_* calls in that case. */
4054 if (vr->type == VR_RANGE
4055 && (vr->min == vr->max
4056 || operand_equal_p (vr->min, vr->max, 0)))
4057 set_value_range_to_varying (vr);
4058 return;
4061 /* Handle extraction of the two results (result of arithmetics and
4062 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4063 internal function. */
4064 else if (is_gimple_assign (stmt)
4065 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4066 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4067 && INTEGRAL_TYPE_P (type))
4069 enum tree_code code = gimple_assign_rhs_code (stmt);
4070 tree op = gimple_assign_rhs1 (stmt);
4071 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4073 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4074 if (is_gimple_call (g) && gimple_call_internal_p (g))
4076 enum tree_code subcode = ERROR_MARK;
4077 switch (gimple_call_internal_fn (g))
4079 case IFN_ADD_OVERFLOW:
4080 subcode = PLUS_EXPR;
4081 break;
4082 case IFN_SUB_OVERFLOW:
4083 subcode = MINUS_EXPR;
4084 break;
4085 case IFN_MUL_OVERFLOW:
4086 subcode = MULT_EXPR;
4087 break;
4088 default:
4089 break;
4091 if (subcode != ERROR_MARK)
4093 tree op0 = gimple_call_arg (g, 0);
4094 tree op1 = gimple_call_arg (g, 1);
4095 if (code == IMAGPART_EXPR)
4097 bool ovf = false;
4098 if (check_for_binary_op_overflow (subcode, type,
4099 op0, op1, &ovf))
4100 set_value_range_to_value (vr,
4101 build_int_cst (type, ovf),
4102 NULL);
4103 else
4104 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4105 build_int_cst (type, 1), NULL);
4107 else if (types_compatible_p (type, TREE_TYPE (op0))
4108 && types_compatible_p (type, TREE_TYPE (op1)))
4110 bool saved_flag_wrapv = flag_wrapv;
4111 /* Pretend the arithmetics is wrapping. If there is
4112 any overflow, IMAGPART_EXPR will be set. */
4113 flag_wrapv = 1;
4114 extract_range_from_binary_expr (vr, subcode, type,
4115 op0, op1);
4116 flag_wrapv = saved_flag_wrapv;
4118 else
4120 value_range vr0 = VR_INITIALIZER;
4121 value_range vr1 = VR_INITIALIZER;
4122 bool saved_flag_wrapv = flag_wrapv;
4123 /* Pretend the arithmetics is wrapping. If there is
4124 any overflow, IMAGPART_EXPR will be set. */
4125 flag_wrapv = 1;
4126 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4127 type, op0);
4128 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4129 type, op1);
4130 extract_range_from_binary_expr_1 (vr, subcode, type,
4131 &vr0, &vr1);
4132 flag_wrapv = saved_flag_wrapv;
4134 return;
4139 if (INTEGRAL_TYPE_P (type)
4140 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4141 set_value_range_to_nonnegative (vr, type,
4142 sop || stmt_overflow_infinity (stmt));
4143 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4144 && !sop)
4145 set_value_range_to_nonnull (vr, type);
4146 else
4147 set_value_range_to_varying (vr);
4151 /* Try to compute a useful range out of assignment STMT and store it
4152 in *VR. */
4154 static void
4155 extract_range_from_assignment (value_range *vr, gassign *stmt)
4157 enum tree_code code = gimple_assign_rhs_code (stmt);
4159 if (code == ASSERT_EXPR)
4160 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4161 else if (code == SSA_NAME)
4162 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4163 else if (TREE_CODE_CLASS (code) == tcc_binary)
4164 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4165 gimple_expr_type (stmt),
4166 gimple_assign_rhs1 (stmt),
4167 gimple_assign_rhs2 (stmt));
4168 else if (TREE_CODE_CLASS (code) == tcc_unary)
4169 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4170 gimple_expr_type (stmt),
4171 gimple_assign_rhs1 (stmt));
4172 else if (code == COND_EXPR)
4173 extract_range_from_cond_expr (vr, stmt);
4174 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4175 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4176 gimple_expr_type (stmt),
4177 gimple_assign_rhs1 (stmt),
4178 gimple_assign_rhs2 (stmt));
4179 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4180 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4181 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4182 else
4183 set_value_range_to_varying (vr);
4185 if (vr->type == VR_VARYING)
4186 extract_range_basic (vr, stmt);
4189 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4190 would be profitable to adjust VR using scalar evolution information
4191 for VAR. If so, update VR with the new limits. */
4193 static void
4194 adjust_range_with_scev (value_range *vr, struct loop *loop,
4195 gimple *stmt, tree var)
4197 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4198 enum ev_direction dir;
4200 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4201 better opportunities than a regular range, but I'm not sure. */
4202 if (vr->type == VR_ANTI_RANGE)
4203 return;
4205 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4207 /* Like in PR19590, scev can return a constant function. */
4208 if (is_gimple_min_invariant (chrec))
4210 set_value_range_to_value (vr, chrec, vr->equiv);
4211 return;
4214 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4215 return;
4217 init = initial_condition_in_loop_num (chrec, loop->num);
4218 tem = op_with_constant_singleton_value_range (init);
4219 if (tem)
4220 init = tem;
4221 step = evolution_part_in_loop_num (chrec, loop->num);
4222 tem = op_with_constant_singleton_value_range (step);
4223 if (tem)
4224 step = tem;
4226 /* If STEP is symbolic, we can't know whether INIT will be the
4227 minimum or maximum value in the range. Also, unless INIT is
4228 a simple expression, compare_values and possibly other functions
4229 in tree-vrp won't be able to handle it. */
4230 if (step == NULL_TREE
4231 || !is_gimple_min_invariant (step)
4232 || !valid_value_p (init))
4233 return;
4235 dir = scev_direction (chrec);
4236 if (/* Do not adjust ranges if we do not know whether the iv increases
4237 or decreases, ... */
4238 dir == EV_DIR_UNKNOWN
4239 /* ... or if it may wrap. */
4240 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4241 true))
4242 return;
4244 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4245 negative_overflow_infinity and positive_overflow_infinity,
4246 because we have concluded that the loop probably does not
4247 wrap. */
4249 type = TREE_TYPE (var);
4250 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4251 tmin = lower_bound_in_type (type, type);
4252 else
4253 tmin = TYPE_MIN_VALUE (type);
4254 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4255 tmax = upper_bound_in_type (type, type);
4256 else
4257 tmax = TYPE_MAX_VALUE (type);
4259 /* Try to use estimated number of iterations for the loop to constrain the
4260 final value in the evolution. */
4261 if (TREE_CODE (step) == INTEGER_CST
4262 && is_gimple_val (init)
4263 && (TREE_CODE (init) != SSA_NAME
4264 || get_value_range (init)->type == VR_RANGE))
4266 widest_int nit;
4268 /* We are only entering here for loop header PHI nodes, so using
4269 the number of latch executions is the correct thing to use. */
4270 if (max_loop_iterations (loop, &nit))
4272 value_range maxvr = VR_INITIALIZER;
4273 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4274 bool overflow;
4276 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4277 &overflow);
4278 /* If the multiplication overflowed we can't do a meaningful
4279 adjustment. Likewise if the result doesn't fit in the type
4280 of the induction variable. For a signed type we have to
4281 check whether the result has the expected signedness which
4282 is that of the step as number of iterations is unsigned. */
4283 if (!overflow
4284 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4285 && (sgn == UNSIGNED
4286 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4288 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4289 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4290 TREE_TYPE (init), init, tem);
4291 /* Likewise if the addition did. */
4292 if (maxvr.type == VR_RANGE)
4294 value_range initvr = VR_INITIALIZER;
4296 if (TREE_CODE (init) == SSA_NAME)
4297 initvr = *(get_value_range (init));
4298 else if (is_gimple_min_invariant (init))
4299 set_value_range_to_value (&initvr, init, NULL);
4300 else
4301 return;
4303 /* Check if init + nit * step overflows. Though we checked
4304 scev {init, step}_loop doesn't wrap, it is not enough
4305 because the loop may exit immediately. Overflow could
4306 happen in the plus expression in this case. */
4307 if ((dir == EV_DIR_DECREASES
4308 && (is_negative_overflow_infinity (maxvr.min)
4309 || compare_values (maxvr.min, initvr.min) != -1))
4310 || (dir == EV_DIR_GROWS
4311 && (is_positive_overflow_infinity (maxvr.max)
4312 || compare_values (maxvr.max, initvr.max) != 1)))
4313 return;
4315 tmin = maxvr.min;
4316 tmax = maxvr.max;
4322 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4324 min = tmin;
4325 max = tmax;
4327 /* For VARYING or UNDEFINED ranges, just about anything we get
4328 from scalar evolutions should be better. */
4330 if (dir == EV_DIR_DECREASES)
4331 max = init;
4332 else
4333 min = init;
4335 else if (vr->type == VR_RANGE)
4337 min = vr->min;
4338 max = vr->max;
4340 if (dir == EV_DIR_DECREASES)
4342 /* INIT is the maximum value. If INIT is lower than VR->MAX
4343 but no smaller than VR->MIN, set VR->MAX to INIT. */
4344 if (compare_values (init, max) == -1)
4345 max = init;
4347 /* According to the loop information, the variable does not
4348 overflow. If we think it does, probably because of an
4349 overflow due to arithmetic on a different INF value,
4350 reset now. */
4351 if (is_negative_overflow_infinity (min)
4352 || compare_values (min, tmin) == -1)
4353 min = tmin;
4356 else
4358 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4359 if (compare_values (init, min) == 1)
4360 min = init;
4362 if (is_positive_overflow_infinity (max)
4363 || compare_values (tmax, max) == -1)
4364 max = tmax;
4367 else
4368 return;
4370 /* If we just created an invalid range with the minimum
4371 greater than the maximum, we fail conservatively.
4372 This should happen only in unreachable
4373 parts of code, or for invalid programs. */
4374 if (compare_values (min, max) == 1
4375 || (is_negative_overflow_infinity (min)
4376 && is_positive_overflow_infinity (max)))
4377 return;
4379 /* Even for valid range info, sometimes overflow flag will leak in.
4380 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4381 drop them except for +-overflow_infinity which still need special
4382 handling in vrp pass. */
4383 if (TREE_OVERFLOW_P (min)
4384 && ! is_negative_overflow_infinity (min))
4385 min = drop_tree_overflow (min);
4386 if (TREE_OVERFLOW_P (max)
4387 && ! is_positive_overflow_infinity (max))
4388 max = drop_tree_overflow (max);
4390 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4394 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4396 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4397 all the values in the ranges.
4399 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4401 - Return NULL_TREE if it is not always possible to determine the
4402 value of the comparison.
4404 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4405 overflow infinity was used in the test. */
4408 static tree
4409 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4410 bool *strict_overflow_p)
4412 /* VARYING or UNDEFINED ranges cannot be compared. */
4413 if (vr0->type == VR_VARYING
4414 || vr0->type == VR_UNDEFINED
4415 || vr1->type == VR_VARYING
4416 || vr1->type == VR_UNDEFINED)
4417 return NULL_TREE;
4419 /* Anti-ranges need to be handled separately. */
4420 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4422 /* If both are anti-ranges, then we cannot compute any
4423 comparison. */
4424 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4425 return NULL_TREE;
4427 /* These comparisons are never statically computable. */
4428 if (comp == GT_EXPR
4429 || comp == GE_EXPR
4430 || comp == LT_EXPR
4431 || comp == LE_EXPR)
4432 return NULL_TREE;
4434 /* Equality can be computed only between a range and an
4435 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4436 if (vr0->type == VR_RANGE)
4438 /* To simplify processing, make VR0 the anti-range. */
4439 value_range *tmp = vr0;
4440 vr0 = vr1;
4441 vr1 = tmp;
4444 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4446 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4447 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4448 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4450 return NULL_TREE;
4453 if (!usable_range_p (vr0, strict_overflow_p)
4454 || !usable_range_p (vr1, strict_overflow_p))
4455 return NULL_TREE;
4457 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4458 operands around and change the comparison code. */
4459 if (comp == GT_EXPR || comp == GE_EXPR)
4461 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4462 std::swap (vr0, vr1);
4465 if (comp == EQ_EXPR)
4467 /* Equality may only be computed if both ranges represent
4468 exactly one value. */
4469 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4470 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4472 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4473 strict_overflow_p);
4474 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4475 strict_overflow_p);
4476 if (cmp_min == 0 && cmp_max == 0)
4477 return boolean_true_node;
4478 else if (cmp_min != -2 && cmp_max != -2)
4479 return boolean_false_node;
4481 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4482 else if (compare_values_warnv (vr0->min, vr1->max,
4483 strict_overflow_p) == 1
4484 || compare_values_warnv (vr1->min, vr0->max,
4485 strict_overflow_p) == 1)
4486 return boolean_false_node;
4488 return NULL_TREE;
4490 else if (comp == NE_EXPR)
4492 int cmp1, cmp2;
4494 /* If VR0 is completely to the left or completely to the right
4495 of VR1, they are always different. Notice that we need to
4496 make sure that both comparisons yield similar results to
4497 avoid comparing values that cannot be compared at
4498 compile-time. */
4499 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4500 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4501 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4502 return boolean_true_node;
4504 /* If VR0 and VR1 represent a single value and are identical,
4505 return false. */
4506 else if (compare_values_warnv (vr0->min, vr0->max,
4507 strict_overflow_p) == 0
4508 && compare_values_warnv (vr1->min, vr1->max,
4509 strict_overflow_p) == 0
4510 && compare_values_warnv (vr0->min, vr1->min,
4511 strict_overflow_p) == 0
4512 && compare_values_warnv (vr0->max, vr1->max,
4513 strict_overflow_p) == 0)
4514 return boolean_false_node;
4516 /* Otherwise, they may or may not be different. */
4517 else
4518 return NULL_TREE;
4520 else if (comp == LT_EXPR || comp == LE_EXPR)
4522 int tst;
4524 /* If VR0 is to the left of VR1, return true. */
4525 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4526 if ((comp == LT_EXPR && tst == -1)
4527 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4529 if (overflow_infinity_range_p (vr0)
4530 || overflow_infinity_range_p (vr1))
4531 *strict_overflow_p = true;
4532 return boolean_true_node;
4535 /* If VR0 is to the right of VR1, return false. */
4536 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4537 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4538 || (comp == LE_EXPR && tst == 1))
4540 if (overflow_infinity_range_p (vr0)
4541 || overflow_infinity_range_p (vr1))
4542 *strict_overflow_p = true;
4543 return boolean_false_node;
4546 /* Otherwise, we don't know. */
4547 return NULL_TREE;
4550 gcc_unreachable ();
4554 /* Given a value range VR, a value VAL and a comparison code COMP, return
4555 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4556 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4557 always returns false. Return NULL_TREE if it is not always
4558 possible to determine the value of the comparison. Also set
4559 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4560 infinity was used in the test. */
4562 static tree
4563 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4564 bool *strict_overflow_p)
4566 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4567 return NULL_TREE;
4569 /* Anti-ranges need to be handled separately. */
4570 if (vr->type == VR_ANTI_RANGE)
4572 /* For anti-ranges, the only predicates that we can compute at
4573 compile time are equality and inequality. */
4574 if (comp == GT_EXPR
4575 || comp == GE_EXPR
4576 || comp == LT_EXPR
4577 || comp == LE_EXPR)
4578 return NULL_TREE;
4580 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4581 if (value_inside_range (val, vr->min, vr->max) == 1)
4582 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4584 return NULL_TREE;
4587 if (!usable_range_p (vr, strict_overflow_p))
4588 return NULL_TREE;
4590 if (comp == EQ_EXPR)
4592 /* EQ_EXPR may only be computed if VR represents exactly
4593 one value. */
4594 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4596 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4597 if (cmp == 0)
4598 return boolean_true_node;
4599 else if (cmp == -1 || cmp == 1 || cmp == 2)
4600 return boolean_false_node;
4602 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4603 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4604 return boolean_false_node;
4606 return NULL_TREE;
4608 else if (comp == NE_EXPR)
4610 /* If VAL is not inside VR, then they are always different. */
4611 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4612 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4613 return boolean_true_node;
4615 /* If VR represents exactly one value equal to VAL, then return
4616 false. */
4617 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4618 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4619 return boolean_false_node;
4621 /* Otherwise, they may or may not be different. */
4622 return NULL_TREE;
4624 else if (comp == LT_EXPR || comp == LE_EXPR)
4626 int tst;
4628 /* If VR is to the left of VAL, return true. */
4629 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4630 if ((comp == LT_EXPR && tst == -1)
4631 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4633 if (overflow_infinity_range_p (vr))
4634 *strict_overflow_p = true;
4635 return boolean_true_node;
4638 /* If VR is to the right of VAL, return false. */
4639 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4640 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4641 || (comp == LE_EXPR && tst == 1))
4643 if (overflow_infinity_range_p (vr))
4644 *strict_overflow_p = true;
4645 return boolean_false_node;
4648 /* Otherwise, we don't know. */
4649 return NULL_TREE;
4651 else if (comp == GT_EXPR || comp == GE_EXPR)
4653 int tst;
4655 /* If VR is to the right of VAL, return true. */
4656 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4657 if ((comp == GT_EXPR && tst == 1)
4658 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4660 if (overflow_infinity_range_p (vr))
4661 *strict_overflow_p = true;
4662 return boolean_true_node;
4665 /* If VR is to the left of VAL, return false. */
4666 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4667 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4668 || (comp == GE_EXPR && tst == -1))
4670 if (overflow_infinity_range_p (vr))
4671 *strict_overflow_p = true;
4672 return boolean_false_node;
4675 /* Otherwise, we don't know. */
4676 return NULL_TREE;
4679 gcc_unreachable ();
4683 /* Debugging dumps. */
4685 void dump_value_range (FILE *, value_range *);
4686 void debug_value_range (value_range *);
4687 void dump_all_value_ranges (FILE *);
4688 void debug_all_value_ranges (void);
4689 void dump_vr_equiv (FILE *, bitmap);
4690 void debug_vr_equiv (bitmap);
4693 /* Dump value range VR to FILE. */
4695 void
4696 dump_value_range (FILE *file, value_range *vr)
4698 if (vr == NULL)
4699 fprintf (file, "[]");
4700 else if (vr->type == VR_UNDEFINED)
4701 fprintf (file, "UNDEFINED");
4702 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4704 tree type = TREE_TYPE (vr->min);
4706 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4708 if (is_negative_overflow_infinity (vr->min))
4709 fprintf (file, "-INF(OVF)");
4710 else if (INTEGRAL_TYPE_P (type)
4711 && !TYPE_UNSIGNED (type)
4712 && vrp_val_is_min (vr->min))
4713 fprintf (file, "-INF");
4714 else
4715 print_generic_expr (file, vr->min, 0);
4717 fprintf (file, ", ");
4719 if (is_positive_overflow_infinity (vr->max))
4720 fprintf (file, "+INF(OVF)");
4721 else if (INTEGRAL_TYPE_P (type)
4722 && vrp_val_is_max (vr->max))
4723 fprintf (file, "+INF");
4724 else
4725 print_generic_expr (file, vr->max, 0);
4727 fprintf (file, "]");
4729 if (vr->equiv)
4731 bitmap_iterator bi;
4732 unsigned i, c = 0;
4734 fprintf (file, " EQUIVALENCES: { ");
4736 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4738 print_generic_expr (file, ssa_name (i), 0);
4739 fprintf (file, " ");
4740 c++;
4743 fprintf (file, "} (%u elements)", c);
4746 else if (vr->type == VR_VARYING)
4747 fprintf (file, "VARYING");
4748 else
4749 fprintf (file, "INVALID RANGE");
4753 /* Dump value range VR to stderr. */
4755 DEBUG_FUNCTION void
4756 debug_value_range (value_range *vr)
4758 dump_value_range (stderr, vr);
4759 fprintf (stderr, "\n");
4763 /* Dump value ranges of all SSA_NAMEs to FILE. */
4765 void
4766 dump_all_value_ranges (FILE *file)
4768 size_t i;
4770 for (i = 0; i < num_vr_values; i++)
4772 if (vr_value[i])
4774 print_generic_expr (file, ssa_name (i), 0);
4775 fprintf (file, ": ");
4776 dump_value_range (file, vr_value[i]);
4777 fprintf (file, "\n");
4781 fprintf (file, "\n");
4785 /* Dump all value ranges to stderr. */
4787 DEBUG_FUNCTION void
4788 debug_all_value_ranges (void)
4790 dump_all_value_ranges (stderr);
4794 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4795 create a new SSA name N and return the assertion assignment
4796 'N = ASSERT_EXPR <V, V OP W>'. */
4798 static gimple *
4799 build_assert_expr_for (tree cond, tree v)
4801 tree a;
4802 gassign *assertion;
4804 gcc_assert (TREE_CODE (v) == SSA_NAME
4805 && COMPARISON_CLASS_P (cond));
4807 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4808 assertion = gimple_build_assign (NULL_TREE, a);
4810 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4811 operand of the ASSERT_EXPR. Create it so the new name and the old one
4812 are registered in the replacement table so that we can fix the SSA web
4813 after adding all the ASSERT_EXPRs. */
4814 create_new_def_for (v, assertion, NULL);
4816 return assertion;
4820 /* Return false if EXPR is a predicate expression involving floating
4821 point values. */
4823 static inline bool
4824 fp_predicate (gimple *stmt)
4826 GIMPLE_CHECK (stmt, GIMPLE_COND);
4828 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4831 /* If the range of values taken by OP can be inferred after STMT executes,
4832 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4833 describes the inferred range. Return true if a range could be
4834 inferred. */
4836 static bool
4837 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4839 *val_p = NULL_TREE;
4840 *comp_code_p = ERROR_MARK;
4842 /* Do not attempt to infer anything in names that flow through
4843 abnormal edges. */
4844 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4845 return false;
4847 /* Similarly, don't infer anything from statements that may throw
4848 exceptions. ??? Relax this requirement? */
4849 if (stmt_could_throw_p (stmt))
4850 return false;
4852 /* If STMT is the last statement of a basic block with no normal
4853 successors, there is no point inferring anything about any of its
4854 operands. We would not be able to find a proper insertion point
4855 for the assertion, anyway. */
4856 if (stmt_ends_bb_p (stmt))
4858 edge_iterator ei;
4859 edge e;
4861 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4862 if (!(e->flags & EDGE_ABNORMAL))
4863 break;
4864 if (e == NULL)
4865 return false;
4868 if (infer_nonnull_range (stmt, op))
4870 *val_p = build_int_cst (TREE_TYPE (op), 0);
4871 *comp_code_p = NE_EXPR;
4872 return true;
4875 return false;
4879 void dump_asserts_for (FILE *, tree);
4880 void debug_asserts_for (tree);
4881 void dump_all_asserts (FILE *);
4882 void debug_all_asserts (void);
4884 /* Dump all the registered assertions for NAME to FILE. */
4886 void
4887 dump_asserts_for (FILE *file, tree name)
4889 assert_locus *loc;
4891 fprintf (file, "Assertions to be inserted for ");
4892 print_generic_expr (file, name, 0);
4893 fprintf (file, "\n");
4895 loc = asserts_for[SSA_NAME_VERSION (name)];
4896 while (loc)
4898 fprintf (file, "\t");
4899 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4900 fprintf (file, "\n\tBB #%d", loc->bb->index);
4901 if (loc->e)
4903 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4904 loc->e->dest->index);
4905 dump_edge_info (file, loc->e, dump_flags, 0);
4907 fprintf (file, "\n\tPREDICATE: ");
4908 print_generic_expr (file, name, 0);
4909 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4910 print_generic_expr (file, loc->val, 0);
4911 fprintf (file, "\n\n");
4912 loc = loc->next;
4915 fprintf (file, "\n");
4919 /* Dump all the registered assertions for NAME to stderr. */
4921 DEBUG_FUNCTION void
4922 debug_asserts_for (tree name)
4924 dump_asserts_for (stderr, name);
4928 /* Dump all the registered assertions for all the names to FILE. */
4930 void
4931 dump_all_asserts (FILE *file)
4933 unsigned i;
4934 bitmap_iterator bi;
4936 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4937 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4938 dump_asserts_for (file, ssa_name (i));
4939 fprintf (file, "\n");
4943 /* Dump all the registered assertions for all the names to stderr. */
4945 DEBUG_FUNCTION void
4946 debug_all_asserts (void)
4948 dump_all_asserts (stderr);
4952 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4953 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4954 E->DEST, then register this location as a possible insertion point
4955 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4957 BB, E and SI provide the exact insertion point for the new
4958 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4959 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4960 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4961 must not be NULL. */
4963 static void
4964 register_new_assert_for (tree name, tree expr,
4965 enum tree_code comp_code,
4966 tree val,
4967 basic_block bb,
4968 edge e,
4969 gimple_stmt_iterator si)
4971 assert_locus *n, *loc, *last_loc;
4972 basic_block dest_bb;
4974 gcc_checking_assert (bb == NULL || e == NULL);
4976 if (e == NULL)
4977 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4978 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4980 /* Never build an assert comparing against an integer constant with
4981 TREE_OVERFLOW set. This confuses our undefined overflow warning
4982 machinery. */
4983 if (TREE_OVERFLOW_P (val))
4984 val = drop_tree_overflow (val);
4986 /* The new assertion A will be inserted at BB or E. We need to
4987 determine if the new location is dominated by a previously
4988 registered location for A. If we are doing an edge insertion,
4989 assume that A will be inserted at E->DEST. Note that this is not
4990 necessarily true.
4992 If E is a critical edge, it will be split. But even if E is
4993 split, the new block will dominate the same set of blocks that
4994 E->DEST dominates.
4996 The reverse, however, is not true, blocks dominated by E->DEST
4997 will not be dominated by the new block created to split E. So,
4998 if the insertion location is on a critical edge, we will not use
4999 the new location to move another assertion previously registered
5000 at a block dominated by E->DEST. */
5001 dest_bb = (bb) ? bb : e->dest;
5003 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5004 VAL at a block dominating DEST_BB, then we don't need to insert a new
5005 one. Similarly, if the same assertion already exists at a block
5006 dominated by DEST_BB and the new location is not on a critical
5007 edge, then update the existing location for the assertion (i.e.,
5008 move the assertion up in the dominance tree).
5010 Note, this is implemented as a simple linked list because there
5011 should not be more than a handful of assertions registered per
5012 name. If this becomes a performance problem, a table hashed by
5013 COMP_CODE and VAL could be implemented. */
5014 loc = asserts_for[SSA_NAME_VERSION (name)];
5015 last_loc = loc;
5016 while (loc)
5018 if (loc->comp_code == comp_code
5019 && (loc->val == val
5020 || operand_equal_p (loc->val, val, 0))
5021 && (loc->expr == expr
5022 || operand_equal_p (loc->expr, expr, 0)))
5024 /* If E is not a critical edge and DEST_BB
5025 dominates the existing location for the assertion, move
5026 the assertion up in the dominance tree by updating its
5027 location information. */
5028 if ((e == NULL || !EDGE_CRITICAL_P (e))
5029 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5031 loc->bb = dest_bb;
5032 loc->e = e;
5033 loc->si = si;
5034 return;
5038 /* Update the last node of the list and move to the next one. */
5039 last_loc = loc;
5040 loc = loc->next;
5043 /* If we didn't find an assertion already registered for
5044 NAME COMP_CODE VAL, add a new one at the end of the list of
5045 assertions associated with NAME. */
5046 n = XNEW (struct assert_locus);
5047 n->bb = dest_bb;
5048 n->e = e;
5049 n->si = si;
5050 n->comp_code = comp_code;
5051 n->val = val;
5052 n->expr = expr;
5053 n->next = NULL;
5055 if (last_loc)
5056 last_loc->next = n;
5057 else
5058 asserts_for[SSA_NAME_VERSION (name)] = n;
5060 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5063 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5064 Extract a suitable test code and value and store them into *CODE_P and
5065 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5067 If no extraction was possible, return FALSE, otherwise return TRUE.
5069 If INVERT is true, then we invert the result stored into *CODE_P. */
5071 static bool
5072 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5073 tree cond_op0, tree cond_op1,
5074 bool invert, enum tree_code *code_p,
5075 tree *val_p)
5077 enum tree_code comp_code;
5078 tree val;
5080 /* Otherwise, we have a comparison of the form NAME COMP VAL
5081 or VAL COMP NAME. */
5082 if (name == cond_op1)
5084 /* If the predicate is of the form VAL COMP NAME, flip
5085 COMP around because we need to register NAME as the
5086 first operand in the predicate. */
5087 comp_code = swap_tree_comparison (cond_code);
5088 val = cond_op0;
5090 else
5092 /* The comparison is of the form NAME COMP VAL, so the
5093 comparison code remains unchanged. */
5094 comp_code = cond_code;
5095 val = cond_op1;
5098 /* Invert the comparison code as necessary. */
5099 if (invert)
5100 comp_code = invert_tree_comparison (comp_code, 0);
5102 /* VRP only handles integral and pointer types. */
5103 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
5104 && ! POINTER_TYPE_P (TREE_TYPE (val)))
5105 return false;
5107 /* Do not register always-false predicates.
5108 FIXME: this works around a limitation in fold() when dealing with
5109 enumerations. Given 'enum { N1, N2 } x;', fold will not
5110 fold 'if (x > N2)' to 'if (0)'. */
5111 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5112 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5114 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5115 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5117 if (comp_code == GT_EXPR
5118 && (!max
5119 || compare_values (val, max) == 0))
5120 return false;
5122 if (comp_code == LT_EXPR
5123 && (!min
5124 || compare_values (val, min) == 0))
5125 return false;
5127 *code_p = comp_code;
5128 *val_p = val;
5129 return true;
5132 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5133 (otherwise return VAL). VAL and MASK must be zero-extended for
5134 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5135 (to transform signed values into unsigned) and at the end xor
5136 SGNBIT back. */
5138 static wide_int
5139 masked_increment (const wide_int &val_in, const wide_int &mask,
5140 const wide_int &sgnbit, unsigned int prec)
5142 wide_int bit = wi::one (prec), res;
5143 unsigned int i;
5145 wide_int val = val_in ^ sgnbit;
5146 for (i = 0; i < prec; i++, bit += bit)
5148 res = mask;
5149 if ((res & bit) == 0)
5150 continue;
5151 res = bit - 1;
5152 res = (val + bit).and_not (res);
5153 res &= mask;
5154 if (wi::gtu_p (res, val))
5155 return res ^ sgnbit;
5157 return val ^ sgnbit;
5160 /* Try to register an edge assertion for SSA name NAME on edge E for
5161 the condition COND contributing to the conditional jump pointed to by BSI.
5162 Invert the condition COND if INVERT is true. */
5164 static void
5165 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5166 enum tree_code cond_code,
5167 tree cond_op0, tree cond_op1, bool invert)
5169 tree val;
5170 enum tree_code comp_code;
5172 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5173 cond_op0,
5174 cond_op1,
5175 invert, &comp_code, &val))
5176 return;
5178 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5179 reachable from E. */
5180 if (live_on_edge (e, name)
5181 && !has_single_use (name))
5182 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5184 /* In the case of NAME <= CST and NAME being defined as
5185 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5186 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5187 This catches range and anti-range tests. */
5188 if ((comp_code == LE_EXPR
5189 || comp_code == GT_EXPR)
5190 && TREE_CODE (val) == INTEGER_CST
5191 && TYPE_UNSIGNED (TREE_TYPE (val)))
5193 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5194 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5196 /* Extract CST2 from the (optional) addition. */
5197 if (is_gimple_assign (def_stmt)
5198 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5200 name2 = gimple_assign_rhs1 (def_stmt);
5201 cst2 = gimple_assign_rhs2 (def_stmt);
5202 if (TREE_CODE (name2) == SSA_NAME
5203 && TREE_CODE (cst2) == INTEGER_CST)
5204 def_stmt = SSA_NAME_DEF_STMT (name2);
5207 /* Extract NAME2 from the (optional) sign-changing cast. */
5208 if (gimple_assign_cast_p (def_stmt))
5210 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5211 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5212 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5213 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5214 name3 = gimple_assign_rhs1 (def_stmt);
5217 /* If name3 is used later, create an ASSERT_EXPR for it. */
5218 if (name3 != NULL_TREE
5219 && TREE_CODE (name3) == SSA_NAME
5220 && (cst2 == NULL_TREE
5221 || TREE_CODE (cst2) == INTEGER_CST)
5222 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5223 && live_on_edge (e, name3)
5224 && !has_single_use (name3))
5226 tree tmp;
5228 /* Build an expression for the range test. */
5229 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5230 if (cst2 != NULL_TREE)
5231 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5233 if (dump_file)
5235 fprintf (dump_file, "Adding assert for ");
5236 print_generic_expr (dump_file, name3, 0);
5237 fprintf (dump_file, " from ");
5238 print_generic_expr (dump_file, tmp, 0);
5239 fprintf (dump_file, "\n");
5242 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5245 /* If name2 is used later, create an ASSERT_EXPR for it. */
5246 if (name2 != NULL_TREE
5247 && TREE_CODE (name2) == SSA_NAME
5248 && TREE_CODE (cst2) == INTEGER_CST
5249 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5250 && live_on_edge (e, name2)
5251 && !has_single_use (name2))
5253 tree tmp;
5255 /* Build an expression for the range test. */
5256 tmp = name2;
5257 if (TREE_TYPE (name) != TREE_TYPE (name2))
5258 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5259 if (cst2 != NULL_TREE)
5260 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5262 if (dump_file)
5264 fprintf (dump_file, "Adding assert for ");
5265 print_generic_expr (dump_file, name2, 0);
5266 fprintf (dump_file, " from ");
5267 print_generic_expr (dump_file, tmp, 0);
5268 fprintf (dump_file, "\n");
5271 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5275 /* In the case of post-in/decrement tests like if (i++) ... and uses
5276 of the in/decremented value on the edge the extra name we want to
5277 assert for is not on the def chain of the name compared. Instead
5278 it is in the set of use stmts.
5279 Similar cases happen for conversions that were simplified through
5280 fold_{sign_changed,widened}_comparison. */
5281 if ((comp_code == NE_EXPR
5282 || comp_code == EQ_EXPR)
5283 && TREE_CODE (val) == INTEGER_CST)
5285 imm_use_iterator ui;
5286 gimple *use_stmt;
5287 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5289 if (!is_gimple_assign (use_stmt))
5290 continue;
5292 /* Cut off to use-stmts that are dominating the predecessor. */
5293 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5294 continue;
5296 tree name2 = gimple_assign_lhs (use_stmt);
5297 if (TREE_CODE (name2) != SSA_NAME
5298 || !live_on_edge (e, name2))
5299 continue;
5301 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5302 tree cst;
5303 if (code == PLUS_EXPR
5304 || code == MINUS_EXPR)
5306 cst = gimple_assign_rhs2 (use_stmt);
5307 if (TREE_CODE (cst) != INTEGER_CST)
5308 continue;
5309 cst = int_const_binop (code, val, cst);
5311 else if (CONVERT_EXPR_CODE_P (code))
5313 /* For truncating conversions we cannot record
5314 an inequality. */
5315 if (comp_code == NE_EXPR
5316 && (TYPE_PRECISION (TREE_TYPE (name2))
5317 < TYPE_PRECISION (TREE_TYPE (name))))
5318 continue;
5319 cst = fold_convert (TREE_TYPE (name2), val);
5321 else
5322 continue;
5324 if (TREE_OVERFLOW_P (cst))
5325 cst = drop_tree_overflow (cst);
5326 register_new_assert_for (name2, name2, comp_code, cst,
5327 NULL, e, bsi);
5331 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5332 && TREE_CODE (val) == INTEGER_CST)
5334 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5335 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5336 tree val2 = NULL_TREE;
5337 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5338 wide_int mask = wi::zero (prec);
5339 unsigned int nprec = prec;
5340 enum tree_code rhs_code = ERROR_MARK;
5342 if (is_gimple_assign (def_stmt))
5343 rhs_code = gimple_assign_rhs_code (def_stmt);
5345 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5346 assert that A != CST1 -+ CST2. */
5347 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5348 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
5350 tree op0 = gimple_assign_rhs1 (def_stmt);
5351 tree op1 = gimple_assign_rhs2 (def_stmt);
5352 if (TREE_CODE (op0) == SSA_NAME
5353 && TREE_CODE (op1) == INTEGER_CST
5354 && live_on_edge (e, op0)
5355 && !has_single_use (op0))
5357 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
5358 ? MINUS_EXPR : PLUS_EXPR);
5359 op1 = int_const_binop (reverse_op, val, op1);
5360 if (TREE_OVERFLOW (op1))
5361 op1 = drop_tree_overflow (op1);
5362 register_new_assert_for (op0, op0, comp_code, op1, NULL, e, bsi);
5366 /* Add asserts for NAME cmp CST and NAME being defined
5367 as NAME = (int) NAME2. */
5368 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5369 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5370 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5371 && gimple_assign_cast_p (def_stmt))
5373 name2 = gimple_assign_rhs1 (def_stmt);
5374 if (CONVERT_EXPR_CODE_P (rhs_code)
5375 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5376 && TYPE_UNSIGNED (TREE_TYPE (name2))
5377 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5378 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5379 || !tree_int_cst_equal (val,
5380 TYPE_MIN_VALUE (TREE_TYPE (val))))
5381 && live_on_edge (e, name2)
5382 && !has_single_use (name2))
5384 tree tmp, cst;
5385 enum tree_code new_comp_code = comp_code;
5387 cst = fold_convert (TREE_TYPE (name2),
5388 TYPE_MIN_VALUE (TREE_TYPE (val)));
5389 /* Build an expression for the range test. */
5390 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5391 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5392 fold_convert (TREE_TYPE (name2), val));
5393 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5395 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5396 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5397 build_int_cst (TREE_TYPE (name2), 1));
5400 if (dump_file)
5402 fprintf (dump_file, "Adding assert for ");
5403 print_generic_expr (dump_file, name2, 0);
5404 fprintf (dump_file, " from ");
5405 print_generic_expr (dump_file, tmp, 0);
5406 fprintf (dump_file, "\n");
5409 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5410 e, bsi);
5414 /* Add asserts for NAME cmp CST and NAME being defined as
5415 NAME = NAME2 >> CST2.
5417 Extract CST2 from the right shift. */
5418 if (rhs_code == RSHIFT_EXPR)
5420 name2 = gimple_assign_rhs1 (def_stmt);
5421 cst2 = gimple_assign_rhs2 (def_stmt);
5422 if (TREE_CODE (name2) == SSA_NAME
5423 && tree_fits_uhwi_p (cst2)
5424 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5425 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5426 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5427 && live_on_edge (e, name2)
5428 && !has_single_use (name2))
5430 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5431 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5434 if (val2 != NULL_TREE
5435 && TREE_CODE (val2) == INTEGER_CST
5436 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5437 TREE_TYPE (val),
5438 val2, cst2), val))
5440 enum tree_code new_comp_code = comp_code;
5441 tree tmp, new_val;
5443 tmp = name2;
5444 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5446 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5448 tree type = build_nonstandard_integer_type (prec, 1);
5449 tmp = build1 (NOP_EXPR, type, name2);
5450 val2 = fold_convert (type, val2);
5452 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5453 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5454 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5456 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5458 wide_int minval
5459 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5460 new_val = val2;
5461 if (minval == new_val)
5462 new_val = NULL_TREE;
5464 else
5466 wide_int maxval
5467 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5468 mask |= val2;
5469 if (mask == maxval)
5470 new_val = NULL_TREE;
5471 else
5472 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5475 if (new_val)
5477 if (dump_file)
5479 fprintf (dump_file, "Adding assert for ");
5480 print_generic_expr (dump_file, name2, 0);
5481 fprintf (dump_file, " from ");
5482 print_generic_expr (dump_file, tmp, 0);
5483 fprintf (dump_file, "\n");
5486 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5487 NULL, e, bsi);
5491 /* Add asserts for NAME cmp CST and NAME being defined as
5492 NAME = NAME2 & CST2.
5494 Extract CST2 from the and.
5496 Also handle
5497 NAME = (unsigned) NAME2;
5498 casts where NAME's type is unsigned and has smaller precision
5499 than NAME2's type as if it was NAME = NAME2 & MASK. */
5500 names[0] = NULL_TREE;
5501 names[1] = NULL_TREE;
5502 cst2 = NULL_TREE;
5503 if (rhs_code == BIT_AND_EXPR
5504 || (CONVERT_EXPR_CODE_P (rhs_code)
5505 && INTEGRAL_TYPE_P (TREE_TYPE (val))
5506 && TYPE_UNSIGNED (TREE_TYPE (val))
5507 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5508 > prec))
5510 name2 = gimple_assign_rhs1 (def_stmt);
5511 if (rhs_code == BIT_AND_EXPR)
5512 cst2 = gimple_assign_rhs2 (def_stmt);
5513 else
5515 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5516 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5518 if (TREE_CODE (name2) == SSA_NAME
5519 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5520 && TREE_CODE (cst2) == INTEGER_CST
5521 && !integer_zerop (cst2)
5522 && (nprec > 1
5523 || TYPE_UNSIGNED (TREE_TYPE (val))))
5525 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5526 if (gimple_assign_cast_p (def_stmt2))
5528 names[1] = gimple_assign_rhs1 (def_stmt2);
5529 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5530 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5531 || (TYPE_PRECISION (TREE_TYPE (name2))
5532 != TYPE_PRECISION (TREE_TYPE (names[1])))
5533 || !live_on_edge (e, names[1])
5534 || has_single_use (names[1]))
5535 names[1] = NULL_TREE;
5537 if (live_on_edge (e, name2)
5538 && !has_single_use (name2))
5539 names[0] = name2;
5542 if (names[0] || names[1])
5544 wide_int minv, maxv, valv, cst2v;
5545 wide_int tem, sgnbit;
5546 bool valid_p = false, valn, cst2n;
5547 enum tree_code ccode = comp_code;
5549 valv = wide_int::from (val, nprec, UNSIGNED);
5550 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5551 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5552 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5553 /* If CST2 doesn't have most significant bit set,
5554 but VAL is negative, we have comparison like
5555 if ((x & 0x123) > -4) (always true). Just give up. */
5556 if (!cst2n && valn)
5557 ccode = ERROR_MARK;
5558 if (cst2n)
5559 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5560 else
5561 sgnbit = wi::zero (nprec);
5562 minv = valv & cst2v;
5563 switch (ccode)
5565 case EQ_EXPR:
5566 /* Minimum unsigned value for equality is VAL & CST2
5567 (should be equal to VAL, otherwise we probably should
5568 have folded the comparison into false) and
5569 maximum unsigned value is VAL | ~CST2. */
5570 maxv = valv | ~cst2v;
5571 valid_p = true;
5572 break;
5574 case NE_EXPR:
5575 tem = valv | ~cst2v;
5576 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5577 if (valv == 0)
5579 cst2n = false;
5580 sgnbit = wi::zero (nprec);
5581 goto gt_expr;
5583 /* If (VAL | ~CST2) is all ones, handle it as
5584 (X & CST2) < VAL. */
5585 if (tem == -1)
5587 cst2n = false;
5588 valn = false;
5589 sgnbit = wi::zero (nprec);
5590 goto lt_expr;
5592 if (!cst2n && wi::neg_p (cst2v))
5593 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5594 if (sgnbit != 0)
5596 if (valv == sgnbit)
5598 cst2n = true;
5599 valn = true;
5600 goto gt_expr;
5602 if (tem == wi::mask (nprec - 1, false, nprec))
5604 cst2n = true;
5605 goto lt_expr;
5607 if (!cst2n)
5608 sgnbit = wi::zero (nprec);
5610 break;
5612 case GE_EXPR:
5613 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5614 is VAL and maximum unsigned value is ~0. For signed
5615 comparison, if CST2 doesn't have most significant bit
5616 set, handle it similarly. If CST2 has MSB set,
5617 the minimum is the same, and maximum is ~0U/2. */
5618 if (minv != valv)
5620 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5621 VAL. */
5622 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5623 if (minv == valv)
5624 break;
5626 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5627 valid_p = true;
5628 break;
5630 case GT_EXPR:
5631 gt_expr:
5632 /* Find out smallest MINV where MINV > VAL
5633 && (MINV & CST2) == MINV, if any. If VAL is signed and
5634 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5635 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5636 if (minv == valv)
5637 break;
5638 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5639 valid_p = true;
5640 break;
5642 case LE_EXPR:
5643 /* Minimum unsigned value for <= is 0 and maximum
5644 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5645 Otherwise, find smallest VAL2 where VAL2 > VAL
5646 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5647 as maximum.
5648 For signed comparison, if CST2 doesn't have most
5649 significant bit set, handle it similarly. If CST2 has
5650 MSB set, the maximum is the same and minimum is INT_MIN. */
5651 if (minv == valv)
5652 maxv = valv;
5653 else
5655 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5656 if (maxv == valv)
5657 break;
5658 maxv -= 1;
5660 maxv |= ~cst2v;
5661 minv = sgnbit;
5662 valid_p = true;
5663 break;
5665 case LT_EXPR:
5666 lt_expr:
5667 /* Minimum unsigned value for < is 0 and maximum
5668 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5669 Otherwise, find smallest VAL2 where VAL2 > VAL
5670 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5671 as maximum.
5672 For signed comparison, if CST2 doesn't have most
5673 significant bit set, handle it similarly. If CST2 has
5674 MSB set, the maximum is the same and minimum is INT_MIN. */
5675 if (minv == valv)
5677 if (valv == sgnbit)
5678 break;
5679 maxv = valv;
5681 else
5683 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5684 if (maxv == valv)
5685 break;
5687 maxv -= 1;
5688 maxv |= ~cst2v;
5689 minv = sgnbit;
5690 valid_p = true;
5691 break;
5693 default:
5694 break;
5696 if (valid_p
5697 && (maxv - minv) != -1)
5699 tree tmp, new_val, type;
5700 int i;
5702 for (i = 0; i < 2; i++)
5703 if (names[i])
5705 wide_int maxv2 = maxv;
5706 tmp = names[i];
5707 type = TREE_TYPE (names[i]);
5708 if (!TYPE_UNSIGNED (type))
5710 type = build_nonstandard_integer_type (nprec, 1);
5711 tmp = build1 (NOP_EXPR, type, names[i]);
5713 if (minv != 0)
5715 tmp = build2 (PLUS_EXPR, type, tmp,
5716 wide_int_to_tree (type, -minv));
5717 maxv2 = maxv - minv;
5719 new_val = wide_int_to_tree (type, maxv2);
5721 if (dump_file)
5723 fprintf (dump_file, "Adding assert for ");
5724 print_generic_expr (dump_file, names[i], 0);
5725 fprintf (dump_file, " from ");
5726 print_generic_expr (dump_file, tmp, 0);
5727 fprintf (dump_file, "\n");
5730 register_new_assert_for (names[i], tmp, LE_EXPR,
5731 new_val, NULL, e, bsi);
5738 /* OP is an operand of a truth value expression which is known to have
5739 a particular value. Register any asserts for OP and for any
5740 operands in OP's defining statement.
5742 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5743 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5745 static void
5746 register_edge_assert_for_1 (tree op, enum tree_code code,
5747 edge e, gimple_stmt_iterator bsi)
5749 gimple *op_def;
5750 tree val;
5751 enum tree_code rhs_code;
5753 /* We only care about SSA_NAMEs. */
5754 if (TREE_CODE (op) != SSA_NAME)
5755 return;
5757 /* We know that OP will have a zero or nonzero value. If OP is used
5758 more than once go ahead and register an assert for OP. */
5759 if (live_on_edge (e, op)
5760 && !has_single_use (op))
5762 val = build_int_cst (TREE_TYPE (op), 0);
5763 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5766 /* Now look at how OP is set. If it's set from a comparison,
5767 a truth operation or some bit operations, then we may be able
5768 to register information about the operands of that assignment. */
5769 op_def = SSA_NAME_DEF_STMT (op);
5770 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5771 return;
5773 rhs_code = gimple_assign_rhs_code (op_def);
5775 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5777 bool invert = (code == EQ_EXPR ? true : false);
5778 tree op0 = gimple_assign_rhs1 (op_def);
5779 tree op1 = gimple_assign_rhs2 (op_def);
5781 if (TREE_CODE (op0) == SSA_NAME)
5782 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5783 if (TREE_CODE (op1) == SSA_NAME)
5784 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5786 else if ((code == NE_EXPR
5787 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5788 || (code == EQ_EXPR
5789 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5791 /* Recurse on each operand. */
5792 tree op0 = gimple_assign_rhs1 (op_def);
5793 tree op1 = gimple_assign_rhs2 (op_def);
5794 if (TREE_CODE (op0) == SSA_NAME
5795 && has_single_use (op0))
5796 register_edge_assert_for_1 (op0, code, e, bsi);
5797 if (TREE_CODE (op1) == SSA_NAME
5798 && has_single_use (op1))
5799 register_edge_assert_for_1 (op1, code, e, bsi);
5801 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5802 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5804 /* Recurse, flipping CODE. */
5805 code = invert_tree_comparison (code, false);
5806 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5808 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5810 /* Recurse through the copy. */
5811 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5813 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5815 /* Recurse through the type conversion, unless it is a narrowing
5816 conversion or conversion from non-integral type. */
5817 tree rhs = gimple_assign_rhs1 (op_def);
5818 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5819 && (TYPE_PRECISION (TREE_TYPE (rhs))
5820 <= TYPE_PRECISION (TREE_TYPE (op))))
5821 register_edge_assert_for_1 (rhs, code, e, bsi);
5825 /* Try to register an edge assertion for SSA name NAME on edge E for
5826 the condition COND contributing to the conditional jump pointed to by
5827 SI. */
5829 static void
5830 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5831 enum tree_code cond_code, tree cond_op0,
5832 tree cond_op1)
5834 tree val;
5835 enum tree_code comp_code;
5836 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5838 /* Do not attempt to infer anything in names that flow through
5839 abnormal edges. */
5840 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5841 return;
5843 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5844 cond_op0, cond_op1,
5845 is_else_edge,
5846 &comp_code, &val))
5847 return;
5849 /* Register ASSERT_EXPRs for name. */
5850 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5851 cond_op1, is_else_edge);
5854 /* If COND is effectively an equality test of an SSA_NAME against
5855 the value zero or one, then we may be able to assert values
5856 for SSA_NAMEs which flow into COND. */
5858 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5859 statement of NAME we can assert both operands of the BIT_AND_EXPR
5860 have nonzero value. */
5861 if (((comp_code == EQ_EXPR && integer_onep (val))
5862 || (comp_code == NE_EXPR && integer_zerop (val))))
5864 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5866 if (is_gimple_assign (def_stmt)
5867 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5869 tree op0 = gimple_assign_rhs1 (def_stmt);
5870 tree op1 = gimple_assign_rhs2 (def_stmt);
5871 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5872 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5876 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5877 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5878 have zero value. */
5879 if (((comp_code == EQ_EXPR && integer_zerop (val))
5880 || (comp_code == NE_EXPR && integer_onep (val))))
5882 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5884 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5885 necessarily zero value, or if type-precision is one. */
5886 if (is_gimple_assign (def_stmt)
5887 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5888 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5889 || comp_code == EQ_EXPR)))
5891 tree op0 = gimple_assign_rhs1 (def_stmt);
5892 tree op1 = gimple_assign_rhs2 (def_stmt);
5893 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5894 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5900 /* Determine whether the outgoing edges of BB should receive an
5901 ASSERT_EXPR for each of the operands of BB's LAST statement.
5902 The last statement of BB must be a COND_EXPR.
5904 If any of the sub-graphs rooted at BB have an interesting use of
5905 the predicate operands, an assert location node is added to the
5906 list of assertions for the corresponding operands. */
5908 static void
5909 find_conditional_asserts (basic_block bb, gcond *last)
5911 gimple_stmt_iterator bsi;
5912 tree op;
5913 edge_iterator ei;
5914 edge e;
5915 ssa_op_iter iter;
5917 bsi = gsi_for_stmt (last);
5919 /* Look for uses of the operands in each of the sub-graphs
5920 rooted at BB. We need to check each of the outgoing edges
5921 separately, so that we know what kind of ASSERT_EXPR to
5922 insert. */
5923 FOR_EACH_EDGE (e, ei, bb->succs)
5925 if (e->dest == bb)
5926 continue;
5928 /* Register the necessary assertions for each operand in the
5929 conditional predicate. */
5930 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5931 register_edge_assert_for (op, e, bsi,
5932 gimple_cond_code (last),
5933 gimple_cond_lhs (last),
5934 gimple_cond_rhs (last));
5938 struct case_info
5940 tree expr;
5941 basic_block bb;
5944 /* Compare two case labels sorting first by the destination bb index
5945 and then by the case value. */
5947 static int
5948 compare_case_labels (const void *p1, const void *p2)
5950 const struct case_info *ci1 = (const struct case_info *) p1;
5951 const struct case_info *ci2 = (const struct case_info *) p2;
5952 int idx1 = ci1->bb->index;
5953 int idx2 = ci2->bb->index;
5955 if (idx1 < idx2)
5956 return -1;
5957 else if (idx1 == idx2)
5959 /* Make sure the default label is first in a group. */
5960 if (!CASE_LOW (ci1->expr))
5961 return -1;
5962 else if (!CASE_LOW (ci2->expr))
5963 return 1;
5964 else
5965 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5966 CASE_LOW (ci2->expr));
5968 else
5969 return 1;
5972 /* Determine whether the outgoing edges of BB should receive an
5973 ASSERT_EXPR for each of the operands of BB's LAST statement.
5974 The last statement of BB must be a SWITCH_EXPR.
5976 If any of the sub-graphs rooted at BB have an interesting use of
5977 the predicate operands, an assert location node is added to the
5978 list of assertions for the corresponding operands. */
5980 static void
5981 find_switch_asserts (basic_block bb, gswitch *last)
5983 gimple_stmt_iterator bsi;
5984 tree op;
5985 edge e;
5986 struct case_info *ci;
5987 size_t n = gimple_switch_num_labels (last);
5988 #if GCC_VERSION >= 4000
5989 unsigned int idx;
5990 #else
5991 /* Work around GCC 3.4 bug (PR 37086). */
5992 volatile unsigned int idx;
5993 #endif
5995 bsi = gsi_for_stmt (last);
5996 op = gimple_switch_index (last);
5997 if (TREE_CODE (op) != SSA_NAME)
5998 return;
6000 /* Build a vector of case labels sorted by destination label. */
6001 ci = XNEWVEC (struct case_info, n);
6002 for (idx = 0; idx < n; ++idx)
6004 ci[idx].expr = gimple_switch_label (last, idx);
6005 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
6007 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
6009 for (idx = 0; idx < n; ++idx)
6011 tree min, max;
6012 tree cl = ci[idx].expr;
6013 basic_block cbb = ci[idx].bb;
6015 min = CASE_LOW (cl);
6016 max = CASE_HIGH (cl);
6018 /* If there are multiple case labels with the same destination
6019 we need to combine them to a single value range for the edge. */
6020 if (idx + 1 < n && cbb == ci[idx + 1].bb)
6022 /* Skip labels until the last of the group. */
6023 do {
6024 ++idx;
6025 } while (idx < n && cbb == ci[idx].bb);
6026 --idx;
6028 /* Pick up the maximum of the case label range. */
6029 if (CASE_HIGH (ci[idx].expr))
6030 max = CASE_HIGH (ci[idx].expr);
6031 else
6032 max = CASE_LOW (ci[idx].expr);
6035 /* Nothing to do if the range includes the default label until we
6036 can register anti-ranges. */
6037 if (min == NULL_TREE)
6038 continue;
6040 /* Find the edge to register the assert expr on. */
6041 e = find_edge (bb, cbb);
6043 /* Register the necessary assertions for the operand in the
6044 SWITCH_EXPR. */
6045 register_edge_assert_for (op, e, bsi,
6046 max ? GE_EXPR : EQ_EXPR,
6047 op, fold_convert (TREE_TYPE (op), min));
6048 if (max)
6049 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6050 fold_convert (TREE_TYPE (op), max));
6053 XDELETEVEC (ci);
6057 /* Traverse all the statements in block BB looking for statements that
6058 may generate useful assertions for the SSA names in their operand.
6059 If a statement produces a useful assertion A for name N_i, then the
6060 list of assertions already generated for N_i is scanned to
6061 determine if A is actually needed.
6063 If N_i already had the assertion A at a location dominating the
6064 current location, then nothing needs to be done. Otherwise, the
6065 new location for A is recorded instead.
6067 1- For every statement S in BB, all the variables used by S are
6068 added to bitmap FOUND_IN_SUBGRAPH.
6070 2- If statement S uses an operand N in a way that exposes a known
6071 value range for N, then if N was not already generated by an
6072 ASSERT_EXPR, create a new assert location for N. For instance,
6073 if N is a pointer and the statement dereferences it, we can
6074 assume that N is not NULL.
6076 3- COND_EXPRs are a special case of #2. We can derive range
6077 information from the predicate but need to insert different
6078 ASSERT_EXPRs for each of the sub-graphs rooted at the
6079 conditional block. If the last statement of BB is a conditional
6080 expression of the form 'X op Y', then
6082 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6084 b) If the conditional is the only entry point to the sub-graph
6085 corresponding to the THEN_CLAUSE, recurse into it. On
6086 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6087 an ASSERT_EXPR is added for the corresponding variable.
6089 c) Repeat step (b) on the ELSE_CLAUSE.
6091 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6093 For instance,
6095 if (a == 9)
6096 b = a;
6097 else
6098 b = c + 1;
6100 In this case, an assertion on the THEN clause is useful to
6101 determine that 'a' is always 9 on that edge. However, an assertion
6102 on the ELSE clause would be unnecessary.
6104 4- If BB does not end in a conditional expression, then we recurse
6105 into BB's dominator children.
6107 At the end of the recursive traversal, every SSA name will have a
6108 list of locations where ASSERT_EXPRs should be added. When a new
6109 location for name N is found, it is registered by calling
6110 register_new_assert_for. That function keeps track of all the
6111 registered assertions to prevent adding unnecessary assertions.
6112 For instance, if a pointer P_4 is dereferenced more than once in a
6113 dominator tree, only the location dominating all the dereference of
6114 P_4 will receive an ASSERT_EXPR. */
6116 static void
6117 find_assert_locations_1 (basic_block bb, sbitmap live)
6119 gimple *last;
6121 last = last_stmt (bb);
6123 /* If BB's last statement is a conditional statement involving integer
6124 operands, determine if we need to add ASSERT_EXPRs. */
6125 if (last
6126 && gimple_code (last) == GIMPLE_COND
6127 && !fp_predicate (last)
6128 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6129 find_conditional_asserts (bb, as_a <gcond *> (last));
6131 /* If BB's last statement is a switch statement involving integer
6132 operands, determine if we need to add ASSERT_EXPRs. */
6133 if (last
6134 && gimple_code (last) == GIMPLE_SWITCH
6135 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6136 find_switch_asserts (bb, as_a <gswitch *> (last));
6138 /* Traverse all the statements in BB marking used names and looking
6139 for statements that may infer assertions for their used operands. */
6140 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6141 gsi_prev (&si))
6143 gimple *stmt;
6144 tree op;
6145 ssa_op_iter i;
6147 stmt = gsi_stmt (si);
6149 if (is_gimple_debug (stmt))
6150 continue;
6152 /* See if we can derive an assertion for any of STMT's operands. */
6153 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6155 tree value;
6156 enum tree_code comp_code;
6158 /* If op is not live beyond this stmt, do not bother to insert
6159 asserts for it. */
6160 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6161 continue;
6163 /* If OP is used in such a way that we can infer a value
6164 range for it, and we don't find a previous assertion for
6165 it, create a new assertion location node for OP. */
6166 if (infer_value_range (stmt, op, &comp_code, &value))
6168 /* If we are able to infer a nonzero value range for OP,
6169 then walk backwards through the use-def chain to see if OP
6170 was set via a typecast.
6172 If so, then we can also infer a nonzero value range
6173 for the operand of the NOP_EXPR. */
6174 if (comp_code == NE_EXPR && integer_zerop (value))
6176 tree t = op;
6177 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6179 while (is_gimple_assign (def_stmt)
6180 && CONVERT_EXPR_CODE_P
6181 (gimple_assign_rhs_code (def_stmt))
6182 && TREE_CODE
6183 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6184 && POINTER_TYPE_P
6185 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6187 t = gimple_assign_rhs1 (def_stmt);
6188 def_stmt = SSA_NAME_DEF_STMT (t);
6190 /* Note we want to register the assert for the
6191 operand of the NOP_EXPR after SI, not after the
6192 conversion. */
6193 if (! has_single_use (t))
6194 register_new_assert_for (t, t, comp_code, value,
6195 bb, NULL, si);
6199 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6203 /* Update live. */
6204 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6205 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6206 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6207 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6210 /* Traverse all PHI nodes in BB, updating live. */
6211 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6212 gsi_next (&si))
6214 use_operand_p arg_p;
6215 ssa_op_iter i;
6216 gphi *phi = si.phi ();
6217 tree res = gimple_phi_result (phi);
6219 if (virtual_operand_p (res))
6220 continue;
6222 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6224 tree arg = USE_FROM_PTR (arg_p);
6225 if (TREE_CODE (arg) == SSA_NAME)
6226 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6229 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6233 /* Do an RPO walk over the function computing SSA name liveness
6234 on-the-fly and deciding on assert expressions to insert. */
6236 static void
6237 find_assert_locations (void)
6239 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6240 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6241 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6242 int rpo_cnt, i;
6244 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6245 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6246 for (i = 0; i < rpo_cnt; ++i)
6247 bb_rpo[rpo[i]] = i;
6249 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6250 the order we compute liveness and insert asserts we otherwise
6251 fail to insert asserts into the loop latch. */
6252 loop_p loop;
6253 FOR_EACH_LOOP (loop, 0)
6255 i = loop->latch->index;
6256 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6257 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6258 !gsi_end_p (gsi); gsi_next (&gsi))
6260 gphi *phi = gsi.phi ();
6261 if (virtual_operand_p (gimple_phi_result (phi)))
6262 continue;
6263 tree arg = gimple_phi_arg_def (phi, j);
6264 if (TREE_CODE (arg) == SSA_NAME)
6266 if (live[i] == NULL)
6268 live[i] = sbitmap_alloc (num_ssa_names);
6269 bitmap_clear (live[i]);
6271 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6276 for (i = rpo_cnt - 1; i >= 0; --i)
6278 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6279 edge e;
6280 edge_iterator ei;
6282 if (!live[rpo[i]])
6284 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6285 bitmap_clear (live[rpo[i]]);
6288 /* Process BB and update the live information with uses in
6289 this block. */
6290 find_assert_locations_1 (bb, live[rpo[i]]);
6292 /* Merge liveness into the predecessor blocks and free it. */
6293 if (!bitmap_empty_p (live[rpo[i]]))
6295 int pred_rpo = i;
6296 FOR_EACH_EDGE (e, ei, bb->preds)
6298 int pred = e->src->index;
6299 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6300 continue;
6302 if (!live[pred])
6304 live[pred] = sbitmap_alloc (num_ssa_names);
6305 bitmap_clear (live[pred]);
6307 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6309 if (bb_rpo[pred] < pred_rpo)
6310 pred_rpo = bb_rpo[pred];
6313 /* Record the RPO number of the last visited block that needs
6314 live information from this block. */
6315 last_rpo[rpo[i]] = pred_rpo;
6317 else
6319 sbitmap_free (live[rpo[i]]);
6320 live[rpo[i]] = NULL;
6323 /* We can free all successors live bitmaps if all their
6324 predecessors have been visited already. */
6325 FOR_EACH_EDGE (e, ei, bb->succs)
6326 if (last_rpo[e->dest->index] == i
6327 && live[e->dest->index])
6329 sbitmap_free (live[e->dest->index]);
6330 live[e->dest->index] = NULL;
6334 XDELETEVEC (rpo);
6335 XDELETEVEC (bb_rpo);
6336 XDELETEVEC (last_rpo);
6337 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6338 if (live[i])
6339 sbitmap_free (live[i]);
6340 XDELETEVEC (live);
6343 /* Create an ASSERT_EXPR for NAME and insert it in the location
6344 indicated by LOC. Return true if we made any edge insertions. */
6346 static bool
6347 process_assert_insertions_for (tree name, assert_locus *loc)
6349 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6350 gimple *stmt;
6351 tree cond;
6352 gimple *assert_stmt;
6353 edge_iterator ei;
6354 edge e;
6356 /* If we have X <=> X do not insert an assert expr for that. */
6357 if (loc->expr == loc->val)
6358 return false;
6360 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6361 assert_stmt = build_assert_expr_for (cond, name);
6362 if (loc->e)
6364 /* We have been asked to insert the assertion on an edge. This
6365 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6366 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6367 || (gimple_code (gsi_stmt (loc->si))
6368 == GIMPLE_SWITCH));
6370 gsi_insert_on_edge (loc->e, assert_stmt);
6371 return true;
6374 /* Otherwise, we can insert right after LOC->SI iff the
6375 statement must not be the last statement in the block. */
6376 stmt = gsi_stmt (loc->si);
6377 if (!stmt_ends_bb_p (stmt))
6379 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6380 return false;
6383 /* If STMT must be the last statement in BB, we can only insert new
6384 assertions on the non-abnormal edge out of BB. Note that since
6385 STMT is not control flow, there may only be one non-abnormal edge
6386 out of BB. */
6387 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6388 if (!(e->flags & EDGE_ABNORMAL))
6390 gsi_insert_on_edge (e, assert_stmt);
6391 return true;
6394 gcc_unreachable ();
6398 /* Process all the insertions registered for every name N_i registered
6399 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6400 found in ASSERTS_FOR[i]. */
6402 static void
6403 process_assert_insertions (void)
6405 unsigned i;
6406 bitmap_iterator bi;
6407 bool update_edges_p = false;
6408 int num_asserts = 0;
6410 if (dump_file && (dump_flags & TDF_DETAILS))
6411 dump_all_asserts (dump_file);
6413 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6415 assert_locus *loc = asserts_for[i];
6416 gcc_assert (loc);
6418 while (loc)
6420 assert_locus *next = loc->next;
6421 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6422 free (loc);
6423 loc = next;
6424 num_asserts++;
6428 if (update_edges_p)
6429 gsi_commit_edge_inserts ();
6431 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6432 num_asserts);
6436 /* Traverse the flowgraph looking for conditional jumps to insert range
6437 expressions. These range expressions are meant to provide information
6438 to optimizations that need to reason in terms of value ranges. They
6439 will not be expanded into RTL. For instance, given:
6441 x = ...
6442 y = ...
6443 if (x < y)
6444 y = x - 2;
6445 else
6446 x = y + 3;
6448 this pass will transform the code into:
6450 x = ...
6451 y = ...
6452 if (x < y)
6454 x = ASSERT_EXPR <x, x < y>
6455 y = x - 2
6457 else
6459 y = ASSERT_EXPR <y, x >= y>
6460 x = y + 3
6463 The idea is that once copy and constant propagation have run, other
6464 optimizations will be able to determine what ranges of values can 'x'
6465 take in different paths of the code, simply by checking the reaching
6466 definition of 'x'. */
6468 static void
6469 insert_range_assertions (void)
6471 need_assert_for = BITMAP_ALLOC (NULL);
6472 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6474 calculate_dominance_info (CDI_DOMINATORS);
6476 find_assert_locations ();
6477 if (!bitmap_empty_p (need_assert_for))
6479 process_assert_insertions ();
6480 update_ssa (TODO_update_ssa_no_phi);
6483 if (dump_file && (dump_flags & TDF_DETAILS))
6485 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6486 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6489 free (asserts_for);
6490 BITMAP_FREE (need_assert_for);
6493 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6494 and "struct" hacks. If VRP can determine that the
6495 array subscript is a constant, check if it is outside valid
6496 range. If the array subscript is a RANGE, warn if it is
6497 non-overlapping with valid range.
6498 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6500 static void
6501 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6503 value_range *vr = NULL;
6504 tree low_sub, up_sub;
6505 tree low_bound, up_bound, up_bound_p1;
6507 if (TREE_NO_WARNING (ref))
6508 return;
6510 low_sub = up_sub = TREE_OPERAND (ref, 1);
6511 up_bound = array_ref_up_bound (ref);
6513 /* Can not check flexible arrays. */
6514 if (!up_bound
6515 || TREE_CODE (up_bound) != INTEGER_CST)
6516 return;
6518 /* Accesses to trailing arrays via pointers may access storage
6519 beyond the types array bounds. */
6520 if (warn_array_bounds < 2
6521 && array_at_struct_end_p (ref))
6522 return;
6524 low_bound = array_ref_low_bound (ref);
6525 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6526 build_int_cst (TREE_TYPE (up_bound), 1));
6528 /* Empty array. */
6529 if (tree_int_cst_equal (low_bound, up_bound_p1))
6531 warning_at (location, OPT_Warray_bounds,
6532 "array subscript is above array bounds");
6533 TREE_NO_WARNING (ref) = 1;
6536 if (TREE_CODE (low_sub) == SSA_NAME)
6538 vr = get_value_range (low_sub);
6539 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6541 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6542 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6546 if (vr && vr->type == VR_ANTI_RANGE)
6548 if (TREE_CODE (up_sub) == INTEGER_CST
6549 && (ignore_off_by_one
6550 ? tree_int_cst_lt (up_bound, up_sub)
6551 : tree_int_cst_le (up_bound, up_sub))
6552 && TREE_CODE (low_sub) == INTEGER_CST
6553 && tree_int_cst_le (low_sub, low_bound))
6555 warning_at (location, OPT_Warray_bounds,
6556 "array subscript is outside array bounds");
6557 TREE_NO_WARNING (ref) = 1;
6560 else if (TREE_CODE (up_sub) == INTEGER_CST
6561 && (ignore_off_by_one
6562 ? !tree_int_cst_le (up_sub, up_bound_p1)
6563 : !tree_int_cst_le (up_sub, up_bound)))
6565 if (dump_file && (dump_flags & TDF_DETAILS))
6567 fprintf (dump_file, "Array bound warning for ");
6568 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6569 fprintf (dump_file, "\n");
6571 warning_at (location, OPT_Warray_bounds,
6572 "array subscript is above array bounds");
6573 TREE_NO_WARNING (ref) = 1;
6575 else if (TREE_CODE (low_sub) == INTEGER_CST
6576 && tree_int_cst_lt (low_sub, low_bound))
6578 if (dump_file && (dump_flags & TDF_DETAILS))
6580 fprintf (dump_file, "Array bound warning for ");
6581 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6582 fprintf (dump_file, "\n");
6584 warning_at (location, OPT_Warray_bounds,
6585 "array subscript is below array bounds");
6586 TREE_NO_WARNING (ref) = 1;
6590 /* Searches if the expr T, located at LOCATION computes
6591 address of an ARRAY_REF, and call check_array_ref on it. */
6593 static void
6594 search_for_addr_array (tree t, location_t location)
6596 /* Check each ARRAY_REFs in the reference chain. */
6599 if (TREE_CODE (t) == ARRAY_REF)
6600 check_array_ref (location, t, true /*ignore_off_by_one*/);
6602 t = TREE_OPERAND (t, 0);
6604 while (handled_component_p (t));
6606 if (TREE_CODE (t) == MEM_REF
6607 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6608 && !TREE_NO_WARNING (t))
6610 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6611 tree low_bound, up_bound, el_sz;
6612 offset_int idx;
6613 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6614 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6615 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6616 return;
6618 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6619 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6620 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6621 if (!low_bound
6622 || TREE_CODE (low_bound) != INTEGER_CST
6623 || !up_bound
6624 || TREE_CODE (up_bound) != INTEGER_CST
6625 || !el_sz
6626 || TREE_CODE (el_sz) != INTEGER_CST)
6627 return;
6629 idx = mem_ref_offset (t);
6630 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6631 if (wi::lts_p (idx, 0))
6633 if (dump_file && (dump_flags & TDF_DETAILS))
6635 fprintf (dump_file, "Array bound warning for ");
6636 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6637 fprintf (dump_file, "\n");
6639 warning_at (location, OPT_Warray_bounds,
6640 "array subscript is below array bounds");
6641 TREE_NO_WARNING (t) = 1;
6643 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6644 - wi::to_offset (low_bound) + 1)))
6646 if (dump_file && (dump_flags & TDF_DETAILS))
6648 fprintf (dump_file, "Array bound warning for ");
6649 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6650 fprintf (dump_file, "\n");
6652 warning_at (location, OPT_Warray_bounds,
6653 "array subscript is above array bounds");
6654 TREE_NO_WARNING (t) = 1;
6659 /* walk_tree() callback that checks if *TP is
6660 an ARRAY_REF inside an ADDR_EXPR (in which an array
6661 subscript one outside the valid range is allowed). Call
6662 check_array_ref for each ARRAY_REF found. The location is
6663 passed in DATA. */
6665 static tree
6666 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6668 tree t = *tp;
6669 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6670 location_t location;
6672 if (EXPR_HAS_LOCATION (t))
6673 location = EXPR_LOCATION (t);
6674 else
6676 location_t *locp = (location_t *) wi->info;
6677 location = *locp;
6680 *walk_subtree = TRUE;
6682 if (TREE_CODE (t) == ARRAY_REF)
6683 check_array_ref (location, t, false /*ignore_off_by_one*/);
6685 else if (TREE_CODE (t) == ADDR_EXPR)
6687 search_for_addr_array (t, location);
6688 *walk_subtree = FALSE;
6691 return NULL_TREE;
6694 /* Walk over all statements of all reachable BBs and call check_array_bounds
6695 on them. */
6697 static void
6698 check_all_array_refs (void)
6700 basic_block bb;
6701 gimple_stmt_iterator si;
6703 FOR_EACH_BB_FN (bb, cfun)
6705 edge_iterator ei;
6706 edge e;
6707 bool executable = false;
6709 /* Skip blocks that were found to be unreachable. */
6710 FOR_EACH_EDGE (e, ei, bb->preds)
6711 executable |= !!(e->flags & EDGE_EXECUTABLE);
6712 if (!executable)
6713 continue;
6715 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6717 gimple *stmt = gsi_stmt (si);
6718 struct walk_stmt_info wi;
6719 if (!gimple_has_location (stmt)
6720 || is_gimple_debug (stmt))
6721 continue;
6723 memset (&wi, 0, sizeof (wi));
6725 location_t loc = gimple_location (stmt);
6726 wi.info = &loc;
6728 walk_gimple_op (gsi_stmt (si),
6729 check_array_bounds,
6730 &wi);
6735 /* Return true if all imm uses of VAR are either in STMT, or
6736 feed (optionally through a chain of single imm uses) GIMPLE_COND
6737 in basic block COND_BB. */
6739 static bool
6740 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6742 use_operand_p use_p, use2_p;
6743 imm_use_iterator iter;
6745 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6746 if (USE_STMT (use_p) != stmt)
6748 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6749 if (is_gimple_debug (use_stmt))
6750 continue;
6751 while (is_gimple_assign (use_stmt)
6752 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6753 && single_imm_use (gimple_assign_lhs (use_stmt),
6754 &use2_p, &use_stmt2))
6755 use_stmt = use_stmt2;
6756 if (gimple_code (use_stmt) != GIMPLE_COND
6757 || gimple_bb (use_stmt) != cond_bb)
6758 return false;
6760 return true;
6763 /* Handle
6764 _4 = x_3 & 31;
6765 if (_4 != 0)
6766 goto <bb 6>;
6767 else
6768 goto <bb 7>;
6769 <bb 6>:
6770 __builtin_unreachable ();
6771 <bb 7>:
6772 x_5 = ASSERT_EXPR <x_3, ...>;
6773 If x_3 has no other immediate uses (checked by caller),
6774 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6775 from the non-zero bitmask. */
6777 static void
6778 maybe_set_nonzero_bits (basic_block bb, tree var)
6780 edge e = single_pred_edge (bb);
6781 basic_block cond_bb = e->src;
6782 gimple *stmt = last_stmt (cond_bb);
6783 tree cst;
6785 if (stmt == NULL
6786 || gimple_code (stmt) != GIMPLE_COND
6787 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6788 ? EQ_EXPR : NE_EXPR)
6789 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6790 || !integer_zerop (gimple_cond_rhs (stmt)))
6791 return;
6793 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6794 if (!is_gimple_assign (stmt)
6795 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6796 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6797 return;
6798 if (gimple_assign_rhs1 (stmt) != var)
6800 gimple *stmt2;
6802 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6803 return;
6804 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6805 if (!gimple_assign_cast_p (stmt2)
6806 || gimple_assign_rhs1 (stmt2) != var
6807 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6808 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6809 != TYPE_PRECISION (TREE_TYPE (var))))
6810 return;
6812 cst = gimple_assign_rhs2 (stmt);
6813 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6816 /* Convert range assertion expressions into the implied copies and
6817 copy propagate away the copies. Doing the trivial copy propagation
6818 here avoids the need to run the full copy propagation pass after
6819 VRP.
6821 FIXME, this will eventually lead to copy propagation removing the
6822 names that had useful range information attached to them. For
6823 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6824 then N_i will have the range [3, +INF].
6826 However, by converting the assertion into the implied copy
6827 operation N_i = N_j, we will then copy-propagate N_j into the uses
6828 of N_i and lose the range information. We may want to hold on to
6829 ASSERT_EXPRs a little while longer as the ranges could be used in
6830 things like jump threading.
6832 The problem with keeping ASSERT_EXPRs around is that passes after
6833 VRP need to handle them appropriately.
6835 Another approach would be to make the range information a first
6836 class property of the SSA_NAME so that it can be queried from
6837 any pass. This is made somewhat more complex by the need for
6838 multiple ranges to be associated with one SSA_NAME. */
6840 static void
6841 remove_range_assertions (void)
6843 basic_block bb;
6844 gimple_stmt_iterator si;
6845 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6846 a basic block preceeded by GIMPLE_COND branching to it and
6847 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6848 int is_unreachable;
6850 /* Note that the BSI iterator bump happens at the bottom of the
6851 loop and no bump is necessary if we're removing the statement
6852 referenced by the current BSI. */
6853 FOR_EACH_BB_FN (bb, cfun)
6854 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6856 gimple *stmt = gsi_stmt (si);
6857 gimple *use_stmt;
6859 if (is_gimple_assign (stmt)
6860 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6862 tree lhs = gimple_assign_lhs (stmt);
6863 tree rhs = gimple_assign_rhs1 (stmt);
6864 tree var;
6865 use_operand_p use_p;
6866 imm_use_iterator iter;
6868 var = ASSERT_EXPR_VAR (rhs);
6869 gcc_assert (TREE_CODE (var) == SSA_NAME);
6871 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6872 && SSA_NAME_RANGE_INFO (lhs))
6874 if (is_unreachable == -1)
6876 is_unreachable = 0;
6877 if (single_pred_p (bb)
6878 && assert_unreachable_fallthru_edge_p
6879 (single_pred_edge (bb)))
6880 is_unreachable = 1;
6882 /* Handle
6883 if (x_7 >= 10 && x_7 < 20)
6884 __builtin_unreachable ();
6885 x_8 = ASSERT_EXPR <x_7, ...>;
6886 if the only uses of x_7 are in the ASSERT_EXPR and
6887 in the condition. In that case, we can copy the
6888 range info from x_8 computed in this pass also
6889 for x_7. */
6890 if (is_unreachable
6891 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6892 single_pred (bb)))
6894 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6895 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6896 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6897 maybe_set_nonzero_bits (bb, var);
6901 /* Propagate the RHS into every use of the LHS. */
6902 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6903 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6904 SET_USE (use_p, var);
6906 /* And finally, remove the copy, it is not needed. */
6907 gsi_remove (&si, true);
6908 release_defs (stmt);
6910 else
6912 if (!is_gimple_debug (gsi_stmt (si)))
6913 is_unreachable = 0;
6914 gsi_next (&si);
6920 /* Return true if STMT is interesting for VRP. */
6922 static bool
6923 stmt_interesting_for_vrp (gimple *stmt)
6925 if (gimple_code (stmt) == GIMPLE_PHI)
6927 tree res = gimple_phi_result (stmt);
6928 return (!virtual_operand_p (res)
6929 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6930 || POINTER_TYPE_P (TREE_TYPE (res))));
6932 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6934 tree lhs = gimple_get_lhs (stmt);
6936 /* In general, assignments with virtual operands are not useful
6937 for deriving ranges, with the obvious exception of calls to
6938 builtin functions. */
6939 if (lhs && TREE_CODE (lhs) == SSA_NAME
6940 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6941 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6942 && (is_gimple_call (stmt)
6943 || !gimple_vuse (stmt)))
6944 return true;
6945 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6946 switch (gimple_call_internal_fn (stmt))
6948 case IFN_ADD_OVERFLOW:
6949 case IFN_SUB_OVERFLOW:
6950 case IFN_MUL_OVERFLOW:
6951 /* These internal calls return _Complex integer type,
6952 but are interesting to VRP nevertheless. */
6953 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6954 return true;
6955 break;
6956 default:
6957 break;
6960 else if (gimple_code (stmt) == GIMPLE_COND
6961 || gimple_code (stmt) == GIMPLE_SWITCH)
6962 return true;
6964 return false;
6968 /* Initialize local data structures for VRP. */
6970 static void
6971 vrp_initialize (void)
6973 basic_block bb;
6975 values_propagated = false;
6976 num_vr_values = num_ssa_names;
6977 vr_value = XCNEWVEC (value_range *, num_vr_values);
6978 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6980 FOR_EACH_BB_FN (bb, cfun)
6982 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6983 gsi_next (&si))
6985 gphi *phi = si.phi ();
6986 if (!stmt_interesting_for_vrp (phi))
6988 tree lhs = PHI_RESULT (phi);
6989 set_value_range_to_varying (get_value_range (lhs));
6990 prop_set_simulate_again (phi, false);
6992 else
6993 prop_set_simulate_again (phi, true);
6996 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
6997 gsi_next (&si))
6999 gimple *stmt = gsi_stmt (si);
7001 /* If the statement is a control insn, then we do not
7002 want to avoid simulating the statement once. Failure
7003 to do so means that those edges will never get added. */
7004 if (stmt_ends_bb_p (stmt))
7005 prop_set_simulate_again (stmt, true);
7006 else if (!stmt_interesting_for_vrp (stmt))
7008 set_defs_to_varying (stmt);
7009 prop_set_simulate_again (stmt, false);
7011 else
7012 prop_set_simulate_again (stmt, true);
7017 /* Return the singleton value-range for NAME or NAME. */
7019 static inline tree
7020 vrp_valueize (tree name)
7022 if (TREE_CODE (name) == SSA_NAME)
7024 value_range *vr = get_value_range (name);
7025 if (vr->type == VR_RANGE
7026 && vrp_operand_equal_p (vr->min, vr->max))
7027 return vr->min;
7029 return name;
7032 /* Return the singleton value-range for NAME if that is a constant
7033 but signal to not follow SSA edges. */
7035 static inline tree
7036 vrp_valueize_1 (tree name)
7038 if (TREE_CODE (name) == SSA_NAME)
7040 /* If the definition may be simulated again we cannot follow
7041 this SSA edge as the SSA propagator does not necessarily
7042 re-visit the use. */
7043 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
7044 if (!gimple_nop_p (def_stmt)
7045 && prop_simulate_again_p (def_stmt))
7046 return NULL_TREE;
7047 value_range *vr = get_value_range (name);
7048 if (range_int_cst_singleton_p (vr))
7049 return vr->min;
7051 return name;
7054 /* Visit assignment STMT. If it produces an interesting range, record
7055 the SSA name in *OUTPUT_P. */
7057 static enum ssa_prop_result
7058 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p)
7060 tree def, lhs;
7061 ssa_op_iter iter;
7062 enum gimple_code code = gimple_code (stmt);
7063 lhs = gimple_get_lhs (stmt);
7065 /* We only keep track of ranges in integral and pointer types. */
7066 if (TREE_CODE (lhs) == SSA_NAME
7067 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7068 /* It is valid to have NULL MIN/MAX values on a type. See
7069 build_range_type. */
7070 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7071 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7072 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7074 value_range new_vr = VR_INITIALIZER;
7076 /* Try folding the statement to a constant first. */
7077 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7078 vrp_valueize_1);
7079 if (tem && is_gimple_min_invariant (tem))
7080 set_value_range_to_value (&new_vr, tem, NULL);
7081 /* Then dispatch to value-range extracting functions. */
7082 else if (code == GIMPLE_CALL)
7083 extract_range_basic (&new_vr, stmt);
7084 else
7085 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7087 if (update_value_range (lhs, &new_vr))
7089 *output_p = lhs;
7091 if (dump_file && (dump_flags & TDF_DETAILS))
7093 fprintf (dump_file, "Found new range for ");
7094 print_generic_expr (dump_file, lhs, 0);
7095 fprintf (dump_file, ": ");
7096 dump_value_range (dump_file, &new_vr);
7097 fprintf (dump_file, "\n");
7100 if (new_vr.type == VR_VARYING)
7101 return SSA_PROP_VARYING;
7103 return SSA_PROP_INTERESTING;
7106 return SSA_PROP_NOT_INTERESTING;
7108 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7109 switch (gimple_call_internal_fn (stmt))
7111 case IFN_ADD_OVERFLOW:
7112 case IFN_SUB_OVERFLOW:
7113 case IFN_MUL_OVERFLOW:
7114 /* These internal calls return _Complex integer type,
7115 which VRP does not track, but the immediate uses
7116 thereof might be interesting. */
7117 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7119 imm_use_iterator iter;
7120 use_operand_p use_p;
7121 enum ssa_prop_result res = SSA_PROP_VARYING;
7123 set_value_range_to_varying (get_value_range (lhs));
7125 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7127 gimple *use_stmt = USE_STMT (use_p);
7128 if (!is_gimple_assign (use_stmt))
7129 continue;
7130 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7131 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7132 continue;
7133 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7134 tree use_lhs = gimple_assign_lhs (use_stmt);
7135 if (TREE_CODE (rhs1) != rhs_code
7136 || TREE_OPERAND (rhs1, 0) != lhs
7137 || TREE_CODE (use_lhs) != SSA_NAME
7138 || !stmt_interesting_for_vrp (use_stmt)
7139 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7140 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7141 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7142 continue;
7144 /* If there is a change in the value range for any of the
7145 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7146 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7147 or IMAGPART_EXPR immediate uses, but none of them have
7148 a change in their value ranges, return
7149 SSA_PROP_NOT_INTERESTING. If there are no
7150 {REAL,IMAG}PART_EXPR uses at all,
7151 return SSA_PROP_VARYING. */
7152 value_range new_vr = VR_INITIALIZER;
7153 extract_range_basic (&new_vr, use_stmt);
7154 value_range *old_vr = get_value_range (use_lhs);
7155 if (old_vr->type != new_vr.type
7156 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7157 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7158 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7159 res = SSA_PROP_INTERESTING;
7160 else
7161 res = SSA_PROP_NOT_INTERESTING;
7162 BITMAP_FREE (new_vr.equiv);
7163 if (res == SSA_PROP_INTERESTING)
7165 *output_p = lhs;
7166 return res;
7170 return res;
7172 break;
7173 default:
7174 break;
7177 /* Every other statement produces no useful ranges. */
7178 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7179 set_value_range_to_varying (get_value_range (def));
7181 return SSA_PROP_VARYING;
7184 /* Helper that gets the value range of the SSA_NAME with version I
7185 or a symbolic range containing the SSA_NAME only if the value range
7186 is varying or undefined. */
7188 static inline value_range
7189 get_vr_for_comparison (int i)
7191 value_range vr = *get_value_range (ssa_name (i));
7193 /* If name N_i does not have a valid range, use N_i as its own
7194 range. This allows us to compare against names that may
7195 have N_i in their ranges. */
7196 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7198 vr.type = VR_RANGE;
7199 vr.min = ssa_name (i);
7200 vr.max = ssa_name (i);
7203 return vr;
7206 /* Compare all the value ranges for names equivalent to VAR with VAL
7207 using comparison code COMP. Return the same value returned by
7208 compare_range_with_value, including the setting of
7209 *STRICT_OVERFLOW_P. */
7211 static tree
7212 compare_name_with_value (enum tree_code comp, tree var, tree val,
7213 bool *strict_overflow_p, bool use_equiv_p)
7215 bitmap_iterator bi;
7216 unsigned i;
7217 bitmap e;
7218 tree retval, t;
7219 int used_strict_overflow;
7220 bool sop;
7221 value_range equiv_vr;
7223 /* Get the set of equivalences for VAR. */
7224 e = get_value_range (var)->equiv;
7226 /* Start at -1. Set it to 0 if we do a comparison without relying
7227 on overflow, or 1 if all comparisons rely on overflow. */
7228 used_strict_overflow = -1;
7230 /* Compare vars' value range with val. */
7231 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7232 sop = false;
7233 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7234 if (retval)
7235 used_strict_overflow = sop ? 1 : 0;
7237 /* If the equiv set is empty we have done all work we need to do. */
7238 if (e == NULL)
7240 if (retval
7241 && used_strict_overflow > 0)
7242 *strict_overflow_p = true;
7243 return retval;
7246 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7248 if (! use_equiv_p
7249 && ! SSA_NAME_IS_DEFAULT_DEF (ssa_name (i))
7250 && prop_simulate_again_p (SSA_NAME_DEF_STMT (ssa_name (i))))
7251 continue;
7253 equiv_vr = get_vr_for_comparison (i);
7254 sop = false;
7255 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7256 if (t)
7258 /* If we get different answers from different members
7259 of the equivalence set this check must be in a dead
7260 code region. Folding it to a trap representation
7261 would be correct here. For now just return don't-know. */
7262 if (retval != NULL
7263 && t != retval)
7265 retval = NULL_TREE;
7266 break;
7268 retval = t;
7270 if (!sop)
7271 used_strict_overflow = 0;
7272 else if (used_strict_overflow < 0)
7273 used_strict_overflow = 1;
7277 if (retval
7278 && used_strict_overflow > 0)
7279 *strict_overflow_p = true;
7281 return retval;
7285 /* Given a comparison code COMP and names N1 and N2, compare all the
7286 ranges equivalent to N1 against all the ranges equivalent to N2
7287 to determine the value of N1 COMP N2. Return the same value
7288 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7289 whether we relied on an overflow infinity in the comparison. */
7292 static tree
7293 compare_names (enum tree_code comp, tree n1, tree n2,
7294 bool *strict_overflow_p)
7296 tree t, retval;
7297 bitmap e1, e2;
7298 bitmap_iterator bi1, bi2;
7299 unsigned i1, i2;
7300 int used_strict_overflow;
7301 static bitmap_obstack *s_obstack = NULL;
7302 static bitmap s_e1 = NULL, s_e2 = NULL;
7304 /* Compare the ranges of every name equivalent to N1 against the
7305 ranges of every name equivalent to N2. */
7306 e1 = get_value_range (n1)->equiv;
7307 e2 = get_value_range (n2)->equiv;
7309 /* Use the fake bitmaps if e1 or e2 are not available. */
7310 if (s_obstack == NULL)
7312 s_obstack = XNEW (bitmap_obstack);
7313 bitmap_obstack_initialize (s_obstack);
7314 s_e1 = BITMAP_ALLOC (s_obstack);
7315 s_e2 = BITMAP_ALLOC (s_obstack);
7317 if (e1 == NULL)
7318 e1 = s_e1;
7319 if (e2 == NULL)
7320 e2 = s_e2;
7322 /* Add N1 and N2 to their own set of equivalences to avoid
7323 duplicating the body of the loop just to check N1 and N2
7324 ranges. */
7325 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7326 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7328 /* If the equivalence sets have a common intersection, then the two
7329 names can be compared without checking their ranges. */
7330 if (bitmap_intersect_p (e1, e2))
7332 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7333 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7335 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7336 ? boolean_true_node
7337 : boolean_false_node;
7340 /* Start at -1. Set it to 0 if we do a comparison without relying
7341 on overflow, or 1 if all comparisons rely on overflow. */
7342 used_strict_overflow = -1;
7344 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7345 N2 to their own set of equivalences to avoid duplicating the body
7346 of the loop just to check N1 and N2 ranges. */
7347 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7349 value_range vr1 = get_vr_for_comparison (i1);
7351 t = retval = NULL_TREE;
7352 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7354 bool sop = false;
7356 value_range vr2 = get_vr_for_comparison (i2);
7358 t = compare_ranges (comp, &vr1, &vr2, &sop);
7359 if (t)
7361 /* If we get different answers from different members
7362 of the equivalence set this check must be in a dead
7363 code region. Folding it to a trap representation
7364 would be correct here. For now just return don't-know. */
7365 if (retval != NULL
7366 && t != retval)
7368 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7369 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7370 return NULL_TREE;
7372 retval = t;
7374 if (!sop)
7375 used_strict_overflow = 0;
7376 else if (used_strict_overflow < 0)
7377 used_strict_overflow = 1;
7381 if (retval)
7383 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7384 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7385 if (used_strict_overflow > 0)
7386 *strict_overflow_p = true;
7387 return retval;
7391 /* None of the equivalent ranges are useful in computing this
7392 comparison. */
7393 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7394 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7395 return NULL_TREE;
7398 /* Helper function for vrp_evaluate_conditional_warnv & other
7399 optimizers. */
7401 static tree
7402 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7403 tree op0, tree op1,
7404 bool * strict_overflow_p)
7406 value_range *vr0, *vr1;
7408 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7409 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7411 tree res = NULL_TREE;
7412 if (vr0 && vr1)
7413 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7414 if (!res && vr0)
7415 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7416 if (!res && vr1)
7417 res = (compare_range_with_value
7418 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7419 return res;
7422 /* Helper function for vrp_evaluate_conditional_warnv. */
7424 static tree
7425 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7426 tree op1, bool use_equiv_p,
7427 bool *strict_overflow_p, bool *only_ranges)
7429 tree ret;
7430 if (only_ranges)
7431 *only_ranges = true;
7433 /* We only deal with integral and pointer types. */
7434 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7435 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7436 return NULL_TREE;
7438 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7439 (code, op0, op1, strict_overflow_p)))
7440 return ret;
7441 if (only_ranges)
7442 *only_ranges = false;
7443 /* Do not use compare_names during propagation, it's quadratic. */
7444 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
7445 && use_equiv_p)
7446 return compare_names (code, op0, op1, strict_overflow_p);
7447 else if (TREE_CODE (op0) == SSA_NAME)
7448 return compare_name_with_value (code, op0, op1,
7449 strict_overflow_p, use_equiv_p);
7450 else if (TREE_CODE (op1) == SSA_NAME)
7451 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
7452 strict_overflow_p, use_equiv_p);
7453 return NULL_TREE;
7456 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7457 information. Return NULL if the conditional can not be evaluated.
7458 The ranges of all the names equivalent with the operands in COND
7459 will be used when trying to compute the value. If the result is
7460 based on undefined signed overflow, issue a warning if
7461 appropriate. */
7463 static tree
7464 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7466 bool sop;
7467 tree ret;
7468 bool only_ranges;
7470 /* Some passes and foldings leak constants with overflow flag set
7471 into the IL. Avoid doing wrong things with these and bail out. */
7472 if ((TREE_CODE (op0) == INTEGER_CST
7473 && TREE_OVERFLOW (op0))
7474 || (TREE_CODE (op1) == INTEGER_CST
7475 && TREE_OVERFLOW (op1)))
7476 return NULL_TREE;
7478 sop = false;
7479 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7480 &only_ranges);
7482 if (ret && sop)
7484 enum warn_strict_overflow_code wc;
7485 const char* warnmsg;
7487 if (is_gimple_min_invariant (ret))
7489 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7490 warnmsg = G_("assuming signed overflow does not occur when "
7491 "simplifying conditional to constant");
7493 else
7495 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7496 warnmsg = G_("assuming signed overflow does not occur when "
7497 "simplifying conditional");
7500 if (issue_strict_overflow_warning (wc))
7502 location_t location;
7504 if (!gimple_has_location (stmt))
7505 location = input_location;
7506 else
7507 location = gimple_location (stmt);
7508 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7512 if (warn_type_limits
7513 && ret && only_ranges
7514 && TREE_CODE_CLASS (code) == tcc_comparison
7515 && TREE_CODE (op0) == SSA_NAME)
7517 /* If the comparison is being folded and the operand on the LHS
7518 is being compared against a constant value that is outside of
7519 the natural range of OP0's type, then the predicate will
7520 always fold regardless of the value of OP0. If -Wtype-limits
7521 was specified, emit a warning. */
7522 tree type = TREE_TYPE (op0);
7523 value_range *vr0 = get_value_range (op0);
7525 if (vr0->type == VR_RANGE
7526 && INTEGRAL_TYPE_P (type)
7527 && vrp_val_is_min (vr0->min)
7528 && vrp_val_is_max (vr0->max)
7529 && is_gimple_min_invariant (op1))
7531 location_t location;
7533 if (!gimple_has_location (stmt))
7534 location = input_location;
7535 else
7536 location = gimple_location (stmt);
7538 warning_at (location, OPT_Wtype_limits,
7539 integer_zerop (ret)
7540 ? G_("comparison always false "
7541 "due to limited range of data type")
7542 : G_("comparison always true "
7543 "due to limited range of data type"));
7547 return ret;
7551 /* Visit conditional statement STMT. If we can determine which edge
7552 will be taken out of STMT's basic block, record it in
7553 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7554 SSA_PROP_VARYING. */
7556 static enum ssa_prop_result
7557 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7559 tree val;
7560 bool sop;
7562 *taken_edge_p = NULL;
7564 if (dump_file && (dump_flags & TDF_DETAILS))
7566 tree use;
7567 ssa_op_iter i;
7569 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7570 print_gimple_stmt (dump_file, stmt, 0, 0);
7571 fprintf (dump_file, "\nWith known ranges\n");
7573 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7575 fprintf (dump_file, "\t");
7576 print_generic_expr (dump_file, use, 0);
7577 fprintf (dump_file, ": ");
7578 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7581 fprintf (dump_file, "\n");
7584 /* Compute the value of the predicate COND by checking the known
7585 ranges of each of its operands.
7587 Note that we cannot evaluate all the equivalent ranges here
7588 because those ranges may not yet be final and with the current
7589 propagation strategy, we cannot determine when the value ranges
7590 of the names in the equivalence set have changed.
7592 For instance, given the following code fragment
7594 i_5 = PHI <8, i_13>
7596 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7597 if (i_14 == 1)
7600 Assume that on the first visit to i_14, i_5 has the temporary
7601 range [8, 8] because the second argument to the PHI function is
7602 not yet executable. We derive the range ~[0, 0] for i_14 and the
7603 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7604 the first time, since i_14 is equivalent to the range [8, 8], we
7605 determine that the predicate is always false.
7607 On the next round of propagation, i_13 is determined to be
7608 VARYING, which causes i_5 to drop down to VARYING. So, another
7609 visit to i_14 is scheduled. In this second visit, we compute the
7610 exact same range and equivalence set for i_14, namely ~[0, 0] and
7611 { i_5 }. But we did not have the previous range for i_5
7612 registered, so vrp_visit_assignment thinks that the range for
7613 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7614 is not visited again, which stops propagation from visiting
7615 statements in the THEN clause of that if().
7617 To properly fix this we would need to keep the previous range
7618 value for the names in the equivalence set. This way we would've
7619 discovered that from one visit to the other i_5 changed from
7620 range [8, 8] to VR_VARYING.
7622 However, fixing this apparent limitation may not be worth the
7623 additional checking. Testing on several code bases (GCC, DLV,
7624 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7625 4 more predicates folded in SPEC. */
7626 sop = false;
7628 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7629 gimple_cond_lhs (stmt),
7630 gimple_cond_rhs (stmt),
7631 false, &sop, NULL);
7632 if (val)
7634 if (!sop)
7635 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7636 else
7638 if (dump_file && (dump_flags & TDF_DETAILS))
7639 fprintf (dump_file,
7640 "\nIgnoring predicate evaluation because "
7641 "it assumes that signed overflow is undefined");
7642 val = NULL_TREE;
7646 if (dump_file && (dump_flags & TDF_DETAILS))
7648 fprintf (dump_file, "\nPredicate evaluates to: ");
7649 if (val == NULL_TREE)
7650 fprintf (dump_file, "DON'T KNOW\n");
7651 else
7652 print_generic_stmt (dump_file, val, 0);
7655 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7658 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7659 that includes the value VAL. The search is restricted to the range
7660 [START_IDX, n - 1] where n is the size of VEC.
7662 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7663 returned.
7665 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7666 it is placed in IDX and false is returned.
7668 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7669 returned. */
7671 static bool
7672 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7674 size_t n = gimple_switch_num_labels (stmt);
7675 size_t low, high;
7677 /* Find case label for minimum of the value range or the next one.
7678 At each iteration we are searching in [low, high - 1]. */
7680 for (low = start_idx, high = n; high != low; )
7682 tree t;
7683 int cmp;
7684 /* Note that i != high, so we never ask for n. */
7685 size_t i = (high + low) / 2;
7686 t = gimple_switch_label (stmt, i);
7688 /* Cache the result of comparing CASE_LOW and val. */
7689 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7691 if (cmp == 0)
7693 /* Ranges cannot be empty. */
7694 *idx = i;
7695 return true;
7697 else if (cmp > 0)
7698 high = i;
7699 else
7701 low = i + 1;
7702 if (CASE_HIGH (t) != NULL
7703 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7705 *idx = i;
7706 return true;
7711 *idx = high;
7712 return false;
7715 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7716 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7717 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7718 then MAX_IDX < MIN_IDX.
7719 Returns true if the default label is not needed. */
7721 static bool
7722 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7723 size_t *max_idx)
7725 size_t i, j;
7726 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7727 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7729 if (i == j
7730 && min_take_default
7731 && max_take_default)
7733 /* Only the default case label reached.
7734 Return an empty range. */
7735 *min_idx = 1;
7736 *max_idx = 0;
7737 return false;
7739 else
7741 bool take_default = min_take_default || max_take_default;
7742 tree low, high;
7743 size_t k;
7745 if (max_take_default)
7746 j--;
7748 /* If the case label range is continuous, we do not need
7749 the default case label. Verify that. */
7750 high = CASE_LOW (gimple_switch_label (stmt, i));
7751 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7752 high = CASE_HIGH (gimple_switch_label (stmt, i));
7753 for (k = i + 1; k <= j; ++k)
7755 low = CASE_LOW (gimple_switch_label (stmt, k));
7756 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7758 take_default = true;
7759 break;
7761 high = low;
7762 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7763 high = CASE_HIGH (gimple_switch_label (stmt, k));
7766 *min_idx = i;
7767 *max_idx = j;
7768 return !take_default;
7772 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7773 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7774 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7775 Returns true if the default label is not needed. */
7777 static bool
7778 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7779 size_t *max_idx1, size_t *min_idx2,
7780 size_t *max_idx2)
7782 size_t i, j, k, l;
7783 unsigned int n = gimple_switch_num_labels (stmt);
7784 bool take_default;
7785 tree case_low, case_high;
7786 tree min = vr->min, max = vr->max;
7788 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7790 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7792 /* Set second range to emtpy. */
7793 *min_idx2 = 1;
7794 *max_idx2 = 0;
7796 if (vr->type == VR_RANGE)
7798 *min_idx1 = i;
7799 *max_idx1 = j;
7800 return !take_default;
7803 /* Set first range to all case labels. */
7804 *min_idx1 = 1;
7805 *max_idx1 = n - 1;
7807 if (i > j)
7808 return false;
7810 /* Make sure all the values of case labels [i , j] are contained in
7811 range [MIN, MAX]. */
7812 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7813 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7814 if (tree_int_cst_compare (case_low, min) < 0)
7815 i += 1;
7816 if (case_high != NULL_TREE
7817 && tree_int_cst_compare (max, case_high) < 0)
7818 j -= 1;
7820 if (i > j)
7821 return false;
7823 /* If the range spans case labels [i, j], the corresponding anti-range spans
7824 the labels [1, i - 1] and [j + 1, n - 1]. */
7825 k = j + 1;
7826 l = n - 1;
7827 if (k > l)
7829 k = 1;
7830 l = 0;
7833 j = i - 1;
7834 i = 1;
7835 if (i > j)
7837 i = k;
7838 j = l;
7839 k = 1;
7840 l = 0;
7843 *min_idx1 = i;
7844 *max_idx1 = j;
7845 *min_idx2 = k;
7846 *max_idx2 = l;
7847 return false;
7850 /* Visit switch statement STMT. If we can determine which edge
7851 will be taken out of STMT's basic block, record it in
7852 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7853 SSA_PROP_VARYING. */
7855 static enum ssa_prop_result
7856 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7858 tree op, val;
7859 value_range *vr;
7860 size_t i = 0, j = 0, k, l;
7861 bool take_default;
7863 *taken_edge_p = NULL;
7864 op = gimple_switch_index (stmt);
7865 if (TREE_CODE (op) != SSA_NAME)
7866 return SSA_PROP_VARYING;
7868 vr = get_value_range (op);
7869 if (dump_file && (dump_flags & TDF_DETAILS))
7871 fprintf (dump_file, "\nVisiting switch expression with operand ");
7872 print_generic_expr (dump_file, op, 0);
7873 fprintf (dump_file, " with known range ");
7874 dump_value_range (dump_file, vr);
7875 fprintf (dump_file, "\n");
7878 if ((vr->type != VR_RANGE
7879 && vr->type != VR_ANTI_RANGE)
7880 || symbolic_range_p (vr))
7881 return SSA_PROP_VARYING;
7883 /* Find the single edge that is taken from the switch expression. */
7884 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7886 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7887 label */
7888 if (j < i)
7890 gcc_assert (take_default);
7891 val = gimple_switch_default_label (stmt);
7893 else
7895 /* Check if labels with index i to j and maybe the default label
7896 are all reaching the same label. */
7898 val = gimple_switch_label (stmt, i);
7899 if (take_default
7900 && CASE_LABEL (gimple_switch_default_label (stmt))
7901 != CASE_LABEL (val))
7903 if (dump_file && (dump_flags & TDF_DETAILS))
7904 fprintf (dump_file, " not a single destination for this "
7905 "range\n");
7906 return SSA_PROP_VARYING;
7908 for (++i; i <= j; ++i)
7910 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7912 if (dump_file && (dump_flags & TDF_DETAILS))
7913 fprintf (dump_file, " not a single destination for this "
7914 "range\n");
7915 return SSA_PROP_VARYING;
7918 for (; k <= l; ++k)
7920 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7922 if (dump_file && (dump_flags & TDF_DETAILS))
7923 fprintf (dump_file, " not a single destination for this "
7924 "range\n");
7925 return SSA_PROP_VARYING;
7930 *taken_edge_p = find_edge (gimple_bb (stmt),
7931 label_to_block (CASE_LABEL (val)));
7933 if (dump_file && (dump_flags & TDF_DETAILS))
7935 fprintf (dump_file, " will take edge to ");
7936 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7939 return SSA_PROP_INTERESTING;
7943 /* Evaluate statement STMT. If the statement produces a useful range,
7944 return SSA_PROP_INTERESTING and record the SSA name with the
7945 interesting range into *OUTPUT_P.
7947 If STMT is a conditional branch and we can determine its truth
7948 value, the taken edge is recorded in *TAKEN_EDGE_P.
7950 If STMT produces a varying value, return SSA_PROP_VARYING. */
7952 static enum ssa_prop_result
7953 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
7955 if (dump_file && (dump_flags & TDF_DETAILS))
7957 fprintf (dump_file, "\nVisiting statement:\n");
7958 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7961 if (!stmt_interesting_for_vrp (stmt))
7962 gcc_assert (stmt_ends_bb_p (stmt));
7963 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7964 return vrp_visit_assignment_or_call (stmt, output_p);
7965 else if (gimple_code (stmt) == GIMPLE_COND)
7966 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7967 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7968 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7970 /* All other statements produce nothing of interest for VRP, so mark
7971 their outputs varying and prevent further simulation. */
7972 set_defs_to_varying (stmt);
7974 return SSA_PROP_VARYING;
7977 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7978 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7979 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7980 possible such range. The resulting range is not canonicalized. */
7982 static void
7983 union_ranges (enum value_range_type *vr0type,
7984 tree *vr0min, tree *vr0max,
7985 enum value_range_type vr1type,
7986 tree vr1min, tree vr1max)
7988 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
7989 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
7991 /* [] is vr0, () is vr1 in the following classification comments. */
7992 if (mineq && maxeq)
7994 /* [( )] */
7995 if (*vr0type == vr1type)
7996 /* Nothing to do for equal ranges. */
7998 else if ((*vr0type == VR_RANGE
7999 && vr1type == VR_ANTI_RANGE)
8000 || (*vr0type == VR_ANTI_RANGE
8001 && vr1type == VR_RANGE))
8003 /* For anti-range with range union the result is varying. */
8004 goto give_up;
8006 else
8007 gcc_unreachable ();
8009 else if (operand_less_p (*vr0max, vr1min) == 1
8010 || operand_less_p (vr1max, *vr0min) == 1)
8012 /* [ ] ( ) or ( ) [ ]
8013 If the ranges have an empty intersection, result of the union
8014 operation is the anti-range or if both are anti-ranges
8015 it covers all. */
8016 if (*vr0type == VR_ANTI_RANGE
8017 && vr1type == VR_ANTI_RANGE)
8018 goto give_up;
8019 else if (*vr0type == VR_ANTI_RANGE
8020 && vr1type == VR_RANGE)
8022 else if (*vr0type == VR_RANGE
8023 && vr1type == VR_ANTI_RANGE)
8025 *vr0type = vr1type;
8026 *vr0min = vr1min;
8027 *vr0max = vr1max;
8029 else if (*vr0type == VR_RANGE
8030 && vr1type == VR_RANGE)
8032 /* The result is the convex hull of both ranges. */
8033 if (operand_less_p (*vr0max, vr1min) == 1)
8035 /* If the result can be an anti-range, create one. */
8036 if (TREE_CODE (*vr0max) == INTEGER_CST
8037 && TREE_CODE (vr1min) == INTEGER_CST
8038 && vrp_val_is_min (*vr0min)
8039 && vrp_val_is_max (vr1max))
8041 tree min = int_const_binop (PLUS_EXPR,
8042 *vr0max,
8043 build_int_cst (TREE_TYPE (*vr0max), 1));
8044 tree max = int_const_binop (MINUS_EXPR,
8045 vr1min,
8046 build_int_cst (TREE_TYPE (vr1min), 1));
8047 if (!operand_less_p (max, min))
8049 *vr0type = VR_ANTI_RANGE;
8050 *vr0min = min;
8051 *vr0max = max;
8053 else
8054 *vr0max = vr1max;
8056 else
8057 *vr0max = vr1max;
8059 else
8061 /* If the result can be an anti-range, create one. */
8062 if (TREE_CODE (vr1max) == INTEGER_CST
8063 && TREE_CODE (*vr0min) == INTEGER_CST
8064 && vrp_val_is_min (vr1min)
8065 && vrp_val_is_max (*vr0max))
8067 tree min = int_const_binop (PLUS_EXPR,
8068 vr1max,
8069 build_int_cst (TREE_TYPE (vr1max), 1));
8070 tree max = int_const_binop (MINUS_EXPR,
8071 *vr0min,
8072 build_int_cst (TREE_TYPE (*vr0min), 1));
8073 if (!operand_less_p (max, min))
8075 *vr0type = VR_ANTI_RANGE;
8076 *vr0min = min;
8077 *vr0max = max;
8079 else
8080 *vr0min = vr1min;
8082 else
8083 *vr0min = vr1min;
8086 else
8087 gcc_unreachable ();
8089 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8090 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8092 /* [ ( ) ] or [( ) ] or [ ( )] */
8093 if (*vr0type == VR_RANGE
8094 && vr1type == VR_RANGE)
8096 else if (*vr0type == VR_ANTI_RANGE
8097 && vr1type == VR_ANTI_RANGE)
8099 *vr0type = vr1type;
8100 *vr0min = vr1min;
8101 *vr0max = vr1max;
8103 else if (*vr0type == VR_ANTI_RANGE
8104 && vr1type == VR_RANGE)
8106 /* Arbitrarily choose the right or left gap. */
8107 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8108 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8109 build_int_cst (TREE_TYPE (vr1min), 1));
8110 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8111 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8112 build_int_cst (TREE_TYPE (vr1max), 1));
8113 else
8114 goto give_up;
8116 else if (*vr0type == VR_RANGE
8117 && vr1type == VR_ANTI_RANGE)
8118 /* The result covers everything. */
8119 goto give_up;
8120 else
8121 gcc_unreachable ();
8123 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8124 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8126 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8127 if (*vr0type == VR_RANGE
8128 && vr1type == VR_RANGE)
8130 *vr0type = vr1type;
8131 *vr0min = vr1min;
8132 *vr0max = vr1max;
8134 else if (*vr0type == VR_ANTI_RANGE
8135 && vr1type == VR_ANTI_RANGE)
8137 else if (*vr0type == VR_RANGE
8138 && vr1type == VR_ANTI_RANGE)
8140 *vr0type = VR_ANTI_RANGE;
8141 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8143 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8144 build_int_cst (TREE_TYPE (*vr0min), 1));
8145 *vr0min = vr1min;
8147 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8149 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8150 build_int_cst (TREE_TYPE (*vr0max), 1));
8151 *vr0max = vr1max;
8153 else
8154 goto give_up;
8156 else if (*vr0type == VR_ANTI_RANGE
8157 && vr1type == VR_RANGE)
8158 /* The result covers everything. */
8159 goto give_up;
8160 else
8161 gcc_unreachable ();
8163 else if ((operand_less_p (vr1min, *vr0max) == 1
8164 || operand_equal_p (vr1min, *vr0max, 0))
8165 && operand_less_p (*vr0min, vr1min) == 1
8166 && operand_less_p (*vr0max, vr1max) == 1)
8168 /* [ ( ] ) or [ ]( ) */
8169 if (*vr0type == VR_RANGE
8170 && vr1type == VR_RANGE)
8171 *vr0max = vr1max;
8172 else if (*vr0type == VR_ANTI_RANGE
8173 && vr1type == VR_ANTI_RANGE)
8174 *vr0min = vr1min;
8175 else if (*vr0type == VR_ANTI_RANGE
8176 && vr1type == VR_RANGE)
8178 if (TREE_CODE (vr1min) == INTEGER_CST)
8179 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8180 build_int_cst (TREE_TYPE (vr1min), 1));
8181 else
8182 goto give_up;
8184 else if (*vr0type == VR_RANGE
8185 && vr1type == VR_ANTI_RANGE)
8187 if (TREE_CODE (*vr0max) == INTEGER_CST)
8189 *vr0type = vr1type;
8190 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8191 build_int_cst (TREE_TYPE (*vr0max), 1));
8192 *vr0max = vr1max;
8194 else
8195 goto give_up;
8197 else
8198 gcc_unreachable ();
8200 else if ((operand_less_p (*vr0min, vr1max) == 1
8201 || operand_equal_p (*vr0min, vr1max, 0))
8202 && operand_less_p (vr1min, *vr0min) == 1
8203 && operand_less_p (vr1max, *vr0max) == 1)
8205 /* ( [ ) ] or ( )[ ] */
8206 if (*vr0type == VR_RANGE
8207 && vr1type == VR_RANGE)
8208 *vr0min = vr1min;
8209 else if (*vr0type == VR_ANTI_RANGE
8210 && vr1type == VR_ANTI_RANGE)
8211 *vr0max = vr1max;
8212 else if (*vr0type == VR_ANTI_RANGE
8213 && vr1type == VR_RANGE)
8215 if (TREE_CODE (vr1max) == INTEGER_CST)
8216 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8217 build_int_cst (TREE_TYPE (vr1max), 1));
8218 else
8219 goto give_up;
8221 else if (*vr0type == VR_RANGE
8222 && vr1type == VR_ANTI_RANGE)
8224 if (TREE_CODE (*vr0min) == INTEGER_CST)
8226 *vr0type = vr1type;
8227 *vr0min = vr1min;
8228 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8229 build_int_cst (TREE_TYPE (*vr0min), 1));
8231 else
8232 goto give_up;
8234 else
8235 gcc_unreachable ();
8237 else
8238 goto give_up;
8240 return;
8242 give_up:
8243 *vr0type = VR_VARYING;
8244 *vr0min = NULL_TREE;
8245 *vr0max = NULL_TREE;
8248 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8249 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8250 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8251 possible such range. The resulting range is not canonicalized. */
8253 static void
8254 intersect_ranges (enum value_range_type *vr0type,
8255 tree *vr0min, tree *vr0max,
8256 enum value_range_type vr1type,
8257 tree vr1min, tree vr1max)
8259 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8260 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8262 /* [] is vr0, () is vr1 in the following classification comments. */
8263 if (mineq && maxeq)
8265 /* [( )] */
8266 if (*vr0type == vr1type)
8267 /* Nothing to do for equal ranges. */
8269 else if ((*vr0type == VR_RANGE
8270 && vr1type == VR_ANTI_RANGE)
8271 || (*vr0type == VR_ANTI_RANGE
8272 && vr1type == VR_RANGE))
8274 /* For anti-range with range intersection the result is empty. */
8275 *vr0type = VR_UNDEFINED;
8276 *vr0min = NULL_TREE;
8277 *vr0max = NULL_TREE;
8279 else
8280 gcc_unreachable ();
8282 else if (operand_less_p (*vr0max, vr1min) == 1
8283 || operand_less_p (vr1max, *vr0min) == 1)
8285 /* [ ] ( ) or ( ) [ ]
8286 If the ranges have an empty intersection, the result of the
8287 intersect operation is the range for intersecting an
8288 anti-range with a range or empty when intersecting two ranges. */
8289 if (*vr0type == VR_RANGE
8290 && vr1type == VR_ANTI_RANGE)
8292 else if (*vr0type == VR_ANTI_RANGE
8293 && vr1type == VR_RANGE)
8295 *vr0type = vr1type;
8296 *vr0min = vr1min;
8297 *vr0max = vr1max;
8299 else if (*vr0type == VR_RANGE
8300 && vr1type == VR_RANGE)
8302 *vr0type = VR_UNDEFINED;
8303 *vr0min = NULL_TREE;
8304 *vr0max = NULL_TREE;
8306 else if (*vr0type == VR_ANTI_RANGE
8307 && vr1type == VR_ANTI_RANGE)
8309 /* If the anti-ranges are adjacent to each other merge them. */
8310 if (TREE_CODE (*vr0max) == INTEGER_CST
8311 && TREE_CODE (vr1min) == INTEGER_CST
8312 && operand_less_p (*vr0max, vr1min) == 1
8313 && integer_onep (int_const_binop (MINUS_EXPR,
8314 vr1min, *vr0max)))
8315 *vr0max = vr1max;
8316 else if (TREE_CODE (vr1max) == INTEGER_CST
8317 && TREE_CODE (*vr0min) == INTEGER_CST
8318 && operand_less_p (vr1max, *vr0min) == 1
8319 && integer_onep (int_const_binop (MINUS_EXPR,
8320 *vr0min, vr1max)))
8321 *vr0min = vr1min;
8322 /* Else arbitrarily take VR0. */
8325 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8326 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8328 /* [ ( ) ] or [( ) ] or [ ( )] */
8329 if (*vr0type == VR_RANGE
8330 && vr1type == VR_RANGE)
8332 /* If both are ranges the result is the inner one. */
8333 *vr0type = vr1type;
8334 *vr0min = vr1min;
8335 *vr0max = vr1max;
8337 else if (*vr0type == VR_RANGE
8338 && vr1type == VR_ANTI_RANGE)
8340 /* Choose the right gap if the left one is empty. */
8341 if (mineq)
8343 if (TREE_CODE (vr1max) == INTEGER_CST)
8344 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8345 build_int_cst (TREE_TYPE (vr1max), 1));
8346 else
8347 *vr0min = vr1max;
8349 /* Choose the left gap if the right one is empty. */
8350 else if (maxeq)
8352 if (TREE_CODE (vr1min) == INTEGER_CST)
8353 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8354 build_int_cst (TREE_TYPE (vr1min), 1));
8355 else
8356 *vr0max = vr1min;
8358 /* Choose the anti-range if the range is effectively varying. */
8359 else if (vrp_val_is_min (*vr0min)
8360 && vrp_val_is_max (*vr0max))
8362 *vr0type = vr1type;
8363 *vr0min = vr1min;
8364 *vr0max = vr1max;
8366 /* Else choose the range. */
8368 else if (*vr0type == VR_ANTI_RANGE
8369 && vr1type == VR_ANTI_RANGE)
8370 /* If both are anti-ranges the result is the outer one. */
8372 else if (*vr0type == VR_ANTI_RANGE
8373 && vr1type == VR_RANGE)
8375 /* The intersection is empty. */
8376 *vr0type = VR_UNDEFINED;
8377 *vr0min = NULL_TREE;
8378 *vr0max = NULL_TREE;
8380 else
8381 gcc_unreachable ();
8383 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8384 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8386 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8387 if (*vr0type == VR_RANGE
8388 && vr1type == VR_RANGE)
8389 /* Choose the inner range. */
8391 else if (*vr0type == VR_ANTI_RANGE
8392 && vr1type == VR_RANGE)
8394 /* Choose the right gap if the left is empty. */
8395 if (mineq)
8397 *vr0type = VR_RANGE;
8398 if (TREE_CODE (*vr0max) == INTEGER_CST)
8399 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8400 build_int_cst (TREE_TYPE (*vr0max), 1));
8401 else
8402 *vr0min = *vr0max;
8403 *vr0max = vr1max;
8405 /* Choose the left gap if the right is empty. */
8406 else if (maxeq)
8408 *vr0type = VR_RANGE;
8409 if (TREE_CODE (*vr0min) == INTEGER_CST)
8410 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8411 build_int_cst (TREE_TYPE (*vr0min), 1));
8412 else
8413 *vr0max = *vr0min;
8414 *vr0min = vr1min;
8416 /* Choose the anti-range if the range is effectively varying. */
8417 else if (vrp_val_is_min (vr1min)
8418 && vrp_val_is_max (vr1max))
8420 /* Else choose the range. */
8421 else
8423 *vr0type = vr1type;
8424 *vr0min = vr1min;
8425 *vr0max = vr1max;
8428 else if (*vr0type == VR_ANTI_RANGE
8429 && vr1type == VR_ANTI_RANGE)
8431 /* If both are anti-ranges the result is the outer one. */
8432 *vr0type = vr1type;
8433 *vr0min = vr1min;
8434 *vr0max = vr1max;
8436 else if (vr1type == VR_ANTI_RANGE
8437 && *vr0type == VR_RANGE)
8439 /* The intersection is empty. */
8440 *vr0type = VR_UNDEFINED;
8441 *vr0min = NULL_TREE;
8442 *vr0max = NULL_TREE;
8444 else
8445 gcc_unreachable ();
8447 else if ((operand_less_p (vr1min, *vr0max) == 1
8448 || operand_equal_p (vr1min, *vr0max, 0))
8449 && operand_less_p (*vr0min, vr1min) == 1)
8451 /* [ ( ] ) or [ ]( ) */
8452 if (*vr0type == VR_ANTI_RANGE
8453 && vr1type == VR_ANTI_RANGE)
8454 *vr0max = vr1max;
8455 else if (*vr0type == VR_RANGE
8456 && vr1type == VR_RANGE)
8457 *vr0min = vr1min;
8458 else if (*vr0type == VR_RANGE
8459 && vr1type == VR_ANTI_RANGE)
8461 if (TREE_CODE (vr1min) == INTEGER_CST)
8462 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8463 build_int_cst (TREE_TYPE (vr1min), 1));
8464 else
8465 *vr0max = vr1min;
8467 else if (*vr0type == VR_ANTI_RANGE
8468 && vr1type == VR_RANGE)
8470 *vr0type = VR_RANGE;
8471 if (TREE_CODE (*vr0max) == INTEGER_CST)
8472 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8473 build_int_cst (TREE_TYPE (*vr0max), 1));
8474 else
8475 *vr0min = *vr0max;
8476 *vr0max = vr1max;
8478 else
8479 gcc_unreachable ();
8481 else if ((operand_less_p (*vr0min, vr1max) == 1
8482 || operand_equal_p (*vr0min, vr1max, 0))
8483 && operand_less_p (vr1min, *vr0min) == 1)
8485 /* ( [ ) ] or ( )[ ] */
8486 if (*vr0type == VR_ANTI_RANGE
8487 && vr1type == VR_ANTI_RANGE)
8488 *vr0min = vr1min;
8489 else if (*vr0type == VR_RANGE
8490 && vr1type == VR_RANGE)
8491 *vr0max = vr1max;
8492 else if (*vr0type == VR_RANGE
8493 && vr1type == VR_ANTI_RANGE)
8495 if (TREE_CODE (vr1max) == INTEGER_CST)
8496 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8497 build_int_cst (TREE_TYPE (vr1max), 1));
8498 else
8499 *vr0min = vr1max;
8501 else if (*vr0type == VR_ANTI_RANGE
8502 && vr1type == VR_RANGE)
8504 *vr0type = VR_RANGE;
8505 if (TREE_CODE (*vr0min) == INTEGER_CST)
8506 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8507 build_int_cst (TREE_TYPE (*vr0min), 1));
8508 else
8509 *vr0max = *vr0min;
8510 *vr0min = vr1min;
8512 else
8513 gcc_unreachable ();
8516 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8517 result for the intersection. That's always a conservative
8518 correct estimate. */
8520 return;
8524 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8525 in *VR0. This may not be the smallest possible such range. */
8527 static void
8528 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8530 value_range saved;
8532 /* If either range is VR_VARYING the other one wins. */
8533 if (vr1->type == VR_VARYING)
8534 return;
8535 if (vr0->type == VR_VARYING)
8537 copy_value_range (vr0, vr1);
8538 return;
8541 /* When either range is VR_UNDEFINED the resulting range is
8542 VR_UNDEFINED, too. */
8543 if (vr0->type == VR_UNDEFINED)
8544 return;
8545 if (vr1->type == VR_UNDEFINED)
8547 set_value_range_to_undefined (vr0);
8548 return;
8551 /* Save the original vr0 so we can return it as conservative intersection
8552 result when our worker turns things to varying. */
8553 saved = *vr0;
8554 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8555 vr1->type, vr1->min, vr1->max);
8556 /* Make sure to canonicalize the result though as the inversion of a
8557 VR_RANGE can still be a VR_RANGE. */
8558 set_and_canonicalize_value_range (vr0, vr0->type,
8559 vr0->min, vr0->max, vr0->equiv);
8560 /* If that failed, use the saved original VR0. */
8561 if (vr0->type == VR_VARYING)
8563 *vr0 = saved;
8564 return;
8566 /* If the result is VR_UNDEFINED there is no need to mess with
8567 the equivalencies. */
8568 if (vr0->type == VR_UNDEFINED)
8569 return;
8571 /* The resulting set of equivalences for range intersection is the union of
8572 the two sets. */
8573 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8574 bitmap_ior_into (vr0->equiv, vr1->equiv);
8575 else if (vr1->equiv && !vr0->equiv)
8577 vr0->equiv = BITMAP_ALLOC (NULL);
8578 bitmap_copy (vr0->equiv, vr1->equiv);
8582 static void
8583 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8585 if (dump_file && (dump_flags & TDF_DETAILS))
8587 fprintf (dump_file, "Intersecting\n ");
8588 dump_value_range (dump_file, vr0);
8589 fprintf (dump_file, "\nand\n ");
8590 dump_value_range (dump_file, vr1);
8591 fprintf (dump_file, "\n");
8593 vrp_intersect_ranges_1 (vr0, vr1);
8594 if (dump_file && (dump_flags & TDF_DETAILS))
8596 fprintf (dump_file, "to\n ");
8597 dump_value_range (dump_file, vr0);
8598 fprintf (dump_file, "\n");
8602 /* Meet operation for value ranges. Given two value ranges VR0 and
8603 VR1, store in VR0 a range that contains both VR0 and VR1. This
8604 may not be the smallest possible such range. */
8606 static void
8607 vrp_meet_1 (value_range *vr0, value_range *vr1)
8609 value_range saved;
8611 if (vr0->type == VR_UNDEFINED)
8613 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8614 return;
8617 if (vr1->type == VR_UNDEFINED)
8619 /* VR0 already has the resulting range. */
8620 return;
8623 if (vr0->type == VR_VARYING)
8625 /* Nothing to do. VR0 already has the resulting range. */
8626 return;
8629 if (vr1->type == VR_VARYING)
8631 set_value_range_to_varying (vr0);
8632 return;
8635 saved = *vr0;
8636 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8637 vr1->type, vr1->min, vr1->max);
8638 if (vr0->type == VR_VARYING)
8640 /* Failed to find an efficient meet. Before giving up and setting
8641 the result to VARYING, see if we can at least derive a useful
8642 anti-range. FIXME, all this nonsense about distinguishing
8643 anti-ranges from ranges is necessary because of the odd
8644 semantics of range_includes_zero_p and friends. */
8645 if (((saved.type == VR_RANGE
8646 && range_includes_zero_p (saved.min, saved.max) == 0)
8647 || (saved.type == VR_ANTI_RANGE
8648 && range_includes_zero_p (saved.min, saved.max) == 1))
8649 && ((vr1->type == VR_RANGE
8650 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8651 || (vr1->type == VR_ANTI_RANGE
8652 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8654 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8656 /* Since this meet operation did not result from the meeting of
8657 two equivalent names, VR0 cannot have any equivalences. */
8658 if (vr0->equiv)
8659 bitmap_clear (vr0->equiv);
8660 return;
8663 set_value_range_to_varying (vr0);
8664 return;
8666 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8667 vr0->equiv);
8668 if (vr0->type == VR_VARYING)
8669 return;
8671 /* The resulting set of equivalences is always the intersection of
8672 the two sets. */
8673 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8674 bitmap_and_into (vr0->equiv, vr1->equiv);
8675 else if (vr0->equiv && !vr1->equiv)
8676 bitmap_clear (vr0->equiv);
8679 static void
8680 vrp_meet (value_range *vr0, value_range *vr1)
8682 if (dump_file && (dump_flags & TDF_DETAILS))
8684 fprintf (dump_file, "Meeting\n ");
8685 dump_value_range (dump_file, vr0);
8686 fprintf (dump_file, "\nand\n ");
8687 dump_value_range (dump_file, vr1);
8688 fprintf (dump_file, "\n");
8690 vrp_meet_1 (vr0, vr1);
8691 if (dump_file && (dump_flags & TDF_DETAILS))
8693 fprintf (dump_file, "to\n ");
8694 dump_value_range (dump_file, vr0);
8695 fprintf (dump_file, "\n");
8700 /* Visit all arguments for PHI node PHI that flow through executable
8701 edges. If a valid value range can be derived from all the incoming
8702 value ranges, set a new range for the LHS of PHI. */
8704 static enum ssa_prop_result
8705 vrp_visit_phi_node (gphi *phi)
8707 size_t i;
8708 tree lhs = PHI_RESULT (phi);
8709 value_range *lhs_vr = get_value_range (lhs);
8710 value_range vr_result = VR_INITIALIZER;
8711 bool first = true;
8712 int edges, old_edges;
8713 struct loop *l;
8715 if (dump_file && (dump_flags & TDF_DETAILS))
8717 fprintf (dump_file, "\nVisiting PHI node: ");
8718 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8721 edges = 0;
8722 for (i = 0; i < gimple_phi_num_args (phi); i++)
8724 edge e = gimple_phi_arg_edge (phi, i);
8726 if (dump_file && (dump_flags & TDF_DETAILS))
8728 fprintf (dump_file,
8729 " Argument #%d (%d -> %d %sexecutable)\n",
8730 (int) i, e->src->index, e->dest->index,
8731 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8734 if (e->flags & EDGE_EXECUTABLE)
8736 tree arg = PHI_ARG_DEF (phi, i);
8737 value_range vr_arg;
8739 ++edges;
8741 if (TREE_CODE (arg) == SSA_NAME)
8743 vr_arg = *(get_value_range (arg));
8744 /* Do not allow equivalences or symbolic ranges to leak in from
8745 backedges. That creates invalid equivalencies.
8746 See PR53465 and PR54767. */
8747 if (e->flags & EDGE_DFS_BACK)
8749 if (vr_arg.type == VR_RANGE
8750 || vr_arg.type == VR_ANTI_RANGE)
8752 vr_arg.equiv = NULL;
8753 if (symbolic_range_p (&vr_arg))
8755 vr_arg.type = VR_VARYING;
8756 vr_arg.min = NULL_TREE;
8757 vr_arg.max = NULL_TREE;
8761 else
8763 /* If the non-backedge arguments range is VR_VARYING then
8764 we can still try recording a simple equivalence. */
8765 if (vr_arg.type == VR_VARYING)
8767 vr_arg.type = VR_RANGE;
8768 vr_arg.min = arg;
8769 vr_arg.max = arg;
8770 vr_arg.equiv = NULL;
8774 else
8776 if (TREE_OVERFLOW_P (arg))
8777 arg = drop_tree_overflow (arg);
8779 vr_arg.type = VR_RANGE;
8780 vr_arg.min = arg;
8781 vr_arg.max = arg;
8782 vr_arg.equiv = NULL;
8785 if (dump_file && (dump_flags & TDF_DETAILS))
8787 fprintf (dump_file, "\t");
8788 print_generic_expr (dump_file, arg, dump_flags);
8789 fprintf (dump_file, ": ");
8790 dump_value_range (dump_file, &vr_arg);
8791 fprintf (dump_file, "\n");
8794 if (first)
8795 copy_value_range (&vr_result, &vr_arg);
8796 else
8797 vrp_meet (&vr_result, &vr_arg);
8798 first = false;
8800 if (vr_result.type == VR_VARYING)
8801 break;
8805 if (vr_result.type == VR_VARYING)
8806 goto varying;
8807 else if (vr_result.type == VR_UNDEFINED)
8808 goto update_range;
8810 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8811 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8813 /* To prevent infinite iterations in the algorithm, derive ranges
8814 when the new value is slightly bigger or smaller than the
8815 previous one. We don't do this if we have seen a new executable
8816 edge; this helps us avoid an overflow infinity for conditionals
8817 which are not in a loop. If the old value-range was VR_UNDEFINED
8818 use the updated range and iterate one more time. */
8819 if (edges > 0
8820 && gimple_phi_num_args (phi) > 1
8821 && edges == old_edges
8822 && lhs_vr->type != VR_UNDEFINED)
8824 /* Compare old and new ranges, fall back to varying if the
8825 values are not comparable. */
8826 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8827 if (cmp_min == -2)
8828 goto varying;
8829 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8830 if (cmp_max == -2)
8831 goto varying;
8833 /* For non VR_RANGE or for pointers fall back to varying if
8834 the range changed. */
8835 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8836 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8837 && (cmp_min != 0 || cmp_max != 0))
8838 goto varying;
8840 /* If the new minimum is larger than the previous one
8841 retain the old value. If the new minimum value is smaller
8842 than the previous one and not -INF go all the way to -INF + 1.
8843 In the first case, to avoid infinite bouncing between different
8844 minimums, and in the other case to avoid iterating millions of
8845 times to reach -INF. Going to -INF + 1 also lets the following
8846 iteration compute whether there will be any overflow, at the
8847 expense of one additional iteration. */
8848 if (cmp_min < 0)
8849 vr_result.min = lhs_vr->min;
8850 else if (cmp_min > 0
8851 && !vrp_val_is_min (vr_result.min))
8852 vr_result.min
8853 = int_const_binop (PLUS_EXPR,
8854 vrp_val_min (TREE_TYPE (vr_result.min)),
8855 build_int_cst (TREE_TYPE (vr_result.min), 1));
8857 /* Similarly for the maximum value. */
8858 if (cmp_max > 0)
8859 vr_result.max = lhs_vr->max;
8860 else if (cmp_max < 0
8861 && !vrp_val_is_max (vr_result.max))
8862 vr_result.max
8863 = int_const_binop (MINUS_EXPR,
8864 vrp_val_max (TREE_TYPE (vr_result.min)),
8865 build_int_cst (TREE_TYPE (vr_result.min), 1));
8867 /* If we dropped either bound to +-INF then if this is a loop
8868 PHI node SCEV may known more about its value-range. */
8869 if (cmp_min > 0 || cmp_min < 0
8870 || cmp_max < 0 || cmp_max > 0)
8871 goto scev_check;
8873 goto infinite_check;
8876 /* If the new range is different than the previous value, keep
8877 iterating. */
8878 update_range:
8879 if (update_value_range (lhs, &vr_result))
8881 if (dump_file && (dump_flags & TDF_DETAILS))
8883 fprintf (dump_file, "Found new range for ");
8884 print_generic_expr (dump_file, lhs, 0);
8885 fprintf (dump_file, ": ");
8886 dump_value_range (dump_file, &vr_result);
8887 fprintf (dump_file, "\n");
8890 if (vr_result.type == VR_VARYING)
8891 return SSA_PROP_VARYING;
8893 return SSA_PROP_INTERESTING;
8896 /* Nothing changed, don't add outgoing edges. */
8897 return SSA_PROP_NOT_INTERESTING;
8899 varying:
8900 set_value_range_to_varying (&vr_result);
8902 scev_check:
8903 /* If this is a loop PHI node SCEV may known more about its value-range.
8904 scev_check can be reached from two paths, one is a fall through from above
8905 "varying" label, the other is direct goto from code block which tries to
8906 avoid infinite simulation. */
8907 if ((l = loop_containing_stmt (phi))
8908 && l->header == gimple_bb (phi))
8909 adjust_range_with_scev (&vr_result, l, phi, lhs);
8911 infinite_check:
8912 /* If we will end up with a (-INF, +INF) range, set it to
8913 VARYING. Same if the previous max value was invalid for
8914 the type and we end up with vr_result.min > vr_result.max. */
8915 if ((vr_result.type == VR_RANGE || vr_result.type == VR_ANTI_RANGE)
8916 && !((vrp_val_is_max (vr_result.max) && vrp_val_is_min (vr_result.min))
8917 || compare_values (vr_result.min, vr_result.max) > 0))
8918 goto update_range;
8920 /* No match found. Set the LHS to VARYING. */
8921 set_value_range_to_varying (lhs_vr);
8922 return SSA_PROP_VARYING;
8925 /* Simplify boolean operations if the source is known
8926 to be already a boolean. */
8927 static bool
8928 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
8930 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8931 tree lhs, op0, op1;
8932 bool need_conversion;
8934 /* We handle only !=/== case here. */
8935 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8937 op0 = gimple_assign_rhs1 (stmt);
8938 if (!op_with_boolean_value_range_p (op0))
8939 return false;
8941 op1 = gimple_assign_rhs2 (stmt);
8942 if (!op_with_boolean_value_range_p (op1))
8943 return false;
8945 /* Reduce number of cases to handle to NE_EXPR. As there is no
8946 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8947 if (rhs_code == EQ_EXPR)
8949 if (TREE_CODE (op1) == INTEGER_CST)
8950 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8951 build_int_cst (TREE_TYPE (op1), 1));
8952 else
8953 return false;
8956 lhs = gimple_assign_lhs (stmt);
8957 need_conversion
8958 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8960 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8961 if (need_conversion
8962 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8963 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8964 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8965 return false;
8967 /* For A != 0 we can substitute A itself. */
8968 if (integer_zerop (op1))
8969 gimple_assign_set_rhs_with_ops (gsi,
8970 need_conversion
8971 ? NOP_EXPR : TREE_CODE (op0), op0);
8972 /* For A != B we substitute A ^ B. Either with conversion. */
8973 else if (need_conversion)
8975 tree tem = make_ssa_name (TREE_TYPE (op0));
8976 gassign *newop
8977 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
8978 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8979 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
8981 /* Or without. */
8982 else
8983 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8984 update_stmt (gsi_stmt (*gsi));
8986 return true;
8989 /* Simplify a division or modulo operator to a right shift or
8990 bitwise and if the first operand is unsigned or is greater
8991 than zero and the second operand is an exact power of two.
8992 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
8993 into just op0 if op0's range is known to be a subset of
8994 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
8995 modulo. */
8997 static bool
8998 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9000 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9001 tree val = NULL;
9002 tree op0 = gimple_assign_rhs1 (stmt);
9003 tree op1 = gimple_assign_rhs2 (stmt);
9004 value_range *vr = get_value_range (op0);
9006 if (rhs_code == TRUNC_MOD_EXPR
9007 && TREE_CODE (op1) == INTEGER_CST
9008 && tree_int_cst_sgn (op1) == 1
9009 && range_int_cst_p (vr)
9010 && tree_int_cst_lt (vr->max, op1))
9012 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9013 || tree_int_cst_sgn (vr->min) >= 0
9014 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9015 vr->min))
9017 /* If op0 already has the range op0 % op1 has,
9018 then TRUNC_MOD_EXPR won't change anything. */
9019 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9020 gimple_assign_set_rhs_from_tree (&gsi, op0);
9021 update_stmt (stmt);
9022 return true;
9026 if (!integer_pow2p (op1))
9028 /* X % -Y can be only optimized into X % Y either if
9029 X is not INT_MIN, or Y is not -1. Fold it now, as after
9030 remove_range_assertions the range info might be not available
9031 anymore. */
9032 if (rhs_code == TRUNC_MOD_EXPR
9033 && fold_stmt (gsi, follow_single_use_edges))
9034 return true;
9035 return false;
9038 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9039 val = integer_one_node;
9040 else
9042 bool sop = false;
9044 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9046 if (val
9047 && sop
9048 && integer_onep (val)
9049 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9051 location_t location;
9053 if (!gimple_has_location (stmt))
9054 location = input_location;
9055 else
9056 location = gimple_location (stmt);
9057 warning_at (location, OPT_Wstrict_overflow,
9058 "assuming signed overflow does not occur when "
9059 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9063 if (val && integer_onep (val))
9065 tree t;
9067 if (rhs_code == TRUNC_DIV_EXPR)
9069 t = build_int_cst (integer_type_node, tree_log2 (op1));
9070 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9071 gimple_assign_set_rhs1 (stmt, op0);
9072 gimple_assign_set_rhs2 (stmt, t);
9074 else
9076 t = build_int_cst (TREE_TYPE (op1), 1);
9077 t = int_const_binop (MINUS_EXPR, op1, t);
9078 t = fold_convert (TREE_TYPE (op0), t);
9080 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9081 gimple_assign_set_rhs1 (stmt, op0);
9082 gimple_assign_set_rhs2 (stmt, t);
9085 update_stmt (stmt);
9086 return true;
9089 return false;
9092 /* Simplify a min or max if the ranges of the two operands are
9093 disjoint. Return true if we do simplify. */
9095 static bool
9096 simplify_min_or_max_using_ranges (gimple *stmt)
9098 tree op0 = gimple_assign_rhs1 (stmt);
9099 tree op1 = gimple_assign_rhs2 (stmt);
9100 bool sop = false;
9101 tree val;
9103 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9104 (LE_EXPR, op0, op1, &sop));
9105 if (!val)
9107 sop = false;
9108 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9109 (LT_EXPR, op0, op1, &sop));
9112 if (val)
9114 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9116 location_t location;
9118 if (!gimple_has_location (stmt))
9119 location = input_location;
9120 else
9121 location = gimple_location (stmt);
9122 warning_at (location, OPT_Wstrict_overflow,
9123 "assuming signed overflow does not occur when "
9124 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9127 /* VAL == TRUE -> OP0 < or <= op1
9128 VAL == FALSE -> OP0 > or >= op1. */
9129 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9130 == integer_zerop (val)) ? op0 : op1;
9131 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9132 gimple_assign_set_rhs_from_tree (&gsi, res);
9133 update_stmt (stmt);
9134 return true;
9137 return false;
9140 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9141 ABS_EXPR. If the operand is <= 0, then simplify the
9142 ABS_EXPR into a NEGATE_EXPR. */
9144 static bool
9145 simplify_abs_using_ranges (gimple *stmt)
9147 tree op = gimple_assign_rhs1 (stmt);
9148 value_range *vr = get_value_range (op);
9150 if (vr)
9152 tree val = NULL;
9153 bool sop = false;
9155 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9156 if (!val)
9158 /* The range is neither <= 0 nor > 0. Now see if it is
9159 either < 0 or >= 0. */
9160 sop = false;
9161 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9162 &sop);
9165 if (val)
9167 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9169 location_t location;
9171 if (!gimple_has_location (stmt))
9172 location = input_location;
9173 else
9174 location = gimple_location (stmt);
9175 warning_at (location, OPT_Wstrict_overflow,
9176 "assuming signed overflow does not occur when "
9177 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9180 gimple_assign_set_rhs1 (stmt, op);
9181 if (integer_zerop (val))
9182 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9183 else
9184 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9185 update_stmt (stmt);
9186 return true;
9190 return false;
9193 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9194 If all the bits that are being cleared by & are already
9195 known to be zero from VR, or all the bits that are being
9196 set by | are already known to be one from VR, the bit
9197 operation is redundant. */
9199 static bool
9200 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9202 tree op0 = gimple_assign_rhs1 (stmt);
9203 tree op1 = gimple_assign_rhs2 (stmt);
9204 tree op = NULL_TREE;
9205 value_range vr0 = VR_INITIALIZER;
9206 value_range vr1 = VR_INITIALIZER;
9207 wide_int may_be_nonzero0, may_be_nonzero1;
9208 wide_int must_be_nonzero0, must_be_nonzero1;
9209 wide_int mask;
9211 if (TREE_CODE (op0) == SSA_NAME)
9212 vr0 = *(get_value_range (op0));
9213 else if (is_gimple_min_invariant (op0))
9214 set_value_range_to_value (&vr0, op0, NULL);
9215 else
9216 return false;
9218 if (TREE_CODE (op1) == SSA_NAME)
9219 vr1 = *(get_value_range (op1));
9220 else if (is_gimple_min_invariant (op1))
9221 set_value_range_to_value (&vr1, op1, NULL);
9222 else
9223 return false;
9225 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9226 &must_be_nonzero0))
9227 return false;
9228 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9229 &must_be_nonzero1))
9230 return false;
9232 switch (gimple_assign_rhs_code (stmt))
9234 case BIT_AND_EXPR:
9235 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9236 if (mask == 0)
9238 op = op0;
9239 break;
9241 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9242 if (mask == 0)
9244 op = op1;
9245 break;
9247 break;
9248 case BIT_IOR_EXPR:
9249 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9250 if (mask == 0)
9252 op = op1;
9253 break;
9255 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9256 if (mask == 0)
9258 op = op0;
9259 break;
9261 break;
9262 default:
9263 gcc_unreachable ();
9266 if (op == NULL_TREE)
9267 return false;
9269 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9270 update_stmt (gsi_stmt (*gsi));
9271 return true;
9274 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9275 a known value range VR.
9277 If there is one and only one value which will satisfy the
9278 conditional, then return that value. Else return NULL.
9280 If signed overflow must be undefined for the value to satisfy
9281 the conditional, then set *STRICT_OVERFLOW_P to true. */
9283 static tree
9284 test_for_singularity (enum tree_code cond_code, tree op0,
9285 tree op1, value_range *vr,
9286 bool *strict_overflow_p)
9288 tree min = NULL;
9289 tree max = NULL;
9291 /* Extract minimum/maximum values which satisfy the conditional as it was
9292 written. */
9293 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9295 /* This should not be negative infinity; there is no overflow
9296 here. */
9297 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9299 max = op1;
9300 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9302 tree one = build_int_cst (TREE_TYPE (op0), 1);
9303 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9304 if (EXPR_P (max))
9305 TREE_NO_WARNING (max) = 1;
9308 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9310 /* This should not be positive infinity; there is no overflow
9311 here. */
9312 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9314 min = op1;
9315 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9317 tree one = build_int_cst (TREE_TYPE (op0), 1);
9318 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9319 if (EXPR_P (min))
9320 TREE_NO_WARNING (min) = 1;
9324 /* Now refine the minimum and maximum values using any
9325 value range information we have for op0. */
9326 if (min && max)
9328 if (compare_values (vr->min, min) == 1)
9329 min = vr->min;
9330 if (compare_values (vr->max, max) == -1)
9331 max = vr->max;
9333 /* If the new min/max values have converged to a single value,
9334 then there is only one value which can satisfy the condition,
9335 return that value. */
9336 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9338 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9339 && is_overflow_infinity (vr->max))
9340 *strict_overflow_p = true;
9341 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9342 && is_overflow_infinity (vr->min))
9343 *strict_overflow_p = true;
9345 return min;
9348 return NULL;
9351 /* Return whether the value range *VR fits in an integer type specified
9352 by PRECISION and UNSIGNED_P. */
9354 static bool
9355 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9357 tree src_type;
9358 unsigned src_precision;
9359 widest_int tem;
9360 signop src_sgn;
9362 /* We can only handle integral and pointer types. */
9363 src_type = TREE_TYPE (vr->min);
9364 if (!INTEGRAL_TYPE_P (src_type)
9365 && !POINTER_TYPE_P (src_type))
9366 return false;
9368 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9369 and so is an identity transform. */
9370 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9371 src_sgn = TYPE_SIGN (src_type);
9372 if ((src_precision < dest_precision
9373 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9374 || (src_precision == dest_precision && src_sgn == dest_sgn))
9375 return true;
9377 /* Now we can only handle ranges with constant bounds. */
9378 if (vr->type != VR_RANGE
9379 || TREE_CODE (vr->min) != INTEGER_CST
9380 || TREE_CODE (vr->max) != INTEGER_CST)
9381 return false;
9383 /* For sign changes, the MSB of the wide_int has to be clear.
9384 An unsigned value with its MSB set cannot be represented by
9385 a signed wide_int, while a negative value cannot be represented
9386 by an unsigned wide_int. */
9387 if (src_sgn != dest_sgn
9388 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9389 return false;
9391 /* Then we can perform the conversion on both ends and compare
9392 the result for equality. */
9393 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9394 if (tem != wi::to_widest (vr->min))
9395 return false;
9396 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9397 if (tem != wi::to_widest (vr->max))
9398 return false;
9400 return true;
9403 /* Simplify a conditional using a relational operator to an equality
9404 test if the range information indicates only one value can satisfy
9405 the original conditional. */
9407 static bool
9408 simplify_cond_using_ranges (gcond *stmt)
9410 tree op0 = gimple_cond_lhs (stmt);
9411 tree op1 = gimple_cond_rhs (stmt);
9412 enum tree_code cond_code = gimple_cond_code (stmt);
9414 if (cond_code != NE_EXPR
9415 && cond_code != EQ_EXPR
9416 && TREE_CODE (op0) == SSA_NAME
9417 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9418 && is_gimple_min_invariant (op1))
9420 value_range *vr = get_value_range (op0);
9422 /* If we have range information for OP0, then we might be
9423 able to simplify this conditional. */
9424 if (vr->type == VR_RANGE)
9426 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9427 bool sop = false;
9428 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9430 if (new_tree
9431 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9433 if (dump_file)
9435 fprintf (dump_file, "Simplified relational ");
9436 print_gimple_stmt (dump_file, stmt, 0, 0);
9437 fprintf (dump_file, " into ");
9440 gimple_cond_set_code (stmt, EQ_EXPR);
9441 gimple_cond_set_lhs (stmt, op0);
9442 gimple_cond_set_rhs (stmt, new_tree);
9444 update_stmt (stmt);
9446 if (dump_file)
9448 print_gimple_stmt (dump_file, stmt, 0, 0);
9449 fprintf (dump_file, "\n");
9452 if (sop && issue_strict_overflow_warning (wc))
9454 location_t location = input_location;
9455 if (gimple_has_location (stmt))
9456 location = gimple_location (stmt);
9458 warning_at (location, OPT_Wstrict_overflow,
9459 "assuming signed overflow does not occur when "
9460 "simplifying conditional");
9463 return true;
9466 /* Try again after inverting the condition. We only deal
9467 with integral types here, so no need to worry about
9468 issues with inverting FP comparisons. */
9469 sop = false;
9470 new_tree = test_for_singularity
9471 (invert_tree_comparison (cond_code, false),
9472 op0, op1, vr, &sop);
9474 if (new_tree
9475 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9477 if (dump_file)
9479 fprintf (dump_file, "Simplified relational ");
9480 print_gimple_stmt (dump_file, stmt, 0, 0);
9481 fprintf (dump_file, " into ");
9484 gimple_cond_set_code (stmt, NE_EXPR);
9485 gimple_cond_set_lhs (stmt, op0);
9486 gimple_cond_set_rhs (stmt, new_tree);
9488 update_stmt (stmt);
9490 if (dump_file)
9492 print_gimple_stmt (dump_file, stmt, 0, 0);
9493 fprintf (dump_file, "\n");
9496 if (sop && issue_strict_overflow_warning (wc))
9498 location_t location = input_location;
9499 if (gimple_has_location (stmt))
9500 location = gimple_location (stmt);
9502 warning_at (location, OPT_Wstrict_overflow,
9503 "assuming signed overflow does not occur when "
9504 "simplifying conditional");
9507 return true;
9512 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9513 see if OP0 was set by a type conversion where the source of
9514 the conversion is another SSA_NAME with a range that fits
9515 into the range of OP0's type.
9517 If so, the conversion is redundant as the earlier SSA_NAME can be
9518 used for the comparison directly if we just massage the constant in the
9519 comparison. */
9520 if (TREE_CODE (op0) == SSA_NAME
9521 && TREE_CODE (op1) == INTEGER_CST)
9523 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9524 tree innerop;
9526 if (!is_gimple_assign (def_stmt)
9527 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9528 return false;
9530 innerop = gimple_assign_rhs1 (def_stmt);
9532 if (TREE_CODE (innerop) == SSA_NAME
9533 && !POINTER_TYPE_P (TREE_TYPE (innerop))
9534 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
9535 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
9537 value_range *vr = get_value_range (innerop);
9539 if (range_int_cst_p (vr)
9540 && range_fits_type_p (vr,
9541 TYPE_PRECISION (TREE_TYPE (op0)),
9542 TYPE_SIGN (TREE_TYPE (op0)))
9543 && int_fits_type_p (op1, TREE_TYPE (innerop))
9544 /* The range must not have overflowed, or if it did overflow
9545 we must not be wrapping/trapping overflow and optimizing
9546 with strict overflow semantics. */
9547 && ((!is_negative_overflow_infinity (vr->min)
9548 && !is_positive_overflow_infinity (vr->max))
9549 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9551 /* If the range overflowed and the user has asked for warnings
9552 when strict overflow semantics were used to optimize code,
9553 issue an appropriate warning. */
9554 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9555 && (is_negative_overflow_infinity (vr->min)
9556 || is_positive_overflow_infinity (vr->max))
9557 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9559 location_t location;
9561 if (!gimple_has_location (stmt))
9562 location = input_location;
9563 else
9564 location = gimple_location (stmt);
9565 warning_at (location, OPT_Wstrict_overflow,
9566 "assuming signed overflow does not occur when "
9567 "simplifying conditional");
9570 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9571 gimple_cond_set_lhs (stmt, innerop);
9572 gimple_cond_set_rhs (stmt, newconst);
9573 return true;
9578 return false;
9581 /* Simplify a switch statement using the value range of the switch
9582 argument. */
9584 static bool
9585 simplify_switch_using_ranges (gswitch *stmt)
9587 tree op = gimple_switch_index (stmt);
9588 value_range *vr;
9589 bool take_default;
9590 edge e;
9591 edge_iterator ei;
9592 size_t i = 0, j = 0, n, n2;
9593 tree vec2;
9594 switch_update su;
9595 size_t k = 1, l = 0;
9597 if (TREE_CODE (op) == SSA_NAME)
9599 vr = get_value_range (op);
9601 /* We can only handle integer ranges. */
9602 if ((vr->type != VR_RANGE
9603 && vr->type != VR_ANTI_RANGE)
9604 || symbolic_range_p (vr))
9605 return false;
9607 /* Find case label for min/max of the value range. */
9608 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9610 else if (TREE_CODE (op) == INTEGER_CST)
9612 take_default = !find_case_label_index (stmt, 1, op, &i);
9613 if (take_default)
9615 i = 1;
9616 j = 0;
9618 else
9620 j = i;
9623 else
9624 return false;
9626 n = gimple_switch_num_labels (stmt);
9628 /* Bail out if this is just all edges taken. */
9629 if (i == 1
9630 && j == n - 1
9631 && take_default)
9632 return false;
9634 /* Build a new vector of taken case labels. */
9635 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9636 n2 = 0;
9638 /* Add the default edge, if necessary. */
9639 if (take_default)
9640 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9642 for (; i <= j; ++i, ++n2)
9643 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9645 for (; k <= l; ++k, ++n2)
9646 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9648 /* Mark needed edges. */
9649 for (i = 0; i < n2; ++i)
9651 e = find_edge (gimple_bb (stmt),
9652 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9653 e->aux = (void *)-1;
9656 /* Queue not needed edges for later removal. */
9657 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9659 if (e->aux == (void *)-1)
9661 e->aux = NULL;
9662 continue;
9665 if (dump_file && (dump_flags & TDF_DETAILS))
9667 fprintf (dump_file, "removing unreachable case label\n");
9669 to_remove_edges.safe_push (e);
9670 e->flags &= ~EDGE_EXECUTABLE;
9673 /* And queue an update for the stmt. */
9674 su.stmt = stmt;
9675 su.vec = vec2;
9676 to_update_switch_stmts.safe_push (su);
9677 return false;
9680 /* Simplify an integral conversion from an SSA name in STMT. */
9682 static bool
9683 simplify_conversion_using_ranges (gimple *stmt)
9685 tree innerop, middleop, finaltype;
9686 gimple *def_stmt;
9687 value_range *innervr;
9688 signop inner_sgn, middle_sgn, final_sgn;
9689 unsigned inner_prec, middle_prec, final_prec;
9690 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9692 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9693 if (!INTEGRAL_TYPE_P (finaltype))
9694 return false;
9695 middleop = gimple_assign_rhs1 (stmt);
9696 def_stmt = SSA_NAME_DEF_STMT (middleop);
9697 if (!is_gimple_assign (def_stmt)
9698 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9699 return false;
9700 innerop = gimple_assign_rhs1 (def_stmt);
9701 if (TREE_CODE (innerop) != SSA_NAME
9702 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9703 return false;
9705 /* Get the value-range of the inner operand. */
9706 innervr = get_value_range (innerop);
9707 if (innervr->type != VR_RANGE
9708 || TREE_CODE (innervr->min) != INTEGER_CST
9709 || TREE_CODE (innervr->max) != INTEGER_CST)
9710 return false;
9712 /* Simulate the conversion chain to check if the result is equal if
9713 the middle conversion is removed. */
9714 innermin = wi::to_widest (innervr->min);
9715 innermax = wi::to_widest (innervr->max);
9717 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9718 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9719 final_prec = TYPE_PRECISION (finaltype);
9721 /* If the first conversion is not injective, the second must not
9722 be widening. */
9723 if (wi::gtu_p (innermax - innermin,
9724 wi::mask <widest_int> (middle_prec, false))
9725 && middle_prec < final_prec)
9726 return false;
9727 /* We also want a medium value so that we can track the effect that
9728 narrowing conversions with sign change have. */
9729 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9730 if (inner_sgn == UNSIGNED)
9731 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9732 else
9733 innermed = 0;
9734 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9735 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9736 innermed = innermin;
9738 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9739 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9740 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9741 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9743 /* Require that the final conversion applied to both the original
9744 and the intermediate range produces the same result. */
9745 final_sgn = TYPE_SIGN (finaltype);
9746 if (wi::ext (middlemin, final_prec, final_sgn)
9747 != wi::ext (innermin, final_prec, final_sgn)
9748 || wi::ext (middlemed, final_prec, final_sgn)
9749 != wi::ext (innermed, final_prec, final_sgn)
9750 || wi::ext (middlemax, final_prec, final_sgn)
9751 != wi::ext (innermax, final_prec, final_sgn))
9752 return false;
9754 gimple_assign_set_rhs1 (stmt, innerop);
9755 update_stmt (stmt);
9756 return true;
9759 /* Simplify a conversion from integral SSA name to float in STMT. */
9761 static bool
9762 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
9763 gimple *stmt)
9765 tree rhs1 = gimple_assign_rhs1 (stmt);
9766 value_range *vr = get_value_range (rhs1);
9767 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9768 machine_mode mode;
9769 tree tem;
9770 gassign *conv;
9772 /* We can only handle constant ranges. */
9773 if (vr->type != VR_RANGE
9774 || TREE_CODE (vr->min) != INTEGER_CST
9775 || TREE_CODE (vr->max) != INTEGER_CST)
9776 return false;
9778 /* First check if we can use a signed type in place of an unsigned. */
9779 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9780 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9781 != CODE_FOR_nothing)
9782 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9783 mode = TYPE_MODE (TREE_TYPE (rhs1));
9784 /* If we can do the conversion in the current input mode do nothing. */
9785 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9786 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9787 return false;
9788 /* Otherwise search for a mode we can use, starting from the narrowest
9789 integer mode available. */
9790 else
9792 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9795 /* If we cannot do a signed conversion to float from mode
9796 or if the value-range does not fit in the signed type
9797 try with a wider mode. */
9798 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9799 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9800 break;
9802 mode = GET_MODE_WIDER_MODE (mode);
9803 /* But do not widen the input. Instead leave that to the
9804 optabs expansion code. */
9805 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9806 return false;
9808 while (mode != VOIDmode);
9809 if (mode == VOIDmode)
9810 return false;
9813 /* It works, insert a truncation or sign-change before the
9814 float conversion. */
9815 tem = make_ssa_name (build_nonstandard_integer_type
9816 (GET_MODE_PRECISION (mode), 0));
9817 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9818 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9819 gimple_assign_set_rhs1 (stmt, tem);
9820 update_stmt (stmt);
9822 return true;
9825 /* Simplify an internal fn call using ranges if possible. */
9827 static bool
9828 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9830 enum tree_code subcode;
9831 bool is_ubsan = false;
9832 bool ovf = false;
9833 switch (gimple_call_internal_fn (stmt))
9835 case IFN_UBSAN_CHECK_ADD:
9836 subcode = PLUS_EXPR;
9837 is_ubsan = true;
9838 break;
9839 case IFN_UBSAN_CHECK_SUB:
9840 subcode = MINUS_EXPR;
9841 is_ubsan = true;
9842 break;
9843 case IFN_UBSAN_CHECK_MUL:
9844 subcode = MULT_EXPR;
9845 is_ubsan = true;
9846 break;
9847 case IFN_ADD_OVERFLOW:
9848 subcode = PLUS_EXPR;
9849 break;
9850 case IFN_SUB_OVERFLOW:
9851 subcode = MINUS_EXPR;
9852 break;
9853 case IFN_MUL_OVERFLOW:
9854 subcode = MULT_EXPR;
9855 break;
9856 default:
9857 return false;
9860 tree op0 = gimple_call_arg (stmt, 0);
9861 tree op1 = gimple_call_arg (stmt, 1);
9862 tree type;
9863 if (is_ubsan)
9864 type = TREE_TYPE (op0);
9865 else if (gimple_call_lhs (stmt) == NULL_TREE)
9866 return false;
9867 else
9868 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9869 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9870 || (is_ubsan && ovf))
9871 return false;
9873 gimple *g;
9874 location_t loc = gimple_location (stmt);
9875 if (is_ubsan)
9876 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9877 else
9879 int prec = TYPE_PRECISION (type);
9880 tree utype = type;
9881 if (ovf
9882 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9883 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9884 utype = build_nonstandard_integer_type (prec, 1);
9885 if (TREE_CODE (op0) == INTEGER_CST)
9886 op0 = fold_convert (utype, op0);
9887 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9889 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9890 gimple_set_location (g, loc);
9891 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9892 op0 = gimple_assign_lhs (g);
9894 if (TREE_CODE (op1) == INTEGER_CST)
9895 op1 = fold_convert (utype, op1);
9896 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9898 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9899 gimple_set_location (g, loc);
9900 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9901 op1 = gimple_assign_lhs (g);
9903 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9904 gimple_set_location (g, loc);
9905 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9906 if (utype != type)
9908 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9909 gimple_assign_lhs (g));
9910 gimple_set_location (g, loc);
9911 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9913 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9914 gimple_assign_lhs (g),
9915 build_int_cst (type, ovf));
9917 gimple_set_location (g, loc);
9918 gsi_replace (gsi, g, false);
9919 return true;
9922 /* Simplify STMT using ranges if possible. */
9924 static bool
9925 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9927 gimple *stmt = gsi_stmt (*gsi);
9928 if (is_gimple_assign (stmt))
9930 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9931 tree rhs1 = gimple_assign_rhs1 (stmt);
9933 switch (rhs_code)
9935 case EQ_EXPR:
9936 case NE_EXPR:
9937 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9938 if the RHS is zero or one, and the LHS are known to be boolean
9939 values. */
9940 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9941 return simplify_truth_ops_using_ranges (gsi, stmt);
9942 break;
9944 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9945 and BIT_AND_EXPR respectively if the first operand is greater
9946 than zero and the second operand is an exact power of two.
9947 Also optimize TRUNC_MOD_EXPR away if the second operand is
9948 constant and the first operand already has the right value
9949 range. */
9950 case TRUNC_DIV_EXPR:
9951 case TRUNC_MOD_EXPR:
9952 if (TREE_CODE (rhs1) == SSA_NAME
9953 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9954 return simplify_div_or_mod_using_ranges (gsi, stmt);
9955 break;
9957 /* Transform ABS (X) into X or -X as appropriate. */
9958 case ABS_EXPR:
9959 if (TREE_CODE (rhs1) == SSA_NAME
9960 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9961 return simplify_abs_using_ranges (stmt);
9962 break;
9964 case BIT_AND_EXPR:
9965 case BIT_IOR_EXPR:
9966 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9967 if all the bits being cleared are already cleared or
9968 all the bits being set are already set. */
9969 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9970 return simplify_bit_ops_using_ranges (gsi, stmt);
9971 break;
9973 CASE_CONVERT:
9974 if (TREE_CODE (rhs1) == SSA_NAME
9975 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9976 return simplify_conversion_using_ranges (stmt);
9977 break;
9979 case FLOAT_EXPR:
9980 if (TREE_CODE (rhs1) == SSA_NAME
9981 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9982 return simplify_float_conversion_using_ranges (gsi, stmt);
9983 break;
9985 case MIN_EXPR:
9986 case MAX_EXPR:
9987 return simplify_min_or_max_using_ranges (stmt);
9988 break;
9990 default:
9991 break;
9994 else if (gimple_code (stmt) == GIMPLE_COND)
9995 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9996 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9997 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
9998 else if (is_gimple_call (stmt)
9999 && gimple_call_internal_p (stmt))
10000 return simplify_internal_call_using_ranges (gsi, stmt);
10002 return false;
10005 /* If the statement pointed by SI has a predicate whose value can be
10006 computed using the value range information computed by VRP, compute
10007 its value and return true. Otherwise, return false. */
10009 static bool
10010 fold_predicate_in (gimple_stmt_iterator *si)
10012 bool assignment_p = false;
10013 tree val;
10014 gimple *stmt = gsi_stmt (*si);
10016 if (is_gimple_assign (stmt)
10017 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10019 assignment_p = true;
10020 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10021 gimple_assign_rhs1 (stmt),
10022 gimple_assign_rhs2 (stmt),
10023 stmt);
10025 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10026 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10027 gimple_cond_lhs (cond_stmt),
10028 gimple_cond_rhs (cond_stmt),
10029 stmt);
10030 else
10031 return false;
10033 if (val)
10035 if (assignment_p)
10036 val = fold_convert (gimple_expr_type (stmt), val);
10038 if (dump_file)
10040 fprintf (dump_file, "Folding predicate ");
10041 print_gimple_expr (dump_file, stmt, 0, 0);
10042 fprintf (dump_file, " to ");
10043 print_generic_expr (dump_file, val, 0);
10044 fprintf (dump_file, "\n");
10047 if (is_gimple_assign (stmt))
10048 gimple_assign_set_rhs_from_tree (si, val);
10049 else
10051 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10052 gcond *cond_stmt = as_a <gcond *> (stmt);
10053 if (integer_zerop (val))
10054 gimple_cond_make_false (cond_stmt);
10055 else if (integer_onep (val))
10056 gimple_cond_make_true (cond_stmt);
10057 else
10058 gcc_unreachable ();
10061 return true;
10064 return false;
10067 /* Callback for substitute_and_fold folding the stmt at *SI. */
10069 static bool
10070 vrp_fold_stmt (gimple_stmt_iterator *si)
10072 if (fold_predicate_in (si))
10073 return true;
10075 return simplify_stmt_using_ranges (si);
10078 /* Unwindable const/copy equivalences. */
10079 const_and_copies *equiv_stack;
10081 /* A trivial wrapper so that we can present the generic jump threading
10082 code with a simple API for simplifying statements. STMT is the
10083 statement we want to simplify, WITHIN_STMT provides the location
10084 for any overflow warnings. */
10086 static tree
10087 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10088 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED)
10090 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10091 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10092 gimple_cond_lhs (cond_stmt),
10093 gimple_cond_rhs (cond_stmt),
10094 within_stmt);
10096 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10098 value_range new_vr = VR_INITIALIZER;
10099 tree lhs = gimple_assign_lhs (assign_stmt);
10101 if (TREE_CODE (lhs) == SSA_NAME
10102 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10103 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10105 extract_range_from_assignment (&new_vr, assign_stmt);
10106 if (range_int_cst_singleton_p (&new_vr))
10107 return new_vr.min;
10111 return NULL_TREE;
10114 /* Blocks which have more than one predecessor and more than
10115 one successor present jump threading opportunities, i.e.,
10116 when the block is reached from a specific predecessor, we
10117 may be able to determine which of the outgoing edges will
10118 be traversed. When this optimization applies, we are able
10119 to avoid conditionals at runtime and we may expose secondary
10120 optimization opportunities.
10122 This routine is effectively a driver for the generic jump
10123 threading code. It basically just presents the generic code
10124 with edges that may be suitable for jump threading.
10126 Unlike DOM, we do not iterate VRP if jump threading was successful.
10127 While iterating may expose new opportunities for VRP, it is expected
10128 those opportunities would be very limited and the compile time cost
10129 to expose those opportunities would be significant.
10131 As jump threading opportunities are discovered, they are registered
10132 for later realization. */
10134 static void
10135 identify_jump_threads (void)
10137 basic_block bb;
10138 gcond *dummy;
10139 int i;
10140 edge e;
10142 /* Ugh. When substituting values earlier in this pass we can
10143 wipe the dominance information. So rebuild the dominator
10144 information as we need it within the jump threading code. */
10145 calculate_dominance_info (CDI_DOMINATORS);
10147 /* We do not allow VRP information to be used for jump threading
10148 across a back edge in the CFG. Otherwise it becomes too
10149 difficult to avoid eliminating loop exit tests. Of course
10150 EDGE_DFS_BACK is not accurate at this time so we have to
10151 recompute it. */
10152 mark_dfs_back_edges ();
10154 /* Do not thread across edges we are about to remove. Just marking
10155 them as EDGE_IGNORE will do. */
10156 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10157 e->flags |= EDGE_IGNORE;
10159 /* Allocate our unwinder stack to unwind any temporary equivalences
10160 that might be recorded. */
10161 equiv_stack = new const_and_copies ();
10163 /* To avoid lots of silly node creation, we create a single
10164 conditional and just modify it in-place when attempting to
10165 thread jumps. */
10166 dummy = gimple_build_cond (EQ_EXPR,
10167 integer_zero_node, integer_zero_node,
10168 NULL, NULL);
10170 /* Walk through all the blocks finding those which present a
10171 potential jump threading opportunity. We could set this up
10172 as a dominator walker and record data during the walk, but
10173 I doubt it's worth the effort for the classes of jump
10174 threading opportunities we are trying to identify at this
10175 point in compilation. */
10176 FOR_EACH_BB_FN (bb, cfun)
10178 gimple *last;
10180 /* If the generic jump threading code does not find this block
10181 interesting, then there is nothing to do. */
10182 if (! potentially_threadable_block (bb))
10183 continue;
10185 last = last_stmt (bb);
10187 /* We're basically looking for a switch or any kind of conditional with
10188 integral or pointer type arguments. Note the type of the second
10189 argument will be the same as the first argument, so no need to
10190 check it explicitly.
10192 We also handle the case where there are no statements in the
10193 block. This come up with forwarder blocks that are not
10194 optimized away because they lead to a loop header. But we do
10195 want to thread through them as we can sometimes thread to the
10196 loop exit which is obviously profitable. */
10197 if (!last
10198 || gimple_code (last) == GIMPLE_SWITCH
10199 || (gimple_code (last) == GIMPLE_COND
10200 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10201 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10202 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10203 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10204 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10206 edge_iterator ei;
10208 /* We've got a block with multiple predecessors and multiple
10209 successors which also ends in a suitable conditional or
10210 switch statement. For each predecessor, see if we can thread
10211 it to a specific successor. */
10212 FOR_EACH_EDGE (e, ei, bb->preds)
10214 /* Do not thread across edges marked to ignoreor abnormal
10215 edges in the CFG. */
10216 if (e->flags & (EDGE_IGNORE | EDGE_COMPLEX))
10217 continue;
10219 thread_across_edge (dummy, e, true, equiv_stack, NULL,
10220 simplify_stmt_for_jump_threading);
10225 /* Clear EDGE_IGNORE. */
10226 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10227 e->flags &= ~EDGE_IGNORE;
10229 /* We do not actually update the CFG or SSA graphs at this point as
10230 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10231 handle ASSERT_EXPRs gracefully. */
10234 /* We identified all the jump threading opportunities earlier, but could
10235 not transform the CFG at that time. This routine transforms the
10236 CFG and arranges for the dominator tree to be rebuilt if necessary.
10238 Note the SSA graph update will occur during the normal TODO
10239 processing by the pass manager. */
10240 static void
10241 finalize_jump_threads (void)
10243 thread_through_all_blocks (false);
10244 delete equiv_stack;
10248 /* Traverse all the blocks folding conditionals with known ranges. */
10250 static void
10251 vrp_finalize (bool warn_array_bounds_p)
10253 size_t i;
10255 values_propagated = true;
10257 if (dump_file)
10259 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10260 dump_all_value_ranges (dump_file);
10261 fprintf (dump_file, "\n");
10264 /* Set value range to non pointer SSA_NAMEs. */
10265 for (i = 0; i < num_vr_values; i++)
10266 if (vr_value[i])
10268 tree name = ssa_name (i);
10270 if (!name
10271 || POINTER_TYPE_P (TREE_TYPE (name))
10272 || (vr_value[i]->type == VR_VARYING)
10273 || (vr_value[i]->type == VR_UNDEFINED))
10274 continue;
10276 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10277 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10278 && (vr_value[i]->type == VR_RANGE
10279 || vr_value[i]->type == VR_ANTI_RANGE))
10280 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10281 vr_value[i]->max);
10284 substitute_and_fold (op_with_constant_singleton_value_range,
10285 vrp_fold_stmt, false);
10287 if (warn_array_bounds && warn_array_bounds_p)
10288 check_all_array_refs ();
10290 /* We must identify jump threading opportunities before we release
10291 the datastructures built by VRP. */
10292 identify_jump_threads ();
10294 /* Free allocated memory. */
10295 for (i = 0; i < num_vr_values; i++)
10296 if (vr_value[i])
10298 BITMAP_FREE (vr_value[i]->equiv);
10299 free (vr_value[i]);
10302 free (vr_value);
10303 free (vr_phi_edge_counts);
10305 /* So that we can distinguish between VRP data being available
10306 and not available. */
10307 vr_value = NULL;
10308 vr_phi_edge_counts = NULL;
10312 /* Main entry point to VRP (Value Range Propagation). This pass is
10313 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10314 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10315 Programming Language Design and Implementation, pp. 67-78, 1995.
10316 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10318 This is essentially an SSA-CCP pass modified to deal with ranges
10319 instead of constants.
10321 While propagating ranges, we may find that two or more SSA name
10322 have equivalent, though distinct ranges. For instance,
10324 1 x_9 = p_3->a;
10325 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10326 3 if (p_4 == q_2)
10327 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10328 5 endif
10329 6 if (q_2)
10331 In the code above, pointer p_5 has range [q_2, q_2], but from the
10332 code we can also determine that p_5 cannot be NULL and, if q_2 had
10333 a non-varying range, p_5's range should also be compatible with it.
10335 These equivalences are created by two expressions: ASSERT_EXPR and
10336 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10337 result of another assertion, then we can use the fact that p_5 and
10338 p_4 are equivalent when evaluating p_5's range.
10340 Together with value ranges, we also propagate these equivalences
10341 between names so that we can take advantage of information from
10342 multiple ranges when doing final replacement. Note that this
10343 equivalency relation is transitive but not symmetric.
10345 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10346 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10347 in contexts where that assertion does not hold (e.g., in line 6).
10349 TODO, the main difference between this pass and Patterson's is that
10350 we do not propagate edge probabilities. We only compute whether
10351 edges can be taken or not. That is, instead of having a spectrum
10352 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10353 DON'T KNOW. In the future, it may be worthwhile to propagate
10354 probabilities to aid branch prediction. */
10356 static unsigned int
10357 execute_vrp (bool warn_array_bounds_p)
10359 int i;
10360 edge e;
10361 switch_update *su;
10363 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10364 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10365 scev_initialize ();
10367 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10368 Inserting assertions may split edges which will invalidate
10369 EDGE_DFS_BACK. */
10370 insert_range_assertions ();
10372 to_remove_edges.create (10);
10373 to_update_switch_stmts.create (5);
10374 threadedge_initialize_values ();
10376 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10377 mark_dfs_back_edges ();
10379 vrp_initialize ();
10380 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10381 vrp_finalize (warn_array_bounds_p);
10383 free_numbers_of_iterations_estimates (cfun);
10385 /* ASSERT_EXPRs must be removed before finalizing jump threads
10386 as finalizing jump threads calls the CFG cleanup code which
10387 does not properly handle ASSERT_EXPRs. */
10388 remove_range_assertions ();
10390 /* If we exposed any new variables, go ahead and put them into
10391 SSA form now, before we handle jump threading. This simplifies
10392 interactions between rewriting of _DECL nodes into SSA form
10393 and rewriting SSA_NAME nodes into SSA form after block
10394 duplication and CFG manipulation. */
10395 update_ssa (TODO_update_ssa);
10397 finalize_jump_threads ();
10399 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10400 CFG in a broken state and requires a cfg_cleanup run. */
10401 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10402 remove_edge (e);
10403 /* Update SWITCH_EXPR case label vector. */
10404 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10406 size_t j;
10407 size_t n = TREE_VEC_LENGTH (su->vec);
10408 tree label;
10409 gimple_switch_set_num_labels (su->stmt, n);
10410 for (j = 0; j < n; j++)
10411 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10412 /* As we may have replaced the default label with a regular one
10413 make sure to make it a real default label again. This ensures
10414 optimal expansion. */
10415 label = gimple_switch_label (su->stmt, 0);
10416 CASE_LOW (label) = NULL_TREE;
10417 CASE_HIGH (label) = NULL_TREE;
10420 if (to_remove_edges.length () > 0)
10422 free_dominance_info (CDI_DOMINATORS);
10423 loops_state_set (LOOPS_NEED_FIXUP);
10426 to_remove_edges.release ();
10427 to_update_switch_stmts.release ();
10428 threadedge_finalize_values ();
10430 scev_finalize ();
10431 loop_optimizer_finalize ();
10432 return 0;
10435 namespace {
10437 const pass_data pass_data_vrp =
10439 GIMPLE_PASS, /* type */
10440 "vrp", /* name */
10441 OPTGROUP_NONE, /* optinfo_flags */
10442 TV_TREE_VRP, /* tv_id */
10443 PROP_ssa, /* properties_required */
10444 0, /* properties_provided */
10445 0, /* properties_destroyed */
10446 0, /* todo_flags_start */
10447 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10450 class pass_vrp : public gimple_opt_pass
10452 public:
10453 pass_vrp (gcc::context *ctxt)
10454 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
10457 /* opt_pass methods: */
10458 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10459 void set_pass_param (unsigned int n, bool param)
10461 gcc_assert (n == 0);
10462 warn_array_bounds_p = param;
10464 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10465 virtual unsigned int execute (function *)
10466 { return execute_vrp (warn_array_bounds_p); }
10468 private:
10469 bool warn_array_bounds_p;
10470 }; // class pass_vrp
10472 } // anon namespace
10474 gimple_opt_pass *
10475 make_pass_vrp (gcc::context *ctxt)
10477 return new pass_vrp (ctxt);