re PR tree-optimization/64454 (optimize (x%5)%5)
[official-gcc.git] / gcc / tree-vrp.c
blobc0b381df648a6d3bcbfbc95c9fe574d1dc0f8dbf
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 (range_is_null (&vr1))
3201 set_value_range_to_undefined (vr);
3202 return;
3204 /* ABS (A % B) < ABS (B) and either
3205 0 <= A % B <= A or A <= A % B <= 0. */
3206 type = VR_RANGE;
3207 signop sgn = TYPE_SIGN (expr_type);
3208 unsigned int prec = TYPE_PRECISION (expr_type);
3209 wide_int wmin, wmax, tmp;
3210 wide_int zero = wi::zero (prec);
3211 wide_int one = wi::one (prec);
3212 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
3214 wmax = wi::sub (vr1.max, one);
3215 if (sgn == SIGNED)
3217 tmp = wi::sub (wi::minus_one (prec), vr1.min);
3218 wmax = wi::smax (wmax, tmp);
3221 else
3223 wmax = wi::max_value (prec, sgn);
3224 /* X % INT_MIN may be INT_MAX. */
3225 if (sgn == UNSIGNED)
3226 wmax = wmax - one;
3229 if (sgn == UNSIGNED)
3230 wmin = zero;
3231 else
3233 wmin = -wmax;
3234 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3236 tmp = vr0.min;
3237 if (wi::gts_p (tmp, zero))
3238 tmp = zero;
3239 wmin = wi::smax (wmin, tmp);
3243 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3245 tmp = vr0.max;
3246 if (sgn == SIGNED && wi::neg_p (tmp))
3247 tmp = zero;
3248 wmax = wi::min (wmax, tmp, sgn);
3251 min = wide_int_to_tree (expr_type, wmin);
3252 max = wide_int_to_tree (expr_type, wmax);
3254 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3256 bool int_cst_range0, int_cst_range1;
3257 wide_int may_be_nonzero0, may_be_nonzero1;
3258 wide_int must_be_nonzero0, must_be_nonzero1;
3260 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3261 &may_be_nonzero0,
3262 &must_be_nonzero0);
3263 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3264 &may_be_nonzero1,
3265 &must_be_nonzero1);
3267 type = VR_RANGE;
3268 if (code == BIT_AND_EXPR)
3270 min = wide_int_to_tree (expr_type,
3271 must_be_nonzero0 & must_be_nonzero1);
3272 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3273 /* If both input ranges contain only negative values we can
3274 truncate the result range maximum to the minimum of the
3275 input range maxima. */
3276 if (int_cst_range0 && int_cst_range1
3277 && tree_int_cst_sgn (vr0.max) < 0
3278 && tree_int_cst_sgn (vr1.max) < 0)
3280 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3281 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3283 /* If either input range contains only non-negative values
3284 we can truncate the result range maximum to the respective
3285 maximum of the input range. */
3286 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3287 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3288 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3289 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3290 max = wide_int_to_tree (expr_type, wmax);
3292 else if (code == BIT_IOR_EXPR)
3294 max = wide_int_to_tree (expr_type,
3295 may_be_nonzero0 | may_be_nonzero1);
3296 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3297 /* If the input ranges contain only positive values we can
3298 truncate the minimum of the result range to the maximum
3299 of the input range minima. */
3300 if (int_cst_range0 && int_cst_range1
3301 && tree_int_cst_sgn (vr0.min) >= 0
3302 && tree_int_cst_sgn (vr1.min) >= 0)
3304 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3305 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3307 /* If either input range contains only negative values
3308 we can truncate the minimum of the result range to the
3309 respective minimum range. */
3310 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3311 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3312 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3313 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3314 min = wide_int_to_tree (expr_type, wmin);
3316 else if (code == BIT_XOR_EXPR)
3318 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3319 | ~(may_be_nonzero0 | may_be_nonzero1));
3320 wide_int result_one_bits
3321 = (must_be_nonzero0.and_not (may_be_nonzero1)
3322 | must_be_nonzero1.and_not (may_be_nonzero0));
3323 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3324 min = wide_int_to_tree (expr_type, result_one_bits);
3325 /* If the range has all positive or all negative values the
3326 result is better than VARYING. */
3327 if (tree_int_cst_sgn (min) < 0
3328 || tree_int_cst_sgn (max) >= 0)
3330 else
3331 max = min = NULL_TREE;
3334 else
3335 gcc_unreachable ();
3337 /* If either MIN or MAX overflowed, then set the resulting range to
3338 VARYING. But we do accept an overflow infinity representation. */
3339 if (min == NULL_TREE
3340 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3341 || max == NULL_TREE
3342 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3344 set_value_range_to_varying (vr);
3345 return;
3348 /* We punt if:
3349 1) [-INF, +INF]
3350 2) [-INF, +-INF(OVF)]
3351 3) [+-INF(OVF), +INF]
3352 4) [+-INF(OVF), +-INF(OVF)]
3353 We learn nothing when we have INF and INF(OVF) on both sides.
3354 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3355 overflow. */
3356 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3357 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3359 set_value_range_to_varying (vr);
3360 return;
3363 cmp = compare_values (min, max);
3364 if (cmp == -2 || cmp == 1)
3366 /* If the new range has its limits swapped around (MIN > MAX),
3367 then the operation caused one of them to wrap around, mark
3368 the new range VARYING. */
3369 set_value_range_to_varying (vr);
3371 else
3372 set_value_range (vr, type, min, max, NULL);
3375 /* Extract range information from a binary expression OP0 CODE OP1 based on
3376 the ranges of each of its operands with resulting type EXPR_TYPE.
3377 The resulting range is stored in *VR. */
3379 static void
3380 extract_range_from_binary_expr (value_range_t *vr,
3381 enum tree_code code,
3382 tree expr_type, tree op0, tree op1)
3384 value_range_t vr0 = VR_INITIALIZER;
3385 value_range_t vr1 = VR_INITIALIZER;
3387 /* Get value ranges for each operand. For constant operands, create
3388 a new value range with the operand to simplify processing. */
3389 if (TREE_CODE (op0) == SSA_NAME)
3390 vr0 = *(get_value_range (op0));
3391 else if (is_gimple_min_invariant (op0))
3392 set_value_range_to_value (&vr0, op0, NULL);
3393 else
3394 set_value_range_to_varying (&vr0);
3396 if (TREE_CODE (op1) == SSA_NAME)
3397 vr1 = *(get_value_range (op1));
3398 else if (is_gimple_min_invariant (op1))
3399 set_value_range_to_value (&vr1, op1, NULL);
3400 else
3401 set_value_range_to_varying (&vr1);
3403 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3405 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3406 and based on the other operand, for example if it was deduced from a
3407 symbolic comparison. When a bound of the range of the first operand
3408 is invariant, we set the corresponding bound of the new range to INF
3409 in order to avoid recursing on the range of the second operand. */
3410 if (vr->type == VR_VARYING
3411 && (code == PLUS_EXPR || code == MINUS_EXPR)
3412 && TREE_CODE (op1) == SSA_NAME
3413 && vr0.type == VR_RANGE
3414 && symbolic_range_based_on_p (&vr0, op1))
3416 const bool minus_p = (code == MINUS_EXPR);
3417 value_range_t n_vr1 = VR_INITIALIZER;
3419 /* Try with VR0 and [-INF, OP1]. */
3420 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3421 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3423 /* Try with VR0 and [OP1, +INF]. */
3424 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3425 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3427 /* Try with VR0 and [OP1, OP1]. */
3428 else
3429 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3431 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3434 if (vr->type == VR_VARYING
3435 && (code == PLUS_EXPR || code == MINUS_EXPR)
3436 && TREE_CODE (op0) == SSA_NAME
3437 && vr1.type == VR_RANGE
3438 && symbolic_range_based_on_p (&vr1, op0))
3440 const bool minus_p = (code == MINUS_EXPR);
3441 value_range_t n_vr0 = VR_INITIALIZER;
3443 /* Try with [-INF, OP0] and VR1. */
3444 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3445 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3447 /* Try with [OP0, +INF] and VR1. */
3448 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3449 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3451 /* Try with [OP0, OP0] and VR1. */
3452 else
3453 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3455 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3459 /* Extract range information from a unary operation CODE based on
3460 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3461 The The resulting range is stored in *VR. */
3463 static void
3464 extract_range_from_unary_expr_1 (value_range_t *vr,
3465 enum tree_code code, tree type,
3466 value_range_t *vr0_, tree op0_type)
3468 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3470 /* VRP only operates on integral and pointer types. */
3471 if (!(INTEGRAL_TYPE_P (op0_type)
3472 || POINTER_TYPE_P (op0_type))
3473 || !(INTEGRAL_TYPE_P (type)
3474 || POINTER_TYPE_P (type)))
3476 set_value_range_to_varying (vr);
3477 return;
3480 /* If VR0 is UNDEFINED, so is the result. */
3481 if (vr0.type == VR_UNDEFINED)
3483 set_value_range_to_undefined (vr);
3484 return;
3487 /* Handle operations that we express in terms of others. */
3488 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3490 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3491 copy_value_range (vr, &vr0);
3492 return;
3494 else if (code == NEGATE_EXPR)
3496 /* -X is simply 0 - X, so re-use existing code that also handles
3497 anti-ranges fine. */
3498 value_range_t zero = VR_INITIALIZER;
3499 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3500 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3501 return;
3503 else if (code == BIT_NOT_EXPR)
3505 /* ~X is simply -1 - X, so re-use existing code that also handles
3506 anti-ranges fine. */
3507 value_range_t minusone = VR_INITIALIZER;
3508 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3509 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3510 type, &minusone, &vr0);
3511 return;
3514 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3515 and express op ~[] as (op []') U (op []''). */
3516 if (vr0.type == VR_ANTI_RANGE
3517 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3519 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3520 if (vrtem1.type != VR_UNDEFINED)
3522 value_range_t vrres = VR_INITIALIZER;
3523 extract_range_from_unary_expr_1 (&vrres, code, type,
3524 &vrtem1, op0_type);
3525 vrp_meet (vr, &vrres);
3527 return;
3530 if (CONVERT_EXPR_CODE_P (code))
3532 tree inner_type = op0_type;
3533 tree outer_type = type;
3535 /* If the expression evaluates to a pointer, we are only interested in
3536 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3537 if (POINTER_TYPE_P (type))
3539 if (range_is_nonnull (&vr0))
3540 set_value_range_to_nonnull (vr, type);
3541 else if (range_is_null (&vr0))
3542 set_value_range_to_null (vr, type);
3543 else
3544 set_value_range_to_varying (vr);
3545 return;
3548 /* If VR0 is varying and we increase the type precision, assume
3549 a full range for the following transformation. */
3550 if (vr0.type == VR_VARYING
3551 && INTEGRAL_TYPE_P (inner_type)
3552 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3554 vr0.type = VR_RANGE;
3555 vr0.min = TYPE_MIN_VALUE (inner_type);
3556 vr0.max = TYPE_MAX_VALUE (inner_type);
3559 /* If VR0 is a constant range or anti-range and the conversion is
3560 not truncating we can convert the min and max values and
3561 canonicalize the resulting range. Otherwise we can do the
3562 conversion if the size of the range is less than what the
3563 precision of the target type can represent and the range is
3564 not an anti-range. */
3565 if ((vr0.type == VR_RANGE
3566 || vr0.type == VR_ANTI_RANGE)
3567 && TREE_CODE (vr0.min) == INTEGER_CST
3568 && TREE_CODE (vr0.max) == INTEGER_CST
3569 && (!is_overflow_infinity (vr0.min)
3570 || (vr0.type == VR_RANGE
3571 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3572 && needs_overflow_infinity (outer_type)
3573 && supports_overflow_infinity (outer_type)))
3574 && (!is_overflow_infinity (vr0.max)
3575 || (vr0.type == VR_RANGE
3576 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3577 && needs_overflow_infinity (outer_type)
3578 && supports_overflow_infinity (outer_type)))
3579 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3580 || (vr0.type == VR_RANGE
3581 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3582 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3583 size_int (TYPE_PRECISION (outer_type)))))))
3585 tree new_min, new_max;
3586 if (is_overflow_infinity (vr0.min))
3587 new_min = negative_overflow_infinity (outer_type);
3588 else
3589 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3590 0, false);
3591 if (is_overflow_infinity (vr0.max))
3592 new_max = positive_overflow_infinity (outer_type);
3593 else
3594 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3595 0, false);
3596 set_and_canonicalize_value_range (vr, vr0.type,
3597 new_min, new_max, NULL);
3598 return;
3601 set_value_range_to_varying (vr);
3602 return;
3604 else if (code == ABS_EXPR)
3606 tree min, max;
3607 int cmp;
3609 /* Pass through vr0 in the easy cases. */
3610 if (TYPE_UNSIGNED (type)
3611 || value_range_nonnegative_p (&vr0))
3613 copy_value_range (vr, &vr0);
3614 return;
3617 /* For the remaining varying or symbolic ranges we can't do anything
3618 useful. */
3619 if (vr0.type == VR_VARYING
3620 || symbolic_range_p (&vr0))
3622 set_value_range_to_varying (vr);
3623 return;
3626 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3627 useful range. */
3628 if (!TYPE_OVERFLOW_UNDEFINED (type)
3629 && ((vr0.type == VR_RANGE
3630 && vrp_val_is_min (vr0.min))
3631 || (vr0.type == VR_ANTI_RANGE
3632 && !vrp_val_is_min (vr0.min))))
3634 set_value_range_to_varying (vr);
3635 return;
3638 /* ABS_EXPR may flip the range around, if the original range
3639 included negative values. */
3640 if (is_overflow_infinity (vr0.min))
3641 min = positive_overflow_infinity (type);
3642 else if (!vrp_val_is_min (vr0.min))
3643 min = fold_unary_to_constant (code, type, vr0.min);
3644 else if (!needs_overflow_infinity (type))
3645 min = TYPE_MAX_VALUE (type);
3646 else if (supports_overflow_infinity (type))
3647 min = positive_overflow_infinity (type);
3648 else
3650 set_value_range_to_varying (vr);
3651 return;
3654 if (is_overflow_infinity (vr0.max))
3655 max = positive_overflow_infinity (type);
3656 else if (!vrp_val_is_min (vr0.max))
3657 max = fold_unary_to_constant (code, type, vr0.max);
3658 else if (!needs_overflow_infinity (type))
3659 max = TYPE_MAX_VALUE (type);
3660 else if (supports_overflow_infinity (type)
3661 /* We shouldn't generate [+INF, +INF] as set_value_range
3662 doesn't like this and ICEs. */
3663 && !is_positive_overflow_infinity (min))
3664 max = positive_overflow_infinity (type);
3665 else
3667 set_value_range_to_varying (vr);
3668 return;
3671 cmp = compare_values (min, max);
3673 /* If a VR_ANTI_RANGEs contains zero, then we have
3674 ~[-INF, min(MIN, MAX)]. */
3675 if (vr0.type == VR_ANTI_RANGE)
3677 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3679 /* Take the lower of the two values. */
3680 if (cmp != 1)
3681 max = min;
3683 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3684 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3685 flag_wrapv is set and the original anti-range doesn't include
3686 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3687 if (TYPE_OVERFLOW_WRAPS (type))
3689 tree type_min_value = TYPE_MIN_VALUE (type);
3691 min = (vr0.min != type_min_value
3692 ? int_const_binop (PLUS_EXPR, type_min_value,
3693 build_int_cst (TREE_TYPE (type_min_value), 1))
3694 : type_min_value);
3696 else
3698 if (overflow_infinity_range_p (&vr0))
3699 min = negative_overflow_infinity (type);
3700 else
3701 min = TYPE_MIN_VALUE (type);
3704 else
3706 /* All else has failed, so create the range [0, INF], even for
3707 flag_wrapv since TYPE_MIN_VALUE is in the original
3708 anti-range. */
3709 vr0.type = VR_RANGE;
3710 min = build_int_cst (type, 0);
3711 if (needs_overflow_infinity (type))
3713 if (supports_overflow_infinity (type))
3714 max = positive_overflow_infinity (type);
3715 else
3717 set_value_range_to_varying (vr);
3718 return;
3721 else
3722 max = TYPE_MAX_VALUE (type);
3726 /* If the range contains zero then we know that the minimum value in the
3727 range will be zero. */
3728 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3730 if (cmp == 1)
3731 max = min;
3732 min = build_int_cst (type, 0);
3734 else
3736 /* If the range was reversed, swap MIN and MAX. */
3737 if (cmp == 1)
3739 tree t = min;
3740 min = max;
3741 max = t;
3745 cmp = compare_values (min, max);
3746 if (cmp == -2 || cmp == 1)
3748 /* If the new range has its limits swapped around (MIN > MAX),
3749 then the operation caused one of them to wrap around, mark
3750 the new range VARYING. */
3751 set_value_range_to_varying (vr);
3753 else
3754 set_value_range (vr, vr0.type, min, max, NULL);
3755 return;
3758 /* For unhandled operations fall back to varying. */
3759 set_value_range_to_varying (vr);
3760 return;
3764 /* Extract range information from a unary expression CODE OP0 based on
3765 the range of its operand with resulting type TYPE.
3766 The resulting range is stored in *VR. */
3768 static void
3769 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3770 tree type, tree op0)
3772 value_range_t vr0 = VR_INITIALIZER;
3774 /* Get value ranges for the operand. For constant operands, create
3775 a new value range with the operand to simplify processing. */
3776 if (TREE_CODE (op0) == SSA_NAME)
3777 vr0 = *(get_value_range (op0));
3778 else if (is_gimple_min_invariant (op0))
3779 set_value_range_to_value (&vr0, op0, NULL);
3780 else
3781 set_value_range_to_varying (&vr0);
3783 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3787 /* Extract range information from a conditional expression STMT based on
3788 the ranges of each of its operands and the expression code. */
3790 static void
3791 extract_range_from_cond_expr (value_range_t *vr, gassign *stmt)
3793 tree op0, op1;
3794 value_range_t vr0 = VR_INITIALIZER;
3795 value_range_t vr1 = VR_INITIALIZER;
3797 /* Get value ranges for each operand. For constant operands, create
3798 a new value range with the operand to simplify processing. */
3799 op0 = gimple_assign_rhs2 (stmt);
3800 if (TREE_CODE (op0) == SSA_NAME)
3801 vr0 = *(get_value_range (op0));
3802 else if (is_gimple_min_invariant (op0))
3803 set_value_range_to_value (&vr0, op0, NULL);
3804 else
3805 set_value_range_to_varying (&vr0);
3807 op1 = gimple_assign_rhs3 (stmt);
3808 if (TREE_CODE (op1) == SSA_NAME)
3809 vr1 = *(get_value_range (op1));
3810 else if (is_gimple_min_invariant (op1))
3811 set_value_range_to_value (&vr1, op1, NULL);
3812 else
3813 set_value_range_to_varying (&vr1);
3815 /* The resulting value range is the union of the operand ranges */
3816 copy_value_range (vr, &vr0);
3817 vrp_meet (vr, &vr1);
3821 /* Extract range information from a comparison expression EXPR based
3822 on the range of its operand and the expression code. */
3824 static void
3825 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3826 tree type, tree op0, tree op1)
3828 bool sop = false;
3829 tree val;
3831 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3832 NULL);
3834 /* A disadvantage of using a special infinity as an overflow
3835 representation is that we lose the ability to record overflow
3836 when we don't have an infinity. So we have to ignore a result
3837 which relies on overflow. */
3839 if (val && !is_overflow_infinity (val) && !sop)
3841 /* Since this expression was found on the RHS of an assignment,
3842 its type may be different from _Bool. Convert VAL to EXPR's
3843 type. */
3844 val = fold_convert (type, val);
3845 if (is_gimple_min_invariant (val))
3846 set_value_range_to_value (vr, val, vr->equiv);
3847 else
3848 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3850 else
3851 /* The result of a comparison is always true or false. */
3852 set_value_range_to_truthvalue (vr, type);
3855 /* Helper function for simplify_internal_call_using_ranges and
3856 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3857 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3858 always overflow. Set *OVF to true if it is known to always
3859 overflow. */
3861 static bool
3862 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3863 tree op0, tree op1, bool *ovf)
3865 value_range_t vr0 = VR_INITIALIZER;
3866 value_range_t vr1 = VR_INITIALIZER;
3867 if (TREE_CODE (op0) == SSA_NAME)
3868 vr0 = *get_value_range (op0);
3869 else if (TREE_CODE (op0) == INTEGER_CST)
3870 set_value_range_to_value (&vr0, op0, NULL);
3871 else
3872 set_value_range_to_varying (&vr0);
3874 if (TREE_CODE (op1) == SSA_NAME)
3875 vr1 = *get_value_range (op1);
3876 else if (TREE_CODE (op1) == INTEGER_CST)
3877 set_value_range_to_value (&vr1, op1, NULL);
3878 else
3879 set_value_range_to_varying (&vr1);
3881 if (!range_int_cst_p (&vr0)
3882 || TREE_OVERFLOW (vr0.min)
3883 || TREE_OVERFLOW (vr0.max))
3885 vr0.min = vrp_val_min (TREE_TYPE (op0));
3886 vr0.max = vrp_val_max (TREE_TYPE (op0));
3888 if (!range_int_cst_p (&vr1)
3889 || TREE_OVERFLOW (vr1.min)
3890 || TREE_OVERFLOW (vr1.max))
3892 vr1.min = vrp_val_min (TREE_TYPE (op1));
3893 vr1.max = vrp_val_max (TREE_TYPE (op1));
3895 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3896 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3897 if (arith_overflowed_p (subcode, type, vr0.max,
3898 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3899 return false;
3900 if (subcode == MULT_EXPR)
3902 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3903 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3904 return false;
3906 if (*ovf)
3908 /* So far we found that there is an overflow on the boundaries.
3909 That doesn't prove that there is an overflow even for all values
3910 in between the boundaries. For that compute widest_int range
3911 of the result and see if it doesn't overlap the range of
3912 type. */
3913 widest_int wmin, wmax;
3914 widest_int w[4];
3915 int i;
3916 w[0] = wi::to_widest (vr0.min);
3917 w[1] = wi::to_widest (vr0.max);
3918 w[2] = wi::to_widest (vr1.min);
3919 w[3] = wi::to_widest (vr1.max);
3920 for (i = 0; i < 4; i++)
3922 widest_int wt;
3923 switch (subcode)
3925 case PLUS_EXPR:
3926 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3927 break;
3928 case MINUS_EXPR:
3929 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3930 break;
3931 case MULT_EXPR:
3932 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3933 break;
3934 default:
3935 gcc_unreachable ();
3937 if (i == 0)
3939 wmin = wt;
3940 wmax = wt;
3942 else
3944 wmin = wi::smin (wmin, wt);
3945 wmax = wi::smax (wmax, wt);
3948 /* The result of op0 CODE op1 is known to be in range
3949 [wmin, wmax]. */
3950 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3951 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3952 /* If all values in [wmin, wmax] are smaller than
3953 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3954 the arithmetic operation will always overflow. */
3955 if (wi::lts_p (wmax, wtmin) || wi::gts_p (wmin, wtmax))
3956 return true;
3957 return false;
3959 return true;
3962 /* Try to derive a nonnegative or nonzero range out of STMT relying
3963 primarily on generic routines in fold in conjunction with range data.
3964 Store the result in *VR */
3966 static void
3967 extract_range_basic (value_range_t *vr, gimple stmt)
3969 bool sop = false;
3970 tree type = gimple_expr_type (stmt);
3972 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3974 tree fndecl = gimple_call_fndecl (stmt), arg;
3975 int mini, maxi, zerov = 0, prec;
3977 switch (DECL_FUNCTION_CODE (fndecl))
3979 case BUILT_IN_CONSTANT_P:
3980 /* If the call is __builtin_constant_p and the argument is a
3981 function parameter resolve it to false. This avoids bogus
3982 array bound warnings.
3983 ??? We could do this as early as inlining is finished. */
3984 arg = gimple_call_arg (stmt, 0);
3985 if (TREE_CODE (arg) == SSA_NAME
3986 && SSA_NAME_IS_DEFAULT_DEF (arg)
3987 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3989 set_value_range_to_null (vr, type);
3990 return;
3992 break;
3993 /* Both __builtin_ffs* and __builtin_popcount return
3994 [0, prec]. */
3995 CASE_INT_FN (BUILT_IN_FFS):
3996 CASE_INT_FN (BUILT_IN_POPCOUNT):
3997 arg = gimple_call_arg (stmt, 0);
3998 prec = TYPE_PRECISION (TREE_TYPE (arg));
3999 mini = 0;
4000 maxi = prec;
4001 if (TREE_CODE (arg) == SSA_NAME)
4003 value_range_t *vr0 = get_value_range (arg);
4004 /* If arg is non-zero, then ffs or popcount
4005 are non-zero. */
4006 if (((vr0->type == VR_RANGE
4007 && range_includes_zero_p (vr0->min, vr0->max) == 0)
4008 || (vr0->type == VR_ANTI_RANGE
4009 && range_includes_zero_p (vr0->min, vr0->max) == 1))
4010 && !is_overflow_infinity (vr0->min)
4011 && !is_overflow_infinity (vr0->max))
4012 mini = 1;
4013 /* If some high bits are known to be zero,
4014 we can decrease the maximum. */
4015 if (vr0->type == VR_RANGE
4016 && TREE_CODE (vr0->max) == INTEGER_CST
4017 && !operand_less_p (vr0->min,
4018 build_zero_cst (TREE_TYPE (vr0->min)))
4019 && !is_overflow_infinity (vr0->max))
4020 maxi = tree_floor_log2 (vr0->max) + 1;
4022 goto bitop_builtin;
4023 /* __builtin_parity* returns [0, 1]. */
4024 CASE_INT_FN (BUILT_IN_PARITY):
4025 mini = 0;
4026 maxi = 1;
4027 goto bitop_builtin;
4028 /* __builtin_c[lt]z* return [0, prec-1], except for
4029 when the argument is 0, but that is undefined behavior.
4030 On many targets where the CLZ RTL or optab value is defined
4031 for 0 the value is prec, so include that in the range
4032 by default. */
4033 CASE_INT_FN (BUILT_IN_CLZ):
4034 arg = gimple_call_arg (stmt, 0);
4035 prec = TYPE_PRECISION (TREE_TYPE (arg));
4036 mini = 0;
4037 maxi = prec;
4038 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
4039 != CODE_FOR_nothing
4040 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
4041 zerov)
4042 /* Handle only the single common value. */
4043 && zerov != prec)
4044 /* Magic value to give up, unless vr0 proves
4045 arg is non-zero. */
4046 mini = -2;
4047 if (TREE_CODE (arg) == SSA_NAME)
4049 value_range_t *vr0 = get_value_range (arg);
4050 /* From clz of VR_RANGE minimum we can compute
4051 result maximum. */
4052 if (vr0->type == VR_RANGE
4053 && TREE_CODE (vr0->min) == INTEGER_CST
4054 && !is_overflow_infinity (vr0->min))
4056 maxi = prec - 1 - tree_floor_log2 (vr0->min);
4057 if (maxi != prec)
4058 mini = 0;
4060 else if (vr0->type == VR_ANTI_RANGE
4061 && integer_zerop (vr0->min)
4062 && !is_overflow_infinity (vr0->min))
4064 maxi = prec - 1;
4065 mini = 0;
4067 if (mini == -2)
4068 break;
4069 /* From clz of VR_RANGE maximum we can compute
4070 result minimum. */
4071 if (vr0->type == VR_RANGE
4072 && TREE_CODE (vr0->max) == INTEGER_CST
4073 && !is_overflow_infinity (vr0->max))
4075 mini = prec - 1 - tree_floor_log2 (vr0->max);
4076 if (mini == prec)
4077 break;
4080 if (mini == -2)
4081 break;
4082 goto bitop_builtin;
4083 /* __builtin_ctz* return [0, prec-1], except for
4084 when the argument is 0, but that is undefined behavior.
4085 If there is a ctz optab for this mode and
4086 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
4087 otherwise just assume 0 won't be seen. */
4088 CASE_INT_FN (BUILT_IN_CTZ):
4089 arg = gimple_call_arg (stmt, 0);
4090 prec = TYPE_PRECISION (TREE_TYPE (arg));
4091 mini = 0;
4092 maxi = prec - 1;
4093 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
4094 != CODE_FOR_nothing
4095 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
4096 zerov))
4098 /* Handle only the two common values. */
4099 if (zerov == -1)
4100 mini = -1;
4101 else if (zerov == prec)
4102 maxi = prec;
4103 else
4104 /* Magic value to give up, unless vr0 proves
4105 arg is non-zero. */
4106 mini = -2;
4108 if (TREE_CODE (arg) == SSA_NAME)
4110 value_range_t *vr0 = get_value_range (arg);
4111 /* If arg is non-zero, then use [0, prec - 1]. */
4112 if (((vr0->type == VR_RANGE
4113 && integer_nonzerop (vr0->min))
4114 || (vr0->type == VR_ANTI_RANGE
4115 && integer_zerop (vr0->min)))
4116 && !is_overflow_infinity (vr0->min))
4118 mini = 0;
4119 maxi = prec - 1;
4121 /* If some high bits are known to be zero,
4122 we can decrease the result maximum. */
4123 if (vr0->type == VR_RANGE
4124 && TREE_CODE (vr0->max) == INTEGER_CST
4125 && !is_overflow_infinity (vr0->max))
4127 maxi = tree_floor_log2 (vr0->max);
4128 /* For vr0 [0, 0] give up. */
4129 if (maxi == -1)
4130 break;
4133 if (mini == -2)
4134 break;
4135 goto bitop_builtin;
4136 /* __builtin_clrsb* returns [0, prec-1]. */
4137 CASE_INT_FN (BUILT_IN_CLRSB):
4138 arg = gimple_call_arg (stmt, 0);
4139 prec = TYPE_PRECISION (TREE_TYPE (arg));
4140 mini = 0;
4141 maxi = prec - 1;
4142 goto bitop_builtin;
4143 bitop_builtin:
4144 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
4145 build_int_cst (type, maxi), NULL);
4146 return;
4147 default:
4148 break;
4151 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
4153 enum tree_code subcode = ERROR_MARK;
4154 switch (gimple_call_internal_fn (stmt))
4156 case IFN_UBSAN_CHECK_ADD:
4157 subcode = PLUS_EXPR;
4158 break;
4159 case IFN_UBSAN_CHECK_SUB:
4160 subcode = MINUS_EXPR;
4161 break;
4162 case IFN_UBSAN_CHECK_MUL:
4163 subcode = MULT_EXPR;
4164 break;
4165 default:
4166 break;
4168 if (subcode != ERROR_MARK)
4170 bool saved_flag_wrapv = flag_wrapv;
4171 /* Pretend the arithmetics is wrapping. If there is
4172 any overflow, we'll complain, but will actually do
4173 wrapping operation. */
4174 flag_wrapv = 1;
4175 extract_range_from_binary_expr (vr, subcode, type,
4176 gimple_call_arg (stmt, 0),
4177 gimple_call_arg (stmt, 1));
4178 flag_wrapv = saved_flag_wrapv;
4180 /* If for both arguments vrp_valueize returned non-NULL,
4181 this should have been already folded and if not, it
4182 wasn't folded because of overflow. Avoid removing the
4183 UBSAN_CHECK_* calls in that case. */
4184 if (vr->type == VR_RANGE
4185 && (vr->min == vr->max
4186 || operand_equal_p (vr->min, vr->max, 0)))
4187 set_value_range_to_varying (vr);
4188 return;
4191 /* Handle extraction of the two results (result of arithmetics and
4192 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4193 internal function. */
4194 else if (is_gimple_assign (stmt)
4195 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4196 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4197 && INTEGRAL_TYPE_P (type))
4199 enum tree_code code = gimple_assign_rhs_code (stmt);
4200 tree op = gimple_assign_rhs1 (stmt);
4201 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4203 gimple g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4204 if (is_gimple_call (g) && gimple_call_internal_p (g))
4206 enum tree_code subcode = ERROR_MARK;
4207 switch (gimple_call_internal_fn (g))
4209 case IFN_ADD_OVERFLOW:
4210 subcode = PLUS_EXPR;
4211 break;
4212 case IFN_SUB_OVERFLOW:
4213 subcode = MINUS_EXPR;
4214 break;
4215 case IFN_MUL_OVERFLOW:
4216 subcode = MULT_EXPR;
4217 break;
4218 default:
4219 break;
4221 if (subcode != ERROR_MARK)
4223 tree op0 = gimple_call_arg (g, 0);
4224 tree op1 = gimple_call_arg (g, 1);
4225 if (code == IMAGPART_EXPR)
4227 bool ovf = false;
4228 if (check_for_binary_op_overflow (subcode, type,
4229 op0, op1, &ovf))
4230 set_value_range_to_value (vr,
4231 build_int_cst (type, ovf),
4232 NULL);
4233 else
4234 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4235 build_int_cst (type, 1), NULL);
4237 else if (types_compatible_p (type, TREE_TYPE (op0))
4238 && types_compatible_p (type, TREE_TYPE (op1)))
4240 bool saved_flag_wrapv = flag_wrapv;
4241 /* Pretend the arithmetics is wrapping. If there is
4242 any overflow, IMAGPART_EXPR will be set. */
4243 flag_wrapv = 1;
4244 extract_range_from_binary_expr (vr, subcode, type,
4245 op0, op1);
4246 flag_wrapv = saved_flag_wrapv;
4248 else
4250 value_range_t vr0 = VR_INITIALIZER;
4251 value_range_t vr1 = VR_INITIALIZER;
4252 bool saved_flag_wrapv = flag_wrapv;
4253 /* Pretend the arithmetics is wrapping. If there is
4254 any overflow, IMAGPART_EXPR will be set. */
4255 flag_wrapv = 1;
4256 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4257 type, op0);
4258 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4259 type, op1);
4260 extract_range_from_binary_expr_1 (vr, subcode, type,
4261 &vr0, &vr1);
4262 flag_wrapv = saved_flag_wrapv;
4264 return;
4269 if (INTEGRAL_TYPE_P (type)
4270 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4271 set_value_range_to_nonnegative (vr, type,
4272 sop || stmt_overflow_infinity (stmt));
4273 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4274 && !sop)
4275 set_value_range_to_nonnull (vr, type);
4276 else
4277 set_value_range_to_varying (vr);
4281 /* Try to compute a useful range out of assignment STMT and store it
4282 in *VR. */
4284 static void
4285 extract_range_from_assignment (value_range_t *vr, gassign *stmt)
4287 enum tree_code code = gimple_assign_rhs_code (stmt);
4289 if (code == ASSERT_EXPR)
4290 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4291 else if (code == SSA_NAME)
4292 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4293 else if (TREE_CODE_CLASS (code) == tcc_binary)
4294 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4295 gimple_expr_type (stmt),
4296 gimple_assign_rhs1 (stmt),
4297 gimple_assign_rhs2 (stmt));
4298 else if (TREE_CODE_CLASS (code) == tcc_unary)
4299 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4300 gimple_expr_type (stmt),
4301 gimple_assign_rhs1 (stmt));
4302 else if (code == COND_EXPR)
4303 extract_range_from_cond_expr (vr, stmt);
4304 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4305 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4306 gimple_expr_type (stmt),
4307 gimple_assign_rhs1 (stmt),
4308 gimple_assign_rhs2 (stmt));
4309 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4310 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4311 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4312 else
4313 set_value_range_to_varying (vr);
4315 if (vr->type == VR_VARYING)
4316 extract_range_basic (vr, stmt);
4319 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4320 would be profitable to adjust VR using scalar evolution information
4321 for VAR. If so, update VR with the new limits. */
4323 static void
4324 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
4325 gimple stmt, tree var)
4327 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4328 enum ev_direction dir;
4330 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4331 better opportunities than a regular range, but I'm not sure. */
4332 if (vr->type == VR_ANTI_RANGE)
4333 return;
4335 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4337 /* Like in PR19590, scev can return a constant function. */
4338 if (is_gimple_min_invariant (chrec))
4340 set_value_range_to_value (vr, chrec, vr->equiv);
4341 return;
4344 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4345 return;
4347 init = initial_condition_in_loop_num (chrec, loop->num);
4348 tem = op_with_constant_singleton_value_range (init);
4349 if (tem)
4350 init = tem;
4351 step = evolution_part_in_loop_num (chrec, loop->num);
4352 tem = op_with_constant_singleton_value_range (step);
4353 if (tem)
4354 step = tem;
4356 /* If STEP is symbolic, we can't know whether INIT will be the
4357 minimum or maximum value in the range. Also, unless INIT is
4358 a simple expression, compare_values and possibly other functions
4359 in tree-vrp won't be able to handle it. */
4360 if (step == NULL_TREE
4361 || !is_gimple_min_invariant (step)
4362 || !valid_value_p (init))
4363 return;
4365 dir = scev_direction (chrec);
4366 if (/* Do not adjust ranges if we do not know whether the iv increases
4367 or decreases, ... */
4368 dir == EV_DIR_UNKNOWN
4369 /* ... or if it may wrap. */
4370 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4371 true))
4372 return;
4374 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4375 negative_overflow_infinity and positive_overflow_infinity,
4376 because we have concluded that the loop probably does not
4377 wrap. */
4379 type = TREE_TYPE (var);
4380 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4381 tmin = lower_bound_in_type (type, type);
4382 else
4383 tmin = TYPE_MIN_VALUE (type);
4384 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4385 tmax = upper_bound_in_type (type, type);
4386 else
4387 tmax = TYPE_MAX_VALUE (type);
4389 /* Try to use estimated number of iterations for the loop to constrain the
4390 final value in the evolution. */
4391 if (TREE_CODE (step) == INTEGER_CST
4392 && is_gimple_val (init)
4393 && (TREE_CODE (init) != SSA_NAME
4394 || get_value_range (init)->type == VR_RANGE))
4396 widest_int nit;
4398 /* We are only entering here for loop header PHI nodes, so using
4399 the number of latch executions is the correct thing to use. */
4400 if (max_loop_iterations (loop, &nit))
4402 value_range_t maxvr = VR_INITIALIZER;
4403 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4404 bool overflow;
4406 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4407 &overflow);
4408 /* If the multiplication overflowed we can't do a meaningful
4409 adjustment. Likewise if the result doesn't fit in the type
4410 of the induction variable. For a signed type we have to
4411 check whether the result has the expected signedness which
4412 is that of the step as number of iterations is unsigned. */
4413 if (!overflow
4414 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4415 && (sgn == UNSIGNED
4416 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4418 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4419 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4420 TREE_TYPE (init), init, tem);
4421 /* Likewise if the addition did. */
4422 if (maxvr.type == VR_RANGE)
4424 tmin = maxvr.min;
4425 tmax = maxvr.max;
4431 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4433 min = tmin;
4434 max = tmax;
4436 /* For VARYING or UNDEFINED ranges, just about anything we get
4437 from scalar evolutions should be better. */
4439 if (dir == EV_DIR_DECREASES)
4440 max = init;
4441 else
4442 min = init;
4444 else if (vr->type == VR_RANGE)
4446 min = vr->min;
4447 max = vr->max;
4449 if (dir == EV_DIR_DECREASES)
4451 /* INIT is the maximum value. If INIT is lower than VR->MAX
4452 but no smaller than VR->MIN, set VR->MAX to INIT. */
4453 if (compare_values (init, max) == -1)
4454 max = init;
4456 /* According to the loop information, the variable does not
4457 overflow. If we think it does, probably because of an
4458 overflow due to arithmetic on a different INF value,
4459 reset now. */
4460 if (is_negative_overflow_infinity (min)
4461 || compare_values (min, tmin) == -1)
4462 min = tmin;
4465 else
4467 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4468 if (compare_values (init, min) == 1)
4469 min = init;
4471 if (is_positive_overflow_infinity (max)
4472 || compare_values (tmax, max) == -1)
4473 max = tmax;
4476 else
4477 return;
4479 /* If we just created an invalid range with the minimum
4480 greater than the maximum, we fail conservatively.
4481 This should happen only in unreachable
4482 parts of code, or for invalid programs. */
4483 if (compare_values (min, max) == 1
4484 || (is_negative_overflow_infinity (min)
4485 && is_positive_overflow_infinity (max)))
4486 return;
4488 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4492 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4494 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4495 all the values in the ranges.
4497 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4499 - Return NULL_TREE if it is not always possible to determine the
4500 value of the comparison.
4502 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4503 overflow infinity was used in the test. */
4506 static tree
4507 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4508 bool *strict_overflow_p)
4510 /* VARYING or UNDEFINED ranges cannot be compared. */
4511 if (vr0->type == VR_VARYING
4512 || vr0->type == VR_UNDEFINED
4513 || vr1->type == VR_VARYING
4514 || vr1->type == VR_UNDEFINED)
4515 return NULL_TREE;
4517 /* Anti-ranges need to be handled separately. */
4518 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4520 /* If both are anti-ranges, then we cannot compute any
4521 comparison. */
4522 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4523 return NULL_TREE;
4525 /* These comparisons are never statically computable. */
4526 if (comp == GT_EXPR
4527 || comp == GE_EXPR
4528 || comp == LT_EXPR
4529 || comp == LE_EXPR)
4530 return NULL_TREE;
4532 /* Equality can be computed only between a range and an
4533 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4534 if (vr0->type == VR_RANGE)
4536 /* To simplify processing, make VR0 the anti-range. */
4537 value_range_t *tmp = vr0;
4538 vr0 = vr1;
4539 vr1 = tmp;
4542 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4544 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4545 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4546 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4548 return NULL_TREE;
4551 if (!usable_range_p (vr0, strict_overflow_p)
4552 || !usable_range_p (vr1, strict_overflow_p))
4553 return NULL_TREE;
4555 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4556 operands around and change the comparison code. */
4557 if (comp == GT_EXPR || comp == GE_EXPR)
4559 value_range_t *tmp;
4560 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4561 tmp = vr0;
4562 vr0 = vr1;
4563 vr1 = tmp;
4566 if (comp == EQ_EXPR)
4568 /* Equality may only be computed if both ranges represent
4569 exactly one value. */
4570 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4571 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4573 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4574 strict_overflow_p);
4575 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4576 strict_overflow_p);
4577 if (cmp_min == 0 && cmp_max == 0)
4578 return boolean_true_node;
4579 else if (cmp_min != -2 && cmp_max != -2)
4580 return boolean_false_node;
4582 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4583 else if (compare_values_warnv (vr0->min, vr1->max,
4584 strict_overflow_p) == 1
4585 || compare_values_warnv (vr1->min, vr0->max,
4586 strict_overflow_p) == 1)
4587 return boolean_false_node;
4589 return NULL_TREE;
4591 else if (comp == NE_EXPR)
4593 int cmp1, cmp2;
4595 /* If VR0 is completely to the left or completely to the right
4596 of VR1, they are always different. Notice that we need to
4597 make sure that both comparisons yield similar results to
4598 avoid comparing values that cannot be compared at
4599 compile-time. */
4600 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4601 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4602 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4603 return boolean_true_node;
4605 /* If VR0 and VR1 represent a single value and are identical,
4606 return false. */
4607 else if (compare_values_warnv (vr0->min, vr0->max,
4608 strict_overflow_p) == 0
4609 && compare_values_warnv (vr1->min, vr1->max,
4610 strict_overflow_p) == 0
4611 && compare_values_warnv (vr0->min, vr1->min,
4612 strict_overflow_p) == 0
4613 && compare_values_warnv (vr0->max, vr1->max,
4614 strict_overflow_p) == 0)
4615 return boolean_false_node;
4617 /* Otherwise, they may or may not be different. */
4618 else
4619 return NULL_TREE;
4621 else if (comp == LT_EXPR || comp == LE_EXPR)
4623 int tst;
4625 /* If VR0 is to the left of VR1, return true. */
4626 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4627 if ((comp == LT_EXPR && tst == -1)
4628 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4630 if (overflow_infinity_range_p (vr0)
4631 || overflow_infinity_range_p (vr1))
4632 *strict_overflow_p = true;
4633 return boolean_true_node;
4636 /* If VR0 is to the right of VR1, return false. */
4637 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4638 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4639 || (comp == LE_EXPR && tst == 1))
4641 if (overflow_infinity_range_p (vr0)
4642 || overflow_infinity_range_p (vr1))
4643 *strict_overflow_p = true;
4644 return boolean_false_node;
4647 /* Otherwise, we don't know. */
4648 return NULL_TREE;
4651 gcc_unreachable ();
4655 /* Given a value range VR, a value VAL and a comparison code COMP, return
4656 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4657 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4658 always returns false. Return NULL_TREE if it is not always
4659 possible to determine the value of the comparison. Also set
4660 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4661 infinity was used in the test. */
4663 static tree
4664 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4665 bool *strict_overflow_p)
4667 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4668 return NULL_TREE;
4670 /* Anti-ranges need to be handled separately. */
4671 if (vr->type == VR_ANTI_RANGE)
4673 /* For anti-ranges, the only predicates that we can compute at
4674 compile time are equality and inequality. */
4675 if (comp == GT_EXPR
4676 || comp == GE_EXPR
4677 || comp == LT_EXPR
4678 || comp == LE_EXPR)
4679 return NULL_TREE;
4681 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4682 if (value_inside_range (val, vr->min, vr->max) == 1)
4683 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4685 return NULL_TREE;
4688 if (!usable_range_p (vr, strict_overflow_p))
4689 return NULL_TREE;
4691 if (comp == EQ_EXPR)
4693 /* EQ_EXPR may only be computed if VR represents exactly
4694 one value. */
4695 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4697 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4698 if (cmp == 0)
4699 return boolean_true_node;
4700 else if (cmp == -1 || cmp == 1 || cmp == 2)
4701 return boolean_false_node;
4703 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4704 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4705 return boolean_false_node;
4707 return NULL_TREE;
4709 else if (comp == NE_EXPR)
4711 /* If VAL is not inside VR, then they are always different. */
4712 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4713 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4714 return boolean_true_node;
4716 /* If VR represents exactly one value equal to VAL, then return
4717 false. */
4718 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4719 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4720 return boolean_false_node;
4722 /* Otherwise, they may or may not be different. */
4723 return NULL_TREE;
4725 else if (comp == LT_EXPR || comp == LE_EXPR)
4727 int tst;
4729 /* If VR is to the left of VAL, return true. */
4730 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4731 if ((comp == LT_EXPR && tst == -1)
4732 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4734 if (overflow_infinity_range_p (vr))
4735 *strict_overflow_p = true;
4736 return boolean_true_node;
4739 /* If VR is to the right of VAL, return false. */
4740 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4741 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4742 || (comp == LE_EXPR && tst == 1))
4744 if (overflow_infinity_range_p (vr))
4745 *strict_overflow_p = true;
4746 return boolean_false_node;
4749 /* Otherwise, we don't know. */
4750 return NULL_TREE;
4752 else if (comp == GT_EXPR || comp == GE_EXPR)
4754 int tst;
4756 /* If VR is to the right of VAL, return true. */
4757 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4758 if ((comp == GT_EXPR && tst == 1)
4759 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4761 if (overflow_infinity_range_p (vr))
4762 *strict_overflow_p = true;
4763 return boolean_true_node;
4766 /* If VR is to the left of VAL, return false. */
4767 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4768 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4769 || (comp == GE_EXPR && tst == -1))
4771 if (overflow_infinity_range_p (vr))
4772 *strict_overflow_p = true;
4773 return boolean_false_node;
4776 /* Otherwise, we don't know. */
4777 return NULL_TREE;
4780 gcc_unreachable ();
4784 /* Debugging dumps. */
4786 void dump_value_range (FILE *, value_range_t *);
4787 void debug_value_range (value_range_t *);
4788 void dump_all_value_ranges (FILE *);
4789 void debug_all_value_ranges (void);
4790 void dump_vr_equiv (FILE *, bitmap);
4791 void debug_vr_equiv (bitmap);
4794 /* Dump value range VR to FILE. */
4796 void
4797 dump_value_range (FILE *file, value_range_t *vr)
4799 if (vr == NULL)
4800 fprintf (file, "[]");
4801 else if (vr->type == VR_UNDEFINED)
4802 fprintf (file, "UNDEFINED");
4803 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4805 tree type = TREE_TYPE (vr->min);
4807 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4809 if (is_negative_overflow_infinity (vr->min))
4810 fprintf (file, "-INF(OVF)");
4811 else if (INTEGRAL_TYPE_P (type)
4812 && !TYPE_UNSIGNED (type)
4813 && vrp_val_is_min (vr->min))
4814 fprintf (file, "-INF");
4815 else
4816 print_generic_expr (file, vr->min, 0);
4818 fprintf (file, ", ");
4820 if (is_positive_overflow_infinity (vr->max))
4821 fprintf (file, "+INF(OVF)");
4822 else if (INTEGRAL_TYPE_P (type)
4823 && vrp_val_is_max (vr->max))
4824 fprintf (file, "+INF");
4825 else
4826 print_generic_expr (file, vr->max, 0);
4828 fprintf (file, "]");
4830 if (vr->equiv)
4832 bitmap_iterator bi;
4833 unsigned i, c = 0;
4835 fprintf (file, " EQUIVALENCES: { ");
4837 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4839 print_generic_expr (file, ssa_name (i), 0);
4840 fprintf (file, " ");
4841 c++;
4844 fprintf (file, "} (%u elements)", c);
4847 else if (vr->type == VR_VARYING)
4848 fprintf (file, "VARYING");
4849 else
4850 fprintf (file, "INVALID RANGE");
4854 /* Dump value range VR to stderr. */
4856 DEBUG_FUNCTION void
4857 debug_value_range (value_range_t *vr)
4859 dump_value_range (stderr, vr);
4860 fprintf (stderr, "\n");
4864 /* Dump value ranges of all SSA_NAMEs to FILE. */
4866 void
4867 dump_all_value_ranges (FILE *file)
4869 size_t i;
4871 for (i = 0; i < num_vr_values; i++)
4873 if (vr_value[i])
4875 print_generic_expr (file, ssa_name (i), 0);
4876 fprintf (file, ": ");
4877 dump_value_range (file, vr_value[i]);
4878 fprintf (file, "\n");
4882 fprintf (file, "\n");
4886 /* Dump all value ranges to stderr. */
4888 DEBUG_FUNCTION void
4889 debug_all_value_ranges (void)
4891 dump_all_value_ranges (stderr);
4895 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4896 create a new SSA name N and return the assertion assignment
4897 'N = ASSERT_EXPR <V, V OP W>'. */
4899 static gimple
4900 build_assert_expr_for (tree cond, tree v)
4902 tree a;
4903 gassign *assertion;
4905 gcc_assert (TREE_CODE (v) == SSA_NAME
4906 && COMPARISON_CLASS_P (cond));
4908 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4909 assertion = gimple_build_assign (NULL_TREE, a);
4911 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4912 operand of the ASSERT_EXPR. Create it so the new name and the old one
4913 are registered in the replacement table so that we can fix the SSA web
4914 after adding all the ASSERT_EXPRs. */
4915 create_new_def_for (v, assertion, NULL);
4917 return assertion;
4921 /* Return false if EXPR is a predicate expression involving floating
4922 point values. */
4924 static inline bool
4925 fp_predicate (gimple stmt)
4927 GIMPLE_CHECK (stmt, GIMPLE_COND);
4929 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4932 /* If the range of values taken by OP can be inferred after STMT executes,
4933 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4934 describes the inferred range. Return true if a range could be
4935 inferred. */
4937 static bool
4938 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4940 *val_p = NULL_TREE;
4941 *comp_code_p = ERROR_MARK;
4943 /* Do not attempt to infer anything in names that flow through
4944 abnormal edges. */
4945 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4946 return false;
4948 /* Similarly, don't infer anything from statements that may throw
4949 exceptions. ??? Relax this requirement? */
4950 if (stmt_could_throw_p (stmt))
4951 return false;
4953 /* If STMT is the last statement of a basic block with no normal
4954 successors, there is no point inferring anything about any of its
4955 operands. We would not be able to find a proper insertion point
4956 for the assertion, anyway. */
4957 if (stmt_ends_bb_p (stmt))
4959 edge_iterator ei;
4960 edge e;
4962 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4963 if (!(e->flags & EDGE_ABNORMAL))
4964 break;
4965 if (e == NULL)
4966 return false;
4969 if (infer_nonnull_range (stmt, op, true, true))
4971 *val_p = build_int_cst (TREE_TYPE (op), 0);
4972 *comp_code_p = NE_EXPR;
4973 return true;
4976 return false;
4980 void dump_asserts_for (FILE *, tree);
4981 void debug_asserts_for (tree);
4982 void dump_all_asserts (FILE *);
4983 void debug_all_asserts (void);
4985 /* Dump all the registered assertions for NAME to FILE. */
4987 void
4988 dump_asserts_for (FILE *file, tree name)
4990 assert_locus_t loc;
4992 fprintf (file, "Assertions to be inserted for ");
4993 print_generic_expr (file, name, 0);
4994 fprintf (file, "\n");
4996 loc = asserts_for[SSA_NAME_VERSION (name)];
4997 while (loc)
4999 fprintf (file, "\t");
5000 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
5001 fprintf (file, "\n\tBB #%d", loc->bb->index);
5002 if (loc->e)
5004 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
5005 loc->e->dest->index);
5006 dump_edge_info (file, loc->e, dump_flags, 0);
5008 fprintf (file, "\n\tPREDICATE: ");
5009 print_generic_expr (file, name, 0);
5010 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
5011 print_generic_expr (file, loc->val, 0);
5012 fprintf (file, "\n\n");
5013 loc = loc->next;
5016 fprintf (file, "\n");
5020 /* Dump all the registered assertions for NAME to stderr. */
5022 DEBUG_FUNCTION void
5023 debug_asserts_for (tree name)
5025 dump_asserts_for (stderr, name);
5029 /* Dump all the registered assertions for all the names to FILE. */
5031 void
5032 dump_all_asserts (FILE *file)
5034 unsigned i;
5035 bitmap_iterator bi;
5037 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
5038 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
5039 dump_asserts_for (file, ssa_name (i));
5040 fprintf (file, "\n");
5044 /* Dump all the registered assertions for all the names to stderr. */
5046 DEBUG_FUNCTION void
5047 debug_all_asserts (void)
5049 dump_all_asserts (stderr);
5053 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
5054 'EXPR COMP_CODE VAL' at a location that dominates block BB or
5055 E->DEST, then register this location as a possible insertion point
5056 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
5058 BB, E and SI provide the exact insertion point for the new
5059 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
5060 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
5061 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
5062 must not be NULL. */
5064 static void
5065 register_new_assert_for (tree name, tree expr,
5066 enum tree_code comp_code,
5067 tree val,
5068 basic_block bb,
5069 edge e,
5070 gimple_stmt_iterator si)
5072 assert_locus_t n, loc, last_loc;
5073 basic_block dest_bb;
5075 gcc_checking_assert (bb == NULL || e == NULL);
5077 if (e == NULL)
5078 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
5079 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
5081 /* Never build an assert comparing against an integer constant with
5082 TREE_OVERFLOW set. This confuses our undefined overflow warning
5083 machinery. */
5084 if (TREE_OVERFLOW_P (val))
5085 val = drop_tree_overflow (val);
5087 /* The new assertion A will be inserted at BB or E. We need to
5088 determine if the new location is dominated by a previously
5089 registered location for A. If we are doing an edge insertion,
5090 assume that A will be inserted at E->DEST. Note that this is not
5091 necessarily true.
5093 If E is a critical edge, it will be split. But even if E is
5094 split, the new block will dominate the same set of blocks that
5095 E->DEST dominates.
5097 The reverse, however, is not true, blocks dominated by E->DEST
5098 will not be dominated by the new block created to split E. So,
5099 if the insertion location is on a critical edge, we will not use
5100 the new location to move another assertion previously registered
5101 at a block dominated by E->DEST. */
5102 dest_bb = (bb) ? bb : e->dest;
5104 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5105 VAL at a block dominating DEST_BB, then we don't need to insert a new
5106 one. Similarly, if the same assertion already exists at a block
5107 dominated by DEST_BB and the new location is not on a critical
5108 edge, then update the existing location for the assertion (i.e.,
5109 move the assertion up in the dominance tree).
5111 Note, this is implemented as a simple linked list because there
5112 should not be more than a handful of assertions registered per
5113 name. If this becomes a performance problem, a table hashed by
5114 COMP_CODE and VAL could be implemented. */
5115 loc = asserts_for[SSA_NAME_VERSION (name)];
5116 last_loc = loc;
5117 while (loc)
5119 if (loc->comp_code == comp_code
5120 && (loc->val == val
5121 || operand_equal_p (loc->val, val, 0))
5122 && (loc->expr == expr
5123 || operand_equal_p (loc->expr, expr, 0)))
5125 /* If E is not a critical edge and DEST_BB
5126 dominates the existing location for the assertion, move
5127 the assertion up in the dominance tree by updating its
5128 location information. */
5129 if ((e == NULL || !EDGE_CRITICAL_P (e))
5130 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5132 loc->bb = dest_bb;
5133 loc->e = e;
5134 loc->si = si;
5135 return;
5139 /* Update the last node of the list and move to the next one. */
5140 last_loc = loc;
5141 loc = loc->next;
5144 /* If we didn't find an assertion already registered for
5145 NAME COMP_CODE VAL, add a new one at the end of the list of
5146 assertions associated with NAME. */
5147 n = XNEW (struct assert_locus_d);
5148 n->bb = dest_bb;
5149 n->e = e;
5150 n->si = si;
5151 n->comp_code = comp_code;
5152 n->val = val;
5153 n->expr = expr;
5154 n->next = NULL;
5156 if (last_loc)
5157 last_loc->next = n;
5158 else
5159 asserts_for[SSA_NAME_VERSION (name)] = n;
5161 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5164 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5165 Extract a suitable test code and value and store them into *CODE_P and
5166 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5168 If no extraction was possible, return FALSE, otherwise return TRUE.
5170 If INVERT is true, then we invert the result stored into *CODE_P. */
5172 static bool
5173 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5174 tree cond_op0, tree cond_op1,
5175 bool invert, enum tree_code *code_p,
5176 tree *val_p)
5178 enum tree_code comp_code;
5179 tree val;
5181 /* Otherwise, we have a comparison of the form NAME COMP VAL
5182 or VAL COMP NAME. */
5183 if (name == cond_op1)
5185 /* If the predicate is of the form VAL COMP NAME, flip
5186 COMP around because we need to register NAME as the
5187 first operand in the predicate. */
5188 comp_code = swap_tree_comparison (cond_code);
5189 val = cond_op0;
5191 else
5193 /* The comparison is of the form NAME COMP VAL, so the
5194 comparison code remains unchanged. */
5195 comp_code = cond_code;
5196 val = cond_op1;
5199 /* Invert the comparison code as necessary. */
5200 if (invert)
5201 comp_code = invert_tree_comparison (comp_code, 0);
5203 /* VRP does not handle float types. */
5204 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
5205 return false;
5207 /* Do not register always-false predicates.
5208 FIXME: this works around a limitation in fold() when dealing with
5209 enumerations. Given 'enum { N1, N2 } x;', fold will not
5210 fold 'if (x > N2)' to 'if (0)'. */
5211 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5212 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5214 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5215 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5217 if (comp_code == GT_EXPR
5218 && (!max
5219 || compare_values (val, max) == 0))
5220 return false;
5222 if (comp_code == LT_EXPR
5223 && (!min
5224 || compare_values (val, min) == 0))
5225 return false;
5227 *code_p = comp_code;
5228 *val_p = val;
5229 return true;
5232 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5233 (otherwise return VAL). VAL and MASK must be zero-extended for
5234 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5235 (to transform signed values into unsigned) and at the end xor
5236 SGNBIT back. */
5238 static wide_int
5239 masked_increment (const wide_int &val_in, const wide_int &mask,
5240 const wide_int &sgnbit, unsigned int prec)
5242 wide_int bit = wi::one (prec), res;
5243 unsigned int i;
5245 wide_int val = val_in ^ sgnbit;
5246 for (i = 0; i < prec; i++, bit += bit)
5248 res = mask;
5249 if ((res & bit) == 0)
5250 continue;
5251 res = bit - 1;
5252 res = (val + bit).and_not (res);
5253 res &= mask;
5254 if (wi::gtu_p (res, val))
5255 return res ^ sgnbit;
5257 return val ^ sgnbit;
5260 /* Try to register an edge assertion for SSA name NAME on edge E for
5261 the condition COND contributing to the conditional jump pointed to by BSI.
5262 Invert the condition COND if INVERT is true. */
5264 static void
5265 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5266 enum tree_code cond_code,
5267 tree cond_op0, tree cond_op1, bool invert)
5269 tree val;
5270 enum tree_code comp_code;
5272 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5273 cond_op0,
5274 cond_op1,
5275 invert, &comp_code, &val))
5276 return;
5278 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5279 reachable from E. */
5280 if (live_on_edge (e, name)
5281 && !has_single_use (name))
5282 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5284 /* In the case of NAME <= CST and NAME being defined as
5285 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5286 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5287 This catches range and anti-range tests. */
5288 if ((comp_code == LE_EXPR
5289 || comp_code == GT_EXPR)
5290 && TREE_CODE (val) == INTEGER_CST
5291 && TYPE_UNSIGNED (TREE_TYPE (val)))
5293 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5294 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5296 /* Extract CST2 from the (optional) addition. */
5297 if (is_gimple_assign (def_stmt)
5298 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5300 name2 = gimple_assign_rhs1 (def_stmt);
5301 cst2 = gimple_assign_rhs2 (def_stmt);
5302 if (TREE_CODE (name2) == SSA_NAME
5303 && TREE_CODE (cst2) == INTEGER_CST)
5304 def_stmt = SSA_NAME_DEF_STMT (name2);
5307 /* Extract NAME2 from the (optional) sign-changing cast. */
5308 if (gimple_assign_cast_p (def_stmt))
5310 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5311 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5312 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5313 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5314 name3 = gimple_assign_rhs1 (def_stmt);
5317 /* If name3 is used later, create an ASSERT_EXPR for it. */
5318 if (name3 != NULL_TREE
5319 && TREE_CODE (name3) == SSA_NAME
5320 && (cst2 == NULL_TREE
5321 || TREE_CODE (cst2) == INTEGER_CST)
5322 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5323 && live_on_edge (e, name3)
5324 && !has_single_use (name3))
5326 tree tmp;
5328 /* Build an expression for the range test. */
5329 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5330 if (cst2 != NULL_TREE)
5331 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5333 if (dump_file)
5335 fprintf (dump_file, "Adding assert for ");
5336 print_generic_expr (dump_file, name3, 0);
5337 fprintf (dump_file, " from ");
5338 print_generic_expr (dump_file, tmp, 0);
5339 fprintf (dump_file, "\n");
5342 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5345 /* If name2 is used later, create an ASSERT_EXPR for it. */
5346 if (name2 != NULL_TREE
5347 && TREE_CODE (name2) == SSA_NAME
5348 && TREE_CODE (cst2) == INTEGER_CST
5349 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5350 && live_on_edge (e, name2)
5351 && !has_single_use (name2))
5353 tree tmp;
5355 /* Build an expression for the range test. */
5356 tmp = name2;
5357 if (TREE_TYPE (name) != TREE_TYPE (name2))
5358 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5359 if (cst2 != NULL_TREE)
5360 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5362 if (dump_file)
5364 fprintf (dump_file, "Adding assert for ");
5365 print_generic_expr (dump_file, name2, 0);
5366 fprintf (dump_file, " from ");
5367 print_generic_expr (dump_file, tmp, 0);
5368 fprintf (dump_file, "\n");
5371 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5375 /* In the case of post-in/decrement tests like if (i++) ... and uses
5376 of the in/decremented value on the edge the extra name we want to
5377 assert for is not on the def chain of the name compared. Instead
5378 it is in the set of use stmts. */
5379 if ((comp_code == NE_EXPR
5380 || comp_code == EQ_EXPR)
5381 && TREE_CODE (val) == INTEGER_CST)
5383 imm_use_iterator ui;
5384 gimple use_stmt;
5385 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5387 /* Cut off to use-stmts that are in the predecessor. */
5388 if (gimple_bb (use_stmt) != e->src)
5389 continue;
5391 if (!is_gimple_assign (use_stmt))
5392 continue;
5394 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5395 if (code != PLUS_EXPR
5396 && code != MINUS_EXPR)
5397 continue;
5399 tree cst = gimple_assign_rhs2 (use_stmt);
5400 if (TREE_CODE (cst) != INTEGER_CST)
5401 continue;
5403 tree name2 = gimple_assign_lhs (use_stmt);
5404 if (live_on_edge (e, name2))
5406 cst = int_const_binop (code, val, cst);
5407 register_new_assert_for (name2, name2, comp_code, cst,
5408 NULL, e, bsi);
5413 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5414 && TREE_CODE (val) == INTEGER_CST)
5416 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5417 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5418 tree val2 = NULL_TREE;
5419 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5420 wide_int mask = wi::zero (prec);
5421 unsigned int nprec = prec;
5422 enum tree_code rhs_code = ERROR_MARK;
5424 if (is_gimple_assign (def_stmt))
5425 rhs_code = gimple_assign_rhs_code (def_stmt);
5427 /* Add asserts for NAME cmp CST and NAME being defined
5428 as NAME = (int) NAME2. */
5429 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5430 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5431 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5432 && gimple_assign_cast_p (def_stmt))
5434 name2 = gimple_assign_rhs1 (def_stmt);
5435 if (CONVERT_EXPR_CODE_P (rhs_code)
5436 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5437 && TYPE_UNSIGNED (TREE_TYPE (name2))
5438 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5439 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5440 || !tree_int_cst_equal (val,
5441 TYPE_MIN_VALUE (TREE_TYPE (val))))
5442 && live_on_edge (e, name2)
5443 && !has_single_use (name2))
5445 tree tmp, cst;
5446 enum tree_code new_comp_code = comp_code;
5448 cst = fold_convert (TREE_TYPE (name2),
5449 TYPE_MIN_VALUE (TREE_TYPE (val)));
5450 /* Build an expression for the range test. */
5451 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5452 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5453 fold_convert (TREE_TYPE (name2), val));
5454 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5456 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5457 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5458 build_int_cst (TREE_TYPE (name2), 1));
5461 if (dump_file)
5463 fprintf (dump_file, "Adding assert for ");
5464 print_generic_expr (dump_file, name2, 0);
5465 fprintf (dump_file, " from ");
5466 print_generic_expr (dump_file, tmp, 0);
5467 fprintf (dump_file, "\n");
5470 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5471 e, bsi);
5475 /* Add asserts for NAME cmp CST and NAME being defined as
5476 NAME = NAME2 >> CST2.
5478 Extract CST2 from the right shift. */
5479 if (rhs_code == RSHIFT_EXPR)
5481 name2 = gimple_assign_rhs1 (def_stmt);
5482 cst2 = gimple_assign_rhs2 (def_stmt);
5483 if (TREE_CODE (name2) == SSA_NAME
5484 && tree_fits_uhwi_p (cst2)
5485 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5486 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5487 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5488 && live_on_edge (e, name2)
5489 && !has_single_use (name2))
5491 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5492 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5495 if (val2 != NULL_TREE
5496 && TREE_CODE (val2) == INTEGER_CST
5497 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5498 TREE_TYPE (val),
5499 val2, cst2), val))
5501 enum tree_code new_comp_code = comp_code;
5502 tree tmp, new_val;
5504 tmp = name2;
5505 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5507 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5509 tree type = build_nonstandard_integer_type (prec, 1);
5510 tmp = build1 (NOP_EXPR, type, name2);
5511 val2 = fold_convert (type, val2);
5513 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5514 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5515 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5517 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5519 wide_int minval
5520 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5521 new_val = val2;
5522 if (minval == new_val)
5523 new_val = NULL_TREE;
5525 else
5527 wide_int maxval
5528 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5529 mask |= val2;
5530 if (mask == maxval)
5531 new_val = NULL_TREE;
5532 else
5533 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5536 if (new_val)
5538 if (dump_file)
5540 fprintf (dump_file, "Adding assert for ");
5541 print_generic_expr (dump_file, name2, 0);
5542 fprintf (dump_file, " from ");
5543 print_generic_expr (dump_file, tmp, 0);
5544 fprintf (dump_file, "\n");
5547 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5548 NULL, e, bsi);
5552 /* Add asserts for NAME cmp CST and NAME being defined as
5553 NAME = NAME2 & CST2.
5555 Extract CST2 from the and.
5557 Also handle
5558 NAME = (unsigned) NAME2;
5559 casts where NAME's type is unsigned and has smaller precision
5560 than NAME2's type as if it was NAME = NAME2 & MASK. */
5561 names[0] = NULL_TREE;
5562 names[1] = NULL_TREE;
5563 cst2 = NULL_TREE;
5564 if (rhs_code == BIT_AND_EXPR
5565 || (CONVERT_EXPR_CODE_P (rhs_code)
5566 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5567 && TYPE_UNSIGNED (TREE_TYPE (val))
5568 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5569 > prec))
5571 name2 = gimple_assign_rhs1 (def_stmt);
5572 if (rhs_code == BIT_AND_EXPR)
5573 cst2 = gimple_assign_rhs2 (def_stmt);
5574 else
5576 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5577 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5579 if (TREE_CODE (name2) == SSA_NAME
5580 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5581 && TREE_CODE (cst2) == INTEGER_CST
5582 && !integer_zerop (cst2)
5583 && (nprec > 1
5584 || TYPE_UNSIGNED (TREE_TYPE (val))))
5586 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5587 if (gimple_assign_cast_p (def_stmt2))
5589 names[1] = gimple_assign_rhs1 (def_stmt2);
5590 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5591 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5592 || (TYPE_PRECISION (TREE_TYPE (name2))
5593 != TYPE_PRECISION (TREE_TYPE (names[1])))
5594 || !live_on_edge (e, names[1])
5595 || has_single_use (names[1]))
5596 names[1] = NULL_TREE;
5598 if (live_on_edge (e, name2)
5599 && !has_single_use (name2))
5600 names[0] = name2;
5603 if (names[0] || names[1])
5605 wide_int minv, maxv, valv, cst2v;
5606 wide_int tem, sgnbit;
5607 bool valid_p = false, valn, cst2n;
5608 enum tree_code ccode = comp_code;
5610 valv = wide_int::from (val, nprec, UNSIGNED);
5611 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5612 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5613 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5614 /* If CST2 doesn't have most significant bit set,
5615 but VAL is negative, we have comparison like
5616 if ((x & 0x123) > -4) (always true). Just give up. */
5617 if (!cst2n && valn)
5618 ccode = ERROR_MARK;
5619 if (cst2n)
5620 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5621 else
5622 sgnbit = wi::zero (nprec);
5623 minv = valv & cst2v;
5624 switch (ccode)
5626 case EQ_EXPR:
5627 /* Minimum unsigned value for equality is VAL & CST2
5628 (should be equal to VAL, otherwise we probably should
5629 have folded the comparison into false) and
5630 maximum unsigned value is VAL | ~CST2. */
5631 maxv = valv | ~cst2v;
5632 valid_p = true;
5633 break;
5635 case NE_EXPR:
5636 tem = valv | ~cst2v;
5637 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5638 if (valv == 0)
5640 cst2n = false;
5641 sgnbit = wi::zero (nprec);
5642 goto gt_expr;
5644 /* If (VAL | ~CST2) is all ones, handle it as
5645 (X & CST2) < VAL. */
5646 if (tem == -1)
5648 cst2n = false;
5649 valn = false;
5650 sgnbit = wi::zero (nprec);
5651 goto lt_expr;
5653 if (!cst2n && wi::neg_p (cst2v))
5654 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5655 if (sgnbit != 0)
5657 if (valv == sgnbit)
5659 cst2n = true;
5660 valn = true;
5661 goto gt_expr;
5663 if (tem == wi::mask (nprec - 1, false, nprec))
5665 cst2n = true;
5666 goto lt_expr;
5668 if (!cst2n)
5669 sgnbit = wi::zero (nprec);
5671 break;
5673 case GE_EXPR:
5674 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5675 is VAL and maximum unsigned value is ~0. For signed
5676 comparison, if CST2 doesn't have most significant bit
5677 set, handle it similarly. If CST2 has MSB set,
5678 the minimum is the same, and maximum is ~0U/2. */
5679 if (minv != valv)
5681 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5682 VAL. */
5683 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5684 if (minv == valv)
5685 break;
5687 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5688 valid_p = true;
5689 break;
5691 case GT_EXPR:
5692 gt_expr:
5693 /* Find out smallest MINV where MINV > VAL
5694 && (MINV & CST2) == MINV, if any. If VAL is signed and
5695 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5696 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5697 if (minv == valv)
5698 break;
5699 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5700 valid_p = true;
5701 break;
5703 case LE_EXPR:
5704 /* Minimum unsigned value for <= is 0 and maximum
5705 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5706 Otherwise, find smallest VAL2 where VAL2 > VAL
5707 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5708 as maximum.
5709 For signed comparison, if CST2 doesn't have most
5710 significant bit set, handle it similarly. If CST2 has
5711 MSB set, the maximum is the same and minimum is INT_MIN. */
5712 if (minv == valv)
5713 maxv = valv;
5714 else
5716 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5717 if (maxv == valv)
5718 break;
5719 maxv -= 1;
5721 maxv |= ~cst2v;
5722 minv = sgnbit;
5723 valid_p = true;
5724 break;
5726 case LT_EXPR:
5727 lt_expr:
5728 /* Minimum unsigned value for < is 0 and maximum
5729 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5730 Otherwise, find smallest VAL2 where VAL2 > VAL
5731 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5732 as maximum.
5733 For signed comparison, if CST2 doesn't have most
5734 significant bit set, handle it similarly. If CST2 has
5735 MSB set, the maximum is the same and minimum is INT_MIN. */
5736 if (minv == valv)
5738 if (valv == sgnbit)
5739 break;
5740 maxv = valv;
5742 else
5744 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5745 if (maxv == valv)
5746 break;
5748 maxv -= 1;
5749 maxv |= ~cst2v;
5750 minv = sgnbit;
5751 valid_p = true;
5752 break;
5754 default:
5755 break;
5757 if (valid_p
5758 && (maxv - minv) != -1)
5760 tree tmp, new_val, type;
5761 int i;
5763 for (i = 0; i < 2; i++)
5764 if (names[i])
5766 wide_int maxv2 = maxv;
5767 tmp = names[i];
5768 type = TREE_TYPE (names[i]);
5769 if (!TYPE_UNSIGNED (type))
5771 type = build_nonstandard_integer_type (nprec, 1);
5772 tmp = build1 (NOP_EXPR, type, names[i]);
5774 if (minv != 0)
5776 tmp = build2 (PLUS_EXPR, type, tmp,
5777 wide_int_to_tree (type, -minv));
5778 maxv2 = maxv - minv;
5780 new_val = wide_int_to_tree (type, maxv2);
5782 if (dump_file)
5784 fprintf (dump_file, "Adding assert for ");
5785 print_generic_expr (dump_file, names[i], 0);
5786 fprintf (dump_file, " from ");
5787 print_generic_expr (dump_file, tmp, 0);
5788 fprintf (dump_file, "\n");
5791 register_new_assert_for (names[i], tmp, LE_EXPR,
5792 new_val, NULL, e, bsi);
5799 /* OP is an operand of a truth value expression which is known to have
5800 a particular value. Register any asserts for OP and for any
5801 operands in OP's defining statement.
5803 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5804 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5806 static void
5807 register_edge_assert_for_1 (tree op, enum tree_code code,
5808 edge e, gimple_stmt_iterator bsi)
5810 gimple op_def;
5811 tree val;
5812 enum tree_code rhs_code;
5814 /* We only care about SSA_NAMEs. */
5815 if (TREE_CODE (op) != SSA_NAME)
5816 return;
5818 /* We know that OP will have a zero or nonzero value. If OP is used
5819 more than once go ahead and register an assert for OP. */
5820 if (live_on_edge (e, op)
5821 && !has_single_use (op))
5823 val = build_int_cst (TREE_TYPE (op), 0);
5824 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5827 /* Now look at how OP is set. If it's set from a comparison,
5828 a truth operation or some bit operations, then we may be able
5829 to register information about the operands of that assignment. */
5830 op_def = SSA_NAME_DEF_STMT (op);
5831 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5832 return;
5834 rhs_code = gimple_assign_rhs_code (op_def);
5836 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5838 bool invert = (code == EQ_EXPR ? true : false);
5839 tree op0 = gimple_assign_rhs1 (op_def);
5840 tree op1 = gimple_assign_rhs2 (op_def);
5842 if (TREE_CODE (op0) == SSA_NAME)
5843 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5844 if (TREE_CODE (op1) == SSA_NAME)
5845 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5847 else if ((code == NE_EXPR
5848 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5849 || (code == EQ_EXPR
5850 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5852 /* Recurse on each operand. */
5853 tree op0 = gimple_assign_rhs1 (op_def);
5854 tree op1 = gimple_assign_rhs2 (op_def);
5855 if (TREE_CODE (op0) == SSA_NAME
5856 && has_single_use (op0))
5857 register_edge_assert_for_1 (op0, code, e, bsi);
5858 if (TREE_CODE (op1) == SSA_NAME
5859 && has_single_use (op1))
5860 register_edge_assert_for_1 (op1, code, e, bsi);
5862 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5863 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5865 /* Recurse, flipping CODE. */
5866 code = invert_tree_comparison (code, false);
5867 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5869 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5871 /* Recurse through the copy. */
5872 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5874 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5876 /* Recurse through the type conversion, unless it is a narrowing
5877 conversion or conversion from non-integral type. */
5878 tree rhs = gimple_assign_rhs1 (op_def);
5879 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5880 && (TYPE_PRECISION (TREE_TYPE (rhs))
5881 <= TYPE_PRECISION (TREE_TYPE (op))))
5882 register_edge_assert_for_1 (rhs, code, e, bsi);
5886 /* Try to register an edge assertion for SSA name NAME on edge E for
5887 the condition COND contributing to the conditional jump pointed to by
5888 SI. */
5890 static void
5891 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5892 enum tree_code cond_code, tree cond_op0,
5893 tree cond_op1)
5895 tree val;
5896 enum tree_code comp_code;
5897 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5899 /* Do not attempt to infer anything in names that flow through
5900 abnormal edges. */
5901 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5902 return;
5904 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5905 cond_op0, cond_op1,
5906 is_else_edge,
5907 &comp_code, &val))
5908 return;
5910 /* Register ASSERT_EXPRs for name. */
5911 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5912 cond_op1, is_else_edge);
5915 /* If COND is effectively an equality test of an SSA_NAME against
5916 the value zero or one, then we may be able to assert values
5917 for SSA_NAMEs which flow into COND. */
5919 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5920 statement of NAME we can assert both operands of the BIT_AND_EXPR
5921 have nonzero value. */
5922 if (((comp_code == EQ_EXPR && integer_onep (val))
5923 || (comp_code == NE_EXPR && integer_zerop (val))))
5925 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5927 if (is_gimple_assign (def_stmt)
5928 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5930 tree op0 = gimple_assign_rhs1 (def_stmt);
5931 tree op1 = gimple_assign_rhs2 (def_stmt);
5932 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5933 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5937 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5938 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5939 have zero value. */
5940 if (((comp_code == EQ_EXPR && integer_zerop (val))
5941 || (comp_code == NE_EXPR && integer_onep (val))))
5943 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5945 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5946 necessarily zero value, or if type-precision is one. */
5947 if (is_gimple_assign (def_stmt)
5948 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5949 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5950 || comp_code == EQ_EXPR)))
5952 tree op0 = gimple_assign_rhs1 (def_stmt);
5953 tree op1 = gimple_assign_rhs2 (def_stmt);
5954 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5955 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5961 /* Determine whether the outgoing edges of BB should receive an
5962 ASSERT_EXPR for each of the operands of BB's LAST statement.
5963 The last statement of BB must be a COND_EXPR.
5965 If any of the sub-graphs rooted at BB have an interesting use of
5966 the predicate operands, an assert location node is added to the
5967 list of assertions for the corresponding operands. */
5969 static void
5970 find_conditional_asserts (basic_block bb, gcond *last)
5972 gimple_stmt_iterator bsi;
5973 tree op;
5974 edge_iterator ei;
5975 edge e;
5976 ssa_op_iter iter;
5978 bsi = gsi_for_stmt (last);
5980 /* Look for uses of the operands in each of the sub-graphs
5981 rooted at BB. We need to check each of the outgoing edges
5982 separately, so that we know what kind of ASSERT_EXPR to
5983 insert. */
5984 FOR_EACH_EDGE (e, ei, bb->succs)
5986 if (e->dest == bb)
5987 continue;
5989 /* Register the necessary assertions for each operand in the
5990 conditional predicate. */
5991 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5992 register_edge_assert_for (op, e, bsi,
5993 gimple_cond_code (last),
5994 gimple_cond_lhs (last),
5995 gimple_cond_rhs (last));
5999 struct case_info
6001 tree expr;
6002 basic_block bb;
6005 /* Compare two case labels sorting first by the destination bb index
6006 and then by the case value. */
6008 static int
6009 compare_case_labels (const void *p1, const void *p2)
6011 const struct case_info *ci1 = (const struct case_info *) p1;
6012 const struct case_info *ci2 = (const struct case_info *) p2;
6013 int idx1 = ci1->bb->index;
6014 int idx2 = ci2->bb->index;
6016 if (idx1 < idx2)
6017 return -1;
6018 else if (idx1 == idx2)
6020 /* Make sure the default label is first in a group. */
6021 if (!CASE_LOW (ci1->expr))
6022 return -1;
6023 else if (!CASE_LOW (ci2->expr))
6024 return 1;
6025 else
6026 return tree_int_cst_compare (CASE_LOW (ci1->expr),
6027 CASE_LOW (ci2->expr));
6029 else
6030 return 1;
6033 /* Determine whether the outgoing edges of BB should receive an
6034 ASSERT_EXPR for each of the operands of BB's LAST statement.
6035 The last statement of BB must be a SWITCH_EXPR.
6037 If any of the sub-graphs rooted at BB have an interesting use of
6038 the predicate operands, an assert location node is added to the
6039 list of assertions for the corresponding operands. */
6041 static void
6042 find_switch_asserts (basic_block bb, gswitch *last)
6044 gimple_stmt_iterator bsi;
6045 tree op;
6046 edge e;
6047 struct case_info *ci;
6048 size_t n = gimple_switch_num_labels (last);
6049 #if GCC_VERSION >= 4000
6050 unsigned int idx;
6051 #else
6052 /* Work around GCC 3.4 bug (PR 37086). */
6053 volatile unsigned int idx;
6054 #endif
6056 bsi = gsi_for_stmt (last);
6057 op = gimple_switch_index (last);
6058 if (TREE_CODE (op) != SSA_NAME)
6059 return;
6061 /* Build a vector of case labels sorted by destination label. */
6062 ci = XNEWVEC (struct case_info, n);
6063 for (idx = 0; idx < n; ++idx)
6065 ci[idx].expr = gimple_switch_label (last, idx);
6066 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
6068 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
6070 for (idx = 0; idx < n; ++idx)
6072 tree min, max;
6073 tree cl = ci[idx].expr;
6074 basic_block cbb = ci[idx].bb;
6076 min = CASE_LOW (cl);
6077 max = CASE_HIGH (cl);
6079 /* If there are multiple case labels with the same destination
6080 we need to combine them to a single value range for the edge. */
6081 if (idx + 1 < n && cbb == ci[idx + 1].bb)
6083 /* Skip labels until the last of the group. */
6084 do {
6085 ++idx;
6086 } while (idx < n && cbb == ci[idx].bb);
6087 --idx;
6089 /* Pick up the maximum of the case label range. */
6090 if (CASE_HIGH (ci[idx].expr))
6091 max = CASE_HIGH (ci[idx].expr);
6092 else
6093 max = CASE_LOW (ci[idx].expr);
6096 /* Nothing to do if the range includes the default label until we
6097 can register anti-ranges. */
6098 if (min == NULL_TREE)
6099 continue;
6101 /* Find the edge to register the assert expr on. */
6102 e = find_edge (bb, cbb);
6104 /* Register the necessary assertions for the operand in the
6105 SWITCH_EXPR. */
6106 register_edge_assert_for (op, e, bsi,
6107 max ? GE_EXPR : EQ_EXPR,
6108 op, fold_convert (TREE_TYPE (op), min));
6109 if (max)
6110 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6111 fold_convert (TREE_TYPE (op), max));
6114 XDELETEVEC (ci);
6118 /* Traverse all the statements in block BB looking for statements that
6119 may generate useful assertions for the SSA names in their operand.
6120 If a statement produces a useful assertion A for name N_i, then the
6121 list of assertions already generated for N_i is scanned to
6122 determine if A is actually needed.
6124 If N_i already had the assertion A at a location dominating the
6125 current location, then nothing needs to be done. Otherwise, the
6126 new location for A is recorded instead.
6128 1- For every statement S in BB, all the variables used by S are
6129 added to bitmap FOUND_IN_SUBGRAPH.
6131 2- If statement S uses an operand N in a way that exposes a known
6132 value range for N, then if N was not already generated by an
6133 ASSERT_EXPR, create a new assert location for N. For instance,
6134 if N is a pointer and the statement dereferences it, we can
6135 assume that N is not NULL.
6137 3- COND_EXPRs are a special case of #2. We can derive range
6138 information from the predicate but need to insert different
6139 ASSERT_EXPRs for each of the sub-graphs rooted at the
6140 conditional block. If the last statement of BB is a conditional
6141 expression of the form 'X op Y', then
6143 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6145 b) If the conditional is the only entry point to the sub-graph
6146 corresponding to the THEN_CLAUSE, recurse into it. On
6147 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6148 an ASSERT_EXPR is added for the corresponding variable.
6150 c) Repeat step (b) on the ELSE_CLAUSE.
6152 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6154 For instance,
6156 if (a == 9)
6157 b = a;
6158 else
6159 b = c + 1;
6161 In this case, an assertion on the THEN clause is useful to
6162 determine that 'a' is always 9 on that edge. However, an assertion
6163 on the ELSE clause would be unnecessary.
6165 4- If BB does not end in a conditional expression, then we recurse
6166 into BB's dominator children.
6168 At the end of the recursive traversal, every SSA name will have a
6169 list of locations where ASSERT_EXPRs should be added. When a new
6170 location for name N is found, it is registered by calling
6171 register_new_assert_for. That function keeps track of all the
6172 registered assertions to prevent adding unnecessary assertions.
6173 For instance, if a pointer P_4 is dereferenced more than once in a
6174 dominator tree, only the location dominating all the dereference of
6175 P_4 will receive an ASSERT_EXPR. */
6177 static void
6178 find_assert_locations_1 (basic_block bb, sbitmap live)
6180 gimple last;
6182 last = last_stmt (bb);
6184 /* If BB's last statement is a conditional statement involving integer
6185 operands, determine if we need to add ASSERT_EXPRs. */
6186 if (last
6187 && gimple_code (last) == GIMPLE_COND
6188 && !fp_predicate (last)
6189 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6190 find_conditional_asserts (bb, as_a <gcond *> (last));
6192 /* If BB's last statement is a switch statement involving integer
6193 operands, determine if we need to add ASSERT_EXPRs. */
6194 if (last
6195 && gimple_code (last) == GIMPLE_SWITCH
6196 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6197 find_switch_asserts (bb, as_a <gswitch *> (last));
6199 /* Traverse all the statements in BB marking used names and looking
6200 for statements that may infer assertions for their used operands. */
6201 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6202 gsi_prev (&si))
6204 gimple stmt;
6205 tree op;
6206 ssa_op_iter i;
6208 stmt = gsi_stmt (si);
6210 if (is_gimple_debug (stmt))
6211 continue;
6213 /* See if we can derive an assertion for any of STMT's operands. */
6214 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6216 tree value;
6217 enum tree_code comp_code;
6219 /* If op is not live beyond this stmt, do not bother to insert
6220 asserts for it. */
6221 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6222 continue;
6224 /* If OP is used in such a way that we can infer a value
6225 range for it, and we don't find a previous assertion for
6226 it, create a new assertion location node for OP. */
6227 if (infer_value_range (stmt, op, &comp_code, &value))
6229 /* If we are able to infer a nonzero value range for OP,
6230 then walk backwards through the use-def chain to see if OP
6231 was set via a typecast.
6233 If so, then we can also infer a nonzero value range
6234 for the operand of the NOP_EXPR. */
6235 if (comp_code == NE_EXPR && integer_zerop (value))
6237 tree t = op;
6238 gimple def_stmt = SSA_NAME_DEF_STMT (t);
6240 while (is_gimple_assign (def_stmt)
6241 && CONVERT_EXPR_CODE_P
6242 (gimple_assign_rhs_code (def_stmt))
6243 && TREE_CODE
6244 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6245 && POINTER_TYPE_P
6246 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6248 t = gimple_assign_rhs1 (def_stmt);
6249 def_stmt = SSA_NAME_DEF_STMT (t);
6251 /* Note we want to register the assert for the
6252 operand of the NOP_EXPR after SI, not after the
6253 conversion. */
6254 if (! has_single_use (t))
6255 register_new_assert_for (t, t, comp_code, value,
6256 bb, NULL, si);
6260 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6264 /* Update live. */
6265 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6266 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6267 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6268 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6271 /* Traverse all PHI nodes in BB, updating live. */
6272 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6273 gsi_next (&si))
6275 use_operand_p arg_p;
6276 ssa_op_iter i;
6277 gphi *phi = si.phi ();
6278 tree res = gimple_phi_result (phi);
6280 if (virtual_operand_p (res))
6281 continue;
6283 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6285 tree arg = USE_FROM_PTR (arg_p);
6286 if (TREE_CODE (arg) == SSA_NAME)
6287 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6290 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6294 /* Do an RPO walk over the function computing SSA name liveness
6295 on-the-fly and deciding on assert expressions to insert. */
6297 static void
6298 find_assert_locations (void)
6300 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6301 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6302 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6303 int rpo_cnt, i;
6305 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6306 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6307 for (i = 0; i < rpo_cnt; ++i)
6308 bb_rpo[rpo[i]] = i;
6310 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6311 the order we compute liveness and insert asserts we otherwise
6312 fail to insert asserts into the loop latch. */
6313 loop_p loop;
6314 FOR_EACH_LOOP (loop, 0)
6316 i = loop->latch->index;
6317 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6318 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6319 !gsi_end_p (gsi); gsi_next (&gsi))
6321 gphi *phi = gsi.phi ();
6322 if (virtual_operand_p (gimple_phi_result (phi)))
6323 continue;
6324 tree arg = gimple_phi_arg_def (phi, j);
6325 if (TREE_CODE (arg) == SSA_NAME)
6327 if (live[i] == NULL)
6329 live[i] = sbitmap_alloc (num_ssa_names);
6330 bitmap_clear (live[i]);
6332 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6337 for (i = rpo_cnt - 1; i >= 0; --i)
6339 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6340 edge e;
6341 edge_iterator ei;
6343 if (!live[rpo[i]])
6345 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6346 bitmap_clear (live[rpo[i]]);
6349 /* Process BB and update the live information with uses in
6350 this block. */
6351 find_assert_locations_1 (bb, live[rpo[i]]);
6353 /* Merge liveness into the predecessor blocks and free it. */
6354 if (!bitmap_empty_p (live[rpo[i]]))
6356 int pred_rpo = i;
6357 FOR_EACH_EDGE (e, ei, bb->preds)
6359 int pred = e->src->index;
6360 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6361 continue;
6363 if (!live[pred])
6365 live[pred] = sbitmap_alloc (num_ssa_names);
6366 bitmap_clear (live[pred]);
6368 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6370 if (bb_rpo[pred] < pred_rpo)
6371 pred_rpo = bb_rpo[pred];
6374 /* Record the RPO number of the last visited block that needs
6375 live information from this block. */
6376 last_rpo[rpo[i]] = pred_rpo;
6378 else
6380 sbitmap_free (live[rpo[i]]);
6381 live[rpo[i]] = NULL;
6384 /* We can free all successors live bitmaps if all their
6385 predecessors have been visited already. */
6386 FOR_EACH_EDGE (e, ei, bb->succs)
6387 if (last_rpo[e->dest->index] == i
6388 && live[e->dest->index])
6390 sbitmap_free (live[e->dest->index]);
6391 live[e->dest->index] = NULL;
6395 XDELETEVEC (rpo);
6396 XDELETEVEC (bb_rpo);
6397 XDELETEVEC (last_rpo);
6398 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6399 if (live[i])
6400 sbitmap_free (live[i]);
6401 XDELETEVEC (live);
6404 /* Create an ASSERT_EXPR for NAME and insert it in the location
6405 indicated by LOC. Return true if we made any edge insertions. */
6407 static bool
6408 process_assert_insertions_for (tree name, assert_locus_t loc)
6410 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6411 gimple stmt;
6412 tree cond;
6413 gimple assert_stmt;
6414 edge_iterator ei;
6415 edge e;
6417 /* If we have X <=> X do not insert an assert expr for that. */
6418 if (loc->expr == loc->val)
6419 return false;
6421 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6422 assert_stmt = build_assert_expr_for (cond, name);
6423 if (loc->e)
6425 /* We have been asked to insert the assertion on an edge. This
6426 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6427 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6428 || (gimple_code (gsi_stmt (loc->si))
6429 == GIMPLE_SWITCH));
6431 gsi_insert_on_edge (loc->e, assert_stmt);
6432 return true;
6435 /* Otherwise, we can insert right after LOC->SI iff the
6436 statement must not be the last statement in the block. */
6437 stmt = gsi_stmt (loc->si);
6438 if (!stmt_ends_bb_p (stmt))
6440 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6441 return false;
6444 /* If STMT must be the last statement in BB, we can only insert new
6445 assertions on the non-abnormal edge out of BB. Note that since
6446 STMT is not control flow, there may only be one non-abnormal edge
6447 out of BB. */
6448 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6449 if (!(e->flags & EDGE_ABNORMAL))
6451 gsi_insert_on_edge (e, assert_stmt);
6452 return true;
6455 gcc_unreachable ();
6459 /* Process all the insertions registered for every name N_i registered
6460 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6461 found in ASSERTS_FOR[i]. */
6463 static void
6464 process_assert_insertions (void)
6466 unsigned i;
6467 bitmap_iterator bi;
6468 bool update_edges_p = false;
6469 int num_asserts = 0;
6471 if (dump_file && (dump_flags & TDF_DETAILS))
6472 dump_all_asserts (dump_file);
6474 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6476 assert_locus_t loc = asserts_for[i];
6477 gcc_assert (loc);
6479 while (loc)
6481 assert_locus_t next = loc->next;
6482 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6483 free (loc);
6484 loc = next;
6485 num_asserts++;
6489 if (update_edges_p)
6490 gsi_commit_edge_inserts ();
6492 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6493 num_asserts);
6497 /* Traverse the flowgraph looking for conditional jumps to insert range
6498 expressions. These range expressions are meant to provide information
6499 to optimizations that need to reason in terms of value ranges. They
6500 will not be expanded into RTL. For instance, given:
6502 x = ...
6503 y = ...
6504 if (x < y)
6505 y = x - 2;
6506 else
6507 x = y + 3;
6509 this pass will transform the code into:
6511 x = ...
6512 y = ...
6513 if (x < y)
6515 x = ASSERT_EXPR <x, x < y>
6516 y = x - 2
6518 else
6520 y = ASSERT_EXPR <y, x >= y>
6521 x = y + 3
6524 The idea is that once copy and constant propagation have run, other
6525 optimizations will be able to determine what ranges of values can 'x'
6526 take in different paths of the code, simply by checking the reaching
6527 definition of 'x'. */
6529 static void
6530 insert_range_assertions (void)
6532 need_assert_for = BITMAP_ALLOC (NULL);
6533 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6535 calculate_dominance_info (CDI_DOMINATORS);
6537 find_assert_locations ();
6538 if (!bitmap_empty_p (need_assert_for))
6540 process_assert_insertions ();
6541 update_ssa (TODO_update_ssa_no_phi);
6544 if (dump_file && (dump_flags & TDF_DETAILS))
6546 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6547 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6550 free (asserts_for);
6551 BITMAP_FREE (need_assert_for);
6554 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6555 and "struct" hacks. If VRP can determine that the
6556 array subscript is a constant, check if it is outside valid
6557 range. If the array subscript is a RANGE, warn if it is
6558 non-overlapping with valid range.
6559 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6561 static void
6562 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6564 value_range_t* vr = NULL;
6565 tree low_sub, up_sub;
6566 tree low_bound, up_bound, up_bound_p1;
6567 tree base;
6569 if (TREE_NO_WARNING (ref))
6570 return;
6572 low_sub = up_sub = TREE_OPERAND (ref, 1);
6573 up_bound = array_ref_up_bound (ref);
6575 /* Can not check flexible arrays. */
6576 if (!up_bound
6577 || TREE_CODE (up_bound) != INTEGER_CST)
6578 return;
6580 /* Accesses to trailing arrays via pointers may access storage
6581 beyond the types array bounds. */
6582 base = get_base_address (ref);
6583 if ((warn_array_bounds < 2)
6584 && base && TREE_CODE (base) == MEM_REF)
6586 tree cref, next = NULL_TREE;
6588 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6589 return;
6591 cref = TREE_OPERAND (ref, 0);
6592 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6593 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6594 next && TREE_CODE (next) != FIELD_DECL;
6595 next = DECL_CHAIN (next))
6598 /* If this is the last field in a struct type or a field in a
6599 union type do not warn. */
6600 if (!next)
6601 return;
6604 low_bound = array_ref_low_bound (ref);
6605 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6606 build_int_cst (TREE_TYPE (up_bound), 1));
6608 /* Empty array. */
6609 if (tree_int_cst_equal (low_bound, up_bound_p1))
6611 warning_at (location, OPT_Warray_bounds,
6612 "array subscript is above array bounds");
6613 TREE_NO_WARNING (ref) = 1;
6616 if (TREE_CODE (low_sub) == SSA_NAME)
6618 vr = get_value_range (low_sub);
6619 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6621 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6622 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6626 if (vr && vr->type == VR_ANTI_RANGE)
6628 if (TREE_CODE (up_sub) == INTEGER_CST
6629 && (ignore_off_by_one
6630 ? tree_int_cst_lt (up_bound, up_sub)
6631 : tree_int_cst_le (up_bound, up_sub))
6632 && TREE_CODE (low_sub) == INTEGER_CST
6633 && tree_int_cst_le (low_sub, low_bound))
6635 warning_at (location, OPT_Warray_bounds,
6636 "array subscript is outside array bounds");
6637 TREE_NO_WARNING (ref) = 1;
6640 else if (TREE_CODE (up_sub) == INTEGER_CST
6641 && (ignore_off_by_one
6642 ? !tree_int_cst_le (up_sub, up_bound_p1)
6643 : !tree_int_cst_le (up_sub, up_bound)))
6645 if (dump_file && (dump_flags & TDF_DETAILS))
6647 fprintf (dump_file, "Array bound warning for ");
6648 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6649 fprintf (dump_file, "\n");
6651 warning_at (location, OPT_Warray_bounds,
6652 "array subscript is above array bounds");
6653 TREE_NO_WARNING (ref) = 1;
6655 else if (TREE_CODE (low_sub) == INTEGER_CST
6656 && tree_int_cst_lt (low_sub, low_bound))
6658 if (dump_file && (dump_flags & TDF_DETAILS))
6660 fprintf (dump_file, "Array bound warning for ");
6661 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6662 fprintf (dump_file, "\n");
6664 warning_at (location, OPT_Warray_bounds,
6665 "array subscript is below array bounds");
6666 TREE_NO_WARNING (ref) = 1;
6670 /* Searches if the expr T, located at LOCATION computes
6671 address of an ARRAY_REF, and call check_array_ref on it. */
6673 static void
6674 search_for_addr_array (tree t, location_t location)
6676 /* Check each ARRAY_REFs in the reference chain. */
6679 if (TREE_CODE (t) == ARRAY_REF)
6680 check_array_ref (location, t, true /*ignore_off_by_one*/);
6682 t = TREE_OPERAND (t, 0);
6684 while (handled_component_p (t));
6686 if (TREE_CODE (t) == MEM_REF
6687 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6688 && !TREE_NO_WARNING (t))
6690 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6691 tree low_bound, up_bound, el_sz;
6692 offset_int idx;
6693 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6694 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6695 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6696 return;
6698 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6699 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6700 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6701 if (!low_bound
6702 || TREE_CODE (low_bound) != INTEGER_CST
6703 || !up_bound
6704 || TREE_CODE (up_bound) != INTEGER_CST
6705 || !el_sz
6706 || TREE_CODE (el_sz) != INTEGER_CST)
6707 return;
6709 idx = mem_ref_offset (t);
6710 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6711 if (wi::lts_p (idx, 0))
6713 if (dump_file && (dump_flags & TDF_DETAILS))
6715 fprintf (dump_file, "Array bound warning for ");
6716 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6717 fprintf (dump_file, "\n");
6719 warning_at (location, OPT_Warray_bounds,
6720 "array subscript is below array bounds");
6721 TREE_NO_WARNING (t) = 1;
6723 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6724 - wi::to_offset (low_bound) + 1)))
6726 if (dump_file && (dump_flags & TDF_DETAILS))
6728 fprintf (dump_file, "Array bound warning for ");
6729 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6730 fprintf (dump_file, "\n");
6732 warning_at (location, OPT_Warray_bounds,
6733 "array subscript is above array bounds");
6734 TREE_NO_WARNING (t) = 1;
6739 /* walk_tree() callback that checks if *TP is
6740 an ARRAY_REF inside an ADDR_EXPR (in which an array
6741 subscript one outside the valid range is allowed). Call
6742 check_array_ref for each ARRAY_REF found. The location is
6743 passed in DATA. */
6745 static tree
6746 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6748 tree t = *tp;
6749 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6750 location_t location;
6752 if (EXPR_HAS_LOCATION (t))
6753 location = EXPR_LOCATION (t);
6754 else
6756 location_t *locp = (location_t *) wi->info;
6757 location = *locp;
6760 *walk_subtree = TRUE;
6762 if (TREE_CODE (t) == ARRAY_REF)
6763 check_array_ref (location, t, false /*ignore_off_by_one*/);
6765 else if (TREE_CODE (t) == ADDR_EXPR)
6767 search_for_addr_array (t, location);
6768 *walk_subtree = FALSE;
6771 return NULL_TREE;
6774 /* Walk over all statements of all reachable BBs and call check_array_bounds
6775 on them. */
6777 static void
6778 check_all_array_refs (void)
6780 basic_block bb;
6781 gimple_stmt_iterator si;
6783 FOR_EACH_BB_FN (bb, cfun)
6785 edge_iterator ei;
6786 edge e;
6787 bool executable = false;
6789 /* Skip blocks that were found to be unreachable. */
6790 FOR_EACH_EDGE (e, ei, bb->preds)
6791 executable |= !!(e->flags & EDGE_EXECUTABLE);
6792 if (!executable)
6793 continue;
6795 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6797 gimple stmt = gsi_stmt (si);
6798 struct walk_stmt_info wi;
6799 if (!gimple_has_location (stmt)
6800 || is_gimple_debug (stmt))
6801 continue;
6803 memset (&wi, 0, sizeof (wi));
6804 wi.info = CONST_CAST (void *, (const void *)
6805 gimple_location_ptr (stmt));
6807 walk_gimple_op (gsi_stmt (si),
6808 check_array_bounds,
6809 &wi);
6814 /* Return true if all imm uses of VAR are either in STMT, or
6815 feed (optionally through a chain of single imm uses) GIMPLE_COND
6816 in basic block COND_BB. */
6818 static bool
6819 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6821 use_operand_p use_p, use2_p;
6822 imm_use_iterator iter;
6824 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6825 if (USE_STMT (use_p) != stmt)
6827 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6828 if (is_gimple_debug (use_stmt))
6829 continue;
6830 while (is_gimple_assign (use_stmt)
6831 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6832 && single_imm_use (gimple_assign_lhs (use_stmt),
6833 &use2_p, &use_stmt2))
6834 use_stmt = use_stmt2;
6835 if (gimple_code (use_stmt) != GIMPLE_COND
6836 || gimple_bb (use_stmt) != cond_bb)
6837 return false;
6839 return true;
6842 /* Handle
6843 _4 = x_3 & 31;
6844 if (_4 != 0)
6845 goto <bb 6>;
6846 else
6847 goto <bb 7>;
6848 <bb 6>:
6849 __builtin_unreachable ();
6850 <bb 7>:
6851 x_5 = ASSERT_EXPR <x_3, ...>;
6852 If x_3 has no other immediate uses (checked by caller),
6853 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6854 from the non-zero bitmask. */
6856 static void
6857 maybe_set_nonzero_bits (basic_block bb, tree var)
6859 edge e = single_pred_edge (bb);
6860 basic_block cond_bb = e->src;
6861 gimple stmt = last_stmt (cond_bb);
6862 tree cst;
6864 if (stmt == NULL
6865 || gimple_code (stmt) != GIMPLE_COND
6866 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6867 ? EQ_EXPR : NE_EXPR)
6868 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6869 || !integer_zerop (gimple_cond_rhs (stmt)))
6870 return;
6872 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6873 if (!is_gimple_assign (stmt)
6874 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6875 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6876 return;
6877 if (gimple_assign_rhs1 (stmt) != var)
6879 gimple stmt2;
6881 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6882 return;
6883 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6884 if (!gimple_assign_cast_p (stmt2)
6885 || gimple_assign_rhs1 (stmt2) != var
6886 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6887 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6888 != TYPE_PRECISION (TREE_TYPE (var))))
6889 return;
6891 cst = gimple_assign_rhs2 (stmt);
6892 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6895 /* Convert range assertion expressions into the implied copies and
6896 copy propagate away the copies. Doing the trivial copy propagation
6897 here avoids the need to run the full copy propagation pass after
6898 VRP.
6900 FIXME, this will eventually lead to copy propagation removing the
6901 names that had useful range information attached to them. For
6902 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6903 then N_i will have the range [3, +INF].
6905 However, by converting the assertion into the implied copy
6906 operation N_i = N_j, we will then copy-propagate N_j into the uses
6907 of N_i and lose the range information. We may want to hold on to
6908 ASSERT_EXPRs a little while longer as the ranges could be used in
6909 things like jump threading.
6911 The problem with keeping ASSERT_EXPRs around is that passes after
6912 VRP need to handle them appropriately.
6914 Another approach would be to make the range information a first
6915 class property of the SSA_NAME so that it can be queried from
6916 any pass. This is made somewhat more complex by the need for
6917 multiple ranges to be associated with one SSA_NAME. */
6919 static void
6920 remove_range_assertions (void)
6922 basic_block bb;
6923 gimple_stmt_iterator si;
6924 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6925 a basic block preceeded by GIMPLE_COND branching to it and
6926 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6927 int is_unreachable;
6929 /* Note that the BSI iterator bump happens at the bottom of the
6930 loop and no bump is necessary if we're removing the statement
6931 referenced by the current BSI. */
6932 FOR_EACH_BB_FN (bb, cfun)
6933 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6935 gimple stmt = gsi_stmt (si);
6936 gimple use_stmt;
6938 if (is_gimple_assign (stmt)
6939 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6941 tree lhs = gimple_assign_lhs (stmt);
6942 tree rhs = gimple_assign_rhs1 (stmt);
6943 tree var;
6944 tree cond = fold (ASSERT_EXPR_COND (rhs));
6945 use_operand_p use_p;
6946 imm_use_iterator iter;
6948 gcc_assert (cond != boolean_false_node);
6950 var = ASSERT_EXPR_VAR (rhs);
6951 gcc_assert (TREE_CODE (var) == SSA_NAME);
6953 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6954 && SSA_NAME_RANGE_INFO (lhs))
6956 if (is_unreachable == -1)
6958 is_unreachable = 0;
6959 if (single_pred_p (bb)
6960 && assert_unreachable_fallthru_edge_p
6961 (single_pred_edge (bb)))
6962 is_unreachable = 1;
6964 /* Handle
6965 if (x_7 >= 10 && x_7 < 20)
6966 __builtin_unreachable ();
6967 x_8 = ASSERT_EXPR <x_7, ...>;
6968 if the only uses of x_7 are in the ASSERT_EXPR and
6969 in the condition. In that case, we can copy the
6970 range info from x_8 computed in this pass also
6971 for x_7. */
6972 if (is_unreachable
6973 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6974 single_pred (bb)))
6976 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6977 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6978 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6979 maybe_set_nonzero_bits (bb, var);
6983 /* Propagate the RHS into every use of the LHS. */
6984 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6985 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6986 SET_USE (use_p, var);
6988 /* And finally, remove the copy, it is not needed. */
6989 gsi_remove (&si, true);
6990 release_defs (stmt);
6992 else
6994 if (!is_gimple_debug (gsi_stmt (si)))
6995 is_unreachable = 0;
6996 gsi_next (&si);
7002 /* Return true if STMT is interesting for VRP. */
7004 static bool
7005 stmt_interesting_for_vrp (gimple stmt)
7007 if (gimple_code (stmt) == GIMPLE_PHI)
7009 tree res = gimple_phi_result (stmt);
7010 return (!virtual_operand_p (res)
7011 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
7012 || POINTER_TYPE_P (TREE_TYPE (res))));
7014 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7016 tree lhs = gimple_get_lhs (stmt);
7018 /* In general, assignments with virtual operands are not useful
7019 for deriving ranges, with the obvious exception of calls to
7020 builtin functions. */
7021 if (lhs && TREE_CODE (lhs) == SSA_NAME
7022 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7023 || POINTER_TYPE_P (TREE_TYPE (lhs)))
7024 && (is_gimple_call (stmt)
7025 || !gimple_vuse (stmt)))
7026 return true;
7027 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7028 switch (gimple_call_internal_fn (stmt))
7030 case IFN_ADD_OVERFLOW:
7031 case IFN_SUB_OVERFLOW:
7032 case IFN_MUL_OVERFLOW:
7033 /* These internal calls return _Complex integer type,
7034 but are interesting to VRP nevertheless. */
7035 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7036 return true;
7037 break;
7038 default:
7039 break;
7042 else if (gimple_code (stmt) == GIMPLE_COND
7043 || gimple_code (stmt) == GIMPLE_SWITCH)
7044 return true;
7046 return false;
7050 /* Initialize local data structures for VRP. */
7052 static void
7053 vrp_initialize (void)
7055 basic_block bb;
7057 values_propagated = false;
7058 num_vr_values = num_ssa_names;
7059 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
7060 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
7062 FOR_EACH_BB_FN (bb, cfun)
7064 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7065 gsi_next (&si))
7067 gphi *phi = si.phi ();
7068 if (!stmt_interesting_for_vrp (phi))
7070 tree lhs = PHI_RESULT (phi);
7071 set_value_range_to_varying (get_value_range (lhs));
7072 prop_set_simulate_again (phi, false);
7074 else
7075 prop_set_simulate_again (phi, true);
7078 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7079 gsi_next (&si))
7081 gimple stmt = gsi_stmt (si);
7083 /* If the statement is a control insn, then we do not
7084 want to avoid simulating the statement once. Failure
7085 to do so means that those edges will never get added. */
7086 if (stmt_ends_bb_p (stmt))
7087 prop_set_simulate_again (stmt, true);
7088 else if (!stmt_interesting_for_vrp (stmt))
7090 ssa_op_iter i;
7091 tree def;
7092 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7093 set_value_range_to_varying (get_value_range (def));
7094 prop_set_simulate_again (stmt, false);
7096 else
7097 prop_set_simulate_again (stmt, true);
7102 /* Return the singleton value-range for NAME or NAME. */
7104 static inline tree
7105 vrp_valueize (tree name)
7107 if (TREE_CODE (name) == SSA_NAME)
7109 value_range_t *vr = get_value_range (name);
7110 if (vr->type == VR_RANGE
7111 && (vr->min == vr->max
7112 || operand_equal_p (vr->min, vr->max, 0)))
7113 return vr->min;
7115 return name;
7118 /* Return the singleton value-range for NAME if that is a constant
7119 but signal to not follow SSA edges. */
7121 static inline tree
7122 vrp_valueize_1 (tree name)
7124 if (TREE_CODE (name) == SSA_NAME)
7126 /* If the definition may be simulated again we cannot follow
7127 this SSA edge as the SSA propagator does not necessarily
7128 re-visit the use. */
7129 gimple def_stmt = SSA_NAME_DEF_STMT (name);
7130 if (!gimple_nop_p (def_stmt)
7131 && prop_simulate_again_p (def_stmt))
7132 return NULL_TREE;
7133 value_range_t *vr = get_value_range (name);
7134 if (range_int_cst_singleton_p (vr))
7135 return vr->min;
7137 return name;
7140 /* Visit assignment STMT. If it produces an interesting range, record
7141 the SSA name in *OUTPUT_P. */
7143 static enum ssa_prop_result
7144 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
7146 tree def, lhs;
7147 ssa_op_iter iter;
7148 enum gimple_code code = gimple_code (stmt);
7149 lhs = gimple_get_lhs (stmt);
7151 /* We only keep track of ranges in integral and pointer types. */
7152 if (TREE_CODE (lhs) == SSA_NAME
7153 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7154 /* It is valid to have NULL MIN/MAX values on a type. See
7155 build_range_type. */
7156 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7157 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7158 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7160 value_range_t new_vr = VR_INITIALIZER;
7162 /* Try folding the statement to a constant first. */
7163 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7164 vrp_valueize_1);
7165 if (tem && is_gimple_min_invariant (tem))
7166 set_value_range_to_value (&new_vr, tem, NULL);
7167 /* Then dispatch to value-range extracting functions. */
7168 else if (code == GIMPLE_CALL)
7169 extract_range_basic (&new_vr, stmt);
7170 else
7171 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7173 if (update_value_range (lhs, &new_vr))
7175 *output_p = lhs;
7177 if (dump_file && (dump_flags & TDF_DETAILS))
7179 fprintf (dump_file, "Found new range for ");
7180 print_generic_expr (dump_file, lhs, 0);
7181 fprintf (dump_file, ": ");
7182 dump_value_range (dump_file, &new_vr);
7183 fprintf (dump_file, "\n");
7186 if (new_vr.type == VR_VARYING)
7187 return SSA_PROP_VARYING;
7189 return SSA_PROP_INTERESTING;
7192 return SSA_PROP_NOT_INTERESTING;
7194 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7195 switch (gimple_call_internal_fn (stmt))
7197 case IFN_ADD_OVERFLOW:
7198 case IFN_SUB_OVERFLOW:
7199 case IFN_MUL_OVERFLOW:
7200 /* These internal calls return _Complex integer type,
7201 which VRP does not track, but the immediate uses
7202 thereof might be interesting. */
7203 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7205 imm_use_iterator iter;
7206 use_operand_p use_p;
7207 enum ssa_prop_result res = SSA_PROP_VARYING;
7209 set_value_range_to_varying (get_value_range (lhs));
7211 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7213 gimple use_stmt = USE_STMT (use_p);
7214 if (!is_gimple_assign (use_stmt))
7215 continue;
7216 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7217 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7218 continue;
7219 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7220 tree use_lhs = gimple_assign_lhs (use_stmt);
7221 if (TREE_CODE (rhs1) != rhs_code
7222 || TREE_OPERAND (rhs1, 0) != lhs
7223 || TREE_CODE (use_lhs) != SSA_NAME
7224 || !stmt_interesting_for_vrp (use_stmt)
7225 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7226 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7227 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7228 continue;
7230 /* If there is a change in the value range for any of the
7231 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7232 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7233 or IMAGPART_EXPR immediate uses, but none of them have
7234 a change in their value ranges, return
7235 SSA_PROP_NOT_INTERESTING. If there are no
7236 {REAL,IMAG}PART_EXPR uses at all,
7237 return SSA_PROP_VARYING. */
7238 value_range_t new_vr = VR_INITIALIZER;
7239 extract_range_basic (&new_vr, use_stmt);
7240 value_range_t *old_vr = get_value_range (use_lhs);
7241 if (old_vr->type != new_vr.type
7242 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7243 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7244 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7245 res = SSA_PROP_INTERESTING;
7246 else
7247 res = SSA_PROP_NOT_INTERESTING;
7248 BITMAP_FREE (new_vr.equiv);
7249 if (res == SSA_PROP_INTERESTING)
7251 *output_p = lhs;
7252 return res;
7256 return res;
7258 break;
7259 default:
7260 break;
7263 /* Every other statement produces no useful ranges. */
7264 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7265 set_value_range_to_varying (get_value_range (def));
7267 return SSA_PROP_VARYING;
7270 /* Helper that gets the value range of the SSA_NAME with version I
7271 or a symbolic range containing the SSA_NAME only if the value range
7272 is varying or undefined. */
7274 static inline value_range_t
7275 get_vr_for_comparison (int i)
7277 value_range_t vr = *get_value_range (ssa_name (i));
7279 /* If name N_i does not have a valid range, use N_i as its own
7280 range. This allows us to compare against names that may
7281 have N_i in their ranges. */
7282 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7284 vr.type = VR_RANGE;
7285 vr.min = ssa_name (i);
7286 vr.max = ssa_name (i);
7289 return vr;
7292 /* Compare all the value ranges for names equivalent to VAR with VAL
7293 using comparison code COMP. Return the same value returned by
7294 compare_range_with_value, including the setting of
7295 *STRICT_OVERFLOW_P. */
7297 static tree
7298 compare_name_with_value (enum tree_code comp, tree var, tree val,
7299 bool *strict_overflow_p)
7301 bitmap_iterator bi;
7302 unsigned i;
7303 bitmap e;
7304 tree retval, t;
7305 int used_strict_overflow;
7306 bool sop;
7307 value_range_t equiv_vr;
7309 /* Get the set of equivalences for VAR. */
7310 e = get_value_range (var)->equiv;
7312 /* Start at -1. Set it to 0 if we do a comparison without relying
7313 on overflow, or 1 if all comparisons rely on overflow. */
7314 used_strict_overflow = -1;
7316 /* Compare vars' value range with val. */
7317 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7318 sop = false;
7319 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7320 if (retval)
7321 used_strict_overflow = sop ? 1 : 0;
7323 /* If the equiv set is empty we have done all work we need to do. */
7324 if (e == NULL)
7326 if (retval
7327 && used_strict_overflow > 0)
7328 *strict_overflow_p = true;
7329 return retval;
7332 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7334 equiv_vr = get_vr_for_comparison (i);
7335 sop = false;
7336 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7337 if (t)
7339 /* If we get different answers from different members
7340 of the equivalence set this check must be in a dead
7341 code region. Folding it to a trap representation
7342 would be correct here. For now just return don't-know. */
7343 if (retval != NULL
7344 && t != retval)
7346 retval = NULL_TREE;
7347 break;
7349 retval = t;
7351 if (!sop)
7352 used_strict_overflow = 0;
7353 else if (used_strict_overflow < 0)
7354 used_strict_overflow = 1;
7358 if (retval
7359 && used_strict_overflow > 0)
7360 *strict_overflow_p = true;
7362 return retval;
7366 /* Given a comparison code COMP and names N1 and N2, compare all the
7367 ranges equivalent to N1 against all the ranges equivalent to N2
7368 to determine the value of N1 COMP N2. Return the same value
7369 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7370 whether we relied on an overflow infinity in the comparison. */
7373 static tree
7374 compare_names (enum tree_code comp, tree n1, tree n2,
7375 bool *strict_overflow_p)
7377 tree t, retval;
7378 bitmap e1, e2;
7379 bitmap_iterator bi1, bi2;
7380 unsigned i1, i2;
7381 int used_strict_overflow;
7382 static bitmap_obstack *s_obstack = NULL;
7383 static bitmap s_e1 = NULL, s_e2 = NULL;
7385 /* Compare the ranges of every name equivalent to N1 against the
7386 ranges of every name equivalent to N2. */
7387 e1 = get_value_range (n1)->equiv;
7388 e2 = get_value_range (n2)->equiv;
7390 /* Use the fake bitmaps if e1 or e2 are not available. */
7391 if (s_obstack == NULL)
7393 s_obstack = XNEW (bitmap_obstack);
7394 bitmap_obstack_initialize (s_obstack);
7395 s_e1 = BITMAP_ALLOC (s_obstack);
7396 s_e2 = BITMAP_ALLOC (s_obstack);
7398 if (e1 == NULL)
7399 e1 = s_e1;
7400 if (e2 == NULL)
7401 e2 = s_e2;
7403 /* Add N1 and N2 to their own set of equivalences to avoid
7404 duplicating the body of the loop just to check N1 and N2
7405 ranges. */
7406 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7407 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7409 /* If the equivalence sets have a common intersection, then the two
7410 names can be compared without checking their ranges. */
7411 if (bitmap_intersect_p (e1, e2))
7413 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7414 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7416 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7417 ? boolean_true_node
7418 : boolean_false_node;
7421 /* Start at -1. Set it to 0 if we do a comparison without relying
7422 on overflow, or 1 if all comparisons rely on overflow. */
7423 used_strict_overflow = -1;
7425 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7426 N2 to their own set of equivalences to avoid duplicating the body
7427 of the loop just to check N1 and N2 ranges. */
7428 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7430 value_range_t vr1 = get_vr_for_comparison (i1);
7432 t = retval = NULL_TREE;
7433 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7435 bool sop = false;
7437 value_range_t vr2 = get_vr_for_comparison (i2);
7439 t = compare_ranges (comp, &vr1, &vr2, &sop);
7440 if (t)
7442 /* If we get different answers from different members
7443 of the equivalence set this check must be in a dead
7444 code region. Folding it to a trap representation
7445 would be correct here. For now just return don't-know. */
7446 if (retval != NULL
7447 && t != retval)
7449 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7450 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7451 return NULL_TREE;
7453 retval = t;
7455 if (!sop)
7456 used_strict_overflow = 0;
7457 else if (used_strict_overflow < 0)
7458 used_strict_overflow = 1;
7462 if (retval)
7464 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7465 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7466 if (used_strict_overflow > 0)
7467 *strict_overflow_p = true;
7468 return retval;
7472 /* None of the equivalent ranges are useful in computing this
7473 comparison. */
7474 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7475 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7476 return NULL_TREE;
7479 /* Helper function for vrp_evaluate_conditional_warnv. */
7481 static tree
7482 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7483 tree op0, tree op1,
7484 bool * strict_overflow_p)
7486 value_range_t *vr0, *vr1;
7488 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7489 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7491 tree res = NULL_TREE;
7492 if (vr0 && vr1)
7493 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7494 if (!res && vr0)
7495 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7496 if (!res && vr1)
7497 res = (compare_range_with_value
7498 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7499 return res;
7502 /* Helper function for vrp_evaluate_conditional_warnv. */
7504 static tree
7505 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7506 tree op1, bool use_equiv_p,
7507 bool *strict_overflow_p, bool *only_ranges)
7509 tree ret;
7510 if (only_ranges)
7511 *only_ranges = true;
7513 /* We only deal with integral and pointer types. */
7514 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7515 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7516 return NULL_TREE;
7518 if (use_equiv_p)
7520 if (only_ranges
7521 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7522 (code, op0, op1, strict_overflow_p)))
7523 return ret;
7524 *only_ranges = false;
7525 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7526 return compare_names (code, op0, op1, strict_overflow_p);
7527 else if (TREE_CODE (op0) == SSA_NAME)
7528 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7529 else if (TREE_CODE (op1) == SSA_NAME)
7530 return (compare_name_with_value
7531 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7533 else
7534 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7535 strict_overflow_p);
7536 return NULL_TREE;
7539 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7540 information. Return NULL if the conditional can not be evaluated.
7541 The ranges of all the names equivalent with the operands in COND
7542 will be used when trying to compute the value. If the result is
7543 based on undefined signed overflow, issue a warning if
7544 appropriate. */
7546 static tree
7547 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7549 bool sop;
7550 tree ret;
7551 bool only_ranges;
7553 /* Some passes and foldings leak constants with overflow flag set
7554 into the IL. Avoid doing wrong things with these and bail out. */
7555 if ((TREE_CODE (op0) == INTEGER_CST
7556 && TREE_OVERFLOW (op0))
7557 || (TREE_CODE (op1) == INTEGER_CST
7558 && TREE_OVERFLOW (op1)))
7559 return NULL_TREE;
7561 sop = false;
7562 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7563 &only_ranges);
7565 if (ret && sop)
7567 enum warn_strict_overflow_code wc;
7568 const char* warnmsg;
7570 if (is_gimple_min_invariant (ret))
7572 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7573 warnmsg = G_("assuming signed overflow does not occur when "
7574 "simplifying conditional to constant");
7576 else
7578 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7579 warnmsg = G_("assuming signed overflow does not occur when "
7580 "simplifying conditional");
7583 if (issue_strict_overflow_warning (wc))
7585 location_t location;
7587 if (!gimple_has_location (stmt))
7588 location = input_location;
7589 else
7590 location = gimple_location (stmt);
7591 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7595 if (warn_type_limits
7596 && ret && only_ranges
7597 && TREE_CODE_CLASS (code) == tcc_comparison
7598 && TREE_CODE (op0) == SSA_NAME)
7600 /* If the comparison is being folded and the operand on the LHS
7601 is being compared against a constant value that is outside of
7602 the natural range of OP0's type, then the predicate will
7603 always fold regardless of the value of OP0. If -Wtype-limits
7604 was specified, emit a warning. */
7605 tree type = TREE_TYPE (op0);
7606 value_range_t *vr0 = get_value_range (op0);
7608 if (vr0->type == VR_RANGE
7609 && INTEGRAL_TYPE_P (type)
7610 && vrp_val_is_min (vr0->min)
7611 && vrp_val_is_max (vr0->max)
7612 && is_gimple_min_invariant (op1))
7614 location_t location;
7616 if (!gimple_has_location (stmt))
7617 location = input_location;
7618 else
7619 location = gimple_location (stmt);
7621 warning_at (location, OPT_Wtype_limits,
7622 integer_zerop (ret)
7623 ? G_("comparison always false "
7624 "due to limited range of data type")
7625 : G_("comparison always true "
7626 "due to limited range of data type"));
7630 return ret;
7634 /* Visit conditional statement STMT. If we can determine which edge
7635 will be taken out of STMT's basic block, record it in
7636 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7637 SSA_PROP_VARYING. */
7639 static enum ssa_prop_result
7640 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7642 tree val;
7643 bool sop;
7645 *taken_edge_p = NULL;
7647 if (dump_file && (dump_flags & TDF_DETAILS))
7649 tree use;
7650 ssa_op_iter i;
7652 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7653 print_gimple_stmt (dump_file, stmt, 0, 0);
7654 fprintf (dump_file, "\nWith known ranges\n");
7656 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7658 fprintf (dump_file, "\t");
7659 print_generic_expr (dump_file, use, 0);
7660 fprintf (dump_file, ": ");
7661 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7664 fprintf (dump_file, "\n");
7667 /* Compute the value of the predicate COND by checking the known
7668 ranges of each of its operands.
7670 Note that we cannot evaluate all the equivalent ranges here
7671 because those ranges may not yet be final and with the current
7672 propagation strategy, we cannot determine when the value ranges
7673 of the names in the equivalence set have changed.
7675 For instance, given the following code fragment
7677 i_5 = PHI <8, i_13>
7679 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7680 if (i_14 == 1)
7683 Assume that on the first visit to i_14, i_5 has the temporary
7684 range [8, 8] because the second argument to the PHI function is
7685 not yet executable. We derive the range ~[0, 0] for i_14 and the
7686 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7687 the first time, since i_14 is equivalent to the range [8, 8], we
7688 determine that the predicate is always false.
7690 On the next round of propagation, i_13 is determined to be
7691 VARYING, which causes i_5 to drop down to VARYING. So, another
7692 visit to i_14 is scheduled. In this second visit, we compute the
7693 exact same range and equivalence set for i_14, namely ~[0, 0] and
7694 { i_5 }. But we did not have the previous range for i_5
7695 registered, so vrp_visit_assignment thinks that the range for
7696 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7697 is not visited again, which stops propagation from visiting
7698 statements in the THEN clause of that if().
7700 To properly fix this we would need to keep the previous range
7701 value for the names in the equivalence set. This way we would've
7702 discovered that from one visit to the other i_5 changed from
7703 range [8, 8] to VR_VARYING.
7705 However, fixing this apparent limitation may not be worth the
7706 additional checking. Testing on several code bases (GCC, DLV,
7707 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7708 4 more predicates folded in SPEC. */
7709 sop = false;
7711 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7712 gimple_cond_lhs (stmt),
7713 gimple_cond_rhs (stmt),
7714 false, &sop, NULL);
7715 if (val)
7717 if (!sop)
7718 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7719 else
7721 if (dump_file && (dump_flags & TDF_DETAILS))
7722 fprintf (dump_file,
7723 "\nIgnoring predicate evaluation because "
7724 "it assumes that signed overflow is undefined");
7725 val = NULL_TREE;
7729 if (dump_file && (dump_flags & TDF_DETAILS))
7731 fprintf (dump_file, "\nPredicate evaluates to: ");
7732 if (val == NULL_TREE)
7733 fprintf (dump_file, "DON'T KNOW\n");
7734 else
7735 print_generic_stmt (dump_file, val, 0);
7738 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7741 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7742 that includes the value VAL. The search is restricted to the range
7743 [START_IDX, n - 1] where n is the size of VEC.
7745 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7746 returned.
7748 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7749 it is placed in IDX and false is returned.
7751 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7752 returned. */
7754 static bool
7755 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7757 size_t n = gimple_switch_num_labels (stmt);
7758 size_t low, high;
7760 /* Find case label for minimum of the value range or the next one.
7761 At each iteration we are searching in [low, high - 1]. */
7763 for (low = start_idx, high = n; high != low; )
7765 tree t;
7766 int cmp;
7767 /* Note that i != high, so we never ask for n. */
7768 size_t i = (high + low) / 2;
7769 t = gimple_switch_label (stmt, i);
7771 /* Cache the result of comparing CASE_LOW and val. */
7772 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7774 if (cmp == 0)
7776 /* Ranges cannot be empty. */
7777 *idx = i;
7778 return true;
7780 else if (cmp > 0)
7781 high = i;
7782 else
7784 low = i + 1;
7785 if (CASE_HIGH (t) != NULL
7786 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7788 *idx = i;
7789 return true;
7794 *idx = high;
7795 return false;
7798 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7799 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7800 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7801 then MAX_IDX < MIN_IDX.
7802 Returns true if the default label is not needed. */
7804 static bool
7805 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7806 size_t *max_idx)
7808 size_t i, j;
7809 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7810 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7812 if (i == j
7813 && min_take_default
7814 && max_take_default)
7816 /* Only the default case label reached.
7817 Return an empty range. */
7818 *min_idx = 1;
7819 *max_idx = 0;
7820 return false;
7822 else
7824 bool take_default = min_take_default || max_take_default;
7825 tree low, high;
7826 size_t k;
7828 if (max_take_default)
7829 j--;
7831 /* If the case label range is continuous, we do not need
7832 the default case label. Verify that. */
7833 high = CASE_LOW (gimple_switch_label (stmt, i));
7834 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7835 high = CASE_HIGH (gimple_switch_label (stmt, i));
7836 for (k = i + 1; k <= j; ++k)
7838 low = CASE_LOW (gimple_switch_label (stmt, k));
7839 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7841 take_default = true;
7842 break;
7844 high = low;
7845 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7846 high = CASE_HIGH (gimple_switch_label (stmt, k));
7849 *min_idx = i;
7850 *max_idx = j;
7851 return !take_default;
7855 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7856 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7857 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7858 Returns true if the default label is not needed. */
7860 static bool
7861 find_case_label_ranges (gswitch *stmt, value_range_t *vr, size_t *min_idx1,
7862 size_t *max_idx1, size_t *min_idx2,
7863 size_t *max_idx2)
7865 size_t i, j, k, l;
7866 unsigned int n = gimple_switch_num_labels (stmt);
7867 bool take_default;
7868 tree case_low, case_high;
7869 tree min = vr->min, max = vr->max;
7871 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7873 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7875 /* Set second range to emtpy. */
7876 *min_idx2 = 1;
7877 *max_idx2 = 0;
7879 if (vr->type == VR_RANGE)
7881 *min_idx1 = i;
7882 *max_idx1 = j;
7883 return !take_default;
7886 /* Set first range to all case labels. */
7887 *min_idx1 = 1;
7888 *max_idx1 = n - 1;
7890 if (i > j)
7891 return false;
7893 /* Make sure all the values of case labels [i , j] are contained in
7894 range [MIN, MAX]. */
7895 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7896 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7897 if (tree_int_cst_compare (case_low, min) < 0)
7898 i += 1;
7899 if (case_high != NULL_TREE
7900 && tree_int_cst_compare (max, case_high) < 0)
7901 j -= 1;
7903 if (i > j)
7904 return false;
7906 /* If the range spans case labels [i, j], the corresponding anti-range spans
7907 the labels [1, i - 1] and [j + 1, n - 1]. */
7908 k = j + 1;
7909 l = n - 1;
7910 if (k > l)
7912 k = 1;
7913 l = 0;
7916 j = i - 1;
7917 i = 1;
7918 if (i > j)
7920 i = k;
7921 j = l;
7922 k = 1;
7923 l = 0;
7926 *min_idx1 = i;
7927 *max_idx1 = j;
7928 *min_idx2 = k;
7929 *max_idx2 = l;
7930 return false;
7933 /* Visit switch statement STMT. If we can determine which edge
7934 will be taken out of STMT's basic block, record it in
7935 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7936 SSA_PROP_VARYING. */
7938 static enum ssa_prop_result
7939 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7941 tree op, val;
7942 value_range_t *vr;
7943 size_t i = 0, j = 0, k, l;
7944 bool take_default;
7946 *taken_edge_p = NULL;
7947 op = gimple_switch_index (stmt);
7948 if (TREE_CODE (op) != SSA_NAME)
7949 return SSA_PROP_VARYING;
7951 vr = get_value_range (op);
7952 if (dump_file && (dump_flags & TDF_DETAILS))
7954 fprintf (dump_file, "\nVisiting switch expression with operand ");
7955 print_generic_expr (dump_file, op, 0);
7956 fprintf (dump_file, " with known range ");
7957 dump_value_range (dump_file, vr);
7958 fprintf (dump_file, "\n");
7961 if ((vr->type != VR_RANGE
7962 && vr->type != VR_ANTI_RANGE)
7963 || symbolic_range_p (vr))
7964 return SSA_PROP_VARYING;
7966 /* Find the single edge that is taken from the switch expression. */
7967 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7969 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7970 label */
7971 if (j < i)
7973 gcc_assert (take_default);
7974 val = gimple_switch_default_label (stmt);
7976 else
7978 /* Check if labels with index i to j and maybe the default label
7979 are all reaching the same label. */
7981 val = gimple_switch_label (stmt, i);
7982 if (take_default
7983 && CASE_LABEL (gimple_switch_default_label (stmt))
7984 != CASE_LABEL (val))
7986 if (dump_file && (dump_flags & TDF_DETAILS))
7987 fprintf (dump_file, " not a single destination for this "
7988 "range\n");
7989 return SSA_PROP_VARYING;
7991 for (++i; i <= j; ++i)
7993 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7995 if (dump_file && (dump_flags & TDF_DETAILS))
7996 fprintf (dump_file, " not a single destination for this "
7997 "range\n");
7998 return SSA_PROP_VARYING;
8001 for (; k <= l; ++k)
8003 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
8005 if (dump_file && (dump_flags & TDF_DETAILS))
8006 fprintf (dump_file, " not a single destination for this "
8007 "range\n");
8008 return SSA_PROP_VARYING;
8013 *taken_edge_p = find_edge (gimple_bb (stmt),
8014 label_to_block (CASE_LABEL (val)));
8016 if (dump_file && (dump_flags & TDF_DETAILS))
8018 fprintf (dump_file, " will take edge to ");
8019 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
8022 return SSA_PROP_INTERESTING;
8026 /* Evaluate statement STMT. If the statement produces a useful range,
8027 return SSA_PROP_INTERESTING and record the SSA name with the
8028 interesting range into *OUTPUT_P.
8030 If STMT is a conditional branch and we can determine its truth
8031 value, the taken edge is recorded in *TAKEN_EDGE_P.
8033 If STMT produces a varying value, return SSA_PROP_VARYING. */
8035 static enum ssa_prop_result
8036 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
8038 tree def;
8039 ssa_op_iter iter;
8041 if (dump_file && (dump_flags & TDF_DETAILS))
8043 fprintf (dump_file, "\nVisiting statement:\n");
8044 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
8047 if (!stmt_interesting_for_vrp (stmt))
8048 gcc_assert (stmt_ends_bb_p (stmt));
8049 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
8050 return vrp_visit_assignment_or_call (stmt, output_p);
8051 else if (gimple_code (stmt) == GIMPLE_COND)
8052 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
8053 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8054 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
8056 /* All other statements produce nothing of interest for VRP, so mark
8057 their outputs varying and prevent further simulation. */
8058 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
8059 set_value_range_to_varying (get_value_range (def));
8061 return SSA_PROP_VARYING;
8064 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8065 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8066 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8067 possible such range. The resulting range is not canonicalized. */
8069 static void
8070 union_ranges (enum value_range_type *vr0type,
8071 tree *vr0min, tree *vr0max,
8072 enum value_range_type vr1type,
8073 tree vr1min, tree vr1max)
8075 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8076 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8078 /* [] is vr0, () is vr1 in the following classification comments. */
8079 if (mineq && maxeq)
8081 /* [( )] */
8082 if (*vr0type == vr1type)
8083 /* Nothing to do for equal ranges. */
8085 else if ((*vr0type == VR_RANGE
8086 && vr1type == VR_ANTI_RANGE)
8087 || (*vr0type == VR_ANTI_RANGE
8088 && vr1type == VR_RANGE))
8090 /* For anti-range with range union the result is varying. */
8091 goto give_up;
8093 else
8094 gcc_unreachable ();
8096 else if (operand_less_p (*vr0max, vr1min) == 1
8097 || operand_less_p (vr1max, *vr0min) == 1)
8099 /* [ ] ( ) or ( ) [ ]
8100 If the ranges have an empty intersection, result of the union
8101 operation is the anti-range or if both are anti-ranges
8102 it covers all. */
8103 if (*vr0type == VR_ANTI_RANGE
8104 && vr1type == VR_ANTI_RANGE)
8105 goto give_up;
8106 else if (*vr0type == VR_ANTI_RANGE
8107 && vr1type == VR_RANGE)
8109 else if (*vr0type == VR_RANGE
8110 && vr1type == VR_ANTI_RANGE)
8112 *vr0type = vr1type;
8113 *vr0min = vr1min;
8114 *vr0max = vr1max;
8116 else if (*vr0type == VR_RANGE
8117 && vr1type == VR_RANGE)
8119 /* The result is the convex hull of both ranges. */
8120 if (operand_less_p (*vr0max, vr1min) == 1)
8122 /* If the result can be an anti-range, create one. */
8123 if (TREE_CODE (*vr0max) == INTEGER_CST
8124 && TREE_CODE (vr1min) == INTEGER_CST
8125 && vrp_val_is_min (*vr0min)
8126 && vrp_val_is_max (vr1max))
8128 tree min = int_const_binop (PLUS_EXPR,
8129 *vr0max,
8130 build_int_cst (TREE_TYPE (*vr0max), 1));
8131 tree max = int_const_binop (MINUS_EXPR,
8132 vr1min,
8133 build_int_cst (TREE_TYPE (vr1min), 1));
8134 if (!operand_less_p (max, min))
8136 *vr0type = VR_ANTI_RANGE;
8137 *vr0min = min;
8138 *vr0max = max;
8140 else
8141 *vr0max = vr1max;
8143 else
8144 *vr0max = vr1max;
8146 else
8148 /* If the result can be an anti-range, create one. */
8149 if (TREE_CODE (vr1max) == INTEGER_CST
8150 && TREE_CODE (*vr0min) == INTEGER_CST
8151 && vrp_val_is_min (vr1min)
8152 && vrp_val_is_max (*vr0max))
8154 tree min = int_const_binop (PLUS_EXPR,
8155 vr1max,
8156 build_int_cst (TREE_TYPE (vr1max), 1));
8157 tree max = int_const_binop (MINUS_EXPR,
8158 *vr0min,
8159 build_int_cst (TREE_TYPE (*vr0min), 1));
8160 if (!operand_less_p (max, min))
8162 *vr0type = VR_ANTI_RANGE;
8163 *vr0min = min;
8164 *vr0max = max;
8166 else
8167 *vr0min = vr1min;
8169 else
8170 *vr0min = vr1min;
8173 else
8174 gcc_unreachable ();
8176 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8177 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8179 /* [ ( ) ] or [( ) ] or [ ( )] */
8180 if (*vr0type == VR_RANGE
8181 && vr1type == VR_RANGE)
8183 else if (*vr0type == VR_ANTI_RANGE
8184 && vr1type == VR_ANTI_RANGE)
8186 *vr0type = vr1type;
8187 *vr0min = vr1min;
8188 *vr0max = vr1max;
8190 else if (*vr0type == VR_ANTI_RANGE
8191 && vr1type == VR_RANGE)
8193 /* Arbitrarily choose the right or left gap. */
8194 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8195 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8196 build_int_cst (TREE_TYPE (vr1min), 1));
8197 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8198 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8199 build_int_cst (TREE_TYPE (vr1max), 1));
8200 else
8201 goto give_up;
8203 else if (*vr0type == VR_RANGE
8204 && vr1type == VR_ANTI_RANGE)
8205 /* The result covers everything. */
8206 goto give_up;
8207 else
8208 gcc_unreachable ();
8210 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8211 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8213 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8214 if (*vr0type == VR_RANGE
8215 && vr1type == VR_RANGE)
8217 *vr0type = vr1type;
8218 *vr0min = vr1min;
8219 *vr0max = vr1max;
8221 else if (*vr0type == VR_ANTI_RANGE
8222 && vr1type == VR_ANTI_RANGE)
8224 else if (*vr0type == VR_RANGE
8225 && vr1type == VR_ANTI_RANGE)
8227 *vr0type = VR_ANTI_RANGE;
8228 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8230 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8231 build_int_cst (TREE_TYPE (*vr0min), 1));
8232 *vr0min = vr1min;
8234 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8236 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8237 build_int_cst (TREE_TYPE (*vr0max), 1));
8238 *vr0max = vr1max;
8240 else
8241 goto give_up;
8243 else if (*vr0type == VR_ANTI_RANGE
8244 && vr1type == VR_RANGE)
8245 /* The result covers everything. */
8246 goto give_up;
8247 else
8248 gcc_unreachable ();
8250 else if ((operand_less_p (vr1min, *vr0max) == 1
8251 || operand_equal_p (vr1min, *vr0max, 0))
8252 && operand_less_p (*vr0min, vr1min) == 1
8253 && operand_less_p (*vr0max, vr1max) == 1)
8255 /* [ ( ] ) or [ ]( ) */
8256 if (*vr0type == VR_RANGE
8257 && vr1type == VR_RANGE)
8258 *vr0max = vr1max;
8259 else if (*vr0type == VR_ANTI_RANGE
8260 && vr1type == VR_ANTI_RANGE)
8261 *vr0min = vr1min;
8262 else if (*vr0type == VR_ANTI_RANGE
8263 && vr1type == VR_RANGE)
8265 if (TREE_CODE (vr1min) == INTEGER_CST)
8266 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8267 build_int_cst (TREE_TYPE (vr1min), 1));
8268 else
8269 goto give_up;
8271 else if (*vr0type == VR_RANGE
8272 && vr1type == VR_ANTI_RANGE)
8274 if (TREE_CODE (*vr0max) == INTEGER_CST)
8276 *vr0type = vr1type;
8277 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8278 build_int_cst (TREE_TYPE (*vr0max), 1));
8279 *vr0max = vr1max;
8281 else
8282 goto give_up;
8284 else
8285 gcc_unreachable ();
8287 else if ((operand_less_p (*vr0min, vr1max) == 1
8288 || operand_equal_p (*vr0min, vr1max, 0))
8289 && operand_less_p (vr1min, *vr0min) == 1
8290 && operand_less_p (vr1max, *vr0max) == 1)
8292 /* ( [ ) ] or ( )[ ] */
8293 if (*vr0type == VR_RANGE
8294 && vr1type == VR_RANGE)
8295 *vr0min = vr1min;
8296 else if (*vr0type == VR_ANTI_RANGE
8297 && vr1type == VR_ANTI_RANGE)
8298 *vr0max = vr1max;
8299 else if (*vr0type == VR_ANTI_RANGE
8300 && vr1type == VR_RANGE)
8302 if (TREE_CODE (vr1max) == INTEGER_CST)
8303 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8304 build_int_cst (TREE_TYPE (vr1max), 1));
8305 else
8306 goto give_up;
8308 else if (*vr0type == VR_RANGE
8309 && vr1type == VR_ANTI_RANGE)
8311 if (TREE_CODE (*vr0min) == INTEGER_CST)
8313 *vr0type = vr1type;
8314 *vr0min = vr1min;
8315 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8316 build_int_cst (TREE_TYPE (*vr0min), 1));
8318 else
8319 goto give_up;
8321 else
8322 gcc_unreachable ();
8324 else
8325 goto give_up;
8327 return;
8329 give_up:
8330 *vr0type = VR_VARYING;
8331 *vr0min = NULL_TREE;
8332 *vr0max = NULL_TREE;
8335 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8336 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8337 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8338 possible such range. The resulting range is not canonicalized. */
8340 static void
8341 intersect_ranges (enum value_range_type *vr0type,
8342 tree *vr0min, tree *vr0max,
8343 enum value_range_type vr1type,
8344 tree vr1min, tree vr1max)
8346 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8347 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8349 /* [] is vr0, () is vr1 in the following classification comments. */
8350 if (mineq && maxeq)
8352 /* [( )] */
8353 if (*vr0type == vr1type)
8354 /* Nothing to do for equal ranges. */
8356 else if ((*vr0type == VR_RANGE
8357 && vr1type == VR_ANTI_RANGE)
8358 || (*vr0type == VR_ANTI_RANGE
8359 && vr1type == VR_RANGE))
8361 /* For anti-range with range intersection the result is empty. */
8362 *vr0type = VR_UNDEFINED;
8363 *vr0min = NULL_TREE;
8364 *vr0max = NULL_TREE;
8366 else
8367 gcc_unreachable ();
8369 else if (operand_less_p (*vr0max, vr1min) == 1
8370 || operand_less_p (vr1max, *vr0min) == 1)
8372 /* [ ] ( ) or ( ) [ ]
8373 If the ranges have an empty intersection, the result of the
8374 intersect operation is the range for intersecting an
8375 anti-range with a range or empty when intersecting two ranges. */
8376 if (*vr0type == VR_RANGE
8377 && vr1type == VR_ANTI_RANGE)
8379 else if (*vr0type == VR_ANTI_RANGE
8380 && vr1type == VR_RANGE)
8382 *vr0type = vr1type;
8383 *vr0min = vr1min;
8384 *vr0max = vr1max;
8386 else if (*vr0type == VR_RANGE
8387 && vr1type == VR_RANGE)
8389 *vr0type = VR_UNDEFINED;
8390 *vr0min = NULL_TREE;
8391 *vr0max = NULL_TREE;
8393 else if (*vr0type == VR_ANTI_RANGE
8394 && vr1type == VR_ANTI_RANGE)
8396 /* If the anti-ranges are adjacent to each other merge them. */
8397 if (TREE_CODE (*vr0max) == INTEGER_CST
8398 && TREE_CODE (vr1min) == INTEGER_CST
8399 && operand_less_p (*vr0max, vr1min) == 1
8400 && integer_onep (int_const_binop (MINUS_EXPR,
8401 vr1min, *vr0max)))
8402 *vr0max = vr1max;
8403 else if (TREE_CODE (vr1max) == INTEGER_CST
8404 && TREE_CODE (*vr0min) == INTEGER_CST
8405 && operand_less_p (vr1max, *vr0min) == 1
8406 && integer_onep (int_const_binop (MINUS_EXPR,
8407 *vr0min, vr1max)))
8408 *vr0min = vr1min;
8409 /* Else arbitrarily take VR0. */
8412 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8413 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8415 /* [ ( ) ] or [( ) ] or [ ( )] */
8416 if (*vr0type == VR_RANGE
8417 && vr1type == VR_RANGE)
8419 /* If both are ranges the result is the inner one. */
8420 *vr0type = vr1type;
8421 *vr0min = vr1min;
8422 *vr0max = vr1max;
8424 else if (*vr0type == VR_RANGE
8425 && vr1type == VR_ANTI_RANGE)
8427 /* Choose the right gap if the left one is empty. */
8428 if (mineq)
8430 if (TREE_CODE (vr1max) == INTEGER_CST)
8431 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8432 build_int_cst (TREE_TYPE (vr1max), 1));
8433 else
8434 *vr0min = vr1max;
8436 /* Choose the left gap if the right one is empty. */
8437 else if (maxeq)
8439 if (TREE_CODE (vr1min) == INTEGER_CST)
8440 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8441 build_int_cst (TREE_TYPE (vr1min), 1));
8442 else
8443 *vr0max = vr1min;
8445 /* Choose the anti-range if the range is effectively varying. */
8446 else if (vrp_val_is_min (*vr0min)
8447 && vrp_val_is_max (*vr0max))
8449 *vr0type = vr1type;
8450 *vr0min = vr1min;
8451 *vr0max = vr1max;
8453 /* Else choose the range. */
8455 else if (*vr0type == VR_ANTI_RANGE
8456 && vr1type == VR_ANTI_RANGE)
8457 /* If both are anti-ranges the result is the outer one. */
8459 else if (*vr0type == VR_ANTI_RANGE
8460 && vr1type == VR_RANGE)
8462 /* The intersection is empty. */
8463 *vr0type = VR_UNDEFINED;
8464 *vr0min = NULL_TREE;
8465 *vr0max = NULL_TREE;
8467 else
8468 gcc_unreachable ();
8470 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8471 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8473 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8474 if (*vr0type == VR_RANGE
8475 && vr1type == VR_RANGE)
8476 /* Choose the inner range. */
8478 else if (*vr0type == VR_ANTI_RANGE
8479 && vr1type == VR_RANGE)
8481 /* Choose the right gap if the left is empty. */
8482 if (mineq)
8484 *vr0type = VR_RANGE;
8485 if (TREE_CODE (*vr0max) == INTEGER_CST)
8486 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8487 build_int_cst (TREE_TYPE (*vr0max), 1));
8488 else
8489 *vr0min = *vr0max;
8490 *vr0max = vr1max;
8492 /* Choose the left gap if the right is empty. */
8493 else if (maxeq)
8495 *vr0type = VR_RANGE;
8496 if (TREE_CODE (*vr0min) == INTEGER_CST)
8497 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8498 build_int_cst (TREE_TYPE (*vr0min), 1));
8499 else
8500 *vr0max = *vr0min;
8501 *vr0min = vr1min;
8503 /* Choose the anti-range if the range is effectively varying. */
8504 else if (vrp_val_is_min (vr1min)
8505 && vrp_val_is_max (vr1max))
8507 /* Else choose the range. */
8508 else
8510 *vr0type = vr1type;
8511 *vr0min = vr1min;
8512 *vr0max = vr1max;
8515 else if (*vr0type == VR_ANTI_RANGE
8516 && vr1type == VR_ANTI_RANGE)
8518 /* If both are anti-ranges the result is the outer one. */
8519 *vr0type = vr1type;
8520 *vr0min = vr1min;
8521 *vr0max = vr1max;
8523 else if (vr1type == VR_ANTI_RANGE
8524 && *vr0type == VR_RANGE)
8526 /* The intersection is empty. */
8527 *vr0type = VR_UNDEFINED;
8528 *vr0min = NULL_TREE;
8529 *vr0max = NULL_TREE;
8531 else
8532 gcc_unreachable ();
8534 else if ((operand_less_p (vr1min, *vr0max) == 1
8535 || operand_equal_p (vr1min, *vr0max, 0))
8536 && operand_less_p (*vr0min, vr1min) == 1)
8538 /* [ ( ] ) or [ ]( ) */
8539 if (*vr0type == VR_ANTI_RANGE
8540 && vr1type == VR_ANTI_RANGE)
8541 *vr0max = vr1max;
8542 else if (*vr0type == VR_RANGE
8543 && vr1type == VR_RANGE)
8544 *vr0min = vr1min;
8545 else if (*vr0type == VR_RANGE
8546 && vr1type == VR_ANTI_RANGE)
8548 if (TREE_CODE (vr1min) == INTEGER_CST)
8549 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8550 build_int_cst (TREE_TYPE (vr1min), 1));
8551 else
8552 *vr0max = vr1min;
8554 else if (*vr0type == VR_ANTI_RANGE
8555 && vr1type == VR_RANGE)
8557 *vr0type = VR_RANGE;
8558 if (TREE_CODE (*vr0max) == INTEGER_CST)
8559 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8560 build_int_cst (TREE_TYPE (*vr0max), 1));
8561 else
8562 *vr0min = *vr0max;
8563 *vr0max = vr1max;
8565 else
8566 gcc_unreachable ();
8568 else if ((operand_less_p (*vr0min, vr1max) == 1
8569 || operand_equal_p (*vr0min, vr1max, 0))
8570 && operand_less_p (vr1min, *vr0min) == 1)
8572 /* ( [ ) ] or ( )[ ] */
8573 if (*vr0type == VR_ANTI_RANGE
8574 && vr1type == VR_ANTI_RANGE)
8575 *vr0min = vr1min;
8576 else if (*vr0type == VR_RANGE
8577 && vr1type == VR_RANGE)
8578 *vr0max = vr1max;
8579 else if (*vr0type == VR_RANGE
8580 && vr1type == VR_ANTI_RANGE)
8582 if (TREE_CODE (vr1max) == INTEGER_CST)
8583 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8584 build_int_cst (TREE_TYPE (vr1max), 1));
8585 else
8586 *vr0min = vr1max;
8588 else if (*vr0type == VR_ANTI_RANGE
8589 && vr1type == VR_RANGE)
8591 *vr0type = VR_RANGE;
8592 if (TREE_CODE (*vr0min) == INTEGER_CST)
8593 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8594 build_int_cst (TREE_TYPE (*vr0min), 1));
8595 else
8596 *vr0max = *vr0min;
8597 *vr0min = vr1min;
8599 else
8600 gcc_unreachable ();
8603 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8604 result for the intersection. That's always a conservative
8605 correct estimate. */
8607 return;
8611 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8612 in *VR0. This may not be the smallest possible such range. */
8614 static void
8615 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8617 value_range_t saved;
8619 /* If either range is VR_VARYING the other one wins. */
8620 if (vr1->type == VR_VARYING)
8621 return;
8622 if (vr0->type == VR_VARYING)
8624 copy_value_range (vr0, vr1);
8625 return;
8628 /* When either range is VR_UNDEFINED the resulting range is
8629 VR_UNDEFINED, too. */
8630 if (vr0->type == VR_UNDEFINED)
8631 return;
8632 if (vr1->type == VR_UNDEFINED)
8634 set_value_range_to_undefined (vr0);
8635 return;
8638 /* Save the original vr0 so we can return it as conservative intersection
8639 result when our worker turns things to varying. */
8640 saved = *vr0;
8641 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8642 vr1->type, vr1->min, vr1->max);
8643 /* Make sure to canonicalize the result though as the inversion of a
8644 VR_RANGE can still be a VR_RANGE. */
8645 set_and_canonicalize_value_range (vr0, vr0->type,
8646 vr0->min, vr0->max, vr0->equiv);
8647 /* If that failed, use the saved original VR0. */
8648 if (vr0->type == VR_VARYING)
8650 *vr0 = saved;
8651 return;
8653 /* If the result is VR_UNDEFINED there is no need to mess with
8654 the equivalencies. */
8655 if (vr0->type == VR_UNDEFINED)
8656 return;
8658 /* The resulting set of equivalences for range intersection is the union of
8659 the two sets. */
8660 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8661 bitmap_ior_into (vr0->equiv, vr1->equiv);
8662 else if (vr1->equiv && !vr0->equiv)
8663 bitmap_copy (vr0->equiv, vr1->equiv);
8666 static void
8667 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8669 if (dump_file && (dump_flags & TDF_DETAILS))
8671 fprintf (dump_file, "Intersecting\n ");
8672 dump_value_range (dump_file, vr0);
8673 fprintf (dump_file, "\nand\n ");
8674 dump_value_range (dump_file, vr1);
8675 fprintf (dump_file, "\n");
8677 vrp_intersect_ranges_1 (vr0, vr1);
8678 if (dump_file && (dump_flags & TDF_DETAILS))
8680 fprintf (dump_file, "to\n ");
8681 dump_value_range (dump_file, vr0);
8682 fprintf (dump_file, "\n");
8686 /* Meet operation for value ranges. Given two value ranges VR0 and
8687 VR1, store in VR0 a range that contains both VR0 and VR1. This
8688 may not be the smallest possible such range. */
8690 static void
8691 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8693 value_range_t saved;
8695 if (vr0->type == VR_UNDEFINED)
8697 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8698 return;
8701 if (vr1->type == VR_UNDEFINED)
8703 /* VR0 already has the resulting range. */
8704 return;
8707 if (vr0->type == VR_VARYING)
8709 /* Nothing to do. VR0 already has the resulting range. */
8710 return;
8713 if (vr1->type == VR_VARYING)
8715 set_value_range_to_varying (vr0);
8716 return;
8719 saved = *vr0;
8720 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8721 vr1->type, vr1->min, vr1->max);
8722 if (vr0->type == VR_VARYING)
8724 /* Failed to find an efficient meet. Before giving up and setting
8725 the result to VARYING, see if we can at least derive a useful
8726 anti-range. FIXME, all this nonsense about distinguishing
8727 anti-ranges from ranges is necessary because of the odd
8728 semantics of range_includes_zero_p and friends. */
8729 if (((saved.type == VR_RANGE
8730 && range_includes_zero_p (saved.min, saved.max) == 0)
8731 || (saved.type == VR_ANTI_RANGE
8732 && range_includes_zero_p (saved.min, saved.max) == 1))
8733 && ((vr1->type == VR_RANGE
8734 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8735 || (vr1->type == VR_ANTI_RANGE
8736 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8738 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8740 /* Since this meet operation did not result from the meeting of
8741 two equivalent names, VR0 cannot have any equivalences. */
8742 if (vr0->equiv)
8743 bitmap_clear (vr0->equiv);
8744 return;
8747 set_value_range_to_varying (vr0);
8748 return;
8750 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8751 vr0->equiv);
8752 if (vr0->type == VR_VARYING)
8753 return;
8755 /* The resulting set of equivalences is always the intersection of
8756 the two sets. */
8757 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8758 bitmap_and_into (vr0->equiv, vr1->equiv);
8759 else if (vr0->equiv && !vr1->equiv)
8760 bitmap_clear (vr0->equiv);
8763 static void
8764 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8766 if (dump_file && (dump_flags & TDF_DETAILS))
8768 fprintf (dump_file, "Meeting\n ");
8769 dump_value_range (dump_file, vr0);
8770 fprintf (dump_file, "\nand\n ");
8771 dump_value_range (dump_file, vr1);
8772 fprintf (dump_file, "\n");
8774 vrp_meet_1 (vr0, vr1);
8775 if (dump_file && (dump_flags & TDF_DETAILS))
8777 fprintf (dump_file, "to\n ");
8778 dump_value_range (dump_file, vr0);
8779 fprintf (dump_file, "\n");
8784 /* Visit all arguments for PHI node PHI that flow through executable
8785 edges. If a valid value range can be derived from all the incoming
8786 value ranges, set a new range for the LHS of PHI. */
8788 static enum ssa_prop_result
8789 vrp_visit_phi_node (gphi *phi)
8791 size_t i;
8792 tree lhs = PHI_RESULT (phi);
8793 value_range_t *lhs_vr = get_value_range (lhs);
8794 value_range_t vr_result = VR_INITIALIZER;
8795 bool first = true;
8796 int edges, old_edges;
8797 struct loop *l;
8799 if (dump_file && (dump_flags & TDF_DETAILS))
8801 fprintf (dump_file, "\nVisiting PHI node: ");
8802 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8805 edges = 0;
8806 for (i = 0; i < gimple_phi_num_args (phi); i++)
8808 edge e = gimple_phi_arg_edge (phi, i);
8810 if (dump_file && (dump_flags & TDF_DETAILS))
8812 fprintf (dump_file,
8813 " Argument #%d (%d -> %d %sexecutable)\n",
8814 (int) i, e->src->index, e->dest->index,
8815 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8818 if (e->flags & EDGE_EXECUTABLE)
8820 tree arg = PHI_ARG_DEF (phi, i);
8821 value_range_t vr_arg;
8823 ++edges;
8825 if (TREE_CODE (arg) == SSA_NAME)
8827 vr_arg = *(get_value_range (arg));
8828 /* Do not allow equivalences or symbolic ranges to leak in from
8829 backedges. That creates invalid equivalencies.
8830 See PR53465 and PR54767. */
8831 if (e->flags & EDGE_DFS_BACK)
8833 if (vr_arg.type == VR_RANGE
8834 || vr_arg.type == VR_ANTI_RANGE)
8836 vr_arg.equiv = NULL;
8837 if (symbolic_range_p (&vr_arg))
8839 vr_arg.type = VR_VARYING;
8840 vr_arg.min = NULL_TREE;
8841 vr_arg.max = NULL_TREE;
8845 else
8847 /* If the non-backedge arguments range is VR_VARYING then
8848 we can still try recording a simple equivalence. */
8849 if (vr_arg.type == VR_VARYING)
8851 vr_arg.type = VR_RANGE;
8852 vr_arg.min = arg;
8853 vr_arg.max = arg;
8854 vr_arg.equiv = NULL;
8858 else
8860 if (TREE_OVERFLOW_P (arg))
8861 arg = drop_tree_overflow (arg);
8863 vr_arg.type = VR_RANGE;
8864 vr_arg.min = arg;
8865 vr_arg.max = arg;
8866 vr_arg.equiv = NULL;
8869 if (dump_file && (dump_flags & TDF_DETAILS))
8871 fprintf (dump_file, "\t");
8872 print_generic_expr (dump_file, arg, dump_flags);
8873 fprintf (dump_file, ": ");
8874 dump_value_range (dump_file, &vr_arg);
8875 fprintf (dump_file, "\n");
8878 if (first)
8879 copy_value_range (&vr_result, &vr_arg);
8880 else
8881 vrp_meet (&vr_result, &vr_arg);
8882 first = false;
8884 if (vr_result.type == VR_VARYING)
8885 break;
8889 if (vr_result.type == VR_VARYING)
8890 goto varying;
8891 else if (vr_result.type == VR_UNDEFINED)
8892 goto update_range;
8894 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8895 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8897 /* To prevent infinite iterations in the algorithm, derive ranges
8898 when the new value is slightly bigger or smaller than the
8899 previous one. We don't do this if we have seen a new executable
8900 edge; this helps us avoid an overflow infinity for conditionals
8901 which are not in a loop. If the old value-range was VR_UNDEFINED
8902 use the updated range and iterate one more time. */
8903 if (edges > 0
8904 && gimple_phi_num_args (phi) > 1
8905 && edges == old_edges
8906 && lhs_vr->type != VR_UNDEFINED)
8908 /* Compare old and new ranges, fall back to varying if the
8909 values are not comparable. */
8910 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8911 if (cmp_min == -2)
8912 goto varying;
8913 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8914 if (cmp_max == -2)
8915 goto varying;
8917 /* For non VR_RANGE or for pointers fall back to varying if
8918 the range changed. */
8919 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8920 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8921 && (cmp_min != 0 || cmp_max != 0))
8922 goto varying;
8924 /* If the new minimum is larger than than the previous one
8925 retain the old value. If the new minimum value is smaller
8926 than the previous one and not -INF go all the way to -INF + 1.
8927 In the first case, to avoid infinite bouncing between different
8928 minimums, and in the other case to avoid iterating millions of
8929 times to reach -INF. Going to -INF + 1 also lets the following
8930 iteration compute whether there will be any overflow, at the
8931 expense of one additional iteration. */
8932 if (cmp_min < 0)
8933 vr_result.min = lhs_vr->min;
8934 else if (cmp_min > 0
8935 && !vrp_val_is_min (vr_result.min))
8936 vr_result.min
8937 = int_const_binop (PLUS_EXPR,
8938 vrp_val_min (TREE_TYPE (vr_result.min)),
8939 build_int_cst (TREE_TYPE (vr_result.min), 1));
8941 /* Similarly for the maximum value. */
8942 if (cmp_max > 0)
8943 vr_result.max = lhs_vr->max;
8944 else if (cmp_max < 0
8945 && !vrp_val_is_max (vr_result.max))
8946 vr_result.max
8947 = int_const_binop (MINUS_EXPR,
8948 vrp_val_max (TREE_TYPE (vr_result.min)),
8949 build_int_cst (TREE_TYPE (vr_result.min), 1));
8951 /* If we dropped either bound to +-INF then if this is a loop
8952 PHI node SCEV may known more about its value-range. */
8953 if ((cmp_min > 0 || cmp_min < 0
8954 || cmp_max < 0 || cmp_max > 0)
8955 && (l = loop_containing_stmt (phi))
8956 && l->header == gimple_bb (phi))
8957 adjust_range_with_scev (&vr_result, l, phi, lhs);
8959 /* If we will end up with a (-INF, +INF) range, set it to
8960 VARYING. Same if the previous max value was invalid for
8961 the type and we end up with vr_result.min > vr_result.max. */
8962 if ((vrp_val_is_max (vr_result.max)
8963 && vrp_val_is_min (vr_result.min))
8964 || compare_values (vr_result.min,
8965 vr_result.max) > 0)
8966 goto varying;
8969 /* If the new range is different than the previous value, keep
8970 iterating. */
8971 update_range:
8972 if (update_value_range (lhs, &vr_result))
8974 if (dump_file && (dump_flags & TDF_DETAILS))
8976 fprintf (dump_file, "Found new range for ");
8977 print_generic_expr (dump_file, lhs, 0);
8978 fprintf (dump_file, ": ");
8979 dump_value_range (dump_file, &vr_result);
8980 fprintf (dump_file, "\n");
8983 if (vr_result.type == VR_VARYING)
8984 return SSA_PROP_VARYING;
8986 return SSA_PROP_INTERESTING;
8989 /* Nothing changed, don't add outgoing edges. */
8990 return SSA_PROP_NOT_INTERESTING;
8992 /* No match found. Set the LHS to VARYING. */
8993 varying:
8994 set_value_range_to_varying (lhs_vr);
8995 return SSA_PROP_VARYING;
8998 /* Simplify boolean operations if the source is known
8999 to be already a boolean. */
9000 static bool
9001 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9003 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9004 tree lhs, op0, op1;
9005 bool need_conversion;
9007 /* We handle only !=/== case here. */
9008 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
9010 op0 = gimple_assign_rhs1 (stmt);
9011 if (!op_with_boolean_value_range_p (op0))
9012 return false;
9014 op1 = gimple_assign_rhs2 (stmt);
9015 if (!op_with_boolean_value_range_p (op1))
9016 return false;
9018 /* Reduce number of cases to handle to NE_EXPR. As there is no
9019 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9020 if (rhs_code == EQ_EXPR)
9022 if (TREE_CODE (op1) == INTEGER_CST)
9023 op1 = int_const_binop (BIT_XOR_EXPR, op1,
9024 build_int_cst (TREE_TYPE (op1), 1));
9025 else
9026 return false;
9029 lhs = gimple_assign_lhs (stmt);
9030 need_conversion
9031 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
9033 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9034 if (need_conversion
9035 && !TYPE_UNSIGNED (TREE_TYPE (op0))
9036 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
9037 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
9038 return false;
9040 /* For A != 0 we can substitute A itself. */
9041 if (integer_zerop (op1))
9042 gimple_assign_set_rhs_with_ops (gsi,
9043 need_conversion
9044 ? NOP_EXPR : TREE_CODE (op0), op0);
9045 /* For A != B we substitute A ^ B. Either with conversion. */
9046 else if (need_conversion)
9048 tree tem = make_ssa_name (TREE_TYPE (op0));
9049 gassign *newop
9050 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
9051 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
9052 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9054 /* Or without. */
9055 else
9056 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9057 update_stmt (gsi_stmt (*gsi));
9059 return true;
9062 /* Simplify a division or modulo operator to a right shift or
9063 bitwise and if the first operand is unsigned or is greater
9064 than zero and the second operand is an exact power of two.
9065 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9066 into just op0 if op0's range is known to be a subset of
9067 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9068 modulo. */
9070 static bool
9071 simplify_div_or_mod_using_ranges (gimple stmt)
9073 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9074 tree val = NULL;
9075 tree op0 = gimple_assign_rhs1 (stmt);
9076 tree op1 = gimple_assign_rhs2 (stmt);
9077 value_range_t *vr = get_value_range (op0);
9079 if (rhs_code == TRUNC_MOD_EXPR
9080 && TREE_CODE (op1) == INTEGER_CST
9081 && tree_int_cst_sgn (op1) == 1
9082 && range_int_cst_p (vr)
9083 && tree_int_cst_lt (vr->max, op1))
9085 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9086 || tree_int_cst_sgn (vr->min) >= 0
9087 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9088 vr->min))
9090 /* If op0 already has the range op0 % op1 has,
9091 then TRUNC_MOD_EXPR won't change anything. */
9092 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9093 gimple_assign_set_rhs_from_tree (&gsi, op0);
9094 update_stmt (stmt);
9095 return true;
9099 if (!integer_pow2p (op1))
9100 return false;
9102 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9104 val = integer_one_node;
9106 else
9108 bool sop = false;
9110 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9112 if (val
9113 && sop
9114 && integer_onep (val)
9115 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9117 location_t location;
9119 if (!gimple_has_location (stmt))
9120 location = input_location;
9121 else
9122 location = gimple_location (stmt);
9123 warning_at (location, OPT_Wstrict_overflow,
9124 "assuming signed overflow does not occur when "
9125 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9129 if (val && integer_onep (val))
9131 tree t;
9133 if (rhs_code == TRUNC_DIV_EXPR)
9135 t = build_int_cst (integer_type_node, tree_log2 (op1));
9136 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9137 gimple_assign_set_rhs1 (stmt, op0);
9138 gimple_assign_set_rhs2 (stmt, t);
9140 else
9142 t = build_int_cst (TREE_TYPE (op1), 1);
9143 t = int_const_binop (MINUS_EXPR, op1, t);
9144 t = fold_convert (TREE_TYPE (op0), t);
9146 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9147 gimple_assign_set_rhs1 (stmt, op0);
9148 gimple_assign_set_rhs2 (stmt, t);
9151 update_stmt (stmt);
9152 return true;
9155 return false;
9158 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9159 ABS_EXPR. If the operand is <= 0, then simplify the
9160 ABS_EXPR into a NEGATE_EXPR. */
9162 static bool
9163 simplify_abs_using_ranges (gimple stmt)
9165 tree val = NULL;
9166 tree op = gimple_assign_rhs1 (stmt);
9167 tree type = TREE_TYPE (op);
9168 value_range_t *vr = get_value_range (op);
9170 if (TYPE_UNSIGNED (type))
9172 val = integer_zero_node;
9174 else if (vr)
9176 bool sop = false;
9178 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9179 if (!val)
9181 sop = false;
9182 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
9183 &sop);
9185 if (val)
9187 if (integer_zerop (val))
9188 val = integer_one_node;
9189 else if (integer_onep (val))
9190 val = integer_zero_node;
9194 if (val
9195 && (integer_onep (val) || integer_zerop (val)))
9197 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9199 location_t location;
9201 if (!gimple_has_location (stmt))
9202 location = input_location;
9203 else
9204 location = gimple_location (stmt);
9205 warning_at (location, OPT_Wstrict_overflow,
9206 "assuming signed overflow does not occur when "
9207 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9210 gimple_assign_set_rhs1 (stmt, op);
9211 if (integer_onep (val))
9212 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9213 else
9214 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9215 update_stmt (stmt);
9216 return true;
9220 return false;
9223 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9224 If all the bits that are being cleared by & are already
9225 known to be zero from VR, or all the bits that are being
9226 set by | are already known to be one from VR, the bit
9227 operation is redundant. */
9229 static bool
9230 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9232 tree op0 = gimple_assign_rhs1 (stmt);
9233 tree op1 = gimple_assign_rhs2 (stmt);
9234 tree op = NULL_TREE;
9235 value_range_t vr0 = VR_INITIALIZER;
9236 value_range_t vr1 = VR_INITIALIZER;
9237 wide_int may_be_nonzero0, may_be_nonzero1;
9238 wide_int must_be_nonzero0, must_be_nonzero1;
9239 wide_int mask;
9241 if (TREE_CODE (op0) == SSA_NAME)
9242 vr0 = *(get_value_range (op0));
9243 else if (is_gimple_min_invariant (op0))
9244 set_value_range_to_value (&vr0, op0, NULL);
9245 else
9246 return false;
9248 if (TREE_CODE (op1) == SSA_NAME)
9249 vr1 = *(get_value_range (op1));
9250 else if (is_gimple_min_invariant (op1))
9251 set_value_range_to_value (&vr1, op1, NULL);
9252 else
9253 return false;
9255 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9256 &must_be_nonzero0))
9257 return false;
9258 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9259 &must_be_nonzero1))
9260 return false;
9262 switch (gimple_assign_rhs_code (stmt))
9264 case BIT_AND_EXPR:
9265 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9266 if (mask == 0)
9268 op = op0;
9269 break;
9271 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9272 if (mask == 0)
9274 op = op1;
9275 break;
9277 break;
9278 case BIT_IOR_EXPR:
9279 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9280 if (mask == 0)
9282 op = op1;
9283 break;
9285 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9286 if (mask == 0)
9288 op = op0;
9289 break;
9291 break;
9292 default:
9293 gcc_unreachable ();
9296 if (op == NULL_TREE)
9297 return false;
9299 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9300 update_stmt (gsi_stmt (*gsi));
9301 return true;
9304 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9305 a known value range VR.
9307 If there is one and only one value which will satisfy the
9308 conditional, then return that value. Else return NULL.
9310 If signed overflow must be undefined for the value to satisfy
9311 the conditional, then set *STRICT_OVERFLOW_P to true. */
9313 static tree
9314 test_for_singularity (enum tree_code cond_code, tree op0,
9315 tree op1, value_range_t *vr,
9316 bool *strict_overflow_p)
9318 tree min = NULL;
9319 tree max = NULL;
9321 /* Extract minimum/maximum values which satisfy the
9322 the conditional as it was written. */
9323 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9325 /* This should not be negative infinity; there is no overflow
9326 here. */
9327 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9329 max = op1;
9330 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9332 tree one = build_int_cst (TREE_TYPE (op0), 1);
9333 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9334 if (EXPR_P (max))
9335 TREE_NO_WARNING (max) = 1;
9338 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9340 /* This should not be positive infinity; there is no overflow
9341 here. */
9342 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9344 min = op1;
9345 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9347 tree one = build_int_cst (TREE_TYPE (op0), 1);
9348 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9349 if (EXPR_P (min))
9350 TREE_NO_WARNING (min) = 1;
9354 /* Now refine the minimum and maximum values using any
9355 value range information we have for op0. */
9356 if (min && max)
9358 if (compare_values (vr->min, min) == 1)
9359 min = vr->min;
9360 if (compare_values (vr->max, max) == -1)
9361 max = vr->max;
9363 /* If the new min/max values have converged to a single value,
9364 then there is only one value which can satisfy the condition,
9365 return that value. */
9366 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9368 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9369 && is_overflow_infinity (vr->max))
9370 *strict_overflow_p = true;
9371 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9372 && is_overflow_infinity (vr->min))
9373 *strict_overflow_p = true;
9375 return min;
9378 return NULL;
9381 /* Return whether the value range *VR fits in an integer type specified
9382 by PRECISION and UNSIGNED_P. */
9384 static bool
9385 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9387 tree src_type;
9388 unsigned src_precision;
9389 widest_int tem;
9390 signop src_sgn;
9392 /* We can only handle integral and pointer types. */
9393 src_type = TREE_TYPE (vr->min);
9394 if (!INTEGRAL_TYPE_P (src_type)
9395 && !POINTER_TYPE_P (src_type))
9396 return false;
9398 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9399 and so is an identity transform. */
9400 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9401 src_sgn = TYPE_SIGN (src_type);
9402 if ((src_precision < dest_precision
9403 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9404 || (src_precision == dest_precision && src_sgn == dest_sgn))
9405 return true;
9407 /* Now we can only handle ranges with constant bounds. */
9408 if (vr->type != VR_RANGE
9409 || TREE_CODE (vr->min) != INTEGER_CST
9410 || TREE_CODE (vr->max) != INTEGER_CST)
9411 return false;
9413 /* For sign changes, the MSB of the wide_int has to be clear.
9414 An unsigned value with its MSB set cannot be represented by
9415 a signed wide_int, while a negative value cannot be represented
9416 by an unsigned wide_int. */
9417 if (src_sgn != dest_sgn
9418 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9419 return false;
9421 /* Then we can perform the conversion on both ends and compare
9422 the result for equality. */
9423 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9424 if (tem != wi::to_widest (vr->min))
9425 return false;
9426 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9427 if (tem != wi::to_widest (vr->max))
9428 return false;
9430 return true;
9433 /* Simplify a conditional using a relational operator to an equality
9434 test if the range information indicates only one value can satisfy
9435 the original conditional. */
9437 static bool
9438 simplify_cond_using_ranges (gcond *stmt)
9440 tree op0 = gimple_cond_lhs (stmt);
9441 tree op1 = gimple_cond_rhs (stmt);
9442 enum tree_code cond_code = gimple_cond_code (stmt);
9444 if (cond_code != NE_EXPR
9445 && cond_code != EQ_EXPR
9446 && TREE_CODE (op0) == SSA_NAME
9447 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9448 && is_gimple_min_invariant (op1))
9450 value_range_t *vr = get_value_range (op0);
9452 /* If we have range information for OP0, then we might be
9453 able to simplify this conditional. */
9454 if (vr->type == VR_RANGE)
9456 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9457 bool sop = false;
9458 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9460 if (new_tree
9461 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9463 if (dump_file)
9465 fprintf (dump_file, "Simplified relational ");
9466 print_gimple_stmt (dump_file, stmt, 0, 0);
9467 fprintf (dump_file, " into ");
9470 gimple_cond_set_code (stmt, EQ_EXPR);
9471 gimple_cond_set_lhs (stmt, op0);
9472 gimple_cond_set_rhs (stmt, new_tree);
9474 update_stmt (stmt);
9476 if (dump_file)
9478 print_gimple_stmt (dump_file, stmt, 0, 0);
9479 fprintf (dump_file, "\n");
9482 if (sop && issue_strict_overflow_warning (wc))
9484 location_t location = input_location;
9485 if (gimple_has_location (stmt))
9486 location = gimple_location (stmt);
9488 warning_at (location, OPT_Wstrict_overflow,
9489 "assuming signed overflow does not occur when "
9490 "simplifying conditional");
9493 return true;
9496 /* Try again after inverting the condition. We only deal
9497 with integral types here, so no need to worry about
9498 issues with inverting FP comparisons. */
9499 sop = false;
9500 new_tree = test_for_singularity
9501 (invert_tree_comparison (cond_code, false),
9502 op0, op1, vr, &sop);
9504 if (new_tree
9505 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9507 if (dump_file)
9509 fprintf (dump_file, "Simplified relational ");
9510 print_gimple_stmt (dump_file, stmt, 0, 0);
9511 fprintf (dump_file, " into ");
9514 gimple_cond_set_code (stmt, NE_EXPR);
9515 gimple_cond_set_lhs (stmt, op0);
9516 gimple_cond_set_rhs (stmt, new_tree);
9518 update_stmt (stmt);
9520 if (dump_file)
9522 print_gimple_stmt (dump_file, stmt, 0, 0);
9523 fprintf (dump_file, "\n");
9526 if (sop && issue_strict_overflow_warning (wc))
9528 location_t location = input_location;
9529 if (gimple_has_location (stmt))
9530 location = gimple_location (stmt);
9532 warning_at (location, OPT_Wstrict_overflow,
9533 "assuming signed overflow does not occur when "
9534 "simplifying conditional");
9537 return true;
9542 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9543 see if OP0 was set by a type conversion where the source of
9544 the conversion is another SSA_NAME with a range that fits
9545 into the range of OP0's type.
9547 If so, the conversion is redundant as the earlier SSA_NAME can be
9548 used for the comparison directly if we just massage the constant in the
9549 comparison. */
9550 if (TREE_CODE (op0) == SSA_NAME
9551 && TREE_CODE (op1) == INTEGER_CST)
9553 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9554 tree innerop;
9556 if (!is_gimple_assign (def_stmt)
9557 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9558 return false;
9560 innerop = gimple_assign_rhs1 (def_stmt);
9562 if (TREE_CODE (innerop) == SSA_NAME
9563 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9565 value_range_t *vr = get_value_range (innerop);
9567 if (range_int_cst_p (vr)
9568 && range_fits_type_p (vr,
9569 TYPE_PRECISION (TREE_TYPE (op0)),
9570 TYPE_SIGN (TREE_TYPE (op0)))
9571 && int_fits_type_p (op1, TREE_TYPE (innerop))
9572 /* The range must not have overflowed, or if it did overflow
9573 we must not be wrapping/trapping overflow and optimizing
9574 with strict overflow semantics. */
9575 && ((!is_negative_overflow_infinity (vr->min)
9576 && !is_positive_overflow_infinity (vr->max))
9577 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9579 /* If the range overflowed and the user has asked for warnings
9580 when strict overflow semantics were used to optimize code,
9581 issue an appropriate warning. */
9582 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9583 && (is_negative_overflow_infinity (vr->min)
9584 || is_positive_overflow_infinity (vr->max))
9585 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9587 location_t location;
9589 if (!gimple_has_location (stmt))
9590 location = input_location;
9591 else
9592 location = gimple_location (stmt);
9593 warning_at (location, OPT_Wstrict_overflow,
9594 "assuming signed overflow does not occur when "
9595 "simplifying conditional");
9598 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9599 gimple_cond_set_lhs (stmt, innerop);
9600 gimple_cond_set_rhs (stmt, newconst);
9601 return true;
9606 return false;
9609 /* Simplify a switch statement using the value range of the switch
9610 argument. */
9612 static bool
9613 simplify_switch_using_ranges (gswitch *stmt)
9615 tree op = gimple_switch_index (stmt);
9616 value_range_t *vr;
9617 bool take_default;
9618 edge e;
9619 edge_iterator ei;
9620 size_t i = 0, j = 0, n, n2;
9621 tree vec2;
9622 switch_update su;
9623 size_t k = 1, l = 0;
9625 if (TREE_CODE (op) == SSA_NAME)
9627 vr = get_value_range (op);
9629 /* We can only handle integer ranges. */
9630 if ((vr->type != VR_RANGE
9631 && vr->type != VR_ANTI_RANGE)
9632 || symbolic_range_p (vr))
9633 return false;
9635 /* Find case label for min/max of the value range. */
9636 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9638 else if (TREE_CODE (op) == INTEGER_CST)
9640 take_default = !find_case_label_index (stmt, 1, op, &i);
9641 if (take_default)
9643 i = 1;
9644 j = 0;
9646 else
9648 j = i;
9651 else
9652 return false;
9654 n = gimple_switch_num_labels (stmt);
9656 /* Bail out if this is just all edges taken. */
9657 if (i == 1
9658 && j == n - 1
9659 && take_default)
9660 return false;
9662 /* Build a new vector of taken case labels. */
9663 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9664 n2 = 0;
9666 /* Add the default edge, if necessary. */
9667 if (take_default)
9668 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9670 for (; i <= j; ++i, ++n2)
9671 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9673 for (; k <= l; ++k, ++n2)
9674 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9676 /* Mark needed edges. */
9677 for (i = 0; i < n2; ++i)
9679 e = find_edge (gimple_bb (stmt),
9680 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9681 e->aux = (void *)-1;
9684 /* Queue not needed edges for later removal. */
9685 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9687 if (e->aux == (void *)-1)
9689 e->aux = NULL;
9690 continue;
9693 if (dump_file && (dump_flags & TDF_DETAILS))
9695 fprintf (dump_file, "removing unreachable case label\n");
9697 to_remove_edges.safe_push (e);
9698 e->flags &= ~EDGE_EXECUTABLE;
9701 /* And queue an update for the stmt. */
9702 su.stmt = stmt;
9703 su.vec = vec2;
9704 to_update_switch_stmts.safe_push (su);
9705 return false;
9708 /* Simplify an integral conversion from an SSA name in STMT. */
9710 static bool
9711 simplify_conversion_using_ranges (gimple stmt)
9713 tree innerop, middleop, finaltype;
9714 gimple def_stmt;
9715 value_range_t *innervr;
9716 signop inner_sgn, middle_sgn, final_sgn;
9717 unsigned inner_prec, middle_prec, final_prec;
9718 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9720 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9721 if (!INTEGRAL_TYPE_P (finaltype))
9722 return false;
9723 middleop = gimple_assign_rhs1 (stmt);
9724 def_stmt = SSA_NAME_DEF_STMT (middleop);
9725 if (!is_gimple_assign (def_stmt)
9726 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9727 return false;
9728 innerop = gimple_assign_rhs1 (def_stmt);
9729 if (TREE_CODE (innerop) != SSA_NAME
9730 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9731 return false;
9733 /* Get the value-range of the inner operand. */
9734 innervr = get_value_range (innerop);
9735 if (innervr->type != VR_RANGE
9736 || TREE_CODE (innervr->min) != INTEGER_CST
9737 || TREE_CODE (innervr->max) != INTEGER_CST)
9738 return false;
9740 /* Simulate the conversion chain to check if the result is equal if
9741 the middle conversion is removed. */
9742 innermin = wi::to_widest (innervr->min);
9743 innermax = wi::to_widest (innervr->max);
9745 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9746 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9747 final_prec = TYPE_PRECISION (finaltype);
9749 /* If the first conversion is not injective, the second must not
9750 be widening. */
9751 if (wi::gtu_p (innermax - innermin,
9752 wi::mask <widest_int> (middle_prec, false))
9753 && middle_prec < final_prec)
9754 return false;
9755 /* We also want a medium value so that we can track the effect that
9756 narrowing conversions with sign change have. */
9757 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9758 if (inner_sgn == UNSIGNED)
9759 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9760 else
9761 innermed = 0;
9762 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9763 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9764 innermed = innermin;
9766 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9767 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9768 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9769 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9771 /* Require that the final conversion applied to both the original
9772 and the intermediate range produces the same result. */
9773 final_sgn = TYPE_SIGN (finaltype);
9774 if (wi::ext (middlemin, final_prec, final_sgn)
9775 != wi::ext (innermin, final_prec, final_sgn)
9776 || wi::ext (middlemed, final_prec, final_sgn)
9777 != wi::ext (innermed, final_prec, final_sgn)
9778 || wi::ext (middlemax, final_prec, final_sgn)
9779 != wi::ext (innermax, final_prec, final_sgn))
9780 return false;
9782 gimple_assign_set_rhs1 (stmt, innerop);
9783 update_stmt (stmt);
9784 return true;
9787 /* Simplify a conversion from integral SSA name to float in STMT. */
9789 static bool
9790 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9792 tree rhs1 = gimple_assign_rhs1 (stmt);
9793 value_range_t *vr = get_value_range (rhs1);
9794 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9795 machine_mode mode;
9796 tree tem;
9797 gassign *conv;
9799 /* We can only handle constant ranges. */
9800 if (vr->type != VR_RANGE
9801 || TREE_CODE (vr->min) != INTEGER_CST
9802 || TREE_CODE (vr->max) != INTEGER_CST)
9803 return false;
9805 /* First check if we can use a signed type in place of an unsigned. */
9806 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9807 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9808 != CODE_FOR_nothing)
9809 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9810 mode = TYPE_MODE (TREE_TYPE (rhs1));
9811 /* If we can do the conversion in the current input mode do nothing. */
9812 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9813 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9814 return false;
9815 /* Otherwise search for a mode we can use, starting from the narrowest
9816 integer mode available. */
9817 else
9819 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9822 /* If we cannot do a signed conversion to float from mode
9823 or if the value-range does not fit in the signed type
9824 try with a wider mode. */
9825 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9826 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9827 break;
9829 mode = GET_MODE_WIDER_MODE (mode);
9830 /* But do not widen the input. Instead leave that to the
9831 optabs expansion code. */
9832 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9833 return false;
9835 while (mode != VOIDmode);
9836 if (mode == VOIDmode)
9837 return false;
9840 /* It works, insert a truncation or sign-change before the
9841 float conversion. */
9842 tem = make_ssa_name (build_nonstandard_integer_type
9843 (GET_MODE_PRECISION (mode), 0));
9844 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9845 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9846 gimple_assign_set_rhs1 (stmt, tem);
9847 update_stmt (stmt);
9849 return true;
9852 /* Simplify an internal fn call using ranges if possible. */
9854 static bool
9855 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9857 enum tree_code subcode;
9858 bool is_ubsan = false;
9859 bool ovf = false;
9860 switch (gimple_call_internal_fn (stmt))
9862 case IFN_UBSAN_CHECK_ADD:
9863 subcode = PLUS_EXPR;
9864 is_ubsan = true;
9865 break;
9866 case IFN_UBSAN_CHECK_SUB:
9867 subcode = MINUS_EXPR;
9868 is_ubsan = true;
9869 break;
9870 case IFN_UBSAN_CHECK_MUL:
9871 subcode = MULT_EXPR;
9872 is_ubsan = true;
9873 break;
9874 case IFN_ADD_OVERFLOW:
9875 subcode = PLUS_EXPR;
9876 break;
9877 case IFN_SUB_OVERFLOW:
9878 subcode = MINUS_EXPR;
9879 break;
9880 case IFN_MUL_OVERFLOW:
9881 subcode = MULT_EXPR;
9882 break;
9883 default:
9884 return false;
9887 tree op0 = gimple_call_arg (stmt, 0);
9888 tree op1 = gimple_call_arg (stmt, 1);
9889 tree type;
9890 if (is_ubsan)
9891 type = TREE_TYPE (op0);
9892 else if (gimple_call_lhs (stmt) == NULL_TREE)
9893 return false;
9894 else
9895 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9896 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9897 || (is_ubsan && ovf))
9898 return false;
9900 gimple g;
9901 location_t loc = gimple_location (stmt);
9902 if (is_ubsan)
9903 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9904 else
9906 int prec = TYPE_PRECISION (type);
9907 tree utype = type;
9908 if (ovf
9909 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9910 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9911 utype = build_nonstandard_integer_type (prec, 1);
9912 if (TREE_CODE (op0) == INTEGER_CST)
9913 op0 = fold_convert (utype, op0);
9914 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9916 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9917 gimple_set_location (g, loc);
9918 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9919 op0 = gimple_assign_lhs (g);
9921 if (TREE_CODE (op1) == INTEGER_CST)
9922 op1 = fold_convert (utype, op1);
9923 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9925 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9926 gimple_set_location (g, loc);
9927 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9928 op1 = gimple_assign_lhs (g);
9930 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9931 gimple_set_location (g, loc);
9932 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9933 if (utype != type)
9935 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9936 gimple_assign_lhs (g));
9937 gimple_set_location (g, loc);
9938 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9940 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9941 gimple_assign_lhs (g),
9942 build_int_cst (type, ovf));
9944 gimple_set_location (g, loc);
9945 gsi_replace (gsi, g, false);
9946 return true;
9949 /* Simplify STMT using ranges if possible. */
9951 static bool
9952 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9954 gimple stmt = gsi_stmt (*gsi);
9955 if (is_gimple_assign (stmt))
9957 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9958 tree rhs1 = gimple_assign_rhs1 (stmt);
9960 switch (rhs_code)
9962 case EQ_EXPR:
9963 case NE_EXPR:
9964 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9965 if the RHS is zero or one, and the LHS are known to be boolean
9966 values. */
9967 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9968 return simplify_truth_ops_using_ranges (gsi, stmt);
9969 break;
9971 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9972 and BIT_AND_EXPR respectively if the first operand is greater
9973 than zero and the second operand is an exact power of two.
9974 Also optimize TRUNC_MOD_EXPR away if the second operand is
9975 constant and the first operand already has the right value
9976 range. */
9977 case TRUNC_DIV_EXPR:
9978 case TRUNC_MOD_EXPR:
9979 if (TREE_CODE (rhs1) == SSA_NAME
9980 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9981 return simplify_div_or_mod_using_ranges (stmt);
9982 break;
9984 /* Transform ABS (X) into X or -X as appropriate. */
9985 case ABS_EXPR:
9986 if (TREE_CODE (rhs1) == SSA_NAME
9987 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9988 return simplify_abs_using_ranges (stmt);
9989 break;
9991 case BIT_AND_EXPR:
9992 case BIT_IOR_EXPR:
9993 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9994 if all the bits being cleared are already cleared or
9995 all the bits being set are already set. */
9996 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9997 return simplify_bit_ops_using_ranges (gsi, stmt);
9998 break;
10000 CASE_CONVERT:
10001 if (TREE_CODE (rhs1) == SSA_NAME
10002 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10003 return simplify_conversion_using_ranges (stmt);
10004 break;
10006 case FLOAT_EXPR:
10007 if (TREE_CODE (rhs1) == SSA_NAME
10008 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10009 return simplify_float_conversion_using_ranges (gsi, stmt);
10010 break;
10012 default:
10013 break;
10016 else if (gimple_code (stmt) == GIMPLE_COND)
10017 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
10018 else if (gimple_code (stmt) == GIMPLE_SWITCH)
10019 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
10020 else if (is_gimple_call (stmt)
10021 && gimple_call_internal_p (stmt))
10022 return simplify_internal_call_using_ranges (gsi, stmt);
10024 return false;
10027 /* If the statement pointed by SI has a predicate whose value can be
10028 computed using the value range information computed by VRP, compute
10029 its value and return true. Otherwise, return false. */
10031 static bool
10032 fold_predicate_in (gimple_stmt_iterator *si)
10034 bool assignment_p = false;
10035 tree val;
10036 gimple stmt = gsi_stmt (*si);
10038 if (is_gimple_assign (stmt)
10039 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10041 assignment_p = true;
10042 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10043 gimple_assign_rhs1 (stmt),
10044 gimple_assign_rhs2 (stmt),
10045 stmt);
10047 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10048 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10049 gimple_cond_lhs (cond_stmt),
10050 gimple_cond_rhs (cond_stmt),
10051 stmt);
10052 else
10053 return false;
10055 if (val)
10057 if (assignment_p)
10058 val = fold_convert (gimple_expr_type (stmt), val);
10060 if (dump_file)
10062 fprintf (dump_file, "Folding predicate ");
10063 print_gimple_expr (dump_file, stmt, 0, 0);
10064 fprintf (dump_file, " to ");
10065 print_generic_expr (dump_file, val, 0);
10066 fprintf (dump_file, "\n");
10069 if (is_gimple_assign (stmt))
10070 gimple_assign_set_rhs_from_tree (si, val);
10071 else
10073 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10074 gcond *cond_stmt = as_a <gcond *> (stmt);
10075 if (integer_zerop (val))
10076 gimple_cond_make_false (cond_stmt);
10077 else if (integer_onep (val))
10078 gimple_cond_make_true (cond_stmt);
10079 else
10080 gcc_unreachable ();
10083 return true;
10086 return false;
10089 /* Callback for substitute_and_fold folding the stmt at *SI. */
10091 static bool
10092 vrp_fold_stmt (gimple_stmt_iterator *si)
10094 if (fold_predicate_in (si))
10095 return true;
10097 return simplify_stmt_using_ranges (si);
10100 /* Unwindable const/copy equivalences. */
10101 const_and_copies *equiv_stack;
10103 /* A trivial wrapper so that we can present the generic jump threading
10104 code with a simple API for simplifying statements. STMT is the
10105 statement we want to simplify, WITHIN_STMT provides the location
10106 for any overflow warnings. */
10108 static tree
10109 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
10111 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10112 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10113 gimple_cond_lhs (cond_stmt),
10114 gimple_cond_rhs (cond_stmt),
10115 within_stmt);
10117 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10119 value_range_t new_vr = VR_INITIALIZER;
10120 tree lhs = gimple_assign_lhs (assign_stmt);
10122 if (TREE_CODE (lhs) == SSA_NAME
10123 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10124 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10126 extract_range_from_assignment (&new_vr, assign_stmt);
10127 if (range_int_cst_singleton_p (&new_vr))
10128 return new_vr.min;
10132 return NULL_TREE;
10135 /* Blocks which have more than one predecessor and more than
10136 one successor present jump threading opportunities, i.e.,
10137 when the block is reached from a specific predecessor, we
10138 may be able to determine which of the outgoing edges will
10139 be traversed. When this optimization applies, we are able
10140 to avoid conditionals at runtime and we may expose secondary
10141 optimization opportunities.
10143 This routine is effectively a driver for the generic jump
10144 threading code. It basically just presents the generic code
10145 with edges that may be suitable for jump threading.
10147 Unlike DOM, we do not iterate VRP if jump threading was successful.
10148 While iterating may expose new opportunities for VRP, it is expected
10149 those opportunities would be very limited and the compile time cost
10150 to expose those opportunities would be significant.
10152 As jump threading opportunities are discovered, they are registered
10153 for later realization. */
10155 static void
10156 identify_jump_threads (void)
10158 basic_block bb;
10159 gcond *dummy;
10160 int i;
10161 edge e;
10163 /* Ugh. When substituting values earlier in this pass we can
10164 wipe the dominance information. So rebuild the dominator
10165 information as we need it within the jump threading code. */
10166 calculate_dominance_info (CDI_DOMINATORS);
10168 /* We do not allow VRP information to be used for jump threading
10169 across a back edge in the CFG. Otherwise it becomes too
10170 difficult to avoid eliminating loop exit tests. Of course
10171 EDGE_DFS_BACK is not accurate at this time so we have to
10172 recompute it. */
10173 mark_dfs_back_edges ();
10175 /* Do not thread across edges we are about to remove. Just marking
10176 them as EDGE_DFS_BACK will do. */
10177 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10178 e->flags |= EDGE_DFS_BACK;
10180 /* Allocate our unwinder stack to unwind any temporary equivalences
10181 that might be recorded. */
10182 equiv_stack = new const_and_copies (dump_file, dump_flags);
10184 /* To avoid lots of silly node creation, we create a single
10185 conditional and just modify it in-place when attempting to
10186 thread jumps. */
10187 dummy = gimple_build_cond (EQ_EXPR,
10188 integer_zero_node, integer_zero_node,
10189 NULL, NULL);
10191 /* Walk through all the blocks finding those which present a
10192 potential jump threading opportunity. We could set this up
10193 as a dominator walker and record data during the walk, but
10194 I doubt it's worth the effort for the classes of jump
10195 threading opportunities we are trying to identify at this
10196 point in compilation. */
10197 FOR_EACH_BB_FN (bb, cfun)
10199 gimple last;
10201 /* If the generic jump threading code does not find this block
10202 interesting, then there is nothing to do. */
10203 if (! potentially_threadable_block (bb))
10204 continue;
10206 last = last_stmt (bb);
10208 /* We're basically looking for a switch or any kind of conditional with
10209 integral or pointer type arguments. Note the type of the second
10210 argument will be the same as the first argument, so no need to
10211 check it explicitly.
10213 We also handle the case where there are no statements in the
10214 block. This come up with forwarder blocks that are not
10215 optimized away because they lead to a loop header. But we do
10216 want to thread through them as we can sometimes thread to the
10217 loop exit which is obviously profitable. */
10218 if (!last
10219 || gimple_code (last) == GIMPLE_SWITCH
10220 || (gimple_code (last) == GIMPLE_COND
10221 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10222 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10223 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10224 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10225 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10227 edge_iterator ei;
10229 /* We've got a block with multiple predecessors and multiple
10230 successors which also ends in a suitable conditional or
10231 switch statement. For each predecessor, see if we can thread
10232 it to a specific successor. */
10233 FOR_EACH_EDGE (e, ei, bb->preds)
10235 /* Do not thread across back edges or abnormal edges
10236 in the CFG. */
10237 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
10238 continue;
10240 thread_across_edge (dummy, e, true, equiv_stack,
10241 simplify_stmt_for_jump_threading);
10246 /* We do not actually update the CFG or SSA graphs at this point as
10247 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10248 handle ASSERT_EXPRs gracefully. */
10251 /* We identified all the jump threading opportunities earlier, but could
10252 not transform the CFG at that time. This routine transforms the
10253 CFG and arranges for the dominator tree to be rebuilt if necessary.
10255 Note the SSA graph update will occur during the normal TODO
10256 processing by the pass manager. */
10257 static void
10258 finalize_jump_threads (void)
10260 thread_through_all_blocks (false);
10261 delete equiv_stack;
10265 /* Traverse all the blocks folding conditionals with known ranges. */
10267 static void
10268 vrp_finalize (void)
10270 size_t i;
10272 values_propagated = true;
10274 if (dump_file)
10276 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10277 dump_all_value_ranges (dump_file);
10278 fprintf (dump_file, "\n");
10281 substitute_and_fold (op_with_constant_singleton_value_range,
10282 vrp_fold_stmt, false);
10284 if (warn_array_bounds && first_pass_instance)
10285 check_all_array_refs ();
10287 /* We must identify jump threading opportunities before we release
10288 the datastructures built by VRP. */
10289 identify_jump_threads ();
10291 /* Set value range to non pointer SSA_NAMEs. */
10292 for (i = 0; i < num_vr_values; i++)
10293 if (vr_value[i])
10295 tree name = ssa_name (i);
10297 if (!name
10298 || POINTER_TYPE_P (TREE_TYPE (name))
10299 || (vr_value[i]->type == VR_VARYING)
10300 || (vr_value[i]->type == VR_UNDEFINED))
10301 continue;
10303 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10304 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10305 && (vr_value[i]->type == VR_RANGE
10306 || vr_value[i]->type == VR_ANTI_RANGE))
10307 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10308 vr_value[i]->max);
10311 /* Free allocated memory. */
10312 for (i = 0; i < num_vr_values; i++)
10313 if (vr_value[i])
10315 BITMAP_FREE (vr_value[i]->equiv);
10316 free (vr_value[i]);
10319 free (vr_value);
10320 free (vr_phi_edge_counts);
10322 /* So that we can distinguish between VRP data being available
10323 and not available. */
10324 vr_value = NULL;
10325 vr_phi_edge_counts = NULL;
10329 /* Main entry point to VRP (Value Range Propagation). This pass is
10330 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10331 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10332 Programming Language Design and Implementation, pp. 67-78, 1995.
10333 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10335 This is essentially an SSA-CCP pass modified to deal with ranges
10336 instead of constants.
10338 While propagating ranges, we may find that two or more SSA name
10339 have equivalent, though distinct ranges. For instance,
10341 1 x_9 = p_3->a;
10342 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10343 3 if (p_4 == q_2)
10344 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10345 5 endif
10346 6 if (q_2)
10348 In the code above, pointer p_5 has range [q_2, q_2], but from the
10349 code we can also determine that p_5 cannot be NULL and, if q_2 had
10350 a non-varying range, p_5's range should also be compatible with it.
10352 These equivalences are created by two expressions: ASSERT_EXPR and
10353 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10354 result of another assertion, then we can use the fact that p_5 and
10355 p_4 are equivalent when evaluating p_5's range.
10357 Together with value ranges, we also propagate these equivalences
10358 between names so that we can take advantage of information from
10359 multiple ranges when doing final replacement. Note that this
10360 equivalency relation is transitive but not symmetric.
10362 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10363 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10364 in contexts where that assertion does not hold (e.g., in line 6).
10366 TODO, the main difference between this pass and Patterson's is that
10367 we do not propagate edge probabilities. We only compute whether
10368 edges can be taken or not. That is, instead of having a spectrum
10369 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10370 DON'T KNOW. In the future, it may be worthwhile to propagate
10371 probabilities to aid branch prediction. */
10373 static unsigned int
10374 execute_vrp (void)
10376 int i;
10377 edge e;
10378 switch_update *su;
10380 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10381 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10382 scev_initialize ();
10384 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10385 Inserting assertions may split edges which will invalidate
10386 EDGE_DFS_BACK. */
10387 insert_range_assertions ();
10389 to_remove_edges.create (10);
10390 to_update_switch_stmts.create (5);
10391 threadedge_initialize_values ();
10393 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10394 mark_dfs_back_edges ();
10396 vrp_initialize ();
10397 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10398 vrp_finalize ();
10400 free_numbers_of_iterations_estimates ();
10402 /* ASSERT_EXPRs must be removed before finalizing jump threads
10403 as finalizing jump threads calls the CFG cleanup code which
10404 does not properly handle ASSERT_EXPRs. */
10405 remove_range_assertions ();
10407 /* If we exposed any new variables, go ahead and put them into
10408 SSA form now, before we handle jump threading. This simplifies
10409 interactions between rewriting of _DECL nodes into SSA form
10410 and rewriting SSA_NAME nodes into SSA form after block
10411 duplication and CFG manipulation. */
10412 update_ssa (TODO_update_ssa);
10414 finalize_jump_threads ();
10416 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10417 CFG in a broken state and requires a cfg_cleanup run. */
10418 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10419 remove_edge (e);
10420 /* Update SWITCH_EXPR case label vector. */
10421 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10423 size_t j;
10424 size_t n = TREE_VEC_LENGTH (su->vec);
10425 tree label;
10426 gimple_switch_set_num_labels (su->stmt, n);
10427 for (j = 0; j < n; j++)
10428 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10429 /* As we may have replaced the default label with a regular one
10430 make sure to make it a real default label again. This ensures
10431 optimal expansion. */
10432 label = gimple_switch_label (su->stmt, 0);
10433 CASE_LOW (label) = NULL_TREE;
10434 CASE_HIGH (label) = NULL_TREE;
10437 if (to_remove_edges.length () > 0)
10439 free_dominance_info (CDI_DOMINATORS);
10440 loops_state_set (LOOPS_NEED_FIXUP);
10443 to_remove_edges.release ();
10444 to_update_switch_stmts.release ();
10445 threadedge_finalize_values ();
10447 scev_finalize ();
10448 loop_optimizer_finalize ();
10449 return 0;
10452 namespace {
10454 const pass_data pass_data_vrp =
10456 GIMPLE_PASS, /* type */
10457 "vrp", /* name */
10458 OPTGROUP_NONE, /* optinfo_flags */
10459 TV_TREE_VRP, /* tv_id */
10460 PROP_ssa, /* properties_required */
10461 0, /* properties_provided */
10462 0, /* properties_destroyed */
10463 0, /* todo_flags_start */
10464 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10467 class pass_vrp : public gimple_opt_pass
10469 public:
10470 pass_vrp (gcc::context *ctxt)
10471 : gimple_opt_pass (pass_data_vrp, ctxt)
10474 /* opt_pass methods: */
10475 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10476 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10477 virtual unsigned int execute (function *) { return execute_vrp (); }
10479 }; // class pass_vrp
10481 } // anon namespace
10483 gimple_opt_pass *
10484 make_pass_vrp (gcc::context *ctxt)
10486 return new pass_vrp (ctxt);