* scanner.c (gfc_open_intrinsic_module): Remove function.
[official-gcc.git] / gcc / tree-vrp.c
blobc47b8e6e313ba5093037f3d01f591723f3775eb9
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2013 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"
66 /* Range of values that can be associated with an SSA_NAME after VRP
67 has executed. */
68 struct value_range_d
70 /* Lattice value represented by this range. */
71 enum value_range_type type;
73 /* Minimum and maximum values represented by this range. These
74 values should be interpreted as follows:
76 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
77 be NULL.
79 - If TYPE == VR_RANGE then MIN holds the minimum value and
80 MAX holds the maximum value of the range [MIN, MAX].
82 - If TYPE == ANTI_RANGE the variable is known to NOT
83 take any values in the range [MIN, MAX]. */
84 tree min;
85 tree max;
87 /* Set of SSA names whose value ranges are equivalent to this one.
88 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
89 bitmap equiv;
92 typedef struct value_range_d value_range_t;
94 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
96 /* Set of SSA names found live during the RPO traversal of the function
97 for still active basic-blocks. */
98 static sbitmap *live;
100 /* Return true if the SSA name NAME is live on the edge E. */
102 static bool
103 live_on_edge (edge e, tree name)
105 return (live[e->dest->index]
106 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
109 /* Local functions. */
110 static int compare_values (tree val1, tree val2);
111 static int compare_values_warnv (tree val1, tree val2, bool *);
112 static void vrp_meet (value_range_t *, value_range_t *);
113 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
114 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
115 tree, tree, bool, bool *,
116 bool *);
118 /* Location information for ASSERT_EXPRs. Each instance of this
119 structure describes an ASSERT_EXPR for an SSA name. Since a single
120 SSA name may have more than one assertion associated with it, these
121 locations are kept in a linked list attached to the corresponding
122 SSA name. */
123 struct assert_locus_d
125 /* Basic block where the assertion would be inserted. */
126 basic_block bb;
128 /* Some assertions need to be inserted on an edge (e.g., assertions
129 generated by COND_EXPRs). In those cases, BB will be NULL. */
130 edge e;
132 /* Pointer to the statement that generated this assertion. */
133 gimple_stmt_iterator si;
135 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
136 enum tree_code comp_code;
138 /* Value being compared against. */
139 tree val;
141 /* Expression to compare. */
142 tree expr;
144 /* Next node in the linked list. */
145 struct assert_locus_d *next;
148 typedef struct assert_locus_d *assert_locus_t;
150 /* If bit I is present, it means that SSA name N_i has a list of
151 assertions that should be inserted in the IL. */
152 static bitmap need_assert_for;
154 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
155 holds a list of ASSERT_LOCUS_T nodes that describe where
156 ASSERT_EXPRs for SSA name N_I should be inserted. */
157 static assert_locus_t *asserts_for;
159 /* Value range array. After propagation, VR_VALUE[I] holds the range
160 of values that SSA name N_I may take. */
161 static unsigned num_vr_values;
162 static value_range_t **vr_value;
163 static bool values_propagated;
165 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
166 number of executable edges we saw the last time we visited the
167 node. */
168 static int *vr_phi_edge_counts;
170 typedef struct {
171 gimple stmt;
172 tree vec;
173 } switch_update;
175 static vec<edge> to_remove_edges;
176 static vec<switch_update> to_update_switch_stmts;
179 /* Return the maximum value for TYPE. */
181 static inline tree
182 vrp_val_max (const_tree type)
184 if (!INTEGRAL_TYPE_P (type))
185 return NULL_TREE;
187 return TYPE_MAX_VALUE (type);
190 /* Return the minimum value for TYPE. */
192 static inline tree
193 vrp_val_min (const_tree type)
195 if (!INTEGRAL_TYPE_P (type))
196 return NULL_TREE;
198 return TYPE_MIN_VALUE (type);
201 /* Return whether VAL is equal to the maximum value of its type. This
202 will be true for a positive overflow infinity. We can't do a
203 simple equality comparison with TYPE_MAX_VALUE because C typedefs
204 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
205 to the integer constant with the same value in the type. */
207 static inline bool
208 vrp_val_is_max (const_tree val)
210 tree type_max = vrp_val_max (TREE_TYPE (val));
211 return (val == type_max
212 || (type_max != NULL_TREE
213 && operand_equal_p (val, type_max, 0)));
216 /* Return whether VAL is equal to the minimum value of its type. This
217 will be true for a negative overflow infinity. */
219 static inline bool
220 vrp_val_is_min (const_tree val)
222 tree type_min = vrp_val_min (TREE_TYPE (val));
223 return (val == type_min
224 || (type_min != NULL_TREE
225 && operand_equal_p (val, type_min, 0)));
229 /* Return whether TYPE should use an overflow infinity distinct from
230 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
231 represent a signed overflow during VRP computations. An infinity
232 is distinct from a half-range, which will go from some number to
233 TYPE_{MIN,MAX}_VALUE. */
235 static inline bool
236 needs_overflow_infinity (const_tree type)
238 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
241 /* Return whether TYPE can support our overflow infinity
242 representation: we use the TREE_OVERFLOW flag, which only exists
243 for constants. If TYPE doesn't support this, we don't optimize
244 cases which would require signed overflow--we drop them to
245 VARYING. */
247 static inline bool
248 supports_overflow_infinity (const_tree type)
250 tree min = vrp_val_min (type), max = vrp_val_max (type);
251 #ifdef ENABLE_CHECKING
252 gcc_assert (needs_overflow_infinity (type));
253 #endif
254 return (min != NULL_TREE
255 && CONSTANT_CLASS_P (min)
256 && max != NULL_TREE
257 && CONSTANT_CLASS_P (max));
260 /* VAL is the maximum or minimum value of a type. Return a
261 corresponding overflow infinity. */
263 static inline tree
264 make_overflow_infinity (tree val)
266 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
267 val = copy_node (val);
268 TREE_OVERFLOW (val) = 1;
269 return val;
272 /* Return a negative overflow infinity for TYPE. */
274 static inline tree
275 negative_overflow_infinity (tree type)
277 gcc_checking_assert (supports_overflow_infinity (type));
278 return make_overflow_infinity (vrp_val_min (type));
281 /* Return a positive overflow infinity for TYPE. */
283 static inline tree
284 positive_overflow_infinity (tree type)
286 gcc_checking_assert (supports_overflow_infinity (type));
287 return make_overflow_infinity (vrp_val_max (type));
290 /* Return whether VAL is a negative overflow infinity. */
292 static inline bool
293 is_negative_overflow_infinity (const_tree val)
295 return (needs_overflow_infinity (TREE_TYPE (val))
296 && CONSTANT_CLASS_P (val)
297 && TREE_OVERFLOW (val)
298 && vrp_val_is_min (val));
301 /* Return whether VAL is a positive overflow infinity. */
303 static inline bool
304 is_positive_overflow_infinity (const_tree val)
306 return (needs_overflow_infinity (TREE_TYPE (val))
307 && CONSTANT_CLASS_P (val)
308 && TREE_OVERFLOW (val)
309 && vrp_val_is_max (val));
312 /* Return whether VAL is a positive or negative overflow infinity. */
314 static inline bool
315 is_overflow_infinity (const_tree val)
317 return (needs_overflow_infinity (TREE_TYPE (val))
318 && CONSTANT_CLASS_P (val)
319 && TREE_OVERFLOW (val)
320 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
323 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
325 static inline bool
326 stmt_overflow_infinity (gimple stmt)
328 if (is_gimple_assign (stmt)
329 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
330 GIMPLE_SINGLE_RHS)
331 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
332 return false;
335 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
336 the same value with TREE_OVERFLOW clear. This can be used to avoid
337 confusing a regular value with an overflow value. */
339 static inline tree
340 avoid_overflow_infinity (tree val)
342 if (!is_overflow_infinity (val))
343 return val;
345 if (vrp_val_is_max (val))
346 return vrp_val_max (TREE_TYPE (val));
347 else
349 gcc_checking_assert (vrp_val_is_min (val));
350 return vrp_val_min (TREE_TYPE (val));
355 /* Return true if ARG is marked with the nonnull attribute in the
356 current function signature. */
358 static bool
359 nonnull_arg_p (const_tree arg)
361 tree t, attrs, fntype;
362 unsigned HOST_WIDE_INT arg_num;
364 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
366 /* The static chain decl is always non null. */
367 if (arg == cfun->static_chain_decl)
368 return true;
370 fntype = TREE_TYPE (current_function_decl);
371 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
373 attrs = lookup_attribute ("nonnull", attrs);
375 /* If "nonnull" wasn't specified, we know nothing about the argument. */
376 if (attrs == NULL_TREE)
377 return false;
379 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
380 if (TREE_VALUE (attrs) == NULL_TREE)
381 return true;
383 /* Get the position number for ARG in the function signature. */
384 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
386 t = DECL_CHAIN (t), arg_num++)
388 if (t == arg)
389 break;
392 gcc_assert (t == arg);
394 /* Now see if ARG_NUM is mentioned in the nonnull list. */
395 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
397 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
398 return true;
402 return false;
406 /* Set value range VR to VR_UNDEFINED. */
408 static inline void
409 set_value_range_to_undefined (value_range_t *vr)
411 vr->type = VR_UNDEFINED;
412 vr->min = vr->max = NULL_TREE;
413 if (vr->equiv)
414 bitmap_clear (vr->equiv);
418 /* Set value range VR to VR_VARYING. */
420 static inline void
421 set_value_range_to_varying (value_range_t *vr)
423 vr->type = VR_VARYING;
424 vr->min = vr->max = NULL_TREE;
425 if (vr->equiv)
426 bitmap_clear (vr->equiv);
430 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
432 static void
433 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
434 tree max, bitmap equiv)
436 #if defined ENABLE_CHECKING
437 /* Check the validity of the range. */
438 if (t == VR_RANGE || t == VR_ANTI_RANGE)
440 int cmp;
442 gcc_assert (min && max);
444 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
445 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
447 cmp = compare_values (min, max);
448 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
450 if (needs_overflow_infinity (TREE_TYPE (min)))
451 gcc_assert (!is_overflow_infinity (min)
452 || !is_overflow_infinity (max));
455 if (t == VR_UNDEFINED || t == VR_VARYING)
456 gcc_assert (min == NULL_TREE && max == NULL_TREE);
458 if (t == VR_UNDEFINED || t == VR_VARYING)
459 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
460 #endif
462 vr->type = t;
463 vr->min = min;
464 vr->max = max;
466 /* Since updating the equivalence set involves deep copying the
467 bitmaps, only do it if absolutely necessary. */
468 if (vr->equiv == NULL
469 && equiv != NULL)
470 vr->equiv = BITMAP_ALLOC (NULL);
472 if (equiv != vr->equiv)
474 if (equiv && !bitmap_empty_p (equiv))
475 bitmap_copy (vr->equiv, equiv);
476 else
477 bitmap_clear (vr->equiv);
482 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
483 This means adjusting T, MIN and MAX representing the case of a
484 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
485 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
486 In corner cases where MAX+1 or MIN-1 wraps this will fall back
487 to varying.
488 This routine exists to ease canonicalization in the case where we
489 extract ranges from var + CST op limit. */
491 static void
492 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
493 tree min, tree max, bitmap equiv)
495 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
496 if (t == VR_UNDEFINED)
498 set_value_range_to_undefined (vr);
499 return;
501 else if (t == VR_VARYING)
503 set_value_range_to_varying (vr);
504 return;
507 /* Nothing to canonicalize for symbolic ranges. */
508 if (TREE_CODE (min) != INTEGER_CST
509 || TREE_CODE (max) != INTEGER_CST)
511 set_value_range (vr, t, min, max, equiv);
512 return;
515 /* Wrong order for min and max, to swap them and the VR type we need
516 to adjust them. */
517 if (tree_int_cst_lt (max, min))
519 tree one, tmp;
521 /* For one bit precision if max < min, then the swapped
522 range covers all values, so for VR_RANGE it is varying and
523 for VR_ANTI_RANGE empty range, so drop to varying as well. */
524 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
526 set_value_range_to_varying (vr);
527 return;
530 one = build_int_cst (TREE_TYPE (min), 1);
531 tmp = int_const_binop (PLUS_EXPR, max, one);
532 max = int_const_binop (MINUS_EXPR, min, one);
533 min = tmp;
535 /* There's one corner case, if we had [C+1, C] before we now have
536 that again. But this represents an empty value range, so drop
537 to varying in this case. */
538 if (tree_int_cst_lt (max, min))
540 set_value_range_to_varying (vr);
541 return;
544 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
547 /* Anti-ranges that can be represented as ranges should be so. */
548 if (t == VR_ANTI_RANGE)
550 bool is_min = vrp_val_is_min (min);
551 bool is_max = vrp_val_is_max (max);
553 if (is_min && is_max)
555 /* We cannot deal with empty ranges, drop to varying.
556 ??? This could be VR_UNDEFINED instead. */
557 set_value_range_to_varying (vr);
558 return;
560 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
561 && (is_min || is_max))
563 /* Non-empty boolean ranges can always be represented
564 as a singleton range. */
565 if (is_min)
566 min = max = vrp_val_max (TREE_TYPE (min));
567 else
568 min = max = vrp_val_min (TREE_TYPE (min));
569 t = VR_RANGE;
571 else if (is_min
572 /* As a special exception preserve non-null ranges. */
573 && !(TYPE_UNSIGNED (TREE_TYPE (min))
574 && integer_zerop (max)))
576 tree one = build_int_cst (TREE_TYPE (max), 1);
577 min = int_const_binop (PLUS_EXPR, max, one);
578 max = vrp_val_max (TREE_TYPE (max));
579 t = VR_RANGE;
581 else if (is_max)
583 tree one = build_int_cst (TREE_TYPE (min), 1);
584 max = int_const_binop (MINUS_EXPR, min, one);
585 min = vrp_val_min (TREE_TYPE (min));
586 t = VR_RANGE;
590 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
591 if (needs_overflow_infinity (TREE_TYPE (min))
592 && is_overflow_infinity (min)
593 && is_overflow_infinity (max))
595 set_value_range_to_varying (vr);
596 return;
599 set_value_range (vr, t, min, max, equiv);
602 /* Copy value range FROM into value range TO. */
604 static inline void
605 copy_value_range (value_range_t *to, value_range_t *from)
607 set_value_range (to, from->type, from->min, from->max, from->equiv);
610 /* Set value range VR to a single value. This function is only called
611 with values we get from statements, and exists to clear the
612 TREE_OVERFLOW flag so that we don't think we have an overflow
613 infinity when we shouldn't. */
615 static inline void
616 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
618 gcc_assert (is_gimple_min_invariant (val));
619 val = avoid_overflow_infinity (val);
620 set_value_range (vr, VR_RANGE, val, val, equiv);
623 /* Set value range VR to a non-negative range of type TYPE.
624 OVERFLOW_INFINITY indicates whether to use an overflow infinity
625 rather than TYPE_MAX_VALUE; this should be true if we determine
626 that the range is nonnegative based on the assumption that signed
627 overflow does not occur. */
629 static inline void
630 set_value_range_to_nonnegative (value_range_t *vr, tree type,
631 bool overflow_infinity)
633 tree zero;
635 if (overflow_infinity && !supports_overflow_infinity (type))
637 set_value_range_to_varying (vr);
638 return;
641 zero = build_int_cst (type, 0);
642 set_value_range (vr, VR_RANGE, zero,
643 (overflow_infinity
644 ? positive_overflow_infinity (type)
645 : TYPE_MAX_VALUE (type)),
646 vr->equiv);
649 /* Set value range VR to a non-NULL range of type TYPE. */
651 static inline void
652 set_value_range_to_nonnull (value_range_t *vr, tree type)
654 tree zero = build_int_cst (type, 0);
655 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
659 /* Set value range VR to a NULL range of type TYPE. */
661 static inline void
662 set_value_range_to_null (value_range_t *vr, tree type)
664 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
668 /* Set value range VR to a range of a truthvalue of type TYPE. */
670 static inline void
671 set_value_range_to_truthvalue (value_range_t *vr, tree type)
673 if (TYPE_PRECISION (type) == 1)
674 set_value_range_to_varying (vr);
675 else
676 set_value_range (vr, VR_RANGE,
677 build_int_cst (type, 0), build_int_cst (type, 1),
678 vr->equiv);
682 /* If abs (min) < abs (max), set VR to [-max, max], if
683 abs (min) >= abs (max), set VR to [-min, min]. */
685 static void
686 abs_extent_range (value_range_t *vr, tree min, tree max)
688 int cmp;
690 gcc_assert (TREE_CODE (min) == INTEGER_CST);
691 gcc_assert (TREE_CODE (max) == INTEGER_CST);
692 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
693 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
694 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
695 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
696 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
698 set_value_range_to_varying (vr);
699 return;
701 cmp = compare_values (min, max);
702 if (cmp == -1)
703 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
704 else if (cmp == 0 || cmp == 1)
706 max = min;
707 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
709 else
711 set_value_range_to_varying (vr);
712 return;
714 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
718 /* Return value range information for VAR.
720 If we have no values ranges recorded (ie, VRP is not running), then
721 return NULL. Otherwise create an empty range if none existed for VAR. */
723 static value_range_t *
724 get_value_range (const_tree var)
726 static const struct value_range_d vr_const_varying
727 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
728 value_range_t *vr;
729 tree sym;
730 unsigned ver = SSA_NAME_VERSION (var);
732 /* If we have no recorded ranges, then return NULL. */
733 if (! vr_value)
734 return NULL;
736 /* If we query the range for a new SSA name return an unmodifiable VARYING.
737 We should get here at most from the substitute-and-fold stage which
738 will never try to change values. */
739 if (ver >= num_vr_values)
740 return CONST_CAST (value_range_t *, &vr_const_varying);
742 vr = vr_value[ver];
743 if (vr)
744 return vr;
746 /* After propagation finished do not allocate new value-ranges. */
747 if (values_propagated)
748 return CONST_CAST (value_range_t *, &vr_const_varying);
750 /* Create a default value range. */
751 vr_value[ver] = vr = XCNEW (value_range_t);
753 /* Defer allocating the equivalence set. */
754 vr->equiv = NULL;
756 /* If VAR is a default definition of a parameter, the variable can
757 take any value in VAR's type. */
758 if (SSA_NAME_IS_DEFAULT_DEF (var))
760 sym = SSA_NAME_VAR (var);
761 if (TREE_CODE (sym) == PARM_DECL)
763 /* Try to use the "nonnull" attribute to create ~[0, 0]
764 anti-ranges for pointers. Note that this is only valid with
765 default definitions of PARM_DECLs. */
766 if (POINTER_TYPE_P (TREE_TYPE (sym))
767 && nonnull_arg_p (sym))
768 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
769 else
770 set_value_range_to_varying (vr);
772 else if (TREE_CODE (sym) == RESULT_DECL
773 && DECL_BY_REFERENCE (sym))
774 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
777 return vr;
780 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
782 static inline bool
783 vrp_operand_equal_p (const_tree val1, const_tree val2)
785 if (val1 == val2)
786 return true;
787 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
788 return false;
789 if (is_overflow_infinity (val1))
790 return is_overflow_infinity (val2);
791 return true;
794 /* Return true, if the bitmaps B1 and B2 are equal. */
796 static inline bool
797 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
799 return (b1 == b2
800 || ((!b1 || bitmap_empty_p (b1))
801 && (!b2 || bitmap_empty_p (b2)))
802 || (b1 && b2
803 && bitmap_equal_p (b1, b2)));
806 /* Update the value range and equivalence set for variable VAR to
807 NEW_VR. Return true if NEW_VR is different from VAR's previous
808 value.
810 NOTE: This function assumes that NEW_VR is a temporary value range
811 object created for the sole purpose of updating VAR's range. The
812 storage used by the equivalence set from NEW_VR will be freed by
813 this function. Do not call update_value_range when NEW_VR
814 is the range object associated with another SSA name. */
816 static inline bool
817 update_value_range (const_tree var, value_range_t *new_vr)
819 value_range_t *old_vr;
820 bool is_new;
822 /* Update the value range, if necessary. */
823 old_vr = get_value_range (var);
824 is_new = old_vr->type != new_vr->type
825 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
826 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
827 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
829 if (is_new)
831 /* Do not allow transitions up the lattice. The following
832 is slightly more awkward than just new_vr->type < old_vr->type
833 because VR_RANGE and VR_ANTI_RANGE need to be considered
834 the same. We may not have is_new when transitioning to
835 UNDEFINED or from VARYING. */
836 if (new_vr->type == VR_UNDEFINED
837 || old_vr->type == VR_VARYING)
838 set_value_range_to_varying (old_vr);
839 else
840 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
841 new_vr->equiv);
844 BITMAP_FREE (new_vr->equiv);
846 return is_new;
850 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
851 point where equivalence processing can be turned on/off. */
853 static void
854 add_equivalence (bitmap *equiv, const_tree var)
856 unsigned ver = SSA_NAME_VERSION (var);
857 value_range_t *vr = vr_value[ver];
859 if (*equiv == NULL)
860 *equiv = BITMAP_ALLOC (NULL);
861 bitmap_set_bit (*equiv, ver);
862 if (vr && vr->equiv)
863 bitmap_ior_into (*equiv, vr->equiv);
867 /* Return true if VR is ~[0, 0]. */
869 static inline bool
870 range_is_nonnull (value_range_t *vr)
872 return vr->type == VR_ANTI_RANGE
873 && integer_zerop (vr->min)
874 && integer_zerop (vr->max);
878 /* Return true if VR is [0, 0]. */
880 static inline bool
881 range_is_null (value_range_t *vr)
883 return vr->type == VR_RANGE
884 && integer_zerop (vr->min)
885 && integer_zerop (vr->max);
888 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
889 a singleton. */
891 static inline bool
892 range_int_cst_p (value_range_t *vr)
894 return (vr->type == VR_RANGE
895 && TREE_CODE (vr->max) == INTEGER_CST
896 && TREE_CODE (vr->min) == INTEGER_CST);
899 /* Return true if VR is a INTEGER_CST singleton. */
901 static inline bool
902 range_int_cst_singleton_p (value_range_t *vr)
904 return (range_int_cst_p (vr)
905 && !is_overflow_infinity (vr->min)
906 && !is_overflow_infinity (vr->max)
907 && tree_int_cst_equal (vr->min, vr->max));
910 /* Return true if value range VR involves at least one symbol. */
912 static inline bool
913 symbolic_range_p (value_range_t *vr)
915 return (!is_gimple_min_invariant (vr->min)
916 || !is_gimple_min_invariant (vr->max));
919 /* Return true if value range VR uses an overflow infinity. */
921 static inline bool
922 overflow_infinity_range_p (value_range_t *vr)
924 return (vr->type == VR_RANGE
925 && (is_overflow_infinity (vr->min)
926 || is_overflow_infinity (vr->max)));
929 /* Return false if we can not make a valid comparison based on VR;
930 this will be the case if it uses an overflow infinity and overflow
931 is not undefined (i.e., -fno-strict-overflow is in effect).
932 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
933 uses an overflow infinity. */
935 static bool
936 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
938 gcc_assert (vr->type == VR_RANGE);
939 if (is_overflow_infinity (vr->min))
941 *strict_overflow_p = true;
942 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
943 return false;
945 if (is_overflow_infinity (vr->max))
947 *strict_overflow_p = true;
948 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
949 return false;
951 return true;
955 /* Return true if the result of assignment STMT is know to be non-negative.
956 If the return value is based on the assumption that signed overflow is
957 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
958 *STRICT_OVERFLOW_P.*/
960 static bool
961 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
963 enum tree_code code = gimple_assign_rhs_code (stmt);
964 switch (get_gimple_rhs_class (code))
966 case GIMPLE_UNARY_RHS:
967 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
968 gimple_expr_type (stmt),
969 gimple_assign_rhs1 (stmt),
970 strict_overflow_p);
971 case GIMPLE_BINARY_RHS:
972 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
973 gimple_expr_type (stmt),
974 gimple_assign_rhs1 (stmt),
975 gimple_assign_rhs2 (stmt),
976 strict_overflow_p);
977 case GIMPLE_TERNARY_RHS:
978 return false;
979 case GIMPLE_SINGLE_RHS:
980 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
981 strict_overflow_p);
982 case GIMPLE_INVALID_RHS:
983 gcc_unreachable ();
984 default:
985 gcc_unreachable ();
989 /* Return true if return value of call STMT is know to be non-negative.
990 If the return value is based on the assumption that signed overflow is
991 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
992 *STRICT_OVERFLOW_P.*/
994 static bool
995 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
997 tree arg0 = gimple_call_num_args (stmt) > 0 ?
998 gimple_call_arg (stmt, 0) : NULL_TREE;
999 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1000 gimple_call_arg (stmt, 1) : NULL_TREE;
1002 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1003 gimple_call_fndecl (stmt),
1004 arg0,
1005 arg1,
1006 strict_overflow_p);
1009 /* Return true if STMT is know to to compute a non-negative value.
1010 If the return value is based on the assumption that signed overflow is
1011 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1012 *STRICT_OVERFLOW_P.*/
1014 static bool
1015 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1017 switch (gimple_code (stmt))
1019 case GIMPLE_ASSIGN:
1020 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1021 case GIMPLE_CALL:
1022 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1023 default:
1024 gcc_unreachable ();
1028 /* Return true if the result of assignment STMT is know to be non-zero.
1029 If the return value is based on the assumption that signed overflow is
1030 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1031 *STRICT_OVERFLOW_P.*/
1033 static bool
1034 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1036 enum tree_code code = gimple_assign_rhs_code (stmt);
1037 switch (get_gimple_rhs_class (code))
1039 case GIMPLE_UNARY_RHS:
1040 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1041 gimple_expr_type (stmt),
1042 gimple_assign_rhs1 (stmt),
1043 strict_overflow_p);
1044 case GIMPLE_BINARY_RHS:
1045 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1046 gimple_expr_type (stmt),
1047 gimple_assign_rhs1 (stmt),
1048 gimple_assign_rhs2 (stmt),
1049 strict_overflow_p);
1050 case GIMPLE_TERNARY_RHS:
1051 return false;
1052 case GIMPLE_SINGLE_RHS:
1053 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1054 strict_overflow_p);
1055 case GIMPLE_INVALID_RHS:
1056 gcc_unreachable ();
1057 default:
1058 gcc_unreachable ();
1062 /* Return true if STMT is known to compute a non-zero value.
1063 If the return value is based on the assumption that signed overflow is
1064 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1065 *STRICT_OVERFLOW_P.*/
1067 static bool
1068 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1070 switch (gimple_code (stmt))
1072 case GIMPLE_ASSIGN:
1073 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1074 case GIMPLE_CALL:
1076 tree fndecl = gimple_call_fndecl (stmt);
1077 if (!fndecl) return false;
1078 if (flag_delete_null_pointer_checks && !flag_check_new
1079 && DECL_IS_OPERATOR_NEW (fndecl)
1080 && !TREE_NOTHROW (fndecl))
1081 return true;
1082 if (flag_delete_null_pointer_checks &&
1083 lookup_attribute ("returns_nonnull",
1084 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1085 return true;
1086 return gimple_alloca_call_p (stmt);
1088 default:
1089 gcc_unreachable ();
1093 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1094 obtained so far. */
1096 static bool
1097 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1099 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1100 return true;
1102 /* If we have an expression of the form &X->a, then the expression
1103 is nonnull if X is nonnull. */
1104 if (is_gimple_assign (stmt)
1105 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1107 tree expr = gimple_assign_rhs1 (stmt);
1108 tree base = get_base_address (TREE_OPERAND (expr, 0));
1110 if (base != NULL_TREE
1111 && TREE_CODE (base) == MEM_REF
1112 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1114 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1115 if (range_is_nonnull (vr))
1116 return true;
1120 return false;
1123 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1124 a gimple invariant, or SSA_NAME +- CST. */
1126 static bool
1127 valid_value_p (tree expr)
1129 if (TREE_CODE (expr) == SSA_NAME)
1130 return true;
1132 if (TREE_CODE (expr) == PLUS_EXPR
1133 || TREE_CODE (expr) == MINUS_EXPR)
1134 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1135 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1137 return is_gimple_min_invariant (expr);
1140 /* Return
1141 1 if VAL < VAL2
1142 0 if !(VAL < VAL2)
1143 -2 if those are incomparable. */
1144 static inline int
1145 operand_less_p (tree val, tree val2)
1147 /* LT is folded faster than GE and others. Inline the common case. */
1148 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1150 if (TYPE_UNSIGNED (TREE_TYPE (val)))
1151 return INT_CST_LT_UNSIGNED (val, val2);
1152 else
1154 if (INT_CST_LT (val, val2))
1155 return 1;
1158 else
1160 tree tcmp;
1162 fold_defer_overflow_warnings ();
1164 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1166 fold_undefer_and_ignore_overflow_warnings ();
1168 if (!tcmp
1169 || TREE_CODE (tcmp) != INTEGER_CST)
1170 return -2;
1172 if (!integer_zerop (tcmp))
1173 return 1;
1176 /* val >= val2, not considering overflow infinity. */
1177 if (is_negative_overflow_infinity (val))
1178 return is_negative_overflow_infinity (val2) ? 0 : 1;
1179 else if (is_positive_overflow_infinity (val2))
1180 return is_positive_overflow_infinity (val) ? 0 : 1;
1182 return 0;
1185 /* Compare two values VAL1 and VAL2. Return
1187 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1188 -1 if VAL1 < VAL2,
1189 0 if VAL1 == VAL2,
1190 +1 if VAL1 > VAL2, and
1191 +2 if VAL1 != VAL2
1193 This is similar to tree_int_cst_compare but supports pointer values
1194 and values that cannot be compared at compile time.
1196 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1197 true if the return value is only valid if we assume that signed
1198 overflow is undefined. */
1200 static int
1201 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1203 if (val1 == val2)
1204 return 0;
1206 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1207 both integers. */
1208 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1209 == POINTER_TYPE_P (TREE_TYPE (val2)));
1210 /* Convert the two values into the same type. This is needed because
1211 sizetype causes sign extension even for unsigned types. */
1212 val2 = fold_convert (TREE_TYPE (val1), val2);
1213 STRIP_USELESS_TYPE_CONVERSION (val2);
1215 if ((TREE_CODE (val1) == SSA_NAME
1216 || TREE_CODE (val1) == PLUS_EXPR
1217 || TREE_CODE (val1) == MINUS_EXPR)
1218 && (TREE_CODE (val2) == SSA_NAME
1219 || TREE_CODE (val2) == PLUS_EXPR
1220 || TREE_CODE (val2) == MINUS_EXPR))
1222 tree n1, c1, n2, c2;
1223 enum tree_code code1, code2;
1225 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1226 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1227 same name, return -2. */
1228 if (TREE_CODE (val1) == SSA_NAME)
1230 code1 = SSA_NAME;
1231 n1 = val1;
1232 c1 = NULL_TREE;
1234 else
1236 code1 = TREE_CODE (val1);
1237 n1 = TREE_OPERAND (val1, 0);
1238 c1 = TREE_OPERAND (val1, 1);
1239 if (tree_int_cst_sgn (c1) == -1)
1241 if (is_negative_overflow_infinity (c1))
1242 return -2;
1243 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1244 if (!c1)
1245 return -2;
1246 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1250 if (TREE_CODE (val2) == SSA_NAME)
1252 code2 = SSA_NAME;
1253 n2 = val2;
1254 c2 = NULL_TREE;
1256 else
1258 code2 = TREE_CODE (val2);
1259 n2 = TREE_OPERAND (val2, 0);
1260 c2 = TREE_OPERAND (val2, 1);
1261 if (tree_int_cst_sgn (c2) == -1)
1263 if (is_negative_overflow_infinity (c2))
1264 return -2;
1265 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1266 if (!c2)
1267 return -2;
1268 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1272 /* Both values must use the same name. */
1273 if (n1 != n2)
1274 return -2;
1276 if (code1 == SSA_NAME
1277 && code2 == SSA_NAME)
1278 /* NAME == NAME */
1279 return 0;
1281 /* If overflow is defined we cannot simplify more. */
1282 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1283 return -2;
1285 if (strict_overflow_p != NULL
1286 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1287 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1288 *strict_overflow_p = true;
1290 if (code1 == SSA_NAME)
1292 if (code2 == PLUS_EXPR)
1293 /* NAME < NAME + CST */
1294 return -1;
1295 else if (code2 == MINUS_EXPR)
1296 /* NAME > NAME - CST */
1297 return 1;
1299 else if (code1 == PLUS_EXPR)
1301 if (code2 == SSA_NAME)
1302 /* NAME + CST > NAME */
1303 return 1;
1304 else if (code2 == PLUS_EXPR)
1305 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1306 return compare_values_warnv (c1, c2, strict_overflow_p);
1307 else if (code2 == MINUS_EXPR)
1308 /* NAME + CST1 > NAME - CST2 */
1309 return 1;
1311 else if (code1 == MINUS_EXPR)
1313 if (code2 == SSA_NAME)
1314 /* NAME - CST < NAME */
1315 return -1;
1316 else if (code2 == PLUS_EXPR)
1317 /* NAME - CST1 < NAME + CST2 */
1318 return -1;
1319 else if (code2 == MINUS_EXPR)
1320 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1321 C1 and C2 are swapped in the call to compare_values. */
1322 return compare_values_warnv (c2, c1, strict_overflow_p);
1325 gcc_unreachable ();
1328 /* We cannot compare non-constants. */
1329 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1330 return -2;
1332 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1334 /* We cannot compare overflowed values, except for overflow
1335 infinities. */
1336 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1338 if (strict_overflow_p != NULL)
1339 *strict_overflow_p = true;
1340 if (is_negative_overflow_infinity (val1))
1341 return is_negative_overflow_infinity (val2) ? 0 : -1;
1342 else if (is_negative_overflow_infinity (val2))
1343 return 1;
1344 else if (is_positive_overflow_infinity (val1))
1345 return is_positive_overflow_infinity (val2) ? 0 : 1;
1346 else if (is_positive_overflow_infinity (val2))
1347 return -1;
1348 return -2;
1351 return tree_int_cst_compare (val1, val2);
1353 else
1355 tree t;
1357 /* First see if VAL1 and VAL2 are not the same. */
1358 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1359 return 0;
1361 /* If VAL1 is a lower address than VAL2, return -1. */
1362 if (operand_less_p (val1, val2) == 1)
1363 return -1;
1365 /* If VAL1 is a higher address than VAL2, return +1. */
1366 if (operand_less_p (val2, val1) == 1)
1367 return 1;
1369 /* If VAL1 is different than VAL2, return +2.
1370 For integer constants we either have already returned -1 or 1
1371 or they are equivalent. We still might succeed in proving
1372 something about non-trivial operands. */
1373 if (TREE_CODE (val1) != INTEGER_CST
1374 || TREE_CODE (val2) != INTEGER_CST)
1376 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1377 if (t && integer_onep (t))
1378 return 2;
1381 return -2;
1385 /* Compare values like compare_values_warnv, but treat comparisons of
1386 nonconstants which rely on undefined overflow as incomparable. */
1388 static int
1389 compare_values (tree val1, tree val2)
1391 bool sop;
1392 int ret;
1394 sop = false;
1395 ret = compare_values_warnv (val1, val2, &sop);
1396 if (sop
1397 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1398 ret = -2;
1399 return ret;
1403 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1404 0 if VAL is not inside [MIN, MAX],
1405 -2 if we cannot tell either way.
1407 Benchmark compile/20001226-1.c compilation time after changing this
1408 function. */
1410 static inline int
1411 value_inside_range (tree val, tree min, tree max)
1413 int cmp1, cmp2;
1415 cmp1 = operand_less_p (val, min);
1416 if (cmp1 == -2)
1417 return -2;
1418 if (cmp1 == 1)
1419 return 0;
1421 cmp2 = operand_less_p (max, val);
1422 if (cmp2 == -2)
1423 return -2;
1425 return !cmp2;
1429 /* Return true if value ranges VR0 and VR1 have a non-empty
1430 intersection.
1432 Benchmark compile/20001226-1.c compilation time after changing this
1433 function.
1436 static inline bool
1437 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1439 /* The value ranges do not intersect if the maximum of the first range is
1440 less than the minimum of the second range or vice versa.
1441 When those relations are unknown, we can't do any better. */
1442 if (operand_less_p (vr0->max, vr1->min) != 0)
1443 return false;
1444 if (operand_less_p (vr1->max, vr0->min) != 0)
1445 return false;
1446 return true;
1450 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1451 include the value zero, -2 if we cannot tell. */
1453 static inline int
1454 range_includes_zero_p (tree min, tree max)
1456 tree zero = build_int_cst (TREE_TYPE (min), 0);
1457 return value_inside_range (zero, min, max);
1460 /* Return true if *VR is know to only contain nonnegative values. */
1462 static inline bool
1463 value_range_nonnegative_p (value_range_t *vr)
1465 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1466 which would return a useful value should be encoded as a
1467 VR_RANGE. */
1468 if (vr->type == VR_RANGE)
1470 int result = compare_values (vr->min, integer_zero_node);
1471 return (result == 0 || result == 1);
1474 return false;
1477 /* If *VR has a value rante that is a single constant value return that,
1478 otherwise return NULL_TREE. */
1480 static tree
1481 value_range_constant_singleton (value_range_t *vr)
1483 if (vr->type == VR_RANGE
1484 && operand_equal_p (vr->min, vr->max, 0)
1485 && is_gimple_min_invariant (vr->min))
1486 return vr->min;
1488 return NULL_TREE;
1491 /* If OP has a value range with a single constant value return that,
1492 otherwise return NULL_TREE. This returns OP itself if OP is a
1493 constant. */
1495 static tree
1496 op_with_constant_singleton_value_range (tree op)
1498 if (is_gimple_min_invariant (op))
1499 return op;
1501 if (TREE_CODE (op) != SSA_NAME)
1502 return NULL_TREE;
1504 return value_range_constant_singleton (get_value_range (op));
1507 /* Return true if op is in a boolean [0, 1] value-range. */
1509 static bool
1510 op_with_boolean_value_range_p (tree op)
1512 value_range_t *vr;
1514 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1515 return true;
1517 if (integer_zerop (op)
1518 || integer_onep (op))
1519 return true;
1521 if (TREE_CODE (op) != SSA_NAME)
1522 return false;
1524 vr = get_value_range (op);
1525 return (vr->type == VR_RANGE
1526 && integer_zerop (vr->min)
1527 && integer_onep (vr->max));
1530 /* Extract value range information from an ASSERT_EXPR EXPR and store
1531 it in *VR_P. */
1533 static void
1534 extract_range_from_assert (value_range_t *vr_p, tree expr)
1536 tree var, cond, limit, min, max, type;
1537 value_range_t *limit_vr;
1538 enum tree_code cond_code;
1540 var = ASSERT_EXPR_VAR (expr);
1541 cond = ASSERT_EXPR_COND (expr);
1543 gcc_assert (COMPARISON_CLASS_P (cond));
1545 /* Find VAR in the ASSERT_EXPR conditional. */
1546 if (var == TREE_OPERAND (cond, 0)
1547 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1548 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1550 /* If the predicate is of the form VAR COMP LIMIT, then we just
1551 take LIMIT from the RHS and use the same comparison code. */
1552 cond_code = TREE_CODE (cond);
1553 limit = TREE_OPERAND (cond, 1);
1554 cond = TREE_OPERAND (cond, 0);
1556 else
1558 /* If the predicate is of the form LIMIT COMP VAR, then we need
1559 to flip around the comparison code to create the proper range
1560 for VAR. */
1561 cond_code = swap_tree_comparison (TREE_CODE (cond));
1562 limit = TREE_OPERAND (cond, 0);
1563 cond = TREE_OPERAND (cond, 1);
1566 limit = avoid_overflow_infinity (limit);
1568 type = TREE_TYPE (var);
1569 gcc_assert (limit != var);
1571 /* For pointer arithmetic, we only keep track of pointer equality
1572 and inequality. */
1573 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1575 set_value_range_to_varying (vr_p);
1576 return;
1579 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1580 try to use LIMIT's range to avoid creating symbolic ranges
1581 unnecessarily. */
1582 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1584 /* LIMIT's range is only interesting if it has any useful information. */
1585 if (limit_vr
1586 && (limit_vr->type == VR_UNDEFINED
1587 || limit_vr->type == VR_VARYING
1588 || symbolic_range_p (limit_vr)))
1589 limit_vr = NULL;
1591 /* Initially, the new range has the same set of equivalences of
1592 VAR's range. This will be revised before returning the final
1593 value. Since assertions may be chained via mutually exclusive
1594 predicates, we will need to trim the set of equivalences before
1595 we are done. */
1596 gcc_assert (vr_p->equiv == NULL);
1597 add_equivalence (&vr_p->equiv, var);
1599 /* Extract a new range based on the asserted comparison for VAR and
1600 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1601 will only use it for equality comparisons (EQ_EXPR). For any
1602 other kind of assertion, we cannot derive a range from LIMIT's
1603 anti-range that can be used to describe the new range. For
1604 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1605 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1606 no single range for x_2 that could describe LE_EXPR, so we might
1607 as well build the range [b_4, +INF] for it.
1608 One special case we handle is extracting a range from a
1609 range test encoded as (unsigned)var + CST <= limit. */
1610 if (TREE_CODE (cond) == NOP_EXPR
1611 || TREE_CODE (cond) == PLUS_EXPR)
1613 if (TREE_CODE (cond) == PLUS_EXPR)
1615 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1616 TREE_OPERAND (cond, 1));
1617 max = int_const_binop (PLUS_EXPR, limit, min);
1618 cond = TREE_OPERAND (cond, 0);
1620 else
1622 min = build_int_cst (TREE_TYPE (var), 0);
1623 max = limit;
1626 /* Make sure to not set TREE_OVERFLOW on the final type
1627 conversion. We are willingly interpreting large positive
1628 unsigned values as negative singed values here. */
1629 min = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (min),
1630 0, false);
1631 max = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (max),
1632 0, false);
1634 /* We can transform a max, min range to an anti-range or
1635 vice-versa. Use set_and_canonicalize_value_range which does
1636 this for us. */
1637 if (cond_code == LE_EXPR)
1638 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1639 min, max, vr_p->equiv);
1640 else if (cond_code == GT_EXPR)
1641 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1642 min, max, vr_p->equiv);
1643 else
1644 gcc_unreachable ();
1646 else if (cond_code == EQ_EXPR)
1648 enum value_range_type range_type;
1650 if (limit_vr)
1652 range_type = limit_vr->type;
1653 min = limit_vr->min;
1654 max = limit_vr->max;
1656 else
1658 range_type = VR_RANGE;
1659 min = limit;
1660 max = limit;
1663 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1665 /* When asserting the equality VAR == LIMIT and LIMIT is another
1666 SSA name, the new range will also inherit the equivalence set
1667 from LIMIT. */
1668 if (TREE_CODE (limit) == SSA_NAME)
1669 add_equivalence (&vr_p->equiv, limit);
1671 else if (cond_code == NE_EXPR)
1673 /* As described above, when LIMIT's range is an anti-range and
1674 this assertion is an inequality (NE_EXPR), then we cannot
1675 derive anything from the anti-range. For instance, if
1676 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1677 not imply that VAR's range is [0, 0]. So, in the case of
1678 anti-ranges, we just assert the inequality using LIMIT and
1679 not its anti-range.
1681 If LIMIT_VR is a range, we can only use it to build a new
1682 anti-range if LIMIT_VR is a single-valued range. For
1683 instance, if LIMIT_VR is [0, 1], the predicate
1684 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1685 Rather, it means that for value 0 VAR should be ~[0, 0]
1686 and for value 1, VAR should be ~[1, 1]. We cannot
1687 represent these ranges.
1689 The only situation in which we can build a valid
1690 anti-range is when LIMIT_VR is a single-valued range
1691 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1692 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1693 if (limit_vr
1694 && limit_vr->type == VR_RANGE
1695 && compare_values (limit_vr->min, limit_vr->max) == 0)
1697 min = limit_vr->min;
1698 max = limit_vr->max;
1700 else
1702 /* In any other case, we cannot use LIMIT's range to build a
1703 valid anti-range. */
1704 min = max = limit;
1707 /* If MIN and MAX cover the whole range for their type, then
1708 just use the original LIMIT. */
1709 if (INTEGRAL_TYPE_P (type)
1710 && vrp_val_is_min (min)
1711 && vrp_val_is_max (max))
1712 min = max = limit;
1714 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1715 min, max, vr_p->equiv);
1717 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1719 min = TYPE_MIN_VALUE (type);
1721 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1722 max = limit;
1723 else
1725 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1726 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1727 LT_EXPR. */
1728 max = limit_vr->max;
1731 /* If the maximum value forces us to be out of bounds, simply punt.
1732 It would be pointless to try and do anything more since this
1733 all should be optimized away above us. */
1734 if ((cond_code == LT_EXPR
1735 && compare_values (max, min) == 0)
1736 || is_overflow_infinity (max))
1737 set_value_range_to_varying (vr_p);
1738 else
1740 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1741 if (cond_code == LT_EXPR)
1743 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1744 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1745 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1746 build_int_cst (TREE_TYPE (max), -1));
1747 else
1748 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1749 build_int_cst (TREE_TYPE (max), 1));
1750 if (EXPR_P (max))
1751 TREE_NO_WARNING (max) = 1;
1754 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1757 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1759 max = TYPE_MAX_VALUE (type);
1761 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1762 min = limit;
1763 else
1765 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1766 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1767 GT_EXPR. */
1768 min = limit_vr->min;
1771 /* If the minimum value forces us to be out of bounds, simply punt.
1772 It would be pointless to try and do anything more since this
1773 all should be optimized away above us. */
1774 if ((cond_code == GT_EXPR
1775 && compare_values (min, max) == 0)
1776 || is_overflow_infinity (min))
1777 set_value_range_to_varying (vr_p);
1778 else
1780 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1781 if (cond_code == GT_EXPR)
1783 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1784 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1785 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1786 build_int_cst (TREE_TYPE (min), -1));
1787 else
1788 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1789 build_int_cst (TREE_TYPE (min), 1));
1790 if (EXPR_P (min))
1791 TREE_NO_WARNING (min) = 1;
1794 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1797 else
1798 gcc_unreachable ();
1800 /* Finally intersect the new range with what we already know about var. */
1801 vrp_intersect_ranges (vr_p, get_value_range (var));
1805 /* Extract range information from SSA name VAR and store it in VR. If
1806 VAR has an interesting range, use it. Otherwise, create the
1807 range [VAR, VAR] and return it. This is useful in situations where
1808 we may have conditionals testing values of VARYING names. For
1809 instance,
1811 x_3 = y_5;
1812 if (x_3 > y_5)
1815 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1816 always false. */
1818 static void
1819 extract_range_from_ssa_name (value_range_t *vr, tree var)
1821 value_range_t *var_vr = get_value_range (var);
1823 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1824 copy_value_range (vr, var_vr);
1825 else
1826 set_value_range (vr, VR_RANGE, var, var, NULL);
1828 add_equivalence (&vr->equiv, var);
1832 /* Wrapper around int_const_binop. If the operation overflows and we
1833 are not using wrapping arithmetic, then adjust the result to be
1834 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1835 NULL_TREE if we need to use an overflow infinity representation but
1836 the type does not support it. */
1838 static tree
1839 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1841 tree res;
1843 res = int_const_binop (code, val1, val2);
1845 /* If we are using unsigned arithmetic, operate symbolically
1846 on -INF and +INF as int_const_binop only handles signed overflow. */
1847 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1849 int checkz = compare_values (res, val1);
1850 bool overflow = false;
1852 /* Ensure that res = val1 [+*] val2 >= val1
1853 or that res = val1 - val2 <= val1. */
1854 if ((code == PLUS_EXPR
1855 && !(checkz == 1 || checkz == 0))
1856 || (code == MINUS_EXPR
1857 && !(checkz == 0 || checkz == -1)))
1859 overflow = true;
1861 /* Checking for multiplication overflow is done by dividing the
1862 output of the multiplication by the first input of the
1863 multiplication. If the result of that division operation is
1864 not equal to the second input of the multiplication, then the
1865 multiplication overflowed. */
1866 else if (code == MULT_EXPR && !integer_zerop (val1))
1868 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1869 res,
1870 val1);
1871 int check = compare_values (tmp, val2);
1873 if (check != 0)
1874 overflow = true;
1877 if (overflow)
1879 res = copy_node (res);
1880 TREE_OVERFLOW (res) = 1;
1884 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1885 /* If the singed operation wraps then int_const_binop has done
1886 everything we want. */
1888 else if ((TREE_OVERFLOW (res)
1889 && !TREE_OVERFLOW (val1)
1890 && !TREE_OVERFLOW (val2))
1891 || is_overflow_infinity (val1)
1892 || is_overflow_infinity (val2))
1894 /* If the operation overflowed but neither VAL1 nor VAL2 are
1895 overflown, return -INF or +INF depending on the operation
1896 and the combination of signs of the operands. */
1897 int sgn1 = tree_int_cst_sgn (val1);
1898 int sgn2 = tree_int_cst_sgn (val2);
1900 if (needs_overflow_infinity (TREE_TYPE (res))
1901 && !supports_overflow_infinity (TREE_TYPE (res)))
1902 return NULL_TREE;
1904 /* We have to punt on adding infinities of different signs,
1905 since we can't tell what the sign of the result should be.
1906 Likewise for subtracting infinities of the same sign. */
1907 if (((code == PLUS_EXPR && sgn1 != sgn2)
1908 || (code == MINUS_EXPR && sgn1 == sgn2))
1909 && is_overflow_infinity (val1)
1910 && is_overflow_infinity (val2))
1911 return NULL_TREE;
1913 /* Don't try to handle division or shifting of infinities. */
1914 if ((code == TRUNC_DIV_EXPR
1915 || code == FLOOR_DIV_EXPR
1916 || code == CEIL_DIV_EXPR
1917 || code == EXACT_DIV_EXPR
1918 || code == ROUND_DIV_EXPR
1919 || code == RSHIFT_EXPR)
1920 && (is_overflow_infinity (val1)
1921 || is_overflow_infinity (val2)))
1922 return NULL_TREE;
1924 /* Notice that we only need to handle the restricted set of
1925 operations handled by extract_range_from_binary_expr.
1926 Among them, only multiplication, addition and subtraction
1927 can yield overflow without overflown operands because we
1928 are working with integral types only... except in the
1929 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1930 for division too. */
1932 /* For multiplication, the sign of the overflow is given
1933 by the comparison of the signs of the operands. */
1934 if ((code == MULT_EXPR && sgn1 == sgn2)
1935 /* For addition, the operands must be of the same sign
1936 to yield an overflow. Its sign is therefore that
1937 of one of the operands, for example the first. For
1938 infinite operands X + -INF is negative, not positive. */
1939 || (code == PLUS_EXPR
1940 && (sgn1 >= 0
1941 ? !is_negative_overflow_infinity (val2)
1942 : is_positive_overflow_infinity (val2)))
1943 /* For subtraction, non-infinite operands must be of
1944 different signs to yield an overflow. Its sign is
1945 therefore that of the first operand or the opposite of
1946 that of the second operand. A first operand of 0 counts
1947 as positive here, for the corner case 0 - (-INF), which
1948 overflows, but must yield +INF. For infinite operands 0
1949 - INF is negative, not positive. */
1950 || (code == MINUS_EXPR
1951 && (sgn1 >= 0
1952 ? !is_positive_overflow_infinity (val2)
1953 : is_negative_overflow_infinity (val2)))
1954 /* We only get in here with positive shift count, so the
1955 overflow direction is the same as the sign of val1.
1956 Actually rshift does not overflow at all, but we only
1957 handle the case of shifting overflowed -INF and +INF. */
1958 || (code == RSHIFT_EXPR
1959 && sgn1 >= 0)
1960 /* For division, the only case is -INF / -1 = +INF. */
1961 || code == TRUNC_DIV_EXPR
1962 || code == FLOOR_DIV_EXPR
1963 || code == CEIL_DIV_EXPR
1964 || code == EXACT_DIV_EXPR
1965 || code == ROUND_DIV_EXPR)
1966 return (needs_overflow_infinity (TREE_TYPE (res))
1967 ? positive_overflow_infinity (TREE_TYPE (res))
1968 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1969 else
1970 return (needs_overflow_infinity (TREE_TYPE (res))
1971 ? negative_overflow_infinity (TREE_TYPE (res))
1972 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1975 return res;
1979 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1980 bitmask if some bit is unset, it means for all numbers in the range
1981 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1982 bitmask if some bit is set, it means for all numbers in the range
1983 the bit is 1, otherwise it might be 0 or 1. */
1985 static bool
1986 zero_nonzero_bits_from_vr (value_range_t *vr,
1987 double_int *may_be_nonzero,
1988 double_int *must_be_nonzero)
1990 *may_be_nonzero = double_int_minus_one;
1991 *must_be_nonzero = double_int_zero;
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 = tree_to_double_int (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 double_int dmin = tree_to_double_int (vr->min);
2006 double_int dmax = tree_to_double_int (vr->max);
2007 double_int xor_mask = dmin ^ dmax;
2008 *may_be_nonzero = dmin | dmax;
2009 *must_be_nonzero = dmin & dmax;
2010 if (xor_mask.high != 0)
2012 unsigned HOST_WIDE_INT mask
2013 = ((unsigned HOST_WIDE_INT) 1
2014 << floor_log2 (xor_mask.high)) - 1;
2015 may_be_nonzero->low = ALL_ONES;
2016 may_be_nonzero->high |= mask;
2017 must_be_nonzero->low = 0;
2018 must_be_nonzero->high &= ~mask;
2020 else if (xor_mask.low != 0)
2022 unsigned HOST_WIDE_INT mask
2023 = ((unsigned HOST_WIDE_INT) 1
2024 << floor_log2 (xor_mask.low)) - 1;
2025 may_be_nonzero->low |= mask;
2026 must_be_nonzero->low &= ~mask;
2030 return true;
2033 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2034 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2035 false otherwise. If *AR can be represented with a single range
2036 *VR1 will be VR_UNDEFINED. */
2038 static bool
2039 ranges_from_anti_range (value_range_t *ar,
2040 value_range_t *vr0, value_range_t *vr1)
2042 tree type = TREE_TYPE (ar->min);
2044 vr0->type = VR_UNDEFINED;
2045 vr1->type = VR_UNDEFINED;
2047 if (ar->type != VR_ANTI_RANGE
2048 || TREE_CODE (ar->min) != INTEGER_CST
2049 || TREE_CODE (ar->max) != INTEGER_CST
2050 || !vrp_val_min (type)
2051 || !vrp_val_max (type))
2052 return false;
2054 if (!vrp_val_is_min (ar->min))
2056 vr0->type = VR_RANGE;
2057 vr0->min = vrp_val_min (type);
2058 vr0->max
2059 = double_int_to_tree (type,
2060 tree_to_double_int (ar->min) - double_int_one);
2062 if (!vrp_val_is_max (ar->max))
2064 vr1->type = VR_RANGE;
2065 vr1->min
2066 = double_int_to_tree (type,
2067 tree_to_double_int (ar->max) + double_int_one);
2068 vr1->max = vrp_val_max (type);
2070 if (vr0->type == VR_UNDEFINED)
2072 *vr0 = *vr1;
2073 vr1->type = VR_UNDEFINED;
2076 return vr0->type != VR_UNDEFINED;
2079 /* Helper to extract a value-range *VR for a multiplicative operation
2080 *VR0 CODE *VR1. */
2082 static void
2083 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2084 enum tree_code code,
2085 value_range_t *vr0, value_range_t *vr1)
2087 enum value_range_type type;
2088 tree val[4];
2089 size_t i;
2090 tree min, max;
2091 bool sop;
2092 int cmp;
2094 /* Multiplications, divisions and shifts are a bit tricky to handle,
2095 depending on the mix of signs we have in the two ranges, we
2096 need to operate on different values to get the minimum and
2097 maximum values for the new range. One approach is to figure
2098 out all the variations of range combinations and do the
2099 operations.
2101 However, this involves several calls to compare_values and it
2102 is pretty convoluted. It's simpler to do the 4 operations
2103 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2104 MAX1) and then figure the smallest and largest values to form
2105 the new range. */
2106 gcc_assert (code == MULT_EXPR
2107 || code == TRUNC_DIV_EXPR
2108 || code == FLOOR_DIV_EXPR
2109 || code == CEIL_DIV_EXPR
2110 || code == EXACT_DIV_EXPR
2111 || code == ROUND_DIV_EXPR
2112 || code == RSHIFT_EXPR
2113 || code == LSHIFT_EXPR);
2114 gcc_assert ((vr0->type == VR_RANGE
2115 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2116 && vr0->type == vr1->type);
2118 type = vr0->type;
2120 /* Compute the 4 cross operations. */
2121 sop = false;
2122 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2123 if (val[0] == NULL_TREE)
2124 sop = true;
2126 if (vr1->max == vr1->min)
2127 val[1] = NULL_TREE;
2128 else
2130 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2131 if (val[1] == NULL_TREE)
2132 sop = true;
2135 if (vr0->max == vr0->min)
2136 val[2] = NULL_TREE;
2137 else
2139 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2140 if (val[2] == NULL_TREE)
2141 sop = true;
2144 if (vr0->min == vr0->max || vr1->min == vr1->max)
2145 val[3] = NULL_TREE;
2146 else
2148 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2149 if (val[3] == NULL_TREE)
2150 sop = true;
2153 if (sop)
2155 set_value_range_to_varying (vr);
2156 return;
2159 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2160 of VAL[i]. */
2161 min = val[0];
2162 max = val[0];
2163 for (i = 1; i < 4; i++)
2165 if (!is_gimple_min_invariant (min)
2166 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2167 || !is_gimple_min_invariant (max)
2168 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2169 break;
2171 if (val[i])
2173 if (!is_gimple_min_invariant (val[i])
2174 || (TREE_OVERFLOW (val[i])
2175 && !is_overflow_infinity (val[i])))
2177 /* If we found an overflowed value, set MIN and MAX
2178 to it so that we set the resulting range to
2179 VARYING. */
2180 min = max = val[i];
2181 break;
2184 if (compare_values (val[i], min) == -1)
2185 min = val[i];
2187 if (compare_values (val[i], max) == 1)
2188 max = val[i];
2192 /* If either MIN or MAX overflowed, then set the resulting range to
2193 VARYING. But we do accept an overflow infinity
2194 representation. */
2195 if (min == NULL_TREE
2196 || !is_gimple_min_invariant (min)
2197 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2198 || max == NULL_TREE
2199 || !is_gimple_min_invariant (max)
2200 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2202 set_value_range_to_varying (vr);
2203 return;
2206 /* We punt if:
2207 1) [-INF, +INF]
2208 2) [-INF, +-INF(OVF)]
2209 3) [+-INF(OVF), +INF]
2210 4) [+-INF(OVF), +-INF(OVF)]
2211 We learn nothing when we have INF and INF(OVF) on both sides.
2212 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2213 overflow. */
2214 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2215 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2217 set_value_range_to_varying (vr);
2218 return;
2221 cmp = compare_values (min, max);
2222 if (cmp == -2 || cmp == 1)
2224 /* If the new range has its limits swapped around (MIN > MAX),
2225 then the operation caused one of them to wrap around, mark
2226 the new range VARYING. */
2227 set_value_range_to_varying (vr);
2229 else
2230 set_value_range (vr, type, min, max, NULL);
2233 /* Some quadruple precision helpers. */
2234 static int
2235 quad_int_cmp (double_int l0, double_int h0,
2236 double_int l1, double_int h1, bool uns)
2238 int c = h0.cmp (h1, uns);
2239 if (c != 0) return c;
2240 return l0.ucmp (l1);
2243 static void
2244 quad_int_pair_sort (double_int *l0, double_int *h0,
2245 double_int *l1, double_int *h1, bool uns)
2247 if (quad_int_cmp (*l0, *h0, *l1, *h1, uns) > 0)
2249 double_int tmp;
2250 tmp = *l0; *l0 = *l1; *l1 = tmp;
2251 tmp = *h0; *h0 = *h1; *h1 = tmp;
2255 /* Extract range information from a binary operation CODE based on
2256 the ranges of each of its operands, *VR0 and *VR1 with resulting
2257 type EXPR_TYPE. The resulting range is stored in *VR. */
2259 static void
2260 extract_range_from_binary_expr_1 (value_range_t *vr,
2261 enum tree_code code, tree expr_type,
2262 value_range_t *vr0_, value_range_t *vr1_)
2264 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2265 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2266 enum value_range_type type;
2267 tree min = NULL_TREE, max = NULL_TREE;
2268 int cmp;
2270 if (!INTEGRAL_TYPE_P (expr_type)
2271 && !POINTER_TYPE_P (expr_type))
2273 set_value_range_to_varying (vr);
2274 return;
2277 /* Not all binary expressions can be applied to ranges in a
2278 meaningful way. Handle only arithmetic operations. */
2279 if (code != PLUS_EXPR
2280 && code != MINUS_EXPR
2281 && code != POINTER_PLUS_EXPR
2282 && code != MULT_EXPR
2283 && code != TRUNC_DIV_EXPR
2284 && code != FLOOR_DIV_EXPR
2285 && code != CEIL_DIV_EXPR
2286 && code != EXACT_DIV_EXPR
2287 && code != ROUND_DIV_EXPR
2288 && code != TRUNC_MOD_EXPR
2289 && code != RSHIFT_EXPR
2290 && code != LSHIFT_EXPR
2291 && code != MIN_EXPR
2292 && code != MAX_EXPR
2293 && code != BIT_AND_EXPR
2294 && code != BIT_IOR_EXPR
2295 && code != BIT_XOR_EXPR)
2297 set_value_range_to_varying (vr);
2298 return;
2301 /* If both ranges are UNDEFINED, so is the result. */
2302 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2304 set_value_range_to_undefined (vr);
2305 return;
2307 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2308 code. At some point we may want to special-case operations that
2309 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2310 operand. */
2311 else if (vr0.type == VR_UNDEFINED)
2312 set_value_range_to_varying (&vr0);
2313 else if (vr1.type == VR_UNDEFINED)
2314 set_value_range_to_varying (&vr1);
2316 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2317 and express ~[] op X as ([]' op X) U ([]'' op X). */
2318 if (vr0.type == VR_ANTI_RANGE
2319 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2321 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2322 if (vrtem1.type != VR_UNDEFINED)
2324 value_range_t vrres = VR_INITIALIZER;
2325 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2326 &vrtem1, vr1_);
2327 vrp_meet (vr, &vrres);
2329 return;
2331 /* Likewise for X op ~[]. */
2332 if (vr1.type == VR_ANTI_RANGE
2333 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2335 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2336 if (vrtem1.type != VR_UNDEFINED)
2338 value_range_t vrres = VR_INITIALIZER;
2339 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2340 vr0_, &vrtem1);
2341 vrp_meet (vr, &vrres);
2343 return;
2346 /* The type of the resulting value range defaults to VR0.TYPE. */
2347 type = vr0.type;
2349 /* Refuse to operate on VARYING ranges, ranges of different kinds
2350 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2351 because we may be able to derive a useful range even if one of
2352 the operands is VR_VARYING or symbolic range. Similarly for
2353 divisions. TODO, we may be able to derive anti-ranges in
2354 some cases. */
2355 if (code != BIT_AND_EXPR
2356 && code != BIT_IOR_EXPR
2357 && code != TRUNC_DIV_EXPR
2358 && code != FLOOR_DIV_EXPR
2359 && code != CEIL_DIV_EXPR
2360 && code != EXACT_DIV_EXPR
2361 && code != ROUND_DIV_EXPR
2362 && code != TRUNC_MOD_EXPR
2363 && code != MIN_EXPR
2364 && code != MAX_EXPR
2365 && (vr0.type == VR_VARYING
2366 || vr1.type == VR_VARYING
2367 || vr0.type != vr1.type
2368 || symbolic_range_p (&vr0)
2369 || symbolic_range_p (&vr1)))
2371 set_value_range_to_varying (vr);
2372 return;
2375 /* Now evaluate the expression to determine the new range. */
2376 if (POINTER_TYPE_P (expr_type))
2378 if (code == MIN_EXPR || code == MAX_EXPR)
2380 /* For MIN/MAX expressions with pointers, we only care about
2381 nullness, if both are non null, then the result is nonnull.
2382 If both are null, then the result is null. Otherwise they
2383 are varying. */
2384 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2385 set_value_range_to_nonnull (vr, expr_type);
2386 else if (range_is_null (&vr0) && range_is_null (&vr1))
2387 set_value_range_to_null (vr, expr_type);
2388 else
2389 set_value_range_to_varying (vr);
2391 else if (code == POINTER_PLUS_EXPR)
2393 /* For pointer types, we are really only interested in asserting
2394 whether the expression evaluates to non-NULL. */
2395 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2396 set_value_range_to_nonnull (vr, expr_type);
2397 else if (range_is_null (&vr0) && range_is_null (&vr1))
2398 set_value_range_to_null (vr, expr_type);
2399 else
2400 set_value_range_to_varying (vr);
2402 else if (code == BIT_AND_EXPR)
2404 /* For pointer types, we are really only interested in asserting
2405 whether the expression evaluates to non-NULL. */
2406 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2407 set_value_range_to_nonnull (vr, expr_type);
2408 else if (range_is_null (&vr0) || range_is_null (&vr1))
2409 set_value_range_to_null (vr, expr_type);
2410 else
2411 set_value_range_to_varying (vr);
2413 else
2414 set_value_range_to_varying (vr);
2416 return;
2419 /* For integer ranges, apply the operation to each end of the
2420 range and see what we end up with. */
2421 if (code == PLUS_EXPR || code == MINUS_EXPR)
2423 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2424 ranges compute the precise range for such case if possible. */
2425 if (range_int_cst_p (&vr0)
2426 && range_int_cst_p (&vr1)
2427 /* We need as many bits as the possibly unsigned inputs. */
2428 && TYPE_PRECISION (expr_type) <= HOST_BITS_PER_DOUBLE_INT)
2430 double_int min0 = tree_to_double_int (vr0.min);
2431 double_int max0 = tree_to_double_int (vr0.max);
2432 double_int min1 = tree_to_double_int (vr1.min);
2433 double_int max1 = tree_to_double_int (vr1.max);
2434 bool uns = TYPE_UNSIGNED (expr_type);
2435 double_int type_min
2436 = double_int::min_value (TYPE_PRECISION (expr_type), uns);
2437 double_int type_max
2438 = double_int::max_value (TYPE_PRECISION (expr_type), uns);
2439 double_int dmin, dmax;
2440 int min_ovf = 0;
2441 int max_ovf = 0;
2443 if (code == PLUS_EXPR)
2445 dmin = min0 + min1;
2446 dmax = max0 + max1;
2448 /* Check for overflow in double_int. */
2449 if (min1.cmp (double_int_zero, uns) != dmin.cmp (min0, uns))
2450 min_ovf = min0.cmp (dmin, uns);
2451 if (max1.cmp (double_int_zero, uns) != dmax.cmp (max0, uns))
2452 max_ovf = max0.cmp (dmax, uns);
2454 else /* if (code == MINUS_EXPR) */
2456 dmin = min0 - max1;
2457 dmax = max0 - min1;
2459 if (double_int_zero.cmp (max1, uns) != dmin.cmp (min0, uns))
2460 min_ovf = min0.cmp (max1, uns);
2461 if (double_int_zero.cmp (min1, uns) != dmax.cmp (max0, uns))
2462 max_ovf = max0.cmp (min1, uns);
2465 /* For non-wrapping arithmetic look at possibly smaller
2466 value-ranges of the type. */
2467 if (!TYPE_OVERFLOW_WRAPS (expr_type))
2469 if (vrp_val_min (expr_type))
2470 type_min = tree_to_double_int (vrp_val_min (expr_type));
2471 if (vrp_val_max (expr_type))
2472 type_max = tree_to_double_int (vrp_val_max (expr_type));
2475 /* Check for type overflow. */
2476 if (min_ovf == 0)
2478 if (dmin.cmp (type_min, uns) == -1)
2479 min_ovf = -1;
2480 else if (dmin.cmp (type_max, uns) == 1)
2481 min_ovf = 1;
2483 if (max_ovf == 0)
2485 if (dmax.cmp (type_min, uns) == -1)
2486 max_ovf = -1;
2487 else if (dmax.cmp (type_max, uns) == 1)
2488 max_ovf = 1;
2491 if (TYPE_OVERFLOW_WRAPS (expr_type))
2493 /* If overflow wraps, truncate the values and adjust the
2494 range kind and bounds appropriately. */
2495 double_int tmin
2496 = dmin.ext (TYPE_PRECISION (expr_type), uns);
2497 double_int tmax
2498 = dmax.ext (TYPE_PRECISION (expr_type), uns);
2499 if (min_ovf == max_ovf)
2501 /* No overflow or both overflow or underflow. The
2502 range kind stays VR_RANGE. */
2503 min = double_int_to_tree (expr_type, tmin);
2504 max = double_int_to_tree (expr_type, tmax);
2506 else if (min_ovf == -1
2507 && max_ovf == 1)
2509 /* Underflow and overflow, drop to VR_VARYING. */
2510 set_value_range_to_varying (vr);
2511 return;
2513 else
2515 /* Min underflow or max overflow. The range kind
2516 changes to VR_ANTI_RANGE. */
2517 bool covers = false;
2518 double_int tem = tmin;
2519 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2520 || (max_ovf == 1 && min_ovf == 0));
2521 type = VR_ANTI_RANGE;
2522 tmin = tmax + double_int_one;
2523 if (tmin.cmp (tmax, uns) < 0)
2524 covers = true;
2525 tmax = tem + double_int_minus_one;
2526 if (tmax.cmp (tem, uns) > 0)
2527 covers = true;
2528 /* If the anti-range would cover nothing, drop to varying.
2529 Likewise if the anti-range bounds are outside of the
2530 types values. */
2531 if (covers || tmin.cmp (tmax, uns) > 0)
2533 set_value_range_to_varying (vr);
2534 return;
2536 min = double_int_to_tree (expr_type, tmin);
2537 max = double_int_to_tree (expr_type, tmax);
2540 else
2542 /* If overflow does not wrap, saturate to the types min/max
2543 value. */
2544 if (min_ovf == -1)
2546 if (needs_overflow_infinity (expr_type)
2547 && supports_overflow_infinity (expr_type))
2548 min = negative_overflow_infinity (expr_type);
2549 else
2550 min = double_int_to_tree (expr_type, type_min);
2552 else if (min_ovf == 1)
2554 if (needs_overflow_infinity (expr_type)
2555 && supports_overflow_infinity (expr_type))
2556 min = positive_overflow_infinity (expr_type);
2557 else
2558 min = double_int_to_tree (expr_type, type_max);
2560 else
2561 min = double_int_to_tree (expr_type, dmin);
2563 if (max_ovf == -1)
2565 if (needs_overflow_infinity (expr_type)
2566 && supports_overflow_infinity (expr_type))
2567 max = negative_overflow_infinity (expr_type);
2568 else
2569 max = double_int_to_tree (expr_type, type_min);
2571 else if (max_ovf == 1)
2573 if (needs_overflow_infinity (expr_type)
2574 && supports_overflow_infinity (expr_type))
2575 max = positive_overflow_infinity (expr_type);
2576 else
2577 max = double_int_to_tree (expr_type, type_max);
2579 else
2580 max = double_int_to_tree (expr_type, dmax);
2582 if (needs_overflow_infinity (expr_type)
2583 && supports_overflow_infinity (expr_type))
2585 if (is_negative_overflow_infinity (vr0.min)
2586 || (code == PLUS_EXPR
2587 ? is_negative_overflow_infinity (vr1.min)
2588 : is_positive_overflow_infinity (vr1.max)))
2589 min = negative_overflow_infinity (expr_type);
2590 if (is_positive_overflow_infinity (vr0.max)
2591 || (code == PLUS_EXPR
2592 ? is_positive_overflow_infinity (vr1.max)
2593 : is_negative_overflow_infinity (vr1.min)))
2594 max = positive_overflow_infinity (expr_type);
2597 else
2599 /* For other cases, for example if we have a PLUS_EXPR with two
2600 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2601 to compute a precise range for such a case.
2602 ??? General even mixed range kind operations can be expressed
2603 by for example transforming ~[3, 5] + [1, 2] to range-only
2604 operations and a union primitive:
2605 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2606 [-INF+1, 4] U [6, +INF(OVF)]
2607 though usually the union is not exactly representable with
2608 a single range or anti-range as the above is
2609 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2610 but one could use a scheme similar to equivalences for this. */
2611 set_value_range_to_varying (vr);
2612 return;
2615 else if (code == MIN_EXPR
2616 || code == MAX_EXPR)
2618 if (vr0.type == VR_RANGE
2619 && !symbolic_range_p (&vr0))
2621 type = VR_RANGE;
2622 if (vr1.type == VR_RANGE
2623 && !symbolic_range_p (&vr1))
2625 /* For operations that make the resulting range directly
2626 proportional to the original ranges, apply the operation to
2627 the same end of each range. */
2628 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2629 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2631 else if (code == MIN_EXPR)
2633 min = vrp_val_min (expr_type);
2634 max = vr0.max;
2636 else if (code == MAX_EXPR)
2638 min = vr0.min;
2639 max = vrp_val_max (expr_type);
2642 else if (vr1.type == VR_RANGE
2643 && !symbolic_range_p (&vr1))
2645 type = VR_RANGE;
2646 if (code == MIN_EXPR)
2648 min = vrp_val_min (expr_type);
2649 max = vr1.max;
2651 else if (code == MAX_EXPR)
2653 min = vr1.min;
2654 max = vrp_val_max (expr_type);
2657 else
2659 set_value_range_to_varying (vr);
2660 return;
2663 else if (code == MULT_EXPR)
2665 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2666 drop to varying. */
2667 if (range_int_cst_p (&vr0)
2668 && range_int_cst_p (&vr1)
2669 && TYPE_OVERFLOW_WRAPS (expr_type))
2671 double_int min0, max0, min1, max1, sizem1, size;
2672 double_int prod0l, prod0h, prod1l, prod1h,
2673 prod2l, prod2h, prod3l, prod3h;
2674 bool uns0, uns1, uns;
2676 sizem1 = double_int::max_value (TYPE_PRECISION (expr_type), true);
2677 size = sizem1 + double_int_one;
2679 min0 = tree_to_double_int (vr0.min);
2680 max0 = tree_to_double_int (vr0.max);
2681 min1 = tree_to_double_int (vr1.min);
2682 max1 = tree_to_double_int (vr1.max);
2684 uns0 = TYPE_UNSIGNED (expr_type);
2685 uns1 = uns0;
2687 /* Canonicalize the intervals. */
2688 if (TYPE_UNSIGNED (expr_type))
2690 double_int min2 = size - min0;
2691 if (!min2.is_zero () && min2.cmp (max0, true) < 0)
2693 min0 = -min2;
2694 max0 -= size;
2695 uns0 = false;
2698 min2 = size - min1;
2699 if (!min2.is_zero () && min2.cmp (max1, true) < 0)
2701 min1 = -min2;
2702 max1 -= size;
2703 uns1 = false;
2706 uns = uns0 & uns1;
2708 bool overflow;
2709 prod0l = min0.wide_mul_with_sign (min1, true, &prod0h, &overflow);
2710 if (!uns0 && min0.is_negative ())
2711 prod0h -= min1;
2712 if (!uns1 && min1.is_negative ())
2713 prod0h -= min0;
2715 prod1l = min0.wide_mul_with_sign (max1, true, &prod1h, &overflow);
2716 if (!uns0 && min0.is_negative ())
2717 prod1h -= max1;
2718 if (!uns1 && max1.is_negative ())
2719 prod1h -= min0;
2721 prod2l = max0.wide_mul_with_sign (min1, true, &prod2h, &overflow);
2722 if (!uns0 && max0.is_negative ())
2723 prod2h -= min1;
2724 if (!uns1 && min1.is_negative ())
2725 prod2h -= max0;
2727 prod3l = max0.wide_mul_with_sign (max1, true, &prod3h, &overflow);
2728 if (!uns0 && max0.is_negative ())
2729 prod3h -= max1;
2730 if (!uns1 && max1.is_negative ())
2731 prod3h -= max0;
2733 /* Sort the 4 products. */
2734 quad_int_pair_sort (&prod0l, &prod0h, &prod3l, &prod3h, uns);
2735 quad_int_pair_sort (&prod1l, &prod1h, &prod2l, &prod2h, uns);
2736 quad_int_pair_sort (&prod0l, &prod0h, &prod1l, &prod1h, uns);
2737 quad_int_pair_sort (&prod2l, &prod2h, &prod3l, &prod3h, uns);
2739 /* Max - min. */
2740 if (prod0l.is_zero ())
2742 prod1l = double_int_zero;
2743 prod1h = -prod0h;
2745 else
2747 prod1l = -prod0l;
2748 prod1h = ~prod0h;
2750 prod2l = prod3l + prod1l;
2751 prod2h = prod3h + prod1h;
2752 if (prod2l.ult (prod3l))
2753 prod2h += double_int_one; /* carry */
2755 if (!prod2h.is_zero ()
2756 || prod2l.cmp (sizem1, true) >= 0)
2758 /* the range covers all values. */
2759 set_value_range_to_varying (vr);
2760 return;
2763 /* The following should handle the wrapping and selecting
2764 VR_ANTI_RANGE for us. */
2765 min = double_int_to_tree (expr_type, prod0l);
2766 max = double_int_to_tree (expr_type, prod3l);
2767 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2768 return;
2771 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2772 drop to VR_VARYING. It would take more effort to compute a
2773 precise range for such a case. For example, if we have
2774 op0 == 65536 and op1 == 65536 with their ranges both being
2775 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2776 we cannot claim that the product is in ~[0,0]. Note that we
2777 are guaranteed to have vr0.type == vr1.type at this
2778 point. */
2779 if (vr0.type == VR_ANTI_RANGE
2780 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2782 set_value_range_to_varying (vr);
2783 return;
2786 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2787 return;
2789 else if (code == RSHIFT_EXPR
2790 || code == LSHIFT_EXPR)
2792 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2793 then drop to VR_VARYING. Outside of this range we get undefined
2794 behavior from the shift operation. We cannot even trust
2795 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2796 shifts, and the operation at the tree level may be widened. */
2797 if (range_int_cst_p (&vr1)
2798 && compare_tree_int (vr1.min, 0) >= 0
2799 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2801 if (code == RSHIFT_EXPR)
2803 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2804 return;
2806 /* We can map lshifts by constants to MULT_EXPR handling. */
2807 else if (code == LSHIFT_EXPR
2808 && range_int_cst_singleton_p (&vr1))
2810 bool saved_flag_wrapv;
2811 value_range_t vr1p = VR_INITIALIZER;
2812 vr1p.type = VR_RANGE;
2813 vr1p.min
2814 = double_int_to_tree (expr_type,
2815 double_int_one
2816 .llshift (TREE_INT_CST_LOW (vr1.min),
2817 TYPE_PRECISION (expr_type)));
2818 vr1p.max = vr1p.min;
2819 /* We have to use a wrapping multiply though as signed overflow
2820 on lshifts is implementation defined in C89. */
2821 saved_flag_wrapv = flag_wrapv;
2822 flag_wrapv = 1;
2823 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2824 &vr0, &vr1p);
2825 flag_wrapv = saved_flag_wrapv;
2826 return;
2828 else if (code == LSHIFT_EXPR
2829 && range_int_cst_p (&vr0))
2831 int prec = TYPE_PRECISION (expr_type);
2832 int overflow_pos = prec;
2833 int bound_shift;
2834 double_int bound, complement, low_bound, high_bound;
2835 bool uns = TYPE_UNSIGNED (expr_type);
2836 bool in_bounds = false;
2838 if (!uns)
2839 overflow_pos -= 1;
2841 bound_shift = overflow_pos - TREE_INT_CST_LOW (vr1.max);
2842 /* If bound_shift == HOST_BITS_PER_DOUBLE_INT, the llshift can
2843 overflow. However, for that to happen, vr1.max needs to be
2844 zero, which means vr1 is a singleton range of zero, which
2845 means it should be handled by the previous LSHIFT_EXPR
2846 if-clause. */
2847 bound = double_int_one.llshift (bound_shift, prec);
2848 complement = ~(bound - double_int_one);
2850 if (uns)
2852 low_bound = bound.zext (prec);
2853 high_bound = complement.zext (prec);
2854 if (tree_to_double_int (vr0.max).ult (low_bound))
2856 /* [5, 6] << [1, 2] == [10, 24]. */
2857 /* We're shifting out only zeroes, the value increases
2858 monotonically. */
2859 in_bounds = true;
2861 else if (high_bound.ult (tree_to_double_int (vr0.min)))
2863 /* [0xffffff00, 0xffffffff] << [1, 2]
2864 == [0xfffffc00, 0xfffffffe]. */
2865 /* We're shifting out only ones, the value decreases
2866 monotonically. */
2867 in_bounds = true;
2870 else
2872 /* [-1, 1] << [1, 2] == [-4, 4]. */
2873 low_bound = complement.sext (prec);
2874 high_bound = bound;
2875 if (tree_to_double_int (vr0.max).slt (high_bound)
2876 && low_bound.slt (tree_to_double_int (vr0.min)))
2878 /* For non-negative numbers, we're shifting out only
2879 zeroes, the value increases monotonically.
2880 For negative numbers, we're shifting out only ones, the
2881 value decreases monotomically. */
2882 in_bounds = true;
2886 if (in_bounds)
2888 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2889 return;
2893 set_value_range_to_varying (vr);
2894 return;
2896 else if (code == TRUNC_DIV_EXPR
2897 || code == FLOOR_DIV_EXPR
2898 || code == CEIL_DIV_EXPR
2899 || code == EXACT_DIV_EXPR
2900 || code == ROUND_DIV_EXPR)
2902 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2904 /* For division, if op1 has VR_RANGE but op0 does not, something
2905 can be deduced just from that range. Say [min, max] / [4, max]
2906 gives [min / 4, max / 4] range. */
2907 if (vr1.type == VR_RANGE
2908 && !symbolic_range_p (&vr1)
2909 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2911 vr0.type = type = VR_RANGE;
2912 vr0.min = vrp_val_min (expr_type);
2913 vr0.max = vrp_val_max (expr_type);
2915 else
2917 set_value_range_to_varying (vr);
2918 return;
2922 /* For divisions, if flag_non_call_exceptions is true, we must
2923 not eliminate a division by zero. */
2924 if (cfun->can_throw_non_call_exceptions
2925 && (vr1.type != VR_RANGE
2926 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2928 set_value_range_to_varying (vr);
2929 return;
2932 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2933 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2934 include 0. */
2935 if (vr0.type == VR_RANGE
2936 && (vr1.type != VR_RANGE
2937 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2939 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2940 int cmp;
2942 min = NULL_TREE;
2943 max = NULL_TREE;
2944 if (TYPE_UNSIGNED (expr_type)
2945 || value_range_nonnegative_p (&vr1))
2947 /* For unsigned division or when divisor is known
2948 to be non-negative, the range has to cover
2949 all numbers from 0 to max for positive max
2950 and all numbers from min to 0 for negative min. */
2951 cmp = compare_values (vr0.max, zero);
2952 if (cmp == -1)
2953 max = zero;
2954 else if (cmp == 0 || cmp == 1)
2955 max = vr0.max;
2956 else
2957 type = VR_VARYING;
2958 cmp = compare_values (vr0.min, zero);
2959 if (cmp == 1)
2960 min = zero;
2961 else if (cmp == 0 || cmp == -1)
2962 min = vr0.min;
2963 else
2964 type = VR_VARYING;
2966 else
2968 /* Otherwise the range is -max .. max or min .. -min
2969 depending on which bound is bigger in absolute value,
2970 as the division can change the sign. */
2971 abs_extent_range (vr, vr0.min, vr0.max);
2972 return;
2974 if (type == VR_VARYING)
2976 set_value_range_to_varying (vr);
2977 return;
2980 else
2982 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2983 return;
2986 else if (code == TRUNC_MOD_EXPR)
2988 if (vr1.type != VR_RANGE
2989 || range_includes_zero_p (vr1.min, vr1.max) != 0
2990 || vrp_val_is_min (vr1.min))
2992 set_value_range_to_varying (vr);
2993 return;
2995 type = VR_RANGE;
2996 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2997 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
2998 if (tree_int_cst_lt (max, vr1.max))
2999 max = vr1.max;
3000 max = int_const_binop (MINUS_EXPR, max, integer_one_node);
3001 /* If the dividend is non-negative the modulus will be
3002 non-negative as well. */
3003 if (TYPE_UNSIGNED (expr_type)
3004 || value_range_nonnegative_p (&vr0))
3005 min = build_int_cst (TREE_TYPE (max), 0);
3006 else
3007 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
3009 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3011 bool int_cst_range0, int_cst_range1;
3012 double_int may_be_nonzero0, may_be_nonzero1;
3013 double_int must_be_nonzero0, must_be_nonzero1;
3015 int_cst_range0 = zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0,
3016 &must_be_nonzero0);
3017 int_cst_range1 = zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1,
3018 &must_be_nonzero1);
3020 type = VR_RANGE;
3021 if (code == BIT_AND_EXPR)
3023 double_int dmax;
3024 min = double_int_to_tree (expr_type,
3025 must_be_nonzero0 & must_be_nonzero1);
3026 dmax = may_be_nonzero0 & may_be_nonzero1;
3027 /* If both input ranges contain only negative values we can
3028 truncate the result range maximum to the minimum of the
3029 input range maxima. */
3030 if (int_cst_range0 && int_cst_range1
3031 && tree_int_cst_sgn (vr0.max) < 0
3032 && tree_int_cst_sgn (vr1.max) < 0)
3034 dmax = dmax.min (tree_to_double_int (vr0.max),
3035 TYPE_UNSIGNED (expr_type));
3036 dmax = dmax.min (tree_to_double_int (vr1.max),
3037 TYPE_UNSIGNED (expr_type));
3039 /* If either input range contains only non-negative values
3040 we can truncate the result range maximum to the respective
3041 maximum of the input range. */
3042 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3043 dmax = dmax.min (tree_to_double_int (vr0.max),
3044 TYPE_UNSIGNED (expr_type));
3045 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3046 dmax = dmax.min (tree_to_double_int (vr1.max),
3047 TYPE_UNSIGNED (expr_type));
3048 max = double_int_to_tree (expr_type, dmax);
3050 else if (code == BIT_IOR_EXPR)
3052 double_int dmin;
3053 max = double_int_to_tree (expr_type,
3054 may_be_nonzero0 | may_be_nonzero1);
3055 dmin = must_be_nonzero0 | must_be_nonzero1;
3056 /* If the input ranges contain only positive values we can
3057 truncate the minimum of the result range to the maximum
3058 of the input range minima. */
3059 if (int_cst_range0 && int_cst_range1
3060 && tree_int_cst_sgn (vr0.min) >= 0
3061 && tree_int_cst_sgn (vr1.min) >= 0)
3063 dmin = dmin.max (tree_to_double_int (vr0.min),
3064 TYPE_UNSIGNED (expr_type));
3065 dmin = dmin.max (tree_to_double_int (vr1.min),
3066 TYPE_UNSIGNED (expr_type));
3068 /* If either input range contains only negative values
3069 we can truncate the minimum of the result range to the
3070 respective minimum range. */
3071 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3072 dmin = dmin.max (tree_to_double_int (vr0.min),
3073 TYPE_UNSIGNED (expr_type));
3074 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3075 dmin = dmin.max (tree_to_double_int (vr1.min),
3076 TYPE_UNSIGNED (expr_type));
3077 min = double_int_to_tree (expr_type, dmin);
3079 else if (code == BIT_XOR_EXPR)
3081 double_int result_zero_bits, result_one_bits;
3082 result_zero_bits = (must_be_nonzero0 & must_be_nonzero1)
3083 | ~(may_be_nonzero0 | may_be_nonzero1);
3084 result_one_bits = must_be_nonzero0.and_not (may_be_nonzero1)
3085 | must_be_nonzero1.and_not (may_be_nonzero0);
3086 max = double_int_to_tree (expr_type, ~result_zero_bits);
3087 min = double_int_to_tree (expr_type, result_one_bits);
3088 /* If the range has all positive or all negative values the
3089 result is better than VARYING. */
3090 if (tree_int_cst_sgn (min) < 0
3091 || tree_int_cst_sgn (max) >= 0)
3093 else
3094 max = min = NULL_TREE;
3097 else
3098 gcc_unreachable ();
3100 /* If either MIN or MAX overflowed, then set the resulting range to
3101 VARYING. But we do accept an overflow infinity
3102 representation. */
3103 if (min == NULL_TREE
3104 || !is_gimple_min_invariant (min)
3105 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
3106 || max == NULL_TREE
3107 || !is_gimple_min_invariant (max)
3108 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
3110 set_value_range_to_varying (vr);
3111 return;
3114 /* We punt if:
3115 1) [-INF, +INF]
3116 2) [-INF, +-INF(OVF)]
3117 3) [+-INF(OVF), +INF]
3118 4) [+-INF(OVF), +-INF(OVF)]
3119 We learn nothing when we have INF and INF(OVF) on both sides.
3120 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3121 overflow. */
3122 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3123 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3125 set_value_range_to_varying (vr);
3126 return;
3129 cmp = compare_values (min, max);
3130 if (cmp == -2 || cmp == 1)
3132 /* If the new range has its limits swapped around (MIN > MAX),
3133 then the operation caused one of them to wrap around, mark
3134 the new range VARYING. */
3135 set_value_range_to_varying (vr);
3137 else
3138 set_value_range (vr, type, min, max, NULL);
3141 /* Extract range information from a binary expression OP0 CODE OP1 based on
3142 the ranges of each of its operands with resulting type EXPR_TYPE.
3143 The resulting range is stored in *VR. */
3145 static void
3146 extract_range_from_binary_expr (value_range_t *vr,
3147 enum tree_code code,
3148 tree expr_type, tree op0, tree op1)
3150 value_range_t vr0 = VR_INITIALIZER;
3151 value_range_t vr1 = VR_INITIALIZER;
3153 /* Get value ranges for each operand. For constant operands, create
3154 a new value range with the operand to simplify processing. */
3155 if (TREE_CODE (op0) == SSA_NAME)
3156 vr0 = *(get_value_range (op0));
3157 else if (is_gimple_min_invariant (op0))
3158 set_value_range_to_value (&vr0, op0, NULL);
3159 else
3160 set_value_range_to_varying (&vr0);
3162 if (TREE_CODE (op1) == SSA_NAME)
3163 vr1 = *(get_value_range (op1));
3164 else if (is_gimple_min_invariant (op1))
3165 set_value_range_to_value (&vr1, op1, NULL);
3166 else
3167 set_value_range_to_varying (&vr1);
3169 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3172 /* Extract range information from a unary operation CODE based on
3173 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3174 The The resulting range is stored in *VR. */
3176 static void
3177 extract_range_from_unary_expr_1 (value_range_t *vr,
3178 enum tree_code code, tree type,
3179 value_range_t *vr0_, tree op0_type)
3181 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3183 /* VRP only operates on integral and pointer types. */
3184 if (!(INTEGRAL_TYPE_P (op0_type)
3185 || POINTER_TYPE_P (op0_type))
3186 || !(INTEGRAL_TYPE_P (type)
3187 || POINTER_TYPE_P (type)))
3189 set_value_range_to_varying (vr);
3190 return;
3193 /* If VR0 is UNDEFINED, so is the result. */
3194 if (vr0.type == VR_UNDEFINED)
3196 set_value_range_to_undefined (vr);
3197 return;
3200 /* Handle operations that we express in terms of others. */
3201 if (code == PAREN_EXPR)
3203 /* PAREN_EXPR is a simple copy. */
3204 copy_value_range (vr, &vr0);
3205 return;
3207 else if (code == NEGATE_EXPR)
3209 /* -X is simply 0 - X, so re-use existing code that also handles
3210 anti-ranges fine. */
3211 value_range_t zero = VR_INITIALIZER;
3212 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3213 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3214 return;
3216 else if (code == BIT_NOT_EXPR)
3218 /* ~X is simply -1 - X, so re-use existing code that also handles
3219 anti-ranges fine. */
3220 value_range_t minusone = VR_INITIALIZER;
3221 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3222 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3223 type, &minusone, &vr0);
3224 return;
3227 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3228 and express op ~[] as (op []') U (op []''). */
3229 if (vr0.type == VR_ANTI_RANGE
3230 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3232 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3233 if (vrtem1.type != VR_UNDEFINED)
3235 value_range_t vrres = VR_INITIALIZER;
3236 extract_range_from_unary_expr_1 (&vrres, code, type,
3237 &vrtem1, op0_type);
3238 vrp_meet (vr, &vrres);
3240 return;
3243 if (CONVERT_EXPR_CODE_P (code))
3245 tree inner_type = op0_type;
3246 tree outer_type = type;
3248 /* If the expression evaluates to a pointer, we are only interested in
3249 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3250 if (POINTER_TYPE_P (type))
3252 if (range_is_nonnull (&vr0))
3253 set_value_range_to_nonnull (vr, type);
3254 else if (range_is_null (&vr0))
3255 set_value_range_to_null (vr, type);
3256 else
3257 set_value_range_to_varying (vr);
3258 return;
3261 /* If VR0 is varying and we increase the type precision, assume
3262 a full range for the following transformation. */
3263 if (vr0.type == VR_VARYING
3264 && INTEGRAL_TYPE_P (inner_type)
3265 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3267 vr0.type = VR_RANGE;
3268 vr0.min = TYPE_MIN_VALUE (inner_type);
3269 vr0.max = TYPE_MAX_VALUE (inner_type);
3272 /* If VR0 is a constant range or anti-range and the conversion is
3273 not truncating we can convert the min and max values and
3274 canonicalize the resulting range. Otherwise we can do the
3275 conversion if the size of the range is less than what the
3276 precision of the target type can represent and the range is
3277 not an anti-range. */
3278 if ((vr0.type == VR_RANGE
3279 || vr0.type == VR_ANTI_RANGE)
3280 && TREE_CODE (vr0.min) == INTEGER_CST
3281 && TREE_CODE (vr0.max) == INTEGER_CST
3282 && (!is_overflow_infinity (vr0.min)
3283 || (vr0.type == VR_RANGE
3284 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3285 && needs_overflow_infinity (outer_type)
3286 && supports_overflow_infinity (outer_type)))
3287 && (!is_overflow_infinity (vr0.max)
3288 || (vr0.type == VR_RANGE
3289 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3290 && needs_overflow_infinity (outer_type)
3291 && supports_overflow_infinity (outer_type)))
3292 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3293 || (vr0.type == VR_RANGE
3294 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3295 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3296 size_int (TYPE_PRECISION (outer_type)))))))
3298 tree new_min, new_max;
3299 if (is_overflow_infinity (vr0.min))
3300 new_min = negative_overflow_infinity (outer_type);
3301 else
3302 new_min = force_fit_type_double (outer_type,
3303 tree_to_double_int (vr0.min),
3304 0, false);
3305 if (is_overflow_infinity (vr0.max))
3306 new_max = positive_overflow_infinity (outer_type);
3307 else
3308 new_max = force_fit_type_double (outer_type,
3309 tree_to_double_int (vr0.max),
3310 0, false);
3311 set_and_canonicalize_value_range (vr, vr0.type,
3312 new_min, new_max, NULL);
3313 return;
3316 set_value_range_to_varying (vr);
3317 return;
3319 else if (code == ABS_EXPR)
3321 tree min, max;
3322 int cmp;
3324 /* Pass through vr0 in the easy cases. */
3325 if (TYPE_UNSIGNED (type)
3326 || value_range_nonnegative_p (&vr0))
3328 copy_value_range (vr, &vr0);
3329 return;
3332 /* For the remaining varying or symbolic ranges we can't do anything
3333 useful. */
3334 if (vr0.type == VR_VARYING
3335 || symbolic_range_p (&vr0))
3337 set_value_range_to_varying (vr);
3338 return;
3341 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3342 useful range. */
3343 if (!TYPE_OVERFLOW_UNDEFINED (type)
3344 && ((vr0.type == VR_RANGE
3345 && vrp_val_is_min (vr0.min))
3346 || (vr0.type == VR_ANTI_RANGE
3347 && !vrp_val_is_min (vr0.min))))
3349 set_value_range_to_varying (vr);
3350 return;
3353 /* ABS_EXPR may flip the range around, if the original range
3354 included negative values. */
3355 if (is_overflow_infinity (vr0.min))
3356 min = positive_overflow_infinity (type);
3357 else if (!vrp_val_is_min (vr0.min))
3358 min = fold_unary_to_constant (code, type, vr0.min);
3359 else if (!needs_overflow_infinity (type))
3360 min = TYPE_MAX_VALUE (type);
3361 else if (supports_overflow_infinity (type))
3362 min = positive_overflow_infinity (type);
3363 else
3365 set_value_range_to_varying (vr);
3366 return;
3369 if (is_overflow_infinity (vr0.max))
3370 max = positive_overflow_infinity (type);
3371 else if (!vrp_val_is_min (vr0.max))
3372 max = fold_unary_to_constant (code, type, vr0.max);
3373 else if (!needs_overflow_infinity (type))
3374 max = TYPE_MAX_VALUE (type);
3375 else if (supports_overflow_infinity (type)
3376 /* We shouldn't generate [+INF, +INF] as set_value_range
3377 doesn't like this and ICEs. */
3378 && !is_positive_overflow_infinity (min))
3379 max = positive_overflow_infinity (type);
3380 else
3382 set_value_range_to_varying (vr);
3383 return;
3386 cmp = compare_values (min, max);
3388 /* If a VR_ANTI_RANGEs contains zero, then we have
3389 ~[-INF, min(MIN, MAX)]. */
3390 if (vr0.type == VR_ANTI_RANGE)
3392 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3394 /* Take the lower of the two values. */
3395 if (cmp != 1)
3396 max = min;
3398 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3399 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3400 flag_wrapv is set and the original anti-range doesn't include
3401 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3402 if (TYPE_OVERFLOW_WRAPS (type))
3404 tree type_min_value = TYPE_MIN_VALUE (type);
3406 min = (vr0.min != type_min_value
3407 ? int_const_binop (PLUS_EXPR, type_min_value,
3408 integer_one_node)
3409 : type_min_value);
3411 else
3413 if (overflow_infinity_range_p (&vr0))
3414 min = negative_overflow_infinity (type);
3415 else
3416 min = TYPE_MIN_VALUE (type);
3419 else
3421 /* All else has failed, so create the range [0, INF], even for
3422 flag_wrapv since TYPE_MIN_VALUE is in the original
3423 anti-range. */
3424 vr0.type = VR_RANGE;
3425 min = build_int_cst (type, 0);
3426 if (needs_overflow_infinity (type))
3428 if (supports_overflow_infinity (type))
3429 max = positive_overflow_infinity (type);
3430 else
3432 set_value_range_to_varying (vr);
3433 return;
3436 else
3437 max = TYPE_MAX_VALUE (type);
3441 /* If the range contains zero then we know that the minimum value in the
3442 range will be zero. */
3443 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3445 if (cmp == 1)
3446 max = min;
3447 min = build_int_cst (type, 0);
3449 else
3451 /* If the range was reversed, swap MIN and MAX. */
3452 if (cmp == 1)
3454 tree t = min;
3455 min = max;
3456 max = t;
3460 cmp = compare_values (min, max);
3461 if (cmp == -2 || cmp == 1)
3463 /* If the new range has its limits swapped around (MIN > MAX),
3464 then the operation caused one of them to wrap around, mark
3465 the new range VARYING. */
3466 set_value_range_to_varying (vr);
3468 else
3469 set_value_range (vr, vr0.type, min, max, NULL);
3470 return;
3473 /* For unhandled operations fall back to varying. */
3474 set_value_range_to_varying (vr);
3475 return;
3479 /* Extract range information from a unary expression CODE OP0 based on
3480 the range of its operand with resulting type TYPE.
3481 The resulting range is stored in *VR. */
3483 static void
3484 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3485 tree type, tree op0)
3487 value_range_t vr0 = VR_INITIALIZER;
3489 /* Get value ranges for the operand. For constant operands, create
3490 a new value range with the operand to simplify processing. */
3491 if (TREE_CODE (op0) == SSA_NAME)
3492 vr0 = *(get_value_range (op0));
3493 else if (is_gimple_min_invariant (op0))
3494 set_value_range_to_value (&vr0, op0, NULL);
3495 else
3496 set_value_range_to_varying (&vr0);
3498 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3502 /* Extract range information from a conditional expression STMT based on
3503 the ranges of each of its operands and the expression code. */
3505 static void
3506 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3508 tree op0, op1;
3509 value_range_t vr0 = VR_INITIALIZER;
3510 value_range_t vr1 = VR_INITIALIZER;
3512 /* Get value ranges for each operand. For constant operands, create
3513 a new value range with the operand to simplify processing. */
3514 op0 = gimple_assign_rhs2 (stmt);
3515 if (TREE_CODE (op0) == SSA_NAME)
3516 vr0 = *(get_value_range (op0));
3517 else if (is_gimple_min_invariant (op0))
3518 set_value_range_to_value (&vr0, op0, NULL);
3519 else
3520 set_value_range_to_varying (&vr0);
3522 op1 = gimple_assign_rhs3 (stmt);
3523 if (TREE_CODE (op1) == SSA_NAME)
3524 vr1 = *(get_value_range (op1));
3525 else if (is_gimple_min_invariant (op1))
3526 set_value_range_to_value (&vr1, op1, NULL);
3527 else
3528 set_value_range_to_varying (&vr1);
3530 /* The resulting value range is the union of the operand ranges */
3531 copy_value_range (vr, &vr0);
3532 vrp_meet (vr, &vr1);
3536 /* Extract range information from a comparison expression EXPR based
3537 on the range of its operand and the expression code. */
3539 static void
3540 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3541 tree type, tree op0, tree op1)
3543 bool sop = false;
3544 tree val;
3546 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3547 NULL);
3549 /* A disadvantage of using a special infinity as an overflow
3550 representation is that we lose the ability to record overflow
3551 when we don't have an infinity. So we have to ignore a result
3552 which relies on overflow. */
3554 if (val && !is_overflow_infinity (val) && !sop)
3556 /* Since this expression was found on the RHS of an assignment,
3557 its type may be different from _Bool. Convert VAL to EXPR's
3558 type. */
3559 val = fold_convert (type, val);
3560 if (is_gimple_min_invariant (val))
3561 set_value_range_to_value (vr, val, vr->equiv);
3562 else
3563 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3565 else
3566 /* The result of a comparison is always true or false. */
3567 set_value_range_to_truthvalue (vr, type);
3570 /* Try to derive a nonnegative or nonzero range out of STMT relying
3571 primarily on generic routines in fold in conjunction with range data.
3572 Store the result in *VR */
3574 static void
3575 extract_range_basic (value_range_t *vr, gimple stmt)
3577 bool sop = false;
3578 tree type = gimple_expr_type (stmt);
3580 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3582 tree fndecl = gimple_call_fndecl (stmt), arg;
3583 int mini, maxi, zerov = 0, prec;
3585 switch (DECL_FUNCTION_CODE (fndecl))
3587 case BUILT_IN_CONSTANT_P:
3588 /* If the call is __builtin_constant_p and the argument is a
3589 function parameter resolve it to false. This avoids bogus
3590 array bound warnings.
3591 ??? We could do this as early as inlining is finished. */
3592 arg = gimple_call_arg (stmt, 0);
3593 if (TREE_CODE (arg) == SSA_NAME
3594 && SSA_NAME_IS_DEFAULT_DEF (arg)
3595 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3597 set_value_range_to_null (vr, type);
3598 return;
3600 break;
3601 /* Both __builtin_ffs* and __builtin_popcount return
3602 [0, prec]. */
3603 CASE_INT_FN (BUILT_IN_FFS):
3604 CASE_INT_FN (BUILT_IN_POPCOUNT):
3605 arg = gimple_call_arg (stmt, 0);
3606 prec = TYPE_PRECISION (TREE_TYPE (arg));
3607 mini = 0;
3608 maxi = prec;
3609 if (TREE_CODE (arg) == SSA_NAME)
3611 value_range_t *vr0 = get_value_range (arg);
3612 /* If arg is non-zero, then ffs or popcount
3613 are non-zero. */
3614 if (((vr0->type == VR_RANGE
3615 && integer_nonzerop (vr0->min))
3616 || (vr0->type == VR_ANTI_RANGE
3617 && integer_zerop (vr0->min)))
3618 && !is_overflow_infinity (vr0->min))
3619 mini = 1;
3620 /* If some high bits are known to be zero,
3621 we can decrease the maximum. */
3622 if (vr0->type == VR_RANGE
3623 && TREE_CODE (vr0->max) == INTEGER_CST
3624 && !is_overflow_infinity (vr0->max))
3625 maxi = tree_floor_log2 (vr0->max) + 1;
3627 goto bitop_builtin;
3628 /* __builtin_parity* returns [0, 1]. */
3629 CASE_INT_FN (BUILT_IN_PARITY):
3630 mini = 0;
3631 maxi = 1;
3632 goto bitop_builtin;
3633 /* __builtin_c[lt]z* return [0, prec-1], except for
3634 when the argument is 0, but that is undefined behavior.
3635 On many targets where the CLZ RTL or optab value is defined
3636 for 0 the value is prec, so include that in the range
3637 by default. */
3638 CASE_INT_FN (BUILT_IN_CLZ):
3639 arg = gimple_call_arg (stmt, 0);
3640 prec = TYPE_PRECISION (TREE_TYPE (arg));
3641 mini = 0;
3642 maxi = prec;
3643 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3644 != CODE_FOR_nothing
3645 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3646 zerov)
3647 /* Handle only the single common value. */
3648 && zerov != prec)
3649 /* Magic value to give up, unless vr0 proves
3650 arg is non-zero. */
3651 mini = -2;
3652 if (TREE_CODE (arg) == SSA_NAME)
3654 value_range_t *vr0 = get_value_range (arg);
3655 /* From clz of VR_RANGE minimum we can compute
3656 result maximum. */
3657 if (vr0->type == VR_RANGE
3658 && TREE_CODE (vr0->min) == INTEGER_CST
3659 && !is_overflow_infinity (vr0->min))
3661 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3662 if (maxi != prec)
3663 mini = 0;
3665 else if (vr0->type == VR_ANTI_RANGE
3666 && integer_zerop (vr0->min)
3667 && !is_overflow_infinity (vr0->min))
3669 maxi = prec - 1;
3670 mini = 0;
3672 if (mini == -2)
3673 break;
3674 /* From clz of VR_RANGE maximum we can compute
3675 result minimum. */
3676 if (vr0->type == VR_RANGE
3677 && TREE_CODE (vr0->max) == INTEGER_CST
3678 && !is_overflow_infinity (vr0->max))
3680 mini = prec - 1 - tree_floor_log2 (vr0->max);
3681 if (mini == prec)
3682 break;
3685 if (mini == -2)
3686 break;
3687 goto bitop_builtin;
3688 /* __builtin_ctz* return [0, prec-1], except for
3689 when the argument is 0, but that is undefined behavior.
3690 If there is a ctz optab for this mode and
3691 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3692 otherwise just assume 0 won't be seen. */
3693 CASE_INT_FN (BUILT_IN_CTZ):
3694 arg = gimple_call_arg (stmt, 0);
3695 prec = TYPE_PRECISION (TREE_TYPE (arg));
3696 mini = 0;
3697 maxi = prec - 1;
3698 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3699 != CODE_FOR_nothing
3700 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3701 zerov))
3703 /* Handle only the two common values. */
3704 if (zerov == -1)
3705 mini = -1;
3706 else if (zerov == prec)
3707 maxi = prec;
3708 else
3709 /* Magic value to give up, unless vr0 proves
3710 arg is non-zero. */
3711 mini = -2;
3713 if (TREE_CODE (arg) == SSA_NAME)
3715 value_range_t *vr0 = get_value_range (arg);
3716 /* If arg is non-zero, then use [0, prec - 1]. */
3717 if (((vr0->type == VR_RANGE
3718 && integer_nonzerop (vr0->min))
3719 || (vr0->type == VR_ANTI_RANGE
3720 && integer_zerop (vr0->min)))
3721 && !is_overflow_infinity (vr0->min))
3723 mini = 0;
3724 maxi = prec - 1;
3726 /* If some high bits are known to be zero,
3727 we can decrease the result maximum. */
3728 if (vr0->type == VR_RANGE
3729 && TREE_CODE (vr0->max) == INTEGER_CST
3730 && !is_overflow_infinity (vr0->max))
3732 maxi = tree_floor_log2 (vr0->max);
3733 /* For vr0 [0, 0] give up. */
3734 if (maxi == -1)
3735 break;
3738 if (mini == -2)
3739 break;
3740 goto bitop_builtin;
3741 /* __builtin_clrsb* returns [0, prec-1]. */
3742 CASE_INT_FN (BUILT_IN_CLRSB):
3743 arg = gimple_call_arg (stmt, 0);
3744 prec = TYPE_PRECISION (TREE_TYPE (arg));
3745 mini = 0;
3746 maxi = prec - 1;
3747 goto bitop_builtin;
3748 bitop_builtin:
3749 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3750 build_int_cst (type, maxi), NULL);
3751 return;
3752 default:
3753 break;
3756 if (INTEGRAL_TYPE_P (type)
3757 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3758 set_value_range_to_nonnegative (vr, type,
3759 sop || stmt_overflow_infinity (stmt));
3760 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3761 && !sop)
3762 set_value_range_to_nonnull (vr, type);
3763 else
3764 set_value_range_to_varying (vr);
3768 /* Try to compute a useful range out of assignment STMT and store it
3769 in *VR. */
3771 static void
3772 extract_range_from_assignment (value_range_t *vr, gimple stmt)
3774 enum tree_code code = gimple_assign_rhs_code (stmt);
3776 if (code == ASSERT_EXPR)
3777 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3778 else if (code == SSA_NAME)
3779 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3780 else if (TREE_CODE_CLASS (code) == tcc_binary)
3781 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3782 gimple_expr_type (stmt),
3783 gimple_assign_rhs1 (stmt),
3784 gimple_assign_rhs2 (stmt));
3785 else if (TREE_CODE_CLASS (code) == tcc_unary)
3786 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3787 gimple_expr_type (stmt),
3788 gimple_assign_rhs1 (stmt));
3789 else if (code == COND_EXPR)
3790 extract_range_from_cond_expr (vr, stmt);
3791 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3792 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3793 gimple_expr_type (stmt),
3794 gimple_assign_rhs1 (stmt),
3795 gimple_assign_rhs2 (stmt));
3796 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3797 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3798 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3799 else
3800 set_value_range_to_varying (vr);
3802 if (vr->type == VR_VARYING)
3803 extract_range_basic (vr, stmt);
3806 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3807 would be profitable to adjust VR using scalar evolution information
3808 for VAR. If so, update VR with the new limits. */
3810 static void
3811 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
3812 gimple stmt, tree var)
3814 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3815 enum ev_direction dir;
3817 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3818 better opportunities than a regular range, but I'm not sure. */
3819 if (vr->type == VR_ANTI_RANGE)
3820 return;
3822 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3824 /* Like in PR19590, scev can return a constant function. */
3825 if (is_gimple_min_invariant (chrec))
3827 set_value_range_to_value (vr, chrec, vr->equiv);
3828 return;
3831 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3832 return;
3834 init = initial_condition_in_loop_num (chrec, loop->num);
3835 tem = op_with_constant_singleton_value_range (init);
3836 if (tem)
3837 init = tem;
3838 step = evolution_part_in_loop_num (chrec, loop->num);
3839 tem = op_with_constant_singleton_value_range (step);
3840 if (tem)
3841 step = tem;
3843 /* If STEP is symbolic, we can't know whether INIT will be the
3844 minimum or maximum value in the range. Also, unless INIT is
3845 a simple expression, compare_values and possibly other functions
3846 in tree-vrp won't be able to handle it. */
3847 if (step == NULL_TREE
3848 || !is_gimple_min_invariant (step)
3849 || !valid_value_p (init))
3850 return;
3852 dir = scev_direction (chrec);
3853 if (/* Do not adjust ranges if we do not know whether the iv increases
3854 or decreases, ... */
3855 dir == EV_DIR_UNKNOWN
3856 /* ... or if it may wrap. */
3857 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3858 true))
3859 return;
3861 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3862 negative_overflow_infinity and positive_overflow_infinity,
3863 because we have concluded that the loop probably does not
3864 wrap. */
3866 type = TREE_TYPE (var);
3867 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
3868 tmin = lower_bound_in_type (type, type);
3869 else
3870 tmin = TYPE_MIN_VALUE (type);
3871 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
3872 tmax = upper_bound_in_type (type, type);
3873 else
3874 tmax = TYPE_MAX_VALUE (type);
3876 /* Try to use estimated number of iterations for the loop to constrain the
3877 final value in the evolution. */
3878 if (TREE_CODE (step) == INTEGER_CST
3879 && is_gimple_val (init)
3880 && (TREE_CODE (init) != SSA_NAME
3881 || get_value_range (init)->type == VR_RANGE))
3883 double_int nit;
3885 /* We are only entering here for loop header PHI nodes, so using
3886 the number of latch executions is the correct thing to use. */
3887 if (max_loop_iterations (loop, &nit))
3889 value_range_t maxvr = VR_INITIALIZER;
3890 double_int dtmp;
3891 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (step));
3892 bool overflow = false;
3894 dtmp = tree_to_double_int (step)
3895 .mul_with_sign (nit, unsigned_p, &overflow);
3896 /* If the multiplication overflowed we can't do a meaningful
3897 adjustment. Likewise if the result doesn't fit in the type
3898 of the induction variable. For a signed type we have to
3899 check whether the result has the expected signedness which
3900 is that of the step as number of iterations is unsigned. */
3901 if (!overflow
3902 && double_int_fits_to_tree_p (TREE_TYPE (init), dtmp)
3903 && (unsigned_p
3904 || ((dtmp.high ^ TREE_INT_CST_HIGH (step)) >= 0)))
3906 tem = double_int_to_tree (TREE_TYPE (init), dtmp);
3907 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
3908 TREE_TYPE (init), init, tem);
3909 /* Likewise if the addition did. */
3910 if (maxvr.type == VR_RANGE)
3912 tmin = maxvr.min;
3913 tmax = maxvr.max;
3919 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3921 min = tmin;
3922 max = tmax;
3924 /* For VARYING or UNDEFINED ranges, just about anything we get
3925 from scalar evolutions should be better. */
3927 if (dir == EV_DIR_DECREASES)
3928 max = init;
3929 else
3930 min = init;
3932 /* If we would create an invalid range, then just assume we
3933 know absolutely nothing. This may be over-conservative,
3934 but it's clearly safe, and should happen only in unreachable
3935 parts of code, or for invalid programs. */
3936 if (compare_values (min, max) == 1)
3937 return;
3939 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3941 else if (vr->type == VR_RANGE)
3943 min = vr->min;
3944 max = vr->max;
3946 if (dir == EV_DIR_DECREASES)
3948 /* INIT is the maximum value. If INIT is lower than VR->MAX
3949 but no smaller than VR->MIN, set VR->MAX to INIT. */
3950 if (compare_values (init, max) == -1)
3951 max = init;
3953 /* According to the loop information, the variable does not
3954 overflow. If we think it does, probably because of an
3955 overflow due to arithmetic on a different INF value,
3956 reset now. */
3957 if (is_negative_overflow_infinity (min)
3958 || compare_values (min, tmin) == -1)
3959 min = tmin;
3962 else
3964 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3965 if (compare_values (init, min) == 1)
3966 min = init;
3968 if (is_positive_overflow_infinity (max)
3969 || compare_values (tmax, max) == -1)
3970 max = tmax;
3973 /* If we just created an invalid range with the minimum
3974 greater than the maximum, we fail conservatively.
3975 This should happen only in unreachable
3976 parts of code, or for invalid programs. */
3977 if (compare_values (min, max) == 1)
3978 return;
3980 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3984 /* Return true if VAR may overflow at STMT. This checks any available
3985 loop information to see if we can determine that VAR does not
3986 overflow. */
3988 static bool
3989 vrp_var_may_overflow (tree var, gimple stmt)
3991 struct loop *l;
3992 tree chrec, init, step;
3994 if (current_loops == NULL)
3995 return true;
3997 l = loop_containing_stmt (stmt);
3998 if (l == NULL
3999 || !loop_outer (l))
4000 return true;
4002 chrec = instantiate_parameters (l, analyze_scalar_evolution (l, var));
4003 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4004 return true;
4006 init = initial_condition_in_loop_num (chrec, l->num);
4007 step = evolution_part_in_loop_num (chrec, l->num);
4009 if (step == NULL_TREE
4010 || !is_gimple_min_invariant (step)
4011 || !valid_value_p (init))
4012 return true;
4014 /* If we get here, we know something useful about VAR based on the
4015 loop information. If it wraps, it may overflow. */
4017 if (scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4018 true))
4019 return true;
4021 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
4023 print_generic_expr (dump_file, var, 0);
4024 fprintf (dump_file, ": loop information indicates does not overflow\n");
4027 return false;
4031 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4033 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4034 all the values in the ranges.
4036 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4038 - Return NULL_TREE if it is not always possible to determine the
4039 value of the comparison.
4041 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4042 overflow infinity was used in the test. */
4045 static tree
4046 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4047 bool *strict_overflow_p)
4049 /* VARYING or UNDEFINED ranges cannot be compared. */
4050 if (vr0->type == VR_VARYING
4051 || vr0->type == VR_UNDEFINED
4052 || vr1->type == VR_VARYING
4053 || vr1->type == VR_UNDEFINED)
4054 return NULL_TREE;
4056 /* Anti-ranges need to be handled separately. */
4057 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4059 /* If both are anti-ranges, then we cannot compute any
4060 comparison. */
4061 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4062 return NULL_TREE;
4064 /* These comparisons are never statically computable. */
4065 if (comp == GT_EXPR
4066 || comp == GE_EXPR
4067 || comp == LT_EXPR
4068 || comp == LE_EXPR)
4069 return NULL_TREE;
4071 /* Equality can be computed only between a range and an
4072 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4073 if (vr0->type == VR_RANGE)
4075 /* To simplify processing, make VR0 the anti-range. */
4076 value_range_t *tmp = vr0;
4077 vr0 = vr1;
4078 vr1 = tmp;
4081 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4083 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4084 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4085 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4087 return NULL_TREE;
4090 if (!usable_range_p (vr0, strict_overflow_p)
4091 || !usable_range_p (vr1, strict_overflow_p))
4092 return NULL_TREE;
4094 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4095 operands around and change the comparison code. */
4096 if (comp == GT_EXPR || comp == GE_EXPR)
4098 value_range_t *tmp;
4099 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4100 tmp = vr0;
4101 vr0 = vr1;
4102 vr1 = tmp;
4105 if (comp == EQ_EXPR)
4107 /* Equality may only be computed if both ranges represent
4108 exactly one value. */
4109 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4110 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4112 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4113 strict_overflow_p);
4114 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4115 strict_overflow_p);
4116 if (cmp_min == 0 && cmp_max == 0)
4117 return boolean_true_node;
4118 else if (cmp_min != -2 && cmp_max != -2)
4119 return boolean_false_node;
4121 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4122 else if (compare_values_warnv (vr0->min, vr1->max,
4123 strict_overflow_p) == 1
4124 || compare_values_warnv (vr1->min, vr0->max,
4125 strict_overflow_p) == 1)
4126 return boolean_false_node;
4128 return NULL_TREE;
4130 else if (comp == NE_EXPR)
4132 int cmp1, cmp2;
4134 /* If VR0 is completely to the left or completely to the right
4135 of VR1, they are always different. Notice that we need to
4136 make sure that both comparisons yield similar results to
4137 avoid comparing values that cannot be compared at
4138 compile-time. */
4139 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4140 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4141 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4142 return boolean_true_node;
4144 /* If VR0 and VR1 represent a single value and are identical,
4145 return false. */
4146 else if (compare_values_warnv (vr0->min, vr0->max,
4147 strict_overflow_p) == 0
4148 && compare_values_warnv (vr1->min, vr1->max,
4149 strict_overflow_p) == 0
4150 && compare_values_warnv (vr0->min, vr1->min,
4151 strict_overflow_p) == 0
4152 && compare_values_warnv (vr0->max, vr1->max,
4153 strict_overflow_p) == 0)
4154 return boolean_false_node;
4156 /* Otherwise, they may or may not be different. */
4157 else
4158 return NULL_TREE;
4160 else if (comp == LT_EXPR || comp == LE_EXPR)
4162 int tst;
4164 /* If VR0 is to the left of VR1, return true. */
4165 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4166 if ((comp == LT_EXPR && tst == -1)
4167 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4169 if (overflow_infinity_range_p (vr0)
4170 || overflow_infinity_range_p (vr1))
4171 *strict_overflow_p = true;
4172 return boolean_true_node;
4175 /* If VR0 is to the right of VR1, return false. */
4176 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4177 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4178 || (comp == LE_EXPR && tst == 1))
4180 if (overflow_infinity_range_p (vr0)
4181 || overflow_infinity_range_p (vr1))
4182 *strict_overflow_p = true;
4183 return boolean_false_node;
4186 /* Otherwise, we don't know. */
4187 return NULL_TREE;
4190 gcc_unreachable ();
4194 /* Given a value range VR, a value VAL and a comparison code COMP, return
4195 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4196 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4197 always returns false. Return NULL_TREE if it is not always
4198 possible to determine the value of the comparison. Also set
4199 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4200 infinity was used in the test. */
4202 static tree
4203 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4204 bool *strict_overflow_p)
4206 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4207 return NULL_TREE;
4209 /* Anti-ranges need to be handled separately. */
4210 if (vr->type == VR_ANTI_RANGE)
4212 /* For anti-ranges, the only predicates that we can compute at
4213 compile time are equality and inequality. */
4214 if (comp == GT_EXPR
4215 || comp == GE_EXPR
4216 || comp == LT_EXPR
4217 || comp == LE_EXPR)
4218 return NULL_TREE;
4220 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4221 if (value_inside_range (val, vr->min, vr->max) == 1)
4222 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4224 return NULL_TREE;
4227 if (!usable_range_p (vr, strict_overflow_p))
4228 return NULL_TREE;
4230 if (comp == EQ_EXPR)
4232 /* EQ_EXPR may only be computed if VR represents exactly
4233 one value. */
4234 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4236 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4237 if (cmp == 0)
4238 return boolean_true_node;
4239 else if (cmp == -1 || cmp == 1 || cmp == 2)
4240 return boolean_false_node;
4242 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4243 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4244 return boolean_false_node;
4246 return NULL_TREE;
4248 else if (comp == NE_EXPR)
4250 /* If VAL is not inside VR, then they are always different. */
4251 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4252 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4253 return boolean_true_node;
4255 /* If VR represents exactly one value equal to VAL, then return
4256 false. */
4257 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4258 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4259 return boolean_false_node;
4261 /* Otherwise, they may or may not be different. */
4262 return NULL_TREE;
4264 else if (comp == LT_EXPR || comp == LE_EXPR)
4266 int tst;
4268 /* If VR is to the left of VAL, return true. */
4269 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4270 if ((comp == LT_EXPR && tst == -1)
4271 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4273 if (overflow_infinity_range_p (vr))
4274 *strict_overflow_p = true;
4275 return boolean_true_node;
4278 /* If VR is to the right of VAL, return false. */
4279 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4280 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4281 || (comp == LE_EXPR && tst == 1))
4283 if (overflow_infinity_range_p (vr))
4284 *strict_overflow_p = true;
4285 return boolean_false_node;
4288 /* Otherwise, we don't know. */
4289 return NULL_TREE;
4291 else if (comp == GT_EXPR || comp == GE_EXPR)
4293 int tst;
4295 /* If VR is to the right of VAL, return true. */
4296 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4297 if ((comp == GT_EXPR && tst == 1)
4298 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4300 if (overflow_infinity_range_p (vr))
4301 *strict_overflow_p = true;
4302 return boolean_true_node;
4305 /* If VR is to the left of VAL, return false. */
4306 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4307 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4308 || (comp == GE_EXPR && tst == -1))
4310 if (overflow_infinity_range_p (vr))
4311 *strict_overflow_p = true;
4312 return boolean_false_node;
4315 /* Otherwise, we don't know. */
4316 return NULL_TREE;
4319 gcc_unreachable ();
4323 /* Debugging dumps. */
4325 void dump_value_range (FILE *, value_range_t *);
4326 void debug_value_range (value_range_t *);
4327 void dump_all_value_ranges (FILE *);
4328 void debug_all_value_ranges (void);
4329 void dump_vr_equiv (FILE *, bitmap);
4330 void debug_vr_equiv (bitmap);
4333 /* Dump value range VR to FILE. */
4335 void
4336 dump_value_range (FILE *file, value_range_t *vr)
4338 if (vr == NULL)
4339 fprintf (file, "[]");
4340 else if (vr->type == VR_UNDEFINED)
4341 fprintf (file, "UNDEFINED");
4342 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4344 tree type = TREE_TYPE (vr->min);
4346 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4348 if (is_negative_overflow_infinity (vr->min))
4349 fprintf (file, "-INF(OVF)");
4350 else if (INTEGRAL_TYPE_P (type)
4351 && !TYPE_UNSIGNED (type)
4352 && vrp_val_is_min (vr->min))
4353 fprintf (file, "-INF");
4354 else
4355 print_generic_expr (file, vr->min, 0);
4357 fprintf (file, ", ");
4359 if (is_positive_overflow_infinity (vr->max))
4360 fprintf (file, "+INF(OVF)");
4361 else if (INTEGRAL_TYPE_P (type)
4362 && vrp_val_is_max (vr->max))
4363 fprintf (file, "+INF");
4364 else
4365 print_generic_expr (file, vr->max, 0);
4367 fprintf (file, "]");
4369 if (vr->equiv)
4371 bitmap_iterator bi;
4372 unsigned i, c = 0;
4374 fprintf (file, " EQUIVALENCES: { ");
4376 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4378 print_generic_expr (file, ssa_name (i), 0);
4379 fprintf (file, " ");
4380 c++;
4383 fprintf (file, "} (%u elements)", c);
4386 else if (vr->type == VR_VARYING)
4387 fprintf (file, "VARYING");
4388 else
4389 fprintf (file, "INVALID RANGE");
4393 /* Dump value range VR to stderr. */
4395 DEBUG_FUNCTION void
4396 debug_value_range (value_range_t *vr)
4398 dump_value_range (stderr, vr);
4399 fprintf (stderr, "\n");
4403 /* Dump value ranges of all SSA_NAMEs to FILE. */
4405 void
4406 dump_all_value_ranges (FILE *file)
4408 size_t i;
4410 for (i = 0; i < num_vr_values; i++)
4412 if (vr_value[i])
4414 print_generic_expr (file, ssa_name (i), 0);
4415 fprintf (file, ": ");
4416 dump_value_range (file, vr_value[i]);
4417 fprintf (file, "\n");
4421 fprintf (file, "\n");
4425 /* Dump all value ranges to stderr. */
4427 DEBUG_FUNCTION void
4428 debug_all_value_ranges (void)
4430 dump_all_value_ranges (stderr);
4434 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4435 create a new SSA name N and return the assertion assignment
4436 'V = ASSERT_EXPR <V, V OP W>'. */
4438 static gimple
4439 build_assert_expr_for (tree cond, tree v)
4441 tree a;
4442 gimple assertion;
4444 gcc_assert (TREE_CODE (v) == SSA_NAME
4445 && COMPARISON_CLASS_P (cond));
4447 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4448 assertion = gimple_build_assign (NULL_TREE, a);
4450 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4451 operand of the ASSERT_EXPR. Create it so the new name and the old one
4452 are registered in the replacement table so that we can fix the SSA web
4453 after adding all the ASSERT_EXPRs. */
4454 create_new_def_for (v, assertion, NULL);
4456 return assertion;
4460 /* Return false if EXPR is a predicate expression involving floating
4461 point values. */
4463 static inline bool
4464 fp_predicate (gimple stmt)
4466 GIMPLE_CHECK (stmt, GIMPLE_COND);
4468 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4471 /* If the range of values taken by OP can be inferred after STMT executes,
4472 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4473 describes the inferred range. Return true if a range could be
4474 inferred. */
4476 static bool
4477 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4479 *val_p = NULL_TREE;
4480 *comp_code_p = ERROR_MARK;
4482 /* Do not attempt to infer anything in names that flow through
4483 abnormal edges. */
4484 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4485 return false;
4487 /* Similarly, don't infer anything from statements that may throw
4488 exceptions. ??? Relax this requirement? */
4489 if (stmt_could_throw_p (stmt))
4490 return false;
4492 /* If STMT is the last statement of a basic block with no
4493 successors, there is no point inferring anything about any of its
4494 operands. We would not be able to find a proper insertion point
4495 for the assertion, anyway. */
4496 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (gimple_bb (stmt)->succs) == 0)
4497 return false;
4499 if (infer_nonnull_range (stmt, op))
4501 *val_p = build_int_cst (TREE_TYPE (op), 0);
4502 *comp_code_p = NE_EXPR;
4503 return true;
4506 return false;
4510 void dump_asserts_for (FILE *, tree);
4511 void debug_asserts_for (tree);
4512 void dump_all_asserts (FILE *);
4513 void debug_all_asserts (void);
4515 /* Dump all the registered assertions for NAME to FILE. */
4517 void
4518 dump_asserts_for (FILE *file, tree name)
4520 assert_locus_t loc;
4522 fprintf (file, "Assertions to be inserted for ");
4523 print_generic_expr (file, name, 0);
4524 fprintf (file, "\n");
4526 loc = asserts_for[SSA_NAME_VERSION (name)];
4527 while (loc)
4529 fprintf (file, "\t");
4530 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4531 fprintf (file, "\n\tBB #%d", loc->bb->index);
4532 if (loc->e)
4534 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4535 loc->e->dest->index);
4536 dump_edge_info (file, loc->e, dump_flags, 0);
4538 fprintf (file, "\n\tPREDICATE: ");
4539 print_generic_expr (file, name, 0);
4540 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4541 print_generic_expr (file, loc->val, 0);
4542 fprintf (file, "\n\n");
4543 loc = loc->next;
4546 fprintf (file, "\n");
4550 /* Dump all the registered assertions for NAME to stderr. */
4552 DEBUG_FUNCTION void
4553 debug_asserts_for (tree name)
4555 dump_asserts_for (stderr, name);
4559 /* Dump all the registered assertions for all the names to FILE. */
4561 void
4562 dump_all_asserts (FILE *file)
4564 unsigned i;
4565 bitmap_iterator bi;
4567 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4568 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4569 dump_asserts_for (file, ssa_name (i));
4570 fprintf (file, "\n");
4574 /* Dump all the registered assertions for all the names to stderr. */
4576 DEBUG_FUNCTION void
4577 debug_all_asserts (void)
4579 dump_all_asserts (stderr);
4583 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4584 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4585 E->DEST, then register this location as a possible insertion point
4586 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4588 BB, E and SI provide the exact insertion point for the new
4589 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4590 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4591 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4592 must not be NULL. */
4594 static void
4595 register_new_assert_for (tree name, tree expr,
4596 enum tree_code comp_code,
4597 tree val,
4598 basic_block bb,
4599 edge e,
4600 gimple_stmt_iterator si)
4602 assert_locus_t n, loc, last_loc;
4603 basic_block dest_bb;
4605 gcc_checking_assert (bb == NULL || e == NULL);
4607 if (e == NULL)
4608 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4609 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4611 /* Never build an assert comparing against an integer constant with
4612 TREE_OVERFLOW set. This confuses our undefined overflow warning
4613 machinery. */
4614 if (TREE_OVERFLOW_P (val))
4615 val = drop_tree_overflow (val);
4617 /* The new assertion A will be inserted at BB or E. We need to
4618 determine if the new location is dominated by a previously
4619 registered location for A. If we are doing an edge insertion,
4620 assume that A will be inserted at E->DEST. Note that this is not
4621 necessarily true.
4623 If E is a critical edge, it will be split. But even if E is
4624 split, the new block will dominate the same set of blocks that
4625 E->DEST dominates.
4627 The reverse, however, is not true, blocks dominated by E->DEST
4628 will not be dominated by the new block created to split E. So,
4629 if the insertion location is on a critical edge, we will not use
4630 the new location to move another assertion previously registered
4631 at a block dominated by E->DEST. */
4632 dest_bb = (bb) ? bb : e->dest;
4634 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4635 VAL at a block dominating DEST_BB, then we don't need to insert a new
4636 one. Similarly, if the same assertion already exists at a block
4637 dominated by DEST_BB and the new location is not on a critical
4638 edge, then update the existing location for the assertion (i.e.,
4639 move the assertion up in the dominance tree).
4641 Note, this is implemented as a simple linked list because there
4642 should not be more than a handful of assertions registered per
4643 name. If this becomes a performance problem, a table hashed by
4644 COMP_CODE and VAL could be implemented. */
4645 loc = asserts_for[SSA_NAME_VERSION (name)];
4646 last_loc = loc;
4647 while (loc)
4649 if (loc->comp_code == comp_code
4650 && (loc->val == val
4651 || operand_equal_p (loc->val, val, 0))
4652 && (loc->expr == expr
4653 || operand_equal_p (loc->expr, expr, 0)))
4655 /* If E is not a critical edge and DEST_BB
4656 dominates the existing location for the assertion, move
4657 the assertion up in the dominance tree by updating its
4658 location information. */
4659 if ((e == NULL || !EDGE_CRITICAL_P (e))
4660 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4662 loc->bb = dest_bb;
4663 loc->e = e;
4664 loc->si = si;
4665 return;
4669 /* Update the last node of the list and move to the next one. */
4670 last_loc = loc;
4671 loc = loc->next;
4674 /* If we didn't find an assertion already registered for
4675 NAME COMP_CODE VAL, add a new one at the end of the list of
4676 assertions associated with NAME. */
4677 n = XNEW (struct assert_locus_d);
4678 n->bb = dest_bb;
4679 n->e = e;
4680 n->si = si;
4681 n->comp_code = comp_code;
4682 n->val = val;
4683 n->expr = expr;
4684 n->next = NULL;
4686 if (last_loc)
4687 last_loc->next = n;
4688 else
4689 asserts_for[SSA_NAME_VERSION (name)] = n;
4691 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4694 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4695 Extract a suitable test code and value and store them into *CODE_P and
4696 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4698 If no extraction was possible, return FALSE, otherwise return TRUE.
4700 If INVERT is true, then we invert the result stored into *CODE_P. */
4702 static bool
4703 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4704 tree cond_op0, tree cond_op1,
4705 bool invert, enum tree_code *code_p,
4706 tree *val_p)
4708 enum tree_code comp_code;
4709 tree val;
4711 /* Otherwise, we have a comparison of the form NAME COMP VAL
4712 or VAL COMP NAME. */
4713 if (name == cond_op1)
4715 /* If the predicate is of the form VAL COMP NAME, flip
4716 COMP around because we need to register NAME as the
4717 first operand in the predicate. */
4718 comp_code = swap_tree_comparison (cond_code);
4719 val = cond_op0;
4721 else
4723 /* The comparison is of the form NAME COMP VAL, so the
4724 comparison code remains unchanged. */
4725 comp_code = cond_code;
4726 val = cond_op1;
4729 /* Invert the comparison code as necessary. */
4730 if (invert)
4731 comp_code = invert_tree_comparison (comp_code, 0);
4733 /* VRP does not handle float types. */
4734 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4735 return false;
4737 /* Do not register always-false predicates.
4738 FIXME: this works around a limitation in fold() when dealing with
4739 enumerations. Given 'enum { N1, N2 } x;', fold will not
4740 fold 'if (x > N2)' to 'if (0)'. */
4741 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4742 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4744 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4745 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4747 if (comp_code == GT_EXPR
4748 && (!max
4749 || compare_values (val, max) == 0))
4750 return false;
4752 if (comp_code == LT_EXPR
4753 && (!min
4754 || compare_values (val, min) == 0))
4755 return false;
4757 *code_p = comp_code;
4758 *val_p = val;
4759 return true;
4762 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4763 (otherwise return VAL). VAL and MASK must be zero-extended for
4764 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4765 (to transform signed values into unsigned) and at the end xor
4766 SGNBIT back. */
4768 static double_int
4769 masked_increment (double_int val, double_int mask, double_int sgnbit,
4770 unsigned int prec)
4772 double_int bit = double_int_one, res;
4773 unsigned int i;
4775 val ^= sgnbit;
4776 for (i = 0; i < prec; i++, bit += bit)
4778 res = mask;
4779 if ((res & bit).is_zero ())
4780 continue;
4781 res = bit - double_int_one;
4782 res = (val + bit).and_not (res);
4783 res &= mask;
4784 if (res.ugt (val))
4785 return res ^ sgnbit;
4787 return val ^ sgnbit;
4790 /* Try to register an edge assertion for SSA name NAME on edge E for
4791 the condition COND contributing to the conditional jump pointed to by BSI.
4792 Invert the condition COND if INVERT is true.
4793 Return true if an assertion for NAME could be registered. */
4795 static bool
4796 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4797 enum tree_code cond_code,
4798 tree cond_op0, tree cond_op1, bool invert)
4800 tree val;
4801 enum tree_code comp_code;
4802 bool retval = false;
4804 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4805 cond_op0,
4806 cond_op1,
4807 invert, &comp_code, &val))
4808 return false;
4810 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4811 reachable from E. */
4812 if (live_on_edge (e, name)
4813 && !has_single_use (name))
4815 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
4816 retval = true;
4819 /* In the case of NAME <= CST and NAME being defined as
4820 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4821 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4822 This catches range and anti-range tests. */
4823 if ((comp_code == LE_EXPR
4824 || comp_code == GT_EXPR)
4825 && TREE_CODE (val) == INTEGER_CST
4826 && TYPE_UNSIGNED (TREE_TYPE (val)))
4828 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4829 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
4831 /* Extract CST2 from the (optional) addition. */
4832 if (is_gimple_assign (def_stmt)
4833 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
4835 name2 = gimple_assign_rhs1 (def_stmt);
4836 cst2 = gimple_assign_rhs2 (def_stmt);
4837 if (TREE_CODE (name2) == SSA_NAME
4838 && TREE_CODE (cst2) == INTEGER_CST)
4839 def_stmt = SSA_NAME_DEF_STMT (name2);
4842 /* Extract NAME2 from the (optional) sign-changing cast. */
4843 if (gimple_assign_cast_p (def_stmt))
4845 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4846 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4847 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
4848 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
4849 name3 = gimple_assign_rhs1 (def_stmt);
4852 /* If name3 is used later, create an ASSERT_EXPR for it. */
4853 if (name3 != NULL_TREE
4854 && TREE_CODE (name3) == SSA_NAME
4855 && (cst2 == NULL_TREE
4856 || TREE_CODE (cst2) == INTEGER_CST)
4857 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
4858 && live_on_edge (e, name3)
4859 && !has_single_use (name3))
4861 tree tmp;
4863 /* Build an expression for the range test. */
4864 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
4865 if (cst2 != NULL_TREE)
4866 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4868 if (dump_file)
4870 fprintf (dump_file, "Adding assert for ");
4871 print_generic_expr (dump_file, name3, 0);
4872 fprintf (dump_file, " from ");
4873 print_generic_expr (dump_file, tmp, 0);
4874 fprintf (dump_file, "\n");
4877 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
4879 retval = true;
4882 /* If name2 is used later, create an ASSERT_EXPR for it. */
4883 if (name2 != NULL_TREE
4884 && TREE_CODE (name2) == SSA_NAME
4885 && TREE_CODE (cst2) == INTEGER_CST
4886 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4887 && live_on_edge (e, name2)
4888 && !has_single_use (name2))
4890 tree tmp;
4892 /* Build an expression for the range test. */
4893 tmp = name2;
4894 if (TREE_TYPE (name) != TREE_TYPE (name2))
4895 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
4896 if (cst2 != NULL_TREE)
4897 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4899 if (dump_file)
4901 fprintf (dump_file, "Adding assert for ");
4902 print_generic_expr (dump_file, name2, 0);
4903 fprintf (dump_file, " from ");
4904 print_generic_expr (dump_file, tmp, 0);
4905 fprintf (dump_file, "\n");
4908 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
4910 retval = true;
4914 /* In the case of post-in/decrement tests like if (i++) ... and uses
4915 of the in/decremented value on the edge the extra name we want to
4916 assert for is not on the def chain of the name compared. Instead
4917 it is in the set of use stmts. */
4918 if ((comp_code == NE_EXPR
4919 || comp_code == EQ_EXPR)
4920 && TREE_CODE (val) == INTEGER_CST)
4922 imm_use_iterator ui;
4923 gimple use_stmt;
4924 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
4926 /* Cut off to use-stmts that are in the predecessor. */
4927 if (gimple_bb (use_stmt) != e->src)
4928 continue;
4930 if (!is_gimple_assign (use_stmt))
4931 continue;
4933 enum tree_code code = gimple_assign_rhs_code (use_stmt);
4934 if (code != PLUS_EXPR
4935 && code != MINUS_EXPR)
4936 continue;
4938 tree cst = gimple_assign_rhs2 (use_stmt);
4939 if (TREE_CODE (cst) != INTEGER_CST)
4940 continue;
4942 tree name2 = gimple_assign_lhs (use_stmt);
4943 if (live_on_edge (e, name2))
4945 cst = int_const_binop (code, val, cst);
4946 register_new_assert_for (name2, name2, comp_code, cst,
4947 NULL, e, bsi);
4948 retval = true;
4953 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
4954 && TREE_CODE (val) == INTEGER_CST)
4956 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4957 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
4958 tree val2 = NULL_TREE;
4959 double_int mask = double_int_zero;
4960 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
4961 unsigned int nprec = prec;
4962 enum tree_code rhs_code = ERROR_MARK;
4964 if (is_gimple_assign (def_stmt))
4965 rhs_code = gimple_assign_rhs_code (def_stmt);
4967 /* Add asserts for NAME cmp CST and NAME being defined
4968 as NAME = (int) NAME2. */
4969 if (!TYPE_UNSIGNED (TREE_TYPE (val))
4970 && (comp_code == LE_EXPR || comp_code == LT_EXPR
4971 || comp_code == GT_EXPR || comp_code == GE_EXPR)
4972 && gimple_assign_cast_p (def_stmt))
4974 name2 = gimple_assign_rhs1 (def_stmt);
4975 if (CONVERT_EXPR_CODE_P (rhs_code)
4976 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4977 && TYPE_UNSIGNED (TREE_TYPE (name2))
4978 && prec == TYPE_PRECISION (TREE_TYPE (name2))
4979 && (comp_code == LE_EXPR || comp_code == GT_EXPR
4980 || !tree_int_cst_equal (val,
4981 TYPE_MIN_VALUE (TREE_TYPE (val))))
4982 && live_on_edge (e, name2)
4983 && !has_single_use (name2))
4985 tree tmp, cst;
4986 enum tree_code new_comp_code = comp_code;
4988 cst = fold_convert (TREE_TYPE (name2),
4989 TYPE_MIN_VALUE (TREE_TYPE (val)));
4990 /* Build an expression for the range test. */
4991 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
4992 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
4993 fold_convert (TREE_TYPE (name2), val));
4994 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4996 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
4997 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
4998 build_int_cst (TREE_TYPE (name2), 1));
5001 if (dump_file)
5003 fprintf (dump_file, "Adding assert for ");
5004 print_generic_expr (dump_file, name2, 0);
5005 fprintf (dump_file, " from ");
5006 print_generic_expr (dump_file, tmp, 0);
5007 fprintf (dump_file, "\n");
5010 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5011 e, bsi);
5013 retval = true;
5017 /* Add asserts for NAME cmp CST and NAME being defined as
5018 NAME = NAME2 >> CST2.
5020 Extract CST2 from the right shift. */
5021 if (rhs_code == RSHIFT_EXPR)
5023 name2 = gimple_assign_rhs1 (def_stmt);
5024 cst2 = gimple_assign_rhs2 (def_stmt);
5025 if (TREE_CODE (name2) == SSA_NAME
5026 && tree_fits_uhwi_p (cst2)
5027 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5028 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5029 && prec <= HOST_BITS_PER_DOUBLE_INT
5030 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5031 && live_on_edge (e, name2)
5032 && !has_single_use (name2))
5034 mask = double_int::mask (tree_to_uhwi (cst2));
5035 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5038 if (val2 != NULL_TREE
5039 && TREE_CODE (val2) == INTEGER_CST
5040 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5041 TREE_TYPE (val),
5042 val2, cst2), val))
5044 enum tree_code new_comp_code = comp_code;
5045 tree tmp, new_val;
5047 tmp = name2;
5048 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5050 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5052 tree type = build_nonstandard_integer_type (prec, 1);
5053 tmp = build1 (NOP_EXPR, type, name2);
5054 val2 = fold_convert (type, val2);
5056 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5057 new_val = double_int_to_tree (TREE_TYPE (tmp), mask);
5058 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5060 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5062 double_int minval
5063 = double_int::min_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
5064 new_val = val2;
5065 if (minval == tree_to_double_int (new_val))
5066 new_val = NULL_TREE;
5068 else
5070 double_int maxval
5071 = double_int::max_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
5072 mask |= tree_to_double_int (val2);
5073 if (mask == maxval)
5074 new_val = NULL_TREE;
5075 else
5076 new_val = double_int_to_tree (TREE_TYPE (val2), mask);
5079 if (new_val)
5081 if (dump_file)
5083 fprintf (dump_file, "Adding assert for ");
5084 print_generic_expr (dump_file, name2, 0);
5085 fprintf (dump_file, " from ");
5086 print_generic_expr (dump_file, tmp, 0);
5087 fprintf (dump_file, "\n");
5090 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5091 NULL, e, bsi);
5092 retval = true;
5096 /* Add asserts for NAME cmp CST and NAME being defined as
5097 NAME = NAME2 & CST2.
5099 Extract CST2 from the and.
5101 Also handle
5102 NAME = (unsigned) NAME2;
5103 casts where NAME's type is unsigned and has smaller precision
5104 than NAME2's type as if it was NAME = NAME2 & MASK. */
5105 names[0] = NULL_TREE;
5106 names[1] = NULL_TREE;
5107 cst2 = NULL_TREE;
5108 if (rhs_code == BIT_AND_EXPR
5109 || (CONVERT_EXPR_CODE_P (rhs_code)
5110 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5111 && TYPE_UNSIGNED (TREE_TYPE (val))
5112 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5113 > prec
5114 && !retval))
5116 name2 = gimple_assign_rhs1 (def_stmt);
5117 if (rhs_code == BIT_AND_EXPR)
5118 cst2 = gimple_assign_rhs2 (def_stmt);
5119 else
5121 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5122 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5124 if (TREE_CODE (name2) == SSA_NAME
5125 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5126 && TREE_CODE (cst2) == INTEGER_CST
5127 && !integer_zerop (cst2)
5128 && nprec <= HOST_BITS_PER_DOUBLE_INT
5129 && (nprec > 1
5130 || TYPE_UNSIGNED (TREE_TYPE (val))))
5132 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5133 if (gimple_assign_cast_p (def_stmt2))
5135 names[1] = gimple_assign_rhs1 (def_stmt2);
5136 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5137 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5138 || (TYPE_PRECISION (TREE_TYPE (name2))
5139 != TYPE_PRECISION (TREE_TYPE (names[1])))
5140 || !live_on_edge (e, names[1])
5141 || has_single_use (names[1]))
5142 names[1] = NULL_TREE;
5144 if (live_on_edge (e, name2)
5145 && !has_single_use (name2))
5146 names[0] = name2;
5149 if (names[0] || names[1])
5151 double_int minv, maxv = double_int_zero, valv, cst2v;
5152 double_int tem, sgnbit;
5153 bool valid_p = false, valn = false, cst2n = false;
5154 enum tree_code ccode = comp_code;
5156 valv = tree_to_double_int (val).zext (nprec);
5157 cst2v = tree_to_double_int (cst2).zext (nprec);
5158 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5160 valn = valv.sext (nprec).is_negative ();
5161 cst2n = cst2v.sext (nprec).is_negative ();
5163 /* If CST2 doesn't have most significant bit set,
5164 but VAL is negative, we have comparison like
5165 if ((x & 0x123) > -4) (always true). Just give up. */
5166 if (!cst2n && valn)
5167 ccode = ERROR_MARK;
5168 if (cst2n)
5169 sgnbit = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
5170 else
5171 sgnbit = double_int_zero;
5172 minv = valv & cst2v;
5173 switch (ccode)
5175 case EQ_EXPR:
5176 /* Minimum unsigned value for equality is VAL & CST2
5177 (should be equal to VAL, otherwise we probably should
5178 have folded the comparison into false) and
5179 maximum unsigned value is VAL | ~CST2. */
5180 maxv = valv | ~cst2v;
5181 maxv = maxv.zext (nprec);
5182 valid_p = true;
5183 break;
5184 case NE_EXPR:
5185 tem = valv | ~cst2v;
5186 tem = tem.zext (nprec);
5187 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5188 if (valv.is_zero ())
5190 cst2n = false;
5191 sgnbit = double_int_zero;
5192 goto gt_expr;
5194 /* If (VAL | ~CST2) is all ones, handle it as
5195 (X & CST2) < VAL. */
5196 if (tem == double_int::mask (nprec))
5198 cst2n = false;
5199 valn = false;
5200 sgnbit = double_int_zero;
5201 goto lt_expr;
5203 if (!cst2n
5204 && cst2v.sext (nprec).is_negative ())
5205 sgnbit
5206 = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
5207 if (!sgnbit.is_zero ())
5209 if (valv == sgnbit)
5211 cst2n = true;
5212 valn = true;
5213 goto gt_expr;
5215 if (tem == double_int::mask (nprec - 1))
5217 cst2n = true;
5218 goto lt_expr;
5220 if (!cst2n)
5221 sgnbit = double_int_zero;
5223 break;
5224 case GE_EXPR:
5225 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5226 is VAL and maximum unsigned value is ~0. For signed
5227 comparison, if CST2 doesn't have most significant bit
5228 set, handle it similarly. If CST2 has MSB set,
5229 the minimum is the same, and maximum is ~0U/2. */
5230 if (minv != valv)
5232 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5233 VAL. */
5234 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5235 if (minv == valv)
5236 break;
5238 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
5239 valid_p = true;
5240 break;
5241 case GT_EXPR:
5242 gt_expr:
5243 /* Find out smallest MINV where MINV > VAL
5244 && (MINV & CST2) == MINV, if any. If VAL is signed and
5245 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5246 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5247 if (minv == valv)
5248 break;
5249 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
5250 valid_p = true;
5251 break;
5252 case LE_EXPR:
5253 /* Minimum unsigned value for <= is 0 and maximum
5254 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5255 Otherwise, find smallest VAL2 where VAL2 > VAL
5256 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5257 as maximum.
5258 For signed comparison, if CST2 doesn't have most
5259 significant bit set, handle it similarly. If CST2 has
5260 MSB set, the maximum is the same and minimum is INT_MIN. */
5261 if (minv == valv)
5262 maxv = valv;
5263 else
5265 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5266 if (maxv == valv)
5267 break;
5268 maxv -= double_int_one;
5270 maxv |= ~cst2v;
5271 maxv = maxv.zext (nprec);
5272 minv = sgnbit;
5273 valid_p = true;
5274 break;
5275 case LT_EXPR:
5276 lt_expr:
5277 /* Minimum unsigned value for < is 0 and maximum
5278 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5279 Otherwise, find smallest VAL2 where VAL2 > VAL
5280 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5281 as maximum.
5282 For signed comparison, if CST2 doesn't have most
5283 significant bit set, handle it similarly. If CST2 has
5284 MSB set, the maximum is the same and minimum is INT_MIN. */
5285 if (minv == valv)
5287 if (valv == sgnbit)
5288 break;
5289 maxv = valv;
5291 else
5293 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5294 if (maxv == valv)
5295 break;
5297 maxv -= double_int_one;
5298 maxv |= ~cst2v;
5299 maxv = maxv.zext (nprec);
5300 minv = sgnbit;
5301 valid_p = true;
5302 break;
5303 default:
5304 break;
5306 if (valid_p
5307 && (maxv - minv).zext (nprec) != double_int::mask (nprec))
5309 tree tmp, new_val, type;
5310 int i;
5312 for (i = 0; i < 2; i++)
5313 if (names[i])
5315 double_int maxv2 = maxv;
5316 tmp = names[i];
5317 type = TREE_TYPE (names[i]);
5318 if (!TYPE_UNSIGNED (type))
5320 type = build_nonstandard_integer_type (nprec, 1);
5321 tmp = build1 (NOP_EXPR, type, names[i]);
5323 if (!minv.is_zero ())
5325 tmp = build2 (PLUS_EXPR, type, tmp,
5326 double_int_to_tree (type, -minv));
5327 maxv2 = maxv - minv;
5329 new_val = double_int_to_tree (type, maxv2);
5331 if (dump_file)
5333 fprintf (dump_file, "Adding assert for ");
5334 print_generic_expr (dump_file, names[i], 0);
5335 fprintf (dump_file, " from ");
5336 print_generic_expr (dump_file, tmp, 0);
5337 fprintf (dump_file, "\n");
5340 register_new_assert_for (names[i], tmp, LE_EXPR,
5341 new_val, NULL, e, bsi);
5342 retval = true;
5348 return retval;
5351 /* OP is an operand of a truth value expression which is known to have
5352 a particular value. Register any asserts for OP and for any
5353 operands in OP's defining statement.
5355 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5356 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5358 static bool
5359 register_edge_assert_for_1 (tree op, enum tree_code code,
5360 edge e, gimple_stmt_iterator bsi)
5362 bool retval = false;
5363 gimple op_def;
5364 tree val;
5365 enum tree_code rhs_code;
5367 /* We only care about SSA_NAMEs. */
5368 if (TREE_CODE (op) != SSA_NAME)
5369 return false;
5371 /* We know that OP will have a zero or nonzero value. If OP is used
5372 more than once go ahead and register an assert for OP.
5374 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5375 it will always be set for OP (because OP is used in a COND_EXPR in
5376 the subgraph). */
5377 if (!has_single_use (op))
5379 val = build_int_cst (TREE_TYPE (op), 0);
5380 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5381 retval = true;
5384 /* Now look at how OP is set. If it's set from a comparison,
5385 a truth operation or some bit operations, then we may be able
5386 to register information about the operands of that assignment. */
5387 op_def = SSA_NAME_DEF_STMT (op);
5388 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5389 return retval;
5391 rhs_code = gimple_assign_rhs_code (op_def);
5393 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5395 bool invert = (code == EQ_EXPR ? true : false);
5396 tree op0 = gimple_assign_rhs1 (op_def);
5397 tree op1 = gimple_assign_rhs2 (op_def);
5399 if (TREE_CODE (op0) == SSA_NAME)
5400 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5401 invert);
5402 if (TREE_CODE (op1) == SSA_NAME)
5403 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5404 invert);
5406 else if ((code == NE_EXPR
5407 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5408 || (code == EQ_EXPR
5409 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5411 /* Recurse on each operand. */
5412 tree op0 = gimple_assign_rhs1 (op_def);
5413 tree op1 = gimple_assign_rhs2 (op_def);
5414 if (TREE_CODE (op0) == SSA_NAME
5415 && has_single_use (op0))
5416 retval |= register_edge_assert_for_1 (op0, code, e, bsi);
5417 if (TREE_CODE (op1) == SSA_NAME
5418 && has_single_use (op1))
5419 retval |= register_edge_assert_for_1 (op1, code, e, bsi);
5421 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5422 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5424 /* Recurse, flipping CODE. */
5425 code = invert_tree_comparison (code, false);
5426 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5427 code, e, bsi);
5429 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5431 /* Recurse through the copy. */
5432 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5433 code, e, bsi);
5435 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5437 /* Recurse through the type conversion. */
5438 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5439 code, e, bsi);
5442 return retval;
5445 /* Try to register an edge assertion for SSA name NAME on edge E for
5446 the condition COND contributing to the conditional jump pointed to by SI.
5447 Return true if an assertion for NAME could be registered. */
5449 static bool
5450 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5451 enum tree_code cond_code, tree cond_op0,
5452 tree cond_op1)
5454 tree val;
5455 enum tree_code comp_code;
5456 bool retval = false;
5457 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5459 /* Do not attempt to infer anything in names that flow through
5460 abnormal edges. */
5461 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5462 return false;
5464 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5465 cond_op0, cond_op1,
5466 is_else_edge,
5467 &comp_code, &val))
5468 return false;
5470 /* Register ASSERT_EXPRs for name. */
5471 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5472 cond_op1, is_else_edge);
5475 /* If COND is effectively an equality test of an SSA_NAME against
5476 the value zero or one, then we may be able to assert values
5477 for SSA_NAMEs which flow into COND. */
5479 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5480 statement of NAME we can assert both operands of the BIT_AND_EXPR
5481 have nonzero value. */
5482 if (((comp_code == EQ_EXPR && integer_onep (val))
5483 || (comp_code == NE_EXPR && integer_zerop (val))))
5485 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5487 if (is_gimple_assign (def_stmt)
5488 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5490 tree op0 = gimple_assign_rhs1 (def_stmt);
5491 tree op1 = gimple_assign_rhs2 (def_stmt);
5492 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5493 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5497 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5498 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5499 have zero value. */
5500 if (((comp_code == EQ_EXPR && integer_zerop (val))
5501 || (comp_code == NE_EXPR && integer_onep (val))))
5503 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5505 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5506 necessarily zero value, or if type-precision is one. */
5507 if (is_gimple_assign (def_stmt)
5508 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5509 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5510 || comp_code == EQ_EXPR)))
5512 tree op0 = gimple_assign_rhs1 (def_stmt);
5513 tree op1 = gimple_assign_rhs2 (def_stmt);
5514 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5515 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5519 return retval;
5523 /* Determine whether the outgoing edges of BB should receive an
5524 ASSERT_EXPR for each of the operands of BB's LAST statement.
5525 The last statement of BB must be a COND_EXPR.
5527 If any of the sub-graphs rooted at BB have an interesting use of
5528 the predicate operands, an assert location node is added to the
5529 list of assertions for the corresponding operands. */
5531 static bool
5532 find_conditional_asserts (basic_block bb, gimple last)
5534 bool need_assert;
5535 gimple_stmt_iterator bsi;
5536 tree op;
5537 edge_iterator ei;
5538 edge e;
5539 ssa_op_iter iter;
5541 need_assert = false;
5542 bsi = gsi_for_stmt (last);
5544 /* Look for uses of the operands in each of the sub-graphs
5545 rooted at BB. We need to check each of the outgoing edges
5546 separately, so that we know what kind of ASSERT_EXPR to
5547 insert. */
5548 FOR_EACH_EDGE (e, ei, bb->succs)
5550 if (e->dest == bb)
5551 continue;
5553 /* Register the necessary assertions for each operand in the
5554 conditional predicate. */
5555 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5557 need_assert |= register_edge_assert_for (op, e, bsi,
5558 gimple_cond_code (last),
5559 gimple_cond_lhs (last),
5560 gimple_cond_rhs (last));
5564 return need_assert;
5567 struct case_info
5569 tree expr;
5570 basic_block bb;
5573 /* Compare two case labels sorting first by the destination bb index
5574 and then by the case value. */
5576 static int
5577 compare_case_labels (const void *p1, const void *p2)
5579 const struct case_info *ci1 = (const struct case_info *) p1;
5580 const struct case_info *ci2 = (const struct case_info *) p2;
5581 int idx1 = ci1->bb->index;
5582 int idx2 = ci2->bb->index;
5584 if (idx1 < idx2)
5585 return -1;
5586 else if (idx1 == idx2)
5588 /* Make sure the default label is first in a group. */
5589 if (!CASE_LOW (ci1->expr))
5590 return -1;
5591 else if (!CASE_LOW (ci2->expr))
5592 return 1;
5593 else
5594 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5595 CASE_LOW (ci2->expr));
5597 else
5598 return 1;
5601 /* Determine whether the outgoing edges of BB should receive an
5602 ASSERT_EXPR for each of the operands of BB's LAST statement.
5603 The last statement of BB must be a SWITCH_EXPR.
5605 If any of the sub-graphs rooted at BB have an interesting use of
5606 the predicate operands, an assert location node is added to the
5607 list of assertions for the corresponding operands. */
5609 static bool
5610 find_switch_asserts (basic_block bb, gimple last)
5612 bool need_assert;
5613 gimple_stmt_iterator bsi;
5614 tree op;
5615 edge e;
5616 struct case_info *ci;
5617 size_t n = gimple_switch_num_labels (last);
5618 #if GCC_VERSION >= 4000
5619 unsigned int idx;
5620 #else
5621 /* Work around GCC 3.4 bug (PR 37086). */
5622 volatile unsigned int idx;
5623 #endif
5625 need_assert = false;
5626 bsi = gsi_for_stmt (last);
5627 op = gimple_switch_index (last);
5628 if (TREE_CODE (op) != SSA_NAME)
5629 return false;
5631 /* Build a vector of case labels sorted by destination label. */
5632 ci = XNEWVEC (struct case_info, n);
5633 for (idx = 0; idx < n; ++idx)
5635 ci[idx].expr = gimple_switch_label (last, idx);
5636 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5638 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5640 for (idx = 0; idx < n; ++idx)
5642 tree min, max;
5643 tree cl = ci[idx].expr;
5644 basic_block cbb = ci[idx].bb;
5646 min = CASE_LOW (cl);
5647 max = CASE_HIGH (cl);
5649 /* If there are multiple case labels with the same destination
5650 we need to combine them to a single value range for the edge. */
5651 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5653 /* Skip labels until the last of the group. */
5654 do {
5655 ++idx;
5656 } while (idx < n && cbb == ci[idx].bb);
5657 --idx;
5659 /* Pick up the maximum of the case label range. */
5660 if (CASE_HIGH (ci[idx].expr))
5661 max = CASE_HIGH (ci[idx].expr);
5662 else
5663 max = CASE_LOW (ci[idx].expr);
5666 /* Nothing to do if the range includes the default label until we
5667 can register anti-ranges. */
5668 if (min == NULL_TREE)
5669 continue;
5671 /* Find the edge to register the assert expr on. */
5672 e = find_edge (bb, cbb);
5674 /* Register the necessary assertions for the operand in the
5675 SWITCH_EXPR. */
5676 need_assert |= register_edge_assert_for (op, e, bsi,
5677 max ? GE_EXPR : EQ_EXPR,
5679 fold_convert (TREE_TYPE (op),
5680 min));
5681 if (max)
5683 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5685 fold_convert (TREE_TYPE (op),
5686 max));
5690 XDELETEVEC (ci);
5691 return need_assert;
5695 /* Traverse all the statements in block BB looking for statements that
5696 may generate useful assertions for the SSA names in their operand.
5697 If a statement produces a useful assertion A for name N_i, then the
5698 list of assertions already generated for N_i is scanned to
5699 determine if A is actually needed.
5701 If N_i already had the assertion A at a location dominating the
5702 current location, then nothing needs to be done. Otherwise, the
5703 new location for A is recorded instead.
5705 1- For every statement S in BB, all the variables used by S are
5706 added to bitmap FOUND_IN_SUBGRAPH.
5708 2- If statement S uses an operand N in a way that exposes a known
5709 value range for N, then if N was not already generated by an
5710 ASSERT_EXPR, create a new assert location for N. For instance,
5711 if N is a pointer and the statement dereferences it, we can
5712 assume that N is not NULL.
5714 3- COND_EXPRs are a special case of #2. We can derive range
5715 information from the predicate but need to insert different
5716 ASSERT_EXPRs for each of the sub-graphs rooted at the
5717 conditional block. If the last statement of BB is a conditional
5718 expression of the form 'X op Y', then
5720 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5722 b) If the conditional is the only entry point to the sub-graph
5723 corresponding to the THEN_CLAUSE, recurse into it. On
5724 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5725 an ASSERT_EXPR is added for the corresponding variable.
5727 c) Repeat step (b) on the ELSE_CLAUSE.
5729 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5731 For instance,
5733 if (a == 9)
5734 b = a;
5735 else
5736 b = c + 1;
5738 In this case, an assertion on the THEN clause is useful to
5739 determine that 'a' is always 9 on that edge. However, an assertion
5740 on the ELSE clause would be unnecessary.
5742 4- If BB does not end in a conditional expression, then we recurse
5743 into BB's dominator children.
5745 At the end of the recursive traversal, every SSA name will have a
5746 list of locations where ASSERT_EXPRs should be added. When a new
5747 location for name N is found, it is registered by calling
5748 register_new_assert_for. That function keeps track of all the
5749 registered assertions to prevent adding unnecessary assertions.
5750 For instance, if a pointer P_4 is dereferenced more than once in a
5751 dominator tree, only the location dominating all the dereference of
5752 P_4 will receive an ASSERT_EXPR.
5754 If this function returns true, then it means that there are names
5755 for which we need to generate ASSERT_EXPRs. Those assertions are
5756 inserted by process_assert_insertions. */
5758 static bool
5759 find_assert_locations_1 (basic_block bb, sbitmap live)
5761 gimple_stmt_iterator si;
5762 gimple last;
5763 bool need_assert;
5765 need_assert = false;
5766 last = last_stmt (bb);
5768 /* If BB's last statement is a conditional statement involving integer
5769 operands, determine if we need to add ASSERT_EXPRs. */
5770 if (last
5771 && gimple_code (last) == GIMPLE_COND
5772 && !fp_predicate (last)
5773 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5774 need_assert |= find_conditional_asserts (bb, last);
5776 /* If BB's last statement is a switch statement involving integer
5777 operands, determine if we need to add ASSERT_EXPRs. */
5778 if (last
5779 && gimple_code (last) == GIMPLE_SWITCH
5780 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5781 need_assert |= find_switch_asserts (bb, last);
5783 /* Traverse all the statements in BB marking used names and looking
5784 for statements that may infer assertions for their used operands. */
5785 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5787 gimple stmt;
5788 tree op;
5789 ssa_op_iter i;
5791 stmt = gsi_stmt (si);
5793 if (is_gimple_debug (stmt))
5794 continue;
5796 /* See if we can derive an assertion for any of STMT's operands. */
5797 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5799 tree value;
5800 enum tree_code comp_code;
5802 /* If op is not live beyond this stmt, do not bother to insert
5803 asserts for it. */
5804 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5805 continue;
5807 /* If OP is used in such a way that we can infer a value
5808 range for it, and we don't find a previous assertion for
5809 it, create a new assertion location node for OP. */
5810 if (infer_value_range (stmt, op, &comp_code, &value))
5812 /* If we are able to infer a nonzero value range for OP,
5813 then walk backwards through the use-def chain to see if OP
5814 was set via a typecast.
5816 If so, then we can also infer a nonzero value range
5817 for the operand of the NOP_EXPR. */
5818 if (comp_code == NE_EXPR && integer_zerop (value))
5820 tree t = op;
5821 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5823 while (is_gimple_assign (def_stmt)
5824 && gimple_assign_rhs_code (def_stmt) == NOP_EXPR
5825 && TREE_CODE
5826 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5827 && POINTER_TYPE_P
5828 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
5830 t = gimple_assign_rhs1 (def_stmt);
5831 def_stmt = SSA_NAME_DEF_STMT (t);
5833 /* Note we want to register the assert for the
5834 operand of the NOP_EXPR after SI, not after the
5835 conversion. */
5836 if (! has_single_use (t))
5838 register_new_assert_for (t, t, comp_code, value,
5839 bb, NULL, si);
5840 need_assert = true;
5845 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
5846 need_assert = true;
5850 /* Update live. */
5851 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5852 bitmap_set_bit (live, SSA_NAME_VERSION (op));
5853 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
5854 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
5857 /* Traverse all PHI nodes in BB, updating live. */
5858 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5860 use_operand_p arg_p;
5861 ssa_op_iter i;
5862 gimple phi = gsi_stmt (si);
5863 tree res = gimple_phi_result (phi);
5865 if (virtual_operand_p (res))
5866 continue;
5868 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
5870 tree arg = USE_FROM_PTR (arg_p);
5871 if (TREE_CODE (arg) == SSA_NAME)
5872 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
5875 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
5878 return need_assert;
5881 /* Do an RPO walk over the function computing SSA name liveness
5882 on-the-fly and deciding on assert expressions to insert.
5883 Returns true if there are assert expressions to be inserted. */
5885 static bool
5886 find_assert_locations (void)
5888 int *rpo = XNEWVEC (int, last_basic_block);
5889 int *bb_rpo = XNEWVEC (int, last_basic_block);
5890 int *last_rpo = XCNEWVEC (int, last_basic_block);
5891 int rpo_cnt, i;
5892 bool need_asserts;
5894 live = XCNEWVEC (sbitmap, last_basic_block);
5895 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
5896 for (i = 0; i < rpo_cnt; ++i)
5897 bb_rpo[rpo[i]] = i;
5899 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
5900 the order we compute liveness and insert asserts we otherwise
5901 fail to insert asserts into the loop latch. */
5902 loop_p loop;
5903 FOR_EACH_LOOP (loop, 0)
5905 i = loop->latch->index;
5906 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
5907 for (gimple_stmt_iterator gsi = gsi_start_phis (loop->header);
5908 !gsi_end_p (gsi); gsi_next (&gsi))
5910 gimple phi = gsi_stmt (gsi);
5911 if (virtual_operand_p (gimple_phi_result (phi)))
5912 continue;
5913 tree arg = gimple_phi_arg_def (phi, j);
5914 if (TREE_CODE (arg) == SSA_NAME)
5916 if (live[i] == NULL)
5918 live[i] = sbitmap_alloc (num_ssa_names);
5919 bitmap_clear (live[i]);
5921 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
5926 need_asserts = false;
5927 for (i = rpo_cnt - 1; i >= 0; --i)
5929 basic_block bb = BASIC_BLOCK (rpo[i]);
5930 edge e;
5931 edge_iterator ei;
5933 if (!live[rpo[i]])
5935 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
5936 bitmap_clear (live[rpo[i]]);
5939 /* Process BB and update the live information with uses in
5940 this block. */
5941 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
5943 /* Merge liveness into the predecessor blocks and free it. */
5944 if (!bitmap_empty_p (live[rpo[i]]))
5946 int pred_rpo = i;
5947 FOR_EACH_EDGE (e, ei, bb->preds)
5949 int pred = e->src->index;
5950 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
5951 continue;
5953 if (!live[pred])
5955 live[pred] = sbitmap_alloc (num_ssa_names);
5956 bitmap_clear (live[pred]);
5958 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
5960 if (bb_rpo[pred] < pred_rpo)
5961 pred_rpo = bb_rpo[pred];
5964 /* Record the RPO number of the last visited block that needs
5965 live information from this block. */
5966 last_rpo[rpo[i]] = pred_rpo;
5968 else
5970 sbitmap_free (live[rpo[i]]);
5971 live[rpo[i]] = NULL;
5974 /* We can free all successors live bitmaps if all their
5975 predecessors have been visited already. */
5976 FOR_EACH_EDGE (e, ei, bb->succs)
5977 if (last_rpo[e->dest->index] == i
5978 && live[e->dest->index])
5980 sbitmap_free (live[e->dest->index]);
5981 live[e->dest->index] = NULL;
5985 XDELETEVEC (rpo);
5986 XDELETEVEC (bb_rpo);
5987 XDELETEVEC (last_rpo);
5988 for (i = 0; i < last_basic_block; ++i)
5989 if (live[i])
5990 sbitmap_free (live[i]);
5991 XDELETEVEC (live);
5993 return need_asserts;
5996 /* Create an ASSERT_EXPR for NAME and insert it in the location
5997 indicated by LOC. Return true if we made any edge insertions. */
5999 static bool
6000 process_assert_insertions_for (tree name, assert_locus_t loc)
6002 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6003 gimple stmt;
6004 tree cond;
6005 gimple assert_stmt;
6006 edge_iterator ei;
6007 edge e;
6009 /* If we have X <=> X do not insert an assert expr for that. */
6010 if (loc->expr == loc->val)
6011 return false;
6013 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6014 assert_stmt = build_assert_expr_for (cond, name);
6015 if (loc->e)
6017 /* We have been asked to insert the assertion on an edge. This
6018 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6019 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6020 || (gimple_code (gsi_stmt (loc->si))
6021 == GIMPLE_SWITCH));
6023 gsi_insert_on_edge (loc->e, assert_stmt);
6024 return true;
6027 /* Otherwise, we can insert right after LOC->SI iff the
6028 statement must not be the last statement in the block. */
6029 stmt = gsi_stmt (loc->si);
6030 if (!stmt_ends_bb_p (stmt))
6032 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6033 return false;
6036 /* If STMT must be the last statement in BB, we can only insert new
6037 assertions on the non-abnormal edge out of BB. Note that since
6038 STMT is not control flow, there may only be one non-abnormal edge
6039 out of BB. */
6040 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6041 if (!(e->flags & EDGE_ABNORMAL))
6043 gsi_insert_on_edge (e, assert_stmt);
6044 return true;
6047 gcc_unreachable ();
6051 /* Process all the insertions registered for every name N_i registered
6052 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6053 found in ASSERTS_FOR[i]. */
6055 static void
6056 process_assert_insertions (void)
6058 unsigned i;
6059 bitmap_iterator bi;
6060 bool update_edges_p = false;
6061 int num_asserts = 0;
6063 if (dump_file && (dump_flags & TDF_DETAILS))
6064 dump_all_asserts (dump_file);
6066 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6068 assert_locus_t loc = asserts_for[i];
6069 gcc_assert (loc);
6071 while (loc)
6073 assert_locus_t next = loc->next;
6074 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6075 free (loc);
6076 loc = next;
6077 num_asserts++;
6081 if (update_edges_p)
6082 gsi_commit_edge_inserts ();
6084 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6085 num_asserts);
6089 /* Traverse the flowgraph looking for conditional jumps to insert range
6090 expressions. These range expressions are meant to provide information
6091 to optimizations that need to reason in terms of value ranges. They
6092 will not be expanded into RTL. For instance, given:
6094 x = ...
6095 y = ...
6096 if (x < y)
6097 y = x - 2;
6098 else
6099 x = y + 3;
6101 this pass will transform the code into:
6103 x = ...
6104 y = ...
6105 if (x < y)
6107 x = ASSERT_EXPR <x, x < y>
6108 y = x - 2
6110 else
6112 y = ASSERT_EXPR <y, x <= y>
6113 x = y + 3
6116 The idea is that once copy and constant propagation have run, other
6117 optimizations will be able to determine what ranges of values can 'x'
6118 take in different paths of the code, simply by checking the reaching
6119 definition of 'x'. */
6121 static void
6122 insert_range_assertions (void)
6124 need_assert_for = BITMAP_ALLOC (NULL);
6125 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6127 calculate_dominance_info (CDI_DOMINATORS);
6129 if (find_assert_locations ())
6131 process_assert_insertions ();
6132 update_ssa (TODO_update_ssa_no_phi);
6135 if (dump_file && (dump_flags & TDF_DETAILS))
6137 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6138 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6141 free (asserts_for);
6142 BITMAP_FREE (need_assert_for);
6145 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6146 and "struct" hacks. If VRP can determine that the
6147 array subscript is a constant, check if it is outside valid
6148 range. If the array subscript is a RANGE, warn if it is
6149 non-overlapping with valid range.
6150 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6152 static void
6153 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6155 value_range_t* vr = NULL;
6156 tree low_sub, up_sub;
6157 tree low_bound, up_bound, up_bound_p1;
6158 tree base;
6160 if (TREE_NO_WARNING (ref))
6161 return;
6163 low_sub = up_sub = TREE_OPERAND (ref, 1);
6164 up_bound = array_ref_up_bound (ref);
6166 /* Can not check flexible arrays. */
6167 if (!up_bound
6168 || TREE_CODE (up_bound) != INTEGER_CST)
6169 return;
6171 /* Accesses to trailing arrays via pointers may access storage
6172 beyond the types array bounds. */
6173 base = get_base_address (ref);
6174 if (base && TREE_CODE (base) == MEM_REF)
6176 tree cref, next = NULL_TREE;
6178 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6179 return;
6181 cref = TREE_OPERAND (ref, 0);
6182 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6183 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6184 next && TREE_CODE (next) != FIELD_DECL;
6185 next = DECL_CHAIN (next))
6188 /* If this is the last field in a struct type or a field in a
6189 union type do not warn. */
6190 if (!next)
6191 return;
6194 low_bound = array_ref_low_bound (ref);
6195 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound, integer_one_node);
6197 if (TREE_CODE (low_sub) == SSA_NAME)
6199 vr = get_value_range (low_sub);
6200 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6202 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6203 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6207 if (vr && vr->type == VR_ANTI_RANGE)
6209 if (TREE_CODE (up_sub) == INTEGER_CST
6210 && tree_int_cst_lt (up_bound, up_sub)
6211 && TREE_CODE (low_sub) == INTEGER_CST
6212 && tree_int_cst_lt (low_sub, low_bound))
6214 warning_at (location, OPT_Warray_bounds,
6215 "array subscript is outside array bounds");
6216 TREE_NO_WARNING (ref) = 1;
6219 else if (TREE_CODE (up_sub) == INTEGER_CST
6220 && (ignore_off_by_one
6221 ? (tree_int_cst_lt (up_bound, up_sub)
6222 && !tree_int_cst_equal (up_bound_p1, up_sub))
6223 : (tree_int_cst_lt (up_bound, up_sub)
6224 || tree_int_cst_equal (up_bound_p1, up_sub))))
6226 if (dump_file && (dump_flags & TDF_DETAILS))
6228 fprintf (dump_file, "Array bound warning for ");
6229 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6230 fprintf (dump_file, "\n");
6232 warning_at (location, OPT_Warray_bounds,
6233 "array subscript is above array bounds");
6234 TREE_NO_WARNING (ref) = 1;
6236 else if (TREE_CODE (low_sub) == INTEGER_CST
6237 && tree_int_cst_lt (low_sub, low_bound))
6239 if (dump_file && (dump_flags & TDF_DETAILS))
6241 fprintf (dump_file, "Array bound warning for ");
6242 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6243 fprintf (dump_file, "\n");
6245 warning_at (location, OPT_Warray_bounds,
6246 "array subscript is below array bounds");
6247 TREE_NO_WARNING (ref) = 1;
6251 /* Searches if the expr T, located at LOCATION computes
6252 address of an ARRAY_REF, and call check_array_ref on it. */
6254 static void
6255 search_for_addr_array (tree t, location_t location)
6257 while (TREE_CODE (t) == SSA_NAME)
6259 gimple g = SSA_NAME_DEF_STMT (t);
6261 if (gimple_code (g) != GIMPLE_ASSIGN)
6262 return;
6264 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6265 != GIMPLE_SINGLE_RHS)
6266 return;
6268 t = gimple_assign_rhs1 (g);
6272 /* We are only interested in addresses of ARRAY_REF's. */
6273 if (TREE_CODE (t) != ADDR_EXPR)
6274 return;
6276 /* Check each ARRAY_REFs in the reference chain. */
6279 if (TREE_CODE (t) == ARRAY_REF)
6280 check_array_ref (location, t, true /*ignore_off_by_one*/);
6282 t = TREE_OPERAND (t, 0);
6284 while (handled_component_p (t));
6286 if (TREE_CODE (t) == MEM_REF
6287 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6288 && !TREE_NO_WARNING (t))
6290 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6291 tree low_bound, up_bound, el_sz;
6292 double_int idx;
6293 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6294 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6295 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6296 return;
6298 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6299 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6300 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6301 if (!low_bound
6302 || TREE_CODE (low_bound) != INTEGER_CST
6303 || !up_bound
6304 || TREE_CODE (up_bound) != INTEGER_CST
6305 || !el_sz
6306 || TREE_CODE (el_sz) != INTEGER_CST)
6307 return;
6309 idx = mem_ref_offset (t);
6310 idx = idx.sdiv (tree_to_double_int (el_sz), TRUNC_DIV_EXPR);
6311 if (idx.slt (double_int_zero))
6313 if (dump_file && (dump_flags & TDF_DETAILS))
6315 fprintf (dump_file, "Array bound warning for ");
6316 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6317 fprintf (dump_file, "\n");
6319 warning_at (location, OPT_Warray_bounds,
6320 "array subscript is below array bounds");
6321 TREE_NO_WARNING (t) = 1;
6323 else if (idx.sgt (tree_to_double_int (up_bound)
6324 - tree_to_double_int (low_bound)
6325 + double_int_one))
6327 if (dump_file && (dump_flags & TDF_DETAILS))
6329 fprintf (dump_file, "Array bound warning for ");
6330 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6331 fprintf (dump_file, "\n");
6333 warning_at (location, OPT_Warray_bounds,
6334 "array subscript is above array bounds");
6335 TREE_NO_WARNING (t) = 1;
6340 /* walk_tree() callback that checks if *TP is
6341 an ARRAY_REF inside an ADDR_EXPR (in which an array
6342 subscript one outside the valid range is allowed). Call
6343 check_array_ref for each ARRAY_REF found. The location is
6344 passed in DATA. */
6346 static tree
6347 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6349 tree t = *tp;
6350 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6351 location_t location;
6353 if (EXPR_HAS_LOCATION (t))
6354 location = EXPR_LOCATION (t);
6355 else
6357 location_t *locp = (location_t *) wi->info;
6358 location = *locp;
6361 *walk_subtree = TRUE;
6363 if (TREE_CODE (t) == ARRAY_REF)
6364 check_array_ref (location, t, false /*ignore_off_by_one*/);
6366 if (TREE_CODE (t) == MEM_REF
6367 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6368 search_for_addr_array (TREE_OPERAND (t, 0), location);
6370 if (TREE_CODE (t) == ADDR_EXPR)
6371 *walk_subtree = FALSE;
6373 return NULL_TREE;
6376 /* Walk over all statements of all reachable BBs and call check_array_bounds
6377 on them. */
6379 static void
6380 check_all_array_refs (void)
6382 basic_block bb;
6383 gimple_stmt_iterator si;
6385 FOR_EACH_BB (bb)
6387 edge_iterator ei;
6388 edge e;
6389 bool executable = false;
6391 /* Skip blocks that were found to be unreachable. */
6392 FOR_EACH_EDGE (e, ei, bb->preds)
6393 executable |= !!(e->flags & EDGE_EXECUTABLE);
6394 if (!executable)
6395 continue;
6397 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6399 gimple stmt = gsi_stmt (si);
6400 struct walk_stmt_info wi;
6401 if (!gimple_has_location (stmt))
6402 continue;
6404 if (is_gimple_call (stmt))
6406 size_t i;
6407 size_t n = gimple_call_num_args (stmt);
6408 for (i = 0; i < n; i++)
6410 tree arg = gimple_call_arg (stmt, i);
6411 search_for_addr_array (arg, gimple_location (stmt));
6414 else
6416 memset (&wi, 0, sizeof (wi));
6417 wi.info = CONST_CAST (void *, (const void *)
6418 gimple_location_ptr (stmt));
6420 walk_gimple_op (gsi_stmt (si),
6421 check_array_bounds,
6422 &wi);
6428 /* Return true if all imm uses of VAR are either in STMT, or
6429 feed (optionally through a chain of single imm uses) GIMPLE_COND
6430 in basic block COND_BB. */
6432 static bool
6433 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6435 use_operand_p use_p, use2_p;
6436 imm_use_iterator iter;
6438 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6439 if (USE_STMT (use_p) != stmt)
6441 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6442 if (is_gimple_debug (use_stmt))
6443 continue;
6444 while (is_gimple_assign (use_stmt)
6445 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6446 && single_imm_use (gimple_assign_lhs (use_stmt),
6447 &use2_p, &use_stmt2))
6448 use_stmt = use_stmt2;
6449 if (gimple_code (use_stmt) != GIMPLE_COND
6450 || gimple_bb (use_stmt) != cond_bb)
6451 return false;
6453 return true;
6456 /* Handle
6457 _4 = x_3 & 31;
6458 if (_4 != 0)
6459 goto <bb 6>;
6460 else
6461 goto <bb 7>;
6462 <bb 6>:
6463 __builtin_unreachable ();
6464 <bb 7>:
6465 x_5 = ASSERT_EXPR <x_3, ...>;
6466 If x_3 has no other immediate uses (checked by caller),
6467 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6468 from the non-zero bitmask. */
6470 static void
6471 maybe_set_nonzero_bits (basic_block bb, tree var)
6473 edge e = single_pred_edge (bb);
6474 basic_block cond_bb = e->src;
6475 gimple stmt = last_stmt (cond_bb);
6476 tree cst;
6478 if (stmt == NULL
6479 || gimple_code (stmt) != GIMPLE_COND
6480 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6481 ? EQ_EXPR : NE_EXPR)
6482 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6483 || !integer_zerop (gimple_cond_rhs (stmt)))
6484 return;
6486 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6487 if (!is_gimple_assign (stmt)
6488 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6489 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6490 return;
6491 if (gimple_assign_rhs1 (stmt) != var)
6493 gimple stmt2;
6495 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6496 return;
6497 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6498 if (!gimple_assign_cast_p (stmt2)
6499 || gimple_assign_rhs1 (stmt2) != var
6500 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6501 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6502 != TYPE_PRECISION (TREE_TYPE (var))))
6503 return;
6505 cst = gimple_assign_rhs2 (stmt);
6506 set_nonzero_bits (var, (get_nonzero_bits (var)
6507 & ~tree_to_double_int (cst)));
6510 /* Convert range assertion expressions into the implied copies and
6511 copy propagate away the copies. Doing the trivial copy propagation
6512 here avoids the need to run the full copy propagation pass after
6513 VRP.
6515 FIXME, this will eventually lead to copy propagation removing the
6516 names that had useful range information attached to them. For
6517 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6518 then N_i will have the range [3, +INF].
6520 However, by converting the assertion into the implied copy
6521 operation N_i = N_j, we will then copy-propagate N_j into the uses
6522 of N_i and lose the range information. We may want to hold on to
6523 ASSERT_EXPRs a little while longer as the ranges could be used in
6524 things like jump threading.
6526 The problem with keeping ASSERT_EXPRs around is that passes after
6527 VRP need to handle them appropriately.
6529 Another approach would be to make the range information a first
6530 class property of the SSA_NAME so that it can be queried from
6531 any pass. This is made somewhat more complex by the need for
6532 multiple ranges to be associated with one SSA_NAME. */
6534 static void
6535 remove_range_assertions (void)
6537 basic_block bb;
6538 gimple_stmt_iterator si;
6539 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6540 a basic block preceeded by GIMPLE_COND branching to it and
6541 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6542 int is_unreachable;
6544 /* Note that the BSI iterator bump happens at the bottom of the
6545 loop and no bump is necessary if we're removing the statement
6546 referenced by the current BSI. */
6547 FOR_EACH_BB (bb)
6548 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6550 gimple stmt = gsi_stmt (si);
6551 gimple use_stmt;
6553 if (is_gimple_assign (stmt)
6554 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6556 tree lhs = gimple_assign_lhs (stmt);
6557 tree rhs = gimple_assign_rhs1 (stmt);
6558 tree var;
6559 tree cond = fold (ASSERT_EXPR_COND (rhs));
6560 use_operand_p use_p;
6561 imm_use_iterator iter;
6563 gcc_assert (cond != boolean_false_node);
6565 var = ASSERT_EXPR_VAR (rhs);
6566 gcc_assert (TREE_CODE (var) == SSA_NAME);
6568 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6569 && SSA_NAME_RANGE_INFO (lhs))
6571 if (is_unreachable == -1)
6573 is_unreachable = 0;
6574 if (single_pred_p (bb)
6575 && assert_unreachable_fallthru_edge_p
6576 (single_pred_edge (bb)))
6577 is_unreachable = 1;
6579 /* Handle
6580 if (x_7 >= 10 && x_7 < 20)
6581 __builtin_unreachable ();
6582 x_8 = ASSERT_EXPR <x_7, ...>;
6583 if the only uses of x_7 are in the ASSERT_EXPR and
6584 in the condition. In that case, we can copy the
6585 range info from x_8 computed in this pass also
6586 for x_7. */
6587 if (is_unreachable
6588 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6589 single_pred (bb)))
6591 set_range_info (var, SSA_NAME_RANGE_INFO (lhs)->min,
6592 SSA_NAME_RANGE_INFO (lhs)->max);
6593 maybe_set_nonzero_bits (bb, var);
6597 /* Propagate the RHS into every use of the LHS. */
6598 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6599 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6600 SET_USE (use_p, var);
6602 /* And finally, remove the copy, it is not needed. */
6603 gsi_remove (&si, true);
6604 release_defs (stmt);
6606 else
6608 gsi_next (&si);
6609 is_unreachable = 0;
6615 /* Return true if STMT is interesting for VRP. */
6617 static bool
6618 stmt_interesting_for_vrp (gimple stmt)
6620 if (gimple_code (stmt) == GIMPLE_PHI)
6622 tree res = gimple_phi_result (stmt);
6623 return (!virtual_operand_p (res)
6624 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6625 || POINTER_TYPE_P (TREE_TYPE (res))));
6627 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6629 tree lhs = gimple_get_lhs (stmt);
6631 /* In general, assignments with virtual operands are not useful
6632 for deriving ranges, with the obvious exception of calls to
6633 builtin functions. */
6634 if (lhs && TREE_CODE (lhs) == SSA_NAME
6635 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6636 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6637 && (is_gimple_call (stmt)
6638 || !gimple_vuse (stmt)))
6639 return true;
6641 else if (gimple_code (stmt) == GIMPLE_COND
6642 || gimple_code (stmt) == GIMPLE_SWITCH)
6643 return true;
6645 return false;
6649 /* Initialize local data structures for VRP. */
6651 static void
6652 vrp_initialize (void)
6654 basic_block bb;
6656 values_propagated = false;
6657 num_vr_values = num_ssa_names;
6658 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6659 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6661 FOR_EACH_BB (bb)
6663 gimple_stmt_iterator si;
6665 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6667 gimple phi = gsi_stmt (si);
6668 if (!stmt_interesting_for_vrp (phi))
6670 tree lhs = PHI_RESULT (phi);
6671 set_value_range_to_varying (get_value_range (lhs));
6672 prop_set_simulate_again (phi, false);
6674 else
6675 prop_set_simulate_again (phi, true);
6678 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6680 gimple stmt = gsi_stmt (si);
6682 /* If the statement is a control insn, then we do not
6683 want to avoid simulating the statement once. Failure
6684 to do so means that those edges will never get added. */
6685 if (stmt_ends_bb_p (stmt))
6686 prop_set_simulate_again (stmt, true);
6687 else if (!stmt_interesting_for_vrp (stmt))
6689 ssa_op_iter i;
6690 tree def;
6691 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6692 set_value_range_to_varying (get_value_range (def));
6693 prop_set_simulate_again (stmt, false);
6695 else
6696 prop_set_simulate_again (stmt, true);
6701 /* Return the singleton value-range for NAME or NAME. */
6703 static inline tree
6704 vrp_valueize (tree name)
6706 if (TREE_CODE (name) == SSA_NAME)
6708 value_range_t *vr = get_value_range (name);
6709 if (vr->type == VR_RANGE
6710 && (vr->min == vr->max
6711 || operand_equal_p (vr->min, vr->max, 0)))
6712 return vr->min;
6714 return name;
6717 /* Visit assignment STMT. If it produces an interesting range, record
6718 the SSA name in *OUTPUT_P. */
6720 static enum ssa_prop_result
6721 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6723 tree def, lhs;
6724 ssa_op_iter iter;
6725 enum gimple_code code = gimple_code (stmt);
6726 lhs = gimple_get_lhs (stmt);
6728 /* We only keep track of ranges in integral and pointer types. */
6729 if (TREE_CODE (lhs) == SSA_NAME
6730 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6731 /* It is valid to have NULL MIN/MAX values on a type. See
6732 build_range_type. */
6733 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6734 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6735 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6737 value_range_t new_vr = VR_INITIALIZER;
6739 /* Try folding the statement to a constant first. */
6740 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6741 if (tem && !is_overflow_infinity (tem))
6742 set_value_range (&new_vr, VR_RANGE, tem, tem, NULL);
6743 /* Then dispatch to value-range extracting functions. */
6744 else if (code == GIMPLE_CALL)
6745 extract_range_basic (&new_vr, stmt);
6746 else
6747 extract_range_from_assignment (&new_vr, stmt);
6749 if (update_value_range (lhs, &new_vr))
6751 *output_p = lhs;
6753 if (dump_file && (dump_flags & TDF_DETAILS))
6755 fprintf (dump_file, "Found new range for ");
6756 print_generic_expr (dump_file, lhs, 0);
6757 fprintf (dump_file, ": ");
6758 dump_value_range (dump_file, &new_vr);
6759 fprintf (dump_file, "\n\n");
6762 if (new_vr.type == VR_VARYING)
6763 return SSA_PROP_VARYING;
6765 return SSA_PROP_INTERESTING;
6768 return SSA_PROP_NOT_INTERESTING;
6771 /* Every other statement produces no useful ranges. */
6772 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6773 set_value_range_to_varying (get_value_range (def));
6775 return SSA_PROP_VARYING;
6778 /* Helper that gets the value range of the SSA_NAME with version I
6779 or a symbolic range containing the SSA_NAME only if the value range
6780 is varying or undefined. */
6782 static inline value_range_t
6783 get_vr_for_comparison (int i)
6785 value_range_t vr = *get_value_range (ssa_name (i));
6787 /* If name N_i does not have a valid range, use N_i as its own
6788 range. This allows us to compare against names that may
6789 have N_i in their ranges. */
6790 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6792 vr.type = VR_RANGE;
6793 vr.min = ssa_name (i);
6794 vr.max = ssa_name (i);
6797 return vr;
6800 /* Compare all the value ranges for names equivalent to VAR with VAL
6801 using comparison code COMP. Return the same value returned by
6802 compare_range_with_value, including the setting of
6803 *STRICT_OVERFLOW_P. */
6805 static tree
6806 compare_name_with_value (enum tree_code comp, tree var, tree val,
6807 bool *strict_overflow_p)
6809 bitmap_iterator bi;
6810 unsigned i;
6811 bitmap e;
6812 tree retval, t;
6813 int used_strict_overflow;
6814 bool sop;
6815 value_range_t equiv_vr;
6817 /* Get the set of equivalences for VAR. */
6818 e = get_value_range (var)->equiv;
6820 /* Start at -1. Set it to 0 if we do a comparison without relying
6821 on overflow, or 1 if all comparisons rely on overflow. */
6822 used_strict_overflow = -1;
6824 /* Compare vars' value range with val. */
6825 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6826 sop = false;
6827 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
6828 if (retval)
6829 used_strict_overflow = sop ? 1 : 0;
6831 /* If the equiv set is empty we have done all work we need to do. */
6832 if (e == NULL)
6834 if (retval
6835 && used_strict_overflow > 0)
6836 *strict_overflow_p = true;
6837 return retval;
6840 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
6842 equiv_vr = get_vr_for_comparison (i);
6843 sop = false;
6844 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
6845 if (t)
6847 /* If we get different answers from different members
6848 of the equivalence set this check must be in a dead
6849 code region. Folding it to a trap representation
6850 would be correct here. For now just return don't-know. */
6851 if (retval != NULL
6852 && t != retval)
6854 retval = NULL_TREE;
6855 break;
6857 retval = t;
6859 if (!sop)
6860 used_strict_overflow = 0;
6861 else if (used_strict_overflow < 0)
6862 used_strict_overflow = 1;
6866 if (retval
6867 && used_strict_overflow > 0)
6868 *strict_overflow_p = true;
6870 return retval;
6874 /* Given a comparison code COMP and names N1 and N2, compare all the
6875 ranges equivalent to N1 against all the ranges equivalent to N2
6876 to determine the value of N1 COMP N2. Return the same value
6877 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6878 whether we relied on an overflow infinity in the comparison. */
6881 static tree
6882 compare_names (enum tree_code comp, tree n1, tree n2,
6883 bool *strict_overflow_p)
6885 tree t, retval;
6886 bitmap e1, e2;
6887 bitmap_iterator bi1, bi2;
6888 unsigned i1, i2;
6889 int used_strict_overflow;
6890 static bitmap_obstack *s_obstack = NULL;
6891 static bitmap s_e1 = NULL, s_e2 = NULL;
6893 /* Compare the ranges of every name equivalent to N1 against the
6894 ranges of every name equivalent to N2. */
6895 e1 = get_value_range (n1)->equiv;
6896 e2 = get_value_range (n2)->equiv;
6898 /* Use the fake bitmaps if e1 or e2 are not available. */
6899 if (s_obstack == NULL)
6901 s_obstack = XNEW (bitmap_obstack);
6902 bitmap_obstack_initialize (s_obstack);
6903 s_e1 = BITMAP_ALLOC (s_obstack);
6904 s_e2 = BITMAP_ALLOC (s_obstack);
6906 if (e1 == NULL)
6907 e1 = s_e1;
6908 if (e2 == NULL)
6909 e2 = s_e2;
6911 /* Add N1 and N2 to their own set of equivalences to avoid
6912 duplicating the body of the loop just to check N1 and N2
6913 ranges. */
6914 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
6915 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
6917 /* If the equivalence sets have a common intersection, then the two
6918 names can be compared without checking their ranges. */
6919 if (bitmap_intersect_p (e1, e2))
6921 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6922 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6924 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
6925 ? boolean_true_node
6926 : boolean_false_node;
6929 /* Start at -1. Set it to 0 if we do a comparison without relying
6930 on overflow, or 1 if all comparisons rely on overflow. */
6931 used_strict_overflow = -1;
6933 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6934 N2 to their own set of equivalences to avoid duplicating the body
6935 of the loop just to check N1 and N2 ranges. */
6936 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
6938 value_range_t vr1 = get_vr_for_comparison (i1);
6940 t = retval = NULL_TREE;
6941 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
6943 bool sop = false;
6945 value_range_t vr2 = get_vr_for_comparison (i2);
6947 t = compare_ranges (comp, &vr1, &vr2, &sop);
6948 if (t)
6950 /* If we get different answers from different members
6951 of the equivalence set this check must be in a dead
6952 code region. Folding it to a trap representation
6953 would be correct here. For now just return don't-know. */
6954 if (retval != NULL
6955 && t != retval)
6957 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6958 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6959 return NULL_TREE;
6961 retval = t;
6963 if (!sop)
6964 used_strict_overflow = 0;
6965 else if (used_strict_overflow < 0)
6966 used_strict_overflow = 1;
6970 if (retval)
6972 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6973 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6974 if (used_strict_overflow > 0)
6975 *strict_overflow_p = true;
6976 return retval;
6980 /* None of the equivalent ranges are useful in computing this
6981 comparison. */
6982 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6983 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6984 return NULL_TREE;
6987 /* Helper function for vrp_evaluate_conditional_warnv. */
6989 static tree
6990 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
6991 tree op0, tree op1,
6992 bool * strict_overflow_p)
6994 value_range_t *vr0, *vr1;
6996 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
6997 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
6999 if (vr0 && vr1)
7000 return compare_ranges (code, vr0, vr1, strict_overflow_p);
7001 else if (vr0 && vr1 == NULL)
7002 return compare_range_with_value (code, vr0, op1, strict_overflow_p);
7003 else if (vr0 == NULL && vr1)
7004 return (compare_range_with_value
7005 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7006 return NULL;
7009 /* Helper function for vrp_evaluate_conditional_warnv. */
7011 static tree
7012 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7013 tree op1, bool use_equiv_p,
7014 bool *strict_overflow_p, bool *only_ranges)
7016 tree ret;
7017 if (only_ranges)
7018 *only_ranges = true;
7020 /* We only deal with integral and pointer types. */
7021 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7022 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7023 return NULL_TREE;
7025 if (use_equiv_p)
7027 if (only_ranges
7028 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7029 (code, op0, op1, strict_overflow_p)))
7030 return ret;
7031 *only_ranges = false;
7032 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7033 return compare_names (code, op0, op1, strict_overflow_p);
7034 else if (TREE_CODE (op0) == SSA_NAME)
7035 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7036 else if (TREE_CODE (op1) == SSA_NAME)
7037 return (compare_name_with_value
7038 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7040 else
7041 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7042 strict_overflow_p);
7043 return NULL_TREE;
7046 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7047 information. Return NULL if the conditional can not be evaluated.
7048 The ranges of all the names equivalent with the operands in COND
7049 will be used when trying to compute the value. If the result is
7050 based on undefined signed overflow, issue a warning if
7051 appropriate. */
7053 static tree
7054 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7056 bool sop;
7057 tree ret;
7058 bool only_ranges;
7060 /* Some passes and foldings leak constants with overflow flag set
7061 into the IL. Avoid doing wrong things with these and bail out. */
7062 if ((TREE_CODE (op0) == INTEGER_CST
7063 && TREE_OVERFLOW (op0))
7064 || (TREE_CODE (op1) == INTEGER_CST
7065 && TREE_OVERFLOW (op1)))
7066 return NULL_TREE;
7068 sop = false;
7069 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7070 &only_ranges);
7072 if (ret && sop)
7074 enum warn_strict_overflow_code wc;
7075 const char* warnmsg;
7077 if (is_gimple_min_invariant (ret))
7079 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7080 warnmsg = G_("assuming signed overflow does not occur when "
7081 "simplifying conditional to constant");
7083 else
7085 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7086 warnmsg = G_("assuming signed overflow does not occur when "
7087 "simplifying conditional");
7090 if (issue_strict_overflow_warning (wc))
7092 location_t location;
7094 if (!gimple_has_location (stmt))
7095 location = input_location;
7096 else
7097 location = gimple_location (stmt);
7098 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7102 if (warn_type_limits
7103 && ret && only_ranges
7104 && TREE_CODE_CLASS (code) == tcc_comparison
7105 && TREE_CODE (op0) == SSA_NAME)
7107 /* If the comparison is being folded and the operand on the LHS
7108 is being compared against a constant value that is outside of
7109 the natural range of OP0's type, then the predicate will
7110 always fold regardless of the value of OP0. If -Wtype-limits
7111 was specified, emit a warning. */
7112 tree type = TREE_TYPE (op0);
7113 value_range_t *vr0 = get_value_range (op0);
7115 if (vr0->type != VR_VARYING
7116 && INTEGRAL_TYPE_P (type)
7117 && vrp_val_is_min (vr0->min)
7118 && vrp_val_is_max (vr0->max)
7119 && is_gimple_min_invariant (op1))
7121 location_t location;
7123 if (!gimple_has_location (stmt))
7124 location = input_location;
7125 else
7126 location = gimple_location (stmt);
7128 warning_at (location, OPT_Wtype_limits,
7129 integer_zerop (ret)
7130 ? G_("comparison always false "
7131 "due to limited range of data type")
7132 : G_("comparison always true "
7133 "due to limited range of data type"));
7137 return ret;
7141 /* Visit conditional statement STMT. If we can determine which edge
7142 will be taken out of STMT's basic block, record it in
7143 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7144 SSA_PROP_VARYING. */
7146 static enum ssa_prop_result
7147 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
7149 tree val;
7150 bool sop;
7152 *taken_edge_p = NULL;
7154 if (dump_file && (dump_flags & TDF_DETAILS))
7156 tree use;
7157 ssa_op_iter i;
7159 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7160 print_gimple_stmt (dump_file, stmt, 0, 0);
7161 fprintf (dump_file, "\nWith known ranges\n");
7163 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7165 fprintf (dump_file, "\t");
7166 print_generic_expr (dump_file, use, 0);
7167 fprintf (dump_file, ": ");
7168 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7171 fprintf (dump_file, "\n");
7174 /* Compute the value of the predicate COND by checking the known
7175 ranges of each of its operands.
7177 Note that we cannot evaluate all the equivalent ranges here
7178 because those ranges may not yet be final and with the current
7179 propagation strategy, we cannot determine when the value ranges
7180 of the names in the equivalence set have changed.
7182 For instance, given the following code fragment
7184 i_5 = PHI <8, i_13>
7186 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7187 if (i_14 == 1)
7190 Assume that on the first visit to i_14, i_5 has the temporary
7191 range [8, 8] because the second argument to the PHI function is
7192 not yet executable. We derive the range ~[0, 0] for i_14 and the
7193 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7194 the first time, since i_14 is equivalent to the range [8, 8], we
7195 determine that the predicate is always false.
7197 On the next round of propagation, i_13 is determined to be
7198 VARYING, which causes i_5 to drop down to VARYING. So, another
7199 visit to i_14 is scheduled. In this second visit, we compute the
7200 exact same range and equivalence set for i_14, namely ~[0, 0] and
7201 { i_5 }. But we did not have the previous range for i_5
7202 registered, so vrp_visit_assignment thinks that the range for
7203 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7204 is not visited again, which stops propagation from visiting
7205 statements in the THEN clause of that if().
7207 To properly fix this we would need to keep the previous range
7208 value for the names in the equivalence set. This way we would've
7209 discovered that from one visit to the other i_5 changed from
7210 range [8, 8] to VR_VARYING.
7212 However, fixing this apparent limitation may not be worth the
7213 additional checking. Testing on several code bases (GCC, DLV,
7214 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7215 4 more predicates folded in SPEC. */
7216 sop = false;
7218 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7219 gimple_cond_lhs (stmt),
7220 gimple_cond_rhs (stmt),
7221 false, &sop, NULL);
7222 if (val)
7224 if (!sop)
7225 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7226 else
7228 if (dump_file && (dump_flags & TDF_DETAILS))
7229 fprintf (dump_file,
7230 "\nIgnoring predicate evaluation because "
7231 "it assumes that signed overflow is undefined");
7232 val = NULL_TREE;
7236 if (dump_file && (dump_flags & TDF_DETAILS))
7238 fprintf (dump_file, "\nPredicate evaluates to: ");
7239 if (val == NULL_TREE)
7240 fprintf (dump_file, "DON'T KNOW\n");
7241 else
7242 print_generic_stmt (dump_file, val, 0);
7245 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7248 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7249 that includes the value VAL. The search is restricted to the range
7250 [START_IDX, n - 1] where n is the size of VEC.
7252 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7253 returned.
7255 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7256 it is placed in IDX and false is returned.
7258 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7259 returned. */
7261 static bool
7262 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
7264 size_t n = gimple_switch_num_labels (stmt);
7265 size_t low, high;
7267 /* Find case label for minimum of the value range or the next one.
7268 At each iteration we are searching in [low, high - 1]. */
7270 for (low = start_idx, high = n; high != low; )
7272 tree t;
7273 int cmp;
7274 /* Note that i != high, so we never ask for n. */
7275 size_t i = (high + low) / 2;
7276 t = gimple_switch_label (stmt, i);
7278 /* Cache the result of comparing CASE_LOW and val. */
7279 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7281 if (cmp == 0)
7283 /* Ranges cannot be empty. */
7284 *idx = i;
7285 return true;
7287 else if (cmp > 0)
7288 high = i;
7289 else
7291 low = i + 1;
7292 if (CASE_HIGH (t) != NULL
7293 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7295 *idx = i;
7296 return true;
7301 *idx = high;
7302 return false;
7305 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7306 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7307 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7308 then MAX_IDX < MIN_IDX.
7309 Returns true if the default label is not needed. */
7311 static bool
7312 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
7313 size_t *max_idx)
7315 size_t i, j;
7316 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7317 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7319 if (i == j
7320 && min_take_default
7321 && max_take_default)
7323 /* Only the default case label reached.
7324 Return an empty range. */
7325 *min_idx = 1;
7326 *max_idx = 0;
7327 return false;
7329 else
7331 bool take_default = min_take_default || max_take_default;
7332 tree low, high;
7333 size_t k;
7335 if (max_take_default)
7336 j--;
7338 /* If the case label range is continuous, we do not need
7339 the default case label. Verify that. */
7340 high = CASE_LOW (gimple_switch_label (stmt, i));
7341 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7342 high = CASE_HIGH (gimple_switch_label (stmt, i));
7343 for (k = i + 1; k <= j; ++k)
7345 low = CASE_LOW (gimple_switch_label (stmt, k));
7346 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7348 take_default = true;
7349 break;
7351 high = low;
7352 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7353 high = CASE_HIGH (gimple_switch_label (stmt, k));
7356 *min_idx = i;
7357 *max_idx = j;
7358 return !take_default;
7362 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7363 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7364 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7365 Returns true if the default label is not needed. */
7367 static bool
7368 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
7369 size_t *max_idx1, size_t *min_idx2,
7370 size_t *max_idx2)
7372 size_t i, j, k, l;
7373 unsigned int n = gimple_switch_num_labels (stmt);
7374 bool take_default;
7375 tree case_low, case_high;
7376 tree min = vr->min, max = vr->max;
7378 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7380 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7382 /* Set second range to emtpy. */
7383 *min_idx2 = 1;
7384 *max_idx2 = 0;
7386 if (vr->type == VR_RANGE)
7388 *min_idx1 = i;
7389 *max_idx1 = j;
7390 return !take_default;
7393 /* Set first range to all case labels. */
7394 *min_idx1 = 1;
7395 *max_idx1 = n - 1;
7397 if (i > j)
7398 return false;
7400 /* Make sure all the values of case labels [i , j] are contained in
7401 range [MIN, MAX]. */
7402 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7403 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7404 if (tree_int_cst_compare (case_low, min) < 0)
7405 i += 1;
7406 if (case_high != NULL_TREE
7407 && tree_int_cst_compare (max, case_high) < 0)
7408 j -= 1;
7410 if (i > j)
7411 return false;
7413 /* If the range spans case labels [i, j], the corresponding anti-range spans
7414 the labels [1, i - 1] and [j + 1, n - 1]. */
7415 k = j + 1;
7416 l = n - 1;
7417 if (k > l)
7419 k = 1;
7420 l = 0;
7423 j = i - 1;
7424 i = 1;
7425 if (i > j)
7427 i = k;
7428 j = l;
7429 k = 1;
7430 l = 0;
7433 *min_idx1 = i;
7434 *max_idx1 = j;
7435 *min_idx2 = k;
7436 *max_idx2 = l;
7437 return false;
7440 /* Visit switch statement STMT. If we can determine which edge
7441 will be taken out of STMT's basic block, record it in
7442 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7443 SSA_PROP_VARYING. */
7445 static enum ssa_prop_result
7446 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7448 tree op, val;
7449 value_range_t *vr;
7450 size_t i = 0, j = 0, k, l;
7451 bool take_default;
7453 *taken_edge_p = NULL;
7454 op = gimple_switch_index (stmt);
7455 if (TREE_CODE (op) != SSA_NAME)
7456 return SSA_PROP_VARYING;
7458 vr = get_value_range (op);
7459 if (dump_file && (dump_flags & TDF_DETAILS))
7461 fprintf (dump_file, "\nVisiting switch expression with operand ");
7462 print_generic_expr (dump_file, op, 0);
7463 fprintf (dump_file, " with known range ");
7464 dump_value_range (dump_file, vr);
7465 fprintf (dump_file, "\n");
7468 if ((vr->type != VR_RANGE
7469 && vr->type != VR_ANTI_RANGE)
7470 || symbolic_range_p (vr))
7471 return SSA_PROP_VARYING;
7473 /* Find the single edge that is taken from the switch expression. */
7474 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7476 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7477 label */
7478 if (j < i)
7480 gcc_assert (take_default);
7481 val = gimple_switch_default_label (stmt);
7483 else
7485 /* Check if labels with index i to j and maybe the default label
7486 are all reaching the same label. */
7488 val = gimple_switch_label (stmt, i);
7489 if (take_default
7490 && CASE_LABEL (gimple_switch_default_label (stmt))
7491 != CASE_LABEL (val))
7493 if (dump_file && (dump_flags & TDF_DETAILS))
7494 fprintf (dump_file, " not a single destination for this "
7495 "range\n");
7496 return SSA_PROP_VARYING;
7498 for (++i; i <= j; ++i)
7500 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7502 if (dump_file && (dump_flags & TDF_DETAILS))
7503 fprintf (dump_file, " not a single destination for this "
7504 "range\n");
7505 return SSA_PROP_VARYING;
7508 for (; k <= l; ++k)
7510 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7512 if (dump_file && (dump_flags & TDF_DETAILS))
7513 fprintf (dump_file, " not a single destination for this "
7514 "range\n");
7515 return SSA_PROP_VARYING;
7520 *taken_edge_p = find_edge (gimple_bb (stmt),
7521 label_to_block (CASE_LABEL (val)));
7523 if (dump_file && (dump_flags & TDF_DETAILS))
7525 fprintf (dump_file, " will take edge to ");
7526 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7529 return SSA_PROP_INTERESTING;
7533 /* Evaluate statement STMT. If the statement produces a useful range,
7534 return SSA_PROP_INTERESTING and record the SSA name with the
7535 interesting range into *OUTPUT_P.
7537 If STMT is a conditional branch and we can determine its truth
7538 value, the taken edge is recorded in *TAKEN_EDGE_P.
7540 If STMT produces a varying value, return SSA_PROP_VARYING. */
7542 static enum ssa_prop_result
7543 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7545 tree def;
7546 ssa_op_iter iter;
7548 if (dump_file && (dump_flags & TDF_DETAILS))
7550 fprintf (dump_file, "\nVisiting statement:\n");
7551 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7552 fprintf (dump_file, "\n");
7555 if (!stmt_interesting_for_vrp (stmt))
7556 gcc_assert (stmt_ends_bb_p (stmt));
7557 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7558 return vrp_visit_assignment_or_call (stmt, output_p);
7559 else if (gimple_code (stmt) == GIMPLE_COND)
7560 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7561 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7562 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7564 /* All other statements produce nothing of interest for VRP, so mark
7565 their outputs varying and prevent further simulation. */
7566 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7567 set_value_range_to_varying (get_value_range (def));
7569 return SSA_PROP_VARYING;
7572 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7573 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7574 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7575 possible such range. The resulting range is not canonicalized. */
7577 static void
7578 union_ranges (enum value_range_type *vr0type,
7579 tree *vr0min, tree *vr0max,
7580 enum value_range_type vr1type,
7581 tree vr1min, tree vr1max)
7583 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7584 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7586 /* [] is vr0, () is vr1 in the following classification comments. */
7587 if (mineq && maxeq)
7589 /* [( )] */
7590 if (*vr0type == vr1type)
7591 /* Nothing to do for equal ranges. */
7593 else if ((*vr0type == VR_RANGE
7594 && vr1type == VR_ANTI_RANGE)
7595 || (*vr0type == VR_ANTI_RANGE
7596 && vr1type == VR_RANGE))
7598 /* For anti-range with range union the result is varying. */
7599 goto give_up;
7601 else
7602 gcc_unreachable ();
7604 else if (operand_less_p (*vr0max, vr1min) == 1
7605 || operand_less_p (vr1max, *vr0min) == 1)
7607 /* [ ] ( ) or ( ) [ ]
7608 If the ranges have an empty intersection, result of the union
7609 operation is the anti-range or if both are anti-ranges
7610 it covers all. */
7611 if (*vr0type == VR_ANTI_RANGE
7612 && vr1type == VR_ANTI_RANGE)
7613 goto give_up;
7614 else if (*vr0type == VR_ANTI_RANGE
7615 && vr1type == VR_RANGE)
7617 else if (*vr0type == VR_RANGE
7618 && vr1type == VR_ANTI_RANGE)
7620 *vr0type = vr1type;
7621 *vr0min = vr1min;
7622 *vr0max = vr1max;
7624 else if (*vr0type == VR_RANGE
7625 && vr1type == VR_RANGE)
7627 /* The result is the convex hull of both ranges. */
7628 if (operand_less_p (*vr0max, vr1min) == 1)
7630 /* If the result can be an anti-range, create one. */
7631 if (TREE_CODE (*vr0max) == INTEGER_CST
7632 && TREE_CODE (vr1min) == INTEGER_CST
7633 && vrp_val_is_min (*vr0min)
7634 && vrp_val_is_max (vr1max))
7636 tree min = int_const_binop (PLUS_EXPR,
7637 *vr0max, integer_one_node);
7638 tree max = int_const_binop (MINUS_EXPR,
7639 vr1min, integer_one_node);
7640 if (!operand_less_p (max, min))
7642 *vr0type = VR_ANTI_RANGE;
7643 *vr0min = min;
7644 *vr0max = max;
7646 else
7647 *vr0max = vr1max;
7649 else
7650 *vr0max = vr1max;
7652 else
7654 /* If the result can be an anti-range, create one. */
7655 if (TREE_CODE (vr1max) == INTEGER_CST
7656 && TREE_CODE (*vr0min) == INTEGER_CST
7657 && vrp_val_is_min (vr1min)
7658 && vrp_val_is_max (*vr0max))
7660 tree min = int_const_binop (PLUS_EXPR,
7661 vr1max, integer_one_node);
7662 tree max = int_const_binop (MINUS_EXPR,
7663 *vr0min, integer_one_node);
7664 if (!operand_less_p (max, min))
7666 *vr0type = VR_ANTI_RANGE;
7667 *vr0min = min;
7668 *vr0max = max;
7670 else
7671 *vr0min = vr1min;
7673 else
7674 *vr0min = vr1min;
7677 else
7678 gcc_unreachable ();
7680 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7681 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7683 /* [ ( ) ] or [( ) ] or [ ( )] */
7684 if (*vr0type == VR_RANGE
7685 && vr1type == VR_RANGE)
7687 else if (*vr0type == VR_ANTI_RANGE
7688 && vr1type == VR_ANTI_RANGE)
7690 *vr0type = vr1type;
7691 *vr0min = vr1min;
7692 *vr0max = vr1max;
7694 else if (*vr0type == VR_ANTI_RANGE
7695 && vr1type == VR_RANGE)
7697 /* Arbitrarily choose the right or left gap. */
7698 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7699 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7700 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7701 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7702 else
7703 goto give_up;
7705 else if (*vr0type == VR_RANGE
7706 && vr1type == VR_ANTI_RANGE)
7707 /* The result covers everything. */
7708 goto give_up;
7709 else
7710 gcc_unreachable ();
7712 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7713 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7715 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7716 if (*vr0type == VR_RANGE
7717 && vr1type == VR_RANGE)
7719 *vr0type = vr1type;
7720 *vr0min = vr1min;
7721 *vr0max = vr1max;
7723 else if (*vr0type == VR_ANTI_RANGE
7724 && vr1type == VR_ANTI_RANGE)
7726 else if (*vr0type == VR_RANGE
7727 && vr1type == VR_ANTI_RANGE)
7729 *vr0type = VR_ANTI_RANGE;
7730 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7732 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7733 *vr0min = vr1min;
7735 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7737 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7738 *vr0max = vr1max;
7740 else
7741 goto give_up;
7743 else if (*vr0type == VR_ANTI_RANGE
7744 && vr1type == VR_RANGE)
7745 /* The result covers everything. */
7746 goto give_up;
7747 else
7748 gcc_unreachable ();
7750 else if ((operand_less_p (vr1min, *vr0max) == 1
7751 || operand_equal_p (vr1min, *vr0max, 0))
7752 && operand_less_p (*vr0min, vr1min) == 1)
7754 /* [ ( ] ) or [ ]( ) */
7755 if (*vr0type == VR_RANGE
7756 && vr1type == VR_RANGE)
7757 *vr0max = vr1max;
7758 else if (*vr0type == VR_ANTI_RANGE
7759 && vr1type == VR_ANTI_RANGE)
7760 *vr0min = vr1min;
7761 else if (*vr0type == VR_ANTI_RANGE
7762 && vr1type == VR_RANGE)
7764 if (TREE_CODE (vr1min) == INTEGER_CST)
7765 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7766 else
7767 goto give_up;
7769 else if (*vr0type == VR_RANGE
7770 && vr1type == VR_ANTI_RANGE)
7772 if (TREE_CODE (*vr0max) == INTEGER_CST)
7774 *vr0type = vr1type;
7775 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7776 *vr0max = vr1max;
7778 else
7779 goto give_up;
7781 else
7782 gcc_unreachable ();
7784 else if ((operand_less_p (*vr0min, vr1max) == 1
7785 || operand_equal_p (*vr0min, vr1max, 0))
7786 && operand_less_p (vr1min, *vr0min) == 1)
7788 /* ( [ ) ] or ( )[ ] */
7789 if (*vr0type == VR_RANGE
7790 && vr1type == VR_RANGE)
7791 *vr0min = vr1min;
7792 else if (*vr0type == VR_ANTI_RANGE
7793 && vr1type == VR_ANTI_RANGE)
7794 *vr0max = vr1max;
7795 else if (*vr0type == VR_ANTI_RANGE
7796 && vr1type == VR_RANGE)
7798 if (TREE_CODE (vr1max) == INTEGER_CST)
7799 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7800 else
7801 goto give_up;
7803 else if (*vr0type == VR_RANGE
7804 && vr1type == VR_ANTI_RANGE)
7806 if (TREE_CODE (*vr0min) == INTEGER_CST)
7808 *vr0type = vr1type;
7809 *vr0min = vr1min;
7810 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7812 else
7813 goto give_up;
7815 else
7816 gcc_unreachable ();
7818 else
7819 goto give_up;
7821 return;
7823 give_up:
7824 *vr0type = VR_VARYING;
7825 *vr0min = NULL_TREE;
7826 *vr0max = NULL_TREE;
7829 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7830 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7831 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7832 possible such range. The resulting range is not canonicalized. */
7834 static void
7835 intersect_ranges (enum value_range_type *vr0type,
7836 tree *vr0min, tree *vr0max,
7837 enum value_range_type vr1type,
7838 tree vr1min, tree vr1max)
7840 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7841 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7843 /* [] is vr0, () is vr1 in the following classification comments. */
7844 if (mineq && maxeq)
7846 /* [( )] */
7847 if (*vr0type == vr1type)
7848 /* Nothing to do for equal ranges. */
7850 else if ((*vr0type == VR_RANGE
7851 && vr1type == VR_ANTI_RANGE)
7852 || (*vr0type == VR_ANTI_RANGE
7853 && vr1type == VR_RANGE))
7855 /* For anti-range with range intersection the result is empty. */
7856 *vr0type = VR_UNDEFINED;
7857 *vr0min = NULL_TREE;
7858 *vr0max = NULL_TREE;
7860 else
7861 gcc_unreachable ();
7863 else if (operand_less_p (*vr0max, vr1min) == 1
7864 || operand_less_p (vr1max, *vr0min) == 1)
7866 /* [ ] ( ) or ( ) [ ]
7867 If the ranges have an empty intersection, the result of the
7868 intersect operation is the range for intersecting an
7869 anti-range with a range or empty when intersecting two ranges. */
7870 if (*vr0type == VR_RANGE
7871 && vr1type == VR_ANTI_RANGE)
7873 else if (*vr0type == VR_ANTI_RANGE
7874 && vr1type == VR_RANGE)
7876 *vr0type = vr1type;
7877 *vr0min = vr1min;
7878 *vr0max = vr1max;
7880 else if (*vr0type == VR_RANGE
7881 && vr1type == VR_RANGE)
7883 *vr0type = VR_UNDEFINED;
7884 *vr0min = NULL_TREE;
7885 *vr0max = NULL_TREE;
7887 else if (*vr0type == VR_ANTI_RANGE
7888 && vr1type == VR_ANTI_RANGE)
7890 /* If the anti-ranges are adjacent to each other merge them. */
7891 if (TREE_CODE (*vr0max) == INTEGER_CST
7892 && TREE_CODE (vr1min) == INTEGER_CST
7893 && operand_less_p (*vr0max, vr1min) == 1
7894 && integer_onep (int_const_binop (MINUS_EXPR,
7895 vr1min, *vr0max)))
7896 *vr0max = vr1max;
7897 else if (TREE_CODE (vr1max) == INTEGER_CST
7898 && TREE_CODE (*vr0min) == INTEGER_CST
7899 && operand_less_p (vr1max, *vr0min) == 1
7900 && integer_onep (int_const_binop (MINUS_EXPR,
7901 *vr0min, vr1max)))
7902 *vr0min = vr1min;
7903 /* Else arbitrarily take VR0. */
7906 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7907 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7909 /* [ ( ) ] or [( ) ] or [ ( )] */
7910 if (*vr0type == VR_RANGE
7911 && vr1type == VR_RANGE)
7913 /* If both are ranges the result is the inner one. */
7914 *vr0type = vr1type;
7915 *vr0min = vr1min;
7916 *vr0max = vr1max;
7918 else if (*vr0type == VR_RANGE
7919 && vr1type == VR_ANTI_RANGE)
7921 /* Choose the right gap if the left one is empty. */
7922 if (mineq)
7924 if (TREE_CODE (vr1max) == INTEGER_CST)
7925 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7926 else
7927 *vr0min = vr1max;
7929 /* Choose the left gap if the right one is empty. */
7930 else if (maxeq)
7932 if (TREE_CODE (vr1min) == INTEGER_CST)
7933 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7934 integer_one_node);
7935 else
7936 *vr0max = vr1min;
7938 /* Choose the anti-range if the range is effectively varying. */
7939 else if (vrp_val_is_min (*vr0min)
7940 && vrp_val_is_max (*vr0max))
7942 *vr0type = vr1type;
7943 *vr0min = vr1min;
7944 *vr0max = vr1max;
7946 /* Else choose the range. */
7948 else if (*vr0type == VR_ANTI_RANGE
7949 && vr1type == VR_ANTI_RANGE)
7950 /* If both are anti-ranges the result is the outer one. */
7952 else if (*vr0type == VR_ANTI_RANGE
7953 && vr1type == VR_RANGE)
7955 /* The intersection is empty. */
7956 *vr0type = VR_UNDEFINED;
7957 *vr0min = NULL_TREE;
7958 *vr0max = NULL_TREE;
7960 else
7961 gcc_unreachable ();
7963 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7964 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7966 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7967 if (*vr0type == VR_RANGE
7968 && vr1type == VR_RANGE)
7969 /* Choose the inner range. */
7971 else if (*vr0type == VR_ANTI_RANGE
7972 && vr1type == VR_RANGE)
7974 /* Choose the right gap if the left is empty. */
7975 if (mineq)
7977 *vr0type = VR_RANGE;
7978 if (TREE_CODE (*vr0max) == INTEGER_CST)
7979 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7980 integer_one_node);
7981 else
7982 *vr0min = *vr0max;
7983 *vr0max = vr1max;
7985 /* Choose the left gap if the right is empty. */
7986 else if (maxeq)
7988 *vr0type = VR_RANGE;
7989 if (TREE_CODE (*vr0min) == INTEGER_CST)
7990 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7991 integer_one_node);
7992 else
7993 *vr0max = *vr0min;
7994 *vr0min = vr1min;
7996 /* Choose the anti-range if the range is effectively varying. */
7997 else if (vrp_val_is_min (vr1min)
7998 && vrp_val_is_max (vr1max))
8000 /* Else choose the range. */
8001 else
8003 *vr0type = vr1type;
8004 *vr0min = vr1min;
8005 *vr0max = vr1max;
8008 else if (*vr0type == VR_ANTI_RANGE
8009 && vr1type == VR_ANTI_RANGE)
8011 /* If both are anti-ranges the result is the outer one. */
8012 *vr0type = vr1type;
8013 *vr0min = vr1min;
8014 *vr0max = vr1max;
8016 else if (vr1type == VR_ANTI_RANGE
8017 && *vr0type == VR_RANGE)
8019 /* The intersection is empty. */
8020 *vr0type = VR_UNDEFINED;
8021 *vr0min = NULL_TREE;
8022 *vr0max = NULL_TREE;
8024 else
8025 gcc_unreachable ();
8027 else if ((operand_less_p (vr1min, *vr0max) == 1
8028 || operand_equal_p (vr1min, *vr0max, 0))
8029 && operand_less_p (*vr0min, vr1min) == 1)
8031 /* [ ( ] ) or [ ]( ) */
8032 if (*vr0type == VR_ANTI_RANGE
8033 && vr1type == VR_ANTI_RANGE)
8034 *vr0max = vr1max;
8035 else if (*vr0type == VR_RANGE
8036 && vr1type == VR_RANGE)
8037 *vr0min = vr1min;
8038 else if (*vr0type == VR_RANGE
8039 && vr1type == VR_ANTI_RANGE)
8041 if (TREE_CODE (vr1min) == INTEGER_CST)
8042 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8043 integer_one_node);
8044 else
8045 *vr0max = vr1min;
8047 else if (*vr0type == VR_ANTI_RANGE
8048 && vr1type == VR_RANGE)
8050 *vr0type = VR_RANGE;
8051 if (TREE_CODE (*vr0max) == INTEGER_CST)
8052 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8053 integer_one_node);
8054 else
8055 *vr0min = *vr0max;
8056 *vr0max = vr1max;
8058 else
8059 gcc_unreachable ();
8061 else if ((operand_less_p (*vr0min, vr1max) == 1
8062 || operand_equal_p (*vr0min, vr1max, 0))
8063 && operand_less_p (vr1min, *vr0min) == 1)
8065 /* ( [ ) ] or ( )[ ] */
8066 if (*vr0type == VR_ANTI_RANGE
8067 && vr1type == VR_ANTI_RANGE)
8068 *vr0min = vr1min;
8069 else if (*vr0type == VR_RANGE
8070 && vr1type == VR_RANGE)
8071 *vr0max = vr1max;
8072 else if (*vr0type == VR_RANGE
8073 && vr1type == VR_ANTI_RANGE)
8075 if (TREE_CODE (vr1max) == INTEGER_CST)
8076 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8077 integer_one_node);
8078 else
8079 *vr0min = vr1max;
8081 else if (*vr0type == VR_ANTI_RANGE
8082 && vr1type == VR_RANGE)
8084 *vr0type = VR_RANGE;
8085 if (TREE_CODE (*vr0min) == INTEGER_CST)
8086 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8087 integer_one_node);
8088 else
8089 *vr0max = *vr0min;
8090 *vr0min = vr1min;
8092 else
8093 gcc_unreachable ();
8096 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8097 result for the intersection. That's always a conservative
8098 correct estimate. */
8100 return;
8104 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8105 in *VR0. This may not be the smallest possible such range. */
8107 static void
8108 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8110 value_range_t saved;
8112 /* If either range is VR_VARYING the other one wins. */
8113 if (vr1->type == VR_VARYING)
8114 return;
8115 if (vr0->type == VR_VARYING)
8117 copy_value_range (vr0, vr1);
8118 return;
8121 /* When either range is VR_UNDEFINED the resulting range is
8122 VR_UNDEFINED, too. */
8123 if (vr0->type == VR_UNDEFINED)
8124 return;
8125 if (vr1->type == VR_UNDEFINED)
8127 set_value_range_to_undefined (vr0);
8128 return;
8131 /* Save the original vr0 so we can return it as conservative intersection
8132 result when our worker turns things to varying. */
8133 saved = *vr0;
8134 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8135 vr1->type, vr1->min, vr1->max);
8136 /* Make sure to canonicalize the result though as the inversion of a
8137 VR_RANGE can still be a VR_RANGE. */
8138 set_and_canonicalize_value_range (vr0, vr0->type,
8139 vr0->min, vr0->max, vr0->equiv);
8140 /* If that failed, use the saved original VR0. */
8141 if (vr0->type == VR_VARYING)
8143 *vr0 = saved;
8144 return;
8146 /* If the result is VR_UNDEFINED there is no need to mess with
8147 the equivalencies. */
8148 if (vr0->type == VR_UNDEFINED)
8149 return;
8151 /* The resulting set of equivalences for range intersection is the union of
8152 the two sets. */
8153 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8154 bitmap_ior_into (vr0->equiv, vr1->equiv);
8155 else if (vr1->equiv && !vr0->equiv)
8156 bitmap_copy (vr0->equiv, vr1->equiv);
8159 static void
8160 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8162 if (dump_file && (dump_flags & TDF_DETAILS))
8164 fprintf (dump_file, "Intersecting\n ");
8165 dump_value_range (dump_file, vr0);
8166 fprintf (dump_file, "\nand\n ");
8167 dump_value_range (dump_file, vr1);
8168 fprintf (dump_file, "\n");
8170 vrp_intersect_ranges_1 (vr0, vr1);
8171 if (dump_file && (dump_flags & TDF_DETAILS))
8173 fprintf (dump_file, "to\n ");
8174 dump_value_range (dump_file, vr0);
8175 fprintf (dump_file, "\n");
8179 /* Meet operation for value ranges. Given two value ranges VR0 and
8180 VR1, store in VR0 a range that contains both VR0 and VR1. This
8181 may not be the smallest possible such range. */
8183 static void
8184 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8186 value_range_t saved;
8188 if (vr0->type == VR_UNDEFINED)
8190 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8191 return;
8194 if (vr1->type == VR_UNDEFINED)
8196 /* VR0 already has the resulting range. */
8197 return;
8200 if (vr0->type == VR_VARYING)
8202 /* Nothing to do. VR0 already has the resulting range. */
8203 return;
8206 if (vr1->type == VR_VARYING)
8208 set_value_range_to_varying (vr0);
8209 return;
8212 saved = *vr0;
8213 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8214 vr1->type, vr1->min, vr1->max);
8215 if (vr0->type == VR_VARYING)
8217 /* Failed to find an efficient meet. Before giving up and setting
8218 the result to VARYING, see if we can at least derive a useful
8219 anti-range. FIXME, all this nonsense about distinguishing
8220 anti-ranges from ranges is necessary because of the odd
8221 semantics of range_includes_zero_p and friends. */
8222 if (((saved.type == VR_RANGE
8223 && range_includes_zero_p (saved.min, saved.max) == 0)
8224 || (saved.type == VR_ANTI_RANGE
8225 && range_includes_zero_p (saved.min, saved.max) == 1))
8226 && ((vr1->type == VR_RANGE
8227 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8228 || (vr1->type == VR_ANTI_RANGE
8229 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8231 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8233 /* Since this meet operation did not result from the meeting of
8234 two equivalent names, VR0 cannot have any equivalences. */
8235 if (vr0->equiv)
8236 bitmap_clear (vr0->equiv);
8237 return;
8240 set_value_range_to_varying (vr0);
8241 return;
8243 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8244 vr0->equiv);
8245 if (vr0->type == VR_VARYING)
8246 return;
8248 /* The resulting set of equivalences is always the intersection of
8249 the two sets. */
8250 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8251 bitmap_and_into (vr0->equiv, vr1->equiv);
8252 else if (vr0->equiv && !vr1->equiv)
8253 bitmap_clear (vr0->equiv);
8256 static void
8257 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8259 if (dump_file && (dump_flags & TDF_DETAILS))
8261 fprintf (dump_file, "Meeting\n ");
8262 dump_value_range (dump_file, vr0);
8263 fprintf (dump_file, "\nand\n ");
8264 dump_value_range (dump_file, vr1);
8265 fprintf (dump_file, "\n");
8267 vrp_meet_1 (vr0, vr1);
8268 if (dump_file && (dump_flags & TDF_DETAILS))
8270 fprintf (dump_file, "to\n ");
8271 dump_value_range (dump_file, vr0);
8272 fprintf (dump_file, "\n");
8277 /* Visit all arguments for PHI node PHI that flow through executable
8278 edges. If a valid value range can be derived from all the incoming
8279 value ranges, set a new range for the LHS of PHI. */
8281 static enum ssa_prop_result
8282 vrp_visit_phi_node (gimple phi)
8284 size_t i;
8285 tree lhs = PHI_RESULT (phi);
8286 value_range_t *lhs_vr = get_value_range (lhs);
8287 value_range_t vr_result = VR_INITIALIZER;
8288 bool first = true;
8289 int edges, old_edges;
8290 struct loop *l;
8292 if (dump_file && (dump_flags & TDF_DETAILS))
8294 fprintf (dump_file, "\nVisiting PHI node: ");
8295 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8298 edges = 0;
8299 for (i = 0; i < gimple_phi_num_args (phi); i++)
8301 edge e = gimple_phi_arg_edge (phi, i);
8303 if (dump_file && (dump_flags & TDF_DETAILS))
8305 fprintf (dump_file,
8306 "\n Argument #%d (%d -> %d %sexecutable)\n",
8307 (int) i, e->src->index, e->dest->index,
8308 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8311 if (e->flags & EDGE_EXECUTABLE)
8313 tree arg = PHI_ARG_DEF (phi, i);
8314 value_range_t vr_arg;
8316 ++edges;
8318 if (TREE_CODE (arg) == SSA_NAME)
8320 vr_arg = *(get_value_range (arg));
8321 /* Do not allow equivalences or symbolic ranges to leak in from
8322 backedges. That creates invalid equivalencies.
8323 See PR53465 and PR54767. */
8324 if (e->flags & EDGE_DFS_BACK
8325 && (vr_arg.type == VR_RANGE
8326 || vr_arg.type == VR_ANTI_RANGE))
8328 vr_arg.equiv = NULL;
8329 if (symbolic_range_p (&vr_arg))
8331 vr_arg.type = VR_VARYING;
8332 vr_arg.min = NULL_TREE;
8333 vr_arg.max = NULL_TREE;
8337 else
8339 if (is_overflow_infinity (arg))
8340 arg = drop_tree_overflow (arg);
8342 vr_arg.type = VR_RANGE;
8343 vr_arg.min = arg;
8344 vr_arg.max = arg;
8345 vr_arg.equiv = NULL;
8348 if (dump_file && (dump_flags & TDF_DETAILS))
8350 fprintf (dump_file, "\t");
8351 print_generic_expr (dump_file, arg, dump_flags);
8352 fprintf (dump_file, "\n\tValue: ");
8353 dump_value_range (dump_file, &vr_arg);
8354 fprintf (dump_file, "\n");
8357 if (first)
8358 copy_value_range (&vr_result, &vr_arg);
8359 else
8360 vrp_meet (&vr_result, &vr_arg);
8361 first = false;
8363 if (vr_result.type == VR_VARYING)
8364 break;
8368 if (vr_result.type == VR_VARYING)
8369 goto varying;
8370 else if (vr_result.type == VR_UNDEFINED)
8371 goto update_range;
8373 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8374 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8376 /* To prevent infinite iterations in the algorithm, derive ranges
8377 when the new value is slightly bigger or smaller than the
8378 previous one. We don't do this if we have seen a new executable
8379 edge; this helps us avoid an overflow infinity for conditionals
8380 which are not in a loop. If the old value-range was VR_UNDEFINED
8381 use the updated range and iterate one more time. */
8382 if (edges > 0
8383 && gimple_phi_num_args (phi) > 1
8384 && edges == old_edges
8385 && lhs_vr->type != VR_UNDEFINED)
8387 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8388 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8390 /* For non VR_RANGE or for pointers fall back to varying if
8391 the range changed. */
8392 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8393 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8394 && (cmp_min != 0 || cmp_max != 0))
8395 goto varying;
8397 /* If the new minimum is smaller or larger than the previous
8398 one, go all the way to -INF. In the first case, to avoid
8399 iterating millions of times to reach -INF, and in the
8400 other case to avoid infinite bouncing between different
8401 minimums. */
8402 if (cmp_min > 0 || cmp_min < 0)
8404 if (!needs_overflow_infinity (TREE_TYPE (vr_result.min))
8405 || !vrp_var_may_overflow (lhs, phi))
8406 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
8407 else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
8408 vr_result.min =
8409 negative_overflow_infinity (TREE_TYPE (vr_result.min));
8412 /* Similarly, if the new maximum is smaller or larger than
8413 the previous one, go all the way to +INF. */
8414 if (cmp_max < 0 || cmp_max > 0)
8416 if (!needs_overflow_infinity (TREE_TYPE (vr_result.max))
8417 || !vrp_var_may_overflow (lhs, phi))
8418 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
8419 else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
8420 vr_result.max =
8421 positive_overflow_infinity (TREE_TYPE (vr_result.max));
8424 /* If we dropped either bound to +-INF then if this is a loop
8425 PHI node SCEV may known more about its value-range. */
8426 if ((cmp_min > 0 || cmp_min < 0
8427 || cmp_max < 0 || cmp_max > 0)
8428 && current_loops
8429 && (l = loop_containing_stmt (phi))
8430 && l->header == gimple_bb (phi))
8431 adjust_range_with_scev (&vr_result, l, phi, lhs);
8433 /* If we will end up with a (-INF, +INF) range, set it to
8434 VARYING. Same if the previous max value was invalid for
8435 the type and we end up with vr_result.min > vr_result.max. */
8436 if ((vrp_val_is_max (vr_result.max)
8437 && vrp_val_is_min (vr_result.min))
8438 || compare_values (vr_result.min,
8439 vr_result.max) > 0)
8440 goto varying;
8443 /* If the new range is different than the previous value, keep
8444 iterating. */
8445 update_range:
8446 if (update_value_range (lhs, &vr_result))
8448 if (dump_file && (dump_flags & TDF_DETAILS))
8450 fprintf (dump_file, "Found new range for ");
8451 print_generic_expr (dump_file, lhs, 0);
8452 fprintf (dump_file, ": ");
8453 dump_value_range (dump_file, &vr_result);
8454 fprintf (dump_file, "\n\n");
8457 return SSA_PROP_INTERESTING;
8460 /* Nothing changed, don't add outgoing edges. */
8461 return SSA_PROP_NOT_INTERESTING;
8463 /* No match found. Set the LHS to VARYING. */
8464 varying:
8465 set_value_range_to_varying (lhs_vr);
8466 return SSA_PROP_VARYING;
8469 /* Simplify boolean operations if the source is known
8470 to be already a boolean. */
8471 static bool
8472 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8474 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8475 tree lhs, op0, op1;
8476 bool need_conversion;
8478 /* We handle only !=/== case here. */
8479 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8481 op0 = gimple_assign_rhs1 (stmt);
8482 if (!op_with_boolean_value_range_p (op0))
8483 return false;
8485 op1 = gimple_assign_rhs2 (stmt);
8486 if (!op_with_boolean_value_range_p (op1))
8487 return false;
8489 /* Reduce number of cases to handle to NE_EXPR. As there is no
8490 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8491 if (rhs_code == EQ_EXPR)
8493 if (TREE_CODE (op1) == INTEGER_CST)
8494 op1 = int_const_binop (BIT_XOR_EXPR, op1, integer_one_node);
8495 else
8496 return false;
8499 lhs = gimple_assign_lhs (stmt);
8500 need_conversion
8501 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8503 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8504 if (need_conversion
8505 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8506 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8507 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8508 return false;
8510 /* For A != 0 we can substitute A itself. */
8511 if (integer_zerop (op1))
8512 gimple_assign_set_rhs_with_ops (gsi,
8513 need_conversion
8514 ? NOP_EXPR : TREE_CODE (op0),
8515 op0, NULL_TREE);
8516 /* For A != B we substitute A ^ B. Either with conversion. */
8517 else if (need_conversion)
8519 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8520 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8521 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8522 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8524 /* Or without. */
8525 else
8526 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8527 update_stmt (gsi_stmt (*gsi));
8529 return true;
8532 /* Simplify a division or modulo operator to a right shift or
8533 bitwise and if the first operand is unsigned or is greater
8534 than zero and the second operand is an exact power of two. */
8536 static bool
8537 simplify_div_or_mod_using_ranges (gimple stmt)
8539 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8540 tree val = NULL;
8541 tree op0 = gimple_assign_rhs1 (stmt);
8542 tree op1 = gimple_assign_rhs2 (stmt);
8543 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8545 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8547 val = integer_one_node;
8549 else
8551 bool sop = false;
8553 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8555 if (val
8556 && sop
8557 && integer_onep (val)
8558 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8560 location_t location;
8562 if (!gimple_has_location (stmt))
8563 location = input_location;
8564 else
8565 location = gimple_location (stmt);
8566 warning_at (location, OPT_Wstrict_overflow,
8567 "assuming signed overflow does not occur when "
8568 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8572 if (val && integer_onep (val))
8574 tree t;
8576 if (rhs_code == TRUNC_DIV_EXPR)
8578 t = build_int_cst (integer_type_node, tree_log2 (op1));
8579 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8580 gimple_assign_set_rhs1 (stmt, op0);
8581 gimple_assign_set_rhs2 (stmt, t);
8583 else
8585 t = build_int_cst (TREE_TYPE (op1), 1);
8586 t = int_const_binop (MINUS_EXPR, op1, t);
8587 t = fold_convert (TREE_TYPE (op0), t);
8589 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8590 gimple_assign_set_rhs1 (stmt, op0);
8591 gimple_assign_set_rhs2 (stmt, t);
8594 update_stmt (stmt);
8595 return true;
8598 return false;
8601 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8602 ABS_EXPR. If the operand is <= 0, then simplify the
8603 ABS_EXPR into a NEGATE_EXPR. */
8605 static bool
8606 simplify_abs_using_ranges (gimple stmt)
8608 tree val = NULL;
8609 tree op = gimple_assign_rhs1 (stmt);
8610 tree type = TREE_TYPE (op);
8611 value_range_t *vr = get_value_range (op);
8613 if (TYPE_UNSIGNED (type))
8615 val = integer_zero_node;
8617 else if (vr)
8619 bool sop = false;
8621 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8622 if (!val)
8624 sop = false;
8625 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8626 &sop);
8628 if (val)
8630 if (integer_zerop (val))
8631 val = integer_one_node;
8632 else if (integer_onep (val))
8633 val = integer_zero_node;
8637 if (val
8638 && (integer_onep (val) || integer_zerop (val)))
8640 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8642 location_t location;
8644 if (!gimple_has_location (stmt))
8645 location = input_location;
8646 else
8647 location = gimple_location (stmt);
8648 warning_at (location, OPT_Wstrict_overflow,
8649 "assuming signed overflow does not occur when "
8650 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8653 gimple_assign_set_rhs1 (stmt, op);
8654 if (integer_onep (val))
8655 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8656 else
8657 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8658 update_stmt (stmt);
8659 return true;
8663 return false;
8666 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8667 If all the bits that are being cleared by & are already
8668 known to be zero from VR, or all the bits that are being
8669 set by | are already known to be one from VR, the bit
8670 operation is redundant. */
8672 static bool
8673 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8675 tree op0 = gimple_assign_rhs1 (stmt);
8676 tree op1 = gimple_assign_rhs2 (stmt);
8677 tree op = NULL_TREE;
8678 value_range_t vr0 = VR_INITIALIZER;
8679 value_range_t vr1 = VR_INITIALIZER;
8680 double_int may_be_nonzero0, may_be_nonzero1;
8681 double_int must_be_nonzero0, must_be_nonzero1;
8682 double_int mask;
8684 if (TREE_CODE (op0) == SSA_NAME)
8685 vr0 = *(get_value_range (op0));
8686 else if (is_gimple_min_invariant (op0))
8687 set_value_range_to_value (&vr0, op0, NULL);
8688 else
8689 return false;
8691 if (TREE_CODE (op1) == SSA_NAME)
8692 vr1 = *(get_value_range (op1));
8693 else if (is_gimple_min_invariant (op1))
8694 set_value_range_to_value (&vr1, op1, NULL);
8695 else
8696 return false;
8698 if (!zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0, &must_be_nonzero0))
8699 return false;
8700 if (!zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1, &must_be_nonzero1))
8701 return false;
8703 switch (gimple_assign_rhs_code (stmt))
8705 case BIT_AND_EXPR:
8706 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8707 if (mask.is_zero ())
8709 op = op0;
8710 break;
8712 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8713 if (mask.is_zero ())
8715 op = op1;
8716 break;
8718 break;
8719 case BIT_IOR_EXPR:
8720 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8721 if (mask.is_zero ())
8723 op = op1;
8724 break;
8726 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8727 if (mask.is_zero ())
8729 op = op0;
8730 break;
8732 break;
8733 default:
8734 gcc_unreachable ();
8737 if (op == NULL_TREE)
8738 return false;
8740 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8741 update_stmt (gsi_stmt (*gsi));
8742 return true;
8745 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8746 a known value range VR.
8748 If there is one and only one value which will satisfy the
8749 conditional, then return that value. Else return NULL. */
8751 static tree
8752 test_for_singularity (enum tree_code cond_code, tree op0,
8753 tree op1, value_range_t *vr)
8755 tree min = NULL;
8756 tree max = NULL;
8758 /* Extract minimum/maximum values which satisfy the
8759 the conditional as it was written. */
8760 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8762 /* This should not be negative infinity; there is no overflow
8763 here. */
8764 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8766 max = op1;
8767 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8769 tree one = build_int_cst (TREE_TYPE (op0), 1);
8770 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8771 if (EXPR_P (max))
8772 TREE_NO_WARNING (max) = 1;
8775 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8777 /* This should not be positive infinity; there is no overflow
8778 here. */
8779 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8781 min = op1;
8782 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8784 tree one = build_int_cst (TREE_TYPE (op0), 1);
8785 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8786 if (EXPR_P (min))
8787 TREE_NO_WARNING (min) = 1;
8791 /* Now refine the minimum and maximum values using any
8792 value range information we have for op0. */
8793 if (min && max)
8795 if (compare_values (vr->min, min) == 1)
8796 min = vr->min;
8797 if (compare_values (vr->max, max) == -1)
8798 max = vr->max;
8800 /* If the new min/max values have converged to a single value,
8801 then there is only one value which can satisfy the condition,
8802 return that value. */
8803 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
8804 return min;
8806 return NULL;
8809 /* Return whether the value range *VR fits in an integer type specified
8810 by PRECISION and UNSIGNED_P. */
8812 static bool
8813 range_fits_type_p (value_range_t *vr, unsigned precision, bool unsigned_p)
8815 tree src_type;
8816 unsigned src_precision;
8817 double_int tem;
8819 /* We can only handle integral and pointer types. */
8820 src_type = TREE_TYPE (vr->min);
8821 if (!INTEGRAL_TYPE_P (src_type)
8822 && !POINTER_TYPE_P (src_type))
8823 return false;
8825 /* An extension is fine unless VR is signed and unsigned_p,
8826 and so is an identity transform. */
8827 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
8828 if ((src_precision < precision
8829 && !(unsigned_p && !TYPE_UNSIGNED (src_type)))
8830 || (src_precision == precision
8831 && TYPE_UNSIGNED (src_type) == unsigned_p))
8832 return true;
8834 /* Now we can only handle ranges with constant bounds. */
8835 if (vr->type != VR_RANGE
8836 || TREE_CODE (vr->min) != INTEGER_CST
8837 || TREE_CODE (vr->max) != INTEGER_CST)
8838 return false;
8840 /* For sign changes, the MSB of the double_int has to be clear.
8841 An unsigned value with its MSB set cannot be represented by
8842 a signed double_int, while a negative value cannot be represented
8843 by an unsigned double_int. */
8844 if (TYPE_UNSIGNED (src_type) != unsigned_p
8845 && (TREE_INT_CST_HIGH (vr->min) | TREE_INT_CST_HIGH (vr->max)) < 0)
8846 return false;
8848 /* Then we can perform the conversion on both ends and compare
8849 the result for equality. */
8850 tem = tree_to_double_int (vr->min).ext (precision, unsigned_p);
8851 if (tree_to_double_int (vr->min) != tem)
8852 return false;
8853 tem = tree_to_double_int (vr->max).ext (precision, unsigned_p);
8854 if (tree_to_double_int (vr->max) != tem)
8855 return false;
8857 return true;
8860 /* Simplify a conditional using a relational operator to an equality
8861 test if the range information indicates only one value can satisfy
8862 the original conditional. */
8864 static bool
8865 simplify_cond_using_ranges (gimple stmt)
8867 tree op0 = gimple_cond_lhs (stmt);
8868 tree op1 = gimple_cond_rhs (stmt);
8869 enum tree_code cond_code = gimple_cond_code (stmt);
8871 if (cond_code != NE_EXPR
8872 && cond_code != EQ_EXPR
8873 && TREE_CODE (op0) == SSA_NAME
8874 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8875 && is_gimple_min_invariant (op1))
8877 value_range_t *vr = get_value_range (op0);
8879 /* If we have range information for OP0, then we might be
8880 able to simplify this conditional. */
8881 if (vr->type == VR_RANGE)
8883 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
8885 if (new_tree)
8887 if (dump_file)
8889 fprintf (dump_file, "Simplified relational ");
8890 print_gimple_stmt (dump_file, stmt, 0, 0);
8891 fprintf (dump_file, " into ");
8894 gimple_cond_set_code (stmt, EQ_EXPR);
8895 gimple_cond_set_lhs (stmt, op0);
8896 gimple_cond_set_rhs (stmt, new_tree);
8898 update_stmt (stmt);
8900 if (dump_file)
8902 print_gimple_stmt (dump_file, stmt, 0, 0);
8903 fprintf (dump_file, "\n");
8906 return true;
8909 /* Try again after inverting the condition. We only deal
8910 with integral types here, so no need to worry about
8911 issues with inverting FP comparisons. */
8912 cond_code = invert_tree_comparison (cond_code, false);
8913 new_tree = test_for_singularity (cond_code, op0, op1, vr);
8915 if (new_tree)
8917 if (dump_file)
8919 fprintf (dump_file, "Simplified relational ");
8920 print_gimple_stmt (dump_file, stmt, 0, 0);
8921 fprintf (dump_file, " into ");
8924 gimple_cond_set_code (stmt, NE_EXPR);
8925 gimple_cond_set_lhs (stmt, op0);
8926 gimple_cond_set_rhs (stmt, new_tree);
8928 update_stmt (stmt);
8930 if (dump_file)
8932 print_gimple_stmt (dump_file, stmt, 0, 0);
8933 fprintf (dump_file, "\n");
8936 return true;
8941 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8942 see if OP0 was set by a type conversion where the source of
8943 the conversion is another SSA_NAME with a range that fits
8944 into the range of OP0's type.
8946 If so, the conversion is redundant as the earlier SSA_NAME can be
8947 used for the comparison directly if we just massage the constant in the
8948 comparison. */
8949 if (TREE_CODE (op0) == SSA_NAME
8950 && TREE_CODE (op1) == INTEGER_CST)
8952 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
8953 tree innerop;
8955 if (!is_gimple_assign (def_stmt)
8956 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
8957 return false;
8959 innerop = gimple_assign_rhs1 (def_stmt);
8961 if (TREE_CODE (innerop) == SSA_NAME
8962 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
8964 value_range_t *vr = get_value_range (innerop);
8966 if (range_int_cst_p (vr)
8967 && range_fits_type_p (vr,
8968 TYPE_PRECISION (TREE_TYPE (op0)),
8969 TYPE_UNSIGNED (TREE_TYPE (op0)))
8970 && int_fits_type_p (op1, TREE_TYPE (innerop))
8971 /* The range must not have overflowed, or if it did overflow
8972 we must not be wrapping/trapping overflow and optimizing
8973 with strict overflow semantics. */
8974 && ((!is_negative_overflow_infinity (vr->min)
8975 && !is_positive_overflow_infinity (vr->max))
8976 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
8978 /* If the range overflowed and the user has asked for warnings
8979 when strict overflow semantics were used to optimize code,
8980 issue an appropriate warning. */
8981 if ((is_negative_overflow_infinity (vr->min)
8982 || is_positive_overflow_infinity (vr->max))
8983 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
8985 location_t location;
8987 if (!gimple_has_location (stmt))
8988 location = input_location;
8989 else
8990 location = gimple_location (stmt);
8991 warning_at (location, OPT_Wstrict_overflow,
8992 "assuming signed overflow does not occur when "
8993 "simplifying conditional");
8996 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
8997 gimple_cond_set_lhs (stmt, innerop);
8998 gimple_cond_set_rhs (stmt, newconst);
8999 return true;
9004 return false;
9007 /* Simplify a switch statement using the value range of the switch
9008 argument. */
9010 static bool
9011 simplify_switch_using_ranges (gimple stmt)
9013 tree op = gimple_switch_index (stmt);
9014 value_range_t *vr;
9015 bool take_default;
9016 edge e;
9017 edge_iterator ei;
9018 size_t i = 0, j = 0, n, n2;
9019 tree vec2;
9020 switch_update su;
9021 size_t k = 1, l = 0;
9023 if (TREE_CODE (op) == SSA_NAME)
9025 vr = get_value_range (op);
9027 /* We can only handle integer ranges. */
9028 if ((vr->type != VR_RANGE
9029 && vr->type != VR_ANTI_RANGE)
9030 || symbolic_range_p (vr))
9031 return false;
9033 /* Find case label for min/max of the value range. */
9034 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9036 else if (TREE_CODE (op) == INTEGER_CST)
9038 take_default = !find_case_label_index (stmt, 1, op, &i);
9039 if (take_default)
9041 i = 1;
9042 j = 0;
9044 else
9046 j = i;
9049 else
9050 return false;
9052 n = gimple_switch_num_labels (stmt);
9054 /* Bail out if this is just all edges taken. */
9055 if (i == 1
9056 && j == n - 1
9057 && take_default)
9058 return false;
9060 /* Build a new vector of taken case labels. */
9061 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9062 n2 = 0;
9064 /* Add the default edge, if necessary. */
9065 if (take_default)
9066 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9068 for (; i <= j; ++i, ++n2)
9069 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9071 for (; k <= l; ++k, ++n2)
9072 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9074 /* Mark needed edges. */
9075 for (i = 0; i < n2; ++i)
9077 e = find_edge (gimple_bb (stmt),
9078 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9079 e->aux = (void *)-1;
9082 /* Queue not needed edges for later removal. */
9083 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9085 if (e->aux == (void *)-1)
9087 e->aux = NULL;
9088 continue;
9091 if (dump_file && (dump_flags & TDF_DETAILS))
9093 fprintf (dump_file, "removing unreachable case label\n");
9095 to_remove_edges.safe_push (e);
9096 e->flags &= ~EDGE_EXECUTABLE;
9099 /* And queue an update for the stmt. */
9100 su.stmt = stmt;
9101 su.vec = vec2;
9102 to_update_switch_stmts.safe_push (su);
9103 return false;
9106 /* Simplify an integral conversion from an SSA name in STMT. */
9108 static bool
9109 simplify_conversion_using_ranges (gimple stmt)
9111 tree innerop, middleop, finaltype;
9112 gimple def_stmt;
9113 value_range_t *innervr;
9114 bool inner_unsigned_p, middle_unsigned_p, final_unsigned_p;
9115 unsigned inner_prec, middle_prec, final_prec;
9116 double_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9118 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9119 if (!INTEGRAL_TYPE_P (finaltype))
9120 return false;
9121 middleop = gimple_assign_rhs1 (stmt);
9122 def_stmt = SSA_NAME_DEF_STMT (middleop);
9123 if (!is_gimple_assign (def_stmt)
9124 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9125 return false;
9126 innerop = gimple_assign_rhs1 (def_stmt);
9127 if (TREE_CODE (innerop) != SSA_NAME
9128 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9129 return false;
9131 /* Get the value-range of the inner operand. */
9132 innervr = get_value_range (innerop);
9133 if (innervr->type != VR_RANGE
9134 || TREE_CODE (innervr->min) != INTEGER_CST
9135 || TREE_CODE (innervr->max) != INTEGER_CST)
9136 return false;
9138 /* Simulate the conversion chain to check if the result is equal if
9139 the middle conversion is removed. */
9140 innermin = tree_to_double_int (innervr->min);
9141 innermax = tree_to_double_int (innervr->max);
9143 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9144 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9145 final_prec = TYPE_PRECISION (finaltype);
9147 /* If the first conversion is not injective, the second must not
9148 be widening. */
9149 if ((innermax - innermin).ugt (double_int::mask (middle_prec))
9150 && middle_prec < final_prec)
9151 return false;
9152 /* We also want a medium value so that we can track the effect that
9153 narrowing conversions with sign change have. */
9154 inner_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (innerop));
9155 if (inner_unsigned_p)
9156 innermed = double_int::mask (inner_prec).lrshift (1, inner_prec);
9157 else
9158 innermed = double_int_zero;
9159 if (innermin.cmp (innermed, inner_unsigned_p) >= 0
9160 || innermed.cmp (innermax, inner_unsigned_p) >= 0)
9161 innermed = innermin;
9163 middle_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (middleop));
9164 middlemin = innermin.ext (middle_prec, middle_unsigned_p);
9165 middlemed = innermed.ext (middle_prec, middle_unsigned_p);
9166 middlemax = innermax.ext (middle_prec, middle_unsigned_p);
9168 /* Require that the final conversion applied to both the original
9169 and the intermediate range produces the same result. */
9170 final_unsigned_p = TYPE_UNSIGNED (finaltype);
9171 if (middlemin.ext (final_prec, final_unsigned_p)
9172 != innermin.ext (final_prec, final_unsigned_p)
9173 || middlemed.ext (final_prec, final_unsigned_p)
9174 != innermed.ext (final_prec, final_unsigned_p)
9175 || middlemax.ext (final_prec, final_unsigned_p)
9176 != innermax.ext (final_prec, final_unsigned_p))
9177 return false;
9179 gimple_assign_set_rhs1 (stmt, innerop);
9180 update_stmt (stmt);
9181 return true;
9184 /* Simplify a conversion from integral SSA name to float in STMT. */
9186 static bool
9187 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9189 tree rhs1 = gimple_assign_rhs1 (stmt);
9190 value_range_t *vr = get_value_range (rhs1);
9191 enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9192 enum machine_mode mode;
9193 tree tem;
9194 gimple conv;
9196 /* We can only handle constant ranges. */
9197 if (vr->type != VR_RANGE
9198 || TREE_CODE (vr->min) != INTEGER_CST
9199 || TREE_CODE (vr->max) != INTEGER_CST)
9200 return false;
9202 /* First check if we can use a signed type in place of an unsigned. */
9203 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9204 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9205 != CODE_FOR_nothing)
9206 && range_fits_type_p (vr, GET_MODE_PRECISION
9207 (TYPE_MODE (TREE_TYPE (rhs1))), 0))
9208 mode = TYPE_MODE (TREE_TYPE (rhs1));
9209 /* If we can do the conversion in the current input mode do nothing. */
9210 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9211 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9212 return false;
9213 /* Otherwise search for a mode we can use, starting from the narrowest
9214 integer mode available. */
9215 else
9217 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9220 /* If we cannot do a signed conversion to float from mode
9221 or if the value-range does not fit in the signed type
9222 try with a wider mode. */
9223 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9224 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), 0))
9225 break;
9227 mode = GET_MODE_WIDER_MODE (mode);
9228 /* But do not widen the input. Instead leave that to the
9229 optabs expansion code. */
9230 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9231 return false;
9233 while (mode != VOIDmode);
9234 if (mode == VOIDmode)
9235 return false;
9238 /* It works, insert a truncation or sign-change before the
9239 float conversion. */
9240 tem = make_ssa_name (build_nonstandard_integer_type
9241 (GET_MODE_PRECISION (mode), 0), NULL);
9242 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
9243 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9244 gimple_assign_set_rhs1 (stmt, tem);
9245 update_stmt (stmt);
9247 return true;
9250 /* Simplify STMT using ranges if possible. */
9252 static bool
9253 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9255 gimple stmt = gsi_stmt (*gsi);
9256 if (is_gimple_assign (stmt))
9258 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9259 tree rhs1 = gimple_assign_rhs1 (stmt);
9261 switch (rhs_code)
9263 case EQ_EXPR:
9264 case NE_EXPR:
9265 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9266 if the RHS is zero or one, and the LHS are known to be boolean
9267 values. */
9268 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9269 return simplify_truth_ops_using_ranges (gsi, stmt);
9270 break;
9272 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9273 and BIT_AND_EXPR respectively if the first operand is greater
9274 than zero and the second operand is an exact power of two. */
9275 case TRUNC_DIV_EXPR:
9276 case TRUNC_MOD_EXPR:
9277 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9278 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9279 return simplify_div_or_mod_using_ranges (stmt);
9280 break;
9282 /* Transform ABS (X) into X or -X as appropriate. */
9283 case ABS_EXPR:
9284 if (TREE_CODE (rhs1) == SSA_NAME
9285 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9286 return simplify_abs_using_ranges (stmt);
9287 break;
9289 case BIT_AND_EXPR:
9290 case BIT_IOR_EXPR:
9291 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9292 if all the bits being cleared are already cleared or
9293 all the bits being set are already set. */
9294 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9295 return simplify_bit_ops_using_ranges (gsi, stmt);
9296 break;
9298 CASE_CONVERT:
9299 if (TREE_CODE (rhs1) == SSA_NAME
9300 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9301 return simplify_conversion_using_ranges (stmt);
9302 break;
9304 case FLOAT_EXPR:
9305 if (TREE_CODE (rhs1) == SSA_NAME
9306 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9307 return simplify_float_conversion_using_ranges (gsi, stmt);
9308 break;
9310 default:
9311 break;
9314 else if (gimple_code (stmt) == GIMPLE_COND)
9315 return simplify_cond_using_ranges (stmt);
9316 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9317 return simplify_switch_using_ranges (stmt);
9319 return false;
9322 /* If the statement pointed by SI has a predicate whose value can be
9323 computed using the value range information computed by VRP, compute
9324 its value and return true. Otherwise, return false. */
9326 static bool
9327 fold_predicate_in (gimple_stmt_iterator *si)
9329 bool assignment_p = false;
9330 tree val;
9331 gimple stmt = gsi_stmt (*si);
9333 if (is_gimple_assign (stmt)
9334 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9336 assignment_p = true;
9337 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9338 gimple_assign_rhs1 (stmt),
9339 gimple_assign_rhs2 (stmt),
9340 stmt);
9342 else if (gimple_code (stmt) == GIMPLE_COND)
9343 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
9344 gimple_cond_lhs (stmt),
9345 gimple_cond_rhs (stmt),
9346 stmt);
9347 else
9348 return false;
9350 if (val)
9352 if (assignment_p)
9353 val = fold_convert (gimple_expr_type (stmt), val);
9355 if (dump_file)
9357 fprintf (dump_file, "Folding predicate ");
9358 print_gimple_expr (dump_file, stmt, 0, 0);
9359 fprintf (dump_file, " to ");
9360 print_generic_expr (dump_file, val, 0);
9361 fprintf (dump_file, "\n");
9364 if (is_gimple_assign (stmt))
9365 gimple_assign_set_rhs_from_tree (si, val);
9366 else
9368 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9369 if (integer_zerop (val))
9370 gimple_cond_make_false (stmt);
9371 else if (integer_onep (val))
9372 gimple_cond_make_true (stmt);
9373 else
9374 gcc_unreachable ();
9377 return true;
9380 return false;
9383 /* Callback for substitute_and_fold folding the stmt at *SI. */
9385 static bool
9386 vrp_fold_stmt (gimple_stmt_iterator *si)
9388 if (fold_predicate_in (si))
9389 return true;
9391 return simplify_stmt_using_ranges (si);
9394 /* Stack of dest,src equivalency pairs that need to be restored after
9395 each attempt to thread a block's incoming edge to an outgoing edge.
9397 A NULL entry is used to mark the end of pairs which need to be
9398 restored. */
9399 static vec<tree> equiv_stack;
9401 /* A trivial wrapper so that we can present the generic jump threading
9402 code with a simple API for simplifying statements. STMT is the
9403 statement we want to simplify, WITHIN_STMT provides the location
9404 for any overflow warnings. */
9406 static tree
9407 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9409 if (gimple_code (stmt) == GIMPLE_COND)
9410 return vrp_evaluate_conditional (gimple_cond_code (stmt),
9411 gimple_cond_lhs (stmt),
9412 gimple_cond_rhs (stmt), within_stmt);
9414 if (gimple_code (stmt) == GIMPLE_ASSIGN)
9416 value_range_t new_vr = VR_INITIALIZER;
9417 tree lhs = gimple_assign_lhs (stmt);
9419 if (TREE_CODE (lhs) == SSA_NAME
9420 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
9421 || POINTER_TYPE_P (TREE_TYPE (lhs))))
9423 extract_range_from_assignment (&new_vr, stmt);
9424 if (range_int_cst_singleton_p (&new_vr))
9425 return new_vr.min;
9429 return NULL_TREE;
9432 /* Blocks which have more than one predecessor and more than
9433 one successor present jump threading opportunities, i.e.,
9434 when the block is reached from a specific predecessor, we
9435 may be able to determine which of the outgoing edges will
9436 be traversed. When this optimization applies, we are able
9437 to avoid conditionals at runtime and we may expose secondary
9438 optimization opportunities.
9440 This routine is effectively a driver for the generic jump
9441 threading code. It basically just presents the generic code
9442 with edges that may be suitable for jump threading.
9444 Unlike DOM, we do not iterate VRP if jump threading was successful.
9445 While iterating may expose new opportunities for VRP, it is expected
9446 those opportunities would be very limited and the compile time cost
9447 to expose those opportunities would be significant.
9449 As jump threading opportunities are discovered, they are registered
9450 for later realization. */
9452 static void
9453 identify_jump_threads (void)
9455 basic_block bb;
9456 gimple dummy;
9457 int i;
9458 edge e;
9460 /* Ugh. When substituting values earlier in this pass we can
9461 wipe the dominance information. So rebuild the dominator
9462 information as we need it within the jump threading code. */
9463 calculate_dominance_info (CDI_DOMINATORS);
9465 /* We do not allow VRP information to be used for jump threading
9466 across a back edge in the CFG. Otherwise it becomes too
9467 difficult to avoid eliminating loop exit tests. Of course
9468 EDGE_DFS_BACK is not accurate at this time so we have to
9469 recompute it. */
9470 mark_dfs_back_edges ();
9472 /* Do not thread across edges we are about to remove. Just marking
9473 them as EDGE_DFS_BACK will do. */
9474 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9475 e->flags |= EDGE_DFS_BACK;
9477 /* Allocate our unwinder stack to unwind any temporary equivalences
9478 that might be recorded. */
9479 equiv_stack.create (20);
9481 /* To avoid lots of silly node creation, we create a single
9482 conditional and just modify it in-place when attempting to
9483 thread jumps. */
9484 dummy = gimple_build_cond (EQ_EXPR,
9485 integer_zero_node, integer_zero_node,
9486 NULL, NULL);
9488 /* Walk through all the blocks finding those which present a
9489 potential jump threading opportunity. We could set this up
9490 as a dominator walker and record data during the walk, but
9491 I doubt it's worth the effort for the classes of jump
9492 threading opportunities we are trying to identify at this
9493 point in compilation. */
9494 FOR_EACH_BB (bb)
9496 gimple last;
9498 /* If the generic jump threading code does not find this block
9499 interesting, then there is nothing to do. */
9500 if (! potentially_threadable_block (bb))
9501 continue;
9503 /* We only care about blocks ending in a COND_EXPR. While there
9504 may be some value in handling SWITCH_EXPR here, I doubt it's
9505 terribly important. */
9506 last = gsi_stmt (gsi_last_bb (bb));
9508 /* We're basically looking for a switch or any kind of conditional with
9509 integral or pointer type arguments. Note the type of the second
9510 argument will be the same as the first argument, so no need to
9511 check it explicitly. */
9512 if (gimple_code (last) == GIMPLE_SWITCH
9513 || (gimple_code (last) == GIMPLE_COND
9514 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9515 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9516 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9517 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9518 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9520 edge_iterator ei;
9522 /* We've got a block with multiple predecessors and multiple
9523 successors which also ends in a suitable conditional or
9524 switch statement. For each predecessor, see if we can thread
9525 it to a specific successor. */
9526 FOR_EACH_EDGE (e, ei, bb->preds)
9528 /* Do not thread across back edges or abnormal edges
9529 in the CFG. */
9530 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9531 continue;
9533 thread_across_edge (dummy, e, true, &equiv_stack,
9534 simplify_stmt_for_jump_threading);
9539 /* We do not actually update the CFG or SSA graphs at this point as
9540 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9541 handle ASSERT_EXPRs gracefully. */
9544 /* We identified all the jump threading opportunities earlier, but could
9545 not transform the CFG at that time. This routine transforms the
9546 CFG and arranges for the dominator tree to be rebuilt if necessary.
9548 Note the SSA graph update will occur during the normal TODO
9549 processing by the pass manager. */
9550 static void
9551 finalize_jump_threads (void)
9553 thread_through_all_blocks (false);
9554 equiv_stack.release ();
9558 /* Traverse all the blocks folding conditionals with known ranges. */
9560 static void
9561 vrp_finalize (void)
9563 size_t i;
9565 values_propagated = true;
9567 if (dump_file)
9569 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9570 dump_all_value_ranges (dump_file);
9571 fprintf (dump_file, "\n");
9574 substitute_and_fold (op_with_constant_singleton_value_range,
9575 vrp_fold_stmt, false);
9577 if (warn_array_bounds)
9578 check_all_array_refs ();
9580 /* We must identify jump threading opportunities before we release
9581 the datastructures built by VRP. */
9582 identify_jump_threads ();
9584 /* Set value range to non pointer SSA_NAMEs. */
9585 for (i = 0; i < num_vr_values; i++)
9586 if (vr_value[i])
9588 tree name = ssa_name (i);
9590 if (!name
9591 || POINTER_TYPE_P (TREE_TYPE (name))
9592 || (vr_value[i]->type == VR_VARYING)
9593 || (vr_value[i]->type == VR_UNDEFINED))
9594 continue;
9596 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
9597 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST))
9599 if (vr_value[i]->type == VR_RANGE)
9600 set_range_info (name,
9601 tree_to_double_int (vr_value[i]->min),
9602 tree_to_double_int (vr_value[i]->max));
9603 else if (vr_value[i]->type == VR_ANTI_RANGE)
9605 /* VR_ANTI_RANGE ~[min, max] is encoded compactly as
9606 [max + 1, min - 1] without additional attributes.
9607 When min value > max value, we know that it is
9608 VR_ANTI_RANGE; it is VR_RANGE otherwise. */
9610 /* ~[0,0] anti-range is represented as
9611 range. */
9612 if (TYPE_UNSIGNED (TREE_TYPE (name))
9613 && integer_zerop (vr_value[i]->min)
9614 && integer_zerop (vr_value[i]->max))
9615 set_range_info (name,
9616 double_int_one,
9617 double_int::max_value
9618 (TYPE_PRECISION (TREE_TYPE (name)), true));
9619 else
9620 set_range_info (name,
9621 tree_to_double_int (vr_value[i]->max)
9622 + double_int_one,
9623 tree_to_double_int (vr_value[i]->min)
9624 - double_int_one);
9629 /* Free allocated memory. */
9630 for (i = 0; i < num_vr_values; i++)
9631 if (vr_value[i])
9633 BITMAP_FREE (vr_value[i]->equiv);
9634 free (vr_value[i]);
9637 free (vr_value);
9638 free (vr_phi_edge_counts);
9640 /* So that we can distinguish between VRP data being available
9641 and not available. */
9642 vr_value = NULL;
9643 vr_phi_edge_counts = NULL;
9647 /* Main entry point to VRP (Value Range Propagation). This pass is
9648 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9649 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9650 Programming Language Design and Implementation, pp. 67-78, 1995.
9651 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9653 This is essentially an SSA-CCP pass modified to deal with ranges
9654 instead of constants.
9656 While propagating ranges, we may find that two or more SSA name
9657 have equivalent, though distinct ranges. For instance,
9659 1 x_9 = p_3->a;
9660 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9661 3 if (p_4 == q_2)
9662 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9663 5 endif
9664 6 if (q_2)
9666 In the code above, pointer p_5 has range [q_2, q_2], but from the
9667 code we can also determine that p_5 cannot be NULL and, if q_2 had
9668 a non-varying range, p_5's range should also be compatible with it.
9670 These equivalences are created by two expressions: ASSERT_EXPR and
9671 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9672 result of another assertion, then we can use the fact that p_5 and
9673 p_4 are equivalent when evaluating p_5's range.
9675 Together with value ranges, we also propagate these equivalences
9676 between names so that we can take advantage of information from
9677 multiple ranges when doing final replacement. Note that this
9678 equivalency relation is transitive but not symmetric.
9680 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9681 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9682 in contexts where that assertion does not hold (e.g., in line 6).
9684 TODO, the main difference between this pass and Patterson's is that
9685 we do not propagate edge probabilities. We only compute whether
9686 edges can be taken or not. That is, instead of having a spectrum
9687 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9688 DON'T KNOW. In the future, it may be worthwhile to propagate
9689 probabilities to aid branch prediction. */
9691 static unsigned int
9692 execute_vrp (void)
9694 int i;
9695 edge e;
9696 switch_update *su;
9698 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9699 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9700 scev_initialize ();
9702 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9703 Inserting assertions may split edges which will invalidate
9704 EDGE_DFS_BACK. */
9705 insert_range_assertions ();
9707 to_remove_edges.create (10);
9708 to_update_switch_stmts.create (5);
9709 threadedge_initialize_values ();
9711 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9712 mark_dfs_back_edges ();
9714 vrp_initialize ();
9715 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
9716 vrp_finalize ();
9718 free_numbers_of_iterations_estimates ();
9720 /* ASSERT_EXPRs must be removed before finalizing jump threads
9721 as finalizing jump threads calls the CFG cleanup code which
9722 does not properly handle ASSERT_EXPRs. */
9723 remove_range_assertions ();
9725 /* If we exposed any new variables, go ahead and put them into
9726 SSA form now, before we handle jump threading. This simplifies
9727 interactions between rewriting of _DECL nodes into SSA form
9728 and rewriting SSA_NAME nodes into SSA form after block
9729 duplication and CFG manipulation. */
9730 update_ssa (TODO_update_ssa);
9732 finalize_jump_threads ();
9734 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9735 CFG in a broken state and requires a cfg_cleanup run. */
9736 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9737 remove_edge (e);
9738 /* Update SWITCH_EXPR case label vector. */
9739 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
9741 size_t j;
9742 size_t n = TREE_VEC_LENGTH (su->vec);
9743 tree label;
9744 gimple_switch_set_num_labels (su->stmt, n);
9745 for (j = 0; j < n; j++)
9746 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
9747 /* As we may have replaced the default label with a regular one
9748 make sure to make it a real default label again. This ensures
9749 optimal expansion. */
9750 label = gimple_switch_label (su->stmt, 0);
9751 CASE_LOW (label) = NULL_TREE;
9752 CASE_HIGH (label) = NULL_TREE;
9755 if (to_remove_edges.length () > 0)
9757 free_dominance_info (CDI_DOMINATORS);
9758 if (current_loops)
9759 loops_state_set (LOOPS_NEED_FIXUP);
9762 to_remove_edges.release ();
9763 to_update_switch_stmts.release ();
9764 threadedge_finalize_values ();
9766 scev_finalize ();
9767 loop_optimizer_finalize ();
9768 return 0;
9771 static bool
9772 gate_vrp (void)
9774 return flag_tree_vrp != 0;
9777 namespace {
9779 const pass_data pass_data_vrp =
9781 GIMPLE_PASS, /* type */
9782 "vrp", /* name */
9783 OPTGROUP_NONE, /* optinfo_flags */
9784 true, /* has_gate */
9785 true, /* has_execute */
9786 TV_TREE_VRP, /* tv_id */
9787 PROP_ssa, /* properties_required */
9788 0, /* properties_provided */
9789 0, /* properties_destroyed */
9790 0, /* todo_flags_start */
9791 ( TODO_cleanup_cfg | TODO_update_ssa
9792 | TODO_verify_ssa
9793 | TODO_verify_flow ), /* todo_flags_finish */
9796 class pass_vrp : public gimple_opt_pass
9798 public:
9799 pass_vrp (gcc::context *ctxt)
9800 : gimple_opt_pass (pass_data_vrp, ctxt)
9803 /* opt_pass methods: */
9804 opt_pass * clone () { return new pass_vrp (m_ctxt); }
9805 bool gate () { return gate_vrp (); }
9806 unsigned int execute () { return execute_vrp (); }
9808 }; // class pass_vrp
9810 } // anon namespace
9812 gimple_opt_pass *
9813 make_pass_vrp (gcc::context *ctxt)
9815 return new pass_vrp (ctxt);