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