2014-11-18 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc / tree-vrp.c
blob13b7c791eea5388a862d872e04235c1a301be1c7
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 "predict.h"
30 #include "vec.h"
31 #include "hashtab.h"
32 #include "hash-set.h"
33 #include "machmode.h"
34 #include "hard-reg-set.h"
35 #include "input.h"
36 #include "function.h"
37 #include "dominance.h"
38 #include "cfg.h"
39 #include "cfganal.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-fold.h"
44 #include "tree-eh.h"
45 #include "gimple-expr.h"
46 #include "is-a.h"
47 #include "gimple.h"
48 #include "gimple-iterator.h"
49 #include "gimple-walk.h"
50 #include "gimple-ssa.h"
51 #include "tree-cfg.h"
52 #include "tree-phinodes.h"
53 #include "ssa-iterators.h"
54 #include "stringpool.h"
55 #include "tree-ssanames.h"
56 #include "tree-ssa-loop-manip.h"
57 #include "tree-ssa-loop-niter.h"
58 #include "tree-ssa-loop.h"
59 #include "tree-into-ssa.h"
60 #include "tree-ssa.h"
61 #include "tree-pass.h"
62 #include "tree-dump.h"
63 #include "gimple-pretty-print.h"
64 #include "diagnostic-core.h"
65 #include "intl.h"
66 #include "cfgloop.h"
67 #include "tree-scalar-evolution.h"
68 #include "tree-ssa-propagate.h"
69 #include "tree-chrec.h"
70 #include "tree-ssa-threadupdate.h"
71 #include "expr.h"
72 #include "insn-codes.h"
73 #include "optabs.h"
74 #include "tree-ssa-threadedge.h"
75 #include "wide-int.h"
79 /* Range of values that can be associated with an SSA_NAME after VRP
80 has executed. */
81 struct value_range_d
83 /* Lattice value represented by this range. */
84 enum value_range_type type;
86 /* Minimum and maximum values represented by this range. These
87 values should be interpreted as follows:
89 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
90 be NULL.
92 - If TYPE == VR_RANGE then MIN holds the minimum value and
93 MAX holds the maximum value of the range [MIN, MAX].
95 - If TYPE == ANTI_RANGE the variable is known to NOT
96 take any values in the range [MIN, MAX]. */
97 tree min;
98 tree max;
100 /* Set of SSA names whose value ranges are equivalent to this one.
101 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
102 bitmap equiv;
105 typedef struct value_range_d value_range_t;
107 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
109 /* Set of SSA names found live during the RPO traversal of the function
110 for still active basic-blocks. */
111 static sbitmap *live;
113 /* Return true if the SSA name NAME is live on the edge E. */
115 static bool
116 live_on_edge (edge e, tree name)
118 return (live[e->dest->index]
119 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
122 /* Local functions. */
123 static int compare_values (tree val1, tree val2);
124 static int compare_values_warnv (tree val1, tree val2, bool *);
125 static void vrp_meet (value_range_t *, value_range_t *);
126 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
127 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
128 tree, tree, bool, bool *,
129 bool *);
131 /* Location information for ASSERT_EXPRs. Each instance of this
132 structure describes an ASSERT_EXPR for an SSA name. Since a single
133 SSA name may have more than one assertion associated with it, these
134 locations are kept in a linked list attached to the corresponding
135 SSA name. */
136 struct assert_locus_d
138 /* Basic block where the assertion would be inserted. */
139 basic_block bb;
141 /* Some assertions need to be inserted on an edge (e.g., assertions
142 generated by COND_EXPRs). In those cases, BB will be NULL. */
143 edge e;
145 /* Pointer to the statement that generated this assertion. */
146 gimple_stmt_iterator si;
148 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
149 enum tree_code comp_code;
151 /* Value being compared against. */
152 tree val;
154 /* Expression to compare. */
155 tree expr;
157 /* Next node in the linked list. */
158 struct assert_locus_d *next;
161 typedef struct assert_locus_d *assert_locus_t;
163 /* If bit I is present, it means that SSA name N_i has a list of
164 assertions that should be inserted in the IL. */
165 static bitmap need_assert_for;
167 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
168 holds a list of ASSERT_LOCUS_T nodes that describe where
169 ASSERT_EXPRs for SSA name N_I should be inserted. */
170 static assert_locus_t *asserts_for;
172 /* Value range array. After propagation, VR_VALUE[I] holds the range
173 of values that SSA name N_I may take. */
174 static unsigned num_vr_values;
175 static value_range_t **vr_value;
176 static bool values_propagated;
178 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
179 number of executable edges we saw the last time we visited the
180 node. */
181 static int *vr_phi_edge_counts;
183 typedef struct {
184 gimple stmt;
185 tree vec;
186 } switch_update;
188 static vec<edge> to_remove_edges;
189 static vec<switch_update> to_update_switch_stmts;
192 /* Return the maximum value for TYPE. */
194 static inline tree
195 vrp_val_max (const_tree type)
197 if (!INTEGRAL_TYPE_P (type))
198 return NULL_TREE;
200 return TYPE_MAX_VALUE (type);
203 /* Return the minimum value for TYPE. */
205 static inline tree
206 vrp_val_min (const_tree type)
208 if (!INTEGRAL_TYPE_P (type))
209 return NULL_TREE;
211 return TYPE_MIN_VALUE (type);
214 /* Return whether VAL is equal to the maximum value of its type. This
215 will be true for a positive overflow infinity. We can't do a
216 simple equality comparison with TYPE_MAX_VALUE because C typedefs
217 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
218 to the integer constant with the same value in the type. */
220 static inline bool
221 vrp_val_is_max (const_tree val)
223 tree type_max = vrp_val_max (TREE_TYPE (val));
224 return (val == type_max
225 || (type_max != NULL_TREE
226 && operand_equal_p (val, type_max, 0)));
229 /* Return whether VAL is equal to the minimum value of its type. This
230 will be true for a negative overflow infinity. */
232 static inline bool
233 vrp_val_is_min (const_tree val)
235 tree type_min = vrp_val_min (TREE_TYPE (val));
236 return (val == type_min
237 || (type_min != NULL_TREE
238 && operand_equal_p (val, type_min, 0)));
242 /* Return whether TYPE should use an overflow infinity distinct from
243 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
244 represent a signed overflow during VRP computations. An infinity
245 is distinct from a half-range, which will go from some number to
246 TYPE_{MIN,MAX}_VALUE. */
248 static inline bool
249 needs_overflow_infinity (const_tree type)
251 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
254 /* Return whether TYPE can support our overflow infinity
255 representation: we use the TREE_OVERFLOW flag, which only exists
256 for constants. If TYPE doesn't support this, we don't optimize
257 cases which would require signed overflow--we drop them to
258 VARYING. */
260 static inline bool
261 supports_overflow_infinity (const_tree type)
263 tree min = vrp_val_min (type), max = vrp_val_max (type);
264 #ifdef ENABLE_CHECKING
265 gcc_assert (needs_overflow_infinity (type));
266 #endif
267 return (min != NULL_TREE
268 && CONSTANT_CLASS_P (min)
269 && max != NULL_TREE
270 && CONSTANT_CLASS_P (max));
273 /* VAL is the maximum or minimum value of a type. Return a
274 corresponding overflow infinity. */
276 static inline tree
277 make_overflow_infinity (tree val)
279 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
280 val = copy_node (val);
281 TREE_OVERFLOW (val) = 1;
282 return val;
285 /* Return a negative overflow infinity for TYPE. */
287 static inline tree
288 negative_overflow_infinity (tree type)
290 gcc_checking_assert (supports_overflow_infinity (type));
291 return make_overflow_infinity (vrp_val_min (type));
294 /* Return a positive overflow infinity for TYPE. */
296 static inline tree
297 positive_overflow_infinity (tree type)
299 gcc_checking_assert (supports_overflow_infinity (type));
300 return make_overflow_infinity (vrp_val_max (type));
303 /* Return whether VAL is a negative overflow infinity. */
305 static inline bool
306 is_negative_overflow_infinity (const_tree val)
308 return (TREE_OVERFLOW_P (val)
309 && needs_overflow_infinity (TREE_TYPE (val))
310 && vrp_val_is_min (val));
313 /* Return whether VAL is a positive overflow infinity. */
315 static inline bool
316 is_positive_overflow_infinity (const_tree val)
318 return (TREE_OVERFLOW_P (val)
319 && needs_overflow_infinity (TREE_TYPE (val))
320 && vrp_val_is_max (val));
323 /* Return whether VAL is a positive or negative overflow infinity. */
325 static inline bool
326 is_overflow_infinity (const_tree val)
328 return (TREE_OVERFLOW_P (val)
329 && needs_overflow_infinity (TREE_TYPE (val))
330 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
333 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
335 static inline bool
336 stmt_overflow_infinity (gimple stmt)
338 if (is_gimple_assign (stmt)
339 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
340 GIMPLE_SINGLE_RHS)
341 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
342 return false;
345 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
346 the same value with TREE_OVERFLOW clear. This can be used to avoid
347 confusing a regular value with an overflow value. */
349 static inline tree
350 avoid_overflow_infinity (tree val)
352 if (!is_overflow_infinity (val))
353 return val;
355 if (vrp_val_is_max (val))
356 return vrp_val_max (TREE_TYPE (val));
357 else
359 gcc_checking_assert (vrp_val_is_min (val));
360 return vrp_val_min (TREE_TYPE (val));
365 /* Return true if ARG is marked with the nonnull attribute in the
366 current function signature. */
368 static bool
369 nonnull_arg_p (const_tree arg)
371 tree t, attrs, fntype;
372 unsigned HOST_WIDE_INT arg_num;
374 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
376 /* The static chain decl is always non null. */
377 if (arg == cfun->static_chain_decl)
378 return true;
380 fntype = TREE_TYPE (current_function_decl);
381 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
383 attrs = lookup_attribute ("nonnull", attrs);
385 /* If "nonnull" wasn't specified, we know nothing about the argument. */
386 if (attrs == NULL_TREE)
387 return false;
389 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
390 if (TREE_VALUE (attrs) == NULL_TREE)
391 return true;
393 /* Get the position number for ARG in the function signature. */
394 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
396 t = DECL_CHAIN (t), arg_num++)
398 if (t == arg)
399 break;
402 gcc_assert (t == arg);
404 /* Now see if ARG_NUM is mentioned in the nonnull list. */
405 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
407 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
408 return true;
412 return false;
416 /* Set value range VR to VR_UNDEFINED. */
418 static inline void
419 set_value_range_to_undefined (value_range_t *vr)
421 vr->type = VR_UNDEFINED;
422 vr->min = vr->max = NULL_TREE;
423 if (vr->equiv)
424 bitmap_clear (vr->equiv);
428 /* Set value range VR to VR_VARYING. */
430 static inline void
431 set_value_range_to_varying (value_range_t *vr)
433 vr->type = VR_VARYING;
434 vr->min = vr->max = NULL_TREE;
435 if (vr->equiv)
436 bitmap_clear (vr->equiv);
440 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
442 static void
443 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
444 tree max, bitmap equiv)
446 #if defined ENABLE_CHECKING
447 /* Check the validity of the range. */
448 if (t == VR_RANGE || t == VR_ANTI_RANGE)
450 int cmp;
452 gcc_assert (min && max);
454 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
455 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
457 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
458 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
460 cmp = compare_values (min, max);
461 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
463 if (needs_overflow_infinity (TREE_TYPE (min)))
464 gcc_assert (!is_overflow_infinity (min)
465 || !is_overflow_infinity (max));
468 if (t == VR_UNDEFINED || t == VR_VARYING)
469 gcc_assert (min == NULL_TREE && max == NULL_TREE);
471 if (t == VR_UNDEFINED || t == VR_VARYING)
472 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
473 #endif
475 vr->type = t;
476 vr->min = min;
477 vr->max = max;
479 /* Since updating the equivalence set involves deep copying the
480 bitmaps, only do it if absolutely necessary. */
481 if (vr->equiv == NULL
482 && equiv != NULL)
483 vr->equiv = BITMAP_ALLOC (NULL);
485 if (equiv != vr->equiv)
487 if (equiv && !bitmap_empty_p (equiv))
488 bitmap_copy (vr->equiv, equiv);
489 else
490 bitmap_clear (vr->equiv);
495 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
496 This means adjusting T, MIN and MAX representing the case of a
497 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
498 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
499 In corner cases where MAX+1 or MIN-1 wraps this will fall back
500 to varying.
501 This routine exists to ease canonicalization in the case where we
502 extract ranges from var + CST op limit. */
504 static void
505 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
506 tree min, tree max, bitmap equiv)
508 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
509 if (t == VR_UNDEFINED)
511 set_value_range_to_undefined (vr);
512 return;
514 else if (t == VR_VARYING)
516 set_value_range_to_varying (vr);
517 return;
520 /* Nothing to canonicalize for symbolic ranges. */
521 if (TREE_CODE (min) != INTEGER_CST
522 || TREE_CODE (max) != INTEGER_CST)
524 set_value_range (vr, t, min, max, equiv);
525 return;
528 /* Wrong order for min and max, to swap them and the VR type we need
529 to adjust them. */
530 if (tree_int_cst_lt (max, min))
532 tree one, tmp;
534 /* For one bit precision if max < min, then the swapped
535 range covers all values, so for VR_RANGE it is varying and
536 for VR_ANTI_RANGE empty range, so drop to varying as well. */
537 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
539 set_value_range_to_varying (vr);
540 return;
543 one = build_int_cst (TREE_TYPE (min), 1);
544 tmp = int_const_binop (PLUS_EXPR, max, one);
545 max = int_const_binop (MINUS_EXPR, min, one);
546 min = tmp;
548 /* There's one corner case, if we had [C+1, C] before we now have
549 that again. But this represents an empty value range, so drop
550 to varying in this case. */
551 if (tree_int_cst_lt (max, min))
553 set_value_range_to_varying (vr);
554 return;
557 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
560 /* Anti-ranges that can be represented as ranges should be so. */
561 if (t == VR_ANTI_RANGE)
563 bool is_min = vrp_val_is_min (min);
564 bool is_max = vrp_val_is_max (max);
566 if (is_min && is_max)
568 /* We cannot deal with empty ranges, drop to varying.
569 ??? This could be VR_UNDEFINED instead. */
570 set_value_range_to_varying (vr);
571 return;
573 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
574 && (is_min || is_max))
576 /* Non-empty boolean ranges can always be represented
577 as a singleton range. */
578 if (is_min)
579 min = max = vrp_val_max (TREE_TYPE (min));
580 else
581 min = max = vrp_val_min (TREE_TYPE (min));
582 t = VR_RANGE;
584 else if (is_min
585 /* As a special exception preserve non-null ranges. */
586 && !(TYPE_UNSIGNED (TREE_TYPE (min))
587 && integer_zerop (max)))
589 tree one = build_int_cst (TREE_TYPE (max), 1);
590 min = int_const_binop (PLUS_EXPR, max, one);
591 max = vrp_val_max (TREE_TYPE (max));
592 t = VR_RANGE;
594 else if (is_max)
596 tree one = build_int_cst (TREE_TYPE (min), 1);
597 max = int_const_binop (MINUS_EXPR, min, one);
598 min = vrp_val_min (TREE_TYPE (min));
599 t = VR_RANGE;
603 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
604 if (needs_overflow_infinity (TREE_TYPE (min))
605 && is_overflow_infinity (min)
606 && is_overflow_infinity (max))
608 set_value_range_to_varying (vr);
609 return;
612 set_value_range (vr, t, min, max, equiv);
615 /* Copy value range FROM into value range TO. */
617 static inline void
618 copy_value_range (value_range_t *to, value_range_t *from)
620 set_value_range (to, from->type, from->min, from->max, from->equiv);
623 /* Set value range VR to a single value. This function is only called
624 with values we get from statements, and exists to clear the
625 TREE_OVERFLOW flag so that we don't think we have an overflow
626 infinity when we shouldn't. */
628 static inline void
629 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
631 gcc_assert (is_gimple_min_invariant (val));
632 if (TREE_OVERFLOW_P (val))
633 val = drop_tree_overflow (val);
634 set_value_range (vr, VR_RANGE, val, val, equiv);
637 /* Set value range VR to a non-negative range of type TYPE.
638 OVERFLOW_INFINITY indicates whether to use an overflow infinity
639 rather than TYPE_MAX_VALUE; this should be true if we determine
640 that the range is nonnegative based on the assumption that signed
641 overflow does not occur. */
643 static inline void
644 set_value_range_to_nonnegative (value_range_t *vr, tree type,
645 bool overflow_infinity)
647 tree zero;
649 if (overflow_infinity && !supports_overflow_infinity (type))
651 set_value_range_to_varying (vr);
652 return;
655 zero = build_int_cst (type, 0);
656 set_value_range (vr, VR_RANGE, zero,
657 (overflow_infinity
658 ? positive_overflow_infinity (type)
659 : TYPE_MAX_VALUE (type)),
660 vr->equiv);
663 /* Set value range VR to a non-NULL range of type TYPE. */
665 static inline void
666 set_value_range_to_nonnull (value_range_t *vr, tree type)
668 tree zero = build_int_cst (type, 0);
669 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
673 /* Set value range VR to a NULL range of type TYPE. */
675 static inline void
676 set_value_range_to_null (value_range_t *vr, tree type)
678 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
682 /* Set value range VR to a range of a truthvalue of type TYPE. */
684 static inline void
685 set_value_range_to_truthvalue (value_range_t *vr, tree type)
687 if (TYPE_PRECISION (type) == 1)
688 set_value_range_to_varying (vr);
689 else
690 set_value_range (vr, VR_RANGE,
691 build_int_cst (type, 0), build_int_cst (type, 1),
692 vr->equiv);
696 /* If abs (min) < abs (max), set VR to [-max, max], if
697 abs (min) >= abs (max), set VR to [-min, min]. */
699 static void
700 abs_extent_range (value_range_t *vr, tree min, tree max)
702 int cmp;
704 gcc_assert (TREE_CODE (min) == INTEGER_CST);
705 gcc_assert (TREE_CODE (max) == INTEGER_CST);
706 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
707 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
708 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
709 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
710 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
712 set_value_range_to_varying (vr);
713 return;
715 cmp = compare_values (min, max);
716 if (cmp == -1)
717 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
718 else if (cmp == 0 || cmp == 1)
720 max = min;
721 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
723 else
725 set_value_range_to_varying (vr);
726 return;
728 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
732 /* Return value range information for VAR.
734 If we have no values ranges recorded (ie, VRP is not running), then
735 return NULL. Otherwise create an empty range if none existed for VAR. */
737 static value_range_t *
738 get_value_range (const_tree var)
740 static const struct value_range_d vr_const_varying
741 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
742 value_range_t *vr;
743 tree sym;
744 unsigned ver = SSA_NAME_VERSION (var);
746 /* If we have no recorded ranges, then return NULL. */
747 if (! vr_value)
748 return NULL;
750 /* If we query the range for a new SSA name return an unmodifiable VARYING.
751 We should get here at most from the substitute-and-fold stage which
752 will never try to change values. */
753 if (ver >= num_vr_values)
754 return CONST_CAST (value_range_t *, &vr_const_varying);
756 vr = vr_value[ver];
757 if (vr)
758 return vr;
760 /* After propagation finished do not allocate new value-ranges. */
761 if (values_propagated)
762 return CONST_CAST (value_range_t *, &vr_const_varying);
764 /* Create a default value range. */
765 vr_value[ver] = vr = XCNEW (value_range_t);
767 /* Defer allocating the equivalence set. */
768 vr->equiv = NULL;
770 /* If VAR is a default definition of a parameter, the variable can
771 take any value in VAR's type. */
772 if (SSA_NAME_IS_DEFAULT_DEF (var))
774 sym = SSA_NAME_VAR (var);
775 if (TREE_CODE (sym) == PARM_DECL)
777 /* Try to use the "nonnull" attribute to create ~[0, 0]
778 anti-ranges for pointers. Note that this is only valid with
779 default definitions of PARM_DECLs. */
780 if (POINTER_TYPE_P (TREE_TYPE (sym))
781 && nonnull_arg_p (sym))
782 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
783 else
784 set_value_range_to_varying (vr);
786 else if (TREE_CODE (sym) == RESULT_DECL
787 && DECL_BY_REFERENCE (sym))
788 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
791 return vr;
794 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
796 static inline bool
797 vrp_operand_equal_p (const_tree val1, const_tree val2)
799 if (val1 == val2)
800 return true;
801 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
802 return false;
803 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
806 /* Return true, if the bitmaps B1 and B2 are equal. */
808 static inline bool
809 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
811 return (b1 == b2
812 || ((!b1 || bitmap_empty_p (b1))
813 && (!b2 || bitmap_empty_p (b2)))
814 || (b1 && b2
815 && bitmap_equal_p (b1, b2)));
818 /* Update the value range and equivalence set for variable VAR to
819 NEW_VR. Return true if NEW_VR is different from VAR's previous
820 value.
822 NOTE: This function assumes that NEW_VR is a temporary value range
823 object created for the sole purpose of updating VAR's range. The
824 storage used by the equivalence set from NEW_VR will be freed by
825 this function. Do not call update_value_range when NEW_VR
826 is the range object associated with another SSA name. */
828 static inline bool
829 update_value_range (const_tree var, value_range_t *new_vr)
831 value_range_t *old_vr;
832 bool is_new;
834 /* Update the value range, if necessary. */
835 old_vr = get_value_range (var);
836 is_new = old_vr->type != new_vr->type
837 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
838 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
839 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
841 if (is_new)
843 /* Do not allow transitions up the lattice. The following
844 is slightly more awkward than just new_vr->type < old_vr->type
845 because VR_RANGE and VR_ANTI_RANGE need to be considered
846 the same. We may not have is_new when transitioning to
847 UNDEFINED or from VARYING. */
848 if (new_vr->type == VR_UNDEFINED
849 || old_vr->type == VR_VARYING)
850 set_value_range_to_varying (old_vr);
851 else
852 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
853 new_vr->equiv);
856 BITMAP_FREE (new_vr->equiv);
858 return is_new;
862 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
863 point where equivalence processing can be turned on/off. */
865 static void
866 add_equivalence (bitmap *equiv, const_tree var)
868 unsigned ver = SSA_NAME_VERSION (var);
869 value_range_t *vr = vr_value[ver];
871 if (*equiv == NULL)
872 *equiv = BITMAP_ALLOC (NULL);
873 bitmap_set_bit (*equiv, ver);
874 if (vr && vr->equiv)
875 bitmap_ior_into (*equiv, vr->equiv);
879 /* Return true if VR is ~[0, 0]. */
881 static inline bool
882 range_is_nonnull (value_range_t *vr)
884 return vr->type == VR_ANTI_RANGE
885 && integer_zerop (vr->min)
886 && integer_zerop (vr->max);
890 /* Return true if VR is [0, 0]. */
892 static inline bool
893 range_is_null (value_range_t *vr)
895 return vr->type == VR_RANGE
896 && integer_zerop (vr->min)
897 && integer_zerop (vr->max);
900 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
901 a singleton. */
903 static inline bool
904 range_int_cst_p (value_range_t *vr)
906 return (vr->type == VR_RANGE
907 && TREE_CODE (vr->max) == INTEGER_CST
908 && TREE_CODE (vr->min) == INTEGER_CST);
911 /* Return true if VR is a INTEGER_CST singleton. */
913 static inline bool
914 range_int_cst_singleton_p (value_range_t *vr)
916 return (range_int_cst_p (vr)
917 && !is_overflow_infinity (vr->min)
918 && !is_overflow_infinity (vr->max)
919 && tree_int_cst_equal (vr->min, vr->max));
922 /* Return true if value range VR involves at least one symbol. */
924 static inline bool
925 symbolic_range_p (value_range_t *vr)
927 return (!is_gimple_min_invariant (vr->min)
928 || !is_gimple_min_invariant (vr->max));
931 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
932 otherwise. We only handle additive operations and set NEG to true if the
933 symbol is negated and INV to the invariant part, if any. */
935 static tree
936 get_single_symbol (tree t, bool *neg, tree *inv)
938 bool neg_;
939 tree inv_;
941 if (TREE_CODE (t) == PLUS_EXPR
942 || TREE_CODE (t) == POINTER_PLUS_EXPR
943 || TREE_CODE (t) == MINUS_EXPR)
945 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
947 neg_ = (TREE_CODE (t) == MINUS_EXPR);
948 inv_ = TREE_OPERAND (t, 0);
949 t = TREE_OPERAND (t, 1);
951 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
953 neg_ = false;
954 inv_ = TREE_OPERAND (t, 1);
955 t = TREE_OPERAND (t, 0);
957 else
958 return NULL_TREE;
960 else
962 neg_ = false;
963 inv_ = NULL_TREE;
966 if (TREE_CODE (t) == NEGATE_EXPR)
968 t = TREE_OPERAND (t, 0);
969 neg_ = !neg_;
972 if (TREE_CODE (t) != SSA_NAME)
973 return NULL_TREE;
975 *neg = neg_;
976 *inv = inv_;
977 return t;
980 /* The reverse operation: build a symbolic expression with TYPE
981 from symbol SYM, negated according to NEG, and invariant INV. */
983 static tree
984 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
986 const bool pointer_p = POINTER_TYPE_P (type);
987 tree t = sym;
989 if (neg)
990 t = build1 (NEGATE_EXPR, type, t);
992 if (integer_zerop (inv))
993 return t;
995 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
998 /* Return true if value range VR involves exactly one symbol SYM. */
1000 static bool
1001 symbolic_range_based_on_p (value_range_t *vr, const_tree sym)
1003 bool neg, min_has_symbol, max_has_symbol;
1004 tree inv;
1006 if (is_gimple_min_invariant (vr->min))
1007 min_has_symbol = false;
1008 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
1009 min_has_symbol = true;
1010 else
1011 return false;
1013 if (is_gimple_min_invariant (vr->max))
1014 max_has_symbol = false;
1015 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
1016 max_has_symbol = true;
1017 else
1018 return false;
1020 return (min_has_symbol || max_has_symbol);
1023 /* Return true if value range VR uses an overflow infinity. */
1025 static inline bool
1026 overflow_infinity_range_p (value_range_t *vr)
1028 return (vr->type == VR_RANGE
1029 && (is_overflow_infinity (vr->min)
1030 || is_overflow_infinity (vr->max)));
1033 /* Return false if we can not make a valid comparison based on VR;
1034 this will be the case if it uses an overflow infinity and overflow
1035 is not undefined (i.e., -fno-strict-overflow is in effect).
1036 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1037 uses an overflow infinity. */
1039 static bool
1040 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
1042 gcc_assert (vr->type == VR_RANGE);
1043 if (is_overflow_infinity (vr->min))
1045 *strict_overflow_p = true;
1046 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1047 return false;
1049 if (is_overflow_infinity (vr->max))
1051 *strict_overflow_p = true;
1052 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1053 return false;
1055 return true;
1059 /* Return true if the result of assignment STMT is know to be non-negative.
1060 If the return value is based on the assumption that signed overflow is
1061 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1062 *STRICT_OVERFLOW_P.*/
1064 static bool
1065 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1067 enum tree_code code = gimple_assign_rhs_code (stmt);
1068 switch (get_gimple_rhs_class (code))
1070 case GIMPLE_UNARY_RHS:
1071 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1072 gimple_expr_type (stmt),
1073 gimple_assign_rhs1 (stmt),
1074 strict_overflow_p);
1075 case GIMPLE_BINARY_RHS:
1076 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1077 gimple_expr_type (stmt),
1078 gimple_assign_rhs1 (stmt),
1079 gimple_assign_rhs2 (stmt),
1080 strict_overflow_p);
1081 case GIMPLE_TERNARY_RHS:
1082 return false;
1083 case GIMPLE_SINGLE_RHS:
1084 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
1085 strict_overflow_p);
1086 case GIMPLE_INVALID_RHS:
1087 gcc_unreachable ();
1088 default:
1089 gcc_unreachable ();
1093 /* Return true if return value of call STMT is know to be non-negative.
1094 If the return value is based on the assumption that signed overflow is
1095 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1096 *STRICT_OVERFLOW_P.*/
1098 static bool
1099 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1101 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1102 gimple_call_arg (stmt, 0) : NULL_TREE;
1103 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1104 gimple_call_arg (stmt, 1) : NULL_TREE;
1106 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1107 gimple_call_fndecl (stmt),
1108 arg0,
1109 arg1,
1110 strict_overflow_p);
1113 /* Return true if STMT is know to to compute a non-negative value.
1114 If the return value is based on the assumption that signed overflow is
1115 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1116 *STRICT_OVERFLOW_P.*/
1118 static bool
1119 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1121 switch (gimple_code (stmt))
1123 case GIMPLE_ASSIGN:
1124 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1125 case GIMPLE_CALL:
1126 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1127 default:
1128 gcc_unreachable ();
1132 /* Return true if the result of assignment STMT is know to be non-zero.
1133 If the return value is based on the assumption that signed overflow is
1134 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1135 *STRICT_OVERFLOW_P.*/
1137 static bool
1138 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1140 enum tree_code code = gimple_assign_rhs_code (stmt);
1141 switch (get_gimple_rhs_class (code))
1143 case GIMPLE_UNARY_RHS:
1144 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1145 gimple_expr_type (stmt),
1146 gimple_assign_rhs1 (stmt),
1147 strict_overflow_p);
1148 case GIMPLE_BINARY_RHS:
1149 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1150 gimple_expr_type (stmt),
1151 gimple_assign_rhs1 (stmt),
1152 gimple_assign_rhs2 (stmt),
1153 strict_overflow_p);
1154 case GIMPLE_TERNARY_RHS:
1155 return false;
1156 case GIMPLE_SINGLE_RHS:
1157 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1158 strict_overflow_p);
1159 case GIMPLE_INVALID_RHS:
1160 gcc_unreachable ();
1161 default:
1162 gcc_unreachable ();
1166 /* Return true if STMT is known to compute a non-zero value.
1167 If the return value is based on the assumption that signed overflow is
1168 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1169 *STRICT_OVERFLOW_P.*/
1171 static bool
1172 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1174 switch (gimple_code (stmt))
1176 case GIMPLE_ASSIGN:
1177 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1178 case GIMPLE_CALL:
1180 tree fndecl = gimple_call_fndecl (stmt);
1181 if (!fndecl) return false;
1182 if (flag_delete_null_pointer_checks && !flag_check_new
1183 && DECL_IS_OPERATOR_NEW (fndecl)
1184 && !TREE_NOTHROW (fndecl))
1185 return true;
1186 if (flag_delete_null_pointer_checks &&
1187 lookup_attribute ("returns_nonnull",
1188 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1189 return true;
1190 return gimple_alloca_call_p (stmt);
1192 default:
1193 gcc_unreachable ();
1197 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1198 obtained so far. */
1200 static bool
1201 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1203 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1204 return true;
1206 /* If we have an expression of the form &X->a, then the expression
1207 is nonnull if X is nonnull. */
1208 if (is_gimple_assign (stmt)
1209 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1211 tree expr = gimple_assign_rhs1 (stmt);
1212 tree base = get_base_address (TREE_OPERAND (expr, 0));
1214 if (base != NULL_TREE
1215 && TREE_CODE (base) == MEM_REF
1216 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1218 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1219 if (range_is_nonnull (vr))
1220 return true;
1224 return false;
1227 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1228 a gimple invariant, or SSA_NAME +- CST. */
1230 static bool
1231 valid_value_p (tree expr)
1233 if (TREE_CODE (expr) == SSA_NAME)
1234 return true;
1236 if (TREE_CODE (expr) == PLUS_EXPR
1237 || TREE_CODE (expr) == MINUS_EXPR)
1238 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1239 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1241 return is_gimple_min_invariant (expr);
1244 /* Return
1245 1 if VAL < VAL2
1246 0 if !(VAL < VAL2)
1247 -2 if those are incomparable. */
1248 static inline int
1249 operand_less_p (tree val, tree val2)
1251 /* LT is folded faster than GE and others. Inline the common case. */
1252 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1253 return tree_int_cst_lt (val, val2);
1254 else
1256 tree tcmp;
1258 fold_defer_overflow_warnings ();
1260 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1262 fold_undefer_and_ignore_overflow_warnings ();
1264 if (!tcmp
1265 || TREE_CODE (tcmp) != INTEGER_CST)
1266 return -2;
1268 if (!integer_zerop (tcmp))
1269 return 1;
1272 /* val >= val2, not considering overflow infinity. */
1273 if (is_negative_overflow_infinity (val))
1274 return is_negative_overflow_infinity (val2) ? 0 : 1;
1275 else if (is_positive_overflow_infinity (val2))
1276 return is_positive_overflow_infinity (val) ? 0 : 1;
1278 return 0;
1281 /* Compare two values VAL1 and VAL2. Return
1283 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1284 -1 if VAL1 < VAL2,
1285 0 if VAL1 == VAL2,
1286 +1 if VAL1 > VAL2, and
1287 +2 if VAL1 != VAL2
1289 This is similar to tree_int_cst_compare but supports pointer values
1290 and values that cannot be compared at compile time.
1292 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1293 true if the return value is only valid if we assume that signed
1294 overflow is undefined. */
1296 static int
1297 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1299 if (val1 == val2)
1300 return 0;
1302 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1303 both integers. */
1304 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1305 == POINTER_TYPE_P (TREE_TYPE (val2)));
1307 /* Convert the two values into the same type. This is needed because
1308 sizetype causes sign extension even for unsigned types. */
1309 val2 = fold_convert (TREE_TYPE (val1), val2);
1310 STRIP_USELESS_TYPE_CONVERSION (val2);
1312 if ((TREE_CODE (val1) == SSA_NAME
1313 || (TREE_CODE (val1) == NEGATE_EXPR
1314 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1315 || TREE_CODE (val1) == PLUS_EXPR
1316 || TREE_CODE (val1) == MINUS_EXPR)
1317 && (TREE_CODE (val2) == SSA_NAME
1318 || (TREE_CODE (val2) == NEGATE_EXPR
1319 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1320 || TREE_CODE (val2) == PLUS_EXPR
1321 || TREE_CODE (val2) == MINUS_EXPR))
1323 tree n1, c1, n2, c2;
1324 enum tree_code code1, code2;
1326 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1327 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1328 same name, return -2. */
1329 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1331 code1 = SSA_NAME;
1332 n1 = val1;
1333 c1 = NULL_TREE;
1335 else
1337 code1 = TREE_CODE (val1);
1338 n1 = TREE_OPERAND (val1, 0);
1339 c1 = TREE_OPERAND (val1, 1);
1340 if (tree_int_cst_sgn (c1) == -1)
1342 if (is_negative_overflow_infinity (c1))
1343 return -2;
1344 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1345 if (!c1)
1346 return -2;
1347 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1351 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1353 code2 = SSA_NAME;
1354 n2 = val2;
1355 c2 = NULL_TREE;
1357 else
1359 code2 = TREE_CODE (val2);
1360 n2 = TREE_OPERAND (val2, 0);
1361 c2 = TREE_OPERAND (val2, 1);
1362 if (tree_int_cst_sgn (c2) == -1)
1364 if (is_negative_overflow_infinity (c2))
1365 return -2;
1366 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1367 if (!c2)
1368 return -2;
1369 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1373 /* Both values must use the same name. */
1374 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1376 n1 = TREE_OPERAND (n1, 0);
1377 n2 = TREE_OPERAND (n2, 0);
1379 if (n1 != n2)
1380 return -2;
1382 if (code1 == SSA_NAME && code2 == SSA_NAME)
1383 /* NAME == NAME */
1384 return 0;
1386 /* If overflow is defined we cannot simplify more. */
1387 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1388 return -2;
1390 if (strict_overflow_p != NULL
1391 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1392 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1393 *strict_overflow_p = true;
1395 if (code1 == SSA_NAME)
1397 if (code2 == PLUS_EXPR)
1398 /* NAME < NAME + CST */
1399 return -1;
1400 else if (code2 == MINUS_EXPR)
1401 /* NAME > NAME - CST */
1402 return 1;
1404 else if (code1 == PLUS_EXPR)
1406 if (code2 == SSA_NAME)
1407 /* NAME + CST > NAME */
1408 return 1;
1409 else if (code2 == PLUS_EXPR)
1410 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1411 return compare_values_warnv (c1, c2, strict_overflow_p);
1412 else if (code2 == MINUS_EXPR)
1413 /* NAME + CST1 > NAME - CST2 */
1414 return 1;
1416 else if (code1 == MINUS_EXPR)
1418 if (code2 == SSA_NAME)
1419 /* NAME - CST < NAME */
1420 return -1;
1421 else if (code2 == PLUS_EXPR)
1422 /* NAME - CST1 < NAME + CST2 */
1423 return -1;
1424 else if (code2 == MINUS_EXPR)
1425 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1426 C1 and C2 are swapped in the call to compare_values. */
1427 return compare_values_warnv (c2, c1, strict_overflow_p);
1430 gcc_unreachable ();
1433 /* We cannot compare non-constants. */
1434 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1435 return -2;
1437 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1439 /* We cannot compare overflowed values, except for overflow
1440 infinities. */
1441 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1443 if (strict_overflow_p != NULL)
1444 *strict_overflow_p = true;
1445 if (is_negative_overflow_infinity (val1))
1446 return is_negative_overflow_infinity (val2) ? 0 : -1;
1447 else if (is_negative_overflow_infinity (val2))
1448 return 1;
1449 else if (is_positive_overflow_infinity (val1))
1450 return is_positive_overflow_infinity (val2) ? 0 : 1;
1451 else if (is_positive_overflow_infinity (val2))
1452 return -1;
1453 return -2;
1456 return tree_int_cst_compare (val1, val2);
1458 else
1460 tree t;
1462 /* First see if VAL1 and VAL2 are not the same. */
1463 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1464 return 0;
1466 /* If VAL1 is a lower address than VAL2, return -1. */
1467 if (operand_less_p (val1, val2) == 1)
1468 return -1;
1470 /* If VAL1 is a higher address than VAL2, return +1. */
1471 if (operand_less_p (val2, val1) == 1)
1472 return 1;
1474 /* If VAL1 is different than VAL2, return +2.
1475 For integer constants we either have already returned -1 or 1
1476 or they are equivalent. We still might succeed in proving
1477 something about non-trivial operands. */
1478 if (TREE_CODE (val1) != INTEGER_CST
1479 || TREE_CODE (val2) != INTEGER_CST)
1481 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1482 if (t && integer_onep (t))
1483 return 2;
1486 return -2;
1490 /* Compare values like compare_values_warnv, but treat comparisons of
1491 nonconstants which rely on undefined overflow as incomparable. */
1493 static int
1494 compare_values (tree val1, tree val2)
1496 bool sop;
1497 int ret;
1499 sop = false;
1500 ret = compare_values_warnv (val1, val2, &sop);
1501 if (sop
1502 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1503 ret = -2;
1504 return ret;
1508 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1509 0 if VAL is not inside [MIN, MAX],
1510 -2 if we cannot tell either way.
1512 Benchmark compile/20001226-1.c compilation time after changing this
1513 function. */
1515 static inline int
1516 value_inside_range (tree val, tree min, tree max)
1518 int cmp1, cmp2;
1520 cmp1 = operand_less_p (val, min);
1521 if (cmp1 == -2)
1522 return -2;
1523 if (cmp1 == 1)
1524 return 0;
1526 cmp2 = operand_less_p (max, val);
1527 if (cmp2 == -2)
1528 return -2;
1530 return !cmp2;
1534 /* Return true if value ranges VR0 and VR1 have a non-empty
1535 intersection.
1537 Benchmark compile/20001226-1.c compilation time after changing this
1538 function.
1541 static inline bool
1542 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1544 /* The value ranges do not intersect if the maximum of the first range is
1545 less than the minimum of the second range or vice versa.
1546 When those relations are unknown, we can't do any better. */
1547 if (operand_less_p (vr0->max, vr1->min) != 0)
1548 return false;
1549 if (operand_less_p (vr1->max, vr0->min) != 0)
1550 return false;
1551 return true;
1555 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1556 include the value zero, -2 if we cannot tell. */
1558 static inline int
1559 range_includes_zero_p (tree min, tree max)
1561 tree zero = build_int_cst (TREE_TYPE (min), 0);
1562 return value_inside_range (zero, min, max);
1565 /* Return true if *VR is know to only contain nonnegative values. */
1567 static inline bool
1568 value_range_nonnegative_p (value_range_t *vr)
1570 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1571 which would return a useful value should be encoded as a
1572 VR_RANGE. */
1573 if (vr->type == VR_RANGE)
1575 int result = compare_values (vr->min, integer_zero_node);
1576 return (result == 0 || result == 1);
1579 return false;
1582 /* If *VR has a value rante that is a single constant value return that,
1583 otherwise return NULL_TREE. */
1585 static tree
1586 value_range_constant_singleton (value_range_t *vr)
1588 if (vr->type == VR_RANGE
1589 && operand_equal_p (vr->min, vr->max, 0)
1590 && is_gimple_min_invariant (vr->min))
1591 return vr->min;
1593 return NULL_TREE;
1596 /* If OP has a value range with a single constant value return that,
1597 otherwise return NULL_TREE. This returns OP itself if OP is a
1598 constant. */
1600 static tree
1601 op_with_constant_singleton_value_range (tree op)
1603 if (is_gimple_min_invariant (op))
1604 return op;
1606 if (TREE_CODE (op) != SSA_NAME)
1607 return NULL_TREE;
1609 return value_range_constant_singleton (get_value_range (op));
1612 /* Return true if op is in a boolean [0, 1] value-range. */
1614 static bool
1615 op_with_boolean_value_range_p (tree op)
1617 value_range_t *vr;
1619 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1620 return true;
1622 if (integer_zerop (op)
1623 || integer_onep (op))
1624 return true;
1626 if (TREE_CODE (op) != SSA_NAME)
1627 return false;
1629 vr = get_value_range (op);
1630 return (vr->type == VR_RANGE
1631 && integer_zerop (vr->min)
1632 && integer_onep (vr->max));
1635 /* Extract value range information from an ASSERT_EXPR EXPR and store
1636 it in *VR_P. */
1638 static void
1639 extract_range_from_assert (value_range_t *vr_p, tree expr)
1641 tree var, cond, limit, min, max, type;
1642 value_range_t *limit_vr;
1643 enum tree_code cond_code;
1645 var = ASSERT_EXPR_VAR (expr);
1646 cond = ASSERT_EXPR_COND (expr);
1648 gcc_assert (COMPARISON_CLASS_P (cond));
1650 /* Find VAR in the ASSERT_EXPR conditional. */
1651 if (var == TREE_OPERAND (cond, 0)
1652 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1653 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1655 /* If the predicate is of the form VAR COMP LIMIT, then we just
1656 take LIMIT from the RHS and use the same comparison code. */
1657 cond_code = TREE_CODE (cond);
1658 limit = TREE_OPERAND (cond, 1);
1659 cond = TREE_OPERAND (cond, 0);
1661 else
1663 /* If the predicate is of the form LIMIT COMP VAR, then we need
1664 to flip around the comparison code to create the proper range
1665 for VAR. */
1666 cond_code = swap_tree_comparison (TREE_CODE (cond));
1667 limit = TREE_OPERAND (cond, 0);
1668 cond = TREE_OPERAND (cond, 1);
1671 limit = avoid_overflow_infinity (limit);
1673 type = TREE_TYPE (var);
1674 gcc_assert (limit != var);
1676 /* For pointer arithmetic, we only keep track of pointer equality
1677 and inequality. */
1678 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1680 set_value_range_to_varying (vr_p);
1681 return;
1684 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1685 try to use LIMIT's range to avoid creating symbolic ranges
1686 unnecessarily. */
1687 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1689 /* LIMIT's range is only interesting if it has any useful information. */
1690 if (limit_vr
1691 && (limit_vr->type == VR_UNDEFINED
1692 || limit_vr->type == VR_VARYING
1693 || symbolic_range_p (limit_vr)))
1694 limit_vr = NULL;
1696 /* Initially, the new range has the same set of equivalences of
1697 VAR's range. This will be revised before returning the final
1698 value. Since assertions may be chained via mutually exclusive
1699 predicates, we will need to trim the set of equivalences before
1700 we are done. */
1701 gcc_assert (vr_p->equiv == NULL);
1702 add_equivalence (&vr_p->equiv, var);
1704 /* Extract a new range based on the asserted comparison for VAR and
1705 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1706 will only use it for equality comparisons (EQ_EXPR). For any
1707 other kind of assertion, we cannot derive a range from LIMIT's
1708 anti-range that can be used to describe the new range. For
1709 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1710 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1711 no single range for x_2 that could describe LE_EXPR, so we might
1712 as well build the range [b_4, +INF] for it.
1713 One special case we handle is extracting a range from a
1714 range test encoded as (unsigned)var + CST <= limit. */
1715 if (TREE_CODE (cond) == NOP_EXPR
1716 || TREE_CODE (cond) == PLUS_EXPR)
1718 if (TREE_CODE (cond) == PLUS_EXPR)
1720 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1721 TREE_OPERAND (cond, 1));
1722 max = int_const_binop (PLUS_EXPR, limit, min);
1723 cond = TREE_OPERAND (cond, 0);
1725 else
1727 min = build_int_cst (TREE_TYPE (var), 0);
1728 max = limit;
1731 /* Make sure to not set TREE_OVERFLOW on the final type
1732 conversion. We are willingly interpreting large positive
1733 unsigned values as negative signed values here. */
1734 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1735 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1737 /* We can transform a max, min range to an anti-range or
1738 vice-versa. Use set_and_canonicalize_value_range which does
1739 this for us. */
1740 if (cond_code == LE_EXPR)
1741 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1742 min, max, vr_p->equiv);
1743 else if (cond_code == GT_EXPR)
1744 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1745 min, max, vr_p->equiv);
1746 else
1747 gcc_unreachable ();
1749 else if (cond_code == EQ_EXPR)
1751 enum value_range_type range_type;
1753 if (limit_vr)
1755 range_type = limit_vr->type;
1756 min = limit_vr->min;
1757 max = limit_vr->max;
1759 else
1761 range_type = VR_RANGE;
1762 min = limit;
1763 max = limit;
1766 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1768 /* When asserting the equality VAR == LIMIT and LIMIT is another
1769 SSA name, the new range will also inherit the equivalence set
1770 from LIMIT. */
1771 if (TREE_CODE (limit) == SSA_NAME)
1772 add_equivalence (&vr_p->equiv, limit);
1774 else if (cond_code == NE_EXPR)
1776 /* As described above, when LIMIT's range is an anti-range and
1777 this assertion is an inequality (NE_EXPR), then we cannot
1778 derive anything from the anti-range. For instance, if
1779 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1780 not imply that VAR's range is [0, 0]. So, in the case of
1781 anti-ranges, we just assert the inequality using LIMIT and
1782 not its anti-range.
1784 If LIMIT_VR is a range, we can only use it to build a new
1785 anti-range if LIMIT_VR is a single-valued range. For
1786 instance, if LIMIT_VR is [0, 1], the predicate
1787 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1788 Rather, it means that for value 0 VAR should be ~[0, 0]
1789 and for value 1, VAR should be ~[1, 1]. We cannot
1790 represent these ranges.
1792 The only situation in which we can build a valid
1793 anti-range is when LIMIT_VR is a single-valued range
1794 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1795 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1796 if (limit_vr
1797 && limit_vr->type == VR_RANGE
1798 && compare_values (limit_vr->min, limit_vr->max) == 0)
1800 min = limit_vr->min;
1801 max = limit_vr->max;
1803 else
1805 /* In any other case, we cannot use LIMIT's range to build a
1806 valid anti-range. */
1807 min = max = limit;
1810 /* If MIN and MAX cover the whole range for their type, then
1811 just use the original LIMIT. */
1812 if (INTEGRAL_TYPE_P (type)
1813 && vrp_val_is_min (min)
1814 && vrp_val_is_max (max))
1815 min = max = limit;
1817 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1818 min, max, vr_p->equiv);
1820 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1822 min = TYPE_MIN_VALUE (type);
1824 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1825 max = limit;
1826 else
1828 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1829 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1830 LT_EXPR. */
1831 max = limit_vr->max;
1834 /* If the maximum value forces us to be out of bounds, simply punt.
1835 It would be pointless to try and do anything more since this
1836 all should be optimized away above us. */
1837 if ((cond_code == LT_EXPR
1838 && compare_values (max, min) == 0)
1839 || is_overflow_infinity (max))
1840 set_value_range_to_varying (vr_p);
1841 else
1843 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1844 if (cond_code == LT_EXPR)
1846 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1847 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1848 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1849 build_int_cst (TREE_TYPE (max), -1));
1850 else
1851 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1852 build_int_cst (TREE_TYPE (max), 1));
1853 if (EXPR_P (max))
1854 TREE_NO_WARNING (max) = 1;
1857 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1860 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1862 max = TYPE_MAX_VALUE (type);
1864 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1865 min = limit;
1866 else
1868 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1869 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1870 GT_EXPR. */
1871 min = limit_vr->min;
1874 /* If the minimum value forces us to be out of bounds, simply punt.
1875 It would be pointless to try and do anything more since this
1876 all should be optimized away above us. */
1877 if ((cond_code == GT_EXPR
1878 && compare_values (min, max) == 0)
1879 || is_overflow_infinity (min))
1880 set_value_range_to_varying (vr_p);
1881 else
1883 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1884 if (cond_code == GT_EXPR)
1886 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1887 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1888 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1889 build_int_cst (TREE_TYPE (min), -1));
1890 else
1891 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1892 build_int_cst (TREE_TYPE (min), 1));
1893 if (EXPR_P (min))
1894 TREE_NO_WARNING (min) = 1;
1897 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1900 else
1901 gcc_unreachable ();
1903 /* Finally intersect the new range with what we already know about var. */
1904 vrp_intersect_ranges (vr_p, get_value_range (var));
1908 /* Extract range information from SSA name VAR and store it in VR. If
1909 VAR has an interesting range, use it. Otherwise, create the
1910 range [VAR, VAR] and return it. This is useful in situations where
1911 we may have conditionals testing values of VARYING names. For
1912 instance,
1914 x_3 = y_5;
1915 if (x_3 > y_5)
1918 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1919 always false. */
1921 static void
1922 extract_range_from_ssa_name (value_range_t *vr, tree var)
1924 value_range_t *var_vr = get_value_range (var);
1926 if (var_vr->type != VR_VARYING)
1927 copy_value_range (vr, var_vr);
1928 else
1929 set_value_range (vr, VR_RANGE, var, var, NULL);
1931 add_equivalence (&vr->equiv, var);
1935 /* Wrapper around int_const_binop. If the operation overflows and we
1936 are not using wrapping arithmetic, then adjust the result to be
1937 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1938 NULL_TREE if we need to use an overflow infinity representation but
1939 the type does not support it. */
1941 static tree
1942 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1944 tree res;
1946 res = int_const_binop (code, val1, val2);
1948 /* If we are using unsigned arithmetic, operate symbolically
1949 on -INF and +INF as int_const_binop only handles signed overflow. */
1950 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1952 int checkz = compare_values (res, val1);
1953 bool overflow = false;
1955 /* Ensure that res = val1 [+*] val2 >= val1
1956 or that res = val1 - val2 <= val1. */
1957 if ((code == PLUS_EXPR
1958 && !(checkz == 1 || checkz == 0))
1959 || (code == MINUS_EXPR
1960 && !(checkz == 0 || checkz == -1)))
1962 overflow = true;
1964 /* Checking for multiplication overflow is done by dividing the
1965 output of the multiplication by the first input of the
1966 multiplication. If the result of that division operation is
1967 not equal to the second input of the multiplication, then the
1968 multiplication overflowed. */
1969 else if (code == MULT_EXPR && !integer_zerop (val1))
1971 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1972 res,
1973 val1);
1974 int check = compare_values (tmp, val2);
1976 if (check != 0)
1977 overflow = true;
1980 if (overflow)
1982 res = copy_node (res);
1983 TREE_OVERFLOW (res) = 1;
1987 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1988 /* If the singed operation wraps then int_const_binop has done
1989 everything we want. */
1991 /* Signed division of -1/0 overflows and by the time it gets here
1992 returns NULL_TREE. */
1993 else if (!res)
1994 return NULL_TREE;
1995 else if ((TREE_OVERFLOW (res)
1996 && !TREE_OVERFLOW (val1)
1997 && !TREE_OVERFLOW (val2))
1998 || is_overflow_infinity (val1)
1999 || is_overflow_infinity (val2))
2001 /* If the operation overflowed but neither VAL1 nor VAL2 are
2002 overflown, return -INF or +INF depending on the operation
2003 and the combination of signs of the operands. */
2004 int sgn1 = tree_int_cst_sgn (val1);
2005 int sgn2 = tree_int_cst_sgn (val2);
2007 if (needs_overflow_infinity (TREE_TYPE (res))
2008 && !supports_overflow_infinity (TREE_TYPE (res)))
2009 return NULL_TREE;
2011 /* We have to punt on adding infinities of different signs,
2012 since we can't tell what the sign of the result should be.
2013 Likewise for subtracting infinities of the same sign. */
2014 if (((code == PLUS_EXPR && sgn1 != sgn2)
2015 || (code == MINUS_EXPR && sgn1 == sgn2))
2016 && is_overflow_infinity (val1)
2017 && is_overflow_infinity (val2))
2018 return NULL_TREE;
2020 /* Don't try to handle division or shifting of infinities. */
2021 if ((code == TRUNC_DIV_EXPR
2022 || code == FLOOR_DIV_EXPR
2023 || code == CEIL_DIV_EXPR
2024 || code == EXACT_DIV_EXPR
2025 || code == ROUND_DIV_EXPR
2026 || code == RSHIFT_EXPR)
2027 && (is_overflow_infinity (val1)
2028 || is_overflow_infinity (val2)))
2029 return NULL_TREE;
2031 /* Notice that we only need to handle the restricted set of
2032 operations handled by extract_range_from_binary_expr.
2033 Among them, only multiplication, addition and subtraction
2034 can yield overflow without overflown operands because we
2035 are working with integral types only... except in the
2036 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2037 for division too. */
2039 /* For multiplication, the sign of the overflow is given
2040 by the comparison of the signs of the operands. */
2041 if ((code == MULT_EXPR && sgn1 == sgn2)
2042 /* For addition, the operands must be of the same sign
2043 to yield an overflow. Its sign is therefore that
2044 of one of the operands, for example the first. For
2045 infinite operands X + -INF is negative, not positive. */
2046 || (code == PLUS_EXPR
2047 && (sgn1 >= 0
2048 ? !is_negative_overflow_infinity (val2)
2049 : is_positive_overflow_infinity (val2)))
2050 /* For subtraction, non-infinite operands must be of
2051 different signs to yield an overflow. Its sign is
2052 therefore that of the first operand or the opposite of
2053 that of the second operand. A first operand of 0 counts
2054 as positive here, for the corner case 0 - (-INF), which
2055 overflows, but must yield +INF. For infinite operands 0
2056 - INF is negative, not positive. */
2057 || (code == MINUS_EXPR
2058 && (sgn1 >= 0
2059 ? !is_positive_overflow_infinity (val2)
2060 : is_negative_overflow_infinity (val2)))
2061 /* We only get in here with positive shift count, so the
2062 overflow direction is the same as the sign of val1.
2063 Actually rshift does not overflow at all, but we only
2064 handle the case of shifting overflowed -INF and +INF. */
2065 || (code == RSHIFT_EXPR
2066 && sgn1 >= 0)
2067 /* For division, the only case is -INF / -1 = +INF. */
2068 || code == TRUNC_DIV_EXPR
2069 || code == FLOOR_DIV_EXPR
2070 || code == CEIL_DIV_EXPR
2071 || code == EXACT_DIV_EXPR
2072 || code == ROUND_DIV_EXPR)
2073 return (needs_overflow_infinity (TREE_TYPE (res))
2074 ? positive_overflow_infinity (TREE_TYPE (res))
2075 : TYPE_MAX_VALUE (TREE_TYPE (res)));
2076 else
2077 return (needs_overflow_infinity (TREE_TYPE (res))
2078 ? negative_overflow_infinity (TREE_TYPE (res))
2079 : TYPE_MIN_VALUE (TREE_TYPE (res)));
2082 return res;
2086 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2087 bitmask if some bit is unset, it means for all numbers in the range
2088 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2089 bitmask if some bit is set, it means for all numbers in the range
2090 the bit is 1, otherwise it might be 0 or 1. */
2092 static bool
2093 zero_nonzero_bits_from_vr (const tree expr_type,
2094 value_range_t *vr,
2095 wide_int *may_be_nonzero,
2096 wide_int *must_be_nonzero)
2098 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2099 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2100 if (!range_int_cst_p (vr)
2101 || is_overflow_infinity (vr->min)
2102 || is_overflow_infinity (vr->max))
2103 return false;
2105 if (range_int_cst_singleton_p (vr))
2107 *may_be_nonzero = vr->min;
2108 *must_be_nonzero = *may_be_nonzero;
2110 else if (tree_int_cst_sgn (vr->min) >= 0
2111 || tree_int_cst_sgn (vr->max) < 0)
2113 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2114 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2115 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2116 if (xor_mask != 0)
2118 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2119 may_be_nonzero->get_precision ());
2120 *may_be_nonzero = *may_be_nonzero | mask;
2121 *must_be_nonzero = must_be_nonzero->and_not (mask);
2125 return true;
2128 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2129 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2130 false otherwise. If *AR can be represented with a single range
2131 *VR1 will be VR_UNDEFINED. */
2133 static bool
2134 ranges_from_anti_range (value_range_t *ar,
2135 value_range_t *vr0, value_range_t *vr1)
2137 tree type = TREE_TYPE (ar->min);
2139 vr0->type = VR_UNDEFINED;
2140 vr1->type = VR_UNDEFINED;
2142 if (ar->type != VR_ANTI_RANGE
2143 || TREE_CODE (ar->min) != INTEGER_CST
2144 || TREE_CODE (ar->max) != INTEGER_CST
2145 || !vrp_val_min (type)
2146 || !vrp_val_max (type))
2147 return false;
2149 if (!vrp_val_is_min (ar->min))
2151 vr0->type = VR_RANGE;
2152 vr0->min = vrp_val_min (type);
2153 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2155 if (!vrp_val_is_max (ar->max))
2157 vr1->type = VR_RANGE;
2158 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2159 vr1->max = vrp_val_max (type);
2161 if (vr0->type == VR_UNDEFINED)
2163 *vr0 = *vr1;
2164 vr1->type = VR_UNDEFINED;
2167 return vr0->type != VR_UNDEFINED;
2170 /* Helper to extract a value-range *VR for a multiplicative operation
2171 *VR0 CODE *VR1. */
2173 static void
2174 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2175 enum tree_code code,
2176 value_range_t *vr0, value_range_t *vr1)
2178 enum value_range_type type;
2179 tree val[4];
2180 size_t i;
2181 tree min, max;
2182 bool sop;
2183 int cmp;
2185 /* Multiplications, divisions and shifts are a bit tricky to handle,
2186 depending on the mix of signs we have in the two ranges, we
2187 need to operate on different values to get the minimum and
2188 maximum values for the new range. One approach is to figure
2189 out all the variations of range combinations and do the
2190 operations.
2192 However, this involves several calls to compare_values and it
2193 is pretty convoluted. It's simpler to do the 4 operations
2194 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2195 MAX1) and then figure the smallest and largest values to form
2196 the new range. */
2197 gcc_assert (code == MULT_EXPR
2198 || code == TRUNC_DIV_EXPR
2199 || code == FLOOR_DIV_EXPR
2200 || code == CEIL_DIV_EXPR
2201 || code == EXACT_DIV_EXPR
2202 || code == ROUND_DIV_EXPR
2203 || code == RSHIFT_EXPR
2204 || code == LSHIFT_EXPR);
2205 gcc_assert ((vr0->type == VR_RANGE
2206 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2207 && vr0->type == vr1->type);
2209 type = vr0->type;
2211 /* Compute the 4 cross operations. */
2212 sop = false;
2213 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2214 if (val[0] == NULL_TREE)
2215 sop = true;
2217 if (vr1->max == vr1->min)
2218 val[1] = NULL_TREE;
2219 else
2221 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2222 if (val[1] == NULL_TREE)
2223 sop = true;
2226 if (vr0->max == vr0->min)
2227 val[2] = NULL_TREE;
2228 else
2230 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2231 if (val[2] == NULL_TREE)
2232 sop = true;
2235 if (vr0->min == vr0->max || vr1->min == vr1->max)
2236 val[3] = NULL_TREE;
2237 else
2239 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2240 if (val[3] == NULL_TREE)
2241 sop = true;
2244 if (sop)
2246 set_value_range_to_varying (vr);
2247 return;
2250 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2251 of VAL[i]. */
2252 min = val[0];
2253 max = val[0];
2254 for (i = 1; i < 4; i++)
2256 if (!is_gimple_min_invariant (min)
2257 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2258 || !is_gimple_min_invariant (max)
2259 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2260 break;
2262 if (val[i])
2264 if (!is_gimple_min_invariant (val[i])
2265 || (TREE_OVERFLOW (val[i])
2266 && !is_overflow_infinity (val[i])))
2268 /* If we found an overflowed value, set MIN and MAX
2269 to it so that we set the resulting range to
2270 VARYING. */
2271 min = max = val[i];
2272 break;
2275 if (compare_values (val[i], min) == -1)
2276 min = val[i];
2278 if (compare_values (val[i], max) == 1)
2279 max = val[i];
2283 /* If either MIN or MAX overflowed, then set the resulting range to
2284 VARYING. But we do accept an overflow infinity
2285 representation. */
2286 if (min == NULL_TREE
2287 || !is_gimple_min_invariant (min)
2288 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2289 || max == NULL_TREE
2290 || !is_gimple_min_invariant (max)
2291 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2293 set_value_range_to_varying (vr);
2294 return;
2297 /* We punt if:
2298 1) [-INF, +INF]
2299 2) [-INF, +-INF(OVF)]
2300 3) [+-INF(OVF), +INF]
2301 4) [+-INF(OVF), +-INF(OVF)]
2302 We learn nothing when we have INF and INF(OVF) on both sides.
2303 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2304 overflow. */
2305 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2306 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2308 set_value_range_to_varying (vr);
2309 return;
2312 cmp = compare_values (min, max);
2313 if (cmp == -2 || cmp == 1)
2315 /* If the new range has its limits swapped around (MIN > MAX),
2316 then the operation caused one of them to wrap around, mark
2317 the new range VARYING. */
2318 set_value_range_to_varying (vr);
2320 else
2321 set_value_range (vr, type, min, max, NULL);
2324 /* Extract range information from a binary operation CODE based on
2325 the ranges of each of its operands *VR0 and *VR1 with resulting
2326 type EXPR_TYPE. The resulting range is stored in *VR. */
2328 static void
2329 extract_range_from_binary_expr_1 (value_range_t *vr,
2330 enum tree_code code, tree expr_type,
2331 value_range_t *vr0_, value_range_t *vr1_)
2333 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2334 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2335 enum value_range_type type;
2336 tree min = NULL_TREE, max = NULL_TREE;
2337 int cmp;
2339 if (!INTEGRAL_TYPE_P (expr_type)
2340 && !POINTER_TYPE_P (expr_type))
2342 set_value_range_to_varying (vr);
2343 return;
2346 /* Not all binary expressions can be applied to ranges in a
2347 meaningful way. Handle only arithmetic operations. */
2348 if (code != PLUS_EXPR
2349 && code != MINUS_EXPR
2350 && code != POINTER_PLUS_EXPR
2351 && code != MULT_EXPR
2352 && code != TRUNC_DIV_EXPR
2353 && code != FLOOR_DIV_EXPR
2354 && code != CEIL_DIV_EXPR
2355 && code != EXACT_DIV_EXPR
2356 && code != ROUND_DIV_EXPR
2357 && code != TRUNC_MOD_EXPR
2358 && code != RSHIFT_EXPR
2359 && code != LSHIFT_EXPR
2360 && code != MIN_EXPR
2361 && code != MAX_EXPR
2362 && code != BIT_AND_EXPR
2363 && code != BIT_IOR_EXPR
2364 && code != BIT_XOR_EXPR)
2366 set_value_range_to_varying (vr);
2367 return;
2370 /* If both ranges are UNDEFINED, so is the result. */
2371 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2373 set_value_range_to_undefined (vr);
2374 return;
2376 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2377 code. At some point we may want to special-case operations that
2378 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2379 operand. */
2380 else if (vr0.type == VR_UNDEFINED)
2381 set_value_range_to_varying (&vr0);
2382 else if (vr1.type == VR_UNDEFINED)
2383 set_value_range_to_varying (&vr1);
2385 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2386 and express ~[] op X as ([]' op X) U ([]'' op X). */
2387 if (vr0.type == VR_ANTI_RANGE
2388 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2390 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2391 if (vrtem1.type != VR_UNDEFINED)
2393 value_range_t vrres = VR_INITIALIZER;
2394 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2395 &vrtem1, vr1_);
2396 vrp_meet (vr, &vrres);
2398 return;
2400 /* Likewise for X op ~[]. */
2401 if (vr1.type == VR_ANTI_RANGE
2402 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2404 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2405 if (vrtem1.type != VR_UNDEFINED)
2407 value_range_t vrres = VR_INITIALIZER;
2408 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2409 vr0_, &vrtem1);
2410 vrp_meet (vr, &vrres);
2412 return;
2415 /* The type of the resulting value range defaults to VR0.TYPE. */
2416 type = vr0.type;
2418 /* Refuse to operate on VARYING ranges, ranges of different kinds
2419 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2420 because we may be able to derive a useful range even if one of
2421 the operands is VR_VARYING or symbolic range. Similarly for
2422 divisions, MIN/MAX and PLUS/MINUS.
2424 TODO, we may be able to derive anti-ranges in some cases. */
2425 if (code != BIT_AND_EXPR
2426 && code != BIT_IOR_EXPR
2427 && code != TRUNC_DIV_EXPR
2428 && code != FLOOR_DIV_EXPR
2429 && code != CEIL_DIV_EXPR
2430 && code != EXACT_DIV_EXPR
2431 && code != ROUND_DIV_EXPR
2432 && code != TRUNC_MOD_EXPR
2433 && code != MIN_EXPR
2434 && code != MAX_EXPR
2435 && code != PLUS_EXPR
2436 && code != MINUS_EXPR
2437 && (vr0.type == VR_VARYING
2438 || vr1.type == VR_VARYING
2439 || vr0.type != vr1.type
2440 || symbolic_range_p (&vr0)
2441 || symbolic_range_p (&vr1)))
2443 set_value_range_to_varying (vr);
2444 return;
2447 /* Now evaluate the expression to determine the new range. */
2448 if (POINTER_TYPE_P (expr_type))
2450 if (code == MIN_EXPR || code == MAX_EXPR)
2452 /* For MIN/MAX expressions with pointers, we only care about
2453 nullness, if both are non null, then the result is nonnull.
2454 If both are null, then the result is null. Otherwise they
2455 are varying. */
2456 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2457 set_value_range_to_nonnull (vr, expr_type);
2458 else if (range_is_null (&vr0) && range_is_null (&vr1))
2459 set_value_range_to_null (vr, expr_type);
2460 else
2461 set_value_range_to_varying (vr);
2463 else if (code == POINTER_PLUS_EXPR)
2465 /* For pointer types, we are really only interested in asserting
2466 whether the expression evaluates to non-NULL. */
2467 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2468 set_value_range_to_nonnull (vr, expr_type);
2469 else if (range_is_null (&vr0) && range_is_null (&vr1))
2470 set_value_range_to_null (vr, expr_type);
2471 else
2472 set_value_range_to_varying (vr);
2474 else if (code == BIT_AND_EXPR)
2476 /* For pointer types, we are really only interested in asserting
2477 whether the expression evaluates to non-NULL. */
2478 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2479 set_value_range_to_nonnull (vr, expr_type);
2480 else if (range_is_null (&vr0) || range_is_null (&vr1))
2481 set_value_range_to_null (vr, expr_type);
2482 else
2483 set_value_range_to_varying (vr);
2485 else
2486 set_value_range_to_varying (vr);
2488 return;
2491 /* For integer ranges, apply the operation to each end of the
2492 range and see what we end up with. */
2493 if (code == PLUS_EXPR || code == MINUS_EXPR)
2495 const bool minus_p = (code == MINUS_EXPR);
2496 tree min_op0 = vr0.min;
2497 tree min_op1 = minus_p ? vr1.max : vr1.min;
2498 tree max_op0 = vr0.max;
2499 tree max_op1 = minus_p ? vr1.min : vr1.max;
2500 tree sym_min_op0 = NULL_TREE;
2501 tree sym_min_op1 = NULL_TREE;
2502 tree sym_max_op0 = NULL_TREE;
2503 tree sym_max_op1 = NULL_TREE;
2504 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2506 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2507 single-symbolic ranges, try to compute the precise resulting range,
2508 but only if we know that this resulting range will also be constant
2509 or single-symbolic. */
2510 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2511 && (TREE_CODE (min_op0) == INTEGER_CST
2512 || (sym_min_op0
2513 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2514 && (TREE_CODE (min_op1) == INTEGER_CST
2515 || (sym_min_op1
2516 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2517 && (!(sym_min_op0 && sym_min_op1)
2518 || (sym_min_op0 == sym_min_op1
2519 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2520 && (TREE_CODE (max_op0) == INTEGER_CST
2521 || (sym_max_op0
2522 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2523 && (TREE_CODE (max_op1) == INTEGER_CST
2524 || (sym_max_op1
2525 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2526 && (!(sym_max_op0 && sym_max_op1)
2527 || (sym_max_op0 == sym_max_op1
2528 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2530 const signop sgn = TYPE_SIGN (expr_type);
2531 const unsigned int prec = TYPE_PRECISION (expr_type);
2532 wide_int type_min, type_max, wmin, wmax;
2533 int min_ovf = 0;
2534 int max_ovf = 0;
2536 /* Get the lower and upper bounds of the type. */
2537 if (TYPE_OVERFLOW_WRAPS (expr_type))
2539 type_min = wi::min_value (prec, sgn);
2540 type_max = wi::max_value (prec, sgn);
2542 else
2544 type_min = vrp_val_min (expr_type);
2545 type_max = vrp_val_max (expr_type);
2548 /* Combine the lower bounds, if any. */
2549 if (min_op0 && min_op1)
2551 if (minus_p)
2553 wmin = wi::sub (min_op0, min_op1);
2555 /* Check for overflow. */
2556 if (wi::cmp (0, min_op1, sgn)
2557 != wi::cmp (wmin, min_op0, sgn))
2558 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2560 else
2562 wmin = wi::add (min_op0, min_op1);
2564 /* Check for overflow. */
2565 if (wi::cmp (min_op1, 0, sgn)
2566 != wi::cmp (wmin, min_op0, sgn))
2567 min_ovf = wi::cmp (min_op0, wmin, sgn);
2570 else if (min_op0)
2571 wmin = min_op0;
2572 else if (min_op1)
2573 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2574 else
2575 wmin = wi::shwi (0, prec);
2577 /* Combine the upper bounds, if any. */
2578 if (max_op0 && max_op1)
2580 if (minus_p)
2582 wmax = wi::sub (max_op0, max_op1);
2584 /* Check for overflow. */
2585 if (wi::cmp (0, max_op1, sgn)
2586 != wi::cmp (wmax, max_op0, sgn))
2587 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2589 else
2591 wmax = wi::add (max_op0, max_op1);
2593 if (wi::cmp (max_op1, 0, sgn)
2594 != wi::cmp (wmax, max_op0, sgn))
2595 max_ovf = wi::cmp (max_op0, wmax, sgn);
2598 else if (max_op0)
2599 wmax = max_op0;
2600 else if (max_op1)
2601 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2602 else
2603 wmax = wi::shwi (0, prec);
2605 /* Check for type overflow. */
2606 if (min_ovf == 0)
2608 if (wi::cmp (wmin, type_min, sgn) == -1)
2609 min_ovf = -1;
2610 else if (wi::cmp (wmin, type_max, sgn) == 1)
2611 min_ovf = 1;
2613 if (max_ovf == 0)
2615 if (wi::cmp (wmax, type_min, sgn) == -1)
2616 max_ovf = -1;
2617 else if (wi::cmp (wmax, type_max, sgn) == 1)
2618 max_ovf = 1;
2621 /* If we have overflow for the constant part and the resulting
2622 range will be symbolic, drop to VR_VARYING. */
2623 if ((min_ovf && sym_min_op0 != sym_min_op1)
2624 || (max_ovf && sym_max_op0 != sym_max_op1))
2626 set_value_range_to_varying (vr);
2627 return;
2630 if (TYPE_OVERFLOW_WRAPS (expr_type))
2632 /* If overflow wraps, truncate the values and adjust the
2633 range kind and bounds appropriately. */
2634 wide_int tmin = wide_int::from (wmin, prec, sgn);
2635 wide_int tmax = wide_int::from (wmax, prec, sgn);
2636 if (min_ovf == max_ovf)
2638 /* No overflow or both overflow or underflow. The
2639 range kind stays VR_RANGE. */
2640 min = wide_int_to_tree (expr_type, tmin);
2641 max = wide_int_to_tree (expr_type, tmax);
2643 else if (min_ovf == -1 && max_ovf == 1)
2645 /* Underflow and overflow, drop to VR_VARYING. */
2646 set_value_range_to_varying (vr);
2647 return;
2649 else
2651 /* Min underflow or max overflow. The range kind
2652 changes to VR_ANTI_RANGE. */
2653 bool covers = false;
2654 wide_int tem = tmin;
2655 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2656 || (max_ovf == 1 && min_ovf == 0));
2657 type = VR_ANTI_RANGE;
2658 tmin = tmax + 1;
2659 if (wi::cmp (tmin, tmax, sgn) < 0)
2660 covers = true;
2661 tmax = tem - 1;
2662 if (wi::cmp (tmax, tem, sgn) > 0)
2663 covers = true;
2664 /* If the anti-range would cover nothing, drop to varying.
2665 Likewise if the anti-range bounds are outside of the
2666 types values. */
2667 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2669 set_value_range_to_varying (vr);
2670 return;
2672 min = wide_int_to_tree (expr_type, tmin);
2673 max = wide_int_to_tree (expr_type, tmax);
2676 else
2678 /* If overflow does not wrap, saturate to the types min/max
2679 value. */
2680 if (min_ovf == -1)
2682 if (needs_overflow_infinity (expr_type)
2683 && supports_overflow_infinity (expr_type))
2684 min = negative_overflow_infinity (expr_type);
2685 else
2686 min = wide_int_to_tree (expr_type, type_min);
2688 else if (min_ovf == 1)
2690 if (needs_overflow_infinity (expr_type)
2691 && supports_overflow_infinity (expr_type))
2692 min = positive_overflow_infinity (expr_type);
2693 else
2694 min = wide_int_to_tree (expr_type, type_max);
2696 else
2697 min = wide_int_to_tree (expr_type, wmin);
2699 if (max_ovf == -1)
2701 if (needs_overflow_infinity (expr_type)
2702 && supports_overflow_infinity (expr_type))
2703 max = negative_overflow_infinity (expr_type);
2704 else
2705 max = wide_int_to_tree (expr_type, type_min);
2707 else if (max_ovf == 1)
2709 if (needs_overflow_infinity (expr_type)
2710 && supports_overflow_infinity (expr_type))
2711 max = positive_overflow_infinity (expr_type);
2712 else
2713 max = wide_int_to_tree (expr_type, type_max);
2715 else
2716 max = wide_int_to_tree (expr_type, wmax);
2719 if (needs_overflow_infinity (expr_type)
2720 && supports_overflow_infinity (expr_type))
2722 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2723 || (min_op1
2724 && (minus_p
2725 ? is_positive_overflow_infinity (min_op1)
2726 : is_negative_overflow_infinity (min_op1))))
2727 min = negative_overflow_infinity (expr_type);
2728 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2729 || (max_op1
2730 && (minus_p
2731 ? is_negative_overflow_infinity (max_op1)
2732 : is_positive_overflow_infinity (max_op1))))
2733 max = positive_overflow_infinity (expr_type);
2736 /* If the result lower bound is constant, we're done;
2737 otherwise, build the symbolic lower bound. */
2738 if (sym_min_op0 == sym_min_op1)
2740 else if (sym_min_op0)
2741 min = build_symbolic_expr (expr_type, sym_min_op0,
2742 neg_min_op0, min);
2743 else if (sym_min_op1)
2744 min = build_symbolic_expr (expr_type, sym_min_op1,
2745 neg_min_op1 ^ minus_p, min);
2747 /* Likewise for the upper bound. */
2748 if (sym_max_op0 == sym_max_op1)
2750 else if (sym_max_op0)
2751 max = build_symbolic_expr (expr_type, sym_max_op0,
2752 neg_max_op0, max);
2753 else if (sym_max_op1)
2754 max = build_symbolic_expr (expr_type, sym_max_op1,
2755 neg_max_op1 ^ minus_p, max);
2757 else
2759 /* For other cases, for example if we have a PLUS_EXPR with two
2760 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2761 to compute a precise range for such a case.
2762 ??? General even mixed range kind operations can be expressed
2763 by for example transforming ~[3, 5] + [1, 2] to range-only
2764 operations and a union primitive:
2765 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2766 [-INF+1, 4] U [6, +INF(OVF)]
2767 though usually the union is not exactly representable with
2768 a single range or anti-range as the above is
2769 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2770 but one could use a scheme similar to equivalences for this. */
2771 set_value_range_to_varying (vr);
2772 return;
2775 else if (code == MIN_EXPR
2776 || code == MAX_EXPR)
2778 if (vr0.type == VR_RANGE
2779 && !symbolic_range_p (&vr0))
2781 type = VR_RANGE;
2782 if (vr1.type == VR_RANGE
2783 && !symbolic_range_p (&vr1))
2785 /* For operations that make the resulting range directly
2786 proportional to the original ranges, apply the operation to
2787 the same end of each range. */
2788 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2789 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2791 else if (code == MIN_EXPR)
2793 min = vrp_val_min (expr_type);
2794 max = vr0.max;
2796 else if (code == MAX_EXPR)
2798 min = vr0.min;
2799 max = vrp_val_max (expr_type);
2802 else if (vr1.type == VR_RANGE
2803 && !symbolic_range_p (&vr1))
2805 type = VR_RANGE;
2806 if (code == MIN_EXPR)
2808 min = vrp_val_min (expr_type);
2809 max = vr1.max;
2811 else if (code == MAX_EXPR)
2813 min = vr1.min;
2814 max = vrp_val_max (expr_type);
2817 else
2819 set_value_range_to_varying (vr);
2820 return;
2823 else if (code == MULT_EXPR)
2825 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2826 drop to varying. This test requires 2*prec bits if both
2827 operands are signed and 2*prec + 2 bits if either is not. */
2829 signop sign = TYPE_SIGN (expr_type);
2830 unsigned int prec = TYPE_PRECISION (expr_type);
2832 if (range_int_cst_p (&vr0)
2833 && range_int_cst_p (&vr1)
2834 && TYPE_OVERFLOW_WRAPS (expr_type))
2836 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2837 typedef generic_wide_int
2838 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2839 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2840 vrp_int size = sizem1 + 1;
2842 /* Extend the values using the sign of the result to PREC2.
2843 From here on out, everthing is just signed math no matter
2844 what the input types were. */
2845 vrp_int min0 = vrp_int_cst (vr0.min);
2846 vrp_int max0 = vrp_int_cst (vr0.max);
2847 vrp_int min1 = vrp_int_cst (vr1.min);
2848 vrp_int max1 = vrp_int_cst (vr1.max);
2849 /* Canonicalize the intervals. */
2850 if (sign == UNSIGNED)
2852 if (wi::ltu_p (size, min0 + max0))
2854 min0 -= size;
2855 max0 -= size;
2858 if (wi::ltu_p (size, min1 + max1))
2860 min1 -= size;
2861 max1 -= size;
2865 vrp_int prod0 = min0 * min1;
2866 vrp_int prod1 = min0 * max1;
2867 vrp_int prod2 = max0 * min1;
2868 vrp_int prod3 = max0 * max1;
2870 /* Sort the 4 products so that min is in prod0 and max is in
2871 prod3. */
2872 /* min0min1 > max0max1 */
2873 if (wi::gts_p (prod0, prod3))
2875 vrp_int tmp = prod3;
2876 prod3 = prod0;
2877 prod0 = tmp;
2880 /* min0max1 > max0min1 */
2881 if (wi::gts_p (prod1, prod2))
2883 vrp_int tmp = prod2;
2884 prod2 = prod1;
2885 prod1 = tmp;
2888 if (wi::gts_p (prod0, prod1))
2890 vrp_int tmp = prod1;
2891 prod1 = prod0;
2892 prod0 = tmp;
2895 if (wi::gts_p (prod2, prod3))
2897 vrp_int tmp = prod3;
2898 prod3 = prod2;
2899 prod2 = tmp;
2902 /* diff = max - min. */
2903 prod2 = prod3 - prod0;
2904 if (wi::geu_p (prod2, sizem1))
2906 /* the range covers all values. */
2907 set_value_range_to_varying (vr);
2908 return;
2911 /* The following should handle the wrapping and selecting
2912 VR_ANTI_RANGE for us. */
2913 min = wide_int_to_tree (expr_type, prod0);
2914 max = wide_int_to_tree (expr_type, prod3);
2915 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2916 return;
2919 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2920 drop to VR_VARYING. It would take more effort to compute a
2921 precise range for such a case. For example, if we have
2922 op0 == 65536 and op1 == 65536 with their ranges both being
2923 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2924 we cannot claim that the product is in ~[0,0]. Note that we
2925 are guaranteed to have vr0.type == vr1.type at this
2926 point. */
2927 if (vr0.type == VR_ANTI_RANGE
2928 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2930 set_value_range_to_varying (vr);
2931 return;
2934 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2935 return;
2937 else if (code == RSHIFT_EXPR
2938 || code == LSHIFT_EXPR)
2940 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2941 then drop to VR_VARYING. Outside of this range we get undefined
2942 behavior from the shift operation. We cannot even trust
2943 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2944 shifts, and the operation at the tree level may be widened. */
2945 if (range_int_cst_p (&vr1)
2946 && compare_tree_int (vr1.min, 0) >= 0
2947 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2949 if (code == RSHIFT_EXPR)
2951 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2952 return;
2954 /* We can map lshifts by constants to MULT_EXPR handling. */
2955 else if (code == LSHIFT_EXPR
2956 && range_int_cst_singleton_p (&vr1))
2958 bool saved_flag_wrapv;
2959 value_range_t vr1p = VR_INITIALIZER;
2960 vr1p.type = VR_RANGE;
2961 vr1p.min = (wide_int_to_tree
2962 (expr_type,
2963 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2964 TYPE_PRECISION (expr_type))));
2965 vr1p.max = vr1p.min;
2966 /* We have to use a wrapping multiply though as signed overflow
2967 on lshifts is implementation defined in C89. */
2968 saved_flag_wrapv = flag_wrapv;
2969 flag_wrapv = 1;
2970 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2971 &vr0, &vr1p);
2972 flag_wrapv = saved_flag_wrapv;
2973 return;
2975 else if (code == LSHIFT_EXPR
2976 && range_int_cst_p (&vr0))
2978 int prec = TYPE_PRECISION (expr_type);
2979 int overflow_pos = prec;
2980 int bound_shift;
2981 wide_int low_bound, high_bound;
2982 bool uns = TYPE_UNSIGNED (expr_type);
2983 bool in_bounds = false;
2985 if (!uns)
2986 overflow_pos -= 1;
2988 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2989 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2990 overflow. However, for that to happen, vr1.max needs to be
2991 zero, which means vr1 is a singleton range of zero, which
2992 means it should be handled by the previous LSHIFT_EXPR
2993 if-clause. */
2994 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2995 wide_int complement = ~(bound - 1);
2997 if (uns)
2999 low_bound = bound;
3000 high_bound = complement;
3001 if (wi::ltu_p (vr0.max, low_bound))
3003 /* [5, 6] << [1, 2] == [10, 24]. */
3004 /* We're shifting out only zeroes, the value increases
3005 monotonically. */
3006 in_bounds = true;
3008 else if (wi::ltu_p (high_bound, vr0.min))
3010 /* [0xffffff00, 0xffffffff] << [1, 2]
3011 == [0xfffffc00, 0xfffffffe]. */
3012 /* We're shifting out only ones, the value decreases
3013 monotonically. */
3014 in_bounds = true;
3017 else
3019 /* [-1, 1] << [1, 2] == [-4, 4]. */
3020 low_bound = complement;
3021 high_bound = bound;
3022 if (wi::lts_p (vr0.max, high_bound)
3023 && wi::lts_p (low_bound, vr0.min))
3025 /* For non-negative numbers, we're shifting out only
3026 zeroes, the value increases monotonically.
3027 For negative numbers, we're shifting out only ones, the
3028 value decreases monotomically. */
3029 in_bounds = true;
3033 if (in_bounds)
3035 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3036 return;
3040 set_value_range_to_varying (vr);
3041 return;
3043 else if (code == TRUNC_DIV_EXPR
3044 || code == FLOOR_DIV_EXPR
3045 || code == CEIL_DIV_EXPR
3046 || code == EXACT_DIV_EXPR
3047 || code == ROUND_DIV_EXPR)
3049 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3051 /* For division, if op1 has VR_RANGE but op0 does not, something
3052 can be deduced just from that range. Say [min, max] / [4, max]
3053 gives [min / 4, max / 4] range. */
3054 if (vr1.type == VR_RANGE
3055 && !symbolic_range_p (&vr1)
3056 && range_includes_zero_p (vr1.min, vr1.max) == 0)
3058 vr0.type = type = VR_RANGE;
3059 vr0.min = vrp_val_min (expr_type);
3060 vr0.max = vrp_val_max (expr_type);
3062 else
3064 set_value_range_to_varying (vr);
3065 return;
3069 /* For divisions, if flag_non_call_exceptions is true, we must
3070 not eliminate a division by zero. */
3071 if (cfun->can_throw_non_call_exceptions
3072 && (vr1.type != VR_RANGE
3073 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3075 set_value_range_to_varying (vr);
3076 return;
3079 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3080 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3081 include 0. */
3082 if (vr0.type == VR_RANGE
3083 && (vr1.type != VR_RANGE
3084 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3086 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
3087 int cmp;
3089 min = NULL_TREE;
3090 max = NULL_TREE;
3091 if (TYPE_UNSIGNED (expr_type)
3092 || value_range_nonnegative_p (&vr1))
3094 /* For unsigned division or when divisor is known
3095 to be non-negative, the range has to cover
3096 all numbers from 0 to max for positive max
3097 and all numbers from min to 0 for negative min. */
3098 cmp = compare_values (vr0.max, zero);
3099 if (cmp == -1)
3100 max = zero;
3101 else if (cmp == 0 || cmp == 1)
3102 max = vr0.max;
3103 else
3104 type = VR_VARYING;
3105 cmp = compare_values (vr0.min, zero);
3106 if (cmp == 1)
3107 min = zero;
3108 else if (cmp == 0 || cmp == -1)
3109 min = vr0.min;
3110 else
3111 type = VR_VARYING;
3113 else
3115 /* Otherwise the range is -max .. max or min .. -min
3116 depending on which bound is bigger in absolute value,
3117 as the division can change the sign. */
3118 abs_extent_range (vr, vr0.min, vr0.max);
3119 return;
3121 if (type == VR_VARYING)
3123 set_value_range_to_varying (vr);
3124 return;
3127 else
3129 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3130 return;
3133 else if (code == TRUNC_MOD_EXPR)
3135 if (vr1.type != VR_RANGE
3136 || range_includes_zero_p (vr1.min, vr1.max) != 0
3137 || vrp_val_is_min (vr1.min))
3139 set_value_range_to_varying (vr);
3140 return;
3142 type = VR_RANGE;
3143 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3144 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
3145 if (tree_int_cst_lt (max, vr1.max))
3146 max = vr1.max;
3147 max = int_const_binop (MINUS_EXPR, max, build_int_cst (TREE_TYPE (max), 1));
3148 /* If the dividend is non-negative the modulus will be
3149 non-negative as well. */
3150 if (TYPE_UNSIGNED (expr_type)
3151 || value_range_nonnegative_p (&vr0))
3152 min = build_int_cst (TREE_TYPE (max), 0);
3153 else
3154 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
3156 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3158 bool int_cst_range0, int_cst_range1;
3159 wide_int may_be_nonzero0, may_be_nonzero1;
3160 wide_int must_be_nonzero0, must_be_nonzero1;
3162 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3163 &may_be_nonzero0,
3164 &must_be_nonzero0);
3165 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3166 &may_be_nonzero1,
3167 &must_be_nonzero1);
3169 type = VR_RANGE;
3170 if (code == BIT_AND_EXPR)
3172 min = wide_int_to_tree (expr_type,
3173 must_be_nonzero0 & must_be_nonzero1);
3174 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3175 /* If both input ranges contain only negative values we can
3176 truncate the result range maximum to the minimum of the
3177 input range maxima. */
3178 if (int_cst_range0 && int_cst_range1
3179 && tree_int_cst_sgn (vr0.max) < 0
3180 && tree_int_cst_sgn (vr1.max) < 0)
3182 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3183 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3185 /* If either input range contains only non-negative values
3186 we can truncate the result range maximum to the respective
3187 maximum of the input range. */
3188 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3189 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3190 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3191 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3192 max = wide_int_to_tree (expr_type, wmax);
3194 else if (code == BIT_IOR_EXPR)
3196 max = wide_int_to_tree (expr_type,
3197 may_be_nonzero0 | may_be_nonzero1);
3198 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3199 /* If the input ranges contain only positive values we can
3200 truncate the minimum of the result range to the maximum
3201 of the input range minima. */
3202 if (int_cst_range0 && int_cst_range1
3203 && tree_int_cst_sgn (vr0.min) >= 0
3204 && tree_int_cst_sgn (vr1.min) >= 0)
3206 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3207 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3209 /* If either input range contains only negative values
3210 we can truncate the minimum of the result range to the
3211 respective minimum range. */
3212 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3213 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3214 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3215 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3216 min = wide_int_to_tree (expr_type, wmin);
3218 else if (code == BIT_XOR_EXPR)
3220 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3221 | ~(may_be_nonzero0 | may_be_nonzero1));
3222 wide_int result_one_bits
3223 = (must_be_nonzero0.and_not (may_be_nonzero1)
3224 | must_be_nonzero1.and_not (may_be_nonzero0));
3225 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3226 min = wide_int_to_tree (expr_type, result_one_bits);
3227 /* If the range has all positive or all negative values the
3228 result is better than VARYING. */
3229 if (tree_int_cst_sgn (min) < 0
3230 || tree_int_cst_sgn (max) >= 0)
3232 else
3233 max = min = NULL_TREE;
3236 else
3237 gcc_unreachable ();
3239 /* If either MIN or MAX overflowed, then set the resulting range to
3240 VARYING. But we do accept an overflow infinity representation. */
3241 if (min == NULL_TREE
3242 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3243 || max == NULL_TREE
3244 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3246 set_value_range_to_varying (vr);
3247 return;
3250 /* We punt if:
3251 1) [-INF, +INF]
3252 2) [-INF, +-INF(OVF)]
3253 3) [+-INF(OVF), +INF]
3254 4) [+-INF(OVF), +-INF(OVF)]
3255 We learn nothing when we have INF and INF(OVF) on both sides.
3256 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3257 overflow. */
3258 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3259 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3261 set_value_range_to_varying (vr);
3262 return;
3265 cmp = compare_values (min, max);
3266 if (cmp == -2 || cmp == 1)
3268 /* If the new range has its limits swapped around (MIN > MAX),
3269 then the operation caused one of them to wrap around, mark
3270 the new range VARYING. */
3271 set_value_range_to_varying (vr);
3273 else
3274 set_value_range (vr, type, min, max, NULL);
3277 /* Extract range information from a binary expression OP0 CODE OP1 based on
3278 the ranges of each of its operands with resulting type EXPR_TYPE.
3279 The resulting range is stored in *VR. */
3281 static void
3282 extract_range_from_binary_expr (value_range_t *vr,
3283 enum tree_code code,
3284 tree expr_type, tree op0, tree op1)
3286 value_range_t vr0 = VR_INITIALIZER;
3287 value_range_t vr1 = VR_INITIALIZER;
3289 /* Get value ranges for each operand. For constant operands, create
3290 a new value range with the operand to simplify processing. */
3291 if (TREE_CODE (op0) == SSA_NAME)
3292 vr0 = *(get_value_range (op0));
3293 else if (is_gimple_min_invariant (op0))
3294 set_value_range_to_value (&vr0, op0, NULL);
3295 else
3296 set_value_range_to_varying (&vr0);
3298 if (TREE_CODE (op1) == SSA_NAME)
3299 vr1 = *(get_value_range (op1));
3300 else if (is_gimple_min_invariant (op1))
3301 set_value_range_to_value (&vr1, op1, NULL);
3302 else
3303 set_value_range_to_varying (&vr1);
3305 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3307 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3308 and based on the other operand, for example if it was deduced from a
3309 symbolic comparison. When a bound of the range of the first operand
3310 is invariant, we set the corresponding bound of the new range to INF
3311 in order to avoid recursing on the range of the second operand. */
3312 if (vr->type == VR_VARYING
3313 && (code == PLUS_EXPR || code == MINUS_EXPR)
3314 && TREE_CODE (op1) == SSA_NAME
3315 && vr0.type == VR_RANGE
3316 && symbolic_range_based_on_p (&vr0, op1))
3318 const bool minus_p = (code == MINUS_EXPR);
3319 value_range_t n_vr1 = VR_INITIALIZER;
3321 /* Try with VR0 and [-INF, OP1]. */
3322 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3323 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3325 /* Try with VR0 and [OP1, +INF]. */
3326 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3327 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3329 /* Try with VR0 and [OP1, OP1]. */
3330 else
3331 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3333 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3336 if (vr->type == VR_VARYING
3337 && (code == PLUS_EXPR || code == MINUS_EXPR)
3338 && TREE_CODE (op0) == SSA_NAME
3339 && vr1.type == VR_RANGE
3340 && symbolic_range_based_on_p (&vr1, op0))
3342 const bool minus_p = (code == MINUS_EXPR);
3343 value_range_t n_vr0 = VR_INITIALIZER;
3345 /* Try with [-INF, OP0] and VR1. */
3346 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3347 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3349 /* Try with [OP0, +INF] and VR1. */
3350 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3351 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3353 /* Try with [OP0, OP0] and VR1. */
3354 else
3355 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3357 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3361 /* Extract range information from a unary operation CODE based on
3362 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3363 The The resulting range is stored in *VR. */
3365 static void
3366 extract_range_from_unary_expr_1 (value_range_t *vr,
3367 enum tree_code code, tree type,
3368 value_range_t *vr0_, tree op0_type)
3370 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3372 /* VRP only operates on integral and pointer types. */
3373 if (!(INTEGRAL_TYPE_P (op0_type)
3374 || POINTER_TYPE_P (op0_type))
3375 || !(INTEGRAL_TYPE_P (type)
3376 || POINTER_TYPE_P (type)))
3378 set_value_range_to_varying (vr);
3379 return;
3382 /* If VR0 is UNDEFINED, so is the result. */
3383 if (vr0.type == VR_UNDEFINED)
3385 set_value_range_to_undefined (vr);
3386 return;
3389 /* Handle operations that we express in terms of others. */
3390 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3392 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3393 copy_value_range (vr, &vr0);
3394 return;
3396 else if (code == NEGATE_EXPR)
3398 /* -X is simply 0 - X, so re-use existing code that also handles
3399 anti-ranges fine. */
3400 value_range_t zero = VR_INITIALIZER;
3401 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3402 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3403 return;
3405 else if (code == BIT_NOT_EXPR)
3407 /* ~X is simply -1 - X, so re-use existing code that also handles
3408 anti-ranges fine. */
3409 value_range_t minusone = VR_INITIALIZER;
3410 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3411 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3412 type, &minusone, &vr0);
3413 return;
3416 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3417 and express op ~[] as (op []') U (op []''). */
3418 if (vr0.type == VR_ANTI_RANGE
3419 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3421 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3422 if (vrtem1.type != VR_UNDEFINED)
3424 value_range_t vrres = VR_INITIALIZER;
3425 extract_range_from_unary_expr_1 (&vrres, code, type,
3426 &vrtem1, op0_type);
3427 vrp_meet (vr, &vrres);
3429 return;
3432 if (CONVERT_EXPR_CODE_P (code))
3434 tree inner_type = op0_type;
3435 tree outer_type = type;
3437 /* If the expression evaluates to a pointer, we are only interested in
3438 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3439 if (POINTER_TYPE_P (type))
3441 if (range_is_nonnull (&vr0))
3442 set_value_range_to_nonnull (vr, type);
3443 else if (range_is_null (&vr0))
3444 set_value_range_to_null (vr, type);
3445 else
3446 set_value_range_to_varying (vr);
3447 return;
3450 /* If VR0 is varying and we increase the type precision, assume
3451 a full range for the following transformation. */
3452 if (vr0.type == VR_VARYING
3453 && INTEGRAL_TYPE_P (inner_type)
3454 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3456 vr0.type = VR_RANGE;
3457 vr0.min = TYPE_MIN_VALUE (inner_type);
3458 vr0.max = TYPE_MAX_VALUE (inner_type);
3461 /* If VR0 is a constant range or anti-range and the conversion is
3462 not truncating we can convert the min and max values and
3463 canonicalize the resulting range. Otherwise we can do the
3464 conversion if the size of the range is less than what the
3465 precision of the target type can represent and the range is
3466 not an anti-range. */
3467 if ((vr0.type == VR_RANGE
3468 || vr0.type == VR_ANTI_RANGE)
3469 && TREE_CODE (vr0.min) == INTEGER_CST
3470 && TREE_CODE (vr0.max) == INTEGER_CST
3471 && (!is_overflow_infinity (vr0.min)
3472 || (vr0.type == VR_RANGE
3473 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3474 && needs_overflow_infinity (outer_type)
3475 && supports_overflow_infinity (outer_type)))
3476 && (!is_overflow_infinity (vr0.max)
3477 || (vr0.type == VR_RANGE
3478 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3479 && needs_overflow_infinity (outer_type)
3480 && supports_overflow_infinity (outer_type)))
3481 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3482 || (vr0.type == VR_RANGE
3483 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3484 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3485 size_int (TYPE_PRECISION (outer_type)))))))
3487 tree new_min, new_max;
3488 if (is_overflow_infinity (vr0.min))
3489 new_min = negative_overflow_infinity (outer_type);
3490 else
3491 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3492 0, false);
3493 if (is_overflow_infinity (vr0.max))
3494 new_max = positive_overflow_infinity (outer_type);
3495 else
3496 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3497 0, false);
3498 set_and_canonicalize_value_range (vr, vr0.type,
3499 new_min, new_max, NULL);
3500 return;
3503 set_value_range_to_varying (vr);
3504 return;
3506 else if (code == ABS_EXPR)
3508 tree min, max;
3509 int cmp;
3511 /* Pass through vr0 in the easy cases. */
3512 if (TYPE_UNSIGNED (type)
3513 || value_range_nonnegative_p (&vr0))
3515 copy_value_range (vr, &vr0);
3516 return;
3519 /* For the remaining varying or symbolic ranges we can't do anything
3520 useful. */
3521 if (vr0.type == VR_VARYING
3522 || symbolic_range_p (&vr0))
3524 set_value_range_to_varying (vr);
3525 return;
3528 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3529 useful range. */
3530 if (!TYPE_OVERFLOW_UNDEFINED (type)
3531 && ((vr0.type == VR_RANGE
3532 && vrp_val_is_min (vr0.min))
3533 || (vr0.type == VR_ANTI_RANGE
3534 && !vrp_val_is_min (vr0.min))))
3536 set_value_range_to_varying (vr);
3537 return;
3540 /* ABS_EXPR may flip the range around, if the original range
3541 included negative values. */
3542 if (is_overflow_infinity (vr0.min))
3543 min = positive_overflow_infinity (type);
3544 else if (!vrp_val_is_min (vr0.min))
3545 min = fold_unary_to_constant (code, type, vr0.min);
3546 else if (!needs_overflow_infinity (type))
3547 min = TYPE_MAX_VALUE (type);
3548 else if (supports_overflow_infinity (type))
3549 min = positive_overflow_infinity (type);
3550 else
3552 set_value_range_to_varying (vr);
3553 return;
3556 if (is_overflow_infinity (vr0.max))
3557 max = positive_overflow_infinity (type);
3558 else if (!vrp_val_is_min (vr0.max))
3559 max = fold_unary_to_constant (code, type, vr0.max);
3560 else if (!needs_overflow_infinity (type))
3561 max = TYPE_MAX_VALUE (type);
3562 else if (supports_overflow_infinity (type)
3563 /* We shouldn't generate [+INF, +INF] as set_value_range
3564 doesn't like this and ICEs. */
3565 && !is_positive_overflow_infinity (min))
3566 max = positive_overflow_infinity (type);
3567 else
3569 set_value_range_to_varying (vr);
3570 return;
3573 cmp = compare_values (min, max);
3575 /* If a VR_ANTI_RANGEs contains zero, then we have
3576 ~[-INF, min(MIN, MAX)]. */
3577 if (vr0.type == VR_ANTI_RANGE)
3579 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3581 /* Take the lower of the two values. */
3582 if (cmp != 1)
3583 max = min;
3585 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3586 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3587 flag_wrapv is set and the original anti-range doesn't include
3588 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3589 if (TYPE_OVERFLOW_WRAPS (type))
3591 tree type_min_value = TYPE_MIN_VALUE (type);
3593 min = (vr0.min != type_min_value
3594 ? int_const_binop (PLUS_EXPR, type_min_value,
3595 build_int_cst (TREE_TYPE (type_min_value), 1))
3596 : type_min_value);
3598 else
3600 if (overflow_infinity_range_p (&vr0))
3601 min = negative_overflow_infinity (type);
3602 else
3603 min = TYPE_MIN_VALUE (type);
3606 else
3608 /* All else has failed, so create the range [0, INF], even for
3609 flag_wrapv since TYPE_MIN_VALUE is in the original
3610 anti-range. */
3611 vr0.type = VR_RANGE;
3612 min = build_int_cst (type, 0);
3613 if (needs_overflow_infinity (type))
3615 if (supports_overflow_infinity (type))
3616 max = positive_overflow_infinity (type);
3617 else
3619 set_value_range_to_varying (vr);
3620 return;
3623 else
3624 max = TYPE_MAX_VALUE (type);
3628 /* If the range contains zero then we know that the minimum value in the
3629 range will be zero. */
3630 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3632 if (cmp == 1)
3633 max = min;
3634 min = build_int_cst (type, 0);
3636 else
3638 /* If the range was reversed, swap MIN and MAX. */
3639 if (cmp == 1)
3641 tree t = min;
3642 min = max;
3643 max = t;
3647 cmp = compare_values (min, max);
3648 if (cmp == -2 || cmp == 1)
3650 /* If the new range has its limits swapped around (MIN > MAX),
3651 then the operation caused one of them to wrap around, mark
3652 the new range VARYING. */
3653 set_value_range_to_varying (vr);
3655 else
3656 set_value_range (vr, vr0.type, min, max, NULL);
3657 return;
3660 /* For unhandled operations fall back to varying. */
3661 set_value_range_to_varying (vr);
3662 return;
3666 /* Extract range information from a unary expression CODE OP0 based on
3667 the range of its operand with resulting type TYPE.
3668 The resulting range is stored in *VR. */
3670 static void
3671 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3672 tree type, tree op0)
3674 value_range_t vr0 = VR_INITIALIZER;
3676 /* Get value ranges for the operand. For constant operands, create
3677 a new value range with the operand to simplify processing. */
3678 if (TREE_CODE (op0) == SSA_NAME)
3679 vr0 = *(get_value_range (op0));
3680 else if (is_gimple_min_invariant (op0))
3681 set_value_range_to_value (&vr0, op0, NULL);
3682 else
3683 set_value_range_to_varying (&vr0);
3685 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3689 /* Extract range information from a conditional expression STMT based on
3690 the ranges of each of its operands and the expression code. */
3692 static void
3693 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3695 tree op0, op1;
3696 value_range_t vr0 = VR_INITIALIZER;
3697 value_range_t vr1 = VR_INITIALIZER;
3699 /* Get value ranges for each operand. For constant operands, create
3700 a new value range with the operand to simplify processing. */
3701 op0 = gimple_assign_rhs2 (stmt);
3702 if (TREE_CODE (op0) == SSA_NAME)
3703 vr0 = *(get_value_range (op0));
3704 else if (is_gimple_min_invariant (op0))
3705 set_value_range_to_value (&vr0, op0, NULL);
3706 else
3707 set_value_range_to_varying (&vr0);
3709 op1 = gimple_assign_rhs3 (stmt);
3710 if (TREE_CODE (op1) == SSA_NAME)
3711 vr1 = *(get_value_range (op1));
3712 else if (is_gimple_min_invariant (op1))
3713 set_value_range_to_value (&vr1, op1, NULL);
3714 else
3715 set_value_range_to_varying (&vr1);
3717 /* The resulting value range is the union of the operand ranges */
3718 copy_value_range (vr, &vr0);
3719 vrp_meet (vr, &vr1);
3723 /* Extract range information from a comparison expression EXPR based
3724 on the range of its operand and the expression code. */
3726 static void
3727 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3728 tree type, tree op0, tree op1)
3730 bool sop = false;
3731 tree val;
3733 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3734 NULL);
3736 /* A disadvantage of using a special infinity as an overflow
3737 representation is that we lose the ability to record overflow
3738 when we don't have an infinity. So we have to ignore a result
3739 which relies on overflow. */
3741 if (val && !is_overflow_infinity (val) && !sop)
3743 /* Since this expression was found on the RHS of an assignment,
3744 its type may be different from _Bool. Convert VAL to EXPR's
3745 type. */
3746 val = fold_convert (type, val);
3747 if (is_gimple_min_invariant (val))
3748 set_value_range_to_value (vr, val, vr->equiv);
3749 else
3750 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3752 else
3753 /* The result of a comparison is always true or false. */
3754 set_value_range_to_truthvalue (vr, type);
3757 /* Helper function for simplify_internal_call_using_ranges and
3758 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3759 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3760 always overflow. Set *OVF to true if it is known to always
3761 overflow. */
3763 static bool
3764 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3765 tree op0, tree op1, bool *ovf)
3767 value_range_t vr0 = VR_INITIALIZER;
3768 value_range_t vr1 = VR_INITIALIZER;
3769 if (TREE_CODE (op0) == SSA_NAME)
3770 vr0 = *get_value_range (op0);
3771 else if (TREE_CODE (op0) == INTEGER_CST)
3772 set_value_range_to_value (&vr0, op0, NULL);
3773 else
3774 set_value_range_to_varying (&vr0);
3776 if (TREE_CODE (op1) == SSA_NAME)
3777 vr1 = *get_value_range (op1);
3778 else if (TREE_CODE (op1) == INTEGER_CST)
3779 set_value_range_to_value (&vr1, op1, NULL);
3780 else
3781 set_value_range_to_varying (&vr1);
3783 if (!range_int_cst_p (&vr0)
3784 || TREE_OVERFLOW (vr0.min)
3785 || TREE_OVERFLOW (vr0.max))
3787 vr0.min = vrp_val_min (TREE_TYPE (op0));
3788 vr0.max = vrp_val_max (TREE_TYPE (op0));
3790 if (!range_int_cst_p (&vr1)
3791 || TREE_OVERFLOW (vr1.min)
3792 || TREE_OVERFLOW (vr1.max))
3794 vr1.min = vrp_val_min (TREE_TYPE (op1));
3795 vr1.max = vrp_val_max (TREE_TYPE (op1));
3797 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3798 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3799 if (arith_overflowed_p (subcode, type, vr0.max,
3800 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3801 return false;
3802 if (subcode == MULT_EXPR)
3804 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3805 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3806 return false;
3808 if (*ovf)
3810 /* So far we found that there is an overflow on the boundaries.
3811 That doesn't prove that there is an overflow even for all values
3812 in between the boundaries. For that compute widest_int range
3813 of the result and see if it doesn't overlap the range of
3814 type. */
3815 widest_int wmin, wmax;
3816 widest_int w[4];
3817 int i;
3818 w[0] = wi::to_widest (vr0.min);
3819 w[1] = wi::to_widest (vr0.max);
3820 w[2] = wi::to_widest (vr1.min);
3821 w[3] = wi::to_widest (vr1.max);
3822 for (i = 0; i < 4; i++)
3824 widest_int wt;
3825 switch (subcode)
3827 case PLUS_EXPR:
3828 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3829 break;
3830 case MINUS_EXPR:
3831 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3832 break;
3833 case MULT_EXPR:
3834 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3835 break;
3836 default:
3837 gcc_unreachable ();
3839 if (i == 0)
3841 wmin = wt;
3842 wmax = wt;
3844 else
3846 wmin = wi::smin (wmin, wt);
3847 wmax = wi::smax (wmax, wt);
3850 /* The result of op0 CODE op1 is known to be in range
3851 [wmin, wmax]. */
3852 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3853 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3854 /* If all values in [wmin, wmax] are smaller than
3855 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3856 the arithmetic operation will always overflow. */
3857 if (wi::lts_p (wmax, wtmin) || wi::gts_p (wmin, wtmax))
3858 return true;
3859 return false;
3861 return true;
3864 /* Try to derive a nonnegative or nonzero range out of STMT relying
3865 primarily on generic routines in fold in conjunction with range data.
3866 Store the result in *VR */
3868 static void
3869 extract_range_basic (value_range_t *vr, gimple stmt)
3871 bool sop = false;
3872 tree type = gimple_expr_type (stmt);
3874 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3876 tree fndecl = gimple_call_fndecl (stmt), arg;
3877 int mini, maxi, zerov = 0, prec;
3879 switch (DECL_FUNCTION_CODE (fndecl))
3881 case BUILT_IN_CONSTANT_P:
3882 /* If the call is __builtin_constant_p and the argument is a
3883 function parameter resolve it to false. This avoids bogus
3884 array bound warnings.
3885 ??? We could do this as early as inlining is finished. */
3886 arg = gimple_call_arg (stmt, 0);
3887 if (TREE_CODE (arg) == SSA_NAME
3888 && SSA_NAME_IS_DEFAULT_DEF (arg)
3889 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3891 set_value_range_to_null (vr, type);
3892 return;
3894 break;
3895 /* Both __builtin_ffs* and __builtin_popcount return
3896 [0, prec]. */
3897 CASE_INT_FN (BUILT_IN_FFS):
3898 CASE_INT_FN (BUILT_IN_POPCOUNT):
3899 arg = gimple_call_arg (stmt, 0);
3900 prec = TYPE_PRECISION (TREE_TYPE (arg));
3901 mini = 0;
3902 maxi = prec;
3903 if (TREE_CODE (arg) == SSA_NAME)
3905 value_range_t *vr0 = get_value_range (arg);
3906 /* If arg is non-zero, then ffs or popcount
3907 are non-zero. */
3908 if (((vr0->type == VR_RANGE
3909 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3910 || (vr0->type == VR_ANTI_RANGE
3911 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3912 && !is_overflow_infinity (vr0->min)
3913 && !is_overflow_infinity (vr0->max))
3914 mini = 1;
3915 /* If some high bits are known to be zero,
3916 we can decrease the maximum. */
3917 if (vr0->type == VR_RANGE
3918 && TREE_CODE (vr0->max) == INTEGER_CST
3919 && !operand_less_p (vr0->min,
3920 build_zero_cst (TREE_TYPE (vr0->min)))
3921 && !is_overflow_infinity (vr0->max))
3922 maxi = tree_floor_log2 (vr0->max) + 1;
3924 goto bitop_builtin;
3925 /* __builtin_parity* returns [0, 1]. */
3926 CASE_INT_FN (BUILT_IN_PARITY):
3927 mini = 0;
3928 maxi = 1;
3929 goto bitop_builtin;
3930 /* __builtin_c[lt]z* return [0, prec-1], except for
3931 when the argument is 0, but that is undefined behavior.
3932 On many targets where the CLZ RTL or optab value is defined
3933 for 0 the value is prec, so include that in the range
3934 by default. */
3935 CASE_INT_FN (BUILT_IN_CLZ):
3936 arg = gimple_call_arg (stmt, 0);
3937 prec = TYPE_PRECISION (TREE_TYPE (arg));
3938 mini = 0;
3939 maxi = prec;
3940 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3941 != CODE_FOR_nothing
3942 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3943 zerov)
3944 /* Handle only the single common value. */
3945 && zerov != prec)
3946 /* Magic value to give up, unless vr0 proves
3947 arg is non-zero. */
3948 mini = -2;
3949 if (TREE_CODE (arg) == SSA_NAME)
3951 value_range_t *vr0 = get_value_range (arg);
3952 /* From clz of VR_RANGE minimum we can compute
3953 result maximum. */
3954 if (vr0->type == VR_RANGE
3955 && TREE_CODE (vr0->min) == INTEGER_CST
3956 && !is_overflow_infinity (vr0->min))
3958 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3959 if (maxi != prec)
3960 mini = 0;
3962 else if (vr0->type == VR_ANTI_RANGE
3963 && integer_zerop (vr0->min)
3964 && !is_overflow_infinity (vr0->min))
3966 maxi = prec - 1;
3967 mini = 0;
3969 if (mini == -2)
3970 break;
3971 /* From clz of VR_RANGE maximum we can compute
3972 result minimum. */
3973 if (vr0->type == VR_RANGE
3974 && TREE_CODE (vr0->max) == INTEGER_CST
3975 && !is_overflow_infinity (vr0->max))
3977 mini = prec - 1 - tree_floor_log2 (vr0->max);
3978 if (mini == prec)
3979 break;
3982 if (mini == -2)
3983 break;
3984 goto bitop_builtin;
3985 /* __builtin_ctz* return [0, prec-1], except for
3986 when the argument is 0, but that is undefined behavior.
3987 If there is a ctz optab for this mode and
3988 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3989 otherwise just assume 0 won't be seen. */
3990 CASE_INT_FN (BUILT_IN_CTZ):
3991 arg = gimple_call_arg (stmt, 0);
3992 prec = TYPE_PRECISION (TREE_TYPE (arg));
3993 mini = 0;
3994 maxi = prec - 1;
3995 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3996 != CODE_FOR_nothing
3997 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3998 zerov))
4000 /* Handle only the two common values. */
4001 if (zerov == -1)
4002 mini = -1;
4003 else if (zerov == prec)
4004 maxi = prec;
4005 else
4006 /* Magic value to give up, unless vr0 proves
4007 arg is non-zero. */
4008 mini = -2;
4010 if (TREE_CODE (arg) == SSA_NAME)
4012 value_range_t *vr0 = get_value_range (arg);
4013 /* If arg is non-zero, then use [0, prec - 1]. */
4014 if (((vr0->type == VR_RANGE
4015 && integer_nonzerop (vr0->min))
4016 || (vr0->type == VR_ANTI_RANGE
4017 && integer_zerop (vr0->min)))
4018 && !is_overflow_infinity (vr0->min))
4020 mini = 0;
4021 maxi = prec - 1;
4023 /* If some high bits are known to be zero,
4024 we can decrease the result maximum. */
4025 if (vr0->type == VR_RANGE
4026 && TREE_CODE (vr0->max) == INTEGER_CST
4027 && !is_overflow_infinity (vr0->max))
4029 maxi = tree_floor_log2 (vr0->max);
4030 /* For vr0 [0, 0] give up. */
4031 if (maxi == -1)
4032 break;
4035 if (mini == -2)
4036 break;
4037 goto bitop_builtin;
4038 /* __builtin_clrsb* returns [0, prec-1]. */
4039 CASE_INT_FN (BUILT_IN_CLRSB):
4040 arg = gimple_call_arg (stmt, 0);
4041 prec = TYPE_PRECISION (TREE_TYPE (arg));
4042 mini = 0;
4043 maxi = prec - 1;
4044 goto bitop_builtin;
4045 bitop_builtin:
4046 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
4047 build_int_cst (type, maxi), NULL);
4048 return;
4049 default:
4050 break;
4053 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
4055 enum tree_code subcode = ERROR_MARK;
4056 switch (gimple_call_internal_fn (stmt))
4058 case IFN_UBSAN_CHECK_ADD:
4059 subcode = PLUS_EXPR;
4060 break;
4061 case IFN_UBSAN_CHECK_SUB:
4062 subcode = MINUS_EXPR;
4063 break;
4064 case IFN_UBSAN_CHECK_MUL:
4065 subcode = MULT_EXPR;
4066 break;
4067 default:
4068 break;
4070 if (subcode != ERROR_MARK)
4072 bool saved_flag_wrapv = flag_wrapv;
4073 /* Pretend the arithmetics is wrapping. If there is
4074 any overflow, we'll complain, but will actually do
4075 wrapping operation. */
4076 flag_wrapv = 1;
4077 extract_range_from_binary_expr (vr, subcode, type,
4078 gimple_call_arg (stmt, 0),
4079 gimple_call_arg (stmt, 1));
4080 flag_wrapv = saved_flag_wrapv;
4082 /* If for both arguments vrp_valueize returned non-NULL,
4083 this should have been already folded and if not, it
4084 wasn't folded because of overflow. Avoid removing the
4085 UBSAN_CHECK_* calls in that case. */
4086 if (vr->type == VR_RANGE
4087 && (vr->min == vr->max
4088 || operand_equal_p (vr->min, vr->max, 0)))
4089 set_value_range_to_varying (vr);
4090 return;
4093 /* Handle extraction of the two results (result of arithmetics and
4094 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4095 internal function. */
4096 else if (is_gimple_assign (stmt)
4097 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4098 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4099 && INTEGRAL_TYPE_P (type))
4101 enum tree_code code = gimple_assign_rhs_code (stmt);
4102 tree op = gimple_assign_rhs1 (stmt);
4103 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4105 gimple g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4106 if (is_gimple_call (g) && gimple_call_internal_p (g))
4108 enum tree_code subcode = ERROR_MARK;
4109 switch (gimple_call_internal_fn (g))
4111 case IFN_ADD_OVERFLOW:
4112 subcode = PLUS_EXPR;
4113 break;
4114 case IFN_SUB_OVERFLOW:
4115 subcode = MINUS_EXPR;
4116 break;
4117 case IFN_MUL_OVERFLOW:
4118 subcode = MULT_EXPR;
4119 break;
4120 default:
4121 break;
4123 if (subcode != ERROR_MARK)
4125 tree op0 = gimple_call_arg (g, 0);
4126 tree op1 = gimple_call_arg (g, 1);
4127 if (code == IMAGPART_EXPR)
4129 bool ovf = false;
4130 if (check_for_binary_op_overflow (subcode, type,
4131 op0, op1, &ovf))
4132 set_value_range_to_value (vr,
4133 build_int_cst (type, ovf),
4134 NULL);
4135 else
4136 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4137 build_int_cst (type, 1), NULL);
4139 else if (types_compatible_p (type, TREE_TYPE (op0))
4140 && types_compatible_p (type, TREE_TYPE (op1)))
4142 bool saved_flag_wrapv = flag_wrapv;
4143 /* Pretend the arithmetics is wrapping. If there is
4144 any overflow, IMAGPART_EXPR will be set. */
4145 flag_wrapv = 1;
4146 extract_range_from_binary_expr (vr, subcode, type,
4147 op0, op1);
4148 flag_wrapv = saved_flag_wrapv;
4150 else
4152 value_range_t vr0 = VR_INITIALIZER;
4153 value_range_t vr1 = VR_INITIALIZER;
4154 bool saved_flag_wrapv = flag_wrapv;
4155 /* Pretend the arithmetics is wrapping. If there is
4156 any overflow, IMAGPART_EXPR will be set. */
4157 flag_wrapv = 1;
4158 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4159 type, op0);
4160 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4161 type, op1);
4162 extract_range_from_binary_expr_1 (vr, subcode, type,
4163 &vr0, &vr1);
4164 flag_wrapv = saved_flag_wrapv;
4166 return;
4171 if (INTEGRAL_TYPE_P (type)
4172 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4173 set_value_range_to_nonnegative (vr, type,
4174 sop || stmt_overflow_infinity (stmt));
4175 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4176 && !sop)
4177 set_value_range_to_nonnull (vr, type);
4178 else
4179 set_value_range_to_varying (vr);
4183 /* Try to compute a useful range out of assignment STMT and store it
4184 in *VR. */
4186 static void
4187 extract_range_from_assignment (value_range_t *vr, gimple stmt)
4189 enum tree_code code = gimple_assign_rhs_code (stmt);
4191 if (code == ASSERT_EXPR)
4192 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4193 else if (code == SSA_NAME)
4194 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4195 else if (TREE_CODE_CLASS (code) == tcc_binary)
4196 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4197 gimple_expr_type (stmt),
4198 gimple_assign_rhs1 (stmt),
4199 gimple_assign_rhs2 (stmt));
4200 else if (TREE_CODE_CLASS (code) == tcc_unary)
4201 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4202 gimple_expr_type (stmt),
4203 gimple_assign_rhs1 (stmt));
4204 else if (code == COND_EXPR)
4205 extract_range_from_cond_expr (vr, stmt);
4206 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4207 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4208 gimple_expr_type (stmt),
4209 gimple_assign_rhs1 (stmt),
4210 gimple_assign_rhs2 (stmt));
4211 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4212 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4213 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4214 else
4215 set_value_range_to_varying (vr);
4217 if (vr->type == VR_VARYING)
4218 extract_range_basic (vr, stmt);
4221 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4222 would be profitable to adjust VR using scalar evolution information
4223 for VAR. If so, update VR with the new limits. */
4225 static void
4226 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
4227 gimple stmt, tree var)
4229 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4230 enum ev_direction dir;
4232 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4233 better opportunities than a regular range, but I'm not sure. */
4234 if (vr->type == VR_ANTI_RANGE)
4235 return;
4237 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4239 /* Like in PR19590, scev can return a constant function. */
4240 if (is_gimple_min_invariant (chrec))
4242 set_value_range_to_value (vr, chrec, vr->equiv);
4243 return;
4246 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4247 return;
4249 init = initial_condition_in_loop_num (chrec, loop->num);
4250 tem = op_with_constant_singleton_value_range (init);
4251 if (tem)
4252 init = tem;
4253 step = evolution_part_in_loop_num (chrec, loop->num);
4254 tem = op_with_constant_singleton_value_range (step);
4255 if (tem)
4256 step = tem;
4258 /* If STEP is symbolic, we can't know whether INIT will be the
4259 minimum or maximum value in the range. Also, unless INIT is
4260 a simple expression, compare_values and possibly other functions
4261 in tree-vrp won't be able to handle it. */
4262 if (step == NULL_TREE
4263 || !is_gimple_min_invariant (step)
4264 || !valid_value_p (init))
4265 return;
4267 dir = scev_direction (chrec);
4268 if (/* Do not adjust ranges if we do not know whether the iv increases
4269 or decreases, ... */
4270 dir == EV_DIR_UNKNOWN
4271 /* ... or if it may wrap. */
4272 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4273 true))
4274 return;
4276 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4277 negative_overflow_infinity and positive_overflow_infinity,
4278 because we have concluded that the loop probably does not
4279 wrap. */
4281 type = TREE_TYPE (var);
4282 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4283 tmin = lower_bound_in_type (type, type);
4284 else
4285 tmin = TYPE_MIN_VALUE (type);
4286 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4287 tmax = upper_bound_in_type (type, type);
4288 else
4289 tmax = TYPE_MAX_VALUE (type);
4291 /* Try to use estimated number of iterations for the loop to constrain the
4292 final value in the evolution. */
4293 if (TREE_CODE (step) == INTEGER_CST
4294 && is_gimple_val (init)
4295 && (TREE_CODE (init) != SSA_NAME
4296 || get_value_range (init)->type == VR_RANGE))
4298 widest_int nit;
4300 /* We are only entering here for loop header PHI nodes, so using
4301 the number of latch executions is the correct thing to use. */
4302 if (max_loop_iterations (loop, &nit))
4304 value_range_t maxvr = VR_INITIALIZER;
4305 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4306 bool overflow;
4308 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4309 &overflow);
4310 /* If the multiplication overflowed we can't do a meaningful
4311 adjustment. Likewise if the result doesn't fit in the type
4312 of the induction variable. For a signed type we have to
4313 check whether the result has the expected signedness which
4314 is that of the step as number of iterations is unsigned. */
4315 if (!overflow
4316 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4317 && (sgn == UNSIGNED
4318 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4320 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4321 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4322 TREE_TYPE (init), init, tem);
4323 /* Likewise if the addition did. */
4324 if (maxvr.type == VR_RANGE)
4326 tmin = maxvr.min;
4327 tmax = maxvr.max;
4333 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4335 min = tmin;
4336 max = tmax;
4338 /* For VARYING or UNDEFINED ranges, just about anything we get
4339 from scalar evolutions should be better. */
4341 if (dir == EV_DIR_DECREASES)
4342 max = init;
4343 else
4344 min = init;
4346 else if (vr->type == VR_RANGE)
4348 min = vr->min;
4349 max = vr->max;
4351 if (dir == EV_DIR_DECREASES)
4353 /* INIT is the maximum value. If INIT is lower than VR->MAX
4354 but no smaller than VR->MIN, set VR->MAX to INIT. */
4355 if (compare_values (init, max) == -1)
4356 max = init;
4358 /* According to the loop information, the variable does not
4359 overflow. If we think it does, probably because of an
4360 overflow due to arithmetic on a different INF value,
4361 reset now. */
4362 if (is_negative_overflow_infinity (min)
4363 || compare_values (min, tmin) == -1)
4364 min = tmin;
4367 else
4369 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4370 if (compare_values (init, min) == 1)
4371 min = init;
4373 if (is_positive_overflow_infinity (max)
4374 || compare_values (tmax, max) == -1)
4375 max = tmax;
4378 else
4379 return;
4381 /* If we just created an invalid range with the minimum
4382 greater than the maximum, we fail conservatively.
4383 This should happen only in unreachable
4384 parts of code, or for invalid programs. */
4385 if (compare_values (min, max) == 1
4386 || (is_negative_overflow_infinity (min)
4387 && is_positive_overflow_infinity (max)))
4388 return;
4390 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4394 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4396 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4397 all the values in the ranges.
4399 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4401 - Return NULL_TREE if it is not always possible to determine the
4402 value of the comparison.
4404 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4405 overflow infinity was used in the test. */
4408 static tree
4409 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4410 bool *strict_overflow_p)
4412 /* VARYING or UNDEFINED ranges cannot be compared. */
4413 if (vr0->type == VR_VARYING
4414 || vr0->type == VR_UNDEFINED
4415 || vr1->type == VR_VARYING
4416 || vr1->type == VR_UNDEFINED)
4417 return NULL_TREE;
4419 /* Anti-ranges need to be handled separately. */
4420 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4422 /* If both are anti-ranges, then we cannot compute any
4423 comparison. */
4424 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4425 return NULL_TREE;
4427 /* These comparisons are never statically computable. */
4428 if (comp == GT_EXPR
4429 || comp == GE_EXPR
4430 || comp == LT_EXPR
4431 || comp == LE_EXPR)
4432 return NULL_TREE;
4434 /* Equality can be computed only between a range and an
4435 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4436 if (vr0->type == VR_RANGE)
4438 /* To simplify processing, make VR0 the anti-range. */
4439 value_range_t *tmp = vr0;
4440 vr0 = vr1;
4441 vr1 = tmp;
4444 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4446 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4447 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4448 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4450 return NULL_TREE;
4453 if (!usable_range_p (vr0, strict_overflow_p)
4454 || !usable_range_p (vr1, strict_overflow_p))
4455 return NULL_TREE;
4457 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4458 operands around and change the comparison code. */
4459 if (comp == GT_EXPR || comp == GE_EXPR)
4461 value_range_t *tmp;
4462 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4463 tmp = vr0;
4464 vr0 = vr1;
4465 vr1 = tmp;
4468 if (comp == EQ_EXPR)
4470 /* Equality may only be computed if both ranges represent
4471 exactly one value. */
4472 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4473 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4475 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4476 strict_overflow_p);
4477 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4478 strict_overflow_p);
4479 if (cmp_min == 0 && cmp_max == 0)
4480 return boolean_true_node;
4481 else if (cmp_min != -2 && cmp_max != -2)
4482 return boolean_false_node;
4484 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4485 else if (compare_values_warnv (vr0->min, vr1->max,
4486 strict_overflow_p) == 1
4487 || compare_values_warnv (vr1->min, vr0->max,
4488 strict_overflow_p) == 1)
4489 return boolean_false_node;
4491 return NULL_TREE;
4493 else if (comp == NE_EXPR)
4495 int cmp1, cmp2;
4497 /* If VR0 is completely to the left or completely to the right
4498 of VR1, they are always different. Notice that we need to
4499 make sure that both comparisons yield similar results to
4500 avoid comparing values that cannot be compared at
4501 compile-time. */
4502 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4503 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4504 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4505 return boolean_true_node;
4507 /* If VR0 and VR1 represent a single value and are identical,
4508 return false. */
4509 else if (compare_values_warnv (vr0->min, vr0->max,
4510 strict_overflow_p) == 0
4511 && compare_values_warnv (vr1->min, vr1->max,
4512 strict_overflow_p) == 0
4513 && compare_values_warnv (vr0->min, vr1->min,
4514 strict_overflow_p) == 0
4515 && compare_values_warnv (vr0->max, vr1->max,
4516 strict_overflow_p) == 0)
4517 return boolean_false_node;
4519 /* Otherwise, they may or may not be different. */
4520 else
4521 return NULL_TREE;
4523 else if (comp == LT_EXPR || comp == LE_EXPR)
4525 int tst;
4527 /* If VR0 is to the left of VR1, return true. */
4528 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4529 if ((comp == LT_EXPR && tst == -1)
4530 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4532 if (overflow_infinity_range_p (vr0)
4533 || overflow_infinity_range_p (vr1))
4534 *strict_overflow_p = true;
4535 return boolean_true_node;
4538 /* If VR0 is to the right of VR1, return false. */
4539 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4540 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4541 || (comp == LE_EXPR && tst == 1))
4543 if (overflow_infinity_range_p (vr0)
4544 || overflow_infinity_range_p (vr1))
4545 *strict_overflow_p = true;
4546 return boolean_false_node;
4549 /* Otherwise, we don't know. */
4550 return NULL_TREE;
4553 gcc_unreachable ();
4557 /* Given a value range VR, a value VAL and a comparison code COMP, return
4558 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4559 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4560 always returns false. Return NULL_TREE if it is not always
4561 possible to determine the value of the comparison. Also set
4562 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4563 infinity was used in the test. */
4565 static tree
4566 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4567 bool *strict_overflow_p)
4569 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4570 return NULL_TREE;
4572 /* Anti-ranges need to be handled separately. */
4573 if (vr->type == VR_ANTI_RANGE)
4575 /* For anti-ranges, the only predicates that we can compute at
4576 compile time are equality and inequality. */
4577 if (comp == GT_EXPR
4578 || comp == GE_EXPR
4579 || comp == LT_EXPR
4580 || comp == LE_EXPR)
4581 return NULL_TREE;
4583 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4584 if (value_inside_range (val, vr->min, vr->max) == 1)
4585 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4587 return NULL_TREE;
4590 if (!usable_range_p (vr, strict_overflow_p))
4591 return NULL_TREE;
4593 if (comp == EQ_EXPR)
4595 /* EQ_EXPR may only be computed if VR represents exactly
4596 one value. */
4597 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4599 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4600 if (cmp == 0)
4601 return boolean_true_node;
4602 else if (cmp == -1 || cmp == 1 || cmp == 2)
4603 return boolean_false_node;
4605 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4606 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4607 return boolean_false_node;
4609 return NULL_TREE;
4611 else if (comp == NE_EXPR)
4613 /* If VAL is not inside VR, then they are always different. */
4614 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4615 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4616 return boolean_true_node;
4618 /* If VR represents exactly one value equal to VAL, then return
4619 false. */
4620 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4621 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4622 return boolean_false_node;
4624 /* Otherwise, they may or may not be different. */
4625 return NULL_TREE;
4627 else if (comp == LT_EXPR || comp == LE_EXPR)
4629 int tst;
4631 /* If VR is to the left of VAL, return true. */
4632 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4633 if ((comp == LT_EXPR && tst == -1)
4634 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4636 if (overflow_infinity_range_p (vr))
4637 *strict_overflow_p = true;
4638 return boolean_true_node;
4641 /* If VR is to the right of VAL, return false. */
4642 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4643 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4644 || (comp == LE_EXPR && tst == 1))
4646 if (overflow_infinity_range_p (vr))
4647 *strict_overflow_p = true;
4648 return boolean_false_node;
4651 /* Otherwise, we don't know. */
4652 return NULL_TREE;
4654 else if (comp == GT_EXPR || comp == GE_EXPR)
4656 int tst;
4658 /* If VR is to the right of VAL, return true. */
4659 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4660 if ((comp == GT_EXPR && tst == 1)
4661 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4663 if (overflow_infinity_range_p (vr))
4664 *strict_overflow_p = true;
4665 return boolean_true_node;
4668 /* If VR is to the left of VAL, return false. */
4669 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4670 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4671 || (comp == GE_EXPR && tst == -1))
4673 if (overflow_infinity_range_p (vr))
4674 *strict_overflow_p = true;
4675 return boolean_false_node;
4678 /* Otherwise, we don't know. */
4679 return NULL_TREE;
4682 gcc_unreachable ();
4686 /* Debugging dumps. */
4688 void dump_value_range (FILE *, value_range_t *);
4689 void debug_value_range (value_range_t *);
4690 void dump_all_value_ranges (FILE *);
4691 void debug_all_value_ranges (void);
4692 void dump_vr_equiv (FILE *, bitmap);
4693 void debug_vr_equiv (bitmap);
4696 /* Dump value range VR to FILE. */
4698 void
4699 dump_value_range (FILE *file, value_range_t *vr)
4701 if (vr == NULL)
4702 fprintf (file, "[]");
4703 else if (vr->type == VR_UNDEFINED)
4704 fprintf (file, "UNDEFINED");
4705 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4707 tree type = TREE_TYPE (vr->min);
4709 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4711 if (is_negative_overflow_infinity (vr->min))
4712 fprintf (file, "-INF(OVF)");
4713 else if (INTEGRAL_TYPE_P (type)
4714 && !TYPE_UNSIGNED (type)
4715 && vrp_val_is_min (vr->min))
4716 fprintf (file, "-INF");
4717 else
4718 print_generic_expr (file, vr->min, 0);
4720 fprintf (file, ", ");
4722 if (is_positive_overflow_infinity (vr->max))
4723 fprintf (file, "+INF(OVF)");
4724 else if (INTEGRAL_TYPE_P (type)
4725 && vrp_val_is_max (vr->max))
4726 fprintf (file, "+INF");
4727 else
4728 print_generic_expr (file, vr->max, 0);
4730 fprintf (file, "]");
4732 if (vr->equiv)
4734 bitmap_iterator bi;
4735 unsigned i, c = 0;
4737 fprintf (file, " EQUIVALENCES: { ");
4739 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4741 print_generic_expr (file, ssa_name (i), 0);
4742 fprintf (file, " ");
4743 c++;
4746 fprintf (file, "} (%u elements)", c);
4749 else if (vr->type == VR_VARYING)
4750 fprintf (file, "VARYING");
4751 else
4752 fprintf (file, "INVALID RANGE");
4756 /* Dump value range VR to stderr. */
4758 DEBUG_FUNCTION void
4759 debug_value_range (value_range_t *vr)
4761 dump_value_range (stderr, vr);
4762 fprintf (stderr, "\n");
4766 /* Dump value ranges of all SSA_NAMEs to FILE. */
4768 void
4769 dump_all_value_ranges (FILE *file)
4771 size_t i;
4773 for (i = 0; i < num_vr_values; i++)
4775 if (vr_value[i])
4777 print_generic_expr (file, ssa_name (i), 0);
4778 fprintf (file, ": ");
4779 dump_value_range (file, vr_value[i]);
4780 fprintf (file, "\n");
4784 fprintf (file, "\n");
4788 /* Dump all value ranges to stderr. */
4790 DEBUG_FUNCTION void
4791 debug_all_value_ranges (void)
4793 dump_all_value_ranges (stderr);
4797 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4798 create a new SSA name N and return the assertion assignment
4799 'N = ASSERT_EXPR <V, V OP W>'. */
4801 static gimple
4802 build_assert_expr_for (tree cond, tree v)
4804 tree a;
4805 gimple assertion;
4807 gcc_assert (TREE_CODE (v) == SSA_NAME
4808 && COMPARISON_CLASS_P (cond));
4810 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4811 assertion = gimple_build_assign (NULL_TREE, a);
4813 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4814 operand of the ASSERT_EXPR. Create it so the new name and the old one
4815 are registered in the replacement table so that we can fix the SSA web
4816 after adding all the ASSERT_EXPRs. */
4817 create_new_def_for (v, assertion, NULL);
4819 return assertion;
4823 /* Return false if EXPR is a predicate expression involving floating
4824 point values. */
4826 static inline bool
4827 fp_predicate (gimple stmt)
4829 GIMPLE_CHECK (stmt, GIMPLE_COND);
4831 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4834 /* If the range of values taken by OP can be inferred after STMT executes,
4835 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4836 describes the inferred range. Return true if a range could be
4837 inferred. */
4839 static bool
4840 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4842 *val_p = NULL_TREE;
4843 *comp_code_p = ERROR_MARK;
4845 /* Do not attempt to infer anything in names that flow through
4846 abnormal edges. */
4847 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4848 return false;
4850 /* Similarly, don't infer anything from statements that may throw
4851 exceptions. ??? Relax this requirement? */
4852 if (stmt_could_throw_p (stmt))
4853 return false;
4855 /* If STMT is the last statement of a basic block with no normal
4856 successors, there is no point inferring anything about any of its
4857 operands. We would not be able to find a proper insertion point
4858 for the assertion, anyway. */
4859 if (stmt_ends_bb_p (stmt))
4861 edge_iterator ei;
4862 edge e;
4864 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4865 if (!(e->flags & EDGE_ABNORMAL))
4866 break;
4867 if (e == NULL)
4868 return false;
4871 if (infer_nonnull_range (stmt, op, true, true))
4873 *val_p = build_int_cst (TREE_TYPE (op), 0);
4874 *comp_code_p = NE_EXPR;
4875 return true;
4878 return false;
4882 void dump_asserts_for (FILE *, tree);
4883 void debug_asserts_for (tree);
4884 void dump_all_asserts (FILE *);
4885 void debug_all_asserts (void);
4887 /* Dump all the registered assertions for NAME to FILE. */
4889 void
4890 dump_asserts_for (FILE *file, tree name)
4892 assert_locus_t loc;
4894 fprintf (file, "Assertions to be inserted for ");
4895 print_generic_expr (file, name, 0);
4896 fprintf (file, "\n");
4898 loc = asserts_for[SSA_NAME_VERSION (name)];
4899 while (loc)
4901 fprintf (file, "\t");
4902 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4903 fprintf (file, "\n\tBB #%d", loc->bb->index);
4904 if (loc->e)
4906 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4907 loc->e->dest->index);
4908 dump_edge_info (file, loc->e, dump_flags, 0);
4910 fprintf (file, "\n\tPREDICATE: ");
4911 print_generic_expr (file, name, 0);
4912 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4913 print_generic_expr (file, loc->val, 0);
4914 fprintf (file, "\n\n");
4915 loc = loc->next;
4918 fprintf (file, "\n");
4922 /* Dump all the registered assertions for NAME to stderr. */
4924 DEBUG_FUNCTION void
4925 debug_asserts_for (tree name)
4927 dump_asserts_for (stderr, name);
4931 /* Dump all the registered assertions for all the names to FILE. */
4933 void
4934 dump_all_asserts (FILE *file)
4936 unsigned i;
4937 bitmap_iterator bi;
4939 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4940 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4941 dump_asserts_for (file, ssa_name (i));
4942 fprintf (file, "\n");
4946 /* Dump all the registered assertions for all the names to stderr. */
4948 DEBUG_FUNCTION void
4949 debug_all_asserts (void)
4951 dump_all_asserts (stderr);
4955 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4956 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4957 E->DEST, then register this location as a possible insertion point
4958 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4960 BB, E and SI provide the exact insertion point for the new
4961 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4962 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4963 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4964 must not be NULL. */
4966 static void
4967 register_new_assert_for (tree name, tree expr,
4968 enum tree_code comp_code,
4969 tree val,
4970 basic_block bb,
4971 edge e,
4972 gimple_stmt_iterator si)
4974 assert_locus_t n, loc, last_loc;
4975 basic_block dest_bb;
4977 gcc_checking_assert (bb == NULL || e == NULL);
4979 if (e == NULL)
4980 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4981 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4983 /* Never build an assert comparing against an integer constant with
4984 TREE_OVERFLOW set. This confuses our undefined overflow warning
4985 machinery. */
4986 if (TREE_OVERFLOW_P (val))
4987 val = drop_tree_overflow (val);
4989 /* The new assertion A will be inserted at BB or E. We need to
4990 determine if the new location is dominated by a previously
4991 registered location for A. If we are doing an edge insertion,
4992 assume that A will be inserted at E->DEST. Note that this is not
4993 necessarily true.
4995 If E is a critical edge, it will be split. But even if E is
4996 split, the new block will dominate the same set of blocks that
4997 E->DEST dominates.
4999 The reverse, however, is not true, blocks dominated by E->DEST
5000 will not be dominated by the new block created to split E. So,
5001 if the insertion location is on a critical edge, we will not use
5002 the new location to move another assertion previously registered
5003 at a block dominated by E->DEST. */
5004 dest_bb = (bb) ? bb : e->dest;
5006 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5007 VAL at a block dominating DEST_BB, then we don't need to insert a new
5008 one. Similarly, if the same assertion already exists at a block
5009 dominated by DEST_BB and the new location is not on a critical
5010 edge, then update the existing location for the assertion (i.e.,
5011 move the assertion up in the dominance tree).
5013 Note, this is implemented as a simple linked list because there
5014 should not be more than a handful of assertions registered per
5015 name. If this becomes a performance problem, a table hashed by
5016 COMP_CODE and VAL could be implemented. */
5017 loc = asserts_for[SSA_NAME_VERSION (name)];
5018 last_loc = loc;
5019 while (loc)
5021 if (loc->comp_code == comp_code
5022 && (loc->val == val
5023 || operand_equal_p (loc->val, val, 0))
5024 && (loc->expr == expr
5025 || operand_equal_p (loc->expr, expr, 0)))
5027 /* If E is not a critical edge and DEST_BB
5028 dominates the existing location for the assertion, move
5029 the assertion up in the dominance tree by updating its
5030 location information. */
5031 if ((e == NULL || !EDGE_CRITICAL_P (e))
5032 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5034 loc->bb = dest_bb;
5035 loc->e = e;
5036 loc->si = si;
5037 return;
5041 /* Update the last node of the list and move to the next one. */
5042 last_loc = loc;
5043 loc = loc->next;
5046 /* If we didn't find an assertion already registered for
5047 NAME COMP_CODE VAL, add a new one at the end of the list of
5048 assertions associated with NAME. */
5049 n = XNEW (struct assert_locus_d);
5050 n->bb = dest_bb;
5051 n->e = e;
5052 n->si = si;
5053 n->comp_code = comp_code;
5054 n->val = val;
5055 n->expr = expr;
5056 n->next = NULL;
5058 if (last_loc)
5059 last_loc->next = n;
5060 else
5061 asserts_for[SSA_NAME_VERSION (name)] = n;
5063 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5066 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5067 Extract a suitable test code and value and store them into *CODE_P and
5068 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5070 If no extraction was possible, return FALSE, otherwise return TRUE.
5072 If INVERT is true, then we invert the result stored into *CODE_P. */
5074 static bool
5075 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5076 tree cond_op0, tree cond_op1,
5077 bool invert, enum tree_code *code_p,
5078 tree *val_p)
5080 enum tree_code comp_code;
5081 tree val;
5083 /* Otherwise, we have a comparison of the form NAME COMP VAL
5084 or VAL COMP NAME. */
5085 if (name == cond_op1)
5087 /* If the predicate is of the form VAL COMP NAME, flip
5088 COMP around because we need to register NAME as the
5089 first operand in the predicate. */
5090 comp_code = swap_tree_comparison (cond_code);
5091 val = cond_op0;
5093 else
5095 /* The comparison is of the form NAME COMP VAL, so the
5096 comparison code remains unchanged. */
5097 comp_code = cond_code;
5098 val = cond_op1;
5101 /* Invert the comparison code as necessary. */
5102 if (invert)
5103 comp_code = invert_tree_comparison (comp_code, 0);
5105 /* VRP does not handle float types. */
5106 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
5107 return false;
5109 /* Do not register always-false predicates.
5110 FIXME: this works around a limitation in fold() when dealing with
5111 enumerations. Given 'enum { N1, N2 } x;', fold will not
5112 fold 'if (x > N2)' to 'if (0)'. */
5113 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5114 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5116 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5117 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5119 if (comp_code == GT_EXPR
5120 && (!max
5121 || compare_values (val, max) == 0))
5122 return false;
5124 if (comp_code == LT_EXPR
5125 && (!min
5126 || compare_values (val, min) == 0))
5127 return false;
5129 *code_p = comp_code;
5130 *val_p = val;
5131 return true;
5134 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5135 (otherwise return VAL). VAL and MASK must be zero-extended for
5136 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5137 (to transform signed values into unsigned) and at the end xor
5138 SGNBIT back. */
5140 static wide_int
5141 masked_increment (const wide_int &val_in, const wide_int &mask,
5142 const wide_int &sgnbit, unsigned int prec)
5144 wide_int bit = wi::one (prec), res;
5145 unsigned int i;
5147 wide_int val = val_in ^ sgnbit;
5148 for (i = 0; i < prec; i++, bit += bit)
5150 res = mask;
5151 if ((res & bit) == 0)
5152 continue;
5153 res = bit - 1;
5154 res = (val + bit).and_not (res);
5155 res &= mask;
5156 if (wi::gtu_p (res, val))
5157 return res ^ sgnbit;
5159 return val ^ sgnbit;
5162 /* Try to register an edge assertion for SSA name NAME on edge E for
5163 the condition COND contributing to the conditional jump pointed to by BSI.
5164 Invert the condition COND if INVERT is true. */
5166 static void
5167 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5168 enum tree_code cond_code,
5169 tree cond_op0, tree cond_op1, bool invert)
5171 tree val;
5172 enum tree_code comp_code;
5174 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5175 cond_op0,
5176 cond_op1,
5177 invert, &comp_code, &val))
5178 return;
5180 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5181 reachable from E. */
5182 if (live_on_edge (e, name)
5183 && !has_single_use (name))
5184 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5186 /* In the case of NAME <= CST and NAME being defined as
5187 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5188 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5189 This catches range and anti-range tests. */
5190 if ((comp_code == LE_EXPR
5191 || comp_code == GT_EXPR)
5192 && TREE_CODE (val) == INTEGER_CST
5193 && TYPE_UNSIGNED (TREE_TYPE (val)))
5195 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5196 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5198 /* Extract CST2 from the (optional) addition. */
5199 if (is_gimple_assign (def_stmt)
5200 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5202 name2 = gimple_assign_rhs1 (def_stmt);
5203 cst2 = gimple_assign_rhs2 (def_stmt);
5204 if (TREE_CODE (name2) == SSA_NAME
5205 && TREE_CODE (cst2) == INTEGER_CST)
5206 def_stmt = SSA_NAME_DEF_STMT (name2);
5209 /* Extract NAME2 from the (optional) sign-changing cast. */
5210 if (gimple_assign_cast_p (def_stmt))
5212 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5213 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5214 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5215 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5216 name3 = gimple_assign_rhs1 (def_stmt);
5219 /* If name3 is used later, create an ASSERT_EXPR for it. */
5220 if (name3 != NULL_TREE
5221 && TREE_CODE (name3) == SSA_NAME
5222 && (cst2 == NULL_TREE
5223 || TREE_CODE (cst2) == INTEGER_CST)
5224 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5225 && live_on_edge (e, name3)
5226 && !has_single_use (name3))
5228 tree tmp;
5230 /* Build an expression for the range test. */
5231 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5232 if (cst2 != NULL_TREE)
5233 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5235 if (dump_file)
5237 fprintf (dump_file, "Adding assert for ");
5238 print_generic_expr (dump_file, name3, 0);
5239 fprintf (dump_file, " from ");
5240 print_generic_expr (dump_file, tmp, 0);
5241 fprintf (dump_file, "\n");
5244 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5247 /* If name2 is used later, create an ASSERT_EXPR for it. */
5248 if (name2 != NULL_TREE
5249 && TREE_CODE (name2) == SSA_NAME
5250 && TREE_CODE (cst2) == INTEGER_CST
5251 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5252 && live_on_edge (e, name2)
5253 && !has_single_use (name2))
5255 tree tmp;
5257 /* Build an expression for the range test. */
5258 tmp = name2;
5259 if (TREE_TYPE (name) != TREE_TYPE (name2))
5260 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5261 if (cst2 != NULL_TREE)
5262 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5264 if (dump_file)
5266 fprintf (dump_file, "Adding assert for ");
5267 print_generic_expr (dump_file, name2, 0);
5268 fprintf (dump_file, " from ");
5269 print_generic_expr (dump_file, tmp, 0);
5270 fprintf (dump_file, "\n");
5273 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5277 /* In the case of post-in/decrement tests like if (i++) ... and uses
5278 of the in/decremented value on the edge the extra name we want to
5279 assert for is not on the def chain of the name compared. Instead
5280 it is in the set of use stmts. */
5281 if ((comp_code == NE_EXPR
5282 || comp_code == EQ_EXPR)
5283 && TREE_CODE (val) == INTEGER_CST)
5285 imm_use_iterator ui;
5286 gimple use_stmt;
5287 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5289 /* Cut off to use-stmts that are in the predecessor. */
5290 if (gimple_bb (use_stmt) != e->src)
5291 continue;
5293 if (!is_gimple_assign (use_stmt))
5294 continue;
5296 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5297 if (code != PLUS_EXPR
5298 && code != MINUS_EXPR)
5299 continue;
5301 tree cst = gimple_assign_rhs2 (use_stmt);
5302 if (TREE_CODE (cst) != INTEGER_CST)
5303 continue;
5305 tree name2 = gimple_assign_lhs (use_stmt);
5306 if (live_on_edge (e, name2))
5308 cst = int_const_binop (code, val, cst);
5309 register_new_assert_for (name2, name2, comp_code, cst,
5310 NULL, e, bsi);
5315 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5316 && TREE_CODE (val) == INTEGER_CST)
5318 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5319 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5320 tree val2 = NULL_TREE;
5321 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5322 wide_int mask = wi::zero (prec);
5323 unsigned int nprec = prec;
5324 enum tree_code rhs_code = ERROR_MARK;
5326 if (is_gimple_assign (def_stmt))
5327 rhs_code = gimple_assign_rhs_code (def_stmt);
5329 /* Add asserts for NAME cmp CST and NAME being defined
5330 as NAME = (int) NAME2. */
5331 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5332 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5333 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5334 && gimple_assign_cast_p (def_stmt))
5336 name2 = gimple_assign_rhs1 (def_stmt);
5337 if (CONVERT_EXPR_CODE_P (rhs_code)
5338 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5339 && TYPE_UNSIGNED (TREE_TYPE (name2))
5340 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5341 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5342 || !tree_int_cst_equal (val,
5343 TYPE_MIN_VALUE (TREE_TYPE (val))))
5344 && live_on_edge (e, name2)
5345 && !has_single_use (name2))
5347 tree tmp, cst;
5348 enum tree_code new_comp_code = comp_code;
5350 cst = fold_convert (TREE_TYPE (name2),
5351 TYPE_MIN_VALUE (TREE_TYPE (val)));
5352 /* Build an expression for the range test. */
5353 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5354 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5355 fold_convert (TREE_TYPE (name2), val));
5356 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5358 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5359 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5360 build_int_cst (TREE_TYPE (name2), 1));
5363 if (dump_file)
5365 fprintf (dump_file, "Adding assert for ");
5366 print_generic_expr (dump_file, name2, 0);
5367 fprintf (dump_file, " from ");
5368 print_generic_expr (dump_file, tmp, 0);
5369 fprintf (dump_file, "\n");
5372 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5373 e, bsi);
5377 /* Add asserts for NAME cmp CST and NAME being defined as
5378 NAME = NAME2 >> CST2.
5380 Extract CST2 from the right shift. */
5381 if (rhs_code == RSHIFT_EXPR)
5383 name2 = gimple_assign_rhs1 (def_stmt);
5384 cst2 = gimple_assign_rhs2 (def_stmt);
5385 if (TREE_CODE (name2) == SSA_NAME
5386 && tree_fits_uhwi_p (cst2)
5387 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5388 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5389 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5390 && live_on_edge (e, name2)
5391 && !has_single_use (name2))
5393 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5394 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5397 if (val2 != NULL_TREE
5398 && TREE_CODE (val2) == INTEGER_CST
5399 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5400 TREE_TYPE (val),
5401 val2, cst2), val))
5403 enum tree_code new_comp_code = comp_code;
5404 tree tmp, new_val;
5406 tmp = name2;
5407 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5409 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5411 tree type = build_nonstandard_integer_type (prec, 1);
5412 tmp = build1 (NOP_EXPR, type, name2);
5413 val2 = fold_convert (type, val2);
5415 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5416 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5417 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5419 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5421 wide_int minval
5422 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5423 new_val = val2;
5424 if (minval == new_val)
5425 new_val = NULL_TREE;
5427 else
5429 wide_int maxval
5430 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5431 mask |= val2;
5432 if (mask == maxval)
5433 new_val = NULL_TREE;
5434 else
5435 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5438 if (new_val)
5440 if (dump_file)
5442 fprintf (dump_file, "Adding assert for ");
5443 print_generic_expr (dump_file, name2, 0);
5444 fprintf (dump_file, " from ");
5445 print_generic_expr (dump_file, tmp, 0);
5446 fprintf (dump_file, "\n");
5449 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5450 NULL, e, bsi);
5454 /* Add asserts for NAME cmp CST and NAME being defined as
5455 NAME = NAME2 & CST2.
5457 Extract CST2 from the and.
5459 Also handle
5460 NAME = (unsigned) NAME2;
5461 casts where NAME's type is unsigned and has smaller precision
5462 than NAME2's type as if it was NAME = NAME2 & MASK. */
5463 names[0] = NULL_TREE;
5464 names[1] = NULL_TREE;
5465 cst2 = NULL_TREE;
5466 if (rhs_code == BIT_AND_EXPR
5467 || (CONVERT_EXPR_CODE_P (rhs_code)
5468 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5469 && TYPE_UNSIGNED (TREE_TYPE (val))
5470 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5471 > prec))
5473 name2 = gimple_assign_rhs1 (def_stmt);
5474 if (rhs_code == BIT_AND_EXPR)
5475 cst2 = gimple_assign_rhs2 (def_stmt);
5476 else
5478 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5479 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5481 if (TREE_CODE (name2) == SSA_NAME
5482 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5483 && TREE_CODE (cst2) == INTEGER_CST
5484 && !integer_zerop (cst2)
5485 && (nprec > 1
5486 || TYPE_UNSIGNED (TREE_TYPE (val))))
5488 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5489 if (gimple_assign_cast_p (def_stmt2))
5491 names[1] = gimple_assign_rhs1 (def_stmt2);
5492 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5493 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5494 || (TYPE_PRECISION (TREE_TYPE (name2))
5495 != TYPE_PRECISION (TREE_TYPE (names[1])))
5496 || !live_on_edge (e, names[1])
5497 || has_single_use (names[1]))
5498 names[1] = NULL_TREE;
5500 if (live_on_edge (e, name2)
5501 && !has_single_use (name2))
5502 names[0] = name2;
5505 if (names[0] || names[1])
5507 wide_int minv, maxv, valv, cst2v;
5508 wide_int tem, sgnbit;
5509 bool valid_p = false, valn, cst2n;
5510 enum tree_code ccode = comp_code;
5512 valv = wide_int::from (val, nprec, UNSIGNED);
5513 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5514 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5515 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5516 /* If CST2 doesn't have most significant bit set,
5517 but VAL is negative, we have comparison like
5518 if ((x & 0x123) > -4) (always true). Just give up. */
5519 if (!cst2n && valn)
5520 ccode = ERROR_MARK;
5521 if (cst2n)
5522 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5523 else
5524 sgnbit = wi::zero (nprec);
5525 minv = valv & cst2v;
5526 switch (ccode)
5528 case EQ_EXPR:
5529 /* Minimum unsigned value for equality is VAL & CST2
5530 (should be equal to VAL, otherwise we probably should
5531 have folded the comparison into false) and
5532 maximum unsigned value is VAL | ~CST2. */
5533 maxv = valv | ~cst2v;
5534 valid_p = true;
5535 break;
5537 case NE_EXPR:
5538 tem = valv | ~cst2v;
5539 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5540 if (valv == 0)
5542 cst2n = false;
5543 sgnbit = wi::zero (nprec);
5544 goto gt_expr;
5546 /* If (VAL | ~CST2) is all ones, handle it as
5547 (X & CST2) < VAL. */
5548 if (tem == -1)
5550 cst2n = false;
5551 valn = false;
5552 sgnbit = wi::zero (nprec);
5553 goto lt_expr;
5555 if (!cst2n && wi::neg_p (cst2v))
5556 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5557 if (sgnbit != 0)
5559 if (valv == sgnbit)
5561 cst2n = true;
5562 valn = true;
5563 goto gt_expr;
5565 if (tem == wi::mask (nprec - 1, false, nprec))
5567 cst2n = true;
5568 goto lt_expr;
5570 if (!cst2n)
5571 sgnbit = wi::zero (nprec);
5573 break;
5575 case GE_EXPR:
5576 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5577 is VAL and maximum unsigned value is ~0. For signed
5578 comparison, if CST2 doesn't have most significant bit
5579 set, handle it similarly. If CST2 has MSB set,
5580 the minimum is the same, and maximum is ~0U/2. */
5581 if (minv != valv)
5583 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5584 VAL. */
5585 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5586 if (minv == valv)
5587 break;
5589 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5590 valid_p = true;
5591 break;
5593 case GT_EXPR:
5594 gt_expr:
5595 /* Find out smallest MINV where MINV > VAL
5596 && (MINV & CST2) == MINV, if any. If VAL is signed and
5597 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5598 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5599 if (minv == valv)
5600 break;
5601 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5602 valid_p = true;
5603 break;
5605 case LE_EXPR:
5606 /* Minimum unsigned value for <= is 0 and maximum
5607 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5608 Otherwise, find smallest VAL2 where VAL2 > VAL
5609 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5610 as maximum.
5611 For signed comparison, if CST2 doesn't have most
5612 significant bit set, handle it similarly. If CST2 has
5613 MSB set, the maximum is the same and minimum is INT_MIN. */
5614 if (minv == valv)
5615 maxv = valv;
5616 else
5618 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5619 if (maxv == valv)
5620 break;
5621 maxv -= 1;
5623 maxv |= ~cst2v;
5624 minv = sgnbit;
5625 valid_p = true;
5626 break;
5628 case LT_EXPR:
5629 lt_expr:
5630 /* Minimum unsigned value for < is 0 and maximum
5631 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5632 Otherwise, find smallest VAL2 where VAL2 > VAL
5633 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5634 as maximum.
5635 For signed comparison, if CST2 doesn't have most
5636 significant bit set, handle it similarly. If CST2 has
5637 MSB set, the maximum is the same and minimum is INT_MIN. */
5638 if (minv == valv)
5640 if (valv == sgnbit)
5641 break;
5642 maxv = valv;
5644 else
5646 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5647 if (maxv == valv)
5648 break;
5650 maxv -= 1;
5651 maxv |= ~cst2v;
5652 minv = sgnbit;
5653 valid_p = true;
5654 break;
5656 default:
5657 break;
5659 if (valid_p
5660 && (maxv - minv) != -1)
5662 tree tmp, new_val, type;
5663 int i;
5665 for (i = 0; i < 2; i++)
5666 if (names[i])
5668 wide_int maxv2 = maxv;
5669 tmp = names[i];
5670 type = TREE_TYPE (names[i]);
5671 if (!TYPE_UNSIGNED (type))
5673 type = build_nonstandard_integer_type (nprec, 1);
5674 tmp = build1 (NOP_EXPR, type, names[i]);
5676 if (minv != 0)
5678 tmp = build2 (PLUS_EXPR, type, tmp,
5679 wide_int_to_tree (type, -minv));
5680 maxv2 = maxv - minv;
5682 new_val = wide_int_to_tree (type, maxv2);
5684 if (dump_file)
5686 fprintf (dump_file, "Adding assert for ");
5687 print_generic_expr (dump_file, names[i], 0);
5688 fprintf (dump_file, " from ");
5689 print_generic_expr (dump_file, tmp, 0);
5690 fprintf (dump_file, "\n");
5693 register_new_assert_for (names[i], tmp, LE_EXPR,
5694 new_val, NULL, e, bsi);
5701 /* OP is an operand of a truth value expression which is known to have
5702 a particular value. Register any asserts for OP and for any
5703 operands in OP's defining statement.
5705 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5706 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5708 static void
5709 register_edge_assert_for_1 (tree op, enum tree_code code,
5710 edge e, gimple_stmt_iterator bsi)
5712 gimple op_def;
5713 tree val;
5714 enum tree_code rhs_code;
5716 /* We only care about SSA_NAMEs. */
5717 if (TREE_CODE (op) != SSA_NAME)
5718 return;
5720 /* We know that OP will have a zero or nonzero value. If OP is used
5721 more than once go ahead and register an assert for OP. */
5722 if (live_on_edge (e, op)
5723 && !has_single_use (op))
5725 val = build_int_cst (TREE_TYPE (op), 0);
5726 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5729 /* Now look at how OP is set. If it's set from a comparison,
5730 a truth operation or some bit operations, then we may be able
5731 to register information about the operands of that assignment. */
5732 op_def = SSA_NAME_DEF_STMT (op);
5733 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5734 return;
5736 rhs_code = gimple_assign_rhs_code (op_def);
5738 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5740 bool invert = (code == EQ_EXPR ? true : false);
5741 tree op0 = gimple_assign_rhs1 (op_def);
5742 tree op1 = gimple_assign_rhs2 (op_def);
5744 if (TREE_CODE (op0) == SSA_NAME)
5745 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5746 if (TREE_CODE (op1) == SSA_NAME)
5747 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5749 else if ((code == NE_EXPR
5750 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5751 || (code == EQ_EXPR
5752 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5754 /* Recurse on each operand. */
5755 tree op0 = gimple_assign_rhs1 (op_def);
5756 tree op1 = gimple_assign_rhs2 (op_def);
5757 if (TREE_CODE (op0) == SSA_NAME
5758 && has_single_use (op0))
5759 register_edge_assert_for_1 (op0, code, e, bsi);
5760 if (TREE_CODE (op1) == SSA_NAME
5761 && has_single_use (op1))
5762 register_edge_assert_for_1 (op1, code, e, bsi);
5764 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5765 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5767 /* Recurse, flipping CODE. */
5768 code = invert_tree_comparison (code, false);
5769 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5771 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5773 /* Recurse through the copy. */
5774 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5776 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5778 /* Recurse through the type conversion, unless it is a narrowing
5779 conversion or conversion from non-integral type. */
5780 tree rhs = gimple_assign_rhs1 (op_def);
5781 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5782 && (TYPE_PRECISION (TREE_TYPE (rhs))
5783 <= TYPE_PRECISION (TREE_TYPE (op))))
5784 register_edge_assert_for_1 (rhs, code, e, bsi);
5788 /* Try to register an edge assertion for SSA name NAME on edge E for
5789 the condition COND contributing to the conditional jump pointed to by
5790 SI. */
5792 static void
5793 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5794 enum tree_code cond_code, tree cond_op0,
5795 tree cond_op1)
5797 tree val;
5798 enum tree_code comp_code;
5799 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5801 /* Do not attempt to infer anything in names that flow through
5802 abnormal edges. */
5803 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5804 return;
5806 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5807 cond_op0, cond_op1,
5808 is_else_edge,
5809 &comp_code, &val))
5810 return;
5812 /* Register ASSERT_EXPRs for name. */
5813 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5814 cond_op1, is_else_edge);
5817 /* If COND is effectively an equality test of an SSA_NAME against
5818 the value zero or one, then we may be able to assert values
5819 for SSA_NAMEs which flow into COND. */
5821 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5822 statement of NAME we can assert both operands of the BIT_AND_EXPR
5823 have nonzero value. */
5824 if (((comp_code == EQ_EXPR && integer_onep (val))
5825 || (comp_code == NE_EXPR && integer_zerop (val))))
5827 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5829 if (is_gimple_assign (def_stmt)
5830 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5832 tree op0 = gimple_assign_rhs1 (def_stmt);
5833 tree op1 = gimple_assign_rhs2 (def_stmt);
5834 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5835 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5839 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5840 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5841 have zero value. */
5842 if (((comp_code == EQ_EXPR && integer_zerop (val))
5843 || (comp_code == NE_EXPR && integer_onep (val))))
5845 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5847 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5848 necessarily zero value, or if type-precision is one. */
5849 if (is_gimple_assign (def_stmt)
5850 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5851 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5852 || comp_code == EQ_EXPR)))
5854 tree op0 = gimple_assign_rhs1 (def_stmt);
5855 tree op1 = gimple_assign_rhs2 (def_stmt);
5856 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5857 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5863 /* Determine whether the outgoing edges of BB should receive an
5864 ASSERT_EXPR for each of the operands of BB's LAST statement.
5865 The last statement of BB must be a COND_EXPR.
5867 If any of the sub-graphs rooted at BB have an interesting use of
5868 the predicate operands, an assert location node is added to the
5869 list of assertions for the corresponding operands. */
5871 static void
5872 find_conditional_asserts (basic_block bb, gimple last)
5874 gimple_stmt_iterator bsi;
5875 tree op;
5876 edge_iterator ei;
5877 edge e;
5878 ssa_op_iter iter;
5880 bsi = gsi_for_stmt (last);
5882 /* Look for uses of the operands in each of the sub-graphs
5883 rooted at BB. We need to check each of the outgoing edges
5884 separately, so that we know what kind of ASSERT_EXPR to
5885 insert. */
5886 FOR_EACH_EDGE (e, ei, bb->succs)
5888 if (e->dest == bb)
5889 continue;
5891 /* Register the necessary assertions for each operand in the
5892 conditional predicate. */
5893 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5894 register_edge_assert_for (op, e, bsi,
5895 gimple_cond_code (last),
5896 gimple_cond_lhs (last),
5897 gimple_cond_rhs (last));
5901 struct case_info
5903 tree expr;
5904 basic_block bb;
5907 /* Compare two case labels sorting first by the destination bb index
5908 and then by the case value. */
5910 static int
5911 compare_case_labels (const void *p1, const void *p2)
5913 const struct case_info *ci1 = (const struct case_info *) p1;
5914 const struct case_info *ci2 = (const struct case_info *) p2;
5915 int idx1 = ci1->bb->index;
5916 int idx2 = ci2->bb->index;
5918 if (idx1 < idx2)
5919 return -1;
5920 else if (idx1 == idx2)
5922 /* Make sure the default label is first in a group. */
5923 if (!CASE_LOW (ci1->expr))
5924 return -1;
5925 else if (!CASE_LOW (ci2->expr))
5926 return 1;
5927 else
5928 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5929 CASE_LOW (ci2->expr));
5931 else
5932 return 1;
5935 /* Determine whether the outgoing edges of BB should receive an
5936 ASSERT_EXPR for each of the operands of BB's LAST statement.
5937 The last statement of BB must be a SWITCH_EXPR.
5939 If any of the sub-graphs rooted at BB have an interesting use of
5940 the predicate operands, an assert location node is added to the
5941 list of assertions for the corresponding operands. */
5943 static void
5944 find_switch_asserts (basic_block bb, gimple last)
5946 gimple_stmt_iterator bsi;
5947 tree op;
5948 edge e;
5949 struct case_info *ci;
5950 size_t n = gimple_switch_num_labels (last);
5951 #if GCC_VERSION >= 4000
5952 unsigned int idx;
5953 #else
5954 /* Work around GCC 3.4 bug (PR 37086). */
5955 volatile unsigned int idx;
5956 #endif
5958 bsi = gsi_for_stmt (last);
5959 op = gimple_switch_index (last);
5960 if (TREE_CODE (op) != SSA_NAME)
5961 return;
5963 /* Build a vector of case labels sorted by destination label. */
5964 ci = XNEWVEC (struct case_info, n);
5965 for (idx = 0; idx < n; ++idx)
5967 ci[idx].expr = gimple_switch_label (last, idx);
5968 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5970 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5972 for (idx = 0; idx < n; ++idx)
5974 tree min, max;
5975 tree cl = ci[idx].expr;
5976 basic_block cbb = ci[idx].bb;
5978 min = CASE_LOW (cl);
5979 max = CASE_HIGH (cl);
5981 /* If there are multiple case labels with the same destination
5982 we need to combine them to a single value range for the edge. */
5983 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5985 /* Skip labels until the last of the group. */
5986 do {
5987 ++idx;
5988 } while (idx < n && cbb == ci[idx].bb);
5989 --idx;
5991 /* Pick up the maximum of the case label range. */
5992 if (CASE_HIGH (ci[idx].expr))
5993 max = CASE_HIGH (ci[idx].expr);
5994 else
5995 max = CASE_LOW (ci[idx].expr);
5998 /* Nothing to do if the range includes the default label until we
5999 can register anti-ranges. */
6000 if (min == NULL_TREE)
6001 continue;
6003 /* Find the edge to register the assert expr on. */
6004 e = find_edge (bb, cbb);
6006 /* Register the necessary assertions for the operand in the
6007 SWITCH_EXPR. */
6008 register_edge_assert_for (op, e, bsi,
6009 max ? GE_EXPR : EQ_EXPR,
6010 op, fold_convert (TREE_TYPE (op), min));
6011 if (max)
6012 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6013 fold_convert (TREE_TYPE (op), max));
6016 XDELETEVEC (ci);
6020 /* Traverse all the statements in block BB looking for statements that
6021 may generate useful assertions for the SSA names in their operand.
6022 If a statement produces a useful assertion A for name N_i, then the
6023 list of assertions already generated for N_i is scanned to
6024 determine if A is actually needed.
6026 If N_i already had the assertion A at a location dominating the
6027 current location, then nothing needs to be done. Otherwise, the
6028 new location for A is recorded instead.
6030 1- For every statement S in BB, all the variables used by S are
6031 added to bitmap FOUND_IN_SUBGRAPH.
6033 2- If statement S uses an operand N in a way that exposes a known
6034 value range for N, then if N was not already generated by an
6035 ASSERT_EXPR, create a new assert location for N. For instance,
6036 if N is a pointer and the statement dereferences it, we can
6037 assume that N is not NULL.
6039 3- COND_EXPRs are a special case of #2. We can derive range
6040 information from the predicate but need to insert different
6041 ASSERT_EXPRs for each of the sub-graphs rooted at the
6042 conditional block. If the last statement of BB is a conditional
6043 expression of the form 'X op Y', then
6045 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6047 b) If the conditional is the only entry point to the sub-graph
6048 corresponding to the THEN_CLAUSE, recurse into it. On
6049 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6050 an ASSERT_EXPR is added for the corresponding variable.
6052 c) Repeat step (b) on the ELSE_CLAUSE.
6054 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6056 For instance,
6058 if (a == 9)
6059 b = a;
6060 else
6061 b = c + 1;
6063 In this case, an assertion on the THEN clause is useful to
6064 determine that 'a' is always 9 on that edge. However, an assertion
6065 on the ELSE clause would be unnecessary.
6067 4- If BB does not end in a conditional expression, then we recurse
6068 into BB's dominator children.
6070 At the end of the recursive traversal, every SSA name will have a
6071 list of locations where ASSERT_EXPRs should be added. When a new
6072 location for name N is found, it is registered by calling
6073 register_new_assert_for. That function keeps track of all the
6074 registered assertions to prevent adding unnecessary assertions.
6075 For instance, if a pointer P_4 is dereferenced more than once in a
6076 dominator tree, only the location dominating all the dereference of
6077 P_4 will receive an ASSERT_EXPR. */
6079 static void
6080 find_assert_locations_1 (basic_block bb, sbitmap live)
6082 gimple_stmt_iterator si;
6083 gimple last;
6085 last = last_stmt (bb);
6087 /* If BB's last statement is a conditional statement involving integer
6088 operands, determine if we need to add ASSERT_EXPRs. */
6089 if (last
6090 && gimple_code (last) == GIMPLE_COND
6091 && !fp_predicate (last)
6092 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6093 find_conditional_asserts (bb, last);
6095 /* If BB's last statement is a switch statement involving integer
6096 operands, determine if we need to add ASSERT_EXPRs. */
6097 if (last
6098 && gimple_code (last) == GIMPLE_SWITCH
6099 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6100 find_switch_asserts (bb, last);
6102 /* Traverse all the statements in BB marking used names and looking
6103 for statements that may infer assertions for their used operands. */
6104 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
6106 gimple stmt;
6107 tree op;
6108 ssa_op_iter i;
6110 stmt = gsi_stmt (si);
6112 if (is_gimple_debug (stmt))
6113 continue;
6115 /* See if we can derive an assertion for any of STMT's operands. */
6116 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6118 tree value;
6119 enum tree_code comp_code;
6121 /* If op is not live beyond this stmt, do not bother to insert
6122 asserts for it. */
6123 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6124 continue;
6126 /* If OP is used in such a way that we can infer a value
6127 range for it, and we don't find a previous assertion for
6128 it, create a new assertion location node for OP. */
6129 if (infer_value_range (stmt, op, &comp_code, &value))
6131 /* If we are able to infer a nonzero value range for OP,
6132 then walk backwards through the use-def chain to see if OP
6133 was set via a typecast.
6135 If so, then we can also infer a nonzero value range
6136 for the operand of the NOP_EXPR. */
6137 if (comp_code == NE_EXPR && integer_zerop (value))
6139 tree t = op;
6140 gimple def_stmt = SSA_NAME_DEF_STMT (t);
6142 while (is_gimple_assign (def_stmt)
6143 && CONVERT_EXPR_CODE_P
6144 (gimple_assign_rhs_code (def_stmt))
6145 && TREE_CODE
6146 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6147 && POINTER_TYPE_P
6148 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6150 t = gimple_assign_rhs1 (def_stmt);
6151 def_stmt = SSA_NAME_DEF_STMT (t);
6153 /* Note we want to register the assert for the
6154 operand of the NOP_EXPR after SI, not after the
6155 conversion. */
6156 if (! has_single_use (t))
6157 register_new_assert_for (t, t, comp_code, value,
6158 bb, NULL, si);
6162 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6166 /* Update live. */
6167 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6168 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6169 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6170 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6173 /* Traverse all PHI nodes in BB, updating live. */
6174 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6176 use_operand_p arg_p;
6177 ssa_op_iter i;
6178 gimple phi = gsi_stmt (si);
6179 tree res = gimple_phi_result (phi);
6181 if (virtual_operand_p (res))
6182 continue;
6184 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6186 tree arg = USE_FROM_PTR (arg_p);
6187 if (TREE_CODE (arg) == SSA_NAME)
6188 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6191 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6195 /* Do an RPO walk over the function computing SSA name liveness
6196 on-the-fly and deciding on assert expressions to insert. */
6198 static void
6199 find_assert_locations (void)
6201 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6202 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6203 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6204 int rpo_cnt, i;
6206 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6207 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6208 for (i = 0; i < rpo_cnt; ++i)
6209 bb_rpo[rpo[i]] = i;
6211 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6212 the order we compute liveness and insert asserts we otherwise
6213 fail to insert asserts into the loop latch. */
6214 loop_p loop;
6215 FOR_EACH_LOOP (loop, 0)
6217 i = loop->latch->index;
6218 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6219 for (gimple_stmt_iterator gsi = gsi_start_phis (loop->header);
6220 !gsi_end_p (gsi); gsi_next (&gsi))
6222 gimple phi = gsi_stmt (gsi);
6223 if (virtual_operand_p (gimple_phi_result (phi)))
6224 continue;
6225 tree arg = gimple_phi_arg_def (phi, j);
6226 if (TREE_CODE (arg) == SSA_NAME)
6228 if (live[i] == NULL)
6230 live[i] = sbitmap_alloc (num_ssa_names);
6231 bitmap_clear (live[i]);
6233 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6238 for (i = rpo_cnt - 1; i >= 0; --i)
6240 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6241 edge e;
6242 edge_iterator ei;
6244 if (!live[rpo[i]])
6246 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6247 bitmap_clear (live[rpo[i]]);
6250 /* Process BB and update the live information with uses in
6251 this block. */
6252 find_assert_locations_1 (bb, live[rpo[i]]);
6254 /* Merge liveness into the predecessor blocks and free it. */
6255 if (!bitmap_empty_p (live[rpo[i]]))
6257 int pred_rpo = i;
6258 FOR_EACH_EDGE (e, ei, bb->preds)
6260 int pred = e->src->index;
6261 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6262 continue;
6264 if (!live[pred])
6266 live[pred] = sbitmap_alloc (num_ssa_names);
6267 bitmap_clear (live[pred]);
6269 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6271 if (bb_rpo[pred] < pred_rpo)
6272 pred_rpo = bb_rpo[pred];
6275 /* Record the RPO number of the last visited block that needs
6276 live information from this block. */
6277 last_rpo[rpo[i]] = pred_rpo;
6279 else
6281 sbitmap_free (live[rpo[i]]);
6282 live[rpo[i]] = NULL;
6285 /* We can free all successors live bitmaps if all their
6286 predecessors have been visited already. */
6287 FOR_EACH_EDGE (e, ei, bb->succs)
6288 if (last_rpo[e->dest->index] == i
6289 && live[e->dest->index])
6291 sbitmap_free (live[e->dest->index]);
6292 live[e->dest->index] = NULL;
6296 XDELETEVEC (rpo);
6297 XDELETEVEC (bb_rpo);
6298 XDELETEVEC (last_rpo);
6299 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6300 if (live[i])
6301 sbitmap_free (live[i]);
6302 XDELETEVEC (live);
6305 /* Create an ASSERT_EXPR for NAME and insert it in the location
6306 indicated by LOC. Return true if we made any edge insertions. */
6308 static bool
6309 process_assert_insertions_for (tree name, assert_locus_t loc)
6311 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6312 gimple stmt;
6313 tree cond;
6314 gimple assert_stmt;
6315 edge_iterator ei;
6316 edge e;
6318 /* If we have X <=> X do not insert an assert expr for that. */
6319 if (loc->expr == loc->val)
6320 return false;
6322 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6323 assert_stmt = build_assert_expr_for (cond, name);
6324 if (loc->e)
6326 /* We have been asked to insert the assertion on an edge. This
6327 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6328 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6329 || (gimple_code (gsi_stmt (loc->si))
6330 == GIMPLE_SWITCH));
6332 gsi_insert_on_edge (loc->e, assert_stmt);
6333 return true;
6336 /* Otherwise, we can insert right after LOC->SI iff the
6337 statement must not be the last statement in the block. */
6338 stmt = gsi_stmt (loc->si);
6339 if (!stmt_ends_bb_p (stmt))
6341 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6342 return false;
6345 /* If STMT must be the last statement in BB, we can only insert new
6346 assertions on the non-abnormal edge out of BB. Note that since
6347 STMT is not control flow, there may only be one non-abnormal edge
6348 out of BB. */
6349 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6350 if (!(e->flags & EDGE_ABNORMAL))
6352 gsi_insert_on_edge (e, assert_stmt);
6353 return true;
6356 gcc_unreachable ();
6360 /* Process all the insertions registered for every name N_i registered
6361 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6362 found in ASSERTS_FOR[i]. */
6364 static void
6365 process_assert_insertions (void)
6367 unsigned i;
6368 bitmap_iterator bi;
6369 bool update_edges_p = false;
6370 int num_asserts = 0;
6372 if (dump_file && (dump_flags & TDF_DETAILS))
6373 dump_all_asserts (dump_file);
6375 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6377 assert_locus_t loc = asserts_for[i];
6378 gcc_assert (loc);
6380 while (loc)
6382 assert_locus_t next = loc->next;
6383 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6384 free (loc);
6385 loc = next;
6386 num_asserts++;
6390 if (update_edges_p)
6391 gsi_commit_edge_inserts ();
6393 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6394 num_asserts);
6398 /* Traverse the flowgraph looking for conditional jumps to insert range
6399 expressions. These range expressions are meant to provide information
6400 to optimizations that need to reason in terms of value ranges. They
6401 will not be expanded into RTL. For instance, given:
6403 x = ...
6404 y = ...
6405 if (x < y)
6406 y = x - 2;
6407 else
6408 x = y + 3;
6410 this pass will transform the code into:
6412 x = ...
6413 y = ...
6414 if (x < y)
6416 x = ASSERT_EXPR <x, x < y>
6417 y = x - 2
6419 else
6421 y = ASSERT_EXPR <y, x >= y>
6422 x = y + 3
6425 The idea is that once copy and constant propagation have run, other
6426 optimizations will be able to determine what ranges of values can 'x'
6427 take in different paths of the code, simply by checking the reaching
6428 definition of 'x'. */
6430 static void
6431 insert_range_assertions (void)
6433 need_assert_for = BITMAP_ALLOC (NULL);
6434 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6436 calculate_dominance_info (CDI_DOMINATORS);
6438 find_assert_locations ();
6439 if (!bitmap_empty_p (need_assert_for))
6441 process_assert_insertions ();
6442 update_ssa (TODO_update_ssa_no_phi);
6445 if (dump_file && (dump_flags & TDF_DETAILS))
6447 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6448 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6451 free (asserts_for);
6452 BITMAP_FREE (need_assert_for);
6455 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6456 and "struct" hacks. If VRP can determine that the
6457 array subscript is a constant, check if it is outside valid
6458 range. If the array subscript is a RANGE, warn if it is
6459 non-overlapping with valid range.
6460 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6462 static void
6463 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6465 value_range_t* vr = NULL;
6466 tree low_sub, up_sub;
6467 tree low_bound, up_bound, up_bound_p1;
6468 tree base;
6470 if (TREE_NO_WARNING (ref))
6471 return;
6473 low_sub = up_sub = TREE_OPERAND (ref, 1);
6474 up_bound = array_ref_up_bound (ref);
6476 /* Can not check flexible arrays. */
6477 if (!up_bound
6478 || TREE_CODE (up_bound) != INTEGER_CST)
6479 return;
6481 /* Accesses to trailing arrays via pointers may access storage
6482 beyond the types array bounds. */
6483 base = get_base_address (ref);
6484 if (base && TREE_CODE (base) == MEM_REF)
6486 tree cref, next = NULL_TREE;
6488 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6489 return;
6491 cref = TREE_OPERAND (ref, 0);
6492 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6493 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6494 next && TREE_CODE (next) != FIELD_DECL;
6495 next = DECL_CHAIN (next))
6498 /* If this is the last field in a struct type or a field in a
6499 union type do not warn. */
6500 if (!next)
6501 return;
6504 low_bound = array_ref_low_bound (ref);
6505 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6506 build_int_cst (TREE_TYPE (up_bound), 1));
6508 if (TREE_CODE (low_sub) == SSA_NAME)
6510 vr = get_value_range (low_sub);
6511 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6513 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6514 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6518 if (vr && vr->type == VR_ANTI_RANGE)
6520 if (TREE_CODE (up_sub) == INTEGER_CST
6521 && tree_int_cst_lt (up_bound, up_sub)
6522 && TREE_CODE (low_sub) == INTEGER_CST
6523 && tree_int_cst_lt (low_sub, low_bound))
6525 warning_at (location, OPT_Warray_bounds,
6526 "array subscript is outside array bounds");
6527 TREE_NO_WARNING (ref) = 1;
6530 else if (TREE_CODE (up_sub) == INTEGER_CST
6531 && (ignore_off_by_one
6532 ? (tree_int_cst_lt (up_bound, up_sub)
6533 && !tree_int_cst_equal (up_bound_p1, up_sub))
6534 : (tree_int_cst_lt (up_bound, up_sub)
6535 || tree_int_cst_equal (up_bound_p1, up_sub))))
6537 if (dump_file && (dump_flags & TDF_DETAILS))
6539 fprintf (dump_file, "Array bound warning for ");
6540 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6541 fprintf (dump_file, "\n");
6543 warning_at (location, OPT_Warray_bounds,
6544 "array subscript is above array bounds");
6545 TREE_NO_WARNING (ref) = 1;
6547 else if (TREE_CODE (low_sub) == INTEGER_CST
6548 && tree_int_cst_lt (low_sub, low_bound))
6550 if (dump_file && (dump_flags & TDF_DETAILS))
6552 fprintf (dump_file, "Array bound warning for ");
6553 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6554 fprintf (dump_file, "\n");
6556 warning_at (location, OPT_Warray_bounds,
6557 "array subscript is below array bounds");
6558 TREE_NO_WARNING (ref) = 1;
6562 /* Searches if the expr T, located at LOCATION computes
6563 address of an ARRAY_REF, and call check_array_ref on it. */
6565 static void
6566 search_for_addr_array (tree t, location_t location)
6568 while (TREE_CODE (t) == SSA_NAME)
6570 gimple g = SSA_NAME_DEF_STMT (t);
6572 if (gimple_code (g) != GIMPLE_ASSIGN)
6573 return;
6575 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6576 != GIMPLE_SINGLE_RHS)
6577 return;
6579 t = gimple_assign_rhs1 (g);
6583 /* We are only interested in addresses of ARRAY_REF's. */
6584 if (TREE_CODE (t) != ADDR_EXPR)
6585 return;
6587 /* Check each ARRAY_REFs in the reference chain. */
6590 if (TREE_CODE (t) == ARRAY_REF)
6591 check_array_ref (location, t, true /*ignore_off_by_one*/);
6593 t = TREE_OPERAND (t, 0);
6595 while (handled_component_p (t));
6597 if (TREE_CODE (t) == MEM_REF
6598 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6599 && !TREE_NO_WARNING (t))
6601 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6602 tree low_bound, up_bound, el_sz;
6603 offset_int idx;
6604 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6605 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6606 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6607 return;
6609 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6610 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6611 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6612 if (!low_bound
6613 || TREE_CODE (low_bound) != INTEGER_CST
6614 || !up_bound
6615 || TREE_CODE (up_bound) != INTEGER_CST
6616 || !el_sz
6617 || TREE_CODE (el_sz) != INTEGER_CST)
6618 return;
6620 idx = mem_ref_offset (t);
6621 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6622 if (wi::lts_p (idx, 0))
6624 if (dump_file && (dump_flags & TDF_DETAILS))
6626 fprintf (dump_file, "Array bound warning for ");
6627 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6628 fprintf (dump_file, "\n");
6630 warning_at (location, OPT_Warray_bounds,
6631 "array subscript is below array bounds");
6632 TREE_NO_WARNING (t) = 1;
6634 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6635 - wi::to_offset (low_bound) + 1)))
6637 if (dump_file && (dump_flags & TDF_DETAILS))
6639 fprintf (dump_file, "Array bound warning for ");
6640 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6641 fprintf (dump_file, "\n");
6643 warning_at (location, OPT_Warray_bounds,
6644 "array subscript is above array bounds");
6645 TREE_NO_WARNING (t) = 1;
6650 /* walk_tree() callback that checks if *TP is
6651 an ARRAY_REF inside an ADDR_EXPR (in which an array
6652 subscript one outside the valid range is allowed). Call
6653 check_array_ref for each ARRAY_REF found. The location is
6654 passed in DATA. */
6656 static tree
6657 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6659 tree t = *tp;
6660 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6661 location_t location;
6663 if (EXPR_HAS_LOCATION (t))
6664 location = EXPR_LOCATION (t);
6665 else
6667 location_t *locp = (location_t *) wi->info;
6668 location = *locp;
6671 *walk_subtree = TRUE;
6673 if (TREE_CODE (t) == ARRAY_REF)
6674 check_array_ref (location, t, false /*ignore_off_by_one*/);
6676 if (TREE_CODE (t) == MEM_REF
6677 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6678 search_for_addr_array (TREE_OPERAND (t, 0), location);
6680 if (TREE_CODE (t) == ADDR_EXPR)
6681 *walk_subtree = FALSE;
6683 return NULL_TREE;
6686 /* Walk over all statements of all reachable BBs and call check_array_bounds
6687 on them. */
6689 static void
6690 check_all_array_refs (void)
6692 basic_block bb;
6693 gimple_stmt_iterator si;
6695 FOR_EACH_BB_FN (bb, cfun)
6697 edge_iterator ei;
6698 edge e;
6699 bool executable = false;
6701 /* Skip blocks that were found to be unreachable. */
6702 FOR_EACH_EDGE (e, ei, bb->preds)
6703 executable |= !!(e->flags & EDGE_EXECUTABLE);
6704 if (!executable)
6705 continue;
6707 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6709 gimple stmt = gsi_stmt (si);
6710 struct walk_stmt_info wi;
6711 if (!gimple_has_location (stmt))
6712 continue;
6714 if (is_gimple_call (stmt))
6716 size_t i;
6717 size_t n = gimple_call_num_args (stmt);
6718 for (i = 0; i < n; i++)
6720 tree arg = gimple_call_arg (stmt, i);
6721 search_for_addr_array (arg, gimple_location (stmt));
6724 else
6726 memset (&wi, 0, sizeof (wi));
6727 wi.info = CONST_CAST (void *, (const void *)
6728 gimple_location_ptr (stmt));
6730 walk_gimple_op (gsi_stmt (si),
6731 check_array_bounds,
6732 &wi);
6738 /* Return true if all imm uses of VAR are either in STMT, or
6739 feed (optionally through a chain of single imm uses) GIMPLE_COND
6740 in basic block COND_BB. */
6742 static bool
6743 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6745 use_operand_p use_p, use2_p;
6746 imm_use_iterator iter;
6748 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6749 if (USE_STMT (use_p) != stmt)
6751 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6752 if (is_gimple_debug (use_stmt))
6753 continue;
6754 while (is_gimple_assign (use_stmt)
6755 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6756 && single_imm_use (gimple_assign_lhs (use_stmt),
6757 &use2_p, &use_stmt2))
6758 use_stmt = use_stmt2;
6759 if (gimple_code (use_stmt) != GIMPLE_COND
6760 || gimple_bb (use_stmt) != cond_bb)
6761 return false;
6763 return true;
6766 /* Handle
6767 _4 = x_3 & 31;
6768 if (_4 != 0)
6769 goto <bb 6>;
6770 else
6771 goto <bb 7>;
6772 <bb 6>:
6773 __builtin_unreachable ();
6774 <bb 7>:
6775 x_5 = ASSERT_EXPR <x_3, ...>;
6776 If x_3 has no other immediate uses (checked by caller),
6777 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6778 from the non-zero bitmask. */
6780 static void
6781 maybe_set_nonzero_bits (basic_block bb, tree var)
6783 edge e = single_pred_edge (bb);
6784 basic_block cond_bb = e->src;
6785 gimple stmt = last_stmt (cond_bb);
6786 tree cst;
6788 if (stmt == NULL
6789 || gimple_code (stmt) != GIMPLE_COND
6790 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6791 ? EQ_EXPR : NE_EXPR)
6792 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6793 || !integer_zerop (gimple_cond_rhs (stmt)))
6794 return;
6796 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6797 if (!is_gimple_assign (stmt)
6798 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6799 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6800 return;
6801 if (gimple_assign_rhs1 (stmt) != var)
6803 gimple stmt2;
6805 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6806 return;
6807 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6808 if (!gimple_assign_cast_p (stmt2)
6809 || gimple_assign_rhs1 (stmt2) != var
6810 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6811 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6812 != TYPE_PRECISION (TREE_TYPE (var))))
6813 return;
6815 cst = gimple_assign_rhs2 (stmt);
6816 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6819 /* Convert range assertion expressions into the implied copies and
6820 copy propagate away the copies. Doing the trivial copy propagation
6821 here avoids the need to run the full copy propagation pass after
6822 VRP.
6824 FIXME, this will eventually lead to copy propagation removing the
6825 names that had useful range information attached to them. For
6826 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6827 then N_i will have the range [3, +INF].
6829 However, by converting the assertion into the implied copy
6830 operation N_i = N_j, we will then copy-propagate N_j into the uses
6831 of N_i and lose the range information. We may want to hold on to
6832 ASSERT_EXPRs a little while longer as the ranges could be used in
6833 things like jump threading.
6835 The problem with keeping ASSERT_EXPRs around is that passes after
6836 VRP need to handle them appropriately.
6838 Another approach would be to make the range information a first
6839 class property of the SSA_NAME so that it can be queried from
6840 any pass. This is made somewhat more complex by the need for
6841 multiple ranges to be associated with one SSA_NAME. */
6843 static void
6844 remove_range_assertions (void)
6846 basic_block bb;
6847 gimple_stmt_iterator si;
6848 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6849 a basic block preceeded by GIMPLE_COND branching to it and
6850 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6851 int is_unreachable;
6853 /* Note that the BSI iterator bump happens at the bottom of the
6854 loop and no bump is necessary if we're removing the statement
6855 referenced by the current BSI. */
6856 FOR_EACH_BB_FN (bb, cfun)
6857 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6859 gimple stmt = gsi_stmt (si);
6860 gimple use_stmt;
6862 if (is_gimple_assign (stmt)
6863 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6865 tree lhs = gimple_assign_lhs (stmt);
6866 tree rhs = gimple_assign_rhs1 (stmt);
6867 tree var;
6868 tree cond = fold (ASSERT_EXPR_COND (rhs));
6869 use_operand_p use_p;
6870 imm_use_iterator iter;
6872 gcc_assert (cond != boolean_false_node);
6874 var = ASSERT_EXPR_VAR (rhs);
6875 gcc_assert (TREE_CODE (var) == SSA_NAME);
6877 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6878 && SSA_NAME_RANGE_INFO (lhs))
6880 if (is_unreachable == -1)
6882 is_unreachable = 0;
6883 if (single_pred_p (bb)
6884 && assert_unreachable_fallthru_edge_p
6885 (single_pred_edge (bb)))
6886 is_unreachable = 1;
6888 /* Handle
6889 if (x_7 >= 10 && x_7 < 20)
6890 __builtin_unreachable ();
6891 x_8 = ASSERT_EXPR <x_7, ...>;
6892 if the only uses of x_7 are in the ASSERT_EXPR and
6893 in the condition. In that case, we can copy the
6894 range info from x_8 computed in this pass also
6895 for x_7. */
6896 if (is_unreachable
6897 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6898 single_pred (bb)))
6900 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6901 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6902 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6903 maybe_set_nonzero_bits (bb, var);
6907 /* Propagate the RHS into every use of the LHS. */
6908 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6909 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6910 SET_USE (use_p, var);
6912 /* And finally, remove the copy, it is not needed. */
6913 gsi_remove (&si, true);
6914 release_defs (stmt);
6916 else
6918 if (!is_gimple_debug (gsi_stmt (si)))
6919 is_unreachable = 0;
6920 gsi_next (&si);
6926 /* Return true if STMT is interesting for VRP. */
6928 static bool
6929 stmt_interesting_for_vrp (gimple stmt)
6931 if (gimple_code (stmt) == GIMPLE_PHI)
6933 tree res = gimple_phi_result (stmt);
6934 return (!virtual_operand_p (res)
6935 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6936 || POINTER_TYPE_P (TREE_TYPE (res))));
6938 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6940 tree lhs = gimple_get_lhs (stmt);
6942 /* In general, assignments with virtual operands are not useful
6943 for deriving ranges, with the obvious exception of calls to
6944 builtin functions. */
6945 if (lhs && TREE_CODE (lhs) == SSA_NAME
6946 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6947 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6948 && (is_gimple_call (stmt)
6949 || !gimple_vuse (stmt)))
6950 return true;
6952 else if (gimple_code (stmt) == GIMPLE_COND
6953 || gimple_code (stmt) == GIMPLE_SWITCH)
6954 return true;
6956 return false;
6960 /* Initialize local data structures for VRP. */
6962 static void
6963 vrp_initialize (void)
6965 basic_block bb;
6967 values_propagated = false;
6968 num_vr_values = num_ssa_names;
6969 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6970 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6972 FOR_EACH_BB_FN (bb, cfun)
6974 gimple_stmt_iterator si;
6976 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6978 gimple phi = gsi_stmt (si);
6979 if (!stmt_interesting_for_vrp (phi))
6981 tree lhs = PHI_RESULT (phi);
6982 set_value_range_to_varying (get_value_range (lhs));
6983 prop_set_simulate_again (phi, false);
6985 else
6986 prop_set_simulate_again (phi, true);
6989 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6991 gimple stmt = gsi_stmt (si);
6993 /* If the statement is a control insn, then we do not
6994 want to avoid simulating the statement once. Failure
6995 to do so means that those edges will never get added. */
6996 if (stmt_ends_bb_p (stmt))
6997 prop_set_simulate_again (stmt, true);
6998 else if (!stmt_interesting_for_vrp (stmt))
7000 ssa_op_iter i;
7001 tree def;
7002 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7003 set_value_range_to_varying (get_value_range (def));
7004 prop_set_simulate_again (stmt, false);
7006 else
7007 prop_set_simulate_again (stmt, true);
7012 /* Return the singleton value-range for NAME or NAME. */
7014 static inline tree
7015 vrp_valueize (tree name)
7017 if (TREE_CODE (name) == SSA_NAME)
7019 value_range_t *vr = get_value_range (name);
7020 if (vr->type == VR_RANGE
7021 && (vr->min == vr->max
7022 || operand_equal_p (vr->min, vr->max, 0)))
7023 return vr->min;
7025 return name;
7028 /* Return the singleton value-range for NAME if that is a constant
7029 but signal to not follow SSA edges. */
7031 static inline tree
7032 vrp_valueize_1 (tree name)
7034 if (TREE_CODE (name) == SSA_NAME)
7036 value_range_t *vr = get_value_range (name);
7037 if (range_int_cst_singleton_p (vr))
7038 return vr->min;
7039 /* If the definition may be simulated again we cannot follow
7040 this SSA edge as the SSA propagator does not necessarily
7041 re-visit the use. */
7042 gimple def_stmt = SSA_NAME_DEF_STMT (name);
7043 if (prop_simulate_again_p (def_stmt))
7044 return NULL_TREE;
7046 return name;
7049 /* Visit assignment STMT. If it produces an interesting range, record
7050 the SSA name in *OUTPUT_P. */
7052 static enum ssa_prop_result
7053 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
7055 tree def, lhs;
7056 ssa_op_iter iter;
7057 enum gimple_code code = gimple_code (stmt);
7058 lhs = gimple_get_lhs (stmt);
7060 /* We only keep track of ranges in integral and pointer types. */
7061 if (TREE_CODE (lhs) == SSA_NAME
7062 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7063 /* It is valid to have NULL MIN/MAX values on a type. See
7064 build_range_type. */
7065 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7066 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7067 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7069 value_range_t new_vr = VR_INITIALIZER;
7071 /* Try folding the statement to a constant first. */
7072 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7073 vrp_valueize_1);
7074 if (tem && is_gimple_min_invariant (tem))
7075 set_value_range_to_value (&new_vr, tem, NULL);
7076 /* Then dispatch to value-range extracting functions. */
7077 else if (code == GIMPLE_CALL)
7078 extract_range_basic (&new_vr, stmt);
7079 else
7080 extract_range_from_assignment (&new_vr, stmt);
7082 if (update_value_range (lhs, &new_vr))
7084 *output_p = lhs;
7086 if (dump_file && (dump_flags & TDF_DETAILS))
7088 fprintf (dump_file, "Found new range for ");
7089 print_generic_expr (dump_file, lhs, 0);
7090 fprintf (dump_file, ": ");
7091 dump_value_range (dump_file, &new_vr);
7092 fprintf (dump_file, "\n");
7095 if (new_vr.type == VR_VARYING)
7096 return SSA_PROP_VARYING;
7098 return SSA_PROP_INTERESTING;
7101 return SSA_PROP_NOT_INTERESTING;
7104 /* Every other statement produces no useful ranges. */
7105 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7106 set_value_range_to_varying (get_value_range (def));
7108 return SSA_PROP_VARYING;
7111 /* Helper that gets the value range of the SSA_NAME with version I
7112 or a symbolic range containing the SSA_NAME only if the value range
7113 is varying or undefined. */
7115 static inline value_range_t
7116 get_vr_for_comparison (int i)
7118 value_range_t vr = *get_value_range (ssa_name (i));
7120 /* If name N_i does not have a valid range, use N_i as its own
7121 range. This allows us to compare against names that may
7122 have N_i in their ranges. */
7123 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7125 vr.type = VR_RANGE;
7126 vr.min = ssa_name (i);
7127 vr.max = ssa_name (i);
7130 return vr;
7133 /* Compare all the value ranges for names equivalent to VAR with VAL
7134 using comparison code COMP. Return the same value returned by
7135 compare_range_with_value, including the setting of
7136 *STRICT_OVERFLOW_P. */
7138 static tree
7139 compare_name_with_value (enum tree_code comp, tree var, tree val,
7140 bool *strict_overflow_p)
7142 bitmap_iterator bi;
7143 unsigned i;
7144 bitmap e;
7145 tree retval, t;
7146 int used_strict_overflow;
7147 bool sop;
7148 value_range_t equiv_vr;
7150 /* Get the set of equivalences for VAR. */
7151 e = get_value_range (var)->equiv;
7153 /* Start at -1. Set it to 0 if we do a comparison without relying
7154 on overflow, or 1 if all comparisons rely on overflow. */
7155 used_strict_overflow = -1;
7157 /* Compare vars' value range with val. */
7158 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7159 sop = false;
7160 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7161 if (retval)
7162 used_strict_overflow = sop ? 1 : 0;
7164 /* If the equiv set is empty we have done all work we need to do. */
7165 if (e == NULL)
7167 if (retval
7168 && used_strict_overflow > 0)
7169 *strict_overflow_p = true;
7170 return retval;
7173 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7175 equiv_vr = get_vr_for_comparison (i);
7176 sop = false;
7177 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7178 if (t)
7180 /* If we get different answers from different members
7181 of the equivalence set this check must be in a dead
7182 code region. Folding it to a trap representation
7183 would be correct here. For now just return don't-know. */
7184 if (retval != NULL
7185 && t != retval)
7187 retval = NULL_TREE;
7188 break;
7190 retval = t;
7192 if (!sop)
7193 used_strict_overflow = 0;
7194 else if (used_strict_overflow < 0)
7195 used_strict_overflow = 1;
7199 if (retval
7200 && used_strict_overflow > 0)
7201 *strict_overflow_p = true;
7203 return retval;
7207 /* Given a comparison code COMP and names N1 and N2, compare all the
7208 ranges equivalent to N1 against all the ranges equivalent to N2
7209 to determine the value of N1 COMP N2. Return the same value
7210 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7211 whether we relied on an overflow infinity in the comparison. */
7214 static tree
7215 compare_names (enum tree_code comp, tree n1, tree n2,
7216 bool *strict_overflow_p)
7218 tree t, retval;
7219 bitmap e1, e2;
7220 bitmap_iterator bi1, bi2;
7221 unsigned i1, i2;
7222 int used_strict_overflow;
7223 static bitmap_obstack *s_obstack = NULL;
7224 static bitmap s_e1 = NULL, s_e2 = NULL;
7226 /* Compare the ranges of every name equivalent to N1 against the
7227 ranges of every name equivalent to N2. */
7228 e1 = get_value_range (n1)->equiv;
7229 e2 = get_value_range (n2)->equiv;
7231 /* Use the fake bitmaps if e1 or e2 are not available. */
7232 if (s_obstack == NULL)
7234 s_obstack = XNEW (bitmap_obstack);
7235 bitmap_obstack_initialize (s_obstack);
7236 s_e1 = BITMAP_ALLOC (s_obstack);
7237 s_e2 = BITMAP_ALLOC (s_obstack);
7239 if (e1 == NULL)
7240 e1 = s_e1;
7241 if (e2 == NULL)
7242 e2 = s_e2;
7244 /* Add N1 and N2 to their own set of equivalences to avoid
7245 duplicating the body of the loop just to check N1 and N2
7246 ranges. */
7247 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7248 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7250 /* If the equivalence sets have a common intersection, then the two
7251 names can be compared without checking their ranges. */
7252 if (bitmap_intersect_p (e1, e2))
7254 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7255 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7257 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7258 ? boolean_true_node
7259 : boolean_false_node;
7262 /* Start at -1. Set it to 0 if we do a comparison without relying
7263 on overflow, or 1 if all comparisons rely on overflow. */
7264 used_strict_overflow = -1;
7266 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7267 N2 to their own set of equivalences to avoid duplicating the body
7268 of the loop just to check N1 and N2 ranges. */
7269 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7271 value_range_t vr1 = get_vr_for_comparison (i1);
7273 t = retval = NULL_TREE;
7274 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7276 bool sop = false;
7278 value_range_t vr2 = get_vr_for_comparison (i2);
7280 t = compare_ranges (comp, &vr1, &vr2, &sop);
7281 if (t)
7283 /* If we get different answers from different members
7284 of the equivalence set this check must be in a dead
7285 code region. Folding it to a trap representation
7286 would be correct here. For now just return don't-know. */
7287 if (retval != NULL
7288 && t != retval)
7290 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7291 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7292 return NULL_TREE;
7294 retval = t;
7296 if (!sop)
7297 used_strict_overflow = 0;
7298 else if (used_strict_overflow < 0)
7299 used_strict_overflow = 1;
7303 if (retval)
7305 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7306 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7307 if (used_strict_overflow > 0)
7308 *strict_overflow_p = true;
7309 return retval;
7313 /* None of the equivalent ranges are useful in computing this
7314 comparison. */
7315 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7316 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7317 return NULL_TREE;
7320 /* Helper function for vrp_evaluate_conditional_warnv. */
7322 static tree
7323 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7324 tree op0, tree op1,
7325 bool * strict_overflow_p)
7327 value_range_t *vr0, *vr1;
7329 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7330 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7332 tree res = NULL_TREE;
7333 if (vr0 && vr1)
7334 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7335 if (!res && vr0)
7336 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7337 if (!res && vr1)
7338 res = (compare_range_with_value
7339 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7340 return res;
7343 /* Helper function for vrp_evaluate_conditional_warnv. */
7345 static tree
7346 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7347 tree op1, bool use_equiv_p,
7348 bool *strict_overflow_p, bool *only_ranges)
7350 tree ret;
7351 if (only_ranges)
7352 *only_ranges = true;
7354 /* We only deal with integral and pointer types. */
7355 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7356 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7357 return NULL_TREE;
7359 if (use_equiv_p)
7361 if (only_ranges
7362 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7363 (code, op0, op1, strict_overflow_p)))
7364 return ret;
7365 *only_ranges = false;
7366 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7367 return compare_names (code, op0, op1, strict_overflow_p);
7368 else if (TREE_CODE (op0) == SSA_NAME)
7369 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7370 else if (TREE_CODE (op1) == SSA_NAME)
7371 return (compare_name_with_value
7372 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7374 else
7375 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7376 strict_overflow_p);
7377 return NULL_TREE;
7380 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7381 information. Return NULL if the conditional can not be evaluated.
7382 The ranges of all the names equivalent with the operands in COND
7383 will be used when trying to compute the value. If the result is
7384 based on undefined signed overflow, issue a warning if
7385 appropriate. */
7387 static tree
7388 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7390 bool sop;
7391 tree ret;
7392 bool only_ranges;
7394 /* Some passes and foldings leak constants with overflow flag set
7395 into the IL. Avoid doing wrong things with these and bail out. */
7396 if ((TREE_CODE (op0) == INTEGER_CST
7397 && TREE_OVERFLOW (op0))
7398 || (TREE_CODE (op1) == INTEGER_CST
7399 && TREE_OVERFLOW (op1)))
7400 return NULL_TREE;
7402 sop = false;
7403 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7404 &only_ranges);
7406 if (ret && sop)
7408 enum warn_strict_overflow_code wc;
7409 const char* warnmsg;
7411 if (is_gimple_min_invariant (ret))
7413 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7414 warnmsg = G_("assuming signed overflow does not occur when "
7415 "simplifying conditional to constant");
7417 else
7419 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7420 warnmsg = G_("assuming signed overflow does not occur when "
7421 "simplifying conditional");
7424 if (issue_strict_overflow_warning (wc))
7426 location_t location;
7428 if (!gimple_has_location (stmt))
7429 location = input_location;
7430 else
7431 location = gimple_location (stmt);
7432 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7436 if (warn_type_limits
7437 && ret && only_ranges
7438 && TREE_CODE_CLASS (code) == tcc_comparison
7439 && TREE_CODE (op0) == SSA_NAME)
7441 /* If the comparison is being folded and the operand on the LHS
7442 is being compared against a constant value that is outside of
7443 the natural range of OP0's type, then the predicate will
7444 always fold regardless of the value of OP0. If -Wtype-limits
7445 was specified, emit a warning. */
7446 tree type = TREE_TYPE (op0);
7447 value_range_t *vr0 = get_value_range (op0);
7449 if (vr0->type != VR_VARYING
7450 && INTEGRAL_TYPE_P (type)
7451 && vrp_val_is_min (vr0->min)
7452 && vrp_val_is_max (vr0->max)
7453 && is_gimple_min_invariant (op1))
7455 location_t location;
7457 if (!gimple_has_location (stmt))
7458 location = input_location;
7459 else
7460 location = gimple_location (stmt);
7462 warning_at (location, OPT_Wtype_limits,
7463 integer_zerop (ret)
7464 ? G_("comparison always false "
7465 "due to limited range of data type")
7466 : G_("comparison always true "
7467 "due to limited range of data type"));
7471 return ret;
7475 /* Visit conditional statement STMT. If we can determine which edge
7476 will be taken out of STMT's basic block, record it in
7477 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7478 SSA_PROP_VARYING. */
7480 static enum ssa_prop_result
7481 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
7483 tree val;
7484 bool sop;
7486 *taken_edge_p = NULL;
7488 if (dump_file && (dump_flags & TDF_DETAILS))
7490 tree use;
7491 ssa_op_iter i;
7493 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7494 print_gimple_stmt (dump_file, stmt, 0, 0);
7495 fprintf (dump_file, "\nWith known ranges\n");
7497 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7499 fprintf (dump_file, "\t");
7500 print_generic_expr (dump_file, use, 0);
7501 fprintf (dump_file, ": ");
7502 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7505 fprintf (dump_file, "\n");
7508 /* Compute the value of the predicate COND by checking the known
7509 ranges of each of its operands.
7511 Note that we cannot evaluate all the equivalent ranges here
7512 because those ranges may not yet be final and with the current
7513 propagation strategy, we cannot determine when the value ranges
7514 of the names in the equivalence set have changed.
7516 For instance, given the following code fragment
7518 i_5 = PHI <8, i_13>
7520 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7521 if (i_14 == 1)
7524 Assume that on the first visit to i_14, i_5 has the temporary
7525 range [8, 8] because the second argument to the PHI function is
7526 not yet executable. We derive the range ~[0, 0] for i_14 and the
7527 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7528 the first time, since i_14 is equivalent to the range [8, 8], we
7529 determine that the predicate is always false.
7531 On the next round of propagation, i_13 is determined to be
7532 VARYING, which causes i_5 to drop down to VARYING. So, another
7533 visit to i_14 is scheduled. In this second visit, we compute the
7534 exact same range and equivalence set for i_14, namely ~[0, 0] and
7535 { i_5 }. But we did not have the previous range for i_5
7536 registered, so vrp_visit_assignment thinks that the range for
7537 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7538 is not visited again, which stops propagation from visiting
7539 statements in the THEN clause of that if().
7541 To properly fix this we would need to keep the previous range
7542 value for the names in the equivalence set. This way we would've
7543 discovered that from one visit to the other i_5 changed from
7544 range [8, 8] to VR_VARYING.
7546 However, fixing this apparent limitation may not be worth the
7547 additional checking. Testing on several code bases (GCC, DLV,
7548 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7549 4 more predicates folded in SPEC. */
7550 sop = false;
7552 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7553 gimple_cond_lhs (stmt),
7554 gimple_cond_rhs (stmt),
7555 false, &sop, NULL);
7556 if (val)
7558 if (!sop)
7559 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7560 else
7562 if (dump_file && (dump_flags & TDF_DETAILS))
7563 fprintf (dump_file,
7564 "\nIgnoring predicate evaluation because "
7565 "it assumes that signed overflow is undefined");
7566 val = NULL_TREE;
7570 if (dump_file && (dump_flags & TDF_DETAILS))
7572 fprintf (dump_file, "\nPredicate evaluates to: ");
7573 if (val == NULL_TREE)
7574 fprintf (dump_file, "DON'T KNOW\n");
7575 else
7576 print_generic_stmt (dump_file, val, 0);
7579 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7582 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7583 that includes the value VAL. The search is restricted to the range
7584 [START_IDX, n - 1] where n is the size of VEC.
7586 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7587 returned.
7589 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7590 it is placed in IDX and false is returned.
7592 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7593 returned. */
7595 static bool
7596 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
7598 size_t n = gimple_switch_num_labels (stmt);
7599 size_t low, high;
7601 /* Find case label for minimum of the value range or the next one.
7602 At each iteration we are searching in [low, high - 1]. */
7604 for (low = start_idx, high = n; high != low; )
7606 tree t;
7607 int cmp;
7608 /* Note that i != high, so we never ask for n. */
7609 size_t i = (high + low) / 2;
7610 t = gimple_switch_label (stmt, i);
7612 /* Cache the result of comparing CASE_LOW and val. */
7613 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7615 if (cmp == 0)
7617 /* Ranges cannot be empty. */
7618 *idx = i;
7619 return true;
7621 else if (cmp > 0)
7622 high = i;
7623 else
7625 low = i + 1;
7626 if (CASE_HIGH (t) != NULL
7627 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7629 *idx = i;
7630 return true;
7635 *idx = high;
7636 return false;
7639 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7640 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7641 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7642 then MAX_IDX < MIN_IDX.
7643 Returns true if the default label is not needed. */
7645 static bool
7646 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
7647 size_t *max_idx)
7649 size_t i, j;
7650 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7651 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7653 if (i == j
7654 && min_take_default
7655 && max_take_default)
7657 /* Only the default case label reached.
7658 Return an empty range. */
7659 *min_idx = 1;
7660 *max_idx = 0;
7661 return false;
7663 else
7665 bool take_default = min_take_default || max_take_default;
7666 tree low, high;
7667 size_t k;
7669 if (max_take_default)
7670 j--;
7672 /* If the case label range is continuous, we do not need
7673 the default case label. Verify that. */
7674 high = CASE_LOW (gimple_switch_label (stmt, i));
7675 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7676 high = CASE_HIGH (gimple_switch_label (stmt, i));
7677 for (k = i + 1; k <= j; ++k)
7679 low = CASE_LOW (gimple_switch_label (stmt, k));
7680 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7682 take_default = true;
7683 break;
7685 high = low;
7686 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7687 high = CASE_HIGH (gimple_switch_label (stmt, k));
7690 *min_idx = i;
7691 *max_idx = j;
7692 return !take_default;
7696 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7697 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7698 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7699 Returns true if the default label is not needed. */
7701 static bool
7702 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
7703 size_t *max_idx1, size_t *min_idx2,
7704 size_t *max_idx2)
7706 size_t i, j, k, l;
7707 unsigned int n = gimple_switch_num_labels (stmt);
7708 bool take_default;
7709 tree case_low, case_high;
7710 tree min = vr->min, max = vr->max;
7712 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7714 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7716 /* Set second range to emtpy. */
7717 *min_idx2 = 1;
7718 *max_idx2 = 0;
7720 if (vr->type == VR_RANGE)
7722 *min_idx1 = i;
7723 *max_idx1 = j;
7724 return !take_default;
7727 /* Set first range to all case labels. */
7728 *min_idx1 = 1;
7729 *max_idx1 = n - 1;
7731 if (i > j)
7732 return false;
7734 /* Make sure all the values of case labels [i , j] are contained in
7735 range [MIN, MAX]. */
7736 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7737 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7738 if (tree_int_cst_compare (case_low, min) < 0)
7739 i += 1;
7740 if (case_high != NULL_TREE
7741 && tree_int_cst_compare (max, case_high) < 0)
7742 j -= 1;
7744 if (i > j)
7745 return false;
7747 /* If the range spans case labels [i, j], the corresponding anti-range spans
7748 the labels [1, i - 1] and [j + 1, n - 1]. */
7749 k = j + 1;
7750 l = n - 1;
7751 if (k > l)
7753 k = 1;
7754 l = 0;
7757 j = i - 1;
7758 i = 1;
7759 if (i > j)
7761 i = k;
7762 j = l;
7763 k = 1;
7764 l = 0;
7767 *min_idx1 = i;
7768 *max_idx1 = j;
7769 *min_idx2 = k;
7770 *max_idx2 = l;
7771 return false;
7774 /* Visit switch statement STMT. If we can determine which edge
7775 will be taken out of STMT's basic block, record it in
7776 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7777 SSA_PROP_VARYING. */
7779 static enum ssa_prop_result
7780 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7782 tree op, val;
7783 value_range_t *vr;
7784 size_t i = 0, j = 0, k, l;
7785 bool take_default;
7787 *taken_edge_p = NULL;
7788 op = gimple_switch_index (stmt);
7789 if (TREE_CODE (op) != SSA_NAME)
7790 return SSA_PROP_VARYING;
7792 vr = get_value_range (op);
7793 if (dump_file && (dump_flags & TDF_DETAILS))
7795 fprintf (dump_file, "\nVisiting switch expression with operand ");
7796 print_generic_expr (dump_file, op, 0);
7797 fprintf (dump_file, " with known range ");
7798 dump_value_range (dump_file, vr);
7799 fprintf (dump_file, "\n");
7802 if ((vr->type != VR_RANGE
7803 && vr->type != VR_ANTI_RANGE)
7804 || symbolic_range_p (vr))
7805 return SSA_PROP_VARYING;
7807 /* Find the single edge that is taken from the switch expression. */
7808 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7810 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7811 label */
7812 if (j < i)
7814 gcc_assert (take_default);
7815 val = gimple_switch_default_label (stmt);
7817 else
7819 /* Check if labels with index i to j and maybe the default label
7820 are all reaching the same label. */
7822 val = gimple_switch_label (stmt, i);
7823 if (take_default
7824 && CASE_LABEL (gimple_switch_default_label (stmt))
7825 != CASE_LABEL (val))
7827 if (dump_file && (dump_flags & TDF_DETAILS))
7828 fprintf (dump_file, " not a single destination for this "
7829 "range\n");
7830 return SSA_PROP_VARYING;
7832 for (++i; i <= j; ++i)
7834 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7836 if (dump_file && (dump_flags & TDF_DETAILS))
7837 fprintf (dump_file, " not a single destination for this "
7838 "range\n");
7839 return SSA_PROP_VARYING;
7842 for (; k <= l; ++k)
7844 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7846 if (dump_file && (dump_flags & TDF_DETAILS))
7847 fprintf (dump_file, " not a single destination for this "
7848 "range\n");
7849 return SSA_PROP_VARYING;
7854 *taken_edge_p = find_edge (gimple_bb (stmt),
7855 label_to_block (CASE_LABEL (val)));
7857 if (dump_file && (dump_flags & TDF_DETAILS))
7859 fprintf (dump_file, " will take edge to ");
7860 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7863 return SSA_PROP_INTERESTING;
7867 /* Evaluate statement STMT. If the statement produces a useful range,
7868 return SSA_PROP_INTERESTING and record the SSA name with the
7869 interesting range into *OUTPUT_P.
7871 If STMT is a conditional branch and we can determine its truth
7872 value, the taken edge is recorded in *TAKEN_EDGE_P.
7874 If STMT produces a varying value, return SSA_PROP_VARYING. */
7876 static enum ssa_prop_result
7877 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7879 tree def;
7880 ssa_op_iter iter;
7882 if (dump_file && (dump_flags & TDF_DETAILS))
7884 fprintf (dump_file, "\nVisiting statement:\n");
7885 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7888 if (!stmt_interesting_for_vrp (stmt))
7889 gcc_assert (stmt_ends_bb_p (stmt));
7890 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7891 return vrp_visit_assignment_or_call (stmt, output_p);
7892 else if (gimple_code (stmt) == GIMPLE_COND)
7893 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7894 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7895 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7897 /* All other statements produce nothing of interest for VRP, so mark
7898 their outputs varying and prevent further simulation. */
7899 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7900 set_value_range_to_varying (get_value_range (def));
7902 return SSA_PROP_VARYING;
7905 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7906 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7907 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7908 possible such range. The resulting range is not canonicalized. */
7910 static void
7911 union_ranges (enum value_range_type *vr0type,
7912 tree *vr0min, tree *vr0max,
7913 enum value_range_type vr1type,
7914 tree vr1min, tree vr1max)
7916 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7917 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7919 /* [] is vr0, () is vr1 in the following classification comments. */
7920 if (mineq && maxeq)
7922 /* [( )] */
7923 if (*vr0type == vr1type)
7924 /* Nothing to do for equal ranges. */
7926 else if ((*vr0type == VR_RANGE
7927 && vr1type == VR_ANTI_RANGE)
7928 || (*vr0type == VR_ANTI_RANGE
7929 && vr1type == VR_RANGE))
7931 /* For anti-range with range union the result is varying. */
7932 goto give_up;
7934 else
7935 gcc_unreachable ();
7937 else if (operand_less_p (*vr0max, vr1min) == 1
7938 || operand_less_p (vr1max, *vr0min) == 1)
7940 /* [ ] ( ) or ( ) [ ]
7941 If the ranges have an empty intersection, result of the union
7942 operation is the anti-range or if both are anti-ranges
7943 it covers all. */
7944 if (*vr0type == VR_ANTI_RANGE
7945 && vr1type == VR_ANTI_RANGE)
7946 goto give_up;
7947 else if (*vr0type == VR_ANTI_RANGE
7948 && vr1type == VR_RANGE)
7950 else if (*vr0type == VR_RANGE
7951 && vr1type == VR_ANTI_RANGE)
7953 *vr0type = vr1type;
7954 *vr0min = vr1min;
7955 *vr0max = vr1max;
7957 else if (*vr0type == VR_RANGE
7958 && vr1type == VR_RANGE)
7960 /* The result is the convex hull of both ranges. */
7961 if (operand_less_p (*vr0max, vr1min) == 1)
7963 /* If the result can be an anti-range, create one. */
7964 if (TREE_CODE (*vr0max) == INTEGER_CST
7965 && TREE_CODE (vr1min) == INTEGER_CST
7966 && vrp_val_is_min (*vr0min)
7967 && vrp_val_is_max (vr1max))
7969 tree min = int_const_binop (PLUS_EXPR,
7970 *vr0max,
7971 build_int_cst (TREE_TYPE (*vr0max), 1));
7972 tree max = int_const_binop (MINUS_EXPR,
7973 vr1min,
7974 build_int_cst (TREE_TYPE (vr1min), 1));
7975 if (!operand_less_p (max, min))
7977 *vr0type = VR_ANTI_RANGE;
7978 *vr0min = min;
7979 *vr0max = max;
7981 else
7982 *vr0max = vr1max;
7984 else
7985 *vr0max = vr1max;
7987 else
7989 /* If the result can be an anti-range, create one. */
7990 if (TREE_CODE (vr1max) == INTEGER_CST
7991 && TREE_CODE (*vr0min) == INTEGER_CST
7992 && vrp_val_is_min (vr1min)
7993 && vrp_val_is_max (*vr0max))
7995 tree min = int_const_binop (PLUS_EXPR,
7996 vr1max,
7997 build_int_cst (TREE_TYPE (vr1max), 1));
7998 tree max = int_const_binop (MINUS_EXPR,
7999 *vr0min,
8000 build_int_cst (TREE_TYPE (*vr0min), 1));
8001 if (!operand_less_p (max, min))
8003 *vr0type = VR_ANTI_RANGE;
8004 *vr0min = min;
8005 *vr0max = max;
8007 else
8008 *vr0min = vr1min;
8010 else
8011 *vr0min = vr1min;
8014 else
8015 gcc_unreachable ();
8017 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8018 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8020 /* [ ( ) ] or [( ) ] or [ ( )] */
8021 if (*vr0type == VR_RANGE
8022 && vr1type == VR_RANGE)
8024 else if (*vr0type == VR_ANTI_RANGE
8025 && vr1type == VR_ANTI_RANGE)
8027 *vr0type = vr1type;
8028 *vr0min = vr1min;
8029 *vr0max = vr1max;
8031 else if (*vr0type == VR_ANTI_RANGE
8032 && vr1type == VR_RANGE)
8034 /* Arbitrarily choose the right or left gap. */
8035 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8036 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8037 build_int_cst (TREE_TYPE (vr1min), 1));
8038 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8039 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8040 build_int_cst (TREE_TYPE (vr1max), 1));
8041 else
8042 goto give_up;
8044 else if (*vr0type == VR_RANGE
8045 && vr1type == VR_ANTI_RANGE)
8046 /* The result covers everything. */
8047 goto give_up;
8048 else
8049 gcc_unreachable ();
8051 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8052 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8054 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8055 if (*vr0type == VR_RANGE
8056 && vr1type == VR_RANGE)
8058 *vr0type = vr1type;
8059 *vr0min = vr1min;
8060 *vr0max = vr1max;
8062 else if (*vr0type == VR_ANTI_RANGE
8063 && vr1type == VR_ANTI_RANGE)
8065 else if (*vr0type == VR_RANGE
8066 && vr1type == VR_ANTI_RANGE)
8068 *vr0type = VR_ANTI_RANGE;
8069 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8071 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8072 build_int_cst (TREE_TYPE (*vr0min), 1));
8073 *vr0min = vr1min;
8075 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8077 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8078 build_int_cst (TREE_TYPE (*vr0max), 1));
8079 *vr0max = vr1max;
8081 else
8082 goto give_up;
8084 else if (*vr0type == VR_ANTI_RANGE
8085 && vr1type == VR_RANGE)
8086 /* The result covers everything. */
8087 goto give_up;
8088 else
8089 gcc_unreachable ();
8091 else if ((operand_less_p (vr1min, *vr0max) == 1
8092 || operand_equal_p (vr1min, *vr0max, 0))
8093 && operand_less_p (*vr0min, vr1min) == 1
8094 && operand_less_p (*vr0max, vr1max) == 1)
8096 /* [ ( ] ) or [ ]( ) */
8097 if (*vr0type == VR_RANGE
8098 && vr1type == VR_RANGE)
8099 *vr0max = vr1max;
8100 else if (*vr0type == VR_ANTI_RANGE
8101 && vr1type == VR_ANTI_RANGE)
8102 *vr0min = vr1min;
8103 else if (*vr0type == VR_ANTI_RANGE
8104 && vr1type == VR_RANGE)
8106 if (TREE_CODE (vr1min) == INTEGER_CST)
8107 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8108 build_int_cst (TREE_TYPE (vr1min), 1));
8109 else
8110 goto give_up;
8112 else if (*vr0type == VR_RANGE
8113 && vr1type == VR_ANTI_RANGE)
8115 if (TREE_CODE (*vr0max) == INTEGER_CST)
8117 *vr0type = vr1type;
8118 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8119 build_int_cst (TREE_TYPE (*vr0max), 1));
8120 *vr0max = vr1max;
8122 else
8123 goto give_up;
8125 else
8126 gcc_unreachable ();
8128 else if ((operand_less_p (*vr0min, vr1max) == 1
8129 || operand_equal_p (*vr0min, vr1max, 0))
8130 && operand_less_p (vr1min, *vr0min) == 1
8131 && operand_less_p (vr1max, *vr0max) == 1)
8133 /* ( [ ) ] or ( )[ ] */
8134 if (*vr0type == VR_RANGE
8135 && vr1type == VR_RANGE)
8136 *vr0min = vr1min;
8137 else if (*vr0type == VR_ANTI_RANGE
8138 && vr1type == VR_ANTI_RANGE)
8139 *vr0max = vr1max;
8140 else if (*vr0type == VR_ANTI_RANGE
8141 && vr1type == VR_RANGE)
8143 if (TREE_CODE (vr1max) == INTEGER_CST)
8144 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8145 build_int_cst (TREE_TYPE (vr1max), 1));
8146 else
8147 goto give_up;
8149 else if (*vr0type == VR_RANGE
8150 && vr1type == VR_ANTI_RANGE)
8152 if (TREE_CODE (*vr0min) == INTEGER_CST)
8154 *vr0type = vr1type;
8155 *vr0min = vr1min;
8156 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8157 build_int_cst (TREE_TYPE (*vr0min), 1));
8159 else
8160 goto give_up;
8162 else
8163 gcc_unreachable ();
8165 else
8166 goto give_up;
8168 return;
8170 give_up:
8171 *vr0type = VR_VARYING;
8172 *vr0min = NULL_TREE;
8173 *vr0max = NULL_TREE;
8176 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8177 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8178 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8179 possible such range. The resulting range is not canonicalized. */
8181 static void
8182 intersect_ranges (enum value_range_type *vr0type,
8183 tree *vr0min, tree *vr0max,
8184 enum value_range_type vr1type,
8185 tree vr1min, tree vr1max)
8187 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8188 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8190 /* [] is vr0, () is vr1 in the following classification comments. */
8191 if (mineq && maxeq)
8193 /* [( )] */
8194 if (*vr0type == vr1type)
8195 /* Nothing to do for equal ranges. */
8197 else if ((*vr0type == VR_RANGE
8198 && vr1type == VR_ANTI_RANGE)
8199 || (*vr0type == VR_ANTI_RANGE
8200 && vr1type == VR_RANGE))
8202 /* For anti-range with range intersection the result is empty. */
8203 *vr0type = VR_UNDEFINED;
8204 *vr0min = NULL_TREE;
8205 *vr0max = NULL_TREE;
8207 else
8208 gcc_unreachable ();
8210 else if (operand_less_p (*vr0max, vr1min) == 1
8211 || operand_less_p (vr1max, *vr0min) == 1)
8213 /* [ ] ( ) or ( ) [ ]
8214 If the ranges have an empty intersection, the result of the
8215 intersect operation is the range for intersecting an
8216 anti-range with a range or empty when intersecting two ranges. */
8217 if (*vr0type == VR_RANGE
8218 && vr1type == VR_ANTI_RANGE)
8220 else if (*vr0type == VR_ANTI_RANGE
8221 && vr1type == VR_RANGE)
8223 *vr0type = vr1type;
8224 *vr0min = vr1min;
8225 *vr0max = vr1max;
8227 else if (*vr0type == VR_RANGE
8228 && vr1type == VR_RANGE)
8230 *vr0type = VR_UNDEFINED;
8231 *vr0min = NULL_TREE;
8232 *vr0max = NULL_TREE;
8234 else if (*vr0type == VR_ANTI_RANGE
8235 && vr1type == VR_ANTI_RANGE)
8237 /* If the anti-ranges are adjacent to each other merge them. */
8238 if (TREE_CODE (*vr0max) == INTEGER_CST
8239 && TREE_CODE (vr1min) == INTEGER_CST
8240 && operand_less_p (*vr0max, vr1min) == 1
8241 && integer_onep (int_const_binop (MINUS_EXPR,
8242 vr1min, *vr0max)))
8243 *vr0max = vr1max;
8244 else if (TREE_CODE (vr1max) == INTEGER_CST
8245 && TREE_CODE (*vr0min) == INTEGER_CST
8246 && operand_less_p (vr1max, *vr0min) == 1
8247 && integer_onep (int_const_binop (MINUS_EXPR,
8248 *vr0min, vr1max)))
8249 *vr0min = vr1min;
8250 /* Else arbitrarily take VR0. */
8253 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8254 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8256 /* [ ( ) ] or [( ) ] or [ ( )] */
8257 if (*vr0type == VR_RANGE
8258 && vr1type == VR_RANGE)
8260 /* If both are ranges the result is the inner one. */
8261 *vr0type = vr1type;
8262 *vr0min = vr1min;
8263 *vr0max = vr1max;
8265 else if (*vr0type == VR_RANGE
8266 && vr1type == VR_ANTI_RANGE)
8268 /* Choose the right gap if the left one is empty. */
8269 if (mineq)
8271 if (TREE_CODE (vr1max) == INTEGER_CST)
8272 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8273 build_int_cst (TREE_TYPE (vr1max), 1));
8274 else
8275 *vr0min = vr1max;
8277 /* Choose the left gap if the right one is empty. */
8278 else if (maxeq)
8280 if (TREE_CODE (vr1min) == INTEGER_CST)
8281 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8282 build_int_cst (TREE_TYPE (vr1min), 1));
8283 else
8284 *vr0max = vr1min;
8286 /* Choose the anti-range if the range is effectively varying. */
8287 else if (vrp_val_is_min (*vr0min)
8288 && vrp_val_is_max (*vr0max))
8290 *vr0type = vr1type;
8291 *vr0min = vr1min;
8292 *vr0max = vr1max;
8294 /* Else choose the range. */
8296 else if (*vr0type == VR_ANTI_RANGE
8297 && vr1type == VR_ANTI_RANGE)
8298 /* If both are anti-ranges the result is the outer one. */
8300 else if (*vr0type == VR_ANTI_RANGE
8301 && vr1type == VR_RANGE)
8303 /* The intersection is empty. */
8304 *vr0type = VR_UNDEFINED;
8305 *vr0min = NULL_TREE;
8306 *vr0max = NULL_TREE;
8308 else
8309 gcc_unreachable ();
8311 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8312 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8314 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8315 if (*vr0type == VR_RANGE
8316 && vr1type == VR_RANGE)
8317 /* Choose the inner range. */
8319 else if (*vr0type == VR_ANTI_RANGE
8320 && vr1type == VR_RANGE)
8322 /* Choose the right gap if the left is empty. */
8323 if (mineq)
8325 *vr0type = VR_RANGE;
8326 if (TREE_CODE (*vr0max) == INTEGER_CST)
8327 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8328 build_int_cst (TREE_TYPE (*vr0max), 1));
8329 else
8330 *vr0min = *vr0max;
8331 *vr0max = vr1max;
8333 /* Choose the left gap if the right is empty. */
8334 else if (maxeq)
8336 *vr0type = VR_RANGE;
8337 if (TREE_CODE (*vr0min) == INTEGER_CST)
8338 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8339 build_int_cst (TREE_TYPE (*vr0min), 1));
8340 else
8341 *vr0max = *vr0min;
8342 *vr0min = vr1min;
8344 /* Choose the anti-range if the range is effectively varying. */
8345 else if (vrp_val_is_min (vr1min)
8346 && vrp_val_is_max (vr1max))
8348 /* Else choose the range. */
8349 else
8351 *vr0type = vr1type;
8352 *vr0min = vr1min;
8353 *vr0max = vr1max;
8356 else if (*vr0type == VR_ANTI_RANGE
8357 && vr1type == VR_ANTI_RANGE)
8359 /* If both are anti-ranges the result is the outer one. */
8360 *vr0type = vr1type;
8361 *vr0min = vr1min;
8362 *vr0max = vr1max;
8364 else if (vr1type == VR_ANTI_RANGE
8365 && *vr0type == VR_RANGE)
8367 /* The intersection is empty. */
8368 *vr0type = VR_UNDEFINED;
8369 *vr0min = NULL_TREE;
8370 *vr0max = NULL_TREE;
8372 else
8373 gcc_unreachable ();
8375 else if ((operand_less_p (vr1min, *vr0max) == 1
8376 || operand_equal_p (vr1min, *vr0max, 0))
8377 && operand_less_p (*vr0min, vr1min) == 1)
8379 /* [ ( ] ) or [ ]( ) */
8380 if (*vr0type == VR_ANTI_RANGE
8381 && vr1type == VR_ANTI_RANGE)
8382 *vr0max = vr1max;
8383 else if (*vr0type == VR_RANGE
8384 && vr1type == VR_RANGE)
8385 *vr0min = vr1min;
8386 else if (*vr0type == VR_RANGE
8387 && vr1type == VR_ANTI_RANGE)
8389 if (TREE_CODE (vr1min) == INTEGER_CST)
8390 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8391 build_int_cst (TREE_TYPE (vr1min), 1));
8392 else
8393 *vr0max = vr1min;
8395 else if (*vr0type == VR_ANTI_RANGE
8396 && vr1type == VR_RANGE)
8398 *vr0type = VR_RANGE;
8399 if (TREE_CODE (*vr0max) == INTEGER_CST)
8400 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8401 build_int_cst (TREE_TYPE (*vr0max), 1));
8402 else
8403 *vr0min = *vr0max;
8404 *vr0max = vr1max;
8406 else
8407 gcc_unreachable ();
8409 else if ((operand_less_p (*vr0min, vr1max) == 1
8410 || operand_equal_p (*vr0min, vr1max, 0))
8411 && operand_less_p (vr1min, *vr0min) == 1)
8413 /* ( [ ) ] or ( )[ ] */
8414 if (*vr0type == VR_ANTI_RANGE
8415 && vr1type == VR_ANTI_RANGE)
8416 *vr0min = vr1min;
8417 else if (*vr0type == VR_RANGE
8418 && vr1type == VR_RANGE)
8419 *vr0max = vr1max;
8420 else if (*vr0type == VR_RANGE
8421 && vr1type == VR_ANTI_RANGE)
8423 if (TREE_CODE (vr1max) == INTEGER_CST)
8424 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8425 build_int_cst (TREE_TYPE (vr1max), 1));
8426 else
8427 *vr0min = vr1max;
8429 else if (*vr0type == VR_ANTI_RANGE
8430 && vr1type == VR_RANGE)
8432 *vr0type = VR_RANGE;
8433 if (TREE_CODE (*vr0min) == INTEGER_CST)
8434 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8435 build_int_cst (TREE_TYPE (*vr0min), 1));
8436 else
8437 *vr0max = *vr0min;
8438 *vr0min = vr1min;
8440 else
8441 gcc_unreachable ();
8444 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8445 result for the intersection. That's always a conservative
8446 correct estimate. */
8448 return;
8452 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8453 in *VR0. This may not be the smallest possible such range. */
8455 static void
8456 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8458 value_range_t saved;
8460 /* If either range is VR_VARYING the other one wins. */
8461 if (vr1->type == VR_VARYING)
8462 return;
8463 if (vr0->type == VR_VARYING)
8465 copy_value_range (vr0, vr1);
8466 return;
8469 /* When either range is VR_UNDEFINED the resulting range is
8470 VR_UNDEFINED, too. */
8471 if (vr0->type == VR_UNDEFINED)
8472 return;
8473 if (vr1->type == VR_UNDEFINED)
8475 set_value_range_to_undefined (vr0);
8476 return;
8479 /* Save the original vr0 so we can return it as conservative intersection
8480 result when our worker turns things to varying. */
8481 saved = *vr0;
8482 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8483 vr1->type, vr1->min, vr1->max);
8484 /* Make sure to canonicalize the result though as the inversion of a
8485 VR_RANGE can still be a VR_RANGE. */
8486 set_and_canonicalize_value_range (vr0, vr0->type,
8487 vr0->min, vr0->max, vr0->equiv);
8488 /* If that failed, use the saved original VR0. */
8489 if (vr0->type == VR_VARYING)
8491 *vr0 = saved;
8492 return;
8494 /* If the result is VR_UNDEFINED there is no need to mess with
8495 the equivalencies. */
8496 if (vr0->type == VR_UNDEFINED)
8497 return;
8499 /* The resulting set of equivalences for range intersection is the union of
8500 the two sets. */
8501 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8502 bitmap_ior_into (vr0->equiv, vr1->equiv);
8503 else if (vr1->equiv && !vr0->equiv)
8504 bitmap_copy (vr0->equiv, vr1->equiv);
8507 static void
8508 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8510 if (dump_file && (dump_flags & TDF_DETAILS))
8512 fprintf (dump_file, "Intersecting\n ");
8513 dump_value_range (dump_file, vr0);
8514 fprintf (dump_file, "\nand\n ");
8515 dump_value_range (dump_file, vr1);
8516 fprintf (dump_file, "\n");
8518 vrp_intersect_ranges_1 (vr0, vr1);
8519 if (dump_file && (dump_flags & TDF_DETAILS))
8521 fprintf (dump_file, "to\n ");
8522 dump_value_range (dump_file, vr0);
8523 fprintf (dump_file, "\n");
8527 /* Meet operation for value ranges. Given two value ranges VR0 and
8528 VR1, store in VR0 a range that contains both VR0 and VR1. This
8529 may not be the smallest possible such range. */
8531 static void
8532 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8534 value_range_t saved;
8536 if (vr0->type == VR_UNDEFINED)
8538 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8539 return;
8542 if (vr1->type == VR_UNDEFINED)
8544 /* VR0 already has the resulting range. */
8545 return;
8548 if (vr0->type == VR_VARYING)
8550 /* Nothing to do. VR0 already has the resulting range. */
8551 return;
8554 if (vr1->type == VR_VARYING)
8556 set_value_range_to_varying (vr0);
8557 return;
8560 saved = *vr0;
8561 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8562 vr1->type, vr1->min, vr1->max);
8563 if (vr0->type == VR_VARYING)
8565 /* Failed to find an efficient meet. Before giving up and setting
8566 the result to VARYING, see if we can at least derive a useful
8567 anti-range. FIXME, all this nonsense about distinguishing
8568 anti-ranges from ranges is necessary because of the odd
8569 semantics of range_includes_zero_p and friends. */
8570 if (((saved.type == VR_RANGE
8571 && range_includes_zero_p (saved.min, saved.max) == 0)
8572 || (saved.type == VR_ANTI_RANGE
8573 && range_includes_zero_p (saved.min, saved.max) == 1))
8574 && ((vr1->type == VR_RANGE
8575 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8576 || (vr1->type == VR_ANTI_RANGE
8577 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8579 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8581 /* Since this meet operation did not result from the meeting of
8582 two equivalent names, VR0 cannot have any equivalences. */
8583 if (vr0->equiv)
8584 bitmap_clear (vr0->equiv);
8585 return;
8588 set_value_range_to_varying (vr0);
8589 return;
8591 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8592 vr0->equiv);
8593 if (vr0->type == VR_VARYING)
8594 return;
8596 /* The resulting set of equivalences is always the intersection of
8597 the two sets. */
8598 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8599 bitmap_and_into (vr0->equiv, vr1->equiv);
8600 else if (vr0->equiv && !vr1->equiv)
8601 bitmap_clear (vr0->equiv);
8604 static void
8605 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8607 if (dump_file && (dump_flags & TDF_DETAILS))
8609 fprintf (dump_file, "Meeting\n ");
8610 dump_value_range (dump_file, vr0);
8611 fprintf (dump_file, "\nand\n ");
8612 dump_value_range (dump_file, vr1);
8613 fprintf (dump_file, "\n");
8615 vrp_meet_1 (vr0, vr1);
8616 if (dump_file && (dump_flags & TDF_DETAILS))
8618 fprintf (dump_file, "to\n ");
8619 dump_value_range (dump_file, vr0);
8620 fprintf (dump_file, "\n");
8625 /* Visit all arguments for PHI node PHI that flow through executable
8626 edges. If a valid value range can be derived from all the incoming
8627 value ranges, set a new range for the LHS of PHI. */
8629 static enum ssa_prop_result
8630 vrp_visit_phi_node (gimple phi)
8632 size_t i;
8633 tree lhs = PHI_RESULT (phi);
8634 value_range_t *lhs_vr = get_value_range (lhs);
8635 value_range_t vr_result = VR_INITIALIZER;
8636 bool first = true;
8637 int edges, old_edges;
8638 struct loop *l;
8640 if (dump_file && (dump_flags & TDF_DETAILS))
8642 fprintf (dump_file, "\nVisiting PHI node: ");
8643 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8646 edges = 0;
8647 for (i = 0; i < gimple_phi_num_args (phi); i++)
8649 edge e = gimple_phi_arg_edge (phi, i);
8651 if (dump_file && (dump_flags & TDF_DETAILS))
8653 fprintf (dump_file,
8654 " Argument #%d (%d -> %d %sexecutable)\n",
8655 (int) i, e->src->index, e->dest->index,
8656 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8659 if (e->flags & EDGE_EXECUTABLE)
8661 tree arg = PHI_ARG_DEF (phi, i);
8662 value_range_t vr_arg;
8664 ++edges;
8666 if (TREE_CODE (arg) == SSA_NAME)
8668 vr_arg = *(get_value_range (arg));
8669 /* Do not allow equivalences or symbolic ranges to leak in from
8670 backedges. That creates invalid equivalencies.
8671 See PR53465 and PR54767. */
8672 if (e->flags & EDGE_DFS_BACK)
8674 if (vr_arg.type == VR_RANGE
8675 || vr_arg.type == VR_ANTI_RANGE)
8677 vr_arg.equiv = NULL;
8678 if (symbolic_range_p (&vr_arg))
8680 vr_arg.type = VR_VARYING;
8681 vr_arg.min = NULL_TREE;
8682 vr_arg.max = NULL_TREE;
8686 else
8688 /* If the non-backedge arguments range is VR_VARYING then
8689 we can still try recording a simple equivalence. */
8690 if (vr_arg.type == VR_VARYING)
8692 vr_arg.type = VR_RANGE;
8693 vr_arg.min = arg;
8694 vr_arg.max = arg;
8695 vr_arg.equiv = NULL;
8699 else
8701 if (TREE_OVERFLOW_P (arg))
8702 arg = drop_tree_overflow (arg);
8704 vr_arg.type = VR_RANGE;
8705 vr_arg.min = arg;
8706 vr_arg.max = arg;
8707 vr_arg.equiv = NULL;
8710 if (dump_file && (dump_flags & TDF_DETAILS))
8712 fprintf (dump_file, "\t");
8713 print_generic_expr (dump_file, arg, dump_flags);
8714 fprintf (dump_file, ": ");
8715 dump_value_range (dump_file, &vr_arg);
8716 fprintf (dump_file, "\n");
8719 if (first)
8720 copy_value_range (&vr_result, &vr_arg);
8721 else
8722 vrp_meet (&vr_result, &vr_arg);
8723 first = false;
8725 if (vr_result.type == VR_VARYING)
8726 break;
8730 if (vr_result.type == VR_VARYING)
8731 goto varying;
8732 else if (vr_result.type == VR_UNDEFINED)
8733 goto update_range;
8735 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8736 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8738 /* To prevent infinite iterations in the algorithm, derive ranges
8739 when the new value is slightly bigger or smaller than the
8740 previous one. We don't do this if we have seen a new executable
8741 edge; this helps us avoid an overflow infinity for conditionals
8742 which are not in a loop. If the old value-range was VR_UNDEFINED
8743 use the updated range and iterate one more time. */
8744 if (edges > 0
8745 && gimple_phi_num_args (phi) > 1
8746 && edges == old_edges
8747 && lhs_vr->type != VR_UNDEFINED)
8749 /* Compare old and new ranges, fall back to varying if the
8750 values are not comparable. */
8751 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8752 if (cmp_min == -2)
8753 goto varying;
8754 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8755 if (cmp_max == -2)
8756 goto varying;
8758 /* For non VR_RANGE or for pointers fall back to varying if
8759 the range changed. */
8760 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8761 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8762 && (cmp_min != 0 || cmp_max != 0))
8763 goto varying;
8765 /* If the new minimum is larger than than the previous one
8766 retain the old value. If the new minimum value is smaller
8767 than the previous one and not -INF go all the way to -INF + 1.
8768 In the first case, to avoid infinite bouncing between different
8769 minimums, and in the other case to avoid iterating millions of
8770 times to reach -INF. Going to -INF + 1 also lets the following
8771 iteration compute whether there will be any overflow, at the
8772 expense of one additional iteration. */
8773 if (cmp_min < 0)
8774 vr_result.min = lhs_vr->min;
8775 else if (cmp_min > 0
8776 && !vrp_val_is_min (vr_result.min))
8777 vr_result.min
8778 = int_const_binop (PLUS_EXPR,
8779 vrp_val_min (TREE_TYPE (vr_result.min)),
8780 build_int_cst (TREE_TYPE (vr_result.min), 1));
8782 /* Similarly for the maximum value. */
8783 if (cmp_max > 0)
8784 vr_result.max = lhs_vr->max;
8785 else if (cmp_max < 0
8786 && !vrp_val_is_max (vr_result.max))
8787 vr_result.max
8788 = int_const_binop (MINUS_EXPR,
8789 vrp_val_max (TREE_TYPE (vr_result.min)),
8790 build_int_cst (TREE_TYPE (vr_result.min), 1));
8792 /* If we dropped either bound to +-INF then if this is a loop
8793 PHI node SCEV may known more about its value-range. */
8794 if ((cmp_min > 0 || cmp_min < 0
8795 || cmp_max < 0 || cmp_max > 0)
8796 && (l = loop_containing_stmt (phi))
8797 && l->header == gimple_bb (phi))
8798 adjust_range_with_scev (&vr_result, l, phi, lhs);
8800 /* If we will end up with a (-INF, +INF) range, set it to
8801 VARYING. Same if the previous max value was invalid for
8802 the type and we end up with vr_result.min > vr_result.max. */
8803 if ((vrp_val_is_max (vr_result.max)
8804 && vrp_val_is_min (vr_result.min))
8805 || compare_values (vr_result.min,
8806 vr_result.max) > 0)
8807 goto varying;
8810 /* If the new range is different than the previous value, keep
8811 iterating. */
8812 update_range:
8813 if (update_value_range (lhs, &vr_result))
8815 if (dump_file && (dump_flags & TDF_DETAILS))
8817 fprintf (dump_file, "Found new range for ");
8818 print_generic_expr (dump_file, lhs, 0);
8819 fprintf (dump_file, ": ");
8820 dump_value_range (dump_file, &vr_result);
8821 fprintf (dump_file, "\n");
8824 return SSA_PROP_INTERESTING;
8827 /* Nothing changed, don't add outgoing edges. */
8828 return SSA_PROP_NOT_INTERESTING;
8830 /* No match found. Set the LHS to VARYING. */
8831 varying:
8832 set_value_range_to_varying (lhs_vr);
8833 return SSA_PROP_VARYING;
8836 /* Simplify boolean operations if the source is known
8837 to be already a boolean. */
8838 static bool
8839 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8841 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8842 tree lhs, op0, op1;
8843 bool need_conversion;
8845 /* We handle only !=/== case here. */
8846 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8848 op0 = gimple_assign_rhs1 (stmt);
8849 if (!op_with_boolean_value_range_p (op0))
8850 return false;
8852 op1 = gimple_assign_rhs2 (stmt);
8853 if (!op_with_boolean_value_range_p (op1))
8854 return false;
8856 /* Reduce number of cases to handle to NE_EXPR. As there is no
8857 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8858 if (rhs_code == EQ_EXPR)
8860 if (TREE_CODE (op1) == INTEGER_CST)
8861 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8862 build_int_cst (TREE_TYPE (op1), 1));
8863 else
8864 return false;
8867 lhs = gimple_assign_lhs (stmt);
8868 need_conversion
8869 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8871 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8872 if (need_conversion
8873 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8874 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8875 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8876 return false;
8878 /* For A != 0 we can substitute A itself. */
8879 if (integer_zerop (op1))
8880 gimple_assign_set_rhs_with_ops (gsi,
8881 need_conversion
8882 ? NOP_EXPR : TREE_CODE (op0),
8883 op0, NULL_TREE);
8884 /* For A != B we substitute A ^ B. Either with conversion. */
8885 else if (need_conversion)
8887 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8888 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8889 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8890 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8892 /* Or without. */
8893 else
8894 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8895 update_stmt (gsi_stmt (*gsi));
8897 return true;
8900 /* Simplify a division or modulo operator to a right shift or
8901 bitwise and if the first operand is unsigned or is greater
8902 than zero and the second operand is an exact power of two. */
8904 static bool
8905 simplify_div_or_mod_using_ranges (gimple stmt)
8907 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8908 tree val = NULL;
8909 tree op0 = gimple_assign_rhs1 (stmt);
8910 tree op1 = gimple_assign_rhs2 (stmt);
8911 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8913 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8915 val = integer_one_node;
8917 else
8919 bool sop = false;
8921 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8923 if (val
8924 && sop
8925 && integer_onep (val)
8926 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8928 location_t location;
8930 if (!gimple_has_location (stmt))
8931 location = input_location;
8932 else
8933 location = gimple_location (stmt);
8934 warning_at (location, OPT_Wstrict_overflow,
8935 "assuming signed overflow does not occur when "
8936 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8940 if (val && integer_onep (val))
8942 tree t;
8944 if (rhs_code == TRUNC_DIV_EXPR)
8946 t = build_int_cst (integer_type_node, tree_log2 (op1));
8947 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8948 gimple_assign_set_rhs1 (stmt, op0);
8949 gimple_assign_set_rhs2 (stmt, t);
8951 else
8953 t = build_int_cst (TREE_TYPE (op1), 1);
8954 t = int_const_binop (MINUS_EXPR, op1, t);
8955 t = fold_convert (TREE_TYPE (op0), t);
8957 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8958 gimple_assign_set_rhs1 (stmt, op0);
8959 gimple_assign_set_rhs2 (stmt, t);
8962 update_stmt (stmt);
8963 return true;
8966 return false;
8969 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8970 ABS_EXPR. If the operand is <= 0, then simplify the
8971 ABS_EXPR into a NEGATE_EXPR. */
8973 static bool
8974 simplify_abs_using_ranges (gimple stmt)
8976 tree val = NULL;
8977 tree op = gimple_assign_rhs1 (stmt);
8978 tree type = TREE_TYPE (op);
8979 value_range_t *vr = get_value_range (op);
8981 if (TYPE_UNSIGNED (type))
8983 val = integer_zero_node;
8985 else if (vr)
8987 bool sop = false;
8989 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8990 if (!val)
8992 sop = false;
8993 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8994 &sop);
8996 if (val)
8998 if (integer_zerop (val))
8999 val = integer_one_node;
9000 else if (integer_onep (val))
9001 val = integer_zero_node;
9005 if (val
9006 && (integer_onep (val) || integer_zerop (val)))
9008 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9010 location_t location;
9012 if (!gimple_has_location (stmt))
9013 location = input_location;
9014 else
9015 location = gimple_location (stmt);
9016 warning_at (location, OPT_Wstrict_overflow,
9017 "assuming signed overflow does not occur when "
9018 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9021 gimple_assign_set_rhs1 (stmt, op);
9022 if (integer_onep (val))
9023 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9024 else
9025 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9026 update_stmt (stmt);
9027 return true;
9031 return false;
9034 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9035 If all the bits that are being cleared by & are already
9036 known to be zero from VR, or all the bits that are being
9037 set by | are already known to be one from VR, the bit
9038 operation is redundant. */
9040 static bool
9041 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9043 tree op0 = gimple_assign_rhs1 (stmt);
9044 tree op1 = gimple_assign_rhs2 (stmt);
9045 tree op = NULL_TREE;
9046 value_range_t vr0 = VR_INITIALIZER;
9047 value_range_t vr1 = VR_INITIALIZER;
9048 wide_int may_be_nonzero0, may_be_nonzero1;
9049 wide_int must_be_nonzero0, must_be_nonzero1;
9050 wide_int mask;
9052 if (TREE_CODE (op0) == SSA_NAME)
9053 vr0 = *(get_value_range (op0));
9054 else if (is_gimple_min_invariant (op0))
9055 set_value_range_to_value (&vr0, op0, NULL);
9056 else
9057 return false;
9059 if (TREE_CODE (op1) == SSA_NAME)
9060 vr1 = *(get_value_range (op1));
9061 else if (is_gimple_min_invariant (op1))
9062 set_value_range_to_value (&vr1, op1, NULL);
9063 else
9064 return false;
9066 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9067 &must_be_nonzero0))
9068 return false;
9069 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9070 &must_be_nonzero1))
9071 return false;
9073 switch (gimple_assign_rhs_code (stmt))
9075 case BIT_AND_EXPR:
9076 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9077 if (mask == 0)
9079 op = op0;
9080 break;
9082 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9083 if (mask == 0)
9085 op = op1;
9086 break;
9088 break;
9089 case BIT_IOR_EXPR:
9090 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9091 if (mask == 0)
9093 op = op1;
9094 break;
9096 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9097 if (mask == 0)
9099 op = op0;
9100 break;
9102 break;
9103 default:
9104 gcc_unreachable ();
9107 if (op == NULL_TREE)
9108 return false;
9110 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
9111 update_stmt (gsi_stmt (*gsi));
9112 return true;
9115 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9116 a known value range VR.
9118 If there is one and only one value which will satisfy the
9119 conditional, then return that value. Else return NULL. */
9121 static tree
9122 test_for_singularity (enum tree_code cond_code, tree op0,
9123 tree op1, value_range_t *vr)
9125 tree min = NULL;
9126 tree max = NULL;
9128 /* Extract minimum/maximum values which satisfy the
9129 the conditional as it was written. */
9130 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9132 /* This should not be negative infinity; there is no overflow
9133 here. */
9134 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9136 max = op1;
9137 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9139 tree one = build_int_cst (TREE_TYPE (op0), 1);
9140 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9141 if (EXPR_P (max))
9142 TREE_NO_WARNING (max) = 1;
9145 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9147 /* This should not be positive infinity; there is no overflow
9148 here. */
9149 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9151 min = op1;
9152 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9154 tree one = build_int_cst (TREE_TYPE (op0), 1);
9155 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9156 if (EXPR_P (min))
9157 TREE_NO_WARNING (min) = 1;
9161 /* Now refine the minimum and maximum values using any
9162 value range information we have for op0. */
9163 if (min && max)
9165 if (compare_values (vr->min, min) == 1)
9166 min = vr->min;
9167 if (compare_values (vr->max, max) == -1)
9168 max = vr->max;
9170 /* If the new min/max values have converged to a single value,
9171 then there is only one value which can satisfy the condition,
9172 return that value. */
9173 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9174 return min;
9176 return NULL;
9179 /* Return whether the value range *VR fits in an integer type specified
9180 by PRECISION and UNSIGNED_P. */
9182 static bool
9183 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9185 tree src_type;
9186 unsigned src_precision;
9187 widest_int tem;
9188 signop src_sgn;
9190 /* We can only handle integral and pointer types. */
9191 src_type = TREE_TYPE (vr->min);
9192 if (!INTEGRAL_TYPE_P (src_type)
9193 && !POINTER_TYPE_P (src_type))
9194 return false;
9196 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9197 and so is an identity transform. */
9198 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9199 src_sgn = TYPE_SIGN (src_type);
9200 if ((src_precision < dest_precision
9201 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9202 || (src_precision == dest_precision && src_sgn == dest_sgn))
9203 return true;
9205 /* Now we can only handle ranges with constant bounds. */
9206 if (vr->type != VR_RANGE
9207 || TREE_CODE (vr->min) != INTEGER_CST
9208 || TREE_CODE (vr->max) != INTEGER_CST)
9209 return false;
9211 /* For sign changes, the MSB of the wide_int has to be clear.
9212 An unsigned value with its MSB set cannot be represented by
9213 a signed wide_int, while a negative value cannot be represented
9214 by an unsigned wide_int. */
9215 if (src_sgn != dest_sgn
9216 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9217 return false;
9219 /* Then we can perform the conversion on both ends and compare
9220 the result for equality. */
9221 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9222 if (tem != wi::to_widest (vr->min))
9223 return false;
9224 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9225 if (tem != wi::to_widest (vr->max))
9226 return false;
9228 return true;
9231 /* Simplify a conditional using a relational operator to an equality
9232 test if the range information indicates only one value can satisfy
9233 the original conditional. */
9235 static bool
9236 simplify_cond_using_ranges (gimple stmt)
9238 tree op0 = gimple_cond_lhs (stmt);
9239 tree op1 = gimple_cond_rhs (stmt);
9240 enum tree_code cond_code = gimple_cond_code (stmt);
9242 if (cond_code != NE_EXPR
9243 && cond_code != EQ_EXPR
9244 && TREE_CODE (op0) == SSA_NAME
9245 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9246 && is_gimple_min_invariant (op1))
9248 value_range_t *vr = get_value_range (op0);
9250 /* If we have range information for OP0, then we might be
9251 able to simplify this conditional. */
9252 if (vr->type == VR_RANGE)
9254 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
9256 if (new_tree)
9258 if (dump_file)
9260 fprintf (dump_file, "Simplified relational ");
9261 print_gimple_stmt (dump_file, stmt, 0, 0);
9262 fprintf (dump_file, " into ");
9265 gimple_cond_set_code (stmt, EQ_EXPR);
9266 gimple_cond_set_lhs (stmt, op0);
9267 gimple_cond_set_rhs (stmt, new_tree);
9269 update_stmt (stmt);
9271 if (dump_file)
9273 print_gimple_stmt (dump_file, stmt, 0, 0);
9274 fprintf (dump_file, "\n");
9277 return true;
9280 /* Try again after inverting the condition. We only deal
9281 with integral types here, so no need to worry about
9282 issues with inverting FP comparisons. */
9283 cond_code = invert_tree_comparison (cond_code, false);
9284 new_tree = test_for_singularity (cond_code, op0, op1, vr);
9286 if (new_tree)
9288 if (dump_file)
9290 fprintf (dump_file, "Simplified relational ");
9291 print_gimple_stmt (dump_file, stmt, 0, 0);
9292 fprintf (dump_file, " into ");
9295 gimple_cond_set_code (stmt, NE_EXPR);
9296 gimple_cond_set_lhs (stmt, op0);
9297 gimple_cond_set_rhs (stmt, new_tree);
9299 update_stmt (stmt);
9301 if (dump_file)
9303 print_gimple_stmt (dump_file, stmt, 0, 0);
9304 fprintf (dump_file, "\n");
9307 return true;
9312 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9313 see if OP0 was set by a type conversion where the source of
9314 the conversion is another SSA_NAME with a range that fits
9315 into the range of OP0's type.
9317 If so, the conversion is redundant as the earlier SSA_NAME can be
9318 used for the comparison directly if we just massage the constant in the
9319 comparison. */
9320 if (TREE_CODE (op0) == SSA_NAME
9321 && TREE_CODE (op1) == INTEGER_CST)
9323 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9324 tree innerop;
9326 if (!is_gimple_assign (def_stmt)
9327 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9328 return false;
9330 innerop = gimple_assign_rhs1 (def_stmt);
9332 if (TREE_CODE (innerop) == SSA_NAME
9333 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9335 value_range_t *vr = get_value_range (innerop);
9337 if (range_int_cst_p (vr)
9338 && range_fits_type_p (vr,
9339 TYPE_PRECISION (TREE_TYPE (op0)),
9340 TYPE_SIGN (TREE_TYPE (op0)))
9341 && int_fits_type_p (op1, TREE_TYPE (innerop))
9342 /* The range must not have overflowed, or if it did overflow
9343 we must not be wrapping/trapping overflow and optimizing
9344 with strict overflow semantics. */
9345 && ((!is_negative_overflow_infinity (vr->min)
9346 && !is_positive_overflow_infinity (vr->max))
9347 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9349 /* If the range overflowed and the user has asked for warnings
9350 when strict overflow semantics were used to optimize code,
9351 issue an appropriate warning. */
9352 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9353 && (is_negative_overflow_infinity (vr->min)
9354 || is_positive_overflow_infinity (vr->max))
9355 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9357 location_t location;
9359 if (!gimple_has_location (stmt))
9360 location = input_location;
9361 else
9362 location = gimple_location (stmt);
9363 warning_at (location, OPT_Wstrict_overflow,
9364 "assuming signed overflow does not occur when "
9365 "simplifying conditional");
9368 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9369 gimple_cond_set_lhs (stmt, innerop);
9370 gimple_cond_set_rhs (stmt, newconst);
9371 return true;
9376 return false;
9379 /* Simplify a switch statement using the value range of the switch
9380 argument. */
9382 static bool
9383 simplify_switch_using_ranges (gimple stmt)
9385 tree op = gimple_switch_index (stmt);
9386 value_range_t *vr;
9387 bool take_default;
9388 edge e;
9389 edge_iterator ei;
9390 size_t i = 0, j = 0, n, n2;
9391 tree vec2;
9392 switch_update su;
9393 size_t k = 1, l = 0;
9395 if (TREE_CODE (op) == SSA_NAME)
9397 vr = get_value_range (op);
9399 /* We can only handle integer ranges. */
9400 if ((vr->type != VR_RANGE
9401 && vr->type != VR_ANTI_RANGE)
9402 || symbolic_range_p (vr))
9403 return false;
9405 /* Find case label for min/max of the value range. */
9406 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9408 else if (TREE_CODE (op) == INTEGER_CST)
9410 take_default = !find_case_label_index (stmt, 1, op, &i);
9411 if (take_default)
9413 i = 1;
9414 j = 0;
9416 else
9418 j = i;
9421 else
9422 return false;
9424 n = gimple_switch_num_labels (stmt);
9426 /* Bail out if this is just all edges taken. */
9427 if (i == 1
9428 && j == n - 1
9429 && take_default)
9430 return false;
9432 /* Build a new vector of taken case labels. */
9433 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9434 n2 = 0;
9436 /* Add the default edge, if necessary. */
9437 if (take_default)
9438 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9440 for (; i <= j; ++i, ++n2)
9441 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9443 for (; k <= l; ++k, ++n2)
9444 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9446 /* Mark needed edges. */
9447 for (i = 0; i < n2; ++i)
9449 e = find_edge (gimple_bb (stmt),
9450 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9451 e->aux = (void *)-1;
9454 /* Queue not needed edges for later removal. */
9455 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9457 if (e->aux == (void *)-1)
9459 e->aux = NULL;
9460 continue;
9463 if (dump_file && (dump_flags & TDF_DETAILS))
9465 fprintf (dump_file, "removing unreachable case label\n");
9467 to_remove_edges.safe_push (e);
9468 e->flags &= ~EDGE_EXECUTABLE;
9471 /* And queue an update for the stmt. */
9472 su.stmt = stmt;
9473 su.vec = vec2;
9474 to_update_switch_stmts.safe_push (su);
9475 return false;
9478 /* Simplify an integral conversion from an SSA name in STMT. */
9480 static bool
9481 simplify_conversion_using_ranges (gimple stmt)
9483 tree innerop, middleop, finaltype;
9484 gimple def_stmt;
9485 value_range_t *innervr;
9486 signop inner_sgn, middle_sgn, final_sgn;
9487 unsigned inner_prec, middle_prec, final_prec;
9488 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9490 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9491 if (!INTEGRAL_TYPE_P (finaltype))
9492 return false;
9493 middleop = gimple_assign_rhs1 (stmt);
9494 def_stmt = SSA_NAME_DEF_STMT (middleop);
9495 if (!is_gimple_assign (def_stmt)
9496 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9497 return false;
9498 innerop = gimple_assign_rhs1 (def_stmt);
9499 if (TREE_CODE (innerop) != SSA_NAME
9500 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9501 return false;
9503 /* Get the value-range of the inner operand. */
9504 innervr = get_value_range (innerop);
9505 if (innervr->type != VR_RANGE
9506 || TREE_CODE (innervr->min) != INTEGER_CST
9507 || TREE_CODE (innervr->max) != INTEGER_CST)
9508 return false;
9510 /* Simulate the conversion chain to check if the result is equal if
9511 the middle conversion is removed. */
9512 innermin = wi::to_widest (innervr->min);
9513 innermax = wi::to_widest (innervr->max);
9515 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9516 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9517 final_prec = TYPE_PRECISION (finaltype);
9519 /* If the first conversion is not injective, the second must not
9520 be widening. */
9521 if (wi::gtu_p (innermax - innermin,
9522 wi::mask <widest_int> (middle_prec, false))
9523 && middle_prec < final_prec)
9524 return false;
9525 /* We also want a medium value so that we can track the effect that
9526 narrowing conversions with sign change have. */
9527 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9528 if (inner_sgn == UNSIGNED)
9529 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9530 else
9531 innermed = 0;
9532 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9533 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9534 innermed = innermin;
9536 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9537 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9538 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9539 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9541 /* Require that the final conversion applied to both the original
9542 and the intermediate range produces the same result. */
9543 final_sgn = TYPE_SIGN (finaltype);
9544 if (wi::ext (middlemin, final_prec, final_sgn)
9545 != wi::ext (innermin, final_prec, final_sgn)
9546 || wi::ext (middlemed, final_prec, final_sgn)
9547 != wi::ext (innermed, final_prec, final_sgn)
9548 || wi::ext (middlemax, final_prec, final_sgn)
9549 != wi::ext (innermax, final_prec, final_sgn))
9550 return false;
9552 gimple_assign_set_rhs1 (stmt, innerop);
9553 update_stmt (stmt);
9554 return true;
9557 /* Simplify a conversion from integral SSA name to float in STMT. */
9559 static bool
9560 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9562 tree rhs1 = gimple_assign_rhs1 (stmt);
9563 value_range_t *vr = get_value_range (rhs1);
9564 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9565 machine_mode mode;
9566 tree tem;
9567 gimple conv;
9569 /* We can only handle constant ranges. */
9570 if (vr->type != VR_RANGE
9571 || TREE_CODE (vr->min) != INTEGER_CST
9572 || TREE_CODE (vr->max) != INTEGER_CST)
9573 return false;
9575 /* First check if we can use a signed type in place of an unsigned. */
9576 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9577 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9578 != CODE_FOR_nothing)
9579 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9580 mode = TYPE_MODE (TREE_TYPE (rhs1));
9581 /* If we can do the conversion in the current input mode do nothing. */
9582 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9583 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9584 return false;
9585 /* Otherwise search for a mode we can use, starting from the narrowest
9586 integer mode available. */
9587 else
9589 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9592 /* If we cannot do a signed conversion to float from mode
9593 or if the value-range does not fit in the signed type
9594 try with a wider mode. */
9595 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9596 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9597 break;
9599 mode = GET_MODE_WIDER_MODE (mode);
9600 /* But do not widen the input. Instead leave that to the
9601 optabs expansion code. */
9602 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9603 return false;
9605 while (mode != VOIDmode);
9606 if (mode == VOIDmode)
9607 return false;
9610 /* It works, insert a truncation or sign-change before the
9611 float conversion. */
9612 tem = make_ssa_name (build_nonstandard_integer_type
9613 (GET_MODE_PRECISION (mode), 0), NULL);
9614 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
9615 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9616 gimple_assign_set_rhs1 (stmt, tem);
9617 update_stmt (stmt);
9619 return true;
9622 /* Simplify an internal fn call using ranges if possible. */
9624 static bool
9625 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9627 enum tree_code subcode;
9628 bool is_ubsan = false;
9629 bool ovf = false;
9630 switch (gimple_call_internal_fn (stmt))
9632 case IFN_UBSAN_CHECK_ADD:
9633 subcode = PLUS_EXPR;
9634 is_ubsan = true;
9635 break;
9636 case IFN_UBSAN_CHECK_SUB:
9637 subcode = MINUS_EXPR;
9638 is_ubsan = true;
9639 break;
9640 case IFN_UBSAN_CHECK_MUL:
9641 subcode = MULT_EXPR;
9642 is_ubsan = true;
9643 break;
9644 case IFN_ADD_OVERFLOW:
9645 subcode = PLUS_EXPR;
9646 break;
9647 case IFN_SUB_OVERFLOW:
9648 subcode = MINUS_EXPR;
9649 break;
9650 case IFN_MUL_OVERFLOW:
9651 subcode = MULT_EXPR;
9652 break;
9653 default:
9654 return false;
9657 tree op0 = gimple_call_arg (stmt, 0);
9658 tree op1 = gimple_call_arg (stmt, 1);
9659 tree type;
9660 if (is_ubsan)
9661 type = TREE_TYPE (op0);
9662 else if (gimple_call_lhs (stmt) == NULL_TREE)
9663 return false;
9664 else
9665 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9666 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9667 || (is_ubsan && ovf))
9668 return false;
9670 gimple g;
9671 location_t loc = gimple_location (stmt);
9672 if (is_ubsan)
9673 g = gimple_build_assign_with_ops (subcode, gimple_call_lhs (stmt),
9674 op0, op1);
9675 else
9677 int prec = TYPE_PRECISION (type);
9678 tree utype = type;
9679 if (ovf
9680 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9681 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9682 utype = build_nonstandard_integer_type (prec, 1);
9683 if (TREE_CODE (op0) == INTEGER_CST)
9684 op0 = fold_convert (utype, op0);
9685 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9687 g = gimple_build_assign_with_ops (NOP_EXPR,
9688 make_ssa_name (utype, NULL),
9689 op0, NULL_TREE);
9690 gimple_set_location (g, loc);
9691 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9692 op0 = gimple_assign_lhs (g);
9694 if (TREE_CODE (op1) == INTEGER_CST)
9695 op1 = fold_convert (utype, op1);
9696 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9698 g = gimple_build_assign_with_ops (NOP_EXPR,
9699 make_ssa_name (utype, NULL),
9700 op1, NULL_TREE);
9701 gimple_set_location (g, loc);
9702 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9703 op1 = gimple_assign_lhs (g);
9705 g = gimple_build_assign_with_ops (subcode, make_ssa_name (utype, NULL),
9706 op0, op1);
9707 gimple_set_location (g, loc);
9708 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9709 if (utype != type)
9711 g = gimple_build_assign_with_ops (NOP_EXPR,
9712 make_ssa_name (type, NULL),
9713 gimple_assign_lhs (g), NULL_TREE);
9714 gimple_set_location (g, loc);
9715 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9717 g = gimple_build_assign_with_ops (COMPLEX_EXPR, gimple_call_lhs (stmt),
9718 gimple_assign_lhs (g),
9719 build_int_cst (type, ovf));
9721 gimple_set_location (g, loc);
9722 gsi_replace (gsi, g, false);
9723 return true;
9726 /* Simplify STMT using ranges if possible. */
9728 static bool
9729 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9731 gimple stmt = gsi_stmt (*gsi);
9732 if (is_gimple_assign (stmt))
9734 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9735 tree rhs1 = gimple_assign_rhs1 (stmt);
9737 switch (rhs_code)
9739 case EQ_EXPR:
9740 case NE_EXPR:
9741 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9742 if the RHS is zero or one, and the LHS are known to be boolean
9743 values. */
9744 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9745 return simplify_truth_ops_using_ranges (gsi, stmt);
9746 break;
9748 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9749 and BIT_AND_EXPR respectively if the first operand is greater
9750 than zero and the second operand is an exact power of two. */
9751 case TRUNC_DIV_EXPR:
9752 case TRUNC_MOD_EXPR:
9753 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9754 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9755 return simplify_div_or_mod_using_ranges (stmt);
9756 break;
9758 /* Transform ABS (X) into X or -X as appropriate. */
9759 case ABS_EXPR:
9760 if (TREE_CODE (rhs1) == SSA_NAME
9761 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9762 return simplify_abs_using_ranges (stmt);
9763 break;
9765 case BIT_AND_EXPR:
9766 case BIT_IOR_EXPR:
9767 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9768 if all the bits being cleared are already cleared or
9769 all the bits being set are already set. */
9770 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9771 return simplify_bit_ops_using_ranges (gsi, stmt);
9772 break;
9774 CASE_CONVERT:
9775 if (TREE_CODE (rhs1) == SSA_NAME
9776 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9777 return simplify_conversion_using_ranges (stmt);
9778 break;
9780 case FLOAT_EXPR:
9781 if (TREE_CODE (rhs1) == SSA_NAME
9782 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9783 return simplify_float_conversion_using_ranges (gsi, stmt);
9784 break;
9786 default:
9787 break;
9790 else if (gimple_code (stmt) == GIMPLE_COND)
9791 return simplify_cond_using_ranges (stmt);
9792 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9793 return simplify_switch_using_ranges (stmt);
9794 else if (is_gimple_call (stmt)
9795 && gimple_call_internal_p (stmt))
9796 return simplify_internal_call_using_ranges (gsi, stmt);
9798 return false;
9801 /* If the statement pointed by SI has a predicate whose value can be
9802 computed using the value range information computed by VRP, compute
9803 its value and return true. Otherwise, return false. */
9805 static bool
9806 fold_predicate_in (gimple_stmt_iterator *si)
9808 bool assignment_p = false;
9809 tree val;
9810 gimple stmt = gsi_stmt (*si);
9812 if (is_gimple_assign (stmt)
9813 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9815 assignment_p = true;
9816 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9817 gimple_assign_rhs1 (stmt),
9818 gimple_assign_rhs2 (stmt),
9819 stmt);
9821 else if (gimple_code (stmt) == GIMPLE_COND)
9822 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
9823 gimple_cond_lhs (stmt),
9824 gimple_cond_rhs (stmt),
9825 stmt);
9826 else
9827 return false;
9829 if (val)
9831 if (assignment_p)
9832 val = fold_convert (gimple_expr_type (stmt), val);
9834 if (dump_file)
9836 fprintf (dump_file, "Folding predicate ");
9837 print_gimple_expr (dump_file, stmt, 0, 0);
9838 fprintf (dump_file, " to ");
9839 print_generic_expr (dump_file, val, 0);
9840 fprintf (dump_file, "\n");
9843 if (is_gimple_assign (stmt))
9844 gimple_assign_set_rhs_from_tree (si, val);
9845 else
9847 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9848 if (integer_zerop (val))
9849 gimple_cond_make_false (stmt);
9850 else if (integer_onep (val))
9851 gimple_cond_make_true (stmt);
9852 else
9853 gcc_unreachable ();
9856 return true;
9859 return false;
9862 /* Callback for substitute_and_fold folding the stmt at *SI. */
9864 static bool
9865 vrp_fold_stmt (gimple_stmt_iterator *si)
9867 if (fold_predicate_in (si))
9868 return true;
9870 return simplify_stmt_using_ranges (si);
9873 /* Stack of dest,src equivalency pairs that need to be restored after
9874 each attempt to thread a block's incoming edge to an outgoing edge.
9876 A NULL entry is used to mark the end of pairs which need to be
9877 restored. */
9878 static vec<tree> equiv_stack;
9880 /* A trivial wrapper so that we can present the generic jump threading
9881 code with a simple API for simplifying statements. STMT is the
9882 statement we want to simplify, WITHIN_STMT provides the location
9883 for any overflow warnings. */
9885 static tree
9886 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9888 if (gimple_code (stmt) == GIMPLE_COND)
9889 return vrp_evaluate_conditional (gimple_cond_code (stmt),
9890 gimple_cond_lhs (stmt),
9891 gimple_cond_rhs (stmt), within_stmt);
9893 if (gimple_code (stmt) == GIMPLE_ASSIGN)
9895 value_range_t new_vr = VR_INITIALIZER;
9896 tree lhs = gimple_assign_lhs (stmt);
9898 if (TREE_CODE (lhs) == SSA_NAME
9899 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
9900 || POINTER_TYPE_P (TREE_TYPE (lhs))))
9902 extract_range_from_assignment (&new_vr, stmt);
9903 if (range_int_cst_singleton_p (&new_vr))
9904 return new_vr.min;
9908 return NULL_TREE;
9911 /* Blocks which have more than one predecessor and more than
9912 one successor present jump threading opportunities, i.e.,
9913 when the block is reached from a specific predecessor, we
9914 may be able to determine which of the outgoing edges will
9915 be traversed. When this optimization applies, we are able
9916 to avoid conditionals at runtime and we may expose secondary
9917 optimization opportunities.
9919 This routine is effectively a driver for the generic jump
9920 threading code. It basically just presents the generic code
9921 with edges that may be suitable for jump threading.
9923 Unlike DOM, we do not iterate VRP if jump threading was successful.
9924 While iterating may expose new opportunities for VRP, it is expected
9925 those opportunities would be very limited and the compile time cost
9926 to expose those opportunities would be significant.
9928 As jump threading opportunities are discovered, they are registered
9929 for later realization. */
9931 static void
9932 identify_jump_threads (void)
9934 basic_block bb;
9935 gimple dummy;
9936 int i;
9937 edge e;
9939 /* Ugh. When substituting values earlier in this pass we can
9940 wipe the dominance information. So rebuild the dominator
9941 information as we need it within the jump threading code. */
9942 calculate_dominance_info (CDI_DOMINATORS);
9944 /* We do not allow VRP information to be used for jump threading
9945 across a back edge in the CFG. Otherwise it becomes too
9946 difficult to avoid eliminating loop exit tests. Of course
9947 EDGE_DFS_BACK is not accurate at this time so we have to
9948 recompute it. */
9949 mark_dfs_back_edges ();
9951 /* Do not thread across edges we are about to remove. Just marking
9952 them as EDGE_DFS_BACK will do. */
9953 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9954 e->flags |= EDGE_DFS_BACK;
9956 /* Allocate our unwinder stack to unwind any temporary equivalences
9957 that might be recorded. */
9958 equiv_stack.create (20);
9960 /* To avoid lots of silly node creation, we create a single
9961 conditional and just modify it in-place when attempting to
9962 thread jumps. */
9963 dummy = gimple_build_cond (EQ_EXPR,
9964 integer_zero_node, integer_zero_node,
9965 NULL, NULL);
9967 /* Walk through all the blocks finding those which present a
9968 potential jump threading opportunity. We could set this up
9969 as a dominator walker and record data during the walk, but
9970 I doubt it's worth the effort for the classes of jump
9971 threading opportunities we are trying to identify at this
9972 point in compilation. */
9973 FOR_EACH_BB_FN (bb, cfun)
9975 gimple last;
9977 /* If the generic jump threading code does not find this block
9978 interesting, then there is nothing to do. */
9979 if (! potentially_threadable_block (bb))
9980 continue;
9982 /* We only care about blocks ending in a COND_EXPR. While there
9983 may be some value in handling SWITCH_EXPR here, I doubt it's
9984 terribly important. */
9985 last = gsi_stmt (gsi_last_bb (bb));
9987 /* We're basically looking for a switch or any kind of conditional with
9988 integral or pointer type arguments. Note the type of the second
9989 argument will be the same as the first argument, so no need to
9990 check it explicitly. */
9991 if (gimple_code (last) == GIMPLE_SWITCH
9992 || (gimple_code (last) == GIMPLE_COND
9993 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9994 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9995 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9996 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9997 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9999 edge_iterator ei;
10001 /* We've got a block with multiple predecessors and multiple
10002 successors which also ends in a suitable conditional or
10003 switch statement. For each predecessor, see if we can thread
10004 it to a specific successor. */
10005 FOR_EACH_EDGE (e, ei, bb->preds)
10007 /* Do not thread across back edges or abnormal edges
10008 in the CFG. */
10009 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
10010 continue;
10012 thread_across_edge (dummy, e, true, &equiv_stack,
10013 simplify_stmt_for_jump_threading);
10018 /* We do not actually update the CFG or SSA graphs at this point as
10019 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10020 handle ASSERT_EXPRs gracefully. */
10023 /* We identified all the jump threading opportunities earlier, but could
10024 not transform the CFG at that time. This routine transforms the
10025 CFG and arranges for the dominator tree to be rebuilt if necessary.
10027 Note the SSA graph update will occur during the normal TODO
10028 processing by the pass manager. */
10029 static void
10030 finalize_jump_threads (void)
10032 thread_through_all_blocks (false);
10033 equiv_stack.release ();
10037 /* Traverse all the blocks folding conditionals with known ranges. */
10039 static void
10040 vrp_finalize (void)
10042 size_t i;
10044 values_propagated = true;
10046 if (dump_file)
10048 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10049 dump_all_value_ranges (dump_file);
10050 fprintf (dump_file, "\n");
10053 substitute_and_fold (op_with_constant_singleton_value_range,
10054 vrp_fold_stmt, false);
10056 if (warn_array_bounds)
10057 check_all_array_refs ();
10059 /* We must identify jump threading opportunities before we release
10060 the datastructures built by VRP. */
10061 identify_jump_threads ();
10063 /* Set value range to non pointer SSA_NAMEs. */
10064 for (i = 0; i < num_vr_values; i++)
10065 if (vr_value[i])
10067 tree name = ssa_name (i);
10069 if (!name
10070 || POINTER_TYPE_P (TREE_TYPE (name))
10071 || (vr_value[i]->type == VR_VARYING)
10072 || (vr_value[i]->type == VR_UNDEFINED))
10073 continue;
10075 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10076 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10077 && (vr_value[i]->type == VR_RANGE
10078 || vr_value[i]->type == VR_ANTI_RANGE))
10079 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10080 vr_value[i]->max);
10083 /* Free allocated memory. */
10084 for (i = 0; i < num_vr_values; i++)
10085 if (vr_value[i])
10087 BITMAP_FREE (vr_value[i]->equiv);
10088 free (vr_value[i]);
10091 free (vr_value);
10092 free (vr_phi_edge_counts);
10094 /* So that we can distinguish between VRP data being available
10095 and not available. */
10096 vr_value = NULL;
10097 vr_phi_edge_counts = NULL;
10101 /* Main entry point to VRP (Value Range Propagation). This pass is
10102 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10103 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10104 Programming Language Design and Implementation, pp. 67-78, 1995.
10105 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10107 This is essentially an SSA-CCP pass modified to deal with ranges
10108 instead of constants.
10110 While propagating ranges, we may find that two or more SSA name
10111 have equivalent, though distinct ranges. For instance,
10113 1 x_9 = p_3->a;
10114 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10115 3 if (p_4 == q_2)
10116 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10117 5 endif
10118 6 if (q_2)
10120 In the code above, pointer p_5 has range [q_2, q_2], but from the
10121 code we can also determine that p_5 cannot be NULL and, if q_2 had
10122 a non-varying range, p_5's range should also be compatible with it.
10124 These equivalences are created by two expressions: ASSERT_EXPR and
10125 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10126 result of another assertion, then we can use the fact that p_5 and
10127 p_4 are equivalent when evaluating p_5's range.
10129 Together with value ranges, we also propagate these equivalences
10130 between names so that we can take advantage of information from
10131 multiple ranges when doing final replacement. Note that this
10132 equivalency relation is transitive but not symmetric.
10134 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10135 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10136 in contexts where that assertion does not hold (e.g., in line 6).
10138 TODO, the main difference between this pass and Patterson's is that
10139 we do not propagate edge probabilities. We only compute whether
10140 edges can be taken or not. That is, instead of having a spectrum
10141 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10142 DON'T KNOW. In the future, it may be worthwhile to propagate
10143 probabilities to aid branch prediction. */
10145 static unsigned int
10146 execute_vrp (void)
10148 int i;
10149 edge e;
10150 switch_update *su;
10152 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10153 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10154 scev_initialize ();
10156 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10157 Inserting assertions may split edges which will invalidate
10158 EDGE_DFS_BACK. */
10159 insert_range_assertions ();
10161 to_remove_edges.create (10);
10162 to_update_switch_stmts.create (5);
10163 threadedge_initialize_values ();
10165 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10166 mark_dfs_back_edges ();
10168 vrp_initialize ();
10169 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10170 vrp_finalize ();
10172 free_numbers_of_iterations_estimates ();
10174 /* ASSERT_EXPRs must be removed before finalizing jump threads
10175 as finalizing jump threads calls the CFG cleanup code which
10176 does not properly handle ASSERT_EXPRs. */
10177 remove_range_assertions ();
10179 /* If we exposed any new variables, go ahead and put them into
10180 SSA form now, before we handle jump threading. This simplifies
10181 interactions between rewriting of _DECL nodes into SSA form
10182 and rewriting SSA_NAME nodes into SSA form after block
10183 duplication and CFG manipulation. */
10184 update_ssa (TODO_update_ssa);
10186 finalize_jump_threads ();
10188 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10189 CFG in a broken state and requires a cfg_cleanup run. */
10190 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10191 remove_edge (e);
10192 /* Update SWITCH_EXPR case label vector. */
10193 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10195 size_t j;
10196 size_t n = TREE_VEC_LENGTH (su->vec);
10197 tree label;
10198 gimple_switch_set_num_labels (su->stmt, n);
10199 for (j = 0; j < n; j++)
10200 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10201 /* As we may have replaced the default label with a regular one
10202 make sure to make it a real default label again. This ensures
10203 optimal expansion. */
10204 label = gimple_switch_label (su->stmt, 0);
10205 CASE_LOW (label) = NULL_TREE;
10206 CASE_HIGH (label) = NULL_TREE;
10209 if (to_remove_edges.length () > 0)
10211 free_dominance_info (CDI_DOMINATORS);
10212 loops_state_set (LOOPS_NEED_FIXUP);
10215 to_remove_edges.release ();
10216 to_update_switch_stmts.release ();
10217 threadedge_finalize_values ();
10219 scev_finalize ();
10220 loop_optimizer_finalize ();
10221 return 0;
10224 namespace {
10226 const pass_data pass_data_vrp =
10228 GIMPLE_PASS, /* type */
10229 "vrp", /* name */
10230 OPTGROUP_NONE, /* optinfo_flags */
10231 TV_TREE_VRP, /* tv_id */
10232 PROP_ssa, /* properties_required */
10233 0, /* properties_provided */
10234 0, /* properties_destroyed */
10235 0, /* todo_flags_start */
10236 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10239 class pass_vrp : public gimple_opt_pass
10241 public:
10242 pass_vrp (gcc::context *ctxt)
10243 : gimple_opt_pass (pass_data_vrp, ctxt)
10246 /* opt_pass methods: */
10247 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10248 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10249 virtual unsigned int execute (function *) { return execute_vrp (); }
10251 }; // class pass_vrp
10253 } // anon namespace
10255 gimple_opt_pass *
10256 make_pass_vrp (gcc::context *ctxt)
10258 return new pass_vrp (ctxt);