2015-05-05 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / gcc / tree-vrp.c
blob0ade55c0b54380ef9a02a5fcc857716ee362b9f8
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "flags.h"
26 #include "hash-set.h"
27 #include "machmode.h"
28 #include "vec.h"
29 #include "double-int.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "predict.h"
40 #include "hard-reg-set.h"
41 #include "function.h"
42 #include "dominance.h"
43 #include "cfg.h"
44 #include "cfganal.h"
45 #include "basic-block.h"
46 #include "tree-ssa-alias.h"
47 #include "internal-fn.h"
48 #include "gimple-fold.h"
49 #include "tree-eh.h"
50 #include "gimple-expr.h"
51 #include "is-a.h"
52 #include "gimple.h"
53 #include "gimple-iterator.h"
54 #include "gimple-walk.h"
55 #include "gimple-ssa.h"
56 #include "tree-cfg.h"
57 #include "tree-phinodes.h"
58 #include "ssa-iterators.h"
59 #include "stringpool.h"
60 #include "tree-ssanames.h"
61 #include "tree-ssa-loop-manip.h"
62 #include "tree-ssa-loop-niter.h"
63 #include "tree-ssa-loop.h"
64 #include "tree-into-ssa.h"
65 #include "tree-ssa.h"
66 #include "tree-pass.h"
67 #include "tree-dump.h"
68 #include "gimple-pretty-print.h"
69 #include "diagnostic-core.h"
70 #include "intl.h"
71 #include "cfgloop.h"
72 #include "tree-scalar-evolution.h"
73 #include "tree-ssa-propagate.h"
74 #include "tree-chrec.h"
75 #include "tree-ssa-threadupdate.h"
76 #include "hashtab.h"
77 #include "rtl.h"
78 #include "statistics.h"
79 #include "real.h"
80 #include "fixed-value.h"
81 #include "insn-config.h"
82 #include "expmed.h"
83 #include "dojump.h"
84 #include "explow.h"
85 #include "emit-rtl.h"
86 #include "varasm.h"
87 #include "stmt.h"
88 #include "expr.h"
89 #include "insn-codes.h"
90 #include "optabs.h"
91 #include "tree-ssa-scopedtables.h"
92 #include "tree-ssa-threadedge.h"
96 /* Range of values that can be associated with an SSA_NAME after VRP
97 has executed. */
98 struct value_range_d
100 /* Lattice value represented by this range. */
101 enum value_range_type type;
103 /* Minimum and maximum values represented by this range. These
104 values should be interpreted as follows:
106 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
107 be NULL.
109 - If TYPE == VR_RANGE then MIN holds the minimum value and
110 MAX holds the maximum value of the range [MIN, MAX].
112 - If TYPE == ANTI_RANGE the variable is known to NOT
113 take any values in the range [MIN, MAX]. */
114 tree min;
115 tree max;
117 /* Set of SSA names whose value ranges are equivalent to this one.
118 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
119 bitmap equiv;
122 typedef struct value_range_d value_range_t;
124 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
126 /* Set of SSA names found live during the RPO traversal of the function
127 for still active basic-blocks. */
128 static sbitmap *live;
130 /* Return true if the SSA name NAME is live on the edge E. */
132 static bool
133 live_on_edge (edge e, tree name)
135 return (live[e->dest->index]
136 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
139 /* Local functions. */
140 static int compare_values (tree val1, tree val2);
141 static int compare_values_warnv (tree val1, tree val2, bool *);
142 static void vrp_meet (value_range_t *, value_range_t *);
143 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
144 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
145 tree, tree, bool, bool *,
146 bool *);
148 /* Location information for ASSERT_EXPRs. Each instance of this
149 structure describes an ASSERT_EXPR for an SSA name. Since a single
150 SSA name may have more than one assertion associated with it, these
151 locations are kept in a linked list attached to the corresponding
152 SSA name. */
153 struct assert_locus_d
155 /* Basic block where the assertion would be inserted. */
156 basic_block bb;
158 /* Some assertions need to be inserted on an edge (e.g., assertions
159 generated by COND_EXPRs). In those cases, BB will be NULL. */
160 edge e;
162 /* Pointer to the statement that generated this assertion. */
163 gimple_stmt_iterator si;
165 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
166 enum tree_code comp_code;
168 /* Value being compared against. */
169 tree val;
171 /* Expression to compare. */
172 tree expr;
174 /* Next node in the linked list. */
175 struct assert_locus_d *next;
178 typedef struct assert_locus_d *assert_locus_t;
180 /* If bit I is present, it means that SSA name N_i has a list of
181 assertions that should be inserted in the IL. */
182 static bitmap need_assert_for;
184 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
185 holds a list of ASSERT_LOCUS_T nodes that describe where
186 ASSERT_EXPRs for SSA name N_I should be inserted. */
187 static assert_locus_t *asserts_for;
189 /* Value range array. After propagation, VR_VALUE[I] holds the range
190 of values that SSA name N_I may take. */
191 static unsigned num_vr_values;
192 static value_range_t **vr_value;
193 static bool values_propagated;
195 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
196 number of executable edges we saw the last time we visited the
197 node. */
198 static int *vr_phi_edge_counts;
200 typedef struct {
201 gswitch *stmt;
202 tree vec;
203 } switch_update;
205 static vec<edge> to_remove_edges;
206 static vec<switch_update> to_update_switch_stmts;
209 /* Return the maximum value for TYPE. */
211 static inline tree
212 vrp_val_max (const_tree type)
214 if (!INTEGRAL_TYPE_P (type))
215 return NULL_TREE;
217 return TYPE_MAX_VALUE (type);
220 /* Return the minimum value for TYPE. */
222 static inline tree
223 vrp_val_min (const_tree type)
225 if (!INTEGRAL_TYPE_P (type))
226 return NULL_TREE;
228 return TYPE_MIN_VALUE (type);
231 /* Return whether VAL is equal to the maximum value of its type. This
232 will be true for a positive overflow infinity. We can't do a
233 simple equality comparison with TYPE_MAX_VALUE because C typedefs
234 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
235 to the integer constant with the same value in the type. */
237 static inline bool
238 vrp_val_is_max (const_tree val)
240 tree type_max = vrp_val_max (TREE_TYPE (val));
241 return (val == type_max
242 || (type_max != NULL_TREE
243 && operand_equal_p (val, type_max, 0)));
246 /* Return whether VAL is equal to the minimum value of its type. This
247 will be true for a negative overflow infinity. */
249 static inline bool
250 vrp_val_is_min (const_tree val)
252 tree type_min = vrp_val_min (TREE_TYPE (val));
253 return (val == type_min
254 || (type_min != NULL_TREE
255 && operand_equal_p (val, type_min, 0)));
259 /* Return whether TYPE should use an overflow infinity distinct from
260 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
261 represent a signed overflow during VRP computations. An infinity
262 is distinct from a half-range, which will go from some number to
263 TYPE_{MIN,MAX}_VALUE. */
265 static inline bool
266 needs_overflow_infinity (const_tree type)
268 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
271 /* Return whether TYPE can support our overflow infinity
272 representation: we use the TREE_OVERFLOW flag, which only exists
273 for constants. If TYPE doesn't support this, we don't optimize
274 cases which would require signed overflow--we drop them to
275 VARYING. */
277 static inline bool
278 supports_overflow_infinity (const_tree type)
280 tree min = vrp_val_min (type), max = vrp_val_max (type);
281 #ifdef ENABLE_CHECKING
282 gcc_assert (needs_overflow_infinity (type));
283 #endif
284 return (min != NULL_TREE
285 && CONSTANT_CLASS_P (min)
286 && max != NULL_TREE
287 && CONSTANT_CLASS_P (max));
290 /* VAL is the maximum or minimum value of a type. Return a
291 corresponding overflow infinity. */
293 static inline tree
294 make_overflow_infinity (tree val)
296 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
297 val = copy_node (val);
298 TREE_OVERFLOW (val) = 1;
299 return val;
302 /* Return a negative overflow infinity for TYPE. */
304 static inline tree
305 negative_overflow_infinity (tree type)
307 gcc_checking_assert (supports_overflow_infinity (type));
308 return make_overflow_infinity (vrp_val_min (type));
311 /* Return a positive overflow infinity for TYPE. */
313 static inline tree
314 positive_overflow_infinity (tree type)
316 gcc_checking_assert (supports_overflow_infinity (type));
317 return make_overflow_infinity (vrp_val_max (type));
320 /* Return whether VAL is a negative overflow infinity. */
322 static inline bool
323 is_negative_overflow_infinity (const_tree val)
325 return (TREE_OVERFLOW_P (val)
326 && needs_overflow_infinity (TREE_TYPE (val))
327 && vrp_val_is_min (val));
330 /* Return whether VAL is a positive overflow infinity. */
332 static inline bool
333 is_positive_overflow_infinity (const_tree val)
335 return (TREE_OVERFLOW_P (val)
336 && needs_overflow_infinity (TREE_TYPE (val))
337 && vrp_val_is_max (val));
340 /* Return whether VAL is a positive or negative overflow infinity. */
342 static inline bool
343 is_overflow_infinity (const_tree val)
345 return (TREE_OVERFLOW_P (val)
346 && needs_overflow_infinity (TREE_TYPE (val))
347 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
350 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
352 static inline bool
353 stmt_overflow_infinity (gimple stmt)
355 if (is_gimple_assign (stmt)
356 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
357 GIMPLE_SINGLE_RHS)
358 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
359 return false;
362 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
363 the same value with TREE_OVERFLOW clear. This can be used to avoid
364 confusing a regular value with an overflow value. */
366 static inline tree
367 avoid_overflow_infinity (tree val)
369 if (!is_overflow_infinity (val))
370 return val;
372 if (vrp_val_is_max (val))
373 return vrp_val_max (TREE_TYPE (val));
374 else
376 gcc_checking_assert (vrp_val_is_min (val));
377 return vrp_val_min (TREE_TYPE (val));
382 /* Return true if ARG is marked with the nonnull attribute in the
383 current function signature. */
385 static bool
386 nonnull_arg_p (const_tree arg)
388 tree t, attrs, fntype;
389 unsigned HOST_WIDE_INT arg_num;
391 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
393 /* The static chain decl is always non null. */
394 if (arg == cfun->static_chain_decl)
395 return true;
397 /* THIS argument of method is always non-NULL. */
398 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
399 && arg == DECL_ARGUMENTS (current_function_decl)
400 && flag_delete_null_pointer_checks)
401 return true;
403 /* Values passed by reference are always non-NULL. */
404 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
405 && flag_delete_null_pointer_checks)
406 return true;
408 fntype = TREE_TYPE (current_function_decl);
409 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
411 attrs = lookup_attribute ("nonnull", attrs);
413 /* If "nonnull" wasn't specified, we know nothing about the argument. */
414 if (attrs == NULL_TREE)
415 return false;
417 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
418 if (TREE_VALUE (attrs) == NULL_TREE)
419 return true;
421 /* Get the position number for ARG in the function signature. */
422 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
424 t = DECL_CHAIN (t), arg_num++)
426 if (t == arg)
427 break;
430 gcc_assert (t == arg);
432 /* Now see if ARG_NUM is mentioned in the nonnull list. */
433 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
435 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
436 return true;
440 return false;
444 /* Set value range VR to VR_UNDEFINED. */
446 static inline void
447 set_value_range_to_undefined (value_range_t *vr)
449 vr->type = VR_UNDEFINED;
450 vr->min = vr->max = NULL_TREE;
451 if (vr->equiv)
452 bitmap_clear (vr->equiv);
456 /* Set value range VR to VR_VARYING. */
458 static inline void
459 set_value_range_to_varying (value_range_t *vr)
461 vr->type = VR_VARYING;
462 vr->min = vr->max = NULL_TREE;
463 if (vr->equiv)
464 bitmap_clear (vr->equiv);
468 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
470 static void
471 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
472 tree max, bitmap equiv)
474 #if defined ENABLE_CHECKING
475 /* Check the validity of the range. */
476 if (t == VR_RANGE || t == VR_ANTI_RANGE)
478 int cmp;
480 gcc_assert (min && max);
482 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
483 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
485 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
486 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
488 cmp = compare_values (min, max);
489 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
491 if (needs_overflow_infinity (TREE_TYPE (min)))
492 gcc_assert (!is_overflow_infinity (min)
493 || !is_overflow_infinity (max));
496 if (t == VR_UNDEFINED || t == VR_VARYING)
497 gcc_assert (min == NULL_TREE && max == NULL_TREE);
499 if (t == VR_UNDEFINED || t == VR_VARYING)
500 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
501 #endif
503 vr->type = t;
504 vr->min = min;
505 vr->max = max;
507 /* Since updating the equivalence set involves deep copying the
508 bitmaps, only do it if absolutely necessary. */
509 if (vr->equiv == NULL
510 && equiv != NULL)
511 vr->equiv = BITMAP_ALLOC (NULL);
513 if (equiv != vr->equiv)
515 if (equiv && !bitmap_empty_p (equiv))
516 bitmap_copy (vr->equiv, equiv);
517 else
518 bitmap_clear (vr->equiv);
523 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
524 This means adjusting T, MIN and MAX representing the case of a
525 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
526 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
527 In corner cases where MAX+1 or MIN-1 wraps this will fall back
528 to varying.
529 This routine exists to ease canonicalization in the case where we
530 extract ranges from var + CST op limit. */
532 static void
533 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
534 tree min, tree max, bitmap equiv)
536 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
537 if (t == VR_UNDEFINED)
539 set_value_range_to_undefined (vr);
540 return;
542 else if (t == VR_VARYING)
544 set_value_range_to_varying (vr);
545 return;
548 /* Nothing to canonicalize for symbolic ranges. */
549 if (TREE_CODE (min) != INTEGER_CST
550 || TREE_CODE (max) != INTEGER_CST)
552 set_value_range (vr, t, min, max, equiv);
553 return;
556 /* Wrong order for min and max, to swap them and the VR type we need
557 to adjust them. */
558 if (tree_int_cst_lt (max, min))
560 tree one, tmp;
562 /* For one bit precision if max < min, then the swapped
563 range covers all values, so for VR_RANGE it is varying and
564 for VR_ANTI_RANGE empty range, so drop to varying as well. */
565 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
567 set_value_range_to_varying (vr);
568 return;
571 one = build_int_cst (TREE_TYPE (min), 1);
572 tmp = int_const_binop (PLUS_EXPR, max, one);
573 max = int_const_binop (MINUS_EXPR, min, one);
574 min = tmp;
576 /* There's one corner case, if we had [C+1, C] before we now have
577 that again. But this represents an empty value range, so drop
578 to varying in this case. */
579 if (tree_int_cst_lt (max, min))
581 set_value_range_to_varying (vr);
582 return;
585 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
588 /* Anti-ranges that can be represented as ranges should be so. */
589 if (t == VR_ANTI_RANGE)
591 bool is_min = vrp_val_is_min (min);
592 bool is_max = vrp_val_is_max (max);
594 if (is_min && is_max)
596 /* We cannot deal with empty ranges, drop to varying.
597 ??? This could be VR_UNDEFINED instead. */
598 set_value_range_to_varying (vr);
599 return;
601 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
602 && (is_min || is_max))
604 /* Non-empty boolean ranges can always be represented
605 as a singleton range. */
606 if (is_min)
607 min = max = vrp_val_max (TREE_TYPE (min));
608 else
609 min = max = vrp_val_min (TREE_TYPE (min));
610 t = VR_RANGE;
612 else if (is_min
613 /* As a special exception preserve non-null ranges. */
614 && !(TYPE_UNSIGNED (TREE_TYPE (min))
615 && integer_zerop (max)))
617 tree one = build_int_cst (TREE_TYPE (max), 1);
618 min = int_const_binop (PLUS_EXPR, max, one);
619 max = vrp_val_max (TREE_TYPE (max));
620 t = VR_RANGE;
622 else if (is_max)
624 tree one = build_int_cst (TREE_TYPE (min), 1);
625 max = int_const_binop (MINUS_EXPR, min, one);
626 min = vrp_val_min (TREE_TYPE (min));
627 t = VR_RANGE;
631 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
632 if (needs_overflow_infinity (TREE_TYPE (min))
633 && is_overflow_infinity (min)
634 && is_overflow_infinity (max))
636 set_value_range_to_varying (vr);
637 return;
640 set_value_range (vr, t, min, max, equiv);
643 /* Copy value range FROM into value range TO. */
645 static inline void
646 copy_value_range (value_range_t *to, value_range_t *from)
648 set_value_range (to, from->type, from->min, from->max, from->equiv);
651 /* Set value range VR to a single value. This function is only called
652 with values we get from statements, and exists to clear the
653 TREE_OVERFLOW flag so that we don't think we have an overflow
654 infinity when we shouldn't. */
656 static inline void
657 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
659 gcc_assert (is_gimple_min_invariant (val));
660 if (TREE_OVERFLOW_P (val))
661 val = drop_tree_overflow (val);
662 set_value_range (vr, VR_RANGE, val, val, equiv);
665 /* Set value range VR to a non-negative range of type TYPE.
666 OVERFLOW_INFINITY indicates whether to use an overflow infinity
667 rather than TYPE_MAX_VALUE; this should be true if we determine
668 that the range is nonnegative based on the assumption that signed
669 overflow does not occur. */
671 static inline void
672 set_value_range_to_nonnegative (value_range_t *vr, tree type,
673 bool overflow_infinity)
675 tree zero;
677 if (overflow_infinity && !supports_overflow_infinity (type))
679 set_value_range_to_varying (vr);
680 return;
683 zero = build_int_cst (type, 0);
684 set_value_range (vr, VR_RANGE, zero,
685 (overflow_infinity
686 ? positive_overflow_infinity (type)
687 : TYPE_MAX_VALUE (type)),
688 vr->equiv);
691 /* Set value range VR to a non-NULL range of type TYPE. */
693 static inline void
694 set_value_range_to_nonnull (value_range_t *vr, tree type)
696 tree zero = build_int_cst (type, 0);
697 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
701 /* Set value range VR to a NULL range of type TYPE. */
703 static inline void
704 set_value_range_to_null (value_range_t *vr, tree type)
706 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
710 /* Set value range VR to a range of a truthvalue of type TYPE. */
712 static inline void
713 set_value_range_to_truthvalue (value_range_t *vr, tree type)
715 if (TYPE_PRECISION (type) == 1)
716 set_value_range_to_varying (vr);
717 else
718 set_value_range (vr, VR_RANGE,
719 build_int_cst (type, 0), build_int_cst (type, 1),
720 vr->equiv);
724 /* If abs (min) < abs (max), set VR to [-max, max], if
725 abs (min) >= abs (max), set VR to [-min, min]. */
727 static void
728 abs_extent_range (value_range_t *vr, tree min, tree max)
730 int cmp;
732 gcc_assert (TREE_CODE (min) == INTEGER_CST);
733 gcc_assert (TREE_CODE (max) == INTEGER_CST);
734 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
735 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
736 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
737 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
738 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
740 set_value_range_to_varying (vr);
741 return;
743 cmp = compare_values (min, max);
744 if (cmp == -1)
745 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
746 else if (cmp == 0 || cmp == 1)
748 max = min;
749 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
751 else
753 set_value_range_to_varying (vr);
754 return;
756 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
760 /* Return value range information for VAR.
762 If we have no values ranges recorded (ie, VRP is not running), then
763 return NULL. Otherwise create an empty range if none existed for VAR. */
765 static value_range_t *
766 get_value_range (const_tree var)
768 static const struct value_range_d vr_const_varying
769 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
770 value_range_t *vr;
771 tree sym;
772 unsigned ver = SSA_NAME_VERSION (var);
774 /* If we have no recorded ranges, then return NULL. */
775 if (! vr_value)
776 return NULL;
778 /* If we query the range for a new SSA name return an unmodifiable VARYING.
779 We should get here at most from the substitute-and-fold stage which
780 will never try to change values. */
781 if (ver >= num_vr_values)
782 return CONST_CAST (value_range_t *, &vr_const_varying);
784 vr = vr_value[ver];
785 if (vr)
786 return vr;
788 /* After propagation finished do not allocate new value-ranges. */
789 if (values_propagated)
790 return CONST_CAST (value_range_t *, &vr_const_varying);
792 /* Create a default value range. */
793 vr_value[ver] = vr = XCNEW (value_range_t);
795 /* Defer allocating the equivalence set. */
796 vr->equiv = NULL;
798 /* If VAR is a default definition of a parameter, the variable can
799 take any value in VAR's type. */
800 if (SSA_NAME_IS_DEFAULT_DEF (var))
802 sym = SSA_NAME_VAR (var);
803 if (TREE_CODE (sym) == PARM_DECL)
805 /* Try to use the "nonnull" attribute to create ~[0, 0]
806 anti-ranges for pointers. Note that this is only valid with
807 default definitions of PARM_DECLs. */
808 if (POINTER_TYPE_P (TREE_TYPE (sym))
809 && nonnull_arg_p (sym))
810 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
811 else
812 set_value_range_to_varying (vr);
814 else if (TREE_CODE (sym) == RESULT_DECL
815 && DECL_BY_REFERENCE (sym))
816 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
819 return vr;
822 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
824 static inline bool
825 vrp_operand_equal_p (const_tree val1, const_tree val2)
827 if (val1 == val2)
828 return true;
829 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
830 return false;
831 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
834 /* Return true, if the bitmaps B1 and B2 are equal. */
836 static inline bool
837 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
839 return (b1 == b2
840 || ((!b1 || bitmap_empty_p (b1))
841 && (!b2 || bitmap_empty_p (b2)))
842 || (b1 && b2
843 && bitmap_equal_p (b1, b2)));
846 /* Update the value range and equivalence set for variable VAR to
847 NEW_VR. Return true if NEW_VR is different from VAR's previous
848 value.
850 NOTE: This function assumes that NEW_VR is a temporary value range
851 object created for the sole purpose of updating VAR's range. The
852 storage used by the equivalence set from NEW_VR will be freed by
853 this function. Do not call update_value_range when NEW_VR
854 is the range object associated with another SSA name. */
856 static inline bool
857 update_value_range (const_tree var, value_range_t *new_vr)
859 value_range_t *old_vr;
860 bool is_new;
862 /* If there is a value-range on the SSA name from earlier analysis
863 factor that in. */
864 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
866 wide_int min, max;
867 value_range_type rtype = get_range_info (var, &min, &max);
868 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
870 value_range_d nr;
871 nr.type = rtype;
872 nr.min = wide_int_to_tree (TREE_TYPE (var), min);
873 nr.max = wide_int_to_tree (TREE_TYPE (var), max);
874 nr.equiv = NULL;
875 vrp_intersect_ranges (new_vr, &nr);
879 /* Update the value range, if necessary. */
880 old_vr = get_value_range (var);
881 is_new = old_vr->type != new_vr->type
882 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
883 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
884 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
886 if (is_new)
888 /* Do not allow transitions up the lattice. The following
889 is slightly more awkward than just new_vr->type < old_vr->type
890 because VR_RANGE and VR_ANTI_RANGE need to be considered
891 the same. We may not have is_new when transitioning to
892 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
893 called. */
894 if (new_vr->type == VR_UNDEFINED)
896 BITMAP_FREE (new_vr->equiv);
897 set_value_range_to_varying (old_vr);
898 set_value_range_to_varying (new_vr);
899 return true;
901 else
902 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
903 new_vr->equiv);
906 BITMAP_FREE (new_vr->equiv);
908 return is_new;
912 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
913 point where equivalence processing can be turned on/off. */
915 static void
916 add_equivalence (bitmap *equiv, const_tree var)
918 unsigned ver = SSA_NAME_VERSION (var);
919 value_range_t *vr = vr_value[ver];
921 if (*equiv == NULL)
922 *equiv = BITMAP_ALLOC (NULL);
923 bitmap_set_bit (*equiv, ver);
924 if (vr && vr->equiv)
925 bitmap_ior_into (*equiv, vr->equiv);
929 /* Return true if VR is ~[0, 0]. */
931 static inline bool
932 range_is_nonnull (value_range_t *vr)
934 return vr->type == VR_ANTI_RANGE
935 && integer_zerop (vr->min)
936 && integer_zerop (vr->max);
940 /* Return true if VR is [0, 0]. */
942 static inline bool
943 range_is_null (value_range_t *vr)
945 return vr->type == VR_RANGE
946 && integer_zerop (vr->min)
947 && integer_zerop (vr->max);
950 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
951 a singleton. */
953 static inline bool
954 range_int_cst_p (value_range_t *vr)
956 return (vr->type == VR_RANGE
957 && TREE_CODE (vr->max) == INTEGER_CST
958 && TREE_CODE (vr->min) == INTEGER_CST);
961 /* Return true if VR is a INTEGER_CST singleton. */
963 static inline bool
964 range_int_cst_singleton_p (value_range_t *vr)
966 return (range_int_cst_p (vr)
967 && !is_overflow_infinity (vr->min)
968 && !is_overflow_infinity (vr->max)
969 && tree_int_cst_equal (vr->min, vr->max));
972 /* Return true if value range VR involves at least one symbol. */
974 static inline bool
975 symbolic_range_p (value_range_t *vr)
977 return (!is_gimple_min_invariant (vr->min)
978 || !is_gimple_min_invariant (vr->max));
981 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
982 otherwise. We only handle additive operations and set NEG to true if the
983 symbol is negated and INV to the invariant part, if any. */
985 static tree
986 get_single_symbol (tree t, bool *neg, tree *inv)
988 bool neg_;
989 tree inv_;
991 if (TREE_CODE (t) == PLUS_EXPR
992 || TREE_CODE (t) == POINTER_PLUS_EXPR
993 || TREE_CODE (t) == MINUS_EXPR)
995 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
997 neg_ = (TREE_CODE (t) == MINUS_EXPR);
998 inv_ = TREE_OPERAND (t, 0);
999 t = TREE_OPERAND (t, 1);
1001 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1003 neg_ = false;
1004 inv_ = TREE_OPERAND (t, 1);
1005 t = TREE_OPERAND (t, 0);
1007 else
1008 return NULL_TREE;
1010 else
1012 neg_ = false;
1013 inv_ = NULL_TREE;
1016 if (TREE_CODE (t) == NEGATE_EXPR)
1018 t = TREE_OPERAND (t, 0);
1019 neg_ = !neg_;
1022 if (TREE_CODE (t) != SSA_NAME)
1023 return NULL_TREE;
1025 *neg = neg_;
1026 *inv = inv_;
1027 return t;
1030 /* The reverse operation: build a symbolic expression with TYPE
1031 from symbol SYM, negated according to NEG, and invariant INV. */
1033 static tree
1034 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
1036 const bool pointer_p = POINTER_TYPE_P (type);
1037 tree t = sym;
1039 if (neg)
1040 t = build1 (NEGATE_EXPR, type, t);
1042 if (integer_zerop (inv))
1043 return t;
1045 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
1048 /* Return true if value range VR involves exactly one symbol SYM. */
1050 static bool
1051 symbolic_range_based_on_p (value_range_t *vr, const_tree sym)
1053 bool neg, min_has_symbol, max_has_symbol;
1054 tree inv;
1056 if (is_gimple_min_invariant (vr->min))
1057 min_has_symbol = false;
1058 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
1059 min_has_symbol = true;
1060 else
1061 return false;
1063 if (is_gimple_min_invariant (vr->max))
1064 max_has_symbol = false;
1065 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
1066 max_has_symbol = true;
1067 else
1068 return false;
1070 return (min_has_symbol || max_has_symbol);
1073 /* Return true if value range VR uses an overflow infinity. */
1075 static inline bool
1076 overflow_infinity_range_p (value_range_t *vr)
1078 return (vr->type == VR_RANGE
1079 && (is_overflow_infinity (vr->min)
1080 || is_overflow_infinity (vr->max)));
1083 /* Return false if we can not make a valid comparison based on VR;
1084 this will be the case if it uses an overflow infinity and overflow
1085 is not undefined (i.e., -fno-strict-overflow is in effect).
1086 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1087 uses an overflow infinity. */
1089 static bool
1090 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
1092 gcc_assert (vr->type == VR_RANGE);
1093 if (is_overflow_infinity (vr->min))
1095 *strict_overflow_p = true;
1096 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1097 return false;
1099 if (is_overflow_infinity (vr->max))
1101 *strict_overflow_p = true;
1102 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1103 return false;
1105 return true;
1109 /* Return true if the result of assignment STMT is know to be non-negative.
1110 If the return value is based on the assumption that signed overflow is
1111 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1112 *STRICT_OVERFLOW_P.*/
1114 static bool
1115 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1117 enum tree_code code = gimple_assign_rhs_code (stmt);
1118 switch (get_gimple_rhs_class (code))
1120 case GIMPLE_UNARY_RHS:
1121 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1122 gimple_expr_type (stmt),
1123 gimple_assign_rhs1 (stmt),
1124 strict_overflow_p);
1125 case GIMPLE_BINARY_RHS:
1126 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1127 gimple_expr_type (stmt),
1128 gimple_assign_rhs1 (stmt),
1129 gimple_assign_rhs2 (stmt),
1130 strict_overflow_p);
1131 case GIMPLE_TERNARY_RHS:
1132 return false;
1133 case GIMPLE_SINGLE_RHS:
1134 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
1135 strict_overflow_p);
1136 case GIMPLE_INVALID_RHS:
1137 gcc_unreachable ();
1138 default:
1139 gcc_unreachable ();
1143 /* Return true if return value of call STMT is know to be non-negative.
1144 If the return value is based on the assumption that signed overflow is
1145 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1146 *STRICT_OVERFLOW_P.*/
1148 static bool
1149 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1151 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1152 gimple_call_arg (stmt, 0) : NULL_TREE;
1153 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1154 gimple_call_arg (stmt, 1) : NULL_TREE;
1156 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1157 gimple_call_fndecl (stmt),
1158 arg0,
1159 arg1,
1160 strict_overflow_p);
1163 /* Return true if STMT is know to to compute a non-negative value.
1164 If the return value is based on the assumption that signed overflow is
1165 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1166 *STRICT_OVERFLOW_P.*/
1168 static bool
1169 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1171 switch (gimple_code (stmt))
1173 case GIMPLE_ASSIGN:
1174 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1175 case GIMPLE_CALL:
1176 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1177 default:
1178 gcc_unreachable ();
1182 /* Return true if the result of assignment STMT is know to be non-zero.
1183 If the return value is based on the assumption that signed overflow is
1184 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1185 *STRICT_OVERFLOW_P.*/
1187 static bool
1188 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1190 enum tree_code code = gimple_assign_rhs_code (stmt);
1191 switch (get_gimple_rhs_class (code))
1193 case GIMPLE_UNARY_RHS:
1194 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1195 gimple_expr_type (stmt),
1196 gimple_assign_rhs1 (stmt),
1197 strict_overflow_p);
1198 case GIMPLE_BINARY_RHS:
1199 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1200 gimple_expr_type (stmt),
1201 gimple_assign_rhs1 (stmt),
1202 gimple_assign_rhs2 (stmt),
1203 strict_overflow_p);
1204 case GIMPLE_TERNARY_RHS:
1205 return false;
1206 case GIMPLE_SINGLE_RHS:
1207 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1208 strict_overflow_p);
1209 case GIMPLE_INVALID_RHS:
1210 gcc_unreachable ();
1211 default:
1212 gcc_unreachable ();
1216 /* Return true if STMT is known to compute a non-zero value.
1217 If the return value is based on the assumption that signed overflow is
1218 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1219 *STRICT_OVERFLOW_P.*/
1221 static bool
1222 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1224 switch (gimple_code (stmt))
1226 case GIMPLE_ASSIGN:
1227 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1228 case GIMPLE_CALL:
1230 tree fndecl = gimple_call_fndecl (stmt);
1231 if (!fndecl) return false;
1232 if (flag_delete_null_pointer_checks && !flag_check_new
1233 && DECL_IS_OPERATOR_NEW (fndecl)
1234 && !TREE_NOTHROW (fndecl))
1235 return true;
1236 /* References are always non-NULL. */
1237 if (flag_delete_null_pointer_checks
1238 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1239 return true;
1240 if (flag_delete_null_pointer_checks &&
1241 lookup_attribute ("returns_nonnull",
1242 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1243 return true;
1244 return gimple_alloca_call_p (stmt);
1246 default:
1247 gcc_unreachable ();
1251 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1252 obtained so far. */
1254 static bool
1255 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1257 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1258 return true;
1260 /* If we have an expression of the form &X->a, then the expression
1261 is nonnull if X is nonnull. */
1262 if (is_gimple_assign (stmt)
1263 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1265 tree expr = gimple_assign_rhs1 (stmt);
1266 tree base = get_base_address (TREE_OPERAND (expr, 0));
1268 if (base != NULL_TREE
1269 && TREE_CODE (base) == MEM_REF
1270 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1272 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1273 if (range_is_nonnull (vr))
1274 return true;
1278 return false;
1281 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1282 a gimple invariant, or SSA_NAME +- CST. */
1284 static bool
1285 valid_value_p (tree expr)
1287 if (TREE_CODE (expr) == SSA_NAME)
1288 return true;
1290 if (TREE_CODE (expr) == PLUS_EXPR
1291 || TREE_CODE (expr) == MINUS_EXPR)
1292 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1293 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1295 return is_gimple_min_invariant (expr);
1298 /* Return
1299 1 if VAL < VAL2
1300 0 if !(VAL < VAL2)
1301 -2 if those are incomparable. */
1302 static inline int
1303 operand_less_p (tree val, tree val2)
1305 /* LT is folded faster than GE and others. Inline the common case. */
1306 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1307 return tree_int_cst_lt (val, val2);
1308 else
1310 tree tcmp;
1312 fold_defer_overflow_warnings ();
1314 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1316 fold_undefer_and_ignore_overflow_warnings ();
1318 if (!tcmp
1319 || TREE_CODE (tcmp) != INTEGER_CST)
1320 return -2;
1322 if (!integer_zerop (tcmp))
1323 return 1;
1326 /* val >= val2, not considering overflow infinity. */
1327 if (is_negative_overflow_infinity (val))
1328 return is_negative_overflow_infinity (val2) ? 0 : 1;
1329 else if (is_positive_overflow_infinity (val2))
1330 return is_positive_overflow_infinity (val) ? 0 : 1;
1332 return 0;
1335 /* Compare two values VAL1 and VAL2. Return
1337 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1338 -1 if VAL1 < VAL2,
1339 0 if VAL1 == VAL2,
1340 +1 if VAL1 > VAL2, and
1341 +2 if VAL1 != VAL2
1343 This is similar to tree_int_cst_compare but supports pointer values
1344 and values that cannot be compared at compile time.
1346 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1347 true if the return value is only valid if we assume that signed
1348 overflow is undefined. */
1350 static int
1351 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1353 if (val1 == val2)
1354 return 0;
1356 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1357 both integers. */
1358 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1359 == POINTER_TYPE_P (TREE_TYPE (val2)));
1361 /* Convert the two values into the same type. This is needed because
1362 sizetype causes sign extension even for unsigned types. */
1363 val2 = fold_convert (TREE_TYPE (val1), val2);
1364 STRIP_USELESS_TYPE_CONVERSION (val2);
1366 if ((TREE_CODE (val1) == SSA_NAME
1367 || (TREE_CODE (val1) == NEGATE_EXPR
1368 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1369 || TREE_CODE (val1) == PLUS_EXPR
1370 || TREE_CODE (val1) == MINUS_EXPR)
1371 && (TREE_CODE (val2) == SSA_NAME
1372 || (TREE_CODE (val2) == NEGATE_EXPR
1373 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1374 || TREE_CODE (val2) == PLUS_EXPR
1375 || TREE_CODE (val2) == MINUS_EXPR))
1377 tree n1, c1, n2, c2;
1378 enum tree_code code1, code2;
1380 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1381 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1382 same name, return -2. */
1383 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1385 code1 = SSA_NAME;
1386 n1 = val1;
1387 c1 = NULL_TREE;
1389 else
1391 code1 = TREE_CODE (val1);
1392 n1 = TREE_OPERAND (val1, 0);
1393 c1 = TREE_OPERAND (val1, 1);
1394 if (tree_int_cst_sgn (c1) == -1)
1396 if (is_negative_overflow_infinity (c1))
1397 return -2;
1398 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1399 if (!c1)
1400 return -2;
1401 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1405 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1407 code2 = SSA_NAME;
1408 n2 = val2;
1409 c2 = NULL_TREE;
1411 else
1413 code2 = TREE_CODE (val2);
1414 n2 = TREE_OPERAND (val2, 0);
1415 c2 = TREE_OPERAND (val2, 1);
1416 if (tree_int_cst_sgn (c2) == -1)
1418 if (is_negative_overflow_infinity (c2))
1419 return -2;
1420 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1421 if (!c2)
1422 return -2;
1423 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1427 /* Both values must use the same name. */
1428 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1430 n1 = TREE_OPERAND (n1, 0);
1431 n2 = TREE_OPERAND (n2, 0);
1433 if (n1 != n2)
1434 return -2;
1436 if (code1 == SSA_NAME && code2 == SSA_NAME)
1437 /* NAME == NAME */
1438 return 0;
1440 /* If overflow is defined we cannot simplify more. */
1441 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1442 return -2;
1444 if (strict_overflow_p != NULL
1445 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1446 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1447 *strict_overflow_p = true;
1449 if (code1 == SSA_NAME)
1451 if (code2 == PLUS_EXPR)
1452 /* NAME < NAME + CST */
1453 return -1;
1454 else if (code2 == MINUS_EXPR)
1455 /* NAME > NAME - CST */
1456 return 1;
1458 else if (code1 == PLUS_EXPR)
1460 if (code2 == SSA_NAME)
1461 /* NAME + CST > NAME */
1462 return 1;
1463 else if (code2 == PLUS_EXPR)
1464 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1465 return compare_values_warnv (c1, c2, strict_overflow_p);
1466 else if (code2 == MINUS_EXPR)
1467 /* NAME + CST1 > NAME - CST2 */
1468 return 1;
1470 else if (code1 == MINUS_EXPR)
1472 if (code2 == SSA_NAME)
1473 /* NAME - CST < NAME */
1474 return -1;
1475 else if (code2 == PLUS_EXPR)
1476 /* NAME - CST1 < NAME + CST2 */
1477 return -1;
1478 else if (code2 == MINUS_EXPR)
1479 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1480 C1 and C2 are swapped in the call to compare_values. */
1481 return compare_values_warnv (c2, c1, strict_overflow_p);
1484 gcc_unreachable ();
1487 /* We cannot compare non-constants. */
1488 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1489 return -2;
1491 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1493 /* We cannot compare overflowed values, except for overflow
1494 infinities. */
1495 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1497 if (strict_overflow_p != NULL)
1498 *strict_overflow_p = true;
1499 if (is_negative_overflow_infinity (val1))
1500 return is_negative_overflow_infinity (val2) ? 0 : -1;
1501 else if (is_negative_overflow_infinity (val2))
1502 return 1;
1503 else if (is_positive_overflow_infinity (val1))
1504 return is_positive_overflow_infinity (val2) ? 0 : 1;
1505 else if (is_positive_overflow_infinity (val2))
1506 return -1;
1507 return -2;
1510 return tree_int_cst_compare (val1, val2);
1512 else
1514 tree t;
1516 /* First see if VAL1 and VAL2 are not the same. */
1517 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1518 return 0;
1520 /* If VAL1 is a lower address than VAL2, return -1. */
1521 if (operand_less_p (val1, val2) == 1)
1522 return -1;
1524 /* If VAL1 is a higher address than VAL2, return +1. */
1525 if (operand_less_p (val2, val1) == 1)
1526 return 1;
1528 /* If VAL1 is different than VAL2, return +2.
1529 For integer constants we either have already returned -1 or 1
1530 or they are equivalent. We still might succeed in proving
1531 something about non-trivial operands. */
1532 if (TREE_CODE (val1) != INTEGER_CST
1533 || TREE_CODE (val2) != INTEGER_CST)
1535 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1536 if (t && integer_onep (t))
1537 return 2;
1540 return -2;
1544 /* Compare values like compare_values_warnv, but treat comparisons of
1545 nonconstants which rely on undefined overflow as incomparable. */
1547 static int
1548 compare_values (tree val1, tree val2)
1550 bool sop;
1551 int ret;
1553 sop = false;
1554 ret = compare_values_warnv (val1, val2, &sop);
1555 if (sop
1556 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1557 ret = -2;
1558 return ret;
1562 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1563 0 if VAL is not inside [MIN, MAX],
1564 -2 if we cannot tell either way.
1566 Benchmark compile/20001226-1.c compilation time after changing this
1567 function. */
1569 static inline int
1570 value_inside_range (tree val, tree min, tree max)
1572 int cmp1, cmp2;
1574 cmp1 = operand_less_p (val, min);
1575 if (cmp1 == -2)
1576 return -2;
1577 if (cmp1 == 1)
1578 return 0;
1580 cmp2 = operand_less_p (max, val);
1581 if (cmp2 == -2)
1582 return -2;
1584 return !cmp2;
1588 /* Return true if value ranges VR0 and VR1 have a non-empty
1589 intersection.
1591 Benchmark compile/20001226-1.c compilation time after changing this
1592 function.
1595 static inline bool
1596 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1598 /* The value ranges do not intersect if the maximum of the first range is
1599 less than the minimum of the second range or vice versa.
1600 When those relations are unknown, we can't do any better. */
1601 if (operand_less_p (vr0->max, vr1->min) != 0)
1602 return false;
1603 if (operand_less_p (vr1->max, vr0->min) != 0)
1604 return false;
1605 return true;
1609 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1610 include the value zero, -2 if we cannot tell. */
1612 static inline int
1613 range_includes_zero_p (tree min, tree max)
1615 tree zero = build_int_cst (TREE_TYPE (min), 0);
1616 return value_inside_range (zero, min, max);
1619 /* Return true if *VR is know to only contain nonnegative values. */
1621 static inline bool
1622 value_range_nonnegative_p (value_range_t *vr)
1624 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1625 which would return a useful value should be encoded as a
1626 VR_RANGE. */
1627 if (vr->type == VR_RANGE)
1629 int result = compare_values (vr->min, integer_zero_node);
1630 return (result == 0 || result == 1);
1633 return false;
1636 /* If *VR has a value rante that is a single constant value return that,
1637 otherwise return NULL_TREE. */
1639 static tree
1640 value_range_constant_singleton (value_range_t *vr)
1642 if (vr->type == VR_RANGE
1643 && operand_equal_p (vr->min, vr->max, 0)
1644 && is_gimple_min_invariant (vr->min))
1645 return vr->min;
1647 return NULL_TREE;
1650 /* If OP has a value range with a single constant value return that,
1651 otherwise return NULL_TREE. This returns OP itself if OP is a
1652 constant. */
1654 static tree
1655 op_with_constant_singleton_value_range (tree op)
1657 if (is_gimple_min_invariant (op))
1658 return op;
1660 if (TREE_CODE (op) != SSA_NAME)
1661 return NULL_TREE;
1663 return value_range_constant_singleton (get_value_range (op));
1666 /* Return true if op is in a boolean [0, 1] value-range. */
1668 static bool
1669 op_with_boolean_value_range_p (tree op)
1671 value_range_t *vr;
1673 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1674 return true;
1676 if (integer_zerop (op)
1677 || integer_onep (op))
1678 return true;
1680 if (TREE_CODE (op) != SSA_NAME)
1681 return false;
1683 vr = get_value_range (op);
1684 return (vr->type == VR_RANGE
1685 && integer_zerop (vr->min)
1686 && integer_onep (vr->max));
1689 /* Extract value range information from an ASSERT_EXPR EXPR and store
1690 it in *VR_P. */
1692 static void
1693 extract_range_from_assert (value_range_t *vr_p, tree expr)
1695 tree var, cond, limit, min, max, type;
1696 value_range_t *limit_vr;
1697 enum tree_code cond_code;
1699 var = ASSERT_EXPR_VAR (expr);
1700 cond = ASSERT_EXPR_COND (expr);
1702 gcc_assert (COMPARISON_CLASS_P (cond));
1704 /* Find VAR in the ASSERT_EXPR conditional. */
1705 if (var == TREE_OPERAND (cond, 0)
1706 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1707 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1709 /* If the predicate is of the form VAR COMP LIMIT, then we just
1710 take LIMIT from the RHS and use the same comparison code. */
1711 cond_code = TREE_CODE (cond);
1712 limit = TREE_OPERAND (cond, 1);
1713 cond = TREE_OPERAND (cond, 0);
1715 else
1717 /* If the predicate is of the form LIMIT COMP VAR, then we need
1718 to flip around the comparison code to create the proper range
1719 for VAR. */
1720 cond_code = swap_tree_comparison (TREE_CODE (cond));
1721 limit = TREE_OPERAND (cond, 0);
1722 cond = TREE_OPERAND (cond, 1);
1725 limit = avoid_overflow_infinity (limit);
1727 type = TREE_TYPE (var);
1728 gcc_assert (limit != var);
1730 /* For pointer arithmetic, we only keep track of pointer equality
1731 and inequality. */
1732 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1734 set_value_range_to_varying (vr_p);
1735 return;
1738 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1739 try to use LIMIT's range to avoid creating symbolic ranges
1740 unnecessarily. */
1741 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1743 /* LIMIT's range is only interesting if it has any useful information. */
1744 if (limit_vr
1745 && (limit_vr->type == VR_UNDEFINED
1746 || limit_vr->type == VR_VARYING
1747 || symbolic_range_p (limit_vr)))
1748 limit_vr = NULL;
1750 /* Initially, the new range has the same set of equivalences of
1751 VAR's range. This will be revised before returning the final
1752 value. Since assertions may be chained via mutually exclusive
1753 predicates, we will need to trim the set of equivalences before
1754 we are done. */
1755 gcc_assert (vr_p->equiv == NULL);
1756 add_equivalence (&vr_p->equiv, var);
1758 /* Extract a new range based on the asserted comparison for VAR and
1759 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1760 will only use it for equality comparisons (EQ_EXPR). For any
1761 other kind of assertion, we cannot derive a range from LIMIT's
1762 anti-range that can be used to describe the new range. For
1763 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1764 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1765 no single range for x_2 that could describe LE_EXPR, so we might
1766 as well build the range [b_4, +INF] for it.
1767 One special case we handle is extracting a range from a
1768 range test encoded as (unsigned)var + CST <= limit. */
1769 if (TREE_CODE (cond) == NOP_EXPR
1770 || TREE_CODE (cond) == PLUS_EXPR)
1772 if (TREE_CODE (cond) == PLUS_EXPR)
1774 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1775 TREE_OPERAND (cond, 1));
1776 max = int_const_binop (PLUS_EXPR, limit, min);
1777 cond = TREE_OPERAND (cond, 0);
1779 else
1781 min = build_int_cst (TREE_TYPE (var), 0);
1782 max = limit;
1785 /* Make sure to not set TREE_OVERFLOW on the final type
1786 conversion. We are willingly interpreting large positive
1787 unsigned values as negative signed values here. */
1788 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1789 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1791 /* We can transform a max, min range to an anti-range or
1792 vice-versa. Use set_and_canonicalize_value_range which does
1793 this for us. */
1794 if (cond_code == LE_EXPR)
1795 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1796 min, max, vr_p->equiv);
1797 else if (cond_code == GT_EXPR)
1798 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1799 min, max, vr_p->equiv);
1800 else
1801 gcc_unreachable ();
1803 else if (cond_code == EQ_EXPR)
1805 enum value_range_type range_type;
1807 if (limit_vr)
1809 range_type = limit_vr->type;
1810 min = limit_vr->min;
1811 max = limit_vr->max;
1813 else
1815 range_type = VR_RANGE;
1816 min = limit;
1817 max = limit;
1820 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1822 /* When asserting the equality VAR == LIMIT and LIMIT is another
1823 SSA name, the new range will also inherit the equivalence set
1824 from LIMIT. */
1825 if (TREE_CODE (limit) == SSA_NAME)
1826 add_equivalence (&vr_p->equiv, limit);
1828 else if (cond_code == NE_EXPR)
1830 /* As described above, when LIMIT's range is an anti-range and
1831 this assertion is an inequality (NE_EXPR), then we cannot
1832 derive anything from the anti-range. For instance, if
1833 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1834 not imply that VAR's range is [0, 0]. So, in the case of
1835 anti-ranges, we just assert the inequality using LIMIT and
1836 not its anti-range.
1838 If LIMIT_VR is a range, we can only use it to build a new
1839 anti-range if LIMIT_VR is a single-valued range. For
1840 instance, if LIMIT_VR is [0, 1], the predicate
1841 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1842 Rather, it means that for value 0 VAR should be ~[0, 0]
1843 and for value 1, VAR should be ~[1, 1]. We cannot
1844 represent these ranges.
1846 The only situation in which we can build a valid
1847 anti-range is when LIMIT_VR is a single-valued range
1848 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1849 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1850 if (limit_vr
1851 && limit_vr->type == VR_RANGE
1852 && compare_values (limit_vr->min, limit_vr->max) == 0)
1854 min = limit_vr->min;
1855 max = limit_vr->max;
1857 else
1859 /* In any other case, we cannot use LIMIT's range to build a
1860 valid anti-range. */
1861 min = max = limit;
1864 /* If MIN and MAX cover the whole range for their type, then
1865 just use the original LIMIT. */
1866 if (INTEGRAL_TYPE_P (type)
1867 && vrp_val_is_min (min)
1868 && vrp_val_is_max (max))
1869 min = max = limit;
1871 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1872 min, max, vr_p->equiv);
1874 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1876 min = TYPE_MIN_VALUE (type);
1878 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1879 max = limit;
1880 else
1882 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1883 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1884 LT_EXPR. */
1885 max = limit_vr->max;
1888 /* If the maximum value forces us to be out of bounds, simply punt.
1889 It would be pointless to try and do anything more since this
1890 all should be optimized away above us. */
1891 if ((cond_code == LT_EXPR
1892 && compare_values (max, min) == 0)
1893 || is_overflow_infinity (max))
1894 set_value_range_to_varying (vr_p);
1895 else
1897 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1898 if (cond_code == LT_EXPR)
1900 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1901 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1902 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1903 build_int_cst (TREE_TYPE (max), -1));
1904 else
1905 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1906 build_int_cst (TREE_TYPE (max), 1));
1907 if (EXPR_P (max))
1908 TREE_NO_WARNING (max) = 1;
1911 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1914 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1916 max = TYPE_MAX_VALUE (type);
1918 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1919 min = limit;
1920 else
1922 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1923 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1924 GT_EXPR. */
1925 min = limit_vr->min;
1928 /* If the minimum value forces us to be out of bounds, simply punt.
1929 It would be pointless to try and do anything more since this
1930 all should be optimized away above us. */
1931 if ((cond_code == GT_EXPR
1932 && compare_values (min, max) == 0)
1933 || is_overflow_infinity (min))
1934 set_value_range_to_varying (vr_p);
1935 else
1937 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1938 if (cond_code == GT_EXPR)
1940 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1941 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1942 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1943 build_int_cst (TREE_TYPE (min), -1));
1944 else
1945 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1946 build_int_cst (TREE_TYPE (min), 1));
1947 if (EXPR_P (min))
1948 TREE_NO_WARNING (min) = 1;
1951 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1954 else
1955 gcc_unreachable ();
1957 /* Finally intersect the new range with what we already know about var. */
1958 vrp_intersect_ranges (vr_p, get_value_range (var));
1962 /* Extract range information from SSA name VAR and store it in VR. If
1963 VAR has an interesting range, use it. Otherwise, create the
1964 range [VAR, VAR] and return it. This is useful in situations where
1965 we may have conditionals testing values of VARYING names. For
1966 instance,
1968 x_3 = y_5;
1969 if (x_3 > y_5)
1972 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1973 always false. */
1975 static void
1976 extract_range_from_ssa_name (value_range_t *vr, tree var)
1978 value_range_t *var_vr = get_value_range (var);
1980 if (var_vr->type != VR_VARYING)
1981 copy_value_range (vr, var_vr);
1982 else
1983 set_value_range (vr, VR_RANGE, var, var, NULL);
1985 add_equivalence (&vr->equiv, var);
1989 /* Wrapper around int_const_binop. If the operation overflows and we
1990 are not using wrapping arithmetic, then adjust the result to be
1991 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1992 NULL_TREE if we need to use an overflow infinity representation but
1993 the type does not support it. */
1995 static tree
1996 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1998 tree res;
2000 res = int_const_binop (code, val1, val2);
2002 /* If we are using unsigned arithmetic, operate symbolically
2003 on -INF and +INF as int_const_binop only handles signed overflow. */
2004 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
2006 int checkz = compare_values (res, val1);
2007 bool overflow = false;
2009 /* Ensure that res = val1 [+*] val2 >= val1
2010 or that res = val1 - val2 <= val1. */
2011 if ((code == PLUS_EXPR
2012 && !(checkz == 1 || checkz == 0))
2013 || (code == MINUS_EXPR
2014 && !(checkz == 0 || checkz == -1)))
2016 overflow = true;
2018 /* Checking for multiplication overflow is done by dividing the
2019 output of the multiplication by the first input of the
2020 multiplication. If the result of that division operation is
2021 not equal to the second input of the multiplication, then the
2022 multiplication overflowed. */
2023 else if (code == MULT_EXPR && !integer_zerop (val1))
2025 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
2026 res,
2027 val1);
2028 int check = compare_values (tmp, val2);
2030 if (check != 0)
2031 overflow = true;
2034 if (overflow)
2036 res = copy_node (res);
2037 TREE_OVERFLOW (res) = 1;
2041 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
2042 /* If the singed operation wraps then int_const_binop has done
2043 everything we want. */
2045 /* Signed division of -1/0 overflows and by the time it gets here
2046 returns NULL_TREE. */
2047 else if (!res)
2048 return NULL_TREE;
2049 else if ((TREE_OVERFLOW (res)
2050 && !TREE_OVERFLOW (val1)
2051 && !TREE_OVERFLOW (val2))
2052 || is_overflow_infinity (val1)
2053 || is_overflow_infinity (val2))
2055 /* If the operation overflowed but neither VAL1 nor VAL2 are
2056 overflown, return -INF or +INF depending on the operation
2057 and the combination of signs of the operands. */
2058 int sgn1 = tree_int_cst_sgn (val1);
2059 int sgn2 = tree_int_cst_sgn (val2);
2061 if (needs_overflow_infinity (TREE_TYPE (res))
2062 && !supports_overflow_infinity (TREE_TYPE (res)))
2063 return NULL_TREE;
2065 /* We have to punt on adding infinities of different signs,
2066 since we can't tell what the sign of the result should be.
2067 Likewise for subtracting infinities of the same sign. */
2068 if (((code == PLUS_EXPR && sgn1 != sgn2)
2069 || (code == MINUS_EXPR && sgn1 == sgn2))
2070 && is_overflow_infinity (val1)
2071 && is_overflow_infinity (val2))
2072 return NULL_TREE;
2074 /* Don't try to handle division or shifting of infinities. */
2075 if ((code == TRUNC_DIV_EXPR
2076 || code == FLOOR_DIV_EXPR
2077 || code == CEIL_DIV_EXPR
2078 || code == EXACT_DIV_EXPR
2079 || code == ROUND_DIV_EXPR
2080 || code == RSHIFT_EXPR)
2081 && (is_overflow_infinity (val1)
2082 || is_overflow_infinity (val2)))
2083 return NULL_TREE;
2085 /* Notice that we only need to handle the restricted set of
2086 operations handled by extract_range_from_binary_expr.
2087 Among them, only multiplication, addition and subtraction
2088 can yield overflow without overflown operands because we
2089 are working with integral types only... except in the
2090 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2091 for division too. */
2093 /* For multiplication, the sign of the overflow is given
2094 by the comparison of the signs of the operands. */
2095 if ((code == MULT_EXPR && sgn1 == sgn2)
2096 /* For addition, the operands must be of the same sign
2097 to yield an overflow. Its sign is therefore that
2098 of one of the operands, for example the first. For
2099 infinite operands X + -INF is negative, not positive. */
2100 || (code == PLUS_EXPR
2101 && (sgn1 >= 0
2102 ? !is_negative_overflow_infinity (val2)
2103 : is_positive_overflow_infinity (val2)))
2104 /* For subtraction, non-infinite operands must be of
2105 different signs to yield an overflow. Its sign is
2106 therefore that of the first operand or the opposite of
2107 that of the second operand. A first operand of 0 counts
2108 as positive here, for the corner case 0 - (-INF), which
2109 overflows, but must yield +INF. For infinite operands 0
2110 - INF is negative, not positive. */
2111 || (code == MINUS_EXPR
2112 && (sgn1 >= 0
2113 ? !is_positive_overflow_infinity (val2)
2114 : is_negative_overflow_infinity (val2)))
2115 /* We only get in here with positive shift count, so the
2116 overflow direction is the same as the sign of val1.
2117 Actually rshift does not overflow at all, but we only
2118 handle the case of shifting overflowed -INF and +INF. */
2119 || (code == RSHIFT_EXPR
2120 && sgn1 >= 0)
2121 /* For division, the only case is -INF / -1 = +INF. */
2122 || code == TRUNC_DIV_EXPR
2123 || code == FLOOR_DIV_EXPR
2124 || code == CEIL_DIV_EXPR
2125 || code == EXACT_DIV_EXPR
2126 || code == ROUND_DIV_EXPR)
2127 return (needs_overflow_infinity (TREE_TYPE (res))
2128 ? positive_overflow_infinity (TREE_TYPE (res))
2129 : TYPE_MAX_VALUE (TREE_TYPE (res)));
2130 else
2131 return (needs_overflow_infinity (TREE_TYPE (res))
2132 ? negative_overflow_infinity (TREE_TYPE (res))
2133 : TYPE_MIN_VALUE (TREE_TYPE (res)));
2136 return res;
2140 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2141 bitmask if some bit is unset, it means for all numbers in the range
2142 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2143 bitmask if some bit is set, it means for all numbers in the range
2144 the bit is 1, otherwise it might be 0 or 1. */
2146 static bool
2147 zero_nonzero_bits_from_vr (const tree expr_type,
2148 value_range_t *vr,
2149 wide_int *may_be_nonzero,
2150 wide_int *must_be_nonzero)
2152 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2153 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2154 if (!range_int_cst_p (vr)
2155 || is_overflow_infinity (vr->min)
2156 || is_overflow_infinity (vr->max))
2157 return false;
2159 if (range_int_cst_singleton_p (vr))
2161 *may_be_nonzero = vr->min;
2162 *must_be_nonzero = *may_be_nonzero;
2164 else if (tree_int_cst_sgn (vr->min) >= 0
2165 || tree_int_cst_sgn (vr->max) < 0)
2167 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2168 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2169 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2170 if (xor_mask != 0)
2172 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2173 may_be_nonzero->get_precision ());
2174 *may_be_nonzero = *may_be_nonzero | mask;
2175 *must_be_nonzero = must_be_nonzero->and_not (mask);
2179 return true;
2182 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2183 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2184 false otherwise. If *AR can be represented with a single range
2185 *VR1 will be VR_UNDEFINED. */
2187 static bool
2188 ranges_from_anti_range (value_range_t *ar,
2189 value_range_t *vr0, value_range_t *vr1)
2191 tree type = TREE_TYPE (ar->min);
2193 vr0->type = VR_UNDEFINED;
2194 vr1->type = VR_UNDEFINED;
2196 if (ar->type != VR_ANTI_RANGE
2197 || TREE_CODE (ar->min) != INTEGER_CST
2198 || TREE_CODE (ar->max) != INTEGER_CST
2199 || !vrp_val_min (type)
2200 || !vrp_val_max (type))
2201 return false;
2203 if (!vrp_val_is_min (ar->min))
2205 vr0->type = VR_RANGE;
2206 vr0->min = vrp_val_min (type);
2207 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2209 if (!vrp_val_is_max (ar->max))
2211 vr1->type = VR_RANGE;
2212 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2213 vr1->max = vrp_val_max (type);
2215 if (vr0->type == VR_UNDEFINED)
2217 *vr0 = *vr1;
2218 vr1->type = VR_UNDEFINED;
2221 return vr0->type != VR_UNDEFINED;
2224 /* Helper to extract a value-range *VR for a multiplicative operation
2225 *VR0 CODE *VR1. */
2227 static void
2228 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2229 enum tree_code code,
2230 value_range_t *vr0, value_range_t *vr1)
2232 enum value_range_type type;
2233 tree val[4];
2234 size_t i;
2235 tree min, max;
2236 bool sop;
2237 int cmp;
2239 /* Multiplications, divisions and shifts are a bit tricky to handle,
2240 depending on the mix of signs we have in the two ranges, we
2241 need to operate on different values to get the minimum and
2242 maximum values for the new range. One approach is to figure
2243 out all the variations of range combinations and do the
2244 operations.
2246 However, this involves several calls to compare_values and it
2247 is pretty convoluted. It's simpler to do the 4 operations
2248 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2249 MAX1) and then figure the smallest and largest values to form
2250 the new range. */
2251 gcc_assert (code == MULT_EXPR
2252 || code == TRUNC_DIV_EXPR
2253 || code == FLOOR_DIV_EXPR
2254 || code == CEIL_DIV_EXPR
2255 || code == EXACT_DIV_EXPR
2256 || code == ROUND_DIV_EXPR
2257 || code == RSHIFT_EXPR
2258 || code == LSHIFT_EXPR);
2259 gcc_assert ((vr0->type == VR_RANGE
2260 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2261 && vr0->type == vr1->type);
2263 type = vr0->type;
2265 /* Compute the 4 cross operations. */
2266 sop = false;
2267 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2268 if (val[0] == NULL_TREE)
2269 sop = true;
2271 if (vr1->max == vr1->min)
2272 val[1] = NULL_TREE;
2273 else
2275 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2276 if (val[1] == NULL_TREE)
2277 sop = true;
2280 if (vr0->max == vr0->min)
2281 val[2] = NULL_TREE;
2282 else
2284 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2285 if (val[2] == NULL_TREE)
2286 sop = true;
2289 if (vr0->min == vr0->max || vr1->min == vr1->max)
2290 val[3] = NULL_TREE;
2291 else
2293 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2294 if (val[3] == NULL_TREE)
2295 sop = true;
2298 if (sop)
2300 set_value_range_to_varying (vr);
2301 return;
2304 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2305 of VAL[i]. */
2306 min = val[0];
2307 max = val[0];
2308 for (i = 1; i < 4; i++)
2310 if (!is_gimple_min_invariant (min)
2311 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2312 || !is_gimple_min_invariant (max)
2313 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2314 break;
2316 if (val[i])
2318 if (!is_gimple_min_invariant (val[i])
2319 || (TREE_OVERFLOW (val[i])
2320 && !is_overflow_infinity (val[i])))
2322 /* If we found an overflowed value, set MIN and MAX
2323 to it so that we set the resulting range to
2324 VARYING. */
2325 min = max = val[i];
2326 break;
2329 if (compare_values (val[i], min) == -1)
2330 min = val[i];
2332 if (compare_values (val[i], max) == 1)
2333 max = val[i];
2337 /* If either MIN or MAX overflowed, then set the resulting range to
2338 VARYING. But we do accept an overflow infinity
2339 representation. */
2340 if (min == NULL_TREE
2341 || !is_gimple_min_invariant (min)
2342 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2343 || max == NULL_TREE
2344 || !is_gimple_min_invariant (max)
2345 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2347 set_value_range_to_varying (vr);
2348 return;
2351 /* We punt if:
2352 1) [-INF, +INF]
2353 2) [-INF, +-INF(OVF)]
2354 3) [+-INF(OVF), +INF]
2355 4) [+-INF(OVF), +-INF(OVF)]
2356 We learn nothing when we have INF and INF(OVF) on both sides.
2357 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2358 overflow. */
2359 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2360 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2362 set_value_range_to_varying (vr);
2363 return;
2366 cmp = compare_values (min, max);
2367 if (cmp == -2 || cmp == 1)
2369 /* If the new range has its limits swapped around (MIN > MAX),
2370 then the operation caused one of them to wrap around, mark
2371 the new range VARYING. */
2372 set_value_range_to_varying (vr);
2374 else
2375 set_value_range (vr, type, min, max, NULL);
2378 /* Extract range information from a binary operation CODE based on
2379 the ranges of each of its operands *VR0 and *VR1 with resulting
2380 type EXPR_TYPE. The resulting range is stored in *VR. */
2382 static void
2383 extract_range_from_binary_expr_1 (value_range_t *vr,
2384 enum tree_code code, tree expr_type,
2385 value_range_t *vr0_, value_range_t *vr1_)
2387 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2388 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2389 enum value_range_type type;
2390 tree min = NULL_TREE, max = NULL_TREE;
2391 int cmp;
2393 if (!INTEGRAL_TYPE_P (expr_type)
2394 && !POINTER_TYPE_P (expr_type))
2396 set_value_range_to_varying (vr);
2397 return;
2400 /* Not all binary expressions can be applied to ranges in a
2401 meaningful way. Handle only arithmetic operations. */
2402 if (code != PLUS_EXPR
2403 && code != MINUS_EXPR
2404 && code != POINTER_PLUS_EXPR
2405 && code != MULT_EXPR
2406 && code != TRUNC_DIV_EXPR
2407 && code != FLOOR_DIV_EXPR
2408 && code != CEIL_DIV_EXPR
2409 && code != EXACT_DIV_EXPR
2410 && code != ROUND_DIV_EXPR
2411 && code != TRUNC_MOD_EXPR
2412 && code != RSHIFT_EXPR
2413 && code != LSHIFT_EXPR
2414 && code != MIN_EXPR
2415 && code != MAX_EXPR
2416 && code != BIT_AND_EXPR
2417 && code != BIT_IOR_EXPR
2418 && code != BIT_XOR_EXPR)
2420 set_value_range_to_varying (vr);
2421 return;
2424 /* If both ranges are UNDEFINED, so is the result. */
2425 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2427 set_value_range_to_undefined (vr);
2428 return;
2430 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2431 code. At some point we may want to special-case operations that
2432 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2433 operand. */
2434 else if (vr0.type == VR_UNDEFINED)
2435 set_value_range_to_varying (&vr0);
2436 else if (vr1.type == VR_UNDEFINED)
2437 set_value_range_to_varying (&vr1);
2439 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2440 and express ~[] op X as ([]' op X) U ([]'' op X). */
2441 if (vr0.type == VR_ANTI_RANGE
2442 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2444 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2445 if (vrtem1.type != VR_UNDEFINED)
2447 value_range_t vrres = VR_INITIALIZER;
2448 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2449 &vrtem1, vr1_);
2450 vrp_meet (vr, &vrres);
2452 return;
2454 /* Likewise for X op ~[]. */
2455 if (vr1.type == VR_ANTI_RANGE
2456 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2458 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2459 if (vrtem1.type != VR_UNDEFINED)
2461 value_range_t vrres = VR_INITIALIZER;
2462 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2463 vr0_, &vrtem1);
2464 vrp_meet (vr, &vrres);
2466 return;
2469 /* The type of the resulting value range defaults to VR0.TYPE. */
2470 type = vr0.type;
2472 /* Refuse to operate on VARYING ranges, ranges of different kinds
2473 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2474 because we may be able to derive a useful range even if one of
2475 the operands is VR_VARYING or symbolic range. Similarly for
2476 divisions, MIN/MAX and PLUS/MINUS.
2478 TODO, we may be able to derive anti-ranges in some cases. */
2479 if (code != BIT_AND_EXPR
2480 && code != BIT_IOR_EXPR
2481 && code != TRUNC_DIV_EXPR
2482 && code != FLOOR_DIV_EXPR
2483 && code != CEIL_DIV_EXPR
2484 && code != EXACT_DIV_EXPR
2485 && code != ROUND_DIV_EXPR
2486 && code != TRUNC_MOD_EXPR
2487 && code != MIN_EXPR
2488 && code != MAX_EXPR
2489 && code != PLUS_EXPR
2490 && code != MINUS_EXPR
2491 && code != RSHIFT_EXPR
2492 && (vr0.type == VR_VARYING
2493 || vr1.type == VR_VARYING
2494 || vr0.type != vr1.type
2495 || symbolic_range_p (&vr0)
2496 || symbolic_range_p (&vr1)))
2498 set_value_range_to_varying (vr);
2499 return;
2502 /* Now evaluate the expression to determine the new range. */
2503 if (POINTER_TYPE_P (expr_type))
2505 if (code == MIN_EXPR || code == MAX_EXPR)
2507 /* For MIN/MAX expressions with pointers, we only care about
2508 nullness, if both are non null, then the result is nonnull.
2509 If both are null, then the result is null. Otherwise they
2510 are varying. */
2511 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2512 set_value_range_to_nonnull (vr, expr_type);
2513 else if (range_is_null (&vr0) && range_is_null (&vr1))
2514 set_value_range_to_null (vr, expr_type);
2515 else
2516 set_value_range_to_varying (vr);
2518 else if (code == POINTER_PLUS_EXPR)
2520 /* For pointer types, we are really only interested in asserting
2521 whether the expression evaluates to non-NULL. */
2522 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2523 set_value_range_to_nonnull (vr, expr_type);
2524 else if (range_is_null (&vr0) && range_is_null (&vr1))
2525 set_value_range_to_null (vr, expr_type);
2526 else
2527 set_value_range_to_varying (vr);
2529 else if (code == BIT_AND_EXPR)
2531 /* For pointer types, we are really only interested in asserting
2532 whether the expression evaluates to non-NULL. */
2533 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2534 set_value_range_to_nonnull (vr, expr_type);
2535 else if (range_is_null (&vr0) || range_is_null (&vr1))
2536 set_value_range_to_null (vr, expr_type);
2537 else
2538 set_value_range_to_varying (vr);
2540 else
2541 set_value_range_to_varying (vr);
2543 return;
2546 /* For integer ranges, apply the operation to each end of the
2547 range and see what we end up with. */
2548 if (code == PLUS_EXPR || code == MINUS_EXPR)
2550 const bool minus_p = (code == MINUS_EXPR);
2551 tree min_op0 = vr0.min;
2552 tree min_op1 = minus_p ? vr1.max : vr1.min;
2553 tree max_op0 = vr0.max;
2554 tree max_op1 = minus_p ? vr1.min : vr1.max;
2555 tree sym_min_op0 = NULL_TREE;
2556 tree sym_min_op1 = NULL_TREE;
2557 tree sym_max_op0 = NULL_TREE;
2558 tree sym_max_op1 = NULL_TREE;
2559 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2561 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2562 single-symbolic ranges, try to compute the precise resulting range,
2563 but only if we know that this resulting range will also be constant
2564 or single-symbolic. */
2565 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2566 && (TREE_CODE (min_op0) == INTEGER_CST
2567 || (sym_min_op0
2568 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2569 && (TREE_CODE (min_op1) == INTEGER_CST
2570 || (sym_min_op1
2571 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2572 && (!(sym_min_op0 && sym_min_op1)
2573 || (sym_min_op0 == sym_min_op1
2574 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2575 && (TREE_CODE (max_op0) == INTEGER_CST
2576 || (sym_max_op0
2577 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2578 && (TREE_CODE (max_op1) == INTEGER_CST
2579 || (sym_max_op1
2580 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2581 && (!(sym_max_op0 && sym_max_op1)
2582 || (sym_max_op0 == sym_max_op1
2583 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2585 const signop sgn = TYPE_SIGN (expr_type);
2586 const unsigned int prec = TYPE_PRECISION (expr_type);
2587 wide_int type_min, type_max, wmin, wmax;
2588 int min_ovf = 0;
2589 int max_ovf = 0;
2591 /* Get the lower and upper bounds of the type. */
2592 if (TYPE_OVERFLOW_WRAPS (expr_type))
2594 type_min = wi::min_value (prec, sgn);
2595 type_max = wi::max_value (prec, sgn);
2597 else
2599 type_min = vrp_val_min (expr_type);
2600 type_max = vrp_val_max (expr_type);
2603 /* Combine the lower bounds, if any. */
2604 if (min_op0 && min_op1)
2606 if (minus_p)
2608 wmin = wi::sub (min_op0, min_op1);
2610 /* Check for overflow. */
2611 if (wi::cmp (0, min_op1, sgn)
2612 != wi::cmp (wmin, min_op0, sgn))
2613 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2615 else
2617 wmin = wi::add (min_op0, min_op1);
2619 /* Check for overflow. */
2620 if (wi::cmp (min_op1, 0, sgn)
2621 != wi::cmp (wmin, min_op0, sgn))
2622 min_ovf = wi::cmp (min_op0, wmin, sgn);
2625 else if (min_op0)
2626 wmin = min_op0;
2627 else if (min_op1)
2628 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2629 else
2630 wmin = wi::shwi (0, prec);
2632 /* Combine the upper bounds, if any. */
2633 if (max_op0 && max_op1)
2635 if (minus_p)
2637 wmax = wi::sub (max_op0, max_op1);
2639 /* Check for overflow. */
2640 if (wi::cmp (0, max_op1, sgn)
2641 != wi::cmp (wmax, max_op0, sgn))
2642 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2644 else
2646 wmax = wi::add (max_op0, max_op1);
2648 if (wi::cmp (max_op1, 0, sgn)
2649 != wi::cmp (wmax, max_op0, sgn))
2650 max_ovf = wi::cmp (max_op0, wmax, sgn);
2653 else if (max_op0)
2654 wmax = max_op0;
2655 else if (max_op1)
2656 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2657 else
2658 wmax = wi::shwi (0, prec);
2660 /* Check for type overflow. */
2661 if (min_ovf == 0)
2663 if (wi::cmp (wmin, type_min, sgn) == -1)
2664 min_ovf = -1;
2665 else if (wi::cmp (wmin, type_max, sgn) == 1)
2666 min_ovf = 1;
2668 if (max_ovf == 0)
2670 if (wi::cmp (wmax, type_min, sgn) == -1)
2671 max_ovf = -1;
2672 else if (wi::cmp (wmax, type_max, sgn) == 1)
2673 max_ovf = 1;
2676 /* If we have overflow for the constant part and the resulting
2677 range will be symbolic, drop to VR_VARYING. */
2678 if ((min_ovf && sym_min_op0 != sym_min_op1)
2679 || (max_ovf && sym_max_op0 != sym_max_op1))
2681 set_value_range_to_varying (vr);
2682 return;
2685 if (TYPE_OVERFLOW_WRAPS (expr_type))
2687 /* If overflow wraps, truncate the values and adjust the
2688 range kind and bounds appropriately. */
2689 wide_int tmin = wide_int::from (wmin, prec, sgn);
2690 wide_int tmax = wide_int::from (wmax, prec, sgn);
2691 if (min_ovf == max_ovf)
2693 /* No overflow or both overflow or underflow. The
2694 range kind stays VR_RANGE. */
2695 min = wide_int_to_tree (expr_type, tmin);
2696 max = wide_int_to_tree (expr_type, tmax);
2698 else if (min_ovf == -1 && max_ovf == 1)
2700 /* Underflow and overflow, drop to VR_VARYING. */
2701 set_value_range_to_varying (vr);
2702 return;
2704 else
2706 /* Min underflow or max overflow. The range kind
2707 changes to VR_ANTI_RANGE. */
2708 bool covers = false;
2709 wide_int tem = tmin;
2710 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2711 || (max_ovf == 1 && min_ovf == 0));
2712 type = VR_ANTI_RANGE;
2713 tmin = tmax + 1;
2714 if (wi::cmp (tmin, tmax, sgn) < 0)
2715 covers = true;
2716 tmax = tem - 1;
2717 if (wi::cmp (tmax, tem, sgn) > 0)
2718 covers = true;
2719 /* If the anti-range would cover nothing, drop to varying.
2720 Likewise if the anti-range bounds are outside of the
2721 types values. */
2722 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2724 set_value_range_to_varying (vr);
2725 return;
2727 min = wide_int_to_tree (expr_type, tmin);
2728 max = wide_int_to_tree (expr_type, tmax);
2731 else
2733 /* If overflow does not wrap, saturate to the types min/max
2734 value. */
2735 if (min_ovf == -1)
2737 if (needs_overflow_infinity (expr_type)
2738 && supports_overflow_infinity (expr_type))
2739 min = negative_overflow_infinity (expr_type);
2740 else
2741 min = wide_int_to_tree (expr_type, type_min);
2743 else if (min_ovf == 1)
2745 if (needs_overflow_infinity (expr_type)
2746 && supports_overflow_infinity (expr_type))
2747 min = positive_overflow_infinity (expr_type);
2748 else
2749 min = wide_int_to_tree (expr_type, type_max);
2751 else
2752 min = wide_int_to_tree (expr_type, wmin);
2754 if (max_ovf == -1)
2756 if (needs_overflow_infinity (expr_type)
2757 && supports_overflow_infinity (expr_type))
2758 max = negative_overflow_infinity (expr_type);
2759 else
2760 max = wide_int_to_tree (expr_type, type_min);
2762 else if (max_ovf == 1)
2764 if (needs_overflow_infinity (expr_type)
2765 && supports_overflow_infinity (expr_type))
2766 max = positive_overflow_infinity (expr_type);
2767 else
2768 max = wide_int_to_tree (expr_type, type_max);
2770 else
2771 max = wide_int_to_tree (expr_type, wmax);
2774 if (needs_overflow_infinity (expr_type)
2775 && supports_overflow_infinity (expr_type))
2777 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2778 || (min_op1
2779 && (minus_p
2780 ? is_positive_overflow_infinity (min_op1)
2781 : is_negative_overflow_infinity (min_op1))))
2782 min = negative_overflow_infinity (expr_type);
2783 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2784 || (max_op1
2785 && (minus_p
2786 ? is_negative_overflow_infinity (max_op1)
2787 : is_positive_overflow_infinity (max_op1))))
2788 max = positive_overflow_infinity (expr_type);
2791 /* If the result lower bound is constant, we're done;
2792 otherwise, build the symbolic lower bound. */
2793 if (sym_min_op0 == sym_min_op1)
2795 else if (sym_min_op0)
2796 min = build_symbolic_expr (expr_type, sym_min_op0,
2797 neg_min_op0, min);
2798 else if (sym_min_op1)
2799 min = build_symbolic_expr (expr_type, sym_min_op1,
2800 neg_min_op1 ^ minus_p, min);
2802 /* Likewise for the upper bound. */
2803 if (sym_max_op0 == sym_max_op1)
2805 else if (sym_max_op0)
2806 max = build_symbolic_expr (expr_type, sym_max_op0,
2807 neg_max_op0, max);
2808 else if (sym_max_op1)
2809 max = build_symbolic_expr (expr_type, sym_max_op1,
2810 neg_max_op1 ^ minus_p, max);
2812 else
2814 /* For other cases, for example if we have a PLUS_EXPR with two
2815 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2816 to compute a precise range for such a case.
2817 ??? General even mixed range kind operations can be expressed
2818 by for example transforming ~[3, 5] + [1, 2] to range-only
2819 operations and a union primitive:
2820 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2821 [-INF+1, 4] U [6, +INF(OVF)]
2822 though usually the union is not exactly representable with
2823 a single range or anti-range as the above is
2824 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2825 but one could use a scheme similar to equivalences for this. */
2826 set_value_range_to_varying (vr);
2827 return;
2830 else if (code == MIN_EXPR
2831 || code == MAX_EXPR)
2833 if (vr0.type == VR_RANGE
2834 && !symbolic_range_p (&vr0))
2836 type = VR_RANGE;
2837 if (vr1.type == VR_RANGE
2838 && !symbolic_range_p (&vr1))
2840 /* For operations that make the resulting range directly
2841 proportional to the original ranges, apply the operation to
2842 the same end of each range. */
2843 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2844 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2846 else if (code == MIN_EXPR)
2848 min = vrp_val_min (expr_type);
2849 max = vr0.max;
2851 else if (code == MAX_EXPR)
2853 min = vr0.min;
2854 max = vrp_val_max (expr_type);
2857 else if (vr1.type == VR_RANGE
2858 && !symbolic_range_p (&vr1))
2860 type = VR_RANGE;
2861 if (code == MIN_EXPR)
2863 min = vrp_val_min (expr_type);
2864 max = vr1.max;
2866 else if (code == MAX_EXPR)
2868 min = vr1.min;
2869 max = vrp_val_max (expr_type);
2872 else
2874 set_value_range_to_varying (vr);
2875 return;
2878 else if (code == MULT_EXPR)
2880 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2881 drop to varying. This test requires 2*prec bits if both
2882 operands are signed and 2*prec + 2 bits if either is not. */
2884 signop sign = TYPE_SIGN (expr_type);
2885 unsigned int prec = TYPE_PRECISION (expr_type);
2887 if (range_int_cst_p (&vr0)
2888 && range_int_cst_p (&vr1)
2889 && TYPE_OVERFLOW_WRAPS (expr_type))
2891 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2892 typedef generic_wide_int
2893 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2894 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2895 vrp_int size = sizem1 + 1;
2897 /* Extend the values using the sign of the result to PREC2.
2898 From here on out, everthing is just signed math no matter
2899 what the input types were. */
2900 vrp_int min0 = vrp_int_cst (vr0.min);
2901 vrp_int max0 = vrp_int_cst (vr0.max);
2902 vrp_int min1 = vrp_int_cst (vr1.min);
2903 vrp_int max1 = vrp_int_cst (vr1.max);
2904 /* Canonicalize the intervals. */
2905 if (sign == UNSIGNED)
2907 if (wi::ltu_p (size, min0 + max0))
2909 min0 -= size;
2910 max0 -= size;
2913 if (wi::ltu_p (size, min1 + max1))
2915 min1 -= size;
2916 max1 -= size;
2920 vrp_int prod0 = min0 * min1;
2921 vrp_int prod1 = min0 * max1;
2922 vrp_int prod2 = max0 * min1;
2923 vrp_int prod3 = max0 * max1;
2925 /* Sort the 4 products so that min is in prod0 and max is in
2926 prod3. */
2927 /* min0min1 > max0max1 */
2928 if (wi::gts_p (prod0, prod3))
2930 vrp_int tmp = prod3;
2931 prod3 = prod0;
2932 prod0 = tmp;
2935 /* min0max1 > max0min1 */
2936 if (wi::gts_p (prod1, prod2))
2938 vrp_int tmp = prod2;
2939 prod2 = prod1;
2940 prod1 = tmp;
2943 if (wi::gts_p (prod0, prod1))
2945 vrp_int tmp = prod1;
2946 prod1 = prod0;
2947 prod0 = tmp;
2950 if (wi::gts_p (prod2, prod3))
2952 vrp_int tmp = prod3;
2953 prod3 = prod2;
2954 prod2 = tmp;
2957 /* diff = max - min. */
2958 prod2 = prod3 - prod0;
2959 if (wi::geu_p (prod2, sizem1))
2961 /* the range covers all values. */
2962 set_value_range_to_varying (vr);
2963 return;
2966 /* The following should handle the wrapping and selecting
2967 VR_ANTI_RANGE for us. */
2968 min = wide_int_to_tree (expr_type, prod0);
2969 max = wide_int_to_tree (expr_type, prod3);
2970 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2971 return;
2974 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2975 drop to VR_VARYING. It would take more effort to compute a
2976 precise range for such a case. For example, if we have
2977 op0 == 65536 and op1 == 65536 with their ranges both being
2978 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2979 we cannot claim that the product is in ~[0,0]. Note that we
2980 are guaranteed to have vr0.type == vr1.type at this
2981 point. */
2982 if (vr0.type == VR_ANTI_RANGE
2983 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2985 set_value_range_to_varying (vr);
2986 return;
2989 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2990 return;
2992 else if (code == RSHIFT_EXPR
2993 || code == LSHIFT_EXPR)
2995 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2996 then drop to VR_VARYING. Outside of this range we get undefined
2997 behavior from the shift operation. We cannot even trust
2998 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2999 shifts, and the operation at the tree level may be widened. */
3000 if (range_int_cst_p (&vr1)
3001 && compare_tree_int (vr1.min, 0) >= 0
3002 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
3004 if (code == RSHIFT_EXPR)
3006 /* Even if vr0 is VARYING or otherwise not usable, we can derive
3007 useful ranges just from the shift count. E.g.
3008 x >> 63 for signed 64-bit x is always [-1, 0]. */
3009 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3011 vr0.type = type = VR_RANGE;
3012 vr0.min = vrp_val_min (expr_type);
3013 vr0.max = vrp_val_max (expr_type);
3015 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3016 return;
3018 /* We can map lshifts by constants to MULT_EXPR handling. */
3019 else if (code == LSHIFT_EXPR
3020 && range_int_cst_singleton_p (&vr1))
3022 bool saved_flag_wrapv;
3023 value_range_t vr1p = VR_INITIALIZER;
3024 vr1p.type = VR_RANGE;
3025 vr1p.min = (wide_int_to_tree
3026 (expr_type,
3027 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
3028 TYPE_PRECISION (expr_type))));
3029 vr1p.max = vr1p.min;
3030 /* We have to use a wrapping multiply though as signed overflow
3031 on lshifts is implementation defined in C89. */
3032 saved_flag_wrapv = flag_wrapv;
3033 flag_wrapv = 1;
3034 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
3035 &vr0, &vr1p);
3036 flag_wrapv = saved_flag_wrapv;
3037 return;
3039 else if (code == LSHIFT_EXPR
3040 && range_int_cst_p (&vr0))
3042 int prec = TYPE_PRECISION (expr_type);
3043 int overflow_pos = prec;
3044 int bound_shift;
3045 wide_int low_bound, high_bound;
3046 bool uns = TYPE_UNSIGNED (expr_type);
3047 bool in_bounds = false;
3049 if (!uns)
3050 overflow_pos -= 1;
3052 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
3053 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
3054 overflow. However, for that to happen, vr1.max needs to be
3055 zero, which means vr1 is a singleton range of zero, which
3056 means it should be handled by the previous LSHIFT_EXPR
3057 if-clause. */
3058 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
3059 wide_int complement = ~(bound - 1);
3061 if (uns)
3063 low_bound = bound;
3064 high_bound = complement;
3065 if (wi::ltu_p (vr0.max, low_bound))
3067 /* [5, 6] << [1, 2] == [10, 24]. */
3068 /* We're shifting out only zeroes, the value increases
3069 monotonically. */
3070 in_bounds = true;
3072 else if (wi::ltu_p (high_bound, vr0.min))
3074 /* [0xffffff00, 0xffffffff] << [1, 2]
3075 == [0xfffffc00, 0xfffffffe]. */
3076 /* We're shifting out only ones, the value decreases
3077 monotonically. */
3078 in_bounds = true;
3081 else
3083 /* [-1, 1] << [1, 2] == [-4, 4]. */
3084 low_bound = complement;
3085 high_bound = bound;
3086 if (wi::lts_p (vr0.max, high_bound)
3087 && wi::lts_p (low_bound, vr0.min))
3089 /* For non-negative numbers, we're shifting out only
3090 zeroes, the value increases monotonically.
3091 For negative numbers, we're shifting out only ones, the
3092 value decreases monotomically. */
3093 in_bounds = true;
3097 if (in_bounds)
3099 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3100 return;
3104 set_value_range_to_varying (vr);
3105 return;
3107 else if (code == TRUNC_DIV_EXPR
3108 || code == FLOOR_DIV_EXPR
3109 || code == CEIL_DIV_EXPR
3110 || code == EXACT_DIV_EXPR
3111 || code == ROUND_DIV_EXPR)
3113 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3115 /* For division, if op1 has VR_RANGE but op0 does not, something
3116 can be deduced just from that range. Say [min, max] / [4, max]
3117 gives [min / 4, max / 4] range. */
3118 if (vr1.type == VR_RANGE
3119 && !symbolic_range_p (&vr1)
3120 && range_includes_zero_p (vr1.min, vr1.max) == 0)
3122 vr0.type = type = VR_RANGE;
3123 vr0.min = vrp_val_min (expr_type);
3124 vr0.max = vrp_val_max (expr_type);
3126 else
3128 set_value_range_to_varying (vr);
3129 return;
3133 /* For divisions, if flag_non_call_exceptions is true, we must
3134 not eliminate a division by zero. */
3135 if (cfun->can_throw_non_call_exceptions
3136 && (vr1.type != VR_RANGE
3137 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3139 set_value_range_to_varying (vr);
3140 return;
3143 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3144 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3145 include 0. */
3146 if (vr0.type == VR_RANGE
3147 && (vr1.type != VR_RANGE
3148 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3150 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
3151 int cmp;
3153 min = NULL_TREE;
3154 max = NULL_TREE;
3155 if (TYPE_UNSIGNED (expr_type)
3156 || value_range_nonnegative_p (&vr1))
3158 /* For unsigned division or when divisor is known
3159 to be non-negative, the range has to cover
3160 all numbers from 0 to max for positive max
3161 and all numbers from min to 0 for negative min. */
3162 cmp = compare_values (vr0.max, zero);
3163 if (cmp == -1)
3164 max = zero;
3165 else if (cmp == 0 || cmp == 1)
3166 max = vr0.max;
3167 else
3168 type = VR_VARYING;
3169 cmp = compare_values (vr0.min, zero);
3170 if (cmp == 1)
3171 min = zero;
3172 else if (cmp == 0 || cmp == -1)
3173 min = vr0.min;
3174 else
3175 type = VR_VARYING;
3177 else
3179 /* Otherwise the range is -max .. max or min .. -min
3180 depending on which bound is bigger in absolute value,
3181 as the division can change the sign. */
3182 abs_extent_range (vr, vr0.min, vr0.max);
3183 return;
3185 if (type == VR_VARYING)
3187 set_value_range_to_varying (vr);
3188 return;
3191 else
3193 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3194 return;
3197 else if (code == TRUNC_MOD_EXPR)
3199 if (vr1.type != VR_RANGE
3200 || range_includes_zero_p (vr1.min, vr1.max) != 0
3201 || vrp_val_is_min (vr1.min))
3203 set_value_range_to_varying (vr);
3204 return;
3206 type = VR_RANGE;
3207 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3208 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
3209 if (tree_int_cst_lt (max, vr1.max))
3210 max = vr1.max;
3211 max = int_const_binop (MINUS_EXPR, max, build_int_cst (TREE_TYPE (max), 1));
3212 /* If the dividend is non-negative the modulus will be
3213 non-negative as well. */
3214 if (TYPE_UNSIGNED (expr_type)
3215 || value_range_nonnegative_p (&vr0))
3216 min = build_int_cst (TREE_TYPE (max), 0);
3217 else
3218 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
3220 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3222 bool int_cst_range0, int_cst_range1;
3223 wide_int may_be_nonzero0, may_be_nonzero1;
3224 wide_int must_be_nonzero0, must_be_nonzero1;
3226 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3227 &may_be_nonzero0,
3228 &must_be_nonzero0);
3229 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3230 &may_be_nonzero1,
3231 &must_be_nonzero1);
3233 type = VR_RANGE;
3234 if (code == BIT_AND_EXPR)
3236 min = wide_int_to_tree (expr_type,
3237 must_be_nonzero0 & must_be_nonzero1);
3238 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3239 /* If both input ranges contain only negative values we can
3240 truncate the result range maximum to the minimum of the
3241 input range maxima. */
3242 if (int_cst_range0 && int_cst_range1
3243 && tree_int_cst_sgn (vr0.max) < 0
3244 && tree_int_cst_sgn (vr1.max) < 0)
3246 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3247 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3249 /* If either input range contains only non-negative values
3250 we can truncate the result range maximum to the respective
3251 maximum of the input range. */
3252 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3253 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3254 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3255 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3256 max = wide_int_to_tree (expr_type, wmax);
3258 else if (code == BIT_IOR_EXPR)
3260 max = wide_int_to_tree (expr_type,
3261 may_be_nonzero0 | may_be_nonzero1);
3262 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3263 /* If the input ranges contain only positive values we can
3264 truncate the minimum of the result range to the maximum
3265 of the input range minima. */
3266 if (int_cst_range0 && int_cst_range1
3267 && tree_int_cst_sgn (vr0.min) >= 0
3268 && tree_int_cst_sgn (vr1.min) >= 0)
3270 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3271 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3273 /* If either input range contains only negative values
3274 we can truncate the minimum of the result range to the
3275 respective minimum range. */
3276 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3277 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3278 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3279 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3280 min = wide_int_to_tree (expr_type, wmin);
3282 else if (code == BIT_XOR_EXPR)
3284 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3285 | ~(may_be_nonzero0 | may_be_nonzero1));
3286 wide_int result_one_bits
3287 = (must_be_nonzero0.and_not (may_be_nonzero1)
3288 | must_be_nonzero1.and_not (may_be_nonzero0));
3289 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3290 min = wide_int_to_tree (expr_type, result_one_bits);
3291 /* If the range has all positive or all negative values the
3292 result is better than VARYING. */
3293 if (tree_int_cst_sgn (min) < 0
3294 || tree_int_cst_sgn (max) >= 0)
3296 else
3297 max = min = NULL_TREE;
3300 else
3301 gcc_unreachable ();
3303 /* If either MIN or MAX overflowed, then set the resulting range to
3304 VARYING. But we do accept an overflow infinity representation. */
3305 if (min == NULL_TREE
3306 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3307 || max == NULL_TREE
3308 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3310 set_value_range_to_varying (vr);
3311 return;
3314 /* We punt if:
3315 1) [-INF, +INF]
3316 2) [-INF, +-INF(OVF)]
3317 3) [+-INF(OVF), +INF]
3318 4) [+-INF(OVF), +-INF(OVF)]
3319 We learn nothing when we have INF and INF(OVF) on both sides.
3320 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3321 overflow. */
3322 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3323 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3325 set_value_range_to_varying (vr);
3326 return;
3329 cmp = compare_values (min, max);
3330 if (cmp == -2 || cmp == 1)
3332 /* If the new range has its limits swapped around (MIN > MAX),
3333 then the operation caused one of them to wrap around, mark
3334 the new range VARYING. */
3335 set_value_range_to_varying (vr);
3337 else
3338 set_value_range (vr, type, min, max, NULL);
3341 /* Extract range information from a binary expression OP0 CODE OP1 based on
3342 the ranges of each of its operands with resulting type EXPR_TYPE.
3343 The resulting range is stored in *VR. */
3345 static void
3346 extract_range_from_binary_expr (value_range_t *vr,
3347 enum tree_code code,
3348 tree expr_type, tree op0, tree op1)
3350 value_range_t vr0 = VR_INITIALIZER;
3351 value_range_t vr1 = VR_INITIALIZER;
3353 /* Get value ranges for each operand. For constant operands, create
3354 a new value range with the operand to simplify processing. */
3355 if (TREE_CODE (op0) == SSA_NAME)
3356 vr0 = *(get_value_range (op0));
3357 else if (is_gimple_min_invariant (op0))
3358 set_value_range_to_value (&vr0, op0, NULL);
3359 else
3360 set_value_range_to_varying (&vr0);
3362 if (TREE_CODE (op1) == SSA_NAME)
3363 vr1 = *(get_value_range (op1));
3364 else if (is_gimple_min_invariant (op1))
3365 set_value_range_to_value (&vr1, op1, NULL);
3366 else
3367 set_value_range_to_varying (&vr1);
3369 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3371 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3372 and based on the other operand, for example if it was deduced from a
3373 symbolic comparison. When a bound of the range of the first operand
3374 is invariant, we set the corresponding bound of the new range to INF
3375 in order to avoid recursing on the range of the second operand. */
3376 if (vr->type == VR_VARYING
3377 && (code == PLUS_EXPR || code == MINUS_EXPR)
3378 && TREE_CODE (op1) == SSA_NAME
3379 && vr0.type == VR_RANGE
3380 && symbolic_range_based_on_p (&vr0, op1))
3382 const bool minus_p = (code == MINUS_EXPR);
3383 value_range_t n_vr1 = VR_INITIALIZER;
3385 /* Try with VR0 and [-INF, OP1]. */
3386 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3387 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3389 /* Try with VR0 and [OP1, +INF]. */
3390 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3391 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3393 /* Try with VR0 and [OP1, OP1]. */
3394 else
3395 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3397 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3400 if (vr->type == VR_VARYING
3401 && (code == PLUS_EXPR || code == MINUS_EXPR)
3402 && TREE_CODE (op0) == SSA_NAME
3403 && vr1.type == VR_RANGE
3404 && symbolic_range_based_on_p (&vr1, op0))
3406 const bool minus_p = (code == MINUS_EXPR);
3407 value_range_t n_vr0 = VR_INITIALIZER;
3409 /* Try with [-INF, OP0] and VR1. */
3410 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3411 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3413 /* Try with [OP0, +INF] and VR1. */
3414 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3415 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3417 /* Try with [OP0, OP0] and VR1. */
3418 else
3419 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3421 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3425 /* Extract range information from a unary operation CODE based on
3426 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3427 The The resulting range is stored in *VR. */
3429 static void
3430 extract_range_from_unary_expr_1 (value_range_t *vr,
3431 enum tree_code code, tree type,
3432 value_range_t *vr0_, tree op0_type)
3434 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3436 /* VRP only operates on integral and pointer types. */
3437 if (!(INTEGRAL_TYPE_P (op0_type)
3438 || POINTER_TYPE_P (op0_type))
3439 || !(INTEGRAL_TYPE_P (type)
3440 || POINTER_TYPE_P (type)))
3442 set_value_range_to_varying (vr);
3443 return;
3446 /* If VR0 is UNDEFINED, so is the result. */
3447 if (vr0.type == VR_UNDEFINED)
3449 set_value_range_to_undefined (vr);
3450 return;
3453 /* Handle operations that we express in terms of others. */
3454 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3456 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3457 copy_value_range (vr, &vr0);
3458 return;
3460 else if (code == NEGATE_EXPR)
3462 /* -X is simply 0 - X, so re-use existing code that also handles
3463 anti-ranges fine. */
3464 value_range_t zero = VR_INITIALIZER;
3465 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3466 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3467 return;
3469 else if (code == BIT_NOT_EXPR)
3471 /* ~X is simply -1 - X, so re-use existing code that also handles
3472 anti-ranges fine. */
3473 value_range_t minusone = VR_INITIALIZER;
3474 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3475 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3476 type, &minusone, &vr0);
3477 return;
3480 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3481 and express op ~[] as (op []') U (op []''). */
3482 if (vr0.type == VR_ANTI_RANGE
3483 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3485 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3486 if (vrtem1.type != VR_UNDEFINED)
3488 value_range_t vrres = VR_INITIALIZER;
3489 extract_range_from_unary_expr_1 (&vrres, code, type,
3490 &vrtem1, op0_type);
3491 vrp_meet (vr, &vrres);
3493 return;
3496 if (CONVERT_EXPR_CODE_P (code))
3498 tree inner_type = op0_type;
3499 tree outer_type = type;
3501 /* If the expression evaluates to a pointer, we are only interested in
3502 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3503 if (POINTER_TYPE_P (type))
3505 if (range_is_nonnull (&vr0))
3506 set_value_range_to_nonnull (vr, type);
3507 else if (range_is_null (&vr0))
3508 set_value_range_to_null (vr, type);
3509 else
3510 set_value_range_to_varying (vr);
3511 return;
3514 /* If VR0 is varying and we increase the type precision, assume
3515 a full range for the following transformation. */
3516 if (vr0.type == VR_VARYING
3517 && INTEGRAL_TYPE_P (inner_type)
3518 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3520 vr0.type = VR_RANGE;
3521 vr0.min = TYPE_MIN_VALUE (inner_type);
3522 vr0.max = TYPE_MAX_VALUE (inner_type);
3525 /* If VR0 is a constant range or anti-range and the conversion is
3526 not truncating we can convert the min and max values and
3527 canonicalize the resulting range. Otherwise we can do the
3528 conversion if the size of the range is less than what the
3529 precision of the target type can represent and the range is
3530 not an anti-range. */
3531 if ((vr0.type == VR_RANGE
3532 || vr0.type == VR_ANTI_RANGE)
3533 && TREE_CODE (vr0.min) == INTEGER_CST
3534 && TREE_CODE (vr0.max) == INTEGER_CST
3535 && (!is_overflow_infinity (vr0.min)
3536 || (vr0.type == VR_RANGE
3537 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3538 && needs_overflow_infinity (outer_type)
3539 && supports_overflow_infinity (outer_type)))
3540 && (!is_overflow_infinity (vr0.max)
3541 || (vr0.type == VR_RANGE
3542 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3543 && needs_overflow_infinity (outer_type)
3544 && supports_overflow_infinity (outer_type)))
3545 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3546 || (vr0.type == VR_RANGE
3547 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3548 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3549 size_int (TYPE_PRECISION (outer_type)))))))
3551 tree new_min, new_max;
3552 if (is_overflow_infinity (vr0.min))
3553 new_min = negative_overflow_infinity (outer_type);
3554 else
3555 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3556 0, false);
3557 if (is_overflow_infinity (vr0.max))
3558 new_max = positive_overflow_infinity (outer_type);
3559 else
3560 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3561 0, false);
3562 set_and_canonicalize_value_range (vr, vr0.type,
3563 new_min, new_max, NULL);
3564 return;
3567 set_value_range_to_varying (vr);
3568 return;
3570 else if (code == ABS_EXPR)
3572 tree min, max;
3573 int cmp;
3575 /* Pass through vr0 in the easy cases. */
3576 if (TYPE_UNSIGNED (type)
3577 || value_range_nonnegative_p (&vr0))
3579 copy_value_range (vr, &vr0);
3580 return;
3583 /* For the remaining varying or symbolic ranges we can't do anything
3584 useful. */
3585 if (vr0.type == VR_VARYING
3586 || symbolic_range_p (&vr0))
3588 set_value_range_to_varying (vr);
3589 return;
3592 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3593 useful range. */
3594 if (!TYPE_OVERFLOW_UNDEFINED (type)
3595 && ((vr0.type == VR_RANGE
3596 && vrp_val_is_min (vr0.min))
3597 || (vr0.type == VR_ANTI_RANGE
3598 && !vrp_val_is_min (vr0.min))))
3600 set_value_range_to_varying (vr);
3601 return;
3604 /* ABS_EXPR may flip the range around, if the original range
3605 included negative values. */
3606 if (is_overflow_infinity (vr0.min))
3607 min = positive_overflow_infinity (type);
3608 else if (!vrp_val_is_min (vr0.min))
3609 min = fold_unary_to_constant (code, type, vr0.min);
3610 else if (!needs_overflow_infinity (type))
3611 min = TYPE_MAX_VALUE (type);
3612 else if (supports_overflow_infinity (type))
3613 min = positive_overflow_infinity (type);
3614 else
3616 set_value_range_to_varying (vr);
3617 return;
3620 if (is_overflow_infinity (vr0.max))
3621 max = positive_overflow_infinity (type);
3622 else if (!vrp_val_is_min (vr0.max))
3623 max = fold_unary_to_constant (code, type, vr0.max);
3624 else if (!needs_overflow_infinity (type))
3625 max = TYPE_MAX_VALUE (type);
3626 else if (supports_overflow_infinity (type)
3627 /* We shouldn't generate [+INF, +INF] as set_value_range
3628 doesn't like this and ICEs. */
3629 && !is_positive_overflow_infinity (min))
3630 max = positive_overflow_infinity (type);
3631 else
3633 set_value_range_to_varying (vr);
3634 return;
3637 cmp = compare_values (min, max);
3639 /* If a VR_ANTI_RANGEs contains zero, then we have
3640 ~[-INF, min(MIN, MAX)]. */
3641 if (vr0.type == VR_ANTI_RANGE)
3643 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3645 /* Take the lower of the two values. */
3646 if (cmp != 1)
3647 max = min;
3649 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3650 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3651 flag_wrapv is set and the original anti-range doesn't include
3652 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3653 if (TYPE_OVERFLOW_WRAPS (type))
3655 tree type_min_value = TYPE_MIN_VALUE (type);
3657 min = (vr0.min != type_min_value
3658 ? int_const_binop (PLUS_EXPR, type_min_value,
3659 build_int_cst (TREE_TYPE (type_min_value), 1))
3660 : type_min_value);
3662 else
3664 if (overflow_infinity_range_p (&vr0))
3665 min = negative_overflow_infinity (type);
3666 else
3667 min = TYPE_MIN_VALUE (type);
3670 else
3672 /* All else has failed, so create the range [0, INF], even for
3673 flag_wrapv since TYPE_MIN_VALUE is in the original
3674 anti-range. */
3675 vr0.type = VR_RANGE;
3676 min = build_int_cst (type, 0);
3677 if (needs_overflow_infinity (type))
3679 if (supports_overflow_infinity (type))
3680 max = positive_overflow_infinity (type);
3681 else
3683 set_value_range_to_varying (vr);
3684 return;
3687 else
3688 max = TYPE_MAX_VALUE (type);
3692 /* If the range contains zero then we know that the minimum value in the
3693 range will be zero. */
3694 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3696 if (cmp == 1)
3697 max = min;
3698 min = build_int_cst (type, 0);
3700 else
3702 /* If the range was reversed, swap MIN and MAX. */
3703 if (cmp == 1)
3705 tree t = min;
3706 min = max;
3707 max = t;
3711 cmp = compare_values (min, max);
3712 if (cmp == -2 || cmp == 1)
3714 /* If the new range has its limits swapped around (MIN > MAX),
3715 then the operation caused one of them to wrap around, mark
3716 the new range VARYING. */
3717 set_value_range_to_varying (vr);
3719 else
3720 set_value_range (vr, vr0.type, min, max, NULL);
3721 return;
3724 /* For unhandled operations fall back to varying. */
3725 set_value_range_to_varying (vr);
3726 return;
3730 /* Extract range information from a unary expression CODE OP0 based on
3731 the range of its operand with resulting type TYPE.
3732 The resulting range is stored in *VR. */
3734 static void
3735 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3736 tree type, tree op0)
3738 value_range_t vr0 = VR_INITIALIZER;
3740 /* Get value ranges for the operand. For constant operands, create
3741 a new value range with the operand to simplify processing. */
3742 if (TREE_CODE (op0) == SSA_NAME)
3743 vr0 = *(get_value_range (op0));
3744 else if (is_gimple_min_invariant (op0))
3745 set_value_range_to_value (&vr0, op0, NULL);
3746 else
3747 set_value_range_to_varying (&vr0);
3749 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3753 /* Extract range information from a conditional expression STMT based on
3754 the ranges of each of its operands and the expression code. */
3756 static void
3757 extract_range_from_cond_expr (value_range_t *vr, gassign *stmt)
3759 tree op0, op1;
3760 value_range_t vr0 = VR_INITIALIZER;
3761 value_range_t vr1 = VR_INITIALIZER;
3763 /* Get value ranges for each operand. For constant operands, create
3764 a new value range with the operand to simplify processing. */
3765 op0 = gimple_assign_rhs2 (stmt);
3766 if (TREE_CODE (op0) == SSA_NAME)
3767 vr0 = *(get_value_range (op0));
3768 else if (is_gimple_min_invariant (op0))
3769 set_value_range_to_value (&vr0, op0, NULL);
3770 else
3771 set_value_range_to_varying (&vr0);
3773 op1 = gimple_assign_rhs3 (stmt);
3774 if (TREE_CODE (op1) == SSA_NAME)
3775 vr1 = *(get_value_range (op1));
3776 else if (is_gimple_min_invariant (op1))
3777 set_value_range_to_value (&vr1, op1, NULL);
3778 else
3779 set_value_range_to_varying (&vr1);
3781 /* The resulting value range is the union of the operand ranges */
3782 copy_value_range (vr, &vr0);
3783 vrp_meet (vr, &vr1);
3787 /* Extract range information from a comparison expression EXPR based
3788 on the range of its operand and the expression code. */
3790 static void
3791 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3792 tree type, tree op0, tree op1)
3794 bool sop = false;
3795 tree val;
3797 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3798 NULL);
3800 /* A disadvantage of using a special infinity as an overflow
3801 representation is that we lose the ability to record overflow
3802 when we don't have an infinity. So we have to ignore a result
3803 which relies on overflow. */
3805 if (val && !is_overflow_infinity (val) && !sop)
3807 /* Since this expression was found on the RHS of an assignment,
3808 its type may be different from _Bool. Convert VAL to EXPR's
3809 type. */
3810 val = fold_convert (type, val);
3811 if (is_gimple_min_invariant (val))
3812 set_value_range_to_value (vr, val, vr->equiv);
3813 else
3814 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3816 else
3817 /* The result of a comparison is always true or false. */
3818 set_value_range_to_truthvalue (vr, type);
3821 /* Helper function for simplify_internal_call_using_ranges and
3822 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3823 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3824 always overflow. Set *OVF to true if it is known to always
3825 overflow. */
3827 static bool
3828 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3829 tree op0, tree op1, bool *ovf)
3831 value_range_t vr0 = VR_INITIALIZER;
3832 value_range_t vr1 = VR_INITIALIZER;
3833 if (TREE_CODE (op0) == SSA_NAME)
3834 vr0 = *get_value_range (op0);
3835 else if (TREE_CODE (op0) == INTEGER_CST)
3836 set_value_range_to_value (&vr0, op0, NULL);
3837 else
3838 set_value_range_to_varying (&vr0);
3840 if (TREE_CODE (op1) == SSA_NAME)
3841 vr1 = *get_value_range (op1);
3842 else if (TREE_CODE (op1) == INTEGER_CST)
3843 set_value_range_to_value (&vr1, op1, NULL);
3844 else
3845 set_value_range_to_varying (&vr1);
3847 if (!range_int_cst_p (&vr0)
3848 || TREE_OVERFLOW (vr0.min)
3849 || TREE_OVERFLOW (vr0.max))
3851 vr0.min = vrp_val_min (TREE_TYPE (op0));
3852 vr0.max = vrp_val_max (TREE_TYPE (op0));
3854 if (!range_int_cst_p (&vr1)
3855 || TREE_OVERFLOW (vr1.min)
3856 || TREE_OVERFLOW (vr1.max))
3858 vr1.min = vrp_val_min (TREE_TYPE (op1));
3859 vr1.max = vrp_val_max (TREE_TYPE (op1));
3861 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3862 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3863 if (arith_overflowed_p (subcode, type, vr0.max,
3864 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3865 return false;
3866 if (subcode == MULT_EXPR)
3868 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3869 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3870 return false;
3872 if (*ovf)
3874 /* So far we found that there is an overflow on the boundaries.
3875 That doesn't prove that there is an overflow even for all values
3876 in between the boundaries. For that compute widest_int range
3877 of the result and see if it doesn't overlap the range of
3878 type. */
3879 widest_int wmin, wmax;
3880 widest_int w[4];
3881 int i;
3882 w[0] = wi::to_widest (vr0.min);
3883 w[1] = wi::to_widest (vr0.max);
3884 w[2] = wi::to_widest (vr1.min);
3885 w[3] = wi::to_widest (vr1.max);
3886 for (i = 0; i < 4; i++)
3888 widest_int wt;
3889 switch (subcode)
3891 case PLUS_EXPR:
3892 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3893 break;
3894 case MINUS_EXPR:
3895 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3896 break;
3897 case MULT_EXPR:
3898 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3899 break;
3900 default:
3901 gcc_unreachable ();
3903 if (i == 0)
3905 wmin = wt;
3906 wmax = wt;
3908 else
3910 wmin = wi::smin (wmin, wt);
3911 wmax = wi::smax (wmax, wt);
3914 /* The result of op0 CODE op1 is known to be in range
3915 [wmin, wmax]. */
3916 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3917 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3918 /* If all values in [wmin, wmax] are smaller than
3919 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3920 the arithmetic operation will always overflow. */
3921 if (wi::lts_p (wmax, wtmin) || wi::gts_p (wmin, wtmax))
3922 return true;
3923 return false;
3925 return true;
3928 /* Try to derive a nonnegative or nonzero range out of STMT relying
3929 primarily on generic routines in fold in conjunction with range data.
3930 Store the result in *VR */
3932 static void
3933 extract_range_basic (value_range_t *vr, gimple stmt)
3935 bool sop = false;
3936 tree type = gimple_expr_type (stmt);
3938 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3940 tree fndecl = gimple_call_fndecl (stmt), arg;
3941 int mini, maxi, zerov = 0, prec;
3943 switch (DECL_FUNCTION_CODE (fndecl))
3945 case BUILT_IN_CONSTANT_P:
3946 /* If the call is __builtin_constant_p and the argument is a
3947 function parameter resolve it to false. This avoids bogus
3948 array bound warnings.
3949 ??? We could do this as early as inlining is finished. */
3950 arg = gimple_call_arg (stmt, 0);
3951 if (TREE_CODE (arg) == SSA_NAME
3952 && SSA_NAME_IS_DEFAULT_DEF (arg)
3953 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3955 set_value_range_to_null (vr, type);
3956 return;
3958 break;
3959 /* Both __builtin_ffs* and __builtin_popcount return
3960 [0, prec]. */
3961 CASE_INT_FN (BUILT_IN_FFS):
3962 CASE_INT_FN (BUILT_IN_POPCOUNT):
3963 arg = gimple_call_arg (stmt, 0);
3964 prec = TYPE_PRECISION (TREE_TYPE (arg));
3965 mini = 0;
3966 maxi = prec;
3967 if (TREE_CODE (arg) == SSA_NAME)
3969 value_range_t *vr0 = get_value_range (arg);
3970 /* If arg is non-zero, then ffs or popcount
3971 are non-zero. */
3972 if (((vr0->type == VR_RANGE
3973 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3974 || (vr0->type == VR_ANTI_RANGE
3975 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3976 && !is_overflow_infinity (vr0->min)
3977 && !is_overflow_infinity (vr0->max))
3978 mini = 1;
3979 /* If some high bits are known to be zero,
3980 we can decrease the maximum. */
3981 if (vr0->type == VR_RANGE
3982 && TREE_CODE (vr0->max) == INTEGER_CST
3983 && !operand_less_p (vr0->min,
3984 build_zero_cst (TREE_TYPE (vr0->min)))
3985 && !is_overflow_infinity (vr0->max))
3986 maxi = tree_floor_log2 (vr0->max) + 1;
3988 goto bitop_builtin;
3989 /* __builtin_parity* returns [0, 1]. */
3990 CASE_INT_FN (BUILT_IN_PARITY):
3991 mini = 0;
3992 maxi = 1;
3993 goto bitop_builtin;
3994 /* __builtin_c[lt]z* return [0, prec-1], except for
3995 when the argument is 0, but that is undefined behavior.
3996 On many targets where the CLZ RTL or optab value is defined
3997 for 0 the value is prec, so include that in the range
3998 by default. */
3999 CASE_INT_FN (BUILT_IN_CLZ):
4000 arg = gimple_call_arg (stmt, 0);
4001 prec = TYPE_PRECISION (TREE_TYPE (arg));
4002 mini = 0;
4003 maxi = prec;
4004 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
4005 != CODE_FOR_nothing
4006 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
4007 zerov)
4008 /* Handle only the single common value. */
4009 && zerov != prec)
4010 /* Magic value to give up, unless vr0 proves
4011 arg is non-zero. */
4012 mini = -2;
4013 if (TREE_CODE (arg) == SSA_NAME)
4015 value_range_t *vr0 = get_value_range (arg);
4016 /* From clz of VR_RANGE minimum we can compute
4017 result maximum. */
4018 if (vr0->type == VR_RANGE
4019 && TREE_CODE (vr0->min) == INTEGER_CST
4020 && !is_overflow_infinity (vr0->min))
4022 maxi = prec - 1 - tree_floor_log2 (vr0->min);
4023 if (maxi != prec)
4024 mini = 0;
4026 else if (vr0->type == VR_ANTI_RANGE
4027 && integer_zerop (vr0->min)
4028 && !is_overflow_infinity (vr0->min))
4030 maxi = prec - 1;
4031 mini = 0;
4033 if (mini == -2)
4034 break;
4035 /* From clz of VR_RANGE maximum we can compute
4036 result minimum. */
4037 if (vr0->type == VR_RANGE
4038 && TREE_CODE (vr0->max) == INTEGER_CST
4039 && !is_overflow_infinity (vr0->max))
4041 mini = prec - 1 - tree_floor_log2 (vr0->max);
4042 if (mini == prec)
4043 break;
4046 if (mini == -2)
4047 break;
4048 goto bitop_builtin;
4049 /* __builtin_ctz* return [0, prec-1], except for
4050 when the argument is 0, but that is undefined behavior.
4051 If there is a ctz optab for this mode and
4052 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
4053 otherwise just assume 0 won't be seen. */
4054 CASE_INT_FN (BUILT_IN_CTZ):
4055 arg = gimple_call_arg (stmt, 0);
4056 prec = TYPE_PRECISION (TREE_TYPE (arg));
4057 mini = 0;
4058 maxi = prec - 1;
4059 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
4060 != CODE_FOR_nothing
4061 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
4062 zerov))
4064 /* Handle only the two common values. */
4065 if (zerov == -1)
4066 mini = -1;
4067 else if (zerov == prec)
4068 maxi = prec;
4069 else
4070 /* Magic value to give up, unless vr0 proves
4071 arg is non-zero. */
4072 mini = -2;
4074 if (TREE_CODE (arg) == SSA_NAME)
4076 value_range_t *vr0 = get_value_range (arg);
4077 /* If arg is non-zero, then use [0, prec - 1]. */
4078 if (((vr0->type == VR_RANGE
4079 && integer_nonzerop (vr0->min))
4080 || (vr0->type == VR_ANTI_RANGE
4081 && integer_zerop (vr0->min)))
4082 && !is_overflow_infinity (vr0->min))
4084 mini = 0;
4085 maxi = prec - 1;
4087 /* If some high bits are known to be zero,
4088 we can decrease the result maximum. */
4089 if (vr0->type == VR_RANGE
4090 && TREE_CODE (vr0->max) == INTEGER_CST
4091 && !is_overflow_infinity (vr0->max))
4093 maxi = tree_floor_log2 (vr0->max);
4094 /* For vr0 [0, 0] give up. */
4095 if (maxi == -1)
4096 break;
4099 if (mini == -2)
4100 break;
4101 goto bitop_builtin;
4102 /* __builtin_clrsb* returns [0, prec-1]. */
4103 CASE_INT_FN (BUILT_IN_CLRSB):
4104 arg = gimple_call_arg (stmt, 0);
4105 prec = TYPE_PRECISION (TREE_TYPE (arg));
4106 mini = 0;
4107 maxi = prec - 1;
4108 goto bitop_builtin;
4109 bitop_builtin:
4110 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
4111 build_int_cst (type, maxi), NULL);
4112 return;
4113 default:
4114 break;
4117 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
4119 enum tree_code subcode = ERROR_MARK;
4120 switch (gimple_call_internal_fn (stmt))
4122 case IFN_UBSAN_CHECK_ADD:
4123 subcode = PLUS_EXPR;
4124 break;
4125 case IFN_UBSAN_CHECK_SUB:
4126 subcode = MINUS_EXPR;
4127 break;
4128 case IFN_UBSAN_CHECK_MUL:
4129 subcode = MULT_EXPR;
4130 break;
4131 default:
4132 break;
4134 if (subcode != ERROR_MARK)
4136 bool saved_flag_wrapv = flag_wrapv;
4137 /* Pretend the arithmetics is wrapping. If there is
4138 any overflow, we'll complain, but will actually do
4139 wrapping operation. */
4140 flag_wrapv = 1;
4141 extract_range_from_binary_expr (vr, subcode, type,
4142 gimple_call_arg (stmt, 0),
4143 gimple_call_arg (stmt, 1));
4144 flag_wrapv = saved_flag_wrapv;
4146 /* If for both arguments vrp_valueize returned non-NULL,
4147 this should have been already folded and if not, it
4148 wasn't folded because of overflow. Avoid removing the
4149 UBSAN_CHECK_* calls in that case. */
4150 if (vr->type == VR_RANGE
4151 && (vr->min == vr->max
4152 || operand_equal_p (vr->min, vr->max, 0)))
4153 set_value_range_to_varying (vr);
4154 return;
4157 /* Handle extraction of the two results (result of arithmetics and
4158 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4159 internal function. */
4160 else if (is_gimple_assign (stmt)
4161 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4162 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4163 && INTEGRAL_TYPE_P (type))
4165 enum tree_code code = gimple_assign_rhs_code (stmt);
4166 tree op = gimple_assign_rhs1 (stmt);
4167 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4169 gimple g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4170 if (is_gimple_call (g) && gimple_call_internal_p (g))
4172 enum tree_code subcode = ERROR_MARK;
4173 switch (gimple_call_internal_fn (g))
4175 case IFN_ADD_OVERFLOW:
4176 subcode = PLUS_EXPR;
4177 break;
4178 case IFN_SUB_OVERFLOW:
4179 subcode = MINUS_EXPR;
4180 break;
4181 case IFN_MUL_OVERFLOW:
4182 subcode = MULT_EXPR;
4183 break;
4184 default:
4185 break;
4187 if (subcode != ERROR_MARK)
4189 tree op0 = gimple_call_arg (g, 0);
4190 tree op1 = gimple_call_arg (g, 1);
4191 if (code == IMAGPART_EXPR)
4193 bool ovf = false;
4194 if (check_for_binary_op_overflow (subcode, type,
4195 op0, op1, &ovf))
4196 set_value_range_to_value (vr,
4197 build_int_cst (type, ovf),
4198 NULL);
4199 else
4200 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4201 build_int_cst (type, 1), NULL);
4203 else if (types_compatible_p (type, TREE_TYPE (op0))
4204 && types_compatible_p (type, TREE_TYPE (op1)))
4206 bool saved_flag_wrapv = flag_wrapv;
4207 /* Pretend the arithmetics is wrapping. If there is
4208 any overflow, IMAGPART_EXPR will be set. */
4209 flag_wrapv = 1;
4210 extract_range_from_binary_expr (vr, subcode, type,
4211 op0, op1);
4212 flag_wrapv = saved_flag_wrapv;
4214 else
4216 value_range_t vr0 = VR_INITIALIZER;
4217 value_range_t vr1 = VR_INITIALIZER;
4218 bool saved_flag_wrapv = flag_wrapv;
4219 /* Pretend the arithmetics is wrapping. If there is
4220 any overflow, IMAGPART_EXPR will be set. */
4221 flag_wrapv = 1;
4222 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4223 type, op0);
4224 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4225 type, op1);
4226 extract_range_from_binary_expr_1 (vr, subcode, type,
4227 &vr0, &vr1);
4228 flag_wrapv = saved_flag_wrapv;
4230 return;
4235 if (INTEGRAL_TYPE_P (type)
4236 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4237 set_value_range_to_nonnegative (vr, type,
4238 sop || stmt_overflow_infinity (stmt));
4239 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4240 && !sop)
4241 set_value_range_to_nonnull (vr, type);
4242 else
4243 set_value_range_to_varying (vr);
4247 /* Try to compute a useful range out of assignment STMT and store it
4248 in *VR. */
4250 static void
4251 extract_range_from_assignment (value_range_t *vr, gassign *stmt)
4253 enum tree_code code = gimple_assign_rhs_code (stmt);
4255 if (code == ASSERT_EXPR)
4256 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4257 else if (code == SSA_NAME)
4258 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4259 else if (TREE_CODE_CLASS (code) == tcc_binary)
4260 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4261 gimple_expr_type (stmt),
4262 gimple_assign_rhs1 (stmt),
4263 gimple_assign_rhs2 (stmt));
4264 else if (TREE_CODE_CLASS (code) == tcc_unary)
4265 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4266 gimple_expr_type (stmt),
4267 gimple_assign_rhs1 (stmt));
4268 else if (code == COND_EXPR)
4269 extract_range_from_cond_expr (vr, stmt);
4270 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4271 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4272 gimple_expr_type (stmt),
4273 gimple_assign_rhs1 (stmt),
4274 gimple_assign_rhs2 (stmt));
4275 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4276 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4277 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4278 else
4279 set_value_range_to_varying (vr);
4281 if (vr->type == VR_VARYING)
4282 extract_range_basic (vr, stmt);
4285 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4286 would be profitable to adjust VR using scalar evolution information
4287 for VAR. If so, update VR with the new limits. */
4289 static void
4290 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
4291 gimple stmt, tree var)
4293 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4294 enum ev_direction dir;
4296 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4297 better opportunities than a regular range, but I'm not sure. */
4298 if (vr->type == VR_ANTI_RANGE)
4299 return;
4301 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4303 /* Like in PR19590, scev can return a constant function. */
4304 if (is_gimple_min_invariant (chrec))
4306 set_value_range_to_value (vr, chrec, vr->equiv);
4307 return;
4310 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4311 return;
4313 init = initial_condition_in_loop_num (chrec, loop->num);
4314 tem = op_with_constant_singleton_value_range (init);
4315 if (tem)
4316 init = tem;
4317 step = evolution_part_in_loop_num (chrec, loop->num);
4318 tem = op_with_constant_singleton_value_range (step);
4319 if (tem)
4320 step = tem;
4322 /* If STEP is symbolic, we can't know whether INIT will be the
4323 minimum or maximum value in the range. Also, unless INIT is
4324 a simple expression, compare_values and possibly other functions
4325 in tree-vrp won't be able to handle it. */
4326 if (step == NULL_TREE
4327 || !is_gimple_min_invariant (step)
4328 || !valid_value_p (init))
4329 return;
4331 dir = scev_direction (chrec);
4332 if (/* Do not adjust ranges if we do not know whether the iv increases
4333 or decreases, ... */
4334 dir == EV_DIR_UNKNOWN
4335 /* ... or if it may wrap. */
4336 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4337 true))
4338 return;
4340 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4341 negative_overflow_infinity and positive_overflow_infinity,
4342 because we have concluded that the loop probably does not
4343 wrap. */
4345 type = TREE_TYPE (var);
4346 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4347 tmin = lower_bound_in_type (type, type);
4348 else
4349 tmin = TYPE_MIN_VALUE (type);
4350 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4351 tmax = upper_bound_in_type (type, type);
4352 else
4353 tmax = TYPE_MAX_VALUE (type);
4355 /* Try to use estimated number of iterations for the loop to constrain the
4356 final value in the evolution. */
4357 if (TREE_CODE (step) == INTEGER_CST
4358 && is_gimple_val (init)
4359 && (TREE_CODE (init) != SSA_NAME
4360 || get_value_range (init)->type == VR_RANGE))
4362 widest_int nit;
4364 /* We are only entering here for loop header PHI nodes, so using
4365 the number of latch executions is the correct thing to use. */
4366 if (max_loop_iterations (loop, &nit))
4368 value_range_t maxvr = VR_INITIALIZER;
4369 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4370 bool overflow;
4372 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4373 &overflow);
4374 /* If the multiplication overflowed we can't do a meaningful
4375 adjustment. Likewise if the result doesn't fit in the type
4376 of the induction variable. For a signed type we have to
4377 check whether the result has the expected signedness which
4378 is that of the step as number of iterations is unsigned. */
4379 if (!overflow
4380 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4381 && (sgn == UNSIGNED
4382 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4384 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4385 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4386 TREE_TYPE (init), init, tem);
4387 /* Likewise if the addition did. */
4388 if (maxvr.type == VR_RANGE)
4390 tmin = maxvr.min;
4391 tmax = maxvr.max;
4397 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4399 min = tmin;
4400 max = tmax;
4402 /* For VARYING or UNDEFINED ranges, just about anything we get
4403 from scalar evolutions should be better. */
4405 if (dir == EV_DIR_DECREASES)
4406 max = init;
4407 else
4408 min = init;
4410 else if (vr->type == VR_RANGE)
4412 min = vr->min;
4413 max = vr->max;
4415 if (dir == EV_DIR_DECREASES)
4417 /* INIT is the maximum value. If INIT is lower than VR->MAX
4418 but no smaller than VR->MIN, set VR->MAX to INIT. */
4419 if (compare_values (init, max) == -1)
4420 max = init;
4422 /* According to the loop information, the variable does not
4423 overflow. If we think it does, probably because of an
4424 overflow due to arithmetic on a different INF value,
4425 reset now. */
4426 if (is_negative_overflow_infinity (min)
4427 || compare_values (min, tmin) == -1)
4428 min = tmin;
4431 else
4433 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4434 if (compare_values (init, min) == 1)
4435 min = init;
4437 if (is_positive_overflow_infinity (max)
4438 || compare_values (tmax, max) == -1)
4439 max = tmax;
4442 else
4443 return;
4445 /* If we just created an invalid range with the minimum
4446 greater than the maximum, we fail conservatively.
4447 This should happen only in unreachable
4448 parts of code, or for invalid programs. */
4449 if (compare_values (min, max) == 1
4450 || (is_negative_overflow_infinity (min)
4451 && is_positive_overflow_infinity (max)))
4452 return;
4454 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4458 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4460 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4461 all the values in the ranges.
4463 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4465 - Return NULL_TREE if it is not always possible to determine the
4466 value of the comparison.
4468 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4469 overflow infinity was used in the test. */
4472 static tree
4473 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4474 bool *strict_overflow_p)
4476 /* VARYING or UNDEFINED ranges cannot be compared. */
4477 if (vr0->type == VR_VARYING
4478 || vr0->type == VR_UNDEFINED
4479 || vr1->type == VR_VARYING
4480 || vr1->type == VR_UNDEFINED)
4481 return NULL_TREE;
4483 /* Anti-ranges need to be handled separately. */
4484 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4486 /* If both are anti-ranges, then we cannot compute any
4487 comparison. */
4488 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4489 return NULL_TREE;
4491 /* These comparisons are never statically computable. */
4492 if (comp == GT_EXPR
4493 || comp == GE_EXPR
4494 || comp == LT_EXPR
4495 || comp == LE_EXPR)
4496 return NULL_TREE;
4498 /* Equality can be computed only between a range and an
4499 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4500 if (vr0->type == VR_RANGE)
4502 /* To simplify processing, make VR0 the anti-range. */
4503 value_range_t *tmp = vr0;
4504 vr0 = vr1;
4505 vr1 = tmp;
4508 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4510 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4511 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4512 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4514 return NULL_TREE;
4517 if (!usable_range_p (vr0, strict_overflow_p)
4518 || !usable_range_p (vr1, strict_overflow_p))
4519 return NULL_TREE;
4521 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4522 operands around and change the comparison code. */
4523 if (comp == GT_EXPR || comp == GE_EXPR)
4525 value_range_t *tmp;
4526 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4527 tmp = vr0;
4528 vr0 = vr1;
4529 vr1 = tmp;
4532 if (comp == EQ_EXPR)
4534 /* Equality may only be computed if both ranges represent
4535 exactly one value. */
4536 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4537 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4539 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4540 strict_overflow_p);
4541 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4542 strict_overflow_p);
4543 if (cmp_min == 0 && cmp_max == 0)
4544 return boolean_true_node;
4545 else if (cmp_min != -2 && cmp_max != -2)
4546 return boolean_false_node;
4548 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4549 else if (compare_values_warnv (vr0->min, vr1->max,
4550 strict_overflow_p) == 1
4551 || compare_values_warnv (vr1->min, vr0->max,
4552 strict_overflow_p) == 1)
4553 return boolean_false_node;
4555 return NULL_TREE;
4557 else if (comp == NE_EXPR)
4559 int cmp1, cmp2;
4561 /* If VR0 is completely to the left or completely to the right
4562 of VR1, they are always different. Notice that we need to
4563 make sure that both comparisons yield similar results to
4564 avoid comparing values that cannot be compared at
4565 compile-time. */
4566 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4567 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4568 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4569 return boolean_true_node;
4571 /* If VR0 and VR1 represent a single value and are identical,
4572 return false. */
4573 else if (compare_values_warnv (vr0->min, vr0->max,
4574 strict_overflow_p) == 0
4575 && compare_values_warnv (vr1->min, vr1->max,
4576 strict_overflow_p) == 0
4577 && compare_values_warnv (vr0->min, vr1->min,
4578 strict_overflow_p) == 0
4579 && compare_values_warnv (vr0->max, vr1->max,
4580 strict_overflow_p) == 0)
4581 return boolean_false_node;
4583 /* Otherwise, they may or may not be different. */
4584 else
4585 return NULL_TREE;
4587 else if (comp == LT_EXPR || comp == LE_EXPR)
4589 int tst;
4591 /* If VR0 is to the left of VR1, return true. */
4592 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4593 if ((comp == LT_EXPR && tst == -1)
4594 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4596 if (overflow_infinity_range_p (vr0)
4597 || overflow_infinity_range_p (vr1))
4598 *strict_overflow_p = true;
4599 return boolean_true_node;
4602 /* If VR0 is to the right of VR1, return false. */
4603 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4604 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4605 || (comp == LE_EXPR && tst == 1))
4607 if (overflow_infinity_range_p (vr0)
4608 || overflow_infinity_range_p (vr1))
4609 *strict_overflow_p = true;
4610 return boolean_false_node;
4613 /* Otherwise, we don't know. */
4614 return NULL_TREE;
4617 gcc_unreachable ();
4621 /* Given a value range VR, a value VAL and a comparison code COMP, return
4622 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4623 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4624 always returns false. Return NULL_TREE if it is not always
4625 possible to determine the value of the comparison. Also set
4626 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4627 infinity was used in the test. */
4629 static tree
4630 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4631 bool *strict_overflow_p)
4633 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4634 return NULL_TREE;
4636 /* Anti-ranges need to be handled separately. */
4637 if (vr->type == VR_ANTI_RANGE)
4639 /* For anti-ranges, the only predicates that we can compute at
4640 compile time are equality and inequality. */
4641 if (comp == GT_EXPR
4642 || comp == GE_EXPR
4643 || comp == LT_EXPR
4644 || comp == LE_EXPR)
4645 return NULL_TREE;
4647 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4648 if (value_inside_range (val, vr->min, vr->max) == 1)
4649 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4651 return NULL_TREE;
4654 if (!usable_range_p (vr, strict_overflow_p))
4655 return NULL_TREE;
4657 if (comp == EQ_EXPR)
4659 /* EQ_EXPR may only be computed if VR represents exactly
4660 one value. */
4661 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4663 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4664 if (cmp == 0)
4665 return boolean_true_node;
4666 else if (cmp == -1 || cmp == 1 || cmp == 2)
4667 return boolean_false_node;
4669 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4670 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4671 return boolean_false_node;
4673 return NULL_TREE;
4675 else if (comp == NE_EXPR)
4677 /* If VAL is not inside VR, then they are always different. */
4678 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4679 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4680 return boolean_true_node;
4682 /* If VR represents exactly one value equal to VAL, then return
4683 false. */
4684 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4685 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4686 return boolean_false_node;
4688 /* Otherwise, they may or may not be different. */
4689 return NULL_TREE;
4691 else if (comp == LT_EXPR || comp == LE_EXPR)
4693 int tst;
4695 /* If VR is to the left of VAL, return true. */
4696 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4697 if ((comp == LT_EXPR && tst == -1)
4698 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4700 if (overflow_infinity_range_p (vr))
4701 *strict_overflow_p = true;
4702 return boolean_true_node;
4705 /* If VR is to the right of VAL, return false. */
4706 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4707 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4708 || (comp == LE_EXPR && tst == 1))
4710 if (overflow_infinity_range_p (vr))
4711 *strict_overflow_p = true;
4712 return boolean_false_node;
4715 /* Otherwise, we don't know. */
4716 return NULL_TREE;
4718 else if (comp == GT_EXPR || comp == GE_EXPR)
4720 int tst;
4722 /* If VR is to the right of VAL, return true. */
4723 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4724 if ((comp == GT_EXPR && tst == 1)
4725 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4727 if (overflow_infinity_range_p (vr))
4728 *strict_overflow_p = true;
4729 return boolean_true_node;
4732 /* If VR is to the left of VAL, return false. */
4733 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4734 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4735 || (comp == GE_EXPR && tst == -1))
4737 if (overflow_infinity_range_p (vr))
4738 *strict_overflow_p = true;
4739 return boolean_false_node;
4742 /* Otherwise, we don't know. */
4743 return NULL_TREE;
4746 gcc_unreachable ();
4750 /* Debugging dumps. */
4752 void dump_value_range (FILE *, value_range_t *);
4753 void debug_value_range (value_range_t *);
4754 void dump_all_value_ranges (FILE *);
4755 void debug_all_value_ranges (void);
4756 void dump_vr_equiv (FILE *, bitmap);
4757 void debug_vr_equiv (bitmap);
4760 /* Dump value range VR to FILE. */
4762 void
4763 dump_value_range (FILE *file, value_range_t *vr)
4765 if (vr == NULL)
4766 fprintf (file, "[]");
4767 else if (vr->type == VR_UNDEFINED)
4768 fprintf (file, "UNDEFINED");
4769 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4771 tree type = TREE_TYPE (vr->min);
4773 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4775 if (is_negative_overflow_infinity (vr->min))
4776 fprintf (file, "-INF(OVF)");
4777 else if (INTEGRAL_TYPE_P (type)
4778 && !TYPE_UNSIGNED (type)
4779 && vrp_val_is_min (vr->min))
4780 fprintf (file, "-INF");
4781 else
4782 print_generic_expr (file, vr->min, 0);
4784 fprintf (file, ", ");
4786 if (is_positive_overflow_infinity (vr->max))
4787 fprintf (file, "+INF(OVF)");
4788 else if (INTEGRAL_TYPE_P (type)
4789 && vrp_val_is_max (vr->max))
4790 fprintf (file, "+INF");
4791 else
4792 print_generic_expr (file, vr->max, 0);
4794 fprintf (file, "]");
4796 if (vr->equiv)
4798 bitmap_iterator bi;
4799 unsigned i, c = 0;
4801 fprintf (file, " EQUIVALENCES: { ");
4803 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4805 print_generic_expr (file, ssa_name (i), 0);
4806 fprintf (file, " ");
4807 c++;
4810 fprintf (file, "} (%u elements)", c);
4813 else if (vr->type == VR_VARYING)
4814 fprintf (file, "VARYING");
4815 else
4816 fprintf (file, "INVALID RANGE");
4820 /* Dump value range VR to stderr. */
4822 DEBUG_FUNCTION void
4823 debug_value_range (value_range_t *vr)
4825 dump_value_range (stderr, vr);
4826 fprintf (stderr, "\n");
4830 /* Dump value ranges of all SSA_NAMEs to FILE. */
4832 void
4833 dump_all_value_ranges (FILE *file)
4835 size_t i;
4837 for (i = 0; i < num_vr_values; i++)
4839 if (vr_value[i])
4841 print_generic_expr (file, ssa_name (i), 0);
4842 fprintf (file, ": ");
4843 dump_value_range (file, vr_value[i]);
4844 fprintf (file, "\n");
4848 fprintf (file, "\n");
4852 /* Dump all value ranges to stderr. */
4854 DEBUG_FUNCTION void
4855 debug_all_value_ranges (void)
4857 dump_all_value_ranges (stderr);
4861 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4862 create a new SSA name N and return the assertion assignment
4863 'N = ASSERT_EXPR <V, V OP W>'. */
4865 static gimple
4866 build_assert_expr_for (tree cond, tree v)
4868 tree a;
4869 gassign *assertion;
4871 gcc_assert (TREE_CODE (v) == SSA_NAME
4872 && COMPARISON_CLASS_P (cond));
4874 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4875 assertion = gimple_build_assign (NULL_TREE, a);
4877 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4878 operand of the ASSERT_EXPR. Create it so the new name and the old one
4879 are registered in the replacement table so that we can fix the SSA web
4880 after adding all the ASSERT_EXPRs. */
4881 create_new_def_for (v, assertion, NULL);
4883 return assertion;
4887 /* Return false if EXPR is a predicate expression involving floating
4888 point values. */
4890 static inline bool
4891 fp_predicate (gimple stmt)
4893 GIMPLE_CHECK (stmt, GIMPLE_COND);
4895 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4898 /* If the range of values taken by OP can be inferred after STMT executes,
4899 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4900 describes the inferred range. Return true if a range could be
4901 inferred. */
4903 static bool
4904 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4906 *val_p = NULL_TREE;
4907 *comp_code_p = ERROR_MARK;
4909 /* Do not attempt to infer anything in names that flow through
4910 abnormal edges. */
4911 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4912 return false;
4914 /* Similarly, don't infer anything from statements that may throw
4915 exceptions. ??? Relax this requirement? */
4916 if (stmt_could_throw_p (stmt))
4917 return false;
4919 /* If STMT is the last statement of a basic block with no normal
4920 successors, there is no point inferring anything about any of its
4921 operands. We would not be able to find a proper insertion point
4922 for the assertion, anyway. */
4923 if (stmt_ends_bb_p (stmt))
4925 edge_iterator ei;
4926 edge e;
4928 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4929 if (!(e->flags & EDGE_ABNORMAL))
4930 break;
4931 if (e == NULL)
4932 return false;
4935 if (infer_nonnull_range (stmt, op, true, true))
4937 *val_p = build_int_cst (TREE_TYPE (op), 0);
4938 *comp_code_p = NE_EXPR;
4939 return true;
4942 return false;
4946 void dump_asserts_for (FILE *, tree);
4947 void debug_asserts_for (tree);
4948 void dump_all_asserts (FILE *);
4949 void debug_all_asserts (void);
4951 /* Dump all the registered assertions for NAME to FILE. */
4953 void
4954 dump_asserts_for (FILE *file, tree name)
4956 assert_locus_t loc;
4958 fprintf (file, "Assertions to be inserted for ");
4959 print_generic_expr (file, name, 0);
4960 fprintf (file, "\n");
4962 loc = asserts_for[SSA_NAME_VERSION (name)];
4963 while (loc)
4965 fprintf (file, "\t");
4966 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4967 fprintf (file, "\n\tBB #%d", loc->bb->index);
4968 if (loc->e)
4970 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4971 loc->e->dest->index);
4972 dump_edge_info (file, loc->e, dump_flags, 0);
4974 fprintf (file, "\n\tPREDICATE: ");
4975 print_generic_expr (file, name, 0);
4976 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4977 print_generic_expr (file, loc->val, 0);
4978 fprintf (file, "\n\n");
4979 loc = loc->next;
4982 fprintf (file, "\n");
4986 /* Dump all the registered assertions for NAME to stderr. */
4988 DEBUG_FUNCTION void
4989 debug_asserts_for (tree name)
4991 dump_asserts_for (stderr, name);
4995 /* Dump all the registered assertions for all the names to FILE. */
4997 void
4998 dump_all_asserts (FILE *file)
5000 unsigned i;
5001 bitmap_iterator bi;
5003 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
5004 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
5005 dump_asserts_for (file, ssa_name (i));
5006 fprintf (file, "\n");
5010 /* Dump all the registered assertions for all the names to stderr. */
5012 DEBUG_FUNCTION void
5013 debug_all_asserts (void)
5015 dump_all_asserts (stderr);
5019 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
5020 'EXPR COMP_CODE VAL' at a location that dominates block BB or
5021 E->DEST, then register this location as a possible insertion point
5022 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
5024 BB, E and SI provide the exact insertion point for the new
5025 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
5026 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
5027 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
5028 must not be NULL. */
5030 static void
5031 register_new_assert_for (tree name, tree expr,
5032 enum tree_code comp_code,
5033 tree val,
5034 basic_block bb,
5035 edge e,
5036 gimple_stmt_iterator si)
5038 assert_locus_t n, loc, last_loc;
5039 basic_block dest_bb;
5041 gcc_checking_assert (bb == NULL || e == NULL);
5043 if (e == NULL)
5044 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
5045 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
5047 /* Never build an assert comparing against an integer constant with
5048 TREE_OVERFLOW set. This confuses our undefined overflow warning
5049 machinery. */
5050 if (TREE_OVERFLOW_P (val))
5051 val = drop_tree_overflow (val);
5053 /* The new assertion A will be inserted at BB or E. We need to
5054 determine if the new location is dominated by a previously
5055 registered location for A. If we are doing an edge insertion,
5056 assume that A will be inserted at E->DEST. Note that this is not
5057 necessarily true.
5059 If E is a critical edge, it will be split. But even if E is
5060 split, the new block will dominate the same set of blocks that
5061 E->DEST dominates.
5063 The reverse, however, is not true, blocks dominated by E->DEST
5064 will not be dominated by the new block created to split E. So,
5065 if the insertion location is on a critical edge, we will not use
5066 the new location to move another assertion previously registered
5067 at a block dominated by E->DEST. */
5068 dest_bb = (bb) ? bb : e->dest;
5070 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5071 VAL at a block dominating DEST_BB, then we don't need to insert a new
5072 one. Similarly, if the same assertion already exists at a block
5073 dominated by DEST_BB and the new location is not on a critical
5074 edge, then update the existing location for the assertion (i.e.,
5075 move the assertion up in the dominance tree).
5077 Note, this is implemented as a simple linked list because there
5078 should not be more than a handful of assertions registered per
5079 name. If this becomes a performance problem, a table hashed by
5080 COMP_CODE and VAL could be implemented. */
5081 loc = asserts_for[SSA_NAME_VERSION (name)];
5082 last_loc = loc;
5083 while (loc)
5085 if (loc->comp_code == comp_code
5086 && (loc->val == val
5087 || operand_equal_p (loc->val, val, 0))
5088 && (loc->expr == expr
5089 || operand_equal_p (loc->expr, expr, 0)))
5091 /* If E is not a critical edge and DEST_BB
5092 dominates the existing location for the assertion, move
5093 the assertion up in the dominance tree by updating its
5094 location information. */
5095 if ((e == NULL || !EDGE_CRITICAL_P (e))
5096 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5098 loc->bb = dest_bb;
5099 loc->e = e;
5100 loc->si = si;
5101 return;
5105 /* Update the last node of the list and move to the next one. */
5106 last_loc = loc;
5107 loc = loc->next;
5110 /* If we didn't find an assertion already registered for
5111 NAME COMP_CODE VAL, add a new one at the end of the list of
5112 assertions associated with NAME. */
5113 n = XNEW (struct assert_locus_d);
5114 n->bb = dest_bb;
5115 n->e = e;
5116 n->si = si;
5117 n->comp_code = comp_code;
5118 n->val = val;
5119 n->expr = expr;
5120 n->next = NULL;
5122 if (last_loc)
5123 last_loc->next = n;
5124 else
5125 asserts_for[SSA_NAME_VERSION (name)] = n;
5127 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5130 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5131 Extract a suitable test code and value and store them into *CODE_P and
5132 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5134 If no extraction was possible, return FALSE, otherwise return TRUE.
5136 If INVERT is true, then we invert the result stored into *CODE_P. */
5138 static bool
5139 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5140 tree cond_op0, tree cond_op1,
5141 bool invert, enum tree_code *code_p,
5142 tree *val_p)
5144 enum tree_code comp_code;
5145 tree val;
5147 /* Otherwise, we have a comparison of the form NAME COMP VAL
5148 or VAL COMP NAME. */
5149 if (name == cond_op1)
5151 /* If the predicate is of the form VAL COMP NAME, flip
5152 COMP around because we need to register NAME as the
5153 first operand in the predicate. */
5154 comp_code = swap_tree_comparison (cond_code);
5155 val = cond_op0;
5157 else
5159 /* The comparison is of the form NAME COMP VAL, so the
5160 comparison code remains unchanged. */
5161 comp_code = cond_code;
5162 val = cond_op1;
5165 /* Invert the comparison code as necessary. */
5166 if (invert)
5167 comp_code = invert_tree_comparison (comp_code, 0);
5169 /* VRP does not handle float types. */
5170 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
5171 return false;
5173 /* Do not register always-false predicates.
5174 FIXME: this works around a limitation in fold() when dealing with
5175 enumerations. Given 'enum { N1, N2 } x;', fold will not
5176 fold 'if (x > N2)' to 'if (0)'. */
5177 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5178 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5180 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5181 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5183 if (comp_code == GT_EXPR
5184 && (!max
5185 || compare_values (val, max) == 0))
5186 return false;
5188 if (comp_code == LT_EXPR
5189 && (!min
5190 || compare_values (val, min) == 0))
5191 return false;
5193 *code_p = comp_code;
5194 *val_p = val;
5195 return true;
5198 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5199 (otherwise return VAL). VAL and MASK must be zero-extended for
5200 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5201 (to transform signed values into unsigned) and at the end xor
5202 SGNBIT back. */
5204 static wide_int
5205 masked_increment (const wide_int &val_in, const wide_int &mask,
5206 const wide_int &sgnbit, unsigned int prec)
5208 wide_int bit = wi::one (prec), res;
5209 unsigned int i;
5211 wide_int val = val_in ^ sgnbit;
5212 for (i = 0; i < prec; i++, bit += bit)
5214 res = mask;
5215 if ((res & bit) == 0)
5216 continue;
5217 res = bit - 1;
5218 res = (val + bit).and_not (res);
5219 res &= mask;
5220 if (wi::gtu_p (res, val))
5221 return res ^ sgnbit;
5223 return val ^ sgnbit;
5226 /* Try to register an edge assertion for SSA name NAME on edge E for
5227 the condition COND contributing to the conditional jump pointed to by BSI.
5228 Invert the condition COND if INVERT is true. */
5230 static void
5231 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5232 enum tree_code cond_code,
5233 tree cond_op0, tree cond_op1, bool invert)
5235 tree val;
5236 enum tree_code comp_code;
5238 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5239 cond_op0,
5240 cond_op1,
5241 invert, &comp_code, &val))
5242 return;
5244 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5245 reachable from E. */
5246 if (live_on_edge (e, name)
5247 && !has_single_use (name))
5248 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5250 /* In the case of NAME <= CST and NAME being defined as
5251 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5252 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5253 This catches range and anti-range tests. */
5254 if ((comp_code == LE_EXPR
5255 || comp_code == GT_EXPR)
5256 && TREE_CODE (val) == INTEGER_CST
5257 && TYPE_UNSIGNED (TREE_TYPE (val)))
5259 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5260 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5262 /* Extract CST2 from the (optional) addition. */
5263 if (is_gimple_assign (def_stmt)
5264 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5266 name2 = gimple_assign_rhs1 (def_stmt);
5267 cst2 = gimple_assign_rhs2 (def_stmt);
5268 if (TREE_CODE (name2) == SSA_NAME
5269 && TREE_CODE (cst2) == INTEGER_CST)
5270 def_stmt = SSA_NAME_DEF_STMT (name2);
5273 /* Extract NAME2 from the (optional) sign-changing cast. */
5274 if (gimple_assign_cast_p (def_stmt))
5276 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5277 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5278 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5279 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5280 name3 = gimple_assign_rhs1 (def_stmt);
5283 /* If name3 is used later, create an ASSERT_EXPR for it. */
5284 if (name3 != NULL_TREE
5285 && TREE_CODE (name3) == SSA_NAME
5286 && (cst2 == NULL_TREE
5287 || TREE_CODE (cst2) == INTEGER_CST)
5288 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5289 && live_on_edge (e, name3)
5290 && !has_single_use (name3))
5292 tree tmp;
5294 /* Build an expression for the range test. */
5295 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5296 if (cst2 != NULL_TREE)
5297 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5299 if (dump_file)
5301 fprintf (dump_file, "Adding assert for ");
5302 print_generic_expr (dump_file, name3, 0);
5303 fprintf (dump_file, " from ");
5304 print_generic_expr (dump_file, tmp, 0);
5305 fprintf (dump_file, "\n");
5308 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5311 /* If name2 is used later, create an ASSERT_EXPR for it. */
5312 if (name2 != NULL_TREE
5313 && TREE_CODE (name2) == SSA_NAME
5314 && TREE_CODE (cst2) == INTEGER_CST
5315 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5316 && live_on_edge (e, name2)
5317 && !has_single_use (name2))
5319 tree tmp;
5321 /* Build an expression for the range test. */
5322 tmp = name2;
5323 if (TREE_TYPE (name) != TREE_TYPE (name2))
5324 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5325 if (cst2 != NULL_TREE)
5326 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5328 if (dump_file)
5330 fprintf (dump_file, "Adding assert for ");
5331 print_generic_expr (dump_file, name2, 0);
5332 fprintf (dump_file, " from ");
5333 print_generic_expr (dump_file, tmp, 0);
5334 fprintf (dump_file, "\n");
5337 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5341 /* In the case of post-in/decrement tests like if (i++) ... and uses
5342 of the in/decremented value on the edge the extra name we want to
5343 assert for is not on the def chain of the name compared. Instead
5344 it is in the set of use stmts. */
5345 if ((comp_code == NE_EXPR
5346 || comp_code == EQ_EXPR)
5347 && TREE_CODE (val) == INTEGER_CST)
5349 imm_use_iterator ui;
5350 gimple use_stmt;
5351 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5353 /* Cut off to use-stmts that are in the predecessor. */
5354 if (gimple_bb (use_stmt) != e->src)
5355 continue;
5357 if (!is_gimple_assign (use_stmt))
5358 continue;
5360 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5361 if (code != PLUS_EXPR
5362 && code != MINUS_EXPR)
5363 continue;
5365 tree cst = gimple_assign_rhs2 (use_stmt);
5366 if (TREE_CODE (cst) != INTEGER_CST)
5367 continue;
5369 tree name2 = gimple_assign_lhs (use_stmt);
5370 if (live_on_edge (e, name2))
5372 cst = int_const_binop (code, val, cst);
5373 register_new_assert_for (name2, name2, comp_code, cst,
5374 NULL, e, bsi);
5379 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5380 && TREE_CODE (val) == INTEGER_CST)
5382 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5383 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5384 tree val2 = NULL_TREE;
5385 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5386 wide_int mask = wi::zero (prec);
5387 unsigned int nprec = prec;
5388 enum tree_code rhs_code = ERROR_MARK;
5390 if (is_gimple_assign (def_stmt))
5391 rhs_code = gimple_assign_rhs_code (def_stmt);
5393 /* Add asserts for NAME cmp CST and NAME being defined
5394 as NAME = (int) NAME2. */
5395 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5396 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5397 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5398 && gimple_assign_cast_p (def_stmt))
5400 name2 = gimple_assign_rhs1 (def_stmt);
5401 if (CONVERT_EXPR_CODE_P (rhs_code)
5402 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5403 && TYPE_UNSIGNED (TREE_TYPE (name2))
5404 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5405 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5406 || !tree_int_cst_equal (val,
5407 TYPE_MIN_VALUE (TREE_TYPE (val))))
5408 && live_on_edge (e, name2)
5409 && !has_single_use (name2))
5411 tree tmp, cst;
5412 enum tree_code new_comp_code = comp_code;
5414 cst = fold_convert (TREE_TYPE (name2),
5415 TYPE_MIN_VALUE (TREE_TYPE (val)));
5416 /* Build an expression for the range test. */
5417 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5418 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5419 fold_convert (TREE_TYPE (name2), val));
5420 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5422 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5423 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5424 build_int_cst (TREE_TYPE (name2), 1));
5427 if (dump_file)
5429 fprintf (dump_file, "Adding assert for ");
5430 print_generic_expr (dump_file, name2, 0);
5431 fprintf (dump_file, " from ");
5432 print_generic_expr (dump_file, tmp, 0);
5433 fprintf (dump_file, "\n");
5436 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5437 e, bsi);
5441 /* Add asserts for NAME cmp CST and NAME being defined as
5442 NAME = NAME2 >> CST2.
5444 Extract CST2 from the right shift. */
5445 if (rhs_code == RSHIFT_EXPR)
5447 name2 = gimple_assign_rhs1 (def_stmt);
5448 cst2 = gimple_assign_rhs2 (def_stmt);
5449 if (TREE_CODE (name2) == SSA_NAME
5450 && tree_fits_uhwi_p (cst2)
5451 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5452 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5453 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5454 && live_on_edge (e, name2)
5455 && !has_single_use (name2))
5457 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5458 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5461 if (val2 != NULL_TREE
5462 && TREE_CODE (val2) == INTEGER_CST
5463 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5464 TREE_TYPE (val),
5465 val2, cst2), val))
5467 enum tree_code new_comp_code = comp_code;
5468 tree tmp, new_val;
5470 tmp = name2;
5471 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5473 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5475 tree type = build_nonstandard_integer_type (prec, 1);
5476 tmp = build1 (NOP_EXPR, type, name2);
5477 val2 = fold_convert (type, val2);
5479 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5480 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5481 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5483 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5485 wide_int minval
5486 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5487 new_val = val2;
5488 if (minval == new_val)
5489 new_val = NULL_TREE;
5491 else
5493 wide_int maxval
5494 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5495 mask |= val2;
5496 if (mask == maxval)
5497 new_val = NULL_TREE;
5498 else
5499 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5502 if (new_val)
5504 if (dump_file)
5506 fprintf (dump_file, "Adding assert for ");
5507 print_generic_expr (dump_file, name2, 0);
5508 fprintf (dump_file, " from ");
5509 print_generic_expr (dump_file, tmp, 0);
5510 fprintf (dump_file, "\n");
5513 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5514 NULL, e, bsi);
5518 /* Add asserts for NAME cmp CST and NAME being defined as
5519 NAME = NAME2 & CST2.
5521 Extract CST2 from the and.
5523 Also handle
5524 NAME = (unsigned) NAME2;
5525 casts where NAME's type is unsigned and has smaller precision
5526 than NAME2's type as if it was NAME = NAME2 & MASK. */
5527 names[0] = NULL_TREE;
5528 names[1] = NULL_TREE;
5529 cst2 = NULL_TREE;
5530 if (rhs_code == BIT_AND_EXPR
5531 || (CONVERT_EXPR_CODE_P (rhs_code)
5532 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5533 && TYPE_UNSIGNED (TREE_TYPE (val))
5534 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5535 > prec))
5537 name2 = gimple_assign_rhs1 (def_stmt);
5538 if (rhs_code == BIT_AND_EXPR)
5539 cst2 = gimple_assign_rhs2 (def_stmt);
5540 else
5542 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5543 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5545 if (TREE_CODE (name2) == SSA_NAME
5546 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5547 && TREE_CODE (cst2) == INTEGER_CST
5548 && !integer_zerop (cst2)
5549 && (nprec > 1
5550 || TYPE_UNSIGNED (TREE_TYPE (val))))
5552 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5553 if (gimple_assign_cast_p (def_stmt2))
5555 names[1] = gimple_assign_rhs1 (def_stmt2);
5556 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5557 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5558 || (TYPE_PRECISION (TREE_TYPE (name2))
5559 != TYPE_PRECISION (TREE_TYPE (names[1])))
5560 || !live_on_edge (e, names[1])
5561 || has_single_use (names[1]))
5562 names[1] = NULL_TREE;
5564 if (live_on_edge (e, name2)
5565 && !has_single_use (name2))
5566 names[0] = name2;
5569 if (names[0] || names[1])
5571 wide_int minv, maxv, valv, cst2v;
5572 wide_int tem, sgnbit;
5573 bool valid_p = false, valn, cst2n;
5574 enum tree_code ccode = comp_code;
5576 valv = wide_int::from (val, nprec, UNSIGNED);
5577 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5578 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5579 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5580 /* If CST2 doesn't have most significant bit set,
5581 but VAL is negative, we have comparison like
5582 if ((x & 0x123) > -4) (always true). Just give up. */
5583 if (!cst2n && valn)
5584 ccode = ERROR_MARK;
5585 if (cst2n)
5586 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5587 else
5588 sgnbit = wi::zero (nprec);
5589 minv = valv & cst2v;
5590 switch (ccode)
5592 case EQ_EXPR:
5593 /* Minimum unsigned value for equality is VAL & CST2
5594 (should be equal to VAL, otherwise we probably should
5595 have folded the comparison into false) and
5596 maximum unsigned value is VAL | ~CST2. */
5597 maxv = valv | ~cst2v;
5598 valid_p = true;
5599 break;
5601 case NE_EXPR:
5602 tem = valv | ~cst2v;
5603 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5604 if (valv == 0)
5606 cst2n = false;
5607 sgnbit = wi::zero (nprec);
5608 goto gt_expr;
5610 /* If (VAL | ~CST2) is all ones, handle it as
5611 (X & CST2) < VAL. */
5612 if (tem == -1)
5614 cst2n = false;
5615 valn = false;
5616 sgnbit = wi::zero (nprec);
5617 goto lt_expr;
5619 if (!cst2n && wi::neg_p (cst2v))
5620 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5621 if (sgnbit != 0)
5623 if (valv == sgnbit)
5625 cst2n = true;
5626 valn = true;
5627 goto gt_expr;
5629 if (tem == wi::mask (nprec - 1, false, nprec))
5631 cst2n = true;
5632 goto lt_expr;
5634 if (!cst2n)
5635 sgnbit = wi::zero (nprec);
5637 break;
5639 case GE_EXPR:
5640 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5641 is VAL and maximum unsigned value is ~0. For signed
5642 comparison, if CST2 doesn't have most significant bit
5643 set, handle it similarly. If CST2 has MSB set,
5644 the minimum is the same, and maximum is ~0U/2. */
5645 if (minv != valv)
5647 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5648 VAL. */
5649 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5650 if (minv == valv)
5651 break;
5653 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5654 valid_p = true;
5655 break;
5657 case GT_EXPR:
5658 gt_expr:
5659 /* Find out smallest MINV where MINV > VAL
5660 && (MINV & CST2) == MINV, if any. If VAL is signed and
5661 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5662 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5663 if (minv == valv)
5664 break;
5665 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5666 valid_p = true;
5667 break;
5669 case LE_EXPR:
5670 /* Minimum unsigned value for <= is 0 and maximum
5671 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5672 Otherwise, find smallest VAL2 where VAL2 > VAL
5673 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5674 as maximum.
5675 For signed comparison, if CST2 doesn't have most
5676 significant bit set, handle it similarly. If CST2 has
5677 MSB set, the maximum is the same and minimum is INT_MIN. */
5678 if (minv == valv)
5679 maxv = valv;
5680 else
5682 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5683 if (maxv == valv)
5684 break;
5685 maxv -= 1;
5687 maxv |= ~cst2v;
5688 minv = sgnbit;
5689 valid_p = true;
5690 break;
5692 case LT_EXPR:
5693 lt_expr:
5694 /* Minimum unsigned value for < is 0 and maximum
5695 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5696 Otherwise, find smallest VAL2 where VAL2 > VAL
5697 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5698 as maximum.
5699 For signed comparison, if CST2 doesn't have most
5700 significant bit set, handle it similarly. If CST2 has
5701 MSB set, the maximum is the same and minimum is INT_MIN. */
5702 if (minv == valv)
5704 if (valv == sgnbit)
5705 break;
5706 maxv = valv;
5708 else
5710 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5711 if (maxv == valv)
5712 break;
5714 maxv -= 1;
5715 maxv |= ~cst2v;
5716 minv = sgnbit;
5717 valid_p = true;
5718 break;
5720 default:
5721 break;
5723 if (valid_p
5724 && (maxv - minv) != -1)
5726 tree tmp, new_val, type;
5727 int i;
5729 for (i = 0; i < 2; i++)
5730 if (names[i])
5732 wide_int maxv2 = maxv;
5733 tmp = names[i];
5734 type = TREE_TYPE (names[i]);
5735 if (!TYPE_UNSIGNED (type))
5737 type = build_nonstandard_integer_type (nprec, 1);
5738 tmp = build1 (NOP_EXPR, type, names[i]);
5740 if (minv != 0)
5742 tmp = build2 (PLUS_EXPR, type, tmp,
5743 wide_int_to_tree (type, -minv));
5744 maxv2 = maxv - minv;
5746 new_val = wide_int_to_tree (type, maxv2);
5748 if (dump_file)
5750 fprintf (dump_file, "Adding assert for ");
5751 print_generic_expr (dump_file, names[i], 0);
5752 fprintf (dump_file, " from ");
5753 print_generic_expr (dump_file, tmp, 0);
5754 fprintf (dump_file, "\n");
5757 register_new_assert_for (names[i], tmp, LE_EXPR,
5758 new_val, NULL, e, bsi);
5765 /* OP is an operand of a truth value expression which is known to have
5766 a particular value. Register any asserts for OP and for any
5767 operands in OP's defining statement.
5769 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5770 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5772 static void
5773 register_edge_assert_for_1 (tree op, enum tree_code code,
5774 edge e, gimple_stmt_iterator bsi)
5776 gimple op_def;
5777 tree val;
5778 enum tree_code rhs_code;
5780 /* We only care about SSA_NAMEs. */
5781 if (TREE_CODE (op) != SSA_NAME)
5782 return;
5784 /* We know that OP will have a zero or nonzero value. If OP is used
5785 more than once go ahead and register an assert for OP. */
5786 if (live_on_edge (e, op)
5787 && !has_single_use (op))
5789 val = build_int_cst (TREE_TYPE (op), 0);
5790 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5793 /* Now look at how OP is set. If it's set from a comparison,
5794 a truth operation or some bit operations, then we may be able
5795 to register information about the operands of that assignment. */
5796 op_def = SSA_NAME_DEF_STMT (op);
5797 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5798 return;
5800 rhs_code = gimple_assign_rhs_code (op_def);
5802 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5804 bool invert = (code == EQ_EXPR ? true : false);
5805 tree op0 = gimple_assign_rhs1 (op_def);
5806 tree op1 = gimple_assign_rhs2 (op_def);
5808 if (TREE_CODE (op0) == SSA_NAME)
5809 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5810 if (TREE_CODE (op1) == SSA_NAME)
5811 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5813 else if ((code == NE_EXPR
5814 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5815 || (code == EQ_EXPR
5816 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5818 /* Recurse on each operand. */
5819 tree op0 = gimple_assign_rhs1 (op_def);
5820 tree op1 = gimple_assign_rhs2 (op_def);
5821 if (TREE_CODE (op0) == SSA_NAME
5822 && has_single_use (op0))
5823 register_edge_assert_for_1 (op0, code, e, bsi);
5824 if (TREE_CODE (op1) == SSA_NAME
5825 && has_single_use (op1))
5826 register_edge_assert_for_1 (op1, code, e, bsi);
5828 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5829 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5831 /* Recurse, flipping CODE. */
5832 code = invert_tree_comparison (code, false);
5833 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5835 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5837 /* Recurse through the copy. */
5838 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5840 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5842 /* Recurse through the type conversion, unless it is a narrowing
5843 conversion or conversion from non-integral type. */
5844 tree rhs = gimple_assign_rhs1 (op_def);
5845 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5846 && (TYPE_PRECISION (TREE_TYPE (rhs))
5847 <= TYPE_PRECISION (TREE_TYPE (op))))
5848 register_edge_assert_for_1 (rhs, code, e, bsi);
5852 /* Try to register an edge assertion for SSA name NAME on edge E for
5853 the condition COND contributing to the conditional jump pointed to by
5854 SI. */
5856 static void
5857 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5858 enum tree_code cond_code, tree cond_op0,
5859 tree cond_op1)
5861 tree val;
5862 enum tree_code comp_code;
5863 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5865 /* Do not attempt to infer anything in names that flow through
5866 abnormal edges. */
5867 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5868 return;
5870 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5871 cond_op0, cond_op1,
5872 is_else_edge,
5873 &comp_code, &val))
5874 return;
5876 /* Register ASSERT_EXPRs for name. */
5877 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5878 cond_op1, is_else_edge);
5881 /* If COND is effectively an equality test of an SSA_NAME against
5882 the value zero or one, then we may be able to assert values
5883 for SSA_NAMEs which flow into COND. */
5885 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5886 statement of NAME we can assert both operands of the BIT_AND_EXPR
5887 have nonzero value. */
5888 if (((comp_code == EQ_EXPR && integer_onep (val))
5889 || (comp_code == NE_EXPR && integer_zerop (val))))
5891 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5893 if (is_gimple_assign (def_stmt)
5894 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5896 tree op0 = gimple_assign_rhs1 (def_stmt);
5897 tree op1 = gimple_assign_rhs2 (def_stmt);
5898 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5899 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5903 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5904 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5905 have zero value. */
5906 if (((comp_code == EQ_EXPR && integer_zerop (val))
5907 || (comp_code == NE_EXPR && integer_onep (val))))
5909 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5911 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5912 necessarily zero value, or if type-precision is one. */
5913 if (is_gimple_assign (def_stmt)
5914 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5915 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5916 || comp_code == EQ_EXPR)))
5918 tree op0 = gimple_assign_rhs1 (def_stmt);
5919 tree op1 = gimple_assign_rhs2 (def_stmt);
5920 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5921 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5927 /* Determine whether the outgoing edges of BB should receive an
5928 ASSERT_EXPR for each of the operands of BB's LAST statement.
5929 The last statement of BB must be a COND_EXPR.
5931 If any of the sub-graphs rooted at BB have an interesting use of
5932 the predicate operands, an assert location node is added to the
5933 list of assertions for the corresponding operands. */
5935 static void
5936 find_conditional_asserts (basic_block bb, gcond *last)
5938 gimple_stmt_iterator bsi;
5939 tree op;
5940 edge_iterator ei;
5941 edge e;
5942 ssa_op_iter iter;
5944 bsi = gsi_for_stmt (last);
5946 /* Look for uses of the operands in each of the sub-graphs
5947 rooted at BB. We need to check each of the outgoing edges
5948 separately, so that we know what kind of ASSERT_EXPR to
5949 insert. */
5950 FOR_EACH_EDGE (e, ei, bb->succs)
5952 if (e->dest == bb)
5953 continue;
5955 /* Register the necessary assertions for each operand in the
5956 conditional predicate. */
5957 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5958 register_edge_assert_for (op, e, bsi,
5959 gimple_cond_code (last),
5960 gimple_cond_lhs (last),
5961 gimple_cond_rhs (last));
5965 struct case_info
5967 tree expr;
5968 basic_block bb;
5971 /* Compare two case labels sorting first by the destination bb index
5972 and then by the case value. */
5974 static int
5975 compare_case_labels (const void *p1, const void *p2)
5977 const struct case_info *ci1 = (const struct case_info *) p1;
5978 const struct case_info *ci2 = (const struct case_info *) p2;
5979 int idx1 = ci1->bb->index;
5980 int idx2 = ci2->bb->index;
5982 if (idx1 < idx2)
5983 return -1;
5984 else if (idx1 == idx2)
5986 /* Make sure the default label is first in a group. */
5987 if (!CASE_LOW (ci1->expr))
5988 return -1;
5989 else if (!CASE_LOW (ci2->expr))
5990 return 1;
5991 else
5992 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5993 CASE_LOW (ci2->expr));
5995 else
5996 return 1;
5999 /* Determine whether the outgoing edges of BB should receive an
6000 ASSERT_EXPR for each of the operands of BB's LAST statement.
6001 The last statement of BB must be a SWITCH_EXPR.
6003 If any of the sub-graphs rooted at BB have an interesting use of
6004 the predicate operands, an assert location node is added to the
6005 list of assertions for the corresponding operands. */
6007 static void
6008 find_switch_asserts (basic_block bb, gswitch *last)
6010 gimple_stmt_iterator bsi;
6011 tree op;
6012 edge e;
6013 struct case_info *ci;
6014 size_t n = gimple_switch_num_labels (last);
6015 #if GCC_VERSION >= 4000
6016 unsigned int idx;
6017 #else
6018 /* Work around GCC 3.4 bug (PR 37086). */
6019 volatile unsigned int idx;
6020 #endif
6022 bsi = gsi_for_stmt (last);
6023 op = gimple_switch_index (last);
6024 if (TREE_CODE (op) != SSA_NAME)
6025 return;
6027 /* Build a vector of case labels sorted by destination label. */
6028 ci = XNEWVEC (struct case_info, n);
6029 for (idx = 0; idx < n; ++idx)
6031 ci[idx].expr = gimple_switch_label (last, idx);
6032 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
6034 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
6036 for (idx = 0; idx < n; ++idx)
6038 tree min, max;
6039 tree cl = ci[idx].expr;
6040 basic_block cbb = ci[idx].bb;
6042 min = CASE_LOW (cl);
6043 max = CASE_HIGH (cl);
6045 /* If there are multiple case labels with the same destination
6046 we need to combine them to a single value range for the edge. */
6047 if (idx + 1 < n && cbb == ci[idx + 1].bb)
6049 /* Skip labels until the last of the group. */
6050 do {
6051 ++idx;
6052 } while (idx < n && cbb == ci[idx].bb);
6053 --idx;
6055 /* Pick up the maximum of the case label range. */
6056 if (CASE_HIGH (ci[idx].expr))
6057 max = CASE_HIGH (ci[idx].expr);
6058 else
6059 max = CASE_LOW (ci[idx].expr);
6062 /* Nothing to do if the range includes the default label until we
6063 can register anti-ranges. */
6064 if (min == NULL_TREE)
6065 continue;
6067 /* Find the edge to register the assert expr on. */
6068 e = find_edge (bb, cbb);
6070 /* Register the necessary assertions for the operand in the
6071 SWITCH_EXPR. */
6072 register_edge_assert_for (op, e, bsi,
6073 max ? GE_EXPR : EQ_EXPR,
6074 op, fold_convert (TREE_TYPE (op), min));
6075 if (max)
6076 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6077 fold_convert (TREE_TYPE (op), max));
6080 XDELETEVEC (ci);
6084 /* Traverse all the statements in block BB looking for statements that
6085 may generate useful assertions for the SSA names in their operand.
6086 If a statement produces a useful assertion A for name N_i, then the
6087 list of assertions already generated for N_i is scanned to
6088 determine if A is actually needed.
6090 If N_i already had the assertion A at a location dominating the
6091 current location, then nothing needs to be done. Otherwise, the
6092 new location for A is recorded instead.
6094 1- For every statement S in BB, all the variables used by S are
6095 added to bitmap FOUND_IN_SUBGRAPH.
6097 2- If statement S uses an operand N in a way that exposes a known
6098 value range for N, then if N was not already generated by an
6099 ASSERT_EXPR, create a new assert location for N. For instance,
6100 if N is a pointer and the statement dereferences it, we can
6101 assume that N is not NULL.
6103 3- COND_EXPRs are a special case of #2. We can derive range
6104 information from the predicate but need to insert different
6105 ASSERT_EXPRs for each of the sub-graphs rooted at the
6106 conditional block. If the last statement of BB is a conditional
6107 expression of the form 'X op Y', then
6109 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6111 b) If the conditional is the only entry point to the sub-graph
6112 corresponding to the THEN_CLAUSE, recurse into it. On
6113 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6114 an ASSERT_EXPR is added for the corresponding variable.
6116 c) Repeat step (b) on the ELSE_CLAUSE.
6118 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6120 For instance,
6122 if (a == 9)
6123 b = a;
6124 else
6125 b = c + 1;
6127 In this case, an assertion on the THEN clause is useful to
6128 determine that 'a' is always 9 on that edge. However, an assertion
6129 on the ELSE clause would be unnecessary.
6131 4- If BB does not end in a conditional expression, then we recurse
6132 into BB's dominator children.
6134 At the end of the recursive traversal, every SSA name will have a
6135 list of locations where ASSERT_EXPRs should be added. When a new
6136 location for name N is found, it is registered by calling
6137 register_new_assert_for. That function keeps track of all the
6138 registered assertions to prevent adding unnecessary assertions.
6139 For instance, if a pointer P_4 is dereferenced more than once in a
6140 dominator tree, only the location dominating all the dereference of
6141 P_4 will receive an ASSERT_EXPR. */
6143 static void
6144 find_assert_locations_1 (basic_block bb, sbitmap live)
6146 gimple last;
6148 last = last_stmt (bb);
6150 /* If BB's last statement is a conditional statement involving integer
6151 operands, determine if we need to add ASSERT_EXPRs. */
6152 if (last
6153 && gimple_code (last) == GIMPLE_COND
6154 && !fp_predicate (last)
6155 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6156 find_conditional_asserts (bb, as_a <gcond *> (last));
6158 /* If BB's last statement is a switch statement involving integer
6159 operands, determine if we need to add ASSERT_EXPRs. */
6160 if (last
6161 && gimple_code (last) == GIMPLE_SWITCH
6162 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6163 find_switch_asserts (bb, as_a <gswitch *> (last));
6165 /* Traverse all the statements in BB marking used names and looking
6166 for statements that may infer assertions for their used operands. */
6167 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6168 gsi_prev (&si))
6170 gimple stmt;
6171 tree op;
6172 ssa_op_iter i;
6174 stmt = gsi_stmt (si);
6176 if (is_gimple_debug (stmt))
6177 continue;
6179 /* See if we can derive an assertion for any of STMT's operands. */
6180 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6182 tree value;
6183 enum tree_code comp_code;
6185 /* If op is not live beyond this stmt, do not bother to insert
6186 asserts for it. */
6187 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6188 continue;
6190 /* If OP is used in such a way that we can infer a value
6191 range for it, and we don't find a previous assertion for
6192 it, create a new assertion location node for OP. */
6193 if (infer_value_range (stmt, op, &comp_code, &value))
6195 /* If we are able to infer a nonzero value range for OP,
6196 then walk backwards through the use-def chain to see if OP
6197 was set via a typecast.
6199 If so, then we can also infer a nonzero value range
6200 for the operand of the NOP_EXPR. */
6201 if (comp_code == NE_EXPR && integer_zerop (value))
6203 tree t = op;
6204 gimple def_stmt = SSA_NAME_DEF_STMT (t);
6206 while (is_gimple_assign (def_stmt)
6207 && CONVERT_EXPR_CODE_P
6208 (gimple_assign_rhs_code (def_stmt))
6209 && TREE_CODE
6210 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6211 && POINTER_TYPE_P
6212 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6214 t = gimple_assign_rhs1 (def_stmt);
6215 def_stmt = SSA_NAME_DEF_STMT (t);
6217 /* Note we want to register the assert for the
6218 operand of the NOP_EXPR after SI, not after the
6219 conversion. */
6220 if (! has_single_use (t))
6221 register_new_assert_for (t, t, comp_code, value,
6222 bb, NULL, si);
6226 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6230 /* Update live. */
6231 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6232 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6233 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6234 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6237 /* Traverse all PHI nodes in BB, updating live. */
6238 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6239 gsi_next (&si))
6241 use_operand_p arg_p;
6242 ssa_op_iter i;
6243 gphi *phi = si.phi ();
6244 tree res = gimple_phi_result (phi);
6246 if (virtual_operand_p (res))
6247 continue;
6249 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6251 tree arg = USE_FROM_PTR (arg_p);
6252 if (TREE_CODE (arg) == SSA_NAME)
6253 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6256 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6260 /* Do an RPO walk over the function computing SSA name liveness
6261 on-the-fly and deciding on assert expressions to insert. */
6263 static void
6264 find_assert_locations (void)
6266 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6267 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6268 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6269 int rpo_cnt, i;
6271 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6272 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6273 for (i = 0; i < rpo_cnt; ++i)
6274 bb_rpo[rpo[i]] = i;
6276 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6277 the order we compute liveness and insert asserts we otherwise
6278 fail to insert asserts into the loop latch. */
6279 loop_p loop;
6280 FOR_EACH_LOOP (loop, 0)
6282 i = loop->latch->index;
6283 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6284 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6285 !gsi_end_p (gsi); gsi_next (&gsi))
6287 gphi *phi = gsi.phi ();
6288 if (virtual_operand_p (gimple_phi_result (phi)))
6289 continue;
6290 tree arg = gimple_phi_arg_def (phi, j);
6291 if (TREE_CODE (arg) == SSA_NAME)
6293 if (live[i] == NULL)
6295 live[i] = sbitmap_alloc (num_ssa_names);
6296 bitmap_clear (live[i]);
6298 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6303 for (i = rpo_cnt - 1; i >= 0; --i)
6305 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6306 edge e;
6307 edge_iterator ei;
6309 if (!live[rpo[i]])
6311 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6312 bitmap_clear (live[rpo[i]]);
6315 /* Process BB and update the live information with uses in
6316 this block. */
6317 find_assert_locations_1 (bb, live[rpo[i]]);
6319 /* Merge liveness into the predecessor blocks and free it. */
6320 if (!bitmap_empty_p (live[rpo[i]]))
6322 int pred_rpo = i;
6323 FOR_EACH_EDGE (e, ei, bb->preds)
6325 int pred = e->src->index;
6326 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6327 continue;
6329 if (!live[pred])
6331 live[pred] = sbitmap_alloc (num_ssa_names);
6332 bitmap_clear (live[pred]);
6334 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6336 if (bb_rpo[pred] < pred_rpo)
6337 pred_rpo = bb_rpo[pred];
6340 /* Record the RPO number of the last visited block that needs
6341 live information from this block. */
6342 last_rpo[rpo[i]] = pred_rpo;
6344 else
6346 sbitmap_free (live[rpo[i]]);
6347 live[rpo[i]] = NULL;
6350 /* We can free all successors live bitmaps if all their
6351 predecessors have been visited already. */
6352 FOR_EACH_EDGE (e, ei, bb->succs)
6353 if (last_rpo[e->dest->index] == i
6354 && live[e->dest->index])
6356 sbitmap_free (live[e->dest->index]);
6357 live[e->dest->index] = NULL;
6361 XDELETEVEC (rpo);
6362 XDELETEVEC (bb_rpo);
6363 XDELETEVEC (last_rpo);
6364 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6365 if (live[i])
6366 sbitmap_free (live[i]);
6367 XDELETEVEC (live);
6370 /* Create an ASSERT_EXPR for NAME and insert it in the location
6371 indicated by LOC. Return true if we made any edge insertions. */
6373 static bool
6374 process_assert_insertions_for (tree name, assert_locus_t loc)
6376 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6377 gimple stmt;
6378 tree cond;
6379 gimple assert_stmt;
6380 edge_iterator ei;
6381 edge e;
6383 /* If we have X <=> X do not insert an assert expr for that. */
6384 if (loc->expr == loc->val)
6385 return false;
6387 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6388 assert_stmt = build_assert_expr_for (cond, name);
6389 if (loc->e)
6391 /* We have been asked to insert the assertion on an edge. This
6392 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6393 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6394 || (gimple_code (gsi_stmt (loc->si))
6395 == GIMPLE_SWITCH));
6397 gsi_insert_on_edge (loc->e, assert_stmt);
6398 return true;
6401 /* Otherwise, we can insert right after LOC->SI iff the
6402 statement must not be the last statement in the block. */
6403 stmt = gsi_stmt (loc->si);
6404 if (!stmt_ends_bb_p (stmt))
6406 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6407 return false;
6410 /* If STMT must be the last statement in BB, we can only insert new
6411 assertions on the non-abnormal edge out of BB. Note that since
6412 STMT is not control flow, there may only be one non-abnormal edge
6413 out of BB. */
6414 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6415 if (!(e->flags & EDGE_ABNORMAL))
6417 gsi_insert_on_edge (e, assert_stmt);
6418 return true;
6421 gcc_unreachable ();
6425 /* Process all the insertions registered for every name N_i registered
6426 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6427 found in ASSERTS_FOR[i]. */
6429 static void
6430 process_assert_insertions (void)
6432 unsigned i;
6433 bitmap_iterator bi;
6434 bool update_edges_p = false;
6435 int num_asserts = 0;
6437 if (dump_file && (dump_flags & TDF_DETAILS))
6438 dump_all_asserts (dump_file);
6440 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6442 assert_locus_t loc = asserts_for[i];
6443 gcc_assert (loc);
6445 while (loc)
6447 assert_locus_t next = loc->next;
6448 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6449 free (loc);
6450 loc = next;
6451 num_asserts++;
6455 if (update_edges_p)
6456 gsi_commit_edge_inserts ();
6458 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6459 num_asserts);
6463 /* Traverse the flowgraph looking for conditional jumps to insert range
6464 expressions. These range expressions are meant to provide information
6465 to optimizations that need to reason in terms of value ranges. They
6466 will not be expanded into RTL. For instance, given:
6468 x = ...
6469 y = ...
6470 if (x < y)
6471 y = x - 2;
6472 else
6473 x = y + 3;
6475 this pass will transform the code into:
6477 x = ...
6478 y = ...
6479 if (x < y)
6481 x = ASSERT_EXPR <x, x < y>
6482 y = x - 2
6484 else
6486 y = ASSERT_EXPR <y, x >= y>
6487 x = y + 3
6490 The idea is that once copy and constant propagation have run, other
6491 optimizations will be able to determine what ranges of values can 'x'
6492 take in different paths of the code, simply by checking the reaching
6493 definition of 'x'. */
6495 static void
6496 insert_range_assertions (void)
6498 need_assert_for = BITMAP_ALLOC (NULL);
6499 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6501 calculate_dominance_info (CDI_DOMINATORS);
6503 find_assert_locations ();
6504 if (!bitmap_empty_p (need_assert_for))
6506 process_assert_insertions ();
6507 update_ssa (TODO_update_ssa_no_phi);
6510 if (dump_file && (dump_flags & TDF_DETAILS))
6512 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6513 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6516 free (asserts_for);
6517 BITMAP_FREE (need_assert_for);
6520 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6521 and "struct" hacks. If VRP can determine that the
6522 array subscript is a constant, check if it is outside valid
6523 range. If the array subscript is a RANGE, warn if it is
6524 non-overlapping with valid range.
6525 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6527 static void
6528 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6530 value_range_t* vr = NULL;
6531 tree low_sub, up_sub;
6532 tree low_bound, up_bound, up_bound_p1;
6533 tree base;
6535 if (TREE_NO_WARNING (ref))
6536 return;
6538 low_sub = up_sub = TREE_OPERAND (ref, 1);
6539 up_bound = array_ref_up_bound (ref);
6541 /* Can not check flexible arrays. */
6542 if (!up_bound
6543 || TREE_CODE (up_bound) != INTEGER_CST)
6544 return;
6546 /* Accesses to trailing arrays via pointers may access storage
6547 beyond the types array bounds. */
6548 base = get_base_address (ref);
6549 if ((warn_array_bounds < 2)
6550 && base && TREE_CODE (base) == MEM_REF)
6552 tree cref, next = NULL_TREE;
6554 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6555 return;
6557 cref = TREE_OPERAND (ref, 0);
6558 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6559 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6560 next && TREE_CODE (next) != FIELD_DECL;
6561 next = DECL_CHAIN (next))
6564 /* If this is the last field in a struct type or a field in a
6565 union type do not warn. */
6566 if (!next)
6567 return;
6570 low_bound = array_ref_low_bound (ref);
6571 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6572 build_int_cst (TREE_TYPE (up_bound), 1));
6574 /* Empty array. */
6575 if (tree_int_cst_equal (low_bound, up_bound_p1))
6577 warning_at (location, OPT_Warray_bounds,
6578 "array subscript is above array bounds");
6579 TREE_NO_WARNING (ref) = 1;
6582 if (TREE_CODE (low_sub) == SSA_NAME)
6584 vr = get_value_range (low_sub);
6585 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6587 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6588 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6592 if (vr && vr->type == VR_ANTI_RANGE)
6594 if (TREE_CODE (up_sub) == INTEGER_CST
6595 && (ignore_off_by_one
6596 ? tree_int_cst_lt (up_bound, up_sub)
6597 : tree_int_cst_le (up_bound, up_sub))
6598 && TREE_CODE (low_sub) == INTEGER_CST
6599 && tree_int_cst_le (low_sub, low_bound))
6601 warning_at (location, OPT_Warray_bounds,
6602 "array subscript is outside array bounds");
6603 TREE_NO_WARNING (ref) = 1;
6606 else if (TREE_CODE (up_sub) == INTEGER_CST
6607 && (ignore_off_by_one
6608 ? !tree_int_cst_le (up_sub, up_bound_p1)
6609 : !tree_int_cst_le (up_sub, up_bound)))
6611 if (dump_file && (dump_flags & TDF_DETAILS))
6613 fprintf (dump_file, "Array bound warning for ");
6614 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6615 fprintf (dump_file, "\n");
6617 warning_at (location, OPT_Warray_bounds,
6618 "array subscript is above array bounds");
6619 TREE_NO_WARNING (ref) = 1;
6621 else if (TREE_CODE (low_sub) == INTEGER_CST
6622 && tree_int_cst_lt (low_sub, low_bound))
6624 if (dump_file && (dump_flags & TDF_DETAILS))
6626 fprintf (dump_file, "Array bound warning for ");
6627 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6628 fprintf (dump_file, "\n");
6630 warning_at (location, OPT_Warray_bounds,
6631 "array subscript is below array bounds");
6632 TREE_NO_WARNING (ref) = 1;
6636 /* Searches if the expr T, located at LOCATION computes
6637 address of an ARRAY_REF, and call check_array_ref on it. */
6639 static void
6640 search_for_addr_array (tree t, location_t location)
6642 /* Check each ARRAY_REFs in the reference chain. */
6645 if (TREE_CODE (t) == ARRAY_REF)
6646 check_array_ref (location, t, true /*ignore_off_by_one*/);
6648 t = TREE_OPERAND (t, 0);
6650 while (handled_component_p (t));
6652 if (TREE_CODE (t) == MEM_REF
6653 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6654 && !TREE_NO_WARNING (t))
6656 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6657 tree low_bound, up_bound, el_sz;
6658 offset_int idx;
6659 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6660 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6661 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6662 return;
6664 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6665 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6666 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6667 if (!low_bound
6668 || TREE_CODE (low_bound) != INTEGER_CST
6669 || !up_bound
6670 || TREE_CODE (up_bound) != INTEGER_CST
6671 || !el_sz
6672 || TREE_CODE (el_sz) != INTEGER_CST)
6673 return;
6675 idx = mem_ref_offset (t);
6676 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6677 if (wi::lts_p (idx, 0))
6679 if (dump_file && (dump_flags & TDF_DETAILS))
6681 fprintf (dump_file, "Array bound warning for ");
6682 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6683 fprintf (dump_file, "\n");
6685 warning_at (location, OPT_Warray_bounds,
6686 "array subscript is below array bounds");
6687 TREE_NO_WARNING (t) = 1;
6689 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6690 - wi::to_offset (low_bound) + 1)))
6692 if (dump_file && (dump_flags & TDF_DETAILS))
6694 fprintf (dump_file, "Array bound warning for ");
6695 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6696 fprintf (dump_file, "\n");
6698 warning_at (location, OPT_Warray_bounds,
6699 "array subscript is above array bounds");
6700 TREE_NO_WARNING (t) = 1;
6705 /* walk_tree() callback that checks if *TP is
6706 an ARRAY_REF inside an ADDR_EXPR (in which an array
6707 subscript one outside the valid range is allowed). Call
6708 check_array_ref for each ARRAY_REF found. The location is
6709 passed in DATA. */
6711 static tree
6712 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6714 tree t = *tp;
6715 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6716 location_t location;
6718 if (EXPR_HAS_LOCATION (t))
6719 location = EXPR_LOCATION (t);
6720 else
6722 location_t *locp = (location_t *) wi->info;
6723 location = *locp;
6726 *walk_subtree = TRUE;
6728 if (TREE_CODE (t) == ARRAY_REF)
6729 check_array_ref (location, t, false /*ignore_off_by_one*/);
6731 else if (TREE_CODE (t) == ADDR_EXPR)
6733 search_for_addr_array (t, location);
6734 *walk_subtree = FALSE;
6737 return NULL_TREE;
6740 /* Walk over all statements of all reachable BBs and call check_array_bounds
6741 on them. */
6743 static void
6744 check_all_array_refs (void)
6746 basic_block bb;
6747 gimple_stmt_iterator si;
6749 FOR_EACH_BB_FN (bb, cfun)
6751 edge_iterator ei;
6752 edge e;
6753 bool executable = false;
6755 /* Skip blocks that were found to be unreachable. */
6756 FOR_EACH_EDGE (e, ei, bb->preds)
6757 executable |= !!(e->flags & EDGE_EXECUTABLE);
6758 if (!executable)
6759 continue;
6761 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6763 gimple stmt = gsi_stmt (si);
6764 struct walk_stmt_info wi;
6765 if (!gimple_has_location (stmt)
6766 || is_gimple_debug (stmt))
6767 continue;
6769 memset (&wi, 0, sizeof (wi));
6770 wi.info = CONST_CAST (void *, (const void *)
6771 gimple_location_ptr (stmt));
6773 walk_gimple_op (gsi_stmt (si),
6774 check_array_bounds,
6775 &wi);
6780 /* Return true if all imm uses of VAR are either in STMT, or
6781 feed (optionally through a chain of single imm uses) GIMPLE_COND
6782 in basic block COND_BB. */
6784 static bool
6785 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6787 use_operand_p use_p, use2_p;
6788 imm_use_iterator iter;
6790 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6791 if (USE_STMT (use_p) != stmt)
6793 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6794 if (is_gimple_debug (use_stmt))
6795 continue;
6796 while (is_gimple_assign (use_stmt)
6797 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6798 && single_imm_use (gimple_assign_lhs (use_stmt),
6799 &use2_p, &use_stmt2))
6800 use_stmt = use_stmt2;
6801 if (gimple_code (use_stmt) != GIMPLE_COND
6802 || gimple_bb (use_stmt) != cond_bb)
6803 return false;
6805 return true;
6808 /* Handle
6809 _4 = x_3 & 31;
6810 if (_4 != 0)
6811 goto <bb 6>;
6812 else
6813 goto <bb 7>;
6814 <bb 6>:
6815 __builtin_unreachable ();
6816 <bb 7>:
6817 x_5 = ASSERT_EXPR <x_3, ...>;
6818 If x_3 has no other immediate uses (checked by caller),
6819 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6820 from the non-zero bitmask. */
6822 static void
6823 maybe_set_nonzero_bits (basic_block bb, tree var)
6825 edge e = single_pred_edge (bb);
6826 basic_block cond_bb = e->src;
6827 gimple stmt = last_stmt (cond_bb);
6828 tree cst;
6830 if (stmt == NULL
6831 || gimple_code (stmt) != GIMPLE_COND
6832 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6833 ? EQ_EXPR : NE_EXPR)
6834 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6835 || !integer_zerop (gimple_cond_rhs (stmt)))
6836 return;
6838 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6839 if (!is_gimple_assign (stmt)
6840 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6841 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6842 return;
6843 if (gimple_assign_rhs1 (stmt) != var)
6845 gimple stmt2;
6847 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6848 return;
6849 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6850 if (!gimple_assign_cast_p (stmt2)
6851 || gimple_assign_rhs1 (stmt2) != var
6852 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6853 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6854 != TYPE_PRECISION (TREE_TYPE (var))))
6855 return;
6857 cst = gimple_assign_rhs2 (stmt);
6858 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6861 /* Convert range assertion expressions into the implied copies and
6862 copy propagate away the copies. Doing the trivial copy propagation
6863 here avoids the need to run the full copy propagation pass after
6864 VRP.
6866 FIXME, this will eventually lead to copy propagation removing the
6867 names that had useful range information attached to them. For
6868 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6869 then N_i will have the range [3, +INF].
6871 However, by converting the assertion into the implied copy
6872 operation N_i = N_j, we will then copy-propagate N_j into the uses
6873 of N_i and lose the range information. We may want to hold on to
6874 ASSERT_EXPRs a little while longer as the ranges could be used in
6875 things like jump threading.
6877 The problem with keeping ASSERT_EXPRs around is that passes after
6878 VRP need to handle them appropriately.
6880 Another approach would be to make the range information a first
6881 class property of the SSA_NAME so that it can be queried from
6882 any pass. This is made somewhat more complex by the need for
6883 multiple ranges to be associated with one SSA_NAME. */
6885 static void
6886 remove_range_assertions (void)
6888 basic_block bb;
6889 gimple_stmt_iterator si;
6890 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6891 a basic block preceeded by GIMPLE_COND branching to it and
6892 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6893 int is_unreachable;
6895 /* Note that the BSI iterator bump happens at the bottom of the
6896 loop and no bump is necessary if we're removing the statement
6897 referenced by the current BSI. */
6898 FOR_EACH_BB_FN (bb, cfun)
6899 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6901 gimple stmt = gsi_stmt (si);
6902 gimple use_stmt;
6904 if (is_gimple_assign (stmt)
6905 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6907 tree lhs = gimple_assign_lhs (stmt);
6908 tree rhs = gimple_assign_rhs1 (stmt);
6909 tree var;
6910 tree cond = fold (ASSERT_EXPR_COND (rhs));
6911 use_operand_p use_p;
6912 imm_use_iterator iter;
6914 gcc_assert (cond != boolean_false_node);
6916 var = ASSERT_EXPR_VAR (rhs);
6917 gcc_assert (TREE_CODE (var) == SSA_NAME);
6919 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6920 && SSA_NAME_RANGE_INFO (lhs))
6922 if (is_unreachable == -1)
6924 is_unreachable = 0;
6925 if (single_pred_p (bb)
6926 && assert_unreachable_fallthru_edge_p
6927 (single_pred_edge (bb)))
6928 is_unreachable = 1;
6930 /* Handle
6931 if (x_7 >= 10 && x_7 < 20)
6932 __builtin_unreachable ();
6933 x_8 = ASSERT_EXPR <x_7, ...>;
6934 if the only uses of x_7 are in the ASSERT_EXPR and
6935 in the condition. In that case, we can copy the
6936 range info from x_8 computed in this pass also
6937 for x_7. */
6938 if (is_unreachable
6939 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6940 single_pred (bb)))
6942 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6943 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6944 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6945 maybe_set_nonzero_bits (bb, var);
6949 /* Propagate the RHS into every use of the LHS. */
6950 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6951 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6952 SET_USE (use_p, var);
6954 /* And finally, remove the copy, it is not needed. */
6955 gsi_remove (&si, true);
6956 release_defs (stmt);
6958 else
6960 if (!is_gimple_debug (gsi_stmt (si)))
6961 is_unreachable = 0;
6962 gsi_next (&si);
6968 /* Return true if STMT is interesting for VRP. */
6970 static bool
6971 stmt_interesting_for_vrp (gimple stmt)
6973 if (gimple_code (stmt) == GIMPLE_PHI)
6975 tree res = gimple_phi_result (stmt);
6976 return (!virtual_operand_p (res)
6977 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6978 || POINTER_TYPE_P (TREE_TYPE (res))));
6980 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6982 tree lhs = gimple_get_lhs (stmt);
6984 /* In general, assignments with virtual operands are not useful
6985 for deriving ranges, with the obvious exception of calls to
6986 builtin functions. */
6987 if (lhs && TREE_CODE (lhs) == SSA_NAME
6988 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6989 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6990 && (is_gimple_call (stmt)
6991 || !gimple_vuse (stmt)))
6992 return true;
6993 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6994 switch (gimple_call_internal_fn (stmt))
6996 case IFN_ADD_OVERFLOW:
6997 case IFN_SUB_OVERFLOW:
6998 case IFN_MUL_OVERFLOW:
6999 /* These internal calls return _Complex integer type,
7000 but are interesting to VRP nevertheless. */
7001 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7002 return true;
7003 break;
7004 default:
7005 break;
7008 else if (gimple_code (stmt) == GIMPLE_COND
7009 || gimple_code (stmt) == GIMPLE_SWITCH)
7010 return true;
7012 return false;
7016 /* Initialize local data structures for VRP. */
7018 static void
7019 vrp_initialize (void)
7021 basic_block bb;
7023 values_propagated = false;
7024 num_vr_values = num_ssa_names;
7025 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
7026 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
7028 FOR_EACH_BB_FN (bb, cfun)
7030 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7031 gsi_next (&si))
7033 gphi *phi = si.phi ();
7034 if (!stmt_interesting_for_vrp (phi))
7036 tree lhs = PHI_RESULT (phi);
7037 set_value_range_to_varying (get_value_range (lhs));
7038 prop_set_simulate_again (phi, false);
7040 else
7041 prop_set_simulate_again (phi, true);
7044 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7045 gsi_next (&si))
7047 gimple stmt = gsi_stmt (si);
7049 /* If the statement is a control insn, then we do not
7050 want to avoid simulating the statement once. Failure
7051 to do so means that those edges will never get added. */
7052 if (stmt_ends_bb_p (stmt))
7053 prop_set_simulate_again (stmt, true);
7054 else if (!stmt_interesting_for_vrp (stmt))
7056 ssa_op_iter i;
7057 tree def;
7058 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7059 set_value_range_to_varying (get_value_range (def));
7060 prop_set_simulate_again (stmt, false);
7062 else
7063 prop_set_simulate_again (stmt, true);
7068 /* Return the singleton value-range for NAME or NAME. */
7070 static inline tree
7071 vrp_valueize (tree name)
7073 if (TREE_CODE (name) == SSA_NAME)
7075 value_range_t *vr = get_value_range (name);
7076 if (vr->type == VR_RANGE
7077 && (vr->min == vr->max
7078 || operand_equal_p (vr->min, vr->max, 0)))
7079 return vr->min;
7081 return name;
7084 /* Return the singleton value-range for NAME if that is a constant
7085 but signal to not follow SSA edges. */
7087 static inline tree
7088 vrp_valueize_1 (tree name)
7090 if (TREE_CODE (name) == SSA_NAME)
7092 /* If the definition may be simulated again we cannot follow
7093 this SSA edge as the SSA propagator does not necessarily
7094 re-visit the use. */
7095 gimple def_stmt = SSA_NAME_DEF_STMT (name);
7096 if (!gimple_nop_p (def_stmt)
7097 && prop_simulate_again_p (def_stmt))
7098 return NULL_TREE;
7099 value_range_t *vr = get_value_range (name);
7100 if (range_int_cst_singleton_p (vr))
7101 return vr->min;
7103 return name;
7106 /* Visit assignment STMT. If it produces an interesting range, record
7107 the SSA name in *OUTPUT_P. */
7109 static enum ssa_prop_result
7110 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
7112 tree def, lhs;
7113 ssa_op_iter iter;
7114 enum gimple_code code = gimple_code (stmt);
7115 lhs = gimple_get_lhs (stmt);
7117 /* We only keep track of ranges in integral and pointer types. */
7118 if (TREE_CODE (lhs) == SSA_NAME
7119 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7120 /* It is valid to have NULL MIN/MAX values on a type. See
7121 build_range_type. */
7122 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7123 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7124 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7126 value_range_t new_vr = VR_INITIALIZER;
7128 /* Try folding the statement to a constant first. */
7129 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7130 vrp_valueize_1);
7131 if (tem && is_gimple_min_invariant (tem))
7132 set_value_range_to_value (&new_vr, tem, NULL);
7133 /* Then dispatch to value-range extracting functions. */
7134 else if (code == GIMPLE_CALL)
7135 extract_range_basic (&new_vr, stmt);
7136 else
7137 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7139 if (update_value_range (lhs, &new_vr))
7141 *output_p = lhs;
7143 if (dump_file && (dump_flags & TDF_DETAILS))
7145 fprintf (dump_file, "Found new range for ");
7146 print_generic_expr (dump_file, lhs, 0);
7147 fprintf (dump_file, ": ");
7148 dump_value_range (dump_file, &new_vr);
7149 fprintf (dump_file, "\n");
7152 if (new_vr.type == VR_VARYING)
7153 return SSA_PROP_VARYING;
7155 return SSA_PROP_INTERESTING;
7158 return SSA_PROP_NOT_INTERESTING;
7160 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7161 switch (gimple_call_internal_fn (stmt))
7163 case IFN_ADD_OVERFLOW:
7164 case IFN_SUB_OVERFLOW:
7165 case IFN_MUL_OVERFLOW:
7166 /* These internal calls return _Complex integer type,
7167 which VRP does not track, but the immediate uses
7168 thereof might be interesting. */
7169 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7171 imm_use_iterator iter;
7172 use_operand_p use_p;
7173 enum ssa_prop_result res = SSA_PROP_VARYING;
7175 set_value_range_to_varying (get_value_range (lhs));
7177 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7179 gimple use_stmt = USE_STMT (use_p);
7180 if (!is_gimple_assign (use_stmt))
7181 continue;
7182 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7183 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7184 continue;
7185 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7186 tree use_lhs = gimple_assign_lhs (use_stmt);
7187 if (TREE_CODE (rhs1) != rhs_code
7188 || TREE_OPERAND (rhs1, 0) != lhs
7189 || TREE_CODE (use_lhs) != SSA_NAME
7190 || !stmt_interesting_for_vrp (use_stmt)
7191 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7192 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7193 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7194 continue;
7196 /* If there is a change in the value range for any of the
7197 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7198 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7199 or IMAGPART_EXPR immediate uses, but none of them have
7200 a change in their value ranges, return
7201 SSA_PROP_NOT_INTERESTING. If there are no
7202 {REAL,IMAG}PART_EXPR uses at all,
7203 return SSA_PROP_VARYING. */
7204 value_range_t new_vr = VR_INITIALIZER;
7205 extract_range_basic (&new_vr, use_stmt);
7206 value_range_t *old_vr = get_value_range (use_lhs);
7207 if (old_vr->type != new_vr.type
7208 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7209 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7210 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7211 res = SSA_PROP_INTERESTING;
7212 else
7213 res = SSA_PROP_NOT_INTERESTING;
7214 BITMAP_FREE (new_vr.equiv);
7215 if (res == SSA_PROP_INTERESTING)
7217 *output_p = lhs;
7218 return res;
7222 return res;
7224 break;
7225 default:
7226 break;
7229 /* Every other statement produces no useful ranges. */
7230 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7231 set_value_range_to_varying (get_value_range (def));
7233 return SSA_PROP_VARYING;
7236 /* Helper that gets the value range of the SSA_NAME with version I
7237 or a symbolic range containing the SSA_NAME only if the value range
7238 is varying or undefined. */
7240 static inline value_range_t
7241 get_vr_for_comparison (int i)
7243 value_range_t vr = *get_value_range (ssa_name (i));
7245 /* If name N_i does not have a valid range, use N_i as its own
7246 range. This allows us to compare against names that may
7247 have N_i in their ranges. */
7248 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7250 vr.type = VR_RANGE;
7251 vr.min = ssa_name (i);
7252 vr.max = ssa_name (i);
7255 return vr;
7258 /* Compare all the value ranges for names equivalent to VAR with VAL
7259 using comparison code COMP. Return the same value returned by
7260 compare_range_with_value, including the setting of
7261 *STRICT_OVERFLOW_P. */
7263 static tree
7264 compare_name_with_value (enum tree_code comp, tree var, tree val,
7265 bool *strict_overflow_p)
7267 bitmap_iterator bi;
7268 unsigned i;
7269 bitmap e;
7270 tree retval, t;
7271 int used_strict_overflow;
7272 bool sop;
7273 value_range_t equiv_vr;
7275 /* Get the set of equivalences for VAR. */
7276 e = get_value_range (var)->equiv;
7278 /* Start at -1. Set it to 0 if we do a comparison without relying
7279 on overflow, or 1 if all comparisons rely on overflow. */
7280 used_strict_overflow = -1;
7282 /* Compare vars' value range with val. */
7283 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7284 sop = false;
7285 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7286 if (retval)
7287 used_strict_overflow = sop ? 1 : 0;
7289 /* If the equiv set is empty we have done all work we need to do. */
7290 if (e == NULL)
7292 if (retval
7293 && used_strict_overflow > 0)
7294 *strict_overflow_p = true;
7295 return retval;
7298 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7300 equiv_vr = get_vr_for_comparison (i);
7301 sop = false;
7302 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7303 if (t)
7305 /* If we get different answers from different members
7306 of the equivalence set this check must be in a dead
7307 code region. Folding it to a trap representation
7308 would be correct here. For now just return don't-know. */
7309 if (retval != NULL
7310 && t != retval)
7312 retval = NULL_TREE;
7313 break;
7315 retval = t;
7317 if (!sop)
7318 used_strict_overflow = 0;
7319 else if (used_strict_overflow < 0)
7320 used_strict_overflow = 1;
7324 if (retval
7325 && used_strict_overflow > 0)
7326 *strict_overflow_p = true;
7328 return retval;
7332 /* Given a comparison code COMP and names N1 and N2, compare all the
7333 ranges equivalent to N1 against all the ranges equivalent to N2
7334 to determine the value of N1 COMP N2. Return the same value
7335 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7336 whether we relied on an overflow infinity in the comparison. */
7339 static tree
7340 compare_names (enum tree_code comp, tree n1, tree n2,
7341 bool *strict_overflow_p)
7343 tree t, retval;
7344 bitmap e1, e2;
7345 bitmap_iterator bi1, bi2;
7346 unsigned i1, i2;
7347 int used_strict_overflow;
7348 static bitmap_obstack *s_obstack = NULL;
7349 static bitmap s_e1 = NULL, s_e2 = NULL;
7351 /* Compare the ranges of every name equivalent to N1 against the
7352 ranges of every name equivalent to N2. */
7353 e1 = get_value_range (n1)->equiv;
7354 e2 = get_value_range (n2)->equiv;
7356 /* Use the fake bitmaps if e1 or e2 are not available. */
7357 if (s_obstack == NULL)
7359 s_obstack = XNEW (bitmap_obstack);
7360 bitmap_obstack_initialize (s_obstack);
7361 s_e1 = BITMAP_ALLOC (s_obstack);
7362 s_e2 = BITMAP_ALLOC (s_obstack);
7364 if (e1 == NULL)
7365 e1 = s_e1;
7366 if (e2 == NULL)
7367 e2 = s_e2;
7369 /* Add N1 and N2 to their own set of equivalences to avoid
7370 duplicating the body of the loop just to check N1 and N2
7371 ranges. */
7372 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7373 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7375 /* If the equivalence sets have a common intersection, then the two
7376 names can be compared without checking their ranges. */
7377 if (bitmap_intersect_p (e1, e2))
7379 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7380 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7382 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7383 ? boolean_true_node
7384 : boolean_false_node;
7387 /* Start at -1. Set it to 0 if we do a comparison without relying
7388 on overflow, or 1 if all comparisons rely on overflow. */
7389 used_strict_overflow = -1;
7391 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7392 N2 to their own set of equivalences to avoid duplicating the body
7393 of the loop just to check N1 and N2 ranges. */
7394 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7396 value_range_t vr1 = get_vr_for_comparison (i1);
7398 t = retval = NULL_TREE;
7399 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7401 bool sop = false;
7403 value_range_t vr2 = get_vr_for_comparison (i2);
7405 t = compare_ranges (comp, &vr1, &vr2, &sop);
7406 if (t)
7408 /* If we get different answers from different members
7409 of the equivalence set this check must be in a dead
7410 code region. Folding it to a trap representation
7411 would be correct here. For now just return don't-know. */
7412 if (retval != NULL
7413 && t != retval)
7415 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7416 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7417 return NULL_TREE;
7419 retval = t;
7421 if (!sop)
7422 used_strict_overflow = 0;
7423 else if (used_strict_overflow < 0)
7424 used_strict_overflow = 1;
7428 if (retval)
7430 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7431 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7432 if (used_strict_overflow > 0)
7433 *strict_overflow_p = true;
7434 return retval;
7438 /* None of the equivalent ranges are useful in computing this
7439 comparison. */
7440 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7441 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7442 return NULL_TREE;
7445 /* Helper function for vrp_evaluate_conditional_warnv. */
7447 static tree
7448 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7449 tree op0, tree op1,
7450 bool * strict_overflow_p)
7452 value_range_t *vr0, *vr1;
7454 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7455 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7457 tree res = NULL_TREE;
7458 if (vr0 && vr1)
7459 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7460 if (!res && vr0)
7461 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7462 if (!res && vr1)
7463 res = (compare_range_with_value
7464 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7465 return res;
7468 /* Helper function for vrp_evaluate_conditional_warnv. */
7470 static tree
7471 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7472 tree op1, bool use_equiv_p,
7473 bool *strict_overflow_p, bool *only_ranges)
7475 tree ret;
7476 if (only_ranges)
7477 *only_ranges = true;
7479 /* We only deal with integral and pointer types. */
7480 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7481 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7482 return NULL_TREE;
7484 if (use_equiv_p)
7486 if (only_ranges
7487 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7488 (code, op0, op1, strict_overflow_p)))
7489 return ret;
7490 *only_ranges = false;
7491 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7492 return compare_names (code, op0, op1, strict_overflow_p);
7493 else if (TREE_CODE (op0) == SSA_NAME)
7494 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7495 else if (TREE_CODE (op1) == SSA_NAME)
7496 return (compare_name_with_value
7497 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7499 else
7500 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7501 strict_overflow_p);
7502 return NULL_TREE;
7505 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7506 information. Return NULL if the conditional can not be evaluated.
7507 The ranges of all the names equivalent with the operands in COND
7508 will be used when trying to compute the value. If the result is
7509 based on undefined signed overflow, issue a warning if
7510 appropriate. */
7512 static tree
7513 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7515 bool sop;
7516 tree ret;
7517 bool only_ranges;
7519 /* Some passes and foldings leak constants with overflow flag set
7520 into the IL. Avoid doing wrong things with these and bail out. */
7521 if ((TREE_CODE (op0) == INTEGER_CST
7522 && TREE_OVERFLOW (op0))
7523 || (TREE_CODE (op1) == INTEGER_CST
7524 && TREE_OVERFLOW (op1)))
7525 return NULL_TREE;
7527 sop = false;
7528 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7529 &only_ranges);
7531 if (ret && sop)
7533 enum warn_strict_overflow_code wc;
7534 const char* warnmsg;
7536 if (is_gimple_min_invariant (ret))
7538 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7539 warnmsg = G_("assuming signed overflow does not occur when "
7540 "simplifying conditional to constant");
7542 else
7544 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7545 warnmsg = G_("assuming signed overflow does not occur when "
7546 "simplifying conditional");
7549 if (issue_strict_overflow_warning (wc))
7551 location_t location;
7553 if (!gimple_has_location (stmt))
7554 location = input_location;
7555 else
7556 location = gimple_location (stmt);
7557 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7561 if (warn_type_limits
7562 && ret && only_ranges
7563 && TREE_CODE_CLASS (code) == tcc_comparison
7564 && TREE_CODE (op0) == SSA_NAME)
7566 /* If the comparison is being folded and the operand on the LHS
7567 is being compared against a constant value that is outside of
7568 the natural range of OP0's type, then the predicate will
7569 always fold regardless of the value of OP0. If -Wtype-limits
7570 was specified, emit a warning. */
7571 tree type = TREE_TYPE (op0);
7572 value_range_t *vr0 = get_value_range (op0);
7574 if (vr0->type == VR_RANGE
7575 && INTEGRAL_TYPE_P (type)
7576 && vrp_val_is_min (vr0->min)
7577 && vrp_val_is_max (vr0->max)
7578 && is_gimple_min_invariant (op1))
7580 location_t location;
7582 if (!gimple_has_location (stmt))
7583 location = input_location;
7584 else
7585 location = gimple_location (stmt);
7587 warning_at (location, OPT_Wtype_limits,
7588 integer_zerop (ret)
7589 ? G_("comparison always false "
7590 "due to limited range of data type")
7591 : G_("comparison always true "
7592 "due to limited range of data type"));
7596 return ret;
7600 /* Visit conditional statement STMT. If we can determine which edge
7601 will be taken out of STMT's basic block, record it in
7602 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7603 SSA_PROP_VARYING. */
7605 static enum ssa_prop_result
7606 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7608 tree val;
7609 bool sop;
7611 *taken_edge_p = NULL;
7613 if (dump_file && (dump_flags & TDF_DETAILS))
7615 tree use;
7616 ssa_op_iter i;
7618 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7619 print_gimple_stmt (dump_file, stmt, 0, 0);
7620 fprintf (dump_file, "\nWith known ranges\n");
7622 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7624 fprintf (dump_file, "\t");
7625 print_generic_expr (dump_file, use, 0);
7626 fprintf (dump_file, ": ");
7627 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7630 fprintf (dump_file, "\n");
7633 /* Compute the value of the predicate COND by checking the known
7634 ranges of each of its operands.
7636 Note that we cannot evaluate all the equivalent ranges here
7637 because those ranges may not yet be final and with the current
7638 propagation strategy, we cannot determine when the value ranges
7639 of the names in the equivalence set have changed.
7641 For instance, given the following code fragment
7643 i_5 = PHI <8, i_13>
7645 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7646 if (i_14 == 1)
7649 Assume that on the first visit to i_14, i_5 has the temporary
7650 range [8, 8] because the second argument to the PHI function is
7651 not yet executable. We derive the range ~[0, 0] for i_14 and the
7652 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7653 the first time, since i_14 is equivalent to the range [8, 8], we
7654 determine that the predicate is always false.
7656 On the next round of propagation, i_13 is determined to be
7657 VARYING, which causes i_5 to drop down to VARYING. So, another
7658 visit to i_14 is scheduled. In this second visit, we compute the
7659 exact same range and equivalence set for i_14, namely ~[0, 0] and
7660 { i_5 }. But we did not have the previous range for i_5
7661 registered, so vrp_visit_assignment thinks that the range for
7662 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7663 is not visited again, which stops propagation from visiting
7664 statements in the THEN clause of that if().
7666 To properly fix this we would need to keep the previous range
7667 value for the names in the equivalence set. This way we would've
7668 discovered that from one visit to the other i_5 changed from
7669 range [8, 8] to VR_VARYING.
7671 However, fixing this apparent limitation may not be worth the
7672 additional checking. Testing on several code bases (GCC, DLV,
7673 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7674 4 more predicates folded in SPEC. */
7675 sop = false;
7677 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7678 gimple_cond_lhs (stmt),
7679 gimple_cond_rhs (stmt),
7680 false, &sop, NULL);
7681 if (val)
7683 if (!sop)
7684 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7685 else
7687 if (dump_file && (dump_flags & TDF_DETAILS))
7688 fprintf (dump_file,
7689 "\nIgnoring predicate evaluation because "
7690 "it assumes that signed overflow is undefined");
7691 val = NULL_TREE;
7695 if (dump_file && (dump_flags & TDF_DETAILS))
7697 fprintf (dump_file, "\nPredicate evaluates to: ");
7698 if (val == NULL_TREE)
7699 fprintf (dump_file, "DON'T KNOW\n");
7700 else
7701 print_generic_stmt (dump_file, val, 0);
7704 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7707 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7708 that includes the value VAL. The search is restricted to the range
7709 [START_IDX, n - 1] where n is the size of VEC.
7711 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7712 returned.
7714 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7715 it is placed in IDX and false is returned.
7717 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7718 returned. */
7720 static bool
7721 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7723 size_t n = gimple_switch_num_labels (stmt);
7724 size_t low, high;
7726 /* Find case label for minimum of the value range or the next one.
7727 At each iteration we are searching in [low, high - 1]. */
7729 for (low = start_idx, high = n; high != low; )
7731 tree t;
7732 int cmp;
7733 /* Note that i != high, so we never ask for n. */
7734 size_t i = (high + low) / 2;
7735 t = gimple_switch_label (stmt, i);
7737 /* Cache the result of comparing CASE_LOW and val. */
7738 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7740 if (cmp == 0)
7742 /* Ranges cannot be empty. */
7743 *idx = i;
7744 return true;
7746 else if (cmp > 0)
7747 high = i;
7748 else
7750 low = i + 1;
7751 if (CASE_HIGH (t) != NULL
7752 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7754 *idx = i;
7755 return true;
7760 *idx = high;
7761 return false;
7764 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7765 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7766 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7767 then MAX_IDX < MIN_IDX.
7768 Returns true if the default label is not needed. */
7770 static bool
7771 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7772 size_t *max_idx)
7774 size_t i, j;
7775 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7776 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7778 if (i == j
7779 && min_take_default
7780 && max_take_default)
7782 /* Only the default case label reached.
7783 Return an empty range. */
7784 *min_idx = 1;
7785 *max_idx = 0;
7786 return false;
7788 else
7790 bool take_default = min_take_default || max_take_default;
7791 tree low, high;
7792 size_t k;
7794 if (max_take_default)
7795 j--;
7797 /* If the case label range is continuous, we do not need
7798 the default case label. Verify that. */
7799 high = CASE_LOW (gimple_switch_label (stmt, i));
7800 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7801 high = CASE_HIGH (gimple_switch_label (stmt, i));
7802 for (k = i + 1; k <= j; ++k)
7804 low = CASE_LOW (gimple_switch_label (stmt, k));
7805 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7807 take_default = true;
7808 break;
7810 high = low;
7811 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7812 high = CASE_HIGH (gimple_switch_label (stmt, k));
7815 *min_idx = i;
7816 *max_idx = j;
7817 return !take_default;
7821 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7822 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7823 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7824 Returns true if the default label is not needed. */
7826 static bool
7827 find_case_label_ranges (gswitch *stmt, value_range_t *vr, size_t *min_idx1,
7828 size_t *max_idx1, size_t *min_idx2,
7829 size_t *max_idx2)
7831 size_t i, j, k, l;
7832 unsigned int n = gimple_switch_num_labels (stmt);
7833 bool take_default;
7834 tree case_low, case_high;
7835 tree min = vr->min, max = vr->max;
7837 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7839 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7841 /* Set second range to emtpy. */
7842 *min_idx2 = 1;
7843 *max_idx2 = 0;
7845 if (vr->type == VR_RANGE)
7847 *min_idx1 = i;
7848 *max_idx1 = j;
7849 return !take_default;
7852 /* Set first range to all case labels. */
7853 *min_idx1 = 1;
7854 *max_idx1 = n - 1;
7856 if (i > j)
7857 return false;
7859 /* Make sure all the values of case labels [i , j] are contained in
7860 range [MIN, MAX]. */
7861 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7862 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7863 if (tree_int_cst_compare (case_low, min) < 0)
7864 i += 1;
7865 if (case_high != NULL_TREE
7866 && tree_int_cst_compare (max, case_high) < 0)
7867 j -= 1;
7869 if (i > j)
7870 return false;
7872 /* If the range spans case labels [i, j], the corresponding anti-range spans
7873 the labels [1, i - 1] and [j + 1, n - 1]. */
7874 k = j + 1;
7875 l = n - 1;
7876 if (k > l)
7878 k = 1;
7879 l = 0;
7882 j = i - 1;
7883 i = 1;
7884 if (i > j)
7886 i = k;
7887 j = l;
7888 k = 1;
7889 l = 0;
7892 *min_idx1 = i;
7893 *max_idx1 = j;
7894 *min_idx2 = k;
7895 *max_idx2 = l;
7896 return false;
7899 /* Visit switch statement STMT. If we can determine which edge
7900 will be taken out of STMT's basic block, record it in
7901 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7902 SSA_PROP_VARYING. */
7904 static enum ssa_prop_result
7905 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7907 tree op, val;
7908 value_range_t *vr;
7909 size_t i = 0, j = 0, k, l;
7910 bool take_default;
7912 *taken_edge_p = NULL;
7913 op = gimple_switch_index (stmt);
7914 if (TREE_CODE (op) != SSA_NAME)
7915 return SSA_PROP_VARYING;
7917 vr = get_value_range (op);
7918 if (dump_file && (dump_flags & TDF_DETAILS))
7920 fprintf (dump_file, "\nVisiting switch expression with operand ");
7921 print_generic_expr (dump_file, op, 0);
7922 fprintf (dump_file, " with known range ");
7923 dump_value_range (dump_file, vr);
7924 fprintf (dump_file, "\n");
7927 if ((vr->type != VR_RANGE
7928 && vr->type != VR_ANTI_RANGE)
7929 || symbolic_range_p (vr))
7930 return SSA_PROP_VARYING;
7932 /* Find the single edge that is taken from the switch expression. */
7933 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7935 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7936 label */
7937 if (j < i)
7939 gcc_assert (take_default);
7940 val = gimple_switch_default_label (stmt);
7942 else
7944 /* Check if labels with index i to j and maybe the default label
7945 are all reaching the same label. */
7947 val = gimple_switch_label (stmt, i);
7948 if (take_default
7949 && CASE_LABEL (gimple_switch_default_label (stmt))
7950 != CASE_LABEL (val))
7952 if (dump_file && (dump_flags & TDF_DETAILS))
7953 fprintf (dump_file, " not a single destination for this "
7954 "range\n");
7955 return SSA_PROP_VARYING;
7957 for (++i; i <= j; ++i)
7959 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7961 if (dump_file && (dump_flags & TDF_DETAILS))
7962 fprintf (dump_file, " not a single destination for this "
7963 "range\n");
7964 return SSA_PROP_VARYING;
7967 for (; k <= l; ++k)
7969 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7971 if (dump_file && (dump_flags & TDF_DETAILS))
7972 fprintf (dump_file, " not a single destination for this "
7973 "range\n");
7974 return SSA_PROP_VARYING;
7979 *taken_edge_p = find_edge (gimple_bb (stmt),
7980 label_to_block (CASE_LABEL (val)));
7982 if (dump_file && (dump_flags & TDF_DETAILS))
7984 fprintf (dump_file, " will take edge to ");
7985 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7988 return SSA_PROP_INTERESTING;
7992 /* Evaluate statement STMT. If the statement produces a useful range,
7993 return SSA_PROP_INTERESTING and record the SSA name with the
7994 interesting range into *OUTPUT_P.
7996 If STMT is a conditional branch and we can determine its truth
7997 value, the taken edge is recorded in *TAKEN_EDGE_P.
7999 If STMT produces a varying value, return SSA_PROP_VARYING. */
8001 static enum ssa_prop_result
8002 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
8004 tree def;
8005 ssa_op_iter iter;
8007 if (dump_file && (dump_flags & TDF_DETAILS))
8009 fprintf (dump_file, "\nVisiting statement:\n");
8010 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
8013 if (!stmt_interesting_for_vrp (stmt))
8014 gcc_assert (stmt_ends_bb_p (stmt));
8015 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
8016 return vrp_visit_assignment_or_call (stmt, output_p);
8017 else if (gimple_code (stmt) == GIMPLE_COND)
8018 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
8019 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8020 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
8022 /* All other statements produce nothing of interest for VRP, so mark
8023 their outputs varying and prevent further simulation. */
8024 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
8025 set_value_range_to_varying (get_value_range (def));
8027 return SSA_PROP_VARYING;
8030 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8031 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8032 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8033 possible such range. The resulting range is not canonicalized. */
8035 static void
8036 union_ranges (enum value_range_type *vr0type,
8037 tree *vr0min, tree *vr0max,
8038 enum value_range_type vr1type,
8039 tree vr1min, tree vr1max)
8041 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8042 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8044 /* [] is vr0, () is vr1 in the following classification comments. */
8045 if (mineq && maxeq)
8047 /* [( )] */
8048 if (*vr0type == vr1type)
8049 /* Nothing to do for equal ranges. */
8051 else if ((*vr0type == VR_RANGE
8052 && vr1type == VR_ANTI_RANGE)
8053 || (*vr0type == VR_ANTI_RANGE
8054 && vr1type == VR_RANGE))
8056 /* For anti-range with range union the result is varying. */
8057 goto give_up;
8059 else
8060 gcc_unreachable ();
8062 else if (operand_less_p (*vr0max, vr1min) == 1
8063 || operand_less_p (vr1max, *vr0min) == 1)
8065 /* [ ] ( ) or ( ) [ ]
8066 If the ranges have an empty intersection, result of the union
8067 operation is the anti-range or if both are anti-ranges
8068 it covers all. */
8069 if (*vr0type == VR_ANTI_RANGE
8070 && vr1type == VR_ANTI_RANGE)
8071 goto give_up;
8072 else if (*vr0type == VR_ANTI_RANGE
8073 && vr1type == VR_RANGE)
8075 else if (*vr0type == VR_RANGE
8076 && vr1type == VR_ANTI_RANGE)
8078 *vr0type = vr1type;
8079 *vr0min = vr1min;
8080 *vr0max = vr1max;
8082 else if (*vr0type == VR_RANGE
8083 && vr1type == VR_RANGE)
8085 /* The result is the convex hull of both ranges. */
8086 if (operand_less_p (*vr0max, vr1min) == 1)
8088 /* If the result can be an anti-range, create one. */
8089 if (TREE_CODE (*vr0max) == INTEGER_CST
8090 && TREE_CODE (vr1min) == INTEGER_CST
8091 && vrp_val_is_min (*vr0min)
8092 && vrp_val_is_max (vr1max))
8094 tree min = int_const_binop (PLUS_EXPR,
8095 *vr0max,
8096 build_int_cst (TREE_TYPE (*vr0max), 1));
8097 tree max = int_const_binop (MINUS_EXPR,
8098 vr1min,
8099 build_int_cst (TREE_TYPE (vr1min), 1));
8100 if (!operand_less_p (max, min))
8102 *vr0type = VR_ANTI_RANGE;
8103 *vr0min = min;
8104 *vr0max = max;
8106 else
8107 *vr0max = vr1max;
8109 else
8110 *vr0max = vr1max;
8112 else
8114 /* If the result can be an anti-range, create one. */
8115 if (TREE_CODE (vr1max) == INTEGER_CST
8116 && TREE_CODE (*vr0min) == INTEGER_CST
8117 && vrp_val_is_min (vr1min)
8118 && vrp_val_is_max (*vr0max))
8120 tree min = int_const_binop (PLUS_EXPR,
8121 vr1max,
8122 build_int_cst (TREE_TYPE (vr1max), 1));
8123 tree max = int_const_binop (MINUS_EXPR,
8124 *vr0min,
8125 build_int_cst (TREE_TYPE (*vr0min), 1));
8126 if (!operand_less_p (max, min))
8128 *vr0type = VR_ANTI_RANGE;
8129 *vr0min = min;
8130 *vr0max = max;
8132 else
8133 *vr0min = vr1min;
8135 else
8136 *vr0min = vr1min;
8139 else
8140 gcc_unreachable ();
8142 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8143 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8145 /* [ ( ) ] or [( ) ] or [ ( )] */
8146 if (*vr0type == VR_RANGE
8147 && vr1type == VR_RANGE)
8149 else if (*vr0type == VR_ANTI_RANGE
8150 && vr1type == VR_ANTI_RANGE)
8152 *vr0type = vr1type;
8153 *vr0min = vr1min;
8154 *vr0max = vr1max;
8156 else if (*vr0type == VR_ANTI_RANGE
8157 && vr1type == VR_RANGE)
8159 /* Arbitrarily choose the right or left gap. */
8160 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8161 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8162 build_int_cst (TREE_TYPE (vr1min), 1));
8163 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8164 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8165 build_int_cst (TREE_TYPE (vr1max), 1));
8166 else
8167 goto give_up;
8169 else if (*vr0type == VR_RANGE
8170 && vr1type == VR_ANTI_RANGE)
8171 /* The result covers everything. */
8172 goto give_up;
8173 else
8174 gcc_unreachable ();
8176 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8177 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8179 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8180 if (*vr0type == VR_RANGE
8181 && vr1type == VR_RANGE)
8183 *vr0type = vr1type;
8184 *vr0min = vr1min;
8185 *vr0max = vr1max;
8187 else if (*vr0type == VR_ANTI_RANGE
8188 && vr1type == VR_ANTI_RANGE)
8190 else if (*vr0type == VR_RANGE
8191 && vr1type == VR_ANTI_RANGE)
8193 *vr0type = VR_ANTI_RANGE;
8194 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8196 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8197 build_int_cst (TREE_TYPE (*vr0min), 1));
8198 *vr0min = vr1min;
8200 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8202 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8203 build_int_cst (TREE_TYPE (*vr0max), 1));
8204 *vr0max = vr1max;
8206 else
8207 goto give_up;
8209 else if (*vr0type == VR_ANTI_RANGE
8210 && vr1type == VR_RANGE)
8211 /* The result covers everything. */
8212 goto give_up;
8213 else
8214 gcc_unreachable ();
8216 else if ((operand_less_p (vr1min, *vr0max) == 1
8217 || operand_equal_p (vr1min, *vr0max, 0))
8218 && operand_less_p (*vr0min, vr1min) == 1
8219 && operand_less_p (*vr0max, vr1max) == 1)
8221 /* [ ( ] ) or [ ]( ) */
8222 if (*vr0type == VR_RANGE
8223 && vr1type == VR_RANGE)
8224 *vr0max = vr1max;
8225 else if (*vr0type == VR_ANTI_RANGE
8226 && vr1type == VR_ANTI_RANGE)
8227 *vr0min = vr1min;
8228 else if (*vr0type == VR_ANTI_RANGE
8229 && vr1type == VR_RANGE)
8231 if (TREE_CODE (vr1min) == INTEGER_CST)
8232 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8233 build_int_cst (TREE_TYPE (vr1min), 1));
8234 else
8235 goto give_up;
8237 else if (*vr0type == VR_RANGE
8238 && vr1type == VR_ANTI_RANGE)
8240 if (TREE_CODE (*vr0max) == INTEGER_CST)
8242 *vr0type = vr1type;
8243 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8244 build_int_cst (TREE_TYPE (*vr0max), 1));
8245 *vr0max = vr1max;
8247 else
8248 goto give_up;
8250 else
8251 gcc_unreachable ();
8253 else if ((operand_less_p (*vr0min, vr1max) == 1
8254 || operand_equal_p (*vr0min, vr1max, 0))
8255 && operand_less_p (vr1min, *vr0min) == 1
8256 && operand_less_p (vr1max, *vr0max) == 1)
8258 /* ( [ ) ] or ( )[ ] */
8259 if (*vr0type == VR_RANGE
8260 && vr1type == VR_RANGE)
8261 *vr0min = vr1min;
8262 else if (*vr0type == VR_ANTI_RANGE
8263 && vr1type == VR_ANTI_RANGE)
8264 *vr0max = vr1max;
8265 else if (*vr0type == VR_ANTI_RANGE
8266 && vr1type == VR_RANGE)
8268 if (TREE_CODE (vr1max) == INTEGER_CST)
8269 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8270 build_int_cst (TREE_TYPE (vr1max), 1));
8271 else
8272 goto give_up;
8274 else if (*vr0type == VR_RANGE
8275 && vr1type == VR_ANTI_RANGE)
8277 if (TREE_CODE (*vr0min) == INTEGER_CST)
8279 *vr0type = vr1type;
8280 *vr0min = vr1min;
8281 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8282 build_int_cst (TREE_TYPE (*vr0min), 1));
8284 else
8285 goto give_up;
8287 else
8288 gcc_unreachable ();
8290 else
8291 goto give_up;
8293 return;
8295 give_up:
8296 *vr0type = VR_VARYING;
8297 *vr0min = NULL_TREE;
8298 *vr0max = NULL_TREE;
8301 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8302 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8303 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8304 possible such range. The resulting range is not canonicalized. */
8306 static void
8307 intersect_ranges (enum value_range_type *vr0type,
8308 tree *vr0min, tree *vr0max,
8309 enum value_range_type vr1type,
8310 tree vr1min, tree vr1max)
8312 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8313 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8315 /* [] is vr0, () is vr1 in the following classification comments. */
8316 if (mineq && maxeq)
8318 /* [( )] */
8319 if (*vr0type == vr1type)
8320 /* Nothing to do for equal ranges. */
8322 else if ((*vr0type == VR_RANGE
8323 && vr1type == VR_ANTI_RANGE)
8324 || (*vr0type == VR_ANTI_RANGE
8325 && vr1type == VR_RANGE))
8327 /* For anti-range with range intersection the result is empty. */
8328 *vr0type = VR_UNDEFINED;
8329 *vr0min = NULL_TREE;
8330 *vr0max = NULL_TREE;
8332 else
8333 gcc_unreachable ();
8335 else if (operand_less_p (*vr0max, vr1min) == 1
8336 || operand_less_p (vr1max, *vr0min) == 1)
8338 /* [ ] ( ) or ( ) [ ]
8339 If the ranges have an empty intersection, the result of the
8340 intersect operation is the range for intersecting an
8341 anti-range with a range or empty when intersecting two ranges. */
8342 if (*vr0type == VR_RANGE
8343 && vr1type == VR_ANTI_RANGE)
8345 else if (*vr0type == VR_ANTI_RANGE
8346 && vr1type == VR_RANGE)
8348 *vr0type = vr1type;
8349 *vr0min = vr1min;
8350 *vr0max = vr1max;
8352 else if (*vr0type == VR_RANGE
8353 && vr1type == VR_RANGE)
8355 *vr0type = VR_UNDEFINED;
8356 *vr0min = NULL_TREE;
8357 *vr0max = NULL_TREE;
8359 else if (*vr0type == VR_ANTI_RANGE
8360 && vr1type == VR_ANTI_RANGE)
8362 /* If the anti-ranges are adjacent to each other merge them. */
8363 if (TREE_CODE (*vr0max) == INTEGER_CST
8364 && TREE_CODE (vr1min) == INTEGER_CST
8365 && operand_less_p (*vr0max, vr1min) == 1
8366 && integer_onep (int_const_binop (MINUS_EXPR,
8367 vr1min, *vr0max)))
8368 *vr0max = vr1max;
8369 else if (TREE_CODE (vr1max) == INTEGER_CST
8370 && TREE_CODE (*vr0min) == INTEGER_CST
8371 && operand_less_p (vr1max, *vr0min) == 1
8372 && integer_onep (int_const_binop (MINUS_EXPR,
8373 *vr0min, vr1max)))
8374 *vr0min = vr1min;
8375 /* Else arbitrarily take VR0. */
8378 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8379 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8381 /* [ ( ) ] or [( ) ] or [ ( )] */
8382 if (*vr0type == VR_RANGE
8383 && vr1type == VR_RANGE)
8385 /* If both are ranges the result is the inner one. */
8386 *vr0type = vr1type;
8387 *vr0min = vr1min;
8388 *vr0max = vr1max;
8390 else if (*vr0type == VR_RANGE
8391 && vr1type == VR_ANTI_RANGE)
8393 /* Choose the right gap if the left one is empty. */
8394 if (mineq)
8396 if (TREE_CODE (vr1max) == INTEGER_CST)
8397 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8398 build_int_cst (TREE_TYPE (vr1max), 1));
8399 else
8400 *vr0min = vr1max;
8402 /* Choose the left gap if the right one is empty. */
8403 else if (maxeq)
8405 if (TREE_CODE (vr1min) == INTEGER_CST)
8406 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8407 build_int_cst (TREE_TYPE (vr1min), 1));
8408 else
8409 *vr0max = vr1min;
8411 /* Choose the anti-range if the range is effectively varying. */
8412 else if (vrp_val_is_min (*vr0min)
8413 && vrp_val_is_max (*vr0max))
8415 *vr0type = vr1type;
8416 *vr0min = vr1min;
8417 *vr0max = vr1max;
8419 /* Else choose the range. */
8421 else if (*vr0type == VR_ANTI_RANGE
8422 && vr1type == VR_ANTI_RANGE)
8423 /* If both are anti-ranges the result is the outer one. */
8425 else if (*vr0type == VR_ANTI_RANGE
8426 && vr1type == VR_RANGE)
8428 /* The intersection is empty. */
8429 *vr0type = VR_UNDEFINED;
8430 *vr0min = NULL_TREE;
8431 *vr0max = NULL_TREE;
8433 else
8434 gcc_unreachable ();
8436 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8437 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8439 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8440 if (*vr0type == VR_RANGE
8441 && vr1type == VR_RANGE)
8442 /* Choose the inner range. */
8444 else if (*vr0type == VR_ANTI_RANGE
8445 && vr1type == VR_RANGE)
8447 /* Choose the right gap if the left is empty. */
8448 if (mineq)
8450 *vr0type = VR_RANGE;
8451 if (TREE_CODE (*vr0max) == INTEGER_CST)
8452 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8453 build_int_cst (TREE_TYPE (*vr0max), 1));
8454 else
8455 *vr0min = *vr0max;
8456 *vr0max = vr1max;
8458 /* Choose the left gap if the right is empty. */
8459 else if (maxeq)
8461 *vr0type = VR_RANGE;
8462 if (TREE_CODE (*vr0min) == INTEGER_CST)
8463 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8464 build_int_cst (TREE_TYPE (*vr0min), 1));
8465 else
8466 *vr0max = *vr0min;
8467 *vr0min = vr1min;
8469 /* Choose the anti-range if the range is effectively varying. */
8470 else if (vrp_val_is_min (vr1min)
8471 && vrp_val_is_max (vr1max))
8473 /* Else choose the range. */
8474 else
8476 *vr0type = vr1type;
8477 *vr0min = vr1min;
8478 *vr0max = vr1max;
8481 else if (*vr0type == VR_ANTI_RANGE
8482 && vr1type == VR_ANTI_RANGE)
8484 /* If both are anti-ranges the result is the outer one. */
8485 *vr0type = vr1type;
8486 *vr0min = vr1min;
8487 *vr0max = vr1max;
8489 else if (vr1type == VR_ANTI_RANGE
8490 && *vr0type == VR_RANGE)
8492 /* The intersection is empty. */
8493 *vr0type = VR_UNDEFINED;
8494 *vr0min = NULL_TREE;
8495 *vr0max = NULL_TREE;
8497 else
8498 gcc_unreachable ();
8500 else if ((operand_less_p (vr1min, *vr0max) == 1
8501 || operand_equal_p (vr1min, *vr0max, 0))
8502 && operand_less_p (*vr0min, vr1min) == 1)
8504 /* [ ( ] ) or [ ]( ) */
8505 if (*vr0type == VR_ANTI_RANGE
8506 && vr1type == VR_ANTI_RANGE)
8507 *vr0max = vr1max;
8508 else if (*vr0type == VR_RANGE
8509 && vr1type == VR_RANGE)
8510 *vr0min = vr1min;
8511 else if (*vr0type == VR_RANGE
8512 && vr1type == VR_ANTI_RANGE)
8514 if (TREE_CODE (vr1min) == INTEGER_CST)
8515 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8516 build_int_cst (TREE_TYPE (vr1min), 1));
8517 else
8518 *vr0max = vr1min;
8520 else if (*vr0type == VR_ANTI_RANGE
8521 && vr1type == VR_RANGE)
8523 *vr0type = VR_RANGE;
8524 if (TREE_CODE (*vr0max) == INTEGER_CST)
8525 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8526 build_int_cst (TREE_TYPE (*vr0max), 1));
8527 else
8528 *vr0min = *vr0max;
8529 *vr0max = vr1max;
8531 else
8532 gcc_unreachable ();
8534 else if ((operand_less_p (*vr0min, vr1max) == 1
8535 || operand_equal_p (*vr0min, vr1max, 0))
8536 && operand_less_p (vr1min, *vr0min) == 1)
8538 /* ( [ ) ] or ( )[ ] */
8539 if (*vr0type == VR_ANTI_RANGE
8540 && vr1type == VR_ANTI_RANGE)
8541 *vr0min = vr1min;
8542 else if (*vr0type == VR_RANGE
8543 && vr1type == VR_RANGE)
8544 *vr0max = vr1max;
8545 else if (*vr0type == VR_RANGE
8546 && vr1type == VR_ANTI_RANGE)
8548 if (TREE_CODE (vr1max) == INTEGER_CST)
8549 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8550 build_int_cst (TREE_TYPE (vr1max), 1));
8551 else
8552 *vr0min = vr1max;
8554 else if (*vr0type == VR_ANTI_RANGE
8555 && vr1type == VR_RANGE)
8557 *vr0type = VR_RANGE;
8558 if (TREE_CODE (*vr0min) == INTEGER_CST)
8559 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8560 build_int_cst (TREE_TYPE (*vr0min), 1));
8561 else
8562 *vr0max = *vr0min;
8563 *vr0min = vr1min;
8565 else
8566 gcc_unreachable ();
8569 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8570 result for the intersection. That's always a conservative
8571 correct estimate. */
8573 return;
8577 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8578 in *VR0. This may not be the smallest possible such range. */
8580 static void
8581 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8583 value_range_t saved;
8585 /* If either range is VR_VARYING the other one wins. */
8586 if (vr1->type == VR_VARYING)
8587 return;
8588 if (vr0->type == VR_VARYING)
8590 copy_value_range (vr0, vr1);
8591 return;
8594 /* When either range is VR_UNDEFINED the resulting range is
8595 VR_UNDEFINED, too. */
8596 if (vr0->type == VR_UNDEFINED)
8597 return;
8598 if (vr1->type == VR_UNDEFINED)
8600 set_value_range_to_undefined (vr0);
8601 return;
8604 /* Save the original vr0 so we can return it as conservative intersection
8605 result when our worker turns things to varying. */
8606 saved = *vr0;
8607 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8608 vr1->type, vr1->min, vr1->max);
8609 /* Make sure to canonicalize the result though as the inversion of a
8610 VR_RANGE can still be a VR_RANGE. */
8611 set_and_canonicalize_value_range (vr0, vr0->type,
8612 vr0->min, vr0->max, vr0->equiv);
8613 /* If that failed, use the saved original VR0. */
8614 if (vr0->type == VR_VARYING)
8616 *vr0 = saved;
8617 return;
8619 /* If the result is VR_UNDEFINED there is no need to mess with
8620 the equivalencies. */
8621 if (vr0->type == VR_UNDEFINED)
8622 return;
8624 /* The resulting set of equivalences for range intersection is the union of
8625 the two sets. */
8626 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8627 bitmap_ior_into (vr0->equiv, vr1->equiv);
8628 else if (vr1->equiv && !vr0->equiv)
8629 bitmap_copy (vr0->equiv, vr1->equiv);
8632 static void
8633 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8635 if (dump_file && (dump_flags & TDF_DETAILS))
8637 fprintf (dump_file, "Intersecting\n ");
8638 dump_value_range (dump_file, vr0);
8639 fprintf (dump_file, "\nand\n ");
8640 dump_value_range (dump_file, vr1);
8641 fprintf (dump_file, "\n");
8643 vrp_intersect_ranges_1 (vr0, vr1);
8644 if (dump_file && (dump_flags & TDF_DETAILS))
8646 fprintf (dump_file, "to\n ");
8647 dump_value_range (dump_file, vr0);
8648 fprintf (dump_file, "\n");
8652 /* Meet operation for value ranges. Given two value ranges VR0 and
8653 VR1, store in VR0 a range that contains both VR0 and VR1. This
8654 may not be the smallest possible such range. */
8656 static void
8657 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8659 value_range_t saved;
8661 if (vr0->type == VR_UNDEFINED)
8663 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8664 return;
8667 if (vr1->type == VR_UNDEFINED)
8669 /* VR0 already has the resulting range. */
8670 return;
8673 if (vr0->type == VR_VARYING)
8675 /* Nothing to do. VR0 already has the resulting range. */
8676 return;
8679 if (vr1->type == VR_VARYING)
8681 set_value_range_to_varying (vr0);
8682 return;
8685 saved = *vr0;
8686 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8687 vr1->type, vr1->min, vr1->max);
8688 if (vr0->type == VR_VARYING)
8690 /* Failed to find an efficient meet. Before giving up and setting
8691 the result to VARYING, see if we can at least derive a useful
8692 anti-range. FIXME, all this nonsense about distinguishing
8693 anti-ranges from ranges is necessary because of the odd
8694 semantics of range_includes_zero_p and friends. */
8695 if (((saved.type == VR_RANGE
8696 && range_includes_zero_p (saved.min, saved.max) == 0)
8697 || (saved.type == VR_ANTI_RANGE
8698 && range_includes_zero_p (saved.min, saved.max) == 1))
8699 && ((vr1->type == VR_RANGE
8700 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8701 || (vr1->type == VR_ANTI_RANGE
8702 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8704 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8706 /* Since this meet operation did not result from the meeting of
8707 two equivalent names, VR0 cannot have any equivalences. */
8708 if (vr0->equiv)
8709 bitmap_clear (vr0->equiv);
8710 return;
8713 set_value_range_to_varying (vr0);
8714 return;
8716 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8717 vr0->equiv);
8718 if (vr0->type == VR_VARYING)
8719 return;
8721 /* The resulting set of equivalences is always the intersection of
8722 the two sets. */
8723 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8724 bitmap_and_into (vr0->equiv, vr1->equiv);
8725 else if (vr0->equiv && !vr1->equiv)
8726 bitmap_clear (vr0->equiv);
8729 static void
8730 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8732 if (dump_file && (dump_flags & TDF_DETAILS))
8734 fprintf (dump_file, "Meeting\n ");
8735 dump_value_range (dump_file, vr0);
8736 fprintf (dump_file, "\nand\n ");
8737 dump_value_range (dump_file, vr1);
8738 fprintf (dump_file, "\n");
8740 vrp_meet_1 (vr0, vr1);
8741 if (dump_file && (dump_flags & TDF_DETAILS))
8743 fprintf (dump_file, "to\n ");
8744 dump_value_range (dump_file, vr0);
8745 fprintf (dump_file, "\n");
8750 /* Visit all arguments for PHI node PHI that flow through executable
8751 edges. If a valid value range can be derived from all the incoming
8752 value ranges, set a new range for the LHS of PHI. */
8754 static enum ssa_prop_result
8755 vrp_visit_phi_node (gphi *phi)
8757 size_t i;
8758 tree lhs = PHI_RESULT (phi);
8759 value_range_t *lhs_vr = get_value_range (lhs);
8760 value_range_t vr_result = VR_INITIALIZER;
8761 bool first = true;
8762 int edges, old_edges;
8763 struct loop *l;
8765 if (dump_file && (dump_flags & TDF_DETAILS))
8767 fprintf (dump_file, "\nVisiting PHI node: ");
8768 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8771 edges = 0;
8772 for (i = 0; i < gimple_phi_num_args (phi); i++)
8774 edge e = gimple_phi_arg_edge (phi, i);
8776 if (dump_file && (dump_flags & TDF_DETAILS))
8778 fprintf (dump_file,
8779 " Argument #%d (%d -> %d %sexecutable)\n",
8780 (int) i, e->src->index, e->dest->index,
8781 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8784 if (e->flags & EDGE_EXECUTABLE)
8786 tree arg = PHI_ARG_DEF (phi, i);
8787 value_range_t vr_arg;
8789 ++edges;
8791 if (TREE_CODE (arg) == SSA_NAME)
8793 vr_arg = *(get_value_range (arg));
8794 /* Do not allow equivalences or symbolic ranges to leak in from
8795 backedges. That creates invalid equivalencies.
8796 See PR53465 and PR54767. */
8797 if (e->flags & EDGE_DFS_BACK)
8799 if (vr_arg.type == VR_RANGE
8800 || vr_arg.type == VR_ANTI_RANGE)
8802 vr_arg.equiv = NULL;
8803 if (symbolic_range_p (&vr_arg))
8805 vr_arg.type = VR_VARYING;
8806 vr_arg.min = NULL_TREE;
8807 vr_arg.max = NULL_TREE;
8811 else
8813 /* If the non-backedge arguments range is VR_VARYING then
8814 we can still try recording a simple equivalence. */
8815 if (vr_arg.type == VR_VARYING)
8817 vr_arg.type = VR_RANGE;
8818 vr_arg.min = arg;
8819 vr_arg.max = arg;
8820 vr_arg.equiv = NULL;
8824 else
8826 if (TREE_OVERFLOW_P (arg))
8827 arg = drop_tree_overflow (arg);
8829 vr_arg.type = VR_RANGE;
8830 vr_arg.min = arg;
8831 vr_arg.max = arg;
8832 vr_arg.equiv = NULL;
8835 if (dump_file && (dump_flags & TDF_DETAILS))
8837 fprintf (dump_file, "\t");
8838 print_generic_expr (dump_file, arg, dump_flags);
8839 fprintf (dump_file, ": ");
8840 dump_value_range (dump_file, &vr_arg);
8841 fprintf (dump_file, "\n");
8844 if (first)
8845 copy_value_range (&vr_result, &vr_arg);
8846 else
8847 vrp_meet (&vr_result, &vr_arg);
8848 first = false;
8850 if (vr_result.type == VR_VARYING)
8851 break;
8855 if (vr_result.type == VR_VARYING)
8856 goto varying;
8857 else if (vr_result.type == VR_UNDEFINED)
8858 goto update_range;
8860 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8861 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8863 /* To prevent infinite iterations in the algorithm, derive ranges
8864 when the new value is slightly bigger or smaller than the
8865 previous one. We don't do this if we have seen a new executable
8866 edge; this helps us avoid an overflow infinity for conditionals
8867 which are not in a loop. If the old value-range was VR_UNDEFINED
8868 use the updated range and iterate one more time. */
8869 if (edges > 0
8870 && gimple_phi_num_args (phi) > 1
8871 && edges == old_edges
8872 && lhs_vr->type != VR_UNDEFINED)
8874 /* Compare old and new ranges, fall back to varying if the
8875 values are not comparable. */
8876 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8877 if (cmp_min == -2)
8878 goto varying;
8879 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8880 if (cmp_max == -2)
8881 goto varying;
8883 /* For non VR_RANGE or for pointers fall back to varying if
8884 the range changed. */
8885 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8886 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8887 && (cmp_min != 0 || cmp_max != 0))
8888 goto varying;
8890 /* If the new minimum is larger than than the previous one
8891 retain the old value. If the new minimum value is smaller
8892 than the previous one and not -INF go all the way to -INF + 1.
8893 In the first case, to avoid infinite bouncing between different
8894 minimums, and in the other case to avoid iterating millions of
8895 times to reach -INF. Going to -INF + 1 also lets the following
8896 iteration compute whether there will be any overflow, at the
8897 expense of one additional iteration. */
8898 if (cmp_min < 0)
8899 vr_result.min = lhs_vr->min;
8900 else if (cmp_min > 0
8901 && !vrp_val_is_min (vr_result.min))
8902 vr_result.min
8903 = int_const_binop (PLUS_EXPR,
8904 vrp_val_min (TREE_TYPE (vr_result.min)),
8905 build_int_cst (TREE_TYPE (vr_result.min), 1));
8907 /* Similarly for the maximum value. */
8908 if (cmp_max > 0)
8909 vr_result.max = lhs_vr->max;
8910 else if (cmp_max < 0
8911 && !vrp_val_is_max (vr_result.max))
8912 vr_result.max
8913 = int_const_binop (MINUS_EXPR,
8914 vrp_val_max (TREE_TYPE (vr_result.min)),
8915 build_int_cst (TREE_TYPE (vr_result.min), 1));
8917 /* If we dropped either bound to +-INF then if this is a loop
8918 PHI node SCEV may known more about its value-range. */
8919 if ((cmp_min > 0 || cmp_min < 0
8920 || cmp_max < 0 || cmp_max > 0)
8921 && (l = loop_containing_stmt (phi))
8922 && l->header == gimple_bb (phi))
8923 adjust_range_with_scev (&vr_result, l, phi, lhs);
8925 /* If we will end up with a (-INF, +INF) range, set it to
8926 VARYING. Same if the previous max value was invalid for
8927 the type and we end up with vr_result.min > vr_result.max. */
8928 if ((vrp_val_is_max (vr_result.max)
8929 && vrp_val_is_min (vr_result.min))
8930 || compare_values (vr_result.min,
8931 vr_result.max) > 0)
8932 goto varying;
8935 /* If the new range is different than the previous value, keep
8936 iterating. */
8937 update_range:
8938 if (update_value_range (lhs, &vr_result))
8940 if (dump_file && (dump_flags & TDF_DETAILS))
8942 fprintf (dump_file, "Found new range for ");
8943 print_generic_expr (dump_file, lhs, 0);
8944 fprintf (dump_file, ": ");
8945 dump_value_range (dump_file, &vr_result);
8946 fprintf (dump_file, "\n");
8949 if (vr_result.type == VR_VARYING)
8950 return SSA_PROP_VARYING;
8952 return SSA_PROP_INTERESTING;
8955 /* Nothing changed, don't add outgoing edges. */
8956 return SSA_PROP_NOT_INTERESTING;
8958 /* No match found. Set the LHS to VARYING. */
8959 varying:
8960 set_value_range_to_varying (lhs_vr);
8961 return SSA_PROP_VARYING;
8964 /* Simplify boolean operations if the source is known
8965 to be already a boolean. */
8966 static bool
8967 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8969 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8970 tree lhs, op0, op1;
8971 bool need_conversion;
8973 /* We handle only !=/== case here. */
8974 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8976 op0 = gimple_assign_rhs1 (stmt);
8977 if (!op_with_boolean_value_range_p (op0))
8978 return false;
8980 op1 = gimple_assign_rhs2 (stmt);
8981 if (!op_with_boolean_value_range_p (op1))
8982 return false;
8984 /* Reduce number of cases to handle to NE_EXPR. As there is no
8985 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8986 if (rhs_code == EQ_EXPR)
8988 if (TREE_CODE (op1) == INTEGER_CST)
8989 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8990 build_int_cst (TREE_TYPE (op1), 1));
8991 else
8992 return false;
8995 lhs = gimple_assign_lhs (stmt);
8996 need_conversion
8997 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8999 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9000 if (need_conversion
9001 && !TYPE_UNSIGNED (TREE_TYPE (op0))
9002 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
9003 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
9004 return false;
9006 /* For A != 0 we can substitute A itself. */
9007 if (integer_zerop (op1))
9008 gimple_assign_set_rhs_with_ops (gsi,
9009 need_conversion
9010 ? NOP_EXPR : TREE_CODE (op0), op0);
9011 /* For A != B we substitute A ^ B. Either with conversion. */
9012 else if (need_conversion)
9014 tree tem = make_ssa_name (TREE_TYPE (op0));
9015 gassign *newop
9016 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
9017 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
9018 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9020 /* Or without. */
9021 else
9022 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9023 update_stmt (gsi_stmt (*gsi));
9025 return true;
9028 /* Simplify a division or modulo operator to a right shift or
9029 bitwise and if the first operand is unsigned or is greater
9030 than zero and the second operand is an exact power of two.
9031 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9032 into just op0 if op0's range is known to be a subset of
9033 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9034 modulo. */
9036 static bool
9037 simplify_div_or_mod_using_ranges (gimple stmt)
9039 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9040 tree val = NULL;
9041 tree op0 = gimple_assign_rhs1 (stmt);
9042 tree op1 = gimple_assign_rhs2 (stmt);
9043 value_range_t *vr = get_value_range (op0);
9045 if (rhs_code == TRUNC_MOD_EXPR
9046 && TREE_CODE (op1) == INTEGER_CST
9047 && tree_int_cst_sgn (op1) == 1
9048 && range_int_cst_p (vr)
9049 && tree_int_cst_lt (vr->max, op1))
9051 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9052 || tree_int_cst_sgn (vr->min) >= 0
9053 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9054 vr->min))
9056 /* If op0 already has the range op0 % op1 has,
9057 then TRUNC_MOD_EXPR won't change anything. */
9058 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9059 gimple_assign_set_rhs_from_tree (&gsi, op0);
9060 update_stmt (stmt);
9061 return true;
9065 if (!integer_pow2p (op1))
9066 return false;
9068 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9070 val = integer_one_node;
9072 else
9074 bool sop = false;
9076 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9078 if (val
9079 && sop
9080 && integer_onep (val)
9081 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9083 location_t location;
9085 if (!gimple_has_location (stmt))
9086 location = input_location;
9087 else
9088 location = gimple_location (stmt);
9089 warning_at (location, OPT_Wstrict_overflow,
9090 "assuming signed overflow does not occur when "
9091 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9095 if (val && integer_onep (val))
9097 tree t;
9099 if (rhs_code == TRUNC_DIV_EXPR)
9101 t = build_int_cst (integer_type_node, tree_log2 (op1));
9102 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9103 gimple_assign_set_rhs1 (stmt, op0);
9104 gimple_assign_set_rhs2 (stmt, t);
9106 else
9108 t = build_int_cst (TREE_TYPE (op1), 1);
9109 t = int_const_binop (MINUS_EXPR, op1, t);
9110 t = fold_convert (TREE_TYPE (op0), t);
9112 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9113 gimple_assign_set_rhs1 (stmt, op0);
9114 gimple_assign_set_rhs2 (stmt, t);
9117 update_stmt (stmt);
9118 return true;
9121 return false;
9124 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9125 ABS_EXPR. If the operand is <= 0, then simplify the
9126 ABS_EXPR into a NEGATE_EXPR. */
9128 static bool
9129 simplify_abs_using_ranges (gimple stmt)
9131 tree val = NULL;
9132 tree op = gimple_assign_rhs1 (stmt);
9133 tree type = TREE_TYPE (op);
9134 value_range_t *vr = get_value_range (op);
9136 if (TYPE_UNSIGNED (type))
9138 val = integer_zero_node;
9140 else if (vr)
9142 bool sop = false;
9144 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9145 if (!val)
9147 sop = false;
9148 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
9149 &sop);
9151 if (val)
9153 if (integer_zerop (val))
9154 val = integer_one_node;
9155 else if (integer_onep (val))
9156 val = integer_zero_node;
9160 if (val
9161 && (integer_onep (val) || integer_zerop (val)))
9163 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9165 location_t location;
9167 if (!gimple_has_location (stmt))
9168 location = input_location;
9169 else
9170 location = gimple_location (stmt);
9171 warning_at (location, OPT_Wstrict_overflow,
9172 "assuming signed overflow does not occur when "
9173 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9176 gimple_assign_set_rhs1 (stmt, op);
9177 if (integer_onep (val))
9178 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9179 else
9180 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9181 update_stmt (stmt);
9182 return true;
9186 return false;
9189 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9190 If all the bits that are being cleared by & are already
9191 known to be zero from VR, or all the bits that are being
9192 set by | are already known to be one from VR, the bit
9193 operation is redundant. */
9195 static bool
9196 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9198 tree op0 = gimple_assign_rhs1 (stmt);
9199 tree op1 = gimple_assign_rhs2 (stmt);
9200 tree op = NULL_TREE;
9201 value_range_t vr0 = VR_INITIALIZER;
9202 value_range_t vr1 = VR_INITIALIZER;
9203 wide_int may_be_nonzero0, may_be_nonzero1;
9204 wide_int must_be_nonzero0, must_be_nonzero1;
9205 wide_int mask;
9207 if (TREE_CODE (op0) == SSA_NAME)
9208 vr0 = *(get_value_range (op0));
9209 else if (is_gimple_min_invariant (op0))
9210 set_value_range_to_value (&vr0, op0, NULL);
9211 else
9212 return false;
9214 if (TREE_CODE (op1) == SSA_NAME)
9215 vr1 = *(get_value_range (op1));
9216 else if (is_gimple_min_invariant (op1))
9217 set_value_range_to_value (&vr1, op1, NULL);
9218 else
9219 return false;
9221 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9222 &must_be_nonzero0))
9223 return false;
9224 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9225 &must_be_nonzero1))
9226 return false;
9228 switch (gimple_assign_rhs_code (stmt))
9230 case BIT_AND_EXPR:
9231 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9232 if (mask == 0)
9234 op = op0;
9235 break;
9237 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9238 if (mask == 0)
9240 op = op1;
9241 break;
9243 break;
9244 case BIT_IOR_EXPR:
9245 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9246 if (mask == 0)
9248 op = op1;
9249 break;
9251 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9252 if (mask == 0)
9254 op = op0;
9255 break;
9257 break;
9258 default:
9259 gcc_unreachable ();
9262 if (op == NULL_TREE)
9263 return false;
9265 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9266 update_stmt (gsi_stmt (*gsi));
9267 return true;
9270 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9271 a known value range VR.
9273 If there is one and only one value which will satisfy the
9274 conditional, then return that value. Else return NULL.
9276 If signed overflow must be undefined for the value to satisfy
9277 the conditional, then set *STRICT_OVERFLOW_P to true. */
9279 static tree
9280 test_for_singularity (enum tree_code cond_code, tree op0,
9281 tree op1, value_range_t *vr,
9282 bool *strict_overflow_p)
9284 tree min = NULL;
9285 tree max = NULL;
9287 /* Extract minimum/maximum values which satisfy the
9288 the conditional as it was written. */
9289 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9291 /* This should not be negative infinity; there is no overflow
9292 here. */
9293 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9295 max = op1;
9296 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9298 tree one = build_int_cst (TREE_TYPE (op0), 1);
9299 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9300 if (EXPR_P (max))
9301 TREE_NO_WARNING (max) = 1;
9304 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9306 /* This should not be positive infinity; there is no overflow
9307 here. */
9308 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9310 min = op1;
9311 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9313 tree one = build_int_cst (TREE_TYPE (op0), 1);
9314 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9315 if (EXPR_P (min))
9316 TREE_NO_WARNING (min) = 1;
9320 /* Now refine the minimum and maximum values using any
9321 value range information we have for op0. */
9322 if (min && max)
9324 if (compare_values (vr->min, min) == 1)
9325 min = vr->min;
9326 if (compare_values (vr->max, max) == -1)
9327 max = vr->max;
9329 /* If the new min/max values have converged to a single value,
9330 then there is only one value which can satisfy the condition,
9331 return that value. */
9332 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9334 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9335 && is_overflow_infinity (vr->max))
9336 *strict_overflow_p = true;
9337 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9338 && is_overflow_infinity (vr->min))
9339 *strict_overflow_p = true;
9341 return min;
9344 return NULL;
9347 /* Return whether the value range *VR fits in an integer type specified
9348 by PRECISION and UNSIGNED_P. */
9350 static bool
9351 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9353 tree src_type;
9354 unsigned src_precision;
9355 widest_int tem;
9356 signop src_sgn;
9358 /* We can only handle integral and pointer types. */
9359 src_type = TREE_TYPE (vr->min);
9360 if (!INTEGRAL_TYPE_P (src_type)
9361 && !POINTER_TYPE_P (src_type))
9362 return false;
9364 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9365 and so is an identity transform. */
9366 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9367 src_sgn = TYPE_SIGN (src_type);
9368 if ((src_precision < dest_precision
9369 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9370 || (src_precision == dest_precision && src_sgn == dest_sgn))
9371 return true;
9373 /* Now we can only handle ranges with constant bounds. */
9374 if (vr->type != VR_RANGE
9375 || TREE_CODE (vr->min) != INTEGER_CST
9376 || TREE_CODE (vr->max) != INTEGER_CST)
9377 return false;
9379 /* For sign changes, the MSB of the wide_int has to be clear.
9380 An unsigned value with its MSB set cannot be represented by
9381 a signed wide_int, while a negative value cannot be represented
9382 by an unsigned wide_int. */
9383 if (src_sgn != dest_sgn
9384 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9385 return false;
9387 /* Then we can perform the conversion on both ends and compare
9388 the result for equality. */
9389 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9390 if (tem != wi::to_widest (vr->min))
9391 return false;
9392 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9393 if (tem != wi::to_widest (vr->max))
9394 return false;
9396 return true;
9399 /* Simplify a conditional using a relational operator to an equality
9400 test if the range information indicates only one value can satisfy
9401 the original conditional. */
9403 static bool
9404 simplify_cond_using_ranges (gcond *stmt)
9406 tree op0 = gimple_cond_lhs (stmt);
9407 tree op1 = gimple_cond_rhs (stmt);
9408 enum tree_code cond_code = gimple_cond_code (stmt);
9410 if (cond_code != NE_EXPR
9411 && cond_code != EQ_EXPR
9412 && TREE_CODE (op0) == SSA_NAME
9413 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9414 && is_gimple_min_invariant (op1))
9416 value_range_t *vr = get_value_range (op0);
9418 /* If we have range information for OP0, then we might be
9419 able to simplify this conditional. */
9420 if (vr->type == VR_RANGE)
9422 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9423 bool sop = false;
9424 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9426 if (new_tree
9427 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9429 if (dump_file)
9431 fprintf (dump_file, "Simplified relational ");
9432 print_gimple_stmt (dump_file, stmt, 0, 0);
9433 fprintf (dump_file, " into ");
9436 gimple_cond_set_code (stmt, EQ_EXPR);
9437 gimple_cond_set_lhs (stmt, op0);
9438 gimple_cond_set_rhs (stmt, new_tree);
9440 update_stmt (stmt);
9442 if (dump_file)
9444 print_gimple_stmt (dump_file, stmt, 0, 0);
9445 fprintf (dump_file, "\n");
9448 if (sop && issue_strict_overflow_warning (wc))
9450 location_t location = input_location;
9451 if (gimple_has_location (stmt))
9452 location = gimple_location (stmt);
9454 warning_at (location, OPT_Wstrict_overflow,
9455 "assuming signed overflow does not occur when "
9456 "simplifying conditional");
9459 return true;
9462 /* Try again after inverting the condition. We only deal
9463 with integral types here, so no need to worry about
9464 issues with inverting FP comparisons. */
9465 sop = false;
9466 new_tree = test_for_singularity
9467 (invert_tree_comparison (cond_code, false),
9468 op0, op1, vr, &sop);
9470 if (new_tree
9471 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9473 if (dump_file)
9475 fprintf (dump_file, "Simplified relational ");
9476 print_gimple_stmt (dump_file, stmt, 0, 0);
9477 fprintf (dump_file, " into ");
9480 gimple_cond_set_code (stmt, NE_EXPR);
9481 gimple_cond_set_lhs (stmt, op0);
9482 gimple_cond_set_rhs (stmt, new_tree);
9484 update_stmt (stmt);
9486 if (dump_file)
9488 print_gimple_stmt (dump_file, stmt, 0, 0);
9489 fprintf (dump_file, "\n");
9492 if (sop && issue_strict_overflow_warning (wc))
9494 location_t location = input_location;
9495 if (gimple_has_location (stmt))
9496 location = gimple_location (stmt);
9498 warning_at (location, OPT_Wstrict_overflow,
9499 "assuming signed overflow does not occur when "
9500 "simplifying conditional");
9503 return true;
9508 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9509 see if OP0 was set by a type conversion where the source of
9510 the conversion is another SSA_NAME with a range that fits
9511 into the range of OP0's type.
9513 If so, the conversion is redundant as the earlier SSA_NAME can be
9514 used for the comparison directly if we just massage the constant in the
9515 comparison. */
9516 if (TREE_CODE (op0) == SSA_NAME
9517 && TREE_CODE (op1) == INTEGER_CST)
9519 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9520 tree innerop;
9522 if (!is_gimple_assign (def_stmt)
9523 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9524 return false;
9526 innerop = gimple_assign_rhs1 (def_stmt);
9528 if (TREE_CODE (innerop) == SSA_NAME
9529 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9531 value_range_t *vr = get_value_range (innerop);
9533 if (range_int_cst_p (vr)
9534 && range_fits_type_p (vr,
9535 TYPE_PRECISION (TREE_TYPE (op0)),
9536 TYPE_SIGN (TREE_TYPE (op0)))
9537 && int_fits_type_p (op1, TREE_TYPE (innerop))
9538 /* The range must not have overflowed, or if it did overflow
9539 we must not be wrapping/trapping overflow and optimizing
9540 with strict overflow semantics. */
9541 && ((!is_negative_overflow_infinity (vr->min)
9542 && !is_positive_overflow_infinity (vr->max))
9543 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9545 /* If the range overflowed and the user has asked for warnings
9546 when strict overflow semantics were used to optimize code,
9547 issue an appropriate warning. */
9548 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9549 && (is_negative_overflow_infinity (vr->min)
9550 || is_positive_overflow_infinity (vr->max))
9551 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9553 location_t location;
9555 if (!gimple_has_location (stmt))
9556 location = input_location;
9557 else
9558 location = gimple_location (stmt);
9559 warning_at (location, OPT_Wstrict_overflow,
9560 "assuming signed overflow does not occur when "
9561 "simplifying conditional");
9564 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9565 gimple_cond_set_lhs (stmt, innerop);
9566 gimple_cond_set_rhs (stmt, newconst);
9567 return true;
9572 return false;
9575 /* Simplify a switch statement using the value range of the switch
9576 argument. */
9578 static bool
9579 simplify_switch_using_ranges (gswitch *stmt)
9581 tree op = gimple_switch_index (stmt);
9582 value_range_t *vr;
9583 bool take_default;
9584 edge e;
9585 edge_iterator ei;
9586 size_t i = 0, j = 0, n, n2;
9587 tree vec2;
9588 switch_update su;
9589 size_t k = 1, l = 0;
9591 if (TREE_CODE (op) == SSA_NAME)
9593 vr = get_value_range (op);
9595 /* We can only handle integer ranges. */
9596 if ((vr->type != VR_RANGE
9597 && vr->type != VR_ANTI_RANGE)
9598 || symbolic_range_p (vr))
9599 return false;
9601 /* Find case label for min/max of the value range. */
9602 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9604 else if (TREE_CODE (op) == INTEGER_CST)
9606 take_default = !find_case_label_index (stmt, 1, op, &i);
9607 if (take_default)
9609 i = 1;
9610 j = 0;
9612 else
9614 j = i;
9617 else
9618 return false;
9620 n = gimple_switch_num_labels (stmt);
9622 /* Bail out if this is just all edges taken. */
9623 if (i == 1
9624 && j == n - 1
9625 && take_default)
9626 return false;
9628 /* Build a new vector of taken case labels. */
9629 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9630 n2 = 0;
9632 /* Add the default edge, if necessary. */
9633 if (take_default)
9634 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9636 for (; i <= j; ++i, ++n2)
9637 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9639 for (; k <= l; ++k, ++n2)
9640 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9642 /* Mark needed edges. */
9643 for (i = 0; i < n2; ++i)
9645 e = find_edge (gimple_bb (stmt),
9646 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9647 e->aux = (void *)-1;
9650 /* Queue not needed edges for later removal. */
9651 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9653 if (e->aux == (void *)-1)
9655 e->aux = NULL;
9656 continue;
9659 if (dump_file && (dump_flags & TDF_DETAILS))
9661 fprintf (dump_file, "removing unreachable case label\n");
9663 to_remove_edges.safe_push (e);
9664 e->flags &= ~EDGE_EXECUTABLE;
9667 /* And queue an update for the stmt. */
9668 su.stmt = stmt;
9669 su.vec = vec2;
9670 to_update_switch_stmts.safe_push (su);
9671 return false;
9674 /* Simplify an integral conversion from an SSA name in STMT. */
9676 static bool
9677 simplify_conversion_using_ranges (gimple stmt)
9679 tree innerop, middleop, finaltype;
9680 gimple def_stmt;
9681 value_range_t *innervr;
9682 signop inner_sgn, middle_sgn, final_sgn;
9683 unsigned inner_prec, middle_prec, final_prec;
9684 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9686 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9687 if (!INTEGRAL_TYPE_P (finaltype))
9688 return false;
9689 middleop = gimple_assign_rhs1 (stmt);
9690 def_stmt = SSA_NAME_DEF_STMT (middleop);
9691 if (!is_gimple_assign (def_stmt)
9692 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9693 return false;
9694 innerop = gimple_assign_rhs1 (def_stmt);
9695 if (TREE_CODE (innerop) != SSA_NAME
9696 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9697 return false;
9699 /* Get the value-range of the inner operand. */
9700 innervr = get_value_range (innerop);
9701 if (innervr->type != VR_RANGE
9702 || TREE_CODE (innervr->min) != INTEGER_CST
9703 || TREE_CODE (innervr->max) != INTEGER_CST)
9704 return false;
9706 /* Simulate the conversion chain to check if the result is equal if
9707 the middle conversion is removed. */
9708 innermin = wi::to_widest (innervr->min);
9709 innermax = wi::to_widest (innervr->max);
9711 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9712 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9713 final_prec = TYPE_PRECISION (finaltype);
9715 /* If the first conversion is not injective, the second must not
9716 be widening. */
9717 if (wi::gtu_p (innermax - innermin,
9718 wi::mask <widest_int> (middle_prec, false))
9719 && middle_prec < final_prec)
9720 return false;
9721 /* We also want a medium value so that we can track the effect that
9722 narrowing conversions with sign change have. */
9723 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9724 if (inner_sgn == UNSIGNED)
9725 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9726 else
9727 innermed = 0;
9728 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9729 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9730 innermed = innermin;
9732 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9733 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9734 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9735 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9737 /* Require that the final conversion applied to both the original
9738 and the intermediate range produces the same result. */
9739 final_sgn = TYPE_SIGN (finaltype);
9740 if (wi::ext (middlemin, final_prec, final_sgn)
9741 != wi::ext (innermin, final_prec, final_sgn)
9742 || wi::ext (middlemed, final_prec, final_sgn)
9743 != wi::ext (innermed, final_prec, final_sgn)
9744 || wi::ext (middlemax, final_prec, final_sgn)
9745 != wi::ext (innermax, final_prec, final_sgn))
9746 return false;
9748 gimple_assign_set_rhs1 (stmt, innerop);
9749 update_stmt (stmt);
9750 return true;
9753 /* Simplify a conversion from integral SSA name to float in STMT. */
9755 static bool
9756 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9758 tree rhs1 = gimple_assign_rhs1 (stmt);
9759 value_range_t *vr = get_value_range (rhs1);
9760 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9761 machine_mode mode;
9762 tree tem;
9763 gassign *conv;
9765 /* We can only handle constant ranges. */
9766 if (vr->type != VR_RANGE
9767 || TREE_CODE (vr->min) != INTEGER_CST
9768 || TREE_CODE (vr->max) != INTEGER_CST)
9769 return false;
9771 /* First check if we can use a signed type in place of an unsigned. */
9772 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9773 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9774 != CODE_FOR_nothing)
9775 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9776 mode = TYPE_MODE (TREE_TYPE (rhs1));
9777 /* If we can do the conversion in the current input mode do nothing. */
9778 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9779 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9780 return false;
9781 /* Otherwise search for a mode we can use, starting from the narrowest
9782 integer mode available. */
9783 else
9785 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9788 /* If we cannot do a signed conversion to float from mode
9789 or if the value-range does not fit in the signed type
9790 try with a wider mode. */
9791 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9792 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9793 break;
9795 mode = GET_MODE_WIDER_MODE (mode);
9796 /* But do not widen the input. Instead leave that to the
9797 optabs expansion code. */
9798 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9799 return false;
9801 while (mode != VOIDmode);
9802 if (mode == VOIDmode)
9803 return false;
9806 /* It works, insert a truncation or sign-change before the
9807 float conversion. */
9808 tem = make_ssa_name (build_nonstandard_integer_type
9809 (GET_MODE_PRECISION (mode), 0));
9810 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9811 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9812 gimple_assign_set_rhs1 (stmt, tem);
9813 update_stmt (stmt);
9815 return true;
9818 /* Simplify an internal fn call using ranges if possible. */
9820 static bool
9821 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9823 enum tree_code subcode;
9824 bool is_ubsan = false;
9825 bool ovf = false;
9826 switch (gimple_call_internal_fn (stmt))
9828 case IFN_UBSAN_CHECK_ADD:
9829 subcode = PLUS_EXPR;
9830 is_ubsan = true;
9831 break;
9832 case IFN_UBSAN_CHECK_SUB:
9833 subcode = MINUS_EXPR;
9834 is_ubsan = true;
9835 break;
9836 case IFN_UBSAN_CHECK_MUL:
9837 subcode = MULT_EXPR;
9838 is_ubsan = true;
9839 break;
9840 case IFN_ADD_OVERFLOW:
9841 subcode = PLUS_EXPR;
9842 break;
9843 case IFN_SUB_OVERFLOW:
9844 subcode = MINUS_EXPR;
9845 break;
9846 case IFN_MUL_OVERFLOW:
9847 subcode = MULT_EXPR;
9848 break;
9849 default:
9850 return false;
9853 tree op0 = gimple_call_arg (stmt, 0);
9854 tree op1 = gimple_call_arg (stmt, 1);
9855 tree type;
9856 if (is_ubsan)
9857 type = TREE_TYPE (op0);
9858 else if (gimple_call_lhs (stmt) == NULL_TREE)
9859 return false;
9860 else
9861 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9862 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9863 || (is_ubsan && ovf))
9864 return false;
9866 gimple g;
9867 location_t loc = gimple_location (stmt);
9868 if (is_ubsan)
9869 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9870 else
9872 int prec = TYPE_PRECISION (type);
9873 tree utype = type;
9874 if (ovf
9875 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9876 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9877 utype = build_nonstandard_integer_type (prec, 1);
9878 if (TREE_CODE (op0) == INTEGER_CST)
9879 op0 = fold_convert (utype, op0);
9880 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9882 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9883 gimple_set_location (g, loc);
9884 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9885 op0 = gimple_assign_lhs (g);
9887 if (TREE_CODE (op1) == INTEGER_CST)
9888 op1 = fold_convert (utype, op1);
9889 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9891 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9892 gimple_set_location (g, loc);
9893 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9894 op1 = gimple_assign_lhs (g);
9896 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9897 gimple_set_location (g, loc);
9898 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9899 if (utype != type)
9901 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9902 gimple_assign_lhs (g));
9903 gimple_set_location (g, loc);
9904 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9906 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9907 gimple_assign_lhs (g),
9908 build_int_cst (type, ovf));
9910 gimple_set_location (g, loc);
9911 gsi_replace (gsi, g, false);
9912 return true;
9915 /* Simplify STMT using ranges if possible. */
9917 static bool
9918 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9920 gimple stmt = gsi_stmt (*gsi);
9921 if (is_gimple_assign (stmt))
9923 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9924 tree rhs1 = gimple_assign_rhs1 (stmt);
9926 switch (rhs_code)
9928 case EQ_EXPR:
9929 case NE_EXPR:
9930 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9931 if the RHS is zero or one, and the LHS are known to be boolean
9932 values. */
9933 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9934 return simplify_truth_ops_using_ranges (gsi, stmt);
9935 break;
9937 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9938 and BIT_AND_EXPR respectively if the first operand is greater
9939 than zero and the second operand is an exact power of two.
9940 Also optimize TRUNC_MOD_EXPR away if the second operand is
9941 constant and the first operand already has the right value
9942 range. */
9943 case TRUNC_DIV_EXPR:
9944 case TRUNC_MOD_EXPR:
9945 if (TREE_CODE (rhs1) == SSA_NAME
9946 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9947 return simplify_div_or_mod_using_ranges (stmt);
9948 break;
9950 /* Transform ABS (X) into X or -X as appropriate. */
9951 case ABS_EXPR:
9952 if (TREE_CODE (rhs1) == SSA_NAME
9953 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9954 return simplify_abs_using_ranges (stmt);
9955 break;
9957 case BIT_AND_EXPR:
9958 case BIT_IOR_EXPR:
9959 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9960 if all the bits being cleared are already cleared or
9961 all the bits being set are already set. */
9962 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9963 return simplify_bit_ops_using_ranges (gsi, stmt);
9964 break;
9966 CASE_CONVERT:
9967 if (TREE_CODE (rhs1) == SSA_NAME
9968 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9969 return simplify_conversion_using_ranges (stmt);
9970 break;
9972 case FLOAT_EXPR:
9973 if (TREE_CODE (rhs1) == SSA_NAME
9974 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9975 return simplify_float_conversion_using_ranges (gsi, stmt);
9976 break;
9978 default:
9979 break;
9982 else if (gimple_code (stmt) == GIMPLE_COND)
9983 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9984 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9985 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
9986 else if (is_gimple_call (stmt)
9987 && gimple_call_internal_p (stmt))
9988 return simplify_internal_call_using_ranges (gsi, stmt);
9990 return false;
9993 /* If the statement pointed by SI has a predicate whose value can be
9994 computed using the value range information computed by VRP, compute
9995 its value and return true. Otherwise, return false. */
9997 static bool
9998 fold_predicate_in (gimple_stmt_iterator *si)
10000 bool assignment_p = false;
10001 tree val;
10002 gimple stmt = gsi_stmt (*si);
10004 if (is_gimple_assign (stmt)
10005 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10007 assignment_p = true;
10008 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10009 gimple_assign_rhs1 (stmt),
10010 gimple_assign_rhs2 (stmt),
10011 stmt);
10013 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10014 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10015 gimple_cond_lhs (cond_stmt),
10016 gimple_cond_rhs (cond_stmt),
10017 stmt);
10018 else
10019 return false;
10021 if (val)
10023 if (assignment_p)
10024 val = fold_convert (gimple_expr_type (stmt), val);
10026 if (dump_file)
10028 fprintf (dump_file, "Folding predicate ");
10029 print_gimple_expr (dump_file, stmt, 0, 0);
10030 fprintf (dump_file, " to ");
10031 print_generic_expr (dump_file, val, 0);
10032 fprintf (dump_file, "\n");
10035 if (is_gimple_assign (stmt))
10036 gimple_assign_set_rhs_from_tree (si, val);
10037 else
10039 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10040 gcond *cond_stmt = as_a <gcond *> (stmt);
10041 if (integer_zerop (val))
10042 gimple_cond_make_false (cond_stmt);
10043 else if (integer_onep (val))
10044 gimple_cond_make_true (cond_stmt);
10045 else
10046 gcc_unreachable ();
10049 return true;
10052 return false;
10055 /* Callback for substitute_and_fold folding the stmt at *SI. */
10057 static bool
10058 vrp_fold_stmt (gimple_stmt_iterator *si)
10060 if (fold_predicate_in (si))
10061 return true;
10063 return simplify_stmt_using_ranges (si);
10066 /* Unwindable const/copy equivalences. */
10067 const_and_copies *equiv_stack;
10069 /* A trivial wrapper so that we can present the generic jump threading
10070 code with a simple API for simplifying statements. STMT is the
10071 statement we want to simplify, WITHIN_STMT provides the location
10072 for any overflow warnings. */
10074 static tree
10075 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
10077 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10078 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10079 gimple_cond_lhs (cond_stmt),
10080 gimple_cond_rhs (cond_stmt),
10081 within_stmt);
10083 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10085 value_range_t new_vr = VR_INITIALIZER;
10086 tree lhs = gimple_assign_lhs (assign_stmt);
10088 if (TREE_CODE (lhs) == SSA_NAME
10089 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10090 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10092 extract_range_from_assignment (&new_vr, assign_stmt);
10093 if (range_int_cst_singleton_p (&new_vr))
10094 return new_vr.min;
10098 return NULL_TREE;
10101 /* Blocks which have more than one predecessor and more than
10102 one successor present jump threading opportunities, i.e.,
10103 when the block is reached from a specific predecessor, we
10104 may be able to determine which of the outgoing edges will
10105 be traversed. When this optimization applies, we are able
10106 to avoid conditionals at runtime and we may expose secondary
10107 optimization opportunities.
10109 This routine is effectively a driver for the generic jump
10110 threading code. It basically just presents the generic code
10111 with edges that may be suitable for jump threading.
10113 Unlike DOM, we do not iterate VRP if jump threading was successful.
10114 While iterating may expose new opportunities for VRP, it is expected
10115 those opportunities would be very limited and the compile time cost
10116 to expose those opportunities would be significant.
10118 As jump threading opportunities are discovered, they are registered
10119 for later realization. */
10121 static void
10122 identify_jump_threads (void)
10124 basic_block bb;
10125 gcond *dummy;
10126 int i;
10127 edge e;
10129 /* Ugh. When substituting values earlier in this pass we can
10130 wipe the dominance information. So rebuild the dominator
10131 information as we need it within the jump threading code. */
10132 calculate_dominance_info (CDI_DOMINATORS);
10134 /* We do not allow VRP information to be used for jump threading
10135 across a back edge in the CFG. Otherwise it becomes too
10136 difficult to avoid eliminating loop exit tests. Of course
10137 EDGE_DFS_BACK is not accurate at this time so we have to
10138 recompute it. */
10139 mark_dfs_back_edges ();
10141 /* Do not thread across edges we are about to remove. Just marking
10142 them as EDGE_DFS_BACK will do. */
10143 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10144 e->flags |= EDGE_DFS_BACK;
10146 /* Allocate our unwinder stack to unwind any temporary equivalences
10147 that might be recorded. */
10148 equiv_stack = new const_and_copies (dump_file, dump_flags);
10150 /* To avoid lots of silly node creation, we create a single
10151 conditional and just modify it in-place when attempting to
10152 thread jumps. */
10153 dummy = gimple_build_cond (EQ_EXPR,
10154 integer_zero_node, integer_zero_node,
10155 NULL, NULL);
10157 /* Walk through all the blocks finding those which present a
10158 potential jump threading opportunity. We could set this up
10159 as a dominator walker and record data during the walk, but
10160 I doubt it's worth the effort for the classes of jump
10161 threading opportunities we are trying to identify at this
10162 point in compilation. */
10163 FOR_EACH_BB_FN (bb, cfun)
10165 gimple last;
10167 /* If the generic jump threading code does not find this block
10168 interesting, then there is nothing to do. */
10169 if (! potentially_threadable_block (bb))
10170 continue;
10172 last = last_stmt (bb);
10174 /* We're basically looking for a switch or any kind of conditional with
10175 integral or pointer type arguments. Note the type of the second
10176 argument will be the same as the first argument, so no need to
10177 check it explicitly.
10179 We also handle the case where there are no statements in the
10180 block. This come up with forwarder blocks that are not
10181 optimized away because they lead to a loop header. But we do
10182 want to thread through them as we can sometimes thread to the
10183 loop exit which is obviously profitable. */
10184 if (!last
10185 || gimple_code (last) == GIMPLE_SWITCH
10186 || (gimple_code (last) == GIMPLE_COND
10187 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10188 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10189 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10190 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10191 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10193 edge_iterator ei;
10195 /* We've got a block with multiple predecessors and multiple
10196 successors which also ends in a suitable conditional or
10197 switch statement. For each predecessor, see if we can thread
10198 it to a specific successor. */
10199 FOR_EACH_EDGE (e, ei, bb->preds)
10201 /* Do not thread across back edges or abnormal edges
10202 in the CFG. */
10203 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
10204 continue;
10206 thread_across_edge (dummy, e, true, equiv_stack,
10207 simplify_stmt_for_jump_threading);
10212 /* We do not actually update the CFG or SSA graphs at this point as
10213 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10214 handle ASSERT_EXPRs gracefully. */
10217 /* We identified all the jump threading opportunities earlier, but could
10218 not transform the CFG at that time. This routine transforms the
10219 CFG and arranges for the dominator tree to be rebuilt if necessary.
10221 Note the SSA graph update will occur during the normal TODO
10222 processing by the pass manager. */
10223 static void
10224 finalize_jump_threads (void)
10226 thread_through_all_blocks (false);
10227 delete equiv_stack;
10231 /* Traverse all the blocks folding conditionals with known ranges. */
10233 static void
10234 vrp_finalize (void)
10236 size_t i;
10238 values_propagated = true;
10240 if (dump_file)
10242 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10243 dump_all_value_ranges (dump_file);
10244 fprintf (dump_file, "\n");
10247 substitute_and_fold (op_with_constant_singleton_value_range,
10248 vrp_fold_stmt, false);
10250 if (warn_array_bounds && first_pass_instance)
10251 check_all_array_refs ();
10253 /* We must identify jump threading opportunities before we release
10254 the datastructures built by VRP. */
10255 identify_jump_threads ();
10257 /* Set value range to non pointer SSA_NAMEs. */
10258 for (i = 0; i < num_vr_values; i++)
10259 if (vr_value[i])
10261 tree name = ssa_name (i);
10263 if (!name
10264 || POINTER_TYPE_P (TREE_TYPE (name))
10265 || (vr_value[i]->type == VR_VARYING)
10266 || (vr_value[i]->type == VR_UNDEFINED))
10267 continue;
10269 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10270 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10271 && (vr_value[i]->type == VR_RANGE
10272 || vr_value[i]->type == VR_ANTI_RANGE))
10273 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10274 vr_value[i]->max);
10277 /* Free allocated memory. */
10278 for (i = 0; i < num_vr_values; i++)
10279 if (vr_value[i])
10281 BITMAP_FREE (vr_value[i]->equiv);
10282 free (vr_value[i]);
10285 free (vr_value);
10286 free (vr_phi_edge_counts);
10288 /* So that we can distinguish between VRP data being available
10289 and not available. */
10290 vr_value = NULL;
10291 vr_phi_edge_counts = NULL;
10295 /* Main entry point to VRP (Value Range Propagation). This pass is
10296 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10297 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10298 Programming Language Design and Implementation, pp. 67-78, 1995.
10299 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10301 This is essentially an SSA-CCP pass modified to deal with ranges
10302 instead of constants.
10304 While propagating ranges, we may find that two or more SSA name
10305 have equivalent, though distinct ranges. For instance,
10307 1 x_9 = p_3->a;
10308 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10309 3 if (p_4 == q_2)
10310 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10311 5 endif
10312 6 if (q_2)
10314 In the code above, pointer p_5 has range [q_2, q_2], but from the
10315 code we can also determine that p_5 cannot be NULL and, if q_2 had
10316 a non-varying range, p_5's range should also be compatible with it.
10318 These equivalences are created by two expressions: ASSERT_EXPR and
10319 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10320 result of another assertion, then we can use the fact that p_5 and
10321 p_4 are equivalent when evaluating p_5's range.
10323 Together with value ranges, we also propagate these equivalences
10324 between names so that we can take advantage of information from
10325 multiple ranges when doing final replacement. Note that this
10326 equivalency relation is transitive but not symmetric.
10328 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10329 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10330 in contexts where that assertion does not hold (e.g., in line 6).
10332 TODO, the main difference between this pass and Patterson's is that
10333 we do not propagate edge probabilities. We only compute whether
10334 edges can be taken or not. That is, instead of having a spectrum
10335 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10336 DON'T KNOW. In the future, it may be worthwhile to propagate
10337 probabilities to aid branch prediction. */
10339 static unsigned int
10340 execute_vrp (void)
10342 int i;
10343 edge e;
10344 switch_update *su;
10346 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10347 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10348 scev_initialize ();
10350 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10351 Inserting assertions may split edges which will invalidate
10352 EDGE_DFS_BACK. */
10353 insert_range_assertions ();
10355 to_remove_edges.create (10);
10356 to_update_switch_stmts.create (5);
10357 threadedge_initialize_values ();
10359 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10360 mark_dfs_back_edges ();
10362 vrp_initialize ();
10363 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10364 vrp_finalize ();
10366 free_numbers_of_iterations_estimates ();
10368 /* ASSERT_EXPRs must be removed before finalizing jump threads
10369 as finalizing jump threads calls the CFG cleanup code which
10370 does not properly handle ASSERT_EXPRs. */
10371 remove_range_assertions ();
10373 /* If we exposed any new variables, go ahead and put them into
10374 SSA form now, before we handle jump threading. This simplifies
10375 interactions between rewriting of _DECL nodes into SSA form
10376 and rewriting SSA_NAME nodes into SSA form after block
10377 duplication and CFG manipulation. */
10378 update_ssa (TODO_update_ssa);
10380 finalize_jump_threads ();
10382 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10383 CFG in a broken state and requires a cfg_cleanup run. */
10384 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10385 remove_edge (e);
10386 /* Update SWITCH_EXPR case label vector. */
10387 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10389 size_t j;
10390 size_t n = TREE_VEC_LENGTH (su->vec);
10391 tree label;
10392 gimple_switch_set_num_labels (su->stmt, n);
10393 for (j = 0; j < n; j++)
10394 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10395 /* As we may have replaced the default label with a regular one
10396 make sure to make it a real default label again. This ensures
10397 optimal expansion. */
10398 label = gimple_switch_label (su->stmt, 0);
10399 CASE_LOW (label) = NULL_TREE;
10400 CASE_HIGH (label) = NULL_TREE;
10403 if (to_remove_edges.length () > 0)
10405 free_dominance_info (CDI_DOMINATORS);
10406 loops_state_set (LOOPS_NEED_FIXUP);
10409 to_remove_edges.release ();
10410 to_update_switch_stmts.release ();
10411 threadedge_finalize_values ();
10413 scev_finalize ();
10414 loop_optimizer_finalize ();
10415 return 0;
10418 namespace {
10420 const pass_data pass_data_vrp =
10422 GIMPLE_PASS, /* type */
10423 "vrp", /* name */
10424 OPTGROUP_NONE, /* optinfo_flags */
10425 TV_TREE_VRP, /* tv_id */
10426 PROP_ssa, /* properties_required */
10427 0, /* properties_provided */
10428 0, /* properties_destroyed */
10429 0, /* todo_flags_start */
10430 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10433 class pass_vrp : public gimple_opt_pass
10435 public:
10436 pass_vrp (gcc::context *ctxt)
10437 : gimple_opt_pass (pass_data_vrp, ctxt)
10440 /* opt_pass methods: */
10441 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10442 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10443 virtual unsigned int execute (function *) { return execute_vrp (); }
10445 }; // class pass_vrp
10447 } // anon namespace
10449 gimple_opt_pass *
10450 make_pass_vrp (gcc::context *ctxt)
10452 return new pass_vrp (ctxt);