Merge branch 'master' r216746-r217593 into gimple-classes-v2-option-3
[official-gcc.git] / gcc / tree-vrp.c
bloba9efcf0ccbd373136e74584c32ffcb836d7106d4
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 gswitch *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, gassign *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, gassign *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 gassign *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, gcond *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, gswitch *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 last;
6084 last = last_stmt (bb);
6086 /* If BB's last statement is a conditional statement involving integer
6087 operands, determine if we need to add ASSERT_EXPRs. */
6088 if (last
6089 && gimple_code (last) == GIMPLE_COND
6090 && !fp_predicate (last)
6091 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6092 find_conditional_asserts (bb, as_a <gcond *> (last));
6094 /* If BB's last statement is a switch statement involving integer
6095 operands, determine if we need to add ASSERT_EXPRs. */
6096 if (last
6097 && gimple_code (last) == GIMPLE_SWITCH
6098 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6099 find_switch_asserts (bb, as_a <gswitch *> (last));
6101 /* Traverse all the statements in BB marking used names and looking
6102 for statements that may infer assertions for their used operands. */
6103 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6104 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 (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6175 gsi_next (&si))
6177 use_operand_p arg_p;
6178 ssa_op_iter i;
6179 gphi *phi = si.phi ();
6180 tree res = gimple_phi_result (phi);
6182 if (virtual_operand_p (res))
6183 continue;
6185 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6187 tree arg = USE_FROM_PTR (arg_p);
6188 if (TREE_CODE (arg) == SSA_NAME)
6189 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6192 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6196 /* Do an RPO walk over the function computing SSA name liveness
6197 on-the-fly and deciding on assert expressions to insert. */
6199 static void
6200 find_assert_locations (void)
6202 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6203 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6204 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6205 int rpo_cnt, i;
6207 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6208 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6209 for (i = 0; i < rpo_cnt; ++i)
6210 bb_rpo[rpo[i]] = i;
6212 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6213 the order we compute liveness and insert asserts we otherwise
6214 fail to insert asserts into the loop latch. */
6215 loop_p loop;
6216 FOR_EACH_LOOP (loop, 0)
6218 i = loop->latch->index;
6219 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6220 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6221 !gsi_end_p (gsi); gsi_next (&gsi))
6223 gphi *phi = gsi.phi ();
6224 if (virtual_operand_p (gimple_phi_result (phi)))
6225 continue;
6226 tree arg = gimple_phi_arg_def (phi, j);
6227 if (TREE_CODE (arg) == SSA_NAME)
6229 if (live[i] == NULL)
6231 live[i] = sbitmap_alloc (num_ssa_names);
6232 bitmap_clear (live[i]);
6234 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6239 for (i = rpo_cnt - 1; i >= 0; --i)
6241 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6242 edge e;
6243 edge_iterator ei;
6245 if (!live[rpo[i]])
6247 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6248 bitmap_clear (live[rpo[i]]);
6251 /* Process BB and update the live information with uses in
6252 this block. */
6253 find_assert_locations_1 (bb, live[rpo[i]]);
6255 /* Merge liveness into the predecessor blocks and free it. */
6256 if (!bitmap_empty_p (live[rpo[i]]))
6258 int pred_rpo = i;
6259 FOR_EACH_EDGE (e, ei, bb->preds)
6261 int pred = e->src->index;
6262 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6263 continue;
6265 if (!live[pred])
6267 live[pred] = sbitmap_alloc (num_ssa_names);
6268 bitmap_clear (live[pred]);
6270 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6272 if (bb_rpo[pred] < pred_rpo)
6273 pred_rpo = bb_rpo[pred];
6276 /* Record the RPO number of the last visited block that needs
6277 live information from this block. */
6278 last_rpo[rpo[i]] = pred_rpo;
6280 else
6282 sbitmap_free (live[rpo[i]]);
6283 live[rpo[i]] = NULL;
6286 /* We can free all successors live bitmaps if all their
6287 predecessors have been visited already. */
6288 FOR_EACH_EDGE (e, ei, bb->succs)
6289 if (last_rpo[e->dest->index] == i
6290 && live[e->dest->index])
6292 sbitmap_free (live[e->dest->index]);
6293 live[e->dest->index] = NULL;
6297 XDELETEVEC (rpo);
6298 XDELETEVEC (bb_rpo);
6299 XDELETEVEC (last_rpo);
6300 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6301 if (live[i])
6302 sbitmap_free (live[i]);
6303 XDELETEVEC (live);
6306 /* Create an ASSERT_EXPR for NAME and insert it in the location
6307 indicated by LOC. Return true if we made any edge insertions. */
6309 static bool
6310 process_assert_insertions_for (tree name, assert_locus_t loc)
6312 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6313 gimple stmt;
6314 tree cond;
6315 gimple assert_stmt;
6316 edge_iterator ei;
6317 edge e;
6319 /* If we have X <=> X do not insert an assert expr for that. */
6320 if (loc->expr == loc->val)
6321 return false;
6323 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6324 assert_stmt = build_assert_expr_for (cond, name);
6325 if (loc->e)
6327 /* We have been asked to insert the assertion on an edge. This
6328 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6329 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6330 || (gimple_code (gsi_stmt (loc->si))
6331 == GIMPLE_SWITCH));
6333 gsi_insert_on_edge (loc->e, assert_stmt);
6334 return true;
6337 /* Otherwise, we can insert right after LOC->SI iff the
6338 statement must not be the last statement in the block. */
6339 stmt = gsi_stmt (loc->si);
6340 if (!stmt_ends_bb_p (stmt))
6342 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6343 return false;
6346 /* If STMT must be the last statement in BB, we can only insert new
6347 assertions on the non-abnormal edge out of BB. Note that since
6348 STMT is not control flow, there may only be one non-abnormal edge
6349 out of BB. */
6350 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6351 if (!(e->flags & EDGE_ABNORMAL))
6353 gsi_insert_on_edge (e, assert_stmt);
6354 return true;
6357 gcc_unreachable ();
6361 /* Process all the insertions registered for every name N_i registered
6362 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6363 found in ASSERTS_FOR[i]. */
6365 static void
6366 process_assert_insertions (void)
6368 unsigned i;
6369 bitmap_iterator bi;
6370 bool update_edges_p = false;
6371 int num_asserts = 0;
6373 if (dump_file && (dump_flags & TDF_DETAILS))
6374 dump_all_asserts (dump_file);
6376 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6378 assert_locus_t loc = asserts_for[i];
6379 gcc_assert (loc);
6381 while (loc)
6383 assert_locus_t next = loc->next;
6384 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6385 free (loc);
6386 loc = next;
6387 num_asserts++;
6391 if (update_edges_p)
6392 gsi_commit_edge_inserts ();
6394 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6395 num_asserts);
6399 /* Traverse the flowgraph looking for conditional jumps to insert range
6400 expressions. These range expressions are meant to provide information
6401 to optimizations that need to reason in terms of value ranges. They
6402 will not be expanded into RTL. For instance, given:
6404 x = ...
6405 y = ...
6406 if (x < y)
6407 y = x - 2;
6408 else
6409 x = y + 3;
6411 this pass will transform the code into:
6413 x = ...
6414 y = ...
6415 if (x < y)
6417 x = ASSERT_EXPR <x, x < y>
6418 y = x - 2
6420 else
6422 y = ASSERT_EXPR <y, x >= y>
6423 x = y + 3
6426 The idea is that once copy and constant propagation have run, other
6427 optimizations will be able to determine what ranges of values can 'x'
6428 take in different paths of the code, simply by checking the reaching
6429 definition of 'x'. */
6431 static void
6432 insert_range_assertions (void)
6434 need_assert_for = BITMAP_ALLOC (NULL);
6435 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6437 calculate_dominance_info (CDI_DOMINATORS);
6439 find_assert_locations ();
6440 if (!bitmap_empty_p (need_assert_for))
6442 process_assert_insertions ();
6443 update_ssa (TODO_update_ssa_no_phi);
6446 if (dump_file && (dump_flags & TDF_DETAILS))
6448 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6449 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6452 free (asserts_for);
6453 BITMAP_FREE (need_assert_for);
6456 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6457 and "struct" hacks. If VRP can determine that the
6458 array subscript is a constant, check if it is outside valid
6459 range. If the array subscript is a RANGE, warn if it is
6460 non-overlapping with valid range.
6461 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6463 static void
6464 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6466 value_range_t* vr = NULL;
6467 tree low_sub, up_sub;
6468 tree low_bound, up_bound, up_bound_p1;
6469 tree base;
6471 if (TREE_NO_WARNING (ref))
6472 return;
6474 low_sub = up_sub = TREE_OPERAND (ref, 1);
6475 up_bound = array_ref_up_bound (ref);
6477 /* Can not check flexible arrays. */
6478 if (!up_bound
6479 || TREE_CODE (up_bound) != INTEGER_CST)
6480 return;
6482 /* Accesses to trailing arrays via pointers may access storage
6483 beyond the types array bounds. */
6484 base = get_base_address (ref);
6485 if (base && TREE_CODE (base) == MEM_REF)
6487 tree cref, next = NULL_TREE;
6489 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6490 return;
6492 cref = TREE_OPERAND (ref, 0);
6493 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6494 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6495 next && TREE_CODE (next) != FIELD_DECL;
6496 next = DECL_CHAIN (next))
6499 /* If this is the last field in a struct type or a field in a
6500 union type do not warn. */
6501 if (!next)
6502 return;
6505 low_bound = array_ref_low_bound (ref);
6506 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6507 build_int_cst (TREE_TYPE (up_bound), 1));
6509 if (TREE_CODE (low_sub) == SSA_NAME)
6511 vr = get_value_range (low_sub);
6512 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6514 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6515 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6519 if (vr && vr->type == VR_ANTI_RANGE)
6521 if (TREE_CODE (up_sub) == INTEGER_CST
6522 && tree_int_cst_lt (up_bound, up_sub)
6523 && TREE_CODE (low_sub) == INTEGER_CST
6524 && tree_int_cst_lt (low_sub, low_bound))
6526 warning_at (location, OPT_Warray_bounds,
6527 "array subscript is outside array bounds");
6528 TREE_NO_WARNING (ref) = 1;
6531 else if (TREE_CODE (up_sub) == INTEGER_CST
6532 && (ignore_off_by_one
6533 ? (tree_int_cst_lt (up_bound, up_sub)
6534 && !tree_int_cst_equal (up_bound_p1, up_sub))
6535 : (tree_int_cst_lt (up_bound, up_sub)
6536 || tree_int_cst_equal (up_bound_p1, up_sub))))
6538 if (dump_file && (dump_flags & TDF_DETAILS))
6540 fprintf (dump_file, "Array bound warning for ");
6541 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6542 fprintf (dump_file, "\n");
6544 warning_at (location, OPT_Warray_bounds,
6545 "array subscript is above array bounds");
6546 TREE_NO_WARNING (ref) = 1;
6548 else if (TREE_CODE (low_sub) == INTEGER_CST
6549 && tree_int_cst_lt (low_sub, low_bound))
6551 if (dump_file && (dump_flags & TDF_DETAILS))
6553 fprintf (dump_file, "Array bound warning for ");
6554 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6555 fprintf (dump_file, "\n");
6557 warning_at (location, OPT_Warray_bounds,
6558 "array subscript is below array bounds");
6559 TREE_NO_WARNING (ref) = 1;
6563 /* Searches if the expr T, located at LOCATION computes
6564 address of an ARRAY_REF, and call check_array_ref on it. */
6566 static void
6567 search_for_addr_array (tree t, location_t location)
6569 while (TREE_CODE (t) == SSA_NAME)
6571 gimple g = SSA_NAME_DEF_STMT (t);
6573 if (gimple_code (g) != GIMPLE_ASSIGN)
6574 return;
6576 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6577 != GIMPLE_SINGLE_RHS)
6578 return;
6580 t = gimple_assign_rhs1 (g);
6584 /* We are only interested in addresses of ARRAY_REF's. */
6585 if (TREE_CODE (t) != ADDR_EXPR)
6586 return;
6588 /* Check each ARRAY_REFs in the reference chain. */
6591 if (TREE_CODE (t) == ARRAY_REF)
6592 check_array_ref (location, t, true /*ignore_off_by_one*/);
6594 t = TREE_OPERAND (t, 0);
6596 while (handled_component_p (t));
6598 if (TREE_CODE (t) == MEM_REF
6599 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6600 && !TREE_NO_WARNING (t))
6602 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6603 tree low_bound, up_bound, el_sz;
6604 offset_int idx;
6605 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6606 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6607 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6608 return;
6610 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6611 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6612 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6613 if (!low_bound
6614 || TREE_CODE (low_bound) != INTEGER_CST
6615 || !up_bound
6616 || TREE_CODE (up_bound) != INTEGER_CST
6617 || !el_sz
6618 || TREE_CODE (el_sz) != INTEGER_CST)
6619 return;
6621 idx = mem_ref_offset (t);
6622 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6623 if (wi::lts_p (idx, 0))
6625 if (dump_file && (dump_flags & TDF_DETAILS))
6627 fprintf (dump_file, "Array bound warning for ");
6628 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6629 fprintf (dump_file, "\n");
6631 warning_at (location, OPT_Warray_bounds,
6632 "array subscript is below array bounds");
6633 TREE_NO_WARNING (t) = 1;
6635 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6636 - wi::to_offset (low_bound) + 1)))
6638 if (dump_file && (dump_flags & TDF_DETAILS))
6640 fprintf (dump_file, "Array bound warning for ");
6641 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6642 fprintf (dump_file, "\n");
6644 warning_at (location, OPT_Warray_bounds,
6645 "array subscript is above array bounds");
6646 TREE_NO_WARNING (t) = 1;
6651 /* walk_tree() callback that checks if *TP is
6652 an ARRAY_REF inside an ADDR_EXPR (in which an array
6653 subscript one outside the valid range is allowed). Call
6654 check_array_ref for each ARRAY_REF found. The location is
6655 passed in DATA. */
6657 static tree
6658 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6660 tree t = *tp;
6661 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6662 location_t location;
6664 if (EXPR_HAS_LOCATION (t))
6665 location = EXPR_LOCATION (t);
6666 else
6668 location_t *locp = (location_t *) wi->info;
6669 location = *locp;
6672 *walk_subtree = TRUE;
6674 if (TREE_CODE (t) == ARRAY_REF)
6675 check_array_ref (location, t, false /*ignore_off_by_one*/);
6677 if (TREE_CODE (t) == MEM_REF
6678 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6679 search_for_addr_array (TREE_OPERAND (t, 0), location);
6681 if (TREE_CODE (t) == ADDR_EXPR)
6682 *walk_subtree = FALSE;
6684 return NULL_TREE;
6687 /* Walk over all statements of all reachable BBs and call check_array_bounds
6688 on them. */
6690 static void
6691 check_all_array_refs (void)
6693 basic_block bb;
6694 gimple_stmt_iterator si;
6696 FOR_EACH_BB_FN (bb, cfun)
6698 edge_iterator ei;
6699 edge e;
6700 bool executable = false;
6702 /* Skip blocks that were found to be unreachable. */
6703 FOR_EACH_EDGE (e, ei, bb->preds)
6704 executable |= !!(e->flags & EDGE_EXECUTABLE);
6705 if (!executable)
6706 continue;
6708 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6710 gimple stmt = gsi_stmt (si);
6711 struct walk_stmt_info wi;
6712 if (!gimple_has_location (stmt))
6713 continue;
6715 if (is_gimple_call (stmt))
6717 size_t i;
6718 size_t n = gimple_call_num_args (stmt);
6719 for (i = 0; i < n; i++)
6721 tree arg = gimple_call_arg (stmt, i);
6722 search_for_addr_array (arg, gimple_location (stmt));
6725 else
6727 memset (&wi, 0, sizeof (wi));
6728 wi.info = CONST_CAST (void *, (const void *)
6729 gimple_location_ptr (stmt));
6731 walk_gimple_op (gsi_stmt (si),
6732 check_array_bounds,
6733 &wi);
6739 /* Return true if all imm uses of VAR are either in STMT, or
6740 feed (optionally through a chain of single imm uses) GIMPLE_COND
6741 in basic block COND_BB. */
6743 static bool
6744 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6746 use_operand_p use_p, use2_p;
6747 imm_use_iterator iter;
6749 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6750 if (USE_STMT (use_p) != stmt)
6752 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6753 if (is_gimple_debug (use_stmt))
6754 continue;
6755 while (is_gimple_assign (use_stmt)
6756 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6757 && single_imm_use (gimple_assign_lhs (use_stmt),
6758 &use2_p, &use_stmt2))
6759 use_stmt = use_stmt2;
6760 if (gimple_code (use_stmt) != GIMPLE_COND
6761 || gimple_bb (use_stmt) != cond_bb)
6762 return false;
6764 return true;
6767 /* Handle
6768 _4 = x_3 & 31;
6769 if (_4 != 0)
6770 goto <bb 6>;
6771 else
6772 goto <bb 7>;
6773 <bb 6>:
6774 __builtin_unreachable ();
6775 <bb 7>:
6776 x_5 = ASSERT_EXPR <x_3, ...>;
6777 If x_3 has no other immediate uses (checked by caller),
6778 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6779 from the non-zero bitmask. */
6781 static void
6782 maybe_set_nonzero_bits (basic_block bb, tree var)
6784 edge e = single_pred_edge (bb);
6785 basic_block cond_bb = e->src;
6786 gimple stmt = last_stmt (cond_bb);
6787 tree cst;
6789 if (stmt == NULL
6790 || gimple_code (stmt) != GIMPLE_COND
6791 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6792 ? EQ_EXPR : NE_EXPR)
6793 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6794 || !integer_zerop (gimple_cond_rhs (stmt)))
6795 return;
6797 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6798 if (!is_gimple_assign (stmt)
6799 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6800 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6801 return;
6802 if (gimple_assign_rhs1 (stmt) != var)
6804 gimple stmt2;
6806 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6807 return;
6808 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6809 if (!gimple_assign_cast_p (stmt2)
6810 || gimple_assign_rhs1 (stmt2) != var
6811 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6812 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6813 != TYPE_PRECISION (TREE_TYPE (var))))
6814 return;
6816 cst = gimple_assign_rhs2 (stmt);
6817 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6820 /* Convert range assertion expressions into the implied copies and
6821 copy propagate away the copies. Doing the trivial copy propagation
6822 here avoids the need to run the full copy propagation pass after
6823 VRP.
6825 FIXME, this will eventually lead to copy propagation removing the
6826 names that had useful range information attached to them. For
6827 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6828 then N_i will have the range [3, +INF].
6830 However, by converting the assertion into the implied copy
6831 operation N_i = N_j, we will then copy-propagate N_j into the uses
6832 of N_i and lose the range information. We may want to hold on to
6833 ASSERT_EXPRs a little while longer as the ranges could be used in
6834 things like jump threading.
6836 The problem with keeping ASSERT_EXPRs around is that passes after
6837 VRP need to handle them appropriately.
6839 Another approach would be to make the range information a first
6840 class property of the SSA_NAME so that it can be queried from
6841 any pass. This is made somewhat more complex by the need for
6842 multiple ranges to be associated with one SSA_NAME. */
6844 static void
6845 remove_range_assertions (void)
6847 basic_block bb;
6848 gimple_stmt_iterator si;
6849 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6850 a basic block preceeded by GIMPLE_COND branching to it and
6851 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6852 int is_unreachable;
6854 /* Note that the BSI iterator bump happens at the bottom of the
6855 loop and no bump is necessary if we're removing the statement
6856 referenced by the current BSI. */
6857 FOR_EACH_BB_FN (bb, cfun)
6858 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6860 gimple stmt = gsi_stmt (si);
6861 gimple use_stmt;
6863 if (is_gimple_assign (stmt)
6864 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6866 tree lhs = gimple_assign_lhs (stmt);
6867 tree rhs = gimple_assign_rhs1 (stmt);
6868 tree var;
6869 tree cond = fold (ASSERT_EXPR_COND (rhs));
6870 use_operand_p use_p;
6871 imm_use_iterator iter;
6873 gcc_assert (cond != boolean_false_node);
6875 var = ASSERT_EXPR_VAR (rhs);
6876 gcc_assert (TREE_CODE (var) == SSA_NAME);
6878 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6879 && SSA_NAME_RANGE_INFO (lhs))
6881 if (is_unreachable == -1)
6883 is_unreachable = 0;
6884 if (single_pred_p (bb)
6885 && assert_unreachable_fallthru_edge_p
6886 (single_pred_edge (bb)))
6887 is_unreachable = 1;
6889 /* Handle
6890 if (x_7 >= 10 && x_7 < 20)
6891 __builtin_unreachable ();
6892 x_8 = ASSERT_EXPR <x_7, ...>;
6893 if the only uses of x_7 are in the ASSERT_EXPR and
6894 in the condition. In that case, we can copy the
6895 range info from x_8 computed in this pass also
6896 for x_7. */
6897 if (is_unreachable
6898 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6899 single_pred (bb)))
6901 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6902 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6903 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6904 maybe_set_nonzero_bits (bb, var);
6908 /* Propagate the RHS into every use of the LHS. */
6909 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6910 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6911 SET_USE (use_p, var);
6913 /* And finally, remove the copy, it is not needed. */
6914 gsi_remove (&si, true);
6915 release_defs (stmt);
6917 else
6919 if (!is_gimple_debug (gsi_stmt (si)))
6920 is_unreachable = 0;
6921 gsi_next (&si);
6927 /* Return true if STMT is interesting for VRP. */
6929 static bool
6930 stmt_interesting_for_vrp (gimple stmt)
6932 if (gimple_code (stmt) == GIMPLE_PHI)
6934 tree res = gimple_phi_result (stmt);
6935 return (!virtual_operand_p (res)
6936 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6937 || POINTER_TYPE_P (TREE_TYPE (res))));
6939 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6941 tree lhs = gimple_get_lhs (stmt);
6943 /* In general, assignments with virtual operands are not useful
6944 for deriving ranges, with the obvious exception of calls to
6945 builtin functions. */
6946 if (lhs && TREE_CODE (lhs) == SSA_NAME
6947 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6948 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6949 && (is_gimple_call (stmt)
6950 || !gimple_vuse (stmt)))
6951 return true;
6953 else if (gimple_code (stmt) == GIMPLE_COND
6954 || gimple_code (stmt) == GIMPLE_SWITCH)
6955 return true;
6957 return false;
6961 /* Initialize local data structures for VRP. */
6963 static void
6964 vrp_initialize (void)
6966 basic_block bb;
6968 values_propagated = false;
6969 num_vr_values = num_ssa_names;
6970 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6971 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6973 FOR_EACH_BB_FN (bb, cfun)
6975 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6976 gsi_next (&si))
6978 gphi *phi = si.phi ();
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 (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
6990 gsi_next (&si))
6992 gimple stmt = gsi_stmt (si);
6994 /* If the statement is a control insn, then we do not
6995 want to avoid simulating the statement once. Failure
6996 to do so means that those edges will never get added. */
6997 if (stmt_ends_bb_p (stmt))
6998 prop_set_simulate_again (stmt, true);
6999 else if (!stmt_interesting_for_vrp (stmt))
7001 ssa_op_iter i;
7002 tree def;
7003 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7004 set_value_range_to_varying (get_value_range (def));
7005 prop_set_simulate_again (stmt, false);
7007 else
7008 prop_set_simulate_again (stmt, true);
7013 /* Return the singleton value-range for NAME or NAME. */
7015 static inline tree
7016 vrp_valueize (tree name)
7018 if (TREE_CODE (name) == SSA_NAME)
7020 value_range_t *vr = get_value_range (name);
7021 if (vr->type == VR_RANGE
7022 && (vr->min == vr->max
7023 || operand_equal_p (vr->min, vr->max, 0)))
7024 return vr->min;
7026 return name;
7029 /* Return the singleton value-range for NAME if that is a constant
7030 but signal to not follow SSA edges. */
7032 static inline tree
7033 vrp_valueize_1 (tree name)
7035 if (TREE_CODE (name) == SSA_NAME)
7037 value_range_t *vr = get_value_range (name);
7038 if (range_int_cst_singleton_p (vr))
7039 return vr->min;
7040 /* If the definition may be simulated again we cannot follow
7041 this SSA edge as the SSA propagator does not necessarily
7042 re-visit the use. */
7043 gimple def_stmt = SSA_NAME_DEF_STMT (name);
7044 if (prop_simulate_again_p (def_stmt))
7045 return NULL_TREE;
7047 return name;
7050 /* Visit assignment STMT. If it produces an interesting range, record
7051 the SSA name in *OUTPUT_P. */
7053 static enum ssa_prop_result
7054 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
7056 tree def, lhs;
7057 ssa_op_iter iter;
7058 enum gimple_code code = gimple_code (stmt);
7059 lhs = gimple_get_lhs (stmt);
7061 /* We only keep track of ranges in integral and pointer types. */
7062 if (TREE_CODE (lhs) == SSA_NAME
7063 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7064 /* It is valid to have NULL MIN/MAX values on a type. See
7065 build_range_type. */
7066 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7067 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7068 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7070 value_range_t new_vr = VR_INITIALIZER;
7072 /* Try folding the statement to a constant first. */
7073 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7074 vrp_valueize_1);
7075 if (tem && is_gimple_min_invariant (tem))
7076 set_value_range_to_value (&new_vr, tem, NULL);
7077 /* Then dispatch to value-range extracting functions. */
7078 else if (code == GIMPLE_CALL)
7079 extract_range_basic (&new_vr, stmt);
7080 else
7081 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7083 if (update_value_range (lhs, &new_vr))
7085 *output_p = lhs;
7087 if (dump_file && (dump_flags & TDF_DETAILS))
7089 fprintf (dump_file, "Found new range for ");
7090 print_generic_expr (dump_file, lhs, 0);
7091 fprintf (dump_file, ": ");
7092 dump_value_range (dump_file, &new_vr);
7093 fprintf (dump_file, "\n");
7096 if (new_vr.type == VR_VARYING)
7097 return SSA_PROP_VARYING;
7099 return SSA_PROP_INTERESTING;
7102 return SSA_PROP_NOT_INTERESTING;
7105 /* Every other statement produces no useful ranges. */
7106 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7107 set_value_range_to_varying (get_value_range (def));
7109 return SSA_PROP_VARYING;
7112 /* Helper that gets the value range of the SSA_NAME with version I
7113 or a symbolic range containing the SSA_NAME only if the value range
7114 is varying or undefined. */
7116 static inline value_range_t
7117 get_vr_for_comparison (int i)
7119 value_range_t vr = *get_value_range (ssa_name (i));
7121 /* If name N_i does not have a valid range, use N_i as its own
7122 range. This allows us to compare against names that may
7123 have N_i in their ranges. */
7124 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7126 vr.type = VR_RANGE;
7127 vr.min = ssa_name (i);
7128 vr.max = ssa_name (i);
7131 return vr;
7134 /* Compare all the value ranges for names equivalent to VAR with VAL
7135 using comparison code COMP. Return the same value returned by
7136 compare_range_with_value, including the setting of
7137 *STRICT_OVERFLOW_P. */
7139 static tree
7140 compare_name_with_value (enum tree_code comp, tree var, tree val,
7141 bool *strict_overflow_p)
7143 bitmap_iterator bi;
7144 unsigned i;
7145 bitmap e;
7146 tree retval, t;
7147 int used_strict_overflow;
7148 bool sop;
7149 value_range_t equiv_vr;
7151 /* Get the set of equivalences for VAR. */
7152 e = get_value_range (var)->equiv;
7154 /* Start at -1. Set it to 0 if we do a comparison without relying
7155 on overflow, or 1 if all comparisons rely on overflow. */
7156 used_strict_overflow = -1;
7158 /* Compare vars' value range with val. */
7159 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7160 sop = false;
7161 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7162 if (retval)
7163 used_strict_overflow = sop ? 1 : 0;
7165 /* If the equiv set is empty we have done all work we need to do. */
7166 if (e == NULL)
7168 if (retval
7169 && used_strict_overflow > 0)
7170 *strict_overflow_p = true;
7171 return retval;
7174 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7176 equiv_vr = get_vr_for_comparison (i);
7177 sop = false;
7178 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7179 if (t)
7181 /* If we get different answers from different members
7182 of the equivalence set this check must be in a dead
7183 code region. Folding it to a trap representation
7184 would be correct here. For now just return don't-know. */
7185 if (retval != NULL
7186 && t != retval)
7188 retval = NULL_TREE;
7189 break;
7191 retval = t;
7193 if (!sop)
7194 used_strict_overflow = 0;
7195 else if (used_strict_overflow < 0)
7196 used_strict_overflow = 1;
7200 if (retval
7201 && used_strict_overflow > 0)
7202 *strict_overflow_p = true;
7204 return retval;
7208 /* Given a comparison code COMP and names N1 and N2, compare all the
7209 ranges equivalent to N1 against all the ranges equivalent to N2
7210 to determine the value of N1 COMP N2. Return the same value
7211 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7212 whether we relied on an overflow infinity in the comparison. */
7215 static tree
7216 compare_names (enum tree_code comp, tree n1, tree n2,
7217 bool *strict_overflow_p)
7219 tree t, retval;
7220 bitmap e1, e2;
7221 bitmap_iterator bi1, bi2;
7222 unsigned i1, i2;
7223 int used_strict_overflow;
7224 static bitmap_obstack *s_obstack = NULL;
7225 static bitmap s_e1 = NULL, s_e2 = NULL;
7227 /* Compare the ranges of every name equivalent to N1 against the
7228 ranges of every name equivalent to N2. */
7229 e1 = get_value_range (n1)->equiv;
7230 e2 = get_value_range (n2)->equiv;
7232 /* Use the fake bitmaps if e1 or e2 are not available. */
7233 if (s_obstack == NULL)
7235 s_obstack = XNEW (bitmap_obstack);
7236 bitmap_obstack_initialize (s_obstack);
7237 s_e1 = BITMAP_ALLOC (s_obstack);
7238 s_e2 = BITMAP_ALLOC (s_obstack);
7240 if (e1 == NULL)
7241 e1 = s_e1;
7242 if (e2 == NULL)
7243 e2 = s_e2;
7245 /* Add N1 and N2 to their own set of equivalences to avoid
7246 duplicating the body of the loop just to check N1 and N2
7247 ranges. */
7248 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7249 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7251 /* If the equivalence sets have a common intersection, then the two
7252 names can be compared without checking their ranges. */
7253 if (bitmap_intersect_p (e1, e2))
7255 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7256 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7258 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7259 ? boolean_true_node
7260 : boolean_false_node;
7263 /* Start at -1. Set it to 0 if we do a comparison without relying
7264 on overflow, or 1 if all comparisons rely on overflow. */
7265 used_strict_overflow = -1;
7267 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7268 N2 to their own set of equivalences to avoid duplicating the body
7269 of the loop just to check N1 and N2 ranges. */
7270 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7272 value_range_t vr1 = get_vr_for_comparison (i1);
7274 t = retval = NULL_TREE;
7275 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7277 bool sop = false;
7279 value_range_t vr2 = get_vr_for_comparison (i2);
7281 t = compare_ranges (comp, &vr1, &vr2, &sop);
7282 if (t)
7284 /* If we get different answers from different members
7285 of the equivalence set this check must be in a dead
7286 code region. Folding it to a trap representation
7287 would be correct here. For now just return don't-know. */
7288 if (retval != NULL
7289 && t != retval)
7291 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7292 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7293 return NULL_TREE;
7295 retval = t;
7297 if (!sop)
7298 used_strict_overflow = 0;
7299 else if (used_strict_overflow < 0)
7300 used_strict_overflow = 1;
7304 if (retval)
7306 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7307 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7308 if (used_strict_overflow > 0)
7309 *strict_overflow_p = true;
7310 return retval;
7314 /* None of the equivalent ranges are useful in computing this
7315 comparison. */
7316 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7317 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7318 return NULL_TREE;
7321 /* Helper function for vrp_evaluate_conditional_warnv. */
7323 static tree
7324 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7325 tree op0, tree op1,
7326 bool * strict_overflow_p)
7328 value_range_t *vr0, *vr1;
7330 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7331 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7333 tree res = NULL_TREE;
7334 if (vr0 && vr1)
7335 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7336 if (!res && vr0)
7337 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7338 if (!res && vr1)
7339 res = (compare_range_with_value
7340 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7341 return res;
7344 /* Helper function for vrp_evaluate_conditional_warnv. */
7346 static tree
7347 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7348 tree op1, bool use_equiv_p,
7349 bool *strict_overflow_p, bool *only_ranges)
7351 tree ret;
7352 if (only_ranges)
7353 *only_ranges = true;
7355 /* We only deal with integral and pointer types. */
7356 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7357 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7358 return NULL_TREE;
7360 if (use_equiv_p)
7362 if (only_ranges
7363 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7364 (code, op0, op1, strict_overflow_p)))
7365 return ret;
7366 *only_ranges = false;
7367 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7368 return compare_names (code, op0, op1, strict_overflow_p);
7369 else if (TREE_CODE (op0) == SSA_NAME)
7370 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7371 else if (TREE_CODE (op1) == SSA_NAME)
7372 return (compare_name_with_value
7373 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7375 else
7376 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7377 strict_overflow_p);
7378 return NULL_TREE;
7381 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7382 information. Return NULL if the conditional can not be evaluated.
7383 The ranges of all the names equivalent with the operands in COND
7384 will be used when trying to compute the value. If the result is
7385 based on undefined signed overflow, issue a warning if
7386 appropriate. */
7388 static tree
7389 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7391 bool sop;
7392 tree ret;
7393 bool only_ranges;
7395 /* Some passes and foldings leak constants with overflow flag set
7396 into the IL. Avoid doing wrong things with these and bail out. */
7397 if ((TREE_CODE (op0) == INTEGER_CST
7398 && TREE_OVERFLOW (op0))
7399 || (TREE_CODE (op1) == INTEGER_CST
7400 && TREE_OVERFLOW (op1)))
7401 return NULL_TREE;
7403 sop = false;
7404 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7405 &only_ranges);
7407 if (ret && sop)
7409 enum warn_strict_overflow_code wc;
7410 const char* warnmsg;
7412 if (is_gimple_min_invariant (ret))
7414 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7415 warnmsg = G_("assuming signed overflow does not occur when "
7416 "simplifying conditional to constant");
7418 else
7420 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7421 warnmsg = G_("assuming signed overflow does not occur when "
7422 "simplifying conditional");
7425 if (issue_strict_overflow_warning (wc))
7427 location_t location;
7429 if (!gimple_has_location (stmt))
7430 location = input_location;
7431 else
7432 location = gimple_location (stmt);
7433 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7437 if (warn_type_limits
7438 && ret && only_ranges
7439 && TREE_CODE_CLASS (code) == tcc_comparison
7440 && TREE_CODE (op0) == SSA_NAME)
7442 /* If the comparison is being folded and the operand on the LHS
7443 is being compared against a constant value that is outside of
7444 the natural range of OP0's type, then the predicate will
7445 always fold regardless of the value of OP0. If -Wtype-limits
7446 was specified, emit a warning. */
7447 tree type = TREE_TYPE (op0);
7448 value_range_t *vr0 = get_value_range (op0);
7450 if (vr0->type != VR_VARYING
7451 && INTEGRAL_TYPE_P (type)
7452 && vrp_val_is_min (vr0->min)
7453 && vrp_val_is_max (vr0->max)
7454 && is_gimple_min_invariant (op1))
7456 location_t location;
7458 if (!gimple_has_location (stmt))
7459 location = input_location;
7460 else
7461 location = gimple_location (stmt);
7463 warning_at (location, OPT_Wtype_limits,
7464 integer_zerop (ret)
7465 ? G_("comparison always false "
7466 "due to limited range of data type")
7467 : G_("comparison always true "
7468 "due to limited range of data type"));
7472 return ret;
7476 /* Visit conditional statement STMT. If we can determine which edge
7477 will be taken out of STMT's basic block, record it in
7478 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7479 SSA_PROP_VARYING. */
7481 static enum ssa_prop_result
7482 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7484 tree val;
7485 bool sop;
7487 *taken_edge_p = NULL;
7489 if (dump_file && (dump_flags & TDF_DETAILS))
7491 tree use;
7492 ssa_op_iter i;
7494 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7495 print_gimple_stmt (dump_file, stmt, 0, 0);
7496 fprintf (dump_file, "\nWith known ranges\n");
7498 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7500 fprintf (dump_file, "\t");
7501 print_generic_expr (dump_file, use, 0);
7502 fprintf (dump_file, ": ");
7503 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7506 fprintf (dump_file, "\n");
7509 /* Compute the value of the predicate COND by checking the known
7510 ranges of each of its operands.
7512 Note that we cannot evaluate all the equivalent ranges here
7513 because those ranges may not yet be final and with the current
7514 propagation strategy, we cannot determine when the value ranges
7515 of the names in the equivalence set have changed.
7517 For instance, given the following code fragment
7519 i_5 = PHI <8, i_13>
7521 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7522 if (i_14 == 1)
7525 Assume that on the first visit to i_14, i_5 has the temporary
7526 range [8, 8] because the second argument to the PHI function is
7527 not yet executable. We derive the range ~[0, 0] for i_14 and the
7528 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7529 the first time, since i_14 is equivalent to the range [8, 8], we
7530 determine that the predicate is always false.
7532 On the next round of propagation, i_13 is determined to be
7533 VARYING, which causes i_5 to drop down to VARYING. So, another
7534 visit to i_14 is scheduled. In this second visit, we compute the
7535 exact same range and equivalence set for i_14, namely ~[0, 0] and
7536 { i_5 }. But we did not have the previous range for i_5
7537 registered, so vrp_visit_assignment thinks that the range for
7538 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7539 is not visited again, which stops propagation from visiting
7540 statements in the THEN clause of that if().
7542 To properly fix this we would need to keep the previous range
7543 value for the names in the equivalence set. This way we would've
7544 discovered that from one visit to the other i_5 changed from
7545 range [8, 8] to VR_VARYING.
7547 However, fixing this apparent limitation may not be worth the
7548 additional checking. Testing on several code bases (GCC, DLV,
7549 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7550 4 more predicates folded in SPEC. */
7551 sop = false;
7553 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7554 gimple_cond_lhs (stmt),
7555 gimple_cond_rhs (stmt),
7556 false, &sop, NULL);
7557 if (val)
7559 if (!sop)
7560 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7561 else
7563 if (dump_file && (dump_flags & TDF_DETAILS))
7564 fprintf (dump_file,
7565 "\nIgnoring predicate evaluation because "
7566 "it assumes that signed overflow is undefined");
7567 val = NULL_TREE;
7571 if (dump_file && (dump_flags & TDF_DETAILS))
7573 fprintf (dump_file, "\nPredicate evaluates to: ");
7574 if (val == NULL_TREE)
7575 fprintf (dump_file, "DON'T KNOW\n");
7576 else
7577 print_generic_stmt (dump_file, val, 0);
7580 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7583 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7584 that includes the value VAL. The search is restricted to the range
7585 [START_IDX, n - 1] where n is the size of VEC.
7587 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7588 returned.
7590 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7591 it is placed in IDX and false is returned.
7593 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7594 returned. */
7596 static bool
7597 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7599 size_t n = gimple_switch_num_labels (stmt);
7600 size_t low, high;
7602 /* Find case label for minimum of the value range or the next one.
7603 At each iteration we are searching in [low, high - 1]. */
7605 for (low = start_idx, high = n; high != low; )
7607 tree t;
7608 int cmp;
7609 /* Note that i != high, so we never ask for n. */
7610 size_t i = (high + low) / 2;
7611 t = gimple_switch_label (stmt, i);
7613 /* Cache the result of comparing CASE_LOW and val. */
7614 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7616 if (cmp == 0)
7618 /* Ranges cannot be empty. */
7619 *idx = i;
7620 return true;
7622 else if (cmp > 0)
7623 high = i;
7624 else
7626 low = i + 1;
7627 if (CASE_HIGH (t) != NULL
7628 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7630 *idx = i;
7631 return true;
7636 *idx = high;
7637 return false;
7640 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7641 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7642 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7643 then MAX_IDX < MIN_IDX.
7644 Returns true if the default label is not needed. */
7646 static bool
7647 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7648 size_t *max_idx)
7650 size_t i, j;
7651 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7652 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7654 if (i == j
7655 && min_take_default
7656 && max_take_default)
7658 /* Only the default case label reached.
7659 Return an empty range. */
7660 *min_idx = 1;
7661 *max_idx = 0;
7662 return false;
7664 else
7666 bool take_default = min_take_default || max_take_default;
7667 tree low, high;
7668 size_t k;
7670 if (max_take_default)
7671 j--;
7673 /* If the case label range is continuous, we do not need
7674 the default case label. Verify that. */
7675 high = CASE_LOW (gimple_switch_label (stmt, i));
7676 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7677 high = CASE_HIGH (gimple_switch_label (stmt, i));
7678 for (k = i + 1; k <= j; ++k)
7680 low = CASE_LOW (gimple_switch_label (stmt, k));
7681 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7683 take_default = true;
7684 break;
7686 high = low;
7687 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7688 high = CASE_HIGH (gimple_switch_label (stmt, k));
7691 *min_idx = i;
7692 *max_idx = j;
7693 return !take_default;
7697 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7698 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7699 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7700 Returns true if the default label is not needed. */
7702 static bool
7703 find_case_label_ranges (gswitch *stmt, value_range_t *vr, size_t *min_idx1,
7704 size_t *max_idx1, size_t *min_idx2,
7705 size_t *max_idx2)
7707 size_t i, j, k, l;
7708 unsigned int n = gimple_switch_num_labels (stmt);
7709 bool take_default;
7710 tree case_low, case_high;
7711 tree min = vr->min, max = vr->max;
7713 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7715 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7717 /* Set second range to emtpy. */
7718 *min_idx2 = 1;
7719 *max_idx2 = 0;
7721 if (vr->type == VR_RANGE)
7723 *min_idx1 = i;
7724 *max_idx1 = j;
7725 return !take_default;
7728 /* Set first range to all case labels. */
7729 *min_idx1 = 1;
7730 *max_idx1 = n - 1;
7732 if (i > j)
7733 return false;
7735 /* Make sure all the values of case labels [i , j] are contained in
7736 range [MIN, MAX]. */
7737 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7738 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7739 if (tree_int_cst_compare (case_low, min) < 0)
7740 i += 1;
7741 if (case_high != NULL_TREE
7742 && tree_int_cst_compare (max, case_high) < 0)
7743 j -= 1;
7745 if (i > j)
7746 return false;
7748 /* If the range spans case labels [i, j], the corresponding anti-range spans
7749 the labels [1, i - 1] and [j + 1, n - 1]. */
7750 k = j + 1;
7751 l = n - 1;
7752 if (k > l)
7754 k = 1;
7755 l = 0;
7758 j = i - 1;
7759 i = 1;
7760 if (i > j)
7762 i = k;
7763 j = l;
7764 k = 1;
7765 l = 0;
7768 *min_idx1 = i;
7769 *max_idx1 = j;
7770 *min_idx2 = k;
7771 *max_idx2 = l;
7772 return false;
7775 /* Visit switch statement STMT. If we can determine which edge
7776 will be taken out of STMT's basic block, record it in
7777 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7778 SSA_PROP_VARYING. */
7780 static enum ssa_prop_result
7781 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7783 tree op, val;
7784 value_range_t *vr;
7785 size_t i = 0, j = 0, k, l;
7786 bool take_default;
7788 *taken_edge_p = NULL;
7789 op = gimple_switch_index (stmt);
7790 if (TREE_CODE (op) != SSA_NAME)
7791 return SSA_PROP_VARYING;
7793 vr = get_value_range (op);
7794 if (dump_file && (dump_flags & TDF_DETAILS))
7796 fprintf (dump_file, "\nVisiting switch expression with operand ");
7797 print_generic_expr (dump_file, op, 0);
7798 fprintf (dump_file, " with known range ");
7799 dump_value_range (dump_file, vr);
7800 fprintf (dump_file, "\n");
7803 if ((vr->type != VR_RANGE
7804 && vr->type != VR_ANTI_RANGE)
7805 || symbolic_range_p (vr))
7806 return SSA_PROP_VARYING;
7808 /* Find the single edge that is taken from the switch expression. */
7809 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7811 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7812 label */
7813 if (j < i)
7815 gcc_assert (take_default);
7816 val = gimple_switch_default_label (stmt);
7818 else
7820 /* Check if labels with index i to j and maybe the default label
7821 are all reaching the same label. */
7823 val = gimple_switch_label (stmt, i);
7824 if (take_default
7825 && CASE_LABEL (gimple_switch_default_label (stmt))
7826 != CASE_LABEL (val))
7828 if (dump_file && (dump_flags & TDF_DETAILS))
7829 fprintf (dump_file, " not a single destination for this "
7830 "range\n");
7831 return SSA_PROP_VARYING;
7833 for (++i; i <= j; ++i)
7835 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7837 if (dump_file && (dump_flags & TDF_DETAILS))
7838 fprintf (dump_file, " not a single destination for this "
7839 "range\n");
7840 return SSA_PROP_VARYING;
7843 for (; k <= l; ++k)
7845 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7847 if (dump_file && (dump_flags & TDF_DETAILS))
7848 fprintf (dump_file, " not a single destination for this "
7849 "range\n");
7850 return SSA_PROP_VARYING;
7855 *taken_edge_p = find_edge (gimple_bb (stmt),
7856 label_to_block (CASE_LABEL (val)));
7858 if (dump_file && (dump_flags & TDF_DETAILS))
7860 fprintf (dump_file, " will take edge to ");
7861 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7864 return SSA_PROP_INTERESTING;
7868 /* Evaluate statement STMT. If the statement produces a useful range,
7869 return SSA_PROP_INTERESTING and record the SSA name with the
7870 interesting range into *OUTPUT_P.
7872 If STMT is a conditional branch and we can determine its truth
7873 value, the taken edge is recorded in *TAKEN_EDGE_P.
7875 If STMT produces a varying value, return SSA_PROP_VARYING. */
7877 static enum ssa_prop_result
7878 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7880 tree def;
7881 ssa_op_iter iter;
7883 if (dump_file && (dump_flags & TDF_DETAILS))
7885 fprintf (dump_file, "\nVisiting statement:\n");
7886 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7889 if (!stmt_interesting_for_vrp (stmt))
7890 gcc_assert (stmt_ends_bb_p (stmt));
7891 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7892 return vrp_visit_assignment_or_call (stmt, output_p);
7893 else if (gimple_code (stmt) == GIMPLE_COND)
7894 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7895 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7896 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7898 /* All other statements produce nothing of interest for VRP, so mark
7899 their outputs varying and prevent further simulation. */
7900 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7901 set_value_range_to_varying (get_value_range (def));
7903 return SSA_PROP_VARYING;
7906 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7907 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7908 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7909 possible such range. The resulting range is not canonicalized. */
7911 static void
7912 union_ranges (enum value_range_type *vr0type,
7913 tree *vr0min, tree *vr0max,
7914 enum value_range_type vr1type,
7915 tree vr1min, tree vr1max)
7917 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7918 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7920 /* [] is vr0, () is vr1 in the following classification comments. */
7921 if (mineq && maxeq)
7923 /* [( )] */
7924 if (*vr0type == vr1type)
7925 /* Nothing to do for equal ranges. */
7927 else if ((*vr0type == VR_RANGE
7928 && vr1type == VR_ANTI_RANGE)
7929 || (*vr0type == VR_ANTI_RANGE
7930 && vr1type == VR_RANGE))
7932 /* For anti-range with range union the result is varying. */
7933 goto give_up;
7935 else
7936 gcc_unreachable ();
7938 else if (operand_less_p (*vr0max, vr1min) == 1
7939 || operand_less_p (vr1max, *vr0min) == 1)
7941 /* [ ] ( ) or ( ) [ ]
7942 If the ranges have an empty intersection, result of the union
7943 operation is the anti-range or if both are anti-ranges
7944 it covers all. */
7945 if (*vr0type == VR_ANTI_RANGE
7946 && vr1type == VR_ANTI_RANGE)
7947 goto give_up;
7948 else if (*vr0type == VR_ANTI_RANGE
7949 && vr1type == VR_RANGE)
7951 else if (*vr0type == VR_RANGE
7952 && vr1type == VR_ANTI_RANGE)
7954 *vr0type = vr1type;
7955 *vr0min = vr1min;
7956 *vr0max = vr1max;
7958 else if (*vr0type == VR_RANGE
7959 && vr1type == VR_RANGE)
7961 /* The result is the convex hull of both ranges. */
7962 if (operand_less_p (*vr0max, vr1min) == 1)
7964 /* If the result can be an anti-range, create one. */
7965 if (TREE_CODE (*vr0max) == INTEGER_CST
7966 && TREE_CODE (vr1min) == INTEGER_CST
7967 && vrp_val_is_min (*vr0min)
7968 && vrp_val_is_max (vr1max))
7970 tree min = int_const_binop (PLUS_EXPR,
7971 *vr0max,
7972 build_int_cst (TREE_TYPE (*vr0max), 1));
7973 tree max = int_const_binop (MINUS_EXPR,
7974 vr1min,
7975 build_int_cst (TREE_TYPE (vr1min), 1));
7976 if (!operand_less_p (max, min))
7978 *vr0type = VR_ANTI_RANGE;
7979 *vr0min = min;
7980 *vr0max = max;
7982 else
7983 *vr0max = vr1max;
7985 else
7986 *vr0max = vr1max;
7988 else
7990 /* If the result can be an anti-range, create one. */
7991 if (TREE_CODE (vr1max) == INTEGER_CST
7992 && TREE_CODE (*vr0min) == INTEGER_CST
7993 && vrp_val_is_min (vr1min)
7994 && vrp_val_is_max (*vr0max))
7996 tree min = int_const_binop (PLUS_EXPR,
7997 vr1max,
7998 build_int_cst (TREE_TYPE (vr1max), 1));
7999 tree max = int_const_binop (MINUS_EXPR,
8000 *vr0min,
8001 build_int_cst (TREE_TYPE (*vr0min), 1));
8002 if (!operand_less_p (max, min))
8004 *vr0type = VR_ANTI_RANGE;
8005 *vr0min = min;
8006 *vr0max = max;
8008 else
8009 *vr0min = vr1min;
8011 else
8012 *vr0min = vr1min;
8015 else
8016 gcc_unreachable ();
8018 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8019 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8021 /* [ ( ) ] or [( ) ] or [ ( )] */
8022 if (*vr0type == VR_RANGE
8023 && vr1type == VR_RANGE)
8025 else if (*vr0type == VR_ANTI_RANGE
8026 && vr1type == VR_ANTI_RANGE)
8028 *vr0type = vr1type;
8029 *vr0min = vr1min;
8030 *vr0max = vr1max;
8032 else if (*vr0type == VR_ANTI_RANGE
8033 && vr1type == VR_RANGE)
8035 /* Arbitrarily choose the right or left gap. */
8036 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8037 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8038 build_int_cst (TREE_TYPE (vr1min), 1));
8039 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8040 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8041 build_int_cst (TREE_TYPE (vr1max), 1));
8042 else
8043 goto give_up;
8045 else if (*vr0type == VR_RANGE
8046 && vr1type == VR_ANTI_RANGE)
8047 /* The result covers everything. */
8048 goto give_up;
8049 else
8050 gcc_unreachable ();
8052 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8053 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8055 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8056 if (*vr0type == VR_RANGE
8057 && vr1type == VR_RANGE)
8059 *vr0type = vr1type;
8060 *vr0min = vr1min;
8061 *vr0max = vr1max;
8063 else if (*vr0type == VR_ANTI_RANGE
8064 && vr1type == VR_ANTI_RANGE)
8066 else if (*vr0type == VR_RANGE
8067 && vr1type == VR_ANTI_RANGE)
8069 *vr0type = VR_ANTI_RANGE;
8070 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8072 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8073 build_int_cst (TREE_TYPE (*vr0min), 1));
8074 *vr0min = vr1min;
8076 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8078 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8079 build_int_cst (TREE_TYPE (*vr0max), 1));
8080 *vr0max = vr1max;
8082 else
8083 goto give_up;
8085 else if (*vr0type == VR_ANTI_RANGE
8086 && vr1type == VR_RANGE)
8087 /* The result covers everything. */
8088 goto give_up;
8089 else
8090 gcc_unreachable ();
8092 else if ((operand_less_p (vr1min, *vr0max) == 1
8093 || operand_equal_p (vr1min, *vr0max, 0))
8094 && operand_less_p (*vr0min, vr1min) == 1
8095 && operand_less_p (*vr0max, vr1max) == 1)
8097 /* [ ( ] ) or [ ]( ) */
8098 if (*vr0type == VR_RANGE
8099 && vr1type == VR_RANGE)
8100 *vr0max = vr1max;
8101 else if (*vr0type == VR_ANTI_RANGE
8102 && vr1type == VR_ANTI_RANGE)
8103 *vr0min = vr1min;
8104 else if (*vr0type == VR_ANTI_RANGE
8105 && vr1type == VR_RANGE)
8107 if (TREE_CODE (vr1min) == INTEGER_CST)
8108 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8109 build_int_cst (TREE_TYPE (vr1min), 1));
8110 else
8111 goto give_up;
8113 else if (*vr0type == VR_RANGE
8114 && vr1type == VR_ANTI_RANGE)
8116 if (TREE_CODE (*vr0max) == INTEGER_CST)
8118 *vr0type = vr1type;
8119 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8120 build_int_cst (TREE_TYPE (*vr0max), 1));
8121 *vr0max = vr1max;
8123 else
8124 goto give_up;
8126 else
8127 gcc_unreachable ();
8129 else if ((operand_less_p (*vr0min, vr1max) == 1
8130 || operand_equal_p (*vr0min, vr1max, 0))
8131 && operand_less_p (vr1min, *vr0min) == 1
8132 && operand_less_p (vr1max, *vr0max) == 1)
8134 /* ( [ ) ] or ( )[ ] */
8135 if (*vr0type == VR_RANGE
8136 && vr1type == VR_RANGE)
8137 *vr0min = vr1min;
8138 else if (*vr0type == VR_ANTI_RANGE
8139 && vr1type == VR_ANTI_RANGE)
8140 *vr0max = vr1max;
8141 else if (*vr0type == VR_ANTI_RANGE
8142 && vr1type == VR_RANGE)
8144 if (TREE_CODE (vr1max) == INTEGER_CST)
8145 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8146 build_int_cst (TREE_TYPE (vr1max), 1));
8147 else
8148 goto give_up;
8150 else if (*vr0type == VR_RANGE
8151 && vr1type == VR_ANTI_RANGE)
8153 if (TREE_CODE (*vr0min) == INTEGER_CST)
8155 *vr0type = vr1type;
8156 *vr0min = vr1min;
8157 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8158 build_int_cst (TREE_TYPE (*vr0min), 1));
8160 else
8161 goto give_up;
8163 else
8164 gcc_unreachable ();
8166 else
8167 goto give_up;
8169 return;
8171 give_up:
8172 *vr0type = VR_VARYING;
8173 *vr0min = NULL_TREE;
8174 *vr0max = NULL_TREE;
8177 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8178 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8179 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8180 possible such range. The resulting range is not canonicalized. */
8182 static void
8183 intersect_ranges (enum value_range_type *vr0type,
8184 tree *vr0min, tree *vr0max,
8185 enum value_range_type vr1type,
8186 tree vr1min, tree vr1max)
8188 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8189 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8191 /* [] is vr0, () is vr1 in the following classification comments. */
8192 if (mineq && maxeq)
8194 /* [( )] */
8195 if (*vr0type == vr1type)
8196 /* Nothing to do for equal ranges. */
8198 else if ((*vr0type == VR_RANGE
8199 && vr1type == VR_ANTI_RANGE)
8200 || (*vr0type == VR_ANTI_RANGE
8201 && vr1type == VR_RANGE))
8203 /* For anti-range with range intersection the result is empty. */
8204 *vr0type = VR_UNDEFINED;
8205 *vr0min = NULL_TREE;
8206 *vr0max = NULL_TREE;
8208 else
8209 gcc_unreachable ();
8211 else if (operand_less_p (*vr0max, vr1min) == 1
8212 || operand_less_p (vr1max, *vr0min) == 1)
8214 /* [ ] ( ) or ( ) [ ]
8215 If the ranges have an empty intersection, the result of the
8216 intersect operation is the range for intersecting an
8217 anti-range with a range or empty when intersecting two ranges. */
8218 if (*vr0type == VR_RANGE
8219 && vr1type == VR_ANTI_RANGE)
8221 else if (*vr0type == VR_ANTI_RANGE
8222 && vr1type == VR_RANGE)
8224 *vr0type = vr1type;
8225 *vr0min = vr1min;
8226 *vr0max = vr1max;
8228 else if (*vr0type == VR_RANGE
8229 && vr1type == VR_RANGE)
8231 *vr0type = VR_UNDEFINED;
8232 *vr0min = NULL_TREE;
8233 *vr0max = NULL_TREE;
8235 else if (*vr0type == VR_ANTI_RANGE
8236 && vr1type == VR_ANTI_RANGE)
8238 /* If the anti-ranges are adjacent to each other merge them. */
8239 if (TREE_CODE (*vr0max) == INTEGER_CST
8240 && TREE_CODE (vr1min) == INTEGER_CST
8241 && operand_less_p (*vr0max, vr1min) == 1
8242 && integer_onep (int_const_binop (MINUS_EXPR,
8243 vr1min, *vr0max)))
8244 *vr0max = vr1max;
8245 else if (TREE_CODE (vr1max) == INTEGER_CST
8246 && TREE_CODE (*vr0min) == INTEGER_CST
8247 && operand_less_p (vr1max, *vr0min) == 1
8248 && integer_onep (int_const_binop (MINUS_EXPR,
8249 *vr0min, vr1max)))
8250 *vr0min = vr1min;
8251 /* Else arbitrarily take VR0. */
8254 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8255 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8257 /* [ ( ) ] or [( ) ] or [ ( )] */
8258 if (*vr0type == VR_RANGE
8259 && vr1type == VR_RANGE)
8261 /* If both are ranges the result is the inner one. */
8262 *vr0type = vr1type;
8263 *vr0min = vr1min;
8264 *vr0max = vr1max;
8266 else if (*vr0type == VR_RANGE
8267 && vr1type == VR_ANTI_RANGE)
8269 /* Choose the right gap if the left one is empty. */
8270 if (mineq)
8272 if (TREE_CODE (vr1max) == INTEGER_CST)
8273 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8274 build_int_cst (TREE_TYPE (vr1max), 1));
8275 else
8276 *vr0min = vr1max;
8278 /* Choose the left gap if the right one is empty. */
8279 else if (maxeq)
8281 if (TREE_CODE (vr1min) == INTEGER_CST)
8282 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8283 build_int_cst (TREE_TYPE (vr1min), 1));
8284 else
8285 *vr0max = vr1min;
8287 /* Choose the anti-range if the range is effectively varying. */
8288 else if (vrp_val_is_min (*vr0min)
8289 && vrp_val_is_max (*vr0max))
8291 *vr0type = vr1type;
8292 *vr0min = vr1min;
8293 *vr0max = vr1max;
8295 /* Else choose the range. */
8297 else if (*vr0type == VR_ANTI_RANGE
8298 && vr1type == VR_ANTI_RANGE)
8299 /* If both are anti-ranges the result is the outer one. */
8301 else if (*vr0type == VR_ANTI_RANGE
8302 && vr1type == VR_RANGE)
8304 /* The intersection is empty. */
8305 *vr0type = VR_UNDEFINED;
8306 *vr0min = NULL_TREE;
8307 *vr0max = NULL_TREE;
8309 else
8310 gcc_unreachable ();
8312 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8313 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8315 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8316 if (*vr0type == VR_RANGE
8317 && vr1type == VR_RANGE)
8318 /* Choose the inner range. */
8320 else if (*vr0type == VR_ANTI_RANGE
8321 && vr1type == VR_RANGE)
8323 /* Choose the right gap if the left is empty. */
8324 if (mineq)
8326 *vr0type = VR_RANGE;
8327 if (TREE_CODE (*vr0max) == INTEGER_CST)
8328 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8329 build_int_cst (TREE_TYPE (*vr0max), 1));
8330 else
8331 *vr0min = *vr0max;
8332 *vr0max = vr1max;
8334 /* Choose the left gap if the right is empty. */
8335 else if (maxeq)
8337 *vr0type = VR_RANGE;
8338 if (TREE_CODE (*vr0min) == INTEGER_CST)
8339 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8340 build_int_cst (TREE_TYPE (*vr0min), 1));
8341 else
8342 *vr0max = *vr0min;
8343 *vr0min = vr1min;
8345 /* Choose the anti-range if the range is effectively varying. */
8346 else if (vrp_val_is_min (vr1min)
8347 && vrp_val_is_max (vr1max))
8349 /* Else choose the range. */
8350 else
8352 *vr0type = vr1type;
8353 *vr0min = vr1min;
8354 *vr0max = vr1max;
8357 else if (*vr0type == VR_ANTI_RANGE
8358 && vr1type == VR_ANTI_RANGE)
8360 /* If both are anti-ranges the result is the outer one. */
8361 *vr0type = vr1type;
8362 *vr0min = vr1min;
8363 *vr0max = vr1max;
8365 else if (vr1type == VR_ANTI_RANGE
8366 && *vr0type == VR_RANGE)
8368 /* The intersection is empty. */
8369 *vr0type = VR_UNDEFINED;
8370 *vr0min = NULL_TREE;
8371 *vr0max = NULL_TREE;
8373 else
8374 gcc_unreachable ();
8376 else if ((operand_less_p (vr1min, *vr0max) == 1
8377 || operand_equal_p (vr1min, *vr0max, 0))
8378 && operand_less_p (*vr0min, vr1min) == 1)
8380 /* [ ( ] ) or [ ]( ) */
8381 if (*vr0type == VR_ANTI_RANGE
8382 && vr1type == VR_ANTI_RANGE)
8383 *vr0max = vr1max;
8384 else if (*vr0type == VR_RANGE
8385 && vr1type == VR_RANGE)
8386 *vr0min = vr1min;
8387 else if (*vr0type == VR_RANGE
8388 && vr1type == VR_ANTI_RANGE)
8390 if (TREE_CODE (vr1min) == INTEGER_CST)
8391 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8392 build_int_cst (TREE_TYPE (vr1min), 1));
8393 else
8394 *vr0max = vr1min;
8396 else if (*vr0type == VR_ANTI_RANGE
8397 && vr1type == VR_RANGE)
8399 *vr0type = VR_RANGE;
8400 if (TREE_CODE (*vr0max) == INTEGER_CST)
8401 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8402 build_int_cst (TREE_TYPE (*vr0max), 1));
8403 else
8404 *vr0min = *vr0max;
8405 *vr0max = vr1max;
8407 else
8408 gcc_unreachable ();
8410 else if ((operand_less_p (*vr0min, vr1max) == 1
8411 || operand_equal_p (*vr0min, vr1max, 0))
8412 && operand_less_p (vr1min, *vr0min) == 1)
8414 /* ( [ ) ] or ( )[ ] */
8415 if (*vr0type == VR_ANTI_RANGE
8416 && vr1type == VR_ANTI_RANGE)
8417 *vr0min = vr1min;
8418 else if (*vr0type == VR_RANGE
8419 && vr1type == VR_RANGE)
8420 *vr0max = vr1max;
8421 else if (*vr0type == VR_RANGE
8422 && vr1type == VR_ANTI_RANGE)
8424 if (TREE_CODE (vr1max) == INTEGER_CST)
8425 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8426 build_int_cst (TREE_TYPE (vr1max), 1));
8427 else
8428 *vr0min = vr1max;
8430 else if (*vr0type == VR_ANTI_RANGE
8431 && vr1type == VR_RANGE)
8433 *vr0type = VR_RANGE;
8434 if (TREE_CODE (*vr0min) == INTEGER_CST)
8435 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8436 build_int_cst (TREE_TYPE (*vr0min), 1));
8437 else
8438 *vr0max = *vr0min;
8439 *vr0min = vr1min;
8441 else
8442 gcc_unreachable ();
8445 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8446 result for the intersection. That's always a conservative
8447 correct estimate. */
8449 return;
8453 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8454 in *VR0. This may not be the smallest possible such range. */
8456 static void
8457 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8459 value_range_t saved;
8461 /* If either range is VR_VARYING the other one wins. */
8462 if (vr1->type == VR_VARYING)
8463 return;
8464 if (vr0->type == VR_VARYING)
8466 copy_value_range (vr0, vr1);
8467 return;
8470 /* When either range is VR_UNDEFINED the resulting range is
8471 VR_UNDEFINED, too. */
8472 if (vr0->type == VR_UNDEFINED)
8473 return;
8474 if (vr1->type == VR_UNDEFINED)
8476 set_value_range_to_undefined (vr0);
8477 return;
8480 /* Save the original vr0 so we can return it as conservative intersection
8481 result when our worker turns things to varying. */
8482 saved = *vr0;
8483 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8484 vr1->type, vr1->min, vr1->max);
8485 /* Make sure to canonicalize the result though as the inversion of a
8486 VR_RANGE can still be a VR_RANGE. */
8487 set_and_canonicalize_value_range (vr0, vr0->type,
8488 vr0->min, vr0->max, vr0->equiv);
8489 /* If that failed, use the saved original VR0. */
8490 if (vr0->type == VR_VARYING)
8492 *vr0 = saved;
8493 return;
8495 /* If the result is VR_UNDEFINED there is no need to mess with
8496 the equivalencies. */
8497 if (vr0->type == VR_UNDEFINED)
8498 return;
8500 /* The resulting set of equivalences for range intersection is the union of
8501 the two sets. */
8502 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8503 bitmap_ior_into (vr0->equiv, vr1->equiv);
8504 else if (vr1->equiv && !vr0->equiv)
8505 bitmap_copy (vr0->equiv, vr1->equiv);
8508 static void
8509 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8511 if (dump_file && (dump_flags & TDF_DETAILS))
8513 fprintf (dump_file, "Intersecting\n ");
8514 dump_value_range (dump_file, vr0);
8515 fprintf (dump_file, "\nand\n ");
8516 dump_value_range (dump_file, vr1);
8517 fprintf (dump_file, "\n");
8519 vrp_intersect_ranges_1 (vr0, vr1);
8520 if (dump_file && (dump_flags & TDF_DETAILS))
8522 fprintf (dump_file, "to\n ");
8523 dump_value_range (dump_file, vr0);
8524 fprintf (dump_file, "\n");
8528 /* Meet operation for value ranges. Given two value ranges VR0 and
8529 VR1, store in VR0 a range that contains both VR0 and VR1. This
8530 may not be the smallest possible such range. */
8532 static void
8533 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8535 value_range_t saved;
8537 if (vr0->type == VR_UNDEFINED)
8539 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8540 return;
8543 if (vr1->type == VR_UNDEFINED)
8545 /* VR0 already has the resulting range. */
8546 return;
8549 if (vr0->type == VR_VARYING)
8551 /* Nothing to do. VR0 already has the resulting range. */
8552 return;
8555 if (vr1->type == VR_VARYING)
8557 set_value_range_to_varying (vr0);
8558 return;
8561 saved = *vr0;
8562 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8563 vr1->type, vr1->min, vr1->max);
8564 if (vr0->type == VR_VARYING)
8566 /* Failed to find an efficient meet. Before giving up and setting
8567 the result to VARYING, see if we can at least derive a useful
8568 anti-range. FIXME, all this nonsense about distinguishing
8569 anti-ranges from ranges is necessary because of the odd
8570 semantics of range_includes_zero_p and friends. */
8571 if (((saved.type == VR_RANGE
8572 && range_includes_zero_p (saved.min, saved.max) == 0)
8573 || (saved.type == VR_ANTI_RANGE
8574 && range_includes_zero_p (saved.min, saved.max) == 1))
8575 && ((vr1->type == VR_RANGE
8576 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8577 || (vr1->type == VR_ANTI_RANGE
8578 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8580 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8582 /* Since this meet operation did not result from the meeting of
8583 two equivalent names, VR0 cannot have any equivalences. */
8584 if (vr0->equiv)
8585 bitmap_clear (vr0->equiv);
8586 return;
8589 set_value_range_to_varying (vr0);
8590 return;
8592 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8593 vr0->equiv);
8594 if (vr0->type == VR_VARYING)
8595 return;
8597 /* The resulting set of equivalences is always the intersection of
8598 the two sets. */
8599 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8600 bitmap_and_into (vr0->equiv, vr1->equiv);
8601 else if (vr0->equiv && !vr1->equiv)
8602 bitmap_clear (vr0->equiv);
8605 static void
8606 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8608 if (dump_file && (dump_flags & TDF_DETAILS))
8610 fprintf (dump_file, "Meeting\n ");
8611 dump_value_range (dump_file, vr0);
8612 fprintf (dump_file, "\nand\n ");
8613 dump_value_range (dump_file, vr1);
8614 fprintf (dump_file, "\n");
8616 vrp_meet_1 (vr0, vr1);
8617 if (dump_file && (dump_flags & TDF_DETAILS))
8619 fprintf (dump_file, "to\n ");
8620 dump_value_range (dump_file, vr0);
8621 fprintf (dump_file, "\n");
8626 /* Visit all arguments for PHI node PHI that flow through executable
8627 edges. If a valid value range can be derived from all the incoming
8628 value ranges, set a new range for the LHS of PHI. */
8630 static enum ssa_prop_result
8631 vrp_visit_phi_node (gphi *phi)
8633 size_t i;
8634 tree lhs = PHI_RESULT (phi);
8635 value_range_t *lhs_vr = get_value_range (lhs);
8636 value_range_t vr_result = VR_INITIALIZER;
8637 bool first = true;
8638 int edges, old_edges;
8639 struct loop *l;
8641 if (dump_file && (dump_flags & TDF_DETAILS))
8643 fprintf (dump_file, "\nVisiting PHI node: ");
8644 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8647 edges = 0;
8648 for (i = 0; i < gimple_phi_num_args (phi); i++)
8650 edge e = gimple_phi_arg_edge (phi, i);
8652 if (dump_file && (dump_flags & TDF_DETAILS))
8654 fprintf (dump_file,
8655 " Argument #%d (%d -> %d %sexecutable)\n",
8656 (int) i, e->src->index, e->dest->index,
8657 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8660 if (e->flags & EDGE_EXECUTABLE)
8662 tree arg = PHI_ARG_DEF (phi, i);
8663 value_range_t vr_arg;
8665 ++edges;
8667 if (TREE_CODE (arg) == SSA_NAME)
8669 vr_arg = *(get_value_range (arg));
8670 /* Do not allow equivalences or symbolic ranges to leak in from
8671 backedges. That creates invalid equivalencies.
8672 See PR53465 and PR54767. */
8673 if (e->flags & EDGE_DFS_BACK)
8675 if (vr_arg.type == VR_RANGE
8676 || vr_arg.type == VR_ANTI_RANGE)
8678 vr_arg.equiv = NULL;
8679 if (symbolic_range_p (&vr_arg))
8681 vr_arg.type = VR_VARYING;
8682 vr_arg.min = NULL_TREE;
8683 vr_arg.max = NULL_TREE;
8687 else
8689 /* If the non-backedge arguments range is VR_VARYING then
8690 we can still try recording a simple equivalence. */
8691 if (vr_arg.type == VR_VARYING)
8693 vr_arg.type = VR_RANGE;
8694 vr_arg.min = arg;
8695 vr_arg.max = arg;
8696 vr_arg.equiv = NULL;
8700 else
8702 if (TREE_OVERFLOW_P (arg))
8703 arg = drop_tree_overflow (arg);
8705 vr_arg.type = VR_RANGE;
8706 vr_arg.min = arg;
8707 vr_arg.max = arg;
8708 vr_arg.equiv = NULL;
8711 if (dump_file && (dump_flags & TDF_DETAILS))
8713 fprintf (dump_file, "\t");
8714 print_generic_expr (dump_file, arg, dump_flags);
8715 fprintf (dump_file, ": ");
8716 dump_value_range (dump_file, &vr_arg);
8717 fprintf (dump_file, "\n");
8720 if (first)
8721 copy_value_range (&vr_result, &vr_arg);
8722 else
8723 vrp_meet (&vr_result, &vr_arg);
8724 first = false;
8726 if (vr_result.type == VR_VARYING)
8727 break;
8731 if (vr_result.type == VR_VARYING)
8732 goto varying;
8733 else if (vr_result.type == VR_UNDEFINED)
8734 goto update_range;
8736 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8737 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8739 /* To prevent infinite iterations in the algorithm, derive ranges
8740 when the new value is slightly bigger or smaller than the
8741 previous one. We don't do this if we have seen a new executable
8742 edge; this helps us avoid an overflow infinity for conditionals
8743 which are not in a loop. If the old value-range was VR_UNDEFINED
8744 use the updated range and iterate one more time. */
8745 if (edges > 0
8746 && gimple_phi_num_args (phi) > 1
8747 && edges == old_edges
8748 && lhs_vr->type != VR_UNDEFINED)
8750 /* Compare old and new ranges, fall back to varying if the
8751 values are not comparable. */
8752 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8753 if (cmp_min == -2)
8754 goto varying;
8755 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8756 if (cmp_max == -2)
8757 goto varying;
8759 /* For non VR_RANGE or for pointers fall back to varying if
8760 the range changed. */
8761 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8762 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8763 && (cmp_min != 0 || cmp_max != 0))
8764 goto varying;
8766 /* If the new minimum is larger than than the previous one
8767 retain the old value. If the new minimum value is smaller
8768 than the previous one and not -INF go all the way to -INF + 1.
8769 In the first case, to avoid infinite bouncing between different
8770 minimums, and in the other case to avoid iterating millions of
8771 times to reach -INF. Going to -INF + 1 also lets the following
8772 iteration compute whether there will be any overflow, at the
8773 expense of one additional iteration. */
8774 if (cmp_min < 0)
8775 vr_result.min = lhs_vr->min;
8776 else if (cmp_min > 0
8777 && !vrp_val_is_min (vr_result.min))
8778 vr_result.min
8779 = int_const_binop (PLUS_EXPR,
8780 vrp_val_min (TREE_TYPE (vr_result.min)),
8781 build_int_cst (TREE_TYPE (vr_result.min), 1));
8783 /* Similarly for the maximum value. */
8784 if (cmp_max > 0)
8785 vr_result.max = lhs_vr->max;
8786 else if (cmp_max < 0
8787 && !vrp_val_is_max (vr_result.max))
8788 vr_result.max
8789 = int_const_binop (MINUS_EXPR,
8790 vrp_val_max (TREE_TYPE (vr_result.min)),
8791 build_int_cst (TREE_TYPE (vr_result.min), 1));
8793 /* If we dropped either bound to +-INF then if this is a loop
8794 PHI node SCEV may known more about its value-range. */
8795 if ((cmp_min > 0 || cmp_min < 0
8796 || cmp_max < 0 || cmp_max > 0)
8797 && (l = loop_containing_stmt (phi))
8798 && l->header == gimple_bb (phi))
8799 adjust_range_with_scev (&vr_result, l, phi, lhs);
8801 /* If we will end up with a (-INF, +INF) range, set it to
8802 VARYING. Same if the previous max value was invalid for
8803 the type and we end up with vr_result.min > vr_result.max. */
8804 if ((vrp_val_is_max (vr_result.max)
8805 && vrp_val_is_min (vr_result.min))
8806 || compare_values (vr_result.min,
8807 vr_result.max) > 0)
8808 goto varying;
8811 /* If the new range is different than the previous value, keep
8812 iterating. */
8813 update_range:
8814 if (update_value_range (lhs, &vr_result))
8816 if (dump_file && (dump_flags & TDF_DETAILS))
8818 fprintf (dump_file, "Found new range for ");
8819 print_generic_expr (dump_file, lhs, 0);
8820 fprintf (dump_file, ": ");
8821 dump_value_range (dump_file, &vr_result);
8822 fprintf (dump_file, "\n");
8825 return SSA_PROP_INTERESTING;
8828 /* Nothing changed, don't add outgoing edges. */
8829 return SSA_PROP_NOT_INTERESTING;
8831 /* No match found. Set the LHS to VARYING. */
8832 varying:
8833 set_value_range_to_varying (lhs_vr);
8834 return SSA_PROP_VARYING;
8837 /* Simplify boolean operations if the source is known
8838 to be already a boolean. */
8839 static bool
8840 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8842 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8843 tree lhs, op0, op1;
8844 bool need_conversion;
8846 /* We handle only !=/== case here. */
8847 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8849 op0 = gimple_assign_rhs1 (stmt);
8850 if (!op_with_boolean_value_range_p (op0))
8851 return false;
8853 op1 = gimple_assign_rhs2 (stmt);
8854 if (!op_with_boolean_value_range_p (op1))
8855 return false;
8857 /* Reduce number of cases to handle to NE_EXPR. As there is no
8858 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8859 if (rhs_code == EQ_EXPR)
8861 if (TREE_CODE (op1) == INTEGER_CST)
8862 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8863 build_int_cst (TREE_TYPE (op1), 1));
8864 else
8865 return false;
8868 lhs = gimple_assign_lhs (stmt);
8869 need_conversion
8870 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8872 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8873 if (need_conversion
8874 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8875 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8876 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8877 return false;
8879 /* For A != 0 we can substitute A itself. */
8880 if (integer_zerop (op1))
8881 gimple_assign_set_rhs_with_ops (gsi,
8882 need_conversion
8883 ? NOP_EXPR : TREE_CODE (op0),
8884 op0, NULL_TREE);
8885 /* For A != B we substitute A ^ B. Either with conversion. */
8886 else if (need_conversion)
8888 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8889 gassign *newop =
8890 gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8891 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8892 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8894 /* Or without. */
8895 else
8896 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8897 update_stmt (gsi_stmt (*gsi));
8899 return true;
8902 /* Simplify a division or modulo operator to a right shift or
8903 bitwise and if the first operand is unsigned or is greater
8904 than zero and the second operand is an exact power of two. */
8906 static bool
8907 simplify_div_or_mod_using_ranges (gimple stmt)
8909 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8910 tree val = NULL;
8911 tree op0 = gimple_assign_rhs1 (stmt);
8912 tree op1 = gimple_assign_rhs2 (stmt);
8913 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8915 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8917 val = integer_one_node;
8919 else
8921 bool sop = false;
8923 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8925 if (val
8926 && sop
8927 && integer_onep (val)
8928 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8930 location_t location;
8932 if (!gimple_has_location (stmt))
8933 location = input_location;
8934 else
8935 location = gimple_location (stmt);
8936 warning_at (location, OPT_Wstrict_overflow,
8937 "assuming signed overflow does not occur when "
8938 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8942 if (val && integer_onep (val))
8944 tree t;
8946 if (rhs_code == TRUNC_DIV_EXPR)
8948 t = build_int_cst (integer_type_node, tree_log2 (op1));
8949 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8950 gimple_assign_set_rhs1 (stmt, op0);
8951 gimple_assign_set_rhs2 (stmt, t);
8953 else
8955 t = build_int_cst (TREE_TYPE (op1), 1);
8956 t = int_const_binop (MINUS_EXPR, op1, t);
8957 t = fold_convert (TREE_TYPE (op0), t);
8959 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8960 gimple_assign_set_rhs1 (stmt, op0);
8961 gimple_assign_set_rhs2 (stmt, t);
8964 update_stmt (stmt);
8965 return true;
8968 return false;
8971 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8972 ABS_EXPR. If the operand is <= 0, then simplify the
8973 ABS_EXPR into a NEGATE_EXPR. */
8975 static bool
8976 simplify_abs_using_ranges (gimple stmt)
8978 tree val = NULL;
8979 tree op = gimple_assign_rhs1 (stmt);
8980 tree type = TREE_TYPE (op);
8981 value_range_t *vr = get_value_range (op);
8983 if (TYPE_UNSIGNED (type))
8985 val = integer_zero_node;
8987 else if (vr)
8989 bool sop = false;
8991 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8992 if (!val)
8994 sop = false;
8995 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8996 &sop);
8998 if (val)
9000 if (integer_zerop (val))
9001 val = integer_one_node;
9002 else if (integer_onep (val))
9003 val = integer_zero_node;
9007 if (val
9008 && (integer_onep (val) || integer_zerop (val)))
9010 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9012 location_t location;
9014 if (!gimple_has_location (stmt))
9015 location = input_location;
9016 else
9017 location = gimple_location (stmt);
9018 warning_at (location, OPT_Wstrict_overflow,
9019 "assuming signed overflow does not occur when "
9020 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9023 gimple_assign_set_rhs1 (stmt, op);
9024 if (integer_onep (val))
9025 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9026 else
9027 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9028 update_stmt (stmt);
9029 return true;
9033 return false;
9036 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9037 If all the bits that are being cleared by & are already
9038 known to be zero from VR, or all the bits that are being
9039 set by | are already known to be one from VR, the bit
9040 operation is redundant. */
9042 static bool
9043 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9045 tree op0 = gimple_assign_rhs1 (stmt);
9046 tree op1 = gimple_assign_rhs2 (stmt);
9047 tree op = NULL_TREE;
9048 value_range_t vr0 = VR_INITIALIZER;
9049 value_range_t vr1 = VR_INITIALIZER;
9050 wide_int may_be_nonzero0, may_be_nonzero1;
9051 wide_int must_be_nonzero0, must_be_nonzero1;
9052 wide_int mask;
9054 if (TREE_CODE (op0) == SSA_NAME)
9055 vr0 = *(get_value_range (op0));
9056 else if (is_gimple_min_invariant (op0))
9057 set_value_range_to_value (&vr0, op0, NULL);
9058 else
9059 return false;
9061 if (TREE_CODE (op1) == SSA_NAME)
9062 vr1 = *(get_value_range (op1));
9063 else if (is_gimple_min_invariant (op1))
9064 set_value_range_to_value (&vr1, op1, NULL);
9065 else
9066 return false;
9068 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9069 &must_be_nonzero0))
9070 return false;
9071 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9072 &must_be_nonzero1))
9073 return false;
9075 switch (gimple_assign_rhs_code (stmt))
9077 case BIT_AND_EXPR:
9078 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9079 if (mask == 0)
9081 op = op0;
9082 break;
9084 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9085 if (mask == 0)
9087 op = op1;
9088 break;
9090 break;
9091 case BIT_IOR_EXPR:
9092 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9093 if (mask == 0)
9095 op = op1;
9096 break;
9098 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9099 if (mask == 0)
9101 op = op0;
9102 break;
9104 break;
9105 default:
9106 gcc_unreachable ();
9109 if (op == NULL_TREE)
9110 return false;
9112 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
9113 update_stmt (gsi_stmt (*gsi));
9114 return true;
9117 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9118 a known value range VR.
9120 If there is one and only one value which will satisfy the
9121 conditional, then return that value. Else return NULL. */
9123 static tree
9124 test_for_singularity (enum tree_code cond_code, tree op0,
9125 tree op1, value_range_t *vr)
9127 tree min = NULL;
9128 tree max = NULL;
9130 /* Extract minimum/maximum values which satisfy the
9131 the conditional as it was written. */
9132 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9134 /* This should not be negative infinity; there is no overflow
9135 here. */
9136 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9138 max = op1;
9139 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9141 tree one = build_int_cst (TREE_TYPE (op0), 1);
9142 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9143 if (EXPR_P (max))
9144 TREE_NO_WARNING (max) = 1;
9147 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9149 /* This should not be positive infinity; there is no overflow
9150 here. */
9151 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9153 min = op1;
9154 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9156 tree one = build_int_cst (TREE_TYPE (op0), 1);
9157 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9158 if (EXPR_P (min))
9159 TREE_NO_WARNING (min) = 1;
9163 /* Now refine the minimum and maximum values using any
9164 value range information we have for op0. */
9165 if (min && max)
9167 if (compare_values (vr->min, min) == 1)
9168 min = vr->min;
9169 if (compare_values (vr->max, max) == -1)
9170 max = vr->max;
9172 /* If the new min/max values have converged to a single value,
9173 then there is only one value which can satisfy the condition,
9174 return that value. */
9175 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9176 return min;
9178 return NULL;
9181 /* Return whether the value range *VR fits in an integer type specified
9182 by PRECISION and UNSIGNED_P. */
9184 static bool
9185 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9187 tree src_type;
9188 unsigned src_precision;
9189 widest_int tem;
9190 signop src_sgn;
9192 /* We can only handle integral and pointer types. */
9193 src_type = TREE_TYPE (vr->min);
9194 if (!INTEGRAL_TYPE_P (src_type)
9195 && !POINTER_TYPE_P (src_type))
9196 return false;
9198 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9199 and so is an identity transform. */
9200 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9201 src_sgn = TYPE_SIGN (src_type);
9202 if ((src_precision < dest_precision
9203 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9204 || (src_precision == dest_precision && src_sgn == dest_sgn))
9205 return true;
9207 /* Now we can only handle ranges with constant bounds. */
9208 if (vr->type != VR_RANGE
9209 || TREE_CODE (vr->min) != INTEGER_CST
9210 || TREE_CODE (vr->max) != INTEGER_CST)
9211 return false;
9213 /* For sign changes, the MSB of the wide_int has to be clear.
9214 An unsigned value with its MSB set cannot be represented by
9215 a signed wide_int, while a negative value cannot be represented
9216 by an unsigned wide_int. */
9217 if (src_sgn != dest_sgn
9218 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9219 return false;
9221 /* Then we can perform the conversion on both ends and compare
9222 the result for equality. */
9223 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9224 if (tem != wi::to_widest (vr->min))
9225 return false;
9226 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9227 if (tem != wi::to_widest (vr->max))
9228 return false;
9230 return true;
9233 /* Simplify a conditional using a relational operator to an equality
9234 test if the range information indicates only one value can satisfy
9235 the original conditional. */
9237 static bool
9238 simplify_cond_using_ranges (gcond *stmt)
9240 tree op0 = gimple_cond_lhs (stmt);
9241 tree op1 = gimple_cond_rhs (stmt);
9242 enum tree_code cond_code = gimple_cond_code (stmt);
9244 if (cond_code != NE_EXPR
9245 && cond_code != EQ_EXPR
9246 && TREE_CODE (op0) == SSA_NAME
9247 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9248 && is_gimple_min_invariant (op1))
9250 value_range_t *vr = get_value_range (op0);
9252 /* If we have range information for OP0, then we might be
9253 able to simplify this conditional. */
9254 if (vr->type == VR_RANGE)
9256 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
9258 if (new_tree)
9260 if (dump_file)
9262 fprintf (dump_file, "Simplified relational ");
9263 print_gimple_stmt (dump_file, stmt, 0, 0);
9264 fprintf (dump_file, " into ");
9267 gimple_cond_set_code (stmt, EQ_EXPR);
9268 gimple_cond_set_lhs (stmt, op0);
9269 gimple_cond_set_rhs (stmt, new_tree);
9271 update_stmt (stmt);
9273 if (dump_file)
9275 print_gimple_stmt (dump_file, stmt, 0, 0);
9276 fprintf (dump_file, "\n");
9279 return true;
9282 /* Try again after inverting the condition. We only deal
9283 with integral types here, so no need to worry about
9284 issues with inverting FP comparisons. */
9285 cond_code = invert_tree_comparison (cond_code, false);
9286 new_tree = test_for_singularity (cond_code, op0, op1, vr);
9288 if (new_tree)
9290 if (dump_file)
9292 fprintf (dump_file, "Simplified relational ");
9293 print_gimple_stmt (dump_file, stmt, 0, 0);
9294 fprintf (dump_file, " into ");
9297 gimple_cond_set_code (stmt, NE_EXPR);
9298 gimple_cond_set_lhs (stmt, op0);
9299 gimple_cond_set_rhs (stmt, new_tree);
9301 update_stmt (stmt);
9303 if (dump_file)
9305 print_gimple_stmt (dump_file, stmt, 0, 0);
9306 fprintf (dump_file, "\n");
9309 return true;
9314 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9315 see if OP0 was set by a type conversion where the source of
9316 the conversion is another SSA_NAME with a range that fits
9317 into the range of OP0's type.
9319 If so, the conversion is redundant as the earlier SSA_NAME can be
9320 used for the comparison directly if we just massage the constant in the
9321 comparison. */
9322 if (TREE_CODE (op0) == SSA_NAME
9323 && TREE_CODE (op1) == INTEGER_CST)
9325 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9326 tree innerop;
9328 if (!is_gimple_assign (def_stmt)
9329 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9330 return false;
9332 innerop = gimple_assign_rhs1 (def_stmt);
9334 if (TREE_CODE (innerop) == SSA_NAME
9335 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9337 value_range_t *vr = get_value_range (innerop);
9339 if (range_int_cst_p (vr)
9340 && range_fits_type_p (vr,
9341 TYPE_PRECISION (TREE_TYPE (op0)),
9342 TYPE_SIGN (TREE_TYPE (op0)))
9343 && int_fits_type_p (op1, TREE_TYPE (innerop))
9344 /* The range must not have overflowed, or if it did overflow
9345 we must not be wrapping/trapping overflow and optimizing
9346 with strict overflow semantics. */
9347 && ((!is_negative_overflow_infinity (vr->min)
9348 && !is_positive_overflow_infinity (vr->max))
9349 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9351 /* If the range overflowed and the user has asked for warnings
9352 when strict overflow semantics were used to optimize code,
9353 issue an appropriate warning. */
9354 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9355 && (is_negative_overflow_infinity (vr->min)
9356 || is_positive_overflow_infinity (vr->max))
9357 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9359 location_t location;
9361 if (!gimple_has_location (stmt))
9362 location = input_location;
9363 else
9364 location = gimple_location (stmt);
9365 warning_at (location, OPT_Wstrict_overflow,
9366 "assuming signed overflow does not occur when "
9367 "simplifying conditional");
9370 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9371 gimple_cond_set_lhs (stmt, innerop);
9372 gimple_cond_set_rhs (stmt, newconst);
9373 return true;
9378 return false;
9381 /* Simplify a switch statement using the value range of the switch
9382 argument. */
9384 static bool
9385 simplify_switch_using_ranges (gswitch *stmt)
9387 tree op = gimple_switch_index (stmt);
9388 value_range_t *vr;
9389 bool take_default;
9390 edge e;
9391 edge_iterator ei;
9392 size_t i = 0, j = 0, n, n2;
9393 tree vec2;
9394 switch_update su;
9395 size_t k = 1, l = 0;
9397 if (TREE_CODE (op) == SSA_NAME)
9399 vr = get_value_range (op);
9401 /* We can only handle integer ranges. */
9402 if ((vr->type != VR_RANGE
9403 && vr->type != VR_ANTI_RANGE)
9404 || symbolic_range_p (vr))
9405 return false;
9407 /* Find case label for min/max of the value range. */
9408 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9410 else if (TREE_CODE (op) == INTEGER_CST)
9412 take_default = !find_case_label_index (stmt, 1, op, &i);
9413 if (take_default)
9415 i = 1;
9416 j = 0;
9418 else
9420 j = i;
9423 else
9424 return false;
9426 n = gimple_switch_num_labels (stmt);
9428 /* Bail out if this is just all edges taken. */
9429 if (i == 1
9430 && j == n - 1
9431 && take_default)
9432 return false;
9434 /* Build a new vector of taken case labels. */
9435 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9436 n2 = 0;
9438 /* Add the default edge, if necessary. */
9439 if (take_default)
9440 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9442 for (; i <= j; ++i, ++n2)
9443 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9445 for (; k <= l; ++k, ++n2)
9446 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9448 /* Mark needed edges. */
9449 for (i = 0; i < n2; ++i)
9451 e = find_edge (gimple_bb (stmt),
9452 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9453 e->aux = (void *)-1;
9456 /* Queue not needed edges for later removal. */
9457 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9459 if (e->aux == (void *)-1)
9461 e->aux = NULL;
9462 continue;
9465 if (dump_file && (dump_flags & TDF_DETAILS))
9467 fprintf (dump_file, "removing unreachable case label\n");
9469 to_remove_edges.safe_push (e);
9470 e->flags &= ~EDGE_EXECUTABLE;
9473 /* And queue an update for the stmt. */
9474 su.stmt = stmt;
9475 su.vec = vec2;
9476 to_update_switch_stmts.safe_push (su);
9477 return false;
9480 /* Simplify an integral conversion from an SSA name in STMT. */
9482 static bool
9483 simplify_conversion_using_ranges (gimple stmt)
9485 tree innerop, middleop, finaltype;
9486 gimple def_stmt;
9487 value_range_t *innervr;
9488 signop inner_sgn, middle_sgn, final_sgn;
9489 unsigned inner_prec, middle_prec, final_prec;
9490 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9492 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9493 if (!INTEGRAL_TYPE_P (finaltype))
9494 return false;
9495 middleop = gimple_assign_rhs1 (stmt);
9496 def_stmt = SSA_NAME_DEF_STMT (middleop);
9497 if (!is_gimple_assign (def_stmt)
9498 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9499 return false;
9500 innerop = gimple_assign_rhs1 (def_stmt);
9501 if (TREE_CODE (innerop) != SSA_NAME
9502 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9503 return false;
9505 /* Get the value-range of the inner operand. */
9506 innervr = get_value_range (innerop);
9507 if (innervr->type != VR_RANGE
9508 || TREE_CODE (innervr->min) != INTEGER_CST
9509 || TREE_CODE (innervr->max) != INTEGER_CST)
9510 return false;
9512 /* Simulate the conversion chain to check if the result is equal if
9513 the middle conversion is removed. */
9514 innermin = wi::to_widest (innervr->min);
9515 innermax = wi::to_widest (innervr->max);
9517 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9518 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9519 final_prec = TYPE_PRECISION (finaltype);
9521 /* If the first conversion is not injective, the second must not
9522 be widening. */
9523 if (wi::gtu_p (innermax - innermin,
9524 wi::mask <widest_int> (middle_prec, false))
9525 && middle_prec < final_prec)
9526 return false;
9527 /* We also want a medium value so that we can track the effect that
9528 narrowing conversions with sign change have. */
9529 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9530 if (inner_sgn == UNSIGNED)
9531 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9532 else
9533 innermed = 0;
9534 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9535 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9536 innermed = innermin;
9538 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9539 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9540 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9541 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9543 /* Require that the final conversion applied to both the original
9544 and the intermediate range produces the same result. */
9545 final_sgn = TYPE_SIGN (finaltype);
9546 if (wi::ext (middlemin, final_prec, final_sgn)
9547 != wi::ext (innermin, final_prec, final_sgn)
9548 || wi::ext (middlemed, final_prec, final_sgn)
9549 != wi::ext (innermed, final_prec, final_sgn)
9550 || wi::ext (middlemax, final_prec, final_sgn)
9551 != wi::ext (innermax, final_prec, final_sgn))
9552 return false;
9554 gimple_assign_set_rhs1 (stmt, innerop);
9555 update_stmt (stmt);
9556 return true;
9559 /* Simplify a conversion from integral SSA name to float in STMT. */
9561 static bool
9562 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9564 tree rhs1 = gimple_assign_rhs1 (stmt);
9565 value_range_t *vr = get_value_range (rhs1);
9566 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9567 machine_mode mode;
9568 tree tem;
9569 gassign *conv;
9571 /* We can only handle constant ranges. */
9572 if (vr->type != VR_RANGE
9573 || TREE_CODE (vr->min) != INTEGER_CST
9574 || TREE_CODE (vr->max) != INTEGER_CST)
9575 return false;
9577 /* First check if we can use a signed type in place of an unsigned. */
9578 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9579 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9580 != CODE_FOR_nothing)
9581 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9582 mode = TYPE_MODE (TREE_TYPE (rhs1));
9583 /* If we can do the conversion in the current input mode do nothing. */
9584 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9585 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9586 return false;
9587 /* Otherwise search for a mode we can use, starting from the narrowest
9588 integer mode available. */
9589 else
9591 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9594 /* If we cannot do a signed conversion to float from mode
9595 or if the value-range does not fit in the signed type
9596 try with a wider mode. */
9597 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9598 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9599 break;
9601 mode = GET_MODE_WIDER_MODE (mode);
9602 /* But do not widen the input. Instead leave that to the
9603 optabs expansion code. */
9604 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9605 return false;
9607 while (mode != VOIDmode);
9608 if (mode == VOIDmode)
9609 return false;
9612 /* It works, insert a truncation or sign-change before the
9613 float conversion. */
9614 tem = make_ssa_name (build_nonstandard_integer_type
9615 (GET_MODE_PRECISION (mode), 0), NULL);
9616 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
9617 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9618 gimple_assign_set_rhs1 (stmt, tem);
9619 update_stmt (stmt);
9621 return true;
9624 /* Simplify an internal fn call using ranges if possible. */
9626 static bool
9627 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9629 enum tree_code subcode;
9630 bool is_ubsan = false;
9631 bool ovf = false;
9632 switch (gimple_call_internal_fn (stmt))
9634 case IFN_UBSAN_CHECK_ADD:
9635 subcode = PLUS_EXPR;
9636 is_ubsan = true;
9637 break;
9638 case IFN_UBSAN_CHECK_SUB:
9639 subcode = MINUS_EXPR;
9640 is_ubsan = true;
9641 break;
9642 case IFN_UBSAN_CHECK_MUL:
9643 subcode = MULT_EXPR;
9644 is_ubsan = true;
9645 break;
9646 case IFN_ADD_OVERFLOW:
9647 subcode = PLUS_EXPR;
9648 break;
9649 case IFN_SUB_OVERFLOW:
9650 subcode = MINUS_EXPR;
9651 break;
9652 case IFN_MUL_OVERFLOW:
9653 subcode = MULT_EXPR;
9654 break;
9655 default:
9656 return false;
9659 tree op0 = gimple_call_arg (stmt, 0);
9660 tree op1 = gimple_call_arg (stmt, 1);
9661 tree type;
9662 if (is_ubsan)
9663 type = TREE_TYPE (op0);
9664 else if (gimple_call_lhs (stmt) == NULL_TREE)
9665 return false;
9666 else
9667 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9668 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9669 || (is_ubsan && ovf))
9670 return false;
9672 gimple g;
9673 location_t loc = gimple_location (stmt);
9674 if (is_ubsan)
9675 g = gimple_build_assign_with_ops (subcode, gimple_call_lhs (stmt),
9676 op0, op1);
9677 else
9679 int prec = TYPE_PRECISION (type);
9680 tree utype = type;
9681 if (ovf
9682 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9683 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9684 utype = build_nonstandard_integer_type (prec, 1);
9685 if (TREE_CODE (op0) == INTEGER_CST)
9686 op0 = fold_convert (utype, op0);
9687 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9689 g = gimple_build_assign_with_ops (NOP_EXPR,
9690 make_ssa_name (utype, NULL),
9691 op0, NULL_TREE);
9692 gimple_set_location (g, loc);
9693 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9694 op0 = gimple_assign_lhs (g);
9696 if (TREE_CODE (op1) == INTEGER_CST)
9697 op1 = fold_convert (utype, op1);
9698 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9700 g = gimple_build_assign_with_ops (NOP_EXPR,
9701 make_ssa_name (utype, NULL),
9702 op1, NULL_TREE);
9703 gimple_set_location (g, loc);
9704 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9705 op1 = gimple_assign_lhs (g);
9707 g = gimple_build_assign_with_ops (subcode, make_ssa_name (utype, NULL),
9708 op0, op1);
9709 gimple_set_location (g, loc);
9710 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9711 if (utype != type)
9713 g = gimple_build_assign_with_ops (NOP_EXPR,
9714 make_ssa_name (type, NULL),
9715 gimple_assign_lhs (g), NULL_TREE);
9716 gimple_set_location (g, loc);
9717 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9719 g = gimple_build_assign_with_ops (COMPLEX_EXPR, gimple_call_lhs (stmt),
9720 gimple_assign_lhs (g),
9721 build_int_cst (type, ovf));
9723 gimple_set_location (g, loc);
9724 gsi_replace (gsi, g, false);
9725 return true;
9728 /* Simplify STMT using ranges if possible. */
9730 static bool
9731 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9733 gimple stmt = gsi_stmt (*gsi);
9734 if (is_gimple_assign (stmt))
9736 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9737 tree rhs1 = gimple_assign_rhs1 (stmt);
9739 switch (rhs_code)
9741 case EQ_EXPR:
9742 case NE_EXPR:
9743 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9744 if the RHS is zero or one, and the LHS are known to be boolean
9745 values. */
9746 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9747 return simplify_truth_ops_using_ranges (gsi, stmt);
9748 break;
9750 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9751 and BIT_AND_EXPR respectively if the first operand is greater
9752 than zero and the second operand is an exact power of two. */
9753 case TRUNC_DIV_EXPR:
9754 case TRUNC_MOD_EXPR:
9755 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9756 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9757 return simplify_div_or_mod_using_ranges (stmt);
9758 break;
9760 /* Transform ABS (X) into X or -X as appropriate. */
9761 case ABS_EXPR:
9762 if (TREE_CODE (rhs1) == SSA_NAME
9763 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9764 return simplify_abs_using_ranges (stmt);
9765 break;
9767 case BIT_AND_EXPR:
9768 case BIT_IOR_EXPR:
9769 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9770 if all the bits being cleared are already cleared or
9771 all the bits being set are already set. */
9772 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9773 return simplify_bit_ops_using_ranges (gsi, stmt);
9774 break;
9776 CASE_CONVERT:
9777 if (TREE_CODE (rhs1) == SSA_NAME
9778 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9779 return simplify_conversion_using_ranges (stmt);
9780 break;
9782 case FLOAT_EXPR:
9783 if (TREE_CODE (rhs1) == SSA_NAME
9784 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9785 return simplify_float_conversion_using_ranges (gsi, stmt);
9786 break;
9788 default:
9789 break;
9792 else if (gimple_code (stmt) == GIMPLE_COND)
9793 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9794 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9795 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
9796 else if (is_gimple_call (stmt)
9797 && gimple_call_internal_p (stmt))
9798 return simplify_internal_call_using_ranges (gsi, stmt);
9800 return false;
9803 /* If the statement pointed by SI has a predicate whose value can be
9804 computed using the value range information computed by VRP, compute
9805 its value and return true. Otherwise, return false. */
9807 static bool
9808 fold_predicate_in (gimple_stmt_iterator *si)
9810 bool assignment_p = false;
9811 tree val;
9812 gimple stmt = gsi_stmt (*si);
9814 if (is_gimple_assign (stmt)
9815 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9817 assignment_p = true;
9818 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9819 gimple_assign_rhs1 (stmt),
9820 gimple_assign_rhs2 (stmt),
9821 stmt);
9823 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
9824 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
9825 gimple_cond_lhs (cond_stmt),
9826 gimple_cond_rhs (cond_stmt),
9827 stmt);
9828 else
9829 return false;
9831 if (val)
9833 if (assignment_p)
9834 val = fold_convert (gimple_expr_type (stmt), val);
9836 if (dump_file)
9838 fprintf (dump_file, "Folding predicate ");
9839 print_gimple_expr (dump_file, stmt, 0, 0);
9840 fprintf (dump_file, " to ");
9841 print_generic_expr (dump_file, val, 0);
9842 fprintf (dump_file, "\n");
9845 if (is_gimple_assign (stmt))
9846 gimple_assign_set_rhs_from_tree (si, val);
9847 else
9849 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9850 gcond *cond_stmt = as_a <gcond *> (stmt);
9851 if (integer_zerop (val))
9852 gimple_cond_make_false (cond_stmt);
9853 else if (integer_onep (val))
9854 gimple_cond_make_true (cond_stmt);
9855 else
9856 gcc_unreachable ();
9859 return true;
9862 return false;
9865 /* Callback for substitute_and_fold folding the stmt at *SI. */
9867 static bool
9868 vrp_fold_stmt (gimple_stmt_iterator *si)
9870 if (fold_predicate_in (si))
9871 return true;
9873 return simplify_stmt_using_ranges (si);
9876 /* Stack of dest,src equivalency pairs that need to be restored after
9877 each attempt to thread a block's incoming edge to an outgoing edge.
9879 A NULL entry is used to mark the end of pairs which need to be
9880 restored. */
9881 static vec<tree> equiv_stack;
9883 /* A trivial wrapper so that we can present the generic jump threading
9884 code with a simple API for simplifying statements. STMT is the
9885 statement we want to simplify, WITHIN_STMT provides the location
9886 for any overflow warnings. */
9888 static tree
9889 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9891 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
9892 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
9893 gimple_cond_lhs (cond_stmt),
9894 gimple_cond_rhs (cond_stmt),
9895 within_stmt);
9897 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
9899 value_range_t new_vr = VR_INITIALIZER;
9900 tree lhs = gimple_assign_lhs (assign_stmt);
9902 if (TREE_CODE (lhs) == SSA_NAME
9903 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
9904 || POINTER_TYPE_P (TREE_TYPE (lhs))))
9906 extract_range_from_assignment (&new_vr, assign_stmt);
9907 if (range_int_cst_singleton_p (&new_vr))
9908 return new_vr.min;
9912 return NULL_TREE;
9915 /* Blocks which have more than one predecessor and more than
9916 one successor present jump threading opportunities, i.e.,
9917 when the block is reached from a specific predecessor, we
9918 may be able to determine which of the outgoing edges will
9919 be traversed. When this optimization applies, we are able
9920 to avoid conditionals at runtime and we may expose secondary
9921 optimization opportunities.
9923 This routine is effectively a driver for the generic jump
9924 threading code. It basically just presents the generic code
9925 with edges that may be suitable for jump threading.
9927 Unlike DOM, we do not iterate VRP if jump threading was successful.
9928 While iterating may expose new opportunities for VRP, it is expected
9929 those opportunities would be very limited and the compile time cost
9930 to expose those opportunities would be significant.
9932 As jump threading opportunities are discovered, they are registered
9933 for later realization. */
9935 static void
9936 identify_jump_threads (void)
9938 basic_block bb;
9939 gcond *dummy;
9940 int i;
9941 edge e;
9943 /* Ugh. When substituting values earlier in this pass we can
9944 wipe the dominance information. So rebuild the dominator
9945 information as we need it within the jump threading code. */
9946 calculate_dominance_info (CDI_DOMINATORS);
9948 /* We do not allow VRP information to be used for jump threading
9949 across a back edge in the CFG. Otherwise it becomes too
9950 difficult to avoid eliminating loop exit tests. Of course
9951 EDGE_DFS_BACK is not accurate at this time so we have to
9952 recompute it. */
9953 mark_dfs_back_edges ();
9955 /* Do not thread across edges we are about to remove. Just marking
9956 them as EDGE_DFS_BACK will do. */
9957 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9958 e->flags |= EDGE_DFS_BACK;
9960 /* Allocate our unwinder stack to unwind any temporary equivalences
9961 that might be recorded. */
9962 equiv_stack.create (20);
9964 /* To avoid lots of silly node creation, we create a single
9965 conditional and just modify it in-place when attempting to
9966 thread jumps. */
9967 dummy = gimple_build_cond (EQ_EXPR,
9968 integer_zero_node, integer_zero_node,
9969 NULL, NULL);
9971 /* Walk through all the blocks finding those which present a
9972 potential jump threading opportunity. We could set this up
9973 as a dominator walker and record data during the walk, but
9974 I doubt it's worth the effort for the classes of jump
9975 threading opportunities we are trying to identify at this
9976 point in compilation. */
9977 FOR_EACH_BB_FN (bb, cfun)
9979 gimple last;
9981 /* If the generic jump threading code does not find this block
9982 interesting, then there is nothing to do. */
9983 if (! potentially_threadable_block (bb))
9984 continue;
9986 /* We only care about blocks ending in a COND_EXPR. While there
9987 may be some value in handling SWITCH_EXPR here, I doubt it's
9988 terribly important. */
9989 last = gsi_stmt (gsi_last_bb (bb));
9991 /* We're basically looking for a switch or any kind of conditional with
9992 integral or pointer type arguments. Note the type of the second
9993 argument will be the same as the first argument, so no need to
9994 check it explicitly. */
9995 if (gimple_code (last) == GIMPLE_SWITCH
9996 || (gimple_code (last) == GIMPLE_COND
9997 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9998 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9999 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10000 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10001 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10003 edge_iterator ei;
10005 /* We've got a block with multiple predecessors and multiple
10006 successors which also ends in a suitable conditional or
10007 switch statement. For each predecessor, see if we can thread
10008 it to a specific successor. */
10009 FOR_EACH_EDGE (e, ei, bb->preds)
10011 /* Do not thread across back edges or abnormal edges
10012 in the CFG. */
10013 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
10014 continue;
10016 thread_across_edge (dummy, e, true, &equiv_stack,
10017 simplify_stmt_for_jump_threading);
10022 /* We do not actually update the CFG or SSA graphs at this point as
10023 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10024 handle ASSERT_EXPRs gracefully. */
10027 /* We identified all the jump threading opportunities earlier, but could
10028 not transform the CFG at that time. This routine transforms the
10029 CFG and arranges for the dominator tree to be rebuilt if necessary.
10031 Note the SSA graph update will occur during the normal TODO
10032 processing by the pass manager. */
10033 static void
10034 finalize_jump_threads (void)
10036 thread_through_all_blocks (false);
10037 equiv_stack.release ();
10041 /* Traverse all the blocks folding conditionals with known ranges. */
10043 static void
10044 vrp_finalize (void)
10046 size_t i;
10048 values_propagated = true;
10050 if (dump_file)
10052 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10053 dump_all_value_ranges (dump_file);
10054 fprintf (dump_file, "\n");
10057 substitute_and_fold (op_with_constant_singleton_value_range,
10058 vrp_fold_stmt, false);
10060 if (warn_array_bounds)
10061 check_all_array_refs ();
10063 /* We must identify jump threading opportunities before we release
10064 the datastructures built by VRP. */
10065 identify_jump_threads ();
10067 /* Set value range to non pointer SSA_NAMEs. */
10068 for (i = 0; i < num_vr_values; i++)
10069 if (vr_value[i])
10071 tree name = ssa_name (i);
10073 if (!name
10074 || POINTER_TYPE_P (TREE_TYPE (name))
10075 || (vr_value[i]->type == VR_VARYING)
10076 || (vr_value[i]->type == VR_UNDEFINED))
10077 continue;
10079 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10080 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10081 && (vr_value[i]->type == VR_RANGE
10082 || vr_value[i]->type == VR_ANTI_RANGE))
10083 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10084 vr_value[i]->max);
10087 /* Free allocated memory. */
10088 for (i = 0; i < num_vr_values; i++)
10089 if (vr_value[i])
10091 BITMAP_FREE (vr_value[i]->equiv);
10092 free (vr_value[i]);
10095 free (vr_value);
10096 free (vr_phi_edge_counts);
10098 /* So that we can distinguish between VRP data being available
10099 and not available. */
10100 vr_value = NULL;
10101 vr_phi_edge_counts = NULL;
10105 /* Main entry point to VRP (Value Range Propagation). This pass is
10106 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10107 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10108 Programming Language Design and Implementation, pp. 67-78, 1995.
10109 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10111 This is essentially an SSA-CCP pass modified to deal with ranges
10112 instead of constants.
10114 While propagating ranges, we may find that two or more SSA name
10115 have equivalent, though distinct ranges. For instance,
10117 1 x_9 = p_3->a;
10118 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10119 3 if (p_4 == q_2)
10120 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10121 5 endif
10122 6 if (q_2)
10124 In the code above, pointer p_5 has range [q_2, q_2], but from the
10125 code we can also determine that p_5 cannot be NULL and, if q_2 had
10126 a non-varying range, p_5's range should also be compatible with it.
10128 These equivalences are created by two expressions: ASSERT_EXPR and
10129 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10130 result of another assertion, then we can use the fact that p_5 and
10131 p_4 are equivalent when evaluating p_5's range.
10133 Together with value ranges, we also propagate these equivalences
10134 between names so that we can take advantage of information from
10135 multiple ranges when doing final replacement. Note that this
10136 equivalency relation is transitive but not symmetric.
10138 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10139 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10140 in contexts where that assertion does not hold (e.g., in line 6).
10142 TODO, the main difference between this pass and Patterson's is that
10143 we do not propagate edge probabilities. We only compute whether
10144 edges can be taken or not. That is, instead of having a spectrum
10145 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10146 DON'T KNOW. In the future, it may be worthwhile to propagate
10147 probabilities to aid branch prediction. */
10149 static unsigned int
10150 execute_vrp (void)
10152 int i;
10153 edge e;
10154 switch_update *su;
10156 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10157 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10158 scev_initialize ();
10160 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10161 Inserting assertions may split edges which will invalidate
10162 EDGE_DFS_BACK. */
10163 insert_range_assertions ();
10165 to_remove_edges.create (10);
10166 to_update_switch_stmts.create (5);
10167 threadedge_initialize_values ();
10169 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10170 mark_dfs_back_edges ();
10172 vrp_initialize ();
10173 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10174 vrp_finalize ();
10176 free_numbers_of_iterations_estimates ();
10178 /* ASSERT_EXPRs must be removed before finalizing jump threads
10179 as finalizing jump threads calls the CFG cleanup code which
10180 does not properly handle ASSERT_EXPRs. */
10181 remove_range_assertions ();
10183 /* If we exposed any new variables, go ahead and put them into
10184 SSA form now, before we handle jump threading. This simplifies
10185 interactions between rewriting of _DECL nodes into SSA form
10186 and rewriting SSA_NAME nodes into SSA form after block
10187 duplication and CFG manipulation. */
10188 update_ssa (TODO_update_ssa);
10190 finalize_jump_threads ();
10192 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10193 CFG in a broken state and requires a cfg_cleanup run. */
10194 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10195 remove_edge (e);
10196 /* Update SWITCH_EXPR case label vector. */
10197 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10199 size_t j;
10200 size_t n = TREE_VEC_LENGTH (su->vec);
10201 tree label;
10202 gimple_switch_set_num_labels (su->stmt, n);
10203 for (j = 0; j < n; j++)
10204 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10205 /* As we may have replaced the default label with a regular one
10206 make sure to make it a real default label again. This ensures
10207 optimal expansion. */
10208 label = gimple_switch_label (su->stmt, 0);
10209 CASE_LOW (label) = NULL_TREE;
10210 CASE_HIGH (label) = NULL_TREE;
10213 if (to_remove_edges.length () > 0)
10215 free_dominance_info (CDI_DOMINATORS);
10216 loops_state_set (LOOPS_NEED_FIXUP);
10219 to_remove_edges.release ();
10220 to_update_switch_stmts.release ();
10221 threadedge_finalize_values ();
10223 scev_finalize ();
10224 loop_optimizer_finalize ();
10225 return 0;
10228 namespace {
10230 const pass_data pass_data_vrp =
10232 GIMPLE_PASS, /* type */
10233 "vrp", /* name */
10234 OPTGROUP_NONE, /* optinfo_flags */
10235 TV_TREE_VRP, /* tv_id */
10236 PROP_ssa, /* properties_required */
10237 0, /* properties_provided */
10238 0, /* properties_destroyed */
10239 0, /* todo_flags_start */
10240 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10243 class pass_vrp : public gimple_opt_pass
10245 public:
10246 pass_vrp (gcc::context *ctxt)
10247 : gimple_opt_pass (pass_data_vrp, ctxt)
10250 /* opt_pass methods: */
10251 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10252 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10253 virtual unsigned int execute (function *) { return execute_vrp (); }
10255 }; // class pass_vrp
10257 } // anon namespace
10259 gimple_opt_pass *
10260 make_pass_vrp (gcc::context *ctxt)
10262 return new pass_vrp (ctxt);