Account for prologue spills in reg_pressure scheduling
[official-gcc.git] / gcc / tree-vrp.c
blobdc5617d17e741a88fefad99d11c7603733ca6548
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 the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
920 otherwise. We only handle additive operations and set NEG to true if the
921 symbol is negated and INV to the invariant part, if any. */
923 static tree
924 get_single_symbol (tree t, bool *neg, tree *inv)
926 bool neg_;
927 tree inv_;
929 if (TREE_CODE (t) == PLUS_EXPR
930 || TREE_CODE (t) == POINTER_PLUS_EXPR
931 || TREE_CODE (t) == MINUS_EXPR)
933 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
935 neg_ = (TREE_CODE (t) == MINUS_EXPR);
936 inv_ = TREE_OPERAND (t, 0);
937 t = TREE_OPERAND (t, 1);
939 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
941 neg_ = false;
942 inv_ = TREE_OPERAND (t, 1);
943 t = TREE_OPERAND (t, 0);
945 else
946 return NULL_TREE;
948 else
950 neg_ = false;
951 inv_ = NULL_TREE;
954 if (TREE_CODE (t) == NEGATE_EXPR)
956 t = TREE_OPERAND (t, 0);
957 neg_ = !neg_;
960 if (TREE_CODE (t) != SSA_NAME)
961 return NULL_TREE;
963 *neg = neg_;
964 *inv = inv_;
965 return t;
968 /* The reverse operation: build a symbolic expression with TYPE
969 from symbol SYM, negated according to NEG, and invariant INV. */
971 static tree
972 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
974 const bool pointer_p = POINTER_TYPE_P (type);
975 tree t = sym;
977 if (neg)
978 t = build1 (NEGATE_EXPR, type, t);
980 if (integer_zerop (inv))
981 return t;
983 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
986 /* Return true if value range VR involves exactly one symbol SYM. */
988 static bool
989 symbolic_range_based_on_p (value_range_t *vr, const_tree sym)
991 bool neg, min_has_symbol, max_has_symbol;
992 tree inv;
994 if (is_gimple_min_invariant (vr->min))
995 min_has_symbol = false;
996 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
997 min_has_symbol = true;
998 else
999 return false;
1001 if (is_gimple_min_invariant (vr->max))
1002 max_has_symbol = false;
1003 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
1004 max_has_symbol = true;
1005 else
1006 return false;
1008 return (min_has_symbol || max_has_symbol);
1011 /* Return true if value range VR uses an overflow infinity. */
1013 static inline bool
1014 overflow_infinity_range_p (value_range_t *vr)
1016 return (vr->type == VR_RANGE
1017 && (is_overflow_infinity (vr->min)
1018 || is_overflow_infinity (vr->max)));
1021 /* Return false if we can not make a valid comparison based on VR;
1022 this will be the case if it uses an overflow infinity and overflow
1023 is not undefined (i.e., -fno-strict-overflow is in effect).
1024 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1025 uses an overflow infinity. */
1027 static bool
1028 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
1030 gcc_assert (vr->type == VR_RANGE);
1031 if (is_overflow_infinity (vr->min))
1033 *strict_overflow_p = true;
1034 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1035 return false;
1037 if (is_overflow_infinity (vr->max))
1039 *strict_overflow_p = true;
1040 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1041 return false;
1043 return true;
1047 /* Return true if the result of assignment STMT is know to be non-negative.
1048 If the return value is based on the assumption that signed overflow is
1049 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1050 *STRICT_OVERFLOW_P.*/
1052 static bool
1053 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1055 enum tree_code code = gimple_assign_rhs_code (stmt);
1056 switch (get_gimple_rhs_class (code))
1058 case GIMPLE_UNARY_RHS:
1059 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1060 gimple_expr_type (stmt),
1061 gimple_assign_rhs1 (stmt),
1062 strict_overflow_p);
1063 case GIMPLE_BINARY_RHS:
1064 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1065 gimple_expr_type (stmt),
1066 gimple_assign_rhs1 (stmt),
1067 gimple_assign_rhs2 (stmt),
1068 strict_overflow_p);
1069 case GIMPLE_TERNARY_RHS:
1070 return false;
1071 case GIMPLE_SINGLE_RHS:
1072 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
1073 strict_overflow_p);
1074 case GIMPLE_INVALID_RHS:
1075 gcc_unreachable ();
1076 default:
1077 gcc_unreachable ();
1081 /* Return true if return value of call STMT is know to be non-negative.
1082 If the return value is based on the assumption that signed overflow is
1083 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1084 *STRICT_OVERFLOW_P.*/
1086 static bool
1087 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1089 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1090 gimple_call_arg (stmt, 0) : NULL_TREE;
1091 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1092 gimple_call_arg (stmt, 1) : NULL_TREE;
1094 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1095 gimple_call_fndecl (stmt),
1096 arg0,
1097 arg1,
1098 strict_overflow_p);
1101 /* Return true if STMT is know to to compute a non-negative value.
1102 If the return value is based on the assumption that signed overflow is
1103 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1104 *STRICT_OVERFLOW_P.*/
1106 static bool
1107 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1109 switch (gimple_code (stmt))
1111 case GIMPLE_ASSIGN:
1112 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1113 case GIMPLE_CALL:
1114 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1115 default:
1116 gcc_unreachable ();
1120 /* Return true if the result of assignment STMT is know to be non-zero.
1121 If the return value is based on the assumption that signed overflow is
1122 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1123 *STRICT_OVERFLOW_P.*/
1125 static bool
1126 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1128 enum tree_code code = gimple_assign_rhs_code (stmt);
1129 switch (get_gimple_rhs_class (code))
1131 case GIMPLE_UNARY_RHS:
1132 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1133 gimple_expr_type (stmt),
1134 gimple_assign_rhs1 (stmt),
1135 strict_overflow_p);
1136 case GIMPLE_BINARY_RHS:
1137 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1138 gimple_expr_type (stmt),
1139 gimple_assign_rhs1 (stmt),
1140 gimple_assign_rhs2 (stmt),
1141 strict_overflow_p);
1142 case GIMPLE_TERNARY_RHS:
1143 return false;
1144 case GIMPLE_SINGLE_RHS:
1145 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1146 strict_overflow_p);
1147 case GIMPLE_INVALID_RHS:
1148 gcc_unreachable ();
1149 default:
1150 gcc_unreachable ();
1154 /* Return true if STMT is known to compute a non-zero value.
1155 If the return value is based on the assumption that signed overflow is
1156 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1157 *STRICT_OVERFLOW_P.*/
1159 static bool
1160 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1162 switch (gimple_code (stmt))
1164 case GIMPLE_ASSIGN:
1165 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1166 case GIMPLE_CALL:
1168 tree fndecl = gimple_call_fndecl (stmt);
1169 if (!fndecl) return false;
1170 if (flag_delete_null_pointer_checks && !flag_check_new
1171 && DECL_IS_OPERATOR_NEW (fndecl)
1172 && !TREE_NOTHROW (fndecl))
1173 return true;
1174 if (flag_delete_null_pointer_checks &&
1175 lookup_attribute ("returns_nonnull",
1176 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1177 return true;
1178 return gimple_alloca_call_p (stmt);
1180 default:
1181 gcc_unreachable ();
1185 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1186 obtained so far. */
1188 static bool
1189 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1191 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1192 return true;
1194 /* If we have an expression of the form &X->a, then the expression
1195 is nonnull if X is nonnull. */
1196 if (is_gimple_assign (stmt)
1197 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1199 tree expr = gimple_assign_rhs1 (stmt);
1200 tree base = get_base_address (TREE_OPERAND (expr, 0));
1202 if (base != NULL_TREE
1203 && TREE_CODE (base) == MEM_REF
1204 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1206 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1207 if (range_is_nonnull (vr))
1208 return true;
1212 return false;
1215 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1216 a gimple invariant, or SSA_NAME +- CST. */
1218 static bool
1219 valid_value_p (tree expr)
1221 if (TREE_CODE (expr) == SSA_NAME)
1222 return true;
1224 if (TREE_CODE (expr) == PLUS_EXPR
1225 || TREE_CODE (expr) == MINUS_EXPR)
1226 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1227 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1229 return is_gimple_min_invariant (expr);
1232 /* Return
1233 1 if VAL < VAL2
1234 0 if !(VAL < VAL2)
1235 -2 if those are incomparable. */
1236 static inline int
1237 operand_less_p (tree val, tree val2)
1239 /* LT is folded faster than GE and others. Inline the common case. */
1240 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1241 return tree_int_cst_lt (val, val2);
1242 else
1244 tree tcmp;
1246 fold_defer_overflow_warnings ();
1248 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1250 fold_undefer_and_ignore_overflow_warnings ();
1252 if (!tcmp
1253 || TREE_CODE (tcmp) != INTEGER_CST)
1254 return -2;
1256 if (!integer_zerop (tcmp))
1257 return 1;
1260 /* val >= val2, not considering overflow infinity. */
1261 if (is_negative_overflow_infinity (val))
1262 return is_negative_overflow_infinity (val2) ? 0 : 1;
1263 else if (is_positive_overflow_infinity (val2))
1264 return is_positive_overflow_infinity (val) ? 0 : 1;
1266 return 0;
1269 /* Compare two values VAL1 and VAL2. Return
1271 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1272 -1 if VAL1 < VAL2,
1273 0 if VAL1 == VAL2,
1274 +1 if VAL1 > VAL2, and
1275 +2 if VAL1 != VAL2
1277 This is similar to tree_int_cst_compare but supports pointer values
1278 and values that cannot be compared at compile time.
1280 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1281 true if the return value is only valid if we assume that signed
1282 overflow is undefined. */
1284 static int
1285 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1287 if (val1 == val2)
1288 return 0;
1290 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1291 both integers. */
1292 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1293 == POINTER_TYPE_P (TREE_TYPE (val2)));
1295 /* Convert the two values into the same type. This is needed because
1296 sizetype causes sign extension even for unsigned types. */
1297 val2 = fold_convert (TREE_TYPE (val1), val2);
1298 STRIP_USELESS_TYPE_CONVERSION (val2);
1300 if ((TREE_CODE (val1) == SSA_NAME
1301 || (TREE_CODE (val1) == NEGATE_EXPR
1302 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1303 || TREE_CODE (val1) == PLUS_EXPR
1304 || TREE_CODE (val1) == MINUS_EXPR)
1305 && (TREE_CODE (val2) == SSA_NAME
1306 || (TREE_CODE (val2) == NEGATE_EXPR
1307 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1308 || TREE_CODE (val2) == PLUS_EXPR
1309 || TREE_CODE (val2) == MINUS_EXPR))
1311 tree n1, c1, n2, c2;
1312 enum tree_code code1, code2;
1314 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1315 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1316 same name, return -2. */
1317 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1319 code1 = SSA_NAME;
1320 n1 = val1;
1321 c1 = NULL_TREE;
1323 else
1325 code1 = TREE_CODE (val1);
1326 n1 = TREE_OPERAND (val1, 0);
1327 c1 = TREE_OPERAND (val1, 1);
1328 if (tree_int_cst_sgn (c1) == -1)
1330 if (is_negative_overflow_infinity (c1))
1331 return -2;
1332 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1333 if (!c1)
1334 return -2;
1335 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1339 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1341 code2 = SSA_NAME;
1342 n2 = val2;
1343 c2 = NULL_TREE;
1345 else
1347 code2 = TREE_CODE (val2);
1348 n2 = TREE_OPERAND (val2, 0);
1349 c2 = TREE_OPERAND (val2, 1);
1350 if (tree_int_cst_sgn (c2) == -1)
1352 if (is_negative_overflow_infinity (c2))
1353 return -2;
1354 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1355 if (!c2)
1356 return -2;
1357 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1361 /* Both values must use the same name. */
1362 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1364 n1 = TREE_OPERAND (n1, 0);
1365 n2 = TREE_OPERAND (n2, 0);
1367 if (n1 != n2)
1368 return -2;
1370 if (code1 == SSA_NAME && code2 == SSA_NAME)
1371 /* NAME == NAME */
1372 return 0;
1374 /* If overflow is defined we cannot simplify more. */
1375 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1376 return -2;
1378 if (strict_overflow_p != NULL
1379 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1380 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1381 *strict_overflow_p = true;
1383 if (code1 == SSA_NAME)
1385 if (code2 == PLUS_EXPR)
1386 /* NAME < NAME + CST */
1387 return -1;
1388 else if (code2 == MINUS_EXPR)
1389 /* NAME > NAME - CST */
1390 return 1;
1392 else if (code1 == PLUS_EXPR)
1394 if (code2 == SSA_NAME)
1395 /* NAME + CST > NAME */
1396 return 1;
1397 else if (code2 == PLUS_EXPR)
1398 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1399 return compare_values_warnv (c1, c2, strict_overflow_p);
1400 else if (code2 == MINUS_EXPR)
1401 /* NAME + CST1 > NAME - CST2 */
1402 return 1;
1404 else if (code1 == MINUS_EXPR)
1406 if (code2 == SSA_NAME)
1407 /* NAME - CST < NAME */
1408 return -1;
1409 else if (code2 == PLUS_EXPR)
1410 /* NAME - CST1 < NAME + CST2 */
1411 return -1;
1412 else if (code2 == MINUS_EXPR)
1413 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1414 C1 and C2 are swapped in the call to compare_values. */
1415 return compare_values_warnv (c2, c1, strict_overflow_p);
1418 gcc_unreachable ();
1421 /* We cannot compare non-constants. */
1422 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1423 return -2;
1425 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1427 /* We cannot compare overflowed values, except for overflow
1428 infinities. */
1429 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1431 if (strict_overflow_p != NULL)
1432 *strict_overflow_p = true;
1433 if (is_negative_overflow_infinity (val1))
1434 return is_negative_overflow_infinity (val2) ? 0 : -1;
1435 else if (is_negative_overflow_infinity (val2))
1436 return 1;
1437 else if (is_positive_overflow_infinity (val1))
1438 return is_positive_overflow_infinity (val2) ? 0 : 1;
1439 else if (is_positive_overflow_infinity (val2))
1440 return -1;
1441 return -2;
1444 return tree_int_cst_compare (val1, val2);
1446 else
1448 tree t;
1450 /* First see if VAL1 and VAL2 are not the same. */
1451 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1452 return 0;
1454 /* If VAL1 is a lower address than VAL2, return -1. */
1455 if (operand_less_p (val1, val2) == 1)
1456 return -1;
1458 /* If VAL1 is a higher address than VAL2, return +1. */
1459 if (operand_less_p (val2, val1) == 1)
1460 return 1;
1462 /* If VAL1 is different than VAL2, return +2.
1463 For integer constants we either have already returned -1 or 1
1464 or they are equivalent. We still might succeed in proving
1465 something about non-trivial operands. */
1466 if (TREE_CODE (val1) != INTEGER_CST
1467 || TREE_CODE (val2) != INTEGER_CST)
1469 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1470 if (t && integer_onep (t))
1471 return 2;
1474 return -2;
1478 /* Compare values like compare_values_warnv, but treat comparisons of
1479 nonconstants which rely on undefined overflow as incomparable. */
1481 static int
1482 compare_values (tree val1, tree val2)
1484 bool sop;
1485 int ret;
1487 sop = false;
1488 ret = compare_values_warnv (val1, val2, &sop);
1489 if (sop
1490 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1491 ret = -2;
1492 return ret;
1496 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1497 0 if VAL is not inside [MIN, MAX],
1498 -2 if we cannot tell either way.
1500 Benchmark compile/20001226-1.c compilation time after changing this
1501 function. */
1503 static inline int
1504 value_inside_range (tree val, tree min, tree max)
1506 int cmp1, cmp2;
1508 cmp1 = operand_less_p (val, min);
1509 if (cmp1 == -2)
1510 return -2;
1511 if (cmp1 == 1)
1512 return 0;
1514 cmp2 = operand_less_p (max, val);
1515 if (cmp2 == -2)
1516 return -2;
1518 return !cmp2;
1522 /* Return true if value ranges VR0 and VR1 have a non-empty
1523 intersection.
1525 Benchmark compile/20001226-1.c compilation time after changing this
1526 function.
1529 static inline bool
1530 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1532 /* The value ranges do not intersect if the maximum of the first range is
1533 less than the minimum of the second range or vice versa.
1534 When those relations are unknown, we can't do any better. */
1535 if (operand_less_p (vr0->max, vr1->min) != 0)
1536 return false;
1537 if (operand_less_p (vr1->max, vr0->min) != 0)
1538 return false;
1539 return true;
1543 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1544 include the value zero, -2 if we cannot tell. */
1546 static inline int
1547 range_includes_zero_p (tree min, tree max)
1549 tree zero = build_int_cst (TREE_TYPE (min), 0);
1550 return value_inside_range (zero, min, max);
1553 /* Return true if *VR is know to only contain nonnegative values. */
1555 static inline bool
1556 value_range_nonnegative_p (value_range_t *vr)
1558 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1559 which would return a useful value should be encoded as a
1560 VR_RANGE. */
1561 if (vr->type == VR_RANGE)
1563 int result = compare_values (vr->min, integer_zero_node);
1564 return (result == 0 || result == 1);
1567 return false;
1570 /* If *VR has a value rante that is a single constant value return that,
1571 otherwise return NULL_TREE. */
1573 static tree
1574 value_range_constant_singleton (value_range_t *vr)
1576 if (vr->type == VR_RANGE
1577 && operand_equal_p (vr->min, vr->max, 0)
1578 && is_gimple_min_invariant (vr->min))
1579 return vr->min;
1581 return NULL_TREE;
1584 /* If OP has a value range with a single constant value return that,
1585 otherwise return NULL_TREE. This returns OP itself if OP is a
1586 constant. */
1588 static tree
1589 op_with_constant_singleton_value_range (tree op)
1591 if (is_gimple_min_invariant (op))
1592 return op;
1594 if (TREE_CODE (op) != SSA_NAME)
1595 return NULL_TREE;
1597 return value_range_constant_singleton (get_value_range (op));
1600 /* Return true if op is in a boolean [0, 1] value-range. */
1602 static bool
1603 op_with_boolean_value_range_p (tree op)
1605 value_range_t *vr;
1607 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1608 return true;
1610 if (integer_zerop (op)
1611 || integer_onep (op))
1612 return true;
1614 if (TREE_CODE (op) != SSA_NAME)
1615 return false;
1617 vr = get_value_range (op);
1618 return (vr->type == VR_RANGE
1619 && integer_zerop (vr->min)
1620 && integer_onep (vr->max));
1623 /* Extract value range information from an ASSERT_EXPR EXPR and store
1624 it in *VR_P. */
1626 static void
1627 extract_range_from_assert (value_range_t *vr_p, tree expr)
1629 tree var, cond, limit, min, max, type;
1630 value_range_t *limit_vr;
1631 enum tree_code cond_code;
1633 var = ASSERT_EXPR_VAR (expr);
1634 cond = ASSERT_EXPR_COND (expr);
1636 gcc_assert (COMPARISON_CLASS_P (cond));
1638 /* Find VAR in the ASSERT_EXPR conditional. */
1639 if (var == TREE_OPERAND (cond, 0)
1640 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1641 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1643 /* If the predicate is of the form VAR COMP LIMIT, then we just
1644 take LIMIT from the RHS and use the same comparison code. */
1645 cond_code = TREE_CODE (cond);
1646 limit = TREE_OPERAND (cond, 1);
1647 cond = TREE_OPERAND (cond, 0);
1649 else
1651 /* If the predicate is of the form LIMIT COMP VAR, then we need
1652 to flip around the comparison code to create the proper range
1653 for VAR. */
1654 cond_code = swap_tree_comparison (TREE_CODE (cond));
1655 limit = TREE_OPERAND (cond, 0);
1656 cond = TREE_OPERAND (cond, 1);
1659 limit = avoid_overflow_infinity (limit);
1661 type = TREE_TYPE (var);
1662 gcc_assert (limit != var);
1664 /* For pointer arithmetic, we only keep track of pointer equality
1665 and inequality. */
1666 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1668 set_value_range_to_varying (vr_p);
1669 return;
1672 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1673 try to use LIMIT's range to avoid creating symbolic ranges
1674 unnecessarily. */
1675 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1677 /* LIMIT's range is only interesting if it has any useful information. */
1678 if (limit_vr
1679 && (limit_vr->type == VR_UNDEFINED
1680 || limit_vr->type == VR_VARYING
1681 || symbolic_range_p (limit_vr)))
1682 limit_vr = NULL;
1684 /* Initially, the new range has the same set of equivalences of
1685 VAR's range. This will be revised before returning the final
1686 value. Since assertions may be chained via mutually exclusive
1687 predicates, we will need to trim the set of equivalences before
1688 we are done. */
1689 gcc_assert (vr_p->equiv == NULL);
1690 add_equivalence (&vr_p->equiv, var);
1692 /* Extract a new range based on the asserted comparison for VAR and
1693 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1694 will only use it for equality comparisons (EQ_EXPR). For any
1695 other kind of assertion, we cannot derive a range from LIMIT's
1696 anti-range that can be used to describe the new range. For
1697 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1698 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1699 no single range for x_2 that could describe LE_EXPR, so we might
1700 as well build the range [b_4, +INF] for it.
1701 One special case we handle is extracting a range from a
1702 range test encoded as (unsigned)var + CST <= limit. */
1703 if (TREE_CODE (cond) == NOP_EXPR
1704 || TREE_CODE (cond) == PLUS_EXPR)
1706 if (TREE_CODE (cond) == PLUS_EXPR)
1708 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1709 TREE_OPERAND (cond, 1));
1710 max = int_const_binop (PLUS_EXPR, limit, min);
1711 cond = TREE_OPERAND (cond, 0);
1713 else
1715 min = build_int_cst (TREE_TYPE (var), 0);
1716 max = limit;
1719 /* Make sure to not set TREE_OVERFLOW on the final type
1720 conversion. We are willingly interpreting large positive
1721 unsigned values as negative signed values here. */
1722 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1723 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1725 /* We can transform a max, min range to an anti-range or
1726 vice-versa. Use set_and_canonicalize_value_range which does
1727 this for us. */
1728 if (cond_code == LE_EXPR)
1729 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1730 min, max, vr_p->equiv);
1731 else if (cond_code == GT_EXPR)
1732 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1733 min, max, vr_p->equiv);
1734 else
1735 gcc_unreachable ();
1737 else if (cond_code == EQ_EXPR)
1739 enum value_range_type range_type;
1741 if (limit_vr)
1743 range_type = limit_vr->type;
1744 min = limit_vr->min;
1745 max = limit_vr->max;
1747 else
1749 range_type = VR_RANGE;
1750 min = limit;
1751 max = limit;
1754 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1756 /* When asserting the equality VAR == LIMIT and LIMIT is another
1757 SSA name, the new range will also inherit the equivalence set
1758 from LIMIT. */
1759 if (TREE_CODE (limit) == SSA_NAME)
1760 add_equivalence (&vr_p->equiv, limit);
1762 else if (cond_code == NE_EXPR)
1764 /* As described above, when LIMIT's range is an anti-range and
1765 this assertion is an inequality (NE_EXPR), then we cannot
1766 derive anything from the anti-range. For instance, if
1767 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1768 not imply that VAR's range is [0, 0]. So, in the case of
1769 anti-ranges, we just assert the inequality using LIMIT and
1770 not its anti-range.
1772 If LIMIT_VR is a range, we can only use it to build a new
1773 anti-range if LIMIT_VR is a single-valued range. For
1774 instance, if LIMIT_VR is [0, 1], the predicate
1775 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1776 Rather, it means that for value 0 VAR should be ~[0, 0]
1777 and for value 1, VAR should be ~[1, 1]. We cannot
1778 represent these ranges.
1780 The only situation in which we can build a valid
1781 anti-range is when LIMIT_VR is a single-valued range
1782 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1783 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1784 if (limit_vr
1785 && limit_vr->type == VR_RANGE
1786 && compare_values (limit_vr->min, limit_vr->max) == 0)
1788 min = limit_vr->min;
1789 max = limit_vr->max;
1791 else
1793 /* In any other case, we cannot use LIMIT's range to build a
1794 valid anti-range. */
1795 min = max = limit;
1798 /* If MIN and MAX cover the whole range for their type, then
1799 just use the original LIMIT. */
1800 if (INTEGRAL_TYPE_P (type)
1801 && vrp_val_is_min (min)
1802 && vrp_val_is_max (max))
1803 min = max = limit;
1805 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1806 min, max, vr_p->equiv);
1808 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1810 min = TYPE_MIN_VALUE (type);
1812 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1813 max = limit;
1814 else
1816 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1817 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1818 LT_EXPR. */
1819 max = limit_vr->max;
1822 /* If the maximum value forces us to be out of bounds, simply punt.
1823 It would be pointless to try and do anything more since this
1824 all should be optimized away above us. */
1825 if ((cond_code == LT_EXPR
1826 && compare_values (max, min) == 0)
1827 || is_overflow_infinity (max))
1828 set_value_range_to_varying (vr_p);
1829 else
1831 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1832 if (cond_code == LT_EXPR)
1834 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1835 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1836 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1837 build_int_cst (TREE_TYPE (max), -1));
1838 else
1839 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1840 build_int_cst (TREE_TYPE (max), 1));
1841 if (EXPR_P (max))
1842 TREE_NO_WARNING (max) = 1;
1845 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1848 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1850 max = TYPE_MAX_VALUE (type);
1852 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1853 min = limit;
1854 else
1856 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1857 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1858 GT_EXPR. */
1859 min = limit_vr->min;
1862 /* If the minimum value forces us to be out of bounds, simply punt.
1863 It would be pointless to try and do anything more since this
1864 all should be optimized away above us. */
1865 if ((cond_code == GT_EXPR
1866 && compare_values (min, max) == 0)
1867 || is_overflow_infinity (min))
1868 set_value_range_to_varying (vr_p);
1869 else
1871 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1872 if (cond_code == GT_EXPR)
1874 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1875 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1876 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1877 build_int_cst (TREE_TYPE (min), -1));
1878 else
1879 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1880 build_int_cst (TREE_TYPE (min), 1));
1881 if (EXPR_P (min))
1882 TREE_NO_WARNING (min) = 1;
1885 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1888 else
1889 gcc_unreachable ();
1891 /* Finally intersect the new range with what we already know about var. */
1892 vrp_intersect_ranges (vr_p, get_value_range (var));
1896 /* Extract range information from SSA name VAR and store it in VR. If
1897 VAR has an interesting range, use it. Otherwise, create the
1898 range [VAR, VAR] and return it. This is useful in situations where
1899 we may have conditionals testing values of VARYING names. For
1900 instance,
1902 x_3 = y_5;
1903 if (x_3 > y_5)
1906 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1907 always false. */
1909 static void
1910 extract_range_from_ssa_name (value_range_t *vr, tree var)
1912 value_range_t *var_vr = get_value_range (var);
1914 if (var_vr->type != VR_VARYING)
1915 copy_value_range (vr, var_vr);
1916 else
1917 set_value_range (vr, VR_RANGE, var, var, NULL);
1919 add_equivalence (&vr->equiv, var);
1923 /* Wrapper around int_const_binop. If the operation overflows and we
1924 are not using wrapping arithmetic, then adjust the result to be
1925 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1926 NULL_TREE if we need to use an overflow infinity representation but
1927 the type does not support it. */
1929 static tree
1930 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1932 tree res;
1934 res = int_const_binop (code, val1, val2);
1936 /* If we are using unsigned arithmetic, operate symbolically
1937 on -INF and +INF as int_const_binop only handles signed overflow. */
1938 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1940 int checkz = compare_values (res, val1);
1941 bool overflow = false;
1943 /* Ensure that res = val1 [+*] val2 >= val1
1944 or that res = val1 - val2 <= val1. */
1945 if ((code == PLUS_EXPR
1946 && !(checkz == 1 || checkz == 0))
1947 || (code == MINUS_EXPR
1948 && !(checkz == 0 || checkz == -1)))
1950 overflow = true;
1952 /* Checking for multiplication overflow is done by dividing the
1953 output of the multiplication by the first input of the
1954 multiplication. If the result of that division operation is
1955 not equal to the second input of the multiplication, then the
1956 multiplication overflowed. */
1957 else if (code == MULT_EXPR && !integer_zerop (val1))
1959 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1960 res,
1961 val1);
1962 int check = compare_values (tmp, val2);
1964 if (check != 0)
1965 overflow = true;
1968 if (overflow)
1970 res = copy_node (res);
1971 TREE_OVERFLOW (res) = 1;
1975 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1976 /* If the singed operation wraps then int_const_binop has done
1977 everything we want. */
1979 /* Signed division of -1/0 overflows and by the time it gets here
1980 returns NULL_TREE. */
1981 else if (!res)
1982 return NULL_TREE;
1983 else if ((TREE_OVERFLOW (res)
1984 && !TREE_OVERFLOW (val1)
1985 && !TREE_OVERFLOW (val2))
1986 || is_overflow_infinity (val1)
1987 || is_overflow_infinity (val2))
1989 /* If the operation overflowed but neither VAL1 nor VAL2 are
1990 overflown, return -INF or +INF depending on the operation
1991 and the combination of signs of the operands. */
1992 int sgn1 = tree_int_cst_sgn (val1);
1993 int sgn2 = tree_int_cst_sgn (val2);
1995 if (needs_overflow_infinity (TREE_TYPE (res))
1996 && !supports_overflow_infinity (TREE_TYPE (res)))
1997 return NULL_TREE;
1999 /* We have to punt on adding infinities of different signs,
2000 since we can't tell what the sign of the result should be.
2001 Likewise for subtracting infinities of the same sign. */
2002 if (((code == PLUS_EXPR && sgn1 != sgn2)
2003 || (code == MINUS_EXPR && sgn1 == sgn2))
2004 && is_overflow_infinity (val1)
2005 && is_overflow_infinity (val2))
2006 return NULL_TREE;
2008 /* Don't try to handle division or shifting of infinities. */
2009 if ((code == TRUNC_DIV_EXPR
2010 || code == FLOOR_DIV_EXPR
2011 || code == CEIL_DIV_EXPR
2012 || code == EXACT_DIV_EXPR
2013 || code == ROUND_DIV_EXPR
2014 || code == RSHIFT_EXPR)
2015 && (is_overflow_infinity (val1)
2016 || is_overflow_infinity (val2)))
2017 return NULL_TREE;
2019 /* Notice that we only need to handle the restricted set of
2020 operations handled by extract_range_from_binary_expr.
2021 Among them, only multiplication, addition and subtraction
2022 can yield overflow without overflown operands because we
2023 are working with integral types only... except in the
2024 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2025 for division too. */
2027 /* For multiplication, the sign of the overflow is given
2028 by the comparison of the signs of the operands. */
2029 if ((code == MULT_EXPR && sgn1 == sgn2)
2030 /* For addition, the operands must be of the same sign
2031 to yield an overflow. Its sign is therefore that
2032 of one of the operands, for example the first. For
2033 infinite operands X + -INF is negative, not positive. */
2034 || (code == PLUS_EXPR
2035 && (sgn1 >= 0
2036 ? !is_negative_overflow_infinity (val2)
2037 : is_positive_overflow_infinity (val2)))
2038 /* For subtraction, non-infinite operands must be of
2039 different signs to yield an overflow. Its sign is
2040 therefore that of the first operand or the opposite of
2041 that of the second operand. A first operand of 0 counts
2042 as positive here, for the corner case 0 - (-INF), which
2043 overflows, but must yield +INF. For infinite operands 0
2044 - INF is negative, not positive. */
2045 || (code == MINUS_EXPR
2046 && (sgn1 >= 0
2047 ? !is_positive_overflow_infinity (val2)
2048 : is_negative_overflow_infinity (val2)))
2049 /* We only get in here with positive shift count, so the
2050 overflow direction is the same as the sign of val1.
2051 Actually rshift does not overflow at all, but we only
2052 handle the case of shifting overflowed -INF and +INF. */
2053 || (code == RSHIFT_EXPR
2054 && sgn1 >= 0)
2055 /* For division, the only case is -INF / -1 = +INF. */
2056 || code == TRUNC_DIV_EXPR
2057 || code == FLOOR_DIV_EXPR
2058 || code == CEIL_DIV_EXPR
2059 || code == EXACT_DIV_EXPR
2060 || code == ROUND_DIV_EXPR)
2061 return (needs_overflow_infinity (TREE_TYPE (res))
2062 ? positive_overflow_infinity (TREE_TYPE (res))
2063 : TYPE_MAX_VALUE (TREE_TYPE (res)));
2064 else
2065 return (needs_overflow_infinity (TREE_TYPE (res))
2066 ? negative_overflow_infinity (TREE_TYPE (res))
2067 : TYPE_MIN_VALUE (TREE_TYPE (res)));
2070 return res;
2074 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2075 bitmask if some bit is unset, it means for all numbers in the range
2076 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2077 bitmask if some bit is set, it means for all numbers in the range
2078 the bit is 1, otherwise it might be 0 or 1. */
2080 static bool
2081 zero_nonzero_bits_from_vr (const tree expr_type,
2082 value_range_t *vr,
2083 wide_int *may_be_nonzero,
2084 wide_int *must_be_nonzero)
2086 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2087 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2088 if (!range_int_cst_p (vr)
2089 || is_overflow_infinity (vr->min)
2090 || is_overflow_infinity (vr->max))
2091 return false;
2093 if (range_int_cst_singleton_p (vr))
2095 *may_be_nonzero = vr->min;
2096 *must_be_nonzero = *may_be_nonzero;
2098 else if (tree_int_cst_sgn (vr->min) >= 0
2099 || tree_int_cst_sgn (vr->max) < 0)
2101 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2102 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2103 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2104 if (xor_mask != 0)
2106 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2107 may_be_nonzero->get_precision ());
2108 *may_be_nonzero = *may_be_nonzero | mask;
2109 *must_be_nonzero = must_be_nonzero->and_not (mask);
2113 return true;
2116 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2117 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2118 false otherwise. If *AR can be represented with a single range
2119 *VR1 will be VR_UNDEFINED. */
2121 static bool
2122 ranges_from_anti_range (value_range_t *ar,
2123 value_range_t *vr0, value_range_t *vr1)
2125 tree type = TREE_TYPE (ar->min);
2127 vr0->type = VR_UNDEFINED;
2128 vr1->type = VR_UNDEFINED;
2130 if (ar->type != VR_ANTI_RANGE
2131 || TREE_CODE (ar->min) != INTEGER_CST
2132 || TREE_CODE (ar->max) != INTEGER_CST
2133 || !vrp_val_min (type)
2134 || !vrp_val_max (type))
2135 return false;
2137 if (!vrp_val_is_min (ar->min))
2139 vr0->type = VR_RANGE;
2140 vr0->min = vrp_val_min (type);
2141 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2143 if (!vrp_val_is_max (ar->max))
2145 vr1->type = VR_RANGE;
2146 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2147 vr1->max = vrp_val_max (type);
2149 if (vr0->type == VR_UNDEFINED)
2151 *vr0 = *vr1;
2152 vr1->type = VR_UNDEFINED;
2155 return vr0->type != VR_UNDEFINED;
2158 /* Helper to extract a value-range *VR for a multiplicative operation
2159 *VR0 CODE *VR1. */
2161 static void
2162 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2163 enum tree_code code,
2164 value_range_t *vr0, value_range_t *vr1)
2166 enum value_range_type type;
2167 tree val[4];
2168 size_t i;
2169 tree min, max;
2170 bool sop;
2171 int cmp;
2173 /* Multiplications, divisions and shifts are a bit tricky to handle,
2174 depending on the mix of signs we have in the two ranges, we
2175 need to operate on different values to get the minimum and
2176 maximum values for the new range. One approach is to figure
2177 out all the variations of range combinations and do the
2178 operations.
2180 However, this involves several calls to compare_values and it
2181 is pretty convoluted. It's simpler to do the 4 operations
2182 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2183 MAX1) and then figure the smallest and largest values to form
2184 the new range. */
2185 gcc_assert (code == MULT_EXPR
2186 || code == TRUNC_DIV_EXPR
2187 || code == FLOOR_DIV_EXPR
2188 || code == CEIL_DIV_EXPR
2189 || code == EXACT_DIV_EXPR
2190 || code == ROUND_DIV_EXPR
2191 || code == RSHIFT_EXPR
2192 || code == LSHIFT_EXPR);
2193 gcc_assert ((vr0->type == VR_RANGE
2194 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2195 && vr0->type == vr1->type);
2197 type = vr0->type;
2199 /* Compute the 4 cross operations. */
2200 sop = false;
2201 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2202 if (val[0] == NULL_TREE)
2203 sop = true;
2205 if (vr1->max == vr1->min)
2206 val[1] = NULL_TREE;
2207 else
2209 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2210 if (val[1] == NULL_TREE)
2211 sop = true;
2214 if (vr0->max == vr0->min)
2215 val[2] = NULL_TREE;
2216 else
2218 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2219 if (val[2] == NULL_TREE)
2220 sop = true;
2223 if (vr0->min == vr0->max || vr1->min == vr1->max)
2224 val[3] = NULL_TREE;
2225 else
2227 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2228 if (val[3] == NULL_TREE)
2229 sop = true;
2232 if (sop)
2234 set_value_range_to_varying (vr);
2235 return;
2238 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2239 of VAL[i]. */
2240 min = val[0];
2241 max = val[0];
2242 for (i = 1; i < 4; i++)
2244 if (!is_gimple_min_invariant (min)
2245 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2246 || !is_gimple_min_invariant (max)
2247 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2248 break;
2250 if (val[i])
2252 if (!is_gimple_min_invariant (val[i])
2253 || (TREE_OVERFLOW (val[i])
2254 && !is_overflow_infinity (val[i])))
2256 /* If we found an overflowed value, set MIN and MAX
2257 to it so that we set the resulting range to
2258 VARYING. */
2259 min = max = val[i];
2260 break;
2263 if (compare_values (val[i], min) == -1)
2264 min = val[i];
2266 if (compare_values (val[i], max) == 1)
2267 max = val[i];
2271 /* If either MIN or MAX overflowed, then set the resulting range to
2272 VARYING. But we do accept an overflow infinity
2273 representation. */
2274 if (min == NULL_TREE
2275 || !is_gimple_min_invariant (min)
2276 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2277 || max == NULL_TREE
2278 || !is_gimple_min_invariant (max)
2279 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2281 set_value_range_to_varying (vr);
2282 return;
2285 /* We punt if:
2286 1) [-INF, +INF]
2287 2) [-INF, +-INF(OVF)]
2288 3) [+-INF(OVF), +INF]
2289 4) [+-INF(OVF), +-INF(OVF)]
2290 We learn nothing when we have INF and INF(OVF) on both sides.
2291 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2292 overflow. */
2293 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2294 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2296 set_value_range_to_varying (vr);
2297 return;
2300 cmp = compare_values (min, max);
2301 if (cmp == -2 || cmp == 1)
2303 /* If the new range has its limits swapped around (MIN > MAX),
2304 then the operation caused one of them to wrap around, mark
2305 the new range VARYING. */
2306 set_value_range_to_varying (vr);
2308 else
2309 set_value_range (vr, type, min, max, NULL);
2312 /* Extract range information from a binary operation CODE based on
2313 the ranges of each of its operands *VR0 and *VR1 with resulting
2314 type EXPR_TYPE. The resulting range is stored in *VR. */
2316 static void
2317 extract_range_from_binary_expr_1 (value_range_t *vr,
2318 enum tree_code code, tree expr_type,
2319 value_range_t *vr0_, value_range_t *vr1_)
2321 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2322 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2323 enum value_range_type type;
2324 tree min = NULL_TREE, max = NULL_TREE;
2325 int cmp;
2327 if (!INTEGRAL_TYPE_P (expr_type)
2328 && !POINTER_TYPE_P (expr_type))
2330 set_value_range_to_varying (vr);
2331 return;
2334 /* Not all binary expressions can be applied to ranges in a
2335 meaningful way. Handle only arithmetic operations. */
2336 if (code != PLUS_EXPR
2337 && code != MINUS_EXPR
2338 && code != POINTER_PLUS_EXPR
2339 && code != MULT_EXPR
2340 && code != TRUNC_DIV_EXPR
2341 && code != FLOOR_DIV_EXPR
2342 && code != CEIL_DIV_EXPR
2343 && code != EXACT_DIV_EXPR
2344 && code != ROUND_DIV_EXPR
2345 && code != TRUNC_MOD_EXPR
2346 && code != RSHIFT_EXPR
2347 && code != LSHIFT_EXPR
2348 && code != MIN_EXPR
2349 && code != MAX_EXPR
2350 && code != BIT_AND_EXPR
2351 && code != BIT_IOR_EXPR
2352 && code != BIT_XOR_EXPR)
2354 set_value_range_to_varying (vr);
2355 return;
2358 /* If both ranges are UNDEFINED, so is the result. */
2359 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2361 set_value_range_to_undefined (vr);
2362 return;
2364 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2365 code. At some point we may want to special-case operations that
2366 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2367 operand. */
2368 else if (vr0.type == VR_UNDEFINED)
2369 set_value_range_to_varying (&vr0);
2370 else if (vr1.type == VR_UNDEFINED)
2371 set_value_range_to_varying (&vr1);
2373 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2374 and express ~[] op X as ([]' op X) U ([]'' op X). */
2375 if (vr0.type == VR_ANTI_RANGE
2376 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2378 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2379 if (vrtem1.type != VR_UNDEFINED)
2381 value_range_t vrres = VR_INITIALIZER;
2382 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2383 &vrtem1, vr1_);
2384 vrp_meet (vr, &vrres);
2386 return;
2388 /* Likewise for X op ~[]. */
2389 if (vr1.type == VR_ANTI_RANGE
2390 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2392 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2393 if (vrtem1.type != VR_UNDEFINED)
2395 value_range_t vrres = VR_INITIALIZER;
2396 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2397 vr0_, &vrtem1);
2398 vrp_meet (vr, &vrres);
2400 return;
2403 /* The type of the resulting value range defaults to VR0.TYPE. */
2404 type = vr0.type;
2406 /* Refuse to operate on VARYING ranges, ranges of different kinds
2407 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2408 because we may be able to derive a useful range even if one of
2409 the operands is VR_VARYING or symbolic range. Similarly for
2410 divisions, MIN/MAX and PLUS/MINUS.
2412 TODO, we may be able to derive anti-ranges in some cases. */
2413 if (code != BIT_AND_EXPR
2414 && code != BIT_IOR_EXPR
2415 && code != TRUNC_DIV_EXPR
2416 && code != FLOOR_DIV_EXPR
2417 && code != CEIL_DIV_EXPR
2418 && code != EXACT_DIV_EXPR
2419 && code != ROUND_DIV_EXPR
2420 && code != TRUNC_MOD_EXPR
2421 && code != MIN_EXPR
2422 && code != MAX_EXPR
2423 && code != PLUS_EXPR
2424 && code != MINUS_EXPR
2425 && (vr0.type == VR_VARYING
2426 || vr1.type == VR_VARYING
2427 || vr0.type != vr1.type
2428 || symbolic_range_p (&vr0)
2429 || symbolic_range_p (&vr1)))
2431 set_value_range_to_varying (vr);
2432 return;
2435 /* Now evaluate the expression to determine the new range. */
2436 if (POINTER_TYPE_P (expr_type))
2438 if (code == MIN_EXPR || code == MAX_EXPR)
2440 /* For MIN/MAX expressions with pointers, we only care about
2441 nullness, if both are non null, then the result is nonnull.
2442 If both are null, then the result is null. Otherwise they
2443 are varying. */
2444 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2445 set_value_range_to_nonnull (vr, expr_type);
2446 else if (range_is_null (&vr0) && range_is_null (&vr1))
2447 set_value_range_to_null (vr, expr_type);
2448 else
2449 set_value_range_to_varying (vr);
2451 else if (code == POINTER_PLUS_EXPR)
2453 /* For pointer types, we are really only interested in asserting
2454 whether the expression evaluates to non-NULL. */
2455 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2456 set_value_range_to_nonnull (vr, expr_type);
2457 else if (range_is_null (&vr0) && range_is_null (&vr1))
2458 set_value_range_to_null (vr, expr_type);
2459 else
2460 set_value_range_to_varying (vr);
2462 else if (code == BIT_AND_EXPR)
2464 /* For pointer types, we are really only interested in asserting
2465 whether the expression evaluates to non-NULL. */
2466 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2467 set_value_range_to_nonnull (vr, expr_type);
2468 else if (range_is_null (&vr0) || range_is_null (&vr1))
2469 set_value_range_to_null (vr, expr_type);
2470 else
2471 set_value_range_to_varying (vr);
2473 else
2474 set_value_range_to_varying (vr);
2476 return;
2479 /* For integer ranges, apply the operation to each end of the
2480 range and see what we end up with. */
2481 if (code == PLUS_EXPR || code == MINUS_EXPR)
2483 const bool minus_p = (code == MINUS_EXPR);
2484 tree min_op0 = vr0.min;
2485 tree min_op1 = minus_p ? vr1.max : vr1.min;
2486 tree max_op0 = vr0.max;
2487 tree max_op1 = minus_p ? vr1.min : vr1.max;
2488 tree sym_min_op0 = NULL_TREE;
2489 tree sym_min_op1 = NULL_TREE;
2490 tree sym_max_op0 = NULL_TREE;
2491 tree sym_max_op1 = NULL_TREE;
2492 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2494 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2495 single-symbolic ranges, try to compute the precise resulting range,
2496 but only if we know that this resulting range will also be constant
2497 or single-symbolic. */
2498 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2499 && (TREE_CODE (min_op0) == INTEGER_CST
2500 || (sym_min_op0
2501 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2502 && (TREE_CODE (min_op1) == INTEGER_CST
2503 || (sym_min_op1
2504 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2505 && (!(sym_min_op0 && sym_min_op1)
2506 || (sym_min_op0 == sym_min_op1
2507 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2508 && (TREE_CODE (max_op0) == INTEGER_CST
2509 || (sym_max_op0
2510 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2511 && (TREE_CODE (max_op1) == INTEGER_CST
2512 || (sym_max_op1
2513 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2514 && (!(sym_max_op0 && sym_max_op1)
2515 || (sym_max_op0 == sym_max_op1
2516 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2518 const signop sgn = TYPE_SIGN (expr_type);
2519 const unsigned int prec = TYPE_PRECISION (expr_type);
2520 wide_int type_min, type_max, wmin, wmax;
2521 int min_ovf = 0;
2522 int max_ovf = 0;
2524 /* Get the lower and upper bounds of the type. */
2525 if (TYPE_OVERFLOW_WRAPS (expr_type))
2527 type_min = wi::min_value (prec, sgn);
2528 type_max = wi::max_value (prec, sgn);
2530 else
2532 type_min = vrp_val_min (expr_type);
2533 type_max = vrp_val_max (expr_type);
2536 /* Combine the lower bounds, if any. */
2537 if (min_op0 && min_op1)
2539 if (minus_p)
2541 wmin = wi::sub (min_op0, min_op1);
2543 /* Check for overflow. */
2544 if (wi::cmp (0, min_op1, sgn)
2545 != wi::cmp (wmin, min_op0, sgn))
2546 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2548 else
2550 wmin = wi::add (min_op0, min_op1);
2552 /* Check for overflow. */
2553 if (wi::cmp (min_op1, 0, sgn)
2554 != wi::cmp (wmin, min_op0, sgn))
2555 min_ovf = wi::cmp (min_op0, wmin, sgn);
2558 else if (min_op0)
2559 wmin = min_op0;
2560 else if (min_op1)
2561 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2562 else
2563 wmin = wi::shwi (0, prec);
2565 /* Combine the upper bounds, if any. */
2566 if (max_op0 && max_op1)
2568 if (minus_p)
2570 wmax = wi::sub (max_op0, max_op1);
2572 /* Check for overflow. */
2573 if (wi::cmp (0, max_op1, sgn)
2574 != wi::cmp (wmax, max_op0, sgn))
2575 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2577 else
2579 wmax = wi::add (max_op0, max_op1);
2581 if (wi::cmp (max_op1, 0, sgn)
2582 != wi::cmp (wmax, max_op0, sgn))
2583 max_ovf = wi::cmp (max_op0, wmax, sgn);
2586 else if (max_op0)
2587 wmax = max_op0;
2588 else if (max_op1)
2589 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2590 else
2591 wmax = wi::shwi (0, prec);
2593 /* Check for type overflow. */
2594 if (min_ovf == 0)
2596 if (wi::cmp (wmin, type_min, sgn) == -1)
2597 min_ovf = -1;
2598 else if (wi::cmp (wmin, type_max, sgn) == 1)
2599 min_ovf = 1;
2601 if (max_ovf == 0)
2603 if (wi::cmp (wmax, type_min, sgn) == -1)
2604 max_ovf = -1;
2605 else if (wi::cmp (wmax, type_max, sgn) == 1)
2606 max_ovf = 1;
2609 /* If we have overflow for the constant part and the resulting
2610 range will be symbolic, drop to VR_VARYING. */
2611 if ((min_ovf && sym_min_op0 != sym_min_op1)
2612 || (max_ovf && sym_max_op0 != sym_max_op1))
2614 set_value_range_to_varying (vr);
2615 return;
2618 if (TYPE_OVERFLOW_WRAPS (expr_type))
2620 /* If overflow wraps, truncate the values and adjust the
2621 range kind and bounds appropriately. */
2622 wide_int tmin = wide_int::from (wmin, prec, sgn);
2623 wide_int tmax = wide_int::from (wmax, prec, sgn);
2624 if (min_ovf == max_ovf)
2626 /* No overflow or both overflow or underflow. The
2627 range kind stays VR_RANGE. */
2628 min = wide_int_to_tree (expr_type, tmin);
2629 max = wide_int_to_tree (expr_type, tmax);
2631 else if (min_ovf == -1 && max_ovf == 1)
2633 /* Underflow and overflow, drop to VR_VARYING. */
2634 set_value_range_to_varying (vr);
2635 return;
2637 else
2639 /* Min underflow or max overflow. The range kind
2640 changes to VR_ANTI_RANGE. */
2641 bool covers = false;
2642 wide_int tem = tmin;
2643 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2644 || (max_ovf == 1 && min_ovf == 0));
2645 type = VR_ANTI_RANGE;
2646 tmin = tmax + 1;
2647 if (wi::cmp (tmin, tmax, sgn) < 0)
2648 covers = true;
2649 tmax = tem - 1;
2650 if (wi::cmp (tmax, tem, sgn) > 0)
2651 covers = true;
2652 /* If the anti-range would cover nothing, drop to varying.
2653 Likewise if the anti-range bounds are outside of the
2654 types values. */
2655 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2657 set_value_range_to_varying (vr);
2658 return;
2660 min = wide_int_to_tree (expr_type, tmin);
2661 max = wide_int_to_tree (expr_type, tmax);
2664 else
2666 /* If overflow does not wrap, saturate to the types min/max
2667 value. */
2668 if (min_ovf == -1)
2670 if (needs_overflow_infinity (expr_type)
2671 && supports_overflow_infinity (expr_type))
2672 min = negative_overflow_infinity (expr_type);
2673 else
2674 min = wide_int_to_tree (expr_type, type_min);
2676 else if (min_ovf == 1)
2678 if (needs_overflow_infinity (expr_type)
2679 && supports_overflow_infinity (expr_type))
2680 min = positive_overflow_infinity (expr_type);
2681 else
2682 min = wide_int_to_tree (expr_type, type_max);
2684 else
2685 min = wide_int_to_tree (expr_type, wmin);
2687 if (max_ovf == -1)
2689 if (needs_overflow_infinity (expr_type)
2690 && supports_overflow_infinity (expr_type))
2691 max = negative_overflow_infinity (expr_type);
2692 else
2693 max = wide_int_to_tree (expr_type, type_min);
2695 else if (max_ovf == 1)
2697 if (needs_overflow_infinity (expr_type)
2698 && supports_overflow_infinity (expr_type))
2699 max = positive_overflow_infinity (expr_type);
2700 else
2701 max = wide_int_to_tree (expr_type, type_max);
2703 else
2704 max = wide_int_to_tree (expr_type, wmax);
2707 if (needs_overflow_infinity (expr_type)
2708 && supports_overflow_infinity (expr_type))
2710 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2711 || (min_op1
2712 && (minus_p
2713 ? is_positive_overflow_infinity (min_op1)
2714 : is_negative_overflow_infinity (min_op1))))
2715 min = negative_overflow_infinity (expr_type);
2716 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2717 || (max_op1
2718 && (minus_p
2719 ? is_negative_overflow_infinity (max_op1)
2720 : is_positive_overflow_infinity (max_op1))))
2721 max = positive_overflow_infinity (expr_type);
2724 /* If the result lower bound is constant, we're done;
2725 otherwise, build the symbolic lower bound. */
2726 if (sym_min_op0 == sym_min_op1)
2728 else if (sym_min_op0)
2729 min = build_symbolic_expr (expr_type, sym_min_op0,
2730 neg_min_op0, min);
2731 else if (sym_min_op1)
2732 min = build_symbolic_expr (expr_type, sym_min_op1,
2733 neg_min_op1 ^ minus_p, min);
2735 /* Likewise for the upper bound. */
2736 if (sym_max_op0 == sym_max_op1)
2738 else if (sym_max_op0)
2739 max = build_symbolic_expr (expr_type, sym_max_op0,
2740 neg_max_op0, max);
2741 else if (sym_max_op1)
2742 max = build_symbolic_expr (expr_type, sym_max_op1,
2743 neg_max_op1 ^ minus_p, max);
2745 else
2747 /* For other cases, for example if we have a PLUS_EXPR with two
2748 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2749 to compute a precise range for such a case.
2750 ??? General even mixed range kind operations can be expressed
2751 by for example transforming ~[3, 5] + [1, 2] to range-only
2752 operations and a union primitive:
2753 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2754 [-INF+1, 4] U [6, +INF(OVF)]
2755 though usually the union is not exactly representable with
2756 a single range or anti-range as the above is
2757 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2758 but one could use a scheme similar to equivalences for this. */
2759 set_value_range_to_varying (vr);
2760 return;
2763 else if (code == MIN_EXPR
2764 || code == MAX_EXPR)
2766 if (vr0.type == VR_RANGE
2767 && !symbolic_range_p (&vr0))
2769 type = VR_RANGE;
2770 if (vr1.type == VR_RANGE
2771 && !symbolic_range_p (&vr1))
2773 /* For operations that make the resulting range directly
2774 proportional to the original ranges, apply the operation to
2775 the same end of each range. */
2776 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2777 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2779 else if (code == MIN_EXPR)
2781 min = vrp_val_min (expr_type);
2782 max = vr0.max;
2784 else if (code == MAX_EXPR)
2786 min = vr0.min;
2787 max = vrp_val_max (expr_type);
2790 else if (vr1.type == VR_RANGE
2791 && !symbolic_range_p (&vr1))
2793 type = VR_RANGE;
2794 if (code == MIN_EXPR)
2796 min = vrp_val_min (expr_type);
2797 max = vr1.max;
2799 else if (code == MAX_EXPR)
2801 min = vr1.min;
2802 max = vrp_val_max (expr_type);
2805 else
2807 set_value_range_to_varying (vr);
2808 return;
2811 else if (code == MULT_EXPR)
2813 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2814 drop to varying. This test requires 2*prec bits if both
2815 operands are signed and 2*prec + 2 bits if either is not. */
2817 signop sign = TYPE_SIGN (expr_type);
2818 unsigned int prec = TYPE_PRECISION (expr_type);
2820 if (range_int_cst_p (&vr0)
2821 && range_int_cst_p (&vr1)
2822 && TYPE_OVERFLOW_WRAPS (expr_type))
2824 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2825 typedef generic_wide_int
2826 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2827 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2828 vrp_int size = sizem1 + 1;
2830 /* Extend the values using the sign of the result to PREC2.
2831 From here on out, everthing is just signed math no matter
2832 what the input types were. */
2833 vrp_int min0 = vrp_int_cst (vr0.min);
2834 vrp_int max0 = vrp_int_cst (vr0.max);
2835 vrp_int min1 = vrp_int_cst (vr1.min);
2836 vrp_int max1 = vrp_int_cst (vr1.max);
2837 /* Canonicalize the intervals. */
2838 if (sign == UNSIGNED)
2840 if (wi::ltu_p (size, min0 + max0))
2842 min0 -= size;
2843 max0 -= size;
2846 if (wi::ltu_p (size, min1 + max1))
2848 min1 -= size;
2849 max1 -= size;
2853 vrp_int prod0 = min0 * min1;
2854 vrp_int prod1 = min0 * max1;
2855 vrp_int prod2 = max0 * min1;
2856 vrp_int prod3 = max0 * max1;
2858 /* Sort the 4 products so that min is in prod0 and max is in
2859 prod3. */
2860 /* min0min1 > max0max1 */
2861 if (wi::gts_p (prod0, prod3))
2863 vrp_int tmp = prod3;
2864 prod3 = prod0;
2865 prod0 = tmp;
2868 /* min0max1 > max0min1 */
2869 if (wi::gts_p (prod1, prod2))
2871 vrp_int tmp = prod2;
2872 prod2 = prod1;
2873 prod1 = tmp;
2876 if (wi::gts_p (prod0, prod1))
2878 vrp_int tmp = prod1;
2879 prod1 = prod0;
2880 prod0 = tmp;
2883 if (wi::gts_p (prod2, prod3))
2885 vrp_int tmp = prod3;
2886 prod3 = prod2;
2887 prod2 = tmp;
2890 /* diff = max - min. */
2891 prod2 = prod3 - prod0;
2892 if (wi::geu_p (prod2, sizem1))
2894 /* the range covers all values. */
2895 set_value_range_to_varying (vr);
2896 return;
2899 /* The following should handle the wrapping and selecting
2900 VR_ANTI_RANGE for us. */
2901 min = wide_int_to_tree (expr_type, prod0);
2902 max = wide_int_to_tree (expr_type, prod3);
2903 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2904 return;
2907 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2908 drop to VR_VARYING. It would take more effort to compute a
2909 precise range for such a case. For example, if we have
2910 op0 == 65536 and op1 == 65536 with their ranges both being
2911 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2912 we cannot claim that the product is in ~[0,0]. Note that we
2913 are guaranteed to have vr0.type == vr1.type at this
2914 point. */
2915 if (vr0.type == VR_ANTI_RANGE
2916 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2918 set_value_range_to_varying (vr);
2919 return;
2922 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2923 return;
2925 else if (code == RSHIFT_EXPR
2926 || code == LSHIFT_EXPR)
2928 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2929 then drop to VR_VARYING. Outside of this range we get undefined
2930 behavior from the shift operation. We cannot even trust
2931 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2932 shifts, and the operation at the tree level may be widened. */
2933 if (range_int_cst_p (&vr1)
2934 && compare_tree_int (vr1.min, 0) >= 0
2935 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2937 if (code == RSHIFT_EXPR)
2939 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2940 return;
2942 /* We can map lshifts by constants to MULT_EXPR handling. */
2943 else if (code == LSHIFT_EXPR
2944 && range_int_cst_singleton_p (&vr1))
2946 bool saved_flag_wrapv;
2947 value_range_t vr1p = VR_INITIALIZER;
2948 vr1p.type = VR_RANGE;
2949 vr1p.min = (wide_int_to_tree
2950 (expr_type,
2951 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2952 TYPE_PRECISION (expr_type))));
2953 vr1p.max = vr1p.min;
2954 /* We have to use a wrapping multiply though as signed overflow
2955 on lshifts is implementation defined in C89. */
2956 saved_flag_wrapv = flag_wrapv;
2957 flag_wrapv = 1;
2958 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2959 &vr0, &vr1p);
2960 flag_wrapv = saved_flag_wrapv;
2961 return;
2963 else if (code == LSHIFT_EXPR
2964 && range_int_cst_p (&vr0))
2966 int prec = TYPE_PRECISION (expr_type);
2967 int overflow_pos = prec;
2968 int bound_shift;
2969 wide_int low_bound, high_bound;
2970 bool uns = TYPE_UNSIGNED (expr_type);
2971 bool in_bounds = false;
2973 if (!uns)
2974 overflow_pos -= 1;
2976 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2977 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2978 overflow. However, for that to happen, vr1.max needs to be
2979 zero, which means vr1 is a singleton range of zero, which
2980 means it should be handled by the previous LSHIFT_EXPR
2981 if-clause. */
2982 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2983 wide_int complement = ~(bound - 1);
2985 if (uns)
2987 low_bound = bound;
2988 high_bound = complement;
2989 if (wi::ltu_p (vr0.max, low_bound))
2991 /* [5, 6] << [1, 2] == [10, 24]. */
2992 /* We're shifting out only zeroes, the value increases
2993 monotonically. */
2994 in_bounds = true;
2996 else if (wi::ltu_p (high_bound, vr0.min))
2998 /* [0xffffff00, 0xffffffff] << [1, 2]
2999 == [0xfffffc00, 0xfffffffe]. */
3000 /* We're shifting out only ones, the value decreases
3001 monotonically. */
3002 in_bounds = true;
3005 else
3007 /* [-1, 1] << [1, 2] == [-4, 4]. */
3008 low_bound = complement;
3009 high_bound = bound;
3010 if (wi::lts_p (vr0.max, high_bound)
3011 && wi::lts_p (low_bound, vr0.min))
3013 /* For non-negative numbers, we're shifting out only
3014 zeroes, the value increases monotonically.
3015 For negative numbers, we're shifting out only ones, the
3016 value decreases monotomically. */
3017 in_bounds = true;
3021 if (in_bounds)
3023 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3024 return;
3028 set_value_range_to_varying (vr);
3029 return;
3031 else if (code == TRUNC_DIV_EXPR
3032 || code == FLOOR_DIV_EXPR
3033 || code == CEIL_DIV_EXPR
3034 || code == EXACT_DIV_EXPR
3035 || code == ROUND_DIV_EXPR)
3037 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3039 /* For division, if op1 has VR_RANGE but op0 does not, something
3040 can be deduced just from that range. Say [min, max] / [4, max]
3041 gives [min / 4, max / 4] range. */
3042 if (vr1.type == VR_RANGE
3043 && !symbolic_range_p (&vr1)
3044 && range_includes_zero_p (vr1.min, vr1.max) == 0)
3046 vr0.type = type = VR_RANGE;
3047 vr0.min = vrp_val_min (expr_type);
3048 vr0.max = vrp_val_max (expr_type);
3050 else
3052 set_value_range_to_varying (vr);
3053 return;
3057 /* For divisions, if flag_non_call_exceptions is true, we must
3058 not eliminate a division by zero. */
3059 if (cfun->can_throw_non_call_exceptions
3060 && (vr1.type != VR_RANGE
3061 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3063 set_value_range_to_varying (vr);
3064 return;
3067 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3068 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3069 include 0. */
3070 if (vr0.type == VR_RANGE
3071 && (vr1.type != VR_RANGE
3072 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3074 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
3075 int cmp;
3077 min = NULL_TREE;
3078 max = NULL_TREE;
3079 if (TYPE_UNSIGNED (expr_type)
3080 || value_range_nonnegative_p (&vr1))
3082 /* For unsigned division or when divisor is known
3083 to be non-negative, the range has to cover
3084 all numbers from 0 to max for positive max
3085 and all numbers from min to 0 for negative min. */
3086 cmp = compare_values (vr0.max, zero);
3087 if (cmp == -1)
3088 max = zero;
3089 else if (cmp == 0 || cmp == 1)
3090 max = vr0.max;
3091 else
3092 type = VR_VARYING;
3093 cmp = compare_values (vr0.min, zero);
3094 if (cmp == 1)
3095 min = zero;
3096 else if (cmp == 0 || cmp == -1)
3097 min = vr0.min;
3098 else
3099 type = VR_VARYING;
3101 else
3103 /* Otherwise the range is -max .. max or min .. -min
3104 depending on which bound is bigger in absolute value,
3105 as the division can change the sign. */
3106 abs_extent_range (vr, vr0.min, vr0.max);
3107 return;
3109 if (type == VR_VARYING)
3111 set_value_range_to_varying (vr);
3112 return;
3115 else
3117 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3118 return;
3121 else if (code == TRUNC_MOD_EXPR)
3123 if (vr1.type != VR_RANGE
3124 || range_includes_zero_p (vr1.min, vr1.max) != 0
3125 || vrp_val_is_min (vr1.min))
3127 set_value_range_to_varying (vr);
3128 return;
3130 type = VR_RANGE;
3131 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3132 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
3133 if (tree_int_cst_lt (max, vr1.max))
3134 max = vr1.max;
3135 max = int_const_binop (MINUS_EXPR, max, build_int_cst (TREE_TYPE (max), 1));
3136 /* If the dividend is non-negative the modulus will be
3137 non-negative as well. */
3138 if (TYPE_UNSIGNED (expr_type)
3139 || value_range_nonnegative_p (&vr0))
3140 min = build_int_cst (TREE_TYPE (max), 0);
3141 else
3142 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
3144 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3146 bool int_cst_range0, int_cst_range1;
3147 wide_int may_be_nonzero0, may_be_nonzero1;
3148 wide_int must_be_nonzero0, must_be_nonzero1;
3150 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3151 &may_be_nonzero0,
3152 &must_be_nonzero0);
3153 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3154 &may_be_nonzero1,
3155 &must_be_nonzero1);
3157 type = VR_RANGE;
3158 if (code == BIT_AND_EXPR)
3160 min = wide_int_to_tree (expr_type,
3161 must_be_nonzero0 & must_be_nonzero1);
3162 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3163 /* If both input ranges contain only negative values we can
3164 truncate the result range maximum to the minimum of the
3165 input range maxima. */
3166 if (int_cst_range0 && int_cst_range1
3167 && tree_int_cst_sgn (vr0.max) < 0
3168 && tree_int_cst_sgn (vr1.max) < 0)
3170 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3171 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3173 /* If either input range contains only non-negative values
3174 we can truncate the result range maximum to the respective
3175 maximum of the input range. */
3176 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3177 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3178 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3179 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3180 max = wide_int_to_tree (expr_type, wmax);
3182 else if (code == BIT_IOR_EXPR)
3184 max = wide_int_to_tree (expr_type,
3185 may_be_nonzero0 | may_be_nonzero1);
3186 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3187 /* If the input ranges contain only positive values we can
3188 truncate the minimum of the result range to the maximum
3189 of the input range minima. */
3190 if (int_cst_range0 && int_cst_range1
3191 && tree_int_cst_sgn (vr0.min) >= 0
3192 && tree_int_cst_sgn (vr1.min) >= 0)
3194 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3195 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3197 /* If either input range contains only negative values
3198 we can truncate the minimum of the result range to the
3199 respective minimum range. */
3200 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3201 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3202 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3203 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3204 min = wide_int_to_tree (expr_type, wmin);
3206 else if (code == BIT_XOR_EXPR)
3208 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3209 | ~(may_be_nonzero0 | may_be_nonzero1));
3210 wide_int result_one_bits
3211 = (must_be_nonzero0.and_not (may_be_nonzero1)
3212 | must_be_nonzero1.and_not (may_be_nonzero0));
3213 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3214 min = wide_int_to_tree (expr_type, result_one_bits);
3215 /* If the range has all positive or all negative values the
3216 result is better than VARYING. */
3217 if (tree_int_cst_sgn (min) < 0
3218 || tree_int_cst_sgn (max) >= 0)
3220 else
3221 max = min = NULL_TREE;
3224 else
3225 gcc_unreachable ();
3227 /* If either MIN or MAX overflowed, then set the resulting range to
3228 VARYING. But we do accept an overflow infinity representation. */
3229 if (min == NULL_TREE
3230 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3231 || max == NULL_TREE
3232 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3234 set_value_range_to_varying (vr);
3235 return;
3238 /* We punt if:
3239 1) [-INF, +INF]
3240 2) [-INF, +-INF(OVF)]
3241 3) [+-INF(OVF), +INF]
3242 4) [+-INF(OVF), +-INF(OVF)]
3243 We learn nothing when we have INF and INF(OVF) on both sides.
3244 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3245 overflow. */
3246 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3247 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3249 set_value_range_to_varying (vr);
3250 return;
3253 cmp = compare_values (min, max);
3254 if (cmp == -2 || cmp == 1)
3256 /* If the new range has its limits swapped around (MIN > MAX),
3257 then the operation caused one of them to wrap around, mark
3258 the new range VARYING. */
3259 set_value_range_to_varying (vr);
3261 else
3262 set_value_range (vr, type, min, max, NULL);
3265 /* Extract range information from a binary expression OP0 CODE OP1 based on
3266 the ranges of each of its operands with resulting type EXPR_TYPE.
3267 The resulting range is stored in *VR. */
3269 static void
3270 extract_range_from_binary_expr (value_range_t *vr,
3271 enum tree_code code,
3272 tree expr_type, tree op0, tree op1)
3274 value_range_t vr0 = VR_INITIALIZER;
3275 value_range_t vr1 = VR_INITIALIZER;
3277 /* Get value ranges for each operand. For constant operands, create
3278 a new value range with the operand to simplify processing. */
3279 if (TREE_CODE (op0) == SSA_NAME)
3280 vr0 = *(get_value_range (op0));
3281 else if (is_gimple_min_invariant (op0))
3282 set_value_range_to_value (&vr0, op0, NULL);
3283 else
3284 set_value_range_to_varying (&vr0);
3286 if (TREE_CODE (op1) == SSA_NAME)
3287 vr1 = *(get_value_range (op1));
3288 else if (is_gimple_min_invariant (op1))
3289 set_value_range_to_value (&vr1, op1, NULL);
3290 else
3291 set_value_range_to_varying (&vr1);
3293 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3295 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3296 and based on the other operand, for example if it was deduced from a
3297 symbolic comparison. When a bound of the range of the first operand
3298 is invariant, we set the corresponding bound of the new range to INF
3299 in order to avoid recursing on the range of the second operand. */
3300 if (vr->type == VR_VARYING
3301 && (code == PLUS_EXPR || code == MINUS_EXPR)
3302 && TREE_CODE (op1) == SSA_NAME
3303 && vr0.type == VR_RANGE
3304 && symbolic_range_based_on_p (&vr0, op1))
3306 const bool minus_p = (code == MINUS_EXPR);
3307 value_range_t n_vr1 = VR_INITIALIZER;
3309 /* Try with VR0 and [-INF, OP1]. */
3310 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3311 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3313 /* Try with VR0 and [OP1, +INF]. */
3314 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3315 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3317 /* Try with VR0 and [OP1, OP1]. */
3318 else
3319 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3321 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3324 if (vr->type == VR_VARYING
3325 && (code == PLUS_EXPR || code == MINUS_EXPR)
3326 && TREE_CODE (op0) == SSA_NAME
3327 && vr1.type == VR_RANGE
3328 && symbolic_range_based_on_p (&vr1, op0))
3330 const bool minus_p = (code == MINUS_EXPR);
3331 value_range_t n_vr0 = VR_INITIALIZER;
3333 /* Try with [-INF, OP0] and VR1. */
3334 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3335 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3337 /* Try with [OP0, +INF] and VR1. */
3338 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3339 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3341 /* Try with [OP0, OP0] and VR1. */
3342 else
3343 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3345 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3349 /* Extract range information from a unary operation CODE based on
3350 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3351 The The resulting range is stored in *VR. */
3353 static void
3354 extract_range_from_unary_expr_1 (value_range_t *vr,
3355 enum tree_code code, tree type,
3356 value_range_t *vr0_, tree op0_type)
3358 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3360 /* VRP only operates on integral and pointer types. */
3361 if (!(INTEGRAL_TYPE_P (op0_type)
3362 || POINTER_TYPE_P (op0_type))
3363 || !(INTEGRAL_TYPE_P (type)
3364 || POINTER_TYPE_P (type)))
3366 set_value_range_to_varying (vr);
3367 return;
3370 /* If VR0 is UNDEFINED, so is the result. */
3371 if (vr0.type == VR_UNDEFINED)
3373 set_value_range_to_undefined (vr);
3374 return;
3377 /* Handle operations that we express in terms of others. */
3378 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3380 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3381 copy_value_range (vr, &vr0);
3382 return;
3384 else if (code == NEGATE_EXPR)
3386 /* -X is simply 0 - X, so re-use existing code that also handles
3387 anti-ranges fine. */
3388 value_range_t zero = VR_INITIALIZER;
3389 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3390 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3391 return;
3393 else if (code == BIT_NOT_EXPR)
3395 /* ~X is simply -1 - X, so re-use existing code that also handles
3396 anti-ranges fine. */
3397 value_range_t minusone = VR_INITIALIZER;
3398 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3399 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3400 type, &minusone, &vr0);
3401 return;
3404 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3405 and express op ~[] as (op []') U (op []''). */
3406 if (vr0.type == VR_ANTI_RANGE
3407 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3409 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3410 if (vrtem1.type != VR_UNDEFINED)
3412 value_range_t vrres = VR_INITIALIZER;
3413 extract_range_from_unary_expr_1 (&vrres, code, type,
3414 &vrtem1, op0_type);
3415 vrp_meet (vr, &vrres);
3417 return;
3420 if (CONVERT_EXPR_CODE_P (code))
3422 tree inner_type = op0_type;
3423 tree outer_type = type;
3425 /* If the expression evaluates to a pointer, we are only interested in
3426 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3427 if (POINTER_TYPE_P (type))
3429 if (range_is_nonnull (&vr0))
3430 set_value_range_to_nonnull (vr, type);
3431 else if (range_is_null (&vr0))
3432 set_value_range_to_null (vr, type);
3433 else
3434 set_value_range_to_varying (vr);
3435 return;
3438 /* If VR0 is varying and we increase the type precision, assume
3439 a full range for the following transformation. */
3440 if (vr0.type == VR_VARYING
3441 && INTEGRAL_TYPE_P (inner_type)
3442 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3444 vr0.type = VR_RANGE;
3445 vr0.min = TYPE_MIN_VALUE (inner_type);
3446 vr0.max = TYPE_MAX_VALUE (inner_type);
3449 /* If VR0 is a constant range or anti-range and the conversion is
3450 not truncating we can convert the min and max values and
3451 canonicalize the resulting range. Otherwise we can do the
3452 conversion if the size of the range is less than what the
3453 precision of the target type can represent and the range is
3454 not an anti-range. */
3455 if ((vr0.type == VR_RANGE
3456 || vr0.type == VR_ANTI_RANGE)
3457 && TREE_CODE (vr0.min) == INTEGER_CST
3458 && TREE_CODE (vr0.max) == INTEGER_CST
3459 && (!is_overflow_infinity (vr0.min)
3460 || (vr0.type == VR_RANGE
3461 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3462 && needs_overflow_infinity (outer_type)
3463 && supports_overflow_infinity (outer_type)))
3464 && (!is_overflow_infinity (vr0.max)
3465 || (vr0.type == VR_RANGE
3466 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3467 && needs_overflow_infinity (outer_type)
3468 && supports_overflow_infinity (outer_type)))
3469 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3470 || (vr0.type == VR_RANGE
3471 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3472 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3473 size_int (TYPE_PRECISION (outer_type)))))))
3475 tree new_min, new_max;
3476 if (is_overflow_infinity (vr0.min))
3477 new_min = negative_overflow_infinity (outer_type);
3478 else
3479 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3480 0, false);
3481 if (is_overflow_infinity (vr0.max))
3482 new_max = positive_overflow_infinity (outer_type);
3483 else
3484 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3485 0, false);
3486 set_and_canonicalize_value_range (vr, vr0.type,
3487 new_min, new_max, NULL);
3488 return;
3491 set_value_range_to_varying (vr);
3492 return;
3494 else if (code == ABS_EXPR)
3496 tree min, max;
3497 int cmp;
3499 /* Pass through vr0 in the easy cases. */
3500 if (TYPE_UNSIGNED (type)
3501 || value_range_nonnegative_p (&vr0))
3503 copy_value_range (vr, &vr0);
3504 return;
3507 /* For the remaining varying or symbolic ranges we can't do anything
3508 useful. */
3509 if (vr0.type == VR_VARYING
3510 || symbolic_range_p (&vr0))
3512 set_value_range_to_varying (vr);
3513 return;
3516 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3517 useful range. */
3518 if (!TYPE_OVERFLOW_UNDEFINED (type)
3519 && ((vr0.type == VR_RANGE
3520 && vrp_val_is_min (vr0.min))
3521 || (vr0.type == VR_ANTI_RANGE
3522 && !vrp_val_is_min (vr0.min))))
3524 set_value_range_to_varying (vr);
3525 return;
3528 /* ABS_EXPR may flip the range around, if the original range
3529 included negative values. */
3530 if (is_overflow_infinity (vr0.min))
3531 min = positive_overflow_infinity (type);
3532 else if (!vrp_val_is_min (vr0.min))
3533 min = fold_unary_to_constant (code, type, vr0.min);
3534 else if (!needs_overflow_infinity (type))
3535 min = TYPE_MAX_VALUE (type);
3536 else if (supports_overflow_infinity (type))
3537 min = positive_overflow_infinity (type);
3538 else
3540 set_value_range_to_varying (vr);
3541 return;
3544 if (is_overflow_infinity (vr0.max))
3545 max = positive_overflow_infinity (type);
3546 else if (!vrp_val_is_min (vr0.max))
3547 max = fold_unary_to_constant (code, type, vr0.max);
3548 else if (!needs_overflow_infinity (type))
3549 max = TYPE_MAX_VALUE (type);
3550 else if (supports_overflow_infinity (type)
3551 /* We shouldn't generate [+INF, +INF] as set_value_range
3552 doesn't like this and ICEs. */
3553 && !is_positive_overflow_infinity (min))
3554 max = positive_overflow_infinity (type);
3555 else
3557 set_value_range_to_varying (vr);
3558 return;
3561 cmp = compare_values (min, max);
3563 /* If a VR_ANTI_RANGEs contains zero, then we have
3564 ~[-INF, min(MIN, MAX)]. */
3565 if (vr0.type == VR_ANTI_RANGE)
3567 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3569 /* Take the lower of the two values. */
3570 if (cmp != 1)
3571 max = min;
3573 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3574 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3575 flag_wrapv is set and the original anti-range doesn't include
3576 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3577 if (TYPE_OVERFLOW_WRAPS (type))
3579 tree type_min_value = TYPE_MIN_VALUE (type);
3581 min = (vr0.min != type_min_value
3582 ? int_const_binop (PLUS_EXPR, type_min_value,
3583 build_int_cst (TREE_TYPE (type_min_value), 1))
3584 : type_min_value);
3586 else
3588 if (overflow_infinity_range_p (&vr0))
3589 min = negative_overflow_infinity (type);
3590 else
3591 min = TYPE_MIN_VALUE (type);
3594 else
3596 /* All else has failed, so create the range [0, INF], even for
3597 flag_wrapv since TYPE_MIN_VALUE is in the original
3598 anti-range. */
3599 vr0.type = VR_RANGE;
3600 min = build_int_cst (type, 0);
3601 if (needs_overflow_infinity (type))
3603 if (supports_overflow_infinity (type))
3604 max = positive_overflow_infinity (type);
3605 else
3607 set_value_range_to_varying (vr);
3608 return;
3611 else
3612 max = TYPE_MAX_VALUE (type);
3616 /* If the range contains zero then we know that the minimum value in the
3617 range will be zero. */
3618 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3620 if (cmp == 1)
3621 max = min;
3622 min = build_int_cst (type, 0);
3624 else
3626 /* If the range was reversed, swap MIN and MAX. */
3627 if (cmp == 1)
3629 tree t = min;
3630 min = max;
3631 max = t;
3635 cmp = compare_values (min, max);
3636 if (cmp == -2 || cmp == 1)
3638 /* If the new range has its limits swapped around (MIN > MAX),
3639 then the operation caused one of them to wrap around, mark
3640 the new range VARYING. */
3641 set_value_range_to_varying (vr);
3643 else
3644 set_value_range (vr, vr0.type, min, max, NULL);
3645 return;
3648 /* For unhandled operations fall back to varying. */
3649 set_value_range_to_varying (vr);
3650 return;
3654 /* Extract range information from a unary expression CODE OP0 based on
3655 the range of its operand with resulting type TYPE.
3656 The resulting range is stored in *VR. */
3658 static void
3659 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3660 tree type, tree op0)
3662 value_range_t vr0 = VR_INITIALIZER;
3664 /* Get value ranges for the operand. For constant operands, create
3665 a new value range with the operand to simplify processing. */
3666 if (TREE_CODE (op0) == SSA_NAME)
3667 vr0 = *(get_value_range (op0));
3668 else if (is_gimple_min_invariant (op0))
3669 set_value_range_to_value (&vr0, op0, NULL);
3670 else
3671 set_value_range_to_varying (&vr0);
3673 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3677 /* Extract range information from a conditional expression STMT based on
3678 the ranges of each of its operands and the expression code. */
3680 static void
3681 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3683 tree op0, op1;
3684 value_range_t vr0 = VR_INITIALIZER;
3685 value_range_t vr1 = VR_INITIALIZER;
3687 /* Get value ranges for each operand. For constant operands, create
3688 a new value range with the operand to simplify processing. */
3689 op0 = gimple_assign_rhs2 (stmt);
3690 if (TREE_CODE (op0) == SSA_NAME)
3691 vr0 = *(get_value_range (op0));
3692 else if (is_gimple_min_invariant (op0))
3693 set_value_range_to_value (&vr0, op0, NULL);
3694 else
3695 set_value_range_to_varying (&vr0);
3697 op1 = gimple_assign_rhs3 (stmt);
3698 if (TREE_CODE (op1) == SSA_NAME)
3699 vr1 = *(get_value_range (op1));
3700 else if (is_gimple_min_invariant (op1))
3701 set_value_range_to_value (&vr1, op1, NULL);
3702 else
3703 set_value_range_to_varying (&vr1);
3705 /* The resulting value range is the union of the operand ranges */
3706 copy_value_range (vr, &vr0);
3707 vrp_meet (vr, &vr1);
3711 /* Extract range information from a comparison expression EXPR based
3712 on the range of its operand and the expression code. */
3714 static void
3715 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3716 tree type, tree op0, tree op1)
3718 bool sop = false;
3719 tree val;
3721 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3722 NULL);
3724 /* A disadvantage of using a special infinity as an overflow
3725 representation is that we lose the ability to record overflow
3726 when we don't have an infinity. So we have to ignore a result
3727 which relies on overflow. */
3729 if (val && !is_overflow_infinity (val) && !sop)
3731 /* Since this expression was found on the RHS of an assignment,
3732 its type may be different from _Bool. Convert VAL to EXPR's
3733 type. */
3734 val = fold_convert (type, val);
3735 if (is_gimple_min_invariant (val))
3736 set_value_range_to_value (vr, val, vr->equiv);
3737 else
3738 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3740 else
3741 /* The result of a comparison is always true or false. */
3742 set_value_range_to_truthvalue (vr, type);
3745 /* Try to derive a nonnegative or nonzero range out of STMT relying
3746 primarily on generic routines in fold in conjunction with range data.
3747 Store the result in *VR */
3749 static void
3750 extract_range_basic (value_range_t *vr, gimple stmt)
3752 bool sop = false;
3753 tree type = gimple_expr_type (stmt);
3755 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3757 tree fndecl = gimple_call_fndecl (stmt), arg;
3758 int mini, maxi, zerov = 0, prec;
3760 switch (DECL_FUNCTION_CODE (fndecl))
3762 case BUILT_IN_CONSTANT_P:
3763 /* If the call is __builtin_constant_p and the argument is a
3764 function parameter resolve it to false. This avoids bogus
3765 array bound warnings.
3766 ??? We could do this as early as inlining is finished. */
3767 arg = gimple_call_arg (stmt, 0);
3768 if (TREE_CODE (arg) == SSA_NAME
3769 && SSA_NAME_IS_DEFAULT_DEF (arg)
3770 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3772 set_value_range_to_null (vr, type);
3773 return;
3775 break;
3776 /* Both __builtin_ffs* and __builtin_popcount return
3777 [0, prec]. */
3778 CASE_INT_FN (BUILT_IN_FFS):
3779 CASE_INT_FN (BUILT_IN_POPCOUNT):
3780 arg = gimple_call_arg (stmt, 0);
3781 prec = TYPE_PRECISION (TREE_TYPE (arg));
3782 mini = 0;
3783 maxi = prec;
3784 if (TREE_CODE (arg) == SSA_NAME)
3786 value_range_t *vr0 = get_value_range (arg);
3787 /* If arg is non-zero, then ffs or popcount
3788 are non-zero. */
3789 if (((vr0->type == VR_RANGE
3790 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3791 || (vr0->type == VR_ANTI_RANGE
3792 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3793 && !is_overflow_infinity (vr0->min)
3794 && !is_overflow_infinity (vr0->max))
3795 mini = 1;
3796 /* If some high bits are known to be zero,
3797 we can decrease the maximum. */
3798 if (vr0->type == VR_RANGE
3799 && TREE_CODE (vr0->max) == INTEGER_CST
3800 && !operand_less_p (vr0->min,
3801 build_zero_cst (TREE_TYPE (vr0->min)))
3802 && !is_overflow_infinity (vr0->max))
3803 maxi = tree_floor_log2 (vr0->max) + 1;
3805 goto bitop_builtin;
3806 /* __builtin_parity* returns [0, 1]. */
3807 CASE_INT_FN (BUILT_IN_PARITY):
3808 mini = 0;
3809 maxi = 1;
3810 goto bitop_builtin;
3811 /* __builtin_c[lt]z* return [0, prec-1], except for
3812 when the argument is 0, but that is undefined behavior.
3813 On many targets where the CLZ RTL or optab value is defined
3814 for 0 the value is prec, so include that in the range
3815 by default. */
3816 CASE_INT_FN (BUILT_IN_CLZ):
3817 arg = gimple_call_arg (stmt, 0);
3818 prec = TYPE_PRECISION (TREE_TYPE (arg));
3819 mini = 0;
3820 maxi = prec;
3821 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3822 != CODE_FOR_nothing
3823 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3824 zerov)
3825 /* Handle only the single common value. */
3826 && zerov != prec)
3827 /* Magic value to give up, unless vr0 proves
3828 arg is non-zero. */
3829 mini = -2;
3830 if (TREE_CODE (arg) == SSA_NAME)
3832 value_range_t *vr0 = get_value_range (arg);
3833 /* From clz of VR_RANGE minimum we can compute
3834 result maximum. */
3835 if (vr0->type == VR_RANGE
3836 && TREE_CODE (vr0->min) == INTEGER_CST
3837 && !is_overflow_infinity (vr0->min))
3839 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3840 if (maxi != prec)
3841 mini = 0;
3843 else if (vr0->type == VR_ANTI_RANGE
3844 && integer_zerop (vr0->min)
3845 && !is_overflow_infinity (vr0->min))
3847 maxi = prec - 1;
3848 mini = 0;
3850 if (mini == -2)
3851 break;
3852 /* From clz of VR_RANGE maximum we can compute
3853 result minimum. */
3854 if (vr0->type == VR_RANGE
3855 && TREE_CODE (vr0->max) == INTEGER_CST
3856 && !is_overflow_infinity (vr0->max))
3858 mini = prec - 1 - tree_floor_log2 (vr0->max);
3859 if (mini == prec)
3860 break;
3863 if (mini == -2)
3864 break;
3865 goto bitop_builtin;
3866 /* __builtin_ctz* return [0, prec-1], except for
3867 when the argument is 0, but that is undefined behavior.
3868 If there is a ctz optab for this mode and
3869 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3870 otherwise just assume 0 won't be seen. */
3871 CASE_INT_FN (BUILT_IN_CTZ):
3872 arg = gimple_call_arg (stmt, 0);
3873 prec = TYPE_PRECISION (TREE_TYPE (arg));
3874 mini = 0;
3875 maxi = prec - 1;
3876 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3877 != CODE_FOR_nothing
3878 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3879 zerov))
3881 /* Handle only the two common values. */
3882 if (zerov == -1)
3883 mini = -1;
3884 else if (zerov == prec)
3885 maxi = prec;
3886 else
3887 /* Magic value to give up, unless vr0 proves
3888 arg is non-zero. */
3889 mini = -2;
3891 if (TREE_CODE (arg) == SSA_NAME)
3893 value_range_t *vr0 = get_value_range (arg);
3894 /* If arg is non-zero, then use [0, prec - 1]. */
3895 if (((vr0->type == VR_RANGE
3896 && integer_nonzerop (vr0->min))
3897 || (vr0->type == VR_ANTI_RANGE
3898 && integer_zerop (vr0->min)))
3899 && !is_overflow_infinity (vr0->min))
3901 mini = 0;
3902 maxi = prec - 1;
3904 /* If some high bits are known to be zero,
3905 we can decrease the result maximum. */
3906 if (vr0->type == VR_RANGE
3907 && TREE_CODE (vr0->max) == INTEGER_CST
3908 && !is_overflow_infinity (vr0->max))
3910 maxi = tree_floor_log2 (vr0->max);
3911 /* For vr0 [0, 0] give up. */
3912 if (maxi == -1)
3913 break;
3916 if (mini == -2)
3917 break;
3918 goto bitop_builtin;
3919 /* __builtin_clrsb* returns [0, prec-1]. */
3920 CASE_INT_FN (BUILT_IN_CLRSB):
3921 arg = gimple_call_arg (stmt, 0);
3922 prec = TYPE_PRECISION (TREE_TYPE (arg));
3923 mini = 0;
3924 maxi = prec - 1;
3925 goto bitop_builtin;
3926 bitop_builtin:
3927 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3928 build_int_cst (type, maxi), NULL);
3929 return;
3930 default:
3931 break;
3934 else if (is_gimple_call (stmt)
3935 && gimple_call_internal_p (stmt))
3937 enum tree_code subcode = ERROR_MARK;
3938 switch (gimple_call_internal_fn (stmt))
3940 case IFN_UBSAN_CHECK_ADD:
3941 subcode = PLUS_EXPR;
3942 break;
3943 case IFN_UBSAN_CHECK_SUB:
3944 subcode = MINUS_EXPR;
3945 break;
3946 case IFN_UBSAN_CHECK_MUL:
3947 subcode = MULT_EXPR;
3948 break;
3949 default:
3950 break;
3952 if (subcode != ERROR_MARK)
3954 bool saved_flag_wrapv = flag_wrapv;
3955 /* Pretend the arithmetics is wrapping. If there is
3956 any overflow, we'll complain, but will actually do
3957 wrapping operation. */
3958 flag_wrapv = 1;
3959 extract_range_from_binary_expr (vr, subcode, type,
3960 gimple_call_arg (stmt, 0),
3961 gimple_call_arg (stmt, 1));
3962 flag_wrapv = saved_flag_wrapv;
3964 /* If for both arguments vrp_valueize returned non-NULL,
3965 this should have been already folded and if not, it
3966 wasn't folded because of overflow. Avoid removing the
3967 UBSAN_CHECK_* calls in that case. */
3968 if (vr->type == VR_RANGE
3969 && (vr->min == vr->max
3970 || operand_equal_p (vr->min, vr->max, 0)))
3971 set_value_range_to_varying (vr);
3972 return;
3975 if (INTEGRAL_TYPE_P (type)
3976 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3977 set_value_range_to_nonnegative (vr, type,
3978 sop || stmt_overflow_infinity (stmt));
3979 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3980 && !sop)
3981 set_value_range_to_nonnull (vr, type);
3982 else
3983 set_value_range_to_varying (vr);
3987 /* Try to compute a useful range out of assignment STMT and store it
3988 in *VR. */
3990 static void
3991 extract_range_from_assignment (value_range_t *vr, gimple stmt)
3993 enum tree_code code = gimple_assign_rhs_code (stmt);
3995 if (code == ASSERT_EXPR)
3996 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3997 else if (code == SSA_NAME)
3998 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3999 else if (TREE_CODE_CLASS (code) == tcc_binary)
4000 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4001 gimple_expr_type (stmt),
4002 gimple_assign_rhs1 (stmt),
4003 gimple_assign_rhs2 (stmt));
4004 else if (TREE_CODE_CLASS (code) == tcc_unary)
4005 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4006 gimple_expr_type (stmt),
4007 gimple_assign_rhs1 (stmt));
4008 else if (code == COND_EXPR)
4009 extract_range_from_cond_expr (vr, stmt);
4010 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4011 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4012 gimple_expr_type (stmt),
4013 gimple_assign_rhs1 (stmt),
4014 gimple_assign_rhs2 (stmt));
4015 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4016 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4017 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4018 else
4019 set_value_range_to_varying (vr);
4021 if (vr->type == VR_VARYING)
4022 extract_range_basic (vr, stmt);
4025 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4026 would be profitable to adjust VR using scalar evolution information
4027 for VAR. If so, update VR with the new limits. */
4029 static void
4030 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
4031 gimple stmt, tree var)
4033 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4034 enum ev_direction dir;
4036 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4037 better opportunities than a regular range, but I'm not sure. */
4038 if (vr->type == VR_ANTI_RANGE)
4039 return;
4041 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4043 /* Like in PR19590, scev can return a constant function. */
4044 if (is_gimple_min_invariant (chrec))
4046 set_value_range_to_value (vr, chrec, vr->equiv);
4047 return;
4050 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4051 return;
4053 init = initial_condition_in_loop_num (chrec, loop->num);
4054 tem = op_with_constant_singleton_value_range (init);
4055 if (tem)
4056 init = tem;
4057 step = evolution_part_in_loop_num (chrec, loop->num);
4058 tem = op_with_constant_singleton_value_range (step);
4059 if (tem)
4060 step = tem;
4062 /* If STEP is symbolic, we can't know whether INIT will be the
4063 minimum or maximum value in the range. Also, unless INIT is
4064 a simple expression, compare_values and possibly other functions
4065 in tree-vrp won't be able to handle it. */
4066 if (step == NULL_TREE
4067 || !is_gimple_min_invariant (step)
4068 || !valid_value_p (init))
4069 return;
4071 dir = scev_direction (chrec);
4072 if (/* Do not adjust ranges if we do not know whether the iv increases
4073 or decreases, ... */
4074 dir == EV_DIR_UNKNOWN
4075 /* ... or if it may wrap. */
4076 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4077 true))
4078 return;
4080 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4081 negative_overflow_infinity and positive_overflow_infinity,
4082 because we have concluded that the loop probably does not
4083 wrap. */
4085 type = TREE_TYPE (var);
4086 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4087 tmin = lower_bound_in_type (type, type);
4088 else
4089 tmin = TYPE_MIN_VALUE (type);
4090 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4091 tmax = upper_bound_in_type (type, type);
4092 else
4093 tmax = TYPE_MAX_VALUE (type);
4095 /* Try to use estimated number of iterations for the loop to constrain the
4096 final value in the evolution. */
4097 if (TREE_CODE (step) == INTEGER_CST
4098 && is_gimple_val (init)
4099 && (TREE_CODE (init) != SSA_NAME
4100 || get_value_range (init)->type == VR_RANGE))
4102 widest_int nit;
4104 /* We are only entering here for loop header PHI nodes, so using
4105 the number of latch executions is the correct thing to use. */
4106 if (max_loop_iterations (loop, &nit))
4108 value_range_t maxvr = VR_INITIALIZER;
4109 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4110 bool overflow;
4112 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4113 &overflow);
4114 /* If the multiplication overflowed we can't do a meaningful
4115 adjustment. Likewise if the result doesn't fit in the type
4116 of the induction variable. For a signed type we have to
4117 check whether the result has the expected signedness which
4118 is that of the step as number of iterations is unsigned. */
4119 if (!overflow
4120 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4121 && (sgn == UNSIGNED
4122 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4124 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4125 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4126 TREE_TYPE (init), init, tem);
4127 /* Likewise if the addition did. */
4128 if (maxvr.type == VR_RANGE)
4130 tmin = maxvr.min;
4131 tmax = maxvr.max;
4137 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4139 min = tmin;
4140 max = tmax;
4142 /* For VARYING or UNDEFINED ranges, just about anything we get
4143 from scalar evolutions should be better. */
4145 if (dir == EV_DIR_DECREASES)
4146 max = init;
4147 else
4148 min = init;
4150 else if (vr->type == VR_RANGE)
4152 min = vr->min;
4153 max = vr->max;
4155 if (dir == EV_DIR_DECREASES)
4157 /* INIT is the maximum value. If INIT is lower than VR->MAX
4158 but no smaller than VR->MIN, set VR->MAX to INIT. */
4159 if (compare_values (init, max) == -1)
4160 max = init;
4162 /* According to the loop information, the variable does not
4163 overflow. If we think it does, probably because of an
4164 overflow due to arithmetic on a different INF value,
4165 reset now. */
4166 if (is_negative_overflow_infinity (min)
4167 || compare_values (min, tmin) == -1)
4168 min = tmin;
4171 else
4173 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4174 if (compare_values (init, min) == 1)
4175 min = init;
4177 if (is_positive_overflow_infinity (max)
4178 || compare_values (tmax, max) == -1)
4179 max = tmax;
4182 else
4183 return;
4185 /* If we just created an invalid range with the minimum
4186 greater than the maximum, we fail conservatively.
4187 This should happen only in unreachable
4188 parts of code, or for invalid programs. */
4189 if (compare_values (min, max) == 1
4190 || (is_negative_overflow_infinity (min)
4191 && is_positive_overflow_infinity (max)))
4192 return;
4194 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4198 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4200 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4201 all the values in the ranges.
4203 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4205 - Return NULL_TREE if it is not always possible to determine the
4206 value of the comparison.
4208 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4209 overflow infinity was used in the test. */
4212 static tree
4213 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4214 bool *strict_overflow_p)
4216 /* VARYING or UNDEFINED ranges cannot be compared. */
4217 if (vr0->type == VR_VARYING
4218 || vr0->type == VR_UNDEFINED
4219 || vr1->type == VR_VARYING
4220 || vr1->type == VR_UNDEFINED)
4221 return NULL_TREE;
4223 /* Anti-ranges need to be handled separately. */
4224 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4226 /* If both are anti-ranges, then we cannot compute any
4227 comparison. */
4228 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4229 return NULL_TREE;
4231 /* These comparisons are never statically computable. */
4232 if (comp == GT_EXPR
4233 || comp == GE_EXPR
4234 || comp == LT_EXPR
4235 || comp == LE_EXPR)
4236 return NULL_TREE;
4238 /* Equality can be computed only between a range and an
4239 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4240 if (vr0->type == VR_RANGE)
4242 /* To simplify processing, make VR0 the anti-range. */
4243 value_range_t *tmp = vr0;
4244 vr0 = vr1;
4245 vr1 = tmp;
4248 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4250 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4251 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4252 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4254 return NULL_TREE;
4257 if (!usable_range_p (vr0, strict_overflow_p)
4258 || !usable_range_p (vr1, strict_overflow_p))
4259 return NULL_TREE;
4261 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4262 operands around and change the comparison code. */
4263 if (comp == GT_EXPR || comp == GE_EXPR)
4265 value_range_t *tmp;
4266 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4267 tmp = vr0;
4268 vr0 = vr1;
4269 vr1 = tmp;
4272 if (comp == EQ_EXPR)
4274 /* Equality may only be computed if both ranges represent
4275 exactly one value. */
4276 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4277 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4279 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4280 strict_overflow_p);
4281 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4282 strict_overflow_p);
4283 if (cmp_min == 0 && cmp_max == 0)
4284 return boolean_true_node;
4285 else if (cmp_min != -2 && cmp_max != -2)
4286 return boolean_false_node;
4288 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4289 else if (compare_values_warnv (vr0->min, vr1->max,
4290 strict_overflow_p) == 1
4291 || compare_values_warnv (vr1->min, vr0->max,
4292 strict_overflow_p) == 1)
4293 return boolean_false_node;
4295 return NULL_TREE;
4297 else if (comp == NE_EXPR)
4299 int cmp1, cmp2;
4301 /* If VR0 is completely to the left or completely to the right
4302 of VR1, they are always different. Notice that we need to
4303 make sure that both comparisons yield similar results to
4304 avoid comparing values that cannot be compared at
4305 compile-time. */
4306 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4307 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4308 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4309 return boolean_true_node;
4311 /* If VR0 and VR1 represent a single value and are identical,
4312 return false. */
4313 else if (compare_values_warnv (vr0->min, vr0->max,
4314 strict_overflow_p) == 0
4315 && compare_values_warnv (vr1->min, vr1->max,
4316 strict_overflow_p) == 0
4317 && compare_values_warnv (vr0->min, vr1->min,
4318 strict_overflow_p) == 0
4319 && compare_values_warnv (vr0->max, vr1->max,
4320 strict_overflow_p) == 0)
4321 return boolean_false_node;
4323 /* Otherwise, they may or may not be different. */
4324 else
4325 return NULL_TREE;
4327 else if (comp == LT_EXPR || comp == LE_EXPR)
4329 int tst;
4331 /* If VR0 is to the left of VR1, return true. */
4332 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4333 if ((comp == LT_EXPR && tst == -1)
4334 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4336 if (overflow_infinity_range_p (vr0)
4337 || overflow_infinity_range_p (vr1))
4338 *strict_overflow_p = true;
4339 return boolean_true_node;
4342 /* If VR0 is to the right of VR1, return false. */
4343 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4344 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4345 || (comp == LE_EXPR && tst == 1))
4347 if (overflow_infinity_range_p (vr0)
4348 || overflow_infinity_range_p (vr1))
4349 *strict_overflow_p = true;
4350 return boolean_false_node;
4353 /* Otherwise, we don't know. */
4354 return NULL_TREE;
4357 gcc_unreachable ();
4361 /* Given a value range VR, a value VAL and a comparison code COMP, return
4362 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4363 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4364 always returns false. Return NULL_TREE if it is not always
4365 possible to determine the value of the comparison. Also set
4366 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4367 infinity was used in the test. */
4369 static tree
4370 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4371 bool *strict_overflow_p)
4373 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4374 return NULL_TREE;
4376 /* Anti-ranges need to be handled separately. */
4377 if (vr->type == VR_ANTI_RANGE)
4379 /* For anti-ranges, the only predicates that we can compute at
4380 compile time are equality and inequality. */
4381 if (comp == GT_EXPR
4382 || comp == GE_EXPR
4383 || comp == LT_EXPR
4384 || comp == LE_EXPR)
4385 return NULL_TREE;
4387 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4388 if (value_inside_range (val, vr->min, vr->max) == 1)
4389 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4391 return NULL_TREE;
4394 if (!usable_range_p (vr, strict_overflow_p))
4395 return NULL_TREE;
4397 if (comp == EQ_EXPR)
4399 /* EQ_EXPR may only be computed if VR represents exactly
4400 one value. */
4401 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4403 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4404 if (cmp == 0)
4405 return boolean_true_node;
4406 else if (cmp == -1 || cmp == 1 || cmp == 2)
4407 return boolean_false_node;
4409 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4410 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4411 return boolean_false_node;
4413 return NULL_TREE;
4415 else if (comp == NE_EXPR)
4417 /* If VAL is not inside VR, then they are always different. */
4418 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4419 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4420 return boolean_true_node;
4422 /* If VR represents exactly one value equal to VAL, then return
4423 false. */
4424 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4425 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4426 return boolean_false_node;
4428 /* Otherwise, they may or may not be different. */
4429 return NULL_TREE;
4431 else if (comp == LT_EXPR || comp == LE_EXPR)
4433 int tst;
4435 /* If VR is to the left of VAL, return true. */
4436 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4437 if ((comp == LT_EXPR && tst == -1)
4438 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4440 if (overflow_infinity_range_p (vr))
4441 *strict_overflow_p = true;
4442 return boolean_true_node;
4445 /* If VR is to the right of VAL, return false. */
4446 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4447 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4448 || (comp == LE_EXPR && tst == 1))
4450 if (overflow_infinity_range_p (vr))
4451 *strict_overflow_p = true;
4452 return boolean_false_node;
4455 /* Otherwise, we don't know. */
4456 return NULL_TREE;
4458 else if (comp == GT_EXPR || comp == GE_EXPR)
4460 int tst;
4462 /* If VR is to the right of VAL, return true. */
4463 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4464 if ((comp == GT_EXPR && tst == 1)
4465 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4467 if (overflow_infinity_range_p (vr))
4468 *strict_overflow_p = true;
4469 return boolean_true_node;
4472 /* If VR is to the left of VAL, return false. */
4473 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4474 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4475 || (comp == GE_EXPR && tst == -1))
4477 if (overflow_infinity_range_p (vr))
4478 *strict_overflow_p = true;
4479 return boolean_false_node;
4482 /* Otherwise, we don't know. */
4483 return NULL_TREE;
4486 gcc_unreachable ();
4490 /* Debugging dumps. */
4492 void dump_value_range (FILE *, value_range_t *);
4493 void debug_value_range (value_range_t *);
4494 void dump_all_value_ranges (FILE *);
4495 void debug_all_value_ranges (void);
4496 void dump_vr_equiv (FILE *, bitmap);
4497 void debug_vr_equiv (bitmap);
4500 /* Dump value range VR to FILE. */
4502 void
4503 dump_value_range (FILE *file, value_range_t *vr)
4505 if (vr == NULL)
4506 fprintf (file, "[]");
4507 else if (vr->type == VR_UNDEFINED)
4508 fprintf (file, "UNDEFINED");
4509 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4511 tree type = TREE_TYPE (vr->min);
4513 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4515 if (is_negative_overflow_infinity (vr->min))
4516 fprintf (file, "-INF(OVF)");
4517 else if (INTEGRAL_TYPE_P (type)
4518 && !TYPE_UNSIGNED (type)
4519 && vrp_val_is_min (vr->min))
4520 fprintf (file, "-INF");
4521 else
4522 print_generic_expr (file, vr->min, 0);
4524 fprintf (file, ", ");
4526 if (is_positive_overflow_infinity (vr->max))
4527 fprintf (file, "+INF(OVF)");
4528 else if (INTEGRAL_TYPE_P (type)
4529 && vrp_val_is_max (vr->max))
4530 fprintf (file, "+INF");
4531 else
4532 print_generic_expr (file, vr->max, 0);
4534 fprintf (file, "]");
4536 if (vr->equiv)
4538 bitmap_iterator bi;
4539 unsigned i, c = 0;
4541 fprintf (file, " EQUIVALENCES: { ");
4543 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4545 print_generic_expr (file, ssa_name (i), 0);
4546 fprintf (file, " ");
4547 c++;
4550 fprintf (file, "} (%u elements)", c);
4553 else if (vr->type == VR_VARYING)
4554 fprintf (file, "VARYING");
4555 else
4556 fprintf (file, "INVALID RANGE");
4560 /* Dump value range VR to stderr. */
4562 DEBUG_FUNCTION void
4563 debug_value_range (value_range_t *vr)
4565 dump_value_range (stderr, vr);
4566 fprintf (stderr, "\n");
4570 /* Dump value ranges of all SSA_NAMEs to FILE. */
4572 void
4573 dump_all_value_ranges (FILE *file)
4575 size_t i;
4577 for (i = 0; i < num_vr_values; i++)
4579 if (vr_value[i])
4581 print_generic_expr (file, ssa_name (i), 0);
4582 fprintf (file, ": ");
4583 dump_value_range (file, vr_value[i]);
4584 fprintf (file, "\n");
4588 fprintf (file, "\n");
4592 /* Dump all value ranges to stderr. */
4594 DEBUG_FUNCTION void
4595 debug_all_value_ranges (void)
4597 dump_all_value_ranges (stderr);
4601 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4602 create a new SSA name N and return the assertion assignment
4603 'N = ASSERT_EXPR <V, V OP W>'. */
4605 static gimple
4606 build_assert_expr_for (tree cond, tree v)
4608 tree a;
4609 gimple assertion;
4611 gcc_assert (TREE_CODE (v) == SSA_NAME
4612 && COMPARISON_CLASS_P (cond));
4614 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4615 assertion = gimple_build_assign (NULL_TREE, a);
4617 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4618 operand of the ASSERT_EXPR. Create it so the new name and the old one
4619 are registered in the replacement table so that we can fix the SSA web
4620 after adding all the ASSERT_EXPRs. */
4621 create_new_def_for (v, assertion, NULL);
4623 return assertion;
4627 /* Return false if EXPR is a predicate expression involving floating
4628 point values. */
4630 static inline bool
4631 fp_predicate (gimple stmt)
4633 GIMPLE_CHECK (stmt, GIMPLE_COND);
4635 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4638 /* If the range of values taken by OP can be inferred after STMT executes,
4639 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4640 describes the inferred range. Return true if a range could be
4641 inferred. */
4643 static bool
4644 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4646 *val_p = NULL_TREE;
4647 *comp_code_p = ERROR_MARK;
4649 /* Do not attempt to infer anything in names that flow through
4650 abnormal edges. */
4651 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4652 return false;
4654 /* Similarly, don't infer anything from statements that may throw
4655 exceptions. ??? Relax this requirement? */
4656 if (stmt_could_throw_p (stmt))
4657 return false;
4659 /* If STMT is the last statement of a basic block with no normal
4660 successors, there is no point inferring anything about any of its
4661 operands. We would not be able to find a proper insertion point
4662 for the assertion, anyway. */
4663 if (stmt_ends_bb_p (stmt))
4665 edge_iterator ei;
4666 edge e;
4668 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4669 if (!(e->flags & EDGE_ABNORMAL))
4670 break;
4671 if (e == NULL)
4672 return false;
4675 if (infer_nonnull_range (stmt, op, true, true))
4677 *val_p = build_int_cst (TREE_TYPE (op), 0);
4678 *comp_code_p = NE_EXPR;
4679 return true;
4682 return false;
4686 void dump_asserts_for (FILE *, tree);
4687 void debug_asserts_for (tree);
4688 void dump_all_asserts (FILE *);
4689 void debug_all_asserts (void);
4691 /* Dump all the registered assertions for NAME to FILE. */
4693 void
4694 dump_asserts_for (FILE *file, tree name)
4696 assert_locus_t loc;
4698 fprintf (file, "Assertions to be inserted for ");
4699 print_generic_expr (file, name, 0);
4700 fprintf (file, "\n");
4702 loc = asserts_for[SSA_NAME_VERSION (name)];
4703 while (loc)
4705 fprintf (file, "\t");
4706 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4707 fprintf (file, "\n\tBB #%d", loc->bb->index);
4708 if (loc->e)
4710 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4711 loc->e->dest->index);
4712 dump_edge_info (file, loc->e, dump_flags, 0);
4714 fprintf (file, "\n\tPREDICATE: ");
4715 print_generic_expr (file, name, 0);
4716 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4717 print_generic_expr (file, loc->val, 0);
4718 fprintf (file, "\n\n");
4719 loc = loc->next;
4722 fprintf (file, "\n");
4726 /* Dump all the registered assertions for NAME to stderr. */
4728 DEBUG_FUNCTION void
4729 debug_asserts_for (tree name)
4731 dump_asserts_for (stderr, name);
4735 /* Dump all the registered assertions for all the names to FILE. */
4737 void
4738 dump_all_asserts (FILE *file)
4740 unsigned i;
4741 bitmap_iterator bi;
4743 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4744 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4745 dump_asserts_for (file, ssa_name (i));
4746 fprintf (file, "\n");
4750 /* Dump all the registered assertions for all the names to stderr. */
4752 DEBUG_FUNCTION void
4753 debug_all_asserts (void)
4755 dump_all_asserts (stderr);
4759 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4760 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4761 E->DEST, then register this location as a possible insertion point
4762 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4764 BB, E and SI provide the exact insertion point for the new
4765 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4766 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4767 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4768 must not be NULL. */
4770 static void
4771 register_new_assert_for (tree name, tree expr,
4772 enum tree_code comp_code,
4773 tree val,
4774 basic_block bb,
4775 edge e,
4776 gimple_stmt_iterator si)
4778 assert_locus_t n, loc, last_loc;
4779 basic_block dest_bb;
4781 gcc_checking_assert (bb == NULL || e == NULL);
4783 if (e == NULL)
4784 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4785 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4787 /* Never build an assert comparing against an integer constant with
4788 TREE_OVERFLOW set. This confuses our undefined overflow warning
4789 machinery. */
4790 if (TREE_OVERFLOW_P (val))
4791 val = drop_tree_overflow (val);
4793 /* The new assertion A will be inserted at BB or E. We need to
4794 determine if the new location is dominated by a previously
4795 registered location for A. If we are doing an edge insertion,
4796 assume that A will be inserted at E->DEST. Note that this is not
4797 necessarily true.
4799 If E is a critical edge, it will be split. But even if E is
4800 split, the new block will dominate the same set of blocks that
4801 E->DEST dominates.
4803 The reverse, however, is not true, blocks dominated by E->DEST
4804 will not be dominated by the new block created to split E. So,
4805 if the insertion location is on a critical edge, we will not use
4806 the new location to move another assertion previously registered
4807 at a block dominated by E->DEST. */
4808 dest_bb = (bb) ? bb : e->dest;
4810 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4811 VAL at a block dominating DEST_BB, then we don't need to insert a new
4812 one. Similarly, if the same assertion already exists at a block
4813 dominated by DEST_BB and the new location is not on a critical
4814 edge, then update the existing location for the assertion (i.e.,
4815 move the assertion up in the dominance tree).
4817 Note, this is implemented as a simple linked list because there
4818 should not be more than a handful of assertions registered per
4819 name. If this becomes a performance problem, a table hashed by
4820 COMP_CODE and VAL could be implemented. */
4821 loc = asserts_for[SSA_NAME_VERSION (name)];
4822 last_loc = loc;
4823 while (loc)
4825 if (loc->comp_code == comp_code
4826 && (loc->val == val
4827 || operand_equal_p (loc->val, val, 0))
4828 && (loc->expr == expr
4829 || operand_equal_p (loc->expr, expr, 0)))
4831 /* If E is not a critical edge and DEST_BB
4832 dominates the existing location for the assertion, move
4833 the assertion up in the dominance tree by updating its
4834 location information. */
4835 if ((e == NULL || !EDGE_CRITICAL_P (e))
4836 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4838 loc->bb = dest_bb;
4839 loc->e = e;
4840 loc->si = si;
4841 return;
4845 /* Update the last node of the list and move to the next one. */
4846 last_loc = loc;
4847 loc = loc->next;
4850 /* If we didn't find an assertion already registered for
4851 NAME COMP_CODE VAL, add a new one at the end of the list of
4852 assertions associated with NAME. */
4853 n = XNEW (struct assert_locus_d);
4854 n->bb = dest_bb;
4855 n->e = e;
4856 n->si = si;
4857 n->comp_code = comp_code;
4858 n->val = val;
4859 n->expr = expr;
4860 n->next = NULL;
4862 if (last_loc)
4863 last_loc->next = n;
4864 else
4865 asserts_for[SSA_NAME_VERSION (name)] = n;
4867 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4870 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4871 Extract a suitable test code and value and store them into *CODE_P and
4872 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4874 If no extraction was possible, return FALSE, otherwise return TRUE.
4876 If INVERT is true, then we invert the result stored into *CODE_P. */
4878 static bool
4879 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4880 tree cond_op0, tree cond_op1,
4881 bool invert, enum tree_code *code_p,
4882 tree *val_p)
4884 enum tree_code comp_code;
4885 tree val;
4887 /* Otherwise, we have a comparison of the form NAME COMP VAL
4888 or VAL COMP NAME. */
4889 if (name == cond_op1)
4891 /* If the predicate is of the form VAL COMP NAME, flip
4892 COMP around because we need to register NAME as the
4893 first operand in the predicate. */
4894 comp_code = swap_tree_comparison (cond_code);
4895 val = cond_op0;
4897 else
4899 /* The comparison is of the form NAME COMP VAL, so the
4900 comparison code remains unchanged. */
4901 comp_code = cond_code;
4902 val = cond_op1;
4905 /* Invert the comparison code as necessary. */
4906 if (invert)
4907 comp_code = invert_tree_comparison (comp_code, 0);
4909 /* VRP does not handle float types. */
4910 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4911 return false;
4913 /* Do not register always-false predicates.
4914 FIXME: this works around a limitation in fold() when dealing with
4915 enumerations. Given 'enum { N1, N2 } x;', fold will not
4916 fold 'if (x > N2)' to 'if (0)'. */
4917 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4918 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4920 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4921 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4923 if (comp_code == GT_EXPR
4924 && (!max
4925 || compare_values (val, max) == 0))
4926 return false;
4928 if (comp_code == LT_EXPR
4929 && (!min
4930 || compare_values (val, min) == 0))
4931 return false;
4933 *code_p = comp_code;
4934 *val_p = val;
4935 return true;
4938 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4939 (otherwise return VAL). VAL and MASK must be zero-extended for
4940 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4941 (to transform signed values into unsigned) and at the end xor
4942 SGNBIT back. */
4944 static wide_int
4945 masked_increment (const wide_int &val_in, const wide_int &mask,
4946 const wide_int &sgnbit, unsigned int prec)
4948 wide_int bit = wi::one (prec), res;
4949 unsigned int i;
4951 wide_int val = val_in ^ sgnbit;
4952 for (i = 0; i < prec; i++, bit += bit)
4954 res = mask;
4955 if ((res & bit) == 0)
4956 continue;
4957 res = bit - 1;
4958 res = (val + bit).and_not (res);
4959 res &= mask;
4960 if (wi::gtu_p (res, val))
4961 return res ^ sgnbit;
4963 return val ^ sgnbit;
4966 /* Try to register an edge assertion for SSA name NAME on edge E for
4967 the condition COND contributing to the conditional jump pointed to by BSI.
4968 Invert the condition COND if INVERT is true.
4969 Return true if an assertion for NAME could be registered. */
4971 static bool
4972 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4973 enum tree_code cond_code,
4974 tree cond_op0, tree cond_op1, bool invert)
4976 tree val;
4977 enum tree_code comp_code;
4978 bool retval = false;
4980 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4981 cond_op0,
4982 cond_op1,
4983 invert, &comp_code, &val))
4984 return false;
4986 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4987 reachable from E. */
4988 if (live_on_edge (e, name)
4989 && !has_single_use (name))
4991 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
4992 retval = true;
4995 /* In the case of NAME <= CST and NAME being defined as
4996 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4997 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4998 This catches range and anti-range tests. */
4999 if ((comp_code == LE_EXPR
5000 || comp_code == GT_EXPR)
5001 && TREE_CODE (val) == INTEGER_CST
5002 && TYPE_UNSIGNED (TREE_TYPE (val)))
5004 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5005 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5007 /* Extract CST2 from the (optional) addition. */
5008 if (is_gimple_assign (def_stmt)
5009 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5011 name2 = gimple_assign_rhs1 (def_stmt);
5012 cst2 = gimple_assign_rhs2 (def_stmt);
5013 if (TREE_CODE (name2) == SSA_NAME
5014 && TREE_CODE (cst2) == INTEGER_CST)
5015 def_stmt = SSA_NAME_DEF_STMT (name2);
5018 /* Extract NAME2 from the (optional) sign-changing cast. */
5019 if (gimple_assign_cast_p (def_stmt))
5021 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5022 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5023 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5024 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5025 name3 = gimple_assign_rhs1 (def_stmt);
5028 /* If name3 is used later, create an ASSERT_EXPR for it. */
5029 if (name3 != NULL_TREE
5030 && TREE_CODE (name3) == SSA_NAME
5031 && (cst2 == NULL_TREE
5032 || TREE_CODE (cst2) == INTEGER_CST)
5033 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5034 && live_on_edge (e, name3)
5035 && !has_single_use (name3))
5037 tree tmp;
5039 /* Build an expression for the range test. */
5040 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5041 if (cst2 != NULL_TREE)
5042 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5044 if (dump_file)
5046 fprintf (dump_file, "Adding assert for ");
5047 print_generic_expr (dump_file, name3, 0);
5048 fprintf (dump_file, " from ");
5049 print_generic_expr (dump_file, tmp, 0);
5050 fprintf (dump_file, "\n");
5053 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5055 retval = true;
5058 /* If name2 is used later, create an ASSERT_EXPR for it. */
5059 if (name2 != NULL_TREE
5060 && TREE_CODE (name2) == SSA_NAME
5061 && TREE_CODE (cst2) == INTEGER_CST
5062 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5063 && live_on_edge (e, name2)
5064 && !has_single_use (name2))
5066 tree tmp;
5068 /* Build an expression for the range test. */
5069 tmp = name2;
5070 if (TREE_TYPE (name) != TREE_TYPE (name2))
5071 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5072 if (cst2 != NULL_TREE)
5073 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5075 if (dump_file)
5077 fprintf (dump_file, "Adding assert for ");
5078 print_generic_expr (dump_file, name2, 0);
5079 fprintf (dump_file, " from ");
5080 print_generic_expr (dump_file, tmp, 0);
5081 fprintf (dump_file, "\n");
5084 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5086 retval = true;
5090 /* In the case of post-in/decrement tests like if (i++) ... and uses
5091 of the in/decremented value on the edge the extra name we want to
5092 assert for is not on the def chain of the name compared. Instead
5093 it is in the set of use stmts. */
5094 if ((comp_code == NE_EXPR
5095 || comp_code == EQ_EXPR)
5096 && TREE_CODE (val) == INTEGER_CST)
5098 imm_use_iterator ui;
5099 gimple use_stmt;
5100 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5102 /* Cut off to use-stmts that are in the predecessor. */
5103 if (gimple_bb (use_stmt) != e->src)
5104 continue;
5106 if (!is_gimple_assign (use_stmt))
5107 continue;
5109 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5110 if (code != PLUS_EXPR
5111 && code != MINUS_EXPR)
5112 continue;
5114 tree cst = gimple_assign_rhs2 (use_stmt);
5115 if (TREE_CODE (cst) != INTEGER_CST)
5116 continue;
5118 tree name2 = gimple_assign_lhs (use_stmt);
5119 if (live_on_edge (e, name2))
5121 cst = int_const_binop (code, val, cst);
5122 register_new_assert_for (name2, name2, comp_code, cst,
5123 NULL, e, bsi);
5124 retval = true;
5129 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5130 && TREE_CODE (val) == INTEGER_CST)
5132 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5133 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5134 tree val2 = NULL_TREE;
5135 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5136 wide_int mask = wi::zero (prec);
5137 unsigned int nprec = prec;
5138 enum tree_code rhs_code = ERROR_MARK;
5140 if (is_gimple_assign (def_stmt))
5141 rhs_code = gimple_assign_rhs_code (def_stmt);
5143 /* Add asserts for NAME cmp CST and NAME being defined
5144 as NAME = (int) NAME2. */
5145 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5146 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5147 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5148 && gimple_assign_cast_p (def_stmt))
5150 name2 = gimple_assign_rhs1 (def_stmt);
5151 if (CONVERT_EXPR_CODE_P (rhs_code)
5152 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5153 && TYPE_UNSIGNED (TREE_TYPE (name2))
5154 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5155 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5156 || !tree_int_cst_equal (val,
5157 TYPE_MIN_VALUE (TREE_TYPE (val))))
5158 && live_on_edge (e, name2)
5159 && !has_single_use (name2))
5161 tree tmp, cst;
5162 enum tree_code new_comp_code = comp_code;
5164 cst = fold_convert (TREE_TYPE (name2),
5165 TYPE_MIN_VALUE (TREE_TYPE (val)));
5166 /* Build an expression for the range test. */
5167 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5168 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5169 fold_convert (TREE_TYPE (name2), val));
5170 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5172 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5173 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5174 build_int_cst (TREE_TYPE (name2), 1));
5177 if (dump_file)
5179 fprintf (dump_file, "Adding assert for ");
5180 print_generic_expr (dump_file, name2, 0);
5181 fprintf (dump_file, " from ");
5182 print_generic_expr (dump_file, tmp, 0);
5183 fprintf (dump_file, "\n");
5186 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5187 e, bsi);
5189 retval = true;
5193 /* Add asserts for NAME cmp CST and NAME being defined as
5194 NAME = NAME2 >> CST2.
5196 Extract CST2 from the right shift. */
5197 if (rhs_code == RSHIFT_EXPR)
5199 name2 = gimple_assign_rhs1 (def_stmt);
5200 cst2 = gimple_assign_rhs2 (def_stmt);
5201 if (TREE_CODE (name2) == SSA_NAME
5202 && tree_fits_uhwi_p (cst2)
5203 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5204 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5205 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5206 && live_on_edge (e, name2)
5207 && !has_single_use (name2))
5209 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5210 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5213 if (val2 != NULL_TREE
5214 && TREE_CODE (val2) == INTEGER_CST
5215 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5216 TREE_TYPE (val),
5217 val2, cst2), val))
5219 enum tree_code new_comp_code = comp_code;
5220 tree tmp, new_val;
5222 tmp = name2;
5223 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5225 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5227 tree type = build_nonstandard_integer_type (prec, 1);
5228 tmp = build1 (NOP_EXPR, type, name2);
5229 val2 = fold_convert (type, val2);
5231 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5232 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5233 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5235 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5237 wide_int minval
5238 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5239 new_val = val2;
5240 if (minval == new_val)
5241 new_val = NULL_TREE;
5243 else
5245 wide_int maxval
5246 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5247 mask |= val2;
5248 if (mask == maxval)
5249 new_val = NULL_TREE;
5250 else
5251 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5254 if (new_val)
5256 if (dump_file)
5258 fprintf (dump_file, "Adding assert for ");
5259 print_generic_expr (dump_file, name2, 0);
5260 fprintf (dump_file, " from ");
5261 print_generic_expr (dump_file, tmp, 0);
5262 fprintf (dump_file, "\n");
5265 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5266 NULL, e, bsi);
5267 retval = true;
5271 /* Add asserts for NAME cmp CST and NAME being defined as
5272 NAME = NAME2 & CST2.
5274 Extract CST2 from the and.
5276 Also handle
5277 NAME = (unsigned) NAME2;
5278 casts where NAME's type is unsigned and has smaller precision
5279 than NAME2's type as if it was NAME = NAME2 & MASK. */
5280 names[0] = NULL_TREE;
5281 names[1] = NULL_TREE;
5282 cst2 = NULL_TREE;
5283 if (rhs_code == BIT_AND_EXPR
5284 || (CONVERT_EXPR_CODE_P (rhs_code)
5285 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5286 && TYPE_UNSIGNED (TREE_TYPE (val))
5287 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5288 > prec
5289 && !retval))
5291 name2 = gimple_assign_rhs1 (def_stmt);
5292 if (rhs_code == BIT_AND_EXPR)
5293 cst2 = gimple_assign_rhs2 (def_stmt);
5294 else
5296 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5297 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5299 if (TREE_CODE (name2) == SSA_NAME
5300 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5301 && TREE_CODE (cst2) == INTEGER_CST
5302 && !integer_zerop (cst2)
5303 && (nprec > 1
5304 || TYPE_UNSIGNED (TREE_TYPE (val))))
5306 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5307 if (gimple_assign_cast_p (def_stmt2))
5309 names[1] = gimple_assign_rhs1 (def_stmt2);
5310 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5311 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5312 || (TYPE_PRECISION (TREE_TYPE (name2))
5313 != TYPE_PRECISION (TREE_TYPE (names[1])))
5314 || !live_on_edge (e, names[1])
5315 || has_single_use (names[1]))
5316 names[1] = NULL_TREE;
5318 if (live_on_edge (e, name2)
5319 && !has_single_use (name2))
5320 names[0] = name2;
5323 if (names[0] || names[1])
5325 wide_int minv, maxv, valv, cst2v;
5326 wide_int tem, sgnbit;
5327 bool valid_p = false, valn, cst2n;
5328 enum tree_code ccode = comp_code;
5330 valv = wide_int::from (val, nprec, UNSIGNED);
5331 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5332 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5333 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5334 /* If CST2 doesn't have most significant bit set,
5335 but VAL is negative, we have comparison like
5336 if ((x & 0x123) > -4) (always true). Just give up. */
5337 if (!cst2n && valn)
5338 ccode = ERROR_MARK;
5339 if (cst2n)
5340 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5341 else
5342 sgnbit = wi::zero (nprec);
5343 minv = valv & cst2v;
5344 switch (ccode)
5346 case EQ_EXPR:
5347 /* Minimum unsigned value for equality is VAL & CST2
5348 (should be equal to VAL, otherwise we probably should
5349 have folded the comparison into false) and
5350 maximum unsigned value is VAL | ~CST2. */
5351 maxv = valv | ~cst2v;
5352 valid_p = true;
5353 break;
5355 case NE_EXPR:
5356 tem = valv | ~cst2v;
5357 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5358 if (valv == 0)
5360 cst2n = false;
5361 sgnbit = wi::zero (nprec);
5362 goto gt_expr;
5364 /* If (VAL | ~CST2) is all ones, handle it as
5365 (X & CST2) < VAL. */
5366 if (tem == -1)
5368 cst2n = false;
5369 valn = false;
5370 sgnbit = wi::zero (nprec);
5371 goto lt_expr;
5373 if (!cst2n && wi::neg_p (cst2v))
5374 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5375 if (sgnbit != 0)
5377 if (valv == sgnbit)
5379 cst2n = true;
5380 valn = true;
5381 goto gt_expr;
5383 if (tem == wi::mask (nprec - 1, false, nprec))
5385 cst2n = true;
5386 goto lt_expr;
5388 if (!cst2n)
5389 sgnbit = wi::zero (nprec);
5391 break;
5393 case GE_EXPR:
5394 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5395 is VAL and maximum unsigned value is ~0. For signed
5396 comparison, if CST2 doesn't have most significant bit
5397 set, handle it similarly. If CST2 has MSB set,
5398 the minimum is the same, and maximum is ~0U/2. */
5399 if (minv != valv)
5401 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5402 VAL. */
5403 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5404 if (minv == valv)
5405 break;
5407 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5408 valid_p = true;
5409 break;
5411 case GT_EXPR:
5412 gt_expr:
5413 /* Find out smallest MINV where MINV > VAL
5414 && (MINV & CST2) == MINV, if any. If VAL is signed and
5415 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5416 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5417 if (minv == valv)
5418 break;
5419 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5420 valid_p = true;
5421 break;
5423 case LE_EXPR:
5424 /* Minimum unsigned value for <= is 0 and maximum
5425 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5426 Otherwise, find smallest VAL2 where VAL2 > VAL
5427 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5428 as maximum.
5429 For signed comparison, if CST2 doesn't have most
5430 significant bit set, handle it similarly. If CST2 has
5431 MSB set, the maximum is the same and minimum is INT_MIN. */
5432 if (minv == valv)
5433 maxv = valv;
5434 else
5436 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5437 if (maxv == valv)
5438 break;
5439 maxv -= 1;
5441 maxv |= ~cst2v;
5442 minv = sgnbit;
5443 valid_p = true;
5444 break;
5446 case LT_EXPR:
5447 lt_expr:
5448 /* Minimum unsigned value for < is 0 and maximum
5449 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5450 Otherwise, find smallest VAL2 where VAL2 > VAL
5451 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5452 as maximum.
5453 For signed comparison, if CST2 doesn't have most
5454 significant bit set, handle it similarly. If CST2 has
5455 MSB set, the maximum is the same and minimum is INT_MIN. */
5456 if (minv == valv)
5458 if (valv == sgnbit)
5459 break;
5460 maxv = valv;
5462 else
5464 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5465 if (maxv == valv)
5466 break;
5468 maxv -= 1;
5469 maxv |= ~cst2v;
5470 minv = sgnbit;
5471 valid_p = true;
5472 break;
5474 default:
5475 break;
5477 if (valid_p
5478 && (maxv - minv) != -1)
5480 tree tmp, new_val, type;
5481 int i;
5483 for (i = 0; i < 2; i++)
5484 if (names[i])
5486 wide_int maxv2 = maxv;
5487 tmp = names[i];
5488 type = TREE_TYPE (names[i]);
5489 if (!TYPE_UNSIGNED (type))
5491 type = build_nonstandard_integer_type (nprec, 1);
5492 tmp = build1 (NOP_EXPR, type, names[i]);
5494 if (minv != 0)
5496 tmp = build2 (PLUS_EXPR, type, tmp,
5497 wide_int_to_tree (type, -minv));
5498 maxv2 = maxv - minv;
5500 new_val = wide_int_to_tree (type, maxv2);
5502 if (dump_file)
5504 fprintf (dump_file, "Adding assert for ");
5505 print_generic_expr (dump_file, names[i], 0);
5506 fprintf (dump_file, " from ");
5507 print_generic_expr (dump_file, tmp, 0);
5508 fprintf (dump_file, "\n");
5511 register_new_assert_for (names[i], tmp, LE_EXPR,
5512 new_val, NULL, e, bsi);
5513 retval = true;
5519 return retval;
5522 /* OP is an operand of a truth value expression which is known to have
5523 a particular value. Register any asserts for OP and for any
5524 operands in OP's defining statement.
5526 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5527 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5529 static bool
5530 register_edge_assert_for_1 (tree op, enum tree_code code,
5531 edge e, gimple_stmt_iterator bsi)
5533 bool retval = false;
5534 gimple op_def;
5535 tree val;
5536 enum tree_code rhs_code;
5538 /* We only care about SSA_NAMEs. */
5539 if (TREE_CODE (op) != SSA_NAME)
5540 return false;
5542 /* We know that OP will have a zero or nonzero value. If OP is used
5543 more than once go ahead and register an assert for OP. */
5544 if (live_on_edge (e, op)
5545 && !has_single_use (op))
5547 val = build_int_cst (TREE_TYPE (op), 0);
5548 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5549 retval = true;
5552 /* Now look at how OP is set. If it's set from a comparison,
5553 a truth operation or some bit operations, then we may be able
5554 to register information about the operands of that assignment. */
5555 op_def = SSA_NAME_DEF_STMT (op);
5556 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5557 return retval;
5559 rhs_code = gimple_assign_rhs_code (op_def);
5561 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5563 bool invert = (code == EQ_EXPR ? true : false);
5564 tree op0 = gimple_assign_rhs1 (op_def);
5565 tree op1 = gimple_assign_rhs2 (op_def);
5567 if (TREE_CODE (op0) == SSA_NAME)
5568 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5569 invert);
5570 if (TREE_CODE (op1) == SSA_NAME)
5571 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5572 invert);
5574 else if ((code == NE_EXPR
5575 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5576 || (code == EQ_EXPR
5577 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5579 /* Recurse on each operand. */
5580 tree op0 = gimple_assign_rhs1 (op_def);
5581 tree op1 = gimple_assign_rhs2 (op_def);
5582 if (TREE_CODE (op0) == SSA_NAME
5583 && has_single_use (op0))
5584 retval |= register_edge_assert_for_1 (op0, code, e, bsi);
5585 if (TREE_CODE (op1) == SSA_NAME
5586 && has_single_use (op1))
5587 retval |= register_edge_assert_for_1 (op1, code, e, bsi);
5589 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5590 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5592 /* Recurse, flipping CODE. */
5593 code = invert_tree_comparison (code, false);
5594 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5595 code, e, bsi);
5597 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5599 /* Recurse through the copy. */
5600 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5601 code, e, bsi);
5603 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5605 /* Recurse through the type conversion, unless it is a narrowing
5606 conversion or conversion from non-integral type. */
5607 tree rhs = gimple_assign_rhs1 (op_def);
5608 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5609 && (TYPE_PRECISION (TREE_TYPE (rhs))
5610 <= TYPE_PRECISION (TREE_TYPE (op))))
5611 retval |= register_edge_assert_for_1 (rhs, code, e, bsi);
5614 return retval;
5617 /* Try to register an edge assertion for SSA name NAME on edge E for
5618 the condition COND contributing to the conditional jump pointed to by SI.
5619 Return true if an assertion for NAME could be registered. */
5621 static bool
5622 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5623 enum tree_code cond_code, tree cond_op0,
5624 tree cond_op1)
5626 tree val;
5627 enum tree_code comp_code;
5628 bool retval = false;
5629 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5631 /* Do not attempt to infer anything in names that flow through
5632 abnormal edges. */
5633 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5634 return false;
5636 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5637 cond_op0, cond_op1,
5638 is_else_edge,
5639 &comp_code, &val))
5640 return false;
5642 /* Register ASSERT_EXPRs for name. */
5643 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5644 cond_op1, is_else_edge);
5647 /* If COND is effectively an equality test of an SSA_NAME against
5648 the value zero or one, then we may be able to assert values
5649 for SSA_NAMEs which flow into COND. */
5651 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5652 statement of NAME we can assert both operands of the BIT_AND_EXPR
5653 have nonzero value. */
5654 if (((comp_code == EQ_EXPR && integer_onep (val))
5655 || (comp_code == NE_EXPR && integer_zerop (val))))
5657 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5659 if (is_gimple_assign (def_stmt)
5660 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5662 tree op0 = gimple_assign_rhs1 (def_stmt);
5663 tree op1 = gimple_assign_rhs2 (def_stmt);
5664 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5665 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5669 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5670 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5671 have zero value. */
5672 if (((comp_code == EQ_EXPR && integer_zerop (val))
5673 || (comp_code == NE_EXPR && integer_onep (val))))
5675 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5677 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5678 necessarily zero value, or if type-precision is one. */
5679 if (is_gimple_assign (def_stmt)
5680 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5681 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5682 || comp_code == EQ_EXPR)))
5684 tree op0 = gimple_assign_rhs1 (def_stmt);
5685 tree op1 = gimple_assign_rhs2 (def_stmt);
5686 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5687 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5691 return retval;
5695 /* Determine whether the outgoing edges of BB should receive an
5696 ASSERT_EXPR for each of the operands of BB's LAST statement.
5697 The last statement of BB must be a COND_EXPR.
5699 If any of the sub-graphs rooted at BB have an interesting use of
5700 the predicate operands, an assert location node is added to the
5701 list of assertions for the corresponding operands. */
5703 static bool
5704 find_conditional_asserts (basic_block bb, gimple last)
5706 bool need_assert;
5707 gimple_stmt_iterator bsi;
5708 tree op;
5709 edge_iterator ei;
5710 edge e;
5711 ssa_op_iter iter;
5713 need_assert = false;
5714 bsi = gsi_for_stmt (last);
5716 /* Look for uses of the operands in each of the sub-graphs
5717 rooted at BB. We need to check each of the outgoing edges
5718 separately, so that we know what kind of ASSERT_EXPR to
5719 insert. */
5720 FOR_EACH_EDGE (e, ei, bb->succs)
5722 if (e->dest == bb)
5723 continue;
5725 /* Register the necessary assertions for each operand in the
5726 conditional predicate. */
5727 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5729 need_assert |= register_edge_assert_for (op, e, bsi,
5730 gimple_cond_code (last),
5731 gimple_cond_lhs (last),
5732 gimple_cond_rhs (last));
5736 return need_assert;
5739 struct case_info
5741 tree expr;
5742 basic_block bb;
5745 /* Compare two case labels sorting first by the destination bb index
5746 and then by the case value. */
5748 static int
5749 compare_case_labels (const void *p1, const void *p2)
5751 const struct case_info *ci1 = (const struct case_info *) p1;
5752 const struct case_info *ci2 = (const struct case_info *) p2;
5753 int idx1 = ci1->bb->index;
5754 int idx2 = ci2->bb->index;
5756 if (idx1 < idx2)
5757 return -1;
5758 else if (idx1 == idx2)
5760 /* Make sure the default label is first in a group. */
5761 if (!CASE_LOW (ci1->expr))
5762 return -1;
5763 else if (!CASE_LOW (ci2->expr))
5764 return 1;
5765 else
5766 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5767 CASE_LOW (ci2->expr));
5769 else
5770 return 1;
5773 /* Determine whether the outgoing edges of BB should receive an
5774 ASSERT_EXPR for each of the operands of BB's LAST statement.
5775 The last statement of BB must be a SWITCH_EXPR.
5777 If any of the sub-graphs rooted at BB have an interesting use of
5778 the predicate operands, an assert location node is added to the
5779 list of assertions for the corresponding operands. */
5781 static bool
5782 find_switch_asserts (basic_block bb, gimple last)
5784 bool need_assert;
5785 gimple_stmt_iterator bsi;
5786 tree op;
5787 edge e;
5788 struct case_info *ci;
5789 size_t n = gimple_switch_num_labels (last);
5790 #if GCC_VERSION >= 4000
5791 unsigned int idx;
5792 #else
5793 /* Work around GCC 3.4 bug (PR 37086). */
5794 volatile unsigned int idx;
5795 #endif
5797 need_assert = false;
5798 bsi = gsi_for_stmt (last);
5799 op = gimple_switch_index (last);
5800 if (TREE_CODE (op) != SSA_NAME)
5801 return false;
5803 /* Build a vector of case labels sorted by destination label. */
5804 ci = XNEWVEC (struct case_info, n);
5805 for (idx = 0; idx < n; ++idx)
5807 ci[idx].expr = gimple_switch_label (last, idx);
5808 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5810 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5812 for (idx = 0; idx < n; ++idx)
5814 tree min, max;
5815 tree cl = ci[idx].expr;
5816 basic_block cbb = ci[idx].bb;
5818 min = CASE_LOW (cl);
5819 max = CASE_HIGH (cl);
5821 /* If there are multiple case labels with the same destination
5822 we need to combine them to a single value range for the edge. */
5823 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5825 /* Skip labels until the last of the group. */
5826 do {
5827 ++idx;
5828 } while (idx < n && cbb == ci[idx].bb);
5829 --idx;
5831 /* Pick up the maximum of the case label range. */
5832 if (CASE_HIGH (ci[idx].expr))
5833 max = CASE_HIGH (ci[idx].expr);
5834 else
5835 max = CASE_LOW (ci[idx].expr);
5838 /* Nothing to do if the range includes the default label until we
5839 can register anti-ranges. */
5840 if (min == NULL_TREE)
5841 continue;
5843 /* Find the edge to register the assert expr on. */
5844 e = find_edge (bb, cbb);
5846 /* Register the necessary assertions for the operand in the
5847 SWITCH_EXPR. */
5848 need_assert |= register_edge_assert_for (op, e, bsi,
5849 max ? GE_EXPR : EQ_EXPR,
5851 fold_convert (TREE_TYPE (op),
5852 min));
5853 if (max)
5855 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5857 fold_convert (TREE_TYPE (op),
5858 max));
5862 XDELETEVEC (ci);
5863 return need_assert;
5867 /* Traverse all the statements in block BB looking for statements that
5868 may generate useful assertions for the SSA names in their operand.
5869 If a statement produces a useful assertion A for name N_i, then the
5870 list of assertions already generated for N_i is scanned to
5871 determine if A is actually needed.
5873 If N_i already had the assertion A at a location dominating the
5874 current location, then nothing needs to be done. Otherwise, the
5875 new location for A is recorded instead.
5877 1- For every statement S in BB, all the variables used by S are
5878 added to bitmap FOUND_IN_SUBGRAPH.
5880 2- If statement S uses an operand N in a way that exposes a known
5881 value range for N, then if N was not already generated by an
5882 ASSERT_EXPR, create a new assert location for N. For instance,
5883 if N is a pointer and the statement dereferences it, we can
5884 assume that N is not NULL.
5886 3- COND_EXPRs are a special case of #2. We can derive range
5887 information from the predicate but need to insert different
5888 ASSERT_EXPRs for each of the sub-graphs rooted at the
5889 conditional block. If the last statement of BB is a conditional
5890 expression of the form 'X op Y', then
5892 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5894 b) If the conditional is the only entry point to the sub-graph
5895 corresponding to the THEN_CLAUSE, recurse into it. On
5896 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5897 an ASSERT_EXPR is added for the corresponding variable.
5899 c) Repeat step (b) on the ELSE_CLAUSE.
5901 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5903 For instance,
5905 if (a == 9)
5906 b = a;
5907 else
5908 b = c + 1;
5910 In this case, an assertion on the THEN clause is useful to
5911 determine that 'a' is always 9 on that edge. However, an assertion
5912 on the ELSE clause would be unnecessary.
5914 4- If BB does not end in a conditional expression, then we recurse
5915 into BB's dominator children.
5917 At the end of the recursive traversal, every SSA name will have a
5918 list of locations where ASSERT_EXPRs should be added. When a new
5919 location for name N is found, it is registered by calling
5920 register_new_assert_for. That function keeps track of all the
5921 registered assertions to prevent adding unnecessary assertions.
5922 For instance, if a pointer P_4 is dereferenced more than once in a
5923 dominator tree, only the location dominating all the dereference of
5924 P_4 will receive an ASSERT_EXPR.
5926 If this function returns true, then it means that there are names
5927 for which we need to generate ASSERT_EXPRs. Those assertions are
5928 inserted by process_assert_insertions. */
5930 static bool
5931 find_assert_locations_1 (basic_block bb, sbitmap live)
5933 gimple_stmt_iterator si;
5934 gimple last;
5935 bool need_assert;
5937 need_assert = false;
5938 last = last_stmt (bb);
5940 /* If BB's last statement is a conditional statement involving integer
5941 operands, determine if we need to add ASSERT_EXPRs. */
5942 if (last
5943 && gimple_code (last) == GIMPLE_COND
5944 && !fp_predicate (last)
5945 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5946 need_assert |= find_conditional_asserts (bb, last);
5948 /* If BB's last statement is a switch statement involving integer
5949 operands, determine if we need to add ASSERT_EXPRs. */
5950 if (last
5951 && gimple_code (last) == GIMPLE_SWITCH
5952 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5953 need_assert |= find_switch_asserts (bb, last);
5955 /* Traverse all the statements in BB marking used names and looking
5956 for statements that may infer assertions for their used operands. */
5957 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5959 gimple stmt;
5960 tree op;
5961 ssa_op_iter i;
5963 stmt = gsi_stmt (si);
5965 if (is_gimple_debug (stmt))
5966 continue;
5968 /* See if we can derive an assertion for any of STMT's operands. */
5969 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5971 tree value;
5972 enum tree_code comp_code;
5974 /* If op is not live beyond this stmt, do not bother to insert
5975 asserts for it. */
5976 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5977 continue;
5979 /* If OP is used in such a way that we can infer a value
5980 range for it, and we don't find a previous assertion for
5981 it, create a new assertion location node for OP. */
5982 if (infer_value_range (stmt, op, &comp_code, &value))
5984 /* If we are able to infer a nonzero value range for OP,
5985 then walk backwards through the use-def chain to see if OP
5986 was set via a typecast.
5988 If so, then we can also infer a nonzero value range
5989 for the operand of the NOP_EXPR. */
5990 if (comp_code == NE_EXPR && integer_zerop (value))
5992 tree t = op;
5993 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5995 while (is_gimple_assign (def_stmt)
5996 && gimple_assign_rhs_code (def_stmt) == NOP_EXPR
5997 && TREE_CODE
5998 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5999 && POINTER_TYPE_P
6000 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6002 t = gimple_assign_rhs1 (def_stmt);
6003 def_stmt = SSA_NAME_DEF_STMT (t);
6005 /* Note we want to register the assert for the
6006 operand of the NOP_EXPR after SI, not after the
6007 conversion. */
6008 if (! has_single_use (t))
6010 register_new_assert_for (t, t, comp_code, value,
6011 bb, NULL, si);
6012 need_assert = true;
6017 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6018 need_assert = true;
6022 /* Update live. */
6023 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6024 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6025 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6026 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6029 /* Traverse all PHI nodes in BB, updating live. */
6030 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6032 use_operand_p arg_p;
6033 ssa_op_iter i;
6034 gimple phi = gsi_stmt (si);
6035 tree res = gimple_phi_result (phi);
6037 if (virtual_operand_p (res))
6038 continue;
6040 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6042 tree arg = USE_FROM_PTR (arg_p);
6043 if (TREE_CODE (arg) == SSA_NAME)
6044 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6047 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6050 return need_assert;
6053 /* Do an RPO walk over the function computing SSA name liveness
6054 on-the-fly and deciding on assert expressions to insert.
6055 Returns true if there are assert expressions to be inserted. */
6057 static bool
6058 find_assert_locations (void)
6060 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6061 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6062 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6063 int rpo_cnt, i;
6064 bool need_asserts;
6066 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6067 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6068 for (i = 0; i < rpo_cnt; ++i)
6069 bb_rpo[rpo[i]] = i;
6071 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6072 the order we compute liveness and insert asserts we otherwise
6073 fail to insert asserts into the loop latch. */
6074 loop_p loop;
6075 FOR_EACH_LOOP (loop, 0)
6077 i = loop->latch->index;
6078 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6079 for (gimple_stmt_iterator gsi = gsi_start_phis (loop->header);
6080 !gsi_end_p (gsi); gsi_next (&gsi))
6082 gimple phi = gsi_stmt (gsi);
6083 if (virtual_operand_p (gimple_phi_result (phi)))
6084 continue;
6085 tree arg = gimple_phi_arg_def (phi, j);
6086 if (TREE_CODE (arg) == SSA_NAME)
6088 if (live[i] == NULL)
6090 live[i] = sbitmap_alloc (num_ssa_names);
6091 bitmap_clear (live[i]);
6093 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6098 need_asserts = false;
6099 for (i = rpo_cnt - 1; i >= 0; --i)
6101 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6102 edge e;
6103 edge_iterator ei;
6105 if (!live[rpo[i]])
6107 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6108 bitmap_clear (live[rpo[i]]);
6111 /* Process BB and update the live information with uses in
6112 this block. */
6113 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
6115 /* Merge liveness into the predecessor blocks and free it. */
6116 if (!bitmap_empty_p (live[rpo[i]]))
6118 int pred_rpo = i;
6119 FOR_EACH_EDGE (e, ei, bb->preds)
6121 int pred = e->src->index;
6122 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6123 continue;
6125 if (!live[pred])
6127 live[pred] = sbitmap_alloc (num_ssa_names);
6128 bitmap_clear (live[pred]);
6130 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6132 if (bb_rpo[pred] < pred_rpo)
6133 pred_rpo = bb_rpo[pred];
6136 /* Record the RPO number of the last visited block that needs
6137 live information from this block. */
6138 last_rpo[rpo[i]] = pred_rpo;
6140 else
6142 sbitmap_free (live[rpo[i]]);
6143 live[rpo[i]] = NULL;
6146 /* We can free all successors live bitmaps if all their
6147 predecessors have been visited already. */
6148 FOR_EACH_EDGE (e, ei, bb->succs)
6149 if (last_rpo[e->dest->index] == i
6150 && live[e->dest->index])
6152 sbitmap_free (live[e->dest->index]);
6153 live[e->dest->index] = NULL;
6157 XDELETEVEC (rpo);
6158 XDELETEVEC (bb_rpo);
6159 XDELETEVEC (last_rpo);
6160 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6161 if (live[i])
6162 sbitmap_free (live[i]);
6163 XDELETEVEC (live);
6165 return need_asserts;
6168 /* Create an ASSERT_EXPR for NAME and insert it in the location
6169 indicated by LOC. Return true if we made any edge insertions. */
6171 static bool
6172 process_assert_insertions_for (tree name, assert_locus_t loc)
6174 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6175 gimple stmt;
6176 tree cond;
6177 gimple assert_stmt;
6178 edge_iterator ei;
6179 edge e;
6181 /* If we have X <=> X do not insert an assert expr for that. */
6182 if (loc->expr == loc->val)
6183 return false;
6185 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6186 assert_stmt = build_assert_expr_for (cond, name);
6187 if (loc->e)
6189 /* We have been asked to insert the assertion on an edge. This
6190 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6191 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6192 || (gimple_code (gsi_stmt (loc->si))
6193 == GIMPLE_SWITCH));
6195 gsi_insert_on_edge (loc->e, assert_stmt);
6196 return true;
6199 /* Otherwise, we can insert right after LOC->SI iff the
6200 statement must not be the last statement in the block. */
6201 stmt = gsi_stmt (loc->si);
6202 if (!stmt_ends_bb_p (stmt))
6204 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6205 return false;
6208 /* If STMT must be the last statement in BB, we can only insert new
6209 assertions on the non-abnormal edge out of BB. Note that since
6210 STMT is not control flow, there may only be one non-abnormal edge
6211 out of BB. */
6212 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6213 if (!(e->flags & EDGE_ABNORMAL))
6215 gsi_insert_on_edge (e, assert_stmt);
6216 return true;
6219 gcc_unreachable ();
6223 /* Process all the insertions registered for every name N_i registered
6224 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6225 found in ASSERTS_FOR[i]. */
6227 static void
6228 process_assert_insertions (void)
6230 unsigned i;
6231 bitmap_iterator bi;
6232 bool update_edges_p = false;
6233 int num_asserts = 0;
6235 if (dump_file && (dump_flags & TDF_DETAILS))
6236 dump_all_asserts (dump_file);
6238 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6240 assert_locus_t loc = asserts_for[i];
6241 gcc_assert (loc);
6243 while (loc)
6245 assert_locus_t next = loc->next;
6246 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6247 free (loc);
6248 loc = next;
6249 num_asserts++;
6253 if (update_edges_p)
6254 gsi_commit_edge_inserts ();
6256 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6257 num_asserts);
6261 /* Traverse the flowgraph looking for conditional jumps to insert range
6262 expressions. These range expressions are meant to provide information
6263 to optimizations that need to reason in terms of value ranges. They
6264 will not be expanded into RTL. For instance, given:
6266 x = ...
6267 y = ...
6268 if (x < y)
6269 y = x - 2;
6270 else
6271 x = y + 3;
6273 this pass will transform the code into:
6275 x = ...
6276 y = ...
6277 if (x < y)
6279 x = ASSERT_EXPR <x, x < y>
6280 y = x - 2
6282 else
6284 y = ASSERT_EXPR <y, x >= y>
6285 x = y + 3
6288 The idea is that once copy and constant propagation have run, other
6289 optimizations will be able to determine what ranges of values can 'x'
6290 take in different paths of the code, simply by checking the reaching
6291 definition of 'x'. */
6293 static void
6294 insert_range_assertions (void)
6296 need_assert_for = BITMAP_ALLOC (NULL);
6297 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6299 calculate_dominance_info (CDI_DOMINATORS);
6301 if (find_assert_locations ())
6303 process_assert_insertions ();
6304 update_ssa (TODO_update_ssa_no_phi);
6307 if (dump_file && (dump_flags & TDF_DETAILS))
6309 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6310 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6313 free (asserts_for);
6314 BITMAP_FREE (need_assert_for);
6317 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6318 and "struct" hacks. If VRP can determine that the
6319 array subscript is a constant, check if it is outside valid
6320 range. If the array subscript is a RANGE, warn if it is
6321 non-overlapping with valid range.
6322 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6324 static void
6325 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6327 value_range_t* vr = NULL;
6328 tree low_sub, up_sub;
6329 tree low_bound, up_bound, up_bound_p1;
6330 tree base;
6332 if (TREE_NO_WARNING (ref))
6333 return;
6335 low_sub = up_sub = TREE_OPERAND (ref, 1);
6336 up_bound = array_ref_up_bound (ref);
6338 /* Can not check flexible arrays. */
6339 if (!up_bound
6340 || TREE_CODE (up_bound) != INTEGER_CST)
6341 return;
6343 /* Accesses to trailing arrays via pointers may access storage
6344 beyond the types array bounds. */
6345 base = get_base_address (ref);
6346 if (base && TREE_CODE (base) == MEM_REF)
6348 tree cref, next = NULL_TREE;
6350 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6351 return;
6353 cref = TREE_OPERAND (ref, 0);
6354 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6355 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6356 next && TREE_CODE (next) != FIELD_DECL;
6357 next = DECL_CHAIN (next))
6360 /* If this is the last field in a struct type or a field in a
6361 union type do not warn. */
6362 if (!next)
6363 return;
6366 low_bound = array_ref_low_bound (ref);
6367 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6368 build_int_cst (TREE_TYPE (up_bound), 1));
6370 if (TREE_CODE (low_sub) == SSA_NAME)
6372 vr = get_value_range (low_sub);
6373 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6375 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6376 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6380 if (vr && vr->type == VR_ANTI_RANGE)
6382 if (TREE_CODE (up_sub) == INTEGER_CST
6383 && tree_int_cst_lt (up_bound, up_sub)
6384 && TREE_CODE (low_sub) == INTEGER_CST
6385 && tree_int_cst_lt (low_sub, low_bound))
6387 warning_at (location, OPT_Warray_bounds,
6388 "array subscript is outside array bounds");
6389 TREE_NO_WARNING (ref) = 1;
6392 else if (TREE_CODE (up_sub) == INTEGER_CST
6393 && (ignore_off_by_one
6394 ? (tree_int_cst_lt (up_bound, up_sub)
6395 && !tree_int_cst_equal (up_bound_p1, up_sub))
6396 : (tree_int_cst_lt (up_bound, up_sub)
6397 || tree_int_cst_equal (up_bound_p1, up_sub))))
6399 if (dump_file && (dump_flags & TDF_DETAILS))
6401 fprintf (dump_file, "Array bound warning for ");
6402 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6403 fprintf (dump_file, "\n");
6405 warning_at (location, OPT_Warray_bounds,
6406 "array subscript is above array bounds");
6407 TREE_NO_WARNING (ref) = 1;
6409 else if (TREE_CODE (low_sub) == INTEGER_CST
6410 && tree_int_cst_lt (low_sub, low_bound))
6412 if (dump_file && (dump_flags & TDF_DETAILS))
6414 fprintf (dump_file, "Array bound warning for ");
6415 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6416 fprintf (dump_file, "\n");
6418 warning_at (location, OPT_Warray_bounds,
6419 "array subscript is below array bounds");
6420 TREE_NO_WARNING (ref) = 1;
6424 /* Searches if the expr T, located at LOCATION computes
6425 address of an ARRAY_REF, and call check_array_ref on it. */
6427 static void
6428 search_for_addr_array (tree t, location_t location)
6430 while (TREE_CODE (t) == SSA_NAME)
6432 gimple g = SSA_NAME_DEF_STMT (t);
6434 if (gimple_code (g) != GIMPLE_ASSIGN)
6435 return;
6437 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6438 != GIMPLE_SINGLE_RHS)
6439 return;
6441 t = gimple_assign_rhs1 (g);
6445 /* We are only interested in addresses of ARRAY_REF's. */
6446 if (TREE_CODE (t) != ADDR_EXPR)
6447 return;
6449 /* Check each ARRAY_REFs in the reference chain. */
6452 if (TREE_CODE (t) == ARRAY_REF)
6453 check_array_ref (location, t, true /*ignore_off_by_one*/);
6455 t = TREE_OPERAND (t, 0);
6457 while (handled_component_p (t));
6459 if (TREE_CODE (t) == MEM_REF
6460 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6461 && !TREE_NO_WARNING (t))
6463 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6464 tree low_bound, up_bound, el_sz;
6465 offset_int idx;
6466 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6467 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6468 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6469 return;
6471 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6472 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6473 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6474 if (!low_bound
6475 || TREE_CODE (low_bound) != INTEGER_CST
6476 || !up_bound
6477 || TREE_CODE (up_bound) != INTEGER_CST
6478 || !el_sz
6479 || TREE_CODE (el_sz) != INTEGER_CST)
6480 return;
6482 idx = mem_ref_offset (t);
6483 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6484 if (wi::lts_p (idx, 0))
6486 if (dump_file && (dump_flags & TDF_DETAILS))
6488 fprintf (dump_file, "Array bound warning for ");
6489 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6490 fprintf (dump_file, "\n");
6492 warning_at (location, OPT_Warray_bounds,
6493 "array subscript is below array bounds");
6494 TREE_NO_WARNING (t) = 1;
6496 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6497 - wi::to_offset (low_bound) + 1)))
6499 if (dump_file && (dump_flags & TDF_DETAILS))
6501 fprintf (dump_file, "Array bound warning for ");
6502 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6503 fprintf (dump_file, "\n");
6505 warning_at (location, OPT_Warray_bounds,
6506 "array subscript is above array bounds");
6507 TREE_NO_WARNING (t) = 1;
6512 /* walk_tree() callback that checks if *TP is
6513 an ARRAY_REF inside an ADDR_EXPR (in which an array
6514 subscript one outside the valid range is allowed). Call
6515 check_array_ref for each ARRAY_REF found. The location is
6516 passed in DATA. */
6518 static tree
6519 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6521 tree t = *tp;
6522 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6523 location_t location;
6525 if (EXPR_HAS_LOCATION (t))
6526 location = EXPR_LOCATION (t);
6527 else
6529 location_t *locp = (location_t *) wi->info;
6530 location = *locp;
6533 *walk_subtree = TRUE;
6535 if (TREE_CODE (t) == ARRAY_REF)
6536 check_array_ref (location, t, false /*ignore_off_by_one*/);
6538 if (TREE_CODE (t) == MEM_REF
6539 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6540 search_for_addr_array (TREE_OPERAND (t, 0), location);
6542 if (TREE_CODE (t) == ADDR_EXPR)
6543 *walk_subtree = FALSE;
6545 return NULL_TREE;
6548 /* Walk over all statements of all reachable BBs and call check_array_bounds
6549 on them. */
6551 static void
6552 check_all_array_refs (void)
6554 basic_block bb;
6555 gimple_stmt_iterator si;
6557 FOR_EACH_BB_FN (bb, cfun)
6559 edge_iterator ei;
6560 edge e;
6561 bool executable = false;
6563 /* Skip blocks that were found to be unreachable. */
6564 FOR_EACH_EDGE (e, ei, bb->preds)
6565 executable |= !!(e->flags & EDGE_EXECUTABLE);
6566 if (!executable)
6567 continue;
6569 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6571 gimple stmt = gsi_stmt (si);
6572 struct walk_stmt_info wi;
6573 if (!gimple_has_location (stmt))
6574 continue;
6576 if (is_gimple_call (stmt))
6578 size_t i;
6579 size_t n = gimple_call_num_args (stmt);
6580 for (i = 0; i < n; i++)
6582 tree arg = gimple_call_arg (stmt, i);
6583 search_for_addr_array (arg, gimple_location (stmt));
6586 else
6588 memset (&wi, 0, sizeof (wi));
6589 wi.info = CONST_CAST (void *, (const void *)
6590 gimple_location_ptr (stmt));
6592 walk_gimple_op (gsi_stmt (si),
6593 check_array_bounds,
6594 &wi);
6600 /* Return true if all imm uses of VAR are either in STMT, or
6601 feed (optionally through a chain of single imm uses) GIMPLE_COND
6602 in basic block COND_BB. */
6604 static bool
6605 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6607 use_operand_p use_p, use2_p;
6608 imm_use_iterator iter;
6610 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6611 if (USE_STMT (use_p) != stmt)
6613 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6614 if (is_gimple_debug (use_stmt))
6615 continue;
6616 while (is_gimple_assign (use_stmt)
6617 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6618 && single_imm_use (gimple_assign_lhs (use_stmt),
6619 &use2_p, &use_stmt2))
6620 use_stmt = use_stmt2;
6621 if (gimple_code (use_stmt) != GIMPLE_COND
6622 || gimple_bb (use_stmt) != cond_bb)
6623 return false;
6625 return true;
6628 /* Handle
6629 _4 = x_3 & 31;
6630 if (_4 != 0)
6631 goto <bb 6>;
6632 else
6633 goto <bb 7>;
6634 <bb 6>:
6635 __builtin_unreachable ();
6636 <bb 7>:
6637 x_5 = ASSERT_EXPR <x_3, ...>;
6638 If x_3 has no other immediate uses (checked by caller),
6639 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6640 from the non-zero bitmask. */
6642 static void
6643 maybe_set_nonzero_bits (basic_block bb, tree var)
6645 edge e = single_pred_edge (bb);
6646 basic_block cond_bb = e->src;
6647 gimple stmt = last_stmt (cond_bb);
6648 tree cst;
6650 if (stmt == NULL
6651 || gimple_code (stmt) != GIMPLE_COND
6652 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6653 ? EQ_EXPR : NE_EXPR)
6654 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6655 || !integer_zerop (gimple_cond_rhs (stmt)))
6656 return;
6658 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6659 if (!is_gimple_assign (stmt)
6660 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6661 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6662 return;
6663 if (gimple_assign_rhs1 (stmt) != var)
6665 gimple stmt2;
6667 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6668 return;
6669 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6670 if (!gimple_assign_cast_p (stmt2)
6671 || gimple_assign_rhs1 (stmt2) != var
6672 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6673 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6674 != TYPE_PRECISION (TREE_TYPE (var))))
6675 return;
6677 cst = gimple_assign_rhs2 (stmt);
6678 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6681 /* Convert range assertion expressions into the implied copies and
6682 copy propagate away the copies. Doing the trivial copy propagation
6683 here avoids the need to run the full copy propagation pass after
6684 VRP.
6686 FIXME, this will eventually lead to copy propagation removing the
6687 names that had useful range information attached to them. For
6688 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6689 then N_i will have the range [3, +INF].
6691 However, by converting the assertion into the implied copy
6692 operation N_i = N_j, we will then copy-propagate N_j into the uses
6693 of N_i and lose the range information. We may want to hold on to
6694 ASSERT_EXPRs a little while longer as the ranges could be used in
6695 things like jump threading.
6697 The problem with keeping ASSERT_EXPRs around is that passes after
6698 VRP need to handle them appropriately.
6700 Another approach would be to make the range information a first
6701 class property of the SSA_NAME so that it can be queried from
6702 any pass. This is made somewhat more complex by the need for
6703 multiple ranges to be associated with one SSA_NAME. */
6705 static void
6706 remove_range_assertions (void)
6708 basic_block bb;
6709 gimple_stmt_iterator si;
6710 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6711 a basic block preceeded by GIMPLE_COND branching to it and
6712 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6713 int is_unreachable;
6715 /* Note that the BSI iterator bump happens at the bottom of the
6716 loop and no bump is necessary if we're removing the statement
6717 referenced by the current BSI. */
6718 FOR_EACH_BB_FN (bb, cfun)
6719 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6721 gimple stmt = gsi_stmt (si);
6722 gimple use_stmt;
6724 if (is_gimple_assign (stmt)
6725 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6727 tree lhs = gimple_assign_lhs (stmt);
6728 tree rhs = gimple_assign_rhs1 (stmt);
6729 tree var;
6730 tree cond = fold (ASSERT_EXPR_COND (rhs));
6731 use_operand_p use_p;
6732 imm_use_iterator iter;
6734 gcc_assert (cond != boolean_false_node);
6736 var = ASSERT_EXPR_VAR (rhs);
6737 gcc_assert (TREE_CODE (var) == SSA_NAME);
6739 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6740 && SSA_NAME_RANGE_INFO (lhs))
6742 if (is_unreachable == -1)
6744 is_unreachable = 0;
6745 if (single_pred_p (bb)
6746 && assert_unreachable_fallthru_edge_p
6747 (single_pred_edge (bb)))
6748 is_unreachable = 1;
6750 /* Handle
6751 if (x_7 >= 10 && x_7 < 20)
6752 __builtin_unreachable ();
6753 x_8 = ASSERT_EXPR <x_7, ...>;
6754 if the only uses of x_7 are in the ASSERT_EXPR and
6755 in the condition. In that case, we can copy the
6756 range info from x_8 computed in this pass also
6757 for x_7. */
6758 if (is_unreachable
6759 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6760 single_pred (bb)))
6762 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6763 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6764 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6765 maybe_set_nonzero_bits (bb, var);
6769 /* Propagate the RHS into every use of the LHS. */
6770 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6771 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6772 SET_USE (use_p, var);
6774 /* And finally, remove the copy, it is not needed. */
6775 gsi_remove (&si, true);
6776 release_defs (stmt);
6778 else
6780 if (!is_gimple_debug (gsi_stmt (si)))
6781 is_unreachable = 0;
6782 gsi_next (&si);
6788 /* Return true if STMT is interesting for VRP. */
6790 static bool
6791 stmt_interesting_for_vrp (gimple stmt)
6793 if (gimple_code (stmt) == GIMPLE_PHI)
6795 tree res = gimple_phi_result (stmt);
6796 return (!virtual_operand_p (res)
6797 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6798 || POINTER_TYPE_P (TREE_TYPE (res))));
6800 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6802 tree lhs = gimple_get_lhs (stmt);
6804 /* In general, assignments with virtual operands are not useful
6805 for deriving ranges, with the obvious exception of calls to
6806 builtin functions. */
6807 if (lhs && TREE_CODE (lhs) == SSA_NAME
6808 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6809 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6810 && (is_gimple_call (stmt)
6811 || !gimple_vuse (stmt)))
6812 return true;
6814 else if (gimple_code (stmt) == GIMPLE_COND
6815 || gimple_code (stmt) == GIMPLE_SWITCH)
6816 return true;
6818 return false;
6822 /* Initialize local data structures for VRP. */
6824 static void
6825 vrp_initialize (void)
6827 basic_block bb;
6829 values_propagated = false;
6830 num_vr_values = num_ssa_names;
6831 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6832 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6834 FOR_EACH_BB_FN (bb, cfun)
6836 gimple_stmt_iterator si;
6838 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6840 gimple phi = gsi_stmt (si);
6841 if (!stmt_interesting_for_vrp (phi))
6843 tree lhs = PHI_RESULT (phi);
6844 set_value_range_to_varying (get_value_range (lhs));
6845 prop_set_simulate_again (phi, false);
6847 else
6848 prop_set_simulate_again (phi, true);
6851 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6853 gimple stmt = gsi_stmt (si);
6855 /* If the statement is a control insn, then we do not
6856 want to avoid simulating the statement once. Failure
6857 to do so means that those edges will never get added. */
6858 if (stmt_ends_bb_p (stmt))
6859 prop_set_simulate_again (stmt, true);
6860 else if (!stmt_interesting_for_vrp (stmt))
6862 ssa_op_iter i;
6863 tree def;
6864 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6865 set_value_range_to_varying (get_value_range (def));
6866 prop_set_simulate_again (stmt, false);
6868 else
6869 prop_set_simulate_again (stmt, true);
6874 /* Return the singleton value-range for NAME or NAME. */
6876 static inline tree
6877 vrp_valueize (tree name)
6879 if (TREE_CODE (name) == SSA_NAME)
6881 value_range_t *vr = get_value_range (name);
6882 if (vr->type == VR_RANGE
6883 && (vr->min == vr->max
6884 || operand_equal_p (vr->min, vr->max, 0)))
6885 return vr->min;
6887 return name;
6890 /* Visit assignment STMT. If it produces an interesting range, record
6891 the SSA name in *OUTPUT_P. */
6893 static enum ssa_prop_result
6894 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6896 tree def, lhs;
6897 ssa_op_iter iter;
6898 enum gimple_code code = gimple_code (stmt);
6899 lhs = gimple_get_lhs (stmt);
6901 /* We only keep track of ranges in integral and pointer types. */
6902 if (TREE_CODE (lhs) == SSA_NAME
6903 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6904 /* It is valid to have NULL MIN/MAX values on a type. See
6905 build_range_type. */
6906 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6907 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6908 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6910 value_range_t new_vr = VR_INITIALIZER;
6912 /* Try folding the statement to a constant first. */
6913 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6914 if (tem)
6915 set_value_range_to_value (&new_vr, tem, NULL);
6916 /* Then dispatch to value-range extracting functions. */
6917 else if (code == GIMPLE_CALL)
6918 extract_range_basic (&new_vr, stmt);
6919 else
6920 extract_range_from_assignment (&new_vr, stmt);
6922 if (update_value_range (lhs, &new_vr))
6924 *output_p = lhs;
6926 if (dump_file && (dump_flags & TDF_DETAILS))
6928 fprintf (dump_file, "Found new range for ");
6929 print_generic_expr (dump_file, lhs, 0);
6930 fprintf (dump_file, ": ");
6931 dump_value_range (dump_file, &new_vr);
6932 fprintf (dump_file, "\n");
6935 if (new_vr.type == VR_VARYING)
6936 return SSA_PROP_VARYING;
6938 return SSA_PROP_INTERESTING;
6941 return SSA_PROP_NOT_INTERESTING;
6944 /* Every other statement produces no useful ranges. */
6945 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6946 set_value_range_to_varying (get_value_range (def));
6948 return SSA_PROP_VARYING;
6951 /* Helper that gets the value range of the SSA_NAME with version I
6952 or a symbolic range containing the SSA_NAME only if the value range
6953 is varying or undefined. */
6955 static inline value_range_t
6956 get_vr_for_comparison (int i)
6958 value_range_t vr = *get_value_range (ssa_name (i));
6960 /* If name N_i does not have a valid range, use N_i as its own
6961 range. This allows us to compare against names that may
6962 have N_i in their ranges. */
6963 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6965 vr.type = VR_RANGE;
6966 vr.min = ssa_name (i);
6967 vr.max = ssa_name (i);
6970 return vr;
6973 /* Compare all the value ranges for names equivalent to VAR with VAL
6974 using comparison code COMP. Return the same value returned by
6975 compare_range_with_value, including the setting of
6976 *STRICT_OVERFLOW_P. */
6978 static tree
6979 compare_name_with_value (enum tree_code comp, tree var, tree val,
6980 bool *strict_overflow_p)
6982 bitmap_iterator bi;
6983 unsigned i;
6984 bitmap e;
6985 tree retval, t;
6986 int used_strict_overflow;
6987 bool sop;
6988 value_range_t equiv_vr;
6990 /* Get the set of equivalences for VAR. */
6991 e = get_value_range (var)->equiv;
6993 /* Start at -1. Set it to 0 if we do a comparison without relying
6994 on overflow, or 1 if all comparisons rely on overflow. */
6995 used_strict_overflow = -1;
6997 /* Compare vars' value range with val. */
6998 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6999 sop = false;
7000 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7001 if (retval)
7002 used_strict_overflow = sop ? 1 : 0;
7004 /* If the equiv set is empty we have done all work we need to do. */
7005 if (e == NULL)
7007 if (retval
7008 && used_strict_overflow > 0)
7009 *strict_overflow_p = true;
7010 return retval;
7013 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7015 equiv_vr = get_vr_for_comparison (i);
7016 sop = false;
7017 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7018 if (t)
7020 /* If we get different answers from different members
7021 of the equivalence set this check must be in a dead
7022 code region. Folding it to a trap representation
7023 would be correct here. For now just return don't-know. */
7024 if (retval != NULL
7025 && t != retval)
7027 retval = NULL_TREE;
7028 break;
7030 retval = t;
7032 if (!sop)
7033 used_strict_overflow = 0;
7034 else if (used_strict_overflow < 0)
7035 used_strict_overflow = 1;
7039 if (retval
7040 && used_strict_overflow > 0)
7041 *strict_overflow_p = true;
7043 return retval;
7047 /* Given a comparison code COMP and names N1 and N2, compare all the
7048 ranges equivalent to N1 against all the ranges equivalent to N2
7049 to determine the value of N1 COMP N2. Return the same value
7050 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7051 whether we relied on an overflow infinity in the comparison. */
7054 static tree
7055 compare_names (enum tree_code comp, tree n1, tree n2,
7056 bool *strict_overflow_p)
7058 tree t, retval;
7059 bitmap e1, e2;
7060 bitmap_iterator bi1, bi2;
7061 unsigned i1, i2;
7062 int used_strict_overflow;
7063 static bitmap_obstack *s_obstack = NULL;
7064 static bitmap s_e1 = NULL, s_e2 = NULL;
7066 /* Compare the ranges of every name equivalent to N1 against the
7067 ranges of every name equivalent to N2. */
7068 e1 = get_value_range (n1)->equiv;
7069 e2 = get_value_range (n2)->equiv;
7071 /* Use the fake bitmaps if e1 or e2 are not available. */
7072 if (s_obstack == NULL)
7074 s_obstack = XNEW (bitmap_obstack);
7075 bitmap_obstack_initialize (s_obstack);
7076 s_e1 = BITMAP_ALLOC (s_obstack);
7077 s_e2 = BITMAP_ALLOC (s_obstack);
7079 if (e1 == NULL)
7080 e1 = s_e1;
7081 if (e2 == NULL)
7082 e2 = s_e2;
7084 /* Add N1 and N2 to their own set of equivalences to avoid
7085 duplicating the body of the loop just to check N1 and N2
7086 ranges. */
7087 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7088 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7090 /* If the equivalence sets have a common intersection, then the two
7091 names can be compared without checking their ranges. */
7092 if (bitmap_intersect_p (e1, e2))
7094 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7095 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7097 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7098 ? boolean_true_node
7099 : boolean_false_node;
7102 /* Start at -1. Set it to 0 if we do a comparison without relying
7103 on overflow, or 1 if all comparisons rely on overflow. */
7104 used_strict_overflow = -1;
7106 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7107 N2 to their own set of equivalences to avoid duplicating the body
7108 of the loop just to check N1 and N2 ranges. */
7109 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7111 value_range_t vr1 = get_vr_for_comparison (i1);
7113 t = retval = NULL_TREE;
7114 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7116 bool sop = false;
7118 value_range_t vr2 = get_vr_for_comparison (i2);
7120 t = compare_ranges (comp, &vr1, &vr2, &sop);
7121 if (t)
7123 /* If we get different answers from different members
7124 of the equivalence set this check must be in a dead
7125 code region. Folding it to a trap representation
7126 would be correct here. For now just return don't-know. */
7127 if (retval != NULL
7128 && t != retval)
7130 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7131 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7132 return NULL_TREE;
7134 retval = t;
7136 if (!sop)
7137 used_strict_overflow = 0;
7138 else if (used_strict_overflow < 0)
7139 used_strict_overflow = 1;
7143 if (retval)
7145 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7146 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7147 if (used_strict_overflow > 0)
7148 *strict_overflow_p = true;
7149 return retval;
7153 /* None of the equivalent ranges are useful in computing this
7154 comparison. */
7155 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7156 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7157 return NULL_TREE;
7160 /* Helper function for vrp_evaluate_conditional_warnv. */
7162 static tree
7163 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7164 tree op0, tree op1,
7165 bool * strict_overflow_p)
7167 value_range_t *vr0, *vr1;
7169 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7170 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7172 tree res = NULL_TREE;
7173 if (vr0 && vr1)
7174 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7175 if (!res && vr0)
7176 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7177 if (!res && vr1)
7178 res = (compare_range_with_value
7179 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7180 return res;
7183 /* Helper function for vrp_evaluate_conditional_warnv. */
7185 static tree
7186 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7187 tree op1, bool use_equiv_p,
7188 bool *strict_overflow_p, bool *only_ranges)
7190 tree ret;
7191 if (only_ranges)
7192 *only_ranges = true;
7194 /* We only deal with integral and pointer types. */
7195 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7196 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7197 return NULL_TREE;
7199 if (use_equiv_p)
7201 if (only_ranges
7202 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7203 (code, op0, op1, strict_overflow_p)))
7204 return ret;
7205 *only_ranges = false;
7206 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7207 return compare_names (code, op0, op1, strict_overflow_p);
7208 else if (TREE_CODE (op0) == SSA_NAME)
7209 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7210 else if (TREE_CODE (op1) == SSA_NAME)
7211 return (compare_name_with_value
7212 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7214 else
7215 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7216 strict_overflow_p);
7217 return NULL_TREE;
7220 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7221 information. Return NULL if the conditional can not be evaluated.
7222 The ranges of all the names equivalent with the operands in COND
7223 will be used when trying to compute the value. If the result is
7224 based on undefined signed overflow, issue a warning if
7225 appropriate. */
7227 static tree
7228 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7230 bool sop;
7231 tree ret;
7232 bool only_ranges;
7234 /* Some passes and foldings leak constants with overflow flag set
7235 into the IL. Avoid doing wrong things with these and bail out. */
7236 if ((TREE_CODE (op0) == INTEGER_CST
7237 && TREE_OVERFLOW (op0))
7238 || (TREE_CODE (op1) == INTEGER_CST
7239 && TREE_OVERFLOW (op1)))
7240 return NULL_TREE;
7242 sop = false;
7243 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7244 &only_ranges);
7246 if (ret && sop)
7248 enum warn_strict_overflow_code wc;
7249 const char* warnmsg;
7251 if (is_gimple_min_invariant (ret))
7253 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7254 warnmsg = G_("assuming signed overflow does not occur when "
7255 "simplifying conditional to constant");
7257 else
7259 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7260 warnmsg = G_("assuming signed overflow does not occur when "
7261 "simplifying conditional");
7264 if (issue_strict_overflow_warning (wc))
7266 location_t location;
7268 if (!gimple_has_location (stmt))
7269 location = input_location;
7270 else
7271 location = gimple_location (stmt);
7272 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7276 if (warn_type_limits
7277 && ret && only_ranges
7278 && TREE_CODE_CLASS (code) == tcc_comparison
7279 && TREE_CODE (op0) == SSA_NAME)
7281 /* If the comparison is being folded and the operand on the LHS
7282 is being compared against a constant value that is outside of
7283 the natural range of OP0's type, then the predicate will
7284 always fold regardless of the value of OP0. If -Wtype-limits
7285 was specified, emit a warning. */
7286 tree type = TREE_TYPE (op0);
7287 value_range_t *vr0 = get_value_range (op0);
7289 if (vr0->type != VR_VARYING
7290 && INTEGRAL_TYPE_P (type)
7291 && vrp_val_is_min (vr0->min)
7292 && vrp_val_is_max (vr0->max)
7293 && is_gimple_min_invariant (op1))
7295 location_t location;
7297 if (!gimple_has_location (stmt))
7298 location = input_location;
7299 else
7300 location = gimple_location (stmt);
7302 warning_at (location, OPT_Wtype_limits,
7303 integer_zerop (ret)
7304 ? G_("comparison always false "
7305 "due to limited range of data type")
7306 : G_("comparison always true "
7307 "due to limited range of data type"));
7311 return ret;
7315 /* Visit conditional statement STMT. If we can determine which edge
7316 will be taken out of STMT's basic block, record it in
7317 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7318 SSA_PROP_VARYING. */
7320 static enum ssa_prop_result
7321 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
7323 tree val;
7324 bool sop;
7326 *taken_edge_p = NULL;
7328 if (dump_file && (dump_flags & TDF_DETAILS))
7330 tree use;
7331 ssa_op_iter i;
7333 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7334 print_gimple_stmt (dump_file, stmt, 0, 0);
7335 fprintf (dump_file, "\nWith known ranges\n");
7337 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7339 fprintf (dump_file, "\t");
7340 print_generic_expr (dump_file, use, 0);
7341 fprintf (dump_file, ": ");
7342 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7345 fprintf (dump_file, "\n");
7348 /* Compute the value of the predicate COND by checking the known
7349 ranges of each of its operands.
7351 Note that we cannot evaluate all the equivalent ranges here
7352 because those ranges may not yet be final and with the current
7353 propagation strategy, we cannot determine when the value ranges
7354 of the names in the equivalence set have changed.
7356 For instance, given the following code fragment
7358 i_5 = PHI <8, i_13>
7360 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7361 if (i_14 == 1)
7364 Assume that on the first visit to i_14, i_5 has the temporary
7365 range [8, 8] because the second argument to the PHI function is
7366 not yet executable. We derive the range ~[0, 0] for i_14 and the
7367 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7368 the first time, since i_14 is equivalent to the range [8, 8], we
7369 determine that the predicate is always false.
7371 On the next round of propagation, i_13 is determined to be
7372 VARYING, which causes i_5 to drop down to VARYING. So, another
7373 visit to i_14 is scheduled. In this second visit, we compute the
7374 exact same range and equivalence set for i_14, namely ~[0, 0] and
7375 { i_5 }. But we did not have the previous range for i_5
7376 registered, so vrp_visit_assignment thinks that the range for
7377 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7378 is not visited again, which stops propagation from visiting
7379 statements in the THEN clause of that if().
7381 To properly fix this we would need to keep the previous range
7382 value for the names in the equivalence set. This way we would've
7383 discovered that from one visit to the other i_5 changed from
7384 range [8, 8] to VR_VARYING.
7386 However, fixing this apparent limitation may not be worth the
7387 additional checking. Testing on several code bases (GCC, DLV,
7388 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7389 4 more predicates folded in SPEC. */
7390 sop = false;
7392 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7393 gimple_cond_lhs (stmt),
7394 gimple_cond_rhs (stmt),
7395 false, &sop, NULL);
7396 if (val)
7398 if (!sop)
7399 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7400 else
7402 if (dump_file && (dump_flags & TDF_DETAILS))
7403 fprintf (dump_file,
7404 "\nIgnoring predicate evaluation because "
7405 "it assumes that signed overflow is undefined");
7406 val = NULL_TREE;
7410 if (dump_file && (dump_flags & TDF_DETAILS))
7412 fprintf (dump_file, "\nPredicate evaluates to: ");
7413 if (val == NULL_TREE)
7414 fprintf (dump_file, "DON'T KNOW\n");
7415 else
7416 print_generic_stmt (dump_file, val, 0);
7419 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7422 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7423 that includes the value VAL. The search is restricted to the range
7424 [START_IDX, n - 1] where n is the size of VEC.
7426 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7427 returned.
7429 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7430 it is placed in IDX and false is returned.
7432 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7433 returned. */
7435 static bool
7436 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
7438 size_t n = gimple_switch_num_labels (stmt);
7439 size_t low, high;
7441 /* Find case label for minimum of the value range or the next one.
7442 At each iteration we are searching in [low, high - 1]. */
7444 for (low = start_idx, high = n; high != low; )
7446 tree t;
7447 int cmp;
7448 /* Note that i != high, so we never ask for n. */
7449 size_t i = (high + low) / 2;
7450 t = gimple_switch_label (stmt, i);
7452 /* Cache the result of comparing CASE_LOW and val. */
7453 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7455 if (cmp == 0)
7457 /* Ranges cannot be empty. */
7458 *idx = i;
7459 return true;
7461 else if (cmp > 0)
7462 high = i;
7463 else
7465 low = i + 1;
7466 if (CASE_HIGH (t) != NULL
7467 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7469 *idx = i;
7470 return true;
7475 *idx = high;
7476 return false;
7479 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7480 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7481 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7482 then MAX_IDX < MIN_IDX.
7483 Returns true if the default label is not needed. */
7485 static bool
7486 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
7487 size_t *max_idx)
7489 size_t i, j;
7490 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7491 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7493 if (i == j
7494 && min_take_default
7495 && max_take_default)
7497 /* Only the default case label reached.
7498 Return an empty range. */
7499 *min_idx = 1;
7500 *max_idx = 0;
7501 return false;
7503 else
7505 bool take_default = min_take_default || max_take_default;
7506 tree low, high;
7507 size_t k;
7509 if (max_take_default)
7510 j--;
7512 /* If the case label range is continuous, we do not need
7513 the default case label. Verify that. */
7514 high = CASE_LOW (gimple_switch_label (stmt, i));
7515 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7516 high = CASE_HIGH (gimple_switch_label (stmt, i));
7517 for (k = i + 1; k <= j; ++k)
7519 low = CASE_LOW (gimple_switch_label (stmt, k));
7520 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7522 take_default = true;
7523 break;
7525 high = low;
7526 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7527 high = CASE_HIGH (gimple_switch_label (stmt, k));
7530 *min_idx = i;
7531 *max_idx = j;
7532 return !take_default;
7536 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7537 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7538 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7539 Returns true if the default label is not needed. */
7541 static bool
7542 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
7543 size_t *max_idx1, size_t *min_idx2,
7544 size_t *max_idx2)
7546 size_t i, j, k, l;
7547 unsigned int n = gimple_switch_num_labels (stmt);
7548 bool take_default;
7549 tree case_low, case_high;
7550 tree min = vr->min, max = vr->max;
7552 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7554 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7556 /* Set second range to emtpy. */
7557 *min_idx2 = 1;
7558 *max_idx2 = 0;
7560 if (vr->type == VR_RANGE)
7562 *min_idx1 = i;
7563 *max_idx1 = j;
7564 return !take_default;
7567 /* Set first range to all case labels. */
7568 *min_idx1 = 1;
7569 *max_idx1 = n - 1;
7571 if (i > j)
7572 return false;
7574 /* Make sure all the values of case labels [i , j] are contained in
7575 range [MIN, MAX]. */
7576 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7577 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7578 if (tree_int_cst_compare (case_low, min) < 0)
7579 i += 1;
7580 if (case_high != NULL_TREE
7581 && tree_int_cst_compare (max, case_high) < 0)
7582 j -= 1;
7584 if (i > j)
7585 return false;
7587 /* If the range spans case labels [i, j], the corresponding anti-range spans
7588 the labels [1, i - 1] and [j + 1, n - 1]. */
7589 k = j + 1;
7590 l = n - 1;
7591 if (k > l)
7593 k = 1;
7594 l = 0;
7597 j = i - 1;
7598 i = 1;
7599 if (i > j)
7601 i = k;
7602 j = l;
7603 k = 1;
7604 l = 0;
7607 *min_idx1 = i;
7608 *max_idx1 = j;
7609 *min_idx2 = k;
7610 *max_idx2 = l;
7611 return false;
7614 /* Visit switch statement STMT. If we can determine which edge
7615 will be taken out of STMT's basic block, record it in
7616 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7617 SSA_PROP_VARYING. */
7619 static enum ssa_prop_result
7620 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7622 tree op, val;
7623 value_range_t *vr;
7624 size_t i = 0, j = 0, k, l;
7625 bool take_default;
7627 *taken_edge_p = NULL;
7628 op = gimple_switch_index (stmt);
7629 if (TREE_CODE (op) != SSA_NAME)
7630 return SSA_PROP_VARYING;
7632 vr = get_value_range (op);
7633 if (dump_file && (dump_flags & TDF_DETAILS))
7635 fprintf (dump_file, "\nVisiting switch expression with operand ");
7636 print_generic_expr (dump_file, op, 0);
7637 fprintf (dump_file, " with known range ");
7638 dump_value_range (dump_file, vr);
7639 fprintf (dump_file, "\n");
7642 if ((vr->type != VR_RANGE
7643 && vr->type != VR_ANTI_RANGE)
7644 || symbolic_range_p (vr))
7645 return SSA_PROP_VARYING;
7647 /* Find the single edge that is taken from the switch expression. */
7648 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7650 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7651 label */
7652 if (j < i)
7654 gcc_assert (take_default);
7655 val = gimple_switch_default_label (stmt);
7657 else
7659 /* Check if labels with index i to j and maybe the default label
7660 are all reaching the same label. */
7662 val = gimple_switch_label (stmt, i);
7663 if (take_default
7664 && CASE_LABEL (gimple_switch_default_label (stmt))
7665 != CASE_LABEL (val))
7667 if (dump_file && (dump_flags & TDF_DETAILS))
7668 fprintf (dump_file, " not a single destination for this "
7669 "range\n");
7670 return SSA_PROP_VARYING;
7672 for (++i; i <= j; ++i)
7674 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7676 if (dump_file && (dump_flags & TDF_DETAILS))
7677 fprintf (dump_file, " not a single destination for this "
7678 "range\n");
7679 return SSA_PROP_VARYING;
7682 for (; k <= l; ++k)
7684 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7686 if (dump_file && (dump_flags & TDF_DETAILS))
7687 fprintf (dump_file, " not a single destination for this "
7688 "range\n");
7689 return SSA_PROP_VARYING;
7694 *taken_edge_p = find_edge (gimple_bb (stmt),
7695 label_to_block (CASE_LABEL (val)));
7697 if (dump_file && (dump_flags & TDF_DETAILS))
7699 fprintf (dump_file, " will take edge to ");
7700 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7703 return SSA_PROP_INTERESTING;
7707 /* Evaluate statement STMT. If the statement produces a useful range,
7708 return SSA_PROP_INTERESTING and record the SSA name with the
7709 interesting range into *OUTPUT_P.
7711 If STMT is a conditional branch and we can determine its truth
7712 value, the taken edge is recorded in *TAKEN_EDGE_P.
7714 If STMT produces a varying value, return SSA_PROP_VARYING. */
7716 static enum ssa_prop_result
7717 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7719 tree def;
7720 ssa_op_iter iter;
7722 if (dump_file && (dump_flags & TDF_DETAILS))
7724 fprintf (dump_file, "\nVisiting statement:\n");
7725 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7728 if (!stmt_interesting_for_vrp (stmt))
7729 gcc_assert (stmt_ends_bb_p (stmt));
7730 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7731 return vrp_visit_assignment_or_call (stmt, output_p);
7732 else if (gimple_code (stmt) == GIMPLE_COND)
7733 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7734 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7735 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7737 /* All other statements produce nothing of interest for VRP, so mark
7738 their outputs varying and prevent further simulation. */
7739 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7740 set_value_range_to_varying (get_value_range (def));
7742 return SSA_PROP_VARYING;
7745 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7746 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7747 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7748 possible such range. The resulting range is not canonicalized. */
7750 static void
7751 union_ranges (enum value_range_type *vr0type,
7752 tree *vr0min, tree *vr0max,
7753 enum value_range_type vr1type,
7754 tree vr1min, tree vr1max)
7756 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7757 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7759 /* [] is vr0, () is vr1 in the following classification comments. */
7760 if (mineq && maxeq)
7762 /* [( )] */
7763 if (*vr0type == vr1type)
7764 /* Nothing to do for equal ranges. */
7766 else if ((*vr0type == VR_RANGE
7767 && vr1type == VR_ANTI_RANGE)
7768 || (*vr0type == VR_ANTI_RANGE
7769 && vr1type == VR_RANGE))
7771 /* For anti-range with range union the result is varying. */
7772 goto give_up;
7774 else
7775 gcc_unreachable ();
7777 else if (operand_less_p (*vr0max, vr1min) == 1
7778 || operand_less_p (vr1max, *vr0min) == 1)
7780 /* [ ] ( ) or ( ) [ ]
7781 If the ranges have an empty intersection, result of the union
7782 operation is the anti-range or if both are anti-ranges
7783 it covers all. */
7784 if (*vr0type == VR_ANTI_RANGE
7785 && vr1type == VR_ANTI_RANGE)
7786 goto give_up;
7787 else if (*vr0type == VR_ANTI_RANGE
7788 && vr1type == VR_RANGE)
7790 else if (*vr0type == VR_RANGE
7791 && vr1type == VR_ANTI_RANGE)
7793 *vr0type = vr1type;
7794 *vr0min = vr1min;
7795 *vr0max = vr1max;
7797 else if (*vr0type == VR_RANGE
7798 && vr1type == VR_RANGE)
7800 /* The result is the convex hull of both ranges. */
7801 if (operand_less_p (*vr0max, vr1min) == 1)
7803 /* If the result can be an anti-range, create one. */
7804 if (TREE_CODE (*vr0max) == INTEGER_CST
7805 && TREE_CODE (vr1min) == INTEGER_CST
7806 && vrp_val_is_min (*vr0min)
7807 && vrp_val_is_max (vr1max))
7809 tree min = int_const_binop (PLUS_EXPR,
7810 *vr0max,
7811 build_int_cst (TREE_TYPE (*vr0max), 1));
7812 tree max = int_const_binop (MINUS_EXPR,
7813 vr1min,
7814 build_int_cst (TREE_TYPE (vr1min), 1));
7815 if (!operand_less_p (max, min))
7817 *vr0type = VR_ANTI_RANGE;
7818 *vr0min = min;
7819 *vr0max = max;
7821 else
7822 *vr0max = vr1max;
7824 else
7825 *vr0max = vr1max;
7827 else
7829 /* If the result can be an anti-range, create one. */
7830 if (TREE_CODE (vr1max) == INTEGER_CST
7831 && TREE_CODE (*vr0min) == INTEGER_CST
7832 && vrp_val_is_min (vr1min)
7833 && vrp_val_is_max (*vr0max))
7835 tree min = int_const_binop (PLUS_EXPR,
7836 vr1max,
7837 build_int_cst (TREE_TYPE (vr1max), 1));
7838 tree max = int_const_binop (MINUS_EXPR,
7839 *vr0min,
7840 build_int_cst (TREE_TYPE (*vr0min), 1));
7841 if (!operand_less_p (max, min))
7843 *vr0type = VR_ANTI_RANGE;
7844 *vr0min = min;
7845 *vr0max = max;
7847 else
7848 *vr0min = vr1min;
7850 else
7851 *vr0min = vr1min;
7854 else
7855 gcc_unreachable ();
7857 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7858 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7860 /* [ ( ) ] or [( ) ] or [ ( )] */
7861 if (*vr0type == VR_RANGE
7862 && vr1type == VR_RANGE)
7864 else if (*vr0type == VR_ANTI_RANGE
7865 && vr1type == VR_ANTI_RANGE)
7867 *vr0type = vr1type;
7868 *vr0min = vr1min;
7869 *vr0max = vr1max;
7871 else if (*vr0type == VR_ANTI_RANGE
7872 && vr1type == VR_RANGE)
7874 /* Arbitrarily choose the right or left gap. */
7875 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7876 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7877 build_int_cst (TREE_TYPE (vr1min), 1));
7878 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7879 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7880 build_int_cst (TREE_TYPE (vr1max), 1));
7881 else
7882 goto give_up;
7884 else if (*vr0type == VR_RANGE
7885 && vr1type == VR_ANTI_RANGE)
7886 /* The result covers everything. */
7887 goto give_up;
7888 else
7889 gcc_unreachable ();
7891 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7892 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7894 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7895 if (*vr0type == VR_RANGE
7896 && vr1type == VR_RANGE)
7898 *vr0type = vr1type;
7899 *vr0min = vr1min;
7900 *vr0max = vr1max;
7902 else if (*vr0type == VR_ANTI_RANGE
7903 && vr1type == VR_ANTI_RANGE)
7905 else if (*vr0type == VR_RANGE
7906 && vr1type == VR_ANTI_RANGE)
7908 *vr0type = VR_ANTI_RANGE;
7909 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7911 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7912 build_int_cst (TREE_TYPE (*vr0min), 1));
7913 *vr0min = vr1min;
7915 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7917 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7918 build_int_cst (TREE_TYPE (*vr0max), 1));
7919 *vr0max = vr1max;
7921 else
7922 goto give_up;
7924 else if (*vr0type == VR_ANTI_RANGE
7925 && vr1type == VR_RANGE)
7926 /* The result covers everything. */
7927 goto give_up;
7928 else
7929 gcc_unreachable ();
7931 else if ((operand_less_p (vr1min, *vr0max) == 1
7932 || operand_equal_p (vr1min, *vr0max, 0))
7933 && operand_less_p (*vr0min, vr1min) == 1
7934 && operand_less_p (*vr0max, vr1max) == 1)
7936 /* [ ( ] ) or [ ]( ) */
7937 if (*vr0type == VR_RANGE
7938 && vr1type == VR_RANGE)
7939 *vr0max = vr1max;
7940 else if (*vr0type == VR_ANTI_RANGE
7941 && vr1type == VR_ANTI_RANGE)
7942 *vr0min = vr1min;
7943 else if (*vr0type == VR_ANTI_RANGE
7944 && vr1type == VR_RANGE)
7946 if (TREE_CODE (vr1min) == INTEGER_CST)
7947 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7948 build_int_cst (TREE_TYPE (vr1min), 1));
7949 else
7950 goto give_up;
7952 else if (*vr0type == VR_RANGE
7953 && vr1type == VR_ANTI_RANGE)
7955 if (TREE_CODE (*vr0max) == INTEGER_CST)
7957 *vr0type = vr1type;
7958 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7959 build_int_cst (TREE_TYPE (*vr0max), 1));
7960 *vr0max = vr1max;
7962 else
7963 goto give_up;
7965 else
7966 gcc_unreachable ();
7968 else if ((operand_less_p (*vr0min, vr1max) == 1
7969 || operand_equal_p (*vr0min, vr1max, 0))
7970 && operand_less_p (vr1min, *vr0min) == 1
7971 && operand_less_p (vr1max, *vr0max) == 1)
7973 /* ( [ ) ] or ( )[ ] */
7974 if (*vr0type == VR_RANGE
7975 && vr1type == VR_RANGE)
7976 *vr0min = vr1min;
7977 else if (*vr0type == VR_ANTI_RANGE
7978 && vr1type == VR_ANTI_RANGE)
7979 *vr0max = vr1max;
7980 else if (*vr0type == VR_ANTI_RANGE
7981 && vr1type == VR_RANGE)
7983 if (TREE_CODE (vr1max) == INTEGER_CST)
7984 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7985 build_int_cst (TREE_TYPE (vr1max), 1));
7986 else
7987 goto give_up;
7989 else if (*vr0type == VR_RANGE
7990 && vr1type == VR_ANTI_RANGE)
7992 if (TREE_CODE (*vr0min) == INTEGER_CST)
7994 *vr0type = vr1type;
7995 *vr0min = vr1min;
7996 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7997 build_int_cst (TREE_TYPE (*vr0min), 1));
7999 else
8000 goto give_up;
8002 else
8003 gcc_unreachable ();
8005 else
8006 goto give_up;
8008 return;
8010 give_up:
8011 *vr0type = VR_VARYING;
8012 *vr0min = NULL_TREE;
8013 *vr0max = NULL_TREE;
8016 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8017 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8018 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8019 possible such range. The resulting range is not canonicalized. */
8021 static void
8022 intersect_ranges (enum value_range_type *vr0type,
8023 tree *vr0min, tree *vr0max,
8024 enum value_range_type vr1type,
8025 tree vr1min, tree vr1max)
8027 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8028 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8030 /* [] is vr0, () is vr1 in the following classification comments. */
8031 if (mineq && maxeq)
8033 /* [( )] */
8034 if (*vr0type == vr1type)
8035 /* Nothing to do for equal ranges. */
8037 else if ((*vr0type == VR_RANGE
8038 && vr1type == VR_ANTI_RANGE)
8039 || (*vr0type == VR_ANTI_RANGE
8040 && vr1type == VR_RANGE))
8042 /* For anti-range with range intersection the result is empty. */
8043 *vr0type = VR_UNDEFINED;
8044 *vr0min = NULL_TREE;
8045 *vr0max = NULL_TREE;
8047 else
8048 gcc_unreachable ();
8050 else if (operand_less_p (*vr0max, vr1min) == 1
8051 || operand_less_p (vr1max, *vr0min) == 1)
8053 /* [ ] ( ) or ( ) [ ]
8054 If the ranges have an empty intersection, the result of the
8055 intersect operation is the range for intersecting an
8056 anti-range with a range or empty when intersecting two ranges. */
8057 if (*vr0type == VR_RANGE
8058 && vr1type == VR_ANTI_RANGE)
8060 else if (*vr0type == VR_ANTI_RANGE
8061 && vr1type == VR_RANGE)
8063 *vr0type = vr1type;
8064 *vr0min = vr1min;
8065 *vr0max = vr1max;
8067 else if (*vr0type == VR_RANGE
8068 && vr1type == VR_RANGE)
8070 *vr0type = VR_UNDEFINED;
8071 *vr0min = NULL_TREE;
8072 *vr0max = NULL_TREE;
8074 else if (*vr0type == VR_ANTI_RANGE
8075 && vr1type == VR_ANTI_RANGE)
8077 /* If the anti-ranges are adjacent to each other merge them. */
8078 if (TREE_CODE (*vr0max) == INTEGER_CST
8079 && TREE_CODE (vr1min) == INTEGER_CST
8080 && operand_less_p (*vr0max, vr1min) == 1
8081 && integer_onep (int_const_binop (MINUS_EXPR,
8082 vr1min, *vr0max)))
8083 *vr0max = vr1max;
8084 else if (TREE_CODE (vr1max) == INTEGER_CST
8085 && TREE_CODE (*vr0min) == INTEGER_CST
8086 && operand_less_p (vr1max, *vr0min) == 1
8087 && integer_onep (int_const_binop (MINUS_EXPR,
8088 *vr0min, vr1max)))
8089 *vr0min = vr1min;
8090 /* Else arbitrarily take VR0. */
8093 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8094 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8096 /* [ ( ) ] or [( ) ] or [ ( )] */
8097 if (*vr0type == VR_RANGE
8098 && vr1type == VR_RANGE)
8100 /* If both are ranges the result is the inner one. */
8101 *vr0type = vr1type;
8102 *vr0min = vr1min;
8103 *vr0max = vr1max;
8105 else if (*vr0type == VR_RANGE
8106 && vr1type == VR_ANTI_RANGE)
8108 /* Choose the right gap if the left one is empty. */
8109 if (mineq)
8111 if (TREE_CODE (vr1max) == INTEGER_CST)
8112 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8113 build_int_cst (TREE_TYPE (vr1max), 1));
8114 else
8115 *vr0min = vr1max;
8117 /* Choose the left gap if the right one is empty. */
8118 else if (maxeq)
8120 if (TREE_CODE (vr1min) == INTEGER_CST)
8121 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8122 build_int_cst (TREE_TYPE (vr1min), 1));
8123 else
8124 *vr0max = vr1min;
8126 /* Choose the anti-range if the range is effectively varying. */
8127 else if (vrp_val_is_min (*vr0min)
8128 && vrp_val_is_max (*vr0max))
8130 *vr0type = vr1type;
8131 *vr0min = vr1min;
8132 *vr0max = vr1max;
8134 /* Else choose the range. */
8136 else if (*vr0type == VR_ANTI_RANGE
8137 && vr1type == VR_ANTI_RANGE)
8138 /* If both are anti-ranges the result is the outer one. */
8140 else if (*vr0type == VR_ANTI_RANGE
8141 && vr1type == VR_RANGE)
8143 /* The intersection is empty. */
8144 *vr0type = VR_UNDEFINED;
8145 *vr0min = NULL_TREE;
8146 *vr0max = NULL_TREE;
8148 else
8149 gcc_unreachable ();
8151 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8152 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8154 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8155 if (*vr0type == VR_RANGE
8156 && vr1type == VR_RANGE)
8157 /* Choose the inner range. */
8159 else if (*vr0type == VR_ANTI_RANGE
8160 && vr1type == VR_RANGE)
8162 /* Choose the right gap if the left is empty. */
8163 if (mineq)
8165 *vr0type = VR_RANGE;
8166 if (TREE_CODE (*vr0max) == INTEGER_CST)
8167 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8168 build_int_cst (TREE_TYPE (*vr0max), 1));
8169 else
8170 *vr0min = *vr0max;
8171 *vr0max = vr1max;
8173 /* Choose the left gap if the right is empty. */
8174 else if (maxeq)
8176 *vr0type = VR_RANGE;
8177 if (TREE_CODE (*vr0min) == INTEGER_CST)
8178 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8179 build_int_cst (TREE_TYPE (*vr0min), 1));
8180 else
8181 *vr0max = *vr0min;
8182 *vr0min = vr1min;
8184 /* Choose the anti-range if the range is effectively varying. */
8185 else if (vrp_val_is_min (vr1min)
8186 && vrp_val_is_max (vr1max))
8188 /* Else choose the range. */
8189 else
8191 *vr0type = vr1type;
8192 *vr0min = vr1min;
8193 *vr0max = vr1max;
8196 else if (*vr0type == VR_ANTI_RANGE
8197 && vr1type == VR_ANTI_RANGE)
8199 /* If both are anti-ranges the result is the outer one. */
8200 *vr0type = vr1type;
8201 *vr0min = vr1min;
8202 *vr0max = vr1max;
8204 else if (vr1type == VR_ANTI_RANGE
8205 && *vr0type == VR_RANGE)
8207 /* The intersection is empty. */
8208 *vr0type = VR_UNDEFINED;
8209 *vr0min = NULL_TREE;
8210 *vr0max = NULL_TREE;
8212 else
8213 gcc_unreachable ();
8215 else if ((operand_less_p (vr1min, *vr0max) == 1
8216 || operand_equal_p (vr1min, *vr0max, 0))
8217 && operand_less_p (*vr0min, vr1min) == 1)
8219 /* [ ( ] ) or [ ]( ) */
8220 if (*vr0type == VR_ANTI_RANGE
8221 && vr1type == VR_ANTI_RANGE)
8222 *vr0max = vr1max;
8223 else if (*vr0type == VR_RANGE
8224 && vr1type == VR_RANGE)
8225 *vr0min = vr1min;
8226 else if (*vr0type == VR_RANGE
8227 && vr1type == VR_ANTI_RANGE)
8229 if (TREE_CODE (vr1min) == INTEGER_CST)
8230 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8231 build_int_cst (TREE_TYPE (vr1min), 1));
8232 else
8233 *vr0max = vr1min;
8235 else if (*vr0type == VR_ANTI_RANGE
8236 && vr1type == VR_RANGE)
8238 *vr0type = VR_RANGE;
8239 if (TREE_CODE (*vr0max) == INTEGER_CST)
8240 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8241 build_int_cst (TREE_TYPE (*vr0max), 1));
8242 else
8243 *vr0min = *vr0max;
8244 *vr0max = vr1max;
8246 else
8247 gcc_unreachable ();
8249 else if ((operand_less_p (*vr0min, vr1max) == 1
8250 || operand_equal_p (*vr0min, vr1max, 0))
8251 && operand_less_p (vr1min, *vr0min) == 1)
8253 /* ( [ ) ] or ( )[ ] */
8254 if (*vr0type == VR_ANTI_RANGE
8255 && vr1type == VR_ANTI_RANGE)
8256 *vr0min = vr1min;
8257 else if (*vr0type == VR_RANGE
8258 && vr1type == VR_RANGE)
8259 *vr0max = vr1max;
8260 else if (*vr0type == VR_RANGE
8261 && vr1type == VR_ANTI_RANGE)
8263 if (TREE_CODE (vr1max) == INTEGER_CST)
8264 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8265 build_int_cst (TREE_TYPE (vr1max), 1));
8266 else
8267 *vr0min = vr1max;
8269 else if (*vr0type == VR_ANTI_RANGE
8270 && vr1type == VR_RANGE)
8272 *vr0type = VR_RANGE;
8273 if (TREE_CODE (*vr0min) == INTEGER_CST)
8274 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8275 build_int_cst (TREE_TYPE (*vr0min), 1));
8276 else
8277 *vr0max = *vr0min;
8278 *vr0min = vr1min;
8280 else
8281 gcc_unreachable ();
8284 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8285 result for the intersection. That's always a conservative
8286 correct estimate. */
8288 return;
8292 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8293 in *VR0. This may not be the smallest possible such range. */
8295 static void
8296 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8298 value_range_t saved;
8300 /* If either range is VR_VARYING the other one wins. */
8301 if (vr1->type == VR_VARYING)
8302 return;
8303 if (vr0->type == VR_VARYING)
8305 copy_value_range (vr0, vr1);
8306 return;
8309 /* When either range is VR_UNDEFINED the resulting range is
8310 VR_UNDEFINED, too. */
8311 if (vr0->type == VR_UNDEFINED)
8312 return;
8313 if (vr1->type == VR_UNDEFINED)
8315 set_value_range_to_undefined (vr0);
8316 return;
8319 /* Save the original vr0 so we can return it as conservative intersection
8320 result when our worker turns things to varying. */
8321 saved = *vr0;
8322 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8323 vr1->type, vr1->min, vr1->max);
8324 /* Make sure to canonicalize the result though as the inversion of a
8325 VR_RANGE can still be a VR_RANGE. */
8326 set_and_canonicalize_value_range (vr0, vr0->type,
8327 vr0->min, vr0->max, vr0->equiv);
8328 /* If that failed, use the saved original VR0. */
8329 if (vr0->type == VR_VARYING)
8331 *vr0 = saved;
8332 return;
8334 /* If the result is VR_UNDEFINED there is no need to mess with
8335 the equivalencies. */
8336 if (vr0->type == VR_UNDEFINED)
8337 return;
8339 /* The resulting set of equivalences for range intersection is the union of
8340 the two sets. */
8341 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8342 bitmap_ior_into (vr0->equiv, vr1->equiv);
8343 else if (vr1->equiv && !vr0->equiv)
8344 bitmap_copy (vr0->equiv, vr1->equiv);
8347 static void
8348 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8350 if (dump_file && (dump_flags & TDF_DETAILS))
8352 fprintf (dump_file, "Intersecting\n ");
8353 dump_value_range (dump_file, vr0);
8354 fprintf (dump_file, "\nand\n ");
8355 dump_value_range (dump_file, vr1);
8356 fprintf (dump_file, "\n");
8358 vrp_intersect_ranges_1 (vr0, vr1);
8359 if (dump_file && (dump_flags & TDF_DETAILS))
8361 fprintf (dump_file, "to\n ");
8362 dump_value_range (dump_file, vr0);
8363 fprintf (dump_file, "\n");
8367 /* Meet operation for value ranges. Given two value ranges VR0 and
8368 VR1, store in VR0 a range that contains both VR0 and VR1. This
8369 may not be the smallest possible such range. */
8371 static void
8372 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8374 value_range_t saved;
8376 if (vr0->type == VR_UNDEFINED)
8378 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8379 return;
8382 if (vr1->type == VR_UNDEFINED)
8384 /* VR0 already has the resulting range. */
8385 return;
8388 if (vr0->type == VR_VARYING)
8390 /* Nothing to do. VR0 already has the resulting range. */
8391 return;
8394 if (vr1->type == VR_VARYING)
8396 set_value_range_to_varying (vr0);
8397 return;
8400 saved = *vr0;
8401 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8402 vr1->type, vr1->min, vr1->max);
8403 if (vr0->type == VR_VARYING)
8405 /* Failed to find an efficient meet. Before giving up and setting
8406 the result to VARYING, see if we can at least derive a useful
8407 anti-range. FIXME, all this nonsense about distinguishing
8408 anti-ranges from ranges is necessary because of the odd
8409 semantics of range_includes_zero_p and friends. */
8410 if (((saved.type == VR_RANGE
8411 && range_includes_zero_p (saved.min, saved.max) == 0)
8412 || (saved.type == VR_ANTI_RANGE
8413 && range_includes_zero_p (saved.min, saved.max) == 1))
8414 && ((vr1->type == VR_RANGE
8415 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8416 || (vr1->type == VR_ANTI_RANGE
8417 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8419 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8421 /* Since this meet operation did not result from the meeting of
8422 two equivalent names, VR0 cannot have any equivalences. */
8423 if (vr0->equiv)
8424 bitmap_clear (vr0->equiv);
8425 return;
8428 set_value_range_to_varying (vr0);
8429 return;
8431 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8432 vr0->equiv);
8433 if (vr0->type == VR_VARYING)
8434 return;
8436 /* The resulting set of equivalences is always the intersection of
8437 the two sets. */
8438 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8439 bitmap_and_into (vr0->equiv, vr1->equiv);
8440 else if (vr0->equiv && !vr1->equiv)
8441 bitmap_clear (vr0->equiv);
8444 static void
8445 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8447 if (dump_file && (dump_flags & TDF_DETAILS))
8449 fprintf (dump_file, "Meeting\n ");
8450 dump_value_range (dump_file, vr0);
8451 fprintf (dump_file, "\nand\n ");
8452 dump_value_range (dump_file, vr1);
8453 fprintf (dump_file, "\n");
8455 vrp_meet_1 (vr0, vr1);
8456 if (dump_file && (dump_flags & TDF_DETAILS))
8458 fprintf (dump_file, "to\n ");
8459 dump_value_range (dump_file, vr0);
8460 fprintf (dump_file, "\n");
8465 /* Visit all arguments for PHI node PHI that flow through executable
8466 edges. If a valid value range can be derived from all the incoming
8467 value ranges, set a new range for the LHS of PHI. */
8469 static enum ssa_prop_result
8470 vrp_visit_phi_node (gimple phi)
8472 size_t i;
8473 tree lhs = PHI_RESULT (phi);
8474 value_range_t *lhs_vr = get_value_range (lhs);
8475 value_range_t vr_result = VR_INITIALIZER;
8476 bool first = true;
8477 int edges, old_edges;
8478 struct loop *l;
8480 if (dump_file && (dump_flags & TDF_DETAILS))
8482 fprintf (dump_file, "\nVisiting PHI node: ");
8483 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8486 edges = 0;
8487 for (i = 0; i < gimple_phi_num_args (phi); i++)
8489 edge e = gimple_phi_arg_edge (phi, i);
8491 if (dump_file && (dump_flags & TDF_DETAILS))
8493 fprintf (dump_file,
8494 " Argument #%d (%d -> %d %sexecutable)\n",
8495 (int) i, e->src->index, e->dest->index,
8496 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8499 if (e->flags & EDGE_EXECUTABLE)
8501 tree arg = PHI_ARG_DEF (phi, i);
8502 value_range_t vr_arg;
8504 ++edges;
8506 if (TREE_CODE (arg) == SSA_NAME)
8508 vr_arg = *(get_value_range (arg));
8509 /* Do not allow equivalences or symbolic ranges to leak in from
8510 backedges. That creates invalid equivalencies.
8511 See PR53465 and PR54767. */
8512 if (e->flags & EDGE_DFS_BACK)
8514 if (vr_arg.type == VR_RANGE
8515 || vr_arg.type == VR_ANTI_RANGE)
8517 vr_arg.equiv = NULL;
8518 if (symbolic_range_p (&vr_arg))
8520 vr_arg.type = VR_VARYING;
8521 vr_arg.min = NULL_TREE;
8522 vr_arg.max = NULL_TREE;
8526 else
8528 /* If the non-backedge arguments range is VR_VARYING then
8529 we can still try recording a simple equivalence. */
8530 if (vr_arg.type == VR_VARYING)
8532 vr_arg.type = VR_RANGE;
8533 vr_arg.min = arg;
8534 vr_arg.max = arg;
8535 vr_arg.equiv = NULL;
8539 else
8541 if (TREE_OVERFLOW_P (arg))
8542 arg = drop_tree_overflow (arg);
8544 vr_arg.type = VR_RANGE;
8545 vr_arg.min = arg;
8546 vr_arg.max = arg;
8547 vr_arg.equiv = NULL;
8550 if (dump_file && (dump_flags & TDF_DETAILS))
8552 fprintf (dump_file, "\t");
8553 print_generic_expr (dump_file, arg, dump_flags);
8554 fprintf (dump_file, ": ");
8555 dump_value_range (dump_file, &vr_arg);
8556 fprintf (dump_file, "\n");
8559 if (first)
8560 copy_value_range (&vr_result, &vr_arg);
8561 else
8562 vrp_meet (&vr_result, &vr_arg);
8563 first = false;
8565 if (vr_result.type == VR_VARYING)
8566 break;
8570 if (vr_result.type == VR_VARYING)
8571 goto varying;
8572 else if (vr_result.type == VR_UNDEFINED)
8573 goto update_range;
8575 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8576 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8578 /* To prevent infinite iterations in the algorithm, derive ranges
8579 when the new value is slightly bigger or smaller than the
8580 previous one. We don't do this if we have seen a new executable
8581 edge; this helps us avoid an overflow infinity for conditionals
8582 which are not in a loop. If the old value-range was VR_UNDEFINED
8583 use the updated range and iterate one more time. */
8584 if (edges > 0
8585 && gimple_phi_num_args (phi) > 1
8586 && edges == old_edges
8587 && lhs_vr->type != VR_UNDEFINED)
8589 /* Compare old and new ranges, fall back to varying if the
8590 values are not comparable. */
8591 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8592 if (cmp_min == -2)
8593 goto varying;
8594 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8595 if (cmp_max == -2)
8596 goto varying;
8598 /* For non VR_RANGE or for pointers fall back to varying if
8599 the range changed. */
8600 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8601 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8602 && (cmp_min != 0 || cmp_max != 0))
8603 goto varying;
8605 /* If the new minimum is larger than than the previous one
8606 retain the old value. If the new minimum value is smaller
8607 than the previous one and not -INF go all the way to -INF + 1.
8608 In the first case, to avoid infinite bouncing between different
8609 minimums, and in the other case to avoid iterating millions of
8610 times to reach -INF. Going to -INF + 1 also lets the following
8611 iteration compute whether there will be any overflow, at the
8612 expense of one additional iteration. */
8613 if (cmp_min < 0)
8614 vr_result.min = lhs_vr->min;
8615 else if (cmp_min > 0
8616 && !vrp_val_is_min (vr_result.min))
8617 vr_result.min
8618 = int_const_binop (PLUS_EXPR,
8619 vrp_val_min (TREE_TYPE (vr_result.min)),
8620 build_int_cst (TREE_TYPE (vr_result.min), 1));
8622 /* Similarly for the maximum value. */
8623 if (cmp_max > 0)
8624 vr_result.max = lhs_vr->max;
8625 else if (cmp_max < 0
8626 && !vrp_val_is_max (vr_result.max))
8627 vr_result.max
8628 = int_const_binop (MINUS_EXPR,
8629 vrp_val_max (TREE_TYPE (vr_result.min)),
8630 build_int_cst (TREE_TYPE (vr_result.min), 1));
8632 /* If we dropped either bound to +-INF then if this is a loop
8633 PHI node SCEV may known more about its value-range. */
8634 if ((cmp_min > 0 || cmp_min < 0
8635 || cmp_max < 0 || cmp_max > 0)
8636 && (l = loop_containing_stmt (phi))
8637 && l->header == gimple_bb (phi))
8638 adjust_range_with_scev (&vr_result, l, phi, lhs);
8640 /* If we will end up with a (-INF, +INF) range, set it to
8641 VARYING. Same if the previous max value was invalid for
8642 the type and we end up with vr_result.min > vr_result.max. */
8643 if ((vrp_val_is_max (vr_result.max)
8644 && vrp_val_is_min (vr_result.min))
8645 || compare_values (vr_result.min,
8646 vr_result.max) > 0)
8647 goto varying;
8650 /* If the new range is different than the previous value, keep
8651 iterating. */
8652 update_range:
8653 if (update_value_range (lhs, &vr_result))
8655 if (dump_file && (dump_flags & TDF_DETAILS))
8657 fprintf (dump_file, "Found new range for ");
8658 print_generic_expr (dump_file, lhs, 0);
8659 fprintf (dump_file, ": ");
8660 dump_value_range (dump_file, &vr_result);
8661 fprintf (dump_file, "\n");
8664 return SSA_PROP_INTERESTING;
8667 /* Nothing changed, don't add outgoing edges. */
8668 return SSA_PROP_NOT_INTERESTING;
8670 /* No match found. Set the LHS to VARYING. */
8671 varying:
8672 set_value_range_to_varying (lhs_vr);
8673 return SSA_PROP_VARYING;
8676 /* Simplify boolean operations if the source is known
8677 to be already a boolean. */
8678 static bool
8679 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8681 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8682 tree lhs, op0, op1;
8683 bool need_conversion;
8685 /* We handle only !=/== case here. */
8686 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8688 op0 = gimple_assign_rhs1 (stmt);
8689 if (!op_with_boolean_value_range_p (op0))
8690 return false;
8692 op1 = gimple_assign_rhs2 (stmt);
8693 if (!op_with_boolean_value_range_p (op1))
8694 return false;
8696 /* Reduce number of cases to handle to NE_EXPR. As there is no
8697 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8698 if (rhs_code == EQ_EXPR)
8700 if (TREE_CODE (op1) == INTEGER_CST)
8701 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8702 build_int_cst (TREE_TYPE (op1), 1));
8703 else
8704 return false;
8707 lhs = gimple_assign_lhs (stmt);
8708 need_conversion
8709 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8711 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8712 if (need_conversion
8713 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8714 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8715 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8716 return false;
8718 /* For A != 0 we can substitute A itself. */
8719 if (integer_zerop (op1))
8720 gimple_assign_set_rhs_with_ops (gsi,
8721 need_conversion
8722 ? NOP_EXPR : TREE_CODE (op0),
8723 op0, NULL_TREE);
8724 /* For A != B we substitute A ^ B. Either with conversion. */
8725 else if (need_conversion)
8727 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8728 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8729 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8730 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8732 /* Or without. */
8733 else
8734 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8735 update_stmt (gsi_stmt (*gsi));
8737 return true;
8740 /* Simplify a division or modulo operator to a right shift or
8741 bitwise and if the first operand is unsigned or is greater
8742 than zero and the second operand is an exact power of two. */
8744 static bool
8745 simplify_div_or_mod_using_ranges (gimple stmt)
8747 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8748 tree val = NULL;
8749 tree op0 = gimple_assign_rhs1 (stmt);
8750 tree op1 = gimple_assign_rhs2 (stmt);
8751 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8753 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8755 val = integer_one_node;
8757 else
8759 bool sop = false;
8761 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8763 if (val
8764 && sop
8765 && integer_onep (val)
8766 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8768 location_t location;
8770 if (!gimple_has_location (stmt))
8771 location = input_location;
8772 else
8773 location = gimple_location (stmt);
8774 warning_at (location, OPT_Wstrict_overflow,
8775 "assuming signed overflow does not occur when "
8776 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8780 if (val && integer_onep (val))
8782 tree t;
8784 if (rhs_code == TRUNC_DIV_EXPR)
8786 t = build_int_cst (integer_type_node, tree_log2 (op1));
8787 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8788 gimple_assign_set_rhs1 (stmt, op0);
8789 gimple_assign_set_rhs2 (stmt, t);
8791 else
8793 t = build_int_cst (TREE_TYPE (op1), 1);
8794 t = int_const_binop (MINUS_EXPR, op1, t);
8795 t = fold_convert (TREE_TYPE (op0), t);
8797 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8798 gimple_assign_set_rhs1 (stmt, op0);
8799 gimple_assign_set_rhs2 (stmt, t);
8802 update_stmt (stmt);
8803 return true;
8806 return false;
8809 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8810 ABS_EXPR. If the operand is <= 0, then simplify the
8811 ABS_EXPR into a NEGATE_EXPR. */
8813 static bool
8814 simplify_abs_using_ranges (gimple stmt)
8816 tree val = NULL;
8817 tree op = gimple_assign_rhs1 (stmt);
8818 tree type = TREE_TYPE (op);
8819 value_range_t *vr = get_value_range (op);
8821 if (TYPE_UNSIGNED (type))
8823 val = integer_zero_node;
8825 else if (vr)
8827 bool sop = false;
8829 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8830 if (!val)
8832 sop = false;
8833 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8834 &sop);
8836 if (val)
8838 if (integer_zerop (val))
8839 val = integer_one_node;
8840 else if (integer_onep (val))
8841 val = integer_zero_node;
8845 if (val
8846 && (integer_onep (val) || integer_zerop (val)))
8848 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8850 location_t location;
8852 if (!gimple_has_location (stmt))
8853 location = input_location;
8854 else
8855 location = gimple_location (stmt);
8856 warning_at (location, OPT_Wstrict_overflow,
8857 "assuming signed overflow does not occur when "
8858 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8861 gimple_assign_set_rhs1 (stmt, op);
8862 if (integer_onep (val))
8863 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8864 else
8865 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8866 update_stmt (stmt);
8867 return true;
8871 return false;
8874 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8875 If all the bits that are being cleared by & are already
8876 known to be zero from VR, or all the bits that are being
8877 set by | are already known to be one from VR, the bit
8878 operation is redundant. */
8880 static bool
8881 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8883 tree op0 = gimple_assign_rhs1 (stmt);
8884 tree op1 = gimple_assign_rhs2 (stmt);
8885 tree op = NULL_TREE;
8886 value_range_t vr0 = VR_INITIALIZER;
8887 value_range_t vr1 = VR_INITIALIZER;
8888 wide_int may_be_nonzero0, may_be_nonzero1;
8889 wide_int must_be_nonzero0, must_be_nonzero1;
8890 wide_int mask;
8892 if (TREE_CODE (op0) == SSA_NAME)
8893 vr0 = *(get_value_range (op0));
8894 else if (is_gimple_min_invariant (op0))
8895 set_value_range_to_value (&vr0, op0, NULL);
8896 else
8897 return false;
8899 if (TREE_CODE (op1) == SSA_NAME)
8900 vr1 = *(get_value_range (op1));
8901 else if (is_gimple_min_invariant (op1))
8902 set_value_range_to_value (&vr1, op1, NULL);
8903 else
8904 return false;
8906 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
8907 &must_be_nonzero0))
8908 return false;
8909 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
8910 &must_be_nonzero1))
8911 return false;
8913 switch (gimple_assign_rhs_code (stmt))
8915 case BIT_AND_EXPR:
8916 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8917 if (mask == 0)
8919 op = op0;
8920 break;
8922 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8923 if (mask == 0)
8925 op = op1;
8926 break;
8928 break;
8929 case BIT_IOR_EXPR:
8930 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8931 if (mask == 0)
8933 op = op1;
8934 break;
8936 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8937 if (mask == 0)
8939 op = op0;
8940 break;
8942 break;
8943 default:
8944 gcc_unreachable ();
8947 if (op == NULL_TREE)
8948 return false;
8950 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8951 update_stmt (gsi_stmt (*gsi));
8952 return true;
8955 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8956 a known value range VR.
8958 If there is one and only one value which will satisfy the
8959 conditional, then return that value. Else return NULL. */
8961 static tree
8962 test_for_singularity (enum tree_code cond_code, tree op0,
8963 tree op1, value_range_t *vr)
8965 tree min = NULL;
8966 tree max = NULL;
8968 /* Extract minimum/maximum values which satisfy the
8969 the conditional as it was written. */
8970 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8972 /* This should not be negative infinity; there is no overflow
8973 here. */
8974 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8976 max = op1;
8977 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8979 tree one = build_int_cst (TREE_TYPE (op0), 1);
8980 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8981 if (EXPR_P (max))
8982 TREE_NO_WARNING (max) = 1;
8985 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8987 /* This should not be positive infinity; there is no overflow
8988 here. */
8989 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8991 min = op1;
8992 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8994 tree one = build_int_cst (TREE_TYPE (op0), 1);
8995 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8996 if (EXPR_P (min))
8997 TREE_NO_WARNING (min) = 1;
9001 /* Now refine the minimum and maximum values using any
9002 value range information we have for op0. */
9003 if (min && max)
9005 if (compare_values (vr->min, min) == 1)
9006 min = vr->min;
9007 if (compare_values (vr->max, max) == -1)
9008 max = vr->max;
9010 /* If the new min/max values have converged to a single value,
9011 then there is only one value which can satisfy the condition,
9012 return that value. */
9013 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9014 return min;
9016 return NULL;
9019 /* Return whether the value range *VR fits in an integer type specified
9020 by PRECISION and UNSIGNED_P. */
9022 static bool
9023 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9025 tree src_type;
9026 unsigned src_precision;
9027 widest_int tem;
9028 signop src_sgn;
9030 /* We can only handle integral and pointer types. */
9031 src_type = TREE_TYPE (vr->min);
9032 if (!INTEGRAL_TYPE_P (src_type)
9033 && !POINTER_TYPE_P (src_type))
9034 return false;
9036 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9037 and so is an identity transform. */
9038 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9039 src_sgn = TYPE_SIGN (src_type);
9040 if ((src_precision < dest_precision
9041 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9042 || (src_precision == dest_precision && src_sgn == dest_sgn))
9043 return true;
9045 /* Now we can only handle ranges with constant bounds. */
9046 if (vr->type != VR_RANGE
9047 || TREE_CODE (vr->min) != INTEGER_CST
9048 || TREE_CODE (vr->max) != INTEGER_CST)
9049 return false;
9051 /* For sign changes, the MSB of the wide_int has to be clear.
9052 An unsigned value with its MSB set cannot be represented by
9053 a signed wide_int, while a negative value cannot be represented
9054 by an unsigned wide_int. */
9055 if (src_sgn != dest_sgn
9056 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9057 return false;
9059 /* Then we can perform the conversion on both ends and compare
9060 the result for equality. */
9061 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9062 if (tem != wi::to_widest (vr->min))
9063 return false;
9064 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9065 if (tem != wi::to_widest (vr->max))
9066 return false;
9068 return true;
9071 /* Simplify a conditional using a relational operator to an equality
9072 test if the range information indicates only one value can satisfy
9073 the original conditional. */
9075 static bool
9076 simplify_cond_using_ranges (gimple stmt)
9078 tree op0 = gimple_cond_lhs (stmt);
9079 tree op1 = gimple_cond_rhs (stmt);
9080 enum tree_code cond_code = gimple_cond_code (stmt);
9082 if (cond_code != NE_EXPR
9083 && cond_code != EQ_EXPR
9084 && TREE_CODE (op0) == SSA_NAME
9085 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9086 && is_gimple_min_invariant (op1))
9088 value_range_t *vr = get_value_range (op0);
9090 /* If we have range information for OP0, then we might be
9091 able to simplify this conditional. */
9092 if (vr->type == VR_RANGE)
9094 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
9096 if (new_tree)
9098 if (dump_file)
9100 fprintf (dump_file, "Simplified relational ");
9101 print_gimple_stmt (dump_file, stmt, 0, 0);
9102 fprintf (dump_file, " into ");
9105 gimple_cond_set_code (stmt, EQ_EXPR);
9106 gimple_cond_set_lhs (stmt, op0);
9107 gimple_cond_set_rhs (stmt, new_tree);
9109 update_stmt (stmt);
9111 if (dump_file)
9113 print_gimple_stmt (dump_file, stmt, 0, 0);
9114 fprintf (dump_file, "\n");
9117 return true;
9120 /* Try again after inverting the condition. We only deal
9121 with integral types here, so no need to worry about
9122 issues with inverting FP comparisons. */
9123 cond_code = invert_tree_comparison (cond_code, false);
9124 new_tree = test_for_singularity (cond_code, op0, op1, vr);
9126 if (new_tree)
9128 if (dump_file)
9130 fprintf (dump_file, "Simplified relational ");
9131 print_gimple_stmt (dump_file, stmt, 0, 0);
9132 fprintf (dump_file, " into ");
9135 gimple_cond_set_code (stmt, NE_EXPR);
9136 gimple_cond_set_lhs (stmt, op0);
9137 gimple_cond_set_rhs (stmt, new_tree);
9139 update_stmt (stmt);
9141 if (dump_file)
9143 print_gimple_stmt (dump_file, stmt, 0, 0);
9144 fprintf (dump_file, "\n");
9147 return true;
9152 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9153 see if OP0 was set by a type conversion where the source of
9154 the conversion is another SSA_NAME with a range that fits
9155 into the range of OP0's type.
9157 If so, the conversion is redundant as the earlier SSA_NAME can be
9158 used for the comparison directly if we just massage the constant in the
9159 comparison. */
9160 if (TREE_CODE (op0) == SSA_NAME
9161 && TREE_CODE (op1) == INTEGER_CST)
9163 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9164 tree innerop;
9166 if (!is_gimple_assign (def_stmt)
9167 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9168 return false;
9170 innerop = gimple_assign_rhs1 (def_stmt);
9172 if (TREE_CODE (innerop) == SSA_NAME
9173 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9175 value_range_t *vr = get_value_range (innerop);
9177 if (range_int_cst_p (vr)
9178 && range_fits_type_p (vr,
9179 TYPE_PRECISION (TREE_TYPE (op0)),
9180 TYPE_SIGN (TREE_TYPE (op0)))
9181 && int_fits_type_p (op1, TREE_TYPE (innerop))
9182 /* The range must not have overflowed, or if it did overflow
9183 we must not be wrapping/trapping overflow and optimizing
9184 with strict overflow semantics. */
9185 && ((!is_negative_overflow_infinity (vr->min)
9186 && !is_positive_overflow_infinity (vr->max))
9187 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9189 /* If the range overflowed and the user has asked for warnings
9190 when strict overflow semantics were used to optimize code,
9191 issue an appropriate warning. */
9192 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9193 && (is_negative_overflow_infinity (vr->min)
9194 || is_positive_overflow_infinity (vr->max))
9195 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9197 location_t location;
9199 if (!gimple_has_location (stmt))
9200 location = input_location;
9201 else
9202 location = gimple_location (stmt);
9203 warning_at (location, OPT_Wstrict_overflow,
9204 "assuming signed overflow does not occur when "
9205 "simplifying conditional");
9208 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9209 gimple_cond_set_lhs (stmt, innerop);
9210 gimple_cond_set_rhs (stmt, newconst);
9211 return true;
9216 return false;
9219 /* Simplify a switch statement using the value range of the switch
9220 argument. */
9222 static bool
9223 simplify_switch_using_ranges (gimple stmt)
9225 tree op = gimple_switch_index (stmt);
9226 value_range_t *vr;
9227 bool take_default;
9228 edge e;
9229 edge_iterator ei;
9230 size_t i = 0, j = 0, n, n2;
9231 tree vec2;
9232 switch_update su;
9233 size_t k = 1, l = 0;
9235 if (TREE_CODE (op) == SSA_NAME)
9237 vr = get_value_range (op);
9239 /* We can only handle integer ranges. */
9240 if ((vr->type != VR_RANGE
9241 && vr->type != VR_ANTI_RANGE)
9242 || symbolic_range_p (vr))
9243 return false;
9245 /* Find case label for min/max of the value range. */
9246 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9248 else if (TREE_CODE (op) == INTEGER_CST)
9250 take_default = !find_case_label_index (stmt, 1, op, &i);
9251 if (take_default)
9253 i = 1;
9254 j = 0;
9256 else
9258 j = i;
9261 else
9262 return false;
9264 n = gimple_switch_num_labels (stmt);
9266 /* Bail out if this is just all edges taken. */
9267 if (i == 1
9268 && j == n - 1
9269 && take_default)
9270 return false;
9272 /* Build a new vector of taken case labels. */
9273 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9274 n2 = 0;
9276 /* Add the default edge, if necessary. */
9277 if (take_default)
9278 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9280 for (; i <= j; ++i, ++n2)
9281 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9283 for (; k <= l; ++k, ++n2)
9284 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9286 /* Mark needed edges. */
9287 for (i = 0; i < n2; ++i)
9289 e = find_edge (gimple_bb (stmt),
9290 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9291 e->aux = (void *)-1;
9294 /* Queue not needed edges for later removal. */
9295 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9297 if (e->aux == (void *)-1)
9299 e->aux = NULL;
9300 continue;
9303 if (dump_file && (dump_flags & TDF_DETAILS))
9305 fprintf (dump_file, "removing unreachable case label\n");
9307 to_remove_edges.safe_push (e);
9308 e->flags &= ~EDGE_EXECUTABLE;
9311 /* And queue an update for the stmt. */
9312 su.stmt = stmt;
9313 su.vec = vec2;
9314 to_update_switch_stmts.safe_push (su);
9315 return false;
9318 /* Simplify an integral conversion from an SSA name in STMT. */
9320 static bool
9321 simplify_conversion_using_ranges (gimple stmt)
9323 tree innerop, middleop, finaltype;
9324 gimple def_stmt;
9325 value_range_t *innervr;
9326 signop inner_sgn, middle_sgn, final_sgn;
9327 unsigned inner_prec, middle_prec, final_prec;
9328 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9330 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9331 if (!INTEGRAL_TYPE_P (finaltype))
9332 return false;
9333 middleop = gimple_assign_rhs1 (stmt);
9334 def_stmt = SSA_NAME_DEF_STMT (middleop);
9335 if (!is_gimple_assign (def_stmt)
9336 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9337 return false;
9338 innerop = gimple_assign_rhs1 (def_stmt);
9339 if (TREE_CODE (innerop) != SSA_NAME
9340 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9341 return false;
9343 /* Get the value-range of the inner operand. */
9344 innervr = get_value_range (innerop);
9345 if (innervr->type != VR_RANGE
9346 || TREE_CODE (innervr->min) != INTEGER_CST
9347 || TREE_CODE (innervr->max) != INTEGER_CST)
9348 return false;
9350 /* Simulate the conversion chain to check if the result is equal if
9351 the middle conversion is removed. */
9352 innermin = wi::to_widest (innervr->min);
9353 innermax = wi::to_widest (innervr->max);
9355 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9356 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9357 final_prec = TYPE_PRECISION (finaltype);
9359 /* If the first conversion is not injective, the second must not
9360 be widening. */
9361 if (wi::gtu_p (innermax - innermin,
9362 wi::mask <widest_int> (middle_prec, false))
9363 && middle_prec < final_prec)
9364 return false;
9365 /* We also want a medium value so that we can track the effect that
9366 narrowing conversions with sign change have. */
9367 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9368 if (inner_sgn == UNSIGNED)
9369 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9370 else
9371 innermed = 0;
9372 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9373 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9374 innermed = innermin;
9376 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9377 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9378 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9379 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9381 /* Require that the final conversion applied to both the original
9382 and the intermediate range produces the same result. */
9383 final_sgn = TYPE_SIGN (finaltype);
9384 if (wi::ext (middlemin, final_prec, final_sgn)
9385 != wi::ext (innermin, final_prec, final_sgn)
9386 || wi::ext (middlemed, final_prec, final_sgn)
9387 != wi::ext (innermed, final_prec, final_sgn)
9388 || wi::ext (middlemax, final_prec, final_sgn)
9389 != wi::ext (innermax, final_prec, final_sgn))
9390 return false;
9392 gimple_assign_set_rhs1 (stmt, innerop);
9393 update_stmt (stmt);
9394 return true;
9397 /* Simplify a conversion from integral SSA name to float in STMT. */
9399 static bool
9400 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9402 tree rhs1 = gimple_assign_rhs1 (stmt);
9403 value_range_t *vr = get_value_range (rhs1);
9404 enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9405 enum machine_mode mode;
9406 tree tem;
9407 gimple conv;
9409 /* We can only handle constant ranges. */
9410 if (vr->type != VR_RANGE
9411 || TREE_CODE (vr->min) != INTEGER_CST
9412 || TREE_CODE (vr->max) != INTEGER_CST)
9413 return false;
9415 /* First check if we can use a signed type in place of an unsigned. */
9416 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9417 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9418 != CODE_FOR_nothing)
9419 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9420 mode = TYPE_MODE (TREE_TYPE (rhs1));
9421 /* If we can do the conversion in the current input mode do nothing. */
9422 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9423 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9424 return false;
9425 /* Otherwise search for a mode we can use, starting from the narrowest
9426 integer mode available. */
9427 else
9429 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9432 /* If we cannot do a signed conversion to float from mode
9433 or if the value-range does not fit in the signed type
9434 try with a wider mode. */
9435 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9436 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9437 break;
9439 mode = GET_MODE_WIDER_MODE (mode);
9440 /* But do not widen the input. Instead leave that to the
9441 optabs expansion code. */
9442 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9443 return false;
9445 while (mode != VOIDmode);
9446 if (mode == VOIDmode)
9447 return false;
9450 /* It works, insert a truncation or sign-change before the
9451 float conversion. */
9452 tem = make_ssa_name (build_nonstandard_integer_type
9453 (GET_MODE_PRECISION (mode), 0), NULL);
9454 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
9455 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9456 gimple_assign_set_rhs1 (stmt, tem);
9457 update_stmt (stmt);
9459 return true;
9462 /* Simplify an internal fn call using ranges if possible. */
9464 static bool
9465 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9467 enum tree_code subcode;
9468 switch (gimple_call_internal_fn (stmt))
9470 case IFN_UBSAN_CHECK_ADD:
9471 subcode = PLUS_EXPR;
9472 break;
9473 case IFN_UBSAN_CHECK_SUB:
9474 subcode = MINUS_EXPR;
9475 break;
9476 case IFN_UBSAN_CHECK_MUL:
9477 subcode = MULT_EXPR;
9478 break;
9479 default:
9480 return false;
9483 value_range_t vr0 = VR_INITIALIZER;
9484 value_range_t vr1 = VR_INITIALIZER;
9485 tree op0 = gimple_call_arg (stmt, 0);
9486 tree op1 = gimple_call_arg (stmt, 1);
9488 if (TREE_CODE (op0) == SSA_NAME)
9489 vr0 = *get_value_range (op0);
9490 else if (TREE_CODE (op0) == INTEGER_CST)
9491 set_value_range_to_value (&vr0, op0, NULL);
9492 else
9493 set_value_range_to_varying (&vr0);
9495 if (TREE_CODE (op1) == SSA_NAME)
9496 vr1 = *get_value_range (op1);
9497 else if (TREE_CODE (op1) == INTEGER_CST)
9498 set_value_range_to_value (&vr1, op1, NULL);
9499 else
9500 set_value_range_to_varying (&vr1);
9502 if (!range_int_cst_p (&vr0))
9504 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9505 optimize at least x = y + 0; x = y - 0; x = y * 0;
9506 and x = y * 1; which never overflow. */
9507 if (!range_int_cst_p (&vr1))
9508 return false;
9509 if (tree_int_cst_sgn (vr1.min) == -1)
9510 return false;
9511 if (compare_tree_int (vr1.max, subcode == MULT_EXPR) == 1)
9512 return false;
9514 else if (!range_int_cst_p (&vr1))
9516 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9517 optimize at least x = 0 + y; x = 0 * y; and x = 1 * y;
9518 which never overflow. */
9519 if (subcode == MINUS_EXPR)
9520 return false;
9521 if (!range_int_cst_p (&vr0))
9522 return false;
9523 if (tree_int_cst_sgn (vr0.min) == -1)
9524 return false;
9525 if (compare_tree_int (vr0.max, subcode == MULT_EXPR) == 1)
9526 return false;
9528 else
9530 tree r1 = int_const_binop (subcode, vr0.min, vr1.min);
9531 tree r2 = int_const_binop (subcode, vr0.max, vr1.max);
9532 if (r1 == NULL_TREE || TREE_OVERFLOW (r1)
9533 || r2 == NULL_TREE || TREE_OVERFLOW (r2))
9534 return false;
9535 if (subcode == MULT_EXPR)
9537 tree r3 = int_const_binop (subcode, vr0.min, vr1.max);
9538 tree r4 = int_const_binop (subcode, vr0.max, vr1.min);
9539 if (r3 == NULL_TREE || TREE_OVERFLOW (r3)
9540 || r4 == NULL_TREE || TREE_OVERFLOW (r4))
9541 return false;
9545 gimple g = gimple_build_assign_with_ops (subcode, gimple_call_lhs (stmt),
9546 op0, op1);
9547 gsi_replace (gsi, g, false);
9548 return true;
9551 /* Simplify STMT using ranges if possible. */
9553 static bool
9554 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9556 gimple stmt = gsi_stmt (*gsi);
9557 if (is_gimple_assign (stmt))
9559 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9560 tree rhs1 = gimple_assign_rhs1 (stmt);
9562 switch (rhs_code)
9564 case EQ_EXPR:
9565 case NE_EXPR:
9566 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9567 if the RHS is zero or one, and the LHS are known to be boolean
9568 values. */
9569 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9570 return simplify_truth_ops_using_ranges (gsi, stmt);
9571 break;
9573 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9574 and BIT_AND_EXPR respectively if the first operand is greater
9575 than zero and the second operand is an exact power of two. */
9576 case TRUNC_DIV_EXPR:
9577 case TRUNC_MOD_EXPR:
9578 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9579 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9580 return simplify_div_or_mod_using_ranges (stmt);
9581 break;
9583 /* Transform ABS (X) into X or -X as appropriate. */
9584 case ABS_EXPR:
9585 if (TREE_CODE (rhs1) == SSA_NAME
9586 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9587 return simplify_abs_using_ranges (stmt);
9588 break;
9590 case BIT_AND_EXPR:
9591 case BIT_IOR_EXPR:
9592 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9593 if all the bits being cleared are already cleared or
9594 all the bits being set are already set. */
9595 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9596 return simplify_bit_ops_using_ranges (gsi, stmt);
9597 break;
9599 CASE_CONVERT:
9600 if (TREE_CODE (rhs1) == SSA_NAME
9601 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9602 return simplify_conversion_using_ranges (stmt);
9603 break;
9605 case FLOAT_EXPR:
9606 if (TREE_CODE (rhs1) == SSA_NAME
9607 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9608 return simplify_float_conversion_using_ranges (gsi, stmt);
9609 break;
9611 default:
9612 break;
9615 else if (gimple_code (stmt) == GIMPLE_COND)
9616 return simplify_cond_using_ranges (stmt);
9617 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9618 return simplify_switch_using_ranges (stmt);
9619 else if (is_gimple_call (stmt)
9620 && gimple_call_internal_p (stmt))
9621 return simplify_internal_call_using_ranges (gsi, stmt);
9623 return false;
9626 /* If the statement pointed by SI has a predicate whose value can be
9627 computed using the value range information computed by VRP, compute
9628 its value and return true. Otherwise, return false. */
9630 static bool
9631 fold_predicate_in (gimple_stmt_iterator *si)
9633 bool assignment_p = false;
9634 tree val;
9635 gimple stmt = gsi_stmt (*si);
9637 if (is_gimple_assign (stmt)
9638 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9640 assignment_p = true;
9641 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9642 gimple_assign_rhs1 (stmt),
9643 gimple_assign_rhs2 (stmt),
9644 stmt);
9646 else if (gimple_code (stmt) == GIMPLE_COND)
9647 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
9648 gimple_cond_lhs (stmt),
9649 gimple_cond_rhs (stmt),
9650 stmt);
9651 else
9652 return false;
9654 if (val)
9656 if (assignment_p)
9657 val = fold_convert (gimple_expr_type (stmt), val);
9659 if (dump_file)
9661 fprintf (dump_file, "Folding predicate ");
9662 print_gimple_expr (dump_file, stmt, 0, 0);
9663 fprintf (dump_file, " to ");
9664 print_generic_expr (dump_file, val, 0);
9665 fprintf (dump_file, "\n");
9668 if (is_gimple_assign (stmt))
9669 gimple_assign_set_rhs_from_tree (si, val);
9670 else
9672 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9673 if (integer_zerop (val))
9674 gimple_cond_make_false (stmt);
9675 else if (integer_onep (val))
9676 gimple_cond_make_true (stmt);
9677 else
9678 gcc_unreachable ();
9681 return true;
9684 return false;
9687 /* Callback for substitute_and_fold folding the stmt at *SI. */
9689 static bool
9690 vrp_fold_stmt (gimple_stmt_iterator *si)
9692 if (fold_predicate_in (si))
9693 return true;
9695 return simplify_stmt_using_ranges (si);
9698 /* Stack of dest,src equivalency pairs that need to be restored after
9699 each attempt to thread a block's incoming edge to an outgoing edge.
9701 A NULL entry is used to mark the end of pairs which need to be
9702 restored. */
9703 static vec<tree> equiv_stack;
9705 /* A trivial wrapper so that we can present the generic jump threading
9706 code with a simple API for simplifying statements. STMT is the
9707 statement we want to simplify, WITHIN_STMT provides the location
9708 for any overflow warnings. */
9710 static tree
9711 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9713 if (gimple_code (stmt) == GIMPLE_COND)
9714 return vrp_evaluate_conditional (gimple_cond_code (stmt),
9715 gimple_cond_lhs (stmt),
9716 gimple_cond_rhs (stmt), within_stmt);
9718 if (gimple_code (stmt) == GIMPLE_ASSIGN)
9720 value_range_t new_vr = VR_INITIALIZER;
9721 tree lhs = gimple_assign_lhs (stmt);
9723 if (TREE_CODE (lhs) == SSA_NAME
9724 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
9725 || POINTER_TYPE_P (TREE_TYPE (lhs))))
9727 extract_range_from_assignment (&new_vr, stmt);
9728 if (range_int_cst_singleton_p (&new_vr))
9729 return new_vr.min;
9733 return NULL_TREE;
9736 /* Blocks which have more than one predecessor and more than
9737 one successor present jump threading opportunities, i.e.,
9738 when the block is reached from a specific predecessor, we
9739 may be able to determine which of the outgoing edges will
9740 be traversed. When this optimization applies, we are able
9741 to avoid conditionals at runtime and we may expose secondary
9742 optimization opportunities.
9744 This routine is effectively a driver for the generic jump
9745 threading code. It basically just presents the generic code
9746 with edges that may be suitable for jump threading.
9748 Unlike DOM, we do not iterate VRP if jump threading was successful.
9749 While iterating may expose new opportunities for VRP, it is expected
9750 those opportunities would be very limited and the compile time cost
9751 to expose those opportunities would be significant.
9753 As jump threading opportunities are discovered, they are registered
9754 for later realization. */
9756 static void
9757 identify_jump_threads (void)
9759 basic_block bb;
9760 gimple dummy;
9761 int i;
9762 edge e;
9764 /* Ugh. When substituting values earlier in this pass we can
9765 wipe the dominance information. So rebuild the dominator
9766 information as we need it within the jump threading code. */
9767 calculate_dominance_info (CDI_DOMINATORS);
9769 /* We do not allow VRP information to be used for jump threading
9770 across a back edge in the CFG. Otherwise it becomes too
9771 difficult to avoid eliminating loop exit tests. Of course
9772 EDGE_DFS_BACK is not accurate at this time so we have to
9773 recompute it. */
9774 mark_dfs_back_edges ();
9776 /* Do not thread across edges we are about to remove. Just marking
9777 them as EDGE_DFS_BACK will do. */
9778 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9779 e->flags |= EDGE_DFS_BACK;
9781 /* Allocate our unwinder stack to unwind any temporary equivalences
9782 that might be recorded. */
9783 equiv_stack.create (20);
9785 /* To avoid lots of silly node creation, we create a single
9786 conditional and just modify it in-place when attempting to
9787 thread jumps. */
9788 dummy = gimple_build_cond (EQ_EXPR,
9789 integer_zero_node, integer_zero_node,
9790 NULL, NULL);
9792 /* Walk through all the blocks finding those which present a
9793 potential jump threading opportunity. We could set this up
9794 as a dominator walker and record data during the walk, but
9795 I doubt it's worth the effort for the classes of jump
9796 threading opportunities we are trying to identify at this
9797 point in compilation. */
9798 FOR_EACH_BB_FN (bb, cfun)
9800 gimple last;
9802 /* If the generic jump threading code does not find this block
9803 interesting, then there is nothing to do. */
9804 if (! potentially_threadable_block (bb))
9805 continue;
9807 /* We only care about blocks ending in a COND_EXPR. While there
9808 may be some value in handling SWITCH_EXPR here, I doubt it's
9809 terribly important. */
9810 last = gsi_stmt (gsi_last_bb (bb));
9812 /* We're basically looking for a switch or any kind of conditional with
9813 integral or pointer type arguments. Note the type of the second
9814 argument will be the same as the first argument, so no need to
9815 check it explicitly. */
9816 if (gimple_code (last) == GIMPLE_SWITCH
9817 || (gimple_code (last) == GIMPLE_COND
9818 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9819 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9820 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9821 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9822 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9824 edge_iterator ei;
9826 /* We've got a block with multiple predecessors and multiple
9827 successors which also ends in a suitable conditional or
9828 switch statement. For each predecessor, see if we can thread
9829 it to a specific successor. */
9830 FOR_EACH_EDGE (e, ei, bb->preds)
9832 /* Do not thread across back edges or abnormal edges
9833 in the CFG. */
9834 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9835 continue;
9837 thread_across_edge (dummy, e, true, &equiv_stack,
9838 simplify_stmt_for_jump_threading);
9843 /* We do not actually update the CFG or SSA graphs at this point as
9844 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9845 handle ASSERT_EXPRs gracefully. */
9848 /* We identified all the jump threading opportunities earlier, but could
9849 not transform the CFG at that time. This routine transforms the
9850 CFG and arranges for the dominator tree to be rebuilt if necessary.
9852 Note the SSA graph update will occur during the normal TODO
9853 processing by the pass manager. */
9854 static void
9855 finalize_jump_threads (void)
9857 thread_through_all_blocks (false);
9858 equiv_stack.release ();
9862 /* Traverse all the blocks folding conditionals with known ranges. */
9864 static void
9865 vrp_finalize (void)
9867 size_t i;
9869 values_propagated = true;
9871 if (dump_file)
9873 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9874 dump_all_value_ranges (dump_file);
9875 fprintf (dump_file, "\n");
9878 substitute_and_fold (op_with_constant_singleton_value_range,
9879 vrp_fold_stmt, false);
9881 if (warn_array_bounds)
9882 check_all_array_refs ();
9884 /* We must identify jump threading opportunities before we release
9885 the datastructures built by VRP. */
9886 identify_jump_threads ();
9888 /* Set value range to non pointer SSA_NAMEs. */
9889 for (i = 0; i < num_vr_values; i++)
9890 if (vr_value[i])
9892 tree name = ssa_name (i);
9894 if (!name
9895 || POINTER_TYPE_P (TREE_TYPE (name))
9896 || (vr_value[i]->type == VR_VARYING)
9897 || (vr_value[i]->type == VR_UNDEFINED))
9898 continue;
9900 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
9901 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
9902 && (vr_value[i]->type == VR_RANGE
9903 || vr_value[i]->type == VR_ANTI_RANGE))
9904 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
9905 vr_value[i]->max);
9908 /* Free allocated memory. */
9909 for (i = 0; i < num_vr_values; i++)
9910 if (vr_value[i])
9912 BITMAP_FREE (vr_value[i]->equiv);
9913 free (vr_value[i]);
9916 free (vr_value);
9917 free (vr_phi_edge_counts);
9919 /* So that we can distinguish between VRP data being available
9920 and not available. */
9921 vr_value = NULL;
9922 vr_phi_edge_counts = NULL;
9926 /* Main entry point to VRP (Value Range Propagation). This pass is
9927 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9928 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9929 Programming Language Design and Implementation, pp. 67-78, 1995.
9930 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9932 This is essentially an SSA-CCP pass modified to deal with ranges
9933 instead of constants.
9935 While propagating ranges, we may find that two or more SSA name
9936 have equivalent, though distinct ranges. For instance,
9938 1 x_9 = p_3->a;
9939 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9940 3 if (p_4 == q_2)
9941 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9942 5 endif
9943 6 if (q_2)
9945 In the code above, pointer p_5 has range [q_2, q_2], but from the
9946 code we can also determine that p_5 cannot be NULL and, if q_2 had
9947 a non-varying range, p_5's range should also be compatible with it.
9949 These equivalences are created by two expressions: ASSERT_EXPR and
9950 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9951 result of another assertion, then we can use the fact that p_5 and
9952 p_4 are equivalent when evaluating p_5's range.
9954 Together with value ranges, we also propagate these equivalences
9955 between names so that we can take advantage of information from
9956 multiple ranges when doing final replacement. Note that this
9957 equivalency relation is transitive but not symmetric.
9959 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9960 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9961 in contexts where that assertion does not hold (e.g., in line 6).
9963 TODO, the main difference between this pass and Patterson's is that
9964 we do not propagate edge probabilities. We only compute whether
9965 edges can be taken or not. That is, instead of having a spectrum
9966 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9967 DON'T KNOW. In the future, it may be worthwhile to propagate
9968 probabilities to aid branch prediction. */
9970 static unsigned int
9971 execute_vrp (void)
9973 int i;
9974 edge e;
9975 switch_update *su;
9977 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9978 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9979 scev_initialize ();
9981 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9982 Inserting assertions may split edges which will invalidate
9983 EDGE_DFS_BACK. */
9984 insert_range_assertions ();
9986 to_remove_edges.create (10);
9987 to_update_switch_stmts.create (5);
9988 threadedge_initialize_values ();
9990 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9991 mark_dfs_back_edges ();
9993 vrp_initialize ();
9994 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
9995 vrp_finalize ();
9997 free_numbers_of_iterations_estimates ();
9999 /* ASSERT_EXPRs must be removed before finalizing jump threads
10000 as finalizing jump threads calls the CFG cleanup code which
10001 does not properly handle ASSERT_EXPRs. */
10002 remove_range_assertions ();
10004 /* If we exposed any new variables, go ahead and put them into
10005 SSA form now, before we handle jump threading. This simplifies
10006 interactions between rewriting of _DECL nodes into SSA form
10007 and rewriting SSA_NAME nodes into SSA form after block
10008 duplication and CFG manipulation. */
10009 update_ssa (TODO_update_ssa);
10011 finalize_jump_threads ();
10013 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10014 CFG in a broken state and requires a cfg_cleanup run. */
10015 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10016 remove_edge (e);
10017 /* Update SWITCH_EXPR case label vector. */
10018 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10020 size_t j;
10021 size_t n = TREE_VEC_LENGTH (su->vec);
10022 tree label;
10023 gimple_switch_set_num_labels (su->stmt, n);
10024 for (j = 0; j < n; j++)
10025 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10026 /* As we may have replaced the default label with a regular one
10027 make sure to make it a real default label again. This ensures
10028 optimal expansion. */
10029 label = gimple_switch_label (su->stmt, 0);
10030 CASE_LOW (label) = NULL_TREE;
10031 CASE_HIGH (label) = NULL_TREE;
10034 if (to_remove_edges.length () > 0)
10036 free_dominance_info (CDI_DOMINATORS);
10037 loops_state_set (LOOPS_NEED_FIXUP);
10040 to_remove_edges.release ();
10041 to_update_switch_stmts.release ();
10042 threadedge_finalize_values ();
10044 scev_finalize ();
10045 loop_optimizer_finalize ();
10046 return 0;
10049 namespace {
10051 const pass_data pass_data_vrp =
10053 GIMPLE_PASS, /* type */
10054 "vrp", /* name */
10055 OPTGROUP_NONE, /* optinfo_flags */
10056 TV_TREE_VRP, /* tv_id */
10057 PROP_ssa, /* properties_required */
10058 0, /* properties_provided */
10059 0, /* properties_destroyed */
10060 0, /* todo_flags_start */
10061 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10064 class pass_vrp : public gimple_opt_pass
10066 public:
10067 pass_vrp (gcc::context *ctxt)
10068 : gimple_opt_pass (pass_data_vrp, ctxt)
10071 /* opt_pass methods: */
10072 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10073 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10074 virtual unsigned int execute (function *) { return execute_vrp (); }
10076 }; // class pass_vrp
10078 } // anon namespace
10080 gimple_opt_pass *
10081 make_pass_vrp (gcc::context *ctxt)
10083 return new pass_vrp (ctxt);