Daily bump.
[official-gcc.git] / gcc / tree-vrp.c
blobdb1df37d4718b2828005246d2c66cb0ed557326a
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2015 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 "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "stor-layout.h"
31 #include "calls.h"
32 #include "predict.h"
33 #include "hard-reg-set.h"
34 #include "function.h"
35 #include "dominance.h"
36 #include "cfg.h"
37 #include "cfganal.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-fold.h"
42 #include "tree-eh.h"
43 #include "gimple-expr.h"
44 #include "gimple.h"
45 #include "gimple-iterator.h"
46 #include "gimple-walk.h"
47 #include "gimple-ssa.h"
48 #include "tree-cfg.h"
49 #include "tree-phinodes.h"
50 #include "ssa-iterators.h"
51 #include "stringpool.h"
52 #include "tree-ssanames.h"
53 #include "tree-ssa-loop-manip.h"
54 #include "tree-ssa-loop-niter.h"
55 #include "tree-ssa-loop.h"
56 #include "tree-into-ssa.h"
57 #include "tree-ssa.h"
58 #include "tree-pass.h"
59 #include "tree-dump.h"
60 #include "gimple-pretty-print.h"
61 #include "diagnostic-core.h"
62 #include "intl.h"
63 #include "cfgloop.h"
64 #include "tree-scalar-evolution.h"
65 #include "tree-ssa-propagate.h"
66 #include "tree-chrec.h"
67 #include "tree-ssa-threadupdate.h"
68 #include "rtl.h"
69 #include "insn-config.h"
70 #include "expmed.h"
71 #include "dojump.h"
72 #include "explow.h"
73 #include "emit-rtl.h"
74 #include "varasm.h"
75 #include "stmt.h"
76 #include "expr.h"
77 #include "insn-codes.h"
78 #include "optabs.h"
79 #include "tree-ssa-scopedtables.h"
80 #include "tree-ssa-threadedge.h"
84 /* Range of values that can be associated with an SSA_NAME after VRP
85 has executed. */
86 struct value_range_d
88 /* Lattice value represented by this range. */
89 enum value_range_type type;
91 /* Minimum and maximum values represented by this range. These
92 values should be interpreted as follows:
94 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
95 be NULL.
97 - If TYPE == VR_RANGE then MIN holds the minimum value and
98 MAX holds the maximum value of the range [MIN, MAX].
100 - If TYPE == ANTI_RANGE the variable is known to NOT
101 take any values in the range [MIN, MAX]. */
102 tree min;
103 tree max;
105 /* Set of SSA names whose value ranges are equivalent to this one.
106 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
107 bitmap equiv;
110 typedef struct value_range_d value_range_t;
112 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
114 /* Set of SSA names found live during the RPO traversal of the function
115 for still active basic-blocks. */
116 static sbitmap *live;
118 /* Return true if the SSA name NAME is live on the edge E. */
120 static bool
121 live_on_edge (edge e, tree name)
123 return (live[e->dest->index]
124 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
127 /* Local functions. */
128 static int compare_values (tree val1, tree val2);
129 static int compare_values_warnv (tree val1, tree val2, bool *);
130 static void vrp_meet (value_range_t *, value_range_t *);
131 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
132 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
133 tree, tree, bool, bool *,
134 bool *);
136 /* Location information for ASSERT_EXPRs. Each instance of this
137 structure describes an ASSERT_EXPR for an SSA name. Since a single
138 SSA name may have more than one assertion associated with it, these
139 locations are kept in a linked list attached to the corresponding
140 SSA name. */
141 struct assert_locus_d
143 /* Basic block where the assertion would be inserted. */
144 basic_block bb;
146 /* Some assertions need to be inserted on an edge (e.g., assertions
147 generated by COND_EXPRs). In those cases, BB will be NULL. */
148 edge e;
150 /* Pointer to the statement that generated this assertion. */
151 gimple_stmt_iterator si;
153 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
154 enum tree_code comp_code;
156 /* Value being compared against. */
157 tree val;
159 /* Expression to compare. */
160 tree expr;
162 /* Next node in the linked list. */
163 struct assert_locus_d *next;
166 typedef struct assert_locus_d *assert_locus_t;
168 /* If bit I is present, it means that SSA name N_i has a list of
169 assertions that should be inserted in the IL. */
170 static bitmap need_assert_for;
172 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
173 holds a list of ASSERT_LOCUS_T nodes that describe where
174 ASSERT_EXPRs for SSA name N_I should be inserted. */
175 static assert_locus_t *asserts_for;
177 /* Value range array. After propagation, VR_VALUE[I] holds the range
178 of values that SSA name N_I may take. */
179 static unsigned num_vr_values;
180 static value_range_t **vr_value;
181 static bool values_propagated;
183 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
184 number of executable edges we saw the last time we visited the
185 node. */
186 static int *vr_phi_edge_counts;
188 typedef struct {
189 gswitch *stmt;
190 tree vec;
191 } switch_update;
193 static vec<edge> to_remove_edges;
194 static vec<switch_update> to_update_switch_stmts;
197 /* Return the maximum value for TYPE. */
199 static inline tree
200 vrp_val_max (const_tree type)
202 if (!INTEGRAL_TYPE_P (type))
203 return NULL_TREE;
205 return TYPE_MAX_VALUE (type);
208 /* Return the minimum value for TYPE. */
210 static inline tree
211 vrp_val_min (const_tree type)
213 if (!INTEGRAL_TYPE_P (type))
214 return NULL_TREE;
216 return TYPE_MIN_VALUE (type);
219 /* Return whether VAL is equal to the maximum value of its type. This
220 will be true for a positive overflow infinity. We can't do a
221 simple equality comparison with TYPE_MAX_VALUE because C typedefs
222 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
223 to the integer constant with the same value in the type. */
225 static inline bool
226 vrp_val_is_max (const_tree val)
228 tree type_max = vrp_val_max (TREE_TYPE (val));
229 return (val == type_max
230 || (type_max != NULL_TREE
231 && operand_equal_p (val, type_max, 0)));
234 /* Return whether VAL is equal to the minimum value of its type. This
235 will be true for a negative overflow infinity. */
237 static inline bool
238 vrp_val_is_min (const_tree val)
240 tree type_min = vrp_val_min (TREE_TYPE (val));
241 return (val == type_min
242 || (type_min != NULL_TREE
243 && operand_equal_p (val, type_min, 0)));
247 /* Return whether TYPE should use an overflow infinity distinct from
248 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
249 represent a signed overflow during VRP computations. An infinity
250 is distinct from a half-range, which will go from some number to
251 TYPE_{MIN,MAX}_VALUE. */
253 static inline bool
254 needs_overflow_infinity (const_tree type)
256 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
259 /* Return whether TYPE can support our overflow infinity
260 representation: we use the TREE_OVERFLOW flag, which only exists
261 for constants. If TYPE doesn't support this, we don't optimize
262 cases which would require signed overflow--we drop them to
263 VARYING. */
265 static inline bool
266 supports_overflow_infinity (const_tree type)
268 tree min = vrp_val_min (type), max = vrp_val_max (type);
269 #ifdef ENABLE_CHECKING
270 gcc_assert (needs_overflow_infinity (type));
271 #endif
272 return (min != NULL_TREE
273 && CONSTANT_CLASS_P (min)
274 && max != NULL_TREE
275 && CONSTANT_CLASS_P (max));
278 /* VAL is the maximum or minimum value of a type. Return a
279 corresponding overflow infinity. */
281 static inline tree
282 make_overflow_infinity (tree val)
284 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
285 val = copy_node (val);
286 TREE_OVERFLOW (val) = 1;
287 return val;
290 /* Return a negative overflow infinity for TYPE. */
292 static inline tree
293 negative_overflow_infinity (tree type)
295 gcc_checking_assert (supports_overflow_infinity (type));
296 return make_overflow_infinity (vrp_val_min (type));
299 /* Return a positive overflow infinity for TYPE. */
301 static inline tree
302 positive_overflow_infinity (tree type)
304 gcc_checking_assert (supports_overflow_infinity (type));
305 return make_overflow_infinity (vrp_val_max (type));
308 /* Return whether VAL is a negative overflow infinity. */
310 static inline bool
311 is_negative_overflow_infinity (const_tree val)
313 return (TREE_OVERFLOW_P (val)
314 && needs_overflow_infinity (TREE_TYPE (val))
315 && vrp_val_is_min (val));
318 /* Return whether VAL is a positive overflow infinity. */
320 static inline bool
321 is_positive_overflow_infinity (const_tree val)
323 return (TREE_OVERFLOW_P (val)
324 && needs_overflow_infinity (TREE_TYPE (val))
325 && vrp_val_is_max (val));
328 /* Return whether VAL is a positive or negative overflow infinity. */
330 static inline bool
331 is_overflow_infinity (const_tree val)
333 return (TREE_OVERFLOW_P (val)
334 && needs_overflow_infinity (TREE_TYPE (val))
335 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
338 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
340 static inline bool
341 stmt_overflow_infinity (gimple stmt)
343 if (is_gimple_assign (stmt)
344 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
345 GIMPLE_SINGLE_RHS)
346 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
347 return false;
350 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
351 the same value with TREE_OVERFLOW clear. This can be used to avoid
352 confusing a regular value with an overflow value. */
354 static inline tree
355 avoid_overflow_infinity (tree val)
357 if (!is_overflow_infinity (val))
358 return val;
360 if (vrp_val_is_max (val))
361 return vrp_val_max (TREE_TYPE (val));
362 else
364 gcc_checking_assert (vrp_val_is_min (val));
365 return vrp_val_min (TREE_TYPE (val));
370 /* Return true if ARG is marked with the nonnull attribute in the
371 current function signature. */
373 static bool
374 nonnull_arg_p (const_tree arg)
376 tree t, attrs, fntype;
377 unsigned HOST_WIDE_INT arg_num;
379 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
381 /* The static chain decl is always non null. */
382 if (arg == cfun->static_chain_decl)
383 return true;
385 /* THIS argument of method is always non-NULL. */
386 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
387 && arg == DECL_ARGUMENTS (current_function_decl)
388 && flag_delete_null_pointer_checks)
389 return true;
391 /* Values passed by reference are always non-NULL. */
392 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
393 && flag_delete_null_pointer_checks)
394 return true;
396 fntype = TREE_TYPE (current_function_decl);
397 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
399 attrs = lookup_attribute ("nonnull", attrs);
401 /* If "nonnull" wasn't specified, we know nothing about the argument. */
402 if (attrs == NULL_TREE)
403 return false;
405 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
406 if (TREE_VALUE (attrs) == NULL_TREE)
407 return true;
409 /* Get the position number for ARG in the function signature. */
410 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
412 t = DECL_CHAIN (t), arg_num++)
414 if (t == arg)
415 break;
418 gcc_assert (t == arg);
420 /* Now see if ARG_NUM is mentioned in the nonnull list. */
421 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
423 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
424 return true;
428 return false;
432 /* Set value range VR to VR_UNDEFINED. */
434 static inline void
435 set_value_range_to_undefined (value_range_t *vr)
437 vr->type = VR_UNDEFINED;
438 vr->min = vr->max = NULL_TREE;
439 if (vr->equiv)
440 bitmap_clear (vr->equiv);
444 /* Set value range VR to VR_VARYING. */
446 static inline void
447 set_value_range_to_varying (value_range_t *vr)
449 vr->type = VR_VARYING;
450 vr->min = vr->max = NULL_TREE;
451 if (vr->equiv)
452 bitmap_clear (vr->equiv);
456 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
458 static void
459 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
460 tree max, bitmap equiv)
462 #if defined ENABLE_CHECKING
463 /* Check the validity of the range. */
464 if (t == VR_RANGE || t == VR_ANTI_RANGE)
466 int cmp;
468 gcc_assert (min && max);
470 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
471 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
473 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
474 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
476 cmp = compare_values (min, max);
477 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
479 if (needs_overflow_infinity (TREE_TYPE (min)))
480 gcc_assert (!is_overflow_infinity (min)
481 || !is_overflow_infinity (max));
484 if (t == VR_UNDEFINED || t == VR_VARYING)
485 gcc_assert (min == NULL_TREE && max == NULL_TREE);
487 if (t == VR_UNDEFINED || t == VR_VARYING)
488 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
489 #endif
491 vr->type = t;
492 vr->min = min;
493 vr->max = max;
495 /* Since updating the equivalence set involves deep copying the
496 bitmaps, only do it if absolutely necessary. */
497 if (vr->equiv == NULL
498 && equiv != NULL)
499 vr->equiv = BITMAP_ALLOC (NULL);
501 if (equiv != vr->equiv)
503 if (equiv && !bitmap_empty_p (equiv))
504 bitmap_copy (vr->equiv, equiv);
505 else
506 bitmap_clear (vr->equiv);
511 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
512 This means adjusting T, MIN and MAX representing the case of a
513 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
514 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
515 In corner cases where MAX+1 or MIN-1 wraps this will fall back
516 to varying.
517 This routine exists to ease canonicalization in the case where we
518 extract ranges from var + CST op limit. */
520 static void
521 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
522 tree min, tree max, bitmap equiv)
524 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
525 if (t == VR_UNDEFINED)
527 set_value_range_to_undefined (vr);
528 return;
530 else if (t == VR_VARYING)
532 set_value_range_to_varying (vr);
533 return;
536 /* Nothing to canonicalize for symbolic ranges. */
537 if (TREE_CODE (min) != INTEGER_CST
538 || TREE_CODE (max) != INTEGER_CST)
540 set_value_range (vr, t, min, max, equiv);
541 return;
544 /* Wrong order for min and max, to swap them and the VR type we need
545 to adjust them. */
546 if (tree_int_cst_lt (max, min))
548 tree one, tmp;
550 /* For one bit precision if max < min, then the swapped
551 range covers all values, so for VR_RANGE it is varying and
552 for VR_ANTI_RANGE empty range, so drop to varying as well. */
553 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
555 set_value_range_to_varying (vr);
556 return;
559 one = build_int_cst (TREE_TYPE (min), 1);
560 tmp = int_const_binop (PLUS_EXPR, max, one);
561 max = int_const_binop (MINUS_EXPR, min, one);
562 min = tmp;
564 /* There's one corner case, if we had [C+1, C] before we now have
565 that again. But this represents an empty value range, so drop
566 to varying in this case. */
567 if (tree_int_cst_lt (max, min))
569 set_value_range_to_varying (vr);
570 return;
573 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
576 /* Anti-ranges that can be represented as ranges should be so. */
577 if (t == VR_ANTI_RANGE)
579 bool is_min = vrp_val_is_min (min);
580 bool is_max = vrp_val_is_max (max);
582 if (is_min && is_max)
584 /* We cannot deal with empty ranges, drop to varying.
585 ??? This could be VR_UNDEFINED instead. */
586 set_value_range_to_varying (vr);
587 return;
589 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
590 && (is_min || is_max))
592 /* Non-empty boolean ranges can always be represented
593 as a singleton range. */
594 if (is_min)
595 min = max = vrp_val_max (TREE_TYPE (min));
596 else
597 min = max = vrp_val_min (TREE_TYPE (min));
598 t = VR_RANGE;
600 else if (is_min
601 /* As a special exception preserve non-null ranges. */
602 && !(TYPE_UNSIGNED (TREE_TYPE (min))
603 && integer_zerop (max)))
605 tree one = build_int_cst (TREE_TYPE (max), 1);
606 min = int_const_binop (PLUS_EXPR, max, one);
607 max = vrp_val_max (TREE_TYPE (max));
608 t = VR_RANGE;
610 else if (is_max)
612 tree one = build_int_cst (TREE_TYPE (min), 1);
613 max = int_const_binop (MINUS_EXPR, min, one);
614 min = vrp_val_min (TREE_TYPE (min));
615 t = VR_RANGE;
619 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
620 if (needs_overflow_infinity (TREE_TYPE (min))
621 && is_overflow_infinity (min)
622 && is_overflow_infinity (max))
624 set_value_range_to_varying (vr);
625 return;
628 set_value_range (vr, t, min, max, equiv);
631 /* Copy value range FROM into value range TO. */
633 static inline void
634 copy_value_range (value_range_t *to, value_range_t *from)
636 set_value_range (to, from->type, from->min, from->max, from->equiv);
639 /* Set value range VR to a single value. This function is only called
640 with values we get from statements, and exists to clear the
641 TREE_OVERFLOW flag so that we don't think we have an overflow
642 infinity when we shouldn't. */
644 static inline void
645 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
647 gcc_assert (is_gimple_min_invariant (val));
648 if (TREE_OVERFLOW_P (val))
649 val = drop_tree_overflow (val);
650 set_value_range (vr, VR_RANGE, val, val, equiv);
653 /* Set value range VR to a non-negative range of type TYPE.
654 OVERFLOW_INFINITY indicates whether to use an overflow infinity
655 rather than TYPE_MAX_VALUE; this should be true if we determine
656 that the range is nonnegative based on the assumption that signed
657 overflow does not occur. */
659 static inline void
660 set_value_range_to_nonnegative (value_range_t *vr, tree type,
661 bool overflow_infinity)
663 tree zero;
665 if (overflow_infinity && !supports_overflow_infinity (type))
667 set_value_range_to_varying (vr);
668 return;
671 zero = build_int_cst (type, 0);
672 set_value_range (vr, VR_RANGE, zero,
673 (overflow_infinity
674 ? positive_overflow_infinity (type)
675 : TYPE_MAX_VALUE (type)),
676 vr->equiv);
679 /* Set value range VR to a non-NULL range of type TYPE. */
681 static inline void
682 set_value_range_to_nonnull (value_range_t *vr, tree type)
684 tree zero = build_int_cst (type, 0);
685 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
689 /* Set value range VR to a NULL range of type TYPE. */
691 static inline void
692 set_value_range_to_null (value_range_t *vr, tree type)
694 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
698 /* Set value range VR to a range of a truthvalue of type TYPE. */
700 static inline void
701 set_value_range_to_truthvalue (value_range_t *vr, tree type)
703 if (TYPE_PRECISION (type) == 1)
704 set_value_range_to_varying (vr);
705 else
706 set_value_range (vr, VR_RANGE,
707 build_int_cst (type, 0), build_int_cst (type, 1),
708 vr->equiv);
712 /* If abs (min) < abs (max), set VR to [-max, max], if
713 abs (min) >= abs (max), set VR to [-min, min]. */
715 static void
716 abs_extent_range (value_range_t *vr, tree min, tree max)
718 int cmp;
720 gcc_assert (TREE_CODE (min) == INTEGER_CST);
721 gcc_assert (TREE_CODE (max) == INTEGER_CST);
722 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
723 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
724 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
725 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
726 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
728 set_value_range_to_varying (vr);
729 return;
731 cmp = compare_values (min, max);
732 if (cmp == -1)
733 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
734 else if (cmp == 0 || cmp == 1)
736 max = min;
737 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
739 else
741 set_value_range_to_varying (vr);
742 return;
744 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
748 /* Return value range information for VAR.
750 If we have no values ranges recorded (ie, VRP is not running), then
751 return NULL. Otherwise create an empty range if none existed for VAR. */
753 static value_range_t *
754 get_value_range (const_tree var)
756 static const struct value_range_d vr_const_varying
757 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
758 value_range_t *vr;
759 tree sym;
760 unsigned ver = SSA_NAME_VERSION (var);
762 /* If we have no recorded ranges, then return NULL. */
763 if (! vr_value)
764 return NULL;
766 /* If we query the range for a new SSA name return an unmodifiable VARYING.
767 We should get here at most from the substitute-and-fold stage which
768 will never try to change values. */
769 if (ver >= num_vr_values)
770 return CONST_CAST (value_range_t *, &vr_const_varying);
772 vr = vr_value[ver];
773 if (vr)
774 return vr;
776 /* After propagation finished do not allocate new value-ranges. */
777 if (values_propagated)
778 return CONST_CAST (value_range_t *, &vr_const_varying);
780 /* Create a default value range. */
781 vr_value[ver] = vr = XCNEW (value_range_t);
783 /* Defer allocating the equivalence set. */
784 vr->equiv = NULL;
786 /* If VAR is a default definition of a parameter, the variable can
787 take any value in VAR's type. */
788 if (SSA_NAME_IS_DEFAULT_DEF (var))
790 sym = SSA_NAME_VAR (var);
791 if (TREE_CODE (sym) == PARM_DECL)
793 /* Try to use the "nonnull" attribute to create ~[0, 0]
794 anti-ranges for pointers. Note that this is only valid with
795 default definitions of PARM_DECLs. */
796 if (POINTER_TYPE_P (TREE_TYPE (sym))
797 && nonnull_arg_p (sym))
798 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
799 else
800 set_value_range_to_varying (vr);
802 else if (TREE_CODE (sym) == RESULT_DECL
803 && DECL_BY_REFERENCE (sym))
804 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
807 return vr;
810 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
812 static inline bool
813 vrp_operand_equal_p (const_tree val1, const_tree val2)
815 if (val1 == val2)
816 return true;
817 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
818 return false;
819 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
822 /* Return true, if the bitmaps B1 and B2 are equal. */
824 static inline bool
825 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
827 return (b1 == b2
828 || ((!b1 || bitmap_empty_p (b1))
829 && (!b2 || bitmap_empty_p (b2)))
830 || (b1 && b2
831 && bitmap_equal_p (b1, b2)));
834 /* Update the value range and equivalence set for variable VAR to
835 NEW_VR. Return true if NEW_VR is different from VAR's previous
836 value.
838 NOTE: This function assumes that NEW_VR is a temporary value range
839 object created for the sole purpose of updating VAR's range. The
840 storage used by the equivalence set from NEW_VR will be freed by
841 this function. Do not call update_value_range when NEW_VR
842 is the range object associated with another SSA name. */
844 static inline bool
845 update_value_range (const_tree var, value_range_t *new_vr)
847 value_range_t *old_vr;
848 bool is_new;
850 /* If there is a value-range on the SSA name from earlier analysis
851 factor that in. */
852 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
854 wide_int min, max;
855 value_range_type rtype = get_range_info (var, &min, &max);
856 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
858 value_range_d nr;
859 nr.type = rtype;
860 nr.min = wide_int_to_tree (TREE_TYPE (var), min);
861 nr.max = wide_int_to_tree (TREE_TYPE (var), max);
862 nr.equiv = NULL;
863 vrp_intersect_ranges (new_vr, &nr);
867 /* Update the value range, if necessary. */
868 old_vr = get_value_range (var);
869 is_new = old_vr->type != new_vr->type
870 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
871 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
872 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
874 if (is_new)
876 /* Do not allow transitions up the lattice. The following
877 is slightly more awkward than just new_vr->type < old_vr->type
878 because VR_RANGE and VR_ANTI_RANGE need to be considered
879 the same. We may not have is_new when transitioning to
880 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
881 called. */
882 if (new_vr->type == VR_UNDEFINED)
884 BITMAP_FREE (new_vr->equiv);
885 set_value_range_to_varying (old_vr);
886 set_value_range_to_varying (new_vr);
887 return true;
889 else
890 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
891 new_vr->equiv);
894 BITMAP_FREE (new_vr->equiv);
896 return is_new;
900 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
901 point where equivalence processing can be turned on/off. */
903 static void
904 add_equivalence (bitmap *equiv, const_tree var)
906 unsigned ver = SSA_NAME_VERSION (var);
907 value_range_t *vr = vr_value[ver];
909 if (*equiv == NULL)
910 *equiv = BITMAP_ALLOC (NULL);
911 bitmap_set_bit (*equiv, ver);
912 if (vr && vr->equiv)
913 bitmap_ior_into (*equiv, vr->equiv);
917 /* Return true if VR is ~[0, 0]. */
919 static inline bool
920 range_is_nonnull (value_range_t *vr)
922 return vr->type == VR_ANTI_RANGE
923 && integer_zerop (vr->min)
924 && integer_zerop (vr->max);
928 /* Return true if VR is [0, 0]. */
930 static inline bool
931 range_is_null (value_range_t *vr)
933 return vr->type == VR_RANGE
934 && integer_zerop (vr->min)
935 && integer_zerop (vr->max);
938 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
939 a singleton. */
941 static inline bool
942 range_int_cst_p (value_range_t *vr)
944 return (vr->type == VR_RANGE
945 && TREE_CODE (vr->max) == INTEGER_CST
946 && TREE_CODE (vr->min) == INTEGER_CST);
949 /* Return true if VR is a INTEGER_CST singleton. */
951 static inline bool
952 range_int_cst_singleton_p (value_range_t *vr)
954 return (range_int_cst_p (vr)
955 && !is_overflow_infinity (vr->min)
956 && !is_overflow_infinity (vr->max)
957 && tree_int_cst_equal (vr->min, vr->max));
960 /* Return true if value range VR involves at least one symbol. */
962 static inline bool
963 symbolic_range_p (value_range_t *vr)
965 return (!is_gimple_min_invariant (vr->min)
966 || !is_gimple_min_invariant (vr->max));
969 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
970 otherwise. We only handle additive operations and set NEG to true if the
971 symbol is negated and INV to the invariant part, if any. */
973 static tree
974 get_single_symbol (tree t, bool *neg, tree *inv)
976 bool neg_;
977 tree inv_;
979 if (TREE_CODE (t) == PLUS_EXPR
980 || TREE_CODE (t) == POINTER_PLUS_EXPR
981 || TREE_CODE (t) == MINUS_EXPR)
983 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
985 neg_ = (TREE_CODE (t) == MINUS_EXPR);
986 inv_ = TREE_OPERAND (t, 0);
987 t = TREE_OPERAND (t, 1);
989 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
991 neg_ = false;
992 inv_ = TREE_OPERAND (t, 1);
993 t = TREE_OPERAND (t, 0);
995 else
996 return NULL_TREE;
998 else
1000 neg_ = false;
1001 inv_ = NULL_TREE;
1004 if (TREE_CODE (t) == NEGATE_EXPR)
1006 t = TREE_OPERAND (t, 0);
1007 neg_ = !neg_;
1010 if (TREE_CODE (t) != SSA_NAME)
1011 return NULL_TREE;
1013 *neg = neg_;
1014 *inv = inv_;
1015 return t;
1018 /* The reverse operation: build a symbolic expression with TYPE
1019 from symbol SYM, negated according to NEG, and invariant INV. */
1021 static tree
1022 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
1024 const bool pointer_p = POINTER_TYPE_P (type);
1025 tree t = sym;
1027 if (neg)
1028 t = build1 (NEGATE_EXPR, type, t);
1030 if (integer_zerop (inv))
1031 return t;
1033 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
1036 /* Return true if value range VR involves exactly one symbol SYM. */
1038 static bool
1039 symbolic_range_based_on_p (value_range_t *vr, const_tree sym)
1041 bool neg, min_has_symbol, max_has_symbol;
1042 tree inv;
1044 if (is_gimple_min_invariant (vr->min))
1045 min_has_symbol = false;
1046 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
1047 min_has_symbol = true;
1048 else
1049 return false;
1051 if (is_gimple_min_invariant (vr->max))
1052 max_has_symbol = false;
1053 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
1054 max_has_symbol = true;
1055 else
1056 return false;
1058 return (min_has_symbol || max_has_symbol);
1061 /* Return true if value range VR uses an overflow infinity. */
1063 static inline bool
1064 overflow_infinity_range_p (value_range_t *vr)
1066 return (vr->type == VR_RANGE
1067 && (is_overflow_infinity (vr->min)
1068 || is_overflow_infinity (vr->max)));
1071 /* Return false if we can not make a valid comparison based on VR;
1072 this will be the case if it uses an overflow infinity and overflow
1073 is not undefined (i.e., -fno-strict-overflow is in effect).
1074 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1075 uses an overflow infinity. */
1077 static bool
1078 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
1080 gcc_assert (vr->type == VR_RANGE);
1081 if (is_overflow_infinity (vr->min))
1083 *strict_overflow_p = true;
1084 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1085 return false;
1087 if (is_overflow_infinity (vr->max))
1089 *strict_overflow_p = true;
1090 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1091 return false;
1093 return true;
1097 /* Return true if the result of assignment STMT is know to be non-negative.
1098 If the return value is based on the assumption that signed overflow is
1099 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1100 *STRICT_OVERFLOW_P.*/
1102 static bool
1103 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1105 enum tree_code code = gimple_assign_rhs_code (stmt);
1106 switch (get_gimple_rhs_class (code))
1108 case GIMPLE_UNARY_RHS:
1109 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1110 gimple_expr_type (stmt),
1111 gimple_assign_rhs1 (stmt),
1112 strict_overflow_p);
1113 case GIMPLE_BINARY_RHS:
1114 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1115 gimple_expr_type (stmt),
1116 gimple_assign_rhs1 (stmt),
1117 gimple_assign_rhs2 (stmt),
1118 strict_overflow_p);
1119 case GIMPLE_TERNARY_RHS:
1120 return false;
1121 case GIMPLE_SINGLE_RHS:
1122 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
1123 strict_overflow_p);
1124 case GIMPLE_INVALID_RHS:
1125 gcc_unreachable ();
1126 default:
1127 gcc_unreachable ();
1131 /* Return true if return value of call STMT is know to be non-negative.
1132 If the return value is based on the assumption that signed overflow is
1133 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1134 *STRICT_OVERFLOW_P.*/
1136 static bool
1137 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1139 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1140 gimple_call_arg (stmt, 0) : NULL_TREE;
1141 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1142 gimple_call_arg (stmt, 1) : NULL_TREE;
1144 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1145 gimple_call_fndecl (stmt),
1146 arg0,
1147 arg1,
1148 strict_overflow_p);
1151 /* Return true if STMT is know to to compute a non-negative value.
1152 If the return value is based on the assumption that signed overflow is
1153 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1154 *STRICT_OVERFLOW_P.*/
1156 static bool
1157 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1159 switch (gimple_code (stmt))
1161 case GIMPLE_ASSIGN:
1162 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1163 case GIMPLE_CALL:
1164 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1165 default:
1166 gcc_unreachable ();
1170 /* Return true if the result of assignment STMT is know to be non-zero.
1171 If the return value is based on the assumption that signed overflow is
1172 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1173 *STRICT_OVERFLOW_P.*/
1175 static bool
1176 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1178 enum tree_code code = gimple_assign_rhs_code (stmt);
1179 switch (get_gimple_rhs_class (code))
1181 case GIMPLE_UNARY_RHS:
1182 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1183 gimple_expr_type (stmt),
1184 gimple_assign_rhs1 (stmt),
1185 strict_overflow_p);
1186 case GIMPLE_BINARY_RHS:
1187 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1188 gimple_expr_type (stmt),
1189 gimple_assign_rhs1 (stmt),
1190 gimple_assign_rhs2 (stmt),
1191 strict_overflow_p);
1192 case GIMPLE_TERNARY_RHS:
1193 return false;
1194 case GIMPLE_SINGLE_RHS:
1195 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1196 strict_overflow_p);
1197 case GIMPLE_INVALID_RHS:
1198 gcc_unreachable ();
1199 default:
1200 gcc_unreachable ();
1204 /* Return true if STMT is known to compute a non-zero value.
1205 If the return value is based on the assumption that signed overflow is
1206 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1207 *STRICT_OVERFLOW_P.*/
1209 static bool
1210 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1212 switch (gimple_code (stmt))
1214 case GIMPLE_ASSIGN:
1215 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1216 case GIMPLE_CALL:
1218 tree fndecl = gimple_call_fndecl (stmt);
1219 if (!fndecl) return false;
1220 if (flag_delete_null_pointer_checks && !flag_check_new
1221 && DECL_IS_OPERATOR_NEW (fndecl)
1222 && !TREE_NOTHROW (fndecl))
1223 return true;
1224 /* References are always non-NULL. */
1225 if (flag_delete_null_pointer_checks
1226 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1227 return true;
1228 if (flag_delete_null_pointer_checks &&
1229 lookup_attribute ("returns_nonnull",
1230 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1231 return true;
1232 return gimple_alloca_call_p (stmt);
1234 default:
1235 gcc_unreachable ();
1239 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1240 obtained so far. */
1242 static bool
1243 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1245 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1246 return true;
1248 /* If we have an expression of the form &X->a, then the expression
1249 is nonnull if X is nonnull. */
1250 if (is_gimple_assign (stmt)
1251 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1253 tree expr = gimple_assign_rhs1 (stmt);
1254 tree base = get_base_address (TREE_OPERAND (expr, 0));
1256 if (base != NULL_TREE
1257 && TREE_CODE (base) == MEM_REF
1258 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1260 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1261 if (range_is_nonnull (vr))
1262 return true;
1266 return false;
1269 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1270 a gimple invariant, or SSA_NAME +- CST. */
1272 static bool
1273 valid_value_p (tree expr)
1275 if (TREE_CODE (expr) == SSA_NAME)
1276 return true;
1278 if (TREE_CODE (expr) == PLUS_EXPR
1279 || TREE_CODE (expr) == MINUS_EXPR)
1280 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1281 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1283 return is_gimple_min_invariant (expr);
1286 /* Return
1287 1 if VAL < VAL2
1288 0 if !(VAL < VAL2)
1289 -2 if those are incomparable. */
1290 static inline int
1291 operand_less_p (tree val, tree val2)
1293 /* LT is folded faster than GE and others. Inline the common case. */
1294 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1295 return tree_int_cst_lt (val, val2);
1296 else
1298 tree tcmp;
1300 fold_defer_overflow_warnings ();
1302 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1304 fold_undefer_and_ignore_overflow_warnings ();
1306 if (!tcmp
1307 || TREE_CODE (tcmp) != INTEGER_CST)
1308 return -2;
1310 if (!integer_zerop (tcmp))
1311 return 1;
1314 /* val >= val2, not considering overflow infinity. */
1315 if (is_negative_overflow_infinity (val))
1316 return is_negative_overflow_infinity (val2) ? 0 : 1;
1317 else if (is_positive_overflow_infinity (val2))
1318 return is_positive_overflow_infinity (val) ? 0 : 1;
1320 return 0;
1323 /* Compare two values VAL1 and VAL2. Return
1325 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1326 -1 if VAL1 < VAL2,
1327 0 if VAL1 == VAL2,
1328 +1 if VAL1 > VAL2, and
1329 +2 if VAL1 != VAL2
1331 This is similar to tree_int_cst_compare but supports pointer values
1332 and values that cannot be compared at compile time.
1334 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1335 true if the return value is only valid if we assume that signed
1336 overflow is undefined. */
1338 static int
1339 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1341 if (val1 == val2)
1342 return 0;
1344 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1345 both integers. */
1346 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1347 == POINTER_TYPE_P (TREE_TYPE (val2)));
1349 /* Convert the two values into the same type. This is needed because
1350 sizetype causes sign extension even for unsigned types. */
1351 val2 = fold_convert (TREE_TYPE (val1), val2);
1352 STRIP_USELESS_TYPE_CONVERSION (val2);
1354 if ((TREE_CODE (val1) == SSA_NAME
1355 || (TREE_CODE (val1) == NEGATE_EXPR
1356 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1357 || TREE_CODE (val1) == PLUS_EXPR
1358 || TREE_CODE (val1) == MINUS_EXPR)
1359 && (TREE_CODE (val2) == SSA_NAME
1360 || (TREE_CODE (val2) == NEGATE_EXPR
1361 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1362 || TREE_CODE (val2) == PLUS_EXPR
1363 || TREE_CODE (val2) == MINUS_EXPR))
1365 tree n1, c1, n2, c2;
1366 enum tree_code code1, code2;
1368 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1369 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1370 same name, return -2. */
1371 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1373 code1 = SSA_NAME;
1374 n1 = val1;
1375 c1 = NULL_TREE;
1377 else
1379 code1 = TREE_CODE (val1);
1380 n1 = TREE_OPERAND (val1, 0);
1381 c1 = TREE_OPERAND (val1, 1);
1382 if (tree_int_cst_sgn (c1) == -1)
1384 if (is_negative_overflow_infinity (c1))
1385 return -2;
1386 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1387 if (!c1)
1388 return -2;
1389 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1393 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1395 code2 = SSA_NAME;
1396 n2 = val2;
1397 c2 = NULL_TREE;
1399 else
1401 code2 = TREE_CODE (val2);
1402 n2 = TREE_OPERAND (val2, 0);
1403 c2 = TREE_OPERAND (val2, 1);
1404 if (tree_int_cst_sgn (c2) == -1)
1406 if (is_negative_overflow_infinity (c2))
1407 return -2;
1408 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1409 if (!c2)
1410 return -2;
1411 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1415 /* Both values must use the same name. */
1416 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1418 n1 = TREE_OPERAND (n1, 0);
1419 n2 = TREE_OPERAND (n2, 0);
1421 if (n1 != n2)
1422 return -2;
1424 if (code1 == SSA_NAME && code2 == SSA_NAME)
1425 /* NAME == NAME */
1426 return 0;
1428 /* If overflow is defined we cannot simplify more. */
1429 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1430 return -2;
1432 if (strict_overflow_p != NULL
1433 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1434 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1435 *strict_overflow_p = true;
1437 if (code1 == SSA_NAME)
1439 if (code2 == PLUS_EXPR)
1440 /* NAME < NAME + CST */
1441 return -1;
1442 else if (code2 == MINUS_EXPR)
1443 /* NAME > NAME - CST */
1444 return 1;
1446 else if (code1 == PLUS_EXPR)
1448 if (code2 == SSA_NAME)
1449 /* NAME + CST > NAME */
1450 return 1;
1451 else if (code2 == PLUS_EXPR)
1452 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1453 return compare_values_warnv (c1, c2, strict_overflow_p);
1454 else if (code2 == MINUS_EXPR)
1455 /* NAME + CST1 > NAME - CST2 */
1456 return 1;
1458 else if (code1 == MINUS_EXPR)
1460 if (code2 == SSA_NAME)
1461 /* NAME - CST < NAME */
1462 return -1;
1463 else if (code2 == PLUS_EXPR)
1464 /* NAME - CST1 < NAME + CST2 */
1465 return -1;
1466 else if (code2 == MINUS_EXPR)
1467 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1468 C1 and C2 are swapped in the call to compare_values. */
1469 return compare_values_warnv (c2, c1, strict_overflow_p);
1472 gcc_unreachable ();
1475 /* We cannot compare non-constants. */
1476 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1477 return -2;
1479 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1481 /* We cannot compare overflowed values, except for overflow
1482 infinities. */
1483 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1485 if (strict_overflow_p != NULL)
1486 *strict_overflow_p = true;
1487 if (is_negative_overflow_infinity (val1))
1488 return is_negative_overflow_infinity (val2) ? 0 : -1;
1489 else if (is_negative_overflow_infinity (val2))
1490 return 1;
1491 else if (is_positive_overflow_infinity (val1))
1492 return is_positive_overflow_infinity (val2) ? 0 : 1;
1493 else if (is_positive_overflow_infinity (val2))
1494 return -1;
1495 return -2;
1498 return tree_int_cst_compare (val1, val2);
1500 else
1502 tree t;
1504 /* First see if VAL1 and VAL2 are not the same. */
1505 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1506 return 0;
1508 /* If VAL1 is a lower address than VAL2, return -1. */
1509 if (operand_less_p (val1, val2) == 1)
1510 return -1;
1512 /* If VAL1 is a higher address than VAL2, return +1. */
1513 if (operand_less_p (val2, val1) == 1)
1514 return 1;
1516 /* If VAL1 is different than VAL2, return +2.
1517 For integer constants we either have already returned -1 or 1
1518 or they are equivalent. We still might succeed in proving
1519 something about non-trivial operands. */
1520 if (TREE_CODE (val1) != INTEGER_CST
1521 || TREE_CODE (val2) != INTEGER_CST)
1523 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1524 if (t && integer_onep (t))
1525 return 2;
1528 return -2;
1532 /* Compare values like compare_values_warnv, but treat comparisons of
1533 nonconstants which rely on undefined overflow as incomparable. */
1535 static int
1536 compare_values (tree val1, tree val2)
1538 bool sop;
1539 int ret;
1541 sop = false;
1542 ret = compare_values_warnv (val1, val2, &sop);
1543 if (sop
1544 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1545 ret = -2;
1546 return ret;
1550 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1551 0 if VAL is not inside [MIN, MAX],
1552 -2 if we cannot tell either way.
1554 Benchmark compile/20001226-1.c compilation time after changing this
1555 function. */
1557 static inline int
1558 value_inside_range (tree val, tree min, tree max)
1560 int cmp1, cmp2;
1562 cmp1 = operand_less_p (val, min);
1563 if (cmp1 == -2)
1564 return -2;
1565 if (cmp1 == 1)
1566 return 0;
1568 cmp2 = operand_less_p (max, val);
1569 if (cmp2 == -2)
1570 return -2;
1572 return !cmp2;
1576 /* Return true if value ranges VR0 and VR1 have a non-empty
1577 intersection.
1579 Benchmark compile/20001226-1.c compilation time after changing this
1580 function.
1583 static inline bool
1584 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1586 /* The value ranges do not intersect if the maximum of the first range is
1587 less than the minimum of the second range or vice versa.
1588 When those relations are unknown, we can't do any better. */
1589 if (operand_less_p (vr0->max, vr1->min) != 0)
1590 return false;
1591 if (operand_less_p (vr1->max, vr0->min) != 0)
1592 return false;
1593 return true;
1597 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1598 include the value zero, -2 if we cannot tell. */
1600 static inline int
1601 range_includes_zero_p (tree min, tree max)
1603 tree zero = build_int_cst (TREE_TYPE (min), 0);
1604 return value_inside_range (zero, min, max);
1607 /* Return true if *VR is know to only contain nonnegative values. */
1609 static inline bool
1610 value_range_nonnegative_p (value_range_t *vr)
1612 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1613 which would return a useful value should be encoded as a
1614 VR_RANGE. */
1615 if (vr->type == VR_RANGE)
1617 int result = compare_values (vr->min, integer_zero_node);
1618 return (result == 0 || result == 1);
1621 return false;
1624 /* If *VR has a value rante that is a single constant value return that,
1625 otherwise return NULL_TREE. */
1627 static tree
1628 value_range_constant_singleton (value_range_t *vr)
1630 if (vr->type == VR_RANGE
1631 && operand_equal_p (vr->min, vr->max, 0)
1632 && is_gimple_min_invariant (vr->min))
1633 return vr->min;
1635 return NULL_TREE;
1638 /* If OP has a value range with a single constant value return that,
1639 otherwise return NULL_TREE. This returns OP itself if OP is a
1640 constant. */
1642 static tree
1643 op_with_constant_singleton_value_range (tree op)
1645 if (is_gimple_min_invariant (op))
1646 return op;
1648 if (TREE_CODE (op) != SSA_NAME)
1649 return NULL_TREE;
1651 return value_range_constant_singleton (get_value_range (op));
1654 /* Return true if op is in a boolean [0, 1] value-range. */
1656 static bool
1657 op_with_boolean_value_range_p (tree op)
1659 value_range_t *vr;
1661 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1662 return true;
1664 if (integer_zerop (op)
1665 || integer_onep (op))
1666 return true;
1668 if (TREE_CODE (op) != SSA_NAME)
1669 return false;
1671 vr = get_value_range (op);
1672 return (vr->type == VR_RANGE
1673 && integer_zerop (vr->min)
1674 && integer_onep (vr->max));
1677 /* Extract value range information from an ASSERT_EXPR EXPR and store
1678 it in *VR_P. */
1680 static void
1681 extract_range_from_assert (value_range_t *vr_p, tree expr)
1683 tree var, cond, limit, min, max, type;
1684 value_range_t *limit_vr;
1685 enum tree_code cond_code;
1687 var = ASSERT_EXPR_VAR (expr);
1688 cond = ASSERT_EXPR_COND (expr);
1690 gcc_assert (COMPARISON_CLASS_P (cond));
1692 /* Find VAR in the ASSERT_EXPR conditional. */
1693 if (var == TREE_OPERAND (cond, 0)
1694 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1695 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1697 /* If the predicate is of the form VAR COMP LIMIT, then we just
1698 take LIMIT from the RHS and use the same comparison code. */
1699 cond_code = TREE_CODE (cond);
1700 limit = TREE_OPERAND (cond, 1);
1701 cond = TREE_OPERAND (cond, 0);
1703 else
1705 /* If the predicate is of the form LIMIT COMP VAR, then we need
1706 to flip around the comparison code to create the proper range
1707 for VAR. */
1708 cond_code = swap_tree_comparison (TREE_CODE (cond));
1709 limit = TREE_OPERAND (cond, 0);
1710 cond = TREE_OPERAND (cond, 1);
1713 limit = avoid_overflow_infinity (limit);
1715 type = TREE_TYPE (var);
1716 gcc_assert (limit != var);
1718 /* For pointer arithmetic, we only keep track of pointer equality
1719 and inequality. */
1720 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1722 set_value_range_to_varying (vr_p);
1723 return;
1726 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1727 try to use LIMIT's range to avoid creating symbolic ranges
1728 unnecessarily. */
1729 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1731 /* LIMIT's range is only interesting if it has any useful information. */
1732 if (limit_vr
1733 && (limit_vr->type == VR_UNDEFINED
1734 || limit_vr->type == VR_VARYING
1735 || symbolic_range_p (limit_vr)))
1736 limit_vr = NULL;
1738 /* Initially, the new range has the same set of equivalences of
1739 VAR's range. This will be revised before returning the final
1740 value. Since assertions may be chained via mutually exclusive
1741 predicates, we will need to trim the set of equivalences before
1742 we are done. */
1743 gcc_assert (vr_p->equiv == NULL);
1744 add_equivalence (&vr_p->equiv, var);
1746 /* Extract a new range based on the asserted comparison for VAR and
1747 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1748 will only use it for equality comparisons (EQ_EXPR). For any
1749 other kind of assertion, we cannot derive a range from LIMIT's
1750 anti-range that can be used to describe the new range. For
1751 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1752 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1753 no single range for x_2 that could describe LE_EXPR, so we might
1754 as well build the range [b_4, +INF] for it.
1755 One special case we handle is extracting a range from a
1756 range test encoded as (unsigned)var + CST <= limit. */
1757 if (TREE_CODE (cond) == NOP_EXPR
1758 || TREE_CODE (cond) == PLUS_EXPR)
1760 if (TREE_CODE (cond) == PLUS_EXPR)
1762 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1763 TREE_OPERAND (cond, 1));
1764 max = int_const_binop (PLUS_EXPR, limit, min);
1765 cond = TREE_OPERAND (cond, 0);
1767 else
1769 min = build_int_cst (TREE_TYPE (var), 0);
1770 max = limit;
1773 /* Make sure to not set TREE_OVERFLOW on the final type
1774 conversion. We are willingly interpreting large positive
1775 unsigned values as negative signed values here. */
1776 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1777 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1779 /* We can transform a max, min range to an anti-range or
1780 vice-versa. Use set_and_canonicalize_value_range which does
1781 this for us. */
1782 if (cond_code == LE_EXPR)
1783 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1784 min, max, vr_p->equiv);
1785 else if (cond_code == GT_EXPR)
1786 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1787 min, max, vr_p->equiv);
1788 else
1789 gcc_unreachable ();
1791 else if (cond_code == EQ_EXPR)
1793 enum value_range_type range_type;
1795 if (limit_vr)
1797 range_type = limit_vr->type;
1798 min = limit_vr->min;
1799 max = limit_vr->max;
1801 else
1803 range_type = VR_RANGE;
1804 min = limit;
1805 max = limit;
1808 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1810 /* When asserting the equality VAR == LIMIT and LIMIT is another
1811 SSA name, the new range will also inherit the equivalence set
1812 from LIMIT. */
1813 if (TREE_CODE (limit) == SSA_NAME)
1814 add_equivalence (&vr_p->equiv, limit);
1816 else if (cond_code == NE_EXPR)
1818 /* As described above, when LIMIT's range is an anti-range and
1819 this assertion is an inequality (NE_EXPR), then we cannot
1820 derive anything from the anti-range. For instance, if
1821 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1822 not imply that VAR's range is [0, 0]. So, in the case of
1823 anti-ranges, we just assert the inequality using LIMIT and
1824 not its anti-range.
1826 If LIMIT_VR is a range, we can only use it to build a new
1827 anti-range if LIMIT_VR is a single-valued range. For
1828 instance, if LIMIT_VR is [0, 1], the predicate
1829 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1830 Rather, it means that for value 0 VAR should be ~[0, 0]
1831 and for value 1, VAR should be ~[1, 1]. We cannot
1832 represent these ranges.
1834 The only situation in which we can build a valid
1835 anti-range is when LIMIT_VR is a single-valued range
1836 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1837 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1838 if (limit_vr
1839 && limit_vr->type == VR_RANGE
1840 && compare_values (limit_vr->min, limit_vr->max) == 0)
1842 min = limit_vr->min;
1843 max = limit_vr->max;
1845 else
1847 /* In any other case, we cannot use LIMIT's range to build a
1848 valid anti-range. */
1849 min = max = limit;
1852 /* If MIN and MAX cover the whole range for their type, then
1853 just use the original LIMIT. */
1854 if (INTEGRAL_TYPE_P (type)
1855 && vrp_val_is_min (min)
1856 && vrp_val_is_max (max))
1857 min = max = limit;
1859 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1860 min, max, vr_p->equiv);
1862 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1864 min = TYPE_MIN_VALUE (type);
1866 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1867 max = limit;
1868 else
1870 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1871 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1872 LT_EXPR. */
1873 max = limit_vr->max;
1876 /* If the maximum value forces us to be out of bounds, simply punt.
1877 It would be pointless to try and do anything more since this
1878 all should be optimized away above us. */
1879 if ((cond_code == LT_EXPR
1880 && compare_values (max, min) == 0)
1881 || is_overflow_infinity (max))
1882 set_value_range_to_varying (vr_p);
1883 else
1885 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1886 if (cond_code == LT_EXPR)
1888 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1889 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1890 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1891 build_int_cst (TREE_TYPE (max), -1));
1892 else
1893 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1894 build_int_cst (TREE_TYPE (max), 1));
1895 if (EXPR_P (max))
1896 TREE_NO_WARNING (max) = 1;
1899 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1902 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1904 max = TYPE_MAX_VALUE (type);
1906 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1907 min = limit;
1908 else
1910 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1911 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1912 GT_EXPR. */
1913 min = limit_vr->min;
1916 /* If the minimum value forces us to be out of bounds, simply punt.
1917 It would be pointless to try and do anything more since this
1918 all should be optimized away above us. */
1919 if ((cond_code == GT_EXPR
1920 && compare_values (min, max) == 0)
1921 || is_overflow_infinity (min))
1922 set_value_range_to_varying (vr_p);
1923 else
1925 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1926 if (cond_code == GT_EXPR)
1928 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1929 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1930 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1931 build_int_cst (TREE_TYPE (min), -1));
1932 else
1933 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1934 build_int_cst (TREE_TYPE (min), 1));
1935 if (EXPR_P (min))
1936 TREE_NO_WARNING (min) = 1;
1939 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1942 else
1943 gcc_unreachable ();
1945 /* Finally intersect the new range with what we already know about var. */
1946 vrp_intersect_ranges (vr_p, get_value_range (var));
1950 /* Extract range information from SSA name VAR and store it in VR. If
1951 VAR has an interesting range, use it. Otherwise, create the
1952 range [VAR, VAR] and return it. This is useful in situations where
1953 we may have conditionals testing values of VARYING names. For
1954 instance,
1956 x_3 = y_5;
1957 if (x_3 > y_5)
1960 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1961 always false. */
1963 static void
1964 extract_range_from_ssa_name (value_range_t *vr, tree var)
1966 value_range_t *var_vr = get_value_range (var);
1968 if (var_vr->type != VR_VARYING)
1969 copy_value_range (vr, var_vr);
1970 else
1971 set_value_range (vr, VR_RANGE, var, var, NULL);
1973 add_equivalence (&vr->equiv, var);
1977 /* Wrapper around int_const_binop. If the operation overflows and we
1978 are not using wrapping arithmetic, then adjust the result to be
1979 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1980 NULL_TREE if we need to use an overflow infinity representation but
1981 the type does not support it. */
1983 static tree
1984 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1986 tree res;
1988 res = int_const_binop (code, val1, val2);
1990 /* If we are using unsigned arithmetic, operate symbolically
1991 on -INF and +INF as int_const_binop only handles signed overflow. */
1992 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1994 int checkz = compare_values (res, val1);
1995 bool overflow = false;
1997 /* Ensure that res = val1 [+*] val2 >= val1
1998 or that res = val1 - val2 <= val1. */
1999 if ((code == PLUS_EXPR
2000 && !(checkz == 1 || checkz == 0))
2001 || (code == MINUS_EXPR
2002 && !(checkz == 0 || checkz == -1)))
2004 overflow = true;
2006 /* Checking for multiplication overflow is done by dividing the
2007 output of the multiplication by the first input of the
2008 multiplication. If the result of that division operation is
2009 not equal to the second input of the multiplication, then the
2010 multiplication overflowed. */
2011 else if (code == MULT_EXPR && !integer_zerop (val1))
2013 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
2014 res,
2015 val1);
2016 int check = compare_values (tmp, val2);
2018 if (check != 0)
2019 overflow = true;
2022 if (overflow)
2024 res = copy_node (res);
2025 TREE_OVERFLOW (res) = 1;
2029 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
2030 /* If the singed operation wraps then int_const_binop has done
2031 everything we want. */
2033 /* Signed division of -1/0 overflows and by the time it gets here
2034 returns NULL_TREE. */
2035 else if (!res)
2036 return NULL_TREE;
2037 else if ((TREE_OVERFLOW (res)
2038 && !TREE_OVERFLOW (val1)
2039 && !TREE_OVERFLOW (val2))
2040 || is_overflow_infinity (val1)
2041 || is_overflow_infinity (val2))
2043 /* If the operation overflowed but neither VAL1 nor VAL2 are
2044 overflown, return -INF or +INF depending on the operation
2045 and the combination of signs of the operands. */
2046 int sgn1 = tree_int_cst_sgn (val1);
2047 int sgn2 = tree_int_cst_sgn (val2);
2049 if (needs_overflow_infinity (TREE_TYPE (res))
2050 && !supports_overflow_infinity (TREE_TYPE (res)))
2051 return NULL_TREE;
2053 /* We have to punt on adding infinities of different signs,
2054 since we can't tell what the sign of the result should be.
2055 Likewise for subtracting infinities of the same sign. */
2056 if (((code == PLUS_EXPR && sgn1 != sgn2)
2057 || (code == MINUS_EXPR && sgn1 == sgn2))
2058 && is_overflow_infinity (val1)
2059 && is_overflow_infinity (val2))
2060 return NULL_TREE;
2062 /* Don't try to handle division or shifting of infinities. */
2063 if ((code == TRUNC_DIV_EXPR
2064 || code == FLOOR_DIV_EXPR
2065 || code == CEIL_DIV_EXPR
2066 || code == EXACT_DIV_EXPR
2067 || code == ROUND_DIV_EXPR
2068 || code == RSHIFT_EXPR)
2069 && (is_overflow_infinity (val1)
2070 || is_overflow_infinity (val2)))
2071 return NULL_TREE;
2073 /* Notice that we only need to handle the restricted set of
2074 operations handled by extract_range_from_binary_expr.
2075 Among them, only multiplication, addition and subtraction
2076 can yield overflow without overflown operands because we
2077 are working with integral types only... except in the
2078 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2079 for division too. */
2081 /* For multiplication, the sign of the overflow is given
2082 by the comparison of the signs of the operands. */
2083 if ((code == MULT_EXPR && sgn1 == sgn2)
2084 /* For addition, the operands must be of the same sign
2085 to yield an overflow. Its sign is therefore that
2086 of one of the operands, for example the first. For
2087 infinite operands X + -INF is negative, not positive. */
2088 || (code == PLUS_EXPR
2089 && (sgn1 >= 0
2090 ? !is_negative_overflow_infinity (val2)
2091 : is_positive_overflow_infinity (val2)))
2092 /* For subtraction, non-infinite operands must be of
2093 different signs to yield an overflow. Its sign is
2094 therefore that of the first operand or the opposite of
2095 that of the second operand. A first operand of 0 counts
2096 as positive here, for the corner case 0 - (-INF), which
2097 overflows, but must yield +INF. For infinite operands 0
2098 - INF is negative, not positive. */
2099 || (code == MINUS_EXPR
2100 && (sgn1 >= 0
2101 ? !is_positive_overflow_infinity (val2)
2102 : is_negative_overflow_infinity (val2)))
2103 /* We only get in here with positive shift count, so the
2104 overflow direction is the same as the sign of val1.
2105 Actually rshift does not overflow at all, but we only
2106 handle the case of shifting overflowed -INF and +INF. */
2107 || (code == RSHIFT_EXPR
2108 && sgn1 >= 0)
2109 /* For division, the only case is -INF / -1 = +INF. */
2110 || code == TRUNC_DIV_EXPR
2111 || code == FLOOR_DIV_EXPR
2112 || code == CEIL_DIV_EXPR
2113 || code == EXACT_DIV_EXPR
2114 || code == ROUND_DIV_EXPR)
2115 return (needs_overflow_infinity (TREE_TYPE (res))
2116 ? positive_overflow_infinity (TREE_TYPE (res))
2117 : TYPE_MAX_VALUE (TREE_TYPE (res)));
2118 else
2119 return (needs_overflow_infinity (TREE_TYPE (res))
2120 ? negative_overflow_infinity (TREE_TYPE (res))
2121 : TYPE_MIN_VALUE (TREE_TYPE (res)));
2124 return res;
2128 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2129 bitmask if some bit is unset, it means for all numbers in the range
2130 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2131 bitmask if some bit is set, it means for all numbers in the range
2132 the bit is 1, otherwise it might be 0 or 1. */
2134 static bool
2135 zero_nonzero_bits_from_vr (const tree expr_type,
2136 value_range_t *vr,
2137 wide_int *may_be_nonzero,
2138 wide_int *must_be_nonzero)
2140 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2141 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2142 if (!range_int_cst_p (vr)
2143 || is_overflow_infinity (vr->min)
2144 || is_overflow_infinity (vr->max))
2145 return false;
2147 if (range_int_cst_singleton_p (vr))
2149 *may_be_nonzero = vr->min;
2150 *must_be_nonzero = *may_be_nonzero;
2152 else if (tree_int_cst_sgn (vr->min) >= 0
2153 || tree_int_cst_sgn (vr->max) < 0)
2155 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2156 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2157 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2158 if (xor_mask != 0)
2160 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2161 may_be_nonzero->get_precision ());
2162 *may_be_nonzero = *may_be_nonzero | mask;
2163 *must_be_nonzero = must_be_nonzero->and_not (mask);
2167 return true;
2170 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2171 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2172 false otherwise. If *AR can be represented with a single range
2173 *VR1 will be VR_UNDEFINED. */
2175 static bool
2176 ranges_from_anti_range (value_range_t *ar,
2177 value_range_t *vr0, value_range_t *vr1)
2179 tree type = TREE_TYPE (ar->min);
2181 vr0->type = VR_UNDEFINED;
2182 vr1->type = VR_UNDEFINED;
2184 if (ar->type != VR_ANTI_RANGE
2185 || TREE_CODE (ar->min) != INTEGER_CST
2186 || TREE_CODE (ar->max) != INTEGER_CST
2187 || !vrp_val_min (type)
2188 || !vrp_val_max (type))
2189 return false;
2191 if (!vrp_val_is_min (ar->min))
2193 vr0->type = VR_RANGE;
2194 vr0->min = vrp_val_min (type);
2195 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2197 if (!vrp_val_is_max (ar->max))
2199 vr1->type = VR_RANGE;
2200 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2201 vr1->max = vrp_val_max (type);
2203 if (vr0->type == VR_UNDEFINED)
2205 *vr0 = *vr1;
2206 vr1->type = VR_UNDEFINED;
2209 return vr0->type != VR_UNDEFINED;
2212 /* Helper to extract a value-range *VR for a multiplicative operation
2213 *VR0 CODE *VR1. */
2215 static void
2216 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2217 enum tree_code code,
2218 value_range_t *vr0, value_range_t *vr1)
2220 enum value_range_type type;
2221 tree val[4];
2222 size_t i;
2223 tree min, max;
2224 bool sop;
2225 int cmp;
2227 /* Multiplications, divisions and shifts are a bit tricky to handle,
2228 depending on the mix of signs we have in the two ranges, we
2229 need to operate on different values to get the minimum and
2230 maximum values for the new range. One approach is to figure
2231 out all the variations of range combinations and do the
2232 operations.
2234 However, this involves several calls to compare_values and it
2235 is pretty convoluted. It's simpler to do the 4 operations
2236 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2237 MAX1) and then figure the smallest and largest values to form
2238 the new range. */
2239 gcc_assert (code == MULT_EXPR
2240 || code == TRUNC_DIV_EXPR
2241 || code == FLOOR_DIV_EXPR
2242 || code == CEIL_DIV_EXPR
2243 || code == EXACT_DIV_EXPR
2244 || code == ROUND_DIV_EXPR
2245 || code == RSHIFT_EXPR
2246 || code == LSHIFT_EXPR);
2247 gcc_assert ((vr0->type == VR_RANGE
2248 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2249 && vr0->type == vr1->type);
2251 type = vr0->type;
2253 /* Compute the 4 cross operations. */
2254 sop = false;
2255 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2256 if (val[0] == NULL_TREE)
2257 sop = true;
2259 if (vr1->max == vr1->min)
2260 val[1] = NULL_TREE;
2261 else
2263 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2264 if (val[1] == NULL_TREE)
2265 sop = true;
2268 if (vr0->max == vr0->min)
2269 val[2] = NULL_TREE;
2270 else
2272 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2273 if (val[2] == NULL_TREE)
2274 sop = true;
2277 if (vr0->min == vr0->max || vr1->min == vr1->max)
2278 val[3] = NULL_TREE;
2279 else
2281 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2282 if (val[3] == NULL_TREE)
2283 sop = true;
2286 if (sop)
2288 set_value_range_to_varying (vr);
2289 return;
2292 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2293 of VAL[i]. */
2294 min = val[0];
2295 max = val[0];
2296 for (i = 1; i < 4; i++)
2298 if (!is_gimple_min_invariant (min)
2299 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2300 || !is_gimple_min_invariant (max)
2301 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2302 break;
2304 if (val[i])
2306 if (!is_gimple_min_invariant (val[i])
2307 || (TREE_OVERFLOW (val[i])
2308 && !is_overflow_infinity (val[i])))
2310 /* If we found an overflowed value, set MIN and MAX
2311 to it so that we set the resulting range to
2312 VARYING. */
2313 min = max = val[i];
2314 break;
2317 if (compare_values (val[i], min) == -1)
2318 min = val[i];
2320 if (compare_values (val[i], max) == 1)
2321 max = val[i];
2325 /* If either MIN or MAX overflowed, then set the resulting range to
2326 VARYING. But we do accept an overflow infinity
2327 representation. */
2328 if (min == NULL_TREE
2329 || !is_gimple_min_invariant (min)
2330 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2331 || max == NULL_TREE
2332 || !is_gimple_min_invariant (max)
2333 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2335 set_value_range_to_varying (vr);
2336 return;
2339 /* We punt if:
2340 1) [-INF, +INF]
2341 2) [-INF, +-INF(OVF)]
2342 3) [+-INF(OVF), +INF]
2343 4) [+-INF(OVF), +-INF(OVF)]
2344 We learn nothing when we have INF and INF(OVF) on both sides.
2345 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2346 overflow. */
2347 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2348 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2350 set_value_range_to_varying (vr);
2351 return;
2354 cmp = compare_values (min, max);
2355 if (cmp == -2 || cmp == 1)
2357 /* If the new range has its limits swapped around (MIN > MAX),
2358 then the operation caused one of them to wrap around, mark
2359 the new range VARYING. */
2360 set_value_range_to_varying (vr);
2362 else
2363 set_value_range (vr, type, min, max, NULL);
2366 /* Extract range information from a binary operation CODE based on
2367 the ranges of each of its operands *VR0 and *VR1 with resulting
2368 type EXPR_TYPE. The resulting range is stored in *VR. */
2370 static void
2371 extract_range_from_binary_expr_1 (value_range_t *vr,
2372 enum tree_code code, tree expr_type,
2373 value_range_t *vr0_, value_range_t *vr1_)
2375 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2376 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2377 enum value_range_type type;
2378 tree min = NULL_TREE, max = NULL_TREE;
2379 int cmp;
2381 if (!INTEGRAL_TYPE_P (expr_type)
2382 && !POINTER_TYPE_P (expr_type))
2384 set_value_range_to_varying (vr);
2385 return;
2388 /* Not all binary expressions can be applied to ranges in a
2389 meaningful way. Handle only arithmetic operations. */
2390 if (code != PLUS_EXPR
2391 && code != MINUS_EXPR
2392 && code != POINTER_PLUS_EXPR
2393 && code != MULT_EXPR
2394 && code != TRUNC_DIV_EXPR
2395 && code != FLOOR_DIV_EXPR
2396 && code != CEIL_DIV_EXPR
2397 && code != EXACT_DIV_EXPR
2398 && code != ROUND_DIV_EXPR
2399 && code != TRUNC_MOD_EXPR
2400 && code != RSHIFT_EXPR
2401 && code != LSHIFT_EXPR
2402 && code != MIN_EXPR
2403 && code != MAX_EXPR
2404 && code != BIT_AND_EXPR
2405 && code != BIT_IOR_EXPR
2406 && code != BIT_XOR_EXPR)
2408 set_value_range_to_varying (vr);
2409 return;
2412 /* If both ranges are UNDEFINED, so is the result. */
2413 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2415 set_value_range_to_undefined (vr);
2416 return;
2418 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2419 code. At some point we may want to special-case operations that
2420 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2421 operand. */
2422 else if (vr0.type == VR_UNDEFINED)
2423 set_value_range_to_varying (&vr0);
2424 else if (vr1.type == VR_UNDEFINED)
2425 set_value_range_to_varying (&vr1);
2427 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2428 and express ~[] op X as ([]' op X) U ([]'' op X). */
2429 if (vr0.type == VR_ANTI_RANGE
2430 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2432 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2433 if (vrtem1.type != VR_UNDEFINED)
2435 value_range_t vrres = VR_INITIALIZER;
2436 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2437 &vrtem1, vr1_);
2438 vrp_meet (vr, &vrres);
2440 return;
2442 /* Likewise for X op ~[]. */
2443 if (vr1.type == VR_ANTI_RANGE
2444 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2446 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2447 if (vrtem1.type != VR_UNDEFINED)
2449 value_range_t vrres = VR_INITIALIZER;
2450 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2451 vr0_, &vrtem1);
2452 vrp_meet (vr, &vrres);
2454 return;
2457 /* The type of the resulting value range defaults to VR0.TYPE. */
2458 type = vr0.type;
2460 /* Refuse to operate on VARYING ranges, ranges of different kinds
2461 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2462 because we may be able to derive a useful range even if one of
2463 the operands is VR_VARYING or symbolic range. Similarly for
2464 divisions, MIN/MAX and PLUS/MINUS.
2466 TODO, we may be able to derive anti-ranges in some cases. */
2467 if (code != BIT_AND_EXPR
2468 && code != BIT_IOR_EXPR
2469 && code != TRUNC_DIV_EXPR
2470 && code != FLOOR_DIV_EXPR
2471 && code != CEIL_DIV_EXPR
2472 && code != EXACT_DIV_EXPR
2473 && code != ROUND_DIV_EXPR
2474 && code != TRUNC_MOD_EXPR
2475 && code != MIN_EXPR
2476 && code != MAX_EXPR
2477 && code != PLUS_EXPR
2478 && code != MINUS_EXPR
2479 && code != RSHIFT_EXPR
2480 && (vr0.type == VR_VARYING
2481 || vr1.type == VR_VARYING
2482 || vr0.type != vr1.type
2483 || symbolic_range_p (&vr0)
2484 || symbolic_range_p (&vr1)))
2486 set_value_range_to_varying (vr);
2487 return;
2490 /* Now evaluate the expression to determine the new range. */
2491 if (POINTER_TYPE_P (expr_type))
2493 if (code == MIN_EXPR || code == MAX_EXPR)
2495 /* For MIN/MAX expressions with pointers, we only care about
2496 nullness, if both are non null, then the result is nonnull.
2497 If both are null, then the result is null. Otherwise they
2498 are varying. */
2499 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2500 set_value_range_to_nonnull (vr, expr_type);
2501 else if (range_is_null (&vr0) && range_is_null (&vr1))
2502 set_value_range_to_null (vr, expr_type);
2503 else
2504 set_value_range_to_varying (vr);
2506 else if (code == POINTER_PLUS_EXPR)
2508 /* For pointer types, we are really only interested in asserting
2509 whether the expression evaluates to non-NULL. */
2510 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2511 set_value_range_to_nonnull (vr, expr_type);
2512 else if (range_is_null (&vr0) && range_is_null (&vr1))
2513 set_value_range_to_null (vr, expr_type);
2514 else
2515 set_value_range_to_varying (vr);
2517 else if (code == BIT_AND_EXPR)
2519 /* For pointer types, we are really only interested in asserting
2520 whether the expression evaluates to non-NULL. */
2521 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2522 set_value_range_to_nonnull (vr, expr_type);
2523 else if (range_is_null (&vr0) || range_is_null (&vr1))
2524 set_value_range_to_null (vr, expr_type);
2525 else
2526 set_value_range_to_varying (vr);
2528 else
2529 set_value_range_to_varying (vr);
2531 return;
2534 /* For integer ranges, apply the operation to each end of the
2535 range and see what we end up with. */
2536 if (code == PLUS_EXPR || code == MINUS_EXPR)
2538 const bool minus_p = (code == MINUS_EXPR);
2539 tree min_op0 = vr0.min;
2540 tree min_op1 = minus_p ? vr1.max : vr1.min;
2541 tree max_op0 = vr0.max;
2542 tree max_op1 = minus_p ? vr1.min : vr1.max;
2543 tree sym_min_op0 = NULL_TREE;
2544 tree sym_min_op1 = NULL_TREE;
2545 tree sym_max_op0 = NULL_TREE;
2546 tree sym_max_op1 = NULL_TREE;
2547 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2549 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2550 single-symbolic ranges, try to compute the precise resulting range,
2551 but only if we know that this resulting range will also be constant
2552 or single-symbolic. */
2553 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2554 && (TREE_CODE (min_op0) == INTEGER_CST
2555 || (sym_min_op0
2556 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2557 && (TREE_CODE (min_op1) == INTEGER_CST
2558 || (sym_min_op1
2559 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2560 && (!(sym_min_op0 && sym_min_op1)
2561 || (sym_min_op0 == sym_min_op1
2562 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2563 && (TREE_CODE (max_op0) == INTEGER_CST
2564 || (sym_max_op0
2565 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2566 && (TREE_CODE (max_op1) == INTEGER_CST
2567 || (sym_max_op1
2568 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2569 && (!(sym_max_op0 && sym_max_op1)
2570 || (sym_max_op0 == sym_max_op1
2571 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2573 const signop sgn = TYPE_SIGN (expr_type);
2574 const unsigned int prec = TYPE_PRECISION (expr_type);
2575 wide_int type_min, type_max, wmin, wmax;
2576 int min_ovf = 0;
2577 int max_ovf = 0;
2579 /* Get the lower and upper bounds of the type. */
2580 if (TYPE_OVERFLOW_WRAPS (expr_type))
2582 type_min = wi::min_value (prec, sgn);
2583 type_max = wi::max_value (prec, sgn);
2585 else
2587 type_min = vrp_val_min (expr_type);
2588 type_max = vrp_val_max (expr_type);
2591 /* Combine the lower bounds, if any. */
2592 if (min_op0 && min_op1)
2594 if (minus_p)
2596 wmin = wi::sub (min_op0, min_op1);
2598 /* Check for overflow. */
2599 if (wi::cmp (0, min_op1, sgn)
2600 != wi::cmp (wmin, min_op0, sgn))
2601 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2603 else
2605 wmin = wi::add (min_op0, min_op1);
2607 /* Check for overflow. */
2608 if (wi::cmp (min_op1, 0, sgn)
2609 != wi::cmp (wmin, min_op0, sgn))
2610 min_ovf = wi::cmp (min_op0, wmin, sgn);
2613 else if (min_op0)
2614 wmin = min_op0;
2615 else if (min_op1)
2616 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2617 else
2618 wmin = wi::shwi (0, prec);
2620 /* Combine the upper bounds, if any. */
2621 if (max_op0 && max_op1)
2623 if (minus_p)
2625 wmax = wi::sub (max_op0, max_op1);
2627 /* Check for overflow. */
2628 if (wi::cmp (0, max_op1, sgn)
2629 != wi::cmp (wmax, max_op0, sgn))
2630 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2632 else
2634 wmax = wi::add (max_op0, max_op1);
2636 if (wi::cmp (max_op1, 0, sgn)
2637 != wi::cmp (wmax, max_op0, sgn))
2638 max_ovf = wi::cmp (max_op0, wmax, sgn);
2641 else if (max_op0)
2642 wmax = max_op0;
2643 else if (max_op1)
2644 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2645 else
2646 wmax = wi::shwi (0, prec);
2648 /* Check for type overflow. */
2649 if (min_ovf == 0)
2651 if (wi::cmp (wmin, type_min, sgn) == -1)
2652 min_ovf = -1;
2653 else if (wi::cmp (wmin, type_max, sgn) == 1)
2654 min_ovf = 1;
2656 if (max_ovf == 0)
2658 if (wi::cmp (wmax, type_min, sgn) == -1)
2659 max_ovf = -1;
2660 else if (wi::cmp (wmax, type_max, sgn) == 1)
2661 max_ovf = 1;
2664 /* If we have overflow for the constant part and the resulting
2665 range will be symbolic, drop to VR_VARYING. */
2666 if ((min_ovf && sym_min_op0 != sym_min_op1)
2667 || (max_ovf && sym_max_op0 != sym_max_op1))
2669 set_value_range_to_varying (vr);
2670 return;
2673 if (TYPE_OVERFLOW_WRAPS (expr_type))
2675 /* If overflow wraps, truncate the values and adjust the
2676 range kind and bounds appropriately. */
2677 wide_int tmin = wide_int::from (wmin, prec, sgn);
2678 wide_int tmax = wide_int::from (wmax, prec, sgn);
2679 if (min_ovf == max_ovf)
2681 /* No overflow or both overflow or underflow. The
2682 range kind stays VR_RANGE. */
2683 min = wide_int_to_tree (expr_type, tmin);
2684 max = wide_int_to_tree (expr_type, tmax);
2686 else if (min_ovf == -1 && max_ovf == 1)
2688 /* Underflow and overflow, drop to VR_VARYING. */
2689 set_value_range_to_varying (vr);
2690 return;
2692 else
2694 /* Min underflow or max overflow. The range kind
2695 changes to VR_ANTI_RANGE. */
2696 bool covers = false;
2697 wide_int tem = tmin;
2698 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2699 || (max_ovf == 1 && min_ovf == 0));
2700 type = VR_ANTI_RANGE;
2701 tmin = tmax + 1;
2702 if (wi::cmp (tmin, tmax, sgn) < 0)
2703 covers = true;
2704 tmax = tem - 1;
2705 if (wi::cmp (tmax, tem, sgn) > 0)
2706 covers = true;
2707 /* If the anti-range would cover nothing, drop to varying.
2708 Likewise if the anti-range bounds are outside of the
2709 types values. */
2710 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2712 set_value_range_to_varying (vr);
2713 return;
2715 min = wide_int_to_tree (expr_type, tmin);
2716 max = wide_int_to_tree (expr_type, tmax);
2719 else
2721 /* If overflow does not wrap, saturate to the types min/max
2722 value. */
2723 if (min_ovf == -1)
2725 if (needs_overflow_infinity (expr_type)
2726 && supports_overflow_infinity (expr_type))
2727 min = negative_overflow_infinity (expr_type);
2728 else
2729 min = wide_int_to_tree (expr_type, type_min);
2731 else if (min_ovf == 1)
2733 if (needs_overflow_infinity (expr_type)
2734 && supports_overflow_infinity (expr_type))
2735 min = positive_overflow_infinity (expr_type);
2736 else
2737 min = wide_int_to_tree (expr_type, type_max);
2739 else
2740 min = wide_int_to_tree (expr_type, wmin);
2742 if (max_ovf == -1)
2744 if (needs_overflow_infinity (expr_type)
2745 && supports_overflow_infinity (expr_type))
2746 max = negative_overflow_infinity (expr_type);
2747 else
2748 max = wide_int_to_tree (expr_type, type_min);
2750 else if (max_ovf == 1)
2752 if (needs_overflow_infinity (expr_type)
2753 && supports_overflow_infinity (expr_type))
2754 max = positive_overflow_infinity (expr_type);
2755 else
2756 max = wide_int_to_tree (expr_type, type_max);
2758 else
2759 max = wide_int_to_tree (expr_type, wmax);
2762 if (needs_overflow_infinity (expr_type)
2763 && supports_overflow_infinity (expr_type))
2765 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2766 || (min_op1
2767 && (minus_p
2768 ? is_positive_overflow_infinity (min_op1)
2769 : is_negative_overflow_infinity (min_op1))))
2770 min = negative_overflow_infinity (expr_type);
2771 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2772 || (max_op1
2773 && (minus_p
2774 ? is_negative_overflow_infinity (max_op1)
2775 : is_positive_overflow_infinity (max_op1))))
2776 max = positive_overflow_infinity (expr_type);
2779 /* If the result lower bound is constant, we're done;
2780 otherwise, build the symbolic lower bound. */
2781 if (sym_min_op0 == sym_min_op1)
2783 else if (sym_min_op0)
2784 min = build_symbolic_expr (expr_type, sym_min_op0,
2785 neg_min_op0, min);
2786 else if (sym_min_op1)
2787 min = build_symbolic_expr (expr_type, sym_min_op1,
2788 neg_min_op1 ^ minus_p, min);
2790 /* Likewise for the upper bound. */
2791 if (sym_max_op0 == sym_max_op1)
2793 else if (sym_max_op0)
2794 max = build_symbolic_expr (expr_type, sym_max_op0,
2795 neg_max_op0, max);
2796 else if (sym_max_op1)
2797 max = build_symbolic_expr (expr_type, sym_max_op1,
2798 neg_max_op1 ^ minus_p, max);
2800 else
2802 /* For other cases, for example if we have a PLUS_EXPR with two
2803 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2804 to compute a precise range for such a case.
2805 ??? General even mixed range kind operations can be expressed
2806 by for example transforming ~[3, 5] + [1, 2] to range-only
2807 operations and a union primitive:
2808 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2809 [-INF+1, 4] U [6, +INF(OVF)]
2810 though usually the union is not exactly representable with
2811 a single range or anti-range as the above is
2812 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2813 but one could use a scheme similar to equivalences for this. */
2814 set_value_range_to_varying (vr);
2815 return;
2818 else if (code == MIN_EXPR
2819 || code == MAX_EXPR)
2821 if (vr0.type == VR_RANGE
2822 && !symbolic_range_p (&vr0))
2824 type = VR_RANGE;
2825 if (vr1.type == VR_RANGE
2826 && !symbolic_range_p (&vr1))
2828 /* For operations that make the resulting range directly
2829 proportional to the original ranges, apply the operation to
2830 the same end of each range. */
2831 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2832 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2834 else if (code == MIN_EXPR)
2836 min = vrp_val_min (expr_type);
2837 max = vr0.max;
2839 else if (code == MAX_EXPR)
2841 min = vr0.min;
2842 max = vrp_val_max (expr_type);
2845 else if (vr1.type == VR_RANGE
2846 && !symbolic_range_p (&vr1))
2848 type = VR_RANGE;
2849 if (code == MIN_EXPR)
2851 min = vrp_val_min (expr_type);
2852 max = vr1.max;
2854 else if (code == MAX_EXPR)
2856 min = vr1.min;
2857 max = vrp_val_max (expr_type);
2860 else
2862 set_value_range_to_varying (vr);
2863 return;
2866 else if (code == MULT_EXPR)
2868 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2869 drop to varying. This test requires 2*prec bits if both
2870 operands are signed and 2*prec + 2 bits if either is not. */
2872 signop sign = TYPE_SIGN (expr_type);
2873 unsigned int prec = TYPE_PRECISION (expr_type);
2875 if (range_int_cst_p (&vr0)
2876 && range_int_cst_p (&vr1)
2877 && TYPE_OVERFLOW_WRAPS (expr_type))
2879 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2880 typedef generic_wide_int
2881 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2882 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2883 vrp_int size = sizem1 + 1;
2885 /* Extend the values using the sign of the result to PREC2.
2886 From here on out, everthing is just signed math no matter
2887 what the input types were. */
2888 vrp_int min0 = vrp_int_cst (vr0.min);
2889 vrp_int max0 = vrp_int_cst (vr0.max);
2890 vrp_int min1 = vrp_int_cst (vr1.min);
2891 vrp_int max1 = vrp_int_cst (vr1.max);
2892 /* Canonicalize the intervals. */
2893 if (sign == UNSIGNED)
2895 if (wi::ltu_p (size, min0 + max0))
2897 min0 -= size;
2898 max0 -= size;
2901 if (wi::ltu_p (size, min1 + max1))
2903 min1 -= size;
2904 max1 -= size;
2908 vrp_int prod0 = min0 * min1;
2909 vrp_int prod1 = min0 * max1;
2910 vrp_int prod2 = max0 * min1;
2911 vrp_int prod3 = max0 * max1;
2913 /* Sort the 4 products so that min is in prod0 and max is in
2914 prod3. */
2915 /* min0min1 > max0max1 */
2916 if (wi::gts_p (prod0, prod3))
2917 std::swap (prod0, prod3);
2919 /* min0max1 > max0min1 */
2920 if (wi::gts_p (prod1, prod2))
2921 std::swap (prod1, prod2);
2923 if (wi::gts_p (prod0, prod1))
2924 std::swap (prod0, prod1);
2926 if (wi::gts_p (prod2, prod3))
2927 std::swap (prod2, prod3);
2929 /* diff = max - min. */
2930 prod2 = prod3 - prod0;
2931 if (wi::geu_p (prod2, sizem1))
2933 /* the range covers all values. */
2934 set_value_range_to_varying (vr);
2935 return;
2938 /* The following should handle the wrapping and selecting
2939 VR_ANTI_RANGE for us. */
2940 min = wide_int_to_tree (expr_type, prod0);
2941 max = wide_int_to_tree (expr_type, prod3);
2942 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2943 return;
2946 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2947 drop to VR_VARYING. It would take more effort to compute a
2948 precise range for such a case. For example, if we have
2949 op0 == 65536 and op1 == 65536 with their ranges both being
2950 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2951 we cannot claim that the product is in ~[0,0]. Note that we
2952 are guaranteed to have vr0.type == vr1.type at this
2953 point. */
2954 if (vr0.type == VR_ANTI_RANGE
2955 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2957 set_value_range_to_varying (vr);
2958 return;
2961 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2962 return;
2964 else if (code == RSHIFT_EXPR
2965 || code == LSHIFT_EXPR)
2967 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2968 then drop to VR_VARYING. Outside of this range we get undefined
2969 behavior from the shift operation. We cannot even trust
2970 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2971 shifts, and the operation at the tree level may be widened. */
2972 if (range_int_cst_p (&vr1)
2973 && compare_tree_int (vr1.min, 0) >= 0
2974 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2976 if (code == RSHIFT_EXPR)
2978 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2979 useful ranges just from the shift count. E.g.
2980 x >> 63 for signed 64-bit x is always [-1, 0]. */
2981 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2983 vr0.type = type = VR_RANGE;
2984 vr0.min = vrp_val_min (expr_type);
2985 vr0.max = vrp_val_max (expr_type);
2987 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2988 return;
2990 /* We can map lshifts by constants to MULT_EXPR handling. */
2991 else if (code == LSHIFT_EXPR
2992 && range_int_cst_singleton_p (&vr1))
2994 bool saved_flag_wrapv;
2995 value_range_t vr1p = VR_INITIALIZER;
2996 vr1p.type = VR_RANGE;
2997 vr1p.min = (wide_int_to_tree
2998 (expr_type,
2999 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
3000 TYPE_PRECISION (expr_type))));
3001 vr1p.max = vr1p.min;
3002 /* We have to use a wrapping multiply though as signed overflow
3003 on lshifts is implementation defined in C89. */
3004 saved_flag_wrapv = flag_wrapv;
3005 flag_wrapv = 1;
3006 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
3007 &vr0, &vr1p);
3008 flag_wrapv = saved_flag_wrapv;
3009 return;
3011 else if (code == LSHIFT_EXPR
3012 && range_int_cst_p (&vr0))
3014 int prec = TYPE_PRECISION (expr_type);
3015 int overflow_pos = prec;
3016 int bound_shift;
3017 wide_int low_bound, high_bound;
3018 bool uns = TYPE_UNSIGNED (expr_type);
3019 bool in_bounds = false;
3021 if (!uns)
3022 overflow_pos -= 1;
3024 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
3025 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
3026 overflow. However, for that to happen, vr1.max needs to be
3027 zero, which means vr1 is a singleton range of zero, which
3028 means it should be handled by the previous LSHIFT_EXPR
3029 if-clause. */
3030 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
3031 wide_int complement = ~(bound - 1);
3033 if (uns)
3035 low_bound = bound;
3036 high_bound = complement;
3037 if (wi::ltu_p (vr0.max, low_bound))
3039 /* [5, 6] << [1, 2] == [10, 24]. */
3040 /* We're shifting out only zeroes, the value increases
3041 monotonically. */
3042 in_bounds = true;
3044 else if (wi::ltu_p (high_bound, vr0.min))
3046 /* [0xffffff00, 0xffffffff] << [1, 2]
3047 == [0xfffffc00, 0xfffffffe]. */
3048 /* We're shifting out only ones, the value decreases
3049 monotonically. */
3050 in_bounds = true;
3053 else
3055 /* [-1, 1] << [1, 2] == [-4, 4]. */
3056 low_bound = complement;
3057 high_bound = bound;
3058 if (wi::lts_p (vr0.max, high_bound)
3059 && wi::lts_p (low_bound, vr0.min))
3061 /* For non-negative numbers, we're shifting out only
3062 zeroes, the value increases monotonically.
3063 For negative numbers, we're shifting out only ones, the
3064 value decreases monotomically. */
3065 in_bounds = true;
3069 if (in_bounds)
3071 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3072 return;
3076 set_value_range_to_varying (vr);
3077 return;
3079 else if (code == TRUNC_DIV_EXPR
3080 || code == FLOOR_DIV_EXPR
3081 || code == CEIL_DIV_EXPR
3082 || code == EXACT_DIV_EXPR
3083 || code == ROUND_DIV_EXPR)
3085 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3087 /* For division, if op1 has VR_RANGE but op0 does not, something
3088 can be deduced just from that range. Say [min, max] / [4, max]
3089 gives [min / 4, max / 4] range. */
3090 if (vr1.type == VR_RANGE
3091 && !symbolic_range_p (&vr1)
3092 && range_includes_zero_p (vr1.min, vr1.max) == 0)
3094 vr0.type = type = VR_RANGE;
3095 vr0.min = vrp_val_min (expr_type);
3096 vr0.max = vrp_val_max (expr_type);
3098 else
3100 set_value_range_to_varying (vr);
3101 return;
3105 /* For divisions, if flag_non_call_exceptions is true, we must
3106 not eliminate a division by zero. */
3107 if (cfun->can_throw_non_call_exceptions
3108 && (vr1.type != VR_RANGE
3109 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3111 set_value_range_to_varying (vr);
3112 return;
3115 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3116 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3117 include 0. */
3118 if (vr0.type == VR_RANGE
3119 && (vr1.type != VR_RANGE
3120 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3122 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
3123 int cmp;
3125 min = NULL_TREE;
3126 max = NULL_TREE;
3127 if (TYPE_UNSIGNED (expr_type)
3128 || value_range_nonnegative_p (&vr1))
3130 /* For unsigned division or when divisor is known
3131 to be non-negative, the range has to cover
3132 all numbers from 0 to max for positive max
3133 and all numbers from min to 0 for negative min. */
3134 cmp = compare_values (vr0.max, zero);
3135 if (cmp == -1)
3137 /* When vr0.max < 0, vr1.min != 0 and value
3138 ranges for dividend and divisor are available. */
3139 if (vr1.type == VR_RANGE
3140 && !symbolic_range_p (&vr0)
3141 && !symbolic_range_p (&vr1)
3142 && !compare_values (vr1.min, zero))
3143 max = int_const_binop (code, vr0.max, vr1.min);
3144 else
3145 max = zero;
3147 else if (cmp == 0 || cmp == 1)
3148 max = vr0.max;
3149 else
3150 type = VR_VARYING;
3151 cmp = compare_values (vr0.min, zero);
3152 if (cmp == 1)
3154 /* For unsigned division when value ranges for dividend
3155 and divisor are available. */
3156 if (vr1.type == VR_RANGE
3157 && !symbolic_range_p (&vr0)
3158 && !symbolic_range_p (&vr1))
3159 min = int_const_binop (code, vr0.min, vr1.max);
3160 else
3161 min = zero;
3163 else if (cmp == 0 || cmp == -1)
3164 min = vr0.min;
3165 else
3166 type = VR_VARYING;
3168 else
3170 /* Otherwise the range is -max .. max or min .. -min
3171 depending on which bound is bigger in absolute value,
3172 as the division can change the sign. */
3173 abs_extent_range (vr, vr0.min, vr0.max);
3174 return;
3176 if (type == VR_VARYING)
3178 set_value_range_to_varying (vr);
3179 return;
3182 else
3184 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3185 return;
3188 else if (code == TRUNC_MOD_EXPR)
3190 if (range_is_null (&vr1))
3192 set_value_range_to_undefined (vr);
3193 return;
3195 /* ABS (A % B) < ABS (B) and either
3196 0 <= A % B <= A or A <= A % B <= 0. */
3197 type = VR_RANGE;
3198 signop sgn = TYPE_SIGN (expr_type);
3199 unsigned int prec = TYPE_PRECISION (expr_type);
3200 wide_int wmin, wmax, tmp;
3201 wide_int zero = wi::zero (prec);
3202 wide_int one = wi::one (prec);
3203 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
3205 wmax = wi::sub (vr1.max, one);
3206 if (sgn == SIGNED)
3208 tmp = wi::sub (wi::minus_one (prec), vr1.min);
3209 wmax = wi::smax (wmax, tmp);
3212 else
3214 wmax = wi::max_value (prec, sgn);
3215 /* X % INT_MIN may be INT_MAX. */
3216 if (sgn == UNSIGNED)
3217 wmax = wmax - one;
3220 if (sgn == UNSIGNED)
3221 wmin = zero;
3222 else
3224 wmin = -wmax;
3225 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3227 tmp = vr0.min;
3228 if (wi::gts_p (tmp, zero))
3229 tmp = zero;
3230 wmin = wi::smax (wmin, tmp);
3234 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3236 tmp = vr0.max;
3237 if (sgn == SIGNED && wi::neg_p (tmp))
3238 tmp = zero;
3239 wmax = wi::min (wmax, tmp, sgn);
3242 min = wide_int_to_tree (expr_type, wmin);
3243 max = wide_int_to_tree (expr_type, wmax);
3245 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3247 bool int_cst_range0, int_cst_range1;
3248 wide_int may_be_nonzero0, may_be_nonzero1;
3249 wide_int must_be_nonzero0, must_be_nonzero1;
3251 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3252 &may_be_nonzero0,
3253 &must_be_nonzero0);
3254 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3255 &may_be_nonzero1,
3256 &must_be_nonzero1);
3258 type = VR_RANGE;
3259 if (code == BIT_AND_EXPR)
3261 min = wide_int_to_tree (expr_type,
3262 must_be_nonzero0 & must_be_nonzero1);
3263 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3264 /* If both input ranges contain only negative values we can
3265 truncate the result range maximum to the minimum of the
3266 input range maxima. */
3267 if (int_cst_range0 && int_cst_range1
3268 && tree_int_cst_sgn (vr0.max) < 0
3269 && tree_int_cst_sgn (vr1.max) < 0)
3271 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3272 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3274 /* If either input range contains only non-negative values
3275 we can truncate the result range maximum to the respective
3276 maximum of the input range. */
3277 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3278 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3279 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3280 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3281 max = wide_int_to_tree (expr_type, wmax);
3283 else if (code == BIT_IOR_EXPR)
3285 max = wide_int_to_tree (expr_type,
3286 may_be_nonzero0 | may_be_nonzero1);
3287 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3288 /* If the input ranges contain only positive values we can
3289 truncate the minimum of the result range to the maximum
3290 of the input range minima. */
3291 if (int_cst_range0 && int_cst_range1
3292 && tree_int_cst_sgn (vr0.min) >= 0
3293 && tree_int_cst_sgn (vr1.min) >= 0)
3295 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3296 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3298 /* If either input range contains only negative values
3299 we can truncate the minimum of the result range to the
3300 respective minimum range. */
3301 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3302 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3303 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3304 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3305 min = wide_int_to_tree (expr_type, wmin);
3307 else if (code == BIT_XOR_EXPR)
3309 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3310 | ~(may_be_nonzero0 | may_be_nonzero1));
3311 wide_int result_one_bits
3312 = (must_be_nonzero0.and_not (may_be_nonzero1)
3313 | must_be_nonzero1.and_not (may_be_nonzero0));
3314 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3315 min = wide_int_to_tree (expr_type, result_one_bits);
3316 /* If the range has all positive or all negative values the
3317 result is better than VARYING. */
3318 if (tree_int_cst_sgn (min) < 0
3319 || tree_int_cst_sgn (max) >= 0)
3321 else
3322 max = min = NULL_TREE;
3325 else
3326 gcc_unreachable ();
3328 /* If either MIN or MAX overflowed, then set the resulting range to
3329 VARYING. But we do accept an overflow infinity representation. */
3330 if (min == NULL_TREE
3331 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3332 || max == NULL_TREE
3333 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3335 set_value_range_to_varying (vr);
3336 return;
3339 /* We punt if:
3340 1) [-INF, +INF]
3341 2) [-INF, +-INF(OVF)]
3342 3) [+-INF(OVF), +INF]
3343 4) [+-INF(OVF), +-INF(OVF)]
3344 We learn nothing when we have INF and INF(OVF) on both sides.
3345 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3346 overflow. */
3347 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3348 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3350 set_value_range_to_varying (vr);
3351 return;
3354 cmp = compare_values (min, max);
3355 if (cmp == -2 || cmp == 1)
3357 /* If the new range has its limits swapped around (MIN > MAX),
3358 then the operation caused one of them to wrap around, mark
3359 the new range VARYING. */
3360 set_value_range_to_varying (vr);
3362 else
3363 set_value_range (vr, type, min, max, NULL);
3366 /* Extract range information from a binary expression OP0 CODE OP1 based on
3367 the ranges of each of its operands with resulting type EXPR_TYPE.
3368 The resulting range is stored in *VR. */
3370 static void
3371 extract_range_from_binary_expr (value_range_t *vr,
3372 enum tree_code code,
3373 tree expr_type, tree op0, tree op1)
3375 value_range_t vr0 = VR_INITIALIZER;
3376 value_range_t vr1 = VR_INITIALIZER;
3378 /* Get value ranges for each operand. For constant operands, create
3379 a new value range with the operand to simplify processing. */
3380 if (TREE_CODE (op0) == SSA_NAME)
3381 vr0 = *(get_value_range (op0));
3382 else if (is_gimple_min_invariant (op0))
3383 set_value_range_to_value (&vr0, op0, NULL);
3384 else
3385 set_value_range_to_varying (&vr0);
3387 if (TREE_CODE (op1) == SSA_NAME)
3388 vr1 = *(get_value_range (op1));
3389 else if (is_gimple_min_invariant (op1))
3390 set_value_range_to_value (&vr1, op1, NULL);
3391 else
3392 set_value_range_to_varying (&vr1);
3394 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3396 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3397 and based on the other operand, for example if it was deduced from a
3398 symbolic comparison. When a bound of the range of the first operand
3399 is invariant, we set the corresponding bound of the new range to INF
3400 in order to avoid recursing on the range of the second operand. */
3401 if (vr->type == VR_VARYING
3402 && (code == PLUS_EXPR || code == MINUS_EXPR)
3403 && TREE_CODE (op1) == SSA_NAME
3404 && vr0.type == VR_RANGE
3405 && symbolic_range_based_on_p (&vr0, op1))
3407 const bool minus_p = (code == MINUS_EXPR);
3408 value_range_t n_vr1 = VR_INITIALIZER;
3410 /* Try with VR0 and [-INF, OP1]. */
3411 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3412 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3414 /* Try with VR0 and [OP1, +INF]. */
3415 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3416 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3418 /* Try with VR0 and [OP1, OP1]. */
3419 else
3420 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3422 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3425 if (vr->type == VR_VARYING
3426 && (code == PLUS_EXPR || code == MINUS_EXPR)
3427 && TREE_CODE (op0) == SSA_NAME
3428 && vr1.type == VR_RANGE
3429 && symbolic_range_based_on_p (&vr1, op0))
3431 const bool minus_p = (code == MINUS_EXPR);
3432 value_range_t n_vr0 = VR_INITIALIZER;
3434 /* Try with [-INF, OP0] and VR1. */
3435 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3436 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3438 /* Try with [OP0, +INF] and VR1. */
3439 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3440 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3442 /* Try with [OP0, OP0] and VR1. */
3443 else
3444 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3446 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3450 /* Extract range information from a unary operation CODE based on
3451 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3452 The The resulting range is stored in *VR. */
3454 static void
3455 extract_range_from_unary_expr_1 (value_range_t *vr,
3456 enum tree_code code, tree type,
3457 value_range_t *vr0_, tree op0_type)
3459 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3461 /* VRP only operates on integral and pointer types. */
3462 if (!(INTEGRAL_TYPE_P (op0_type)
3463 || POINTER_TYPE_P (op0_type))
3464 || !(INTEGRAL_TYPE_P (type)
3465 || POINTER_TYPE_P (type)))
3467 set_value_range_to_varying (vr);
3468 return;
3471 /* If VR0 is UNDEFINED, so is the result. */
3472 if (vr0.type == VR_UNDEFINED)
3474 set_value_range_to_undefined (vr);
3475 return;
3478 /* Handle operations that we express in terms of others. */
3479 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3481 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3482 copy_value_range (vr, &vr0);
3483 return;
3485 else if (code == NEGATE_EXPR)
3487 /* -X is simply 0 - X, so re-use existing code that also handles
3488 anti-ranges fine. */
3489 value_range_t zero = VR_INITIALIZER;
3490 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3491 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3492 return;
3494 else if (code == BIT_NOT_EXPR)
3496 /* ~X is simply -1 - X, so re-use existing code that also handles
3497 anti-ranges fine. */
3498 value_range_t minusone = VR_INITIALIZER;
3499 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3500 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3501 type, &minusone, &vr0);
3502 return;
3505 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3506 and express op ~[] as (op []') U (op []''). */
3507 if (vr0.type == VR_ANTI_RANGE
3508 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3510 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3511 if (vrtem1.type != VR_UNDEFINED)
3513 value_range_t vrres = VR_INITIALIZER;
3514 extract_range_from_unary_expr_1 (&vrres, code, type,
3515 &vrtem1, op0_type);
3516 vrp_meet (vr, &vrres);
3518 return;
3521 if (CONVERT_EXPR_CODE_P (code))
3523 tree inner_type = op0_type;
3524 tree outer_type = type;
3526 /* If the expression evaluates to a pointer, we are only interested in
3527 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3528 if (POINTER_TYPE_P (type))
3530 if (range_is_nonnull (&vr0))
3531 set_value_range_to_nonnull (vr, type);
3532 else if (range_is_null (&vr0))
3533 set_value_range_to_null (vr, type);
3534 else
3535 set_value_range_to_varying (vr);
3536 return;
3539 /* If VR0 is varying and we increase the type precision, assume
3540 a full range for the following transformation. */
3541 if (vr0.type == VR_VARYING
3542 && INTEGRAL_TYPE_P (inner_type)
3543 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3545 vr0.type = VR_RANGE;
3546 vr0.min = TYPE_MIN_VALUE (inner_type);
3547 vr0.max = TYPE_MAX_VALUE (inner_type);
3550 /* If VR0 is a constant range or anti-range and the conversion is
3551 not truncating we can convert the min and max values and
3552 canonicalize the resulting range. Otherwise we can do the
3553 conversion if the size of the range is less than what the
3554 precision of the target type can represent and the range is
3555 not an anti-range. */
3556 if ((vr0.type == VR_RANGE
3557 || vr0.type == VR_ANTI_RANGE)
3558 && TREE_CODE (vr0.min) == INTEGER_CST
3559 && TREE_CODE (vr0.max) == INTEGER_CST
3560 && (!is_overflow_infinity (vr0.min)
3561 || (vr0.type == VR_RANGE
3562 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3563 && needs_overflow_infinity (outer_type)
3564 && supports_overflow_infinity (outer_type)))
3565 && (!is_overflow_infinity (vr0.max)
3566 || (vr0.type == VR_RANGE
3567 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3568 && needs_overflow_infinity (outer_type)
3569 && supports_overflow_infinity (outer_type)))
3570 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3571 || (vr0.type == VR_RANGE
3572 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3573 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3574 size_int (TYPE_PRECISION (outer_type)))))))
3576 tree new_min, new_max;
3577 if (is_overflow_infinity (vr0.min))
3578 new_min = negative_overflow_infinity (outer_type);
3579 else
3580 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3581 0, false);
3582 if (is_overflow_infinity (vr0.max))
3583 new_max = positive_overflow_infinity (outer_type);
3584 else
3585 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3586 0, false);
3587 set_and_canonicalize_value_range (vr, vr0.type,
3588 new_min, new_max, NULL);
3589 return;
3592 set_value_range_to_varying (vr);
3593 return;
3595 else if (code == ABS_EXPR)
3597 tree min, max;
3598 int cmp;
3600 /* Pass through vr0 in the easy cases. */
3601 if (TYPE_UNSIGNED (type)
3602 || value_range_nonnegative_p (&vr0))
3604 copy_value_range (vr, &vr0);
3605 return;
3608 /* For the remaining varying or symbolic ranges we can't do anything
3609 useful. */
3610 if (vr0.type == VR_VARYING
3611 || symbolic_range_p (&vr0))
3613 set_value_range_to_varying (vr);
3614 return;
3617 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3618 useful range. */
3619 if (!TYPE_OVERFLOW_UNDEFINED (type)
3620 && ((vr0.type == VR_RANGE
3621 && vrp_val_is_min (vr0.min))
3622 || (vr0.type == VR_ANTI_RANGE
3623 && !vrp_val_is_min (vr0.min))))
3625 set_value_range_to_varying (vr);
3626 return;
3629 /* ABS_EXPR may flip the range around, if the original range
3630 included negative values. */
3631 if (is_overflow_infinity (vr0.min))
3632 min = positive_overflow_infinity (type);
3633 else if (!vrp_val_is_min (vr0.min))
3634 min = fold_unary_to_constant (code, type, vr0.min);
3635 else if (!needs_overflow_infinity (type))
3636 min = TYPE_MAX_VALUE (type);
3637 else if (supports_overflow_infinity (type))
3638 min = positive_overflow_infinity (type);
3639 else
3641 set_value_range_to_varying (vr);
3642 return;
3645 if (is_overflow_infinity (vr0.max))
3646 max = positive_overflow_infinity (type);
3647 else if (!vrp_val_is_min (vr0.max))
3648 max = fold_unary_to_constant (code, type, vr0.max);
3649 else if (!needs_overflow_infinity (type))
3650 max = TYPE_MAX_VALUE (type);
3651 else if (supports_overflow_infinity (type)
3652 /* We shouldn't generate [+INF, +INF] as set_value_range
3653 doesn't like this and ICEs. */
3654 && !is_positive_overflow_infinity (min))
3655 max = positive_overflow_infinity (type);
3656 else
3658 set_value_range_to_varying (vr);
3659 return;
3662 cmp = compare_values (min, max);
3664 /* If a VR_ANTI_RANGEs contains zero, then we have
3665 ~[-INF, min(MIN, MAX)]. */
3666 if (vr0.type == VR_ANTI_RANGE)
3668 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3670 /* Take the lower of the two values. */
3671 if (cmp != 1)
3672 max = min;
3674 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3675 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3676 flag_wrapv is set and the original anti-range doesn't include
3677 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3678 if (TYPE_OVERFLOW_WRAPS (type))
3680 tree type_min_value = TYPE_MIN_VALUE (type);
3682 min = (vr0.min != type_min_value
3683 ? int_const_binop (PLUS_EXPR, type_min_value,
3684 build_int_cst (TREE_TYPE (type_min_value), 1))
3685 : type_min_value);
3687 else
3689 if (overflow_infinity_range_p (&vr0))
3690 min = negative_overflow_infinity (type);
3691 else
3692 min = TYPE_MIN_VALUE (type);
3695 else
3697 /* All else has failed, so create the range [0, INF], even for
3698 flag_wrapv since TYPE_MIN_VALUE is in the original
3699 anti-range. */
3700 vr0.type = VR_RANGE;
3701 min = build_int_cst (type, 0);
3702 if (needs_overflow_infinity (type))
3704 if (supports_overflow_infinity (type))
3705 max = positive_overflow_infinity (type);
3706 else
3708 set_value_range_to_varying (vr);
3709 return;
3712 else
3713 max = TYPE_MAX_VALUE (type);
3717 /* If the range contains zero then we know that the minimum value in the
3718 range will be zero. */
3719 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3721 if (cmp == 1)
3722 max = min;
3723 min = build_int_cst (type, 0);
3725 else
3727 /* If the range was reversed, swap MIN and MAX. */
3728 if (cmp == 1)
3729 std::swap (min, max);
3732 cmp = compare_values (min, max);
3733 if (cmp == -2 || cmp == 1)
3735 /* If the new range has its limits swapped around (MIN > MAX),
3736 then the operation caused one of them to wrap around, mark
3737 the new range VARYING. */
3738 set_value_range_to_varying (vr);
3740 else
3741 set_value_range (vr, vr0.type, min, max, NULL);
3742 return;
3745 /* For unhandled operations fall back to varying. */
3746 set_value_range_to_varying (vr);
3747 return;
3751 /* Extract range information from a unary expression CODE OP0 based on
3752 the range of its operand with resulting type TYPE.
3753 The resulting range is stored in *VR. */
3755 static void
3756 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3757 tree type, tree op0)
3759 value_range_t vr0 = VR_INITIALIZER;
3761 /* Get value ranges for the operand. For constant operands, create
3762 a new value range with the operand to simplify processing. */
3763 if (TREE_CODE (op0) == SSA_NAME)
3764 vr0 = *(get_value_range (op0));
3765 else if (is_gimple_min_invariant (op0))
3766 set_value_range_to_value (&vr0, op0, NULL);
3767 else
3768 set_value_range_to_varying (&vr0);
3770 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3774 /* Extract range information from a conditional expression STMT based on
3775 the ranges of each of its operands and the expression code. */
3777 static void
3778 extract_range_from_cond_expr (value_range_t *vr, gassign *stmt)
3780 tree op0, op1;
3781 value_range_t vr0 = VR_INITIALIZER;
3782 value_range_t vr1 = VR_INITIALIZER;
3784 /* Get value ranges for each operand. For constant operands, create
3785 a new value range with the operand to simplify processing. */
3786 op0 = gimple_assign_rhs2 (stmt);
3787 if (TREE_CODE (op0) == SSA_NAME)
3788 vr0 = *(get_value_range (op0));
3789 else if (is_gimple_min_invariant (op0))
3790 set_value_range_to_value (&vr0, op0, NULL);
3791 else
3792 set_value_range_to_varying (&vr0);
3794 op1 = gimple_assign_rhs3 (stmt);
3795 if (TREE_CODE (op1) == SSA_NAME)
3796 vr1 = *(get_value_range (op1));
3797 else if (is_gimple_min_invariant (op1))
3798 set_value_range_to_value (&vr1, op1, NULL);
3799 else
3800 set_value_range_to_varying (&vr1);
3802 /* The resulting value range is the union of the operand ranges */
3803 copy_value_range (vr, &vr0);
3804 vrp_meet (vr, &vr1);
3808 /* Extract range information from a comparison expression EXPR based
3809 on the range of its operand and the expression code. */
3811 static void
3812 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3813 tree type, tree op0, tree op1)
3815 bool sop = false;
3816 tree val;
3818 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3819 NULL);
3821 /* A disadvantage of using a special infinity as an overflow
3822 representation is that we lose the ability to record overflow
3823 when we don't have an infinity. So we have to ignore a result
3824 which relies on overflow. */
3826 if (val && !is_overflow_infinity (val) && !sop)
3828 /* Since this expression was found on the RHS of an assignment,
3829 its type may be different from _Bool. Convert VAL to EXPR's
3830 type. */
3831 val = fold_convert (type, val);
3832 if (is_gimple_min_invariant (val))
3833 set_value_range_to_value (vr, val, vr->equiv);
3834 else
3835 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3837 else
3838 /* The result of a comparison is always true or false. */
3839 set_value_range_to_truthvalue (vr, type);
3842 /* Helper function for simplify_internal_call_using_ranges and
3843 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3844 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3845 always overflow. Set *OVF to true if it is known to always
3846 overflow. */
3848 static bool
3849 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3850 tree op0, tree op1, bool *ovf)
3852 value_range_t vr0 = VR_INITIALIZER;
3853 value_range_t vr1 = VR_INITIALIZER;
3854 if (TREE_CODE (op0) == SSA_NAME)
3855 vr0 = *get_value_range (op0);
3856 else if (TREE_CODE (op0) == INTEGER_CST)
3857 set_value_range_to_value (&vr0, op0, NULL);
3858 else
3859 set_value_range_to_varying (&vr0);
3861 if (TREE_CODE (op1) == SSA_NAME)
3862 vr1 = *get_value_range (op1);
3863 else if (TREE_CODE (op1) == INTEGER_CST)
3864 set_value_range_to_value (&vr1, op1, NULL);
3865 else
3866 set_value_range_to_varying (&vr1);
3868 if (!range_int_cst_p (&vr0)
3869 || TREE_OVERFLOW (vr0.min)
3870 || TREE_OVERFLOW (vr0.max))
3872 vr0.min = vrp_val_min (TREE_TYPE (op0));
3873 vr0.max = vrp_val_max (TREE_TYPE (op0));
3875 if (!range_int_cst_p (&vr1)
3876 || TREE_OVERFLOW (vr1.min)
3877 || TREE_OVERFLOW (vr1.max))
3879 vr1.min = vrp_val_min (TREE_TYPE (op1));
3880 vr1.max = vrp_val_max (TREE_TYPE (op1));
3882 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3883 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3884 if (arith_overflowed_p (subcode, type, vr0.max,
3885 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3886 return false;
3887 if (subcode == MULT_EXPR)
3889 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3890 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3891 return false;
3893 if (*ovf)
3895 /* So far we found that there is an overflow on the boundaries.
3896 That doesn't prove that there is an overflow even for all values
3897 in between the boundaries. For that compute widest_int range
3898 of the result and see if it doesn't overlap the range of
3899 type. */
3900 widest_int wmin, wmax;
3901 widest_int w[4];
3902 int i;
3903 w[0] = wi::to_widest (vr0.min);
3904 w[1] = wi::to_widest (vr0.max);
3905 w[2] = wi::to_widest (vr1.min);
3906 w[3] = wi::to_widest (vr1.max);
3907 for (i = 0; i < 4; i++)
3909 widest_int wt;
3910 switch (subcode)
3912 case PLUS_EXPR:
3913 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3914 break;
3915 case MINUS_EXPR:
3916 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3917 break;
3918 case MULT_EXPR:
3919 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3920 break;
3921 default:
3922 gcc_unreachable ();
3924 if (i == 0)
3926 wmin = wt;
3927 wmax = wt;
3929 else
3931 wmin = wi::smin (wmin, wt);
3932 wmax = wi::smax (wmax, wt);
3935 /* The result of op0 CODE op1 is known to be in range
3936 [wmin, wmax]. */
3937 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3938 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3939 /* If all values in [wmin, wmax] are smaller than
3940 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3941 the arithmetic operation will always overflow. */
3942 if (wi::lts_p (wmax, wtmin) || wi::gts_p (wmin, wtmax))
3943 return true;
3944 return false;
3946 return true;
3949 /* Try to derive a nonnegative or nonzero range out of STMT relying
3950 primarily on generic routines in fold in conjunction with range data.
3951 Store the result in *VR */
3953 static void
3954 extract_range_basic (value_range_t *vr, gimple stmt)
3956 bool sop = false;
3957 tree type = gimple_expr_type (stmt);
3959 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3961 tree fndecl = gimple_call_fndecl (stmt), arg;
3962 int mini, maxi, zerov = 0, prec;
3964 switch (DECL_FUNCTION_CODE (fndecl))
3966 case BUILT_IN_CONSTANT_P:
3967 /* If the call is __builtin_constant_p and the argument is a
3968 function parameter resolve it to false. This avoids bogus
3969 array bound warnings.
3970 ??? We could do this as early as inlining is finished. */
3971 arg = gimple_call_arg (stmt, 0);
3972 if (TREE_CODE (arg) == SSA_NAME
3973 && SSA_NAME_IS_DEFAULT_DEF (arg)
3974 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3976 set_value_range_to_null (vr, type);
3977 return;
3979 break;
3980 /* Both __builtin_ffs* and __builtin_popcount return
3981 [0, prec]. */
3982 CASE_INT_FN (BUILT_IN_FFS):
3983 CASE_INT_FN (BUILT_IN_POPCOUNT):
3984 arg = gimple_call_arg (stmt, 0);
3985 prec = TYPE_PRECISION (TREE_TYPE (arg));
3986 mini = 0;
3987 maxi = prec;
3988 if (TREE_CODE (arg) == SSA_NAME)
3990 value_range_t *vr0 = get_value_range (arg);
3991 /* If arg is non-zero, then ffs or popcount
3992 are non-zero. */
3993 if (((vr0->type == VR_RANGE
3994 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3995 || (vr0->type == VR_ANTI_RANGE
3996 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3997 && !is_overflow_infinity (vr0->min)
3998 && !is_overflow_infinity (vr0->max))
3999 mini = 1;
4000 /* If some high bits are known to be zero,
4001 we can decrease the maximum. */
4002 if (vr0->type == VR_RANGE
4003 && TREE_CODE (vr0->max) == INTEGER_CST
4004 && !operand_less_p (vr0->min,
4005 build_zero_cst (TREE_TYPE (vr0->min)))
4006 && !is_overflow_infinity (vr0->max))
4007 maxi = tree_floor_log2 (vr0->max) + 1;
4009 goto bitop_builtin;
4010 /* __builtin_parity* returns [0, 1]. */
4011 CASE_INT_FN (BUILT_IN_PARITY):
4012 mini = 0;
4013 maxi = 1;
4014 goto bitop_builtin;
4015 /* __builtin_c[lt]z* return [0, prec-1], except for
4016 when the argument is 0, but that is undefined behavior.
4017 On many targets where the CLZ RTL or optab value is defined
4018 for 0 the value is prec, so include that in the range
4019 by default. */
4020 CASE_INT_FN (BUILT_IN_CLZ):
4021 arg = gimple_call_arg (stmt, 0);
4022 prec = TYPE_PRECISION (TREE_TYPE (arg));
4023 mini = 0;
4024 maxi = prec;
4025 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
4026 != CODE_FOR_nothing
4027 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
4028 zerov)
4029 /* Handle only the single common value. */
4030 && zerov != prec)
4031 /* Magic value to give up, unless vr0 proves
4032 arg is non-zero. */
4033 mini = -2;
4034 if (TREE_CODE (arg) == SSA_NAME)
4036 value_range_t *vr0 = get_value_range (arg);
4037 /* From clz of VR_RANGE minimum we can compute
4038 result maximum. */
4039 if (vr0->type == VR_RANGE
4040 && TREE_CODE (vr0->min) == INTEGER_CST
4041 && !is_overflow_infinity (vr0->min))
4043 maxi = prec - 1 - tree_floor_log2 (vr0->min);
4044 if (maxi != prec)
4045 mini = 0;
4047 else if (vr0->type == VR_ANTI_RANGE
4048 && integer_zerop (vr0->min)
4049 && !is_overflow_infinity (vr0->min))
4051 maxi = prec - 1;
4052 mini = 0;
4054 if (mini == -2)
4055 break;
4056 /* From clz of VR_RANGE maximum we can compute
4057 result minimum. */
4058 if (vr0->type == VR_RANGE
4059 && TREE_CODE (vr0->max) == INTEGER_CST
4060 && !is_overflow_infinity (vr0->max))
4062 mini = prec - 1 - tree_floor_log2 (vr0->max);
4063 if (mini == prec)
4064 break;
4067 if (mini == -2)
4068 break;
4069 goto bitop_builtin;
4070 /* __builtin_ctz* return [0, prec-1], except for
4071 when the argument is 0, but that is undefined behavior.
4072 If there is a ctz optab for this mode and
4073 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
4074 otherwise just assume 0 won't be seen. */
4075 CASE_INT_FN (BUILT_IN_CTZ):
4076 arg = gimple_call_arg (stmt, 0);
4077 prec = TYPE_PRECISION (TREE_TYPE (arg));
4078 mini = 0;
4079 maxi = prec - 1;
4080 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
4081 != CODE_FOR_nothing
4082 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
4083 zerov))
4085 /* Handle only the two common values. */
4086 if (zerov == -1)
4087 mini = -1;
4088 else if (zerov == prec)
4089 maxi = prec;
4090 else
4091 /* Magic value to give up, unless vr0 proves
4092 arg is non-zero. */
4093 mini = -2;
4095 if (TREE_CODE (arg) == SSA_NAME)
4097 value_range_t *vr0 = get_value_range (arg);
4098 /* If arg is non-zero, then use [0, prec - 1]. */
4099 if (((vr0->type == VR_RANGE
4100 && integer_nonzerop (vr0->min))
4101 || (vr0->type == VR_ANTI_RANGE
4102 && integer_zerop (vr0->min)))
4103 && !is_overflow_infinity (vr0->min))
4105 mini = 0;
4106 maxi = prec - 1;
4108 /* If some high bits are known to be zero,
4109 we can decrease the result maximum. */
4110 if (vr0->type == VR_RANGE
4111 && TREE_CODE (vr0->max) == INTEGER_CST
4112 && !is_overflow_infinity (vr0->max))
4114 maxi = tree_floor_log2 (vr0->max);
4115 /* For vr0 [0, 0] give up. */
4116 if (maxi == -1)
4117 break;
4120 if (mini == -2)
4121 break;
4122 goto bitop_builtin;
4123 /* __builtin_clrsb* returns [0, prec-1]. */
4124 CASE_INT_FN (BUILT_IN_CLRSB):
4125 arg = gimple_call_arg (stmt, 0);
4126 prec = TYPE_PRECISION (TREE_TYPE (arg));
4127 mini = 0;
4128 maxi = prec - 1;
4129 goto bitop_builtin;
4130 bitop_builtin:
4131 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
4132 build_int_cst (type, maxi), NULL);
4133 return;
4134 default:
4135 break;
4138 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
4140 enum tree_code subcode = ERROR_MARK;
4141 switch (gimple_call_internal_fn (stmt))
4143 case IFN_UBSAN_CHECK_ADD:
4144 subcode = PLUS_EXPR;
4145 break;
4146 case IFN_UBSAN_CHECK_SUB:
4147 subcode = MINUS_EXPR;
4148 break;
4149 case IFN_UBSAN_CHECK_MUL:
4150 subcode = MULT_EXPR;
4151 break;
4152 default:
4153 break;
4155 if (subcode != ERROR_MARK)
4157 bool saved_flag_wrapv = flag_wrapv;
4158 /* Pretend the arithmetics is wrapping. If there is
4159 any overflow, we'll complain, but will actually do
4160 wrapping operation. */
4161 flag_wrapv = 1;
4162 extract_range_from_binary_expr (vr, subcode, type,
4163 gimple_call_arg (stmt, 0),
4164 gimple_call_arg (stmt, 1));
4165 flag_wrapv = saved_flag_wrapv;
4167 /* If for both arguments vrp_valueize returned non-NULL,
4168 this should have been already folded and if not, it
4169 wasn't folded because of overflow. Avoid removing the
4170 UBSAN_CHECK_* calls in that case. */
4171 if (vr->type == VR_RANGE
4172 && (vr->min == vr->max
4173 || operand_equal_p (vr->min, vr->max, 0)))
4174 set_value_range_to_varying (vr);
4175 return;
4178 /* Handle extraction of the two results (result of arithmetics and
4179 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4180 internal function. */
4181 else if (is_gimple_assign (stmt)
4182 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4183 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4184 && INTEGRAL_TYPE_P (type))
4186 enum tree_code code = gimple_assign_rhs_code (stmt);
4187 tree op = gimple_assign_rhs1 (stmt);
4188 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4190 gimple g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4191 if (is_gimple_call (g) && gimple_call_internal_p (g))
4193 enum tree_code subcode = ERROR_MARK;
4194 switch (gimple_call_internal_fn (g))
4196 case IFN_ADD_OVERFLOW:
4197 subcode = PLUS_EXPR;
4198 break;
4199 case IFN_SUB_OVERFLOW:
4200 subcode = MINUS_EXPR;
4201 break;
4202 case IFN_MUL_OVERFLOW:
4203 subcode = MULT_EXPR;
4204 break;
4205 default:
4206 break;
4208 if (subcode != ERROR_MARK)
4210 tree op0 = gimple_call_arg (g, 0);
4211 tree op1 = gimple_call_arg (g, 1);
4212 if (code == IMAGPART_EXPR)
4214 bool ovf = false;
4215 if (check_for_binary_op_overflow (subcode, type,
4216 op0, op1, &ovf))
4217 set_value_range_to_value (vr,
4218 build_int_cst (type, ovf),
4219 NULL);
4220 else
4221 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4222 build_int_cst (type, 1), NULL);
4224 else if (types_compatible_p (type, TREE_TYPE (op0))
4225 && types_compatible_p (type, TREE_TYPE (op1)))
4227 bool saved_flag_wrapv = flag_wrapv;
4228 /* Pretend the arithmetics is wrapping. If there is
4229 any overflow, IMAGPART_EXPR will be set. */
4230 flag_wrapv = 1;
4231 extract_range_from_binary_expr (vr, subcode, type,
4232 op0, op1);
4233 flag_wrapv = saved_flag_wrapv;
4235 else
4237 value_range_t vr0 = VR_INITIALIZER;
4238 value_range_t vr1 = VR_INITIALIZER;
4239 bool saved_flag_wrapv = flag_wrapv;
4240 /* Pretend the arithmetics is wrapping. If there is
4241 any overflow, IMAGPART_EXPR will be set. */
4242 flag_wrapv = 1;
4243 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4244 type, op0);
4245 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4246 type, op1);
4247 extract_range_from_binary_expr_1 (vr, subcode, type,
4248 &vr0, &vr1);
4249 flag_wrapv = saved_flag_wrapv;
4251 return;
4256 if (INTEGRAL_TYPE_P (type)
4257 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4258 set_value_range_to_nonnegative (vr, type,
4259 sop || stmt_overflow_infinity (stmt));
4260 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4261 && !sop)
4262 set_value_range_to_nonnull (vr, type);
4263 else
4264 set_value_range_to_varying (vr);
4268 /* Try to compute a useful range out of assignment STMT and store it
4269 in *VR. */
4271 static void
4272 extract_range_from_assignment (value_range_t *vr, gassign *stmt)
4274 enum tree_code code = gimple_assign_rhs_code (stmt);
4276 if (code == ASSERT_EXPR)
4277 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4278 else if (code == SSA_NAME)
4279 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4280 else if (TREE_CODE_CLASS (code) == tcc_binary)
4281 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4282 gimple_expr_type (stmt),
4283 gimple_assign_rhs1 (stmt),
4284 gimple_assign_rhs2 (stmt));
4285 else if (TREE_CODE_CLASS (code) == tcc_unary)
4286 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4287 gimple_expr_type (stmt),
4288 gimple_assign_rhs1 (stmt));
4289 else if (code == COND_EXPR)
4290 extract_range_from_cond_expr (vr, stmt);
4291 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4292 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4293 gimple_expr_type (stmt),
4294 gimple_assign_rhs1 (stmt),
4295 gimple_assign_rhs2 (stmt));
4296 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4297 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4298 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4299 else
4300 set_value_range_to_varying (vr);
4302 if (vr->type == VR_VARYING)
4303 extract_range_basic (vr, stmt);
4306 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4307 would be profitable to adjust VR using scalar evolution information
4308 for VAR. If so, update VR with the new limits. */
4310 static void
4311 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
4312 gimple stmt, tree var)
4314 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4315 enum ev_direction dir;
4317 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4318 better opportunities than a regular range, but I'm not sure. */
4319 if (vr->type == VR_ANTI_RANGE)
4320 return;
4322 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4324 /* Like in PR19590, scev can return a constant function. */
4325 if (is_gimple_min_invariant (chrec))
4327 set_value_range_to_value (vr, chrec, vr->equiv);
4328 return;
4331 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4332 return;
4334 init = initial_condition_in_loop_num (chrec, loop->num);
4335 tem = op_with_constant_singleton_value_range (init);
4336 if (tem)
4337 init = tem;
4338 step = evolution_part_in_loop_num (chrec, loop->num);
4339 tem = op_with_constant_singleton_value_range (step);
4340 if (tem)
4341 step = tem;
4343 /* If STEP is symbolic, we can't know whether INIT will be the
4344 minimum or maximum value in the range. Also, unless INIT is
4345 a simple expression, compare_values and possibly other functions
4346 in tree-vrp won't be able to handle it. */
4347 if (step == NULL_TREE
4348 || !is_gimple_min_invariant (step)
4349 || !valid_value_p (init))
4350 return;
4352 dir = scev_direction (chrec);
4353 if (/* Do not adjust ranges if we do not know whether the iv increases
4354 or decreases, ... */
4355 dir == EV_DIR_UNKNOWN
4356 /* ... or if it may wrap. */
4357 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4358 true))
4359 return;
4361 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4362 negative_overflow_infinity and positive_overflow_infinity,
4363 because we have concluded that the loop probably does not
4364 wrap. */
4366 type = TREE_TYPE (var);
4367 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4368 tmin = lower_bound_in_type (type, type);
4369 else
4370 tmin = TYPE_MIN_VALUE (type);
4371 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4372 tmax = upper_bound_in_type (type, type);
4373 else
4374 tmax = TYPE_MAX_VALUE (type);
4376 /* Try to use estimated number of iterations for the loop to constrain the
4377 final value in the evolution. */
4378 if (TREE_CODE (step) == INTEGER_CST
4379 && is_gimple_val (init)
4380 && (TREE_CODE (init) != SSA_NAME
4381 || get_value_range (init)->type == VR_RANGE))
4383 widest_int nit;
4385 /* We are only entering here for loop header PHI nodes, so using
4386 the number of latch executions is the correct thing to use. */
4387 if (max_loop_iterations (loop, &nit))
4389 value_range_t maxvr = VR_INITIALIZER;
4390 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4391 bool overflow;
4393 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4394 &overflow);
4395 /* If the multiplication overflowed we can't do a meaningful
4396 adjustment. Likewise if the result doesn't fit in the type
4397 of the induction variable. For a signed type we have to
4398 check whether the result has the expected signedness which
4399 is that of the step as number of iterations is unsigned. */
4400 if (!overflow
4401 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4402 && (sgn == UNSIGNED
4403 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4405 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4406 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4407 TREE_TYPE (init), init, tem);
4408 /* Likewise if the addition did. */
4409 if (maxvr.type == VR_RANGE)
4411 tmin = maxvr.min;
4412 tmax = maxvr.max;
4418 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4420 min = tmin;
4421 max = tmax;
4423 /* For VARYING or UNDEFINED ranges, just about anything we get
4424 from scalar evolutions should be better. */
4426 if (dir == EV_DIR_DECREASES)
4427 max = init;
4428 else
4429 min = init;
4431 else if (vr->type == VR_RANGE)
4433 min = vr->min;
4434 max = vr->max;
4436 if (dir == EV_DIR_DECREASES)
4438 /* INIT is the maximum value. If INIT is lower than VR->MAX
4439 but no smaller than VR->MIN, set VR->MAX to INIT. */
4440 if (compare_values (init, max) == -1)
4441 max = init;
4443 /* According to the loop information, the variable does not
4444 overflow. If we think it does, probably because of an
4445 overflow due to arithmetic on a different INF value,
4446 reset now. */
4447 if (is_negative_overflow_infinity (min)
4448 || compare_values (min, tmin) == -1)
4449 min = tmin;
4452 else
4454 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4455 if (compare_values (init, min) == 1)
4456 min = init;
4458 if (is_positive_overflow_infinity (max)
4459 || compare_values (tmax, max) == -1)
4460 max = tmax;
4463 else
4464 return;
4466 /* If we just created an invalid range with the minimum
4467 greater than the maximum, we fail conservatively.
4468 This should happen only in unreachable
4469 parts of code, or for invalid programs. */
4470 if (compare_values (min, max) == 1
4471 || (is_negative_overflow_infinity (min)
4472 && is_positive_overflow_infinity (max)))
4473 return;
4475 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4479 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4481 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4482 all the values in the ranges.
4484 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4486 - Return NULL_TREE if it is not always possible to determine the
4487 value of the comparison.
4489 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4490 overflow infinity was used in the test. */
4493 static tree
4494 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4495 bool *strict_overflow_p)
4497 /* VARYING or UNDEFINED ranges cannot be compared. */
4498 if (vr0->type == VR_VARYING
4499 || vr0->type == VR_UNDEFINED
4500 || vr1->type == VR_VARYING
4501 || vr1->type == VR_UNDEFINED)
4502 return NULL_TREE;
4504 /* Anti-ranges need to be handled separately. */
4505 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4507 /* If both are anti-ranges, then we cannot compute any
4508 comparison. */
4509 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4510 return NULL_TREE;
4512 /* These comparisons are never statically computable. */
4513 if (comp == GT_EXPR
4514 || comp == GE_EXPR
4515 || comp == LT_EXPR
4516 || comp == LE_EXPR)
4517 return NULL_TREE;
4519 /* Equality can be computed only between a range and an
4520 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4521 if (vr0->type == VR_RANGE)
4523 /* To simplify processing, make VR0 the anti-range. */
4524 value_range_t *tmp = vr0;
4525 vr0 = vr1;
4526 vr1 = tmp;
4529 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4531 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4532 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4533 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4535 return NULL_TREE;
4538 if (!usable_range_p (vr0, strict_overflow_p)
4539 || !usable_range_p (vr1, strict_overflow_p))
4540 return NULL_TREE;
4542 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4543 operands around and change the comparison code. */
4544 if (comp == GT_EXPR || comp == GE_EXPR)
4546 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4547 std::swap (vr0, vr1);
4550 if (comp == EQ_EXPR)
4552 /* Equality may only be computed if both ranges represent
4553 exactly one value. */
4554 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4555 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4557 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4558 strict_overflow_p);
4559 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4560 strict_overflow_p);
4561 if (cmp_min == 0 && cmp_max == 0)
4562 return boolean_true_node;
4563 else if (cmp_min != -2 && cmp_max != -2)
4564 return boolean_false_node;
4566 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4567 else if (compare_values_warnv (vr0->min, vr1->max,
4568 strict_overflow_p) == 1
4569 || compare_values_warnv (vr1->min, vr0->max,
4570 strict_overflow_p) == 1)
4571 return boolean_false_node;
4573 return NULL_TREE;
4575 else if (comp == NE_EXPR)
4577 int cmp1, cmp2;
4579 /* If VR0 is completely to the left or completely to the right
4580 of VR1, they are always different. Notice that we need to
4581 make sure that both comparisons yield similar results to
4582 avoid comparing values that cannot be compared at
4583 compile-time. */
4584 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4585 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4586 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4587 return boolean_true_node;
4589 /* If VR0 and VR1 represent a single value and are identical,
4590 return false. */
4591 else if (compare_values_warnv (vr0->min, vr0->max,
4592 strict_overflow_p) == 0
4593 && compare_values_warnv (vr1->min, vr1->max,
4594 strict_overflow_p) == 0
4595 && compare_values_warnv (vr0->min, vr1->min,
4596 strict_overflow_p) == 0
4597 && compare_values_warnv (vr0->max, vr1->max,
4598 strict_overflow_p) == 0)
4599 return boolean_false_node;
4601 /* Otherwise, they may or may not be different. */
4602 else
4603 return NULL_TREE;
4605 else if (comp == LT_EXPR || comp == LE_EXPR)
4607 int tst;
4609 /* If VR0 is to the left of VR1, return true. */
4610 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4611 if ((comp == LT_EXPR && tst == -1)
4612 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4614 if (overflow_infinity_range_p (vr0)
4615 || overflow_infinity_range_p (vr1))
4616 *strict_overflow_p = true;
4617 return boolean_true_node;
4620 /* If VR0 is to the right of VR1, return false. */
4621 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4622 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4623 || (comp == LE_EXPR && tst == 1))
4625 if (overflow_infinity_range_p (vr0)
4626 || overflow_infinity_range_p (vr1))
4627 *strict_overflow_p = true;
4628 return boolean_false_node;
4631 /* Otherwise, we don't know. */
4632 return NULL_TREE;
4635 gcc_unreachable ();
4639 /* Given a value range VR, a value VAL and a comparison code COMP, return
4640 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4641 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4642 always returns false. Return NULL_TREE if it is not always
4643 possible to determine the value of the comparison. Also set
4644 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4645 infinity was used in the test. */
4647 static tree
4648 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4649 bool *strict_overflow_p)
4651 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4652 return NULL_TREE;
4654 /* Anti-ranges need to be handled separately. */
4655 if (vr->type == VR_ANTI_RANGE)
4657 /* For anti-ranges, the only predicates that we can compute at
4658 compile time are equality and inequality. */
4659 if (comp == GT_EXPR
4660 || comp == GE_EXPR
4661 || comp == LT_EXPR
4662 || comp == LE_EXPR)
4663 return NULL_TREE;
4665 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4666 if (value_inside_range (val, vr->min, vr->max) == 1)
4667 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4669 return NULL_TREE;
4672 if (!usable_range_p (vr, strict_overflow_p))
4673 return NULL_TREE;
4675 if (comp == EQ_EXPR)
4677 /* EQ_EXPR may only be computed if VR represents exactly
4678 one value. */
4679 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4681 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4682 if (cmp == 0)
4683 return boolean_true_node;
4684 else if (cmp == -1 || cmp == 1 || cmp == 2)
4685 return boolean_false_node;
4687 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4688 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4689 return boolean_false_node;
4691 return NULL_TREE;
4693 else if (comp == NE_EXPR)
4695 /* If VAL is not inside VR, then they are always different. */
4696 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4697 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4698 return boolean_true_node;
4700 /* If VR represents exactly one value equal to VAL, then return
4701 false. */
4702 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4703 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4704 return boolean_false_node;
4706 /* Otherwise, they may or may not be different. */
4707 return NULL_TREE;
4709 else if (comp == LT_EXPR || comp == LE_EXPR)
4711 int tst;
4713 /* If VR is to the left of VAL, return true. */
4714 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4715 if ((comp == LT_EXPR && tst == -1)
4716 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4718 if (overflow_infinity_range_p (vr))
4719 *strict_overflow_p = true;
4720 return boolean_true_node;
4723 /* If VR is to the right of VAL, return false. */
4724 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4725 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4726 || (comp == LE_EXPR && tst == 1))
4728 if (overflow_infinity_range_p (vr))
4729 *strict_overflow_p = true;
4730 return boolean_false_node;
4733 /* Otherwise, we don't know. */
4734 return NULL_TREE;
4736 else if (comp == GT_EXPR || comp == GE_EXPR)
4738 int tst;
4740 /* If VR is to the right of VAL, return true. */
4741 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4742 if ((comp == GT_EXPR && tst == 1)
4743 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4745 if (overflow_infinity_range_p (vr))
4746 *strict_overflow_p = true;
4747 return boolean_true_node;
4750 /* If VR is to the left of VAL, return false. */
4751 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4752 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4753 || (comp == GE_EXPR && tst == -1))
4755 if (overflow_infinity_range_p (vr))
4756 *strict_overflow_p = true;
4757 return boolean_false_node;
4760 /* Otherwise, we don't know. */
4761 return NULL_TREE;
4764 gcc_unreachable ();
4768 /* Debugging dumps. */
4770 void dump_value_range (FILE *, value_range_t *);
4771 void debug_value_range (value_range_t *);
4772 void dump_all_value_ranges (FILE *);
4773 void debug_all_value_ranges (void);
4774 void dump_vr_equiv (FILE *, bitmap);
4775 void debug_vr_equiv (bitmap);
4778 /* Dump value range VR to FILE. */
4780 void
4781 dump_value_range (FILE *file, value_range_t *vr)
4783 if (vr == NULL)
4784 fprintf (file, "[]");
4785 else if (vr->type == VR_UNDEFINED)
4786 fprintf (file, "UNDEFINED");
4787 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4789 tree type = TREE_TYPE (vr->min);
4791 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4793 if (is_negative_overflow_infinity (vr->min))
4794 fprintf (file, "-INF(OVF)");
4795 else if (INTEGRAL_TYPE_P (type)
4796 && !TYPE_UNSIGNED (type)
4797 && vrp_val_is_min (vr->min))
4798 fprintf (file, "-INF");
4799 else
4800 print_generic_expr (file, vr->min, 0);
4802 fprintf (file, ", ");
4804 if (is_positive_overflow_infinity (vr->max))
4805 fprintf (file, "+INF(OVF)");
4806 else if (INTEGRAL_TYPE_P (type)
4807 && vrp_val_is_max (vr->max))
4808 fprintf (file, "+INF");
4809 else
4810 print_generic_expr (file, vr->max, 0);
4812 fprintf (file, "]");
4814 if (vr->equiv)
4816 bitmap_iterator bi;
4817 unsigned i, c = 0;
4819 fprintf (file, " EQUIVALENCES: { ");
4821 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4823 print_generic_expr (file, ssa_name (i), 0);
4824 fprintf (file, " ");
4825 c++;
4828 fprintf (file, "} (%u elements)", c);
4831 else if (vr->type == VR_VARYING)
4832 fprintf (file, "VARYING");
4833 else
4834 fprintf (file, "INVALID RANGE");
4838 /* Dump value range VR to stderr. */
4840 DEBUG_FUNCTION void
4841 debug_value_range (value_range_t *vr)
4843 dump_value_range (stderr, vr);
4844 fprintf (stderr, "\n");
4848 /* Dump value ranges of all SSA_NAMEs to FILE. */
4850 void
4851 dump_all_value_ranges (FILE *file)
4853 size_t i;
4855 for (i = 0; i < num_vr_values; i++)
4857 if (vr_value[i])
4859 print_generic_expr (file, ssa_name (i), 0);
4860 fprintf (file, ": ");
4861 dump_value_range (file, vr_value[i]);
4862 fprintf (file, "\n");
4866 fprintf (file, "\n");
4870 /* Dump all value ranges to stderr. */
4872 DEBUG_FUNCTION void
4873 debug_all_value_ranges (void)
4875 dump_all_value_ranges (stderr);
4879 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4880 create a new SSA name N and return the assertion assignment
4881 'N = ASSERT_EXPR <V, V OP W>'. */
4883 static gimple
4884 build_assert_expr_for (tree cond, tree v)
4886 tree a;
4887 gassign *assertion;
4889 gcc_assert (TREE_CODE (v) == SSA_NAME
4890 && COMPARISON_CLASS_P (cond));
4892 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4893 assertion = gimple_build_assign (NULL_TREE, a);
4895 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4896 operand of the ASSERT_EXPR. Create it so the new name and the old one
4897 are registered in the replacement table so that we can fix the SSA web
4898 after adding all the ASSERT_EXPRs. */
4899 create_new_def_for (v, assertion, NULL);
4901 return assertion;
4905 /* Return false if EXPR is a predicate expression involving floating
4906 point values. */
4908 static inline bool
4909 fp_predicate (gimple stmt)
4911 GIMPLE_CHECK (stmt, GIMPLE_COND);
4913 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4916 /* If the range of values taken by OP can be inferred after STMT executes,
4917 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4918 describes the inferred range. Return true if a range could be
4919 inferred. */
4921 static bool
4922 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4924 *val_p = NULL_TREE;
4925 *comp_code_p = ERROR_MARK;
4927 /* Do not attempt to infer anything in names that flow through
4928 abnormal edges. */
4929 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4930 return false;
4932 /* Similarly, don't infer anything from statements that may throw
4933 exceptions. ??? Relax this requirement? */
4934 if (stmt_could_throw_p (stmt))
4935 return false;
4937 /* If STMT is the last statement of a basic block with no normal
4938 successors, there is no point inferring anything about any of its
4939 operands. We would not be able to find a proper insertion point
4940 for the assertion, anyway. */
4941 if (stmt_ends_bb_p (stmt))
4943 edge_iterator ei;
4944 edge e;
4946 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4947 if (!(e->flags & EDGE_ABNORMAL))
4948 break;
4949 if (e == NULL)
4950 return false;
4953 if (infer_nonnull_range (stmt, op, true, true))
4955 *val_p = build_int_cst (TREE_TYPE (op), 0);
4956 *comp_code_p = NE_EXPR;
4957 return true;
4960 return false;
4964 void dump_asserts_for (FILE *, tree);
4965 void debug_asserts_for (tree);
4966 void dump_all_asserts (FILE *);
4967 void debug_all_asserts (void);
4969 /* Dump all the registered assertions for NAME to FILE. */
4971 void
4972 dump_asserts_for (FILE *file, tree name)
4974 assert_locus_t loc;
4976 fprintf (file, "Assertions to be inserted for ");
4977 print_generic_expr (file, name, 0);
4978 fprintf (file, "\n");
4980 loc = asserts_for[SSA_NAME_VERSION (name)];
4981 while (loc)
4983 fprintf (file, "\t");
4984 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4985 fprintf (file, "\n\tBB #%d", loc->bb->index);
4986 if (loc->e)
4988 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4989 loc->e->dest->index);
4990 dump_edge_info (file, loc->e, dump_flags, 0);
4992 fprintf (file, "\n\tPREDICATE: ");
4993 print_generic_expr (file, name, 0);
4994 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4995 print_generic_expr (file, loc->val, 0);
4996 fprintf (file, "\n\n");
4997 loc = loc->next;
5000 fprintf (file, "\n");
5004 /* Dump all the registered assertions for NAME to stderr. */
5006 DEBUG_FUNCTION void
5007 debug_asserts_for (tree name)
5009 dump_asserts_for (stderr, name);
5013 /* Dump all the registered assertions for all the names to FILE. */
5015 void
5016 dump_all_asserts (FILE *file)
5018 unsigned i;
5019 bitmap_iterator bi;
5021 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
5022 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
5023 dump_asserts_for (file, ssa_name (i));
5024 fprintf (file, "\n");
5028 /* Dump all the registered assertions for all the names to stderr. */
5030 DEBUG_FUNCTION void
5031 debug_all_asserts (void)
5033 dump_all_asserts (stderr);
5037 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
5038 'EXPR COMP_CODE VAL' at a location that dominates block BB or
5039 E->DEST, then register this location as a possible insertion point
5040 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
5042 BB, E and SI provide the exact insertion point for the new
5043 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
5044 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
5045 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
5046 must not be NULL. */
5048 static void
5049 register_new_assert_for (tree name, tree expr,
5050 enum tree_code comp_code,
5051 tree val,
5052 basic_block bb,
5053 edge e,
5054 gimple_stmt_iterator si)
5056 assert_locus_t n, loc, last_loc;
5057 basic_block dest_bb;
5059 gcc_checking_assert (bb == NULL || e == NULL);
5061 if (e == NULL)
5062 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
5063 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
5065 /* Never build an assert comparing against an integer constant with
5066 TREE_OVERFLOW set. This confuses our undefined overflow warning
5067 machinery. */
5068 if (TREE_OVERFLOW_P (val))
5069 val = drop_tree_overflow (val);
5071 /* The new assertion A will be inserted at BB or E. We need to
5072 determine if the new location is dominated by a previously
5073 registered location for A. If we are doing an edge insertion,
5074 assume that A will be inserted at E->DEST. Note that this is not
5075 necessarily true.
5077 If E is a critical edge, it will be split. But even if E is
5078 split, the new block will dominate the same set of blocks that
5079 E->DEST dominates.
5081 The reverse, however, is not true, blocks dominated by E->DEST
5082 will not be dominated by the new block created to split E. So,
5083 if the insertion location is on a critical edge, we will not use
5084 the new location to move another assertion previously registered
5085 at a block dominated by E->DEST. */
5086 dest_bb = (bb) ? bb : e->dest;
5088 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5089 VAL at a block dominating DEST_BB, then we don't need to insert a new
5090 one. Similarly, if the same assertion already exists at a block
5091 dominated by DEST_BB and the new location is not on a critical
5092 edge, then update the existing location for the assertion (i.e.,
5093 move the assertion up in the dominance tree).
5095 Note, this is implemented as a simple linked list because there
5096 should not be more than a handful of assertions registered per
5097 name. If this becomes a performance problem, a table hashed by
5098 COMP_CODE and VAL could be implemented. */
5099 loc = asserts_for[SSA_NAME_VERSION (name)];
5100 last_loc = loc;
5101 while (loc)
5103 if (loc->comp_code == comp_code
5104 && (loc->val == val
5105 || operand_equal_p (loc->val, val, 0))
5106 && (loc->expr == expr
5107 || operand_equal_p (loc->expr, expr, 0)))
5109 /* If E is not a critical edge and DEST_BB
5110 dominates the existing location for the assertion, move
5111 the assertion up in the dominance tree by updating its
5112 location information. */
5113 if ((e == NULL || !EDGE_CRITICAL_P (e))
5114 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5116 loc->bb = dest_bb;
5117 loc->e = e;
5118 loc->si = si;
5119 return;
5123 /* Update the last node of the list and move to the next one. */
5124 last_loc = loc;
5125 loc = loc->next;
5128 /* If we didn't find an assertion already registered for
5129 NAME COMP_CODE VAL, add a new one at the end of the list of
5130 assertions associated with NAME. */
5131 n = XNEW (struct assert_locus_d);
5132 n->bb = dest_bb;
5133 n->e = e;
5134 n->si = si;
5135 n->comp_code = comp_code;
5136 n->val = val;
5137 n->expr = expr;
5138 n->next = NULL;
5140 if (last_loc)
5141 last_loc->next = n;
5142 else
5143 asserts_for[SSA_NAME_VERSION (name)] = n;
5145 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5148 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5149 Extract a suitable test code and value and store them into *CODE_P and
5150 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5152 If no extraction was possible, return FALSE, otherwise return TRUE.
5154 If INVERT is true, then we invert the result stored into *CODE_P. */
5156 static bool
5157 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5158 tree cond_op0, tree cond_op1,
5159 bool invert, enum tree_code *code_p,
5160 tree *val_p)
5162 enum tree_code comp_code;
5163 tree val;
5165 /* Otherwise, we have a comparison of the form NAME COMP VAL
5166 or VAL COMP NAME. */
5167 if (name == cond_op1)
5169 /* If the predicate is of the form VAL COMP NAME, flip
5170 COMP around because we need to register NAME as the
5171 first operand in the predicate. */
5172 comp_code = swap_tree_comparison (cond_code);
5173 val = cond_op0;
5175 else
5177 /* The comparison is of the form NAME COMP VAL, so the
5178 comparison code remains unchanged. */
5179 comp_code = cond_code;
5180 val = cond_op1;
5183 /* Invert the comparison code as necessary. */
5184 if (invert)
5185 comp_code = invert_tree_comparison (comp_code, 0);
5187 /* VRP does not handle float types. */
5188 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
5189 return false;
5191 /* Do not register always-false predicates.
5192 FIXME: this works around a limitation in fold() when dealing with
5193 enumerations. Given 'enum { N1, N2 } x;', fold will not
5194 fold 'if (x > N2)' to 'if (0)'. */
5195 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5196 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5198 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5199 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5201 if (comp_code == GT_EXPR
5202 && (!max
5203 || compare_values (val, max) == 0))
5204 return false;
5206 if (comp_code == LT_EXPR
5207 && (!min
5208 || compare_values (val, min) == 0))
5209 return false;
5211 *code_p = comp_code;
5212 *val_p = val;
5213 return true;
5216 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5217 (otherwise return VAL). VAL and MASK must be zero-extended for
5218 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5219 (to transform signed values into unsigned) and at the end xor
5220 SGNBIT back. */
5222 static wide_int
5223 masked_increment (const wide_int &val_in, const wide_int &mask,
5224 const wide_int &sgnbit, unsigned int prec)
5226 wide_int bit = wi::one (prec), res;
5227 unsigned int i;
5229 wide_int val = val_in ^ sgnbit;
5230 for (i = 0; i < prec; i++, bit += bit)
5232 res = mask;
5233 if ((res & bit) == 0)
5234 continue;
5235 res = bit - 1;
5236 res = (val + bit).and_not (res);
5237 res &= mask;
5238 if (wi::gtu_p (res, val))
5239 return res ^ sgnbit;
5241 return val ^ sgnbit;
5244 /* Try to register an edge assertion for SSA name NAME on edge E for
5245 the condition COND contributing to the conditional jump pointed to by BSI.
5246 Invert the condition COND if INVERT is true. */
5248 static void
5249 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5250 enum tree_code cond_code,
5251 tree cond_op0, tree cond_op1, bool invert)
5253 tree val;
5254 enum tree_code comp_code;
5256 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5257 cond_op0,
5258 cond_op1,
5259 invert, &comp_code, &val))
5260 return;
5262 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5263 reachable from E. */
5264 if (live_on_edge (e, name)
5265 && !has_single_use (name))
5266 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5268 /* In the case of NAME <= CST and NAME being defined as
5269 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5270 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5271 This catches range and anti-range tests. */
5272 if ((comp_code == LE_EXPR
5273 || comp_code == GT_EXPR)
5274 && TREE_CODE (val) == INTEGER_CST
5275 && TYPE_UNSIGNED (TREE_TYPE (val)))
5277 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5278 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5280 /* Extract CST2 from the (optional) addition. */
5281 if (is_gimple_assign (def_stmt)
5282 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5284 name2 = gimple_assign_rhs1 (def_stmt);
5285 cst2 = gimple_assign_rhs2 (def_stmt);
5286 if (TREE_CODE (name2) == SSA_NAME
5287 && TREE_CODE (cst2) == INTEGER_CST)
5288 def_stmt = SSA_NAME_DEF_STMT (name2);
5291 /* Extract NAME2 from the (optional) sign-changing cast. */
5292 if (gimple_assign_cast_p (def_stmt))
5294 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5295 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5296 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5297 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5298 name3 = gimple_assign_rhs1 (def_stmt);
5301 /* If name3 is used later, create an ASSERT_EXPR for it. */
5302 if (name3 != NULL_TREE
5303 && TREE_CODE (name3) == SSA_NAME
5304 && (cst2 == NULL_TREE
5305 || TREE_CODE (cst2) == INTEGER_CST)
5306 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5307 && live_on_edge (e, name3)
5308 && !has_single_use (name3))
5310 tree tmp;
5312 /* Build an expression for the range test. */
5313 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5314 if (cst2 != NULL_TREE)
5315 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5317 if (dump_file)
5319 fprintf (dump_file, "Adding assert for ");
5320 print_generic_expr (dump_file, name3, 0);
5321 fprintf (dump_file, " from ");
5322 print_generic_expr (dump_file, tmp, 0);
5323 fprintf (dump_file, "\n");
5326 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5329 /* If name2 is used later, create an ASSERT_EXPR for it. */
5330 if (name2 != NULL_TREE
5331 && TREE_CODE (name2) == SSA_NAME
5332 && TREE_CODE (cst2) == INTEGER_CST
5333 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5334 && live_on_edge (e, name2)
5335 && !has_single_use (name2))
5337 tree tmp;
5339 /* Build an expression for the range test. */
5340 tmp = name2;
5341 if (TREE_TYPE (name) != TREE_TYPE (name2))
5342 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5343 if (cst2 != NULL_TREE)
5344 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5346 if (dump_file)
5348 fprintf (dump_file, "Adding assert for ");
5349 print_generic_expr (dump_file, name2, 0);
5350 fprintf (dump_file, " from ");
5351 print_generic_expr (dump_file, tmp, 0);
5352 fprintf (dump_file, "\n");
5355 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5359 /* In the case of post-in/decrement tests like if (i++) ... and uses
5360 of the in/decremented value on the edge the extra name we want to
5361 assert for is not on the def chain of the name compared. Instead
5362 it is in the set of use stmts.
5363 Similar cases happen for conversions that were simplified through
5364 fold_{sign_changed,widened}_comparison. */
5365 if ((comp_code == NE_EXPR
5366 || comp_code == EQ_EXPR)
5367 && TREE_CODE (val) == INTEGER_CST)
5369 imm_use_iterator ui;
5370 gimple use_stmt;
5371 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5373 if (!is_gimple_assign (use_stmt))
5374 continue;
5376 /* Cut off to use-stmts that are dominating the predecessor. */
5377 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5378 continue;
5380 tree name2 = gimple_assign_lhs (use_stmt);
5381 if (TREE_CODE (name2) != SSA_NAME
5382 || !live_on_edge (e, name2))
5383 continue;
5385 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5386 tree cst;
5387 if (code == PLUS_EXPR
5388 || code == MINUS_EXPR)
5390 cst = gimple_assign_rhs2 (use_stmt);
5391 if (TREE_CODE (cst) != INTEGER_CST)
5392 continue;
5393 cst = int_const_binop (code, val, cst);
5395 else if (CONVERT_EXPR_CODE_P (code))
5396 cst = fold_convert (TREE_TYPE (name2), val);
5397 else
5398 continue;
5400 if (TREE_OVERFLOW_P (cst))
5401 cst = drop_tree_overflow (cst);
5402 register_new_assert_for (name2, name2, comp_code, cst,
5403 NULL, e, bsi);
5407 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5408 && TREE_CODE (val) == INTEGER_CST)
5410 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5411 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5412 tree val2 = NULL_TREE;
5413 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5414 wide_int mask = wi::zero (prec);
5415 unsigned int nprec = prec;
5416 enum tree_code rhs_code = ERROR_MARK;
5418 if (is_gimple_assign (def_stmt))
5419 rhs_code = gimple_assign_rhs_code (def_stmt);
5421 /* Add asserts for NAME cmp CST and NAME being defined
5422 as NAME = (int) NAME2. */
5423 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5424 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5425 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5426 && gimple_assign_cast_p (def_stmt))
5428 name2 = gimple_assign_rhs1 (def_stmt);
5429 if (CONVERT_EXPR_CODE_P (rhs_code)
5430 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5431 && TYPE_UNSIGNED (TREE_TYPE (name2))
5432 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5433 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5434 || !tree_int_cst_equal (val,
5435 TYPE_MIN_VALUE (TREE_TYPE (val))))
5436 && live_on_edge (e, name2)
5437 && !has_single_use (name2))
5439 tree tmp, cst;
5440 enum tree_code new_comp_code = comp_code;
5442 cst = fold_convert (TREE_TYPE (name2),
5443 TYPE_MIN_VALUE (TREE_TYPE (val)));
5444 /* Build an expression for the range test. */
5445 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5446 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5447 fold_convert (TREE_TYPE (name2), val));
5448 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5450 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5451 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5452 build_int_cst (TREE_TYPE (name2), 1));
5455 if (dump_file)
5457 fprintf (dump_file, "Adding assert for ");
5458 print_generic_expr (dump_file, name2, 0);
5459 fprintf (dump_file, " from ");
5460 print_generic_expr (dump_file, tmp, 0);
5461 fprintf (dump_file, "\n");
5464 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5465 e, bsi);
5469 /* Add asserts for NAME cmp CST and NAME being defined as
5470 NAME = NAME2 >> CST2.
5472 Extract CST2 from the right shift. */
5473 if (rhs_code == RSHIFT_EXPR)
5475 name2 = gimple_assign_rhs1 (def_stmt);
5476 cst2 = gimple_assign_rhs2 (def_stmt);
5477 if (TREE_CODE (name2) == SSA_NAME
5478 && tree_fits_uhwi_p (cst2)
5479 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5480 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5481 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5482 && live_on_edge (e, name2)
5483 && !has_single_use (name2))
5485 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5486 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5489 if (val2 != NULL_TREE
5490 && TREE_CODE (val2) == INTEGER_CST
5491 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5492 TREE_TYPE (val),
5493 val2, cst2), val))
5495 enum tree_code new_comp_code = comp_code;
5496 tree tmp, new_val;
5498 tmp = name2;
5499 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5501 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5503 tree type = build_nonstandard_integer_type (prec, 1);
5504 tmp = build1 (NOP_EXPR, type, name2);
5505 val2 = fold_convert (type, val2);
5507 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5508 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5509 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5511 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5513 wide_int minval
5514 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5515 new_val = val2;
5516 if (minval == new_val)
5517 new_val = NULL_TREE;
5519 else
5521 wide_int maxval
5522 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5523 mask |= val2;
5524 if (mask == maxval)
5525 new_val = NULL_TREE;
5526 else
5527 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5530 if (new_val)
5532 if (dump_file)
5534 fprintf (dump_file, "Adding assert for ");
5535 print_generic_expr (dump_file, name2, 0);
5536 fprintf (dump_file, " from ");
5537 print_generic_expr (dump_file, tmp, 0);
5538 fprintf (dump_file, "\n");
5541 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5542 NULL, e, bsi);
5546 /* Add asserts for NAME cmp CST and NAME being defined as
5547 NAME = NAME2 & CST2.
5549 Extract CST2 from the and.
5551 Also handle
5552 NAME = (unsigned) NAME2;
5553 casts where NAME's type is unsigned and has smaller precision
5554 than NAME2's type as if it was NAME = NAME2 & MASK. */
5555 names[0] = NULL_TREE;
5556 names[1] = NULL_TREE;
5557 cst2 = NULL_TREE;
5558 if (rhs_code == BIT_AND_EXPR
5559 || (CONVERT_EXPR_CODE_P (rhs_code)
5560 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5561 && TYPE_UNSIGNED (TREE_TYPE (val))
5562 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5563 > prec))
5565 name2 = gimple_assign_rhs1 (def_stmt);
5566 if (rhs_code == BIT_AND_EXPR)
5567 cst2 = gimple_assign_rhs2 (def_stmt);
5568 else
5570 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5571 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5573 if (TREE_CODE (name2) == SSA_NAME
5574 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5575 && TREE_CODE (cst2) == INTEGER_CST
5576 && !integer_zerop (cst2)
5577 && (nprec > 1
5578 || TYPE_UNSIGNED (TREE_TYPE (val))))
5580 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5581 if (gimple_assign_cast_p (def_stmt2))
5583 names[1] = gimple_assign_rhs1 (def_stmt2);
5584 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5585 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5586 || (TYPE_PRECISION (TREE_TYPE (name2))
5587 != TYPE_PRECISION (TREE_TYPE (names[1])))
5588 || !live_on_edge (e, names[1])
5589 || has_single_use (names[1]))
5590 names[1] = NULL_TREE;
5592 if (live_on_edge (e, name2)
5593 && !has_single_use (name2))
5594 names[0] = name2;
5597 if (names[0] || names[1])
5599 wide_int minv, maxv, valv, cst2v;
5600 wide_int tem, sgnbit;
5601 bool valid_p = false, valn, cst2n;
5602 enum tree_code ccode = comp_code;
5604 valv = wide_int::from (val, nprec, UNSIGNED);
5605 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5606 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5607 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5608 /* If CST2 doesn't have most significant bit set,
5609 but VAL is negative, we have comparison like
5610 if ((x & 0x123) > -4) (always true). Just give up. */
5611 if (!cst2n && valn)
5612 ccode = ERROR_MARK;
5613 if (cst2n)
5614 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5615 else
5616 sgnbit = wi::zero (nprec);
5617 minv = valv & cst2v;
5618 switch (ccode)
5620 case EQ_EXPR:
5621 /* Minimum unsigned value for equality is VAL & CST2
5622 (should be equal to VAL, otherwise we probably should
5623 have folded the comparison into false) and
5624 maximum unsigned value is VAL | ~CST2. */
5625 maxv = valv | ~cst2v;
5626 valid_p = true;
5627 break;
5629 case NE_EXPR:
5630 tem = valv | ~cst2v;
5631 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5632 if (valv == 0)
5634 cst2n = false;
5635 sgnbit = wi::zero (nprec);
5636 goto gt_expr;
5638 /* If (VAL | ~CST2) is all ones, handle it as
5639 (X & CST2) < VAL. */
5640 if (tem == -1)
5642 cst2n = false;
5643 valn = false;
5644 sgnbit = wi::zero (nprec);
5645 goto lt_expr;
5647 if (!cst2n && wi::neg_p (cst2v))
5648 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5649 if (sgnbit != 0)
5651 if (valv == sgnbit)
5653 cst2n = true;
5654 valn = true;
5655 goto gt_expr;
5657 if (tem == wi::mask (nprec - 1, false, nprec))
5659 cst2n = true;
5660 goto lt_expr;
5662 if (!cst2n)
5663 sgnbit = wi::zero (nprec);
5665 break;
5667 case GE_EXPR:
5668 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5669 is VAL and maximum unsigned value is ~0. For signed
5670 comparison, if CST2 doesn't have most significant bit
5671 set, handle it similarly. If CST2 has MSB set,
5672 the minimum is the same, and maximum is ~0U/2. */
5673 if (minv != valv)
5675 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5676 VAL. */
5677 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5678 if (minv == valv)
5679 break;
5681 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5682 valid_p = true;
5683 break;
5685 case GT_EXPR:
5686 gt_expr:
5687 /* Find out smallest MINV where MINV > VAL
5688 && (MINV & CST2) == MINV, if any. If VAL is signed and
5689 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5690 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5691 if (minv == valv)
5692 break;
5693 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5694 valid_p = true;
5695 break;
5697 case LE_EXPR:
5698 /* Minimum unsigned value for <= is 0 and maximum
5699 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5700 Otherwise, find smallest VAL2 where VAL2 > VAL
5701 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5702 as maximum.
5703 For signed comparison, if CST2 doesn't have most
5704 significant bit set, handle it similarly. If CST2 has
5705 MSB set, the maximum is the same and minimum is INT_MIN. */
5706 if (minv == valv)
5707 maxv = valv;
5708 else
5710 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5711 if (maxv == valv)
5712 break;
5713 maxv -= 1;
5715 maxv |= ~cst2v;
5716 minv = sgnbit;
5717 valid_p = true;
5718 break;
5720 case LT_EXPR:
5721 lt_expr:
5722 /* Minimum unsigned value for < is 0 and maximum
5723 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5724 Otherwise, find smallest VAL2 where VAL2 > VAL
5725 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5726 as maximum.
5727 For signed comparison, if CST2 doesn't have most
5728 significant bit set, handle it similarly. If CST2 has
5729 MSB set, the maximum is the same and minimum is INT_MIN. */
5730 if (minv == valv)
5732 if (valv == sgnbit)
5733 break;
5734 maxv = valv;
5736 else
5738 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5739 if (maxv == valv)
5740 break;
5742 maxv -= 1;
5743 maxv |= ~cst2v;
5744 minv = sgnbit;
5745 valid_p = true;
5746 break;
5748 default:
5749 break;
5751 if (valid_p
5752 && (maxv - minv) != -1)
5754 tree tmp, new_val, type;
5755 int i;
5757 for (i = 0; i < 2; i++)
5758 if (names[i])
5760 wide_int maxv2 = maxv;
5761 tmp = names[i];
5762 type = TREE_TYPE (names[i]);
5763 if (!TYPE_UNSIGNED (type))
5765 type = build_nonstandard_integer_type (nprec, 1);
5766 tmp = build1 (NOP_EXPR, type, names[i]);
5768 if (minv != 0)
5770 tmp = build2 (PLUS_EXPR, type, tmp,
5771 wide_int_to_tree (type, -minv));
5772 maxv2 = maxv - minv;
5774 new_val = wide_int_to_tree (type, maxv2);
5776 if (dump_file)
5778 fprintf (dump_file, "Adding assert for ");
5779 print_generic_expr (dump_file, names[i], 0);
5780 fprintf (dump_file, " from ");
5781 print_generic_expr (dump_file, tmp, 0);
5782 fprintf (dump_file, "\n");
5785 register_new_assert_for (names[i], tmp, LE_EXPR,
5786 new_val, NULL, e, bsi);
5793 /* OP is an operand of a truth value expression which is known to have
5794 a particular value. Register any asserts for OP and for any
5795 operands in OP's defining statement.
5797 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5798 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5800 static void
5801 register_edge_assert_for_1 (tree op, enum tree_code code,
5802 edge e, gimple_stmt_iterator bsi)
5804 gimple op_def;
5805 tree val;
5806 enum tree_code rhs_code;
5808 /* We only care about SSA_NAMEs. */
5809 if (TREE_CODE (op) != SSA_NAME)
5810 return;
5812 /* We know that OP will have a zero or nonzero value. If OP is used
5813 more than once go ahead and register an assert for OP. */
5814 if (live_on_edge (e, op)
5815 && !has_single_use (op))
5817 val = build_int_cst (TREE_TYPE (op), 0);
5818 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5821 /* Now look at how OP is set. If it's set from a comparison,
5822 a truth operation or some bit operations, then we may be able
5823 to register information about the operands of that assignment. */
5824 op_def = SSA_NAME_DEF_STMT (op);
5825 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5826 return;
5828 rhs_code = gimple_assign_rhs_code (op_def);
5830 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5832 bool invert = (code == EQ_EXPR ? true : false);
5833 tree op0 = gimple_assign_rhs1 (op_def);
5834 tree op1 = gimple_assign_rhs2 (op_def);
5836 if (TREE_CODE (op0) == SSA_NAME)
5837 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5838 if (TREE_CODE (op1) == SSA_NAME)
5839 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5841 else if ((code == NE_EXPR
5842 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5843 || (code == EQ_EXPR
5844 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5846 /* Recurse on each operand. */
5847 tree op0 = gimple_assign_rhs1 (op_def);
5848 tree op1 = gimple_assign_rhs2 (op_def);
5849 if (TREE_CODE (op0) == SSA_NAME
5850 && has_single_use (op0))
5851 register_edge_assert_for_1 (op0, code, e, bsi);
5852 if (TREE_CODE (op1) == SSA_NAME
5853 && has_single_use (op1))
5854 register_edge_assert_for_1 (op1, code, e, bsi);
5856 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5857 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5859 /* Recurse, flipping CODE. */
5860 code = invert_tree_comparison (code, false);
5861 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5863 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5865 /* Recurse through the copy. */
5866 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5868 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5870 /* Recurse through the type conversion, unless it is a narrowing
5871 conversion or conversion from non-integral type. */
5872 tree rhs = gimple_assign_rhs1 (op_def);
5873 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5874 && (TYPE_PRECISION (TREE_TYPE (rhs))
5875 <= TYPE_PRECISION (TREE_TYPE (op))))
5876 register_edge_assert_for_1 (rhs, code, e, bsi);
5880 /* Try to register an edge assertion for SSA name NAME on edge E for
5881 the condition COND contributing to the conditional jump pointed to by
5882 SI. */
5884 static void
5885 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5886 enum tree_code cond_code, tree cond_op0,
5887 tree cond_op1)
5889 tree val;
5890 enum tree_code comp_code;
5891 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5893 /* Do not attempt to infer anything in names that flow through
5894 abnormal edges. */
5895 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5896 return;
5898 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5899 cond_op0, cond_op1,
5900 is_else_edge,
5901 &comp_code, &val))
5902 return;
5904 /* Register ASSERT_EXPRs for name. */
5905 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5906 cond_op1, is_else_edge);
5909 /* If COND is effectively an equality test of an SSA_NAME against
5910 the value zero or one, then we may be able to assert values
5911 for SSA_NAMEs which flow into COND. */
5913 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5914 statement of NAME we can assert both operands of the BIT_AND_EXPR
5915 have nonzero value. */
5916 if (((comp_code == EQ_EXPR && integer_onep (val))
5917 || (comp_code == NE_EXPR && integer_zerop (val))))
5919 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5921 if (is_gimple_assign (def_stmt)
5922 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5924 tree op0 = gimple_assign_rhs1 (def_stmt);
5925 tree op1 = gimple_assign_rhs2 (def_stmt);
5926 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5927 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5931 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5932 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5933 have zero value. */
5934 if (((comp_code == EQ_EXPR && integer_zerop (val))
5935 || (comp_code == NE_EXPR && integer_onep (val))))
5937 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5939 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5940 necessarily zero value, or if type-precision is one. */
5941 if (is_gimple_assign (def_stmt)
5942 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5943 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5944 || comp_code == EQ_EXPR)))
5946 tree op0 = gimple_assign_rhs1 (def_stmt);
5947 tree op1 = gimple_assign_rhs2 (def_stmt);
5948 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5949 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5955 /* Determine whether the outgoing edges of BB should receive an
5956 ASSERT_EXPR for each of the operands of BB's LAST statement.
5957 The last statement of BB must be a COND_EXPR.
5959 If any of the sub-graphs rooted at BB have an interesting use of
5960 the predicate operands, an assert location node is added to the
5961 list of assertions for the corresponding operands. */
5963 static void
5964 find_conditional_asserts (basic_block bb, gcond *last)
5966 gimple_stmt_iterator bsi;
5967 tree op;
5968 edge_iterator ei;
5969 edge e;
5970 ssa_op_iter iter;
5972 bsi = gsi_for_stmt (last);
5974 /* Look for uses of the operands in each of the sub-graphs
5975 rooted at BB. We need to check each of the outgoing edges
5976 separately, so that we know what kind of ASSERT_EXPR to
5977 insert. */
5978 FOR_EACH_EDGE (e, ei, bb->succs)
5980 if (e->dest == bb)
5981 continue;
5983 /* Register the necessary assertions for each operand in the
5984 conditional predicate. */
5985 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5986 register_edge_assert_for (op, e, bsi,
5987 gimple_cond_code (last),
5988 gimple_cond_lhs (last),
5989 gimple_cond_rhs (last));
5993 struct case_info
5995 tree expr;
5996 basic_block bb;
5999 /* Compare two case labels sorting first by the destination bb index
6000 and then by the case value. */
6002 static int
6003 compare_case_labels (const void *p1, const void *p2)
6005 const struct case_info *ci1 = (const struct case_info *) p1;
6006 const struct case_info *ci2 = (const struct case_info *) p2;
6007 int idx1 = ci1->bb->index;
6008 int idx2 = ci2->bb->index;
6010 if (idx1 < idx2)
6011 return -1;
6012 else if (idx1 == idx2)
6014 /* Make sure the default label is first in a group. */
6015 if (!CASE_LOW (ci1->expr))
6016 return -1;
6017 else if (!CASE_LOW (ci2->expr))
6018 return 1;
6019 else
6020 return tree_int_cst_compare (CASE_LOW (ci1->expr),
6021 CASE_LOW (ci2->expr));
6023 else
6024 return 1;
6027 /* Determine whether the outgoing edges of BB should receive an
6028 ASSERT_EXPR for each of the operands of BB's LAST statement.
6029 The last statement of BB must be a SWITCH_EXPR.
6031 If any of the sub-graphs rooted at BB have an interesting use of
6032 the predicate operands, an assert location node is added to the
6033 list of assertions for the corresponding operands. */
6035 static void
6036 find_switch_asserts (basic_block bb, gswitch *last)
6038 gimple_stmt_iterator bsi;
6039 tree op;
6040 edge e;
6041 struct case_info *ci;
6042 size_t n = gimple_switch_num_labels (last);
6043 #if GCC_VERSION >= 4000
6044 unsigned int idx;
6045 #else
6046 /* Work around GCC 3.4 bug (PR 37086). */
6047 volatile unsigned int idx;
6048 #endif
6050 bsi = gsi_for_stmt (last);
6051 op = gimple_switch_index (last);
6052 if (TREE_CODE (op) != SSA_NAME)
6053 return;
6055 /* Build a vector of case labels sorted by destination label. */
6056 ci = XNEWVEC (struct case_info, n);
6057 for (idx = 0; idx < n; ++idx)
6059 ci[idx].expr = gimple_switch_label (last, idx);
6060 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
6062 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
6064 for (idx = 0; idx < n; ++idx)
6066 tree min, max;
6067 tree cl = ci[idx].expr;
6068 basic_block cbb = ci[idx].bb;
6070 min = CASE_LOW (cl);
6071 max = CASE_HIGH (cl);
6073 /* If there are multiple case labels with the same destination
6074 we need to combine them to a single value range for the edge. */
6075 if (idx + 1 < n && cbb == ci[idx + 1].bb)
6077 /* Skip labels until the last of the group. */
6078 do {
6079 ++idx;
6080 } while (idx < n && cbb == ci[idx].bb);
6081 --idx;
6083 /* Pick up the maximum of the case label range. */
6084 if (CASE_HIGH (ci[idx].expr))
6085 max = CASE_HIGH (ci[idx].expr);
6086 else
6087 max = CASE_LOW (ci[idx].expr);
6090 /* Nothing to do if the range includes the default label until we
6091 can register anti-ranges. */
6092 if (min == NULL_TREE)
6093 continue;
6095 /* Find the edge to register the assert expr on. */
6096 e = find_edge (bb, cbb);
6098 /* Register the necessary assertions for the operand in the
6099 SWITCH_EXPR. */
6100 register_edge_assert_for (op, e, bsi,
6101 max ? GE_EXPR : EQ_EXPR,
6102 op, fold_convert (TREE_TYPE (op), min));
6103 if (max)
6104 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6105 fold_convert (TREE_TYPE (op), max));
6108 XDELETEVEC (ci);
6112 /* Traverse all the statements in block BB looking for statements that
6113 may generate useful assertions for the SSA names in their operand.
6114 If a statement produces a useful assertion A for name N_i, then the
6115 list of assertions already generated for N_i is scanned to
6116 determine if A is actually needed.
6118 If N_i already had the assertion A at a location dominating the
6119 current location, then nothing needs to be done. Otherwise, the
6120 new location for A is recorded instead.
6122 1- For every statement S in BB, all the variables used by S are
6123 added to bitmap FOUND_IN_SUBGRAPH.
6125 2- If statement S uses an operand N in a way that exposes a known
6126 value range for N, then if N was not already generated by an
6127 ASSERT_EXPR, create a new assert location for N. For instance,
6128 if N is a pointer and the statement dereferences it, we can
6129 assume that N is not NULL.
6131 3- COND_EXPRs are a special case of #2. We can derive range
6132 information from the predicate but need to insert different
6133 ASSERT_EXPRs for each of the sub-graphs rooted at the
6134 conditional block. If the last statement of BB is a conditional
6135 expression of the form 'X op Y', then
6137 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6139 b) If the conditional is the only entry point to the sub-graph
6140 corresponding to the THEN_CLAUSE, recurse into it. On
6141 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6142 an ASSERT_EXPR is added for the corresponding variable.
6144 c) Repeat step (b) on the ELSE_CLAUSE.
6146 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6148 For instance,
6150 if (a == 9)
6151 b = a;
6152 else
6153 b = c + 1;
6155 In this case, an assertion on the THEN clause is useful to
6156 determine that 'a' is always 9 on that edge. However, an assertion
6157 on the ELSE clause would be unnecessary.
6159 4- If BB does not end in a conditional expression, then we recurse
6160 into BB's dominator children.
6162 At the end of the recursive traversal, every SSA name will have a
6163 list of locations where ASSERT_EXPRs should be added. When a new
6164 location for name N is found, it is registered by calling
6165 register_new_assert_for. That function keeps track of all the
6166 registered assertions to prevent adding unnecessary assertions.
6167 For instance, if a pointer P_4 is dereferenced more than once in a
6168 dominator tree, only the location dominating all the dereference of
6169 P_4 will receive an ASSERT_EXPR. */
6171 static void
6172 find_assert_locations_1 (basic_block bb, sbitmap live)
6174 gimple last;
6176 last = last_stmt (bb);
6178 /* If BB's last statement is a conditional statement involving integer
6179 operands, determine if we need to add ASSERT_EXPRs. */
6180 if (last
6181 && gimple_code (last) == GIMPLE_COND
6182 && !fp_predicate (last)
6183 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6184 find_conditional_asserts (bb, as_a <gcond *> (last));
6186 /* If BB's last statement is a switch statement involving integer
6187 operands, determine if we need to add ASSERT_EXPRs. */
6188 if (last
6189 && gimple_code (last) == GIMPLE_SWITCH
6190 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6191 find_switch_asserts (bb, as_a <gswitch *> (last));
6193 /* Traverse all the statements in BB marking used names and looking
6194 for statements that may infer assertions for their used operands. */
6195 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6196 gsi_prev (&si))
6198 gimple stmt;
6199 tree op;
6200 ssa_op_iter i;
6202 stmt = gsi_stmt (si);
6204 if (is_gimple_debug (stmt))
6205 continue;
6207 /* See if we can derive an assertion for any of STMT's operands. */
6208 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6210 tree value;
6211 enum tree_code comp_code;
6213 /* If op is not live beyond this stmt, do not bother to insert
6214 asserts for it. */
6215 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6216 continue;
6218 /* If OP is used in such a way that we can infer a value
6219 range for it, and we don't find a previous assertion for
6220 it, create a new assertion location node for OP. */
6221 if (infer_value_range (stmt, op, &comp_code, &value))
6223 /* If we are able to infer a nonzero value range for OP,
6224 then walk backwards through the use-def chain to see if OP
6225 was set via a typecast.
6227 If so, then we can also infer a nonzero value range
6228 for the operand of the NOP_EXPR. */
6229 if (comp_code == NE_EXPR && integer_zerop (value))
6231 tree t = op;
6232 gimple def_stmt = SSA_NAME_DEF_STMT (t);
6234 while (is_gimple_assign (def_stmt)
6235 && CONVERT_EXPR_CODE_P
6236 (gimple_assign_rhs_code (def_stmt))
6237 && TREE_CODE
6238 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6239 && POINTER_TYPE_P
6240 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6242 t = gimple_assign_rhs1 (def_stmt);
6243 def_stmt = SSA_NAME_DEF_STMT (t);
6245 /* Note we want to register the assert for the
6246 operand of the NOP_EXPR after SI, not after the
6247 conversion. */
6248 if (! has_single_use (t))
6249 register_new_assert_for (t, t, comp_code, value,
6250 bb, NULL, si);
6254 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6258 /* Update live. */
6259 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6260 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6261 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6262 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6265 /* Traverse all PHI nodes in BB, updating live. */
6266 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6267 gsi_next (&si))
6269 use_operand_p arg_p;
6270 ssa_op_iter i;
6271 gphi *phi = si.phi ();
6272 tree res = gimple_phi_result (phi);
6274 if (virtual_operand_p (res))
6275 continue;
6277 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6279 tree arg = USE_FROM_PTR (arg_p);
6280 if (TREE_CODE (arg) == SSA_NAME)
6281 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6284 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6288 /* Do an RPO walk over the function computing SSA name liveness
6289 on-the-fly and deciding on assert expressions to insert. */
6291 static void
6292 find_assert_locations (void)
6294 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6295 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6296 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6297 int rpo_cnt, i;
6299 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6300 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6301 for (i = 0; i < rpo_cnt; ++i)
6302 bb_rpo[rpo[i]] = i;
6304 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6305 the order we compute liveness and insert asserts we otherwise
6306 fail to insert asserts into the loop latch. */
6307 loop_p loop;
6308 FOR_EACH_LOOP (loop, 0)
6310 i = loop->latch->index;
6311 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6312 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6313 !gsi_end_p (gsi); gsi_next (&gsi))
6315 gphi *phi = gsi.phi ();
6316 if (virtual_operand_p (gimple_phi_result (phi)))
6317 continue;
6318 tree arg = gimple_phi_arg_def (phi, j);
6319 if (TREE_CODE (arg) == SSA_NAME)
6321 if (live[i] == NULL)
6323 live[i] = sbitmap_alloc (num_ssa_names);
6324 bitmap_clear (live[i]);
6326 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6331 for (i = rpo_cnt - 1; i >= 0; --i)
6333 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6334 edge e;
6335 edge_iterator ei;
6337 if (!live[rpo[i]])
6339 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6340 bitmap_clear (live[rpo[i]]);
6343 /* Process BB and update the live information with uses in
6344 this block. */
6345 find_assert_locations_1 (bb, live[rpo[i]]);
6347 /* Merge liveness into the predecessor blocks and free it. */
6348 if (!bitmap_empty_p (live[rpo[i]]))
6350 int pred_rpo = i;
6351 FOR_EACH_EDGE (e, ei, bb->preds)
6353 int pred = e->src->index;
6354 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6355 continue;
6357 if (!live[pred])
6359 live[pred] = sbitmap_alloc (num_ssa_names);
6360 bitmap_clear (live[pred]);
6362 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6364 if (bb_rpo[pred] < pred_rpo)
6365 pred_rpo = bb_rpo[pred];
6368 /* Record the RPO number of the last visited block that needs
6369 live information from this block. */
6370 last_rpo[rpo[i]] = pred_rpo;
6372 else
6374 sbitmap_free (live[rpo[i]]);
6375 live[rpo[i]] = NULL;
6378 /* We can free all successors live bitmaps if all their
6379 predecessors have been visited already. */
6380 FOR_EACH_EDGE (e, ei, bb->succs)
6381 if (last_rpo[e->dest->index] == i
6382 && live[e->dest->index])
6384 sbitmap_free (live[e->dest->index]);
6385 live[e->dest->index] = NULL;
6389 XDELETEVEC (rpo);
6390 XDELETEVEC (bb_rpo);
6391 XDELETEVEC (last_rpo);
6392 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6393 if (live[i])
6394 sbitmap_free (live[i]);
6395 XDELETEVEC (live);
6398 /* Create an ASSERT_EXPR for NAME and insert it in the location
6399 indicated by LOC. Return true if we made any edge insertions. */
6401 static bool
6402 process_assert_insertions_for (tree name, assert_locus_t loc)
6404 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6405 gimple stmt;
6406 tree cond;
6407 gimple assert_stmt;
6408 edge_iterator ei;
6409 edge e;
6411 /* If we have X <=> X do not insert an assert expr for that. */
6412 if (loc->expr == loc->val)
6413 return false;
6415 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6416 assert_stmt = build_assert_expr_for (cond, name);
6417 if (loc->e)
6419 /* We have been asked to insert the assertion on an edge. This
6420 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6421 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6422 || (gimple_code (gsi_stmt (loc->si))
6423 == GIMPLE_SWITCH));
6425 gsi_insert_on_edge (loc->e, assert_stmt);
6426 return true;
6429 /* Otherwise, we can insert right after LOC->SI iff the
6430 statement must not be the last statement in the block. */
6431 stmt = gsi_stmt (loc->si);
6432 if (!stmt_ends_bb_p (stmt))
6434 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6435 return false;
6438 /* If STMT must be the last statement in BB, we can only insert new
6439 assertions on the non-abnormal edge out of BB. Note that since
6440 STMT is not control flow, there may only be one non-abnormal edge
6441 out of BB. */
6442 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6443 if (!(e->flags & EDGE_ABNORMAL))
6445 gsi_insert_on_edge (e, assert_stmt);
6446 return true;
6449 gcc_unreachable ();
6453 /* Process all the insertions registered for every name N_i registered
6454 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6455 found in ASSERTS_FOR[i]. */
6457 static void
6458 process_assert_insertions (void)
6460 unsigned i;
6461 bitmap_iterator bi;
6462 bool update_edges_p = false;
6463 int num_asserts = 0;
6465 if (dump_file && (dump_flags & TDF_DETAILS))
6466 dump_all_asserts (dump_file);
6468 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6470 assert_locus_t loc = asserts_for[i];
6471 gcc_assert (loc);
6473 while (loc)
6475 assert_locus_t next = loc->next;
6476 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6477 free (loc);
6478 loc = next;
6479 num_asserts++;
6483 if (update_edges_p)
6484 gsi_commit_edge_inserts ();
6486 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6487 num_asserts);
6491 /* Traverse the flowgraph looking for conditional jumps to insert range
6492 expressions. These range expressions are meant to provide information
6493 to optimizations that need to reason in terms of value ranges. They
6494 will not be expanded into RTL. For instance, given:
6496 x = ...
6497 y = ...
6498 if (x < y)
6499 y = x - 2;
6500 else
6501 x = y + 3;
6503 this pass will transform the code into:
6505 x = ...
6506 y = ...
6507 if (x < y)
6509 x = ASSERT_EXPR <x, x < y>
6510 y = x - 2
6512 else
6514 y = ASSERT_EXPR <y, x >= y>
6515 x = y + 3
6518 The idea is that once copy and constant propagation have run, other
6519 optimizations will be able to determine what ranges of values can 'x'
6520 take in different paths of the code, simply by checking the reaching
6521 definition of 'x'. */
6523 static void
6524 insert_range_assertions (void)
6526 need_assert_for = BITMAP_ALLOC (NULL);
6527 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6529 calculate_dominance_info (CDI_DOMINATORS);
6531 find_assert_locations ();
6532 if (!bitmap_empty_p (need_assert_for))
6534 process_assert_insertions ();
6535 update_ssa (TODO_update_ssa_no_phi);
6538 if (dump_file && (dump_flags & TDF_DETAILS))
6540 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6541 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6544 free (asserts_for);
6545 BITMAP_FREE (need_assert_for);
6548 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6549 and "struct" hacks. If VRP can determine that the
6550 array subscript is a constant, check if it is outside valid
6551 range. If the array subscript is a RANGE, warn if it is
6552 non-overlapping with valid range.
6553 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6555 static void
6556 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6558 value_range_t* vr = NULL;
6559 tree low_sub, up_sub;
6560 tree low_bound, up_bound, up_bound_p1;
6561 tree base;
6563 if (TREE_NO_WARNING (ref))
6564 return;
6566 low_sub = up_sub = TREE_OPERAND (ref, 1);
6567 up_bound = array_ref_up_bound (ref);
6569 /* Can not check flexible arrays. */
6570 if (!up_bound
6571 || TREE_CODE (up_bound) != INTEGER_CST)
6572 return;
6574 /* Accesses to trailing arrays via pointers may access storage
6575 beyond the types array bounds. */
6576 base = get_base_address (ref);
6577 if ((warn_array_bounds < 2)
6578 && base && TREE_CODE (base) == MEM_REF)
6580 tree cref, next = NULL_TREE;
6582 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6583 return;
6585 cref = TREE_OPERAND (ref, 0);
6586 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6587 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6588 next && TREE_CODE (next) != FIELD_DECL;
6589 next = DECL_CHAIN (next))
6592 /* If this is the last field in a struct type or a field in a
6593 union type do not warn. */
6594 if (!next)
6595 return;
6598 low_bound = array_ref_low_bound (ref);
6599 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6600 build_int_cst (TREE_TYPE (up_bound), 1));
6602 /* Empty array. */
6603 if (tree_int_cst_equal (low_bound, up_bound_p1))
6605 warning_at (location, OPT_Warray_bounds,
6606 "array subscript is above array bounds");
6607 TREE_NO_WARNING (ref) = 1;
6610 if (TREE_CODE (low_sub) == SSA_NAME)
6612 vr = get_value_range (low_sub);
6613 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6615 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6616 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6620 if (vr && vr->type == VR_ANTI_RANGE)
6622 if (TREE_CODE (up_sub) == INTEGER_CST
6623 && (ignore_off_by_one
6624 ? tree_int_cst_lt (up_bound, up_sub)
6625 : tree_int_cst_le (up_bound, up_sub))
6626 && TREE_CODE (low_sub) == INTEGER_CST
6627 && tree_int_cst_le (low_sub, low_bound))
6629 warning_at (location, OPT_Warray_bounds,
6630 "array subscript is outside array bounds");
6631 TREE_NO_WARNING (ref) = 1;
6634 else if (TREE_CODE (up_sub) == INTEGER_CST
6635 && (ignore_off_by_one
6636 ? !tree_int_cst_le (up_sub, up_bound_p1)
6637 : !tree_int_cst_le (up_sub, up_bound)))
6639 if (dump_file && (dump_flags & TDF_DETAILS))
6641 fprintf (dump_file, "Array bound warning for ");
6642 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6643 fprintf (dump_file, "\n");
6645 warning_at (location, OPT_Warray_bounds,
6646 "array subscript is above array bounds");
6647 TREE_NO_WARNING (ref) = 1;
6649 else if (TREE_CODE (low_sub) == INTEGER_CST
6650 && tree_int_cst_lt (low_sub, low_bound))
6652 if (dump_file && (dump_flags & TDF_DETAILS))
6654 fprintf (dump_file, "Array bound warning for ");
6655 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6656 fprintf (dump_file, "\n");
6658 warning_at (location, OPT_Warray_bounds,
6659 "array subscript is below array bounds");
6660 TREE_NO_WARNING (ref) = 1;
6664 /* Searches if the expr T, located at LOCATION computes
6665 address of an ARRAY_REF, and call check_array_ref on it. */
6667 static void
6668 search_for_addr_array (tree t, location_t location)
6670 /* Check each ARRAY_REFs in the reference chain. */
6673 if (TREE_CODE (t) == ARRAY_REF)
6674 check_array_ref (location, t, true /*ignore_off_by_one*/);
6676 t = TREE_OPERAND (t, 0);
6678 while (handled_component_p (t));
6680 if (TREE_CODE (t) == MEM_REF
6681 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6682 && !TREE_NO_WARNING (t))
6684 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6685 tree low_bound, up_bound, el_sz;
6686 offset_int idx;
6687 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6688 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6689 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6690 return;
6692 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6693 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6694 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6695 if (!low_bound
6696 || TREE_CODE (low_bound) != INTEGER_CST
6697 || !up_bound
6698 || TREE_CODE (up_bound) != INTEGER_CST
6699 || !el_sz
6700 || TREE_CODE (el_sz) != INTEGER_CST)
6701 return;
6703 idx = mem_ref_offset (t);
6704 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6705 if (wi::lts_p (idx, 0))
6707 if (dump_file && (dump_flags & TDF_DETAILS))
6709 fprintf (dump_file, "Array bound warning for ");
6710 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6711 fprintf (dump_file, "\n");
6713 warning_at (location, OPT_Warray_bounds,
6714 "array subscript is below array bounds");
6715 TREE_NO_WARNING (t) = 1;
6717 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6718 - wi::to_offset (low_bound) + 1)))
6720 if (dump_file && (dump_flags & TDF_DETAILS))
6722 fprintf (dump_file, "Array bound warning for ");
6723 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6724 fprintf (dump_file, "\n");
6726 warning_at (location, OPT_Warray_bounds,
6727 "array subscript is above array bounds");
6728 TREE_NO_WARNING (t) = 1;
6733 /* walk_tree() callback that checks if *TP is
6734 an ARRAY_REF inside an ADDR_EXPR (in which an array
6735 subscript one outside the valid range is allowed). Call
6736 check_array_ref for each ARRAY_REF found. The location is
6737 passed in DATA. */
6739 static tree
6740 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6742 tree t = *tp;
6743 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6744 location_t location;
6746 if (EXPR_HAS_LOCATION (t))
6747 location = EXPR_LOCATION (t);
6748 else
6750 location_t *locp = (location_t *) wi->info;
6751 location = *locp;
6754 *walk_subtree = TRUE;
6756 if (TREE_CODE (t) == ARRAY_REF)
6757 check_array_ref (location, t, false /*ignore_off_by_one*/);
6759 else if (TREE_CODE (t) == ADDR_EXPR)
6761 search_for_addr_array (t, location);
6762 *walk_subtree = FALSE;
6765 return NULL_TREE;
6768 /* Walk over all statements of all reachable BBs and call check_array_bounds
6769 on them. */
6771 static void
6772 check_all_array_refs (void)
6774 basic_block bb;
6775 gimple_stmt_iterator si;
6777 FOR_EACH_BB_FN (bb, cfun)
6779 edge_iterator ei;
6780 edge e;
6781 bool executable = false;
6783 /* Skip blocks that were found to be unreachable. */
6784 FOR_EACH_EDGE (e, ei, bb->preds)
6785 executable |= !!(e->flags & EDGE_EXECUTABLE);
6786 if (!executable)
6787 continue;
6789 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6791 gimple stmt = gsi_stmt (si);
6792 struct walk_stmt_info wi;
6793 if (!gimple_has_location (stmt)
6794 || is_gimple_debug (stmt))
6795 continue;
6797 memset (&wi, 0, sizeof (wi));
6798 wi.info = CONST_CAST (void *, (const void *)
6799 gimple_location_ptr (stmt));
6801 walk_gimple_op (gsi_stmt (si),
6802 check_array_bounds,
6803 &wi);
6808 /* Return true if all imm uses of VAR are either in STMT, or
6809 feed (optionally through a chain of single imm uses) GIMPLE_COND
6810 in basic block COND_BB. */
6812 static bool
6813 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6815 use_operand_p use_p, use2_p;
6816 imm_use_iterator iter;
6818 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6819 if (USE_STMT (use_p) != stmt)
6821 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6822 if (is_gimple_debug (use_stmt))
6823 continue;
6824 while (is_gimple_assign (use_stmt)
6825 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6826 && single_imm_use (gimple_assign_lhs (use_stmt),
6827 &use2_p, &use_stmt2))
6828 use_stmt = use_stmt2;
6829 if (gimple_code (use_stmt) != GIMPLE_COND
6830 || gimple_bb (use_stmt) != cond_bb)
6831 return false;
6833 return true;
6836 /* Handle
6837 _4 = x_3 & 31;
6838 if (_4 != 0)
6839 goto <bb 6>;
6840 else
6841 goto <bb 7>;
6842 <bb 6>:
6843 __builtin_unreachable ();
6844 <bb 7>:
6845 x_5 = ASSERT_EXPR <x_3, ...>;
6846 If x_3 has no other immediate uses (checked by caller),
6847 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6848 from the non-zero bitmask. */
6850 static void
6851 maybe_set_nonzero_bits (basic_block bb, tree var)
6853 edge e = single_pred_edge (bb);
6854 basic_block cond_bb = e->src;
6855 gimple stmt = last_stmt (cond_bb);
6856 tree cst;
6858 if (stmt == NULL
6859 || gimple_code (stmt) != GIMPLE_COND
6860 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6861 ? EQ_EXPR : NE_EXPR)
6862 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6863 || !integer_zerop (gimple_cond_rhs (stmt)))
6864 return;
6866 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6867 if (!is_gimple_assign (stmt)
6868 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6869 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6870 return;
6871 if (gimple_assign_rhs1 (stmt) != var)
6873 gimple stmt2;
6875 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6876 return;
6877 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6878 if (!gimple_assign_cast_p (stmt2)
6879 || gimple_assign_rhs1 (stmt2) != var
6880 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6881 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6882 != TYPE_PRECISION (TREE_TYPE (var))))
6883 return;
6885 cst = gimple_assign_rhs2 (stmt);
6886 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6889 /* Convert range assertion expressions into the implied copies and
6890 copy propagate away the copies. Doing the trivial copy propagation
6891 here avoids the need to run the full copy propagation pass after
6892 VRP.
6894 FIXME, this will eventually lead to copy propagation removing the
6895 names that had useful range information attached to them. For
6896 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6897 then N_i will have the range [3, +INF].
6899 However, by converting the assertion into the implied copy
6900 operation N_i = N_j, we will then copy-propagate N_j into the uses
6901 of N_i and lose the range information. We may want to hold on to
6902 ASSERT_EXPRs a little while longer as the ranges could be used in
6903 things like jump threading.
6905 The problem with keeping ASSERT_EXPRs around is that passes after
6906 VRP need to handle them appropriately.
6908 Another approach would be to make the range information a first
6909 class property of the SSA_NAME so that it can be queried from
6910 any pass. This is made somewhat more complex by the need for
6911 multiple ranges to be associated with one SSA_NAME. */
6913 static void
6914 remove_range_assertions (void)
6916 basic_block bb;
6917 gimple_stmt_iterator si;
6918 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6919 a basic block preceeded by GIMPLE_COND branching to it and
6920 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6921 int is_unreachable;
6923 /* Note that the BSI iterator bump happens at the bottom of the
6924 loop and no bump is necessary if we're removing the statement
6925 referenced by the current BSI. */
6926 FOR_EACH_BB_FN (bb, cfun)
6927 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6929 gimple stmt = gsi_stmt (si);
6930 gimple use_stmt;
6932 if (is_gimple_assign (stmt)
6933 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6935 tree lhs = gimple_assign_lhs (stmt);
6936 tree rhs = gimple_assign_rhs1 (stmt);
6937 tree var;
6938 tree cond = fold (ASSERT_EXPR_COND (rhs));
6939 use_operand_p use_p;
6940 imm_use_iterator iter;
6942 gcc_assert (cond != boolean_false_node);
6944 var = ASSERT_EXPR_VAR (rhs);
6945 gcc_assert (TREE_CODE (var) == SSA_NAME);
6947 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6948 && SSA_NAME_RANGE_INFO (lhs))
6950 if (is_unreachable == -1)
6952 is_unreachable = 0;
6953 if (single_pred_p (bb)
6954 && assert_unreachable_fallthru_edge_p
6955 (single_pred_edge (bb)))
6956 is_unreachable = 1;
6958 /* Handle
6959 if (x_7 >= 10 && x_7 < 20)
6960 __builtin_unreachable ();
6961 x_8 = ASSERT_EXPR <x_7, ...>;
6962 if the only uses of x_7 are in the ASSERT_EXPR and
6963 in the condition. In that case, we can copy the
6964 range info from x_8 computed in this pass also
6965 for x_7. */
6966 if (is_unreachable
6967 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6968 single_pred (bb)))
6970 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6971 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6972 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6973 maybe_set_nonzero_bits (bb, var);
6977 /* Propagate the RHS into every use of the LHS. */
6978 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6979 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6980 SET_USE (use_p, var);
6982 /* And finally, remove the copy, it is not needed. */
6983 gsi_remove (&si, true);
6984 release_defs (stmt);
6986 else
6988 if (!is_gimple_debug (gsi_stmt (si)))
6989 is_unreachable = 0;
6990 gsi_next (&si);
6996 /* Return true if STMT is interesting for VRP. */
6998 static bool
6999 stmt_interesting_for_vrp (gimple stmt)
7001 if (gimple_code (stmt) == GIMPLE_PHI)
7003 tree res = gimple_phi_result (stmt);
7004 return (!virtual_operand_p (res)
7005 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
7006 || POINTER_TYPE_P (TREE_TYPE (res))));
7008 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7010 tree lhs = gimple_get_lhs (stmt);
7012 /* In general, assignments with virtual operands are not useful
7013 for deriving ranges, with the obvious exception of calls to
7014 builtin functions. */
7015 if (lhs && TREE_CODE (lhs) == SSA_NAME
7016 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7017 || POINTER_TYPE_P (TREE_TYPE (lhs)))
7018 && (is_gimple_call (stmt)
7019 || !gimple_vuse (stmt)))
7020 return true;
7021 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7022 switch (gimple_call_internal_fn (stmt))
7024 case IFN_ADD_OVERFLOW:
7025 case IFN_SUB_OVERFLOW:
7026 case IFN_MUL_OVERFLOW:
7027 /* These internal calls return _Complex integer type,
7028 but are interesting to VRP nevertheless. */
7029 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7030 return true;
7031 break;
7032 default:
7033 break;
7036 else if (gimple_code (stmt) == GIMPLE_COND
7037 || gimple_code (stmt) == GIMPLE_SWITCH)
7038 return true;
7040 return false;
7044 /* Initialize local data structures for VRP. */
7046 static void
7047 vrp_initialize (void)
7049 basic_block bb;
7051 values_propagated = false;
7052 num_vr_values = num_ssa_names;
7053 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
7054 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
7056 FOR_EACH_BB_FN (bb, cfun)
7058 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7059 gsi_next (&si))
7061 gphi *phi = si.phi ();
7062 if (!stmt_interesting_for_vrp (phi))
7064 tree lhs = PHI_RESULT (phi);
7065 set_value_range_to_varying (get_value_range (lhs));
7066 prop_set_simulate_again (phi, false);
7068 else
7069 prop_set_simulate_again (phi, true);
7072 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7073 gsi_next (&si))
7075 gimple stmt = gsi_stmt (si);
7077 /* If the statement is a control insn, then we do not
7078 want to avoid simulating the statement once. Failure
7079 to do so means that those edges will never get added. */
7080 if (stmt_ends_bb_p (stmt))
7081 prop_set_simulate_again (stmt, true);
7082 else if (!stmt_interesting_for_vrp (stmt))
7084 ssa_op_iter i;
7085 tree def;
7086 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7087 set_value_range_to_varying (get_value_range (def));
7088 prop_set_simulate_again (stmt, false);
7090 else
7091 prop_set_simulate_again (stmt, true);
7096 /* Return the singleton value-range for NAME or NAME. */
7098 static inline tree
7099 vrp_valueize (tree name)
7101 if (TREE_CODE (name) == SSA_NAME)
7103 value_range_t *vr = get_value_range (name);
7104 if (vr->type == VR_RANGE
7105 && (vr->min == vr->max
7106 || operand_equal_p (vr->min, vr->max, 0)))
7107 return vr->min;
7109 return name;
7112 /* Return the singleton value-range for NAME if that is a constant
7113 but signal to not follow SSA edges. */
7115 static inline tree
7116 vrp_valueize_1 (tree name)
7118 if (TREE_CODE (name) == SSA_NAME)
7120 /* If the definition may be simulated again we cannot follow
7121 this SSA edge as the SSA propagator does not necessarily
7122 re-visit the use. */
7123 gimple def_stmt = SSA_NAME_DEF_STMT (name);
7124 if (!gimple_nop_p (def_stmt)
7125 && prop_simulate_again_p (def_stmt))
7126 return NULL_TREE;
7127 value_range_t *vr = get_value_range (name);
7128 if (range_int_cst_singleton_p (vr))
7129 return vr->min;
7131 return name;
7134 /* Visit assignment STMT. If it produces an interesting range, record
7135 the SSA name in *OUTPUT_P. */
7137 static enum ssa_prop_result
7138 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
7140 tree def, lhs;
7141 ssa_op_iter iter;
7142 enum gimple_code code = gimple_code (stmt);
7143 lhs = gimple_get_lhs (stmt);
7145 /* We only keep track of ranges in integral and pointer types. */
7146 if (TREE_CODE (lhs) == SSA_NAME
7147 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7148 /* It is valid to have NULL MIN/MAX values on a type. See
7149 build_range_type. */
7150 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7151 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7152 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7154 value_range_t new_vr = VR_INITIALIZER;
7156 /* Try folding the statement to a constant first. */
7157 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7158 vrp_valueize_1);
7159 if (tem && is_gimple_min_invariant (tem))
7160 set_value_range_to_value (&new_vr, tem, NULL);
7161 /* Then dispatch to value-range extracting functions. */
7162 else if (code == GIMPLE_CALL)
7163 extract_range_basic (&new_vr, stmt);
7164 else
7165 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7167 if (update_value_range (lhs, &new_vr))
7169 *output_p = lhs;
7171 if (dump_file && (dump_flags & TDF_DETAILS))
7173 fprintf (dump_file, "Found new range for ");
7174 print_generic_expr (dump_file, lhs, 0);
7175 fprintf (dump_file, ": ");
7176 dump_value_range (dump_file, &new_vr);
7177 fprintf (dump_file, "\n");
7180 if (new_vr.type == VR_VARYING)
7181 return SSA_PROP_VARYING;
7183 return SSA_PROP_INTERESTING;
7186 return SSA_PROP_NOT_INTERESTING;
7188 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7189 switch (gimple_call_internal_fn (stmt))
7191 case IFN_ADD_OVERFLOW:
7192 case IFN_SUB_OVERFLOW:
7193 case IFN_MUL_OVERFLOW:
7194 /* These internal calls return _Complex integer type,
7195 which VRP does not track, but the immediate uses
7196 thereof might be interesting. */
7197 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7199 imm_use_iterator iter;
7200 use_operand_p use_p;
7201 enum ssa_prop_result res = SSA_PROP_VARYING;
7203 set_value_range_to_varying (get_value_range (lhs));
7205 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7207 gimple use_stmt = USE_STMT (use_p);
7208 if (!is_gimple_assign (use_stmt))
7209 continue;
7210 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7211 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7212 continue;
7213 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7214 tree use_lhs = gimple_assign_lhs (use_stmt);
7215 if (TREE_CODE (rhs1) != rhs_code
7216 || TREE_OPERAND (rhs1, 0) != lhs
7217 || TREE_CODE (use_lhs) != SSA_NAME
7218 || !stmt_interesting_for_vrp (use_stmt)
7219 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7220 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7221 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7222 continue;
7224 /* If there is a change in the value range for any of the
7225 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7226 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7227 or IMAGPART_EXPR immediate uses, but none of them have
7228 a change in their value ranges, return
7229 SSA_PROP_NOT_INTERESTING. If there are no
7230 {REAL,IMAG}PART_EXPR uses at all,
7231 return SSA_PROP_VARYING. */
7232 value_range_t new_vr = VR_INITIALIZER;
7233 extract_range_basic (&new_vr, use_stmt);
7234 value_range_t *old_vr = get_value_range (use_lhs);
7235 if (old_vr->type != new_vr.type
7236 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7237 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7238 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7239 res = SSA_PROP_INTERESTING;
7240 else
7241 res = SSA_PROP_NOT_INTERESTING;
7242 BITMAP_FREE (new_vr.equiv);
7243 if (res == SSA_PROP_INTERESTING)
7245 *output_p = lhs;
7246 return res;
7250 return res;
7252 break;
7253 default:
7254 break;
7257 /* Every other statement produces no useful ranges. */
7258 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7259 set_value_range_to_varying (get_value_range (def));
7261 return SSA_PROP_VARYING;
7264 /* Helper that gets the value range of the SSA_NAME with version I
7265 or a symbolic range containing the SSA_NAME only if the value range
7266 is varying or undefined. */
7268 static inline value_range_t
7269 get_vr_for_comparison (int i)
7271 value_range_t vr = *get_value_range (ssa_name (i));
7273 /* If name N_i does not have a valid range, use N_i as its own
7274 range. This allows us to compare against names that may
7275 have N_i in their ranges. */
7276 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7278 vr.type = VR_RANGE;
7279 vr.min = ssa_name (i);
7280 vr.max = ssa_name (i);
7283 return vr;
7286 /* Compare all the value ranges for names equivalent to VAR with VAL
7287 using comparison code COMP. Return the same value returned by
7288 compare_range_with_value, including the setting of
7289 *STRICT_OVERFLOW_P. */
7291 static tree
7292 compare_name_with_value (enum tree_code comp, tree var, tree val,
7293 bool *strict_overflow_p)
7295 bitmap_iterator bi;
7296 unsigned i;
7297 bitmap e;
7298 tree retval, t;
7299 int used_strict_overflow;
7300 bool sop;
7301 value_range_t equiv_vr;
7303 /* Get the set of equivalences for VAR. */
7304 e = get_value_range (var)->equiv;
7306 /* Start at -1. Set it to 0 if we do a comparison without relying
7307 on overflow, or 1 if all comparisons rely on overflow. */
7308 used_strict_overflow = -1;
7310 /* Compare vars' value range with val. */
7311 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7312 sop = false;
7313 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7314 if (retval)
7315 used_strict_overflow = sop ? 1 : 0;
7317 /* If the equiv set is empty we have done all work we need to do. */
7318 if (e == NULL)
7320 if (retval
7321 && used_strict_overflow > 0)
7322 *strict_overflow_p = true;
7323 return retval;
7326 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7328 equiv_vr = get_vr_for_comparison (i);
7329 sop = false;
7330 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7331 if (t)
7333 /* If we get different answers from different members
7334 of the equivalence set this check must be in a dead
7335 code region. Folding it to a trap representation
7336 would be correct here. For now just return don't-know. */
7337 if (retval != NULL
7338 && t != retval)
7340 retval = NULL_TREE;
7341 break;
7343 retval = t;
7345 if (!sop)
7346 used_strict_overflow = 0;
7347 else if (used_strict_overflow < 0)
7348 used_strict_overflow = 1;
7352 if (retval
7353 && used_strict_overflow > 0)
7354 *strict_overflow_p = true;
7356 return retval;
7360 /* Given a comparison code COMP and names N1 and N2, compare all the
7361 ranges equivalent to N1 against all the ranges equivalent to N2
7362 to determine the value of N1 COMP N2. Return the same value
7363 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7364 whether we relied on an overflow infinity in the comparison. */
7367 static tree
7368 compare_names (enum tree_code comp, tree n1, tree n2,
7369 bool *strict_overflow_p)
7371 tree t, retval;
7372 bitmap e1, e2;
7373 bitmap_iterator bi1, bi2;
7374 unsigned i1, i2;
7375 int used_strict_overflow;
7376 static bitmap_obstack *s_obstack = NULL;
7377 static bitmap s_e1 = NULL, s_e2 = NULL;
7379 /* Compare the ranges of every name equivalent to N1 against the
7380 ranges of every name equivalent to N2. */
7381 e1 = get_value_range (n1)->equiv;
7382 e2 = get_value_range (n2)->equiv;
7384 /* Use the fake bitmaps if e1 or e2 are not available. */
7385 if (s_obstack == NULL)
7387 s_obstack = XNEW (bitmap_obstack);
7388 bitmap_obstack_initialize (s_obstack);
7389 s_e1 = BITMAP_ALLOC (s_obstack);
7390 s_e2 = BITMAP_ALLOC (s_obstack);
7392 if (e1 == NULL)
7393 e1 = s_e1;
7394 if (e2 == NULL)
7395 e2 = s_e2;
7397 /* Add N1 and N2 to their own set of equivalences to avoid
7398 duplicating the body of the loop just to check N1 and N2
7399 ranges. */
7400 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7401 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7403 /* If the equivalence sets have a common intersection, then the two
7404 names can be compared without checking their ranges. */
7405 if (bitmap_intersect_p (e1, e2))
7407 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7408 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7410 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7411 ? boolean_true_node
7412 : boolean_false_node;
7415 /* Start at -1. Set it to 0 if we do a comparison without relying
7416 on overflow, or 1 if all comparisons rely on overflow. */
7417 used_strict_overflow = -1;
7419 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7420 N2 to their own set of equivalences to avoid duplicating the body
7421 of the loop just to check N1 and N2 ranges. */
7422 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7424 value_range_t vr1 = get_vr_for_comparison (i1);
7426 t = retval = NULL_TREE;
7427 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7429 bool sop = false;
7431 value_range_t vr2 = get_vr_for_comparison (i2);
7433 t = compare_ranges (comp, &vr1, &vr2, &sop);
7434 if (t)
7436 /* If we get different answers from different members
7437 of the equivalence set this check must be in a dead
7438 code region. Folding it to a trap representation
7439 would be correct here. For now just return don't-know. */
7440 if (retval != NULL
7441 && t != retval)
7443 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7444 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7445 return NULL_TREE;
7447 retval = t;
7449 if (!sop)
7450 used_strict_overflow = 0;
7451 else if (used_strict_overflow < 0)
7452 used_strict_overflow = 1;
7456 if (retval)
7458 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7459 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7460 if (used_strict_overflow > 0)
7461 *strict_overflow_p = true;
7462 return retval;
7466 /* None of the equivalent ranges are useful in computing this
7467 comparison. */
7468 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7469 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7470 return NULL_TREE;
7473 /* Helper function for vrp_evaluate_conditional_warnv. */
7475 static tree
7476 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7477 tree op0, tree op1,
7478 bool * strict_overflow_p)
7480 value_range_t *vr0, *vr1;
7482 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7483 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7485 tree res = NULL_TREE;
7486 if (vr0 && vr1)
7487 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7488 if (!res && vr0)
7489 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7490 if (!res && vr1)
7491 res = (compare_range_with_value
7492 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7493 return res;
7496 /* Helper function for vrp_evaluate_conditional_warnv. */
7498 static tree
7499 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7500 tree op1, bool use_equiv_p,
7501 bool *strict_overflow_p, bool *only_ranges)
7503 tree ret;
7504 if (only_ranges)
7505 *only_ranges = true;
7507 /* We only deal with integral and pointer types. */
7508 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7509 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7510 return NULL_TREE;
7512 if (use_equiv_p)
7514 if (only_ranges
7515 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7516 (code, op0, op1, strict_overflow_p)))
7517 return ret;
7518 *only_ranges = false;
7519 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7520 return compare_names (code, op0, op1, strict_overflow_p);
7521 else if (TREE_CODE (op0) == SSA_NAME)
7522 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7523 else if (TREE_CODE (op1) == SSA_NAME)
7524 return (compare_name_with_value
7525 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7527 else
7528 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7529 strict_overflow_p);
7530 return NULL_TREE;
7533 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7534 information. Return NULL if the conditional can not be evaluated.
7535 The ranges of all the names equivalent with the operands in COND
7536 will be used when trying to compute the value. If the result is
7537 based on undefined signed overflow, issue a warning if
7538 appropriate. */
7540 static tree
7541 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7543 bool sop;
7544 tree ret;
7545 bool only_ranges;
7547 /* Some passes and foldings leak constants with overflow flag set
7548 into the IL. Avoid doing wrong things with these and bail out. */
7549 if ((TREE_CODE (op0) == INTEGER_CST
7550 && TREE_OVERFLOW (op0))
7551 || (TREE_CODE (op1) == INTEGER_CST
7552 && TREE_OVERFLOW (op1)))
7553 return NULL_TREE;
7555 sop = false;
7556 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7557 &only_ranges);
7559 if (ret && sop)
7561 enum warn_strict_overflow_code wc;
7562 const char* warnmsg;
7564 if (is_gimple_min_invariant (ret))
7566 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7567 warnmsg = G_("assuming signed overflow does not occur when "
7568 "simplifying conditional to constant");
7570 else
7572 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7573 warnmsg = G_("assuming signed overflow does not occur when "
7574 "simplifying conditional");
7577 if (issue_strict_overflow_warning (wc))
7579 location_t location;
7581 if (!gimple_has_location (stmt))
7582 location = input_location;
7583 else
7584 location = gimple_location (stmt);
7585 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7589 if (warn_type_limits
7590 && ret && only_ranges
7591 && TREE_CODE_CLASS (code) == tcc_comparison
7592 && TREE_CODE (op0) == SSA_NAME)
7594 /* If the comparison is being folded and the operand on the LHS
7595 is being compared against a constant value that is outside of
7596 the natural range of OP0's type, then the predicate will
7597 always fold regardless of the value of OP0. If -Wtype-limits
7598 was specified, emit a warning. */
7599 tree type = TREE_TYPE (op0);
7600 value_range_t *vr0 = get_value_range (op0);
7602 if (vr0->type == VR_RANGE
7603 && INTEGRAL_TYPE_P (type)
7604 && vrp_val_is_min (vr0->min)
7605 && vrp_val_is_max (vr0->max)
7606 && is_gimple_min_invariant (op1))
7608 location_t location;
7610 if (!gimple_has_location (stmt))
7611 location = input_location;
7612 else
7613 location = gimple_location (stmt);
7615 warning_at (location, OPT_Wtype_limits,
7616 integer_zerop (ret)
7617 ? G_("comparison always false "
7618 "due to limited range of data type")
7619 : G_("comparison always true "
7620 "due to limited range of data type"));
7624 return ret;
7628 /* Visit conditional statement STMT. If we can determine which edge
7629 will be taken out of STMT's basic block, record it in
7630 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7631 SSA_PROP_VARYING. */
7633 static enum ssa_prop_result
7634 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7636 tree val;
7637 bool sop;
7639 *taken_edge_p = NULL;
7641 if (dump_file && (dump_flags & TDF_DETAILS))
7643 tree use;
7644 ssa_op_iter i;
7646 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7647 print_gimple_stmt (dump_file, stmt, 0, 0);
7648 fprintf (dump_file, "\nWith known ranges\n");
7650 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7652 fprintf (dump_file, "\t");
7653 print_generic_expr (dump_file, use, 0);
7654 fprintf (dump_file, ": ");
7655 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7658 fprintf (dump_file, "\n");
7661 /* Compute the value of the predicate COND by checking the known
7662 ranges of each of its operands.
7664 Note that we cannot evaluate all the equivalent ranges here
7665 because those ranges may not yet be final and with the current
7666 propagation strategy, we cannot determine when the value ranges
7667 of the names in the equivalence set have changed.
7669 For instance, given the following code fragment
7671 i_5 = PHI <8, i_13>
7673 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7674 if (i_14 == 1)
7677 Assume that on the first visit to i_14, i_5 has the temporary
7678 range [8, 8] because the second argument to the PHI function is
7679 not yet executable. We derive the range ~[0, 0] for i_14 and the
7680 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7681 the first time, since i_14 is equivalent to the range [8, 8], we
7682 determine that the predicate is always false.
7684 On the next round of propagation, i_13 is determined to be
7685 VARYING, which causes i_5 to drop down to VARYING. So, another
7686 visit to i_14 is scheduled. In this second visit, we compute the
7687 exact same range and equivalence set for i_14, namely ~[0, 0] and
7688 { i_5 }. But we did not have the previous range for i_5
7689 registered, so vrp_visit_assignment thinks that the range for
7690 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7691 is not visited again, which stops propagation from visiting
7692 statements in the THEN clause of that if().
7694 To properly fix this we would need to keep the previous range
7695 value for the names in the equivalence set. This way we would've
7696 discovered that from one visit to the other i_5 changed from
7697 range [8, 8] to VR_VARYING.
7699 However, fixing this apparent limitation may not be worth the
7700 additional checking. Testing on several code bases (GCC, DLV,
7701 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7702 4 more predicates folded in SPEC. */
7703 sop = false;
7705 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7706 gimple_cond_lhs (stmt),
7707 gimple_cond_rhs (stmt),
7708 false, &sop, NULL);
7709 if (val)
7711 if (!sop)
7712 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7713 else
7715 if (dump_file && (dump_flags & TDF_DETAILS))
7716 fprintf (dump_file,
7717 "\nIgnoring predicate evaluation because "
7718 "it assumes that signed overflow is undefined");
7719 val = NULL_TREE;
7723 if (dump_file && (dump_flags & TDF_DETAILS))
7725 fprintf (dump_file, "\nPredicate evaluates to: ");
7726 if (val == NULL_TREE)
7727 fprintf (dump_file, "DON'T KNOW\n");
7728 else
7729 print_generic_stmt (dump_file, val, 0);
7732 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7735 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7736 that includes the value VAL. The search is restricted to the range
7737 [START_IDX, n - 1] where n is the size of VEC.
7739 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7740 returned.
7742 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7743 it is placed in IDX and false is returned.
7745 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7746 returned. */
7748 static bool
7749 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7751 size_t n = gimple_switch_num_labels (stmt);
7752 size_t low, high;
7754 /* Find case label for minimum of the value range or the next one.
7755 At each iteration we are searching in [low, high - 1]. */
7757 for (low = start_idx, high = n; high != low; )
7759 tree t;
7760 int cmp;
7761 /* Note that i != high, so we never ask for n. */
7762 size_t i = (high + low) / 2;
7763 t = gimple_switch_label (stmt, i);
7765 /* Cache the result of comparing CASE_LOW and val. */
7766 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7768 if (cmp == 0)
7770 /* Ranges cannot be empty. */
7771 *idx = i;
7772 return true;
7774 else if (cmp > 0)
7775 high = i;
7776 else
7778 low = i + 1;
7779 if (CASE_HIGH (t) != NULL
7780 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7782 *idx = i;
7783 return true;
7788 *idx = high;
7789 return false;
7792 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7793 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7794 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7795 then MAX_IDX < MIN_IDX.
7796 Returns true if the default label is not needed. */
7798 static bool
7799 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7800 size_t *max_idx)
7802 size_t i, j;
7803 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7804 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7806 if (i == j
7807 && min_take_default
7808 && max_take_default)
7810 /* Only the default case label reached.
7811 Return an empty range. */
7812 *min_idx = 1;
7813 *max_idx = 0;
7814 return false;
7816 else
7818 bool take_default = min_take_default || max_take_default;
7819 tree low, high;
7820 size_t k;
7822 if (max_take_default)
7823 j--;
7825 /* If the case label range is continuous, we do not need
7826 the default case label. Verify that. */
7827 high = CASE_LOW (gimple_switch_label (stmt, i));
7828 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7829 high = CASE_HIGH (gimple_switch_label (stmt, i));
7830 for (k = i + 1; k <= j; ++k)
7832 low = CASE_LOW (gimple_switch_label (stmt, k));
7833 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7835 take_default = true;
7836 break;
7838 high = low;
7839 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7840 high = CASE_HIGH (gimple_switch_label (stmt, k));
7843 *min_idx = i;
7844 *max_idx = j;
7845 return !take_default;
7849 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7850 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7851 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7852 Returns true if the default label is not needed. */
7854 static bool
7855 find_case_label_ranges (gswitch *stmt, value_range_t *vr, size_t *min_idx1,
7856 size_t *max_idx1, size_t *min_idx2,
7857 size_t *max_idx2)
7859 size_t i, j, k, l;
7860 unsigned int n = gimple_switch_num_labels (stmt);
7861 bool take_default;
7862 tree case_low, case_high;
7863 tree min = vr->min, max = vr->max;
7865 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7867 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7869 /* Set second range to emtpy. */
7870 *min_idx2 = 1;
7871 *max_idx2 = 0;
7873 if (vr->type == VR_RANGE)
7875 *min_idx1 = i;
7876 *max_idx1 = j;
7877 return !take_default;
7880 /* Set first range to all case labels. */
7881 *min_idx1 = 1;
7882 *max_idx1 = n - 1;
7884 if (i > j)
7885 return false;
7887 /* Make sure all the values of case labels [i , j] are contained in
7888 range [MIN, MAX]. */
7889 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7890 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7891 if (tree_int_cst_compare (case_low, min) < 0)
7892 i += 1;
7893 if (case_high != NULL_TREE
7894 && tree_int_cst_compare (max, case_high) < 0)
7895 j -= 1;
7897 if (i > j)
7898 return false;
7900 /* If the range spans case labels [i, j], the corresponding anti-range spans
7901 the labels [1, i - 1] and [j + 1, n - 1]. */
7902 k = j + 1;
7903 l = n - 1;
7904 if (k > l)
7906 k = 1;
7907 l = 0;
7910 j = i - 1;
7911 i = 1;
7912 if (i > j)
7914 i = k;
7915 j = l;
7916 k = 1;
7917 l = 0;
7920 *min_idx1 = i;
7921 *max_idx1 = j;
7922 *min_idx2 = k;
7923 *max_idx2 = l;
7924 return false;
7927 /* Visit switch statement STMT. If we can determine which edge
7928 will be taken out of STMT's basic block, record it in
7929 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7930 SSA_PROP_VARYING. */
7932 static enum ssa_prop_result
7933 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7935 tree op, val;
7936 value_range_t *vr;
7937 size_t i = 0, j = 0, k, l;
7938 bool take_default;
7940 *taken_edge_p = NULL;
7941 op = gimple_switch_index (stmt);
7942 if (TREE_CODE (op) != SSA_NAME)
7943 return SSA_PROP_VARYING;
7945 vr = get_value_range (op);
7946 if (dump_file && (dump_flags & TDF_DETAILS))
7948 fprintf (dump_file, "\nVisiting switch expression with operand ");
7949 print_generic_expr (dump_file, op, 0);
7950 fprintf (dump_file, " with known range ");
7951 dump_value_range (dump_file, vr);
7952 fprintf (dump_file, "\n");
7955 if ((vr->type != VR_RANGE
7956 && vr->type != VR_ANTI_RANGE)
7957 || symbolic_range_p (vr))
7958 return SSA_PROP_VARYING;
7960 /* Find the single edge that is taken from the switch expression. */
7961 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7963 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7964 label */
7965 if (j < i)
7967 gcc_assert (take_default);
7968 val = gimple_switch_default_label (stmt);
7970 else
7972 /* Check if labels with index i to j and maybe the default label
7973 are all reaching the same label. */
7975 val = gimple_switch_label (stmt, i);
7976 if (take_default
7977 && CASE_LABEL (gimple_switch_default_label (stmt))
7978 != CASE_LABEL (val))
7980 if (dump_file && (dump_flags & TDF_DETAILS))
7981 fprintf (dump_file, " not a single destination for this "
7982 "range\n");
7983 return SSA_PROP_VARYING;
7985 for (++i; i <= j; ++i)
7987 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7989 if (dump_file && (dump_flags & TDF_DETAILS))
7990 fprintf (dump_file, " not a single destination for this "
7991 "range\n");
7992 return SSA_PROP_VARYING;
7995 for (; k <= l; ++k)
7997 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7999 if (dump_file && (dump_flags & TDF_DETAILS))
8000 fprintf (dump_file, " not a single destination for this "
8001 "range\n");
8002 return SSA_PROP_VARYING;
8007 *taken_edge_p = find_edge (gimple_bb (stmt),
8008 label_to_block (CASE_LABEL (val)));
8010 if (dump_file && (dump_flags & TDF_DETAILS))
8012 fprintf (dump_file, " will take edge to ");
8013 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
8016 return SSA_PROP_INTERESTING;
8020 /* Evaluate statement STMT. If the statement produces a useful range,
8021 return SSA_PROP_INTERESTING and record the SSA name with the
8022 interesting range into *OUTPUT_P.
8024 If STMT is a conditional branch and we can determine its truth
8025 value, the taken edge is recorded in *TAKEN_EDGE_P.
8027 If STMT produces a varying value, return SSA_PROP_VARYING. */
8029 static enum ssa_prop_result
8030 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
8032 tree def;
8033 ssa_op_iter iter;
8035 if (dump_file && (dump_flags & TDF_DETAILS))
8037 fprintf (dump_file, "\nVisiting statement:\n");
8038 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
8041 if (!stmt_interesting_for_vrp (stmt))
8042 gcc_assert (stmt_ends_bb_p (stmt));
8043 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
8044 return vrp_visit_assignment_or_call (stmt, output_p);
8045 else if (gimple_code (stmt) == GIMPLE_COND)
8046 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
8047 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8048 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
8050 /* All other statements produce nothing of interest for VRP, so mark
8051 their outputs varying and prevent further simulation. */
8052 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
8053 set_value_range_to_varying (get_value_range (def));
8055 return SSA_PROP_VARYING;
8058 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8059 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8060 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8061 possible such range. The resulting range is not canonicalized. */
8063 static void
8064 union_ranges (enum value_range_type *vr0type,
8065 tree *vr0min, tree *vr0max,
8066 enum value_range_type vr1type,
8067 tree vr1min, tree vr1max)
8069 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8070 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8072 /* [] is vr0, () is vr1 in the following classification comments. */
8073 if (mineq && maxeq)
8075 /* [( )] */
8076 if (*vr0type == vr1type)
8077 /* Nothing to do for equal ranges. */
8079 else if ((*vr0type == VR_RANGE
8080 && vr1type == VR_ANTI_RANGE)
8081 || (*vr0type == VR_ANTI_RANGE
8082 && vr1type == VR_RANGE))
8084 /* For anti-range with range union the result is varying. */
8085 goto give_up;
8087 else
8088 gcc_unreachable ();
8090 else if (operand_less_p (*vr0max, vr1min) == 1
8091 || operand_less_p (vr1max, *vr0min) == 1)
8093 /* [ ] ( ) or ( ) [ ]
8094 If the ranges have an empty intersection, result of the union
8095 operation is the anti-range or if both are anti-ranges
8096 it covers all. */
8097 if (*vr0type == VR_ANTI_RANGE
8098 && vr1type == VR_ANTI_RANGE)
8099 goto give_up;
8100 else if (*vr0type == VR_ANTI_RANGE
8101 && vr1type == VR_RANGE)
8103 else if (*vr0type == VR_RANGE
8104 && vr1type == VR_ANTI_RANGE)
8106 *vr0type = vr1type;
8107 *vr0min = vr1min;
8108 *vr0max = vr1max;
8110 else if (*vr0type == VR_RANGE
8111 && vr1type == VR_RANGE)
8113 /* The result is the convex hull of both ranges. */
8114 if (operand_less_p (*vr0max, vr1min) == 1)
8116 /* If the result can be an anti-range, create one. */
8117 if (TREE_CODE (*vr0max) == INTEGER_CST
8118 && TREE_CODE (vr1min) == INTEGER_CST
8119 && vrp_val_is_min (*vr0min)
8120 && vrp_val_is_max (vr1max))
8122 tree min = int_const_binop (PLUS_EXPR,
8123 *vr0max,
8124 build_int_cst (TREE_TYPE (*vr0max), 1));
8125 tree max = int_const_binop (MINUS_EXPR,
8126 vr1min,
8127 build_int_cst (TREE_TYPE (vr1min), 1));
8128 if (!operand_less_p (max, min))
8130 *vr0type = VR_ANTI_RANGE;
8131 *vr0min = min;
8132 *vr0max = max;
8134 else
8135 *vr0max = vr1max;
8137 else
8138 *vr0max = vr1max;
8140 else
8142 /* If the result can be an anti-range, create one. */
8143 if (TREE_CODE (vr1max) == INTEGER_CST
8144 && TREE_CODE (*vr0min) == INTEGER_CST
8145 && vrp_val_is_min (vr1min)
8146 && vrp_val_is_max (*vr0max))
8148 tree min = int_const_binop (PLUS_EXPR,
8149 vr1max,
8150 build_int_cst (TREE_TYPE (vr1max), 1));
8151 tree max = int_const_binop (MINUS_EXPR,
8152 *vr0min,
8153 build_int_cst (TREE_TYPE (*vr0min), 1));
8154 if (!operand_less_p (max, min))
8156 *vr0type = VR_ANTI_RANGE;
8157 *vr0min = min;
8158 *vr0max = max;
8160 else
8161 *vr0min = vr1min;
8163 else
8164 *vr0min = vr1min;
8167 else
8168 gcc_unreachable ();
8170 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8171 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8173 /* [ ( ) ] or [( ) ] or [ ( )] */
8174 if (*vr0type == VR_RANGE
8175 && vr1type == VR_RANGE)
8177 else if (*vr0type == VR_ANTI_RANGE
8178 && vr1type == VR_ANTI_RANGE)
8180 *vr0type = vr1type;
8181 *vr0min = vr1min;
8182 *vr0max = vr1max;
8184 else if (*vr0type == VR_ANTI_RANGE
8185 && vr1type == VR_RANGE)
8187 /* Arbitrarily choose the right or left gap. */
8188 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8189 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8190 build_int_cst (TREE_TYPE (vr1min), 1));
8191 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8192 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8193 build_int_cst (TREE_TYPE (vr1max), 1));
8194 else
8195 goto give_up;
8197 else if (*vr0type == VR_RANGE
8198 && vr1type == VR_ANTI_RANGE)
8199 /* The result covers everything. */
8200 goto give_up;
8201 else
8202 gcc_unreachable ();
8204 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8205 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8207 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8208 if (*vr0type == VR_RANGE
8209 && vr1type == VR_RANGE)
8211 *vr0type = vr1type;
8212 *vr0min = vr1min;
8213 *vr0max = vr1max;
8215 else if (*vr0type == VR_ANTI_RANGE
8216 && vr1type == VR_ANTI_RANGE)
8218 else if (*vr0type == VR_RANGE
8219 && vr1type == VR_ANTI_RANGE)
8221 *vr0type = VR_ANTI_RANGE;
8222 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8224 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8225 build_int_cst (TREE_TYPE (*vr0min), 1));
8226 *vr0min = vr1min;
8228 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8230 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8231 build_int_cst (TREE_TYPE (*vr0max), 1));
8232 *vr0max = vr1max;
8234 else
8235 goto give_up;
8237 else if (*vr0type == VR_ANTI_RANGE
8238 && vr1type == VR_RANGE)
8239 /* The result covers everything. */
8240 goto give_up;
8241 else
8242 gcc_unreachable ();
8244 else if ((operand_less_p (vr1min, *vr0max) == 1
8245 || operand_equal_p (vr1min, *vr0max, 0))
8246 && operand_less_p (*vr0min, vr1min) == 1
8247 && operand_less_p (*vr0max, vr1max) == 1)
8249 /* [ ( ] ) or [ ]( ) */
8250 if (*vr0type == VR_RANGE
8251 && vr1type == VR_RANGE)
8252 *vr0max = vr1max;
8253 else if (*vr0type == VR_ANTI_RANGE
8254 && vr1type == VR_ANTI_RANGE)
8255 *vr0min = vr1min;
8256 else if (*vr0type == VR_ANTI_RANGE
8257 && vr1type == VR_RANGE)
8259 if (TREE_CODE (vr1min) == INTEGER_CST)
8260 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8261 build_int_cst (TREE_TYPE (vr1min), 1));
8262 else
8263 goto give_up;
8265 else if (*vr0type == VR_RANGE
8266 && vr1type == VR_ANTI_RANGE)
8268 if (TREE_CODE (*vr0max) == INTEGER_CST)
8270 *vr0type = vr1type;
8271 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8272 build_int_cst (TREE_TYPE (*vr0max), 1));
8273 *vr0max = vr1max;
8275 else
8276 goto give_up;
8278 else
8279 gcc_unreachable ();
8281 else if ((operand_less_p (*vr0min, vr1max) == 1
8282 || operand_equal_p (*vr0min, vr1max, 0))
8283 && operand_less_p (vr1min, *vr0min) == 1
8284 && operand_less_p (vr1max, *vr0max) == 1)
8286 /* ( [ ) ] or ( )[ ] */
8287 if (*vr0type == VR_RANGE
8288 && vr1type == VR_RANGE)
8289 *vr0min = vr1min;
8290 else if (*vr0type == VR_ANTI_RANGE
8291 && vr1type == VR_ANTI_RANGE)
8292 *vr0max = vr1max;
8293 else if (*vr0type == VR_ANTI_RANGE
8294 && vr1type == VR_RANGE)
8296 if (TREE_CODE (vr1max) == INTEGER_CST)
8297 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8298 build_int_cst (TREE_TYPE (vr1max), 1));
8299 else
8300 goto give_up;
8302 else if (*vr0type == VR_RANGE
8303 && vr1type == VR_ANTI_RANGE)
8305 if (TREE_CODE (*vr0min) == INTEGER_CST)
8307 *vr0type = vr1type;
8308 *vr0min = vr1min;
8309 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8310 build_int_cst (TREE_TYPE (*vr0min), 1));
8312 else
8313 goto give_up;
8315 else
8316 gcc_unreachable ();
8318 else
8319 goto give_up;
8321 return;
8323 give_up:
8324 *vr0type = VR_VARYING;
8325 *vr0min = NULL_TREE;
8326 *vr0max = NULL_TREE;
8329 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8330 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8331 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8332 possible such range. The resulting range is not canonicalized. */
8334 static void
8335 intersect_ranges (enum value_range_type *vr0type,
8336 tree *vr0min, tree *vr0max,
8337 enum value_range_type vr1type,
8338 tree vr1min, tree vr1max)
8340 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8341 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8343 /* [] is vr0, () is vr1 in the following classification comments. */
8344 if (mineq && maxeq)
8346 /* [( )] */
8347 if (*vr0type == vr1type)
8348 /* Nothing to do for equal ranges. */
8350 else if ((*vr0type == VR_RANGE
8351 && vr1type == VR_ANTI_RANGE)
8352 || (*vr0type == VR_ANTI_RANGE
8353 && vr1type == VR_RANGE))
8355 /* For anti-range with range intersection the result is empty. */
8356 *vr0type = VR_UNDEFINED;
8357 *vr0min = NULL_TREE;
8358 *vr0max = NULL_TREE;
8360 else
8361 gcc_unreachable ();
8363 else if (operand_less_p (*vr0max, vr1min) == 1
8364 || operand_less_p (vr1max, *vr0min) == 1)
8366 /* [ ] ( ) or ( ) [ ]
8367 If the ranges have an empty intersection, the result of the
8368 intersect operation is the range for intersecting an
8369 anti-range with a range or empty when intersecting two ranges. */
8370 if (*vr0type == VR_RANGE
8371 && vr1type == VR_ANTI_RANGE)
8373 else if (*vr0type == VR_ANTI_RANGE
8374 && vr1type == VR_RANGE)
8376 *vr0type = vr1type;
8377 *vr0min = vr1min;
8378 *vr0max = vr1max;
8380 else if (*vr0type == VR_RANGE
8381 && vr1type == VR_RANGE)
8383 *vr0type = VR_UNDEFINED;
8384 *vr0min = NULL_TREE;
8385 *vr0max = NULL_TREE;
8387 else if (*vr0type == VR_ANTI_RANGE
8388 && vr1type == VR_ANTI_RANGE)
8390 /* If the anti-ranges are adjacent to each other merge them. */
8391 if (TREE_CODE (*vr0max) == INTEGER_CST
8392 && TREE_CODE (vr1min) == INTEGER_CST
8393 && operand_less_p (*vr0max, vr1min) == 1
8394 && integer_onep (int_const_binop (MINUS_EXPR,
8395 vr1min, *vr0max)))
8396 *vr0max = vr1max;
8397 else if (TREE_CODE (vr1max) == INTEGER_CST
8398 && TREE_CODE (*vr0min) == INTEGER_CST
8399 && operand_less_p (vr1max, *vr0min) == 1
8400 && integer_onep (int_const_binop (MINUS_EXPR,
8401 *vr0min, vr1max)))
8402 *vr0min = vr1min;
8403 /* Else arbitrarily take VR0. */
8406 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8407 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8409 /* [ ( ) ] or [( ) ] or [ ( )] */
8410 if (*vr0type == VR_RANGE
8411 && vr1type == VR_RANGE)
8413 /* If both are ranges the result is the inner one. */
8414 *vr0type = vr1type;
8415 *vr0min = vr1min;
8416 *vr0max = vr1max;
8418 else if (*vr0type == VR_RANGE
8419 && vr1type == VR_ANTI_RANGE)
8421 /* Choose the right gap if the left one is empty. */
8422 if (mineq)
8424 if (TREE_CODE (vr1max) == INTEGER_CST)
8425 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8426 build_int_cst (TREE_TYPE (vr1max), 1));
8427 else
8428 *vr0min = vr1max;
8430 /* Choose the left gap if the right one is empty. */
8431 else if (maxeq)
8433 if (TREE_CODE (vr1min) == INTEGER_CST)
8434 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8435 build_int_cst (TREE_TYPE (vr1min), 1));
8436 else
8437 *vr0max = vr1min;
8439 /* Choose the anti-range if the range is effectively varying. */
8440 else if (vrp_val_is_min (*vr0min)
8441 && vrp_val_is_max (*vr0max))
8443 *vr0type = vr1type;
8444 *vr0min = vr1min;
8445 *vr0max = vr1max;
8447 /* Else choose the range. */
8449 else if (*vr0type == VR_ANTI_RANGE
8450 && vr1type == VR_ANTI_RANGE)
8451 /* If both are anti-ranges the result is the outer one. */
8453 else if (*vr0type == VR_ANTI_RANGE
8454 && vr1type == VR_RANGE)
8456 /* The intersection is empty. */
8457 *vr0type = VR_UNDEFINED;
8458 *vr0min = NULL_TREE;
8459 *vr0max = NULL_TREE;
8461 else
8462 gcc_unreachable ();
8464 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8465 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8467 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8468 if (*vr0type == VR_RANGE
8469 && vr1type == VR_RANGE)
8470 /* Choose the inner range. */
8472 else if (*vr0type == VR_ANTI_RANGE
8473 && vr1type == VR_RANGE)
8475 /* Choose the right gap if the left is empty. */
8476 if (mineq)
8478 *vr0type = VR_RANGE;
8479 if (TREE_CODE (*vr0max) == INTEGER_CST)
8480 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8481 build_int_cst (TREE_TYPE (*vr0max), 1));
8482 else
8483 *vr0min = *vr0max;
8484 *vr0max = vr1max;
8486 /* Choose the left gap if the right is empty. */
8487 else if (maxeq)
8489 *vr0type = VR_RANGE;
8490 if (TREE_CODE (*vr0min) == INTEGER_CST)
8491 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8492 build_int_cst (TREE_TYPE (*vr0min), 1));
8493 else
8494 *vr0max = *vr0min;
8495 *vr0min = vr1min;
8497 /* Choose the anti-range if the range is effectively varying. */
8498 else if (vrp_val_is_min (vr1min)
8499 && vrp_val_is_max (vr1max))
8501 /* Else choose the range. */
8502 else
8504 *vr0type = vr1type;
8505 *vr0min = vr1min;
8506 *vr0max = vr1max;
8509 else if (*vr0type == VR_ANTI_RANGE
8510 && vr1type == VR_ANTI_RANGE)
8512 /* If both are anti-ranges the result is the outer one. */
8513 *vr0type = vr1type;
8514 *vr0min = vr1min;
8515 *vr0max = vr1max;
8517 else if (vr1type == VR_ANTI_RANGE
8518 && *vr0type == VR_RANGE)
8520 /* The intersection is empty. */
8521 *vr0type = VR_UNDEFINED;
8522 *vr0min = NULL_TREE;
8523 *vr0max = NULL_TREE;
8525 else
8526 gcc_unreachable ();
8528 else if ((operand_less_p (vr1min, *vr0max) == 1
8529 || operand_equal_p (vr1min, *vr0max, 0))
8530 && operand_less_p (*vr0min, vr1min) == 1)
8532 /* [ ( ] ) or [ ]( ) */
8533 if (*vr0type == VR_ANTI_RANGE
8534 && vr1type == VR_ANTI_RANGE)
8535 *vr0max = vr1max;
8536 else if (*vr0type == VR_RANGE
8537 && vr1type == VR_RANGE)
8538 *vr0min = vr1min;
8539 else if (*vr0type == VR_RANGE
8540 && vr1type == VR_ANTI_RANGE)
8542 if (TREE_CODE (vr1min) == INTEGER_CST)
8543 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8544 build_int_cst (TREE_TYPE (vr1min), 1));
8545 else
8546 *vr0max = vr1min;
8548 else if (*vr0type == VR_ANTI_RANGE
8549 && vr1type == VR_RANGE)
8551 *vr0type = VR_RANGE;
8552 if (TREE_CODE (*vr0max) == INTEGER_CST)
8553 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8554 build_int_cst (TREE_TYPE (*vr0max), 1));
8555 else
8556 *vr0min = *vr0max;
8557 *vr0max = vr1max;
8559 else
8560 gcc_unreachable ();
8562 else if ((operand_less_p (*vr0min, vr1max) == 1
8563 || operand_equal_p (*vr0min, vr1max, 0))
8564 && operand_less_p (vr1min, *vr0min) == 1)
8566 /* ( [ ) ] or ( )[ ] */
8567 if (*vr0type == VR_ANTI_RANGE
8568 && vr1type == VR_ANTI_RANGE)
8569 *vr0min = vr1min;
8570 else if (*vr0type == VR_RANGE
8571 && vr1type == VR_RANGE)
8572 *vr0max = vr1max;
8573 else if (*vr0type == VR_RANGE
8574 && vr1type == VR_ANTI_RANGE)
8576 if (TREE_CODE (vr1max) == INTEGER_CST)
8577 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8578 build_int_cst (TREE_TYPE (vr1max), 1));
8579 else
8580 *vr0min = vr1max;
8582 else if (*vr0type == VR_ANTI_RANGE
8583 && vr1type == VR_RANGE)
8585 *vr0type = VR_RANGE;
8586 if (TREE_CODE (*vr0min) == INTEGER_CST)
8587 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8588 build_int_cst (TREE_TYPE (*vr0min), 1));
8589 else
8590 *vr0max = *vr0min;
8591 *vr0min = vr1min;
8593 else
8594 gcc_unreachable ();
8597 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8598 result for the intersection. That's always a conservative
8599 correct estimate. */
8601 return;
8605 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8606 in *VR0. This may not be the smallest possible such range. */
8608 static void
8609 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8611 value_range_t saved;
8613 /* If either range is VR_VARYING the other one wins. */
8614 if (vr1->type == VR_VARYING)
8615 return;
8616 if (vr0->type == VR_VARYING)
8618 copy_value_range (vr0, vr1);
8619 return;
8622 /* When either range is VR_UNDEFINED the resulting range is
8623 VR_UNDEFINED, too. */
8624 if (vr0->type == VR_UNDEFINED)
8625 return;
8626 if (vr1->type == VR_UNDEFINED)
8628 set_value_range_to_undefined (vr0);
8629 return;
8632 /* Save the original vr0 so we can return it as conservative intersection
8633 result when our worker turns things to varying. */
8634 saved = *vr0;
8635 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8636 vr1->type, vr1->min, vr1->max);
8637 /* Make sure to canonicalize the result though as the inversion of a
8638 VR_RANGE can still be a VR_RANGE. */
8639 set_and_canonicalize_value_range (vr0, vr0->type,
8640 vr0->min, vr0->max, vr0->equiv);
8641 /* If that failed, use the saved original VR0. */
8642 if (vr0->type == VR_VARYING)
8644 *vr0 = saved;
8645 return;
8647 /* If the result is VR_UNDEFINED there is no need to mess with
8648 the equivalencies. */
8649 if (vr0->type == VR_UNDEFINED)
8650 return;
8652 /* The resulting set of equivalences for range intersection is the union of
8653 the two sets. */
8654 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8655 bitmap_ior_into (vr0->equiv, vr1->equiv);
8656 else if (vr1->equiv && !vr0->equiv)
8657 bitmap_copy (vr0->equiv, vr1->equiv);
8660 static void
8661 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8663 if (dump_file && (dump_flags & TDF_DETAILS))
8665 fprintf (dump_file, "Intersecting\n ");
8666 dump_value_range (dump_file, vr0);
8667 fprintf (dump_file, "\nand\n ");
8668 dump_value_range (dump_file, vr1);
8669 fprintf (dump_file, "\n");
8671 vrp_intersect_ranges_1 (vr0, vr1);
8672 if (dump_file && (dump_flags & TDF_DETAILS))
8674 fprintf (dump_file, "to\n ");
8675 dump_value_range (dump_file, vr0);
8676 fprintf (dump_file, "\n");
8680 /* Meet operation for value ranges. Given two value ranges VR0 and
8681 VR1, store in VR0 a range that contains both VR0 and VR1. This
8682 may not be the smallest possible such range. */
8684 static void
8685 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8687 value_range_t saved;
8689 if (vr0->type == VR_UNDEFINED)
8691 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8692 return;
8695 if (vr1->type == VR_UNDEFINED)
8697 /* VR0 already has the resulting range. */
8698 return;
8701 if (vr0->type == VR_VARYING)
8703 /* Nothing to do. VR0 already has the resulting range. */
8704 return;
8707 if (vr1->type == VR_VARYING)
8709 set_value_range_to_varying (vr0);
8710 return;
8713 saved = *vr0;
8714 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8715 vr1->type, vr1->min, vr1->max);
8716 if (vr0->type == VR_VARYING)
8718 /* Failed to find an efficient meet. Before giving up and setting
8719 the result to VARYING, see if we can at least derive a useful
8720 anti-range. FIXME, all this nonsense about distinguishing
8721 anti-ranges from ranges is necessary because of the odd
8722 semantics of range_includes_zero_p and friends. */
8723 if (((saved.type == VR_RANGE
8724 && range_includes_zero_p (saved.min, saved.max) == 0)
8725 || (saved.type == VR_ANTI_RANGE
8726 && range_includes_zero_p (saved.min, saved.max) == 1))
8727 && ((vr1->type == VR_RANGE
8728 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8729 || (vr1->type == VR_ANTI_RANGE
8730 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8732 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8734 /* Since this meet operation did not result from the meeting of
8735 two equivalent names, VR0 cannot have any equivalences. */
8736 if (vr0->equiv)
8737 bitmap_clear (vr0->equiv);
8738 return;
8741 set_value_range_to_varying (vr0);
8742 return;
8744 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8745 vr0->equiv);
8746 if (vr0->type == VR_VARYING)
8747 return;
8749 /* The resulting set of equivalences is always the intersection of
8750 the two sets. */
8751 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8752 bitmap_and_into (vr0->equiv, vr1->equiv);
8753 else if (vr0->equiv && !vr1->equiv)
8754 bitmap_clear (vr0->equiv);
8757 static void
8758 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8760 if (dump_file && (dump_flags & TDF_DETAILS))
8762 fprintf (dump_file, "Meeting\n ");
8763 dump_value_range (dump_file, vr0);
8764 fprintf (dump_file, "\nand\n ");
8765 dump_value_range (dump_file, vr1);
8766 fprintf (dump_file, "\n");
8768 vrp_meet_1 (vr0, vr1);
8769 if (dump_file && (dump_flags & TDF_DETAILS))
8771 fprintf (dump_file, "to\n ");
8772 dump_value_range (dump_file, vr0);
8773 fprintf (dump_file, "\n");
8778 /* Visit all arguments for PHI node PHI that flow through executable
8779 edges. If a valid value range can be derived from all the incoming
8780 value ranges, set a new range for the LHS of PHI. */
8782 static enum ssa_prop_result
8783 vrp_visit_phi_node (gphi *phi)
8785 size_t i;
8786 tree lhs = PHI_RESULT (phi);
8787 value_range_t *lhs_vr = get_value_range (lhs);
8788 value_range_t vr_result = VR_INITIALIZER;
8789 bool first = true;
8790 int edges, old_edges;
8791 struct loop *l;
8793 if (dump_file && (dump_flags & TDF_DETAILS))
8795 fprintf (dump_file, "\nVisiting PHI node: ");
8796 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8799 edges = 0;
8800 for (i = 0; i < gimple_phi_num_args (phi); i++)
8802 edge e = gimple_phi_arg_edge (phi, i);
8804 if (dump_file && (dump_flags & TDF_DETAILS))
8806 fprintf (dump_file,
8807 " Argument #%d (%d -> %d %sexecutable)\n",
8808 (int) i, e->src->index, e->dest->index,
8809 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8812 if (e->flags & EDGE_EXECUTABLE)
8814 tree arg = PHI_ARG_DEF (phi, i);
8815 value_range_t vr_arg;
8817 ++edges;
8819 if (TREE_CODE (arg) == SSA_NAME)
8821 vr_arg = *(get_value_range (arg));
8822 /* Do not allow equivalences or symbolic ranges to leak in from
8823 backedges. That creates invalid equivalencies.
8824 See PR53465 and PR54767. */
8825 if (e->flags & EDGE_DFS_BACK)
8827 if (vr_arg.type == VR_RANGE
8828 || vr_arg.type == VR_ANTI_RANGE)
8830 vr_arg.equiv = NULL;
8831 if (symbolic_range_p (&vr_arg))
8833 vr_arg.type = VR_VARYING;
8834 vr_arg.min = NULL_TREE;
8835 vr_arg.max = NULL_TREE;
8839 else
8841 /* If the non-backedge arguments range is VR_VARYING then
8842 we can still try recording a simple equivalence. */
8843 if (vr_arg.type == VR_VARYING)
8845 vr_arg.type = VR_RANGE;
8846 vr_arg.min = arg;
8847 vr_arg.max = arg;
8848 vr_arg.equiv = NULL;
8852 else
8854 if (TREE_OVERFLOW_P (arg))
8855 arg = drop_tree_overflow (arg);
8857 vr_arg.type = VR_RANGE;
8858 vr_arg.min = arg;
8859 vr_arg.max = arg;
8860 vr_arg.equiv = NULL;
8863 if (dump_file && (dump_flags & TDF_DETAILS))
8865 fprintf (dump_file, "\t");
8866 print_generic_expr (dump_file, arg, dump_flags);
8867 fprintf (dump_file, ": ");
8868 dump_value_range (dump_file, &vr_arg);
8869 fprintf (dump_file, "\n");
8872 if (first)
8873 copy_value_range (&vr_result, &vr_arg);
8874 else
8875 vrp_meet (&vr_result, &vr_arg);
8876 first = false;
8878 if (vr_result.type == VR_VARYING)
8879 break;
8883 if (vr_result.type == VR_VARYING)
8884 goto varying;
8885 else if (vr_result.type == VR_UNDEFINED)
8886 goto update_range;
8888 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8889 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8891 /* To prevent infinite iterations in the algorithm, derive ranges
8892 when the new value is slightly bigger or smaller than the
8893 previous one. We don't do this if we have seen a new executable
8894 edge; this helps us avoid an overflow infinity for conditionals
8895 which are not in a loop. If the old value-range was VR_UNDEFINED
8896 use the updated range and iterate one more time. */
8897 if (edges > 0
8898 && gimple_phi_num_args (phi) > 1
8899 && edges == old_edges
8900 && lhs_vr->type != VR_UNDEFINED)
8902 /* Compare old and new ranges, fall back to varying if the
8903 values are not comparable. */
8904 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8905 if (cmp_min == -2)
8906 goto varying;
8907 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8908 if (cmp_max == -2)
8909 goto varying;
8911 /* For non VR_RANGE or for pointers fall back to varying if
8912 the range changed. */
8913 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8914 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8915 && (cmp_min != 0 || cmp_max != 0))
8916 goto varying;
8918 /* If the new minimum is larger than than the previous one
8919 retain the old value. If the new minimum value is smaller
8920 than the previous one and not -INF go all the way to -INF + 1.
8921 In the first case, to avoid infinite bouncing between different
8922 minimums, and in the other case to avoid iterating millions of
8923 times to reach -INF. Going to -INF + 1 also lets the following
8924 iteration compute whether there will be any overflow, at the
8925 expense of one additional iteration. */
8926 if (cmp_min < 0)
8927 vr_result.min = lhs_vr->min;
8928 else if (cmp_min > 0
8929 && !vrp_val_is_min (vr_result.min))
8930 vr_result.min
8931 = int_const_binop (PLUS_EXPR,
8932 vrp_val_min (TREE_TYPE (vr_result.min)),
8933 build_int_cst (TREE_TYPE (vr_result.min), 1));
8935 /* Similarly for the maximum value. */
8936 if (cmp_max > 0)
8937 vr_result.max = lhs_vr->max;
8938 else if (cmp_max < 0
8939 && !vrp_val_is_max (vr_result.max))
8940 vr_result.max
8941 = int_const_binop (MINUS_EXPR,
8942 vrp_val_max (TREE_TYPE (vr_result.min)),
8943 build_int_cst (TREE_TYPE (vr_result.min), 1));
8945 /* If we dropped either bound to +-INF then if this is a loop
8946 PHI node SCEV may known more about its value-range. */
8947 if ((cmp_min > 0 || cmp_min < 0
8948 || cmp_max < 0 || cmp_max > 0)
8949 && (l = loop_containing_stmt (phi))
8950 && l->header == gimple_bb (phi))
8951 adjust_range_with_scev (&vr_result, l, phi, lhs);
8953 /* If we will end up with a (-INF, +INF) range, set it to
8954 VARYING. Same if the previous max value was invalid for
8955 the type and we end up with vr_result.min > vr_result.max. */
8956 if ((vrp_val_is_max (vr_result.max)
8957 && vrp_val_is_min (vr_result.min))
8958 || compare_values (vr_result.min,
8959 vr_result.max) > 0)
8960 goto varying;
8963 /* If the new range is different than the previous value, keep
8964 iterating. */
8965 update_range:
8966 if (update_value_range (lhs, &vr_result))
8968 if (dump_file && (dump_flags & TDF_DETAILS))
8970 fprintf (dump_file, "Found new range for ");
8971 print_generic_expr (dump_file, lhs, 0);
8972 fprintf (dump_file, ": ");
8973 dump_value_range (dump_file, &vr_result);
8974 fprintf (dump_file, "\n");
8977 if (vr_result.type == VR_VARYING)
8978 return SSA_PROP_VARYING;
8980 return SSA_PROP_INTERESTING;
8983 /* Nothing changed, don't add outgoing edges. */
8984 return SSA_PROP_NOT_INTERESTING;
8986 /* No match found. Set the LHS to VARYING. */
8987 varying:
8988 set_value_range_to_varying (lhs_vr);
8989 return SSA_PROP_VARYING;
8992 /* Simplify boolean operations if the source is known
8993 to be already a boolean. */
8994 static bool
8995 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8997 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8998 tree lhs, op0, op1;
8999 bool need_conversion;
9001 /* We handle only !=/== case here. */
9002 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
9004 op0 = gimple_assign_rhs1 (stmt);
9005 if (!op_with_boolean_value_range_p (op0))
9006 return false;
9008 op1 = gimple_assign_rhs2 (stmt);
9009 if (!op_with_boolean_value_range_p (op1))
9010 return false;
9012 /* Reduce number of cases to handle to NE_EXPR. As there is no
9013 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9014 if (rhs_code == EQ_EXPR)
9016 if (TREE_CODE (op1) == INTEGER_CST)
9017 op1 = int_const_binop (BIT_XOR_EXPR, op1,
9018 build_int_cst (TREE_TYPE (op1), 1));
9019 else
9020 return false;
9023 lhs = gimple_assign_lhs (stmt);
9024 need_conversion
9025 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
9027 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9028 if (need_conversion
9029 && !TYPE_UNSIGNED (TREE_TYPE (op0))
9030 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
9031 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
9032 return false;
9034 /* For A != 0 we can substitute A itself. */
9035 if (integer_zerop (op1))
9036 gimple_assign_set_rhs_with_ops (gsi,
9037 need_conversion
9038 ? NOP_EXPR : TREE_CODE (op0), op0);
9039 /* For A != B we substitute A ^ B. Either with conversion. */
9040 else if (need_conversion)
9042 tree tem = make_ssa_name (TREE_TYPE (op0));
9043 gassign *newop
9044 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
9045 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
9046 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9048 /* Or without. */
9049 else
9050 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9051 update_stmt (gsi_stmt (*gsi));
9053 return true;
9056 /* Simplify a division or modulo operator to a right shift or
9057 bitwise and if the first operand is unsigned or is greater
9058 than zero and the second operand is an exact power of two.
9059 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9060 into just op0 if op0's range is known to be a subset of
9061 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9062 modulo. */
9064 static bool
9065 simplify_div_or_mod_using_ranges (gimple stmt)
9067 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9068 tree val = NULL;
9069 tree op0 = gimple_assign_rhs1 (stmt);
9070 tree op1 = gimple_assign_rhs2 (stmt);
9071 value_range_t *vr = get_value_range (op0);
9073 if (rhs_code == TRUNC_MOD_EXPR
9074 && TREE_CODE (op1) == INTEGER_CST
9075 && tree_int_cst_sgn (op1) == 1
9076 && range_int_cst_p (vr)
9077 && tree_int_cst_lt (vr->max, op1))
9079 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9080 || tree_int_cst_sgn (vr->min) >= 0
9081 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9082 vr->min))
9084 /* If op0 already has the range op0 % op1 has,
9085 then TRUNC_MOD_EXPR won't change anything. */
9086 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9087 gimple_assign_set_rhs_from_tree (&gsi, op0);
9088 update_stmt (stmt);
9089 return true;
9093 if (!integer_pow2p (op1))
9094 return false;
9096 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9098 val = integer_one_node;
9100 else
9102 bool sop = false;
9104 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9106 if (val
9107 && sop
9108 && integer_onep (val)
9109 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9111 location_t location;
9113 if (!gimple_has_location (stmt))
9114 location = input_location;
9115 else
9116 location = gimple_location (stmt);
9117 warning_at (location, OPT_Wstrict_overflow,
9118 "assuming signed overflow does not occur when "
9119 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9123 if (val && integer_onep (val))
9125 tree t;
9127 if (rhs_code == TRUNC_DIV_EXPR)
9129 t = build_int_cst (integer_type_node, tree_log2 (op1));
9130 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9131 gimple_assign_set_rhs1 (stmt, op0);
9132 gimple_assign_set_rhs2 (stmt, t);
9134 else
9136 t = build_int_cst (TREE_TYPE (op1), 1);
9137 t = int_const_binop (MINUS_EXPR, op1, t);
9138 t = fold_convert (TREE_TYPE (op0), t);
9140 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9141 gimple_assign_set_rhs1 (stmt, op0);
9142 gimple_assign_set_rhs2 (stmt, t);
9145 update_stmt (stmt);
9146 return true;
9149 return false;
9152 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9153 ABS_EXPR. If the operand is <= 0, then simplify the
9154 ABS_EXPR into a NEGATE_EXPR. */
9156 static bool
9157 simplify_abs_using_ranges (gimple stmt)
9159 tree val = NULL;
9160 tree op = gimple_assign_rhs1 (stmt);
9161 tree type = TREE_TYPE (op);
9162 value_range_t *vr = get_value_range (op);
9164 if (TYPE_UNSIGNED (type))
9166 val = integer_zero_node;
9168 else if (vr)
9170 bool sop = false;
9172 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9173 if (!val)
9175 sop = false;
9176 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
9177 &sop);
9179 if (val)
9181 if (integer_zerop (val))
9182 val = integer_one_node;
9183 else if (integer_onep (val))
9184 val = integer_zero_node;
9188 if (val
9189 && (integer_onep (val) || integer_zerop (val)))
9191 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9193 location_t location;
9195 if (!gimple_has_location (stmt))
9196 location = input_location;
9197 else
9198 location = gimple_location (stmt);
9199 warning_at (location, OPT_Wstrict_overflow,
9200 "assuming signed overflow does not occur when "
9201 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9204 gimple_assign_set_rhs1 (stmt, op);
9205 if (integer_onep (val))
9206 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9207 else
9208 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9209 update_stmt (stmt);
9210 return true;
9214 return false;
9217 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9218 If all the bits that are being cleared by & are already
9219 known to be zero from VR, or all the bits that are being
9220 set by | are already known to be one from VR, the bit
9221 operation is redundant. */
9223 static bool
9224 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9226 tree op0 = gimple_assign_rhs1 (stmt);
9227 tree op1 = gimple_assign_rhs2 (stmt);
9228 tree op = NULL_TREE;
9229 value_range_t vr0 = VR_INITIALIZER;
9230 value_range_t vr1 = VR_INITIALIZER;
9231 wide_int may_be_nonzero0, may_be_nonzero1;
9232 wide_int must_be_nonzero0, must_be_nonzero1;
9233 wide_int mask;
9235 if (TREE_CODE (op0) == SSA_NAME)
9236 vr0 = *(get_value_range (op0));
9237 else if (is_gimple_min_invariant (op0))
9238 set_value_range_to_value (&vr0, op0, NULL);
9239 else
9240 return false;
9242 if (TREE_CODE (op1) == SSA_NAME)
9243 vr1 = *(get_value_range (op1));
9244 else if (is_gimple_min_invariant (op1))
9245 set_value_range_to_value (&vr1, op1, NULL);
9246 else
9247 return false;
9249 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9250 &must_be_nonzero0))
9251 return false;
9252 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9253 &must_be_nonzero1))
9254 return false;
9256 switch (gimple_assign_rhs_code (stmt))
9258 case BIT_AND_EXPR:
9259 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9260 if (mask == 0)
9262 op = op0;
9263 break;
9265 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9266 if (mask == 0)
9268 op = op1;
9269 break;
9271 break;
9272 case BIT_IOR_EXPR:
9273 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9274 if (mask == 0)
9276 op = op1;
9277 break;
9279 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9280 if (mask == 0)
9282 op = op0;
9283 break;
9285 break;
9286 default:
9287 gcc_unreachable ();
9290 if (op == NULL_TREE)
9291 return false;
9293 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9294 update_stmt (gsi_stmt (*gsi));
9295 return true;
9298 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9299 a known value range VR.
9301 If there is one and only one value which will satisfy the
9302 conditional, then return that value. Else return NULL.
9304 If signed overflow must be undefined for the value to satisfy
9305 the conditional, then set *STRICT_OVERFLOW_P to true. */
9307 static tree
9308 test_for_singularity (enum tree_code cond_code, tree op0,
9309 tree op1, value_range_t *vr,
9310 bool *strict_overflow_p)
9312 tree min = NULL;
9313 tree max = NULL;
9315 /* Extract minimum/maximum values which satisfy the
9316 the conditional as it was written. */
9317 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9319 /* This should not be negative infinity; there is no overflow
9320 here. */
9321 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9323 max = op1;
9324 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9326 tree one = build_int_cst (TREE_TYPE (op0), 1);
9327 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9328 if (EXPR_P (max))
9329 TREE_NO_WARNING (max) = 1;
9332 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9334 /* This should not be positive infinity; there is no overflow
9335 here. */
9336 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9338 min = op1;
9339 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9341 tree one = build_int_cst (TREE_TYPE (op0), 1);
9342 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9343 if (EXPR_P (min))
9344 TREE_NO_WARNING (min) = 1;
9348 /* Now refine the minimum and maximum values using any
9349 value range information we have for op0. */
9350 if (min && max)
9352 if (compare_values (vr->min, min) == 1)
9353 min = vr->min;
9354 if (compare_values (vr->max, max) == -1)
9355 max = vr->max;
9357 /* If the new min/max values have converged to a single value,
9358 then there is only one value which can satisfy the condition,
9359 return that value. */
9360 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9362 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9363 && is_overflow_infinity (vr->max))
9364 *strict_overflow_p = true;
9365 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9366 && is_overflow_infinity (vr->min))
9367 *strict_overflow_p = true;
9369 return min;
9372 return NULL;
9375 /* Return whether the value range *VR fits in an integer type specified
9376 by PRECISION and UNSIGNED_P. */
9378 static bool
9379 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9381 tree src_type;
9382 unsigned src_precision;
9383 widest_int tem;
9384 signop src_sgn;
9386 /* We can only handle integral and pointer types. */
9387 src_type = TREE_TYPE (vr->min);
9388 if (!INTEGRAL_TYPE_P (src_type)
9389 && !POINTER_TYPE_P (src_type))
9390 return false;
9392 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9393 and so is an identity transform. */
9394 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9395 src_sgn = TYPE_SIGN (src_type);
9396 if ((src_precision < dest_precision
9397 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9398 || (src_precision == dest_precision && src_sgn == dest_sgn))
9399 return true;
9401 /* Now we can only handle ranges with constant bounds. */
9402 if (vr->type != VR_RANGE
9403 || TREE_CODE (vr->min) != INTEGER_CST
9404 || TREE_CODE (vr->max) != INTEGER_CST)
9405 return false;
9407 /* For sign changes, the MSB of the wide_int has to be clear.
9408 An unsigned value with its MSB set cannot be represented by
9409 a signed wide_int, while a negative value cannot be represented
9410 by an unsigned wide_int. */
9411 if (src_sgn != dest_sgn
9412 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9413 return false;
9415 /* Then we can perform the conversion on both ends and compare
9416 the result for equality. */
9417 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9418 if (tem != wi::to_widest (vr->min))
9419 return false;
9420 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9421 if (tem != wi::to_widest (vr->max))
9422 return false;
9424 return true;
9427 /* Simplify a conditional using a relational operator to an equality
9428 test if the range information indicates only one value can satisfy
9429 the original conditional. */
9431 static bool
9432 simplify_cond_using_ranges (gcond *stmt)
9434 tree op0 = gimple_cond_lhs (stmt);
9435 tree op1 = gimple_cond_rhs (stmt);
9436 enum tree_code cond_code = gimple_cond_code (stmt);
9438 if (cond_code != NE_EXPR
9439 && cond_code != EQ_EXPR
9440 && TREE_CODE (op0) == SSA_NAME
9441 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9442 && is_gimple_min_invariant (op1))
9444 value_range_t *vr = get_value_range (op0);
9446 /* If we have range information for OP0, then we might be
9447 able to simplify this conditional. */
9448 if (vr->type == VR_RANGE)
9450 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9451 bool sop = false;
9452 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9454 if (new_tree
9455 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9457 if (dump_file)
9459 fprintf (dump_file, "Simplified relational ");
9460 print_gimple_stmt (dump_file, stmt, 0, 0);
9461 fprintf (dump_file, " into ");
9464 gimple_cond_set_code (stmt, EQ_EXPR);
9465 gimple_cond_set_lhs (stmt, op0);
9466 gimple_cond_set_rhs (stmt, new_tree);
9468 update_stmt (stmt);
9470 if (dump_file)
9472 print_gimple_stmt (dump_file, stmt, 0, 0);
9473 fprintf (dump_file, "\n");
9476 if (sop && issue_strict_overflow_warning (wc))
9478 location_t location = input_location;
9479 if (gimple_has_location (stmt))
9480 location = gimple_location (stmt);
9482 warning_at (location, OPT_Wstrict_overflow,
9483 "assuming signed overflow does not occur when "
9484 "simplifying conditional");
9487 return true;
9490 /* Try again after inverting the condition. We only deal
9491 with integral types here, so no need to worry about
9492 issues with inverting FP comparisons. */
9493 sop = false;
9494 new_tree = test_for_singularity
9495 (invert_tree_comparison (cond_code, false),
9496 op0, op1, vr, &sop);
9498 if (new_tree
9499 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9501 if (dump_file)
9503 fprintf (dump_file, "Simplified relational ");
9504 print_gimple_stmt (dump_file, stmt, 0, 0);
9505 fprintf (dump_file, " into ");
9508 gimple_cond_set_code (stmt, NE_EXPR);
9509 gimple_cond_set_lhs (stmt, op0);
9510 gimple_cond_set_rhs (stmt, new_tree);
9512 update_stmt (stmt);
9514 if (dump_file)
9516 print_gimple_stmt (dump_file, stmt, 0, 0);
9517 fprintf (dump_file, "\n");
9520 if (sop && issue_strict_overflow_warning (wc))
9522 location_t location = input_location;
9523 if (gimple_has_location (stmt))
9524 location = gimple_location (stmt);
9526 warning_at (location, OPT_Wstrict_overflow,
9527 "assuming signed overflow does not occur when "
9528 "simplifying conditional");
9531 return true;
9536 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9537 see if OP0 was set by a type conversion where the source of
9538 the conversion is another SSA_NAME with a range that fits
9539 into the range of OP0's type.
9541 If so, the conversion is redundant as the earlier SSA_NAME can be
9542 used for the comparison directly if we just massage the constant in the
9543 comparison. */
9544 if (TREE_CODE (op0) == SSA_NAME
9545 && TREE_CODE (op1) == INTEGER_CST)
9547 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9548 tree innerop;
9550 if (!is_gimple_assign (def_stmt)
9551 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9552 return false;
9554 innerop = gimple_assign_rhs1 (def_stmt);
9556 if (TREE_CODE (innerop) == SSA_NAME
9557 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9559 value_range_t *vr = get_value_range (innerop);
9561 if (range_int_cst_p (vr)
9562 && range_fits_type_p (vr,
9563 TYPE_PRECISION (TREE_TYPE (op0)),
9564 TYPE_SIGN (TREE_TYPE (op0)))
9565 && int_fits_type_p (op1, TREE_TYPE (innerop))
9566 /* The range must not have overflowed, or if it did overflow
9567 we must not be wrapping/trapping overflow and optimizing
9568 with strict overflow semantics. */
9569 && ((!is_negative_overflow_infinity (vr->min)
9570 && !is_positive_overflow_infinity (vr->max))
9571 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9573 /* If the range overflowed and the user has asked for warnings
9574 when strict overflow semantics were used to optimize code,
9575 issue an appropriate warning. */
9576 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9577 && (is_negative_overflow_infinity (vr->min)
9578 || is_positive_overflow_infinity (vr->max))
9579 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9581 location_t location;
9583 if (!gimple_has_location (stmt))
9584 location = input_location;
9585 else
9586 location = gimple_location (stmt);
9587 warning_at (location, OPT_Wstrict_overflow,
9588 "assuming signed overflow does not occur when "
9589 "simplifying conditional");
9592 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9593 gimple_cond_set_lhs (stmt, innerop);
9594 gimple_cond_set_rhs (stmt, newconst);
9595 return true;
9600 return false;
9603 /* Simplify a switch statement using the value range of the switch
9604 argument. */
9606 static bool
9607 simplify_switch_using_ranges (gswitch *stmt)
9609 tree op = gimple_switch_index (stmt);
9610 value_range_t *vr;
9611 bool take_default;
9612 edge e;
9613 edge_iterator ei;
9614 size_t i = 0, j = 0, n, n2;
9615 tree vec2;
9616 switch_update su;
9617 size_t k = 1, l = 0;
9619 if (TREE_CODE (op) == SSA_NAME)
9621 vr = get_value_range (op);
9623 /* We can only handle integer ranges. */
9624 if ((vr->type != VR_RANGE
9625 && vr->type != VR_ANTI_RANGE)
9626 || symbolic_range_p (vr))
9627 return false;
9629 /* Find case label for min/max of the value range. */
9630 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9632 else if (TREE_CODE (op) == INTEGER_CST)
9634 take_default = !find_case_label_index (stmt, 1, op, &i);
9635 if (take_default)
9637 i = 1;
9638 j = 0;
9640 else
9642 j = i;
9645 else
9646 return false;
9648 n = gimple_switch_num_labels (stmt);
9650 /* Bail out if this is just all edges taken. */
9651 if (i == 1
9652 && j == n - 1
9653 && take_default)
9654 return false;
9656 /* Build a new vector of taken case labels. */
9657 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9658 n2 = 0;
9660 /* Add the default edge, if necessary. */
9661 if (take_default)
9662 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9664 for (; i <= j; ++i, ++n2)
9665 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9667 for (; k <= l; ++k, ++n2)
9668 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9670 /* Mark needed edges. */
9671 for (i = 0; i < n2; ++i)
9673 e = find_edge (gimple_bb (stmt),
9674 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9675 e->aux = (void *)-1;
9678 /* Queue not needed edges for later removal. */
9679 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9681 if (e->aux == (void *)-1)
9683 e->aux = NULL;
9684 continue;
9687 if (dump_file && (dump_flags & TDF_DETAILS))
9689 fprintf (dump_file, "removing unreachable case label\n");
9691 to_remove_edges.safe_push (e);
9692 e->flags &= ~EDGE_EXECUTABLE;
9695 /* And queue an update for the stmt. */
9696 su.stmt = stmt;
9697 su.vec = vec2;
9698 to_update_switch_stmts.safe_push (su);
9699 return false;
9702 /* Simplify an integral conversion from an SSA name in STMT. */
9704 static bool
9705 simplify_conversion_using_ranges (gimple stmt)
9707 tree innerop, middleop, finaltype;
9708 gimple def_stmt;
9709 value_range_t *innervr;
9710 signop inner_sgn, middle_sgn, final_sgn;
9711 unsigned inner_prec, middle_prec, final_prec;
9712 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9714 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9715 if (!INTEGRAL_TYPE_P (finaltype))
9716 return false;
9717 middleop = gimple_assign_rhs1 (stmt);
9718 def_stmt = SSA_NAME_DEF_STMT (middleop);
9719 if (!is_gimple_assign (def_stmt)
9720 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9721 return false;
9722 innerop = gimple_assign_rhs1 (def_stmt);
9723 if (TREE_CODE (innerop) != SSA_NAME
9724 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9725 return false;
9727 /* Get the value-range of the inner operand. */
9728 innervr = get_value_range (innerop);
9729 if (innervr->type != VR_RANGE
9730 || TREE_CODE (innervr->min) != INTEGER_CST
9731 || TREE_CODE (innervr->max) != INTEGER_CST)
9732 return false;
9734 /* Simulate the conversion chain to check if the result is equal if
9735 the middle conversion is removed. */
9736 innermin = wi::to_widest (innervr->min);
9737 innermax = wi::to_widest (innervr->max);
9739 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9740 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9741 final_prec = TYPE_PRECISION (finaltype);
9743 /* If the first conversion is not injective, the second must not
9744 be widening. */
9745 if (wi::gtu_p (innermax - innermin,
9746 wi::mask <widest_int> (middle_prec, false))
9747 && middle_prec < final_prec)
9748 return false;
9749 /* We also want a medium value so that we can track the effect that
9750 narrowing conversions with sign change have. */
9751 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9752 if (inner_sgn == UNSIGNED)
9753 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9754 else
9755 innermed = 0;
9756 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9757 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9758 innermed = innermin;
9760 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9761 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9762 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9763 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9765 /* Require that the final conversion applied to both the original
9766 and the intermediate range produces the same result. */
9767 final_sgn = TYPE_SIGN (finaltype);
9768 if (wi::ext (middlemin, final_prec, final_sgn)
9769 != wi::ext (innermin, final_prec, final_sgn)
9770 || wi::ext (middlemed, final_prec, final_sgn)
9771 != wi::ext (innermed, final_prec, final_sgn)
9772 || wi::ext (middlemax, final_prec, final_sgn)
9773 != wi::ext (innermax, final_prec, final_sgn))
9774 return false;
9776 gimple_assign_set_rhs1 (stmt, innerop);
9777 update_stmt (stmt);
9778 return true;
9781 /* Simplify a conversion from integral SSA name to float in STMT. */
9783 static bool
9784 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9786 tree rhs1 = gimple_assign_rhs1 (stmt);
9787 value_range_t *vr = get_value_range (rhs1);
9788 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9789 machine_mode mode;
9790 tree tem;
9791 gassign *conv;
9793 /* We can only handle constant ranges. */
9794 if (vr->type != VR_RANGE
9795 || TREE_CODE (vr->min) != INTEGER_CST
9796 || TREE_CODE (vr->max) != INTEGER_CST)
9797 return false;
9799 /* First check if we can use a signed type in place of an unsigned. */
9800 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9801 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9802 != CODE_FOR_nothing)
9803 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9804 mode = TYPE_MODE (TREE_TYPE (rhs1));
9805 /* If we can do the conversion in the current input mode do nothing. */
9806 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9807 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9808 return false;
9809 /* Otherwise search for a mode we can use, starting from the narrowest
9810 integer mode available. */
9811 else
9813 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9816 /* If we cannot do a signed conversion to float from mode
9817 or if the value-range does not fit in the signed type
9818 try with a wider mode. */
9819 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9820 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9821 break;
9823 mode = GET_MODE_WIDER_MODE (mode);
9824 /* But do not widen the input. Instead leave that to the
9825 optabs expansion code. */
9826 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9827 return false;
9829 while (mode != VOIDmode);
9830 if (mode == VOIDmode)
9831 return false;
9834 /* It works, insert a truncation or sign-change before the
9835 float conversion. */
9836 tem = make_ssa_name (build_nonstandard_integer_type
9837 (GET_MODE_PRECISION (mode), 0));
9838 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9839 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9840 gimple_assign_set_rhs1 (stmt, tem);
9841 update_stmt (stmt);
9843 return true;
9846 /* Simplify an internal fn call using ranges if possible. */
9848 static bool
9849 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9851 enum tree_code subcode;
9852 bool is_ubsan = false;
9853 bool ovf = false;
9854 switch (gimple_call_internal_fn (stmt))
9856 case IFN_UBSAN_CHECK_ADD:
9857 subcode = PLUS_EXPR;
9858 is_ubsan = true;
9859 break;
9860 case IFN_UBSAN_CHECK_SUB:
9861 subcode = MINUS_EXPR;
9862 is_ubsan = true;
9863 break;
9864 case IFN_UBSAN_CHECK_MUL:
9865 subcode = MULT_EXPR;
9866 is_ubsan = true;
9867 break;
9868 case IFN_ADD_OVERFLOW:
9869 subcode = PLUS_EXPR;
9870 break;
9871 case IFN_SUB_OVERFLOW:
9872 subcode = MINUS_EXPR;
9873 break;
9874 case IFN_MUL_OVERFLOW:
9875 subcode = MULT_EXPR;
9876 break;
9877 default:
9878 return false;
9881 tree op0 = gimple_call_arg (stmt, 0);
9882 tree op1 = gimple_call_arg (stmt, 1);
9883 tree type;
9884 if (is_ubsan)
9885 type = TREE_TYPE (op0);
9886 else if (gimple_call_lhs (stmt) == NULL_TREE)
9887 return false;
9888 else
9889 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9890 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9891 || (is_ubsan && ovf))
9892 return false;
9894 gimple g;
9895 location_t loc = gimple_location (stmt);
9896 if (is_ubsan)
9897 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9898 else
9900 int prec = TYPE_PRECISION (type);
9901 tree utype = type;
9902 if (ovf
9903 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9904 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9905 utype = build_nonstandard_integer_type (prec, 1);
9906 if (TREE_CODE (op0) == INTEGER_CST)
9907 op0 = fold_convert (utype, op0);
9908 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9910 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9911 gimple_set_location (g, loc);
9912 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9913 op0 = gimple_assign_lhs (g);
9915 if (TREE_CODE (op1) == INTEGER_CST)
9916 op1 = fold_convert (utype, op1);
9917 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9919 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9920 gimple_set_location (g, loc);
9921 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9922 op1 = gimple_assign_lhs (g);
9924 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9925 gimple_set_location (g, loc);
9926 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9927 if (utype != type)
9929 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9930 gimple_assign_lhs (g));
9931 gimple_set_location (g, loc);
9932 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9934 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9935 gimple_assign_lhs (g),
9936 build_int_cst (type, ovf));
9938 gimple_set_location (g, loc);
9939 gsi_replace (gsi, g, false);
9940 return true;
9943 /* Simplify STMT using ranges if possible. */
9945 static bool
9946 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9948 gimple stmt = gsi_stmt (*gsi);
9949 if (is_gimple_assign (stmt))
9951 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9952 tree rhs1 = gimple_assign_rhs1 (stmt);
9954 switch (rhs_code)
9956 case EQ_EXPR:
9957 case NE_EXPR:
9958 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9959 if the RHS is zero or one, and the LHS are known to be boolean
9960 values. */
9961 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9962 return simplify_truth_ops_using_ranges (gsi, stmt);
9963 break;
9965 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9966 and BIT_AND_EXPR respectively if the first operand is greater
9967 than zero and the second operand is an exact power of two.
9968 Also optimize TRUNC_MOD_EXPR away if the second operand is
9969 constant and the first operand already has the right value
9970 range. */
9971 case TRUNC_DIV_EXPR:
9972 case TRUNC_MOD_EXPR:
9973 if (TREE_CODE (rhs1) == SSA_NAME
9974 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9975 return simplify_div_or_mod_using_ranges (stmt);
9976 break;
9978 /* Transform ABS (X) into X or -X as appropriate. */
9979 case ABS_EXPR:
9980 if (TREE_CODE (rhs1) == SSA_NAME
9981 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9982 return simplify_abs_using_ranges (stmt);
9983 break;
9985 case BIT_AND_EXPR:
9986 case BIT_IOR_EXPR:
9987 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9988 if all the bits being cleared are already cleared or
9989 all the bits being set are already set. */
9990 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9991 return simplify_bit_ops_using_ranges (gsi, stmt);
9992 break;
9994 CASE_CONVERT:
9995 if (TREE_CODE (rhs1) == SSA_NAME
9996 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9997 return simplify_conversion_using_ranges (stmt);
9998 break;
10000 case FLOAT_EXPR:
10001 if (TREE_CODE (rhs1) == SSA_NAME
10002 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10003 return simplify_float_conversion_using_ranges (gsi, stmt);
10004 break;
10006 default:
10007 break;
10010 else if (gimple_code (stmt) == GIMPLE_COND)
10011 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
10012 else if (gimple_code (stmt) == GIMPLE_SWITCH)
10013 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
10014 else if (is_gimple_call (stmt)
10015 && gimple_call_internal_p (stmt))
10016 return simplify_internal_call_using_ranges (gsi, stmt);
10018 return false;
10021 /* If the statement pointed by SI has a predicate whose value can be
10022 computed using the value range information computed by VRP, compute
10023 its value and return true. Otherwise, return false. */
10025 static bool
10026 fold_predicate_in (gimple_stmt_iterator *si)
10028 bool assignment_p = false;
10029 tree val;
10030 gimple stmt = gsi_stmt (*si);
10032 if (is_gimple_assign (stmt)
10033 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10035 assignment_p = true;
10036 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10037 gimple_assign_rhs1 (stmt),
10038 gimple_assign_rhs2 (stmt),
10039 stmt);
10041 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10042 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10043 gimple_cond_lhs (cond_stmt),
10044 gimple_cond_rhs (cond_stmt),
10045 stmt);
10046 else
10047 return false;
10049 if (val)
10051 if (assignment_p)
10052 val = fold_convert (gimple_expr_type (stmt), val);
10054 if (dump_file)
10056 fprintf (dump_file, "Folding predicate ");
10057 print_gimple_expr (dump_file, stmt, 0, 0);
10058 fprintf (dump_file, " to ");
10059 print_generic_expr (dump_file, val, 0);
10060 fprintf (dump_file, "\n");
10063 if (is_gimple_assign (stmt))
10064 gimple_assign_set_rhs_from_tree (si, val);
10065 else
10067 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10068 gcond *cond_stmt = as_a <gcond *> (stmt);
10069 if (integer_zerop (val))
10070 gimple_cond_make_false (cond_stmt);
10071 else if (integer_onep (val))
10072 gimple_cond_make_true (cond_stmt);
10073 else
10074 gcc_unreachable ();
10077 return true;
10080 return false;
10083 /* Callback for substitute_and_fold folding the stmt at *SI. */
10085 static bool
10086 vrp_fold_stmt (gimple_stmt_iterator *si)
10088 if (fold_predicate_in (si))
10089 return true;
10091 return simplify_stmt_using_ranges (si);
10094 /* Unwindable const/copy equivalences. */
10095 const_and_copies *equiv_stack;
10097 /* A trivial wrapper so that we can present the generic jump threading
10098 code with a simple API for simplifying statements. STMT is the
10099 statement we want to simplify, WITHIN_STMT provides the location
10100 for any overflow warnings. */
10102 static tree
10103 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
10105 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10106 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10107 gimple_cond_lhs (cond_stmt),
10108 gimple_cond_rhs (cond_stmt),
10109 within_stmt);
10111 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10113 value_range_t new_vr = VR_INITIALIZER;
10114 tree lhs = gimple_assign_lhs (assign_stmt);
10116 if (TREE_CODE (lhs) == SSA_NAME
10117 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10118 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10120 extract_range_from_assignment (&new_vr, assign_stmt);
10121 if (range_int_cst_singleton_p (&new_vr))
10122 return new_vr.min;
10126 return NULL_TREE;
10129 /* Blocks which have more than one predecessor and more than
10130 one successor present jump threading opportunities, i.e.,
10131 when the block is reached from a specific predecessor, we
10132 may be able to determine which of the outgoing edges will
10133 be traversed. When this optimization applies, we are able
10134 to avoid conditionals at runtime and we may expose secondary
10135 optimization opportunities.
10137 This routine is effectively a driver for the generic jump
10138 threading code. It basically just presents the generic code
10139 with edges that may be suitable for jump threading.
10141 Unlike DOM, we do not iterate VRP if jump threading was successful.
10142 While iterating may expose new opportunities for VRP, it is expected
10143 those opportunities would be very limited and the compile time cost
10144 to expose those opportunities would be significant.
10146 As jump threading opportunities are discovered, they are registered
10147 for later realization. */
10149 static void
10150 identify_jump_threads (void)
10152 basic_block bb;
10153 gcond *dummy;
10154 int i;
10155 edge e;
10157 /* Ugh. When substituting values earlier in this pass we can
10158 wipe the dominance information. So rebuild the dominator
10159 information as we need it within the jump threading code. */
10160 calculate_dominance_info (CDI_DOMINATORS);
10162 /* We do not allow VRP information to be used for jump threading
10163 across a back edge in the CFG. Otherwise it becomes too
10164 difficult to avoid eliminating loop exit tests. Of course
10165 EDGE_DFS_BACK is not accurate at this time so we have to
10166 recompute it. */
10167 mark_dfs_back_edges ();
10169 /* Do not thread across edges we are about to remove. Just marking
10170 them as EDGE_DFS_BACK will do. */
10171 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10172 e->flags |= EDGE_DFS_BACK;
10174 /* Allocate our unwinder stack to unwind any temporary equivalences
10175 that might be recorded. */
10176 equiv_stack = new const_and_copies (dump_file, dump_flags);
10178 /* To avoid lots of silly node creation, we create a single
10179 conditional and just modify it in-place when attempting to
10180 thread jumps. */
10181 dummy = gimple_build_cond (EQ_EXPR,
10182 integer_zero_node, integer_zero_node,
10183 NULL, NULL);
10185 /* Walk through all the blocks finding those which present a
10186 potential jump threading opportunity. We could set this up
10187 as a dominator walker and record data during the walk, but
10188 I doubt it's worth the effort for the classes of jump
10189 threading opportunities we are trying to identify at this
10190 point in compilation. */
10191 FOR_EACH_BB_FN (bb, cfun)
10193 gimple last;
10195 /* If the generic jump threading code does not find this block
10196 interesting, then there is nothing to do. */
10197 if (! potentially_threadable_block (bb))
10198 continue;
10200 last = last_stmt (bb);
10202 /* We're basically looking for a switch or any kind of conditional with
10203 integral or pointer type arguments. Note the type of the second
10204 argument will be the same as the first argument, so no need to
10205 check it explicitly.
10207 We also handle the case where there are no statements in the
10208 block. This come up with forwarder blocks that are not
10209 optimized away because they lead to a loop header. But we do
10210 want to thread through them as we can sometimes thread to the
10211 loop exit which is obviously profitable. */
10212 if (!last
10213 || gimple_code (last) == GIMPLE_SWITCH
10214 || (gimple_code (last) == GIMPLE_COND
10215 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10216 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10217 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10218 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10219 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10221 edge_iterator ei;
10223 /* We've got a block with multiple predecessors and multiple
10224 successors which also ends in a suitable conditional or
10225 switch statement. For each predecessor, see if we can thread
10226 it to a specific successor. */
10227 FOR_EACH_EDGE (e, ei, bb->preds)
10229 /* Do not thread across back edges or abnormal edges
10230 in the CFG. */
10231 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
10232 continue;
10234 thread_across_edge (dummy, e, true, equiv_stack,
10235 simplify_stmt_for_jump_threading);
10240 /* We do not actually update the CFG or SSA graphs at this point as
10241 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10242 handle ASSERT_EXPRs gracefully. */
10245 /* We identified all the jump threading opportunities earlier, but could
10246 not transform the CFG at that time. This routine transforms the
10247 CFG and arranges for the dominator tree to be rebuilt if necessary.
10249 Note the SSA graph update will occur during the normal TODO
10250 processing by the pass manager. */
10251 static void
10252 finalize_jump_threads (void)
10254 thread_through_all_blocks (false);
10255 delete equiv_stack;
10259 /* Traverse all the blocks folding conditionals with known ranges. */
10261 static void
10262 vrp_finalize (void)
10264 size_t i;
10266 values_propagated = true;
10268 if (dump_file)
10270 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10271 dump_all_value_ranges (dump_file);
10272 fprintf (dump_file, "\n");
10275 substitute_and_fold (op_with_constant_singleton_value_range,
10276 vrp_fold_stmt, false);
10278 if (warn_array_bounds && first_pass_instance)
10279 check_all_array_refs ();
10281 /* We must identify jump threading opportunities before we release
10282 the datastructures built by VRP. */
10283 identify_jump_threads ();
10285 /* Set value range to non pointer SSA_NAMEs. */
10286 for (i = 0; i < num_vr_values; i++)
10287 if (vr_value[i])
10289 tree name = ssa_name (i);
10291 if (!name
10292 || POINTER_TYPE_P (TREE_TYPE (name))
10293 || (vr_value[i]->type == VR_VARYING)
10294 || (vr_value[i]->type == VR_UNDEFINED))
10295 continue;
10297 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10298 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10299 && (vr_value[i]->type == VR_RANGE
10300 || vr_value[i]->type == VR_ANTI_RANGE))
10301 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10302 vr_value[i]->max);
10305 /* Free allocated memory. */
10306 for (i = 0; i < num_vr_values; i++)
10307 if (vr_value[i])
10309 BITMAP_FREE (vr_value[i]->equiv);
10310 free (vr_value[i]);
10313 free (vr_value);
10314 free (vr_phi_edge_counts);
10316 /* So that we can distinguish between VRP data being available
10317 and not available. */
10318 vr_value = NULL;
10319 vr_phi_edge_counts = NULL;
10323 /* Main entry point to VRP (Value Range Propagation). This pass is
10324 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10325 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10326 Programming Language Design and Implementation, pp. 67-78, 1995.
10327 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10329 This is essentially an SSA-CCP pass modified to deal with ranges
10330 instead of constants.
10332 While propagating ranges, we may find that two or more SSA name
10333 have equivalent, though distinct ranges. For instance,
10335 1 x_9 = p_3->a;
10336 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10337 3 if (p_4 == q_2)
10338 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10339 5 endif
10340 6 if (q_2)
10342 In the code above, pointer p_5 has range [q_2, q_2], but from the
10343 code we can also determine that p_5 cannot be NULL and, if q_2 had
10344 a non-varying range, p_5's range should also be compatible with it.
10346 These equivalences are created by two expressions: ASSERT_EXPR and
10347 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10348 result of another assertion, then we can use the fact that p_5 and
10349 p_4 are equivalent when evaluating p_5's range.
10351 Together with value ranges, we also propagate these equivalences
10352 between names so that we can take advantage of information from
10353 multiple ranges when doing final replacement. Note that this
10354 equivalency relation is transitive but not symmetric.
10356 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10357 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10358 in contexts where that assertion does not hold (e.g., in line 6).
10360 TODO, the main difference between this pass and Patterson's is that
10361 we do not propagate edge probabilities. We only compute whether
10362 edges can be taken or not. That is, instead of having a spectrum
10363 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10364 DON'T KNOW. In the future, it may be worthwhile to propagate
10365 probabilities to aid branch prediction. */
10367 static unsigned int
10368 execute_vrp (void)
10370 int i;
10371 edge e;
10372 switch_update *su;
10374 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10375 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10376 scev_initialize ();
10378 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10379 Inserting assertions may split edges which will invalidate
10380 EDGE_DFS_BACK. */
10381 insert_range_assertions ();
10383 to_remove_edges.create (10);
10384 to_update_switch_stmts.create (5);
10385 threadedge_initialize_values ();
10387 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10388 mark_dfs_back_edges ();
10390 vrp_initialize ();
10391 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10392 vrp_finalize ();
10394 free_numbers_of_iterations_estimates ();
10396 /* ASSERT_EXPRs must be removed before finalizing jump threads
10397 as finalizing jump threads calls the CFG cleanup code which
10398 does not properly handle ASSERT_EXPRs. */
10399 remove_range_assertions ();
10401 /* If we exposed any new variables, go ahead and put them into
10402 SSA form now, before we handle jump threading. This simplifies
10403 interactions between rewriting of _DECL nodes into SSA form
10404 and rewriting SSA_NAME nodes into SSA form after block
10405 duplication and CFG manipulation. */
10406 update_ssa (TODO_update_ssa);
10408 finalize_jump_threads ();
10410 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10411 CFG in a broken state and requires a cfg_cleanup run. */
10412 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10413 remove_edge (e);
10414 /* Update SWITCH_EXPR case label vector. */
10415 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10417 size_t j;
10418 size_t n = TREE_VEC_LENGTH (su->vec);
10419 tree label;
10420 gimple_switch_set_num_labels (su->stmt, n);
10421 for (j = 0; j < n; j++)
10422 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10423 /* As we may have replaced the default label with a regular one
10424 make sure to make it a real default label again. This ensures
10425 optimal expansion. */
10426 label = gimple_switch_label (su->stmt, 0);
10427 CASE_LOW (label) = NULL_TREE;
10428 CASE_HIGH (label) = NULL_TREE;
10431 if (to_remove_edges.length () > 0)
10433 free_dominance_info (CDI_DOMINATORS);
10434 loops_state_set (LOOPS_NEED_FIXUP);
10437 to_remove_edges.release ();
10438 to_update_switch_stmts.release ();
10439 threadedge_finalize_values ();
10441 scev_finalize ();
10442 loop_optimizer_finalize ();
10443 return 0;
10446 namespace {
10448 const pass_data pass_data_vrp =
10450 GIMPLE_PASS, /* type */
10451 "vrp", /* name */
10452 OPTGROUP_NONE, /* optinfo_flags */
10453 TV_TREE_VRP, /* tv_id */
10454 PROP_ssa, /* properties_required */
10455 0, /* properties_provided */
10456 0, /* properties_destroyed */
10457 0, /* todo_flags_start */
10458 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10461 class pass_vrp : public gimple_opt_pass
10463 public:
10464 pass_vrp (gcc::context *ctxt)
10465 : gimple_opt_pass (pass_data_vrp, ctxt)
10468 /* opt_pass methods: */
10469 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10470 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10471 virtual unsigned int execute (function *) { return execute_vrp (); }
10473 }; // class pass_vrp
10475 } // anon namespace
10477 gimple_opt_pass *
10478 make_pass_vrp (gcc::context *ctxt)
10480 return new pass_vrp (ctxt);