Allow Objective-c++ to recognise lambdas.
[official-gcc.git] / gcc / tree-vrp.c
blobc49c9424885ca646a3bcfb6e57b38c0f75efee73
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "flags.h"
26 #include "tree.h"
27 #include "stor-layout.h"
28 #include "calls.h"
29 #include "predict.h"
30 #include "vec.h"
31 #include "hashtab.h"
32 #include "hash-set.h"
33 #include "machmode.h"
34 #include "hard-reg-set.h"
35 #include "input.h"
36 #include "function.h"
37 #include "dominance.h"
38 #include "cfg.h"
39 #include "cfganal.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-fold.h"
44 #include "tree-eh.h"
45 #include "gimple-expr.h"
46 #include "is-a.h"
47 #include "gimple.h"
48 #include "gimple-iterator.h"
49 #include "gimple-walk.h"
50 #include "gimple-ssa.h"
51 #include "tree-cfg.h"
52 #include "tree-phinodes.h"
53 #include "ssa-iterators.h"
54 #include "stringpool.h"
55 #include "tree-ssanames.h"
56 #include "tree-ssa-loop-manip.h"
57 #include "tree-ssa-loop-niter.h"
58 #include "tree-ssa-loop.h"
59 #include "tree-into-ssa.h"
60 #include "tree-ssa.h"
61 #include "tree-pass.h"
62 #include "tree-dump.h"
63 #include "gimple-pretty-print.h"
64 #include "diagnostic-core.h"
65 #include "intl.h"
66 #include "cfgloop.h"
67 #include "tree-scalar-evolution.h"
68 #include "tree-ssa-propagate.h"
69 #include "tree-chrec.h"
70 #include "tree-ssa-threadupdate.h"
71 #include "expr.h"
72 #include "insn-codes.h"
73 #include "optabs.h"
74 #include "tree-ssa-threadedge.h"
75 #include "wide-int.h"
79 /* Range of values that can be associated with an SSA_NAME after VRP
80 has executed. */
81 struct value_range_d
83 /* Lattice value represented by this range. */
84 enum value_range_type type;
86 /* Minimum and maximum values represented by this range. These
87 values should be interpreted as follows:
89 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
90 be NULL.
92 - If TYPE == VR_RANGE then MIN holds the minimum value and
93 MAX holds the maximum value of the range [MIN, MAX].
95 - If TYPE == ANTI_RANGE the variable is known to NOT
96 take any values in the range [MIN, MAX]. */
97 tree min;
98 tree max;
100 /* Set of SSA names whose value ranges are equivalent to this one.
101 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
102 bitmap equiv;
105 typedef struct value_range_d value_range_t;
107 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
109 /* Set of SSA names found live during the RPO traversal of the function
110 for still active basic-blocks. */
111 static sbitmap *live;
113 /* Return true if the SSA name NAME is live on the edge E. */
115 static bool
116 live_on_edge (edge e, tree name)
118 return (live[e->dest->index]
119 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
122 /* Local functions. */
123 static int compare_values (tree val1, tree val2);
124 static int compare_values_warnv (tree val1, tree val2, bool *);
125 static void vrp_meet (value_range_t *, value_range_t *);
126 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
127 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
128 tree, tree, bool, bool *,
129 bool *);
131 /* Location information for ASSERT_EXPRs. Each instance of this
132 structure describes an ASSERT_EXPR for an SSA name. Since a single
133 SSA name may have more than one assertion associated with it, these
134 locations are kept in a linked list attached to the corresponding
135 SSA name. */
136 struct assert_locus_d
138 /* Basic block where the assertion would be inserted. */
139 basic_block bb;
141 /* Some assertions need to be inserted on an edge (e.g., assertions
142 generated by COND_EXPRs). In those cases, BB will be NULL. */
143 edge e;
145 /* Pointer to the statement that generated this assertion. */
146 gimple_stmt_iterator si;
148 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
149 enum tree_code comp_code;
151 /* Value being compared against. */
152 tree val;
154 /* Expression to compare. */
155 tree expr;
157 /* Next node in the linked list. */
158 struct assert_locus_d *next;
161 typedef struct assert_locus_d *assert_locus_t;
163 /* If bit I is present, it means that SSA name N_i has a list of
164 assertions that should be inserted in the IL. */
165 static bitmap need_assert_for;
167 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
168 holds a list of ASSERT_LOCUS_T nodes that describe where
169 ASSERT_EXPRs for SSA name N_I should be inserted. */
170 static assert_locus_t *asserts_for;
172 /* Value range array. After propagation, VR_VALUE[I] holds the range
173 of values that SSA name N_I may take. */
174 static unsigned num_vr_values;
175 static value_range_t **vr_value;
176 static bool values_propagated;
178 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
179 number of executable edges we saw the last time we visited the
180 node. */
181 static int *vr_phi_edge_counts;
183 typedef struct {
184 gswitch *stmt;
185 tree vec;
186 } switch_update;
188 static vec<edge> to_remove_edges;
189 static vec<switch_update> to_update_switch_stmts;
192 /* Return the maximum value for TYPE. */
194 static inline tree
195 vrp_val_max (const_tree type)
197 if (!INTEGRAL_TYPE_P (type))
198 return NULL_TREE;
200 return TYPE_MAX_VALUE (type);
203 /* Return the minimum value for TYPE. */
205 static inline tree
206 vrp_val_min (const_tree type)
208 if (!INTEGRAL_TYPE_P (type))
209 return NULL_TREE;
211 return TYPE_MIN_VALUE (type);
214 /* Return whether VAL is equal to the maximum value of its type. This
215 will be true for a positive overflow infinity. We can't do a
216 simple equality comparison with TYPE_MAX_VALUE because C typedefs
217 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
218 to the integer constant with the same value in the type. */
220 static inline bool
221 vrp_val_is_max (const_tree val)
223 tree type_max = vrp_val_max (TREE_TYPE (val));
224 return (val == type_max
225 || (type_max != NULL_TREE
226 && operand_equal_p (val, type_max, 0)));
229 /* Return whether VAL is equal to the minimum value of its type. This
230 will be true for a negative overflow infinity. */
232 static inline bool
233 vrp_val_is_min (const_tree val)
235 tree type_min = vrp_val_min (TREE_TYPE (val));
236 return (val == type_min
237 || (type_min != NULL_TREE
238 && operand_equal_p (val, type_min, 0)));
242 /* Return whether TYPE should use an overflow infinity distinct from
243 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
244 represent a signed overflow during VRP computations. An infinity
245 is distinct from a half-range, which will go from some number to
246 TYPE_{MIN,MAX}_VALUE. */
248 static inline bool
249 needs_overflow_infinity (const_tree type)
251 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
254 /* Return whether TYPE can support our overflow infinity
255 representation: we use the TREE_OVERFLOW flag, which only exists
256 for constants. If TYPE doesn't support this, we don't optimize
257 cases which would require signed overflow--we drop them to
258 VARYING. */
260 static inline bool
261 supports_overflow_infinity (const_tree type)
263 tree min = vrp_val_min (type), max = vrp_val_max (type);
264 #ifdef ENABLE_CHECKING
265 gcc_assert (needs_overflow_infinity (type));
266 #endif
267 return (min != NULL_TREE
268 && CONSTANT_CLASS_P (min)
269 && max != NULL_TREE
270 && CONSTANT_CLASS_P (max));
273 /* VAL is the maximum or minimum value of a type. Return a
274 corresponding overflow infinity. */
276 static inline tree
277 make_overflow_infinity (tree val)
279 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
280 val = copy_node (val);
281 TREE_OVERFLOW (val) = 1;
282 return val;
285 /* Return a negative overflow infinity for TYPE. */
287 static inline tree
288 negative_overflow_infinity (tree type)
290 gcc_checking_assert (supports_overflow_infinity (type));
291 return make_overflow_infinity (vrp_val_min (type));
294 /* Return a positive overflow infinity for TYPE. */
296 static inline tree
297 positive_overflow_infinity (tree type)
299 gcc_checking_assert (supports_overflow_infinity (type));
300 return make_overflow_infinity (vrp_val_max (type));
303 /* Return whether VAL is a negative overflow infinity. */
305 static inline bool
306 is_negative_overflow_infinity (const_tree val)
308 return (TREE_OVERFLOW_P (val)
309 && needs_overflow_infinity (TREE_TYPE (val))
310 && vrp_val_is_min (val));
313 /* Return whether VAL is a positive overflow infinity. */
315 static inline bool
316 is_positive_overflow_infinity (const_tree val)
318 return (TREE_OVERFLOW_P (val)
319 && needs_overflow_infinity (TREE_TYPE (val))
320 && vrp_val_is_max (val));
323 /* Return whether VAL is a positive or negative overflow infinity. */
325 static inline bool
326 is_overflow_infinity (const_tree val)
328 return (TREE_OVERFLOW_P (val)
329 && needs_overflow_infinity (TREE_TYPE (val))
330 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
333 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
335 static inline bool
336 stmt_overflow_infinity (gimple stmt)
338 if (is_gimple_assign (stmt)
339 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
340 GIMPLE_SINGLE_RHS)
341 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
342 return false;
345 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
346 the same value with TREE_OVERFLOW clear. This can be used to avoid
347 confusing a regular value with an overflow value. */
349 static inline tree
350 avoid_overflow_infinity (tree val)
352 if (!is_overflow_infinity (val))
353 return val;
355 if (vrp_val_is_max (val))
356 return vrp_val_max (TREE_TYPE (val));
357 else
359 gcc_checking_assert (vrp_val_is_min (val));
360 return vrp_val_min (TREE_TYPE (val));
365 /* Return true if ARG is marked with the nonnull attribute in the
366 current function signature. */
368 static bool
369 nonnull_arg_p (const_tree arg)
371 tree t, attrs, fntype;
372 unsigned HOST_WIDE_INT arg_num;
374 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
376 /* The static chain decl is always non null. */
377 if (arg == cfun->static_chain_decl)
378 return true;
380 fntype = TREE_TYPE (current_function_decl);
381 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
383 attrs = lookup_attribute ("nonnull", attrs);
385 /* If "nonnull" wasn't specified, we know nothing about the argument. */
386 if (attrs == NULL_TREE)
387 return false;
389 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
390 if (TREE_VALUE (attrs) == NULL_TREE)
391 return true;
393 /* Get the position number for ARG in the function signature. */
394 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
396 t = DECL_CHAIN (t), arg_num++)
398 if (t == arg)
399 break;
402 gcc_assert (t == arg);
404 /* Now see if ARG_NUM is mentioned in the nonnull list. */
405 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
407 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
408 return true;
412 return false;
416 /* Set value range VR to VR_UNDEFINED. */
418 static inline void
419 set_value_range_to_undefined (value_range_t *vr)
421 vr->type = VR_UNDEFINED;
422 vr->min = vr->max = NULL_TREE;
423 if (vr->equiv)
424 bitmap_clear (vr->equiv);
428 /* Set value range VR to VR_VARYING. */
430 static inline void
431 set_value_range_to_varying (value_range_t *vr)
433 vr->type = VR_VARYING;
434 vr->min = vr->max = NULL_TREE;
435 if (vr->equiv)
436 bitmap_clear (vr->equiv);
440 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
442 static void
443 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
444 tree max, bitmap equiv)
446 #if defined ENABLE_CHECKING
447 /* Check the validity of the range. */
448 if (t == VR_RANGE || t == VR_ANTI_RANGE)
450 int cmp;
452 gcc_assert (min && max);
454 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
455 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
457 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
458 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
460 cmp = compare_values (min, max);
461 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
463 if (needs_overflow_infinity (TREE_TYPE (min)))
464 gcc_assert (!is_overflow_infinity (min)
465 || !is_overflow_infinity (max));
468 if (t == VR_UNDEFINED || t == VR_VARYING)
469 gcc_assert (min == NULL_TREE && max == NULL_TREE);
471 if (t == VR_UNDEFINED || t == VR_VARYING)
472 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
473 #endif
475 vr->type = t;
476 vr->min = min;
477 vr->max = max;
479 /* Since updating the equivalence set involves deep copying the
480 bitmaps, only do it if absolutely necessary. */
481 if (vr->equiv == NULL
482 && equiv != NULL)
483 vr->equiv = BITMAP_ALLOC (NULL);
485 if (equiv != vr->equiv)
487 if (equiv && !bitmap_empty_p (equiv))
488 bitmap_copy (vr->equiv, equiv);
489 else
490 bitmap_clear (vr->equiv);
495 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
496 This means adjusting T, MIN and MAX representing the case of a
497 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
498 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
499 In corner cases where MAX+1 or MIN-1 wraps this will fall back
500 to varying.
501 This routine exists to ease canonicalization in the case where we
502 extract ranges from var + CST op limit. */
504 static void
505 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
506 tree min, tree max, bitmap equiv)
508 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
509 if (t == VR_UNDEFINED)
511 set_value_range_to_undefined (vr);
512 return;
514 else if (t == VR_VARYING)
516 set_value_range_to_varying (vr);
517 return;
520 /* Nothing to canonicalize for symbolic ranges. */
521 if (TREE_CODE (min) != INTEGER_CST
522 || TREE_CODE (max) != INTEGER_CST)
524 set_value_range (vr, t, min, max, equiv);
525 return;
528 /* Wrong order for min and max, to swap them and the VR type we need
529 to adjust them. */
530 if (tree_int_cst_lt (max, min))
532 tree one, tmp;
534 /* For one bit precision if max < min, then the swapped
535 range covers all values, so for VR_RANGE it is varying and
536 for VR_ANTI_RANGE empty range, so drop to varying as well. */
537 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
539 set_value_range_to_varying (vr);
540 return;
543 one = build_int_cst (TREE_TYPE (min), 1);
544 tmp = int_const_binop (PLUS_EXPR, max, one);
545 max = int_const_binop (MINUS_EXPR, min, one);
546 min = tmp;
548 /* There's one corner case, if we had [C+1, C] before we now have
549 that again. But this represents an empty value range, so drop
550 to varying in this case. */
551 if (tree_int_cst_lt (max, min))
553 set_value_range_to_varying (vr);
554 return;
557 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
560 /* Anti-ranges that can be represented as ranges should be so. */
561 if (t == VR_ANTI_RANGE)
563 bool is_min = vrp_val_is_min (min);
564 bool is_max = vrp_val_is_max (max);
566 if (is_min && is_max)
568 /* We cannot deal with empty ranges, drop to varying.
569 ??? This could be VR_UNDEFINED instead. */
570 set_value_range_to_varying (vr);
571 return;
573 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
574 && (is_min || is_max))
576 /* Non-empty boolean ranges can always be represented
577 as a singleton range. */
578 if (is_min)
579 min = max = vrp_val_max (TREE_TYPE (min));
580 else
581 min = max = vrp_val_min (TREE_TYPE (min));
582 t = VR_RANGE;
584 else if (is_min
585 /* As a special exception preserve non-null ranges. */
586 && !(TYPE_UNSIGNED (TREE_TYPE (min))
587 && integer_zerop (max)))
589 tree one = build_int_cst (TREE_TYPE (max), 1);
590 min = int_const_binop (PLUS_EXPR, max, one);
591 max = vrp_val_max (TREE_TYPE (max));
592 t = VR_RANGE;
594 else if (is_max)
596 tree one = build_int_cst (TREE_TYPE (min), 1);
597 max = int_const_binop (MINUS_EXPR, min, one);
598 min = vrp_val_min (TREE_TYPE (min));
599 t = VR_RANGE;
603 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
604 if (needs_overflow_infinity (TREE_TYPE (min))
605 && is_overflow_infinity (min)
606 && is_overflow_infinity (max))
608 set_value_range_to_varying (vr);
609 return;
612 set_value_range (vr, t, min, max, equiv);
615 /* Copy value range FROM into value range TO. */
617 static inline void
618 copy_value_range (value_range_t *to, value_range_t *from)
620 set_value_range (to, from->type, from->min, from->max, from->equiv);
623 /* Set value range VR to a single value. This function is only called
624 with values we get from statements, and exists to clear the
625 TREE_OVERFLOW flag so that we don't think we have an overflow
626 infinity when we shouldn't. */
628 static inline void
629 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
631 gcc_assert (is_gimple_min_invariant (val));
632 if (TREE_OVERFLOW_P (val))
633 val = drop_tree_overflow (val);
634 set_value_range (vr, VR_RANGE, val, val, equiv);
637 /* Set value range VR to a non-negative range of type TYPE.
638 OVERFLOW_INFINITY indicates whether to use an overflow infinity
639 rather than TYPE_MAX_VALUE; this should be true if we determine
640 that the range is nonnegative based on the assumption that signed
641 overflow does not occur. */
643 static inline void
644 set_value_range_to_nonnegative (value_range_t *vr, tree type,
645 bool overflow_infinity)
647 tree zero;
649 if (overflow_infinity && !supports_overflow_infinity (type))
651 set_value_range_to_varying (vr);
652 return;
655 zero = build_int_cst (type, 0);
656 set_value_range (vr, VR_RANGE, zero,
657 (overflow_infinity
658 ? positive_overflow_infinity (type)
659 : TYPE_MAX_VALUE (type)),
660 vr->equiv);
663 /* Set value range VR to a non-NULL range of type TYPE. */
665 static inline void
666 set_value_range_to_nonnull (value_range_t *vr, tree type)
668 tree zero = build_int_cst (type, 0);
669 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
673 /* Set value range VR to a NULL range of type TYPE. */
675 static inline void
676 set_value_range_to_null (value_range_t *vr, tree type)
678 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
682 /* Set value range VR to a range of a truthvalue of type TYPE. */
684 static inline void
685 set_value_range_to_truthvalue (value_range_t *vr, tree type)
687 if (TYPE_PRECISION (type) == 1)
688 set_value_range_to_varying (vr);
689 else
690 set_value_range (vr, VR_RANGE,
691 build_int_cst (type, 0), build_int_cst (type, 1),
692 vr->equiv);
696 /* If abs (min) < abs (max), set VR to [-max, max], if
697 abs (min) >= abs (max), set VR to [-min, min]. */
699 static void
700 abs_extent_range (value_range_t *vr, tree min, tree max)
702 int cmp;
704 gcc_assert (TREE_CODE (min) == INTEGER_CST);
705 gcc_assert (TREE_CODE (max) == INTEGER_CST);
706 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
707 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
708 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
709 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
710 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
712 set_value_range_to_varying (vr);
713 return;
715 cmp = compare_values (min, max);
716 if (cmp == -1)
717 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
718 else if (cmp == 0 || cmp == 1)
720 max = min;
721 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
723 else
725 set_value_range_to_varying (vr);
726 return;
728 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
732 /* Return value range information for VAR.
734 If we have no values ranges recorded (ie, VRP is not running), then
735 return NULL. Otherwise create an empty range if none existed for VAR. */
737 static value_range_t *
738 get_value_range (const_tree var)
740 static const struct value_range_d vr_const_varying
741 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
742 value_range_t *vr;
743 tree sym;
744 unsigned ver = SSA_NAME_VERSION (var);
746 /* If we have no recorded ranges, then return NULL. */
747 if (! vr_value)
748 return NULL;
750 /* If we query the range for a new SSA name return an unmodifiable VARYING.
751 We should get here at most from the substitute-and-fold stage which
752 will never try to change values. */
753 if (ver >= num_vr_values)
754 return CONST_CAST (value_range_t *, &vr_const_varying);
756 vr = vr_value[ver];
757 if (vr)
758 return vr;
760 /* After propagation finished do not allocate new value-ranges. */
761 if (values_propagated)
762 return CONST_CAST (value_range_t *, &vr_const_varying);
764 /* Create a default value range. */
765 vr_value[ver] = vr = XCNEW (value_range_t);
767 /* Defer allocating the equivalence set. */
768 vr->equiv = NULL;
770 /* If VAR is a default definition of a parameter, the variable can
771 take any value in VAR's type. */
772 if (SSA_NAME_IS_DEFAULT_DEF (var))
774 sym = SSA_NAME_VAR (var);
775 if (TREE_CODE (sym) == PARM_DECL)
777 /* Try to use the "nonnull" attribute to create ~[0, 0]
778 anti-ranges for pointers. Note that this is only valid with
779 default definitions of PARM_DECLs. */
780 if (POINTER_TYPE_P (TREE_TYPE (sym))
781 && nonnull_arg_p (sym))
782 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
783 else
784 set_value_range_to_varying (vr);
786 else if (TREE_CODE (sym) == RESULT_DECL
787 && DECL_BY_REFERENCE (sym))
788 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
791 return vr;
794 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
796 static inline bool
797 vrp_operand_equal_p (const_tree val1, const_tree val2)
799 if (val1 == val2)
800 return true;
801 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
802 return false;
803 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
806 /* Return true, if the bitmaps B1 and B2 are equal. */
808 static inline bool
809 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
811 return (b1 == b2
812 || ((!b1 || bitmap_empty_p (b1))
813 && (!b2 || bitmap_empty_p (b2)))
814 || (b1 && b2
815 && bitmap_equal_p (b1, b2)));
818 /* Update the value range and equivalence set for variable VAR to
819 NEW_VR. Return true if NEW_VR is different from VAR's previous
820 value.
822 NOTE: This function assumes that NEW_VR is a temporary value range
823 object created for the sole purpose of updating VAR's range. The
824 storage used by the equivalence set from NEW_VR will be freed by
825 this function. Do not call update_value_range when NEW_VR
826 is the range object associated with another SSA name. */
828 static inline bool
829 update_value_range (const_tree var, value_range_t *new_vr)
831 value_range_t *old_vr;
832 bool is_new;
834 /* Update the value range, if necessary. */
835 old_vr = get_value_range (var);
836 is_new = old_vr->type != new_vr->type
837 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
838 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
839 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
841 if (is_new)
843 /* Do not allow transitions up the lattice. The following
844 is slightly more awkward than just new_vr->type < old_vr->type
845 because VR_RANGE and VR_ANTI_RANGE need to be considered
846 the same. We may not have is_new when transitioning to
847 UNDEFINED or from VARYING. */
848 if (new_vr->type == VR_UNDEFINED
849 || old_vr->type == VR_VARYING)
850 set_value_range_to_varying (old_vr);
851 else
852 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
853 new_vr->equiv);
856 BITMAP_FREE (new_vr->equiv);
858 return is_new;
862 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
863 point where equivalence processing can be turned on/off. */
865 static void
866 add_equivalence (bitmap *equiv, const_tree var)
868 unsigned ver = SSA_NAME_VERSION (var);
869 value_range_t *vr = vr_value[ver];
871 if (*equiv == NULL)
872 *equiv = BITMAP_ALLOC (NULL);
873 bitmap_set_bit (*equiv, ver);
874 if (vr && vr->equiv)
875 bitmap_ior_into (*equiv, vr->equiv);
879 /* Return true if VR is ~[0, 0]. */
881 static inline bool
882 range_is_nonnull (value_range_t *vr)
884 return vr->type == VR_ANTI_RANGE
885 && integer_zerop (vr->min)
886 && integer_zerop (vr->max);
890 /* Return true if VR is [0, 0]. */
892 static inline bool
893 range_is_null (value_range_t *vr)
895 return vr->type == VR_RANGE
896 && integer_zerop (vr->min)
897 && integer_zerop (vr->max);
900 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
901 a singleton. */
903 static inline bool
904 range_int_cst_p (value_range_t *vr)
906 return (vr->type == VR_RANGE
907 && TREE_CODE (vr->max) == INTEGER_CST
908 && TREE_CODE (vr->min) == INTEGER_CST);
911 /* Return true if VR is a INTEGER_CST singleton. */
913 static inline bool
914 range_int_cst_singleton_p (value_range_t *vr)
916 return (range_int_cst_p (vr)
917 && !is_overflow_infinity (vr->min)
918 && !is_overflow_infinity (vr->max)
919 && tree_int_cst_equal (vr->min, vr->max));
922 /* Return true if value range VR involves at least one symbol. */
924 static inline bool
925 symbolic_range_p (value_range_t *vr)
927 return (!is_gimple_min_invariant (vr->min)
928 || !is_gimple_min_invariant (vr->max));
931 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
932 otherwise. We only handle additive operations and set NEG to true if the
933 symbol is negated and INV to the invariant part, if any. */
935 static tree
936 get_single_symbol (tree t, bool *neg, tree *inv)
938 bool neg_;
939 tree inv_;
941 if (TREE_CODE (t) == PLUS_EXPR
942 || TREE_CODE (t) == POINTER_PLUS_EXPR
943 || TREE_CODE (t) == MINUS_EXPR)
945 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
947 neg_ = (TREE_CODE (t) == MINUS_EXPR);
948 inv_ = TREE_OPERAND (t, 0);
949 t = TREE_OPERAND (t, 1);
951 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
953 neg_ = false;
954 inv_ = TREE_OPERAND (t, 1);
955 t = TREE_OPERAND (t, 0);
957 else
958 return NULL_TREE;
960 else
962 neg_ = false;
963 inv_ = NULL_TREE;
966 if (TREE_CODE (t) == NEGATE_EXPR)
968 t = TREE_OPERAND (t, 0);
969 neg_ = !neg_;
972 if (TREE_CODE (t) != SSA_NAME)
973 return NULL_TREE;
975 *neg = neg_;
976 *inv = inv_;
977 return t;
980 /* The reverse operation: build a symbolic expression with TYPE
981 from symbol SYM, negated according to NEG, and invariant INV. */
983 static tree
984 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
986 const bool pointer_p = POINTER_TYPE_P (type);
987 tree t = sym;
989 if (neg)
990 t = build1 (NEGATE_EXPR, type, t);
992 if (integer_zerop (inv))
993 return t;
995 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
998 /* Return true if value range VR involves exactly one symbol SYM. */
1000 static bool
1001 symbolic_range_based_on_p (value_range_t *vr, const_tree sym)
1003 bool neg, min_has_symbol, max_has_symbol;
1004 tree inv;
1006 if (is_gimple_min_invariant (vr->min))
1007 min_has_symbol = false;
1008 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
1009 min_has_symbol = true;
1010 else
1011 return false;
1013 if (is_gimple_min_invariant (vr->max))
1014 max_has_symbol = false;
1015 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
1016 max_has_symbol = true;
1017 else
1018 return false;
1020 return (min_has_symbol || max_has_symbol);
1023 /* Return true if value range VR uses an overflow infinity. */
1025 static inline bool
1026 overflow_infinity_range_p (value_range_t *vr)
1028 return (vr->type == VR_RANGE
1029 && (is_overflow_infinity (vr->min)
1030 || is_overflow_infinity (vr->max)));
1033 /* Return false if we can not make a valid comparison based on VR;
1034 this will be the case if it uses an overflow infinity and overflow
1035 is not undefined (i.e., -fno-strict-overflow is in effect).
1036 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1037 uses an overflow infinity. */
1039 static bool
1040 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
1042 gcc_assert (vr->type == VR_RANGE);
1043 if (is_overflow_infinity (vr->min))
1045 *strict_overflow_p = true;
1046 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1047 return false;
1049 if (is_overflow_infinity (vr->max))
1051 *strict_overflow_p = true;
1052 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1053 return false;
1055 return true;
1059 /* Return true if the result of assignment STMT is know to be non-negative.
1060 If the return value is based on the assumption that signed overflow is
1061 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1062 *STRICT_OVERFLOW_P.*/
1064 static bool
1065 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1067 enum tree_code code = gimple_assign_rhs_code (stmt);
1068 switch (get_gimple_rhs_class (code))
1070 case GIMPLE_UNARY_RHS:
1071 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1072 gimple_expr_type (stmt),
1073 gimple_assign_rhs1 (stmt),
1074 strict_overflow_p);
1075 case GIMPLE_BINARY_RHS:
1076 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1077 gimple_expr_type (stmt),
1078 gimple_assign_rhs1 (stmt),
1079 gimple_assign_rhs2 (stmt),
1080 strict_overflow_p);
1081 case GIMPLE_TERNARY_RHS:
1082 return false;
1083 case GIMPLE_SINGLE_RHS:
1084 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
1085 strict_overflow_p);
1086 case GIMPLE_INVALID_RHS:
1087 gcc_unreachable ();
1088 default:
1089 gcc_unreachable ();
1093 /* Return true if return value of call STMT is know to be non-negative.
1094 If the return value is based on the assumption that signed overflow is
1095 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1096 *STRICT_OVERFLOW_P.*/
1098 static bool
1099 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1101 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1102 gimple_call_arg (stmt, 0) : NULL_TREE;
1103 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1104 gimple_call_arg (stmt, 1) : NULL_TREE;
1106 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1107 gimple_call_fndecl (stmt),
1108 arg0,
1109 arg1,
1110 strict_overflow_p);
1113 /* Return true if STMT is know to to compute a non-negative value.
1114 If the return value is based on the assumption that signed overflow is
1115 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1116 *STRICT_OVERFLOW_P.*/
1118 static bool
1119 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1121 switch (gimple_code (stmt))
1123 case GIMPLE_ASSIGN:
1124 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1125 case GIMPLE_CALL:
1126 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1127 default:
1128 gcc_unreachable ();
1132 /* Return true if the result of assignment STMT is know to be non-zero.
1133 If the return value is based on the assumption that signed overflow is
1134 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1135 *STRICT_OVERFLOW_P.*/
1137 static bool
1138 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1140 enum tree_code code = gimple_assign_rhs_code (stmt);
1141 switch (get_gimple_rhs_class (code))
1143 case GIMPLE_UNARY_RHS:
1144 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1145 gimple_expr_type (stmt),
1146 gimple_assign_rhs1 (stmt),
1147 strict_overflow_p);
1148 case GIMPLE_BINARY_RHS:
1149 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1150 gimple_expr_type (stmt),
1151 gimple_assign_rhs1 (stmt),
1152 gimple_assign_rhs2 (stmt),
1153 strict_overflow_p);
1154 case GIMPLE_TERNARY_RHS:
1155 return false;
1156 case GIMPLE_SINGLE_RHS:
1157 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1158 strict_overflow_p);
1159 case GIMPLE_INVALID_RHS:
1160 gcc_unreachable ();
1161 default:
1162 gcc_unreachable ();
1166 /* Return true if STMT is known to compute a non-zero value.
1167 If the return value is based on the assumption that signed overflow is
1168 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1169 *STRICT_OVERFLOW_P.*/
1171 static bool
1172 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1174 switch (gimple_code (stmt))
1176 case GIMPLE_ASSIGN:
1177 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1178 case GIMPLE_CALL:
1180 tree fndecl = gimple_call_fndecl (stmt);
1181 if (!fndecl) return false;
1182 if (flag_delete_null_pointer_checks && !flag_check_new
1183 && DECL_IS_OPERATOR_NEW (fndecl)
1184 && !TREE_NOTHROW (fndecl))
1185 return true;
1186 if (flag_delete_null_pointer_checks &&
1187 lookup_attribute ("returns_nonnull",
1188 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1189 return true;
1190 return gimple_alloca_call_p (stmt);
1192 default:
1193 gcc_unreachable ();
1197 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1198 obtained so far. */
1200 static bool
1201 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1203 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1204 return true;
1206 /* If we have an expression of the form &X->a, then the expression
1207 is nonnull if X is nonnull. */
1208 if (is_gimple_assign (stmt)
1209 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1211 tree expr = gimple_assign_rhs1 (stmt);
1212 tree base = get_base_address (TREE_OPERAND (expr, 0));
1214 if (base != NULL_TREE
1215 && TREE_CODE (base) == MEM_REF
1216 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1218 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1219 if (range_is_nonnull (vr))
1220 return true;
1224 return false;
1227 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1228 a gimple invariant, or SSA_NAME +- CST. */
1230 static bool
1231 valid_value_p (tree expr)
1233 if (TREE_CODE (expr) == SSA_NAME)
1234 return true;
1236 if (TREE_CODE (expr) == PLUS_EXPR
1237 || TREE_CODE (expr) == MINUS_EXPR)
1238 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1239 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1241 return is_gimple_min_invariant (expr);
1244 /* Return
1245 1 if VAL < VAL2
1246 0 if !(VAL < VAL2)
1247 -2 if those are incomparable. */
1248 static inline int
1249 operand_less_p (tree val, tree val2)
1251 /* LT is folded faster than GE and others. Inline the common case. */
1252 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1253 return tree_int_cst_lt (val, val2);
1254 else
1256 tree tcmp;
1258 fold_defer_overflow_warnings ();
1260 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1262 fold_undefer_and_ignore_overflow_warnings ();
1264 if (!tcmp
1265 || TREE_CODE (tcmp) != INTEGER_CST)
1266 return -2;
1268 if (!integer_zerop (tcmp))
1269 return 1;
1272 /* val >= val2, not considering overflow infinity. */
1273 if (is_negative_overflow_infinity (val))
1274 return is_negative_overflow_infinity (val2) ? 0 : 1;
1275 else if (is_positive_overflow_infinity (val2))
1276 return is_positive_overflow_infinity (val) ? 0 : 1;
1278 return 0;
1281 /* Compare two values VAL1 and VAL2. Return
1283 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1284 -1 if VAL1 < VAL2,
1285 0 if VAL1 == VAL2,
1286 +1 if VAL1 > VAL2, and
1287 +2 if VAL1 != VAL2
1289 This is similar to tree_int_cst_compare but supports pointer values
1290 and values that cannot be compared at compile time.
1292 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1293 true if the return value is only valid if we assume that signed
1294 overflow is undefined. */
1296 static int
1297 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1299 if (val1 == val2)
1300 return 0;
1302 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1303 both integers. */
1304 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1305 == POINTER_TYPE_P (TREE_TYPE (val2)));
1307 /* Convert the two values into the same type. This is needed because
1308 sizetype causes sign extension even for unsigned types. */
1309 val2 = fold_convert (TREE_TYPE (val1), val2);
1310 STRIP_USELESS_TYPE_CONVERSION (val2);
1312 if ((TREE_CODE (val1) == SSA_NAME
1313 || (TREE_CODE (val1) == NEGATE_EXPR
1314 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1315 || TREE_CODE (val1) == PLUS_EXPR
1316 || TREE_CODE (val1) == MINUS_EXPR)
1317 && (TREE_CODE (val2) == SSA_NAME
1318 || (TREE_CODE (val2) == NEGATE_EXPR
1319 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1320 || TREE_CODE (val2) == PLUS_EXPR
1321 || TREE_CODE (val2) == MINUS_EXPR))
1323 tree n1, c1, n2, c2;
1324 enum tree_code code1, code2;
1326 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1327 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1328 same name, return -2. */
1329 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1331 code1 = SSA_NAME;
1332 n1 = val1;
1333 c1 = NULL_TREE;
1335 else
1337 code1 = TREE_CODE (val1);
1338 n1 = TREE_OPERAND (val1, 0);
1339 c1 = TREE_OPERAND (val1, 1);
1340 if (tree_int_cst_sgn (c1) == -1)
1342 if (is_negative_overflow_infinity (c1))
1343 return -2;
1344 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1345 if (!c1)
1346 return -2;
1347 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1351 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1353 code2 = SSA_NAME;
1354 n2 = val2;
1355 c2 = NULL_TREE;
1357 else
1359 code2 = TREE_CODE (val2);
1360 n2 = TREE_OPERAND (val2, 0);
1361 c2 = TREE_OPERAND (val2, 1);
1362 if (tree_int_cst_sgn (c2) == -1)
1364 if (is_negative_overflow_infinity (c2))
1365 return -2;
1366 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1367 if (!c2)
1368 return -2;
1369 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1373 /* Both values must use the same name. */
1374 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1376 n1 = TREE_OPERAND (n1, 0);
1377 n2 = TREE_OPERAND (n2, 0);
1379 if (n1 != n2)
1380 return -2;
1382 if (code1 == SSA_NAME && code2 == SSA_NAME)
1383 /* NAME == NAME */
1384 return 0;
1386 /* If overflow is defined we cannot simplify more. */
1387 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1388 return -2;
1390 if (strict_overflow_p != NULL
1391 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1392 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1393 *strict_overflow_p = true;
1395 if (code1 == SSA_NAME)
1397 if (code2 == PLUS_EXPR)
1398 /* NAME < NAME + CST */
1399 return -1;
1400 else if (code2 == MINUS_EXPR)
1401 /* NAME > NAME - CST */
1402 return 1;
1404 else if (code1 == PLUS_EXPR)
1406 if (code2 == SSA_NAME)
1407 /* NAME + CST > NAME */
1408 return 1;
1409 else if (code2 == PLUS_EXPR)
1410 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1411 return compare_values_warnv (c1, c2, strict_overflow_p);
1412 else if (code2 == MINUS_EXPR)
1413 /* NAME + CST1 > NAME - CST2 */
1414 return 1;
1416 else if (code1 == MINUS_EXPR)
1418 if (code2 == SSA_NAME)
1419 /* NAME - CST < NAME */
1420 return -1;
1421 else if (code2 == PLUS_EXPR)
1422 /* NAME - CST1 < NAME + CST2 */
1423 return -1;
1424 else if (code2 == MINUS_EXPR)
1425 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1426 C1 and C2 are swapped in the call to compare_values. */
1427 return compare_values_warnv (c2, c1, strict_overflow_p);
1430 gcc_unreachable ();
1433 /* We cannot compare non-constants. */
1434 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1435 return -2;
1437 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1439 /* We cannot compare overflowed values, except for overflow
1440 infinities. */
1441 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1443 if (strict_overflow_p != NULL)
1444 *strict_overflow_p = true;
1445 if (is_negative_overflow_infinity (val1))
1446 return is_negative_overflow_infinity (val2) ? 0 : -1;
1447 else if (is_negative_overflow_infinity (val2))
1448 return 1;
1449 else if (is_positive_overflow_infinity (val1))
1450 return is_positive_overflow_infinity (val2) ? 0 : 1;
1451 else if (is_positive_overflow_infinity (val2))
1452 return -1;
1453 return -2;
1456 return tree_int_cst_compare (val1, val2);
1458 else
1460 tree t;
1462 /* First see if VAL1 and VAL2 are not the same. */
1463 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1464 return 0;
1466 /* If VAL1 is a lower address than VAL2, return -1. */
1467 if (operand_less_p (val1, val2) == 1)
1468 return -1;
1470 /* If VAL1 is a higher address than VAL2, return +1. */
1471 if (operand_less_p (val2, val1) == 1)
1472 return 1;
1474 /* If VAL1 is different than VAL2, return +2.
1475 For integer constants we either have already returned -1 or 1
1476 or they are equivalent. We still might succeed in proving
1477 something about non-trivial operands. */
1478 if (TREE_CODE (val1) != INTEGER_CST
1479 || TREE_CODE (val2) != INTEGER_CST)
1481 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1482 if (t && integer_onep (t))
1483 return 2;
1486 return -2;
1490 /* Compare values like compare_values_warnv, but treat comparisons of
1491 nonconstants which rely on undefined overflow as incomparable. */
1493 static int
1494 compare_values (tree val1, tree val2)
1496 bool sop;
1497 int ret;
1499 sop = false;
1500 ret = compare_values_warnv (val1, val2, &sop);
1501 if (sop
1502 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1503 ret = -2;
1504 return ret;
1508 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1509 0 if VAL is not inside [MIN, MAX],
1510 -2 if we cannot tell either way.
1512 Benchmark compile/20001226-1.c compilation time after changing this
1513 function. */
1515 static inline int
1516 value_inside_range (tree val, tree min, tree max)
1518 int cmp1, cmp2;
1520 cmp1 = operand_less_p (val, min);
1521 if (cmp1 == -2)
1522 return -2;
1523 if (cmp1 == 1)
1524 return 0;
1526 cmp2 = operand_less_p (max, val);
1527 if (cmp2 == -2)
1528 return -2;
1530 return !cmp2;
1534 /* Return true if value ranges VR0 and VR1 have a non-empty
1535 intersection.
1537 Benchmark compile/20001226-1.c compilation time after changing this
1538 function.
1541 static inline bool
1542 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1544 /* The value ranges do not intersect if the maximum of the first range is
1545 less than the minimum of the second range or vice versa.
1546 When those relations are unknown, we can't do any better. */
1547 if (operand_less_p (vr0->max, vr1->min) != 0)
1548 return false;
1549 if (operand_less_p (vr1->max, vr0->min) != 0)
1550 return false;
1551 return true;
1555 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1556 include the value zero, -2 if we cannot tell. */
1558 static inline int
1559 range_includes_zero_p (tree min, tree max)
1561 tree zero = build_int_cst (TREE_TYPE (min), 0);
1562 return value_inside_range (zero, min, max);
1565 /* Return true if *VR is know to only contain nonnegative values. */
1567 static inline bool
1568 value_range_nonnegative_p (value_range_t *vr)
1570 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1571 which would return a useful value should be encoded as a
1572 VR_RANGE. */
1573 if (vr->type == VR_RANGE)
1575 int result = compare_values (vr->min, integer_zero_node);
1576 return (result == 0 || result == 1);
1579 return false;
1582 /* If *VR has a value rante that is a single constant value return that,
1583 otherwise return NULL_TREE. */
1585 static tree
1586 value_range_constant_singleton (value_range_t *vr)
1588 if (vr->type == VR_RANGE
1589 && operand_equal_p (vr->min, vr->max, 0)
1590 && is_gimple_min_invariant (vr->min))
1591 return vr->min;
1593 return NULL_TREE;
1596 /* If OP has a value range with a single constant value return that,
1597 otherwise return NULL_TREE. This returns OP itself if OP is a
1598 constant. */
1600 static tree
1601 op_with_constant_singleton_value_range (tree op)
1603 if (is_gimple_min_invariant (op))
1604 return op;
1606 if (TREE_CODE (op) != SSA_NAME)
1607 return NULL_TREE;
1609 return value_range_constant_singleton (get_value_range (op));
1612 /* Return true if op is in a boolean [0, 1] value-range. */
1614 static bool
1615 op_with_boolean_value_range_p (tree op)
1617 value_range_t *vr;
1619 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1620 return true;
1622 if (integer_zerop (op)
1623 || integer_onep (op))
1624 return true;
1626 if (TREE_CODE (op) != SSA_NAME)
1627 return false;
1629 vr = get_value_range (op);
1630 return (vr->type == VR_RANGE
1631 && integer_zerop (vr->min)
1632 && integer_onep (vr->max));
1635 /* Extract value range information from an ASSERT_EXPR EXPR and store
1636 it in *VR_P. */
1638 static void
1639 extract_range_from_assert (value_range_t *vr_p, tree expr)
1641 tree var, cond, limit, min, max, type;
1642 value_range_t *limit_vr;
1643 enum tree_code cond_code;
1645 var = ASSERT_EXPR_VAR (expr);
1646 cond = ASSERT_EXPR_COND (expr);
1648 gcc_assert (COMPARISON_CLASS_P (cond));
1650 /* Find VAR in the ASSERT_EXPR conditional. */
1651 if (var == TREE_OPERAND (cond, 0)
1652 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1653 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1655 /* If the predicate is of the form VAR COMP LIMIT, then we just
1656 take LIMIT from the RHS and use the same comparison code. */
1657 cond_code = TREE_CODE (cond);
1658 limit = TREE_OPERAND (cond, 1);
1659 cond = TREE_OPERAND (cond, 0);
1661 else
1663 /* If the predicate is of the form LIMIT COMP VAR, then we need
1664 to flip around the comparison code to create the proper range
1665 for VAR. */
1666 cond_code = swap_tree_comparison (TREE_CODE (cond));
1667 limit = TREE_OPERAND (cond, 0);
1668 cond = TREE_OPERAND (cond, 1);
1671 limit = avoid_overflow_infinity (limit);
1673 type = TREE_TYPE (var);
1674 gcc_assert (limit != var);
1676 /* For pointer arithmetic, we only keep track of pointer equality
1677 and inequality. */
1678 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1680 set_value_range_to_varying (vr_p);
1681 return;
1684 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1685 try to use LIMIT's range to avoid creating symbolic ranges
1686 unnecessarily. */
1687 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1689 /* LIMIT's range is only interesting if it has any useful information. */
1690 if (limit_vr
1691 && (limit_vr->type == VR_UNDEFINED
1692 || limit_vr->type == VR_VARYING
1693 || symbolic_range_p (limit_vr)))
1694 limit_vr = NULL;
1696 /* Initially, the new range has the same set of equivalences of
1697 VAR's range. This will be revised before returning the final
1698 value. Since assertions may be chained via mutually exclusive
1699 predicates, we will need to trim the set of equivalences before
1700 we are done. */
1701 gcc_assert (vr_p->equiv == NULL);
1702 add_equivalence (&vr_p->equiv, var);
1704 /* Extract a new range based on the asserted comparison for VAR and
1705 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1706 will only use it for equality comparisons (EQ_EXPR). For any
1707 other kind of assertion, we cannot derive a range from LIMIT's
1708 anti-range that can be used to describe the new range. For
1709 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1710 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1711 no single range for x_2 that could describe LE_EXPR, so we might
1712 as well build the range [b_4, +INF] for it.
1713 One special case we handle is extracting a range from a
1714 range test encoded as (unsigned)var + CST <= limit. */
1715 if (TREE_CODE (cond) == NOP_EXPR
1716 || TREE_CODE (cond) == PLUS_EXPR)
1718 if (TREE_CODE (cond) == PLUS_EXPR)
1720 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1721 TREE_OPERAND (cond, 1));
1722 max = int_const_binop (PLUS_EXPR, limit, min);
1723 cond = TREE_OPERAND (cond, 0);
1725 else
1727 min = build_int_cst (TREE_TYPE (var), 0);
1728 max = limit;
1731 /* Make sure to not set TREE_OVERFLOW on the final type
1732 conversion. We are willingly interpreting large positive
1733 unsigned values as negative signed values here. */
1734 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1735 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1737 /* We can transform a max, min range to an anti-range or
1738 vice-versa. Use set_and_canonicalize_value_range which does
1739 this for us. */
1740 if (cond_code == LE_EXPR)
1741 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1742 min, max, vr_p->equiv);
1743 else if (cond_code == GT_EXPR)
1744 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1745 min, max, vr_p->equiv);
1746 else
1747 gcc_unreachable ();
1749 else if (cond_code == EQ_EXPR)
1751 enum value_range_type range_type;
1753 if (limit_vr)
1755 range_type = limit_vr->type;
1756 min = limit_vr->min;
1757 max = limit_vr->max;
1759 else
1761 range_type = VR_RANGE;
1762 min = limit;
1763 max = limit;
1766 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1768 /* When asserting the equality VAR == LIMIT and LIMIT is another
1769 SSA name, the new range will also inherit the equivalence set
1770 from LIMIT. */
1771 if (TREE_CODE (limit) == SSA_NAME)
1772 add_equivalence (&vr_p->equiv, limit);
1774 else if (cond_code == NE_EXPR)
1776 /* As described above, when LIMIT's range is an anti-range and
1777 this assertion is an inequality (NE_EXPR), then we cannot
1778 derive anything from the anti-range. For instance, if
1779 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1780 not imply that VAR's range is [0, 0]. So, in the case of
1781 anti-ranges, we just assert the inequality using LIMIT and
1782 not its anti-range.
1784 If LIMIT_VR is a range, we can only use it to build a new
1785 anti-range if LIMIT_VR is a single-valued range. For
1786 instance, if LIMIT_VR is [0, 1], the predicate
1787 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1788 Rather, it means that for value 0 VAR should be ~[0, 0]
1789 and for value 1, VAR should be ~[1, 1]. We cannot
1790 represent these ranges.
1792 The only situation in which we can build a valid
1793 anti-range is when LIMIT_VR is a single-valued range
1794 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1795 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1796 if (limit_vr
1797 && limit_vr->type == VR_RANGE
1798 && compare_values (limit_vr->min, limit_vr->max) == 0)
1800 min = limit_vr->min;
1801 max = limit_vr->max;
1803 else
1805 /* In any other case, we cannot use LIMIT's range to build a
1806 valid anti-range. */
1807 min = max = limit;
1810 /* If MIN and MAX cover the whole range for their type, then
1811 just use the original LIMIT. */
1812 if (INTEGRAL_TYPE_P (type)
1813 && vrp_val_is_min (min)
1814 && vrp_val_is_max (max))
1815 min = max = limit;
1817 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1818 min, max, vr_p->equiv);
1820 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1822 min = TYPE_MIN_VALUE (type);
1824 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1825 max = limit;
1826 else
1828 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1829 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1830 LT_EXPR. */
1831 max = limit_vr->max;
1834 /* If the maximum value forces us to be out of bounds, simply punt.
1835 It would be pointless to try and do anything more since this
1836 all should be optimized away above us. */
1837 if ((cond_code == LT_EXPR
1838 && compare_values (max, min) == 0)
1839 || is_overflow_infinity (max))
1840 set_value_range_to_varying (vr_p);
1841 else
1843 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1844 if (cond_code == LT_EXPR)
1846 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1847 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1848 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1849 build_int_cst (TREE_TYPE (max), -1));
1850 else
1851 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1852 build_int_cst (TREE_TYPE (max), 1));
1853 if (EXPR_P (max))
1854 TREE_NO_WARNING (max) = 1;
1857 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1860 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1862 max = TYPE_MAX_VALUE (type);
1864 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1865 min = limit;
1866 else
1868 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1869 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1870 GT_EXPR. */
1871 min = limit_vr->min;
1874 /* If the minimum value forces us to be out of bounds, simply punt.
1875 It would be pointless to try and do anything more since this
1876 all should be optimized away above us. */
1877 if ((cond_code == GT_EXPR
1878 && compare_values (min, max) == 0)
1879 || is_overflow_infinity (min))
1880 set_value_range_to_varying (vr_p);
1881 else
1883 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1884 if (cond_code == GT_EXPR)
1886 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1887 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1888 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1889 build_int_cst (TREE_TYPE (min), -1));
1890 else
1891 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1892 build_int_cst (TREE_TYPE (min), 1));
1893 if (EXPR_P (min))
1894 TREE_NO_WARNING (min) = 1;
1897 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1900 else
1901 gcc_unreachable ();
1903 /* Finally intersect the new range with what we already know about var. */
1904 vrp_intersect_ranges (vr_p, get_value_range (var));
1908 /* Extract range information from SSA name VAR and store it in VR. If
1909 VAR has an interesting range, use it. Otherwise, create the
1910 range [VAR, VAR] and return it. This is useful in situations where
1911 we may have conditionals testing values of VARYING names. For
1912 instance,
1914 x_3 = y_5;
1915 if (x_3 > y_5)
1918 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1919 always false. */
1921 static void
1922 extract_range_from_ssa_name (value_range_t *vr, tree var)
1924 value_range_t *var_vr = get_value_range (var);
1926 if (var_vr->type != VR_VARYING)
1927 copy_value_range (vr, var_vr);
1928 else
1929 set_value_range (vr, VR_RANGE, var, var, NULL);
1931 add_equivalence (&vr->equiv, var);
1935 /* Wrapper around int_const_binop. If the operation overflows and we
1936 are not using wrapping arithmetic, then adjust the result to be
1937 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1938 NULL_TREE if we need to use an overflow infinity representation but
1939 the type does not support it. */
1941 static tree
1942 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1944 tree res;
1946 res = int_const_binop (code, val1, val2);
1948 /* If we are using unsigned arithmetic, operate symbolically
1949 on -INF and +INF as int_const_binop only handles signed overflow. */
1950 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1952 int checkz = compare_values (res, val1);
1953 bool overflow = false;
1955 /* Ensure that res = val1 [+*] val2 >= val1
1956 or that res = val1 - val2 <= val1. */
1957 if ((code == PLUS_EXPR
1958 && !(checkz == 1 || checkz == 0))
1959 || (code == MINUS_EXPR
1960 && !(checkz == 0 || checkz == -1)))
1962 overflow = true;
1964 /* Checking for multiplication overflow is done by dividing the
1965 output of the multiplication by the first input of the
1966 multiplication. If the result of that division operation is
1967 not equal to the second input of the multiplication, then the
1968 multiplication overflowed. */
1969 else if (code == MULT_EXPR && !integer_zerop (val1))
1971 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1972 res,
1973 val1);
1974 int check = compare_values (tmp, val2);
1976 if (check != 0)
1977 overflow = true;
1980 if (overflow)
1982 res = copy_node (res);
1983 TREE_OVERFLOW (res) = 1;
1987 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1988 /* If the singed operation wraps then int_const_binop has done
1989 everything we want. */
1991 /* Signed division of -1/0 overflows and by the time it gets here
1992 returns NULL_TREE. */
1993 else if (!res)
1994 return NULL_TREE;
1995 else if ((TREE_OVERFLOW (res)
1996 && !TREE_OVERFLOW (val1)
1997 && !TREE_OVERFLOW (val2))
1998 || is_overflow_infinity (val1)
1999 || is_overflow_infinity (val2))
2001 /* If the operation overflowed but neither VAL1 nor VAL2 are
2002 overflown, return -INF or +INF depending on the operation
2003 and the combination of signs of the operands. */
2004 int sgn1 = tree_int_cst_sgn (val1);
2005 int sgn2 = tree_int_cst_sgn (val2);
2007 if (needs_overflow_infinity (TREE_TYPE (res))
2008 && !supports_overflow_infinity (TREE_TYPE (res)))
2009 return NULL_TREE;
2011 /* We have to punt on adding infinities of different signs,
2012 since we can't tell what the sign of the result should be.
2013 Likewise for subtracting infinities of the same sign. */
2014 if (((code == PLUS_EXPR && sgn1 != sgn2)
2015 || (code == MINUS_EXPR && sgn1 == sgn2))
2016 && is_overflow_infinity (val1)
2017 && is_overflow_infinity (val2))
2018 return NULL_TREE;
2020 /* Don't try to handle division or shifting of infinities. */
2021 if ((code == TRUNC_DIV_EXPR
2022 || code == FLOOR_DIV_EXPR
2023 || code == CEIL_DIV_EXPR
2024 || code == EXACT_DIV_EXPR
2025 || code == ROUND_DIV_EXPR
2026 || code == RSHIFT_EXPR)
2027 && (is_overflow_infinity (val1)
2028 || is_overflow_infinity (val2)))
2029 return NULL_TREE;
2031 /* Notice that we only need to handle the restricted set of
2032 operations handled by extract_range_from_binary_expr.
2033 Among them, only multiplication, addition and subtraction
2034 can yield overflow without overflown operands because we
2035 are working with integral types only... except in the
2036 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2037 for division too. */
2039 /* For multiplication, the sign of the overflow is given
2040 by the comparison of the signs of the operands. */
2041 if ((code == MULT_EXPR && sgn1 == sgn2)
2042 /* For addition, the operands must be of the same sign
2043 to yield an overflow. Its sign is therefore that
2044 of one of the operands, for example the first. For
2045 infinite operands X + -INF is negative, not positive. */
2046 || (code == PLUS_EXPR
2047 && (sgn1 >= 0
2048 ? !is_negative_overflow_infinity (val2)
2049 : is_positive_overflow_infinity (val2)))
2050 /* For subtraction, non-infinite operands must be of
2051 different signs to yield an overflow. Its sign is
2052 therefore that of the first operand or the opposite of
2053 that of the second operand. A first operand of 0 counts
2054 as positive here, for the corner case 0 - (-INF), which
2055 overflows, but must yield +INF. For infinite operands 0
2056 - INF is negative, not positive. */
2057 || (code == MINUS_EXPR
2058 && (sgn1 >= 0
2059 ? !is_positive_overflow_infinity (val2)
2060 : is_negative_overflow_infinity (val2)))
2061 /* We only get in here with positive shift count, so the
2062 overflow direction is the same as the sign of val1.
2063 Actually rshift does not overflow at all, but we only
2064 handle the case of shifting overflowed -INF and +INF. */
2065 || (code == RSHIFT_EXPR
2066 && sgn1 >= 0)
2067 /* For division, the only case is -INF / -1 = +INF. */
2068 || code == TRUNC_DIV_EXPR
2069 || code == FLOOR_DIV_EXPR
2070 || code == CEIL_DIV_EXPR
2071 || code == EXACT_DIV_EXPR
2072 || code == ROUND_DIV_EXPR)
2073 return (needs_overflow_infinity (TREE_TYPE (res))
2074 ? positive_overflow_infinity (TREE_TYPE (res))
2075 : TYPE_MAX_VALUE (TREE_TYPE (res)));
2076 else
2077 return (needs_overflow_infinity (TREE_TYPE (res))
2078 ? negative_overflow_infinity (TREE_TYPE (res))
2079 : TYPE_MIN_VALUE (TREE_TYPE (res)));
2082 return res;
2086 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2087 bitmask if some bit is unset, it means for all numbers in the range
2088 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2089 bitmask if some bit is set, it means for all numbers in the range
2090 the bit is 1, otherwise it might be 0 or 1. */
2092 static bool
2093 zero_nonzero_bits_from_vr (const tree expr_type,
2094 value_range_t *vr,
2095 wide_int *may_be_nonzero,
2096 wide_int *must_be_nonzero)
2098 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2099 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2100 if (!range_int_cst_p (vr)
2101 || is_overflow_infinity (vr->min)
2102 || is_overflow_infinity (vr->max))
2103 return false;
2105 if (range_int_cst_singleton_p (vr))
2107 *may_be_nonzero = vr->min;
2108 *must_be_nonzero = *may_be_nonzero;
2110 else if (tree_int_cst_sgn (vr->min) >= 0
2111 || tree_int_cst_sgn (vr->max) < 0)
2113 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2114 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2115 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2116 if (xor_mask != 0)
2118 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2119 may_be_nonzero->get_precision ());
2120 *may_be_nonzero = *may_be_nonzero | mask;
2121 *must_be_nonzero = must_be_nonzero->and_not (mask);
2125 return true;
2128 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2129 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2130 false otherwise. If *AR can be represented with a single range
2131 *VR1 will be VR_UNDEFINED. */
2133 static bool
2134 ranges_from_anti_range (value_range_t *ar,
2135 value_range_t *vr0, value_range_t *vr1)
2137 tree type = TREE_TYPE (ar->min);
2139 vr0->type = VR_UNDEFINED;
2140 vr1->type = VR_UNDEFINED;
2142 if (ar->type != VR_ANTI_RANGE
2143 || TREE_CODE (ar->min) != INTEGER_CST
2144 || TREE_CODE (ar->max) != INTEGER_CST
2145 || !vrp_val_min (type)
2146 || !vrp_val_max (type))
2147 return false;
2149 if (!vrp_val_is_min (ar->min))
2151 vr0->type = VR_RANGE;
2152 vr0->min = vrp_val_min (type);
2153 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2155 if (!vrp_val_is_max (ar->max))
2157 vr1->type = VR_RANGE;
2158 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2159 vr1->max = vrp_val_max (type);
2161 if (vr0->type == VR_UNDEFINED)
2163 *vr0 = *vr1;
2164 vr1->type = VR_UNDEFINED;
2167 return vr0->type != VR_UNDEFINED;
2170 /* Helper to extract a value-range *VR for a multiplicative operation
2171 *VR0 CODE *VR1. */
2173 static void
2174 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2175 enum tree_code code,
2176 value_range_t *vr0, value_range_t *vr1)
2178 enum value_range_type type;
2179 tree val[4];
2180 size_t i;
2181 tree min, max;
2182 bool sop;
2183 int cmp;
2185 /* Multiplications, divisions and shifts are a bit tricky to handle,
2186 depending on the mix of signs we have in the two ranges, we
2187 need to operate on different values to get the minimum and
2188 maximum values for the new range. One approach is to figure
2189 out all the variations of range combinations and do the
2190 operations.
2192 However, this involves several calls to compare_values and it
2193 is pretty convoluted. It's simpler to do the 4 operations
2194 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2195 MAX1) and then figure the smallest and largest values to form
2196 the new range. */
2197 gcc_assert (code == MULT_EXPR
2198 || code == TRUNC_DIV_EXPR
2199 || code == FLOOR_DIV_EXPR
2200 || code == CEIL_DIV_EXPR
2201 || code == EXACT_DIV_EXPR
2202 || code == ROUND_DIV_EXPR
2203 || code == RSHIFT_EXPR
2204 || code == LSHIFT_EXPR);
2205 gcc_assert ((vr0->type == VR_RANGE
2206 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2207 && vr0->type == vr1->type);
2209 type = vr0->type;
2211 /* Compute the 4 cross operations. */
2212 sop = false;
2213 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2214 if (val[0] == NULL_TREE)
2215 sop = true;
2217 if (vr1->max == vr1->min)
2218 val[1] = NULL_TREE;
2219 else
2221 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2222 if (val[1] == NULL_TREE)
2223 sop = true;
2226 if (vr0->max == vr0->min)
2227 val[2] = NULL_TREE;
2228 else
2230 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2231 if (val[2] == NULL_TREE)
2232 sop = true;
2235 if (vr0->min == vr0->max || vr1->min == vr1->max)
2236 val[3] = NULL_TREE;
2237 else
2239 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2240 if (val[3] == NULL_TREE)
2241 sop = true;
2244 if (sop)
2246 set_value_range_to_varying (vr);
2247 return;
2250 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2251 of VAL[i]. */
2252 min = val[0];
2253 max = val[0];
2254 for (i = 1; i < 4; i++)
2256 if (!is_gimple_min_invariant (min)
2257 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2258 || !is_gimple_min_invariant (max)
2259 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2260 break;
2262 if (val[i])
2264 if (!is_gimple_min_invariant (val[i])
2265 || (TREE_OVERFLOW (val[i])
2266 && !is_overflow_infinity (val[i])))
2268 /* If we found an overflowed value, set MIN and MAX
2269 to it so that we set the resulting range to
2270 VARYING. */
2271 min = max = val[i];
2272 break;
2275 if (compare_values (val[i], min) == -1)
2276 min = val[i];
2278 if (compare_values (val[i], max) == 1)
2279 max = val[i];
2283 /* If either MIN or MAX overflowed, then set the resulting range to
2284 VARYING. But we do accept an overflow infinity
2285 representation. */
2286 if (min == NULL_TREE
2287 || !is_gimple_min_invariant (min)
2288 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2289 || max == NULL_TREE
2290 || !is_gimple_min_invariant (max)
2291 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2293 set_value_range_to_varying (vr);
2294 return;
2297 /* We punt if:
2298 1) [-INF, +INF]
2299 2) [-INF, +-INF(OVF)]
2300 3) [+-INF(OVF), +INF]
2301 4) [+-INF(OVF), +-INF(OVF)]
2302 We learn nothing when we have INF and INF(OVF) on both sides.
2303 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2304 overflow. */
2305 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2306 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2308 set_value_range_to_varying (vr);
2309 return;
2312 cmp = compare_values (min, max);
2313 if (cmp == -2 || cmp == 1)
2315 /* If the new range has its limits swapped around (MIN > MAX),
2316 then the operation caused one of them to wrap around, mark
2317 the new range VARYING. */
2318 set_value_range_to_varying (vr);
2320 else
2321 set_value_range (vr, type, min, max, NULL);
2324 /* Extract range information from a binary operation CODE based on
2325 the ranges of each of its operands *VR0 and *VR1 with resulting
2326 type EXPR_TYPE. The resulting range is stored in *VR. */
2328 static void
2329 extract_range_from_binary_expr_1 (value_range_t *vr,
2330 enum tree_code code, tree expr_type,
2331 value_range_t *vr0_, value_range_t *vr1_)
2333 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2334 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2335 enum value_range_type type;
2336 tree min = NULL_TREE, max = NULL_TREE;
2337 int cmp;
2339 if (!INTEGRAL_TYPE_P (expr_type)
2340 && !POINTER_TYPE_P (expr_type))
2342 set_value_range_to_varying (vr);
2343 return;
2346 /* Not all binary expressions can be applied to ranges in a
2347 meaningful way. Handle only arithmetic operations. */
2348 if (code != PLUS_EXPR
2349 && code != MINUS_EXPR
2350 && code != POINTER_PLUS_EXPR
2351 && code != MULT_EXPR
2352 && code != TRUNC_DIV_EXPR
2353 && code != FLOOR_DIV_EXPR
2354 && code != CEIL_DIV_EXPR
2355 && code != EXACT_DIV_EXPR
2356 && code != ROUND_DIV_EXPR
2357 && code != TRUNC_MOD_EXPR
2358 && code != RSHIFT_EXPR
2359 && code != LSHIFT_EXPR
2360 && code != MIN_EXPR
2361 && code != MAX_EXPR
2362 && code != BIT_AND_EXPR
2363 && code != BIT_IOR_EXPR
2364 && code != BIT_XOR_EXPR)
2366 set_value_range_to_varying (vr);
2367 return;
2370 /* If both ranges are UNDEFINED, so is the result. */
2371 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2373 set_value_range_to_undefined (vr);
2374 return;
2376 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2377 code. At some point we may want to special-case operations that
2378 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2379 operand. */
2380 else if (vr0.type == VR_UNDEFINED)
2381 set_value_range_to_varying (&vr0);
2382 else if (vr1.type == VR_UNDEFINED)
2383 set_value_range_to_varying (&vr1);
2385 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2386 and express ~[] op X as ([]' op X) U ([]'' op X). */
2387 if (vr0.type == VR_ANTI_RANGE
2388 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2390 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2391 if (vrtem1.type != VR_UNDEFINED)
2393 value_range_t vrres = VR_INITIALIZER;
2394 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2395 &vrtem1, vr1_);
2396 vrp_meet (vr, &vrres);
2398 return;
2400 /* Likewise for X op ~[]. */
2401 if (vr1.type == VR_ANTI_RANGE
2402 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2404 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2405 if (vrtem1.type != VR_UNDEFINED)
2407 value_range_t vrres = VR_INITIALIZER;
2408 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2409 vr0_, &vrtem1);
2410 vrp_meet (vr, &vrres);
2412 return;
2415 /* The type of the resulting value range defaults to VR0.TYPE. */
2416 type = vr0.type;
2418 /* Refuse to operate on VARYING ranges, ranges of different kinds
2419 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2420 because we may be able to derive a useful range even if one of
2421 the operands is VR_VARYING or symbolic range. Similarly for
2422 divisions, MIN/MAX and PLUS/MINUS.
2424 TODO, we may be able to derive anti-ranges in some cases. */
2425 if (code != BIT_AND_EXPR
2426 && code != BIT_IOR_EXPR
2427 && code != TRUNC_DIV_EXPR
2428 && code != FLOOR_DIV_EXPR
2429 && code != CEIL_DIV_EXPR
2430 && code != EXACT_DIV_EXPR
2431 && code != ROUND_DIV_EXPR
2432 && code != TRUNC_MOD_EXPR
2433 && code != MIN_EXPR
2434 && code != MAX_EXPR
2435 && code != PLUS_EXPR
2436 && code != MINUS_EXPR
2437 && code != RSHIFT_EXPR
2438 && (vr0.type == VR_VARYING
2439 || vr1.type == VR_VARYING
2440 || vr0.type != vr1.type
2441 || symbolic_range_p (&vr0)
2442 || symbolic_range_p (&vr1)))
2444 set_value_range_to_varying (vr);
2445 return;
2448 /* Now evaluate the expression to determine the new range. */
2449 if (POINTER_TYPE_P (expr_type))
2451 if (code == MIN_EXPR || code == MAX_EXPR)
2453 /* For MIN/MAX expressions with pointers, we only care about
2454 nullness, if both are non null, then the result is nonnull.
2455 If both are null, then the result is null. Otherwise they
2456 are varying. */
2457 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2458 set_value_range_to_nonnull (vr, expr_type);
2459 else if (range_is_null (&vr0) && range_is_null (&vr1))
2460 set_value_range_to_null (vr, expr_type);
2461 else
2462 set_value_range_to_varying (vr);
2464 else if (code == POINTER_PLUS_EXPR)
2466 /* For pointer types, we are really only interested in asserting
2467 whether the expression evaluates to non-NULL. */
2468 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2469 set_value_range_to_nonnull (vr, expr_type);
2470 else if (range_is_null (&vr0) && range_is_null (&vr1))
2471 set_value_range_to_null (vr, expr_type);
2472 else
2473 set_value_range_to_varying (vr);
2475 else if (code == BIT_AND_EXPR)
2477 /* For pointer types, we are really only interested in asserting
2478 whether the expression evaluates to non-NULL. */
2479 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2480 set_value_range_to_nonnull (vr, expr_type);
2481 else if (range_is_null (&vr0) || range_is_null (&vr1))
2482 set_value_range_to_null (vr, expr_type);
2483 else
2484 set_value_range_to_varying (vr);
2486 else
2487 set_value_range_to_varying (vr);
2489 return;
2492 /* For integer ranges, apply the operation to each end of the
2493 range and see what we end up with. */
2494 if (code == PLUS_EXPR || code == MINUS_EXPR)
2496 const bool minus_p = (code == MINUS_EXPR);
2497 tree min_op0 = vr0.min;
2498 tree min_op1 = minus_p ? vr1.max : vr1.min;
2499 tree max_op0 = vr0.max;
2500 tree max_op1 = minus_p ? vr1.min : vr1.max;
2501 tree sym_min_op0 = NULL_TREE;
2502 tree sym_min_op1 = NULL_TREE;
2503 tree sym_max_op0 = NULL_TREE;
2504 tree sym_max_op1 = NULL_TREE;
2505 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2507 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2508 single-symbolic ranges, try to compute the precise resulting range,
2509 but only if we know that this resulting range will also be constant
2510 or single-symbolic. */
2511 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2512 && (TREE_CODE (min_op0) == INTEGER_CST
2513 || (sym_min_op0
2514 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2515 && (TREE_CODE (min_op1) == INTEGER_CST
2516 || (sym_min_op1
2517 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2518 && (!(sym_min_op0 && sym_min_op1)
2519 || (sym_min_op0 == sym_min_op1
2520 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2521 && (TREE_CODE (max_op0) == INTEGER_CST
2522 || (sym_max_op0
2523 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2524 && (TREE_CODE (max_op1) == INTEGER_CST
2525 || (sym_max_op1
2526 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2527 && (!(sym_max_op0 && sym_max_op1)
2528 || (sym_max_op0 == sym_max_op1
2529 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2531 const signop sgn = TYPE_SIGN (expr_type);
2532 const unsigned int prec = TYPE_PRECISION (expr_type);
2533 wide_int type_min, type_max, wmin, wmax;
2534 int min_ovf = 0;
2535 int max_ovf = 0;
2537 /* Get the lower and upper bounds of the type. */
2538 if (TYPE_OVERFLOW_WRAPS (expr_type))
2540 type_min = wi::min_value (prec, sgn);
2541 type_max = wi::max_value (prec, sgn);
2543 else
2545 type_min = vrp_val_min (expr_type);
2546 type_max = vrp_val_max (expr_type);
2549 /* Combine the lower bounds, if any. */
2550 if (min_op0 && min_op1)
2552 if (minus_p)
2554 wmin = wi::sub (min_op0, min_op1);
2556 /* Check for overflow. */
2557 if (wi::cmp (0, min_op1, sgn)
2558 != wi::cmp (wmin, min_op0, sgn))
2559 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2561 else
2563 wmin = wi::add (min_op0, min_op1);
2565 /* Check for overflow. */
2566 if (wi::cmp (min_op1, 0, sgn)
2567 != wi::cmp (wmin, min_op0, sgn))
2568 min_ovf = wi::cmp (min_op0, wmin, sgn);
2571 else if (min_op0)
2572 wmin = min_op0;
2573 else if (min_op1)
2574 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2575 else
2576 wmin = wi::shwi (0, prec);
2578 /* Combine the upper bounds, if any. */
2579 if (max_op0 && max_op1)
2581 if (minus_p)
2583 wmax = wi::sub (max_op0, max_op1);
2585 /* Check for overflow. */
2586 if (wi::cmp (0, max_op1, sgn)
2587 != wi::cmp (wmax, max_op0, sgn))
2588 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2590 else
2592 wmax = wi::add (max_op0, max_op1);
2594 if (wi::cmp (max_op1, 0, sgn)
2595 != wi::cmp (wmax, max_op0, sgn))
2596 max_ovf = wi::cmp (max_op0, wmax, sgn);
2599 else if (max_op0)
2600 wmax = max_op0;
2601 else if (max_op1)
2602 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2603 else
2604 wmax = wi::shwi (0, prec);
2606 /* Check for type overflow. */
2607 if (min_ovf == 0)
2609 if (wi::cmp (wmin, type_min, sgn) == -1)
2610 min_ovf = -1;
2611 else if (wi::cmp (wmin, type_max, sgn) == 1)
2612 min_ovf = 1;
2614 if (max_ovf == 0)
2616 if (wi::cmp (wmax, type_min, sgn) == -1)
2617 max_ovf = -1;
2618 else if (wi::cmp (wmax, type_max, sgn) == 1)
2619 max_ovf = 1;
2622 /* If we have overflow for the constant part and the resulting
2623 range will be symbolic, drop to VR_VARYING. */
2624 if ((min_ovf && sym_min_op0 != sym_min_op1)
2625 || (max_ovf && sym_max_op0 != sym_max_op1))
2627 set_value_range_to_varying (vr);
2628 return;
2631 if (TYPE_OVERFLOW_WRAPS (expr_type))
2633 /* If overflow wraps, truncate the values and adjust the
2634 range kind and bounds appropriately. */
2635 wide_int tmin = wide_int::from (wmin, prec, sgn);
2636 wide_int tmax = wide_int::from (wmax, prec, sgn);
2637 if (min_ovf == max_ovf)
2639 /* No overflow or both overflow or underflow. The
2640 range kind stays VR_RANGE. */
2641 min = wide_int_to_tree (expr_type, tmin);
2642 max = wide_int_to_tree (expr_type, tmax);
2644 else if (min_ovf == -1 && max_ovf == 1)
2646 /* Underflow and overflow, drop to VR_VARYING. */
2647 set_value_range_to_varying (vr);
2648 return;
2650 else
2652 /* Min underflow or max overflow. The range kind
2653 changes to VR_ANTI_RANGE. */
2654 bool covers = false;
2655 wide_int tem = tmin;
2656 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2657 || (max_ovf == 1 && min_ovf == 0));
2658 type = VR_ANTI_RANGE;
2659 tmin = tmax + 1;
2660 if (wi::cmp (tmin, tmax, sgn) < 0)
2661 covers = true;
2662 tmax = tem - 1;
2663 if (wi::cmp (tmax, tem, sgn) > 0)
2664 covers = true;
2665 /* If the anti-range would cover nothing, drop to varying.
2666 Likewise if the anti-range bounds are outside of the
2667 types values. */
2668 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2670 set_value_range_to_varying (vr);
2671 return;
2673 min = wide_int_to_tree (expr_type, tmin);
2674 max = wide_int_to_tree (expr_type, tmax);
2677 else
2679 /* If overflow does not wrap, saturate to the types min/max
2680 value. */
2681 if (min_ovf == -1)
2683 if (needs_overflow_infinity (expr_type)
2684 && supports_overflow_infinity (expr_type))
2685 min = negative_overflow_infinity (expr_type);
2686 else
2687 min = wide_int_to_tree (expr_type, type_min);
2689 else if (min_ovf == 1)
2691 if (needs_overflow_infinity (expr_type)
2692 && supports_overflow_infinity (expr_type))
2693 min = positive_overflow_infinity (expr_type);
2694 else
2695 min = wide_int_to_tree (expr_type, type_max);
2697 else
2698 min = wide_int_to_tree (expr_type, wmin);
2700 if (max_ovf == -1)
2702 if (needs_overflow_infinity (expr_type)
2703 && supports_overflow_infinity (expr_type))
2704 max = negative_overflow_infinity (expr_type);
2705 else
2706 max = wide_int_to_tree (expr_type, type_min);
2708 else if (max_ovf == 1)
2710 if (needs_overflow_infinity (expr_type)
2711 && supports_overflow_infinity (expr_type))
2712 max = positive_overflow_infinity (expr_type);
2713 else
2714 max = wide_int_to_tree (expr_type, type_max);
2716 else
2717 max = wide_int_to_tree (expr_type, wmax);
2720 if (needs_overflow_infinity (expr_type)
2721 && supports_overflow_infinity (expr_type))
2723 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2724 || (min_op1
2725 && (minus_p
2726 ? is_positive_overflow_infinity (min_op1)
2727 : is_negative_overflow_infinity (min_op1))))
2728 min = negative_overflow_infinity (expr_type);
2729 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2730 || (max_op1
2731 && (minus_p
2732 ? is_negative_overflow_infinity (max_op1)
2733 : is_positive_overflow_infinity (max_op1))))
2734 max = positive_overflow_infinity (expr_type);
2737 /* If the result lower bound is constant, we're done;
2738 otherwise, build the symbolic lower bound. */
2739 if (sym_min_op0 == sym_min_op1)
2741 else if (sym_min_op0)
2742 min = build_symbolic_expr (expr_type, sym_min_op0,
2743 neg_min_op0, min);
2744 else if (sym_min_op1)
2745 min = build_symbolic_expr (expr_type, sym_min_op1,
2746 neg_min_op1 ^ minus_p, min);
2748 /* Likewise for the upper bound. */
2749 if (sym_max_op0 == sym_max_op1)
2751 else if (sym_max_op0)
2752 max = build_symbolic_expr (expr_type, sym_max_op0,
2753 neg_max_op0, max);
2754 else if (sym_max_op1)
2755 max = build_symbolic_expr (expr_type, sym_max_op1,
2756 neg_max_op1 ^ minus_p, max);
2758 else
2760 /* For other cases, for example if we have a PLUS_EXPR with two
2761 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2762 to compute a precise range for such a case.
2763 ??? General even mixed range kind operations can be expressed
2764 by for example transforming ~[3, 5] + [1, 2] to range-only
2765 operations and a union primitive:
2766 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2767 [-INF+1, 4] U [6, +INF(OVF)]
2768 though usually the union is not exactly representable with
2769 a single range or anti-range as the above is
2770 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2771 but one could use a scheme similar to equivalences for this. */
2772 set_value_range_to_varying (vr);
2773 return;
2776 else if (code == MIN_EXPR
2777 || code == MAX_EXPR)
2779 if (vr0.type == VR_RANGE
2780 && !symbolic_range_p (&vr0))
2782 type = VR_RANGE;
2783 if (vr1.type == VR_RANGE
2784 && !symbolic_range_p (&vr1))
2786 /* For operations that make the resulting range directly
2787 proportional to the original ranges, apply the operation to
2788 the same end of each range. */
2789 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2790 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2792 else if (code == MIN_EXPR)
2794 min = vrp_val_min (expr_type);
2795 max = vr0.max;
2797 else if (code == MAX_EXPR)
2799 min = vr0.min;
2800 max = vrp_val_max (expr_type);
2803 else if (vr1.type == VR_RANGE
2804 && !symbolic_range_p (&vr1))
2806 type = VR_RANGE;
2807 if (code == MIN_EXPR)
2809 min = vrp_val_min (expr_type);
2810 max = vr1.max;
2812 else if (code == MAX_EXPR)
2814 min = vr1.min;
2815 max = vrp_val_max (expr_type);
2818 else
2820 set_value_range_to_varying (vr);
2821 return;
2824 else if (code == MULT_EXPR)
2826 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2827 drop to varying. This test requires 2*prec bits if both
2828 operands are signed and 2*prec + 2 bits if either is not. */
2830 signop sign = TYPE_SIGN (expr_type);
2831 unsigned int prec = TYPE_PRECISION (expr_type);
2833 if (range_int_cst_p (&vr0)
2834 && range_int_cst_p (&vr1)
2835 && TYPE_OVERFLOW_WRAPS (expr_type))
2837 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2838 typedef generic_wide_int
2839 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2840 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2841 vrp_int size = sizem1 + 1;
2843 /* Extend the values using the sign of the result to PREC2.
2844 From here on out, everthing is just signed math no matter
2845 what the input types were. */
2846 vrp_int min0 = vrp_int_cst (vr0.min);
2847 vrp_int max0 = vrp_int_cst (vr0.max);
2848 vrp_int min1 = vrp_int_cst (vr1.min);
2849 vrp_int max1 = vrp_int_cst (vr1.max);
2850 /* Canonicalize the intervals. */
2851 if (sign == UNSIGNED)
2853 if (wi::ltu_p (size, min0 + max0))
2855 min0 -= size;
2856 max0 -= size;
2859 if (wi::ltu_p (size, min1 + max1))
2861 min1 -= size;
2862 max1 -= size;
2866 vrp_int prod0 = min0 * min1;
2867 vrp_int prod1 = min0 * max1;
2868 vrp_int prod2 = max0 * min1;
2869 vrp_int prod3 = max0 * max1;
2871 /* Sort the 4 products so that min is in prod0 and max is in
2872 prod3. */
2873 /* min0min1 > max0max1 */
2874 if (wi::gts_p (prod0, prod3))
2876 vrp_int tmp = prod3;
2877 prod3 = prod0;
2878 prod0 = tmp;
2881 /* min0max1 > max0min1 */
2882 if (wi::gts_p (prod1, prod2))
2884 vrp_int tmp = prod2;
2885 prod2 = prod1;
2886 prod1 = tmp;
2889 if (wi::gts_p (prod0, prod1))
2891 vrp_int tmp = prod1;
2892 prod1 = prod0;
2893 prod0 = tmp;
2896 if (wi::gts_p (prod2, prod3))
2898 vrp_int tmp = prod3;
2899 prod3 = prod2;
2900 prod2 = tmp;
2903 /* diff = max - min. */
2904 prod2 = prod3 - prod0;
2905 if (wi::geu_p (prod2, sizem1))
2907 /* the range covers all values. */
2908 set_value_range_to_varying (vr);
2909 return;
2912 /* The following should handle the wrapping and selecting
2913 VR_ANTI_RANGE for us. */
2914 min = wide_int_to_tree (expr_type, prod0);
2915 max = wide_int_to_tree (expr_type, prod3);
2916 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2917 return;
2920 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2921 drop to VR_VARYING. It would take more effort to compute a
2922 precise range for such a case. For example, if we have
2923 op0 == 65536 and op1 == 65536 with their ranges both being
2924 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2925 we cannot claim that the product is in ~[0,0]. Note that we
2926 are guaranteed to have vr0.type == vr1.type at this
2927 point. */
2928 if (vr0.type == VR_ANTI_RANGE
2929 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2931 set_value_range_to_varying (vr);
2932 return;
2935 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2936 return;
2938 else if (code == RSHIFT_EXPR
2939 || code == LSHIFT_EXPR)
2941 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2942 then drop to VR_VARYING. Outside of this range we get undefined
2943 behavior from the shift operation. We cannot even trust
2944 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2945 shifts, and the operation at the tree level may be widened. */
2946 if (range_int_cst_p (&vr1)
2947 && compare_tree_int (vr1.min, 0) >= 0
2948 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2950 if (code == RSHIFT_EXPR)
2952 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2953 useful ranges just from the shift count. E.g.
2954 x >> 63 for signed 64-bit x is always [-1, 0]. */
2955 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2957 vr0.type = type = VR_RANGE;
2958 vr0.min = vrp_val_min (expr_type);
2959 vr0.max = vrp_val_max (expr_type);
2961 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2962 return;
2964 /* We can map lshifts by constants to MULT_EXPR handling. */
2965 else if (code == LSHIFT_EXPR
2966 && range_int_cst_singleton_p (&vr1))
2968 bool saved_flag_wrapv;
2969 value_range_t vr1p = VR_INITIALIZER;
2970 vr1p.type = VR_RANGE;
2971 vr1p.min = (wide_int_to_tree
2972 (expr_type,
2973 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2974 TYPE_PRECISION (expr_type))));
2975 vr1p.max = vr1p.min;
2976 /* We have to use a wrapping multiply though as signed overflow
2977 on lshifts is implementation defined in C89. */
2978 saved_flag_wrapv = flag_wrapv;
2979 flag_wrapv = 1;
2980 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2981 &vr0, &vr1p);
2982 flag_wrapv = saved_flag_wrapv;
2983 return;
2985 else if (code == LSHIFT_EXPR
2986 && range_int_cst_p (&vr0))
2988 int prec = TYPE_PRECISION (expr_type);
2989 int overflow_pos = prec;
2990 int bound_shift;
2991 wide_int low_bound, high_bound;
2992 bool uns = TYPE_UNSIGNED (expr_type);
2993 bool in_bounds = false;
2995 if (!uns)
2996 overflow_pos -= 1;
2998 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2999 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
3000 overflow. However, for that to happen, vr1.max needs to be
3001 zero, which means vr1 is a singleton range of zero, which
3002 means it should be handled by the previous LSHIFT_EXPR
3003 if-clause. */
3004 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
3005 wide_int complement = ~(bound - 1);
3007 if (uns)
3009 low_bound = bound;
3010 high_bound = complement;
3011 if (wi::ltu_p (vr0.max, low_bound))
3013 /* [5, 6] << [1, 2] == [10, 24]. */
3014 /* We're shifting out only zeroes, the value increases
3015 monotonically. */
3016 in_bounds = true;
3018 else if (wi::ltu_p (high_bound, vr0.min))
3020 /* [0xffffff00, 0xffffffff] << [1, 2]
3021 == [0xfffffc00, 0xfffffffe]. */
3022 /* We're shifting out only ones, the value decreases
3023 monotonically. */
3024 in_bounds = true;
3027 else
3029 /* [-1, 1] << [1, 2] == [-4, 4]. */
3030 low_bound = complement;
3031 high_bound = bound;
3032 if (wi::lts_p (vr0.max, high_bound)
3033 && wi::lts_p (low_bound, vr0.min))
3035 /* For non-negative numbers, we're shifting out only
3036 zeroes, the value increases monotonically.
3037 For negative numbers, we're shifting out only ones, the
3038 value decreases monotomically. */
3039 in_bounds = true;
3043 if (in_bounds)
3045 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3046 return;
3050 set_value_range_to_varying (vr);
3051 return;
3053 else if (code == TRUNC_DIV_EXPR
3054 || code == FLOOR_DIV_EXPR
3055 || code == CEIL_DIV_EXPR
3056 || code == EXACT_DIV_EXPR
3057 || code == ROUND_DIV_EXPR)
3059 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3061 /* For division, if op1 has VR_RANGE but op0 does not, something
3062 can be deduced just from that range. Say [min, max] / [4, max]
3063 gives [min / 4, max / 4] range. */
3064 if (vr1.type == VR_RANGE
3065 && !symbolic_range_p (&vr1)
3066 && range_includes_zero_p (vr1.min, vr1.max) == 0)
3068 vr0.type = type = VR_RANGE;
3069 vr0.min = vrp_val_min (expr_type);
3070 vr0.max = vrp_val_max (expr_type);
3072 else
3074 set_value_range_to_varying (vr);
3075 return;
3079 /* For divisions, if flag_non_call_exceptions is true, we must
3080 not eliminate a division by zero. */
3081 if (cfun->can_throw_non_call_exceptions
3082 && (vr1.type != VR_RANGE
3083 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3085 set_value_range_to_varying (vr);
3086 return;
3089 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3090 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3091 include 0. */
3092 if (vr0.type == VR_RANGE
3093 && (vr1.type != VR_RANGE
3094 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3096 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
3097 int cmp;
3099 min = NULL_TREE;
3100 max = NULL_TREE;
3101 if (TYPE_UNSIGNED (expr_type)
3102 || value_range_nonnegative_p (&vr1))
3104 /* For unsigned division or when divisor is known
3105 to be non-negative, the range has to cover
3106 all numbers from 0 to max for positive max
3107 and all numbers from min to 0 for negative min. */
3108 cmp = compare_values (vr0.max, zero);
3109 if (cmp == -1)
3110 max = zero;
3111 else if (cmp == 0 || cmp == 1)
3112 max = vr0.max;
3113 else
3114 type = VR_VARYING;
3115 cmp = compare_values (vr0.min, zero);
3116 if (cmp == 1)
3117 min = zero;
3118 else if (cmp == 0 || cmp == -1)
3119 min = vr0.min;
3120 else
3121 type = VR_VARYING;
3123 else
3125 /* Otherwise the range is -max .. max or min .. -min
3126 depending on which bound is bigger in absolute value,
3127 as the division can change the sign. */
3128 abs_extent_range (vr, vr0.min, vr0.max);
3129 return;
3131 if (type == VR_VARYING)
3133 set_value_range_to_varying (vr);
3134 return;
3137 else
3139 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3140 return;
3143 else if (code == TRUNC_MOD_EXPR)
3145 if (vr1.type != VR_RANGE
3146 || range_includes_zero_p (vr1.min, vr1.max) != 0
3147 || vrp_val_is_min (vr1.min))
3149 set_value_range_to_varying (vr);
3150 return;
3152 type = VR_RANGE;
3153 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3154 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
3155 if (tree_int_cst_lt (max, vr1.max))
3156 max = vr1.max;
3157 max = int_const_binop (MINUS_EXPR, max, build_int_cst (TREE_TYPE (max), 1));
3158 /* If the dividend is non-negative the modulus will be
3159 non-negative as well. */
3160 if (TYPE_UNSIGNED (expr_type)
3161 || value_range_nonnegative_p (&vr0))
3162 min = build_int_cst (TREE_TYPE (max), 0);
3163 else
3164 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
3166 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3168 bool int_cst_range0, int_cst_range1;
3169 wide_int may_be_nonzero0, may_be_nonzero1;
3170 wide_int must_be_nonzero0, must_be_nonzero1;
3172 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3173 &may_be_nonzero0,
3174 &must_be_nonzero0);
3175 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3176 &may_be_nonzero1,
3177 &must_be_nonzero1);
3179 type = VR_RANGE;
3180 if (code == BIT_AND_EXPR)
3182 min = wide_int_to_tree (expr_type,
3183 must_be_nonzero0 & must_be_nonzero1);
3184 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3185 /* If both input ranges contain only negative values we can
3186 truncate the result range maximum to the minimum of the
3187 input range maxima. */
3188 if (int_cst_range0 && int_cst_range1
3189 && tree_int_cst_sgn (vr0.max) < 0
3190 && tree_int_cst_sgn (vr1.max) < 0)
3192 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3193 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3195 /* If either input range contains only non-negative values
3196 we can truncate the result range maximum to the respective
3197 maximum of the input range. */
3198 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3199 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3200 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3201 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3202 max = wide_int_to_tree (expr_type, wmax);
3204 else if (code == BIT_IOR_EXPR)
3206 max = wide_int_to_tree (expr_type,
3207 may_be_nonzero0 | may_be_nonzero1);
3208 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3209 /* If the input ranges contain only positive values we can
3210 truncate the minimum of the result range to the maximum
3211 of the input range minima. */
3212 if (int_cst_range0 && int_cst_range1
3213 && tree_int_cst_sgn (vr0.min) >= 0
3214 && tree_int_cst_sgn (vr1.min) >= 0)
3216 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3217 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3219 /* If either input range contains only negative values
3220 we can truncate the minimum of the result range to the
3221 respective minimum range. */
3222 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3223 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3224 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3225 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3226 min = wide_int_to_tree (expr_type, wmin);
3228 else if (code == BIT_XOR_EXPR)
3230 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3231 | ~(may_be_nonzero0 | may_be_nonzero1));
3232 wide_int result_one_bits
3233 = (must_be_nonzero0.and_not (may_be_nonzero1)
3234 | must_be_nonzero1.and_not (may_be_nonzero0));
3235 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3236 min = wide_int_to_tree (expr_type, result_one_bits);
3237 /* If the range has all positive or all negative values the
3238 result is better than VARYING. */
3239 if (tree_int_cst_sgn (min) < 0
3240 || tree_int_cst_sgn (max) >= 0)
3242 else
3243 max = min = NULL_TREE;
3246 else
3247 gcc_unreachable ();
3249 /* If either MIN or MAX overflowed, then set the resulting range to
3250 VARYING. But we do accept an overflow infinity representation. */
3251 if (min == NULL_TREE
3252 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3253 || max == NULL_TREE
3254 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3256 set_value_range_to_varying (vr);
3257 return;
3260 /* We punt if:
3261 1) [-INF, +INF]
3262 2) [-INF, +-INF(OVF)]
3263 3) [+-INF(OVF), +INF]
3264 4) [+-INF(OVF), +-INF(OVF)]
3265 We learn nothing when we have INF and INF(OVF) on both sides.
3266 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3267 overflow. */
3268 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3269 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3271 set_value_range_to_varying (vr);
3272 return;
3275 cmp = compare_values (min, max);
3276 if (cmp == -2 || cmp == 1)
3278 /* If the new range has its limits swapped around (MIN > MAX),
3279 then the operation caused one of them to wrap around, mark
3280 the new range VARYING. */
3281 set_value_range_to_varying (vr);
3283 else
3284 set_value_range (vr, type, min, max, NULL);
3287 /* Extract range information from a binary expression OP0 CODE OP1 based on
3288 the ranges of each of its operands with resulting type EXPR_TYPE.
3289 The resulting range is stored in *VR. */
3291 static void
3292 extract_range_from_binary_expr (value_range_t *vr,
3293 enum tree_code code,
3294 tree expr_type, tree op0, tree op1)
3296 value_range_t vr0 = VR_INITIALIZER;
3297 value_range_t vr1 = VR_INITIALIZER;
3299 /* Get value ranges for each operand. For constant operands, create
3300 a new value range with the operand to simplify processing. */
3301 if (TREE_CODE (op0) == SSA_NAME)
3302 vr0 = *(get_value_range (op0));
3303 else if (is_gimple_min_invariant (op0))
3304 set_value_range_to_value (&vr0, op0, NULL);
3305 else
3306 set_value_range_to_varying (&vr0);
3308 if (TREE_CODE (op1) == SSA_NAME)
3309 vr1 = *(get_value_range (op1));
3310 else if (is_gimple_min_invariant (op1))
3311 set_value_range_to_value (&vr1, op1, NULL);
3312 else
3313 set_value_range_to_varying (&vr1);
3315 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3317 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3318 and based on the other operand, for example if it was deduced from a
3319 symbolic comparison. When a bound of the range of the first operand
3320 is invariant, we set the corresponding bound of the new range to INF
3321 in order to avoid recursing on the range of the second operand. */
3322 if (vr->type == VR_VARYING
3323 && (code == PLUS_EXPR || code == MINUS_EXPR)
3324 && TREE_CODE (op1) == SSA_NAME
3325 && vr0.type == VR_RANGE
3326 && symbolic_range_based_on_p (&vr0, op1))
3328 const bool minus_p = (code == MINUS_EXPR);
3329 value_range_t n_vr1 = VR_INITIALIZER;
3331 /* Try with VR0 and [-INF, OP1]. */
3332 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3333 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3335 /* Try with VR0 and [OP1, +INF]. */
3336 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3337 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3339 /* Try with VR0 and [OP1, OP1]. */
3340 else
3341 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3343 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3346 if (vr->type == VR_VARYING
3347 && (code == PLUS_EXPR || code == MINUS_EXPR)
3348 && TREE_CODE (op0) == SSA_NAME
3349 && vr1.type == VR_RANGE
3350 && symbolic_range_based_on_p (&vr1, op0))
3352 const bool minus_p = (code == MINUS_EXPR);
3353 value_range_t n_vr0 = VR_INITIALIZER;
3355 /* Try with [-INF, OP0] and VR1. */
3356 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3357 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3359 /* Try with [OP0, +INF] and VR1. */
3360 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3361 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3363 /* Try with [OP0, OP0] and VR1. */
3364 else
3365 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3367 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3371 /* Extract range information from a unary operation CODE based on
3372 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3373 The The resulting range is stored in *VR. */
3375 static void
3376 extract_range_from_unary_expr_1 (value_range_t *vr,
3377 enum tree_code code, tree type,
3378 value_range_t *vr0_, tree op0_type)
3380 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3382 /* VRP only operates on integral and pointer types. */
3383 if (!(INTEGRAL_TYPE_P (op0_type)
3384 || POINTER_TYPE_P (op0_type))
3385 || !(INTEGRAL_TYPE_P (type)
3386 || POINTER_TYPE_P (type)))
3388 set_value_range_to_varying (vr);
3389 return;
3392 /* If VR0 is UNDEFINED, so is the result. */
3393 if (vr0.type == VR_UNDEFINED)
3395 set_value_range_to_undefined (vr);
3396 return;
3399 /* Handle operations that we express in terms of others. */
3400 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3402 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3403 copy_value_range (vr, &vr0);
3404 return;
3406 else if (code == NEGATE_EXPR)
3408 /* -X is simply 0 - X, so re-use existing code that also handles
3409 anti-ranges fine. */
3410 value_range_t zero = VR_INITIALIZER;
3411 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3412 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3413 return;
3415 else if (code == BIT_NOT_EXPR)
3417 /* ~X is simply -1 - X, so re-use existing code that also handles
3418 anti-ranges fine. */
3419 value_range_t minusone = VR_INITIALIZER;
3420 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3421 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3422 type, &minusone, &vr0);
3423 return;
3426 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3427 and express op ~[] as (op []') U (op []''). */
3428 if (vr0.type == VR_ANTI_RANGE
3429 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3431 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3432 if (vrtem1.type != VR_UNDEFINED)
3434 value_range_t vrres = VR_INITIALIZER;
3435 extract_range_from_unary_expr_1 (&vrres, code, type,
3436 &vrtem1, op0_type);
3437 vrp_meet (vr, &vrres);
3439 return;
3442 if (CONVERT_EXPR_CODE_P (code))
3444 tree inner_type = op0_type;
3445 tree outer_type = type;
3447 /* If the expression evaluates to a pointer, we are only interested in
3448 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3449 if (POINTER_TYPE_P (type))
3451 if (range_is_nonnull (&vr0))
3452 set_value_range_to_nonnull (vr, type);
3453 else if (range_is_null (&vr0))
3454 set_value_range_to_null (vr, type);
3455 else
3456 set_value_range_to_varying (vr);
3457 return;
3460 /* If VR0 is varying and we increase the type precision, assume
3461 a full range for the following transformation. */
3462 if (vr0.type == VR_VARYING
3463 && INTEGRAL_TYPE_P (inner_type)
3464 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3466 vr0.type = VR_RANGE;
3467 vr0.min = TYPE_MIN_VALUE (inner_type);
3468 vr0.max = TYPE_MAX_VALUE (inner_type);
3471 /* If VR0 is a constant range or anti-range and the conversion is
3472 not truncating we can convert the min and max values and
3473 canonicalize the resulting range. Otherwise we can do the
3474 conversion if the size of the range is less than what the
3475 precision of the target type can represent and the range is
3476 not an anti-range. */
3477 if ((vr0.type == VR_RANGE
3478 || vr0.type == VR_ANTI_RANGE)
3479 && TREE_CODE (vr0.min) == INTEGER_CST
3480 && TREE_CODE (vr0.max) == INTEGER_CST
3481 && (!is_overflow_infinity (vr0.min)
3482 || (vr0.type == VR_RANGE
3483 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3484 && needs_overflow_infinity (outer_type)
3485 && supports_overflow_infinity (outer_type)))
3486 && (!is_overflow_infinity (vr0.max)
3487 || (vr0.type == VR_RANGE
3488 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3489 && needs_overflow_infinity (outer_type)
3490 && supports_overflow_infinity (outer_type)))
3491 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3492 || (vr0.type == VR_RANGE
3493 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3494 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3495 size_int (TYPE_PRECISION (outer_type)))))))
3497 tree new_min, new_max;
3498 if (is_overflow_infinity (vr0.min))
3499 new_min = negative_overflow_infinity (outer_type);
3500 else
3501 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3502 0, false);
3503 if (is_overflow_infinity (vr0.max))
3504 new_max = positive_overflow_infinity (outer_type);
3505 else
3506 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3507 0, false);
3508 set_and_canonicalize_value_range (vr, vr0.type,
3509 new_min, new_max, NULL);
3510 return;
3513 set_value_range_to_varying (vr);
3514 return;
3516 else if (code == ABS_EXPR)
3518 tree min, max;
3519 int cmp;
3521 /* Pass through vr0 in the easy cases. */
3522 if (TYPE_UNSIGNED (type)
3523 || value_range_nonnegative_p (&vr0))
3525 copy_value_range (vr, &vr0);
3526 return;
3529 /* For the remaining varying or symbolic ranges we can't do anything
3530 useful. */
3531 if (vr0.type == VR_VARYING
3532 || symbolic_range_p (&vr0))
3534 set_value_range_to_varying (vr);
3535 return;
3538 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3539 useful range. */
3540 if (!TYPE_OVERFLOW_UNDEFINED (type)
3541 && ((vr0.type == VR_RANGE
3542 && vrp_val_is_min (vr0.min))
3543 || (vr0.type == VR_ANTI_RANGE
3544 && !vrp_val_is_min (vr0.min))))
3546 set_value_range_to_varying (vr);
3547 return;
3550 /* ABS_EXPR may flip the range around, if the original range
3551 included negative values. */
3552 if (is_overflow_infinity (vr0.min))
3553 min = positive_overflow_infinity (type);
3554 else if (!vrp_val_is_min (vr0.min))
3555 min = fold_unary_to_constant (code, type, vr0.min);
3556 else if (!needs_overflow_infinity (type))
3557 min = TYPE_MAX_VALUE (type);
3558 else if (supports_overflow_infinity (type))
3559 min = positive_overflow_infinity (type);
3560 else
3562 set_value_range_to_varying (vr);
3563 return;
3566 if (is_overflow_infinity (vr0.max))
3567 max = positive_overflow_infinity (type);
3568 else if (!vrp_val_is_min (vr0.max))
3569 max = fold_unary_to_constant (code, type, vr0.max);
3570 else if (!needs_overflow_infinity (type))
3571 max = TYPE_MAX_VALUE (type);
3572 else if (supports_overflow_infinity (type)
3573 /* We shouldn't generate [+INF, +INF] as set_value_range
3574 doesn't like this and ICEs. */
3575 && !is_positive_overflow_infinity (min))
3576 max = positive_overflow_infinity (type);
3577 else
3579 set_value_range_to_varying (vr);
3580 return;
3583 cmp = compare_values (min, max);
3585 /* If a VR_ANTI_RANGEs contains zero, then we have
3586 ~[-INF, min(MIN, MAX)]. */
3587 if (vr0.type == VR_ANTI_RANGE)
3589 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3591 /* Take the lower of the two values. */
3592 if (cmp != 1)
3593 max = min;
3595 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3596 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3597 flag_wrapv is set and the original anti-range doesn't include
3598 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3599 if (TYPE_OVERFLOW_WRAPS (type))
3601 tree type_min_value = TYPE_MIN_VALUE (type);
3603 min = (vr0.min != type_min_value
3604 ? int_const_binop (PLUS_EXPR, type_min_value,
3605 build_int_cst (TREE_TYPE (type_min_value), 1))
3606 : type_min_value);
3608 else
3610 if (overflow_infinity_range_p (&vr0))
3611 min = negative_overflow_infinity (type);
3612 else
3613 min = TYPE_MIN_VALUE (type);
3616 else
3618 /* All else has failed, so create the range [0, INF], even for
3619 flag_wrapv since TYPE_MIN_VALUE is in the original
3620 anti-range. */
3621 vr0.type = VR_RANGE;
3622 min = build_int_cst (type, 0);
3623 if (needs_overflow_infinity (type))
3625 if (supports_overflow_infinity (type))
3626 max = positive_overflow_infinity (type);
3627 else
3629 set_value_range_to_varying (vr);
3630 return;
3633 else
3634 max = TYPE_MAX_VALUE (type);
3638 /* If the range contains zero then we know that the minimum value in the
3639 range will be zero. */
3640 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3642 if (cmp == 1)
3643 max = min;
3644 min = build_int_cst (type, 0);
3646 else
3648 /* If the range was reversed, swap MIN and MAX. */
3649 if (cmp == 1)
3651 tree t = min;
3652 min = max;
3653 max = t;
3657 cmp = compare_values (min, max);
3658 if (cmp == -2 || cmp == 1)
3660 /* If the new range has its limits swapped around (MIN > MAX),
3661 then the operation caused one of them to wrap around, mark
3662 the new range VARYING. */
3663 set_value_range_to_varying (vr);
3665 else
3666 set_value_range (vr, vr0.type, min, max, NULL);
3667 return;
3670 /* For unhandled operations fall back to varying. */
3671 set_value_range_to_varying (vr);
3672 return;
3676 /* Extract range information from a unary expression CODE OP0 based on
3677 the range of its operand with resulting type TYPE.
3678 The resulting range is stored in *VR. */
3680 static void
3681 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3682 tree type, tree op0)
3684 value_range_t vr0 = VR_INITIALIZER;
3686 /* Get value ranges for the operand. For constant operands, create
3687 a new value range with the operand to simplify processing. */
3688 if (TREE_CODE (op0) == SSA_NAME)
3689 vr0 = *(get_value_range (op0));
3690 else if (is_gimple_min_invariant (op0))
3691 set_value_range_to_value (&vr0, op0, NULL);
3692 else
3693 set_value_range_to_varying (&vr0);
3695 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3699 /* Extract range information from a conditional expression STMT based on
3700 the ranges of each of its operands and the expression code. */
3702 static void
3703 extract_range_from_cond_expr (value_range_t *vr, gassign *stmt)
3705 tree op0, op1;
3706 value_range_t vr0 = VR_INITIALIZER;
3707 value_range_t vr1 = VR_INITIALIZER;
3709 /* Get value ranges for each operand. For constant operands, create
3710 a new value range with the operand to simplify processing. */
3711 op0 = gimple_assign_rhs2 (stmt);
3712 if (TREE_CODE (op0) == SSA_NAME)
3713 vr0 = *(get_value_range (op0));
3714 else if (is_gimple_min_invariant (op0))
3715 set_value_range_to_value (&vr0, op0, NULL);
3716 else
3717 set_value_range_to_varying (&vr0);
3719 op1 = gimple_assign_rhs3 (stmt);
3720 if (TREE_CODE (op1) == SSA_NAME)
3721 vr1 = *(get_value_range (op1));
3722 else if (is_gimple_min_invariant (op1))
3723 set_value_range_to_value (&vr1, op1, NULL);
3724 else
3725 set_value_range_to_varying (&vr1);
3727 /* The resulting value range is the union of the operand ranges */
3728 copy_value_range (vr, &vr0);
3729 vrp_meet (vr, &vr1);
3733 /* Extract range information from a comparison expression EXPR based
3734 on the range of its operand and the expression code. */
3736 static void
3737 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3738 tree type, tree op0, tree op1)
3740 bool sop = false;
3741 tree val;
3743 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3744 NULL);
3746 /* A disadvantage of using a special infinity as an overflow
3747 representation is that we lose the ability to record overflow
3748 when we don't have an infinity. So we have to ignore a result
3749 which relies on overflow. */
3751 if (val && !is_overflow_infinity (val) && !sop)
3753 /* Since this expression was found on the RHS of an assignment,
3754 its type may be different from _Bool. Convert VAL to EXPR's
3755 type. */
3756 val = fold_convert (type, val);
3757 if (is_gimple_min_invariant (val))
3758 set_value_range_to_value (vr, val, vr->equiv);
3759 else
3760 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3762 else
3763 /* The result of a comparison is always true or false. */
3764 set_value_range_to_truthvalue (vr, type);
3767 /* Helper function for simplify_internal_call_using_ranges and
3768 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3769 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3770 always overflow. Set *OVF to true if it is known to always
3771 overflow. */
3773 static bool
3774 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3775 tree op0, tree op1, bool *ovf)
3777 value_range_t vr0 = VR_INITIALIZER;
3778 value_range_t vr1 = VR_INITIALIZER;
3779 if (TREE_CODE (op0) == SSA_NAME)
3780 vr0 = *get_value_range (op0);
3781 else if (TREE_CODE (op0) == INTEGER_CST)
3782 set_value_range_to_value (&vr0, op0, NULL);
3783 else
3784 set_value_range_to_varying (&vr0);
3786 if (TREE_CODE (op1) == SSA_NAME)
3787 vr1 = *get_value_range (op1);
3788 else if (TREE_CODE (op1) == INTEGER_CST)
3789 set_value_range_to_value (&vr1, op1, NULL);
3790 else
3791 set_value_range_to_varying (&vr1);
3793 if (!range_int_cst_p (&vr0)
3794 || TREE_OVERFLOW (vr0.min)
3795 || TREE_OVERFLOW (vr0.max))
3797 vr0.min = vrp_val_min (TREE_TYPE (op0));
3798 vr0.max = vrp_val_max (TREE_TYPE (op0));
3800 if (!range_int_cst_p (&vr1)
3801 || TREE_OVERFLOW (vr1.min)
3802 || TREE_OVERFLOW (vr1.max))
3804 vr1.min = vrp_val_min (TREE_TYPE (op1));
3805 vr1.max = vrp_val_max (TREE_TYPE (op1));
3807 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3808 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3809 if (arith_overflowed_p (subcode, type, vr0.max,
3810 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3811 return false;
3812 if (subcode == MULT_EXPR)
3814 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3815 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3816 return false;
3818 if (*ovf)
3820 /* So far we found that there is an overflow on the boundaries.
3821 That doesn't prove that there is an overflow even for all values
3822 in between the boundaries. For that compute widest_int range
3823 of the result and see if it doesn't overlap the range of
3824 type. */
3825 widest_int wmin, wmax;
3826 widest_int w[4];
3827 int i;
3828 w[0] = wi::to_widest (vr0.min);
3829 w[1] = wi::to_widest (vr0.max);
3830 w[2] = wi::to_widest (vr1.min);
3831 w[3] = wi::to_widest (vr1.max);
3832 for (i = 0; i < 4; i++)
3834 widest_int wt;
3835 switch (subcode)
3837 case PLUS_EXPR:
3838 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3839 break;
3840 case MINUS_EXPR:
3841 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3842 break;
3843 case MULT_EXPR:
3844 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3845 break;
3846 default:
3847 gcc_unreachable ();
3849 if (i == 0)
3851 wmin = wt;
3852 wmax = wt;
3854 else
3856 wmin = wi::smin (wmin, wt);
3857 wmax = wi::smax (wmax, wt);
3860 /* The result of op0 CODE op1 is known to be in range
3861 [wmin, wmax]. */
3862 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3863 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3864 /* If all values in [wmin, wmax] are smaller than
3865 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3866 the arithmetic operation will always overflow. */
3867 if (wi::lts_p (wmax, wtmin) || wi::gts_p (wmin, wtmax))
3868 return true;
3869 return false;
3871 return true;
3874 /* Try to derive a nonnegative or nonzero range out of STMT relying
3875 primarily on generic routines in fold in conjunction with range data.
3876 Store the result in *VR */
3878 static void
3879 extract_range_basic (value_range_t *vr, gimple stmt)
3881 bool sop = false;
3882 tree type = gimple_expr_type (stmt);
3884 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3886 tree fndecl = gimple_call_fndecl (stmt), arg;
3887 int mini, maxi, zerov = 0, prec;
3889 switch (DECL_FUNCTION_CODE (fndecl))
3891 case BUILT_IN_CONSTANT_P:
3892 /* If the call is __builtin_constant_p and the argument is a
3893 function parameter resolve it to false. This avoids bogus
3894 array bound warnings.
3895 ??? We could do this as early as inlining is finished. */
3896 arg = gimple_call_arg (stmt, 0);
3897 if (TREE_CODE (arg) == SSA_NAME
3898 && SSA_NAME_IS_DEFAULT_DEF (arg)
3899 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3901 set_value_range_to_null (vr, type);
3902 return;
3904 break;
3905 /* Both __builtin_ffs* and __builtin_popcount return
3906 [0, prec]. */
3907 CASE_INT_FN (BUILT_IN_FFS):
3908 CASE_INT_FN (BUILT_IN_POPCOUNT):
3909 arg = gimple_call_arg (stmt, 0);
3910 prec = TYPE_PRECISION (TREE_TYPE (arg));
3911 mini = 0;
3912 maxi = prec;
3913 if (TREE_CODE (arg) == SSA_NAME)
3915 value_range_t *vr0 = get_value_range (arg);
3916 /* If arg is non-zero, then ffs or popcount
3917 are non-zero. */
3918 if (((vr0->type == VR_RANGE
3919 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3920 || (vr0->type == VR_ANTI_RANGE
3921 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3922 && !is_overflow_infinity (vr0->min)
3923 && !is_overflow_infinity (vr0->max))
3924 mini = 1;
3925 /* If some high bits are known to be zero,
3926 we can decrease the maximum. */
3927 if (vr0->type == VR_RANGE
3928 && TREE_CODE (vr0->max) == INTEGER_CST
3929 && !operand_less_p (vr0->min,
3930 build_zero_cst (TREE_TYPE (vr0->min)))
3931 && !is_overflow_infinity (vr0->max))
3932 maxi = tree_floor_log2 (vr0->max) + 1;
3934 goto bitop_builtin;
3935 /* __builtin_parity* returns [0, 1]. */
3936 CASE_INT_FN (BUILT_IN_PARITY):
3937 mini = 0;
3938 maxi = 1;
3939 goto bitop_builtin;
3940 /* __builtin_c[lt]z* return [0, prec-1], except for
3941 when the argument is 0, but that is undefined behavior.
3942 On many targets where the CLZ RTL or optab value is defined
3943 for 0 the value is prec, so include that in the range
3944 by default. */
3945 CASE_INT_FN (BUILT_IN_CLZ):
3946 arg = gimple_call_arg (stmt, 0);
3947 prec = TYPE_PRECISION (TREE_TYPE (arg));
3948 mini = 0;
3949 maxi = prec;
3950 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3951 != CODE_FOR_nothing
3952 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3953 zerov)
3954 /* Handle only the single common value. */
3955 && zerov != prec)
3956 /* Magic value to give up, unless vr0 proves
3957 arg is non-zero. */
3958 mini = -2;
3959 if (TREE_CODE (arg) == SSA_NAME)
3961 value_range_t *vr0 = get_value_range (arg);
3962 /* From clz of VR_RANGE minimum we can compute
3963 result maximum. */
3964 if (vr0->type == VR_RANGE
3965 && TREE_CODE (vr0->min) == INTEGER_CST
3966 && !is_overflow_infinity (vr0->min))
3968 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3969 if (maxi != prec)
3970 mini = 0;
3972 else if (vr0->type == VR_ANTI_RANGE
3973 && integer_zerop (vr0->min)
3974 && !is_overflow_infinity (vr0->min))
3976 maxi = prec - 1;
3977 mini = 0;
3979 if (mini == -2)
3980 break;
3981 /* From clz of VR_RANGE maximum we can compute
3982 result minimum. */
3983 if (vr0->type == VR_RANGE
3984 && TREE_CODE (vr0->max) == INTEGER_CST
3985 && !is_overflow_infinity (vr0->max))
3987 mini = prec - 1 - tree_floor_log2 (vr0->max);
3988 if (mini == prec)
3989 break;
3992 if (mini == -2)
3993 break;
3994 goto bitop_builtin;
3995 /* __builtin_ctz* return [0, prec-1], except for
3996 when the argument is 0, but that is undefined behavior.
3997 If there is a ctz optab for this mode and
3998 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3999 otherwise just assume 0 won't be seen. */
4000 CASE_INT_FN (BUILT_IN_CTZ):
4001 arg = gimple_call_arg (stmt, 0);
4002 prec = TYPE_PRECISION (TREE_TYPE (arg));
4003 mini = 0;
4004 maxi = prec - 1;
4005 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
4006 != CODE_FOR_nothing
4007 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
4008 zerov))
4010 /* Handle only the two common values. */
4011 if (zerov == -1)
4012 mini = -1;
4013 else if (zerov == prec)
4014 maxi = prec;
4015 else
4016 /* Magic value to give up, unless vr0 proves
4017 arg is non-zero. */
4018 mini = -2;
4020 if (TREE_CODE (arg) == SSA_NAME)
4022 value_range_t *vr0 = get_value_range (arg);
4023 /* If arg is non-zero, then use [0, prec - 1]. */
4024 if (((vr0->type == VR_RANGE
4025 && integer_nonzerop (vr0->min))
4026 || (vr0->type == VR_ANTI_RANGE
4027 && integer_zerop (vr0->min)))
4028 && !is_overflow_infinity (vr0->min))
4030 mini = 0;
4031 maxi = prec - 1;
4033 /* If some high bits are known to be zero,
4034 we can decrease the result maximum. */
4035 if (vr0->type == VR_RANGE
4036 && TREE_CODE (vr0->max) == INTEGER_CST
4037 && !is_overflow_infinity (vr0->max))
4039 maxi = tree_floor_log2 (vr0->max);
4040 /* For vr0 [0, 0] give up. */
4041 if (maxi == -1)
4042 break;
4045 if (mini == -2)
4046 break;
4047 goto bitop_builtin;
4048 /* __builtin_clrsb* returns [0, prec-1]. */
4049 CASE_INT_FN (BUILT_IN_CLRSB):
4050 arg = gimple_call_arg (stmt, 0);
4051 prec = TYPE_PRECISION (TREE_TYPE (arg));
4052 mini = 0;
4053 maxi = prec - 1;
4054 goto bitop_builtin;
4055 bitop_builtin:
4056 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
4057 build_int_cst (type, maxi), NULL);
4058 return;
4059 default:
4060 break;
4063 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
4065 enum tree_code subcode = ERROR_MARK;
4066 switch (gimple_call_internal_fn (stmt))
4068 case IFN_UBSAN_CHECK_ADD:
4069 subcode = PLUS_EXPR;
4070 break;
4071 case IFN_UBSAN_CHECK_SUB:
4072 subcode = MINUS_EXPR;
4073 break;
4074 case IFN_UBSAN_CHECK_MUL:
4075 subcode = MULT_EXPR;
4076 break;
4077 default:
4078 break;
4080 if (subcode != ERROR_MARK)
4082 bool saved_flag_wrapv = flag_wrapv;
4083 /* Pretend the arithmetics is wrapping. If there is
4084 any overflow, we'll complain, but will actually do
4085 wrapping operation. */
4086 flag_wrapv = 1;
4087 extract_range_from_binary_expr (vr, subcode, type,
4088 gimple_call_arg (stmt, 0),
4089 gimple_call_arg (stmt, 1));
4090 flag_wrapv = saved_flag_wrapv;
4092 /* If for both arguments vrp_valueize returned non-NULL,
4093 this should have been already folded and if not, it
4094 wasn't folded because of overflow. Avoid removing the
4095 UBSAN_CHECK_* calls in that case. */
4096 if (vr->type == VR_RANGE
4097 && (vr->min == vr->max
4098 || operand_equal_p (vr->min, vr->max, 0)))
4099 set_value_range_to_varying (vr);
4100 return;
4103 /* Handle extraction of the two results (result of arithmetics and
4104 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4105 internal function. */
4106 else if (is_gimple_assign (stmt)
4107 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4108 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4109 && INTEGRAL_TYPE_P (type))
4111 enum tree_code code = gimple_assign_rhs_code (stmt);
4112 tree op = gimple_assign_rhs1 (stmt);
4113 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4115 gimple g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4116 if (is_gimple_call (g) && gimple_call_internal_p (g))
4118 enum tree_code subcode = ERROR_MARK;
4119 switch (gimple_call_internal_fn (g))
4121 case IFN_ADD_OVERFLOW:
4122 subcode = PLUS_EXPR;
4123 break;
4124 case IFN_SUB_OVERFLOW:
4125 subcode = MINUS_EXPR;
4126 break;
4127 case IFN_MUL_OVERFLOW:
4128 subcode = MULT_EXPR;
4129 break;
4130 default:
4131 break;
4133 if (subcode != ERROR_MARK)
4135 tree op0 = gimple_call_arg (g, 0);
4136 tree op1 = gimple_call_arg (g, 1);
4137 if (code == IMAGPART_EXPR)
4139 bool ovf = false;
4140 if (check_for_binary_op_overflow (subcode, type,
4141 op0, op1, &ovf))
4142 set_value_range_to_value (vr,
4143 build_int_cst (type, ovf),
4144 NULL);
4145 else
4146 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4147 build_int_cst (type, 1), NULL);
4149 else if (types_compatible_p (type, TREE_TYPE (op0))
4150 && types_compatible_p (type, TREE_TYPE (op1)))
4152 bool saved_flag_wrapv = flag_wrapv;
4153 /* Pretend the arithmetics is wrapping. If there is
4154 any overflow, IMAGPART_EXPR will be set. */
4155 flag_wrapv = 1;
4156 extract_range_from_binary_expr (vr, subcode, type,
4157 op0, op1);
4158 flag_wrapv = saved_flag_wrapv;
4160 else
4162 value_range_t vr0 = VR_INITIALIZER;
4163 value_range_t vr1 = VR_INITIALIZER;
4164 bool saved_flag_wrapv = flag_wrapv;
4165 /* Pretend the arithmetics is wrapping. If there is
4166 any overflow, IMAGPART_EXPR will be set. */
4167 flag_wrapv = 1;
4168 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4169 type, op0);
4170 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4171 type, op1);
4172 extract_range_from_binary_expr_1 (vr, subcode, type,
4173 &vr0, &vr1);
4174 flag_wrapv = saved_flag_wrapv;
4176 return;
4181 if (INTEGRAL_TYPE_P (type)
4182 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4183 set_value_range_to_nonnegative (vr, type,
4184 sop || stmt_overflow_infinity (stmt));
4185 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4186 && !sop)
4187 set_value_range_to_nonnull (vr, type);
4188 else
4189 set_value_range_to_varying (vr);
4193 /* Try to compute a useful range out of assignment STMT and store it
4194 in *VR. */
4196 static void
4197 extract_range_from_assignment (value_range_t *vr, gassign *stmt)
4199 enum tree_code code = gimple_assign_rhs_code (stmt);
4201 if (code == ASSERT_EXPR)
4202 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4203 else if (code == SSA_NAME)
4204 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4205 else if (TREE_CODE_CLASS (code) == tcc_binary)
4206 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4207 gimple_expr_type (stmt),
4208 gimple_assign_rhs1 (stmt),
4209 gimple_assign_rhs2 (stmt));
4210 else if (TREE_CODE_CLASS (code) == tcc_unary)
4211 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4212 gimple_expr_type (stmt),
4213 gimple_assign_rhs1 (stmt));
4214 else if (code == COND_EXPR)
4215 extract_range_from_cond_expr (vr, stmt);
4216 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4217 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4218 gimple_expr_type (stmt),
4219 gimple_assign_rhs1 (stmt),
4220 gimple_assign_rhs2 (stmt));
4221 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4222 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4223 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4224 else
4225 set_value_range_to_varying (vr);
4227 if (vr->type == VR_VARYING)
4228 extract_range_basic (vr, stmt);
4231 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4232 would be profitable to adjust VR using scalar evolution information
4233 for VAR. If so, update VR with the new limits. */
4235 static void
4236 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
4237 gimple stmt, tree var)
4239 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4240 enum ev_direction dir;
4242 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4243 better opportunities than a regular range, but I'm not sure. */
4244 if (vr->type == VR_ANTI_RANGE)
4245 return;
4247 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4249 /* Like in PR19590, scev can return a constant function. */
4250 if (is_gimple_min_invariant (chrec))
4252 set_value_range_to_value (vr, chrec, vr->equiv);
4253 return;
4256 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4257 return;
4259 init = initial_condition_in_loop_num (chrec, loop->num);
4260 tem = op_with_constant_singleton_value_range (init);
4261 if (tem)
4262 init = tem;
4263 step = evolution_part_in_loop_num (chrec, loop->num);
4264 tem = op_with_constant_singleton_value_range (step);
4265 if (tem)
4266 step = tem;
4268 /* If STEP is symbolic, we can't know whether INIT will be the
4269 minimum or maximum value in the range. Also, unless INIT is
4270 a simple expression, compare_values and possibly other functions
4271 in tree-vrp won't be able to handle it. */
4272 if (step == NULL_TREE
4273 || !is_gimple_min_invariant (step)
4274 || !valid_value_p (init))
4275 return;
4277 dir = scev_direction (chrec);
4278 if (/* Do not adjust ranges if we do not know whether the iv increases
4279 or decreases, ... */
4280 dir == EV_DIR_UNKNOWN
4281 /* ... or if it may wrap. */
4282 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4283 true))
4284 return;
4286 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4287 negative_overflow_infinity and positive_overflow_infinity,
4288 because we have concluded that the loop probably does not
4289 wrap. */
4291 type = TREE_TYPE (var);
4292 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4293 tmin = lower_bound_in_type (type, type);
4294 else
4295 tmin = TYPE_MIN_VALUE (type);
4296 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4297 tmax = upper_bound_in_type (type, type);
4298 else
4299 tmax = TYPE_MAX_VALUE (type);
4301 /* Try to use estimated number of iterations for the loop to constrain the
4302 final value in the evolution. */
4303 if (TREE_CODE (step) == INTEGER_CST
4304 && is_gimple_val (init)
4305 && (TREE_CODE (init) != SSA_NAME
4306 || get_value_range (init)->type == VR_RANGE))
4308 widest_int nit;
4310 /* We are only entering here for loop header PHI nodes, so using
4311 the number of latch executions is the correct thing to use. */
4312 if (max_loop_iterations (loop, &nit))
4314 value_range_t maxvr = VR_INITIALIZER;
4315 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4316 bool overflow;
4318 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4319 &overflow);
4320 /* If the multiplication overflowed we can't do a meaningful
4321 adjustment. Likewise if the result doesn't fit in the type
4322 of the induction variable. For a signed type we have to
4323 check whether the result has the expected signedness which
4324 is that of the step as number of iterations is unsigned. */
4325 if (!overflow
4326 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4327 && (sgn == UNSIGNED
4328 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4330 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4331 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4332 TREE_TYPE (init), init, tem);
4333 /* Likewise if the addition did. */
4334 if (maxvr.type == VR_RANGE)
4336 tmin = maxvr.min;
4337 tmax = maxvr.max;
4343 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4345 min = tmin;
4346 max = tmax;
4348 /* For VARYING or UNDEFINED ranges, just about anything we get
4349 from scalar evolutions should be better. */
4351 if (dir == EV_DIR_DECREASES)
4352 max = init;
4353 else
4354 min = init;
4356 else if (vr->type == VR_RANGE)
4358 min = vr->min;
4359 max = vr->max;
4361 if (dir == EV_DIR_DECREASES)
4363 /* INIT is the maximum value. If INIT is lower than VR->MAX
4364 but no smaller than VR->MIN, set VR->MAX to INIT. */
4365 if (compare_values (init, max) == -1)
4366 max = init;
4368 /* According to the loop information, the variable does not
4369 overflow. If we think it does, probably because of an
4370 overflow due to arithmetic on a different INF value,
4371 reset now. */
4372 if (is_negative_overflow_infinity (min)
4373 || compare_values (min, tmin) == -1)
4374 min = tmin;
4377 else
4379 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4380 if (compare_values (init, min) == 1)
4381 min = init;
4383 if (is_positive_overflow_infinity (max)
4384 || compare_values (tmax, max) == -1)
4385 max = tmax;
4388 else
4389 return;
4391 /* If we just created an invalid range with the minimum
4392 greater than the maximum, we fail conservatively.
4393 This should happen only in unreachable
4394 parts of code, or for invalid programs. */
4395 if (compare_values (min, max) == 1
4396 || (is_negative_overflow_infinity (min)
4397 && is_positive_overflow_infinity (max)))
4398 return;
4400 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4404 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4406 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4407 all the values in the ranges.
4409 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4411 - Return NULL_TREE if it is not always possible to determine the
4412 value of the comparison.
4414 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4415 overflow infinity was used in the test. */
4418 static tree
4419 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4420 bool *strict_overflow_p)
4422 /* VARYING or UNDEFINED ranges cannot be compared. */
4423 if (vr0->type == VR_VARYING
4424 || vr0->type == VR_UNDEFINED
4425 || vr1->type == VR_VARYING
4426 || vr1->type == VR_UNDEFINED)
4427 return NULL_TREE;
4429 /* Anti-ranges need to be handled separately. */
4430 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4432 /* If both are anti-ranges, then we cannot compute any
4433 comparison. */
4434 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4435 return NULL_TREE;
4437 /* These comparisons are never statically computable. */
4438 if (comp == GT_EXPR
4439 || comp == GE_EXPR
4440 || comp == LT_EXPR
4441 || comp == LE_EXPR)
4442 return NULL_TREE;
4444 /* Equality can be computed only between a range and an
4445 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4446 if (vr0->type == VR_RANGE)
4448 /* To simplify processing, make VR0 the anti-range. */
4449 value_range_t *tmp = vr0;
4450 vr0 = vr1;
4451 vr1 = tmp;
4454 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4456 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4457 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4458 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4460 return NULL_TREE;
4463 if (!usable_range_p (vr0, strict_overflow_p)
4464 || !usable_range_p (vr1, strict_overflow_p))
4465 return NULL_TREE;
4467 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4468 operands around and change the comparison code. */
4469 if (comp == GT_EXPR || comp == GE_EXPR)
4471 value_range_t *tmp;
4472 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4473 tmp = vr0;
4474 vr0 = vr1;
4475 vr1 = tmp;
4478 if (comp == EQ_EXPR)
4480 /* Equality may only be computed if both ranges represent
4481 exactly one value. */
4482 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4483 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4485 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4486 strict_overflow_p);
4487 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4488 strict_overflow_p);
4489 if (cmp_min == 0 && cmp_max == 0)
4490 return boolean_true_node;
4491 else if (cmp_min != -2 && cmp_max != -2)
4492 return boolean_false_node;
4494 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4495 else if (compare_values_warnv (vr0->min, vr1->max,
4496 strict_overflow_p) == 1
4497 || compare_values_warnv (vr1->min, vr0->max,
4498 strict_overflow_p) == 1)
4499 return boolean_false_node;
4501 return NULL_TREE;
4503 else if (comp == NE_EXPR)
4505 int cmp1, cmp2;
4507 /* If VR0 is completely to the left or completely to the right
4508 of VR1, they are always different. Notice that we need to
4509 make sure that both comparisons yield similar results to
4510 avoid comparing values that cannot be compared at
4511 compile-time. */
4512 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4513 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4514 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4515 return boolean_true_node;
4517 /* If VR0 and VR1 represent a single value and are identical,
4518 return false. */
4519 else if (compare_values_warnv (vr0->min, vr0->max,
4520 strict_overflow_p) == 0
4521 && compare_values_warnv (vr1->min, vr1->max,
4522 strict_overflow_p) == 0
4523 && compare_values_warnv (vr0->min, vr1->min,
4524 strict_overflow_p) == 0
4525 && compare_values_warnv (vr0->max, vr1->max,
4526 strict_overflow_p) == 0)
4527 return boolean_false_node;
4529 /* Otherwise, they may or may not be different. */
4530 else
4531 return NULL_TREE;
4533 else if (comp == LT_EXPR || comp == LE_EXPR)
4535 int tst;
4537 /* If VR0 is to the left of VR1, return true. */
4538 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4539 if ((comp == LT_EXPR && tst == -1)
4540 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4542 if (overflow_infinity_range_p (vr0)
4543 || overflow_infinity_range_p (vr1))
4544 *strict_overflow_p = true;
4545 return boolean_true_node;
4548 /* If VR0 is to the right of VR1, return false. */
4549 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4550 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4551 || (comp == LE_EXPR && tst == 1))
4553 if (overflow_infinity_range_p (vr0)
4554 || overflow_infinity_range_p (vr1))
4555 *strict_overflow_p = true;
4556 return boolean_false_node;
4559 /* Otherwise, we don't know. */
4560 return NULL_TREE;
4563 gcc_unreachable ();
4567 /* Given a value range VR, a value VAL and a comparison code COMP, return
4568 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4569 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4570 always returns false. Return NULL_TREE if it is not always
4571 possible to determine the value of the comparison. Also set
4572 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4573 infinity was used in the test. */
4575 static tree
4576 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4577 bool *strict_overflow_p)
4579 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4580 return NULL_TREE;
4582 /* Anti-ranges need to be handled separately. */
4583 if (vr->type == VR_ANTI_RANGE)
4585 /* For anti-ranges, the only predicates that we can compute at
4586 compile time are equality and inequality. */
4587 if (comp == GT_EXPR
4588 || comp == GE_EXPR
4589 || comp == LT_EXPR
4590 || comp == LE_EXPR)
4591 return NULL_TREE;
4593 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4594 if (value_inside_range (val, vr->min, vr->max) == 1)
4595 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4597 return NULL_TREE;
4600 if (!usable_range_p (vr, strict_overflow_p))
4601 return NULL_TREE;
4603 if (comp == EQ_EXPR)
4605 /* EQ_EXPR may only be computed if VR represents exactly
4606 one value. */
4607 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4609 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4610 if (cmp == 0)
4611 return boolean_true_node;
4612 else if (cmp == -1 || cmp == 1 || cmp == 2)
4613 return boolean_false_node;
4615 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4616 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4617 return boolean_false_node;
4619 return NULL_TREE;
4621 else if (comp == NE_EXPR)
4623 /* If VAL is not inside VR, then they are always different. */
4624 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4625 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4626 return boolean_true_node;
4628 /* If VR represents exactly one value equal to VAL, then return
4629 false. */
4630 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4631 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4632 return boolean_false_node;
4634 /* Otherwise, they may or may not be different. */
4635 return NULL_TREE;
4637 else if (comp == LT_EXPR || comp == LE_EXPR)
4639 int tst;
4641 /* If VR is to the left of VAL, return true. */
4642 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4643 if ((comp == LT_EXPR && tst == -1)
4644 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4646 if (overflow_infinity_range_p (vr))
4647 *strict_overflow_p = true;
4648 return boolean_true_node;
4651 /* If VR is to the right of VAL, return false. */
4652 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4653 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4654 || (comp == LE_EXPR && tst == 1))
4656 if (overflow_infinity_range_p (vr))
4657 *strict_overflow_p = true;
4658 return boolean_false_node;
4661 /* Otherwise, we don't know. */
4662 return NULL_TREE;
4664 else if (comp == GT_EXPR || comp == GE_EXPR)
4666 int tst;
4668 /* If VR is to the right of VAL, return true. */
4669 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4670 if ((comp == GT_EXPR && tst == 1)
4671 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4673 if (overflow_infinity_range_p (vr))
4674 *strict_overflow_p = true;
4675 return boolean_true_node;
4678 /* If VR is to the left of VAL, return false. */
4679 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4680 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4681 || (comp == GE_EXPR && tst == -1))
4683 if (overflow_infinity_range_p (vr))
4684 *strict_overflow_p = true;
4685 return boolean_false_node;
4688 /* Otherwise, we don't know. */
4689 return NULL_TREE;
4692 gcc_unreachable ();
4696 /* Debugging dumps. */
4698 void dump_value_range (FILE *, value_range_t *);
4699 void debug_value_range (value_range_t *);
4700 void dump_all_value_ranges (FILE *);
4701 void debug_all_value_ranges (void);
4702 void dump_vr_equiv (FILE *, bitmap);
4703 void debug_vr_equiv (bitmap);
4706 /* Dump value range VR to FILE. */
4708 void
4709 dump_value_range (FILE *file, value_range_t *vr)
4711 if (vr == NULL)
4712 fprintf (file, "[]");
4713 else if (vr->type == VR_UNDEFINED)
4714 fprintf (file, "UNDEFINED");
4715 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4717 tree type = TREE_TYPE (vr->min);
4719 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4721 if (is_negative_overflow_infinity (vr->min))
4722 fprintf (file, "-INF(OVF)");
4723 else if (INTEGRAL_TYPE_P (type)
4724 && !TYPE_UNSIGNED (type)
4725 && vrp_val_is_min (vr->min))
4726 fprintf (file, "-INF");
4727 else
4728 print_generic_expr (file, vr->min, 0);
4730 fprintf (file, ", ");
4732 if (is_positive_overflow_infinity (vr->max))
4733 fprintf (file, "+INF(OVF)");
4734 else if (INTEGRAL_TYPE_P (type)
4735 && vrp_val_is_max (vr->max))
4736 fprintf (file, "+INF");
4737 else
4738 print_generic_expr (file, vr->max, 0);
4740 fprintf (file, "]");
4742 if (vr->equiv)
4744 bitmap_iterator bi;
4745 unsigned i, c = 0;
4747 fprintf (file, " EQUIVALENCES: { ");
4749 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4751 print_generic_expr (file, ssa_name (i), 0);
4752 fprintf (file, " ");
4753 c++;
4756 fprintf (file, "} (%u elements)", c);
4759 else if (vr->type == VR_VARYING)
4760 fprintf (file, "VARYING");
4761 else
4762 fprintf (file, "INVALID RANGE");
4766 /* Dump value range VR to stderr. */
4768 DEBUG_FUNCTION void
4769 debug_value_range (value_range_t *vr)
4771 dump_value_range (stderr, vr);
4772 fprintf (stderr, "\n");
4776 /* Dump value ranges of all SSA_NAMEs to FILE. */
4778 void
4779 dump_all_value_ranges (FILE *file)
4781 size_t i;
4783 for (i = 0; i < num_vr_values; i++)
4785 if (vr_value[i])
4787 print_generic_expr (file, ssa_name (i), 0);
4788 fprintf (file, ": ");
4789 dump_value_range (file, vr_value[i]);
4790 fprintf (file, "\n");
4794 fprintf (file, "\n");
4798 /* Dump all value ranges to stderr. */
4800 DEBUG_FUNCTION void
4801 debug_all_value_ranges (void)
4803 dump_all_value_ranges (stderr);
4807 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4808 create a new SSA name N and return the assertion assignment
4809 'N = ASSERT_EXPR <V, V OP W>'. */
4811 static gimple
4812 build_assert_expr_for (tree cond, tree v)
4814 tree a;
4815 gassign *assertion;
4817 gcc_assert (TREE_CODE (v) == SSA_NAME
4818 && COMPARISON_CLASS_P (cond));
4820 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4821 assertion = gimple_build_assign (NULL_TREE, a);
4823 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4824 operand of the ASSERT_EXPR. Create it so the new name and the old one
4825 are registered in the replacement table so that we can fix the SSA web
4826 after adding all the ASSERT_EXPRs. */
4827 create_new_def_for (v, assertion, NULL);
4829 return assertion;
4833 /* Return false if EXPR is a predicate expression involving floating
4834 point values. */
4836 static inline bool
4837 fp_predicate (gimple stmt)
4839 GIMPLE_CHECK (stmt, GIMPLE_COND);
4841 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4844 /* If the range of values taken by OP can be inferred after STMT executes,
4845 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4846 describes the inferred range. Return true if a range could be
4847 inferred. */
4849 static bool
4850 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4852 *val_p = NULL_TREE;
4853 *comp_code_p = ERROR_MARK;
4855 /* Do not attempt to infer anything in names that flow through
4856 abnormal edges. */
4857 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4858 return false;
4860 /* Similarly, don't infer anything from statements that may throw
4861 exceptions. ??? Relax this requirement? */
4862 if (stmt_could_throw_p (stmt))
4863 return false;
4865 /* If STMT is the last statement of a basic block with no normal
4866 successors, there is no point inferring anything about any of its
4867 operands. We would not be able to find a proper insertion point
4868 for the assertion, anyway. */
4869 if (stmt_ends_bb_p (stmt))
4871 edge_iterator ei;
4872 edge e;
4874 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4875 if (!(e->flags & EDGE_ABNORMAL))
4876 break;
4877 if (e == NULL)
4878 return false;
4881 if (infer_nonnull_range (stmt, op, true, true))
4883 *val_p = build_int_cst (TREE_TYPE (op), 0);
4884 *comp_code_p = NE_EXPR;
4885 return true;
4888 return false;
4892 void dump_asserts_for (FILE *, tree);
4893 void debug_asserts_for (tree);
4894 void dump_all_asserts (FILE *);
4895 void debug_all_asserts (void);
4897 /* Dump all the registered assertions for NAME to FILE. */
4899 void
4900 dump_asserts_for (FILE *file, tree name)
4902 assert_locus_t loc;
4904 fprintf (file, "Assertions to be inserted for ");
4905 print_generic_expr (file, name, 0);
4906 fprintf (file, "\n");
4908 loc = asserts_for[SSA_NAME_VERSION (name)];
4909 while (loc)
4911 fprintf (file, "\t");
4912 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4913 fprintf (file, "\n\tBB #%d", loc->bb->index);
4914 if (loc->e)
4916 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4917 loc->e->dest->index);
4918 dump_edge_info (file, loc->e, dump_flags, 0);
4920 fprintf (file, "\n\tPREDICATE: ");
4921 print_generic_expr (file, name, 0);
4922 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4923 print_generic_expr (file, loc->val, 0);
4924 fprintf (file, "\n\n");
4925 loc = loc->next;
4928 fprintf (file, "\n");
4932 /* Dump all the registered assertions for NAME to stderr. */
4934 DEBUG_FUNCTION void
4935 debug_asserts_for (tree name)
4937 dump_asserts_for (stderr, name);
4941 /* Dump all the registered assertions for all the names to FILE. */
4943 void
4944 dump_all_asserts (FILE *file)
4946 unsigned i;
4947 bitmap_iterator bi;
4949 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4950 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4951 dump_asserts_for (file, ssa_name (i));
4952 fprintf (file, "\n");
4956 /* Dump all the registered assertions for all the names to stderr. */
4958 DEBUG_FUNCTION void
4959 debug_all_asserts (void)
4961 dump_all_asserts (stderr);
4965 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4966 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4967 E->DEST, then register this location as a possible insertion point
4968 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4970 BB, E and SI provide the exact insertion point for the new
4971 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4972 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4973 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4974 must not be NULL. */
4976 static void
4977 register_new_assert_for (tree name, tree expr,
4978 enum tree_code comp_code,
4979 tree val,
4980 basic_block bb,
4981 edge e,
4982 gimple_stmt_iterator si)
4984 assert_locus_t n, loc, last_loc;
4985 basic_block dest_bb;
4987 gcc_checking_assert (bb == NULL || e == NULL);
4989 if (e == NULL)
4990 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4991 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4993 /* Never build an assert comparing against an integer constant with
4994 TREE_OVERFLOW set. This confuses our undefined overflow warning
4995 machinery. */
4996 if (TREE_OVERFLOW_P (val))
4997 val = drop_tree_overflow (val);
4999 /* The new assertion A will be inserted at BB or E. We need to
5000 determine if the new location is dominated by a previously
5001 registered location for A. If we are doing an edge insertion,
5002 assume that A will be inserted at E->DEST. Note that this is not
5003 necessarily true.
5005 If E is a critical edge, it will be split. But even if E is
5006 split, the new block will dominate the same set of blocks that
5007 E->DEST dominates.
5009 The reverse, however, is not true, blocks dominated by E->DEST
5010 will not be dominated by the new block created to split E. So,
5011 if the insertion location is on a critical edge, we will not use
5012 the new location to move another assertion previously registered
5013 at a block dominated by E->DEST. */
5014 dest_bb = (bb) ? bb : e->dest;
5016 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5017 VAL at a block dominating DEST_BB, then we don't need to insert a new
5018 one. Similarly, if the same assertion already exists at a block
5019 dominated by DEST_BB and the new location is not on a critical
5020 edge, then update the existing location for the assertion (i.e.,
5021 move the assertion up in the dominance tree).
5023 Note, this is implemented as a simple linked list because there
5024 should not be more than a handful of assertions registered per
5025 name. If this becomes a performance problem, a table hashed by
5026 COMP_CODE and VAL could be implemented. */
5027 loc = asserts_for[SSA_NAME_VERSION (name)];
5028 last_loc = loc;
5029 while (loc)
5031 if (loc->comp_code == comp_code
5032 && (loc->val == val
5033 || operand_equal_p (loc->val, val, 0))
5034 && (loc->expr == expr
5035 || operand_equal_p (loc->expr, expr, 0)))
5037 /* If E is not a critical edge and DEST_BB
5038 dominates the existing location for the assertion, move
5039 the assertion up in the dominance tree by updating its
5040 location information. */
5041 if ((e == NULL || !EDGE_CRITICAL_P (e))
5042 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5044 loc->bb = dest_bb;
5045 loc->e = e;
5046 loc->si = si;
5047 return;
5051 /* Update the last node of the list and move to the next one. */
5052 last_loc = loc;
5053 loc = loc->next;
5056 /* If we didn't find an assertion already registered for
5057 NAME COMP_CODE VAL, add a new one at the end of the list of
5058 assertions associated with NAME. */
5059 n = XNEW (struct assert_locus_d);
5060 n->bb = dest_bb;
5061 n->e = e;
5062 n->si = si;
5063 n->comp_code = comp_code;
5064 n->val = val;
5065 n->expr = expr;
5066 n->next = NULL;
5068 if (last_loc)
5069 last_loc->next = n;
5070 else
5071 asserts_for[SSA_NAME_VERSION (name)] = n;
5073 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5076 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5077 Extract a suitable test code and value and store them into *CODE_P and
5078 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5080 If no extraction was possible, return FALSE, otherwise return TRUE.
5082 If INVERT is true, then we invert the result stored into *CODE_P. */
5084 static bool
5085 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5086 tree cond_op0, tree cond_op1,
5087 bool invert, enum tree_code *code_p,
5088 tree *val_p)
5090 enum tree_code comp_code;
5091 tree val;
5093 /* Otherwise, we have a comparison of the form NAME COMP VAL
5094 or VAL COMP NAME. */
5095 if (name == cond_op1)
5097 /* If the predicate is of the form VAL COMP NAME, flip
5098 COMP around because we need to register NAME as the
5099 first operand in the predicate. */
5100 comp_code = swap_tree_comparison (cond_code);
5101 val = cond_op0;
5103 else
5105 /* The comparison is of the form NAME COMP VAL, so the
5106 comparison code remains unchanged. */
5107 comp_code = cond_code;
5108 val = cond_op1;
5111 /* Invert the comparison code as necessary. */
5112 if (invert)
5113 comp_code = invert_tree_comparison (comp_code, 0);
5115 /* VRP does not handle float types. */
5116 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
5117 return false;
5119 /* Do not register always-false predicates.
5120 FIXME: this works around a limitation in fold() when dealing with
5121 enumerations. Given 'enum { N1, N2 } x;', fold will not
5122 fold 'if (x > N2)' to 'if (0)'. */
5123 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5124 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5126 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5127 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5129 if (comp_code == GT_EXPR
5130 && (!max
5131 || compare_values (val, max) == 0))
5132 return false;
5134 if (comp_code == LT_EXPR
5135 && (!min
5136 || compare_values (val, min) == 0))
5137 return false;
5139 *code_p = comp_code;
5140 *val_p = val;
5141 return true;
5144 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5145 (otherwise return VAL). VAL and MASK must be zero-extended for
5146 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5147 (to transform signed values into unsigned) and at the end xor
5148 SGNBIT back. */
5150 static wide_int
5151 masked_increment (const wide_int &val_in, const wide_int &mask,
5152 const wide_int &sgnbit, unsigned int prec)
5154 wide_int bit = wi::one (prec), res;
5155 unsigned int i;
5157 wide_int val = val_in ^ sgnbit;
5158 for (i = 0; i < prec; i++, bit += bit)
5160 res = mask;
5161 if ((res & bit) == 0)
5162 continue;
5163 res = bit - 1;
5164 res = (val + bit).and_not (res);
5165 res &= mask;
5166 if (wi::gtu_p (res, val))
5167 return res ^ sgnbit;
5169 return val ^ sgnbit;
5172 /* Try to register an edge assertion for SSA name NAME on edge E for
5173 the condition COND contributing to the conditional jump pointed to by BSI.
5174 Invert the condition COND if INVERT is true. */
5176 static void
5177 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5178 enum tree_code cond_code,
5179 tree cond_op0, tree cond_op1, bool invert)
5181 tree val;
5182 enum tree_code comp_code;
5184 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5185 cond_op0,
5186 cond_op1,
5187 invert, &comp_code, &val))
5188 return;
5190 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5191 reachable from E. */
5192 if (live_on_edge (e, name)
5193 && !has_single_use (name))
5194 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5196 /* In the case of NAME <= CST and NAME being defined as
5197 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5198 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5199 This catches range and anti-range tests. */
5200 if ((comp_code == LE_EXPR
5201 || comp_code == GT_EXPR)
5202 && TREE_CODE (val) == INTEGER_CST
5203 && TYPE_UNSIGNED (TREE_TYPE (val)))
5205 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5206 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5208 /* Extract CST2 from the (optional) addition. */
5209 if (is_gimple_assign (def_stmt)
5210 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5212 name2 = gimple_assign_rhs1 (def_stmt);
5213 cst2 = gimple_assign_rhs2 (def_stmt);
5214 if (TREE_CODE (name2) == SSA_NAME
5215 && TREE_CODE (cst2) == INTEGER_CST)
5216 def_stmt = SSA_NAME_DEF_STMT (name2);
5219 /* Extract NAME2 from the (optional) sign-changing cast. */
5220 if (gimple_assign_cast_p (def_stmt))
5222 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5223 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5224 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5225 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5226 name3 = gimple_assign_rhs1 (def_stmt);
5229 /* If name3 is used later, create an ASSERT_EXPR for it. */
5230 if (name3 != NULL_TREE
5231 && TREE_CODE (name3) == SSA_NAME
5232 && (cst2 == NULL_TREE
5233 || TREE_CODE (cst2) == INTEGER_CST)
5234 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5235 && live_on_edge (e, name3)
5236 && !has_single_use (name3))
5238 tree tmp;
5240 /* Build an expression for the range test. */
5241 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5242 if (cst2 != NULL_TREE)
5243 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5245 if (dump_file)
5247 fprintf (dump_file, "Adding assert for ");
5248 print_generic_expr (dump_file, name3, 0);
5249 fprintf (dump_file, " from ");
5250 print_generic_expr (dump_file, tmp, 0);
5251 fprintf (dump_file, "\n");
5254 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5257 /* If name2 is used later, create an ASSERT_EXPR for it. */
5258 if (name2 != NULL_TREE
5259 && TREE_CODE (name2) == SSA_NAME
5260 && TREE_CODE (cst2) == INTEGER_CST
5261 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5262 && live_on_edge (e, name2)
5263 && !has_single_use (name2))
5265 tree tmp;
5267 /* Build an expression for the range test. */
5268 tmp = name2;
5269 if (TREE_TYPE (name) != TREE_TYPE (name2))
5270 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5271 if (cst2 != NULL_TREE)
5272 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5274 if (dump_file)
5276 fprintf (dump_file, "Adding assert for ");
5277 print_generic_expr (dump_file, name2, 0);
5278 fprintf (dump_file, " from ");
5279 print_generic_expr (dump_file, tmp, 0);
5280 fprintf (dump_file, "\n");
5283 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5287 /* In the case of post-in/decrement tests like if (i++) ... and uses
5288 of the in/decremented value on the edge the extra name we want to
5289 assert for is not on the def chain of the name compared. Instead
5290 it is in the set of use stmts. */
5291 if ((comp_code == NE_EXPR
5292 || comp_code == EQ_EXPR)
5293 && TREE_CODE (val) == INTEGER_CST)
5295 imm_use_iterator ui;
5296 gimple use_stmt;
5297 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5299 /* Cut off to use-stmts that are in the predecessor. */
5300 if (gimple_bb (use_stmt) != e->src)
5301 continue;
5303 if (!is_gimple_assign (use_stmt))
5304 continue;
5306 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5307 if (code != PLUS_EXPR
5308 && code != MINUS_EXPR)
5309 continue;
5311 tree cst = gimple_assign_rhs2 (use_stmt);
5312 if (TREE_CODE (cst) != INTEGER_CST)
5313 continue;
5315 tree name2 = gimple_assign_lhs (use_stmt);
5316 if (live_on_edge (e, name2))
5318 cst = int_const_binop (code, val, cst);
5319 register_new_assert_for (name2, name2, comp_code, cst,
5320 NULL, e, bsi);
5325 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5326 && TREE_CODE (val) == INTEGER_CST)
5328 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5329 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5330 tree val2 = NULL_TREE;
5331 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5332 wide_int mask = wi::zero (prec);
5333 unsigned int nprec = prec;
5334 enum tree_code rhs_code = ERROR_MARK;
5336 if (is_gimple_assign (def_stmt))
5337 rhs_code = gimple_assign_rhs_code (def_stmt);
5339 /* Add asserts for NAME cmp CST and NAME being defined
5340 as NAME = (int) NAME2. */
5341 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5342 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5343 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5344 && gimple_assign_cast_p (def_stmt))
5346 name2 = gimple_assign_rhs1 (def_stmt);
5347 if (CONVERT_EXPR_CODE_P (rhs_code)
5348 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5349 && TYPE_UNSIGNED (TREE_TYPE (name2))
5350 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5351 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5352 || !tree_int_cst_equal (val,
5353 TYPE_MIN_VALUE (TREE_TYPE (val))))
5354 && live_on_edge (e, name2)
5355 && !has_single_use (name2))
5357 tree tmp, cst;
5358 enum tree_code new_comp_code = comp_code;
5360 cst = fold_convert (TREE_TYPE (name2),
5361 TYPE_MIN_VALUE (TREE_TYPE (val)));
5362 /* Build an expression for the range test. */
5363 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5364 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5365 fold_convert (TREE_TYPE (name2), val));
5366 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5368 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5369 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5370 build_int_cst (TREE_TYPE (name2), 1));
5373 if (dump_file)
5375 fprintf (dump_file, "Adding assert for ");
5376 print_generic_expr (dump_file, name2, 0);
5377 fprintf (dump_file, " from ");
5378 print_generic_expr (dump_file, tmp, 0);
5379 fprintf (dump_file, "\n");
5382 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5383 e, bsi);
5387 /* Add asserts for NAME cmp CST and NAME being defined as
5388 NAME = NAME2 >> CST2.
5390 Extract CST2 from the right shift. */
5391 if (rhs_code == RSHIFT_EXPR)
5393 name2 = gimple_assign_rhs1 (def_stmt);
5394 cst2 = gimple_assign_rhs2 (def_stmt);
5395 if (TREE_CODE (name2) == SSA_NAME
5396 && tree_fits_uhwi_p (cst2)
5397 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5398 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5399 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5400 && live_on_edge (e, name2)
5401 && !has_single_use (name2))
5403 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5404 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5407 if (val2 != NULL_TREE
5408 && TREE_CODE (val2) == INTEGER_CST
5409 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5410 TREE_TYPE (val),
5411 val2, cst2), val))
5413 enum tree_code new_comp_code = comp_code;
5414 tree tmp, new_val;
5416 tmp = name2;
5417 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5419 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5421 tree type = build_nonstandard_integer_type (prec, 1);
5422 tmp = build1 (NOP_EXPR, type, name2);
5423 val2 = fold_convert (type, val2);
5425 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5426 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5427 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5429 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5431 wide_int minval
5432 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5433 new_val = val2;
5434 if (minval == new_val)
5435 new_val = NULL_TREE;
5437 else
5439 wide_int maxval
5440 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5441 mask |= val2;
5442 if (mask == maxval)
5443 new_val = NULL_TREE;
5444 else
5445 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5448 if (new_val)
5450 if (dump_file)
5452 fprintf (dump_file, "Adding assert for ");
5453 print_generic_expr (dump_file, name2, 0);
5454 fprintf (dump_file, " from ");
5455 print_generic_expr (dump_file, tmp, 0);
5456 fprintf (dump_file, "\n");
5459 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5460 NULL, e, bsi);
5464 /* Add asserts for NAME cmp CST and NAME being defined as
5465 NAME = NAME2 & CST2.
5467 Extract CST2 from the and.
5469 Also handle
5470 NAME = (unsigned) NAME2;
5471 casts where NAME's type is unsigned and has smaller precision
5472 than NAME2's type as if it was NAME = NAME2 & MASK. */
5473 names[0] = NULL_TREE;
5474 names[1] = NULL_TREE;
5475 cst2 = NULL_TREE;
5476 if (rhs_code == BIT_AND_EXPR
5477 || (CONVERT_EXPR_CODE_P (rhs_code)
5478 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5479 && TYPE_UNSIGNED (TREE_TYPE (val))
5480 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5481 > prec))
5483 name2 = gimple_assign_rhs1 (def_stmt);
5484 if (rhs_code == BIT_AND_EXPR)
5485 cst2 = gimple_assign_rhs2 (def_stmt);
5486 else
5488 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5489 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5491 if (TREE_CODE (name2) == SSA_NAME
5492 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5493 && TREE_CODE (cst2) == INTEGER_CST
5494 && !integer_zerop (cst2)
5495 && (nprec > 1
5496 || TYPE_UNSIGNED (TREE_TYPE (val))))
5498 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5499 if (gimple_assign_cast_p (def_stmt2))
5501 names[1] = gimple_assign_rhs1 (def_stmt2);
5502 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5503 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5504 || (TYPE_PRECISION (TREE_TYPE (name2))
5505 != TYPE_PRECISION (TREE_TYPE (names[1])))
5506 || !live_on_edge (e, names[1])
5507 || has_single_use (names[1]))
5508 names[1] = NULL_TREE;
5510 if (live_on_edge (e, name2)
5511 && !has_single_use (name2))
5512 names[0] = name2;
5515 if (names[0] || names[1])
5517 wide_int minv, maxv, valv, cst2v;
5518 wide_int tem, sgnbit;
5519 bool valid_p = false, valn, cst2n;
5520 enum tree_code ccode = comp_code;
5522 valv = wide_int::from (val, nprec, UNSIGNED);
5523 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5524 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5525 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5526 /* If CST2 doesn't have most significant bit set,
5527 but VAL is negative, we have comparison like
5528 if ((x & 0x123) > -4) (always true). Just give up. */
5529 if (!cst2n && valn)
5530 ccode = ERROR_MARK;
5531 if (cst2n)
5532 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5533 else
5534 sgnbit = wi::zero (nprec);
5535 minv = valv & cst2v;
5536 switch (ccode)
5538 case EQ_EXPR:
5539 /* Minimum unsigned value for equality is VAL & CST2
5540 (should be equal to VAL, otherwise we probably should
5541 have folded the comparison into false) and
5542 maximum unsigned value is VAL | ~CST2. */
5543 maxv = valv | ~cst2v;
5544 valid_p = true;
5545 break;
5547 case NE_EXPR:
5548 tem = valv | ~cst2v;
5549 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5550 if (valv == 0)
5552 cst2n = false;
5553 sgnbit = wi::zero (nprec);
5554 goto gt_expr;
5556 /* If (VAL | ~CST2) is all ones, handle it as
5557 (X & CST2) < VAL. */
5558 if (tem == -1)
5560 cst2n = false;
5561 valn = false;
5562 sgnbit = wi::zero (nprec);
5563 goto lt_expr;
5565 if (!cst2n && wi::neg_p (cst2v))
5566 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5567 if (sgnbit != 0)
5569 if (valv == sgnbit)
5571 cst2n = true;
5572 valn = true;
5573 goto gt_expr;
5575 if (tem == wi::mask (nprec - 1, false, nprec))
5577 cst2n = true;
5578 goto lt_expr;
5580 if (!cst2n)
5581 sgnbit = wi::zero (nprec);
5583 break;
5585 case GE_EXPR:
5586 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5587 is VAL and maximum unsigned value is ~0. For signed
5588 comparison, if CST2 doesn't have most significant bit
5589 set, handle it similarly. If CST2 has MSB set,
5590 the minimum is the same, and maximum is ~0U/2. */
5591 if (minv != valv)
5593 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5594 VAL. */
5595 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5596 if (minv == valv)
5597 break;
5599 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5600 valid_p = true;
5601 break;
5603 case GT_EXPR:
5604 gt_expr:
5605 /* Find out smallest MINV where MINV > VAL
5606 && (MINV & CST2) == MINV, if any. If VAL is signed and
5607 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5608 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5609 if (minv == valv)
5610 break;
5611 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5612 valid_p = true;
5613 break;
5615 case LE_EXPR:
5616 /* Minimum unsigned value for <= is 0 and maximum
5617 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5618 Otherwise, find smallest VAL2 where VAL2 > VAL
5619 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5620 as maximum.
5621 For signed comparison, if CST2 doesn't have most
5622 significant bit set, handle it similarly. If CST2 has
5623 MSB set, the maximum is the same and minimum is INT_MIN. */
5624 if (minv == valv)
5625 maxv = valv;
5626 else
5628 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5629 if (maxv == valv)
5630 break;
5631 maxv -= 1;
5633 maxv |= ~cst2v;
5634 minv = sgnbit;
5635 valid_p = true;
5636 break;
5638 case LT_EXPR:
5639 lt_expr:
5640 /* Minimum unsigned value for < is 0 and maximum
5641 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5642 Otherwise, find smallest VAL2 where VAL2 > VAL
5643 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5644 as maximum.
5645 For signed comparison, if CST2 doesn't have most
5646 significant bit set, handle it similarly. If CST2 has
5647 MSB set, the maximum is the same and minimum is INT_MIN. */
5648 if (minv == valv)
5650 if (valv == sgnbit)
5651 break;
5652 maxv = valv;
5654 else
5656 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5657 if (maxv == valv)
5658 break;
5660 maxv -= 1;
5661 maxv |= ~cst2v;
5662 minv = sgnbit;
5663 valid_p = true;
5664 break;
5666 default:
5667 break;
5669 if (valid_p
5670 && (maxv - minv) != -1)
5672 tree tmp, new_val, type;
5673 int i;
5675 for (i = 0; i < 2; i++)
5676 if (names[i])
5678 wide_int maxv2 = maxv;
5679 tmp = names[i];
5680 type = TREE_TYPE (names[i]);
5681 if (!TYPE_UNSIGNED (type))
5683 type = build_nonstandard_integer_type (nprec, 1);
5684 tmp = build1 (NOP_EXPR, type, names[i]);
5686 if (minv != 0)
5688 tmp = build2 (PLUS_EXPR, type, tmp,
5689 wide_int_to_tree (type, -minv));
5690 maxv2 = maxv - minv;
5692 new_val = wide_int_to_tree (type, maxv2);
5694 if (dump_file)
5696 fprintf (dump_file, "Adding assert for ");
5697 print_generic_expr (dump_file, names[i], 0);
5698 fprintf (dump_file, " from ");
5699 print_generic_expr (dump_file, tmp, 0);
5700 fprintf (dump_file, "\n");
5703 register_new_assert_for (names[i], tmp, LE_EXPR,
5704 new_val, NULL, e, bsi);
5711 /* OP is an operand of a truth value expression which is known to have
5712 a particular value. Register any asserts for OP and for any
5713 operands in OP's defining statement.
5715 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5716 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5718 static void
5719 register_edge_assert_for_1 (tree op, enum tree_code code,
5720 edge e, gimple_stmt_iterator bsi)
5722 gimple op_def;
5723 tree val;
5724 enum tree_code rhs_code;
5726 /* We only care about SSA_NAMEs. */
5727 if (TREE_CODE (op) != SSA_NAME)
5728 return;
5730 /* We know that OP will have a zero or nonzero value. If OP is used
5731 more than once go ahead and register an assert for OP. */
5732 if (live_on_edge (e, op)
5733 && !has_single_use (op))
5735 val = build_int_cst (TREE_TYPE (op), 0);
5736 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5739 /* Now look at how OP is set. If it's set from a comparison,
5740 a truth operation or some bit operations, then we may be able
5741 to register information about the operands of that assignment. */
5742 op_def = SSA_NAME_DEF_STMT (op);
5743 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5744 return;
5746 rhs_code = gimple_assign_rhs_code (op_def);
5748 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5750 bool invert = (code == EQ_EXPR ? true : false);
5751 tree op0 = gimple_assign_rhs1 (op_def);
5752 tree op1 = gimple_assign_rhs2 (op_def);
5754 if (TREE_CODE (op0) == SSA_NAME)
5755 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5756 if (TREE_CODE (op1) == SSA_NAME)
5757 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5759 else if ((code == NE_EXPR
5760 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5761 || (code == EQ_EXPR
5762 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5764 /* Recurse on each operand. */
5765 tree op0 = gimple_assign_rhs1 (op_def);
5766 tree op1 = gimple_assign_rhs2 (op_def);
5767 if (TREE_CODE (op0) == SSA_NAME
5768 && has_single_use (op0))
5769 register_edge_assert_for_1 (op0, code, e, bsi);
5770 if (TREE_CODE (op1) == SSA_NAME
5771 && has_single_use (op1))
5772 register_edge_assert_for_1 (op1, code, e, bsi);
5774 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5775 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5777 /* Recurse, flipping CODE. */
5778 code = invert_tree_comparison (code, false);
5779 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5781 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5783 /* Recurse through the copy. */
5784 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5786 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5788 /* Recurse through the type conversion, unless it is a narrowing
5789 conversion or conversion from non-integral type. */
5790 tree rhs = gimple_assign_rhs1 (op_def);
5791 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5792 && (TYPE_PRECISION (TREE_TYPE (rhs))
5793 <= TYPE_PRECISION (TREE_TYPE (op))))
5794 register_edge_assert_for_1 (rhs, code, e, bsi);
5798 /* Try to register an edge assertion for SSA name NAME on edge E for
5799 the condition COND contributing to the conditional jump pointed to by
5800 SI. */
5802 static void
5803 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5804 enum tree_code cond_code, tree cond_op0,
5805 tree cond_op1)
5807 tree val;
5808 enum tree_code comp_code;
5809 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5811 /* Do not attempt to infer anything in names that flow through
5812 abnormal edges. */
5813 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5814 return;
5816 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5817 cond_op0, cond_op1,
5818 is_else_edge,
5819 &comp_code, &val))
5820 return;
5822 /* Register ASSERT_EXPRs for name. */
5823 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5824 cond_op1, is_else_edge);
5827 /* If COND is effectively an equality test of an SSA_NAME against
5828 the value zero or one, then we may be able to assert values
5829 for SSA_NAMEs which flow into COND. */
5831 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5832 statement of NAME we can assert both operands of the BIT_AND_EXPR
5833 have nonzero value. */
5834 if (((comp_code == EQ_EXPR && integer_onep (val))
5835 || (comp_code == NE_EXPR && integer_zerop (val))))
5837 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5839 if (is_gimple_assign (def_stmt)
5840 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5842 tree op0 = gimple_assign_rhs1 (def_stmt);
5843 tree op1 = gimple_assign_rhs2 (def_stmt);
5844 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5845 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5849 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5850 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5851 have zero value. */
5852 if (((comp_code == EQ_EXPR && integer_zerop (val))
5853 || (comp_code == NE_EXPR && integer_onep (val))))
5855 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5857 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5858 necessarily zero value, or if type-precision is one. */
5859 if (is_gimple_assign (def_stmt)
5860 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5861 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5862 || comp_code == EQ_EXPR)))
5864 tree op0 = gimple_assign_rhs1 (def_stmt);
5865 tree op1 = gimple_assign_rhs2 (def_stmt);
5866 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5867 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5873 /* Determine whether the outgoing edges of BB should receive an
5874 ASSERT_EXPR for each of the operands of BB's LAST statement.
5875 The last statement of BB must be a COND_EXPR.
5877 If any of the sub-graphs rooted at BB have an interesting use of
5878 the predicate operands, an assert location node is added to the
5879 list of assertions for the corresponding operands. */
5881 static void
5882 find_conditional_asserts (basic_block bb, gcond *last)
5884 gimple_stmt_iterator bsi;
5885 tree op;
5886 edge_iterator ei;
5887 edge e;
5888 ssa_op_iter iter;
5890 bsi = gsi_for_stmt (last);
5892 /* Look for uses of the operands in each of the sub-graphs
5893 rooted at BB. We need to check each of the outgoing edges
5894 separately, so that we know what kind of ASSERT_EXPR to
5895 insert. */
5896 FOR_EACH_EDGE (e, ei, bb->succs)
5898 if (e->dest == bb)
5899 continue;
5901 /* Register the necessary assertions for each operand in the
5902 conditional predicate. */
5903 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5904 register_edge_assert_for (op, e, bsi,
5905 gimple_cond_code (last),
5906 gimple_cond_lhs (last),
5907 gimple_cond_rhs (last));
5911 struct case_info
5913 tree expr;
5914 basic_block bb;
5917 /* Compare two case labels sorting first by the destination bb index
5918 and then by the case value. */
5920 static int
5921 compare_case_labels (const void *p1, const void *p2)
5923 const struct case_info *ci1 = (const struct case_info *) p1;
5924 const struct case_info *ci2 = (const struct case_info *) p2;
5925 int idx1 = ci1->bb->index;
5926 int idx2 = ci2->bb->index;
5928 if (idx1 < idx2)
5929 return -1;
5930 else if (idx1 == idx2)
5932 /* Make sure the default label is first in a group. */
5933 if (!CASE_LOW (ci1->expr))
5934 return -1;
5935 else if (!CASE_LOW (ci2->expr))
5936 return 1;
5937 else
5938 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5939 CASE_LOW (ci2->expr));
5941 else
5942 return 1;
5945 /* Determine whether the outgoing edges of BB should receive an
5946 ASSERT_EXPR for each of the operands of BB's LAST statement.
5947 The last statement of BB must be a SWITCH_EXPR.
5949 If any of the sub-graphs rooted at BB have an interesting use of
5950 the predicate operands, an assert location node is added to the
5951 list of assertions for the corresponding operands. */
5953 static void
5954 find_switch_asserts (basic_block bb, gswitch *last)
5956 gimple_stmt_iterator bsi;
5957 tree op;
5958 edge e;
5959 struct case_info *ci;
5960 size_t n = gimple_switch_num_labels (last);
5961 #if GCC_VERSION >= 4000
5962 unsigned int idx;
5963 #else
5964 /* Work around GCC 3.4 bug (PR 37086). */
5965 volatile unsigned int idx;
5966 #endif
5968 bsi = gsi_for_stmt (last);
5969 op = gimple_switch_index (last);
5970 if (TREE_CODE (op) != SSA_NAME)
5971 return;
5973 /* Build a vector of case labels sorted by destination label. */
5974 ci = XNEWVEC (struct case_info, n);
5975 for (idx = 0; idx < n; ++idx)
5977 ci[idx].expr = gimple_switch_label (last, idx);
5978 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5980 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5982 for (idx = 0; idx < n; ++idx)
5984 tree min, max;
5985 tree cl = ci[idx].expr;
5986 basic_block cbb = ci[idx].bb;
5988 min = CASE_LOW (cl);
5989 max = CASE_HIGH (cl);
5991 /* If there are multiple case labels with the same destination
5992 we need to combine them to a single value range for the edge. */
5993 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5995 /* Skip labels until the last of the group. */
5996 do {
5997 ++idx;
5998 } while (idx < n && cbb == ci[idx].bb);
5999 --idx;
6001 /* Pick up the maximum of the case label range. */
6002 if (CASE_HIGH (ci[idx].expr))
6003 max = CASE_HIGH (ci[idx].expr);
6004 else
6005 max = CASE_LOW (ci[idx].expr);
6008 /* Nothing to do if the range includes the default label until we
6009 can register anti-ranges. */
6010 if (min == NULL_TREE)
6011 continue;
6013 /* Find the edge to register the assert expr on. */
6014 e = find_edge (bb, cbb);
6016 /* Register the necessary assertions for the operand in the
6017 SWITCH_EXPR. */
6018 register_edge_assert_for (op, e, bsi,
6019 max ? GE_EXPR : EQ_EXPR,
6020 op, fold_convert (TREE_TYPE (op), min));
6021 if (max)
6022 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6023 fold_convert (TREE_TYPE (op), max));
6026 XDELETEVEC (ci);
6030 /* Traverse all the statements in block BB looking for statements that
6031 may generate useful assertions for the SSA names in their operand.
6032 If a statement produces a useful assertion A for name N_i, then the
6033 list of assertions already generated for N_i is scanned to
6034 determine if A is actually needed.
6036 If N_i already had the assertion A at a location dominating the
6037 current location, then nothing needs to be done. Otherwise, the
6038 new location for A is recorded instead.
6040 1- For every statement S in BB, all the variables used by S are
6041 added to bitmap FOUND_IN_SUBGRAPH.
6043 2- If statement S uses an operand N in a way that exposes a known
6044 value range for N, then if N was not already generated by an
6045 ASSERT_EXPR, create a new assert location for N. For instance,
6046 if N is a pointer and the statement dereferences it, we can
6047 assume that N is not NULL.
6049 3- COND_EXPRs are a special case of #2. We can derive range
6050 information from the predicate but need to insert different
6051 ASSERT_EXPRs for each of the sub-graphs rooted at the
6052 conditional block. If the last statement of BB is a conditional
6053 expression of the form 'X op Y', then
6055 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6057 b) If the conditional is the only entry point to the sub-graph
6058 corresponding to the THEN_CLAUSE, recurse into it. On
6059 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6060 an ASSERT_EXPR is added for the corresponding variable.
6062 c) Repeat step (b) on the ELSE_CLAUSE.
6064 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6066 For instance,
6068 if (a == 9)
6069 b = a;
6070 else
6071 b = c + 1;
6073 In this case, an assertion on the THEN clause is useful to
6074 determine that 'a' is always 9 on that edge. However, an assertion
6075 on the ELSE clause would be unnecessary.
6077 4- If BB does not end in a conditional expression, then we recurse
6078 into BB's dominator children.
6080 At the end of the recursive traversal, every SSA name will have a
6081 list of locations where ASSERT_EXPRs should be added. When a new
6082 location for name N is found, it is registered by calling
6083 register_new_assert_for. That function keeps track of all the
6084 registered assertions to prevent adding unnecessary assertions.
6085 For instance, if a pointer P_4 is dereferenced more than once in a
6086 dominator tree, only the location dominating all the dereference of
6087 P_4 will receive an ASSERT_EXPR. */
6089 static void
6090 find_assert_locations_1 (basic_block bb, sbitmap live)
6092 gimple last;
6094 last = last_stmt (bb);
6096 /* If BB's last statement is a conditional statement involving integer
6097 operands, determine if we need to add ASSERT_EXPRs. */
6098 if (last
6099 && gimple_code (last) == GIMPLE_COND
6100 && !fp_predicate (last)
6101 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6102 find_conditional_asserts (bb, as_a <gcond *> (last));
6104 /* If BB's last statement is a switch statement involving integer
6105 operands, determine if we need to add ASSERT_EXPRs. */
6106 if (last
6107 && gimple_code (last) == GIMPLE_SWITCH
6108 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6109 find_switch_asserts (bb, as_a <gswitch *> (last));
6111 /* Traverse all the statements in BB marking used names and looking
6112 for statements that may infer assertions for their used operands. */
6113 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6114 gsi_prev (&si))
6116 gimple stmt;
6117 tree op;
6118 ssa_op_iter i;
6120 stmt = gsi_stmt (si);
6122 if (is_gimple_debug (stmt))
6123 continue;
6125 /* See if we can derive an assertion for any of STMT's operands. */
6126 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6128 tree value;
6129 enum tree_code comp_code;
6131 /* If op is not live beyond this stmt, do not bother to insert
6132 asserts for it. */
6133 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6134 continue;
6136 /* If OP is used in such a way that we can infer a value
6137 range for it, and we don't find a previous assertion for
6138 it, create a new assertion location node for OP. */
6139 if (infer_value_range (stmt, op, &comp_code, &value))
6141 /* If we are able to infer a nonzero value range for OP,
6142 then walk backwards through the use-def chain to see if OP
6143 was set via a typecast.
6145 If so, then we can also infer a nonzero value range
6146 for the operand of the NOP_EXPR. */
6147 if (comp_code == NE_EXPR && integer_zerop (value))
6149 tree t = op;
6150 gimple def_stmt = SSA_NAME_DEF_STMT (t);
6152 while (is_gimple_assign (def_stmt)
6153 && CONVERT_EXPR_CODE_P
6154 (gimple_assign_rhs_code (def_stmt))
6155 && TREE_CODE
6156 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6157 && POINTER_TYPE_P
6158 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6160 t = gimple_assign_rhs1 (def_stmt);
6161 def_stmt = SSA_NAME_DEF_STMT (t);
6163 /* Note we want to register the assert for the
6164 operand of the NOP_EXPR after SI, not after the
6165 conversion. */
6166 if (! has_single_use (t))
6167 register_new_assert_for (t, t, comp_code, value,
6168 bb, NULL, si);
6172 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6176 /* Update live. */
6177 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6178 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6179 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6180 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6183 /* Traverse all PHI nodes in BB, updating live. */
6184 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6185 gsi_next (&si))
6187 use_operand_p arg_p;
6188 ssa_op_iter i;
6189 gphi *phi = si.phi ();
6190 tree res = gimple_phi_result (phi);
6192 if (virtual_operand_p (res))
6193 continue;
6195 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6197 tree arg = USE_FROM_PTR (arg_p);
6198 if (TREE_CODE (arg) == SSA_NAME)
6199 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6202 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6206 /* Do an RPO walk over the function computing SSA name liveness
6207 on-the-fly and deciding on assert expressions to insert. */
6209 static void
6210 find_assert_locations (void)
6212 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6213 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6214 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6215 int rpo_cnt, i;
6217 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6218 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6219 for (i = 0; i < rpo_cnt; ++i)
6220 bb_rpo[rpo[i]] = i;
6222 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6223 the order we compute liveness and insert asserts we otherwise
6224 fail to insert asserts into the loop latch. */
6225 loop_p loop;
6226 FOR_EACH_LOOP (loop, 0)
6228 i = loop->latch->index;
6229 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6230 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6231 !gsi_end_p (gsi); gsi_next (&gsi))
6233 gphi *phi = gsi.phi ();
6234 if (virtual_operand_p (gimple_phi_result (phi)))
6235 continue;
6236 tree arg = gimple_phi_arg_def (phi, j);
6237 if (TREE_CODE (arg) == SSA_NAME)
6239 if (live[i] == NULL)
6241 live[i] = sbitmap_alloc (num_ssa_names);
6242 bitmap_clear (live[i]);
6244 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6249 for (i = rpo_cnt - 1; i >= 0; --i)
6251 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6252 edge e;
6253 edge_iterator ei;
6255 if (!live[rpo[i]])
6257 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6258 bitmap_clear (live[rpo[i]]);
6261 /* Process BB and update the live information with uses in
6262 this block. */
6263 find_assert_locations_1 (bb, live[rpo[i]]);
6265 /* Merge liveness into the predecessor blocks and free it. */
6266 if (!bitmap_empty_p (live[rpo[i]]))
6268 int pred_rpo = i;
6269 FOR_EACH_EDGE (e, ei, bb->preds)
6271 int pred = e->src->index;
6272 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6273 continue;
6275 if (!live[pred])
6277 live[pred] = sbitmap_alloc (num_ssa_names);
6278 bitmap_clear (live[pred]);
6280 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6282 if (bb_rpo[pred] < pred_rpo)
6283 pred_rpo = bb_rpo[pred];
6286 /* Record the RPO number of the last visited block that needs
6287 live information from this block. */
6288 last_rpo[rpo[i]] = pred_rpo;
6290 else
6292 sbitmap_free (live[rpo[i]]);
6293 live[rpo[i]] = NULL;
6296 /* We can free all successors live bitmaps if all their
6297 predecessors have been visited already. */
6298 FOR_EACH_EDGE (e, ei, bb->succs)
6299 if (last_rpo[e->dest->index] == i
6300 && live[e->dest->index])
6302 sbitmap_free (live[e->dest->index]);
6303 live[e->dest->index] = NULL;
6307 XDELETEVEC (rpo);
6308 XDELETEVEC (bb_rpo);
6309 XDELETEVEC (last_rpo);
6310 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6311 if (live[i])
6312 sbitmap_free (live[i]);
6313 XDELETEVEC (live);
6316 /* Create an ASSERT_EXPR for NAME and insert it in the location
6317 indicated by LOC. Return true if we made any edge insertions. */
6319 static bool
6320 process_assert_insertions_for (tree name, assert_locus_t loc)
6322 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6323 gimple stmt;
6324 tree cond;
6325 gimple assert_stmt;
6326 edge_iterator ei;
6327 edge e;
6329 /* If we have X <=> X do not insert an assert expr for that. */
6330 if (loc->expr == loc->val)
6331 return false;
6333 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6334 assert_stmt = build_assert_expr_for (cond, name);
6335 if (loc->e)
6337 /* We have been asked to insert the assertion on an edge. This
6338 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6339 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6340 || (gimple_code (gsi_stmt (loc->si))
6341 == GIMPLE_SWITCH));
6343 gsi_insert_on_edge (loc->e, assert_stmt);
6344 return true;
6347 /* Otherwise, we can insert right after LOC->SI iff the
6348 statement must not be the last statement in the block. */
6349 stmt = gsi_stmt (loc->si);
6350 if (!stmt_ends_bb_p (stmt))
6352 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6353 return false;
6356 /* If STMT must be the last statement in BB, we can only insert new
6357 assertions on the non-abnormal edge out of BB. Note that since
6358 STMT is not control flow, there may only be one non-abnormal edge
6359 out of BB. */
6360 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6361 if (!(e->flags & EDGE_ABNORMAL))
6363 gsi_insert_on_edge (e, assert_stmt);
6364 return true;
6367 gcc_unreachable ();
6371 /* Process all the insertions registered for every name N_i registered
6372 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6373 found in ASSERTS_FOR[i]. */
6375 static void
6376 process_assert_insertions (void)
6378 unsigned i;
6379 bitmap_iterator bi;
6380 bool update_edges_p = false;
6381 int num_asserts = 0;
6383 if (dump_file && (dump_flags & TDF_DETAILS))
6384 dump_all_asserts (dump_file);
6386 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6388 assert_locus_t loc = asserts_for[i];
6389 gcc_assert (loc);
6391 while (loc)
6393 assert_locus_t next = loc->next;
6394 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6395 free (loc);
6396 loc = next;
6397 num_asserts++;
6401 if (update_edges_p)
6402 gsi_commit_edge_inserts ();
6404 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6405 num_asserts);
6409 /* Traverse the flowgraph looking for conditional jumps to insert range
6410 expressions. These range expressions are meant to provide information
6411 to optimizations that need to reason in terms of value ranges. They
6412 will not be expanded into RTL. For instance, given:
6414 x = ...
6415 y = ...
6416 if (x < y)
6417 y = x - 2;
6418 else
6419 x = y + 3;
6421 this pass will transform the code into:
6423 x = ...
6424 y = ...
6425 if (x < y)
6427 x = ASSERT_EXPR <x, x < y>
6428 y = x - 2
6430 else
6432 y = ASSERT_EXPR <y, x >= y>
6433 x = y + 3
6436 The idea is that once copy and constant propagation have run, other
6437 optimizations will be able to determine what ranges of values can 'x'
6438 take in different paths of the code, simply by checking the reaching
6439 definition of 'x'. */
6441 static void
6442 insert_range_assertions (void)
6444 need_assert_for = BITMAP_ALLOC (NULL);
6445 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6447 calculate_dominance_info (CDI_DOMINATORS);
6449 find_assert_locations ();
6450 if (!bitmap_empty_p (need_assert_for))
6452 process_assert_insertions ();
6453 update_ssa (TODO_update_ssa_no_phi);
6456 if (dump_file && (dump_flags & TDF_DETAILS))
6458 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6459 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6462 free (asserts_for);
6463 BITMAP_FREE (need_assert_for);
6466 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6467 and "struct" hacks. If VRP can determine that the
6468 array subscript is a constant, check if it is outside valid
6469 range. If the array subscript is a RANGE, warn if it is
6470 non-overlapping with valid range.
6471 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6473 static void
6474 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6476 value_range_t* vr = NULL;
6477 tree low_sub, up_sub;
6478 tree low_bound, up_bound, up_bound_p1;
6479 tree base;
6481 if (TREE_NO_WARNING (ref))
6482 return;
6484 low_sub = up_sub = TREE_OPERAND (ref, 1);
6485 up_bound = array_ref_up_bound (ref);
6487 /* Can not check flexible arrays. */
6488 if (!up_bound
6489 || TREE_CODE (up_bound) != INTEGER_CST)
6490 return;
6492 /* Accesses to trailing arrays via pointers may access storage
6493 beyond the types array bounds. */
6494 base = get_base_address (ref);
6495 if (base && TREE_CODE (base) == MEM_REF)
6497 tree cref, next = NULL_TREE;
6499 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6500 return;
6502 cref = TREE_OPERAND (ref, 0);
6503 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6504 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6505 next && TREE_CODE (next) != FIELD_DECL;
6506 next = DECL_CHAIN (next))
6509 /* If this is the last field in a struct type or a field in a
6510 union type do not warn. */
6511 if (!next)
6512 return;
6515 low_bound = array_ref_low_bound (ref);
6516 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6517 build_int_cst (TREE_TYPE (up_bound), 1));
6519 if (TREE_CODE (low_sub) == SSA_NAME)
6521 vr = get_value_range (low_sub);
6522 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6524 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6525 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6529 if (vr && vr->type == VR_ANTI_RANGE)
6531 if (TREE_CODE (up_sub) == INTEGER_CST
6532 && tree_int_cst_lt (up_bound, up_sub)
6533 && TREE_CODE (low_sub) == INTEGER_CST
6534 && tree_int_cst_lt (low_sub, low_bound))
6536 warning_at (location, OPT_Warray_bounds,
6537 "array subscript is outside array bounds");
6538 TREE_NO_WARNING (ref) = 1;
6541 else if (TREE_CODE (up_sub) == INTEGER_CST
6542 && (ignore_off_by_one
6543 ? (tree_int_cst_lt (up_bound, up_sub)
6544 && !tree_int_cst_equal (up_bound_p1, up_sub))
6545 : (tree_int_cst_lt (up_bound, up_sub)
6546 || tree_int_cst_equal (up_bound_p1, up_sub))))
6548 if (dump_file && (dump_flags & TDF_DETAILS))
6550 fprintf (dump_file, "Array bound warning for ");
6551 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6552 fprintf (dump_file, "\n");
6554 warning_at (location, OPT_Warray_bounds,
6555 "array subscript is above array bounds");
6556 TREE_NO_WARNING (ref) = 1;
6558 else if (TREE_CODE (low_sub) == INTEGER_CST
6559 && tree_int_cst_lt (low_sub, low_bound))
6561 if (dump_file && (dump_flags & TDF_DETAILS))
6563 fprintf (dump_file, "Array bound warning for ");
6564 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6565 fprintf (dump_file, "\n");
6567 warning_at (location, OPT_Warray_bounds,
6568 "array subscript is below array bounds");
6569 TREE_NO_WARNING (ref) = 1;
6573 /* Searches if the expr T, located at LOCATION computes
6574 address of an ARRAY_REF, and call check_array_ref on it. */
6576 static void
6577 search_for_addr_array (tree t, location_t location)
6579 while (TREE_CODE (t) == SSA_NAME)
6581 gimple g = SSA_NAME_DEF_STMT (t);
6583 if (gimple_code (g) != GIMPLE_ASSIGN)
6584 return;
6586 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6587 != GIMPLE_SINGLE_RHS)
6588 return;
6590 t = gimple_assign_rhs1 (g);
6594 /* We are only interested in addresses of ARRAY_REF's. */
6595 if (TREE_CODE (t) != ADDR_EXPR)
6596 return;
6598 /* Check each ARRAY_REFs in the reference chain. */
6601 if (TREE_CODE (t) == ARRAY_REF)
6602 check_array_ref (location, t, true /*ignore_off_by_one*/);
6604 t = TREE_OPERAND (t, 0);
6606 while (handled_component_p (t));
6608 if (TREE_CODE (t) == MEM_REF
6609 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6610 && !TREE_NO_WARNING (t))
6612 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6613 tree low_bound, up_bound, el_sz;
6614 offset_int idx;
6615 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6616 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6617 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6618 return;
6620 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6621 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6622 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6623 if (!low_bound
6624 || TREE_CODE (low_bound) != INTEGER_CST
6625 || !up_bound
6626 || TREE_CODE (up_bound) != INTEGER_CST
6627 || !el_sz
6628 || TREE_CODE (el_sz) != INTEGER_CST)
6629 return;
6631 idx = mem_ref_offset (t);
6632 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6633 if (wi::lts_p (idx, 0))
6635 if (dump_file && (dump_flags & TDF_DETAILS))
6637 fprintf (dump_file, "Array bound warning for ");
6638 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6639 fprintf (dump_file, "\n");
6641 warning_at (location, OPT_Warray_bounds,
6642 "array subscript is below array bounds");
6643 TREE_NO_WARNING (t) = 1;
6645 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6646 - wi::to_offset (low_bound) + 1)))
6648 if (dump_file && (dump_flags & TDF_DETAILS))
6650 fprintf (dump_file, "Array bound warning for ");
6651 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6652 fprintf (dump_file, "\n");
6654 warning_at (location, OPT_Warray_bounds,
6655 "array subscript is above array bounds");
6656 TREE_NO_WARNING (t) = 1;
6661 /* walk_tree() callback that checks if *TP is
6662 an ARRAY_REF inside an ADDR_EXPR (in which an array
6663 subscript one outside the valid range is allowed). Call
6664 check_array_ref for each ARRAY_REF found. The location is
6665 passed in DATA. */
6667 static tree
6668 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6670 tree t = *tp;
6671 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6672 location_t location;
6674 if (EXPR_HAS_LOCATION (t))
6675 location = EXPR_LOCATION (t);
6676 else
6678 location_t *locp = (location_t *) wi->info;
6679 location = *locp;
6682 *walk_subtree = TRUE;
6684 if (TREE_CODE (t) == ARRAY_REF)
6685 check_array_ref (location, t, false /*ignore_off_by_one*/);
6687 if (TREE_CODE (t) == MEM_REF
6688 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6689 search_for_addr_array (TREE_OPERAND (t, 0), location);
6691 if (TREE_CODE (t) == ADDR_EXPR)
6692 *walk_subtree = FALSE;
6694 return NULL_TREE;
6697 /* Walk over all statements of all reachable BBs and call check_array_bounds
6698 on them. */
6700 static void
6701 check_all_array_refs (void)
6703 basic_block bb;
6704 gimple_stmt_iterator si;
6706 FOR_EACH_BB_FN (bb, cfun)
6708 edge_iterator ei;
6709 edge e;
6710 bool executable = false;
6712 /* Skip blocks that were found to be unreachable. */
6713 FOR_EACH_EDGE (e, ei, bb->preds)
6714 executable |= !!(e->flags & EDGE_EXECUTABLE);
6715 if (!executable)
6716 continue;
6718 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6720 gimple stmt = gsi_stmt (si);
6721 struct walk_stmt_info wi;
6722 if (!gimple_has_location (stmt))
6723 continue;
6725 if (is_gimple_call (stmt))
6727 size_t i;
6728 size_t n = gimple_call_num_args (stmt);
6729 for (i = 0; i < n; i++)
6731 tree arg = gimple_call_arg (stmt, i);
6732 search_for_addr_array (arg, gimple_location (stmt));
6735 else
6737 memset (&wi, 0, sizeof (wi));
6738 wi.info = CONST_CAST (void *, (const void *)
6739 gimple_location_ptr (stmt));
6741 walk_gimple_op (gsi_stmt (si),
6742 check_array_bounds,
6743 &wi);
6749 /* Return true if all imm uses of VAR are either in STMT, or
6750 feed (optionally through a chain of single imm uses) GIMPLE_COND
6751 in basic block COND_BB. */
6753 static bool
6754 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6756 use_operand_p use_p, use2_p;
6757 imm_use_iterator iter;
6759 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6760 if (USE_STMT (use_p) != stmt)
6762 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6763 if (is_gimple_debug (use_stmt))
6764 continue;
6765 while (is_gimple_assign (use_stmt)
6766 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6767 && single_imm_use (gimple_assign_lhs (use_stmt),
6768 &use2_p, &use_stmt2))
6769 use_stmt = use_stmt2;
6770 if (gimple_code (use_stmt) != GIMPLE_COND
6771 || gimple_bb (use_stmt) != cond_bb)
6772 return false;
6774 return true;
6777 /* Handle
6778 _4 = x_3 & 31;
6779 if (_4 != 0)
6780 goto <bb 6>;
6781 else
6782 goto <bb 7>;
6783 <bb 6>:
6784 __builtin_unreachable ();
6785 <bb 7>:
6786 x_5 = ASSERT_EXPR <x_3, ...>;
6787 If x_3 has no other immediate uses (checked by caller),
6788 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6789 from the non-zero bitmask. */
6791 static void
6792 maybe_set_nonzero_bits (basic_block bb, tree var)
6794 edge e = single_pred_edge (bb);
6795 basic_block cond_bb = e->src;
6796 gimple stmt = last_stmt (cond_bb);
6797 tree cst;
6799 if (stmt == NULL
6800 || gimple_code (stmt) != GIMPLE_COND
6801 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6802 ? EQ_EXPR : NE_EXPR)
6803 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6804 || !integer_zerop (gimple_cond_rhs (stmt)))
6805 return;
6807 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6808 if (!is_gimple_assign (stmt)
6809 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6810 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6811 return;
6812 if (gimple_assign_rhs1 (stmt) != var)
6814 gimple stmt2;
6816 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6817 return;
6818 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6819 if (!gimple_assign_cast_p (stmt2)
6820 || gimple_assign_rhs1 (stmt2) != var
6821 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6822 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6823 != TYPE_PRECISION (TREE_TYPE (var))))
6824 return;
6826 cst = gimple_assign_rhs2 (stmt);
6827 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6830 /* Convert range assertion expressions into the implied copies and
6831 copy propagate away the copies. Doing the trivial copy propagation
6832 here avoids the need to run the full copy propagation pass after
6833 VRP.
6835 FIXME, this will eventually lead to copy propagation removing the
6836 names that had useful range information attached to them. For
6837 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6838 then N_i will have the range [3, +INF].
6840 However, by converting the assertion into the implied copy
6841 operation N_i = N_j, we will then copy-propagate N_j into the uses
6842 of N_i and lose the range information. We may want to hold on to
6843 ASSERT_EXPRs a little while longer as the ranges could be used in
6844 things like jump threading.
6846 The problem with keeping ASSERT_EXPRs around is that passes after
6847 VRP need to handle them appropriately.
6849 Another approach would be to make the range information a first
6850 class property of the SSA_NAME so that it can be queried from
6851 any pass. This is made somewhat more complex by the need for
6852 multiple ranges to be associated with one SSA_NAME. */
6854 static void
6855 remove_range_assertions (void)
6857 basic_block bb;
6858 gimple_stmt_iterator si;
6859 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6860 a basic block preceeded by GIMPLE_COND branching to it and
6861 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6862 int is_unreachable;
6864 /* Note that the BSI iterator bump happens at the bottom of the
6865 loop and no bump is necessary if we're removing the statement
6866 referenced by the current BSI. */
6867 FOR_EACH_BB_FN (bb, cfun)
6868 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6870 gimple stmt = gsi_stmt (si);
6871 gimple use_stmt;
6873 if (is_gimple_assign (stmt)
6874 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6876 tree lhs = gimple_assign_lhs (stmt);
6877 tree rhs = gimple_assign_rhs1 (stmt);
6878 tree var;
6879 tree cond = fold (ASSERT_EXPR_COND (rhs));
6880 use_operand_p use_p;
6881 imm_use_iterator iter;
6883 gcc_assert (cond != boolean_false_node);
6885 var = ASSERT_EXPR_VAR (rhs);
6886 gcc_assert (TREE_CODE (var) == SSA_NAME);
6888 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6889 && SSA_NAME_RANGE_INFO (lhs))
6891 if (is_unreachable == -1)
6893 is_unreachable = 0;
6894 if (single_pred_p (bb)
6895 && assert_unreachable_fallthru_edge_p
6896 (single_pred_edge (bb)))
6897 is_unreachable = 1;
6899 /* Handle
6900 if (x_7 >= 10 && x_7 < 20)
6901 __builtin_unreachable ();
6902 x_8 = ASSERT_EXPR <x_7, ...>;
6903 if the only uses of x_7 are in the ASSERT_EXPR and
6904 in the condition. In that case, we can copy the
6905 range info from x_8 computed in this pass also
6906 for x_7. */
6907 if (is_unreachable
6908 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6909 single_pred (bb)))
6911 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6912 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6913 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6914 maybe_set_nonzero_bits (bb, var);
6918 /* Propagate the RHS into every use of the LHS. */
6919 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6920 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6921 SET_USE (use_p, var);
6923 /* And finally, remove the copy, it is not needed. */
6924 gsi_remove (&si, true);
6925 release_defs (stmt);
6927 else
6929 if (!is_gimple_debug (gsi_stmt (si)))
6930 is_unreachable = 0;
6931 gsi_next (&si);
6937 /* Return true if STMT is interesting for VRP. */
6939 static bool
6940 stmt_interesting_for_vrp (gimple stmt)
6942 if (gimple_code (stmt) == GIMPLE_PHI)
6944 tree res = gimple_phi_result (stmt);
6945 return (!virtual_operand_p (res)
6946 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6947 || POINTER_TYPE_P (TREE_TYPE (res))));
6949 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6951 tree lhs = gimple_get_lhs (stmt);
6953 /* In general, assignments with virtual operands are not useful
6954 for deriving ranges, with the obvious exception of calls to
6955 builtin functions. */
6956 if (lhs && TREE_CODE (lhs) == SSA_NAME
6957 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6958 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6959 && (is_gimple_call (stmt)
6960 || !gimple_vuse (stmt)))
6961 return true;
6962 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6963 switch (gimple_call_internal_fn (stmt))
6965 case IFN_ADD_OVERFLOW:
6966 case IFN_SUB_OVERFLOW:
6967 case IFN_MUL_OVERFLOW:
6968 /* These internal calls return _Complex integer type,
6969 but are interesting to VRP nevertheless. */
6970 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6971 return true;
6972 break;
6973 default:
6974 break;
6977 else if (gimple_code (stmt) == GIMPLE_COND
6978 || gimple_code (stmt) == GIMPLE_SWITCH)
6979 return true;
6981 return false;
6985 /* Initialize local data structures for VRP. */
6987 static void
6988 vrp_initialize (void)
6990 basic_block bb;
6992 values_propagated = false;
6993 num_vr_values = num_ssa_names;
6994 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6995 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6997 FOR_EACH_BB_FN (bb, cfun)
6999 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7000 gsi_next (&si))
7002 gphi *phi = si.phi ();
7003 if (!stmt_interesting_for_vrp (phi))
7005 tree lhs = PHI_RESULT (phi);
7006 set_value_range_to_varying (get_value_range (lhs));
7007 prop_set_simulate_again (phi, false);
7009 else
7010 prop_set_simulate_again (phi, true);
7013 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7014 gsi_next (&si))
7016 gimple stmt = gsi_stmt (si);
7018 /* If the statement is a control insn, then we do not
7019 want to avoid simulating the statement once. Failure
7020 to do so means that those edges will never get added. */
7021 if (stmt_ends_bb_p (stmt))
7022 prop_set_simulate_again (stmt, true);
7023 else if (!stmt_interesting_for_vrp (stmt))
7025 ssa_op_iter i;
7026 tree def;
7027 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7028 set_value_range_to_varying (get_value_range (def));
7029 prop_set_simulate_again (stmt, false);
7031 else
7032 prop_set_simulate_again (stmt, true);
7037 /* Return the singleton value-range for NAME or NAME. */
7039 static inline tree
7040 vrp_valueize (tree name)
7042 if (TREE_CODE (name) == SSA_NAME)
7044 value_range_t *vr = get_value_range (name);
7045 if (vr->type == VR_RANGE
7046 && (vr->min == vr->max
7047 || operand_equal_p (vr->min, vr->max, 0)))
7048 return vr->min;
7050 return name;
7053 /* Return the singleton value-range for NAME if that is a constant
7054 but signal to not follow SSA edges. */
7056 static inline tree
7057 vrp_valueize_1 (tree name)
7059 if (TREE_CODE (name) == SSA_NAME)
7061 value_range_t *vr = get_value_range (name);
7062 if (range_int_cst_singleton_p (vr))
7063 return vr->min;
7064 /* If the definition may be simulated again we cannot follow
7065 this SSA edge as the SSA propagator does not necessarily
7066 re-visit the use. */
7067 gimple def_stmt = SSA_NAME_DEF_STMT (name);
7068 if (prop_simulate_again_p (def_stmt))
7069 return NULL_TREE;
7071 return name;
7074 /* Visit assignment STMT. If it produces an interesting range, record
7075 the SSA name in *OUTPUT_P. */
7077 static enum ssa_prop_result
7078 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
7080 tree def, lhs;
7081 ssa_op_iter iter;
7082 enum gimple_code code = gimple_code (stmt);
7083 lhs = gimple_get_lhs (stmt);
7085 /* We only keep track of ranges in integral and pointer types. */
7086 if (TREE_CODE (lhs) == SSA_NAME
7087 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7088 /* It is valid to have NULL MIN/MAX values on a type. See
7089 build_range_type. */
7090 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7091 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7092 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7094 value_range_t new_vr = VR_INITIALIZER;
7096 /* Try folding the statement to a constant first. */
7097 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7098 vrp_valueize_1);
7099 if (tem && is_gimple_min_invariant (tem))
7100 set_value_range_to_value (&new_vr, tem, NULL);
7101 /* Then dispatch to value-range extracting functions. */
7102 else if (code == GIMPLE_CALL)
7103 extract_range_basic (&new_vr, stmt);
7104 else
7105 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7107 if (update_value_range (lhs, &new_vr))
7109 *output_p = lhs;
7111 if (dump_file && (dump_flags & TDF_DETAILS))
7113 fprintf (dump_file, "Found new range for ");
7114 print_generic_expr (dump_file, lhs, 0);
7115 fprintf (dump_file, ": ");
7116 dump_value_range (dump_file, &new_vr);
7117 fprintf (dump_file, "\n");
7120 if (new_vr.type == VR_VARYING)
7121 return SSA_PROP_VARYING;
7123 return SSA_PROP_INTERESTING;
7126 return SSA_PROP_NOT_INTERESTING;
7128 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7129 switch (gimple_call_internal_fn (stmt))
7131 case IFN_ADD_OVERFLOW:
7132 case IFN_SUB_OVERFLOW:
7133 case IFN_MUL_OVERFLOW:
7134 /* These internal calls return _Complex integer type,
7135 which VRP does not track, but the immediate uses
7136 thereof might be interesting. */
7137 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7139 imm_use_iterator iter;
7140 use_operand_p use_p;
7141 enum ssa_prop_result res = SSA_PROP_VARYING;
7143 set_value_range_to_varying (get_value_range (lhs));
7145 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7147 gimple use_stmt = USE_STMT (use_p);
7148 if (!is_gimple_assign (use_stmt))
7149 continue;
7150 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7151 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7152 continue;
7153 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7154 tree use_lhs = gimple_assign_lhs (use_stmt);
7155 if (TREE_CODE (rhs1) != rhs_code
7156 || TREE_OPERAND (rhs1, 0) != lhs
7157 || TREE_CODE (use_lhs) != SSA_NAME
7158 || !stmt_interesting_for_vrp (use_stmt)
7159 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7160 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7161 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7162 continue;
7164 /* If there is a change in the value range for any of the
7165 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7166 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7167 or IMAGPART_EXPR immediate uses, but none of them have
7168 a change in their value ranges, return
7169 SSA_PROP_NOT_INTERESTING. If there are no
7170 {REAL,IMAG}PART_EXPR uses at all,
7171 return SSA_PROP_VARYING. */
7172 value_range_t new_vr = VR_INITIALIZER;
7173 extract_range_basic (&new_vr, use_stmt);
7174 value_range_t *old_vr = get_value_range (use_lhs);
7175 if (old_vr->type != new_vr.type
7176 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7177 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7178 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7179 res = SSA_PROP_INTERESTING;
7180 else
7181 res = SSA_PROP_NOT_INTERESTING;
7182 BITMAP_FREE (new_vr.equiv);
7183 if (res == SSA_PROP_INTERESTING)
7185 *output_p = lhs;
7186 return res;
7190 return res;
7192 break;
7193 default:
7194 break;
7197 /* Every other statement produces no useful ranges. */
7198 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7199 set_value_range_to_varying (get_value_range (def));
7201 return SSA_PROP_VARYING;
7204 /* Helper that gets the value range of the SSA_NAME with version I
7205 or a symbolic range containing the SSA_NAME only if the value range
7206 is varying or undefined. */
7208 static inline value_range_t
7209 get_vr_for_comparison (int i)
7211 value_range_t vr = *get_value_range (ssa_name (i));
7213 /* If name N_i does not have a valid range, use N_i as its own
7214 range. This allows us to compare against names that may
7215 have N_i in their ranges. */
7216 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7218 vr.type = VR_RANGE;
7219 vr.min = ssa_name (i);
7220 vr.max = ssa_name (i);
7223 return vr;
7226 /* Compare all the value ranges for names equivalent to VAR with VAL
7227 using comparison code COMP. Return the same value returned by
7228 compare_range_with_value, including the setting of
7229 *STRICT_OVERFLOW_P. */
7231 static tree
7232 compare_name_with_value (enum tree_code comp, tree var, tree val,
7233 bool *strict_overflow_p)
7235 bitmap_iterator bi;
7236 unsigned i;
7237 bitmap e;
7238 tree retval, t;
7239 int used_strict_overflow;
7240 bool sop;
7241 value_range_t equiv_vr;
7243 /* Get the set of equivalences for VAR. */
7244 e = get_value_range (var)->equiv;
7246 /* Start at -1. Set it to 0 if we do a comparison without relying
7247 on overflow, or 1 if all comparisons rely on overflow. */
7248 used_strict_overflow = -1;
7250 /* Compare vars' value range with val. */
7251 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7252 sop = false;
7253 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7254 if (retval)
7255 used_strict_overflow = sop ? 1 : 0;
7257 /* If the equiv set is empty we have done all work we need to do. */
7258 if (e == NULL)
7260 if (retval
7261 && used_strict_overflow > 0)
7262 *strict_overflow_p = true;
7263 return retval;
7266 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7268 equiv_vr = get_vr_for_comparison (i);
7269 sop = false;
7270 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7271 if (t)
7273 /* If we get different answers from different members
7274 of the equivalence set this check must be in a dead
7275 code region. Folding it to a trap representation
7276 would be correct here. For now just return don't-know. */
7277 if (retval != NULL
7278 && t != retval)
7280 retval = NULL_TREE;
7281 break;
7283 retval = t;
7285 if (!sop)
7286 used_strict_overflow = 0;
7287 else if (used_strict_overflow < 0)
7288 used_strict_overflow = 1;
7292 if (retval
7293 && used_strict_overflow > 0)
7294 *strict_overflow_p = true;
7296 return retval;
7300 /* Given a comparison code COMP and names N1 and N2, compare all the
7301 ranges equivalent to N1 against all the ranges equivalent to N2
7302 to determine the value of N1 COMP N2. Return the same value
7303 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7304 whether we relied on an overflow infinity in the comparison. */
7307 static tree
7308 compare_names (enum tree_code comp, tree n1, tree n2,
7309 bool *strict_overflow_p)
7311 tree t, retval;
7312 bitmap e1, e2;
7313 bitmap_iterator bi1, bi2;
7314 unsigned i1, i2;
7315 int used_strict_overflow;
7316 static bitmap_obstack *s_obstack = NULL;
7317 static bitmap s_e1 = NULL, s_e2 = NULL;
7319 /* Compare the ranges of every name equivalent to N1 against the
7320 ranges of every name equivalent to N2. */
7321 e1 = get_value_range (n1)->equiv;
7322 e2 = get_value_range (n2)->equiv;
7324 /* Use the fake bitmaps if e1 or e2 are not available. */
7325 if (s_obstack == NULL)
7327 s_obstack = XNEW (bitmap_obstack);
7328 bitmap_obstack_initialize (s_obstack);
7329 s_e1 = BITMAP_ALLOC (s_obstack);
7330 s_e2 = BITMAP_ALLOC (s_obstack);
7332 if (e1 == NULL)
7333 e1 = s_e1;
7334 if (e2 == NULL)
7335 e2 = s_e2;
7337 /* Add N1 and N2 to their own set of equivalences to avoid
7338 duplicating the body of the loop just to check N1 and N2
7339 ranges. */
7340 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7341 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7343 /* If the equivalence sets have a common intersection, then the two
7344 names can be compared without checking their ranges. */
7345 if (bitmap_intersect_p (e1, e2))
7347 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7348 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7350 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7351 ? boolean_true_node
7352 : boolean_false_node;
7355 /* Start at -1. Set it to 0 if we do a comparison without relying
7356 on overflow, or 1 if all comparisons rely on overflow. */
7357 used_strict_overflow = -1;
7359 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7360 N2 to their own set of equivalences to avoid duplicating the body
7361 of the loop just to check N1 and N2 ranges. */
7362 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7364 value_range_t vr1 = get_vr_for_comparison (i1);
7366 t = retval = NULL_TREE;
7367 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7369 bool sop = false;
7371 value_range_t vr2 = get_vr_for_comparison (i2);
7373 t = compare_ranges (comp, &vr1, &vr2, &sop);
7374 if (t)
7376 /* If we get different answers from different members
7377 of the equivalence set this check must be in a dead
7378 code region. Folding it to a trap representation
7379 would be correct here. For now just return don't-know. */
7380 if (retval != NULL
7381 && t != retval)
7383 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7384 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7385 return NULL_TREE;
7387 retval = t;
7389 if (!sop)
7390 used_strict_overflow = 0;
7391 else if (used_strict_overflow < 0)
7392 used_strict_overflow = 1;
7396 if (retval)
7398 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7399 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7400 if (used_strict_overflow > 0)
7401 *strict_overflow_p = true;
7402 return retval;
7406 /* None of the equivalent ranges are useful in computing this
7407 comparison. */
7408 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7409 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7410 return NULL_TREE;
7413 /* Helper function for vrp_evaluate_conditional_warnv. */
7415 static tree
7416 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7417 tree op0, tree op1,
7418 bool * strict_overflow_p)
7420 value_range_t *vr0, *vr1;
7422 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7423 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7425 tree res = NULL_TREE;
7426 if (vr0 && vr1)
7427 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7428 if (!res && vr0)
7429 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7430 if (!res && vr1)
7431 res = (compare_range_with_value
7432 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7433 return res;
7436 /* Helper function for vrp_evaluate_conditional_warnv. */
7438 static tree
7439 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7440 tree op1, bool use_equiv_p,
7441 bool *strict_overflow_p, bool *only_ranges)
7443 tree ret;
7444 if (only_ranges)
7445 *only_ranges = true;
7447 /* We only deal with integral and pointer types. */
7448 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7449 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7450 return NULL_TREE;
7452 if (use_equiv_p)
7454 if (only_ranges
7455 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7456 (code, op0, op1, strict_overflow_p)))
7457 return ret;
7458 *only_ranges = false;
7459 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7460 return compare_names (code, op0, op1, strict_overflow_p);
7461 else if (TREE_CODE (op0) == SSA_NAME)
7462 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7463 else if (TREE_CODE (op1) == SSA_NAME)
7464 return (compare_name_with_value
7465 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7467 else
7468 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7469 strict_overflow_p);
7470 return NULL_TREE;
7473 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7474 information. Return NULL if the conditional can not be evaluated.
7475 The ranges of all the names equivalent with the operands in COND
7476 will be used when trying to compute the value. If the result is
7477 based on undefined signed overflow, issue a warning if
7478 appropriate. */
7480 static tree
7481 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7483 bool sop;
7484 tree ret;
7485 bool only_ranges;
7487 /* Some passes and foldings leak constants with overflow flag set
7488 into the IL. Avoid doing wrong things with these and bail out. */
7489 if ((TREE_CODE (op0) == INTEGER_CST
7490 && TREE_OVERFLOW (op0))
7491 || (TREE_CODE (op1) == INTEGER_CST
7492 && TREE_OVERFLOW (op1)))
7493 return NULL_TREE;
7495 sop = false;
7496 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7497 &only_ranges);
7499 if (ret && sop)
7501 enum warn_strict_overflow_code wc;
7502 const char* warnmsg;
7504 if (is_gimple_min_invariant (ret))
7506 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7507 warnmsg = G_("assuming signed overflow does not occur when "
7508 "simplifying conditional to constant");
7510 else
7512 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7513 warnmsg = G_("assuming signed overflow does not occur when "
7514 "simplifying conditional");
7517 if (issue_strict_overflow_warning (wc))
7519 location_t location;
7521 if (!gimple_has_location (stmt))
7522 location = input_location;
7523 else
7524 location = gimple_location (stmt);
7525 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7529 if (warn_type_limits
7530 && ret && only_ranges
7531 && TREE_CODE_CLASS (code) == tcc_comparison
7532 && TREE_CODE (op0) == SSA_NAME)
7534 /* If the comparison is being folded and the operand on the LHS
7535 is being compared against a constant value that is outside of
7536 the natural range of OP0's type, then the predicate will
7537 always fold regardless of the value of OP0. If -Wtype-limits
7538 was specified, emit a warning. */
7539 tree type = TREE_TYPE (op0);
7540 value_range_t *vr0 = get_value_range (op0);
7542 if (vr0->type != VR_VARYING
7543 && INTEGRAL_TYPE_P (type)
7544 && vrp_val_is_min (vr0->min)
7545 && vrp_val_is_max (vr0->max)
7546 && is_gimple_min_invariant (op1))
7548 location_t location;
7550 if (!gimple_has_location (stmt))
7551 location = input_location;
7552 else
7553 location = gimple_location (stmt);
7555 warning_at (location, OPT_Wtype_limits,
7556 integer_zerop (ret)
7557 ? G_("comparison always false "
7558 "due to limited range of data type")
7559 : G_("comparison always true "
7560 "due to limited range of data type"));
7564 return ret;
7568 /* Visit conditional statement STMT. If we can determine which edge
7569 will be taken out of STMT's basic block, record it in
7570 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7571 SSA_PROP_VARYING. */
7573 static enum ssa_prop_result
7574 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7576 tree val;
7577 bool sop;
7579 *taken_edge_p = NULL;
7581 if (dump_file && (dump_flags & TDF_DETAILS))
7583 tree use;
7584 ssa_op_iter i;
7586 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7587 print_gimple_stmt (dump_file, stmt, 0, 0);
7588 fprintf (dump_file, "\nWith known ranges\n");
7590 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7592 fprintf (dump_file, "\t");
7593 print_generic_expr (dump_file, use, 0);
7594 fprintf (dump_file, ": ");
7595 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7598 fprintf (dump_file, "\n");
7601 /* Compute the value of the predicate COND by checking the known
7602 ranges of each of its operands.
7604 Note that we cannot evaluate all the equivalent ranges here
7605 because those ranges may not yet be final and with the current
7606 propagation strategy, we cannot determine when the value ranges
7607 of the names in the equivalence set have changed.
7609 For instance, given the following code fragment
7611 i_5 = PHI <8, i_13>
7613 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7614 if (i_14 == 1)
7617 Assume that on the first visit to i_14, i_5 has the temporary
7618 range [8, 8] because the second argument to the PHI function is
7619 not yet executable. We derive the range ~[0, 0] for i_14 and the
7620 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7621 the first time, since i_14 is equivalent to the range [8, 8], we
7622 determine that the predicate is always false.
7624 On the next round of propagation, i_13 is determined to be
7625 VARYING, which causes i_5 to drop down to VARYING. So, another
7626 visit to i_14 is scheduled. In this second visit, we compute the
7627 exact same range and equivalence set for i_14, namely ~[0, 0] and
7628 { i_5 }. But we did not have the previous range for i_5
7629 registered, so vrp_visit_assignment thinks that the range for
7630 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7631 is not visited again, which stops propagation from visiting
7632 statements in the THEN clause of that if().
7634 To properly fix this we would need to keep the previous range
7635 value for the names in the equivalence set. This way we would've
7636 discovered that from one visit to the other i_5 changed from
7637 range [8, 8] to VR_VARYING.
7639 However, fixing this apparent limitation may not be worth the
7640 additional checking. Testing on several code bases (GCC, DLV,
7641 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7642 4 more predicates folded in SPEC. */
7643 sop = false;
7645 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7646 gimple_cond_lhs (stmt),
7647 gimple_cond_rhs (stmt),
7648 false, &sop, NULL);
7649 if (val)
7651 if (!sop)
7652 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7653 else
7655 if (dump_file && (dump_flags & TDF_DETAILS))
7656 fprintf (dump_file,
7657 "\nIgnoring predicate evaluation because "
7658 "it assumes that signed overflow is undefined");
7659 val = NULL_TREE;
7663 if (dump_file && (dump_flags & TDF_DETAILS))
7665 fprintf (dump_file, "\nPredicate evaluates to: ");
7666 if (val == NULL_TREE)
7667 fprintf (dump_file, "DON'T KNOW\n");
7668 else
7669 print_generic_stmt (dump_file, val, 0);
7672 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7675 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7676 that includes the value VAL. The search is restricted to the range
7677 [START_IDX, n - 1] where n is the size of VEC.
7679 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7680 returned.
7682 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7683 it is placed in IDX and false is returned.
7685 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7686 returned. */
7688 static bool
7689 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7691 size_t n = gimple_switch_num_labels (stmt);
7692 size_t low, high;
7694 /* Find case label for minimum of the value range or the next one.
7695 At each iteration we are searching in [low, high - 1]. */
7697 for (low = start_idx, high = n; high != low; )
7699 tree t;
7700 int cmp;
7701 /* Note that i != high, so we never ask for n. */
7702 size_t i = (high + low) / 2;
7703 t = gimple_switch_label (stmt, i);
7705 /* Cache the result of comparing CASE_LOW and val. */
7706 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7708 if (cmp == 0)
7710 /* Ranges cannot be empty. */
7711 *idx = i;
7712 return true;
7714 else if (cmp > 0)
7715 high = i;
7716 else
7718 low = i + 1;
7719 if (CASE_HIGH (t) != NULL
7720 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7722 *idx = i;
7723 return true;
7728 *idx = high;
7729 return false;
7732 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7733 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7734 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7735 then MAX_IDX < MIN_IDX.
7736 Returns true if the default label is not needed. */
7738 static bool
7739 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7740 size_t *max_idx)
7742 size_t i, j;
7743 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7744 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7746 if (i == j
7747 && min_take_default
7748 && max_take_default)
7750 /* Only the default case label reached.
7751 Return an empty range. */
7752 *min_idx = 1;
7753 *max_idx = 0;
7754 return false;
7756 else
7758 bool take_default = min_take_default || max_take_default;
7759 tree low, high;
7760 size_t k;
7762 if (max_take_default)
7763 j--;
7765 /* If the case label range is continuous, we do not need
7766 the default case label. Verify that. */
7767 high = CASE_LOW (gimple_switch_label (stmt, i));
7768 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7769 high = CASE_HIGH (gimple_switch_label (stmt, i));
7770 for (k = i + 1; k <= j; ++k)
7772 low = CASE_LOW (gimple_switch_label (stmt, k));
7773 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7775 take_default = true;
7776 break;
7778 high = low;
7779 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7780 high = CASE_HIGH (gimple_switch_label (stmt, k));
7783 *min_idx = i;
7784 *max_idx = j;
7785 return !take_default;
7789 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7790 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7791 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7792 Returns true if the default label is not needed. */
7794 static bool
7795 find_case_label_ranges (gswitch *stmt, value_range_t *vr, size_t *min_idx1,
7796 size_t *max_idx1, size_t *min_idx2,
7797 size_t *max_idx2)
7799 size_t i, j, k, l;
7800 unsigned int n = gimple_switch_num_labels (stmt);
7801 bool take_default;
7802 tree case_low, case_high;
7803 tree min = vr->min, max = vr->max;
7805 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7807 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7809 /* Set second range to emtpy. */
7810 *min_idx2 = 1;
7811 *max_idx2 = 0;
7813 if (vr->type == VR_RANGE)
7815 *min_idx1 = i;
7816 *max_idx1 = j;
7817 return !take_default;
7820 /* Set first range to all case labels. */
7821 *min_idx1 = 1;
7822 *max_idx1 = n - 1;
7824 if (i > j)
7825 return false;
7827 /* Make sure all the values of case labels [i , j] are contained in
7828 range [MIN, MAX]. */
7829 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7830 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7831 if (tree_int_cst_compare (case_low, min) < 0)
7832 i += 1;
7833 if (case_high != NULL_TREE
7834 && tree_int_cst_compare (max, case_high) < 0)
7835 j -= 1;
7837 if (i > j)
7838 return false;
7840 /* If the range spans case labels [i, j], the corresponding anti-range spans
7841 the labels [1, i - 1] and [j + 1, n - 1]. */
7842 k = j + 1;
7843 l = n - 1;
7844 if (k > l)
7846 k = 1;
7847 l = 0;
7850 j = i - 1;
7851 i = 1;
7852 if (i > j)
7854 i = k;
7855 j = l;
7856 k = 1;
7857 l = 0;
7860 *min_idx1 = i;
7861 *max_idx1 = j;
7862 *min_idx2 = k;
7863 *max_idx2 = l;
7864 return false;
7867 /* Visit switch statement STMT. If we can determine which edge
7868 will be taken out of STMT's basic block, record it in
7869 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7870 SSA_PROP_VARYING. */
7872 static enum ssa_prop_result
7873 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7875 tree op, val;
7876 value_range_t *vr;
7877 size_t i = 0, j = 0, k, l;
7878 bool take_default;
7880 *taken_edge_p = NULL;
7881 op = gimple_switch_index (stmt);
7882 if (TREE_CODE (op) != SSA_NAME)
7883 return SSA_PROP_VARYING;
7885 vr = get_value_range (op);
7886 if (dump_file && (dump_flags & TDF_DETAILS))
7888 fprintf (dump_file, "\nVisiting switch expression with operand ");
7889 print_generic_expr (dump_file, op, 0);
7890 fprintf (dump_file, " with known range ");
7891 dump_value_range (dump_file, vr);
7892 fprintf (dump_file, "\n");
7895 if ((vr->type != VR_RANGE
7896 && vr->type != VR_ANTI_RANGE)
7897 || symbolic_range_p (vr))
7898 return SSA_PROP_VARYING;
7900 /* Find the single edge that is taken from the switch expression. */
7901 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7903 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7904 label */
7905 if (j < i)
7907 gcc_assert (take_default);
7908 val = gimple_switch_default_label (stmt);
7910 else
7912 /* Check if labels with index i to j and maybe the default label
7913 are all reaching the same label. */
7915 val = gimple_switch_label (stmt, i);
7916 if (take_default
7917 && CASE_LABEL (gimple_switch_default_label (stmt))
7918 != CASE_LABEL (val))
7920 if (dump_file && (dump_flags & TDF_DETAILS))
7921 fprintf (dump_file, " not a single destination for this "
7922 "range\n");
7923 return SSA_PROP_VARYING;
7925 for (++i; i <= j; ++i)
7927 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7929 if (dump_file && (dump_flags & TDF_DETAILS))
7930 fprintf (dump_file, " not a single destination for this "
7931 "range\n");
7932 return SSA_PROP_VARYING;
7935 for (; k <= l; ++k)
7937 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7939 if (dump_file && (dump_flags & TDF_DETAILS))
7940 fprintf (dump_file, " not a single destination for this "
7941 "range\n");
7942 return SSA_PROP_VARYING;
7947 *taken_edge_p = find_edge (gimple_bb (stmt),
7948 label_to_block (CASE_LABEL (val)));
7950 if (dump_file && (dump_flags & TDF_DETAILS))
7952 fprintf (dump_file, " will take edge to ");
7953 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7956 return SSA_PROP_INTERESTING;
7960 /* Evaluate statement STMT. If the statement produces a useful range,
7961 return SSA_PROP_INTERESTING and record the SSA name with the
7962 interesting range into *OUTPUT_P.
7964 If STMT is a conditional branch and we can determine its truth
7965 value, the taken edge is recorded in *TAKEN_EDGE_P.
7967 If STMT produces a varying value, return SSA_PROP_VARYING. */
7969 static enum ssa_prop_result
7970 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7972 tree def;
7973 ssa_op_iter iter;
7975 if (dump_file && (dump_flags & TDF_DETAILS))
7977 fprintf (dump_file, "\nVisiting statement:\n");
7978 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7981 if (!stmt_interesting_for_vrp (stmt))
7982 gcc_assert (stmt_ends_bb_p (stmt));
7983 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7984 return vrp_visit_assignment_or_call (stmt, output_p);
7985 else if (gimple_code (stmt) == GIMPLE_COND)
7986 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7987 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7988 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7990 /* All other statements produce nothing of interest for VRP, so mark
7991 their outputs varying and prevent further simulation. */
7992 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7993 set_value_range_to_varying (get_value_range (def));
7995 return SSA_PROP_VARYING;
7998 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7999 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8000 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8001 possible such range. The resulting range is not canonicalized. */
8003 static void
8004 union_ranges (enum value_range_type *vr0type,
8005 tree *vr0min, tree *vr0max,
8006 enum value_range_type vr1type,
8007 tree vr1min, tree vr1max)
8009 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8010 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8012 /* [] is vr0, () is vr1 in the following classification comments. */
8013 if (mineq && maxeq)
8015 /* [( )] */
8016 if (*vr0type == vr1type)
8017 /* Nothing to do for equal ranges. */
8019 else if ((*vr0type == VR_RANGE
8020 && vr1type == VR_ANTI_RANGE)
8021 || (*vr0type == VR_ANTI_RANGE
8022 && vr1type == VR_RANGE))
8024 /* For anti-range with range union the result is varying. */
8025 goto give_up;
8027 else
8028 gcc_unreachable ();
8030 else if (operand_less_p (*vr0max, vr1min) == 1
8031 || operand_less_p (vr1max, *vr0min) == 1)
8033 /* [ ] ( ) or ( ) [ ]
8034 If the ranges have an empty intersection, result of the union
8035 operation is the anti-range or if both are anti-ranges
8036 it covers all. */
8037 if (*vr0type == VR_ANTI_RANGE
8038 && vr1type == VR_ANTI_RANGE)
8039 goto give_up;
8040 else if (*vr0type == VR_ANTI_RANGE
8041 && vr1type == VR_RANGE)
8043 else if (*vr0type == VR_RANGE
8044 && vr1type == VR_ANTI_RANGE)
8046 *vr0type = vr1type;
8047 *vr0min = vr1min;
8048 *vr0max = vr1max;
8050 else if (*vr0type == VR_RANGE
8051 && vr1type == VR_RANGE)
8053 /* The result is the convex hull of both ranges. */
8054 if (operand_less_p (*vr0max, vr1min) == 1)
8056 /* If the result can be an anti-range, create one. */
8057 if (TREE_CODE (*vr0max) == INTEGER_CST
8058 && TREE_CODE (vr1min) == INTEGER_CST
8059 && vrp_val_is_min (*vr0min)
8060 && vrp_val_is_max (vr1max))
8062 tree min = int_const_binop (PLUS_EXPR,
8063 *vr0max,
8064 build_int_cst (TREE_TYPE (*vr0max), 1));
8065 tree max = int_const_binop (MINUS_EXPR,
8066 vr1min,
8067 build_int_cst (TREE_TYPE (vr1min), 1));
8068 if (!operand_less_p (max, min))
8070 *vr0type = VR_ANTI_RANGE;
8071 *vr0min = min;
8072 *vr0max = max;
8074 else
8075 *vr0max = vr1max;
8077 else
8078 *vr0max = vr1max;
8080 else
8082 /* If the result can be an anti-range, create one. */
8083 if (TREE_CODE (vr1max) == INTEGER_CST
8084 && TREE_CODE (*vr0min) == INTEGER_CST
8085 && vrp_val_is_min (vr1min)
8086 && vrp_val_is_max (*vr0max))
8088 tree min = int_const_binop (PLUS_EXPR,
8089 vr1max,
8090 build_int_cst (TREE_TYPE (vr1max), 1));
8091 tree max = int_const_binop (MINUS_EXPR,
8092 *vr0min,
8093 build_int_cst (TREE_TYPE (*vr0min), 1));
8094 if (!operand_less_p (max, min))
8096 *vr0type = VR_ANTI_RANGE;
8097 *vr0min = min;
8098 *vr0max = max;
8100 else
8101 *vr0min = vr1min;
8103 else
8104 *vr0min = vr1min;
8107 else
8108 gcc_unreachable ();
8110 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8111 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8113 /* [ ( ) ] or [( ) ] or [ ( )] */
8114 if (*vr0type == VR_RANGE
8115 && vr1type == VR_RANGE)
8117 else if (*vr0type == VR_ANTI_RANGE
8118 && vr1type == VR_ANTI_RANGE)
8120 *vr0type = vr1type;
8121 *vr0min = vr1min;
8122 *vr0max = vr1max;
8124 else if (*vr0type == VR_ANTI_RANGE
8125 && vr1type == VR_RANGE)
8127 /* Arbitrarily choose the right or left gap. */
8128 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8129 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8130 build_int_cst (TREE_TYPE (vr1min), 1));
8131 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8132 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8133 build_int_cst (TREE_TYPE (vr1max), 1));
8134 else
8135 goto give_up;
8137 else if (*vr0type == VR_RANGE
8138 && vr1type == VR_ANTI_RANGE)
8139 /* The result covers everything. */
8140 goto give_up;
8141 else
8142 gcc_unreachable ();
8144 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8145 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8147 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8148 if (*vr0type == VR_RANGE
8149 && vr1type == VR_RANGE)
8151 *vr0type = vr1type;
8152 *vr0min = vr1min;
8153 *vr0max = vr1max;
8155 else if (*vr0type == VR_ANTI_RANGE
8156 && vr1type == VR_ANTI_RANGE)
8158 else if (*vr0type == VR_RANGE
8159 && vr1type == VR_ANTI_RANGE)
8161 *vr0type = VR_ANTI_RANGE;
8162 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8164 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8165 build_int_cst (TREE_TYPE (*vr0min), 1));
8166 *vr0min = vr1min;
8168 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8170 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8171 build_int_cst (TREE_TYPE (*vr0max), 1));
8172 *vr0max = vr1max;
8174 else
8175 goto give_up;
8177 else if (*vr0type == VR_ANTI_RANGE
8178 && vr1type == VR_RANGE)
8179 /* The result covers everything. */
8180 goto give_up;
8181 else
8182 gcc_unreachable ();
8184 else if ((operand_less_p (vr1min, *vr0max) == 1
8185 || operand_equal_p (vr1min, *vr0max, 0))
8186 && operand_less_p (*vr0min, vr1min) == 1
8187 && operand_less_p (*vr0max, vr1max) == 1)
8189 /* [ ( ] ) or [ ]( ) */
8190 if (*vr0type == VR_RANGE
8191 && vr1type == VR_RANGE)
8192 *vr0max = vr1max;
8193 else if (*vr0type == VR_ANTI_RANGE
8194 && vr1type == VR_ANTI_RANGE)
8195 *vr0min = vr1min;
8196 else if (*vr0type == VR_ANTI_RANGE
8197 && vr1type == VR_RANGE)
8199 if (TREE_CODE (vr1min) == INTEGER_CST)
8200 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8201 build_int_cst (TREE_TYPE (vr1min), 1));
8202 else
8203 goto give_up;
8205 else if (*vr0type == VR_RANGE
8206 && vr1type == VR_ANTI_RANGE)
8208 if (TREE_CODE (*vr0max) == INTEGER_CST)
8210 *vr0type = vr1type;
8211 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8212 build_int_cst (TREE_TYPE (*vr0max), 1));
8213 *vr0max = vr1max;
8215 else
8216 goto give_up;
8218 else
8219 gcc_unreachable ();
8221 else if ((operand_less_p (*vr0min, vr1max) == 1
8222 || operand_equal_p (*vr0min, vr1max, 0))
8223 && operand_less_p (vr1min, *vr0min) == 1
8224 && operand_less_p (vr1max, *vr0max) == 1)
8226 /* ( [ ) ] or ( )[ ] */
8227 if (*vr0type == VR_RANGE
8228 && vr1type == VR_RANGE)
8229 *vr0min = vr1min;
8230 else if (*vr0type == VR_ANTI_RANGE
8231 && vr1type == VR_ANTI_RANGE)
8232 *vr0max = vr1max;
8233 else if (*vr0type == VR_ANTI_RANGE
8234 && vr1type == VR_RANGE)
8236 if (TREE_CODE (vr1max) == INTEGER_CST)
8237 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8238 build_int_cst (TREE_TYPE (vr1max), 1));
8239 else
8240 goto give_up;
8242 else if (*vr0type == VR_RANGE
8243 && vr1type == VR_ANTI_RANGE)
8245 if (TREE_CODE (*vr0min) == INTEGER_CST)
8247 *vr0type = vr1type;
8248 *vr0min = vr1min;
8249 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8250 build_int_cst (TREE_TYPE (*vr0min), 1));
8252 else
8253 goto give_up;
8255 else
8256 gcc_unreachable ();
8258 else
8259 goto give_up;
8261 return;
8263 give_up:
8264 *vr0type = VR_VARYING;
8265 *vr0min = NULL_TREE;
8266 *vr0max = NULL_TREE;
8269 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8270 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8271 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8272 possible such range. The resulting range is not canonicalized. */
8274 static void
8275 intersect_ranges (enum value_range_type *vr0type,
8276 tree *vr0min, tree *vr0max,
8277 enum value_range_type vr1type,
8278 tree vr1min, tree vr1max)
8280 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8281 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8283 /* [] is vr0, () is vr1 in the following classification comments. */
8284 if (mineq && maxeq)
8286 /* [( )] */
8287 if (*vr0type == vr1type)
8288 /* Nothing to do for equal ranges. */
8290 else if ((*vr0type == VR_RANGE
8291 && vr1type == VR_ANTI_RANGE)
8292 || (*vr0type == VR_ANTI_RANGE
8293 && vr1type == VR_RANGE))
8295 /* For anti-range with range intersection the result is empty. */
8296 *vr0type = VR_UNDEFINED;
8297 *vr0min = NULL_TREE;
8298 *vr0max = NULL_TREE;
8300 else
8301 gcc_unreachable ();
8303 else if (operand_less_p (*vr0max, vr1min) == 1
8304 || operand_less_p (vr1max, *vr0min) == 1)
8306 /* [ ] ( ) or ( ) [ ]
8307 If the ranges have an empty intersection, the result of the
8308 intersect operation is the range for intersecting an
8309 anti-range with a range or empty when intersecting two ranges. */
8310 if (*vr0type == VR_RANGE
8311 && vr1type == VR_ANTI_RANGE)
8313 else if (*vr0type == VR_ANTI_RANGE
8314 && vr1type == VR_RANGE)
8316 *vr0type = vr1type;
8317 *vr0min = vr1min;
8318 *vr0max = vr1max;
8320 else if (*vr0type == VR_RANGE
8321 && vr1type == VR_RANGE)
8323 *vr0type = VR_UNDEFINED;
8324 *vr0min = NULL_TREE;
8325 *vr0max = NULL_TREE;
8327 else if (*vr0type == VR_ANTI_RANGE
8328 && vr1type == VR_ANTI_RANGE)
8330 /* If the anti-ranges are adjacent to each other merge them. */
8331 if (TREE_CODE (*vr0max) == INTEGER_CST
8332 && TREE_CODE (vr1min) == INTEGER_CST
8333 && operand_less_p (*vr0max, vr1min) == 1
8334 && integer_onep (int_const_binop (MINUS_EXPR,
8335 vr1min, *vr0max)))
8336 *vr0max = vr1max;
8337 else if (TREE_CODE (vr1max) == INTEGER_CST
8338 && TREE_CODE (*vr0min) == INTEGER_CST
8339 && operand_less_p (vr1max, *vr0min) == 1
8340 && integer_onep (int_const_binop (MINUS_EXPR,
8341 *vr0min, vr1max)))
8342 *vr0min = vr1min;
8343 /* Else arbitrarily take VR0. */
8346 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8347 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8349 /* [ ( ) ] or [( ) ] or [ ( )] */
8350 if (*vr0type == VR_RANGE
8351 && vr1type == VR_RANGE)
8353 /* If both are ranges the result is the inner one. */
8354 *vr0type = vr1type;
8355 *vr0min = vr1min;
8356 *vr0max = vr1max;
8358 else if (*vr0type == VR_RANGE
8359 && vr1type == VR_ANTI_RANGE)
8361 /* Choose the right gap if the left one is empty. */
8362 if (mineq)
8364 if (TREE_CODE (vr1max) == INTEGER_CST)
8365 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8366 build_int_cst (TREE_TYPE (vr1max), 1));
8367 else
8368 *vr0min = vr1max;
8370 /* Choose the left gap if the right one is empty. */
8371 else if (maxeq)
8373 if (TREE_CODE (vr1min) == INTEGER_CST)
8374 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8375 build_int_cst (TREE_TYPE (vr1min), 1));
8376 else
8377 *vr0max = vr1min;
8379 /* Choose the anti-range if the range is effectively varying. */
8380 else if (vrp_val_is_min (*vr0min)
8381 && vrp_val_is_max (*vr0max))
8383 *vr0type = vr1type;
8384 *vr0min = vr1min;
8385 *vr0max = vr1max;
8387 /* Else choose the range. */
8389 else if (*vr0type == VR_ANTI_RANGE
8390 && vr1type == VR_ANTI_RANGE)
8391 /* If both are anti-ranges the result is the outer one. */
8393 else if (*vr0type == VR_ANTI_RANGE
8394 && vr1type == VR_RANGE)
8396 /* The intersection is empty. */
8397 *vr0type = VR_UNDEFINED;
8398 *vr0min = NULL_TREE;
8399 *vr0max = NULL_TREE;
8401 else
8402 gcc_unreachable ();
8404 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8405 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8407 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8408 if (*vr0type == VR_RANGE
8409 && vr1type == VR_RANGE)
8410 /* Choose the inner range. */
8412 else if (*vr0type == VR_ANTI_RANGE
8413 && vr1type == VR_RANGE)
8415 /* Choose the right gap if the left is empty. */
8416 if (mineq)
8418 *vr0type = VR_RANGE;
8419 if (TREE_CODE (*vr0max) == INTEGER_CST)
8420 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8421 build_int_cst (TREE_TYPE (*vr0max), 1));
8422 else
8423 *vr0min = *vr0max;
8424 *vr0max = vr1max;
8426 /* Choose the left gap if the right is empty. */
8427 else if (maxeq)
8429 *vr0type = VR_RANGE;
8430 if (TREE_CODE (*vr0min) == INTEGER_CST)
8431 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8432 build_int_cst (TREE_TYPE (*vr0min), 1));
8433 else
8434 *vr0max = *vr0min;
8435 *vr0min = vr1min;
8437 /* Choose the anti-range if the range is effectively varying. */
8438 else if (vrp_val_is_min (vr1min)
8439 && vrp_val_is_max (vr1max))
8441 /* Else choose the range. */
8442 else
8444 *vr0type = vr1type;
8445 *vr0min = vr1min;
8446 *vr0max = vr1max;
8449 else if (*vr0type == VR_ANTI_RANGE
8450 && vr1type == VR_ANTI_RANGE)
8452 /* If both are anti-ranges the result is the outer one. */
8453 *vr0type = vr1type;
8454 *vr0min = vr1min;
8455 *vr0max = vr1max;
8457 else if (vr1type == VR_ANTI_RANGE
8458 && *vr0type == VR_RANGE)
8460 /* The intersection is empty. */
8461 *vr0type = VR_UNDEFINED;
8462 *vr0min = NULL_TREE;
8463 *vr0max = NULL_TREE;
8465 else
8466 gcc_unreachable ();
8468 else if ((operand_less_p (vr1min, *vr0max) == 1
8469 || operand_equal_p (vr1min, *vr0max, 0))
8470 && operand_less_p (*vr0min, vr1min) == 1)
8472 /* [ ( ] ) or [ ]( ) */
8473 if (*vr0type == VR_ANTI_RANGE
8474 && vr1type == VR_ANTI_RANGE)
8475 *vr0max = vr1max;
8476 else if (*vr0type == VR_RANGE
8477 && vr1type == VR_RANGE)
8478 *vr0min = vr1min;
8479 else if (*vr0type == VR_RANGE
8480 && vr1type == VR_ANTI_RANGE)
8482 if (TREE_CODE (vr1min) == INTEGER_CST)
8483 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8484 build_int_cst (TREE_TYPE (vr1min), 1));
8485 else
8486 *vr0max = vr1min;
8488 else if (*vr0type == VR_ANTI_RANGE
8489 && vr1type == VR_RANGE)
8491 *vr0type = VR_RANGE;
8492 if (TREE_CODE (*vr0max) == INTEGER_CST)
8493 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8494 build_int_cst (TREE_TYPE (*vr0max), 1));
8495 else
8496 *vr0min = *vr0max;
8497 *vr0max = vr1max;
8499 else
8500 gcc_unreachable ();
8502 else if ((operand_less_p (*vr0min, vr1max) == 1
8503 || operand_equal_p (*vr0min, vr1max, 0))
8504 && operand_less_p (vr1min, *vr0min) == 1)
8506 /* ( [ ) ] or ( )[ ] */
8507 if (*vr0type == VR_ANTI_RANGE
8508 && vr1type == VR_ANTI_RANGE)
8509 *vr0min = vr1min;
8510 else if (*vr0type == VR_RANGE
8511 && vr1type == VR_RANGE)
8512 *vr0max = vr1max;
8513 else if (*vr0type == VR_RANGE
8514 && vr1type == VR_ANTI_RANGE)
8516 if (TREE_CODE (vr1max) == INTEGER_CST)
8517 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8518 build_int_cst (TREE_TYPE (vr1max), 1));
8519 else
8520 *vr0min = vr1max;
8522 else if (*vr0type == VR_ANTI_RANGE
8523 && vr1type == VR_RANGE)
8525 *vr0type = VR_RANGE;
8526 if (TREE_CODE (*vr0min) == INTEGER_CST)
8527 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8528 build_int_cst (TREE_TYPE (*vr0min), 1));
8529 else
8530 *vr0max = *vr0min;
8531 *vr0min = vr1min;
8533 else
8534 gcc_unreachable ();
8537 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8538 result for the intersection. That's always a conservative
8539 correct estimate. */
8541 return;
8545 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8546 in *VR0. This may not be the smallest possible such range. */
8548 static void
8549 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8551 value_range_t saved;
8553 /* If either range is VR_VARYING the other one wins. */
8554 if (vr1->type == VR_VARYING)
8555 return;
8556 if (vr0->type == VR_VARYING)
8558 copy_value_range (vr0, vr1);
8559 return;
8562 /* When either range is VR_UNDEFINED the resulting range is
8563 VR_UNDEFINED, too. */
8564 if (vr0->type == VR_UNDEFINED)
8565 return;
8566 if (vr1->type == VR_UNDEFINED)
8568 set_value_range_to_undefined (vr0);
8569 return;
8572 /* Save the original vr0 so we can return it as conservative intersection
8573 result when our worker turns things to varying. */
8574 saved = *vr0;
8575 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8576 vr1->type, vr1->min, vr1->max);
8577 /* Make sure to canonicalize the result though as the inversion of a
8578 VR_RANGE can still be a VR_RANGE. */
8579 set_and_canonicalize_value_range (vr0, vr0->type,
8580 vr0->min, vr0->max, vr0->equiv);
8581 /* If that failed, use the saved original VR0. */
8582 if (vr0->type == VR_VARYING)
8584 *vr0 = saved;
8585 return;
8587 /* If the result is VR_UNDEFINED there is no need to mess with
8588 the equivalencies. */
8589 if (vr0->type == VR_UNDEFINED)
8590 return;
8592 /* The resulting set of equivalences for range intersection is the union of
8593 the two sets. */
8594 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8595 bitmap_ior_into (vr0->equiv, vr1->equiv);
8596 else if (vr1->equiv && !vr0->equiv)
8597 bitmap_copy (vr0->equiv, vr1->equiv);
8600 static void
8601 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8603 if (dump_file && (dump_flags & TDF_DETAILS))
8605 fprintf (dump_file, "Intersecting\n ");
8606 dump_value_range (dump_file, vr0);
8607 fprintf (dump_file, "\nand\n ");
8608 dump_value_range (dump_file, vr1);
8609 fprintf (dump_file, "\n");
8611 vrp_intersect_ranges_1 (vr0, vr1);
8612 if (dump_file && (dump_flags & TDF_DETAILS))
8614 fprintf (dump_file, "to\n ");
8615 dump_value_range (dump_file, vr0);
8616 fprintf (dump_file, "\n");
8620 /* Meet operation for value ranges. Given two value ranges VR0 and
8621 VR1, store in VR0 a range that contains both VR0 and VR1. This
8622 may not be the smallest possible such range. */
8624 static void
8625 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8627 value_range_t saved;
8629 if (vr0->type == VR_UNDEFINED)
8631 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8632 return;
8635 if (vr1->type == VR_UNDEFINED)
8637 /* VR0 already has the resulting range. */
8638 return;
8641 if (vr0->type == VR_VARYING)
8643 /* Nothing to do. VR0 already has the resulting range. */
8644 return;
8647 if (vr1->type == VR_VARYING)
8649 set_value_range_to_varying (vr0);
8650 return;
8653 saved = *vr0;
8654 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8655 vr1->type, vr1->min, vr1->max);
8656 if (vr0->type == VR_VARYING)
8658 /* Failed to find an efficient meet. Before giving up and setting
8659 the result to VARYING, see if we can at least derive a useful
8660 anti-range. FIXME, all this nonsense about distinguishing
8661 anti-ranges from ranges is necessary because of the odd
8662 semantics of range_includes_zero_p and friends. */
8663 if (((saved.type == VR_RANGE
8664 && range_includes_zero_p (saved.min, saved.max) == 0)
8665 || (saved.type == VR_ANTI_RANGE
8666 && range_includes_zero_p (saved.min, saved.max) == 1))
8667 && ((vr1->type == VR_RANGE
8668 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8669 || (vr1->type == VR_ANTI_RANGE
8670 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8672 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8674 /* Since this meet operation did not result from the meeting of
8675 two equivalent names, VR0 cannot have any equivalences. */
8676 if (vr0->equiv)
8677 bitmap_clear (vr0->equiv);
8678 return;
8681 set_value_range_to_varying (vr0);
8682 return;
8684 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8685 vr0->equiv);
8686 if (vr0->type == VR_VARYING)
8687 return;
8689 /* The resulting set of equivalences is always the intersection of
8690 the two sets. */
8691 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8692 bitmap_and_into (vr0->equiv, vr1->equiv);
8693 else if (vr0->equiv && !vr1->equiv)
8694 bitmap_clear (vr0->equiv);
8697 static void
8698 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8700 if (dump_file && (dump_flags & TDF_DETAILS))
8702 fprintf (dump_file, "Meeting\n ");
8703 dump_value_range (dump_file, vr0);
8704 fprintf (dump_file, "\nand\n ");
8705 dump_value_range (dump_file, vr1);
8706 fprintf (dump_file, "\n");
8708 vrp_meet_1 (vr0, vr1);
8709 if (dump_file && (dump_flags & TDF_DETAILS))
8711 fprintf (dump_file, "to\n ");
8712 dump_value_range (dump_file, vr0);
8713 fprintf (dump_file, "\n");
8718 /* Visit all arguments for PHI node PHI that flow through executable
8719 edges. If a valid value range can be derived from all the incoming
8720 value ranges, set a new range for the LHS of PHI. */
8722 static enum ssa_prop_result
8723 vrp_visit_phi_node (gphi *phi)
8725 size_t i;
8726 tree lhs = PHI_RESULT (phi);
8727 value_range_t *lhs_vr = get_value_range (lhs);
8728 value_range_t vr_result = VR_INITIALIZER;
8729 bool first = true;
8730 int edges, old_edges;
8731 struct loop *l;
8733 if (dump_file && (dump_flags & TDF_DETAILS))
8735 fprintf (dump_file, "\nVisiting PHI node: ");
8736 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8739 edges = 0;
8740 for (i = 0; i < gimple_phi_num_args (phi); i++)
8742 edge e = gimple_phi_arg_edge (phi, i);
8744 if (dump_file && (dump_flags & TDF_DETAILS))
8746 fprintf (dump_file,
8747 " Argument #%d (%d -> %d %sexecutable)\n",
8748 (int) i, e->src->index, e->dest->index,
8749 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8752 if (e->flags & EDGE_EXECUTABLE)
8754 tree arg = PHI_ARG_DEF (phi, i);
8755 value_range_t vr_arg;
8757 ++edges;
8759 if (TREE_CODE (arg) == SSA_NAME)
8761 vr_arg = *(get_value_range (arg));
8762 /* Do not allow equivalences or symbolic ranges to leak in from
8763 backedges. That creates invalid equivalencies.
8764 See PR53465 and PR54767. */
8765 if (e->flags & EDGE_DFS_BACK)
8767 if (vr_arg.type == VR_RANGE
8768 || vr_arg.type == VR_ANTI_RANGE)
8770 vr_arg.equiv = NULL;
8771 if (symbolic_range_p (&vr_arg))
8773 vr_arg.type = VR_VARYING;
8774 vr_arg.min = NULL_TREE;
8775 vr_arg.max = NULL_TREE;
8779 else
8781 /* If the non-backedge arguments range is VR_VARYING then
8782 we can still try recording a simple equivalence. */
8783 if (vr_arg.type == VR_VARYING)
8785 vr_arg.type = VR_RANGE;
8786 vr_arg.min = arg;
8787 vr_arg.max = arg;
8788 vr_arg.equiv = NULL;
8792 else
8794 if (TREE_OVERFLOW_P (arg))
8795 arg = drop_tree_overflow (arg);
8797 vr_arg.type = VR_RANGE;
8798 vr_arg.min = arg;
8799 vr_arg.max = arg;
8800 vr_arg.equiv = NULL;
8803 if (dump_file && (dump_flags & TDF_DETAILS))
8805 fprintf (dump_file, "\t");
8806 print_generic_expr (dump_file, arg, dump_flags);
8807 fprintf (dump_file, ": ");
8808 dump_value_range (dump_file, &vr_arg);
8809 fprintf (dump_file, "\n");
8812 if (first)
8813 copy_value_range (&vr_result, &vr_arg);
8814 else
8815 vrp_meet (&vr_result, &vr_arg);
8816 first = false;
8818 if (vr_result.type == VR_VARYING)
8819 break;
8823 if (vr_result.type == VR_VARYING)
8824 goto varying;
8825 else if (vr_result.type == VR_UNDEFINED)
8826 goto update_range;
8828 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8829 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8831 /* To prevent infinite iterations in the algorithm, derive ranges
8832 when the new value is slightly bigger or smaller than the
8833 previous one. We don't do this if we have seen a new executable
8834 edge; this helps us avoid an overflow infinity for conditionals
8835 which are not in a loop. If the old value-range was VR_UNDEFINED
8836 use the updated range and iterate one more time. */
8837 if (edges > 0
8838 && gimple_phi_num_args (phi) > 1
8839 && edges == old_edges
8840 && lhs_vr->type != VR_UNDEFINED)
8842 /* Compare old and new ranges, fall back to varying if the
8843 values are not comparable. */
8844 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8845 if (cmp_min == -2)
8846 goto varying;
8847 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8848 if (cmp_max == -2)
8849 goto varying;
8851 /* For non VR_RANGE or for pointers fall back to varying if
8852 the range changed. */
8853 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8854 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8855 && (cmp_min != 0 || cmp_max != 0))
8856 goto varying;
8858 /* If the new minimum is larger than than the previous one
8859 retain the old value. If the new minimum value is smaller
8860 than the previous one and not -INF go all the way to -INF + 1.
8861 In the first case, to avoid infinite bouncing between different
8862 minimums, and in the other case to avoid iterating millions of
8863 times to reach -INF. Going to -INF + 1 also lets the following
8864 iteration compute whether there will be any overflow, at the
8865 expense of one additional iteration. */
8866 if (cmp_min < 0)
8867 vr_result.min = lhs_vr->min;
8868 else if (cmp_min > 0
8869 && !vrp_val_is_min (vr_result.min))
8870 vr_result.min
8871 = int_const_binop (PLUS_EXPR,
8872 vrp_val_min (TREE_TYPE (vr_result.min)),
8873 build_int_cst (TREE_TYPE (vr_result.min), 1));
8875 /* Similarly for the maximum value. */
8876 if (cmp_max > 0)
8877 vr_result.max = lhs_vr->max;
8878 else if (cmp_max < 0
8879 && !vrp_val_is_max (vr_result.max))
8880 vr_result.max
8881 = int_const_binop (MINUS_EXPR,
8882 vrp_val_max (TREE_TYPE (vr_result.min)),
8883 build_int_cst (TREE_TYPE (vr_result.min), 1));
8885 /* If we dropped either bound to +-INF then if this is a loop
8886 PHI node SCEV may known more about its value-range. */
8887 if ((cmp_min > 0 || cmp_min < 0
8888 || cmp_max < 0 || cmp_max > 0)
8889 && (l = loop_containing_stmt (phi))
8890 && l->header == gimple_bb (phi))
8891 adjust_range_with_scev (&vr_result, l, phi, lhs);
8893 /* If we will end up with a (-INF, +INF) range, set it to
8894 VARYING. Same if the previous max value was invalid for
8895 the type and we end up with vr_result.min > vr_result.max. */
8896 if ((vrp_val_is_max (vr_result.max)
8897 && vrp_val_is_min (vr_result.min))
8898 || compare_values (vr_result.min,
8899 vr_result.max) > 0)
8900 goto varying;
8903 /* If the new range is different than the previous value, keep
8904 iterating. */
8905 update_range:
8906 if (update_value_range (lhs, &vr_result))
8908 if (dump_file && (dump_flags & TDF_DETAILS))
8910 fprintf (dump_file, "Found new range for ");
8911 print_generic_expr (dump_file, lhs, 0);
8912 fprintf (dump_file, ": ");
8913 dump_value_range (dump_file, &vr_result);
8914 fprintf (dump_file, "\n");
8917 return SSA_PROP_INTERESTING;
8920 /* Nothing changed, don't add outgoing edges. */
8921 return SSA_PROP_NOT_INTERESTING;
8923 /* No match found. Set the LHS to VARYING. */
8924 varying:
8925 set_value_range_to_varying (lhs_vr);
8926 return SSA_PROP_VARYING;
8929 /* Simplify boolean operations if the source is known
8930 to be already a boolean. */
8931 static bool
8932 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8934 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8935 tree lhs, op0, op1;
8936 bool need_conversion;
8938 /* We handle only !=/== case here. */
8939 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8941 op0 = gimple_assign_rhs1 (stmt);
8942 if (!op_with_boolean_value_range_p (op0))
8943 return false;
8945 op1 = gimple_assign_rhs2 (stmt);
8946 if (!op_with_boolean_value_range_p (op1))
8947 return false;
8949 /* Reduce number of cases to handle to NE_EXPR. As there is no
8950 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8951 if (rhs_code == EQ_EXPR)
8953 if (TREE_CODE (op1) == INTEGER_CST)
8954 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8955 build_int_cst (TREE_TYPE (op1), 1));
8956 else
8957 return false;
8960 lhs = gimple_assign_lhs (stmt);
8961 need_conversion
8962 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8964 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8965 if (need_conversion
8966 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8967 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8968 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8969 return false;
8971 /* For A != 0 we can substitute A itself. */
8972 if (integer_zerop (op1))
8973 gimple_assign_set_rhs_with_ops (gsi,
8974 need_conversion
8975 ? NOP_EXPR : TREE_CODE (op0), op0);
8976 /* For A != B we substitute A ^ B. Either with conversion. */
8977 else if (need_conversion)
8979 tree tem = make_ssa_name (TREE_TYPE (op0));
8980 gassign *newop
8981 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
8982 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8983 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
8985 /* Or without. */
8986 else
8987 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8988 update_stmt (gsi_stmt (*gsi));
8990 return true;
8993 /* Simplify a division or modulo operator to a right shift or
8994 bitwise and if the first operand is unsigned or is greater
8995 than zero and the second operand is an exact power of two. */
8997 static bool
8998 simplify_div_or_mod_using_ranges (gimple stmt)
9000 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9001 tree val = NULL;
9002 tree op0 = gimple_assign_rhs1 (stmt);
9003 tree op1 = gimple_assign_rhs2 (stmt);
9004 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
9006 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9008 val = integer_one_node;
9010 else
9012 bool sop = false;
9014 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9016 if (val
9017 && sop
9018 && integer_onep (val)
9019 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9021 location_t location;
9023 if (!gimple_has_location (stmt))
9024 location = input_location;
9025 else
9026 location = gimple_location (stmt);
9027 warning_at (location, OPT_Wstrict_overflow,
9028 "assuming signed overflow does not occur when "
9029 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9033 if (val && integer_onep (val))
9035 tree t;
9037 if (rhs_code == TRUNC_DIV_EXPR)
9039 t = build_int_cst (integer_type_node, tree_log2 (op1));
9040 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9041 gimple_assign_set_rhs1 (stmt, op0);
9042 gimple_assign_set_rhs2 (stmt, t);
9044 else
9046 t = build_int_cst (TREE_TYPE (op1), 1);
9047 t = int_const_binop (MINUS_EXPR, op1, t);
9048 t = fold_convert (TREE_TYPE (op0), t);
9050 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9051 gimple_assign_set_rhs1 (stmt, op0);
9052 gimple_assign_set_rhs2 (stmt, t);
9055 update_stmt (stmt);
9056 return true;
9059 return false;
9062 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9063 ABS_EXPR. If the operand is <= 0, then simplify the
9064 ABS_EXPR into a NEGATE_EXPR. */
9066 static bool
9067 simplify_abs_using_ranges (gimple stmt)
9069 tree val = NULL;
9070 tree op = gimple_assign_rhs1 (stmt);
9071 tree type = TREE_TYPE (op);
9072 value_range_t *vr = get_value_range (op);
9074 if (TYPE_UNSIGNED (type))
9076 val = integer_zero_node;
9078 else if (vr)
9080 bool sop = false;
9082 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9083 if (!val)
9085 sop = false;
9086 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
9087 &sop);
9089 if (val)
9091 if (integer_zerop (val))
9092 val = integer_one_node;
9093 else if (integer_onep (val))
9094 val = integer_zero_node;
9098 if (val
9099 && (integer_onep (val) || integer_zerop (val)))
9101 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9103 location_t location;
9105 if (!gimple_has_location (stmt))
9106 location = input_location;
9107 else
9108 location = gimple_location (stmt);
9109 warning_at (location, OPT_Wstrict_overflow,
9110 "assuming signed overflow does not occur when "
9111 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9114 gimple_assign_set_rhs1 (stmt, op);
9115 if (integer_onep (val))
9116 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9117 else
9118 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9119 update_stmt (stmt);
9120 return true;
9124 return false;
9127 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9128 If all the bits that are being cleared by & are already
9129 known to be zero from VR, or all the bits that are being
9130 set by | are already known to be one from VR, the bit
9131 operation is redundant. */
9133 static bool
9134 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9136 tree op0 = gimple_assign_rhs1 (stmt);
9137 tree op1 = gimple_assign_rhs2 (stmt);
9138 tree op = NULL_TREE;
9139 value_range_t vr0 = VR_INITIALIZER;
9140 value_range_t vr1 = VR_INITIALIZER;
9141 wide_int may_be_nonzero0, may_be_nonzero1;
9142 wide_int must_be_nonzero0, must_be_nonzero1;
9143 wide_int mask;
9145 if (TREE_CODE (op0) == SSA_NAME)
9146 vr0 = *(get_value_range (op0));
9147 else if (is_gimple_min_invariant (op0))
9148 set_value_range_to_value (&vr0, op0, NULL);
9149 else
9150 return false;
9152 if (TREE_CODE (op1) == SSA_NAME)
9153 vr1 = *(get_value_range (op1));
9154 else if (is_gimple_min_invariant (op1))
9155 set_value_range_to_value (&vr1, op1, NULL);
9156 else
9157 return false;
9159 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9160 &must_be_nonzero0))
9161 return false;
9162 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9163 &must_be_nonzero1))
9164 return false;
9166 switch (gimple_assign_rhs_code (stmt))
9168 case BIT_AND_EXPR:
9169 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9170 if (mask == 0)
9172 op = op0;
9173 break;
9175 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9176 if (mask == 0)
9178 op = op1;
9179 break;
9181 break;
9182 case BIT_IOR_EXPR:
9183 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9184 if (mask == 0)
9186 op = op1;
9187 break;
9189 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9190 if (mask == 0)
9192 op = op0;
9193 break;
9195 break;
9196 default:
9197 gcc_unreachable ();
9200 if (op == NULL_TREE)
9201 return false;
9203 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9204 update_stmt (gsi_stmt (*gsi));
9205 return true;
9208 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9209 a known value range VR.
9211 If there is one and only one value which will satisfy the
9212 conditional, then return that value. Else return NULL.
9214 If signed overflow must be undefined for the value to satisfy
9215 the conditional, then set *STRICT_OVERFLOW_P to true. */
9217 static tree
9218 test_for_singularity (enum tree_code cond_code, tree op0,
9219 tree op1, value_range_t *vr,
9220 bool *strict_overflow_p)
9222 tree min = NULL;
9223 tree max = NULL;
9225 /* Extract minimum/maximum values which satisfy the
9226 the conditional as it was written. */
9227 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9229 /* This should not be negative infinity; there is no overflow
9230 here. */
9231 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9233 max = op1;
9234 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9236 tree one = build_int_cst (TREE_TYPE (op0), 1);
9237 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9238 if (EXPR_P (max))
9239 TREE_NO_WARNING (max) = 1;
9242 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9244 /* This should not be positive infinity; there is no overflow
9245 here. */
9246 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9248 min = op1;
9249 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9251 tree one = build_int_cst (TREE_TYPE (op0), 1);
9252 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9253 if (EXPR_P (min))
9254 TREE_NO_WARNING (min) = 1;
9258 /* Now refine the minimum and maximum values using any
9259 value range information we have for op0. */
9260 if (min && max)
9262 if (compare_values (vr->min, min) == 1)
9263 min = vr->min;
9264 if (compare_values (vr->max, max) == -1)
9265 max = vr->max;
9267 /* If the new min/max values have converged to a single value,
9268 then there is only one value which can satisfy the condition,
9269 return that value. */
9270 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9272 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9273 && is_overflow_infinity (vr->max))
9274 *strict_overflow_p = true;
9275 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9276 && is_overflow_infinity (vr->min))
9277 *strict_overflow_p = true;
9279 return min;
9282 return NULL;
9285 /* Return whether the value range *VR fits in an integer type specified
9286 by PRECISION and UNSIGNED_P. */
9288 static bool
9289 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9291 tree src_type;
9292 unsigned src_precision;
9293 widest_int tem;
9294 signop src_sgn;
9296 /* We can only handle integral and pointer types. */
9297 src_type = TREE_TYPE (vr->min);
9298 if (!INTEGRAL_TYPE_P (src_type)
9299 && !POINTER_TYPE_P (src_type))
9300 return false;
9302 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9303 and so is an identity transform. */
9304 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9305 src_sgn = TYPE_SIGN (src_type);
9306 if ((src_precision < dest_precision
9307 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9308 || (src_precision == dest_precision && src_sgn == dest_sgn))
9309 return true;
9311 /* Now we can only handle ranges with constant bounds. */
9312 if (vr->type != VR_RANGE
9313 || TREE_CODE (vr->min) != INTEGER_CST
9314 || TREE_CODE (vr->max) != INTEGER_CST)
9315 return false;
9317 /* For sign changes, the MSB of the wide_int has to be clear.
9318 An unsigned value with its MSB set cannot be represented by
9319 a signed wide_int, while a negative value cannot be represented
9320 by an unsigned wide_int. */
9321 if (src_sgn != dest_sgn
9322 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9323 return false;
9325 /* Then we can perform the conversion on both ends and compare
9326 the result for equality. */
9327 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9328 if (tem != wi::to_widest (vr->min))
9329 return false;
9330 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9331 if (tem != wi::to_widest (vr->max))
9332 return false;
9334 return true;
9337 /* Simplify a conditional using a relational operator to an equality
9338 test if the range information indicates only one value can satisfy
9339 the original conditional. */
9341 static bool
9342 simplify_cond_using_ranges (gcond *stmt)
9344 tree op0 = gimple_cond_lhs (stmt);
9345 tree op1 = gimple_cond_rhs (stmt);
9346 enum tree_code cond_code = gimple_cond_code (stmt);
9348 if (cond_code != NE_EXPR
9349 && cond_code != EQ_EXPR
9350 && TREE_CODE (op0) == SSA_NAME
9351 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9352 && is_gimple_min_invariant (op1))
9354 value_range_t *vr = get_value_range (op0);
9356 /* If we have range information for OP0, then we might be
9357 able to simplify this conditional. */
9358 if (vr->type == VR_RANGE)
9360 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9361 bool sop = false;
9362 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9364 if (new_tree
9365 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9367 if (dump_file)
9369 fprintf (dump_file, "Simplified relational ");
9370 print_gimple_stmt (dump_file, stmt, 0, 0);
9371 fprintf (dump_file, " into ");
9374 gimple_cond_set_code (stmt, EQ_EXPR);
9375 gimple_cond_set_lhs (stmt, op0);
9376 gimple_cond_set_rhs (stmt, new_tree);
9378 update_stmt (stmt);
9380 if (dump_file)
9382 print_gimple_stmt (dump_file, stmt, 0, 0);
9383 fprintf (dump_file, "\n");
9386 if (sop && issue_strict_overflow_warning (wc))
9388 location_t location = input_location;
9389 if (gimple_has_location (stmt))
9390 location = gimple_location (stmt);
9392 warning_at (location, OPT_Wstrict_overflow,
9393 "assuming signed overflow does not occur when "
9394 "simplifying conditional");
9397 return true;
9400 /* Try again after inverting the condition. We only deal
9401 with integral types here, so no need to worry about
9402 issues with inverting FP comparisons. */
9403 sop = false;
9404 new_tree = test_for_singularity
9405 (invert_tree_comparison (cond_code, false),
9406 op0, op1, vr, &sop);
9408 if (new_tree
9409 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9411 if (dump_file)
9413 fprintf (dump_file, "Simplified relational ");
9414 print_gimple_stmt (dump_file, stmt, 0, 0);
9415 fprintf (dump_file, " into ");
9418 gimple_cond_set_code (stmt, NE_EXPR);
9419 gimple_cond_set_lhs (stmt, op0);
9420 gimple_cond_set_rhs (stmt, new_tree);
9422 update_stmt (stmt);
9424 if (dump_file)
9426 print_gimple_stmt (dump_file, stmt, 0, 0);
9427 fprintf (dump_file, "\n");
9430 if (sop && issue_strict_overflow_warning (wc))
9432 location_t location = input_location;
9433 if (gimple_has_location (stmt))
9434 location = gimple_location (stmt);
9436 warning_at (location, OPT_Wstrict_overflow,
9437 "assuming signed overflow does not occur when "
9438 "simplifying conditional");
9441 return true;
9446 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9447 see if OP0 was set by a type conversion where the source of
9448 the conversion is another SSA_NAME with a range that fits
9449 into the range of OP0's type.
9451 If so, the conversion is redundant as the earlier SSA_NAME can be
9452 used for the comparison directly if we just massage the constant in the
9453 comparison. */
9454 if (TREE_CODE (op0) == SSA_NAME
9455 && TREE_CODE (op1) == INTEGER_CST)
9457 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9458 tree innerop;
9460 if (!is_gimple_assign (def_stmt)
9461 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9462 return false;
9464 innerop = gimple_assign_rhs1 (def_stmt);
9466 if (TREE_CODE (innerop) == SSA_NAME
9467 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9469 value_range_t *vr = get_value_range (innerop);
9471 if (range_int_cst_p (vr)
9472 && range_fits_type_p (vr,
9473 TYPE_PRECISION (TREE_TYPE (op0)),
9474 TYPE_SIGN (TREE_TYPE (op0)))
9475 && int_fits_type_p (op1, TREE_TYPE (innerop))
9476 /* The range must not have overflowed, or if it did overflow
9477 we must not be wrapping/trapping overflow and optimizing
9478 with strict overflow semantics. */
9479 && ((!is_negative_overflow_infinity (vr->min)
9480 && !is_positive_overflow_infinity (vr->max))
9481 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9483 /* If the range overflowed and the user has asked for warnings
9484 when strict overflow semantics were used to optimize code,
9485 issue an appropriate warning. */
9486 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9487 && (is_negative_overflow_infinity (vr->min)
9488 || is_positive_overflow_infinity (vr->max))
9489 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9491 location_t location;
9493 if (!gimple_has_location (stmt))
9494 location = input_location;
9495 else
9496 location = gimple_location (stmt);
9497 warning_at (location, OPT_Wstrict_overflow,
9498 "assuming signed overflow does not occur when "
9499 "simplifying conditional");
9502 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9503 gimple_cond_set_lhs (stmt, innerop);
9504 gimple_cond_set_rhs (stmt, newconst);
9505 return true;
9510 return false;
9513 /* Simplify a switch statement using the value range of the switch
9514 argument. */
9516 static bool
9517 simplify_switch_using_ranges (gswitch *stmt)
9519 tree op = gimple_switch_index (stmt);
9520 value_range_t *vr;
9521 bool take_default;
9522 edge e;
9523 edge_iterator ei;
9524 size_t i = 0, j = 0, n, n2;
9525 tree vec2;
9526 switch_update su;
9527 size_t k = 1, l = 0;
9529 if (TREE_CODE (op) == SSA_NAME)
9531 vr = get_value_range (op);
9533 /* We can only handle integer ranges. */
9534 if ((vr->type != VR_RANGE
9535 && vr->type != VR_ANTI_RANGE)
9536 || symbolic_range_p (vr))
9537 return false;
9539 /* Find case label for min/max of the value range. */
9540 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9542 else if (TREE_CODE (op) == INTEGER_CST)
9544 take_default = !find_case_label_index (stmt, 1, op, &i);
9545 if (take_default)
9547 i = 1;
9548 j = 0;
9550 else
9552 j = i;
9555 else
9556 return false;
9558 n = gimple_switch_num_labels (stmt);
9560 /* Bail out if this is just all edges taken. */
9561 if (i == 1
9562 && j == n - 1
9563 && take_default)
9564 return false;
9566 /* Build a new vector of taken case labels. */
9567 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9568 n2 = 0;
9570 /* Add the default edge, if necessary. */
9571 if (take_default)
9572 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9574 for (; i <= j; ++i, ++n2)
9575 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9577 for (; k <= l; ++k, ++n2)
9578 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9580 /* Mark needed edges. */
9581 for (i = 0; i < n2; ++i)
9583 e = find_edge (gimple_bb (stmt),
9584 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9585 e->aux = (void *)-1;
9588 /* Queue not needed edges for later removal. */
9589 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9591 if (e->aux == (void *)-1)
9593 e->aux = NULL;
9594 continue;
9597 if (dump_file && (dump_flags & TDF_DETAILS))
9599 fprintf (dump_file, "removing unreachable case label\n");
9601 to_remove_edges.safe_push (e);
9602 e->flags &= ~EDGE_EXECUTABLE;
9605 /* And queue an update for the stmt. */
9606 su.stmt = stmt;
9607 su.vec = vec2;
9608 to_update_switch_stmts.safe_push (su);
9609 return false;
9612 /* Simplify an integral conversion from an SSA name in STMT. */
9614 static bool
9615 simplify_conversion_using_ranges (gimple stmt)
9617 tree innerop, middleop, finaltype;
9618 gimple def_stmt;
9619 value_range_t *innervr;
9620 signop inner_sgn, middle_sgn, final_sgn;
9621 unsigned inner_prec, middle_prec, final_prec;
9622 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9624 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9625 if (!INTEGRAL_TYPE_P (finaltype))
9626 return false;
9627 middleop = gimple_assign_rhs1 (stmt);
9628 def_stmt = SSA_NAME_DEF_STMT (middleop);
9629 if (!is_gimple_assign (def_stmt)
9630 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9631 return false;
9632 innerop = gimple_assign_rhs1 (def_stmt);
9633 if (TREE_CODE (innerop) != SSA_NAME
9634 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9635 return false;
9637 /* Get the value-range of the inner operand. */
9638 innervr = get_value_range (innerop);
9639 if (innervr->type != VR_RANGE
9640 || TREE_CODE (innervr->min) != INTEGER_CST
9641 || TREE_CODE (innervr->max) != INTEGER_CST)
9642 return false;
9644 /* Simulate the conversion chain to check if the result is equal if
9645 the middle conversion is removed. */
9646 innermin = wi::to_widest (innervr->min);
9647 innermax = wi::to_widest (innervr->max);
9649 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9650 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9651 final_prec = TYPE_PRECISION (finaltype);
9653 /* If the first conversion is not injective, the second must not
9654 be widening. */
9655 if (wi::gtu_p (innermax - innermin,
9656 wi::mask <widest_int> (middle_prec, false))
9657 && middle_prec < final_prec)
9658 return false;
9659 /* We also want a medium value so that we can track the effect that
9660 narrowing conversions with sign change have. */
9661 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9662 if (inner_sgn == UNSIGNED)
9663 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9664 else
9665 innermed = 0;
9666 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9667 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9668 innermed = innermin;
9670 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9671 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9672 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9673 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9675 /* Require that the final conversion applied to both the original
9676 and the intermediate range produces the same result. */
9677 final_sgn = TYPE_SIGN (finaltype);
9678 if (wi::ext (middlemin, final_prec, final_sgn)
9679 != wi::ext (innermin, final_prec, final_sgn)
9680 || wi::ext (middlemed, final_prec, final_sgn)
9681 != wi::ext (innermed, final_prec, final_sgn)
9682 || wi::ext (middlemax, final_prec, final_sgn)
9683 != wi::ext (innermax, final_prec, final_sgn))
9684 return false;
9686 gimple_assign_set_rhs1 (stmt, innerop);
9687 update_stmt (stmt);
9688 return true;
9691 /* Simplify a conversion from integral SSA name to float in STMT. */
9693 static bool
9694 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9696 tree rhs1 = gimple_assign_rhs1 (stmt);
9697 value_range_t *vr = get_value_range (rhs1);
9698 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9699 machine_mode mode;
9700 tree tem;
9701 gassign *conv;
9703 /* We can only handle constant ranges. */
9704 if (vr->type != VR_RANGE
9705 || TREE_CODE (vr->min) != INTEGER_CST
9706 || TREE_CODE (vr->max) != INTEGER_CST)
9707 return false;
9709 /* First check if we can use a signed type in place of an unsigned. */
9710 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9711 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9712 != CODE_FOR_nothing)
9713 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9714 mode = TYPE_MODE (TREE_TYPE (rhs1));
9715 /* If we can do the conversion in the current input mode do nothing. */
9716 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9717 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9718 return false;
9719 /* Otherwise search for a mode we can use, starting from the narrowest
9720 integer mode available. */
9721 else
9723 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9726 /* If we cannot do a signed conversion to float from mode
9727 or if the value-range does not fit in the signed type
9728 try with a wider mode. */
9729 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9730 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9731 break;
9733 mode = GET_MODE_WIDER_MODE (mode);
9734 /* But do not widen the input. Instead leave that to the
9735 optabs expansion code. */
9736 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9737 return false;
9739 while (mode != VOIDmode);
9740 if (mode == VOIDmode)
9741 return false;
9744 /* It works, insert a truncation or sign-change before the
9745 float conversion. */
9746 tem = make_ssa_name (build_nonstandard_integer_type
9747 (GET_MODE_PRECISION (mode), 0));
9748 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9749 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9750 gimple_assign_set_rhs1 (stmt, tem);
9751 update_stmt (stmt);
9753 return true;
9756 /* Simplify an internal fn call using ranges if possible. */
9758 static bool
9759 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9761 enum tree_code subcode;
9762 bool is_ubsan = false;
9763 bool ovf = false;
9764 switch (gimple_call_internal_fn (stmt))
9766 case IFN_UBSAN_CHECK_ADD:
9767 subcode = PLUS_EXPR;
9768 is_ubsan = true;
9769 break;
9770 case IFN_UBSAN_CHECK_SUB:
9771 subcode = MINUS_EXPR;
9772 is_ubsan = true;
9773 break;
9774 case IFN_UBSAN_CHECK_MUL:
9775 subcode = MULT_EXPR;
9776 is_ubsan = true;
9777 break;
9778 case IFN_ADD_OVERFLOW:
9779 subcode = PLUS_EXPR;
9780 break;
9781 case IFN_SUB_OVERFLOW:
9782 subcode = MINUS_EXPR;
9783 break;
9784 case IFN_MUL_OVERFLOW:
9785 subcode = MULT_EXPR;
9786 break;
9787 default:
9788 return false;
9791 tree op0 = gimple_call_arg (stmt, 0);
9792 tree op1 = gimple_call_arg (stmt, 1);
9793 tree type;
9794 if (is_ubsan)
9795 type = TREE_TYPE (op0);
9796 else if (gimple_call_lhs (stmt) == NULL_TREE)
9797 return false;
9798 else
9799 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9800 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9801 || (is_ubsan && ovf))
9802 return false;
9804 gimple g;
9805 location_t loc = gimple_location (stmt);
9806 if (is_ubsan)
9807 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9808 else
9810 int prec = TYPE_PRECISION (type);
9811 tree utype = type;
9812 if (ovf
9813 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9814 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9815 utype = build_nonstandard_integer_type (prec, 1);
9816 if (TREE_CODE (op0) == INTEGER_CST)
9817 op0 = fold_convert (utype, op0);
9818 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9820 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9821 gimple_set_location (g, loc);
9822 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9823 op0 = gimple_assign_lhs (g);
9825 if (TREE_CODE (op1) == INTEGER_CST)
9826 op1 = fold_convert (utype, op1);
9827 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9829 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9830 gimple_set_location (g, loc);
9831 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9832 op1 = gimple_assign_lhs (g);
9834 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9835 gimple_set_location (g, loc);
9836 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9837 if (utype != type)
9839 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9840 gimple_assign_lhs (g));
9841 gimple_set_location (g, loc);
9842 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9844 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9845 gimple_assign_lhs (g),
9846 build_int_cst (type, ovf));
9848 gimple_set_location (g, loc);
9849 gsi_replace (gsi, g, false);
9850 return true;
9853 /* Simplify STMT using ranges if possible. */
9855 static bool
9856 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9858 gimple stmt = gsi_stmt (*gsi);
9859 if (is_gimple_assign (stmt))
9861 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9862 tree rhs1 = gimple_assign_rhs1 (stmt);
9864 switch (rhs_code)
9866 case EQ_EXPR:
9867 case NE_EXPR:
9868 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9869 if the RHS is zero or one, and the LHS are known to be boolean
9870 values. */
9871 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9872 return simplify_truth_ops_using_ranges (gsi, stmt);
9873 break;
9875 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9876 and BIT_AND_EXPR respectively if the first operand is greater
9877 than zero and the second operand is an exact power of two. */
9878 case TRUNC_DIV_EXPR:
9879 case TRUNC_MOD_EXPR:
9880 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9881 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9882 return simplify_div_or_mod_using_ranges (stmt);
9883 break;
9885 /* Transform ABS (X) into X or -X as appropriate. */
9886 case ABS_EXPR:
9887 if (TREE_CODE (rhs1) == SSA_NAME
9888 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9889 return simplify_abs_using_ranges (stmt);
9890 break;
9892 case BIT_AND_EXPR:
9893 case BIT_IOR_EXPR:
9894 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9895 if all the bits being cleared are already cleared or
9896 all the bits being set are already set. */
9897 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9898 return simplify_bit_ops_using_ranges (gsi, stmt);
9899 break;
9901 CASE_CONVERT:
9902 if (TREE_CODE (rhs1) == SSA_NAME
9903 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9904 return simplify_conversion_using_ranges (stmt);
9905 break;
9907 case FLOAT_EXPR:
9908 if (TREE_CODE (rhs1) == SSA_NAME
9909 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9910 return simplify_float_conversion_using_ranges (gsi, stmt);
9911 break;
9913 default:
9914 break;
9917 else if (gimple_code (stmt) == GIMPLE_COND)
9918 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9919 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9920 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
9921 else if (is_gimple_call (stmt)
9922 && gimple_call_internal_p (stmt))
9923 return simplify_internal_call_using_ranges (gsi, stmt);
9925 return false;
9928 /* If the statement pointed by SI has a predicate whose value can be
9929 computed using the value range information computed by VRP, compute
9930 its value and return true. Otherwise, return false. */
9932 static bool
9933 fold_predicate_in (gimple_stmt_iterator *si)
9935 bool assignment_p = false;
9936 tree val;
9937 gimple stmt = gsi_stmt (*si);
9939 if (is_gimple_assign (stmt)
9940 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9942 assignment_p = true;
9943 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9944 gimple_assign_rhs1 (stmt),
9945 gimple_assign_rhs2 (stmt),
9946 stmt);
9948 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
9949 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
9950 gimple_cond_lhs (cond_stmt),
9951 gimple_cond_rhs (cond_stmt),
9952 stmt);
9953 else
9954 return false;
9956 if (val)
9958 if (assignment_p)
9959 val = fold_convert (gimple_expr_type (stmt), val);
9961 if (dump_file)
9963 fprintf (dump_file, "Folding predicate ");
9964 print_gimple_expr (dump_file, stmt, 0, 0);
9965 fprintf (dump_file, " to ");
9966 print_generic_expr (dump_file, val, 0);
9967 fprintf (dump_file, "\n");
9970 if (is_gimple_assign (stmt))
9971 gimple_assign_set_rhs_from_tree (si, val);
9972 else
9974 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9975 gcond *cond_stmt = as_a <gcond *> (stmt);
9976 if (integer_zerop (val))
9977 gimple_cond_make_false (cond_stmt);
9978 else if (integer_onep (val))
9979 gimple_cond_make_true (cond_stmt);
9980 else
9981 gcc_unreachable ();
9984 return true;
9987 return false;
9990 /* Callback for substitute_and_fold folding the stmt at *SI. */
9992 static bool
9993 vrp_fold_stmt (gimple_stmt_iterator *si)
9995 if (fold_predicate_in (si))
9996 return true;
9998 return simplify_stmt_using_ranges (si);
10001 /* Stack of dest,src equivalency pairs that need to be restored after
10002 each attempt to thread a block's incoming edge to an outgoing edge.
10004 A NULL entry is used to mark the end of pairs which need to be
10005 restored. */
10006 static vec<tree> equiv_stack;
10008 /* A trivial wrapper so that we can present the generic jump threading
10009 code with a simple API for simplifying statements. STMT is the
10010 statement we want to simplify, WITHIN_STMT provides the location
10011 for any overflow warnings. */
10013 static tree
10014 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
10016 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10017 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10018 gimple_cond_lhs (cond_stmt),
10019 gimple_cond_rhs (cond_stmt),
10020 within_stmt);
10022 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10024 value_range_t new_vr = VR_INITIALIZER;
10025 tree lhs = gimple_assign_lhs (assign_stmt);
10027 if (TREE_CODE (lhs) == SSA_NAME
10028 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10029 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10031 extract_range_from_assignment (&new_vr, assign_stmt);
10032 if (range_int_cst_singleton_p (&new_vr))
10033 return new_vr.min;
10037 return NULL_TREE;
10040 /* Blocks which have more than one predecessor and more than
10041 one successor present jump threading opportunities, i.e.,
10042 when the block is reached from a specific predecessor, we
10043 may be able to determine which of the outgoing edges will
10044 be traversed. When this optimization applies, we are able
10045 to avoid conditionals at runtime and we may expose secondary
10046 optimization opportunities.
10048 This routine is effectively a driver for the generic jump
10049 threading code. It basically just presents the generic code
10050 with edges that may be suitable for jump threading.
10052 Unlike DOM, we do not iterate VRP if jump threading was successful.
10053 While iterating may expose new opportunities for VRP, it is expected
10054 those opportunities would be very limited and the compile time cost
10055 to expose those opportunities would be significant.
10057 As jump threading opportunities are discovered, they are registered
10058 for later realization. */
10060 static void
10061 identify_jump_threads (void)
10063 basic_block bb;
10064 gcond *dummy;
10065 int i;
10066 edge e;
10068 /* Ugh. When substituting values earlier in this pass we can
10069 wipe the dominance information. So rebuild the dominator
10070 information as we need it within the jump threading code. */
10071 calculate_dominance_info (CDI_DOMINATORS);
10073 /* We do not allow VRP information to be used for jump threading
10074 across a back edge in the CFG. Otherwise it becomes too
10075 difficult to avoid eliminating loop exit tests. Of course
10076 EDGE_DFS_BACK is not accurate at this time so we have to
10077 recompute it. */
10078 mark_dfs_back_edges ();
10080 /* Do not thread across edges we are about to remove. Just marking
10081 them as EDGE_DFS_BACK will do. */
10082 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10083 e->flags |= EDGE_DFS_BACK;
10085 /* Allocate our unwinder stack to unwind any temporary equivalences
10086 that might be recorded. */
10087 equiv_stack.create (20);
10089 /* To avoid lots of silly node creation, we create a single
10090 conditional and just modify it in-place when attempting to
10091 thread jumps. */
10092 dummy = gimple_build_cond (EQ_EXPR,
10093 integer_zero_node, integer_zero_node,
10094 NULL, NULL);
10096 /* Walk through all the blocks finding those which present a
10097 potential jump threading opportunity. We could set this up
10098 as a dominator walker and record data during the walk, but
10099 I doubt it's worth the effort for the classes of jump
10100 threading opportunities we are trying to identify at this
10101 point in compilation. */
10102 FOR_EACH_BB_FN (bb, cfun)
10104 gimple last;
10106 /* If the generic jump threading code does not find this block
10107 interesting, then there is nothing to do. */
10108 if (! potentially_threadable_block (bb))
10109 continue;
10111 /* We only care about blocks ending in a COND_EXPR. While there
10112 may be some value in handling SWITCH_EXPR here, I doubt it's
10113 terribly important. */
10114 last = gsi_stmt (gsi_last_bb (bb));
10116 /* We're basically looking for a switch or any kind of conditional with
10117 integral or pointer type arguments. Note the type of the second
10118 argument will be the same as the first argument, so no need to
10119 check it explicitly. */
10120 if (gimple_code (last) == GIMPLE_SWITCH
10121 || (gimple_code (last) == GIMPLE_COND
10122 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10123 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10124 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10125 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10126 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10128 edge_iterator ei;
10130 /* We've got a block with multiple predecessors and multiple
10131 successors which also ends in a suitable conditional or
10132 switch statement. For each predecessor, see if we can thread
10133 it to a specific successor. */
10134 FOR_EACH_EDGE (e, ei, bb->preds)
10136 /* Do not thread across back edges or abnormal edges
10137 in the CFG. */
10138 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
10139 continue;
10141 thread_across_edge (dummy, e, true, &equiv_stack,
10142 simplify_stmt_for_jump_threading);
10147 /* We do not actually update the CFG or SSA graphs at this point as
10148 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10149 handle ASSERT_EXPRs gracefully. */
10152 /* We identified all the jump threading opportunities earlier, but could
10153 not transform the CFG at that time. This routine transforms the
10154 CFG and arranges for the dominator tree to be rebuilt if necessary.
10156 Note the SSA graph update will occur during the normal TODO
10157 processing by the pass manager. */
10158 static void
10159 finalize_jump_threads (void)
10161 thread_through_all_blocks (false);
10162 equiv_stack.release ();
10166 /* Traverse all the blocks folding conditionals with known ranges. */
10168 static void
10169 vrp_finalize (void)
10171 size_t i;
10173 values_propagated = true;
10175 if (dump_file)
10177 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10178 dump_all_value_ranges (dump_file);
10179 fprintf (dump_file, "\n");
10182 substitute_and_fold (op_with_constant_singleton_value_range,
10183 vrp_fold_stmt, false);
10185 if (warn_array_bounds)
10186 check_all_array_refs ();
10188 /* We must identify jump threading opportunities before we release
10189 the datastructures built by VRP. */
10190 identify_jump_threads ();
10192 /* Set value range to non pointer SSA_NAMEs. */
10193 for (i = 0; i < num_vr_values; i++)
10194 if (vr_value[i])
10196 tree name = ssa_name (i);
10198 if (!name
10199 || POINTER_TYPE_P (TREE_TYPE (name))
10200 || (vr_value[i]->type == VR_VARYING)
10201 || (vr_value[i]->type == VR_UNDEFINED))
10202 continue;
10204 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10205 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10206 && (vr_value[i]->type == VR_RANGE
10207 || vr_value[i]->type == VR_ANTI_RANGE))
10208 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10209 vr_value[i]->max);
10212 /* Free allocated memory. */
10213 for (i = 0; i < num_vr_values; i++)
10214 if (vr_value[i])
10216 BITMAP_FREE (vr_value[i]->equiv);
10217 free (vr_value[i]);
10220 free (vr_value);
10221 free (vr_phi_edge_counts);
10223 /* So that we can distinguish between VRP data being available
10224 and not available. */
10225 vr_value = NULL;
10226 vr_phi_edge_counts = NULL;
10230 /* Main entry point to VRP (Value Range Propagation). This pass is
10231 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10232 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10233 Programming Language Design and Implementation, pp. 67-78, 1995.
10234 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10236 This is essentially an SSA-CCP pass modified to deal with ranges
10237 instead of constants.
10239 While propagating ranges, we may find that two or more SSA name
10240 have equivalent, though distinct ranges. For instance,
10242 1 x_9 = p_3->a;
10243 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10244 3 if (p_4 == q_2)
10245 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10246 5 endif
10247 6 if (q_2)
10249 In the code above, pointer p_5 has range [q_2, q_2], but from the
10250 code we can also determine that p_5 cannot be NULL and, if q_2 had
10251 a non-varying range, p_5's range should also be compatible with it.
10253 These equivalences are created by two expressions: ASSERT_EXPR and
10254 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10255 result of another assertion, then we can use the fact that p_5 and
10256 p_4 are equivalent when evaluating p_5's range.
10258 Together with value ranges, we also propagate these equivalences
10259 between names so that we can take advantage of information from
10260 multiple ranges when doing final replacement. Note that this
10261 equivalency relation is transitive but not symmetric.
10263 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10264 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10265 in contexts where that assertion does not hold (e.g., in line 6).
10267 TODO, the main difference between this pass and Patterson's is that
10268 we do not propagate edge probabilities. We only compute whether
10269 edges can be taken or not. That is, instead of having a spectrum
10270 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10271 DON'T KNOW. In the future, it may be worthwhile to propagate
10272 probabilities to aid branch prediction. */
10274 static unsigned int
10275 execute_vrp (void)
10277 int i;
10278 edge e;
10279 switch_update *su;
10281 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10282 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10283 scev_initialize ();
10285 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10286 Inserting assertions may split edges which will invalidate
10287 EDGE_DFS_BACK. */
10288 insert_range_assertions ();
10290 to_remove_edges.create (10);
10291 to_update_switch_stmts.create (5);
10292 threadedge_initialize_values ();
10294 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10295 mark_dfs_back_edges ();
10297 vrp_initialize ();
10298 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10299 vrp_finalize ();
10301 free_numbers_of_iterations_estimates ();
10303 /* ASSERT_EXPRs must be removed before finalizing jump threads
10304 as finalizing jump threads calls the CFG cleanup code which
10305 does not properly handle ASSERT_EXPRs. */
10306 remove_range_assertions ();
10308 /* If we exposed any new variables, go ahead and put them into
10309 SSA form now, before we handle jump threading. This simplifies
10310 interactions between rewriting of _DECL nodes into SSA form
10311 and rewriting SSA_NAME nodes into SSA form after block
10312 duplication and CFG manipulation. */
10313 update_ssa (TODO_update_ssa);
10315 finalize_jump_threads ();
10317 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10318 CFG in a broken state and requires a cfg_cleanup run. */
10319 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10320 remove_edge (e);
10321 /* Update SWITCH_EXPR case label vector. */
10322 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10324 size_t j;
10325 size_t n = TREE_VEC_LENGTH (su->vec);
10326 tree label;
10327 gimple_switch_set_num_labels (su->stmt, n);
10328 for (j = 0; j < n; j++)
10329 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10330 /* As we may have replaced the default label with a regular one
10331 make sure to make it a real default label again. This ensures
10332 optimal expansion. */
10333 label = gimple_switch_label (su->stmt, 0);
10334 CASE_LOW (label) = NULL_TREE;
10335 CASE_HIGH (label) = NULL_TREE;
10338 if (to_remove_edges.length () > 0)
10340 free_dominance_info (CDI_DOMINATORS);
10341 loops_state_set (LOOPS_NEED_FIXUP);
10344 to_remove_edges.release ();
10345 to_update_switch_stmts.release ();
10346 threadedge_finalize_values ();
10348 scev_finalize ();
10349 loop_optimizer_finalize ();
10350 return 0;
10353 namespace {
10355 const pass_data pass_data_vrp =
10357 GIMPLE_PASS, /* type */
10358 "vrp", /* name */
10359 OPTGROUP_NONE, /* optinfo_flags */
10360 TV_TREE_VRP, /* tv_id */
10361 PROP_ssa, /* properties_required */
10362 0, /* properties_provided */
10363 0, /* properties_destroyed */
10364 0, /* todo_flags_start */
10365 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10368 class pass_vrp : public gimple_opt_pass
10370 public:
10371 pass_vrp (gcc::context *ctxt)
10372 : gimple_opt_pass (pass_data_vrp, ctxt)
10375 /* opt_pass methods: */
10376 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10377 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10378 virtual unsigned int execute (function *) { return execute_vrp (); }
10380 }; // class pass_vrp
10382 } // anon namespace
10384 gimple_opt_pass *
10385 make_pass_vrp (gcc::context *ctxt)
10387 return new pass_vrp (ctxt);