PR 61187 Fix use of uninitialized memory.
[official-gcc.git] / gcc / tree-vrp.c
blob633c329030f604c102fc5b5bb5908dce2d6b6bed
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2014 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 "tree.h"
27 #include "stor-layout.h"
28 #include "calls.h"
29 #include "basic-block.h"
30 #include "tree-ssa-alias.h"
31 #include "internal-fn.h"
32 #include "gimple-fold.h"
33 #include "tree-eh.h"
34 #include "gimple-expr.h"
35 #include "is-a.h"
36 #include "gimple.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "gimple-ssa.h"
40 #include "tree-cfg.h"
41 #include "tree-phinodes.h"
42 #include "ssa-iterators.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
49 #include "tree-ssa.h"
50 #include "tree-pass.h"
51 #include "tree-dump.h"
52 #include "gimple-pretty-print.h"
53 #include "diagnostic-core.h"
54 #include "intl.h"
55 #include "cfgloop.h"
56 #include "tree-scalar-evolution.h"
57 #include "tree-ssa-propagate.h"
58 #include "tree-chrec.h"
59 #include "tree-ssa-threadupdate.h"
60 #include "expr.h"
61 #include "optabs.h"
62 #include "tree-ssa-threadedge.h"
63 #include "wide-int.h"
67 /* Range of values that can be associated with an SSA_NAME after VRP
68 has executed. */
69 struct value_range_d
71 /* Lattice value represented by this range. */
72 enum value_range_type type;
74 /* Minimum and maximum values represented by this range. These
75 values should be interpreted as follows:
77 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
78 be NULL.
80 - If TYPE == VR_RANGE then MIN holds the minimum value and
81 MAX holds the maximum value of the range [MIN, MAX].
83 - If TYPE == ANTI_RANGE the variable is known to NOT
84 take any values in the range [MIN, MAX]. */
85 tree min;
86 tree max;
88 /* Set of SSA names whose value ranges are equivalent to this one.
89 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
90 bitmap equiv;
93 typedef struct value_range_d value_range_t;
95 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
97 /* Set of SSA names found live during the RPO traversal of the function
98 for still active basic-blocks. */
99 static sbitmap *live;
101 /* Return true if the SSA name NAME is live on the edge E. */
103 static bool
104 live_on_edge (edge e, tree name)
106 return (live[e->dest->index]
107 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
110 /* Local functions. */
111 static int compare_values (tree val1, tree val2);
112 static int compare_values_warnv (tree val1, tree val2, bool *);
113 static void vrp_meet (value_range_t *, value_range_t *);
114 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
115 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
116 tree, tree, bool, bool *,
117 bool *);
119 /* Location information for ASSERT_EXPRs. Each instance of this
120 structure describes an ASSERT_EXPR for an SSA name. Since a single
121 SSA name may have more than one assertion associated with it, these
122 locations are kept in a linked list attached to the corresponding
123 SSA name. */
124 struct assert_locus_d
126 /* Basic block where the assertion would be inserted. */
127 basic_block bb;
129 /* Some assertions need to be inserted on an edge (e.g., assertions
130 generated by COND_EXPRs). In those cases, BB will be NULL. */
131 edge e;
133 /* Pointer to the statement that generated this assertion. */
134 gimple_stmt_iterator si;
136 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
137 enum tree_code comp_code;
139 /* Value being compared against. */
140 tree val;
142 /* Expression to compare. */
143 tree expr;
145 /* Next node in the linked list. */
146 struct assert_locus_d *next;
149 typedef struct assert_locus_d *assert_locus_t;
151 /* If bit I is present, it means that SSA name N_i has a list of
152 assertions that should be inserted in the IL. */
153 static bitmap need_assert_for;
155 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
156 holds a list of ASSERT_LOCUS_T nodes that describe where
157 ASSERT_EXPRs for SSA name N_I should be inserted. */
158 static assert_locus_t *asserts_for;
160 /* Value range array. After propagation, VR_VALUE[I] holds the range
161 of values that SSA name N_I may take. */
162 static unsigned num_vr_values;
163 static value_range_t **vr_value;
164 static bool values_propagated;
166 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
167 number of executable edges we saw the last time we visited the
168 node. */
169 static int *vr_phi_edge_counts;
171 typedef struct {
172 gimple stmt;
173 tree vec;
174 } switch_update;
176 static vec<edge> to_remove_edges;
177 static vec<switch_update> to_update_switch_stmts;
180 /* Return the maximum value for TYPE. */
182 static inline tree
183 vrp_val_max (const_tree type)
185 if (!INTEGRAL_TYPE_P (type))
186 return NULL_TREE;
188 return TYPE_MAX_VALUE (type);
191 /* Return the minimum value for TYPE. */
193 static inline tree
194 vrp_val_min (const_tree type)
196 if (!INTEGRAL_TYPE_P (type))
197 return NULL_TREE;
199 return TYPE_MIN_VALUE (type);
202 /* Return whether VAL is equal to the maximum value of its type. This
203 will be true for a positive overflow infinity. We can't do a
204 simple equality comparison with TYPE_MAX_VALUE because C typedefs
205 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
206 to the integer constant with the same value in the type. */
208 static inline bool
209 vrp_val_is_max (const_tree val)
211 tree type_max = vrp_val_max (TREE_TYPE (val));
212 return (val == type_max
213 || (type_max != NULL_TREE
214 && operand_equal_p (val, type_max, 0)));
217 /* Return whether VAL is equal to the minimum value of its type. This
218 will be true for a negative overflow infinity. */
220 static inline bool
221 vrp_val_is_min (const_tree val)
223 tree type_min = vrp_val_min (TREE_TYPE (val));
224 return (val == type_min
225 || (type_min != NULL_TREE
226 && operand_equal_p (val, type_min, 0)));
230 /* Return whether TYPE should use an overflow infinity distinct from
231 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
232 represent a signed overflow during VRP computations. An infinity
233 is distinct from a half-range, which will go from some number to
234 TYPE_{MIN,MAX}_VALUE. */
236 static inline bool
237 needs_overflow_infinity (const_tree type)
239 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
242 /* Return whether TYPE can support our overflow infinity
243 representation: we use the TREE_OVERFLOW flag, which only exists
244 for constants. If TYPE doesn't support this, we don't optimize
245 cases which would require signed overflow--we drop them to
246 VARYING. */
248 static inline bool
249 supports_overflow_infinity (const_tree type)
251 tree min = vrp_val_min (type), max = vrp_val_max (type);
252 #ifdef ENABLE_CHECKING
253 gcc_assert (needs_overflow_infinity (type));
254 #endif
255 return (min != NULL_TREE
256 && CONSTANT_CLASS_P (min)
257 && max != NULL_TREE
258 && CONSTANT_CLASS_P (max));
261 /* VAL is the maximum or minimum value of a type. Return a
262 corresponding overflow infinity. */
264 static inline tree
265 make_overflow_infinity (tree val)
267 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
268 val = copy_node (val);
269 TREE_OVERFLOW (val) = 1;
270 return val;
273 /* Return a negative overflow infinity for TYPE. */
275 static inline tree
276 negative_overflow_infinity (tree type)
278 gcc_checking_assert (supports_overflow_infinity (type));
279 return make_overflow_infinity (vrp_val_min (type));
282 /* Return a positive overflow infinity for TYPE. */
284 static inline tree
285 positive_overflow_infinity (tree type)
287 gcc_checking_assert (supports_overflow_infinity (type));
288 return make_overflow_infinity (vrp_val_max (type));
291 /* Return whether VAL is a negative overflow infinity. */
293 static inline bool
294 is_negative_overflow_infinity (const_tree val)
296 return (needs_overflow_infinity (TREE_TYPE (val))
297 && CONSTANT_CLASS_P (val)
298 && TREE_OVERFLOW (val)
299 && vrp_val_is_min (val));
302 /* Return whether VAL is a positive overflow infinity. */
304 static inline bool
305 is_positive_overflow_infinity (const_tree val)
307 return (needs_overflow_infinity (TREE_TYPE (val))
308 && CONSTANT_CLASS_P (val)
309 && TREE_OVERFLOW (val)
310 && vrp_val_is_max (val));
313 /* Return whether VAL is a positive or negative overflow infinity. */
315 static inline bool
316 is_overflow_infinity (const_tree val)
318 return (needs_overflow_infinity (TREE_TYPE (val))
319 && CONSTANT_CLASS_P (val)
320 && TREE_OVERFLOW (val)
321 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
324 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
326 static inline bool
327 stmt_overflow_infinity (gimple stmt)
329 if (is_gimple_assign (stmt)
330 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
331 GIMPLE_SINGLE_RHS)
332 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
333 return false;
336 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
337 the same value with TREE_OVERFLOW clear. This can be used to avoid
338 confusing a regular value with an overflow value. */
340 static inline tree
341 avoid_overflow_infinity (tree val)
343 if (!is_overflow_infinity (val))
344 return val;
346 if (vrp_val_is_max (val))
347 return vrp_val_max (TREE_TYPE (val));
348 else
350 gcc_checking_assert (vrp_val_is_min (val));
351 return vrp_val_min (TREE_TYPE (val));
356 /* Return true if ARG is marked with the nonnull attribute in the
357 current function signature. */
359 static bool
360 nonnull_arg_p (const_tree arg)
362 tree t, attrs, fntype;
363 unsigned HOST_WIDE_INT arg_num;
365 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
367 /* The static chain decl is always non null. */
368 if (arg == cfun->static_chain_decl)
369 return true;
371 fntype = TREE_TYPE (current_function_decl);
372 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
374 attrs = lookup_attribute ("nonnull", attrs);
376 /* If "nonnull" wasn't specified, we know nothing about the argument. */
377 if (attrs == NULL_TREE)
378 return false;
380 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
381 if (TREE_VALUE (attrs) == NULL_TREE)
382 return true;
384 /* Get the position number for ARG in the function signature. */
385 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
387 t = DECL_CHAIN (t), arg_num++)
389 if (t == arg)
390 break;
393 gcc_assert (t == arg);
395 /* Now see if ARG_NUM is mentioned in the nonnull list. */
396 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
398 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
399 return true;
403 return false;
407 /* Set value range VR to VR_UNDEFINED. */
409 static inline void
410 set_value_range_to_undefined (value_range_t *vr)
412 vr->type = VR_UNDEFINED;
413 vr->min = vr->max = NULL_TREE;
414 if (vr->equiv)
415 bitmap_clear (vr->equiv);
419 /* Set value range VR to VR_VARYING. */
421 static inline void
422 set_value_range_to_varying (value_range_t *vr)
424 vr->type = VR_VARYING;
425 vr->min = vr->max = NULL_TREE;
426 if (vr->equiv)
427 bitmap_clear (vr->equiv);
431 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
433 static void
434 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
435 tree max, bitmap equiv)
437 #if defined ENABLE_CHECKING
438 /* Check the validity of the range. */
439 if (t == VR_RANGE || t == VR_ANTI_RANGE)
441 int cmp;
443 gcc_assert (min && max);
445 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
446 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
448 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
449 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
451 cmp = compare_values (min, max);
452 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
454 if (needs_overflow_infinity (TREE_TYPE (min)))
455 gcc_assert (!is_overflow_infinity (min)
456 || !is_overflow_infinity (max));
459 if (t == VR_UNDEFINED || t == VR_VARYING)
460 gcc_assert (min == NULL_TREE && max == NULL_TREE);
462 if (t == VR_UNDEFINED || t == VR_VARYING)
463 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
464 #endif
466 vr->type = t;
467 vr->min = min;
468 vr->max = max;
470 /* Since updating the equivalence set involves deep copying the
471 bitmaps, only do it if absolutely necessary. */
472 if (vr->equiv == NULL
473 && equiv != NULL)
474 vr->equiv = BITMAP_ALLOC (NULL);
476 if (equiv != vr->equiv)
478 if (equiv && !bitmap_empty_p (equiv))
479 bitmap_copy (vr->equiv, equiv);
480 else
481 bitmap_clear (vr->equiv);
486 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
487 This means adjusting T, MIN and MAX representing the case of a
488 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
489 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
490 In corner cases where MAX+1 or MIN-1 wraps this will fall back
491 to varying.
492 This routine exists to ease canonicalization in the case where we
493 extract ranges from var + CST op limit. */
495 static void
496 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
497 tree min, tree max, bitmap equiv)
499 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
500 if (t == VR_UNDEFINED)
502 set_value_range_to_undefined (vr);
503 return;
505 else if (t == VR_VARYING)
507 set_value_range_to_varying (vr);
508 return;
511 /* Nothing to canonicalize for symbolic ranges. */
512 if (TREE_CODE (min) != INTEGER_CST
513 || TREE_CODE (max) != INTEGER_CST)
515 set_value_range (vr, t, min, max, equiv);
516 return;
519 /* Wrong order for min and max, to swap them and the VR type we need
520 to adjust them. */
521 if (tree_int_cst_lt (max, min))
523 tree one, tmp;
525 /* For one bit precision if max < min, then the swapped
526 range covers all values, so for VR_RANGE it is varying and
527 for VR_ANTI_RANGE empty range, so drop to varying as well. */
528 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
530 set_value_range_to_varying (vr);
531 return;
534 one = build_int_cst (TREE_TYPE (min), 1);
535 tmp = int_const_binop (PLUS_EXPR, max, one);
536 max = int_const_binop (MINUS_EXPR, min, one);
537 min = tmp;
539 /* There's one corner case, if we had [C+1, C] before we now have
540 that again. But this represents an empty value range, so drop
541 to varying in this case. */
542 if (tree_int_cst_lt (max, min))
544 set_value_range_to_varying (vr);
545 return;
548 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
551 /* Anti-ranges that can be represented as ranges should be so. */
552 if (t == VR_ANTI_RANGE)
554 bool is_min = vrp_val_is_min (min);
555 bool is_max = vrp_val_is_max (max);
557 if (is_min && is_max)
559 /* We cannot deal with empty ranges, drop to varying.
560 ??? This could be VR_UNDEFINED instead. */
561 set_value_range_to_varying (vr);
562 return;
564 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
565 && (is_min || is_max))
567 /* Non-empty boolean ranges can always be represented
568 as a singleton range. */
569 if (is_min)
570 min = max = vrp_val_max (TREE_TYPE (min));
571 else
572 min = max = vrp_val_min (TREE_TYPE (min));
573 t = VR_RANGE;
575 else if (is_min
576 /* As a special exception preserve non-null ranges. */
577 && !(TYPE_UNSIGNED (TREE_TYPE (min))
578 && integer_zerop (max)))
580 tree one = build_int_cst (TREE_TYPE (max), 1);
581 min = int_const_binop (PLUS_EXPR, max, one);
582 max = vrp_val_max (TREE_TYPE (max));
583 t = VR_RANGE;
585 else if (is_max)
587 tree one = build_int_cst (TREE_TYPE (min), 1);
588 max = int_const_binop (MINUS_EXPR, min, one);
589 min = vrp_val_min (TREE_TYPE (min));
590 t = VR_RANGE;
594 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
595 if (needs_overflow_infinity (TREE_TYPE (min))
596 && is_overflow_infinity (min)
597 && is_overflow_infinity (max))
599 set_value_range_to_varying (vr);
600 return;
603 set_value_range (vr, t, min, max, equiv);
606 /* Copy value range FROM into value range TO. */
608 static inline void
609 copy_value_range (value_range_t *to, value_range_t *from)
611 set_value_range (to, from->type, from->min, from->max, from->equiv);
614 /* Set value range VR to a single value. This function is only called
615 with values we get from statements, and exists to clear the
616 TREE_OVERFLOW flag so that we don't think we have an overflow
617 infinity when we shouldn't. */
619 static inline void
620 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
622 gcc_assert (is_gimple_min_invariant (val));
623 if (TREE_OVERFLOW_P (val))
624 val = drop_tree_overflow (val);
625 set_value_range (vr, VR_RANGE, val, val, equiv);
628 /* Set value range VR to a non-negative range of type TYPE.
629 OVERFLOW_INFINITY indicates whether to use an overflow infinity
630 rather than TYPE_MAX_VALUE; this should be true if we determine
631 that the range is nonnegative based on the assumption that signed
632 overflow does not occur. */
634 static inline void
635 set_value_range_to_nonnegative (value_range_t *vr, tree type,
636 bool overflow_infinity)
638 tree zero;
640 if (overflow_infinity && !supports_overflow_infinity (type))
642 set_value_range_to_varying (vr);
643 return;
646 zero = build_int_cst (type, 0);
647 set_value_range (vr, VR_RANGE, zero,
648 (overflow_infinity
649 ? positive_overflow_infinity (type)
650 : TYPE_MAX_VALUE (type)),
651 vr->equiv);
654 /* Set value range VR to a non-NULL range of type TYPE. */
656 static inline void
657 set_value_range_to_nonnull (value_range_t *vr, tree type)
659 tree zero = build_int_cst (type, 0);
660 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
664 /* Set value range VR to a NULL range of type TYPE. */
666 static inline void
667 set_value_range_to_null (value_range_t *vr, tree type)
669 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
673 /* Set value range VR to a range of a truthvalue of type TYPE. */
675 static inline void
676 set_value_range_to_truthvalue (value_range_t *vr, tree type)
678 if (TYPE_PRECISION (type) == 1)
679 set_value_range_to_varying (vr);
680 else
681 set_value_range (vr, VR_RANGE,
682 build_int_cst (type, 0), build_int_cst (type, 1),
683 vr->equiv);
687 /* If abs (min) < abs (max), set VR to [-max, max], if
688 abs (min) >= abs (max), set VR to [-min, min]. */
690 static void
691 abs_extent_range (value_range_t *vr, tree min, tree max)
693 int cmp;
695 gcc_assert (TREE_CODE (min) == INTEGER_CST);
696 gcc_assert (TREE_CODE (max) == INTEGER_CST);
697 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
698 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
699 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
700 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
701 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
703 set_value_range_to_varying (vr);
704 return;
706 cmp = compare_values (min, max);
707 if (cmp == -1)
708 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
709 else if (cmp == 0 || cmp == 1)
711 max = min;
712 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
714 else
716 set_value_range_to_varying (vr);
717 return;
719 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
723 /* Return value range information for VAR.
725 If we have no values ranges recorded (ie, VRP is not running), then
726 return NULL. Otherwise create an empty range if none existed for VAR. */
728 static value_range_t *
729 get_value_range (const_tree var)
731 static const struct value_range_d vr_const_varying
732 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
733 value_range_t *vr;
734 tree sym;
735 unsigned ver = SSA_NAME_VERSION (var);
737 /* If we have no recorded ranges, then return NULL. */
738 if (! vr_value)
739 return NULL;
741 /* If we query the range for a new SSA name return an unmodifiable VARYING.
742 We should get here at most from the substitute-and-fold stage which
743 will never try to change values. */
744 if (ver >= num_vr_values)
745 return CONST_CAST (value_range_t *, &vr_const_varying);
747 vr = vr_value[ver];
748 if (vr)
749 return vr;
751 /* After propagation finished do not allocate new value-ranges. */
752 if (values_propagated)
753 return CONST_CAST (value_range_t *, &vr_const_varying);
755 /* Create a default value range. */
756 vr_value[ver] = vr = XCNEW (value_range_t);
758 /* Defer allocating the equivalence set. */
759 vr->equiv = NULL;
761 /* If VAR is a default definition of a parameter, the variable can
762 take any value in VAR's type. */
763 if (SSA_NAME_IS_DEFAULT_DEF (var))
765 sym = SSA_NAME_VAR (var);
766 if (TREE_CODE (sym) == PARM_DECL)
768 /* Try to use the "nonnull" attribute to create ~[0, 0]
769 anti-ranges for pointers. Note that this is only valid with
770 default definitions of PARM_DECLs. */
771 if (POINTER_TYPE_P (TREE_TYPE (sym))
772 && nonnull_arg_p (sym))
773 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
774 else
775 set_value_range_to_varying (vr);
777 else if (TREE_CODE (sym) == RESULT_DECL
778 && DECL_BY_REFERENCE (sym))
779 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
782 return vr;
785 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
787 static inline bool
788 vrp_operand_equal_p (const_tree val1, const_tree val2)
790 if (val1 == val2)
791 return true;
792 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
793 return false;
794 if (is_overflow_infinity (val1))
795 return is_overflow_infinity (val2);
796 return true;
799 /* Return true, if the bitmaps B1 and B2 are equal. */
801 static inline bool
802 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
804 return (b1 == b2
805 || ((!b1 || bitmap_empty_p (b1))
806 && (!b2 || bitmap_empty_p (b2)))
807 || (b1 && b2
808 && bitmap_equal_p (b1, b2)));
811 /* Update the value range and equivalence set for variable VAR to
812 NEW_VR. Return true if NEW_VR is different from VAR's previous
813 value.
815 NOTE: This function assumes that NEW_VR is a temporary value range
816 object created for the sole purpose of updating VAR's range. The
817 storage used by the equivalence set from NEW_VR will be freed by
818 this function. Do not call update_value_range when NEW_VR
819 is the range object associated with another SSA name. */
821 static inline bool
822 update_value_range (const_tree var, value_range_t *new_vr)
824 value_range_t *old_vr;
825 bool is_new;
827 /* Update the value range, if necessary. */
828 old_vr = get_value_range (var);
829 is_new = old_vr->type != new_vr->type
830 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
831 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
832 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
834 if (is_new)
836 /* Do not allow transitions up the lattice. The following
837 is slightly more awkward than just new_vr->type < old_vr->type
838 because VR_RANGE and VR_ANTI_RANGE need to be considered
839 the same. We may not have is_new when transitioning to
840 UNDEFINED or from VARYING. */
841 if (new_vr->type == VR_UNDEFINED
842 || old_vr->type == VR_VARYING)
843 set_value_range_to_varying (old_vr);
844 else
845 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
846 new_vr->equiv);
849 BITMAP_FREE (new_vr->equiv);
851 return is_new;
855 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
856 point where equivalence processing can be turned on/off. */
858 static void
859 add_equivalence (bitmap *equiv, const_tree var)
861 unsigned ver = SSA_NAME_VERSION (var);
862 value_range_t *vr = vr_value[ver];
864 if (*equiv == NULL)
865 *equiv = BITMAP_ALLOC (NULL);
866 bitmap_set_bit (*equiv, ver);
867 if (vr && vr->equiv)
868 bitmap_ior_into (*equiv, vr->equiv);
872 /* Return true if VR is ~[0, 0]. */
874 static inline bool
875 range_is_nonnull (value_range_t *vr)
877 return vr->type == VR_ANTI_RANGE
878 && integer_zerop (vr->min)
879 && integer_zerop (vr->max);
883 /* Return true if VR is [0, 0]. */
885 static inline bool
886 range_is_null (value_range_t *vr)
888 return vr->type == VR_RANGE
889 && integer_zerop (vr->min)
890 && integer_zerop (vr->max);
893 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
894 a singleton. */
896 static inline bool
897 range_int_cst_p (value_range_t *vr)
899 return (vr->type == VR_RANGE
900 && TREE_CODE (vr->max) == INTEGER_CST
901 && TREE_CODE (vr->min) == INTEGER_CST);
904 /* Return true if VR is a INTEGER_CST singleton. */
906 static inline bool
907 range_int_cst_singleton_p (value_range_t *vr)
909 return (range_int_cst_p (vr)
910 && !is_overflow_infinity (vr->min)
911 && !is_overflow_infinity (vr->max)
912 && tree_int_cst_equal (vr->min, vr->max));
915 /* Return true if value range VR involves at least one symbol. */
917 static inline bool
918 symbolic_range_p (value_range_t *vr)
920 return (!is_gimple_min_invariant (vr->min)
921 || !is_gimple_min_invariant (vr->max));
924 /* Return true if value range VR uses an overflow infinity. */
926 static inline bool
927 overflow_infinity_range_p (value_range_t *vr)
929 return (vr->type == VR_RANGE
930 && (is_overflow_infinity (vr->min)
931 || is_overflow_infinity (vr->max)));
934 /* Return false if we can not make a valid comparison based on VR;
935 this will be the case if it uses an overflow infinity and overflow
936 is not undefined (i.e., -fno-strict-overflow is in effect).
937 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
938 uses an overflow infinity. */
940 static bool
941 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
943 gcc_assert (vr->type == VR_RANGE);
944 if (is_overflow_infinity (vr->min))
946 *strict_overflow_p = true;
947 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
948 return false;
950 if (is_overflow_infinity (vr->max))
952 *strict_overflow_p = true;
953 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
954 return false;
956 return true;
960 /* Return true if the result of assignment STMT is know to be non-negative.
961 If the return value is based on the assumption that signed overflow is
962 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
963 *STRICT_OVERFLOW_P.*/
965 static bool
966 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
968 enum tree_code code = gimple_assign_rhs_code (stmt);
969 switch (get_gimple_rhs_class (code))
971 case GIMPLE_UNARY_RHS:
972 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
973 gimple_expr_type (stmt),
974 gimple_assign_rhs1 (stmt),
975 strict_overflow_p);
976 case GIMPLE_BINARY_RHS:
977 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
978 gimple_expr_type (stmt),
979 gimple_assign_rhs1 (stmt),
980 gimple_assign_rhs2 (stmt),
981 strict_overflow_p);
982 case GIMPLE_TERNARY_RHS:
983 return false;
984 case GIMPLE_SINGLE_RHS:
985 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
986 strict_overflow_p);
987 case GIMPLE_INVALID_RHS:
988 gcc_unreachable ();
989 default:
990 gcc_unreachable ();
994 /* Return true if return value of call STMT is know to be non-negative.
995 If the return value is based on the assumption that signed overflow is
996 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
997 *STRICT_OVERFLOW_P.*/
999 static bool
1000 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1002 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1003 gimple_call_arg (stmt, 0) : NULL_TREE;
1004 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1005 gimple_call_arg (stmt, 1) : NULL_TREE;
1007 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1008 gimple_call_fndecl (stmt),
1009 arg0,
1010 arg1,
1011 strict_overflow_p);
1014 /* Return true if STMT is know to to compute a non-negative value.
1015 If the return value is based on the assumption that signed overflow is
1016 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1017 *STRICT_OVERFLOW_P.*/
1019 static bool
1020 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1022 switch (gimple_code (stmt))
1024 case GIMPLE_ASSIGN:
1025 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1026 case GIMPLE_CALL:
1027 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1028 default:
1029 gcc_unreachable ();
1033 /* Return true if the result of assignment STMT is know to be non-zero.
1034 If the return value is based on the assumption that signed overflow is
1035 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1036 *STRICT_OVERFLOW_P.*/
1038 static bool
1039 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1041 enum tree_code code = gimple_assign_rhs_code (stmt);
1042 switch (get_gimple_rhs_class (code))
1044 case GIMPLE_UNARY_RHS:
1045 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1046 gimple_expr_type (stmt),
1047 gimple_assign_rhs1 (stmt),
1048 strict_overflow_p);
1049 case GIMPLE_BINARY_RHS:
1050 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1051 gimple_expr_type (stmt),
1052 gimple_assign_rhs1 (stmt),
1053 gimple_assign_rhs2 (stmt),
1054 strict_overflow_p);
1055 case GIMPLE_TERNARY_RHS:
1056 return false;
1057 case GIMPLE_SINGLE_RHS:
1058 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1059 strict_overflow_p);
1060 case GIMPLE_INVALID_RHS:
1061 gcc_unreachable ();
1062 default:
1063 gcc_unreachable ();
1067 /* Return true if STMT is known to compute a non-zero value.
1068 If the return value is based on the assumption that signed overflow is
1069 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1070 *STRICT_OVERFLOW_P.*/
1072 static bool
1073 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1075 switch (gimple_code (stmt))
1077 case GIMPLE_ASSIGN:
1078 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1079 case GIMPLE_CALL:
1081 tree fndecl = gimple_call_fndecl (stmt);
1082 if (!fndecl) return false;
1083 if (flag_delete_null_pointer_checks && !flag_check_new
1084 && DECL_IS_OPERATOR_NEW (fndecl)
1085 && !TREE_NOTHROW (fndecl))
1086 return true;
1087 if (flag_delete_null_pointer_checks &&
1088 lookup_attribute ("returns_nonnull",
1089 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1090 return true;
1091 return gimple_alloca_call_p (stmt);
1093 default:
1094 gcc_unreachable ();
1098 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1099 obtained so far. */
1101 static bool
1102 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1104 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1105 return true;
1107 /* If we have an expression of the form &X->a, then the expression
1108 is nonnull if X is nonnull. */
1109 if (is_gimple_assign (stmt)
1110 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1112 tree expr = gimple_assign_rhs1 (stmt);
1113 tree base = get_base_address (TREE_OPERAND (expr, 0));
1115 if (base != NULL_TREE
1116 && TREE_CODE (base) == MEM_REF
1117 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1119 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1120 if (range_is_nonnull (vr))
1121 return true;
1125 return false;
1128 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1129 a gimple invariant, or SSA_NAME +- CST. */
1131 static bool
1132 valid_value_p (tree expr)
1134 if (TREE_CODE (expr) == SSA_NAME)
1135 return true;
1137 if (TREE_CODE (expr) == PLUS_EXPR
1138 || TREE_CODE (expr) == MINUS_EXPR)
1139 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1140 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1142 return is_gimple_min_invariant (expr);
1145 /* Return
1146 1 if VAL < VAL2
1147 0 if !(VAL < VAL2)
1148 -2 if those are incomparable. */
1149 static inline int
1150 operand_less_p (tree val, tree val2)
1152 /* LT is folded faster than GE and others. Inline the common case. */
1153 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1154 return tree_int_cst_lt (val, val2);
1155 else
1157 tree tcmp;
1159 fold_defer_overflow_warnings ();
1161 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1163 fold_undefer_and_ignore_overflow_warnings ();
1165 if (!tcmp
1166 || TREE_CODE (tcmp) != INTEGER_CST)
1167 return -2;
1169 if (!integer_zerop (tcmp))
1170 return 1;
1173 /* val >= val2, not considering overflow infinity. */
1174 if (is_negative_overflow_infinity (val))
1175 return is_negative_overflow_infinity (val2) ? 0 : 1;
1176 else if (is_positive_overflow_infinity (val2))
1177 return is_positive_overflow_infinity (val) ? 0 : 1;
1179 return 0;
1182 /* Compare two values VAL1 and VAL2. Return
1184 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1185 -1 if VAL1 < VAL2,
1186 0 if VAL1 == VAL2,
1187 +1 if VAL1 > VAL2, and
1188 +2 if VAL1 != VAL2
1190 This is similar to tree_int_cst_compare but supports pointer values
1191 and values that cannot be compared at compile time.
1193 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1194 true if the return value is only valid if we assume that signed
1195 overflow is undefined. */
1197 static int
1198 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1200 if (val1 == val2)
1201 return 0;
1203 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1204 both integers. */
1205 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1206 == POINTER_TYPE_P (TREE_TYPE (val2)));
1207 /* Convert the two values into the same type. This is needed because
1208 sizetype causes sign extension even for unsigned types. */
1209 val2 = fold_convert (TREE_TYPE (val1), val2);
1210 STRIP_USELESS_TYPE_CONVERSION (val2);
1212 if ((TREE_CODE (val1) == SSA_NAME
1213 || TREE_CODE (val1) == PLUS_EXPR
1214 || TREE_CODE (val1) == MINUS_EXPR)
1215 && (TREE_CODE (val2) == SSA_NAME
1216 || TREE_CODE (val2) == PLUS_EXPR
1217 || TREE_CODE (val2) == MINUS_EXPR))
1219 tree n1, c1, n2, c2;
1220 enum tree_code code1, code2;
1222 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1223 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1224 same name, return -2. */
1225 if (TREE_CODE (val1) == SSA_NAME)
1227 code1 = SSA_NAME;
1228 n1 = val1;
1229 c1 = NULL_TREE;
1231 else
1233 code1 = TREE_CODE (val1);
1234 n1 = TREE_OPERAND (val1, 0);
1235 c1 = TREE_OPERAND (val1, 1);
1236 if (tree_int_cst_sgn (c1) == -1)
1238 if (is_negative_overflow_infinity (c1))
1239 return -2;
1240 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1241 if (!c1)
1242 return -2;
1243 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1247 if (TREE_CODE (val2) == SSA_NAME)
1249 code2 = SSA_NAME;
1250 n2 = val2;
1251 c2 = NULL_TREE;
1253 else
1255 code2 = TREE_CODE (val2);
1256 n2 = TREE_OPERAND (val2, 0);
1257 c2 = TREE_OPERAND (val2, 1);
1258 if (tree_int_cst_sgn (c2) == -1)
1260 if (is_negative_overflow_infinity (c2))
1261 return -2;
1262 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1263 if (!c2)
1264 return -2;
1265 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1269 /* Both values must use the same name. */
1270 if (n1 != n2)
1271 return -2;
1273 if (code1 == SSA_NAME
1274 && code2 == SSA_NAME)
1275 /* NAME == NAME */
1276 return 0;
1278 /* If overflow is defined we cannot simplify more. */
1279 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1280 return -2;
1282 if (strict_overflow_p != NULL
1283 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1284 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1285 *strict_overflow_p = true;
1287 if (code1 == SSA_NAME)
1289 if (code2 == PLUS_EXPR)
1290 /* NAME < NAME + CST */
1291 return -1;
1292 else if (code2 == MINUS_EXPR)
1293 /* NAME > NAME - CST */
1294 return 1;
1296 else if (code1 == PLUS_EXPR)
1298 if (code2 == SSA_NAME)
1299 /* NAME + CST > NAME */
1300 return 1;
1301 else if (code2 == PLUS_EXPR)
1302 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1303 return compare_values_warnv (c1, c2, strict_overflow_p);
1304 else if (code2 == MINUS_EXPR)
1305 /* NAME + CST1 > NAME - CST2 */
1306 return 1;
1308 else if (code1 == MINUS_EXPR)
1310 if (code2 == SSA_NAME)
1311 /* NAME - CST < NAME */
1312 return -1;
1313 else if (code2 == PLUS_EXPR)
1314 /* NAME - CST1 < NAME + CST2 */
1315 return -1;
1316 else if (code2 == MINUS_EXPR)
1317 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1318 C1 and C2 are swapped in the call to compare_values. */
1319 return compare_values_warnv (c2, c1, strict_overflow_p);
1322 gcc_unreachable ();
1325 /* We cannot compare non-constants. */
1326 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1327 return -2;
1329 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1331 /* We cannot compare overflowed values, except for overflow
1332 infinities. */
1333 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1335 if (strict_overflow_p != NULL)
1336 *strict_overflow_p = true;
1337 if (is_negative_overflow_infinity (val1))
1338 return is_negative_overflow_infinity (val2) ? 0 : -1;
1339 else if (is_negative_overflow_infinity (val2))
1340 return 1;
1341 else if (is_positive_overflow_infinity (val1))
1342 return is_positive_overflow_infinity (val2) ? 0 : 1;
1343 else if (is_positive_overflow_infinity (val2))
1344 return -1;
1345 return -2;
1348 return tree_int_cst_compare (val1, val2);
1350 else
1352 tree t;
1354 /* First see if VAL1 and VAL2 are not the same. */
1355 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1356 return 0;
1358 /* If VAL1 is a lower address than VAL2, return -1. */
1359 if (operand_less_p (val1, val2) == 1)
1360 return -1;
1362 /* If VAL1 is a higher address than VAL2, return +1. */
1363 if (operand_less_p (val2, val1) == 1)
1364 return 1;
1366 /* If VAL1 is different than VAL2, return +2.
1367 For integer constants we either have already returned -1 or 1
1368 or they are equivalent. We still might succeed in proving
1369 something about non-trivial operands. */
1370 if (TREE_CODE (val1) != INTEGER_CST
1371 || TREE_CODE (val2) != INTEGER_CST)
1373 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1374 if (t && integer_onep (t))
1375 return 2;
1378 return -2;
1382 /* Compare values like compare_values_warnv, but treat comparisons of
1383 nonconstants which rely on undefined overflow as incomparable. */
1385 static int
1386 compare_values (tree val1, tree val2)
1388 bool sop;
1389 int ret;
1391 sop = false;
1392 ret = compare_values_warnv (val1, val2, &sop);
1393 if (sop
1394 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1395 ret = -2;
1396 return ret;
1400 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1401 0 if VAL is not inside [MIN, MAX],
1402 -2 if we cannot tell either way.
1404 Benchmark compile/20001226-1.c compilation time after changing this
1405 function. */
1407 static inline int
1408 value_inside_range (tree val, tree min, tree max)
1410 int cmp1, cmp2;
1412 cmp1 = operand_less_p (val, min);
1413 if (cmp1 == -2)
1414 return -2;
1415 if (cmp1 == 1)
1416 return 0;
1418 cmp2 = operand_less_p (max, val);
1419 if (cmp2 == -2)
1420 return -2;
1422 return !cmp2;
1426 /* Return true if value ranges VR0 and VR1 have a non-empty
1427 intersection.
1429 Benchmark compile/20001226-1.c compilation time after changing this
1430 function.
1433 static inline bool
1434 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1436 /* The value ranges do not intersect if the maximum of the first range is
1437 less than the minimum of the second range or vice versa.
1438 When those relations are unknown, we can't do any better. */
1439 if (operand_less_p (vr0->max, vr1->min) != 0)
1440 return false;
1441 if (operand_less_p (vr1->max, vr0->min) != 0)
1442 return false;
1443 return true;
1447 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1448 include the value zero, -2 if we cannot tell. */
1450 static inline int
1451 range_includes_zero_p (tree min, tree max)
1453 tree zero = build_int_cst (TREE_TYPE (min), 0);
1454 return value_inside_range (zero, min, max);
1457 /* Return true if *VR is know to only contain nonnegative values. */
1459 static inline bool
1460 value_range_nonnegative_p (value_range_t *vr)
1462 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1463 which would return a useful value should be encoded as a
1464 VR_RANGE. */
1465 if (vr->type == VR_RANGE)
1467 int result = compare_values (vr->min, integer_zero_node);
1468 return (result == 0 || result == 1);
1471 return false;
1474 /* If *VR has a value rante that is a single constant value return that,
1475 otherwise return NULL_TREE. */
1477 static tree
1478 value_range_constant_singleton (value_range_t *vr)
1480 if (vr->type == VR_RANGE
1481 && operand_equal_p (vr->min, vr->max, 0)
1482 && is_gimple_min_invariant (vr->min))
1483 return vr->min;
1485 return NULL_TREE;
1488 /* If OP has a value range with a single constant value return that,
1489 otherwise return NULL_TREE. This returns OP itself if OP is a
1490 constant. */
1492 static tree
1493 op_with_constant_singleton_value_range (tree op)
1495 if (is_gimple_min_invariant (op))
1496 return op;
1498 if (TREE_CODE (op) != SSA_NAME)
1499 return NULL_TREE;
1501 return value_range_constant_singleton (get_value_range (op));
1504 /* Return true if op is in a boolean [0, 1] value-range. */
1506 static bool
1507 op_with_boolean_value_range_p (tree op)
1509 value_range_t *vr;
1511 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1512 return true;
1514 if (integer_zerop (op)
1515 || integer_onep (op))
1516 return true;
1518 if (TREE_CODE (op) != SSA_NAME)
1519 return false;
1521 vr = get_value_range (op);
1522 return (vr->type == VR_RANGE
1523 && integer_zerop (vr->min)
1524 && integer_onep (vr->max));
1527 /* Extract value range information from an ASSERT_EXPR EXPR and store
1528 it in *VR_P. */
1530 static void
1531 extract_range_from_assert (value_range_t *vr_p, tree expr)
1533 tree var, cond, limit, min, max, type;
1534 value_range_t *limit_vr;
1535 enum tree_code cond_code;
1537 var = ASSERT_EXPR_VAR (expr);
1538 cond = ASSERT_EXPR_COND (expr);
1540 gcc_assert (COMPARISON_CLASS_P (cond));
1542 /* Find VAR in the ASSERT_EXPR conditional. */
1543 if (var == TREE_OPERAND (cond, 0)
1544 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1545 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1547 /* If the predicate is of the form VAR COMP LIMIT, then we just
1548 take LIMIT from the RHS and use the same comparison code. */
1549 cond_code = TREE_CODE (cond);
1550 limit = TREE_OPERAND (cond, 1);
1551 cond = TREE_OPERAND (cond, 0);
1553 else
1555 /* If the predicate is of the form LIMIT COMP VAR, then we need
1556 to flip around the comparison code to create the proper range
1557 for VAR. */
1558 cond_code = swap_tree_comparison (TREE_CODE (cond));
1559 limit = TREE_OPERAND (cond, 0);
1560 cond = TREE_OPERAND (cond, 1);
1563 limit = avoid_overflow_infinity (limit);
1565 type = TREE_TYPE (var);
1566 gcc_assert (limit != var);
1568 /* For pointer arithmetic, we only keep track of pointer equality
1569 and inequality. */
1570 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1572 set_value_range_to_varying (vr_p);
1573 return;
1576 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1577 try to use LIMIT's range to avoid creating symbolic ranges
1578 unnecessarily. */
1579 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1581 /* LIMIT's range is only interesting if it has any useful information. */
1582 if (limit_vr
1583 && (limit_vr->type == VR_UNDEFINED
1584 || limit_vr->type == VR_VARYING
1585 || symbolic_range_p (limit_vr)))
1586 limit_vr = NULL;
1588 /* Initially, the new range has the same set of equivalences of
1589 VAR's range. This will be revised before returning the final
1590 value. Since assertions may be chained via mutually exclusive
1591 predicates, we will need to trim the set of equivalences before
1592 we are done. */
1593 gcc_assert (vr_p->equiv == NULL);
1594 add_equivalence (&vr_p->equiv, var);
1596 /* Extract a new range based on the asserted comparison for VAR and
1597 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1598 will only use it for equality comparisons (EQ_EXPR). For any
1599 other kind of assertion, we cannot derive a range from LIMIT's
1600 anti-range that can be used to describe the new range. For
1601 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1602 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1603 no single range for x_2 that could describe LE_EXPR, so we might
1604 as well build the range [b_4, +INF] for it.
1605 One special case we handle is extracting a range from a
1606 range test encoded as (unsigned)var + CST <= limit. */
1607 if (TREE_CODE (cond) == NOP_EXPR
1608 || TREE_CODE (cond) == PLUS_EXPR)
1610 if (TREE_CODE (cond) == PLUS_EXPR)
1612 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1613 TREE_OPERAND (cond, 1));
1614 max = int_const_binop (PLUS_EXPR, limit, min);
1615 cond = TREE_OPERAND (cond, 0);
1617 else
1619 min = build_int_cst (TREE_TYPE (var), 0);
1620 max = limit;
1623 /* Make sure to not set TREE_OVERFLOW on the final type
1624 conversion. We are willingly interpreting large positive
1625 unsigned values as negative singed values here. */
1626 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1627 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1629 /* We can transform a max, min range to an anti-range or
1630 vice-versa. Use set_and_canonicalize_value_range which does
1631 this for us. */
1632 if (cond_code == LE_EXPR)
1633 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1634 min, max, vr_p->equiv);
1635 else if (cond_code == GT_EXPR)
1636 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1637 min, max, vr_p->equiv);
1638 else
1639 gcc_unreachable ();
1641 else if (cond_code == EQ_EXPR)
1643 enum value_range_type range_type;
1645 if (limit_vr)
1647 range_type = limit_vr->type;
1648 min = limit_vr->min;
1649 max = limit_vr->max;
1651 else
1653 range_type = VR_RANGE;
1654 min = limit;
1655 max = limit;
1658 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1660 /* When asserting the equality VAR == LIMIT and LIMIT is another
1661 SSA name, the new range will also inherit the equivalence set
1662 from LIMIT. */
1663 if (TREE_CODE (limit) == SSA_NAME)
1664 add_equivalence (&vr_p->equiv, limit);
1666 else if (cond_code == NE_EXPR)
1668 /* As described above, when LIMIT's range is an anti-range and
1669 this assertion is an inequality (NE_EXPR), then we cannot
1670 derive anything from the anti-range. For instance, if
1671 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1672 not imply that VAR's range is [0, 0]. So, in the case of
1673 anti-ranges, we just assert the inequality using LIMIT and
1674 not its anti-range.
1676 If LIMIT_VR is a range, we can only use it to build a new
1677 anti-range if LIMIT_VR is a single-valued range. For
1678 instance, if LIMIT_VR is [0, 1], the predicate
1679 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1680 Rather, it means that for value 0 VAR should be ~[0, 0]
1681 and for value 1, VAR should be ~[1, 1]. We cannot
1682 represent these ranges.
1684 The only situation in which we can build a valid
1685 anti-range is when LIMIT_VR is a single-valued range
1686 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1687 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1688 if (limit_vr
1689 && limit_vr->type == VR_RANGE
1690 && compare_values (limit_vr->min, limit_vr->max) == 0)
1692 min = limit_vr->min;
1693 max = limit_vr->max;
1695 else
1697 /* In any other case, we cannot use LIMIT's range to build a
1698 valid anti-range. */
1699 min = max = limit;
1702 /* If MIN and MAX cover the whole range for their type, then
1703 just use the original LIMIT. */
1704 if (INTEGRAL_TYPE_P (type)
1705 && vrp_val_is_min (min)
1706 && vrp_val_is_max (max))
1707 min = max = limit;
1709 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1710 min, max, vr_p->equiv);
1712 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1714 min = TYPE_MIN_VALUE (type);
1716 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1717 max = limit;
1718 else
1720 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1721 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1722 LT_EXPR. */
1723 max = limit_vr->max;
1726 /* If the maximum value forces us to be out of bounds, simply punt.
1727 It would be pointless to try and do anything more since this
1728 all should be optimized away above us. */
1729 if ((cond_code == LT_EXPR
1730 && compare_values (max, min) == 0)
1731 || is_overflow_infinity (max))
1732 set_value_range_to_varying (vr_p);
1733 else
1735 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1736 if (cond_code == LT_EXPR)
1738 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1739 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1740 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1741 build_int_cst (TREE_TYPE (max), -1));
1742 else
1743 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1744 build_int_cst (TREE_TYPE (max), 1));
1745 if (EXPR_P (max))
1746 TREE_NO_WARNING (max) = 1;
1749 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1752 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1754 max = TYPE_MAX_VALUE (type);
1756 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1757 min = limit;
1758 else
1760 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1761 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1762 GT_EXPR. */
1763 min = limit_vr->min;
1766 /* If the minimum value forces us to be out of bounds, simply punt.
1767 It would be pointless to try and do anything more since this
1768 all should be optimized away above us. */
1769 if ((cond_code == GT_EXPR
1770 && compare_values (min, max) == 0)
1771 || is_overflow_infinity (min))
1772 set_value_range_to_varying (vr_p);
1773 else
1775 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1776 if (cond_code == GT_EXPR)
1778 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1779 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1780 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1781 build_int_cst (TREE_TYPE (min), -1));
1782 else
1783 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1784 build_int_cst (TREE_TYPE (min), 1));
1785 if (EXPR_P (min))
1786 TREE_NO_WARNING (min) = 1;
1789 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1792 else
1793 gcc_unreachable ();
1795 /* Finally intersect the new range with what we already know about var. */
1796 vrp_intersect_ranges (vr_p, get_value_range (var));
1800 /* Extract range information from SSA name VAR and store it in VR. If
1801 VAR has an interesting range, use it. Otherwise, create the
1802 range [VAR, VAR] and return it. This is useful in situations where
1803 we may have conditionals testing values of VARYING names. For
1804 instance,
1806 x_3 = y_5;
1807 if (x_3 > y_5)
1810 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1811 always false. */
1813 static void
1814 extract_range_from_ssa_name (value_range_t *vr, tree var)
1816 value_range_t *var_vr = get_value_range (var);
1818 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1819 copy_value_range (vr, var_vr);
1820 else
1821 set_value_range (vr, VR_RANGE, var, var, NULL);
1823 add_equivalence (&vr->equiv, var);
1827 /* Wrapper around int_const_binop. If the operation overflows and we
1828 are not using wrapping arithmetic, then adjust the result to be
1829 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1830 NULL_TREE if we need to use an overflow infinity representation but
1831 the type does not support it. */
1833 static tree
1834 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1836 tree res;
1838 res = int_const_binop (code, val1, val2);
1840 /* If we are using unsigned arithmetic, operate symbolically
1841 on -INF and +INF as int_const_binop only handles signed overflow. */
1842 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1844 int checkz = compare_values (res, val1);
1845 bool overflow = false;
1847 /* Ensure that res = val1 [+*] val2 >= val1
1848 or that res = val1 - val2 <= val1. */
1849 if ((code == PLUS_EXPR
1850 && !(checkz == 1 || checkz == 0))
1851 || (code == MINUS_EXPR
1852 && !(checkz == 0 || checkz == -1)))
1854 overflow = true;
1856 /* Checking for multiplication overflow is done by dividing the
1857 output of the multiplication by the first input of the
1858 multiplication. If the result of that division operation is
1859 not equal to the second input of the multiplication, then the
1860 multiplication overflowed. */
1861 else if (code == MULT_EXPR && !integer_zerop (val1))
1863 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1864 res,
1865 val1);
1866 int check = compare_values (tmp, val2);
1868 if (check != 0)
1869 overflow = true;
1872 if (overflow)
1874 res = copy_node (res);
1875 TREE_OVERFLOW (res) = 1;
1879 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1880 /* If the singed operation wraps then int_const_binop has done
1881 everything we want. */
1883 /* Signed division of -1/0 overflows and by the time it gets here
1884 returns NULL_TREE. */
1885 else if (!res)
1886 return NULL_TREE;
1887 else if ((TREE_OVERFLOW (res)
1888 && !TREE_OVERFLOW (val1)
1889 && !TREE_OVERFLOW (val2))
1890 || is_overflow_infinity (val1)
1891 || is_overflow_infinity (val2))
1893 /* If the operation overflowed but neither VAL1 nor VAL2 are
1894 overflown, return -INF or +INF depending on the operation
1895 and the combination of signs of the operands. */
1896 int sgn1 = tree_int_cst_sgn (val1);
1897 int sgn2 = tree_int_cst_sgn (val2);
1899 if (needs_overflow_infinity (TREE_TYPE (res))
1900 && !supports_overflow_infinity (TREE_TYPE (res)))
1901 return NULL_TREE;
1903 /* We have to punt on adding infinities of different signs,
1904 since we can't tell what the sign of the result should be.
1905 Likewise for subtracting infinities of the same sign. */
1906 if (((code == PLUS_EXPR && sgn1 != sgn2)
1907 || (code == MINUS_EXPR && sgn1 == sgn2))
1908 && is_overflow_infinity (val1)
1909 && is_overflow_infinity (val2))
1910 return NULL_TREE;
1912 /* Don't try to handle division or shifting of infinities. */
1913 if ((code == TRUNC_DIV_EXPR
1914 || code == FLOOR_DIV_EXPR
1915 || code == CEIL_DIV_EXPR
1916 || code == EXACT_DIV_EXPR
1917 || code == ROUND_DIV_EXPR
1918 || code == RSHIFT_EXPR)
1919 && (is_overflow_infinity (val1)
1920 || is_overflow_infinity (val2)))
1921 return NULL_TREE;
1923 /* Notice that we only need to handle the restricted set of
1924 operations handled by extract_range_from_binary_expr.
1925 Among them, only multiplication, addition and subtraction
1926 can yield overflow without overflown operands because we
1927 are working with integral types only... except in the
1928 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1929 for division too. */
1931 /* For multiplication, the sign of the overflow is given
1932 by the comparison of the signs of the operands. */
1933 if ((code == MULT_EXPR && sgn1 == sgn2)
1934 /* For addition, the operands must be of the same sign
1935 to yield an overflow. Its sign is therefore that
1936 of one of the operands, for example the first. For
1937 infinite operands X + -INF is negative, not positive. */
1938 || (code == PLUS_EXPR
1939 && (sgn1 >= 0
1940 ? !is_negative_overflow_infinity (val2)
1941 : is_positive_overflow_infinity (val2)))
1942 /* For subtraction, non-infinite operands must be of
1943 different signs to yield an overflow. Its sign is
1944 therefore that of the first operand or the opposite of
1945 that of the second operand. A first operand of 0 counts
1946 as positive here, for the corner case 0 - (-INF), which
1947 overflows, but must yield +INF. For infinite operands 0
1948 - INF is negative, not positive. */
1949 || (code == MINUS_EXPR
1950 && (sgn1 >= 0
1951 ? !is_positive_overflow_infinity (val2)
1952 : is_negative_overflow_infinity (val2)))
1953 /* We only get in here with positive shift count, so the
1954 overflow direction is the same as the sign of val1.
1955 Actually rshift does not overflow at all, but we only
1956 handle the case of shifting overflowed -INF and +INF. */
1957 || (code == RSHIFT_EXPR
1958 && sgn1 >= 0)
1959 /* For division, the only case is -INF / -1 = +INF. */
1960 || code == TRUNC_DIV_EXPR
1961 || code == FLOOR_DIV_EXPR
1962 || code == CEIL_DIV_EXPR
1963 || code == EXACT_DIV_EXPR
1964 || code == ROUND_DIV_EXPR)
1965 return (needs_overflow_infinity (TREE_TYPE (res))
1966 ? positive_overflow_infinity (TREE_TYPE (res))
1967 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1968 else
1969 return (needs_overflow_infinity (TREE_TYPE (res))
1970 ? negative_overflow_infinity (TREE_TYPE (res))
1971 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1974 return res;
1978 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1979 bitmask if some bit is unset, it means for all numbers in the range
1980 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1981 bitmask if some bit is set, it means for all numbers in the range
1982 the bit is 1, otherwise it might be 0 or 1. */
1984 static bool
1985 zero_nonzero_bits_from_vr (const tree expr_type,
1986 value_range_t *vr,
1987 wide_int *may_be_nonzero,
1988 wide_int *must_be_nonzero)
1990 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1991 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1992 if (!range_int_cst_p (vr)
1993 || is_overflow_infinity (vr->min)
1994 || is_overflow_infinity (vr->max))
1995 return false;
1997 if (range_int_cst_singleton_p (vr))
1999 *may_be_nonzero = vr->min;
2000 *must_be_nonzero = *may_be_nonzero;
2002 else if (tree_int_cst_sgn (vr->min) >= 0
2003 || tree_int_cst_sgn (vr->max) < 0)
2005 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2006 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2007 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2008 if (xor_mask != 0)
2010 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2011 may_be_nonzero->get_precision ());
2012 *may_be_nonzero = *may_be_nonzero | mask;
2013 *must_be_nonzero = must_be_nonzero->and_not (mask);
2017 return true;
2020 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2021 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2022 false otherwise. If *AR can be represented with a single range
2023 *VR1 will be VR_UNDEFINED. */
2025 static bool
2026 ranges_from_anti_range (value_range_t *ar,
2027 value_range_t *vr0, value_range_t *vr1)
2029 tree type = TREE_TYPE (ar->min);
2031 vr0->type = VR_UNDEFINED;
2032 vr1->type = VR_UNDEFINED;
2034 if (ar->type != VR_ANTI_RANGE
2035 || TREE_CODE (ar->min) != INTEGER_CST
2036 || TREE_CODE (ar->max) != INTEGER_CST
2037 || !vrp_val_min (type)
2038 || !vrp_val_max (type))
2039 return false;
2041 if (!vrp_val_is_min (ar->min))
2043 vr0->type = VR_RANGE;
2044 vr0->min = vrp_val_min (type);
2045 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2047 if (!vrp_val_is_max (ar->max))
2049 vr1->type = VR_RANGE;
2050 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2051 vr1->max = vrp_val_max (type);
2053 if (vr0->type == VR_UNDEFINED)
2055 *vr0 = *vr1;
2056 vr1->type = VR_UNDEFINED;
2059 return vr0->type != VR_UNDEFINED;
2062 /* Helper to extract a value-range *VR for a multiplicative operation
2063 *VR0 CODE *VR1. */
2065 static void
2066 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2067 enum tree_code code,
2068 value_range_t *vr0, value_range_t *vr1)
2070 enum value_range_type type;
2071 tree val[4];
2072 size_t i;
2073 tree min, max;
2074 bool sop;
2075 int cmp;
2077 /* Multiplications, divisions and shifts are a bit tricky to handle,
2078 depending on the mix of signs we have in the two ranges, we
2079 need to operate on different values to get the minimum and
2080 maximum values for the new range. One approach is to figure
2081 out all the variations of range combinations and do the
2082 operations.
2084 However, this involves several calls to compare_values and it
2085 is pretty convoluted. It's simpler to do the 4 operations
2086 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2087 MAX1) and then figure the smallest and largest values to form
2088 the new range. */
2089 gcc_assert (code == MULT_EXPR
2090 || code == TRUNC_DIV_EXPR
2091 || code == FLOOR_DIV_EXPR
2092 || code == CEIL_DIV_EXPR
2093 || code == EXACT_DIV_EXPR
2094 || code == ROUND_DIV_EXPR
2095 || code == RSHIFT_EXPR
2096 || code == LSHIFT_EXPR);
2097 gcc_assert ((vr0->type == VR_RANGE
2098 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2099 && vr0->type == vr1->type);
2101 type = vr0->type;
2103 /* Compute the 4 cross operations. */
2104 sop = false;
2105 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2106 if (val[0] == NULL_TREE)
2107 sop = true;
2109 if (vr1->max == vr1->min)
2110 val[1] = NULL_TREE;
2111 else
2113 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2114 if (val[1] == NULL_TREE)
2115 sop = true;
2118 if (vr0->max == vr0->min)
2119 val[2] = NULL_TREE;
2120 else
2122 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2123 if (val[2] == NULL_TREE)
2124 sop = true;
2127 if (vr0->min == vr0->max || vr1->min == vr1->max)
2128 val[3] = NULL_TREE;
2129 else
2131 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2132 if (val[3] == NULL_TREE)
2133 sop = true;
2136 if (sop)
2138 set_value_range_to_varying (vr);
2139 return;
2142 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2143 of VAL[i]. */
2144 min = val[0];
2145 max = val[0];
2146 for (i = 1; i < 4; i++)
2148 if (!is_gimple_min_invariant (min)
2149 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2150 || !is_gimple_min_invariant (max)
2151 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2152 break;
2154 if (val[i])
2156 if (!is_gimple_min_invariant (val[i])
2157 || (TREE_OVERFLOW (val[i])
2158 && !is_overflow_infinity (val[i])))
2160 /* If we found an overflowed value, set MIN and MAX
2161 to it so that we set the resulting range to
2162 VARYING. */
2163 min = max = val[i];
2164 break;
2167 if (compare_values (val[i], min) == -1)
2168 min = val[i];
2170 if (compare_values (val[i], max) == 1)
2171 max = val[i];
2175 /* If either MIN or MAX overflowed, then set the resulting range to
2176 VARYING. But we do accept an overflow infinity
2177 representation. */
2178 if (min == NULL_TREE
2179 || !is_gimple_min_invariant (min)
2180 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2181 || max == NULL_TREE
2182 || !is_gimple_min_invariant (max)
2183 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2185 set_value_range_to_varying (vr);
2186 return;
2189 /* We punt if:
2190 1) [-INF, +INF]
2191 2) [-INF, +-INF(OVF)]
2192 3) [+-INF(OVF), +INF]
2193 4) [+-INF(OVF), +-INF(OVF)]
2194 We learn nothing when we have INF and INF(OVF) on both sides.
2195 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2196 overflow. */
2197 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2198 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2200 set_value_range_to_varying (vr);
2201 return;
2204 cmp = compare_values (min, max);
2205 if (cmp == -2 || cmp == 1)
2207 /* If the new range has its limits swapped around (MIN > MAX),
2208 then the operation caused one of them to wrap around, mark
2209 the new range VARYING. */
2210 set_value_range_to_varying (vr);
2212 else
2213 set_value_range (vr, type, min, max, NULL);
2216 /* Extract range information from a binary operation CODE based on
2217 the ranges of each of its operands, *VR0 and *VR1 with resulting
2218 type EXPR_TYPE. The resulting range is stored in *VR. */
2220 static void
2221 extract_range_from_binary_expr_1 (value_range_t *vr,
2222 enum tree_code code, tree expr_type,
2223 value_range_t *vr0_, value_range_t *vr1_)
2225 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2226 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2227 enum value_range_type type;
2228 tree min = NULL_TREE, max = NULL_TREE;
2229 int cmp;
2231 if (!INTEGRAL_TYPE_P (expr_type)
2232 && !POINTER_TYPE_P (expr_type))
2234 set_value_range_to_varying (vr);
2235 return;
2238 /* Not all binary expressions can be applied to ranges in a
2239 meaningful way. Handle only arithmetic operations. */
2240 if (code != PLUS_EXPR
2241 && code != MINUS_EXPR
2242 && code != POINTER_PLUS_EXPR
2243 && code != MULT_EXPR
2244 && code != TRUNC_DIV_EXPR
2245 && code != FLOOR_DIV_EXPR
2246 && code != CEIL_DIV_EXPR
2247 && code != EXACT_DIV_EXPR
2248 && code != ROUND_DIV_EXPR
2249 && code != TRUNC_MOD_EXPR
2250 && code != RSHIFT_EXPR
2251 && code != LSHIFT_EXPR
2252 && code != MIN_EXPR
2253 && code != MAX_EXPR
2254 && code != BIT_AND_EXPR
2255 && code != BIT_IOR_EXPR
2256 && code != BIT_XOR_EXPR)
2258 set_value_range_to_varying (vr);
2259 return;
2262 /* If both ranges are UNDEFINED, so is the result. */
2263 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2265 set_value_range_to_undefined (vr);
2266 return;
2268 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2269 code. At some point we may want to special-case operations that
2270 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2271 operand. */
2272 else if (vr0.type == VR_UNDEFINED)
2273 set_value_range_to_varying (&vr0);
2274 else if (vr1.type == VR_UNDEFINED)
2275 set_value_range_to_varying (&vr1);
2277 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2278 and express ~[] op X as ([]' op X) U ([]'' op X). */
2279 if (vr0.type == VR_ANTI_RANGE
2280 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2282 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2283 if (vrtem1.type != VR_UNDEFINED)
2285 value_range_t vrres = VR_INITIALIZER;
2286 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2287 &vrtem1, vr1_);
2288 vrp_meet (vr, &vrres);
2290 return;
2292 /* Likewise for X op ~[]. */
2293 if (vr1.type == VR_ANTI_RANGE
2294 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2296 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2297 if (vrtem1.type != VR_UNDEFINED)
2299 value_range_t vrres = VR_INITIALIZER;
2300 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2301 vr0_, &vrtem1);
2302 vrp_meet (vr, &vrres);
2304 return;
2307 /* The type of the resulting value range defaults to VR0.TYPE. */
2308 type = vr0.type;
2310 /* Refuse to operate on VARYING ranges, ranges of different kinds
2311 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2312 because we may be able to derive a useful range even if one of
2313 the operands is VR_VARYING or symbolic range. Similarly for
2314 divisions. TODO, we may be able to derive anti-ranges in
2315 some cases. */
2316 if (code != BIT_AND_EXPR
2317 && code != BIT_IOR_EXPR
2318 && code != TRUNC_DIV_EXPR
2319 && code != FLOOR_DIV_EXPR
2320 && code != CEIL_DIV_EXPR
2321 && code != EXACT_DIV_EXPR
2322 && code != ROUND_DIV_EXPR
2323 && code != TRUNC_MOD_EXPR
2324 && code != MIN_EXPR
2325 && code != MAX_EXPR
2326 && (vr0.type == VR_VARYING
2327 || vr1.type == VR_VARYING
2328 || vr0.type != vr1.type
2329 || symbolic_range_p (&vr0)
2330 || symbolic_range_p (&vr1)))
2332 set_value_range_to_varying (vr);
2333 return;
2336 /* Now evaluate the expression to determine the new range. */
2337 if (POINTER_TYPE_P (expr_type))
2339 if (code == MIN_EXPR || code == MAX_EXPR)
2341 /* For MIN/MAX expressions with pointers, we only care about
2342 nullness, if both are non null, then the result is nonnull.
2343 If both are null, then the result is null. Otherwise they
2344 are varying. */
2345 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2346 set_value_range_to_nonnull (vr, expr_type);
2347 else if (range_is_null (&vr0) && range_is_null (&vr1))
2348 set_value_range_to_null (vr, expr_type);
2349 else
2350 set_value_range_to_varying (vr);
2352 else if (code == POINTER_PLUS_EXPR)
2354 /* For pointer types, we are really only interested in asserting
2355 whether the expression evaluates to non-NULL. */
2356 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2357 set_value_range_to_nonnull (vr, expr_type);
2358 else if (range_is_null (&vr0) && range_is_null (&vr1))
2359 set_value_range_to_null (vr, expr_type);
2360 else
2361 set_value_range_to_varying (vr);
2363 else if (code == BIT_AND_EXPR)
2365 /* For pointer types, we are really only interested in asserting
2366 whether the expression evaluates to non-NULL. */
2367 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2368 set_value_range_to_nonnull (vr, expr_type);
2369 else if (range_is_null (&vr0) || range_is_null (&vr1))
2370 set_value_range_to_null (vr, expr_type);
2371 else
2372 set_value_range_to_varying (vr);
2374 else
2375 set_value_range_to_varying (vr);
2377 return;
2380 /* For integer ranges, apply the operation to each end of the
2381 range and see what we end up with. */
2382 if (code == PLUS_EXPR || code == MINUS_EXPR)
2384 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2385 ranges compute the precise range for such case if possible. */
2386 if (range_int_cst_p (&vr0)
2387 && range_int_cst_p (&vr1))
2389 signop sgn = TYPE_SIGN (expr_type);
2390 unsigned int prec = TYPE_PRECISION (expr_type);
2391 wide_int type_min = wi::min_value (TYPE_PRECISION (expr_type), sgn);
2392 wide_int type_max = wi::max_value (TYPE_PRECISION (expr_type), sgn);
2393 wide_int wmin, wmax;
2394 int min_ovf = 0;
2395 int max_ovf = 0;
2397 if (code == PLUS_EXPR)
2399 wmin = wi::add (vr0.min, vr1.min);
2400 wmax = wi::add (vr0.max, vr1.max);
2402 /* Check for overflow. */
2403 if (wi::cmp (vr1.min, 0, sgn) != wi::cmp (wmin, vr0.min, sgn))
2404 min_ovf = wi::cmp (vr0.min, wmin, sgn);
2405 if (wi::cmp (vr1.max, 0, sgn) != wi::cmp (wmax, vr0.max, sgn))
2406 max_ovf = wi::cmp (vr0.max, wmax, sgn);
2408 else /* if (code == MINUS_EXPR) */
2410 wmin = wi::sub (vr0.min, vr1.max);
2411 wmax = wi::sub (vr0.max, vr1.min);
2413 if (wi::cmp (0, vr1.max, sgn) != wi::cmp (wmin, vr0.min, sgn))
2414 min_ovf = wi::cmp (vr0.min, vr1.max, sgn);
2415 if (wi::cmp (0, vr1.min, sgn) != wi::cmp (wmax, vr0.max, sgn))
2416 max_ovf = wi::cmp (vr0.max, vr1.min, sgn);
2419 /* For non-wrapping arithmetic look at possibly smaller
2420 value-ranges of the type. */
2421 if (!TYPE_OVERFLOW_WRAPS (expr_type))
2423 if (vrp_val_min (expr_type))
2424 type_min = vrp_val_min (expr_type);
2425 if (vrp_val_max (expr_type))
2426 type_max = vrp_val_max (expr_type);
2429 /* Check for type overflow. */
2430 if (min_ovf == 0)
2432 if (wi::cmp (wmin, type_min, sgn) == -1)
2433 min_ovf = -1;
2434 else if (wi::cmp (wmin, type_max, sgn) == 1)
2435 min_ovf = 1;
2437 if (max_ovf == 0)
2439 if (wi::cmp (wmax, type_min, sgn) == -1)
2440 max_ovf = -1;
2441 else if (wi::cmp (wmax, type_max, sgn) == 1)
2442 max_ovf = 1;
2445 if (TYPE_OVERFLOW_WRAPS (expr_type))
2447 /* If overflow wraps, truncate the values and adjust the
2448 range kind and bounds appropriately. */
2449 wide_int tmin = wide_int::from (wmin, prec, sgn);
2450 wide_int tmax = wide_int::from (wmax, prec, sgn);
2451 if (min_ovf == max_ovf)
2453 /* No overflow or both overflow or underflow. The
2454 range kind stays VR_RANGE. */
2455 min = wide_int_to_tree (expr_type, tmin);
2456 max = wide_int_to_tree (expr_type, tmax);
2458 else if (min_ovf == -1
2459 && max_ovf == 1)
2461 /* Underflow and overflow, drop to VR_VARYING. */
2462 set_value_range_to_varying (vr);
2463 return;
2465 else
2467 /* Min underflow or max overflow. The range kind
2468 changes to VR_ANTI_RANGE. */
2469 bool covers = false;
2470 wide_int tem = tmin;
2471 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2472 || (max_ovf == 1 && min_ovf == 0));
2473 type = VR_ANTI_RANGE;
2474 tmin = tmax + 1;
2475 if (wi::cmp (tmin, tmax, sgn) < 0)
2476 covers = true;
2477 tmax = tem - 1;
2478 if (wi::cmp (tmax, tem, sgn) > 0)
2479 covers = true;
2480 /* If the anti-range would cover nothing, drop to varying.
2481 Likewise if the anti-range bounds are outside of the
2482 types values. */
2483 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2485 set_value_range_to_varying (vr);
2486 return;
2488 min = wide_int_to_tree (expr_type, tmin);
2489 max = wide_int_to_tree (expr_type, tmax);
2492 else
2494 /* If overflow does not wrap, saturate to the types min/max
2495 value. */
2496 if (min_ovf == -1)
2498 if (needs_overflow_infinity (expr_type)
2499 && supports_overflow_infinity (expr_type))
2500 min = negative_overflow_infinity (expr_type);
2501 else
2502 min = wide_int_to_tree (expr_type, type_min);
2504 else if (min_ovf == 1)
2506 if (needs_overflow_infinity (expr_type)
2507 && supports_overflow_infinity (expr_type))
2508 min = positive_overflow_infinity (expr_type);
2509 else
2510 min = wide_int_to_tree (expr_type, type_max);
2512 else
2513 min = wide_int_to_tree (expr_type, wmin);
2515 if (max_ovf == -1)
2517 if (needs_overflow_infinity (expr_type)
2518 && supports_overflow_infinity (expr_type))
2519 max = negative_overflow_infinity (expr_type);
2520 else
2521 max = wide_int_to_tree (expr_type, type_min);
2523 else if (max_ovf == 1)
2525 if (needs_overflow_infinity (expr_type)
2526 && supports_overflow_infinity (expr_type))
2527 max = positive_overflow_infinity (expr_type);
2528 else
2529 max = wide_int_to_tree (expr_type, type_max);
2531 else
2532 max = wide_int_to_tree (expr_type, wmax);
2534 if (needs_overflow_infinity (expr_type)
2535 && supports_overflow_infinity (expr_type))
2537 if (is_negative_overflow_infinity (vr0.min)
2538 || (code == PLUS_EXPR
2539 ? is_negative_overflow_infinity (vr1.min)
2540 : is_positive_overflow_infinity (vr1.max)))
2541 min = negative_overflow_infinity (expr_type);
2542 if (is_positive_overflow_infinity (vr0.max)
2543 || (code == PLUS_EXPR
2544 ? is_positive_overflow_infinity (vr1.max)
2545 : is_negative_overflow_infinity (vr1.min)))
2546 max = positive_overflow_infinity (expr_type);
2549 else
2551 /* For other cases, for example if we have a PLUS_EXPR with two
2552 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2553 to compute a precise range for such a case.
2554 ??? General even mixed range kind operations can be expressed
2555 by for example transforming ~[3, 5] + [1, 2] to range-only
2556 operations and a union primitive:
2557 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2558 [-INF+1, 4] U [6, +INF(OVF)]
2559 though usually the union is not exactly representable with
2560 a single range or anti-range as the above is
2561 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2562 but one could use a scheme similar to equivalences for this. */
2563 set_value_range_to_varying (vr);
2564 return;
2567 else if (code == MIN_EXPR
2568 || code == MAX_EXPR)
2570 if (vr0.type == VR_RANGE
2571 && !symbolic_range_p (&vr0))
2573 type = VR_RANGE;
2574 if (vr1.type == VR_RANGE
2575 && !symbolic_range_p (&vr1))
2577 /* For operations that make the resulting range directly
2578 proportional to the original ranges, apply the operation to
2579 the same end of each range. */
2580 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2581 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2583 else if (code == MIN_EXPR)
2585 min = vrp_val_min (expr_type);
2586 max = vr0.max;
2588 else if (code == MAX_EXPR)
2590 min = vr0.min;
2591 max = vrp_val_max (expr_type);
2594 else if (vr1.type == VR_RANGE
2595 && !symbolic_range_p (&vr1))
2597 type = VR_RANGE;
2598 if (code == MIN_EXPR)
2600 min = vrp_val_min (expr_type);
2601 max = vr1.max;
2603 else if (code == MAX_EXPR)
2605 min = vr1.min;
2606 max = vrp_val_max (expr_type);
2609 else
2611 set_value_range_to_varying (vr);
2612 return;
2615 else if (code == MULT_EXPR)
2617 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2618 drop to varying. This test requires 2*prec bits if both
2619 operands are signed and 2*prec + 2 bits if either is not. */
2621 signop sign = TYPE_SIGN (expr_type);
2622 unsigned int prec = TYPE_PRECISION (expr_type);
2624 if (range_int_cst_p (&vr0)
2625 && range_int_cst_p (&vr1)
2626 && TYPE_OVERFLOW_WRAPS (expr_type))
2628 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2629 typedef generic_wide_int
2630 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2631 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2632 vrp_int size = sizem1 + 1;
2634 /* Extend the values using the sign of the result to PREC2.
2635 From here on out, everthing is just signed math no matter
2636 what the input types were. */
2637 vrp_int min0 = vrp_int_cst (vr0.min);
2638 vrp_int max0 = vrp_int_cst (vr0.max);
2639 vrp_int min1 = vrp_int_cst (vr1.min);
2640 vrp_int max1 = vrp_int_cst (vr1.max);
2641 /* Canonicalize the intervals. */
2642 if (sign == UNSIGNED)
2644 if (wi::ltu_p (size, min0 + max0))
2646 min0 -= size;
2647 max0 -= size;
2650 if (wi::ltu_p (size, min1 + max1))
2652 min1 -= size;
2653 max1 -= size;
2657 vrp_int prod0 = min0 * min1;
2658 vrp_int prod1 = min0 * max1;
2659 vrp_int prod2 = max0 * min1;
2660 vrp_int prod3 = max0 * max1;
2662 /* Sort the 4 products so that min is in prod0 and max is in
2663 prod3. */
2664 /* min0min1 > max0max1 */
2665 if (wi::gts_p (prod0, prod3))
2667 vrp_int tmp = prod3;
2668 prod3 = prod0;
2669 prod0 = tmp;
2672 /* min0max1 > max0min1 */
2673 if (wi::gts_p (prod1, prod2))
2675 vrp_int tmp = prod2;
2676 prod2 = prod1;
2677 prod1 = tmp;
2680 if (wi::gts_p (prod0, prod1))
2682 vrp_int tmp = prod1;
2683 prod1 = prod0;
2684 prod0 = tmp;
2687 if (wi::gts_p (prod2, prod3))
2689 vrp_int tmp = prod3;
2690 prod3 = prod2;
2691 prod2 = tmp;
2694 /* diff = max - min. */
2695 prod2 = prod3 - prod0;
2696 if (wi::geu_p (prod2, sizem1))
2698 /* the range covers all values. */
2699 set_value_range_to_varying (vr);
2700 return;
2703 /* The following should handle the wrapping and selecting
2704 VR_ANTI_RANGE for us. */
2705 min = wide_int_to_tree (expr_type, prod0);
2706 max = wide_int_to_tree (expr_type, prod3);
2707 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2708 return;
2711 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2712 drop to VR_VARYING. It would take more effort to compute a
2713 precise range for such a case. For example, if we have
2714 op0 == 65536 and op1 == 65536 with their ranges both being
2715 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2716 we cannot claim that the product is in ~[0,0]. Note that we
2717 are guaranteed to have vr0.type == vr1.type at this
2718 point. */
2719 if (vr0.type == VR_ANTI_RANGE
2720 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2722 set_value_range_to_varying (vr);
2723 return;
2726 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2727 return;
2729 else if (code == RSHIFT_EXPR
2730 || code == LSHIFT_EXPR)
2732 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2733 then drop to VR_VARYING. Outside of this range we get undefined
2734 behavior from the shift operation. We cannot even trust
2735 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2736 shifts, and the operation at the tree level may be widened. */
2737 if (range_int_cst_p (&vr1)
2738 && compare_tree_int (vr1.min, 0) >= 0
2739 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2741 if (code == RSHIFT_EXPR)
2743 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2744 return;
2746 /* We can map lshifts by constants to MULT_EXPR handling. */
2747 else if (code == LSHIFT_EXPR
2748 && range_int_cst_singleton_p (&vr1))
2750 bool saved_flag_wrapv;
2751 value_range_t vr1p = VR_INITIALIZER;
2752 vr1p.type = VR_RANGE;
2753 vr1p.min = (wide_int_to_tree
2754 (expr_type,
2755 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2756 TYPE_PRECISION (expr_type))));
2757 vr1p.max = vr1p.min;
2758 /* We have to use a wrapping multiply though as signed overflow
2759 on lshifts is implementation defined in C89. */
2760 saved_flag_wrapv = flag_wrapv;
2761 flag_wrapv = 1;
2762 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2763 &vr0, &vr1p);
2764 flag_wrapv = saved_flag_wrapv;
2765 return;
2767 else if (code == LSHIFT_EXPR
2768 && range_int_cst_p (&vr0))
2770 int prec = TYPE_PRECISION (expr_type);
2771 int overflow_pos = prec;
2772 int bound_shift;
2773 wide_int low_bound, high_bound;
2774 bool uns = TYPE_UNSIGNED (expr_type);
2775 bool in_bounds = false;
2777 if (!uns)
2778 overflow_pos -= 1;
2780 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2781 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2782 overflow. However, for that to happen, vr1.max needs to be
2783 zero, which means vr1 is a singleton range of zero, which
2784 means it should be handled by the previous LSHIFT_EXPR
2785 if-clause. */
2786 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2787 wide_int complement = ~(bound - 1);
2789 if (uns)
2791 low_bound = bound;
2792 high_bound = complement;
2793 if (wi::ltu_p (vr0.max, low_bound))
2795 /* [5, 6] << [1, 2] == [10, 24]. */
2796 /* We're shifting out only zeroes, the value increases
2797 monotonically. */
2798 in_bounds = true;
2800 else if (wi::ltu_p (high_bound, vr0.min))
2802 /* [0xffffff00, 0xffffffff] << [1, 2]
2803 == [0xfffffc00, 0xfffffffe]. */
2804 /* We're shifting out only ones, the value decreases
2805 monotonically. */
2806 in_bounds = true;
2809 else
2811 /* [-1, 1] << [1, 2] == [-4, 4]. */
2812 low_bound = complement;
2813 high_bound = bound;
2814 if (wi::lts_p (vr0.max, high_bound)
2815 && wi::lts_p (low_bound, vr0.min))
2817 /* For non-negative numbers, we're shifting out only
2818 zeroes, the value increases monotonically.
2819 For negative numbers, we're shifting out only ones, the
2820 value decreases monotomically. */
2821 in_bounds = true;
2825 if (in_bounds)
2827 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2828 return;
2832 set_value_range_to_varying (vr);
2833 return;
2835 else if (code == TRUNC_DIV_EXPR
2836 || code == FLOOR_DIV_EXPR
2837 || code == CEIL_DIV_EXPR
2838 || code == EXACT_DIV_EXPR
2839 || code == ROUND_DIV_EXPR)
2841 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2843 /* For division, if op1 has VR_RANGE but op0 does not, something
2844 can be deduced just from that range. Say [min, max] / [4, max]
2845 gives [min / 4, max / 4] range. */
2846 if (vr1.type == VR_RANGE
2847 && !symbolic_range_p (&vr1)
2848 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2850 vr0.type = type = VR_RANGE;
2851 vr0.min = vrp_val_min (expr_type);
2852 vr0.max = vrp_val_max (expr_type);
2854 else
2856 set_value_range_to_varying (vr);
2857 return;
2861 /* For divisions, if flag_non_call_exceptions is true, we must
2862 not eliminate a division by zero. */
2863 if (cfun->can_throw_non_call_exceptions
2864 && (vr1.type != VR_RANGE
2865 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2867 set_value_range_to_varying (vr);
2868 return;
2871 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2872 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2873 include 0. */
2874 if (vr0.type == VR_RANGE
2875 && (vr1.type != VR_RANGE
2876 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2878 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2879 int cmp;
2881 min = NULL_TREE;
2882 max = NULL_TREE;
2883 if (TYPE_UNSIGNED (expr_type)
2884 || value_range_nonnegative_p (&vr1))
2886 /* For unsigned division or when divisor is known
2887 to be non-negative, the range has to cover
2888 all numbers from 0 to max for positive max
2889 and all numbers from min to 0 for negative min. */
2890 cmp = compare_values (vr0.max, zero);
2891 if (cmp == -1)
2892 max = zero;
2893 else if (cmp == 0 || cmp == 1)
2894 max = vr0.max;
2895 else
2896 type = VR_VARYING;
2897 cmp = compare_values (vr0.min, zero);
2898 if (cmp == 1)
2899 min = zero;
2900 else if (cmp == 0 || cmp == -1)
2901 min = vr0.min;
2902 else
2903 type = VR_VARYING;
2905 else
2907 /* Otherwise the range is -max .. max or min .. -min
2908 depending on which bound is bigger in absolute value,
2909 as the division can change the sign. */
2910 abs_extent_range (vr, vr0.min, vr0.max);
2911 return;
2913 if (type == VR_VARYING)
2915 set_value_range_to_varying (vr);
2916 return;
2919 else
2921 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2922 return;
2925 else if (code == TRUNC_MOD_EXPR)
2927 if (vr1.type != VR_RANGE
2928 || range_includes_zero_p (vr1.min, vr1.max) != 0
2929 || vrp_val_is_min (vr1.min))
2931 set_value_range_to_varying (vr);
2932 return;
2934 type = VR_RANGE;
2935 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2936 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
2937 if (tree_int_cst_lt (max, vr1.max))
2938 max = vr1.max;
2939 max = int_const_binop (MINUS_EXPR, max, build_int_cst (TREE_TYPE (max), 1));
2940 /* If the dividend is non-negative the modulus will be
2941 non-negative as well. */
2942 if (TYPE_UNSIGNED (expr_type)
2943 || value_range_nonnegative_p (&vr0))
2944 min = build_int_cst (TREE_TYPE (max), 0);
2945 else
2946 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
2948 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
2950 bool int_cst_range0, int_cst_range1;
2951 wide_int may_be_nonzero0, may_be_nonzero1;
2952 wide_int must_be_nonzero0, must_be_nonzero1;
2954 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
2955 &may_be_nonzero0,
2956 &must_be_nonzero0);
2957 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
2958 &may_be_nonzero1,
2959 &must_be_nonzero1);
2961 type = VR_RANGE;
2962 if (code == BIT_AND_EXPR)
2964 min = wide_int_to_tree (expr_type,
2965 must_be_nonzero0 & must_be_nonzero1);
2966 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
2967 /* If both input ranges contain only negative values we can
2968 truncate the result range maximum to the minimum of the
2969 input range maxima. */
2970 if (int_cst_range0 && int_cst_range1
2971 && tree_int_cst_sgn (vr0.max) < 0
2972 && tree_int_cst_sgn (vr1.max) < 0)
2974 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
2975 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
2977 /* If either input range contains only non-negative values
2978 we can truncate the result range maximum to the respective
2979 maximum of the input range. */
2980 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
2981 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
2982 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
2983 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
2984 max = wide_int_to_tree (expr_type, wmax);
2986 else if (code == BIT_IOR_EXPR)
2988 max = wide_int_to_tree (expr_type,
2989 may_be_nonzero0 | may_be_nonzero1);
2990 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
2991 /* If the input ranges contain only positive values we can
2992 truncate the minimum of the result range to the maximum
2993 of the input range minima. */
2994 if (int_cst_range0 && int_cst_range1
2995 && tree_int_cst_sgn (vr0.min) >= 0
2996 && tree_int_cst_sgn (vr1.min) >= 0)
2998 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
2999 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3001 /* If either input range contains only negative values
3002 we can truncate the minimum of the result range to the
3003 respective minimum range. */
3004 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3005 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3006 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3007 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3008 min = wide_int_to_tree (expr_type, wmin);
3010 else if (code == BIT_XOR_EXPR)
3012 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3013 | ~(may_be_nonzero0 | may_be_nonzero1));
3014 wide_int result_one_bits
3015 = (must_be_nonzero0.and_not (may_be_nonzero1)
3016 | must_be_nonzero1.and_not (may_be_nonzero0));
3017 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3018 min = wide_int_to_tree (expr_type, result_one_bits);
3019 /* If the range has all positive or all negative values the
3020 result is better than VARYING. */
3021 if (tree_int_cst_sgn (min) < 0
3022 || tree_int_cst_sgn (max) >= 0)
3024 else
3025 max = min = NULL_TREE;
3028 else
3029 gcc_unreachable ();
3031 /* If either MIN or MAX overflowed, then set the resulting range to
3032 VARYING. But we do accept an overflow infinity
3033 representation. */
3034 if (min == NULL_TREE
3035 || !is_gimple_min_invariant (min)
3036 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
3037 || max == NULL_TREE
3038 || !is_gimple_min_invariant (max)
3039 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
3041 set_value_range_to_varying (vr);
3042 return;
3045 /* We punt if:
3046 1) [-INF, +INF]
3047 2) [-INF, +-INF(OVF)]
3048 3) [+-INF(OVF), +INF]
3049 4) [+-INF(OVF), +-INF(OVF)]
3050 We learn nothing when we have INF and INF(OVF) on both sides.
3051 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3052 overflow. */
3053 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3054 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3056 set_value_range_to_varying (vr);
3057 return;
3060 cmp = compare_values (min, max);
3061 if (cmp == -2 || cmp == 1)
3063 /* If the new range has its limits swapped around (MIN > MAX),
3064 then the operation caused one of them to wrap around, mark
3065 the new range VARYING. */
3066 set_value_range_to_varying (vr);
3068 else
3069 set_value_range (vr, type, min, max, NULL);
3072 /* Extract range information from a binary expression OP0 CODE OP1 based on
3073 the ranges of each of its operands with resulting type EXPR_TYPE.
3074 The resulting range is stored in *VR. */
3076 static void
3077 extract_range_from_binary_expr (value_range_t *vr,
3078 enum tree_code code,
3079 tree expr_type, tree op0, tree op1)
3081 value_range_t vr0 = VR_INITIALIZER;
3082 value_range_t vr1 = VR_INITIALIZER;
3084 /* Get value ranges for each operand. For constant operands, create
3085 a new value range with the operand to simplify processing. */
3086 if (TREE_CODE (op0) == SSA_NAME)
3087 vr0 = *(get_value_range (op0));
3088 else if (is_gimple_min_invariant (op0))
3089 set_value_range_to_value (&vr0, op0, NULL);
3090 else
3091 set_value_range_to_varying (&vr0);
3093 if (TREE_CODE (op1) == SSA_NAME)
3094 vr1 = *(get_value_range (op1));
3095 else if (is_gimple_min_invariant (op1))
3096 set_value_range_to_value (&vr1, op1, NULL);
3097 else
3098 set_value_range_to_varying (&vr1);
3100 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3103 /* Extract range information from a unary operation CODE based on
3104 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3105 The The resulting range is stored in *VR. */
3107 static void
3108 extract_range_from_unary_expr_1 (value_range_t *vr,
3109 enum tree_code code, tree type,
3110 value_range_t *vr0_, tree op0_type)
3112 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3114 /* VRP only operates on integral and pointer types. */
3115 if (!(INTEGRAL_TYPE_P (op0_type)
3116 || POINTER_TYPE_P (op0_type))
3117 || !(INTEGRAL_TYPE_P (type)
3118 || POINTER_TYPE_P (type)))
3120 set_value_range_to_varying (vr);
3121 return;
3124 /* If VR0 is UNDEFINED, so is the result. */
3125 if (vr0.type == VR_UNDEFINED)
3127 set_value_range_to_undefined (vr);
3128 return;
3131 /* Handle operations that we express in terms of others. */
3132 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3134 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3135 copy_value_range (vr, &vr0);
3136 return;
3138 else if (code == NEGATE_EXPR)
3140 /* -X is simply 0 - X, so re-use existing code that also handles
3141 anti-ranges fine. */
3142 value_range_t zero = VR_INITIALIZER;
3143 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3144 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3145 return;
3147 else if (code == BIT_NOT_EXPR)
3149 /* ~X is simply -1 - X, so re-use existing code that also handles
3150 anti-ranges fine. */
3151 value_range_t minusone = VR_INITIALIZER;
3152 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3153 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3154 type, &minusone, &vr0);
3155 return;
3158 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3159 and express op ~[] as (op []') U (op []''). */
3160 if (vr0.type == VR_ANTI_RANGE
3161 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3163 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3164 if (vrtem1.type != VR_UNDEFINED)
3166 value_range_t vrres = VR_INITIALIZER;
3167 extract_range_from_unary_expr_1 (&vrres, code, type,
3168 &vrtem1, op0_type);
3169 vrp_meet (vr, &vrres);
3171 return;
3174 if (CONVERT_EXPR_CODE_P (code))
3176 tree inner_type = op0_type;
3177 tree outer_type = type;
3179 /* If the expression evaluates to a pointer, we are only interested in
3180 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3181 if (POINTER_TYPE_P (type))
3183 if (range_is_nonnull (&vr0))
3184 set_value_range_to_nonnull (vr, type);
3185 else if (range_is_null (&vr0))
3186 set_value_range_to_null (vr, type);
3187 else
3188 set_value_range_to_varying (vr);
3189 return;
3192 /* If VR0 is varying and we increase the type precision, assume
3193 a full range for the following transformation. */
3194 if (vr0.type == VR_VARYING
3195 && INTEGRAL_TYPE_P (inner_type)
3196 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3198 vr0.type = VR_RANGE;
3199 vr0.min = TYPE_MIN_VALUE (inner_type);
3200 vr0.max = TYPE_MAX_VALUE (inner_type);
3203 /* If VR0 is a constant range or anti-range and the conversion is
3204 not truncating we can convert the min and max values and
3205 canonicalize the resulting range. Otherwise we can do the
3206 conversion if the size of the range is less than what the
3207 precision of the target type can represent and the range is
3208 not an anti-range. */
3209 if ((vr0.type == VR_RANGE
3210 || vr0.type == VR_ANTI_RANGE)
3211 && TREE_CODE (vr0.min) == INTEGER_CST
3212 && TREE_CODE (vr0.max) == INTEGER_CST
3213 && (!is_overflow_infinity (vr0.min)
3214 || (vr0.type == VR_RANGE
3215 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3216 && needs_overflow_infinity (outer_type)
3217 && supports_overflow_infinity (outer_type)))
3218 && (!is_overflow_infinity (vr0.max)
3219 || (vr0.type == VR_RANGE
3220 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3221 && needs_overflow_infinity (outer_type)
3222 && supports_overflow_infinity (outer_type)))
3223 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3224 || (vr0.type == VR_RANGE
3225 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3226 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3227 size_int (TYPE_PRECISION (outer_type)))))))
3229 tree new_min, new_max;
3230 if (is_overflow_infinity (vr0.min))
3231 new_min = negative_overflow_infinity (outer_type);
3232 else
3233 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3234 0, false);
3235 if (is_overflow_infinity (vr0.max))
3236 new_max = positive_overflow_infinity (outer_type);
3237 else
3238 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3239 0, false);
3240 set_and_canonicalize_value_range (vr, vr0.type,
3241 new_min, new_max, NULL);
3242 return;
3245 set_value_range_to_varying (vr);
3246 return;
3248 else if (code == ABS_EXPR)
3250 tree min, max;
3251 int cmp;
3253 /* Pass through vr0 in the easy cases. */
3254 if (TYPE_UNSIGNED (type)
3255 || value_range_nonnegative_p (&vr0))
3257 copy_value_range (vr, &vr0);
3258 return;
3261 /* For the remaining varying or symbolic ranges we can't do anything
3262 useful. */
3263 if (vr0.type == VR_VARYING
3264 || symbolic_range_p (&vr0))
3266 set_value_range_to_varying (vr);
3267 return;
3270 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3271 useful range. */
3272 if (!TYPE_OVERFLOW_UNDEFINED (type)
3273 && ((vr0.type == VR_RANGE
3274 && vrp_val_is_min (vr0.min))
3275 || (vr0.type == VR_ANTI_RANGE
3276 && !vrp_val_is_min (vr0.min))))
3278 set_value_range_to_varying (vr);
3279 return;
3282 /* ABS_EXPR may flip the range around, if the original range
3283 included negative values. */
3284 if (is_overflow_infinity (vr0.min))
3285 min = positive_overflow_infinity (type);
3286 else if (!vrp_val_is_min (vr0.min))
3287 min = fold_unary_to_constant (code, type, vr0.min);
3288 else if (!needs_overflow_infinity (type))
3289 min = TYPE_MAX_VALUE (type);
3290 else if (supports_overflow_infinity (type))
3291 min = positive_overflow_infinity (type);
3292 else
3294 set_value_range_to_varying (vr);
3295 return;
3298 if (is_overflow_infinity (vr0.max))
3299 max = positive_overflow_infinity (type);
3300 else if (!vrp_val_is_min (vr0.max))
3301 max = fold_unary_to_constant (code, type, vr0.max);
3302 else if (!needs_overflow_infinity (type))
3303 max = TYPE_MAX_VALUE (type);
3304 else if (supports_overflow_infinity (type)
3305 /* We shouldn't generate [+INF, +INF] as set_value_range
3306 doesn't like this and ICEs. */
3307 && !is_positive_overflow_infinity (min))
3308 max = positive_overflow_infinity (type);
3309 else
3311 set_value_range_to_varying (vr);
3312 return;
3315 cmp = compare_values (min, max);
3317 /* If a VR_ANTI_RANGEs contains zero, then we have
3318 ~[-INF, min(MIN, MAX)]. */
3319 if (vr0.type == VR_ANTI_RANGE)
3321 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3323 /* Take the lower of the two values. */
3324 if (cmp != 1)
3325 max = min;
3327 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3328 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3329 flag_wrapv is set and the original anti-range doesn't include
3330 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3331 if (TYPE_OVERFLOW_WRAPS (type))
3333 tree type_min_value = TYPE_MIN_VALUE (type);
3335 min = (vr0.min != type_min_value
3336 ? int_const_binop (PLUS_EXPR, type_min_value,
3337 build_int_cst (TREE_TYPE (type_min_value), 1))
3338 : type_min_value);
3340 else
3342 if (overflow_infinity_range_p (&vr0))
3343 min = negative_overflow_infinity (type);
3344 else
3345 min = TYPE_MIN_VALUE (type);
3348 else
3350 /* All else has failed, so create the range [0, INF], even for
3351 flag_wrapv since TYPE_MIN_VALUE is in the original
3352 anti-range. */
3353 vr0.type = VR_RANGE;
3354 min = build_int_cst (type, 0);
3355 if (needs_overflow_infinity (type))
3357 if (supports_overflow_infinity (type))
3358 max = positive_overflow_infinity (type);
3359 else
3361 set_value_range_to_varying (vr);
3362 return;
3365 else
3366 max = TYPE_MAX_VALUE (type);
3370 /* If the range contains zero then we know that the minimum value in the
3371 range will be zero. */
3372 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3374 if (cmp == 1)
3375 max = min;
3376 min = build_int_cst (type, 0);
3378 else
3380 /* If the range was reversed, swap MIN and MAX. */
3381 if (cmp == 1)
3383 tree t = min;
3384 min = max;
3385 max = t;
3389 cmp = compare_values (min, max);
3390 if (cmp == -2 || cmp == 1)
3392 /* If the new range has its limits swapped around (MIN > MAX),
3393 then the operation caused one of them to wrap around, mark
3394 the new range VARYING. */
3395 set_value_range_to_varying (vr);
3397 else
3398 set_value_range (vr, vr0.type, min, max, NULL);
3399 return;
3402 /* For unhandled operations fall back to varying. */
3403 set_value_range_to_varying (vr);
3404 return;
3408 /* Extract range information from a unary expression CODE OP0 based on
3409 the range of its operand with resulting type TYPE.
3410 The resulting range is stored in *VR. */
3412 static void
3413 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3414 tree type, tree op0)
3416 value_range_t vr0 = VR_INITIALIZER;
3418 /* Get value ranges for the operand. For constant operands, create
3419 a new value range with the operand to simplify processing. */
3420 if (TREE_CODE (op0) == SSA_NAME)
3421 vr0 = *(get_value_range (op0));
3422 else if (is_gimple_min_invariant (op0))
3423 set_value_range_to_value (&vr0, op0, NULL);
3424 else
3425 set_value_range_to_varying (&vr0);
3427 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3431 /* Extract range information from a conditional expression STMT based on
3432 the ranges of each of its operands and the expression code. */
3434 static void
3435 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3437 tree op0, op1;
3438 value_range_t vr0 = VR_INITIALIZER;
3439 value_range_t vr1 = VR_INITIALIZER;
3441 /* Get value ranges for each operand. For constant operands, create
3442 a new value range with the operand to simplify processing. */
3443 op0 = gimple_assign_rhs2 (stmt);
3444 if (TREE_CODE (op0) == SSA_NAME)
3445 vr0 = *(get_value_range (op0));
3446 else if (is_gimple_min_invariant (op0))
3447 set_value_range_to_value (&vr0, op0, NULL);
3448 else
3449 set_value_range_to_varying (&vr0);
3451 op1 = gimple_assign_rhs3 (stmt);
3452 if (TREE_CODE (op1) == SSA_NAME)
3453 vr1 = *(get_value_range (op1));
3454 else if (is_gimple_min_invariant (op1))
3455 set_value_range_to_value (&vr1, op1, NULL);
3456 else
3457 set_value_range_to_varying (&vr1);
3459 /* The resulting value range is the union of the operand ranges */
3460 copy_value_range (vr, &vr0);
3461 vrp_meet (vr, &vr1);
3465 /* Extract range information from a comparison expression EXPR based
3466 on the range of its operand and the expression code. */
3468 static void
3469 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3470 tree type, tree op0, tree op1)
3472 bool sop = false;
3473 tree val;
3475 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3476 NULL);
3478 /* A disadvantage of using a special infinity as an overflow
3479 representation is that we lose the ability to record overflow
3480 when we don't have an infinity. So we have to ignore a result
3481 which relies on overflow. */
3483 if (val && !is_overflow_infinity (val) && !sop)
3485 /* Since this expression was found on the RHS of an assignment,
3486 its type may be different from _Bool. Convert VAL to EXPR's
3487 type. */
3488 val = fold_convert (type, val);
3489 if (is_gimple_min_invariant (val))
3490 set_value_range_to_value (vr, val, vr->equiv);
3491 else
3492 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3494 else
3495 /* The result of a comparison is always true or false. */
3496 set_value_range_to_truthvalue (vr, type);
3499 /* Try to derive a nonnegative or nonzero range out of STMT relying
3500 primarily on generic routines in fold in conjunction with range data.
3501 Store the result in *VR */
3503 static void
3504 extract_range_basic (value_range_t *vr, gimple stmt)
3506 bool sop = false;
3507 tree type = gimple_expr_type (stmt);
3509 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3511 tree fndecl = gimple_call_fndecl (stmt), arg;
3512 int mini, maxi, zerov = 0, prec;
3514 switch (DECL_FUNCTION_CODE (fndecl))
3516 case BUILT_IN_CONSTANT_P:
3517 /* If the call is __builtin_constant_p and the argument is a
3518 function parameter resolve it to false. This avoids bogus
3519 array bound warnings.
3520 ??? We could do this as early as inlining is finished. */
3521 arg = gimple_call_arg (stmt, 0);
3522 if (TREE_CODE (arg) == SSA_NAME
3523 && SSA_NAME_IS_DEFAULT_DEF (arg)
3524 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3526 set_value_range_to_null (vr, type);
3527 return;
3529 break;
3530 /* Both __builtin_ffs* and __builtin_popcount return
3531 [0, prec]. */
3532 CASE_INT_FN (BUILT_IN_FFS):
3533 CASE_INT_FN (BUILT_IN_POPCOUNT):
3534 arg = gimple_call_arg (stmt, 0);
3535 prec = TYPE_PRECISION (TREE_TYPE (arg));
3536 mini = 0;
3537 maxi = prec;
3538 if (TREE_CODE (arg) == SSA_NAME)
3540 value_range_t *vr0 = get_value_range (arg);
3541 /* If arg is non-zero, then ffs or popcount
3542 are non-zero. */
3543 if (((vr0->type == VR_RANGE
3544 && integer_nonzerop (vr0->min))
3545 || (vr0->type == VR_ANTI_RANGE
3546 && integer_zerop (vr0->min)))
3547 && !is_overflow_infinity (vr0->min))
3548 mini = 1;
3549 /* If some high bits are known to be zero,
3550 we can decrease the maximum. */
3551 if (vr0->type == VR_RANGE
3552 && TREE_CODE (vr0->max) == INTEGER_CST
3553 && !is_overflow_infinity (vr0->max))
3554 maxi = tree_floor_log2 (vr0->max) + 1;
3556 goto bitop_builtin;
3557 /* __builtin_parity* returns [0, 1]. */
3558 CASE_INT_FN (BUILT_IN_PARITY):
3559 mini = 0;
3560 maxi = 1;
3561 goto bitop_builtin;
3562 /* __builtin_c[lt]z* return [0, prec-1], except for
3563 when the argument is 0, but that is undefined behavior.
3564 On many targets where the CLZ RTL or optab value is defined
3565 for 0 the value is prec, so include that in the range
3566 by default. */
3567 CASE_INT_FN (BUILT_IN_CLZ):
3568 arg = gimple_call_arg (stmt, 0);
3569 prec = TYPE_PRECISION (TREE_TYPE (arg));
3570 mini = 0;
3571 maxi = prec;
3572 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3573 != CODE_FOR_nothing
3574 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3575 zerov)
3576 /* Handle only the single common value. */
3577 && zerov != prec)
3578 /* Magic value to give up, unless vr0 proves
3579 arg is non-zero. */
3580 mini = -2;
3581 if (TREE_CODE (arg) == SSA_NAME)
3583 value_range_t *vr0 = get_value_range (arg);
3584 /* From clz of VR_RANGE minimum we can compute
3585 result maximum. */
3586 if (vr0->type == VR_RANGE
3587 && TREE_CODE (vr0->min) == INTEGER_CST
3588 && !is_overflow_infinity (vr0->min))
3590 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3591 if (maxi != prec)
3592 mini = 0;
3594 else if (vr0->type == VR_ANTI_RANGE
3595 && integer_zerop (vr0->min)
3596 && !is_overflow_infinity (vr0->min))
3598 maxi = prec - 1;
3599 mini = 0;
3601 if (mini == -2)
3602 break;
3603 /* From clz of VR_RANGE maximum we can compute
3604 result minimum. */
3605 if (vr0->type == VR_RANGE
3606 && TREE_CODE (vr0->max) == INTEGER_CST
3607 && !is_overflow_infinity (vr0->max))
3609 mini = prec - 1 - tree_floor_log2 (vr0->max);
3610 if (mini == prec)
3611 break;
3614 if (mini == -2)
3615 break;
3616 goto bitop_builtin;
3617 /* __builtin_ctz* return [0, prec-1], except for
3618 when the argument is 0, but that is undefined behavior.
3619 If there is a ctz optab for this mode and
3620 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3621 otherwise just assume 0 won't be seen. */
3622 CASE_INT_FN (BUILT_IN_CTZ):
3623 arg = gimple_call_arg (stmt, 0);
3624 prec = TYPE_PRECISION (TREE_TYPE (arg));
3625 mini = 0;
3626 maxi = prec - 1;
3627 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3628 != CODE_FOR_nothing
3629 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3630 zerov))
3632 /* Handle only the two common values. */
3633 if (zerov == -1)
3634 mini = -1;
3635 else if (zerov == prec)
3636 maxi = prec;
3637 else
3638 /* Magic value to give up, unless vr0 proves
3639 arg is non-zero. */
3640 mini = -2;
3642 if (TREE_CODE (arg) == SSA_NAME)
3644 value_range_t *vr0 = get_value_range (arg);
3645 /* If arg is non-zero, then use [0, prec - 1]. */
3646 if (((vr0->type == VR_RANGE
3647 && integer_nonzerop (vr0->min))
3648 || (vr0->type == VR_ANTI_RANGE
3649 && integer_zerop (vr0->min)))
3650 && !is_overflow_infinity (vr0->min))
3652 mini = 0;
3653 maxi = prec - 1;
3655 /* If some high bits are known to be zero,
3656 we can decrease the result maximum. */
3657 if (vr0->type == VR_RANGE
3658 && TREE_CODE (vr0->max) == INTEGER_CST
3659 && !is_overflow_infinity (vr0->max))
3661 maxi = tree_floor_log2 (vr0->max);
3662 /* For vr0 [0, 0] give up. */
3663 if (maxi == -1)
3664 break;
3667 if (mini == -2)
3668 break;
3669 goto bitop_builtin;
3670 /* __builtin_clrsb* returns [0, prec-1]. */
3671 CASE_INT_FN (BUILT_IN_CLRSB):
3672 arg = gimple_call_arg (stmt, 0);
3673 prec = TYPE_PRECISION (TREE_TYPE (arg));
3674 mini = 0;
3675 maxi = prec - 1;
3676 goto bitop_builtin;
3677 bitop_builtin:
3678 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3679 build_int_cst (type, maxi), NULL);
3680 return;
3681 default:
3682 break;
3685 else if (is_gimple_call (stmt)
3686 && gimple_call_internal_p (stmt))
3688 enum tree_code subcode = ERROR_MARK;
3689 switch (gimple_call_internal_fn (stmt))
3691 case IFN_UBSAN_CHECK_ADD:
3692 subcode = PLUS_EXPR;
3693 break;
3694 case IFN_UBSAN_CHECK_SUB:
3695 subcode = MINUS_EXPR;
3696 break;
3697 case IFN_UBSAN_CHECK_MUL:
3698 subcode = MULT_EXPR;
3699 break;
3700 default:
3701 break;
3703 if (subcode != ERROR_MARK)
3705 bool saved_flag_wrapv = flag_wrapv;
3706 /* Pretend the arithmetics is wrapping. If there is
3707 any overflow, we'll complain, but will actually do
3708 wrapping operation. */
3709 flag_wrapv = 1;
3710 extract_range_from_binary_expr (vr, subcode, type,
3711 gimple_call_arg (stmt, 0),
3712 gimple_call_arg (stmt, 1));
3713 flag_wrapv = saved_flag_wrapv;
3715 /* If for both arguments vrp_valueize returned non-NULL,
3716 this should have been already folded and if not, it
3717 wasn't folded because of overflow. Avoid removing the
3718 UBSAN_CHECK_* calls in that case. */
3719 if (vr->type == VR_RANGE
3720 && (vr->min == vr->max
3721 || operand_equal_p (vr->min, vr->max, 0)))
3722 set_value_range_to_varying (vr);
3723 return;
3726 if (INTEGRAL_TYPE_P (type)
3727 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3728 set_value_range_to_nonnegative (vr, type,
3729 sop || stmt_overflow_infinity (stmt));
3730 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3731 && !sop)
3732 set_value_range_to_nonnull (vr, type);
3733 else
3734 set_value_range_to_varying (vr);
3738 /* Try to compute a useful range out of assignment STMT and store it
3739 in *VR. */
3741 static void
3742 extract_range_from_assignment (value_range_t *vr, gimple stmt)
3744 enum tree_code code = gimple_assign_rhs_code (stmt);
3746 if (code == ASSERT_EXPR)
3747 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3748 else if (code == SSA_NAME)
3749 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3750 else if (TREE_CODE_CLASS (code) == tcc_binary)
3751 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3752 gimple_expr_type (stmt),
3753 gimple_assign_rhs1 (stmt),
3754 gimple_assign_rhs2 (stmt));
3755 else if (TREE_CODE_CLASS (code) == tcc_unary)
3756 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3757 gimple_expr_type (stmt),
3758 gimple_assign_rhs1 (stmt));
3759 else if (code == COND_EXPR)
3760 extract_range_from_cond_expr (vr, stmt);
3761 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3762 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3763 gimple_expr_type (stmt),
3764 gimple_assign_rhs1 (stmt),
3765 gimple_assign_rhs2 (stmt));
3766 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3767 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3768 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3769 else
3770 set_value_range_to_varying (vr);
3772 if (vr->type == VR_VARYING)
3773 extract_range_basic (vr, stmt);
3776 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3777 would be profitable to adjust VR using scalar evolution information
3778 for VAR. If so, update VR with the new limits. */
3780 static void
3781 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
3782 gimple stmt, tree var)
3784 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3785 enum ev_direction dir;
3787 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3788 better opportunities than a regular range, but I'm not sure. */
3789 if (vr->type == VR_ANTI_RANGE)
3790 return;
3792 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3794 /* Like in PR19590, scev can return a constant function. */
3795 if (is_gimple_min_invariant (chrec))
3797 set_value_range_to_value (vr, chrec, vr->equiv);
3798 return;
3801 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3802 return;
3804 init = initial_condition_in_loop_num (chrec, loop->num);
3805 tem = op_with_constant_singleton_value_range (init);
3806 if (tem)
3807 init = tem;
3808 step = evolution_part_in_loop_num (chrec, loop->num);
3809 tem = op_with_constant_singleton_value_range (step);
3810 if (tem)
3811 step = tem;
3813 /* If STEP is symbolic, we can't know whether INIT will be the
3814 minimum or maximum value in the range. Also, unless INIT is
3815 a simple expression, compare_values and possibly other functions
3816 in tree-vrp won't be able to handle it. */
3817 if (step == NULL_TREE
3818 || !is_gimple_min_invariant (step)
3819 || !valid_value_p (init))
3820 return;
3822 dir = scev_direction (chrec);
3823 if (/* Do not adjust ranges if we do not know whether the iv increases
3824 or decreases, ... */
3825 dir == EV_DIR_UNKNOWN
3826 /* ... or if it may wrap. */
3827 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3828 true))
3829 return;
3831 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3832 negative_overflow_infinity and positive_overflow_infinity,
3833 because we have concluded that the loop probably does not
3834 wrap. */
3836 type = TREE_TYPE (var);
3837 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
3838 tmin = lower_bound_in_type (type, type);
3839 else
3840 tmin = TYPE_MIN_VALUE (type);
3841 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
3842 tmax = upper_bound_in_type (type, type);
3843 else
3844 tmax = TYPE_MAX_VALUE (type);
3846 /* Try to use estimated number of iterations for the loop to constrain the
3847 final value in the evolution. */
3848 if (TREE_CODE (step) == INTEGER_CST
3849 && is_gimple_val (init)
3850 && (TREE_CODE (init) != SSA_NAME
3851 || get_value_range (init)->type == VR_RANGE))
3853 widest_int nit;
3855 /* We are only entering here for loop header PHI nodes, so using
3856 the number of latch executions is the correct thing to use. */
3857 if (max_loop_iterations (loop, &nit))
3859 value_range_t maxvr = VR_INITIALIZER;
3860 signop sgn = TYPE_SIGN (TREE_TYPE (step));
3861 bool overflow;
3863 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
3864 &overflow);
3865 /* If the multiplication overflowed we can't do a meaningful
3866 adjustment. Likewise if the result doesn't fit in the type
3867 of the induction variable. For a signed type we have to
3868 check whether the result has the expected signedness which
3869 is that of the step as number of iterations is unsigned. */
3870 if (!overflow
3871 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
3872 && (sgn == UNSIGNED
3873 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
3875 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
3876 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
3877 TREE_TYPE (init), init, tem);
3878 /* Likewise if the addition did. */
3879 if (maxvr.type == VR_RANGE)
3881 tmin = maxvr.min;
3882 tmax = maxvr.max;
3888 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3890 min = tmin;
3891 max = tmax;
3893 /* For VARYING or UNDEFINED ranges, just about anything we get
3894 from scalar evolutions should be better. */
3896 if (dir == EV_DIR_DECREASES)
3897 max = init;
3898 else
3899 min = init;
3901 /* If we would create an invalid range, then just assume we
3902 know absolutely nothing. This may be over-conservative,
3903 but it's clearly safe, and should happen only in unreachable
3904 parts of code, or for invalid programs. */
3905 if (compare_values (min, max) == 1)
3906 return;
3908 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3910 else if (vr->type == VR_RANGE)
3912 min = vr->min;
3913 max = vr->max;
3915 if (dir == EV_DIR_DECREASES)
3917 /* INIT is the maximum value. If INIT is lower than VR->MAX
3918 but no smaller than VR->MIN, set VR->MAX to INIT. */
3919 if (compare_values (init, max) == -1)
3920 max = init;
3922 /* According to the loop information, the variable does not
3923 overflow. If we think it does, probably because of an
3924 overflow due to arithmetic on a different INF value,
3925 reset now. */
3926 if (is_negative_overflow_infinity (min)
3927 || compare_values (min, tmin) == -1)
3928 min = tmin;
3931 else
3933 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3934 if (compare_values (init, min) == 1)
3935 min = init;
3937 if (is_positive_overflow_infinity (max)
3938 || compare_values (tmax, max) == -1)
3939 max = tmax;
3942 /* If we just created an invalid range with the minimum
3943 greater than the maximum, we fail conservatively.
3944 This should happen only in unreachable
3945 parts of code, or for invalid programs. */
3946 if (compare_values (min, max) == 1)
3947 return;
3949 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3954 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3956 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3957 all the values in the ranges.
3959 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3961 - Return NULL_TREE if it is not always possible to determine the
3962 value of the comparison.
3964 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3965 overflow infinity was used in the test. */
3968 static tree
3969 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
3970 bool *strict_overflow_p)
3972 /* VARYING or UNDEFINED ranges cannot be compared. */
3973 if (vr0->type == VR_VARYING
3974 || vr0->type == VR_UNDEFINED
3975 || vr1->type == VR_VARYING
3976 || vr1->type == VR_UNDEFINED)
3977 return NULL_TREE;
3979 /* Anti-ranges need to be handled separately. */
3980 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
3982 /* If both are anti-ranges, then we cannot compute any
3983 comparison. */
3984 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
3985 return NULL_TREE;
3987 /* These comparisons are never statically computable. */
3988 if (comp == GT_EXPR
3989 || comp == GE_EXPR
3990 || comp == LT_EXPR
3991 || comp == LE_EXPR)
3992 return NULL_TREE;
3994 /* Equality can be computed only between a range and an
3995 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3996 if (vr0->type == VR_RANGE)
3998 /* To simplify processing, make VR0 the anti-range. */
3999 value_range_t *tmp = vr0;
4000 vr0 = vr1;
4001 vr1 = tmp;
4004 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4006 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4007 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4008 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4010 return NULL_TREE;
4013 if (!usable_range_p (vr0, strict_overflow_p)
4014 || !usable_range_p (vr1, strict_overflow_p))
4015 return NULL_TREE;
4017 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4018 operands around and change the comparison code. */
4019 if (comp == GT_EXPR || comp == GE_EXPR)
4021 value_range_t *tmp;
4022 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4023 tmp = vr0;
4024 vr0 = vr1;
4025 vr1 = tmp;
4028 if (comp == EQ_EXPR)
4030 /* Equality may only be computed if both ranges represent
4031 exactly one value. */
4032 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4033 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4035 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4036 strict_overflow_p);
4037 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4038 strict_overflow_p);
4039 if (cmp_min == 0 && cmp_max == 0)
4040 return boolean_true_node;
4041 else if (cmp_min != -2 && cmp_max != -2)
4042 return boolean_false_node;
4044 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4045 else if (compare_values_warnv (vr0->min, vr1->max,
4046 strict_overflow_p) == 1
4047 || compare_values_warnv (vr1->min, vr0->max,
4048 strict_overflow_p) == 1)
4049 return boolean_false_node;
4051 return NULL_TREE;
4053 else if (comp == NE_EXPR)
4055 int cmp1, cmp2;
4057 /* If VR0 is completely to the left or completely to the right
4058 of VR1, they are always different. Notice that we need to
4059 make sure that both comparisons yield similar results to
4060 avoid comparing values that cannot be compared at
4061 compile-time. */
4062 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4063 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4064 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4065 return boolean_true_node;
4067 /* If VR0 and VR1 represent a single value and are identical,
4068 return false. */
4069 else if (compare_values_warnv (vr0->min, vr0->max,
4070 strict_overflow_p) == 0
4071 && compare_values_warnv (vr1->min, vr1->max,
4072 strict_overflow_p) == 0
4073 && compare_values_warnv (vr0->min, vr1->min,
4074 strict_overflow_p) == 0
4075 && compare_values_warnv (vr0->max, vr1->max,
4076 strict_overflow_p) == 0)
4077 return boolean_false_node;
4079 /* Otherwise, they may or may not be different. */
4080 else
4081 return NULL_TREE;
4083 else if (comp == LT_EXPR || comp == LE_EXPR)
4085 int tst;
4087 /* If VR0 is to the left of VR1, return true. */
4088 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4089 if ((comp == LT_EXPR && tst == -1)
4090 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4092 if (overflow_infinity_range_p (vr0)
4093 || overflow_infinity_range_p (vr1))
4094 *strict_overflow_p = true;
4095 return boolean_true_node;
4098 /* If VR0 is to the right of VR1, return false. */
4099 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4100 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4101 || (comp == LE_EXPR && tst == 1))
4103 if (overflow_infinity_range_p (vr0)
4104 || overflow_infinity_range_p (vr1))
4105 *strict_overflow_p = true;
4106 return boolean_false_node;
4109 /* Otherwise, we don't know. */
4110 return NULL_TREE;
4113 gcc_unreachable ();
4117 /* Given a value range VR, a value VAL and a comparison code COMP, return
4118 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4119 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4120 always returns false. Return NULL_TREE if it is not always
4121 possible to determine the value of the comparison. Also set
4122 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4123 infinity was used in the test. */
4125 static tree
4126 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4127 bool *strict_overflow_p)
4129 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4130 return NULL_TREE;
4132 /* Anti-ranges need to be handled separately. */
4133 if (vr->type == VR_ANTI_RANGE)
4135 /* For anti-ranges, the only predicates that we can compute at
4136 compile time are equality and inequality. */
4137 if (comp == GT_EXPR
4138 || comp == GE_EXPR
4139 || comp == LT_EXPR
4140 || comp == LE_EXPR)
4141 return NULL_TREE;
4143 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4144 if (value_inside_range (val, vr->min, vr->max) == 1)
4145 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4147 return NULL_TREE;
4150 if (!usable_range_p (vr, strict_overflow_p))
4151 return NULL_TREE;
4153 if (comp == EQ_EXPR)
4155 /* EQ_EXPR may only be computed if VR represents exactly
4156 one value. */
4157 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4159 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4160 if (cmp == 0)
4161 return boolean_true_node;
4162 else if (cmp == -1 || cmp == 1 || cmp == 2)
4163 return boolean_false_node;
4165 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4166 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4167 return boolean_false_node;
4169 return NULL_TREE;
4171 else if (comp == NE_EXPR)
4173 /* If VAL is not inside VR, then they are always different. */
4174 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4175 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4176 return boolean_true_node;
4178 /* If VR represents exactly one value equal to VAL, then return
4179 false. */
4180 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4181 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4182 return boolean_false_node;
4184 /* Otherwise, they may or may not be different. */
4185 return NULL_TREE;
4187 else if (comp == LT_EXPR || comp == LE_EXPR)
4189 int tst;
4191 /* If VR is to the left of VAL, return true. */
4192 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4193 if ((comp == LT_EXPR && tst == -1)
4194 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4196 if (overflow_infinity_range_p (vr))
4197 *strict_overflow_p = true;
4198 return boolean_true_node;
4201 /* If VR is to the right of VAL, return false. */
4202 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4203 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4204 || (comp == LE_EXPR && tst == 1))
4206 if (overflow_infinity_range_p (vr))
4207 *strict_overflow_p = true;
4208 return boolean_false_node;
4211 /* Otherwise, we don't know. */
4212 return NULL_TREE;
4214 else if (comp == GT_EXPR || comp == GE_EXPR)
4216 int tst;
4218 /* If VR is to the right of VAL, return true. */
4219 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4220 if ((comp == GT_EXPR && tst == 1)
4221 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4223 if (overflow_infinity_range_p (vr))
4224 *strict_overflow_p = true;
4225 return boolean_true_node;
4228 /* If VR is to the left of VAL, return false. */
4229 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4230 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4231 || (comp == GE_EXPR && tst == -1))
4233 if (overflow_infinity_range_p (vr))
4234 *strict_overflow_p = true;
4235 return boolean_false_node;
4238 /* Otherwise, we don't know. */
4239 return NULL_TREE;
4242 gcc_unreachable ();
4246 /* Debugging dumps. */
4248 void dump_value_range (FILE *, value_range_t *);
4249 void debug_value_range (value_range_t *);
4250 void dump_all_value_ranges (FILE *);
4251 void debug_all_value_ranges (void);
4252 void dump_vr_equiv (FILE *, bitmap);
4253 void debug_vr_equiv (bitmap);
4256 /* Dump value range VR to FILE. */
4258 void
4259 dump_value_range (FILE *file, value_range_t *vr)
4261 if (vr == NULL)
4262 fprintf (file, "[]");
4263 else if (vr->type == VR_UNDEFINED)
4264 fprintf (file, "UNDEFINED");
4265 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4267 tree type = TREE_TYPE (vr->min);
4269 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4271 if (is_negative_overflow_infinity (vr->min))
4272 fprintf (file, "-INF(OVF)");
4273 else if (INTEGRAL_TYPE_P (type)
4274 && !TYPE_UNSIGNED (type)
4275 && vrp_val_is_min (vr->min))
4276 fprintf (file, "-INF");
4277 else
4278 print_generic_expr (file, vr->min, 0);
4280 fprintf (file, ", ");
4282 if (is_positive_overflow_infinity (vr->max))
4283 fprintf (file, "+INF(OVF)");
4284 else if (INTEGRAL_TYPE_P (type)
4285 && vrp_val_is_max (vr->max))
4286 fprintf (file, "+INF");
4287 else
4288 print_generic_expr (file, vr->max, 0);
4290 fprintf (file, "]");
4292 if (vr->equiv)
4294 bitmap_iterator bi;
4295 unsigned i, c = 0;
4297 fprintf (file, " EQUIVALENCES: { ");
4299 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4301 print_generic_expr (file, ssa_name (i), 0);
4302 fprintf (file, " ");
4303 c++;
4306 fprintf (file, "} (%u elements)", c);
4309 else if (vr->type == VR_VARYING)
4310 fprintf (file, "VARYING");
4311 else
4312 fprintf (file, "INVALID RANGE");
4316 /* Dump value range VR to stderr. */
4318 DEBUG_FUNCTION void
4319 debug_value_range (value_range_t *vr)
4321 dump_value_range (stderr, vr);
4322 fprintf (stderr, "\n");
4326 /* Dump value ranges of all SSA_NAMEs to FILE. */
4328 void
4329 dump_all_value_ranges (FILE *file)
4331 size_t i;
4333 for (i = 0; i < num_vr_values; i++)
4335 if (vr_value[i])
4337 print_generic_expr (file, ssa_name (i), 0);
4338 fprintf (file, ": ");
4339 dump_value_range (file, vr_value[i]);
4340 fprintf (file, "\n");
4344 fprintf (file, "\n");
4348 /* Dump all value ranges to stderr. */
4350 DEBUG_FUNCTION void
4351 debug_all_value_ranges (void)
4353 dump_all_value_ranges (stderr);
4357 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4358 create a new SSA name N and return the assertion assignment
4359 'N = ASSERT_EXPR <V, V OP W>'. */
4361 static gimple
4362 build_assert_expr_for (tree cond, tree v)
4364 tree a;
4365 gimple assertion;
4367 gcc_assert (TREE_CODE (v) == SSA_NAME
4368 && COMPARISON_CLASS_P (cond));
4370 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4371 assertion = gimple_build_assign (NULL_TREE, a);
4373 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4374 operand of the ASSERT_EXPR. Create it so the new name and the old one
4375 are registered in the replacement table so that we can fix the SSA web
4376 after adding all the ASSERT_EXPRs. */
4377 create_new_def_for (v, assertion, NULL);
4379 return assertion;
4383 /* Return false if EXPR is a predicate expression involving floating
4384 point values. */
4386 static inline bool
4387 fp_predicate (gimple stmt)
4389 GIMPLE_CHECK (stmt, GIMPLE_COND);
4391 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4394 /* If the range of values taken by OP can be inferred after STMT executes,
4395 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4396 describes the inferred range. Return true if a range could be
4397 inferred. */
4399 static bool
4400 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4402 *val_p = NULL_TREE;
4403 *comp_code_p = ERROR_MARK;
4405 /* Do not attempt to infer anything in names that flow through
4406 abnormal edges. */
4407 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4408 return false;
4410 /* Similarly, don't infer anything from statements that may throw
4411 exceptions. ??? Relax this requirement? */
4412 if (stmt_could_throw_p (stmt))
4413 return false;
4415 /* If STMT is the last statement of a basic block with no normal
4416 successors, there is no point inferring anything about any of its
4417 operands. We would not be able to find a proper insertion point
4418 for the assertion, anyway. */
4419 if (stmt_ends_bb_p (stmt))
4421 edge_iterator ei;
4422 edge e;
4424 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4425 if (!(e->flags & EDGE_ABNORMAL))
4426 break;
4427 if (e == NULL)
4428 return false;
4431 if (infer_nonnull_range (stmt, op, true, true))
4433 *val_p = build_int_cst (TREE_TYPE (op), 0);
4434 *comp_code_p = NE_EXPR;
4435 return true;
4438 return false;
4442 void dump_asserts_for (FILE *, tree);
4443 void debug_asserts_for (tree);
4444 void dump_all_asserts (FILE *);
4445 void debug_all_asserts (void);
4447 /* Dump all the registered assertions for NAME to FILE. */
4449 void
4450 dump_asserts_for (FILE *file, tree name)
4452 assert_locus_t loc;
4454 fprintf (file, "Assertions to be inserted for ");
4455 print_generic_expr (file, name, 0);
4456 fprintf (file, "\n");
4458 loc = asserts_for[SSA_NAME_VERSION (name)];
4459 while (loc)
4461 fprintf (file, "\t");
4462 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4463 fprintf (file, "\n\tBB #%d", loc->bb->index);
4464 if (loc->e)
4466 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4467 loc->e->dest->index);
4468 dump_edge_info (file, loc->e, dump_flags, 0);
4470 fprintf (file, "\n\tPREDICATE: ");
4471 print_generic_expr (file, name, 0);
4472 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4473 print_generic_expr (file, loc->val, 0);
4474 fprintf (file, "\n\n");
4475 loc = loc->next;
4478 fprintf (file, "\n");
4482 /* Dump all the registered assertions for NAME to stderr. */
4484 DEBUG_FUNCTION void
4485 debug_asserts_for (tree name)
4487 dump_asserts_for (stderr, name);
4491 /* Dump all the registered assertions for all the names to FILE. */
4493 void
4494 dump_all_asserts (FILE *file)
4496 unsigned i;
4497 bitmap_iterator bi;
4499 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4500 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4501 dump_asserts_for (file, ssa_name (i));
4502 fprintf (file, "\n");
4506 /* Dump all the registered assertions for all the names to stderr. */
4508 DEBUG_FUNCTION void
4509 debug_all_asserts (void)
4511 dump_all_asserts (stderr);
4515 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4516 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4517 E->DEST, then register this location as a possible insertion point
4518 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4520 BB, E and SI provide the exact insertion point for the new
4521 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4522 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4523 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4524 must not be NULL. */
4526 static void
4527 register_new_assert_for (tree name, tree expr,
4528 enum tree_code comp_code,
4529 tree val,
4530 basic_block bb,
4531 edge e,
4532 gimple_stmt_iterator si)
4534 assert_locus_t n, loc, last_loc;
4535 basic_block dest_bb;
4537 gcc_checking_assert (bb == NULL || e == NULL);
4539 if (e == NULL)
4540 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4541 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4543 /* Never build an assert comparing against an integer constant with
4544 TREE_OVERFLOW set. This confuses our undefined overflow warning
4545 machinery. */
4546 if (TREE_OVERFLOW_P (val))
4547 val = drop_tree_overflow (val);
4549 /* The new assertion A will be inserted at BB or E. We need to
4550 determine if the new location is dominated by a previously
4551 registered location for A. If we are doing an edge insertion,
4552 assume that A will be inserted at E->DEST. Note that this is not
4553 necessarily true.
4555 If E is a critical edge, it will be split. But even if E is
4556 split, the new block will dominate the same set of blocks that
4557 E->DEST dominates.
4559 The reverse, however, is not true, blocks dominated by E->DEST
4560 will not be dominated by the new block created to split E. So,
4561 if the insertion location is on a critical edge, we will not use
4562 the new location to move another assertion previously registered
4563 at a block dominated by E->DEST. */
4564 dest_bb = (bb) ? bb : e->dest;
4566 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4567 VAL at a block dominating DEST_BB, then we don't need to insert a new
4568 one. Similarly, if the same assertion already exists at a block
4569 dominated by DEST_BB and the new location is not on a critical
4570 edge, then update the existing location for the assertion (i.e.,
4571 move the assertion up in the dominance tree).
4573 Note, this is implemented as a simple linked list because there
4574 should not be more than a handful of assertions registered per
4575 name. If this becomes a performance problem, a table hashed by
4576 COMP_CODE and VAL could be implemented. */
4577 loc = asserts_for[SSA_NAME_VERSION (name)];
4578 last_loc = loc;
4579 while (loc)
4581 if (loc->comp_code == comp_code
4582 && (loc->val == val
4583 || operand_equal_p (loc->val, val, 0))
4584 && (loc->expr == expr
4585 || operand_equal_p (loc->expr, expr, 0)))
4587 /* If E is not a critical edge and DEST_BB
4588 dominates the existing location for the assertion, move
4589 the assertion up in the dominance tree by updating its
4590 location information. */
4591 if ((e == NULL || !EDGE_CRITICAL_P (e))
4592 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4594 loc->bb = dest_bb;
4595 loc->e = e;
4596 loc->si = si;
4597 return;
4601 /* Update the last node of the list and move to the next one. */
4602 last_loc = loc;
4603 loc = loc->next;
4606 /* If we didn't find an assertion already registered for
4607 NAME COMP_CODE VAL, add a new one at the end of the list of
4608 assertions associated with NAME. */
4609 n = XNEW (struct assert_locus_d);
4610 n->bb = dest_bb;
4611 n->e = e;
4612 n->si = si;
4613 n->comp_code = comp_code;
4614 n->val = val;
4615 n->expr = expr;
4616 n->next = NULL;
4618 if (last_loc)
4619 last_loc->next = n;
4620 else
4621 asserts_for[SSA_NAME_VERSION (name)] = n;
4623 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4626 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4627 Extract a suitable test code and value and store them into *CODE_P and
4628 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4630 If no extraction was possible, return FALSE, otherwise return TRUE.
4632 If INVERT is true, then we invert the result stored into *CODE_P. */
4634 static bool
4635 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4636 tree cond_op0, tree cond_op1,
4637 bool invert, enum tree_code *code_p,
4638 tree *val_p)
4640 enum tree_code comp_code;
4641 tree val;
4643 /* Otherwise, we have a comparison of the form NAME COMP VAL
4644 or VAL COMP NAME. */
4645 if (name == cond_op1)
4647 /* If the predicate is of the form VAL COMP NAME, flip
4648 COMP around because we need to register NAME as the
4649 first operand in the predicate. */
4650 comp_code = swap_tree_comparison (cond_code);
4651 val = cond_op0;
4653 else
4655 /* The comparison is of the form NAME COMP VAL, so the
4656 comparison code remains unchanged. */
4657 comp_code = cond_code;
4658 val = cond_op1;
4661 /* Invert the comparison code as necessary. */
4662 if (invert)
4663 comp_code = invert_tree_comparison (comp_code, 0);
4665 /* VRP does not handle float types. */
4666 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4667 return false;
4669 /* Do not register always-false predicates.
4670 FIXME: this works around a limitation in fold() when dealing with
4671 enumerations. Given 'enum { N1, N2 } x;', fold will not
4672 fold 'if (x > N2)' to 'if (0)'. */
4673 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4674 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4676 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4677 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4679 if (comp_code == GT_EXPR
4680 && (!max
4681 || compare_values (val, max) == 0))
4682 return false;
4684 if (comp_code == LT_EXPR
4685 && (!min
4686 || compare_values (val, min) == 0))
4687 return false;
4689 *code_p = comp_code;
4690 *val_p = val;
4691 return true;
4694 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4695 (otherwise return VAL). VAL and MASK must be zero-extended for
4696 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4697 (to transform signed values into unsigned) and at the end xor
4698 SGNBIT back. */
4700 static wide_int
4701 masked_increment (const wide_int &val_in, const wide_int &mask,
4702 const wide_int &sgnbit, unsigned int prec)
4704 wide_int bit = wi::one (prec), res;
4705 unsigned int i;
4707 wide_int val = val_in ^ sgnbit;
4708 for (i = 0; i < prec; i++, bit += bit)
4710 res = mask;
4711 if ((res & bit) == 0)
4712 continue;
4713 res = bit - 1;
4714 res = (val + bit).and_not (res);
4715 res &= mask;
4716 if (wi::gtu_p (res, val))
4717 return res ^ sgnbit;
4719 return val ^ sgnbit;
4722 /* Try to register an edge assertion for SSA name NAME on edge E for
4723 the condition COND contributing to the conditional jump pointed to by BSI.
4724 Invert the condition COND if INVERT is true.
4725 Return true if an assertion for NAME could be registered. */
4727 static bool
4728 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4729 enum tree_code cond_code,
4730 tree cond_op0, tree cond_op1, bool invert)
4732 tree val;
4733 enum tree_code comp_code;
4734 bool retval = false;
4736 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4737 cond_op0,
4738 cond_op1,
4739 invert, &comp_code, &val))
4740 return false;
4742 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4743 reachable from E. */
4744 if (live_on_edge (e, name)
4745 && !has_single_use (name))
4747 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
4748 retval = true;
4751 /* In the case of NAME <= CST and NAME being defined as
4752 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4753 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4754 This catches range and anti-range tests. */
4755 if ((comp_code == LE_EXPR
4756 || comp_code == GT_EXPR)
4757 && TREE_CODE (val) == INTEGER_CST
4758 && TYPE_UNSIGNED (TREE_TYPE (val)))
4760 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4761 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
4763 /* Extract CST2 from the (optional) addition. */
4764 if (is_gimple_assign (def_stmt)
4765 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
4767 name2 = gimple_assign_rhs1 (def_stmt);
4768 cst2 = gimple_assign_rhs2 (def_stmt);
4769 if (TREE_CODE (name2) == SSA_NAME
4770 && TREE_CODE (cst2) == INTEGER_CST)
4771 def_stmt = SSA_NAME_DEF_STMT (name2);
4774 /* Extract NAME2 from the (optional) sign-changing cast. */
4775 if (gimple_assign_cast_p (def_stmt))
4777 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4778 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4779 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
4780 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
4781 name3 = gimple_assign_rhs1 (def_stmt);
4784 /* If name3 is used later, create an ASSERT_EXPR for it. */
4785 if (name3 != NULL_TREE
4786 && TREE_CODE (name3) == SSA_NAME
4787 && (cst2 == NULL_TREE
4788 || TREE_CODE (cst2) == INTEGER_CST)
4789 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
4790 && live_on_edge (e, name3)
4791 && !has_single_use (name3))
4793 tree tmp;
4795 /* Build an expression for the range test. */
4796 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
4797 if (cst2 != NULL_TREE)
4798 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4800 if (dump_file)
4802 fprintf (dump_file, "Adding assert for ");
4803 print_generic_expr (dump_file, name3, 0);
4804 fprintf (dump_file, " from ");
4805 print_generic_expr (dump_file, tmp, 0);
4806 fprintf (dump_file, "\n");
4809 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
4811 retval = true;
4814 /* If name2 is used later, create an ASSERT_EXPR for it. */
4815 if (name2 != NULL_TREE
4816 && TREE_CODE (name2) == SSA_NAME
4817 && TREE_CODE (cst2) == INTEGER_CST
4818 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4819 && live_on_edge (e, name2)
4820 && !has_single_use (name2))
4822 tree tmp;
4824 /* Build an expression for the range test. */
4825 tmp = name2;
4826 if (TREE_TYPE (name) != TREE_TYPE (name2))
4827 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
4828 if (cst2 != NULL_TREE)
4829 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4831 if (dump_file)
4833 fprintf (dump_file, "Adding assert for ");
4834 print_generic_expr (dump_file, name2, 0);
4835 fprintf (dump_file, " from ");
4836 print_generic_expr (dump_file, tmp, 0);
4837 fprintf (dump_file, "\n");
4840 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
4842 retval = true;
4846 /* In the case of post-in/decrement tests like if (i++) ... and uses
4847 of the in/decremented value on the edge the extra name we want to
4848 assert for is not on the def chain of the name compared. Instead
4849 it is in the set of use stmts. */
4850 if ((comp_code == NE_EXPR
4851 || comp_code == EQ_EXPR)
4852 && TREE_CODE (val) == INTEGER_CST)
4854 imm_use_iterator ui;
4855 gimple use_stmt;
4856 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
4858 /* Cut off to use-stmts that are in the predecessor. */
4859 if (gimple_bb (use_stmt) != e->src)
4860 continue;
4862 if (!is_gimple_assign (use_stmt))
4863 continue;
4865 enum tree_code code = gimple_assign_rhs_code (use_stmt);
4866 if (code != PLUS_EXPR
4867 && code != MINUS_EXPR)
4868 continue;
4870 tree cst = gimple_assign_rhs2 (use_stmt);
4871 if (TREE_CODE (cst) != INTEGER_CST)
4872 continue;
4874 tree name2 = gimple_assign_lhs (use_stmt);
4875 if (live_on_edge (e, name2))
4877 cst = int_const_binop (code, val, cst);
4878 register_new_assert_for (name2, name2, comp_code, cst,
4879 NULL, e, bsi);
4880 retval = true;
4885 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
4886 && TREE_CODE (val) == INTEGER_CST)
4888 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4889 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
4890 tree val2 = NULL_TREE;
4891 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
4892 wide_int mask = wi::zero (prec);
4893 unsigned int nprec = prec;
4894 enum tree_code rhs_code = ERROR_MARK;
4896 if (is_gimple_assign (def_stmt))
4897 rhs_code = gimple_assign_rhs_code (def_stmt);
4899 /* Add asserts for NAME cmp CST and NAME being defined
4900 as NAME = (int) NAME2. */
4901 if (!TYPE_UNSIGNED (TREE_TYPE (val))
4902 && (comp_code == LE_EXPR || comp_code == LT_EXPR
4903 || comp_code == GT_EXPR || comp_code == GE_EXPR)
4904 && gimple_assign_cast_p (def_stmt))
4906 name2 = gimple_assign_rhs1 (def_stmt);
4907 if (CONVERT_EXPR_CODE_P (rhs_code)
4908 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4909 && TYPE_UNSIGNED (TREE_TYPE (name2))
4910 && prec == TYPE_PRECISION (TREE_TYPE (name2))
4911 && (comp_code == LE_EXPR || comp_code == GT_EXPR
4912 || !tree_int_cst_equal (val,
4913 TYPE_MIN_VALUE (TREE_TYPE (val))))
4914 && live_on_edge (e, name2)
4915 && !has_single_use (name2))
4917 tree tmp, cst;
4918 enum tree_code new_comp_code = comp_code;
4920 cst = fold_convert (TREE_TYPE (name2),
4921 TYPE_MIN_VALUE (TREE_TYPE (val)));
4922 /* Build an expression for the range test. */
4923 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
4924 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
4925 fold_convert (TREE_TYPE (name2), val));
4926 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4928 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
4929 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
4930 build_int_cst (TREE_TYPE (name2), 1));
4933 if (dump_file)
4935 fprintf (dump_file, "Adding assert for ");
4936 print_generic_expr (dump_file, name2, 0);
4937 fprintf (dump_file, " from ");
4938 print_generic_expr (dump_file, tmp, 0);
4939 fprintf (dump_file, "\n");
4942 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
4943 e, bsi);
4945 retval = true;
4949 /* Add asserts for NAME cmp CST and NAME being defined as
4950 NAME = NAME2 >> CST2.
4952 Extract CST2 from the right shift. */
4953 if (rhs_code == RSHIFT_EXPR)
4955 name2 = gimple_assign_rhs1 (def_stmt);
4956 cst2 = gimple_assign_rhs2 (def_stmt);
4957 if (TREE_CODE (name2) == SSA_NAME
4958 && tree_fits_uhwi_p (cst2)
4959 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4960 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
4961 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
4962 && live_on_edge (e, name2)
4963 && !has_single_use (name2))
4965 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
4966 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
4969 if (val2 != NULL_TREE
4970 && TREE_CODE (val2) == INTEGER_CST
4971 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
4972 TREE_TYPE (val),
4973 val2, cst2), val))
4975 enum tree_code new_comp_code = comp_code;
4976 tree tmp, new_val;
4978 tmp = name2;
4979 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
4981 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
4983 tree type = build_nonstandard_integer_type (prec, 1);
4984 tmp = build1 (NOP_EXPR, type, name2);
4985 val2 = fold_convert (type, val2);
4987 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
4988 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
4989 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
4991 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4993 wide_int minval
4994 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
4995 new_val = val2;
4996 if (minval == new_val)
4997 new_val = NULL_TREE;
4999 else
5001 wide_int maxval
5002 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5003 mask |= val2;
5004 if (mask == maxval)
5005 new_val = NULL_TREE;
5006 else
5007 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5010 if (new_val)
5012 if (dump_file)
5014 fprintf (dump_file, "Adding assert for ");
5015 print_generic_expr (dump_file, name2, 0);
5016 fprintf (dump_file, " from ");
5017 print_generic_expr (dump_file, tmp, 0);
5018 fprintf (dump_file, "\n");
5021 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5022 NULL, e, bsi);
5023 retval = true;
5027 /* Add asserts for NAME cmp CST and NAME being defined as
5028 NAME = NAME2 & CST2.
5030 Extract CST2 from the and.
5032 Also handle
5033 NAME = (unsigned) NAME2;
5034 casts where NAME's type is unsigned and has smaller precision
5035 than NAME2's type as if it was NAME = NAME2 & MASK. */
5036 names[0] = NULL_TREE;
5037 names[1] = NULL_TREE;
5038 cst2 = NULL_TREE;
5039 if (rhs_code == BIT_AND_EXPR
5040 || (CONVERT_EXPR_CODE_P (rhs_code)
5041 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5042 && TYPE_UNSIGNED (TREE_TYPE (val))
5043 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5044 > prec
5045 && !retval))
5047 name2 = gimple_assign_rhs1 (def_stmt);
5048 if (rhs_code == BIT_AND_EXPR)
5049 cst2 = gimple_assign_rhs2 (def_stmt);
5050 else
5052 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5053 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5055 if (TREE_CODE (name2) == SSA_NAME
5056 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5057 && TREE_CODE (cst2) == INTEGER_CST
5058 && !integer_zerop (cst2)
5059 && (nprec > 1
5060 || TYPE_UNSIGNED (TREE_TYPE (val))))
5062 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5063 if (gimple_assign_cast_p (def_stmt2))
5065 names[1] = gimple_assign_rhs1 (def_stmt2);
5066 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5067 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5068 || (TYPE_PRECISION (TREE_TYPE (name2))
5069 != TYPE_PRECISION (TREE_TYPE (names[1])))
5070 || !live_on_edge (e, names[1])
5071 || has_single_use (names[1]))
5072 names[1] = NULL_TREE;
5074 if (live_on_edge (e, name2)
5075 && !has_single_use (name2))
5076 names[0] = name2;
5079 if (names[0] || names[1])
5081 wide_int minv, maxv, valv, cst2v;
5082 wide_int tem, sgnbit;
5083 bool valid_p = false, valn, cst2n;
5084 enum tree_code ccode = comp_code;
5086 valv = wide_int::from (val, nprec, UNSIGNED);
5087 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5088 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5089 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5090 /* If CST2 doesn't have most significant bit set,
5091 but VAL is negative, we have comparison like
5092 if ((x & 0x123) > -4) (always true). Just give up. */
5093 if (!cst2n && valn)
5094 ccode = ERROR_MARK;
5095 if (cst2n)
5096 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5097 else
5098 sgnbit = wi::zero (nprec);
5099 minv = valv & cst2v;
5100 switch (ccode)
5102 case EQ_EXPR:
5103 /* Minimum unsigned value for equality is VAL & CST2
5104 (should be equal to VAL, otherwise we probably should
5105 have folded the comparison into false) and
5106 maximum unsigned value is VAL | ~CST2. */
5107 maxv = valv | ~cst2v;
5108 valid_p = true;
5109 break;
5111 case NE_EXPR:
5112 tem = valv | ~cst2v;
5113 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5114 if (valv == 0)
5116 cst2n = false;
5117 sgnbit = wi::zero (nprec);
5118 goto gt_expr;
5120 /* If (VAL | ~CST2) is all ones, handle it as
5121 (X & CST2) < VAL. */
5122 if (tem == -1)
5124 cst2n = false;
5125 valn = false;
5126 sgnbit = wi::zero (nprec);
5127 goto lt_expr;
5129 if (!cst2n && wi::neg_p (cst2v))
5130 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5131 if (sgnbit != 0)
5133 if (valv == sgnbit)
5135 cst2n = true;
5136 valn = true;
5137 goto gt_expr;
5139 if (tem == wi::mask (nprec - 1, false, nprec))
5141 cst2n = true;
5142 goto lt_expr;
5144 if (!cst2n)
5145 sgnbit = wi::zero (nprec);
5147 break;
5149 case GE_EXPR:
5150 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5151 is VAL and maximum unsigned value is ~0. For signed
5152 comparison, if CST2 doesn't have most significant bit
5153 set, handle it similarly. If CST2 has MSB set,
5154 the minimum is the same, and maximum is ~0U/2. */
5155 if (minv != valv)
5157 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5158 VAL. */
5159 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5160 if (minv == valv)
5161 break;
5163 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5164 valid_p = true;
5165 break;
5167 case GT_EXPR:
5168 gt_expr:
5169 /* Find out smallest MINV where MINV > VAL
5170 && (MINV & CST2) == MINV, if any. If VAL is signed and
5171 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5172 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5173 if (minv == valv)
5174 break;
5175 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5176 valid_p = true;
5177 break;
5179 case LE_EXPR:
5180 /* Minimum unsigned value for <= is 0 and maximum
5181 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5182 Otherwise, find smallest VAL2 where VAL2 > VAL
5183 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5184 as maximum.
5185 For signed comparison, if CST2 doesn't have most
5186 significant bit set, handle it similarly. If CST2 has
5187 MSB set, the maximum is the same and minimum is INT_MIN. */
5188 if (minv == valv)
5189 maxv = valv;
5190 else
5192 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5193 if (maxv == valv)
5194 break;
5195 maxv -= 1;
5197 maxv |= ~cst2v;
5198 minv = sgnbit;
5199 valid_p = true;
5200 break;
5202 case LT_EXPR:
5203 lt_expr:
5204 /* Minimum unsigned value for < is 0 and maximum
5205 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5206 Otherwise, find smallest VAL2 where VAL2 > VAL
5207 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5208 as maximum.
5209 For signed comparison, if CST2 doesn't have most
5210 significant bit set, handle it similarly. If CST2 has
5211 MSB set, the maximum is the same and minimum is INT_MIN. */
5212 if (minv == valv)
5214 if (valv == sgnbit)
5215 break;
5216 maxv = valv;
5218 else
5220 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5221 if (maxv == valv)
5222 break;
5224 maxv -= 1;
5225 maxv |= ~cst2v;
5226 minv = sgnbit;
5227 valid_p = true;
5228 break;
5230 default:
5231 break;
5233 if (valid_p
5234 && (maxv - minv) != -1)
5236 tree tmp, new_val, type;
5237 int i;
5239 for (i = 0; i < 2; i++)
5240 if (names[i])
5242 wide_int maxv2 = maxv;
5243 tmp = names[i];
5244 type = TREE_TYPE (names[i]);
5245 if (!TYPE_UNSIGNED (type))
5247 type = build_nonstandard_integer_type (nprec, 1);
5248 tmp = build1 (NOP_EXPR, type, names[i]);
5250 if (minv != 0)
5252 tmp = build2 (PLUS_EXPR, type, tmp,
5253 wide_int_to_tree (type, -minv));
5254 maxv2 = maxv - minv;
5256 new_val = wide_int_to_tree (type, maxv2);
5258 if (dump_file)
5260 fprintf (dump_file, "Adding assert for ");
5261 print_generic_expr (dump_file, names[i], 0);
5262 fprintf (dump_file, " from ");
5263 print_generic_expr (dump_file, tmp, 0);
5264 fprintf (dump_file, "\n");
5267 register_new_assert_for (names[i], tmp, LE_EXPR,
5268 new_val, NULL, e, bsi);
5269 retval = true;
5275 return retval;
5278 /* OP is an operand of a truth value expression which is known to have
5279 a particular value. Register any asserts for OP and for any
5280 operands in OP's defining statement.
5282 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5283 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5285 static bool
5286 register_edge_assert_for_1 (tree op, enum tree_code code,
5287 edge e, gimple_stmt_iterator bsi)
5289 bool retval = false;
5290 gimple op_def;
5291 tree val;
5292 enum tree_code rhs_code;
5294 /* We only care about SSA_NAMEs. */
5295 if (TREE_CODE (op) != SSA_NAME)
5296 return false;
5298 /* We know that OP will have a zero or nonzero value. If OP is used
5299 more than once go ahead and register an assert for OP. */
5300 if (live_on_edge (e, op)
5301 && !has_single_use (op))
5303 val = build_int_cst (TREE_TYPE (op), 0);
5304 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5305 retval = true;
5308 /* Now look at how OP is set. If it's set from a comparison,
5309 a truth operation or some bit operations, then we may be able
5310 to register information about the operands of that assignment. */
5311 op_def = SSA_NAME_DEF_STMT (op);
5312 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5313 return retval;
5315 rhs_code = gimple_assign_rhs_code (op_def);
5317 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5319 bool invert = (code == EQ_EXPR ? true : false);
5320 tree op0 = gimple_assign_rhs1 (op_def);
5321 tree op1 = gimple_assign_rhs2 (op_def);
5323 if (TREE_CODE (op0) == SSA_NAME)
5324 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5325 invert);
5326 if (TREE_CODE (op1) == SSA_NAME)
5327 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5328 invert);
5330 else if ((code == NE_EXPR
5331 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5332 || (code == EQ_EXPR
5333 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5335 /* Recurse on each operand. */
5336 tree op0 = gimple_assign_rhs1 (op_def);
5337 tree op1 = gimple_assign_rhs2 (op_def);
5338 if (TREE_CODE (op0) == SSA_NAME
5339 && has_single_use (op0))
5340 retval |= register_edge_assert_for_1 (op0, code, e, bsi);
5341 if (TREE_CODE (op1) == SSA_NAME
5342 && has_single_use (op1))
5343 retval |= register_edge_assert_for_1 (op1, code, e, bsi);
5345 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5346 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5348 /* Recurse, flipping CODE. */
5349 code = invert_tree_comparison (code, false);
5350 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5351 code, e, bsi);
5353 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5355 /* Recurse through the copy. */
5356 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5357 code, e, bsi);
5359 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5361 /* Recurse through the type conversion, unless it is a narrowing
5362 conversion or conversion from non-integral type. */
5363 tree rhs = gimple_assign_rhs1 (op_def);
5364 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5365 && (TYPE_PRECISION (TREE_TYPE (rhs))
5366 <= TYPE_PRECISION (TREE_TYPE (op))))
5367 retval |= register_edge_assert_for_1 (rhs, code, e, bsi);
5370 return retval;
5373 /* Try to register an edge assertion for SSA name NAME on edge E for
5374 the condition COND contributing to the conditional jump pointed to by SI.
5375 Return true if an assertion for NAME could be registered. */
5377 static bool
5378 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5379 enum tree_code cond_code, tree cond_op0,
5380 tree cond_op1)
5382 tree val;
5383 enum tree_code comp_code;
5384 bool retval = false;
5385 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5387 /* Do not attempt to infer anything in names that flow through
5388 abnormal edges. */
5389 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5390 return false;
5392 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5393 cond_op0, cond_op1,
5394 is_else_edge,
5395 &comp_code, &val))
5396 return false;
5398 /* Register ASSERT_EXPRs for name. */
5399 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5400 cond_op1, is_else_edge);
5403 /* If COND is effectively an equality test of an SSA_NAME against
5404 the value zero or one, then we may be able to assert values
5405 for SSA_NAMEs which flow into COND. */
5407 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5408 statement of NAME we can assert both operands of the BIT_AND_EXPR
5409 have nonzero value. */
5410 if (((comp_code == EQ_EXPR && integer_onep (val))
5411 || (comp_code == NE_EXPR && integer_zerop (val))))
5413 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5415 if (is_gimple_assign (def_stmt)
5416 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5418 tree op0 = gimple_assign_rhs1 (def_stmt);
5419 tree op1 = gimple_assign_rhs2 (def_stmt);
5420 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5421 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5425 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5426 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5427 have zero value. */
5428 if (((comp_code == EQ_EXPR && integer_zerop (val))
5429 || (comp_code == NE_EXPR && integer_onep (val))))
5431 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5433 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5434 necessarily zero value, or if type-precision is one. */
5435 if (is_gimple_assign (def_stmt)
5436 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5437 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5438 || comp_code == EQ_EXPR)))
5440 tree op0 = gimple_assign_rhs1 (def_stmt);
5441 tree op1 = gimple_assign_rhs2 (def_stmt);
5442 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5443 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5447 return retval;
5451 /* Determine whether the outgoing edges of BB should receive an
5452 ASSERT_EXPR for each of the operands of BB's LAST statement.
5453 The last statement of BB must be a COND_EXPR.
5455 If any of the sub-graphs rooted at BB have an interesting use of
5456 the predicate operands, an assert location node is added to the
5457 list of assertions for the corresponding operands. */
5459 static bool
5460 find_conditional_asserts (basic_block bb, gimple last)
5462 bool need_assert;
5463 gimple_stmt_iterator bsi;
5464 tree op;
5465 edge_iterator ei;
5466 edge e;
5467 ssa_op_iter iter;
5469 need_assert = false;
5470 bsi = gsi_for_stmt (last);
5472 /* Look for uses of the operands in each of the sub-graphs
5473 rooted at BB. We need to check each of the outgoing edges
5474 separately, so that we know what kind of ASSERT_EXPR to
5475 insert. */
5476 FOR_EACH_EDGE (e, ei, bb->succs)
5478 if (e->dest == bb)
5479 continue;
5481 /* Register the necessary assertions for each operand in the
5482 conditional predicate. */
5483 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5485 need_assert |= register_edge_assert_for (op, e, bsi,
5486 gimple_cond_code (last),
5487 gimple_cond_lhs (last),
5488 gimple_cond_rhs (last));
5492 return need_assert;
5495 struct case_info
5497 tree expr;
5498 basic_block bb;
5501 /* Compare two case labels sorting first by the destination bb index
5502 and then by the case value. */
5504 static int
5505 compare_case_labels (const void *p1, const void *p2)
5507 const struct case_info *ci1 = (const struct case_info *) p1;
5508 const struct case_info *ci2 = (const struct case_info *) p2;
5509 int idx1 = ci1->bb->index;
5510 int idx2 = ci2->bb->index;
5512 if (idx1 < idx2)
5513 return -1;
5514 else if (idx1 == idx2)
5516 /* Make sure the default label is first in a group. */
5517 if (!CASE_LOW (ci1->expr))
5518 return -1;
5519 else if (!CASE_LOW (ci2->expr))
5520 return 1;
5521 else
5522 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5523 CASE_LOW (ci2->expr));
5525 else
5526 return 1;
5529 /* Determine whether the outgoing edges of BB should receive an
5530 ASSERT_EXPR for each of the operands of BB's LAST statement.
5531 The last statement of BB must be a SWITCH_EXPR.
5533 If any of the sub-graphs rooted at BB have an interesting use of
5534 the predicate operands, an assert location node is added to the
5535 list of assertions for the corresponding operands. */
5537 static bool
5538 find_switch_asserts (basic_block bb, gimple last)
5540 bool need_assert;
5541 gimple_stmt_iterator bsi;
5542 tree op;
5543 edge e;
5544 struct case_info *ci;
5545 size_t n = gimple_switch_num_labels (last);
5546 #if GCC_VERSION >= 4000
5547 unsigned int idx;
5548 #else
5549 /* Work around GCC 3.4 bug (PR 37086). */
5550 volatile unsigned int idx;
5551 #endif
5553 need_assert = false;
5554 bsi = gsi_for_stmt (last);
5555 op = gimple_switch_index (last);
5556 if (TREE_CODE (op) != SSA_NAME)
5557 return false;
5559 /* Build a vector of case labels sorted by destination label. */
5560 ci = XNEWVEC (struct case_info, n);
5561 for (idx = 0; idx < n; ++idx)
5563 ci[idx].expr = gimple_switch_label (last, idx);
5564 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5566 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5568 for (idx = 0; idx < n; ++idx)
5570 tree min, max;
5571 tree cl = ci[idx].expr;
5572 basic_block cbb = ci[idx].bb;
5574 min = CASE_LOW (cl);
5575 max = CASE_HIGH (cl);
5577 /* If there are multiple case labels with the same destination
5578 we need to combine them to a single value range for the edge. */
5579 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5581 /* Skip labels until the last of the group. */
5582 do {
5583 ++idx;
5584 } while (idx < n && cbb == ci[idx].bb);
5585 --idx;
5587 /* Pick up the maximum of the case label range. */
5588 if (CASE_HIGH (ci[idx].expr))
5589 max = CASE_HIGH (ci[idx].expr);
5590 else
5591 max = CASE_LOW (ci[idx].expr);
5594 /* Nothing to do if the range includes the default label until we
5595 can register anti-ranges. */
5596 if (min == NULL_TREE)
5597 continue;
5599 /* Find the edge to register the assert expr on. */
5600 e = find_edge (bb, cbb);
5602 /* Register the necessary assertions for the operand in the
5603 SWITCH_EXPR. */
5604 need_assert |= register_edge_assert_for (op, e, bsi,
5605 max ? GE_EXPR : EQ_EXPR,
5607 fold_convert (TREE_TYPE (op),
5608 min));
5609 if (max)
5611 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5613 fold_convert (TREE_TYPE (op),
5614 max));
5618 XDELETEVEC (ci);
5619 return need_assert;
5623 /* Traverse all the statements in block BB looking for statements that
5624 may generate useful assertions for the SSA names in their operand.
5625 If a statement produces a useful assertion A for name N_i, then the
5626 list of assertions already generated for N_i is scanned to
5627 determine if A is actually needed.
5629 If N_i already had the assertion A at a location dominating the
5630 current location, then nothing needs to be done. Otherwise, the
5631 new location for A is recorded instead.
5633 1- For every statement S in BB, all the variables used by S are
5634 added to bitmap FOUND_IN_SUBGRAPH.
5636 2- If statement S uses an operand N in a way that exposes a known
5637 value range for N, then if N was not already generated by an
5638 ASSERT_EXPR, create a new assert location for N. For instance,
5639 if N is a pointer and the statement dereferences it, we can
5640 assume that N is not NULL.
5642 3- COND_EXPRs are a special case of #2. We can derive range
5643 information from the predicate but need to insert different
5644 ASSERT_EXPRs for each of the sub-graphs rooted at the
5645 conditional block. If the last statement of BB is a conditional
5646 expression of the form 'X op Y', then
5648 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5650 b) If the conditional is the only entry point to the sub-graph
5651 corresponding to the THEN_CLAUSE, recurse into it. On
5652 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5653 an ASSERT_EXPR is added for the corresponding variable.
5655 c) Repeat step (b) on the ELSE_CLAUSE.
5657 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5659 For instance,
5661 if (a == 9)
5662 b = a;
5663 else
5664 b = c + 1;
5666 In this case, an assertion on the THEN clause is useful to
5667 determine that 'a' is always 9 on that edge. However, an assertion
5668 on the ELSE clause would be unnecessary.
5670 4- If BB does not end in a conditional expression, then we recurse
5671 into BB's dominator children.
5673 At the end of the recursive traversal, every SSA name will have a
5674 list of locations where ASSERT_EXPRs should be added. When a new
5675 location for name N is found, it is registered by calling
5676 register_new_assert_for. That function keeps track of all the
5677 registered assertions to prevent adding unnecessary assertions.
5678 For instance, if a pointer P_4 is dereferenced more than once in a
5679 dominator tree, only the location dominating all the dereference of
5680 P_4 will receive an ASSERT_EXPR.
5682 If this function returns true, then it means that there are names
5683 for which we need to generate ASSERT_EXPRs. Those assertions are
5684 inserted by process_assert_insertions. */
5686 static bool
5687 find_assert_locations_1 (basic_block bb, sbitmap live)
5689 gimple_stmt_iterator si;
5690 gimple last;
5691 bool need_assert;
5693 need_assert = false;
5694 last = last_stmt (bb);
5696 /* If BB's last statement is a conditional statement involving integer
5697 operands, determine if we need to add ASSERT_EXPRs. */
5698 if (last
5699 && gimple_code (last) == GIMPLE_COND
5700 && !fp_predicate (last)
5701 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5702 need_assert |= find_conditional_asserts (bb, last);
5704 /* If BB's last statement is a switch statement involving integer
5705 operands, determine if we need to add ASSERT_EXPRs. */
5706 if (last
5707 && gimple_code (last) == GIMPLE_SWITCH
5708 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5709 need_assert |= find_switch_asserts (bb, last);
5711 /* Traverse all the statements in BB marking used names and looking
5712 for statements that may infer assertions for their used operands. */
5713 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5715 gimple stmt;
5716 tree op;
5717 ssa_op_iter i;
5719 stmt = gsi_stmt (si);
5721 if (is_gimple_debug (stmt))
5722 continue;
5724 /* See if we can derive an assertion for any of STMT's operands. */
5725 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5727 tree value;
5728 enum tree_code comp_code;
5730 /* If op is not live beyond this stmt, do not bother to insert
5731 asserts for it. */
5732 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5733 continue;
5735 /* If OP is used in such a way that we can infer a value
5736 range for it, and we don't find a previous assertion for
5737 it, create a new assertion location node for OP. */
5738 if (infer_value_range (stmt, op, &comp_code, &value))
5740 /* If we are able to infer a nonzero value range for OP,
5741 then walk backwards through the use-def chain to see if OP
5742 was set via a typecast.
5744 If so, then we can also infer a nonzero value range
5745 for the operand of the NOP_EXPR. */
5746 if (comp_code == NE_EXPR && integer_zerop (value))
5748 tree t = op;
5749 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5751 while (is_gimple_assign (def_stmt)
5752 && gimple_assign_rhs_code (def_stmt) == NOP_EXPR
5753 && TREE_CODE
5754 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5755 && POINTER_TYPE_P
5756 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
5758 t = gimple_assign_rhs1 (def_stmt);
5759 def_stmt = SSA_NAME_DEF_STMT (t);
5761 /* Note we want to register the assert for the
5762 operand of the NOP_EXPR after SI, not after the
5763 conversion. */
5764 if (! has_single_use (t))
5766 register_new_assert_for (t, t, comp_code, value,
5767 bb, NULL, si);
5768 need_assert = true;
5773 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
5774 need_assert = true;
5778 /* Update live. */
5779 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5780 bitmap_set_bit (live, SSA_NAME_VERSION (op));
5781 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
5782 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
5785 /* Traverse all PHI nodes in BB, updating live. */
5786 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5788 use_operand_p arg_p;
5789 ssa_op_iter i;
5790 gimple phi = gsi_stmt (si);
5791 tree res = gimple_phi_result (phi);
5793 if (virtual_operand_p (res))
5794 continue;
5796 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
5798 tree arg = USE_FROM_PTR (arg_p);
5799 if (TREE_CODE (arg) == SSA_NAME)
5800 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
5803 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
5806 return need_assert;
5809 /* Do an RPO walk over the function computing SSA name liveness
5810 on-the-fly and deciding on assert expressions to insert.
5811 Returns true if there are assert expressions to be inserted. */
5813 static bool
5814 find_assert_locations (void)
5816 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
5817 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
5818 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
5819 int rpo_cnt, i;
5820 bool need_asserts;
5822 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
5823 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
5824 for (i = 0; i < rpo_cnt; ++i)
5825 bb_rpo[rpo[i]] = i;
5827 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
5828 the order we compute liveness and insert asserts we otherwise
5829 fail to insert asserts into the loop latch. */
5830 loop_p loop;
5831 FOR_EACH_LOOP (loop, 0)
5833 i = loop->latch->index;
5834 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
5835 for (gimple_stmt_iterator gsi = gsi_start_phis (loop->header);
5836 !gsi_end_p (gsi); gsi_next (&gsi))
5838 gimple phi = gsi_stmt (gsi);
5839 if (virtual_operand_p (gimple_phi_result (phi)))
5840 continue;
5841 tree arg = gimple_phi_arg_def (phi, j);
5842 if (TREE_CODE (arg) == SSA_NAME)
5844 if (live[i] == NULL)
5846 live[i] = sbitmap_alloc (num_ssa_names);
5847 bitmap_clear (live[i]);
5849 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
5854 need_asserts = false;
5855 for (i = rpo_cnt - 1; i >= 0; --i)
5857 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
5858 edge e;
5859 edge_iterator ei;
5861 if (!live[rpo[i]])
5863 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
5864 bitmap_clear (live[rpo[i]]);
5867 /* Process BB and update the live information with uses in
5868 this block. */
5869 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
5871 /* Merge liveness into the predecessor blocks and free it. */
5872 if (!bitmap_empty_p (live[rpo[i]]))
5874 int pred_rpo = i;
5875 FOR_EACH_EDGE (e, ei, bb->preds)
5877 int pred = e->src->index;
5878 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
5879 continue;
5881 if (!live[pred])
5883 live[pred] = sbitmap_alloc (num_ssa_names);
5884 bitmap_clear (live[pred]);
5886 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
5888 if (bb_rpo[pred] < pred_rpo)
5889 pred_rpo = bb_rpo[pred];
5892 /* Record the RPO number of the last visited block that needs
5893 live information from this block. */
5894 last_rpo[rpo[i]] = pred_rpo;
5896 else
5898 sbitmap_free (live[rpo[i]]);
5899 live[rpo[i]] = NULL;
5902 /* We can free all successors live bitmaps if all their
5903 predecessors have been visited already. */
5904 FOR_EACH_EDGE (e, ei, bb->succs)
5905 if (last_rpo[e->dest->index] == i
5906 && live[e->dest->index])
5908 sbitmap_free (live[e->dest->index]);
5909 live[e->dest->index] = NULL;
5913 XDELETEVEC (rpo);
5914 XDELETEVEC (bb_rpo);
5915 XDELETEVEC (last_rpo);
5916 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
5917 if (live[i])
5918 sbitmap_free (live[i]);
5919 XDELETEVEC (live);
5921 return need_asserts;
5924 /* Create an ASSERT_EXPR for NAME and insert it in the location
5925 indicated by LOC. Return true if we made any edge insertions. */
5927 static bool
5928 process_assert_insertions_for (tree name, assert_locus_t loc)
5930 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5931 gimple stmt;
5932 tree cond;
5933 gimple assert_stmt;
5934 edge_iterator ei;
5935 edge e;
5937 /* If we have X <=> X do not insert an assert expr for that. */
5938 if (loc->expr == loc->val)
5939 return false;
5941 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
5942 assert_stmt = build_assert_expr_for (cond, name);
5943 if (loc->e)
5945 /* We have been asked to insert the assertion on an edge. This
5946 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5947 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
5948 || (gimple_code (gsi_stmt (loc->si))
5949 == GIMPLE_SWITCH));
5951 gsi_insert_on_edge (loc->e, assert_stmt);
5952 return true;
5955 /* Otherwise, we can insert right after LOC->SI iff the
5956 statement must not be the last statement in the block. */
5957 stmt = gsi_stmt (loc->si);
5958 if (!stmt_ends_bb_p (stmt))
5960 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
5961 return false;
5964 /* If STMT must be the last statement in BB, we can only insert new
5965 assertions on the non-abnormal edge out of BB. Note that since
5966 STMT is not control flow, there may only be one non-abnormal edge
5967 out of BB. */
5968 FOR_EACH_EDGE (e, ei, loc->bb->succs)
5969 if (!(e->flags & EDGE_ABNORMAL))
5971 gsi_insert_on_edge (e, assert_stmt);
5972 return true;
5975 gcc_unreachable ();
5979 /* Process all the insertions registered for every name N_i registered
5980 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5981 found in ASSERTS_FOR[i]. */
5983 static void
5984 process_assert_insertions (void)
5986 unsigned i;
5987 bitmap_iterator bi;
5988 bool update_edges_p = false;
5989 int num_asserts = 0;
5991 if (dump_file && (dump_flags & TDF_DETAILS))
5992 dump_all_asserts (dump_file);
5994 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
5996 assert_locus_t loc = asserts_for[i];
5997 gcc_assert (loc);
5999 while (loc)
6001 assert_locus_t next = loc->next;
6002 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6003 free (loc);
6004 loc = next;
6005 num_asserts++;
6009 if (update_edges_p)
6010 gsi_commit_edge_inserts ();
6012 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6013 num_asserts);
6017 /* Traverse the flowgraph looking for conditional jumps to insert range
6018 expressions. These range expressions are meant to provide information
6019 to optimizations that need to reason in terms of value ranges. They
6020 will not be expanded into RTL. For instance, given:
6022 x = ...
6023 y = ...
6024 if (x < y)
6025 y = x - 2;
6026 else
6027 x = y + 3;
6029 this pass will transform the code into:
6031 x = ...
6032 y = ...
6033 if (x < y)
6035 x = ASSERT_EXPR <x, x < y>
6036 y = x - 2
6038 else
6040 y = ASSERT_EXPR <y, x >= y>
6041 x = y + 3
6044 The idea is that once copy and constant propagation have run, other
6045 optimizations will be able to determine what ranges of values can 'x'
6046 take in different paths of the code, simply by checking the reaching
6047 definition of 'x'. */
6049 static void
6050 insert_range_assertions (void)
6052 need_assert_for = BITMAP_ALLOC (NULL);
6053 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6055 calculate_dominance_info (CDI_DOMINATORS);
6057 if (find_assert_locations ())
6059 process_assert_insertions ();
6060 update_ssa (TODO_update_ssa_no_phi);
6063 if (dump_file && (dump_flags & TDF_DETAILS))
6065 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6066 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6069 free (asserts_for);
6070 BITMAP_FREE (need_assert_for);
6073 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6074 and "struct" hacks. If VRP can determine that the
6075 array subscript is a constant, check if it is outside valid
6076 range. If the array subscript is a RANGE, warn if it is
6077 non-overlapping with valid range.
6078 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6080 static void
6081 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6083 value_range_t* vr = NULL;
6084 tree low_sub, up_sub;
6085 tree low_bound, up_bound, up_bound_p1;
6086 tree base;
6088 if (TREE_NO_WARNING (ref))
6089 return;
6091 low_sub = up_sub = TREE_OPERAND (ref, 1);
6092 up_bound = array_ref_up_bound (ref);
6094 /* Can not check flexible arrays. */
6095 if (!up_bound
6096 || TREE_CODE (up_bound) != INTEGER_CST)
6097 return;
6099 /* Accesses to trailing arrays via pointers may access storage
6100 beyond the types array bounds. */
6101 base = get_base_address (ref);
6102 if (base && TREE_CODE (base) == MEM_REF)
6104 tree cref, next = NULL_TREE;
6106 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6107 return;
6109 cref = TREE_OPERAND (ref, 0);
6110 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6111 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6112 next && TREE_CODE (next) != FIELD_DECL;
6113 next = DECL_CHAIN (next))
6116 /* If this is the last field in a struct type or a field in a
6117 union type do not warn. */
6118 if (!next)
6119 return;
6122 low_bound = array_ref_low_bound (ref);
6123 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6124 build_int_cst (TREE_TYPE (up_bound), 1));
6126 if (TREE_CODE (low_sub) == SSA_NAME)
6128 vr = get_value_range (low_sub);
6129 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6131 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6132 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6136 if (vr && vr->type == VR_ANTI_RANGE)
6138 if (TREE_CODE (up_sub) == INTEGER_CST
6139 && tree_int_cst_lt (up_bound, up_sub)
6140 && TREE_CODE (low_sub) == INTEGER_CST
6141 && tree_int_cst_lt (low_sub, low_bound))
6143 warning_at (location, OPT_Warray_bounds,
6144 "array subscript is outside array bounds");
6145 TREE_NO_WARNING (ref) = 1;
6148 else if (TREE_CODE (up_sub) == INTEGER_CST
6149 && (ignore_off_by_one
6150 ? (tree_int_cst_lt (up_bound, up_sub)
6151 && !tree_int_cst_equal (up_bound_p1, up_sub))
6152 : (tree_int_cst_lt (up_bound, up_sub)
6153 || tree_int_cst_equal (up_bound_p1, up_sub))))
6155 if (dump_file && (dump_flags & TDF_DETAILS))
6157 fprintf (dump_file, "Array bound warning for ");
6158 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6159 fprintf (dump_file, "\n");
6161 warning_at (location, OPT_Warray_bounds,
6162 "array subscript is above array bounds");
6163 TREE_NO_WARNING (ref) = 1;
6165 else if (TREE_CODE (low_sub) == INTEGER_CST
6166 && tree_int_cst_lt (low_sub, low_bound))
6168 if (dump_file && (dump_flags & TDF_DETAILS))
6170 fprintf (dump_file, "Array bound warning for ");
6171 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6172 fprintf (dump_file, "\n");
6174 warning_at (location, OPT_Warray_bounds,
6175 "array subscript is below array bounds");
6176 TREE_NO_WARNING (ref) = 1;
6180 /* Searches if the expr T, located at LOCATION computes
6181 address of an ARRAY_REF, and call check_array_ref on it. */
6183 static void
6184 search_for_addr_array (tree t, location_t location)
6186 while (TREE_CODE (t) == SSA_NAME)
6188 gimple g = SSA_NAME_DEF_STMT (t);
6190 if (gimple_code (g) != GIMPLE_ASSIGN)
6191 return;
6193 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6194 != GIMPLE_SINGLE_RHS)
6195 return;
6197 t = gimple_assign_rhs1 (g);
6201 /* We are only interested in addresses of ARRAY_REF's. */
6202 if (TREE_CODE (t) != ADDR_EXPR)
6203 return;
6205 /* Check each ARRAY_REFs in the reference chain. */
6208 if (TREE_CODE (t) == ARRAY_REF)
6209 check_array_ref (location, t, true /*ignore_off_by_one*/);
6211 t = TREE_OPERAND (t, 0);
6213 while (handled_component_p (t));
6215 if (TREE_CODE (t) == MEM_REF
6216 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6217 && !TREE_NO_WARNING (t))
6219 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6220 tree low_bound, up_bound, el_sz;
6221 offset_int idx;
6222 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6223 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6224 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6225 return;
6227 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6228 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6229 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6230 if (!low_bound
6231 || TREE_CODE (low_bound) != INTEGER_CST
6232 || !up_bound
6233 || TREE_CODE (up_bound) != INTEGER_CST
6234 || !el_sz
6235 || TREE_CODE (el_sz) != INTEGER_CST)
6236 return;
6238 idx = mem_ref_offset (t);
6239 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6240 if (wi::lts_p (idx, 0))
6242 if (dump_file && (dump_flags & TDF_DETAILS))
6244 fprintf (dump_file, "Array bound warning for ");
6245 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6246 fprintf (dump_file, "\n");
6248 warning_at (location, OPT_Warray_bounds,
6249 "array subscript is below array bounds");
6250 TREE_NO_WARNING (t) = 1;
6252 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6253 - wi::to_offset (low_bound) + 1)))
6255 if (dump_file && (dump_flags & TDF_DETAILS))
6257 fprintf (dump_file, "Array bound warning for ");
6258 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6259 fprintf (dump_file, "\n");
6261 warning_at (location, OPT_Warray_bounds,
6262 "array subscript is above array bounds");
6263 TREE_NO_WARNING (t) = 1;
6268 /* walk_tree() callback that checks if *TP is
6269 an ARRAY_REF inside an ADDR_EXPR (in which an array
6270 subscript one outside the valid range is allowed). Call
6271 check_array_ref for each ARRAY_REF found. The location is
6272 passed in DATA. */
6274 static tree
6275 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6277 tree t = *tp;
6278 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6279 location_t location;
6281 if (EXPR_HAS_LOCATION (t))
6282 location = EXPR_LOCATION (t);
6283 else
6285 location_t *locp = (location_t *) wi->info;
6286 location = *locp;
6289 *walk_subtree = TRUE;
6291 if (TREE_CODE (t) == ARRAY_REF)
6292 check_array_ref (location, t, false /*ignore_off_by_one*/);
6294 if (TREE_CODE (t) == MEM_REF
6295 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6296 search_for_addr_array (TREE_OPERAND (t, 0), location);
6298 if (TREE_CODE (t) == ADDR_EXPR)
6299 *walk_subtree = FALSE;
6301 return NULL_TREE;
6304 /* Walk over all statements of all reachable BBs and call check_array_bounds
6305 on them. */
6307 static void
6308 check_all_array_refs (void)
6310 basic_block bb;
6311 gimple_stmt_iterator si;
6313 FOR_EACH_BB_FN (bb, cfun)
6315 edge_iterator ei;
6316 edge e;
6317 bool executable = false;
6319 /* Skip blocks that were found to be unreachable. */
6320 FOR_EACH_EDGE (e, ei, bb->preds)
6321 executable |= !!(e->flags & EDGE_EXECUTABLE);
6322 if (!executable)
6323 continue;
6325 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6327 gimple stmt = gsi_stmt (si);
6328 struct walk_stmt_info wi;
6329 if (!gimple_has_location (stmt))
6330 continue;
6332 if (is_gimple_call (stmt))
6334 size_t i;
6335 size_t n = gimple_call_num_args (stmt);
6336 for (i = 0; i < n; i++)
6338 tree arg = gimple_call_arg (stmt, i);
6339 search_for_addr_array (arg, gimple_location (stmt));
6342 else
6344 memset (&wi, 0, sizeof (wi));
6345 wi.info = CONST_CAST (void *, (const void *)
6346 gimple_location_ptr (stmt));
6348 walk_gimple_op (gsi_stmt (si),
6349 check_array_bounds,
6350 &wi);
6356 /* Return true if all imm uses of VAR are either in STMT, or
6357 feed (optionally through a chain of single imm uses) GIMPLE_COND
6358 in basic block COND_BB. */
6360 static bool
6361 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6363 use_operand_p use_p, use2_p;
6364 imm_use_iterator iter;
6366 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6367 if (USE_STMT (use_p) != stmt)
6369 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6370 if (is_gimple_debug (use_stmt))
6371 continue;
6372 while (is_gimple_assign (use_stmt)
6373 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6374 && single_imm_use (gimple_assign_lhs (use_stmt),
6375 &use2_p, &use_stmt2))
6376 use_stmt = use_stmt2;
6377 if (gimple_code (use_stmt) != GIMPLE_COND
6378 || gimple_bb (use_stmt) != cond_bb)
6379 return false;
6381 return true;
6384 /* Handle
6385 _4 = x_3 & 31;
6386 if (_4 != 0)
6387 goto <bb 6>;
6388 else
6389 goto <bb 7>;
6390 <bb 6>:
6391 __builtin_unreachable ();
6392 <bb 7>:
6393 x_5 = ASSERT_EXPR <x_3, ...>;
6394 If x_3 has no other immediate uses (checked by caller),
6395 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6396 from the non-zero bitmask. */
6398 static void
6399 maybe_set_nonzero_bits (basic_block bb, tree var)
6401 edge e = single_pred_edge (bb);
6402 basic_block cond_bb = e->src;
6403 gimple stmt = last_stmt (cond_bb);
6404 tree cst;
6406 if (stmt == NULL
6407 || gimple_code (stmt) != GIMPLE_COND
6408 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6409 ? EQ_EXPR : NE_EXPR)
6410 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6411 || !integer_zerop (gimple_cond_rhs (stmt)))
6412 return;
6414 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6415 if (!is_gimple_assign (stmt)
6416 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6417 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6418 return;
6419 if (gimple_assign_rhs1 (stmt) != var)
6421 gimple stmt2;
6423 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6424 return;
6425 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6426 if (!gimple_assign_cast_p (stmt2)
6427 || gimple_assign_rhs1 (stmt2) != var
6428 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6429 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6430 != TYPE_PRECISION (TREE_TYPE (var))))
6431 return;
6433 cst = gimple_assign_rhs2 (stmt);
6434 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6437 /* Convert range assertion expressions into the implied copies and
6438 copy propagate away the copies. Doing the trivial copy propagation
6439 here avoids the need to run the full copy propagation pass after
6440 VRP.
6442 FIXME, this will eventually lead to copy propagation removing the
6443 names that had useful range information attached to them. For
6444 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6445 then N_i will have the range [3, +INF].
6447 However, by converting the assertion into the implied copy
6448 operation N_i = N_j, we will then copy-propagate N_j into the uses
6449 of N_i and lose the range information. We may want to hold on to
6450 ASSERT_EXPRs a little while longer as the ranges could be used in
6451 things like jump threading.
6453 The problem with keeping ASSERT_EXPRs around is that passes after
6454 VRP need to handle them appropriately.
6456 Another approach would be to make the range information a first
6457 class property of the SSA_NAME so that it can be queried from
6458 any pass. This is made somewhat more complex by the need for
6459 multiple ranges to be associated with one SSA_NAME. */
6461 static void
6462 remove_range_assertions (void)
6464 basic_block bb;
6465 gimple_stmt_iterator si;
6466 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6467 a basic block preceeded by GIMPLE_COND branching to it and
6468 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6469 int is_unreachable;
6471 /* Note that the BSI iterator bump happens at the bottom of the
6472 loop and no bump is necessary if we're removing the statement
6473 referenced by the current BSI. */
6474 FOR_EACH_BB_FN (bb, cfun)
6475 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6477 gimple stmt = gsi_stmt (si);
6478 gimple use_stmt;
6480 if (is_gimple_assign (stmt)
6481 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6483 tree lhs = gimple_assign_lhs (stmt);
6484 tree rhs = gimple_assign_rhs1 (stmt);
6485 tree var;
6486 tree cond = fold (ASSERT_EXPR_COND (rhs));
6487 use_operand_p use_p;
6488 imm_use_iterator iter;
6490 gcc_assert (cond != boolean_false_node);
6492 var = ASSERT_EXPR_VAR (rhs);
6493 gcc_assert (TREE_CODE (var) == SSA_NAME);
6495 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6496 && SSA_NAME_RANGE_INFO (lhs))
6498 if (is_unreachable == -1)
6500 is_unreachable = 0;
6501 if (single_pred_p (bb)
6502 && assert_unreachable_fallthru_edge_p
6503 (single_pred_edge (bb)))
6504 is_unreachable = 1;
6506 /* Handle
6507 if (x_7 >= 10 && x_7 < 20)
6508 __builtin_unreachable ();
6509 x_8 = ASSERT_EXPR <x_7, ...>;
6510 if the only uses of x_7 are in the ASSERT_EXPR and
6511 in the condition. In that case, we can copy the
6512 range info from x_8 computed in this pass also
6513 for x_7. */
6514 if (is_unreachable
6515 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6516 single_pred (bb)))
6518 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6519 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6520 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6521 maybe_set_nonzero_bits (bb, var);
6525 /* Propagate the RHS into every use of the LHS. */
6526 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6527 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6528 SET_USE (use_p, var);
6530 /* And finally, remove the copy, it is not needed. */
6531 gsi_remove (&si, true);
6532 release_defs (stmt);
6534 else
6536 gsi_next (&si);
6537 is_unreachable = 0;
6543 /* Return true if STMT is interesting for VRP. */
6545 static bool
6546 stmt_interesting_for_vrp (gimple stmt)
6548 if (gimple_code (stmt) == GIMPLE_PHI)
6550 tree res = gimple_phi_result (stmt);
6551 return (!virtual_operand_p (res)
6552 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6553 || POINTER_TYPE_P (TREE_TYPE (res))));
6555 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6557 tree lhs = gimple_get_lhs (stmt);
6559 /* In general, assignments with virtual operands are not useful
6560 for deriving ranges, with the obvious exception of calls to
6561 builtin functions. */
6562 if (lhs && TREE_CODE (lhs) == SSA_NAME
6563 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6564 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6565 && (is_gimple_call (stmt)
6566 || !gimple_vuse (stmt)))
6567 return true;
6569 else if (gimple_code (stmt) == GIMPLE_COND
6570 || gimple_code (stmt) == GIMPLE_SWITCH)
6571 return true;
6573 return false;
6577 /* Initialize local data structures for VRP. */
6579 static void
6580 vrp_initialize (void)
6582 basic_block bb;
6584 values_propagated = false;
6585 num_vr_values = num_ssa_names;
6586 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6587 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6589 FOR_EACH_BB_FN (bb, cfun)
6591 gimple_stmt_iterator si;
6593 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6595 gimple phi = gsi_stmt (si);
6596 if (!stmt_interesting_for_vrp (phi))
6598 tree lhs = PHI_RESULT (phi);
6599 set_value_range_to_varying (get_value_range (lhs));
6600 prop_set_simulate_again (phi, false);
6602 else
6603 prop_set_simulate_again (phi, true);
6606 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6608 gimple stmt = gsi_stmt (si);
6610 /* If the statement is a control insn, then we do not
6611 want to avoid simulating the statement once. Failure
6612 to do so means that those edges will never get added. */
6613 if (stmt_ends_bb_p (stmt))
6614 prop_set_simulate_again (stmt, true);
6615 else if (!stmt_interesting_for_vrp (stmt))
6617 ssa_op_iter i;
6618 tree def;
6619 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6620 set_value_range_to_varying (get_value_range (def));
6621 prop_set_simulate_again (stmt, false);
6623 else
6624 prop_set_simulate_again (stmt, true);
6629 /* Return the singleton value-range for NAME or NAME. */
6631 static inline tree
6632 vrp_valueize (tree name)
6634 if (TREE_CODE (name) == SSA_NAME)
6636 value_range_t *vr = get_value_range (name);
6637 if (vr->type == VR_RANGE
6638 && (vr->min == vr->max
6639 || operand_equal_p (vr->min, vr->max, 0)))
6640 return vr->min;
6642 return name;
6645 /* Visit assignment STMT. If it produces an interesting range, record
6646 the SSA name in *OUTPUT_P. */
6648 static enum ssa_prop_result
6649 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6651 tree def, lhs;
6652 ssa_op_iter iter;
6653 enum gimple_code code = gimple_code (stmt);
6654 lhs = gimple_get_lhs (stmt);
6656 /* We only keep track of ranges in integral and pointer types. */
6657 if (TREE_CODE (lhs) == SSA_NAME
6658 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6659 /* It is valid to have NULL MIN/MAX values on a type. See
6660 build_range_type. */
6661 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6662 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6663 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6665 value_range_t new_vr = VR_INITIALIZER;
6667 /* Try folding the statement to a constant first. */
6668 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6669 if (tem)
6670 set_value_range_to_value (&new_vr, tem, NULL);
6671 /* Then dispatch to value-range extracting functions. */
6672 else if (code == GIMPLE_CALL)
6673 extract_range_basic (&new_vr, stmt);
6674 else
6675 extract_range_from_assignment (&new_vr, stmt);
6677 if (update_value_range (lhs, &new_vr))
6679 *output_p = lhs;
6681 if (dump_file && (dump_flags & TDF_DETAILS))
6683 fprintf (dump_file, "Found new range for ");
6684 print_generic_expr (dump_file, lhs, 0);
6685 fprintf (dump_file, ": ");
6686 dump_value_range (dump_file, &new_vr);
6687 fprintf (dump_file, "\n\n");
6690 if (new_vr.type == VR_VARYING)
6691 return SSA_PROP_VARYING;
6693 return SSA_PROP_INTERESTING;
6696 return SSA_PROP_NOT_INTERESTING;
6699 /* Every other statement produces no useful ranges. */
6700 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6701 set_value_range_to_varying (get_value_range (def));
6703 return SSA_PROP_VARYING;
6706 /* Helper that gets the value range of the SSA_NAME with version I
6707 or a symbolic range containing the SSA_NAME only if the value range
6708 is varying or undefined. */
6710 static inline value_range_t
6711 get_vr_for_comparison (int i)
6713 value_range_t vr = *get_value_range (ssa_name (i));
6715 /* If name N_i does not have a valid range, use N_i as its own
6716 range. This allows us to compare against names that may
6717 have N_i in their ranges. */
6718 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6720 vr.type = VR_RANGE;
6721 vr.min = ssa_name (i);
6722 vr.max = ssa_name (i);
6725 return vr;
6728 /* Compare all the value ranges for names equivalent to VAR with VAL
6729 using comparison code COMP. Return the same value returned by
6730 compare_range_with_value, including the setting of
6731 *STRICT_OVERFLOW_P. */
6733 static tree
6734 compare_name_with_value (enum tree_code comp, tree var, tree val,
6735 bool *strict_overflow_p)
6737 bitmap_iterator bi;
6738 unsigned i;
6739 bitmap e;
6740 tree retval, t;
6741 int used_strict_overflow;
6742 bool sop;
6743 value_range_t equiv_vr;
6745 /* Get the set of equivalences for VAR. */
6746 e = get_value_range (var)->equiv;
6748 /* Start at -1. Set it to 0 if we do a comparison without relying
6749 on overflow, or 1 if all comparisons rely on overflow. */
6750 used_strict_overflow = -1;
6752 /* Compare vars' value range with val. */
6753 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6754 sop = false;
6755 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
6756 if (retval)
6757 used_strict_overflow = sop ? 1 : 0;
6759 /* If the equiv set is empty we have done all work we need to do. */
6760 if (e == NULL)
6762 if (retval
6763 && used_strict_overflow > 0)
6764 *strict_overflow_p = true;
6765 return retval;
6768 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
6770 equiv_vr = get_vr_for_comparison (i);
6771 sop = false;
6772 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
6773 if (t)
6775 /* If we get different answers from different members
6776 of the equivalence set this check must be in a dead
6777 code region. Folding it to a trap representation
6778 would be correct here. For now just return don't-know. */
6779 if (retval != NULL
6780 && t != retval)
6782 retval = NULL_TREE;
6783 break;
6785 retval = t;
6787 if (!sop)
6788 used_strict_overflow = 0;
6789 else if (used_strict_overflow < 0)
6790 used_strict_overflow = 1;
6794 if (retval
6795 && used_strict_overflow > 0)
6796 *strict_overflow_p = true;
6798 return retval;
6802 /* Given a comparison code COMP and names N1 and N2, compare all the
6803 ranges equivalent to N1 against all the ranges equivalent to N2
6804 to determine the value of N1 COMP N2. Return the same value
6805 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6806 whether we relied on an overflow infinity in the comparison. */
6809 static tree
6810 compare_names (enum tree_code comp, tree n1, tree n2,
6811 bool *strict_overflow_p)
6813 tree t, retval;
6814 bitmap e1, e2;
6815 bitmap_iterator bi1, bi2;
6816 unsigned i1, i2;
6817 int used_strict_overflow;
6818 static bitmap_obstack *s_obstack = NULL;
6819 static bitmap s_e1 = NULL, s_e2 = NULL;
6821 /* Compare the ranges of every name equivalent to N1 against the
6822 ranges of every name equivalent to N2. */
6823 e1 = get_value_range (n1)->equiv;
6824 e2 = get_value_range (n2)->equiv;
6826 /* Use the fake bitmaps if e1 or e2 are not available. */
6827 if (s_obstack == NULL)
6829 s_obstack = XNEW (bitmap_obstack);
6830 bitmap_obstack_initialize (s_obstack);
6831 s_e1 = BITMAP_ALLOC (s_obstack);
6832 s_e2 = BITMAP_ALLOC (s_obstack);
6834 if (e1 == NULL)
6835 e1 = s_e1;
6836 if (e2 == NULL)
6837 e2 = s_e2;
6839 /* Add N1 and N2 to their own set of equivalences to avoid
6840 duplicating the body of the loop just to check N1 and N2
6841 ranges. */
6842 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
6843 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
6845 /* If the equivalence sets have a common intersection, then the two
6846 names can be compared without checking their ranges. */
6847 if (bitmap_intersect_p (e1, e2))
6849 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6850 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6852 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
6853 ? boolean_true_node
6854 : boolean_false_node;
6857 /* Start at -1. Set it to 0 if we do a comparison without relying
6858 on overflow, or 1 if all comparisons rely on overflow. */
6859 used_strict_overflow = -1;
6861 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6862 N2 to their own set of equivalences to avoid duplicating the body
6863 of the loop just to check N1 and N2 ranges. */
6864 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
6866 value_range_t vr1 = get_vr_for_comparison (i1);
6868 t = retval = NULL_TREE;
6869 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
6871 bool sop = false;
6873 value_range_t vr2 = get_vr_for_comparison (i2);
6875 t = compare_ranges (comp, &vr1, &vr2, &sop);
6876 if (t)
6878 /* If we get different answers from different members
6879 of the equivalence set this check must be in a dead
6880 code region. Folding it to a trap representation
6881 would be correct here. For now just return don't-know. */
6882 if (retval != NULL
6883 && t != retval)
6885 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6886 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6887 return NULL_TREE;
6889 retval = t;
6891 if (!sop)
6892 used_strict_overflow = 0;
6893 else if (used_strict_overflow < 0)
6894 used_strict_overflow = 1;
6898 if (retval)
6900 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6901 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6902 if (used_strict_overflow > 0)
6903 *strict_overflow_p = true;
6904 return retval;
6908 /* None of the equivalent ranges are useful in computing this
6909 comparison. */
6910 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6911 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6912 return NULL_TREE;
6915 /* Helper function for vrp_evaluate_conditional_warnv. */
6917 static tree
6918 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
6919 tree op0, tree op1,
6920 bool * strict_overflow_p)
6922 value_range_t *vr0, *vr1;
6924 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
6925 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
6927 if (vr0 && vr1)
6928 return compare_ranges (code, vr0, vr1, strict_overflow_p);
6929 else if (vr0 && vr1 == NULL)
6930 return compare_range_with_value (code, vr0, op1, strict_overflow_p);
6931 else if (vr0 == NULL && vr1)
6932 return (compare_range_with_value
6933 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
6934 return NULL;
6937 /* Helper function for vrp_evaluate_conditional_warnv. */
6939 static tree
6940 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
6941 tree op1, bool use_equiv_p,
6942 bool *strict_overflow_p, bool *only_ranges)
6944 tree ret;
6945 if (only_ranges)
6946 *only_ranges = true;
6948 /* We only deal with integral and pointer types. */
6949 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
6950 && !POINTER_TYPE_P (TREE_TYPE (op0)))
6951 return NULL_TREE;
6953 if (use_equiv_p)
6955 if (only_ranges
6956 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
6957 (code, op0, op1, strict_overflow_p)))
6958 return ret;
6959 *only_ranges = false;
6960 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
6961 return compare_names (code, op0, op1, strict_overflow_p);
6962 else if (TREE_CODE (op0) == SSA_NAME)
6963 return compare_name_with_value (code, op0, op1, strict_overflow_p);
6964 else if (TREE_CODE (op1) == SSA_NAME)
6965 return (compare_name_with_value
6966 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
6968 else
6969 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
6970 strict_overflow_p);
6971 return NULL_TREE;
6974 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6975 information. Return NULL if the conditional can not be evaluated.
6976 The ranges of all the names equivalent with the operands in COND
6977 will be used when trying to compute the value. If the result is
6978 based on undefined signed overflow, issue a warning if
6979 appropriate. */
6981 static tree
6982 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
6984 bool sop;
6985 tree ret;
6986 bool only_ranges;
6988 /* Some passes and foldings leak constants with overflow flag set
6989 into the IL. Avoid doing wrong things with these and bail out. */
6990 if ((TREE_CODE (op0) == INTEGER_CST
6991 && TREE_OVERFLOW (op0))
6992 || (TREE_CODE (op1) == INTEGER_CST
6993 && TREE_OVERFLOW (op1)))
6994 return NULL_TREE;
6996 sop = false;
6997 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
6998 &only_ranges);
7000 if (ret && sop)
7002 enum warn_strict_overflow_code wc;
7003 const char* warnmsg;
7005 if (is_gimple_min_invariant (ret))
7007 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7008 warnmsg = G_("assuming signed overflow does not occur when "
7009 "simplifying conditional to constant");
7011 else
7013 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7014 warnmsg = G_("assuming signed overflow does not occur when "
7015 "simplifying conditional");
7018 if (issue_strict_overflow_warning (wc))
7020 location_t location;
7022 if (!gimple_has_location (stmt))
7023 location = input_location;
7024 else
7025 location = gimple_location (stmt);
7026 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7030 if (warn_type_limits
7031 && ret && only_ranges
7032 && TREE_CODE_CLASS (code) == tcc_comparison
7033 && TREE_CODE (op0) == SSA_NAME)
7035 /* If the comparison is being folded and the operand on the LHS
7036 is being compared against a constant value that is outside of
7037 the natural range of OP0's type, then the predicate will
7038 always fold regardless of the value of OP0. If -Wtype-limits
7039 was specified, emit a warning. */
7040 tree type = TREE_TYPE (op0);
7041 value_range_t *vr0 = get_value_range (op0);
7043 if (vr0->type != VR_VARYING
7044 && INTEGRAL_TYPE_P (type)
7045 && vrp_val_is_min (vr0->min)
7046 && vrp_val_is_max (vr0->max)
7047 && is_gimple_min_invariant (op1))
7049 location_t location;
7051 if (!gimple_has_location (stmt))
7052 location = input_location;
7053 else
7054 location = gimple_location (stmt);
7056 warning_at (location, OPT_Wtype_limits,
7057 integer_zerop (ret)
7058 ? G_("comparison always false "
7059 "due to limited range of data type")
7060 : G_("comparison always true "
7061 "due to limited range of data type"));
7065 return ret;
7069 /* Visit conditional statement STMT. If we can determine which edge
7070 will be taken out of STMT's basic block, record it in
7071 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7072 SSA_PROP_VARYING. */
7074 static enum ssa_prop_result
7075 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
7077 tree val;
7078 bool sop;
7080 *taken_edge_p = NULL;
7082 if (dump_file && (dump_flags & TDF_DETAILS))
7084 tree use;
7085 ssa_op_iter i;
7087 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7088 print_gimple_stmt (dump_file, stmt, 0, 0);
7089 fprintf (dump_file, "\nWith known ranges\n");
7091 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7093 fprintf (dump_file, "\t");
7094 print_generic_expr (dump_file, use, 0);
7095 fprintf (dump_file, ": ");
7096 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7099 fprintf (dump_file, "\n");
7102 /* Compute the value of the predicate COND by checking the known
7103 ranges of each of its operands.
7105 Note that we cannot evaluate all the equivalent ranges here
7106 because those ranges may not yet be final and with the current
7107 propagation strategy, we cannot determine when the value ranges
7108 of the names in the equivalence set have changed.
7110 For instance, given the following code fragment
7112 i_5 = PHI <8, i_13>
7114 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7115 if (i_14 == 1)
7118 Assume that on the first visit to i_14, i_5 has the temporary
7119 range [8, 8] because the second argument to the PHI function is
7120 not yet executable. We derive the range ~[0, 0] for i_14 and the
7121 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7122 the first time, since i_14 is equivalent to the range [8, 8], we
7123 determine that the predicate is always false.
7125 On the next round of propagation, i_13 is determined to be
7126 VARYING, which causes i_5 to drop down to VARYING. So, another
7127 visit to i_14 is scheduled. In this second visit, we compute the
7128 exact same range and equivalence set for i_14, namely ~[0, 0] and
7129 { i_5 }. But we did not have the previous range for i_5
7130 registered, so vrp_visit_assignment thinks that the range for
7131 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7132 is not visited again, which stops propagation from visiting
7133 statements in the THEN clause of that if().
7135 To properly fix this we would need to keep the previous range
7136 value for the names in the equivalence set. This way we would've
7137 discovered that from one visit to the other i_5 changed from
7138 range [8, 8] to VR_VARYING.
7140 However, fixing this apparent limitation may not be worth the
7141 additional checking. Testing on several code bases (GCC, DLV,
7142 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7143 4 more predicates folded in SPEC. */
7144 sop = false;
7146 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7147 gimple_cond_lhs (stmt),
7148 gimple_cond_rhs (stmt),
7149 false, &sop, NULL);
7150 if (val)
7152 if (!sop)
7153 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7154 else
7156 if (dump_file && (dump_flags & TDF_DETAILS))
7157 fprintf (dump_file,
7158 "\nIgnoring predicate evaluation because "
7159 "it assumes that signed overflow is undefined");
7160 val = NULL_TREE;
7164 if (dump_file && (dump_flags & TDF_DETAILS))
7166 fprintf (dump_file, "\nPredicate evaluates to: ");
7167 if (val == NULL_TREE)
7168 fprintf (dump_file, "DON'T KNOW\n");
7169 else
7170 print_generic_stmt (dump_file, val, 0);
7173 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7176 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7177 that includes the value VAL. The search is restricted to the range
7178 [START_IDX, n - 1] where n is the size of VEC.
7180 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7181 returned.
7183 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7184 it is placed in IDX and false is returned.
7186 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7187 returned. */
7189 static bool
7190 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
7192 size_t n = gimple_switch_num_labels (stmt);
7193 size_t low, high;
7195 /* Find case label for minimum of the value range or the next one.
7196 At each iteration we are searching in [low, high - 1]. */
7198 for (low = start_idx, high = n; high != low; )
7200 tree t;
7201 int cmp;
7202 /* Note that i != high, so we never ask for n. */
7203 size_t i = (high + low) / 2;
7204 t = gimple_switch_label (stmt, i);
7206 /* Cache the result of comparing CASE_LOW and val. */
7207 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7209 if (cmp == 0)
7211 /* Ranges cannot be empty. */
7212 *idx = i;
7213 return true;
7215 else if (cmp > 0)
7216 high = i;
7217 else
7219 low = i + 1;
7220 if (CASE_HIGH (t) != NULL
7221 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7223 *idx = i;
7224 return true;
7229 *idx = high;
7230 return false;
7233 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7234 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7235 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7236 then MAX_IDX < MIN_IDX.
7237 Returns true if the default label is not needed. */
7239 static bool
7240 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
7241 size_t *max_idx)
7243 size_t i, j;
7244 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7245 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7247 if (i == j
7248 && min_take_default
7249 && max_take_default)
7251 /* Only the default case label reached.
7252 Return an empty range. */
7253 *min_idx = 1;
7254 *max_idx = 0;
7255 return false;
7257 else
7259 bool take_default = min_take_default || max_take_default;
7260 tree low, high;
7261 size_t k;
7263 if (max_take_default)
7264 j--;
7266 /* If the case label range is continuous, we do not need
7267 the default case label. Verify that. */
7268 high = CASE_LOW (gimple_switch_label (stmt, i));
7269 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7270 high = CASE_HIGH (gimple_switch_label (stmt, i));
7271 for (k = i + 1; k <= j; ++k)
7273 low = CASE_LOW (gimple_switch_label (stmt, k));
7274 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7276 take_default = true;
7277 break;
7279 high = low;
7280 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7281 high = CASE_HIGH (gimple_switch_label (stmt, k));
7284 *min_idx = i;
7285 *max_idx = j;
7286 return !take_default;
7290 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7291 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7292 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7293 Returns true if the default label is not needed. */
7295 static bool
7296 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
7297 size_t *max_idx1, size_t *min_idx2,
7298 size_t *max_idx2)
7300 size_t i, j, k, l;
7301 unsigned int n = gimple_switch_num_labels (stmt);
7302 bool take_default;
7303 tree case_low, case_high;
7304 tree min = vr->min, max = vr->max;
7306 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7308 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7310 /* Set second range to emtpy. */
7311 *min_idx2 = 1;
7312 *max_idx2 = 0;
7314 if (vr->type == VR_RANGE)
7316 *min_idx1 = i;
7317 *max_idx1 = j;
7318 return !take_default;
7321 /* Set first range to all case labels. */
7322 *min_idx1 = 1;
7323 *max_idx1 = n - 1;
7325 if (i > j)
7326 return false;
7328 /* Make sure all the values of case labels [i , j] are contained in
7329 range [MIN, MAX]. */
7330 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7331 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7332 if (tree_int_cst_compare (case_low, min) < 0)
7333 i += 1;
7334 if (case_high != NULL_TREE
7335 && tree_int_cst_compare (max, case_high) < 0)
7336 j -= 1;
7338 if (i > j)
7339 return false;
7341 /* If the range spans case labels [i, j], the corresponding anti-range spans
7342 the labels [1, i - 1] and [j + 1, n - 1]. */
7343 k = j + 1;
7344 l = n - 1;
7345 if (k > l)
7347 k = 1;
7348 l = 0;
7351 j = i - 1;
7352 i = 1;
7353 if (i > j)
7355 i = k;
7356 j = l;
7357 k = 1;
7358 l = 0;
7361 *min_idx1 = i;
7362 *max_idx1 = j;
7363 *min_idx2 = k;
7364 *max_idx2 = l;
7365 return false;
7368 /* Visit switch statement STMT. If we can determine which edge
7369 will be taken out of STMT's basic block, record it in
7370 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7371 SSA_PROP_VARYING. */
7373 static enum ssa_prop_result
7374 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7376 tree op, val;
7377 value_range_t *vr;
7378 size_t i = 0, j = 0, k, l;
7379 bool take_default;
7381 *taken_edge_p = NULL;
7382 op = gimple_switch_index (stmt);
7383 if (TREE_CODE (op) != SSA_NAME)
7384 return SSA_PROP_VARYING;
7386 vr = get_value_range (op);
7387 if (dump_file && (dump_flags & TDF_DETAILS))
7389 fprintf (dump_file, "\nVisiting switch expression with operand ");
7390 print_generic_expr (dump_file, op, 0);
7391 fprintf (dump_file, " with known range ");
7392 dump_value_range (dump_file, vr);
7393 fprintf (dump_file, "\n");
7396 if ((vr->type != VR_RANGE
7397 && vr->type != VR_ANTI_RANGE)
7398 || symbolic_range_p (vr))
7399 return SSA_PROP_VARYING;
7401 /* Find the single edge that is taken from the switch expression. */
7402 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7404 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7405 label */
7406 if (j < i)
7408 gcc_assert (take_default);
7409 val = gimple_switch_default_label (stmt);
7411 else
7413 /* Check if labels with index i to j and maybe the default label
7414 are all reaching the same label. */
7416 val = gimple_switch_label (stmt, i);
7417 if (take_default
7418 && CASE_LABEL (gimple_switch_default_label (stmt))
7419 != CASE_LABEL (val))
7421 if (dump_file && (dump_flags & TDF_DETAILS))
7422 fprintf (dump_file, " not a single destination for this "
7423 "range\n");
7424 return SSA_PROP_VARYING;
7426 for (++i; i <= j; ++i)
7428 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7430 if (dump_file && (dump_flags & TDF_DETAILS))
7431 fprintf (dump_file, " not a single destination for this "
7432 "range\n");
7433 return SSA_PROP_VARYING;
7436 for (; k <= l; ++k)
7438 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7440 if (dump_file && (dump_flags & TDF_DETAILS))
7441 fprintf (dump_file, " not a single destination for this "
7442 "range\n");
7443 return SSA_PROP_VARYING;
7448 *taken_edge_p = find_edge (gimple_bb (stmt),
7449 label_to_block (CASE_LABEL (val)));
7451 if (dump_file && (dump_flags & TDF_DETAILS))
7453 fprintf (dump_file, " will take edge to ");
7454 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7457 return SSA_PROP_INTERESTING;
7461 /* Evaluate statement STMT. If the statement produces a useful range,
7462 return SSA_PROP_INTERESTING and record the SSA name with the
7463 interesting range into *OUTPUT_P.
7465 If STMT is a conditional branch and we can determine its truth
7466 value, the taken edge is recorded in *TAKEN_EDGE_P.
7468 If STMT produces a varying value, return SSA_PROP_VARYING. */
7470 static enum ssa_prop_result
7471 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7473 tree def;
7474 ssa_op_iter iter;
7476 if (dump_file && (dump_flags & TDF_DETAILS))
7478 fprintf (dump_file, "\nVisiting statement:\n");
7479 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7480 fprintf (dump_file, "\n");
7483 if (!stmt_interesting_for_vrp (stmt))
7484 gcc_assert (stmt_ends_bb_p (stmt));
7485 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7486 return vrp_visit_assignment_or_call (stmt, output_p);
7487 else if (gimple_code (stmt) == GIMPLE_COND)
7488 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7489 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7490 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7492 /* All other statements produce nothing of interest for VRP, so mark
7493 their outputs varying and prevent further simulation. */
7494 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7495 set_value_range_to_varying (get_value_range (def));
7497 return SSA_PROP_VARYING;
7500 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7501 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7502 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7503 possible such range. The resulting range is not canonicalized. */
7505 static void
7506 union_ranges (enum value_range_type *vr0type,
7507 tree *vr0min, tree *vr0max,
7508 enum value_range_type vr1type,
7509 tree vr1min, tree vr1max)
7511 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7512 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7514 /* [] is vr0, () is vr1 in the following classification comments. */
7515 if (mineq && maxeq)
7517 /* [( )] */
7518 if (*vr0type == vr1type)
7519 /* Nothing to do for equal ranges. */
7521 else if ((*vr0type == VR_RANGE
7522 && vr1type == VR_ANTI_RANGE)
7523 || (*vr0type == VR_ANTI_RANGE
7524 && vr1type == VR_RANGE))
7526 /* For anti-range with range union the result is varying. */
7527 goto give_up;
7529 else
7530 gcc_unreachable ();
7532 else if (operand_less_p (*vr0max, vr1min) == 1
7533 || operand_less_p (vr1max, *vr0min) == 1)
7535 /* [ ] ( ) or ( ) [ ]
7536 If the ranges have an empty intersection, result of the union
7537 operation is the anti-range or if both are anti-ranges
7538 it covers all. */
7539 if (*vr0type == VR_ANTI_RANGE
7540 && vr1type == VR_ANTI_RANGE)
7541 goto give_up;
7542 else if (*vr0type == VR_ANTI_RANGE
7543 && vr1type == VR_RANGE)
7545 else if (*vr0type == VR_RANGE
7546 && vr1type == VR_ANTI_RANGE)
7548 *vr0type = vr1type;
7549 *vr0min = vr1min;
7550 *vr0max = vr1max;
7552 else if (*vr0type == VR_RANGE
7553 && vr1type == VR_RANGE)
7555 /* The result is the convex hull of both ranges. */
7556 if (operand_less_p (*vr0max, vr1min) == 1)
7558 /* If the result can be an anti-range, create one. */
7559 if (TREE_CODE (*vr0max) == INTEGER_CST
7560 && TREE_CODE (vr1min) == INTEGER_CST
7561 && vrp_val_is_min (*vr0min)
7562 && vrp_val_is_max (vr1max))
7564 tree min = int_const_binop (PLUS_EXPR,
7565 *vr0max,
7566 build_int_cst (TREE_TYPE (*vr0max), 1));
7567 tree max = int_const_binop (MINUS_EXPR,
7568 vr1min,
7569 build_int_cst (TREE_TYPE (vr1min), 1));
7570 if (!operand_less_p (max, min))
7572 *vr0type = VR_ANTI_RANGE;
7573 *vr0min = min;
7574 *vr0max = max;
7576 else
7577 *vr0max = vr1max;
7579 else
7580 *vr0max = vr1max;
7582 else
7584 /* If the result can be an anti-range, create one. */
7585 if (TREE_CODE (vr1max) == INTEGER_CST
7586 && TREE_CODE (*vr0min) == INTEGER_CST
7587 && vrp_val_is_min (vr1min)
7588 && vrp_val_is_max (*vr0max))
7590 tree min = int_const_binop (PLUS_EXPR,
7591 vr1max,
7592 build_int_cst (TREE_TYPE (vr1max), 1));
7593 tree max = int_const_binop (MINUS_EXPR,
7594 *vr0min,
7595 build_int_cst (TREE_TYPE (*vr0min), 1));
7596 if (!operand_less_p (max, min))
7598 *vr0type = VR_ANTI_RANGE;
7599 *vr0min = min;
7600 *vr0max = max;
7602 else
7603 *vr0min = vr1min;
7605 else
7606 *vr0min = vr1min;
7609 else
7610 gcc_unreachable ();
7612 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7613 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7615 /* [ ( ) ] or [( ) ] or [ ( )] */
7616 if (*vr0type == VR_RANGE
7617 && vr1type == VR_RANGE)
7619 else if (*vr0type == VR_ANTI_RANGE
7620 && vr1type == VR_ANTI_RANGE)
7622 *vr0type = vr1type;
7623 *vr0min = vr1min;
7624 *vr0max = vr1max;
7626 else if (*vr0type == VR_ANTI_RANGE
7627 && vr1type == VR_RANGE)
7629 /* Arbitrarily choose the right or left gap. */
7630 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7631 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7632 build_int_cst (TREE_TYPE (vr1min), 1));
7633 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7634 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7635 build_int_cst (TREE_TYPE (vr1max), 1));
7636 else
7637 goto give_up;
7639 else if (*vr0type == VR_RANGE
7640 && vr1type == VR_ANTI_RANGE)
7641 /* The result covers everything. */
7642 goto give_up;
7643 else
7644 gcc_unreachable ();
7646 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7647 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7649 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7650 if (*vr0type == VR_RANGE
7651 && vr1type == VR_RANGE)
7653 *vr0type = vr1type;
7654 *vr0min = vr1min;
7655 *vr0max = vr1max;
7657 else if (*vr0type == VR_ANTI_RANGE
7658 && vr1type == VR_ANTI_RANGE)
7660 else if (*vr0type == VR_RANGE
7661 && vr1type == VR_ANTI_RANGE)
7663 *vr0type = VR_ANTI_RANGE;
7664 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7666 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7667 build_int_cst (TREE_TYPE (*vr0min), 1));
7668 *vr0min = vr1min;
7670 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7672 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7673 build_int_cst (TREE_TYPE (*vr0max), 1));
7674 *vr0max = vr1max;
7676 else
7677 goto give_up;
7679 else if (*vr0type == VR_ANTI_RANGE
7680 && vr1type == VR_RANGE)
7681 /* The result covers everything. */
7682 goto give_up;
7683 else
7684 gcc_unreachable ();
7686 else if ((operand_less_p (vr1min, *vr0max) == 1
7687 || operand_equal_p (vr1min, *vr0max, 0))
7688 && operand_less_p (*vr0min, vr1min) == 1
7689 && operand_less_p (*vr0max, vr1max) == 1)
7691 /* [ ( ] ) or [ ]( ) */
7692 if (*vr0type == VR_RANGE
7693 && vr1type == VR_RANGE)
7694 *vr0max = vr1max;
7695 else if (*vr0type == VR_ANTI_RANGE
7696 && vr1type == VR_ANTI_RANGE)
7697 *vr0min = vr1min;
7698 else if (*vr0type == VR_ANTI_RANGE
7699 && vr1type == VR_RANGE)
7701 if (TREE_CODE (vr1min) == INTEGER_CST)
7702 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7703 build_int_cst (TREE_TYPE (vr1min), 1));
7704 else
7705 goto give_up;
7707 else if (*vr0type == VR_RANGE
7708 && vr1type == VR_ANTI_RANGE)
7710 if (TREE_CODE (*vr0max) == INTEGER_CST)
7712 *vr0type = vr1type;
7713 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7714 build_int_cst (TREE_TYPE (*vr0max), 1));
7715 *vr0max = vr1max;
7717 else
7718 goto give_up;
7720 else
7721 gcc_unreachable ();
7723 else if ((operand_less_p (*vr0min, vr1max) == 1
7724 || operand_equal_p (*vr0min, vr1max, 0))
7725 && operand_less_p (vr1min, *vr0min) == 1
7726 && operand_less_p (vr1max, *vr0max) == 1)
7728 /* ( [ ) ] or ( )[ ] */
7729 if (*vr0type == VR_RANGE
7730 && vr1type == VR_RANGE)
7731 *vr0min = vr1min;
7732 else if (*vr0type == VR_ANTI_RANGE
7733 && vr1type == VR_ANTI_RANGE)
7734 *vr0max = vr1max;
7735 else if (*vr0type == VR_ANTI_RANGE
7736 && vr1type == VR_RANGE)
7738 if (TREE_CODE (vr1max) == INTEGER_CST)
7739 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7740 build_int_cst (TREE_TYPE (vr1max), 1));
7741 else
7742 goto give_up;
7744 else if (*vr0type == VR_RANGE
7745 && vr1type == VR_ANTI_RANGE)
7747 if (TREE_CODE (*vr0min) == INTEGER_CST)
7749 *vr0type = vr1type;
7750 *vr0min = vr1min;
7751 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7752 build_int_cst (TREE_TYPE (*vr0min), 1));
7754 else
7755 goto give_up;
7757 else
7758 gcc_unreachable ();
7760 else
7761 goto give_up;
7763 return;
7765 give_up:
7766 *vr0type = VR_VARYING;
7767 *vr0min = NULL_TREE;
7768 *vr0max = NULL_TREE;
7771 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7772 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7773 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7774 possible such range. The resulting range is not canonicalized. */
7776 static void
7777 intersect_ranges (enum value_range_type *vr0type,
7778 tree *vr0min, tree *vr0max,
7779 enum value_range_type vr1type,
7780 tree vr1min, tree vr1max)
7782 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7783 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7785 /* [] is vr0, () is vr1 in the following classification comments. */
7786 if (mineq && maxeq)
7788 /* [( )] */
7789 if (*vr0type == vr1type)
7790 /* Nothing to do for equal ranges. */
7792 else if ((*vr0type == VR_RANGE
7793 && vr1type == VR_ANTI_RANGE)
7794 || (*vr0type == VR_ANTI_RANGE
7795 && vr1type == VR_RANGE))
7797 /* For anti-range with range intersection the result is empty. */
7798 *vr0type = VR_UNDEFINED;
7799 *vr0min = NULL_TREE;
7800 *vr0max = NULL_TREE;
7802 else
7803 gcc_unreachable ();
7805 else if (operand_less_p (*vr0max, vr1min) == 1
7806 || operand_less_p (vr1max, *vr0min) == 1)
7808 /* [ ] ( ) or ( ) [ ]
7809 If the ranges have an empty intersection, the result of the
7810 intersect operation is the range for intersecting an
7811 anti-range with a range or empty when intersecting two ranges. */
7812 if (*vr0type == VR_RANGE
7813 && vr1type == VR_ANTI_RANGE)
7815 else if (*vr0type == VR_ANTI_RANGE
7816 && vr1type == VR_RANGE)
7818 *vr0type = vr1type;
7819 *vr0min = vr1min;
7820 *vr0max = vr1max;
7822 else if (*vr0type == VR_RANGE
7823 && vr1type == VR_RANGE)
7825 *vr0type = VR_UNDEFINED;
7826 *vr0min = NULL_TREE;
7827 *vr0max = NULL_TREE;
7829 else if (*vr0type == VR_ANTI_RANGE
7830 && vr1type == VR_ANTI_RANGE)
7832 /* If the anti-ranges are adjacent to each other merge them. */
7833 if (TREE_CODE (*vr0max) == INTEGER_CST
7834 && TREE_CODE (vr1min) == INTEGER_CST
7835 && operand_less_p (*vr0max, vr1min) == 1
7836 && integer_onep (int_const_binop (MINUS_EXPR,
7837 vr1min, *vr0max)))
7838 *vr0max = vr1max;
7839 else if (TREE_CODE (vr1max) == INTEGER_CST
7840 && TREE_CODE (*vr0min) == INTEGER_CST
7841 && operand_less_p (vr1max, *vr0min) == 1
7842 && integer_onep (int_const_binop (MINUS_EXPR,
7843 *vr0min, vr1max)))
7844 *vr0min = vr1min;
7845 /* Else arbitrarily take VR0. */
7848 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7849 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7851 /* [ ( ) ] or [( ) ] or [ ( )] */
7852 if (*vr0type == VR_RANGE
7853 && vr1type == VR_RANGE)
7855 /* If both are ranges the result is the inner one. */
7856 *vr0type = vr1type;
7857 *vr0min = vr1min;
7858 *vr0max = vr1max;
7860 else if (*vr0type == VR_RANGE
7861 && vr1type == VR_ANTI_RANGE)
7863 /* Choose the right gap if the left one is empty. */
7864 if (mineq)
7866 if (TREE_CODE (vr1max) == INTEGER_CST)
7867 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7868 build_int_cst (TREE_TYPE (vr1max), 1));
7869 else
7870 *vr0min = vr1max;
7872 /* Choose the left gap if the right one is empty. */
7873 else if (maxeq)
7875 if (TREE_CODE (vr1min) == INTEGER_CST)
7876 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7877 build_int_cst (TREE_TYPE (vr1min), 1));
7878 else
7879 *vr0max = vr1min;
7881 /* Choose the anti-range if the range is effectively varying. */
7882 else if (vrp_val_is_min (*vr0min)
7883 && vrp_val_is_max (*vr0max))
7885 *vr0type = vr1type;
7886 *vr0min = vr1min;
7887 *vr0max = vr1max;
7889 /* Else choose the range. */
7891 else if (*vr0type == VR_ANTI_RANGE
7892 && vr1type == VR_ANTI_RANGE)
7893 /* If both are anti-ranges the result is the outer one. */
7895 else if (*vr0type == VR_ANTI_RANGE
7896 && vr1type == VR_RANGE)
7898 /* The intersection is empty. */
7899 *vr0type = VR_UNDEFINED;
7900 *vr0min = NULL_TREE;
7901 *vr0max = NULL_TREE;
7903 else
7904 gcc_unreachable ();
7906 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7907 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7909 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7910 if (*vr0type == VR_RANGE
7911 && vr1type == VR_RANGE)
7912 /* Choose the inner range. */
7914 else if (*vr0type == VR_ANTI_RANGE
7915 && vr1type == VR_RANGE)
7917 /* Choose the right gap if the left is empty. */
7918 if (mineq)
7920 *vr0type = VR_RANGE;
7921 if (TREE_CODE (*vr0max) == INTEGER_CST)
7922 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7923 build_int_cst (TREE_TYPE (*vr0max), 1));
7924 else
7925 *vr0min = *vr0max;
7926 *vr0max = vr1max;
7928 /* Choose the left gap if the right is empty. */
7929 else if (maxeq)
7931 *vr0type = VR_RANGE;
7932 if (TREE_CODE (*vr0min) == INTEGER_CST)
7933 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7934 build_int_cst (TREE_TYPE (*vr0min), 1));
7935 else
7936 *vr0max = *vr0min;
7937 *vr0min = vr1min;
7939 /* Choose the anti-range if the range is effectively varying. */
7940 else if (vrp_val_is_min (vr1min)
7941 && vrp_val_is_max (vr1max))
7943 /* Else choose the range. */
7944 else
7946 *vr0type = vr1type;
7947 *vr0min = vr1min;
7948 *vr0max = vr1max;
7951 else if (*vr0type == VR_ANTI_RANGE
7952 && vr1type == VR_ANTI_RANGE)
7954 /* If both are anti-ranges the result is the outer one. */
7955 *vr0type = vr1type;
7956 *vr0min = vr1min;
7957 *vr0max = vr1max;
7959 else if (vr1type == VR_ANTI_RANGE
7960 && *vr0type == VR_RANGE)
7962 /* The intersection is empty. */
7963 *vr0type = VR_UNDEFINED;
7964 *vr0min = NULL_TREE;
7965 *vr0max = NULL_TREE;
7967 else
7968 gcc_unreachable ();
7970 else if ((operand_less_p (vr1min, *vr0max) == 1
7971 || operand_equal_p (vr1min, *vr0max, 0))
7972 && operand_less_p (*vr0min, vr1min) == 1)
7974 /* [ ( ] ) or [ ]( ) */
7975 if (*vr0type == VR_ANTI_RANGE
7976 && vr1type == VR_ANTI_RANGE)
7977 *vr0max = vr1max;
7978 else if (*vr0type == VR_RANGE
7979 && vr1type == VR_RANGE)
7980 *vr0min = vr1min;
7981 else if (*vr0type == VR_RANGE
7982 && vr1type == VR_ANTI_RANGE)
7984 if (TREE_CODE (vr1min) == INTEGER_CST)
7985 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7986 build_int_cst (TREE_TYPE (vr1min), 1));
7987 else
7988 *vr0max = vr1min;
7990 else if (*vr0type == VR_ANTI_RANGE
7991 && vr1type == VR_RANGE)
7993 *vr0type = VR_RANGE;
7994 if (TREE_CODE (*vr0max) == INTEGER_CST)
7995 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7996 build_int_cst (TREE_TYPE (*vr0max), 1));
7997 else
7998 *vr0min = *vr0max;
7999 *vr0max = vr1max;
8001 else
8002 gcc_unreachable ();
8004 else if ((operand_less_p (*vr0min, vr1max) == 1
8005 || operand_equal_p (*vr0min, vr1max, 0))
8006 && operand_less_p (vr1min, *vr0min) == 1)
8008 /* ( [ ) ] or ( )[ ] */
8009 if (*vr0type == VR_ANTI_RANGE
8010 && vr1type == VR_ANTI_RANGE)
8011 *vr0min = vr1min;
8012 else if (*vr0type == VR_RANGE
8013 && vr1type == VR_RANGE)
8014 *vr0max = vr1max;
8015 else if (*vr0type == VR_RANGE
8016 && vr1type == VR_ANTI_RANGE)
8018 if (TREE_CODE (vr1max) == INTEGER_CST)
8019 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8020 build_int_cst (TREE_TYPE (vr1max), 1));
8021 else
8022 *vr0min = vr1max;
8024 else if (*vr0type == VR_ANTI_RANGE
8025 && vr1type == VR_RANGE)
8027 *vr0type = VR_RANGE;
8028 if (TREE_CODE (*vr0min) == INTEGER_CST)
8029 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8030 build_int_cst (TREE_TYPE (*vr0min), 1));
8031 else
8032 *vr0max = *vr0min;
8033 *vr0min = vr1min;
8035 else
8036 gcc_unreachable ();
8039 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8040 result for the intersection. That's always a conservative
8041 correct estimate. */
8043 return;
8047 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8048 in *VR0. This may not be the smallest possible such range. */
8050 static void
8051 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8053 value_range_t saved;
8055 /* If either range is VR_VARYING the other one wins. */
8056 if (vr1->type == VR_VARYING)
8057 return;
8058 if (vr0->type == VR_VARYING)
8060 copy_value_range (vr0, vr1);
8061 return;
8064 /* When either range is VR_UNDEFINED the resulting range is
8065 VR_UNDEFINED, too. */
8066 if (vr0->type == VR_UNDEFINED)
8067 return;
8068 if (vr1->type == VR_UNDEFINED)
8070 set_value_range_to_undefined (vr0);
8071 return;
8074 /* Save the original vr0 so we can return it as conservative intersection
8075 result when our worker turns things to varying. */
8076 saved = *vr0;
8077 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8078 vr1->type, vr1->min, vr1->max);
8079 /* Make sure to canonicalize the result though as the inversion of a
8080 VR_RANGE can still be a VR_RANGE. */
8081 set_and_canonicalize_value_range (vr0, vr0->type,
8082 vr0->min, vr0->max, vr0->equiv);
8083 /* If that failed, use the saved original VR0. */
8084 if (vr0->type == VR_VARYING)
8086 *vr0 = saved;
8087 return;
8089 /* If the result is VR_UNDEFINED there is no need to mess with
8090 the equivalencies. */
8091 if (vr0->type == VR_UNDEFINED)
8092 return;
8094 /* The resulting set of equivalences for range intersection is the union of
8095 the two sets. */
8096 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8097 bitmap_ior_into (vr0->equiv, vr1->equiv);
8098 else if (vr1->equiv && !vr0->equiv)
8099 bitmap_copy (vr0->equiv, vr1->equiv);
8102 static void
8103 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8105 if (dump_file && (dump_flags & TDF_DETAILS))
8107 fprintf (dump_file, "Intersecting\n ");
8108 dump_value_range (dump_file, vr0);
8109 fprintf (dump_file, "\nand\n ");
8110 dump_value_range (dump_file, vr1);
8111 fprintf (dump_file, "\n");
8113 vrp_intersect_ranges_1 (vr0, vr1);
8114 if (dump_file && (dump_flags & TDF_DETAILS))
8116 fprintf (dump_file, "to\n ");
8117 dump_value_range (dump_file, vr0);
8118 fprintf (dump_file, "\n");
8122 /* Meet operation for value ranges. Given two value ranges VR0 and
8123 VR1, store in VR0 a range that contains both VR0 and VR1. This
8124 may not be the smallest possible such range. */
8126 static void
8127 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8129 value_range_t saved;
8131 if (vr0->type == VR_UNDEFINED)
8133 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8134 return;
8137 if (vr1->type == VR_UNDEFINED)
8139 /* VR0 already has the resulting range. */
8140 return;
8143 if (vr0->type == VR_VARYING)
8145 /* Nothing to do. VR0 already has the resulting range. */
8146 return;
8149 if (vr1->type == VR_VARYING)
8151 set_value_range_to_varying (vr0);
8152 return;
8155 saved = *vr0;
8156 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8157 vr1->type, vr1->min, vr1->max);
8158 if (vr0->type == VR_VARYING)
8160 /* Failed to find an efficient meet. Before giving up and setting
8161 the result to VARYING, see if we can at least derive a useful
8162 anti-range. FIXME, all this nonsense about distinguishing
8163 anti-ranges from ranges is necessary because of the odd
8164 semantics of range_includes_zero_p and friends. */
8165 if (((saved.type == VR_RANGE
8166 && range_includes_zero_p (saved.min, saved.max) == 0)
8167 || (saved.type == VR_ANTI_RANGE
8168 && range_includes_zero_p (saved.min, saved.max) == 1))
8169 && ((vr1->type == VR_RANGE
8170 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8171 || (vr1->type == VR_ANTI_RANGE
8172 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8174 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8176 /* Since this meet operation did not result from the meeting of
8177 two equivalent names, VR0 cannot have any equivalences. */
8178 if (vr0->equiv)
8179 bitmap_clear (vr0->equiv);
8180 return;
8183 set_value_range_to_varying (vr0);
8184 return;
8186 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8187 vr0->equiv);
8188 if (vr0->type == VR_VARYING)
8189 return;
8191 /* The resulting set of equivalences is always the intersection of
8192 the two sets. */
8193 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8194 bitmap_and_into (vr0->equiv, vr1->equiv);
8195 else if (vr0->equiv && !vr1->equiv)
8196 bitmap_clear (vr0->equiv);
8199 static void
8200 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8202 if (dump_file && (dump_flags & TDF_DETAILS))
8204 fprintf (dump_file, "Meeting\n ");
8205 dump_value_range (dump_file, vr0);
8206 fprintf (dump_file, "\nand\n ");
8207 dump_value_range (dump_file, vr1);
8208 fprintf (dump_file, "\n");
8210 vrp_meet_1 (vr0, vr1);
8211 if (dump_file && (dump_flags & TDF_DETAILS))
8213 fprintf (dump_file, "to\n ");
8214 dump_value_range (dump_file, vr0);
8215 fprintf (dump_file, "\n");
8220 /* Visit all arguments for PHI node PHI that flow through executable
8221 edges. If a valid value range can be derived from all the incoming
8222 value ranges, set a new range for the LHS of PHI. */
8224 static enum ssa_prop_result
8225 vrp_visit_phi_node (gimple phi)
8227 size_t i;
8228 tree lhs = PHI_RESULT (phi);
8229 value_range_t *lhs_vr = get_value_range (lhs);
8230 value_range_t vr_result = VR_INITIALIZER;
8231 bool first = true;
8232 int edges, old_edges;
8233 struct loop *l;
8235 if (dump_file && (dump_flags & TDF_DETAILS))
8237 fprintf (dump_file, "\nVisiting PHI node: ");
8238 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8241 edges = 0;
8242 for (i = 0; i < gimple_phi_num_args (phi); i++)
8244 edge e = gimple_phi_arg_edge (phi, i);
8246 if (dump_file && (dump_flags & TDF_DETAILS))
8248 fprintf (dump_file,
8249 "\n Argument #%d (%d -> %d %sexecutable)\n",
8250 (int) i, e->src->index, e->dest->index,
8251 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8254 if (e->flags & EDGE_EXECUTABLE)
8256 tree arg = PHI_ARG_DEF (phi, i);
8257 value_range_t vr_arg;
8259 ++edges;
8261 if (TREE_CODE (arg) == SSA_NAME)
8263 vr_arg = *(get_value_range (arg));
8264 /* Do not allow equivalences or symbolic ranges to leak in from
8265 backedges. That creates invalid equivalencies.
8266 See PR53465 and PR54767. */
8267 if (e->flags & EDGE_DFS_BACK
8268 && (vr_arg.type == VR_RANGE
8269 || vr_arg.type == VR_ANTI_RANGE))
8271 vr_arg.equiv = NULL;
8272 if (symbolic_range_p (&vr_arg))
8274 vr_arg.type = VR_VARYING;
8275 vr_arg.min = NULL_TREE;
8276 vr_arg.max = NULL_TREE;
8280 else
8282 if (TREE_OVERFLOW_P (arg))
8283 arg = drop_tree_overflow (arg);
8285 vr_arg.type = VR_RANGE;
8286 vr_arg.min = arg;
8287 vr_arg.max = arg;
8288 vr_arg.equiv = NULL;
8291 if (dump_file && (dump_flags & TDF_DETAILS))
8293 fprintf (dump_file, "\t");
8294 print_generic_expr (dump_file, arg, dump_flags);
8295 fprintf (dump_file, "\n\tValue: ");
8296 dump_value_range (dump_file, &vr_arg);
8297 fprintf (dump_file, "\n");
8300 if (first)
8301 copy_value_range (&vr_result, &vr_arg);
8302 else
8303 vrp_meet (&vr_result, &vr_arg);
8304 first = false;
8306 if (vr_result.type == VR_VARYING)
8307 break;
8311 if (vr_result.type == VR_VARYING)
8312 goto varying;
8313 else if (vr_result.type == VR_UNDEFINED)
8314 goto update_range;
8316 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8317 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8319 /* To prevent infinite iterations in the algorithm, derive ranges
8320 when the new value is slightly bigger or smaller than the
8321 previous one. We don't do this if we have seen a new executable
8322 edge; this helps us avoid an overflow infinity for conditionals
8323 which are not in a loop. If the old value-range was VR_UNDEFINED
8324 use the updated range and iterate one more time. */
8325 if (edges > 0
8326 && gimple_phi_num_args (phi) > 1
8327 && edges == old_edges
8328 && lhs_vr->type != VR_UNDEFINED)
8330 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8331 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8333 /* For non VR_RANGE or for pointers fall back to varying if
8334 the range changed. */
8335 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8336 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8337 && (cmp_min != 0 || cmp_max != 0))
8338 goto varying;
8340 /* If the new minimum is larger than than the previous one
8341 retain the old value. If the new minimum value is smaller
8342 than the previous one and not -INF go all the way to -INF + 1.
8343 In the first case, to avoid infinite bouncing between different
8344 minimums, and in the other case to avoid iterating millions of
8345 times to reach -INF. Going to -INF + 1 also lets the following
8346 iteration compute whether there will be any overflow, at the
8347 expense of one additional iteration. */
8348 if (cmp_min < 0)
8349 vr_result.min = lhs_vr->min;
8350 else if (cmp_min > 0
8351 && !vrp_val_is_min (vr_result.min))
8352 vr_result.min
8353 = int_const_binop (PLUS_EXPR,
8354 vrp_val_min (TREE_TYPE (vr_result.min)),
8355 build_int_cst (TREE_TYPE (vr_result.min), 1));
8357 /* Similarly for the maximum value. */
8358 if (cmp_max > 0)
8359 vr_result.max = lhs_vr->max;
8360 else if (cmp_max < 0
8361 && !vrp_val_is_max (vr_result.max))
8362 vr_result.max
8363 = int_const_binop (MINUS_EXPR,
8364 vrp_val_max (TREE_TYPE (vr_result.min)),
8365 build_int_cst (TREE_TYPE (vr_result.min), 1));
8367 /* If we dropped either bound to +-INF then if this is a loop
8368 PHI node SCEV may known more about its value-range. */
8369 if ((cmp_min > 0 || cmp_min < 0
8370 || cmp_max < 0 || cmp_max > 0)
8371 && current_loops
8372 && (l = loop_containing_stmt (phi))
8373 && l->header == gimple_bb (phi))
8374 adjust_range_with_scev (&vr_result, l, phi, lhs);
8376 /* If we will end up with a (-INF, +INF) range, set it to
8377 VARYING. Same if the previous max value was invalid for
8378 the type and we end up with vr_result.min > vr_result.max. */
8379 if ((vrp_val_is_max (vr_result.max)
8380 && vrp_val_is_min (vr_result.min))
8381 || compare_values (vr_result.min,
8382 vr_result.max) > 0)
8383 goto varying;
8386 /* If the new range is different than the previous value, keep
8387 iterating. */
8388 update_range:
8389 if (update_value_range (lhs, &vr_result))
8391 if (dump_file && (dump_flags & TDF_DETAILS))
8393 fprintf (dump_file, "Found new range for ");
8394 print_generic_expr (dump_file, lhs, 0);
8395 fprintf (dump_file, ": ");
8396 dump_value_range (dump_file, &vr_result);
8397 fprintf (dump_file, "\n\n");
8400 return SSA_PROP_INTERESTING;
8403 /* Nothing changed, don't add outgoing edges. */
8404 return SSA_PROP_NOT_INTERESTING;
8406 /* No match found. Set the LHS to VARYING. */
8407 varying:
8408 set_value_range_to_varying (lhs_vr);
8409 return SSA_PROP_VARYING;
8412 /* Simplify boolean operations if the source is known
8413 to be already a boolean. */
8414 static bool
8415 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8417 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8418 tree lhs, op0, op1;
8419 bool need_conversion;
8421 /* We handle only !=/== case here. */
8422 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8424 op0 = gimple_assign_rhs1 (stmt);
8425 if (!op_with_boolean_value_range_p (op0))
8426 return false;
8428 op1 = gimple_assign_rhs2 (stmt);
8429 if (!op_with_boolean_value_range_p (op1))
8430 return false;
8432 /* Reduce number of cases to handle to NE_EXPR. As there is no
8433 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8434 if (rhs_code == EQ_EXPR)
8436 if (TREE_CODE (op1) == INTEGER_CST)
8437 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8438 build_int_cst (TREE_TYPE (op1), 1));
8439 else
8440 return false;
8443 lhs = gimple_assign_lhs (stmt);
8444 need_conversion
8445 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8447 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8448 if (need_conversion
8449 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8450 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8451 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8452 return false;
8454 /* For A != 0 we can substitute A itself. */
8455 if (integer_zerop (op1))
8456 gimple_assign_set_rhs_with_ops (gsi,
8457 need_conversion
8458 ? NOP_EXPR : TREE_CODE (op0),
8459 op0, NULL_TREE);
8460 /* For A != B we substitute A ^ B. Either with conversion. */
8461 else if (need_conversion)
8463 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8464 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8465 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8466 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8468 /* Or without. */
8469 else
8470 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8471 update_stmt (gsi_stmt (*gsi));
8473 return true;
8476 /* Simplify a division or modulo operator to a right shift or
8477 bitwise and if the first operand is unsigned or is greater
8478 than zero and the second operand is an exact power of two. */
8480 static bool
8481 simplify_div_or_mod_using_ranges (gimple stmt)
8483 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8484 tree val = NULL;
8485 tree op0 = gimple_assign_rhs1 (stmt);
8486 tree op1 = gimple_assign_rhs2 (stmt);
8487 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8489 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8491 val = integer_one_node;
8493 else
8495 bool sop = false;
8497 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8499 if (val
8500 && sop
8501 && integer_onep (val)
8502 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8504 location_t location;
8506 if (!gimple_has_location (stmt))
8507 location = input_location;
8508 else
8509 location = gimple_location (stmt);
8510 warning_at (location, OPT_Wstrict_overflow,
8511 "assuming signed overflow does not occur when "
8512 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8516 if (val && integer_onep (val))
8518 tree t;
8520 if (rhs_code == TRUNC_DIV_EXPR)
8522 t = build_int_cst (integer_type_node, tree_log2 (op1));
8523 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8524 gimple_assign_set_rhs1 (stmt, op0);
8525 gimple_assign_set_rhs2 (stmt, t);
8527 else
8529 t = build_int_cst (TREE_TYPE (op1), 1);
8530 t = int_const_binop (MINUS_EXPR, op1, t);
8531 t = fold_convert (TREE_TYPE (op0), t);
8533 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8534 gimple_assign_set_rhs1 (stmt, op0);
8535 gimple_assign_set_rhs2 (stmt, t);
8538 update_stmt (stmt);
8539 return true;
8542 return false;
8545 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8546 ABS_EXPR. If the operand is <= 0, then simplify the
8547 ABS_EXPR into a NEGATE_EXPR. */
8549 static bool
8550 simplify_abs_using_ranges (gimple stmt)
8552 tree val = NULL;
8553 tree op = gimple_assign_rhs1 (stmt);
8554 tree type = TREE_TYPE (op);
8555 value_range_t *vr = get_value_range (op);
8557 if (TYPE_UNSIGNED (type))
8559 val = integer_zero_node;
8561 else if (vr)
8563 bool sop = false;
8565 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8566 if (!val)
8568 sop = false;
8569 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8570 &sop);
8572 if (val)
8574 if (integer_zerop (val))
8575 val = integer_one_node;
8576 else if (integer_onep (val))
8577 val = integer_zero_node;
8581 if (val
8582 && (integer_onep (val) || integer_zerop (val)))
8584 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8586 location_t location;
8588 if (!gimple_has_location (stmt))
8589 location = input_location;
8590 else
8591 location = gimple_location (stmt);
8592 warning_at (location, OPT_Wstrict_overflow,
8593 "assuming signed overflow does not occur when "
8594 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8597 gimple_assign_set_rhs1 (stmt, op);
8598 if (integer_onep (val))
8599 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8600 else
8601 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8602 update_stmt (stmt);
8603 return true;
8607 return false;
8610 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8611 If all the bits that are being cleared by & are already
8612 known to be zero from VR, or all the bits that are being
8613 set by | are already known to be one from VR, the bit
8614 operation is redundant. */
8616 static bool
8617 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8619 tree op0 = gimple_assign_rhs1 (stmt);
8620 tree op1 = gimple_assign_rhs2 (stmt);
8621 tree op = NULL_TREE;
8622 value_range_t vr0 = VR_INITIALIZER;
8623 value_range_t vr1 = VR_INITIALIZER;
8624 wide_int may_be_nonzero0, may_be_nonzero1;
8625 wide_int must_be_nonzero0, must_be_nonzero1;
8626 wide_int mask;
8628 if (TREE_CODE (op0) == SSA_NAME)
8629 vr0 = *(get_value_range (op0));
8630 else if (is_gimple_min_invariant (op0))
8631 set_value_range_to_value (&vr0, op0, NULL);
8632 else
8633 return false;
8635 if (TREE_CODE (op1) == SSA_NAME)
8636 vr1 = *(get_value_range (op1));
8637 else if (is_gimple_min_invariant (op1))
8638 set_value_range_to_value (&vr1, op1, NULL);
8639 else
8640 return false;
8642 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
8643 &must_be_nonzero0))
8644 return false;
8645 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
8646 &must_be_nonzero1))
8647 return false;
8649 switch (gimple_assign_rhs_code (stmt))
8651 case BIT_AND_EXPR:
8652 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8653 if (mask == 0)
8655 op = op0;
8656 break;
8658 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8659 if (mask == 0)
8661 op = op1;
8662 break;
8664 break;
8665 case BIT_IOR_EXPR:
8666 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8667 if (mask == 0)
8669 op = op1;
8670 break;
8672 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8673 if (mask == 0)
8675 op = op0;
8676 break;
8678 break;
8679 default:
8680 gcc_unreachable ();
8683 if (op == NULL_TREE)
8684 return false;
8686 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8687 update_stmt (gsi_stmt (*gsi));
8688 return true;
8691 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8692 a known value range VR.
8694 If there is one and only one value which will satisfy the
8695 conditional, then return that value. Else return NULL. */
8697 static tree
8698 test_for_singularity (enum tree_code cond_code, tree op0,
8699 tree op1, value_range_t *vr)
8701 tree min = NULL;
8702 tree max = NULL;
8704 /* Extract minimum/maximum values which satisfy the
8705 the conditional as it was written. */
8706 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8708 /* This should not be negative infinity; there is no overflow
8709 here. */
8710 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8712 max = op1;
8713 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8715 tree one = build_int_cst (TREE_TYPE (op0), 1);
8716 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8717 if (EXPR_P (max))
8718 TREE_NO_WARNING (max) = 1;
8721 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8723 /* This should not be positive infinity; there is no overflow
8724 here. */
8725 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8727 min = op1;
8728 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8730 tree one = build_int_cst (TREE_TYPE (op0), 1);
8731 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8732 if (EXPR_P (min))
8733 TREE_NO_WARNING (min) = 1;
8737 /* Now refine the minimum and maximum values using any
8738 value range information we have for op0. */
8739 if (min && max)
8741 if (compare_values (vr->min, min) == 1)
8742 min = vr->min;
8743 if (compare_values (vr->max, max) == -1)
8744 max = vr->max;
8746 /* If the new min/max values have converged to a single value,
8747 then there is only one value which can satisfy the condition,
8748 return that value. */
8749 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
8750 return min;
8752 return NULL;
8755 /* Return whether the value range *VR fits in an integer type specified
8756 by PRECISION and UNSIGNED_P. */
8758 static bool
8759 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
8761 tree src_type;
8762 unsigned src_precision;
8763 widest_int tem;
8764 signop src_sgn;
8766 /* We can only handle integral and pointer types. */
8767 src_type = TREE_TYPE (vr->min);
8768 if (!INTEGRAL_TYPE_P (src_type)
8769 && !POINTER_TYPE_P (src_type))
8770 return false;
8772 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
8773 and so is an identity transform. */
8774 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
8775 src_sgn = TYPE_SIGN (src_type);
8776 if ((src_precision < dest_precision
8777 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
8778 || (src_precision == dest_precision && src_sgn == dest_sgn))
8779 return true;
8781 /* Now we can only handle ranges with constant bounds. */
8782 if (vr->type != VR_RANGE
8783 || TREE_CODE (vr->min) != INTEGER_CST
8784 || TREE_CODE (vr->max) != INTEGER_CST)
8785 return false;
8787 /* For sign changes, the MSB of the wide_int has to be clear.
8788 An unsigned value with its MSB set cannot be represented by
8789 a signed wide_int, while a negative value cannot be represented
8790 by an unsigned wide_int. */
8791 if (src_sgn != dest_sgn
8792 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
8793 return false;
8795 /* Then we can perform the conversion on both ends and compare
8796 the result for equality. */
8797 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
8798 if (tem != wi::to_widest (vr->min))
8799 return false;
8800 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
8801 if (tem != wi::to_widest (vr->max))
8802 return false;
8804 return true;
8807 /* Simplify a conditional using a relational operator to an equality
8808 test if the range information indicates only one value can satisfy
8809 the original conditional. */
8811 static bool
8812 simplify_cond_using_ranges (gimple stmt)
8814 tree op0 = gimple_cond_lhs (stmt);
8815 tree op1 = gimple_cond_rhs (stmt);
8816 enum tree_code cond_code = gimple_cond_code (stmt);
8818 if (cond_code != NE_EXPR
8819 && cond_code != EQ_EXPR
8820 && TREE_CODE (op0) == SSA_NAME
8821 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8822 && is_gimple_min_invariant (op1))
8824 value_range_t *vr = get_value_range (op0);
8826 /* If we have range information for OP0, then we might be
8827 able to simplify this conditional. */
8828 if (vr->type == VR_RANGE)
8830 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
8832 if (new_tree)
8834 if (dump_file)
8836 fprintf (dump_file, "Simplified relational ");
8837 print_gimple_stmt (dump_file, stmt, 0, 0);
8838 fprintf (dump_file, " into ");
8841 gimple_cond_set_code (stmt, EQ_EXPR);
8842 gimple_cond_set_lhs (stmt, op0);
8843 gimple_cond_set_rhs (stmt, new_tree);
8845 update_stmt (stmt);
8847 if (dump_file)
8849 print_gimple_stmt (dump_file, stmt, 0, 0);
8850 fprintf (dump_file, "\n");
8853 return true;
8856 /* Try again after inverting the condition. We only deal
8857 with integral types here, so no need to worry about
8858 issues with inverting FP comparisons. */
8859 cond_code = invert_tree_comparison (cond_code, false);
8860 new_tree = test_for_singularity (cond_code, op0, op1, vr);
8862 if (new_tree)
8864 if (dump_file)
8866 fprintf (dump_file, "Simplified relational ");
8867 print_gimple_stmt (dump_file, stmt, 0, 0);
8868 fprintf (dump_file, " into ");
8871 gimple_cond_set_code (stmt, NE_EXPR);
8872 gimple_cond_set_lhs (stmt, op0);
8873 gimple_cond_set_rhs (stmt, new_tree);
8875 update_stmt (stmt);
8877 if (dump_file)
8879 print_gimple_stmt (dump_file, stmt, 0, 0);
8880 fprintf (dump_file, "\n");
8883 return true;
8888 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8889 see if OP0 was set by a type conversion where the source of
8890 the conversion is another SSA_NAME with a range that fits
8891 into the range of OP0's type.
8893 If so, the conversion is redundant as the earlier SSA_NAME can be
8894 used for the comparison directly if we just massage the constant in the
8895 comparison. */
8896 if (TREE_CODE (op0) == SSA_NAME
8897 && TREE_CODE (op1) == INTEGER_CST)
8899 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
8900 tree innerop;
8902 if (!is_gimple_assign (def_stmt)
8903 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
8904 return false;
8906 innerop = gimple_assign_rhs1 (def_stmt);
8908 if (TREE_CODE (innerop) == SSA_NAME
8909 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
8911 value_range_t *vr = get_value_range (innerop);
8913 if (range_int_cst_p (vr)
8914 && range_fits_type_p (vr,
8915 TYPE_PRECISION (TREE_TYPE (op0)),
8916 TYPE_SIGN (TREE_TYPE (op0)))
8917 && int_fits_type_p (op1, TREE_TYPE (innerop))
8918 /* The range must not have overflowed, or if it did overflow
8919 we must not be wrapping/trapping overflow and optimizing
8920 with strict overflow semantics. */
8921 && ((!is_negative_overflow_infinity (vr->min)
8922 && !is_positive_overflow_infinity (vr->max))
8923 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
8925 /* If the range overflowed and the user has asked for warnings
8926 when strict overflow semantics were used to optimize code,
8927 issue an appropriate warning. */
8928 if ((is_negative_overflow_infinity (vr->min)
8929 || is_positive_overflow_infinity (vr->max))
8930 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
8932 location_t location;
8934 if (!gimple_has_location (stmt))
8935 location = input_location;
8936 else
8937 location = gimple_location (stmt);
8938 warning_at (location, OPT_Wstrict_overflow,
8939 "assuming signed overflow does not occur when "
8940 "simplifying conditional");
8943 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
8944 gimple_cond_set_lhs (stmt, innerop);
8945 gimple_cond_set_rhs (stmt, newconst);
8946 return true;
8951 return false;
8954 /* Simplify a switch statement using the value range of the switch
8955 argument. */
8957 static bool
8958 simplify_switch_using_ranges (gimple stmt)
8960 tree op = gimple_switch_index (stmt);
8961 value_range_t *vr;
8962 bool take_default;
8963 edge e;
8964 edge_iterator ei;
8965 size_t i = 0, j = 0, n, n2;
8966 tree vec2;
8967 switch_update su;
8968 size_t k = 1, l = 0;
8970 if (TREE_CODE (op) == SSA_NAME)
8972 vr = get_value_range (op);
8974 /* We can only handle integer ranges. */
8975 if ((vr->type != VR_RANGE
8976 && vr->type != VR_ANTI_RANGE)
8977 || symbolic_range_p (vr))
8978 return false;
8980 /* Find case label for min/max of the value range. */
8981 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
8983 else if (TREE_CODE (op) == INTEGER_CST)
8985 take_default = !find_case_label_index (stmt, 1, op, &i);
8986 if (take_default)
8988 i = 1;
8989 j = 0;
8991 else
8993 j = i;
8996 else
8997 return false;
8999 n = gimple_switch_num_labels (stmt);
9001 /* Bail out if this is just all edges taken. */
9002 if (i == 1
9003 && j == n - 1
9004 && take_default)
9005 return false;
9007 /* Build a new vector of taken case labels. */
9008 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9009 n2 = 0;
9011 /* Add the default edge, if necessary. */
9012 if (take_default)
9013 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9015 for (; i <= j; ++i, ++n2)
9016 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9018 for (; k <= l; ++k, ++n2)
9019 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9021 /* Mark needed edges. */
9022 for (i = 0; i < n2; ++i)
9024 e = find_edge (gimple_bb (stmt),
9025 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9026 e->aux = (void *)-1;
9029 /* Queue not needed edges for later removal. */
9030 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9032 if (e->aux == (void *)-1)
9034 e->aux = NULL;
9035 continue;
9038 if (dump_file && (dump_flags & TDF_DETAILS))
9040 fprintf (dump_file, "removing unreachable case label\n");
9042 to_remove_edges.safe_push (e);
9043 e->flags &= ~EDGE_EXECUTABLE;
9046 /* And queue an update for the stmt. */
9047 su.stmt = stmt;
9048 su.vec = vec2;
9049 to_update_switch_stmts.safe_push (su);
9050 return false;
9053 /* Simplify an integral conversion from an SSA name in STMT. */
9055 static bool
9056 simplify_conversion_using_ranges (gimple stmt)
9058 tree innerop, middleop, finaltype;
9059 gimple def_stmt;
9060 value_range_t *innervr;
9061 signop inner_sgn, middle_sgn, final_sgn;
9062 unsigned inner_prec, middle_prec, final_prec;
9063 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9065 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9066 if (!INTEGRAL_TYPE_P (finaltype))
9067 return false;
9068 middleop = gimple_assign_rhs1 (stmt);
9069 def_stmt = SSA_NAME_DEF_STMT (middleop);
9070 if (!is_gimple_assign (def_stmt)
9071 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9072 return false;
9073 innerop = gimple_assign_rhs1 (def_stmt);
9074 if (TREE_CODE (innerop) != SSA_NAME
9075 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9076 return false;
9078 /* Get the value-range of the inner operand. */
9079 innervr = get_value_range (innerop);
9080 if (innervr->type != VR_RANGE
9081 || TREE_CODE (innervr->min) != INTEGER_CST
9082 || TREE_CODE (innervr->max) != INTEGER_CST)
9083 return false;
9085 /* Simulate the conversion chain to check if the result is equal if
9086 the middle conversion is removed. */
9087 innermin = wi::to_widest (innervr->min);
9088 innermax = wi::to_widest (innervr->max);
9090 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9091 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9092 final_prec = TYPE_PRECISION (finaltype);
9094 /* If the first conversion is not injective, the second must not
9095 be widening. */
9096 if (wi::gtu_p (innermax - innermin,
9097 wi::mask <widest_int> (middle_prec, false))
9098 && middle_prec < final_prec)
9099 return false;
9100 /* We also want a medium value so that we can track the effect that
9101 narrowing conversions with sign change have. */
9102 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9103 if (inner_sgn == UNSIGNED)
9104 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9105 else
9106 innermed = 0;
9107 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9108 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9109 innermed = innermin;
9111 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9112 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9113 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9114 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9116 /* Require that the final conversion applied to both the original
9117 and the intermediate range produces the same result. */
9118 final_sgn = TYPE_SIGN (finaltype);
9119 if (wi::ext (middlemin, final_prec, final_sgn)
9120 != wi::ext (innermin, final_prec, final_sgn)
9121 || wi::ext (middlemed, final_prec, final_sgn)
9122 != wi::ext (innermed, final_prec, final_sgn)
9123 || wi::ext (middlemax, final_prec, final_sgn)
9124 != wi::ext (innermax, final_prec, final_sgn))
9125 return false;
9127 gimple_assign_set_rhs1 (stmt, innerop);
9128 update_stmt (stmt);
9129 return true;
9132 /* Simplify a conversion from integral SSA name to float in STMT. */
9134 static bool
9135 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9137 tree rhs1 = gimple_assign_rhs1 (stmt);
9138 value_range_t *vr = get_value_range (rhs1);
9139 enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9140 enum machine_mode mode;
9141 tree tem;
9142 gimple conv;
9144 /* We can only handle constant ranges. */
9145 if (vr->type != VR_RANGE
9146 || TREE_CODE (vr->min) != INTEGER_CST
9147 || TREE_CODE (vr->max) != INTEGER_CST)
9148 return false;
9150 /* First check if we can use a signed type in place of an unsigned. */
9151 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9152 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9153 != CODE_FOR_nothing)
9154 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9155 mode = TYPE_MODE (TREE_TYPE (rhs1));
9156 /* If we can do the conversion in the current input mode do nothing. */
9157 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9158 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9159 return false;
9160 /* Otherwise search for a mode we can use, starting from the narrowest
9161 integer mode available. */
9162 else
9164 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9167 /* If we cannot do a signed conversion to float from mode
9168 or if the value-range does not fit in the signed type
9169 try with a wider mode. */
9170 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9171 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9172 break;
9174 mode = GET_MODE_WIDER_MODE (mode);
9175 /* But do not widen the input. Instead leave that to the
9176 optabs expansion code. */
9177 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9178 return false;
9180 while (mode != VOIDmode);
9181 if (mode == VOIDmode)
9182 return false;
9185 /* It works, insert a truncation or sign-change before the
9186 float conversion. */
9187 tem = make_ssa_name (build_nonstandard_integer_type
9188 (GET_MODE_PRECISION (mode), 0), NULL);
9189 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
9190 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9191 gimple_assign_set_rhs1 (stmt, tem);
9192 update_stmt (stmt);
9194 return true;
9197 /* Simplify an internal fn call using ranges if possible. */
9199 static bool
9200 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9202 enum tree_code subcode;
9203 switch (gimple_call_internal_fn (stmt))
9205 case IFN_UBSAN_CHECK_ADD:
9206 subcode = PLUS_EXPR;
9207 break;
9208 case IFN_UBSAN_CHECK_SUB:
9209 subcode = MINUS_EXPR;
9210 break;
9211 case IFN_UBSAN_CHECK_MUL:
9212 subcode = MULT_EXPR;
9213 break;
9214 default:
9215 return false;
9218 value_range_t vr0 = VR_INITIALIZER;
9219 value_range_t vr1 = VR_INITIALIZER;
9220 tree op0 = gimple_call_arg (stmt, 0);
9221 tree op1 = gimple_call_arg (stmt, 1);
9223 if (TREE_CODE (op0) == SSA_NAME)
9224 vr0 = *get_value_range (op0);
9225 else if (TREE_CODE (op0) == INTEGER_CST)
9226 set_value_range_to_value (&vr0, op0, NULL);
9227 else
9228 set_value_range_to_varying (&vr0);
9230 if (TREE_CODE (op1) == SSA_NAME)
9231 vr1 = *get_value_range (op1);
9232 else if (TREE_CODE (op1) == INTEGER_CST)
9233 set_value_range_to_value (&vr1, op1, NULL);
9234 else
9235 set_value_range_to_varying (&vr1);
9237 if (!range_int_cst_p (&vr0))
9239 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9240 optimize at least x = y + 0; x = y - 0; x = y * 0;
9241 and x = y * 1; which never overflow. */
9242 if (!range_int_cst_p (&vr1))
9243 return false;
9244 if (tree_int_cst_sgn (vr1.min) == -1)
9245 return false;
9246 if (compare_tree_int (vr1.max, subcode == MULT_EXPR) == 1)
9247 return false;
9249 else if (!range_int_cst_p (&vr1))
9251 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9252 optimize at least x = 0 + y; x = 0 * y; and x = 1 * y;
9253 which never overflow. */
9254 if (subcode == MINUS_EXPR)
9255 return false;
9256 if (!range_int_cst_p (&vr0))
9257 return false;
9258 if (tree_int_cst_sgn (vr0.min) == -1)
9259 return false;
9260 if (compare_tree_int (vr0.max, subcode == MULT_EXPR) == 1)
9261 return false;
9263 else
9265 tree r1 = int_const_binop (subcode, vr0.min, vr1.min);
9266 tree r2 = int_const_binop (subcode, vr0.max, vr1.max);
9267 if (r1 == NULL_TREE || TREE_OVERFLOW (r1)
9268 || r2 == NULL_TREE || TREE_OVERFLOW (r2))
9269 return false;
9270 if (subcode == MULT_EXPR)
9272 tree r3 = int_const_binop (subcode, vr0.min, vr1.max);
9273 tree r4 = int_const_binop (subcode, vr0.max, vr1.min);
9274 if (r3 == NULL_TREE || TREE_OVERFLOW (r3)
9275 || r4 == NULL_TREE || TREE_OVERFLOW (r4))
9276 return false;
9280 gimple g = gimple_build_assign_with_ops (subcode, gimple_call_lhs (stmt),
9281 op0, op1);
9282 gsi_replace (gsi, g, false);
9283 return true;
9286 /* Simplify STMT using ranges if possible. */
9288 static bool
9289 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9291 gimple stmt = gsi_stmt (*gsi);
9292 if (is_gimple_assign (stmt))
9294 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9295 tree rhs1 = gimple_assign_rhs1 (stmt);
9297 switch (rhs_code)
9299 case EQ_EXPR:
9300 case NE_EXPR:
9301 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9302 if the RHS is zero or one, and the LHS are known to be boolean
9303 values. */
9304 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9305 return simplify_truth_ops_using_ranges (gsi, stmt);
9306 break;
9308 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9309 and BIT_AND_EXPR respectively if the first operand is greater
9310 than zero and the second operand is an exact power of two. */
9311 case TRUNC_DIV_EXPR:
9312 case TRUNC_MOD_EXPR:
9313 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9314 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9315 return simplify_div_or_mod_using_ranges (stmt);
9316 break;
9318 /* Transform ABS (X) into X or -X as appropriate. */
9319 case ABS_EXPR:
9320 if (TREE_CODE (rhs1) == SSA_NAME
9321 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9322 return simplify_abs_using_ranges (stmt);
9323 break;
9325 case BIT_AND_EXPR:
9326 case BIT_IOR_EXPR:
9327 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9328 if all the bits being cleared are already cleared or
9329 all the bits being set are already set. */
9330 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9331 return simplify_bit_ops_using_ranges (gsi, stmt);
9332 break;
9334 CASE_CONVERT:
9335 if (TREE_CODE (rhs1) == SSA_NAME
9336 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9337 return simplify_conversion_using_ranges (stmt);
9338 break;
9340 case FLOAT_EXPR:
9341 if (TREE_CODE (rhs1) == SSA_NAME
9342 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9343 return simplify_float_conversion_using_ranges (gsi, stmt);
9344 break;
9346 default:
9347 break;
9350 else if (gimple_code (stmt) == GIMPLE_COND)
9351 return simplify_cond_using_ranges (stmt);
9352 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9353 return simplify_switch_using_ranges (stmt);
9354 else if (is_gimple_call (stmt)
9355 && gimple_call_internal_p (stmt))
9356 return simplify_internal_call_using_ranges (gsi, stmt);
9358 return false;
9361 /* If the statement pointed by SI has a predicate whose value can be
9362 computed using the value range information computed by VRP, compute
9363 its value and return true. Otherwise, return false. */
9365 static bool
9366 fold_predicate_in (gimple_stmt_iterator *si)
9368 bool assignment_p = false;
9369 tree val;
9370 gimple stmt = gsi_stmt (*si);
9372 if (is_gimple_assign (stmt)
9373 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9375 assignment_p = true;
9376 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9377 gimple_assign_rhs1 (stmt),
9378 gimple_assign_rhs2 (stmt),
9379 stmt);
9381 else if (gimple_code (stmt) == GIMPLE_COND)
9382 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
9383 gimple_cond_lhs (stmt),
9384 gimple_cond_rhs (stmt),
9385 stmt);
9386 else
9387 return false;
9389 if (val)
9391 if (assignment_p)
9392 val = fold_convert (gimple_expr_type (stmt), val);
9394 if (dump_file)
9396 fprintf (dump_file, "Folding predicate ");
9397 print_gimple_expr (dump_file, stmt, 0, 0);
9398 fprintf (dump_file, " to ");
9399 print_generic_expr (dump_file, val, 0);
9400 fprintf (dump_file, "\n");
9403 if (is_gimple_assign (stmt))
9404 gimple_assign_set_rhs_from_tree (si, val);
9405 else
9407 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9408 if (integer_zerop (val))
9409 gimple_cond_make_false (stmt);
9410 else if (integer_onep (val))
9411 gimple_cond_make_true (stmt);
9412 else
9413 gcc_unreachable ();
9416 return true;
9419 return false;
9422 /* Callback for substitute_and_fold folding the stmt at *SI. */
9424 static bool
9425 vrp_fold_stmt (gimple_stmt_iterator *si)
9427 if (fold_predicate_in (si))
9428 return true;
9430 return simplify_stmt_using_ranges (si);
9433 /* Stack of dest,src equivalency pairs that need to be restored after
9434 each attempt to thread a block's incoming edge to an outgoing edge.
9436 A NULL entry is used to mark the end of pairs which need to be
9437 restored. */
9438 static vec<tree> equiv_stack;
9440 /* A trivial wrapper so that we can present the generic jump threading
9441 code with a simple API for simplifying statements. STMT is the
9442 statement we want to simplify, WITHIN_STMT provides the location
9443 for any overflow warnings. */
9445 static tree
9446 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9448 if (gimple_code (stmt) == GIMPLE_COND)
9449 return vrp_evaluate_conditional (gimple_cond_code (stmt),
9450 gimple_cond_lhs (stmt),
9451 gimple_cond_rhs (stmt), within_stmt);
9453 if (gimple_code (stmt) == GIMPLE_ASSIGN)
9455 value_range_t new_vr = VR_INITIALIZER;
9456 tree lhs = gimple_assign_lhs (stmt);
9458 if (TREE_CODE (lhs) == SSA_NAME
9459 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
9460 || POINTER_TYPE_P (TREE_TYPE (lhs))))
9462 extract_range_from_assignment (&new_vr, stmt);
9463 if (range_int_cst_singleton_p (&new_vr))
9464 return new_vr.min;
9468 return NULL_TREE;
9471 /* Blocks which have more than one predecessor and more than
9472 one successor present jump threading opportunities, i.e.,
9473 when the block is reached from a specific predecessor, we
9474 may be able to determine which of the outgoing edges will
9475 be traversed. When this optimization applies, we are able
9476 to avoid conditionals at runtime and we may expose secondary
9477 optimization opportunities.
9479 This routine is effectively a driver for the generic jump
9480 threading code. It basically just presents the generic code
9481 with edges that may be suitable for jump threading.
9483 Unlike DOM, we do not iterate VRP if jump threading was successful.
9484 While iterating may expose new opportunities for VRP, it is expected
9485 those opportunities would be very limited and the compile time cost
9486 to expose those opportunities would be significant.
9488 As jump threading opportunities are discovered, they are registered
9489 for later realization. */
9491 static void
9492 identify_jump_threads (void)
9494 basic_block bb;
9495 gimple dummy;
9496 int i;
9497 edge e;
9499 /* Ugh. When substituting values earlier in this pass we can
9500 wipe the dominance information. So rebuild the dominator
9501 information as we need it within the jump threading code. */
9502 calculate_dominance_info (CDI_DOMINATORS);
9504 /* We do not allow VRP information to be used for jump threading
9505 across a back edge in the CFG. Otherwise it becomes too
9506 difficult to avoid eliminating loop exit tests. Of course
9507 EDGE_DFS_BACK is not accurate at this time so we have to
9508 recompute it. */
9509 mark_dfs_back_edges ();
9511 /* Do not thread across edges we are about to remove. Just marking
9512 them as EDGE_DFS_BACK will do. */
9513 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9514 e->flags |= EDGE_DFS_BACK;
9516 /* Allocate our unwinder stack to unwind any temporary equivalences
9517 that might be recorded. */
9518 equiv_stack.create (20);
9520 /* To avoid lots of silly node creation, we create a single
9521 conditional and just modify it in-place when attempting to
9522 thread jumps. */
9523 dummy = gimple_build_cond (EQ_EXPR,
9524 integer_zero_node, integer_zero_node,
9525 NULL, NULL);
9527 /* Walk through all the blocks finding those which present a
9528 potential jump threading opportunity. We could set this up
9529 as a dominator walker and record data during the walk, but
9530 I doubt it's worth the effort for the classes of jump
9531 threading opportunities we are trying to identify at this
9532 point in compilation. */
9533 FOR_EACH_BB_FN (bb, cfun)
9535 gimple last;
9537 /* If the generic jump threading code does not find this block
9538 interesting, then there is nothing to do. */
9539 if (! potentially_threadable_block (bb))
9540 continue;
9542 /* We only care about blocks ending in a COND_EXPR. While there
9543 may be some value in handling SWITCH_EXPR here, I doubt it's
9544 terribly important. */
9545 last = gsi_stmt (gsi_last_bb (bb));
9547 /* We're basically looking for a switch or any kind of conditional with
9548 integral or pointer type arguments. Note the type of the second
9549 argument will be the same as the first argument, so no need to
9550 check it explicitly. */
9551 if (gimple_code (last) == GIMPLE_SWITCH
9552 || (gimple_code (last) == GIMPLE_COND
9553 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9554 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9555 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9556 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9557 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9559 edge_iterator ei;
9561 /* We've got a block with multiple predecessors and multiple
9562 successors which also ends in a suitable conditional or
9563 switch statement. For each predecessor, see if we can thread
9564 it to a specific successor. */
9565 FOR_EACH_EDGE (e, ei, bb->preds)
9567 /* Do not thread across back edges or abnormal edges
9568 in the CFG. */
9569 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9570 continue;
9572 thread_across_edge (dummy, e, true, &equiv_stack,
9573 simplify_stmt_for_jump_threading);
9578 /* We do not actually update the CFG or SSA graphs at this point as
9579 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9580 handle ASSERT_EXPRs gracefully. */
9583 /* We identified all the jump threading opportunities earlier, but could
9584 not transform the CFG at that time. This routine transforms the
9585 CFG and arranges for the dominator tree to be rebuilt if necessary.
9587 Note the SSA graph update will occur during the normal TODO
9588 processing by the pass manager. */
9589 static void
9590 finalize_jump_threads (void)
9592 thread_through_all_blocks (false);
9593 equiv_stack.release ();
9597 /* Traverse all the blocks folding conditionals with known ranges. */
9599 static void
9600 vrp_finalize (void)
9602 size_t i;
9604 values_propagated = true;
9606 if (dump_file)
9608 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9609 dump_all_value_ranges (dump_file);
9610 fprintf (dump_file, "\n");
9613 substitute_and_fold (op_with_constant_singleton_value_range,
9614 vrp_fold_stmt, false);
9616 if (warn_array_bounds)
9617 check_all_array_refs ();
9619 /* We must identify jump threading opportunities before we release
9620 the datastructures built by VRP. */
9621 identify_jump_threads ();
9623 /* Set value range to non pointer SSA_NAMEs. */
9624 for (i = 0; i < num_vr_values; i++)
9625 if (vr_value[i])
9627 tree name = ssa_name (i);
9629 if (!name
9630 || POINTER_TYPE_P (TREE_TYPE (name))
9631 || (vr_value[i]->type == VR_VARYING)
9632 || (vr_value[i]->type == VR_UNDEFINED))
9633 continue;
9635 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
9636 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
9637 && (vr_value[i]->type == VR_RANGE
9638 || vr_value[i]->type == VR_ANTI_RANGE))
9639 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
9640 vr_value[i]->max);
9643 /* Free allocated memory. */
9644 for (i = 0; i < num_vr_values; i++)
9645 if (vr_value[i])
9647 BITMAP_FREE (vr_value[i]->equiv);
9648 free (vr_value[i]);
9651 free (vr_value);
9652 free (vr_phi_edge_counts);
9654 /* So that we can distinguish between VRP data being available
9655 and not available. */
9656 vr_value = NULL;
9657 vr_phi_edge_counts = NULL;
9661 /* Main entry point to VRP (Value Range Propagation). This pass is
9662 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9663 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9664 Programming Language Design and Implementation, pp. 67-78, 1995.
9665 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9667 This is essentially an SSA-CCP pass modified to deal with ranges
9668 instead of constants.
9670 While propagating ranges, we may find that two or more SSA name
9671 have equivalent, though distinct ranges. For instance,
9673 1 x_9 = p_3->a;
9674 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9675 3 if (p_4 == q_2)
9676 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9677 5 endif
9678 6 if (q_2)
9680 In the code above, pointer p_5 has range [q_2, q_2], but from the
9681 code we can also determine that p_5 cannot be NULL and, if q_2 had
9682 a non-varying range, p_5's range should also be compatible with it.
9684 These equivalences are created by two expressions: ASSERT_EXPR and
9685 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9686 result of another assertion, then we can use the fact that p_5 and
9687 p_4 are equivalent when evaluating p_5's range.
9689 Together with value ranges, we also propagate these equivalences
9690 between names so that we can take advantage of information from
9691 multiple ranges when doing final replacement. Note that this
9692 equivalency relation is transitive but not symmetric.
9694 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9695 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9696 in contexts where that assertion does not hold (e.g., in line 6).
9698 TODO, the main difference between this pass and Patterson's is that
9699 we do not propagate edge probabilities. We only compute whether
9700 edges can be taken or not. That is, instead of having a spectrum
9701 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9702 DON'T KNOW. In the future, it may be worthwhile to propagate
9703 probabilities to aid branch prediction. */
9705 static unsigned int
9706 execute_vrp (void)
9708 int i;
9709 edge e;
9710 switch_update *su;
9712 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9713 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9714 scev_initialize ();
9716 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9717 Inserting assertions may split edges which will invalidate
9718 EDGE_DFS_BACK. */
9719 insert_range_assertions ();
9721 to_remove_edges.create (10);
9722 to_update_switch_stmts.create (5);
9723 threadedge_initialize_values ();
9725 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9726 mark_dfs_back_edges ();
9728 vrp_initialize ();
9729 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
9730 vrp_finalize ();
9732 free_numbers_of_iterations_estimates ();
9734 /* ASSERT_EXPRs must be removed before finalizing jump threads
9735 as finalizing jump threads calls the CFG cleanup code which
9736 does not properly handle ASSERT_EXPRs. */
9737 remove_range_assertions ();
9739 /* If we exposed any new variables, go ahead and put them into
9740 SSA form now, before we handle jump threading. This simplifies
9741 interactions between rewriting of _DECL nodes into SSA form
9742 and rewriting SSA_NAME nodes into SSA form after block
9743 duplication and CFG manipulation. */
9744 update_ssa (TODO_update_ssa);
9746 finalize_jump_threads ();
9748 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9749 CFG in a broken state and requires a cfg_cleanup run. */
9750 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9751 remove_edge (e);
9752 /* Update SWITCH_EXPR case label vector. */
9753 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
9755 size_t j;
9756 size_t n = TREE_VEC_LENGTH (su->vec);
9757 tree label;
9758 gimple_switch_set_num_labels (su->stmt, n);
9759 for (j = 0; j < n; j++)
9760 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
9761 /* As we may have replaced the default label with a regular one
9762 make sure to make it a real default label again. This ensures
9763 optimal expansion. */
9764 label = gimple_switch_label (su->stmt, 0);
9765 CASE_LOW (label) = NULL_TREE;
9766 CASE_HIGH (label) = NULL_TREE;
9769 if (to_remove_edges.length () > 0)
9771 free_dominance_info (CDI_DOMINATORS);
9772 if (current_loops)
9773 loops_state_set (LOOPS_NEED_FIXUP);
9776 to_remove_edges.release ();
9777 to_update_switch_stmts.release ();
9778 threadedge_finalize_values ();
9780 scev_finalize ();
9781 loop_optimizer_finalize ();
9782 return 0;
9785 namespace {
9787 const pass_data pass_data_vrp =
9789 GIMPLE_PASS, /* type */
9790 "vrp", /* name */
9791 OPTGROUP_NONE, /* optinfo_flags */
9792 true, /* has_execute */
9793 TV_TREE_VRP, /* tv_id */
9794 PROP_ssa, /* properties_required */
9795 0, /* properties_provided */
9796 0, /* properties_destroyed */
9797 0, /* todo_flags_start */
9798 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
9801 class pass_vrp : public gimple_opt_pass
9803 public:
9804 pass_vrp (gcc::context *ctxt)
9805 : gimple_opt_pass (pass_data_vrp, ctxt)
9808 /* opt_pass methods: */
9809 opt_pass * clone () { return new pass_vrp (m_ctxt); }
9810 virtual bool gate (function *) { return flag_tree_vrp != 0; }
9811 virtual unsigned int execute (function *) { return execute_vrp (); }
9813 }; // class pass_vrp
9815 } // anon namespace
9817 gimple_opt_pass *
9818 make_pass_vrp (gcc::context *ctxt)
9820 return new pass_vrp (ctxt);