[ARM] Fix CLZ_DEFINED_VALUE_AT_ZERO for vector modes
[official-gcc.git] / gcc / tree-vrp.c
blob4e4ebe030570bf5cd48f6cc0ad6efbdde8336868
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "flags.h"
26 #include "tree.h"
27 #include "stor-layout.h"
28 #include "calls.h"
29 #include "predict.h"
30 #include "vec.h"
31 #include "hashtab.h"
32 #include "hash-set.h"
33 #include "machmode.h"
34 #include "hard-reg-set.h"
35 #include "input.h"
36 #include "function.h"
37 #include "dominance.h"
38 #include "cfg.h"
39 #include "cfganal.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-fold.h"
44 #include "tree-eh.h"
45 #include "gimple-expr.h"
46 #include "is-a.h"
47 #include "gimple.h"
48 #include "gimple-iterator.h"
49 #include "gimple-walk.h"
50 #include "gimple-ssa.h"
51 #include "tree-cfg.h"
52 #include "tree-phinodes.h"
53 #include "ssa-iterators.h"
54 #include "stringpool.h"
55 #include "tree-ssanames.h"
56 #include "tree-ssa-loop-manip.h"
57 #include "tree-ssa-loop-niter.h"
58 #include "tree-ssa-loop.h"
59 #include "tree-into-ssa.h"
60 #include "tree-ssa.h"
61 #include "tree-pass.h"
62 #include "tree-dump.h"
63 #include "gimple-pretty-print.h"
64 #include "diagnostic-core.h"
65 #include "intl.h"
66 #include "cfgloop.h"
67 #include "tree-scalar-evolution.h"
68 #include "tree-ssa-propagate.h"
69 #include "tree-chrec.h"
70 #include "tree-ssa-threadupdate.h"
71 #include "expr.h"
72 #include "insn-codes.h"
73 #include "optabs.h"
74 #include "tree-ssa-threadedge.h"
75 #include "wide-int.h"
79 /* Range of values that can be associated with an SSA_NAME after VRP
80 has executed. */
81 struct value_range_d
83 /* Lattice value represented by this range. */
84 enum value_range_type type;
86 /* Minimum and maximum values represented by this range. These
87 values should be interpreted as follows:
89 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
90 be NULL.
92 - If TYPE == VR_RANGE then MIN holds the minimum value and
93 MAX holds the maximum value of the range [MIN, MAX].
95 - If TYPE == ANTI_RANGE the variable is known to NOT
96 take any values in the range [MIN, MAX]. */
97 tree min;
98 tree max;
100 /* Set of SSA names whose value ranges are equivalent to this one.
101 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
102 bitmap equiv;
105 typedef struct value_range_d value_range_t;
107 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
109 /* Set of SSA names found live during the RPO traversal of the function
110 for still active basic-blocks. */
111 static sbitmap *live;
113 /* Return true if the SSA name NAME is live on the edge E. */
115 static bool
116 live_on_edge (edge e, tree name)
118 return (live[e->dest->index]
119 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
122 /* Local functions. */
123 static int compare_values (tree val1, tree val2);
124 static int compare_values_warnv (tree val1, tree val2, bool *);
125 static void vrp_meet (value_range_t *, value_range_t *);
126 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
127 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
128 tree, tree, bool, bool *,
129 bool *);
131 /* Location information for ASSERT_EXPRs. Each instance of this
132 structure describes an ASSERT_EXPR for an SSA name. Since a single
133 SSA name may have more than one assertion associated with it, these
134 locations are kept in a linked list attached to the corresponding
135 SSA name. */
136 struct assert_locus_d
138 /* Basic block where the assertion would be inserted. */
139 basic_block bb;
141 /* Some assertions need to be inserted on an edge (e.g., assertions
142 generated by COND_EXPRs). In those cases, BB will be NULL. */
143 edge e;
145 /* Pointer to the statement that generated this assertion. */
146 gimple_stmt_iterator si;
148 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
149 enum tree_code comp_code;
151 /* Value being compared against. */
152 tree val;
154 /* Expression to compare. */
155 tree expr;
157 /* Next node in the linked list. */
158 struct assert_locus_d *next;
161 typedef struct assert_locus_d *assert_locus_t;
163 /* If bit I is present, it means that SSA name N_i has a list of
164 assertions that should be inserted in the IL. */
165 static bitmap need_assert_for;
167 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
168 holds a list of ASSERT_LOCUS_T nodes that describe where
169 ASSERT_EXPRs for SSA name N_I should be inserted. */
170 static assert_locus_t *asserts_for;
172 /* Value range array. After propagation, VR_VALUE[I] holds the range
173 of values that SSA name N_I may take. */
174 static unsigned num_vr_values;
175 static value_range_t **vr_value;
176 static bool values_propagated;
178 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
179 number of executable edges we saw the last time we visited the
180 node. */
181 static int *vr_phi_edge_counts;
183 typedef struct {
184 gimple stmt;
185 tree vec;
186 } switch_update;
188 static vec<edge> to_remove_edges;
189 static vec<switch_update> to_update_switch_stmts;
192 /* Return the maximum value for TYPE. */
194 static inline tree
195 vrp_val_max (const_tree type)
197 if (!INTEGRAL_TYPE_P (type))
198 return NULL_TREE;
200 return TYPE_MAX_VALUE (type);
203 /* Return the minimum value for TYPE. */
205 static inline tree
206 vrp_val_min (const_tree type)
208 if (!INTEGRAL_TYPE_P (type))
209 return NULL_TREE;
211 return TYPE_MIN_VALUE (type);
214 /* Return whether VAL is equal to the maximum value of its type. This
215 will be true for a positive overflow infinity. We can't do a
216 simple equality comparison with TYPE_MAX_VALUE because C typedefs
217 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
218 to the integer constant with the same value in the type. */
220 static inline bool
221 vrp_val_is_max (const_tree val)
223 tree type_max = vrp_val_max (TREE_TYPE (val));
224 return (val == type_max
225 || (type_max != NULL_TREE
226 && operand_equal_p (val, type_max, 0)));
229 /* Return whether VAL is equal to the minimum value of its type. This
230 will be true for a negative overflow infinity. */
232 static inline bool
233 vrp_val_is_min (const_tree val)
235 tree type_min = vrp_val_min (TREE_TYPE (val));
236 return (val == type_min
237 || (type_min != NULL_TREE
238 && operand_equal_p (val, type_min, 0)));
242 /* Return whether TYPE should use an overflow infinity distinct from
243 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
244 represent a signed overflow during VRP computations. An infinity
245 is distinct from a half-range, which will go from some number to
246 TYPE_{MIN,MAX}_VALUE. */
248 static inline bool
249 needs_overflow_infinity (const_tree type)
251 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
254 /* Return whether TYPE can support our overflow infinity
255 representation: we use the TREE_OVERFLOW flag, which only exists
256 for constants. If TYPE doesn't support this, we don't optimize
257 cases which would require signed overflow--we drop them to
258 VARYING. */
260 static inline bool
261 supports_overflow_infinity (const_tree type)
263 tree min = vrp_val_min (type), max = vrp_val_max (type);
264 #ifdef ENABLE_CHECKING
265 gcc_assert (needs_overflow_infinity (type));
266 #endif
267 return (min != NULL_TREE
268 && CONSTANT_CLASS_P (min)
269 && max != NULL_TREE
270 && CONSTANT_CLASS_P (max));
273 /* VAL is the maximum or minimum value of a type. Return a
274 corresponding overflow infinity. */
276 static inline tree
277 make_overflow_infinity (tree val)
279 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
280 val = copy_node (val);
281 TREE_OVERFLOW (val) = 1;
282 return val;
285 /* Return a negative overflow infinity for TYPE. */
287 static inline tree
288 negative_overflow_infinity (tree type)
290 gcc_checking_assert (supports_overflow_infinity (type));
291 return make_overflow_infinity (vrp_val_min (type));
294 /* Return a positive overflow infinity for TYPE. */
296 static inline tree
297 positive_overflow_infinity (tree type)
299 gcc_checking_assert (supports_overflow_infinity (type));
300 return make_overflow_infinity (vrp_val_max (type));
303 /* Return whether VAL is a negative overflow infinity. */
305 static inline bool
306 is_negative_overflow_infinity (const_tree val)
308 return (TREE_OVERFLOW_P (val)
309 && needs_overflow_infinity (TREE_TYPE (val))
310 && vrp_val_is_min (val));
313 /* Return whether VAL is a positive overflow infinity. */
315 static inline bool
316 is_positive_overflow_infinity (const_tree val)
318 return (TREE_OVERFLOW_P (val)
319 && needs_overflow_infinity (TREE_TYPE (val))
320 && vrp_val_is_max (val));
323 /* Return whether VAL is a positive or negative overflow infinity. */
325 static inline bool
326 is_overflow_infinity (const_tree val)
328 return (TREE_OVERFLOW_P (val)
329 && needs_overflow_infinity (TREE_TYPE (val))
330 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
333 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
335 static inline bool
336 stmt_overflow_infinity (gimple stmt)
338 if (is_gimple_assign (stmt)
339 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
340 GIMPLE_SINGLE_RHS)
341 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
342 return false;
345 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
346 the same value with TREE_OVERFLOW clear. This can be used to avoid
347 confusing a regular value with an overflow value. */
349 static inline tree
350 avoid_overflow_infinity (tree val)
352 if (!is_overflow_infinity (val))
353 return val;
355 if (vrp_val_is_max (val))
356 return vrp_val_max (TREE_TYPE (val));
357 else
359 gcc_checking_assert (vrp_val_is_min (val));
360 return vrp_val_min (TREE_TYPE (val));
365 /* Return true if ARG is marked with the nonnull attribute in the
366 current function signature. */
368 static bool
369 nonnull_arg_p (const_tree arg)
371 tree t, attrs, fntype;
372 unsigned HOST_WIDE_INT arg_num;
374 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
376 /* The static chain decl is always non null. */
377 if (arg == cfun->static_chain_decl)
378 return true;
380 fntype = TREE_TYPE (current_function_decl);
381 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
383 attrs = lookup_attribute ("nonnull", attrs);
385 /* If "nonnull" wasn't specified, we know nothing about the argument. */
386 if (attrs == NULL_TREE)
387 return false;
389 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
390 if (TREE_VALUE (attrs) == NULL_TREE)
391 return true;
393 /* Get the position number for ARG in the function signature. */
394 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
396 t = DECL_CHAIN (t), arg_num++)
398 if (t == arg)
399 break;
402 gcc_assert (t == arg);
404 /* Now see if ARG_NUM is mentioned in the nonnull list. */
405 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
407 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
408 return true;
412 return false;
416 /* Set value range VR to VR_UNDEFINED. */
418 static inline void
419 set_value_range_to_undefined (value_range_t *vr)
421 vr->type = VR_UNDEFINED;
422 vr->min = vr->max = NULL_TREE;
423 if (vr->equiv)
424 bitmap_clear (vr->equiv);
428 /* Set value range VR to VR_VARYING. */
430 static inline void
431 set_value_range_to_varying (value_range_t *vr)
433 vr->type = VR_VARYING;
434 vr->min = vr->max = NULL_TREE;
435 if (vr->equiv)
436 bitmap_clear (vr->equiv);
440 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
442 static void
443 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
444 tree max, bitmap equiv)
446 #if defined ENABLE_CHECKING
447 /* Check the validity of the range. */
448 if (t == VR_RANGE || t == VR_ANTI_RANGE)
450 int cmp;
452 gcc_assert (min && max);
454 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
455 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
457 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
458 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
460 cmp = compare_values (min, max);
461 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
463 if (needs_overflow_infinity (TREE_TYPE (min)))
464 gcc_assert (!is_overflow_infinity (min)
465 || !is_overflow_infinity (max));
468 if (t == VR_UNDEFINED || t == VR_VARYING)
469 gcc_assert (min == NULL_TREE && max == NULL_TREE);
471 if (t == VR_UNDEFINED || t == VR_VARYING)
472 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
473 #endif
475 vr->type = t;
476 vr->min = min;
477 vr->max = max;
479 /* Since updating the equivalence set involves deep copying the
480 bitmaps, only do it if absolutely necessary. */
481 if (vr->equiv == NULL
482 && equiv != NULL)
483 vr->equiv = BITMAP_ALLOC (NULL);
485 if (equiv != vr->equiv)
487 if (equiv && !bitmap_empty_p (equiv))
488 bitmap_copy (vr->equiv, equiv);
489 else
490 bitmap_clear (vr->equiv);
495 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
496 This means adjusting T, MIN and MAX representing the case of a
497 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
498 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
499 In corner cases where MAX+1 or MIN-1 wraps this will fall back
500 to varying.
501 This routine exists to ease canonicalization in the case where we
502 extract ranges from var + CST op limit. */
504 static void
505 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
506 tree min, tree max, bitmap equiv)
508 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
509 if (t == VR_UNDEFINED)
511 set_value_range_to_undefined (vr);
512 return;
514 else if (t == VR_VARYING)
516 set_value_range_to_varying (vr);
517 return;
520 /* Nothing to canonicalize for symbolic ranges. */
521 if (TREE_CODE (min) != INTEGER_CST
522 || TREE_CODE (max) != INTEGER_CST)
524 set_value_range (vr, t, min, max, equiv);
525 return;
528 /* Wrong order for min and max, to swap them and the VR type we need
529 to adjust them. */
530 if (tree_int_cst_lt (max, min))
532 tree one, tmp;
534 /* For one bit precision if max < min, then the swapped
535 range covers all values, so for VR_RANGE it is varying and
536 for VR_ANTI_RANGE empty range, so drop to varying as well. */
537 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
539 set_value_range_to_varying (vr);
540 return;
543 one = build_int_cst (TREE_TYPE (min), 1);
544 tmp = int_const_binop (PLUS_EXPR, max, one);
545 max = int_const_binop (MINUS_EXPR, min, one);
546 min = tmp;
548 /* There's one corner case, if we had [C+1, C] before we now have
549 that again. But this represents an empty value range, so drop
550 to varying in this case. */
551 if (tree_int_cst_lt (max, min))
553 set_value_range_to_varying (vr);
554 return;
557 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
560 /* Anti-ranges that can be represented as ranges should be so. */
561 if (t == VR_ANTI_RANGE)
563 bool is_min = vrp_val_is_min (min);
564 bool is_max = vrp_val_is_max (max);
566 if (is_min && is_max)
568 /* We cannot deal with empty ranges, drop to varying.
569 ??? This could be VR_UNDEFINED instead. */
570 set_value_range_to_varying (vr);
571 return;
573 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
574 && (is_min || is_max))
576 /* Non-empty boolean ranges can always be represented
577 as a singleton range. */
578 if (is_min)
579 min = max = vrp_val_max (TREE_TYPE (min));
580 else
581 min = max = vrp_val_min (TREE_TYPE (min));
582 t = VR_RANGE;
584 else if (is_min
585 /* As a special exception preserve non-null ranges. */
586 && !(TYPE_UNSIGNED (TREE_TYPE (min))
587 && integer_zerop (max)))
589 tree one = build_int_cst (TREE_TYPE (max), 1);
590 min = int_const_binop (PLUS_EXPR, max, one);
591 max = vrp_val_max (TREE_TYPE (max));
592 t = VR_RANGE;
594 else if (is_max)
596 tree one = build_int_cst (TREE_TYPE (min), 1);
597 max = int_const_binop (MINUS_EXPR, min, one);
598 min = vrp_val_min (TREE_TYPE (min));
599 t = VR_RANGE;
603 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
604 if (needs_overflow_infinity (TREE_TYPE (min))
605 && is_overflow_infinity (min)
606 && is_overflow_infinity (max))
608 set_value_range_to_varying (vr);
609 return;
612 set_value_range (vr, t, min, max, equiv);
615 /* Copy value range FROM into value range TO. */
617 static inline void
618 copy_value_range (value_range_t *to, value_range_t *from)
620 set_value_range (to, from->type, from->min, from->max, from->equiv);
623 /* Set value range VR to a single value. This function is only called
624 with values we get from statements, and exists to clear the
625 TREE_OVERFLOW flag so that we don't think we have an overflow
626 infinity when we shouldn't. */
628 static inline void
629 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
631 gcc_assert (is_gimple_min_invariant (val));
632 if (TREE_OVERFLOW_P (val))
633 val = drop_tree_overflow (val);
634 set_value_range (vr, VR_RANGE, val, val, equiv);
637 /* Set value range VR to a non-negative range of type TYPE.
638 OVERFLOW_INFINITY indicates whether to use an overflow infinity
639 rather than TYPE_MAX_VALUE; this should be true if we determine
640 that the range is nonnegative based on the assumption that signed
641 overflow does not occur. */
643 static inline void
644 set_value_range_to_nonnegative (value_range_t *vr, tree type,
645 bool overflow_infinity)
647 tree zero;
649 if (overflow_infinity && !supports_overflow_infinity (type))
651 set_value_range_to_varying (vr);
652 return;
655 zero = build_int_cst (type, 0);
656 set_value_range (vr, VR_RANGE, zero,
657 (overflow_infinity
658 ? positive_overflow_infinity (type)
659 : TYPE_MAX_VALUE (type)),
660 vr->equiv);
663 /* Set value range VR to a non-NULL range of type TYPE. */
665 static inline void
666 set_value_range_to_nonnull (value_range_t *vr, tree type)
668 tree zero = build_int_cst (type, 0);
669 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
673 /* Set value range VR to a NULL range of type TYPE. */
675 static inline void
676 set_value_range_to_null (value_range_t *vr, tree type)
678 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
682 /* Set value range VR to a range of a truthvalue of type TYPE. */
684 static inline void
685 set_value_range_to_truthvalue (value_range_t *vr, tree type)
687 if (TYPE_PRECISION (type) == 1)
688 set_value_range_to_varying (vr);
689 else
690 set_value_range (vr, VR_RANGE,
691 build_int_cst (type, 0), build_int_cst (type, 1),
692 vr->equiv);
696 /* If abs (min) < abs (max), set VR to [-max, max], if
697 abs (min) >= abs (max), set VR to [-min, min]. */
699 static void
700 abs_extent_range (value_range_t *vr, tree min, tree max)
702 int cmp;
704 gcc_assert (TREE_CODE (min) == INTEGER_CST);
705 gcc_assert (TREE_CODE (max) == INTEGER_CST);
706 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
707 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
708 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
709 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
710 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
712 set_value_range_to_varying (vr);
713 return;
715 cmp = compare_values (min, max);
716 if (cmp == -1)
717 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
718 else if (cmp == 0 || cmp == 1)
720 max = min;
721 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
723 else
725 set_value_range_to_varying (vr);
726 return;
728 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
732 /* Return value range information for VAR.
734 If we have no values ranges recorded (ie, VRP is not running), then
735 return NULL. Otherwise create an empty range if none existed for VAR. */
737 static value_range_t *
738 get_value_range (const_tree var)
740 static const struct value_range_d vr_const_varying
741 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
742 value_range_t *vr;
743 tree sym;
744 unsigned ver = SSA_NAME_VERSION (var);
746 /* If we have no recorded ranges, then return NULL. */
747 if (! vr_value)
748 return NULL;
750 /* If we query the range for a new SSA name return an unmodifiable VARYING.
751 We should get here at most from the substitute-and-fold stage which
752 will never try to change values. */
753 if (ver >= num_vr_values)
754 return CONST_CAST (value_range_t *, &vr_const_varying);
756 vr = vr_value[ver];
757 if (vr)
758 return vr;
760 /* After propagation finished do not allocate new value-ranges. */
761 if (values_propagated)
762 return CONST_CAST (value_range_t *, &vr_const_varying);
764 /* Create a default value range. */
765 vr_value[ver] = vr = XCNEW (value_range_t);
767 /* Defer allocating the equivalence set. */
768 vr->equiv = NULL;
770 /* If VAR is a default definition of a parameter, the variable can
771 take any value in VAR's type. */
772 if (SSA_NAME_IS_DEFAULT_DEF (var))
774 sym = SSA_NAME_VAR (var);
775 if (TREE_CODE (sym) == PARM_DECL)
777 /* Try to use the "nonnull" attribute to create ~[0, 0]
778 anti-ranges for pointers. Note that this is only valid with
779 default definitions of PARM_DECLs. */
780 if (POINTER_TYPE_P (TREE_TYPE (sym))
781 && nonnull_arg_p (sym))
782 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
783 else
784 set_value_range_to_varying (vr);
786 else if (TREE_CODE (sym) == RESULT_DECL
787 && DECL_BY_REFERENCE (sym))
788 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
791 return vr;
794 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
796 static inline bool
797 vrp_operand_equal_p (const_tree val1, const_tree val2)
799 if (val1 == val2)
800 return true;
801 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
802 return false;
803 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
806 /* Return true, if the bitmaps B1 and B2 are equal. */
808 static inline bool
809 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
811 return (b1 == b2
812 || ((!b1 || bitmap_empty_p (b1))
813 && (!b2 || bitmap_empty_p (b2)))
814 || (b1 && b2
815 && bitmap_equal_p (b1, b2)));
818 /* Update the value range and equivalence set for variable VAR to
819 NEW_VR. Return true if NEW_VR is different from VAR's previous
820 value.
822 NOTE: This function assumes that NEW_VR is a temporary value range
823 object created for the sole purpose of updating VAR's range. The
824 storage used by the equivalence set from NEW_VR will be freed by
825 this function. Do not call update_value_range when NEW_VR
826 is the range object associated with another SSA name. */
828 static inline bool
829 update_value_range (const_tree var, value_range_t *new_vr)
831 value_range_t *old_vr;
832 bool is_new;
834 /* Update the value range, if necessary. */
835 old_vr = get_value_range (var);
836 is_new = old_vr->type != new_vr->type
837 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
838 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
839 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
841 if (is_new)
843 /* Do not allow transitions up the lattice. The following
844 is slightly more awkward than just new_vr->type < old_vr->type
845 because VR_RANGE and VR_ANTI_RANGE need to be considered
846 the same. We may not have is_new when transitioning to
847 UNDEFINED or from VARYING. */
848 if (new_vr->type == VR_UNDEFINED
849 || old_vr->type == VR_VARYING)
850 set_value_range_to_varying (old_vr);
851 else
852 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
853 new_vr->equiv);
856 BITMAP_FREE (new_vr->equiv);
858 return is_new;
862 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
863 point where equivalence processing can be turned on/off. */
865 static void
866 add_equivalence (bitmap *equiv, const_tree var)
868 unsigned ver = SSA_NAME_VERSION (var);
869 value_range_t *vr = vr_value[ver];
871 if (*equiv == NULL)
872 *equiv = BITMAP_ALLOC (NULL);
873 bitmap_set_bit (*equiv, ver);
874 if (vr && vr->equiv)
875 bitmap_ior_into (*equiv, vr->equiv);
879 /* Return true if VR is ~[0, 0]. */
881 static inline bool
882 range_is_nonnull (value_range_t *vr)
884 return vr->type == VR_ANTI_RANGE
885 && integer_zerop (vr->min)
886 && integer_zerop (vr->max);
890 /* Return true if VR is [0, 0]. */
892 static inline bool
893 range_is_null (value_range_t *vr)
895 return vr->type == VR_RANGE
896 && integer_zerop (vr->min)
897 && integer_zerop (vr->max);
900 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
901 a singleton. */
903 static inline bool
904 range_int_cst_p (value_range_t *vr)
906 return (vr->type == VR_RANGE
907 && TREE_CODE (vr->max) == INTEGER_CST
908 && TREE_CODE (vr->min) == INTEGER_CST);
911 /* Return true if VR is a INTEGER_CST singleton. */
913 static inline bool
914 range_int_cst_singleton_p (value_range_t *vr)
916 return (range_int_cst_p (vr)
917 && !is_overflow_infinity (vr->min)
918 && !is_overflow_infinity (vr->max)
919 && tree_int_cst_equal (vr->min, vr->max));
922 /* Return true if value range VR involves at least one symbol. */
924 static inline bool
925 symbolic_range_p (value_range_t *vr)
927 return (!is_gimple_min_invariant (vr->min)
928 || !is_gimple_min_invariant (vr->max));
931 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
932 otherwise. We only handle additive operations and set NEG to true if the
933 symbol is negated and INV to the invariant part, if any. */
935 static tree
936 get_single_symbol (tree t, bool *neg, tree *inv)
938 bool neg_;
939 tree inv_;
941 if (TREE_CODE (t) == PLUS_EXPR
942 || TREE_CODE (t) == POINTER_PLUS_EXPR
943 || TREE_CODE (t) == MINUS_EXPR)
945 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
947 neg_ = (TREE_CODE (t) == MINUS_EXPR);
948 inv_ = TREE_OPERAND (t, 0);
949 t = TREE_OPERAND (t, 1);
951 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
953 neg_ = false;
954 inv_ = TREE_OPERAND (t, 1);
955 t = TREE_OPERAND (t, 0);
957 else
958 return NULL_TREE;
960 else
962 neg_ = false;
963 inv_ = NULL_TREE;
966 if (TREE_CODE (t) == NEGATE_EXPR)
968 t = TREE_OPERAND (t, 0);
969 neg_ = !neg_;
972 if (TREE_CODE (t) != SSA_NAME)
973 return NULL_TREE;
975 *neg = neg_;
976 *inv = inv_;
977 return t;
980 /* The reverse operation: build a symbolic expression with TYPE
981 from symbol SYM, negated according to NEG, and invariant INV. */
983 static tree
984 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
986 const bool pointer_p = POINTER_TYPE_P (type);
987 tree t = sym;
989 if (neg)
990 t = build1 (NEGATE_EXPR, type, t);
992 if (integer_zerop (inv))
993 return t;
995 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
998 /* Return true if value range VR involves exactly one symbol SYM. */
1000 static bool
1001 symbolic_range_based_on_p (value_range_t *vr, const_tree sym)
1003 bool neg, min_has_symbol, max_has_symbol;
1004 tree inv;
1006 if (is_gimple_min_invariant (vr->min))
1007 min_has_symbol = false;
1008 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
1009 min_has_symbol = true;
1010 else
1011 return false;
1013 if (is_gimple_min_invariant (vr->max))
1014 max_has_symbol = false;
1015 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
1016 max_has_symbol = true;
1017 else
1018 return false;
1020 return (min_has_symbol || max_has_symbol);
1023 /* Return true if value range VR uses an overflow infinity. */
1025 static inline bool
1026 overflow_infinity_range_p (value_range_t *vr)
1028 return (vr->type == VR_RANGE
1029 && (is_overflow_infinity (vr->min)
1030 || is_overflow_infinity (vr->max)));
1033 /* Return false if we can not make a valid comparison based on VR;
1034 this will be the case if it uses an overflow infinity and overflow
1035 is not undefined (i.e., -fno-strict-overflow is in effect).
1036 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1037 uses an overflow infinity. */
1039 static bool
1040 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
1042 gcc_assert (vr->type == VR_RANGE);
1043 if (is_overflow_infinity (vr->min))
1045 *strict_overflow_p = true;
1046 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1047 return false;
1049 if (is_overflow_infinity (vr->max))
1051 *strict_overflow_p = true;
1052 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1053 return false;
1055 return true;
1059 /* Return true if the result of assignment STMT is know to be non-negative.
1060 If the return value is based on the assumption that signed overflow is
1061 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1062 *STRICT_OVERFLOW_P.*/
1064 static bool
1065 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1067 enum tree_code code = gimple_assign_rhs_code (stmt);
1068 switch (get_gimple_rhs_class (code))
1070 case GIMPLE_UNARY_RHS:
1071 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1072 gimple_expr_type (stmt),
1073 gimple_assign_rhs1 (stmt),
1074 strict_overflow_p);
1075 case GIMPLE_BINARY_RHS:
1076 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1077 gimple_expr_type (stmt),
1078 gimple_assign_rhs1 (stmt),
1079 gimple_assign_rhs2 (stmt),
1080 strict_overflow_p);
1081 case GIMPLE_TERNARY_RHS:
1082 return false;
1083 case GIMPLE_SINGLE_RHS:
1084 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
1085 strict_overflow_p);
1086 case GIMPLE_INVALID_RHS:
1087 gcc_unreachable ();
1088 default:
1089 gcc_unreachable ();
1093 /* Return true if return value of call STMT is know to be non-negative.
1094 If the return value is based on the assumption that signed overflow is
1095 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1096 *STRICT_OVERFLOW_P.*/
1098 static bool
1099 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1101 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1102 gimple_call_arg (stmt, 0) : NULL_TREE;
1103 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1104 gimple_call_arg (stmt, 1) : NULL_TREE;
1106 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1107 gimple_call_fndecl (stmt),
1108 arg0,
1109 arg1,
1110 strict_overflow_p);
1113 /* Return true if STMT is know to to compute a non-negative value.
1114 If the return value is based on the assumption that signed overflow is
1115 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1116 *STRICT_OVERFLOW_P.*/
1118 static bool
1119 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1121 switch (gimple_code (stmt))
1123 case GIMPLE_ASSIGN:
1124 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1125 case GIMPLE_CALL:
1126 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1127 default:
1128 gcc_unreachable ();
1132 /* Return true if the result of assignment STMT is know to be non-zero.
1133 If the return value is based on the assumption that signed overflow is
1134 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1135 *STRICT_OVERFLOW_P.*/
1137 static bool
1138 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1140 enum tree_code code = gimple_assign_rhs_code (stmt);
1141 switch (get_gimple_rhs_class (code))
1143 case GIMPLE_UNARY_RHS:
1144 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1145 gimple_expr_type (stmt),
1146 gimple_assign_rhs1 (stmt),
1147 strict_overflow_p);
1148 case GIMPLE_BINARY_RHS:
1149 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1150 gimple_expr_type (stmt),
1151 gimple_assign_rhs1 (stmt),
1152 gimple_assign_rhs2 (stmt),
1153 strict_overflow_p);
1154 case GIMPLE_TERNARY_RHS:
1155 return false;
1156 case GIMPLE_SINGLE_RHS:
1157 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1158 strict_overflow_p);
1159 case GIMPLE_INVALID_RHS:
1160 gcc_unreachable ();
1161 default:
1162 gcc_unreachable ();
1166 /* Return true if STMT is known to compute a non-zero value.
1167 If the return value is based on the assumption that signed overflow is
1168 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1169 *STRICT_OVERFLOW_P.*/
1171 static bool
1172 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1174 switch (gimple_code (stmt))
1176 case GIMPLE_ASSIGN:
1177 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1178 case GIMPLE_CALL:
1180 tree fndecl = gimple_call_fndecl (stmt);
1181 if (!fndecl) return false;
1182 if (flag_delete_null_pointer_checks && !flag_check_new
1183 && DECL_IS_OPERATOR_NEW (fndecl)
1184 && !TREE_NOTHROW (fndecl))
1185 return true;
1186 if (flag_delete_null_pointer_checks &&
1187 lookup_attribute ("returns_nonnull",
1188 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1189 return true;
1190 return gimple_alloca_call_p (stmt);
1192 default:
1193 gcc_unreachable ();
1197 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1198 obtained so far. */
1200 static bool
1201 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1203 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1204 return true;
1206 /* If we have an expression of the form &X->a, then the expression
1207 is nonnull if X is nonnull. */
1208 if (is_gimple_assign (stmt)
1209 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1211 tree expr = gimple_assign_rhs1 (stmt);
1212 tree base = get_base_address (TREE_OPERAND (expr, 0));
1214 if (base != NULL_TREE
1215 && TREE_CODE (base) == MEM_REF
1216 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1218 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1219 if (range_is_nonnull (vr))
1220 return true;
1224 return false;
1227 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1228 a gimple invariant, or SSA_NAME +- CST. */
1230 static bool
1231 valid_value_p (tree expr)
1233 if (TREE_CODE (expr) == SSA_NAME)
1234 return true;
1236 if (TREE_CODE (expr) == PLUS_EXPR
1237 || TREE_CODE (expr) == MINUS_EXPR)
1238 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1239 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1241 return is_gimple_min_invariant (expr);
1244 /* Return
1245 1 if VAL < VAL2
1246 0 if !(VAL < VAL2)
1247 -2 if those are incomparable. */
1248 static inline int
1249 operand_less_p (tree val, tree val2)
1251 /* LT is folded faster than GE and others. Inline the common case. */
1252 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1253 return tree_int_cst_lt (val, val2);
1254 else
1256 tree tcmp;
1258 fold_defer_overflow_warnings ();
1260 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1262 fold_undefer_and_ignore_overflow_warnings ();
1264 if (!tcmp
1265 || TREE_CODE (tcmp) != INTEGER_CST)
1266 return -2;
1268 if (!integer_zerop (tcmp))
1269 return 1;
1272 /* val >= val2, not considering overflow infinity. */
1273 if (is_negative_overflow_infinity (val))
1274 return is_negative_overflow_infinity (val2) ? 0 : 1;
1275 else if (is_positive_overflow_infinity (val2))
1276 return is_positive_overflow_infinity (val) ? 0 : 1;
1278 return 0;
1281 /* Compare two values VAL1 and VAL2. Return
1283 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1284 -1 if VAL1 < VAL2,
1285 0 if VAL1 == VAL2,
1286 +1 if VAL1 > VAL2, and
1287 +2 if VAL1 != VAL2
1289 This is similar to tree_int_cst_compare but supports pointer values
1290 and values that cannot be compared at compile time.
1292 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1293 true if the return value is only valid if we assume that signed
1294 overflow is undefined. */
1296 static int
1297 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1299 if (val1 == val2)
1300 return 0;
1302 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1303 both integers. */
1304 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1305 == POINTER_TYPE_P (TREE_TYPE (val2)));
1307 /* Convert the two values into the same type. This is needed because
1308 sizetype causes sign extension even for unsigned types. */
1309 val2 = fold_convert (TREE_TYPE (val1), val2);
1310 STRIP_USELESS_TYPE_CONVERSION (val2);
1312 if ((TREE_CODE (val1) == SSA_NAME
1313 || (TREE_CODE (val1) == NEGATE_EXPR
1314 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1315 || TREE_CODE (val1) == PLUS_EXPR
1316 || TREE_CODE (val1) == MINUS_EXPR)
1317 && (TREE_CODE (val2) == SSA_NAME
1318 || (TREE_CODE (val2) == NEGATE_EXPR
1319 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1320 || TREE_CODE (val2) == PLUS_EXPR
1321 || TREE_CODE (val2) == MINUS_EXPR))
1323 tree n1, c1, n2, c2;
1324 enum tree_code code1, code2;
1326 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1327 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1328 same name, return -2. */
1329 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1331 code1 = SSA_NAME;
1332 n1 = val1;
1333 c1 = NULL_TREE;
1335 else
1337 code1 = TREE_CODE (val1);
1338 n1 = TREE_OPERAND (val1, 0);
1339 c1 = TREE_OPERAND (val1, 1);
1340 if (tree_int_cst_sgn (c1) == -1)
1342 if (is_negative_overflow_infinity (c1))
1343 return -2;
1344 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1345 if (!c1)
1346 return -2;
1347 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1351 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1353 code2 = SSA_NAME;
1354 n2 = val2;
1355 c2 = NULL_TREE;
1357 else
1359 code2 = TREE_CODE (val2);
1360 n2 = TREE_OPERAND (val2, 0);
1361 c2 = TREE_OPERAND (val2, 1);
1362 if (tree_int_cst_sgn (c2) == -1)
1364 if (is_negative_overflow_infinity (c2))
1365 return -2;
1366 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1367 if (!c2)
1368 return -2;
1369 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1373 /* Both values must use the same name. */
1374 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1376 n1 = TREE_OPERAND (n1, 0);
1377 n2 = TREE_OPERAND (n2, 0);
1379 if (n1 != n2)
1380 return -2;
1382 if (code1 == SSA_NAME && code2 == SSA_NAME)
1383 /* NAME == NAME */
1384 return 0;
1386 /* If overflow is defined we cannot simplify more. */
1387 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1388 return -2;
1390 if (strict_overflow_p != NULL
1391 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1392 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1393 *strict_overflow_p = true;
1395 if (code1 == SSA_NAME)
1397 if (code2 == PLUS_EXPR)
1398 /* NAME < NAME + CST */
1399 return -1;
1400 else if (code2 == MINUS_EXPR)
1401 /* NAME > NAME - CST */
1402 return 1;
1404 else if (code1 == PLUS_EXPR)
1406 if (code2 == SSA_NAME)
1407 /* NAME + CST > NAME */
1408 return 1;
1409 else if (code2 == PLUS_EXPR)
1410 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1411 return compare_values_warnv (c1, c2, strict_overflow_p);
1412 else if (code2 == MINUS_EXPR)
1413 /* NAME + CST1 > NAME - CST2 */
1414 return 1;
1416 else if (code1 == MINUS_EXPR)
1418 if (code2 == SSA_NAME)
1419 /* NAME - CST < NAME */
1420 return -1;
1421 else if (code2 == PLUS_EXPR)
1422 /* NAME - CST1 < NAME + CST2 */
1423 return -1;
1424 else if (code2 == MINUS_EXPR)
1425 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1426 C1 and C2 are swapped in the call to compare_values. */
1427 return compare_values_warnv (c2, c1, strict_overflow_p);
1430 gcc_unreachable ();
1433 /* We cannot compare non-constants. */
1434 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1435 return -2;
1437 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1439 /* We cannot compare overflowed values, except for overflow
1440 infinities. */
1441 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1443 if (strict_overflow_p != NULL)
1444 *strict_overflow_p = true;
1445 if (is_negative_overflow_infinity (val1))
1446 return is_negative_overflow_infinity (val2) ? 0 : -1;
1447 else if (is_negative_overflow_infinity (val2))
1448 return 1;
1449 else if (is_positive_overflow_infinity (val1))
1450 return is_positive_overflow_infinity (val2) ? 0 : 1;
1451 else if (is_positive_overflow_infinity (val2))
1452 return -1;
1453 return -2;
1456 return tree_int_cst_compare (val1, val2);
1458 else
1460 tree t;
1462 /* First see if VAL1 and VAL2 are not the same. */
1463 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1464 return 0;
1466 /* If VAL1 is a lower address than VAL2, return -1. */
1467 if (operand_less_p (val1, val2) == 1)
1468 return -1;
1470 /* If VAL1 is a higher address than VAL2, return +1. */
1471 if (operand_less_p (val2, val1) == 1)
1472 return 1;
1474 /* If VAL1 is different than VAL2, return +2.
1475 For integer constants we either have already returned -1 or 1
1476 or they are equivalent. We still might succeed in proving
1477 something about non-trivial operands. */
1478 if (TREE_CODE (val1) != INTEGER_CST
1479 || TREE_CODE (val2) != INTEGER_CST)
1481 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1482 if (t && integer_onep (t))
1483 return 2;
1486 return -2;
1490 /* Compare values like compare_values_warnv, but treat comparisons of
1491 nonconstants which rely on undefined overflow as incomparable. */
1493 static int
1494 compare_values (tree val1, tree val2)
1496 bool sop;
1497 int ret;
1499 sop = false;
1500 ret = compare_values_warnv (val1, val2, &sop);
1501 if (sop
1502 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1503 ret = -2;
1504 return ret;
1508 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1509 0 if VAL is not inside [MIN, MAX],
1510 -2 if we cannot tell either way.
1512 Benchmark compile/20001226-1.c compilation time after changing this
1513 function. */
1515 static inline int
1516 value_inside_range (tree val, tree min, tree max)
1518 int cmp1, cmp2;
1520 cmp1 = operand_less_p (val, min);
1521 if (cmp1 == -2)
1522 return -2;
1523 if (cmp1 == 1)
1524 return 0;
1526 cmp2 = operand_less_p (max, val);
1527 if (cmp2 == -2)
1528 return -2;
1530 return !cmp2;
1534 /* Return true if value ranges VR0 and VR1 have a non-empty
1535 intersection.
1537 Benchmark compile/20001226-1.c compilation time after changing this
1538 function.
1541 static inline bool
1542 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1544 /* The value ranges do not intersect if the maximum of the first range is
1545 less than the minimum of the second range or vice versa.
1546 When those relations are unknown, we can't do any better. */
1547 if (operand_less_p (vr0->max, vr1->min) != 0)
1548 return false;
1549 if (operand_less_p (vr1->max, vr0->min) != 0)
1550 return false;
1551 return true;
1555 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1556 include the value zero, -2 if we cannot tell. */
1558 static inline int
1559 range_includes_zero_p (tree min, tree max)
1561 tree zero = build_int_cst (TREE_TYPE (min), 0);
1562 return value_inside_range (zero, min, max);
1565 /* Return true if *VR is know to only contain nonnegative values. */
1567 static inline bool
1568 value_range_nonnegative_p (value_range_t *vr)
1570 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1571 which would return a useful value should be encoded as a
1572 VR_RANGE. */
1573 if (vr->type == VR_RANGE)
1575 int result = compare_values (vr->min, integer_zero_node);
1576 return (result == 0 || result == 1);
1579 return false;
1582 /* If *VR has a value rante that is a single constant value return that,
1583 otherwise return NULL_TREE. */
1585 static tree
1586 value_range_constant_singleton (value_range_t *vr)
1588 if (vr->type == VR_RANGE
1589 && operand_equal_p (vr->min, vr->max, 0)
1590 && is_gimple_min_invariant (vr->min))
1591 return vr->min;
1593 return NULL_TREE;
1596 /* If OP has a value range with a single constant value return that,
1597 otherwise return NULL_TREE. This returns OP itself if OP is a
1598 constant. */
1600 static tree
1601 op_with_constant_singleton_value_range (tree op)
1603 if (is_gimple_min_invariant (op))
1604 return op;
1606 if (TREE_CODE (op) != SSA_NAME)
1607 return NULL_TREE;
1609 return value_range_constant_singleton (get_value_range (op));
1612 /* Return true if op is in a boolean [0, 1] value-range. */
1614 static bool
1615 op_with_boolean_value_range_p (tree op)
1617 value_range_t *vr;
1619 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1620 return true;
1622 if (integer_zerop (op)
1623 || integer_onep (op))
1624 return true;
1626 if (TREE_CODE (op) != SSA_NAME)
1627 return false;
1629 vr = get_value_range (op);
1630 return (vr->type == VR_RANGE
1631 && integer_zerop (vr->min)
1632 && integer_onep (vr->max));
1635 /* Extract value range information from an ASSERT_EXPR EXPR and store
1636 it in *VR_P. */
1638 static void
1639 extract_range_from_assert (value_range_t *vr_p, tree expr)
1641 tree var, cond, limit, min, max, type;
1642 value_range_t *limit_vr;
1643 enum tree_code cond_code;
1645 var = ASSERT_EXPR_VAR (expr);
1646 cond = ASSERT_EXPR_COND (expr);
1648 gcc_assert (COMPARISON_CLASS_P (cond));
1650 /* Find VAR in the ASSERT_EXPR conditional. */
1651 if (var == TREE_OPERAND (cond, 0)
1652 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1653 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1655 /* If the predicate is of the form VAR COMP LIMIT, then we just
1656 take LIMIT from the RHS and use the same comparison code. */
1657 cond_code = TREE_CODE (cond);
1658 limit = TREE_OPERAND (cond, 1);
1659 cond = TREE_OPERAND (cond, 0);
1661 else
1663 /* If the predicate is of the form LIMIT COMP VAR, then we need
1664 to flip around the comparison code to create the proper range
1665 for VAR. */
1666 cond_code = swap_tree_comparison (TREE_CODE (cond));
1667 limit = TREE_OPERAND (cond, 0);
1668 cond = TREE_OPERAND (cond, 1);
1671 limit = avoid_overflow_infinity (limit);
1673 type = TREE_TYPE (var);
1674 gcc_assert (limit != var);
1676 /* For pointer arithmetic, we only keep track of pointer equality
1677 and inequality. */
1678 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1680 set_value_range_to_varying (vr_p);
1681 return;
1684 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1685 try to use LIMIT's range to avoid creating symbolic ranges
1686 unnecessarily. */
1687 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1689 /* LIMIT's range is only interesting if it has any useful information. */
1690 if (limit_vr
1691 && (limit_vr->type == VR_UNDEFINED
1692 || limit_vr->type == VR_VARYING
1693 || symbolic_range_p (limit_vr)))
1694 limit_vr = NULL;
1696 /* Initially, the new range has the same set of equivalences of
1697 VAR's range. This will be revised before returning the final
1698 value. Since assertions may be chained via mutually exclusive
1699 predicates, we will need to trim the set of equivalences before
1700 we are done. */
1701 gcc_assert (vr_p->equiv == NULL);
1702 add_equivalence (&vr_p->equiv, var);
1704 /* Extract a new range based on the asserted comparison for VAR and
1705 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1706 will only use it for equality comparisons (EQ_EXPR). For any
1707 other kind of assertion, we cannot derive a range from LIMIT's
1708 anti-range that can be used to describe the new range. For
1709 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1710 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1711 no single range for x_2 that could describe LE_EXPR, so we might
1712 as well build the range [b_4, +INF] for it.
1713 One special case we handle is extracting a range from a
1714 range test encoded as (unsigned)var + CST <= limit. */
1715 if (TREE_CODE (cond) == NOP_EXPR
1716 || TREE_CODE (cond) == PLUS_EXPR)
1718 if (TREE_CODE (cond) == PLUS_EXPR)
1720 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1721 TREE_OPERAND (cond, 1));
1722 max = int_const_binop (PLUS_EXPR, limit, min);
1723 cond = TREE_OPERAND (cond, 0);
1725 else
1727 min = build_int_cst (TREE_TYPE (var), 0);
1728 max = limit;
1731 /* Make sure to not set TREE_OVERFLOW on the final type
1732 conversion. We are willingly interpreting large positive
1733 unsigned values as negative signed values here. */
1734 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1735 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1737 /* We can transform a max, min range to an anti-range or
1738 vice-versa. Use set_and_canonicalize_value_range which does
1739 this for us. */
1740 if (cond_code == LE_EXPR)
1741 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1742 min, max, vr_p->equiv);
1743 else if (cond_code == GT_EXPR)
1744 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1745 min, max, vr_p->equiv);
1746 else
1747 gcc_unreachable ();
1749 else if (cond_code == EQ_EXPR)
1751 enum value_range_type range_type;
1753 if (limit_vr)
1755 range_type = limit_vr->type;
1756 min = limit_vr->min;
1757 max = limit_vr->max;
1759 else
1761 range_type = VR_RANGE;
1762 min = limit;
1763 max = limit;
1766 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1768 /* When asserting the equality VAR == LIMIT and LIMIT is another
1769 SSA name, the new range will also inherit the equivalence set
1770 from LIMIT. */
1771 if (TREE_CODE (limit) == SSA_NAME)
1772 add_equivalence (&vr_p->equiv, limit);
1774 else if (cond_code == NE_EXPR)
1776 /* As described above, when LIMIT's range is an anti-range and
1777 this assertion is an inequality (NE_EXPR), then we cannot
1778 derive anything from the anti-range. For instance, if
1779 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1780 not imply that VAR's range is [0, 0]. So, in the case of
1781 anti-ranges, we just assert the inequality using LIMIT and
1782 not its anti-range.
1784 If LIMIT_VR is a range, we can only use it to build a new
1785 anti-range if LIMIT_VR is a single-valued range. For
1786 instance, if LIMIT_VR is [0, 1], the predicate
1787 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1788 Rather, it means that for value 0 VAR should be ~[0, 0]
1789 and for value 1, VAR should be ~[1, 1]. We cannot
1790 represent these ranges.
1792 The only situation in which we can build a valid
1793 anti-range is when LIMIT_VR is a single-valued range
1794 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1795 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1796 if (limit_vr
1797 && limit_vr->type == VR_RANGE
1798 && compare_values (limit_vr->min, limit_vr->max) == 0)
1800 min = limit_vr->min;
1801 max = limit_vr->max;
1803 else
1805 /* In any other case, we cannot use LIMIT's range to build a
1806 valid anti-range. */
1807 min = max = limit;
1810 /* If MIN and MAX cover the whole range for their type, then
1811 just use the original LIMIT. */
1812 if (INTEGRAL_TYPE_P (type)
1813 && vrp_val_is_min (min)
1814 && vrp_val_is_max (max))
1815 min = max = limit;
1817 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1818 min, max, vr_p->equiv);
1820 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1822 min = TYPE_MIN_VALUE (type);
1824 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1825 max = limit;
1826 else
1828 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1829 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1830 LT_EXPR. */
1831 max = limit_vr->max;
1834 /* If the maximum value forces us to be out of bounds, simply punt.
1835 It would be pointless to try and do anything more since this
1836 all should be optimized away above us. */
1837 if ((cond_code == LT_EXPR
1838 && compare_values (max, min) == 0)
1839 || is_overflow_infinity (max))
1840 set_value_range_to_varying (vr_p);
1841 else
1843 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1844 if (cond_code == LT_EXPR)
1846 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1847 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1848 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1849 build_int_cst (TREE_TYPE (max), -1));
1850 else
1851 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1852 build_int_cst (TREE_TYPE (max), 1));
1853 if (EXPR_P (max))
1854 TREE_NO_WARNING (max) = 1;
1857 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1860 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1862 max = TYPE_MAX_VALUE (type);
1864 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1865 min = limit;
1866 else
1868 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1869 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1870 GT_EXPR. */
1871 min = limit_vr->min;
1874 /* If the minimum value forces us to be out of bounds, simply punt.
1875 It would be pointless to try and do anything more since this
1876 all should be optimized away above us. */
1877 if ((cond_code == GT_EXPR
1878 && compare_values (min, max) == 0)
1879 || is_overflow_infinity (min))
1880 set_value_range_to_varying (vr_p);
1881 else
1883 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1884 if (cond_code == GT_EXPR)
1886 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1887 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1888 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1889 build_int_cst (TREE_TYPE (min), -1));
1890 else
1891 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1892 build_int_cst (TREE_TYPE (min), 1));
1893 if (EXPR_P (min))
1894 TREE_NO_WARNING (min) = 1;
1897 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1900 else
1901 gcc_unreachable ();
1903 /* Finally intersect the new range with what we already know about var. */
1904 vrp_intersect_ranges (vr_p, get_value_range (var));
1908 /* Extract range information from SSA name VAR and store it in VR. If
1909 VAR has an interesting range, use it. Otherwise, create the
1910 range [VAR, VAR] and return it. This is useful in situations where
1911 we may have conditionals testing values of VARYING names. For
1912 instance,
1914 x_3 = y_5;
1915 if (x_3 > y_5)
1918 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1919 always false. */
1921 static void
1922 extract_range_from_ssa_name (value_range_t *vr, tree var)
1924 value_range_t *var_vr = get_value_range (var);
1926 if (var_vr->type != VR_VARYING)
1927 copy_value_range (vr, var_vr);
1928 else
1929 set_value_range (vr, VR_RANGE, var, var, NULL);
1931 add_equivalence (&vr->equiv, var);
1935 /* Wrapper around int_const_binop. If the operation overflows and we
1936 are not using wrapping arithmetic, then adjust the result to be
1937 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1938 NULL_TREE if we need to use an overflow infinity representation but
1939 the type does not support it. */
1941 static tree
1942 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1944 tree res;
1946 res = int_const_binop (code, val1, val2);
1948 /* If we are using unsigned arithmetic, operate symbolically
1949 on -INF and +INF as int_const_binop only handles signed overflow. */
1950 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1952 int checkz = compare_values (res, val1);
1953 bool overflow = false;
1955 /* Ensure that res = val1 [+*] val2 >= val1
1956 or that res = val1 - val2 <= val1. */
1957 if ((code == PLUS_EXPR
1958 && !(checkz == 1 || checkz == 0))
1959 || (code == MINUS_EXPR
1960 && !(checkz == 0 || checkz == -1)))
1962 overflow = true;
1964 /* Checking for multiplication overflow is done by dividing the
1965 output of the multiplication by the first input of the
1966 multiplication. If the result of that division operation is
1967 not equal to the second input of the multiplication, then the
1968 multiplication overflowed. */
1969 else if (code == MULT_EXPR && !integer_zerop (val1))
1971 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1972 res,
1973 val1);
1974 int check = compare_values (tmp, val2);
1976 if (check != 0)
1977 overflow = true;
1980 if (overflow)
1982 res = copy_node (res);
1983 TREE_OVERFLOW (res) = 1;
1987 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1988 /* If the singed operation wraps then int_const_binop has done
1989 everything we want. */
1991 /* Signed division of -1/0 overflows and by the time it gets here
1992 returns NULL_TREE. */
1993 else if (!res)
1994 return NULL_TREE;
1995 else if ((TREE_OVERFLOW (res)
1996 && !TREE_OVERFLOW (val1)
1997 && !TREE_OVERFLOW (val2))
1998 || is_overflow_infinity (val1)
1999 || is_overflow_infinity (val2))
2001 /* If the operation overflowed but neither VAL1 nor VAL2 are
2002 overflown, return -INF or +INF depending on the operation
2003 and the combination of signs of the operands. */
2004 int sgn1 = tree_int_cst_sgn (val1);
2005 int sgn2 = tree_int_cst_sgn (val2);
2007 if (needs_overflow_infinity (TREE_TYPE (res))
2008 && !supports_overflow_infinity (TREE_TYPE (res)))
2009 return NULL_TREE;
2011 /* We have to punt on adding infinities of different signs,
2012 since we can't tell what the sign of the result should be.
2013 Likewise for subtracting infinities of the same sign. */
2014 if (((code == PLUS_EXPR && sgn1 != sgn2)
2015 || (code == MINUS_EXPR && sgn1 == sgn2))
2016 && is_overflow_infinity (val1)
2017 && is_overflow_infinity (val2))
2018 return NULL_TREE;
2020 /* Don't try to handle division or shifting of infinities. */
2021 if ((code == TRUNC_DIV_EXPR
2022 || code == FLOOR_DIV_EXPR
2023 || code == CEIL_DIV_EXPR
2024 || code == EXACT_DIV_EXPR
2025 || code == ROUND_DIV_EXPR
2026 || code == RSHIFT_EXPR)
2027 && (is_overflow_infinity (val1)
2028 || is_overflow_infinity (val2)))
2029 return NULL_TREE;
2031 /* Notice that we only need to handle the restricted set of
2032 operations handled by extract_range_from_binary_expr.
2033 Among them, only multiplication, addition and subtraction
2034 can yield overflow without overflown operands because we
2035 are working with integral types only... except in the
2036 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2037 for division too. */
2039 /* For multiplication, the sign of the overflow is given
2040 by the comparison of the signs of the operands. */
2041 if ((code == MULT_EXPR && sgn1 == sgn2)
2042 /* For addition, the operands must be of the same sign
2043 to yield an overflow. Its sign is therefore that
2044 of one of the operands, for example the first. For
2045 infinite operands X + -INF is negative, not positive. */
2046 || (code == PLUS_EXPR
2047 && (sgn1 >= 0
2048 ? !is_negative_overflow_infinity (val2)
2049 : is_positive_overflow_infinity (val2)))
2050 /* For subtraction, non-infinite operands must be of
2051 different signs to yield an overflow. Its sign is
2052 therefore that of the first operand or the opposite of
2053 that of the second operand. A first operand of 0 counts
2054 as positive here, for the corner case 0 - (-INF), which
2055 overflows, but must yield +INF. For infinite operands 0
2056 - INF is negative, not positive. */
2057 || (code == MINUS_EXPR
2058 && (sgn1 >= 0
2059 ? !is_positive_overflow_infinity (val2)
2060 : is_negative_overflow_infinity (val2)))
2061 /* We only get in here with positive shift count, so the
2062 overflow direction is the same as the sign of val1.
2063 Actually rshift does not overflow at all, but we only
2064 handle the case of shifting overflowed -INF and +INF. */
2065 || (code == RSHIFT_EXPR
2066 && sgn1 >= 0)
2067 /* For division, the only case is -INF / -1 = +INF. */
2068 || code == TRUNC_DIV_EXPR
2069 || code == FLOOR_DIV_EXPR
2070 || code == CEIL_DIV_EXPR
2071 || code == EXACT_DIV_EXPR
2072 || code == ROUND_DIV_EXPR)
2073 return (needs_overflow_infinity (TREE_TYPE (res))
2074 ? positive_overflow_infinity (TREE_TYPE (res))
2075 : TYPE_MAX_VALUE (TREE_TYPE (res)));
2076 else
2077 return (needs_overflow_infinity (TREE_TYPE (res))
2078 ? negative_overflow_infinity (TREE_TYPE (res))
2079 : TYPE_MIN_VALUE (TREE_TYPE (res)));
2082 return res;
2086 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2087 bitmask if some bit is unset, it means for all numbers in the range
2088 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2089 bitmask if some bit is set, it means for all numbers in the range
2090 the bit is 1, otherwise it might be 0 or 1. */
2092 static bool
2093 zero_nonzero_bits_from_vr (const tree expr_type,
2094 value_range_t *vr,
2095 wide_int *may_be_nonzero,
2096 wide_int *must_be_nonzero)
2098 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2099 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2100 if (!range_int_cst_p (vr)
2101 || is_overflow_infinity (vr->min)
2102 || is_overflow_infinity (vr->max))
2103 return false;
2105 if (range_int_cst_singleton_p (vr))
2107 *may_be_nonzero = vr->min;
2108 *must_be_nonzero = *may_be_nonzero;
2110 else if (tree_int_cst_sgn (vr->min) >= 0
2111 || tree_int_cst_sgn (vr->max) < 0)
2113 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2114 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2115 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2116 if (xor_mask != 0)
2118 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2119 may_be_nonzero->get_precision ());
2120 *may_be_nonzero = *may_be_nonzero | mask;
2121 *must_be_nonzero = must_be_nonzero->and_not (mask);
2125 return true;
2128 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2129 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2130 false otherwise. If *AR can be represented with a single range
2131 *VR1 will be VR_UNDEFINED. */
2133 static bool
2134 ranges_from_anti_range (value_range_t *ar,
2135 value_range_t *vr0, value_range_t *vr1)
2137 tree type = TREE_TYPE (ar->min);
2139 vr0->type = VR_UNDEFINED;
2140 vr1->type = VR_UNDEFINED;
2142 if (ar->type != VR_ANTI_RANGE
2143 || TREE_CODE (ar->min) != INTEGER_CST
2144 || TREE_CODE (ar->max) != INTEGER_CST
2145 || !vrp_val_min (type)
2146 || !vrp_val_max (type))
2147 return false;
2149 if (!vrp_val_is_min (ar->min))
2151 vr0->type = VR_RANGE;
2152 vr0->min = vrp_val_min (type);
2153 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2155 if (!vrp_val_is_max (ar->max))
2157 vr1->type = VR_RANGE;
2158 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2159 vr1->max = vrp_val_max (type);
2161 if (vr0->type == VR_UNDEFINED)
2163 *vr0 = *vr1;
2164 vr1->type = VR_UNDEFINED;
2167 return vr0->type != VR_UNDEFINED;
2170 /* Helper to extract a value-range *VR for a multiplicative operation
2171 *VR0 CODE *VR1. */
2173 static void
2174 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2175 enum tree_code code,
2176 value_range_t *vr0, value_range_t *vr1)
2178 enum value_range_type type;
2179 tree val[4];
2180 size_t i;
2181 tree min, max;
2182 bool sop;
2183 int cmp;
2185 /* Multiplications, divisions and shifts are a bit tricky to handle,
2186 depending on the mix of signs we have in the two ranges, we
2187 need to operate on different values to get the minimum and
2188 maximum values for the new range. One approach is to figure
2189 out all the variations of range combinations and do the
2190 operations.
2192 However, this involves several calls to compare_values and it
2193 is pretty convoluted. It's simpler to do the 4 operations
2194 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2195 MAX1) and then figure the smallest and largest values to form
2196 the new range. */
2197 gcc_assert (code == MULT_EXPR
2198 || code == TRUNC_DIV_EXPR
2199 || code == FLOOR_DIV_EXPR
2200 || code == CEIL_DIV_EXPR
2201 || code == EXACT_DIV_EXPR
2202 || code == ROUND_DIV_EXPR
2203 || code == RSHIFT_EXPR
2204 || code == LSHIFT_EXPR);
2205 gcc_assert ((vr0->type == VR_RANGE
2206 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2207 && vr0->type == vr1->type);
2209 type = vr0->type;
2211 /* Compute the 4 cross operations. */
2212 sop = false;
2213 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2214 if (val[0] == NULL_TREE)
2215 sop = true;
2217 if (vr1->max == vr1->min)
2218 val[1] = NULL_TREE;
2219 else
2221 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2222 if (val[1] == NULL_TREE)
2223 sop = true;
2226 if (vr0->max == vr0->min)
2227 val[2] = NULL_TREE;
2228 else
2230 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2231 if (val[2] == NULL_TREE)
2232 sop = true;
2235 if (vr0->min == vr0->max || vr1->min == vr1->max)
2236 val[3] = NULL_TREE;
2237 else
2239 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2240 if (val[3] == NULL_TREE)
2241 sop = true;
2244 if (sop)
2246 set_value_range_to_varying (vr);
2247 return;
2250 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2251 of VAL[i]. */
2252 min = val[0];
2253 max = val[0];
2254 for (i = 1; i < 4; i++)
2256 if (!is_gimple_min_invariant (min)
2257 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2258 || !is_gimple_min_invariant (max)
2259 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2260 break;
2262 if (val[i])
2264 if (!is_gimple_min_invariant (val[i])
2265 || (TREE_OVERFLOW (val[i])
2266 && !is_overflow_infinity (val[i])))
2268 /* If we found an overflowed value, set MIN and MAX
2269 to it so that we set the resulting range to
2270 VARYING. */
2271 min = max = val[i];
2272 break;
2275 if (compare_values (val[i], min) == -1)
2276 min = val[i];
2278 if (compare_values (val[i], max) == 1)
2279 max = val[i];
2283 /* If either MIN or MAX overflowed, then set the resulting range to
2284 VARYING. But we do accept an overflow infinity
2285 representation. */
2286 if (min == NULL_TREE
2287 || !is_gimple_min_invariant (min)
2288 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2289 || max == NULL_TREE
2290 || !is_gimple_min_invariant (max)
2291 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2293 set_value_range_to_varying (vr);
2294 return;
2297 /* We punt if:
2298 1) [-INF, +INF]
2299 2) [-INF, +-INF(OVF)]
2300 3) [+-INF(OVF), +INF]
2301 4) [+-INF(OVF), +-INF(OVF)]
2302 We learn nothing when we have INF and INF(OVF) on both sides.
2303 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2304 overflow. */
2305 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2306 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2308 set_value_range_to_varying (vr);
2309 return;
2312 cmp = compare_values (min, max);
2313 if (cmp == -2 || cmp == 1)
2315 /* If the new range has its limits swapped around (MIN > MAX),
2316 then the operation caused one of them to wrap around, mark
2317 the new range VARYING. */
2318 set_value_range_to_varying (vr);
2320 else
2321 set_value_range (vr, type, min, max, NULL);
2324 /* Extract range information from a binary operation CODE based on
2325 the ranges of each of its operands *VR0 and *VR1 with resulting
2326 type EXPR_TYPE. The resulting range is stored in *VR. */
2328 static void
2329 extract_range_from_binary_expr_1 (value_range_t *vr,
2330 enum tree_code code, tree expr_type,
2331 value_range_t *vr0_, value_range_t *vr1_)
2333 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2334 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2335 enum value_range_type type;
2336 tree min = NULL_TREE, max = NULL_TREE;
2337 int cmp;
2339 if (!INTEGRAL_TYPE_P (expr_type)
2340 && !POINTER_TYPE_P (expr_type))
2342 set_value_range_to_varying (vr);
2343 return;
2346 /* Not all binary expressions can be applied to ranges in a
2347 meaningful way. Handle only arithmetic operations. */
2348 if (code != PLUS_EXPR
2349 && code != MINUS_EXPR
2350 && code != POINTER_PLUS_EXPR
2351 && code != MULT_EXPR
2352 && code != TRUNC_DIV_EXPR
2353 && code != FLOOR_DIV_EXPR
2354 && code != CEIL_DIV_EXPR
2355 && code != EXACT_DIV_EXPR
2356 && code != ROUND_DIV_EXPR
2357 && code != TRUNC_MOD_EXPR
2358 && code != RSHIFT_EXPR
2359 && code != LSHIFT_EXPR
2360 && code != MIN_EXPR
2361 && code != MAX_EXPR
2362 && code != BIT_AND_EXPR
2363 && code != BIT_IOR_EXPR
2364 && code != BIT_XOR_EXPR)
2366 set_value_range_to_varying (vr);
2367 return;
2370 /* If both ranges are UNDEFINED, so is the result. */
2371 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2373 set_value_range_to_undefined (vr);
2374 return;
2376 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2377 code. At some point we may want to special-case operations that
2378 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2379 operand. */
2380 else if (vr0.type == VR_UNDEFINED)
2381 set_value_range_to_varying (&vr0);
2382 else if (vr1.type == VR_UNDEFINED)
2383 set_value_range_to_varying (&vr1);
2385 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2386 and express ~[] op X as ([]' op X) U ([]'' op X). */
2387 if (vr0.type == VR_ANTI_RANGE
2388 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2390 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2391 if (vrtem1.type != VR_UNDEFINED)
2393 value_range_t vrres = VR_INITIALIZER;
2394 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2395 &vrtem1, vr1_);
2396 vrp_meet (vr, &vrres);
2398 return;
2400 /* Likewise for X op ~[]. */
2401 if (vr1.type == VR_ANTI_RANGE
2402 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2404 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2405 if (vrtem1.type != VR_UNDEFINED)
2407 value_range_t vrres = VR_INITIALIZER;
2408 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2409 vr0_, &vrtem1);
2410 vrp_meet (vr, &vrres);
2412 return;
2415 /* The type of the resulting value range defaults to VR0.TYPE. */
2416 type = vr0.type;
2418 /* Refuse to operate on VARYING ranges, ranges of different kinds
2419 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2420 because we may be able to derive a useful range even if one of
2421 the operands is VR_VARYING or symbolic range. Similarly for
2422 divisions, MIN/MAX and PLUS/MINUS.
2424 TODO, we may be able to derive anti-ranges in some cases. */
2425 if (code != BIT_AND_EXPR
2426 && code != BIT_IOR_EXPR
2427 && code != TRUNC_DIV_EXPR
2428 && code != FLOOR_DIV_EXPR
2429 && code != CEIL_DIV_EXPR
2430 && code != EXACT_DIV_EXPR
2431 && code != ROUND_DIV_EXPR
2432 && code != TRUNC_MOD_EXPR
2433 && code != MIN_EXPR
2434 && code != MAX_EXPR
2435 && code != PLUS_EXPR
2436 && code != MINUS_EXPR
2437 && (vr0.type == VR_VARYING
2438 || vr1.type == VR_VARYING
2439 || vr0.type != vr1.type
2440 || symbolic_range_p (&vr0)
2441 || symbolic_range_p (&vr1)))
2443 set_value_range_to_varying (vr);
2444 return;
2447 /* Now evaluate the expression to determine the new range. */
2448 if (POINTER_TYPE_P (expr_type))
2450 if (code == MIN_EXPR || code == MAX_EXPR)
2452 /* For MIN/MAX expressions with pointers, we only care about
2453 nullness, if both are non null, then the result is nonnull.
2454 If both are null, then the result is null. Otherwise they
2455 are varying. */
2456 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2457 set_value_range_to_nonnull (vr, expr_type);
2458 else if (range_is_null (&vr0) && range_is_null (&vr1))
2459 set_value_range_to_null (vr, expr_type);
2460 else
2461 set_value_range_to_varying (vr);
2463 else if (code == POINTER_PLUS_EXPR)
2465 /* For pointer types, we are really only interested in asserting
2466 whether the expression evaluates to non-NULL. */
2467 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2468 set_value_range_to_nonnull (vr, expr_type);
2469 else if (range_is_null (&vr0) && range_is_null (&vr1))
2470 set_value_range_to_null (vr, expr_type);
2471 else
2472 set_value_range_to_varying (vr);
2474 else if (code == BIT_AND_EXPR)
2476 /* For pointer types, we are really only interested in asserting
2477 whether the expression evaluates to non-NULL. */
2478 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2479 set_value_range_to_nonnull (vr, expr_type);
2480 else if (range_is_null (&vr0) || range_is_null (&vr1))
2481 set_value_range_to_null (vr, expr_type);
2482 else
2483 set_value_range_to_varying (vr);
2485 else
2486 set_value_range_to_varying (vr);
2488 return;
2491 /* For integer ranges, apply the operation to each end of the
2492 range and see what we end up with. */
2493 if (code == PLUS_EXPR || code == MINUS_EXPR)
2495 const bool minus_p = (code == MINUS_EXPR);
2496 tree min_op0 = vr0.min;
2497 tree min_op1 = minus_p ? vr1.max : vr1.min;
2498 tree max_op0 = vr0.max;
2499 tree max_op1 = minus_p ? vr1.min : vr1.max;
2500 tree sym_min_op0 = NULL_TREE;
2501 tree sym_min_op1 = NULL_TREE;
2502 tree sym_max_op0 = NULL_TREE;
2503 tree sym_max_op1 = NULL_TREE;
2504 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2506 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2507 single-symbolic ranges, try to compute the precise resulting range,
2508 but only if we know that this resulting range will also be constant
2509 or single-symbolic. */
2510 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2511 && (TREE_CODE (min_op0) == INTEGER_CST
2512 || (sym_min_op0
2513 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2514 && (TREE_CODE (min_op1) == INTEGER_CST
2515 || (sym_min_op1
2516 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2517 && (!(sym_min_op0 && sym_min_op1)
2518 || (sym_min_op0 == sym_min_op1
2519 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2520 && (TREE_CODE (max_op0) == INTEGER_CST
2521 || (sym_max_op0
2522 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2523 && (TREE_CODE (max_op1) == INTEGER_CST
2524 || (sym_max_op1
2525 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2526 && (!(sym_max_op0 && sym_max_op1)
2527 || (sym_max_op0 == sym_max_op1
2528 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2530 const signop sgn = TYPE_SIGN (expr_type);
2531 const unsigned int prec = TYPE_PRECISION (expr_type);
2532 wide_int type_min, type_max, wmin, wmax;
2533 int min_ovf = 0;
2534 int max_ovf = 0;
2536 /* Get the lower and upper bounds of the type. */
2537 if (TYPE_OVERFLOW_WRAPS (expr_type))
2539 type_min = wi::min_value (prec, sgn);
2540 type_max = wi::max_value (prec, sgn);
2542 else
2544 type_min = vrp_val_min (expr_type);
2545 type_max = vrp_val_max (expr_type);
2548 /* Combine the lower bounds, if any. */
2549 if (min_op0 && min_op1)
2551 if (minus_p)
2553 wmin = wi::sub (min_op0, min_op1);
2555 /* Check for overflow. */
2556 if (wi::cmp (0, min_op1, sgn)
2557 != wi::cmp (wmin, min_op0, sgn))
2558 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2560 else
2562 wmin = wi::add (min_op0, min_op1);
2564 /* Check for overflow. */
2565 if (wi::cmp (min_op1, 0, sgn)
2566 != wi::cmp (wmin, min_op0, sgn))
2567 min_ovf = wi::cmp (min_op0, wmin, sgn);
2570 else if (min_op0)
2571 wmin = min_op0;
2572 else if (min_op1)
2573 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2574 else
2575 wmin = wi::shwi (0, prec);
2577 /* Combine the upper bounds, if any. */
2578 if (max_op0 && max_op1)
2580 if (minus_p)
2582 wmax = wi::sub (max_op0, max_op1);
2584 /* Check for overflow. */
2585 if (wi::cmp (0, max_op1, sgn)
2586 != wi::cmp (wmax, max_op0, sgn))
2587 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2589 else
2591 wmax = wi::add (max_op0, max_op1);
2593 if (wi::cmp (max_op1, 0, sgn)
2594 != wi::cmp (wmax, max_op0, sgn))
2595 max_ovf = wi::cmp (max_op0, wmax, sgn);
2598 else if (max_op0)
2599 wmax = max_op0;
2600 else if (max_op1)
2601 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2602 else
2603 wmax = wi::shwi (0, prec);
2605 /* Check for type overflow. */
2606 if (min_ovf == 0)
2608 if (wi::cmp (wmin, type_min, sgn) == -1)
2609 min_ovf = -1;
2610 else if (wi::cmp (wmin, type_max, sgn) == 1)
2611 min_ovf = 1;
2613 if (max_ovf == 0)
2615 if (wi::cmp (wmax, type_min, sgn) == -1)
2616 max_ovf = -1;
2617 else if (wi::cmp (wmax, type_max, sgn) == 1)
2618 max_ovf = 1;
2621 /* If we have overflow for the constant part and the resulting
2622 range will be symbolic, drop to VR_VARYING. */
2623 if ((min_ovf && sym_min_op0 != sym_min_op1)
2624 || (max_ovf && sym_max_op0 != sym_max_op1))
2626 set_value_range_to_varying (vr);
2627 return;
2630 if (TYPE_OVERFLOW_WRAPS (expr_type))
2632 /* If overflow wraps, truncate the values and adjust the
2633 range kind and bounds appropriately. */
2634 wide_int tmin = wide_int::from (wmin, prec, sgn);
2635 wide_int tmax = wide_int::from (wmax, prec, sgn);
2636 if (min_ovf == max_ovf)
2638 /* No overflow or both overflow or underflow. The
2639 range kind stays VR_RANGE. */
2640 min = wide_int_to_tree (expr_type, tmin);
2641 max = wide_int_to_tree (expr_type, tmax);
2643 else if (min_ovf == -1 && max_ovf == 1)
2645 /* Underflow and overflow, drop to VR_VARYING. */
2646 set_value_range_to_varying (vr);
2647 return;
2649 else
2651 /* Min underflow or max overflow. The range kind
2652 changes to VR_ANTI_RANGE. */
2653 bool covers = false;
2654 wide_int tem = tmin;
2655 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2656 || (max_ovf == 1 && min_ovf == 0));
2657 type = VR_ANTI_RANGE;
2658 tmin = tmax + 1;
2659 if (wi::cmp (tmin, tmax, sgn) < 0)
2660 covers = true;
2661 tmax = tem - 1;
2662 if (wi::cmp (tmax, tem, sgn) > 0)
2663 covers = true;
2664 /* If the anti-range would cover nothing, drop to varying.
2665 Likewise if the anti-range bounds are outside of the
2666 types values. */
2667 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2669 set_value_range_to_varying (vr);
2670 return;
2672 min = wide_int_to_tree (expr_type, tmin);
2673 max = wide_int_to_tree (expr_type, tmax);
2676 else
2678 /* If overflow does not wrap, saturate to the types min/max
2679 value. */
2680 if (min_ovf == -1)
2682 if (needs_overflow_infinity (expr_type)
2683 && supports_overflow_infinity (expr_type))
2684 min = negative_overflow_infinity (expr_type);
2685 else
2686 min = wide_int_to_tree (expr_type, type_min);
2688 else if (min_ovf == 1)
2690 if (needs_overflow_infinity (expr_type)
2691 && supports_overflow_infinity (expr_type))
2692 min = positive_overflow_infinity (expr_type);
2693 else
2694 min = wide_int_to_tree (expr_type, type_max);
2696 else
2697 min = wide_int_to_tree (expr_type, wmin);
2699 if (max_ovf == -1)
2701 if (needs_overflow_infinity (expr_type)
2702 && supports_overflow_infinity (expr_type))
2703 max = negative_overflow_infinity (expr_type);
2704 else
2705 max = wide_int_to_tree (expr_type, type_min);
2707 else if (max_ovf == 1)
2709 if (needs_overflow_infinity (expr_type)
2710 && supports_overflow_infinity (expr_type))
2711 max = positive_overflow_infinity (expr_type);
2712 else
2713 max = wide_int_to_tree (expr_type, type_max);
2715 else
2716 max = wide_int_to_tree (expr_type, wmax);
2719 if (needs_overflow_infinity (expr_type)
2720 && supports_overflow_infinity (expr_type))
2722 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2723 || (min_op1
2724 && (minus_p
2725 ? is_positive_overflow_infinity (min_op1)
2726 : is_negative_overflow_infinity (min_op1))))
2727 min = negative_overflow_infinity (expr_type);
2728 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2729 || (max_op1
2730 && (minus_p
2731 ? is_negative_overflow_infinity (max_op1)
2732 : is_positive_overflow_infinity (max_op1))))
2733 max = positive_overflow_infinity (expr_type);
2736 /* If the result lower bound is constant, we're done;
2737 otherwise, build the symbolic lower bound. */
2738 if (sym_min_op0 == sym_min_op1)
2740 else if (sym_min_op0)
2741 min = build_symbolic_expr (expr_type, sym_min_op0,
2742 neg_min_op0, min);
2743 else if (sym_min_op1)
2744 min = build_symbolic_expr (expr_type, sym_min_op1,
2745 neg_min_op1 ^ minus_p, min);
2747 /* Likewise for the upper bound. */
2748 if (sym_max_op0 == sym_max_op1)
2750 else if (sym_max_op0)
2751 max = build_symbolic_expr (expr_type, sym_max_op0,
2752 neg_max_op0, max);
2753 else if (sym_max_op1)
2754 max = build_symbolic_expr (expr_type, sym_max_op1,
2755 neg_max_op1 ^ minus_p, max);
2757 else
2759 /* For other cases, for example if we have a PLUS_EXPR with two
2760 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2761 to compute a precise range for such a case.
2762 ??? General even mixed range kind operations can be expressed
2763 by for example transforming ~[3, 5] + [1, 2] to range-only
2764 operations and a union primitive:
2765 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2766 [-INF+1, 4] U [6, +INF(OVF)]
2767 though usually the union is not exactly representable with
2768 a single range or anti-range as the above is
2769 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2770 but one could use a scheme similar to equivalences for this. */
2771 set_value_range_to_varying (vr);
2772 return;
2775 else if (code == MIN_EXPR
2776 || code == MAX_EXPR)
2778 if (vr0.type == VR_RANGE
2779 && !symbolic_range_p (&vr0))
2781 type = VR_RANGE;
2782 if (vr1.type == VR_RANGE
2783 && !symbolic_range_p (&vr1))
2785 /* For operations that make the resulting range directly
2786 proportional to the original ranges, apply the operation to
2787 the same end of each range. */
2788 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2789 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2791 else if (code == MIN_EXPR)
2793 min = vrp_val_min (expr_type);
2794 max = vr0.max;
2796 else if (code == MAX_EXPR)
2798 min = vr0.min;
2799 max = vrp_val_max (expr_type);
2802 else if (vr1.type == VR_RANGE
2803 && !symbolic_range_p (&vr1))
2805 type = VR_RANGE;
2806 if (code == MIN_EXPR)
2808 min = vrp_val_min (expr_type);
2809 max = vr1.max;
2811 else if (code == MAX_EXPR)
2813 min = vr1.min;
2814 max = vrp_val_max (expr_type);
2817 else
2819 set_value_range_to_varying (vr);
2820 return;
2823 else if (code == MULT_EXPR)
2825 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2826 drop to varying. This test requires 2*prec bits if both
2827 operands are signed and 2*prec + 2 bits if either is not. */
2829 signop sign = TYPE_SIGN (expr_type);
2830 unsigned int prec = TYPE_PRECISION (expr_type);
2832 if (range_int_cst_p (&vr0)
2833 && range_int_cst_p (&vr1)
2834 && TYPE_OVERFLOW_WRAPS (expr_type))
2836 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2837 typedef generic_wide_int
2838 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2839 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2840 vrp_int size = sizem1 + 1;
2842 /* Extend the values using the sign of the result to PREC2.
2843 From here on out, everthing is just signed math no matter
2844 what the input types were. */
2845 vrp_int min0 = vrp_int_cst (vr0.min);
2846 vrp_int max0 = vrp_int_cst (vr0.max);
2847 vrp_int min1 = vrp_int_cst (vr1.min);
2848 vrp_int max1 = vrp_int_cst (vr1.max);
2849 /* Canonicalize the intervals. */
2850 if (sign == UNSIGNED)
2852 if (wi::ltu_p (size, min0 + max0))
2854 min0 -= size;
2855 max0 -= size;
2858 if (wi::ltu_p (size, min1 + max1))
2860 min1 -= size;
2861 max1 -= size;
2865 vrp_int prod0 = min0 * min1;
2866 vrp_int prod1 = min0 * max1;
2867 vrp_int prod2 = max0 * min1;
2868 vrp_int prod3 = max0 * max1;
2870 /* Sort the 4 products so that min is in prod0 and max is in
2871 prod3. */
2872 /* min0min1 > max0max1 */
2873 if (wi::gts_p (prod0, prod3))
2875 vrp_int tmp = prod3;
2876 prod3 = prod0;
2877 prod0 = tmp;
2880 /* min0max1 > max0min1 */
2881 if (wi::gts_p (prod1, prod2))
2883 vrp_int tmp = prod2;
2884 prod2 = prod1;
2885 prod1 = tmp;
2888 if (wi::gts_p (prod0, prod1))
2890 vrp_int tmp = prod1;
2891 prod1 = prod0;
2892 prod0 = tmp;
2895 if (wi::gts_p (prod2, prod3))
2897 vrp_int tmp = prod3;
2898 prod3 = prod2;
2899 prod2 = tmp;
2902 /* diff = max - min. */
2903 prod2 = prod3 - prod0;
2904 if (wi::geu_p (prod2, sizem1))
2906 /* the range covers all values. */
2907 set_value_range_to_varying (vr);
2908 return;
2911 /* The following should handle the wrapping and selecting
2912 VR_ANTI_RANGE for us. */
2913 min = wide_int_to_tree (expr_type, prod0);
2914 max = wide_int_to_tree (expr_type, prod3);
2915 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2916 return;
2919 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2920 drop to VR_VARYING. It would take more effort to compute a
2921 precise range for such a case. For example, if we have
2922 op0 == 65536 and op1 == 65536 with their ranges both being
2923 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2924 we cannot claim that the product is in ~[0,0]. Note that we
2925 are guaranteed to have vr0.type == vr1.type at this
2926 point. */
2927 if (vr0.type == VR_ANTI_RANGE
2928 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2930 set_value_range_to_varying (vr);
2931 return;
2934 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2935 return;
2937 else if (code == RSHIFT_EXPR
2938 || code == LSHIFT_EXPR)
2940 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2941 then drop to VR_VARYING. Outside of this range we get undefined
2942 behavior from the shift operation. We cannot even trust
2943 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2944 shifts, and the operation at the tree level may be widened. */
2945 if (range_int_cst_p (&vr1)
2946 && compare_tree_int (vr1.min, 0) >= 0
2947 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2949 if (code == RSHIFT_EXPR)
2951 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2952 return;
2954 /* We can map lshifts by constants to MULT_EXPR handling. */
2955 else if (code == LSHIFT_EXPR
2956 && range_int_cst_singleton_p (&vr1))
2958 bool saved_flag_wrapv;
2959 value_range_t vr1p = VR_INITIALIZER;
2960 vr1p.type = VR_RANGE;
2961 vr1p.min = (wide_int_to_tree
2962 (expr_type,
2963 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2964 TYPE_PRECISION (expr_type))));
2965 vr1p.max = vr1p.min;
2966 /* We have to use a wrapping multiply though as signed overflow
2967 on lshifts is implementation defined in C89. */
2968 saved_flag_wrapv = flag_wrapv;
2969 flag_wrapv = 1;
2970 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2971 &vr0, &vr1p);
2972 flag_wrapv = saved_flag_wrapv;
2973 return;
2975 else if (code == LSHIFT_EXPR
2976 && range_int_cst_p (&vr0))
2978 int prec = TYPE_PRECISION (expr_type);
2979 int overflow_pos = prec;
2980 int bound_shift;
2981 wide_int low_bound, high_bound;
2982 bool uns = TYPE_UNSIGNED (expr_type);
2983 bool in_bounds = false;
2985 if (!uns)
2986 overflow_pos -= 1;
2988 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2989 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2990 overflow. However, for that to happen, vr1.max needs to be
2991 zero, which means vr1 is a singleton range of zero, which
2992 means it should be handled by the previous LSHIFT_EXPR
2993 if-clause. */
2994 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2995 wide_int complement = ~(bound - 1);
2997 if (uns)
2999 low_bound = bound;
3000 high_bound = complement;
3001 if (wi::ltu_p (vr0.max, low_bound))
3003 /* [5, 6] << [1, 2] == [10, 24]. */
3004 /* We're shifting out only zeroes, the value increases
3005 monotonically. */
3006 in_bounds = true;
3008 else if (wi::ltu_p (high_bound, vr0.min))
3010 /* [0xffffff00, 0xffffffff] << [1, 2]
3011 == [0xfffffc00, 0xfffffffe]. */
3012 /* We're shifting out only ones, the value decreases
3013 monotonically. */
3014 in_bounds = true;
3017 else
3019 /* [-1, 1] << [1, 2] == [-4, 4]. */
3020 low_bound = complement;
3021 high_bound = bound;
3022 if (wi::lts_p (vr0.max, high_bound)
3023 && wi::lts_p (low_bound, vr0.min))
3025 /* For non-negative numbers, we're shifting out only
3026 zeroes, the value increases monotonically.
3027 For negative numbers, we're shifting out only ones, the
3028 value decreases monotomically. */
3029 in_bounds = true;
3033 if (in_bounds)
3035 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3036 return;
3040 set_value_range_to_varying (vr);
3041 return;
3043 else if (code == TRUNC_DIV_EXPR
3044 || code == FLOOR_DIV_EXPR
3045 || code == CEIL_DIV_EXPR
3046 || code == EXACT_DIV_EXPR
3047 || code == ROUND_DIV_EXPR)
3049 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3051 /* For division, if op1 has VR_RANGE but op0 does not, something
3052 can be deduced just from that range. Say [min, max] / [4, max]
3053 gives [min / 4, max / 4] range. */
3054 if (vr1.type == VR_RANGE
3055 && !symbolic_range_p (&vr1)
3056 && range_includes_zero_p (vr1.min, vr1.max) == 0)
3058 vr0.type = type = VR_RANGE;
3059 vr0.min = vrp_val_min (expr_type);
3060 vr0.max = vrp_val_max (expr_type);
3062 else
3064 set_value_range_to_varying (vr);
3065 return;
3069 /* For divisions, if flag_non_call_exceptions is true, we must
3070 not eliminate a division by zero. */
3071 if (cfun->can_throw_non_call_exceptions
3072 && (vr1.type != VR_RANGE
3073 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3075 set_value_range_to_varying (vr);
3076 return;
3079 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3080 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3081 include 0. */
3082 if (vr0.type == VR_RANGE
3083 && (vr1.type != VR_RANGE
3084 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3086 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
3087 int cmp;
3089 min = NULL_TREE;
3090 max = NULL_TREE;
3091 if (TYPE_UNSIGNED (expr_type)
3092 || value_range_nonnegative_p (&vr1))
3094 /* For unsigned division or when divisor is known
3095 to be non-negative, the range has to cover
3096 all numbers from 0 to max for positive max
3097 and all numbers from min to 0 for negative min. */
3098 cmp = compare_values (vr0.max, zero);
3099 if (cmp == -1)
3100 max = zero;
3101 else if (cmp == 0 || cmp == 1)
3102 max = vr0.max;
3103 else
3104 type = VR_VARYING;
3105 cmp = compare_values (vr0.min, zero);
3106 if (cmp == 1)
3107 min = zero;
3108 else if (cmp == 0 || cmp == -1)
3109 min = vr0.min;
3110 else
3111 type = VR_VARYING;
3113 else
3115 /* Otherwise the range is -max .. max or min .. -min
3116 depending on which bound is bigger in absolute value,
3117 as the division can change the sign. */
3118 abs_extent_range (vr, vr0.min, vr0.max);
3119 return;
3121 if (type == VR_VARYING)
3123 set_value_range_to_varying (vr);
3124 return;
3127 else
3129 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3130 return;
3133 else if (code == TRUNC_MOD_EXPR)
3135 if (vr1.type != VR_RANGE
3136 || range_includes_zero_p (vr1.min, vr1.max) != 0
3137 || vrp_val_is_min (vr1.min))
3139 set_value_range_to_varying (vr);
3140 return;
3142 type = VR_RANGE;
3143 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3144 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
3145 if (tree_int_cst_lt (max, vr1.max))
3146 max = vr1.max;
3147 max = int_const_binop (MINUS_EXPR, max, build_int_cst (TREE_TYPE (max), 1));
3148 /* If the dividend is non-negative the modulus will be
3149 non-negative as well. */
3150 if (TYPE_UNSIGNED (expr_type)
3151 || value_range_nonnegative_p (&vr0))
3152 min = build_int_cst (TREE_TYPE (max), 0);
3153 else
3154 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
3156 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3158 bool int_cst_range0, int_cst_range1;
3159 wide_int may_be_nonzero0, may_be_nonzero1;
3160 wide_int must_be_nonzero0, must_be_nonzero1;
3162 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3163 &may_be_nonzero0,
3164 &must_be_nonzero0);
3165 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3166 &may_be_nonzero1,
3167 &must_be_nonzero1);
3169 type = VR_RANGE;
3170 if (code == BIT_AND_EXPR)
3172 min = wide_int_to_tree (expr_type,
3173 must_be_nonzero0 & must_be_nonzero1);
3174 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3175 /* If both input ranges contain only negative values we can
3176 truncate the result range maximum to the minimum of the
3177 input range maxima. */
3178 if (int_cst_range0 && int_cst_range1
3179 && tree_int_cst_sgn (vr0.max) < 0
3180 && tree_int_cst_sgn (vr1.max) < 0)
3182 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3183 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3185 /* If either input range contains only non-negative values
3186 we can truncate the result range maximum to the respective
3187 maximum of the input range. */
3188 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3189 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3190 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3191 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3192 max = wide_int_to_tree (expr_type, wmax);
3194 else if (code == BIT_IOR_EXPR)
3196 max = wide_int_to_tree (expr_type,
3197 may_be_nonzero0 | may_be_nonzero1);
3198 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3199 /* If the input ranges contain only positive values we can
3200 truncate the minimum of the result range to the maximum
3201 of the input range minima. */
3202 if (int_cst_range0 && int_cst_range1
3203 && tree_int_cst_sgn (vr0.min) >= 0
3204 && tree_int_cst_sgn (vr1.min) >= 0)
3206 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3207 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3209 /* If either input range contains only negative values
3210 we can truncate the minimum of the result range to the
3211 respective minimum range. */
3212 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3213 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3214 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3215 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3216 min = wide_int_to_tree (expr_type, wmin);
3218 else if (code == BIT_XOR_EXPR)
3220 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3221 | ~(may_be_nonzero0 | may_be_nonzero1));
3222 wide_int result_one_bits
3223 = (must_be_nonzero0.and_not (may_be_nonzero1)
3224 | must_be_nonzero1.and_not (may_be_nonzero0));
3225 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3226 min = wide_int_to_tree (expr_type, result_one_bits);
3227 /* If the range has all positive or all negative values the
3228 result is better than VARYING. */
3229 if (tree_int_cst_sgn (min) < 0
3230 || tree_int_cst_sgn (max) >= 0)
3232 else
3233 max = min = NULL_TREE;
3236 else
3237 gcc_unreachable ();
3239 /* If either MIN or MAX overflowed, then set the resulting range to
3240 VARYING. But we do accept an overflow infinity representation. */
3241 if (min == NULL_TREE
3242 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3243 || max == NULL_TREE
3244 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3246 set_value_range_to_varying (vr);
3247 return;
3250 /* We punt if:
3251 1) [-INF, +INF]
3252 2) [-INF, +-INF(OVF)]
3253 3) [+-INF(OVF), +INF]
3254 4) [+-INF(OVF), +-INF(OVF)]
3255 We learn nothing when we have INF and INF(OVF) on both sides.
3256 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3257 overflow. */
3258 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3259 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3261 set_value_range_to_varying (vr);
3262 return;
3265 cmp = compare_values (min, max);
3266 if (cmp == -2 || cmp == 1)
3268 /* If the new range has its limits swapped around (MIN > MAX),
3269 then the operation caused one of them to wrap around, mark
3270 the new range VARYING. */
3271 set_value_range_to_varying (vr);
3273 else
3274 set_value_range (vr, type, min, max, NULL);
3277 /* Extract range information from a binary expression OP0 CODE OP1 based on
3278 the ranges of each of its operands with resulting type EXPR_TYPE.
3279 The resulting range is stored in *VR. */
3281 static void
3282 extract_range_from_binary_expr (value_range_t *vr,
3283 enum tree_code code,
3284 tree expr_type, tree op0, tree op1)
3286 value_range_t vr0 = VR_INITIALIZER;
3287 value_range_t vr1 = VR_INITIALIZER;
3289 /* Get value ranges for each operand. For constant operands, create
3290 a new value range with the operand to simplify processing. */
3291 if (TREE_CODE (op0) == SSA_NAME)
3292 vr0 = *(get_value_range (op0));
3293 else if (is_gimple_min_invariant (op0))
3294 set_value_range_to_value (&vr0, op0, NULL);
3295 else
3296 set_value_range_to_varying (&vr0);
3298 if (TREE_CODE (op1) == SSA_NAME)
3299 vr1 = *(get_value_range (op1));
3300 else if (is_gimple_min_invariant (op1))
3301 set_value_range_to_value (&vr1, op1, NULL);
3302 else
3303 set_value_range_to_varying (&vr1);
3305 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3307 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3308 and based on the other operand, for example if it was deduced from a
3309 symbolic comparison. When a bound of the range of the first operand
3310 is invariant, we set the corresponding bound of the new range to INF
3311 in order to avoid recursing on the range of the second operand. */
3312 if (vr->type == VR_VARYING
3313 && (code == PLUS_EXPR || code == MINUS_EXPR)
3314 && TREE_CODE (op1) == SSA_NAME
3315 && vr0.type == VR_RANGE
3316 && symbolic_range_based_on_p (&vr0, op1))
3318 const bool minus_p = (code == MINUS_EXPR);
3319 value_range_t n_vr1 = VR_INITIALIZER;
3321 /* Try with VR0 and [-INF, OP1]. */
3322 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3323 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3325 /* Try with VR0 and [OP1, +INF]. */
3326 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3327 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3329 /* Try with VR0 and [OP1, OP1]. */
3330 else
3331 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3333 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3336 if (vr->type == VR_VARYING
3337 && (code == PLUS_EXPR || code == MINUS_EXPR)
3338 && TREE_CODE (op0) == SSA_NAME
3339 && vr1.type == VR_RANGE
3340 && symbolic_range_based_on_p (&vr1, op0))
3342 const bool minus_p = (code == MINUS_EXPR);
3343 value_range_t n_vr0 = VR_INITIALIZER;
3345 /* Try with [-INF, OP0] and VR1. */
3346 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3347 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3349 /* Try with [OP0, +INF] and VR1. */
3350 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3351 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3353 /* Try with [OP0, OP0] and VR1. */
3354 else
3355 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3357 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3361 /* Extract range information from a unary operation CODE based on
3362 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3363 The The resulting range is stored in *VR. */
3365 static void
3366 extract_range_from_unary_expr_1 (value_range_t *vr,
3367 enum tree_code code, tree type,
3368 value_range_t *vr0_, tree op0_type)
3370 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3372 /* VRP only operates on integral and pointer types. */
3373 if (!(INTEGRAL_TYPE_P (op0_type)
3374 || POINTER_TYPE_P (op0_type))
3375 || !(INTEGRAL_TYPE_P (type)
3376 || POINTER_TYPE_P (type)))
3378 set_value_range_to_varying (vr);
3379 return;
3382 /* If VR0 is UNDEFINED, so is the result. */
3383 if (vr0.type == VR_UNDEFINED)
3385 set_value_range_to_undefined (vr);
3386 return;
3389 /* Handle operations that we express in terms of others. */
3390 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3392 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3393 copy_value_range (vr, &vr0);
3394 return;
3396 else if (code == NEGATE_EXPR)
3398 /* -X is simply 0 - X, so re-use existing code that also handles
3399 anti-ranges fine. */
3400 value_range_t zero = VR_INITIALIZER;
3401 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3402 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3403 return;
3405 else if (code == BIT_NOT_EXPR)
3407 /* ~X is simply -1 - X, so re-use existing code that also handles
3408 anti-ranges fine. */
3409 value_range_t minusone = VR_INITIALIZER;
3410 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3411 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3412 type, &minusone, &vr0);
3413 return;
3416 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3417 and express op ~[] as (op []') U (op []''). */
3418 if (vr0.type == VR_ANTI_RANGE
3419 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3421 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3422 if (vrtem1.type != VR_UNDEFINED)
3424 value_range_t vrres = VR_INITIALIZER;
3425 extract_range_from_unary_expr_1 (&vrres, code, type,
3426 &vrtem1, op0_type);
3427 vrp_meet (vr, &vrres);
3429 return;
3432 if (CONVERT_EXPR_CODE_P (code))
3434 tree inner_type = op0_type;
3435 tree outer_type = type;
3437 /* If the expression evaluates to a pointer, we are only interested in
3438 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3439 if (POINTER_TYPE_P (type))
3441 if (range_is_nonnull (&vr0))
3442 set_value_range_to_nonnull (vr, type);
3443 else if (range_is_null (&vr0))
3444 set_value_range_to_null (vr, type);
3445 else
3446 set_value_range_to_varying (vr);
3447 return;
3450 /* If VR0 is varying and we increase the type precision, assume
3451 a full range for the following transformation. */
3452 if (vr0.type == VR_VARYING
3453 && INTEGRAL_TYPE_P (inner_type)
3454 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3456 vr0.type = VR_RANGE;
3457 vr0.min = TYPE_MIN_VALUE (inner_type);
3458 vr0.max = TYPE_MAX_VALUE (inner_type);
3461 /* If VR0 is a constant range or anti-range and the conversion is
3462 not truncating we can convert the min and max values and
3463 canonicalize the resulting range. Otherwise we can do the
3464 conversion if the size of the range is less than what the
3465 precision of the target type can represent and the range is
3466 not an anti-range. */
3467 if ((vr0.type == VR_RANGE
3468 || vr0.type == VR_ANTI_RANGE)
3469 && TREE_CODE (vr0.min) == INTEGER_CST
3470 && TREE_CODE (vr0.max) == INTEGER_CST
3471 && (!is_overflow_infinity (vr0.min)
3472 || (vr0.type == VR_RANGE
3473 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3474 && needs_overflow_infinity (outer_type)
3475 && supports_overflow_infinity (outer_type)))
3476 && (!is_overflow_infinity (vr0.max)
3477 || (vr0.type == VR_RANGE
3478 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3479 && needs_overflow_infinity (outer_type)
3480 && supports_overflow_infinity (outer_type)))
3481 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3482 || (vr0.type == VR_RANGE
3483 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3484 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3485 size_int (TYPE_PRECISION (outer_type)))))))
3487 tree new_min, new_max;
3488 if (is_overflow_infinity (vr0.min))
3489 new_min = negative_overflow_infinity (outer_type);
3490 else
3491 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3492 0, false);
3493 if (is_overflow_infinity (vr0.max))
3494 new_max = positive_overflow_infinity (outer_type);
3495 else
3496 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3497 0, false);
3498 set_and_canonicalize_value_range (vr, vr0.type,
3499 new_min, new_max, NULL);
3500 return;
3503 set_value_range_to_varying (vr);
3504 return;
3506 else if (code == ABS_EXPR)
3508 tree min, max;
3509 int cmp;
3511 /* Pass through vr0 in the easy cases. */
3512 if (TYPE_UNSIGNED (type)
3513 || value_range_nonnegative_p (&vr0))
3515 copy_value_range (vr, &vr0);
3516 return;
3519 /* For the remaining varying or symbolic ranges we can't do anything
3520 useful. */
3521 if (vr0.type == VR_VARYING
3522 || symbolic_range_p (&vr0))
3524 set_value_range_to_varying (vr);
3525 return;
3528 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3529 useful range. */
3530 if (!TYPE_OVERFLOW_UNDEFINED (type)
3531 && ((vr0.type == VR_RANGE
3532 && vrp_val_is_min (vr0.min))
3533 || (vr0.type == VR_ANTI_RANGE
3534 && !vrp_val_is_min (vr0.min))))
3536 set_value_range_to_varying (vr);
3537 return;
3540 /* ABS_EXPR may flip the range around, if the original range
3541 included negative values. */
3542 if (is_overflow_infinity (vr0.min))
3543 min = positive_overflow_infinity (type);
3544 else if (!vrp_val_is_min (vr0.min))
3545 min = fold_unary_to_constant (code, type, vr0.min);
3546 else if (!needs_overflow_infinity (type))
3547 min = TYPE_MAX_VALUE (type);
3548 else if (supports_overflow_infinity (type))
3549 min = positive_overflow_infinity (type);
3550 else
3552 set_value_range_to_varying (vr);
3553 return;
3556 if (is_overflow_infinity (vr0.max))
3557 max = positive_overflow_infinity (type);
3558 else if (!vrp_val_is_min (vr0.max))
3559 max = fold_unary_to_constant (code, type, vr0.max);
3560 else if (!needs_overflow_infinity (type))
3561 max = TYPE_MAX_VALUE (type);
3562 else if (supports_overflow_infinity (type)
3563 /* We shouldn't generate [+INF, +INF] as set_value_range
3564 doesn't like this and ICEs. */
3565 && !is_positive_overflow_infinity (min))
3566 max = positive_overflow_infinity (type);
3567 else
3569 set_value_range_to_varying (vr);
3570 return;
3573 cmp = compare_values (min, max);
3575 /* If a VR_ANTI_RANGEs contains zero, then we have
3576 ~[-INF, min(MIN, MAX)]. */
3577 if (vr0.type == VR_ANTI_RANGE)
3579 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3581 /* Take the lower of the two values. */
3582 if (cmp != 1)
3583 max = min;
3585 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3586 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3587 flag_wrapv is set and the original anti-range doesn't include
3588 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3589 if (TYPE_OVERFLOW_WRAPS (type))
3591 tree type_min_value = TYPE_MIN_VALUE (type);
3593 min = (vr0.min != type_min_value
3594 ? int_const_binop (PLUS_EXPR, type_min_value,
3595 build_int_cst (TREE_TYPE (type_min_value), 1))
3596 : type_min_value);
3598 else
3600 if (overflow_infinity_range_p (&vr0))
3601 min = negative_overflow_infinity (type);
3602 else
3603 min = TYPE_MIN_VALUE (type);
3606 else
3608 /* All else has failed, so create the range [0, INF], even for
3609 flag_wrapv since TYPE_MIN_VALUE is in the original
3610 anti-range. */
3611 vr0.type = VR_RANGE;
3612 min = build_int_cst (type, 0);
3613 if (needs_overflow_infinity (type))
3615 if (supports_overflow_infinity (type))
3616 max = positive_overflow_infinity (type);
3617 else
3619 set_value_range_to_varying (vr);
3620 return;
3623 else
3624 max = TYPE_MAX_VALUE (type);
3628 /* If the range contains zero then we know that the minimum value in the
3629 range will be zero. */
3630 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3632 if (cmp == 1)
3633 max = min;
3634 min = build_int_cst (type, 0);
3636 else
3638 /* If the range was reversed, swap MIN and MAX. */
3639 if (cmp == 1)
3641 tree t = min;
3642 min = max;
3643 max = t;
3647 cmp = compare_values (min, max);
3648 if (cmp == -2 || cmp == 1)
3650 /* If the new range has its limits swapped around (MIN > MAX),
3651 then the operation caused one of them to wrap around, mark
3652 the new range VARYING. */
3653 set_value_range_to_varying (vr);
3655 else
3656 set_value_range (vr, vr0.type, min, max, NULL);
3657 return;
3660 /* For unhandled operations fall back to varying. */
3661 set_value_range_to_varying (vr);
3662 return;
3666 /* Extract range information from a unary expression CODE OP0 based on
3667 the range of its operand with resulting type TYPE.
3668 The resulting range is stored in *VR. */
3670 static void
3671 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3672 tree type, tree op0)
3674 value_range_t vr0 = VR_INITIALIZER;
3676 /* Get value ranges for the operand. For constant operands, create
3677 a new value range with the operand to simplify processing. */
3678 if (TREE_CODE (op0) == SSA_NAME)
3679 vr0 = *(get_value_range (op0));
3680 else if (is_gimple_min_invariant (op0))
3681 set_value_range_to_value (&vr0, op0, NULL);
3682 else
3683 set_value_range_to_varying (&vr0);
3685 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3689 /* Extract range information from a conditional expression STMT based on
3690 the ranges of each of its operands and the expression code. */
3692 static void
3693 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3695 tree op0, op1;
3696 value_range_t vr0 = VR_INITIALIZER;
3697 value_range_t vr1 = VR_INITIALIZER;
3699 /* Get value ranges for each operand. For constant operands, create
3700 a new value range with the operand to simplify processing. */
3701 op0 = gimple_assign_rhs2 (stmt);
3702 if (TREE_CODE (op0) == SSA_NAME)
3703 vr0 = *(get_value_range (op0));
3704 else if (is_gimple_min_invariant (op0))
3705 set_value_range_to_value (&vr0, op0, NULL);
3706 else
3707 set_value_range_to_varying (&vr0);
3709 op1 = gimple_assign_rhs3 (stmt);
3710 if (TREE_CODE (op1) == SSA_NAME)
3711 vr1 = *(get_value_range (op1));
3712 else if (is_gimple_min_invariant (op1))
3713 set_value_range_to_value (&vr1, op1, NULL);
3714 else
3715 set_value_range_to_varying (&vr1);
3717 /* The resulting value range is the union of the operand ranges */
3718 copy_value_range (vr, &vr0);
3719 vrp_meet (vr, &vr1);
3723 /* Extract range information from a comparison expression EXPR based
3724 on the range of its operand and the expression code. */
3726 static void
3727 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3728 tree type, tree op0, tree op1)
3730 bool sop = false;
3731 tree val;
3733 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3734 NULL);
3736 /* A disadvantage of using a special infinity as an overflow
3737 representation is that we lose the ability to record overflow
3738 when we don't have an infinity. So we have to ignore a result
3739 which relies on overflow. */
3741 if (val && !is_overflow_infinity (val) && !sop)
3743 /* Since this expression was found on the RHS of an assignment,
3744 its type may be different from _Bool. Convert VAL to EXPR's
3745 type. */
3746 val = fold_convert (type, val);
3747 if (is_gimple_min_invariant (val))
3748 set_value_range_to_value (vr, val, vr->equiv);
3749 else
3750 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3752 else
3753 /* The result of a comparison is always true or false. */
3754 set_value_range_to_truthvalue (vr, type);
3757 /* Try to derive a nonnegative or nonzero range out of STMT relying
3758 primarily on generic routines in fold in conjunction with range data.
3759 Store the result in *VR */
3761 static void
3762 extract_range_basic (value_range_t *vr, gimple stmt)
3764 bool sop = false;
3765 tree type = gimple_expr_type (stmt);
3767 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3769 tree fndecl = gimple_call_fndecl (stmt), arg;
3770 int mini, maxi, zerov = 0, prec;
3772 switch (DECL_FUNCTION_CODE (fndecl))
3774 case BUILT_IN_CONSTANT_P:
3775 /* If the call is __builtin_constant_p and the argument is a
3776 function parameter resolve it to false. This avoids bogus
3777 array bound warnings.
3778 ??? We could do this as early as inlining is finished. */
3779 arg = gimple_call_arg (stmt, 0);
3780 if (TREE_CODE (arg) == SSA_NAME
3781 && SSA_NAME_IS_DEFAULT_DEF (arg)
3782 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3784 set_value_range_to_null (vr, type);
3785 return;
3787 break;
3788 /* Both __builtin_ffs* and __builtin_popcount return
3789 [0, prec]. */
3790 CASE_INT_FN (BUILT_IN_FFS):
3791 CASE_INT_FN (BUILT_IN_POPCOUNT):
3792 arg = gimple_call_arg (stmt, 0);
3793 prec = TYPE_PRECISION (TREE_TYPE (arg));
3794 mini = 0;
3795 maxi = prec;
3796 if (TREE_CODE (arg) == SSA_NAME)
3798 value_range_t *vr0 = get_value_range (arg);
3799 /* If arg is non-zero, then ffs or popcount
3800 are non-zero. */
3801 if (((vr0->type == VR_RANGE
3802 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3803 || (vr0->type == VR_ANTI_RANGE
3804 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3805 && !is_overflow_infinity (vr0->min)
3806 && !is_overflow_infinity (vr0->max))
3807 mini = 1;
3808 /* If some high bits are known to be zero,
3809 we can decrease the maximum. */
3810 if (vr0->type == VR_RANGE
3811 && TREE_CODE (vr0->max) == INTEGER_CST
3812 && !operand_less_p (vr0->min,
3813 build_zero_cst (TREE_TYPE (vr0->min)))
3814 && !is_overflow_infinity (vr0->max))
3815 maxi = tree_floor_log2 (vr0->max) + 1;
3817 goto bitop_builtin;
3818 /* __builtin_parity* returns [0, 1]. */
3819 CASE_INT_FN (BUILT_IN_PARITY):
3820 mini = 0;
3821 maxi = 1;
3822 goto bitop_builtin;
3823 /* __builtin_c[lt]z* return [0, prec-1], except for
3824 when the argument is 0, but that is undefined behavior.
3825 On many targets where the CLZ RTL or optab value is defined
3826 for 0 the value is prec, so include that in the range
3827 by default. */
3828 CASE_INT_FN (BUILT_IN_CLZ):
3829 arg = gimple_call_arg (stmt, 0);
3830 prec = TYPE_PRECISION (TREE_TYPE (arg));
3831 mini = 0;
3832 maxi = prec;
3833 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3834 != CODE_FOR_nothing
3835 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3836 zerov)
3837 /* Handle only the single common value. */
3838 && zerov != prec)
3839 /* Magic value to give up, unless vr0 proves
3840 arg is non-zero. */
3841 mini = -2;
3842 if (TREE_CODE (arg) == SSA_NAME)
3844 value_range_t *vr0 = get_value_range (arg);
3845 /* From clz of VR_RANGE minimum we can compute
3846 result maximum. */
3847 if (vr0->type == VR_RANGE
3848 && TREE_CODE (vr0->min) == INTEGER_CST
3849 && !is_overflow_infinity (vr0->min))
3851 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3852 if (maxi != prec)
3853 mini = 0;
3855 else if (vr0->type == VR_ANTI_RANGE
3856 && integer_zerop (vr0->min)
3857 && !is_overflow_infinity (vr0->min))
3859 maxi = prec - 1;
3860 mini = 0;
3862 if (mini == -2)
3863 break;
3864 /* From clz of VR_RANGE maximum we can compute
3865 result minimum. */
3866 if (vr0->type == VR_RANGE
3867 && TREE_CODE (vr0->max) == INTEGER_CST
3868 && !is_overflow_infinity (vr0->max))
3870 mini = prec - 1 - tree_floor_log2 (vr0->max);
3871 if (mini == prec)
3872 break;
3875 if (mini == -2)
3876 break;
3877 goto bitop_builtin;
3878 /* __builtin_ctz* return [0, prec-1], except for
3879 when the argument is 0, but that is undefined behavior.
3880 If there is a ctz optab for this mode and
3881 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3882 otherwise just assume 0 won't be seen. */
3883 CASE_INT_FN (BUILT_IN_CTZ):
3884 arg = gimple_call_arg (stmt, 0);
3885 prec = TYPE_PRECISION (TREE_TYPE (arg));
3886 mini = 0;
3887 maxi = prec - 1;
3888 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3889 != CODE_FOR_nothing
3890 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3891 zerov))
3893 /* Handle only the two common values. */
3894 if (zerov == -1)
3895 mini = -1;
3896 else if (zerov == prec)
3897 maxi = prec;
3898 else
3899 /* Magic value to give up, unless vr0 proves
3900 arg is non-zero. */
3901 mini = -2;
3903 if (TREE_CODE (arg) == SSA_NAME)
3905 value_range_t *vr0 = get_value_range (arg);
3906 /* If arg is non-zero, then use [0, prec - 1]. */
3907 if (((vr0->type == VR_RANGE
3908 && integer_nonzerop (vr0->min))
3909 || (vr0->type == VR_ANTI_RANGE
3910 && integer_zerop (vr0->min)))
3911 && !is_overflow_infinity (vr0->min))
3913 mini = 0;
3914 maxi = prec - 1;
3916 /* If some high bits are known to be zero,
3917 we can decrease the result maximum. */
3918 if (vr0->type == VR_RANGE
3919 && TREE_CODE (vr0->max) == INTEGER_CST
3920 && !is_overflow_infinity (vr0->max))
3922 maxi = tree_floor_log2 (vr0->max);
3923 /* For vr0 [0, 0] give up. */
3924 if (maxi == -1)
3925 break;
3928 if (mini == -2)
3929 break;
3930 goto bitop_builtin;
3931 /* __builtin_clrsb* returns [0, prec-1]. */
3932 CASE_INT_FN (BUILT_IN_CLRSB):
3933 arg = gimple_call_arg (stmt, 0);
3934 prec = TYPE_PRECISION (TREE_TYPE (arg));
3935 mini = 0;
3936 maxi = prec - 1;
3937 goto bitop_builtin;
3938 bitop_builtin:
3939 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3940 build_int_cst (type, maxi), NULL);
3941 return;
3942 default:
3943 break;
3946 else if (is_gimple_call (stmt)
3947 && gimple_call_internal_p (stmt))
3949 enum tree_code subcode = ERROR_MARK;
3950 switch (gimple_call_internal_fn (stmt))
3952 case IFN_UBSAN_CHECK_ADD:
3953 subcode = PLUS_EXPR;
3954 break;
3955 case IFN_UBSAN_CHECK_SUB:
3956 subcode = MINUS_EXPR;
3957 break;
3958 case IFN_UBSAN_CHECK_MUL:
3959 subcode = MULT_EXPR;
3960 break;
3961 default:
3962 break;
3964 if (subcode != ERROR_MARK)
3966 bool saved_flag_wrapv = flag_wrapv;
3967 /* Pretend the arithmetics is wrapping. If there is
3968 any overflow, we'll complain, but will actually do
3969 wrapping operation. */
3970 flag_wrapv = 1;
3971 extract_range_from_binary_expr (vr, subcode, type,
3972 gimple_call_arg (stmt, 0),
3973 gimple_call_arg (stmt, 1));
3974 flag_wrapv = saved_flag_wrapv;
3976 /* If for both arguments vrp_valueize returned non-NULL,
3977 this should have been already folded and if not, it
3978 wasn't folded because of overflow. Avoid removing the
3979 UBSAN_CHECK_* calls in that case. */
3980 if (vr->type == VR_RANGE
3981 && (vr->min == vr->max
3982 || operand_equal_p (vr->min, vr->max, 0)))
3983 set_value_range_to_varying (vr);
3984 return;
3987 if (INTEGRAL_TYPE_P (type)
3988 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3989 set_value_range_to_nonnegative (vr, type,
3990 sop || stmt_overflow_infinity (stmt));
3991 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3992 && !sop)
3993 set_value_range_to_nonnull (vr, type);
3994 else
3995 set_value_range_to_varying (vr);
3999 /* Try to compute a useful range out of assignment STMT and store it
4000 in *VR. */
4002 static void
4003 extract_range_from_assignment (value_range_t *vr, gimple stmt)
4005 enum tree_code code = gimple_assign_rhs_code (stmt);
4007 if (code == ASSERT_EXPR)
4008 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4009 else if (code == SSA_NAME)
4010 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4011 else if (TREE_CODE_CLASS (code) == tcc_binary)
4012 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4013 gimple_expr_type (stmt),
4014 gimple_assign_rhs1 (stmt),
4015 gimple_assign_rhs2 (stmt));
4016 else if (TREE_CODE_CLASS (code) == tcc_unary)
4017 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4018 gimple_expr_type (stmt),
4019 gimple_assign_rhs1 (stmt));
4020 else if (code == COND_EXPR)
4021 extract_range_from_cond_expr (vr, stmt);
4022 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4023 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4024 gimple_expr_type (stmt),
4025 gimple_assign_rhs1 (stmt),
4026 gimple_assign_rhs2 (stmt));
4027 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4028 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4029 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4030 else
4031 set_value_range_to_varying (vr);
4033 if (vr->type == VR_VARYING)
4034 extract_range_basic (vr, stmt);
4037 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4038 would be profitable to adjust VR using scalar evolution information
4039 for VAR. If so, update VR with the new limits. */
4041 static void
4042 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
4043 gimple stmt, tree var)
4045 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4046 enum ev_direction dir;
4048 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4049 better opportunities than a regular range, but I'm not sure. */
4050 if (vr->type == VR_ANTI_RANGE)
4051 return;
4053 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4055 /* Like in PR19590, scev can return a constant function. */
4056 if (is_gimple_min_invariant (chrec))
4058 set_value_range_to_value (vr, chrec, vr->equiv);
4059 return;
4062 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4063 return;
4065 init = initial_condition_in_loop_num (chrec, loop->num);
4066 tem = op_with_constant_singleton_value_range (init);
4067 if (tem)
4068 init = tem;
4069 step = evolution_part_in_loop_num (chrec, loop->num);
4070 tem = op_with_constant_singleton_value_range (step);
4071 if (tem)
4072 step = tem;
4074 /* If STEP is symbolic, we can't know whether INIT will be the
4075 minimum or maximum value in the range. Also, unless INIT is
4076 a simple expression, compare_values and possibly other functions
4077 in tree-vrp won't be able to handle it. */
4078 if (step == NULL_TREE
4079 || !is_gimple_min_invariant (step)
4080 || !valid_value_p (init))
4081 return;
4083 dir = scev_direction (chrec);
4084 if (/* Do not adjust ranges if we do not know whether the iv increases
4085 or decreases, ... */
4086 dir == EV_DIR_UNKNOWN
4087 /* ... or if it may wrap. */
4088 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4089 true))
4090 return;
4092 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4093 negative_overflow_infinity and positive_overflow_infinity,
4094 because we have concluded that the loop probably does not
4095 wrap. */
4097 type = TREE_TYPE (var);
4098 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4099 tmin = lower_bound_in_type (type, type);
4100 else
4101 tmin = TYPE_MIN_VALUE (type);
4102 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4103 tmax = upper_bound_in_type (type, type);
4104 else
4105 tmax = TYPE_MAX_VALUE (type);
4107 /* Try to use estimated number of iterations for the loop to constrain the
4108 final value in the evolution. */
4109 if (TREE_CODE (step) == INTEGER_CST
4110 && is_gimple_val (init)
4111 && (TREE_CODE (init) != SSA_NAME
4112 || get_value_range (init)->type == VR_RANGE))
4114 widest_int nit;
4116 /* We are only entering here for loop header PHI nodes, so using
4117 the number of latch executions is the correct thing to use. */
4118 if (max_loop_iterations (loop, &nit))
4120 value_range_t maxvr = VR_INITIALIZER;
4121 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4122 bool overflow;
4124 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4125 &overflow);
4126 /* If the multiplication overflowed we can't do a meaningful
4127 adjustment. Likewise if the result doesn't fit in the type
4128 of the induction variable. For a signed type we have to
4129 check whether the result has the expected signedness which
4130 is that of the step as number of iterations is unsigned. */
4131 if (!overflow
4132 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4133 && (sgn == UNSIGNED
4134 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4136 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4137 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4138 TREE_TYPE (init), init, tem);
4139 /* Likewise if the addition did. */
4140 if (maxvr.type == VR_RANGE)
4142 tmin = maxvr.min;
4143 tmax = maxvr.max;
4149 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4151 min = tmin;
4152 max = tmax;
4154 /* For VARYING or UNDEFINED ranges, just about anything we get
4155 from scalar evolutions should be better. */
4157 if (dir == EV_DIR_DECREASES)
4158 max = init;
4159 else
4160 min = init;
4162 else if (vr->type == VR_RANGE)
4164 min = vr->min;
4165 max = vr->max;
4167 if (dir == EV_DIR_DECREASES)
4169 /* INIT is the maximum value. If INIT is lower than VR->MAX
4170 but no smaller than VR->MIN, set VR->MAX to INIT. */
4171 if (compare_values (init, max) == -1)
4172 max = init;
4174 /* According to the loop information, the variable does not
4175 overflow. If we think it does, probably because of an
4176 overflow due to arithmetic on a different INF value,
4177 reset now. */
4178 if (is_negative_overflow_infinity (min)
4179 || compare_values (min, tmin) == -1)
4180 min = tmin;
4183 else
4185 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4186 if (compare_values (init, min) == 1)
4187 min = init;
4189 if (is_positive_overflow_infinity (max)
4190 || compare_values (tmax, max) == -1)
4191 max = tmax;
4194 else
4195 return;
4197 /* If we just created an invalid range with the minimum
4198 greater than the maximum, we fail conservatively.
4199 This should happen only in unreachable
4200 parts of code, or for invalid programs. */
4201 if (compare_values (min, max) == 1
4202 || (is_negative_overflow_infinity (min)
4203 && is_positive_overflow_infinity (max)))
4204 return;
4206 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4210 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4212 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4213 all the values in the ranges.
4215 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4217 - Return NULL_TREE if it is not always possible to determine the
4218 value of the comparison.
4220 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4221 overflow infinity was used in the test. */
4224 static tree
4225 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4226 bool *strict_overflow_p)
4228 /* VARYING or UNDEFINED ranges cannot be compared. */
4229 if (vr0->type == VR_VARYING
4230 || vr0->type == VR_UNDEFINED
4231 || vr1->type == VR_VARYING
4232 || vr1->type == VR_UNDEFINED)
4233 return NULL_TREE;
4235 /* Anti-ranges need to be handled separately. */
4236 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4238 /* If both are anti-ranges, then we cannot compute any
4239 comparison. */
4240 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4241 return NULL_TREE;
4243 /* These comparisons are never statically computable. */
4244 if (comp == GT_EXPR
4245 || comp == GE_EXPR
4246 || comp == LT_EXPR
4247 || comp == LE_EXPR)
4248 return NULL_TREE;
4250 /* Equality can be computed only between a range and an
4251 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4252 if (vr0->type == VR_RANGE)
4254 /* To simplify processing, make VR0 the anti-range. */
4255 value_range_t *tmp = vr0;
4256 vr0 = vr1;
4257 vr1 = tmp;
4260 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4262 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4263 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4264 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4266 return NULL_TREE;
4269 if (!usable_range_p (vr0, strict_overflow_p)
4270 || !usable_range_p (vr1, strict_overflow_p))
4271 return NULL_TREE;
4273 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4274 operands around and change the comparison code. */
4275 if (comp == GT_EXPR || comp == GE_EXPR)
4277 value_range_t *tmp;
4278 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4279 tmp = vr0;
4280 vr0 = vr1;
4281 vr1 = tmp;
4284 if (comp == EQ_EXPR)
4286 /* Equality may only be computed if both ranges represent
4287 exactly one value. */
4288 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4289 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4291 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4292 strict_overflow_p);
4293 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4294 strict_overflow_p);
4295 if (cmp_min == 0 && cmp_max == 0)
4296 return boolean_true_node;
4297 else if (cmp_min != -2 && cmp_max != -2)
4298 return boolean_false_node;
4300 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4301 else if (compare_values_warnv (vr0->min, vr1->max,
4302 strict_overflow_p) == 1
4303 || compare_values_warnv (vr1->min, vr0->max,
4304 strict_overflow_p) == 1)
4305 return boolean_false_node;
4307 return NULL_TREE;
4309 else if (comp == NE_EXPR)
4311 int cmp1, cmp2;
4313 /* If VR0 is completely to the left or completely to the right
4314 of VR1, they are always different. Notice that we need to
4315 make sure that both comparisons yield similar results to
4316 avoid comparing values that cannot be compared at
4317 compile-time. */
4318 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4319 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4320 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4321 return boolean_true_node;
4323 /* If VR0 and VR1 represent a single value and are identical,
4324 return false. */
4325 else if (compare_values_warnv (vr0->min, vr0->max,
4326 strict_overflow_p) == 0
4327 && compare_values_warnv (vr1->min, vr1->max,
4328 strict_overflow_p) == 0
4329 && compare_values_warnv (vr0->min, vr1->min,
4330 strict_overflow_p) == 0
4331 && compare_values_warnv (vr0->max, vr1->max,
4332 strict_overflow_p) == 0)
4333 return boolean_false_node;
4335 /* Otherwise, they may or may not be different. */
4336 else
4337 return NULL_TREE;
4339 else if (comp == LT_EXPR || comp == LE_EXPR)
4341 int tst;
4343 /* If VR0 is to the left of VR1, return true. */
4344 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4345 if ((comp == LT_EXPR && tst == -1)
4346 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4348 if (overflow_infinity_range_p (vr0)
4349 || overflow_infinity_range_p (vr1))
4350 *strict_overflow_p = true;
4351 return boolean_true_node;
4354 /* If VR0 is to the right of VR1, return false. */
4355 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4356 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4357 || (comp == LE_EXPR && tst == 1))
4359 if (overflow_infinity_range_p (vr0)
4360 || overflow_infinity_range_p (vr1))
4361 *strict_overflow_p = true;
4362 return boolean_false_node;
4365 /* Otherwise, we don't know. */
4366 return NULL_TREE;
4369 gcc_unreachable ();
4373 /* Given a value range VR, a value VAL and a comparison code COMP, return
4374 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4375 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4376 always returns false. Return NULL_TREE if it is not always
4377 possible to determine the value of the comparison. Also set
4378 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4379 infinity was used in the test. */
4381 static tree
4382 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4383 bool *strict_overflow_p)
4385 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4386 return NULL_TREE;
4388 /* Anti-ranges need to be handled separately. */
4389 if (vr->type == VR_ANTI_RANGE)
4391 /* For anti-ranges, the only predicates that we can compute at
4392 compile time are equality and inequality. */
4393 if (comp == GT_EXPR
4394 || comp == GE_EXPR
4395 || comp == LT_EXPR
4396 || comp == LE_EXPR)
4397 return NULL_TREE;
4399 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4400 if (value_inside_range (val, vr->min, vr->max) == 1)
4401 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4403 return NULL_TREE;
4406 if (!usable_range_p (vr, strict_overflow_p))
4407 return NULL_TREE;
4409 if (comp == EQ_EXPR)
4411 /* EQ_EXPR may only be computed if VR represents exactly
4412 one value. */
4413 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4415 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4416 if (cmp == 0)
4417 return boolean_true_node;
4418 else if (cmp == -1 || cmp == 1 || cmp == 2)
4419 return boolean_false_node;
4421 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4422 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4423 return boolean_false_node;
4425 return NULL_TREE;
4427 else if (comp == NE_EXPR)
4429 /* If VAL is not inside VR, then they are always different. */
4430 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4431 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4432 return boolean_true_node;
4434 /* If VR represents exactly one value equal to VAL, then return
4435 false. */
4436 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4437 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4438 return boolean_false_node;
4440 /* Otherwise, they may or may not be different. */
4441 return NULL_TREE;
4443 else if (comp == LT_EXPR || comp == LE_EXPR)
4445 int tst;
4447 /* If VR is to the left of VAL, return true. */
4448 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4449 if ((comp == LT_EXPR && tst == -1)
4450 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4452 if (overflow_infinity_range_p (vr))
4453 *strict_overflow_p = true;
4454 return boolean_true_node;
4457 /* If VR is to the right of VAL, return false. */
4458 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4459 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4460 || (comp == LE_EXPR && tst == 1))
4462 if (overflow_infinity_range_p (vr))
4463 *strict_overflow_p = true;
4464 return boolean_false_node;
4467 /* Otherwise, we don't know. */
4468 return NULL_TREE;
4470 else if (comp == GT_EXPR || comp == GE_EXPR)
4472 int tst;
4474 /* If VR is to the right of VAL, return true. */
4475 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4476 if ((comp == GT_EXPR && tst == 1)
4477 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4479 if (overflow_infinity_range_p (vr))
4480 *strict_overflow_p = true;
4481 return boolean_true_node;
4484 /* If VR is to the left of VAL, return false. */
4485 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4486 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4487 || (comp == GE_EXPR && tst == -1))
4489 if (overflow_infinity_range_p (vr))
4490 *strict_overflow_p = true;
4491 return boolean_false_node;
4494 /* Otherwise, we don't know. */
4495 return NULL_TREE;
4498 gcc_unreachable ();
4502 /* Debugging dumps. */
4504 void dump_value_range (FILE *, value_range_t *);
4505 void debug_value_range (value_range_t *);
4506 void dump_all_value_ranges (FILE *);
4507 void debug_all_value_ranges (void);
4508 void dump_vr_equiv (FILE *, bitmap);
4509 void debug_vr_equiv (bitmap);
4512 /* Dump value range VR to FILE. */
4514 void
4515 dump_value_range (FILE *file, value_range_t *vr)
4517 if (vr == NULL)
4518 fprintf (file, "[]");
4519 else if (vr->type == VR_UNDEFINED)
4520 fprintf (file, "UNDEFINED");
4521 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4523 tree type = TREE_TYPE (vr->min);
4525 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4527 if (is_negative_overflow_infinity (vr->min))
4528 fprintf (file, "-INF(OVF)");
4529 else if (INTEGRAL_TYPE_P (type)
4530 && !TYPE_UNSIGNED (type)
4531 && vrp_val_is_min (vr->min))
4532 fprintf (file, "-INF");
4533 else
4534 print_generic_expr (file, vr->min, 0);
4536 fprintf (file, ", ");
4538 if (is_positive_overflow_infinity (vr->max))
4539 fprintf (file, "+INF(OVF)");
4540 else if (INTEGRAL_TYPE_P (type)
4541 && vrp_val_is_max (vr->max))
4542 fprintf (file, "+INF");
4543 else
4544 print_generic_expr (file, vr->max, 0);
4546 fprintf (file, "]");
4548 if (vr->equiv)
4550 bitmap_iterator bi;
4551 unsigned i, c = 0;
4553 fprintf (file, " EQUIVALENCES: { ");
4555 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4557 print_generic_expr (file, ssa_name (i), 0);
4558 fprintf (file, " ");
4559 c++;
4562 fprintf (file, "} (%u elements)", c);
4565 else if (vr->type == VR_VARYING)
4566 fprintf (file, "VARYING");
4567 else
4568 fprintf (file, "INVALID RANGE");
4572 /* Dump value range VR to stderr. */
4574 DEBUG_FUNCTION void
4575 debug_value_range (value_range_t *vr)
4577 dump_value_range (stderr, vr);
4578 fprintf (stderr, "\n");
4582 /* Dump value ranges of all SSA_NAMEs to FILE. */
4584 void
4585 dump_all_value_ranges (FILE *file)
4587 size_t i;
4589 for (i = 0; i < num_vr_values; i++)
4591 if (vr_value[i])
4593 print_generic_expr (file, ssa_name (i), 0);
4594 fprintf (file, ": ");
4595 dump_value_range (file, vr_value[i]);
4596 fprintf (file, "\n");
4600 fprintf (file, "\n");
4604 /* Dump all value ranges to stderr. */
4606 DEBUG_FUNCTION void
4607 debug_all_value_ranges (void)
4609 dump_all_value_ranges (stderr);
4613 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4614 create a new SSA name N and return the assertion assignment
4615 'N = ASSERT_EXPR <V, V OP W>'. */
4617 static gimple
4618 build_assert_expr_for (tree cond, tree v)
4620 tree a;
4621 gimple assertion;
4623 gcc_assert (TREE_CODE (v) == SSA_NAME
4624 && COMPARISON_CLASS_P (cond));
4626 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4627 assertion = gimple_build_assign (NULL_TREE, a);
4629 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4630 operand of the ASSERT_EXPR. Create it so the new name and the old one
4631 are registered in the replacement table so that we can fix the SSA web
4632 after adding all the ASSERT_EXPRs. */
4633 create_new_def_for (v, assertion, NULL);
4635 return assertion;
4639 /* Return false if EXPR is a predicate expression involving floating
4640 point values. */
4642 static inline bool
4643 fp_predicate (gimple stmt)
4645 GIMPLE_CHECK (stmt, GIMPLE_COND);
4647 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4650 /* If the range of values taken by OP can be inferred after STMT executes,
4651 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4652 describes the inferred range. Return true if a range could be
4653 inferred. */
4655 static bool
4656 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4658 *val_p = NULL_TREE;
4659 *comp_code_p = ERROR_MARK;
4661 /* Do not attempt to infer anything in names that flow through
4662 abnormal edges. */
4663 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4664 return false;
4666 /* Similarly, don't infer anything from statements that may throw
4667 exceptions. ??? Relax this requirement? */
4668 if (stmt_could_throw_p (stmt))
4669 return false;
4671 /* If STMT is the last statement of a basic block with no normal
4672 successors, there is no point inferring anything about any of its
4673 operands. We would not be able to find a proper insertion point
4674 for the assertion, anyway. */
4675 if (stmt_ends_bb_p (stmt))
4677 edge_iterator ei;
4678 edge e;
4680 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4681 if (!(e->flags & EDGE_ABNORMAL))
4682 break;
4683 if (e == NULL)
4684 return false;
4687 if (infer_nonnull_range (stmt, op, true, true))
4689 *val_p = build_int_cst (TREE_TYPE (op), 0);
4690 *comp_code_p = NE_EXPR;
4691 return true;
4694 return false;
4698 void dump_asserts_for (FILE *, tree);
4699 void debug_asserts_for (tree);
4700 void dump_all_asserts (FILE *);
4701 void debug_all_asserts (void);
4703 /* Dump all the registered assertions for NAME to FILE. */
4705 void
4706 dump_asserts_for (FILE *file, tree name)
4708 assert_locus_t loc;
4710 fprintf (file, "Assertions to be inserted for ");
4711 print_generic_expr (file, name, 0);
4712 fprintf (file, "\n");
4714 loc = asserts_for[SSA_NAME_VERSION (name)];
4715 while (loc)
4717 fprintf (file, "\t");
4718 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4719 fprintf (file, "\n\tBB #%d", loc->bb->index);
4720 if (loc->e)
4722 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4723 loc->e->dest->index);
4724 dump_edge_info (file, loc->e, dump_flags, 0);
4726 fprintf (file, "\n\tPREDICATE: ");
4727 print_generic_expr (file, name, 0);
4728 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4729 print_generic_expr (file, loc->val, 0);
4730 fprintf (file, "\n\n");
4731 loc = loc->next;
4734 fprintf (file, "\n");
4738 /* Dump all the registered assertions for NAME to stderr. */
4740 DEBUG_FUNCTION void
4741 debug_asserts_for (tree name)
4743 dump_asserts_for (stderr, name);
4747 /* Dump all the registered assertions for all the names to FILE. */
4749 void
4750 dump_all_asserts (FILE *file)
4752 unsigned i;
4753 bitmap_iterator bi;
4755 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4756 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4757 dump_asserts_for (file, ssa_name (i));
4758 fprintf (file, "\n");
4762 /* Dump all the registered assertions for all the names to stderr. */
4764 DEBUG_FUNCTION void
4765 debug_all_asserts (void)
4767 dump_all_asserts (stderr);
4771 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4772 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4773 E->DEST, then register this location as a possible insertion point
4774 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4776 BB, E and SI provide the exact insertion point for the new
4777 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4778 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4779 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4780 must not be NULL. */
4782 static void
4783 register_new_assert_for (tree name, tree expr,
4784 enum tree_code comp_code,
4785 tree val,
4786 basic_block bb,
4787 edge e,
4788 gimple_stmt_iterator si)
4790 assert_locus_t n, loc, last_loc;
4791 basic_block dest_bb;
4793 gcc_checking_assert (bb == NULL || e == NULL);
4795 if (e == NULL)
4796 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4797 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4799 /* Never build an assert comparing against an integer constant with
4800 TREE_OVERFLOW set. This confuses our undefined overflow warning
4801 machinery. */
4802 if (TREE_OVERFLOW_P (val))
4803 val = drop_tree_overflow (val);
4805 /* The new assertion A will be inserted at BB or E. We need to
4806 determine if the new location is dominated by a previously
4807 registered location for A. If we are doing an edge insertion,
4808 assume that A will be inserted at E->DEST. Note that this is not
4809 necessarily true.
4811 If E is a critical edge, it will be split. But even if E is
4812 split, the new block will dominate the same set of blocks that
4813 E->DEST dominates.
4815 The reverse, however, is not true, blocks dominated by E->DEST
4816 will not be dominated by the new block created to split E. So,
4817 if the insertion location is on a critical edge, we will not use
4818 the new location to move another assertion previously registered
4819 at a block dominated by E->DEST. */
4820 dest_bb = (bb) ? bb : e->dest;
4822 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4823 VAL at a block dominating DEST_BB, then we don't need to insert a new
4824 one. Similarly, if the same assertion already exists at a block
4825 dominated by DEST_BB and the new location is not on a critical
4826 edge, then update the existing location for the assertion (i.e.,
4827 move the assertion up in the dominance tree).
4829 Note, this is implemented as a simple linked list because there
4830 should not be more than a handful of assertions registered per
4831 name. If this becomes a performance problem, a table hashed by
4832 COMP_CODE and VAL could be implemented. */
4833 loc = asserts_for[SSA_NAME_VERSION (name)];
4834 last_loc = loc;
4835 while (loc)
4837 if (loc->comp_code == comp_code
4838 && (loc->val == val
4839 || operand_equal_p (loc->val, val, 0))
4840 && (loc->expr == expr
4841 || operand_equal_p (loc->expr, expr, 0)))
4843 /* If E is not a critical edge and DEST_BB
4844 dominates the existing location for the assertion, move
4845 the assertion up in the dominance tree by updating its
4846 location information. */
4847 if ((e == NULL || !EDGE_CRITICAL_P (e))
4848 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4850 loc->bb = dest_bb;
4851 loc->e = e;
4852 loc->si = si;
4853 return;
4857 /* Update the last node of the list and move to the next one. */
4858 last_loc = loc;
4859 loc = loc->next;
4862 /* If we didn't find an assertion already registered for
4863 NAME COMP_CODE VAL, add a new one at the end of the list of
4864 assertions associated with NAME. */
4865 n = XNEW (struct assert_locus_d);
4866 n->bb = dest_bb;
4867 n->e = e;
4868 n->si = si;
4869 n->comp_code = comp_code;
4870 n->val = val;
4871 n->expr = expr;
4872 n->next = NULL;
4874 if (last_loc)
4875 last_loc->next = n;
4876 else
4877 asserts_for[SSA_NAME_VERSION (name)] = n;
4879 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4882 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4883 Extract a suitable test code and value and store them into *CODE_P and
4884 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4886 If no extraction was possible, return FALSE, otherwise return TRUE.
4888 If INVERT is true, then we invert the result stored into *CODE_P. */
4890 static bool
4891 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4892 tree cond_op0, tree cond_op1,
4893 bool invert, enum tree_code *code_p,
4894 tree *val_p)
4896 enum tree_code comp_code;
4897 tree val;
4899 /* Otherwise, we have a comparison of the form NAME COMP VAL
4900 or VAL COMP NAME. */
4901 if (name == cond_op1)
4903 /* If the predicate is of the form VAL COMP NAME, flip
4904 COMP around because we need to register NAME as the
4905 first operand in the predicate. */
4906 comp_code = swap_tree_comparison (cond_code);
4907 val = cond_op0;
4909 else
4911 /* The comparison is of the form NAME COMP VAL, so the
4912 comparison code remains unchanged. */
4913 comp_code = cond_code;
4914 val = cond_op1;
4917 /* Invert the comparison code as necessary. */
4918 if (invert)
4919 comp_code = invert_tree_comparison (comp_code, 0);
4921 /* VRP does not handle float types. */
4922 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4923 return false;
4925 /* Do not register always-false predicates.
4926 FIXME: this works around a limitation in fold() when dealing with
4927 enumerations. Given 'enum { N1, N2 } x;', fold will not
4928 fold 'if (x > N2)' to 'if (0)'. */
4929 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4930 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4932 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4933 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4935 if (comp_code == GT_EXPR
4936 && (!max
4937 || compare_values (val, max) == 0))
4938 return false;
4940 if (comp_code == LT_EXPR
4941 && (!min
4942 || compare_values (val, min) == 0))
4943 return false;
4945 *code_p = comp_code;
4946 *val_p = val;
4947 return true;
4950 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4951 (otherwise return VAL). VAL and MASK must be zero-extended for
4952 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4953 (to transform signed values into unsigned) and at the end xor
4954 SGNBIT back. */
4956 static wide_int
4957 masked_increment (const wide_int &val_in, const wide_int &mask,
4958 const wide_int &sgnbit, unsigned int prec)
4960 wide_int bit = wi::one (prec), res;
4961 unsigned int i;
4963 wide_int val = val_in ^ sgnbit;
4964 for (i = 0; i < prec; i++, bit += bit)
4966 res = mask;
4967 if ((res & bit) == 0)
4968 continue;
4969 res = bit - 1;
4970 res = (val + bit).and_not (res);
4971 res &= mask;
4972 if (wi::gtu_p (res, val))
4973 return res ^ sgnbit;
4975 return val ^ sgnbit;
4978 /* Try to register an edge assertion for SSA name NAME on edge E for
4979 the condition COND contributing to the conditional jump pointed to by BSI.
4980 Invert the condition COND if INVERT is true.
4981 Return true if an assertion for NAME could be registered. */
4983 static bool
4984 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4985 enum tree_code cond_code,
4986 tree cond_op0, tree cond_op1, bool invert)
4988 tree val;
4989 enum tree_code comp_code;
4990 bool retval = false;
4992 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4993 cond_op0,
4994 cond_op1,
4995 invert, &comp_code, &val))
4996 return false;
4998 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4999 reachable from E. */
5000 if (live_on_edge (e, name)
5001 && !has_single_use (name))
5003 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5004 retval = true;
5007 /* In the case of NAME <= CST and NAME being defined as
5008 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5009 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5010 This catches range and anti-range tests. */
5011 if ((comp_code == LE_EXPR
5012 || comp_code == GT_EXPR)
5013 && TREE_CODE (val) == INTEGER_CST
5014 && TYPE_UNSIGNED (TREE_TYPE (val)))
5016 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5017 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5019 /* Extract CST2 from the (optional) addition. */
5020 if (is_gimple_assign (def_stmt)
5021 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5023 name2 = gimple_assign_rhs1 (def_stmt);
5024 cst2 = gimple_assign_rhs2 (def_stmt);
5025 if (TREE_CODE (name2) == SSA_NAME
5026 && TREE_CODE (cst2) == INTEGER_CST)
5027 def_stmt = SSA_NAME_DEF_STMT (name2);
5030 /* Extract NAME2 from the (optional) sign-changing cast. */
5031 if (gimple_assign_cast_p (def_stmt))
5033 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5034 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5035 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5036 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5037 name3 = gimple_assign_rhs1 (def_stmt);
5040 /* If name3 is used later, create an ASSERT_EXPR for it. */
5041 if (name3 != NULL_TREE
5042 && TREE_CODE (name3) == SSA_NAME
5043 && (cst2 == NULL_TREE
5044 || TREE_CODE (cst2) == INTEGER_CST)
5045 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5046 && live_on_edge (e, name3)
5047 && !has_single_use (name3))
5049 tree tmp;
5051 /* Build an expression for the range test. */
5052 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5053 if (cst2 != NULL_TREE)
5054 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5056 if (dump_file)
5058 fprintf (dump_file, "Adding assert for ");
5059 print_generic_expr (dump_file, name3, 0);
5060 fprintf (dump_file, " from ");
5061 print_generic_expr (dump_file, tmp, 0);
5062 fprintf (dump_file, "\n");
5065 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5067 retval = true;
5070 /* If name2 is used later, create an ASSERT_EXPR for it. */
5071 if (name2 != NULL_TREE
5072 && TREE_CODE (name2) == SSA_NAME
5073 && TREE_CODE (cst2) == INTEGER_CST
5074 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5075 && live_on_edge (e, name2)
5076 && !has_single_use (name2))
5078 tree tmp;
5080 /* Build an expression for the range test. */
5081 tmp = name2;
5082 if (TREE_TYPE (name) != TREE_TYPE (name2))
5083 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5084 if (cst2 != NULL_TREE)
5085 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5087 if (dump_file)
5089 fprintf (dump_file, "Adding assert for ");
5090 print_generic_expr (dump_file, name2, 0);
5091 fprintf (dump_file, " from ");
5092 print_generic_expr (dump_file, tmp, 0);
5093 fprintf (dump_file, "\n");
5096 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5098 retval = true;
5102 /* In the case of post-in/decrement tests like if (i++) ... and uses
5103 of the in/decremented value on the edge the extra name we want to
5104 assert for is not on the def chain of the name compared. Instead
5105 it is in the set of use stmts. */
5106 if ((comp_code == NE_EXPR
5107 || comp_code == EQ_EXPR)
5108 && TREE_CODE (val) == INTEGER_CST)
5110 imm_use_iterator ui;
5111 gimple use_stmt;
5112 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5114 /* Cut off to use-stmts that are in the predecessor. */
5115 if (gimple_bb (use_stmt) != e->src)
5116 continue;
5118 if (!is_gimple_assign (use_stmt))
5119 continue;
5121 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5122 if (code != PLUS_EXPR
5123 && code != MINUS_EXPR)
5124 continue;
5126 tree cst = gimple_assign_rhs2 (use_stmt);
5127 if (TREE_CODE (cst) != INTEGER_CST)
5128 continue;
5130 tree name2 = gimple_assign_lhs (use_stmt);
5131 if (live_on_edge (e, name2))
5133 cst = int_const_binop (code, val, cst);
5134 register_new_assert_for (name2, name2, comp_code, cst,
5135 NULL, e, bsi);
5136 retval = true;
5141 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5142 && TREE_CODE (val) == INTEGER_CST)
5144 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5145 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5146 tree val2 = NULL_TREE;
5147 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5148 wide_int mask = wi::zero (prec);
5149 unsigned int nprec = prec;
5150 enum tree_code rhs_code = ERROR_MARK;
5152 if (is_gimple_assign (def_stmt))
5153 rhs_code = gimple_assign_rhs_code (def_stmt);
5155 /* Add asserts for NAME cmp CST and NAME being defined
5156 as NAME = (int) NAME2. */
5157 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5158 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5159 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5160 && gimple_assign_cast_p (def_stmt))
5162 name2 = gimple_assign_rhs1 (def_stmt);
5163 if (CONVERT_EXPR_CODE_P (rhs_code)
5164 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5165 && TYPE_UNSIGNED (TREE_TYPE (name2))
5166 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5167 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5168 || !tree_int_cst_equal (val,
5169 TYPE_MIN_VALUE (TREE_TYPE (val))))
5170 && live_on_edge (e, name2)
5171 && !has_single_use (name2))
5173 tree tmp, cst;
5174 enum tree_code new_comp_code = comp_code;
5176 cst = fold_convert (TREE_TYPE (name2),
5177 TYPE_MIN_VALUE (TREE_TYPE (val)));
5178 /* Build an expression for the range test. */
5179 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5180 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5181 fold_convert (TREE_TYPE (name2), val));
5182 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5184 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5185 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5186 build_int_cst (TREE_TYPE (name2), 1));
5189 if (dump_file)
5191 fprintf (dump_file, "Adding assert for ");
5192 print_generic_expr (dump_file, name2, 0);
5193 fprintf (dump_file, " from ");
5194 print_generic_expr (dump_file, tmp, 0);
5195 fprintf (dump_file, "\n");
5198 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5199 e, bsi);
5201 retval = true;
5205 /* Add asserts for NAME cmp CST and NAME being defined as
5206 NAME = NAME2 >> CST2.
5208 Extract CST2 from the right shift. */
5209 if (rhs_code == RSHIFT_EXPR)
5211 name2 = gimple_assign_rhs1 (def_stmt);
5212 cst2 = gimple_assign_rhs2 (def_stmt);
5213 if (TREE_CODE (name2) == SSA_NAME
5214 && tree_fits_uhwi_p (cst2)
5215 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5216 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5217 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5218 && live_on_edge (e, name2)
5219 && !has_single_use (name2))
5221 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5222 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5225 if (val2 != NULL_TREE
5226 && TREE_CODE (val2) == INTEGER_CST
5227 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5228 TREE_TYPE (val),
5229 val2, cst2), val))
5231 enum tree_code new_comp_code = comp_code;
5232 tree tmp, new_val;
5234 tmp = name2;
5235 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5237 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5239 tree type = build_nonstandard_integer_type (prec, 1);
5240 tmp = build1 (NOP_EXPR, type, name2);
5241 val2 = fold_convert (type, val2);
5243 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5244 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5245 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5247 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5249 wide_int minval
5250 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5251 new_val = val2;
5252 if (minval == new_val)
5253 new_val = NULL_TREE;
5255 else
5257 wide_int maxval
5258 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5259 mask |= val2;
5260 if (mask == maxval)
5261 new_val = NULL_TREE;
5262 else
5263 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5266 if (new_val)
5268 if (dump_file)
5270 fprintf (dump_file, "Adding assert for ");
5271 print_generic_expr (dump_file, name2, 0);
5272 fprintf (dump_file, " from ");
5273 print_generic_expr (dump_file, tmp, 0);
5274 fprintf (dump_file, "\n");
5277 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5278 NULL, e, bsi);
5279 retval = true;
5283 /* Add asserts for NAME cmp CST and NAME being defined as
5284 NAME = NAME2 & CST2.
5286 Extract CST2 from the and.
5288 Also handle
5289 NAME = (unsigned) NAME2;
5290 casts where NAME's type is unsigned and has smaller precision
5291 than NAME2's type as if it was NAME = NAME2 & MASK. */
5292 names[0] = NULL_TREE;
5293 names[1] = NULL_TREE;
5294 cst2 = NULL_TREE;
5295 if (rhs_code == BIT_AND_EXPR
5296 || (CONVERT_EXPR_CODE_P (rhs_code)
5297 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5298 && TYPE_UNSIGNED (TREE_TYPE (val))
5299 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5300 > prec
5301 && !retval))
5303 name2 = gimple_assign_rhs1 (def_stmt);
5304 if (rhs_code == BIT_AND_EXPR)
5305 cst2 = gimple_assign_rhs2 (def_stmt);
5306 else
5308 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5309 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5311 if (TREE_CODE (name2) == SSA_NAME
5312 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5313 && TREE_CODE (cst2) == INTEGER_CST
5314 && !integer_zerop (cst2)
5315 && (nprec > 1
5316 || TYPE_UNSIGNED (TREE_TYPE (val))))
5318 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5319 if (gimple_assign_cast_p (def_stmt2))
5321 names[1] = gimple_assign_rhs1 (def_stmt2);
5322 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5323 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5324 || (TYPE_PRECISION (TREE_TYPE (name2))
5325 != TYPE_PRECISION (TREE_TYPE (names[1])))
5326 || !live_on_edge (e, names[1])
5327 || has_single_use (names[1]))
5328 names[1] = NULL_TREE;
5330 if (live_on_edge (e, name2)
5331 && !has_single_use (name2))
5332 names[0] = name2;
5335 if (names[0] || names[1])
5337 wide_int minv, maxv, valv, cst2v;
5338 wide_int tem, sgnbit;
5339 bool valid_p = false, valn, cst2n;
5340 enum tree_code ccode = comp_code;
5342 valv = wide_int::from (val, nprec, UNSIGNED);
5343 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5344 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5345 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5346 /* If CST2 doesn't have most significant bit set,
5347 but VAL is negative, we have comparison like
5348 if ((x & 0x123) > -4) (always true). Just give up. */
5349 if (!cst2n && valn)
5350 ccode = ERROR_MARK;
5351 if (cst2n)
5352 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5353 else
5354 sgnbit = wi::zero (nprec);
5355 minv = valv & cst2v;
5356 switch (ccode)
5358 case EQ_EXPR:
5359 /* Minimum unsigned value for equality is VAL & CST2
5360 (should be equal to VAL, otherwise we probably should
5361 have folded the comparison into false) and
5362 maximum unsigned value is VAL | ~CST2. */
5363 maxv = valv | ~cst2v;
5364 valid_p = true;
5365 break;
5367 case NE_EXPR:
5368 tem = valv | ~cst2v;
5369 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5370 if (valv == 0)
5372 cst2n = false;
5373 sgnbit = wi::zero (nprec);
5374 goto gt_expr;
5376 /* If (VAL | ~CST2) is all ones, handle it as
5377 (X & CST2) < VAL. */
5378 if (tem == -1)
5380 cst2n = false;
5381 valn = false;
5382 sgnbit = wi::zero (nprec);
5383 goto lt_expr;
5385 if (!cst2n && wi::neg_p (cst2v))
5386 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5387 if (sgnbit != 0)
5389 if (valv == sgnbit)
5391 cst2n = true;
5392 valn = true;
5393 goto gt_expr;
5395 if (tem == wi::mask (nprec - 1, false, nprec))
5397 cst2n = true;
5398 goto lt_expr;
5400 if (!cst2n)
5401 sgnbit = wi::zero (nprec);
5403 break;
5405 case GE_EXPR:
5406 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5407 is VAL and maximum unsigned value is ~0. For signed
5408 comparison, if CST2 doesn't have most significant bit
5409 set, handle it similarly. If CST2 has MSB set,
5410 the minimum is the same, and maximum is ~0U/2. */
5411 if (minv != valv)
5413 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5414 VAL. */
5415 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5416 if (minv == valv)
5417 break;
5419 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5420 valid_p = true;
5421 break;
5423 case GT_EXPR:
5424 gt_expr:
5425 /* Find out smallest MINV where MINV > VAL
5426 && (MINV & CST2) == MINV, if any. If VAL is signed and
5427 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5428 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5429 if (minv == valv)
5430 break;
5431 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5432 valid_p = true;
5433 break;
5435 case LE_EXPR:
5436 /* Minimum unsigned value for <= is 0 and maximum
5437 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5438 Otherwise, find smallest VAL2 where VAL2 > VAL
5439 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5440 as maximum.
5441 For signed comparison, if CST2 doesn't have most
5442 significant bit set, handle it similarly. If CST2 has
5443 MSB set, the maximum is the same and minimum is INT_MIN. */
5444 if (minv == valv)
5445 maxv = valv;
5446 else
5448 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5449 if (maxv == valv)
5450 break;
5451 maxv -= 1;
5453 maxv |= ~cst2v;
5454 minv = sgnbit;
5455 valid_p = true;
5456 break;
5458 case LT_EXPR:
5459 lt_expr:
5460 /* Minimum unsigned value for < is 0 and maximum
5461 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5462 Otherwise, find smallest VAL2 where VAL2 > VAL
5463 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5464 as maximum.
5465 For signed comparison, if CST2 doesn't have most
5466 significant bit set, handle it similarly. If CST2 has
5467 MSB set, the maximum is the same and minimum is INT_MIN. */
5468 if (minv == valv)
5470 if (valv == sgnbit)
5471 break;
5472 maxv = valv;
5474 else
5476 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5477 if (maxv == valv)
5478 break;
5480 maxv -= 1;
5481 maxv |= ~cst2v;
5482 minv = sgnbit;
5483 valid_p = true;
5484 break;
5486 default:
5487 break;
5489 if (valid_p
5490 && (maxv - minv) != -1)
5492 tree tmp, new_val, type;
5493 int i;
5495 for (i = 0; i < 2; i++)
5496 if (names[i])
5498 wide_int maxv2 = maxv;
5499 tmp = names[i];
5500 type = TREE_TYPE (names[i]);
5501 if (!TYPE_UNSIGNED (type))
5503 type = build_nonstandard_integer_type (nprec, 1);
5504 tmp = build1 (NOP_EXPR, type, names[i]);
5506 if (minv != 0)
5508 tmp = build2 (PLUS_EXPR, type, tmp,
5509 wide_int_to_tree (type, -minv));
5510 maxv2 = maxv - minv;
5512 new_val = wide_int_to_tree (type, maxv2);
5514 if (dump_file)
5516 fprintf (dump_file, "Adding assert for ");
5517 print_generic_expr (dump_file, names[i], 0);
5518 fprintf (dump_file, " from ");
5519 print_generic_expr (dump_file, tmp, 0);
5520 fprintf (dump_file, "\n");
5523 register_new_assert_for (names[i], tmp, LE_EXPR,
5524 new_val, NULL, e, bsi);
5525 retval = true;
5531 return retval;
5534 /* OP is an operand of a truth value expression which is known to have
5535 a particular value. Register any asserts for OP and for any
5536 operands in OP's defining statement.
5538 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5539 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5541 static bool
5542 register_edge_assert_for_1 (tree op, enum tree_code code,
5543 edge e, gimple_stmt_iterator bsi)
5545 bool retval = false;
5546 gimple op_def;
5547 tree val;
5548 enum tree_code rhs_code;
5550 /* We only care about SSA_NAMEs. */
5551 if (TREE_CODE (op) != SSA_NAME)
5552 return false;
5554 /* We know that OP will have a zero or nonzero value. If OP is used
5555 more than once go ahead and register an assert for OP. */
5556 if (live_on_edge (e, op)
5557 && !has_single_use (op))
5559 val = build_int_cst (TREE_TYPE (op), 0);
5560 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5561 retval = true;
5564 /* Now look at how OP is set. If it's set from a comparison,
5565 a truth operation or some bit operations, then we may be able
5566 to register information about the operands of that assignment. */
5567 op_def = SSA_NAME_DEF_STMT (op);
5568 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5569 return retval;
5571 rhs_code = gimple_assign_rhs_code (op_def);
5573 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5575 bool invert = (code == EQ_EXPR ? true : false);
5576 tree op0 = gimple_assign_rhs1 (op_def);
5577 tree op1 = gimple_assign_rhs2 (op_def);
5579 if (TREE_CODE (op0) == SSA_NAME)
5580 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5581 invert);
5582 if (TREE_CODE (op1) == SSA_NAME)
5583 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5584 invert);
5586 else if ((code == NE_EXPR
5587 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5588 || (code == EQ_EXPR
5589 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5591 /* Recurse on each operand. */
5592 tree op0 = gimple_assign_rhs1 (op_def);
5593 tree op1 = gimple_assign_rhs2 (op_def);
5594 if (TREE_CODE (op0) == SSA_NAME
5595 && has_single_use (op0))
5596 retval |= register_edge_assert_for_1 (op0, code, e, bsi);
5597 if (TREE_CODE (op1) == SSA_NAME
5598 && has_single_use (op1))
5599 retval |= register_edge_assert_for_1 (op1, code, e, bsi);
5601 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5602 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5604 /* Recurse, flipping CODE. */
5605 code = invert_tree_comparison (code, false);
5606 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5607 code, e, bsi);
5609 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5611 /* Recurse through the copy. */
5612 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5613 code, e, bsi);
5615 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5617 /* Recurse through the type conversion, unless it is a narrowing
5618 conversion or conversion from non-integral type. */
5619 tree rhs = gimple_assign_rhs1 (op_def);
5620 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5621 && (TYPE_PRECISION (TREE_TYPE (rhs))
5622 <= TYPE_PRECISION (TREE_TYPE (op))))
5623 retval |= register_edge_assert_for_1 (rhs, code, e, bsi);
5626 return retval;
5629 /* Try to register an edge assertion for SSA name NAME on edge E for
5630 the condition COND contributing to the conditional jump pointed to by SI.
5631 Return true if an assertion for NAME could be registered. */
5633 static bool
5634 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5635 enum tree_code cond_code, tree cond_op0,
5636 tree cond_op1)
5638 tree val;
5639 enum tree_code comp_code;
5640 bool retval = false;
5641 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5643 /* Do not attempt to infer anything in names that flow through
5644 abnormal edges. */
5645 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5646 return false;
5648 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5649 cond_op0, cond_op1,
5650 is_else_edge,
5651 &comp_code, &val))
5652 return false;
5654 /* Register ASSERT_EXPRs for name. */
5655 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5656 cond_op1, is_else_edge);
5659 /* If COND is effectively an equality test of an SSA_NAME against
5660 the value zero or one, then we may be able to assert values
5661 for SSA_NAMEs which flow into COND. */
5663 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5664 statement of NAME we can assert both operands of the BIT_AND_EXPR
5665 have nonzero value. */
5666 if (((comp_code == EQ_EXPR && integer_onep (val))
5667 || (comp_code == NE_EXPR && integer_zerop (val))))
5669 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5671 if (is_gimple_assign (def_stmt)
5672 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5674 tree op0 = gimple_assign_rhs1 (def_stmt);
5675 tree op1 = gimple_assign_rhs2 (def_stmt);
5676 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5677 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5681 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5682 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5683 have zero value. */
5684 if (((comp_code == EQ_EXPR && integer_zerop (val))
5685 || (comp_code == NE_EXPR && integer_onep (val))))
5687 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5689 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5690 necessarily zero value, or if type-precision is one. */
5691 if (is_gimple_assign (def_stmt)
5692 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5693 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5694 || comp_code == EQ_EXPR)))
5696 tree op0 = gimple_assign_rhs1 (def_stmt);
5697 tree op1 = gimple_assign_rhs2 (def_stmt);
5698 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5699 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5703 return retval;
5707 /* Determine whether the outgoing edges of BB should receive an
5708 ASSERT_EXPR for each of the operands of BB's LAST statement.
5709 The last statement of BB must be a COND_EXPR.
5711 If any of the sub-graphs rooted at BB have an interesting use of
5712 the predicate operands, an assert location node is added to the
5713 list of assertions for the corresponding operands. */
5715 static bool
5716 find_conditional_asserts (basic_block bb, gimple last)
5718 bool need_assert;
5719 gimple_stmt_iterator bsi;
5720 tree op;
5721 edge_iterator ei;
5722 edge e;
5723 ssa_op_iter iter;
5725 need_assert = false;
5726 bsi = gsi_for_stmt (last);
5728 /* Look for uses of the operands in each of the sub-graphs
5729 rooted at BB. We need to check each of the outgoing edges
5730 separately, so that we know what kind of ASSERT_EXPR to
5731 insert. */
5732 FOR_EACH_EDGE (e, ei, bb->succs)
5734 if (e->dest == bb)
5735 continue;
5737 /* Register the necessary assertions for each operand in the
5738 conditional predicate. */
5739 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5741 need_assert |= register_edge_assert_for (op, e, bsi,
5742 gimple_cond_code (last),
5743 gimple_cond_lhs (last),
5744 gimple_cond_rhs (last));
5748 return need_assert;
5751 struct case_info
5753 tree expr;
5754 basic_block bb;
5757 /* Compare two case labels sorting first by the destination bb index
5758 and then by the case value. */
5760 static int
5761 compare_case_labels (const void *p1, const void *p2)
5763 const struct case_info *ci1 = (const struct case_info *) p1;
5764 const struct case_info *ci2 = (const struct case_info *) p2;
5765 int idx1 = ci1->bb->index;
5766 int idx2 = ci2->bb->index;
5768 if (idx1 < idx2)
5769 return -1;
5770 else if (idx1 == idx2)
5772 /* Make sure the default label is first in a group. */
5773 if (!CASE_LOW (ci1->expr))
5774 return -1;
5775 else if (!CASE_LOW (ci2->expr))
5776 return 1;
5777 else
5778 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5779 CASE_LOW (ci2->expr));
5781 else
5782 return 1;
5785 /* Determine whether the outgoing edges of BB should receive an
5786 ASSERT_EXPR for each of the operands of BB's LAST statement.
5787 The last statement of BB must be a SWITCH_EXPR.
5789 If any of the sub-graphs rooted at BB have an interesting use of
5790 the predicate operands, an assert location node is added to the
5791 list of assertions for the corresponding operands. */
5793 static bool
5794 find_switch_asserts (basic_block bb, gimple last)
5796 bool need_assert;
5797 gimple_stmt_iterator bsi;
5798 tree op;
5799 edge e;
5800 struct case_info *ci;
5801 size_t n = gimple_switch_num_labels (last);
5802 #if GCC_VERSION >= 4000
5803 unsigned int idx;
5804 #else
5805 /* Work around GCC 3.4 bug (PR 37086). */
5806 volatile unsigned int idx;
5807 #endif
5809 need_assert = false;
5810 bsi = gsi_for_stmt (last);
5811 op = gimple_switch_index (last);
5812 if (TREE_CODE (op) != SSA_NAME)
5813 return false;
5815 /* Build a vector of case labels sorted by destination label. */
5816 ci = XNEWVEC (struct case_info, n);
5817 for (idx = 0; idx < n; ++idx)
5819 ci[idx].expr = gimple_switch_label (last, idx);
5820 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5822 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5824 for (idx = 0; idx < n; ++idx)
5826 tree min, max;
5827 tree cl = ci[idx].expr;
5828 basic_block cbb = ci[idx].bb;
5830 min = CASE_LOW (cl);
5831 max = CASE_HIGH (cl);
5833 /* If there are multiple case labels with the same destination
5834 we need to combine them to a single value range for the edge. */
5835 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5837 /* Skip labels until the last of the group. */
5838 do {
5839 ++idx;
5840 } while (idx < n && cbb == ci[idx].bb);
5841 --idx;
5843 /* Pick up the maximum of the case label range. */
5844 if (CASE_HIGH (ci[idx].expr))
5845 max = CASE_HIGH (ci[idx].expr);
5846 else
5847 max = CASE_LOW (ci[idx].expr);
5850 /* Nothing to do if the range includes the default label until we
5851 can register anti-ranges. */
5852 if (min == NULL_TREE)
5853 continue;
5855 /* Find the edge to register the assert expr on. */
5856 e = find_edge (bb, cbb);
5858 /* Register the necessary assertions for the operand in the
5859 SWITCH_EXPR. */
5860 need_assert |= register_edge_assert_for (op, e, bsi,
5861 max ? GE_EXPR : EQ_EXPR,
5863 fold_convert (TREE_TYPE (op),
5864 min));
5865 if (max)
5867 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5869 fold_convert (TREE_TYPE (op),
5870 max));
5874 XDELETEVEC (ci);
5875 return need_assert;
5879 /* Traverse all the statements in block BB looking for statements that
5880 may generate useful assertions for the SSA names in their operand.
5881 If a statement produces a useful assertion A for name N_i, then the
5882 list of assertions already generated for N_i is scanned to
5883 determine if A is actually needed.
5885 If N_i already had the assertion A at a location dominating the
5886 current location, then nothing needs to be done. Otherwise, the
5887 new location for A is recorded instead.
5889 1- For every statement S in BB, all the variables used by S are
5890 added to bitmap FOUND_IN_SUBGRAPH.
5892 2- If statement S uses an operand N in a way that exposes a known
5893 value range for N, then if N was not already generated by an
5894 ASSERT_EXPR, create a new assert location for N. For instance,
5895 if N is a pointer and the statement dereferences it, we can
5896 assume that N is not NULL.
5898 3- COND_EXPRs are a special case of #2. We can derive range
5899 information from the predicate but need to insert different
5900 ASSERT_EXPRs for each of the sub-graphs rooted at the
5901 conditional block. If the last statement of BB is a conditional
5902 expression of the form 'X op Y', then
5904 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5906 b) If the conditional is the only entry point to the sub-graph
5907 corresponding to the THEN_CLAUSE, recurse into it. On
5908 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5909 an ASSERT_EXPR is added for the corresponding variable.
5911 c) Repeat step (b) on the ELSE_CLAUSE.
5913 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5915 For instance,
5917 if (a == 9)
5918 b = a;
5919 else
5920 b = c + 1;
5922 In this case, an assertion on the THEN clause is useful to
5923 determine that 'a' is always 9 on that edge. However, an assertion
5924 on the ELSE clause would be unnecessary.
5926 4- If BB does not end in a conditional expression, then we recurse
5927 into BB's dominator children.
5929 At the end of the recursive traversal, every SSA name will have a
5930 list of locations where ASSERT_EXPRs should be added. When a new
5931 location for name N is found, it is registered by calling
5932 register_new_assert_for. That function keeps track of all the
5933 registered assertions to prevent adding unnecessary assertions.
5934 For instance, if a pointer P_4 is dereferenced more than once in a
5935 dominator tree, only the location dominating all the dereference of
5936 P_4 will receive an ASSERT_EXPR.
5938 If this function returns true, then it means that there are names
5939 for which we need to generate ASSERT_EXPRs. Those assertions are
5940 inserted by process_assert_insertions. */
5942 static bool
5943 find_assert_locations_1 (basic_block bb, sbitmap live)
5945 gimple_stmt_iterator si;
5946 gimple last;
5947 bool need_assert;
5949 need_assert = false;
5950 last = last_stmt (bb);
5952 /* If BB's last statement is a conditional statement involving integer
5953 operands, determine if we need to add ASSERT_EXPRs. */
5954 if (last
5955 && gimple_code (last) == GIMPLE_COND
5956 && !fp_predicate (last)
5957 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5958 need_assert |= find_conditional_asserts (bb, last);
5960 /* If BB's last statement is a switch statement involving integer
5961 operands, determine if we need to add ASSERT_EXPRs. */
5962 if (last
5963 && gimple_code (last) == GIMPLE_SWITCH
5964 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5965 need_assert |= find_switch_asserts (bb, last);
5967 /* Traverse all the statements in BB marking used names and looking
5968 for statements that may infer assertions for their used operands. */
5969 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5971 gimple stmt;
5972 tree op;
5973 ssa_op_iter i;
5975 stmt = gsi_stmt (si);
5977 if (is_gimple_debug (stmt))
5978 continue;
5980 /* See if we can derive an assertion for any of STMT's operands. */
5981 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5983 tree value;
5984 enum tree_code comp_code;
5986 /* If op is not live beyond this stmt, do not bother to insert
5987 asserts for it. */
5988 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5989 continue;
5991 /* If OP is used in such a way that we can infer a value
5992 range for it, and we don't find a previous assertion for
5993 it, create a new assertion location node for OP. */
5994 if (infer_value_range (stmt, op, &comp_code, &value))
5996 /* If we are able to infer a nonzero value range for OP,
5997 then walk backwards through the use-def chain to see if OP
5998 was set via a typecast.
6000 If so, then we can also infer a nonzero value range
6001 for the operand of the NOP_EXPR. */
6002 if (comp_code == NE_EXPR && integer_zerop (value))
6004 tree t = op;
6005 gimple def_stmt = SSA_NAME_DEF_STMT (t);
6007 while (is_gimple_assign (def_stmt)
6008 && CONVERT_EXPR_CODE_P
6009 (gimple_assign_rhs_code (def_stmt))
6010 && TREE_CODE
6011 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6012 && POINTER_TYPE_P
6013 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6015 t = gimple_assign_rhs1 (def_stmt);
6016 def_stmt = SSA_NAME_DEF_STMT (t);
6018 /* Note we want to register the assert for the
6019 operand of the NOP_EXPR after SI, not after the
6020 conversion. */
6021 if (! has_single_use (t))
6023 register_new_assert_for (t, t, comp_code, value,
6024 bb, NULL, si);
6025 need_assert = true;
6030 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6031 need_assert = true;
6035 /* Update live. */
6036 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6037 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6038 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6039 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6042 /* Traverse all PHI nodes in BB, updating live. */
6043 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6045 use_operand_p arg_p;
6046 ssa_op_iter i;
6047 gimple phi = gsi_stmt (si);
6048 tree res = gimple_phi_result (phi);
6050 if (virtual_operand_p (res))
6051 continue;
6053 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6055 tree arg = USE_FROM_PTR (arg_p);
6056 if (TREE_CODE (arg) == SSA_NAME)
6057 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6060 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6063 return need_assert;
6066 /* Do an RPO walk over the function computing SSA name liveness
6067 on-the-fly and deciding on assert expressions to insert.
6068 Returns true if there are assert expressions to be inserted. */
6070 static bool
6071 find_assert_locations (void)
6073 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6074 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6075 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6076 int rpo_cnt, i;
6077 bool need_asserts;
6079 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6080 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6081 for (i = 0; i < rpo_cnt; ++i)
6082 bb_rpo[rpo[i]] = i;
6084 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6085 the order we compute liveness and insert asserts we otherwise
6086 fail to insert asserts into the loop latch. */
6087 loop_p loop;
6088 FOR_EACH_LOOP (loop, 0)
6090 i = loop->latch->index;
6091 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6092 for (gimple_stmt_iterator gsi = gsi_start_phis (loop->header);
6093 !gsi_end_p (gsi); gsi_next (&gsi))
6095 gimple phi = gsi_stmt (gsi);
6096 if (virtual_operand_p (gimple_phi_result (phi)))
6097 continue;
6098 tree arg = gimple_phi_arg_def (phi, j);
6099 if (TREE_CODE (arg) == SSA_NAME)
6101 if (live[i] == NULL)
6103 live[i] = sbitmap_alloc (num_ssa_names);
6104 bitmap_clear (live[i]);
6106 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6111 need_asserts = false;
6112 for (i = rpo_cnt - 1; i >= 0; --i)
6114 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6115 edge e;
6116 edge_iterator ei;
6118 if (!live[rpo[i]])
6120 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6121 bitmap_clear (live[rpo[i]]);
6124 /* Process BB and update the live information with uses in
6125 this block. */
6126 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
6128 /* Merge liveness into the predecessor blocks and free it. */
6129 if (!bitmap_empty_p (live[rpo[i]]))
6131 int pred_rpo = i;
6132 FOR_EACH_EDGE (e, ei, bb->preds)
6134 int pred = e->src->index;
6135 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6136 continue;
6138 if (!live[pred])
6140 live[pred] = sbitmap_alloc (num_ssa_names);
6141 bitmap_clear (live[pred]);
6143 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6145 if (bb_rpo[pred] < pred_rpo)
6146 pred_rpo = bb_rpo[pred];
6149 /* Record the RPO number of the last visited block that needs
6150 live information from this block. */
6151 last_rpo[rpo[i]] = pred_rpo;
6153 else
6155 sbitmap_free (live[rpo[i]]);
6156 live[rpo[i]] = NULL;
6159 /* We can free all successors live bitmaps if all their
6160 predecessors have been visited already. */
6161 FOR_EACH_EDGE (e, ei, bb->succs)
6162 if (last_rpo[e->dest->index] == i
6163 && live[e->dest->index])
6165 sbitmap_free (live[e->dest->index]);
6166 live[e->dest->index] = NULL;
6170 XDELETEVEC (rpo);
6171 XDELETEVEC (bb_rpo);
6172 XDELETEVEC (last_rpo);
6173 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6174 if (live[i])
6175 sbitmap_free (live[i]);
6176 XDELETEVEC (live);
6178 return need_asserts;
6181 /* Create an ASSERT_EXPR for NAME and insert it in the location
6182 indicated by LOC. Return true if we made any edge insertions. */
6184 static bool
6185 process_assert_insertions_for (tree name, assert_locus_t loc)
6187 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6188 gimple stmt;
6189 tree cond;
6190 gimple assert_stmt;
6191 edge_iterator ei;
6192 edge e;
6194 /* If we have X <=> X do not insert an assert expr for that. */
6195 if (loc->expr == loc->val)
6196 return false;
6198 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6199 assert_stmt = build_assert_expr_for (cond, name);
6200 if (loc->e)
6202 /* We have been asked to insert the assertion on an edge. This
6203 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6204 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6205 || (gimple_code (gsi_stmt (loc->si))
6206 == GIMPLE_SWITCH));
6208 gsi_insert_on_edge (loc->e, assert_stmt);
6209 return true;
6212 /* Otherwise, we can insert right after LOC->SI iff the
6213 statement must not be the last statement in the block. */
6214 stmt = gsi_stmt (loc->si);
6215 if (!stmt_ends_bb_p (stmt))
6217 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6218 return false;
6221 /* If STMT must be the last statement in BB, we can only insert new
6222 assertions on the non-abnormal edge out of BB. Note that since
6223 STMT is not control flow, there may only be one non-abnormal edge
6224 out of BB. */
6225 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6226 if (!(e->flags & EDGE_ABNORMAL))
6228 gsi_insert_on_edge (e, assert_stmt);
6229 return true;
6232 gcc_unreachable ();
6236 /* Process all the insertions registered for every name N_i registered
6237 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6238 found in ASSERTS_FOR[i]. */
6240 static void
6241 process_assert_insertions (void)
6243 unsigned i;
6244 bitmap_iterator bi;
6245 bool update_edges_p = false;
6246 int num_asserts = 0;
6248 if (dump_file && (dump_flags & TDF_DETAILS))
6249 dump_all_asserts (dump_file);
6251 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6253 assert_locus_t loc = asserts_for[i];
6254 gcc_assert (loc);
6256 while (loc)
6258 assert_locus_t next = loc->next;
6259 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6260 free (loc);
6261 loc = next;
6262 num_asserts++;
6266 if (update_edges_p)
6267 gsi_commit_edge_inserts ();
6269 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6270 num_asserts);
6274 /* Traverse the flowgraph looking for conditional jumps to insert range
6275 expressions. These range expressions are meant to provide information
6276 to optimizations that need to reason in terms of value ranges. They
6277 will not be expanded into RTL. For instance, given:
6279 x = ...
6280 y = ...
6281 if (x < y)
6282 y = x - 2;
6283 else
6284 x = y + 3;
6286 this pass will transform the code into:
6288 x = ...
6289 y = ...
6290 if (x < y)
6292 x = ASSERT_EXPR <x, x < y>
6293 y = x - 2
6295 else
6297 y = ASSERT_EXPR <y, x >= y>
6298 x = y + 3
6301 The idea is that once copy and constant propagation have run, other
6302 optimizations will be able to determine what ranges of values can 'x'
6303 take in different paths of the code, simply by checking the reaching
6304 definition of 'x'. */
6306 static void
6307 insert_range_assertions (void)
6309 need_assert_for = BITMAP_ALLOC (NULL);
6310 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6312 calculate_dominance_info (CDI_DOMINATORS);
6314 if (find_assert_locations ())
6316 process_assert_insertions ();
6317 update_ssa (TODO_update_ssa_no_phi);
6320 if (dump_file && (dump_flags & TDF_DETAILS))
6322 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6323 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6326 free (asserts_for);
6327 BITMAP_FREE (need_assert_for);
6330 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6331 and "struct" hacks. If VRP can determine that the
6332 array subscript is a constant, check if it is outside valid
6333 range. If the array subscript is a RANGE, warn if it is
6334 non-overlapping with valid range.
6335 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6337 static void
6338 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6340 value_range_t* vr = NULL;
6341 tree low_sub, up_sub;
6342 tree low_bound, up_bound, up_bound_p1;
6343 tree base;
6345 if (TREE_NO_WARNING (ref))
6346 return;
6348 low_sub = up_sub = TREE_OPERAND (ref, 1);
6349 up_bound = array_ref_up_bound (ref);
6351 /* Can not check flexible arrays. */
6352 if (!up_bound
6353 || TREE_CODE (up_bound) != INTEGER_CST)
6354 return;
6356 /* Accesses to trailing arrays via pointers may access storage
6357 beyond the types array bounds. */
6358 base = get_base_address (ref);
6359 if (base && TREE_CODE (base) == MEM_REF)
6361 tree cref, next = NULL_TREE;
6363 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6364 return;
6366 cref = TREE_OPERAND (ref, 0);
6367 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6368 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6369 next && TREE_CODE (next) != FIELD_DECL;
6370 next = DECL_CHAIN (next))
6373 /* If this is the last field in a struct type or a field in a
6374 union type do not warn. */
6375 if (!next)
6376 return;
6379 low_bound = array_ref_low_bound (ref);
6380 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6381 build_int_cst (TREE_TYPE (up_bound), 1));
6383 if (TREE_CODE (low_sub) == SSA_NAME)
6385 vr = get_value_range (low_sub);
6386 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6388 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6389 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6393 if (vr && vr->type == VR_ANTI_RANGE)
6395 if (TREE_CODE (up_sub) == INTEGER_CST
6396 && tree_int_cst_lt (up_bound, up_sub)
6397 && TREE_CODE (low_sub) == INTEGER_CST
6398 && tree_int_cst_lt (low_sub, low_bound))
6400 warning_at (location, OPT_Warray_bounds,
6401 "array subscript is outside array bounds");
6402 TREE_NO_WARNING (ref) = 1;
6405 else if (TREE_CODE (up_sub) == INTEGER_CST
6406 && (ignore_off_by_one
6407 ? (tree_int_cst_lt (up_bound, up_sub)
6408 && !tree_int_cst_equal (up_bound_p1, up_sub))
6409 : (tree_int_cst_lt (up_bound, up_sub)
6410 || tree_int_cst_equal (up_bound_p1, up_sub))))
6412 if (dump_file && (dump_flags & TDF_DETAILS))
6414 fprintf (dump_file, "Array bound warning for ");
6415 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6416 fprintf (dump_file, "\n");
6418 warning_at (location, OPT_Warray_bounds,
6419 "array subscript is above array bounds");
6420 TREE_NO_WARNING (ref) = 1;
6422 else if (TREE_CODE (low_sub) == INTEGER_CST
6423 && tree_int_cst_lt (low_sub, low_bound))
6425 if (dump_file && (dump_flags & TDF_DETAILS))
6427 fprintf (dump_file, "Array bound warning for ");
6428 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6429 fprintf (dump_file, "\n");
6431 warning_at (location, OPT_Warray_bounds,
6432 "array subscript is below array bounds");
6433 TREE_NO_WARNING (ref) = 1;
6437 /* Searches if the expr T, located at LOCATION computes
6438 address of an ARRAY_REF, and call check_array_ref on it. */
6440 static void
6441 search_for_addr_array (tree t, location_t location)
6443 while (TREE_CODE (t) == SSA_NAME)
6445 gimple g = SSA_NAME_DEF_STMT (t);
6447 if (gimple_code (g) != GIMPLE_ASSIGN)
6448 return;
6450 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6451 != GIMPLE_SINGLE_RHS)
6452 return;
6454 t = gimple_assign_rhs1 (g);
6458 /* We are only interested in addresses of ARRAY_REF's. */
6459 if (TREE_CODE (t) != ADDR_EXPR)
6460 return;
6462 /* Check each ARRAY_REFs in the reference chain. */
6465 if (TREE_CODE (t) == ARRAY_REF)
6466 check_array_ref (location, t, true /*ignore_off_by_one*/);
6468 t = TREE_OPERAND (t, 0);
6470 while (handled_component_p (t));
6472 if (TREE_CODE (t) == MEM_REF
6473 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6474 && !TREE_NO_WARNING (t))
6476 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6477 tree low_bound, up_bound, el_sz;
6478 offset_int idx;
6479 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6480 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6481 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6482 return;
6484 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6485 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6486 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6487 if (!low_bound
6488 || TREE_CODE (low_bound) != INTEGER_CST
6489 || !up_bound
6490 || TREE_CODE (up_bound) != INTEGER_CST
6491 || !el_sz
6492 || TREE_CODE (el_sz) != INTEGER_CST)
6493 return;
6495 idx = mem_ref_offset (t);
6496 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6497 if (wi::lts_p (idx, 0))
6499 if (dump_file && (dump_flags & TDF_DETAILS))
6501 fprintf (dump_file, "Array bound warning for ");
6502 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6503 fprintf (dump_file, "\n");
6505 warning_at (location, OPT_Warray_bounds,
6506 "array subscript is below array bounds");
6507 TREE_NO_WARNING (t) = 1;
6509 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6510 - wi::to_offset (low_bound) + 1)))
6512 if (dump_file && (dump_flags & TDF_DETAILS))
6514 fprintf (dump_file, "Array bound warning for ");
6515 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6516 fprintf (dump_file, "\n");
6518 warning_at (location, OPT_Warray_bounds,
6519 "array subscript is above array bounds");
6520 TREE_NO_WARNING (t) = 1;
6525 /* walk_tree() callback that checks if *TP is
6526 an ARRAY_REF inside an ADDR_EXPR (in which an array
6527 subscript one outside the valid range is allowed). Call
6528 check_array_ref for each ARRAY_REF found. The location is
6529 passed in DATA. */
6531 static tree
6532 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6534 tree t = *tp;
6535 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6536 location_t location;
6538 if (EXPR_HAS_LOCATION (t))
6539 location = EXPR_LOCATION (t);
6540 else
6542 location_t *locp = (location_t *) wi->info;
6543 location = *locp;
6546 *walk_subtree = TRUE;
6548 if (TREE_CODE (t) == ARRAY_REF)
6549 check_array_ref (location, t, false /*ignore_off_by_one*/);
6551 if (TREE_CODE (t) == MEM_REF
6552 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6553 search_for_addr_array (TREE_OPERAND (t, 0), location);
6555 if (TREE_CODE (t) == ADDR_EXPR)
6556 *walk_subtree = FALSE;
6558 return NULL_TREE;
6561 /* Walk over all statements of all reachable BBs and call check_array_bounds
6562 on them. */
6564 static void
6565 check_all_array_refs (void)
6567 basic_block bb;
6568 gimple_stmt_iterator si;
6570 FOR_EACH_BB_FN (bb, cfun)
6572 edge_iterator ei;
6573 edge e;
6574 bool executable = false;
6576 /* Skip blocks that were found to be unreachable. */
6577 FOR_EACH_EDGE (e, ei, bb->preds)
6578 executable |= !!(e->flags & EDGE_EXECUTABLE);
6579 if (!executable)
6580 continue;
6582 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6584 gimple stmt = gsi_stmt (si);
6585 struct walk_stmt_info wi;
6586 if (!gimple_has_location (stmt))
6587 continue;
6589 if (is_gimple_call (stmt))
6591 size_t i;
6592 size_t n = gimple_call_num_args (stmt);
6593 for (i = 0; i < n; i++)
6595 tree arg = gimple_call_arg (stmt, i);
6596 search_for_addr_array (arg, gimple_location (stmt));
6599 else
6601 memset (&wi, 0, sizeof (wi));
6602 wi.info = CONST_CAST (void *, (const void *)
6603 gimple_location_ptr (stmt));
6605 walk_gimple_op (gsi_stmt (si),
6606 check_array_bounds,
6607 &wi);
6613 /* Return true if all imm uses of VAR are either in STMT, or
6614 feed (optionally through a chain of single imm uses) GIMPLE_COND
6615 in basic block COND_BB. */
6617 static bool
6618 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6620 use_operand_p use_p, use2_p;
6621 imm_use_iterator iter;
6623 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6624 if (USE_STMT (use_p) != stmt)
6626 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6627 if (is_gimple_debug (use_stmt))
6628 continue;
6629 while (is_gimple_assign (use_stmt)
6630 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6631 && single_imm_use (gimple_assign_lhs (use_stmt),
6632 &use2_p, &use_stmt2))
6633 use_stmt = use_stmt2;
6634 if (gimple_code (use_stmt) != GIMPLE_COND
6635 || gimple_bb (use_stmt) != cond_bb)
6636 return false;
6638 return true;
6641 /* Handle
6642 _4 = x_3 & 31;
6643 if (_4 != 0)
6644 goto <bb 6>;
6645 else
6646 goto <bb 7>;
6647 <bb 6>:
6648 __builtin_unreachable ();
6649 <bb 7>:
6650 x_5 = ASSERT_EXPR <x_3, ...>;
6651 If x_3 has no other immediate uses (checked by caller),
6652 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6653 from the non-zero bitmask. */
6655 static void
6656 maybe_set_nonzero_bits (basic_block bb, tree var)
6658 edge e = single_pred_edge (bb);
6659 basic_block cond_bb = e->src;
6660 gimple stmt = last_stmt (cond_bb);
6661 tree cst;
6663 if (stmt == NULL
6664 || gimple_code (stmt) != GIMPLE_COND
6665 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6666 ? EQ_EXPR : NE_EXPR)
6667 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6668 || !integer_zerop (gimple_cond_rhs (stmt)))
6669 return;
6671 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6672 if (!is_gimple_assign (stmt)
6673 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6674 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6675 return;
6676 if (gimple_assign_rhs1 (stmt) != var)
6678 gimple stmt2;
6680 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6681 return;
6682 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6683 if (!gimple_assign_cast_p (stmt2)
6684 || gimple_assign_rhs1 (stmt2) != var
6685 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6686 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6687 != TYPE_PRECISION (TREE_TYPE (var))))
6688 return;
6690 cst = gimple_assign_rhs2 (stmt);
6691 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6694 /* Convert range assertion expressions into the implied copies and
6695 copy propagate away the copies. Doing the trivial copy propagation
6696 here avoids the need to run the full copy propagation pass after
6697 VRP.
6699 FIXME, this will eventually lead to copy propagation removing the
6700 names that had useful range information attached to them. For
6701 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6702 then N_i will have the range [3, +INF].
6704 However, by converting the assertion into the implied copy
6705 operation N_i = N_j, we will then copy-propagate N_j into the uses
6706 of N_i and lose the range information. We may want to hold on to
6707 ASSERT_EXPRs a little while longer as the ranges could be used in
6708 things like jump threading.
6710 The problem with keeping ASSERT_EXPRs around is that passes after
6711 VRP need to handle them appropriately.
6713 Another approach would be to make the range information a first
6714 class property of the SSA_NAME so that it can be queried from
6715 any pass. This is made somewhat more complex by the need for
6716 multiple ranges to be associated with one SSA_NAME. */
6718 static void
6719 remove_range_assertions (void)
6721 basic_block bb;
6722 gimple_stmt_iterator si;
6723 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6724 a basic block preceeded by GIMPLE_COND branching to it and
6725 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6726 int is_unreachable;
6728 /* Note that the BSI iterator bump happens at the bottom of the
6729 loop and no bump is necessary if we're removing the statement
6730 referenced by the current BSI. */
6731 FOR_EACH_BB_FN (bb, cfun)
6732 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6734 gimple stmt = gsi_stmt (si);
6735 gimple use_stmt;
6737 if (is_gimple_assign (stmt)
6738 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6740 tree lhs = gimple_assign_lhs (stmt);
6741 tree rhs = gimple_assign_rhs1 (stmt);
6742 tree var;
6743 tree cond = fold (ASSERT_EXPR_COND (rhs));
6744 use_operand_p use_p;
6745 imm_use_iterator iter;
6747 gcc_assert (cond != boolean_false_node);
6749 var = ASSERT_EXPR_VAR (rhs);
6750 gcc_assert (TREE_CODE (var) == SSA_NAME);
6752 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6753 && SSA_NAME_RANGE_INFO (lhs))
6755 if (is_unreachable == -1)
6757 is_unreachable = 0;
6758 if (single_pred_p (bb)
6759 && assert_unreachable_fallthru_edge_p
6760 (single_pred_edge (bb)))
6761 is_unreachable = 1;
6763 /* Handle
6764 if (x_7 >= 10 && x_7 < 20)
6765 __builtin_unreachable ();
6766 x_8 = ASSERT_EXPR <x_7, ...>;
6767 if the only uses of x_7 are in the ASSERT_EXPR and
6768 in the condition. In that case, we can copy the
6769 range info from x_8 computed in this pass also
6770 for x_7. */
6771 if (is_unreachable
6772 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6773 single_pred (bb)))
6775 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6776 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6777 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6778 maybe_set_nonzero_bits (bb, var);
6782 /* Propagate the RHS into every use of the LHS. */
6783 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6784 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6785 SET_USE (use_p, var);
6787 /* And finally, remove the copy, it is not needed. */
6788 gsi_remove (&si, true);
6789 release_defs (stmt);
6791 else
6793 if (!is_gimple_debug (gsi_stmt (si)))
6794 is_unreachable = 0;
6795 gsi_next (&si);
6801 /* Return true if STMT is interesting for VRP. */
6803 static bool
6804 stmt_interesting_for_vrp (gimple stmt)
6806 if (gimple_code (stmt) == GIMPLE_PHI)
6808 tree res = gimple_phi_result (stmt);
6809 return (!virtual_operand_p (res)
6810 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6811 || POINTER_TYPE_P (TREE_TYPE (res))));
6813 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6815 tree lhs = gimple_get_lhs (stmt);
6817 /* In general, assignments with virtual operands are not useful
6818 for deriving ranges, with the obvious exception of calls to
6819 builtin functions. */
6820 if (lhs && TREE_CODE (lhs) == SSA_NAME
6821 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6822 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6823 && (is_gimple_call (stmt)
6824 || !gimple_vuse (stmt)))
6825 return true;
6827 else if (gimple_code (stmt) == GIMPLE_COND
6828 || gimple_code (stmt) == GIMPLE_SWITCH)
6829 return true;
6831 return false;
6835 /* Initialize local data structures for VRP. */
6837 static void
6838 vrp_initialize (void)
6840 basic_block bb;
6842 values_propagated = false;
6843 num_vr_values = num_ssa_names;
6844 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6845 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6847 FOR_EACH_BB_FN (bb, cfun)
6849 gimple_stmt_iterator si;
6851 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6853 gimple phi = gsi_stmt (si);
6854 if (!stmt_interesting_for_vrp (phi))
6856 tree lhs = PHI_RESULT (phi);
6857 set_value_range_to_varying (get_value_range (lhs));
6858 prop_set_simulate_again (phi, false);
6860 else
6861 prop_set_simulate_again (phi, true);
6864 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6866 gimple stmt = gsi_stmt (si);
6868 /* If the statement is a control insn, then we do not
6869 want to avoid simulating the statement once. Failure
6870 to do so means that those edges will never get added. */
6871 if (stmt_ends_bb_p (stmt))
6872 prop_set_simulate_again (stmt, true);
6873 else if (!stmt_interesting_for_vrp (stmt))
6875 ssa_op_iter i;
6876 tree def;
6877 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6878 set_value_range_to_varying (get_value_range (def));
6879 prop_set_simulate_again (stmt, false);
6881 else
6882 prop_set_simulate_again (stmt, true);
6887 /* Return the singleton value-range for NAME or NAME. */
6889 static inline tree
6890 vrp_valueize (tree name)
6892 if (TREE_CODE (name) == SSA_NAME)
6894 value_range_t *vr = get_value_range (name);
6895 if (vr->type == VR_RANGE
6896 && (vr->min == vr->max
6897 || operand_equal_p (vr->min, vr->max, 0)))
6898 return vr->min;
6900 return name;
6903 /* Visit assignment STMT. If it produces an interesting range, record
6904 the SSA name in *OUTPUT_P. */
6906 static enum ssa_prop_result
6907 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6909 tree def, lhs;
6910 ssa_op_iter iter;
6911 enum gimple_code code = gimple_code (stmt);
6912 lhs = gimple_get_lhs (stmt);
6914 /* We only keep track of ranges in integral and pointer types. */
6915 if (TREE_CODE (lhs) == SSA_NAME
6916 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6917 /* It is valid to have NULL MIN/MAX values on a type. See
6918 build_range_type. */
6919 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6920 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6921 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6923 value_range_t new_vr = VR_INITIALIZER;
6925 /* Try folding the statement to a constant first. */
6926 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6927 if (tem)
6928 set_value_range_to_value (&new_vr, tem, NULL);
6929 /* Then dispatch to value-range extracting functions. */
6930 else if (code == GIMPLE_CALL)
6931 extract_range_basic (&new_vr, stmt);
6932 else
6933 extract_range_from_assignment (&new_vr, stmt);
6935 if (update_value_range (lhs, &new_vr))
6937 *output_p = lhs;
6939 if (dump_file && (dump_flags & TDF_DETAILS))
6941 fprintf (dump_file, "Found new range for ");
6942 print_generic_expr (dump_file, lhs, 0);
6943 fprintf (dump_file, ": ");
6944 dump_value_range (dump_file, &new_vr);
6945 fprintf (dump_file, "\n");
6948 if (new_vr.type == VR_VARYING)
6949 return SSA_PROP_VARYING;
6951 return SSA_PROP_INTERESTING;
6954 return SSA_PROP_NOT_INTERESTING;
6957 /* Every other statement produces no useful ranges. */
6958 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6959 set_value_range_to_varying (get_value_range (def));
6961 return SSA_PROP_VARYING;
6964 /* Helper that gets the value range of the SSA_NAME with version I
6965 or a symbolic range containing the SSA_NAME only if the value range
6966 is varying or undefined. */
6968 static inline value_range_t
6969 get_vr_for_comparison (int i)
6971 value_range_t vr = *get_value_range (ssa_name (i));
6973 /* If name N_i does not have a valid range, use N_i as its own
6974 range. This allows us to compare against names that may
6975 have N_i in their ranges. */
6976 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6978 vr.type = VR_RANGE;
6979 vr.min = ssa_name (i);
6980 vr.max = ssa_name (i);
6983 return vr;
6986 /* Compare all the value ranges for names equivalent to VAR with VAL
6987 using comparison code COMP. Return the same value returned by
6988 compare_range_with_value, including the setting of
6989 *STRICT_OVERFLOW_P. */
6991 static tree
6992 compare_name_with_value (enum tree_code comp, tree var, tree val,
6993 bool *strict_overflow_p)
6995 bitmap_iterator bi;
6996 unsigned i;
6997 bitmap e;
6998 tree retval, t;
6999 int used_strict_overflow;
7000 bool sop;
7001 value_range_t equiv_vr;
7003 /* Get the set of equivalences for VAR. */
7004 e = get_value_range (var)->equiv;
7006 /* Start at -1. Set it to 0 if we do a comparison without relying
7007 on overflow, or 1 if all comparisons rely on overflow. */
7008 used_strict_overflow = -1;
7010 /* Compare vars' value range with val. */
7011 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7012 sop = false;
7013 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7014 if (retval)
7015 used_strict_overflow = sop ? 1 : 0;
7017 /* If the equiv set is empty we have done all work we need to do. */
7018 if (e == NULL)
7020 if (retval
7021 && used_strict_overflow > 0)
7022 *strict_overflow_p = true;
7023 return retval;
7026 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7028 equiv_vr = get_vr_for_comparison (i);
7029 sop = false;
7030 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7031 if (t)
7033 /* If we get different answers from different members
7034 of the equivalence set this check must be in a dead
7035 code region. Folding it to a trap representation
7036 would be correct here. For now just return don't-know. */
7037 if (retval != NULL
7038 && t != retval)
7040 retval = NULL_TREE;
7041 break;
7043 retval = t;
7045 if (!sop)
7046 used_strict_overflow = 0;
7047 else if (used_strict_overflow < 0)
7048 used_strict_overflow = 1;
7052 if (retval
7053 && used_strict_overflow > 0)
7054 *strict_overflow_p = true;
7056 return retval;
7060 /* Given a comparison code COMP and names N1 and N2, compare all the
7061 ranges equivalent to N1 against all the ranges equivalent to N2
7062 to determine the value of N1 COMP N2. Return the same value
7063 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7064 whether we relied on an overflow infinity in the comparison. */
7067 static tree
7068 compare_names (enum tree_code comp, tree n1, tree n2,
7069 bool *strict_overflow_p)
7071 tree t, retval;
7072 bitmap e1, e2;
7073 bitmap_iterator bi1, bi2;
7074 unsigned i1, i2;
7075 int used_strict_overflow;
7076 static bitmap_obstack *s_obstack = NULL;
7077 static bitmap s_e1 = NULL, s_e2 = NULL;
7079 /* Compare the ranges of every name equivalent to N1 against the
7080 ranges of every name equivalent to N2. */
7081 e1 = get_value_range (n1)->equiv;
7082 e2 = get_value_range (n2)->equiv;
7084 /* Use the fake bitmaps if e1 or e2 are not available. */
7085 if (s_obstack == NULL)
7087 s_obstack = XNEW (bitmap_obstack);
7088 bitmap_obstack_initialize (s_obstack);
7089 s_e1 = BITMAP_ALLOC (s_obstack);
7090 s_e2 = BITMAP_ALLOC (s_obstack);
7092 if (e1 == NULL)
7093 e1 = s_e1;
7094 if (e2 == NULL)
7095 e2 = s_e2;
7097 /* Add N1 and N2 to their own set of equivalences to avoid
7098 duplicating the body of the loop just to check N1 and N2
7099 ranges. */
7100 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7101 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7103 /* If the equivalence sets have a common intersection, then the two
7104 names can be compared without checking their ranges. */
7105 if (bitmap_intersect_p (e1, e2))
7107 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7108 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7110 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7111 ? boolean_true_node
7112 : boolean_false_node;
7115 /* Start at -1. Set it to 0 if we do a comparison without relying
7116 on overflow, or 1 if all comparisons rely on overflow. */
7117 used_strict_overflow = -1;
7119 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7120 N2 to their own set of equivalences to avoid duplicating the body
7121 of the loop just to check N1 and N2 ranges. */
7122 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7124 value_range_t vr1 = get_vr_for_comparison (i1);
7126 t = retval = NULL_TREE;
7127 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7129 bool sop = false;
7131 value_range_t vr2 = get_vr_for_comparison (i2);
7133 t = compare_ranges (comp, &vr1, &vr2, &sop);
7134 if (t)
7136 /* If we get different answers from different members
7137 of the equivalence set this check must be in a dead
7138 code region. Folding it to a trap representation
7139 would be correct here. For now just return don't-know. */
7140 if (retval != NULL
7141 && t != retval)
7143 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7144 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7145 return NULL_TREE;
7147 retval = t;
7149 if (!sop)
7150 used_strict_overflow = 0;
7151 else if (used_strict_overflow < 0)
7152 used_strict_overflow = 1;
7156 if (retval)
7158 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7159 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7160 if (used_strict_overflow > 0)
7161 *strict_overflow_p = true;
7162 return retval;
7166 /* None of the equivalent ranges are useful in computing this
7167 comparison. */
7168 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7169 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7170 return NULL_TREE;
7173 /* Helper function for vrp_evaluate_conditional_warnv. */
7175 static tree
7176 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7177 tree op0, tree op1,
7178 bool * strict_overflow_p)
7180 value_range_t *vr0, *vr1;
7182 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7183 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7185 tree res = NULL_TREE;
7186 if (vr0 && vr1)
7187 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7188 if (!res && vr0)
7189 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7190 if (!res && vr1)
7191 res = (compare_range_with_value
7192 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7193 return res;
7196 /* Helper function for vrp_evaluate_conditional_warnv. */
7198 static tree
7199 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7200 tree op1, bool use_equiv_p,
7201 bool *strict_overflow_p, bool *only_ranges)
7203 tree ret;
7204 if (only_ranges)
7205 *only_ranges = true;
7207 /* We only deal with integral and pointer types. */
7208 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7209 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7210 return NULL_TREE;
7212 if (use_equiv_p)
7214 if (only_ranges
7215 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7216 (code, op0, op1, strict_overflow_p)))
7217 return ret;
7218 *only_ranges = false;
7219 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7220 return compare_names (code, op0, op1, strict_overflow_p);
7221 else if (TREE_CODE (op0) == SSA_NAME)
7222 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7223 else if (TREE_CODE (op1) == SSA_NAME)
7224 return (compare_name_with_value
7225 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7227 else
7228 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7229 strict_overflow_p);
7230 return NULL_TREE;
7233 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7234 information. Return NULL if the conditional can not be evaluated.
7235 The ranges of all the names equivalent with the operands in COND
7236 will be used when trying to compute the value. If the result is
7237 based on undefined signed overflow, issue a warning if
7238 appropriate. */
7240 static tree
7241 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7243 bool sop;
7244 tree ret;
7245 bool only_ranges;
7247 /* Some passes and foldings leak constants with overflow flag set
7248 into the IL. Avoid doing wrong things with these and bail out. */
7249 if ((TREE_CODE (op0) == INTEGER_CST
7250 && TREE_OVERFLOW (op0))
7251 || (TREE_CODE (op1) == INTEGER_CST
7252 && TREE_OVERFLOW (op1)))
7253 return NULL_TREE;
7255 sop = false;
7256 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7257 &only_ranges);
7259 if (ret && sop)
7261 enum warn_strict_overflow_code wc;
7262 const char* warnmsg;
7264 if (is_gimple_min_invariant (ret))
7266 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7267 warnmsg = G_("assuming signed overflow does not occur when "
7268 "simplifying conditional to constant");
7270 else
7272 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7273 warnmsg = G_("assuming signed overflow does not occur when "
7274 "simplifying conditional");
7277 if (issue_strict_overflow_warning (wc))
7279 location_t location;
7281 if (!gimple_has_location (stmt))
7282 location = input_location;
7283 else
7284 location = gimple_location (stmt);
7285 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7289 if (warn_type_limits
7290 && ret && only_ranges
7291 && TREE_CODE_CLASS (code) == tcc_comparison
7292 && TREE_CODE (op0) == SSA_NAME)
7294 /* If the comparison is being folded and the operand on the LHS
7295 is being compared against a constant value that is outside of
7296 the natural range of OP0's type, then the predicate will
7297 always fold regardless of the value of OP0. If -Wtype-limits
7298 was specified, emit a warning. */
7299 tree type = TREE_TYPE (op0);
7300 value_range_t *vr0 = get_value_range (op0);
7302 if (vr0->type != VR_VARYING
7303 && INTEGRAL_TYPE_P (type)
7304 && vrp_val_is_min (vr0->min)
7305 && vrp_val_is_max (vr0->max)
7306 && is_gimple_min_invariant (op1))
7308 location_t location;
7310 if (!gimple_has_location (stmt))
7311 location = input_location;
7312 else
7313 location = gimple_location (stmt);
7315 warning_at (location, OPT_Wtype_limits,
7316 integer_zerop (ret)
7317 ? G_("comparison always false "
7318 "due to limited range of data type")
7319 : G_("comparison always true "
7320 "due to limited range of data type"));
7324 return ret;
7328 /* Visit conditional statement STMT. If we can determine which edge
7329 will be taken out of STMT's basic block, record it in
7330 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7331 SSA_PROP_VARYING. */
7333 static enum ssa_prop_result
7334 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
7336 tree val;
7337 bool sop;
7339 *taken_edge_p = NULL;
7341 if (dump_file && (dump_flags & TDF_DETAILS))
7343 tree use;
7344 ssa_op_iter i;
7346 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7347 print_gimple_stmt (dump_file, stmt, 0, 0);
7348 fprintf (dump_file, "\nWith known ranges\n");
7350 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7352 fprintf (dump_file, "\t");
7353 print_generic_expr (dump_file, use, 0);
7354 fprintf (dump_file, ": ");
7355 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7358 fprintf (dump_file, "\n");
7361 /* Compute the value of the predicate COND by checking the known
7362 ranges of each of its operands.
7364 Note that we cannot evaluate all the equivalent ranges here
7365 because those ranges may not yet be final and with the current
7366 propagation strategy, we cannot determine when the value ranges
7367 of the names in the equivalence set have changed.
7369 For instance, given the following code fragment
7371 i_5 = PHI <8, i_13>
7373 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7374 if (i_14 == 1)
7377 Assume that on the first visit to i_14, i_5 has the temporary
7378 range [8, 8] because the second argument to the PHI function is
7379 not yet executable. We derive the range ~[0, 0] for i_14 and the
7380 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7381 the first time, since i_14 is equivalent to the range [8, 8], we
7382 determine that the predicate is always false.
7384 On the next round of propagation, i_13 is determined to be
7385 VARYING, which causes i_5 to drop down to VARYING. So, another
7386 visit to i_14 is scheduled. In this second visit, we compute the
7387 exact same range and equivalence set for i_14, namely ~[0, 0] and
7388 { i_5 }. But we did not have the previous range for i_5
7389 registered, so vrp_visit_assignment thinks that the range for
7390 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7391 is not visited again, which stops propagation from visiting
7392 statements in the THEN clause of that if().
7394 To properly fix this we would need to keep the previous range
7395 value for the names in the equivalence set. This way we would've
7396 discovered that from one visit to the other i_5 changed from
7397 range [8, 8] to VR_VARYING.
7399 However, fixing this apparent limitation may not be worth the
7400 additional checking. Testing on several code bases (GCC, DLV,
7401 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7402 4 more predicates folded in SPEC. */
7403 sop = false;
7405 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7406 gimple_cond_lhs (stmt),
7407 gimple_cond_rhs (stmt),
7408 false, &sop, NULL);
7409 if (val)
7411 if (!sop)
7412 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7413 else
7415 if (dump_file && (dump_flags & TDF_DETAILS))
7416 fprintf (dump_file,
7417 "\nIgnoring predicate evaluation because "
7418 "it assumes that signed overflow is undefined");
7419 val = NULL_TREE;
7423 if (dump_file && (dump_flags & TDF_DETAILS))
7425 fprintf (dump_file, "\nPredicate evaluates to: ");
7426 if (val == NULL_TREE)
7427 fprintf (dump_file, "DON'T KNOW\n");
7428 else
7429 print_generic_stmt (dump_file, val, 0);
7432 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7435 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7436 that includes the value VAL. The search is restricted to the range
7437 [START_IDX, n - 1] where n is the size of VEC.
7439 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7440 returned.
7442 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7443 it is placed in IDX and false is returned.
7445 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7446 returned. */
7448 static bool
7449 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
7451 size_t n = gimple_switch_num_labels (stmt);
7452 size_t low, high;
7454 /* Find case label for minimum of the value range or the next one.
7455 At each iteration we are searching in [low, high - 1]. */
7457 for (low = start_idx, high = n; high != low; )
7459 tree t;
7460 int cmp;
7461 /* Note that i != high, so we never ask for n. */
7462 size_t i = (high + low) / 2;
7463 t = gimple_switch_label (stmt, i);
7465 /* Cache the result of comparing CASE_LOW and val. */
7466 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7468 if (cmp == 0)
7470 /* Ranges cannot be empty. */
7471 *idx = i;
7472 return true;
7474 else if (cmp > 0)
7475 high = i;
7476 else
7478 low = i + 1;
7479 if (CASE_HIGH (t) != NULL
7480 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7482 *idx = i;
7483 return true;
7488 *idx = high;
7489 return false;
7492 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7493 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7494 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7495 then MAX_IDX < MIN_IDX.
7496 Returns true if the default label is not needed. */
7498 static bool
7499 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
7500 size_t *max_idx)
7502 size_t i, j;
7503 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7504 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7506 if (i == j
7507 && min_take_default
7508 && max_take_default)
7510 /* Only the default case label reached.
7511 Return an empty range. */
7512 *min_idx = 1;
7513 *max_idx = 0;
7514 return false;
7516 else
7518 bool take_default = min_take_default || max_take_default;
7519 tree low, high;
7520 size_t k;
7522 if (max_take_default)
7523 j--;
7525 /* If the case label range is continuous, we do not need
7526 the default case label. Verify that. */
7527 high = CASE_LOW (gimple_switch_label (stmt, i));
7528 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7529 high = CASE_HIGH (gimple_switch_label (stmt, i));
7530 for (k = i + 1; k <= j; ++k)
7532 low = CASE_LOW (gimple_switch_label (stmt, k));
7533 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7535 take_default = true;
7536 break;
7538 high = low;
7539 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7540 high = CASE_HIGH (gimple_switch_label (stmt, k));
7543 *min_idx = i;
7544 *max_idx = j;
7545 return !take_default;
7549 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7550 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7551 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7552 Returns true if the default label is not needed. */
7554 static bool
7555 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
7556 size_t *max_idx1, size_t *min_idx2,
7557 size_t *max_idx2)
7559 size_t i, j, k, l;
7560 unsigned int n = gimple_switch_num_labels (stmt);
7561 bool take_default;
7562 tree case_low, case_high;
7563 tree min = vr->min, max = vr->max;
7565 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7567 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7569 /* Set second range to emtpy. */
7570 *min_idx2 = 1;
7571 *max_idx2 = 0;
7573 if (vr->type == VR_RANGE)
7575 *min_idx1 = i;
7576 *max_idx1 = j;
7577 return !take_default;
7580 /* Set first range to all case labels. */
7581 *min_idx1 = 1;
7582 *max_idx1 = n - 1;
7584 if (i > j)
7585 return false;
7587 /* Make sure all the values of case labels [i , j] are contained in
7588 range [MIN, MAX]. */
7589 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7590 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7591 if (tree_int_cst_compare (case_low, min) < 0)
7592 i += 1;
7593 if (case_high != NULL_TREE
7594 && tree_int_cst_compare (max, case_high) < 0)
7595 j -= 1;
7597 if (i > j)
7598 return false;
7600 /* If the range spans case labels [i, j], the corresponding anti-range spans
7601 the labels [1, i - 1] and [j + 1, n - 1]. */
7602 k = j + 1;
7603 l = n - 1;
7604 if (k > l)
7606 k = 1;
7607 l = 0;
7610 j = i - 1;
7611 i = 1;
7612 if (i > j)
7614 i = k;
7615 j = l;
7616 k = 1;
7617 l = 0;
7620 *min_idx1 = i;
7621 *max_idx1 = j;
7622 *min_idx2 = k;
7623 *max_idx2 = l;
7624 return false;
7627 /* Visit switch statement STMT. If we can determine which edge
7628 will be taken out of STMT's basic block, record it in
7629 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7630 SSA_PROP_VARYING. */
7632 static enum ssa_prop_result
7633 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7635 tree op, val;
7636 value_range_t *vr;
7637 size_t i = 0, j = 0, k, l;
7638 bool take_default;
7640 *taken_edge_p = NULL;
7641 op = gimple_switch_index (stmt);
7642 if (TREE_CODE (op) != SSA_NAME)
7643 return SSA_PROP_VARYING;
7645 vr = get_value_range (op);
7646 if (dump_file && (dump_flags & TDF_DETAILS))
7648 fprintf (dump_file, "\nVisiting switch expression with operand ");
7649 print_generic_expr (dump_file, op, 0);
7650 fprintf (dump_file, " with known range ");
7651 dump_value_range (dump_file, vr);
7652 fprintf (dump_file, "\n");
7655 if ((vr->type != VR_RANGE
7656 && vr->type != VR_ANTI_RANGE)
7657 || symbolic_range_p (vr))
7658 return SSA_PROP_VARYING;
7660 /* Find the single edge that is taken from the switch expression. */
7661 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7663 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7664 label */
7665 if (j < i)
7667 gcc_assert (take_default);
7668 val = gimple_switch_default_label (stmt);
7670 else
7672 /* Check if labels with index i to j and maybe the default label
7673 are all reaching the same label. */
7675 val = gimple_switch_label (stmt, i);
7676 if (take_default
7677 && CASE_LABEL (gimple_switch_default_label (stmt))
7678 != CASE_LABEL (val))
7680 if (dump_file && (dump_flags & TDF_DETAILS))
7681 fprintf (dump_file, " not a single destination for this "
7682 "range\n");
7683 return SSA_PROP_VARYING;
7685 for (++i; i <= j; ++i)
7687 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7689 if (dump_file && (dump_flags & TDF_DETAILS))
7690 fprintf (dump_file, " not a single destination for this "
7691 "range\n");
7692 return SSA_PROP_VARYING;
7695 for (; k <= l; ++k)
7697 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7699 if (dump_file && (dump_flags & TDF_DETAILS))
7700 fprintf (dump_file, " not a single destination for this "
7701 "range\n");
7702 return SSA_PROP_VARYING;
7707 *taken_edge_p = find_edge (gimple_bb (stmt),
7708 label_to_block (CASE_LABEL (val)));
7710 if (dump_file && (dump_flags & TDF_DETAILS))
7712 fprintf (dump_file, " will take edge to ");
7713 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7716 return SSA_PROP_INTERESTING;
7720 /* Evaluate statement STMT. If the statement produces a useful range,
7721 return SSA_PROP_INTERESTING and record the SSA name with the
7722 interesting range into *OUTPUT_P.
7724 If STMT is a conditional branch and we can determine its truth
7725 value, the taken edge is recorded in *TAKEN_EDGE_P.
7727 If STMT produces a varying value, return SSA_PROP_VARYING. */
7729 static enum ssa_prop_result
7730 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7732 tree def;
7733 ssa_op_iter iter;
7735 if (dump_file && (dump_flags & TDF_DETAILS))
7737 fprintf (dump_file, "\nVisiting statement:\n");
7738 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7741 if (!stmt_interesting_for_vrp (stmt))
7742 gcc_assert (stmt_ends_bb_p (stmt));
7743 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7744 return vrp_visit_assignment_or_call (stmt, output_p);
7745 else if (gimple_code (stmt) == GIMPLE_COND)
7746 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7747 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7748 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7750 /* All other statements produce nothing of interest for VRP, so mark
7751 their outputs varying and prevent further simulation. */
7752 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7753 set_value_range_to_varying (get_value_range (def));
7755 return SSA_PROP_VARYING;
7758 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7759 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7760 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7761 possible such range. The resulting range is not canonicalized. */
7763 static void
7764 union_ranges (enum value_range_type *vr0type,
7765 tree *vr0min, tree *vr0max,
7766 enum value_range_type vr1type,
7767 tree vr1min, tree vr1max)
7769 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7770 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7772 /* [] is vr0, () is vr1 in the following classification comments. */
7773 if (mineq && maxeq)
7775 /* [( )] */
7776 if (*vr0type == vr1type)
7777 /* Nothing to do for equal ranges. */
7779 else if ((*vr0type == VR_RANGE
7780 && vr1type == VR_ANTI_RANGE)
7781 || (*vr0type == VR_ANTI_RANGE
7782 && vr1type == VR_RANGE))
7784 /* For anti-range with range union the result is varying. */
7785 goto give_up;
7787 else
7788 gcc_unreachable ();
7790 else if (operand_less_p (*vr0max, vr1min) == 1
7791 || operand_less_p (vr1max, *vr0min) == 1)
7793 /* [ ] ( ) or ( ) [ ]
7794 If the ranges have an empty intersection, result of the union
7795 operation is the anti-range or if both are anti-ranges
7796 it covers all. */
7797 if (*vr0type == VR_ANTI_RANGE
7798 && vr1type == VR_ANTI_RANGE)
7799 goto give_up;
7800 else if (*vr0type == VR_ANTI_RANGE
7801 && vr1type == VR_RANGE)
7803 else if (*vr0type == VR_RANGE
7804 && vr1type == VR_ANTI_RANGE)
7806 *vr0type = vr1type;
7807 *vr0min = vr1min;
7808 *vr0max = vr1max;
7810 else if (*vr0type == VR_RANGE
7811 && vr1type == VR_RANGE)
7813 /* The result is the convex hull of both ranges. */
7814 if (operand_less_p (*vr0max, vr1min) == 1)
7816 /* If the result can be an anti-range, create one. */
7817 if (TREE_CODE (*vr0max) == INTEGER_CST
7818 && TREE_CODE (vr1min) == INTEGER_CST
7819 && vrp_val_is_min (*vr0min)
7820 && vrp_val_is_max (vr1max))
7822 tree min = int_const_binop (PLUS_EXPR,
7823 *vr0max,
7824 build_int_cst (TREE_TYPE (*vr0max), 1));
7825 tree max = int_const_binop (MINUS_EXPR,
7826 vr1min,
7827 build_int_cst (TREE_TYPE (vr1min), 1));
7828 if (!operand_less_p (max, min))
7830 *vr0type = VR_ANTI_RANGE;
7831 *vr0min = min;
7832 *vr0max = max;
7834 else
7835 *vr0max = vr1max;
7837 else
7838 *vr0max = vr1max;
7840 else
7842 /* If the result can be an anti-range, create one. */
7843 if (TREE_CODE (vr1max) == INTEGER_CST
7844 && TREE_CODE (*vr0min) == INTEGER_CST
7845 && vrp_val_is_min (vr1min)
7846 && vrp_val_is_max (*vr0max))
7848 tree min = int_const_binop (PLUS_EXPR,
7849 vr1max,
7850 build_int_cst (TREE_TYPE (vr1max), 1));
7851 tree max = int_const_binop (MINUS_EXPR,
7852 *vr0min,
7853 build_int_cst (TREE_TYPE (*vr0min), 1));
7854 if (!operand_less_p (max, min))
7856 *vr0type = VR_ANTI_RANGE;
7857 *vr0min = min;
7858 *vr0max = max;
7860 else
7861 *vr0min = vr1min;
7863 else
7864 *vr0min = vr1min;
7867 else
7868 gcc_unreachable ();
7870 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7871 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7873 /* [ ( ) ] or [( ) ] or [ ( )] */
7874 if (*vr0type == VR_RANGE
7875 && vr1type == VR_RANGE)
7877 else if (*vr0type == VR_ANTI_RANGE
7878 && vr1type == VR_ANTI_RANGE)
7880 *vr0type = vr1type;
7881 *vr0min = vr1min;
7882 *vr0max = vr1max;
7884 else if (*vr0type == VR_ANTI_RANGE
7885 && vr1type == VR_RANGE)
7887 /* Arbitrarily choose the right or left gap. */
7888 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7889 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7890 build_int_cst (TREE_TYPE (vr1min), 1));
7891 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7892 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7893 build_int_cst (TREE_TYPE (vr1max), 1));
7894 else
7895 goto give_up;
7897 else if (*vr0type == VR_RANGE
7898 && vr1type == VR_ANTI_RANGE)
7899 /* The result covers everything. */
7900 goto give_up;
7901 else
7902 gcc_unreachable ();
7904 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7905 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7907 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7908 if (*vr0type == VR_RANGE
7909 && vr1type == VR_RANGE)
7911 *vr0type = vr1type;
7912 *vr0min = vr1min;
7913 *vr0max = vr1max;
7915 else if (*vr0type == VR_ANTI_RANGE
7916 && vr1type == VR_ANTI_RANGE)
7918 else if (*vr0type == VR_RANGE
7919 && vr1type == VR_ANTI_RANGE)
7921 *vr0type = VR_ANTI_RANGE;
7922 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7924 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7925 build_int_cst (TREE_TYPE (*vr0min), 1));
7926 *vr0min = vr1min;
7928 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7930 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7931 build_int_cst (TREE_TYPE (*vr0max), 1));
7932 *vr0max = vr1max;
7934 else
7935 goto give_up;
7937 else if (*vr0type == VR_ANTI_RANGE
7938 && vr1type == VR_RANGE)
7939 /* The result covers everything. */
7940 goto give_up;
7941 else
7942 gcc_unreachable ();
7944 else if ((operand_less_p (vr1min, *vr0max) == 1
7945 || operand_equal_p (vr1min, *vr0max, 0))
7946 && operand_less_p (*vr0min, vr1min) == 1
7947 && operand_less_p (*vr0max, vr1max) == 1)
7949 /* [ ( ] ) or [ ]( ) */
7950 if (*vr0type == VR_RANGE
7951 && vr1type == VR_RANGE)
7952 *vr0max = vr1max;
7953 else if (*vr0type == VR_ANTI_RANGE
7954 && vr1type == VR_ANTI_RANGE)
7955 *vr0min = vr1min;
7956 else if (*vr0type == VR_ANTI_RANGE
7957 && vr1type == VR_RANGE)
7959 if (TREE_CODE (vr1min) == INTEGER_CST)
7960 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7961 build_int_cst (TREE_TYPE (vr1min), 1));
7962 else
7963 goto give_up;
7965 else if (*vr0type == VR_RANGE
7966 && vr1type == VR_ANTI_RANGE)
7968 if (TREE_CODE (*vr0max) == INTEGER_CST)
7970 *vr0type = vr1type;
7971 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7972 build_int_cst (TREE_TYPE (*vr0max), 1));
7973 *vr0max = vr1max;
7975 else
7976 goto give_up;
7978 else
7979 gcc_unreachable ();
7981 else if ((operand_less_p (*vr0min, vr1max) == 1
7982 || operand_equal_p (*vr0min, vr1max, 0))
7983 && operand_less_p (vr1min, *vr0min) == 1
7984 && operand_less_p (vr1max, *vr0max) == 1)
7986 /* ( [ ) ] or ( )[ ] */
7987 if (*vr0type == VR_RANGE
7988 && vr1type == VR_RANGE)
7989 *vr0min = vr1min;
7990 else if (*vr0type == VR_ANTI_RANGE
7991 && vr1type == VR_ANTI_RANGE)
7992 *vr0max = vr1max;
7993 else if (*vr0type == VR_ANTI_RANGE
7994 && vr1type == VR_RANGE)
7996 if (TREE_CODE (vr1max) == INTEGER_CST)
7997 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7998 build_int_cst (TREE_TYPE (vr1max), 1));
7999 else
8000 goto give_up;
8002 else if (*vr0type == VR_RANGE
8003 && vr1type == VR_ANTI_RANGE)
8005 if (TREE_CODE (*vr0min) == INTEGER_CST)
8007 *vr0type = vr1type;
8008 *vr0min = vr1min;
8009 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8010 build_int_cst (TREE_TYPE (*vr0min), 1));
8012 else
8013 goto give_up;
8015 else
8016 gcc_unreachable ();
8018 else
8019 goto give_up;
8021 return;
8023 give_up:
8024 *vr0type = VR_VARYING;
8025 *vr0min = NULL_TREE;
8026 *vr0max = NULL_TREE;
8029 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8030 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8031 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8032 possible such range. The resulting range is not canonicalized. */
8034 static void
8035 intersect_ranges (enum value_range_type *vr0type,
8036 tree *vr0min, tree *vr0max,
8037 enum value_range_type vr1type,
8038 tree vr1min, tree vr1max)
8040 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8041 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8043 /* [] is vr0, () is vr1 in the following classification comments. */
8044 if (mineq && maxeq)
8046 /* [( )] */
8047 if (*vr0type == vr1type)
8048 /* Nothing to do for equal ranges. */
8050 else if ((*vr0type == VR_RANGE
8051 && vr1type == VR_ANTI_RANGE)
8052 || (*vr0type == VR_ANTI_RANGE
8053 && vr1type == VR_RANGE))
8055 /* For anti-range with range intersection the result is empty. */
8056 *vr0type = VR_UNDEFINED;
8057 *vr0min = NULL_TREE;
8058 *vr0max = NULL_TREE;
8060 else
8061 gcc_unreachable ();
8063 else if (operand_less_p (*vr0max, vr1min) == 1
8064 || operand_less_p (vr1max, *vr0min) == 1)
8066 /* [ ] ( ) or ( ) [ ]
8067 If the ranges have an empty intersection, the result of the
8068 intersect operation is the range for intersecting an
8069 anti-range with a range or empty when intersecting two ranges. */
8070 if (*vr0type == VR_RANGE
8071 && vr1type == VR_ANTI_RANGE)
8073 else if (*vr0type == VR_ANTI_RANGE
8074 && vr1type == VR_RANGE)
8076 *vr0type = vr1type;
8077 *vr0min = vr1min;
8078 *vr0max = vr1max;
8080 else if (*vr0type == VR_RANGE
8081 && vr1type == VR_RANGE)
8083 *vr0type = VR_UNDEFINED;
8084 *vr0min = NULL_TREE;
8085 *vr0max = NULL_TREE;
8087 else if (*vr0type == VR_ANTI_RANGE
8088 && vr1type == VR_ANTI_RANGE)
8090 /* If the anti-ranges are adjacent to each other merge them. */
8091 if (TREE_CODE (*vr0max) == INTEGER_CST
8092 && TREE_CODE (vr1min) == INTEGER_CST
8093 && operand_less_p (*vr0max, vr1min) == 1
8094 && integer_onep (int_const_binop (MINUS_EXPR,
8095 vr1min, *vr0max)))
8096 *vr0max = vr1max;
8097 else if (TREE_CODE (vr1max) == INTEGER_CST
8098 && TREE_CODE (*vr0min) == INTEGER_CST
8099 && operand_less_p (vr1max, *vr0min) == 1
8100 && integer_onep (int_const_binop (MINUS_EXPR,
8101 *vr0min, vr1max)))
8102 *vr0min = vr1min;
8103 /* Else arbitrarily take VR0. */
8106 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8107 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8109 /* [ ( ) ] or [( ) ] or [ ( )] */
8110 if (*vr0type == VR_RANGE
8111 && vr1type == VR_RANGE)
8113 /* If both are ranges the result is the inner one. */
8114 *vr0type = vr1type;
8115 *vr0min = vr1min;
8116 *vr0max = vr1max;
8118 else if (*vr0type == VR_RANGE
8119 && vr1type == VR_ANTI_RANGE)
8121 /* Choose the right gap if the left one is empty. */
8122 if (mineq)
8124 if (TREE_CODE (vr1max) == INTEGER_CST)
8125 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8126 build_int_cst (TREE_TYPE (vr1max), 1));
8127 else
8128 *vr0min = vr1max;
8130 /* Choose the left gap if the right one is empty. */
8131 else if (maxeq)
8133 if (TREE_CODE (vr1min) == INTEGER_CST)
8134 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8135 build_int_cst (TREE_TYPE (vr1min), 1));
8136 else
8137 *vr0max = vr1min;
8139 /* Choose the anti-range if the range is effectively varying. */
8140 else if (vrp_val_is_min (*vr0min)
8141 && vrp_val_is_max (*vr0max))
8143 *vr0type = vr1type;
8144 *vr0min = vr1min;
8145 *vr0max = vr1max;
8147 /* Else choose the range. */
8149 else if (*vr0type == VR_ANTI_RANGE
8150 && vr1type == VR_ANTI_RANGE)
8151 /* If both are anti-ranges the result is the outer one. */
8153 else if (*vr0type == VR_ANTI_RANGE
8154 && vr1type == VR_RANGE)
8156 /* The intersection is empty. */
8157 *vr0type = VR_UNDEFINED;
8158 *vr0min = NULL_TREE;
8159 *vr0max = NULL_TREE;
8161 else
8162 gcc_unreachable ();
8164 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8165 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8167 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8168 if (*vr0type == VR_RANGE
8169 && vr1type == VR_RANGE)
8170 /* Choose the inner range. */
8172 else if (*vr0type == VR_ANTI_RANGE
8173 && vr1type == VR_RANGE)
8175 /* Choose the right gap if the left is empty. */
8176 if (mineq)
8178 *vr0type = VR_RANGE;
8179 if (TREE_CODE (*vr0max) == INTEGER_CST)
8180 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8181 build_int_cst (TREE_TYPE (*vr0max), 1));
8182 else
8183 *vr0min = *vr0max;
8184 *vr0max = vr1max;
8186 /* Choose the left gap if the right is empty. */
8187 else if (maxeq)
8189 *vr0type = VR_RANGE;
8190 if (TREE_CODE (*vr0min) == INTEGER_CST)
8191 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8192 build_int_cst (TREE_TYPE (*vr0min), 1));
8193 else
8194 *vr0max = *vr0min;
8195 *vr0min = vr1min;
8197 /* Choose the anti-range if the range is effectively varying. */
8198 else if (vrp_val_is_min (vr1min)
8199 && vrp_val_is_max (vr1max))
8201 /* Else choose the range. */
8202 else
8204 *vr0type = vr1type;
8205 *vr0min = vr1min;
8206 *vr0max = vr1max;
8209 else if (*vr0type == VR_ANTI_RANGE
8210 && vr1type == VR_ANTI_RANGE)
8212 /* If both are anti-ranges the result is the outer one. */
8213 *vr0type = vr1type;
8214 *vr0min = vr1min;
8215 *vr0max = vr1max;
8217 else if (vr1type == VR_ANTI_RANGE
8218 && *vr0type == VR_RANGE)
8220 /* The intersection is empty. */
8221 *vr0type = VR_UNDEFINED;
8222 *vr0min = NULL_TREE;
8223 *vr0max = NULL_TREE;
8225 else
8226 gcc_unreachable ();
8228 else if ((operand_less_p (vr1min, *vr0max) == 1
8229 || operand_equal_p (vr1min, *vr0max, 0))
8230 && operand_less_p (*vr0min, vr1min) == 1)
8232 /* [ ( ] ) or [ ]( ) */
8233 if (*vr0type == VR_ANTI_RANGE
8234 && vr1type == VR_ANTI_RANGE)
8235 *vr0max = vr1max;
8236 else if (*vr0type == VR_RANGE
8237 && vr1type == VR_RANGE)
8238 *vr0min = vr1min;
8239 else if (*vr0type == VR_RANGE
8240 && vr1type == VR_ANTI_RANGE)
8242 if (TREE_CODE (vr1min) == INTEGER_CST)
8243 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8244 build_int_cst (TREE_TYPE (vr1min), 1));
8245 else
8246 *vr0max = vr1min;
8248 else if (*vr0type == VR_ANTI_RANGE
8249 && vr1type == VR_RANGE)
8251 *vr0type = VR_RANGE;
8252 if (TREE_CODE (*vr0max) == INTEGER_CST)
8253 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8254 build_int_cst (TREE_TYPE (*vr0max), 1));
8255 else
8256 *vr0min = *vr0max;
8257 *vr0max = vr1max;
8259 else
8260 gcc_unreachable ();
8262 else if ((operand_less_p (*vr0min, vr1max) == 1
8263 || operand_equal_p (*vr0min, vr1max, 0))
8264 && operand_less_p (vr1min, *vr0min) == 1)
8266 /* ( [ ) ] or ( )[ ] */
8267 if (*vr0type == VR_ANTI_RANGE
8268 && vr1type == VR_ANTI_RANGE)
8269 *vr0min = vr1min;
8270 else if (*vr0type == VR_RANGE
8271 && vr1type == VR_RANGE)
8272 *vr0max = vr1max;
8273 else if (*vr0type == VR_RANGE
8274 && vr1type == VR_ANTI_RANGE)
8276 if (TREE_CODE (vr1max) == INTEGER_CST)
8277 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8278 build_int_cst (TREE_TYPE (vr1max), 1));
8279 else
8280 *vr0min = vr1max;
8282 else if (*vr0type == VR_ANTI_RANGE
8283 && vr1type == VR_RANGE)
8285 *vr0type = VR_RANGE;
8286 if (TREE_CODE (*vr0min) == INTEGER_CST)
8287 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8288 build_int_cst (TREE_TYPE (*vr0min), 1));
8289 else
8290 *vr0max = *vr0min;
8291 *vr0min = vr1min;
8293 else
8294 gcc_unreachable ();
8297 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8298 result for the intersection. That's always a conservative
8299 correct estimate. */
8301 return;
8305 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8306 in *VR0. This may not be the smallest possible such range. */
8308 static void
8309 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8311 value_range_t saved;
8313 /* If either range is VR_VARYING the other one wins. */
8314 if (vr1->type == VR_VARYING)
8315 return;
8316 if (vr0->type == VR_VARYING)
8318 copy_value_range (vr0, vr1);
8319 return;
8322 /* When either range is VR_UNDEFINED the resulting range is
8323 VR_UNDEFINED, too. */
8324 if (vr0->type == VR_UNDEFINED)
8325 return;
8326 if (vr1->type == VR_UNDEFINED)
8328 set_value_range_to_undefined (vr0);
8329 return;
8332 /* Save the original vr0 so we can return it as conservative intersection
8333 result when our worker turns things to varying. */
8334 saved = *vr0;
8335 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8336 vr1->type, vr1->min, vr1->max);
8337 /* Make sure to canonicalize the result though as the inversion of a
8338 VR_RANGE can still be a VR_RANGE. */
8339 set_and_canonicalize_value_range (vr0, vr0->type,
8340 vr0->min, vr0->max, vr0->equiv);
8341 /* If that failed, use the saved original VR0. */
8342 if (vr0->type == VR_VARYING)
8344 *vr0 = saved;
8345 return;
8347 /* If the result is VR_UNDEFINED there is no need to mess with
8348 the equivalencies. */
8349 if (vr0->type == VR_UNDEFINED)
8350 return;
8352 /* The resulting set of equivalences for range intersection is the union of
8353 the two sets. */
8354 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8355 bitmap_ior_into (vr0->equiv, vr1->equiv);
8356 else if (vr1->equiv && !vr0->equiv)
8357 bitmap_copy (vr0->equiv, vr1->equiv);
8360 static void
8361 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8363 if (dump_file && (dump_flags & TDF_DETAILS))
8365 fprintf (dump_file, "Intersecting\n ");
8366 dump_value_range (dump_file, vr0);
8367 fprintf (dump_file, "\nand\n ");
8368 dump_value_range (dump_file, vr1);
8369 fprintf (dump_file, "\n");
8371 vrp_intersect_ranges_1 (vr0, vr1);
8372 if (dump_file && (dump_flags & TDF_DETAILS))
8374 fprintf (dump_file, "to\n ");
8375 dump_value_range (dump_file, vr0);
8376 fprintf (dump_file, "\n");
8380 /* Meet operation for value ranges. Given two value ranges VR0 and
8381 VR1, store in VR0 a range that contains both VR0 and VR1. This
8382 may not be the smallest possible such range. */
8384 static void
8385 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8387 value_range_t saved;
8389 if (vr0->type == VR_UNDEFINED)
8391 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8392 return;
8395 if (vr1->type == VR_UNDEFINED)
8397 /* VR0 already has the resulting range. */
8398 return;
8401 if (vr0->type == VR_VARYING)
8403 /* Nothing to do. VR0 already has the resulting range. */
8404 return;
8407 if (vr1->type == VR_VARYING)
8409 set_value_range_to_varying (vr0);
8410 return;
8413 saved = *vr0;
8414 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8415 vr1->type, vr1->min, vr1->max);
8416 if (vr0->type == VR_VARYING)
8418 /* Failed to find an efficient meet. Before giving up and setting
8419 the result to VARYING, see if we can at least derive a useful
8420 anti-range. FIXME, all this nonsense about distinguishing
8421 anti-ranges from ranges is necessary because of the odd
8422 semantics of range_includes_zero_p and friends. */
8423 if (((saved.type == VR_RANGE
8424 && range_includes_zero_p (saved.min, saved.max) == 0)
8425 || (saved.type == VR_ANTI_RANGE
8426 && range_includes_zero_p (saved.min, saved.max) == 1))
8427 && ((vr1->type == VR_RANGE
8428 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8429 || (vr1->type == VR_ANTI_RANGE
8430 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8432 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8434 /* Since this meet operation did not result from the meeting of
8435 two equivalent names, VR0 cannot have any equivalences. */
8436 if (vr0->equiv)
8437 bitmap_clear (vr0->equiv);
8438 return;
8441 set_value_range_to_varying (vr0);
8442 return;
8444 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8445 vr0->equiv);
8446 if (vr0->type == VR_VARYING)
8447 return;
8449 /* The resulting set of equivalences is always the intersection of
8450 the two sets. */
8451 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8452 bitmap_and_into (vr0->equiv, vr1->equiv);
8453 else if (vr0->equiv && !vr1->equiv)
8454 bitmap_clear (vr0->equiv);
8457 static void
8458 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8460 if (dump_file && (dump_flags & TDF_DETAILS))
8462 fprintf (dump_file, "Meeting\n ");
8463 dump_value_range (dump_file, vr0);
8464 fprintf (dump_file, "\nand\n ");
8465 dump_value_range (dump_file, vr1);
8466 fprintf (dump_file, "\n");
8468 vrp_meet_1 (vr0, vr1);
8469 if (dump_file && (dump_flags & TDF_DETAILS))
8471 fprintf (dump_file, "to\n ");
8472 dump_value_range (dump_file, vr0);
8473 fprintf (dump_file, "\n");
8478 /* Visit all arguments for PHI node PHI that flow through executable
8479 edges. If a valid value range can be derived from all the incoming
8480 value ranges, set a new range for the LHS of PHI. */
8482 static enum ssa_prop_result
8483 vrp_visit_phi_node (gimple phi)
8485 size_t i;
8486 tree lhs = PHI_RESULT (phi);
8487 value_range_t *lhs_vr = get_value_range (lhs);
8488 value_range_t vr_result = VR_INITIALIZER;
8489 bool first = true;
8490 int edges, old_edges;
8491 struct loop *l;
8493 if (dump_file && (dump_flags & TDF_DETAILS))
8495 fprintf (dump_file, "\nVisiting PHI node: ");
8496 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8499 edges = 0;
8500 for (i = 0; i < gimple_phi_num_args (phi); i++)
8502 edge e = gimple_phi_arg_edge (phi, i);
8504 if (dump_file && (dump_flags & TDF_DETAILS))
8506 fprintf (dump_file,
8507 " Argument #%d (%d -> %d %sexecutable)\n",
8508 (int) i, e->src->index, e->dest->index,
8509 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8512 if (e->flags & EDGE_EXECUTABLE)
8514 tree arg = PHI_ARG_DEF (phi, i);
8515 value_range_t vr_arg;
8517 ++edges;
8519 if (TREE_CODE (arg) == SSA_NAME)
8521 vr_arg = *(get_value_range (arg));
8522 /* Do not allow equivalences or symbolic ranges to leak in from
8523 backedges. That creates invalid equivalencies.
8524 See PR53465 and PR54767. */
8525 if (e->flags & EDGE_DFS_BACK)
8527 if (vr_arg.type == VR_RANGE
8528 || vr_arg.type == VR_ANTI_RANGE)
8530 vr_arg.equiv = NULL;
8531 if (symbolic_range_p (&vr_arg))
8533 vr_arg.type = VR_VARYING;
8534 vr_arg.min = NULL_TREE;
8535 vr_arg.max = NULL_TREE;
8539 else
8541 /* If the non-backedge arguments range is VR_VARYING then
8542 we can still try recording a simple equivalence. */
8543 if (vr_arg.type == VR_VARYING)
8545 vr_arg.type = VR_RANGE;
8546 vr_arg.min = arg;
8547 vr_arg.max = arg;
8548 vr_arg.equiv = NULL;
8552 else
8554 if (TREE_OVERFLOW_P (arg))
8555 arg = drop_tree_overflow (arg);
8557 vr_arg.type = VR_RANGE;
8558 vr_arg.min = arg;
8559 vr_arg.max = arg;
8560 vr_arg.equiv = NULL;
8563 if (dump_file && (dump_flags & TDF_DETAILS))
8565 fprintf (dump_file, "\t");
8566 print_generic_expr (dump_file, arg, dump_flags);
8567 fprintf (dump_file, ": ");
8568 dump_value_range (dump_file, &vr_arg);
8569 fprintf (dump_file, "\n");
8572 if (first)
8573 copy_value_range (&vr_result, &vr_arg);
8574 else
8575 vrp_meet (&vr_result, &vr_arg);
8576 first = false;
8578 if (vr_result.type == VR_VARYING)
8579 break;
8583 if (vr_result.type == VR_VARYING)
8584 goto varying;
8585 else if (vr_result.type == VR_UNDEFINED)
8586 goto update_range;
8588 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8589 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8591 /* To prevent infinite iterations in the algorithm, derive ranges
8592 when the new value is slightly bigger or smaller than the
8593 previous one. We don't do this if we have seen a new executable
8594 edge; this helps us avoid an overflow infinity for conditionals
8595 which are not in a loop. If the old value-range was VR_UNDEFINED
8596 use the updated range and iterate one more time. */
8597 if (edges > 0
8598 && gimple_phi_num_args (phi) > 1
8599 && edges == old_edges
8600 && lhs_vr->type != VR_UNDEFINED)
8602 /* Compare old and new ranges, fall back to varying if the
8603 values are not comparable. */
8604 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8605 if (cmp_min == -2)
8606 goto varying;
8607 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8608 if (cmp_max == -2)
8609 goto varying;
8611 /* For non VR_RANGE or for pointers fall back to varying if
8612 the range changed. */
8613 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8614 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8615 && (cmp_min != 0 || cmp_max != 0))
8616 goto varying;
8618 /* If the new minimum is larger than than the previous one
8619 retain the old value. If the new minimum value is smaller
8620 than the previous one and not -INF go all the way to -INF + 1.
8621 In the first case, to avoid infinite bouncing between different
8622 minimums, and in the other case to avoid iterating millions of
8623 times to reach -INF. Going to -INF + 1 also lets the following
8624 iteration compute whether there will be any overflow, at the
8625 expense of one additional iteration. */
8626 if (cmp_min < 0)
8627 vr_result.min = lhs_vr->min;
8628 else if (cmp_min > 0
8629 && !vrp_val_is_min (vr_result.min))
8630 vr_result.min
8631 = int_const_binop (PLUS_EXPR,
8632 vrp_val_min (TREE_TYPE (vr_result.min)),
8633 build_int_cst (TREE_TYPE (vr_result.min), 1));
8635 /* Similarly for the maximum value. */
8636 if (cmp_max > 0)
8637 vr_result.max = lhs_vr->max;
8638 else if (cmp_max < 0
8639 && !vrp_val_is_max (vr_result.max))
8640 vr_result.max
8641 = int_const_binop (MINUS_EXPR,
8642 vrp_val_max (TREE_TYPE (vr_result.min)),
8643 build_int_cst (TREE_TYPE (vr_result.min), 1));
8645 /* If we dropped either bound to +-INF then if this is a loop
8646 PHI node SCEV may known more about its value-range. */
8647 if ((cmp_min > 0 || cmp_min < 0
8648 || cmp_max < 0 || cmp_max > 0)
8649 && (l = loop_containing_stmt (phi))
8650 && l->header == gimple_bb (phi))
8651 adjust_range_with_scev (&vr_result, l, phi, lhs);
8653 /* If we will end up with a (-INF, +INF) range, set it to
8654 VARYING. Same if the previous max value was invalid for
8655 the type and we end up with vr_result.min > vr_result.max. */
8656 if ((vrp_val_is_max (vr_result.max)
8657 && vrp_val_is_min (vr_result.min))
8658 || compare_values (vr_result.min,
8659 vr_result.max) > 0)
8660 goto varying;
8663 /* If the new range is different than the previous value, keep
8664 iterating. */
8665 update_range:
8666 if (update_value_range (lhs, &vr_result))
8668 if (dump_file && (dump_flags & TDF_DETAILS))
8670 fprintf (dump_file, "Found new range for ");
8671 print_generic_expr (dump_file, lhs, 0);
8672 fprintf (dump_file, ": ");
8673 dump_value_range (dump_file, &vr_result);
8674 fprintf (dump_file, "\n");
8677 return SSA_PROP_INTERESTING;
8680 /* Nothing changed, don't add outgoing edges. */
8681 return SSA_PROP_NOT_INTERESTING;
8683 /* No match found. Set the LHS to VARYING. */
8684 varying:
8685 set_value_range_to_varying (lhs_vr);
8686 return SSA_PROP_VARYING;
8689 /* Simplify boolean operations if the source is known
8690 to be already a boolean. */
8691 static bool
8692 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8694 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8695 tree lhs, op0, op1;
8696 bool need_conversion;
8698 /* We handle only !=/== case here. */
8699 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8701 op0 = gimple_assign_rhs1 (stmt);
8702 if (!op_with_boolean_value_range_p (op0))
8703 return false;
8705 op1 = gimple_assign_rhs2 (stmt);
8706 if (!op_with_boolean_value_range_p (op1))
8707 return false;
8709 /* Reduce number of cases to handle to NE_EXPR. As there is no
8710 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8711 if (rhs_code == EQ_EXPR)
8713 if (TREE_CODE (op1) == INTEGER_CST)
8714 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8715 build_int_cst (TREE_TYPE (op1), 1));
8716 else
8717 return false;
8720 lhs = gimple_assign_lhs (stmt);
8721 need_conversion
8722 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8724 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8725 if (need_conversion
8726 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8727 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8728 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8729 return false;
8731 /* For A != 0 we can substitute A itself. */
8732 if (integer_zerop (op1))
8733 gimple_assign_set_rhs_with_ops (gsi,
8734 need_conversion
8735 ? NOP_EXPR : TREE_CODE (op0),
8736 op0, NULL_TREE);
8737 /* For A != B we substitute A ^ B. Either with conversion. */
8738 else if (need_conversion)
8740 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8741 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8742 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8743 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8745 /* Or without. */
8746 else
8747 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8748 update_stmt (gsi_stmt (*gsi));
8750 return true;
8753 /* Simplify a division or modulo operator to a right shift or
8754 bitwise and if the first operand is unsigned or is greater
8755 than zero and the second operand is an exact power of two. */
8757 static bool
8758 simplify_div_or_mod_using_ranges (gimple stmt)
8760 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8761 tree val = NULL;
8762 tree op0 = gimple_assign_rhs1 (stmt);
8763 tree op1 = gimple_assign_rhs2 (stmt);
8764 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8766 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8768 val = integer_one_node;
8770 else
8772 bool sop = false;
8774 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8776 if (val
8777 && sop
8778 && integer_onep (val)
8779 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8781 location_t location;
8783 if (!gimple_has_location (stmt))
8784 location = input_location;
8785 else
8786 location = gimple_location (stmt);
8787 warning_at (location, OPT_Wstrict_overflow,
8788 "assuming signed overflow does not occur when "
8789 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8793 if (val && integer_onep (val))
8795 tree t;
8797 if (rhs_code == TRUNC_DIV_EXPR)
8799 t = build_int_cst (integer_type_node, tree_log2 (op1));
8800 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8801 gimple_assign_set_rhs1 (stmt, op0);
8802 gimple_assign_set_rhs2 (stmt, t);
8804 else
8806 t = build_int_cst (TREE_TYPE (op1), 1);
8807 t = int_const_binop (MINUS_EXPR, op1, t);
8808 t = fold_convert (TREE_TYPE (op0), t);
8810 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8811 gimple_assign_set_rhs1 (stmt, op0);
8812 gimple_assign_set_rhs2 (stmt, t);
8815 update_stmt (stmt);
8816 return true;
8819 return false;
8822 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8823 ABS_EXPR. If the operand is <= 0, then simplify the
8824 ABS_EXPR into a NEGATE_EXPR. */
8826 static bool
8827 simplify_abs_using_ranges (gimple stmt)
8829 tree val = NULL;
8830 tree op = gimple_assign_rhs1 (stmt);
8831 tree type = TREE_TYPE (op);
8832 value_range_t *vr = get_value_range (op);
8834 if (TYPE_UNSIGNED (type))
8836 val = integer_zero_node;
8838 else if (vr)
8840 bool sop = false;
8842 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8843 if (!val)
8845 sop = false;
8846 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8847 &sop);
8849 if (val)
8851 if (integer_zerop (val))
8852 val = integer_one_node;
8853 else if (integer_onep (val))
8854 val = integer_zero_node;
8858 if (val
8859 && (integer_onep (val) || integer_zerop (val)))
8861 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8863 location_t location;
8865 if (!gimple_has_location (stmt))
8866 location = input_location;
8867 else
8868 location = gimple_location (stmt);
8869 warning_at (location, OPT_Wstrict_overflow,
8870 "assuming signed overflow does not occur when "
8871 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8874 gimple_assign_set_rhs1 (stmt, op);
8875 if (integer_onep (val))
8876 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8877 else
8878 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8879 update_stmt (stmt);
8880 return true;
8884 return false;
8887 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8888 If all the bits that are being cleared by & are already
8889 known to be zero from VR, or all the bits that are being
8890 set by | are already known to be one from VR, the bit
8891 operation is redundant. */
8893 static bool
8894 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8896 tree op0 = gimple_assign_rhs1 (stmt);
8897 tree op1 = gimple_assign_rhs2 (stmt);
8898 tree op = NULL_TREE;
8899 value_range_t vr0 = VR_INITIALIZER;
8900 value_range_t vr1 = VR_INITIALIZER;
8901 wide_int may_be_nonzero0, may_be_nonzero1;
8902 wide_int must_be_nonzero0, must_be_nonzero1;
8903 wide_int mask;
8905 if (TREE_CODE (op0) == SSA_NAME)
8906 vr0 = *(get_value_range (op0));
8907 else if (is_gimple_min_invariant (op0))
8908 set_value_range_to_value (&vr0, op0, NULL);
8909 else
8910 return false;
8912 if (TREE_CODE (op1) == SSA_NAME)
8913 vr1 = *(get_value_range (op1));
8914 else if (is_gimple_min_invariant (op1))
8915 set_value_range_to_value (&vr1, op1, NULL);
8916 else
8917 return false;
8919 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
8920 &must_be_nonzero0))
8921 return false;
8922 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
8923 &must_be_nonzero1))
8924 return false;
8926 switch (gimple_assign_rhs_code (stmt))
8928 case BIT_AND_EXPR:
8929 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8930 if (mask == 0)
8932 op = op0;
8933 break;
8935 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8936 if (mask == 0)
8938 op = op1;
8939 break;
8941 break;
8942 case BIT_IOR_EXPR:
8943 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8944 if (mask == 0)
8946 op = op1;
8947 break;
8949 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8950 if (mask == 0)
8952 op = op0;
8953 break;
8955 break;
8956 default:
8957 gcc_unreachable ();
8960 if (op == NULL_TREE)
8961 return false;
8963 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8964 update_stmt (gsi_stmt (*gsi));
8965 return true;
8968 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8969 a known value range VR.
8971 If there is one and only one value which will satisfy the
8972 conditional, then return that value. Else return NULL. */
8974 static tree
8975 test_for_singularity (enum tree_code cond_code, tree op0,
8976 tree op1, value_range_t *vr)
8978 tree min = NULL;
8979 tree max = NULL;
8981 /* Extract minimum/maximum values which satisfy the
8982 the conditional as it was written. */
8983 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8985 /* This should not be negative infinity; there is no overflow
8986 here. */
8987 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8989 max = op1;
8990 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8992 tree one = build_int_cst (TREE_TYPE (op0), 1);
8993 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8994 if (EXPR_P (max))
8995 TREE_NO_WARNING (max) = 1;
8998 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9000 /* This should not be positive infinity; there is no overflow
9001 here. */
9002 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9004 min = op1;
9005 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9007 tree one = build_int_cst (TREE_TYPE (op0), 1);
9008 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9009 if (EXPR_P (min))
9010 TREE_NO_WARNING (min) = 1;
9014 /* Now refine the minimum and maximum values using any
9015 value range information we have for op0. */
9016 if (min && max)
9018 if (compare_values (vr->min, min) == 1)
9019 min = vr->min;
9020 if (compare_values (vr->max, max) == -1)
9021 max = vr->max;
9023 /* If the new min/max values have converged to a single value,
9024 then there is only one value which can satisfy the condition,
9025 return that value. */
9026 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9027 return min;
9029 return NULL;
9032 /* Return whether the value range *VR fits in an integer type specified
9033 by PRECISION and UNSIGNED_P. */
9035 static bool
9036 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9038 tree src_type;
9039 unsigned src_precision;
9040 widest_int tem;
9041 signop src_sgn;
9043 /* We can only handle integral and pointer types. */
9044 src_type = TREE_TYPE (vr->min);
9045 if (!INTEGRAL_TYPE_P (src_type)
9046 && !POINTER_TYPE_P (src_type))
9047 return false;
9049 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9050 and so is an identity transform. */
9051 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9052 src_sgn = TYPE_SIGN (src_type);
9053 if ((src_precision < dest_precision
9054 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9055 || (src_precision == dest_precision && src_sgn == dest_sgn))
9056 return true;
9058 /* Now we can only handle ranges with constant bounds. */
9059 if (vr->type != VR_RANGE
9060 || TREE_CODE (vr->min) != INTEGER_CST
9061 || TREE_CODE (vr->max) != INTEGER_CST)
9062 return false;
9064 /* For sign changes, the MSB of the wide_int has to be clear.
9065 An unsigned value with its MSB set cannot be represented by
9066 a signed wide_int, while a negative value cannot be represented
9067 by an unsigned wide_int. */
9068 if (src_sgn != dest_sgn
9069 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9070 return false;
9072 /* Then we can perform the conversion on both ends and compare
9073 the result for equality. */
9074 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9075 if (tem != wi::to_widest (vr->min))
9076 return false;
9077 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9078 if (tem != wi::to_widest (vr->max))
9079 return false;
9081 return true;
9084 /* Simplify a conditional using a relational operator to an equality
9085 test if the range information indicates only one value can satisfy
9086 the original conditional. */
9088 static bool
9089 simplify_cond_using_ranges (gimple stmt)
9091 tree op0 = gimple_cond_lhs (stmt);
9092 tree op1 = gimple_cond_rhs (stmt);
9093 enum tree_code cond_code = gimple_cond_code (stmt);
9095 if (cond_code != NE_EXPR
9096 && cond_code != EQ_EXPR
9097 && TREE_CODE (op0) == SSA_NAME
9098 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9099 && is_gimple_min_invariant (op1))
9101 value_range_t *vr = get_value_range (op0);
9103 /* If we have range information for OP0, then we might be
9104 able to simplify this conditional. */
9105 if (vr->type == VR_RANGE)
9107 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
9109 if (new_tree)
9111 if (dump_file)
9113 fprintf (dump_file, "Simplified relational ");
9114 print_gimple_stmt (dump_file, stmt, 0, 0);
9115 fprintf (dump_file, " into ");
9118 gimple_cond_set_code (stmt, EQ_EXPR);
9119 gimple_cond_set_lhs (stmt, op0);
9120 gimple_cond_set_rhs (stmt, new_tree);
9122 update_stmt (stmt);
9124 if (dump_file)
9126 print_gimple_stmt (dump_file, stmt, 0, 0);
9127 fprintf (dump_file, "\n");
9130 return true;
9133 /* Try again after inverting the condition. We only deal
9134 with integral types here, so no need to worry about
9135 issues with inverting FP comparisons. */
9136 cond_code = invert_tree_comparison (cond_code, false);
9137 new_tree = test_for_singularity (cond_code, op0, op1, vr);
9139 if (new_tree)
9141 if (dump_file)
9143 fprintf (dump_file, "Simplified relational ");
9144 print_gimple_stmt (dump_file, stmt, 0, 0);
9145 fprintf (dump_file, " into ");
9148 gimple_cond_set_code (stmt, NE_EXPR);
9149 gimple_cond_set_lhs (stmt, op0);
9150 gimple_cond_set_rhs (stmt, new_tree);
9152 update_stmt (stmt);
9154 if (dump_file)
9156 print_gimple_stmt (dump_file, stmt, 0, 0);
9157 fprintf (dump_file, "\n");
9160 return true;
9165 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9166 see if OP0 was set by a type conversion where the source of
9167 the conversion is another SSA_NAME with a range that fits
9168 into the range of OP0's type.
9170 If so, the conversion is redundant as the earlier SSA_NAME can be
9171 used for the comparison directly if we just massage the constant in the
9172 comparison. */
9173 if (TREE_CODE (op0) == SSA_NAME
9174 && TREE_CODE (op1) == INTEGER_CST)
9176 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9177 tree innerop;
9179 if (!is_gimple_assign (def_stmt)
9180 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9181 return false;
9183 innerop = gimple_assign_rhs1 (def_stmt);
9185 if (TREE_CODE (innerop) == SSA_NAME
9186 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9188 value_range_t *vr = get_value_range (innerop);
9190 if (range_int_cst_p (vr)
9191 && range_fits_type_p (vr,
9192 TYPE_PRECISION (TREE_TYPE (op0)),
9193 TYPE_SIGN (TREE_TYPE (op0)))
9194 && int_fits_type_p (op1, TREE_TYPE (innerop))
9195 /* The range must not have overflowed, or if it did overflow
9196 we must not be wrapping/trapping overflow and optimizing
9197 with strict overflow semantics. */
9198 && ((!is_negative_overflow_infinity (vr->min)
9199 && !is_positive_overflow_infinity (vr->max))
9200 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9202 /* If the range overflowed and the user has asked for warnings
9203 when strict overflow semantics were used to optimize code,
9204 issue an appropriate warning. */
9205 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9206 && (is_negative_overflow_infinity (vr->min)
9207 || is_positive_overflow_infinity (vr->max))
9208 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9210 location_t location;
9212 if (!gimple_has_location (stmt))
9213 location = input_location;
9214 else
9215 location = gimple_location (stmt);
9216 warning_at (location, OPT_Wstrict_overflow,
9217 "assuming signed overflow does not occur when "
9218 "simplifying conditional");
9221 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9222 gimple_cond_set_lhs (stmt, innerop);
9223 gimple_cond_set_rhs (stmt, newconst);
9224 return true;
9229 return false;
9232 /* Simplify a switch statement using the value range of the switch
9233 argument. */
9235 static bool
9236 simplify_switch_using_ranges (gimple stmt)
9238 tree op = gimple_switch_index (stmt);
9239 value_range_t *vr;
9240 bool take_default;
9241 edge e;
9242 edge_iterator ei;
9243 size_t i = 0, j = 0, n, n2;
9244 tree vec2;
9245 switch_update su;
9246 size_t k = 1, l = 0;
9248 if (TREE_CODE (op) == SSA_NAME)
9250 vr = get_value_range (op);
9252 /* We can only handle integer ranges. */
9253 if ((vr->type != VR_RANGE
9254 && vr->type != VR_ANTI_RANGE)
9255 || symbolic_range_p (vr))
9256 return false;
9258 /* Find case label for min/max of the value range. */
9259 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9261 else if (TREE_CODE (op) == INTEGER_CST)
9263 take_default = !find_case_label_index (stmt, 1, op, &i);
9264 if (take_default)
9266 i = 1;
9267 j = 0;
9269 else
9271 j = i;
9274 else
9275 return false;
9277 n = gimple_switch_num_labels (stmt);
9279 /* Bail out if this is just all edges taken. */
9280 if (i == 1
9281 && j == n - 1
9282 && take_default)
9283 return false;
9285 /* Build a new vector of taken case labels. */
9286 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9287 n2 = 0;
9289 /* Add the default edge, if necessary. */
9290 if (take_default)
9291 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9293 for (; i <= j; ++i, ++n2)
9294 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9296 for (; k <= l; ++k, ++n2)
9297 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9299 /* Mark needed edges. */
9300 for (i = 0; i < n2; ++i)
9302 e = find_edge (gimple_bb (stmt),
9303 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9304 e->aux = (void *)-1;
9307 /* Queue not needed edges for later removal. */
9308 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9310 if (e->aux == (void *)-1)
9312 e->aux = NULL;
9313 continue;
9316 if (dump_file && (dump_flags & TDF_DETAILS))
9318 fprintf (dump_file, "removing unreachable case label\n");
9320 to_remove_edges.safe_push (e);
9321 e->flags &= ~EDGE_EXECUTABLE;
9324 /* And queue an update for the stmt. */
9325 su.stmt = stmt;
9326 su.vec = vec2;
9327 to_update_switch_stmts.safe_push (su);
9328 return false;
9331 /* Simplify an integral conversion from an SSA name in STMT. */
9333 static bool
9334 simplify_conversion_using_ranges (gimple stmt)
9336 tree innerop, middleop, finaltype;
9337 gimple def_stmt;
9338 value_range_t *innervr;
9339 signop inner_sgn, middle_sgn, final_sgn;
9340 unsigned inner_prec, middle_prec, final_prec;
9341 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9343 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9344 if (!INTEGRAL_TYPE_P (finaltype))
9345 return false;
9346 middleop = gimple_assign_rhs1 (stmt);
9347 def_stmt = SSA_NAME_DEF_STMT (middleop);
9348 if (!is_gimple_assign (def_stmt)
9349 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9350 return false;
9351 innerop = gimple_assign_rhs1 (def_stmt);
9352 if (TREE_CODE (innerop) != SSA_NAME
9353 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9354 return false;
9356 /* Get the value-range of the inner operand. */
9357 innervr = get_value_range (innerop);
9358 if (innervr->type != VR_RANGE
9359 || TREE_CODE (innervr->min) != INTEGER_CST
9360 || TREE_CODE (innervr->max) != INTEGER_CST)
9361 return false;
9363 /* Simulate the conversion chain to check if the result is equal if
9364 the middle conversion is removed. */
9365 innermin = wi::to_widest (innervr->min);
9366 innermax = wi::to_widest (innervr->max);
9368 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9369 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9370 final_prec = TYPE_PRECISION (finaltype);
9372 /* If the first conversion is not injective, the second must not
9373 be widening. */
9374 if (wi::gtu_p (innermax - innermin,
9375 wi::mask <widest_int> (middle_prec, false))
9376 && middle_prec < final_prec)
9377 return false;
9378 /* We also want a medium value so that we can track the effect that
9379 narrowing conversions with sign change have. */
9380 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9381 if (inner_sgn == UNSIGNED)
9382 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9383 else
9384 innermed = 0;
9385 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9386 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9387 innermed = innermin;
9389 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9390 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9391 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9392 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9394 /* Require that the final conversion applied to both the original
9395 and the intermediate range produces the same result. */
9396 final_sgn = TYPE_SIGN (finaltype);
9397 if (wi::ext (middlemin, final_prec, final_sgn)
9398 != wi::ext (innermin, final_prec, final_sgn)
9399 || wi::ext (middlemed, final_prec, final_sgn)
9400 != wi::ext (innermed, final_prec, final_sgn)
9401 || wi::ext (middlemax, final_prec, final_sgn)
9402 != wi::ext (innermax, final_prec, final_sgn))
9403 return false;
9405 gimple_assign_set_rhs1 (stmt, innerop);
9406 update_stmt (stmt);
9407 return true;
9410 /* Simplify a conversion from integral SSA name to float in STMT. */
9412 static bool
9413 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9415 tree rhs1 = gimple_assign_rhs1 (stmt);
9416 value_range_t *vr = get_value_range (rhs1);
9417 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9418 machine_mode mode;
9419 tree tem;
9420 gimple conv;
9422 /* We can only handle constant ranges. */
9423 if (vr->type != VR_RANGE
9424 || TREE_CODE (vr->min) != INTEGER_CST
9425 || TREE_CODE (vr->max) != INTEGER_CST)
9426 return false;
9428 /* First check if we can use a signed type in place of an unsigned. */
9429 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9430 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9431 != CODE_FOR_nothing)
9432 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9433 mode = TYPE_MODE (TREE_TYPE (rhs1));
9434 /* If we can do the conversion in the current input mode do nothing. */
9435 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9436 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9437 return false;
9438 /* Otherwise search for a mode we can use, starting from the narrowest
9439 integer mode available. */
9440 else
9442 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9445 /* If we cannot do a signed conversion to float from mode
9446 or if the value-range does not fit in the signed type
9447 try with a wider mode. */
9448 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9449 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9450 break;
9452 mode = GET_MODE_WIDER_MODE (mode);
9453 /* But do not widen the input. Instead leave that to the
9454 optabs expansion code. */
9455 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9456 return false;
9458 while (mode != VOIDmode);
9459 if (mode == VOIDmode)
9460 return false;
9463 /* It works, insert a truncation or sign-change before the
9464 float conversion. */
9465 tem = make_ssa_name (build_nonstandard_integer_type
9466 (GET_MODE_PRECISION (mode), 0), NULL);
9467 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
9468 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9469 gimple_assign_set_rhs1 (stmt, tem);
9470 update_stmt (stmt);
9472 return true;
9475 /* Simplify an internal fn call using ranges if possible. */
9477 static bool
9478 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9480 enum tree_code subcode;
9481 switch (gimple_call_internal_fn (stmt))
9483 case IFN_UBSAN_CHECK_ADD:
9484 subcode = PLUS_EXPR;
9485 break;
9486 case IFN_UBSAN_CHECK_SUB:
9487 subcode = MINUS_EXPR;
9488 break;
9489 case IFN_UBSAN_CHECK_MUL:
9490 subcode = MULT_EXPR;
9491 break;
9492 default:
9493 return false;
9496 value_range_t vr0 = VR_INITIALIZER;
9497 value_range_t vr1 = VR_INITIALIZER;
9498 tree op0 = gimple_call_arg (stmt, 0);
9499 tree op1 = gimple_call_arg (stmt, 1);
9501 if (TREE_CODE (op0) == SSA_NAME)
9502 vr0 = *get_value_range (op0);
9503 else if (TREE_CODE (op0) == INTEGER_CST)
9504 set_value_range_to_value (&vr0, op0, NULL);
9505 else
9506 set_value_range_to_varying (&vr0);
9508 if (TREE_CODE (op1) == SSA_NAME)
9509 vr1 = *get_value_range (op1);
9510 else if (TREE_CODE (op1) == INTEGER_CST)
9511 set_value_range_to_value (&vr1, op1, NULL);
9512 else
9513 set_value_range_to_varying (&vr1);
9515 if (!range_int_cst_p (&vr0))
9517 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9518 optimize at least x = y + 0; x = y - 0; x = y * 0;
9519 and x = y * 1; which never overflow. */
9520 if (!range_int_cst_p (&vr1))
9521 return false;
9522 if (tree_int_cst_sgn (vr1.min) == -1)
9523 return false;
9524 if (compare_tree_int (vr1.max, subcode == MULT_EXPR) == 1)
9525 return false;
9527 else if (!range_int_cst_p (&vr1))
9529 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9530 optimize at least x = 0 + y; x = 0 * y; and x = 1 * y;
9531 which never overflow. */
9532 if (subcode == MINUS_EXPR)
9533 return false;
9534 if (!range_int_cst_p (&vr0))
9535 return false;
9536 if (tree_int_cst_sgn (vr0.min) == -1)
9537 return false;
9538 if (compare_tree_int (vr0.max, subcode == MULT_EXPR) == 1)
9539 return false;
9541 else
9543 tree r1 = int_const_binop (subcode, vr0.min,
9544 subcode == MINUS_EXPR ? vr1.max : vr1.min);
9545 tree r2 = int_const_binop (subcode, vr0.max,
9546 subcode == MINUS_EXPR ? vr1.min : vr1.max);
9547 if (r1 == NULL_TREE || TREE_OVERFLOW (r1)
9548 || r2 == NULL_TREE || TREE_OVERFLOW (r2))
9549 return false;
9550 if (subcode == MULT_EXPR)
9552 tree r3 = int_const_binop (subcode, vr0.min, vr1.max);
9553 tree r4 = int_const_binop (subcode, vr0.max, vr1.min);
9554 if (r3 == NULL_TREE || TREE_OVERFLOW (r3)
9555 || r4 == NULL_TREE || TREE_OVERFLOW (r4))
9556 return false;
9560 gimple g = gimple_build_assign_with_ops (subcode, gimple_call_lhs (stmt),
9561 op0, op1);
9562 gsi_replace (gsi, g, false);
9563 return true;
9566 /* Simplify STMT using ranges if possible. */
9568 static bool
9569 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9571 gimple stmt = gsi_stmt (*gsi);
9572 if (is_gimple_assign (stmt))
9574 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9575 tree rhs1 = gimple_assign_rhs1 (stmt);
9577 switch (rhs_code)
9579 case EQ_EXPR:
9580 case NE_EXPR:
9581 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9582 if the RHS is zero or one, and the LHS are known to be boolean
9583 values. */
9584 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9585 return simplify_truth_ops_using_ranges (gsi, stmt);
9586 break;
9588 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9589 and BIT_AND_EXPR respectively if the first operand is greater
9590 than zero and the second operand is an exact power of two. */
9591 case TRUNC_DIV_EXPR:
9592 case TRUNC_MOD_EXPR:
9593 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9594 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9595 return simplify_div_or_mod_using_ranges (stmt);
9596 break;
9598 /* Transform ABS (X) into X or -X as appropriate. */
9599 case ABS_EXPR:
9600 if (TREE_CODE (rhs1) == SSA_NAME
9601 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9602 return simplify_abs_using_ranges (stmt);
9603 break;
9605 case BIT_AND_EXPR:
9606 case BIT_IOR_EXPR:
9607 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9608 if all the bits being cleared are already cleared or
9609 all the bits being set are already set. */
9610 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9611 return simplify_bit_ops_using_ranges (gsi, stmt);
9612 break;
9614 CASE_CONVERT:
9615 if (TREE_CODE (rhs1) == SSA_NAME
9616 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9617 return simplify_conversion_using_ranges (stmt);
9618 break;
9620 case FLOAT_EXPR:
9621 if (TREE_CODE (rhs1) == SSA_NAME
9622 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9623 return simplify_float_conversion_using_ranges (gsi, stmt);
9624 break;
9626 default:
9627 break;
9630 else if (gimple_code (stmt) == GIMPLE_COND)
9631 return simplify_cond_using_ranges (stmt);
9632 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9633 return simplify_switch_using_ranges (stmt);
9634 else if (is_gimple_call (stmt)
9635 && gimple_call_internal_p (stmt))
9636 return simplify_internal_call_using_ranges (gsi, stmt);
9638 return false;
9641 /* If the statement pointed by SI has a predicate whose value can be
9642 computed using the value range information computed by VRP, compute
9643 its value and return true. Otherwise, return false. */
9645 static bool
9646 fold_predicate_in (gimple_stmt_iterator *si)
9648 bool assignment_p = false;
9649 tree val;
9650 gimple stmt = gsi_stmt (*si);
9652 if (is_gimple_assign (stmt)
9653 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9655 assignment_p = true;
9656 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9657 gimple_assign_rhs1 (stmt),
9658 gimple_assign_rhs2 (stmt),
9659 stmt);
9661 else if (gimple_code (stmt) == GIMPLE_COND)
9662 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
9663 gimple_cond_lhs (stmt),
9664 gimple_cond_rhs (stmt),
9665 stmt);
9666 else
9667 return false;
9669 if (val)
9671 if (assignment_p)
9672 val = fold_convert (gimple_expr_type (stmt), val);
9674 if (dump_file)
9676 fprintf (dump_file, "Folding predicate ");
9677 print_gimple_expr (dump_file, stmt, 0, 0);
9678 fprintf (dump_file, " to ");
9679 print_generic_expr (dump_file, val, 0);
9680 fprintf (dump_file, "\n");
9683 if (is_gimple_assign (stmt))
9684 gimple_assign_set_rhs_from_tree (si, val);
9685 else
9687 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9688 if (integer_zerop (val))
9689 gimple_cond_make_false (stmt);
9690 else if (integer_onep (val))
9691 gimple_cond_make_true (stmt);
9692 else
9693 gcc_unreachable ();
9696 return true;
9699 return false;
9702 /* Callback for substitute_and_fold folding the stmt at *SI. */
9704 static bool
9705 vrp_fold_stmt (gimple_stmt_iterator *si)
9707 if (fold_predicate_in (si))
9708 return true;
9710 return simplify_stmt_using_ranges (si);
9713 /* Stack of dest,src equivalency pairs that need to be restored after
9714 each attempt to thread a block's incoming edge to an outgoing edge.
9716 A NULL entry is used to mark the end of pairs which need to be
9717 restored. */
9718 static vec<tree> equiv_stack;
9720 /* A trivial wrapper so that we can present the generic jump threading
9721 code with a simple API for simplifying statements. STMT is the
9722 statement we want to simplify, WITHIN_STMT provides the location
9723 for any overflow warnings. */
9725 static tree
9726 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9728 if (gimple_code (stmt) == GIMPLE_COND)
9729 return vrp_evaluate_conditional (gimple_cond_code (stmt),
9730 gimple_cond_lhs (stmt),
9731 gimple_cond_rhs (stmt), within_stmt);
9733 if (gimple_code (stmt) == GIMPLE_ASSIGN)
9735 value_range_t new_vr = VR_INITIALIZER;
9736 tree lhs = gimple_assign_lhs (stmt);
9738 if (TREE_CODE (lhs) == SSA_NAME
9739 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
9740 || POINTER_TYPE_P (TREE_TYPE (lhs))))
9742 extract_range_from_assignment (&new_vr, stmt);
9743 if (range_int_cst_singleton_p (&new_vr))
9744 return new_vr.min;
9748 return NULL_TREE;
9751 /* Blocks which have more than one predecessor and more than
9752 one successor present jump threading opportunities, i.e.,
9753 when the block is reached from a specific predecessor, we
9754 may be able to determine which of the outgoing edges will
9755 be traversed. When this optimization applies, we are able
9756 to avoid conditionals at runtime and we may expose secondary
9757 optimization opportunities.
9759 This routine is effectively a driver for the generic jump
9760 threading code. It basically just presents the generic code
9761 with edges that may be suitable for jump threading.
9763 Unlike DOM, we do not iterate VRP if jump threading was successful.
9764 While iterating may expose new opportunities for VRP, it is expected
9765 those opportunities would be very limited and the compile time cost
9766 to expose those opportunities would be significant.
9768 As jump threading opportunities are discovered, they are registered
9769 for later realization. */
9771 static void
9772 identify_jump_threads (void)
9774 basic_block bb;
9775 gimple dummy;
9776 int i;
9777 edge e;
9779 /* Ugh. When substituting values earlier in this pass we can
9780 wipe the dominance information. So rebuild the dominator
9781 information as we need it within the jump threading code. */
9782 calculate_dominance_info (CDI_DOMINATORS);
9784 /* We do not allow VRP information to be used for jump threading
9785 across a back edge in the CFG. Otherwise it becomes too
9786 difficult to avoid eliminating loop exit tests. Of course
9787 EDGE_DFS_BACK is not accurate at this time so we have to
9788 recompute it. */
9789 mark_dfs_back_edges ();
9791 /* Do not thread across edges we are about to remove. Just marking
9792 them as EDGE_DFS_BACK will do. */
9793 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9794 e->flags |= EDGE_DFS_BACK;
9796 /* Allocate our unwinder stack to unwind any temporary equivalences
9797 that might be recorded. */
9798 equiv_stack.create (20);
9800 /* To avoid lots of silly node creation, we create a single
9801 conditional and just modify it in-place when attempting to
9802 thread jumps. */
9803 dummy = gimple_build_cond (EQ_EXPR,
9804 integer_zero_node, integer_zero_node,
9805 NULL, NULL);
9807 /* Walk through all the blocks finding those which present a
9808 potential jump threading opportunity. We could set this up
9809 as a dominator walker and record data during the walk, but
9810 I doubt it's worth the effort for the classes of jump
9811 threading opportunities we are trying to identify at this
9812 point in compilation. */
9813 FOR_EACH_BB_FN (bb, cfun)
9815 gimple last;
9817 /* If the generic jump threading code does not find this block
9818 interesting, then there is nothing to do. */
9819 if (! potentially_threadable_block (bb))
9820 continue;
9822 /* We only care about blocks ending in a COND_EXPR. While there
9823 may be some value in handling SWITCH_EXPR here, I doubt it's
9824 terribly important. */
9825 last = gsi_stmt (gsi_last_bb (bb));
9827 /* We're basically looking for a switch or any kind of conditional with
9828 integral or pointer type arguments. Note the type of the second
9829 argument will be the same as the first argument, so no need to
9830 check it explicitly. */
9831 if (gimple_code (last) == GIMPLE_SWITCH
9832 || (gimple_code (last) == GIMPLE_COND
9833 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9834 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9835 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9836 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9837 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9839 edge_iterator ei;
9841 /* We've got a block with multiple predecessors and multiple
9842 successors which also ends in a suitable conditional or
9843 switch statement. For each predecessor, see if we can thread
9844 it to a specific successor. */
9845 FOR_EACH_EDGE (e, ei, bb->preds)
9847 /* Do not thread across back edges or abnormal edges
9848 in the CFG. */
9849 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9850 continue;
9852 thread_across_edge (dummy, e, true, &equiv_stack,
9853 simplify_stmt_for_jump_threading);
9858 /* We do not actually update the CFG or SSA graphs at this point as
9859 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9860 handle ASSERT_EXPRs gracefully. */
9863 /* We identified all the jump threading opportunities earlier, but could
9864 not transform the CFG at that time. This routine transforms the
9865 CFG and arranges for the dominator tree to be rebuilt if necessary.
9867 Note the SSA graph update will occur during the normal TODO
9868 processing by the pass manager. */
9869 static void
9870 finalize_jump_threads (void)
9872 thread_through_all_blocks (false);
9873 equiv_stack.release ();
9877 /* Traverse all the blocks folding conditionals with known ranges. */
9879 static void
9880 vrp_finalize (void)
9882 size_t i;
9884 values_propagated = true;
9886 if (dump_file)
9888 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9889 dump_all_value_ranges (dump_file);
9890 fprintf (dump_file, "\n");
9893 substitute_and_fold (op_with_constant_singleton_value_range,
9894 vrp_fold_stmt, false);
9896 if (warn_array_bounds)
9897 check_all_array_refs ();
9899 /* We must identify jump threading opportunities before we release
9900 the datastructures built by VRP. */
9901 identify_jump_threads ();
9903 /* Set value range to non pointer SSA_NAMEs. */
9904 for (i = 0; i < num_vr_values; i++)
9905 if (vr_value[i])
9907 tree name = ssa_name (i);
9909 if (!name
9910 || POINTER_TYPE_P (TREE_TYPE (name))
9911 || (vr_value[i]->type == VR_VARYING)
9912 || (vr_value[i]->type == VR_UNDEFINED))
9913 continue;
9915 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
9916 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
9917 && (vr_value[i]->type == VR_RANGE
9918 || vr_value[i]->type == VR_ANTI_RANGE))
9919 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
9920 vr_value[i]->max);
9923 /* Free allocated memory. */
9924 for (i = 0; i < num_vr_values; i++)
9925 if (vr_value[i])
9927 BITMAP_FREE (vr_value[i]->equiv);
9928 free (vr_value[i]);
9931 free (vr_value);
9932 free (vr_phi_edge_counts);
9934 /* So that we can distinguish between VRP data being available
9935 and not available. */
9936 vr_value = NULL;
9937 vr_phi_edge_counts = NULL;
9941 /* Main entry point to VRP (Value Range Propagation). This pass is
9942 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9943 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9944 Programming Language Design and Implementation, pp. 67-78, 1995.
9945 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9947 This is essentially an SSA-CCP pass modified to deal with ranges
9948 instead of constants.
9950 While propagating ranges, we may find that two or more SSA name
9951 have equivalent, though distinct ranges. For instance,
9953 1 x_9 = p_3->a;
9954 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9955 3 if (p_4 == q_2)
9956 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9957 5 endif
9958 6 if (q_2)
9960 In the code above, pointer p_5 has range [q_2, q_2], but from the
9961 code we can also determine that p_5 cannot be NULL and, if q_2 had
9962 a non-varying range, p_5's range should also be compatible with it.
9964 These equivalences are created by two expressions: ASSERT_EXPR and
9965 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9966 result of another assertion, then we can use the fact that p_5 and
9967 p_4 are equivalent when evaluating p_5's range.
9969 Together with value ranges, we also propagate these equivalences
9970 between names so that we can take advantage of information from
9971 multiple ranges when doing final replacement. Note that this
9972 equivalency relation is transitive but not symmetric.
9974 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9975 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9976 in contexts where that assertion does not hold (e.g., in line 6).
9978 TODO, the main difference between this pass and Patterson's is that
9979 we do not propagate edge probabilities. We only compute whether
9980 edges can be taken or not. That is, instead of having a spectrum
9981 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9982 DON'T KNOW. In the future, it may be worthwhile to propagate
9983 probabilities to aid branch prediction. */
9985 static unsigned int
9986 execute_vrp (void)
9988 int i;
9989 edge e;
9990 switch_update *su;
9992 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9993 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9994 scev_initialize ();
9996 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9997 Inserting assertions may split edges which will invalidate
9998 EDGE_DFS_BACK. */
9999 insert_range_assertions ();
10001 to_remove_edges.create (10);
10002 to_update_switch_stmts.create (5);
10003 threadedge_initialize_values ();
10005 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10006 mark_dfs_back_edges ();
10008 vrp_initialize ();
10009 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10010 vrp_finalize ();
10012 free_numbers_of_iterations_estimates ();
10014 /* ASSERT_EXPRs must be removed before finalizing jump threads
10015 as finalizing jump threads calls the CFG cleanup code which
10016 does not properly handle ASSERT_EXPRs. */
10017 remove_range_assertions ();
10019 /* If we exposed any new variables, go ahead and put them into
10020 SSA form now, before we handle jump threading. This simplifies
10021 interactions between rewriting of _DECL nodes into SSA form
10022 and rewriting SSA_NAME nodes into SSA form after block
10023 duplication and CFG manipulation. */
10024 update_ssa (TODO_update_ssa);
10026 finalize_jump_threads ();
10028 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10029 CFG in a broken state and requires a cfg_cleanup run. */
10030 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10031 remove_edge (e);
10032 /* Update SWITCH_EXPR case label vector. */
10033 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10035 size_t j;
10036 size_t n = TREE_VEC_LENGTH (su->vec);
10037 tree label;
10038 gimple_switch_set_num_labels (su->stmt, n);
10039 for (j = 0; j < n; j++)
10040 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10041 /* As we may have replaced the default label with a regular one
10042 make sure to make it a real default label again. This ensures
10043 optimal expansion. */
10044 label = gimple_switch_label (su->stmt, 0);
10045 CASE_LOW (label) = NULL_TREE;
10046 CASE_HIGH (label) = NULL_TREE;
10049 if (to_remove_edges.length () > 0)
10051 free_dominance_info (CDI_DOMINATORS);
10052 loops_state_set (LOOPS_NEED_FIXUP);
10055 to_remove_edges.release ();
10056 to_update_switch_stmts.release ();
10057 threadedge_finalize_values ();
10059 scev_finalize ();
10060 loop_optimizer_finalize ();
10061 return 0;
10064 namespace {
10066 const pass_data pass_data_vrp =
10068 GIMPLE_PASS, /* type */
10069 "vrp", /* name */
10070 OPTGROUP_NONE, /* optinfo_flags */
10071 TV_TREE_VRP, /* tv_id */
10072 PROP_ssa, /* properties_required */
10073 0, /* properties_provided */
10074 0, /* properties_destroyed */
10075 0, /* todo_flags_start */
10076 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10079 class pass_vrp : public gimple_opt_pass
10081 public:
10082 pass_vrp (gcc::context *ctxt)
10083 : gimple_opt_pass (pass_data_vrp, ctxt)
10086 /* opt_pass methods: */
10087 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10088 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10089 virtual unsigned int execute (function *) { return execute_vrp (); }
10091 }; // class pass_vrp
10093 } // anon namespace
10095 gimple_opt_pass *
10096 make_pass_vrp (gcc::context *ctxt)
10098 return new pass_vrp (ctxt);