gcc/testsuite/
[official-gcc.git] / gcc / tree-vrp.c
blob7f0489f3c255d59b76c863c6ffa9319a3e219b5d
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "flags.h"
26 #include "tree.h"
27 #include "stor-layout.h"
28 #include "calls.h"
29 #include "basic-block.h"
30 #include "tree-ssa-alias.h"
31 #include "internal-fn.h"
32 #include "gimple-fold.h"
33 #include "tree-eh.h"
34 #include "gimple-expr.h"
35 #include "is-a.h"
36 #include "gimple.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "gimple-ssa.h"
40 #include "tree-cfg.h"
41 #include "tree-phinodes.h"
42 #include "ssa-iterators.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
49 #include "tree-ssa.h"
50 #include "tree-pass.h"
51 #include "tree-dump.h"
52 #include "gimple-pretty-print.h"
53 #include "diagnostic-core.h"
54 #include "intl.h"
55 #include "cfgloop.h"
56 #include "tree-scalar-evolution.h"
57 #include "tree-ssa-propagate.h"
58 #include "tree-chrec.h"
59 #include "tree-ssa-threadupdate.h"
60 #include "expr.h"
61 #include "optabs.h"
62 #include "tree-ssa-threadedge.h"
63 #include "wide-int.h"
67 /* Range of values that can be associated with an SSA_NAME after VRP
68 has executed. */
69 struct value_range_d
71 /* Lattice value represented by this range. */
72 enum value_range_type type;
74 /* Minimum and maximum values represented by this range. These
75 values should be interpreted as follows:
77 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
78 be NULL.
80 - If TYPE == VR_RANGE then MIN holds the minimum value and
81 MAX holds the maximum value of the range [MIN, MAX].
83 - If TYPE == ANTI_RANGE the variable is known to NOT
84 take any values in the range [MIN, MAX]. */
85 tree min;
86 tree max;
88 /* Set of SSA names whose value ranges are equivalent to this one.
89 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
90 bitmap equiv;
93 typedef struct value_range_d value_range_t;
95 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
97 /* Set of SSA names found live during the RPO traversal of the function
98 for still active basic-blocks. */
99 static sbitmap *live;
101 /* Return true if the SSA name NAME is live on the edge E. */
103 static bool
104 live_on_edge (edge e, tree name)
106 return (live[e->dest->index]
107 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
110 /* Local functions. */
111 static int compare_values (tree val1, tree val2);
112 static int compare_values_warnv (tree val1, tree val2, bool *);
113 static void vrp_meet (value_range_t *, value_range_t *);
114 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
115 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
116 tree, tree, bool, bool *,
117 bool *);
119 /* Location information for ASSERT_EXPRs. Each instance of this
120 structure describes an ASSERT_EXPR for an SSA name. Since a single
121 SSA name may have more than one assertion associated with it, these
122 locations are kept in a linked list attached to the corresponding
123 SSA name. */
124 struct assert_locus_d
126 /* Basic block where the assertion would be inserted. */
127 basic_block bb;
129 /* Some assertions need to be inserted on an edge (e.g., assertions
130 generated by COND_EXPRs). In those cases, BB will be NULL. */
131 edge e;
133 /* Pointer to the statement that generated this assertion. */
134 gimple_stmt_iterator si;
136 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
137 enum tree_code comp_code;
139 /* Value being compared against. */
140 tree val;
142 /* Expression to compare. */
143 tree expr;
145 /* Next node in the linked list. */
146 struct assert_locus_d *next;
149 typedef struct assert_locus_d *assert_locus_t;
151 /* If bit I is present, it means that SSA name N_i has a list of
152 assertions that should be inserted in the IL. */
153 static bitmap need_assert_for;
155 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
156 holds a list of ASSERT_LOCUS_T nodes that describe where
157 ASSERT_EXPRs for SSA name N_I should be inserted. */
158 static assert_locus_t *asserts_for;
160 /* Value range array. After propagation, VR_VALUE[I] holds the range
161 of values that SSA name N_I may take. */
162 static unsigned num_vr_values;
163 static value_range_t **vr_value;
164 static bool values_propagated;
166 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
167 number of executable edges we saw the last time we visited the
168 node. */
169 static int *vr_phi_edge_counts;
171 typedef struct {
172 gimple stmt;
173 tree vec;
174 } switch_update;
176 static vec<edge> to_remove_edges;
177 static vec<switch_update> to_update_switch_stmts;
180 /* Return the maximum value for TYPE. */
182 static inline tree
183 vrp_val_max (const_tree type)
185 if (!INTEGRAL_TYPE_P (type))
186 return NULL_TREE;
188 return TYPE_MAX_VALUE (type);
191 /* Return the minimum value for TYPE. */
193 static inline tree
194 vrp_val_min (const_tree type)
196 if (!INTEGRAL_TYPE_P (type))
197 return NULL_TREE;
199 return TYPE_MIN_VALUE (type);
202 /* Return whether VAL is equal to the maximum value of its type. This
203 will be true for a positive overflow infinity. We can't do a
204 simple equality comparison with TYPE_MAX_VALUE because C typedefs
205 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
206 to the integer constant with the same value in the type. */
208 static inline bool
209 vrp_val_is_max (const_tree val)
211 tree type_max = vrp_val_max (TREE_TYPE (val));
212 return (val == type_max
213 || (type_max != NULL_TREE
214 && operand_equal_p (val, type_max, 0)));
217 /* Return whether VAL is equal to the minimum value of its type. This
218 will be true for a negative overflow infinity. */
220 static inline bool
221 vrp_val_is_min (const_tree val)
223 tree type_min = vrp_val_min (TREE_TYPE (val));
224 return (val == type_min
225 || (type_min != NULL_TREE
226 && operand_equal_p (val, type_min, 0)));
230 /* Return whether TYPE should use an overflow infinity distinct from
231 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
232 represent a signed overflow during VRP computations. An infinity
233 is distinct from a half-range, which will go from some number to
234 TYPE_{MIN,MAX}_VALUE. */
236 static inline bool
237 needs_overflow_infinity (const_tree type)
239 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
242 /* Return whether TYPE can support our overflow infinity
243 representation: we use the TREE_OVERFLOW flag, which only exists
244 for constants. If TYPE doesn't support this, we don't optimize
245 cases which would require signed overflow--we drop them to
246 VARYING. */
248 static inline bool
249 supports_overflow_infinity (const_tree type)
251 tree min = vrp_val_min (type), max = vrp_val_max (type);
252 #ifdef ENABLE_CHECKING
253 gcc_assert (needs_overflow_infinity (type));
254 #endif
255 return (min != NULL_TREE
256 && CONSTANT_CLASS_P (min)
257 && max != NULL_TREE
258 && CONSTANT_CLASS_P (max));
261 /* VAL is the maximum or minimum value of a type. Return a
262 corresponding overflow infinity. */
264 static inline tree
265 make_overflow_infinity (tree val)
267 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
268 val = copy_node (val);
269 TREE_OVERFLOW (val) = 1;
270 return val;
273 /* Return a negative overflow infinity for TYPE. */
275 static inline tree
276 negative_overflow_infinity (tree type)
278 gcc_checking_assert (supports_overflow_infinity (type));
279 return make_overflow_infinity (vrp_val_min (type));
282 /* Return a positive overflow infinity for TYPE. */
284 static inline tree
285 positive_overflow_infinity (tree type)
287 gcc_checking_assert (supports_overflow_infinity (type));
288 return make_overflow_infinity (vrp_val_max (type));
291 /* Return whether VAL is a negative overflow infinity. */
293 static inline bool
294 is_negative_overflow_infinity (const_tree val)
296 return (TREE_OVERFLOW_P (val)
297 && needs_overflow_infinity (TREE_TYPE (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 (TREE_OVERFLOW_P (val)
307 && needs_overflow_infinity (TREE_TYPE (val))
308 && vrp_val_is_max (val));
311 /* Return whether VAL is a positive or negative overflow infinity. */
313 static inline bool
314 is_overflow_infinity (const_tree val)
316 return (TREE_OVERFLOW_P (val)
317 && needs_overflow_infinity (TREE_TYPE (val))
318 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
321 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
323 static inline bool
324 stmt_overflow_infinity (gimple stmt)
326 if (is_gimple_assign (stmt)
327 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
328 GIMPLE_SINGLE_RHS)
329 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
330 return false;
333 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
334 the same value with TREE_OVERFLOW clear. This can be used to avoid
335 confusing a regular value with an overflow value. */
337 static inline tree
338 avoid_overflow_infinity (tree val)
340 if (!is_overflow_infinity (val))
341 return val;
343 if (vrp_val_is_max (val))
344 return vrp_val_max (TREE_TYPE (val));
345 else
347 gcc_checking_assert (vrp_val_is_min (val));
348 return vrp_val_min (TREE_TYPE (val));
353 /* Return true if ARG is marked with the nonnull attribute in the
354 current function signature. */
356 static bool
357 nonnull_arg_p (const_tree arg)
359 tree t, attrs, fntype;
360 unsigned HOST_WIDE_INT arg_num;
362 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
364 /* The static chain decl is always non null. */
365 if (arg == cfun->static_chain_decl)
366 return true;
368 fntype = TREE_TYPE (current_function_decl);
369 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
371 attrs = lookup_attribute ("nonnull", attrs);
373 /* If "nonnull" wasn't specified, we know nothing about the argument. */
374 if (attrs == NULL_TREE)
375 return false;
377 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
378 if (TREE_VALUE (attrs) == NULL_TREE)
379 return true;
381 /* Get the position number for ARG in the function signature. */
382 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
384 t = DECL_CHAIN (t), arg_num++)
386 if (t == arg)
387 break;
390 gcc_assert (t == arg);
392 /* Now see if ARG_NUM is mentioned in the nonnull list. */
393 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
395 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
396 return true;
400 return false;
404 /* Set value range VR to VR_UNDEFINED. */
406 static inline void
407 set_value_range_to_undefined (value_range_t *vr)
409 vr->type = VR_UNDEFINED;
410 vr->min = vr->max = NULL_TREE;
411 if (vr->equiv)
412 bitmap_clear (vr->equiv);
416 /* Set value range VR to VR_VARYING. */
418 static inline void
419 set_value_range_to_varying (value_range_t *vr)
421 vr->type = VR_VARYING;
422 vr->min = vr->max = NULL_TREE;
423 if (vr->equiv)
424 bitmap_clear (vr->equiv);
428 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
430 static void
431 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
432 tree max, bitmap equiv)
434 #if defined ENABLE_CHECKING
435 /* Check the validity of the range. */
436 if (t == VR_RANGE || t == VR_ANTI_RANGE)
438 int cmp;
440 gcc_assert (min && max);
442 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
443 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
445 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
446 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
448 cmp = compare_values (min, max);
449 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
451 if (needs_overflow_infinity (TREE_TYPE (min)))
452 gcc_assert (!is_overflow_infinity (min)
453 || !is_overflow_infinity (max));
456 if (t == VR_UNDEFINED || t == VR_VARYING)
457 gcc_assert (min == NULL_TREE && max == NULL_TREE);
459 if (t == VR_UNDEFINED || t == VR_VARYING)
460 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
461 #endif
463 vr->type = t;
464 vr->min = min;
465 vr->max = max;
467 /* Since updating the equivalence set involves deep copying the
468 bitmaps, only do it if absolutely necessary. */
469 if (vr->equiv == NULL
470 && equiv != NULL)
471 vr->equiv = BITMAP_ALLOC (NULL);
473 if (equiv != vr->equiv)
475 if (equiv && !bitmap_empty_p (equiv))
476 bitmap_copy (vr->equiv, equiv);
477 else
478 bitmap_clear (vr->equiv);
483 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
484 This means adjusting T, MIN and MAX representing the case of a
485 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
486 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
487 In corner cases where MAX+1 or MIN-1 wraps this will fall back
488 to varying.
489 This routine exists to ease canonicalization in the case where we
490 extract ranges from var + CST op limit. */
492 static void
493 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
494 tree min, tree max, bitmap equiv)
496 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
497 if (t == VR_UNDEFINED)
499 set_value_range_to_undefined (vr);
500 return;
502 else if (t == VR_VARYING)
504 set_value_range_to_varying (vr);
505 return;
508 /* Nothing to canonicalize for symbolic ranges. */
509 if (TREE_CODE (min) != INTEGER_CST
510 || TREE_CODE (max) != INTEGER_CST)
512 set_value_range (vr, t, min, max, equiv);
513 return;
516 /* Wrong order for min and max, to swap them and the VR type we need
517 to adjust them. */
518 if (tree_int_cst_lt (max, min))
520 tree one, tmp;
522 /* For one bit precision if max < min, then the swapped
523 range covers all values, so for VR_RANGE it is varying and
524 for VR_ANTI_RANGE empty range, so drop to varying as well. */
525 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
527 set_value_range_to_varying (vr);
528 return;
531 one = build_int_cst (TREE_TYPE (min), 1);
532 tmp = int_const_binop (PLUS_EXPR, max, one);
533 max = int_const_binop (MINUS_EXPR, min, one);
534 min = tmp;
536 /* There's one corner case, if we had [C+1, C] before we now have
537 that again. But this represents an empty value range, so drop
538 to varying in this case. */
539 if (tree_int_cst_lt (max, min))
541 set_value_range_to_varying (vr);
542 return;
545 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
548 /* Anti-ranges that can be represented as ranges should be so. */
549 if (t == VR_ANTI_RANGE)
551 bool is_min = vrp_val_is_min (min);
552 bool is_max = vrp_val_is_max (max);
554 if (is_min && is_max)
556 /* We cannot deal with empty ranges, drop to varying.
557 ??? This could be VR_UNDEFINED instead. */
558 set_value_range_to_varying (vr);
559 return;
561 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
562 && (is_min || is_max))
564 /* Non-empty boolean ranges can always be represented
565 as a singleton range. */
566 if (is_min)
567 min = max = vrp_val_max (TREE_TYPE (min));
568 else
569 min = max = vrp_val_min (TREE_TYPE (min));
570 t = VR_RANGE;
572 else if (is_min
573 /* As a special exception preserve non-null ranges. */
574 && !(TYPE_UNSIGNED (TREE_TYPE (min))
575 && integer_zerop (max)))
577 tree one = build_int_cst (TREE_TYPE (max), 1);
578 min = int_const_binop (PLUS_EXPR, max, one);
579 max = vrp_val_max (TREE_TYPE (max));
580 t = VR_RANGE;
582 else if (is_max)
584 tree one = build_int_cst (TREE_TYPE (min), 1);
585 max = int_const_binop (MINUS_EXPR, min, one);
586 min = vrp_val_min (TREE_TYPE (min));
587 t = VR_RANGE;
591 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
592 if (needs_overflow_infinity (TREE_TYPE (min))
593 && is_overflow_infinity (min)
594 && is_overflow_infinity (max))
596 set_value_range_to_varying (vr);
597 return;
600 set_value_range (vr, t, min, max, equiv);
603 /* Copy value range FROM into value range TO. */
605 static inline void
606 copy_value_range (value_range_t *to, value_range_t *from)
608 set_value_range (to, from->type, from->min, from->max, from->equiv);
611 /* Set value range VR to a single value. This function is only called
612 with values we get from statements, and exists to clear the
613 TREE_OVERFLOW flag so that we don't think we have an overflow
614 infinity when we shouldn't. */
616 static inline void
617 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
619 gcc_assert (is_gimple_min_invariant (val));
620 if (TREE_OVERFLOW_P (val))
621 val = drop_tree_overflow (val);
622 set_value_range (vr, VR_RANGE, val, val, equiv);
625 /* Set value range VR to a non-negative range of type TYPE.
626 OVERFLOW_INFINITY indicates whether to use an overflow infinity
627 rather than TYPE_MAX_VALUE; this should be true if we determine
628 that the range is nonnegative based on the assumption that signed
629 overflow does not occur. */
631 static inline void
632 set_value_range_to_nonnegative (value_range_t *vr, tree type,
633 bool overflow_infinity)
635 tree zero;
637 if (overflow_infinity && !supports_overflow_infinity (type))
639 set_value_range_to_varying (vr);
640 return;
643 zero = build_int_cst (type, 0);
644 set_value_range (vr, VR_RANGE, zero,
645 (overflow_infinity
646 ? positive_overflow_infinity (type)
647 : TYPE_MAX_VALUE (type)),
648 vr->equiv);
651 /* Set value range VR to a non-NULL range of type TYPE. */
653 static inline void
654 set_value_range_to_nonnull (value_range_t *vr, tree type)
656 tree zero = build_int_cst (type, 0);
657 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
661 /* Set value range VR to a NULL range of type TYPE. */
663 static inline void
664 set_value_range_to_null (value_range_t *vr, tree type)
666 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
670 /* Set value range VR to a range of a truthvalue of type TYPE. */
672 static inline void
673 set_value_range_to_truthvalue (value_range_t *vr, tree type)
675 if (TYPE_PRECISION (type) == 1)
676 set_value_range_to_varying (vr);
677 else
678 set_value_range (vr, VR_RANGE,
679 build_int_cst (type, 0), build_int_cst (type, 1),
680 vr->equiv);
684 /* If abs (min) < abs (max), set VR to [-max, max], if
685 abs (min) >= abs (max), set VR to [-min, min]. */
687 static void
688 abs_extent_range (value_range_t *vr, tree min, tree max)
690 int cmp;
692 gcc_assert (TREE_CODE (min) == INTEGER_CST);
693 gcc_assert (TREE_CODE (max) == INTEGER_CST);
694 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
695 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
696 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
697 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
698 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
700 set_value_range_to_varying (vr);
701 return;
703 cmp = compare_values (min, max);
704 if (cmp == -1)
705 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
706 else if (cmp == 0 || cmp == 1)
708 max = min;
709 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
711 else
713 set_value_range_to_varying (vr);
714 return;
716 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
720 /* Return value range information for VAR.
722 If we have no values ranges recorded (ie, VRP is not running), then
723 return NULL. Otherwise create an empty range if none existed for VAR. */
725 static value_range_t *
726 get_value_range (const_tree var)
728 static const struct value_range_d vr_const_varying
729 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
730 value_range_t *vr;
731 tree sym;
732 unsigned ver = SSA_NAME_VERSION (var);
734 /* If we have no recorded ranges, then return NULL. */
735 if (! vr_value)
736 return NULL;
738 /* If we query the range for a new SSA name return an unmodifiable VARYING.
739 We should get here at most from the substitute-and-fold stage which
740 will never try to change values. */
741 if (ver >= num_vr_values)
742 return CONST_CAST (value_range_t *, &vr_const_varying);
744 vr = vr_value[ver];
745 if (vr)
746 return vr;
748 /* After propagation finished do not allocate new value-ranges. */
749 if (values_propagated)
750 return CONST_CAST (value_range_t *, &vr_const_varying);
752 /* Create a default value range. */
753 vr_value[ver] = vr = XCNEW (value_range_t);
755 /* Defer allocating the equivalence set. */
756 vr->equiv = NULL;
758 /* If VAR is a default definition of a parameter, the variable can
759 take any value in VAR's type. */
760 if (SSA_NAME_IS_DEFAULT_DEF (var))
762 sym = SSA_NAME_VAR (var);
763 if (TREE_CODE (sym) == PARM_DECL)
765 /* Try to use the "nonnull" attribute to create ~[0, 0]
766 anti-ranges for pointers. Note that this is only valid with
767 default definitions of PARM_DECLs. */
768 if (POINTER_TYPE_P (TREE_TYPE (sym))
769 && nonnull_arg_p (sym))
770 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
771 else
772 set_value_range_to_varying (vr);
774 else if (TREE_CODE (sym) == RESULT_DECL
775 && DECL_BY_REFERENCE (sym))
776 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
779 return vr;
782 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
784 static inline bool
785 vrp_operand_equal_p (const_tree val1, const_tree val2)
787 if (val1 == val2)
788 return true;
789 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
790 return false;
791 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
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)
1149 return tree_int_cst_lt (val, val2);
1150 else
1152 tree tcmp;
1154 fold_defer_overflow_warnings ();
1156 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1158 fold_undefer_and_ignore_overflow_warnings ();
1160 if (!tcmp
1161 || TREE_CODE (tcmp) != INTEGER_CST)
1162 return -2;
1164 if (!integer_zerop (tcmp))
1165 return 1;
1168 /* val >= val2, not considering overflow infinity. */
1169 if (is_negative_overflow_infinity (val))
1170 return is_negative_overflow_infinity (val2) ? 0 : 1;
1171 else if (is_positive_overflow_infinity (val2))
1172 return is_positive_overflow_infinity (val) ? 0 : 1;
1174 return 0;
1177 /* Compare two values VAL1 and VAL2. Return
1179 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1180 -1 if VAL1 < VAL2,
1181 0 if VAL1 == VAL2,
1182 +1 if VAL1 > VAL2, and
1183 +2 if VAL1 != VAL2
1185 This is similar to tree_int_cst_compare but supports pointer values
1186 and values that cannot be compared at compile time.
1188 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1189 true if the return value is only valid if we assume that signed
1190 overflow is undefined. */
1192 static int
1193 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1195 if (val1 == val2)
1196 return 0;
1198 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1199 both integers. */
1200 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1201 == POINTER_TYPE_P (TREE_TYPE (val2)));
1202 /* Convert the two values into the same type. This is needed because
1203 sizetype causes sign extension even for unsigned types. */
1204 val2 = fold_convert (TREE_TYPE (val1), val2);
1205 STRIP_USELESS_TYPE_CONVERSION (val2);
1207 if ((TREE_CODE (val1) == SSA_NAME
1208 || TREE_CODE (val1) == PLUS_EXPR
1209 || TREE_CODE (val1) == MINUS_EXPR)
1210 && (TREE_CODE (val2) == SSA_NAME
1211 || TREE_CODE (val2) == PLUS_EXPR
1212 || TREE_CODE (val2) == MINUS_EXPR))
1214 tree n1, c1, n2, c2;
1215 enum tree_code code1, code2;
1217 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1218 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1219 same name, return -2. */
1220 if (TREE_CODE (val1) == SSA_NAME)
1222 code1 = SSA_NAME;
1223 n1 = val1;
1224 c1 = NULL_TREE;
1226 else
1228 code1 = TREE_CODE (val1);
1229 n1 = TREE_OPERAND (val1, 0);
1230 c1 = TREE_OPERAND (val1, 1);
1231 if (tree_int_cst_sgn (c1) == -1)
1233 if (is_negative_overflow_infinity (c1))
1234 return -2;
1235 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1236 if (!c1)
1237 return -2;
1238 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1242 if (TREE_CODE (val2) == SSA_NAME)
1244 code2 = SSA_NAME;
1245 n2 = val2;
1246 c2 = NULL_TREE;
1248 else
1250 code2 = TREE_CODE (val2);
1251 n2 = TREE_OPERAND (val2, 0);
1252 c2 = TREE_OPERAND (val2, 1);
1253 if (tree_int_cst_sgn (c2) == -1)
1255 if (is_negative_overflow_infinity (c2))
1256 return -2;
1257 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1258 if (!c2)
1259 return -2;
1260 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1264 /* Both values must use the same name. */
1265 if (n1 != n2)
1266 return -2;
1268 if (code1 == SSA_NAME
1269 && code2 == SSA_NAME)
1270 /* NAME == NAME */
1271 return 0;
1273 /* If overflow is defined we cannot simplify more. */
1274 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1275 return -2;
1277 if (strict_overflow_p != NULL
1278 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1279 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1280 *strict_overflow_p = true;
1282 if (code1 == SSA_NAME)
1284 if (code2 == PLUS_EXPR)
1285 /* NAME < NAME + CST */
1286 return -1;
1287 else if (code2 == MINUS_EXPR)
1288 /* NAME > NAME - CST */
1289 return 1;
1291 else if (code1 == PLUS_EXPR)
1293 if (code2 == SSA_NAME)
1294 /* NAME + CST > NAME */
1295 return 1;
1296 else if (code2 == PLUS_EXPR)
1297 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1298 return compare_values_warnv (c1, c2, strict_overflow_p);
1299 else if (code2 == MINUS_EXPR)
1300 /* NAME + CST1 > NAME - CST2 */
1301 return 1;
1303 else if (code1 == MINUS_EXPR)
1305 if (code2 == SSA_NAME)
1306 /* NAME - CST < NAME */
1307 return -1;
1308 else if (code2 == PLUS_EXPR)
1309 /* NAME - CST1 < NAME + CST2 */
1310 return -1;
1311 else if (code2 == MINUS_EXPR)
1312 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1313 C1 and C2 are swapped in the call to compare_values. */
1314 return compare_values_warnv (c2, c1, strict_overflow_p);
1317 gcc_unreachable ();
1320 /* We cannot compare non-constants. */
1321 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1322 return -2;
1324 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1326 /* We cannot compare overflowed values, except for overflow
1327 infinities. */
1328 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1330 if (strict_overflow_p != NULL)
1331 *strict_overflow_p = true;
1332 if (is_negative_overflow_infinity (val1))
1333 return is_negative_overflow_infinity (val2) ? 0 : -1;
1334 else if (is_negative_overflow_infinity (val2))
1335 return 1;
1336 else if (is_positive_overflow_infinity (val1))
1337 return is_positive_overflow_infinity (val2) ? 0 : 1;
1338 else if (is_positive_overflow_infinity (val2))
1339 return -1;
1340 return -2;
1343 return tree_int_cst_compare (val1, val2);
1345 else
1347 tree t;
1349 /* First see if VAL1 and VAL2 are not the same. */
1350 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1351 return 0;
1353 /* If VAL1 is a lower address than VAL2, return -1. */
1354 if (operand_less_p (val1, val2) == 1)
1355 return -1;
1357 /* If VAL1 is a higher address than VAL2, return +1. */
1358 if (operand_less_p (val2, val1) == 1)
1359 return 1;
1361 /* If VAL1 is different than VAL2, return +2.
1362 For integer constants we either have already returned -1 or 1
1363 or they are equivalent. We still might succeed in proving
1364 something about non-trivial operands. */
1365 if (TREE_CODE (val1) != INTEGER_CST
1366 || TREE_CODE (val2) != INTEGER_CST)
1368 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1369 if (t && integer_onep (t))
1370 return 2;
1373 return -2;
1377 /* Compare values like compare_values_warnv, but treat comparisons of
1378 nonconstants which rely on undefined overflow as incomparable. */
1380 static int
1381 compare_values (tree val1, tree val2)
1383 bool sop;
1384 int ret;
1386 sop = false;
1387 ret = compare_values_warnv (val1, val2, &sop);
1388 if (sop
1389 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1390 ret = -2;
1391 return ret;
1395 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1396 0 if VAL is not inside [MIN, MAX],
1397 -2 if we cannot tell either way.
1399 Benchmark compile/20001226-1.c compilation time after changing this
1400 function. */
1402 static inline int
1403 value_inside_range (tree val, tree min, tree max)
1405 int cmp1, cmp2;
1407 cmp1 = operand_less_p (val, min);
1408 if (cmp1 == -2)
1409 return -2;
1410 if (cmp1 == 1)
1411 return 0;
1413 cmp2 = operand_less_p (max, val);
1414 if (cmp2 == -2)
1415 return -2;
1417 return !cmp2;
1421 /* Return true if value ranges VR0 and VR1 have a non-empty
1422 intersection.
1424 Benchmark compile/20001226-1.c compilation time after changing this
1425 function.
1428 static inline bool
1429 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1431 /* The value ranges do not intersect if the maximum of the first range is
1432 less than the minimum of the second range or vice versa.
1433 When those relations are unknown, we can't do any better. */
1434 if (operand_less_p (vr0->max, vr1->min) != 0)
1435 return false;
1436 if (operand_less_p (vr1->max, vr0->min) != 0)
1437 return false;
1438 return true;
1442 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1443 include the value zero, -2 if we cannot tell. */
1445 static inline int
1446 range_includes_zero_p (tree min, tree max)
1448 tree zero = build_int_cst (TREE_TYPE (min), 0);
1449 return value_inside_range (zero, min, max);
1452 /* Return true if *VR is know to only contain nonnegative values. */
1454 static inline bool
1455 value_range_nonnegative_p (value_range_t *vr)
1457 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1458 which would return a useful value should be encoded as a
1459 VR_RANGE. */
1460 if (vr->type == VR_RANGE)
1462 int result = compare_values (vr->min, integer_zero_node);
1463 return (result == 0 || result == 1);
1466 return false;
1469 /* If *VR has a value rante that is a single constant value return that,
1470 otherwise return NULL_TREE. */
1472 static tree
1473 value_range_constant_singleton (value_range_t *vr)
1475 if (vr->type == VR_RANGE
1476 && operand_equal_p (vr->min, vr->max, 0)
1477 && is_gimple_min_invariant (vr->min))
1478 return vr->min;
1480 return NULL_TREE;
1483 /* If OP has a value range with a single constant value return that,
1484 otherwise return NULL_TREE. This returns OP itself if OP is a
1485 constant. */
1487 static tree
1488 op_with_constant_singleton_value_range (tree op)
1490 if (is_gimple_min_invariant (op))
1491 return op;
1493 if (TREE_CODE (op) != SSA_NAME)
1494 return NULL_TREE;
1496 return value_range_constant_singleton (get_value_range (op));
1499 /* Return true if op is in a boolean [0, 1] value-range. */
1501 static bool
1502 op_with_boolean_value_range_p (tree op)
1504 value_range_t *vr;
1506 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1507 return true;
1509 if (integer_zerop (op)
1510 || integer_onep (op))
1511 return true;
1513 if (TREE_CODE (op) != SSA_NAME)
1514 return false;
1516 vr = get_value_range (op);
1517 return (vr->type == VR_RANGE
1518 && integer_zerop (vr->min)
1519 && integer_onep (vr->max));
1522 /* Extract value range information from an ASSERT_EXPR EXPR and store
1523 it in *VR_P. */
1525 static void
1526 extract_range_from_assert (value_range_t *vr_p, tree expr)
1528 tree var, cond, limit, min, max, type;
1529 value_range_t *limit_vr;
1530 enum tree_code cond_code;
1532 var = ASSERT_EXPR_VAR (expr);
1533 cond = ASSERT_EXPR_COND (expr);
1535 gcc_assert (COMPARISON_CLASS_P (cond));
1537 /* Find VAR in the ASSERT_EXPR conditional. */
1538 if (var == TREE_OPERAND (cond, 0)
1539 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1540 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1542 /* If the predicate is of the form VAR COMP LIMIT, then we just
1543 take LIMIT from the RHS and use the same comparison code. */
1544 cond_code = TREE_CODE (cond);
1545 limit = TREE_OPERAND (cond, 1);
1546 cond = TREE_OPERAND (cond, 0);
1548 else
1550 /* If the predicate is of the form LIMIT COMP VAR, then we need
1551 to flip around the comparison code to create the proper range
1552 for VAR. */
1553 cond_code = swap_tree_comparison (TREE_CODE (cond));
1554 limit = TREE_OPERAND (cond, 0);
1555 cond = TREE_OPERAND (cond, 1);
1558 limit = avoid_overflow_infinity (limit);
1560 type = TREE_TYPE (var);
1561 gcc_assert (limit != var);
1563 /* For pointer arithmetic, we only keep track of pointer equality
1564 and inequality. */
1565 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1567 set_value_range_to_varying (vr_p);
1568 return;
1571 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1572 try to use LIMIT's range to avoid creating symbolic ranges
1573 unnecessarily. */
1574 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1576 /* LIMIT's range is only interesting if it has any useful information. */
1577 if (limit_vr
1578 && (limit_vr->type == VR_UNDEFINED
1579 || limit_vr->type == VR_VARYING
1580 || symbolic_range_p (limit_vr)))
1581 limit_vr = NULL;
1583 /* Initially, the new range has the same set of equivalences of
1584 VAR's range. This will be revised before returning the final
1585 value. Since assertions may be chained via mutually exclusive
1586 predicates, we will need to trim the set of equivalences before
1587 we are done. */
1588 gcc_assert (vr_p->equiv == NULL);
1589 add_equivalence (&vr_p->equiv, var);
1591 /* Extract a new range based on the asserted comparison for VAR and
1592 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1593 will only use it for equality comparisons (EQ_EXPR). For any
1594 other kind of assertion, we cannot derive a range from LIMIT's
1595 anti-range that can be used to describe the new range. For
1596 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1597 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1598 no single range for x_2 that could describe LE_EXPR, so we might
1599 as well build the range [b_4, +INF] for it.
1600 One special case we handle is extracting a range from a
1601 range test encoded as (unsigned)var + CST <= limit. */
1602 if (TREE_CODE (cond) == NOP_EXPR
1603 || TREE_CODE (cond) == PLUS_EXPR)
1605 if (TREE_CODE (cond) == PLUS_EXPR)
1607 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1608 TREE_OPERAND (cond, 1));
1609 max = int_const_binop (PLUS_EXPR, limit, min);
1610 cond = TREE_OPERAND (cond, 0);
1612 else
1614 min = build_int_cst (TREE_TYPE (var), 0);
1615 max = limit;
1618 /* Make sure to not set TREE_OVERFLOW on the final type
1619 conversion. We are willingly interpreting large positive
1620 unsigned values as negative singed values here. */
1621 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1622 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1624 /* We can transform a max, min range to an anti-range or
1625 vice-versa. Use set_and_canonicalize_value_range which does
1626 this for us. */
1627 if (cond_code == LE_EXPR)
1628 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1629 min, max, vr_p->equiv);
1630 else if (cond_code == GT_EXPR)
1631 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1632 min, max, vr_p->equiv);
1633 else
1634 gcc_unreachable ();
1636 else if (cond_code == EQ_EXPR)
1638 enum value_range_type range_type;
1640 if (limit_vr)
1642 range_type = limit_vr->type;
1643 min = limit_vr->min;
1644 max = limit_vr->max;
1646 else
1648 range_type = VR_RANGE;
1649 min = limit;
1650 max = limit;
1653 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1655 /* When asserting the equality VAR == LIMIT and LIMIT is another
1656 SSA name, the new range will also inherit the equivalence set
1657 from LIMIT. */
1658 if (TREE_CODE (limit) == SSA_NAME)
1659 add_equivalence (&vr_p->equiv, limit);
1661 else if (cond_code == NE_EXPR)
1663 /* As described above, when LIMIT's range is an anti-range and
1664 this assertion is an inequality (NE_EXPR), then we cannot
1665 derive anything from the anti-range. For instance, if
1666 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1667 not imply that VAR's range is [0, 0]. So, in the case of
1668 anti-ranges, we just assert the inequality using LIMIT and
1669 not its anti-range.
1671 If LIMIT_VR is a range, we can only use it to build a new
1672 anti-range if LIMIT_VR is a single-valued range. For
1673 instance, if LIMIT_VR is [0, 1], the predicate
1674 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1675 Rather, it means that for value 0 VAR should be ~[0, 0]
1676 and for value 1, VAR should be ~[1, 1]. We cannot
1677 represent these ranges.
1679 The only situation in which we can build a valid
1680 anti-range is when LIMIT_VR is a single-valued range
1681 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1682 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1683 if (limit_vr
1684 && limit_vr->type == VR_RANGE
1685 && compare_values (limit_vr->min, limit_vr->max) == 0)
1687 min = limit_vr->min;
1688 max = limit_vr->max;
1690 else
1692 /* In any other case, we cannot use LIMIT's range to build a
1693 valid anti-range. */
1694 min = max = limit;
1697 /* If MIN and MAX cover the whole range for their type, then
1698 just use the original LIMIT. */
1699 if (INTEGRAL_TYPE_P (type)
1700 && vrp_val_is_min (min)
1701 && vrp_val_is_max (max))
1702 min = max = limit;
1704 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1705 min, max, vr_p->equiv);
1707 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1709 min = TYPE_MIN_VALUE (type);
1711 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1712 max = limit;
1713 else
1715 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1716 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1717 LT_EXPR. */
1718 max = limit_vr->max;
1721 /* If the maximum value forces us to be out of bounds, simply punt.
1722 It would be pointless to try and do anything more since this
1723 all should be optimized away above us. */
1724 if ((cond_code == LT_EXPR
1725 && compare_values (max, min) == 0)
1726 || is_overflow_infinity (max))
1727 set_value_range_to_varying (vr_p);
1728 else
1730 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1731 if (cond_code == LT_EXPR)
1733 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1734 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1735 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1736 build_int_cst (TREE_TYPE (max), -1));
1737 else
1738 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1739 build_int_cst (TREE_TYPE (max), 1));
1740 if (EXPR_P (max))
1741 TREE_NO_WARNING (max) = 1;
1744 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1747 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1749 max = TYPE_MAX_VALUE (type);
1751 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1752 min = limit;
1753 else
1755 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1756 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1757 GT_EXPR. */
1758 min = limit_vr->min;
1761 /* If the minimum value forces us to be out of bounds, simply punt.
1762 It would be pointless to try and do anything more since this
1763 all should be optimized away above us. */
1764 if ((cond_code == GT_EXPR
1765 && compare_values (min, max) == 0)
1766 || is_overflow_infinity (min))
1767 set_value_range_to_varying (vr_p);
1768 else
1770 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1771 if (cond_code == GT_EXPR)
1773 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1774 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1775 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1776 build_int_cst (TREE_TYPE (min), -1));
1777 else
1778 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1779 build_int_cst (TREE_TYPE (min), 1));
1780 if (EXPR_P (min))
1781 TREE_NO_WARNING (min) = 1;
1784 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1787 else
1788 gcc_unreachable ();
1790 /* Finally intersect the new range with what we already know about var. */
1791 vrp_intersect_ranges (vr_p, get_value_range (var));
1795 /* Extract range information from SSA name VAR and store it in VR. If
1796 VAR has an interesting range, use it. Otherwise, create the
1797 range [VAR, VAR] and return it. This is useful in situations where
1798 we may have conditionals testing values of VARYING names. For
1799 instance,
1801 x_3 = y_5;
1802 if (x_3 > y_5)
1805 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1806 always false. */
1808 static void
1809 extract_range_from_ssa_name (value_range_t *vr, tree var)
1811 value_range_t *var_vr = get_value_range (var);
1813 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1814 copy_value_range (vr, var_vr);
1815 else
1816 set_value_range (vr, VR_RANGE, var, var, NULL);
1818 add_equivalence (&vr->equiv, var);
1822 /* Wrapper around int_const_binop. If the operation overflows and we
1823 are not using wrapping arithmetic, then adjust the result to be
1824 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1825 NULL_TREE if we need to use an overflow infinity representation but
1826 the type does not support it. */
1828 static tree
1829 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1831 tree res;
1833 res = int_const_binop (code, val1, val2);
1835 /* If we are using unsigned arithmetic, operate symbolically
1836 on -INF and +INF as int_const_binop only handles signed overflow. */
1837 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1839 int checkz = compare_values (res, val1);
1840 bool overflow = false;
1842 /* Ensure that res = val1 [+*] val2 >= val1
1843 or that res = val1 - val2 <= val1. */
1844 if ((code == PLUS_EXPR
1845 && !(checkz == 1 || checkz == 0))
1846 || (code == MINUS_EXPR
1847 && !(checkz == 0 || checkz == -1)))
1849 overflow = true;
1851 /* Checking for multiplication overflow is done by dividing the
1852 output of the multiplication by the first input of the
1853 multiplication. If the result of that division operation is
1854 not equal to the second input of the multiplication, then the
1855 multiplication overflowed. */
1856 else if (code == MULT_EXPR && !integer_zerop (val1))
1858 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1859 res,
1860 val1);
1861 int check = compare_values (tmp, val2);
1863 if (check != 0)
1864 overflow = true;
1867 if (overflow)
1869 res = copy_node (res);
1870 TREE_OVERFLOW (res) = 1;
1874 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1875 /* If the singed operation wraps then int_const_binop has done
1876 everything we want. */
1878 /* Signed division of -1/0 overflows and by the time it gets here
1879 returns NULL_TREE. */
1880 else if (!res)
1881 return NULL_TREE;
1882 else if ((TREE_OVERFLOW (res)
1883 && !TREE_OVERFLOW (val1)
1884 && !TREE_OVERFLOW (val2))
1885 || is_overflow_infinity (val1)
1886 || is_overflow_infinity (val2))
1888 /* If the operation overflowed but neither VAL1 nor VAL2 are
1889 overflown, return -INF or +INF depending on the operation
1890 and the combination of signs of the operands. */
1891 int sgn1 = tree_int_cst_sgn (val1);
1892 int sgn2 = tree_int_cst_sgn (val2);
1894 if (needs_overflow_infinity (TREE_TYPE (res))
1895 && !supports_overflow_infinity (TREE_TYPE (res)))
1896 return NULL_TREE;
1898 /* We have to punt on adding infinities of different signs,
1899 since we can't tell what the sign of the result should be.
1900 Likewise for subtracting infinities of the same sign. */
1901 if (((code == PLUS_EXPR && sgn1 != sgn2)
1902 || (code == MINUS_EXPR && sgn1 == sgn2))
1903 && is_overflow_infinity (val1)
1904 && is_overflow_infinity (val2))
1905 return NULL_TREE;
1907 /* Don't try to handle division or shifting of infinities. */
1908 if ((code == TRUNC_DIV_EXPR
1909 || code == FLOOR_DIV_EXPR
1910 || code == CEIL_DIV_EXPR
1911 || code == EXACT_DIV_EXPR
1912 || code == ROUND_DIV_EXPR
1913 || code == RSHIFT_EXPR)
1914 && (is_overflow_infinity (val1)
1915 || is_overflow_infinity (val2)))
1916 return NULL_TREE;
1918 /* Notice that we only need to handle the restricted set of
1919 operations handled by extract_range_from_binary_expr.
1920 Among them, only multiplication, addition and subtraction
1921 can yield overflow without overflown operands because we
1922 are working with integral types only... except in the
1923 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1924 for division too. */
1926 /* For multiplication, the sign of the overflow is given
1927 by the comparison of the signs of the operands. */
1928 if ((code == MULT_EXPR && sgn1 == sgn2)
1929 /* For addition, the operands must be of the same sign
1930 to yield an overflow. Its sign is therefore that
1931 of one of the operands, for example the first. For
1932 infinite operands X + -INF is negative, not positive. */
1933 || (code == PLUS_EXPR
1934 && (sgn1 >= 0
1935 ? !is_negative_overflow_infinity (val2)
1936 : is_positive_overflow_infinity (val2)))
1937 /* For subtraction, non-infinite operands must be of
1938 different signs to yield an overflow. Its sign is
1939 therefore that of the first operand or the opposite of
1940 that of the second operand. A first operand of 0 counts
1941 as positive here, for the corner case 0 - (-INF), which
1942 overflows, but must yield +INF. For infinite operands 0
1943 - INF is negative, not positive. */
1944 || (code == MINUS_EXPR
1945 && (sgn1 >= 0
1946 ? !is_positive_overflow_infinity (val2)
1947 : is_negative_overflow_infinity (val2)))
1948 /* We only get in here with positive shift count, so the
1949 overflow direction is the same as the sign of val1.
1950 Actually rshift does not overflow at all, but we only
1951 handle the case of shifting overflowed -INF and +INF. */
1952 || (code == RSHIFT_EXPR
1953 && sgn1 >= 0)
1954 /* For division, the only case is -INF / -1 = +INF. */
1955 || code == TRUNC_DIV_EXPR
1956 || code == FLOOR_DIV_EXPR
1957 || code == CEIL_DIV_EXPR
1958 || code == EXACT_DIV_EXPR
1959 || code == ROUND_DIV_EXPR)
1960 return (needs_overflow_infinity (TREE_TYPE (res))
1961 ? positive_overflow_infinity (TREE_TYPE (res))
1962 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1963 else
1964 return (needs_overflow_infinity (TREE_TYPE (res))
1965 ? negative_overflow_infinity (TREE_TYPE (res))
1966 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1969 return res;
1973 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1974 bitmask if some bit is unset, it means for all numbers in the range
1975 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1976 bitmask if some bit is set, it means for all numbers in the range
1977 the bit is 1, otherwise it might be 0 or 1. */
1979 static bool
1980 zero_nonzero_bits_from_vr (const tree expr_type,
1981 value_range_t *vr,
1982 wide_int *may_be_nonzero,
1983 wide_int *must_be_nonzero)
1985 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1986 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1987 if (!range_int_cst_p (vr)
1988 || is_overflow_infinity (vr->min)
1989 || is_overflow_infinity (vr->max))
1990 return false;
1992 if (range_int_cst_singleton_p (vr))
1994 *may_be_nonzero = vr->min;
1995 *must_be_nonzero = *may_be_nonzero;
1997 else if (tree_int_cst_sgn (vr->min) >= 0
1998 || tree_int_cst_sgn (vr->max) < 0)
2000 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2001 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2002 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2003 if (xor_mask != 0)
2005 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2006 may_be_nonzero->get_precision ());
2007 *may_be_nonzero = *may_be_nonzero | mask;
2008 *must_be_nonzero = must_be_nonzero->and_not (mask);
2012 return true;
2015 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2016 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2017 false otherwise. If *AR can be represented with a single range
2018 *VR1 will be VR_UNDEFINED. */
2020 static bool
2021 ranges_from_anti_range (value_range_t *ar,
2022 value_range_t *vr0, value_range_t *vr1)
2024 tree type = TREE_TYPE (ar->min);
2026 vr0->type = VR_UNDEFINED;
2027 vr1->type = VR_UNDEFINED;
2029 if (ar->type != VR_ANTI_RANGE
2030 || TREE_CODE (ar->min) != INTEGER_CST
2031 || TREE_CODE (ar->max) != INTEGER_CST
2032 || !vrp_val_min (type)
2033 || !vrp_val_max (type))
2034 return false;
2036 if (!vrp_val_is_min (ar->min))
2038 vr0->type = VR_RANGE;
2039 vr0->min = vrp_val_min (type);
2040 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2042 if (!vrp_val_is_max (ar->max))
2044 vr1->type = VR_RANGE;
2045 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2046 vr1->max = vrp_val_max (type);
2048 if (vr0->type == VR_UNDEFINED)
2050 *vr0 = *vr1;
2051 vr1->type = VR_UNDEFINED;
2054 return vr0->type != VR_UNDEFINED;
2057 /* Helper to extract a value-range *VR for a multiplicative operation
2058 *VR0 CODE *VR1. */
2060 static void
2061 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2062 enum tree_code code,
2063 value_range_t *vr0, value_range_t *vr1)
2065 enum value_range_type type;
2066 tree val[4];
2067 size_t i;
2068 tree min, max;
2069 bool sop;
2070 int cmp;
2072 /* Multiplications, divisions and shifts are a bit tricky to handle,
2073 depending on the mix of signs we have in the two ranges, we
2074 need to operate on different values to get the minimum and
2075 maximum values for the new range. One approach is to figure
2076 out all the variations of range combinations and do the
2077 operations.
2079 However, this involves several calls to compare_values and it
2080 is pretty convoluted. It's simpler to do the 4 operations
2081 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2082 MAX1) and then figure the smallest and largest values to form
2083 the new range. */
2084 gcc_assert (code == MULT_EXPR
2085 || code == TRUNC_DIV_EXPR
2086 || code == FLOOR_DIV_EXPR
2087 || code == CEIL_DIV_EXPR
2088 || code == EXACT_DIV_EXPR
2089 || code == ROUND_DIV_EXPR
2090 || code == RSHIFT_EXPR
2091 || code == LSHIFT_EXPR);
2092 gcc_assert ((vr0->type == VR_RANGE
2093 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2094 && vr0->type == vr1->type);
2096 type = vr0->type;
2098 /* Compute the 4 cross operations. */
2099 sop = false;
2100 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2101 if (val[0] == NULL_TREE)
2102 sop = true;
2104 if (vr1->max == vr1->min)
2105 val[1] = NULL_TREE;
2106 else
2108 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2109 if (val[1] == NULL_TREE)
2110 sop = true;
2113 if (vr0->max == vr0->min)
2114 val[2] = NULL_TREE;
2115 else
2117 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2118 if (val[2] == NULL_TREE)
2119 sop = true;
2122 if (vr0->min == vr0->max || vr1->min == vr1->max)
2123 val[3] = NULL_TREE;
2124 else
2126 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2127 if (val[3] == NULL_TREE)
2128 sop = true;
2131 if (sop)
2133 set_value_range_to_varying (vr);
2134 return;
2137 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2138 of VAL[i]. */
2139 min = val[0];
2140 max = val[0];
2141 for (i = 1; i < 4; i++)
2143 if (!is_gimple_min_invariant (min)
2144 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2145 || !is_gimple_min_invariant (max)
2146 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2147 break;
2149 if (val[i])
2151 if (!is_gimple_min_invariant (val[i])
2152 || (TREE_OVERFLOW (val[i])
2153 && !is_overflow_infinity (val[i])))
2155 /* If we found an overflowed value, set MIN and MAX
2156 to it so that we set the resulting range to
2157 VARYING. */
2158 min = max = val[i];
2159 break;
2162 if (compare_values (val[i], min) == -1)
2163 min = val[i];
2165 if (compare_values (val[i], max) == 1)
2166 max = val[i];
2170 /* If either MIN or MAX overflowed, then set the resulting range to
2171 VARYING. But we do accept an overflow infinity
2172 representation. */
2173 if (min == NULL_TREE
2174 || !is_gimple_min_invariant (min)
2175 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2176 || max == NULL_TREE
2177 || !is_gimple_min_invariant (max)
2178 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2180 set_value_range_to_varying (vr);
2181 return;
2184 /* We punt if:
2185 1) [-INF, +INF]
2186 2) [-INF, +-INF(OVF)]
2187 3) [+-INF(OVF), +INF]
2188 4) [+-INF(OVF), +-INF(OVF)]
2189 We learn nothing when we have INF and INF(OVF) on both sides.
2190 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2191 overflow. */
2192 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2193 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2195 set_value_range_to_varying (vr);
2196 return;
2199 cmp = compare_values (min, max);
2200 if (cmp == -2 || cmp == 1)
2202 /* If the new range has its limits swapped around (MIN > MAX),
2203 then the operation caused one of them to wrap around, mark
2204 the new range VARYING. */
2205 set_value_range_to_varying (vr);
2207 else
2208 set_value_range (vr, type, min, max, NULL);
2211 /* Extract range information from a binary operation CODE based on
2212 the ranges of each of its operands, *VR0 and *VR1 with resulting
2213 type EXPR_TYPE. The resulting range is stored in *VR. */
2215 static void
2216 extract_range_from_binary_expr_1 (value_range_t *vr,
2217 enum tree_code code, tree expr_type,
2218 value_range_t *vr0_, value_range_t *vr1_)
2220 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2221 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2222 enum value_range_type type;
2223 tree min = NULL_TREE, max = NULL_TREE;
2224 int cmp;
2226 if (!INTEGRAL_TYPE_P (expr_type)
2227 && !POINTER_TYPE_P (expr_type))
2229 set_value_range_to_varying (vr);
2230 return;
2233 /* Not all binary expressions can be applied to ranges in a
2234 meaningful way. Handle only arithmetic operations. */
2235 if (code != PLUS_EXPR
2236 && code != MINUS_EXPR
2237 && code != POINTER_PLUS_EXPR
2238 && code != MULT_EXPR
2239 && code != TRUNC_DIV_EXPR
2240 && code != FLOOR_DIV_EXPR
2241 && code != CEIL_DIV_EXPR
2242 && code != EXACT_DIV_EXPR
2243 && code != ROUND_DIV_EXPR
2244 && code != TRUNC_MOD_EXPR
2245 && code != RSHIFT_EXPR
2246 && code != LSHIFT_EXPR
2247 && code != MIN_EXPR
2248 && code != MAX_EXPR
2249 && code != BIT_AND_EXPR
2250 && code != BIT_IOR_EXPR
2251 && code != BIT_XOR_EXPR)
2253 set_value_range_to_varying (vr);
2254 return;
2257 /* If both ranges are UNDEFINED, so is the result. */
2258 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2260 set_value_range_to_undefined (vr);
2261 return;
2263 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2264 code. At some point we may want to special-case operations that
2265 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2266 operand. */
2267 else if (vr0.type == VR_UNDEFINED)
2268 set_value_range_to_varying (&vr0);
2269 else if (vr1.type == VR_UNDEFINED)
2270 set_value_range_to_varying (&vr1);
2272 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2273 and express ~[] op X as ([]' op X) U ([]'' op X). */
2274 if (vr0.type == VR_ANTI_RANGE
2275 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2277 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2278 if (vrtem1.type != VR_UNDEFINED)
2280 value_range_t vrres = VR_INITIALIZER;
2281 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2282 &vrtem1, vr1_);
2283 vrp_meet (vr, &vrres);
2285 return;
2287 /* Likewise for X op ~[]. */
2288 if (vr1.type == VR_ANTI_RANGE
2289 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2291 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2292 if (vrtem1.type != VR_UNDEFINED)
2294 value_range_t vrres = VR_INITIALIZER;
2295 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2296 vr0_, &vrtem1);
2297 vrp_meet (vr, &vrres);
2299 return;
2302 /* The type of the resulting value range defaults to VR0.TYPE. */
2303 type = vr0.type;
2305 /* Refuse to operate on VARYING ranges, ranges of different kinds
2306 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2307 because we may be able to derive a useful range even if one of
2308 the operands is VR_VARYING or symbolic range. Similarly for
2309 divisions. TODO, we may be able to derive anti-ranges in
2310 some cases. */
2311 if (code != BIT_AND_EXPR
2312 && code != BIT_IOR_EXPR
2313 && code != TRUNC_DIV_EXPR
2314 && code != FLOOR_DIV_EXPR
2315 && code != CEIL_DIV_EXPR
2316 && code != EXACT_DIV_EXPR
2317 && code != ROUND_DIV_EXPR
2318 && code != TRUNC_MOD_EXPR
2319 && code != MIN_EXPR
2320 && code != MAX_EXPR
2321 && (vr0.type == VR_VARYING
2322 || vr1.type == VR_VARYING
2323 || vr0.type != vr1.type
2324 || symbolic_range_p (&vr0)
2325 || symbolic_range_p (&vr1)))
2327 set_value_range_to_varying (vr);
2328 return;
2331 /* Now evaluate the expression to determine the new range. */
2332 if (POINTER_TYPE_P (expr_type))
2334 if (code == MIN_EXPR || code == MAX_EXPR)
2336 /* For MIN/MAX expressions with pointers, we only care about
2337 nullness, if both are non null, then the result is nonnull.
2338 If both are null, then the result is null. Otherwise they
2339 are varying. */
2340 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2341 set_value_range_to_nonnull (vr, expr_type);
2342 else if (range_is_null (&vr0) && range_is_null (&vr1))
2343 set_value_range_to_null (vr, expr_type);
2344 else
2345 set_value_range_to_varying (vr);
2347 else if (code == POINTER_PLUS_EXPR)
2349 /* For pointer types, we are really only interested in asserting
2350 whether the expression evaluates to non-NULL. */
2351 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2352 set_value_range_to_nonnull (vr, expr_type);
2353 else if (range_is_null (&vr0) && range_is_null (&vr1))
2354 set_value_range_to_null (vr, expr_type);
2355 else
2356 set_value_range_to_varying (vr);
2358 else if (code == BIT_AND_EXPR)
2360 /* For pointer types, we are really only interested in asserting
2361 whether the expression evaluates to non-NULL. */
2362 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2363 set_value_range_to_nonnull (vr, expr_type);
2364 else if (range_is_null (&vr0) || range_is_null (&vr1))
2365 set_value_range_to_null (vr, expr_type);
2366 else
2367 set_value_range_to_varying (vr);
2369 else
2370 set_value_range_to_varying (vr);
2372 return;
2375 /* For integer ranges, apply the operation to each end of the
2376 range and see what we end up with. */
2377 if (code == PLUS_EXPR || code == MINUS_EXPR)
2379 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2380 ranges compute the precise range for such case if possible. */
2381 if (range_int_cst_p (&vr0)
2382 && range_int_cst_p (&vr1))
2384 signop sgn = TYPE_SIGN (expr_type);
2385 unsigned int prec = TYPE_PRECISION (expr_type);
2386 wide_int type_min = wi::min_value (TYPE_PRECISION (expr_type), sgn);
2387 wide_int type_max = wi::max_value (TYPE_PRECISION (expr_type), sgn);
2388 wide_int wmin, wmax;
2389 int min_ovf = 0;
2390 int max_ovf = 0;
2392 if (code == PLUS_EXPR)
2394 wmin = wi::add (vr0.min, vr1.min);
2395 wmax = wi::add (vr0.max, vr1.max);
2397 /* Check for overflow. */
2398 if (wi::cmp (vr1.min, 0, sgn) != wi::cmp (wmin, vr0.min, sgn))
2399 min_ovf = wi::cmp (vr0.min, wmin, sgn);
2400 if (wi::cmp (vr1.max, 0, sgn) != wi::cmp (wmax, vr0.max, sgn))
2401 max_ovf = wi::cmp (vr0.max, wmax, sgn);
2403 else /* if (code == MINUS_EXPR) */
2405 wmin = wi::sub (vr0.min, vr1.max);
2406 wmax = wi::sub (vr0.max, vr1.min);
2408 if (wi::cmp (0, vr1.max, sgn) != wi::cmp (wmin, vr0.min, sgn))
2409 min_ovf = wi::cmp (vr0.min, vr1.max, sgn);
2410 if (wi::cmp (0, vr1.min, sgn) != wi::cmp (wmax, vr0.max, sgn))
2411 max_ovf = wi::cmp (vr0.max, vr1.min, sgn);
2414 /* For non-wrapping arithmetic look at possibly smaller
2415 value-ranges of the type. */
2416 if (!TYPE_OVERFLOW_WRAPS (expr_type))
2418 if (vrp_val_min (expr_type))
2419 type_min = vrp_val_min (expr_type);
2420 if (vrp_val_max (expr_type))
2421 type_max = vrp_val_max (expr_type);
2424 /* Check for type overflow. */
2425 if (min_ovf == 0)
2427 if (wi::cmp (wmin, type_min, sgn) == -1)
2428 min_ovf = -1;
2429 else if (wi::cmp (wmin, type_max, sgn) == 1)
2430 min_ovf = 1;
2432 if (max_ovf == 0)
2434 if (wi::cmp (wmax, type_min, sgn) == -1)
2435 max_ovf = -1;
2436 else if (wi::cmp (wmax, type_max, sgn) == 1)
2437 max_ovf = 1;
2440 if (TYPE_OVERFLOW_WRAPS (expr_type))
2442 /* If overflow wraps, truncate the values and adjust the
2443 range kind and bounds appropriately. */
2444 wide_int tmin = wide_int::from (wmin, prec, sgn);
2445 wide_int tmax = wide_int::from (wmax, prec, sgn);
2446 if (min_ovf == max_ovf)
2448 /* No overflow or both overflow or underflow. The
2449 range kind stays VR_RANGE. */
2450 min = wide_int_to_tree (expr_type, tmin);
2451 max = wide_int_to_tree (expr_type, tmax);
2453 else if (min_ovf == -1
2454 && max_ovf == 1)
2456 /* Underflow and overflow, drop to VR_VARYING. */
2457 set_value_range_to_varying (vr);
2458 return;
2460 else
2462 /* Min underflow or max overflow. The range kind
2463 changes to VR_ANTI_RANGE. */
2464 bool covers = false;
2465 wide_int tem = tmin;
2466 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2467 || (max_ovf == 1 && min_ovf == 0));
2468 type = VR_ANTI_RANGE;
2469 tmin = tmax + 1;
2470 if (wi::cmp (tmin, tmax, sgn) < 0)
2471 covers = true;
2472 tmax = tem - 1;
2473 if (wi::cmp (tmax, tem, sgn) > 0)
2474 covers = true;
2475 /* If the anti-range would cover nothing, drop to varying.
2476 Likewise if the anti-range bounds are outside of the
2477 types values. */
2478 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2480 set_value_range_to_varying (vr);
2481 return;
2483 min = wide_int_to_tree (expr_type, tmin);
2484 max = wide_int_to_tree (expr_type, tmax);
2487 else
2489 /* If overflow does not wrap, saturate to the types min/max
2490 value. */
2491 if (min_ovf == -1)
2493 if (needs_overflow_infinity (expr_type)
2494 && supports_overflow_infinity (expr_type))
2495 min = negative_overflow_infinity (expr_type);
2496 else
2497 min = wide_int_to_tree (expr_type, type_min);
2499 else if (min_ovf == 1)
2501 if (needs_overflow_infinity (expr_type)
2502 && supports_overflow_infinity (expr_type))
2503 min = positive_overflow_infinity (expr_type);
2504 else
2505 min = wide_int_to_tree (expr_type, type_max);
2507 else
2508 min = wide_int_to_tree (expr_type, wmin);
2510 if (max_ovf == -1)
2512 if (needs_overflow_infinity (expr_type)
2513 && supports_overflow_infinity (expr_type))
2514 max = negative_overflow_infinity (expr_type);
2515 else
2516 max = wide_int_to_tree (expr_type, type_min);
2518 else if (max_ovf == 1)
2520 if (needs_overflow_infinity (expr_type)
2521 && supports_overflow_infinity (expr_type))
2522 max = positive_overflow_infinity (expr_type);
2523 else
2524 max = wide_int_to_tree (expr_type, type_max);
2526 else
2527 max = wide_int_to_tree (expr_type, wmax);
2529 if (needs_overflow_infinity (expr_type)
2530 && supports_overflow_infinity (expr_type))
2532 if (is_negative_overflow_infinity (vr0.min)
2533 || (code == PLUS_EXPR
2534 ? is_negative_overflow_infinity (vr1.min)
2535 : is_positive_overflow_infinity (vr1.max)))
2536 min = negative_overflow_infinity (expr_type);
2537 if (is_positive_overflow_infinity (vr0.max)
2538 || (code == PLUS_EXPR
2539 ? is_positive_overflow_infinity (vr1.max)
2540 : is_negative_overflow_infinity (vr1.min)))
2541 max = positive_overflow_infinity (expr_type);
2544 else
2546 /* For other cases, for example if we have a PLUS_EXPR with two
2547 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2548 to compute a precise range for such a case.
2549 ??? General even mixed range kind operations can be expressed
2550 by for example transforming ~[3, 5] + [1, 2] to range-only
2551 operations and a union primitive:
2552 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2553 [-INF+1, 4] U [6, +INF(OVF)]
2554 though usually the union is not exactly representable with
2555 a single range or anti-range as the above is
2556 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2557 but one could use a scheme similar to equivalences for this. */
2558 set_value_range_to_varying (vr);
2559 return;
2562 else if (code == MIN_EXPR
2563 || code == MAX_EXPR)
2565 if (vr0.type == VR_RANGE
2566 && !symbolic_range_p (&vr0))
2568 type = VR_RANGE;
2569 if (vr1.type == VR_RANGE
2570 && !symbolic_range_p (&vr1))
2572 /* For operations that make the resulting range directly
2573 proportional to the original ranges, apply the operation to
2574 the same end of each range. */
2575 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2576 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2578 else if (code == MIN_EXPR)
2580 min = vrp_val_min (expr_type);
2581 max = vr0.max;
2583 else if (code == MAX_EXPR)
2585 min = vr0.min;
2586 max = vrp_val_max (expr_type);
2589 else if (vr1.type == VR_RANGE
2590 && !symbolic_range_p (&vr1))
2592 type = VR_RANGE;
2593 if (code == MIN_EXPR)
2595 min = vrp_val_min (expr_type);
2596 max = vr1.max;
2598 else if (code == MAX_EXPR)
2600 min = vr1.min;
2601 max = vrp_val_max (expr_type);
2604 else
2606 set_value_range_to_varying (vr);
2607 return;
2610 else if (code == MULT_EXPR)
2612 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2613 drop to varying. This test requires 2*prec bits if both
2614 operands are signed and 2*prec + 2 bits if either is not. */
2616 signop sign = TYPE_SIGN (expr_type);
2617 unsigned int prec = TYPE_PRECISION (expr_type);
2619 if (range_int_cst_p (&vr0)
2620 && range_int_cst_p (&vr1)
2621 && TYPE_OVERFLOW_WRAPS (expr_type))
2623 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2624 typedef generic_wide_int
2625 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2626 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2627 vrp_int size = sizem1 + 1;
2629 /* Extend the values using the sign of the result to PREC2.
2630 From here on out, everthing is just signed math no matter
2631 what the input types were. */
2632 vrp_int min0 = vrp_int_cst (vr0.min);
2633 vrp_int max0 = vrp_int_cst (vr0.max);
2634 vrp_int min1 = vrp_int_cst (vr1.min);
2635 vrp_int max1 = vrp_int_cst (vr1.max);
2636 /* Canonicalize the intervals. */
2637 if (sign == UNSIGNED)
2639 if (wi::ltu_p (size, min0 + max0))
2641 min0 -= size;
2642 max0 -= size;
2645 if (wi::ltu_p (size, min1 + max1))
2647 min1 -= size;
2648 max1 -= size;
2652 vrp_int prod0 = min0 * min1;
2653 vrp_int prod1 = min0 * max1;
2654 vrp_int prod2 = max0 * min1;
2655 vrp_int prod3 = max0 * max1;
2657 /* Sort the 4 products so that min is in prod0 and max is in
2658 prod3. */
2659 /* min0min1 > max0max1 */
2660 if (wi::gts_p (prod0, prod3))
2662 vrp_int tmp = prod3;
2663 prod3 = prod0;
2664 prod0 = tmp;
2667 /* min0max1 > max0min1 */
2668 if (wi::gts_p (prod1, prod2))
2670 vrp_int tmp = prod2;
2671 prod2 = prod1;
2672 prod1 = tmp;
2675 if (wi::gts_p (prod0, prod1))
2677 vrp_int tmp = prod1;
2678 prod1 = prod0;
2679 prod0 = tmp;
2682 if (wi::gts_p (prod2, prod3))
2684 vrp_int tmp = prod3;
2685 prod3 = prod2;
2686 prod2 = tmp;
2689 /* diff = max - min. */
2690 prod2 = prod3 - prod0;
2691 if (wi::geu_p (prod2, sizem1))
2693 /* the range covers all values. */
2694 set_value_range_to_varying (vr);
2695 return;
2698 /* The following should handle the wrapping and selecting
2699 VR_ANTI_RANGE for us. */
2700 min = wide_int_to_tree (expr_type, prod0);
2701 max = wide_int_to_tree (expr_type, prod3);
2702 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2703 return;
2706 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2707 drop to VR_VARYING. It would take more effort to compute a
2708 precise range for such a case. For example, if we have
2709 op0 == 65536 and op1 == 65536 with their ranges both being
2710 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2711 we cannot claim that the product is in ~[0,0]. Note that we
2712 are guaranteed to have vr0.type == vr1.type at this
2713 point. */
2714 if (vr0.type == VR_ANTI_RANGE
2715 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2717 set_value_range_to_varying (vr);
2718 return;
2721 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2722 return;
2724 else if (code == RSHIFT_EXPR
2725 || code == LSHIFT_EXPR)
2727 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2728 then drop to VR_VARYING. Outside of this range we get undefined
2729 behavior from the shift operation. We cannot even trust
2730 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2731 shifts, and the operation at the tree level may be widened. */
2732 if (range_int_cst_p (&vr1)
2733 && compare_tree_int (vr1.min, 0) >= 0
2734 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2736 if (code == RSHIFT_EXPR)
2738 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2739 return;
2741 /* We can map lshifts by constants to MULT_EXPR handling. */
2742 else if (code == LSHIFT_EXPR
2743 && range_int_cst_singleton_p (&vr1))
2745 bool saved_flag_wrapv;
2746 value_range_t vr1p = VR_INITIALIZER;
2747 vr1p.type = VR_RANGE;
2748 vr1p.min = (wide_int_to_tree
2749 (expr_type,
2750 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2751 TYPE_PRECISION (expr_type))));
2752 vr1p.max = vr1p.min;
2753 /* We have to use a wrapping multiply though as signed overflow
2754 on lshifts is implementation defined in C89. */
2755 saved_flag_wrapv = flag_wrapv;
2756 flag_wrapv = 1;
2757 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2758 &vr0, &vr1p);
2759 flag_wrapv = saved_flag_wrapv;
2760 return;
2762 else if (code == LSHIFT_EXPR
2763 && range_int_cst_p (&vr0))
2765 int prec = TYPE_PRECISION (expr_type);
2766 int overflow_pos = prec;
2767 int bound_shift;
2768 wide_int low_bound, high_bound;
2769 bool uns = TYPE_UNSIGNED (expr_type);
2770 bool in_bounds = false;
2772 if (!uns)
2773 overflow_pos -= 1;
2775 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2776 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2777 overflow. However, for that to happen, vr1.max needs to be
2778 zero, which means vr1 is a singleton range of zero, which
2779 means it should be handled by the previous LSHIFT_EXPR
2780 if-clause. */
2781 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2782 wide_int complement = ~(bound - 1);
2784 if (uns)
2786 low_bound = bound;
2787 high_bound = complement;
2788 if (wi::ltu_p (vr0.max, low_bound))
2790 /* [5, 6] << [1, 2] == [10, 24]. */
2791 /* We're shifting out only zeroes, the value increases
2792 monotonically. */
2793 in_bounds = true;
2795 else if (wi::ltu_p (high_bound, vr0.min))
2797 /* [0xffffff00, 0xffffffff] << [1, 2]
2798 == [0xfffffc00, 0xfffffffe]. */
2799 /* We're shifting out only ones, the value decreases
2800 monotonically. */
2801 in_bounds = true;
2804 else
2806 /* [-1, 1] << [1, 2] == [-4, 4]. */
2807 low_bound = complement;
2808 high_bound = bound;
2809 if (wi::lts_p (vr0.max, high_bound)
2810 && wi::lts_p (low_bound, vr0.min))
2812 /* For non-negative numbers, we're shifting out only
2813 zeroes, the value increases monotonically.
2814 For negative numbers, we're shifting out only ones, the
2815 value decreases monotomically. */
2816 in_bounds = true;
2820 if (in_bounds)
2822 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2823 return;
2827 set_value_range_to_varying (vr);
2828 return;
2830 else if (code == TRUNC_DIV_EXPR
2831 || code == FLOOR_DIV_EXPR
2832 || code == CEIL_DIV_EXPR
2833 || code == EXACT_DIV_EXPR
2834 || code == ROUND_DIV_EXPR)
2836 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2838 /* For division, if op1 has VR_RANGE but op0 does not, something
2839 can be deduced just from that range. Say [min, max] / [4, max]
2840 gives [min / 4, max / 4] range. */
2841 if (vr1.type == VR_RANGE
2842 && !symbolic_range_p (&vr1)
2843 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2845 vr0.type = type = VR_RANGE;
2846 vr0.min = vrp_val_min (expr_type);
2847 vr0.max = vrp_val_max (expr_type);
2849 else
2851 set_value_range_to_varying (vr);
2852 return;
2856 /* For divisions, if flag_non_call_exceptions is true, we must
2857 not eliminate a division by zero. */
2858 if (cfun->can_throw_non_call_exceptions
2859 && (vr1.type != VR_RANGE
2860 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2862 set_value_range_to_varying (vr);
2863 return;
2866 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2867 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2868 include 0. */
2869 if (vr0.type == VR_RANGE
2870 && (vr1.type != VR_RANGE
2871 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2873 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2874 int cmp;
2876 min = NULL_TREE;
2877 max = NULL_TREE;
2878 if (TYPE_UNSIGNED (expr_type)
2879 || value_range_nonnegative_p (&vr1))
2881 /* For unsigned division or when divisor is known
2882 to be non-negative, the range has to cover
2883 all numbers from 0 to max for positive max
2884 and all numbers from min to 0 for negative min. */
2885 cmp = compare_values (vr0.max, zero);
2886 if (cmp == -1)
2887 max = zero;
2888 else if (cmp == 0 || cmp == 1)
2889 max = vr0.max;
2890 else
2891 type = VR_VARYING;
2892 cmp = compare_values (vr0.min, zero);
2893 if (cmp == 1)
2894 min = zero;
2895 else if (cmp == 0 || cmp == -1)
2896 min = vr0.min;
2897 else
2898 type = VR_VARYING;
2900 else
2902 /* Otherwise the range is -max .. max or min .. -min
2903 depending on which bound is bigger in absolute value,
2904 as the division can change the sign. */
2905 abs_extent_range (vr, vr0.min, vr0.max);
2906 return;
2908 if (type == VR_VARYING)
2910 set_value_range_to_varying (vr);
2911 return;
2914 else
2916 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2917 return;
2920 else if (code == TRUNC_MOD_EXPR)
2922 if (vr1.type != VR_RANGE
2923 || range_includes_zero_p (vr1.min, vr1.max) != 0
2924 || vrp_val_is_min (vr1.min))
2926 set_value_range_to_varying (vr);
2927 return;
2929 type = VR_RANGE;
2930 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2931 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
2932 if (tree_int_cst_lt (max, vr1.max))
2933 max = vr1.max;
2934 max = int_const_binop (MINUS_EXPR, max, build_int_cst (TREE_TYPE (max), 1));
2935 /* If the dividend is non-negative the modulus will be
2936 non-negative as well. */
2937 if (TYPE_UNSIGNED (expr_type)
2938 || value_range_nonnegative_p (&vr0))
2939 min = build_int_cst (TREE_TYPE (max), 0);
2940 else
2941 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
2943 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
2945 bool int_cst_range0, int_cst_range1;
2946 wide_int may_be_nonzero0, may_be_nonzero1;
2947 wide_int must_be_nonzero0, must_be_nonzero1;
2949 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
2950 &may_be_nonzero0,
2951 &must_be_nonzero0);
2952 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
2953 &may_be_nonzero1,
2954 &must_be_nonzero1);
2956 type = VR_RANGE;
2957 if (code == BIT_AND_EXPR)
2959 min = wide_int_to_tree (expr_type,
2960 must_be_nonzero0 & must_be_nonzero1);
2961 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
2962 /* If both input ranges contain only negative values we can
2963 truncate the result range maximum to the minimum of the
2964 input range maxima. */
2965 if (int_cst_range0 && int_cst_range1
2966 && tree_int_cst_sgn (vr0.max) < 0
2967 && tree_int_cst_sgn (vr1.max) < 0)
2969 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
2970 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
2972 /* If either input range contains only non-negative values
2973 we can truncate the result range maximum to the respective
2974 maximum of the input range. */
2975 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
2976 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
2977 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
2978 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
2979 max = wide_int_to_tree (expr_type, wmax);
2981 else if (code == BIT_IOR_EXPR)
2983 max = wide_int_to_tree (expr_type,
2984 may_be_nonzero0 | may_be_nonzero1);
2985 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
2986 /* If the input ranges contain only positive values we can
2987 truncate the minimum of the result range to the maximum
2988 of the input range minima. */
2989 if (int_cst_range0 && int_cst_range1
2990 && tree_int_cst_sgn (vr0.min) >= 0
2991 && tree_int_cst_sgn (vr1.min) >= 0)
2993 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
2994 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
2996 /* If either input range contains only negative values
2997 we can truncate the minimum of the result range to the
2998 respective minimum range. */
2999 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3000 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3001 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3002 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3003 min = wide_int_to_tree (expr_type, wmin);
3005 else if (code == BIT_XOR_EXPR)
3007 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3008 | ~(may_be_nonzero0 | may_be_nonzero1));
3009 wide_int result_one_bits
3010 = (must_be_nonzero0.and_not (may_be_nonzero1)
3011 | must_be_nonzero1.and_not (may_be_nonzero0));
3012 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3013 min = wide_int_to_tree (expr_type, result_one_bits);
3014 /* If the range has all positive or all negative values the
3015 result is better than VARYING. */
3016 if (tree_int_cst_sgn (min) < 0
3017 || tree_int_cst_sgn (max) >= 0)
3019 else
3020 max = min = NULL_TREE;
3023 else
3024 gcc_unreachable ();
3026 /* If either MIN or MAX overflowed, then set the resulting range to
3027 VARYING. But we do accept an overflow infinity
3028 representation. */
3029 if (min == NULL_TREE
3030 || !is_gimple_min_invariant (min)
3031 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
3032 || max == NULL_TREE
3033 || !is_gimple_min_invariant (max)
3034 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
3036 set_value_range_to_varying (vr);
3037 return;
3040 /* We punt if:
3041 1) [-INF, +INF]
3042 2) [-INF, +-INF(OVF)]
3043 3) [+-INF(OVF), +INF]
3044 4) [+-INF(OVF), +-INF(OVF)]
3045 We learn nothing when we have INF and INF(OVF) on both sides.
3046 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3047 overflow. */
3048 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3049 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3051 set_value_range_to_varying (vr);
3052 return;
3055 cmp = compare_values (min, max);
3056 if (cmp == -2 || cmp == 1)
3058 /* If the new range has its limits swapped around (MIN > MAX),
3059 then the operation caused one of them to wrap around, mark
3060 the new range VARYING. */
3061 set_value_range_to_varying (vr);
3063 else
3064 set_value_range (vr, type, min, max, NULL);
3067 /* Extract range information from a binary expression OP0 CODE OP1 based on
3068 the ranges of each of its operands with resulting type EXPR_TYPE.
3069 The resulting range is stored in *VR. */
3071 static void
3072 extract_range_from_binary_expr (value_range_t *vr,
3073 enum tree_code code,
3074 tree expr_type, tree op0, tree op1)
3076 value_range_t vr0 = VR_INITIALIZER;
3077 value_range_t vr1 = VR_INITIALIZER;
3079 /* Get value ranges for each operand. For constant operands, create
3080 a new value range with the operand to simplify processing. */
3081 if (TREE_CODE (op0) == SSA_NAME)
3082 vr0 = *(get_value_range (op0));
3083 else if (is_gimple_min_invariant (op0))
3084 set_value_range_to_value (&vr0, op0, NULL);
3085 else
3086 set_value_range_to_varying (&vr0);
3088 if (TREE_CODE (op1) == SSA_NAME)
3089 vr1 = *(get_value_range (op1));
3090 else if (is_gimple_min_invariant (op1))
3091 set_value_range_to_value (&vr1, op1, NULL);
3092 else
3093 set_value_range_to_varying (&vr1);
3095 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3098 /* Extract range information from a unary operation CODE based on
3099 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3100 The The resulting range is stored in *VR. */
3102 static void
3103 extract_range_from_unary_expr_1 (value_range_t *vr,
3104 enum tree_code code, tree type,
3105 value_range_t *vr0_, tree op0_type)
3107 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3109 /* VRP only operates on integral and pointer types. */
3110 if (!(INTEGRAL_TYPE_P (op0_type)
3111 || POINTER_TYPE_P (op0_type))
3112 || !(INTEGRAL_TYPE_P (type)
3113 || POINTER_TYPE_P (type)))
3115 set_value_range_to_varying (vr);
3116 return;
3119 /* If VR0 is UNDEFINED, so is the result. */
3120 if (vr0.type == VR_UNDEFINED)
3122 set_value_range_to_undefined (vr);
3123 return;
3126 /* Handle operations that we express in terms of others. */
3127 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3129 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3130 copy_value_range (vr, &vr0);
3131 return;
3133 else if (code == NEGATE_EXPR)
3135 /* -X is simply 0 - X, so re-use existing code that also handles
3136 anti-ranges fine. */
3137 value_range_t zero = VR_INITIALIZER;
3138 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3139 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3140 return;
3142 else if (code == BIT_NOT_EXPR)
3144 /* ~X is simply -1 - X, so re-use existing code that also handles
3145 anti-ranges fine. */
3146 value_range_t minusone = VR_INITIALIZER;
3147 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3148 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3149 type, &minusone, &vr0);
3150 return;
3153 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3154 and express op ~[] as (op []') U (op []''). */
3155 if (vr0.type == VR_ANTI_RANGE
3156 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3158 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3159 if (vrtem1.type != VR_UNDEFINED)
3161 value_range_t vrres = VR_INITIALIZER;
3162 extract_range_from_unary_expr_1 (&vrres, code, type,
3163 &vrtem1, op0_type);
3164 vrp_meet (vr, &vrres);
3166 return;
3169 if (CONVERT_EXPR_CODE_P (code))
3171 tree inner_type = op0_type;
3172 tree outer_type = type;
3174 /* If the expression evaluates to a pointer, we are only interested in
3175 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3176 if (POINTER_TYPE_P (type))
3178 if (range_is_nonnull (&vr0))
3179 set_value_range_to_nonnull (vr, type);
3180 else if (range_is_null (&vr0))
3181 set_value_range_to_null (vr, type);
3182 else
3183 set_value_range_to_varying (vr);
3184 return;
3187 /* If VR0 is varying and we increase the type precision, assume
3188 a full range for the following transformation. */
3189 if (vr0.type == VR_VARYING
3190 && INTEGRAL_TYPE_P (inner_type)
3191 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3193 vr0.type = VR_RANGE;
3194 vr0.min = TYPE_MIN_VALUE (inner_type);
3195 vr0.max = TYPE_MAX_VALUE (inner_type);
3198 /* If VR0 is a constant range or anti-range and the conversion is
3199 not truncating we can convert the min and max values and
3200 canonicalize the resulting range. Otherwise we can do the
3201 conversion if the size of the range is less than what the
3202 precision of the target type can represent and the range is
3203 not an anti-range. */
3204 if ((vr0.type == VR_RANGE
3205 || vr0.type == VR_ANTI_RANGE)
3206 && TREE_CODE (vr0.min) == INTEGER_CST
3207 && TREE_CODE (vr0.max) == INTEGER_CST
3208 && (!is_overflow_infinity (vr0.min)
3209 || (vr0.type == VR_RANGE
3210 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3211 && needs_overflow_infinity (outer_type)
3212 && supports_overflow_infinity (outer_type)))
3213 && (!is_overflow_infinity (vr0.max)
3214 || (vr0.type == VR_RANGE
3215 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3216 && needs_overflow_infinity (outer_type)
3217 && supports_overflow_infinity (outer_type)))
3218 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3219 || (vr0.type == VR_RANGE
3220 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3221 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3222 size_int (TYPE_PRECISION (outer_type)))))))
3224 tree new_min, new_max;
3225 if (is_overflow_infinity (vr0.min))
3226 new_min = negative_overflow_infinity (outer_type);
3227 else
3228 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3229 0, false);
3230 if (is_overflow_infinity (vr0.max))
3231 new_max = positive_overflow_infinity (outer_type);
3232 else
3233 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3234 0, false);
3235 set_and_canonicalize_value_range (vr, vr0.type,
3236 new_min, new_max, NULL);
3237 return;
3240 set_value_range_to_varying (vr);
3241 return;
3243 else if (code == ABS_EXPR)
3245 tree min, max;
3246 int cmp;
3248 /* Pass through vr0 in the easy cases. */
3249 if (TYPE_UNSIGNED (type)
3250 || value_range_nonnegative_p (&vr0))
3252 copy_value_range (vr, &vr0);
3253 return;
3256 /* For the remaining varying or symbolic ranges we can't do anything
3257 useful. */
3258 if (vr0.type == VR_VARYING
3259 || symbolic_range_p (&vr0))
3261 set_value_range_to_varying (vr);
3262 return;
3265 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3266 useful range. */
3267 if (!TYPE_OVERFLOW_UNDEFINED (type)
3268 && ((vr0.type == VR_RANGE
3269 && vrp_val_is_min (vr0.min))
3270 || (vr0.type == VR_ANTI_RANGE
3271 && !vrp_val_is_min (vr0.min))))
3273 set_value_range_to_varying (vr);
3274 return;
3277 /* ABS_EXPR may flip the range around, if the original range
3278 included negative values. */
3279 if (is_overflow_infinity (vr0.min))
3280 min = positive_overflow_infinity (type);
3281 else if (!vrp_val_is_min (vr0.min))
3282 min = fold_unary_to_constant (code, type, vr0.min);
3283 else if (!needs_overflow_infinity (type))
3284 min = TYPE_MAX_VALUE (type);
3285 else if (supports_overflow_infinity (type))
3286 min = positive_overflow_infinity (type);
3287 else
3289 set_value_range_to_varying (vr);
3290 return;
3293 if (is_overflow_infinity (vr0.max))
3294 max = positive_overflow_infinity (type);
3295 else if (!vrp_val_is_min (vr0.max))
3296 max = fold_unary_to_constant (code, type, vr0.max);
3297 else if (!needs_overflow_infinity (type))
3298 max = TYPE_MAX_VALUE (type);
3299 else if (supports_overflow_infinity (type)
3300 /* We shouldn't generate [+INF, +INF] as set_value_range
3301 doesn't like this and ICEs. */
3302 && !is_positive_overflow_infinity (min))
3303 max = positive_overflow_infinity (type);
3304 else
3306 set_value_range_to_varying (vr);
3307 return;
3310 cmp = compare_values (min, max);
3312 /* If a VR_ANTI_RANGEs contains zero, then we have
3313 ~[-INF, min(MIN, MAX)]. */
3314 if (vr0.type == VR_ANTI_RANGE)
3316 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3318 /* Take the lower of the two values. */
3319 if (cmp != 1)
3320 max = min;
3322 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3323 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3324 flag_wrapv is set and the original anti-range doesn't include
3325 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3326 if (TYPE_OVERFLOW_WRAPS (type))
3328 tree type_min_value = TYPE_MIN_VALUE (type);
3330 min = (vr0.min != type_min_value
3331 ? int_const_binop (PLUS_EXPR, type_min_value,
3332 build_int_cst (TREE_TYPE (type_min_value), 1))
3333 : type_min_value);
3335 else
3337 if (overflow_infinity_range_p (&vr0))
3338 min = negative_overflow_infinity (type);
3339 else
3340 min = TYPE_MIN_VALUE (type);
3343 else
3345 /* All else has failed, so create the range [0, INF], even for
3346 flag_wrapv since TYPE_MIN_VALUE is in the original
3347 anti-range. */
3348 vr0.type = VR_RANGE;
3349 min = build_int_cst (type, 0);
3350 if (needs_overflow_infinity (type))
3352 if (supports_overflow_infinity (type))
3353 max = positive_overflow_infinity (type);
3354 else
3356 set_value_range_to_varying (vr);
3357 return;
3360 else
3361 max = TYPE_MAX_VALUE (type);
3365 /* If the range contains zero then we know that the minimum value in the
3366 range will be zero. */
3367 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3369 if (cmp == 1)
3370 max = min;
3371 min = build_int_cst (type, 0);
3373 else
3375 /* If the range was reversed, swap MIN and MAX. */
3376 if (cmp == 1)
3378 tree t = min;
3379 min = max;
3380 max = t;
3384 cmp = compare_values (min, max);
3385 if (cmp == -2 || cmp == 1)
3387 /* If the new range has its limits swapped around (MIN > MAX),
3388 then the operation caused one of them to wrap around, mark
3389 the new range VARYING. */
3390 set_value_range_to_varying (vr);
3392 else
3393 set_value_range (vr, vr0.type, min, max, NULL);
3394 return;
3397 /* For unhandled operations fall back to varying. */
3398 set_value_range_to_varying (vr);
3399 return;
3403 /* Extract range information from a unary expression CODE OP0 based on
3404 the range of its operand with resulting type TYPE.
3405 The resulting range is stored in *VR. */
3407 static void
3408 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3409 tree type, tree op0)
3411 value_range_t vr0 = VR_INITIALIZER;
3413 /* Get value ranges for the operand. For constant operands, create
3414 a new value range with the operand to simplify processing. */
3415 if (TREE_CODE (op0) == SSA_NAME)
3416 vr0 = *(get_value_range (op0));
3417 else if (is_gimple_min_invariant (op0))
3418 set_value_range_to_value (&vr0, op0, NULL);
3419 else
3420 set_value_range_to_varying (&vr0);
3422 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3426 /* Extract range information from a conditional expression STMT based on
3427 the ranges of each of its operands and the expression code. */
3429 static void
3430 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3432 tree op0, op1;
3433 value_range_t vr0 = VR_INITIALIZER;
3434 value_range_t vr1 = VR_INITIALIZER;
3436 /* Get value ranges for each operand. For constant operands, create
3437 a new value range with the operand to simplify processing. */
3438 op0 = gimple_assign_rhs2 (stmt);
3439 if (TREE_CODE (op0) == SSA_NAME)
3440 vr0 = *(get_value_range (op0));
3441 else if (is_gimple_min_invariant (op0))
3442 set_value_range_to_value (&vr0, op0, NULL);
3443 else
3444 set_value_range_to_varying (&vr0);
3446 op1 = gimple_assign_rhs3 (stmt);
3447 if (TREE_CODE (op1) == SSA_NAME)
3448 vr1 = *(get_value_range (op1));
3449 else if (is_gimple_min_invariant (op1))
3450 set_value_range_to_value (&vr1, op1, NULL);
3451 else
3452 set_value_range_to_varying (&vr1);
3454 /* The resulting value range is the union of the operand ranges */
3455 copy_value_range (vr, &vr0);
3456 vrp_meet (vr, &vr1);
3460 /* Extract range information from a comparison expression EXPR based
3461 on the range of its operand and the expression code. */
3463 static void
3464 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3465 tree type, tree op0, tree op1)
3467 bool sop = false;
3468 tree val;
3470 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3471 NULL);
3473 /* A disadvantage of using a special infinity as an overflow
3474 representation is that we lose the ability to record overflow
3475 when we don't have an infinity. So we have to ignore a result
3476 which relies on overflow. */
3478 if (val && !is_overflow_infinity (val) && !sop)
3480 /* Since this expression was found on the RHS of an assignment,
3481 its type may be different from _Bool. Convert VAL to EXPR's
3482 type. */
3483 val = fold_convert (type, val);
3484 if (is_gimple_min_invariant (val))
3485 set_value_range_to_value (vr, val, vr->equiv);
3486 else
3487 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3489 else
3490 /* The result of a comparison is always true or false. */
3491 set_value_range_to_truthvalue (vr, type);
3494 /* Try to derive a nonnegative or nonzero range out of STMT relying
3495 primarily on generic routines in fold in conjunction with range data.
3496 Store the result in *VR */
3498 static void
3499 extract_range_basic (value_range_t *vr, gimple stmt)
3501 bool sop = false;
3502 tree type = gimple_expr_type (stmt);
3504 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3506 tree fndecl = gimple_call_fndecl (stmt), arg;
3507 int mini, maxi, zerov = 0, prec;
3509 switch (DECL_FUNCTION_CODE (fndecl))
3511 case BUILT_IN_CONSTANT_P:
3512 /* If the call is __builtin_constant_p and the argument is a
3513 function parameter resolve it to false. This avoids bogus
3514 array bound warnings.
3515 ??? We could do this as early as inlining is finished. */
3516 arg = gimple_call_arg (stmt, 0);
3517 if (TREE_CODE (arg) == SSA_NAME
3518 && SSA_NAME_IS_DEFAULT_DEF (arg)
3519 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3521 set_value_range_to_null (vr, type);
3522 return;
3524 break;
3525 /* Both __builtin_ffs* and __builtin_popcount return
3526 [0, prec]. */
3527 CASE_INT_FN (BUILT_IN_FFS):
3528 CASE_INT_FN (BUILT_IN_POPCOUNT):
3529 arg = gimple_call_arg (stmt, 0);
3530 prec = TYPE_PRECISION (TREE_TYPE (arg));
3531 mini = 0;
3532 maxi = prec;
3533 if (TREE_CODE (arg) == SSA_NAME)
3535 value_range_t *vr0 = get_value_range (arg);
3536 /* If arg is non-zero, then ffs or popcount
3537 are non-zero. */
3538 if (((vr0->type == VR_RANGE
3539 && integer_nonzerop (vr0->min))
3540 || (vr0->type == VR_ANTI_RANGE
3541 && integer_zerop (vr0->min)))
3542 && !is_overflow_infinity (vr0->min))
3543 mini = 1;
3544 /* If some high bits are known to be zero,
3545 we can decrease the maximum. */
3546 if (vr0->type == VR_RANGE
3547 && TREE_CODE (vr0->max) == INTEGER_CST
3548 && !is_overflow_infinity (vr0->max))
3549 maxi = tree_floor_log2 (vr0->max) + 1;
3551 goto bitop_builtin;
3552 /* __builtin_parity* returns [0, 1]. */
3553 CASE_INT_FN (BUILT_IN_PARITY):
3554 mini = 0;
3555 maxi = 1;
3556 goto bitop_builtin;
3557 /* __builtin_c[lt]z* return [0, prec-1], except for
3558 when the argument is 0, but that is undefined behavior.
3559 On many targets where the CLZ RTL or optab value is defined
3560 for 0 the value is prec, so include that in the range
3561 by default. */
3562 CASE_INT_FN (BUILT_IN_CLZ):
3563 arg = gimple_call_arg (stmt, 0);
3564 prec = TYPE_PRECISION (TREE_TYPE (arg));
3565 mini = 0;
3566 maxi = prec;
3567 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3568 != CODE_FOR_nothing
3569 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3570 zerov)
3571 /* Handle only the single common value. */
3572 && zerov != prec)
3573 /* Magic value to give up, unless vr0 proves
3574 arg is non-zero. */
3575 mini = -2;
3576 if (TREE_CODE (arg) == SSA_NAME)
3578 value_range_t *vr0 = get_value_range (arg);
3579 /* From clz of VR_RANGE minimum we can compute
3580 result maximum. */
3581 if (vr0->type == VR_RANGE
3582 && TREE_CODE (vr0->min) == INTEGER_CST
3583 && !is_overflow_infinity (vr0->min))
3585 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3586 if (maxi != prec)
3587 mini = 0;
3589 else if (vr0->type == VR_ANTI_RANGE
3590 && integer_zerop (vr0->min)
3591 && !is_overflow_infinity (vr0->min))
3593 maxi = prec - 1;
3594 mini = 0;
3596 if (mini == -2)
3597 break;
3598 /* From clz of VR_RANGE maximum we can compute
3599 result minimum. */
3600 if (vr0->type == VR_RANGE
3601 && TREE_CODE (vr0->max) == INTEGER_CST
3602 && !is_overflow_infinity (vr0->max))
3604 mini = prec - 1 - tree_floor_log2 (vr0->max);
3605 if (mini == prec)
3606 break;
3609 if (mini == -2)
3610 break;
3611 goto bitop_builtin;
3612 /* __builtin_ctz* return [0, prec-1], except for
3613 when the argument is 0, but that is undefined behavior.
3614 If there is a ctz optab for this mode and
3615 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3616 otherwise just assume 0 won't be seen. */
3617 CASE_INT_FN (BUILT_IN_CTZ):
3618 arg = gimple_call_arg (stmt, 0);
3619 prec = TYPE_PRECISION (TREE_TYPE (arg));
3620 mini = 0;
3621 maxi = prec - 1;
3622 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3623 != CODE_FOR_nothing
3624 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3625 zerov))
3627 /* Handle only the two common values. */
3628 if (zerov == -1)
3629 mini = -1;
3630 else if (zerov == prec)
3631 maxi = prec;
3632 else
3633 /* Magic value to give up, unless vr0 proves
3634 arg is non-zero. */
3635 mini = -2;
3637 if (TREE_CODE (arg) == SSA_NAME)
3639 value_range_t *vr0 = get_value_range (arg);
3640 /* If arg is non-zero, then use [0, prec - 1]. */
3641 if (((vr0->type == VR_RANGE
3642 && integer_nonzerop (vr0->min))
3643 || (vr0->type == VR_ANTI_RANGE
3644 && integer_zerop (vr0->min)))
3645 && !is_overflow_infinity (vr0->min))
3647 mini = 0;
3648 maxi = prec - 1;
3650 /* If some high bits are known to be zero,
3651 we can decrease the result maximum. */
3652 if (vr0->type == VR_RANGE
3653 && TREE_CODE (vr0->max) == INTEGER_CST
3654 && !is_overflow_infinity (vr0->max))
3656 maxi = tree_floor_log2 (vr0->max);
3657 /* For vr0 [0, 0] give up. */
3658 if (maxi == -1)
3659 break;
3662 if (mini == -2)
3663 break;
3664 goto bitop_builtin;
3665 /* __builtin_clrsb* returns [0, prec-1]. */
3666 CASE_INT_FN (BUILT_IN_CLRSB):
3667 arg = gimple_call_arg (stmt, 0);
3668 prec = TYPE_PRECISION (TREE_TYPE (arg));
3669 mini = 0;
3670 maxi = prec - 1;
3671 goto bitop_builtin;
3672 bitop_builtin:
3673 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3674 build_int_cst (type, maxi), NULL);
3675 return;
3676 default:
3677 break;
3680 else if (is_gimple_call (stmt)
3681 && gimple_call_internal_p (stmt))
3683 enum tree_code subcode = ERROR_MARK;
3684 switch (gimple_call_internal_fn (stmt))
3686 case IFN_UBSAN_CHECK_ADD:
3687 subcode = PLUS_EXPR;
3688 break;
3689 case IFN_UBSAN_CHECK_SUB:
3690 subcode = MINUS_EXPR;
3691 break;
3692 case IFN_UBSAN_CHECK_MUL:
3693 subcode = MULT_EXPR;
3694 break;
3695 default:
3696 break;
3698 if (subcode != ERROR_MARK)
3700 bool saved_flag_wrapv = flag_wrapv;
3701 /* Pretend the arithmetics is wrapping. If there is
3702 any overflow, we'll complain, but will actually do
3703 wrapping operation. */
3704 flag_wrapv = 1;
3705 extract_range_from_binary_expr (vr, subcode, type,
3706 gimple_call_arg (stmt, 0),
3707 gimple_call_arg (stmt, 1));
3708 flag_wrapv = saved_flag_wrapv;
3710 /* If for both arguments vrp_valueize returned non-NULL,
3711 this should have been already folded and if not, it
3712 wasn't folded because of overflow. Avoid removing the
3713 UBSAN_CHECK_* calls in that case. */
3714 if (vr->type == VR_RANGE
3715 && (vr->min == vr->max
3716 || operand_equal_p (vr->min, vr->max, 0)))
3717 set_value_range_to_varying (vr);
3718 return;
3721 if (INTEGRAL_TYPE_P (type)
3722 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3723 set_value_range_to_nonnegative (vr, type,
3724 sop || stmt_overflow_infinity (stmt));
3725 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3726 && !sop)
3727 set_value_range_to_nonnull (vr, type);
3728 else
3729 set_value_range_to_varying (vr);
3733 /* Try to compute a useful range out of assignment STMT and store it
3734 in *VR. */
3736 static void
3737 extract_range_from_assignment (value_range_t *vr, gimple stmt)
3739 enum tree_code code = gimple_assign_rhs_code (stmt);
3741 if (code == ASSERT_EXPR)
3742 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3743 else if (code == SSA_NAME)
3744 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3745 else if (TREE_CODE_CLASS (code) == tcc_binary)
3746 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3747 gimple_expr_type (stmt),
3748 gimple_assign_rhs1 (stmt),
3749 gimple_assign_rhs2 (stmt));
3750 else if (TREE_CODE_CLASS (code) == tcc_unary)
3751 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3752 gimple_expr_type (stmt),
3753 gimple_assign_rhs1 (stmt));
3754 else if (code == COND_EXPR)
3755 extract_range_from_cond_expr (vr, stmt);
3756 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3757 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3758 gimple_expr_type (stmt),
3759 gimple_assign_rhs1 (stmt),
3760 gimple_assign_rhs2 (stmt));
3761 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3762 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3763 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3764 else
3765 set_value_range_to_varying (vr);
3767 if (vr->type == VR_VARYING)
3768 extract_range_basic (vr, stmt);
3771 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3772 would be profitable to adjust VR using scalar evolution information
3773 for VAR. If so, update VR with the new limits. */
3775 static void
3776 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
3777 gimple stmt, tree var)
3779 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3780 enum ev_direction dir;
3782 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3783 better opportunities than a regular range, but I'm not sure. */
3784 if (vr->type == VR_ANTI_RANGE)
3785 return;
3787 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3789 /* Like in PR19590, scev can return a constant function. */
3790 if (is_gimple_min_invariant (chrec))
3792 set_value_range_to_value (vr, chrec, vr->equiv);
3793 return;
3796 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3797 return;
3799 init = initial_condition_in_loop_num (chrec, loop->num);
3800 tem = op_with_constant_singleton_value_range (init);
3801 if (tem)
3802 init = tem;
3803 step = evolution_part_in_loop_num (chrec, loop->num);
3804 tem = op_with_constant_singleton_value_range (step);
3805 if (tem)
3806 step = tem;
3808 /* If STEP is symbolic, we can't know whether INIT will be the
3809 minimum or maximum value in the range. Also, unless INIT is
3810 a simple expression, compare_values and possibly other functions
3811 in tree-vrp won't be able to handle it. */
3812 if (step == NULL_TREE
3813 || !is_gimple_min_invariant (step)
3814 || !valid_value_p (init))
3815 return;
3817 dir = scev_direction (chrec);
3818 if (/* Do not adjust ranges if we do not know whether the iv increases
3819 or decreases, ... */
3820 dir == EV_DIR_UNKNOWN
3821 /* ... or if it may wrap. */
3822 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3823 true))
3824 return;
3826 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3827 negative_overflow_infinity and positive_overflow_infinity,
3828 because we have concluded that the loop probably does not
3829 wrap. */
3831 type = TREE_TYPE (var);
3832 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
3833 tmin = lower_bound_in_type (type, type);
3834 else
3835 tmin = TYPE_MIN_VALUE (type);
3836 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
3837 tmax = upper_bound_in_type (type, type);
3838 else
3839 tmax = TYPE_MAX_VALUE (type);
3841 /* Try to use estimated number of iterations for the loop to constrain the
3842 final value in the evolution. */
3843 if (TREE_CODE (step) == INTEGER_CST
3844 && is_gimple_val (init)
3845 && (TREE_CODE (init) != SSA_NAME
3846 || get_value_range (init)->type == VR_RANGE))
3848 widest_int nit;
3850 /* We are only entering here for loop header PHI nodes, so using
3851 the number of latch executions is the correct thing to use. */
3852 if (max_loop_iterations (loop, &nit))
3854 value_range_t maxvr = VR_INITIALIZER;
3855 signop sgn = TYPE_SIGN (TREE_TYPE (step));
3856 bool overflow;
3858 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
3859 &overflow);
3860 /* If the multiplication overflowed we can't do a meaningful
3861 adjustment. Likewise if the result doesn't fit in the type
3862 of the induction variable. For a signed type we have to
3863 check whether the result has the expected signedness which
3864 is that of the step as number of iterations is unsigned. */
3865 if (!overflow
3866 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
3867 && (sgn == UNSIGNED
3868 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
3870 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
3871 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
3872 TREE_TYPE (init), init, tem);
3873 /* Likewise if the addition did. */
3874 if (maxvr.type == VR_RANGE)
3876 tmin = maxvr.min;
3877 tmax = maxvr.max;
3883 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3885 min = tmin;
3886 max = tmax;
3888 /* For VARYING or UNDEFINED ranges, just about anything we get
3889 from scalar evolutions should be better. */
3891 if (dir == EV_DIR_DECREASES)
3892 max = init;
3893 else
3894 min = init;
3896 /* If we would create an invalid range, then just assume we
3897 know absolutely nothing. This may be over-conservative,
3898 but it's clearly safe, and should happen only in unreachable
3899 parts of code, or for invalid programs. */
3900 if (compare_values (min, max) == 1)
3901 return;
3903 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3905 else if (vr->type == VR_RANGE)
3907 min = vr->min;
3908 max = vr->max;
3910 if (dir == EV_DIR_DECREASES)
3912 /* INIT is the maximum value. If INIT is lower than VR->MAX
3913 but no smaller than VR->MIN, set VR->MAX to INIT. */
3914 if (compare_values (init, max) == -1)
3915 max = init;
3917 /* According to the loop information, the variable does not
3918 overflow. If we think it does, probably because of an
3919 overflow due to arithmetic on a different INF value,
3920 reset now. */
3921 if (is_negative_overflow_infinity (min)
3922 || compare_values (min, tmin) == -1)
3923 min = tmin;
3926 else
3928 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3929 if (compare_values (init, min) == 1)
3930 min = init;
3932 if (is_positive_overflow_infinity (max)
3933 || compare_values (tmax, max) == -1)
3934 max = tmax;
3937 /* If we just created an invalid range with the minimum
3938 greater than the maximum, we fail conservatively.
3939 This should happen only in unreachable
3940 parts of code, or for invalid programs. */
3941 if (compare_values (min, max) == 1)
3942 return;
3944 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3949 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3951 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3952 all the values in the ranges.
3954 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3956 - Return NULL_TREE if it is not always possible to determine the
3957 value of the comparison.
3959 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3960 overflow infinity was used in the test. */
3963 static tree
3964 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
3965 bool *strict_overflow_p)
3967 /* VARYING or UNDEFINED ranges cannot be compared. */
3968 if (vr0->type == VR_VARYING
3969 || vr0->type == VR_UNDEFINED
3970 || vr1->type == VR_VARYING
3971 || vr1->type == VR_UNDEFINED)
3972 return NULL_TREE;
3974 /* Anti-ranges need to be handled separately. */
3975 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
3977 /* If both are anti-ranges, then we cannot compute any
3978 comparison. */
3979 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
3980 return NULL_TREE;
3982 /* These comparisons are never statically computable. */
3983 if (comp == GT_EXPR
3984 || comp == GE_EXPR
3985 || comp == LT_EXPR
3986 || comp == LE_EXPR)
3987 return NULL_TREE;
3989 /* Equality can be computed only between a range and an
3990 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3991 if (vr0->type == VR_RANGE)
3993 /* To simplify processing, make VR0 the anti-range. */
3994 value_range_t *tmp = vr0;
3995 vr0 = vr1;
3996 vr1 = tmp;
3999 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4001 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4002 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4003 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4005 return NULL_TREE;
4008 if (!usable_range_p (vr0, strict_overflow_p)
4009 || !usable_range_p (vr1, strict_overflow_p))
4010 return NULL_TREE;
4012 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4013 operands around and change the comparison code. */
4014 if (comp == GT_EXPR || comp == GE_EXPR)
4016 value_range_t *tmp;
4017 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4018 tmp = vr0;
4019 vr0 = vr1;
4020 vr1 = tmp;
4023 if (comp == EQ_EXPR)
4025 /* Equality may only be computed if both ranges represent
4026 exactly one value. */
4027 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4028 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4030 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4031 strict_overflow_p);
4032 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4033 strict_overflow_p);
4034 if (cmp_min == 0 && cmp_max == 0)
4035 return boolean_true_node;
4036 else if (cmp_min != -2 && cmp_max != -2)
4037 return boolean_false_node;
4039 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4040 else if (compare_values_warnv (vr0->min, vr1->max,
4041 strict_overflow_p) == 1
4042 || compare_values_warnv (vr1->min, vr0->max,
4043 strict_overflow_p) == 1)
4044 return boolean_false_node;
4046 return NULL_TREE;
4048 else if (comp == NE_EXPR)
4050 int cmp1, cmp2;
4052 /* If VR0 is completely to the left or completely to the right
4053 of VR1, they are always different. Notice that we need to
4054 make sure that both comparisons yield similar results to
4055 avoid comparing values that cannot be compared at
4056 compile-time. */
4057 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4058 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4059 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4060 return boolean_true_node;
4062 /* If VR0 and VR1 represent a single value and are identical,
4063 return false. */
4064 else if (compare_values_warnv (vr0->min, vr0->max,
4065 strict_overflow_p) == 0
4066 && compare_values_warnv (vr1->min, vr1->max,
4067 strict_overflow_p) == 0
4068 && compare_values_warnv (vr0->min, vr1->min,
4069 strict_overflow_p) == 0
4070 && compare_values_warnv (vr0->max, vr1->max,
4071 strict_overflow_p) == 0)
4072 return boolean_false_node;
4074 /* Otherwise, they may or may not be different. */
4075 else
4076 return NULL_TREE;
4078 else if (comp == LT_EXPR || comp == LE_EXPR)
4080 int tst;
4082 /* If VR0 is to the left of VR1, return true. */
4083 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4084 if ((comp == LT_EXPR && tst == -1)
4085 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4087 if (overflow_infinity_range_p (vr0)
4088 || overflow_infinity_range_p (vr1))
4089 *strict_overflow_p = true;
4090 return boolean_true_node;
4093 /* If VR0 is to the right of VR1, return false. */
4094 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4095 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4096 || (comp == LE_EXPR && tst == 1))
4098 if (overflow_infinity_range_p (vr0)
4099 || overflow_infinity_range_p (vr1))
4100 *strict_overflow_p = true;
4101 return boolean_false_node;
4104 /* Otherwise, we don't know. */
4105 return NULL_TREE;
4108 gcc_unreachable ();
4112 /* Given a value range VR, a value VAL and a comparison code COMP, return
4113 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4114 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4115 always returns false. Return NULL_TREE if it is not always
4116 possible to determine the value of the comparison. Also set
4117 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4118 infinity was used in the test. */
4120 static tree
4121 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4122 bool *strict_overflow_p)
4124 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4125 return NULL_TREE;
4127 /* Anti-ranges need to be handled separately. */
4128 if (vr->type == VR_ANTI_RANGE)
4130 /* For anti-ranges, the only predicates that we can compute at
4131 compile time are equality and inequality. */
4132 if (comp == GT_EXPR
4133 || comp == GE_EXPR
4134 || comp == LT_EXPR
4135 || comp == LE_EXPR)
4136 return NULL_TREE;
4138 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4139 if (value_inside_range (val, vr->min, vr->max) == 1)
4140 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4142 return NULL_TREE;
4145 if (!usable_range_p (vr, strict_overflow_p))
4146 return NULL_TREE;
4148 if (comp == EQ_EXPR)
4150 /* EQ_EXPR may only be computed if VR represents exactly
4151 one value. */
4152 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4154 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4155 if (cmp == 0)
4156 return boolean_true_node;
4157 else if (cmp == -1 || cmp == 1 || cmp == 2)
4158 return boolean_false_node;
4160 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4161 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4162 return boolean_false_node;
4164 return NULL_TREE;
4166 else if (comp == NE_EXPR)
4168 /* If VAL is not inside VR, then they are always different. */
4169 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4170 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4171 return boolean_true_node;
4173 /* If VR represents exactly one value equal to VAL, then return
4174 false. */
4175 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4176 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4177 return boolean_false_node;
4179 /* Otherwise, they may or may not be different. */
4180 return NULL_TREE;
4182 else if (comp == LT_EXPR || comp == LE_EXPR)
4184 int tst;
4186 /* If VR is to the left of VAL, return true. */
4187 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4188 if ((comp == LT_EXPR && tst == -1)
4189 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4191 if (overflow_infinity_range_p (vr))
4192 *strict_overflow_p = true;
4193 return boolean_true_node;
4196 /* If VR is to the right of VAL, return false. */
4197 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4198 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4199 || (comp == LE_EXPR && tst == 1))
4201 if (overflow_infinity_range_p (vr))
4202 *strict_overflow_p = true;
4203 return boolean_false_node;
4206 /* Otherwise, we don't know. */
4207 return NULL_TREE;
4209 else if (comp == GT_EXPR || comp == GE_EXPR)
4211 int tst;
4213 /* If VR is to the right of VAL, return true. */
4214 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4215 if ((comp == GT_EXPR && tst == 1)
4216 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4218 if (overflow_infinity_range_p (vr))
4219 *strict_overflow_p = true;
4220 return boolean_true_node;
4223 /* If VR is to the left of VAL, return false. */
4224 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4225 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4226 || (comp == GE_EXPR && tst == -1))
4228 if (overflow_infinity_range_p (vr))
4229 *strict_overflow_p = true;
4230 return boolean_false_node;
4233 /* Otherwise, we don't know. */
4234 return NULL_TREE;
4237 gcc_unreachable ();
4241 /* Debugging dumps. */
4243 void dump_value_range (FILE *, value_range_t *);
4244 void debug_value_range (value_range_t *);
4245 void dump_all_value_ranges (FILE *);
4246 void debug_all_value_ranges (void);
4247 void dump_vr_equiv (FILE *, bitmap);
4248 void debug_vr_equiv (bitmap);
4251 /* Dump value range VR to FILE. */
4253 void
4254 dump_value_range (FILE *file, value_range_t *vr)
4256 if (vr == NULL)
4257 fprintf (file, "[]");
4258 else if (vr->type == VR_UNDEFINED)
4259 fprintf (file, "UNDEFINED");
4260 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4262 tree type = TREE_TYPE (vr->min);
4264 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4266 if (is_negative_overflow_infinity (vr->min))
4267 fprintf (file, "-INF(OVF)");
4268 else if (INTEGRAL_TYPE_P (type)
4269 && !TYPE_UNSIGNED (type)
4270 && vrp_val_is_min (vr->min))
4271 fprintf (file, "-INF");
4272 else
4273 print_generic_expr (file, vr->min, 0);
4275 fprintf (file, ", ");
4277 if (is_positive_overflow_infinity (vr->max))
4278 fprintf (file, "+INF(OVF)");
4279 else if (INTEGRAL_TYPE_P (type)
4280 && vrp_val_is_max (vr->max))
4281 fprintf (file, "+INF");
4282 else
4283 print_generic_expr (file, vr->max, 0);
4285 fprintf (file, "]");
4287 if (vr->equiv)
4289 bitmap_iterator bi;
4290 unsigned i, c = 0;
4292 fprintf (file, " EQUIVALENCES: { ");
4294 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4296 print_generic_expr (file, ssa_name (i), 0);
4297 fprintf (file, " ");
4298 c++;
4301 fprintf (file, "} (%u elements)", c);
4304 else if (vr->type == VR_VARYING)
4305 fprintf (file, "VARYING");
4306 else
4307 fprintf (file, "INVALID RANGE");
4311 /* Dump value range VR to stderr. */
4313 DEBUG_FUNCTION void
4314 debug_value_range (value_range_t *vr)
4316 dump_value_range (stderr, vr);
4317 fprintf (stderr, "\n");
4321 /* Dump value ranges of all SSA_NAMEs to FILE. */
4323 void
4324 dump_all_value_ranges (FILE *file)
4326 size_t i;
4328 for (i = 0; i < num_vr_values; i++)
4330 if (vr_value[i])
4332 print_generic_expr (file, ssa_name (i), 0);
4333 fprintf (file, ": ");
4334 dump_value_range (file, vr_value[i]);
4335 fprintf (file, "\n");
4339 fprintf (file, "\n");
4343 /* Dump all value ranges to stderr. */
4345 DEBUG_FUNCTION void
4346 debug_all_value_ranges (void)
4348 dump_all_value_ranges (stderr);
4352 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4353 create a new SSA name N and return the assertion assignment
4354 'N = ASSERT_EXPR <V, V OP W>'. */
4356 static gimple
4357 build_assert_expr_for (tree cond, tree v)
4359 tree a;
4360 gimple assertion;
4362 gcc_assert (TREE_CODE (v) == SSA_NAME
4363 && COMPARISON_CLASS_P (cond));
4365 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4366 assertion = gimple_build_assign (NULL_TREE, a);
4368 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4369 operand of the ASSERT_EXPR. Create it so the new name and the old one
4370 are registered in the replacement table so that we can fix the SSA web
4371 after adding all the ASSERT_EXPRs. */
4372 create_new_def_for (v, assertion, NULL);
4374 return assertion;
4378 /* Return false if EXPR is a predicate expression involving floating
4379 point values. */
4381 static inline bool
4382 fp_predicate (gimple stmt)
4384 GIMPLE_CHECK (stmt, GIMPLE_COND);
4386 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4389 /* If the range of values taken by OP can be inferred after STMT executes,
4390 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4391 describes the inferred range. Return true if a range could be
4392 inferred. */
4394 static bool
4395 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4397 *val_p = NULL_TREE;
4398 *comp_code_p = ERROR_MARK;
4400 /* Do not attempt to infer anything in names that flow through
4401 abnormal edges. */
4402 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4403 return false;
4405 /* Similarly, don't infer anything from statements that may throw
4406 exceptions. ??? Relax this requirement? */
4407 if (stmt_could_throw_p (stmt))
4408 return false;
4410 /* If STMT is the last statement of a basic block with no normal
4411 successors, there is no point inferring anything about any of its
4412 operands. We would not be able to find a proper insertion point
4413 for the assertion, anyway. */
4414 if (stmt_ends_bb_p (stmt))
4416 edge_iterator ei;
4417 edge e;
4419 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4420 if (!(e->flags & EDGE_ABNORMAL))
4421 break;
4422 if (e == NULL)
4423 return false;
4426 if (infer_nonnull_range (stmt, op, true, true))
4428 *val_p = build_int_cst (TREE_TYPE (op), 0);
4429 *comp_code_p = NE_EXPR;
4430 return true;
4433 return false;
4437 void dump_asserts_for (FILE *, tree);
4438 void debug_asserts_for (tree);
4439 void dump_all_asserts (FILE *);
4440 void debug_all_asserts (void);
4442 /* Dump all the registered assertions for NAME to FILE. */
4444 void
4445 dump_asserts_for (FILE *file, tree name)
4447 assert_locus_t loc;
4449 fprintf (file, "Assertions to be inserted for ");
4450 print_generic_expr (file, name, 0);
4451 fprintf (file, "\n");
4453 loc = asserts_for[SSA_NAME_VERSION (name)];
4454 while (loc)
4456 fprintf (file, "\t");
4457 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4458 fprintf (file, "\n\tBB #%d", loc->bb->index);
4459 if (loc->e)
4461 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4462 loc->e->dest->index);
4463 dump_edge_info (file, loc->e, dump_flags, 0);
4465 fprintf (file, "\n\tPREDICATE: ");
4466 print_generic_expr (file, name, 0);
4467 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4468 print_generic_expr (file, loc->val, 0);
4469 fprintf (file, "\n\n");
4470 loc = loc->next;
4473 fprintf (file, "\n");
4477 /* Dump all the registered assertions for NAME to stderr. */
4479 DEBUG_FUNCTION void
4480 debug_asserts_for (tree name)
4482 dump_asserts_for (stderr, name);
4486 /* Dump all the registered assertions for all the names to FILE. */
4488 void
4489 dump_all_asserts (FILE *file)
4491 unsigned i;
4492 bitmap_iterator bi;
4494 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4495 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4496 dump_asserts_for (file, ssa_name (i));
4497 fprintf (file, "\n");
4501 /* Dump all the registered assertions for all the names to stderr. */
4503 DEBUG_FUNCTION void
4504 debug_all_asserts (void)
4506 dump_all_asserts (stderr);
4510 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4511 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4512 E->DEST, then register this location as a possible insertion point
4513 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4515 BB, E and SI provide the exact insertion point for the new
4516 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4517 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4518 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4519 must not be NULL. */
4521 static void
4522 register_new_assert_for (tree name, tree expr,
4523 enum tree_code comp_code,
4524 tree val,
4525 basic_block bb,
4526 edge e,
4527 gimple_stmt_iterator si)
4529 assert_locus_t n, loc, last_loc;
4530 basic_block dest_bb;
4532 gcc_checking_assert (bb == NULL || e == NULL);
4534 if (e == NULL)
4535 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4536 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4538 /* Never build an assert comparing against an integer constant with
4539 TREE_OVERFLOW set. This confuses our undefined overflow warning
4540 machinery. */
4541 if (TREE_OVERFLOW_P (val))
4542 val = drop_tree_overflow (val);
4544 /* The new assertion A will be inserted at BB or E. We need to
4545 determine if the new location is dominated by a previously
4546 registered location for A. If we are doing an edge insertion,
4547 assume that A will be inserted at E->DEST. Note that this is not
4548 necessarily true.
4550 If E is a critical edge, it will be split. But even if E is
4551 split, the new block will dominate the same set of blocks that
4552 E->DEST dominates.
4554 The reverse, however, is not true, blocks dominated by E->DEST
4555 will not be dominated by the new block created to split E. So,
4556 if the insertion location is on a critical edge, we will not use
4557 the new location to move another assertion previously registered
4558 at a block dominated by E->DEST. */
4559 dest_bb = (bb) ? bb : e->dest;
4561 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4562 VAL at a block dominating DEST_BB, then we don't need to insert a new
4563 one. Similarly, if the same assertion already exists at a block
4564 dominated by DEST_BB and the new location is not on a critical
4565 edge, then update the existing location for the assertion (i.e.,
4566 move the assertion up in the dominance tree).
4568 Note, this is implemented as a simple linked list because there
4569 should not be more than a handful of assertions registered per
4570 name. If this becomes a performance problem, a table hashed by
4571 COMP_CODE and VAL could be implemented. */
4572 loc = asserts_for[SSA_NAME_VERSION (name)];
4573 last_loc = loc;
4574 while (loc)
4576 if (loc->comp_code == comp_code
4577 && (loc->val == val
4578 || operand_equal_p (loc->val, val, 0))
4579 && (loc->expr == expr
4580 || operand_equal_p (loc->expr, expr, 0)))
4582 /* If E is not a critical edge and DEST_BB
4583 dominates the existing location for the assertion, move
4584 the assertion up in the dominance tree by updating its
4585 location information. */
4586 if ((e == NULL || !EDGE_CRITICAL_P (e))
4587 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4589 loc->bb = dest_bb;
4590 loc->e = e;
4591 loc->si = si;
4592 return;
4596 /* Update the last node of the list and move to the next one. */
4597 last_loc = loc;
4598 loc = loc->next;
4601 /* If we didn't find an assertion already registered for
4602 NAME COMP_CODE VAL, add a new one at the end of the list of
4603 assertions associated with NAME. */
4604 n = XNEW (struct assert_locus_d);
4605 n->bb = dest_bb;
4606 n->e = e;
4607 n->si = si;
4608 n->comp_code = comp_code;
4609 n->val = val;
4610 n->expr = expr;
4611 n->next = NULL;
4613 if (last_loc)
4614 last_loc->next = n;
4615 else
4616 asserts_for[SSA_NAME_VERSION (name)] = n;
4618 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4621 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4622 Extract a suitable test code and value and store them into *CODE_P and
4623 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4625 If no extraction was possible, return FALSE, otherwise return TRUE.
4627 If INVERT is true, then we invert the result stored into *CODE_P. */
4629 static bool
4630 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4631 tree cond_op0, tree cond_op1,
4632 bool invert, enum tree_code *code_p,
4633 tree *val_p)
4635 enum tree_code comp_code;
4636 tree val;
4638 /* Otherwise, we have a comparison of the form NAME COMP VAL
4639 or VAL COMP NAME. */
4640 if (name == cond_op1)
4642 /* If the predicate is of the form VAL COMP NAME, flip
4643 COMP around because we need to register NAME as the
4644 first operand in the predicate. */
4645 comp_code = swap_tree_comparison (cond_code);
4646 val = cond_op0;
4648 else
4650 /* The comparison is of the form NAME COMP VAL, so the
4651 comparison code remains unchanged. */
4652 comp_code = cond_code;
4653 val = cond_op1;
4656 /* Invert the comparison code as necessary. */
4657 if (invert)
4658 comp_code = invert_tree_comparison (comp_code, 0);
4660 /* VRP does not handle float types. */
4661 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4662 return false;
4664 /* Do not register always-false predicates.
4665 FIXME: this works around a limitation in fold() when dealing with
4666 enumerations. Given 'enum { N1, N2 } x;', fold will not
4667 fold 'if (x > N2)' to 'if (0)'. */
4668 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4669 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4671 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4672 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4674 if (comp_code == GT_EXPR
4675 && (!max
4676 || compare_values (val, max) == 0))
4677 return false;
4679 if (comp_code == LT_EXPR
4680 && (!min
4681 || compare_values (val, min) == 0))
4682 return false;
4684 *code_p = comp_code;
4685 *val_p = val;
4686 return true;
4689 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4690 (otherwise return VAL). VAL and MASK must be zero-extended for
4691 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4692 (to transform signed values into unsigned) and at the end xor
4693 SGNBIT back. */
4695 static wide_int
4696 masked_increment (const wide_int &val_in, const wide_int &mask,
4697 const wide_int &sgnbit, unsigned int prec)
4699 wide_int bit = wi::one (prec), res;
4700 unsigned int i;
4702 wide_int val = val_in ^ sgnbit;
4703 for (i = 0; i < prec; i++, bit += bit)
4705 res = mask;
4706 if ((res & bit) == 0)
4707 continue;
4708 res = bit - 1;
4709 res = (val + bit).and_not (res);
4710 res &= mask;
4711 if (wi::gtu_p (res, val))
4712 return res ^ sgnbit;
4714 return val ^ sgnbit;
4717 /* Try to register an edge assertion for SSA name NAME on edge E for
4718 the condition COND contributing to the conditional jump pointed to by BSI.
4719 Invert the condition COND if INVERT is true.
4720 Return true if an assertion for NAME could be registered. */
4722 static bool
4723 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4724 enum tree_code cond_code,
4725 tree cond_op0, tree cond_op1, bool invert)
4727 tree val;
4728 enum tree_code comp_code;
4729 bool retval = false;
4731 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4732 cond_op0,
4733 cond_op1,
4734 invert, &comp_code, &val))
4735 return false;
4737 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4738 reachable from E. */
4739 if (live_on_edge (e, name)
4740 && !has_single_use (name))
4742 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
4743 retval = true;
4746 /* In the case of NAME <= CST and NAME being defined as
4747 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4748 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4749 This catches range and anti-range tests. */
4750 if ((comp_code == LE_EXPR
4751 || comp_code == GT_EXPR)
4752 && TREE_CODE (val) == INTEGER_CST
4753 && TYPE_UNSIGNED (TREE_TYPE (val)))
4755 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4756 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
4758 /* Extract CST2 from the (optional) addition. */
4759 if (is_gimple_assign (def_stmt)
4760 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
4762 name2 = gimple_assign_rhs1 (def_stmt);
4763 cst2 = gimple_assign_rhs2 (def_stmt);
4764 if (TREE_CODE (name2) == SSA_NAME
4765 && TREE_CODE (cst2) == INTEGER_CST)
4766 def_stmt = SSA_NAME_DEF_STMT (name2);
4769 /* Extract NAME2 from the (optional) sign-changing cast. */
4770 if (gimple_assign_cast_p (def_stmt))
4772 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4773 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4774 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
4775 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
4776 name3 = gimple_assign_rhs1 (def_stmt);
4779 /* If name3 is used later, create an ASSERT_EXPR for it. */
4780 if (name3 != NULL_TREE
4781 && TREE_CODE (name3) == SSA_NAME
4782 && (cst2 == NULL_TREE
4783 || TREE_CODE (cst2) == INTEGER_CST)
4784 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
4785 && live_on_edge (e, name3)
4786 && !has_single_use (name3))
4788 tree tmp;
4790 /* Build an expression for the range test. */
4791 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
4792 if (cst2 != NULL_TREE)
4793 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4795 if (dump_file)
4797 fprintf (dump_file, "Adding assert for ");
4798 print_generic_expr (dump_file, name3, 0);
4799 fprintf (dump_file, " from ");
4800 print_generic_expr (dump_file, tmp, 0);
4801 fprintf (dump_file, "\n");
4804 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
4806 retval = true;
4809 /* If name2 is used later, create an ASSERT_EXPR for it. */
4810 if (name2 != NULL_TREE
4811 && TREE_CODE (name2) == SSA_NAME
4812 && TREE_CODE (cst2) == INTEGER_CST
4813 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4814 && live_on_edge (e, name2)
4815 && !has_single_use (name2))
4817 tree tmp;
4819 /* Build an expression for the range test. */
4820 tmp = name2;
4821 if (TREE_TYPE (name) != TREE_TYPE (name2))
4822 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
4823 if (cst2 != NULL_TREE)
4824 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4826 if (dump_file)
4828 fprintf (dump_file, "Adding assert for ");
4829 print_generic_expr (dump_file, name2, 0);
4830 fprintf (dump_file, " from ");
4831 print_generic_expr (dump_file, tmp, 0);
4832 fprintf (dump_file, "\n");
4835 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
4837 retval = true;
4841 /* In the case of post-in/decrement tests like if (i++) ... and uses
4842 of the in/decremented value on the edge the extra name we want to
4843 assert for is not on the def chain of the name compared. Instead
4844 it is in the set of use stmts. */
4845 if ((comp_code == NE_EXPR
4846 || comp_code == EQ_EXPR)
4847 && TREE_CODE (val) == INTEGER_CST)
4849 imm_use_iterator ui;
4850 gimple use_stmt;
4851 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
4853 /* Cut off to use-stmts that are in the predecessor. */
4854 if (gimple_bb (use_stmt) != e->src)
4855 continue;
4857 if (!is_gimple_assign (use_stmt))
4858 continue;
4860 enum tree_code code = gimple_assign_rhs_code (use_stmt);
4861 if (code != PLUS_EXPR
4862 && code != MINUS_EXPR)
4863 continue;
4865 tree cst = gimple_assign_rhs2 (use_stmt);
4866 if (TREE_CODE (cst) != INTEGER_CST)
4867 continue;
4869 tree name2 = gimple_assign_lhs (use_stmt);
4870 if (live_on_edge (e, name2))
4872 cst = int_const_binop (code, val, cst);
4873 register_new_assert_for (name2, name2, comp_code, cst,
4874 NULL, e, bsi);
4875 retval = true;
4880 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
4881 && TREE_CODE (val) == INTEGER_CST)
4883 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4884 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
4885 tree val2 = NULL_TREE;
4886 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
4887 wide_int mask = wi::zero (prec);
4888 unsigned int nprec = prec;
4889 enum tree_code rhs_code = ERROR_MARK;
4891 if (is_gimple_assign (def_stmt))
4892 rhs_code = gimple_assign_rhs_code (def_stmt);
4894 /* Add asserts for NAME cmp CST and NAME being defined
4895 as NAME = (int) NAME2. */
4896 if (!TYPE_UNSIGNED (TREE_TYPE (val))
4897 && (comp_code == LE_EXPR || comp_code == LT_EXPR
4898 || comp_code == GT_EXPR || comp_code == GE_EXPR)
4899 && gimple_assign_cast_p (def_stmt))
4901 name2 = gimple_assign_rhs1 (def_stmt);
4902 if (CONVERT_EXPR_CODE_P (rhs_code)
4903 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4904 && TYPE_UNSIGNED (TREE_TYPE (name2))
4905 && prec == TYPE_PRECISION (TREE_TYPE (name2))
4906 && (comp_code == LE_EXPR || comp_code == GT_EXPR
4907 || !tree_int_cst_equal (val,
4908 TYPE_MIN_VALUE (TREE_TYPE (val))))
4909 && live_on_edge (e, name2)
4910 && !has_single_use (name2))
4912 tree tmp, cst;
4913 enum tree_code new_comp_code = comp_code;
4915 cst = fold_convert (TREE_TYPE (name2),
4916 TYPE_MIN_VALUE (TREE_TYPE (val)));
4917 /* Build an expression for the range test. */
4918 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
4919 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
4920 fold_convert (TREE_TYPE (name2), val));
4921 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4923 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
4924 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
4925 build_int_cst (TREE_TYPE (name2), 1));
4928 if (dump_file)
4930 fprintf (dump_file, "Adding assert for ");
4931 print_generic_expr (dump_file, name2, 0);
4932 fprintf (dump_file, " from ");
4933 print_generic_expr (dump_file, tmp, 0);
4934 fprintf (dump_file, "\n");
4937 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
4938 e, bsi);
4940 retval = true;
4944 /* Add asserts for NAME cmp CST and NAME being defined as
4945 NAME = NAME2 >> CST2.
4947 Extract CST2 from the right shift. */
4948 if (rhs_code == RSHIFT_EXPR)
4950 name2 = gimple_assign_rhs1 (def_stmt);
4951 cst2 = gimple_assign_rhs2 (def_stmt);
4952 if (TREE_CODE (name2) == SSA_NAME
4953 && tree_fits_uhwi_p (cst2)
4954 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4955 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
4956 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
4957 && live_on_edge (e, name2)
4958 && !has_single_use (name2))
4960 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
4961 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
4964 if (val2 != NULL_TREE
4965 && TREE_CODE (val2) == INTEGER_CST
4966 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
4967 TREE_TYPE (val),
4968 val2, cst2), val))
4970 enum tree_code new_comp_code = comp_code;
4971 tree tmp, new_val;
4973 tmp = name2;
4974 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
4976 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
4978 tree type = build_nonstandard_integer_type (prec, 1);
4979 tmp = build1 (NOP_EXPR, type, name2);
4980 val2 = fold_convert (type, val2);
4982 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
4983 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
4984 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
4986 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4988 wide_int minval
4989 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
4990 new_val = val2;
4991 if (minval == new_val)
4992 new_val = NULL_TREE;
4994 else
4996 wide_int maxval
4997 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
4998 mask |= val2;
4999 if (mask == maxval)
5000 new_val = NULL_TREE;
5001 else
5002 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5005 if (new_val)
5007 if (dump_file)
5009 fprintf (dump_file, "Adding assert for ");
5010 print_generic_expr (dump_file, name2, 0);
5011 fprintf (dump_file, " from ");
5012 print_generic_expr (dump_file, tmp, 0);
5013 fprintf (dump_file, "\n");
5016 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5017 NULL, e, bsi);
5018 retval = true;
5022 /* Add asserts for NAME cmp CST and NAME being defined as
5023 NAME = NAME2 & CST2.
5025 Extract CST2 from the and.
5027 Also handle
5028 NAME = (unsigned) NAME2;
5029 casts where NAME's type is unsigned and has smaller precision
5030 than NAME2's type as if it was NAME = NAME2 & MASK. */
5031 names[0] = NULL_TREE;
5032 names[1] = NULL_TREE;
5033 cst2 = NULL_TREE;
5034 if (rhs_code == BIT_AND_EXPR
5035 || (CONVERT_EXPR_CODE_P (rhs_code)
5036 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5037 && TYPE_UNSIGNED (TREE_TYPE (val))
5038 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5039 > prec
5040 && !retval))
5042 name2 = gimple_assign_rhs1 (def_stmt);
5043 if (rhs_code == BIT_AND_EXPR)
5044 cst2 = gimple_assign_rhs2 (def_stmt);
5045 else
5047 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5048 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5050 if (TREE_CODE (name2) == SSA_NAME
5051 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5052 && TREE_CODE (cst2) == INTEGER_CST
5053 && !integer_zerop (cst2)
5054 && (nprec > 1
5055 || TYPE_UNSIGNED (TREE_TYPE (val))))
5057 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5058 if (gimple_assign_cast_p (def_stmt2))
5060 names[1] = gimple_assign_rhs1 (def_stmt2);
5061 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5062 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5063 || (TYPE_PRECISION (TREE_TYPE (name2))
5064 != TYPE_PRECISION (TREE_TYPE (names[1])))
5065 || !live_on_edge (e, names[1])
5066 || has_single_use (names[1]))
5067 names[1] = NULL_TREE;
5069 if (live_on_edge (e, name2)
5070 && !has_single_use (name2))
5071 names[0] = name2;
5074 if (names[0] || names[1])
5076 wide_int minv, maxv, valv, cst2v;
5077 wide_int tem, sgnbit;
5078 bool valid_p = false, valn, cst2n;
5079 enum tree_code ccode = comp_code;
5081 valv = wide_int::from (val, nprec, UNSIGNED);
5082 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5083 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5084 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5085 /* If CST2 doesn't have most significant bit set,
5086 but VAL is negative, we have comparison like
5087 if ((x & 0x123) > -4) (always true). Just give up. */
5088 if (!cst2n && valn)
5089 ccode = ERROR_MARK;
5090 if (cst2n)
5091 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5092 else
5093 sgnbit = wi::zero (nprec);
5094 minv = valv & cst2v;
5095 switch (ccode)
5097 case EQ_EXPR:
5098 /* Minimum unsigned value for equality is VAL & CST2
5099 (should be equal to VAL, otherwise we probably should
5100 have folded the comparison into false) and
5101 maximum unsigned value is VAL | ~CST2. */
5102 maxv = valv | ~cst2v;
5103 valid_p = true;
5104 break;
5106 case NE_EXPR:
5107 tem = valv | ~cst2v;
5108 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5109 if (valv == 0)
5111 cst2n = false;
5112 sgnbit = wi::zero (nprec);
5113 goto gt_expr;
5115 /* If (VAL | ~CST2) is all ones, handle it as
5116 (X & CST2) < VAL. */
5117 if (tem == -1)
5119 cst2n = false;
5120 valn = false;
5121 sgnbit = wi::zero (nprec);
5122 goto lt_expr;
5124 if (!cst2n && wi::neg_p (cst2v))
5125 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5126 if (sgnbit != 0)
5128 if (valv == sgnbit)
5130 cst2n = true;
5131 valn = true;
5132 goto gt_expr;
5134 if (tem == wi::mask (nprec - 1, false, nprec))
5136 cst2n = true;
5137 goto lt_expr;
5139 if (!cst2n)
5140 sgnbit = wi::zero (nprec);
5142 break;
5144 case GE_EXPR:
5145 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5146 is VAL and maximum unsigned value is ~0. For signed
5147 comparison, if CST2 doesn't have most significant bit
5148 set, handle it similarly. If CST2 has MSB set,
5149 the minimum is the same, and maximum is ~0U/2. */
5150 if (minv != valv)
5152 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5153 VAL. */
5154 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5155 if (minv == valv)
5156 break;
5158 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5159 valid_p = true;
5160 break;
5162 case GT_EXPR:
5163 gt_expr:
5164 /* Find out smallest MINV where MINV > VAL
5165 && (MINV & CST2) == MINV, if any. If VAL is signed and
5166 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5167 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5168 if (minv == valv)
5169 break;
5170 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5171 valid_p = true;
5172 break;
5174 case LE_EXPR:
5175 /* Minimum unsigned value for <= is 0 and maximum
5176 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5177 Otherwise, find smallest VAL2 where VAL2 > VAL
5178 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5179 as maximum.
5180 For signed comparison, if CST2 doesn't have most
5181 significant bit set, handle it similarly. If CST2 has
5182 MSB set, the maximum is the same and minimum is INT_MIN. */
5183 if (minv == valv)
5184 maxv = valv;
5185 else
5187 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5188 if (maxv == valv)
5189 break;
5190 maxv -= 1;
5192 maxv |= ~cst2v;
5193 minv = sgnbit;
5194 valid_p = true;
5195 break;
5197 case LT_EXPR:
5198 lt_expr:
5199 /* Minimum unsigned value for < is 0 and maximum
5200 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5201 Otherwise, find smallest VAL2 where VAL2 > VAL
5202 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5203 as maximum.
5204 For signed comparison, if CST2 doesn't have most
5205 significant bit set, handle it similarly. If CST2 has
5206 MSB set, the maximum is the same and minimum is INT_MIN. */
5207 if (minv == valv)
5209 if (valv == sgnbit)
5210 break;
5211 maxv = valv;
5213 else
5215 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5216 if (maxv == valv)
5217 break;
5219 maxv -= 1;
5220 maxv |= ~cst2v;
5221 minv = sgnbit;
5222 valid_p = true;
5223 break;
5225 default:
5226 break;
5228 if (valid_p
5229 && (maxv - minv) != -1)
5231 tree tmp, new_val, type;
5232 int i;
5234 for (i = 0; i < 2; i++)
5235 if (names[i])
5237 wide_int maxv2 = maxv;
5238 tmp = names[i];
5239 type = TREE_TYPE (names[i]);
5240 if (!TYPE_UNSIGNED (type))
5242 type = build_nonstandard_integer_type (nprec, 1);
5243 tmp = build1 (NOP_EXPR, type, names[i]);
5245 if (minv != 0)
5247 tmp = build2 (PLUS_EXPR, type, tmp,
5248 wide_int_to_tree (type, -minv));
5249 maxv2 = maxv - minv;
5251 new_val = wide_int_to_tree (type, maxv2);
5253 if (dump_file)
5255 fprintf (dump_file, "Adding assert for ");
5256 print_generic_expr (dump_file, names[i], 0);
5257 fprintf (dump_file, " from ");
5258 print_generic_expr (dump_file, tmp, 0);
5259 fprintf (dump_file, "\n");
5262 register_new_assert_for (names[i], tmp, LE_EXPR,
5263 new_val, NULL, e, bsi);
5264 retval = true;
5270 return retval;
5273 /* OP is an operand of a truth value expression which is known to have
5274 a particular value. Register any asserts for OP and for any
5275 operands in OP's defining statement.
5277 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5278 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5280 static bool
5281 register_edge_assert_for_1 (tree op, enum tree_code code,
5282 edge e, gimple_stmt_iterator bsi)
5284 bool retval = false;
5285 gimple op_def;
5286 tree val;
5287 enum tree_code rhs_code;
5289 /* We only care about SSA_NAMEs. */
5290 if (TREE_CODE (op) != SSA_NAME)
5291 return false;
5293 /* We know that OP will have a zero or nonzero value. If OP is used
5294 more than once go ahead and register an assert for OP. */
5295 if (live_on_edge (e, op)
5296 && !has_single_use (op))
5298 val = build_int_cst (TREE_TYPE (op), 0);
5299 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5300 retval = true;
5303 /* Now look at how OP is set. If it's set from a comparison,
5304 a truth operation or some bit operations, then we may be able
5305 to register information about the operands of that assignment. */
5306 op_def = SSA_NAME_DEF_STMT (op);
5307 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5308 return retval;
5310 rhs_code = gimple_assign_rhs_code (op_def);
5312 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5314 bool invert = (code == EQ_EXPR ? true : false);
5315 tree op0 = gimple_assign_rhs1 (op_def);
5316 tree op1 = gimple_assign_rhs2 (op_def);
5318 if (TREE_CODE (op0) == SSA_NAME)
5319 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5320 invert);
5321 if (TREE_CODE (op1) == SSA_NAME)
5322 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5323 invert);
5325 else if ((code == NE_EXPR
5326 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5327 || (code == EQ_EXPR
5328 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5330 /* Recurse on each operand. */
5331 tree op0 = gimple_assign_rhs1 (op_def);
5332 tree op1 = gimple_assign_rhs2 (op_def);
5333 if (TREE_CODE (op0) == SSA_NAME
5334 && has_single_use (op0))
5335 retval |= register_edge_assert_for_1 (op0, code, e, bsi);
5336 if (TREE_CODE (op1) == SSA_NAME
5337 && has_single_use (op1))
5338 retval |= register_edge_assert_for_1 (op1, code, e, bsi);
5340 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5341 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5343 /* Recurse, flipping CODE. */
5344 code = invert_tree_comparison (code, false);
5345 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5346 code, e, bsi);
5348 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5350 /* Recurse through the copy. */
5351 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5352 code, e, bsi);
5354 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5356 /* Recurse through the type conversion, unless it is a narrowing
5357 conversion or conversion from non-integral type. */
5358 tree rhs = gimple_assign_rhs1 (op_def);
5359 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5360 && (TYPE_PRECISION (TREE_TYPE (rhs))
5361 <= TYPE_PRECISION (TREE_TYPE (op))))
5362 retval |= register_edge_assert_for_1 (rhs, code, e, bsi);
5365 return retval;
5368 /* Try to register an edge assertion for SSA name NAME on edge E for
5369 the condition COND contributing to the conditional jump pointed to by SI.
5370 Return true if an assertion for NAME could be registered. */
5372 static bool
5373 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5374 enum tree_code cond_code, tree cond_op0,
5375 tree cond_op1)
5377 tree val;
5378 enum tree_code comp_code;
5379 bool retval = false;
5380 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5382 /* Do not attempt to infer anything in names that flow through
5383 abnormal edges. */
5384 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5385 return false;
5387 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5388 cond_op0, cond_op1,
5389 is_else_edge,
5390 &comp_code, &val))
5391 return false;
5393 /* Register ASSERT_EXPRs for name. */
5394 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5395 cond_op1, is_else_edge);
5398 /* If COND is effectively an equality test of an SSA_NAME against
5399 the value zero or one, then we may be able to assert values
5400 for SSA_NAMEs which flow into COND. */
5402 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5403 statement of NAME we can assert both operands of the BIT_AND_EXPR
5404 have nonzero value. */
5405 if (((comp_code == EQ_EXPR && integer_onep (val))
5406 || (comp_code == NE_EXPR && integer_zerop (val))))
5408 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5410 if (is_gimple_assign (def_stmt)
5411 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5413 tree op0 = gimple_assign_rhs1 (def_stmt);
5414 tree op1 = gimple_assign_rhs2 (def_stmt);
5415 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5416 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5420 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5421 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5422 have zero value. */
5423 if (((comp_code == EQ_EXPR && integer_zerop (val))
5424 || (comp_code == NE_EXPR && integer_onep (val))))
5426 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5428 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5429 necessarily zero value, or if type-precision is one. */
5430 if (is_gimple_assign (def_stmt)
5431 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5432 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5433 || comp_code == EQ_EXPR)))
5435 tree op0 = gimple_assign_rhs1 (def_stmt);
5436 tree op1 = gimple_assign_rhs2 (def_stmt);
5437 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5438 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5442 return retval;
5446 /* Determine whether the outgoing edges of BB should receive an
5447 ASSERT_EXPR for each of the operands of BB's LAST statement.
5448 The last statement of BB must be a COND_EXPR.
5450 If any of the sub-graphs rooted at BB have an interesting use of
5451 the predicate operands, an assert location node is added to the
5452 list of assertions for the corresponding operands. */
5454 static bool
5455 find_conditional_asserts (basic_block bb, gimple last)
5457 bool need_assert;
5458 gimple_stmt_iterator bsi;
5459 tree op;
5460 edge_iterator ei;
5461 edge e;
5462 ssa_op_iter iter;
5464 need_assert = false;
5465 bsi = gsi_for_stmt (last);
5467 /* Look for uses of the operands in each of the sub-graphs
5468 rooted at BB. We need to check each of the outgoing edges
5469 separately, so that we know what kind of ASSERT_EXPR to
5470 insert. */
5471 FOR_EACH_EDGE (e, ei, bb->succs)
5473 if (e->dest == bb)
5474 continue;
5476 /* Register the necessary assertions for each operand in the
5477 conditional predicate. */
5478 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5480 need_assert |= register_edge_assert_for (op, e, bsi,
5481 gimple_cond_code (last),
5482 gimple_cond_lhs (last),
5483 gimple_cond_rhs (last));
5487 return need_assert;
5490 struct case_info
5492 tree expr;
5493 basic_block bb;
5496 /* Compare two case labels sorting first by the destination bb index
5497 and then by the case value. */
5499 static int
5500 compare_case_labels (const void *p1, const void *p2)
5502 const struct case_info *ci1 = (const struct case_info *) p1;
5503 const struct case_info *ci2 = (const struct case_info *) p2;
5504 int idx1 = ci1->bb->index;
5505 int idx2 = ci2->bb->index;
5507 if (idx1 < idx2)
5508 return -1;
5509 else if (idx1 == idx2)
5511 /* Make sure the default label is first in a group. */
5512 if (!CASE_LOW (ci1->expr))
5513 return -1;
5514 else if (!CASE_LOW (ci2->expr))
5515 return 1;
5516 else
5517 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5518 CASE_LOW (ci2->expr));
5520 else
5521 return 1;
5524 /* Determine whether the outgoing edges of BB should receive an
5525 ASSERT_EXPR for each of the operands of BB's LAST statement.
5526 The last statement of BB must be a SWITCH_EXPR.
5528 If any of the sub-graphs rooted at BB have an interesting use of
5529 the predicate operands, an assert location node is added to the
5530 list of assertions for the corresponding operands. */
5532 static bool
5533 find_switch_asserts (basic_block bb, gimple last)
5535 bool need_assert;
5536 gimple_stmt_iterator bsi;
5537 tree op;
5538 edge e;
5539 struct case_info *ci;
5540 size_t n = gimple_switch_num_labels (last);
5541 #if GCC_VERSION >= 4000
5542 unsigned int idx;
5543 #else
5544 /* Work around GCC 3.4 bug (PR 37086). */
5545 volatile unsigned int idx;
5546 #endif
5548 need_assert = false;
5549 bsi = gsi_for_stmt (last);
5550 op = gimple_switch_index (last);
5551 if (TREE_CODE (op) != SSA_NAME)
5552 return false;
5554 /* Build a vector of case labels sorted by destination label. */
5555 ci = XNEWVEC (struct case_info, n);
5556 for (idx = 0; idx < n; ++idx)
5558 ci[idx].expr = gimple_switch_label (last, idx);
5559 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5561 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5563 for (idx = 0; idx < n; ++idx)
5565 tree min, max;
5566 tree cl = ci[idx].expr;
5567 basic_block cbb = ci[idx].bb;
5569 min = CASE_LOW (cl);
5570 max = CASE_HIGH (cl);
5572 /* If there are multiple case labels with the same destination
5573 we need to combine them to a single value range for the edge. */
5574 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5576 /* Skip labels until the last of the group. */
5577 do {
5578 ++idx;
5579 } while (idx < n && cbb == ci[idx].bb);
5580 --idx;
5582 /* Pick up the maximum of the case label range. */
5583 if (CASE_HIGH (ci[idx].expr))
5584 max = CASE_HIGH (ci[idx].expr);
5585 else
5586 max = CASE_LOW (ci[idx].expr);
5589 /* Nothing to do if the range includes the default label until we
5590 can register anti-ranges. */
5591 if (min == NULL_TREE)
5592 continue;
5594 /* Find the edge to register the assert expr on. */
5595 e = find_edge (bb, cbb);
5597 /* Register the necessary assertions for the operand in the
5598 SWITCH_EXPR. */
5599 need_assert |= register_edge_assert_for (op, e, bsi,
5600 max ? GE_EXPR : EQ_EXPR,
5602 fold_convert (TREE_TYPE (op),
5603 min));
5604 if (max)
5606 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5608 fold_convert (TREE_TYPE (op),
5609 max));
5613 XDELETEVEC (ci);
5614 return need_assert;
5618 /* Traverse all the statements in block BB looking for statements that
5619 may generate useful assertions for the SSA names in their operand.
5620 If a statement produces a useful assertion A for name N_i, then the
5621 list of assertions already generated for N_i is scanned to
5622 determine if A is actually needed.
5624 If N_i already had the assertion A at a location dominating the
5625 current location, then nothing needs to be done. Otherwise, the
5626 new location for A is recorded instead.
5628 1- For every statement S in BB, all the variables used by S are
5629 added to bitmap FOUND_IN_SUBGRAPH.
5631 2- If statement S uses an operand N in a way that exposes a known
5632 value range for N, then if N was not already generated by an
5633 ASSERT_EXPR, create a new assert location for N. For instance,
5634 if N is a pointer and the statement dereferences it, we can
5635 assume that N is not NULL.
5637 3- COND_EXPRs are a special case of #2. We can derive range
5638 information from the predicate but need to insert different
5639 ASSERT_EXPRs for each of the sub-graphs rooted at the
5640 conditional block. If the last statement of BB is a conditional
5641 expression of the form 'X op Y', then
5643 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5645 b) If the conditional is the only entry point to the sub-graph
5646 corresponding to the THEN_CLAUSE, recurse into it. On
5647 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5648 an ASSERT_EXPR is added for the corresponding variable.
5650 c) Repeat step (b) on the ELSE_CLAUSE.
5652 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5654 For instance,
5656 if (a == 9)
5657 b = a;
5658 else
5659 b = c + 1;
5661 In this case, an assertion on the THEN clause is useful to
5662 determine that 'a' is always 9 on that edge. However, an assertion
5663 on the ELSE clause would be unnecessary.
5665 4- If BB does not end in a conditional expression, then we recurse
5666 into BB's dominator children.
5668 At the end of the recursive traversal, every SSA name will have a
5669 list of locations where ASSERT_EXPRs should be added. When a new
5670 location for name N is found, it is registered by calling
5671 register_new_assert_for. That function keeps track of all the
5672 registered assertions to prevent adding unnecessary assertions.
5673 For instance, if a pointer P_4 is dereferenced more than once in a
5674 dominator tree, only the location dominating all the dereference of
5675 P_4 will receive an ASSERT_EXPR.
5677 If this function returns true, then it means that there are names
5678 for which we need to generate ASSERT_EXPRs. Those assertions are
5679 inserted by process_assert_insertions. */
5681 static bool
5682 find_assert_locations_1 (basic_block bb, sbitmap live)
5684 gimple_stmt_iterator si;
5685 gimple last;
5686 bool need_assert;
5688 need_assert = false;
5689 last = last_stmt (bb);
5691 /* If BB's last statement is a conditional statement involving integer
5692 operands, determine if we need to add ASSERT_EXPRs. */
5693 if (last
5694 && gimple_code (last) == GIMPLE_COND
5695 && !fp_predicate (last)
5696 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5697 need_assert |= find_conditional_asserts (bb, last);
5699 /* If BB's last statement is a switch statement involving integer
5700 operands, determine if we need to add ASSERT_EXPRs. */
5701 if (last
5702 && gimple_code (last) == GIMPLE_SWITCH
5703 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5704 need_assert |= find_switch_asserts (bb, last);
5706 /* Traverse all the statements in BB marking used names and looking
5707 for statements that may infer assertions for their used operands. */
5708 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5710 gimple stmt;
5711 tree op;
5712 ssa_op_iter i;
5714 stmt = gsi_stmt (si);
5716 if (is_gimple_debug (stmt))
5717 continue;
5719 /* See if we can derive an assertion for any of STMT's operands. */
5720 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5722 tree value;
5723 enum tree_code comp_code;
5725 /* If op is not live beyond this stmt, do not bother to insert
5726 asserts for it. */
5727 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5728 continue;
5730 /* If OP is used in such a way that we can infer a value
5731 range for it, and we don't find a previous assertion for
5732 it, create a new assertion location node for OP. */
5733 if (infer_value_range (stmt, op, &comp_code, &value))
5735 /* If we are able to infer a nonzero value range for OP,
5736 then walk backwards through the use-def chain to see if OP
5737 was set via a typecast.
5739 If so, then we can also infer a nonzero value range
5740 for the operand of the NOP_EXPR. */
5741 if (comp_code == NE_EXPR && integer_zerop (value))
5743 tree t = op;
5744 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5746 while (is_gimple_assign (def_stmt)
5747 && gimple_assign_rhs_code (def_stmt) == NOP_EXPR
5748 && TREE_CODE
5749 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5750 && POINTER_TYPE_P
5751 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
5753 t = gimple_assign_rhs1 (def_stmt);
5754 def_stmt = SSA_NAME_DEF_STMT (t);
5756 /* Note we want to register the assert for the
5757 operand of the NOP_EXPR after SI, not after the
5758 conversion. */
5759 if (! has_single_use (t))
5761 register_new_assert_for (t, t, comp_code, value,
5762 bb, NULL, si);
5763 need_assert = true;
5768 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
5769 need_assert = true;
5773 /* Update live. */
5774 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5775 bitmap_set_bit (live, SSA_NAME_VERSION (op));
5776 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
5777 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
5780 /* Traverse all PHI nodes in BB, updating live. */
5781 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5783 use_operand_p arg_p;
5784 ssa_op_iter i;
5785 gimple phi = gsi_stmt (si);
5786 tree res = gimple_phi_result (phi);
5788 if (virtual_operand_p (res))
5789 continue;
5791 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
5793 tree arg = USE_FROM_PTR (arg_p);
5794 if (TREE_CODE (arg) == SSA_NAME)
5795 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
5798 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
5801 return need_assert;
5804 /* Do an RPO walk over the function computing SSA name liveness
5805 on-the-fly and deciding on assert expressions to insert.
5806 Returns true if there are assert expressions to be inserted. */
5808 static bool
5809 find_assert_locations (void)
5811 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
5812 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
5813 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
5814 int rpo_cnt, i;
5815 bool need_asserts;
5817 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
5818 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
5819 for (i = 0; i < rpo_cnt; ++i)
5820 bb_rpo[rpo[i]] = i;
5822 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
5823 the order we compute liveness and insert asserts we otherwise
5824 fail to insert asserts into the loop latch. */
5825 loop_p loop;
5826 FOR_EACH_LOOP (loop, 0)
5828 i = loop->latch->index;
5829 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
5830 for (gimple_stmt_iterator gsi = gsi_start_phis (loop->header);
5831 !gsi_end_p (gsi); gsi_next (&gsi))
5833 gimple phi = gsi_stmt (gsi);
5834 if (virtual_operand_p (gimple_phi_result (phi)))
5835 continue;
5836 tree arg = gimple_phi_arg_def (phi, j);
5837 if (TREE_CODE (arg) == SSA_NAME)
5839 if (live[i] == NULL)
5841 live[i] = sbitmap_alloc (num_ssa_names);
5842 bitmap_clear (live[i]);
5844 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
5849 need_asserts = false;
5850 for (i = rpo_cnt - 1; i >= 0; --i)
5852 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
5853 edge e;
5854 edge_iterator ei;
5856 if (!live[rpo[i]])
5858 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
5859 bitmap_clear (live[rpo[i]]);
5862 /* Process BB and update the live information with uses in
5863 this block. */
5864 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
5866 /* Merge liveness into the predecessor blocks and free it. */
5867 if (!bitmap_empty_p (live[rpo[i]]))
5869 int pred_rpo = i;
5870 FOR_EACH_EDGE (e, ei, bb->preds)
5872 int pred = e->src->index;
5873 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
5874 continue;
5876 if (!live[pred])
5878 live[pred] = sbitmap_alloc (num_ssa_names);
5879 bitmap_clear (live[pred]);
5881 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
5883 if (bb_rpo[pred] < pred_rpo)
5884 pred_rpo = bb_rpo[pred];
5887 /* Record the RPO number of the last visited block that needs
5888 live information from this block. */
5889 last_rpo[rpo[i]] = pred_rpo;
5891 else
5893 sbitmap_free (live[rpo[i]]);
5894 live[rpo[i]] = NULL;
5897 /* We can free all successors live bitmaps if all their
5898 predecessors have been visited already. */
5899 FOR_EACH_EDGE (e, ei, bb->succs)
5900 if (last_rpo[e->dest->index] == i
5901 && live[e->dest->index])
5903 sbitmap_free (live[e->dest->index]);
5904 live[e->dest->index] = NULL;
5908 XDELETEVEC (rpo);
5909 XDELETEVEC (bb_rpo);
5910 XDELETEVEC (last_rpo);
5911 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
5912 if (live[i])
5913 sbitmap_free (live[i]);
5914 XDELETEVEC (live);
5916 return need_asserts;
5919 /* Create an ASSERT_EXPR for NAME and insert it in the location
5920 indicated by LOC. Return true if we made any edge insertions. */
5922 static bool
5923 process_assert_insertions_for (tree name, assert_locus_t loc)
5925 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5926 gimple stmt;
5927 tree cond;
5928 gimple assert_stmt;
5929 edge_iterator ei;
5930 edge e;
5932 /* If we have X <=> X do not insert an assert expr for that. */
5933 if (loc->expr == loc->val)
5934 return false;
5936 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
5937 assert_stmt = build_assert_expr_for (cond, name);
5938 if (loc->e)
5940 /* We have been asked to insert the assertion on an edge. This
5941 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5942 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
5943 || (gimple_code (gsi_stmt (loc->si))
5944 == GIMPLE_SWITCH));
5946 gsi_insert_on_edge (loc->e, assert_stmt);
5947 return true;
5950 /* Otherwise, we can insert right after LOC->SI iff the
5951 statement must not be the last statement in the block. */
5952 stmt = gsi_stmt (loc->si);
5953 if (!stmt_ends_bb_p (stmt))
5955 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
5956 return false;
5959 /* If STMT must be the last statement in BB, we can only insert new
5960 assertions on the non-abnormal edge out of BB. Note that since
5961 STMT is not control flow, there may only be one non-abnormal edge
5962 out of BB. */
5963 FOR_EACH_EDGE (e, ei, loc->bb->succs)
5964 if (!(e->flags & EDGE_ABNORMAL))
5966 gsi_insert_on_edge (e, assert_stmt);
5967 return true;
5970 gcc_unreachable ();
5974 /* Process all the insertions registered for every name N_i registered
5975 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5976 found in ASSERTS_FOR[i]. */
5978 static void
5979 process_assert_insertions (void)
5981 unsigned i;
5982 bitmap_iterator bi;
5983 bool update_edges_p = false;
5984 int num_asserts = 0;
5986 if (dump_file && (dump_flags & TDF_DETAILS))
5987 dump_all_asserts (dump_file);
5989 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
5991 assert_locus_t loc = asserts_for[i];
5992 gcc_assert (loc);
5994 while (loc)
5996 assert_locus_t next = loc->next;
5997 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
5998 free (loc);
5999 loc = next;
6000 num_asserts++;
6004 if (update_edges_p)
6005 gsi_commit_edge_inserts ();
6007 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6008 num_asserts);
6012 /* Traverse the flowgraph looking for conditional jumps to insert range
6013 expressions. These range expressions are meant to provide information
6014 to optimizations that need to reason in terms of value ranges. They
6015 will not be expanded into RTL. For instance, given:
6017 x = ...
6018 y = ...
6019 if (x < y)
6020 y = x - 2;
6021 else
6022 x = y + 3;
6024 this pass will transform the code into:
6026 x = ...
6027 y = ...
6028 if (x < y)
6030 x = ASSERT_EXPR <x, x < y>
6031 y = x - 2
6033 else
6035 y = ASSERT_EXPR <y, x >= y>
6036 x = y + 3
6039 The idea is that once copy and constant propagation have run, other
6040 optimizations will be able to determine what ranges of values can 'x'
6041 take in different paths of the code, simply by checking the reaching
6042 definition of 'x'. */
6044 static void
6045 insert_range_assertions (void)
6047 need_assert_for = BITMAP_ALLOC (NULL);
6048 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6050 calculate_dominance_info (CDI_DOMINATORS);
6052 if (find_assert_locations ())
6054 process_assert_insertions ();
6055 update_ssa (TODO_update_ssa_no_phi);
6058 if (dump_file && (dump_flags & TDF_DETAILS))
6060 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6061 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6064 free (asserts_for);
6065 BITMAP_FREE (need_assert_for);
6068 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6069 and "struct" hacks. If VRP can determine that the
6070 array subscript is a constant, check if it is outside valid
6071 range. If the array subscript is a RANGE, warn if it is
6072 non-overlapping with valid range.
6073 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6075 static void
6076 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6078 value_range_t* vr = NULL;
6079 tree low_sub, up_sub;
6080 tree low_bound, up_bound, up_bound_p1;
6081 tree base;
6083 if (TREE_NO_WARNING (ref))
6084 return;
6086 low_sub = up_sub = TREE_OPERAND (ref, 1);
6087 up_bound = array_ref_up_bound (ref);
6089 /* Can not check flexible arrays. */
6090 if (!up_bound
6091 || TREE_CODE (up_bound) != INTEGER_CST)
6092 return;
6094 /* Accesses to trailing arrays via pointers may access storage
6095 beyond the types array bounds. */
6096 base = get_base_address (ref);
6097 if (base && TREE_CODE (base) == MEM_REF)
6099 tree cref, next = NULL_TREE;
6101 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6102 return;
6104 cref = TREE_OPERAND (ref, 0);
6105 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6106 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6107 next && TREE_CODE (next) != FIELD_DECL;
6108 next = DECL_CHAIN (next))
6111 /* If this is the last field in a struct type or a field in a
6112 union type do not warn. */
6113 if (!next)
6114 return;
6117 low_bound = array_ref_low_bound (ref);
6118 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6119 build_int_cst (TREE_TYPE (up_bound), 1));
6121 if (TREE_CODE (low_sub) == SSA_NAME)
6123 vr = get_value_range (low_sub);
6124 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6126 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6127 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6131 if (vr && vr->type == VR_ANTI_RANGE)
6133 if (TREE_CODE (up_sub) == INTEGER_CST
6134 && tree_int_cst_lt (up_bound, up_sub)
6135 && TREE_CODE (low_sub) == INTEGER_CST
6136 && tree_int_cst_lt (low_sub, low_bound))
6138 warning_at (location, OPT_Warray_bounds,
6139 "array subscript is outside array bounds");
6140 TREE_NO_WARNING (ref) = 1;
6143 else if (TREE_CODE (up_sub) == INTEGER_CST
6144 && (ignore_off_by_one
6145 ? (tree_int_cst_lt (up_bound, up_sub)
6146 && !tree_int_cst_equal (up_bound_p1, up_sub))
6147 : (tree_int_cst_lt (up_bound, up_sub)
6148 || tree_int_cst_equal (up_bound_p1, up_sub))))
6150 if (dump_file && (dump_flags & TDF_DETAILS))
6152 fprintf (dump_file, "Array bound warning for ");
6153 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6154 fprintf (dump_file, "\n");
6156 warning_at (location, OPT_Warray_bounds,
6157 "array subscript is above array bounds");
6158 TREE_NO_WARNING (ref) = 1;
6160 else if (TREE_CODE (low_sub) == INTEGER_CST
6161 && tree_int_cst_lt (low_sub, low_bound))
6163 if (dump_file && (dump_flags & TDF_DETAILS))
6165 fprintf (dump_file, "Array bound warning for ");
6166 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6167 fprintf (dump_file, "\n");
6169 warning_at (location, OPT_Warray_bounds,
6170 "array subscript is below array bounds");
6171 TREE_NO_WARNING (ref) = 1;
6175 /* Searches if the expr T, located at LOCATION computes
6176 address of an ARRAY_REF, and call check_array_ref on it. */
6178 static void
6179 search_for_addr_array (tree t, location_t location)
6181 while (TREE_CODE (t) == SSA_NAME)
6183 gimple g = SSA_NAME_DEF_STMT (t);
6185 if (gimple_code (g) != GIMPLE_ASSIGN)
6186 return;
6188 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6189 != GIMPLE_SINGLE_RHS)
6190 return;
6192 t = gimple_assign_rhs1 (g);
6196 /* We are only interested in addresses of ARRAY_REF's. */
6197 if (TREE_CODE (t) != ADDR_EXPR)
6198 return;
6200 /* Check each ARRAY_REFs in the reference chain. */
6203 if (TREE_CODE (t) == ARRAY_REF)
6204 check_array_ref (location, t, true /*ignore_off_by_one*/);
6206 t = TREE_OPERAND (t, 0);
6208 while (handled_component_p (t));
6210 if (TREE_CODE (t) == MEM_REF
6211 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6212 && !TREE_NO_WARNING (t))
6214 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6215 tree low_bound, up_bound, el_sz;
6216 offset_int idx;
6217 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6218 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6219 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6220 return;
6222 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6223 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6224 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6225 if (!low_bound
6226 || TREE_CODE (low_bound) != INTEGER_CST
6227 || !up_bound
6228 || TREE_CODE (up_bound) != INTEGER_CST
6229 || !el_sz
6230 || TREE_CODE (el_sz) != INTEGER_CST)
6231 return;
6233 idx = mem_ref_offset (t);
6234 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6235 if (wi::lts_p (idx, 0))
6237 if (dump_file && (dump_flags & TDF_DETAILS))
6239 fprintf (dump_file, "Array bound warning for ");
6240 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6241 fprintf (dump_file, "\n");
6243 warning_at (location, OPT_Warray_bounds,
6244 "array subscript is below array bounds");
6245 TREE_NO_WARNING (t) = 1;
6247 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6248 - wi::to_offset (low_bound) + 1)))
6250 if (dump_file && (dump_flags & TDF_DETAILS))
6252 fprintf (dump_file, "Array bound warning for ");
6253 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6254 fprintf (dump_file, "\n");
6256 warning_at (location, OPT_Warray_bounds,
6257 "array subscript is above array bounds");
6258 TREE_NO_WARNING (t) = 1;
6263 /* walk_tree() callback that checks if *TP is
6264 an ARRAY_REF inside an ADDR_EXPR (in which an array
6265 subscript one outside the valid range is allowed). Call
6266 check_array_ref for each ARRAY_REF found. The location is
6267 passed in DATA. */
6269 static tree
6270 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6272 tree t = *tp;
6273 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6274 location_t location;
6276 if (EXPR_HAS_LOCATION (t))
6277 location = EXPR_LOCATION (t);
6278 else
6280 location_t *locp = (location_t *) wi->info;
6281 location = *locp;
6284 *walk_subtree = TRUE;
6286 if (TREE_CODE (t) == ARRAY_REF)
6287 check_array_ref (location, t, false /*ignore_off_by_one*/);
6289 if (TREE_CODE (t) == MEM_REF
6290 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6291 search_for_addr_array (TREE_OPERAND (t, 0), location);
6293 if (TREE_CODE (t) == ADDR_EXPR)
6294 *walk_subtree = FALSE;
6296 return NULL_TREE;
6299 /* Walk over all statements of all reachable BBs and call check_array_bounds
6300 on them. */
6302 static void
6303 check_all_array_refs (void)
6305 basic_block bb;
6306 gimple_stmt_iterator si;
6308 FOR_EACH_BB_FN (bb, cfun)
6310 edge_iterator ei;
6311 edge e;
6312 bool executable = false;
6314 /* Skip blocks that were found to be unreachable. */
6315 FOR_EACH_EDGE (e, ei, bb->preds)
6316 executable |= !!(e->flags & EDGE_EXECUTABLE);
6317 if (!executable)
6318 continue;
6320 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6322 gimple stmt = gsi_stmt (si);
6323 struct walk_stmt_info wi;
6324 if (!gimple_has_location (stmt))
6325 continue;
6327 if (is_gimple_call (stmt))
6329 size_t i;
6330 size_t n = gimple_call_num_args (stmt);
6331 for (i = 0; i < n; i++)
6333 tree arg = gimple_call_arg (stmt, i);
6334 search_for_addr_array (arg, gimple_location (stmt));
6337 else
6339 memset (&wi, 0, sizeof (wi));
6340 wi.info = CONST_CAST (void *, (const void *)
6341 gimple_location_ptr (stmt));
6343 walk_gimple_op (gsi_stmt (si),
6344 check_array_bounds,
6345 &wi);
6351 /* Return true if all imm uses of VAR are either in STMT, or
6352 feed (optionally through a chain of single imm uses) GIMPLE_COND
6353 in basic block COND_BB. */
6355 static bool
6356 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6358 use_operand_p use_p, use2_p;
6359 imm_use_iterator iter;
6361 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6362 if (USE_STMT (use_p) != stmt)
6364 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6365 if (is_gimple_debug (use_stmt))
6366 continue;
6367 while (is_gimple_assign (use_stmt)
6368 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6369 && single_imm_use (gimple_assign_lhs (use_stmt),
6370 &use2_p, &use_stmt2))
6371 use_stmt = use_stmt2;
6372 if (gimple_code (use_stmt) != GIMPLE_COND
6373 || gimple_bb (use_stmt) != cond_bb)
6374 return false;
6376 return true;
6379 /* Handle
6380 _4 = x_3 & 31;
6381 if (_4 != 0)
6382 goto <bb 6>;
6383 else
6384 goto <bb 7>;
6385 <bb 6>:
6386 __builtin_unreachable ();
6387 <bb 7>:
6388 x_5 = ASSERT_EXPR <x_3, ...>;
6389 If x_3 has no other immediate uses (checked by caller),
6390 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6391 from the non-zero bitmask. */
6393 static void
6394 maybe_set_nonzero_bits (basic_block bb, tree var)
6396 edge e = single_pred_edge (bb);
6397 basic_block cond_bb = e->src;
6398 gimple stmt = last_stmt (cond_bb);
6399 tree cst;
6401 if (stmt == NULL
6402 || gimple_code (stmt) != GIMPLE_COND
6403 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6404 ? EQ_EXPR : NE_EXPR)
6405 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6406 || !integer_zerop (gimple_cond_rhs (stmt)))
6407 return;
6409 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6410 if (!is_gimple_assign (stmt)
6411 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6412 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6413 return;
6414 if (gimple_assign_rhs1 (stmt) != var)
6416 gimple stmt2;
6418 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6419 return;
6420 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6421 if (!gimple_assign_cast_p (stmt2)
6422 || gimple_assign_rhs1 (stmt2) != var
6423 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6424 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6425 != TYPE_PRECISION (TREE_TYPE (var))))
6426 return;
6428 cst = gimple_assign_rhs2 (stmt);
6429 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6432 /* Convert range assertion expressions into the implied copies and
6433 copy propagate away the copies. Doing the trivial copy propagation
6434 here avoids the need to run the full copy propagation pass after
6435 VRP.
6437 FIXME, this will eventually lead to copy propagation removing the
6438 names that had useful range information attached to them. For
6439 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6440 then N_i will have the range [3, +INF].
6442 However, by converting the assertion into the implied copy
6443 operation N_i = N_j, we will then copy-propagate N_j into the uses
6444 of N_i and lose the range information. We may want to hold on to
6445 ASSERT_EXPRs a little while longer as the ranges could be used in
6446 things like jump threading.
6448 The problem with keeping ASSERT_EXPRs around is that passes after
6449 VRP need to handle them appropriately.
6451 Another approach would be to make the range information a first
6452 class property of the SSA_NAME so that it can be queried from
6453 any pass. This is made somewhat more complex by the need for
6454 multiple ranges to be associated with one SSA_NAME. */
6456 static void
6457 remove_range_assertions (void)
6459 basic_block bb;
6460 gimple_stmt_iterator si;
6461 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6462 a basic block preceeded by GIMPLE_COND branching to it and
6463 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6464 int is_unreachable;
6466 /* Note that the BSI iterator bump happens at the bottom of the
6467 loop and no bump is necessary if we're removing the statement
6468 referenced by the current BSI. */
6469 FOR_EACH_BB_FN (bb, cfun)
6470 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6472 gimple stmt = gsi_stmt (si);
6473 gimple use_stmt;
6475 if (is_gimple_assign (stmt)
6476 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6478 tree lhs = gimple_assign_lhs (stmt);
6479 tree rhs = gimple_assign_rhs1 (stmt);
6480 tree var;
6481 tree cond = fold (ASSERT_EXPR_COND (rhs));
6482 use_operand_p use_p;
6483 imm_use_iterator iter;
6485 gcc_assert (cond != boolean_false_node);
6487 var = ASSERT_EXPR_VAR (rhs);
6488 gcc_assert (TREE_CODE (var) == SSA_NAME);
6490 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6491 && SSA_NAME_RANGE_INFO (lhs))
6493 if (is_unreachable == -1)
6495 is_unreachable = 0;
6496 if (single_pred_p (bb)
6497 && assert_unreachable_fallthru_edge_p
6498 (single_pred_edge (bb)))
6499 is_unreachable = 1;
6501 /* Handle
6502 if (x_7 >= 10 && x_7 < 20)
6503 __builtin_unreachable ();
6504 x_8 = ASSERT_EXPR <x_7, ...>;
6505 if the only uses of x_7 are in the ASSERT_EXPR and
6506 in the condition. In that case, we can copy the
6507 range info from x_8 computed in this pass also
6508 for x_7. */
6509 if (is_unreachable
6510 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6511 single_pred (bb)))
6513 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6514 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6515 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6516 maybe_set_nonzero_bits (bb, var);
6520 /* Propagate the RHS into every use of the LHS. */
6521 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6522 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6523 SET_USE (use_p, var);
6525 /* And finally, remove the copy, it is not needed. */
6526 gsi_remove (&si, true);
6527 release_defs (stmt);
6529 else
6531 gsi_next (&si);
6532 is_unreachable = 0;
6538 /* Return true if STMT is interesting for VRP. */
6540 static bool
6541 stmt_interesting_for_vrp (gimple stmt)
6543 if (gimple_code (stmt) == GIMPLE_PHI)
6545 tree res = gimple_phi_result (stmt);
6546 return (!virtual_operand_p (res)
6547 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6548 || POINTER_TYPE_P (TREE_TYPE (res))));
6550 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6552 tree lhs = gimple_get_lhs (stmt);
6554 /* In general, assignments with virtual operands are not useful
6555 for deriving ranges, with the obvious exception of calls to
6556 builtin functions. */
6557 if (lhs && TREE_CODE (lhs) == SSA_NAME
6558 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6559 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6560 && (is_gimple_call (stmt)
6561 || !gimple_vuse (stmt)))
6562 return true;
6564 else if (gimple_code (stmt) == GIMPLE_COND
6565 || gimple_code (stmt) == GIMPLE_SWITCH)
6566 return true;
6568 return false;
6572 /* Initialize local data structures for VRP. */
6574 static void
6575 vrp_initialize (void)
6577 basic_block bb;
6579 values_propagated = false;
6580 num_vr_values = num_ssa_names;
6581 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6582 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6584 FOR_EACH_BB_FN (bb, cfun)
6586 gimple_stmt_iterator si;
6588 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6590 gimple phi = gsi_stmt (si);
6591 if (!stmt_interesting_for_vrp (phi))
6593 tree lhs = PHI_RESULT (phi);
6594 set_value_range_to_varying (get_value_range (lhs));
6595 prop_set_simulate_again (phi, false);
6597 else
6598 prop_set_simulate_again (phi, true);
6601 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6603 gimple stmt = gsi_stmt (si);
6605 /* If the statement is a control insn, then we do not
6606 want to avoid simulating the statement once. Failure
6607 to do so means that those edges will never get added. */
6608 if (stmt_ends_bb_p (stmt))
6609 prop_set_simulate_again (stmt, true);
6610 else if (!stmt_interesting_for_vrp (stmt))
6612 ssa_op_iter i;
6613 tree def;
6614 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6615 set_value_range_to_varying (get_value_range (def));
6616 prop_set_simulate_again (stmt, false);
6618 else
6619 prop_set_simulate_again (stmt, true);
6624 /* Return the singleton value-range for NAME or NAME. */
6626 static inline tree
6627 vrp_valueize (tree name)
6629 if (TREE_CODE (name) == SSA_NAME)
6631 value_range_t *vr = get_value_range (name);
6632 if (vr->type == VR_RANGE
6633 && (vr->min == vr->max
6634 || operand_equal_p (vr->min, vr->max, 0)))
6635 return vr->min;
6637 return name;
6640 /* Visit assignment STMT. If it produces an interesting range, record
6641 the SSA name in *OUTPUT_P. */
6643 static enum ssa_prop_result
6644 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6646 tree def, lhs;
6647 ssa_op_iter iter;
6648 enum gimple_code code = gimple_code (stmt);
6649 lhs = gimple_get_lhs (stmt);
6651 /* We only keep track of ranges in integral and pointer types. */
6652 if (TREE_CODE (lhs) == SSA_NAME
6653 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6654 /* It is valid to have NULL MIN/MAX values on a type. See
6655 build_range_type. */
6656 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6657 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6658 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6660 value_range_t new_vr = VR_INITIALIZER;
6662 /* Try folding the statement to a constant first. */
6663 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6664 if (tem)
6665 set_value_range_to_value (&new_vr, tem, NULL);
6666 /* Then dispatch to value-range extracting functions. */
6667 else if (code == GIMPLE_CALL)
6668 extract_range_basic (&new_vr, stmt);
6669 else
6670 extract_range_from_assignment (&new_vr, stmt);
6672 if (update_value_range (lhs, &new_vr))
6674 *output_p = lhs;
6676 if (dump_file && (dump_flags & TDF_DETAILS))
6678 fprintf (dump_file, "Found new range for ");
6679 print_generic_expr (dump_file, lhs, 0);
6680 fprintf (dump_file, ": ");
6681 dump_value_range (dump_file, &new_vr);
6682 fprintf (dump_file, "\n\n");
6685 if (new_vr.type == VR_VARYING)
6686 return SSA_PROP_VARYING;
6688 return SSA_PROP_INTERESTING;
6691 return SSA_PROP_NOT_INTERESTING;
6694 /* Every other statement produces no useful ranges. */
6695 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6696 set_value_range_to_varying (get_value_range (def));
6698 return SSA_PROP_VARYING;
6701 /* Helper that gets the value range of the SSA_NAME with version I
6702 or a symbolic range containing the SSA_NAME only if the value range
6703 is varying or undefined. */
6705 static inline value_range_t
6706 get_vr_for_comparison (int i)
6708 value_range_t vr = *get_value_range (ssa_name (i));
6710 /* If name N_i does not have a valid range, use N_i as its own
6711 range. This allows us to compare against names that may
6712 have N_i in their ranges. */
6713 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6715 vr.type = VR_RANGE;
6716 vr.min = ssa_name (i);
6717 vr.max = ssa_name (i);
6720 return vr;
6723 /* Compare all the value ranges for names equivalent to VAR with VAL
6724 using comparison code COMP. Return the same value returned by
6725 compare_range_with_value, including the setting of
6726 *STRICT_OVERFLOW_P. */
6728 static tree
6729 compare_name_with_value (enum tree_code comp, tree var, tree val,
6730 bool *strict_overflow_p)
6732 bitmap_iterator bi;
6733 unsigned i;
6734 bitmap e;
6735 tree retval, t;
6736 int used_strict_overflow;
6737 bool sop;
6738 value_range_t equiv_vr;
6740 /* Get the set of equivalences for VAR. */
6741 e = get_value_range (var)->equiv;
6743 /* Start at -1. Set it to 0 if we do a comparison without relying
6744 on overflow, or 1 if all comparisons rely on overflow. */
6745 used_strict_overflow = -1;
6747 /* Compare vars' value range with val. */
6748 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6749 sop = false;
6750 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
6751 if (retval)
6752 used_strict_overflow = sop ? 1 : 0;
6754 /* If the equiv set is empty we have done all work we need to do. */
6755 if (e == NULL)
6757 if (retval
6758 && used_strict_overflow > 0)
6759 *strict_overflow_p = true;
6760 return retval;
6763 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
6765 equiv_vr = get_vr_for_comparison (i);
6766 sop = false;
6767 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
6768 if (t)
6770 /* If we get different answers from different members
6771 of the equivalence set this check must be in a dead
6772 code region. Folding it to a trap representation
6773 would be correct here. For now just return don't-know. */
6774 if (retval != NULL
6775 && t != retval)
6777 retval = NULL_TREE;
6778 break;
6780 retval = t;
6782 if (!sop)
6783 used_strict_overflow = 0;
6784 else if (used_strict_overflow < 0)
6785 used_strict_overflow = 1;
6789 if (retval
6790 && used_strict_overflow > 0)
6791 *strict_overflow_p = true;
6793 return retval;
6797 /* Given a comparison code COMP and names N1 and N2, compare all the
6798 ranges equivalent to N1 against all the ranges equivalent to N2
6799 to determine the value of N1 COMP N2. Return the same value
6800 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6801 whether we relied on an overflow infinity in the comparison. */
6804 static tree
6805 compare_names (enum tree_code comp, tree n1, tree n2,
6806 bool *strict_overflow_p)
6808 tree t, retval;
6809 bitmap e1, e2;
6810 bitmap_iterator bi1, bi2;
6811 unsigned i1, i2;
6812 int used_strict_overflow;
6813 static bitmap_obstack *s_obstack = NULL;
6814 static bitmap s_e1 = NULL, s_e2 = NULL;
6816 /* Compare the ranges of every name equivalent to N1 against the
6817 ranges of every name equivalent to N2. */
6818 e1 = get_value_range (n1)->equiv;
6819 e2 = get_value_range (n2)->equiv;
6821 /* Use the fake bitmaps if e1 or e2 are not available. */
6822 if (s_obstack == NULL)
6824 s_obstack = XNEW (bitmap_obstack);
6825 bitmap_obstack_initialize (s_obstack);
6826 s_e1 = BITMAP_ALLOC (s_obstack);
6827 s_e2 = BITMAP_ALLOC (s_obstack);
6829 if (e1 == NULL)
6830 e1 = s_e1;
6831 if (e2 == NULL)
6832 e2 = s_e2;
6834 /* Add N1 and N2 to their own set of equivalences to avoid
6835 duplicating the body of the loop just to check N1 and N2
6836 ranges. */
6837 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
6838 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
6840 /* If the equivalence sets have a common intersection, then the two
6841 names can be compared without checking their ranges. */
6842 if (bitmap_intersect_p (e1, e2))
6844 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6845 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6847 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
6848 ? boolean_true_node
6849 : boolean_false_node;
6852 /* Start at -1. Set it to 0 if we do a comparison without relying
6853 on overflow, or 1 if all comparisons rely on overflow. */
6854 used_strict_overflow = -1;
6856 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6857 N2 to their own set of equivalences to avoid duplicating the body
6858 of the loop just to check N1 and N2 ranges. */
6859 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
6861 value_range_t vr1 = get_vr_for_comparison (i1);
6863 t = retval = NULL_TREE;
6864 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
6866 bool sop = false;
6868 value_range_t vr2 = get_vr_for_comparison (i2);
6870 t = compare_ranges (comp, &vr1, &vr2, &sop);
6871 if (t)
6873 /* If we get different answers from different members
6874 of the equivalence set this check must be in a dead
6875 code region. Folding it to a trap representation
6876 would be correct here. For now just return don't-know. */
6877 if (retval != NULL
6878 && t != retval)
6880 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6881 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6882 return NULL_TREE;
6884 retval = t;
6886 if (!sop)
6887 used_strict_overflow = 0;
6888 else if (used_strict_overflow < 0)
6889 used_strict_overflow = 1;
6893 if (retval)
6895 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6896 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6897 if (used_strict_overflow > 0)
6898 *strict_overflow_p = true;
6899 return retval;
6903 /* None of the equivalent ranges are useful in computing this
6904 comparison. */
6905 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6906 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6907 return NULL_TREE;
6910 /* Helper function for vrp_evaluate_conditional_warnv. */
6912 static tree
6913 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
6914 tree op0, tree op1,
6915 bool * strict_overflow_p)
6917 value_range_t *vr0, *vr1;
6919 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
6920 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
6922 if (vr0 && vr1)
6923 return compare_ranges (code, vr0, vr1, strict_overflow_p);
6924 else if (vr0 && vr1 == NULL)
6925 return compare_range_with_value (code, vr0, op1, strict_overflow_p);
6926 else if (vr0 == NULL && vr1)
6927 return (compare_range_with_value
6928 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
6929 return NULL;
6932 /* Helper function for vrp_evaluate_conditional_warnv. */
6934 static tree
6935 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
6936 tree op1, bool use_equiv_p,
6937 bool *strict_overflow_p, bool *only_ranges)
6939 tree ret;
6940 if (only_ranges)
6941 *only_ranges = true;
6943 /* We only deal with integral and pointer types. */
6944 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
6945 && !POINTER_TYPE_P (TREE_TYPE (op0)))
6946 return NULL_TREE;
6948 if (use_equiv_p)
6950 if (only_ranges
6951 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
6952 (code, op0, op1, strict_overflow_p)))
6953 return ret;
6954 *only_ranges = false;
6955 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
6956 return compare_names (code, op0, op1, strict_overflow_p);
6957 else if (TREE_CODE (op0) == SSA_NAME)
6958 return compare_name_with_value (code, op0, op1, strict_overflow_p);
6959 else if (TREE_CODE (op1) == SSA_NAME)
6960 return (compare_name_with_value
6961 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
6963 else
6964 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
6965 strict_overflow_p);
6966 return NULL_TREE;
6969 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
6970 information. Return NULL if the conditional can not be evaluated.
6971 The ranges of all the names equivalent with the operands in COND
6972 will be used when trying to compute the value. If the result is
6973 based on undefined signed overflow, issue a warning if
6974 appropriate. */
6976 static tree
6977 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
6979 bool sop;
6980 tree ret;
6981 bool only_ranges;
6983 /* Some passes and foldings leak constants with overflow flag set
6984 into the IL. Avoid doing wrong things with these and bail out. */
6985 if ((TREE_CODE (op0) == INTEGER_CST
6986 && TREE_OVERFLOW (op0))
6987 || (TREE_CODE (op1) == INTEGER_CST
6988 && TREE_OVERFLOW (op1)))
6989 return NULL_TREE;
6991 sop = false;
6992 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
6993 &only_ranges);
6995 if (ret && sop)
6997 enum warn_strict_overflow_code wc;
6998 const char* warnmsg;
7000 if (is_gimple_min_invariant (ret))
7002 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7003 warnmsg = G_("assuming signed overflow does not occur when "
7004 "simplifying conditional to constant");
7006 else
7008 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7009 warnmsg = G_("assuming signed overflow does not occur when "
7010 "simplifying conditional");
7013 if (issue_strict_overflow_warning (wc))
7015 location_t location;
7017 if (!gimple_has_location (stmt))
7018 location = input_location;
7019 else
7020 location = gimple_location (stmt);
7021 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7025 if (warn_type_limits
7026 && ret && only_ranges
7027 && TREE_CODE_CLASS (code) == tcc_comparison
7028 && TREE_CODE (op0) == SSA_NAME)
7030 /* If the comparison is being folded and the operand on the LHS
7031 is being compared against a constant value that is outside of
7032 the natural range of OP0's type, then the predicate will
7033 always fold regardless of the value of OP0. If -Wtype-limits
7034 was specified, emit a warning. */
7035 tree type = TREE_TYPE (op0);
7036 value_range_t *vr0 = get_value_range (op0);
7038 if (vr0->type != VR_VARYING
7039 && INTEGRAL_TYPE_P (type)
7040 && vrp_val_is_min (vr0->min)
7041 && vrp_val_is_max (vr0->max)
7042 && is_gimple_min_invariant (op1))
7044 location_t location;
7046 if (!gimple_has_location (stmt))
7047 location = input_location;
7048 else
7049 location = gimple_location (stmt);
7051 warning_at (location, OPT_Wtype_limits,
7052 integer_zerop (ret)
7053 ? G_("comparison always false "
7054 "due to limited range of data type")
7055 : G_("comparison always true "
7056 "due to limited range of data type"));
7060 return ret;
7064 /* Visit conditional statement STMT. If we can determine which edge
7065 will be taken out of STMT's basic block, record it in
7066 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7067 SSA_PROP_VARYING. */
7069 static enum ssa_prop_result
7070 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
7072 tree val;
7073 bool sop;
7075 *taken_edge_p = NULL;
7077 if (dump_file && (dump_flags & TDF_DETAILS))
7079 tree use;
7080 ssa_op_iter i;
7082 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7083 print_gimple_stmt (dump_file, stmt, 0, 0);
7084 fprintf (dump_file, "\nWith known ranges\n");
7086 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7088 fprintf (dump_file, "\t");
7089 print_generic_expr (dump_file, use, 0);
7090 fprintf (dump_file, ": ");
7091 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7094 fprintf (dump_file, "\n");
7097 /* Compute the value of the predicate COND by checking the known
7098 ranges of each of its operands.
7100 Note that we cannot evaluate all the equivalent ranges here
7101 because those ranges may not yet be final and with the current
7102 propagation strategy, we cannot determine when the value ranges
7103 of the names in the equivalence set have changed.
7105 For instance, given the following code fragment
7107 i_5 = PHI <8, i_13>
7109 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7110 if (i_14 == 1)
7113 Assume that on the first visit to i_14, i_5 has the temporary
7114 range [8, 8] because the second argument to the PHI function is
7115 not yet executable. We derive the range ~[0, 0] for i_14 and the
7116 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7117 the first time, since i_14 is equivalent to the range [8, 8], we
7118 determine that the predicate is always false.
7120 On the next round of propagation, i_13 is determined to be
7121 VARYING, which causes i_5 to drop down to VARYING. So, another
7122 visit to i_14 is scheduled. In this second visit, we compute the
7123 exact same range and equivalence set for i_14, namely ~[0, 0] and
7124 { i_5 }. But we did not have the previous range for i_5
7125 registered, so vrp_visit_assignment thinks that the range for
7126 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7127 is not visited again, which stops propagation from visiting
7128 statements in the THEN clause of that if().
7130 To properly fix this we would need to keep the previous range
7131 value for the names in the equivalence set. This way we would've
7132 discovered that from one visit to the other i_5 changed from
7133 range [8, 8] to VR_VARYING.
7135 However, fixing this apparent limitation may not be worth the
7136 additional checking. Testing on several code bases (GCC, DLV,
7137 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7138 4 more predicates folded in SPEC. */
7139 sop = false;
7141 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7142 gimple_cond_lhs (stmt),
7143 gimple_cond_rhs (stmt),
7144 false, &sop, NULL);
7145 if (val)
7147 if (!sop)
7148 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7149 else
7151 if (dump_file && (dump_flags & TDF_DETAILS))
7152 fprintf (dump_file,
7153 "\nIgnoring predicate evaluation because "
7154 "it assumes that signed overflow is undefined");
7155 val = NULL_TREE;
7159 if (dump_file && (dump_flags & TDF_DETAILS))
7161 fprintf (dump_file, "\nPredicate evaluates to: ");
7162 if (val == NULL_TREE)
7163 fprintf (dump_file, "DON'T KNOW\n");
7164 else
7165 print_generic_stmt (dump_file, val, 0);
7168 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7171 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7172 that includes the value VAL. The search is restricted to the range
7173 [START_IDX, n - 1] where n is the size of VEC.
7175 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7176 returned.
7178 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7179 it is placed in IDX and false is returned.
7181 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7182 returned. */
7184 static bool
7185 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
7187 size_t n = gimple_switch_num_labels (stmt);
7188 size_t low, high;
7190 /* Find case label for minimum of the value range or the next one.
7191 At each iteration we are searching in [low, high - 1]. */
7193 for (low = start_idx, high = n; high != low; )
7195 tree t;
7196 int cmp;
7197 /* Note that i != high, so we never ask for n. */
7198 size_t i = (high + low) / 2;
7199 t = gimple_switch_label (stmt, i);
7201 /* Cache the result of comparing CASE_LOW and val. */
7202 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7204 if (cmp == 0)
7206 /* Ranges cannot be empty. */
7207 *idx = i;
7208 return true;
7210 else if (cmp > 0)
7211 high = i;
7212 else
7214 low = i + 1;
7215 if (CASE_HIGH (t) != NULL
7216 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7218 *idx = i;
7219 return true;
7224 *idx = high;
7225 return false;
7228 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7229 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7230 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7231 then MAX_IDX < MIN_IDX.
7232 Returns true if the default label is not needed. */
7234 static bool
7235 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
7236 size_t *max_idx)
7238 size_t i, j;
7239 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7240 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7242 if (i == j
7243 && min_take_default
7244 && max_take_default)
7246 /* Only the default case label reached.
7247 Return an empty range. */
7248 *min_idx = 1;
7249 *max_idx = 0;
7250 return false;
7252 else
7254 bool take_default = min_take_default || max_take_default;
7255 tree low, high;
7256 size_t k;
7258 if (max_take_default)
7259 j--;
7261 /* If the case label range is continuous, we do not need
7262 the default case label. Verify that. */
7263 high = CASE_LOW (gimple_switch_label (stmt, i));
7264 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7265 high = CASE_HIGH (gimple_switch_label (stmt, i));
7266 for (k = i + 1; k <= j; ++k)
7268 low = CASE_LOW (gimple_switch_label (stmt, k));
7269 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7271 take_default = true;
7272 break;
7274 high = low;
7275 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7276 high = CASE_HIGH (gimple_switch_label (stmt, k));
7279 *min_idx = i;
7280 *max_idx = j;
7281 return !take_default;
7285 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7286 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7287 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7288 Returns true if the default label is not needed. */
7290 static bool
7291 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
7292 size_t *max_idx1, size_t *min_idx2,
7293 size_t *max_idx2)
7295 size_t i, j, k, l;
7296 unsigned int n = gimple_switch_num_labels (stmt);
7297 bool take_default;
7298 tree case_low, case_high;
7299 tree min = vr->min, max = vr->max;
7301 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7303 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7305 /* Set second range to emtpy. */
7306 *min_idx2 = 1;
7307 *max_idx2 = 0;
7309 if (vr->type == VR_RANGE)
7311 *min_idx1 = i;
7312 *max_idx1 = j;
7313 return !take_default;
7316 /* Set first range to all case labels. */
7317 *min_idx1 = 1;
7318 *max_idx1 = n - 1;
7320 if (i > j)
7321 return false;
7323 /* Make sure all the values of case labels [i , j] are contained in
7324 range [MIN, MAX]. */
7325 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7326 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7327 if (tree_int_cst_compare (case_low, min) < 0)
7328 i += 1;
7329 if (case_high != NULL_TREE
7330 && tree_int_cst_compare (max, case_high) < 0)
7331 j -= 1;
7333 if (i > j)
7334 return false;
7336 /* If the range spans case labels [i, j], the corresponding anti-range spans
7337 the labels [1, i - 1] and [j + 1, n - 1]. */
7338 k = j + 1;
7339 l = n - 1;
7340 if (k > l)
7342 k = 1;
7343 l = 0;
7346 j = i - 1;
7347 i = 1;
7348 if (i > j)
7350 i = k;
7351 j = l;
7352 k = 1;
7353 l = 0;
7356 *min_idx1 = i;
7357 *max_idx1 = j;
7358 *min_idx2 = k;
7359 *max_idx2 = l;
7360 return false;
7363 /* Visit switch statement STMT. If we can determine which edge
7364 will be taken out of STMT's basic block, record it in
7365 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7366 SSA_PROP_VARYING. */
7368 static enum ssa_prop_result
7369 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7371 tree op, val;
7372 value_range_t *vr;
7373 size_t i = 0, j = 0, k, l;
7374 bool take_default;
7376 *taken_edge_p = NULL;
7377 op = gimple_switch_index (stmt);
7378 if (TREE_CODE (op) != SSA_NAME)
7379 return SSA_PROP_VARYING;
7381 vr = get_value_range (op);
7382 if (dump_file && (dump_flags & TDF_DETAILS))
7384 fprintf (dump_file, "\nVisiting switch expression with operand ");
7385 print_generic_expr (dump_file, op, 0);
7386 fprintf (dump_file, " with known range ");
7387 dump_value_range (dump_file, vr);
7388 fprintf (dump_file, "\n");
7391 if ((vr->type != VR_RANGE
7392 && vr->type != VR_ANTI_RANGE)
7393 || symbolic_range_p (vr))
7394 return SSA_PROP_VARYING;
7396 /* Find the single edge that is taken from the switch expression. */
7397 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7399 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7400 label */
7401 if (j < i)
7403 gcc_assert (take_default);
7404 val = gimple_switch_default_label (stmt);
7406 else
7408 /* Check if labels with index i to j and maybe the default label
7409 are all reaching the same label. */
7411 val = gimple_switch_label (stmt, i);
7412 if (take_default
7413 && CASE_LABEL (gimple_switch_default_label (stmt))
7414 != CASE_LABEL (val))
7416 if (dump_file && (dump_flags & TDF_DETAILS))
7417 fprintf (dump_file, " not a single destination for this "
7418 "range\n");
7419 return SSA_PROP_VARYING;
7421 for (++i; i <= j; ++i)
7423 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7425 if (dump_file && (dump_flags & TDF_DETAILS))
7426 fprintf (dump_file, " not a single destination for this "
7427 "range\n");
7428 return SSA_PROP_VARYING;
7431 for (; k <= l; ++k)
7433 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7435 if (dump_file && (dump_flags & TDF_DETAILS))
7436 fprintf (dump_file, " not a single destination for this "
7437 "range\n");
7438 return SSA_PROP_VARYING;
7443 *taken_edge_p = find_edge (gimple_bb (stmt),
7444 label_to_block (CASE_LABEL (val)));
7446 if (dump_file && (dump_flags & TDF_DETAILS))
7448 fprintf (dump_file, " will take edge to ");
7449 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7452 return SSA_PROP_INTERESTING;
7456 /* Evaluate statement STMT. If the statement produces a useful range,
7457 return SSA_PROP_INTERESTING and record the SSA name with the
7458 interesting range into *OUTPUT_P.
7460 If STMT is a conditional branch and we can determine its truth
7461 value, the taken edge is recorded in *TAKEN_EDGE_P.
7463 If STMT produces a varying value, return SSA_PROP_VARYING. */
7465 static enum ssa_prop_result
7466 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7468 tree def;
7469 ssa_op_iter iter;
7471 if (dump_file && (dump_flags & TDF_DETAILS))
7473 fprintf (dump_file, "\nVisiting statement:\n");
7474 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7475 fprintf (dump_file, "\n");
7478 if (!stmt_interesting_for_vrp (stmt))
7479 gcc_assert (stmt_ends_bb_p (stmt));
7480 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7481 return vrp_visit_assignment_or_call (stmt, output_p);
7482 else if (gimple_code (stmt) == GIMPLE_COND)
7483 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7484 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7485 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7487 /* All other statements produce nothing of interest for VRP, so mark
7488 their outputs varying and prevent further simulation. */
7489 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7490 set_value_range_to_varying (get_value_range (def));
7492 return SSA_PROP_VARYING;
7495 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7496 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7497 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7498 possible such range. The resulting range is not canonicalized. */
7500 static void
7501 union_ranges (enum value_range_type *vr0type,
7502 tree *vr0min, tree *vr0max,
7503 enum value_range_type vr1type,
7504 tree vr1min, tree vr1max)
7506 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7507 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7509 /* [] is vr0, () is vr1 in the following classification comments. */
7510 if (mineq && maxeq)
7512 /* [( )] */
7513 if (*vr0type == vr1type)
7514 /* Nothing to do for equal ranges. */
7516 else if ((*vr0type == VR_RANGE
7517 && vr1type == VR_ANTI_RANGE)
7518 || (*vr0type == VR_ANTI_RANGE
7519 && vr1type == VR_RANGE))
7521 /* For anti-range with range union the result is varying. */
7522 goto give_up;
7524 else
7525 gcc_unreachable ();
7527 else if (operand_less_p (*vr0max, vr1min) == 1
7528 || operand_less_p (vr1max, *vr0min) == 1)
7530 /* [ ] ( ) or ( ) [ ]
7531 If the ranges have an empty intersection, result of the union
7532 operation is the anti-range or if both are anti-ranges
7533 it covers all. */
7534 if (*vr0type == VR_ANTI_RANGE
7535 && vr1type == VR_ANTI_RANGE)
7536 goto give_up;
7537 else if (*vr0type == VR_ANTI_RANGE
7538 && vr1type == VR_RANGE)
7540 else if (*vr0type == VR_RANGE
7541 && vr1type == VR_ANTI_RANGE)
7543 *vr0type = vr1type;
7544 *vr0min = vr1min;
7545 *vr0max = vr1max;
7547 else if (*vr0type == VR_RANGE
7548 && vr1type == VR_RANGE)
7550 /* The result is the convex hull of both ranges. */
7551 if (operand_less_p (*vr0max, vr1min) == 1)
7553 /* If the result can be an anti-range, create one. */
7554 if (TREE_CODE (*vr0max) == INTEGER_CST
7555 && TREE_CODE (vr1min) == INTEGER_CST
7556 && vrp_val_is_min (*vr0min)
7557 && vrp_val_is_max (vr1max))
7559 tree min = int_const_binop (PLUS_EXPR,
7560 *vr0max,
7561 build_int_cst (TREE_TYPE (*vr0max), 1));
7562 tree max = int_const_binop (MINUS_EXPR,
7563 vr1min,
7564 build_int_cst (TREE_TYPE (vr1min), 1));
7565 if (!operand_less_p (max, min))
7567 *vr0type = VR_ANTI_RANGE;
7568 *vr0min = min;
7569 *vr0max = max;
7571 else
7572 *vr0max = vr1max;
7574 else
7575 *vr0max = vr1max;
7577 else
7579 /* If the result can be an anti-range, create one. */
7580 if (TREE_CODE (vr1max) == INTEGER_CST
7581 && TREE_CODE (*vr0min) == INTEGER_CST
7582 && vrp_val_is_min (vr1min)
7583 && vrp_val_is_max (*vr0max))
7585 tree min = int_const_binop (PLUS_EXPR,
7586 vr1max,
7587 build_int_cst (TREE_TYPE (vr1max), 1));
7588 tree max = int_const_binop (MINUS_EXPR,
7589 *vr0min,
7590 build_int_cst (TREE_TYPE (*vr0min), 1));
7591 if (!operand_less_p (max, min))
7593 *vr0type = VR_ANTI_RANGE;
7594 *vr0min = min;
7595 *vr0max = max;
7597 else
7598 *vr0min = vr1min;
7600 else
7601 *vr0min = vr1min;
7604 else
7605 gcc_unreachable ();
7607 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7608 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7610 /* [ ( ) ] or [( ) ] or [ ( )] */
7611 if (*vr0type == VR_RANGE
7612 && vr1type == VR_RANGE)
7614 else if (*vr0type == VR_ANTI_RANGE
7615 && vr1type == VR_ANTI_RANGE)
7617 *vr0type = vr1type;
7618 *vr0min = vr1min;
7619 *vr0max = vr1max;
7621 else if (*vr0type == VR_ANTI_RANGE
7622 && vr1type == VR_RANGE)
7624 /* Arbitrarily choose the right or left gap. */
7625 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7626 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7627 build_int_cst (TREE_TYPE (vr1min), 1));
7628 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7629 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7630 build_int_cst (TREE_TYPE (vr1max), 1));
7631 else
7632 goto give_up;
7634 else if (*vr0type == VR_RANGE
7635 && vr1type == VR_ANTI_RANGE)
7636 /* The result covers everything. */
7637 goto give_up;
7638 else
7639 gcc_unreachable ();
7641 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7642 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7644 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7645 if (*vr0type == VR_RANGE
7646 && vr1type == VR_RANGE)
7648 *vr0type = vr1type;
7649 *vr0min = vr1min;
7650 *vr0max = vr1max;
7652 else if (*vr0type == VR_ANTI_RANGE
7653 && vr1type == VR_ANTI_RANGE)
7655 else if (*vr0type == VR_RANGE
7656 && vr1type == VR_ANTI_RANGE)
7658 *vr0type = VR_ANTI_RANGE;
7659 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7661 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7662 build_int_cst (TREE_TYPE (*vr0min), 1));
7663 *vr0min = vr1min;
7665 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7667 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7668 build_int_cst (TREE_TYPE (*vr0max), 1));
7669 *vr0max = vr1max;
7671 else
7672 goto give_up;
7674 else if (*vr0type == VR_ANTI_RANGE
7675 && vr1type == VR_RANGE)
7676 /* The result covers everything. */
7677 goto give_up;
7678 else
7679 gcc_unreachable ();
7681 else if ((operand_less_p (vr1min, *vr0max) == 1
7682 || operand_equal_p (vr1min, *vr0max, 0))
7683 && operand_less_p (*vr0min, vr1min) == 1
7684 && operand_less_p (*vr0max, vr1max) == 1)
7686 /* [ ( ] ) or [ ]( ) */
7687 if (*vr0type == VR_RANGE
7688 && vr1type == VR_RANGE)
7689 *vr0max = vr1max;
7690 else if (*vr0type == VR_ANTI_RANGE
7691 && vr1type == VR_ANTI_RANGE)
7692 *vr0min = vr1min;
7693 else if (*vr0type == VR_ANTI_RANGE
7694 && vr1type == VR_RANGE)
7696 if (TREE_CODE (vr1min) == INTEGER_CST)
7697 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7698 build_int_cst (TREE_TYPE (vr1min), 1));
7699 else
7700 goto give_up;
7702 else if (*vr0type == VR_RANGE
7703 && vr1type == VR_ANTI_RANGE)
7705 if (TREE_CODE (*vr0max) == INTEGER_CST)
7707 *vr0type = vr1type;
7708 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7709 build_int_cst (TREE_TYPE (*vr0max), 1));
7710 *vr0max = vr1max;
7712 else
7713 goto give_up;
7715 else
7716 gcc_unreachable ();
7718 else if ((operand_less_p (*vr0min, vr1max) == 1
7719 || operand_equal_p (*vr0min, vr1max, 0))
7720 && operand_less_p (vr1min, *vr0min) == 1
7721 && operand_less_p (vr1max, *vr0max) == 1)
7723 /* ( [ ) ] or ( )[ ] */
7724 if (*vr0type == VR_RANGE
7725 && vr1type == VR_RANGE)
7726 *vr0min = vr1min;
7727 else if (*vr0type == VR_ANTI_RANGE
7728 && vr1type == VR_ANTI_RANGE)
7729 *vr0max = vr1max;
7730 else if (*vr0type == VR_ANTI_RANGE
7731 && vr1type == VR_RANGE)
7733 if (TREE_CODE (vr1max) == INTEGER_CST)
7734 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7735 build_int_cst (TREE_TYPE (vr1max), 1));
7736 else
7737 goto give_up;
7739 else if (*vr0type == VR_RANGE
7740 && vr1type == VR_ANTI_RANGE)
7742 if (TREE_CODE (*vr0min) == INTEGER_CST)
7744 *vr0type = vr1type;
7745 *vr0min = vr1min;
7746 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7747 build_int_cst (TREE_TYPE (*vr0min), 1));
7749 else
7750 goto give_up;
7752 else
7753 gcc_unreachable ();
7755 else
7756 goto give_up;
7758 return;
7760 give_up:
7761 *vr0type = VR_VARYING;
7762 *vr0min = NULL_TREE;
7763 *vr0max = NULL_TREE;
7766 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7767 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7768 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7769 possible such range. The resulting range is not canonicalized. */
7771 static void
7772 intersect_ranges (enum value_range_type *vr0type,
7773 tree *vr0min, tree *vr0max,
7774 enum value_range_type vr1type,
7775 tree vr1min, tree vr1max)
7777 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7778 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7780 /* [] is vr0, () is vr1 in the following classification comments. */
7781 if (mineq && maxeq)
7783 /* [( )] */
7784 if (*vr0type == vr1type)
7785 /* Nothing to do for equal ranges. */
7787 else if ((*vr0type == VR_RANGE
7788 && vr1type == VR_ANTI_RANGE)
7789 || (*vr0type == VR_ANTI_RANGE
7790 && vr1type == VR_RANGE))
7792 /* For anti-range with range intersection the result is empty. */
7793 *vr0type = VR_UNDEFINED;
7794 *vr0min = NULL_TREE;
7795 *vr0max = NULL_TREE;
7797 else
7798 gcc_unreachable ();
7800 else if (operand_less_p (*vr0max, vr1min) == 1
7801 || operand_less_p (vr1max, *vr0min) == 1)
7803 /* [ ] ( ) or ( ) [ ]
7804 If the ranges have an empty intersection, the result of the
7805 intersect operation is the range for intersecting an
7806 anti-range with a range or empty when intersecting two ranges. */
7807 if (*vr0type == VR_RANGE
7808 && vr1type == VR_ANTI_RANGE)
7810 else if (*vr0type == VR_ANTI_RANGE
7811 && vr1type == VR_RANGE)
7813 *vr0type = vr1type;
7814 *vr0min = vr1min;
7815 *vr0max = vr1max;
7817 else if (*vr0type == VR_RANGE
7818 && vr1type == VR_RANGE)
7820 *vr0type = VR_UNDEFINED;
7821 *vr0min = NULL_TREE;
7822 *vr0max = NULL_TREE;
7824 else if (*vr0type == VR_ANTI_RANGE
7825 && vr1type == VR_ANTI_RANGE)
7827 /* If the anti-ranges are adjacent to each other merge them. */
7828 if (TREE_CODE (*vr0max) == INTEGER_CST
7829 && TREE_CODE (vr1min) == INTEGER_CST
7830 && operand_less_p (*vr0max, vr1min) == 1
7831 && integer_onep (int_const_binop (MINUS_EXPR,
7832 vr1min, *vr0max)))
7833 *vr0max = vr1max;
7834 else if (TREE_CODE (vr1max) == INTEGER_CST
7835 && TREE_CODE (*vr0min) == INTEGER_CST
7836 && operand_less_p (vr1max, *vr0min) == 1
7837 && integer_onep (int_const_binop (MINUS_EXPR,
7838 *vr0min, vr1max)))
7839 *vr0min = vr1min;
7840 /* Else arbitrarily take VR0. */
7843 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7844 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7846 /* [ ( ) ] or [( ) ] or [ ( )] */
7847 if (*vr0type == VR_RANGE
7848 && vr1type == VR_RANGE)
7850 /* If both are ranges the result is the inner one. */
7851 *vr0type = vr1type;
7852 *vr0min = vr1min;
7853 *vr0max = vr1max;
7855 else if (*vr0type == VR_RANGE
7856 && vr1type == VR_ANTI_RANGE)
7858 /* Choose the right gap if the left one is empty. */
7859 if (mineq)
7861 if (TREE_CODE (vr1max) == INTEGER_CST)
7862 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7863 build_int_cst (TREE_TYPE (vr1max), 1));
7864 else
7865 *vr0min = vr1max;
7867 /* Choose the left gap if the right one is empty. */
7868 else if (maxeq)
7870 if (TREE_CODE (vr1min) == INTEGER_CST)
7871 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7872 build_int_cst (TREE_TYPE (vr1min), 1));
7873 else
7874 *vr0max = vr1min;
7876 /* Choose the anti-range if the range is effectively varying. */
7877 else if (vrp_val_is_min (*vr0min)
7878 && vrp_val_is_max (*vr0max))
7880 *vr0type = vr1type;
7881 *vr0min = vr1min;
7882 *vr0max = vr1max;
7884 /* Else choose the range. */
7886 else if (*vr0type == VR_ANTI_RANGE
7887 && vr1type == VR_ANTI_RANGE)
7888 /* If both are anti-ranges the result is the outer one. */
7890 else if (*vr0type == VR_ANTI_RANGE
7891 && vr1type == VR_RANGE)
7893 /* The intersection is empty. */
7894 *vr0type = VR_UNDEFINED;
7895 *vr0min = NULL_TREE;
7896 *vr0max = NULL_TREE;
7898 else
7899 gcc_unreachable ();
7901 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7902 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7904 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7905 if (*vr0type == VR_RANGE
7906 && vr1type == VR_RANGE)
7907 /* Choose the inner range. */
7909 else if (*vr0type == VR_ANTI_RANGE
7910 && vr1type == VR_RANGE)
7912 /* Choose the right gap if the left is empty. */
7913 if (mineq)
7915 *vr0type = VR_RANGE;
7916 if (TREE_CODE (*vr0max) == INTEGER_CST)
7917 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7918 build_int_cst (TREE_TYPE (*vr0max), 1));
7919 else
7920 *vr0min = *vr0max;
7921 *vr0max = vr1max;
7923 /* Choose the left gap if the right is empty. */
7924 else if (maxeq)
7926 *vr0type = VR_RANGE;
7927 if (TREE_CODE (*vr0min) == INTEGER_CST)
7928 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7929 build_int_cst (TREE_TYPE (*vr0min), 1));
7930 else
7931 *vr0max = *vr0min;
7932 *vr0min = vr1min;
7934 /* Choose the anti-range if the range is effectively varying. */
7935 else if (vrp_val_is_min (vr1min)
7936 && vrp_val_is_max (vr1max))
7938 /* Else choose the range. */
7939 else
7941 *vr0type = vr1type;
7942 *vr0min = vr1min;
7943 *vr0max = vr1max;
7946 else if (*vr0type == VR_ANTI_RANGE
7947 && vr1type == VR_ANTI_RANGE)
7949 /* If both are anti-ranges the result is the outer one. */
7950 *vr0type = vr1type;
7951 *vr0min = vr1min;
7952 *vr0max = vr1max;
7954 else if (vr1type == VR_ANTI_RANGE
7955 && *vr0type == VR_RANGE)
7957 /* The intersection is empty. */
7958 *vr0type = VR_UNDEFINED;
7959 *vr0min = NULL_TREE;
7960 *vr0max = NULL_TREE;
7962 else
7963 gcc_unreachable ();
7965 else if ((operand_less_p (vr1min, *vr0max) == 1
7966 || operand_equal_p (vr1min, *vr0max, 0))
7967 && operand_less_p (*vr0min, vr1min) == 1)
7969 /* [ ( ] ) or [ ]( ) */
7970 if (*vr0type == VR_ANTI_RANGE
7971 && vr1type == VR_ANTI_RANGE)
7972 *vr0max = vr1max;
7973 else if (*vr0type == VR_RANGE
7974 && vr1type == VR_RANGE)
7975 *vr0min = vr1min;
7976 else if (*vr0type == VR_RANGE
7977 && vr1type == VR_ANTI_RANGE)
7979 if (TREE_CODE (vr1min) == INTEGER_CST)
7980 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7981 build_int_cst (TREE_TYPE (vr1min), 1));
7982 else
7983 *vr0max = vr1min;
7985 else if (*vr0type == VR_ANTI_RANGE
7986 && vr1type == VR_RANGE)
7988 *vr0type = VR_RANGE;
7989 if (TREE_CODE (*vr0max) == INTEGER_CST)
7990 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7991 build_int_cst (TREE_TYPE (*vr0max), 1));
7992 else
7993 *vr0min = *vr0max;
7994 *vr0max = vr1max;
7996 else
7997 gcc_unreachable ();
7999 else if ((operand_less_p (*vr0min, vr1max) == 1
8000 || operand_equal_p (*vr0min, vr1max, 0))
8001 && operand_less_p (vr1min, *vr0min) == 1)
8003 /* ( [ ) ] or ( )[ ] */
8004 if (*vr0type == VR_ANTI_RANGE
8005 && vr1type == VR_ANTI_RANGE)
8006 *vr0min = vr1min;
8007 else if (*vr0type == VR_RANGE
8008 && vr1type == VR_RANGE)
8009 *vr0max = vr1max;
8010 else if (*vr0type == VR_RANGE
8011 && vr1type == VR_ANTI_RANGE)
8013 if (TREE_CODE (vr1max) == INTEGER_CST)
8014 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8015 build_int_cst (TREE_TYPE (vr1max), 1));
8016 else
8017 *vr0min = vr1max;
8019 else if (*vr0type == VR_ANTI_RANGE
8020 && vr1type == VR_RANGE)
8022 *vr0type = VR_RANGE;
8023 if (TREE_CODE (*vr0min) == INTEGER_CST)
8024 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8025 build_int_cst (TREE_TYPE (*vr0min), 1));
8026 else
8027 *vr0max = *vr0min;
8028 *vr0min = vr1min;
8030 else
8031 gcc_unreachable ();
8034 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8035 result for the intersection. That's always a conservative
8036 correct estimate. */
8038 return;
8042 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8043 in *VR0. This may not be the smallest possible such range. */
8045 static void
8046 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8048 value_range_t saved;
8050 /* If either range is VR_VARYING the other one wins. */
8051 if (vr1->type == VR_VARYING)
8052 return;
8053 if (vr0->type == VR_VARYING)
8055 copy_value_range (vr0, vr1);
8056 return;
8059 /* When either range is VR_UNDEFINED the resulting range is
8060 VR_UNDEFINED, too. */
8061 if (vr0->type == VR_UNDEFINED)
8062 return;
8063 if (vr1->type == VR_UNDEFINED)
8065 set_value_range_to_undefined (vr0);
8066 return;
8069 /* Save the original vr0 so we can return it as conservative intersection
8070 result when our worker turns things to varying. */
8071 saved = *vr0;
8072 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8073 vr1->type, vr1->min, vr1->max);
8074 /* Make sure to canonicalize the result though as the inversion of a
8075 VR_RANGE can still be a VR_RANGE. */
8076 set_and_canonicalize_value_range (vr0, vr0->type,
8077 vr0->min, vr0->max, vr0->equiv);
8078 /* If that failed, use the saved original VR0. */
8079 if (vr0->type == VR_VARYING)
8081 *vr0 = saved;
8082 return;
8084 /* If the result is VR_UNDEFINED there is no need to mess with
8085 the equivalencies. */
8086 if (vr0->type == VR_UNDEFINED)
8087 return;
8089 /* The resulting set of equivalences for range intersection is the union of
8090 the two sets. */
8091 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8092 bitmap_ior_into (vr0->equiv, vr1->equiv);
8093 else if (vr1->equiv && !vr0->equiv)
8094 bitmap_copy (vr0->equiv, vr1->equiv);
8097 static void
8098 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8100 if (dump_file && (dump_flags & TDF_DETAILS))
8102 fprintf (dump_file, "Intersecting\n ");
8103 dump_value_range (dump_file, vr0);
8104 fprintf (dump_file, "\nand\n ");
8105 dump_value_range (dump_file, vr1);
8106 fprintf (dump_file, "\n");
8108 vrp_intersect_ranges_1 (vr0, vr1);
8109 if (dump_file && (dump_flags & TDF_DETAILS))
8111 fprintf (dump_file, "to\n ");
8112 dump_value_range (dump_file, vr0);
8113 fprintf (dump_file, "\n");
8117 /* Meet operation for value ranges. Given two value ranges VR0 and
8118 VR1, store in VR0 a range that contains both VR0 and VR1. This
8119 may not be the smallest possible such range. */
8121 static void
8122 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8124 value_range_t saved;
8126 if (vr0->type == VR_UNDEFINED)
8128 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8129 return;
8132 if (vr1->type == VR_UNDEFINED)
8134 /* VR0 already has the resulting range. */
8135 return;
8138 if (vr0->type == VR_VARYING)
8140 /* Nothing to do. VR0 already has the resulting range. */
8141 return;
8144 if (vr1->type == VR_VARYING)
8146 set_value_range_to_varying (vr0);
8147 return;
8150 saved = *vr0;
8151 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8152 vr1->type, vr1->min, vr1->max);
8153 if (vr0->type == VR_VARYING)
8155 /* Failed to find an efficient meet. Before giving up and setting
8156 the result to VARYING, see if we can at least derive a useful
8157 anti-range. FIXME, all this nonsense about distinguishing
8158 anti-ranges from ranges is necessary because of the odd
8159 semantics of range_includes_zero_p and friends. */
8160 if (((saved.type == VR_RANGE
8161 && range_includes_zero_p (saved.min, saved.max) == 0)
8162 || (saved.type == VR_ANTI_RANGE
8163 && range_includes_zero_p (saved.min, saved.max) == 1))
8164 && ((vr1->type == VR_RANGE
8165 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8166 || (vr1->type == VR_ANTI_RANGE
8167 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8169 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8171 /* Since this meet operation did not result from the meeting of
8172 two equivalent names, VR0 cannot have any equivalences. */
8173 if (vr0->equiv)
8174 bitmap_clear (vr0->equiv);
8175 return;
8178 set_value_range_to_varying (vr0);
8179 return;
8181 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8182 vr0->equiv);
8183 if (vr0->type == VR_VARYING)
8184 return;
8186 /* The resulting set of equivalences is always the intersection of
8187 the two sets. */
8188 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8189 bitmap_and_into (vr0->equiv, vr1->equiv);
8190 else if (vr0->equiv && !vr1->equiv)
8191 bitmap_clear (vr0->equiv);
8194 static void
8195 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8197 if (dump_file && (dump_flags & TDF_DETAILS))
8199 fprintf (dump_file, "Meeting\n ");
8200 dump_value_range (dump_file, vr0);
8201 fprintf (dump_file, "\nand\n ");
8202 dump_value_range (dump_file, vr1);
8203 fprintf (dump_file, "\n");
8205 vrp_meet_1 (vr0, vr1);
8206 if (dump_file && (dump_flags & TDF_DETAILS))
8208 fprintf (dump_file, "to\n ");
8209 dump_value_range (dump_file, vr0);
8210 fprintf (dump_file, "\n");
8215 /* Visit all arguments for PHI node PHI that flow through executable
8216 edges. If a valid value range can be derived from all the incoming
8217 value ranges, set a new range for the LHS of PHI. */
8219 static enum ssa_prop_result
8220 vrp_visit_phi_node (gimple phi)
8222 size_t i;
8223 tree lhs = PHI_RESULT (phi);
8224 value_range_t *lhs_vr = get_value_range (lhs);
8225 value_range_t vr_result = VR_INITIALIZER;
8226 bool first = true;
8227 int edges, old_edges;
8228 struct loop *l;
8230 if (dump_file && (dump_flags & TDF_DETAILS))
8232 fprintf (dump_file, "\nVisiting PHI node: ");
8233 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8236 edges = 0;
8237 for (i = 0; i < gimple_phi_num_args (phi); i++)
8239 edge e = gimple_phi_arg_edge (phi, i);
8241 if (dump_file && (dump_flags & TDF_DETAILS))
8243 fprintf (dump_file,
8244 "\n Argument #%d (%d -> %d %sexecutable)\n",
8245 (int) i, e->src->index, e->dest->index,
8246 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8249 if (e->flags & EDGE_EXECUTABLE)
8251 tree arg = PHI_ARG_DEF (phi, i);
8252 value_range_t vr_arg;
8254 ++edges;
8256 if (TREE_CODE (arg) == SSA_NAME)
8258 vr_arg = *(get_value_range (arg));
8259 /* Do not allow equivalences or symbolic ranges to leak in from
8260 backedges. That creates invalid equivalencies.
8261 See PR53465 and PR54767. */
8262 if (e->flags & EDGE_DFS_BACK
8263 && (vr_arg.type == VR_RANGE
8264 || vr_arg.type == VR_ANTI_RANGE))
8266 vr_arg.equiv = NULL;
8267 if (symbolic_range_p (&vr_arg))
8269 vr_arg.type = VR_VARYING;
8270 vr_arg.min = NULL_TREE;
8271 vr_arg.max = NULL_TREE;
8275 else
8277 if (TREE_OVERFLOW_P (arg))
8278 arg = drop_tree_overflow (arg);
8280 vr_arg.type = VR_RANGE;
8281 vr_arg.min = arg;
8282 vr_arg.max = arg;
8283 vr_arg.equiv = NULL;
8286 if (dump_file && (dump_flags & TDF_DETAILS))
8288 fprintf (dump_file, "\t");
8289 print_generic_expr (dump_file, arg, dump_flags);
8290 fprintf (dump_file, "\n\tValue: ");
8291 dump_value_range (dump_file, &vr_arg);
8292 fprintf (dump_file, "\n");
8295 if (first)
8296 copy_value_range (&vr_result, &vr_arg);
8297 else
8298 vrp_meet (&vr_result, &vr_arg);
8299 first = false;
8301 if (vr_result.type == VR_VARYING)
8302 break;
8306 if (vr_result.type == VR_VARYING)
8307 goto varying;
8308 else if (vr_result.type == VR_UNDEFINED)
8309 goto update_range;
8311 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8312 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8314 /* To prevent infinite iterations in the algorithm, derive ranges
8315 when the new value is slightly bigger or smaller than the
8316 previous one. We don't do this if we have seen a new executable
8317 edge; this helps us avoid an overflow infinity for conditionals
8318 which are not in a loop. If the old value-range was VR_UNDEFINED
8319 use the updated range and iterate one more time. */
8320 if (edges > 0
8321 && gimple_phi_num_args (phi) > 1
8322 && edges == old_edges
8323 && lhs_vr->type != VR_UNDEFINED)
8325 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8326 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8328 /* For non VR_RANGE or for pointers fall back to varying if
8329 the range changed. */
8330 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8331 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8332 && (cmp_min != 0 || cmp_max != 0))
8333 goto varying;
8335 /* If the new minimum is larger than than the previous one
8336 retain the old value. If the new minimum value is smaller
8337 than the previous one and not -INF go all the way to -INF + 1.
8338 In the first case, to avoid infinite bouncing between different
8339 minimums, and in the other case to avoid iterating millions of
8340 times to reach -INF. Going to -INF + 1 also lets the following
8341 iteration compute whether there will be any overflow, at the
8342 expense of one additional iteration. */
8343 if (cmp_min < 0)
8344 vr_result.min = lhs_vr->min;
8345 else if (cmp_min > 0
8346 && !vrp_val_is_min (vr_result.min))
8347 vr_result.min
8348 = int_const_binop (PLUS_EXPR,
8349 vrp_val_min (TREE_TYPE (vr_result.min)),
8350 build_int_cst (TREE_TYPE (vr_result.min), 1));
8352 /* Similarly for the maximum value. */
8353 if (cmp_max > 0)
8354 vr_result.max = lhs_vr->max;
8355 else if (cmp_max < 0
8356 && !vrp_val_is_max (vr_result.max))
8357 vr_result.max
8358 = int_const_binop (MINUS_EXPR,
8359 vrp_val_max (TREE_TYPE (vr_result.min)),
8360 build_int_cst (TREE_TYPE (vr_result.min), 1));
8362 /* If we dropped either bound to +-INF then if this is a loop
8363 PHI node SCEV may known more about its value-range. */
8364 if ((cmp_min > 0 || cmp_min < 0
8365 || cmp_max < 0 || cmp_max > 0)
8366 && current_loops
8367 && (l = loop_containing_stmt (phi))
8368 && l->header == gimple_bb (phi))
8369 adjust_range_with_scev (&vr_result, l, phi, lhs);
8371 /* If we will end up with a (-INF, +INF) range, set it to
8372 VARYING. Same if the previous max value was invalid for
8373 the type and we end up with vr_result.min > vr_result.max. */
8374 if ((vrp_val_is_max (vr_result.max)
8375 && vrp_val_is_min (vr_result.min))
8376 || compare_values (vr_result.min,
8377 vr_result.max) > 0)
8378 goto varying;
8381 /* If the new range is different than the previous value, keep
8382 iterating. */
8383 update_range:
8384 if (update_value_range (lhs, &vr_result))
8386 if (dump_file && (dump_flags & TDF_DETAILS))
8388 fprintf (dump_file, "Found new range for ");
8389 print_generic_expr (dump_file, lhs, 0);
8390 fprintf (dump_file, ": ");
8391 dump_value_range (dump_file, &vr_result);
8392 fprintf (dump_file, "\n\n");
8395 return SSA_PROP_INTERESTING;
8398 /* Nothing changed, don't add outgoing edges. */
8399 return SSA_PROP_NOT_INTERESTING;
8401 /* No match found. Set the LHS to VARYING. */
8402 varying:
8403 set_value_range_to_varying (lhs_vr);
8404 return SSA_PROP_VARYING;
8407 /* Simplify boolean operations if the source is known
8408 to be already a boolean. */
8409 static bool
8410 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8412 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8413 tree lhs, op0, op1;
8414 bool need_conversion;
8416 /* We handle only !=/== case here. */
8417 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8419 op0 = gimple_assign_rhs1 (stmt);
8420 if (!op_with_boolean_value_range_p (op0))
8421 return false;
8423 op1 = gimple_assign_rhs2 (stmt);
8424 if (!op_with_boolean_value_range_p (op1))
8425 return false;
8427 /* Reduce number of cases to handle to NE_EXPR. As there is no
8428 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8429 if (rhs_code == EQ_EXPR)
8431 if (TREE_CODE (op1) == INTEGER_CST)
8432 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8433 build_int_cst (TREE_TYPE (op1), 1));
8434 else
8435 return false;
8438 lhs = gimple_assign_lhs (stmt);
8439 need_conversion
8440 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8442 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8443 if (need_conversion
8444 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8445 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8446 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8447 return false;
8449 /* For A != 0 we can substitute A itself. */
8450 if (integer_zerop (op1))
8451 gimple_assign_set_rhs_with_ops (gsi,
8452 need_conversion
8453 ? NOP_EXPR : TREE_CODE (op0),
8454 op0, NULL_TREE);
8455 /* For A != B we substitute A ^ B. Either with conversion. */
8456 else if (need_conversion)
8458 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8459 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8460 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8461 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8463 /* Or without. */
8464 else
8465 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8466 update_stmt (gsi_stmt (*gsi));
8468 return true;
8471 /* Simplify a division or modulo operator to a right shift or
8472 bitwise and if the first operand is unsigned or is greater
8473 than zero and the second operand is an exact power of two. */
8475 static bool
8476 simplify_div_or_mod_using_ranges (gimple stmt)
8478 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8479 tree val = NULL;
8480 tree op0 = gimple_assign_rhs1 (stmt);
8481 tree op1 = gimple_assign_rhs2 (stmt);
8482 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8484 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8486 val = integer_one_node;
8488 else
8490 bool sop = false;
8492 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8494 if (val
8495 && sop
8496 && integer_onep (val)
8497 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8499 location_t location;
8501 if (!gimple_has_location (stmt))
8502 location = input_location;
8503 else
8504 location = gimple_location (stmt);
8505 warning_at (location, OPT_Wstrict_overflow,
8506 "assuming signed overflow does not occur when "
8507 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8511 if (val && integer_onep (val))
8513 tree t;
8515 if (rhs_code == TRUNC_DIV_EXPR)
8517 t = build_int_cst (integer_type_node, tree_log2 (op1));
8518 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8519 gimple_assign_set_rhs1 (stmt, op0);
8520 gimple_assign_set_rhs2 (stmt, t);
8522 else
8524 t = build_int_cst (TREE_TYPE (op1), 1);
8525 t = int_const_binop (MINUS_EXPR, op1, t);
8526 t = fold_convert (TREE_TYPE (op0), t);
8528 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8529 gimple_assign_set_rhs1 (stmt, op0);
8530 gimple_assign_set_rhs2 (stmt, t);
8533 update_stmt (stmt);
8534 return true;
8537 return false;
8540 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8541 ABS_EXPR. If the operand is <= 0, then simplify the
8542 ABS_EXPR into a NEGATE_EXPR. */
8544 static bool
8545 simplify_abs_using_ranges (gimple stmt)
8547 tree val = NULL;
8548 tree op = gimple_assign_rhs1 (stmt);
8549 tree type = TREE_TYPE (op);
8550 value_range_t *vr = get_value_range (op);
8552 if (TYPE_UNSIGNED (type))
8554 val = integer_zero_node;
8556 else if (vr)
8558 bool sop = false;
8560 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8561 if (!val)
8563 sop = false;
8564 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8565 &sop);
8567 if (val)
8569 if (integer_zerop (val))
8570 val = integer_one_node;
8571 else if (integer_onep (val))
8572 val = integer_zero_node;
8576 if (val
8577 && (integer_onep (val) || integer_zerop (val)))
8579 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8581 location_t location;
8583 if (!gimple_has_location (stmt))
8584 location = input_location;
8585 else
8586 location = gimple_location (stmt);
8587 warning_at (location, OPT_Wstrict_overflow,
8588 "assuming signed overflow does not occur when "
8589 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8592 gimple_assign_set_rhs1 (stmt, op);
8593 if (integer_onep (val))
8594 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8595 else
8596 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8597 update_stmt (stmt);
8598 return true;
8602 return false;
8605 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8606 If all the bits that are being cleared by & are already
8607 known to be zero from VR, or all the bits that are being
8608 set by | are already known to be one from VR, the bit
8609 operation is redundant. */
8611 static bool
8612 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8614 tree op0 = gimple_assign_rhs1 (stmt);
8615 tree op1 = gimple_assign_rhs2 (stmt);
8616 tree op = NULL_TREE;
8617 value_range_t vr0 = VR_INITIALIZER;
8618 value_range_t vr1 = VR_INITIALIZER;
8619 wide_int may_be_nonzero0, may_be_nonzero1;
8620 wide_int must_be_nonzero0, must_be_nonzero1;
8621 wide_int mask;
8623 if (TREE_CODE (op0) == SSA_NAME)
8624 vr0 = *(get_value_range (op0));
8625 else if (is_gimple_min_invariant (op0))
8626 set_value_range_to_value (&vr0, op0, NULL);
8627 else
8628 return false;
8630 if (TREE_CODE (op1) == SSA_NAME)
8631 vr1 = *(get_value_range (op1));
8632 else if (is_gimple_min_invariant (op1))
8633 set_value_range_to_value (&vr1, op1, NULL);
8634 else
8635 return false;
8637 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
8638 &must_be_nonzero0))
8639 return false;
8640 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
8641 &must_be_nonzero1))
8642 return false;
8644 switch (gimple_assign_rhs_code (stmt))
8646 case BIT_AND_EXPR:
8647 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8648 if (mask == 0)
8650 op = op0;
8651 break;
8653 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8654 if (mask == 0)
8656 op = op1;
8657 break;
8659 break;
8660 case BIT_IOR_EXPR:
8661 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8662 if (mask == 0)
8664 op = op1;
8665 break;
8667 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8668 if (mask == 0)
8670 op = op0;
8671 break;
8673 break;
8674 default:
8675 gcc_unreachable ();
8678 if (op == NULL_TREE)
8679 return false;
8681 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8682 update_stmt (gsi_stmt (*gsi));
8683 return true;
8686 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8687 a known value range VR.
8689 If there is one and only one value which will satisfy the
8690 conditional, then return that value. Else return NULL. */
8692 static tree
8693 test_for_singularity (enum tree_code cond_code, tree op0,
8694 tree op1, value_range_t *vr)
8696 tree min = NULL;
8697 tree max = NULL;
8699 /* Extract minimum/maximum values which satisfy the
8700 the conditional as it was written. */
8701 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8703 /* This should not be negative infinity; there is no overflow
8704 here. */
8705 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8707 max = op1;
8708 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8710 tree one = build_int_cst (TREE_TYPE (op0), 1);
8711 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8712 if (EXPR_P (max))
8713 TREE_NO_WARNING (max) = 1;
8716 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8718 /* This should not be positive infinity; there is no overflow
8719 here. */
8720 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8722 min = op1;
8723 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8725 tree one = build_int_cst (TREE_TYPE (op0), 1);
8726 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8727 if (EXPR_P (min))
8728 TREE_NO_WARNING (min) = 1;
8732 /* Now refine the minimum and maximum values using any
8733 value range information we have for op0. */
8734 if (min && max)
8736 if (compare_values (vr->min, min) == 1)
8737 min = vr->min;
8738 if (compare_values (vr->max, max) == -1)
8739 max = vr->max;
8741 /* If the new min/max values have converged to a single value,
8742 then there is only one value which can satisfy the condition,
8743 return that value. */
8744 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
8745 return min;
8747 return NULL;
8750 /* Return whether the value range *VR fits in an integer type specified
8751 by PRECISION and UNSIGNED_P. */
8753 static bool
8754 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
8756 tree src_type;
8757 unsigned src_precision;
8758 widest_int tem;
8759 signop src_sgn;
8761 /* We can only handle integral and pointer types. */
8762 src_type = TREE_TYPE (vr->min);
8763 if (!INTEGRAL_TYPE_P (src_type)
8764 && !POINTER_TYPE_P (src_type))
8765 return false;
8767 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
8768 and so is an identity transform. */
8769 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
8770 src_sgn = TYPE_SIGN (src_type);
8771 if ((src_precision < dest_precision
8772 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
8773 || (src_precision == dest_precision && src_sgn == dest_sgn))
8774 return true;
8776 /* Now we can only handle ranges with constant bounds. */
8777 if (vr->type != VR_RANGE
8778 || TREE_CODE (vr->min) != INTEGER_CST
8779 || TREE_CODE (vr->max) != INTEGER_CST)
8780 return false;
8782 /* For sign changes, the MSB of the wide_int has to be clear.
8783 An unsigned value with its MSB set cannot be represented by
8784 a signed wide_int, while a negative value cannot be represented
8785 by an unsigned wide_int. */
8786 if (src_sgn != dest_sgn
8787 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
8788 return false;
8790 /* Then we can perform the conversion on both ends and compare
8791 the result for equality. */
8792 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
8793 if (tem != wi::to_widest (vr->min))
8794 return false;
8795 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
8796 if (tem != wi::to_widest (vr->max))
8797 return false;
8799 return true;
8802 /* Simplify a conditional using a relational operator to an equality
8803 test if the range information indicates only one value can satisfy
8804 the original conditional. */
8806 static bool
8807 simplify_cond_using_ranges (gimple stmt)
8809 tree op0 = gimple_cond_lhs (stmt);
8810 tree op1 = gimple_cond_rhs (stmt);
8811 enum tree_code cond_code = gimple_cond_code (stmt);
8813 if (cond_code != NE_EXPR
8814 && cond_code != EQ_EXPR
8815 && TREE_CODE (op0) == SSA_NAME
8816 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8817 && is_gimple_min_invariant (op1))
8819 value_range_t *vr = get_value_range (op0);
8821 /* If we have range information for OP0, then we might be
8822 able to simplify this conditional. */
8823 if (vr->type == VR_RANGE)
8825 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
8827 if (new_tree)
8829 if (dump_file)
8831 fprintf (dump_file, "Simplified relational ");
8832 print_gimple_stmt (dump_file, stmt, 0, 0);
8833 fprintf (dump_file, " into ");
8836 gimple_cond_set_code (stmt, EQ_EXPR);
8837 gimple_cond_set_lhs (stmt, op0);
8838 gimple_cond_set_rhs (stmt, new_tree);
8840 update_stmt (stmt);
8842 if (dump_file)
8844 print_gimple_stmt (dump_file, stmt, 0, 0);
8845 fprintf (dump_file, "\n");
8848 return true;
8851 /* Try again after inverting the condition. We only deal
8852 with integral types here, so no need to worry about
8853 issues with inverting FP comparisons. */
8854 cond_code = invert_tree_comparison (cond_code, false);
8855 new_tree = test_for_singularity (cond_code, op0, op1, vr);
8857 if (new_tree)
8859 if (dump_file)
8861 fprintf (dump_file, "Simplified relational ");
8862 print_gimple_stmt (dump_file, stmt, 0, 0);
8863 fprintf (dump_file, " into ");
8866 gimple_cond_set_code (stmt, NE_EXPR);
8867 gimple_cond_set_lhs (stmt, op0);
8868 gimple_cond_set_rhs (stmt, new_tree);
8870 update_stmt (stmt);
8872 if (dump_file)
8874 print_gimple_stmt (dump_file, stmt, 0, 0);
8875 fprintf (dump_file, "\n");
8878 return true;
8883 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8884 see if OP0 was set by a type conversion where the source of
8885 the conversion is another SSA_NAME with a range that fits
8886 into the range of OP0's type.
8888 If so, the conversion is redundant as the earlier SSA_NAME can be
8889 used for the comparison directly if we just massage the constant in the
8890 comparison. */
8891 if (TREE_CODE (op0) == SSA_NAME
8892 && TREE_CODE (op1) == INTEGER_CST)
8894 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
8895 tree innerop;
8897 if (!is_gimple_assign (def_stmt)
8898 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
8899 return false;
8901 innerop = gimple_assign_rhs1 (def_stmt);
8903 if (TREE_CODE (innerop) == SSA_NAME
8904 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
8906 value_range_t *vr = get_value_range (innerop);
8908 if (range_int_cst_p (vr)
8909 && range_fits_type_p (vr,
8910 TYPE_PRECISION (TREE_TYPE (op0)),
8911 TYPE_SIGN (TREE_TYPE (op0)))
8912 && int_fits_type_p (op1, TREE_TYPE (innerop))
8913 /* The range must not have overflowed, or if it did overflow
8914 we must not be wrapping/trapping overflow and optimizing
8915 with strict overflow semantics. */
8916 && ((!is_negative_overflow_infinity (vr->min)
8917 && !is_positive_overflow_infinity (vr->max))
8918 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
8920 /* If the range overflowed and the user has asked for warnings
8921 when strict overflow semantics were used to optimize code,
8922 issue an appropriate warning. */
8923 if ((is_negative_overflow_infinity (vr->min)
8924 || is_positive_overflow_infinity (vr->max))
8925 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
8927 location_t location;
8929 if (!gimple_has_location (stmt))
8930 location = input_location;
8931 else
8932 location = gimple_location (stmt);
8933 warning_at (location, OPT_Wstrict_overflow,
8934 "assuming signed overflow does not occur when "
8935 "simplifying conditional");
8938 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
8939 gimple_cond_set_lhs (stmt, innerop);
8940 gimple_cond_set_rhs (stmt, newconst);
8941 return true;
8946 return false;
8949 /* Simplify a switch statement using the value range of the switch
8950 argument. */
8952 static bool
8953 simplify_switch_using_ranges (gimple stmt)
8955 tree op = gimple_switch_index (stmt);
8956 value_range_t *vr;
8957 bool take_default;
8958 edge e;
8959 edge_iterator ei;
8960 size_t i = 0, j = 0, n, n2;
8961 tree vec2;
8962 switch_update su;
8963 size_t k = 1, l = 0;
8965 if (TREE_CODE (op) == SSA_NAME)
8967 vr = get_value_range (op);
8969 /* We can only handle integer ranges. */
8970 if ((vr->type != VR_RANGE
8971 && vr->type != VR_ANTI_RANGE)
8972 || symbolic_range_p (vr))
8973 return false;
8975 /* Find case label for min/max of the value range. */
8976 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
8978 else if (TREE_CODE (op) == INTEGER_CST)
8980 take_default = !find_case_label_index (stmt, 1, op, &i);
8981 if (take_default)
8983 i = 1;
8984 j = 0;
8986 else
8988 j = i;
8991 else
8992 return false;
8994 n = gimple_switch_num_labels (stmt);
8996 /* Bail out if this is just all edges taken. */
8997 if (i == 1
8998 && j == n - 1
8999 && take_default)
9000 return false;
9002 /* Build a new vector of taken case labels. */
9003 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9004 n2 = 0;
9006 /* Add the default edge, if necessary. */
9007 if (take_default)
9008 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9010 for (; i <= j; ++i, ++n2)
9011 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9013 for (; k <= l; ++k, ++n2)
9014 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9016 /* Mark needed edges. */
9017 for (i = 0; i < n2; ++i)
9019 e = find_edge (gimple_bb (stmt),
9020 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9021 e->aux = (void *)-1;
9024 /* Queue not needed edges for later removal. */
9025 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9027 if (e->aux == (void *)-1)
9029 e->aux = NULL;
9030 continue;
9033 if (dump_file && (dump_flags & TDF_DETAILS))
9035 fprintf (dump_file, "removing unreachable case label\n");
9037 to_remove_edges.safe_push (e);
9038 e->flags &= ~EDGE_EXECUTABLE;
9041 /* And queue an update for the stmt. */
9042 su.stmt = stmt;
9043 su.vec = vec2;
9044 to_update_switch_stmts.safe_push (su);
9045 return false;
9048 /* Simplify an integral conversion from an SSA name in STMT. */
9050 static bool
9051 simplify_conversion_using_ranges (gimple stmt)
9053 tree innerop, middleop, finaltype;
9054 gimple def_stmt;
9055 value_range_t *innervr;
9056 signop inner_sgn, middle_sgn, final_sgn;
9057 unsigned inner_prec, middle_prec, final_prec;
9058 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9060 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9061 if (!INTEGRAL_TYPE_P (finaltype))
9062 return false;
9063 middleop = gimple_assign_rhs1 (stmt);
9064 def_stmt = SSA_NAME_DEF_STMT (middleop);
9065 if (!is_gimple_assign (def_stmt)
9066 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9067 return false;
9068 innerop = gimple_assign_rhs1 (def_stmt);
9069 if (TREE_CODE (innerop) != SSA_NAME
9070 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9071 return false;
9073 /* Get the value-range of the inner operand. */
9074 innervr = get_value_range (innerop);
9075 if (innervr->type != VR_RANGE
9076 || TREE_CODE (innervr->min) != INTEGER_CST
9077 || TREE_CODE (innervr->max) != INTEGER_CST)
9078 return false;
9080 /* Simulate the conversion chain to check if the result is equal if
9081 the middle conversion is removed. */
9082 innermin = wi::to_widest (innervr->min);
9083 innermax = wi::to_widest (innervr->max);
9085 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9086 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9087 final_prec = TYPE_PRECISION (finaltype);
9089 /* If the first conversion is not injective, the second must not
9090 be widening. */
9091 if (wi::gtu_p (innermax - innermin,
9092 wi::mask <widest_int> (middle_prec, false))
9093 && middle_prec < final_prec)
9094 return false;
9095 /* We also want a medium value so that we can track the effect that
9096 narrowing conversions with sign change have. */
9097 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9098 if (inner_sgn == UNSIGNED)
9099 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9100 else
9101 innermed = 0;
9102 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9103 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9104 innermed = innermin;
9106 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9107 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9108 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9109 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9111 /* Require that the final conversion applied to both the original
9112 and the intermediate range produces the same result. */
9113 final_sgn = TYPE_SIGN (finaltype);
9114 if (wi::ext (middlemin, final_prec, final_sgn)
9115 != wi::ext (innermin, final_prec, final_sgn)
9116 || wi::ext (middlemed, final_prec, final_sgn)
9117 != wi::ext (innermed, final_prec, final_sgn)
9118 || wi::ext (middlemax, final_prec, final_sgn)
9119 != wi::ext (innermax, final_prec, final_sgn))
9120 return false;
9122 gimple_assign_set_rhs1 (stmt, innerop);
9123 update_stmt (stmt);
9124 return true;
9127 /* Simplify a conversion from integral SSA name to float in STMT. */
9129 static bool
9130 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9132 tree rhs1 = gimple_assign_rhs1 (stmt);
9133 value_range_t *vr = get_value_range (rhs1);
9134 enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9135 enum machine_mode mode;
9136 tree tem;
9137 gimple conv;
9139 /* We can only handle constant ranges. */
9140 if (vr->type != VR_RANGE
9141 || TREE_CODE (vr->min) != INTEGER_CST
9142 || TREE_CODE (vr->max) != INTEGER_CST)
9143 return false;
9145 /* First check if we can use a signed type in place of an unsigned. */
9146 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9147 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9148 != CODE_FOR_nothing)
9149 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9150 mode = TYPE_MODE (TREE_TYPE (rhs1));
9151 /* If we can do the conversion in the current input mode do nothing. */
9152 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9153 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9154 return false;
9155 /* Otherwise search for a mode we can use, starting from the narrowest
9156 integer mode available. */
9157 else
9159 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9162 /* If we cannot do a signed conversion to float from mode
9163 or if the value-range does not fit in the signed type
9164 try with a wider mode. */
9165 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9166 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9167 break;
9169 mode = GET_MODE_WIDER_MODE (mode);
9170 /* But do not widen the input. Instead leave that to the
9171 optabs expansion code. */
9172 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9173 return false;
9175 while (mode != VOIDmode);
9176 if (mode == VOIDmode)
9177 return false;
9180 /* It works, insert a truncation or sign-change before the
9181 float conversion. */
9182 tem = make_ssa_name (build_nonstandard_integer_type
9183 (GET_MODE_PRECISION (mode), 0), NULL);
9184 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
9185 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9186 gimple_assign_set_rhs1 (stmt, tem);
9187 update_stmt (stmt);
9189 return true;
9192 /* Simplify an internal fn call using ranges if possible. */
9194 static bool
9195 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9197 enum tree_code subcode;
9198 switch (gimple_call_internal_fn (stmt))
9200 case IFN_UBSAN_CHECK_ADD:
9201 subcode = PLUS_EXPR;
9202 break;
9203 case IFN_UBSAN_CHECK_SUB:
9204 subcode = MINUS_EXPR;
9205 break;
9206 case IFN_UBSAN_CHECK_MUL:
9207 subcode = MULT_EXPR;
9208 break;
9209 default:
9210 return false;
9213 value_range_t vr0 = VR_INITIALIZER;
9214 value_range_t vr1 = VR_INITIALIZER;
9215 tree op0 = gimple_call_arg (stmt, 0);
9216 tree op1 = gimple_call_arg (stmt, 1);
9218 if (TREE_CODE (op0) == SSA_NAME)
9219 vr0 = *get_value_range (op0);
9220 else if (TREE_CODE (op0) == INTEGER_CST)
9221 set_value_range_to_value (&vr0, op0, NULL);
9222 else
9223 set_value_range_to_varying (&vr0);
9225 if (TREE_CODE (op1) == SSA_NAME)
9226 vr1 = *get_value_range (op1);
9227 else if (TREE_CODE (op1) == INTEGER_CST)
9228 set_value_range_to_value (&vr1, op1, NULL);
9229 else
9230 set_value_range_to_varying (&vr1);
9232 if (!range_int_cst_p (&vr0))
9234 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9235 optimize at least x = y + 0; x = y - 0; x = y * 0;
9236 and x = y * 1; which never overflow. */
9237 if (!range_int_cst_p (&vr1))
9238 return false;
9239 if (tree_int_cst_sgn (vr1.min) == -1)
9240 return false;
9241 if (compare_tree_int (vr1.max, subcode == MULT_EXPR) == 1)
9242 return false;
9244 else if (!range_int_cst_p (&vr1))
9246 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9247 optimize at least x = 0 + y; x = 0 * y; and x = 1 * y;
9248 which never overflow. */
9249 if (subcode == MINUS_EXPR)
9250 return false;
9251 if (!range_int_cst_p (&vr0))
9252 return false;
9253 if (tree_int_cst_sgn (vr0.min) == -1)
9254 return false;
9255 if (compare_tree_int (vr0.max, subcode == MULT_EXPR) == 1)
9256 return false;
9258 else
9260 tree r1 = int_const_binop (subcode, vr0.min, vr1.min);
9261 tree r2 = int_const_binop (subcode, vr0.max, vr1.max);
9262 if (r1 == NULL_TREE || TREE_OVERFLOW (r1)
9263 || r2 == NULL_TREE || TREE_OVERFLOW (r2))
9264 return false;
9265 if (subcode == MULT_EXPR)
9267 tree r3 = int_const_binop (subcode, vr0.min, vr1.max);
9268 tree r4 = int_const_binop (subcode, vr0.max, vr1.min);
9269 if (r3 == NULL_TREE || TREE_OVERFLOW (r3)
9270 || r4 == NULL_TREE || TREE_OVERFLOW (r4))
9271 return false;
9275 gimple g = gimple_build_assign_with_ops (subcode, gimple_call_lhs (stmt),
9276 op0, op1);
9277 gsi_replace (gsi, g, false);
9278 return true;
9281 /* Simplify STMT using ranges if possible. */
9283 static bool
9284 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9286 gimple stmt = gsi_stmt (*gsi);
9287 if (is_gimple_assign (stmt))
9289 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9290 tree rhs1 = gimple_assign_rhs1 (stmt);
9292 switch (rhs_code)
9294 case EQ_EXPR:
9295 case NE_EXPR:
9296 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9297 if the RHS is zero or one, and the LHS are known to be boolean
9298 values. */
9299 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9300 return simplify_truth_ops_using_ranges (gsi, stmt);
9301 break;
9303 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9304 and BIT_AND_EXPR respectively if the first operand is greater
9305 than zero and the second operand is an exact power of two. */
9306 case TRUNC_DIV_EXPR:
9307 case TRUNC_MOD_EXPR:
9308 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9309 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9310 return simplify_div_or_mod_using_ranges (stmt);
9311 break;
9313 /* Transform ABS (X) into X or -X as appropriate. */
9314 case ABS_EXPR:
9315 if (TREE_CODE (rhs1) == SSA_NAME
9316 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9317 return simplify_abs_using_ranges (stmt);
9318 break;
9320 case BIT_AND_EXPR:
9321 case BIT_IOR_EXPR:
9322 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9323 if all the bits being cleared are already cleared or
9324 all the bits being set are already set. */
9325 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9326 return simplify_bit_ops_using_ranges (gsi, stmt);
9327 break;
9329 CASE_CONVERT:
9330 if (TREE_CODE (rhs1) == SSA_NAME
9331 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9332 return simplify_conversion_using_ranges (stmt);
9333 break;
9335 case FLOAT_EXPR:
9336 if (TREE_CODE (rhs1) == SSA_NAME
9337 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9338 return simplify_float_conversion_using_ranges (gsi, stmt);
9339 break;
9341 default:
9342 break;
9345 else if (gimple_code (stmt) == GIMPLE_COND)
9346 return simplify_cond_using_ranges (stmt);
9347 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9348 return simplify_switch_using_ranges (stmt);
9349 else if (is_gimple_call (stmt)
9350 && gimple_call_internal_p (stmt))
9351 return simplify_internal_call_using_ranges (gsi, stmt);
9353 return false;
9356 /* If the statement pointed by SI has a predicate whose value can be
9357 computed using the value range information computed by VRP, compute
9358 its value and return true. Otherwise, return false. */
9360 static bool
9361 fold_predicate_in (gimple_stmt_iterator *si)
9363 bool assignment_p = false;
9364 tree val;
9365 gimple stmt = gsi_stmt (*si);
9367 if (is_gimple_assign (stmt)
9368 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9370 assignment_p = true;
9371 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9372 gimple_assign_rhs1 (stmt),
9373 gimple_assign_rhs2 (stmt),
9374 stmt);
9376 else if (gimple_code (stmt) == GIMPLE_COND)
9377 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
9378 gimple_cond_lhs (stmt),
9379 gimple_cond_rhs (stmt),
9380 stmt);
9381 else
9382 return false;
9384 if (val)
9386 if (assignment_p)
9387 val = fold_convert (gimple_expr_type (stmt), val);
9389 if (dump_file)
9391 fprintf (dump_file, "Folding predicate ");
9392 print_gimple_expr (dump_file, stmt, 0, 0);
9393 fprintf (dump_file, " to ");
9394 print_generic_expr (dump_file, val, 0);
9395 fprintf (dump_file, "\n");
9398 if (is_gimple_assign (stmt))
9399 gimple_assign_set_rhs_from_tree (si, val);
9400 else
9402 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9403 if (integer_zerop (val))
9404 gimple_cond_make_false (stmt);
9405 else if (integer_onep (val))
9406 gimple_cond_make_true (stmt);
9407 else
9408 gcc_unreachable ();
9411 return true;
9414 return false;
9417 /* Callback for substitute_and_fold folding the stmt at *SI. */
9419 static bool
9420 vrp_fold_stmt (gimple_stmt_iterator *si)
9422 if (fold_predicate_in (si))
9423 return true;
9425 return simplify_stmt_using_ranges (si);
9428 /* Stack of dest,src equivalency pairs that need to be restored after
9429 each attempt to thread a block's incoming edge to an outgoing edge.
9431 A NULL entry is used to mark the end of pairs which need to be
9432 restored. */
9433 static vec<tree> equiv_stack;
9435 /* A trivial wrapper so that we can present the generic jump threading
9436 code with a simple API for simplifying statements. STMT is the
9437 statement we want to simplify, WITHIN_STMT provides the location
9438 for any overflow warnings. */
9440 static tree
9441 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9443 if (gimple_code (stmt) == GIMPLE_COND)
9444 return vrp_evaluate_conditional (gimple_cond_code (stmt),
9445 gimple_cond_lhs (stmt),
9446 gimple_cond_rhs (stmt), within_stmt);
9448 if (gimple_code (stmt) == GIMPLE_ASSIGN)
9450 value_range_t new_vr = VR_INITIALIZER;
9451 tree lhs = gimple_assign_lhs (stmt);
9453 if (TREE_CODE (lhs) == SSA_NAME
9454 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
9455 || POINTER_TYPE_P (TREE_TYPE (lhs))))
9457 extract_range_from_assignment (&new_vr, stmt);
9458 if (range_int_cst_singleton_p (&new_vr))
9459 return new_vr.min;
9463 return NULL_TREE;
9466 /* Blocks which have more than one predecessor and more than
9467 one successor present jump threading opportunities, i.e.,
9468 when the block is reached from a specific predecessor, we
9469 may be able to determine which of the outgoing edges will
9470 be traversed. When this optimization applies, we are able
9471 to avoid conditionals at runtime and we may expose secondary
9472 optimization opportunities.
9474 This routine is effectively a driver for the generic jump
9475 threading code. It basically just presents the generic code
9476 with edges that may be suitable for jump threading.
9478 Unlike DOM, we do not iterate VRP if jump threading was successful.
9479 While iterating may expose new opportunities for VRP, it is expected
9480 those opportunities would be very limited and the compile time cost
9481 to expose those opportunities would be significant.
9483 As jump threading opportunities are discovered, they are registered
9484 for later realization. */
9486 static void
9487 identify_jump_threads (void)
9489 basic_block bb;
9490 gimple dummy;
9491 int i;
9492 edge e;
9494 /* Ugh. When substituting values earlier in this pass we can
9495 wipe the dominance information. So rebuild the dominator
9496 information as we need it within the jump threading code. */
9497 calculate_dominance_info (CDI_DOMINATORS);
9499 /* We do not allow VRP information to be used for jump threading
9500 across a back edge in the CFG. Otherwise it becomes too
9501 difficult to avoid eliminating loop exit tests. Of course
9502 EDGE_DFS_BACK is not accurate at this time so we have to
9503 recompute it. */
9504 mark_dfs_back_edges ();
9506 /* Do not thread across edges we are about to remove. Just marking
9507 them as EDGE_DFS_BACK will do. */
9508 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9509 e->flags |= EDGE_DFS_BACK;
9511 /* Allocate our unwinder stack to unwind any temporary equivalences
9512 that might be recorded. */
9513 equiv_stack.create (20);
9515 /* To avoid lots of silly node creation, we create a single
9516 conditional and just modify it in-place when attempting to
9517 thread jumps. */
9518 dummy = gimple_build_cond (EQ_EXPR,
9519 integer_zero_node, integer_zero_node,
9520 NULL, NULL);
9522 /* Walk through all the blocks finding those which present a
9523 potential jump threading opportunity. We could set this up
9524 as a dominator walker and record data during the walk, but
9525 I doubt it's worth the effort for the classes of jump
9526 threading opportunities we are trying to identify at this
9527 point in compilation. */
9528 FOR_EACH_BB_FN (bb, cfun)
9530 gimple last;
9532 /* If the generic jump threading code does not find this block
9533 interesting, then there is nothing to do. */
9534 if (! potentially_threadable_block (bb))
9535 continue;
9537 /* We only care about blocks ending in a COND_EXPR. While there
9538 may be some value in handling SWITCH_EXPR here, I doubt it's
9539 terribly important. */
9540 last = gsi_stmt (gsi_last_bb (bb));
9542 /* We're basically looking for a switch or any kind of conditional with
9543 integral or pointer type arguments. Note the type of the second
9544 argument will be the same as the first argument, so no need to
9545 check it explicitly. */
9546 if (gimple_code (last) == GIMPLE_SWITCH
9547 || (gimple_code (last) == GIMPLE_COND
9548 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9549 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9550 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9551 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9552 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9554 edge_iterator ei;
9556 /* We've got a block with multiple predecessors and multiple
9557 successors which also ends in a suitable conditional or
9558 switch statement. For each predecessor, see if we can thread
9559 it to a specific successor. */
9560 FOR_EACH_EDGE (e, ei, bb->preds)
9562 /* Do not thread across back edges or abnormal edges
9563 in the CFG. */
9564 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9565 continue;
9567 thread_across_edge (dummy, e, true, &equiv_stack,
9568 simplify_stmt_for_jump_threading);
9573 /* We do not actually update the CFG or SSA graphs at this point as
9574 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9575 handle ASSERT_EXPRs gracefully. */
9578 /* We identified all the jump threading opportunities earlier, but could
9579 not transform the CFG at that time. This routine transforms the
9580 CFG and arranges for the dominator tree to be rebuilt if necessary.
9582 Note the SSA graph update will occur during the normal TODO
9583 processing by the pass manager. */
9584 static void
9585 finalize_jump_threads (void)
9587 thread_through_all_blocks (false);
9588 equiv_stack.release ();
9592 /* Traverse all the blocks folding conditionals with known ranges. */
9594 static void
9595 vrp_finalize (void)
9597 size_t i;
9599 values_propagated = true;
9601 if (dump_file)
9603 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9604 dump_all_value_ranges (dump_file);
9605 fprintf (dump_file, "\n");
9608 substitute_and_fold (op_with_constant_singleton_value_range,
9609 vrp_fold_stmt, false);
9611 if (warn_array_bounds)
9612 check_all_array_refs ();
9614 /* We must identify jump threading opportunities before we release
9615 the datastructures built by VRP. */
9616 identify_jump_threads ();
9618 /* Set value range to non pointer SSA_NAMEs. */
9619 for (i = 0; i < num_vr_values; i++)
9620 if (vr_value[i])
9622 tree name = ssa_name (i);
9624 if (!name
9625 || POINTER_TYPE_P (TREE_TYPE (name))
9626 || (vr_value[i]->type == VR_VARYING)
9627 || (vr_value[i]->type == VR_UNDEFINED))
9628 continue;
9630 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
9631 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
9632 && (vr_value[i]->type == VR_RANGE
9633 || vr_value[i]->type == VR_ANTI_RANGE))
9634 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
9635 vr_value[i]->max);
9638 /* Free allocated memory. */
9639 for (i = 0; i < num_vr_values; i++)
9640 if (vr_value[i])
9642 BITMAP_FREE (vr_value[i]->equiv);
9643 free (vr_value[i]);
9646 free (vr_value);
9647 free (vr_phi_edge_counts);
9649 /* So that we can distinguish between VRP data being available
9650 and not available. */
9651 vr_value = NULL;
9652 vr_phi_edge_counts = NULL;
9656 /* Main entry point to VRP (Value Range Propagation). This pass is
9657 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9658 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9659 Programming Language Design and Implementation, pp. 67-78, 1995.
9660 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9662 This is essentially an SSA-CCP pass modified to deal with ranges
9663 instead of constants.
9665 While propagating ranges, we may find that two or more SSA name
9666 have equivalent, though distinct ranges. For instance,
9668 1 x_9 = p_3->a;
9669 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9670 3 if (p_4 == q_2)
9671 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9672 5 endif
9673 6 if (q_2)
9675 In the code above, pointer p_5 has range [q_2, q_2], but from the
9676 code we can also determine that p_5 cannot be NULL and, if q_2 had
9677 a non-varying range, p_5's range should also be compatible with it.
9679 These equivalences are created by two expressions: ASSERT_EXPR and
9680 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9681 result of another assertion, then we can use the fact that p_5 and
9682 p_4 are equivalent when evaluating p_5's range.
9684 Together with value ranges, we also propagate these equivalences
9685 between names so that we can take advantage of information from
9686 multiple ranges when doing final replacement. Note that this
9687 equivalency relation is transitive but not symmetric.
9689 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9690 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9691 in contexts where that assertion does not hold (e.g., in line 6).
9693 TODO, the main difference between this pass and Patterson's is that
9694 we do not propagate edge probabilities. We only compute whether
9695 edges can be taken or not. That is, instead of having a spectrum
9696 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9697 DON'T KNOW. In the future, it may be worthwhile to propagate
9698 probabilities to aid branch prediction. */
9700 static unsigned int
9701 execute_vrp (void)
9703 int i;
9704 edge e;
9705 switch_update *su;
9707 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9708 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9709 scev_initialize ();
9711 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9712 Inserting assertions may split edges which will invalidate
9713 EDGE_DFS_BACK. */
9714 insert_range_assertions ();
9716 to_remove_edges.create (10);
9717 to_update_switch_stmts.create (5);
9718 threadedge_initialize_values ();
9720 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9721 mark_dfs_back_edges ();
9723 vrp_initialize ();
9724 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
9725 vrp_finalize ();
9727 free_numbers_of_iterations_estimates ();
9729 /* ASSERT_EXPRs must be removed before finalizing jump threads
9730 as finalizing jump threads calls the CFG cleanup code which
9731 does not properly handle ASSERT_EXPRs. */
9732 remove_range_assertions ();
9734 /* If we exposed any new variables, go ahead and put them into
9735 SSA form now, before we handle jump threading. This simplifies
9736 interactions between rewriting of _DECL nodes into SSA form
9737 and rewriting SSA_NAME nodes into SSA form after block
9738 duplication and CFG manipulation. */
9739 update_ssa (TODO_update_ssa);
9741 finalize_jump_threads ();
9743 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9744 CFG in a broken state and requires a cfg_cleanup run. */
9745 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9746 remove_edge (e);
9747 /* Update SWITCH_EXPR case label vector. */
9748 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
9750 size_t j;
9751 size_t n = TREE_VEC_LENGTH (su->vec);
9752 tree label;
9753 gimple_switch_set_num_labels (su->stmt, n);
9754 for (j = 0; j < n; j++)
9755 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
9756 /* As we may have replaced the default label with a regular one
9757 make sure to make it a real default label again. This ensures
9758 optimal expansion. */
9759 label = gimple_switch_label (su->stmt, 0);
9760 CASE_LOW (label) = NULL_TREE;
9761 CASE_HIGH (label) = NULL_TREE;
9764 if (to_remove_edges.length () > 0)
9766 free_dominance_info (CDI_DOMINATORS);
9767 if (current_loops)
9768 loops_state_set (LOOPS_NEED_FIXUP);
9771 to_remove_edges.release ();
9772 to_update_switch_stmts.release ();
9773 threadedge_finalize_values ();
9775 scev_finalize ();
9776 loop_optimizer_finalize ();
9777 return 0;
9780 namespace {
9782 const pass_data pass_data_vrp =
9784 GIMPLE_PASS, /* type */
9785 "vrp", /* name */
9786 OPTGROUP_NONE, /* optinfo_flags */
9787 true, /* has_execute */
9788 TV_TREE_VRP, /* tv_id */
9789 PROP_ssa, /* properties_required */
9790 0, /* properties_provided */
9791 0, /* properties_destroyed */
9792 0, /* todo_flags_start */
9793 ( TODO_cleanup_cfg | TODO_update_ssa ), /* 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 virtual bool gate (function *) { return flag_tree_vrp != 0; }
9806 virtual unsigned int execute (function *) { 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);