MIPS compact branch support
[official-gcc.git] / gcc / tree-vrp.c
blob21fbed0811520ab6bc33c9a406f4ad2bb1026573
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 "backend.h"
25 #include "cfghooks.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "rtl.h"
29 #include "ssa.h"
30 #include "flags.h"
31 #include "alias.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 #include "calls.h"
35 #include "cfganal.h"
36 #include "internal-fn.h"
37 #include "gimple-fold.h"
38 #include "tree-eh.h"
39 #include "gimple-iterator.h"
40 #include "gimple-walk.h"
41 #include "tree-cfg.h"
42 #include "tree-ssa-loop-manip.h"
43 #include "tree-ssa-loop-niter.h"
44 #include "tree-ssa-loop.h"
45 #include "tree-into-ssa.h"
46 #include "tree-ssa.h"
47 #include "tree-pass.h"
48 #include "tree-dump.h"
49 #include "gimple-pretty-print.h"
50 #include "diagnostic-core.h"
51 #include "intl.h"
52 #include "cfgloop.h"
53 #include "tree-scalar-evolution.h"
54 #include "tree-ssa-propagate.h"
55 #include "tree-chrec.h"
56 #include "tree-ssa-threadupdate.h"
57 #include "insn-config.h"
58 #include "expmed.h"
59 #include "dojump.h"
60 #include "explow.h"
61 #include "emit-rtl.h"
62 #include "varasm.h"
63 #include "stmt.h"
64 #include "expr.h"
65 #include "insn-codes.h"
66 #include "optabs.h"
67 #include "tree-ssa-scopedtables.h"
68 #include "tree-ssa-threadedge.h"
72 /* Range of values that can be associated with an SSA_NAME after VRP
73 has executed. */
74 struct value_range_d
76 /* Lattice value represented by this range. */
77 enum value_range_type type;
79 /* Minimum and maximum values represented by this range. These
80 values should be interpreted as follows:
82 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
83 be NULL.
85 - If TYPE == VR_RANGE then MIN holds the minimum value and
86 MAX holds the maximum value of the range [MIN, MAX].
88 - If TYPE == ANTI_RANGE the variable is known to NOT
89 take any values in the range [MIN, MAX]. */
90 tree min;
91 tree max;
93 /* Set of SSA names whose value ranges are equivalent to this one.
94 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
95 bitmap equiv;
98 typedef struct value_range_d value_range_t;
100 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
102 /* Set of SSA names found live during the RPO traversal of the function
103 for still active basic-blocks. */
104 static sbitmap *live;
106 /* Return true if the SSA name NAME is live on the edge E. */
108 static bool
109 live_on_edge (edge e, tree name)
111 return (live[e->dest->index]
112 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
115 /* Local functions. */
116 static int compare_values (tree val1, tree val2);
117 static int compare_values_warnv (tree val1, tree val2, bool *);
118 static void vrp_meet (value_range_t *, value_range_t *);
119 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
120 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
121 tree, tree, bool, bool *,
122 bool *);
124 /* Location information for ASSERT_EXPRs. Each instance of this
125 structure describes an ASSERT_EXPR for an SSA name. Since a single
126 SSA name may have more than one assertion associated with it, these
127 locations are kept in a linked list attached to the corresponding
128 SSA name. */
129 struct assert_locus_d
131 /* Basic block where the assertion would be inserted. */
132 basic_block bb;
134 /* Some assertions need to be inserted on an edge (e.g., assertions
135 generated by COND_EXPRs). In those cases, BB will be NULL. */
136 edge e;
138 /* Pointer to the statement that generated this assertion. */
139 gimple_stmt_iterator si;
141 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
142 enum tree_code comp_code;
144 /* Value being compared against. */
145 tree val;
147 /* Expression to compare. */
148 tree expr;
150 /* Next node in the linked list. */
151 struct assert_locus_d *next;
154 typedef struct assert_locus_d *assert_locus_t;
156 /* If bit I is present, it means that SSA name N_i has a list of
157 assertions that should be inserted in the IL. */
158 static bitmap need_assert_for;
160 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
161 holds a list of ASSERT_LOCUS_T nodes that describe where
162 ASSERT_EXPRs for SSA name N_I should be inserted. */
163 static assert_locus_t *asserts_for;
165 /* Value range array. After propagation, VR_VALUE[I] holds the range
166 of values that SSA name N_I may take. */
167 static unsigned num_vr_values;
168 static value_range_t **vr_value;
169 static bool values_propagated;
171 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
172 number of executable edges we saw the last time we visited the
173 node. */
174 static int *vr_phi_edge_counts;
176 struct switch_update {
177 gswitch *stmt;
178 tree vec;
181 static vec<edge> to_remove_edges;
182 static vec<switch_update> to_update_switch_stmts;
185 /* Return the maximum value for TYPE. */
187 static inline tree
188 vrp_val_max (const_tree type)
190 if (!INTEGRAL_TYPE_P (type))
191 return NULL_TREE;
193 return TYPE_MAX_VALUE (type);
196 /* Return the minimum value for TYPE. */
198 static inline tree
199 vrp_val_min (const_tree type)
201 if (!INTEGRAL_TYPE_P (type))
202 return NULL_TREE;
204 return TYPE_MIN_VALUE (type);
207 /* Return whether VAL is equal to the maximum value of its type. This
208 will be true for a positive overflow infinity. We can't do a
209 simple equality comparison with TYPE_MAX_VALUE because C typedefs
210 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
211 to the integer constant with the same value in the type. */
213 static inline bool
214 vrp_val_is_max (const_tree val)
216 tree type_max = vrp_val_max (TREE_TYPE (val));
217 return (val == type_max
218 || (type_max != NULL_TREE
219 && operand_equal_p (val, type_max, 0)));
222 /* Return whether VAL is equal to the minimum value of its type. This
223 will be true for a negative overflow infinity. */
225 static inline bool
226 vrp_val_is_min (const_tree val)
228 tree type_min = vrp_val_min (TREE_TYPE (val));
229 return (val == type_min
230 || (type_min != NULL_TREE
231 && operand_equal_p (val, type_min, 0)));
235 /* Return whether TYPE should use an overflow infinity distinct from
236 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
237 represent a signed overflow during VRP computations. An infinity
238 is distinct from a half-range, which will go from some number to
239 TYPE_{MIN,MAX}_VALUE. */
241 static inline bool
242 needs_overflow_infinity (const_tree type)
244 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
247 /* Return whether TYPE can support our overflow infinity
248 representation: we use the TREE_OVERFLOW flag, which only exists
249 for constants. If TYPE doesn't support this, we don't optimize
250 cases which would require signed overflow--we drop them to
251 VARYING. */
253 static inline bool
254 supports_overflow_infinity (const_tree type)
256 tree min = vrp_val_min (type), max = vrp_val_max (type);
257 #ifdef ENABLE_CHECKING
258 gcc_assert (needs_overflow_infinity (type));
259 #endif
260 return (min != NULL_TREE
261 && CONSTANT_CLASS_P (min)
262 && max != NULL_TREE
263 && CONSTANT_CLASS_P (max));
266 /* VAL is the maximum or minimum value of a type. Return a
267 corresponding overflow infinity. */
269 static inline tree
270 make_overflow_infinity (tree val)
272 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
273 val = copy_node (val);
274 TREE_OVERFLOW (val) = 1;
275 return val;
278 /* Return a negative overflow infinity for TYPE. */
280 static inline tree
281 negative_overflow_infinity (tree type)
283 gcc_checking_assert (supports_overflow_infinity (type));
284 return make_overflow_infinity (vrp_val_min (type));
287 /* Return a positive overflow infinity for TYPE. */
289 static inline tree
290 positive_overflow_infinity (tree type)
292 gcc_checking_assert (supports_overflow_infinity (type));
293 return make_overflow_infinity (vrp_val_max (type));
296 /* Return whether VAL is a negative overflow infinity. */
298 static inline bool
299 is_negative_overflow_infinity (const_tree val)
301 return (TREE_OVERFLOW_P (val)
302 && needs_overflow_infinity (TREE_TYPE (val))
303 && vrp_val_is_min (val));
306 /* Return whether VAL is a positive overflow infinity. */
308 static inline bool
309 is_positive_overflow_infinity (const_tree val)
311 return (TREE_OVERFLOW_P (val)
312 && needs_overflow_infinity (TREE_TYPE (val))
313 && vrp_val_is_max (val));
316 /* Return whether VAL is a positive or negative overflow infinity. */
318 static inline bool
319 is_overflow_infinity (const_tree val)
321 return (TREE_OVERFLOW_P (val)
322 && needs_overflow_infinity (TREE_TYPE (val))
323 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
326 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
328 static inline bool
329 stmt_overflow_infinity (gimple stmt)
331 if (is_gimple_assign (stmt)
332 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
333 GIMPLE_SINGLE_RHS)
334 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
335 return false;
338 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
339 the same value with TREE_OVERFLOW clear. This can be used to avoid
340 confusing a regular value with an overflow value. */
342 static inline tree
343 avoid_overflow_infinity (tree val)
345 if (!is_overflow_infinity (val))
346 return val;
348 if (vrp_val_is_max (val))
349 return vrp_val_max (TREE_TYPE (val));
350 else
352 gcc_checking_assert (vrp_val_is_min (val));
353 return vrp_val_min (TREE_TYPE (val));
358 /* Set value range VR to VR_UNDEFINED. */
360 static inline void
361 set_value_range_to_undefined (value_range_t *vr)
363 vr->type = VR_UNDEFINED;
364 vr->min = vr->max = NULL_TREE;
365 if (vr->equiv)
366 bitmap_clear (vr->equiv);
370 /* Set value range VR to VR_VARYING. */
372 static inline void
373 set_value_range_to_varying (value_range_t *vr)
375 vr->type = VR_VARYING;
376 vr->min = vr->max = NULL_TREE;
377 if (vr->equiv)
378 bitmap_clear (vr->equiv);
382 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
384 static void
385 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
386 tree max, bitmap equiv)
388 #if defined ENABLE_CHECKING
389 /* Check the validity of the range. */
390 if (t == VR_RANGE || t == VR_ANTI_RANGE)
392 int cmp;
394 gcc_assert (min && max);
396 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
397 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
399 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
400 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
402 cmp = compare_values (min, max);
403 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
405 if (needs_overflow_infinity (TREE_TYPE (min)))
406 gcc_assert (!is_overflow_infinity (min)
407 || !is_overflow_infinity (max));
410 if (t == VR_UNDEFINED || t == VR_VARYING)
411 gcc_assert (min == NULL_TREE && max == NULL_TREE);
413 if (t == VR_UNDEFINED || t == VR_VARYING)
414 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
415 #endif
417 vr->type = t;
418 vr->min = min;
419 vr->max = max;
421 /* Since updating the equivalence set involves deep copying the
422 bitmaps, only do it if absolutely necessary. */
423 if (vr->equiv == NULL
424 && equiv != NULL)
425 vr->equiv = BITMAP_ALLOC (NULL);
427 if (equiv != vr->equiv)
429 if (equiv && !bitmap_empty_p (equiv))
430 bitmap_copy (vr->equiv, equiv);
431 else
432 bitmap_clear (vr->equiv);
437 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
438 This means adjusting T, MIN and MAX representing the case of a
439 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
440 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
441 In corner cases where MAX+1 or MIN-1 wraps this will fall back
442 to varying.
443 This routine exists to ease canonicalization in the case where we
444 extract ranges from var + CST op limit. */
446 static void
447 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
448 tree min, tree max, bitmap equiv)
450 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
451 if (t == VR_UNDEFINED)
453 set_value_range_to_undefined (vr);
454 return;
456 else if (t == VR_VARYING)
458 set_value_range_to_varying (vr);
459 return;
462 /* Nothing to canonicalize for symbolic ranges. */
463 if (TREE_CODE (min) != INTEGER_CST
464 || TREE_CODE (max) != INTEGER_CST)
466 set_value_range (vr, t, min, max, equiv);
467 return;
470 /* Wrong order for min and max, to swap them and the VR type we need
471 to adjust them. */
472 if (tree_int_cst_lt (max, min))
474 tree one, tmp;
476 /* For one bit precision if max < min, then the swapped
477 range covers all values, so for VR_RANGE it is varying and
478 for VR_ANTI_RANGE empty range, so drop to varying as well. */
479 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
481 set_value_range_to_varying (vr);
482 return;
485 one = build_int_cst (TREE_TYPE (min), 1);
486 tmp = int_const_binop (PLUS_EXPR, max, one);
487 max = int_const_binop (MINUS_EXPR, min, one);
488 min = tmp;
490 /* There's one corner case, if we had [C+1, C] before we now have
491 that again. But this represents an empty value range, so drop
492 to varying in this case. */
493 if (tree_int_cst_lt (max, min))
495 set_value_range_to_varying (vr);
496 return;
499 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
502 /* Anti-ranges that can be represented as ranges should be so. */
503 if (t == VR_ANTI_RANGE)
505 bool is_min = vrp_val_is_min (min);
506 bool is_max = vrp_val_is_max (max);
508 if (is_min && is_max)
510 /* We cannot deal with empty ranges, drop to varying.
511 ??? This could be VR_UNDEFINED instead. */
512 set_value_range_to_varying (vr);
513 return;
515 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
516 && (is_min || is_max))
518 /* Non-empty boolean ranges can always be represented
519 as a singleton range. */
520 if (is_min)
521 min = max = vrp_val_max (TREE_TYPE (min));
522 else
523 min = max = vrp_val_min (TREE_TYPE (min));
524 t = VR_RANGE;
526 else if (is_min
527 /* As a special exception preserve non-null ranges. */
528 && !(TYPE_UNSIGNED (TREE_TYPE (min))
529 && integer_zerop (max)))
531 tree one = build_int_cst (TREE_TYPE (max), 1);
532 min = int_const_binop (PLUS_EXPR, max, one);
533 max = vrp_val_max (TREE_TYPE (max));
534 t = VR_RANGE;
536 else if (is_max)
538 tree one = build_int_cst (TREE_TYPE (min), 1);
539 max = int_const_binop (MINUS_EXPR, min, one);
540 min = vrp_val_min (TREE_TYPE (min));
541 t = VR_RANGE;
545 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
546 if (needs_overflow_infinity (TREE_TYPE (min))
547 && is_overflow_infinity (min)
548 && is_overflow_infinity (max))
550 set_value_range_to_varying (vr);
551 return;
554 set_value_range (vr, t, min, max, equiv);
557 /* Copy value range FROM into value range TO. */
559 static inline void
560 copy_value_range (value_range_t *to, value_range_t *from)
562 set_value_range (to, from->type, from->min, from->max, from->equiv);
565 /* Set value range VR to a single value. This function is only called
566 with values we get from statements, and exists to clear the
567 TREE_OVERFLOW flag so that we don't think we have an overflow
568 infinity when we shouldn't. */
570 static inline void
571 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
573 gcc_assert (is_gimple_min_invariant (val));
574 if (TREE_OVERFLOW_P (val))
575 val = drop_tree_overflow (val);
576 set_value_range (vr, VR_RANGE, val, val, equiv);
579 /* Set value range VR to a non-negative range of type TYPE.
580 OVERFLOW_INFINITY indicates whether to use an overflow infinity
581 rather than TYPE_MAX_VALUE; this should be true if we determine
582 that the range is nonnegative based on the assumption that signed
583 overflow does not occur. */
585 static inline void
586 set_value_range_to_nonnegative (value_range_t *vr, tree type,
587 bool overflow_infinity)
589 tree zero;
591 if (overflow_infinity && !supports_overflow_infinity (type))
593 set_value_range_to_varying (vr);
594 return;
597 zero = build_int_cst (type, 0);
598 set_value_range (vr, VR_RANGE, zero,
599 (overflow_infinity
600 ? positive_overflow_infinity (type)
601 : TYPE_MAX_VALUE (type)),
602 vr->equiv);
605 /* Set value range VR to a non-NULL range of type TYPE. */
607 static inline void
608 set_value_range_to_nonnull (value_range_t *vr, tree type)
610 tree zero = build_int_cst (type, 0);
611 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
615 /* Set value range VR to a NULL range of type TYPE. */
617 static inline void
618 set_value_range_to_null (value_range_t *vr, tree type)
620 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
624 /* Set value range VR to a range of a truthvalue of type TYPE. */
626 static inline void
627 set_value_range_to_truthvalue (value_range_t *vr, tree type)
629 if (TYPE_PRECISION (type) == 1)
630 set_value_range_to_varying (vr);
631 else
632 set_value_range (vr, VR_RANGE,
633 build_int_cst (type, 0), build_int_cst (type, 1),
634 vr->equiv);
638 /* If abs (min) < abs (max), set VR to [-max, max], if
639 abs (min) >= abs (max), set VR to [-min, min]. */
641 static void
642 abs_extent_range (value_range_t *vr, tree min, tree max)
644 int cmp;
646 gcc_assert (TREE_CODE (min) == INTEGER_CST);
647 gcc_assert (TREE_CODE (max) == INTEGER_CST);
648 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
649 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
650 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
651 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
652 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
654 set_value_range_to_varying (vr);
655 return;
657 cmp = compare_values (min, max);
658 if (cmp == -1)
659 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
660 else if (cmp == 0 || cmp == 1)
662 max = min;
663 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
665 else
667 set_value_range_to_varying (vr);
668 return;
670 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
674 /* Return value range information for VAR.
676 If we have no values ranges recorded (ie, VRP is not running), then
677 return NULL. Otherwise create an empty range if none existed for VAR. */
679 static value_range_t *
680 get_value_range (const_tree var)
682 static const struct value_range_d vr_const_varying
683 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
684 value_range_t *vr;
685 tree sym;
686 unsigned ver = SSA_NAME_VERSION (var);
688 /* If we have no recorded ranges, then return NULL. */
689 if (! vr_value)
690 return NULL;
692 /* If we query the range for a new SSA name return an unmodifiable VARYING.
693 We should get here at most from the substitute-and-fold stage which
694 will never try to change values. */
695 if (ver >= num_vr_values)
696 return CONST_CAST (value_range_t *, &vr_const_varying);
698 vr = vr_value[ver];
699 if (vr)
700 return vr;
702 /* After propagation finished do not allocate new value-ranges. */
703 if (values_propagated)
704 return CONST_CAST (value_range_t *, &vr_const_varying);
706 /* Create a default value range. */
707 vr_value[ver] = vr = XCNEW (value_range_t);
709 /* Defer allocating the equivalence set. */
710 vr->equiv = NULL;
712 /* If VAR is a default definition of a parameter, the variable can
713 take any value in VAR's type. */
714 if (SSA_NAME_IS_DEFAULT_DEF (var))
716 sym = SSA_NAME_VAR (var);
717 if (TREE_CODE (sym) == PARM_DECL)
719 /* Try to use the "nonnull" attribute to create ~[0, 0]
720 anti-ranges for pointers. Note that this is only valid with
721 default definitions of PARM_DECLs. */
722 if (POINTER_TYPE_P (TREE_TYPE (sym))
723 && nonnull_arg_p (sym))
724 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
725 else
726 set_value_range_to_varying (vr);
728 else if (TREE_CODE (sym) == RESULT_DECL
729 && DECL_BY_REFERENCE (sym))
730 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
733 return vr;
736 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
738 static inline bool
739 vrp_operand_equal_p (const_tree val1, const_tree val2)
741 if (val1 == val2)
742 return true;
743 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
744 return false;
745 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
748 /* Return true, if the bitmaps B1 and B2 are equal. */
750 static inline bool
751 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
753 return (b1 == b2
754 || ((!b1 || bitmap_empty_p (b1))
755 && (!b2 || bitmap_empty_p (b2)))
756 || (b1 && b2
757 && bitmap_equal_p (b1, b2)));
760 /* Update the value range and equivalence set for variable VAR to
761 NEW_VR. Return true if NEW_VR is different from VAR's previous
762 value.
764 NOTE: This function assumes that NEW_VR is a temporary value range
765 object created for the sole purpose of updating VAR's range. The
766 storage used by the equivalence set from NEW_VR will be freed by
767 this function. Do not call update_value_range when NEW_VR
768 is the range object associated with another SSA name. */
770 static inline bool
771 update_value_range (const_tree var, value_range_t *new_vr)
773 value_range_t *old_vr;
774 bool is_new;
776 /* If there is a value-range on the SSA name from earlier analysis
777 factor that in. */
778 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
780 wide_int min, max;
781 value_range_type rtype = get_range_info (var, &min, &max);
782 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
784 value_range_d nr;
785 nr.type = rtype;
786 nr.min = wide_int_to_tree (TREE_TYPE (var), min);
787 nr.max = wide_int_to_tree (TREE_TYPE (var), max);
788 nr.equiv = NULL;
789 vrp_intersect_ranges (new_vr, &nr);
793 /* Update the value range, if necessary. */
794 old_vr = get_value_range (var);
795 is_new = old_vr->type != new_vr->type
796 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
797 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
798 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
800 if (is_new)
802 /* Do not allow transitions up the lattice. The following
803 is slightly more awkward than just new_vr->type < old_vr->type
804 because VR_RANGE and VR_ANTI_RANGE need to be considered
805 the same. We may not have is_new when transitioning to
806 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
807 called. */
808 if (new_vr->type == VR_UNDEFINED)
810 BITMAP_FREE (new_vr->equiv);
811 set_value_range_to_varying (old_vr);
812 set_value_range_to_varying (new_vr);
813 return true;
815 else
816 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
817 new_vr->equiv);
820 BITMAP_FREE (new_vr->equiv);
822 return is_new;
826 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
827 point where equivalence processing can be turned on/off. */
829 static void
830 add_equivalence (bitmap *equiv, const_tree var)
832 unsigned ver = SSA_NAME_VERSION (var);
833 value_range_t *vr = vr_value[ver];
835 if (*equiv == NULL)
836 *equiv = BITMAP_ALLOC (NULL);
837 bitmap_set_bit (*equiv, ver);
838 if (vr && vr->equiv)
839 bitmap_ior_into (*equiv, vr->equiv);
843 /* Return true if VR is ~[0, 0]. */
845 static inline bool
846 range_is_nonnull (value_range_t *vr)
848 return vr->type == VR_ANTI_RANGE
849 && integer_zerop (vr->min)
850 && integer_zerop (vr->max);
854 /* Return true if VR is [0, 0]. */
856 static inline bool
857 range_is_null (value_range_t *vr)
859 return vr->type == VR_RANGE
860 && integer_zerop (vr->min)
861 && integer_zerop (vr->max);
864 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
865 a singleton. */
867 static inline bool
868 range_int_cst_p (value_range_t *vr)
870 return (vr->type == VR_RANGE
871 && TREE_CODE (vr->max) == INTEGER_CST
872 && TREE_CODE (vr->min) == INTEGER_CST);
875 /* Return true if VR is a INTEGER_CST singleton. */
877 static inline bool
878 range_int_cst_singleton_p (value_range_t *vr)
880 return (range_int_cst_p (vr)
881 && !is_overflow_infinity (vr->min)
882 && !is_overflow_infinity (vr->max)
883 && tree_int_cst_equal (vr->min, vr->max));
886 /* Return true if value range VR involves at least one symbol. */
888 static inline bool
889 symbolic_range_p (value_range_t *vr)
891 return (!is_gimple_min_invariant (vr->min)
892 || !is_gimple_min_invariant (vr->max));
895 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
896 otherwise. We only handle additive operations and set NEG to true if the
897 symbol is negated and INV to the invariant part, if any. */
899 static tree
900 get_single_symbol (tree t, bool *neg, tree *inv)
902 bool neg_;
903 tree inv_;
905 if (TREE_CODE (t) == PLUS_EXPR
906 || TREE_CODE (t) == POINTER_PLUS_EXPR
907 || TREE_CODE (t) == MINUS_EXPR)
909 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
911 neg_ = (TREE_CODE (t) == MINUS_EXPR);
912 inv_ = TREE_OPERAND (t, 0);
913 t = TREE_OPERAND (t, 1);
915 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
917 neg_ = false;
918 inv_ = TREE_OPERAND (t, 1);
919 t = TREE_OPERAND (t, 0);
921 else
922 return NULL_TREE;
924 else
926 neg_ = false;
927 inv_ = NULL_TREE;
930 if (TREE_CODE (t) == NEGATE_EXPR)
932 t = TREE_OPERAND (t, 0);
933 neg_ = !neg_;
936 if (TREE_CODE (t) != SSA_NAME)
937 return NULL_TREE;
939 *neg = neg_;
940 *inv = inv_;
941 return t;
944 /* The reverse operation: build a symbolic expression with TYPE
945 from symbol SYM, negated according to NEG, and invariant INV. */
947 static tree
948 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
950 const bool pointer_p = POINTER_TYPE_P (type);
951 tree t = sym;
953 if (neg)
954 t = build1 (NEGATE_EXPR, type, t);
956 if (integer_zerop (inv))
957 return t;
959 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
962 /* Return true if value range VR involves exactly one symbol SYM. */
964 static bool
965 symbolic_range_based_on_p (value_range_t *vr, const_tree sym)
967 bool neg, min_has_symbol, max_has_symbol;
968 tree inv;
970 if (is_gimple_min_invariant (vr->min))
971 min_has_symbol = false;
972 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
973 min_has_symbol = true;
974 else
975 return false;
977 if (is_gimple_min_invariant (vr->max))
978 max_has_symbol = false;
979 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
980 max_has_symbol = true;
981 else
982 return false;
984 return (min_has_symbol || max_has_symbol);
987 /* Return true if value range VR uses an overflow infinity. */
989 static inline bool
990 overflow_infinity_range_p (value_range_t *vr)
992 return (vr->type == VR_RANGE
993 && (is_overflow_infinity (vr->min)
994 || is_overflow_infinity (vr->max)));
997 /* Return false if we can not make a valid comparison based on VR;
998 this will be the case if it uses an overflow infinity and overflow
999 is not undefined (i.e., -fno-strict-overflow is in effect).
1000 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1001 uses an overflow infinity. */
1003 static bool
1004 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
1006 gcc_assert (vr->type == VR_RANGE);
1007 if (is_overflow_infinity (vr->min))
1009 *strict_overflow_p = true;
1010 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1011 return false;
1013 if (is_overflow_infinity (vr->max))
1015 *strict_overflow_p = true;
1016 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1017 return false;
1019 return true;
1023 /* Return true if the result of assignment STMT is know to be non-negative.
1024 If the return value is based on the assumption that signed overflow is
1025 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1026 *STRICT_OVERFLOW_P.*/
1028 static bool
1029 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1031 enum tree_code code = gimple_assign_rhs_code (stmt);
1032 switch (get_gimple_rhs_class (code))
1034 case GIMPLE_UNARY_RHS:
1035 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1036 gimple_expr_type (stmt),
1037 gimple_assign_rhs1 (stmt),
1038 strict_overflow_p);
1039 case GIMPLE_BINARY_RHS:
1040 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1041 gimple_expr_type (stmt),
1042 gimple_assign_rhs1 (stmt),
1043 gimple_assign_rhs2 (stmt),
1044 strict_overflow_p);
1045 case GIMPLE_TERNARY_RHS:
1046 return false;
1047 case GIMPLE_SINGLE_RHS:
1048 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
1049 strict_overflow_p);
1050 case GIMPLE_INVALID_RHS:
1051 gcc_unreachable ();
1052 default:
1053 gcc_unreachable ();
1057 /* Return true if return value of call STMT is know to be non-negative.
1058 If the return value is based on the assumption that signed overflow is
1059 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1060 *STRICT_OVERFLOW_P.*/
1062 static bool
1063 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1065 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1066 gimple_call_arg (stmt, 0) : NULL_TREE;
1067 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1068 gimple_call_arg (stmt, 1) : NULL_TREE;
1070 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1071 gimple_call_fndecl (stmt),
1072 arg0,
1073 arg1,
1074 strict_overflow_p);
1077 /* Return true if STMT is know to compute a non-negative value.
1078 If the return value is based on the assumption that signed overflow is
1079 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1080 *STRICT_OVERFLOW_P.*/
1082 static bool
1083 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1085 switch (gimple_code (stmt))
1087 case GIMPLE_ASSIGN:
1088 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1089 case GIMPLE_CALL:
1090 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1091 default:
1092 gcc_unreachable ();
1096 /* Return true if the result of assignment STMT is know to be non-zero.
1097 If the return value is based on the assumption that signed overflow is
1098 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1099 *STRICT_OVERFLOW_P.*/
1101 static bool
1102 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1104 enum tree_code code = gimple_assign_rhs_code (stmt);
1105 switch (get_gimple_rhs_class (code))
1107 case GIMPLE_UNARY_RHS:
1108 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1109 gimple_expr_type (stmt),
1110 gimple_assign_rhs1 (stmt),
1111 strict_overflow_p);
1112 case GIMPLE_BINARY_RHS:
1113 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1114 gimple_expr_type (stmt),
1115 gimple_assign_rhs1 (stmt),
1116 gimple_assign_rhs2 (stmt),
1117 strict_overflow_p);
1118 case GIMPLE_TERNARY_RHS:
1119 return false;
1120 case GIMPLE_SINGLE_RHS:
1121 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1122 strict_overflow_p);
1123 case GIMPLE_INVALID_RHS:
1124 gcc_unreachable ();
1125 default:
1126 gcc_unreachable ();
1130 /* Return true if STMT is known to compute a non-zero value.
1131 If the return value is based on the assumption that signed overflow is
1132 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1133 *STRICT_OVERFLOW_P.*/
1135 static bool
1136 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1138 switch (gimple_code (stmt))
1140 case GIMPLE_ASSIGN:
1141 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1142 case GIMPLE_CALL:
1144 tree fndecl = gimple_call_fndecl (stmt);
1145 if (!fndecl) return false;
1146 if (flag_delete_null_pointer_checks && !flag_check_new
1147 && DECL_IS_OPERATOR_NEW (fndecl)
1148 && !TREE_NOTHROW (fndecl))
1149 return true;
1150 /* References are always non-NULL. */
1151 if (flag_delete_null_pointer_checks
1152 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1153 return true;
1154 if (flag_delete_null_pointer_checks &&
1155 lookup_attribute ("returns_nonnull",
1156 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1157 return true;
1158 return gimple_alloca_call_p (stmt);
1160 default:
1161 gcc_unreachable ();
1165 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1166 obtained so far. */
1168 static bool
1169 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1171 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1172 return true;
1174 /* If we have an expression of the form &X->a, then the expression
1175 is nonnull if X is nonnull. */
1176 if (is_gimple_assign (stmt)
1177 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1179 tree expr = gimple_assign_rhs1 (stmt);
1180 tree base = get_base_address (TREE_OPERAND (expr, 0));
1182 if (base != NULL_TREE
1183 && TREE_CODE (base) == MEM_REF
1184 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1186 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1187 if (range_is_nonnull (vr))
1188 return true;
1192 return false;
1195 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1196 a gimple invariant, or SSA_NAME +- CST. */
1198 static bool
1199 valid_value_p (tree expr)
1201 if (TREE_CODE (expr) == SSA_NAME)
1202 return true;
1204 if (TREE_CODE (expr) == PLUS_EXPR
1205 || TREE_CODE (expr) == MINUS_EXPR)
1206 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1207 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1209 return is_gimple_min_invariant (expr);
1212 /* Return
1213 1 if VAL < VAL2
1214 0 if !(VAL < VAL2)
1215 -2 if those are incomparable. */
1216 static inline int
1217 operand_less_p (tree val, tree val2)
1219 /* LT is folded faster than GE and others. Inline the common case. */
1220 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1221 return tree_int_cst_lt (val, val2);
1222 else
1224 tree tcmp;
1226 fold_defer_overflow_warnings ();
1228 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1230 fold_undefer_and_ignore_overflow_warnings ();
1232 if (!tcmp
1233 || TREE_CODE (tcmp) != INTEGER_CST)
1234 return -2;
1236 if (!integer_zerop (tcmp))
1237 return 1;
1240 /* val >= val2, not considering overflow infinity. */
1241 if (is_negative_overflow_infinity (val))
1242 return is_negative_overflow_infinity (val2) ? 0 : 1;
1243 else if (is_positive_overflow_infinity (val2))
1244 return is_positive_overflow_infinity (val) ? 0 : 1;
1246 return 0;
1249 /* Compare two values VAL1 and VAL2. Return
1251 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1252 -1 if VAL1 < VAL2,
1253 0 if VAL1 == VAL2,
1254 +1 if VAL1 > VAL2, and
1255 +2 if VAL1 != VAL2
1257 This is similar to tree_int_cst_compare but supports pointer values
1258 and values that cannot be compared at compile time.
1260 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1261 true if the return value is only valid if we assume that signed
1262 overflow is undefined. */
1264 static int
1265 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1267 if (val1 == val2)
1268 return 0;
1270 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1271 both integers. */
1272 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1273 == POINTER_TYPE_P (TREE_TYPE (val2)));
1275 /* Convert the two values into the same type. This is needed because
1276 sizetype causes sign extension even for unsigned types. */
1277 val2 = fold_convert (TREE_TYPE (val1), val2);
1278 STRIP_USELESS_TYPE_CONVERSION (val2);
1280 if ((TREE_CODE (val1) == SSA_NAME
1281 || (TREE_CODE (val1) == NEGATE_EXPR
1282 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1283 || TREE_CODE (val1) == PLUS_EXPR
1284 || TREE_CODE (val1) == MINUS_EXPR)
1285 && (TREE_CODE (val2) == SSA_NAME
1286 || (TREE_CODE (val2) == NEGATE_EXPR
1287 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1288 || TREE_CODE (val2) == PLUS_EXPR
1289 || TREE_CODE (val2) == MINUS_EXPR))
1291 tree n1, c1, n2, c2;
1292 enum tree_code code1, code2;
1294 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1295 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1296 same name, return -2. */
1297 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1299 code1 = SSA_NAME;
1300 n1 = val1;
1301 c1 = NULL_TREE;
1303 else
1305 code1 = TREE_CODE (val1);
1306 n1 = TREE_OPERAND (val1, 0);
1307 c1 = TREE_OPERAND (val1, 1);
1308 if (tree_int_cst_sgn (c1) == -1)
1310 if (is_negative_overflow_infinity (c1))
1311 return -2;
1312 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1313 if (!c1)
1314 return -2;
1315 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1319 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1321 code2 = SSA_NAME;
1322 n2 = val2;
1323 c2 = NULL_TREE;
1325 else
1327 code2 = TREE_CODE (val2);
1328 n2 = TREE_OPERAND (val2, 0);
1329 c2 = TREE_OPERAND (val2, 1);
1330 if (tree_int_cst_sgn (c2) == -1)
1332 if (is_negative_overflow_infinity (c2))
1333 return -2;
1334 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1335 if (!c2)
1336 return -2;
1337 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1341 /* Both values must use the same name. */
1342 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1344 n1 = TREE_OPERAND (n1, 0);
1345 n2 = TREE_OPERAND (n2, 0);
1347 if (n1 != n2)
1348 return -2;
1350 if (code1 == SSA_NAME && code2 == SSA_NAME)
1351 /* NAME == NAME */
1352 return 0;
1354 /* If overflow is defined we cannot simplify more. */
1355 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1356 return -2;
1358 if (strict_overflow_p != NULL
1359 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1360 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1361 *strict_overflow_p = true;
1363 if (code1 == SSA_NAME)
1365 if (code2 == PLUS_EXPR)
1366 /* NAME < NAME + CST */
1367 return -1;
1368 else if (code2 == MINUS_EXPR)
1369 /* NAME > NAME - CST */
1370 return 1;
1372 else if (code1 == PLUS_EXPR)
1374 if (code2 == SSA_NAME)
1375 /* NAME + CST > NAME */
1376 return 1;
1377 else if (code2 == PLUS_EXPR)
1378 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1379 return compare_values_warnv (c1, c2, strict_overflow_p);
1380 else if (code2 == MINUS_EXPR)
1381 /* NAME + CST1 > NAME - CST2 */
1382 return 1;
1384 else if (code1 == MINUS_EXPR)
1386 if (code2 == SSA_NAME)
1387 /* NAME - CST < NAME */
1388 return -1;
1389 else if (code2 == PLUS_EXPR)
1390 /* NAME - CST1 < NAME + CST2 */
1391 return -1;
1392 else if (code2 == MINUS_EXPR)
1393 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1394 C1 and C2 are swapped in the call to compare_values. */
1395 return compare_values_warnv (c2, c1, strict_overflow_p);
1398 gcc_unreachable ();
1401 /* We cannot compare non-constants. */
1402 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1403 return -2;
1405 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1407 /* We cannot compare overflowed values, except for overflow
1408 infinities. */
1409 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1411 if (strict_overflow_p != NULL)
1412 *strict_overflow_p = true;
1413 if (is_negative_overflow_infinity (val1))
1414 return is_negative_overflow_infinity (val2) ? 0 : -1;
1415 else if (is_negative_overflow_infinity (val2))
1416 return 1;
1417 else if (is_positive_overflow_infinity (val1))
1418 return is_positive_overflow_infinity (val2) ? 0 : 1;
1419 else if (is_positive_overflow_infinity (val2))
1420 return -1;
1421 return -2;
1424 return tree_int_cst_compare (val1, val2);
1426 else
1428 tree t;
1430 /* First see if VAL1 and VAL2 are not the same. */
1431 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1432 return 0;
1434 /* If VAL1 is a lower address than VAL2, return -1. */
1435 if (operand_less_p (val1, val2) == 1)
1436 return -1;
1438 /* If VAL1 is a higher address than VAL2, return +1. */
1439 if (operand_less_p (val2, val1) == 1)
1440 return 1;
1442 /* If VAL1 is different than VAL2, return +2.
1443 For integer constants we either have already returned -1 or 1
1444 or they are equivalent. We still might succeed in proving
1445 something about non-trivial operands. */
1446 if (TREE_CODE (val1) != INTEGER_CST
1447 || TREE_CODE (val2) != INTEGER_CST)
1449 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1450 if (t && integer_onep (t))
1451 return 2;
1454 return -2;
1458 /* Compare values like compare_values_warnv, but treat comparisons of
1459 nonconstants which rely on undefined overflow as incomparable. */
1461 static int
1462 compare_values (tree val1, tree val2)
1464 bool sop;
1465 int ret;
1467 sop = false;
1468 ret = compare_values_warnv (val1, val2, &sop);
1469 if (sop
1470 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1471 ret = -2;
1472 return ret;
1476 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1477 0 if VAL is not inside [MIN, MAX],
1478 -2 if we cannot tell either way.
1480 Benchmark compile/20001226-1.c compilation time after changing this
1481 function. */
1483 static inline int
1484 value_inside_range (tree val, tree min, tree max)
1486 int cmp1, cmp2;
1488 cmp1 = operand_less_p (val, min);
1489 if (cmp1 == -2)
1490 return -2;
1491 if (cmp1 == 1)
1492 return 0;
1494 cmp2 = operand_less_p (max, val);
1495 if (cmp2 == -2)
1496 return -2;
1498 return !cmp2;
1502 /* Return true if value ranges VR0 and VR1 have a non-empty
1503 intersection.
1505 Benchmark compile/20001226-1.c compilation time after changing this
1506 function.
1509 static inline bool
1510 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1512 /* The value ranges do not intersect if the maximum of the first range is
1513 less than the minimum of the second range or vice versa.
1514 When those relations are unknown, we can't do any better. */
1515 if (operand_less_p (vr0->max, vr1->min) != 0)
1516 return false;
1517 if (operand_less_p (vr1->max, vr0->min) != 0)
1518 return false;
1519 return true;
1523 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1524 include the value zero, -2 if we cannot tell. */
1526 static inline int
1527 range_includes_zero_p (tree min, tree max)
1529 tree zero = build_int_cst (TREE_TYPE (min), 0);
1530 return value_inside_range (zero, min, max);
1533 /* Return true if *VR is know to only contain nonnegative values. */
1535 static inline bool
1536 value_range_nonnegative_p (value_range_t *vr)
1538 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1539 which would return a useful value should be encoded as a
1540 VR_RANGE. */
1541 if (vr->type == VR_RANGE)
1543 int result = compare_values (vr->min, integer_zero_node);
1544 return (result == 0 || result == 1);
1547 return false;
1550 /* If *VR has a value rante that is a single constant value return that,
1551 otherwise return NULL_TREE. */
1553 static tree
1554 value_range_constant_singleton (value_range_t *vr)
1556 if (vr->type == VR_RANGE
1557 && operand_equal_p (vr->min, vr->max, 0)
1558 && is_gimple_min_invariant (vr->min))
1559 return vr->min;
1561 return NULL_TREE;
1564 /* If OP has a value range with a single constant value return that,
1565 otherwise return NULL_TREE. This returns OP itself if OP is a
1566 constant. */
1568 static tree
1569 op_with_constant_singleton_value_range (tree op)
1571 if (is_gimple_min_invariant (op))
1572 return op;
1574 if (TREE_CODE (op) != SSA_NAME)
1575 return NULL_TREE;
1577 return value_range_constant_singleton (get_value_range (op));
1580 /* Return true if op is in a boolean [0, 1] value-range. */
1582 static bool
1583 op_with_boolean_value_range_p (tree op)
1585 value_range_t *vr;
1587 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1588 return true;
1590 if (integer_zerop (op)
1591 || integer_onep (op))
1592 return true;
1594 if (TREE_CODE (op) != SSA_NAME)
1595 return false;
1597 vr = get_value_range (op);
1598 return (vr->type == VR_RANGE
1599 && integer_zerop (vr->min)
1600 && integer_onep (vr->max));
1603 /* Extract value range information from an ASSERT_EXPR EXPR and store
1604 it in *VR_P. */
1606 static void
1607 extract_range_from_assert (value_range_t *vr_p, tree expr)
1609 tree var, cond, limit, min, max, type;
1610 value_range_t *limit_vr;
1611 enum tree_code cond_code;
1613 var = ASSERT_EXPR_VAR (expr);
1614 cond = ASSERT_EXPR_COND (expr);
1616 gcc_assert (COMPARISON_CLASS_P (cond));
1618 /* Find VAR in the ASSERT_EXPR conditional. */
1619 if (var == TREE_OPERAND (cond, 0)
1620 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1621 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1623 /* If the predicate is of the form VAR COMP LIMIT, then we just
1624 take LIMIT from the RHS and use the same comparison code. */
1625 cond_code = TREE_CODE (cond);
1626 limit = TREE_OPERAND (cond, 1);
1627 cond = TREE_OPERAND (cond, 0);
1629 else
1631 /* If the predicate is of the form LIMIT COMP VAR, then we need
1632 to flip around the comparison code to create the proper range
1633 for VAR. */
1634 cond_code = swap_tree_comparison (TREE_CODE (cond));
1635 limit = TREE_OPERAND (cond, 0);
1636 cond = TREE_OPERAND (cond, 1);
1639 limit = avoid_overflow_infinity (limit);
1641 type = TREE_TYPE (var);
1642 gcc_assert (limit != var);
1644 /* For pointer arithmetic, we only keep track of pointer equality
1645 and inequality. */
1646 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1648 set_value_range_to_varying (vr_p);
1649 return;
1652 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1653 try to use LIMIT's range to avoid creating symbolic ranges
1654 unnecessarily. */
1655 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1657 /* LIMIT's range is only interesting if it has any useful information. */
1658 if (limit_vr
1659 && (limit_vr->type == VR_UNDEFINED
1660 || limit_vr->type == VR_VARYING
1661 || symbolic_range_p (limit_vr)))
1662 limit_vr = NULL;
1664 /* Initially, the new range has the same set of equivalences of
1665 VAR's range. This will be revised before returning the final
1666 value. Since assertions may be chained via mutually exclusive
1667 predicates, we will need to trim the set of equivalences before
1668 we are done. */
1669 gcc_assert (vr_p->equiv == NULL);
1670 add_equivalence (&vr_p->equiv, var);
1672 /* Extract a new range based on the asserted comparison for VAR and
1673 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1674 will only use it for equality comparisons (EQ_EXPR). For any
1675 other kind of assertion, we cannot derive a range from LIMIT's
1676 anti-range that can be used to describe the new range. For
1677 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1678 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1679 no single range for x_2 that could describe LE_EXPR, so we might
1680 as well build the range [b_4, +INF] for it.
1681 One special case we handle is extracting a range from a
1682 range test encoded as (unsigned)var + CST <= limit. */
1683 if (TREE_CODE (cond) == NOP_EXPR
1684 || TREE_CODE (cond) == PLUS_EXPR)
1686 if (TREE_CODE (cond) == PLUS_EXPR)
1688 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1689 TREE_OPERAND (cond, 1));
1690 max = int_const_binop (PLUS_EXPR, limit, min);
1691 cond = TREE_OPERAND (cond, 0);
1693 else
1695 min = build_int_cst (TREE_TYPE (var), 0);
1696 max = limit;
1699 /* Make sure to not set TREE_OVERFLOW on the final type
1700 conversion. We are willingly interpreting large positive
1701 unsigned values as negative signed values here. */
1702 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1703 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1705 /* We can transform a max, min range to an anti-range or
1706 vice-versa. Use set_and_canonicalize_value_range which does
1707 this for us. */
1708 if (cond_code == LE_EXPR)
1709 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1710 min, max, vr_p->equiv);
1711 else if (cond_code == GT_EXPR)
1712 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1713 min, max, vr_p->equiv);
1714 else
1715 gcc_unreachable ();
1717 else if (cond_code == EQ_EXPR)
1719 enum value_range_type range_type;
1721 if (limit_vr)
1723 range_type = limit_vr->type;
1724 min = limit_vr->min;
1725 max = limit_vr->max;
1727 else
1729 range_type = VR_RANGE;
1730 min = limit;
1731 max = limit;
1734 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1736 /* When asserting the equality VAR == LIMIT and LIMIT is another
1737 SSA name, the new range will also inherit the equivalence set
1738 from LIMIT. */
1739 if (TREE_CODE (limit) == SSA_NAME)
1740 add_equivalence (&vr_p->equiv, limit);
1742 else if (cond_code == NE_EXPR)
1744 /* As described above, when LIMIT's range is an anti-range and
1745 this assertion is an inequality (NE_EXPR), then we cannot
1746 derive anything from the anti-range. For instance, if
1747 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1748 not imply that VAR's range is [0, 0]. So, in the case of
1749 anti-ranges, we just assert the inequality using LIMIT and
1750 not its anti-range.
1752 If LIMIT_VR is a range, we can only use it to build a new
1753 anti-range if LIMIT_VR is a single-valued range. For
1754 instance, if LIMIT_VR is [0, 1], the predicate
1755 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1756 Rather, it means that for value 0 VAR should be ~[0, 0]
1757 and for value 1, VAR should be ~[1, 1]. We cannot
1758 represent these ranges.
1760 The only situation in which we can build a valid
1761 anti-range is when LIMIT_VR is a single-valued range
1762 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1763 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1764 if (limit_vr
1765 && limit_vr->type == VR_RANGE
1766 && compare_values (limit_vr->min, limit_vr->max) == 0)
1768 min = limit_vr->min;
1769 max = limit_vr->max;
1771 else
1773 /* In any other case, we cannot use LIMIT's range to build a
1774 valid anti-range. */
1775 min = max = limit;
1778 /* If MIN and MAX cover the whole range for their type, then
1779 just use the original LIMIT. */
1780 if (INTEGRAL_TYPE_P (type)
1781 && vrp_val_is_min (min)
1782 && vrp_val_is_max (max))
1783 min = max = limit;
1785 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1786 min, max, vr_p->equiv);
1788 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1790 min = TYPE_MIN_VALUE (type);
1792 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1793 max = limit;
1794 else
1796 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1797 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1798 LT_EXPR. */
1799 max = limit_vr->max;
1802 /* If the maximum value forces us to be out of bounds, simply punt.
1803 It would be pointless to try and do anything more since this
1804 all should be optimized away above us. */
1805 if ((cond_code == LT_EXPR
1806 && compare_values (max, min) == 0)
1807 || is_overflow_infinity (max))
1808 set_value_range_to_varying (vr_p);
1809 else
1811 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1812 if (cond_code == LT_EXPR)
1814 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1815 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1816 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1817 build_int_cst (TREE_TYPE (max), -1));
1818 else
1819 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1820 build_int_cst (TREE_TYPE (max), 1));
1821 if (EXPR_P (max))
1822 TREE_NO_WARNING (max) = 1;
1825 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1828 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1830 max = TYPE_MAX_VALUE (type);
1832 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1833 min = limit;
1834 else
1836 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1837 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1838 GT_EXPR. */
1839 min = limit_vr->min;
1842 /* If the minimum value forces us to be out of bounds, simply punt.
1843 It would be pointless to try and do anything more since this
1844 all should be optimized away above us. */
1845 if ((cond_code == GT_EXPR
1846 && compare_values (min, max) == 0)
1847 || is_overflow_infinity (min))
1848 set_value_range_to_varying (vr_p);
1849 else
1851 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1852 if (cond_code == GT_EXPR)
1854 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1855 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1856 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1857 build_int_cst (TREE_TYPE (min), -1));
1858 else
1859 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1860 build_int_cst (TREE_TYPE (min), 1));
1861 if (EXPR_P (min))
1862 TREE_NO_WARNING (min) = 1;
1865 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1868 else
1869 gcc_unreachable ();
1871 /* Finally intersect the new range with what we already know about var. */
1872 vrp_intersect_ranges (vr_p, get_value_range (var));
1876 /* Extract range information from SSA name VAR and store it in VR. If
1877 VAR has an interesting range, use it. Otherwise, create the
1878 range [VAR, VAR] and return it. This is useful in situations where
1879 we may have conditionals testing values of VARYING names. For
1880 instance,
1882 x_3 = y_5;
1883 if (x_3 > y_5)
1886 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1887 always false. */
1889 static void
1890 extract_range_from_ssa_name (value_range_t *vr, tree var)
1892 value_range_t *var_vr = get_value_range (var);
1894 if (var_vr->type != VR_VARYING)
1895 copy_value_range (vr, var_vr);
1896 else
1897 set_value_range (vr, VR_RANGE, var, var, NULL);
1899 add_equivalence (&vr->equiv, var);
1903 /* Wrapper around int_const_binop. If the operation overflows and we
1904 are not using wrapping arithmetic, then adjust the result to be
1905 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1906 NULL_TREE if we need to use an overflow infinity representation but
1907 the type does not support it. */
1909 static tree
1910 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1912 tree res;
1914 res = int_const_binop (code, val1, val2);
1916 /* If we are using unsigned arithmetic, operate symbolically
1917 on -INF and +INF as int_const_binop only handles signed overflow. */
1918 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1920 int checkz = compare_values (res, val1);
1921 bool overflow = false;
1923 /* Ensure that res = val1 [+*] val2 >= val1
1924 or that res = val1 - val2 <= val1. */
1925 if ((code == PLUS_EXPR
1926 && !(checkz == 1 || checkz == 0))
1927 || (code == MINUS_EXPR
1928 && !(checkz == 0 || checkz == -1)))
1930 overflow = true;
1932 /* Checking for multiplication overflow is done by dividing the
1933 output of the multiplication by the first input of the
1934 multiplication. If the result of that division operation is
1935 not equal to the second input of the multiplication, then the
1936 multiplication overflowed. */
1937 else if (code == MULT_EXPR && !integer_zerop (val1))
1939 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1940 res,
1941 val1);
1942 int check = compare_values (tmp, val2);
1944 if (check != 0)
1945 overflow = true;
1948 if (overflow)
1950 res = copy_node (res);
1951 TREE_OVERFLOW (res) = 1;
1955 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1956 /* If the singed operation wraps then int_const_binop has done
1957 everything we want. */
1959 /* Signed division of -1/0 overflows and by the time it gets here
1960 returns NULL_TREE. */
1961 else if (!res)
1962 return NULL_TREE;
1963 else if ((TREE_OVERFLOW (res)
1964 && !TREE_OVERFLOW (val1)
1965 && !TREE_OVERFLOW (val2))
1966 || is_overflow_infinity (val1)
1967 || is_overflow_infinity (val2))
1969 /* If the operation overflowed but neither VAL1 nor VAL2 are
1970 overflown, return -INF or +INF depending on the operation
1971 and the combination of signs of the operands. */
1972 int sgn1 = tree_int_cst_sgn (val1);
1973 int sgn2 = tree_int_cst_sgn (val2);
1975 if (needs_overflow_infinity (TREE_TYPE (res))
1976 && !supports_overflow_infinity (TREE_TYPE (res)))
1977 return NULL_TREE;
1979 /* We have to punt on adding infinities of different signs,
1980 since we can't tell what the sign of the result should be.
1981 Likewise for subtracting infinities of the same sign. */
1982 if (((code == PLUS_EXPR && sgn1 != sgn2)
1983 || (code == MINUS_EXPR && sgn1 == sgn2))
1984 && is_overflow_infinity (val1)
1985 && is_overflow_infinity (val2))
1986 return NULL_TREE;
1988 /* Don't try to handle division or shifting of infinities. */
1989 if ((code == TRUNC_DIV_EXPR
1990 || code == FLOOR_DIV_EXPR
1991 || code == CEIL_DIV_EXPR
1992 || code == EXACT_DIV_EXPR
1993 || code == ROUND_DIV_EXPR
1994 || code == RSHIFT_EXPR)
1995 && (is_overflow_infinity (val1)
1996 || is_overflow_infinity (val2)))
1997 return NULL_TREE;
1999 /* Notice that we only need to handle the restricted set of
2000 operations handled by extract_range_from_binary_expr.
2001 Among them, only multiplication, addition and subtraction
2002 can yield overflow without overflown operands because we
2003 are working with integral types only... except in the
2004 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2005 for division too. */
2007 /* For multiplication, the sign of the overflow is given
2008 by the comparison of the signs of the operands. */
2009 if ((code == MULT_EXPR && sgn1 == sgn2)
2010 /* For addition, the operands must be of the same sign
2011 to yield an overflow. Its sign is therefore that
2012 of one of the operands, for example the first. For
2013 infinite operands X + -INF is negative, not positive. */
2014 || (code == PLUS_EXPR
2015 && (sgn1 >= 0
2016 ? !is_negative_overflow_infinity (val2)
2017 : is_positive_overflow_infinity (val2)))
2018 /* For subtraction, non-infinite operands must be of
2019 different signs to yield an overflow. Its sign is
2020 therefore that of the first operand or the opposite of
2021 that of the second operand. A first operand of 0 counts
2022 as positive here, for the corner case 0 - (-INF), which
2023 overflows, but must yield +INF. For infinite operands 0
2024 - INF is negative, not positive. */
2025 || (code == MINUS_EXPR
2026 && (sgn1 >= 0
2027 ? !is_positive_overflow_infinity (val2)
2028 : is_negative_overflow_infinity (val2)))
2029 /* We only get in here with positive shift count, so the
2030 overflow direction is the same as the sign of val1.
2031 Actually rshift does not overflow at all, but we only
2032 handle the case of shifting overflowed -INF and +INF. */
2033 || (code == RSHIFT_EXPR
2034 && sgn1 >= 0)
2035 /* For division, the only case is -INF / -1 = +INF. */
2036 || code == TRUNC_DIV_EXPR
2037 || code == FLOOR_DIV_EXPR
2038 || code == CEIL_DIV_EXPR
2039 || code == EXACT_DIV_EXPR
2040 || code == ROUND_DIV_EXPR)
2041 return (needs_overflow_infinity (TREE_TYPE (res))
2042 ? positive_overflow_infinity (TREE_TYPE (res))
2043 : TYPE_MAX_VALUE (TREE_TYPE (res)));
2044 else
2045 return (needs_overflow_infinity (TREE_TYPE (res))
2046 ? negative_overflow_infinity (TREE_TYPE (res))
2047 : TYPE_MIN_VALUE (TREE_TYPE (res)));
2050 return res;
2054 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2055 bitmask if some bit is unset, it means for all numbers in the range
2056 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2057 bitmask if some bit is set, it means for all numbers in the range
2058 the bit is 1, otherwise it might be 0 or 1. */
2060 static bool
2061 zero_nonzero_bits_from_vr (const tree expr_type,
2062 value_range_t *vr,
2063 wide_int *may_be_nonzero,
2064 wide_int *must_be_nonzero)
2066 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2067 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2068 if (!range_int_cst_p (vr)
2069 || is_overflow_infinity (vr->min)
2070 || is_overflow_infinity (vr->max))
2071 return false;
2073 if (range_int_cst_singleton_p (vr))
2075 *may_be_nonzero = vr->min;
2076 *must_be_nonzero = *may_be_nonzero;
2078 else if (tree_int_cst_sgn (vr->min) >= 0
2079 || tree_int_cst_sgn (vr->max) < 0)
2081 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2082 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2083 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2084 if (xor_mask != 0)
2086 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2087 may_be_nonzero->get_precision ());
2088 *may_be_nonzero = *may_be_nonzero | mask;
2089 *must_be_nonzero = must_be_nonzero->and_not (mask);
2093 return true;
2096 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2097 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2098 false otherwise. If *AR can be represented with a single range
2099 *VR1 will be VR_UNDEFINED. */
2101 static bool
2102 ranges_from_anti_range (value_range_t *ar,
2103 value_range_t *vr0, value_range_t *vr1)
2105 tree type = TREE_TYPE (ar->min);
2107 vr0->type = VR_UNDEFINED;
2108 vr1->type = VR_UNDEFINED;
2110 if (ar->type != VR_ANTI_RANGE
2111 || TREE_CODE (ar->min) != INTEGER_CST
2112 || TREE_CODE (ar->max) != INTEGER_CST
2113 || !vrp_val_min (type)
2114 || !vrp_val_max (type))
2115 return false;
2117 if (!vrp_val_is_min (ar->min))
2119 vr0->type = VR_RANGE;
2120 vr0->min = vrp_val_min (type);
2121 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2123 if (!vrp_val_is_max (ar->max))
2125 vr1->type = VR_RANGE;
2126 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2127 vr1->max = vrp_val_max (type);
2129 if (vr0->type == VR_UNDEFINED)
2131 *vr0 = *vr1;
2132 vr1->type = VR_UNDEFINED;
2135 return vr0->type != VR_UNDEFINED;
2138 /* Helper to extract a value-range *VR for a multiplicative operation
2139 *VR0 CODE *VR1. */
2141 static void
2142 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2143 enum tree_code code,
2144 value_range_t *vr0, value_range_t *vr1)
2146 enum value_range_type type;
2147 tree val[4];
2148 size_t i;
2149 tree min, max;
2150 bool sop;
2151 int cmp;
2153 /* Multiplications, divisions and shifts are a bit tricky to handle,
2154 depending on the mix of signs we have in the two ranges, we
2155 need to operate on different values to get the minimum and
2156 maximum values for the new range. One approach is to figure
2157 out all the variations of range combinations and do the
2158 operations.
2160 However, this involves several calls to compare_values and it
2161 is pretty convoluted. It's simpler to do the 4 operations
2162 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2163 MAX1) and then figure the smallest and largest values to form
2164 the new range. */
2165 gcc_assert (code == MULT_EXPR
2166 || code == TRUNC_DIV_EXPR
2167 || code == FLOOR_DIV_EXPR
2168 || code == CEIL_DIV_EXPR
2169 || code == EXACT_DIV_EXPR
2170 || code == ROUND_DIV_EXPR
2171 || code == RSHIFT_EXPR
2172 || code == LSHIFT_EXPR);
2173 gcc_assert ((vr0->type == VR_RANGE
2174 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2175 && vr0->type == vr1->type);
2177 type = vr0->type;
2179 /* Compute the 4 cross operations. */
2180 sop = false;
2181 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2182 if (val[0] == NULL_TREE)
2183 sop = true;
2185 if (vr1->max == vr1->min)
2186 val[1] = NULL_TREE;
2187 else
2189 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2190 if (val[1] == NULL_TREE)
2191 sop = true;
2194 if (vr0->max == vr0->min)
2195 val[2] = NULL_TREE;
2196 else
2198 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2199 if (val[2] == NULL_TREE)
2200 sop = true;
2203 if (vr0->min == vr0->max || vr1->min == vr1->max)
2204 val[3] = NULL_TREE;
2205 else
2207 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2208 if (val[3] == NULL_TREE)
2209 sop = true;
2212 if (sop)
2214 set_value_range_to_varying (vr);
2215 return;
2218 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2219 of VAL[i]. */
2220 min = val[0];
2221 max = val[0];
2222 for (i = 1; i < 4; i++)
2224 if (!is_gimple_min_invariant (min)
2225 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2226 || !is_gimple_min_invariant (max)
2227 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2228 break;
2230 if (val[i])
2232 if (!is_gimple_min_invariant (val[i])
2233 || (TREE_OVERFLOW (val[i])
2234 && !is_overflow_infinity (val[i])))
2236 /* If we found an overflowed value, set MIN and MAX
2237 to it so that we set the resulting range to
2238 VARYING. */
2239 min = max = val[i];
2240 break;
2243 if (compare_values (val[i], min) == -1)
2244 min = val[i];
2246 if (compare_values (val[i], max) == 1)
2247 max = val[i];
2251 /* If either MIN or MAX overflowed, then set the resulting range to
2252 VARYING. But we do accept an overflow infinity
2253 representation. */
2254 if (min == NULL_TREE
2255 || !is_gimple_min_invariant (min)
2256 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2257 || max == NULL_TREE
2258 || !is_gimple_min_invariant (max)
2259 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2261 set_value_range_to_varying (vr);
2262 return;
2265 /* We punt if:
2266 1) [-INF, +INF]
2267 2) [-INF, +-INF(OVF)]
2268 3) [+-INF(OVF), +INF]
2269 4) [+-INF(OVF), +-INF(OVF)]
2270 We learn nothing when we have INF and INF(OVF) on both sides.
2271 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2272 overflow. */
2273 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2274 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2276 set_value_range_to_varying (vr);
2277 return;
2280 cmp = compare_values (min, max);
2281 if (cmp == -2 || cmp == 1)
2283 /* If the new range has its limits swapped around (MIN > MAX),
2284 then the operation caused one of them to wrap around, mark
2285 the new range VARYING. */
2286 set_value_range_to_varying (vr);
2288 else
2289 set_value_range (vr, type, min, max, NULL);
2292 /* Extract range information from a binary operation CODE based on
2293 the ranges of each of its operands *VR0 and *VR1 with resulting
2294 type EXPR_TYPE. The resulting range is stored in *VR. */
2296 static void
2297 extract_range_from_binary_expr_1 (value_range_t *vr,
2298 enum tree_code code, tree expr_type,
2299 value_range_t *vr0_, value_range_t *vr1_)
2301 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2302 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2303 enum value_range_type type;
2304 tree min = NULL_TREE, max = NULL_TREE;
2305 int cmp;
2307 if (!INTEGRAL_TYPE_P (expr_type)
2308 && !POINTER_TYPE_P (expr_type))
2310 set_value_range_to_varying (vr);
2311 return;
2314 /* Not all binary expressions can be applied to ranges in a
2315 meaningful way. Handle only arithmetic operations. */
2316 if (code != PLUS_EXPR
2317 && code != MINUS_EXPR
2318 && code != POINTER_PLUS_EXPR
2319 && code != MULT_EXPR
2320 && code != TRUNC_DIV_EXPR
2321 && code != FLOOR_DIV_EXPR
2322 && code != CEIL_DIV_EXPR
2323 && code != EXACT_DIV_EXPR
2324 && code != ROUND_DIV_EXPR
2325 && code != TRUNC_MOD_EXPR
2326 && code != RSHIFT_EXPR
2327 && code != LSHIFT_EXPR
2328 && code != MIN_EXPR
2329 && code != MAX_EXPR
2330 && code != BIT_AND_EXPR
2331 && code != BIT_IOR_EXPR
2332 && code != BIT_XOR_EXPR)
2334 set_value_range_to_varying (vr);
2335 return;
2338 /* If both ranges are UNDEFINED, so is the result. */
2339 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2341 set_value_range_to_undefined (vr);
2342 return;
2344 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2345 code. At some point we may want to special-case operations that
2346 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2347 operand. */
2348 else if (vr0.type == VR_UNDEFINED)
2349 set_value_range_to_varying (&vr0);
2350 else if (vr1.type == VR_UNDEFINED)
2351 set_value_range_to_varying (&vr1);
2353 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2354 and express ~[] op X as ([]' op X) U ([]'' op X). */
2355 if (vr0.type == VR_ANTI_RANGE
2356 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2358 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2359 if (vrtem1.type != VR_UNDEFINED)
2361 value_range_t vrres = VR_INITIALIZER;
2362 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2363 &vrtem1, vr1_);
2364 vrp_meet (vr, &vrres);
2366 return;
2368 /* Likewise for X op ~[]. */
2369 if (vr1.type == VR_ANTI_RANGE
2370 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2372 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2373 if (vrtem1.type != VR_UNDEFINED)
2375 value_range_t vrres = VR_INITIALIZER;
2376 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2377 vr0_, &vrtem1);
2378 vrp_meet (vr, &vrres);
2380 return;
2383 /* The type of the resulting value range defaults to VR0.TYPE. */
2384 type = vr0.type;
2386 /* Refuse to operate on VARYING ranges, ranges of different kinds
2387 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2388 because we may be able to derive a useful range even if one of
2389 the operands is VR_VARYING or symbolic range. Similarly for
2390 divisions, MIN/MAX and PLUS/MINUS.
2392 TODO, we may be able to derive anti-ranges in some cases. */
2393 if (code != BIT_AND_EXPR
2394 && code != BIT_IOR_EXPR
2395 && code != TRUNC_DIV_EXPR
2396 && code != FLOOR_DIV_EXPR
2397 && code != CEIL_DIV_EXPR
2398 && code != EXACT_DIV_EXPR
2399 && code != ROUND_DIV_EXPR
2400 && code != TRUNC_MOD_EXPR
2401 && code != MIN_EXPR
2402 && code != MAX_EXPR
2403 && code != PLUS_EXPR
2404 && code != MINUS_EXPR
2405 && code != RSHIFT_EXPR
2406 && (vr0.type == VR_VARYING
2407 || vr1.type == VR_VARYING
2408 || vr0.type != vr1.type
2409 || symbolic_range_p (&vr0)
2410 || symbolic_range_p (&vr1)))
2412 set_value_range_to_varying (vr);
2413 return;
2416 /* Now evaluate the expression to determine the new range. */
2417 if (POINTER_TYPE_P (expr_type))
2419 if (code == MIN_EXPR || code == MAX_EXPR)
2421 /* For MIN/MAX expressions with pointers, we only care about
2422 nullness, if both are non null, then the result is nonnull.
2423 If both are null, then the result is null. Otherwise they
2424 are varying. */
2425 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2426 set_value_range_to_nonnull (vr, expr_type);
2427 else if (range_is_null (&vr0) && range_is_null (&vr1))
2428 set_value_range_to_null (vr, expr_type);
2429 else
2430 set_value_range_to_varying (vr);
2432 else if (code == POINTER_PLUS_EXPR)
2434 /* For pointer types, we are really only interested in asserting
2435 whether the expression evaluates to non-NULL. */
2436 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2437 set_value_range_to_nonnull (vr, expr_type);
2438 else if (range_is_null (&vr0) && range_is_null (&vr1))
2439 set_value_range_to_null (vr, expr_type);
2440 else
2441 set_value_range_to_varying (vr);
2443 else if (code == BIT_AND_EXPR)
2445 /* For pointer types, we are really only interested in asserting
2446 whether the expression evaluates to non-NULL. */
2447 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2448 set_value_range_to_nonnull (vr, expr_type);
2449 else if (range_is_null (&vr0) || range_is_null (&vr1))
2450 set_value_range_to_null (vr, expr_type);
2451 else
2452 set_value_range_to_varying (vr);
2454 else
2455 set_value_range_to_varying (vr);
2457 return;
2460 /* For integer ranges, apply the operation to each end of the
2461 range and see what we end up with. */
2462 if (code == PLUS_EXPR || code == MINUS_EXPR)
2464 const bool minus_p = (code == MINUS_EXPR);
2465 tree min_op0 = vr0.min;
2466 tree min_op1 = minus_p ? vr1.max : vr1.min;
2467 tree max_op0 = vr0.max;
2468 tree max_op1 = minus_p ? vr1.min : vr1.max;
2469 tree sym_min_op0 = NULL_TREE;
2470 tree sym_min_op1 = NULL_TREE;
2471 tree sym_max_op0 = NULL_TREE;
2472 tree sym_max_op1 = NULL_TREE;
2473 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2475 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2476 single-symbolic ranges, try to compute the precise resulting range,
2477 but only if we know that this resulting range will also be constant
2478 or single-symbolic. */
2479 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2480 && (TREE_CODE (min_op0) == INTEGER_CST
2481 || (sym_min_op0
2482 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2483 && (TREE_CODE (min_op1) == INTEGER_CST
2484 || (sym_min_op1
2485 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2486 && (!(sym_min_op0 && sym_min_op1)
2487 || (sym_min_op0 == sym_min_op1
2488 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2489 && (TREE_CODE (max_op0) == INTEGER_CST
2490 || (sym_max_op0
2491 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2492 && (TREE_CODE (max_op1) == INTEGER_CST
2493 || (sym_max_op1
2494 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2495 && (!(sym_max_op0 && sym_max_op1)
2496 || (sym_max_op0 == sym_max_op1
2497 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2499 const signop sgn = TYPE_SIGN (expr_type);
2500 const unsigned int prec = TYPE_PRECISION (expr_type);
2501 wide_int type_min, type_max, wmin, wmax;
2502 int min_ovf = 0;
2503 int max_ovf = 0;
2505 /* Get the lower and upper bounds of the type. */
2506 if (TYPE_OVERFLOW_WRAPS (expr_type))
2508 type_min = wi::min_value (prec, sgn);
2509 type_max = wi::max_value (prec, sgn);
2511 else
2513 type_min = vrp_val_min (expr_type);
2514 type_max = vrp_val_max (expr_type);
2517 /* Combine the lower bounds, if any. */
2518 if (min_op0 && min_op1)
2520 if (minus_p)
2522 wmin = wi::sub (min_op0, min_op1);
2524 /* Check for overflow. */
2525 if (wi::cmp (0, min_op1, sgn)
2526 != wi::cmp (wmin, min_op0, sgn))
2527 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2529 else
2531 wmin = wi::add (min_op0, min_op1);
2533 /* Check for overflow. */
2534 if (wi::cmp (min_op1, 0, sgn)
2535 != wi::cmp (wmin, min_op0, sgn))
2536 min_ovf = wi::cmp (min_op0, wmin, sgn);
2539 else if (min_op0)
2540 wmin = min_op0;
2541 else if (min_op1)
2542 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2543 else
2544 wmin = wi::shwi (0, prec);
2546 /* Combine the upper bounds, if any. */
2547 if (max_op0 && max_op1)
2549 if (minus_p)
2551 wmax = wi::sub (max_op0, max_op1);
2553 /* Check for overflow. */
2554 if (wi::cmp (0, max_op1, sgn)
2555 != wi::cmp (wmax, max_op0, sgn))
2556 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2558 else
2560 wmax = wi::add (max_op0, max_op1);
2562 if (wi::cmp (max_op1, 0, sgn)
2563 != wi::cmp (wmax, max_op0, sgn))
2564 max_ovf = wi::cmp (max_op0, wmax, sgn);
2567 else if (max_op0)
2568 wmax = max_op0;
2569 else if (max_op1)
2570 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2571 else
2572 wmax = wi::shwi (0, prec);
2574 /* Check for type overflow. */
2575 if (min_ovf == 0)
2577 if (wi::cmp (wmin, type_min, sgn) == -1)
2578 min_ovf = -1;
2579 else if (wi::cmp (wmin, type_max, sgn) == 1)
2580 min_ovf = 1;
2582 if (max_ovf == 0)
2584 if (wi::cmp (wmax, type_min, sgn) == -1)
2585 max_ovf = -1;
2586 else if (wi::cmp (wmax, type_max, sgn) == 1)
2587 max_ovf = 1;
2590 /* If we have overflow for the constant part and the resulting
2591 range will be symbolic, drop to VR_VARYING. */
2592 if ((min_ovf && sym_min_op0 != sym_min_op1)
2593 || (max_ovf && sym_max_op0 != sym_max_op1))
2595 set_value_range_to_varying (vr);
2596 return;
2599 if (TYPE_OVERFLOW_WRAPS (expr_type))
2601 /* If overflow wraps, truncate the values and adjust the
2602 range kind and bounds appropriately. */
2603 wide_int tmin = wide_int::from (wmin, prec, sgn);
2604 wide_int tmax = wide_int::from (wmax, prec, sgn);
2605 if (min_ovf == max_ovf)
2607 /* No overflow or both overflow or underflow. The
2608 range kind stays VR_RANGE. */
2609 min = wide_int_to_tree (expr_type, tmin);
2610 max = wide_int_to_tree (expr_type, tmax);
2612 else if (min_ovf == -1 && max_ovf == 1)
2614 /* Underflow and overflow, drop to VR_VARYING. */
2615 set_value_range_to_varying (vr);
2616 return;
2618 else
2620 /* Min underflow or max overflow. The range kind
2621 changes to VR_ANTI_RANGE. */
2622 bool covers = false;
2623 wide_int tem = tmin;
2624 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2625 || (max_ovf == 1 && min_ovf == 0));
2626 type = VR_ANTI_RANGE;
2627 tmin = tmax + 1;
2628 if (wi::cmp (tmin, tmax, sgn) < 0)
2629 covers = true;
2630 tmax = tem - 1;
2631 if (wi::cmp (tmax, tem, sgn) > 0)
2632 covers = true;
2633 /* If the anti-range would cover nothing, drop to varying.
2634 Likewise if the anti-range bounds are outside of the
2635 types values. */
2636 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2638 set_value_range_to_varying (vr);
2639 return;
2641 min = wide_int_to_tree (expr_type, tmin);
2642 max = wide_int_to_tree (expr_type, tmax);
2645 else
2647 /* If overflow does not wrap, saturate to the types min/max
2648 value. */
2649 if (min_ovf == -1)
2651 if (needs_overflow_infinity (expr_type)
2652 && supports_overflow_infinity (expr_type))
2653 min = negative_overflow_infinity (expr_type);
2654 else
2655 min = wide_int_to_tree (expr_type, type_min);
2657 else if (min_ovf == 1)
2659 if (needs_overflow_infinity (expr_type)
2660 && supports_overflow_infinity (expr_type))
2661 min = positive_overflow_infinity (expr_type);
2662 else
2663 min = wide_int_to_tree (expr_type, type_max);
2665 else
2666 min = wide_int_to_tree (expr_type, wmin);
2668 if (max_ovf == -1)
2670 if (needs_overflow_infinity (expr_type)
2671 && supports_overflow_infinity (expr_type))
2672 max = negative_overflow_infinity (expr_type);
2673 else
2674 max = wide_int_to_tree (expr_type, type_min);
2676 else if (max_ovf == 1)
2678 if (needs_overflow_infinity (expr_type)
2679 && supports_overflow_infinity (expr_type))
2680 max = positive_overflow_infinity (expr_type);
2681 else
2682 max = wide_int_to_tree (expr_type, type_max);
2684 else
2685 max = wide_int_to_tree (expr_type, wmax);
2688 if (needs_overflow_infinity (expr_type)
2689 && supports_overflow_infinity (expr_type))
2691 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2692 || (min_op1
2693 && (minus_p
2694 ? is_positive_overflow_infinity (min_op1)
2695 : is_negative_overflow_infinity (min_op1))))
2696 min = negative_overflow_infinity (expr_type);
2697 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2698 || (max_op1
2699 && (minus_p
2700 ? is_negative_overflow_infinity (max_op1)
2701 : is_positive_overflow_infinity (max_op1))))
2702 max = positive_overflow_infinity (expr_type);
2705 /* If the result lower bound is constant, we're done;
2706 otherwise, build the symbolic lower bound. */
2707 if (sym_min_op0 == sym_min_op1)
2709 else if (sym_min_op0)
2710 min = build_symbolic_expr (expr_type, sym_min_op0,
2711 neg_min_op0, min);
2712 else if (sym_min_op1)
2713 min = build_symbolic_expr (expr_type, sym_min_op1,
2714 neg_min_op1 ^ minus_p, min);
2716 /* Likewise for the upper bound. */
2717 if (sym_max_op0 == sym_max_op1)
2719 else if (sym_max_op0)
2720 max = build_symbolic_expr (expr_type, sym_max_op0,
2721 neg_max_op0, max);
2722 else if (sym_max_op1)
2723 max = build_symbolic_expr (expr_type, sym_max_op1,
2724 neg_max_op1 ^ minus_p, max);
2726 else
2728 /* For other cases, for example if we have a PLUS_EXPR with two
2729 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2730 to compute a precise range for such a case.
2731 ??? General even mixed range kind operations can be expressed
2732 by for example transforming ~[3, 5] + [1, 2] to range-only
2733 operations and a union primitive:
2734 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2735 [-INF+1, 4] U [6, +INF(OVF)]
2736 though usually the union is not exactly representable with
2737 a single range or anti-range as the above is
2738 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2739 but one could use a scheme similar to equivalences for this. */
2740 set_value_range_to_varying (vr);
2741 return;
2744 else if (code == MIN_EXPR
2745 || code == MAX_EXPR)
2747 if (vr0.type == VR_RANGE
2748 && !symbolic_range_p (&vr0))
2750 type = VR_RANGE;
2751 if (vr1.type == VR_RANGE
2752 && !symbolic_range_p (&vr1))
2754 /* For operations that make the resulting range directly
2755 proportional to the original ranges, apply the operation to
2756 the same end of each range. */
2757 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2758 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2760 else if (code == MIN_EXPR)
2762 min = vrp_val_min (expr_type);
2763 max = vr0.max;
2765 else if (code == MAX_EXPR)
2767 min = vr0.min;
2768 max = vrp_val_max (expr_type);
2771 else if (vr1.type == VR_RANGE
2772 && !symbolic_range_p (&vr1))
2774 type = VR_RANGE;
2775 if (code == MIN_EXPR)
2777 min = vrp_val_min (expr_type);
2778 max = vr1.max;
2780 else if (code == MAX_EXPR)
2782 min = vr1.min;
2783 max = vrp_val_max (expr_type);
2786 else
2788 set_value_range_to_varying (vr);
2789 return;
2792 else if (code == MULT_EXPR)
2794 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2795 drop to varying. This test requires 2*prec bits if both
2796 operands are signed and 2*prec + 2 bits if either is not. */
2798 signop sign = TYPE_SIGN (expr_type);
2799 unsigned int prec = TYPE_PRECISION (expr_type);
2801 if (range_int_cst_p (&vr0)
2802 && range_int_cst_p (&vr1)
2803 && TYPE_OVERFLOW_WRAPS (expr_type))
2805 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2806 typedef generic_wide_int
2807 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2808 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2809 vrp_int size = sizem1 + 1;
2811 /* Extend the values using the sign of the result to PREC2.
2812 From here on out, everthing is just signed math no matter
2813 what the input types were. */
2814 vrp_int min0 = vrp_int_cst (vr0.min);
2815 vrp_int max0 = vrp_int_cst (vr0.max);
2816 vrp_int min1 = vrp_int_cst (vr1.min);
2817 vrp_int max1 = vrp_int_cst (vr1.max);
2818 /* Canonicalize the intervals. */
2819 if (sign == UNSIGNED)
2821 if (wi::ltu_p (size, min0 + max0))
2823 min0 -= size;
2824 max0 -= size;
2827 if (wi::ltu_p (size, min1 + max1))
2829 min1 -= size;
2830 max1 -= size;
2834 vrp_int prod0 = min0 * min1;
2835 vrp_int prod1 = min0 * max1;
2836 vrp_int prod2 = max0 * min1;
2837 vrp_int prod3 = max0 * max1;
2839 /* Sort the 4 products so that min is in prod0 and max is in
2840 prod3. */
2841 /* min0min1 > max0max1 */
2842 if (wi::gts_p (prod0, prod3))
2843 std::swap (prod0, prod3);
2845 /* min0max1 > max0min1 */
2846 if (wi::gts_p (prod1, prod2))
2847 std::swap (prod1, prod2);
2849 if (wi::gts_p (prod0, prod1))
2850 std::swap (prod0, prod1);
2852 if (wi::gts_p (prod2, prod3))
2853 std::swap (prod2, prod3);
2855 /* diff = max - min. */
2856 prod2 = prod3 - prod0;
2857 if (wi::geu_p (prod2, sizem1))
2859 /* the range covers all values. */
2860 set_value_range_to_varying (vr);
2861 return;
2864 /* The following should handle the wrapping and selecting
2865 VR_ANTI_RANGE for us. */
2866 min = wide_int_to_tree (expr_type, prod0);
2867 max = wide_int_to_tree (expr_type, prod3);
2868 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2869 return;
2872 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2873 drop to VR_VARYING. It would take more effort to compute a
2874 precise range for such a case. For example, if we have
2875 op0 == 65536 and op1 == 65536 with their ranges both being
2876 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2877 we cannot claim that the product is in ~[0,0]. Note that we
2878 are guaranteed to have vr0.type == vr1.type at this
2879 point. */
2880 if (vr0.type == VR_ANTI_RANGE
2881 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2883 set_value_range_to_varying (vr);
2884 return;
2887 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2888 return;
2890 else if (code == RSHIFT_EXPR
2891 || code == LSHIFT_EXPR)
2893 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2894 then drop to VR_VARYING. Outside of this range we get undefined
2895 behavior from the shift operation. We cannot even trust
2896 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2897 shifts, and the operation at the tree level may be widened. */
2898 if (range_int_cst_p (&vr1)
2899 && compare_tree_int (vr1.min, 0) >= 0
2900 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2902 if (code == RSHIFT_EXPR)
2904 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2905 useful ranges just from the shift count. E.g.
2906 x >> 63 for signed 64-bit x is always [-1, 0]. */
2907 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2909 vr0.type = type = VR_RANGE;
2910 vr0.min = vrp_val_min (expr_type);
2911 vr0.max = vrp_val_max (expr_type);
2913 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2914 return;
2916 /* We can map lshifts by constants to MULT_EXPR handling. */
2917 else if (code == LSHIFT_EXPR
2918 && range_int_cst_singleton_p (&vr1))
2920 bool saved_flag_wrapv;
2921 value_range_t vr1p = VR_INITIALIZER;
2922 vr1p.type = VR_RANGE;
2923 vr1p.min = (wide_int_to_tree
2924 (expr_type,
2925 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2926 TYPE_PRECISION (expr_type))));
2927 vr1p.max = vr1p.min;
2928 /* We have to use a wrapping multiply though as signed overflow
2929 on lshifts is implementation defined in C89. */
2930 saved_flag_wrapv = flag_wrapv;
2931 flag_wrapv = 1;
2932 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2933 &vr0, &vr1p);
2934 flag_wrapv = saved_flag_wrapv;
2935 return;
2937 else if (code == LSHIFT_EXPR
2938 && range_int_cst_p (&vr0))
2940 int prec = TYPE_PRECISION (expr_type);
2941 int overflow_pos = prec;
2942 int bound_shift;
2943 wide_int low_bound, high_bound;
2944 bool uns = TYPE_UNSIGNED (expr_type);
2945 bool in_bounds = false;
2947 if (!uns)
2948 overflow_pos -= 1;
2950 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2951 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2952 overflow. However, for that to happen, vr1.max needs to be
2953 zero, which means vr1 is a singleton range of zero, which
2954 means it should be handled by the previous LSHIFT_EXPR
2955 if-clause. */
2956 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2957 wide_int complement = ~(bound - 1);
2959 if (uns)
2961 low_bound = bound;
2962 high_bound = complement;
2963 if (wi::ltu_p (vr0.max, low_bound))
2965 /* [5, 6] << [1, 2] == [10, 24]. */
2966 /* We're shifting out only zeroes, the value increases
2967 monotonically. */
2968 in_bounds = true;
2970 else if (wi::ltu_p (high_bound, vr0.min))
2972 /* [0xffffff00, 0xffffffff] << [1, 2]
2973 == [0xfffffc00, 0xfffffffe]. */
2974 /* We're shifting out only ones, the value decreases
2975 monotonically. */
2976 in_bounds = true;
2979 else
2981 /* [-1, 1] << [1, 2] == [-4, 4]. */
2982 low_bound = complement;
2983 high_bound = bound;
2984 if (wi::lts_p (vr0.max, high_bound)
2985 && wi::lts_p (low_bound, vr0.min))
2987 /* For non-negative numbers, we're shifting out only
2988 zeroes, the value increases monotonically.
2989 For negative numbers, we're shifting out only ones, the
2990 value decreases monotomically. */
2991 in_bounds = true;
2995 if (in_bounds)
2997 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2998 return;
3002 set_value_range_to_varying (vr);
3003 return;
3005 else if (code == TRUNC_DIV_EXPR
3006 || code == FLOOR_DIV_EXPR
3007 || code == CEIL_DIV_EXPR
3008 || code == EXACT_DIV_EXPR
3009 || code == ROUND_DIV_EXPR)
3011 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3013 /* For division, if op1 has VR_RANGE but op0 does not, something
3014 can be deduced just from that range. Say [min, max] / [4, max]
3015 gives [min / 4, max / 4] range. */
3016 if (vr1.type == VR_RANGE
3017 && !symbolic_range_p (&vr1)
3018 && range_includes_zero_p (vr1.min, vr1.max) == 0)
3020 vr0.type = type = VR_RANGE;
3021 vr0.min = vrp_val_min (expr_type);
3022 vr0.max = vrp_val_max (expr_type);
3024 else
3026 set_value_range_to_varying (vr);
3027 return;
3031 /* For divisions, if flag_non_call_exceptions is true, we must
3032 not eliminate a division by zero. */
3033 if (cfun->can_throw_non_call_exceptions
3034 && (vr1.type != VR_RANGE
3035 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3037 set_value_range_to_varying (vr);
3038 return;
3041 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3042 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3043 include 0. */
3044 if (vr0.type == VR_RANGE
3045 && (vr1.type != VR_RANGE
3046 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3048 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
3049 int cmp;
3051 min = NULL_TREE;
3052 max = NULL_TREE;
3053 if (TYPE_UNSIGNED (expr_type)
3054 || value_range_nonnegative_p (&vr1))
3056 /* For unsigned division or when divisor is known
3057 to be non-negative, the range has to cover
3058 all numbers from 0 to max for positive max
3059 and all numbers from min to 0 for negative min. */
3060 cmp = compare_values (vr0.max, zero);
3061 if (cmp == -1)
3063 /* When vr0.max < 0, vr1.min != 0 and value
3064 ranges for dividend and divisor are available. */
3065 if (vr1.type == VR_RANGE
3066 && !symbolic_range_p (&vr0)
3067 && !symbolic_range_p (&vr1)
3068 && !compare_values (vr1.min, zero))
3069 max = int_const_binop (code, vr0.max, vr1.min);
3070 else
3071 max = zero;
3073 else if (cmp == 0 || cmp == 1)
3074 max = vr0.max;
3075 else
3076 type = VR_VARYING;
3077 cmp = compare_values (vr0.min, zero);
3078 if (cmp == 1)
3080 /* For unsigned division when value ranges for dividend
3081 and divisor are available. */
3082 if (vr1.type == VR_RANGE
3083 && !symbolic_range_p (&vr0)
3084 && !symbolic_range_p (&vr1))
3085 min = int_const_binop (code, vr0.min, vr1.max);
3086 else
3087 min = zero;
3089 else if (cmp == 0 || cmp == -1)
3090 min = vr0.min;
3091 else
3092 type = VR_VARYING;
3094 else
3096 /* Otherwise the range is -max .. max or min .. -min
3097 depending on which bound is bigger in absolute value,
3098 as the division can change the sign. */
3099 abs_extent_range (vr, vr0.min, vr0.max);
3100 return;
3102 if (type == VR_VARYING)
3104 set_value_range_to_varying (vr);
3105 return;
3108 else
3110 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3111 return;
3114 else if (code == TRUNC_MOD_EXPR)
3116 if (range_is_null (&vr1))
3118 set_value_range_to_undefined (vr);
3119 return;
3121 /* ABS (A % B) < ABS (B) and either
3122 0 <= A % B <= A or A <= A % B <= 0. */
3123 type = VR_RANGE;
3124 signop sgn = TYPE_SIGN (expr_type);
3125 unsigned int prec = TYPE_PRECISION (expr_type);
3126 wide_int wmin, wmax, tmp;
3127 wide_int zero = wi::zero (prec);
3128 wide_int one = wi::one (prec);
3129 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
3131 wmax = wi::sub (vr1.max, one);
3132 if (sgn == SIGNED)
3134 tmp = wi::sub (wi::minus_one (prec), vr1.min);
3135 wmax = wi::smax (wmax, tmp);
3138 else
3140 wmax = wi::max_value (prec, sgn);
3141 /* X % INT_MIN may be INT_MAX. */
3142 if (sgn == UNSIGNED)
3143 wmax = wmax - one;
3146 if (sgn == UNSIGNED)
3147 wmin = zero;
3148 else
3150 wmin = -wmax;
3151 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3153 tmp = vr0.min;
3154 if (wi::gts_p (tmp, zero))
3155 tmp = zero;
3156 wmin = wi::smax (wmin, tmp);
3160 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3162 tmp = vr0.max;
3163 if (sgn == SIGNED && wi::neg_p (tmp))
3164 tmp = zero;
3165 wmax = wi::min (wmax, tmp, sgn);
3168 min = wide_int_to_tree (expr_type, wmin);
3169 max = wide_int_to_tree (expr_type, wmax);
3171 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3173 bool int_cst_range0, int_cst_range1;
3174 wide_int may_be_nonzero0, may_be_nonzero1;
3175 wide_int must_be_nonzero0, must_be_nonzero1;
3177 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3178 &may_be_nonzero0,
3179 &must_be_nonzero0);
3180 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3181 &may_be_nonzero1,
3182 &must_be_nonzero1);
3184 type = VR_RANGE;
3185 if (code == BIT_AND_EXPR)
3187 min = wide_int_to_tree (expr_type,
3188 must_be_nonzero0 & must_be_nonzero1);
3189 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3190 /* If both input ranges contain only negative values we can
3191 truncate the result range maximum to the minimum of the
3192 input range maxima. */
3193 if (int_cst_range0 && int_cst_range1
3194 && tree_int_cst_sgn (vr0.max) < 0
3195 && tree_int_cst_sgn (vr1.max) < 0)
3197 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3198 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3200 /* If either input range contains only non-negative values
3201 we can truncate the result range maximum to the respective
3202 maximum of the input range. */
3203 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3204 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3205 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3206 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3207 max = wide_int_to_tree (expr_type, wmax);
3209 else if (code == BIT_IOR_EXPR)
3211 max = wide_int_to_tree (expr_type,
3212 may_be_nonzero0 | may_be_nonzero1);
3213 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3214 /* If the input ranges contain only positive values we can
3215 truncate the minimum of the result range to the maximum
3216 of the input range minima. */
3217 if (int_cst_range0 && int_cst_range1
3218 && tree_int_cst_sgn (vr0.min) >= 0
3219 && tree_int_cst_sgn (vr1.min) >= 0)
3221 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3222 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3224 /* If either input range contains only negative values
3225 we can truncate the minimum of the result range to the
3226 respective minimum range. */
3227 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3228 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3229 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3230 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3231 min = wide_int_to_tree (expr_type, wmin);
3233 else if (code == BIT_XOR_EXPR)
3235 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3236 | ~(may_be_nonzero0 | may_be_nonzero1));
3237 wide_int result_one_bits
3238 = (must_be_nonzero0.and_not (may_be_nonzero1)
3239 | must_be_nonzero1.and_not (may_be_nonzero0));
3240 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3241 min = wide_int_to_tree (expr_type, result_one_bits);
3242 /* If the range has all positive or all negative values the
3243 result is better than VARYING. */
3244 if (tree_int_cst_sgn (min) < 0
3245 || tree_int_cst_sgn (max) >= 0)
3247 else
3248 max = min = NULL_TREE;
3251 else
3252 gcc_unreachable ();
3254 /* If either MIN or MAX overflowed, then set the resulting range to
3255 VARYING. But we do accept an overflow infinity representation. */
3256 if (min == NULL_TREE
3257 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3258 || max == NULL_TREE
3259 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3261 set_value_range_to_varying (vr);
3262 return;
3265 /* We punt if:
3266 1) [-INF, +INF]
3267 2) [-INF, +-INF(OVF)]
3268 3) [+-INF(OVF), +INF]
3269 4) [+-INF(OVF), +-INF(OVF)]
3270 We learn nothing when we have INF and INF(OVF) on both sides.
3271 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3272 overflow. */
3273 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3274 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3276 set_value_range_to_varying (vr);
3277 return;
3280 cmp = compare_values (min, max);
3281 if (cmp == -2 || cmp == 1)
3283 /* If the new range has its limits swapped around (MIN > MAX),
3284 then the operation caused one of them to wrap around, mark
3285 the new range VARYING. */
3286 set_value_range_to_varying (vr);
3288 else
3289 set_value_range (vr, type, min, max, NULL);
3292 /* Extract range information from a binary expression OP0 CODE OP1 based on
3293 the ranges of each of its operands with resulting type EXPR_TYPE.
3294 The resulting range is stored in *VR. */
3296 static void
3297 extract_range_from_binary_expr (value_range_t *vr,
3298 enum tree_code code,
3299 tree expr_type, tree op0, tree op1)
3301 value_range_t vr0 = VR_INITIALIZER;
3302 value_range_t vr1 = VR_INITIALIZER;
3304 /* Get value ranges for each operand. For constant operands, create
3305 a new value range with the operand to simplify processing. */
3306 if (TREE_CODE (op0) == SSA_NAME)
3307 vr0 = *(get_value_range (op0));
3308 else if (is_gimple_min_invariant (op0))
3309 set_value_range_to_value (&vr0, op0, NULL);
3310 else
3311 set_value_range_to_varying (&vr0);
3313 if (TREE_CODE (op1) == SSA_NAME)
3314 vr1 = *(get_value_range (op1));
3315 else if (is_gimple_min_invariant (op1))
3316 set_value_range_to_value (&vr1, op1, NULL);
3317 else
3318 set_value_range_to_varying (&vr1);
3320 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3322 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3323 and based on the other operand, for example if it was deduced from a
3324 symbolic comparison. When a bound of the range of the first operand
3325 is invariant, we set the corresponding bound of the new range to INF
3326 in order to avoid recursing on the range of the second operand. */
3327 if (vr->type == VR_VARYING
3328 && (code == PLUS_EXPR || code == MINUS_EXPR)
3329 && TREE_CODE (op1) == SSA_NAME
3330 && vr0.type == VR_RANGE
3331 && symbolic_range_based_on_p (&vr0, op1))
3333 const bool minus_p = (code == MINUS_EXPR);
3334 value_range_t n_vr1 = VR_INITIALIZER;
3336 /* Try with VR0 and [-INF, OP1]. */
3337 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3338 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3340 /* Try with VR0 and [OP1, +INF]. */
3341 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3342 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3344 /* Try with VR0 and [OP1, OP1]. */
3345 else
3346 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3348 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3351 if (vr->type == VR_VARYING
3352 && (code == PLUS_EXPR || code == MINUS_EXPR)
3353 && TREE_CODE (op0) == SSA_NAME
3354 && vr1.type == VR_RANGE
3355 && symbolic_range_based_on_p (&vr1, op0))
3357 const bool minus_p = (code == MINUS_EXPR);
3358 value_range_t n_vr0 = VR_INITIALIZER;
3360 /* Try with [-INF, OP0] and VR1. */
3361 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3362 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3364 /* Try with [OP0, +INF] and VR1. */
3365 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3366 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3368 /* Try with [OP0, OP0] and VR1. */
3369 else
3370 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3372 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3376 /* Extract range information from a unary operation CODE based on
3377 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3378 The resulting range is stored in *VR. */
3380 static void
3381 extract_range_from_unary_expr_1 (value_range_t *vr,
3382 enum tree_code code, tree type,
3383 value_range_t *vr0_, tree op0_type)
3385 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3387 /* VRP only operates on integral and pointer types. */
3388 if (!(INTEGRAL_TYPE_P (op0_type)
3389 || POINTER_TYPE_P (op0_type))
3390 || !(INTEGRAL_TYPE_P (type)
3391 || POINTER_TYPE_P (type)))
3393 set_value_range_to_varying (vr);
3394 return;
3397 /* If VR0 is UNDEFINED, so is the result. */
3398 if (vr0.type == VR_UNDEFINED)
3400 set_value_range_to_undefined (vr);
3401 return;
3404 /* Handle operations that we express in terms of others. */
3405 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3407 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3408 copy_value_range (vr, &vr0);
3409 return;
3411 else if (code == NEGATE_EXPR)
3413 /* -X is simply 0 - X, so re-use existing code that also handles
3414 anti-ranges fine. */
3415 value_range_t zero = VR_INITIALIZER;
3416 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3417 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3418 return;
3420 else if (code == BIT_NOT_EXPR)
3422 /* ~X is simply -1 - X, so re-use existing code that also handles
3423 anti-ranges fine. */
3424 value_range_t minusone = VR_INITIALIZER;
3425 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3426 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3427 type, &minusone, &vr0);
3428 return;
3431 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3432 and express op ~[] as (op []') U (op []''). */
3433 if (vr0.type == VR_ANTI_RANGE
3434 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3436 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3437 if (vrtem1.type != VR_UNDEFINED)
3439 value_range_t vrres = VR_INITIALIZER;
3440 extract_range_from_unary_expr_1 (&vrres, code, type,
3441 &vrtem1, op0_type);
3442 vrp_meet (vr, &vrres);
3444 return;
3447 if (CONVERT_EXPR_CODE_P (code))
3449 tree inner_type = op0_type;
3450 tree outer_type = type;
3452 /* If the expression evaluates to a pointer, we are only interested in
3453 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3454 if (POINTER_TYPE_P (type))
3456 if (range_is_nonnull (&vr0))
3457 set_value_range_to_nonnull (vr, type);
3458 else if (range_is_null (&vr0))
3459 set_value_range_to_null (vr, type);
3460 else
3461 set_value_range_to_varying (vr);
3462 return;
3465 /* If VR0 is varying and we increase the type precision, assume
3466 a full range for the following transformation. */
3467 if (vr0.type == VR_VARYING
3468 && INTEGRAL_TYPE_P (inner_type)
3469 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3471 vr0.type = VR_RANGE;
3472 vr0.min = TYPE_MIN_VALUE (inner_type);
3473 vr0.max = TYPE_MAX_VALUE (inner_type);
3476 /* If VR0 is a constant range or anti-range and the conversion is
3477 not truncating we can convert the min and max values and
3478 canonicalize the resulting range. Otherwise we can do the
3479 conversion if the size of the range is less than what the
3480 precision of the target type can represent and the range is
3481 not an anti-range. */
3482 if ((vr0.type == VR_RANGE
3483 || vr0.type == VR_ANTI_RANGE)
3484 && TREE_CODE (vr0.min) == INTEGER_CST
3485 && TREE_CODE (vr0.max) == INTEGER_CST
3486 && (!is_overflow_infinity (vr0.min)
3487 || (vr0.type == VR_RANGE
3488 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3489 && needs_overflow_infinity (outer_type)
3490 && supports_overflow_infinity (outer_type)))
3491 && (!is_overflow_infinity (vr0.max)
3492 || (vr0.type == VR_RANGE
3493 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3494 && needs_overflow_infinity (outer_type)
3495 && supports_overflow_infinity (outer_type)))
3496 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3497 || (vr0.type == VR_RANGE
3498 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3499 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3500 size_int (TYPE_PRECISION (outer_type)))))))
3502 tree new_min, new_max;
3503 if (is_overflow_infinity (vr0.min))
3504 new_min = negative_overflow_infinity (outer_type);
3505 else
3506 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3507 0, false);
3508 if (is_overflow_infinity (vr0.max))
3509 new_max = positive_overflow_infinity (outer_type);
3510 else
3511 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3512 0, false);
3513 set_and_canonicalize_value_range (vr, vr0.type,
3514 new_min, new_max, NULL);
3515 return;
3518 set_value_range_to_varying (vr);
3519 return;
3521 else if (code == ABS_EXPR)
3523 tree min, max;
3524 int cmp;
3526 /* Pass through vr0 in the easy cases. */
3527 if (TYPE_UNSIGNED (type)
3528 || value_range_nonnegative_p (&vr0))
3530 copy_value_range (vr, &vr0);
3531 return;
3534 /* For the remaining varying or symbolic ranges we can't do anything
3535 useful. */
3536 if (vr0.type == VR_VARYING
3537 || symbolic_range_p (&vr0))
3539 set_value_range_to_varying (vr);
3540 return;
3543 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3544 useful range. */
3545 if (!TYPE_OVERFLOW_UNDEFINED (type)
3546 && ((vr0.type == VR_RANGE
3547 && vrp_val_is_min (vr0.min))
3548 || (vr0.type == VR_ANTI_RANGE
3549 && !vrp_val_is_min (vr0.min))))
3551 set_value_range_to_varying (vr);
3552 return;
3555 /* ABS_EXPR may flip the range around, if the original range
3556 included negative values. */
3557 if (is_overflow_infinity (vr0.min))
3558 min = positive_overflow_infinity (type);
3559 else if (!vrp_val_is_min (vr0.min))
3560 min = fold_unary_to_constant (code, type, vr0.min);
3561 else if (!needs_overflow_infinity (type))
3562 min = TYPE_MAX_VALUE (type);
3563 else if (supports_overflow_infinity (type))
3564 min = positive_overflow_infinity (type);
3565 else
3567 set_value_range_to_varying (vr);
3568 return;
3571 if (is_overflow_infinity (vr0.max))
3572 max = positive_overflow_infinity (type);
3573 else if (!vrp_val_is_min (vr0.max))
3574 max = fold_unary_to_constant (code, type, vr0.max);
3575 else if (!needs_overflow_infinity (type))
3576 max = TYPE_MAX_VALUE (type);
3577 else if (supports_overflow_infinity (type)
3578 /* We shouldn't generate [+INF, +INF] as set_value_range
3579 doesn't like this and ICEs. */
3580 && !is_positive_overflow_infinity (min))
3581 max = positive_overflow_infinity (type);
3582 else
3584 set_value_range_to_varying (vr);
3585 return;
3588 cmp = compare_values (min, max);
3590 /* If a VR_ANTI_RANGEs contains zero, then we have
3591 ~[-INF, min(MIN, MAX)]. */
3592 if (vr0.type == VR_ANTI_RANGE)
3594 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3596 /* Take the lower of the two values. */
3597 if (cmp != 1)
3598 max = min;
3600 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3601 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3602 flag_wrapv is set and the original anti-range doesn't include
3603 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3604 if (TYPE_OVERFLOW_WRAPS (type))
3606 tree type_min_value = TYPE_MIN_VALUE (type);
3608 min = (vr0.min != type_min_value
3609 ? int_const_binop (PLUS_EXPR, type_min_value,
3610 build_int_cst (TREE_TYPE (type_min_value), 1))
3611 : type_min_value);
3613 else
3615 if (overflow_infinity_range_p (&vr0))
3616 min = negative_overflow_infinity (type);
3617 else
3618 min = TYPE_MIN_VALUE (type);
3621 else
3623 /* All else has failed, so create the range [0, INF], even for
3624 flag_wrapv since TYPE_MIN_VALUE is in the original
3625 anti-range. */
3626 vr0.type = VR_RANGE;
3627 min = build_int_cst (type, 0);
3628 if (needs_overflow_infinity (type))
3630 if (supports_overflow_infinity (type))
3631 max = positive_overflow_infinity (type);
3632 else
3634 set_value_range_to_varying (vr);
3635 return;
3638 else
3639 max = TYPE_MAX_VALUE (type);
3643 /* If the range contains zero then we know that the minimum value in the
3644 range will be zero. */
3645 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3647 if (cmp == 1)
3648 max = min;
3649 min = build_int_cst (type, 0);
3651 else
3653 /* If the range was reversed, swap MIN and MAX. */
3654 if (cmp == 1)
3655 std::swap (min, max);
3658 cmp = compare_values (min, max);
3659 if (cmp == -2 || cmp == 1)
3661 /* If the new range has its limits swapped around (MIN > MAX),
3662 then the operation caused one of them to wrap around, mark
3663 the new range VARYING. */
3664 set_value_range_to_varying (vr);
3666 else
3667 set_value_range (vr, vr0.type, min, max, NULL);
3668 return;
3671 /* For unhandled operations fall back to varying. */
3672 set_value_range_to_varying (vr);
3673 return;
3677 /* Extract range information from a unary expression CODE OP0 based on
3678 the range of its operand with resulting type TYPE.
3679 The resulting range is stored in *VR. */
3681 static void
3682 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3683 tree type, tree op0)
3685 value_range_t vr0 = VR_INITIALIZER;
3687 /* Get value ranges for the operand. For constant operands, create
3688 a new value range with the operand to simplify processing. */
3689 if (TREE_CODE (op0) == SSA_NAME)
3690 vr0 = *(get_value_range (op0));
3691 else if (is_gimple_min_invariant (op0))
3692 set_value_range_to_value (&vr0, op0, NULL);
3693 else
3694 set_value_range_to_varying (&vr0);
3696 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3700 /* Extract range information from a conditional expression STMT based on
3701 the ranges of each of its operands and the expression code. */
3703 static void
3704 extract_range_from_cond_expr (value_range_t *vr, gassign *stmt)
3706 tree op0, op1;
3707 value_range_t vr0 = VR_INITIALIZER;
3708 value_range_t vr1 = VR_INITIALIZER;
3710 /* Get value ranges for each operand. For constant operands, create
3711 a new value range with the operand to simplify processing. */
3712 op0 = gimple_assign_rhs2 (stmt);
3713 if (TREE_CODE (op0) == SSA_NAME)
3714 vr0 = *(get_value_range (op0));
3715 else if (is_gimple_min_invariant (op0))
3716 set_value_range_to_value (&vr0, op0, NULL);
3717 else
3718 set_value_range_to_varying (&vr0);
3720 op1 = gimple_assign_rhs3 (stmt);
3721 if (TREE_CODE (op1) == SSA_NAME)
3722 vr1 = *(get_value_range (op1));
3723 else if (is_gimple_min_invariant (op1))
3724 set_value_range_to_value (&vr1, op1, NULL);
3725 else
3726 set_value_range_to_varying (&vr1);
3728 /* The resulting value range is the union of the operand ranges */
3729 copy_value_range (vr, &vr0);
3730 vrp_meet (vr, &vr1);
3734 /* Extract range information from a comparison expression EXPR based
3735 on the range of its operand and the expression code. */
3737 static void
3738 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3739 tree type, tree op0, tree op1)
3741 bool sop = false;
3742 tree val;
3744 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3745 NULL);
3747 /* A disadvantage of using a special infinity as an overflow
3748 representation is that we lose the ability to record overflow
3749 when we don't have an infinity. So we have to ignore a result
3750 which relies on overflow. */
3752 if (val && !is_overflow_infinity (val) && !sop)
3754 /* Since this expression was found on the RHS of an assignment,
3755 its type may be different from _Bool. Convert VAL to EXPR's
3756 type. */
3757 val = fold_convert (type, val);
3758 if (is_gimple_min_invariant (val))
3759 set_value_range_to_value (vr, val, vr->equiv);
3760 else
3761 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3763 else
3764 /* The result of a comparison is always true or false. */
3765 set_value_range_to_truthvalue (vr, type);
3768 /* Helper function for simplify_internal_call_using_ranges and
3769 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3770 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3771 always overflow. Set *OVF to true if it is known to always
3772 overflow. */
3774 static bool
3775 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3776 tree op0, tree op1, bool *ovf)
3778 value_range_t vr0 = VR_INITIALIZER;
3779 value_range_t vr1 = VR_INITIALIZER;
3780 if (TREE_CODE (op0) == SSA_NAME)
3781 vr0 = *get_value_range (op0);
3782 else if (TREE_CODE (op0) == INTEGER_CST)
3783 set_value_range_to_value (&vr0, op0, NULL);
3784 else
3785 set_value_range_to_varying (&vr0);
3787 if (TREE_CODE (op1) == SSA_NAME)
3788 vr1 = *get_value_range (op1);
3789 else if (TREE_CODE (op1) == INTEGER_CST)
3790 set_value_range_to_value (&vr1, op1, NULL);
3791 else
3792 set_value_range_to_varying (&vr1);
3794 if (!range_int_cst_p (&vr0)
3795 || TREE_OVERFLOW (vr0.min)
3796 || TREE_OVERFLOW (vr0.max))
3798 vr0.min = vrp_val_min (TREE_TYPE (op0));
3799 vr0.max = vrp_val_max (TREE_TYPE (op0));
3801 if (!range_int_cst_p (&vr1)
3802 || TREE_OVERFLOW (vr1.min)
3803 || TREE_OVERFLOW (vr1.max))
3805 vr1.min = vrp_val_min (TREE_TYPE (op1));
3806 vr1.max = vrp_val_max (TREE_TYPE (op1));
3808 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3809 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3810 if (arith_overflowed_p (subcode, type, vr0.max,
3811 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3812 return false;
3813 if (subcode == MULT_EXPR)
3815 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3816 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3817 return false;
3819 if (*ovf)
3821 /* So far we found that there is an overflow on the boundaries.
3822 That doesn't prove that there is an overflow even for all values
3823 in between the boundaries. For that compute widest_int range
3824 of the result and see if it doesn't overlap the range of
3825 type. */
3826 widest_int wmin, wmax;
3827 widest_int w[4];
3828 int i;
3829 w[0] = wi::to_widest (vr0.min);
3830 w[1] = wi::to_widest (vr0.max);
3831 w[2] = wi::to_widest (vr1.min);
3832 w[3] = wi::to_widest (vr1.max);
3833 for (i = 0; i < 4; i++)
3835 widest_int wt;
3836 switch (subcode)
3838 case PLUS_EXPR:
3839 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3840 break;
3841 case MINUS_EXPR:
3842 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3843 break;
3844 case MULT_EXPR:
3845 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3846 break;
3847 default:
3848 gcc_unreachable ();
3850 if (i == 0)
3852 wmin = wt;
3853 wmax = wt;
3855 else
3857 wmin = wi::smin (wmin, wt);
3858 wmax = wi::smax (wmax, wt);
3861 /* The result of op0 CODE op1 is known to be in range
3862 [wmin, wmax]. */
3863 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3864 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3865 /* If all values in [wmin, wmax] are smaller than
3866 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3867 the arithmetic operation will always overflow. */
3868 if (wi::lts_p (wmax, wtmin) || wi::gts_p (wmin, wtmax))
3869 return true;
3870 return false;
3872 return true;
3875 /* Try to derive a nonnegative or nonzero range out of STMT relying
3876 primarily on generic routines in fold in conjunction with range data.
3877 Store the result in *VR */
3879 static void
3880 extract_range_basic (value_range_t *vr, gimple stmt)
3882 bool sop = false;
3883 tree type = gimple_expr_type (stmt);
3885 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3887 tree fndecl = gimple_call_fndecl (stmt), arg;
3888 int mini, maxi, zerov = 0, prec;
3890 switch (DECL_FUNCTION_CODE (fndecl))
3892 case BUILT_IN_CONSTANT_P:
3893 /* If the call is __builtin_constant_p and the argument is a
3894 function parameter resolve it to false. This avoids bogus
3895 array bound warnings.
3896 ??? We could do this as early as inlining is finished. */
3897 arg = gimple_call_arg (stmt, 0);
3898 if (TREE_CODE (arg) == SSA_NAME
3899 && SSA_NAME_IS_DEFAULT_DEF (arg)
3900 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3902 set_value_range_to_null (vr, type);
3903 return;
3905 break;
3906 /* Both __builtin_ffs* and __builtin_popcount return
3907 [0, prec]. */
3908 CASE_INT_FN (BUILT_IN_FFS):
3909 CASE_INT_FN (BUILT_IN_POPCOUNT):
3910 arg = gimple_call_arg (stmt, 0);
3911 prec = TYPE_PRECISION (TREE_TYPE (arg));
3912 mini = 0;
3913 maxi = prec;
3914 if (TREE_CODE (arg) == SSA_NAME)
3916 value_range_t *vr0 = get_value_range (arg);
3917 /* If arg is non-zero, then ffs or popcount
3918 are non-zero. */
3919 if (((vr0->type == VR_RANGE
3920 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3921 || (vr0->type == VR_ANTI_RANGE
3922 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3923 && !is_overflow_infinity (vr0->min)
3924 && !is_overflow_infinity (vr0->max))
3925 mini = 1;
3926 /* If some high bits are known to be zero,
3927 we can decrease the maximum. */
3928 if (vr0->type == VR_RANGE
3929 && TREE_CODE (vr0->max) == INTEGER_CST
3930 && !operand_less_p (vr0->min,
3931 build_zero_cst (TREE_TYPE (vr0->min)))
3932 && !is_overflow_infinity (vr0->max))
3933 maxi = tree_floor_log2 (vr0->max) + 1;
3935 goto bitop_builtin;
3936 /* __builtin_parity* returns [0, 1]. */
3937 CASE_INT_FN (BUILT_IN_PARITY):
3938 mini = 0;
3939 maxi = 1;
3940 goto bitop_builtin;
3941 /* __builtin_c[lt]z* return [0, prec-1], except for
3942 when the argument is 0, but that is undefined behavior.
3943 On many targets where the CLZ RTL or optab value is defined
3944 for 0 the value is prec, so include that in the range
3945 by default. */
3946 CASE_INT_FN (BUILT_IN_CLZ):
3947 arg = gimple_call_arg (stmt, 0);
3948 prec = TYPE_PRECISION (TREE_TYPE (arg));
3949 mini = 0;
3950 maxi = prec;
3951 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3952 != CODE_FOR_nothing
3953 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3954 zerov)
3955 /* Handle only the single common value. */
3956 && zerov != prec)
3957 /* Magic value to give up, unless vr0 proves
3958 arg is non-zero. */
3959 mini = -2;
3960 if (TREE_CODE (arg) == SSA_NAME)
3962 value_range_t *vr0 = get_value_range (arg);
3963 /* From clz of VR_RANGE minimum we can compute
3964 result maximum. */
3965 if (vr0->type == VR_RANGE
3966 && TREE_CODE (vr0->min) == INTEGER_CST
3967 && !is_overflow_infinity (vr0->min))
3969 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3970 if (maxi != prec)
3971 mini = 0;
3973 else if (vr0->type == VR_ANTI_RANGE
3974 && integer_zerop (vr0->min)
3975 && !is_overflow_infinity (vr0->min))
3977 maxi = prec - 1;
3978 mini = 0;
3980 if (mini == -2)
3981 break;
3982 /* From clz of VR_RANGE maximum we can compute
3983 result minimum. */
3984 if (vr0->type == VR_RANGE
3985 && TREE_CODE (vr0->max) == INTEGER_CST
3986 && !is_overflow_infinity (vr0->max))
3988 mini = prec - 1 - tree_floor_log2 (vr0->max);
3989 if (mini == prec)
3990 break;
3993 if (mini == -2)
3994 break;
3995 goto bitop_builtin;
3996 /* __builtin_ctz* return [0, prec-1], except for
3997 when the argument is 0, but that is undefined behavior.
3998 If there is a ctz optab for this mode and
3999 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
4000 otherwise just assume 0 won't be seen. */
4001 CASE_INT_FN (BUILT_IN_CTZ):
4002 arg = gimple_call_arg (stmt, 0);
4003 prec = TYPE_PRECISION (TREE_TYPE (arg));
4004 mini = 0;
4005 maxi = prec - 1;
4006 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
4007 != CODE_FOR_nothing
4008 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
4009 zerov))
4011 /* Handle only the two common values. */
4012 if (zerov == -1)
4013 mini = -1;
4014 else if (zerov == prec)
4015 maxi = prec;
4016 else
4017 /* Magic value to give up, unless vr0 proves
4018 arg is non-zero. */
4019 mini = -2;
4021 if (TREE_CODE (arg) == SSA_NAME)
4023 value_range_t *vr0 = get_value_range (arg);
4024 /* If arg is non-zero, then use [0, prec - 1]. */
4025 if (((vr0->type == VR_RANGE
4026 && integer_nonzerop (vr0->min))
4027 || (vr0->type == VR_ANTI_RANGE
4028 && integer_zerop (vr0->min)))
4029 && !is_overflow_infinity (vr0->min))
4031 mini = 0;
4032 maxi = prec - 1;
4034 /* If some high bits are known to be zero,
4035 we can decrease the result maximum. */
4036 if (vr0->type == VR_RANGE
4037 && TREE_CODE (vr0->max) == INTEGER_CST
4038 && !is_overflow_infinity (vr0->max))
4040 maxi = tree_floor_log2 (vr0->max);
4041 /* For vr0 [0, 0] give up. */
4042 if (maxi == -1)
4043 break;
4046 if (mini == -2)
4047 break;
4048 goto bitop_builtin;
4049 /* __builtin_clrsb* returns [0, prec-1]. */
4050 CASE_INT_FN (BUILT_IN_CLRSB):
4051 arg = gimple_call_arg (stmt, 0);
4052 prec = TYPE_PRECISION (TREE_TYPE (arg));
4053 mini = 0;
4054 maxi = prec - 1;
4055 goto bitop_builtin;
4056 bitop_builtin:
4057 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
4058 build_int_cst (type, maxi), NULL);
4059 return;
4060 default:
4061 break;
4064 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
4066 enum tree_code subcode = ERROR_MARK;
4067 switch (gimple_call_internal_fn (stmt))
4069 case IFN_UBSAN_CHECK_ADD:
4070 subcode = PLUS_EXPR;
4071 break;
4072 case IFN_UBSAN_CHECK_SUB:
4073 subcode = MINUS_EXPR;
4074 break;
4075 case IFN_UBSAN_CHECK_MUL:
4076 subcode = MULT_EXPR;
4077 break;
4078 default:
4079 break;
4081 if (subcode != ERROR_MARK)
4083 bool saved_flag_wrapv = flag_wrapv;
4084 /* Pretend the arithmetics is wrapping. If there is
4085 any overflow, we'll complain, but will actually do
4086 wrapping operation. */
4087 flag_wrapv = 1;
4088 extract_range_from_binary_expr (vr, subcode, type,
4089 gimple_call_arg (stmt, 0),
4090 gimple_call_arg (stmt, 1));
4091 flag_wrapv = saved_flag_wrapv;
4093 /* If for both arguments vrp_valueize returned non-NULL,
4094 this should have been already folded and if not, it
4095 wasn't folded because of overflow. Avoid removing the
4096 UBSAN_CHECK_* calls in that case. */
4097 if (vr->type == VR_RANGE
4098 && (vr->min == vr->max
4099 || operand_equal_p (vr->min, vr->max, 0)))
4100 set_value_range_to_varying (vr);
4101 return;
4104 /* Handle extraction of the two results (result of arithmetics and
4105 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4106 internal function. */
4107 else if (is_gimple_assign (stmt)
4108 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4109 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4110 && INTEGRAL_TYPE_P (type))
4112 enum tree_code code = gimple_assign_rhs_code (stmt);
4113 tree op = gimple_assign_rhs1 (stmt);
4114 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4116 gimple g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4117 if (is_gimple_call (g) && gimple_call_internal_p (g))
4119 enum tree_code subcode = ERROR_MARK;
4120 switch (gimple_call_internal_fn (g))
4122 case IFN_ADD_OVERFLOW:
4123 subcode = PLUS_EXPR;
4124 break;
4125 case IFN_SUB_OVERFLOW:
4126 subcode = MINUS_EXPR;
4127 break;
4128 case IFN_MUL_OVERFLOW:
4129 subcode = MULT_EXPR;
4130 break;
4131 default:
4132 break;
4134 if (subcode != ERROR_MARK)
4136 tree op0 = gimple_call_arg (g, 0);
4137 tree op1 = gimple_call_arg (g, 1);
4138 if (code == IMAGPART_EXPR)
4140 bool ovf = false;
4141 if (check_for_binary_op_overflow (subcode, type,
4142 op0, op1, &ovf))
4143 set_value_range_to_value (vr,
4144 build_int_cst (type, ovf),
4145 NULL);
4146 else
4147 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4148 build_int_cst (type, 1), NULL);
4150 else if (types_compatible_p (type, TREE_TYPE (op0))
4151 && types_compatible_p (type, TREE_TYPE (op1)))
4153 bool saved_flag_wrapv = flag_wrapv;
4154 /* Pretend the arithmetics is wrapping. If there is
4155 any overflow, IMAGPART_EXPR will be set. */
4156 flag_wrapv = 1;
4157 extract_range_from_binary_expr (vr, subcode, type,
4158 op0, op1);
4159 flag_wrapv = saved_flag_wrapv;
4161 else
4163 value_range_t vr0 = VR_INITIALIZER;
4164 value_range_t vr1 = VR_INITIALIZER;
4165 bool saved_flag_wrapv = flag_wrapv;
4166 /* Pretend the arithmetics is wrapping. If there is
4167 any overflow, IMAGPART_EXPR will be set. */
4168 flag_wrapv = 1;
4169 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4170 type, op0);
4171 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4172 type, op1);
4173 extract_range_from_binary_expr_1 (vr, subcode, type,
4174 &vr0, &vr1);
4175 flag_wrapv = saved_flag_wrapv;
4177 return;
4182 if (INTEGRAL_TYPE_P (type)
4183 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4184 set_value_range_to_nonnegative (vr, type,
4185 sop || stmt_overflow_infinity (stmt));
4186 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4187 && !sop)
4188 set_value_range_to_nonnull (vr, type);
4189 else
4190 set_value_range_to_varying (vr);
4194 /* Try to compute a useful range out of assignment STMT and store it
4195 in *VR. */
4197 static void
4198 extract_range_from_assignment (value_range_t *vr, gassign *stmt)
4200 enum tree_code code = gimple_assign_rhs_code (stmt);
4202 if (code == ASSERT_EXPR)
4203 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4204 else if (code == SSA_NAME)
4205 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4206 else if (TREE_CODE_CLASS (code) == tcc_binary)
4207 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4208 gimple_expr_type (stmt),
4209 gimple_assign_rhs1 (stmt),
4210 gimple_assign_rhs2 (stmt));
4211 else if (TREE_CODE_CLASS (code) == tcc_unary)
4212 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4213 gimple_expr_type (stmt),
4214 gimple_assign_rhs1 (stmt));
4215 else if (code == COND_EXPR)
4216 extract_range_from_cond_expr (vr, stmt);
4217 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4218 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4219 gimple_expr_type (stmt),
4220 gimple_assign_rhs1 (stmt),
4221 gimple_assign_rhs2 (stmt));
4222 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4223 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4224 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4225 else
4226 set_value_range_to_varying (vr);
4228 if (vr->type == VR_VARYING)
4229 extract_range_basic (vr, stmt);
4232 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4233 would be profitable to adjust VR using scalar evolution information
4234 for VAR. If so, update VR with the new limits. */
4236 static void
4237 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
4238 gimple stmt, tree var)
4240 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4241 enum ev_direction dir;
4243 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4244 better opportunities than a regular range, but I'm not sure. */
4245 if (vr->type == VR_ANTI_RANGE)
4246 return;
4248 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4250 /* Like in PR19590, scev can return a constant function. */
4251 if (is_gimple_min_invariant (chrec))
4253 set_value_range_to_value (vr, chrec, vr->equiv);
4254 return;
4257 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4258 return;
4260 init = initial_condition_in_loop_num (chrec, loop->num);
4261 tem = op_with_constant_singleton_value_range (init);
4262 if (tem)
4263 init = tem;
4264 step = evolution_part_in_loop_num (chrec, loop->num);
4265 tem = op_with_constant_singleton_value_range (step);
4266 if (tem)
4267 step = tem;
4269 /* If STEP is symbolic, we can't know whether INIT will be the
4270 minimum or maximum value in the range. Also, unless INIT is
4271 a simple expression, compare_values and possibly other functions
4272 in tree-vrp won't be able to handle it. */
4273 if (step == NULL_TREE
4274 || !is_gimple_min_invariant (step)
4275 || !valid_value_p (init))
4276 return;
4278 dir = scev_direction (chrec);
4279 if (/* Do not adjust ranges if we do not know whether the iv increases
4280 or decreases, ... */
4281 dir == EV_DIR_UNKNOWN
4282 /* ... or if it may wrap. */
4283 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4284 true))
4285 return;
4287 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4288 negative_overflow_infinity and positive_overflow_infinity,
4289 because we have concluded that the loop probably does not
4290 wrap. */
4292 type = TREE_TYPE (var);
4293 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4294 tmin = lower_bound_in_type (type, type);
4295 else
4296 tmin = TYPE_MIN_VALUE (type);
4297 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4298 tmax = upper_bound_in_type (type, type);
4299 else
4300 tmax = TYPE_MAX_VALUE (type);
4302 /* Try to use estimated number of iterations for the loop to constrain the
4303 final value in the evolution. */
4304 if (TREE_CODE (step) == INTEGER_CST
4305 && is_gimple_val (init)
4306 && (TREE_CODE (init) != SSA_NAME
4307 || get_value_range (init)->type == VR_RANGE))
4309 widest_int nit;
4311 /* We are only entering here for loop header PHI nodes, so using
4312 the number of latch executions is the correct thing to use. */
4313 if (max_loop_iterations (loop, &nit))
4315 value_range_t maxvr = VR_INITIALIZER;
4316 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4317 bool overflow;
4319 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4320 &overflow);
4321 /* If the multiplication overflowed we can't do a meaningful
4322 adjustment. Likewise if the result doesn't fit in the type
4323 of the induction variable. For a signed type we have to
4324 check whether the result has the expected signedness which
4325 is that of the step as number of iterations is unsigned. */
4326 if (!overflow
4327 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4328 && (sgn == UNSIGNED
4329 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4331 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4332 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4333 TREE_TYPE (init), init, tem);
4334 /* Likewise if the addition did. */
4335 if (maxvr.type == VR_RANGE)
4337 tmin = maxvr.min;
4338 tmax = maxvr.max;
4344 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4346 min = tmin;
4347 max = tmax;
4349 /* For VARYING or UNDEFINED ranges, just about anything we get
4350 from scalar evolutions should be better. */
4352 if (dir == EV_DIR_DECREASES)
4353 max = init;
4354 else
4355 min = init;
4357 else if (vr->type == VR_RANGE)
4359 min = vr->min;
4360 max = vr->max;
4362 if (dir == EV_DIR_DECREASES)
4364 /* INIT is the maximum value. If INIT is lower than VR->MAX
4365 but no smaller than VR->MIN, set VR->MAX to INIT. */
4366 if (compare_values (init, max) == -1)
4367 max = init;
4369 /* According to the loop information, the variable does not
4370 overflow. If we think it does, probably because of an
4371 overflow due to arithmetic on a different INF value,
4372 reset now. */
4373 if (is_negative_overflow_infinity (min)
4374 || compare_values (min, tmin) == -1)
4375 min = tmin;
4378 else
4380 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4381 if (compare_values (init, min) == 1)
4382 min = init;
4384 if (is_positive_overflow_infinity (max)
4385 || compare_values (tmax, max) == -1)
4386 max = tmax;
4389 else
4390 return;
4392 /* If we just created an invalid range with the minimum
4393 greater than the maximum, we fail conservatively.
4394 This should happen only in unreachable
4395 parts of code, or for invalid programs. */
4396 if (compare_values (min, max) == 1
4397 || (is_negative_overflow_infinity (min)
4398 && is_positive_overflow_infinity (max)))
4399 return;
4401 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4405 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4407 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4408 all the values in the ranges.
4410 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4412 - Return NULL_TREE if it is not always possible to determine the
4413 value of the comparison.
4415 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4416 overflow infinity was used in the test. */
4419 static tree
4420 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4421 bool *strict_overflow_p)
4423 /* VARYING or UNDEFINED ranges cannot be compared. */
4424 if (vr0->type == VR_VARYING
4425 || vr0->type == VR_UNDEFINED
4426 || vr1->type == VR_VARYING
4427 || vr1->type == VR_UNDEFINED)
4428 return NULL_TREE;
4430 /* Anti-ranges need to be handled separately. */
4431 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4433 /* If both are anti-ranges, then we cannot compute any
4434 comparison. */
4435 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4436 return NULL_TREE;
4438 /* These comparisons are never statically computable. */
4439 if (comp == GT_EXPR
4440 || comp == GE_EXPR
4441 || comp == LT_EXPR
4442 || comp == LE_EXPR)
4443 return NULL_TREE;
4445 /* Equality can be computed only between a range and an
4446 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4447 if (vr0->type == VR_RANGE)
4449 /* To simplify processing, make VR0 the anti-range. */
4450 value_range_t *tmp = vr0;
4451 vr0 = vr1;
4452 vr1 = tmp;
4455 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4457 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4458 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4459 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4461 return NULL_TREE;
4464 if (!usable_range_p (vr0, strict_overflow_p)
4465 || !usable_range_p (vr1, strict_overflow_p))
4466 return NULL_TREE;
4468 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4469 operands around and change the comparison code. */
4470 if (comp == GT_EXPR || comp == GE_EXPR)
4472 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4473 std::swap (vr0, vr1);
4476 if (comp == EQ_EXPR)
4478 /* Equality may only be computed if both ranges represent
4479 exactly one value. */
4480 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4481 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4483 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4484 strict_overflow_p);
4485 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4486 strict_overflow_p);
4487 if (cmp_min == 0 && cmp_max == 0)
4488 return boolean_true_node;
4489 else if (cmp_min != -2 && cmp_max != -2)
4490 return boolean_false_node;
4492 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4493 else if (compare_values_warnv (vr0->min, vr1->max,
4494 strict_overflow_p) == 1
4495 || compare_values_warnv (vr1->min, vr0->max,
4496 strict_overflow_p) == 1)
4497 return boolean_false_node;
4499 return NULL_TREE;
4501 else if (comp == NE_EXPR)
4503 int cmp1, cmp2;
4505 /* If VR0 is completely to the left or completely to the right
4506 of VR1, they are always different. Notice that we need to
4507 make sure that both comparisons yield similar results to
4508 avoid comparing values that cannot be compared at
4509 compile-time. */
4510 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4511 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4512 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4513 return boolean_true_node;
4515 /* If VR0 and VR1 represent a single value and are identical,
4516 return false. */
4517 else if (compare_values_warnv (vr0->min, vr0->max,
4518 strict_overflow_p) == 0
4519 && compare_values_warnv (vr1->min, vr1->max,
4520 strict_overflow_p) == 0
4521 && compare_values_warnv (vr0->min, vr1->min,
4522 strict_overflow_p) == 0
4523 && compare_values_warnv (vr0->max, vr1->max,
4524 strict_overflow_p) == 0)
4525 return boolean_false_node;
4527 /* Otherwise, they may or may not be different. */
4528 else
4529 return NULL_TREE;
4531 else if (comp == LT_EXPR || comp == LE_EXPR)
4533 int tst;
4535 /* If VR0 is to the left of VR1, return true. */
4536 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4537 if ((comp == LT_EXPR && tst == -1)
4538 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4540 if (overflow_infinity_range_p (vr0)
4541 || overflow_infinity_range_p (vr1))
4542 *strict_overflow_p = true;
4543 return boolean_true_node;
4546 /* If VR0 is to the right of VR1, return false. */
4547 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4548 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4549 || (comp == LE_EXPR && tst == 1))
4551 if (overflow_infinity_range_p (vr0)
4552 || overflow_infinity_range_p (vr1))
4553 *strict_overflow_p = true;
4554 return boolean_false_node;
4557 /* Otherwise, we don't know. */
4558 return NULL_TREE;
4561 gcc_unreachable ();
4565 /* Given a value range VR, a value VAL and a comparison code COMP, return
4566 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4567 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4568 always returns false. Return NULL_TREE if it is not always
4569 possible to determine the value of the comparison. Also set
4570 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4571 infinity was used in the test. */
4573 static tree
4574 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4575 bool *strict_overflow_p)
4577 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4578 return NULL_TREE;
4580 /* Anti-ranges need to be handled separately. */
4581 if (vr->type == VR_ANTI_RANGE)
4583 /* For anti-ranges, the only predicates that we can compute at
4584 compile time are equality and inequality. */
4585 if (comp == GT_EXPR
4586 || comp == GE_EXPR
4587 || comp == LT_EXPR
4588 || comp == LE_EXPR)
4589 return NULL_TREE;
4591 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4592 if (value_inside_range (val, vr->min, vr->max) == 1)
4593 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4595 return NULL_TREE;
4598 if (!usable_range_p (vr, strict_overflow_p))
4599 return NULL_TREE;
4601 if (comp == EQ_EXPR)
4603 /* EQ_EXPR may only be computed if VR represents exactly
4604 one value. */
4605 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4607 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4608 if (cmp == 0)
4609 return boolean_true_node;
4610 else if (cmp == -1 || cmp == 1 || cmp == 2)
4611 return boolean_false_node;
4613 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4614 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4615 return boolean_false_node;
4617 return NULL_TREE;
4619 else if (comp == NE_EXPR)
4621 /* If VAL is not inside VR, then they are always different. */
4622 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4623 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4624 return boolean_true_node;
4626 /* If VR represents exactly one value equal to VAL, then return
4627 false. */
4628 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4629 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4630 return boolean_false_node;
4632 /* Otherwise, they may or may not be different. */
4633 return NULL_TREE;
4635 else if (comp == LT_EXPR || comp == LE_EXPR)
4637 int tst;
4639 /* If VR is to the left of VAL, return true. */
4640 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4641 if ((comp == LT_EXPR && tst == -1)
4642 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4644 if (overflow_infinity_range_p (vr))
4645 *strict_overflow_p = true;
4646 return boolean_true_node;
4649 /* If VR is to the right of VAL, return false. */
4650 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4651 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4652 || (comp == LE_EXPR && tst == 1))
4654 if (overflow_infinity_range_p (vr))
4655 *strict_overflow_p = true;
4656 return boolean_false_node;
4659 /* Otherwise, we don't know. */
4660 return NULL_TREE;
4662 else if (comp == GT_EXPR || comp == GE_EXPR)
4664 int tst;
4666 /* If VR is to the right of VAL, return true. */
4667 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4668 if ((comp == GT_EXPR && tst == 1)
4669 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4671 if (overflow_infinity_range_p (vr))
4672 *strict_overflow_p = true;
4673 return boolean_true_node;
4676 /* If VR is to the left of VAL, return false. */
4677 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4678 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4679 || (comp == GE_EXPR && tst == -1))
4681 if (overflow_infinity_range_p (vr))
4682 *strict_overflow_p = true;
4683 return boolean_false_node;
4686 /* Otherwise, we don't know. */
4687 return NULL_TREE;
4690 gcc_unreachable ();
4694 /* Debugging dumps. */
4696 void dump_value_range (FILE *, value_range_t *);
4697 void debug_value_range (value_range_t *);
4698 void dump_all_value_ranges (FILE *);
4699 void debug_all_value_ranges (void);
4700 void dump_vr_equiv (FILE *, bitmap);
4701 void debug_vr_equiv (bitmap);
4704 /* Dump value range VR to FILE. */
4706 void
4707 dump_value_range (FILE *file, value_range_t *vr)
4709 if (vr == NULL)
4710 fprintf (file, "[]");
4711 else if (vr->type == VR_UNDEFINED)
4712 fprintf (file, "UNDEFINED");
4713 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4715 tree type = TREE_TYPE (vr->min);
4717 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4719 if (is_negative_overflow_infinity (vr->min))
4720 fprintf (file, "-INF(OVF)");
4721 else if (INTEGRAL_TYPE_P (type)
4722 && !TYPE_UNSIGNED (type)
4723 && vrp_val_is_min (vr->min))
4724 fprintf (file, "-INF");
4725 else
4726 print_generic_expr (file, vr->min, 0);
4728 fprintf (file, ", ");
4730 if (is_positive_overflow_infinity (vr->max))
4731 fprintf (file, "+INF(OVF)");
4732 else if (INTEGRAL_TYPE_P (type)
4733 && vrp_val_is_max (vr->max))
4734 fprintf (file, "+INF");
4735 else
4736 print_generic_expr (file, vr->max, 0);
4738 fprintf (file, "]");
4740 if (vr->equiv)
4742 bitmap_iterator bi;
4743 unsigned i, c = 0;
4745 fprintf (file, " EQUIVALENCES: { ");
4747 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4749 print_generic_expr (file, ssa_name (i), 0);
4750 fprintf (file, " ");
4751 c++;
4754 fprintf (file, "} (%u elements)", c);
4757 else if (vr->type == VR_VARYING)
4758 fprintf (file, "VARYING");
4759 else
4760 fprintf (file, "INVALID RANGE");
4764 /* Dump value range VR to stderr. */
4766 DEBUG_FUNCTION void
4767 debug_value_range (value_range_t *vr)
4769 dump_value_range (stderr, vr);
4770 fprintf (stderr, "\n");
4774 /* Dump value ranges of all SSA_NAMEs to FILE. */
4776 void
4777 dump_all_value_ranges (FILE *file)
4779 size_t i;
4781 for (i = 0; i < num_vr_values; i++)
4783 if (vr_value[i])
4785 print_generic_expr (file, ssa_name (i), 0);
4786 fprintf (file, ": ");
4787 dump_value_range (file, vr_value[i]);
4788 fprintf (file, "\n");
4792 fprintf (file, "\n");
4796 /* Dump all value ranges to stderr. */
4798 DEBUG_FUNCTION void
4799 debug_all_value_ranges (void)
4801 dump_all_value_ranges (stderr);
4805 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4806 create a new SSA name N and return the assertion assignment
4807 'N = ASSERT_EXPR <V, V OP W>'. */
4809 static gimple
4810 build_assert_expr_for (tree cond, tree v)
4812 tree a;
4813 gassign *assertion;
4815 gcc_assert (TREE_CODE (v) == SSA_NAME
4816 && COMPARISON_CLASS_P (cond));
4818 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4819 assertion = gimple_build_assign (NULL_TREE, a);
4821 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4822 operand of the ASSERT_EXPR. Create it so the new name and the old one
4823 are registered in the replacement table so that we can fix the SSA web
4824 after adding all the ASSERT_EXPRs. */
4825 create_new_def_for (v, assertion, NULL);
4827 return assertion;
4831 /* Return false if EXPR is a predicate expression involving floating
4832 point values. */
4834 static inline bool
4835 fp_predicate (gimple stmt)
4837 GIMPLE_CHECK (stmt, GIMPLE_COND);
4839 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4842 /* If the range of values taken by OP can be inferred after STMT executes,
4843 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4844 describes the inferred range. Return true if a range could be
4845 inferred. */
4847 static bool
4848 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4850 *val_p = NULL_TREE;
4851 *comp_code_p = ERROR_MARK;
4853 /* Do not attempt to infer anything in names that flow through
4854 abnormal edges. */
4855 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4856 return false;
4858 /* Similarly, don't infer anything from statements that may throw
4859 exceptions. ??? Relax this requirement? */
4860 if (stmt_could_throw_p (stmt))
4861 return false;
4863 /* If STMT is the last statement of a basic block with no normal
4864 successors, there is no point inferring anything about any of its
4865 operands. We would not be able to find a proper insertion point
4866 for the assertion, anyway. */
4867 if (stmt_ends_bb_p (stmt))
4869 edge_iterator ei;
4870 edge e;
4872 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4873 if (!(e->flags & EDGE_ABNORMAL))
4874 break;
4875 if (e == NULL)
4876 return false;
4879 if (infer_nonnull_range (stmt, op))
4881 *val_p = build_int_cst (TREE_TYPE (op), 0);
4882 *comp_code_p = NE_EXPR;
4883 return true;
4886 return false;
4890 void dump_asserts_for (FILE *, tree);
4891 void debug_asserts_for (tree);
4892 void dump_all_asserts (FILE *);
4893 void debug_all_asserts (void);
4895 /* Dump all the registered assertions for NAME to FILE. */
4897 void
4898 dump_asserts_for (FILE *file, tree name)
4900 assert_locus_t loc;
4902 fprintf (file, "Assertions to be inserted for ");
4903 print_generic_expr (file, name, 0);
4904 fprintf (file, "\n");
4906 loc = asserts_for[SSA_NAME_VERSION (name)];
4907 while (loc)
4909 fprintf (file, "\t");
4910 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4911 fprintf (file, "\n\tBB #%d", loc->bb->index);
4912 if (loc->e)
4914 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4915 loc->e->dest->index);
4916 dump_edge_info (file, loc->e, dump_flags, 0);
4918 fprintf (file, "\n\tPREDICATE: ");
4919 print_generic_expr (file, name, 0);
4920 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4921 print_generic_expr (file, loc->val, 0);
4922 fprintf (file, "\n\n");
4923 loc = loc->next;
4926 fprintf (file, "\n");
4930 /* Dump all the registered assertions for NAME to stderr. */
4932 DEBUG_FUNCTION void
4933 debug_asserts_for (tree name)
4935 dump_asserts_for (stderr, name);
4939 /* Dump all the registered assertions for all the names to FILE. */
4941 void
4942 dump_all_asserts (FILE *file)
4944 unsigned i;
4945 bitmap_iterator bi;
4947 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4948 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4949 dump_asserts_for (file, ssa_name (i));
4950 fprintf (file, "\n");
4954 /* Dump all the registered assertions for all the names to stderr. */
4956 DEBUG_FUNCTION void
4957 debug_all_asserts (void)
4959 dump_all_asserts (stderr);
4963 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4964 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4965 E->DEST, then register this location as a possible insertion point
4966 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4968 BB, E and SI provide the exact insertion point for the new
4969 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4970 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4971 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4972 must not be NULL. */
4974 static void
4975 register_new_assert_for (tree name, tree expr,
4976 enum tree_code comp_code,
4977 tree val,
4978 basic_block bb,
4979 edge e,
4980 gimple_stmt_iterator si)
4982 assert_locus_t n, loc, last_loc;
4983 basic_block dest_bb;
4985 gcc_checking_assert (bb == NULL || e == NULL);
4987 if (e == NULL)
4988 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4989 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4991 /* Never build an assert comparing against an integer constant with
4992 TREE_OVERFLOW set. This confuses our undefined overflow warning
4993 machinery. */
4994 if (TREE_OVERFLOW_P (val))
4995 val = drop_tree_overflow (val);
4997 /* The new assertion A will be inserted at BB or E. We need to
4998 determine if the new location is dominated by a previously
4999 registered location for A. If we are doing an edge insertion,
5000 assume that A will be inserted at E->DEST. Note that this is not
5001 necessarily true.
5003 If E is a critical edge, it will be split. But even if E is
5004 split, the new block will dominate the same set of blocks that
5005 E->DEST dominates.
5007 The reverse, however, is not true, blocks dominated by E->DEST
5008 will not be dominated by the new block created to split E. So,
5009 if the insertion location is on a critical edge, we will not use
5010 the new location to move another assertion previously registered
5011 at a block dominated by E->DEST. */
5012 dest_bb = (bb) ? bb : e->dest;
5014 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5015 VAL at a block dominating DEST_BB, then we don't need to insert a new
5016 one. Similarly, if the same assertion already exists at a block
5017 dominated by DEST_BB and the new location is not on a critical
5018 edge, then update the existing location for the assertion (i.e.,
5019 move the assertion up in the dominance tree).
5021 Note, this is implemented as a simple linked list because there
5022 should not be more than a handful of assertions registered per
5023 name. If this becomes a performance problem, a table hashed by
5024 COMP_CODE and VAL could be implemented. */
5025 loc = asserts_for[SSA_NAME_VERSION (name)];
5026 last_loc = loc;
5027 while (loc)
5029 if (loc->comp_code == comp_code
5030 && (loc->val == val
5031 || operand_equal_p (loc->val, val, 0))
5032 && (loc->expr == expr
5033 || operand_equal_p (loc->expr, expr, 0)))
5035 /* If E is not a critical edge and DEST_BB
5036 dominates the existing location for the assertion, move
5037 the assertion up in the dominance tree by updating its
5038 location information. */
5039 if ((e == NULL || !EDGE_CRITICAL_P (e))
5040 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5042 loc->bb = dest_bb;
5043 loc->e = e;
5044 loc->si = si;
5045 return;
5049 /* Update the last node of the list and move to the next one. */
5050 last_loc = loc;
5051 loc = loc->next;
5054 /* If we didn't find an assertion already registered for
5055 NAME COMP_CODE VAL, add a new one at the end of the list of
5056 assertions associated with NAME. */
5057 n = XNEW (struct assert_locus_d);
5058 n->bb = dest_bb;
5059 n->e = e;
5060 n->si = si;
5061 n->comp_code = comp_code;
5062 n->val = val;
5063 n->expr = expr;
5064 n->next = NULL;
5066 if (last_loc)
5067 last_loc->next = n;
5068 else
5069 asserts_for[SSA_NAME_VERSION (name)] = n;
5071 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5074 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5075 Extract a suitable test code and value and store them into *CODE_P and
5076 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5078 If no extraction was possible, return FALSE, otherwise return TRUE.
5080 If INVERT is true, then we invert the result stored into *CODE_P. */
5082 static bool
5083 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5084 tree cond_op0, tree cond_op1,
5085 bool invert, enum tree_code *code_p,
5086 tree *val_p)
5088 enum tree_code comp_code;
5089 tree val;
5091 /* Otherwise, we have a comparison of the form NAME COMP VAL
5092 or VAL COMP NAME. */
5093 if (name == cond_op1)
5095 /* If the predicate is of the form VAL COMP NAME, flip
5096 COMP around because we need to register NAME as the
5097 first operand in the predicate. */
5098 comp_code = swap_tree_comparison (cond_code);
5099 val = cond_op0;
5101 else
5103 /* The comparison is of the form NAME COMP VAL, so the
5104 comparison code remains unchanged. */
5105 comp_code = cond_code;
5106 val = cond_op1;
5109 /* Invert the comparison code as necessary. */
5110 if (invert)
5111 comp_code = invert_tree_comparison (comp_code, 0);
5113 /* VRP does not handle float types. */
5114 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
5115 return false;
5117 /* Do not register always-false predicates.
5118 FIXME: this works around a limitation in fold() when dealing with
5119 enumerations. Given 'enum { N1, N2 } x;', fold will not
5120 fold 'if (x > N2)' to 'if (0)'. */
5121 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5122 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5124 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5125 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5127 if (comp_code == GT_EXPR
5128 && (!max
5129 || compare_values (val, max) == 0))
5130 return false;
5132 if (comp_code == LT_EXPR
5133 && (!min
5134 || compare_values (val, min) == 0))
5135 return false;
5137 *code_p = comp_code;
5138 *val_p = val;
5139 return true;
5142 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5143 (otherwise return VAL). VAL and MASK must be zero-extended for
5144 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5145 (to transform signed values into unsigned) and at the end xor
5146 SGNBIT back. */
5148 static wide_int
5149 masked_increment (const wide_int &val_in, const wide_int &mask,
5150 const wide_int &sgnbit, unsigned int prec)
5152 wide_int bit = wi::one (prec), res;
5153 unsigned int i;
5155 wide_int val = val_in ^ sgnbit;
5156 for (i = 0; i < prec; i++, bit += bit)
5158 res = mask;
5159 if ((res & bit) == 0)
5160 continue;
5161 res = bit - 1;
5162 res = (val + bit).and_not (res);
5163 res &= mask;
5164 if (wi::gtu_p (res, val))
5165 return res ^ sgnbit;
5167 return val ^ sgnbit;
5170 /* Try to register an edge assertion for SSA name NAME on edge E for
5171 the condition COND contributing to the conditional jump pointed to by BSI.
5172 Invert the condition COND if INVERT is true. */
5174 static void
5175 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5176 enum tree_code cond_code,
5177 tree cond_op0, tree cond_op1, bool invert)
5179 tree val;
5180 enum tree_code comp_code;
5182 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5183 cond_op0,
5184 cond_op1,
5185 invert, &comp_code, &val))
5186 return;
5188 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5189 reachable from E. */
5190 if (live_on_edge (e, name)
5191 && !has_single_use (name))
5192 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5194 /* In the case of NAME <= CST and NAME being defined as
5195 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5196 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5197 This catches range and anti-range tests. */
5198 if ((comp_code == LE_EXPR
5199 || comp_code == GT_EXPR)
5200 && TREE_CODE (val) == INTEGER_CST
5201 && TYPE_UNSIGNED (TREE_TYPE (val)))
5203 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5204 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5206 /* Extract CST2 from the (optional) addition. */
5207 if (is_gimple_assign (def_stmt)
5208 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5210 name2 = gimple_assign_rhs1 (def_stmt);
5211 cst2 = gimple_assign_rhs2 (def_stmt);
5212 if (TREE_CODE (name2) == SSA_NAME
5213 && TREE_CODE (cst2) == INTEGER_CST)
5214 def_stmt = SSA_NAME_DEF_STMT (name2);
5217 /* Extract NAME2 from the (optional) sign-changing cast. */
5218 if (gimple_assign_cast_p (def_stmt))
5220 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5221 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5222 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5223 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5224 name3 = gimple_assign_rhs1 (def_stmt);
5227 /* If name3 is used later, create an ASSERT_EXPR for it. */
5228 if (name3 != NULL_TREE
5229 && TREE_CODE (name3) == SSA_NAME
5230 && (cst2 == NULL_TREE
5231 || TREE_CODE (cst2) == INTEGER_CST)
5232 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5233 && live_on_edge (e, name3)
5234 && !has_single_use (name3))
5236 tree tmp;
5238 /* Build an expression for the range test. */
5239 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5240 if (cst2 != NULL_TREE)
5241 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5243 if (dump_file)
5245 fprintf (dump_file, "Adding assert for ");
5246 print_generic_expr (dump_file, name3, 0);
5247 fprintf (dump_file, " from ");
5248 print_generic_expr (dump_file, tmp, 0);
5249 fprintf (dump_file, "\n");
5252 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5255 /* If name2 is used later, create an ASSERT_EXPR for it. */
5256 if (name2 != NULL_TREE
5257 && TREE_CODE (name2) == SSA_NAME
5258 && TREE_CODE (cst2) == INTEGER_CST
5259 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5260 && live_on_edge (e, name2)
5261 && !has_single_use (name2))
5263 tree tmp;
5265 /* Build an expression for the range test. */
5266 tmp = name2;
5267 if (TREE_TYPE (name) != TREE_TYPE (name2))
5268 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5269 if (cst2 != NULL_TREE)
5270 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5272 if (dump_file)
5274 fprintf (dump_file, "Adding assert for ");
5275 print_generic_expr (dump_file, name2, 0);
5276 fprintf (dump_file, " from ");
5277 print_generic_expr (dump_file, tmp, 0);
5278 fprintf (dump_file, "\n");
5281 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5285 /* In the case of post-in/decrement tests like if (i++) ... and uses
5286 of the in/decremented value on the edge the extra name we want to
5287 assert for is not on the def chain of the name compared. Instead
5288 it is in the set of use stmts.
5289 Similar cases happen for conversions that were simplified through
5290 fold_{sign_changed,widened}_comparison. */
5291 if ((comp_code == NE_EXPR
5292 || comp_code == EQ_EXPR)
5293 && TREE_CODE (val) == INTEGER_CST)
5295 imm_use_iterator ui;
5296 gimple use_stmt;
5297 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5299 if (!is_gimple_assign (use_stmt))
5300 continue;
5302 /* Cut off to use-stmts that are dominating the predecessor. */
5303 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5304 continue;
5306 tree name2 = gimple_assign_lhs (use_stmt);
5307 if (TREE_CODE (name2) != SSA_NAME
5308 || !live_on_edge (e, name2))
5309 continue;
5311 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5312 tree cst;
5313 if (code == PLUS_EXPR
5314 || code == MINUS_EXPR)
5316 cst = gimple_assign_rhs2 (use_stmt);
5317 if (TREE_CODE (cst) != INTEGER_CST)
5318 continue;
5319 cst = int_const_binop (code, val, cst);
5321 else if (CONVERT_EXPR_CODE_P (code))
5323 /* For truncating conversions we cannot record
5324 an inequality. */
5325 if (comp_code == NE_EXPR
5326 && (TYPE_PRECISION (TREE_TYPE (name2))
5327 < TYPE_PRECISION (TREE_TYPE (name))))
5328 continue;
5329 cst = fold_convert (TREE_TYPE (name2), val);
5331 else
5332 continue;
5334 if (TREE_OVERFLOW_P (cst))
5335 cst = drop_tree_overflow (cst);
5336 register_new_assert_for (name2, name2, comp_code, cst,
5337 NULL, e, bsi);
5341 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5342 && TREE_CODE (val) == INTEGER_CST)
5344 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5345 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5346 tree val2 = NULL_TREE;
5347 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5348 wide_int mask = wi::zero (prec);
5349 unsigned int nprec = prec;
5350 enum tree_code rhs_code = ERROR_MARK;
5352 if (is_gimple_assign (def_stmt))
5353 rhs_code = gimple_assign_rhs_code (def_stmt);
5355 /* Add asserts for NAME cmp CST and NAME being defined
5356 as NAME = (int) NAME2. */
5357 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5358 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5359 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5360 && gimple_assign_cast_p (def_stmt))
5362 name2 = gimple_assign_rhs1 (def_stmt);
5363 if (CONVERT_EXPR_CODE_P (rhs_code)
5364 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5365 && TYPE_UNSIGNED (TREE_TYPE (name2))
5366 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5367 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5368 || !tree_int_cst_equal (val,
5369 TYPE_MIN_VALUE (TREE_TYPE (val))))
5370 && live_on_edge (e, name2)
5371 && !has_single_use (name2))
5373 tree tmp, cst;
5374 enum tree_code new_comp_code = comp_code;
5376 cst = fold_convert (TREE_TYPE (name2),
5377 TYPE_MIN_VALUE (TREE_TYPE (val)));
5378 /* Build an expression for the range test. */
5379 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5380 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5381 fold_convert (TREE_TYPE (name2), val));
5382 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5384 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5385 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5386 build_int_cst (TREE_TYPE (name2), 1));
5389 if (dump_file)
5391 fprintf (dump_file, "Adding assert for ");
5392 print_generic_expr (dump_file, name2, 0);
5393 fprintf (dump_file, " from ");
5394 print_generic_expr (dump_file, tmp, 0);
5395 fprintf (dump_file, "\n");
5398 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5399 e, bsi);
5403 /* Add asserts for NAME cmp CST and NAME being defined as
5404 NAME = NAME2 >> CST2.
5406 Extract CST2 from the right shift. */
5407 if (rhs_code == RSHIFT_EXPR)
5409 name2 = gimple_assign_rhs1 (def_stmt);
5410 cst2 = gimple_assign_rhs2 (def_stmt);
5411 if (TREE_CODE (name2) == SSA_NAME
5412 && tree_fits_uhwi_p (cst2)
5413 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5414 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5415 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5416 && live_on_edge (e, name2)
5417 && !has_single_use (name2))
5419 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5420 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5423 if (val2 != NULL_TREE
5424 && TREE_CODE (val2) == INTEGER_CST
5425 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5426 TREE_TYPE (val),
5427 val2, cst2), val))
5429 enum tree_code new_comp_code = comp_code;
5430 tree tmp, new_val;
5432 tmp = name2;
5433 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5435 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5437 tree type = build_nonstandard_integer_type (prec, 1);
5438 tmp = build1 (NOP_EXPR, type, name2);
5439 val2 = fold_convert (type, val2);
5441 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5442 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5443 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5445 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5447 wide_int minval
5448 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5449 new_val = val2;
5450 if (minval == new_val)
5451 new_val = NULL_TREE;
5453 else
5455 wide_int maxval
5456 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5457 mask |= val2;
5458 if (mask == maxval)
5459 new_val = NULL_TREE;
5460 else
5461 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5464 if (new_val)
5466 if (dump_file)
5468 fprintf (dump_file, "Adding assert for ");
5469 print_generic_expr (dump_file, name2, 0);
5470 fprintf (dump_file, " from ");
5471 print_generic_expr (dump_file, tmp, 0);
5472 fprintf (dump_file, "\n");
5475 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5476 NULL, e, bsi);
5480 /* Add asserts for NAME cmp CST and NAME being defined as
5481 NAME = NAME2 & CST2.
5483 Extract CST2 from the and.
5485 Also handle
5486 NAME = (unsigned) NAME2;
5487 casts where NAME's type is unsigned and has smaller precision
5488 than NAME2's type as if it was NAME = NAME2 & MASK. */
5489 names[0] = NULL_TREE;
5490 names[1] = NULL_TREE;
5491 cst2 = NULL_TREE;
5492 if (rhs_code == BIT_AND_EXPR
5493 || (CONVERT_EXPR_CODE_P (rhs_code)
5494 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5495 && TYPE_UNSIGNED (TREE_TYPE (val))
5496 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5497 > prec))
5499 name2 = gimple_assign_rhs1 (def_stmt);
5500 if (rhs_code == BIT_AND_EXPR)
5501 cst2 = gimple_assign_rhs2 (def_stmt);
5502 else
5504 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5505 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5507 if (TREE_CODE (name2) == SSA_NAME
5508 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5509 && TREE_CODE (cst2) == INTEGER_CST
5510 && !integer_zerop (cst2)
5511 && (nprec > 1
5512 || TYPE_UNSIGNED (TREE_TYPE (val))))
5514 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5515 if (gimple_assign_cast_p (def_stmt2))
5517 names[1] = gimple_assign_rhs1 (def_stmt2);
5518 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5519 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5520 || (TYPE_PRECISION (TREE_TYPE (name2))
5521 != TYPE_PRECISION (TREE_TYPE (names[1])))
5522 || !live_on_edge (e, names[1])
5523 || has_single_use (names[1]))
5524 names[1] = NULL_TREE;
5526 if (live_on_edge (e, name2)
5527 && !has_single_use (name2))
5528 names[0] = name2;
5531 if (names[0] || names[1])
5533 wide_int minv, maxv, valv, cst2v;
5534 wide_int tem, sgnbit;
5535 bool valid_p = false, valn, cst2n;
5536 enum tree_code ccode = comp_code;
5538 valv = wide_int::from (val, nprec, UNSIGNED);
5539 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5540 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5541 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5542 /* If CST2 doesn't have most significant bit set,
5543 but VAL is negative, we have comparison like
5544 if ((x & 0x123) > -4) (always true). Just give up. */
5545 if (!cst2n && valn)
5546 ccode = ERROR_MARK;
5547 if (cst2n)
5548 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5549 else
5550 sgnbit = wi::zero (nprec);
5551 minv = valv & cst2v;
5552 switch (ccode)
5554 case EQ_EXPR:
5555 /* Minimum unsigned value for equality is VAL & CST2
5556 (should be equal to VAL, otherwise we probably should
5557 have folded the comparison into false) and
5558 maximum unsigned value is VAL | ~CST2. */
5559 maxv = valv | ~cst2v;
5560 valid_p = true;
5561 break;
5563 case NE_EXPR:
5564 tem = valv | ~cst2v;
5565 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5566 if (valv == 0)
5568 cst2n = false;
5569 sgnbit = wi::zero (nprec);
5570 goto gt_expr;
5572 /* If (VAL | ~CST2) is all ones, handle it as
5573 (X & CST2) < VAL. */
5574 if (tem == -1)
5576 cst2n = false;
5577 valn = false;
5578 sgnbit = wi::zero (nprec);
5579 goto lt_expr;
5581 if (!cst2n && wi::neg_p (cst2v))
5582 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5583 if (sgnbit != 0)
5585 if (valv == sgnbit)
5587 cst2n = true;
5588 valn = true;
5589 goto gt_expr;
5591 if (tem == wi::mask (nprec - 1, false, nprec))
5593 cst2n = true;
5594 goto lt_expr;
5596 if (!cst2n)
5597 sgnbit = wi::zero (nprec);
5599 break;
5601 case GE_EXPR:
5602 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5603 is VAL and maximum unsigned value is ~0. For signed
5604 comparison, if CST2 doesn't have most significant bit
5605 set, handle it similarly. If CST2 has MSB set,
5606 the minimum is the same, and maximum is ~0U/2. */
5607 if (minv != valv)
5609 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5610 VAL. */
5611 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5612 if (minv == valv)
5613 break;
5615 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5616 valid_p = true;
5617 break;
5619 case GT_EXPR:
5620 gt_expr:
5621 /* Find out smallest MINV where MINV > VAL
5622 && (MINV & CST2) == MINV, if any. If VAL is signed and
5623 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5624 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5625 if (minv == valv)
5626 break;
5627 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5628 valid_p = true;
5629 break;
5631 case LE_EXPR:
5632 /* Minimum unsigned value for <= is 0 and maximum
5633 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5634 Otherwise, find smallest VAL2 where VAL2 > VAL
5635 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5636 as maximum.
5637 For signed comparison, if CST2 doesn't have most
5638 significant bit set, handle it similarly. If CST2 has
5639 MSB set, the maximum is the same and minimum is INT_MIN. */
5640 if (minv == valv)
5641 maxv = valv;
5642 else
5644 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5645 if (maxv == valv)
5646 break;
5647 maxv -= 1;
5649 maxv |= ~cst2v;
5650 minv = sgnbit;
5651 valid_p = true;
5652 break;
5654 case LT_EXPR:
5655 lt_expr:
5656 /* Minimum unsigned value for < is 0 and maximum
5657 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5658 Otherwise, find smallest VAL2 where VAL2 > VAL
5659 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5660 as maximum.
5661 For signed comparison, if CST2 doesn't have most
5662 significant bit set, handle it similarly. If CST2 has
5663 MSB set, the maximum is the same and minimum is INT_MIN. */
5664 if (minv == valv)
5666 if (valv == sgnbit)
5667 break;
5668 maxv = valv;
5670 else
5672 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5673 if (maxv == valv)
5674 break;
5676 maxv -= 1;
5677 maxv |= ~cst2v;
5678 minv = sgnbit;
5679 valid_p = true;
5680 break;
5682 default:
5683 break;
5685 if (valid_p
5686 && (maxv - minv) != -1)
5688 tree tmp, new_val, type;
5689 int i;
5691 for (i = 0; i < 2; i++)
5692 if (names[i])
5694 wide_int maxv2 = maxv;
5695 tmp = names[i];
5696 type = TREE_TYPE (names[i]);
5697 if (!TYPE_UNSIGNED (type))
5699 type = build_nonstandard_integer_type (nprec, 1);
5700 tmp = build1 (NOP_EXPR, type, names[i]);
5702 if (minv != 0)
5704 tmp = build2 (PLUS_EXPR, type, tmp,
5705 wide_int_to_tree (type, -minv));
5706 maxv2 = maxv - minv;
5708 new_val = wide_int_to_tree (type, maxv2);
5710 if (dump_file)
5712 fprintf (dump_file, "Adding assert for ");
5713 print_generic_expr (dump_file, names[i], 0);
5714 fprintf (dump_file, " from ");
5715 print_generic_expr (dump_file, tmp, 0);
5716 fprintf (dump_file, "\n");
5719 register_new_assert_for (names[i], tmp, LE_EXPR,
5720 new_val, NULL, e, bsi);
5727 /* OP is an operand of a truth value expression which is known to have
5728 a particular value. Register any asserts for OP and for any
5729 operands in OP's defining statement.
5731 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5732 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5734 static void
5735 register_edge_assert_for_1 (tree op, enum tree_code code,
5736 edge e, gimple_stmt_iterator bsi)
5738 gimple op_def;
5739 tree val;
5740 enum tree_code rhs_code;
5742 /* We only care about SSA_NAMEs. */
5743 if (TREE_CODE (op) != SSA_NAME)
5744 return;
5746 /* We know that OP will have a zero or nonzero value. If OP is used
5747 more than once go ahead and register an assert for OP. */
5748 if (live_on_edge (e, op)
5749 && !has_single_use (op))
5751 val = build_int_cst (TREE_TYPE (op), 0);
5752 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5755 /* Now look at how OP is set. If it's set from a comparison,
5756 a truth operation or some bit operations, then we may be able
5757 to register information about the operands of that assignment. */
5758 op_def = SSA_NAME_DEF_STMT (op);
5759 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5760 return;
5762 rhs_code = gimple_assign_rhs_code (op_def);
5764 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5766 bool invert = (code == EQ_EXPR ? true : false);
5767 tree op0 = gimple_assign_rhs1 (op_def);
5768 tree op1 = gimple_assign_rhs2 (op_def);
5770 if (TREE_CODE (op0) == SSA_NAME)
5771 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5772 if (TREE_CODE (op1) == SSA_NAME)
5773 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5775 else if ((code == NE_EXPR
5776 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5777 || (code == EQ_EXPR
5778 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5780 /* Recurse on each operand. */
5781 tree op0 = gimple_assign_rhs1 (op_def);
5782 tree op1 = gimple_assign_rhs2 (op_def);
5783 if (TREE_CODE (op0) == SSA_NAME
5784 && has_single_use (op0))
5785 register_edge_assert_for_1 (op0, code, e, bsi);
5786 if (TREE_CODE (op1) == SSA_NAME
5787 && has_single_use (op1))
5788 register_edge_assert_for_1 (op1, code, e, bsi);
5790 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5791 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5793 /* Recurse, flipping CODE. */
5794 code = invert_tree_comparison (code, false);
5795 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5797 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5799 /* Recurse through the copy. */
5800 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5802 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5804 /* Recurse through the type conversion, unless it is a narrowing
5805 conversion or conversion from non-integral type. */
5806 tree rhs = gimple_assign_rhs1 (op_def);
5807 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5808 && (TYPE_PRECISION (TREE_TYPE (rhs))
5809 <= TYPE_PRECISION (TREE_TYPE (op))))
5810 register_edge_assert_for_1 (rhs, code, e, bsi);
5814 /* Try to register an edge assertion for SSA name NAME on edge E for
5815 the condition COND contributing to the conditional jump pointed to by
5816 SI. */
5818 static void
5819 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5820 enum tree_code cond_code, tree cond_op0,
5821 tree cond_op1)
5823 tree val;
5824 enum tree_code comp_code;
5825 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5827 /* Do not attempt to infer anything in names that flow through
5828 abnormal edges. */
5829 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5830 return;
5832 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5833 cond_op0, cond_op1,
5834 is_else_edge,
5835 &comp_code, &val))
5836 return;
5838 /* Register ASSERT_EXPRs for name. */
5839 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5840 cond_op1, is_else_edge);
5843 /* If COND is effectively an equality test of an SSA_NAME against
5844 the value zero or one, then we may be able to assert values
5845 for SSA_NAMEs which flow into COND. */
5847 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5848 statement of NAME we can assert both operands of the BIT_AND_EXPR
5849 have nonzero value. */
5850 if (((comp_code == EQ_EXPR && integer_onep (val))
5851 || (comp_code == NE_EXPR && integer_zerop (val))))
5853 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5855 if (is_gimple_assign (def_stmt)
5856 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5858 tree op0 = gimple_assign_rhs1 (def_stmt);
5859 tree op1 = gimple_assign_rhs2 (def_stmt);
5860 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5861 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5865 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5866 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5867 have zero value. */
5868 if (((comp_code == EQ_EXPR && integer_zerop (val))
5869 || (comp_code == NE_EXPR && integer_onep (val))))
5871 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5873 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5874 necessarily zero value, or if type-precision is one. */
5875 if (is_gimple_assign (def_stmt)
5876 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5877 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5878 || comp_code == EQ_EXPR)))
5880 tree op0 = gimple_assign_rhs1 (def_stmt);
5881 tree op1 = gimple_assign_rhs2 (def_stmt);
5882 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5883 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5889 /* Determine whether the outgoing edges of BB should receive an
5890 ASSERT_EXPR for each of the operands of BB's LAST statement.
5891 The last statement of BB must be a COND_EXPR.
5893 If any of the sub-graphs rooted at BB have an interesting use of
5894 the predicate operands, an assert location node is added to the
5895 list of assertions for the corresponding operands. */
5897 static void
5898 find_conditional_asserts (basic_block bb, gcond *last)
5900 gimple_stmt_iterator bsi;
5901 tree op;
5902 edge_iterator ei;
5903 edge e;
5904 ssa_op_iter iter;
5906 bsi = gsi_for_stmt (last);
5908 /* Look for uses of the operands in each of the sub-graphs
5909 rooted at BB. We need to check each of the outgoing edges
5910 separately, so that we know what kind of ASSERT_EXPR to
5911 insert. */
5912 FOR_EACH_EDGE (e, ei, bb->succs)
5914 if (e->dest == bb)
5915 continue;
5917 /* Register the necessary assertions for each operand in the
5918 conditional predicate. */
5919 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5920 register_edge_assert_for (op, e, bsi,
5921 gimple_cond_code (last),
5922 gimple_cond_lhs (last),
5923 gimple_cond_rhs (last));
5927 struct case_info
5929 tree expr;
5930 basic_block bb;
5933 /* Compare two case labels sorting first by the destination bb index
5934 and then by the case value. */
5936 static int
5937 compare_case_labels (const void *p1, const void *p2)
5939 const struct case_info *ci1 = (const struct case_info *) p1;
5940 const struct case_info *ci2 = (const struct case_info *) p2;
5941 int idx1 = ci1->bb->index;
5942 int idx2 = ci2->bb->index;
5944 if (idx1 < idx2)
5945 return -1;
5946 else if (idx1 == idx2)
5948 /* Make sure the default label is first in a group. */
5949 if (!CASE_LOW (ci1->expr))
5950 return -1;
5951 else if (!CASE_LOW (ci2->expr))
5952 return 1;
5953 else
5954 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5955 CASE_LOW (ci2->expr));
5957 else
5958 return 1;
5961 /* Determine whether the outgoing edges of BB should receive an
5962 ASSERT_EXPR for each of the operands of BB's LAST statement.
5963 The last statement of BB must be a SWITCH_EXPR.
5965 If any of the sub-graphs rooted at BB have an interesting use of
5966 the predicate operands, an assert location node is added to the
5967 list of assertions for the corresponding operands. */
5969 static void
5970 find_switch_asserts (basic_block bb, gswitch *last)
5972 gimple_stmt_iterator bsi;
5973 tree op;
5974 edge e;
5975 struct case_info *ci;
5976 size_t n = gimple_switch_num_labels (last);
5977 #if GCC_VERSION >= 4000
5978 unsigned int idx;
5979 #else
5980 /* Work around GCC 3.4 bug (PR 37086). */
5981 volatile unsigned int idx;
5982 #endif
5984 bsi = gsi_for_stmt (last);
5985 op = gimple_switch_index (last);
5986 if (TREE_CODE (op) != SSA_NAME)
5987 return;
5989 /* Build a vector of case labels sorted by destination label. */
5990 ci = XNEWVEC (struct case_info, n);
5991 for (idx = 0; idx < n; ++idx)
5993 ci[idx].expr = gimple_switch_label (last, idx);
5994 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5996 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5998 for (idx = 0; idx < n; ++idx)
6000 tree min, max;
6001 tree cl = ci[idx].expr;
6002 basic_block cbb = ci[idx].bb;
6004 min = CASE_LOW (cl);
6005 max = CASE_HIGH (cl);
6007 /* If there are multiple case labels with the same destination
6008 we need to combine them to a single value range for the edge. */
6009 if (idx + 1 < n && cbb == ci[idx + 1].bb)
6011 /* Skip labels until the last of the group. */
6012 do {
6013 ++idx;
6014 } while (idx < n && cbb == ci[idx].bb);
6015 --idx;
6017 /* Pick up the maximum of the case label range. */
6018 if (CASE_HIGH (ci[idx].expr))
6019 max = CASE_HIGH (ci[idx].expr);
6020 else
6021 max = CASE_LOW (ci[idx].expr);
6024 /* Nothing to do if the range includes the default label until we
6025 can register anti-ranges. */
6026 if (min == NULL_TREE)
6027 continue;
6029 /* Find the edge to register the assert expr on. */
6030 e = find_edge (bb, cbb);
6032 /* Register the necessary assertions for the operand in the
6033 SWITCH_EXPR. */
6034 register_edge_assert_for (op, e, bsi,
6035 max ? GE_EXPR : EQ_EXPR,
6036 op, fold_convert (TREE_TYPE (op), min));
6037 if (max)
6038 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6039 fold_convert (TREE_TYPE (op), max));
6042 XDELETEVEC (ci);
6046 /* Traverse all the statements in block BB looking for statements that
6047 may generate useful assertions for the SSA names in their operand.
6048 If a statement produces a useful assertion A for name N_i, then the
6049 list of assertions already generated for N_i is scanned to
6050 determine if A is actually needed.
6052 If N_i already had the assertion A at a location dominating the
6053 current location, then nothing needs to be done. Otherwise, the
6054 new location for A is recorded instead.
6056 1- For every statement S in BB, all the variables used by S are
6057 added to bitmap FOUND_IN_SUBGRAPH.
6059 2- If statement S uses an operand N in a way that exposes a known
6060 value range for N, then if N was not already generated by an
6061 ASSERT_EXPR, create a new assert location for N. For instance,
6062 if N is a pointer and the statement dereferences it, we can
6063 assume that N is not NULL.
6065 3- COND_EXPRs are a special case of #2. We can derive range
6066 information from the predicate but need to insert different
6067 ASSERT_EXPRs for each of the sub-graphs rooted at the
6068 conditional block. If the last statement of BB is a conditional
6069 expression of the form 'X op Y', then
6071 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6073 b) If the conditional is the only entry point to the sub-graph
6074 corresponding to the THEN_CLAUSE, recurse into it. On
6075 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6076 an ASSERT_EXPR is added for the corresponding variable.
6078 c) Repeat step (b) on the ELSE_CLAUSE.
6080 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6082 For instance,
6084 if (a == 9)
6085 b = a;
6086 else
6087 b = c + 1;
6089 In this case, an assertion on the THEN clause is useful to
6090 determine that 'a' is always 9 on that edge. However, an assertion
6091 on the ELSE clause would be unnecessary.
6093 4- If BB does not end in a conditional expression, then we recurse
6094 into BB's dominator children.
6096 At the end of the recursive traversal, every SSA name will have a
6097 list of locations where ASSERT_EXPRs should be added. When a new
6098 location for name N is found, it is registered by calling
6099 register_new_assert_for. That function keeps track of all the
6100 registered assertions to prevent adding unnecessary assertions.
6101 For instance, if a pointer P_4 is dereferenced more than once in a
6102 dominator tree, only the location dominating all the dereference of
6103 P_4 will receive an ASSERT_EXPR. */
6105 static void
6106 find_assert_locations_1 (basic_block bb, sbitmap live)
6108 gimple last;
6110 last = last_stmt (bb);
6112 /* If BB's last statement is a conditional statement involving integer
6113 operands, determine if we need to add ASSERT_EXPRs. */
6114 if (last
6115 && gimple_code (last) == GIMPLE_COND
6116 && !fp_predicate (last)
6117 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6118 find_conditional_asserts (bb, as_a <gcond *> (last));
6120 /* If BB's last statement is a switch statement involving integer
6121 operands, determine if we need to add ASSERT_EXPRs. */
6122 if (last
6123 && gimple_code (last) == GIMPLE_SWITCH
6124 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6125 find_switch_asserts (bb, as_a <gswitch *> (last));
6127 /* Traverse all the statements in BB marking used names and looking
6128 for statements that may infer assertions for their used operands. */
6129 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6130 gsi_prev (&si))
6132 gimple stmt;
6133 tree op;
6134 ssa_op_iter i;
6136 stmt = gsi_stmt (si);
6138 if (is_gimple_debug (stmt))
6139 continue;
6141 /* See if we can derive an assertion for any of STMT's operands. */
6142 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6144 tree value;
6145 enum tree_code comp_code;
6147 /* If op is not live beyond this stmt, do not bother to insert
6148 asserts for it. */
6149 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6150 continue;
6152 /* If OP is used in such a way that we can infer a value
6153 range for it, and we don't find a previous assertion for
6154 it, create a new assertion location node for OP. */
6155 if (infer_value_range (stmt, op, &comp_code, &value))
6157 /* If we are able to infer a nonzero value range for OP,
6158 then walk backwards through the use-def chain to see if OP
6159 was set via a typecast.
6161 If so, then we can also infer a nonzero value range
6162 for the operand of the NOP_EXPR. */
6163 if (comp_code == NE_EXPR && integer_zerop (value))
6165 tree t = op;
6166 gimple def_stmt = SSA_NAME_DEF_STMT (t);
6168 while (is_gimple_assign (def_stmt)
6169 && CONVERT_EXPR_CODE_P
6170 (gimple_assign_rhs_code (def_stmt))
6171 && TREE_CODE
6172 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6173 && POINTER_TYPE_P
6174 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6176 t = gimple_assign_rhs1 (def_stmt);
6177 def_stmt = SSA_NAME_DEF_STMT (t);
6179 /* Note we want to register the assert for the
6180 operand of the NOP_EXPR after SI, not after the
6181 conversion. */
6182 if (! has_single_use (t))
6183 register_new_assert_for (t, t, comp_code, value,
6184 bb, NULL, si);
6188 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6192 /* Update live. */
6193 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6194 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6195 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6196 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6199 /* Traverse all PHI nodes in BB, updating live. */
6200 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6201 gsi_next (&si))
6203 use_operand_p arg_p;
6204 ssa_op_iter i;
6205 gphi *phi = si.phi ();
6206 tree res = gimple_phi_result (phi);
6208 if (virtual_operand_p (res))
6209 continue;
6211 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6213 tree arg = USE_FROM_PTR (arg_p);
6214 if (TREE_CODE (arg) == SSA_NAME)
6215 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6218 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6222 /* Do an RPO walk over the function computing SSA name liveness
6223 on-the-fly and deciding on assert expressions to insert. */
6225 static void
6226 find_assert_locations (void)
6228 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6229 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6230 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6231 int rpo_cnt, i;
6233 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6234 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6235 for (i = 0; i < rpo_cnt; ++i)
6236 bb_rpo[rpo[i]] = i;
6238 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6239 the order we compute liveness and insert asserts we otherwise
6240 fail to insert asserts into the loop latch. */
6241 loop_p loop;
6242 FOR_EACH_LOOP (loop, 0)
6244 i = loop->latch->index;
6245 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6246 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6247 !gsi_end_p (gsi); gsi_next (&gsi))
6249 gphi *phi = gsi.phi ();
6250 if (virtual_operand_p (gimple_phi_result (phi)))
6251 continue;
6252 tree arg = gimple_phi_arg_def (phi, j);
6253 if (TREE_CODE (arg) == SSA_NAME)
6255 if (live[i] == NULL)
6257 live[i] = sbitmap_alloc (num_ssa_names);
6258 bitmap_clear (live[i]);
6260 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6265 for (i = rpo_cnt - 1; i >= 0; --i)
6267 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6268 edge e;
6269 edge_iterator ei;
6271 if (!live[rpo[i]])
6273 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6274 bitmap_clear (live[rpo[i]]);
6277 /* Process BB and update the live information with uses in
6278 this block. */
6279 find_assert_locations_1 (bb, live[rpo[i]]);
6281 /* Merge liveness into the predecessor blocks and free it. */
6282 if (!bitmap_empty_p (live[rpo[i]]))
6284 int pred_rpo = i;
6285 FOR_EACH_EDGE (e, ei, bb->preds)
6287 int pred = e->src->index;
6288 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6289 continue;
6291 if (!live[pred])
6293 live[pred] = sbitmap_alloc (num_ssa_names);
6294 bitmap_clear (live[pred]);
6296 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6298 if (bb_rpo[pred] < pred_rpo)
6299 pred_rpo = bb_rpo[pred];
6302 /* Record the RPO number of the last visited block that needs
6303 live information from this block. */
6304 last_rpo[rpo[i]] = pred_rpo;
6306 else
6308 sbitmap_free (live[rpo[i]]);
6309 live[rpo[i]] = NULL;
6312 /* We can free all successors live bitmaps if all their
6313 predecessors have been visited already. */
6314 FOR_EACH_EDGE (e, ei, bb->succs)
6315 if (last_rpo[e->dest->index] == i
6316 && live[e->dest->index])
6318 sbitmap_free (live[e->dest->index]);
6319 live[e->dest->index] = NULL;
6323 XDELETEVEC (rpo);
6324 XDELETEVEC (bb_rpo);
6325 XDELETEVEC (last_rpo);
6326 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6327 if (live[i])
6328 sbitmap_free (live[i]);
6329 XDELETEVEC (live);
6332 /* Create an ASSERT_EXPR for NAME and insert it in the location
6333 indicated by LOC. Return true if we made any edge insertions. */
6335 static bool
6336 process_assert_insertions_for (tree name, assert_locus_t loc)
6338 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6339 gimple stmt;
6340 tree cond;
6341 gimple assert_stmt;
6342 edge_iterator ei;
6343 edge e;
6345 /* If we have X <=> X do not insert an assert expr for that. */
6346 if (loc->expr == loc->val)
6347 return false;
6349 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6350 assert_stmt = build_assert_expr_for (cond, name);
6351 if (loc->e)
6353 /* We have been asked to insert the assertion on an edge. This
6354 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6355 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6356 || (gimple_code (gsi_stmt (loc->si))
6357 == GIMPLE_SWITCH));
6359 gsi_insert_on_edge (loc->e, assert_stmt);
6360 return true;
6363 /* Otherwise, we can insert right after LOC->SI iff the
6364 statement must not be the last statement in the block. */
6365 stmt = gsi_stmt (loc->si);
6366 if (!stmt_ends_bb_p (stmt))
6368 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6369 return false;
6372 /* If STMT must be the last statement in BB, we can only insert new
6373 assertions on the non-abnormal edge out of BB. Note that since
6374 STMT is not control flow, there may only be one non-abnormal edge
6375 out of BB. */
6376 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6377 if (!(e->flags & EDGE_ABNORMAL))
6379 gsi_insert_on_edge (e, assert_stmt);
6380 return true;
6383 gcc_unreachable ();
6387 /* Process all the insertions registered for every name N_i registered
6388 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6389 found in ASSERTS_FOR[i]. */
6391 static void
6392 process_assert_insertions (void)
6394 unsigned i;
6395 bitmap_iterator bi;
6396 bool update_edges_p = false;
6397 int num_asserts = 0;
6399 if (dump_file && (dump_flags & TDF_DETAILS))
6400 dump_all_asserts (dump_file);
6402 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6404 assert_locus_t loc = asserts_for[i];
6405 gcc_assert (loc);
6407 while (loc)
6409 assert_locus_t next = loc->next;
6410 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6411 free (loc);
6412 loc = next;
6413 num_asserts++;
6417 if (update_edges_p)
6418 gsi_commit_edge_inserts ();
6420 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6421 num_asserts);
6425 /* Traverse the flowgraph looking for conditional jumps to insert range
6426 expressions. These range expressions are meant to provide information
6427 to optimizations that need to reason in terms of value ranges. They
6428 will not be expanded into RTL. For instance, given:
6430 x = ...
6431 y = ...
6432 if (x < y)
6433 y = x - 2;
6434 else
6435 x = y + 3;
6437 this pass will transform the code into:
6439 x = ...
6440 y = ...
6441 if (x < y)
6443 x = ASSERT_EXPR <x, x < y>
6444 y = x - 2
6446 else
6448 y = ASSERT_EXPR <y, x >= y>
6449 x = y + 3
6452 The idea is that once copy and constant propagation have run, other
6453 optimizations will be able to determine what ranges of values can 'x'
6454 take in different paths of the code, simply by checking the reaching
6455 definition of 'x'. */
6457 static void
6458 insert_range_assertions (void)
6460 need_assert_for = BITMAP_ALLOC (NULL);
6461 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6463 calculate_dominance_info (CDI_DOMINATORS);
6465 find_assert_locations ();
6466 if (!bitmap_empty_p (need_assert_for))
6468 process_assert_insertions ();
6469 update_ssa (TODO_update_ssa_no_phi);
6472 if (dump_file && (dump_flags & TDF_DETAILS))
6474 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6475 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6478 free (asserts_for);
6479 BITMAP_FREE (need_assert_for);
6482 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6483 and "struct" hacks. If VRP can determine that the
6484 array subscript is a constant, check if it is outside valid
6485 range. If the array subscript is a RANGE, warn if it is
6486 non-overlapping with valid range.
6487 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6489 static void
6490 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6492 value_range_t* vr = NULL;
6493 tree low_sub, up_sub;
6494 tree low_bound, up_bound, up_bound_p1;
6495 tree base;
6497 if (TREE_NO_WARNING (ref))
6498 return;
6500 low_sub = up_sub = TREE_OPERAND (ref, 1);
6501 up_bound = array_ref_up_bound (ref);
6503 /* Can not check flexible arrays. */
6504 if (!up_bound
6505 || TREE_CODE (up_bound) != INTEGER_CST)
6506 return;
6508 /* Accesses to trailing arrays via pointers may access storage
6509 beyond the types array bounds. */
6510 base = get_base_address (ref);
6511 if ((warn_array_bounds < 2)
6512 && base && TREE_CODE (base) == MEM_REF)
6514 tree cref, next = NULL_TREE;
6516 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6517 return;
6519 cref = TREE_OPERAND (ref, 0);
6520 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6521 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6522 next && TREE_CODE (next) != FIELD_DECL;
6523 next = DECL_CHAIN (next))
6526 /* If this is the last field in a struct type or a field in a
6527 union type do not warn. */
6528 if (!next)
6529 return;
6532 low_bound = array_ref_low_bound (ref);
6533 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6534 build_int_cst (TREE_TYPE (up_bound), 1));
6536 /* Empty array. */
6537 if (tree_int_cst_equal (low_bound, up_bound_p1))
6539 warning_at (location, OPT_Warray_bounds,
6540 "array subscript is above array bounds");
6541 TREE_NO_WARNING (ref) = 1;
6544 if (TREE_CODE (low_sub) == SSA_NAME)
6546 vr = get_value_range (low_sub);
6547 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6549 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6550 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6554 if (vr && vr->type == VR_ANTI_RANGE)
6556 if (TREE_CODE (up_sub) == INTEGER_CST
6557 && (ignore_off_by_one
6558 ? tree_int_cst_lt (up_bound, up_sub)
6559 : tree_int_cst_le (up_bound, up_sub))
6560 && TREE_CODE (low_sub) == INTEGER_CST
6561 && tree_int_cst_le (low_sub, low_bound))
6563 warning_at (location, OPT_Warray_bounds,
6564 "array subscript is outside array bounds");
6565 TREE_NO_WARNING (ref) = 1;
6568 else if (TREE_CODE (up_sub) == INTEGER_CST
6569 && (ignore_off_by_one
6570 ? !tree_int_cst_le (up_sub, up_bound_p1)
6571 : !tree_int_cst_le (up_sub, up_bound)))
6573 if (dump_file && (dump_flags & TDF_DETAILS))
6575 fprintf (dump_file, "Array bound warning for ");
6576 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6577 fprintf (dump_file, "\n");
6579 warning_at (location, OPT_Warray_bounds,
6580 "array subscript is above array bounds");
6581 TREE_NO_WARNING (ref) = 1;
6583 else if (TREE_CODE (low_sub) == INTEGER_CST
6584 && tree_int_cst_lt (low_sub, low_bound))
6586 if (dump_file && (dump_flags & TDF_DETAILS))
6588 fprintf (dump_file, "Array bound warning for ");
6589 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6590 fprintf (dump_file, "\n");
6592 warning_at (location, OPT_Warray_bounds,
6593 "array subscript is below array bounds");
6594 TREE_NO_WARNING (ref) = 1;
6598 /* Searches if the expr T, located at LOCATION computes
6599 address of an ARRAY_REF, and call check_array_ref on it. */
6601 static void
6602 search_for_addr_array (tree t, location_t location)
6604 /* Check each ARRAY_REFs in the reference chain. */
6607 if (TREE_CODE (t) == ARRAY_REF)
6608 check_array_ref (location, t, true /*ignore_off_by_one*/);
6610 t = TREE_OPERAND (t, 0);
6612 while (handled_component_p (t));
6614 if (TREE_CODE (t) == MEM_REF
6615 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6616 && !TREE_NO_WARNING (t))
6618 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6619 tree low_bound, up_bound, el_sz;
6620 offset_int idx;
6621 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6622 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6623 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6624 return;
6626 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6627 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6628 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6629 if (!low_bound
6630 || TREE_CODE (low_bound) != INTEGER_CST
6631 || !up_bound
6632 || TREE_CODE (up_bound) != INTEGER_CST
6633 || !el_sz
6634 || TREE_CODE (el_sz) != INTEGER_CST)
6635 return;
6637 idx = mem_ref_offset (t);
6638 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6639 if (wi::lts_p (idx, 0))
6641 if (dump_file && (dump_flags & TDF_DETAILS))
6643 fprintf (dump_file, "Array bound warning for ");
6644 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6645 fprintf (dump_file, "\n");
6647 warning_at (location, OPT_Warray_bounds,
6648 "array subscript is below array bounds");
6649 TREE_NO_WARNING (t) = 1;
6651 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6652 - wi::to_offset (low_bound) + 1)))
6654 if (dump_file && (dump_flags & TDF_DETAILS))
6656 fprintf (dump_file, "Array bound warning for ");
6657 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6658 fprintf (dump_file, "\n");
6660 warning_at (location, OPT_Warray_bounds,
6661 "array subscript is above array bounds");
6662 TREE_NO_WARNING (t) = 1;
6667 /* walk_tree() callback that checks if *TP is
6668 an ARRAY_REF inside an ADDR_EXPR (in which an array
6669 subscript one outside the valid range is allowed). Call
6670 check_array_ref for each ARRAY_REF found. The location is
6671 passed in DATA. */
6673 static tree
6674 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6676 tree t = *tp;
6677 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6678 location_t location;
6680 if (EXPR_HAS_LOCATION (t))
6681 location = EXPR_LOCATION (t);
6682 else
6684 location_t *locp = (location_t *) wi->info;
6685 location = *locp;
6688 *walk_subtree = TRUE;
6690 if (TREE_CODE (t) == ARRAY_REF)
6691 check_array_ref (location, t, false /*ignore_off_by_one*/);
6693 else if (TREE_CODE (t) == ADDR_EXPR)
6695 search_for_addr_array (t, location);
6696 *walk_subtree = FALSE;
6699 return NULL_TREE;
6702 /* Walk over all statements of all reachable BBs and call check_array_bounds
6703 on them. */
6705 static void
6706 check_all_array_refs (void)
6708 basic_block bb;
6709 gimple_stmt_iterator si;
6711 FOR_EACH_BB_FN (bb, cfun)
6713 edge_iterator ei;
6714 edge e;
6715 bool executable = false;
6717 /* Skip blocks that were found to be unreachable. */
6718 FOR_EACH_EDGE (e, ei, bb->preds)
6719 executable |= !!(e->flags & EDGE_EXECUTABLE);
6720 if (!executable)
6721 continue;
6723 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6725 gimple stmt = gsi_stmt (si);
6726 struct walk_stmt_info wi;
6727 if (!gimple_has_location (stmt)
6728 || is_gimple_debug (stmt))
6729 continue;
6731 memset (&wi, 0, sizeof (wi));
6732 wi.info = CONST_CAST (void *, (const void *)
6733 gimple_location_ptr (stmt));
6735 walk_gimple_op (gsi_stmt (si),
6736 check_array_bounds,
6737 &wi);
6742 /* Return true if all imm uses of VAR are either in STMT, or
6743 feed (optionally through a chain of single imm uses) GIMPLE_COND
6744 in basic block COND_BB. */
6746 static bool
6747 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6749 use_operand_p use_p, use2_p;
6750 imm_use_iterator iter;
6752 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6753 if (USE_STMT (use_p) != stmt)
6755 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6756 if (is_gimple_debug (use_stmt))
6757 continue;
6758 while (is_gimple_assign (use_stmt)
6759 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6760 && single_imm_use (gimple_assign_lhs (use_stmt),
6761 &use2_p, &use_stmt2))
6762 use_stmt = use_stmt2;
6763 if (gimple_code (use_stmt) != GIMPLE_COND
6764 || gimple_bb (use_stmt) != cond_bb)
6765 return false;
6767 return true;
6770 /* Handle
6771 _4 = x_3 & 31;
6772 if (_4 != 0)
6773 goto <bb 6>;
6774 else
6775 goto <bb 7>;
6776 <bb 6>:
6777 __builtin_unreachable ();
6778 <bb 7>:
6779 x_5 = ASSERT_EXPR <x_3, ...>;
6780 If x_3 has no other immediate uses (checked by caller),
6781 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6782 from the non-zero bitmask. */
6784 static void
6785 maybe_set_nonzero_bits (basic_block bb, tree var)
6787 edge e = single_pred_edge (bb);
6788 basic_block cond_bb = e->src;
6789 gimple stmt = last_stmt (cond_bb);
6790 tree cst;
6792 if (stmt == NULL
6793 || gimple_code (stmt) != GIMPLE_COND
6794 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6795 ? EQ_EXPR : NE_EXPR)
6796 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6797 || !integer_zerop (gimple_cond_rhs (stmt)))
6798 return;
6800 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6801 if (!is_gimple_assign (stmt)
6802 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6803 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6804 return;
6805 if (gimple_assign_rhs1 (stmt) != var)
6807 gimple stmt2;
6809 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6810 return;
6811 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6812 if (!gimple_assign_cast_p (stmt2)
6813 || gimple_assign_rhs1 (stmt2) != var
6814 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6815 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6816 != TYPE_PRECISION (TREE_TYPE (var))))
6817 return;
6819 cst = gimple_assign_rhs2 (stmt);
6820 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6823 /* Convert range assertion expressions into the implied copies and
6824 copy propagate away the copies. Doing the trivial copy propagation
6825 here avoids the need to run the full copy propagation pass after
6826 VRP.
6828 FIXME, this will eventually lead to copy propagation removing the
6829 names that had useful range information attached to them. For
6830 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6831 then N_i will have the range [3, +INF].
6833 However, by converting the assertion into the implied copy
6834 operation N_i = N_j, we will then copy-propagate N_j into the uses
6835 of N_i and lose the range information. We may want to hold on to
6836 ASSERT_EXPRs a little while longer as the ranges could be used in
6837 things like jump threading.
6839 The problem with keeping ASSERT_EXPRs around is that passes after
6840 VRP need to handle them appropriately.
6842 Another approach would be to make the range information a first
6843 class property of the SSA_NAME so that it can be queried from
6844 any pass. This is made somewhat more complex by the need for
6845 multiple ranges to be associated with one SSA_NAME. */
6847 static void
6848 remove_range_assertions (void)
6850 basic_block bb;
6851 gimple_stmt_iterator si;
6852 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6853 a basic block preceeded by GIMPLE_COND branching to it and
6854 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6855 int is_unreachable;
6857 /* Note that the BSI iterator bump happens at the bottom of the
6858 loop and no bump is necessary if we're removing the statement
6859 referenced by the current BSI. */
6860 FOR_EACH_BB_FN (bb, cfun)
6861 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6863 gimple stmt = gsi_stmt (si);
6864 gimple use_stmt;
6866 if (is_gimple_assign (stmt)
6867 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6869 tree lhs = gimple_assign_lhs (stmt);
6870 tree rhs = gimple_assign_rhs1 (stmt);
6871 tree var;
6872 tree cond = fold (ASSERT_EXPR_COND (rhs));
6873 use_operand_p use_p;
6874 imm_use_iterator iter;
6876 gcc_assert (cond != boolean_false_node);
6878 var = ASSERT_EXPR_VAR (rhs);
6879 gcc_assert (TREE_CODE (var) == SSA_NAME);
6881 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6882 && SSA_NAME_RANGE_INFO (lhs))
6884 if (is_unreachable == -1)
6886 is_unreachable = 0;
6887 if (single_pred_p (bb)
6888 && assert_unreachable_fallthru_edge_p
6889 (single_pred_edge (bb)))
6890 is_unreachable = 1;
6892 /* Handle
6893 if (x_7 >= 10 && x_7 < 20)
6894 __builtin_unreachable ();
6895 x_8 = ASSERT_EXPR <x_7, ...>;
6896 if the only uses of x_7 are in the ASSERT_EXPR and
6897 in the condition. In that case, we can copy the
6898 range info from x_8 computed in this pass also
6899 for x_7. */
6900 if (is_unreachable
6901 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6902 single_pred (bb)))
6904 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6905 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6906 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6907 maybe_set_nonzero_bits (bb, var);
6911 /* Propagate the RHS into every use of the LHS. */
6912 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6913 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6914 SET_USE (use_p, var);
6916 /* And finally, remove the copy, it is not needed. */
6917 gsi_remove (&si, true);
6918 release_defs (stmt);
6920 else
6922 if (!is_gimple_debug (gsi_stmt (si)))
6923 is_unreachable = 0;
6924 gsi_next (&si);
6930 /* Return true if STMT is interesting for VRP. */
6932 static bool
6933 stmt_interesting_for_vrp (gimple stmt)
6935 if (gimple_code (stmt) == GIMPLE_PHI)
6937 tree res = gimple_phi_result (stmt);
6938 return (!virtual_operand_p (res)
6939 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6940 || POINTER_TYPE_P (TREE_TYPE (res))));
6942 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6944 tree lhs = gimple_get_lhs (stmt);
6946 /* In general, assignments with virtual operands are not useful
6947 for deriving ranges, with the obvious exception of calls to
6948 builtin functions. */
6949 if (lhs && TREE_CODE (lhs) == SSA_NAME
6950 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6951 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6952 && (is_gimple_call (stmt)
6953 || !gimple_vuse (stmt)))
6954 return true;
6955 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6956 switch (gimple_call_internal_fn (stmt))
6958 case IFN_ADD_OVERFLOW:
6959 case IFN_SUB_OVERFLOW:
6960 case IFN_MUL_OVERFLOW:
6961 /* These internal calls return _Complex integer type,
6962 but are interesting to VRP nevertheless. */
6963 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6964 return true;
6965 break;
6966 default:
6967 break;
6970 else if (gimple_code (stmt) == GIMPLE_COND
6971 || gimple_code (stmt) == GIMPLE_SWITCH)
6972 return true;
6974 return false;
6978 /* Initialize local data structures for VRP. */
6980 static void
6981 vrp_initialize (void)
6983 basic_block bb;
6985 values_propagated = false;
6986 num_vr_values = num_ssa_names;
6987 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6988 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6990 FOR_EACH_BB_FN (bb, cfun)
6992 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6993 gsi_next (&si))
6995 gphi *phi = si.phi ();
6996 if (!stmt_interesting_for_vrp (phi))
6998 tree lhs = PHI_RESULT (phi);
6999 set_value_range_to_varying (get_value_range (lhs));
7000 prop_set_simulate_again (phi, false);
7002 else
7003 prop_set_simulate_again (phi, true);
7006 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7007 gsi_next (&si))
7009 gimple stmt = gsi_stmt (si);
7011 /* If the statement is a control insn, then we do not
7012 want to avoid simulating the statement once. Failure
7013 to do so means that those edges will never get added. */
7014 if (stmt_ends_bb_p (stmt))
7015 prop_set_simulate_again (stmt, true);
7016 else if (!stmt_interesting_for_vrp (stmt))
7018 ssa_op_iter i;
7019 tree def;
7020 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7021 set_value_range_to_varying (get_value_range (def));
7022 prop_set_simulate_again (stmt, false);
7024 else
7025 prop_set_simulate_again (stmt, true);
7030 /* Return the singleton value-range for NAME or NAME. */
7032 static inline tree
7033 vrp_valueize (tree name)
7035 if (TREE_CODE (name) == SSA_NAME)
7037 value_range_t *vr = get_value_range (name);
7038 if (vr->type == VR_RANGE
7039 && (vr->min == vr->max
7040 || operand_equal_p (vr->min, vr->max, 0)))
7041 return vr->min;
7043 return name;
7046 /* Return the singleton value-range for NAME if that is a constant
7047 but signal to not follow SSA edges. */
7049 static inline tree
7050 vrp_valueize_1 (tree name)
7052 if (TREE_CODE (name) == SSA_NAME)
7054 /* If the definition may be simulated again we cannot follow
7055 this SSA edge as the SSA propagator does not necessarily
7056 re-visit the use. */
7057 gimple def_stmt = SSA_NAME_DEF_STMT (name);
7058 if (!gimple_nop_p (def_stmt)
7059 && prop_simulate_again_p (def_stmt))
7060 return NULL_TREE;
7061 value_range_t *vr = get_value_range (name);
7062 if (range_int_cst_singleton_p (vr))
7063 return vr->min;
7065 return name;
7068 /* Visit assignment STMT. If it produces an interesting range, record
7069 the SSA name in *OUTPUT_P. */
7071 static enum ssa_prop_result
7072 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
7074 tree def, lhs;
7075 ssa_op_iter iter;
7076 enum gimple_code code = gimple_code (stmt);
7077 lhs = gimple_get_lhs (stmt);
7079 /* We only keep track of ranges in integral and pointer types. */
7080 if (TREE_CODE (lhs) == SSA_NAME
7081 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7082 /* It is valid to have NULL MIN/MAX values on a type. See
7083 build_range_type. */
7084 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7085 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7086 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7088 value_range_t new_vr = VR_INITIALIZER;
7090 /* Try folding the statement to a constant first. */
7091 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7092 vrp_valueize_1);
7093 if (tem && is_gimple_min_invariant (tem))
7094 set_value_range_to_value (&new_vr, tem, NULL);
7095 /* Then dispatch to value-range extracting functions. */
7096 else if (code == GIMPLE_CALL)
7097 extract_range_basic (&new_vr, stmt);
7098 else
7099 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7101 if (update_value_range (lhs, &new_vr))
7103 *output_p = lhs;
7105 if (dump_file && (dump_flags & TDF_DETAILS))
7107 fprintf (dump_file, "Found new range for ");
7108 print_generic_expr (dump_file, lhs, 0);
7109 fprintf (dump_file, ": ");
7110 dump_value_range (dump_file, &new_vr);
7111 fprintf (dump_file, "\n");
7114 if (new_vr.type == VR_VARYING)
7115 return SSA_PROP_VARYING;
7117 return SSA_PROP_INTERESTING;
7120 return SSA_PROP_NOT_INTERESTING;
7122 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7123 switch (gimple_call_internal_fn (stmt))
7125 case IFN_ADD_OVERFLOW:
7126 case IFN_SUB_OVERFLOW:
7127 case IFN_MUL_OVERFLOW:
7128 /* These internal calls return _Complex integer type,
7129 which VRP does not track, but the immediate uses
7130 thereof might be interesting. */
7131 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7133 imm_use_iterator iter;
7134 use_operand_p use_p;
7135 enum ssa_prop_result res = SSA_PROP_VARYING;
7137 set_value_range_to_varying (get_value_range (lhs));
7139 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7141 gimple use_stmt = USE_STMT (use_p);
7142 if (!is_gimple_assign (use_stmt))
7143 continue;
7144 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7145 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7146 continue;
7147 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7148 tree use_lhs = gimple_assign_lhs (use_stmt);
7149 if (TREE_CODE (rhs1) != rhs_code
7150 || TREE_OPERAND (rhs1, 0) != lhs
7151 || TREE_CODE (use_lhs) != SSA_NAME
7152 || !stmt_interesting_for_vrp (use_stmt)
7153 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7154 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7155 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7156 continue;
7158 /* If there is a change in the value range for any of the
7159 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7160 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7161 or IMAGPART_EXPR immediate uses, but none of them have
7162 a change in their value ranges, return
7163 SSA_PROP_NOT_INTERESTING. If there are no
7164 {REAL,IMAG}PART_EXPR uses at all,
7165 return SSA_PROP_VARYING. */
7166 value_range_t new_vr = VR_INITIALIZER;
7167 extract_range_basic (&new_vr, use_stmt);
7168 value_range_t *old_vr = get_value_range (use_lhs);
7169 if (old_vr->type != new_vr.type
7170 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7171 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7172 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7173 res = SSA_PROP_INTERESTING;
7174 else
7175 res = SSA_PROP_NOT_INTERESTING;
7176 BITMAP_FREE (new_vr.equiv);
7177 if (res == SSA_PROP_INTERESTING)
7179 *output_p = lhs;
7180 return res;
7184 return res;
7186 break;
7187 default:
7188 break;
7191 /* Every other statement produces no useful ranges. */
7192 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7193 set_value_range_to_varying (get_value_range (def));
7195 return SSA_PROP_VARYING;
7198 /* Helper that gets the value range of the SSA_NAME with version I
7199 or a symbolic range containing the SSA_NAME only if the value range
7200 is varying or undefined. */
7202 static inline value_range_t
7203 get_vr_for_comparison (int i)
7205 value_range_t vr = *get_value_range (ssa_name (i));
7207 /* If name N_i does not have a valid range, use N_i as its own
7208 range. This allows us to compare against names that may
7209 have N_i in their ranges. */
7210 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7212 vr.type = VR_RANGE;
7213 vr.min = ssa_name (i);
7214 vr.max = ssa_name (i);
7217 return vr;
7220 /* Compare all the value ranges for names equivalent to VAR with VAL
7221 using comparison code COMP. Return the same value returned by
7222 compare_range_with_value, including the setting of
7223 *STRICT_OVERFLOW_P. */
7225 static tree
7226 compare_name_with_value (enum tree_code comp, tree var, tree val,
7227 bool *strict_overflow_p)
7229 bitmap_iterator bi;
7230 unsigned i;
7231 bitmap e;
7232 tree retval, t;
7233 int used_strict_overflow;
7234 bool sop;
7235 value_range_t equiv_vr;
7237 /* Get the set of equivalences for VAR. */
7238 e = get_value_range (var)->equiv;
7240 /* Start at -1. Set it to 0 if we do a comparison without relying
7241 on overflow, or 1 if all comparisons rely on overflow. */
7242 used_strict_overflow = -1;
7244 /* Compare vars' value range with val. */
7245 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7246 sop = false;
7247 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7248 if (retval)
7249 used_strict_overflow = sop ? 1 : 0;
7251 /* If the equiv set is empty we have done all work we need to do. */
7252 if (e == NULL)
7254 if (retval
7255 && used_strict_overflow > 0)
7256 *strict_overflow_p = true;
7257 return retval;
7260 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7262 equiv_vr = get_vr_for_comparison (i);
7263 sop = false;
7264 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7265 if (t)
7267 /* If we get different answers from different members
7268 of the equivalence set this check must be in a dead
7269 code region. Folding it to a trap representation
7270 would be correct here. For now just return don't-know. */
7271 if (retval != NULL
7272 && t != retval)
7274 retval = NULL_TREE;
7275 break;
7277 retval = t;
7279 if (!sop)
7280 used_strict_overflow = 0;
7281 else if (used_strict_overflow < 0)
7282 used_strict_overflow = 1;
7286 if (retval
7287 && used_strict_overflow > 0)
7288 *strict_overflow_p = true;
7290 return retval;
7294 /* Given a comparison code COMP and names N1 and N2, compare all the
7295 ranges equivalent to N1 against all the ranges equivalent to N2
7296 to determine the value of N1 COMP N2. Return the same value
7297 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7298 whether we relied on an overflow infinity in the comparison. */
7301 static tree
7302 compare_names (enum tree_code comp, tree n1, tree n2,
7303 bool *strict_overflow_p)
7305 tree t, retval;
7306 bitmap e1, e2;
7307 bitmap_iterator bi1, bi2;
7308 unsigned i1, i2;
7309 int used_strict_overflow;
7310 static bitmap_obstack *s_obstack = NULL;
7311 static bitmap s_e1 = NULL, s_e2 = NULL;
7313 /* Compare the ranges of every name equivalent to N1 against the
7314 ranges of every name equivalent to N2. */
7315 e1 = get_value_range (n1)->equiv;
7316 e2 = get_value_range (n2)->equiv;
7318 /* Use the fake bitmaps if e1 or e2 are not available. */
7319 if (s_obstack == NULL)
7321 s_obstack = XNEW (bitmap_obstack);
7322 bitmap_obstack_initialize (s_obstack);
7323 s_e1 = BITMAP_ALLOC (s_obstack);
7324 s_e2 = BITMAP_ALLOC (s_obstack);
7326 if (e1 == NULL)
7327 e1 = s_e1;
7328 if (e2 == NULL)
7329 e2 = s_e2;
7331 /* Add N1 and N2 to their own set of equivalences to avoid
7332 duplicating the body of the loop just to check N1 and N2
7333 ranges. */
7334 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7335 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7337 /* If the equivalence sets have a common intersection, then the two
7338 names can be compared without checking their ranges. */
7339 if (bitmap_intersect_p (e1, e2))
7341 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7342 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7344 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7345 ? boolean_true_node
7346 : boolean_false_node;
7349 /* Start at -1. Set it to 0 if we do a comparison without relying
7350 on overflow, or 1 if all comparisons rely on overflow. */
7351 used_strict_overflow = -1;
7353 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7354 N2 to their own set of equivalences to avoid duplicating the body
7355 of the loop just to check N1 and N2 ranges. */
7356 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7358 value_range_t vr1 = get_vr_for_comparison (i1);
7360 t = retval = NULL_TREE;
7361 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7363 bool sop = false;
7365 value_range_t vr2 = get_vr_for_comparison (i2);
7367 t = compare_ranges (comp, &vr1, &vr2, &sop);
7368 if (t)
7370 /* If we get different answers from different members
7371 of the equivalence set this check must be in a dead
7372 code region. Folding it to a trap representation
7373 would be correct here. For now just return don't-know. */
7374 if (retval != NULL
7375 && t != retval)
7377 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7378 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7379 return NULL_TREE;
7381 retval = t;
7383 if (!sop)
7384 used_strict_overflow = 0;
7385 else if (used_strict_overflow < 0)
7386 used_strict_overflow = 1;
7390 if (retval)
7392 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7393 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7394 if (used_strict_overflow > 0)
7395 *strict_overflow_p = true;
7396 return retval;
7400 /* None of the equivalent ranges are useful in computing this
7401 comparison. */
7402 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7403 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7404 return NULL_TREE;
7407 /* Helper function for vrp_evaluate_conditional_warnv & other
7408 optimizers. */
7410 static tree
7411 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7412 tree op0, tree op1,
7413 bool * strict_overflow_p)
7415 value_range_t *vr0, *vr1;
7417 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7418 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7420 tree res = NULL_TREE;
7421 if (vr0 && vr1)
7422 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7423 if (!res && vr0)
7424 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7425 if (!res && vr1)
7426 res = (compare_range_with_value
7427 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7428 return res;
7431 /* Helper function for vrp_evaluate_conditional_warnv. */
7433 static tree
7434 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7435 tree op1, bool use_equiv_p,
7436 bool *strict_overflow_p, bool *only_ranges)
7438 tree ret;
7439 if (only_ranges)
7440 *only_ranges = true;
7442 /* We only deal with integral and pointer types. */
7443 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7444 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7445 return NULL_TREE;
7447 if (use_equiv_p)
7449 if (only_ranges
7450 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7451 (code, op0, op1, strict_overflow_p)))
7452 return ret;
7453 *only_ranges = false;
7454 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7455 return compare_names (code, op0, op1, strict_overflow_p);
7456 else if (TREE_CODE (op0) == SSA_NAME)
7457 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7458 else if (TREE_CODE (op1) == SSA_NAME)
7459 return (compare_name_with_value
7460 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7462 else
7463 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7464 strict_overflow_p);
7465 return NULL_TREE;
7468 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7469 information. Return NULL if the conditional can not be evaluated.
7470 The ranges of all the names equivalent with the operands in COND
7471 will be used when trying to compute the value. If the result is
7472 based on undefined signed overflow, issue a warning if
7473 appropriate. */
7475 static tree
7476 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7478 bool sop;
7479 tree ret;
7480 bool only_ranges;
7482 /* Some passes and foldings leak constants with overflow flag set
7483 into the IL. Avoid doing wrong things with these and bail out. */
7484 if ((TREE_CODE (op0) == INTEGER_CST
7485 && TREE_OVERFLOW (op0))
7486 || (TREE_CODE (op1) == INTEGER_CST
7487 && TREE_OVERFLOW (op1)))
7488 return NULL_TREE;
7490 sop = false;
7491 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7492 &only_ranges);
7494 if (ret && sop)
7496 enum warn_strict_overflow_code wc;
7497 const char* warnmsg;
7499 if (is_gimple_min_invariant (ret))
7501 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7502 warnmsg = G_("assuming signed overflow does not occur when "
7503 "simplifying conditional to constant");
7505 else
7507 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7508 warnmsg = G_("assuming signed overflow does not occur when "
7509 "simplifying conditional");
7512 if (issue_strict_overflow_warning (wc))
7514 location_t location;
7516 if (!gimple_has_location (stmt))
7517 location = input_location;
7518 else
7519 location = gimple_location (stmt);
7520 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7524 if (warn_type_limits
7525 && ret && only_ranges
7526 && TREE_CODE_CLASS (code) == tcc_comparison
7527 && TREE_CODE (op0) == SSA_NAME)
7529 /* If the comparison is being folded and the operand on the LHS
7530 is being compared against a constant value that is outside of
7531 the natural range of OP0's type, then the predicate will
7532 always fold regardless of the value of OP0. If -Wtype-limits
7533 was specified, emit a warning. */
7534 tree type = TREE_TYPE (op0);
7535 value_range_t *vr0 = get_value_range (op0);
7537 if (vr0->type == VR_RANGE
7538 && INTEGRAL_TYPE_P (type)
7539 && vrp_val_is_min (vr0->min)
7540 && vrp_val_is_max (vr0->max)
7541 && is_gimple_min_invariant (op1))
7543 location_t location;
7545 if (!gimple_has_location (stmt))
7546 location = input_location;
7547 else
7548 location = gimple_location (stmt);
7550 warning_at (location, OPT_Wtype_limits,
7551 integer_zerop (ret)
7552 ? G_("comparison always false "
7553 "due to limited range of data type")
7554 : G_("comparison always true "
7555 "due to limited range of data type"));
7559 return ret;
7563 /* Visit conditional statement STMT. If we can determine which edge
7564 will be taken out of STMT's basic block, record it in
7565 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7566 SSA_PROP_VARYING. */
7568 static enum ssa_prop_result
7569 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7571 tree val;
7572 bool sop;
7574 *taken_edge_p = NULL;
7576 if (dump_file && (dump_flags & TDF_DETAILS))
7578 tree use;
7579 ssa_op_iter i;
7581 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7582 print_gimple_stmt (dump_file, stmt, 0, 0);
7583 fprintf (dump_file, "\nWith known ranges\n");
7585 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7587 fprintf (dump_file, "\t");
7588 print_generic_expr (dump_file, use, 0);
7589 fprintf (dump_file, ": ");
7590 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7593 fprintf (dump_file, "\n");
7596 /* Compute the value of the predicate COND by checking the known
7597 ranges of each of its operands.
7599 Note that we cannot evaluate all the equivalent ranges here
7600 because those ranges may not yet be final and with the current
7601 propagation strategy, we cannot determine when the value ranges
7602 of the names in the equivalence set have changed.
7604 For instance, given the following code fragment
7606 i_5 = PHI <8, i_13>
7608 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7609 if (i_14 == 1)
7612 Assume that on the first visit to i_14, i_5 has the temporary
7613 range [8, 8] because the second argument to the PHI function is
7614 not yet executable. We derive the range ~[0, 0] for i_14 and the
7615 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7616 the first time, since i_14 is equivalent to the range [8, 8], we
7617 determine that the predicate is always false.
7619 On the next round of propagation, i_13 is determined to be
7620 VARYING, which causes i_5 to drop down to VARYING. So, another
7621 visit to i_14 is scheduled. In this second visit, we compute the
7622 exact same range and equivalence set for i_14, namely ~[0, 0] and
7623 { i_5 }. But we did not have the previous range for i_5
7624 registered, so vrp_visit_assignment thinks that the range for
7625 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7626 is not visited again, which stops propagation from visiting
7627 statements in the THEN clause of that if().
7629 To properly fix this we would need to keep the previous range
7630 value for the names in the equivalence set. This way we would've
7631 discovered that from one visit to the other i_5 changed from
7632 range [8, 8] to VR_VARYING.
7634 However, fixing this apparent limitation may not be worth the
7635 additional checking. Testing on several code bases (GCC, DLV,
7636 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7637 4 more predicates folded in SPEC. */
7638 sop = false;
7640 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7641 gimple_cond_lhs (stmt),
7642 gimple_cond_rhs (stmt),
7643 false, &sop, NULL);
7644 if (val)
7646 if (!sop)
7647 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7648 else
7650 if (dump_file && (dump_flags & TDF_DETAILS))
7651 fprintf (dump_file,
7652 "\nIgnoring predicate evaluation because "
7653 "it assumes that signed overflow is undefined");
7654 val = NULL_TREE;
7658 if (dump_file && (dump_flags & TDF_DETAILS))
7660 fprintf (dump_file, "\nPredicate evaluates to: ");
7661 if (val == NULL_TREE)
7662 fprintf (dump_file, "DON'T KNOW\n");
7663 else
7664 print_generic_stmt (dump_file, val, 0);
7667 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7670 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7671 that includes the value VAL. The search is restricted to the range
7672 [START_IDX, n - 1] where n is the size of VEC.
7674 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7675 returned.
7677 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7678 it is placed in IDX and false is returned.
7680 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7681 returned. */
7683 static bool
7684 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7686 size_t n = gimple_switch_num_labels (stmt);
7687 size_t low, high;
7689 /* Find case label for minimum of the value range or the next one.
7690 At each iteration we are searching in [low, high - 1]. */
7692 for (low = start_idx, high = n; high != low; )
7694 tree t;
7695 int cmp;
7696 /* Note that i != high, so we never ask for n. */
7697 size_t i = (high + low) / 2;
7698 t = gimple_switch_label (stmt, i);
7700 /* Cache the result of comparing CASE_LOW and val. */
7701 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7703 if (cmp == 0)
7705 /* Ranges cannot be empty. */
7706 *idx = i;
7707 return true;
7709 else if (cmp > 0)
7710 high = i;
7711 else
7713 low = i + 1;
7714 if (CASE_HIGH (t) != NULL
7715 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7717 *idx = i;
7718 return true;
7723 *idx = high;
7724 return false;
7727 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7728 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7729 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7730 then MAX_IDX < MIN_IDX.
7731 Returns true if the default label is not needed. */
7733 static bool
7734 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7735 size_t *max_idx)
7737 size_t i, j;
7738 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7739 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7741 if (i == j
7742 && min_take_default
7743 && max_take_default)
7745 /* Only the default case label reached.
7746 Return an empty range. */
7747 *min_idx = 1;
7748 *max_idx = 0;
7749 return false;
7751 else
7753 bool take_default = min_take_default || max_take_default;
7754 tree low, high;
7755 size_t k;
7757 if (max_take_default)
7758 j--;
7760 /* If the case label range is continuous, we do not need
7761 the default case label. Verify that. */
7762 high = CASE_LOW (gimple_switch_label (stmt, i));
7763 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7764 high = CASE_HIGH (gimple_switch_label (stmt, i));
7765 for (k = i + 1; k <= j; ++k)
7767 low = CASE_LOW (gimple_switch_label (stmt, k));
7768 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7770 take_default = true;
7771 break;
7773 high = low;
7774 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7775 high = CASE_HIGH (gimple_switch_label (stmt, k));
7778 *min_idx = i;
7779 *max_idx = j;
7780 return !take_default;
7784 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7785 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7786 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7787 Returns true if the default label is not needed. */
7789 static bool
7790 find_case_label_ranges (gswitch *stmt, value_range_t *vr, size_t *min_idx1,
7791 size_t *max_idx1, size_t *min_idx2,
7792 size_t *max_idx2)
7794 size_t i, j, k, l;
7795 unsigned int n = gimple_switch_num_labels (stmt);
7796 bool take_default;
7797 tree case_low, case_high;
7798 tree min = vr->min, max = vr->max;
7800 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7802 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7804 /* Set second range to emtpy. */
7805 *min_idx2 = 1;
7806 *max_idx2 = 0;
7808 if (vr->type == VR_RANGE)
7810 *min_idx1 = i;
7811 *max_idx1 = j;
7812 return !take_default;
7815 /* Set first range to all case labels. */
7816 *min_idx1 = 1;
7817 *max_idx1 = n - 1;
7819 if (i > j)
7820 return false;
7822 /* Make sure all the values of case labels [i , j] are contained in
7823 range [MIN, MAX]. */
7824 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7825 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7826 if (tree_int_cst_compare (case_low, min) < 0)
7827 i += 1;
7828 if (case_high != NULL_TREE
7829 && tree_int_cst_compare (max, case_high) < 0)
7830 j -= 1;
7832 if (i > j)
7833 return false;
7835 /* If the range spans case labels [i, j], the corresponding anti-range spans
7836 the labels [1, i - 1] and [j + 1, n - 1]. */
7837 k = j + 1;
7838 l = n - 1;
7839 if (k > l)
7841 k = 1;
7842 l = 0;
7845 j = i - 1;
7846 i = 1;
7847 if (i > j)
7849 i = k;
7850 j = l;
7851 k = 1;
7852 l = 0;
7855 *min_idx1 = i;
7856 *max_idx1 = j;
7857 *min_idx2 = k;
7858 *max_idx2 = l;
7859 return false;
7862 /* Visit switch statement STMT. If we can determine which edge
7863 will be taken out of STMT's basic block, record it in
7864 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7865 SSA_PROP_VARYING. */
7867 static enum ssa_prop_result
7868 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7870 tree op, val;
7871 value_range_t *vr;
7872 size_t i = 0, j = 0, k, l;
7873 bool take_default;
7875 *taken_edge_p = NULL;
7876 op = gimple_switch_index (stmt);
7877 if (TREE_CODE (op) != SSA_NAME)
7878 return SSA_PROP_VARYING;
7880 vr = get_value_range (op);
7881 if (dump_file && (dump_flags & TDF_DETAILS))
7883 fprintf (dump_file, "\nVisiting switch expression with operand ");
7884 print_generic_expr (dump_file, op, 0);
7885 fprintf (dump_file, " with known range ");
7886 dump_value_range (dump_file, vr);
7887 fprintf (dump_file, "\n");
7890 if ((vr->type != VR_RANGE
7891 && vr->type != VR_ANTI_RANGE)
7892 || symbolic_range_p (vr))
7893 return SSA_PROP_VARYING;
7895 /* Find the single edge that is taken from the switch expression. */
7896 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7898 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7899 label */
7900 if (j < i)
7902 gcc_assert (take_default);
7903 val = gimple_switch_default_label (stmt);
7905 else
7907 /* Check if labels with index i to j and maybe the default label
7908 are all reaching the same label. */
7910 val = gimple_switch_label (stmt, i);
7911 if (take_default
7912 && CASE_LABEL (gimple_switch_default_label (stmt))
7913 != CASE_LABEL (val))
7915 if (dump_file && (dump_flags & TDF_DETAILS))
7916 fprintf (dump_file, " not a single destination for this "
7917 "range\n");
7918 return SSA_PROP_VARYING;
7920 for (++i; i <= j; ++i)
7922 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7924 if (dump_file && (dump_flags & TDF_DETAILS))
7925 fprintf (dump_file, " not a single destination for this "
7926 "range\n");
7927 return SSA_PROP_VARYING;
7930 for (; k <= l; ++k)
7932 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7934 if (dump_file && (dump_flags & TDF_DETAILS))
7935 fprintf (dump_file, " not a single destination for this "
7936 "range\n");
7937 return SSA_PROP_VARYING;
7942 *taken_edge_p = find_edge (gimple_bb (stmt),
7943 label_to_block (CASE_LABEL (val)));
7945 if (dump_file && (dump_flags & TDF_DETAILS))
7947 fprintf (dump_file, " will take edge to ");
7948 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7951 return SSA_PROP_INTERESTING;
7955 /* Evaluate statement STMT. If the statement produces a useful range,
7956 return SSA_PROP_INTERESTING and record the SSA name with the
7957 interesting range into *OUTPUT_P.
7959 If STMT is a conditional branch and we can determine its truth
7960 value, the taken edge is recorded in *TAKEN_EDGE_P.
7962 If STMT produces a varying value, return SSA_PROP_VARYING. */
7964 static enum ssa_prop_result
7965 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7967 tree def;
7968 ssa_op_iter iter;
7970 if (dump_file && (dump_flags & TDF_DETAILS))
7972 fprintf (dump_file, "\nVisiting statement:\n");
7973 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7976 if (!stmt_interesting_for_vrp (stmt))
7977 gcc_assert (stmt_ends_bb_p (stmt));
7978 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7979 return vrp_visit_assignment_or_call (stmt, output_p);
7980 else if (gimple_code (stmt) == GIMPLE_COND)
7981 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7982 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7983 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7985 /* All other statements produce nothing of interest for VRP, so mark
7986 their outputs varying and prevent further simulation. */
7987 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7988 set_value_range_to_varying (get_value_range (def));
7990 return SSA_PROP_VARYING;
7993 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7994 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7995 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7996 possible such range. The resulting range is not canonicalized. */
7998 static void
7999 union_ranges (enum value_range_type *vr0type,
8000 tree *vr0min, tree *vr0max,
8001 enum value_range_type vr1type,
8002 tree vr1min, tree vr1max)
8004 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8005 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8007 /* [] is vr0, () is vr1 in the following classification comments. */
8008 if (mineq && maxeq)
8010 /* [( )] */
8011 if (*vr0type == vr1type)
8012 /* Nothing to do for equal ranges. */
8014 else if ((*vr0type == VR_RANGE
8015 && vr1type == VR_ANTI_RANGE)
8016 || (*vr0type == VR_ANTI_RANGE
8017 && vr1type == VR_RANGE))
8019 /* For anti-range with range union the result is varying. */
8020 goto give_up;
8022 else
8023 gcc_unreachable ();
8025 else if (operand_less_p (*vr0max, vr1min) == 1
8026 || operand_less_p (vr1max, *vr0min) == 1)
8028 /* [ ] ( ) or ( ) [ ]
8029 If the ranges have an empty intersection, result of the union
8030 operation is the anti-range or if both are anti-ranges
8031 it covers all. */
8032 if (*vr0type == VR_ANTI_RANGE
8033 && vr1type == VR_ANTI_RANGE)
8034 goto give_up;
8035 else if (*vr0type == VR_ANTI_RANGE
8036 && vr1type == VR_RANGE)
8038 else if (*vr0type == VR_RANGE
8039 && vr1type == VR_ANTI_RANGE)
8041 *vr0type = vr1type;
8042 *vr0min = vr1min;
8043 *vr0max = vr1max;
8045 else if (*vr0type == VR_RANGE
8046 && vr1type == VR_RANGE)
8048 /* The result is the convex hull of both ranges. */
8049 if (operand_less_p (*vr0max, vr1min) == 1)
8051 /* If the result can be an anti-range, create one. */
8052 if (TREE_CODE (*vr0max) == INTEGER_CST
8053 && TREE_CODE (vr1min) == INTEGER_CST
8054 && vrp_val_is_min (*vr0min)
8055 && vrp_val_is_max (vr1max))
8057 tree min = int_const_binop (PLUS_EXPR,
8058 *vr0max,
8059 build_int_cst (TREE_TYPE (*vr0max), 1));
8060 tree max = int_const_binop (MINUS_EXPR,
8061 vr1min,
8062 build_int_cst (TREE_TYPE (vr1min), 1));
8063 if (!operand_less_p (max, min))
8065 *vr0type = VR_ANTI_RANGE;
8066 *vr0min = min;
8067 *vr0max = max;
8069 else
8070 *vr0max = vr1max;
8072 else
8073 *vr0max = vr1max;
8075 else
8077 /* If the result can be an anti-range, create one. */
8078 if (TREE_CODE (vr1max) == INTEGER_CST
8079 && TREE_CODE (*vr0min) == INTEGER_CST
8080 && vrp_val_is_min (vr1min)
8081 && vrp_val_is_max (*vr0max))
8083 tree min = int_const_binop (PLUS_EXPR,
8084 vr1max,
8085 build_int_cst (TREE_TYPE (vr1max), 1));
8086 tree max = int_const_binop (MINUS_EXPR,
8087 *vr0min,
8088 build_int_cst (TREE_TYPE (*vr0min), 1));
8089 if (!operand_less_p (max, min))
8091 *vr0type = VR_ANTI_RANGE;
8092 *vr0min = min;
8093 *vr0max = max;
8095 else
8096 *vr0min = vr1min;
8098 else
8099 *vr0min = vr1min;
8102 else
8103 gcc_unreachable ();
8105 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8106 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8108 /* [ ( ) ] or [( ) ] or [ ( )] */
8109 if (*vr0type == VR_RANGE
8110 && vr1type == VR_RANGE)
8112 else if (*vr0type == VR_ANTI_RANGE
8113 && vr1type == VR_ANTI_RANGE)
8115 *vr0type = vr1type;
8116 *vr0min = vr1min;
8117 *vr0max = vr1max;
8119 else if (*vr0type == VR_ANTI_RANGE
8120 && vr1type == VR_RANGE)
8122 /* Arbitrarily choose the right or left gap. */
8123 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8124 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8125 build_int_cst (TREE_TYPE (vr1min), 1));
8126 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8127 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8128 build_int_cst (TREE_TYPE (vr1max), 1));
8129 else
8130 goto give_up;
8132 else if (*vr0type == VR_RANGE
8133 && vr1type == VR_ANTI_RANGE)
8134 /* The result covers everything. */
8135 goto give_up;
8136 else
8137 gcc_unreachable ();
8139 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8140 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8142 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8143 if (*vr0type == VR_RANGE
8144 && vr1type == VR_RANGE)
8146 *vr0type = vr1type;
8147 *vr0min = vr1min;
8148 *vr0max = vr1max;
8150 else if (*vr0type == VR_ANTI_RANGE
8151 && vr1type == VR_ANTI_RANGE)
8153 else if (*vr0type == VR_RANGE
8154 && vr1type == VR_ANTI_RANGE)
8156 *vr0type = VR_ANTI_RANGE;
8157 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8159 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8160 build_int_cst (TREE_TYPE (*vr0min), 1));
8161 *vr0min = vr1min;
8163 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8165 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8166 build_int_cst (TREE_TYPE (*vr0max), 1));
8167 *vr0max = vr1max;
8169 else
8170 goto give_up;
8172 else if (*vr0type == VR_ANTI_RANGE
8173 && vr1type == VR_RANGE)
8174 /* The result covers everything. */
8175 goto give_up;
8176 else
8177 gcc_unreachable ();
8179 else if ((operand_less_p (vr1min, *vr0max) == 1
8180 || operand_equal_p (vr1min, *vr0max, 0))
8181 && operand_less_p (*vr0min, vr1min) == 1
8182 && operand_less_p (*vr0max, vr1max) == 1)
8184 /* [ ( ] ) or [ ]( ) */
8185 if (*vr0type == VR_RANGE
8186 && vr1type == VR_RANGE)
8187 *vr0max = vr1max;
8188 else if (*vr0type == VR_ANTI_RANGE
8189 && vr1type == VR_ANTI_RANGE)
8190 *vr0min = vr1min;
8191 else if (*vr0type == VR_ANTI_RANGE
8192 && vr1type == VR_RANGE)
8194 if (TREE_CODE (vr1min) == INTEGER_CST)
8195 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8196 build_int_cst (TREE_TYPE (vr1min), 1));
8197 else
8198 goto give_up;
8200 else if (*vr0type == VR_RANGE
8201 && vr1type == VR_ANTI_RANGE)
8203 if (TREE_CODE (*vr0max) == INTEGER_CST)
8205 *vr0type = vr1type;
8206 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8207 build_int_cst (TREE_TYPE (*vr0max), 1));
8208 *vr0max = vr1max;
8210 else
8211 goto give_up;
8213 else
8214 gcc_unreachable ();
8216 else if ((operand_less_p (*vr0min, vr1max) == 1
8217 || operand_equal_p (*vr0min, vr1max, 0))
8218 && operand_less_p (vr1min, *vr0min) == 1
8219 && operand_less_p (vr1max, *vr0max) == 1)
8221 /* ( [ ) ] or ( )[ ] */
8222 if (*vr0type == VR_RANGE
8223 && vr1type == VR_RANGE)
8224 *vr0min = vr1min;
8225 else if (*vr0type == VR_ANTI_RANGE
8226 && vr1type == VR_ANTI_RANGE)
8227 *vr0max = vr1max;
8228 else if (*vr0type == VR_ANTI_RANGE
8229 && vr1type == VR_RANGE)
8231 if (TREE_CODE (vr1max) == INTEGER_CST)
8232 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8233 build_int_cst (TREE_TYPE (vr1max), 1));
8234 else
8235 goto give_up;
8237 else if (*vr0type == VR_RANGE
8238 && vr1type == VR_ANTI_RANGE)
8240 if (TREE_CODE (*vr0min) == INTEGER_CST)
8242 *vr0type = vr1type;
8243 *vr0min = vr1min;
8244 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8245 build_int_cst (TREE_TYPE (*vr0min), 1));
8247 else
8248 goto give_up;
8250 else
8251 gcc_unreachable ();
8253 else
8254 goto give_up;
8256 return;
8258 give_up:
8259 *vr0type = VR_VARYING;
8260 *vr0min = NULL_TREE;
8261 *vr0max = NULL_TREE;
8264 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8265 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8266 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8267 possible such range. The resulting range is not canonicalized. */
8269 static void
8270 intersect_ranges (enum value_range_type *vr0type,
8271 tree *vr0min, tree *vr0max,
8272 enum value_range_type vr1type,
8273 tree vr1min, tree vr1max)
8275 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8276 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8278 /* [] is vr0, () is vr1 in the following classification comments. */
8279 if (mineq && maxeq)
8281 /* [( )] */
8282 if (*vr0type == vr1type)
8283 /* Nothing to do for equal ranges. */
8285 else if ((*vr0type == VR_RANGE
8286 && vr1type == VR_ANTI_RANGE)
8287 || (*vr0type == VR_ANTI_RANGE
8288 && vr1type == VR_RANGE))
8290 /* For anti-range with range intersection the result is empty. */
8291 *vr0type = VR_UNDEFINED;
8292 *vr0min = NULL_TREE;
8293 *vr0max = NULL_TREE;
8295 else
8296 gcc_unreachable ();
8298 else if (operand_less_p (*vr0max, vr1min) == 1
8299 || operand_less_p (vr1max, *vr0min) == 1)
8301 /* [ ] ( ) or ( ) [ ]
8302 If the ranges have an empty intersection, the result of the
8303 intersect operation is the range for intersecting an
8304 anti-range with a range or empty when intersecting two ranges. */
8305 if (*vr0type == VR_RANGE
8306 && vr1type == VR_ANTI_RANGE)
8308 else if (*vr0type == VR_ANTI_RANGE
8309 && vr1type == VR_RANGE)
8311 *vr0type = vr1type;
8312 *vr0min = vr1min;
8313 *vr0max = vr1max;
8315 else if (*vr0type == VR_RANGE
8316 && vr1type == VR_RANGE)
8318 *vr0type = VR_UNDEFINED;
8319 *vr0min = NULL_TREE;
8320 *vr0max = NULL_TREE;
8322 else if (*vr0type == VR_ANTI_RANGE
8323 && vr1type == VR_ANTI_RANGE)
8325 /* If the anti-ranges are adjacent to each other merge them. */
8326 if (TREE_CODE (*vr0max) == INTEGER_CST
8327 && TREE_CODE (vr1min) == INTEGER_CST
8328 && operand_less_p (*vr0max, vr1min) == 1
8329 && integer_onep (int_const_binop (MINUS_EXPR,
8330 vr1min, *vr0max)))
8331 *vr0max = vr1max;
8332 else if (TREE_CODE (vr1max) == INTEGER_CST
8333 && TREE_CODE (*vr0min) == INTEGER_CST
8334 && operand_less_p (vr1max, *vr0min) == 1
8335 && integer_onep (int_const_binop (MINUS_EXPR,
8336 *vr0min, vr1max)))
8337 *vr0min = vr1min;
8338 /* Else arbitrarily take VR0. */
8341 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8342 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8344 /* [ ( ) ] or [( ) ] or [ ( )] */
8345 if (*vr0type == VR_RANGE
8346 && vr1type == VR_RANGE)
8348 /* If both are ranges the result is the inner one. */
8349 *vr0type = vr1type;
8350 *vr0min = vr1min;
8351 *vr0max = vr1max;
8353 else if (*vr0type == VR_RANGE
8354 && vr1type == VR_ANTI_RANGE)
8356 /* Choose the right gap if the left one is empty. */
8357 if (mineq)
8359 if (TREE_CODE (vr1max) == INTEGER_CST)
8360 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8361 build_int_cst (TREE_TYPE (vr1max), 1));
8362 else
8363 *vr0min = vr1max;
8365 /* Choose the left gap if the right one is empty. */
8366 else if (maxeq)
8368 if (TREE_CODE (vr1min) == INTEGER_CST)
8369 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8370 build_int_cst (TREE_TYPE (vr1min), 1));
8371 else
8372 *vr0max = vr1min;
8374 /* Choose the anti-range if the range is effectively varying. */
8375 else if (vrp_val_is_min (*vr0min)
8376 && vrp_val_is_max (*vr0max))
8378 *vr0type = vr1type;
8379 *vr0min = vr1min;
8380 *vr0max = vr1max;
8382 /* Else choose the range. */
8384 else if (*vr0type == VR_ANTI_RANGE
8385 && vr1type == VR_ANTI_RANGE)
8386 /* If both are anti-ranges the result is the outer one. */
8388 else if (*vr0type == VR_ANTI_RANGE
8389 && vr1type == VR_RANGE)
8391 /* The intersection is empty. */
8392 *vr0type = VR_UNDEFINED;
8393 *vr0min = NULL_TREE;
8394 *vr0max = NULL_TREE;
8396 else
8397 gcc_unreachable ();
8399 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8400 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8402 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8403 if (*vr0type == VR_RANGE
8404 && vr1type == VR_RANGE)
8405 /* Choose the inner range. */
8407 else if (*vr0type == VR_ANTI_RANGE
8408 && vr1type == VR_RANGE)
8410 /* Choose the right gap if the left is empty. */
8411 if (mineq)
8413 *vr0type = VR_RANGE;
8414 if (TREE_CODE (*vr0max) == INTEGER_CST)
8415 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8416 build_int_cst (TREE_TYPE (*vr0max), 1));
8417 else
8418 *vr0min = *vr0max;
8419 *vr0max = vr1max;
8421 /* Choose the left gap if the right is empty. */
8422 else if (maxeq)
8424 *vr0type = VR_RANGE;
8425 if (TREE_CODE (*vr0min) == INTEGER_CST)
8426 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8427 build_int_cst (TREE_TYPE (*vr0min), 1));
8428 else
8429 *vr0max = *vr0min;
8430 *vr0min = vr1min;
8432 /* Choose the anti-range if the range is effectively varying. */
8433 else if (vrp_val_is_min (vr1min)
8434 && vrp_val_is_max (vr1max))
8436 /* Else choose the range. */
8437 else
8439 *vr0type = vr1type;
8440 *vr0min = vr1min;
8441 *vr0max = vr1max;
8444 else if (*vr0type == VR_ANTI_RANGE
8445 && vr1type == VR_ANTI_RANGE)
8447 /* If both are anti-ranges the result is the outer one. */
8448 *vr0type = vr1type;
8449 *vr0min = vr1min;
8450 *vr0max = vr1max;
8452 else if (vr1type == VR_ANTI_RANGE
8453 && *vr0type == VR_RANGE)
8455 /* The intersection is empty. */
8456 *vr0type = VR_UNDEFINED;
8457 *vr0min = NULL_TREE;
8458 *vr0max = NULL_TREE;
8460 else
8461 gcc_unreachable ();
8463 else if ((operand_less_p (vr1min, *vr0max) == 1
8464 || operand_equal_p (vr1min, *vr0max, 0))
8465 && operand_less_p (*vr0min, vr1min) == 1)
8467 /* [ ( ] ) or [ ]( ) */
8468 if (*vr0type == VR_ANTI_RANGE
8469 && vr1type == VR_ANTI_RANGE)
8470 *vr0max = vr1max;
8471 else if (*vr0type == VR_RANGE
8472 && vr1type == VR_RANGE)
8473 *vr0min = vr1min;
8474 else if (*vr0type == VR_RANGE
8475 && vr1type == VR_ANTI_RANGE)
8477 if (TREE_CODE (vr1min) == INTEGER_CST)
8478 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8479 build_int_cst (TREE_TYPE (vr1min), 1));
8480 else
8481 *vr0max = vr1min;
8483 else if (*vr0type == VR_ANTI_RANGE
8484 && vr1type == VR_RANGE)
8486 *vr0type = VR_RANGE;
8487 if (TREE_CODE (*vr0max) == INTEGER_CST)
8488 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8489 build_int_cst (TREE_TYPE (*vr0max), 1));
8490 else
8491 *vr0min = *vr0max;
8492 *vr0max = vr1max;
8494 else
8495 gcc_unreachable ();
8497 else if ((operand_less_p (*vr0min, vr1max) == 1
8498 || operand_equal_p (*vr0min, vr1max, 0))
8499 && operand_less_p (vr1min, *vr0min) == 1)
8501 /* ( [ ) ] or ( )[ ] */
8502 if (*vr0type == VR_ANTI_RANGE
8503 && vr1type == VR_ANTI_RANGE)
8504 *vr0min = vr1min;
8505 else if (*vr0type == VR_RANGE
8506 && vr1type == VR_RANGE)
8507 *vr0max = vr1max;
8508 else if (*vr0type == VR_RANGE
8509 && vr1type == VR_ANTI_RANGE)
8511 if (TREE_CODE (vr1max) == INTEGER_CST)
8512 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8513 build_int_cst (TREE_TYPE (vr1max), 1));
8514 else
8515 *vr0min = vr1max;
8517 else if (*vr0type == VR_ANTI_RANGE
8518 && vr1type == VR_RANGE)
8520 *vr0type = VR_RANGE;
8521 if (TREE_CODE (*vr0min) == INTEGER_CST)
8522 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8523 build_int_cst (TREE_TYPE (*vr0min), 1));
8524 else
8525 *vr0max = *vr0min;
8526 *vr0min = vr1min;
8528 else
8529 gcc_unreachable ();
8532 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8533 result for the intersection. That's always a conservative
8534 correct estimate. */
8536 return;
8540 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8541 in *VR0. This may not be the smallest possible such range. */
8543 static void
8544 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8546 value_range_t saved;
8548 /* If either range is VR_VARYING the other one wins. */
8549 if (vr1->type == VR_VARYING)
8550 return;
8551 if (vr0->type == VR_VARYING)
8553 copy_value_range (vr0, vr1);
8554 return;
8557 /* When either range is VR_UNDEFINED the resulting range is
8558 VR_UNDEFINED, too. */
8559 if (vr0->type == VR_UNDEFINED)
8560 return;
8561 if (vr1->type == VR_UNDEFINED)
8563 set_value_range_to_undefined (vr0);
8564 return;
8567 /* Save the original vr0 so we can return it as conservative intersection
8568 result when our worker turns things to varying. */
8569 saved = *vr0;
8570 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8571 vr1->type, vr1->min, vr1->max);
8572 /* Make sure to canonicalize the result though as the inversion of a
8573 VR_RANGE can still be a VR_RANGE. */
8574 set_and_canonicalize_value_range (vr0, vr0->type,
8575 vr0->min, vr0->max, vr0->equiv);
8576 /* If that failed, use the saved original VR0. */
8577 if (vr0->type == VR_VARYING)
8579 *vr0 = saved;
8580 return;
8582 /* If the result is VR_UNDEFINED there is no need to mess with
8583 the equivalencies. */
8584 if (vr0->type == VR_UNDEFINED)
8585 return;
8587 /* The resulting set of equivalences for range intersection is the union of
8588 the two sets. */
8589 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8590 bitmap_ior_into (vr0->equiv, vr1->equiv);
8591 else if (vr1->equiv && !vr0->equiv)
8592 bitmap_copy (vr0->equiv, vr1->equiv);
8595 static void
8596 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8598 if (dump_file && (dump_flags & TDF_DETAILS))
8600 fprintf (dump_file, "Intersecting\n ");
8601 dump_value_range (dump_file, vr0);
8602 fprintf (dump_file, "\nand\n ");
8603 dump_value_range (dump_file, vr1);
8604 fprintf (dump_file, "\n");
8606 vrp_intersect_ranges_1 (vr0, vr1);
8607 if (dump_file && (dump_flags & TDF_DETAILS))
8609 fprintf (dump_file, "to\n ");
8610 dump_value_range (dump_file, vr0);
8611 fprintf (dump_file, "\n");
8615 /* Meet operation for value ranges. Given two value ranges VR0 and
8616 VR1, store in VR0 a range that contains both VR0 and VR1. This
8617 may not be the smallest possible such range. */
8619 static void
8620 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8622 value_range_t saved;
8624 if (vr0->type == VR_UNDEFINED)
8626 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8627 return;
8630 if (vr1->type == VR_UNDEFINED)
8632 /* VR0 already has the resulting range. */
8633 return;
8636 if (vr0->type == VR_VARYING)
8638 /* Nothing to do. VR0 already has the resulting range. */
8639 return;
8642 if (vr1->type == VR_VARYING)
8644 set_value_range_to_varying (vr0);
8645 return;
8648 saved = *vr0;
8649 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8650 vr1->type, vr1->min, vr1->max);
8651 if (vr0->type == VR_VARYING)
8653 /* Failed to find an efficient meet. Before giving up and setting
8654 the result to VARYING, see if we can at least derive a useful
8655 anti-range. FIXME, all this nonsense about distinguishing
8656 anti-ranges from ranges is necessary because of the odd
8657 semantics of range_includes_zero_p and friends. */
8658 if (((saved.type == VR_RANGE
8659 && range_includes_zero_p (saved.min, saved.max) == 0)
8660 || (saved.type == VR_ANTI_RANGE
8661 && range_includes_zero_p (saved.min, saved.max) == 1))
8662 && ((vr1->type == VR_RANGE
8663 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8664 || (vr1->type == VR_ANTI_RANGE
8665 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8667 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8669 /* Since this meet operation did not result from the meeting of
8670 two equivalent names, VR0 cannot have any equivalences. */
8671 if (vr0->equiv)
8672 bitmap_clear (vr0->equiv);
8673 return;
8676 set_value_range_to_varying (vr0);
8677 return;
8679 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8680 vr0->equiv);
8681 if (vr0->type == VR_VARYING)
8682 return;
8684 /* The resulting set of equivalences is always the intersection of
8685 the two sets. */
8686 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8687 bitmap_and_into (vr0->equiv, vr1->equiv);
8688 else if (vr0->equiv && !vr1->equiv)
8689 bitmap_clear (vr0->equiv);
8692 static void
8693 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8695 if (dump_file && (dump_flags & TDF_DETAILS))
8697 fprintf (dump_file, "Meeting\n ");
8698 dump_value_range (dump_file, vr0);
8699 fprintf (dump_file, "\nand\n ");
8700 dump_value_range (dump_file, vr1);
8701 fprintf (dump_file, "\n");
8703 vrp_meet_1 (vr0, vr1);
8704 if (dump_file && (dump_flags & TDF_DETAILS))
8706 fprintf (dump_file, "to\n ");
8707 dump_value_range (dump_file, vr0);
8708 fprintf (dump_file, "\n");
8713 /* Visit all arguments for PHI node PHI that flow through executable
8714 edges. If a valid value range can be derived from all the incoming
8715 value ranges, set a new range for the LHS of PHI. */
8717 static enum ssa_prop_result
8718 vrp_visit_phi_node (gphi *phi)
8720 size_t i;
8721 tree lhs = PHI_RESULT (phi);
8722 value_range_t *lhs_vr = get_value_range (lhs);
8723 value_range_t vr_result = VR_INITIALIZER;
8724 bool first = true;
8725 int edges, old_edges;
8726 struct loop *l;
8728 if (dump_file && (dump_flags & TDF_DETAILS))
8730 fprintf (dump_file, "\nVisiting PHI node: ");
8731 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8734 edges = 0;
8735 for (i = 0; i < gimple_phi_num_args (phi); i++)
8737 edge e = gimple_phi_arg_edge (phi, i);
8739 if (dump_file && (dump_flags & TDF_DETAILS))
8741 fprintf (dump_file,
8742 " Argument #%d (%d -> %d %sexecutable)\n",
8743 (int) i, e->src->index, e->dest->index,
8744 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8747 if (e->flags & EDGE_EXECUTABLE)
8749 tree arg = PHI_ARG_DEF (phi, i);
8750 value_range_t vr_arg;
8752 ++edges;
8754 if (TREE_CODE (arg) == SSA_NAME)
8756 vr_arg = *(get_value_range (arg));
8757 /* Do not allow equivalences or symbolic ranges to leak in from
8758 backedges. That creates invalid equivalencies.
8759 See PR53465 and PR54767. */
8760 if (e->flags & EDGE_DFS_BACK)
8762 if (vr_arg.type == VR_RANGE
8763 || vr_arg.type == VR_ANTI_RANGE)
8765 vr_arg.equiv = NULL;
8766 if (symbolic_range_p (&vr_arg))
8768 vr_arg.type = VR_VARYING;
8769 vr_arg.min = NULL_TREE;
8770 vr_arg.max = NULL_TREE;
8774 else
8776 /* If the non-backedge arguments range is VR_VARYING then
8777 we can still try recording a simple equivalence. */
8778 if (vr_arg.type == VR_VARYING)
8780 vr_arg.type = VR_RANGE;
8781 vr_arg.min = arg;
8782 vr_arg.max = arg;
8783 vr_arg.equiv = NULL;
8787 else
8789 if (TREE_OVERFLOW_P (arg))
8790 arg = drop_tree_overflow (arg);
8792 vr_arg.type = VR_RANGE;
8793 vr_arg.min = arg;
8794 vr_arg.max = arg;
8795 vr_arg.equiv = NULL;
8798 if (dump_file && (dump_flags & TDF_DETAILS))
8800 fprintf (dump_file, "\t");
8801 print_generic_expr (dump_file, arg, dump_flags);
8802 fprintf (dump_file, ": ");
8803 dump_value_range (dump_file, &vr_arg);
8804 fprintf (dump_file, "\n");
8807 if (first)
8808 copy_value_range (&vr_result, &vr_arg);
8809 else
8810 vrp_meet (&vr_result, &vr_arg);
8811 first = false;
8813 if (vr_result.type == VR_VARYING)
8814 break;
8818 if (vr_result.type == VR_VARYING)
8819 goto varying;
8820 else if (vr_result.type == VR_UNDEFINED)
8821 goto update_range;
8823 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8824 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8826 /* To prevent infinite iterations in the algorithm, derive ranges
8827 when the new value is slightly bigger or smaller than the
8828 previous one. We don't do this if we have seen a new executable
8829 edge; this helps us avoid an overflow infinity for conditionals
8830 which are not in a loop. If the old value-range was VR_UNDEFINED
8831 use the updated range and iterate one more time. */
8832 if (edges > 0
8833 && gimple_phi_num_args (phi) > 1
8834 && edges == old_edges
8835 && lhs_vr->type != VR_UNDEFINED)
8837 /* Compare old and new ranges, fall back to varying if the
8838 values are not comparable. */
8839 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8840 if (cmp_min == -2)
8841 goto varying;
8842 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8843 if (cmp_max == -2)
8844 goto varying;
8846 /* For non VR_RANGE or for pointers fall back to varying if
8847 the range changed. */
8848 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8849 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8850 && (cmp_min != 0 || cmp_max != 0))
8851 goto varying;
8853 /* If the new minimum is larger than the previous one
8854 retain the old value. If the new minimum value is smaller
8855 than the previous one and not -INF go all the way to -INF + 1.
8856 In the first case, to avoid infinite bouncing between different
8857 minimums, and in the other case to avoid iterating millions of
8858 times to reach -INF. Going to -INF + 1 also lets the following
8859 iteration compute whether there will be any overflow, at the
8860 expense of one additional iteration. */
8861 if (cmp_min < 0)
8862 vr_result.min = lhs_vr->min;
8863 else if (cmp_min > 0
8864 && !vrp_val_is_min (vr_result.min))
8865 vr_result.min
8866 = int_const_binop (PLUS_EXPR,
8867 vrp_val_min (TREE_TYPE (vr_result.min)),
8868 build_int_cst (TREE_TYPE (vr_result.min), 1));
8870 /* Similarly for the maximum value. */
8871 if (cmp_max > 0)
8872 vr_result.max = lhs_vr->max;
8873 else if (cmp_max < 0
8874 && !vrp_val_is_max (vr_result.max))
8875 vr_result.max
8876 = int_const_binop (MINUS_EXPR,
8877 vrp_val_max (TREE_TYPE (vr_result.min)),
8878 build_int_cst (TREE_TYPE (vr_result.min), 1));
8880 /* If we dropped either bound to +-INF then if this is a loop
8881 PHI node SCEV may known more about its value-range. */
8882 if ((cmp_min > 0 || cmp_min < 0
8883 || cmp_max < 0 || cmp_max > 0)
8884 && (l = loop_containing_stmt (phi))
8885 && l->header == gimple_bb (phi))
8886 adjust_range_with_scev (&vr_result, l, phi, lhs);
8888 /* If we will end up with a (-INF, +INF) range, set it to
8889 VARYING. Same if the previous max value was invalid for
8890 the type and we end up with vr_result.min > vr_result.max. */
8891 if ((vrp_val_is_max (vr_result.max)
8892 && vrp_val_is_min (vr_result.min))
8893 || compare_values (vr_result.min,
8894 vr_result.max) > 0)
8895 goto varying;
8898 /* If the new range is different than the previous value, keep
8899 iterating. */
8900 update_range:
8901 if (update_value_range (lhs, &vr_result))
8903 if (dump_file && (dump_flags & TDF_DETAILS))
8905 fprintf (dump_file, "Found new range for ");
8906 print_generic_expr (dump_file, lhs, 0);
8907 fprintf (dump_file, ": ");
8908 dump_value_range (dump_file, &vr_result);
8909 fprintf (dump_file, "\n");
8912 if (vr_result.type == VR_VARYING)
8913 return SSA_PROP_VARYING;
8915 return SSA_PROP_INTERESTING;
8918 /* Nothing changed, don't add outgoing edges. */
8919 return SSA_PROP_NOT_INTERESTING;
8921 /* No match found. Set the LHS to VARYING. */
8922 varying:
8923 set_value_range_to_varying (lhs_vr);
8924 return SSA_PROP_VARYING;
8927 /* Simplify boolean operations if the source is known
8928 to be already a boolean. */
8929 static bool
8930 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8932 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8933 tree lhs, op0, op1;
8934 bool need_conversion;
8936 /* We handle only !=/== case here. */
8937 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8939 op0 = gimple_assign_rhs1 (stmt);
8940 if (!op_with_boolean_value_range_p (op0))
8941 return false;
8943 op1 = gimple_assign_rhs2 (stmt);
8944 if (!op_with_boolean_value_range_p (op1))
8945 return false;
8947 /* Reduce number of cases to handle to NE_EXPR. As there is no
8948 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8949 if (rhs_code == EQ_EXPR)
8951 if (TREE_CODE (op1) == INTEGER_CST)
8952 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8953 build_int_cst (TREE_TYPE (op1), 1));
8954 else
8955 return false;
8958 lhs = gimple_assign_lhs (stmt);
8959 need_conversion
8960 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8962 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8963 if (need_conversion
8964 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8965 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8966 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8967 return false;
8969 /* For A != 0 we can substitute A itself. */
8970 if (integer_zerop (op1))
8971 gimple_assign_set_rhs_with_ops (gsi,
8972 need_conversion
8973 ? NOP_EXPR : TREE_CODE (op0), op0);
8974 /* For A != B we substitute A ^ B. Either with conversion. */
8975 else if (need_conversion)
8977 tree tem = make_ssa_name (TREE_TYPE (op0));
8978 gassign *newop
8979 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
8980 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8981 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
8983 /* Or without. */
8984 else
8985 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8986 update_stmt (gsi_stmt (*gsi));
8988 return true;
8991 /* Simplify a division or modulo operator to a right shift or
8992 bitwise and if the first operand is unsigned or is greater
8993 than zero and the second operand is an exact power of two.
8994 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
8995 into just op0 if op0's range is known to be a subset of
8996 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
8997 modulo. */
8999 static bool
9000 simplify_div_or_mod_using_ranges (gimple stmt)
9002 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9003 tree val = NULL;
9004 tree op0 = gimple_assign_rhs1 (stmt);
9005 tree op1 = gimple_assign_rhs2 (stmt);
9006 value_range_t *vr = get_value_range (op0);
9008 if (rhs_code == TRUNC_MOD_EXPR
9009 && TREE_CODE (op1) == INTEGER_CST
9010 && tree_int_cst_sgn (op1) == 1
9011 && range_int_cst_p (vr)
9012 && tree_int_cst_lt (vr->max, op1))
9014 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9015 || tree_int_cst_sgn (vr->min) >= 0
9016 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9017 vr->min))
9019 /* If op0 already has the range op0 % op1 has,
9020 then TRUNC_MOD_EXPR won't change anything. */
9021 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9022 gimple_assign_set_rhs_from_tree (&gsi, op0);
9023 update_stmt (stmt);
9024 return true;
9028 if (!integer_pow2p (op1))
9029 return false;
9031 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9033 val = integer_one_node;
9035 else
9037 bool sop = false;
9039 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9041 if (val
9042 && sop
9043 && integer_onep (val)
9044 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9046 location_t location;
9048 if (!gimple_has_location (stmt))
9049 location = input_location;
9050 else
9051 location = gimple_location (stmt);
9052 warning_at (location, OPT_Wstrict_overflow,
9053 "assuming signed overflow does not occur when "
9054 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9058 if (val && integer_onep (val))
9060 tree t;
9062 if (rhs_code == TRUNC_DIV_EXPR)
9064 t = build_int_cst (integer_type_node, tree_log2 (op1));
9065 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9066 gimple_assign_set_rhs1 (stmt, op0);
9067 gimple_assign_set_rhs2 (stmt, t);
9069 else
9071 t = build_int_cst (TREE_TYPE (op1), 1);
9072 t = int_const_binop (MINUS_EXPR, op1, t);
9073 t = fold_convert (TREE_TYPE (op0), t);
9075 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9076 gimple_assign_set_rhs1 (stmt, op0);
9077 gimple_assign_set_rhs2 (stmt, t);
9080 update_stmt (stmt);
9081 return true;
9084 return false;
9087 /* Simplify a min or max if the ranges of the two operands are
9088 disjoint. Return true if we do simplify. */
9090 static bool
9091 simplify_min_or_max_using_ranges (gimple stmt)
9093 tree op0 = gimple_assign_rhs1 (stmt);
9094 tree op1 = gimple_assign_rhs2 (stmt);
9095 bool sop = false;
9096 tree val;
9098 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9099 (LE_EXPR, op0, op1, &sop));
9100 if (!val)
9102 sop = false;
9103 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9104 (LT_EXPR, op0, op1, &sop));
9107 if (val)
9109 if (sop && 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 %<min/max (X,Y)%> to %<X%> or %<Y%>");
9122 /* VAL == TRUE -> OP0 < or <= op1
9123 VAL == FALSE -> OP0 > or >= op1. */
9124 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9125 == integer_zerop (val)) ? op0 : op1;
9126 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9127 gimple_assign_set_rhs_from_tree (&gsi, res);
9128 update_stmt (stmt);
9129 return true;
9132 return false;
9135 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9136 ABS_EXPR. If the operand is <= 0, then simplify the
9137 ABS_EXPR into a NEGATE_EXPR. */
9139 static bool
9140 simplify_abs_using_ranges (gimple stmt)
9142 tree op = gimple_assign_rhs1 (stmt);
9143 value_range_t *vr = get_value_range (op);
9145 if (vr)
9147 tree val = NULL;
9148 bool sop = false;
9150 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9151 if (!val)
9153 /* The range is neither <= 0 nor > 0. Now see if it is
9154 either < 0 or >= 0. */
9155 sop = false;
9156 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9157 &sop);
9160 if (val)
9162 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9164 location_t location;
9166 if (!gimple_has_location (stmt))
9167 location = input_location;
9168 else
9169 location = gimple_location (stmt);
9170 warning_at (location, OPT_Wstrict_overflow,
9171 "assuming signed overflow does not occur when "
9172 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9175 gimple_assign_set_rhs1 (stmt, op);
9176 if (integer_zerop (val))
9177 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9178 else
9179 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9180 update_stmt (stmt);
9181 return true;
9185 return false;
9188 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9189 If all the bits that are being cleared by & are already
9190 known to be zero from VR, or all the bits that are being
9191 set by | are already known to be one from VR, the bit
9192 operation is redundant. */
9194 static bool
9195 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9197 tree op0 = gimple_assign_rhs1 (stmt);
9198 tree op1 = gimple_assign_rhs2 (stmt);
9199 tree op = NULL_TREE;
9200 value_range_t vr0 = VR_INITIALIZER;
9201 value_range_t vr1 = VR_INITIALIZER;
9202 wide_int may_be_nonzero0, may_be_nonzero1;
9203 wide_int must_be_nonzero0, must_be_nonzero1;
9204 wide_int mask;
9206 if (TREE_CODE (op0) == SSA_NAME)
9207 vr0 = *(get_value_range (op0));
9208 else if (is_gimple_min_invariant (op0))
9209 set_value_range_to_value (&vr0, op0, NULL);
9210 else
9211 return false;
9213 if (TREE_CODE (op1) == SSA_NAME)
9214 vr1 = *(get_value_range (op1));
9215 else if (is_gimple_min_invariant (op1))
9216 set_value_range_to_value (&vr1, op1, NULL);
9217 else
9218 return false;
9220 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9221 &must_be_nonzero0))
9222 return false;
9223 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9224 &must_be_nonzero1))
9225 return false;
9227 switch (gimple_assign_rhs_code (stmt))
9229 case BIT_AND_EXPR:
9230 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9231 if (mask == 0)
9233 op = op0;
9234 break;
9236 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9237 if (mask == 0)
9239 op = op1;
9240 break;
9242 break;
9243 case BIT_IOR_EXPR:
9244 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9245 if (mask == 0)
9247 op = op1;
9248 break;
9250 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9251 if (mask == 0)
9253 op = op0;
9254 break;
9256 break;
9257 default:
9258 gcc_unreachable ();
9261 if (op == NULL_TREE)
9262 return false;
9264 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9265 update_stmt (gsi_stmt (*gsi));
9266 return true;
9269 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9270 a known value range VR.
9272 If there is one and only one value which will satisfy the
9273 conditional, then return that value. Else return NULL.
9275 If signed overflow must be undefined for the value to satisfy
9276 the conditional, then set *STRICT_OVERFLOW_P to true. */
9278 static tree
9279 test_for_singularity (enum tree_code cond_code, tree op0,
9280 tree op1, value_range_t *vr,
9281 bool *strict_overflow_p)
9283 tree min = NULL;
9284 tree max = NULL;
9286 /* Extract minimum/maximum values which satisfy the
9287 the conditional as it was written. */
9288 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9290 /* This should not be negative infinity; there is no overflow
9291 here. */
9292 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9294 max = op1;
9295 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9297 tree one = build_int_cst (TREE_TYPE (op0), 1);
9298 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9299 if (EXPR_P (max))
9300 TREE_NO_WARNING (max) = 1;
9303 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9305 /* This should not be positive infinity; there is no overflow
9306 here. */
9307 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9309 min = op1;
9310 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9312 tree one = build_int_cst (TREE_TYPE (op0), 1);
9313 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9314 if (EXPR_P (min))
9315 TREE_NO_WARNING (min) = 1;
9319 /* Now refine the minimum and maximum values using any
9320 value range information we have for op0. */
9321 if (min && max)
9323 if (compare_values (vr->min, min) == 1)
9324 min = vr->min;
9325 if (compare_values (vr->max, max) == -1)
9326 max = vr->max;
9328 /* If the new min/max values have converged to a single value,
9329 then there is only one value which can satisfy the condition,
9330 return that value. */
9331 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9333 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9334 && is_overflow_infinity (vr->max))
9335 *strict_overflow_p = true;
9336 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9337 && is_overflow_infinity (vr->min))
9338 *strict_overflow_p = true;
9340 return min;
9343 return NULL;
9346 /* Return whether the value range *VR fits in an integer type specified
9347 by PRECISION and UNSIGNED_P. */
9349 static bool
9350 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9352 tree src_type;
9353 unsigned src_precision;
9354 widest_int tem;
9355 signop src_sgn;
9357 /* We can only handle integral and pointer types. */
9358 src_type = TREE_TYPE (vr->min);
9359 if (!INTEGRAL_TYPE_P (src_type)
9360 && !POINTER_TYPE_P (src_type))
9361 return false;
9363 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9364 and so is an identity transform. */
9365 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9366 src_sgn = TYPE_SIGN (src_type);
9367 if ((src_precision < dest_precision
9368 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9369 || (src_precision == dest_precision && src_sgn == dest_sgn))
9370 return true;
9372 /* Now we can only handle ranges with constant bounds. */
9373 if (vr->type != VR_RANGE
9374 || TREE_CODE (vr->min) != INTEGER_CST
9375 || TREE_CODE (vr->max) != INTEGER_CST)
9376 return false;
9378 /* For sign changes, the MSB of the wide_int has to be clear.
9379 An unsigned value with its MSB set cannot be represented by
9380 a signed wide_int, while a negative value cannot be represented
9381 by an unsigned wide_int. */
9382 if (src_sgn != dest_sgn
9383 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9384 return false;
9386 /* Then we can perform the conversion on both ends and compare
9387 the result for equality. */
9388 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9389 if (tem != wi::to_widest (vr->min))
9390 return false;
9391 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9392 if (tem != wi::to_widest (vr->max))
9393 return false;
9395 return true;
9398 /* Simplify a conditional using a relational operator to an equality
9399 test if the range information indicates only one value can satisfy
9400 the original conditional. */
9402 static bool
9403 simplify_cond_using_ranges (gcond *stmt)
9405 tree op0 = gimple_cond_lhs (stmt);
9406 tree op1 = gimple_cond_rhs (stmt);
9407 enum tree_code cond_code = gimple_cond_code (stmt);
9409 if (cond_code != NE_EXPR
9410 && cond_code != EQ_EXPR
9411 && TREE_CODE (op0) == SSA_NAME
9412 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9413 && is_gimple_min_invariant (op1))
9415 value_range_t *vr = get_value_range (op0);
9417 /* If we have range information for OP0, then we might be
9418 able to simplify this conditional. */
9419 if (vr->type == VR_RANGE)
9421 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9422 bool sop = false;
9423 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9425 if (new_tree
9426 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9428 if (dump_file)
9430 fprintf (dump_file, "Simplified relational ");
9431 print_gimple_stmt (dump_file, stmt, 0, 0);
9432 fprintf (dump_file, " into ");
9435 gimple_cond_set_code (stmt, EQ_EXPR);
9436 gimple_cond_set_lhs (stmt, op0);
9437 gimple_cond_set_rhs (stmt, new_tree);
9439 update_stmt (stmt);
9441 if (dump_file)
9443 print_gimple_stmt (dump_file, stmt, 0, 0);
9444 fprintf (dump_file, "\n");
9447 if (sop && issue_strict_overflow_warning (wc))
9449 location_t location = input_location;
9450 if (gimple_has_location (stmt))
9451 location = gimple_location (stmt);
9453 warning_at (location, OPT_Wstrict_overflow,
9454 "assuming signed overflow does not occur when "
9455 "simplifying conditional");
9458 return true;
9461 /* Try again after inverting the condition. We only deal
9462 with integral types here, so no need to worry about
9463 issues with inverting FP comparisons. */
9464 sop = false;
9465 new_tree = test_for_singularity
9466 (invert_tree_comparison (cond_code, false),
9467 op0, op1, vr, &sop);
9469 if (new_tree
9470 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9472 if (dump_file)
9474 fprintf (dump_file, "Simplified relational ");
9475 print_gimple_stmt (dump_file, stmt, 0, 0);
9476 fprintf (dump_file, " into ");
9479 gimple_cond_set_code (stmt, NE_EXPR);
9480 gimple_cond_set_lhs (stmt, op0);
9481 gimple_cond_set_rhs (stmt, new_tree);
9483 update_stmt (stmt);
9485 if (dump_file)
9487 print_gimple_stmt (dump_file, stmt, 0, 0);
9488 fprintf (dump_file, "\n");
9491 if (sop && issue_strict_overflow_warning (wc))
9493 location_t location = input_location;
9494 if (gimple_has_location (stmt))
9495 location = gimple_location (stmt);
9497 warning_at (location, OPT_Wstrict_overflow,
9498 "assuming signed overflow does not occur when "
9499 "simplifying conditional");
9502 return true;
9507 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9508 see if OP0 was set by a type conversion where the source of
9509 the conversion is another SSA_NAME with a range that fits
9510 into the range of OP0's type.
9512 If so, the conversion is redundant as the earlier SSA_NAME can be
9513 used for the comparison directly if we just massage the constant in the
9514 comparison. */
9515 if (TREE_CODE (op0) == SSA_NAME
9516 && TREE_CODE (op1) == INTEGER_CST)
9518 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9519 tree innerop;
9521 if (!is_gimple_assign (def_stmt)
9522 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9523 return false;
9525 innerop = gimple_assign_rhs1 (def_stmt);
9527 if (TREE_CODE (innerop) == SSA_NAME
9528 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9530 value_range_t *vr = get_value_range (innerop);
9532 if (range_int_cst_p (vr)
9533 && range_fits_type_p (vr,
9534 TYPE_PRECISION (TREE_TYPE (op0)),
9535 TYPE_SIGN (TREE_TYPE (op0)))
9536 && int_fits_type_p (op1, TREE_TYPE (innerop))
9537 /* The range must not have overflowed, or if it did overflow
9538 we must not be wrapping/trapping overflow and optimizing
9539 with strict overflow semantics. */
9540 && ((!is_negative_overflow_infinity (vr->min)
9541 && !is_positive_overflow_infinity (vr->max))
9542 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9544 /* If the range overflowed and the user has asked for warnings
9545 when strict overflow semantics were used to optimize code,
9546 issue an appropriate warning. */
9547 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9548 && (is_negative_overflow_infinity (vr->min)
9549 || is_positive_overflow_infinity (vr->max))
9550 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9552 location_t location;
9554 if (!gimple_has_location (stmt))
9555 location = input_location;
9556 else
9557 location = gimple_location (stmt);
9558 warning_at (location, OPT_Wstrict_overflow,
9559 "assuming signed overflow does not occur when "
9560 "simplifying conditional");
9563 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9564 gimple_cond_set_lhs (stmt, innerop);
9565 gimple_cond_set_rhs (stmt, newconst);
9566 return true;
9571 return false;
9574 /* Simplify a switch statement using the value range of the switch
9575 argument. */
9577 static bool
9578 simplify_switch_using_ranges (gswitch *stmt)
9580 tree op = gimple_switch_index (stmt);
9581 value_range_t *vr;
9582 bool take_default;
9583 edge e;
9584 edge_iterator ei;
9585 size_t i = 0, j = 0, n, n2;
9586 tree vec2;
9587 switch_update su;
9588 size_t k = 1, l = 0;
9590 if (TREE_CODE (op) == SSA_NAME)
9592 vr = get_value_range (op);
9594 /* We can only handle integer ranges. */
9595 if ((vr->type != VR_RANGE
9596 && vr->type != VR_ANTI_RANGE)
9597 || symbolic_range_p (vr))
9598 return false;
9600 /* Find case label for min/max of the value range. */
9601 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9603 else if (TREE_CODE (op) == INTEGER_CST)
9605 take_default = !find_case_label_index (stmt, 1, op, &i);
9606 if (take_default)
9608 i = 1;
9609 j = 0;
9611 else
9613 j = i;
9616 else
9617 return false;
9619 n = gimple_switch_num_labels (stmt);
9621 /* Bail out if this is just all edges taken. */
9622 if (i == 1
9623 && j == n - 1
9624 && take_default)
9625 return false;
9627 /* Build a new vector of taken case labels. */
9628 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9629 n2 = 0;
9631 /* Add the default edge, if necessary. */
9632 if (take_default)
9633 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9635 for (; i <= j; ++i, ++n2)
9636 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9638 for (; k <= l; ++k, ++n2)
9639 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9641 /* Mark needed edges. */
9642 for (i = 0; i < n2; ++i)
9644 e = find_edge (gimple_bb (stmt),
9645 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9646 e->aux = (void *)-1;
9649 /* Queue not needed edges for later removal. */
9650 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9652 if (e->aux == (void *)-1)
9654 e->aux = NULL;
9655 continue;
9658 if (dump_file && (dump_flags & TDF_DETAILS))
9660 fprintf (dump_file, "removing unreachable case label\n");
9662 to_remove_edges.safe_push (e);
9663 e->flags &= ~EDGE_EXECUTABLE;
9666 /* And queue an update for the stmt. */
9667 su.stmt = stmt;
9668 su.vec = vec2;
9669 to_update_switch_stmts.safe_push (su);
9670 return false;
9673 /* Simplify an integral conversion from an SSA name in STMT. */
9675 static bool
9676 simplify_conversion_using_ranges (gimple stmt)
9678 tree innerop, middleop, finaltype;
9679 gimple def_stmt;
9680 value_range_t *innervr;
9681 signop inner_sgn, middle_sgn, final_sgn;
9682 unsigned inner_prec, middle_prec, final_prec;
9683 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9685 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9686 if (!INTEGRAL_TYPE_P (finaltype))
9687 return false;
9688 middleop = gimple_assign_rhs1 (stmt);
9689 def_stmt = SSA_NAME_DEF_STMT (middleop);
9690 if (!is_gimple_assign (def_stmt)
9691 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9692 return false;
9693 innerop = gimple_assign_rhs1 (def_stmt);
9694 if (TREE_CODE (innerop) != SSA_NAME
9695 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9696 return false;
9698 /* Get the value-range of the inner operand. */
9699 innervr = get_value_range (innerop);
9700 if (innervr->type != VR_RANGE
9701 || TREE_CODE (innervr->min) != INTEGER_CST
9702 || TREE_CODE (innervr->max) != INTEGER_CST)
9703 return false;
9705 /* Simulate the conversion chain to check if the result is equal if
9706 the middle conversion is removed. */
9707 innermin = wi::to_widest (innervr->min);
9708 innermax = wi::to_widest (innervr->max);
9710 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9711 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9712 final_prec = TYPE_PRECISION (finaltype);
9714 /* If the first conversion is not injective, the second must not
9715 be widening. */
9716 if (wi::gtu_p (innermax - innermin,
9717 wi::mask <widest_int> (middle_prec, false))
9718 && middle_prec < final_prec)
9719 return false;
9720 /* We also want a medium value so that we can track the effect that
9721 narrowing conversions with sign change have. */
9722 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9723 if (inner_sgn == UNSIGNED)
9724 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9725 else
9726 innermed = 0;
9727 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9728 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9729 innermed = innermin;
9731 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9732 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9733 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9734 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9736 /* Require that the final conversion applied to both the original
9737 and the intermediate range produces the same result. */
9738 final_sgn = TYPE_SIGN (finaltype);
9739 if (wi::ext (middlemin, final_prec, final_sgn)
9740 != wi::ext (innermin, final_prec, final_sgn)
9741 || wi::ext (middlemed, final_prec, final_sgn)
9742 != wi::ext (innermed, final_prec, final_sgn)
9743 || wi::ext (middlemax, final_prec, final_sgn)
9744 != wi::ext (innermax, final_prec, final_sgn))
9745 return false;
9747 gimple_assign_set_rhs1 (stmt, innerop);
9748 update_stmt (stmt);
9749 return true;
9752 /* Simplify a conversion from integral SSA name to float in STMT. */
9754 static bool
9755 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9757 tree rhs1 = gimple_assign_rhs1 (stmt);
9758 value_range_t *vr = get_value_range (rhs1);
9759 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9760 machine_mode mode;
9761 tree tem;
9762 gassign *conv;
9764 /* We can only handle constant ranges. */
9765 if (vr->type != VR_RANGE
9766 || TREE_CODE (vr->min) != INTEGER_CST
9767 || TREE_CODE (vr->max) != INTEGER_CST)
9768 return false;
9770 /* First check if we can use a signed type in place of an unsigned. */
9771 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9772 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9773 != CODE_FOR_nothing)
9774 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9775 mode = TYPE_MODE (TREE_TYPE (rhs1));
9776 /* If we can do the conversion in the current input mode do nothing. */
9777 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9778 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9779 return false;
9780 /* Otherwise search for a mode we can use, starting from the narrowest
9781 integer mode available. */
9782 else
9784 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9787 /* If we cannot do a signed conversion to float from mode
9788 or if the value-range does not fit in the signed type
9789 try with a wider mode. */
9790 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9791 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9792 break;
9794 mode = GET_MODE_WIDER_MODE (mode);
9795 /* But do not widen the input. Instead leave that to the
9796 optabs expansion code. */
9797 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9798 return false;
9800 while (mode != VOIDmode);
9801 if (mode == VOIDmode)
9802 return false;
9805 /* It works, insert a truncation or sign-change before the
9806 float conversion. */
9807 tem = make_ssa_name (build_nonstandard_integer_type
9808 (GET_MODE_PRECISION (mode), 0));
9809 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9810 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9811 gimple_assign_set_rhs1 (stmt, tem);
9812 update_stmt (stmt);
9814 return true;
9817 /* Simplify an internal fn call using ranges if possible. */
9819 static bool
9820 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9822 enum tree_code subcode;
9823 bool is_ubsan = false;
9824 bool ovf = false;
9825 switch (gimple_call_internal_fn (stmt))
9827 case IFN_UBSAN_CHECK_ADD:
9828 subcode = PLUS_EXPR;
9829 is_ubsan = true;
9830 break;
9831 case IFN_UBSAN_CHECK_SUB:
9832 subcode = MINUS_EXPR;
9833 is_ubsan = true;
9834 break;
9835 case IFN_UBSAN_CHECK_MUL:
9836 subcode = MULT_EXPR;
9837 is_ubsan = true;
9838 break;
9839 case IFN_ADD_OVERFLOW:
9840 subcode = PLUS_EXPR;
9841 break;
9842 case IFN_SUB_OVERFLOW:
9843 subcode = MINUS_EXPR;
9844 break;
9845 case IFN_MUL_OVERFLOW:
9846 subcode = MULT_EXPR;
9847 break;
9848 default:
9849 return false;
9852 tree op0 = gimple_call_arg (stmt, 0);
9853 tree op1 = gimple_call_arg (stmt, 1);
9854 tree type;
9855 if (is_ubsan)
9856 type = TREE_TYPE (op0);
9857 else if (gimple_call_lhs (stmt) == NULL_TREE)
9858 return false;
9859 else
9860 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9861 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9862 || (is_ubsan && ovf))
9863 return false;
9865 gimple g;
9866 location_t loc = gimple_location (stmt);
9867 if (is_ubsan)
9868 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9869 else
9871 int prec = TYPE_PRECISION (type);
9872 tree utype = type;
9873 if (ovf
9874 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9875 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9876 utype = build_nonstandard_integer_type (prec, 1);
9877 if (TREE_CODE (op0) == INTEGER_CST)
9878 op0 = fold_convert (utype, op0);
9879 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9881 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9882 gimple_set_location (g, loc);
9883 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9884 op0 = gimple_assign_lhs (g);
9886 if (TREE_CODE (op1) == INTEGER_CST)
9887 op1 = fold_convert (utype, op1);
9888 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9890 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9891 gimple_set_location (g, loc);
9892 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9893 op1 = gimple_assign_lhs (g);
9895 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9896 gimple_set_location (g, loc);
9897 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9898 if (utype != type)
9900 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9901 gimple_assign_lhs (g));
9902 gimple_set_location (g, loc);
9903 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9905 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9906 gimple_assign_lhs (g),
9907 build_int_cst (type, ovf));
9909 gimple_set_location (g, loc);
9910 gsi_replace (gsi, g, false);
9911 return true;
9914 /* Simplify STMT using ranges if possible. */
9916 static bool
9917 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9919 gimple stmt = gsi_stmt (*gsi);
9920 if (is_gimple_assign (stmt))
9922 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9923 tree rhs1 = gimple_assign_rhs1 (stmt);
9925 switch (rhs_code)
9927 case EQ_EXPR:
9928 case NE_EXPR:
9929 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9930 if the RHS is zero or one, and the LHS are known to be boolean
9931 values. */
9932 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9933 return simplify_truth_ops_using_ranges (gsi, stmt);
9934 break;
9936 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9937 and BIT_AND_EXPR respectively if the first operand is greater
9938 than zero and the second operand is an exact power of two.
9939 Also optimize TRUNC_MOD_EXPR away if the second operand is
9940 constant and the first operand already has the right value
9941 range. */
9942 case TRUNC_DIV_EXPR:
9943 case TRUNC_MOD_EXPR:
9944 if (TREE_CODE (rhs1) == SSA_NAME
9945 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9946 return simplify_div_or_mod_using_ranges (stmt);
9947 break;
9949 /* Transform ABS (X) into X or -X as appropriate. */
9950 case ABS_EXPR:
9951 if (TREE_CODE (rhs1) == SSA_NAME
9952 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9953 return simplify_abs_using_ranges (stmt);
9954 break;
9956 case BIT_AND_EXPR:
9957 case BIT_IOR_EXPR:
9958 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9959 if all the bits being cleared are already cleared or
9960 all the bits being set are already set. */
9961 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9962 return simplify_bit_ops_using_ranges (gsi, stmt);
9963 break;
9965 CASE_CONVERT:
9966 if (TREE_CODE (rhs1) == SSA_NAME
9967 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9968 return simplify_conversion_using_ranges (stmt);
9969 break;
9971 case FLOAT_EXPR:
9972 if (TREE_CODE (rhs1) == SSA_NAME
9973 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9974 return simplify_float_conversion_using_ranges (gsi, stmt);
9975 break;
9977 case MIN_EXPR:
9978 case MAX_EXPR:
9979 return simplify_min_or_max_using_ranges (stmt);
9980 break;
9982 default:
9983 break;
9986 else if (gimple_code (stmt) == GIMPLE_COND)
9987 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9988 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9989 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
9990 else if (is_gimple_call (stmt)
9991 && gimple_call_internal_p (stmt))
9992 return simplify_internal_call_using_ranges (gsi, stmt);
9994 return false;
9997 /* If the statement pointed by SI has a predicate whose value can be
9998 computed using the value range information computed by VRP, compute
9999 its value and return true. Otherwise, return false. */
10001 static bool
10002 fold_predicate_in (gimple_stmt_iterator *si)
10004 bool assignment_p = false;
10005 tree val;
10006 gimple stmt = gsi_stmt (*si);
10008 if (is_gimple_assign (stmt)
10009 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10011 assignment_p = true;
10012 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10013 gimple_assign_rhs1 (stmt),
10014 gimple_assign_rhs2 (stmt),
10015 stmt);
10017 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10018 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10019 gimple_cond_lhs (cond_stmt),
10020 gimple_cond_rhs (cond_stmt),
10021 stmt);
10022 else
10023 return false;
10025 if (val)
10027 if (assignment_p)
10028 val = fold_convert (gimple_expr_type (stmt), val);
10030 if (dump_file)
10032 fprintf (dump_file, "Folding predicate ");
10033 print_gimple_expr (dump_file, stmt, 0, 0);
10034 fprintf (dump_file, " to ");
10035 print_generic_expr (dump_file, val, 0);
10036 fprintf (dump_file, "\n");
10039 if (is_gimple_assign (stmt))
10040 gimple_assign_set_rhs_from_tree (si, val);
10041 else
10043 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10044 gcond *cond_stmt = as_a <gcond *> (stmt);
10045 if (integer_zerop (val))
10046 gimple_cond_make_false (cond_stmt);
10047 else if (integer_onep (val))
10048 gimple_cond_make_true (cond_stmt);
10049 else
10050 gcc_unreachable ();
10053 return true;
10056 return false;
10059 /* Callback for substitute_and_fold folding the stmt at *SI. */
10061 static bool
10062 vrp_fold_stmt (gimple_stmt_iterator *si)
10064 if (fold_predicate_in (si))
10065 return true;
10067 return simplify_stmt_using_ranges (si);
10070 /* Unwindable const/copy equivalences. */
10071 const_and_copies *equiv_stack;
10073 /* A trivial wrapper so that we can present the generic jump threading
10074 code with a simple API for simplifying statements. STMT is the
10075 statement we want to simplify, WITHIN_STMT provides the location
10076 for any overflow warnings. */
10078 static tree
10079 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
10081 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10082 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10083 gimple_cond_lhs (cond_stmt),
10084 gimple_cond_rhs (cond_stmt),
10085 within_stmt);
10087 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10089 value_range_t new_vr = VR_INITIALIZER;
10090 tree lhs = gimple_assign_lhs (assign_stmt);
10092 if (TREE_CODE (lhs) == SSA_NAME
10093 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10094 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10096 extract_range_from_assignment (&new_vr, assign_stmt);
10097 if (range_int_cst_singleton_p (&new_vr))
10098 return new_vr.min;
10102 return NULL_TREE;
10105 /* Blocks which have more than one predecessor and more than
10106 one successor present jump threading opportunities, i.e.,
10107 when the block is reached from a specific predecessor, we
10108 may be able to determine which of the outgoing edges will
10109 be traversed. When this optimization applies, we are able
10110 to avoid conditionals at runtime and we may expose secondary
10111 optimization opportunities.
10113 This routine is effectively a driver for the generic jump
10114 threading code. It basically just presents the generic code
10115 with edges that may be suitable for jump threading.
10117 Unlike DOM, we do not iterate VRP if jump threading was successful.
10118 While iterating may expose new opportunities for VRP, it is expected
10119 those opportunities would be very limited and the compile time cost
10120 to expose those opportunities would be significant.
10122 As jump threading opportunities are discovered, they are registered
10123 for later realization. */
10125 static void
10126 identify_jump_threads (void)
10128 basic_block bb;
10129 gcond *dummy;
10130 int i;
10131 edge e;
10133 /* Ugh. When substituting values earlier in this pass we can
10134 wipe the dominance information. So rebuild the dominator
10135 information as we need it within the jump threading code. */
10136 calculate_dominance_info (CDI_DOMINATORS);
10138 /* We do not allow VRP information to be used for jump threading
10139 across a back edge in the CFG. Otherwise it becomes too
10140 difficult to avoid eliminating loop exit tests. Of course
10141 EDGE_DFS_BACK is not accurate at this time so we have to
10142 recompute it. */
10143 mark_dfs_back_edges ();
10145 /* Do not thread across edges we are about to remove. Just marking
10146 them as EDGE_DFS_BACK will do. */
10147 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10148 e->flags |= EDGE_DFS_BACK;
10150 /* Allocate our unwinder stack to unwind any temporary equivalences
10151 that might be recorded. */
10152 equiv_stack = new const_and_copies (dump_file, dump_flags);
10154 /* To avoid lots of silly node creation, we create a single
10155 conditional and just modify it in-place when attempting to
10156 thread jumps. */
10157 dummy = gimple_build_cond (EQ_EXPR,
10158 integer_zero_node, integer_zero_node,
10159 NULL, NULL);
10161 /* Walk through all the blocks finding those which present a
10162 potential jump threading opportunity. We could set this up
10163 as a dominator walker and record data during the walk, but
10164 I doubt it's worth the effort for the classes of jump
10165 threading opportunities we are trying to identify at this
10166 point in compilation. */
10167 FOR_EACH_BB_FN (bb, cfun)
10169 gimple last;
10171 /* If the generic jump threading code does not find this block
10172 interesting, then there is nothing to do. */
10173 if (! potentially_threadable_block (bb))
10174 continue;
10176 last = last_stmt (bb);
10178 /* We're basically looking for a switch or any kind of conditional with
10179 integral or pointer type arguments. Note the type of the second
10180 argument will be the same as the first argument, so no need to
10181 check it explicitly.
10183 We also handle the case where there are no statements in the
10184 block. This come up with forwarder blocks that are not
10185 optimized away because they lead to a loop header. But we do
10186 want to thread through them as we can sometimes thread to the
10187 loop exit which is obviously profitable. */
10188 if (!last
10189 || gimple_code (last) == GIMPLE_SWITCH
10190 || (gimple_code (last) == GIMPLE_COND
10191 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10192 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10193 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10194 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10195 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10197 edge_iterator ei;
10199 /* We've got a block with multiple predecessors and multiple
10200 successors which also ends in a suitable conditional or
10201 switch statement. For each predecessor, see if we can thread
10202 it to a specific successor. */
10203 FOR_EACH_EDGE (e, ei, bb->preds)
10205 /* Do not thread across back edges or abnormal edges
10206 in the CFG. */
10207 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
10208 continue;
10210 thread_across_edge (dummy, e, true, equiv_stack,
10211 simplify_stmt_for_jump_threading);
10216 /* We do not actually update the CFG or SSA graphs at this point as
10217 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10218 handle ASSERT_EXPRs gracefully. */
10221 /* We identified all the jump threading opportunities earlier, but could
10222 not transform the CFG at that time. This routine transforms the
10223 CFG and arranges for the dominator tree to be rebuilt if necessary.
10225 Note the SSA graph update will occur during the normal TODO
10226 processing by the pass manager. */
10227 static void
10228 finalize_jump_threads (void)
10230 thread_through_all_blocks (false);
10231 delete equiv_stack;
10235 /* Traverse all the blocks folding conditionals with known ranges. */
10237 static void
10238 vrp_finalize (void)
10240 size_t i;
10242 values_propagated = true;
10244 if (dump_file)
10246 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10247 dump_all_value_ranges (dump_file);
10248 fprintf (dump_file, "\n");
10251 substitute_and_fold (op_with_constant_singleton_value_range,
10252 vrp_fold_stmt, false);
10254 if (warn_array_bounds && first_pass_instance)
10255 check_all_array_refs ();
10257 /* We must identify jump threading opportunities before we release
10258 the datastructures built by VRP. */
10259 identify_jump_threads ();
10261 /* Set value range to non pointer SSA_NAMEs. */
10262 for (i = 0; i < num_vr_values; i++)
10263 if (vr_value[i])
10265 tree name = ssa_name (i);
10267 if (!name
10268 || POINTER_TYPE_P (TREE_TYPE (name))
10269 || (vr_value[i]->type == VR_VARYING)
10270 || (vr_value[i]->type == VR_UNDEFINED))
10271 continue;
10273 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10274 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10275 && (vr_value[i]->type == VR_RANGE
10276 || vr_value[i]->type == VR_ANTI_RANGE))
10277 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10278 vr_value[i]->max);
10281 /* Free allocated memory. */
10282 for (i = 0; i < num_vr_values; i++)
10283 if (vr_value[i])
10285 BITMAP_FREE (vr_value[i]->equiv);
10286 free (vr_value[i]);
10289 free (vr_value);
10290 free (vr_phi_edge_counts);
10292 /* So that we can distinguish between VRP data being available
10293 and not available. */
10294 vr_value = NULL;
10295 vr_phi_edge_counts = NULL;
10299 /* Main entry point to VRP (Value Range Propagation). This pass is
10300 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10301 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10302 Programming Language Design and Implementation, pp. 67-78, 1995.
10303 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10305 This is essentially an SSA-CCP pass modified to deal with ranges
10306 instead of constants.
10308 While propagating ranges, we may find that two or more SSA name
10309 have equivalent, though distinct ranges. For instance,
10311 1 x_9 = p_3->a;
10312 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10313 3 if (p_4 == q_2)
10314 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10315 5 endif
10316 6 if (q_2)
10318 In the code above, pointer p_5 has range [q_2, q_2], but from the
10319 code we can also determine that p_5 cannot be NULL and, if q_2 had
10320 a non-varying range, p_5's range should also be compatible with it.
10322 These equivalences are created by two expressions: ASSERT_EXPR and
10323 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10324 result of another assertion, then we can use the fact that p_5 and
10325 p_4 are equivalent when evaluating p_5's range.
10327 Together with value ranges, we also propagate these equivalences
10328 between names so that we can take advantage of information from
10329 multiple ranges when doing final replacement. Note that this
10330 equivalency relation is transitive but not symmetric.
10332 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10333 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10334 in contexts where that assertion does not hold (e.g., in line 6).
10336 TODO, the main difference between this pass and Patterson's is that
10337 we do not propagate edge probabilities. We only compute whether
10338 edges can be taken or not. That is, instead of having a spectrum
10339 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10340 DON'T KNOW. In the future, it may be worthwhile to propagate
10341 probabilities to aid branch prediction. */
10343 static unsigned int
10344 execute_vrp (void)
10346 int i;
10347 edge e;
10348 switch_update *su;
10350 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10351 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10352 scev_initialize ();
10354 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10355 Inserting assertions may split edges which will invalidate
10356 EDGE_DFS_BACK. */
10357 insert_range_assertions ();
10359 to_remove_edges.create (10);
10360 to_update_switch_stmts.create (5);
10361 threadedge_initialize_values ();
10363 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10364 mark_dfs_back_edges ();
10366 vrp_initialize ();
10367 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10368 vrp_finalize ();
10370 free_numbers_of_iterations_estimates ();
10372 /* ASSERT_EXPRs must be removed before finalizing jump threads
10373 as finalizing jump threads calls the CFG cleanup code which
10374 does not properly handle ASSERT_EXPRs. */
10375 remove_range_assertions ();
10377 /* If we exposed any new variables, go ahead and put them into
10378 SSA form now, before we handle jump threading. This simplifies
10379 interactions between rewriting of _DECL nodes into SSA form
10380 and rewriting SSA_NAME nodes into SSA form after block
10381 duplication and CFG manipulation. */
10382 update_ssa (TODO_update_ssa);
10384 finalize_jump_threads ();
10386 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10387 CFG in a broken state and requires a cfg_cleanup run. */
10388 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10389 remove_edge (e);
10390 /* Update SWITCH_EXPR case label vector. */
10391 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10393 size_t j;
10394 size_t n = TREE_VEC_LENGTH (su->vec);
10395 tree label;
10396 gimple_switch_set_num_labels (su->stmt, n);
10397 for (j = 0; j < n; j++)
10398 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10399 /* As we may have replaced the default label with a regular one
10400 make sure to make it a real default label again. This ensures
10401 optimal expansion. */
10402 label = gimple_switch_label (su->stmt, 0);
10403 CASE_LOW (label) = NULL_TREE;
10404 CASE_HIGH (label) = NULL_TREE;
10407 if (to_remove_edges.length () > 0)
10409 free_dominance_info (CDI_DOMINATORS);
10410 loops_state_set (LOOPS_NEED_FIXUP);
10413 to_remove_edges.release ();
10414 to_update_switch_stmts.release ();
10415 threadedge_finalize_values ();
10417 scev_finalize ();
10418 loop_optimizer_finalize ();
10419 return 0;
10422 namespace {
10424 const pass_data pass_data_vrp =
10426 GIMPLE_PASS, /* type */
10427 "vrp", /* name */
10428 OPTGROUP_NONE, /* optinfo_flags */
10429 TV_TREE_VRP, /* tv_id */
10430 PROP_ssa, /* properties_required */
10431 0, /* properties_provided */
10432 0, /* properties_destroyed */
10433 0, /* todo_flags_start */
10434 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10437 class pass_vrp : public gimple_opt_pass
10439 public:
10440 pass_vrp (gcc::context *ctxt)
10441 : gimple_opt_pass (pass_data_vrp, ctxt)
10444 /* opt_pass methods: */
10445 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10446 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10447 virtual unsigned int execute (function *) { return execute_vrp (); }
10449 }; // class pass_vrp
10451 } // anon namespace
10453 gimple_opt_pass *
10454 make_pass_vrp (gcc::context *ctxt)
10456 return new pass_vrp (ctxt);