PR libstdc++/66354
[official-gcc.git] / gcc / tree-vrp.c
blob22587d00bc564239448c099f56ad723df9a076d4
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 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4560 std::swap (vr0, vr1);
4563 if (comp == EQ_EXPR)
4565 /* Equality may only be computed if both ranges represent
4566 exactly one value. */
4567 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4568 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4570 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4571 strict_overflow_p);
4572 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4573 strict_overflow_p);
4574 if (cmp_min == 0 && cmp_max == 0)
4575 return boolean_true_node;
4576 else if (cmp_min != -2 && cmp_max != -2)
4577 return boolean_false_node;
4579 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4580 else if (compare_values_warnv (vr0->min, vr1->max,
4581 strict_overflow_p) == 1
4582 || compare_values_warnv (vr1->min, vr0->max,
4583 strict_overflow_p) == 1)
4584 return boolean_false_node;
4586 return NULL_TREE;
4588 else if (comp == NE_EXPR)
4590 int cmp1, cmp2;
4592 /* If VR0 is completely to the left or completely to the right
4593 of VR1, they are always different. Notice that we need to
4594 make sure that both comparisons yield similar results to
4595 avoid comparing values that cannot be compared at
4596 compile-time. */
4597 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4598 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4599 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4600 return boolean_true_node;
4602 /* If VR0 and VR1 represent a single value and are identical,
4603 return false. */
4604 else if (compare_values_warnv (vr0->min, vr0->max,
4605 strict_overflow_p) == 0
4606 && compare_values_warnv (vr1->min, vr1->max,
4607 strict_overflow_p) == 0
4608 && compare_values_warnv (vr0->min, vr1->min,
4609 strict_overflow_p) == 0
4610 && compare_values_warnv (vr0->max, vr1->max,
4611 strict_overflow_p) == 0)
4612 return boolean_false_node;
4614 /* Otherwise, they may or may not be different. */
4615 else
4616 return NULL_TREE;
4618 else if (comp == LT_EXPR || comp == LE_EXPR)
4620 int tst;
4622 /* If VR0 is to the left of VR1, return true. */
4623 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4624 if ((comp == LT_EXPR && tst == -1)
4625 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4627 if (overflow_infinity_range_p (vr0)
4628 || overflow_infinity_range_p (vr1))
4629 *strict_overflow_p = true;
4630 return boolean_true_node;
4633 /* If VR0 is to the right of VR1, return false. */
4634 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4635 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4636 || (comp == LE_EXPR && tst == 1))
4638 if (overflow_infinity_range_p (vr0)
4639 || overflow_infinity_range_p (vr1))
4640 *strict_overflow_p = true;
4641 return boolean_false_node;
4644 /* Otherwise, we don't know. */
4645 return NULL_TREE;
4648 gcc_unreachable ();
4652 /* Given a value range VR, a value VAL and a comparison code COMP, return
4653 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4654 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4655 always returns false. Return NULL_TREE if it is not always
4656 possible to determine the value of the comparison. Also set
4657 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4658 infinity was used in the test. */
4660 static tree
4661 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4662 bool *strict_overflow_p)
4664 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4665 return NULL_TREE;
4667 /* Anti-ranges need to be handled separately. */
4668 if (vr->type == VR_ANTI_RANGE)
4670 /* For anti-ranges, the only predicates that we can compute at
4671 compile time are equality and inequality. */
4672 if (comp == GT_EXPR
4673 || comp == GE_EXPR
4674 || comp == LT_EXPR
4675 || comp == LE_EXPR)
4676 return NULL_TREE;
4678 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4679 if (value_inside_range (val, vr->min, vr->max) == 1)
4680 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4682 return NULL_TREE;
4685 if (!usable_range_p (vr, strict_overflow_p))
4686 return NULL_TREE;
4688 if (comp == EQ_EXPR)
4690 /* EQ_EXPR may only be computed if VR represents exactly
4691 one value. */
4692 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4694 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4695 if (cmp == 0)
4696 return boolean_true_node;
4697 else if (cmp == -1 || cmp == 1 || cmp == 2)
4698 return boolean_false_node;
4700 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4701 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4702 return boolean_false_node;
4704 return NULL_TREE;
4706 else if (comp == NE_EXPR)
4708 /* If VAL is not inside VR, then they are always different. */
4709 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4710 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4711 return boolean_true_node;
4713 /* If VR represents exactly one value equal to VAL, then return
4714 false. */
4715 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4716 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4717 return boolean_false_node;
4719 /* Otherwise, they may or may not be different. */
4720 return NULL_TREE;
4722 else if (comp == LT_EXPR || comp == LE_EXPR)
4724 int tst;
4726 /* If VR is to the left of VAL, return true. */
4727 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4728 if ((comp == LT_EXPR && tst == -1)
4729 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4731 if (overflow_infinity_range_p (vr))
4732 *strict_overflow_p = true;
4733 return boolean_true_node;
4736 /* If VR is to the right of VAL, return false. */
4737 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4738 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4739 || (comp == LE_EXPR && tst == 1))
4741 if (overflow_infinity_range_p (vr))
4742 *strict_overflow_p = true;
4743 return boolean_false_node;
4746 /* Otherwise, we don't know. */
4747 return NULL_TREE;
4749 else if (comp == GT_EXPR || comp == GE_EXPR)
4751 int tst;
4753 /* If VR is to the right of VAL, return true. */
4754 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4755 if ((comp == GT_EXPR && tst == 1)
4756 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4758 if (overflow_infinity_range_p (vr))
4759 *strict_overflow_p = true;
4760 return boolean_true_node;
4763 /* If VR is to the left of VAL, return false. */
4764 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4765 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4766 || (comp == GE_EXPR && tst == -1))
4768 if (overflow_infinity_range_p (vr))
4769 *strict_overflow_p = true;
4770 return boolean_false_node;
4773 /* Otherwise, we don't know. */
4774 return NULL_TREE;
4777 gcc_unreachable ();
4781 /* Debugging dumps. */
4783 void dump_value_range (FILE *, value_range_t *);
4784 void debug_value_range (value_range_t *);
4785 void dump_all_value_ranges (FILE *);
4786 void debug_all_value_ranges (void);
4787 void dump_vr_equiv (FILE *, bitmap);
4788 void debug_vr_equiv (bitmap);
4791 /* Dump value range VR to FILE. */
4793 void
4794 dump_value_range (FILE *file, value_range_t *vr)
4796 if (vr == NULL)
4797 fprintf (file, "[]");
4798 else if (vr->type == VR_UNDEFINED)
4799 fprintf (file, "UNDEFINED");
4800 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4802 tree type = TREE_TYPE (vr->min);
4804 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4806 if (is_negative_overflow_infinity (vr->min))
4807 fprintf (file, "-INF(OVF)");
4808 else if (INTEGRAL_TYPE_P (type)
4809 && !TYPE_UNSIGNED (type)
4810 && vrp_val_is_min (vr->min))
4811 fprintf (file, "-INF");
4812 else
4813 print_generic_expr (file, vr->min, 0);
4815 fprintf (file, ", ");
4817 if (is_positive_overflow_infinity (vr->max))
4818 fprintf (file, "+INF(OVF)");
4819 else if (INTEGRAL_TYPE_P (type)
4820 && vrp_val_is_max (vr->max))
4821 fprintf (file, "+INF");
4822 else
4823 print_generic_expr (file, vr->max, 0);
4825 fprintf (file, "]");
4827 if (vr->equiv)
4829 bitmap_iterator bi;
4830 unsigned i, c = 0;
4832 fprintf (file, " EQUIVALENCES: { ");
4834 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4836 print_generic_expr (file, ssa_name (i), 0);
4837 fprintf (file, " ");
4838 c++;
4841 fprintf (file, "} (%u elements)", c);
4844 else if (vr->type == VR_VARYING)
4845 fprintf (file, "VARYING");
4846 else
4847 fprintf (file, "INVALID RANGE");
4851 /* Dump value range VR to stderr. */
4853 DEBUG_FUNCTION void
4854 debug_value_range (value_range_t *vr)
4856 dump_value_range (stderr, vr);
4857 fprintf (stderr, "\n");
4861 /* Dump value ranges of all SSA_NAMEs to FILE. */
4863 void
4864 dump_all_value_ranges (FILE *file)
4866 size_t i;
4868 for (i = 0; i < num_vr_values; i++)
4870 if (vr_value[i])
4872 print_generic_expr (file, ssa_name (i), 0);
4873 fprintf (file, ": ");
4874 dump_value_range (file, vr_value[i]);
4875 fprintf (file, "\n");
4879 fprintf (file, "\n");
4883 /* Dump all value ranges to stderr. */
4885 DEBUG_FUNCTION void
4886 debug_all_value_ranges (void)
4888 dump_all_value_ranges (stderr);
4892 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4893 create a new SSA name N and return the assertion assignment
4894 'N = ASSERT_EXPR <V, V OP W>'. */
4896 static gimple
4897 build_assert_expr_for (tree cond, tree v)
4899 tree a;
4900 gassign *assertion;
4902 gcc_assert (TREE_CODE (v) == SSA_NAME
4903 && COMPARISON_CLASS_P (cond));
4905 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4906 assertion = gimple_build_assign (NULL_TREE, a);
4908 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4909 operand of the ASSERT_EXPR. Create it so the new name and the old one
4910 are registered in the replacement table so that we can fix the SSA web
4911 after adding all the ASSERT_EXPRs. */
4912 create_new_def_for (v, assertion, NULL);
4914 return assertion;
4918 /* Return false if EXPR is a predicate expression involving floating
4919 point values. */
4921 static inline bool
4922 fp_predicate (gimple stmt)
4924 GIMPLE_CHECK (stmt, GIMPLE_COND);
4926 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4929 /* If the range of values taken by OP can be inferred after STMT executes,
4930 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4931 describes the inferred range. Return true if a range could be
4932 inferred. */
4934 static bool
4935 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4937 *val_p = NULL_TREE;
4938 *comp_code_p = ERROR_MARK;
4940 /* Do not attempt to infer anything in names that flow through
4941 abnormal edges. */
4942 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4943 return false;
4945 /* Similarly, don't infer anything from statements that may throw
4946 exceptions. ??? Relax this requirement? */
4947 if (stmt_could_throw_p (stmt))
4948 return false;
4950 /* If STMT is the last statement of a basic block with no normal
4951 successors, there is no point inferring anything about any of its
4952 operands. We would not be able to find a proper insertion point
4953 for the assertion, anyway. */
4954 if (stmt_ends_bb_p (stmt))
4956 edge_iterator ei;
4957 edge e;
4959 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4960 if (!(e->flags & EDGE_ABNORMAL))
4961 break;
4962 if (e == NULL)
4963 return false;
4966 if (infer_nonnull_range (stmt, op, true, true))
4968 *val_p = build_int_cst (TREE_TYPE (op), 0);
4969 *comp_code_p = NE_EXPR;
4970 return true;
4973 return false;
4977 void dump_asserts_for (FILE *, tree);
4978 void debug_asserts_for (tree);
4979 void dump_all_asserts (FILE *);
4980 void debug_all_asserts (void);
4982 /* Dump all the registered assertions for NAME to FILE. */
4984 void
4985 dump_asserts_for (FILE *file, tree name)
4987 assert_locus_t loc;
4989 fprintf (file, "Assertions to be inserted for ");
4990 print_generic_expr (file, name, 0);
4991 fprintf (file, "\n");
4993 loc = asserts_for[SSA_NAME_VERSION (name)];
4994 while (loc)
4996 fprintf (file, "\t");
4997 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4998 fprintf (file, "\n\tBB #%d", loc->bb->index);
4999 if (loc->e)
5001 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
5002 loc->e->dest->index);
5003 dump_edge_info (file, loc->e, dump_flags, 0);
5005 fprintf (file, "\n\tPREDICATE: ");
5006 print_generic_expr (file, name, 0);
5007 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
5008 print_generic_expr (file, loc->val, 0);
5009 fprintf (file, "\n\n");
5010 loc = loc->next;
5013 fprintf (file, "\n");
5017 /* Dump all the registered assertions for NAME to stderr. */
5019 DEBUG_FUNCTION void
5020 debug_asserts_for (tree name)
5022 dump_asserts_for (stderr, name);
5026 /* Dump all the registered assertions for all the names to FILE. */
5028 void
5029 dump_all_asserts (FILE *file)
5031 unsigned i;
5032 bitmap_iterator bi;
5034 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
5035 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
5036 dump_asserts_for (file, ssa_name (i));
5037 fprintf (file, "\n");
5041 /* Dump all the registered assertions for all the names to stderr. */
5043 DEBUG_FUNCTION void
5044 debug_all_asserts (void)
5046 dump_all_asserts (stderr);
5050 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
5051 'EXPR COMP_CODE VAL' at a location that dominates block BB or
5052 E->DEST, then register this location as a possible insertion point
5053 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
5055 BB, E and SI provide the exact insertion point for the new
5056 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
5057 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
5058 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
5059 must not be NULL. */
5061 static void
5062 register_new_assert_for (tree name, tree expr,
5063 enum tree_code comp_code,
5064 tree val,
5065 basic_block bb,
5066 edge e,
5067 gimple_stmt_iterator si)
5069 assert_locus_t n, loc, last_loc;
5070 basic_block dest_bb;
5072 gcc_checking_assert (bb == NULL || e == NULL);
5074 if (e == NULL)
5075 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
5076 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
5078 /* Never build an assert comparing against an integer constant with
5079 TREE_OVERFLOW set. This confuses our undefined overflow warning
5080 machinery. */
5081 if (TREE_OVERFLOW_P (val))
5082 val = drop_tree_overflow (val);
5084 /* The new assertion A will be inserted at BB or E. We need to
5085 determine if the new location is dominated by a previously
5086 registered location for A. If we are doing an edge insertion,
5087 assume that A will be inserted at E->DEST. Note that this is not
5088 necessarily true.
5090 If E is a critical edge, it will be split. But even if E is
5091 split, the new block will dominate the same set of blocks that
5092 E->DEST dominates.
5094 The reverse, however, is not true, blocks dominated by E->DEST
5095 will not be dominated by the new block created to split E. So,
5096 if the insertion location is on a critical edge, we will not use
5097 the new location to move another assertion previously registered
5098 at a block dominated by E->DEST. */
5099 dest_bb = (bb) ? bb : e->dest;
5101 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5102 VAL at a block dominating DEST_BB, then we don't need to insert a new
5103 one. Similarly, if the same assertion already exists at a block
5104 dominated by DEST_BB and the new location is not on a critical
5105 edge, then update the existing location for the assertion (i.e.,
5106 move the assertion up in the dominance tree).
5108 Note, this is implemented as a simple linked list because there
5109 should not be more than a handful of assertions registered per
5110 name. If this becomes a performance problem, a table hashed by
5111 COMP_CODE and VAL could be implemented. */
5112 loc = asserts_for[SSA_NAME_VERSION (name)];
5113 last_loc = loc;
5114 while (loc)
5116 if (loc->comp_code == comp_code
5117 && (loc->val == val
5118 || operand_equal_p (loc->val, val, 0))
5119 && (loc->expr == expr
5120 || operand_equal_p (loc->expr, expr, 0)))
5122 /* If E is not a critical edge and DEST_BB
5123 dominates the existing location for the assertion, move
5124 the assertion up in the dominance tree by updating its
5125 location information. */
5126 if ((e == NULL || !EDGE_CRITICAL_P (e))
5127 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5129 loc->bb = dest_bb;
5130 loc->e = e;
5131 loc->si = si;
5132 return;
5136 /* Update the last node of the list and move to the next one. */
5137 last_loc = loc;
5138 loc = loc->next;
5141 /* If we didn't find an assertion already registered for
5142 NAME COMP_CODE VAL, add a new one at the end of the list of
5143 assertions associated with NAME. */
5144 n = XNEW (struct assert_locus_d);
5145 n->bb = dest_bb;
5146 n->e = e;
5147 n->si = si;
5148 n->comp_code = comp_code;
5149 n->val = val;
5150 n->expr = expr;
5151 n->next = NULL;
5153 if (last_loc)
5154 last_loc->next = n;
5155 else
5156 asserts_for[SSA_NAME_VERSION (name)] = n;
5158 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5161 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5162 Extract a suitable test code and value and store them into *CODE_P and
5163 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5165 If no extraction was possible, return FALSE, otherwise return TRUE.
5167 If INVERT is true, then we invert the result stored into *CODE_P. */
5169 static bool
5170 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5171 tree cond_op0, tree cond_op1,
5172 bool invert, enum tree_code *code_p,
5173 tree *val_p)
5175 enum tree_code comp_code;
5176 tree val;
5178 /* Otherwise, we have a comparison of the form NAME COMP VAL
5179 or VAL COMP NAME. */
5180 if (name == cond_op1)
5182 /* If the predicate is of the form VAL COMP NAME, flip
5183 COMP around because we need to register NAME as the
5184 first operand in the predicate. */
5185 comp_code = swap_tree_comparison (cond_code);
5186 val = cond_op0;
5188 else
5190 /* The comparison is of the form NAME COMP VAL, so the
5191 comparison code remains unchanged. */
5192 comp_code = cond_code;
5193 val = cond_op1;
5196 /* Invert the comparison code as necessary. */
5197 if (invert)
5198 comp_code = invert_tree_comparison (comp_code, 0);
5200 /* VRP does not handle float types. */
5201 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
5202 return false;
5204 /* Do not register always-false predicates.
5205 FIXME: this works around a limitation in fold() when dealing with
5206 enumerations. Given 'enum { N1, N2 } x;', fold will not
5207 fold 'if (x > N2)' to 'if (0)'. */
5208 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5209 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5211 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5212 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5214 if (comp_code == GT_EXPR
5215 && (!max
5216 || compare_values (val, max) == 0))
5217 return false;
5219 if (comp_code == LT_EXPR
5220 && (!min
5221 || compare_values (val, min) == 0))
5222 return false;
5224 *code_p = comp_code;
5225 *val_p = val;
5226 return true;
5229 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5230 (otherwise return VAL). VAL and MASK must be zero-extended for
5231 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5232 (to transform signed values into unsigned) and at the end xor
5233 SGNBIT back. */
5235 static wide_int
5236 masked_increment (const wide_int &val_in, const wide_int &mask,
5237 const wide_int &sgnbit, unsigned int prec)
5239 wide_int bit = wi::one (prec), res;
5240 unsigned int i;
5242 wide_int val = val_in ^ sgnbit;
5243 for (i = 0; i < prec; i++, bit += bit)
5245 res = mask;
5246 if ((res & bit) == 0)
5247 continue;
5248 res = bit - 1;
5249 res = (val + bit).and_not (res);
5250 res &= mask;
5251 if (wi::gtu_p (res, val))
5252 return res ^ sgnbit;
5254 return val ^ sgnbit;
5257 /* Try to register an edge assertion for SSA name NAME on edge E for
5258 the condition COND contributing to the conditional jump pointed to by BSI.
5259 Invert the condition COND if INVERT is true. */
5261 static void
5262 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5263 enum tree_code cond_code,
5264 tree cond_op0, tree cond_op1, bool invert)
5266 tree val;
5267 enum tree_code comp_code;
5269 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5270 cond_op0,
5271 cond_op1,
5272 invert, &comp_code, &val))
5273 return;
5275 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5276 reachable from E. */
5277 if (live_on_edge (e, name)
5278 && !has_single_use (name))
5279 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5281 /* In the case of NAME <= CST and NAME being defined as
5282 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5283 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5284 This catches range and anti-range tests. */
5285 if ((comp_code == LE_EXPR
5286 || comp_code == GT_EXPR)
5287 && TREE_CODE (val) == INTEGER_CST
5288 && TYPE_UNSIGNED (TREE_TYPE (val)))
5290 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5291 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5293 /* Extract CST2 from the (optional) addition. */
5294 if (is_gimple_assign (def_stmt)
5295 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5297 name2 = gimple_assign_rhs1 (def_stmt);
5298 cst2 = gimple_assign_rhs2 (def_stmt);
5299 if (TREE_CODE (name2) == SSA_NAME
5300 && TREE_CODE (cst2) == INTEGER_CST)
5301 def_stmt = SSA_NAME_DEF_STMT (name2);
5304 /* Extract NAME2 from the (optional) sign-changing cast. */
5305 if (gimple_assign_cast_p (def_stmt))
5307 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5308 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5309 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5310 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5311 name3 = gimple_assign_rhs1 (def_stmt);
5314 /* If name3 is used later, create an ASSERT_EXPR for it. */
5315 if (name3 != NULL_TREE
5316 && TREE_CODE (name3) == SSA_NAME
5317 && (cst2 == NULL_TREE
5318 || TREE_CODE (cst2) == INTEGER_CST)
5319 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5320 && live_on_edge (e, name3)
5321 && !has_single_use (name3))
5323 tree tmp;
5325 /* Build an expression for the range test. */
5326 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5327 if (cst2 != NULL_TREE)
5328 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5330 if (dump_file)
5332 fprintf (dump_file, "Adding assert for ");
5333 print_generic_expr (dump_file, name3, 0);
5334 fprintf (dump_file, " from ");
5335 print_generic_expr (dump_file, tmp, 0);
5336 fprintf (dump_file, "\n");
5339 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5342 /* If name2 is used later, create an ASSERT_EXPR for it. */
5343 if (name2 != NULL_TREE
5344 && TREE_CODE (name2) == SSA_NAME
5345 && TREE_CODE (cst2) == INTEGER_CST
5346 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5347 && live_on_edge (e, name2)
5348 && !has_single_use (name2))
5350 tree tmp;
5352 /* Build an expression for the range test. */
5353 tmp = name2;
5354 if (TREE_TYPE (name) != TREE_TYPE (name2))
5355 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5356 if (cst2 != NULL_TREE)
5357 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5359 if (dump_file)
5361 fprintf (dump_file, "Adding assert for ");
5362 print_generic_expr (dump_file, name2, 0);
5363 fprintf (dump_file, " from ");
5364 print_generic_expr (dump_file, tmp, 0);
5365 fprintf (dump_file, "\n");
5368 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5372 /* In the case of post-in/decrement tests like if (i++) ... and uses
5373 of the in/decremented value on the edge the extra name we want to
5374 assert for is not on the def chain of the name compared. Instead
5375 it is in the set of use stmts. */
5376 if ((comp_code == NE_EXPR
5377 || comp_code == EQ_EXPR)
5378 && TREE_CODE (val) == INTEGER_CST)
5380 imm_use_iterator ui;
5381 gimple use_stmt;
5382 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5384 /* Cut off to use-stmts that are in the predecessor. */
5385 if (gimple_bb (use_stmt) != e->src)
5386 continue;
5388 if (!is_gimple_assign (use_stmt))
5389 continue;
5391 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5392 if (code != PLUS_EXPR
5393 && code != MINUS_EXPR)
5394 continue;
5396 tree cst = gimple_assign_rhs2 (use_stmt);
5397 if (TREE_CODE (cst) != INTEGER_CST)
5398 continue;
5400 tree name2 = gimple_assign_lhs (use_stmt);
5401 if (live_on_edge (e, name2))
5403 cst = int_const_binop (code, val, cst);
5404 register_new_assert_for (name2, name2, comp_code, cst,
5405 NULL, e, bsi);
5410 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5411 && TREE_CODE (val) == INTEGER_CST)
5413 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5414 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5415 tree val2 = NULL_TREE;
5416 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5417 wide_int mask = wi::zero (prec);
5418 unsigned int nprec = prec;
5419 enum tree_code rhs_code = ERROR_MARK;
5421 if (is_gimple_assign (def_stmt))
5422 rhs_code = gimple_assign_rhs_code (def_stmt);
5424 /* Add asserts for NAME cmp CST and NAME being defined
5425 as NAME = (int) NAME2. */
5426 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5427 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5428 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5429 && gimple_assign_cast_p (def_stmt))
5431 name2 = gimple_assign_rhs1 (def_stmt);
5432 if (CONVERT_EXPR_CODE_P (rhs_code)
5433 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5434 && TYPE_UNSIGNED (TREE_TYPE (name2))
5435 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5436 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5437 || !tree_int_cst_equal (val,
5438 TYPE_MIN_VALUE (TREE_TYPE (val))))
5439 && live_on_edge (e, name2)
5440 && !has_single_use (name2))
5442 tree tmp, cst;
5443 enum tree_code new_comp_code = comp_code;
5445 cst = fold_convert (TREE_TYPE (name2),
5446 TYPE_MIN_VALUE (TREE_TYPE (val)));
5447 /* Build an expression for the range test. */
5448 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5449 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5450 fold_convert (TREE_TYPE (name2), val));
5451 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5453 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5454 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5455 build_int_cst (TREE_TYPE (name2), 1));
5458 if (dump_file)
5460 fprintf (dump_file, "Adding assert for ");
5461 print_generic_expr (dump_file, name2, 0);
5462 fprintf (dump_file, " from ");
5463 print_generic_expr (dump_file, tmp, 0);
5464 fprintf (dump_file, "\n");
5467 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5468 e, bsi);
5472 /* Add asserts for NAME cmp CST and NAME being defined as
5473 NAME = NAME2 >> CST2.
5475 Extract CST2 from the right shift. */
5476 if (rhs_code == RSHIFT_EXPR)
5478 name2 = gimple_assign_rhs1 (def_stmt);
5479 cst2 = gimple_assign_rhs2 (def_stmt);
5480 if (TREE_CODE (name2) == SSA_NAME
5481 && tree_fits_uhwi_p (cst2)
5482 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5483 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5484 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5485 && live_on_edge (e, name2)
5486 && !has_single_use (name2))
5488 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5489 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5492 if (val2 != NULL_TREE
5493 && TREE_CODE (val2) == INTEGER_CST
5494 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5495 TREE_TYPE (val),
5496 val2, cst2), val))
5498 enum tree_code new_comp_code = comp_code;
5499 tree tmp, new_val;
5501 tmp = name2;
5502 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5504 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5506 tree type = build_nonstandard_integer_type (prec, 1);
5507 tmp = build1 (NOP_EXPR, type, name2);
5508 val2 = fold_convert (type, val2);
5510 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5511 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5512 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5514 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5516 wide_int minval
5517 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5518 new_val = val2;
5519 if (minval == new_val)
5520 new_val = NULL_TREE;
5522 else
5524 wide_int maxval
5525 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5526 mask |= val2;
5527 if (mask == maxval)
5528 new_val = NULL_TREE;
5529 else
5530 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5533 if (new_val)
5535 if (dump_file)
5537 fprintf (dump_file, "Adding assert for ");
5538 print_generic_expr (dump_file, name2, 0);
5539 fprintf (dump_file, " from ");
5540 print_generic_expr (dump_file, tmp, 0);
5541 fprintf (dump_file, "\n");
5544 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5545 NULL, e, bsi);
5549 /* Add asserts for NAME cmp CST and NAME being defined as
5550 NAME = NAME2 & CST2.
5552 Extract CST2 from the and.
5554 Also handle
5555 NAME = (unsigned) NAME2;
5556 casts where NAME's type is unsigned and has smaller precision
5557 than NAME2's type as if it was NAME = NAME2 & MASK. */
5558 names[0] = NULL_TREE;
5559 names[1] = NULL_TREE;
5560 cst2 = NULL_TREE;
5561 if (rhs_code == BIT_AND_EXPR
5562 || (CONVERT_EXPR_CODE_P (rhs_code)
5563 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5564 && TYPE_UNSIGNED (TREE_TYPE (val))
5565 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5566 > prec))
5568 name2 = gimple_assign_rhs1 (def_stmt);
5569 if (rhs_code == BIT_AND_EXPR)
5570 cst2 = gimple_assign_rhs2 (def_stmt);
5571 else
5573 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5574 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5576 if (TREE_CODE (name2) == SSA_NAME
5577 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5578 && TREE_CODE (cst2) == INTEGER_CST
5579 && !integer_zerop (cst2)
5580 && (nprec > 1
5581 || TYPE_UNSIGNED (TREE_TYPE (val))))
5583 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5584 if (gimple_assign_cast_p (def_stmt2))
5586 names[1] = gimple_assign_rhs1 (def_stmt2);
5587 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5588 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5589 || (TYPE_PRECISION (TREE_TYPE (name2))
5590 != TYPE_PRECISION (TREE_TYPE (names[1])))
5591 || !live_on_edge (e, names[1])
5592 || has_single_use (names[1]))
5593 names[1] = NULL_TREE;
5595 if (live_on_edge (e, name2)
5596 && !has_single_use (name2))
5597 names[0] = name2;
5600 if (names[0] || names[1])
5602 wide_int minv, maxv, valv, cst2v;
5603 wide_int tem, sgnbit;
5604 bool valid_p = false, valn, cst2n;
5605 enum tree_code ccode = comp_code;
5607 valv = wide_int::from (val, nprec, UNSIGNED);
5608 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5609 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5610 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5611 /* If CST2 doesn't have most significant bit set,
5612 but VAL is negative, we have comparison like
5613 if ((x & 0x123) > -4) (always true). Just give up. */
5614 if (!cst2n && valn)
5615 ccode = ERROR_MARK;
5616 if (cst2n)
5617 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5618 else
5619 sgnbit = wi::zero (nprec);
5620 minv = valv & cst2v;
5621 switch (ccode)
5623 case EQ_EXPR:
5624 /* Minimum unsigned value for equality is VAL & CST2
5625 (should be equal to VAL, otherwise we probably should
5626 have folded the comparison into false) and
5627 maximum unsigned value is VAL | ~CST2. */
5628 maxv = valv | ~cst2v;
5629 valid_p = true;
5630 break;
5632 case NE_EXPR:
5633 tem = valv | ~cst2v;
5634 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5635 if (valv == 0)
5637 cst2n = false;
5638 sgnbit = wi::zero (nprec);
5639 goto gt_expr;
5641 /* If (VAL | ~CST2) is all ones, handle it as
5642 (X & CST2) < VAL. */
5643 if (tem == -1)
5645 cst2n = false;
5646 valn = false;
5647 sgnbit = wi::zero (nprec);
5648 goto lt_expr;
5650 if (!cst2n && wi::neg_p (cst2v))
5651 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5652 if (sgnbit != 0)
5654 if (valv == sgnbit)
5656 cst2n = true;
5657 valn = true;
5658 goto gt_expr;
5660 if (tem == wi::mask (nprec - 1, false, nprec))
5662 cst2n = true;
5663 goto lt_expr;
5665 if (!cst2n)
5666 sgnbit = wi::zero (nprec);
5668 break;
5670 case GE_EXPR:
5671 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5672 is VAL and maximum unsigned value is ~0. For signed
5673 comparison, if CST2 doesn't have most significant bit
5674 set, handle it similarly. If CST2 has MSB set,
5675 the minimum is the same, and maximum is ~0U/2. */
5676 if (minv != valv)
5678 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5679 VAL. */
5680 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5681 if (minv == valv)
5682 break;
5684 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5685 valid_p = true;
5686 break;
5688 case GT_EXPR:
5689 gt_expr:
5690 /* Find out smallest MINV where MINV > VAL
5691 && (MINV & CST2) == MINV, if any. If VAL is signed and
5692 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5693 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5694 if (minv == valv)
5695 break;
5696 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5697 valid_p = true;
5698 break;
5700 case LE_EXPR:
5701 /* Minimum unsigned value for <= is 0 and maximum
5702 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5703 Otherwise, find smallest VAL2 where VAL2 > VAL
5704 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5705 as maximum.
5706 For signed comparison, if CST2 doesn't have most
5707 significant bit set, handle it similarly. If CST2 has
5708 MSB set, the maximum is the same and minimum is INT_MIN. */
5709 if (minv == valv)
5710 maxv = valv;
5711 else
5713 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5714 if (maxv == valv)
5715 break;
5716 maxv -= 1;
5718 maxv |= ~cst2v;
5719 minv = sgnbit;
5720 valid_p = true;
5721 break;
5723 case LT_EXPR:
5724 lt_expr:
5725 /* Minimum unsigned value for < is 0 and maximum
5726 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5727 Otherwise, find smallest VAL2 where VAL2 > VAL
5728 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5729 as maximum.
5730 For signed comparison, if CST2 doesn't have most
5731 significant bit set, handle it similarly. If CST2 has
5732 MSB set, the maximum is the same and minimum is INT_MIN. */
5733 if (minv == valv)
5735 if (valv == sgnbit)
5736 break;
5737 maxv = valv;
5739 else
5741 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5742 if (maxv == valv)
5743 break;
5745 maxv -= 1;
5746 maxv |= ~cst2v;
5747 minv = sgnbit;
5748 valid_p = true;
5749 break;
5751 default:
5752 break;
5754 if (valid_p
5755 && (maxv - minv) != -1)
5757 tree tmp, new_val, type;
5758 int i;
5760 for (i = 0; i < 2; i++)
5761 if (names[i])
5763 wide_int maxv2 = maxv;
5764 tmp = names[i];
5765 type = TREE_TYPE (names[i]);
5766 if (!TYPE_UNSIGNED (type))
5768 type = build_nonstandard_integer_type (nprec, 1);
5769 tmp = build1 (NOP_EXPR, type, names[i]);
5771 if (minv != 0)
5773 tmp = build2 (PLUS_EXPR, type, tmp,
5774 wide_int_to_tree (type, -minv));
5775 maxv2 = maxv - minv;
5777 new_val = wide_int_to_tree (type, maxv2);
5779 if (dump_file)
5781 fprintf (dump_file, "Adding assert for ");
5782 print_generic_expr (dump_file, names[i], 0);
5783 fprintf (dump_file, " from ");
5784 print_generic_expr (dump_file, tmp, 0);
5785 fprintf (dump_file, "\n");
5788 register_new_assert_for (names[i], tmp, LE_EXPR,
5789 new_val, NULL, e, bsi);
5796 /* OP is an operand of a truth value expression which is known to have
5797 a particular value. Register any asserts for OP and for any
5798 operands in OP's defining statement.
5800 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5801 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5803 static void
5804 register_edge_assert_for_1 (tree op, enum tree_code code,
5805 edge e, gimple_stmt_iterator bsi)
5807 gimple op_def;
5808 tree val;
5809 enum tree_code rhs_code;
5811 /* We only care about SSA_NAMEs. */
5812 if (TREE_CODE (op) != SSA_NAME)
5813 return;
5815 /* We know that OP will have a zero or nonzero value. If OP is used
5816 more than once go ahead and register an assert for OP. */
5817 if (live_on_edge (e, op)
5818 && !has_single_use (op))
5820 val = build_int_cst (TREE_TYPE (op), 0);
5821 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5824 /* Now look at how OP is set. If it's set from a comparison,
5825 a truth operation or some bit operations, then we may be able
5826 to register information about the operands of that assignment. */
5827 op_def = SSA_NAME_DEF_STMT (op);
5828 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5829 return;
5831 rhs_code = gimple_assign_rhs_code (op_def);
5833 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5835 bool invert = (code == EQ_EXPR ? true : false);
5836 tree op0 = gimple_assign_rhs1 (op_def);
5837 tree op1 = gimple_assign_rhs2 (op_def);
5839 if (TREE_CODE (op0) == SSA_NAME)
5840 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5841 if (TREE_CODE (op1) == SSA_NAME)
5842 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5844 else if ((code == NE_EXPR
5845 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5846 || (code == EQ_EXPR
5847 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5849 /* Recurse on each operand. */
5850 tree op0 = gimple_assign_rhs1 (op_def);
5851 tree op1 = gimple_assign_rhs2 (op_def);
5852 if (TREE_CODE (op0) == SSA_NAME
5853 && has_single_use (op0))
5854 register_edge_assert_for_1 (op0, code, e, bsi);
5855 if (TREE_CODE (op1) == SSA_NAME
5856 && has_single_use (op1))
5857 register_edge_assert_for_1 (op1, code, e, bsi);
5859 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5860 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5862 /* Recurse, flipping CODE. */
5863 code = invert_tree_comparison (code, false);
5864 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5866 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5868 /* Recurse through the copy. */
5869 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5871 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5873 /* Recurse through the type conversion, unless it is a narrowing
5874 conversion or conversion from non-integral type. */
5875 tree rhs = gimple_assign_rhs1 (op_def);
5876 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5877 && (TYPE_PRECISION (TREE_TYPE (rhs))
5878 <= TYPE_PRECISION (TREE_TYPE (op))))
5879 register_edge_assert_for_1 (rhs, code, e, bsi);
5883 /* Try to register an edge assertion for SSA name NAME on edge E for
5884 the condition COND contributing to the conditional jump pointed to by
5885 SI. */
5887 static void
5888 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5889 enum tree_code cond_code, tree cond_op0,
5890 tree cond_op1)
5892 tree val;
5893 enum tree_code comp_code;
5894 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5896 /* Do not attempt to infer anything in names that flow through
5897 abnormal edges. */
5898 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5899 return;
5901 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5902 cond_op0, cond_op1,
5903 is_else_edge,
5904 &comp_code, &val))
5905 return;
5907 /* Register ASSERT_EXPRs for name. */
5908 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5909 cond_op1, is_else_edge);
5912 /* If COND is effectively an equality test of an SSA_NAME against
5913 the value zero or one, then we may be able to assert values
5914 for SSA_NAMEs which flow into COND. */
5916 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5917 statement of NAME we can assert both operands of the BIT_AND_EXPR
5918 have nonzero value. */
5919 if (((comp_code == EQ_EXPR && integer_onep (val))
5920 || (comp_code == NE_EXPR && integer_zerop (val))))
5922 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5924 if (is_gimple_assign (def_stmt)
5925 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5927 tree op0 = gimple_assign_rhs1 (def_stmt);
5928 tree op1 = gimple_assign_rhs2 (def_stmt);
5929 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5930 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5934 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5935 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5936 have zero value. */
5937 if (((comp_code == EQ_EXPR && integer_zerop (val))
5938 || (comp_code == NE_EXPR && integer_onep (val))))
5940 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5942 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5943 necessarily zero value, or if type-precision is one. */
5944 if (is_gimple_assign (def_stmt)
5945 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5946 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5947 || comp_code == EQ_EXPR)))
5949 tree op0 = gimple_assign_rhs1 (def_stmt);
5950 tree op1 = gimple_assign_rhs2 (def_stmt);
5951 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5952 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5958 /* Determine whether the outgoing edges of BB should receive an
5959 ASSERT_EXPR for each of the operands of BB's LAST statement.
5960 The last statement of BB must be a COND_EXPR.
5962 If any of the sub-graphs rooted at BB have an interesting use of
5963 the predicate operands, an assert location node is added to the
5964 list of assertions for the corresponding operands. */
5966 static void
5967 find_conditional_asserts (basic_block bb, gcond *last)
5969 gimple_stmt_iterator bsi;
5970 tree op;
5971 edge_iterator ei;
5972 edge e;
5973 ssa_op_iter iter;
5975 bsi = gsi_for_stmt (last);
5977 /* Look for uses of the operands in each of the sub-graphs
5978 rooted at BB. We need to check each of the outgoing edges
5979 separately, so that we know what kind of ASSERT_EXPR to
5980 insert. */
5981 FOR_EACH_EDGE (e, ei, bb->succs)
5983 if (e->dest == bb)
5984 continue;
5986 /* Register the necessary assertions for each operand in the
5987 conditional predicate. */
5988 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5989 register_edge_assert_for (op, e, bsi,
5990 gimple_cond_code (last),
5991 gimple_cond_lhs (last),
5992 gimple_cond_rhs (last));
5996 struct case_info
5998 tree expr;
5999 basic_block bb;
6002 /* Compare two case labels sorting first by the destination bb index
6003 and then by the case value. */
6005 static int
6006 compare_case_labels (const void *p1, const void *p2)
6008 const struct case_info *ci1 = (const struct case_info *) p1;
6009 const struct case_info *ci2 = (const struct case_info *) p2;
6010 int idx1 = ci1->bb->index;
6011 int idx2 = ci2->bb->index;
6013 if (idx1 < idx2)
6014 return -1;
6015 else if (idx1 == idx2)
6017 /* Make sure the default label is first in a group. */
6018 if (!CASE_LOW (ci1->expr))
6019 return -1;
6020 else if (!CASE_LOW (ci2->expr))
6021 return 1;
6022 else
6023 return tree_int_cst_compare (CASE_LOW (ci1->expr),
6024 CASE_LOW (ci2->expr));
6026 else
6027 return 1;
6030 /* Determine whether the outgoing edges of BB should receive an
6031 ASSERT_EXPR for each of the operands of BB's LAST statement.
6032 The last statement of BB must be a SWITCH_EXPR.
6034 If any of the sub-graphs rooted at BB have an interesting use of
6035 the predicate operands, an assert location node is added to the
6036 list of assertions for the corresponding operands. */
6038 static void
6039 find_switch_asserts (basic_block bb, gswitch *last)
6041 gimple_stmt_iterator bsi;
6042 tree op;
6043 edge e;
6044 struct case_info *ci;
6045 size_t n = gimple_switch_num_labels (last);
6046 #if GCC_VERSION >= 4000
6047 unsigned int idx;
6048 #else
6049 /* Work around GCC 3.4 bug (PR 37086). */
6050 volatile unsigned int idx;
6051 #endif
6053 bsi = gsi_for_stmt (last);
6054 op = gimple_switch_index (last);
6055 if (TREE_CODE (op) != SSA_NAME)
6056 return;
6058 /* Build a vector of case labels sorted by destination label. */
6059 ci = XNEWVEC (struct case_info, n);
6060 for (idx = 0; idx < n; ++idx)
6062 ci[idx].expr = gimple_switch_label (last, idx);
6063 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
6065 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
6067 for (idx = 0; idx < n; ++idx)
6069 tree min, max;
6070 tree cl = ci[idx].expr;
6071 basic_block cbb = ci[idx].bb;
6073 min = CASE_LOW (cl);
6074 max = CASE_HIGH (cl);
6076 /* If there are multiple case labels with the same destination
6077 we need to combine them to a single value range for the edge. */
6078 if (idx + 1 < n && cbb == ci[idx + 1].bb)
6080 /* Skip labels until the last of the group. */
6081 do {
6082 ++idx;
6083 } while (idx < n && cbb == ci[idx].bb);
6084 --idx;
6086 /* Pick up the maximum of the case label range. */
6087 if (CASE_HIGH (ci[idx].expr))
6088 max = CASE_HIGH (ci[idx].expr);
6089 else
6090 max = CASE_LOW (ci[idx].expr);
6093 /* Nothing to do if the range includes the default label until we
6094 can register anti-ranges. */
6095 if (min == NULL_TREE)
6096 continue;
6098 /* Find the edge to register the assert expr on. */
6099 e = find_edge (bb, cbb);
6101 /* Register the necessary assertions for the operand in the
6102 SWITCH_EXPR. */
6103 register_edge_assert_for (op, e, bsi,
6104 max ? GE_EXPR : EQ_EXPR,
6105 op, fold_convert (TREE_TYPE (op), min));
6106 if (max)
6107 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6108 fold_convert (TREE_TYPE (op), max));
6111 XDELETEVEC (ci);
6115 /* Traverse all the statements in block BB looking for statements that
6116 may generate useful assertions for the SSA names in their operand.
6117 If a statement produces a useful assertion A for name N_i, then the
6118 list of assertions already generated for N_i is scanned to
6119 determine if A is actually needed.
6121 If N_i already had the assertion A at a location dominating the
6122 current location, then nothing needs to be done. Otherwise, the
6123 new location for A is recorded instead.
6125 1- For every statement S in BB, all the variables used by S are
6126 added to bitmap FOUND_IN_SUBGRAPH.
6128 2- If statement S uses an operand N in a way that exposes a known
6129 value range for N, then if N was not already generated by an
6130 ASSERT_EXPR, create a new assert location for N. For instance,
6131 if N is a pointer and the statement dereferences it, we can
6132 assume that N is not NULL.
6134 3- COND_EXPRs are a special case of #2. We can derive range
6135 information from the predicate but need to insert different
6136 ASSERT_EXPRs for each of the sub-graphs rooted at the
6137 conditional block. If the last statement of BB is a conditional
6138 expression of the form 'X op Y', then
6140 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6142 b) If the conditional is the only entry point to the sub-graph
6143 corresponding to the THEN_CLAUSE, recurse into it. On
6144 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6145 an ASSERT_EXPR is added for the corresponding variable.
6147 c) Repeat step (b) on the ELSE_CLAUSE.
6149 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6151 For instance,
6153 if (a == 9)
6154 b = a;
6155 else
6156 b = c + 1;
6158 In this case, an assertion on the THEN clause is useful to
6159 determine that 'a' is always 9 on that edge. However, an assertion
6160 on the ELSE clause would be unnecessary.
6162 4- If BB does not end in a conditional expression, then we recurse
6163 into BB's dominator children.
6165 At the end of the recursive traversal, every SSA name will have a
6166 list of locations where ASSERT_EXPRs should be added. When a new
6167 location for name N is found, it is registered by calling
6168 register_new_assert_for. That function keeps track of all the
6169 registered assertions to prevent adding unnecessary assertions.
6170 For instance, if a pointer P_4 is dereferenced more than once in a
6171 dominator tree, only the location dominating all the dereference of
6172 P_4 will receive an ASSERT_EXPR. */
6174 static void
6175 find_assert_locations_1 (basic_block bb, sbitmap live)
6177 gimple last;
6179 last = last_stmt (bb);
6181 /* If BB's last statement is a conditional statement involving integer
6182 operands, determine if we need to add ASSERT_EXPRs. */
6183 if (last
6184 && gimple_code (last) == GIMPLE_COND
6185 && !fp_predicate (last)
6186 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6187 find_conditional_asserts (bb, as_a <gcond *> (last));
6189 /* If BB's last statement is a switch statement involving integer
6190 operands, determine if we need to add ASSERT_EXPRs. */
6191 if (last
6192 && gimple_code (last) == GIMPLE_SWITCH
6193 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6194 find_switch_asserts (bb, as_a <gswitch *> (last));
6196 /* Traverse all the statements in BB marking used names and looking
6197 for statements that may infer assertions for their used operands. */
6198 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6199 gsi_prev (&si))
6201 gimple stmt;
6202 tree op;
6203 ssa_op_iter i;
6205 stmt = gsi_stmt (si);
6207 if (is_gimple_debug (stmt))
6208 continue;
6210 /* See if we can derive an assertion for any of STMT's operands. */
6211 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6213 tree value;
6214 enum tree_code comp_code;
6216 /* If op is not live beyond this stmt, do not bother to insert
6217 asserts for it. */
6218 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6219 continue;
6221 /* If OP is used in such a way that we can infer a value
6222 range for it, and we don't find a previous assertion for
6223 it, create a new assertion location node for OP. */
6224 if (infer_value_range (stmt, op, &comp_code, &value))
6226 /* If we are able to infer a nonzero value range for OP,
6227 then walk backwards through the use-def chain to see if OP
6228 was set via a typecast.
6230 If so, then we can also infer a nonzero value range
6231 for the operand of the NOP_EXPR. */
6232 if (comp_code == NE_EXPR && integer_zerop (value))
6234 tree t = op;
6235 gimple def_stmt = SSA_NAME_DEF_STMT (t);
6237 while (is_gimple_assign (def_stmt)
6238 && CONVERT_EXPR_CODE_P
6239 (gimple_assign_rhs_code (def_stmt))
6240 && TREE_CODE
6241 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6242 && POINTER_TYPE_P
6243 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6245 t = gimple_assign_rhs1 (def_stmt);
6246 def_stmt = SSA_NAME_DEF_STMT (t);
6248 /* Note we want to register the assert for the
6249 operand of the NOP_EXPR after SI, not after the
6250 conversion. */
6251 if (! has_single_use (t))
6252 register_new_assert_for (t, t, comp_code, value,
6253 bb, NULL, si);
6257 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6261 /* Update live. */
6262 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6263 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6264 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6265 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6268 /* Traverse all PHI nodes in BB, updating live. */
6269 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6270 gsi_next (&si))
6272 use_operand_p arg_p;
6273 ssa_op_iter i;
6274 gphi *phi = si.phi ();
6275 tree res = gimple_phi_result (phi);
6277 if (virtual_operand_p (res))
6278 continue;
6280 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6282 tree arg = USE_FROM_PTR (arg_p);
6283 if (TREE_CODE (arg) == SSA_NAME)
6284 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6287 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6291 /* Do an RPO walk over the function computing SSA name liveness
6292 on-the-fly and deciding on assert expressions to insert. */
6294 static void
6295 find_assert_locations (void)
6297 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6298 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6299 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6300 int rpo_cnt, i;
6302 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6303 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6304 for (i = 0; i < rpo_cnt; ++i)
6305 bb_rpo[rpo[i]] = i;
6307 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6308 the order we compute liveness and insert asserts we otherwise
6309 fail to insert asserts into the loop latch. */
6310 loop_p loop;
6311 FOR_EACH_LOOP (loop, 0)
6313 i = loop->latch->index;
6314 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6315 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6316 !gsi_end_p (gsi); gsi_next (&gsi))
6318 gphi *phi = gsi.phi ();
6319 if (virtual_operand_p (gimple_phi_result (phi)))
6320 continue;
6321 tree arg = gimple_phi_arg_def (phi, j);
6322 if (TREE_CODE (arg) == SSA_NAME)
6324 if (live[i] == NULL)
6326 live[i] = sbitmap_alloc (num_ssa_names);
6327 bitmap_clear (live[i]);
6329 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6334 for (i = rpo_cnt - 1; i >= 0; --i)
6336 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6337 edge e;
6338 edge_iterator ei;
6340 if (!live[rpo[i]])
6342 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6343 bitmap_clear (live[rpo[i]]);
6346 /* Process BB and update the live information with uses in
6347 this block. */
6348 find_assert_locations_1 (bb, live[rpo[i]]);
6350 /* Merge liveness into the predecessor blocks and free it. */
6351 if (!bitmap_empty_p (live[rpo[i]]))
6353 int pred_rpo = i;
6354 FOR_EACH_EDGE (e, ei, bb->preds)
6356 int pred = e->src->index;
6357 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6358 continue;
6360 if (!live[pred])
6362 live[pred] = sbitmap_alloc (num_ssa_names);
6363 bitmap_clear (live[pred]);
6365 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6367 if (bb_rpo[pred] < pred_rpo)
6368 pred_rpo = bb_rpo[pred];
6371 /* Record the RPO number of the last visited block that needs
6372 live information from this block. */
6373 last_rpo[rpo[i]] = pred_rpo;
6375 else
6377 sbitmap_free (live[rpo[i]]);
6378 live[rpo[i]] = NULL;
6381 /* We can free all successors live bitmaps if all their
6382 predecessors have been visited already. */
6383 FOR_EACH_EDGE (e, ei, bb->succs)
6384 if (last_rpo[e->dest->index] == i
6385 && live[e->dest->index])
6387 sbitmap_free (live[e->dest->index]);
6388 live[e->dest->index] = NULL;
6392 XDELETEVEC (rpo);
6393 XDELETEVEC (bb_rpo);
6394 XDELETEVEC (last_rpo);
6395 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6396 if (live[i])
6397 sbitmap_free (live[i]);
6398 XDELETEVEC (live);
6401 /* Create an ASSERT_EXPR for NAME and insert it in the location
6402 indicated by LOC. Return true if we made any edge insertions. */
6404 static bool
6405 process_assert_insertions_for (tree name, assert_locus_t loc)
6407 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6408 gimple stmt;
6409 tree cond;
6410 gimple assert_stmt;
6411 edge_iterator ei;
6412 edge e;
6414 /* If we have X <=> X do not insert an assert expr for that. */
6415 if (loc->expr == loc->val)
6416 return false;
6418 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6419 assert_stmt = build_assert_expr_for (cond, name);
6420 if (loc->e)
6422 /* We have been asked to insert the assertion on an edge. This
6423 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6424 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6425 || (gimple_code (gsi_stmt (loc->si))
6426 == GIMPLE_SWITCH));
6428 gsi_insert_on_edge (loc->e, assert_stmt);
6429 return true;
6432 /* Otherwise, we can insert right after LOC->SI iff the
6433 statement must not be the last statement in the block. */
6434 stmt = gsi_stmt (loc->si);
6435 if (!stmt_ends_bb_p (stmt))
6437 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6438 return false;
6441 /* If STMT must be the last statement in BB, we can only insert new
6442 assertions on the non-abnormal edge out of BB. Note that since
6443 STMT is not control flow, there may only be one non-abnormal edge
6444 out of BB. */
6445 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6446 if (!(e->flags & EDGE_ABNORMAL))
6448 gsi_insert_on_edge (e, assert_stmt);
6449 return true;
6452 gcc_unreachable ();
6456 /* Process all the insertions registered for every name N_i registered
6457 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6458 found in ASSERTS_FOR[i]. */
6460 static void
6461 process_assert_insertions (void)
6463 unsigned i;
6464 bitmap_iterator bi;
6465 bool update_edges_p = false;
6466 int num_asserts = 0;
6468 if (dump_file && (dump_flags & TDF_DETAILS))
6469 dump_all_asserts (dump_file);
6471 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6473 assert_locus_t loc = asserts_for[i];
6474 gcc_assert (loc);
6476 while (loc)
6478 assert_locus_t next = loc->next;
6479 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6480 free (loc);
6481 loc = next;
6482 num_asserts++;
6486 if (update_edges_p)
6487 gsi_commit_edge_inserts ();
6489 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6490 num_asserts);
6494 /* Traverse the flowgraph looking for conditional jumps to insert range
6495 expressions. These range expressions are meant to provide information
6496 to optimizations that need to reason in terms of value ranges. They
6497 will not be expanded into RTL. For instance, given:
6499 x = ...
6500 y = ...
6501 if (x < y)
6502 y = x - 2;
6503 else
6504 x = y + 3;
6506 this pass will transform the code into:
6508 x = ...
6509 y = ...
6510 if (x < y)
6512 x = ASSERT_EXPR <x, x < y>
6513 y = x - 2
6515 else
6517 y = ASSERT_EXPR <y, x >= y>
6518 x = y + 3
6521 The idea is that once copy and constant propagation have run, other
6522 optimizations will be able to determine what ranges of values can 'x'
6523 take in different paths of the code, simply by checking the reaching
6524 definition of 'x'. */
6526 static void
6527 insert_range_assertions (void)
6529 need_assert_for = BITMAP_ALLOC (NULL);
6530 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6532 calculate_dominance_info (CDI_DOMINATORS);
6534 find_assert_locations ();
6535 if (!bitmap_empty_p (need_assert_for))
6537 process_assert_insertions ();
6538 update_ssa (TODO_update_ssa_no_phi);
6541 if (dump_file && (dump_flags & TDF_DETAILS))
6543 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6544 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6547 free (asserts_for);
6548 BITMAP_FREE (need_assert_for);
6551 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6552 and "struct" hacks. If VRP can determine that the
6553 array subscript is a constant, check if it is outside valid
6554 range. If the array subscript is a RANGE, warn if it is
6555 non-overlapping with valid range.
6556 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6558 static void
6559 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6561 value_range_t* vr = NULL;
6562 tree low_sub, up_sub;
6563 tree low_bound, up_bound, up_bound_p1;
6564 tree base;
6566 if (TREE_NO_WARNING (ref))
6567 return;
6569 low_sub = up_sub = TREE_OPERAND (ref, 1);
6570 up_bound = array_ref_up_bound (ref);
6572 /* Can not check flexible arrays. */
6573 if (!up_bound
6574 || TREE_CODE (up_bound) != INTEGER_CST)
6575 return;
6577 /* Accesses to trailing arrays via pointers may access storage
6578 beyond the types array bounds. */
6579 base = get_base_address (ref);
6580 if ((warn_array_bounds < 2)
6581 && base && TREE_CODE (base) == MEM_REF)
6583 tree cref, next = NULL_TREE;
6585 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6586 return;
6588 cref = TREE_OPERAND (ref, 0);
6589 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6590 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6591 next && TREE_CODE (next) != FIELD_DECL;
6592 next = DECL_CHAIN (next))
6595 /* If this is the last field in a struct type or a field in a
6596 union type do not warn. */
6597 if (!next)
6598 return;
6601 low_bound = array_ref_low_bound (ref);
6602 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6603 build_int_cst (TREE_TYPE (up_bound), 1));
6605 /* Empty array. */
6606 if (tree_int_cst_equal (low_bound, up_bound_p1))
6608 warning_at (location, OPT_Warray_bounds,
6609 "array subscript is above array bounds");
6610 TREE_NO_WARNING (ref) = 1;
6613 if (TREE_CODE (low_sub) == SSA_NAME)
6615 vr = get_value_range (low_sub);
6616 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6618 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6619 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6623 if (vr && vr->type == VR_ANTI_RANGE)
6625 if (TREE_CODE (up_sub) == INTEGER_CST
6626 && (ignore_off_by_one
6627 ? tree_int_cst_lt (up_bound, up_sub)
6628 : tree_int_cst_le (up_bound, up_sub))
6629 && TREE_CODE (low_sub) == INTEGER_CST
6630 && tree_int_cst_le (low_sub, low_bound))
6632 warning_at (location, OPT_Warray_bounds,
6633 "array subscript is outside array bounds");
6634 TREE_NO_WARNING (ref) = 1;
6637 else if (TREE_CODE (up_sub) == INTEGER_CST
6638 && (ignore_off_by_one
6639 ? !tree_int_cst_le (up_sub, up_bound_p1)
6640 : !tree_int_cst_le (up_sub, up_bound)))
6642 if (dump_file && (dump_flags & TDF_DETAILS))
6644 fprintf (dump_file, "Array bound warning for ");
6645 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6646 fprintf (dump_file, "\n");
6648 warning_at (location, OPT_Warray_bounds,
6649 "array subscript is above array bounds");
6650 TREE_NO_WARNING (ref) = 1;
6652 else if (TREE_CODE (low_sub) == INTEGER_CST
6653 && tree_int_cst_lt (low_sub, low_bound))
6655 if (dump_file && (dump_flags & TDF_DETAILS))
6657 fprintf (dump_file, "Array bound warning for ");
6658 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6659 fprintf (dump_file, "\n");
6661 warning_at (location, OPT_Warray_bounds,
6662 "array subscript is below array bounds");
6663 TREE_NO_WARNING (ref) = 1;
6667 /* Searches if the expr T, located at LOCATION computes
6668 address of an ARRAY_REF, and call check_array_ref on it. */
6670 static void
6671 search_for_addr_array (tree t, location_t location)
6673 /* Check each ARRAY_REFs in the reference chain. */
6676 if (TREE_CODE (t) == ARRAY_REF)
6677 check_array_ref (location, t, true /*ignore_off_by_one*/);
6679 t = TREE_OPERAND (t, 0);
6681 while (handled_component_p (t));
6683 if (TREE_CODE (t) == MEM_REF
6684 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6685 && !TREE_NO_WARNING (t))
6687 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6688 tree low_bound, up_bound, el_sz;
6689 offset_int idx;
6690 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6691 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6692 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6693 return;
6695 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6696 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6697 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6698 if (!low_bound
6699 || TREE_CODE (low_bound) != INTEGER_CST
6700 || !up_bound
6701 || TREE_CODE (up_bound) != INTEGER_CST
6702 || !el_sz
6703 || TREE_CODE (el_sz) != INTEGER_CST)
6704 return;
6706 idx = mem_ref_offset (t);
6707 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6708 if (wi::lts_p (idx, 0))
6710 if (dump_file && (dump_flags & TDF_DETAILS))
6712 fprintf (dump_file, "Array bound warning for ");
6713 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6714 fprintf (dump_file, "\n");
6716 warning_at (location, OPT_Warray_bounds,
6717 "array subscript is below array bounds");
6718 TREE_NO_WARNING (t) = 1;
6720 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6721 - wi::to_offset (low_bound) + 1)))
6723 if (dump_file && (dump_flags & TDF_DETAILS))
6725 fprintf (dump_file, "Array bound warning for ");
6726 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6727 fprintf (dump_file, "\n");
6729 warning_at (location, OPT_Warray_bounds,
6730 "array subscript is above array bounds");
6731 TREE_NO_WARNING (t) = 1;
6736 /* walk_tree() callback that checks if *TP is
6737 an ARRAY_REF inside an ADDR_EXPR (in which an array
6738 subscript one outside the valid range is allowed). Call
6739 check_array_ref for each ARRAY_REF found. The location is
6740 passed in DATA. */
6742 static tree
6743 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6745 tree t = *tp;
6746 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6747 location_t location;
6749 if (EXPR_HAS_LOCATION (t))
6750 location = EXPR_LOCATION (t);
6751 else
6753 location_t *locp = (location_t *) wi->info;
6754 location = *locp;
6757 *walk_subtree = TRUE;
6759 if (TREE_CODE (t) == ARRAY_REF)
6760 check_array_ref (location, t, false /*ignore_off_by_one*/);
6762 else if (TREE_CODE (t) == ADDR_EXPR)
6764 search_for_addr_array (t, location);
6765 *walk_subtree = FALSE;
6768 return NULL_TREE;
6771 /* Walk over all statements of all reachable BBs and call check_array_bounds
6772 on them. */
6774 static void
6775 check_all_array_refs (void)
6777 basic_block bb;
6778 gimple_stmt_iterator si;
6780 FOR_EACH_BB_FN (bb, cfun)
6782 edge_iterator ei;
6783 edge e;
6784 bool executable = false;
6786 /* Skip blocks that were found to be unreachable. */
6787 FOR_EACH_EDGE (e, ei, bb->preds)
6788 executable |= !!(e->flags & EDGE_EXECUTABLE);
6789 if (!executable)
6790 continue;
6792 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6794 gimple stmt = gsi_stmt (si);
6795 struct walk_stmt_info wi;
6796 if (!gimple_has_location (stmt)
6797 || is_gimple_debug (stmt))
6798 continue;
6800 memset (&wi, 0, sizeof (wi));
6801 wi.info = CONST_CAST (void *, (const void *)
6802 gimple_location_ptr (stmt));
6804 walk_gimple_op (gsi_stmt (si),
6805 check_array_bounds,
6806 &wi);
6811 /* Return true if all imm uses of VAR are either in STMT, or
6812 feed (optionally through a chain of single imm uses) GIMPLE_COND
6813 in basic block COND_BB. */
6815 static bool
6816 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6818 use_operand_p use_p, use2_p;
6819 imm_use_iterator iter;
6821 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6822 if (USE_STMT (use_p) != stmt)
6824 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6825 if (is_gimple_debug (use_stmt))
6826 continue;
6827 while (is_gimple_assign (use_stmt)
6828 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6829 && single_imm_use (gimple_assign_lhs (use_stmt),
6830 &use2_p, &use_stmt2))
6831 use_stmt = use_stmt2;
6832 if (gimple_code (use_stmt) != GIMPLE_COND
6833 || gimple_bb (use_stmt) != cond_bb)
6834 return false;
6836 return true;
6839 /* Handle
6840 _4 = x_3 & 31;
6841 if (_4 != 0)
6842 goto <bb 6>;
6843 else
6844 goto <bb 7>;
6845 <bb 6>:
6846 __builtin_unreachable ();
6847 <bb 7>:
6848 x_5 = ASSERT_EXPR <x_3, ...>;
6849 If x_3 has no other immediate uses (checked by caller),
6850 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6851 from the non-zero bitmask. */
6853 static void
6854 maybe_set_nonzero_bits (basic_block bb, tree var)
6856 edge e = single_pred_edge (bb);
6857 basic_block cond_bb = e->src;
6858 gimple stmt = last_stmt (cond_bb);
6859 tree cst;
6861 if (stmt == NULL
6862 || gimple_code (stmt) != GIMPLE_COND
6863 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6864 ? EQ_EXPR : NE_EXPR)
6865 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6866 || !integer_zerop (gimple_cond_rhs (stmt)))
6867 return;
6869 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6870 if (!is_gimple_assign (stmt)
6871 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6872 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6873 return;
6874 if (gimple_assign_rhs1 (stmt) != var)
6876 gimple stmt2;
6878 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6879 return;
6880 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6881 if (!gimple_assign_cast_p (stmt2)
6882 || gimple_assign_rhs1 (stmt2) != var
6883 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6884 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6885 != TYPE_PRECISION (TREE_TYPE (var))))
6886 return;
6888 cst = gimple_assign_rhs2 (stmt);
6889 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6892 /* Convert range assertion expressions into the implied copies and
6893 copy propagate away the copies. Doing the trivial copy propagation
6894 here avoids the need to run the full copy propagation pass after
6895 VRP.
6897 FIXME, this will eventually lead to copy propagation removing the
6898 names that had useful range information attached to them. For
6899 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6900 then N_i will have the range [3, +INF].
6902 However, by converting the assertion into the implied copy
6903 operation N_i = N_j, we will then copy-propagate N_j into the uses
6904 of N_i and lose the range information. We may want to hold on to
6905 ASSERT_EXPRs a little while longer as the ranges could be used in
6906 things like jump threading.
6908 The problem with keeping ASSERT_EXPRs around is that passes after
6909 VRP need to handle them appropriately.
6911 Another approach would be to make the range information a first
6912 class property of the SSA_NAME so that it can be queried from
6913 any pass. This is made somewhat more complex by the need for
6914 multiple ranges to be associated with one SSA_NAME. */
6916 static void
6917 remove_range_assertions (void)
6919 basic_block bb;
6920 gimple_stmt_iterator si;
6921 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6922 a basic block preceeded by GIMPLE_COND branching to it and
6923 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6924 int is_unreachable;
6926 /* Note that the BSI iterator bump happens at the bottom of the
6927 loop and no bump is necessary if we're removing the statement
6928 referenced by the current BSI. */
6929 FOR_EACH_BB_FN (bb, cfun)
6930 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6932 gimple stmt = gsi_stmt (si);
6933 gimple use_stmt;
6935 if (is_gimple_assign (stmt)
6936 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6938 tree lhs = gimple_assign_lhs (stmt);
6939 tree rhs = gimple_assign_rhs1 (stmt);
6940 tree var;
6941 tree cond = fold (ASSERT_EXPR_COND (rhs));
6942 use_operand_p use_p;
6943 imm_use_iterator iter;
6945 gcc_assert (cond != boolean_false_node);
6947 var = ASSERT_EXPR_VAR (rhs);
6948 gcc_assert (TREE_CODE (var) == SSA_NAME);
6950 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6951 && SSA_NAME_RANGE_INFO (lhs))
6953 if (is_unreachable == -1)
6955 is_unreachable = 0;
6956 if (single_pred_p (bb)
6957 && assert_unreachable_fallthru_edge_p
6958 (single_pred_edge (bb)))
6959 is_unreachable = 1;
6961 /* Handle
6962 if (x_7 >= 10 && x_7 < 20)
6963 __builtin_unreachable ();
6964 x_8 = ASSERT_EXPR <x_7, ...>;
6965 if the only uses of x_7 are in the ASSERT_EXPR and
6966 in the condition. In that case, we can copy the
6967 range info from x_8 computed in this pass also
6968 for x_7. */
6969 if (is_unreachable
6970 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6971 single_pred (bb)))
6973 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6974 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6975 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6976 maybe_set_nonzero_bits (bb, var);
6980 /* Propagate the RHS into every use of the LHS. */
6981 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6982 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6983 SET_USE (use_p, var);
6985 /* And finally, remove the copy, it is not needed. */
6986 gsi_remove (&si, true);
6987 release_defs (stmt);
6989 else
6991 if (!is_gimple_debug (gsi_stmt (si)))
6992 is_unreachable = 0;
6993 gsi_next (&si);
6999 /* Return true if STMT is interesting for VRP. */
7001 static bool
7002 stmt_interesting_for_vrp (gimple stmt)
7004 if (gimple_code (stmt) == GIMPLE_PHI)
7006 tree res = gimple_phi_result (stmt);
7007 return (!virtual_operand_p (res)
7008 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
7009 || POINTER_TYPE_P (TREE_TYPE (res))));
7011 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7013 tree lhs = gimple_get_lhs (stmt);
7015 /* In general, assignments with virtual operands are not useful
7016 for deriving ranges, with the obvious exception of calls to
7017 builtin functions. */
7018 if (lhs && TREE_CODE (lhs) == SSA_NAME
7019 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7020 || POINTER_TYPE_P (TREE_TYPE (lhs)))
7021 && (is_gimple_call (stmt)
7022 || !gimple_vuse (stmt)))
7023 return true;
7024 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7025 switch (gimple_call_internal_fn (stmt))
7027 case IFN_ADD_OVERFLOW:
7028 case IFN_SUB_OVERFLOW:
7029 case IFN_MUL_OVERFLOW:
7030 /* These internal calls return _Complex integer type,
7031 but are interesting to VRP nevertheless. */
7032 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7033 return true;
7034 break;
7035 default:
7036 break;
7039 else if (gimple_code (stmt) == GIMPLE_COND
7040 || gimple_code (stmt) == GIMPLE_SWITCH)
7041 return true;
7043 return false;
7047 /* Initialize local data structures for VRP. */
7049 static void
7050 vrp_initialize (void)
7052 basic_block bb;
7054 values_propagated = false;
7055 num_vr_values = num_ssa_names;
7056 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
7057 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
7059 FOR_EACH_BB_FN (bb, cfun)
7061 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7062 gsi_next (&si))
7064 gphi *phi = si.phi ();
7065 if (!stmt_interesting_for_vrp (phi))
7067 tree lhs = PHI_RESULT (phi);
7068 set_value_range_to_varying (get_value_range (lhs));
7069 prop_set_simulate_again (phi, false);
7071 else
7072 prop_set_simulate_again (phi, true);
7075 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7076 gsi_next (&si))
7078 gimple stmt = gsi_stmt (si);
7080 /* If the statement is a control insn, then we do not
7081 want to avoid simulating the statement once. Failure
7082 to do so means that those edges will never get added. */
7083 if (stmt_ends_bb_p (stmt))
7084 prop_set_simulate_again (stmt, true);
7085 else if (!stmt_interesting_for_vrp (stmt))
7087 ssa_op_iter i;
7088 tree def;
7089 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7090 set_value_range_to_varying (get_value_range (def));
7091 prop_set_simulate_again (stmt, false);
7093 else
7094 prop_set_simulate_again (stmt, true);
7099 /* Return the singleton value-range for NAME or NAME. */
7101 static inline tree
7102 vrp_valueize (tree name)
7104 if (TREE_CODE (name) == SSA_NAME)
7106 value_range_t *vr = get_value_range (name);
7107 if (vr->type == VR_RANGE
7108 && (vr->min == vr->max
7109 || operand_equal_p (vr->min, vr->max, 0)))
7110 return vr->min;
7112 return name;
7115 /* Return the singleton value-range for NAME if that is a constant
7116 but signal to not follow SSA edges. */
7118 static inline tree
7119 vrp_valueize_1 (tree name)
7121 if (TREE_CODE (name) == SSA_NAME)
7123 /* If the definition may be simulated again we cannot follow
7124 this SSA edge as the SSA propagator does not necessarily
7125 re-visit the use. */
7126 gimple def_stmt = SSA_NAME_DEF_STMT (name);
7127 if (!gimple_nop_p (def_stmt)
7128 && prop_simulate_again_p (def_stmt))
7129 return NULL_TREE;
7130 value_range_t *vr = get_value_range (name);
7131 if (range_int_cst_singleton_p (vr))
7132 return vr->min;
7134 return name;
7137 /* Visit assignment STMT. If it produces an interesting range, record
7138 the SSA name in *OUTPUT_P. */
7140 static enum ssa_prop_result
7141 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
7143 tree def, lhs;
7144 ssa_op_iter iter;
7145 enum gimple_code code = gimple_code (stmt);
7146 lhs = gimple_get_lhs (stmt);
7148 /* We only keep track of ranges in integral and pointer types. */
7149 if (TREE_CODE (lhs) == SSA_NAME
7150 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7151 /* It is valid to have NULL MIN/MAX values on a type. See
7152 build_range_type. */
7153 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7154 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7155 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7157 value_range_t new_vr = VR_INITIALIZER;
7159 /* Try folding the statement to a constant first. */
7160 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7161 vrp_valueize_1);
7162 if (tem && is_gimple_min_invariant (tem))
7163 set_value_range_to_value (&new_vr, tem, NULL);
7164 /* Then dispatch to value-range extracting functions. */
7165 else if (code == GIMPLE_CALL)
7166 extract_range_basic (&new_vr, stmt);
7167 else
7168 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7170 if (update_value_range (lhs, &new_vr))
7172 *output_p = lhs;
7174 if (dump_file && (dump_flags & TDF_DETAILS))
7176 fprintf (dump_file, "Found new range for ");
7177 print_generic_expr (dump_file, lhs, 0);
7178 fprintf (dump_file, ": ");
7179 dump_value_range (dump_file, &new_vr);
7180 fprintf (dump_file, "\n");
7183 if (new_vr.type == VR_VARYING)
7184 return SSA_PROP_VARYING;
7186 return SSA_PROP_INTERESTING;
7189 return SSA_PROP_NOT_INTERESTING;
7191 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7192 switch (gimple_call_internal_fn (stmt))
7194 case IFN_ADD_OVERFLOW:
7195 case IFN_SUB_OVERFLOW:
7196 case IFN_MUL_OVERFLOW:
7197 /* These internal calls return _Complex integer type,
7198 which VRP does not track, but the immediate uses
7199 thereof might be interesting. */
7200 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7202 imm_use_iterator iter;
7203 use_operand_p use_p;
7204 enum ssa_prop_result res = SSA_PROP_VARYING;
7206 set_value_range_to_varying (get_value_range (lhs));
7208 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7210 gimple use_stmt = USE_STMT (use_p);
7211 if (!is_gimple_assign (use_stmt))
7212 continue;
7213 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7214 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7215 continue;
7216 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7217 tree use_lhs = gimple_assign_lhs (use_stmt);
7218 if (TREE_CODE (rhs1) != rhs_code
7219 || TREE_OPERAND (rhs1, 0) != lhs
7220 || TREE_CODE (use_lhs) != SSA_NAME
7221 || !stmt_interesting_for_vrp (use_stmt)
7222 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7223 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7224 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7225 continue;
7227 /* If there is a change in the value range for any of the
7228 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7229 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7230 or IMAGPART_EXPR immediate uses, but none of them have
7231 a change in their value ranges, return
7232 SSA_PROP_NOT_INTERESTING. If there are no
7233 {REAL,IMAG}PART_EXPR uses at all,
7234 return SSA_PROP_VARYING. */
7235 value_range_t new_vr = VR_INITIALIZER;
7236 extract_range_basic (&new_vr, use_stmt);
7237 value_range_t *old_vr = get_value_range (use_lhs);
7238 if (old_vr->type != new_vr.type
7239 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7240 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7241 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7242 res = SSA_PROP_INTERESTING;
7243 else
7244 res = SSA_PROP_NOT_INTERESTING;
7245 BITMAP_FREE (new_vr.equiv);
7246 if (res == SSA_PROP_INTERESTING)
7248 *output_p = lhs;
7249 return res;
7253 return res;
7255 break;
7256 default:
7257 break;
7260 /* Every other statement produces no useful ranges. */
7261 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7262 set_value_range_to_varying (get_value_range (def));
7264 return SSA_PROP_VARYING;
7267 /* Helper that gets the value range of the SSA_NAME with version I
7268 or a symbolic range containing the SSA_NAME only if the value range
7269 is varying or undefined. */
7271 static inline value_range_t
7272 get_vr_for_comparison (int i)
7274 value_range_t vr = *get_value_range (ssa_name (i));
7276 /* If name N_i does not have a valid range, use N_i as its own
7277 range. This allows us to compare against names that may
7278 have N_i in their ranges. */
7279 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7281 vr.type = VR_RANGE;
7282 vr.min = ssa_name (i);
7283 vr.max = ssa_name (i);
7286 return vr;
7289 /* Compare all the value ranges for names equivalent to VAR with VAL
7290 using comparison code COMP. Return the same value returned by
7291 compare_range_with_value, including the setting of
7292 *STRICT_OVERFLOW_P. */
7294 static tree
7295 compare_name_with_value (enum tree_code comp, tree var, tree val,
7296 bool *strict_overflow_p)
7298 bitmap_iterator bi;
7299 unsigned i;
7300 bitmap e;
7301 tree retval, t;
7302 int used_strict_overflow;
7303 bool sop;
7304 value_range_t equiv_vr;
7306 /* Get the set of equivalences for VAR. */
7307 e = get_value_range (var)->equiv;
7309 /* Start at -1. Set it to 0 if we do a comparison without relying
7310 on overflow, or 1 if all comparisons rely on overflow. */
7311 used_strict_overflow = -1;
7313 /* Compare vars' value range with val. */
7314 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7315 sop = false;
7316 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7317 if (retval)
7318 used_strict_overflow = sop ? 1 : 0;
7320 /* If the equiv set is empty we have done all work we need to do. */
7321 if (e == NULL)
7323 if (retval
7324 && used_strict_overflow > 0)
7325 *strict_overflow_p = true;
7326 return retval;
7329 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7331 equiv_vr = get_vr_for_comparison (i);
7332 sop = false;
7333 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7334 if (t)
7336 /* If we get different answers from different members
7337 of the equivalence set this check must be in a dead
7338 code region. Folding it to a trap representation
7339 would be correct here. For now just return don't-know. */
7340 if (retval != NULL
7341 && t != retval)
7343 retval = NULL_TREE;
7344 break;
7346 retval = t;
7348 if (!sop)
7349 used_strict_overflow = 0;
7350 else if (used_strict_overflow < 0)
7351 used_strict_overflow = 1;
7355 if (retval
7356 && used_strict_overflow > 0)
7357 *strict_overflow_p = true;
7359 return retval;
7363 /* Given a comparison code COMP and names N1 and N2, compare all the
7364 ranges equivalent to N1 against all the ranges equivalent to N2
7365 to determine the value of N1 COMP N2. Return the same value
7366 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7367 whether we relied on an overflow infinity in the comparison. */
7370 static tree
7371 compare_names (enum tree_code comp, tree n1, tree n2,
7372 bool *strict_overflow_p)
7374 tree t, retval;
7375 bitmap e1, e2;
7376 bitmap_iterator bi1, bi2;
7377 unsigned i1, i2;
7378 int used_strict_overflow;
7379 static bitmap_obstack *s_obstack = NULL;
7380 static bitmap s_e1 = NULL, s_e2 = NULL;
7382 /* Compare the ranges of every name equivalent to N1 against the
7383 ranges of every name equivalent to N2. */
7384 e1 = get_value_range (n1)->equiv;
7385 e2 = get_value_range (n2)->equiv;
7387 /* Use the fake bitmaps if e1 or e2 are not available. */
7388 if (s_obstack == NULL)
7390 s_obstack = XNEW (bitmap_obstack);
7391 bitmap_obstack_initialize (s_obstack);
7392 s_e1 = BITMAP_ALLOC (s_obstack);
7393 s_e2 = BITMAP_ALLOC (s_obstack);
7395 if (e1 == NULL)
7396 e1 = s_e1;
7397 if (e2 == NULL)
7398 e2 = s_e2;
7400 /* Add N1 and N2 to their own set of equivalences to avoid
7401 duplicating the body of the loop just to check N1 and N2
7402 ranges. */
7403 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7404 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7406 /* If the equivalence sets have a common intersection, then the two
7407 names can be compared without checking their ranges. */
7408 if (bitmap_intersect_p (e1, e2))
7410 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7411 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7413 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7414 ? boolean_true_node
7415 : boolean_false_node;
7418 /* Start at -1. Set it to 0 if we do a comparison without relying
7419 on overflow, or 1 if all comparisons rely on overflow. */
7420 used_strict_overflow = -1;
7422 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7423 N2 to their own set of equivalences to avoid duplicating the body
7424 of the loop just to check N1 and N2 ranges. */
7425 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7427 value_range_t vr1 = get_vr_for_comparison (i1);
7429 t = retval = NULL_TREE;
7430 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7432 bool sop = false;
7434 value_range_t vr2 = get_vr_for_comparison (i2);
7436 t = compare_ranges (comp, &vr1, &vr2, &sop);
7437 if (t)
7439 /* If we get different answers from different members
7440 of the equivalence set this check must be in a dead
7441 code region. Folding it to a trap representation
7442 would be correct here. For now just return don't-know. */
7443 if (retval != NULL
7444 && t != retval)
7446 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7447 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7448 return NULL_TREE;
7450 retval = t;
7452 if (!sop)
7453 used_strict_overflow = 0;
7454 else if (used_strict_overflow < 0)
7455 used_strict_overflow = 1;
7459 if (retval)
7461 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7462 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7463 if (used_strict_overflow > 0)
7464 *strict_overflow_p = true;
7465 return retval;
7469 /* None of the equivalent ranges are useful in computing this
7470 comparison. */
7471 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7472 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7473 return NULL_TREE;
7476 /* Helper function for vrp_evaluate_conditional_warnv. */
7478 static tree
7479 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7480 tree op0, tree op1,
7481 bool * strict_overflow_p)
7483 value_range_t *vr0, *vr1;
7485 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7486 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7488 tree res = NULL_TREE;
7489 if (vr0 && vr1)
7490 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7491 if (!res && vr0)
7492 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7493 if (!res && vr1)
7494 res = (compare_range_with_value
7495 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7496 return res;
7499 /* Helper function for vrp_evaluate_conditional_warnv. */
7501 static tree
7502 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7503 tree op1, bool use_equiv_p,
7504 bool *strict_overflow_p, bool *only_ranges)
7506 tree ret;
7507 if (only_ranges)
7508 *only_ranges = true;
7510 /* We only deal with integral and pointer types. */
7511 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7512 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7513 return NULL_TREE;
7515 if (use_equiv_p)
7517 if (only_ranges
7518 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7519 (code, op0, op1, strict_overflow_p)))
7520 return ret;
7521 *only_ranges = false;
7522 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7523 return compare_names (code, op0, op1, strict_overflow_p);
7524 else if (TREE_CODE (op0) == SSA_NAME)
7525 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7526 else if (TREE_CODE (op1) == SSA_NAME)
7527 return (compare_name_with_value
7528 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7530 else
7531 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7532 strict_overflow_p);
7533 return NULL_TREE;
7536 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7537 information. Return NULL if the conditional can not be evaluated.
7538 The ranges of all the names equivalent with the operands in COND
7539 will be used when trying to compute the value. If the result is
7540 based on undefined signed overflow, issue a warning if
7541 appropriate. */
7543 static tree
7544 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7546 bool sop;
7547 tree ret;
7548 bool only_ranges;
7550 /* Some passes and foldings leak constants with overflow flag set
7551 into the IL. Avoid doing wrong things with these and bail out. */
7552 if ((TREE_CODE (op0) == INTEGER_CST
7553 && TREE_OVERFLOW (op0))
7554 || (TREE_CODE (op1) == INTEGER_CST
7555 && TREE_OVERFLOW (op1)))
7556 return NULL_TREE;
7558 sop = false;
7559 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7560 &only_ranges);
7562 if (ret && sop)
7564 enum warn_strict_overflow_code wc;
7565 const char* warnmsg;
7567 if (is_gimple_min_invariant (ret))
7569 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7570 warnmsg = G_("assuming signed overflow does not occur when "
7571 "simplifying conditional to constant");
7573 else
7575 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7576 warnmsg = G_("assuming signed overflow does not occur when "
7577 "simplifying conditional");
7580 if (issue_strict_overflow_warning (wc))
7582 location_t location;
7584 if (!gimple_has_location (stmt))
7585 location = input_location;
7586 else
7587 location = gimple_location (stmt);
7588 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7592 if (warn_type_limits
7593 && ret && only_ranges
7594 && TREE_CODE_CLASS (code) == tcc_comparison
7595 && TREE_CODE (op0) == SSA_NAME)
7597 /* If the comparison is being folded and the operand on the LHS
7598 is being compared against a constant value that is outside of
7599 the natural range of OP0's type, then the predicate will
7600 always fold regardless of the value of OP0. If -Wtype-limits
7601 was specified, emit a warning. */
7602 tree type = TREE_TYPE (op0);
7603 value_range_t *vr0 = get_value_range (op0);
7605 if (vr0->type == VR_RANGE
7606 && INTEGRAL_TYPE_P (type)
7607 && vrp_val_is_min (vr0->min)
7608 && vrp_val_is_max (vr0->max)
7609 && is_gimple_min_invariant (op1))
7611 location_t location;
7613 if (!gimple_has_location (stmt))
7614 location = input_location;
7615 else
7616 location = gimple_location (stmt);
7618 warning_at (location, OPT_Wtype_limits,
7619 integer_zerop (ret)
7620 ? G_("comparison always false "
7621 "due to limited range of data type")
7622 : G_("comparison always true "
7623 "due to limited range of data type"));
7627 return ret;
7631 /* Visit conditional statement STMT. If we can determine which edge
7632 will be taken out of STMT's basic block, record it in
7633 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7634 SSA_PROP_VARYING. */
7636 static enum ssa_prop_result
7637 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7639 tree val;
7640 bool sop;
7642 *taken_edge_p = NULL;
7644 if (dump_file && (dump_flags & TDF_DETAILS))
7646 tree use;
7647 ssa_op_iter i;
7649 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7650 print_gimple_stmt (dump_file, stmt, 0, 0);
7651 fprintf (dump_file, "\nWith known ranges\n");
7653 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7655 fprintf (dump_file, "\t");
7656 print_generic_expr (dump_file, use, 0);
7657 fprintf (dump_file, ": ");
7658 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7661 fprintf (dump_file, "\n");
7664 /* Compute the value of the predicate COND by checking the known
7665 ranges of each of its operands.
7667 Note that we cannot evaluate all the equivalent ranges here
7668 because those ranges may not yet be final and with the current
7669 propagation strategy, we cannot determine when the value ranges
7670 of the names in the equivalence set have changed.
7672 For instance, given the following code fragment
7674 i_5 = PHI <8, i_13>
7676 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7677 if (i_14 == 1)
7680 Assume that on the first visit to i_14, i_5 has the temporary
7681 range [8, 8] because the second argument to the PHI function is
7682 not yet executable. We derive the range ~[0, 0] for i_14 and the
7683 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7684 the first time, since i_14 is equivalent to the range [8, 8], we
7685 determine that the predicate is always false.
7687 On the next round of propagation, i_13 is determined to be
7688 VARYING, which causes i_5 to drop down to VARYING. So, another
7689 visit to i_14 is scheduled. In this second visit, we compute the
7690 exact same range and equivalence set for i_14, namely ~[0, 0] and
7691 { i_5 }. But we did not have the previous range for i_5
7692 registered, so vrp_visit_assignment thinks that the range for
7693 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7694 is not visited again, which stops propagation from visiting
7695 statements in the THEN clause of that if().
7697 To properly fix this we would need to keep the previous range
7698 value for the names in the equivalence set. This way we would've
7699 discovered that from one visit to the other i_5 changed from
7700 range [8, 8] to VR_VARYING.
7702 However, fixing this apparent limitation may not be worth the
7703 additional checking. Testing on several code bases (GCC, DLV,
7704 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7705 4 more predicates folded in SPEC. */
7706 sop = false;
7708 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7709 gimple_cond_lhs (stmt),
7710 gimple_cond_rhs (stmt),
7711 false, &sop, NULL);
7712 if (val)
7714 if (!sop)
7715 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7716 else
7718 if (dump_file && (dump_flags & TDF_DETAILS))
7719 fprintf (dump_file,
7720 "\nIgnoring predicate evaluation because "
7721 "it assumes that signed overflow is undefined");
7722 val = NULL_TREE;
7726 if (dump_file && (dump_flags & TDF_DETAILS))
7728 fprintf (dump_file, "\nPredicate evaluates to: ");
7729 if (val == NULL_TREE)
7730 fprintf (dump_file, "DON'T KNOW\n");
7731 else
7732 print_generic_stmt (dump_file, val, 0);
7735 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7738 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7739 that includes the value VAL. The search is restricted to the range
7740 [START_IDX, n - 1] where n is the size of VEC.
7742 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7743 returned.
7745 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7746 it is placed in IDX and false is returned.
7748 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7749 returned. */
7751 static bool
7752 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7754 size_t n = gimple_switch_num_labels (stmt);
7755 size_t low, high;
7757 /* Find case label for minimum of the value range or the next one.
7758 At each iteration we are searching in [low, high - 1]. */
7760 for (low = start_idx, high = n; high != low; )
7762 tree t;
7763 int cmp;
7764 /* Note that i != high, so we never ask for n. */
7765 size_t i = (high + low) / 2;
7766 t = gimple_switch_label (stmt, i);
7768 /* Cache the result of comparing CASE_LOW and val. */
7769 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7771 if (cmp == 0)
7773 /* Ranges cannot be empty. */
7774 *idx = i;
7775 return true;
7777 else if (cmp > 0)
7778 high = i;
7779 else
7781 low = i + 1;
7782 if (CASE_HIGH (t) != NULL
7783 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7785 *idx = i;
7786 return true;
7791 *idx = high;
7792 return false;
7795 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7796 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7797 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7798 then MAX_IDX < MIN_IDX.
7799 Returns true if the default label is not needed. */
7801 static bool
7802 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7803 size_t *max_idx)
7805 size_t i, j;
7806 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7807 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7809 if (i == j
7810 && min_take_default
7811 && max_take_default)
7813 /* Only the default case label reached.
7814 Return an empty range. */
7815 *min_idx = 1;
7816 *max_idx = 0;
7817 return false;
7819 else
7821 bool take_default = min_take_default || max_take_default;
7822 tree low, high;
7823 size_t k;
7825 if (max_take_default)
7826 j--;
7828 /* If the case label range is continuous, we do not need
7829 the default case label. Verify that. */
7830 high = CASE_LOW (gimple_switch_label (stmt, i));
7831 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7832 high = CASE_HIGH (gimple_switch_label (stmt, i));
7833 for (k = i + 1; k <= j; ++k)
7835 low = CASE_LOW (gimple_switch_label (stmt, k));
7836 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7838 take_default = true;
7839 break;
7841 high = low;
7842 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7843 high = CASE_HIGH (gimple_switch_label (stmt, k));
7846 *min_idx = i;
7847 *max_idx = j;
7848 return !take_default;
7852 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7853 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7854 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7855 Returns true if the default label is not needed. */
7857 static bool
7858 find_case_label_ranges (gswitch *stmt, value_range_t *vr, size_t *min_idx1,
7859 size_t *max_idx1, size_t *min_idx2,
7860 size_t *max_idx2)
7862 size_t i, j, k, l;
7863 unsigned int n = gimple_switch_num_labels (stmt);
7864 bool take_default;
7865 tree case_low, case_high;
7866 tree min = vr->min, max = vr->max;
7868 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7870 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7872 /* Set second range to emtpy. */
7873 *min_idx2 = 1;
7874 *max_idx2 = 0;
7876 if (vr->type == VR_RANGE)
7878 *min_idx1 = i;
7879 *max_idx1 = j;
7880 return !take_default;
7883 /* Set first range to all case labels. */
7884 *min_idx1 = 1;
7885 *max_idx1 = n - 1;
7887 if (i > j)
7888 return false;
7890 /* Make sure all the values of case labels [i , j] are contained in
7891 range [MIN, MAX]. */
7892 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7893 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7894 if (tree_int_cst_compare (case_low, min) < 0)
7895 i += 1;
7896 if (case_high != NULL_TREE
7897 && tree_int_cst_compare (max, case_high) < 0)
7898 j -= 1;
7900 if (i > j)
7901 return false;
7903 /* If the range spans case labels [i, j], the corresponding anti-range spans
7904 the labels [1, i - 1] and [j + 1, n - 1]. */
7905 k = j + 1;
7906 l = n - 1;
7907 if (k > l)
7909 k = 1;
7910 l = 0;
7913 j = i - 1;
7914 i = 1;
7915 if (i > j)
7917 i = k;
7918 j = l;
7919 k = 1;
7920 l = 0;
7923 *min_idx1 = i;
7924 *max_idx1 = j;
7925 *min_idx2 = k;
7926 *max_idx2 = l;
7927 return false;
7930 /* Visit switch statement STMT. If we can determine which edge
7931 will be taken out of STMT's basic block, record it in
7932 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7933 SSA_PROP_VARYING. */
7935 static enum ssa_prop_result
7936 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7938 tree op, val;
7939 value_range_t *vr;
7940 size_t i = 0, j = 0, k, l;
7941 bool take_default;
7943 *taken_edge_p = NULL;
7944 op = gimple_switch_index (stmt);
7945 if (TREE_CODE (op) != SSA_NAME)
7946 return SSA_PROP_VARYING;
7948 vr = get_value_range (op);
7949 if (dump_file && (dump_flags & TDF_DETAILS))
7951 fprintf (dump_file, "\nVisiting switch expression with operand ");
7952 print_generic_expr (dump_file, op, 0);
7953 fprintf (dump_file, " with known range ");
7954 dump_value_range (dump_file, vr);
7955 fprintf (dump_file, "\n");
7958 if ((vr->type != VR_RANGE
7959 && vr->type != VR_ANTI_RANGE)
7960 || symbolic_range_p (vr))
7961 return SSA_PROP_VARYING;
7963 /* Find the single edge that is taken from the switch expression. */
7964 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7966 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7967 label */
7968 if (j < i)
7970 gcc_assert (take_default);
7971 val = gimple_switch_default_label (stmt);
7973 else
7975 /* Check if labels with index i to j and maybe the default label
7976 are all reaching the same label. */
7978 val = gimple_switch_label (stmt, i);
7979 if (take_default
7980 && CASE_LABEL (gimple_switch_default_label (stmt))
7981 != CASE_LABEL (val))
7983 if (dump_file && (dump_flags & TDF_DETAILS))
7984 fprintf (dump_file, " not a single destination for this "
7985 "range\n");
7986 return SSA_PROP_VARYING;
7988 for (++i; i <= j; ++i)
7990 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7992 if (dump_file && (dump_flags & TDF_DETAILS))
7993 fprintf (dump_file, " not a single destination for this "
7994 "range\n");
7995 return SSA_PROP_VARYING;
7998 for (; k <= l; ++k)
8000 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
8002 if (dump_file && (dump_flags & TDF_DETAILS))
8003 fprintf (dump_file, " not a single destination for this "
8004 "range\n");
8005 return SSA_PROP_VARYING;
8010 *taken_edge_p = find_edge (gimple_bb (stmt),
8011 label_to_block (CASE_LABEL (val)));
8013 if (dump_file && (dump_flags & TDF_DETAILS))
8015 fprintf (dump_file, " will take edge to ");
8016 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
8019 return SSA_PROP_INTERESTING;
8023 /* Evaluate statement STMT. If the statement produces a useful range,
8024 return SSA_PROP_INTERESTING and record the SSA name with the
8025 interesting range into *OUTPUT_P.
8027 If STMT is a conditional branch and we can determine its truth
8028 value, the taken edge is recorded in *TAKEN_EDGE_P.
8030 If STMT produces a varying value, return SSA_PROP_VARYING. */
8032 static enum ssa_prop_result
8033 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
8035 tree def;
8036 ssa_op_iter iter;
8038 if (dump_file && (dump_flags & TDF_DETAILS))
8040 fprintf (dump_file, "\nVisiting statement:\n");
8041 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
8044 if (!stmt_interesting_for_vrp (stmt))
8045 gcc_assert (stmt_ends_bb_p (stmt));
8046 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
8047 return vrp_visit_assignment_or_call (stmt, output_p);
8048 else if (gimple_code (stmt) == GIMPLE_COND)
8049 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
8050 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8051 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
8053 /* All other statements produce nothing of interest for VRP, so mark
8054 their outputs varying and prevent further simulation. */
8055 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
8056 set_value_range_to_varying (get_value_range (def));
8058 return SSA_PROP_VARYING;
8061 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8062 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8063 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8064 possible such range. The resulting range is not canonicalized. */
8066 static void
8067 union_ranges (enum value_range_type *vr0type,
8068 tree *vr0min, tree *vr0max,
8069 enum value_range_type vr1type,
8070 tree vr1min, tree vr1max)
8072 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8073 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8075 /* [] is vr0, () is vr1 in the following classification comments. */
8076 if (mineq && maxeq)
8078 /* [( )] */
8079 if (*vr0type == vr1type)
8080 /* Nothing to do for equal ranges. */
8082 else if ((*vr0type == VR_RANGE
8083 && vr1type == VR_ANTI_RANGE)
8084 || (*vr0type == VR_ANTI_RANGE
8085 && vr1type == VR_RANGE))
8087 /* For anti-range with range union the result is varying. */
8088 goto give_up;
8090 else
8091 gcc_unreachable ();
8093 else if (operand_less_p (*vr0max, vr1min) == 1
8094 || operand_less_p (vr1max, *vr0min) == 1)
8096 /* [ ] ( ) or ( ) [ ]
8097 If the ranges have an empty intersection, result of the union
8098 operation is the anti-range or if both are anti-ranges
8099 it covers all. */
8100 if (*vr0type == VR_ANTI_RANGE
8101 && vr1type == VR_ANTI_RANGE)
8102 goto give_up;
8103 else if (*vr0type == VR_ANTI_RANGE
8104 && vr1type == VR_RANGE)
8106 else if (*vr0type == VR_RANGE
8107 && vr1type == VR_ANTI_RANGE)
8109 *vr0type = vr1type;
8110 *vr0min = vr1min;
8111 *vr0max = vr1max;
8113 else if (*vr0type == VR_RANGE
8114 && vr1type == VR_RANGE)
8116 /* The result is the convex hull of both ranges. */
8117 if (operand_less_p (*vr0max, vr1min) == 1)
8119 /* If the result can be an anti-range, create one. */
8120 if (TREE_CODE (*vr0max) == INTEGER_CST
8121 && TREE_CODE (vr1min) == INTEGER_CST
8122 && vrp_val_is_min (*vr0min)
8123 && vrp_val_is_max (vr1max))
8125 tree min = int_const_binop (PLUS_EXPR,
8126 *vr0max,
8127 build_int_cst (TREE_TYPE (*vr0max), 1));
8128 tree max = int_const_binop (MINUS_EXPR,
8129 vr1min,
8130 build_int_cst (TREE_TYPE (vr1min), 1));
8131 if (!operand_less_p (max, min))
8133 *vr0type = VR_ANTI_RANGE;
8134 *vr0min = min;
8135 *vr0max = max;
8137 else
8138 *vr0max = vr1max;
8140 else
8141 *vr0max = vr1max;
8143 else
8145 /* If the result can be an anti-range, create one. */
8146 if (TREE_CODE (vr1max) == INTEGER_CST
8147 && TREE_CODE (*vr0min) == INTEGER_CST
8148 && vrp_val_is_min (vr1min)
8149 && vrp_val_is_max (*vr0max))
8151 tree min = int_const_binop (PLUS_EXPR,
8152 vr1max,
8153 build_int_cst (TREE_TYPE (vr1max), 1));
8154 tree max = int_const_binop (MINUS_EXPR,
8155 *vr0min,
8156 build_int_cst (TREE_TYPE (*vr0min), 1));
8157 if (!operand_less_p (max, min))
8159 *vr0type = VR_ANTI_RANGE;
8160 *vr0min = min;
8161 *vr0max = max;
8163 else
8164 *vr0min = vr1min;
8166 else
8167 *vr0min = vr1min;
8170 else
8171 gcc_unreachable ();
8173 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8174 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8176 /* [ ( ) ] or [( ) ] or [ ( )] */
8177 if (*vr0type == VR_RANGE
8178 && vr1type == VR_RANGE)
8180 else if (*vr0type == VR_ANTI_RANGE
8181 && vr1type == VR_ANTI_RANGE)
8183 *vr0type = vr1type;
8184 *vr0min = vr1min;
8185 *vr0max = vr1max;
8187 else if (*vr0type == VR_ANTI_RANGE
8188 && vr1type == VR_RANGE)
8190 /* Arbitrarily choose the right or left gap. */
8191 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8192 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8193 build_int_cst (TREE_TYPE (vr1min), 1));
8194 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8195 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8196 build_int_cst (TREE_TYPE (vr1max), 1));
8197 else
8198 goto give_up;
8200 else if (*vr0type == VR_RANGE
8201 && vr1type == VR_ANTI_RANGE)
8202 /* The result covers everything. */
8203 goto give_up;
8204 else
8205 gcc_unreachable ();
8207 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8208 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8210 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8211 if (*vr0type == VR_RANGE
8212 && vr1type == VR_RANGE)
8214 *vr0type = vr1type;
8215 *vr0min = vr1min;
8216 *vr0max = vr1max;
8218 else if (*vr0type == VR_ANTI_RANGE
8219 && vr1type == VR_ANTI_RANGE)
8221 else if (*vr0type == VR_RANGE
8222 && vr1type == VR_ANTI_RANGE)
8224 *vr0type = VR_ANTI_RANGE;
8225 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8227 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8228 build_int_cst (TREE_TYPE (*vr0min), 1));
8229 *vr0min = vr1min;
8231 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8233 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8234 build_int_cst (TREE_TYPE (*vr0max), 1));
8235 *vr0max = vr1max;
8237 else
8238 goto give_up;
8240 else if (*vr0type == VR_ANTI_RANGE
8241 && vr1type == VR_RANGE)
8242 /* The result covers everything. */
8243 goto give_up;
8244 else
8245 gcc_unreachable ();
8247 else if ((operand_less_p (vr1min, *vr0max) == 1
8248 || operand_equal_p (vr1min, *vr0max, 0))
8249 && operand_less_p (*vr0min, vr1min) == 1
8250 && operand_less_p (*vr0max, vr1max) == 1)
8252 /* [ ( ] ) or [ ]( ) */
8253 if (*vr0type == VR_RANGE
8254 && vr1type == VR_RANGE)
8255 *vr0max = vr1max;
8256 else if (*vr0type == VR_ANTI_RANGE
8257 && vr1type == VR_ANTI_RANGE)
8258 *vr0min = vr1min;
8259 else if (*vr0type == VR_ANTI_RANGE
8260 && vr1type == VR_RANGE)
8262 if (TREE_CODE (vr1min) == INTEGER_CST)
8263 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8264 build_int_cst (TREE_TYPE (vr1min), 1));
8265 else
8266 goto give_up;
8268 else if (*vr0type == VR_RANGE
8269 && vr1type == VR_ANTI_RANGE)
8271 if (TREE_CODE (*vr0max) == INTEGER_CST)
8273 *vr0type = vr1type;
8274 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8275 build_int_cst (TREE_TYPE (*vr0max), 1));
8276 *vr0max = vr1max;
8278 else
8279 goto give_up;
8281 else
8282 gcc_unreachable ();
8284 else if ((operand_less_p (*vr0min, vr1max) == 1
8285 || operand_equal_p (*vr0min, vr1max, 0))
8286 && operand_less_p (vr1min, *vr0min) == 1
8287 && operand_less_p (vr1max, *vr0max) == 1)
8289 /* ( [ ) ] or ( )[ ] */
8290 if (*vr0type == VR_RANGE
8291 && vr1type == VR_RANGE)
8292 *vr0min = vr1min;
8293 else if (*vr0type == VR_ANTI_RANGE
8294 && vr1type == VR_ANTI_RANGE)
8295 *vr0max = vr1max;
8296 else if (*vr0type == VR_ANTI_RANGE
8297 && vr1type == VR_RANGE)
8299 if (TREE_CODE (vr1max) == INTEGER_CST)
8300 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8301 build_int_cst (TREE_TYPE (vr1max), 1));
8302 else
8303 goto give_up;
8305 else if (*vr0type == VR_RANGE
8306 && vr1type == VR_ANTI_RANGE)
8308 if (TREE_CODE (*vr0min) == INTEGER_CST)
8310 *vr0type = vr1type;
8311 *vr0min = vr1min;
8312 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8313 build_int_cst (TREE_TYPE (*vr0min), 1));
8315 else
8316 goto give_up;
8318 else
8319 gcc_unreachable ();
8321 else
8322 goto give_up;
8324 return;
8326 give_up:
8327 *vr0type = VR_VARYING;
8328 *vr0min = NULL_TREE;
8329 *vr0max = NULL_TREE;
8332 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8333 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8334 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8335 possible such range. The resulting range is not canonicalized. */
8337 static void
8338 intersect_ranges (enum value_range_type *vr0type,
8339 tree *vr0min, tree *vr0max,
8340 enum value_range_type vr1type,
8341 tree vr1min, tree vr1max)
8343 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8344 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8346 /* [] is vr0, () is vr1 in the following classification comments. */
8347 if (mineq && maxeq)
8349 /* [( )] */
8350 if (*vr0type == vr1type)
8351 /* Nothing to do for equal ranges. */
8353 else if ((*vr0type == VR_RANGE
8354 && vr1type == VR_ANTI_RANGE)
8355 || (*vr0type == VR_ANTI_RANGE
8356 && vr1type == VR_RANGE))
8358 /* For anti-range with range intersection the result is empty. */
8359 *vr0type = VR_UNDEFINED;
8360 *vr0min = NULL_TREE;
8361 *vr0max = NULL_TREE;
8363 else
8364 gcc_unreachable ();
8366 else if (operand_less_p (*vr0max, vr1min) == 1
8367 || operand_less_p (vr1max, *vr0min) == 1)
8369 /* [ ] ( ) or ( ) [ ]
8370 If the ranges have an empty intersection, the result of the
8371 intersect operation is the range for intersecting an
8372 anti-range with a range or empty when intersecting two ranges. */
8373 if (*vr0type == VR_RANGE
8374 && vr1type == VR_ANTI_RANGE)
8376 else if (*vr0type == VR_ANTI_RANGE
8377 && vr1type == VR_RANGE)
8379 *vr0type = vr1type;
8380 *vr0min = vr1min;
8381 *vr0max = vr1max;
8383 else if (*vr0type == VR_RANGE
8384 && vr1type == VR_RANGE)
8386 *vr0type = VR_UNDEFINED;
8387 *vr0min = NULL_TREE;
8388 *vr0max = NULL_TREE;
8390 else if (*vr0type == VR_ANTI_RANGE
8391 && vr1type == VR_ANTI_RANGE)
8393 /* If the anti-ranges are adjacent to each other merge them. */
8394 if (TREE_CODE (*vr0max) == INTEGER_CST
8395 && TREE_CODE (vr1min) == INTEGER_CST
8396 && operand_less_p (*vr0max, vr1min) == 1
8397 && integer_onep (int_const_binop (MINUS_EXPR,
8398 vr1min, *vr0max)))
8399 *vr0max = vr1max;
8400 else if (TREE_CODE (vr1max) == INTEGER_CST
8401 && TREE_CODE (*vr0min) == INTEGER_CST
8402 && operand_less_p (vr1max, *vr0min) == 1
8403 && integer_onep (int_const_binop (MINUS_EXPR,
8404 *vr0min, vr1max)))
8405 *vr0min = vr1min;
8406 /* Else arbitrarily take VR0. */
8409 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8410 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8412 /* [ ( ) ] or [( ) ] or [ ( )] */
8413 if (*vr0type == VR_RANGE
8414 && vr1type == VR_RANGE)
8416 /* If both are ranges the result is the inner one. */
8417 *vr0type = vr1type;
8418 *vr0min = vr1min;
8419 *vr0max = vr1max;
8421 else if (*vr0type == VR_RANGE
8422 && vr1type == VR_ANTI_RANGE)
8424 /* Choose the right gap if the left one is empty. */
8425 if (mineq)
8427 if (TREE_CODE (vr1max) == INTEGER_CST)
8428 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8429 build_int_cst (TREE_TYPE (vr1max), 1));
8430 else
8431 *vr0min = vr1max;
8433 /* Choose the left gap if the right one is empty. */
8434 else if (maxeq)
8436 if (TREE_CODE (vr1min) == INTEGER_CST)
8437 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8438 build_int_cst (TREE_TYPE (vr1min), 1));
8439 else
8440 *vr0max = vr1min;
8442 /* Choose the anti-range if the range is effectively varying. */
8443 else if (vrp_val_is_min (*vr0min)
8444 && vrp_val_is_max (*vr0max))
8446 *vr0type = vr1type;
8447 *vr0min = vr1min;
8448 *vr0max = vr1max;
8450 /* Else choose the range. */
8452 else if (*vr0type == VR_ANTI_RANGE
8453 && vr1type == VR_ANTI_RANGE)
8454 /* If both are anti-ranges the result is the outer one. */
8456 else if (*vr0type == VR_ANTI_RANGE
8457 && vr1type == VR_RANGE)
8459 /* The intersection is empty. */
8460 *vr0type = VR_UNDEFINED;
8461 *vr0min = NULL_TREE;
8462 *vr0max = NULL_TREE;
8464 else
8465 gcc_unreachable ();
8467 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8468 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8470 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8471 if (*vr0type == VR_RANGE
8472 && vr1type == VR_RANGE)
8473 /* Choose the inner range. */
8475 else if (*vr0type == VR_ANTI_RANGE
8476 && vr1type == VR_RANGE)
8478 /* Choose the right gap if the left is empty. */
8479 if (mineq)
8481 *vr0type = VR_RANGE;
8482 if (TREE_CODE (*vr0max) == INTEGER_CST)
8483 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8484 build_int_cst (TREE_TYPE (*vr0max), 1));
8485 else
8486 *vr0min = *vr0max;
8487 *vr0max = vr1max;
8489 /* Choose the left gap if the right is empty. */
8490 else if (maxeq)
8492 *vr0type = VR_RANGE;
8493 if (TREE_CODE (*vr0min) == INTEGER_CST)
8494 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8495 build_int_cst (TREE_TYPE (*vr0min), 1));
8496 else
8497 *vr0max = *vr0min;
8498 *vr0min = vr1min;
8500 /* Choose the anti-range if the range is effectively varying. */
8501 else if (vrp_val_is_min (vr1min)
8502 && vrp_val_is_max (vr1max))
8504 /* Else choose the range. */
8505 else
8507 *vr0type = vr1type;
8508 *vr0min = vr1min;
8509 *vr0max = vr1max;
8512 else if (*vr0type == VR_ANTI_RANGE
8513 && vr1type == VR_ANTI_RANGE)
8515 /* If both are anti-ranges the result is the outer one. */
8516 *vr0type = vr1type;
8517 *vr0min = vr1min;
8518 *vr0max = vr1max;
8520 else if (vr1type == VR_ANTI_RANGE
8521 && *vr0type == VR_RANGE)
8523 /* The intersection is empty. */
8524 *vr0type = VR_UNDEFINED;
8525 *vr0min = NULL_TREE;
8526 *vr0max = NULL_TREE;
8528 else
8529 gcc_unreachable ();
8531 else if ((operand_less_p (vr1min, *vr0max) == 1
8532 || operand_equal_p (vr1min, *vr0max, 0))
8533 && operand_less_p (*vr0min, vr1min) == 1)
8535 /* [ ( ] ) or [ ]( ) */
8536 if (*vr0type == VR_ANTI_RANGE
8537 && vr1type == VR_ANTI_RANGE)
8538 *vr0max = vr1max;
8539 else if (*vr0type == VR_RANGE
8540 && vr1type == VR_RANGE)
8541 *vr0min = vr1min;
8542 else if (*vr0type == VR_RANGE
8543 && vr1type == VR_ANTI_RANGE)
8545 if (TREE_CODE (vr1min) == INTEGER_CST)
8546 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8547 build_int_cst (TREE_TYPE (vr1min), 1));
8548 else
8549 *vr0max = vr1min;
8551 else if (*vr0type == VR_ANTI_RANGE
8552 && vr1type == VR_RANGE)
8554 *vr0type = VR_RANGE;
8555 if (TREE_CODE (*vr0max) == INTEGER_CST)
8556 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8557 build_int_cst (TREE_TYPE (*vr0max), 1));
8558 else
8559 *vr0min = *vr0max;
8560 *vr0max = vr1max;
8562 else
8563 gcc_unreachable ();
8565 else if ((operand_less_p (*vr0min, vr1max) == 1
8566 || operand_equal_p (*vr0min, vr1max, 0))
8567 && operand_less_p (vr1min, *vr0min) == 1)
8569 /* ( [ ) ] or ( )[ ] */
8570 if (*vr0type == VR_ANTI_RANGE
8571 && vr1type == VR_ANTI_RANGE)
8572 *vr0min = vr1min;
8573 else if (*vr0type == VR_RANGE
8574 && vr1type == VR_RANGE)
8575 *vr0max = vr1max;
8576 else if (*vr0type == VR_RANGE
8577 && vr1type == VR_ANTI_RANGE)
8579 if (TREE_CODE (vr1max) == INTEGER_CST)
8580 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8581 build_int_cst (TREE_TYPE (vr1max), 1));
8582 else
8583 *vr0min = vr1max;
8585 else if (*vr0type == VR_ANTI_RANGE
8586 && vr1type == VR_RANGE)
8588 *vr0type = VR_RANGE;
8589 if (TREE_CODE (*vr0min) == INTEGER_CST)
8590 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8591 build_int_cst (TREE_TYPE (*vr0min), 1));
8592 else
8593 *vr0max = *vr0min;
8594 *vr0min = vr1min;
8596 else
8597 gcc_unreachable ();
8600 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8601 result for the intersection. That's always a conservative
8602 correct estimate. */
8604 return;
8608 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8609 in *VR0. This may not be the smallest possible such range. */
8611 static void
8612 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8614 value_range_t saved;
8616 /* If either range is VR_VARYING the other one wins. */
8617 if (vr1->type == VR_VARYING)
8618 return;
8619 if (vr0->type == VR_VARYING)
8621 copy_value_range (vr0, vr1);
8622 return;
8625 /* When either range is VR_UNDEFINED the resulting range is
8626 VR_UNDEFINED, too. */
8627 if (vr0->type == VR_UNDEFINED)
8628 return;
8629 if (vr1->type == VR_UNDEFINED)
8631 set_value_range_to_undefined (vr0);
8632 return;
8635 /* Save the original vr0 so we can return it as conservative intersection
8636 result when our worker turns things to varying. */
8637 saved = *vr0;
8638 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8639 vr1->type, vr1->min, vr1->max);
8640 /* Make sure to canonicalize the result though as the inversion of a
8641 VR_RANGE can still be a VR_RANGE. */
8642 set_and_canonicalize_value_range (vr0, vr0->type,
8643 vr0->min, vr0->max, vr0->equiv);
8644 /* If that failed, use the saved original VR0. */
8645 if (vr0->type == VR_VARYING)
8647 *vr0 = saved;
8648 return;
8650 /* If the result is VR_UNDEFINED there is no need to mess with
8651 the equivalencies. */
8652 if (vr0->type == VR_UNDEFINED)
8653 return;
8655 /* The resulting set of equivalences for range intersection is the union of
8656 the two sets. */
8657 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8658 bitmap_ior_into (vr0->equiv, vr1->equiv);
8659 else if (vr1->equiv && !vr0->equiv)
8660 bitmap_copy (vr0->equiv, vr1->equiv);
8663 static void
8664 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8666 if (dump_file && (dump_flags & TDF_DETAILS))
8668 fprintf (dump_file, "Intersecting\n ");
8669 dump_value_range (dump_file, vr0);
8670 fprintf (dump_file, "\nand\n ");
8671 dump_value_range (dump_file, vr1);
8672 fprintf (dump_file, "\n");
8674 vrp_intersect_ranges_1 (vr0, vr1);
8675 if (dump_file && (dump_flags & TDF_DETAILS))
8677 fprintf (dump_file, "to\n ");
8678 dump_value_range (dump_file, vr0);
8679 fprintf (dump_file, "\n");
8683 /* Meet operation for value ranges. Given two value ranges VR0 and
8684 VR1, store in VR0 a range that contains both VR0 and VR1. This
8685 may not be the smallest possible such range. */
8687 static void
8688 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8690 value_range_t saved;
8692 if (vr0->type == VR_UNDEFINED)
8694 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8695 return;
8698 if (vr1->type == VR_UNDEFINED)
8700 /* VR0 already has the resulting range. */
8701 return;
8704 if (vr0->type == VR_VARYING)
8706 /* Nothing to do. VR0 already has the resulting range. */
8707 return;
8710 if (vr1->type == VR_VARYING)
8712 set_value_range_to_varying (vr0);
8713 return;
8716 saved = *vr0;
8717 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8718 vr1->type, vr1->min, vr1->max);
8719 if (vr0->type == VR_VARYING)
8721 /* Failed to find an efficient meet. Before giving up and setting
8722 the result to VARYING, see if we can at least derive a useful
8723 anti-range. FIXME, all this nonsense about distinguishing
8724 anti-ranges from ranges is necessary because of the odd
8725 semantics of range_includes_zero_p and friends. */
8726 if (((saved.type == VR_RANGE
8727 && range_includes_zero_p (saved.min, saved.max) == 0)
8728 || (saved.type == VR_ANTI_RANGE
8729 && range_includes_zero_p (saved.min, saved.max) == 1))
8730 && ((vr1->type == VR_RANGE
8731 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8732 || (vr1->type == VR_ANTI_RANGE
8733 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8735 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8737 /* Since this meet operation did not result from the meeting of
8738 two equivalent names, VR0 cannot have any equivalences. */
8739 if (vr0->equiv)
8740 bitmap_clear (vr0->equiv);
8741 return;
8744 set_value_range_to_varying (vr0);
8745 return;
8747 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8748 vr0->equiv);
8749 if (vr0->type == VR_VARYING)
8750 return;
8752 /* The resulting set of equivalences is always the intersection of
8753 the two sets. */
8754 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8755 bitmap_and_into (vr0->equiv, vr1->equiv);
8756 else if (vr0->equiv && !vr1->equiv)
8757 bitmap_clear (vr0->equiv);
8760 static void
8761 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8763 if (dump_file && (dump_flags & TDF_DETAILS))
8765 fprintf (dump_file, "Meeting\n ");
8766 dump_value_range (dump_file, vr0);
8767 fprintf (dump_file, "\nand\n ");
8768 dump_value_range (dump_file, vr1);
8769 fprintf (dump_file, "\n");
8771 vrp_meet_1 (vr0, vr1);
8772 if (dump_file && (dump_flags & TDF_DETAILS))
8774 fprintf (dump_file, "to\n ");
8775 dump_value_range (dump_file, vr0);
8776 fprintf (dump_file, "\n");
8781 /* Visit all arguments for PHI node PHI that flow through executable
8782 edges. If a valid value range can be derived from all the incoming
8783 value ranges, set a new range for the LHS of PHI. */
8785 static enum ssa_prop_result
8786 vrp_visit_phi_node (gphi *phi)
8788 size_t i;
8789 tree lhs = PHI_RESULT (phi);
8790 value_range_t *lhs_vr = get_value_range (lhs);
8791 value_range_t vr_result = VR_INITIALIZER;
8792 bool first = true;
8793 int edges, old_edges;
8794 struct loop *l;
8796 if (dump_file && (dump_flags & TDF_DETAILS))
8798 fprintf (dump_file, "\nVisiting PHI node: ");
8799 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8802 edges = 0;
8803 for (i = 0; i < gimple_phi_num_args (phi); i++)
8805 edge e = gimple_phi_arg_edge (phi, i);
8807 if (dump_file && (dump_flags & TDF_DETAILS))
8809 fprintf (dump_file,
8810 " Argument #%d (%d -> %d %sexecutable)\n",
8811 (int) i, e->src->index, e->dest->index,
8812 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8815 if (e->flags & EDGE_EXECUTABLE)
8817 tree arg = PHI_ARG_DEF (phi, i);
8818 value_range_t vr_arg;
8820 ++edges;
8822 if (TREE_CODE (arg) == SSA_NAME)
8824 vr_arg = *(get_value_range (arg));
8825 /* Do not allow equivalences or symbolic ranges to leak in from
8826 backedges. That creates invalid equivalencies.
8827 See PR53465 and PR54767. */
8828 if (e->flags & EDGE_DFS_BACK)
8830 if (vr_arg.type == VR_RANGE
8831 || vr_arg.type == VR_ANTI_RANGE)
8833 vr_arg.equiv = NULL;
8834 if (symbolic_range_p (&vr_arg))
8836 vr_arg.type = VR_VARYING;
8837 vr_arg.min = NULL_TREE;
8838 vr_arg.max = NULL_TREE;
8842 else
8844 /* If the non-backedge arguments range is VR_VARYING then
8845 we can still try recording a simple equivalence. */
8846 if (vr_arg.type == VR_VARYING)
8848 vr_arg.type = VR_RANGE;
8849 vr_arg.min = arg;
8850 vr_arg.max = arg;
8851 vr_arg.equiv = NULL;
8855 else
8857 if (TREE_OVERFLOW_P (arg))
8858 arg = drop_tree_overflow (arg);
8860 vr_arg.type = VR_RANGE;
8861 vr_arg.min = arg;
8862 vr_arg.max = arg;
8863 vr_arg.equiv = NULL;
8866 if (dump_file && (dump_flags & TDF_DETAILS))
8868 fprintf (dump_file, "\t");
8869 print_generic_expr (dump_file, arg, dump_flags);
8870 fprintf (dump_file, ": ");
8871 dump_value_range (dump_file, &vr_arg);
8872 fprintf (dump_file, "\n");
8875 if (first)
8876 copy_value_range (&vr_result, &vr_arg);
8877 else
8878 vrp_meet (&vr_result, &vr_arg);
8879 first = false;
8881 if (vr_result.type == VR_VARYING)
8882 break;
8886 if (vr_result.type == VR_VARYING)
8887 goto varying;
8888 else if (vr_result.type == VR_UNDEFINED)
8889 goto update_range;
8891 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8892 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8894 /* To prevent infinite iterations in the algorithm, derive ranges
8895 when the new value is slightly bigger or smaller than the
8896 previous one. We don't do this if we have seen a new executable
8897 edge; this helps us avoid an overflow infinity for conditionals
8898 which are not in a loop. If the old value-range was VR_UNDEFINED
8899 use the updated range and iterate one more time. */
8900 if (edges > 0
8901 && gimple_phi_num_args (phi) > 1
8902 && edges == old_edges
8903 && lhs_vr->type != VR_UNDEFINED)
8905 /* Compare old and new ranges, fall back to varying if the
8906 values are not comparable. */
8907 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8908 if (cmp_min == -2)
8909 goto varying;
8910 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8911 if (cmp_max == -2)
8912 goto varying;
8914 /* For non VR_RANGE or for pointers fall back to varying if
8915 the range changed. */
8916 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8917 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8918 && (cmp_min != 0 || cmp_max != 0))
8919 goto varying;
8921 /* If the new minimum is larger than than the previous one
8922 retain the old value. If the new minimum value is smaller
8923 than the previous one and not -INF go all the way to -INF + 1.
8924 In the first case, to avoid infinite bouncing between different
8925 minimums, and in the other case to avoid iterating millions of
8926 times to reach -INF. Going to -INF + 1 also lets the following
8927 iteration compute whether there will be any overflow, at the
8928 expense of one additional iteration. */
8929 if (cmp_min < 0)
8930 vr_result.min = lhs_vr->min;
8931 else if (cmp_min > 0
8932 && !vrp_val_is_min (vr_result.min))
8933 vr_result.min
8934 = int_const_binop (PLUS_EXPR,
8935 vrp_val_min (TREE_TYPE (vr_result.min)),
8936 build_int_cst (TREE_TYPE (vr_result.min), 1));
8938 /* Similarly for the maximum value. */
8939 if (cmp_max > 0)
8940 vr_result.max = lhs_vr->max;
8941 else if (cmp_max < 0
8942 && !vrp_val_is_max (vr_result.max))
8943 vr_result.max
8944 = int_const_binop (MINUS_EXPR,
8945 vrp_val_max (TREE_TYPE (vr_result.min)),
8946 build_int_cst (TREE_TYPE (vr_result.min), 1));
8948 /* If we dropped either bound to +-INF then if this is a loop
8949 PHI node SCEV may known more about its value-range. */
8950 if ((cmp_min > 0 || cmp_min < 0
8951 || cmp_max < 0 || cmp_max > 0)
8952 && (l = loop_containing_stmt (phi))
8953 && l->header == gimple_bb (phi))
8954 adjust_range_with_scev (&vr_result, l, phi, lhs);
8956 /* If we will end up with a (-INF, +INF) range, set it to
8957 VARYING. Same if the previous max value was invalid for
8958 the type and we end up with vr_result.min > vr_result.max. */
8959 if ((vrp_val_is_max (vr_result.max)
8960 && vrp_val_is_min (vr_result.min))
8961 || compare_values (vr_result.min,
8962 vr_result.max) > 0)
8963 goto varying;
8966 /* If the new range is different than the previous value, keep
8967 iterating. */
8968 update_range:
8969 if (update_value_range (lhs, &vr_result))
8971 if (dump_file && (dump_flags & TDF_DETAILS))
8973 fprintf (dump_file, "Found new range for ");
8974 print_generic_expr (dump_file, lhs, 0);
8975 fprintf (dump_file, ": ");
8976 dump_value_range (dump_file, &vr_result);
8977 fprintf (dump_file, "\n");
8980 if (vr_result.type == VR_VARYING)
8981 return SSA_PROP_VARYING;
8983 return SSA_PROP_INTERESTING;
8986 /* Nothing changed, don't add outgoing edges. */
8987 return SSA_PROP_NOT_INTERESTING;
8989 /* No match found. Set the LHS to VARYING. */
8990 varying:
8991 set_value_range_to_varying (lhs_vr);
8992 return SSA_PROP_VARYING;
8995 /* Simplify boolean operations if the source is known
8996 to be already a boolean. */
8997 static bool
8998 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9000 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9001 tree lhs, op0, op1;
9002 bool need_conversion;
9004 /* We handle only !=/== case here. */
9005 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
9007 op0 = gimple_assign_rhs1 (stmt);
9008 if (!op_with_boolean_value_range_p (op0))
9009 return false;
9011 op1 = gimple_assign_rhs2 (stmt);
9012 if (!op_with_boolean_value_range_p (op1))
9013 return false;
9015 /* Reduce number of cases to handle to NE_EXPR. As there is no
9016 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9017 if (rhs_code == EQ_EXPR)
9019 if (TREE_CODE (op1) == INTEGER_CST)
9020 op1 = int_const_binop (BIT_XOR_EXPR, op1,
9021 build_int_cst (TREE_TYPE (op1), 1));
9022 else
9023 return false;
9026 lhs = gimple_assign_lhs (stmt);
9027 need_conversion
9028 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
9030 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9031 if (need_conversion
9032 && !TYPE_UNSIGNED (TREE_TYPE (op0))
9033 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
9034 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
9035 return false;
9037 /* For A != 0 we can substitute A itself. */
9038 if (integer_zerop (op1))
9039 gimple_assign_set_rhs_with_ops (gsi,
9040 need_conversion
9041 ? NOP_EXPR : TREE_CODE (op0), op0);
9042 /* For A != B we substitute A ^ B. Either with conversion. */
9043 else if (need_conversion)
9045 tree tem = make_ssa_name (TREE_TYPE (op0));
9046 gassign *newop
9047 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
9048 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
9049 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9051 /* Or without. */
9052 else
9053 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9054 update_stmt (gsi_stmt (*gsi));
9056 return true;
9059 /* Simplify a division or modulo operator to a right shift or
9060 bitwise and if the first operand is unsigned or is greater
9061 than zero and the second operand is an exact power of two.
9062 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9063 into just op0 if op0's range is known to be a subset of
9064 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9065 modulo. */
9067 static bool
9068 simplify_div_or_mod_using_ranges (gimple stmt)
9070 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9071 tree val = NULL;
9072 tree op0 = gimple_assign_rhs1 (stmt);
9073 tree op1 = gimple_assign_rhs2 (stmt);
9074 value_range_t *vr = get_value_range (op0);
9076 if (rhs_code == TRUNC_MOD_EXPR
9077 && TREE_CODE (op1) == INTEGER_CST
9078 && tree_int_cst_sgn (op1) == 1
9079 && range_int_cst_p (vr)
9080 && tree_int_cst_lt (vr->max, op1))
9082 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9083 || tree_int_cst_sgn (vr->min) >= 0
9084 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9085 vr->min))
9087 /* If op0 already has the range op0 % op1 has,
9088 then TRUNC_MOD_EXPR won't change anything. */
9089 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9090 gimple_assign_set_rhs_from_tree (&gsi, op0);
9091 update_stmt (stmt);
9092 return true;
9096 if (!integer_pow2p (op1))
9097 return false;
9099 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9101 val = integer_one_node;
9103 else
9105 bool sop = false;
9107 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9109 if (val
9110 && sop
9111 && integer_onep (val)
9112 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9114 location_t location;
9116 if (!gimple_has_location (stmt))
9117 location = input_location;
9118 else
9119 location = gimple_location (stmt);
9120 warning_at (location, OPT_Wstrict_overflow,
9121 "assuming signed overflow does not occur when "
9122 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9126 if (val && integer_onep (val))
9128 tree t;
9130 if (rhs_code == TRUNC_DIV_EXPR)
9132 t = build_int_cst (integer_type_node, tree_log2 (op1));
9133 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9134 gimple_assign_set_rhs1 (stmt, op0);
9135 gimple_assign_set_rhs2 (stmt, t);
9137 else
9139 t = build_int_cst (TREE_TYPE (op1), 1);
9140 t = int_const_binop (MINUS_EXPR, op1, t);
9141 t = fold_convert (TREE_TYPE (op0), t);
9143 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9144 gimple_assign_set_rhs1 (stmt, op0);
9145 gimple_assign_set_rhs2 (stmt, t);
9148 update_stmt (stmt);
9149 return true;
9152 return false;
9155 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9156 ABS_EXPR. If the operand is <= 0, then simplify the
9157 ABS_EXPR into a NEGATE_EXPR. */
9159 static bool
9160 simplify_abs_using_ranges (gimple stmt)
9162 tree val = NULL;
9163 tree op = gimple_assign_rhs1 (stmt);
9164 tree type = TREE_TYPE (op);
9165 value_range_t *vr = get_value_range (op);
9167 if (TYPE_UNSIGNED (type))
9169 val = integer_zero_node;
9171 else if (vr)
9173 bool sop = false;
9175 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9176 if (!val)
9178 sop = false;
9179 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
9180 &sop);
9182 if (val)
9184 if (integer_zerop (val))
9185 val = integer_one_node;
9186 else if (integer_onep (val))
9187 val = integer_zero_node;
9191 if (val
9192 && (integer_onep (val) || integer_zerop (val)))
9194 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9196 location_t location;
9198 if (!gimple_has_location (stmt))
9199 location = input_location;
9200 else
9201 location = gimple_location (stmt);
9202 warning_at (location, OPT_Wstrict_overflow,
9203 "assuming signed overflow does not occur when "
9204 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9207 gimple_assign_set_rhs1 (stmt, op);
9208 if (integer_onep (val))
9209 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9210 else
9211 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9212 update_stmt (stmt);
9213 return true;
9217 return false;
9220 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9221 If all the bits that are being cleared by & are already
9222 known to be zero from VR, or all the bits that are being
9223 set by | are already known to be one from VR, the bit
9224 operation is redundant. */
9226 static bool
9227 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9229 tree op0 = gimple_assign_rhs1 (stmt);
9230 tree op1 = gimple_assign_rhs2 (stmt);
9231 tree op = NULL_TREE;
9232 value_range_t vr0 = VR_INITIALIZER;
9233 value_range_t vr1 = VR_INITIALIZER;
9234 wide_int may_be_nonzero0, may_be_nonzero1;
9235 wide_int must_be_nonzero0, must_be_nonzero1;
9236 wide_int mask;
9238 if (TREE_CODE (op0) == SSA_NAME)
9239 vr0 = *(get_value_range (op0));
9240 else if (is_gimple_min_invariant (op0))
9241 set_value_range_to_value (&vr0, op0, NULL);
9242 else
9243 return false;
9245 if (TREE_CODE (op1) == SSA_NAME)
9246 vr1 = *(get_value_range (op1));
9247 else if (is_gimple_min_invariant (op1))
9248 set_value_range_to_value (&vr1, op1, NULL);
9249 else
9250 return false;
9252 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9253 &must_be_nonzero0))
9254 return false;
9255 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9256 &must_be_nonzero1))
9257 return false;
9259 switch (gimple_assign_rhs_code (stmt))
9261 case BIT_AND_EXPR:
9262 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9263 if (mask == 0)
9265 op = op0;
9266 break;
9268 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9269 if (mask == 0)
9271 op = op1;
9272 break;
9274 break;
9275 case BIT_IOR_EXPR:
9276 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9277 if (mask == 0)
9279 op = op1;
9280 break;
9282 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9283 if (mask == 0)
9285 op = op0;
9286 break;
9288 break;
9289 default:
9290 gcc_unreachable ();
9293 if (op == NULL_TREE)
9294 return false;
9296 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9297 update_stmt (gsi_stmt (*gsi));
9298 return true;
9301 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9302 a known value range VR.
9304 If there is one and only one value which will satisfy the
9305 conditional, then return that value. Else return NULL.
9307 If signed overflow must be undefined for the value to satisfy
9308 the conditional, then set *STRICT_OVERFLOW_P to true. */
9310 static tree
9311 test_for_singularity (enum tree_code cond_code, tree op0,
9312 tree op1, value_range_t *vr,
9313 bool *strict_overflow_p)
9315 tree min = NULL;
9316 tree max = NULL;
9318 /* Extract minimum/maximum values which satisfy the
9319 the conditional as it was written. */
9320 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9322 /* This should not be negative infinity; there is no overflow
9323 here. */
9324 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9326 max = op1;
9327 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9329 tree one = build_int_cst (TREE_TYPE (op0), 1);
9330 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9331 if (EXPR_P (max))
9332 TREE_NO_WARNING (max) = 1;
9335 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9337 /* This should not be positive infinity; there is no overflow
9338 here. */
9339 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9341 min = op1;
9342 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9344 tree one = build_int_cst (TREE_TYPE (op0), 1);
9345 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9346 if (EXPR_P (min))
9347 TREE_NO_WARNING (min) = 1;
9351 /* Now refine the minimum and maximum values using any
9352 value range information we have for op0. */
9353 if (min && max)
9355 if (compare_values (vr->min, min) == 1)
9356 min = vr->min;
9357 if (compare_values (vr->max, max) == -1)
9358 max = vr->max;
9360 /* If the new min/max values have converged to a single value,
9361 then there is only one value which can satisfy the condition,
9362 return that value. */
9363 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9365 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9366 && is_overflow_infinity (vr->max))
9367 *strict_overflow_p = true;
9368 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9369 && is_overflow_infinity (vr->min))
9370 *strict_overflow_p = true;
9372 return min;
9375 return NULL;
9378 /* Return whether the value range *VR fits in an integer type specified
9379 by PRECISION and UNSIGNED_P. */
9381 static bool
9382 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9384 tree src_type;
9385 unsigned src_precision;
9386 widest_int tem;
9387 signop src_sgn;
9389 /* We can only handle integral and pointer types. */
9390 src_type = TREE_TYPE (vr->min);
9391 if (!INTEGRAL_TYPE_P (src_type)
9392 && !POINTER_TYPE_P (src_type))
9393 return false;
9395 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9396 and so is an identity transform. */
9397 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9398 src_sgn = TYPE_SIGN (src_type);
9399 if ((src_precision < dest_precision
9400 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9401 || (src_precision == dest_precision && src_sgn == dest_sgn))
9402 return true;
9404 /* Now we can only handle ranges with constant bounds. */
9405 if (vr->type != VR_RANGE
9406 || TREE_CODE (vr->min) != INTEGER_CST
9407 || TREE_CODE (vr->max) != INTEGER_CST)
9408 return false;
9410 /* For sign changes, the MSB of the wide_int has to be clear.
9411 An unsigned value with its MSB set cannot be represented by
9412 a signed wide_int, while a negative value cannot be represented
9413 by an unsigned wide_int. */
9414 if (src_sgn != dest_sgn
9415 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9416 return false;
9418 /* Then we can perform the conversion on both ends and compare
9419 the result for equality. */
9420 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9421 if (tem != wi::to_widest (vr->min))
9422 return false;
9423 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9424 if (tem != wi::to_widest (vr->max))
9425 return false;
9427 return true;
9430 /* Simplify a conditional using a relational operator to an equality
9431 test if the range information indicates only one value can satisfy
9432 the original conditional. */
9434 static bool
9435 simplify_cond_using_ranges (gcond *stmt)
9437 tree op0 = gimple_cond_lhs (stmt);
9438 tree op1 = gimple_cond_rhs (stmt);
9439 enum tree_code cond_code = gimple_cond_code (stmt);
9441 if (cond_code != NE_EXPR
9442 && cond_code != EQ_EXPR
9443 && TREE_CODE (op0) == SSA_NAME
9444 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9445 && is_gimple_min_invariant (op1))
9447 value_range_t *vr = get_value_range (op0);
9449 /* If we have range information for OP0, then we might be
9450 able to simplify this conditional. */
9451 if (vr->type == VR_RANGE)
9453 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9454 bool sop = false;
9455 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9457 if (new_tree
9458 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9460 if (dump_file)
9462 fprintf (dump_file, "Simplified relational ");
9463 print_gimple_stmt (dump_file, stmt, 0, 0);
9464 fprintf (dump_file, " into ");
9467 gimple_cond_set_code (stmt, EQ_EXPR);
9468 gimple_cond_set_lhs (stmt, op0);
9469 gimple_cond_set_rhs (stmt, new_tree);
9471 update_stmt (stmt);
9473 if (dump_file)
9475 print_gimple_stmt (dump_file, stmt, 0, 0);
9476 fprintf (dump_file, "\n");
9479 if (sop && issue_strict_overflow_warning (wc))
9481 location_t location = input_location;
9482 if (gimple_has_location (stmt))
9483 location = gimple_location (stmt);
9485 warning_at (location, OPT_Wstrict_overflow,
9486 "assuming signed overflow does not occur when "
9487 "simplifying conditional");
9490 return true;
9493 /* Try again after inverting the condition. We only deal
9494 with integral types here, so no need to worry about
9495 issues with inverting FP comparisons. */
9496 sop = false;
9497 new_tree = test_for_singularity
9498 (invert_tree_comparison (cond_code, false),
9499 op0, op1, vr, &sop);
9501 if (new_tree
9502 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9504 if (dump_file)
9506 fprintf (dump_file, "Simplified relational ");
9507 print_gimple_stmt (dump_file, stmt, 0, 0);
9508 fprintf (dump_file, " into ");
9511 gimple_cond_set_code (stmt, NE_EXPR);
9512 gimple_cond_set_lhs (stmt, op0);
9513 gimple_cond_set_rhs (stmt, new_tree);
9515 update_stmt (stmt);
9517 if (dump_file)
9519 print_gimple_stmt (dump_file, stmt, 0, 0);
9520 fprintf (dump_file, "\n");
9523 if (sop && issue_strict_overflow_warning (wc))
9525 location_t location = input_location;
9526 if (gimple_has_location (stmt))
9527 location = gimple_location (stmt);
9529 warning_at (location, OPT_Wstrict_overflow,
9530 "assuming signed overflow does not occur when "
9531 "simplifying conditional");
9534 return true;
9539 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9540 see if OP0 was set by a type conversion where the source of
9541 the conversion is another SSA_NAME with a range that fits
9542 into the range of OP0's type.
9544 If so, the conversion is redundant as the earlier SSA_NAME can be
9545 used for the comparison directly if we just massage the constant in the
9546 comparison. */
9547 if (TREE_CODE (op0) == SSA_NAME
9548 && TREE_CODE (op1) == INTEGER_CST)
9550 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9551 tree innerop;
9553 if (!is_gimple_assign (def_stmt)
9554 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9555 return false;
9557 innerop = gimple_assign_rhs1 (def_stmt);
9559 if (TREE_CODE (innerop) == SSA_NAME
9560 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9562 value_range_t *vr = get_value_range (innerop);
9564 if (range_int_cst_p (vr)
9565 && range_fits_type_p (vr,
9566 TYPE_PRECISION (TREE_TYPE (op0)),
9567 TYPE_SIGN (TREE_TYPE (op0)))
9568 && int_fits_type_p (op1, TREE_TYPE (innerop))
9569 /* The range must not have overflowed, or if it did overflow
9570 we must not be wrapping/trapping overflow and optimizing
9571 with strict overflow semantics. */
9572 && ((!is_negative_overflow_infinity (vr->min)
9573 && !is_positive_overflow_infinity (vr->max))
9574 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9576 /* If the range overflowed and the user has asked for warnings
9577 when strict overflow semantics were used to optimize code,
9578 issue an appropriate warning. */
9579 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9580 && (is_negative_overflow_infinity (vr->min)
9581 || is_positive_overflow_infinity (vr->max))
9582 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9584 location_t location;
9586 if (!gimple_has_location (stmt))
9587 location = input_location;
9588 else
9589 location = gimple_location (stmt);
9590 warning_at (location, OPT_Wstrict_overflow,
9591 "assuming signed overflow does not occur when "
9592 "simplifying conditional");
9595 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9596 gimple_cond_set_lhs (stmt, innerop);
9597 gimple_cond_set_rhs (stmt, newconst);
9598 return true;
9603 return false;
9606 /* Simplify a switch statement using the value range of the switch
9607 argument. */
9609 static bool
9610 simplify_switch_using_ranges (gswitch *stmt)
9612 tree op = gimple_switch_index (stmt);
9613 value_range_t *vr;
9614 bool take_default;
9615 edge e;
9616 edge_iterator ei;
9617 size_t i = 0, j = 0, n, n2;
9618 tree vec2;
9619 switch_update su;
9620 size_t k = 1, l = 0;
9622 if (TREE_CODE (op) == SSA_NAME)
9624 vr = get_value_range (op);
9626 /* We can only handle integer ranges. */
9627 if ((vr->type != VR_RANGE
9628 && vr->type != VR_ANTI_RANGE)
9629 || symbolic_range_p (vr))
9630 return false;
9632 /* Find case label for min/max of the value range. */
9633 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9635 else if (TREE_CODE (op) == INTEGER_CST)
9637 take_default = !find_case_label_index (stmt, 1, op, &i);
9638 if (take_default)
9640 i = 1;
9641 j = 0;
9643 else
9645 j = i;
9648 else
9649 return false;
9651 n = gimple_switch_num_labels (stmt);
9653 /* Bail out if this is just all edges taken. */
9654 if (i == 1
9655 && j == n - 1
9656 && take_default)
9657 return false;
9659 /* Build a new vector of taken case labels. */
9660 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9661 n2 = 0;
9663 /* Add the default edge, if necessary. */
9664 if (take_default)
9665 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9667 for (; i <= j; ++i, ++n2)
9668 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9670 for (; k <= l; ++k, ++n2)
9671 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9673 /* Mark needed edges. */
9674 for (i = 0; i < n2; ++i)
9676 e = find_edge (gimple_bb (stmt),
9677 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9678 e->aux = (void *)-1;
9681 /* Queue not needed edges for later removal. */
9682 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9684 if (e->aux == (void *)-1)
9686 e->aux = NULL;
9687 continue;
9690 if (dump_file && (dump_flags & TDF_DETAILS))
9692 fprintf (dump_file, "removing unreachable case label\n");
9694 to_remove_edges.safe_push (e);
9695 e->flags &= ~EDGE_EXECUTABLE;
9698 /* And queue an update for the stmt. */
9699 su.stmt = stmt;
9700 su.vec = vec2;
9701 to_update_switch_stmts.safe_push (su);
9702 return false;
9705 /* Simplify an integral conversion from an SSA name in STMT. */
9707 static bool
9708 simplify_conversion_using_ranges (gimple stmt)
9710 tree innerop, middleop, finaltype;
9711 gimple def_stmt;
9712 value_range_t *innervr;
9713 signop inner_sgn, middle_sgn, final_sgn;
9714 unsigned inner_prec, middle_prec, final_prec;
9715 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9717 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9718 if (!INTEGRAL_TYPE_P (finaltype))
9719 return false;
9720 middleop = gimple_assign_rhs1 (stmt);
9721 def_stmt = SSA_NAME_DEF_STMT (middleop);
9722 if (!is_gimple_assign (def_stmt)
9723 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9724 return false;
9725 innerop = gimple_assign_rhs1 (def_stmt);
9726 if (TREE_CODE (innerop) != SSA_NAME
9727 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9728 return false;
9730 /* Get the value-range of the inner operand. */
9731 innervr = get_value_range (innerop);
9732 if (innervr->type != VR_RANGE
9733 || TREE_CODE (innervr->min) != INTEGER_CST
9734 || TREE_CODE (innervr->max) != INTEGER_CST)
9735 return false;
9737 /* Simulate the conversion chain to check if the result is equal if
9738 the middle conversion is removed. */
9739 innermin = wi::to_widest (innervr->min);
9740 innermax = wi::to_widest (innervr->max);
9742 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9743 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9744 final_prec = TYPE_PRECISION (finaltype);
9746 /* If the first conversion is not injective, the second must not
9747 be widening. */
9748 if (wi::gtu_p (innermax - innermin,
9749 wi::mask <widest_int> (middle_prec, false))
9750 && middle_prec < final_prec)
9751 return false;
9752 /* We also want a medium value so that we can track the effect that
9753 narrowing conversions with sign change have. */
9754 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9755 if (inner_sgn == UNSIGNED)
9756 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9757 else
9758 innermed = 0;
9759 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9760 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9761 innermed = innermin;
9763 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9764 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9765 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9766 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9768 /* Require that the final conversion applied to both the original
9769 and the intermediate range produces the same result. */
9770 final_sgn = TYPE_SIGN (finaltype);
9771 if (wi::ext (middlemin, final_prec, final_sgn)
9772 != wi::ext (innermin, final_prec, final_sgn)
9773 || wi::ext (middlemed, final_prec, final_sgn)
9774 != wi::ext (innermed, final_prec, final_sgn)
9775 || wi::ext (middlemax, final_prec, final_sgn)
9776 != wi::ext (innermax, final_prec, final_sgn))
9777 return false;
9779 gimple_assign_set_rhs1 (stmt, innerop);
9780 update_stmt (stmt);
9781 return true;
9784 /* Simplify a conversion from integral SSA name to float in STMT. */
9786 static bool
9787 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9789 tree rhs1 = gimple_assign_rhs1 (stmt);
9790 value_range_t *vr = get_value_range (rhs1);
9791 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9792 machine_mode mode;
9793 tree tem;
9794 gassign *conv;
9796 /* We can only handle constant ranges. */
9797 if (vr->type != VR_RANGE
9798 || TREE_CODE (vr->min) != INTEGER_CST
9799 || TREE_CODE (vr->max) != INTEGER_CST)
9800 return false;
9802 /* First check if we can use a signed type in place of an unsigned. */
9803 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9804 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9805 != CODE_FOR_nothing)
9806 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9807 mode = TYPE_MODE (TREE_TYPE (rhs1));
9808 /* If we can do the conversion in the current input mode do nothing. */
9809 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9810 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9811 return false;
9812 /* Otherwise search for a mode we can use, starting from the narrowest
9813 integer mode available. */
9814 else
9816 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9819 /* If we cannot do a signed conversion to float from mode
9820 or if the value-range does not fit in the signed type
9821 try with a wider mode. */
9822 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9823 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9824 break;
9826 mode = GET_MODE_WIDER_MODE (mode);
9827 /* But do not widen the input. Instead leave that to the
9828 optabs expansion code. */
9829 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9830 return false;
9832 while (mode != VOIDmode);
9833 if (mode == VOIDmode)
9834 return false;
9837 /* It works, insert a truncation or sign-change before the
9838 float conversion. */
9839 tem = make_ssa_name (build_nonstandard_integer_type
9840 (GET_MODE_PRECISION (mode), 0));
9841 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9842 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9843 gimple_assign_set_rhs1 (stmt, tem);
9844 update_stmt (stmt);
9846 return true;
9849 /* Simplify an internal fn call using ranges if possible. */
9851 static bool
9852 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9854 enum tree_code subcode;
9855 bool is_ubsan = false;
9856 bool ovf = false;
9857 switch (gimple_call_internal_fn (stmt))
9859 case IFN_UBSAN_CHECK_ADD:
9860 subcode = PLUS_EXPR;
9861 is_ubsan = true;
9862 break;
9863 case IFN_UBSAN_CHECK_SUB:
9864 subcode = MINUS_EXPR;
9865 is_ubsan = true;
9866 break;
9867 case IFN_UBSAN_CHECK_MUL:
9868 subcode = MULT_EXPR;
9869 is_ubsan = true;
9870 break;
9871 case IFN_ADD_OVERFLOW:
9872 subcode = PLUS_EXPR;
9873 break;
9874 case IFN_SUB_OVERFLOW:
9875 subcode = MINUS_EXPR;
9876 break;
9877 case IFN_MUL_OVERFLOW:
9878 subcode = MULT_EXPR;
9879 break;
9880 default:
9881 return false;
9884 tree op0 = gimple_call_arg (stmt, 0);
9885 tree op1 = gimple_call_arg (stmt, 1);
9886 tree type;
9887 if (is_ubsan)
9888 type = TREE_TYPE (op0);
9889 else if (gimple_call_lhs (stmt) == NULL_TREE)
9890 return false;
9891 else
9892 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9893 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9894 || (is_ubsan && ovf))
9895 return false;
9897 gimple g;
9898 location_t loc = gimple_location (stmt);
9899 if (is_ubsan)
9900 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9901 else
9903 int prec = TYPE_PRECISION (type);
9904 tree utype = type;
9905 if (ovf
9906 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9907 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9908 utype = build_nonstandard_integer_type (prec, 1);
9909 if (TREE_CODE (op0) == INTEGER_CST)
9910 op0 = fold_convert (utype, op0);
9911 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9913 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9914 gimple_set_location (g, loc);
9915 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9916 op0 = gimple_assign_lhs (g);
9918 if (TREE_CODE (op1) == INTEGER_CST)
9919 op1 = fold_convert (utype, op1);
9920 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9922 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9923 gimple_set_location (g, loc);
9924 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9925 op1 = gimple_assign_lhs (g);
9927 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9928 gimple_set_location (g, loc);
9929 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9930 if (utype != type)
9932 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9933 gimple_assign_lhs (g));
9934 gimple_set_location (g, loc);
9935 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9937 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9938 gimple_assign_lhs (g),
9939 build_int_cst (type, ovf));
9941 gimple_set_location (g, loc);
9942 gsi_replace (gsi, g, false);
9943 return true;
9946 /* Simplify STMT using ranges if possible. */
9948 static bool
9949 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9951 gimple stmt = gsi_stmt (*gsi);
9952 if (is_gimple_assign (stmt))
9954 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9955 tree rhs1 = gimple_assign_rhs1 (stmt);
9957 switch (rhs_code)
9959 case EQ_EXPR:
9960 case NE_EXPR:
9961 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9962 if the RHS is zero or one, and the LHS are known to be boolean
9963 values. */
9964 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9965 return simplify_truth_ops_using_ranges (gsi, stmt);
9966 break;
9968 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9969 and BIT_AND_EXPR respectively if the first operand is greater
9970 than zero and the second operand is an exact power of two.
9971 Also optimize TRUNC_MOD_EXPR away if the second operand is
9972 constant and the first operand already has the right value
9973 range. */
9974 case TRUNC_DIV_EXPR:
9975 case TRUNC_MOD_EXPR:
9976 if (TREE_CODE (rhs1) == SSA_NAME
9977 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9978 return simplify_div_or_mod_using_ranges (stmt);
9979 break;
9981 /* Transform ABS (X) into X or -X as appropriate. */
9982 case ABS_EXPR:
9983 if (TREE_CODE (rhs1) == SSA_NAME
9984 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9985 return simplify_abs_using_ranges (stmt);
9986 break;
9988 case BIT_AND_EXPR:
9989 case BIT_IOR_EXPR:
9990 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9991 if all the bits being cleared are already cleared or
9992 all the bits being set are already set. */
9993 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9994 return simplify_bit_ops_using_ranges (gsi, stmt);
9995 break;
9997 CASE_CONVERT:
9998 if (TREE_CODE (rhs1) == SSA_NAME
9999 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10000 return simplify_conversion_using_ranges (stmt);
10001 break;
10003 case FLOAT_EXPR:
10004 if (TREE_CODE (rhs1) == SSA_NAME
10005 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10006 return simplify_float_conversion_using_ranges (gsi, stmt);
10007 break;
10009 default:
10010 break;
10013 else if (gimple_code (stmt) == GIMPLE_COND)
10014 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
10015 else if (gimple_code (stmt) == GIMPLE_SWITCH)
10016 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
10017 else if (is_gimple_call (stmt)
10018 && gimple_call_internal_p (stmt))
10019 return simplify_internal_call_using_ranges (gsi, stmt);
10021 return false;
10024 /* If the statement pointed by SI has a predicate whose value can be
10025 computed using the value range information computed by VRP, compute
10026 its value and return true. Otherwise, return false. */
10028 static bool
10029 fold_predicate_in (gimple_stmt_iterator *si)
10031 bool assignment_p = false;
10032 tree val;
10033 gimple stmt = gsi_stmt (*si);
10035 if (is_gimple_assign (stmt)
10036 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10038 assignment_p = true;
10039 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10040 gimple_assign_rhs1 (stmt),
10041 gimple_assign_rhs2 (stmt),
10042 stmt);
10044 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10045 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10046 gimple_cond_lhs (cond_stmt),
10047 gimple_cond_rhs (cond_stmt),
10048 stmt);
10049 else
10050 return false;
10052 if (val)
10054 if (assignment_p)
10055 val = fold_convert (gimple_expr_type (stmt), val);
10057 if (dump_file)
10059 fprintf (dump_file, "Folding predicate ");
10060 print_gimple_expr (dump_file, stmt, 0, 0);
10061 fprintf (dump_file, " to ");
10062 print_generic_expr (dump_file, val, 0);
10063 fprintf (dump_file, "\n");
10066 if (is_gimple_assign (stmt))
10067 gimple_assign_set_rhs_from_tree (si, val);
10068 else
10070 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10071 gcond *cond_stmt = as_a <gcond *> (stmt);
10072 if (integer_zerop (val))
10073 gimple_cond_make_false (cond_stmt);
10074 else if (integer_onep (val))
10075 gimple_cond_make_true (cond_stmt);
10076 else
10077 gcc_unreachable ();
10080 return true;
10083 return false;
10086 /* Callback for substitute_and_fold folding the stmt at *SI. */
10088 static bool
10089 vrp_fold_stmt (gimple_stmt_iterator *si)
10091 if (fold_predicate_in (si))
10092 return true;
10094 return simplify_stmt_using_ranges (si);
10097 /* Unwindable const/copy equivalences. */
10098 const_and_copies *equiv_stack;
10100 /* A trivial wrapper so that we can present the generic jump threading
10101 code with a simple API for simplifying statements. STMT is the
10102 statement we want to simplify, WITHIN_STMT provides the location
10103 for any overflow warnings. */
10105 static tree
10106 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
10108 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10109 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10110 gimple_cond_lhs (cond_stmt),
10111 gimple_cond_rhs (cond_stmt),
10112 within_stmt);
10114 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10116 value_range_t new_vr = VR_INITIALIZER;
10117 tree lhs = gimple_assign_lhs (assign_stmt);
10119 if (TREE_CODE (lhs) == SSA_NAME
10120 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10121 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10123 extract_range_from_assignment (&new_vr, assign_stmt);
10124 if (range_int_cst_singleton_p (&new_vr))
10125 return new_vr.min;
10129 return NULL_TREE;
10132 /* Blocks which have more than one predecessor and more than
10133 one successor present jump threading opportunities, i.e.,
10134 when the block is reached from a specific predecessor, we
10135 may be able to determine which of the outgoing edges will
10136 be traversed. When this optimization applies, we are able
10137 to avoid conditionals at runtime and we may expose secondary
10138 optimization opportunities.
10140 This routine is effectively a driver for the generic jump
10141 threading code. It basically just presents the generic code
10142 with edges that may be suitable for jump threading.
10144 Unlike DOM, we do not iterate VRP if jump threading was successful.
10145 While iterating may expose new opportunities for VRP, it is expected
10146 those opportunities would be very limited and the compile time cost
10147 to expose those opportunities would be significant.
10149 As jump threading opportunities are discovered, they are registered
10150 for later realization. */
10152 static void
10153 identify_jump_threads (void)
10155 basic_block bb;
10156 gcond *dummy;
10157 int i;
10158 edge e;
10160 /* Ugh. When substituting values earlier in this pass we can
10161 wipe the dominance information. So rebuild the dominator
10162 information as we need it within the jump threading code. */
10163 calculate_dominance_info (CDI_DOMINATORS);
10165 /* We do not allow VRP information to be used for jump threading
10166 across a back edge in the CFG. Otherwise it becomes too
10167 difficult to avoid eliminating loop exit tests. Of course
10168 EDGE_DFS_BACK is not accurate at this time so we have to
10169 recompute it. */
10170 mark_dfs_back_edges ();
10172 /* Do not thread across edges we are about to remove. Just marking
10173 them as EDGE_DFS_BACK will do. */
10174 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10175 e->flags |= EDGE_DFS_BACK;
10177 /* Allocate our unwinder stack to unwind any temporary equivalences
10178 that might be recorded. */
10179 equiv_stack = new const_and_copies (dump_file, dump_flags);
10181 /* To avoid lots of silly node creation, we create a single
10182 conditional and just modify it in-place when attempting to
10183 thread jumps. */
10184 dummy = gimple_build_cond (EQ_EXPR,
10185 integer_zero_node, integer_zero_node,
10186 NULL, NULL);
10188 /* Walk through all the blocks finding those which present a
10189 potential jump threading opportunity. We could set this up
10190 as a dominator walker and record data during the walk, but
10191 I doubt it's worth the effort for the classes of jump
10192 threading opportunities we are trying to identify at this
10193 point in compilation. */
10194 FOR_EACH_BB_FN (bb, cfun)
10196 gimple last;
10198 /* If the generic jump threading code does not find this block
10199 interesting, then there is nothing to do. */
10200 if (! potentially_threadable_block (bb))
10201 continue;
10203 last = last_stmt (bb);
10205 /* We're basically looking for a switch or any kind of conditional with
10206 integral or pointer type arguments. Note the type of the second
10207 argument will be the same as the first argument, so no need to
10208 check it explicitly.
10210 We also handle the case where there are no statements in the
10211 block. This come up with forwarder blocks that are not
10212 optimized away because they lead to a loop header. But we do
10213 want to thread through them as we can sometimes thread to the
10214 loop exit which is obviously profitable. */
10215 if (!last
10216 || gimple_code (last) == GIMPLE_SWITCH
10217 || (gimple_code (last) == GIMPLE_COND
10218 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10219 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10220 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10221 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10222 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10224 edge_iterator ei;
10226 /* We've got a block with multiple predecessors and multiple
10227 successors which also ends in a suitable conditional or
10228 switch statement. For each predecessor, see if we can thread
10229 it to a specific successor. */
10230 FOR_EACH_EDGE (e, ei, bb->preds)
10232 /* Do not thread across back edges or abnormal edges
10233 in the CFG. */
10234 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
10235 continue;
10237 thread_across_edge (dummy, e, true, equiv_stack,
10238 simplify_stmt_for_jump_threading);
10243 /* We do not actually update the CFG or SSA graphs at this point as
10244 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10245 handle ASSERT_EXPRs gracefully. */
10248 /* We identified all the jump threading opportunities earlier, but could
10249 not transform the CFG at that time. This routine transforms the
10250 CFG and arranges for the dominator tree to be rebuilt if necessary.
10252 Note the SSA graph update will occur during the normal TODO
10253 processing by the pass manager. */
10254 static void
10255 finalize_jump_threads (void)
10257 thread_through_all_blocks (false);
10258 delete equiv_stack;
10262 /* Traverse all the blocks folding conditionals with known ranges. */
10264 static void
10265 vrp_finalize (void)
10267 size_t i;
10269 values_propagated = true;
10271 if (dump_file)
10273 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10274 dump_all_value_ranges (dump_file);
10275 fprintf (dump_file, "\n");
10278 substitute_and_fold (op_with_constant_singleton_value_range,
10279 vrp_fold_stmt, false);
10281 if (warn_array_bounds && first_pass_instance)
10282 check_all_array_refs ();
10284 /* We must identify jump threading opportunities before we release
10285 the datastructures built by VRP. */
10286 identify_jump_threads ();
10288 /* Set value range to non pointer SSA_NAMEs. */
10289 for (i = 0; i < num_vr_values; i++)
10290 if (vr_value[i])
10292 tree name = ssa_name (i);
10294 if (!name
10295 || POINTER_TYPE_P (TREE_TYPE (name))
10296 || (vr_value[i]->type == VR_VARYING)
10297 || (vr_value[i]->type == VR_UNDEFINED))
10298 continue;
10300 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10301 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10302 && (vr_value[i]->type == VR_RANGE
10303 || vr_value[i]->type == VR_ANTI_RANGE))
10304 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10305 vr_value[i]->max);
10308 /* Free allocated memory. */
10309 for (i = 0; i < num_vr_values; i++)
10310 if (vr_value[i])
10312 BITMAP_FREE (vr_value[i]->equiv);
10313 free (vr_value[i]);
10316 free (vr_value);
10317 free (vr_phi_edge_counts);
10319 /* So that we can distinguish between VRP data being available
10320 and not available. */
10321 vr_value = NULL;
10322 vr_phi_edge_counts = NULL;
10326 /* Main entry point to VRP (Value Range Propagation). This pass is
10327 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10328 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10329 Programming Language Design and Implementation, pp. 67-78, 1995.
10330 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10332 This is essentially an SSA-CCP pass modified to deal with ranges
10333 instead of constants.
10335 While propagating ranges, we may find that two or more SSA name
10336 have equivalent, though distinct ranges. For instance,
10338 1 x_9 = p_3->a;
10339 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10340 3 if (p_4 == q_2)
10341 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10342 5 endif
10343 6 if (q_2)
10345 In the code above, pointer p_5 has range [q_2, q_2], but from the
10346 code we can also determine that p_5 cannot be NULL and, if q_2 had
10347 a non-varying range, p_5's range should also be compatible with it.
10349 These equivalences are created by two expressions: ASSERT_EXPR and
10350 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10351 result of another assertion, then we can use the fact that p_5 and
10352 p_4 are equivalent when evaluating p_5's range.
10354 Together with value ranges, we also propagate these equivalences
10355 between names so that we can take advantage of information from
10356 multiple ranges when doing final replacement. Note that this
10357 equivalency relation is transitive but not symmetric.
10359 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10360 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10361 in contexts where that assertion does not hold (e.g., in line 6).
10363 TODO, the main difference between this pass and Patterson's is that
10364 we do not propagate edge probabilities. We only compute whether
10365 edges can be taken or not. That is, instead of having a spectrum
10366 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10367 DON'T KNOW. In the future, it may be worthwhile to propagate
10368 probabilities to aid branch prediction. */
10370 static unsigned int
10371 execute_vrp (void)
10373 int i;
10374 edge e;
10375 switch_update *su;
10377 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10378 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10379 scev_initialize ();
10381 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10382 Inserting assertions may split edges which will invalidate
10383 EDGE_DFS_BACK. */
10384 insert_range_assertions ();
10386 to_remove_edges.create (10);
10387 to_update_switch_stmts.create (5);
10388 threadedge_initialize_values ();
10390 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10391 mark_dfs_back_edges ();
10393 vrp_initialize ();
10394 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10395 vrp_finalize ();
10397 free_numbers_of_iterations_estimates ();
10399 /* ASSERT_EXPRs must be removed before finalizing jump threads
10400 as finalizing jump threads calls the CFG cleanup code which
10401 does not properly handle ASSERT_EXPRs. */
10402 remove_range_assertions ();
10404 /* If we exposed any new variables, go ahead and put them into
10405 SSA form now, before we handle jump threading. This simplifies
10406 interactions between rewriting of _DECL nodes into SSA form
10407 and rewriting SSA_NAME nodes into SSA form after block
10408 duplication and CFG manipulation. */
10409 update_ssa (TODO_update_ssa);
10411 finalize_jump_threads ();
10413 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10414 CFG in a broken state and requires a cfg_cleanup run. */
10415 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10416 remove_edge (e);
10417 /* Update SWITCH_EXPR case label vector. */
10418 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10420 size_t j;
10421 size_t n = TREE_VEC_LENGTH (su->vec);
10422 tree label;
10423 gimple_switch_set_num_labels (su->stmt, n);
10424 for (j = 0; j < n; j++)
10425 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10426 /* As we may have replaced the default label with a regular one
10427 make sure to make it a real default label again. This ensures
10428 optimal expansion. */
10429 label = gimple_switch_label (su->stmt, 0);
10430 CASE_LOW (label) = NULL_TREE;
10431 CASE_HIGH (label) = NULL_TREE;
10434 if (to_remove_edges.length () > 0)
10436 free_dominance_info (CDI_DOMINATORS);
10437 loops_state_set (LOOPS_NEED_FIXUP);
10440 to_remove_edges.release ();
10441 to_update_switch_stmts.release ();
10442 threadedge_finalize_values ();
10444 scev_finalize ();
10445 loop_optimizer_finalize ();
10446 return 0;
10449 namespace {
10451 const pass_data pass_data_vrp =
10453 GIMPLE_PASS, /* type */
10454 "vrp", /* name */
10455 OPTGROUP_NONE, /* optinfo_flags */
10456 TV_TREE_VRP, /* tv_id */
10457 PROP_ssa, /* properties_required */
10458 0, /* properties_provided */
10459 0, /* properties_destroyed */
10460 0, /* todo_flags_start */
10461 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10464 class pass_vrp : public gimple_opt_pass
10466 public:
10467 pass_vrp (gcc::context *ctxt)
10468 : gimple_opt_pass (pass_data_vrp, ctxt)
10471 /* opt_pass methods: */
10472 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10473 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10474 virtual unsigned int execute (function *) { return execute_vrp (); }
10476 }; // class pass_vrp
10478 } // anon namespace
10480 gimple_opt_pass *
10481 make_pass_vrp (gcc::context *ctxt)
10483 return new pass_vrp (ctxt);