2014-07-29 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / tree-vrp.c
blobd16fd8ab5cdd8fac3b0b5deee9ec447e3a1d59a7
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_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 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3540 || (vr0->type == VR_ANTI_RANGE
3541 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3542 && !is_overflow_infinity (vr0->min)
3543 && !is_overflow_infinity (vr0->max))
3544 mini = 1;
3545 /* If some high bits are known to be zero,
3546 we can decrease the maximum. */
3547 if (vr0->type == VR_RANGE
3548 && TREE_CODE (vr0->max) == INTEGER_CST
3549 && !operand_less_p (vr0->min,
3550 build_zero_cst (TREE_TYPE (vr0->min)))
3551 && !is_overflow_infinity (vr0->max))
3552 maxi = tree_floor_log2 (vr0->max) + 1;
3554 goto bitop_builtin;
3555 /* __builtin_parity* returns [0, 1]. */
3556 CASE_INT_FN (BUILT_IN_PARITY):
3557 mini = 0;
3558 maxi = 1;
3559 goto bitop_builtin;
3560 /* __builtin_c[lt]z* return [0, prec-1], except for
3561 when the argument is 0, but that is undefined behavior.
3562 On many targets where the CLZ RTL or optab value is defined
3563 for 0 the value is prec, so include that in the range
3564 by default. */
3565 CASE_INT_FN (BUILT_IN_CLZ):
3566 arg = gimple_call_arg (stmt, 0);
3567 prec = TYPE_PRECISION (TREE_TYPE (arg));
3568 mini = 0;
3569 maxi = prec;
3570 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3571 != CODE_FOR_nothing
3572 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3573 zerov)
3574 /* Handle only the single common value. */
3575 && zerov != prec)
3576 /* Magic value to give up, unless vr0 proves
3577 arg is non-zero. */
3578 mini = -2;
3579 if (TREE_CODE (arg) == SSA_NAME)
3581 value_range_t *vr0 = get_value_range (arg);
3582 /* From clz of VR_RANGE minimum we can compute
3583 result maximum. */
3584 if (vr0->type == VR_RANGE
3585 && TREE_CODE (vr0->min) == INTEGER_CST
3586 && !is_overflow_infinity (vr0->min))
3588 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3589 if (maxi != prec)
3590 mini = 0;
3592 else if (vr0->type == VR_ANTI_RANGE
3593 && integer_zerop (vr0->min)
3594 && !is_overflow_infinity (vr0->min))
3596 maxi = prec - 1;
3597 mini = 0;
3599 if (mini == -2)
3600 break;
3601 /* From clz of VR_RANGE maximum we can compute
3602 result minimum. */
3603 if (vr0->type == VR_RANGE
3604 && TREE_CODE (vr0->max) == INTEGER_CST
3605 && !is_overflow_infinity (vr0->max))
3607 mini = prec - 1 - tree_floor_log2 (vr0->max);
3608 if (mini == prec)
3609 break;
3612 if (mini == -2)
3613 break;
3614 goto bitop_builtin;
3615 /* __builtin_ctz* return [0, prec-1], except for
3616 when the argument is 0, but that is undefined behavior.
3617 If there is a ctz optab for this mode and
3618 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3619 otherwise just assume 0 won't be seen. */
3620 CASE_INT_FN (BUILT_IN_CTZ):
3621 arg = gimple_call_arg (stmt, 0);
3622 prec = TYPE_PRECISION (TREE_TYPE (arg));
3623 mini = 0;
3624 maxi = prec - 1;
3625 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3626 != CODE_FOR_nothing
3627 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3628 zerov))
3630 /* Handle only the two common values. */
3631 if (zerov == -1)
3632 mini = -1;
3633 else if (zerov == prec)
3634 maxi = prec;
3635 else
3636 /* Magic value to give up, unless vr0 proves
3637 arg is non-zero. */
3638 mini = -2;
3640 if (TREE_CODE (arg) == SSA_NAME)
3642 value_range_t *vr0 = get_value_range (arg);
3643 /* If arg is non-zero, then use [0, prec - 1]. */
3644 if (((vr0->type == VR_RANGE
3645 && integer_nonzerop (vr0->min))
3646 || (vr0->type == VR_ANTI_RANGE
3647 && integer_zerop (vr0->min)))
3648 && !is_overflow_infinity (vr0->min))
3650 mini = 0;
3651 maxi = prec - 1;
3653 /* If some high bits are known to be zero,
3654 we can decrease the result maximum. */
3655 if (vr0->type == VR_RANGE
3656 && TREE_CODE (vr0->max) == INTEGER_CST
3657 && !is_overflow_infinity (vr0->max))
3659 maxi = tree_floor_log2 (vr0->max);
3660 /* For vr0 [0, 0] give up. */
3661 if (maxi == -1)
3662 break;
3665 if (mini == -2)
3666 break;
3667 goto bitop_builtin;
3668 /* __builtin_clrsb* returns [0, prec-1]. */
3669 CASE_INT_FN (BUILT_IN_CLRSB):
3670 arg = gimple_call_arg (stmt, 0);
3671 prec = TYPE_PRECISION (TREE_TYPE (arg));
3672 mini = 0;
3673 maxi = prec - 1;
3674 goto bitop_builtin;
3675 bitop_builtin:
3676 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3677 build_int_cst (type, maxi), NULL);
3678 return;
3679 default:
3680 break;
3683 else if (is_gimple_call (stmt)
3684 && gimple_call_internal_p (stmt))
3686 enum tree_code subcode = ERROR_MARK;
3687 switch (gimple_call_internal_fn (stmt))
3689 case IFN_UBSAN_CHECK_ADD:
3690 subcode = PLUS_EXPR;
3691 break;
3692 case IFN_UBSAN_CHECK_SUB:
3693 subcode = MINUS_EXPR;
3694 break;
3695 case IFN_UBSAN_CHECK_MUL:
3696 subcode = MULT_EXPR;
3697 break;
3698 default:
3699 break;
3701 if (subcode != ERROR_MARK)
3703 bool saved_flag_wrapv = flag_wrapv;
3704 /* Pretend the arithmetics is wrapping. If there is
3705 any overflow, we'll complain, but will actually do
3706 wrapping operation. */
3707 flag_wrapv = 1;
3708 extract_range_from_binary_expr (vr, subcode, type,
3709 gimple_call_arg (stmt, 0),
3710 gimple_call_arg (stmt, 1));
3711 flag_wrapv = saved_flag_wrapv;
3713 /* If for both arguments vrp_valueize returned non-NULL,
3714 this should have been already folded and if not, it
3715 wasn't folded because of overflow. Avoid removing the
3716 UBSAN_CHECK_* calls in that case. */
3717 if (vr->type == VR_RANGE
3718 && (vr->min == vr->max
3719 || operand_equal_p (vr->min, vr->max, 0)))
3720 set_value_range_to_varying (vr);
3721 return;
3724 if (INTEGRAL_TYPE_P (type)
3725 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3726 set_value_range_to_nonnegative (vr, type,
3727 sop || stmt_overflow_infinity (stmt));
3728 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3729 && !sop)
3730 set_value_range_to_nonnull (vr, type);
3731 else
3732 set_value_range_to_varying (vr);
3736 /* Try to compute a useful range out of assignment STMT and store it
3737 in *VR. */
3739 static void
3740 extract_range_from_assignment (value_range_t *vr, gimple stmt)
3742 enum tree_code code = gimple_assign_rhs_code (stmt);
3744 if (code == ASSERT_EXPR)
3745 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3746 else if (code == SSA_NAME)
3747 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3748 else if (TREE_CODE_CLASS (code) == tcc_binary)
3749 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3750 gimple_expr_type (stmt),
3751 gimple_assign_rhs1 (stmt),
3752 gimple_assign_rhs2 (stmt));
3753 else if (TREE_CODE_CLASS (code) == tcc_unary)
3754 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3755 gimple_expr_type (stmt),
3756 gimple_assign_rhs1 (stmt));
3757 else if (code == COND_EXPR)
3758 extract_range_from_cond_expr (vr, stmt);
3759 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3760 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3761 gimple_expr_type (stmt),
3762 gimple_assign_rhs1 (stmt),
3763 gimple_assign_rhs2 (stmt));
3764 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3765 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3766 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3767 else
3768 set_value_range_to_varying (vr);
3770 if (vr->type == VR_VARYING)
3771 extract_range_basic (vr, stmt);
3774 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3775 would be profitable to adjust VR using scalar evolution information
3776 for VAR. If so, update VR with the new limits. */
3778 static void
3779 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
3780 gimple stmt, tree var)
3782 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3783 enum ev_direction dir;
3785 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3786 better opportunities than a regular range, but I'm not sure. */
3787 if (vr->type == VR_ANTI_RANGE)
3788 return;
3790 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3792 /* Like in PR19590, scev can return a constant function. */
3793 if (is_gimple_min_invariant (chrec))
3795 set_value_range_to_value (vr, chrec, vr->equiv);
3796 return;
3799 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3800 return;
3802 init = initial_condition_in_loop_num (chrec, loop->num);
3803 tem = op_with_constant_singleton_value_range (init);
3804 if (tem)
3805 init = tem;
3806 step = evolution_part_in_loop_num (chrec, loop->num);
3807 tem = op_with_constant_singleton_value_range (step);
3808 if (tem)
3809 step = tem;
3811 /* If STEP is symbolic, we can't know whether INIT will be the
3812 minimum or maximum value in the range. Also, unless INIT is
3813 a simple expression, compare_values and possibly other functions
3814 in tree-vrp won't be able to handle it. */
3815 if (step == NULL_TREE
3816 || !is_gimple_min_invariant (step)
3817 || !valid_value_p (init))
3818 return;
3820 dir = scev_direction (chrec);
3821 if (/* Do not adjust ranges if we do not know whether the iv increases
3822 or decreases, ... */
3823 dir == EV_DIR_UNKNOWN
3824 /* ... or if it may wrap. */
3825 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3826 true))
3827 return;
3829 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3830 negative_overflow_infinity and positive_overflow_infinity,
3831 because we have concluded that the loop probably does not
3832 wrap. */
3834 type = TREE_TYPE (var);
3835 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
3836 tmin = lower_bound_in_type (type, type);
3837 else
3838 tmin = TYPE_MIN_VALUE (type);
3839 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
3840 tmax = upper_bound_in_type (type, type);
3841 else
3842 tmax = TYPE_MAX_VALUE (type);
3844 /* Try to use estimated number of iterations for the loop to constrain the
3845 final value in the evolution. */
3846 if (TREE_CODE (step) == INTEGER_CST
3847 && is_gimple_val (init)
3848 && (TREE_CODE (init) != SSA_NAME
3849 || get_value_range (init)->type == VR_RANGE))
3851 widest_int nit;
3853 /* We are only entering here for loop header PHI nodes, so using
3854 the number of latch executions is the correct thing to use. */
3855 if (max_loop_iterations (loop, &nit))
3857 value_range_t maxvr = VR_INITIALIZER;
3858 signop sgn = TYPE_SIGN (TREE_TYPE (step));
3859 bool overflow;
3861 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
3862 &overflow);
3863 /* If the multiplication overflowed we can't do a meaningful
3864 adjustment. Likewise if the result doesn't fit in the type
3865 of the induction variable. For a signed type we have to
3866 check whether the result has the expected signedness which
3867 is that of the step as number of iterations is unsigned. */
3868 if (!overflow
3869 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
3870 && (sgn == UNSIGNED
3871 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
3873 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
3874 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
3875 TREE_TYPE (init), init, tem);
3876 /* Likewise if the addition did. */
3877 if (maxvr.type == VR_RANGE)
3879 tmin = maxvr.min;
3880 tmax = maxvr.max;
3886 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3888 min = tmin;
3889 max = tmax;
3891 /* For VARYING or UNDEFINED ranges, just about anything we get
3892 from scalar evolutions should be better. */
3894 if (dir == EV_DIR_DECREASES)
3895 max = init;
3896 else
3897 min = init;
3899 else if (vr->type == VR_RANGE)
3901 min = vr->min;
3902 max = vr->max;
3904 if (dir == EV_DIR_DECREASES)
3906 /* INIT is the maximum value. If INIT is lower than VR->MAX
3907 but no smaller than VR->MIN, set VR->MAX to INIT. */
3908 if (compare_values (init, max) == -1)
3909 max = init;
3911 /* According to the loop information, the variable does not
3912 overflow. If we think it does, probably because of an
3913 overflow due to arithmetic on a different INF value,
3914 reset now. */
3915 if (is_negative_overflow_infinity (min)
3916 || compare_values (min, tmin) == -1)
3917 min = tmin;
3920 else
3922 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3923 if (compare_values (init, min) == 1)
3924 min = init;
3926 if (is_positive_overflow_infinity (max)
3927 || compare_values (tmax, max) == -1)
3928 max = tmax;
3931 else
3932 return;
3934 /* If we just created an invalid range with the minimum
3935 greater than the maximum, we fail conservatively.
3936 This should happen only in unreachable
3937 parts of code, or for invalid programs. */
3938 if (compare_values (min, max) == 1
3939 || (is_negative_overflow_infinity (min)
3940 && is_positive_overflow_infinity (max)))
3941 return;
3943 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3947 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
3949 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
3950 all the values in the ranges.
3952 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
3954 - Return NULL_TREE if it is not always possible to determine the
3955 value of the comparison.
3957 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
3958 overflow infinity was used in the test. */
3961 static tree
3962 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
3963 bool *strict_overflow_p)
3965 /* VARYING or UNDEFINED ranges cannot be compared. */
3966 if (vr0->type == VR_VARYING
3967 || vr0->type == VR_UNDEFINED
3968 || vr1->type == VR_VARYING
3969 || vr1->type == VR_UNDEFINED)
3970 return NULL_TREE;
3972 /* Anti-ranges need to be handled separately. */
3973 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
3975 /* If both are anti-ranges, then we cannot compute any
3976 comparison. */
3977 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
3978 return NULL_TREE;
3980 /* These comparisons are never statically computable. */
3981 if (comp == GT_EXPR
3982 || comp == GE_EXPR
3983 || comp == LT_EXPR
3984 || comp == LE_EXPR)
3985 return NULL_TREE;
3987 /* Equality can be computed only between a range and an
3988 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
3989 if (vr0->type == VR_RANGE)
3991 /* To simplify processing, make VR0 the anti-range. */
3992 value_range_t *tmp = vr0;
3993 vr0 = vr1;
3994 vr1 = tmp;
3997 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
3999 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4000 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4001 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4003 return NULL_TREE;
4006 if (!usable_range_p (vr0, strict_overflow_p)
4007 || !usable_range_p (vr1, strict_overflow_p))
4008 return NULL_TREE;
4010 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4011 operands around and change the comparison code. */
4012 if (comp == GT_EXPR || comp == GE_EXPR)
4014 value_range_t *tmp;
4015 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4016 tmp = vr0;
4017 vr0 = vr1;
4018 vr1 = tmp;
4021 if (comp == EQ_EXPR)
4023 /* Equality may only be computed if both ranges represent
4024 exactly one value. */
4025 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4026 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4028 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4029 strict_overflow_p);
4030 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4031 strict_overflow_p);
4032 if (cmp_min == 0 && cmp_max == 0)
4033 return boolean_true_node;
4034 else if (cmp_min != -2 && cmp_max != -2)
4035 return boolean_false_node;
4037 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4038 else if (compare_values_warnv (vr0->min, vr1->max,
4039 strict_overflow_p) == 1
4040 || compare_values_warnv (vr1->min, vr0->max,
4041 strict_overflow_p) == 1)
4042 return boolean_false_node;
4044 return NULL_TREE;
4046 else if (comp == NE_EXPR)
4048 int cmp1, cmp2;
4050 /* If VR0 is completely to the left or completely to the right
4051 of VR1, they are always different. Notice that we need to
4052 make sure that both comparisons yield similar results to
4053 avoid comparing values that cannot be compared at
4054 compile-time. */
4055 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4056 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4057 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4058 return boolean_true_node;
4060 /* If VR0 and VR1 represent a single value and are identical,
4061 return false. */
4062 else if (compare_values_warnv (vr0->min, vr0->max,
4063 strict_overflow_p) == 0
4064 && compare_values_warnv (vr1->min, vr1->max,
4065 strict_overflow_p) == 0
4066 && compare_values_warnv (vr0->min, vr1->min,
4067 strict_overflow_p) == 0
4068 && compare_values_warnv (vr0->max, vr1->max,
4069 strict_overflow_p) == 0)
4070 return boolean_false_node;
4072 /* Otherwise, they may or may not be different. */
4073 else
4074 return NULL_TREE;
4076 else if (comp == LT_EXPR || comp == LE_EXPR)
4078 int tst;
4080 /* If VR0 is to the left of VR1, return true. */
4081 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4082 if ((comp == LT_EXPR && tst == -1)
4083 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4085 if (overflow_infinity_range_p (vr0)
4086 || overflow_infinity_range_p (vr1))
4087 *strict_overflow_p = true;
4088 return boolean_true_node;
4091 /* If VR0 is to the right of VR1, return false. */
4092 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4093 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4094 || (comp == LE_EXPR && tst == 1))
4096 if (overflow_infinity_range_p (vr0)
4097 || overflow_infinity_range_p (vr1))
4098 *strict_overflow_p = true;
4099 return boolean_false_node;
4102 /* Otherwise, we don't know. */
4103 return NULL_TREE;
4106 gcc_unreachable ();
4110 /* Given a value range VR, a value VAL and a comparison code COMP, return
4111 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4112 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4113 always returns false. Return NULL_TREE if it is not always
4114 possible to determine the value of the comparison. Also set
4115 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4116 infinity was used in the test. */
4118 static tree
4119 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4120 bool *strict_overflow_p)
4122 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4123 return NULL_TREE;
4125 /* Anti-ranges need to be handled separately. */
4126 if (vr->type == VR_ANTI_RANGE)
4128 /* For anti-ranges, the only predicates that we can compute at
4129 compile time are equality and inequality. */
4130 if (comp == GT_EXPR
4131 || comp == GE_EXPR
4132 || comp == LT_EXPR
4133 || comp == LE_EXPR)
4134 return NULL_TREE;
4136 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4137 if (value_inside_range (val, vr->min, vr->max) == 1)
4138 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4140 return NULL_TREE;
4143 if (!usable_range_p (vr, strict_overflow_p))
4144 return NULL_TREE;
4146 if (comp == EQ_EXPR)
4148 /* EQ_EXPR may only be computed if VR represents exactly
4149 one value. */
4150 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4152 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4153 if (cmp == 0)
4154 return boolean_true_node;
4155 else if (cmp == -1 || cmp == 1 || cmp == 2)
4156 return boolean_false_node;
4158 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4159 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4160 return boolean_false_node;
4162 return NULL_TREE;
4164 else if (comp == NE_EXPR)
4166 /* If VAL is not inside VR, then they are always different. */
4167 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4168 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4169 return boolean_true_node;
4171 /* If VR represents exactly one value equal to VAL, then return
4172 false. */
4173 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4174 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4175 return boolean_false_node;
4177 /* Otherwise, they may or may not be different. */
4178 return NULL_TREE;
4180 else if (comp == LT_EXPR || comp == LE_EXPR)
4182 int tst;
4184 /* If VR is to the left of VAL, return true. */
4185 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4186 if ((comp == LT_EXPR && tst == -1)
4187 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4189 if (overflow_infinity_range_p (vr))
4190 *strict_overflow_p = true;
4191 return boolean_true_node;
4194 /* If VR is to the right of VAL, return false. */
4195 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4196 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4197 || (comp == LE_EXPR && tst == 1))
4199 if (overflow_infinity_range_p (vr))
4200 *strict_overflow_p = true;
4201 return boolean_false_node;
4204 /* Otherwise, we don't know. */
4205 return NULL_TREE;
4207 else if (comp == GT_EXPR || comp == GE_EXPR)
4209 int tst;
4211 /* If VR is to the right of VAL, return true. */
4212 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4213 if ((comp == GT_EXPR && tst == 1)
4214 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4216 if (overflow_infinity_range_p (vr))
4217 *strict_overflow_p = true;
4218 return boolean_true_node;
4221 /* If VR is to the left of VAL, return false. */
4222 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4223 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4224 || (comp == GE_EXPR && tst == -1))
4226 if (overflow_infinity_range_p (vr))
4227 *strict_overflow_p = true;
4228 return boolean_false_node;
4231 /* Otherwise, we don't know. */
4232 return NULL_TREE;
4235 gcc_unreachable ();
4239 /* Debugging dumps. */
4241 void dump_value_range (FILE *, value_range_t *);
4242 void debug_value_range (value_range_t *);
4243 void dump_all_value_ranges (FILE *);
4244 void debug_all_value_ranges (void);
4245 void dump_vr_equiv (FILE *, bitmap);
4246 void debug_vr_equiv (bitmap);
4249 /* Dump value range VR to FILE. */
4251 void
4252 dump_value_range (FILE *file, value_range_t *vr)
4254 if (vr == NULL)
4255 fprintf (file, "[]");
4256 else if (vr->type == VR_UNDEFINED)
4257 fprintf (file, "UNDEFINED");
4258 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4260 tree type = TREE_TYPE (vr->min);
4262 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4264 if (is_negative_overflow_infinity (vr->min))
4265 fprintf (file, "-INF(OVF)");
4266 else if (INTEGRAL_TYPE_P (type)
4267 && !TYPE_UNSIGNED (type)
4268 && vrp_val_is_min (vr->min))
4269 fprintf (file, "-INF");
4270 else
4271 print_generic_expr (file, vr->min, 0);
4273 fprintf (file, ", ");
4275 if (is_positive_overflow_infinity (vr->max))
4276 fprintf (file, "+INF(OVF)");
4277 else if (INTEGRAL_TYPE_P (type)
4278 && vrp_val_is_max (vr->max))
4279 fprintf (file, "+INF");
4280 else
4281 print_generic_expr (file, vr->max, 0);
4283 fprintf (file, "]");
4285 if (vr->equiv)
4287 bitmap_iterator bi;
4288 unsigned i, c = 0;
4290 fprintf (file, " EQUIVALENCES: { ");
4292 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4294 print_generic_expr (file, ssa_name (i), 0);
4295 fprintf (file, " ");
4296 c++;
4299 fprintf (file, "} (%u elements)", c);
4302 else if (vr->type == VR_VARYING)
4303 fprintf (file, "VARYING");
4304 else
4305 fprintf (file, "INVALID RANGE");
4309 /* Dump value range VR to stderr. */
4311 DEBUG_FUNCTION void
4312 debug_value_range (value_range_t *vr)
4314 dump_value_range (stderr, vr);
4315 fprintf (stderr, "\n");
4319 /* Dump value ranges of all SSA_NAMEs to FILE. */
4321 void
4322 dump_all_value_ranges (FILE *file)
4324 size_t i;
4326 for (i = 0; i < num_vr_values; i++)
4328 if (vr_value[i])
4330 print_generic_expr (file, ssa_name (i), 0);
4331 fprintf (file, ": ");
4332 dump_value_range (file, vr_value[i]);
4333 fprintf (file, "\n");
4337 fprintf (file, "\n");
4341 /* Dump all value ranges to stderr. */
4343 DEBUG_FUNCTION void
4344 debug_all_value_ranges (void)
4346 dump_all_value_ranges (stderr);
4350 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4351 create a new SSA name N and return the assertion assignment
4352 'N = ASSERT_EXPR <V, V OP W>'. */
4354 static gimple
4355 build_assert_expr_for (tree cond, tree v)
4357 tree a;
4358 gimple assertion;
4360 gcc_assert (TREE_CODE (v) == SSA_NAME
4361 && COMPARISON_CLASS_P (cond));
4363 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4364 assertion = gimple_build_assign (NULL_TREE, a);
4366 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4367 operand of the ASSERT_EXPR. Create it so the new name and the old one
4368 are registered in the replacement table so that we can fix the SSA web
4369 after adding all the ASSERT_EXPRs. */
4370 create_new_def_for (v, assertion, NULL);
4372 return assertion;
4376 /* Return false if EXPR is a predicate expression involving floating
4377 point values. */
4379 static inline bool
4380 fp_predicate (gimple stmt)
4382 GIMPLE_CHECK (stmt, GIMPLE_COND);
4384 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4387 /* If the range of values taken by OP can be inferred after STMT executes,
4388 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4389 describes the inferred range. Return true if a range could be
4390 inferred. */
4392 static bool
4393 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4395 *val_p = NULL_TREE;
4396 *comp_code_p = ERROR_MARK;
4398 /* Do not attempt to infer anything in names that flow through
4399 abnormal edges. */
4400 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4401 return false;
4403 /* Similarly, don't infer anything from statements that may throw
4404 exceptions. ??? Relax this requirement? */
4405 if (stmt_could_throw_p (stmt))
4406 return false;
4408 /* If STMT is the last statement of a basic block with no normal
4409 successors, there is no point inferring anything about any of its
4410 operands. We would not be able to find a proper insertion point
4411 for the assertion, anyway. */
4412 if (stmt_ends_bb_p (stmt))
4414 edge_iterator ei;
4415 edge e;
4417 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4418 if (!(e->flags & EDGE_ABNORMAL))
4419 break;
4420 if (e == NULL)
4421 return false;
4424 if (infer_nonnull_range (stmt, op, true, true))
4426 *val_p = build_int_cst (TREE_TYPE (op), 0);
4427 *comp_code_p = NE_EXPR;
4428 return true;
4431 return false;
4435 void dump_asserts_for (FILE *, tree);
4436 void debug_asserts_for (tree);
4437 void dump_all_asserts (FILE *);
4438 void debug_all_asserts (void);
4440 /* Dump all the registered assertions for NAME to FILE. */
4442 void
4443 dump_asserts_for (FILE *file, tree name)
4445 assert_locus_t loc;
4447 fprintf (file, "Assertions to be inserted for ");
4448 print_generic_expr (file, name, 0);
4449 fprintf (file, "\n");
4451 loc = asserts_for[SSA_NAME_VERSION (name)];
4452 while (loc)
4454 fprintf (file, "\t");
4455 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4456 fprintf (file, "\n\tBB #%d", loc->bb->index);
4457 if (loc->e)
4459 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4460 loc->e->dest->index);
4461 dump_edge_info (file, loc->e, dump_flags, 0);
4463 fprintf (file, "\n\tPREDICATE: ");
4464 print_generic_expr (file, name, 0);
4465 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4466 print_generic_expr (file, loc->val, 0);
4467 fprintf (file, "\n\n");
4468 loc = loc->next;
4471 fprintf (file, "\n");
4475 /* Dump all the registered assertions for NAME to stderr. */
4477 DEBUG_FUNCTION void
4478 debug_asserts_for (tree name)
4480 dump_asserts_for (stderr, name);
4484 /* Dump all the registered assertions for all the names to FILE. */
4486 void
4487 dump_all_asserts (FILE *file)
4489 unsigned i;
4490 bitmap_iterator bi;
4492 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4493 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4494 dump_asserts_for (file, ssa_name (i));
4495 fprintf (file, "\n");
4499 /* Dump all the registered assertions for all the names to stderr. */
4501 DEBUG_FUNCTION void
4502 debug_all_asserts (void)
4504 dump_all_asserts (stderr);
4508 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4509 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4510 E->DEST, then register this location as a possible insertion point
4511 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4513 BB, E and SI provide the exact insertion point for the new
4514 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4515 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4516 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4517 must not be NULL. */
4519 static void
4520 register_new_assert_for (tree name, tree expr,
4521 enum tree_code comp_code,
4522 tree val,
4523 basic_block bb,
4524 edge e,
4525 gimple_stmt_iterator si)
4527 assert_locus_t n, loc, last_loc;
4528 basic_block dest_bb;
4530 gcc_checking_assert (bb == NULL || e == NULL);
4532 if (e == NULL)
4533 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4534 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4536 /* Never build an assert comparing against an integer constant with
4537 TREE_OVERFLOW set. This confuses our undefined overflow warning
4538 machinery. */
4539 if (TREE_OVERFLOW_P (val))
4540 val = drop_tree_overflow (val);
4542 /* The new assertion A will be inserted at BB or E. We need to
4543 determine if the new location is dominated by a previously
4544 registered location for A. If we are doing an edge insertion,
4545 assume that A will be inserted at E->DEST. Note that this is not
4546 necessarily true.
4548 If E is a critical edge, it will be split. But even if E is
4549 split, the new block will dominate the same set of blocks that
4550 E->DEST dominates.
4552 The reverse, however, is not true, blocks dominated by E->DEST
4553 will not be dominated by the new block created to split E. So,
4554 if the insertion location is on a critical edge, we will not use
4555 the new location to move another assertion previously registered
4556 at a block dominated by E->DEST. */
4557 dest_bb = (bb) ? bb : e->dest;
4559 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4560 VAL at a block dominating DEST_BB, then we don't need to insert a new
4561 one. Similarly, if the same assertion already exists at a block
4562 dominated by DEST_BB and the new location is not on a critical
4563 edge, then update the existing location for the assertion (i.e.,
4564 move the assertion up in the dominance tree).
4566 Note, this is implemented as a simple linked list because there
4567 should not be more than a handful of assertions registered per
4568 name. If this becomes a performance problem, a table hashed by
4569 COMP_CODE and VAL could be implemented. */
4570 loc = asserts_for[SSA_NAME_VERSION (name)];
4571 last_loc = loc;
4572 while (loc)
4574 if (loc->comp_code == comp_code
4575 && (loc->val == val
4576 || operand_equal_p (loc->val, val, 0))
4577 && (loc->expr == expr
4578 || operand_equal_p (loc->expr, expr, 0)))
4580 /* If E is not a critical edge and DEST_BB
4581 dominates the existing location for the assertion, move
4582 the assertion up in the dominance tree by updating its
4583 location information. */
4584 if ((e == NULL || !EDGE_CRITICAL_P (e))
4585 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4587 loc->bb = dest_bb;
4588 loc->e = e;
4589 loc->si = si;
4590 return;
4594 /* Update the last node of the list and move to the next one. */
4595 last_loc = loc;
4596 loc = loc->next;
4599 /* If we didn't find an assertion already registered for
4600 NAME COMP_CODE VAL, add a new one at the end of the list of
4601 assertions associated with NAME. */
4602 n = XNEW (struct assert_locus_d);
4603 n->bb = dest_bb;
4604 n->e = e;
4605 n->si = si;
4606 n->comp_code = comp_code;
4607 n->val = val;
4608 n->expr = expr;
4609 n->next = NULL;
4611 if (last_loc)
4612 last_loc->next = n;
4613 else
4614 asserts_for[SSA_NAME_VERSION (name)] = n;
4616 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4619 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4620 Extract a suitable test code and value and store them into *CODE_P and
4621 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4623 If no extraction was possible, return FALSE, otherwise return TRUE.
4625 If INVERT is true, then we invert the result stored into *CODE_P. */
4627 static bool
4628 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4629 tree cond_op0, tree cond_op1,
4630 bool invert, enum tree_code *code_p,
4631 tree *val_p)
4633 enum tree_code comp_code;
4634 tree val;
4636 /* Otherwise, we have a comparison of the form NAME COMP VAL
4637 or VAL COMP NAME. */
4638 if (name == cond_op1)
4640 /* If the predicate is of the form VAL COMP NAME, flip
4641 COMP around because we need to register NAME as the
4642 first operand in the predicate. */
4643 comp_code = swap_tree_comparison (cond_code);
4644 val = cond_op0;
4646 else
4648 /* The comparison is of the form NAME COMP VAL, so the
4649 comparison code remains unchanged. */
4650 comp_code = cond_code;
4651 val = cond_op1;
4654 /* Invert the comparison code as necessary. */
4655 if (invert)
4656 comp_code = invert_tree_comparison (comp_code, 0);
4658 /* VRP does not handle float types. */
4659 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4660 return false;
4662 /* Do not register always-false predicates.
4663 FIXME: this works around a limitation in fold() when dealing with
4664 enumerations. Given 'enum { N1, N2 } x;', fold will not
4665 fold 'if (x > N2)' to 'if (0)'. */
4666 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4667 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4669 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4670 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4672 if (comp_code == GT_EXPR
4673 && (!max
4674 || compare_values (val, max) == 0))
4675 return false;
4677 if (comp_code == LT_EXPR
4678 && (!min
4679 || compare_values (val, min) == 0))
4680 return false;
4682 *code_p = comp_code;
4683 *val_p = val;
4684 return true;
4687 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4688 (otherwise return VAL). VAL and MASK must be zero-extended for
4689 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4690 (to transform signed values into unsigned) and at the end xor
4691 SGNBIT back. */
4693 static wide_int
4694 masked_increment (const wide_int &val_in, const wide_int &mask,
4695 const wide_int &sgnbit, unsigned int prec)
4697 wide_int bit = wi::one (prec), res;
4698 unsigned int i;
4700 wide_int val = val_in ^ sgnbit;
4701 for (i = 0; i < prec; i++, bit += bit)
4703 res = mask;
4704 if ((res & bit) == 0)
4705 continue;
4706 res = bit - 1;
4707 res = (val + bit).and_not (res);
4708 res &= mask;
4709 if (wi::gtu_p (res, val))
4710 return res ^ sgnbit;
4712 return val ^ sgnbit;
4715 /* Try to register an edge assertion for SSA name NAME on edge E for
4716 the condition COND contributing to the conditional jump pointed to by BSI.
4717 Invert the condition COND if INVERT is true.
4718 Return true if an assertion for NAME could be registered. */
4720 static bool
4721 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4722 enum tree_code cond_code,
4723 tree cond_op0, tree cond_op1, bool invert)
4725 tree val;
4726 enum tree_code comp_code;
4727 bool retval = false;
4729 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4730 cond_op0,
4731 cond_op1,
4732 invert, &comp_code, &val))
4733 return false;
4735 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4736 reachable from E. */
4737 if (live_on_edge (e, name)
4738 && !has_single_use (name))
4740 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
4741 retval = true;
4744 /* In the case of NAME <= CST and NAME being defined as
4745 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4746 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4747 This catches range and anti-range tests. */
4748 if ((comp_code == LE_EXPR
4749 || comp_code == GT_EXPR)
4750 && TREE_CODE (val) == INTEGER_CST
4751 && TYPE_UNSIGNED (TREE_TYPE (val)))
4753 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4754 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
4756 /* Extract CST2 from the (optional) addition. */
4757 if (is_gimple_assign (def_stmt)
4758 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
4760 name2 = gimple_assign_rhs1 (def_stmt);
4761 cst2 = gimple_assign_rhs2 (def_stmt);
4762 if (TREE_CODE (name2) == SSA_NAME
4763 && TREE_CODE (cst2) == INTEGER_CST)
4764 def_stmt = SSA_NAME_DEF_STMT (name2);
4767 /* Extract NAME2 from the (optional) sign-changing cast. */
4768 if (gimple_assign_cast_p (def_stmt))
4770 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4771 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4772 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
4773 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
4774 name3 = gimple_assign_rhs1 (def_stmt);
4777 /* If name3 is used later, create an ASSERT_EXPR for it. */
4778 if (name3 != NULL_TREE
4779 && TREE_CODE (name3) == SSA_NAME
4780 && (cst2 == NULL_TREE
4781 || TREE_CODE (cst2) == INTEGER_CST)
4782 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
4783 && live_on_edge (e, name3)
4784 && !has_single_use (name3))
4786 tree tmp;
4788 /* Build an expression for the range test. */
4789 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
4790 if (cst2 != NULL_TREE)
4791 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4793 if (dump_file)
4795 fprintf (dump_file, "Adding assert for ");
4796 print_generic_expr (dump_file, name3, 0);
4797 fprintf (dump_file, " from ");
4798 print_generic_expr (dump_file, tmp, 0);
4799 fprintf (dump_file, "\n");
4802 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
4804 retval = true;
4807 /* If name2 is used later, create an ASSERT_EXPR for it. */
4808 if (name2 != NULL_TREE
4809 && TREE_CODE (name2) == SSA_NAME
4810 && TREE_CODE (cst2) == INTEGER_CST
4811 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4812 && live_on_edge (e, name2)
4813 && !has_single_use (name2))
4815 tree tmp;
4817 /* Build an expression for the range test. */
4818 tmp = name2;
4819 if (TREE_TYPE (name) != TREE_TYPE (name2))
4820 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
4821 if (cst2 != NULL_TREE)
4822 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4824 if (dump_file)
4826 fprintf (dump_file, "Adding assert for ");
4827 print_generic_expr (dump_file, name2, 0);
4828 fprintf (dump_file, " from ");
4829 print_generic_expr (dump_file, tmp, 0);
4830 fprintf (dump_file, "\n");
4833 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
4835 retval = true;
4839 /* In the case of post-in/decrement tests like if (i++) ... and uses
4840 of the in/decremented value on the edge the extra name we want to
4841 assert for is not on the def chain of the name compared. Instead
4842 it is in the set of use stmts. */
4843 if ((comp_code == NE_EXPR
4844 || comp_code == EQ_EXPR)
4845 && TREE_CODE (val) == INTEGER_CST)
4847 imm_use_iterator ui;
4848 gimple use_stmt;
4849 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
4851 /* Cut off to use-stmts that are in the predecessor. */
4852 if (gimple_bb (use_stmt) != e->src)
4853 continue;
4855 if (!is_gimple_assign (use_stmt))
4856 continue;
4858 enum tree_code code = gimple_assign_rhs_code (use_stmt);
4859 if (code != PLUS_EXPR
4860 && code != MINUS_EXPR)
4861 continue;
4863 tree cst = gimple_assign_rhs2 (use_stmt);
4864 if (TREE_CODE (cst) != INTEGER_CST)
4865 continue;
4867 tree name2 = gimple_assign_lhs (use_stmt);
4868 if (live_on_edge (e, name2))
4870 cst = int_const_binop (code, val, cst);
4871 register_new_assert_for (name2, name2, comp_code, cst,
4872 NULL, e, bsi);
4873 retval = true;
4878 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
4879 && TREE_CODE (val) == INTEGER_CST)
4881 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4882 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
4883 tree val2 = NULL_TREE;
4884 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
4885 wide_int mask = wi::zero (prec);
4886 unsigned int nprec = prec;
4887 enum tree_code rhs_code = ERROR_MARK;
4889 if (is_gimple_assign (def_stmt))
4890 rhs_code = gimple_assign_rhs_code (def_stmt);
4892 /* Add asserts for NAME cmp CST and NAME being defined
4893 as NAME = (int) NAME2. */
4894 if (!TYPE_UNSIGNED (TREE_TYPE (val))
4895 && (comp_code == LE_EXPR || comp_code == LT_EXPR
4896 || comp_code == GT_EXPR || comp_code == GE_EXPR)
4897 && gimple_assign_cast_p (def_stmt))
4899 name2 = gimple_assign_rhs1 (def_stmt);
4900 if (CONVERT_EXPR_CODE_P (rhs_code)
4901 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4902 && TYPE_UNSIGNED (TREE_TYPE (name2))
4903 && prec == TYPE_PRECISION (TREE_TYPE (name2))
4904 && (comp_code == LE_EXPR || comp_code == GT_EXPR
4905 || !tree_int_cst_equal (val,
4906 TYPE_MIN_VALUE (TREE_TYPE (val))))
4907 && live_on_edge (e, name2)
4908 && !has_single_use (name2))
4910 tree tmp, cst;
4911 enum tree_code new_comp_code = comp_code;
4913 cst = fold_convert (TREE_TYPE (name2),
4914 TYPE_MIN_VALUE (TREE_TYPE (val)));
4915 /* Build an expression for the range test. */
4916 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
4917 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
4918 fold_convert (TREE_TYPE (name2), val));
4919 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4921 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
4922 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
4923 build_int_cst (TREE_TYPE (name2), 1));
4926 if (dump_file)
4928 fprintf (dump_file, "Adding assert for ");
4929 print_generic_expr (dump_file, name2, 0);
4930 fprintf (dump_file, " from ");
4931 print_generic_expr (dump_file, tmp, 0);
4932 fprintf (dump_file, "\n");
4935 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
4936 e, bsi);
4938 retval = true;
4942 /* Add asserts for NAME cmp CST and NAME being defined as
4943 NAME = NAME2 >> CST2.
4945 Extract CST2 from the right shift. */
4946 if (rhs_code == RSHIFT_EXPR)
4948 name2 = gimple_assign_rhs1 (def_stmt);
4949 cst2 = gimple_assign_rhs2 (def_stmt);
4950 if (TREE_CODE (name2) == SSA_NAME
4951 && tree_fits_uhwi_p (cst2)
4952 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4953 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
4954 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
4955 && live_on_edge (e, name2)
4956 && !has_single_use (name2))
4958 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
4959 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
4962 if (val2 != NULL_TREE
4963 && TREE_CODE (val2) == INTEGER_CST
4964 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
4965 TREE_TYPE (val),
4966 val2, cst2), val))
4968 enum tree_code new_comp_code = comp_code;
4969 tree tmp, new_val;
4971 tmp = name2;
4972 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
4974 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
4976 tree type = build_nonstandard_integer_type (prec, 1);
4977 tmp = build1 (NOP_EXPR, type, name2);
4978 val2 = fold_convert (type, val2);
4980 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
4981 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
4982 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
4984 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4986 wide_int minval
4987 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
4988 new_val = val2;
4989 if (minval == new_val)
4990 new_val = NULL_TREE;
4992 else
4994 wide_int maxval
4995 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
4996 mask |= val2;
4997 if (mask == maxval)
4998 new_val = NULL_TREE;
4999 else
5000 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5003 if (new_val)
5005 if (dump_file)
5007 fprintf (dump_file, "Adding assert for ");
5008 print_generic_expr (dump_file, name2, 0);
5009 fprintf (dump_file, " from ");
5010 print_generic_expr (dump_file, tmp, 0);
5011 fprintf (dump_file, "\n");
5014 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5015 NULL, e, bsi);
5016 retval = true;
5020 /* Add asserts for NAME cmp CST and NAME being defined as
5021 NAME = NAME2 & CST2.
5023 Extract CST2 from the and.
5025 Also handle
5026 NAME = (unsigned) NAME2;
5027 casts where NAME's type is unsigned and has smaller precision
5028 than NAME2's type as if it was NAME = NAME2 & MASK. */
5029 names[0] = NULL_TREE;
5030 names[1] = NULL_TREE;
5031 cst2 = NULL_TREE;
5032 if (rhs_code == BIT_AND_EXPR
5033 || (CONVERT_EXPR_CODE_P (rhs_code)
5034 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5035 && TYPE_UNSIGNED (TREE_TYPE (val))
5036 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5037 > prec
5038 && !retval))
5040 name2 = gimple_assign_rhs1 (def_stmt);
5041 if (rhs_code == BIT_AND_EXPR)
5042 cst2 = gimple_assign_rhs2 (def_stmt);
5043 else
5045 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5046 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5048 if (TREE_CODE (name2) == SSA_NAME
5049 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5050 && TREE_CODE (cst2) == INTEGER_CST
5051 && !integer_zerop (cst2)
5052 && (nprec > 1
5053 || TYPE_UNSIGNED (TREE_TYPE (val))))
5055 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5056 if (gimple_assign_cast_p (def_stmt2))
5058 names[1] = gimple_assign_rhs1 (def_stmt2);
5059 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5060 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5061 || (TYPE_PRECISION (TREE_TYPE (name2))
5062 != TYPE_PRECISION (TREE_TYPE (names[1])))
5063 || !live_on_edge (e, names[1])
5064 || has_single_use (names[1]))
5065 names[1] = NULL_TREE;
5067 if (live_on_edge (e, name2)
5068 && !has_single_use (name2))
5069 names[0] = name2;
5072 if (names[0] || names[1])
5074 wide_int minv, maxv, valv, cst2v;
5075 wide_int tem, sgnbit;
5076 bool valid_p = false, valn, cst2n;
5077 enum tree_code ccode = comp_code;
5079 valv = wide_int::from (val, nprec, UNSIGNED);
5080 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5081 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5082 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5083 /* If CST2 doesn't have most significant bit set,
5084 but VAL is negative, we have comparison like
5085 if ((x & 0x123) > -4) (always true). Just give up. */
5086 if (!cst2n && valn)
5087 ccode = ERROR_MARK;
5088 if (cst2n)
5089 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5090 else
5091 sgnbit = wi::zero (nprec);
5092 minv = valv & cst2v;
5093 switch (ccode)
5095 case EQ_EXPR:
5096 /* Minimum unsigned value for equality is VAL & CST2
5097 (should be equal to VAL, otherwise we probably should
5098 have folded the comparison into false) and
5099 maximum unsigned value is VAL | ~CST2. */
5100 maxv = valv | ~cst2v;
5101 valid_p = true;
5102 break;
5104 case NE_EXPR:
5105 tem = valv | ~cst2v;
5106 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5107 if (valv == 0)
5109 cst2n = false;
5110 sgnbit = wi::zero (nprec);
5111 goto gt_expr;
5113 /* If (VAL | ~CST2) is all ones, handle it as
5114 (X & CST2) < VAL. */
5115 if (tem == -1)
5117 cst2n = false;
5118 valn = false;
5119 sgnbit = wi::zero (nprec);
5120 goto lt_expr;
5122 if (!cst2n && wi::neg_p (cst2v))
5123 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5124 if (sgnbit != 0)
5126 if (valv == sgnbit)
5128 cst2n = true;
5129 valn = true;
5130 goto gt_expr;
5132 if (tem == wi::mask (nprec - 1, false, nprec))
5134 cst2n = true;
5135 goto lt_expr;
5137 if (!cst2n)
5138 sgnbit = wi::zero (nprec);
5140 break;
5142 case GE_EXPR:
5143 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5144 is VAL and maximum unsigned value is ~0. For signed
5145 comparison, if CST2 doesn't have most significant bit
5146 set, handle it similarly. If CST2 has MSB set,
5147 the minimum is the same, and maximum is ~0U/2. */
5148 if (minv != valv)
5150 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5151 VAL. */
5152 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5153 if (minv == valv)
5154 break;
5156 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5157 valid_p = true;
5158 break;
5160 case GT_EXPR:
5161 gt_expr:
5162 /* Find out smallest MINV where MINV > VAL
5163 && (MINV & CST2) == MINV, if any. If VAL is signed and
5164 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5165 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5166 if (minv == valv)
5167 break;
5168 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5169 valid_p = true;
5170 break;
5172 case LE_EXPR:
5173 /* Minimum unsigned value for <= is 0 and maximum
5174 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5175 Otherwise, find smallest VAL2 where VAL2 > VAL
5176 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5177 as maximum.
5178 For signed comparison, if CST2 doesn't have most
5179 significant bit set, handle it similarly. If CST2 has
5180 MSB set, the maximum is the same and minimum is INT_MIN. */
5181 if (minv == valv)
5182 maxv = valv;
5183 else
5185 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5186 if (maxv == valv)
5187 break;
5188 maxv -= 1;
5190 maxv |= ~cst2v;
5191 minv = sgnbit;
5192 valid_p = true;
5193 break;
5195 case LT_EXPR:
5196 lt_expr:
5197 /* Minimum unsigned value for < is 0 and maximum
5198 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5199 Otherwise, find smallest VAL2 where VAL2 > VAL
5200 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5201 as maximum.
5202 For signed comparison, if CST2 doesn't have most
5203 significant bit set, handle it similarly. If CST2 has
5204 MSB set, the maximum is the same and minimum is INT_MIN. */
5205 if (minv == valv)
5207 if (valv == sgnbit)
5208 break;
5209 maxv = valv;
5211 else
5213 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5214 if (maxv == valv)
5215 break;
5217 maxv -= 1;
5218 maxv |= ~cst2v;
5219 minv = sgnbit;
5220 valid_p = true;
5221 break;
5223 default:
5224 break;
5226 if (valid_p
5227 && (maxv - minv) != -1)
5229 tree tmp, new_val, type;
5230 int i;
5232 for (i = 0; i < 2; i++)
5233 if (names[i])
5235 wide_int maxv2 = maxv;
5236 tmp = names[i];
5237 type = TREE_TYPE (names[i]);
5238 if (!TYPE_UNSIGNED (type))
5240 type = build_nonstandard_integer_type (nprec, 1);
5241 tmp = build1 (NOP_EXPR, type, names[i]);
5243 if (minv != 0)
5245 tmp = build2 (PLUS_EXPR, type, tmp,
5246 wide_int_to_tree (type, -minv));
5247 maxv2 = maxv - minv;
5249 new_val = wide_int_to_tree (type, maxv2);
5251 if (dump_file)
5253 fprintf (dump_file, "Adding assert for ");
5254 print_generic_expr (dump_file, names[i], 0);
5255 fprintf (dump_file, " from ");
5256 print_generic_expr (dump_file, tmp, 0);
5257 fprintf (dump_file, "\n");
5260 register_new_assert_for (names[i], tmp, LE_EXPR,
5261 new_val, NULL, e, bsi);
5262 retval = true;
5268 return retval;
5271 /* OP is an operand of a truth value expression which is known to have
5272 a particular value. Register any asserts for OP and for any
5273 operands in OP's defining statement.
5275 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5276 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5278 static bool
5279 register_edge_assert_for_1 (tree op, enum tree_code code,
5280 edge e, gimple_stmt_iterator bsi)
5282 bool retval = false;
5283 gimple op_def;
5284 tree val;
5285 enum tree_code rhs_code;
5287 /* We only care about SSA_NAMEs. */
5288 if (TREE_CODE (op) != SSA_NAME)
5289 return false;
5291 /* We know that OP will have a zero or nonzero value. If OP is used
5292 more than once go ahead and register an assert for OP. */
5293 if (live_on_edge (e, op)
5294 && !has_single_use (op))
5296 val = build_int_cst (TREE_TYPE (op), 0);
5297 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5298 retval = true;
5301 /* Now look at how OP is set. If it's set from a comparison,
5302 a truth operation or some bit operations, then we may be able
5303 to register information about the operands of that assignment. */
5304 op_def = SSA_NAME_DEF_STMT (op);
5305 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5306 return retval;
5308 rhs_code = gimple_assign_rhs_code (op_def);
5310 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5312 bool invert = (code == EQ_EXPR ? true : false);
5313 tree op0 = gimple_assign_rhs1 (op_def);
5314 tree op1 = gimple_assign_rhs2 (op_def);
5316 if (TREE_CODE (op0) == SSA_NAME)
5317 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5318 invert);
5319 if (TREE_CODE (op1) == SSA_NAME)
5320 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5321 invert);
5323 else if ((code == NE_EXPR
5324 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5325 || (code == EQ_EXPR
5326 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5328 /* Recurse on each operand. */
5329 tree op0 = gimple_assign_rhs1 (op_def);
5330 tree op1 = gimple_assign_rhs2 (op_def);
5331 if (TREE_CODE (op0) == SSA_NAME
5332 && has_single_use (op0))
5333 retval |= register_edge_assert_for_1 (op0, code, e, bsi);
5334 if (TREE_CODE (op1) == SSA_NAME
5335 && has_single_use (op1))
5336 retval |= register_edge_assert_for_1 (op1, code, e, bsi);
5338 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5339 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5341 /* Recurse, flipping CODE. */
5342 code = invert_tree_comparison (code, false);
5343 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5344 code, e, bsi);
5346 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5348 /* Recurse through the copy. */
5349 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5350 code, e, bsi);
5352 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5354 /* Recurse through the type conversion, unless it is a narrowing
5355 conversion or conversion from non-integral type. */
5356 tree rhs = gimple_assign_rhs1 (op_def);
5357 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5358 && (TYPE_PRECISION (TREE_TYPE (rhs))
5359 <= TYPE_PRECISION (TREE_TYPE (op))))
5360 retval |= register_edge_assert_for_1 (rhs, code, e, bsi);
5363 return retval;
5366 /* Try to register an edge assertion for SSA name NAME on edge E for
5367 the condition COND contributing to the conditional jump pointed to by SI.
5368 Return true if an assertion for NAME could be registered. */
5370 static bool
5371 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5372 enum tree_code cond_code, tree cond_op0,
5373 tree cond_op1)
5375 tree val;
5376 enum tree_code comp_code;
5377 bool retval = false;
5378 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5380 /* Do not attempt to infer anything in names that flow through
5381 abnormal edges. */
5382 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5383 return false;
5385 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5386 cond_op0, cond_op1,
5387 is_else_edge,
5388 &comp_code, &val))
5389 return false;
5391 /* Register ASSERT_EXPRs for name. */
5392 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5393 cond_op1, is_else_edge);
5396 /* If COND is effectively an equality test of an SSA_NAME against
5397 the value zero or one, then we may be able to assert values
5398 for SSA_NAMEs which flow into COND. */
5400 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5401 statement of NAME we can assert both operands of the BIT_AND_EXPR
5402 have nonzero value. */
5403 if (((comp_code == EQ_EXPR && integer_onep (val))
5404 || (comp_code == NE_EXPR && integer_zerop (val))))
5406 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5408 if (is_gimple_assign (def_stmt)
5409 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5411 tree op0 = gimple_assign_rhs1 (def_stmt);
5412 tree op1 = gimple_assign_rhs2 (def_stmt);
5413 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5414 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5418 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5419 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5420 have zero value. */
5421 if (((comp_code == EQ_EXPR && integer_zerop (val))
5422 || (comp_code == NE_EXPR && integer_onep (val))))
5424 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5426 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5427 necessarily zero value, or if type-precision is one. */
5428 if (is_gimple_assign (def_stmt)
5429 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5430 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5431 || comp_code == EQ_EXPR)))
5433 tree op0 = gimple_assign_rhs1 (def_stmt);
5434 tree op1 = gimple_assign_rhs2 (def_stmt);
5435 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5436 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5440 return retval;
5444 /* Determine whether the outgoing edges of BB should receive an
5445 ASSERT_EXPR for each of the operands of BB's LAST statement.
5446 The last statement of BB must be a COND_EXPR.
5448 If any of the sub-graphs rooted at BB have an interesting use of
5449 the predicate operands, an assert location node is added to the
5450 list of assertions for the corresponding operands. */
5452 static bool
5453 find_conditional_asserts (basic_block bb, gimple last)
5455 bool need_assert;
5456 gimple_stmt_iterator bsi;
5457 tree op;
5458 edge_iterator ei;
5459 edge e;
5460 ssa_op_iter iter;
5462 need_assert = false;
5463 bsi = gsi_for_stmt (last);
5465 /* Look for uses of the operands in each of the sub-graphs
5466 rooted at BB. We need to check each of the outgoing edges
5467 separately, so that we know what kind of ASSERT_EXPR to
5468 insert. */
5469 FOR_EACH_EDGE (e, ei, bb->succs)
5471 if (e->dest == bb)
5472 continue;
5474 /* Register the necessary assertions for each operand in the
5475 conditional predicate. */
5476 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5478 need_assert |= register_edge_assert_for (op, e, bsi,
5479 gimple_cond_code (last),
5480 gimple_cond_lhs (last),
5481 gimple_cond_rhs (last));
5485 return need_assert;
5488 struct case_info
5490 tree expr;
5491 basic_block bb;
5494 /* Compare two case labels sorting first by the destination bb index
5495 and then by the case value. */
5497 static int
5498 compare_case_labels (const void *p1, const void *p2)
5500 const struct case_info *ci1 = (const struct case_info *) p1;
5501 const struct case_info *ci2 = (const struct case_info *) p2;
5502 int idx1 = ci1->bb->index;
5503 int idx2 = ci2->bb->index;
5505 if (idx1 < idx2)
5506 return -1;
5507 else if (idx1 == idx2)
5509 /* Make sure the default label is first in a group. */
5510 if (!CASE_LOW (ci1->expr))
5511 return -1;
5512 else if (!CASE_LOW (ci2->expr))
5513 return 1;
5514 else
5515 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5516 CASE_LOW (ci2->expr));
5518 else
5519 return 1;
5522 /* Determine whether the outgoing edges of BB should receive an
5523 ASSERT_EXPR for each of the operands of BB's LAST statement.
5524 The last statement of BB must be a SWITCH_EXPR.
5526 If any of the sub-graphs rooted at BB have an interesting use of
5527 the predicate operands, an assert location node is added to the
5528 list of assertions for the corresponding operands. */
5530 static bool
5531 find_switch_asserts (basic_block bb, gimple last)
5533 bool need_assert;
5534 gimple_stmt_iterator bsi;
5535 tree op;
5536 edge e;
5537 struct case_info *ci;
5538 size_t n = gimple_switch_num_labels (last);
5539 #if GCC_VERSION >= 4000
5540 unsigned int idx;
5541 #else
5542 /* Work around GCC 3.4 bug (PR 37086). */
5543 volatile unsigned int idx;
5544 #endif
5546 need_assert = false;
5547 bsi = gsi_for_stmt (last);
5548 op = gimple_switch_index (last);
5549 if (TREE_CODE (op) != SSA_NAME)
5550 return false;
5552 /* Build a vector of case labels sorted by destination label. */
5553 ci = XNEWVEC (struct case_info, n);
5554 for (idx = 0; idx < n; ++idx)
5556 ci[idx].expr = gimple_switch_label (last, idx);
5557 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5559 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5561 for (idx = 0; idx < n; ++idx)
5563 tree min, max;
5564 tree cl = ci[idx].expr;
5565 basic_block cbb = ci[idx].bb;
5567 min = CASE_LOW (cl);
5568 max = CASE_HIGH (cl);
5570 /* If there are multiple case labels with the same destination
5571 we need to combine them to a single value range for the edge. */
5572 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5574 /* Skip labels until the last of the group. */
5575 do {
5576 ++idx;
5577 } while (idx < n && cbb == ci[idx].bb);
5578 --idx;
5580 /* Pick up the maximum of the case label range. */
5581 if (CASE_HIGH (ci[idx].expr))
5582 max = CASE_HIGH (ci[idx].expr);
5583 else
5584 max = CASE_LOW (ci[idx].expr);
5587 /* Nothing to do if the range includes the default label until we
5588 can register anti-ranges. */
5589 if (min == NULL_TREE)
5590 continue;
5592 /* Find the edge to register the assert expr on. */
5593 e = find_edge (bb, cbb);
5595 /* Register the necessary assertions for the operand in the
5596 SWITCH_EXPR. */
5597 need_assert |= register_edge_assert_for (op, e, bsi,
5598 max ? GE_EXPR : EQ_EXPR,
5600 fold_convert (TREE_TYPE (op),
5601 min));
5602 if (max)
5604 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5606 fold_convert (TREE_TYPE (op),
5607 max));
5611 XDELETEVEC (ci);
5612 return need_assert;
5616 /* Traverse all the statements in block BB looking for statements that
5617 may generate useful assertions for the SSA names in their operand.
5618 If a statement produces a useful assertion A for name N_i, then the
5619 list of assertions already generated for N_i is scanned to
5620 determine if A is actually needed.
5622 If N_i already had the assertion A at a location dominating the
5623 current location, then nothing needs to be done. Otherwise, the
5624 new location for A is recorded instead.
5626 1- For every statement S in BB, all the variables used by S are
5627 added to bitmap FOUND_IN_SUBGRAPH.
5629 2- If statement S uses an operand N in a way that exposes a known
5630 value range for N, then if N was not already generated by an
5631 ASSERT_EXPR, create a new assert location for N. For instance,
5632 if N is a pointer and the statement dereferences it, we can
5633 assume that N is not NULL.
5635 3- COND_EXPRs are a special case of #2. We can derive range
5636 information from the predicate but need to insert different
5637 ASSERT_EXPRs for each of the sub-graphs rooted at the
5638 conditional block. If the last statement of BB is a conditional
5639 expression of the form 'X op Y', then
5641 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5643 b) If the conditional is the only entry point to the sub-graph
5644 corresponding to the THEN_CLAUSE, recurse into it. On
5645 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5646 an ASSERT_EXPR is added for the corresponding variable.
5648 c) Repeat step (b) on the ELSE_CLAUSE.
5650 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5652 For instance,
5654 if (a == 9)
5655 b = a;
5656 else
5657 b = c + 1;
5659 In this case, an assertion on the THEN clause is useful to
5660 determine that 'a' is always 9 on that edge. However, an assertion
5661 on the ELSE clause would be unnecessary.
5663 4- If BB does not end in a conditional expression, then we recurse
5664 into BB's dominator children.
5666 At the end of the recursive traversal, every SSA name will have a
5667 list of locations where ASSERT_EXPRs should be added. When a new
5668 location for name N is found, it is registered by calling
5669 register_new_assert_for. That function keeps track of all the
5670 registered assertions to prevent adding unnecessary assertions.
5671 For instance, if a pointer P_4 is dereferenced more than once in a
5672 dominator tree, only the location dominating all the dereference of
5673 P_4 will receive an ASSERT_EXPR.
5675 If this function returns true, then it means that there are names
5676 for which we need to generate ASSERT_EXPRs. Those assertions are
5677 inserted by process_assert_insertions. */
5679 static bool
5680 find_assert_locations_1 (basic_block bb, sbitmap live)
5682 gimple_stmt_iterator si;
5683 gimple last;
5684 bool need_assert;
5686 need_assert = false;
5687 last = last_stmt (bb);
5689 /* If BB's last statement is a conditional statement involving integer
5690 operands, determine if we need to add ASSERT_EXPRs. */
5691 if (last
5692 && gimple_code (last) == GIMPLE_COND
5693 && !fp_predicate (last)
5694 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5695 need_assert |= find_conditional_asserts (bb, last);
5697 /* If BB's last statement is a switch statement involving integer
5698 operands, determine if we need to add ASSERT_EXPRs. */
5699 if (last
5700 && gimple_code (last) == GIMPLE_SWITCH
5701 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5702 need_assert |= find_switch_asserts (bb, last);
5704 /* Traverse all the statements in BB marking used names and looking
5705 for statements that may infer assertions for their used operands. */
5706 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5708 gimple stmt;
5709 tree op;
5710 ssa_op_iter i;
5712 stmt = gsi_stmt (si);
5714 if (is_gimple_debug (stmt))
5715 continue;
5717 /* See if we can derive an assertion for any of STMT's operands. */
5718 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5720 tree value;
5721 enum tree_code comp_code;
5723 /* If op is not live beyond this stmt, do not bother to insert
5724 asserts for it. */
5725 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5726 continue;
5728 /* If OP is used in such a way that we can infer a value
5729 range for it, and we don't find a previous assertion for
5730 it, create a new assertion location node for OP. */
5731 if (infer_value_range (stmt, op, &comp_code, &value))
5733 /* If we are able to infer a nonzero value range for OP,
5734 then walk backwards through the use-def chain to see if OP
5735 was set via a typecast.
5737 If so, then we can also infer a nonzero value range
5738 for the operand of the NOP_EXPR. */
5739 if (comp_code == NE_EXPR && integer_zerop (value))
5741 tree t = op;
5742 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5744 while (is_gimple_assign (def_stmt)
5745 && gimple_assign_rhs_code (def_stmt) == NOP_EXPR
5746 && TREE_CODE
5747 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5748 && POINTER_TYPE_P
5749 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
5751 t = gimple_assign_rhs1 (def_stmt);
5752 def_stmt = SSA_NAME_DEF_STMT (t);
5754 /* Note we want to register the assert for the
5755 operand of the NOP_EXPR after SI, not after the
5756 conversion. */
5757 if (! has_single_use (t))
5759 register_new_assert_for (t, t, comp_code, value,
5760 bb, NULL, si);
5761 need_assert = true;
5766 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
5767 need_assert = true;
5771 /* Update live. */
5772 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5773 bitmap_set_bit (live, SSA_NAME_VERSION (op));
5774 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
5775 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
5778 /* Traverse all PHI nodes in BB, updating live. */
5779 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5781 use_operand_p arg_p;
5782 ssa_op_iter i;
5783 gimple phi = gsi_stmt (si);
5784 tree res = gimple_phi_result (phi);
5786 if (virtual_operand_p (res))
5787 continue;
5789 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
5791 tree arg = USE_FROM_PTR (arg_p);
5792 if (TREE_CODE (arg) == SSA_NAME)
5793 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
5796 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
5799 return need_assert;
5802 /* Do an RPO walk over the function computing SSA name liveness
5803 on-the-fly and deciding on assert expressions to insert.
5804 Returns true if there are assert expressions to be inserted. */
5806 static bool
5807 find_assert_locations (void)
5809 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
5810 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
5811 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
5812 int rpo_cnt, i;
5813 bool need_asserts;
5815 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
5816 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
5817 for (i = 0; i < rpo_cnt; ++i)
5818 bb_rpo[rpo[i]] = i;
5820 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
5821 the order we compute liveness and insert asserts we otherwise
5822 fail to insert asserts into the loop latch. */
5823 loop_p loop;
5824 FOR_EACH_LOOP (loop, 0)
5826 i = loop->latch->index;
5827 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
5828 for (gimple_stmt_iterator gsi = gsi_start_phis (loop->header);
5829 !gsi_end_p (gsi); gsi_next (&gsi))
5831 gimple phi = gsi_stmt (gsi);
5832 if (virtual_operand_p (gimple_phi_result (phi)))
5833 continue;
5834 tree arg = gimple_phi_arg_def (phi, j);
5835 if (TREE_CODE (arg) == SSA_NAME)
5837 if (live[i] == NULL)
5839 live[i] = sbitmap_alloc (num_ssa_names);
5840 bitmap_clear (live[i]);
5842 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
5847 need_asserts = false;
5848 for (i = rpo_cnt - 1; i >= 0; --i)
5850 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
5851 edge e;
5852 edge_iterator ei;
5854 if (!live[rpo[i]])
5856 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
5857 bitmap_clear (live[rpo[i]]);
5860 /* Process BB and update the live information with uses in
5861 this block. */
5862 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
5864 /* Merge liveness into the predecessor blocks and free it. */
5865 if (!bitmap_empty_p (live[rpo[i]]))
5867 int pred_rpo = i;
5868 FOR_EACH_EDGE (e, ei, bb->preds)
5870 int pred = e->src->index;
5871 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
5872 continue;
5874 if (!live[pred])
5876 live[pred] = sbitmap_alloc (num_ssa_names);
5877 bitmap_clear (live[pred]);
5879 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
5881 if (bb_rpo[pred] < pred_rpo)
5882 pred_rpo = bb_rpo[pred];
5885 /* Record the RPO number of the last visited block that needs
5886 live information from this block. */
5887 last_rpo[rpo[i]] = pred_rpo;
5889 else
5891 sbitmap_free (live[rpo[i]]);
5892 live[rpo[i]] = NULL;
5895 /* We can free all successors live bitmaps if all their
5896 predecessors have been visited already. */
5897 FOR_EACH_EDGE (e, ei, bb->succs)
5898 if (last_rpo[e->dest->index] == i
5899 && live[e->dest->index])
5901 sbitmap_free (live[e->dest->index]);
5902 live[e->dest->index] = NULL;
5906 XDELETEVEC (rpo);
5907 XDELETEVEC (bb_rpo);
5908 XDELETEVEC (last_rpo);
5909 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
5910 if (live[i])
5911 sbitmap_free (live[i]);
5912 XDELETEVEC (live);
5914 return need_asserts;
5917 /* Create an ASSERT_EXPR for NAME and insert it in the location
5918 indicated by LOC. Return true if we made any edge insertions. */
5920 static bool
5921 process_assert_insertions_for (tree name, assert_locus_t loc)
5923 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5924 gimple stmt;
5925 tree cond;
5926 gimple assert_stmt;
5927 edge_iterator ei;
5928 edge e;
5930 /* If we have X <=> X do not insert an assert expr for that. */
5931 if (loc->expr == loc->val)
5932 return false;
5934 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
5935 assert_stmt = build_assert_expr_for (cond, name);
5936 if (loc->e)
5938 /* We have been asked to insert the assertion on an edge. This
5939 is used only by COND_EXPR and SWITCH_EXPR assertions. */
5940 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
5941 || (gimple_code (gsi_stmt (loc->si))
5942 == GIMPLE_SWITCH));
5944 gsi_insert_on_edge (loc->e, assert_stmt);
5945 return true;
5948 /* Otherwise, we can insert right after LOC->SI iff the
5949 statement must not be the last statement in the block. */
5950 stmt = gsi_stmt (loc->si);
5951 if (!stmt_ends_bb_p (stmt))
5953 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
5954 return false;
5957 /* If STMT must be the last statement in BB, we can only insert new
5958 assertions on the non-abnormal edge out of BB. Note that since
5959 STMT is not control flow, there may only be one non-abnormal edge
5960 out of BB. */
5961 FOR_EACH_EDGE (e, ei, loc->bb->succs)
5962 if (!(e->flags & EDGE_ABNORMAL))
5964 gsi_insert_on_edge (e, assert_stmt);
5965 return true;
5968 gcc_unreachable ();
5972 /* Process all the insertions registered for every name N_i registered
5973 in NEED_ASSERT_FOR. The list of assertions to be inserted are
5974 found in ASSERTS_FOR[i]. */
5976 static void
5977 process_assert_insertions (void)
5979 unsigned i;
5980 bitmap_iterator bi;
5981 bool update_edges_p = false;
5982 int num_asserts = 0;
5984 if (dump_file && (dump_flags & TDF_DETAILS))
5985 dump_all_asserts (dump_file);
5987 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
5989 assert_locus_t loc = asserts_for[i];
5990 gcc_assert (loc);
5992 while (loc)
5994 assert_locus_t next = loc->next;
5995 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
5996 free (loc);
5997 loc = next;
5998 num_asserts++;
6002 if (update_edges_p)
6003 gsi_commit_edge_inserts ();
6005 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6006 num_asserts);
6010 /* Traverse the flowgraph looking for conditional jumps to insert range
6011 expressions. These range expressions are meant to provide information
6012 to optimizations that need to reason in terms of value ranges. They
6013 will not be expanded into RTL. For instance, given:
6015 x = ...
6016 y = ...
6017 if (x < y)
6018 y = x - 2;
6019 else
6020 x = y + 3;
6022 this pass will transform the code into:
6024 x = ...
6025 y = ...
6026 if (x < y)
6028 x = ASSERT_EXPR <x, x < y>
6029 y = x - 2
6031 else
6033 y = ASSERT_EXPR <y, x >= y>
6034 x = y + 3
6037 The idea is that once copy and constant propagation have run, other
6038 optimizations will be able to determine what ranges of values can 'x'
6039 take in different paths of the code, simply by checking the reaching
6040 definition of 'x'. */
6042 static void
6043 insert_range_assertions (void)
6045 need_assert_for = BITMAP_ALLOC (NULL);
6046 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6048 calculate_dominance_info (CDI_DOMINATORS);
6050 if (find_assert_locations ())
6052 process_assert_insertions ();
6053 update_ssa (TODO_update_ssa_no_phi);
6056 if (dump_file && (dump_flags & TDF_DETAILS))
6058 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6059 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6062 free (asserts_for);
6063 BITMAP_FREE (need_assert_for);
6066 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6067 and "struct" hacks. If VRP can determine that the
6068 array subscript is a constant, check if it is outside valid
6069 range. If the array subscript is a RANGE, warn if it is
6070 non-overlapping with valid range.
6071 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6073 static void
6074 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6076 value_range_t* vr = NULL;
6077 tree low_sub, up_sub;
6078 tree low_bound, up_bound, up_bound_p1;
6079 tree base;
6081 if (TREE_NO_WARNING (ref))
6082 return;
6084 low_sub = up_sub = TREE_OPERAND (ref, 1);
6085 up_bound = array_ref_up_bound (ref);
6087 /* Can not check flexible arrays. */
6088 if (!up_bound
6089 || TREE_CODE (up_bound) != INTEGER_CST)
6090 return;
6092 /* Accesses to trailing arrays via pointers may access storage
6093 beyond the types array bounds. */
6094 base = get_base_address (ref);
6095 if (base && TREE_CODE (base) == MEM_REF)
6097 tree cref, next = NULL_TREE;
6099 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6100 return;
6102 cref = TREE_OPERAND (ref, 0);
6103 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6104 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6105 next && TREE_CODE (next) != FIELD_DECL;
6106 next = DECL_CHAIN (next))
6109 /* If this is the last field in a struct type or a field in a
6110 union type do not warn. */
6111 if (!next)
6112 return;
6115 low_bound = array_ref_low_bound (ref);
6116 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6117 build_int_cst (TREE_TYPE (up_bound), 1));
6119 if (TREE_CODE (low_sub) == SSA_NAME)
6121 vr = get_value_range (low_sub);
6122 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6124 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6125 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6129 if (vr && vr->type == VR_ANTI_RANGE)
6131 if (TREE_CODE (up_sub) == INTEGER_CST
6132 && tree_int_cst_lt (up_bound, up_sub)
6133 && TREE_CODE (low_sub) == INTEGER_CST
6134 && tree_int_cst_lt (low_sub, low_bound))
6136 warning_at (location, OPT_Warray_bounds,
6137 "array subscript is outside array bounds");
6138 TREE_NO_WARNING (ref) = 1;
6141 else if (TREE_CODE (up_sub) == INTEGER_CST
6142 && (ignore_off_by_one
6143 ? (tree_int_cst_lt (up_bound, up_sub)
6144 && !tree_int_cst_equal (up_bound_p1, up_sub))
6145 : (tree_int_cst_lt (up_bound, up_sub)
6146 || tree_int_cst_equal (up_bound_p1, up_sub))))
6148 if (dump_file && (dump_flags & TDF_DETAILS))
6150 fprintf (dump_file, "Array bound warning for ");
6151 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6152 fprintf (dump_file, "\n");
6154 warning_at (location, OPT_Warray_bounds,
6155 "array subscript is above array bounds");
6156 TREE_NO_WARNING (ref) = 1;
6158 else if (TREE_CODE (low_sub) == INTEGER_CST
6159 && tree_int_cst_lt (low_sub, low_bound))
6161 if (dump_file && (dump_flags & TDF_DETAILS))
6163 fprintf (dump_file, "Array bound warning for ");
6164 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6165 fprintf (dump_file, "\n");
6167 warning_at (location, OPT_Warray_bounds,
6168 "array subscript is below array bounds");
6169 TREE_NO_WARNING (ref) = 1;
6173 /* Searches if the expr T, located at LOCATION computes
6174 address of an ARRAY_REF, and call check_array_ref on it. */
6176 static void
6177 search_for_addr_array (tree t, location_t location)
6179 while (TREE_CODE (t) == SSA_NAME)
6181 gimple g = SSA_NAME_DEF_STMT (t);
6183 if (gimple_code (g) != GIMPLE_ASSIGN)
6184 return;
6186 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6187 != GIMPLE_SINGLE_RHS)
6188 return;
6190 t = gimple_assign_rhs1 (g);
6194 /* We are only interested in addresses of ARRAY_REF's. */
6195 if (TREE_CODE (t) != ADDR_EXPR)
6196 return;
6198 /* Check each ARRAY_REFs in the reference chain. */
6201 if (TREE_CODE (t) == ARRAY_REF)
6202 check_array_ref (location, t, true /*ignore_off_by_one*/);
6204 t = TREE_OPERAND (t, 0);
6206 while (handled_component_p (t));
6208 if (TREE_CODE (t) == MEM_REF
6209 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6210 && !TREE_NO_WARNING (t))
6212 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6213 tree low_bound, up_bound, el_sz;
6214 offset_int idx;
6215 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6216 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6217 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6218 return;
6220 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6221 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6222 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6223 if (!low_bound
6224 || TREE_CODE (low_bound) != INTEGER_CST
6225 || !up_bound
6226 || TREE_CODE (up_bound) != INTEGER_CST
6227 || !el_sz
6228 || TREE_CODE (el_sz) != INTEGER_CST)
6229 return;
6231 idx = mem_ref_offset (t);
6232 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6233 if (wi::lts_p (idx, 0))
6235 if (dump_file && (dump_flags & TDF_DETAILS))
6237 fprintf (dump_file, "Array bound warning for ");
6238 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6239 fprintf (dump_file, "\n");
6241 warning_at (location, OPT_Warray_bounds,
6242 "array subscript is below array bounds");
6243 TREE_NO_WARNING (t) = 1;
6245 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6246 - wi::to_offset (low_bound) + 1)))
6248 if (dump_file && (dump_flags & TDF_DETAILS))
6250 fprintf (dump_file, "Array bound warning for ");
6251 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6252 fprintf (dump_file, "\n");
6254 warning_at (location, OPT_Warray_bounds,
6255 "array subscript is above array bounds");
6256 TREE_NO_WARNING (t) = 1;
6261 /* walk_tree() callback that checks if *TP is
6262 an ARRAY_REF inside an ADDR_EXPR (in which an array
6263 subscript one outside the valid range is allowed). Call
6264 check_array_ref for each ARRAY_REF found. The location is
6265 passed in DATA. */
6267 static tree
6268 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6270 tree t = *tp;
6271 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6272 location_t location;
6274 if (EXPR_HAS_LOCATION (t))
6275 location = EXPR_LOCATION (t);
6276 else
6278 location_t *locp = (location_t *) wi->info;
6279 location = *locp;
6282 *walk_subtree = TRUE;
6284 if (TREE_CODE (t) == ARRAY_REF)
6285 check_array_ref (location, t, false /*ignore_off_by_one*/);
6287 if (TREE_CODE (t) == MEM_REF
6288 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6289 search_for_addr_array (TREE_OPERAND (t, 0), location);
6291 if (TREE_CODE (t) == ADDR_EXPR)
6292 *walk_subtree = FALSE;
6294 return NULL_TREE;
6297 /* Walk over all statements of all reachable BBs and call check_array_bounds
6298 on them. */
6300 static void
6301 check_all_array_refs (void)
6303 basic_block bb;
6304 gimple_stmt_iterator si;
6306 FOR_EACH_BB_FN (bb, cfun)
6308 edge_iterator ei;
6309 edge e;
6310 bool executable = false;
6312 /* Skip blocks that were found to be unreachable. */
6313 FOR_EACH_EDGE (e, ei, bb->preds)
6314 executable |= !!(e->flags & EDGE_EXECUTABLE);
6315 if (!executable)
6316 continue;
6318 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6320 gimple stmt = gsi_stmt (si);
6321 struct walk_stmt_info wi;
6322 if (!gimple_has_location (stmt))
6323 continue;
6325 if (is_gimple_call (stmt))
6327 size_t i;
6328 size_t n = gimple_call_num_args (stmt);
6329 for (i = 0; i < n; i++)
6331 tree arg = gimple_call_arg (stmt, i);
6332 search_for_addr_array (arg, gimple_location (stmt));
6335 else
6337 memset (&wi, 0, sizeof (wi));
6338 wi.info = CONST_CAST (void *, (const void *)
6339 gimple_location_ptr (stmt));
6341 walk_gimple_op (gsi_stmt (si),
6342 check_array_bounds,
6343 &wi);
6349 /* Return true if all imm uses of VAR are either in STMT, or
6350 feed (optionally through a chain of single imm uses) GIMPLE_COND
6351 in basic block COND_BB. */
6353 static bool
6354 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6356 use_operand_p use_p, use2_p;
6357 imm_use_iterator iter;
6359 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6360 if (USE_STMT (use_p) != stmt)
6362 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6363 if (is_gimple_debug (use_stmt))
6364 continue;
6365 while (is_gimple_assign (use_stmt)
6366 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6367 && single_imm_use (gimple_assign_lhs (use_stmt),
6368 &use2_p, &use_stmt2))
6369 use_stmt = use_stmt2;
6370 if (gimple_code (use_stmt) != GIMPLE_COND
6371 || gimple_bb (use_stmt) != cond_bb)
6372 return false;
6374 return true;
6377 /* Handle
6378 _4 = x_3 & 31;
6379 if (_4 != 0)
6380 goto <bb 6>;
6381 else
6382 goto <bb 7>;
6383 <bb 6>:
6384 __builtin_unreachable ();
6385 <bb 7>:
6386 x_5 = ASSERT_EXPR <x_3, ...>;
6387 If x_3 has no other immediate uses (checked by caller),
6388 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6389 from the non-zero bitmask. */
6391 static void
6392 maybe_set_nonzero_bits (basic_block bb, tree var)
6394 edge e = single_pred_edge (bb);
6395 basic_block cond_bb = e->src;
6396 gimple stmt = last_stmt (cond_bb);
6397 tree cst;
6399 if (stmt == NULL
6400 || gimple_code (stmt) != GIMPLE_COND
6401 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6402 ? EQ_EXPR : NE_EXPR)
6403 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6404 || !integer_zerop (gimple_cond_rhs (stmt)))
6405 return;
6407 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6408 if (!is_gimple_assign (stmt)
6409 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6410 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6411 return;
6412 if (gimple_assign_rhs1 (stmt) != var)
6414 gimple stmt2;
6416 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6417 return;
6418 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6419 if (!gimple_assign_cast_p (stmt2)
6420 || gimple_assign_rhs1 (stmt2) != var
6421 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6422 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6423 != TYPE_PRECISION (TREE_TYPE (var))))
6424 return;
6426 cst = gimple_assign_rhs2 (stmt);
6427 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6430 /* Convert range assertion expressions into the implied copies and
6431 copy propagate away the copies. Doing the trivial copy propagation
6432 here avoids the need to run the full copy propagation pass after
6433 VRP.
6435 FIXME, this will eventually lead to copy propagation removing the
6436 names that had useful range information attached to them. For
6437 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6438 then N_i will have the range [3, +INF].
6440 However, by converting the assertion into the implied copy
6441 operation N_i = N_j, we will then copy-propagate N_j into the uses
6442 of N_i and lose the range information. We may want to hold on to
6443 ASSERT_EXPRs a little while longer as the ranges could be used in
6444 things like jump threading.
6446 The problem with keeping ASSERT_EXPRs around is that passes after
6447 VRP need to handle them appropriately.
6449 Another approach would be to make the range information a first
6450 class property of the SSA_NAME so that it can be queried from
6451 any pass. This is made somewhat more complex by the need for
6452 multiple ranges to be associated with one SSA_NAME. */
6454 static void
6455 remove_range_assertions (void)
6457 basic_block bb;
6458 gimple_stmt_iterator si;
6459 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6460 a basic block preceeded by GIMPLE_COND branching to it and
6461 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6462 int is_unreachable;
6464 /* Note that the BSI iterator bump happens at the bottom of the
6465 loop and no bump is necessary if we're removing the statement
6466 referenced by the current BSI. */
6467 FOR_EACH_BB_FN (bb, cfun)
6468 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6470 gimple stmt = gsi_stmt (si);
6471 gimple use_stmt;
6473 if (is_gimple_assign (stmt)
6474 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6476 tree lhs = gimple_assign_lhs (stmt);
6477 tree rhs = gimple_assign_rhs1 (stmt);
6478 tree var;
6479 tree cond = fold (ASSERT_EXPR_COND (rhs));
6480 use_operand_p use_p;
6481 imm_use_iterator iter;
6483 gcc_assert (cond != boolean_false_node);
6485 var = ASSERT_EXPR_VAR (rhs);
6486 gcc_assert (TREE_CODE (var) == SSA_NAME);
6488 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6489 && SSA_NAME_RANGE_INFO (lhs))
6491 if (is_unreachable == -1)
6493 is_unreachable = 0;
6494 if (single_pred_p (bb)
6495 && assert_unreachable_fallthru_edge_p
6496 (single_pred_edge (bb)))
6497 is_unreachable = 1;
6499 /* Handle
6500 if (x_7 >= 10 && x_7 < 20)
6501 __builtin_unreachable ();
6502 x_8 = ASSERT_EXPR <x_7, ...>;
6503 if the only uses of x_7 are in the ASSERT_EXPR and
6504 in the condition. In that case, we can copy the
6505 range info from x_8 computed in this pass also
6506 for x_7. */
6507 if (is_unreachable
6508 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6509 single_pred (bb)))
6511 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6512 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6513 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6514 maybe_set_nonzero_bits (bb, var);
6518 /* Propagate the RHS into every use of the LHS. */
6519 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6520 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6521 SET_USE (use_p, var);
6523 /* And finally, remove the copy, it is not needed. */
6524 gsi_remove (&si, true);
6525 release_defs (stmt);
6527 else
6529 if (!is_gimple_debug (gsi_stmt (si)))
6530 is_unreachable = 0;
6531 gsi_next (&si);
6537 /* Return true if STMT is interesting for VRP. */
6539 static bool
6540 stmt_interesting_for_vrp (gimple stmt)
6542 if (gimple_code (stmt) == GIMPLE_PHI)
6544 tree res = gimple_phi_result (stmt);
6545 return (!virtual_operand_p (res)
6546 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6547 || POINTER_TYPE_P (TREE_TYPE (res))));
6549 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6551 tree lhs = gimple_get_lhs (stmt);
6553 /* In general, assignments with virtual operands are not useful
6554 for deriving ranges, with the obvious exception of calls to
6555 builtin functions. */
6556 if (lhs && TREE_CODE (lhs) == SSA_NAME
6557 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6558 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6559 && (is_gimple_call (stmt)
6560 || !gimple_vuse (stmt)))
6561 return true;
6563 else if (gimple_code (stmt) == GIMPLE_COND
6564 || gimple_code (stmt) == GIMPLE_SWITCH)
6565 return true;
6567 return false;
6571 /* Initialize local data structures for VRP. */
6573 static void
6574 vrp_initialize (void)
6576 basic_block bb;
6578 values_propagated = false;
6579 num_vr_values = num_ssa_names;
6580 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6581 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6583 FOR_EACH_BB_FN (bb, cfun)
6585 gimple_stmt_iterator si;
6587 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6589 gimple phi = gsi_stmt (si);
6590 if (!stmt_interesting_for_vrp (phi))
6592 tree lhs = PHI_RESULT (phi);
6593 set_value_range_to_varying (get_value_range (lhs));
6594 prop_set_simulate_again (phi, false);
6596 else
6597 prop_set_simulate_again (phi, true);
6600 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6602 gimple stmt = gsi_stmt (si);
6604 /* If the statement is a control insn, then we do not
6605 want to avoid simulating the statement once. Failure
6606 to do so means that those edges will never get added. */
6607 if (stmt_ends_bb_p (stmt))
6608 prop_set_simulate_again (stmt, true);
6609 else if (!stmt_interesting_for_vrp (stmt))
6611 ssa_op_iter i;
6612 tree def;
6613 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6614 set_value_range_to_varying (get_value_range (def));
6615 prop_set_simulate_again (stmt, false);
6617 else
6618 prop_set_simulate_again (stmt, true);
6623 /* Return the singleton value-range for NAME or NAME. */
6625 static inline tree
6626 vrp_valueize (tree name)
6628 if (TREE_CODE (name) == SSA_NAME)
6630 value_range_t *vr = get_value_range (name);
6631 if (vr->type == VR_RANGE
6632 && (vr->min == vr->max
6633 || operand_equal_p (vr->min, vr->max, 0)))
6634 return vr->min;
6636 return name;
6639 /* Visit assignment STMT. If it produces an interesting range, record
6640 the SSA name in *OUTPUT_P. */
6642 static enum ssa_prop_result
6643 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6645 tree def, lhs;
6646 ssa_op_iter iter;
6647 enum gimple_code code = gimple_code (stmt);
6648 lhs = gimple_get_lhs (stmt);
6650 /* We only keep track of ranges in integral and pointer types. */
6651 if (TREE_CODE (lhs) == SSA_NAME
6652 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6653 /* It is valid to have NULL MIN/MAX values on a type. See
6654 build_range_type. */
6655 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6656 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6657 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6659 value_range_t new_vr = VR_INITIALIZER;
6661 /* Try folding the statement to a constant first. */
6662 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6663 if (tem)
6664 set_value_range_to_value (&new_vr, tem, NULL);
6665 /* Then dispatch to value-range extracting functions. */
6666 else if (code == GIMPLE_CALL)
6667 extract_range_basic (&new_vr, stmt);
6668 else
6669 extract_range_from_assignment (&new_vr, stmt);
6671 if (update_value_range (lhs, &new_vr))
6673 *output_p = lhs;
6675 if (dump_file && (dump_flags & TDF_DETAILS))
6677 fprintf (dump_file, "Found new range for ");
6678 print_generic_expr (dump_file, lhs, 0);
6679 fprintf (dump_file, ": ");
6680 dump_value_range (dump_file, &new_vr);
6681 fprintf (dump_file, "\n");
6684 if (new_vr.type == VR_VARYING)
6685 return SSA_PROP_VARYING;
6687 return SSA_PROP_INTERESTING;
6690 return SSA_PROP_NOT_INTERESTING;
6693 /* Every other statement produces no useful ranges. */
6694 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6695 set_value_range_to_varying (get_value_range (def));
6697 return SSA_PROP_VARYING;
6700 /* Helper that gets the value range of the SSA_NAME with version I
6701 or a symbolic range containing the SSA_NAME only if the value range
6702 is varying or undefined. */
6704 static inline value_range_t
6705 get_vr_for_comparison (int i)
6707 value_range_t vr = *get_value_range (ssa_name (i));
6709 /* If name N_i does not have a valid range, use N_i as its own
6710 range. This allows us to compare against names that may
6711 have N_i in their ranges. */
6712 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6714 vr.type = VR_RANGE;
6715 vr.min = ssa_name (i);
6716 vr.max = ssa_name (i);
6719 return vr;
6722 /* Compare all the value ranges for names equivalent to VAR with VAL
6723 using comparison code COMP. Return the same value returned by
6724 compare_range_with_value, including the setting of
6725 *STRICT_OVERFLOW_P. */
6727 static tree
6728 compare_name_with_value (enum tree_code comp, tree var, tree val,
6729 bool *strict_overflow_p)
6731 bitmap_iterator bi;
6732 unsigned i;
6733 bitmap e;
6734 tree retval, t;
6735 int used_strict_overflow;
6736 bool sop;
6737 value_range_t equiv_vr;
6739 /* Get the set of equivalences for VAR. */
6740 e = get_value_range (var)->equiv;
6742 /* Start at -1. Set it to 0 if we do a comparison without relying
6743 on overflow, or 1 if all comparisons rely on overflow. */
6744 used_strict_overflow = -1;
6746 /* Compare vars' value range with val. */
6747 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6748 sop = false;
6749 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
6750 if (retval)
6751 used_strict_overflow = sop ? 1 : 0;
6753 /* If the equiv set is empty we have done all work we need to do. */
6754 if (e == NULL)
6756 if (retval
6757 && used_strict_overflow > 0)
6758 *strict_overflow_p = true;
6759 return retval;
6762 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
6764 equiv_vr = get_vr_for_comparison (i);
6765 sop = false;
6766 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
6767 if (t)
6769 /* If we get different answers from different members
6770 of the equivalence set this check must be in a dead
6771 code region. Folding it to a trap representation
6772 would be correct here. For now just return don't-know. */
6773 if (retval != NULL
6774 && t != retval)
6776 retval = NULL_TREE;
6777 break;
6779 retval = t;
6781 if (!sop)
6782 used_strict_overflow = 0;
6783 else if (used_strict_overflow < 0)
6784 used_strict_overflow = 1;
6788 if (retval
6789 && used_strict_overflow > 0)
6790 *strict_overflow_p = true;
6792 return retval;
6796 /* Given a comparison code COMP and names N1 and N2, compare all the
6797 ranges equivalent to N1 against all the ranges equivalent to N2
6798 to determine the value of N1 COMP N2. Return the same value
6799 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6800 whether we relied on an overflow infinity in the comparison. */
6803 static tree
6804 compare_names (enum tree_code comp, tree n1, tree n2,
6805 bool *strict_overflow_p)
6807 tree t, retval;
6808 bitmap e1, e2;
6809 bitmap_iterator bi1, bi2;
6810 unsigned i1, i2;
6811 int used_strict_overflow;
6812 static bitmap_obstack *s_obstack = NULL;
6813 static bitmap s_e1 = NULL, s_e2 = NULL;
6815 /* Compare the ranges of every name equivalent to N1 against the
6816 ranges of every name equivalent to N2. */
6817 e1 = get_value_range (n1)->equiv;
6818 e2 = get_value_range (n2)->equiv;
6820 /* Use the fake bitmaps if e1 or e2 are not available. */
6821 if (s_obstack == NULL)
6823 s_obstack = XNEW (bitmap_obstack);
6824 bitmap_obstack_initialize (s_obstack);
6825 s_e1 = BITMAP_ALLOC (s_obstack);
6826 s_e2 = BITMAP_ALLOC (s_obstack);
6828 if (e1 == NULL)
6829 e1 = s_e1;
6830 if (e2 == NULL)
6831 e2 = s_e2;
6833 /* Add N1 and N2 to their own set of equivalences to avoid
6834 duplicating the body of the loop just to check N1 and N2
6835 ranges. */
6836 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
6837 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
6839 /* If the equivalence sets have a common intersection, then the two
6840 names can be compared without checking their ranges. */
6841 if (bitmap_intersect_p (e1, e2))
6843 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6844 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6846 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
6847 ? boolean_true_node
6848 : boolean_false_node;
6851 /* Start at -1. Set it to 0 if we do a comparison without relying
6852 on overflow, or 1 if all comparisons rely on overflow. */
6853 used_strict_overflow = -1;
6855 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6856 N2 to their own set of equivalences to avoid duplicating the body
6857 of the loop just to check N1 and N2 ranges. */
6858 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
6860 value_range_t vr1 = get_vr_for_comparison (i1);
6862 t = retval = NULL_TREE;
6863 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
6865 bool sop = false;
6867 value_range_t vr2 = get_vr_for_comparison (i2);
6869 t = compare_ranges (comp, &vr1, &vr2, &sop);
6870 if (t)
6872 /* If we get different answers from different members
6873 of the equivalence set this check must be in a dead
6874 code region. Folding it to a trap representation
6875 would be correct here. For now just return don't-know. */
6876 if (retval != NULL
6877 && t != retval)
6879 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6880 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6881 return NULL_TREE;
6883 retval = t;
6885 if (!sop)
6886 used_strict_overflow = 0;
6887 else if (used_strict_overflow < 0)
6888 used_strict_overflow = 1;
6892 if (retval)
6894 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6895 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6896 if (used_strict_overflow > 0)
6897 *strict_overflow_p = true;
6898 return retval;
6902 /* None of the equivalent ranges are useful in computing this
6903 comparison. */
6904 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6905 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6906 return NULL_TREE;
6909 /* Helper function for vrp_evaluate_conditional_warnv. */
6911 static tree
6912 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
6913 tree op0, tree op1,
6914 bool * strict_overflow_p)
6916 value_range_t *vr0, *vr1;
6918 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
6919 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
6921 tree res = NULL_TREE;
6922 if (vr0 && vr1)
6923 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
6924 if (!res && vr0)
6925 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
6926 if (!res && vr1)
6927 res = (compare_range_with_value
6928 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
6929 return res;
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);
7477 if (!stmt_interesting_for_vrp (stmt))
7478 gcc_assert (stmt_ends_bb_p (stmt));
7479 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7480 return vrp_visit_assignment_or_call (stmt, output_p);
7481 else if (gimple_code (stmt) == GIMPLE_COND)
7482 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7483 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7484 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7486 /* All other statements produce nothing of interest for VRP, so mark
7487 their outputs varying and prevent further simulation. */
7488 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7489 set_value_range_to_varying (get_value_range (def));
7491 return SSA_PROP_VARYING;
7494 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7495 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7496 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7497 possible such range. The resulting range is not canonicalized. */
7499 static void
7500 union_ranges (enum value_range_type *vr0type,
7501 tree *vr0min, tree *vr0max,
7502 enum value_range_type vr1type,
7503 tree vr1min, tree vr1max)
7505 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7506 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7508 /* [] is vr0, () is vr1 in the following classification comments. */
7509 if (mineq && maxeq)
7511 /* [( )] */
7512 if (*vr0type == vr1type)
7513 /* Nothing to do for equal ranges. */
7515 else if ((*vr0type == VR_RANGE
7516 && vr1type == VR_ANTI_RANGE)
7517 || (*vr0type == VR_ANTI_RANGE
7518 && vr1type == VR_RANGE))
7520 /* For anti-range with range union the result is varying. */
7521 goto give_up;
7523 else
7524 gcc_unreachable ();
7526 else if (operand_less_p (*vr0max, vr1min) == 1
7527 || operand_less_p (vr1max, *vr0min) == 1)
7529 /* [ ] ( ) or ( ) [ ]
7530 If the ranges have an empty intersection, result of the union
7531 operation is the anti-range or if both are anti-ranges
7532 it covers all. */
7533 if (*vr0type == VR_ANTI_RANGE
7534 && vr1type == VR_ANTI_RANGE)
7535 goto give_up;
7536 else if (*vr0type == VR_ANTI_RANGE
7537 && vr1type == VR_RANGE)
7539 else if (*vr0type == VR_RANGE
7540 && vr1type == VR_ANTI_RANGE)
7542 *vr0type = vr1type;
7543 *vr0min = vr1min;
7544 *vr0max = vr1max;
7546 else if (*vr0type == VR_RANGE
7547 && vr1type == VR_RANGE)
7549 /* The result is the convex hull of both ranges. */
7550 if (operand_less_p (*vr0max, vr1min) == 1)
7552 /* If the result can be an anti-range, create one. */
7553 if (TREE_CODE (*vr0max) == INTEGER_CST
7554 && TREE_CODE (vr1min) == INTEGER_CST
7555 && vrp_val_is_min (*vr0min)
7556 && vrp_val_is_max (vr1max))
7558 tree min = int_const_binop (PLUS_EXPR,
7559 *vr0max,
7560 build_int_cst (TREE_TYPE (*vr0max), 1));
7561 tree max = int_const_binop (MINUS_EXPR,
7562 vr1min,
7563 build_int_cst (TREE_TYPE (vr1min), 1));
7564 if (!operand_less_p (max, min))
7566 *vr0type = VR_ANTI_RANGE;
7567 *vr0min = min;
7568 *vr0max = max;
7570 else
7571 *vr0max = vr1max;
7573 else
7574 *vr0max = vr1max;
7576 else
7578 /* If the result can be an anti-range, create one. */
7579 if (TREE_CODE (vr1max) == INTEGER_CST
7580 && TREE_CODE (*vr0min) == INTEGER_CST
7581 && vrp_val_is_min (vr1min)
7582 && vrp_val_is_max (*vr0max))
7584 tree min = int_const_binop (PLUS_EXPR,
7585 vr1max,
7586 build_int_cst (TREE_TYPE (vr1max), 1));
7587 tree max = int_const_binop (MINUS_EXPR,
7588 *vr0min,
7589 build_int_cst (TREE_TYPE (*vr0min), 1));
7590 if (!operand_less_p (max, min))
7592 *vr0type = VR_ANTI_RANGE;
7593 *vr0min = min;
7594 *vr0max = max;
7596 else
7597 *vr0min = vr1min;
7599 else
7600 *vr0min = vr1min;
7603 else
7604 gcc_unreachable ();
7606 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7607 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7609 /* [ ( ) ] or [( ) ] or [ ( )] */
7610 if (*vr0type == VR_RANGE
7611 && vr1type == VR_RANGE)
7613 else if (*vr0type == VR_ANTI_RANGE
7614 && vr1type == VR_ANTI_RANGE)
7616 *vr0type = vr1type;
7617 *vr0min = vr1min;
7618 *vr0max = vr1max;
7620 else if (*vr0type == VR_ANTI_RANGE
7621 && vr1type == VR_RANGE)
7623 /* Arbitrarily choose the right or left gap. */
7624 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7625 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7626 build_int_cst (TREE_TYPE (vr1min), 1));
7627 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7628 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7629 build_int_cst (TREE_TYPE (vr1max), 1));
7630 else
7631 goto give_up;
7633 else if (*vr0type == VR_RANGE
7634 && vr1type == VR_ANTI_RANGE)
7635 /* The result covers everything. */
7636 goto give_up;
7637 else
7638 gcc_unreachable ();
7640 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7641 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7643 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7644 if (*vr0type == VR_RANGE
7645 && vr1type == VR_RANGE)
7647 *vr0type = vr1type;
7648 *vr0min = vr1min;
7649 *vr0max = vr1max;
7651 else if (*vr0type == VR_ANTI_RANGE
7652 && vr1type == VR_ANTI_RANGE)
7654 else if (*vr0type == VR_RANGE
7655 && vr1type == VR_ANTI_RANGE)
7657 *vr0type = VR_ANTI_RANGE;
7658 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7660 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7661 build_int_cst (TREE_TYPE (*vr0min), 1));
7662 *vr0min = vr1min;
7664 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7666 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7667 build_int_cst (TREE_TYPE (*vr0max), 1));
7668 *vr0max = vr1max;
7670 else
7671 goto give_up;
7673 else if (*vr0type == VR_ANTI_RANGE
7674 && vr1type == VR_RANGE)
7675 /* The result covers everything. */
7676 goto give_up;
7677 else
7678 gcc_unreachable ();
7680 else if ((operand_less_p (vr1min, *vr0max) == 1
7681 || operand_equal_p (vr1min, *vr0max, 0))
7682 && operand_less_p (*vr0min, vr1min) == 1
7683 && operand_less_p (*vr0max, vr1max) == 1)
7685 /* [ ( ] ) or [ ]( ) */
7686 if (*vr0type == VR_RANGE
7687 && vr1type == VR_RANGE)
7688 *vr0max = vr1max;
7689 else if (*vr0type == VR_ANTI_RANGE
7690 && vr1type == VR_ANTI_RANGE)
7691 *vr0min = vr1min;
7692 else if (*vr0type == VR_ANTI_RANGE
7693 && vr1type == VR_RANGE)
7695 if (TREE_CODE (vr1min) == INTEGER_CST)
7696 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7697 build_int_cst (TREE_TYPE (vr1min), 1));
7698 else
7699 goto give_up;
7701 else if (*vr0type == VR_RANGE
7702 && vr1type == VR_ANTI_RANGE)
7704 if (TREE_CODE (*vr0max) == INTEGER_CST)
7706 *vr0type = vr1type;
7707 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7708 build_int_cst (TREE_TYPE (*vr0max), 1));
7709 *vr0max = vr1max;
7711 else
7712 goto give_up;
7714 else
7715 gcc_unreachable ();
7717 else if ((operand_less_p (*vr0min, vr1max) == 1
7718 || operand_equal_p (*vr0min, vr1max, 0))
7719 && operand_less_p (vr1min, *vr0min) == 1
7720 && operand_less_p (vr1max, *vr0max) == 1)
7722 /* ( [ ) ] or ( )[ ] */
7723 if (*vr0type == VR_RANGE
7724 && vr1type == VR_RANGE)
7725 *vr0min = vr1min;
7726 else if (*vr0type == VR_ANTI_RANGE
7727 && vr1type == VR_ANTI_RANGE)
7728 *vr0max = vr1max;
7729 else if (*vr0type == VR_ANTI_RANGE
7730 && vr1type == VR_RANGE)
7732 if (TREE_CODE (vr1max) == INTEGER_CST)
7733 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7734 build_int_cst (TREE_TYPE (vr1max), 1));
7735 else
7736 goto give_up;
7738 else if (*vr0type == VR_RANGE
7739 && vr1type == VR_ANTI_RANGE)
7741 if (TREE_CODE (*vr0min) == INTEGER_CST)
7743 *vr0type = vr1type;
7744 *vr0min = vr1min;
7745 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7746 build_int_cst (TREE_TYPE (*vr0min), 1));
7748 else
7749 goto give_up;
7751 else
7752 gcc_unreachable ();
7754 else
7755 goto give_up;
7757 return;
7759 give_up:
7760 *vr0type = VR_VARYING;
7761 *vr0min = NULL_TREE;
7762 *vr0max = NULL_TREE;
7765 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7766 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7767 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7768 possible such range. The resulting range is not canonicalized. */
7770 static void
7771 intersect_ranges (enum value_range_type *vr0type,
7772 tree *vr0min, tree *vr0max,
7773 enum value_range_type vr1type,
7774 tree vr1min, tree vr1max)
7776 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7777 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7779 /* [] is vr0, () is vr1 in the following classification comments. */
7780 if (mineq && maxeq)
7782 /* [( )] */
7783 if (*vr0type == vr1type)
7784 /* Nothing to do for equal ranges. */
7786 else if ((*vr0type == VR_RANGE
7787 && vr1type == VR_ANTI_RANGE)
7788 || (*vr0type == VR_ANTI_RANGE
7789 && vr1type == VR_RANGE))
7791 /* For anti-range with range intersection the result is empty. */
7792 *vr0type = VR_UNDEFINED;
7793 *vr0min = NULL_TREE;
7794 *vr0max = NULL_TREE;
7796 else
7797 gcc_unreachable ();
7799 else if (operand_less_p (*vr0max, vr1min) == 1
7800 || operand_less_p (vr1max, *vr0min) == 1)
7802 /* [ ] ( ) or ( ) [ ]
7803 If the ranges have an empty intersection, the result of the
7804 intersect operation is the range for intersecting an
7805 anti-range with a range or empty when intersecting two ranges. */
7806 if (*vr0type == VR_RANGE
7807 && vr1type == VR_ANTI_RANGE)
7809 else if (*vr0type == VR_ANTI_RANGE
7810 && vr1type == VR_RANGE)
7812 *vr0type = vr1type;
7813 *vr0min = vr1min;
7814 *vr0max = vr1max;
7816 else if (*vr0type == VR_RANGE
7817 && vr1type == VR_RANGE)
7819 *vr0type = VR_UNDEFINED;
7820 *vr0min = NULL_TREE;
7821 *vr0max = NULL_TREE;
7823 else if (*vr0type == VR_ANTI_RANGE
7824 && vr1type == VR_ANTI_RANGE)
7826 /* If the anti-ranges are adjacent to each other merge them. */
7827 if (TREE_CODE (*vr0max) == INTEGER_CST
7828 && TREE_CODE (vr1min) == INTEGER_CST
7829 && operand_less_p (*vr0max, vr1min) == 1
7830 && integer_onep (int_const_binop (MINUS_EXPR,
7831 vr1min, *vr0max)))
7832 *vr0max = vr1max;
7833 else if (TREE_CODE (vr1max) == INTEGER_CST
7834 && TREE_CODE (*vr0min) == INTEGER_CST
7835 && operand_less_p (vr1max, *vr0min) == 1
7836 && integer_onep (int_const_binop (MINUS_EXPR,
7837 *vr0min, vr1max)))
7838 *vr0min = vr1min;
7839 /* Else arbitrarily take VR0. */
7842 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7843 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7845 /* [ ( ) ] or [( ) ] or [ ( )] */
7846 if (*vr0type == VR_RANGE
7847 && vr1type == VR_RANGE)
7849 /* If both are ranges the result is the inner one. */
7850 *vr0type = vr1type;
7851 *vr0min = vr1min;
7852 *vr0max = vr1max;
7854 else if (*vr0type == VR_RANGE
7855 && vr1type == VR_ANTI_RANGE)
7857 /* Choose the right gap if the left one is empty. */
7858 if (mineq)
7860 if (TREE_CODE (vr1max) == INTEGER_CST)
7861 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7862 build_int_cst (TREE_TYPE (vr1max), 1));
7863 else
7864 *vr0min = vr1max;
7866 /* Choose the left gap if the right one is empty. */
7867 else if (maxeq)
7869 if (TREE_CODE (vr1min) == INTEGER_CST)
7870 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7871 build_int_cst (TREE_TYPE (vr1min), 1));
7872 else
7873 *vr0max = vr1min;
7875 /* Choose the anti-range if the range is effectively varying. */
7876 else if (vrp_val_is_min (*vr0min)
7877 && vrp_val_is_max (*vr0max))
7879 *vr0type = vr1type;
7880 *vr0min = vr1min;
7881 *vr0max = vr1max;
7883 /* Else choose the range. */
7885 else if (*vr0type == VR_ANTI_RANGE
7886 && vr1type == VR_ANTI_RANGE)
7887 /* If both are anti-ranges the result is the outer one. */
7889 else if (*vr0type == VR_ANTI_RANGE
7890 && vr1type == VR_RANGE)
7892 /* The intersection is empty. */
7893 *vr0type = VR_UNDEFINED;
7894 *vr0min = NULL_TREE;
7895 *vr0max = NULL_TREE;
7897 else
7898 gcc_unreachable ();
7900 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7901 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7903 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7904 if (*vr0type == VR_RANGE
7905 && vr1type == VR_RANGE)
7906 /* Choose the inner range. */
7908 else if (*vr0type == VR_ANTI_RANGE
7909 && vr1type == VR_RANGE)
7911 /* Choose the right gap if the left is empty. */
7912 if (mineq)
7914 *vr0type = VR_RANGE;
7915 if (TREE_CODE (*vr0max) == INTEGER_CST)
7916 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7917 build_int_cst (TREE_TYPE (*vr0max), 1));
7918 else
7919 *vr0min = *vr0max;
7920 *vr0max = vr1max;
7922 /* Choose the left gap if the right is empty. */
7923 else if (maxeq)
7925 *vr0type = VR_RANGE;
7926 if (TREE_CODE (*vr0min) == INTEGER_CST)
7927 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7928 build_int_cst (TREE_TYPE (*vr0min), 1));
7929 else
7930 *vr0max = *vr0min;
7931 *vr0min = vr1min;
7933 /* Choose the anti-range if the range is effectively varying. */
7934 else if (vrp_val_is_min (vr1min)
7935 && vrp_val_is_max (vr1max))
7937 /* Else choose the range. */
7938 else
7940 *vr0type = vr1type;
7941 *vr0min = vr1min;
7942 *vr0max = vr1max;
7945 else if (*vr0type == VR_ANTI_RANGE
7946 && vr1type == VR_ANTI_RANGE)
7948 /* If both are anti-ranges the result is the outer one. */
7949 *vr0type = vr1type;
7950 *vr0min = vr1min;
7951 *vr0max = vr1max;
7953 else if (vr1type == VR_ANTI_RANGE
7954 && *vr0type == VR_RANGE)
7956 /* The intersection is empty. */
7957 *vr0type = VR_UNDEFINED;
7958 *vr0min = NULL_TREE;
7959 *vr0max = NULL_TREE;
7961 else
7962 gcc_unreachable ();
7964 else if ((operand_less_p (vr1min, *vr0max) == 1
7965 || operand_equal_p (vr1min, *vr0max, 0))
7966 && operand_less_p (*vr0min, vr1min) == 1)
7968 /* [ ( ] ) or [ ]( ) */
7969 if (*vr0type == VR_ANTI_RANGE
7970 && vr1type == VR_ANTI_RANGE)
7971 *vr0max = vr1max;
7972 else if (*vr0type == VR_RANGE
7973 && vr1type == VR_RANGE)
7974 *vr0min = vr1min;
7975 else if (*vr0type == VR_RANGE
7976 && vr1type == VR_ANTI_RANGE)
7978 if (TREE_CODE (vr1min) == INTEGER_CST)
7979 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7980 build_int_cst (TREE_TYPE (vr1min), 1));
7981 else
7982 *vr0max = vr1min;
7984 else if (*vr0type == VR_ANTI_RANGE
7985 && vr1type == VR_RANGE)
7987 *vr0type = VR_RANGE;
7988 if (TREE_CODE (*vr0max) == INTEGER_CST)
7989 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7990 build_int_cst (TREE_TYPE (*vr0max), 1));
7991 else
7992 *vr0min = *vr0max;
7993 *vr0max = vr1max;
7995 else
7996 gcc_unreachable ();
7998 else if ((operand_less_p (*vr0min, vr1max) == 1
7999 || operand_equal_p (*vr0min, vr1max, 0))
8000 && operand_less_p (vr1min, *vr0min) == 1)
8002 /* ( [ ) ] or ( )[ ] */
8003 if (*vr0type == VR_ANTI_RANGE
8004 && vr1type == VR_ANTI_RANGE)
8005 *vr0min = vr1min;
8006 else if (*vr0type == VR_RANGE
8007 && vr1type == VR_RANGE)
8008 *vr0max = vr1max;
8009 else if (*vr0type == VR_RANGE
8010 && vr1type == VR_ANTI_RANGE)
8012 if (TREE_CODE (vr1max) == INTEGER_CST)
8013 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8014 build_int_cst (TREE_TYPE (vr1max), 1));
8015 else
8016 *vr0min = vr1max;
8018 else if (*vr0type == VR_ANTI_RANGE
8019 && vr1type == VR_RANGE)
8021 *vr0type = VR_RANGE;
8022 if (TREE_CODE (*vr0min) == INTEGER_CST)
8023 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8024 build_int_cst (TREE_TYPE (*vr0min), 1));
8025 else
8026 *vr0max = *vr0min;
8027 *vr0min = vr1min;
8029 else
8030 gcc_unreachable ();
8033 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8034 result for the intersection. That's always a conservative
8035 correct estimate. */
8037 return;
8041 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8042 in *VR0. This may not be the smallest possible such range. */
8044 static void
8045 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8047 value_range_t saved;
8049 /* If either range is VR_VARYING the other one wins. */
8050 if (vr1->type == VR_VARYING)
8051 return;
8052 if (vr0->type == VR_VARYING)
8054 copy_value_range (vr0, vr1);
8055 return;
8058 /* When either range is VR_UNDEFINED the resulting range is
8059 VR_UNDEFINED, too. */
8060 if (vr0->type == VR_UNDEFINED)
8061 return;
8062 if (vr1->type == VR_UNDEFINED)
8064 set_value_range_to_undefined (vr0);
8065 return;
8068 /* Save the original vr0 so we can return it as conservative intersection
8069 result when our worker turns things to varying. */
8070 saved = *vr0;
8071 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8072 vr1->type, vr1->min, vr1->max);
8073 /* Make sure to canonicalize the result though as the inversion of a
8074 VR_RANGE can still be a VR_RANGE. */
8075 set_and_canonicalize_value_range (vr0, vr0->type,
8076 vr0->min, vr0->max, vr0->equiv);
8077 /* If that failed, use the saved original VR0. */
8078 if (vr0->type == VR_VARYING)
8080 *vr0 = saved;
8081 return;
8083 /* If the result is VR_UNDEFINED there is no need to mess with
8084 the equivalencies. */
8085 if (vr0->type == VR_UNDEFINED)
8086 return;
8088 /* The resulting set of equivalences for range intersection is the union of
8089 the two sets. */
8090 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8091 bitmap_ior_into (vr0->equiv, vr1->equiv);
8092 else if (vr1->equiv && !vr0->equiv)
8093 bitmap_copy (vr0->equiv, vr1->equiv);
8096 static void
8097 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8099 if (dump_file && (dump_flags & TDF_DETAILS))
8101 fprintf (dump_file, "Intersecting\n ");
8102 dump_value_range (dump_file, vr0);
8103 fprintf (dump_file, "\nand\n ");
8104 dump_value_range (dump_file, vr1);
8105 fprintf (dump_file, "\n");
8107 vrp_intersect_ranges_1 (vr0, vr1);
8108 if (dump_file && (dump_flags & TDF_DETAILS))
8110 fprintf (dump_file, "to\n ");
8111 dump_value_range (dump_file, vr0);
8112 fprintf (dump_file, "\n");
8116 /* Meet operation for value ranges. Given two value ranges VR0 and
8117 VR1, store in VR0 a range that contains both VR0 and VR1. This
8118 may not be the smallest possible such range. */
8120 static void
8121 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8123 value_range_t saved;
8125 if (vr0->type == VR_UNDEFINED)
8127 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8128 return;
8131 if (vr1->type == VR_UNDEFINED)
8133 /* VR0 already has the resulting range. */
8134 return;
8137 if (vr0->type == VR_VARYING)
8139 /* Nothing to do. VR0 already has the resulting range. */
8140 return;
8143 if (vr1->type == VR_VARYING)
8145 set_value_range_to_varying (vr0);
8146 return;
8149 saved = *vr0;
8150 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8151 vr1->type, vr1->min, vr1->max);
8152 if (vr0->type == VR_VARYING)
8154 /* Failed to find an efficient meet. Before giving up and setting
8155 the result to VARYING, see if we can at least derive a useful
8156 anti-range. FIXME, all this nonsense about distinguishing
8157 anti-ranges from ranges is necessary because of the odd
8158 semantics of range_includes_zero_p and friends. */
8159 if (((saved.type == VR_RANGE
8160 && range_includes_zero_p (saved.min, saved.max) == 0)
8161 || (saved.type == VR_ANTI_RANGE
8162 && range_includes_zero_p (saved.min, saved.max) == 1))
8163 && ((vr1->type == VR_RANGE
8164 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8165 || (vr1->type == VR_ANTI_RANGE
8166 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8168 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8170 /* Since this meet operation did not result from the meeting of
8171 two equivalent names, VR0 cannot have any equivalences. */
8172 if (vr0->equiv)
8173 bitmap_clear (vr0->equiv);
8174 return;
8177 set_value_range_to_varying (vr0);
8178 return;
8180 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8181 vr0->equiv);
8182 if (vr0->type == VR_VARYING)
8183 return;
8185 /* The resulting set of equivalences is always the intersection of
8186 the two sets. */
8187 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8188 bitmap_and_into (vr0->equiv, vr1->equiv);
8189 else if (vr0->equiv && !vr1->equiv)
8190 bitmap_clear (vr0->equiv);
8193 static void
8194 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8196 if (dump_file && (dump_flags & TDF_DETAILS))
8198 fprintf (dump_file, "Meeting\n ");
8199 dump_value_range (dump_file, vr0);
8200 fprintf (dump_file, "\nand\n ");
8201 dump_value_range (dump_file, vr1);
8202 fprintf (dump_file, "\n");
8204 vrp_meet_1 (vr0, vr1);
8205 if (dump_file && (dump_flags & TDF_DETAILS))
8207 fprintf (dump_file, "to\n ");
8208 dump_value_range (dump_file, vr0);
8209 fprintf (dump_file, "\n");
8214 /* Visit all arguments for PHI node PHI that flow through executable
8215 edges. If a valid value range can be derived from all the incoming
8216 value ranges, set a new range for the LHS of PHI. */
8218 static enum ssa_prop_result
8219 vrp_visit_phi_node (gimple phi)
8221 size_t i;
8222 tree lhs = PHI_RESULT (phi);
8223 value_range_t *lhs_vr = get_value_range (lhs);
8224 value_range_t vr_result = VR_INITIALIZER;
8225 bool first = true;
8226 int edges, old_edges;
8227 struct loop *l;
8229 if (dump_file && (dump_flags & TDF_DETAILS))
8231 fprintf (dump_file, "\nVisiting PHI node: ");
8232 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8235 edges = 0;
8236 for (i = 0; i < gimple_phi_num_args (phi); i++)
8238 edge e = gimple_phi_arg_edge (phi, i);
8240 if (dump_file && (dump_flags & TDF_DETAILS))
8242 fprintf (dump_file,
8243 " Argument #%d (%d -> %d %sexecutable)\n",
8244 (int) i, e->src->index, e->dest->index,
8245 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8248 if (e->flags & EDGE_EXECUTABLE)
8250 tree arg = PHI_ARG_DEF (phi, i);
8251 value_range_t vr_arg;
8253 ++edges;
8255 if (TREE_CODE (arg) == SSA_NAME)
8257 vr_arg = *(get_value_range (arg));
8258 /* Do not allow equivalences or symbolic ranges to leak in from
8259 backedges. That creates invalid equivalencies.
8260 See PR53465 and PR54767. */
8261 if (e->flags & EDGE_DFS_BACK)
8263 if (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 the non-backedge arguments range is VR_VARYING then
8278 we can still try recording a simple equivalence. */
8279 if (vr_arg.type == VR_VARYING)
8281 vr_arg.type = VR_RANGE;
8282 vr_arg.min = arg;
8283 vr_arg.max = arg;
8284 vr_arg.equiv = NULL;
8288 else
8290 if (TREE_OVERFLOW_P (arg))
8291 arg = drop_tree_overflow (arg);
8293 vr_arg.type = VR_RANGE;
8294 vr_arg.min = arg;
8295 vr_arg.max = arg;
8296 vr_arg.equiv = NULL;
8299 if (dump_file && (dump_flags & TDF_DETAILS))
8301 fprintf (dump_file, "\t");
8302 print_generic_expr (dump_file, arg, dump_flags);
8303 fprintf (dump_file, ": ");
8304 dump_value_range (dump_file, &vr_arg);
8305 fprintf (dump_file, "\n");
8308 if (first)
8309 copy_value_range (&vr_result, &vr_arg);
8310 else
8311 vrp_meet (&vr_result, &vr_arg);
8312 first = false;
8314 if (vr_result.type == VR_VARYING)
8315 break;
8319 if (vr_result.type == VR_VARYING)
8320 goto varying;
8321 else if (vr_result.type == VR_UNDEFINED)
8322 goto update_range;
8324 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8325 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8327 /* To prevent infinite iterations in the algorithm, derive ranges
8328 when the new value is slightly bigger or smaller than the
8329 previous one. We don't do this if we have seen a new executable
8330 edge; this helps us avoid an overflow infinity for conditionals
8331 which are not in a loop. If the old value-range was VR_UNDEFINED
8332 use the updated range and iterate one more time. */
8333 if (edges > 0
8334 && gimple_phi_num_args (phi) > 1
8335 && edges == old_edges
8336 && lhs_vr->type != VR_UNDEFINED)
8338 /* Compare old and new ranges, fall back to varying if the
8339 values are not comparable. */
8340 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8341 if (cmp_min == -2)
8342 goto varying;
8343 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8344 if (cmp_max == -2)
8345 goto varying;
8347 /* For non VR_RANGE or for pointers fall back to varying if
8348 the range changed. */
8349 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8350 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8351 && (cmp_min != 0 || cmp_max != 0))
8352 goto varying;
8354 /* If the new minimum is larger than than the previous one
8355 retain the old value. If the new minimum value is smaller
8356 than the previous one and not -INF go all the way to -INF + 1.
8357 In the first case, to avoid infinite bouncing between different
8358 minimums, and in the other case to avoid iterating millions of
8359 times to reach -INF. Going to -INF + 1 also lets the following
8360 iteration compute whether there will be any overflow, at the
8361 expense of one additional iteration. */
8362 if (cmp_min < 0)
8363 vr_result.min = lhs_vr->min;
8364 else if (cmp_min > 0
8365 && !vrp_val_is_min (vr_result.min))
8366 vr_result.min
8367 = int_const_binop (PLUS_EXPR,
8368 vrp_val_min (TREE_TYPE (vr_result.min)),
8369 build_int_cst (TREE_TYPE (vr_result.min), 1));
8371 /* Similarly for the maximum value. */
8372 if (cmp_max > 0)
8373 vr_result.max = lhs_vr->max;
8374 else if (cmp_max < 0
8375 && !vrp_val_is_max (vr_result.max))
8376 vr_result.max
8377 = int_const_binop (MINUS_EXPR,
8378 vrp_val_max (TREE_TYPE (vr_result.min)),
8379 build_int_cst (TREE_TYPE (vr_result.min), 1));
8381 /* If we dropped either bound to +-INF then if this is a loop
8382 PHI node SCEV may known more about its value-range. */
8383 if ((cmp_min > 0 || cmp_min < 0
8384 || cmp_max < 0 || cmp_max > 0)
8385 && (l = loop_containing_stmt (phi))
8386 && l->header == gimple_bb (phi))
8387 adjust_range_with_scev (&vr_result, l, phi, lhs);
8389 /* If we will end up with a (-INF, +INF) range, set it to
8390 VARYING. Same if the previous max value was invalid for
8391 the type and we end up with vr_result.min > vr_result.max. */
8392 if ((vrp_val_is_max (vr_result.max)
8393 && vrp_val_is_min (vr_result.min))
8394 || compare_values (vr_result.min,
8395 vr_result.max) > 0)
8396 goto varying;
8399 /* If the new range is different than the previous value, keep
8400 iterating. */
8401 update_range:
8402 if (update_value_range (lhs, &vr_result))
8404 if (dump_file && (dump_flags & TDF_DETAILS))
8406 fprintf (dump_file, "Found new range for ");
8407 print_generic_expr (dump_file, lhs, 0);
8408 fprintf (dump_file, ": ");
8409 dump_value_range (dump_file, &vr_result);
8410 fprintf (dump_file, "\n");
8413 return SSA_PROP_INTERESTING;
8416 /* Nothing changed, don't add outgoing edges. */
8417 return SSA_PROP_NOT_INTERESTING;
8419 /* No match found. Set the LHS to VARYING. */
8420 varying:
8421 set_value_range_to_varying (lhs_vr);
8422 return SSA_PROP_VARYING;
8425 /* Simplify boolean operations if the source is known
8426 to be already a boolean. */
8427 static bool
8428 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8430 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8431 tree lhs, op0, op1;
8432 bool need_conversion;
8434 /* We handle only !=/== case here. */
8435 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8437 op0 = gimple_assign_rhs1 (stmt);
8438 if (!op_with_boolean_value_range_p (op0))
8439 return false;
8441 op1 = gimple_assign_rhs2 (stmt);
8442 if (!op_with_boolean_value_range_p (op1))
8443 return false;
8445 /* Reduce number of cases to handle to NE_EXPR. As there is no
8446 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8447 if (rhs_code == EQ_EXPR)
8449 if (TREE_CODE (op1) == INTEGER_CST)
8450 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8451 build_int_cst (TREE_TYPE (op1), 1));
8452 else
8453 return false;
8456 lhs = gimple_assign_lhs (stmt);
8457 need_conversion
8458 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8460 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8461 if (need_conversion
8462 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8463 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8464 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8465 return false;
8467 /* For A != 0 we can substitute A itself. */
8468 if (integer_zerop (op1))
8469 gimple_assign_set_rhs_with_ops (gsi,
8470 need_conversion
8471 ? NOP_EXPR : TREE_CODE (op0),
8472 op0, NULL_TREE);
8473 /* For A != B we substitute A ^ B. Either with conversion. */
8474 else if (need_conversion)
8476 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8477 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8478 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8479 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8481 /* Or without. */
8482 else
8483 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8484 update_stmt (gsi_stmt (*gsi));
8486 return true;
8489 /* Simplify a division or modulo operator to a right shift or
8490 bitwise and if the first operand is unsigned or is greater
8491 than zero and the second operand is an exact power of two. */
8493 static bool
8494 simplify_div_or_mod_using_ranges (gimple stmt)
8496 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8497 tree val = NULL;
8498 tree op0 = gimple_assign_rhs1 (stmt);
8499 tree op1 = gimple_assign_rhs2 (stmt);
8500 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8502 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8504 val = integer_one_node;
8506 else
8508 bool sop = false;
8510 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8512 if (val
8513 && sop
8514 && integer_onep (val)
8515 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8517 location_t location;
8519 if (!gimple_has_location (stmt))
8520 location = input_location;
8521 else
8522 location = gimple_location (stmt);
8523 warning_at (location, OPT_Wstrict_overflow,
8524 "assuming signed overflow does not occur when "
8525 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8529 if (val && integer_onep (val))
8531 tree t;
8533 if (rhs_code == TRUNC_DIV_EXPR)
8535 t = build_int_cst (integer_type_node, tree_log2 (op1));
8536 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8537 gimple_assign_set_rhs1 (stmt, op0);
8538 gimple_assign_set_rhs2 (stmt, t);
8540 else
8542 t = build_int_cst (TREE_TYPE (op1), 1);
8543 t = int_const_binop (MINUS_EXPR, op1, t);
8544 t = fold_convert (TREE_TYPE (op0), t);
8546 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8547 gimple_assign_set_rhs1 (stmt, op0);
8548 gimple_assign_set_rhs2 (stmt, t);
8551 update_stmt (stmt);
8552 return true;
8555 return false;
8558 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8559 ABS_EXPR. If the operand is <= 0, then simplify the
8560 ABS_EXPR into a NEGATE_EXPR. */
8562 static bool
8563 simplify_abs_using_ranges (gimple stmt)
8565 tree val = NULL;
8566 tree op = gimple_assign_rhs1 (stmt);
8567 tree type = TREE_TYPE (op);
8568 value_range_t *vr = get_value_range (op);
8570 if (TYPE_UNSIGNED (type))
8572 val = integer_zero_node;
8574 else if (vr)
8576 bool sop = false;
8578 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8579 if (!val)
8581 sop = false;
8582 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8583 &sop);
8585 if (val)
8587 if (integer_zerop (val))
8588 val = integer_one_node;
8589 else if (integer_onep (val))
8590 val = integer_zero_node;
8594 if (val
8595 && (integer_onep (val) || integer_zerop (val)))
8597 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8599 location_t location;
8601 if (!gimple_has_location (stmt))
8602 location = input_location;
8603 else
8604 location = gimple_location (stmt);
8605 warning_at (location, OPT_Wstrict_overflow,
8606 "assuming signed overflow does not occur when "
8607 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8610 gimple_assign_set_rhs1 (stmt, op);
8611 if (integer_onep (val))
8612 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8613 else
8614 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8615 update_stmt (stmt);
8616 return true;
8620 return false;
8623 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8624 If all the bits that are being cleared by & are already
8625 known to be zero from VR, or all the bits that are being
8626 set by | are already known to be one from VR, the bit
8627 operation is redundant. */
8629 static bool
8630 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8632 tree op0 = gimple_assign_rhs1 (stmt);
8633 tree op1 = gimple_assign_rhs2 (stmt);
8634 tree op = NULL_TREE;
8635 value_range_t vr0 = VR_INITIALIZER;
8636 value_range_t vr1 = VR_INITIALIZER;
8637 wide_int may_be_nonzero0, may_be_nonzero1;
8638 wide_int must_be_nonzero0, must_be_nonzero1;
8639 wide_int mask;
8641 if (TREE_CODE (op0) == SSA_NAME)
8642 vr0 = *(get_value_range (op0));
8643 else if (is_gimple_min_invariant (op0))
8644 set_value_range_to_value (&vr0, op0, NULL);
8645 else
8646 return false;
8648 if (TREE_CODE (op1) == SSA_NAME)
8649 vr1 = *(get_value_range (op1));
8650 else if (is_gimple_min_invariant (op1))
8651 set_value_range_to_value (&vr1, op1, NULL);
8652 else
8653 return false;
8655 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
8656 &must_be_nonzero0))
8657 return false;
8658 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
8659 &must_be_nonzero1))
8660 return false;
8662 switch (gimple_assign_rhs_code (stmt))
8664 case BIT_AND_EXPR:
8665 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8666 if (mask == 0)
8668 op = op0;
8669 break;
8671 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8672 if (mask == 0)
8674 op = op1;
8675 break;
8677 break;
8678 case BIT_IOR_EXPR:
8679 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8680 if (mask == 0)
8682 op = op1;
8683 break;
8685 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8686 if (mask == 0)
8688 op = op0;
8689 break;
8691 break;
8692 default:
8693 gcc_unreachable ();
8696 if (op == NULL_TREE)
8697 return false;
8699 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8700 update_stmt (gsi_stmt (*gsi));
8701 return true;
8704 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8705 a known value range VR.
8707 If there is one and only one value which will satisfy the
8708 conditional, then return that value. Else return NULL. */
8710 static tree
8711 test_for_singularity (enum tree_code cond_code, tree op0,
8712 tree op1, value_range_t *vr)
8714 tree min = NULL;
8715 tree max = NULL;
8717 /* Extract minimum/maximum values which satisfy the
8718 the conditional as it was written. */
8719 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8721 /* This should not be negative infinity; there is no overflow
8722 here. */
8723 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8725 max = op1;
8726 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8728 tree one = build_int_cst (TREE_TYPE (op0), 1);
8729 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8730 if (EXPR_P (max))
8731 TREE_NO_WARNING (max) = 1;
8734 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8736 /* This should not be positive infinity; there is no overflow
8737 here. */
8738 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8740 min = op1;
8741 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8743 tree one = build_int_cst (TREE_TYPE (op0), 1);
8744 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8745 if (EXPR_P (min))
8746 TREE_NO_WARNING (min) = 1;
8750 /* Now refine the minimum and maximum values using any
8751 value range information we have for op0. */
8752 if (min && max)
8754 if (compare_values (vr->min, min) == 1)
8755 min = vr->min;
8756 if (compare_values (vr->max, max) == -1)
8757 max = vr->max;
8759 /* If the new min/max values have converged to a single value,
8760 then there is only one value which can satisfy the condition,
8761 return that value. */
8762 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
8763 return min;
8765 return NULL;
8768 /* Return whether the value range *VR fits in an integer type specified
8769 by PRECISION and UNSIGNED_P. */
8771 static bool
8772 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
8774 tree src_type;
8775 unsigned src_precision;
8776 widest_int tem;
8777 signop src_sgn;
8779 /* We can only handle integral and pointer types. */
8780 src_type = TREE_TYPE (vr->min);
8781 if (!INTEGRAL_TYPE_P (src_type)
8782 && !POINTER_TYPE_P (src_type))
8783 return false;
8785 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
8786 and so is an identity transform. */
8787 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
8788 src_sgn = TYPE_SIGN (src_type);
8789 if ((src_precision < dest_precision
8790 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
8791 || (src_precision == dest_precision && src_sgn == dest_sgn))
8792 return true;
8794 /* Now we can only handle ranges with constant bounds. */
8795 if (vr->type != VR_RANGE
8796 || TREE_CODE (vr->min) != INTEGER_CST
8797 || TREE_CODE (vr->max) != INTEGER_CST)
8798 return false;
8800 /* For sign changes, the MSB of the wide_int has to be clear.
8801 An unsigned value with its MSB set cannot be represented by
8802 a signed wide_int, while a negative value cannot be represented
8803 by an unsigned wide_int. */
8804 if (src_sgn != dest_sgn
8805 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
8806 return false;
8808 /* Then we can perform the conversion on both ends and compare
8809 the result for equality. */
8810 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
8811 if (tem != wi::to_widest (vr->min))
8812 return false;
8813 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
8814 if (tem != wi::to_widest (vr->max))
8815 return false;
8817 return true;
8820 /* Simplify a conditional using a relational operator to an equality
8821 test if the range information indicates only one value can satisfy
8822 the original conditional. */
8824 static bool
8825 simplify_cond_using_ranges (gimple stmt)
8827 tree op0 = gimple_cond_lhs (stmt);
8828 tree op1 = gimple_cond_rhs (stmt);
8829 enum tree_code cond_code = gimple_cond_code (stmt);
8831 if (cond_code != NE_EXPR
8832 && cond_code != EQ_EXPR
8833 && TREE_CODE (op0) == SSA_NAME
8834 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8835 && is_gimple_min_invariant (op1))
8837 value_range_t *vr = get_value_range (op0);
8839 /* If we have range information for OP0, then we might be
8840 able to simplify this conditional. */
8841 if (vr->type == VR_RANGE)
8843 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
8845 if (new_tree)
8847 if (dump_file)
8849 fprintf (dump_file, "Simplified relational ");
8850 print_gimple_stmt (dump_file, stmt, 0, 0);
8851 fprintf (dump_file, " into ");
8854 gimple_cond_set_code (stmt, EQ_EXPR);
8855 gimple_cond_set_lhs (stmt, op0);
8856 gimple_cond_set_rhs (stmt, new_tree);
8858 update_stmt (stmt);
8860 if (dump_file)
8862 print_gimple_stmt (dump_file, stmt, 0, 0);
8863 fprintf (dump_file, "\n");
8866 return true;
8869 /* Try again after inverting the condition. We only deal
8870 with integral types here, so no need to worry about
8871 issues with inverting FP comparisons. */
8872 cond_code = invert_tree_comparison (cond_code, false);
8873 new_tree = test_for_singularity (cond_code, op0, op1, vr);
8875 if (new_tree)
8877 if (dump_file)
8879 fprintf (dump_file, "Simplified relational ");
8880 print_gimple_stmt (dump_file, stmt, 0, 0);
8881 fprintf (dump_file, " into ");
8884 gimple_cond_set_code (stmt, NE_EXPR);
8885 gimple_cond_set_lhs (stmt, op0);
8886 gimple_cond_set_rhs (stmt, new_tree);
8888 update_stmt (stmt);
8890 if (dump_file)
8892 print_gimple_stmt (dump_file, stmt, 0, 0);
8893 fprintf (dump_file, "\n");
8896 return true;
8901 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8902 see if OP0 was set by a type conversion where the source of
8903 the conversion is another SSA_NAME with a range that fits
8904 into the range of OP0's type.
8906 If so, the conversion is redundant as the earlier SSA_NAME can be
8907 used for the comparison directly if we just massage the constant in the
8908 comparison. */
8909 if (TREE_CODE (op0) == SSA_NAME
8910 && TREE_CODE (op1) == INTEGER_CST)
8912 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
8913 tree innerop;
8915 if (!is_gimple_assign (def_stmt)
8916 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
8917 return false;
8919 innerop = gimple_assign_rhs1 (def_stmt);
8921 if (TREE_CODE (innerop) == SSA_NAME
8922 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
8924 value_range_t *vr = get_value_range (innerop);
8926 if (range_int_cst_p (vr)
8927 && range_fits_type_p (vr,
8928 TYPE_PRECISION (TREE_TYPE (op0)),
8929 TYPE_SIGN (TREE_TYPE (op0)))
8930 && int_fits_type_p (op1, TREE_TYPE (innerop))
8931 /* The range must not have overflowed, or if it did overflow
8932 we must not be wrapping/trapping overflow and optimizing
8933 with strict overflow semantics. */
8934 && ((!is_negative_overflow_infinity (vr->min)
8935 && !is_positive_overflow_infinity (vr->max))
8936 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
8938 /* If the range overflowed and the user has asked for warnings
8939 when strict overflow semantics were used to optimize code,
8940 issue an appropriate warning. */
8941 if ((is_negative_overflow_infinity (vr->min)
8942 || is_positive_overflow_infinity (vr->max))
8943 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
8945 location_t location;
8947 if (!gimple_has_location (stmt))
8948 location = input_location;
8949 else
8950 location = gimple_location (stmt);
8951 warning_at (location, OPT_Wstrict_overflow,
8952 "assuming signed overflow does not occur when "
8953 "simplifying conditional");
8956 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
8957 gimple_cond_set_lhs (stmt, innerop);
8958 gimple_cond_set_rhs (stmt, newconst);
8959 return true;
8964 return false;
8967 /* Simplify a switch statement using the value range of the switch
8968 argument. */
8970 static bool
8971 simplify_switch_using_ranges (gimple stmt)
8973 tree op = gimple_switch_index (stmt);
8974 value_range_t *vr;
8975 bool take_default;
8976 edge e;
8977 edge_iterator ei;
8978 size_t i = 0, j = 0, n, n2;
8979 tree vec2;
8980 switch_update su;
8981 size_t k = 1, l = 0;
8983 if (TREE_CODE (op) == SSA_NAME)
8985 vr = get_value_range (op);
8987 /* We can only handle integer ranges. */
8988 if ((vr->type != VR_RANGE
8989 && vr->type != VR_ANTI_RANGE)
8990 || symbolic_range_p (vr))
8991 return false;
8993 /* Find case label for min/max of the value range. */
8994 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
8996 else if (TREE_CODE (op) == INTEGER_CST)
8998 take_default = !find_case_label_index (stmt, 1, op, &i);
8999 if (take_default)
9001 i = 1;
9002 j = 0;
9004 else
9006 j = i;
9009 else
9010 return false;
9012 n = gimple_switch_num_labels (stmt);
9014 /* Bail out if this is just all edges taken. */
9015 if (i == 1
9016 && j == n - 1
9017 && take_default)
9018 return false;
9020 /* Build a new vector of taken case labels. */
9021 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9022 n2 = 0;
9024 /* Add the default edge, if necessary. */
9025 if (take_default)
9026 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9028 for (; i <= j; ++i, ++n2)
9029 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9031 for (; k <= l; ++k, ++n2)
9032 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9034 /* Mark needed edges. */
9035 for (i = 0; i < n2; ++i)
9037 e = find_edge (gimple_bb (stmt),
9038 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9039 e->aux = (void *)-1;
9042 /* Queue not needed edges for later removal. */
9043 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9045 if (e->aux == (void *)-1)
9047 e->aux = NULL;
9048 continue;
9051 if (dump_file && (dump_flags & TDF_DETAILS))
9053 fprintf (dump_file, "removing unreachable case label\n");
9055 to_remove_edges.safe_push (e);
9056 e->flags &= ~EDGE_EXECUTABLE;
9059 /* And queue an update for the stmt. */
9060 su.stmt = stmt;
9061 su.vec = vec2;
9062 to_update_switch_stmts.safe_push (su);
9063 return false;
9066 /* Simplify an integral conversion from an SSA name in STMT. */
9068 static bool
9069 simplify_conversion_using_ranges (gimple stmt)
9071 tree innerop, middleop, finaltype;
9072 gimple def_stmt;
9073 value_range_t *innervr;
9074 signop inner_sgn, middle_sgn, final_sgn;
9075 unsigned inner_prec, middle_prec, final_prec;
9076 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9078 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9079 if (!INTEGRAL_TYPE_P (finaltype))
9080 return false;
9081 middleop = gimple_assign_rhs1 (stmt);
9082 def_stmt = SSA_NAME_DEF_STMT (middleop);
9083 if (!is_gimple_assign (def_stmt)
9084 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9085 return false;
9086 innerop = gimple_assign_rhs1 (def_stmt);
9087 if (TREE_CODE (innerop) != SSA_NAME
9088 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9089 return false;
9091 /* Get the value-range of the inner operand. */
9092 innervr = get_value_range (innerop);
9093 if (innervr->type != VR_RANGE
9094 || TREE_CODE (innervr->min) != INTEGER_CST
9095 || TREE_CODE (innervr->max) != INTEGER_CST)
9096 return false;
9098 /* Simulate the conversion chain to check if the result is equal if
9099 the middle conversion is removed. */
9100 innermin = wi::to_widest (innervr->min);
9101 innermax = wi::to_widest (innervr->max);
9103 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9104 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9105 final_prec = TYPE_PRECISION (finaltype);
9107 /* If the first conversion is not injective, the second must not
9108 be widening. */
9109 if (wi::gtu_p (innermax - innermin,
9110 wi::mask <widest_int> (middle_prec, false))
9111 && middle_prec < final_prec)
9112 return false;
9113 /* We also want a medium value so that we can track the effect that
9114 narrowing conversions with sign change have. */
9115 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9116 if (inner_sgn == UNSIGNED)
9117 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9118 else
9119 innermed = 0;
9120 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9121 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9122 innermed = innermin;
9124 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9125 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9126 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9127 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9129 /* Require that the final conversion applied to both the original
9130 and the intermediate range produces the same result. */
9131 final_sgn = TYPE_SIGN (finaltype);
9132 if (wi::ext (middlemin, final_prec, final_sgn)
9133 != wi::ext (innermin, final_prec, final_sgn)
9134 || wi::ext (middlemed, final_prec, final_sgn)
9135 != wi::ext (innermed, final_prec, final_sgn)
9136 || wi::ext (middlemax, final_prec, final_sgn)
9137 != wi::ext (innermax, final_prec, final_sgn))
9138 return false;
9140 gimple_assign_set_rhs1 (stmt, innerop);
9141 update_stmt (stmt);
9142 return true;
9145 /* Simplify a conversion from integral SSA name to float in STMT. */
9147 static bool
9148 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9150 tree rhs1 = gimple_assign_rhs1 (stmt);
9151 value_range_t *vr = get_value_range (rhs1);
9152 enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9153 enum machine_mode mode;
9154 tree tem;
9155 gimple conv;
9157 /* We can only handle constant ranges. */
9158 if (vr->type != VR_RANGE
9159 || TREE_CODE (vr->min) != INTEGER_CST
9160 || TREE_CODE (vr->max) != INTEGER_CST)
9161 return false;
9163 /* First check if we can use a signed type in place of an unsigned. */
9164 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9165 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9166 != CODE_FOR_nothing)
9167 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9168 mode = TYPE_MODE (TREE_TYPE (rhs1));
9169 /* If we can do the conversion in the current input mode do nothing. */
9170 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9171 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9172 return false;
9173 /* Otherwise search for a mode we can use, starting from the narrowest
9174 integer mode available. */
9175 else
9177 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9180 /* If we cannot do a signed conversion to float from mode
9181 or if the value-range does not fit in the signed type
9182 try with a wider mode. */
9183 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9184 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9185 break;
9187 mode = GET_MODE_WIDER_MODE (mode);
9188 /* But do not widen the input. Instead leave that to the
9189 optabs expansion code. */
9190 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9191 return false;
9193 while (mode != VOIDmode);
9194 if (mode == VOIDmode)
9195 return false;
9198 /* It works, insert a truncation or sign-change before the
9199 float conversion. */
9200 tem = make_ssa_name (build_nonstandard_integer_type
9201 (GET_MODE_PRECISION (mode), 0), NULL);
9202 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
9203 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9204 gimple_assign_set_rhs1 (stmt, tem);
9205 update_stmt (stmt);
9207 return true;
9210 /* Simplify an internal fn call using ranges if possible. */
9212 static bool
9213 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9215 enum tree_code subcode;
9216 switch (gimple_call_internal_fn (stmt))
9218 case IFN_UBSAN_CHECK_ADD:
9219 subcode = PLUS_EXPR;
9220 break;
9221 case IFN_UBSAN_CHECK_SUB:
9222 subcode = MINUS_EXPR;
9223 break;
9224 case IFN_UBSAN_CHECK_MUL:
9225 subcode = MULT_EXPR;
9226 break;
9227 default:
9228 return false;
9231 value_range_t vr0 = VR_INITIALIZER;
9232 value_range_t vr1 = VR_INITIALIZER;
9233 tree op0 = gimple_call_arg (stmt, 0);
9234 tree op1 = gimple_call_arg (stmt, 1);
9236 if (TREE_CODE (op0) == SSA_NAME)
9237 vr0 = *get_value_range (op0);
9238 else if (TREE_CODE (op0) == INTEGER_CST)
9239 set_value_range_to_value (&vr0, op0, NULL);
9240 else
9241 set_value_range_to_varying (&vr0);
9243 if (TREE_CODE (op1) == SSA_NAME)
9244 vr1 = *get_value_range (op1);
9245 else if (TREE_CODE (op1) == INTEGER_CST)
9246 set_value_range_to_value (&vr1, op1, NULL);
9247 else
9248 set_value_range_to_varying (&vr1);
9250 if (!range_int_cst_p (&vr0))
9252 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9253 optimize at least x = y + 0; x = y - 0; x = y * 0;
9254 and x = y * 1; which never overflow. */
9255 if (!range_int_cst_p (&vr1))
9256 return false;
9257 if (tree_int_cst_sgn (vr1.min) == -1)
9258 return false;
9259 if (compare_tree_int (vr1.max, subcode == MULT_EXPR) == 1)
9260 return false;
9262 else if (!range_int_cst_p (&vr1))
9264 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9265 optimize at least x = 0 + y; x = 0 * y; and x = 1 * y;
9266 which never overflow. */
9267 if (subcode == MINUS_EXPR)
9268 return false;
9269 if (!range_int_cst_p (&vr0))
9270 return false;
9271 if (tree_int_cst_sgn (vr0.min) == -1)
9272 return false;
9273 if (compare_tree_int (vr0.max, subcode == MULT_EXPR) == 1)
9274 return false;
9276 else
9278 tree r1 = int_const_binop (subcode, vr0.min, vr1.min);
9279 tree r2 = int_const_binop (subcode, vr0.max, vr1.max);
9280 if (r1 == NULL_TREE || TREE_OVERFLOW (r1)
9281 || r2 == NULL_TREE || TREE_OVERFLOW (r2))
9282 return false;
9283 if (subcode == MULT_EXPR)
9285 tree r3 = int_const_binop (subcode, vr0.min, vr1.max);
9286 tree r4 = int_const_binop (subcode, vr0.max, vr1.min);
9287 if (r3 == NULL_TREE || TREE_OVERFLOW (r3)
9288 || r4 == NULL_TREE || TREE_OVERFLOW (r4))
9289 return false;
9293 gimple g = gimple_build_assign_with_ops (subcode, gimple_call_lhs (stmt),
9294 op0, op1);
9295 gsi_replace (gsi, g, false);
9296 return true;
9299 /* Simplify STMT using ranges if possible. */
9301 static bool
9302 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9304 gimple stmt = gsi_stmt (*gsi);
9305 if (is_gimple_assign (stmt))
9307 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9308 tree rhs1 = gimple_assign_rhs1 (stmt);
9310 switch (rhs_code)
9312 case EQ_EXPR:
9313 case NE_EXPR:
9314 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9315 if the RHS is zero or one, and the LHS are known to be boolean
9316 values. */
9317 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9318 return simplify_truth_ops_using_ranges (gsi, stmt);
9319 break;
9321 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9322 and BIT_AND_EXPR respectively if the first operand is greater
9323 than zero and the second operand is an exact power of two. */
9324 case TRUNC_DIV_EXPR:
9325 case TRUNC_MOD_EXPR:
9326 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9327 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9328 return simplify_div_or_mod_using_ranges (stmt);
9329 break;
9331 /* Transform ABS (X) into X or -X as appropriate. */
9332 case ABS_EXPR:
9333 if (TREE_CODE (rhs1) == SSA_NAME
9334 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9335 return simplify_abs_using_ranges (stmt);
9336 break;
9338 case BIT_AND_EXPR:
9339 case BIT_IOR_EXPR:
9340 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9341 if all the bits being cleared are already cleared or
9342 all the bits being set are already set. */
9343 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9344 return simplify_bit_ops_using_ranges (gsi, stmt);
9345 break;
9347 CASE_CONVERT:
9348 if (TREE_CODE (rhs1) == SSA_NAME
9349 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9350 return simplify_conversion_using_ranges (stmt);
9351 break;
9353 case FLOAT_EXPR:
9354 if (TREE_CODE (rhs1) == SSA_NAME
9355 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9356 return simplify_float_conversion_using_ranges (gsi, stmt);
9357 break;
9359 default:
9360 break;
9363 else if (gimple_code (stmt) == GIMPLE_COND)
9364 return simplify_cond_using_ranges (stmt);
9365 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9366 return simplify_switch_using_ranges (stmt);
9367 else if (is_gimple_call (stmt)
9368 && gimple_call_internal_p (stmt))
9369 return simplify_internal_call_using_ranges (gsi, stmt);
9371 return false;
9374 /* If the statement pointed by SI has a predicate whose value can be
9375 computed using the value range information computed by VRP, compute
9376 its value and return true. Otherwise, return false. */
9378 static bool
9379 fold_predicate_in (gimple_stmt_iterator *si)
9381 bool assignment_p = false;
9382 tree val;
9383 gimple stmt = gsi_stmt (*si);
9385 if (is_gimple_assign (stmt)
9386 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9388 assignment_p = true;
9389 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9390 gimple_assign_rhs1 (stmt),
9391 gimple_assign_rhs2 (stmt),
9392 stmt);
9394 else if (gimple_code (stmt) == GIMPLE_COND)
9395 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
9396 gimple_cond_lhs (stmt),
9397 gimple_cond_rhs (stmt),
9398 stmt);
9399 else
9400 return false;
9402 if (val)
9404 if (assignment_p)
9405 val = fold_convert (gimple_expr_type (stmt), val);
9407 if (dump_file)
9409 fprintf (dump_file, "Folding predicate ");
9410 print_gimple_expr (dump_file, stmt, 0, 0);
9411 fprintf (dump_file, " to ");
9412 print_generic_expr (dump_file, val, 0);
9413 fprintf (dump_file, "\n");
9416 if (is_gimple_assign (stmt))
9417 gimple_assign_set_rhs_from_tree (si, val);
9418 else
9420 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9421 if (integer_zerop (val))
9422 gimple_cond_make_false (stmt);
9423 else if (integer_onep (val))
9424 gimple_cond_make_true (stmt);
9425 else
9426 gcc_unreachable ();
9429 return true;
9432 return false;
9435 /* Callback for substitute_and_fold folding the stmt at *SI. */
9437 static bool
9438 vrp_fold_stmt (gimple_stmt_iterator *si)
9440 if (fold_predicate_in (si))
9441 return true;
9443 return simplify_stmt_using_ranges (si);
9446 /* Stack of dest,src equivalency pairs that need to be restored after
9447 each attempt to thread a block's incoming edge to an outgoing edge.
9449 A NULL entry is used to mark the end of pairs which need to be
9450 restored. */
9451 static vec<tree> equiv_stack;
9453 /* A trivial wrapper so that we can present the generic jump threading
9454 code with a simple API for simplifying statements. STMT is the
9455 statement we want to simplify, WITHIN_STMT provides the location
9456 for any overflow warnings. */
9458 static tree
9459 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9461 if (gimple_code (stmt) == GIMPLE_COND)
9462 return vrp_evaluate_conditional (gimple_cond_code (stmt),
9463 gimple_cond_lhs (stmt),
9464 gimple_cond_rhs (stmt), within_stmt);
9466 if (gimple_code (stmt) == GIMPLE_ASSIGN)
9468 value_range_t new_vr = VR_INITIALIZER;
9469 tree lhs = gimple_assign_lhs (stmt);
9471 if (TREE_CODE (lhs) == SSA_NAME
9472 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
9473 || POINTER_TYPE_P (TREE_TYPE (lhs))))
9475 extract_range_from_assignment (&new_vr, stmt);
9476 if (range_int_cst_singleton_p (&new_vr))
9477 return new_vr.min;
9481 return NULL_TREE;
9484 /* Blocks which have more than one predecessor and more than
9485 one successor present jump threading opportunities, i.e.,
9486 when the block is reached from a specific predecessor, we
9487 may be able to determine which of the outgoing edges will
9488 be traversed. When this optimization applies, we are able
9489 to avoid conditionals at runtime and we may expose secondary
9490 optimization opportunities.
9492 This routine is effectively a driver for the generic jump
9493 threading code. It basically just presents the generic code
9494 with edges that may be suitable for jump threading.
9496 Unlike DOM, we do not iterate VRP if jump threading was successful.
9497 While iterating may expose new opportunities for VRP, it is expected
9498 those opportunities would be very limited and the compile time cost
9499 to expose those opportunities would be significant.
9501 As jump threading opportunities are discovered, they are registered
9502 for later realization. */
9504 static void
9505 identify_jump_threads (void)
9507 basic_block bb;
9508 gimple dummy;
9509 int i;
9510 edge e;
9512 /* Ugh. When substituting values earlier in this pass we can
9513 wipe the dominance information. So rebuild the dominator
9514 information as we need it within the jump threading code. */
9515 calculate_dominance_info (CDI_DOMINATORS);
9517 /* We do not allow VRP information to be used for jump threading
9518 across a back edge in the CFG. Otherwise it becomes too
9519 difficult to avoid eliminating loop exit tests. Of course
9520 EDGE_DFS_BACK is not accurate at this time so we have to
9521 recompute it. */
9522 mark_dfs_back_edges ();
9524 /* Do not thread across edges we are about to remove. Just marking
9525 them as EDGE_DFS_BACK will do. */
9526 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9527 e->flags |= EDGE_DFS_BACK;
9529 /* Allocate our unwinder stack to unwind any temporary equivalences
9530 that might be recorded. */
9531 equiv_stack.create (20);
9533 /* To avoid lots of silly node creation, we create a single
9534 conditional and just modify it in-place when attempting to
9535 thread jumps. */
9536 dummy = gimple_build_cond (EQ_EXPR,
9537 integer_zero_node, integer_zero_node,
9538 NULL, NULL);
9540 /* Walk through all the blocks finding those which present a
9541 potential jump threading opportunity. We could set this up
9542 as a dominator walker and record data during the walk, but
9543 I doubt it's worth the effort for the classes of jump
9544 threading opportunities we are trying to identify at this
9545 point in compilation. */
9546 FOR_EACH_BB_FN (bb, cfun)
9548 gimple last;
9550 /* If the generic jump threading code does not find this block
9551 interesting, then there is nothing to do. */
9552 if (! potentially_threadable_block (bb))
9553 continue;
9555 /* We only care about blocks ending in a COND_EXPR. While there
9556 may be some value in handling SWITCH_EXPR here, I doubt it's
9557 terribly important. */
9558 last = gsi_stmt (gsi_last_bb (bb));
9560 /* We're basically looking for a switch or any kind of conditional with
9561 integral or pointer type arguments. Note the type of the second
9562 argument will be the same as the first argument, so no need to
9563 check it explicitly. */
9564 if (gimple_code (last) == GIMPLE_SWITCH
9565 || (gimple_code (last) == GIMPLE_COND
9566 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9567 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9568 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9569 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9570 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9572 edge_iterator ei;
9574 /* We've got a block with multiple predecessors and multiple
9575 successors which also ends in a suitable conditional or
9576 switch statement. For each predecessor, see if we can thread
9577 it to a specific successor. */
9578 FOR_EACH_EDGE (e, ei, bb->preds)
9580 /* Do not thread across back edges or abnormal edges
9581 in the CFG. */
9582 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9583 continue;
9585 thread_across_edge (dummy, e, true, &equiv_stack,
9586 simplify_stmt_for_jump_threading);
9591 /* We do not actually update the CFG or SSA graphs at this point as
9592 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9593 handle ASSERT_EXPRs gracefully. */
9596 /* We identified all the jump threading opportunities earlier, but could
9597 not transform the CFG at that time. This routine transforms the
9598 CFG and arranges for the dominator tree to be rebuilt if necessary.
9600 Note the SSA graph update will occur during the normal TODO
9601 processing by the pass manager. */
9602 static void
9603 finalize_jump_threads (void)
9605 thread_through_all_blocks (false);
9606 equiv_stack.release ();
9610 /* Traverse all the blocks folding conditionals with known ranges. */
9612 static void
9613 vrp_finalize (void)
9615 size_t i;
9617 values_propagated = true;
9619 if (dump_file)
9621 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9622 dump_all_value_ranges (dump_file);
9623 fprintf (dump_file, "\n");
9626 substitute_and_fold (op_with_constant_singleton_value_range,
9627 vrp_fold_stmt, false);
9629 if (warn_array_bounds)
9630 check_all_array_refs ();
9632 /* We must identify jump threading opportunities before we release
9633 the datastructures built by VRP. */
9634 identify_jump_threads ();
9636 /* Set value range to non pointer SSA_NAMEs. */
9637 for (i = 0; i < num_vr_values; i++)
9638 if (vr_value[i])
9640 tree name = ssa_name (i);
9642 if (!name
9643 || POINTER_TYPE_P (TREE_TYPE (name))
9644 || (vr_value[i]->type == VR_VARYING)
9645 || (vr_value[i]->type == VR_UNDEFINED))
9646 continue;
9648 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
9649 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
9650 && (vr_value[i]->type == VR_RANGE
9651 || vr_value[i]->type == VR_ANTI_RANGE))
9652 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
9653 vr_value[i]->max);
9656 /* Free allocated memory. */
9657 for (i = 0; i < num_vr_values; i++)
9658 if (vr_value[i])
9660 BITMAP_FREE (vr_value[i]->equiv);
9661 free (vr_value[i]);
9664 free (vr_value);
9665 free (vr_phi_edge_counts);
9667 /* So that we can distinguish between VRP data being available
9668 and not available. */
9669 vr_value = NULL;
9670 vr_phi_edge_counts = NULL;
9674 /* Main entry point to VRP (Value Range Propagation). This pass is
9675 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9676 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9677 Programming Language Design and Implementation, pp. 67-78, 1995.
9678 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9680 This is essentially an SSA-CCP pass modified to deal with ranges
9681 instead of constants.
9683 While propagating ranges, we may find that two or more SSA name
9684 have equivalent, though distinct ranges. For instance,
9686 1 x_9 = p_3->a;
9687 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9688 3 if (p_4 == q_2)
9689 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9690 5 endif
9691 6 if (q_2)
9693 In the code above, pointer p_5 has range [q_2, q_2], but from the
9694 code we can also determine that p_5 cannot be NULL and, if q_2 had
9695 a non-varying range, p_5's range should also be compatible with it.
9697 These equivalences are created by two expressions: ASSERT_EXPR and
9698 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9699 result of another assertion, then we can use the fact that p_5 and
9700 p_4 are equivalent when evaluating p_5's range.
9702 Together with value ranges, we also propagate these equivalences
9703 between names so that we can take advantage of information from
9704 multiple ranges when doing final replacement. Note that this
9705 equivalency relation is transitive but not symmetric.
9707 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9708 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9709 in contexts where that assertion does not hold (e.g., in line 6).
9711 TODO, the main difference between this pass and Patterson's is that
9712 we do not propagate edge probabilities. We only compute whether
9713 edges can be taken or not. That is, instead of having a spectrum
9714 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9715 DON'T KNOW. In the future, it may be worthwhile to propagate
9716 probabilities to aid branch prediction. */
9718 static unsigned int
9719 execute_vrp (void)
9721 int i;
9722 edge e;
9723 switch_update *su;
9725 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9726 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9727 scev_initialize ();
9729 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9730 Inserting assertions may split edges which will invalidate
9731 EDGE_DFS_BACK. */
9732 insert_range_assertions ();
9734 to_remove_edges.create (10);
9735 to_update_switch_stmts.create (5);
9736 threadedge_initialize_values ();
9738 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9739 mark_dfs_back_edges ();
9741 vrp_initialize ();
9742 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
9743 vrp_finalize ();
9745 free_numbers_of_iterations_estimates ();
9747 /* ASSERT_EXPRs must be removed before finalizing jump threads
9748 as finalizing jump threads calls the CFG cleanup code which
9749 does not properly handle ASSERT_EXPRs. */
9750 remove_range_assertions ();
9752 /* If we exposed any new variables, go ahead and put them into
9753 SSA form now, before we handle jump threading. This simplifies
9754 interactions between rewriting of _DECL nodes into SSA form
9755 and rewriting SSA_NAME nodes into SSA form after block
9756 duplication and CFG manipulation. */
9757 update_ssa (TODO_update_ssa);
9759 finalize_jump_threads ();
9761 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9762 CFG in a broken state and requires a cfg_cleanup run. */
9763 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9764 remove_edge (e);
9765 /* Update SWITCH_EXPR case label vector. */
9766 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
9768 size_t j;
9769 size_t n = TREE_VEC_LENGTH (su->vec);
9770 tree label;
9771 gimple_switch_set_num_labels (su->stmt, n);
9772 for (j = 0; j < n; j++)
9773 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
9774 /* As we may have replaced the default label with a regular one
9775 make sure to make it a real default label again. This ensures
9776 optimal expansion. */
9777 label = gimple_switch_label (su->stmt, 0);
9778 CASE_LOW (label) = NULL_TREE;
9779 CASE_HIGH (label) = NULL_TREE;
9782 if (to_remove_edges.length () > 0)
9784 free_dominance_info (CDI_DOMINATORS);
9785 loops_state_set (LOOPS_NEED_FIXUP);
9788 to_remove_edges.release ();
9789 to_update_switch_stmts.release ();
9790 threadedge_finalize_values ();
9792 scev_finalize ();
9793 loop_optimizer_finalize ();
9794 return 0;
9797 namespace {
9799 const pass_data pass_data_vrp =
9801 GIMPLE_PASS, /* type */
9802 "vrp", /* name */
9803 OPTGROUP_NONE, /* optinfo_flags */
9804 TV_TREE_VRP, /* tv_id */
9805 PROP_ssa, /* properties_required */
9806 0, /* properties_provided */
9807 0, /* properties_destroyed */
9808 0, /* todo_flags_start */
9809 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
9812 class pass_vrp : public gimple_opt_pass
9814 public:
9815 pass_vrp (gcc::context *ctxt)
9816 : gimple_opt_pass (pass_data_vrp, ctxt)
9819 /* opt_pass methods: */
9820 opt_pass * clone () { return new pass_vrp (m_ctxt); }
9821 virtual bool gate (function *) { return flag_tree_vrp != 0; }
9822 virtual unsigned int execute (function *) { return execute_vrp (); }
9824 }; // class pass_vrp
9826 } // anon namespace
9828 gimple_opt_pass *
9829 make_pass_vrp (gcc::context *ctxt)
9831 return new pass_vrp (ctxt);