PR rtl-optimization/84682
[official-gcc.git] / gcc / tree-vrp.c
blob625e65b1184c86963f36f0c66a9e56311c99e101
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2018 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 "insn-codes.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "tree-pass.h"
31 #include "ssa.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
35 #include "flags.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "cfganal.h"
40 #include "gimple-fold.h"
41 #include "tree-eh.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
44 #include "tree-cfg.h"
45 #include "tree-dfa.h"
46 #include "tree-ssa-loop-manip.h"
47 #include "tree-ssa-loop-niter.h"
48 #include "tree-ssa-loop.h"
49 #include "tree-into-ssa.h"
50 #include "tree-ssa.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 "tree-ssa-scopedtables.h"
58 #include "tree-ssa-threadedge.h"
59 #include "omp-general.h"
60 #include "target.h"
61 #include "case-cfn-macros.h"
62 #include "params.h"
63 #include "alloc-pool.h"
64 #include "domwalk.h"
65 #include "tree-cfgcleanup.h"
66 #include "stringpool.h"
67 #include "attribs.h"
68 #include "vr-values.h"
69 #include "builtins.h"
71 /* Set of SSA names found live during the RPO traversal of the function
72 for still active basic-blocks. */
73 static sbitmap *live;
75 /* Return true if the SSA name NAME is live on the edge E. */
77 static bool
78 live_on_edge (edge e, tree name)
80 return (live[e->dest->index]
81 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
84 /* Location information for ASSERT_EXPRs. Each instance of this
85 structure describes an ASSERT_EXPR for an SSA name. Since a single
86 SSA name may have more than one assertion associated with it, these
87 locations are kept in a linked list attached to the corresponding
88 SSA name. */
89 struct assert_locus
91 /* Basic block where the assertion would be inserted. */
92 basic_block bb;
94 /* Some assertions need to be inserted on an edge (e.g., assertions
95 generated by COND_EXPRs). In those cases, BB will be NULL. */
96 edge e;
98 /* Pointer to the statement that generated this assertion. */
99 gimple_stmt_iterator si;
101 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
102 enum tree_code comp_code;
104 /* Value being compared against. */
105 tree val;
107 /* Expression to compare. */
108 tree expr;
110 /* Next node in the linked list. */
111 assert_locus *next;
114 /* If bit I is present, it means that SSA name N_i has a list of
115 assertions that should be inserted in the IL. */
116 static bitmap need_assert_for;
118 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
119 holds a list of ASSERT_LOCUS_T nodes that describe where
120 ASSERT_EXPRs for SSA name N_I should be inserted. */
121 static assert_locus **asserts_for;
123 vec<edge> to_remove_edges;
124 vec<switch_update> to_update_switch_stmts;
127 /* Return the maximum value for TYPE. */
129 tree
130 vrp_val_max (const_tree type)
132 if (!INTEGRAL_TYPE_P (type))
133 return NULL_TREE;
135 return TYPE_MAX_VALUE (type);
138 /* Return the minimum value for TYPE. */
140 tree
141 vrp_val_min (const_tree type)
143 if (!INTEGRAL_TYPE_P (type))
144 return NULL_TREE;
146 return TYPE_MIN_VALUE (type);
149 /* Return whether VAL is equal to the maximum value of its type.
150 We can't do a simple equality comparison with TYPE_MAX_VALUE because
151 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
152 is not == to the integer constant with the same value in the type. */
154 bool
155 vrp_val_is_max (const_tree val)
157 tree type_max = vrp_val_max (TREE_TYPE (val));
158 return (val == type_max
159 || (type_max != NULL_TREE
160 && operand_equal_p (val, type_max, 0)));
163 /* Return whether VAL is equal to the minimum value of its type. */
165 bool
166 vrp_val_is_min (const_tree val)
168 tree type_min = vrp_val_min (TREE_TYPE (val));
169 return (val == type_min
170 || (type_min != NULL_TREE
171 && operand_equal_p (val, type_min, 0)));
174 /* VR_TYPE describes a range with mininum value *MIN and maximum
175 value *MAX. Restrict the range to the set of values that have
176 no bits set outside NONZERO_BITS. Update *MIN and *MAX and
177 return the new range type.
179 SGN gives the sign of the values described by the range. */
181 enum value_range_type
182 intersect_range_with_nonzero_bits (enum value_range_type vr_type,
183 wide_int *min, wide_int *max,
184 const wide_int &nonzero_bits,
185 signop sgn)
187 if (vr_type == VR_ANTI_RANGE)
189 /* The VR_ANTI_RANGE is equivalent to the union of the ranges
190 A: [-INF, *MIN) and B: (*MAX, +INF]. First use NONZERO_BITS
191 to create an inclusive upper bound for A and an inclusive lower
192 bound for B. */
193 wide_int a_max = wi::round_down_for_mask (*min - 1, nonzero_bits);
194 wide_int b_min = wi::round_up_for_mask (*max + 1, nonzero_bits);
196 /* If the calculation of A_MAX wrapped, A is effectively empty
197 and A_MAX is the highest value that satisfies NONZERO_BITS.
198 Likewise if the calculation of B_MIN wrapped, B is effectively
199 empty and B_MIN is the lowest value that satisfies NONZERO_BITS. */
200 bool a_empty = wi::ge_p (a_max, *min, sgn);
201 bool b_empty = wi::le_p (b_min, *max, sgn);
203 /* If both A and B are empty, there are no valid values. */
204 if (a_empty && b_empty)
205 return VR_UNDEFINED;
207 /* If exactly one of A or B is empty, return a VR_RANGE for the
208 other one. */
209 if (a_empty || b_empty)
211 *min = b_min;
212 *max = a_max;
213 gcc_checking_assert (wi::le_p (*min, *max, sgn));
214 return VR_RANGE;
217 /* Update the VR_ANTI_RANGE bounds. */
218 *min = a_max + 1;
219 *max = b_min - 1;
220 gcc_checking_assert (wi::le_p (*min, *max, sgn));
222 /* Now check whether the excluded range includes any values that
223 satisfy NONZERO_BITS. If not, switch to a full VR_RANGE. */
224 if (wi::round_up_for_mask (*min, nonzero_bits) == b_min)
226 unsigned int precision = min->get_precision ();
227 *min = wi::min_value (precision, sgn);
228 *max = wi::max_value (precision, sgn);
229 vr_type = VR_RANGE;
232 if (vr_type == VR_RANGE)
234 *max = wi::round_down_for_mask (*max, nonzero_bits);
236 /* Check that the range contains at least one valid value. */
237 if (wi::gt_p (*min, *max, sgn))
238 return VR_UNDEFINED;
240 *min = wi::round_up_for_mask (*min, nonzero_bits);
241 gcc_checking_assert (wi::le_p (*min, *max, sgn));
243 return vr_type;
246 /* Set value range VR to VR_UNDEFINED. */
248 static inline void
249 set_value_range_to_undefined (value_range *vr)
251 vr->type = VR_UNDEFINED;
252 vr->min = vr->max = NULL_TREE;
253 if (vr->equiv)
254 bitmap_clear (vr->equiv);
257 /* Set value range VR to VR_VARYING. */
259 void
260 set_value_range_to_varying (value_range *vr)
262 vr->type = VR_VARYING;
263 vr->min = vr->max = NULL_TREE;
264 if (vr->equiv)
265 bitmap_clear (vr->equiv);
268 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
270 void
271 set_value_range (value_range *vr, enum value_range_type t, tree min,
272 tree max, bitmap equiv)
274 /* Check the validity of the range. */
275 if (flag_checking
276 && (t == VR_RANGE || t == VR_ANTI_RANGE))
278 int cmp;
280 gcc_assert (min && max);
282 gcc_assert (!TREE_OVERFLOW_P (min) && !TREE_OVERFLOW_P (max));
284 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
285 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
287 cmp = compare_values (min, max);
288 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
291 if (flag_checking
292 && (t == VR_UNDEFINED || t == VR_VARYING))
294 gcc_assert (min == NULL_TREE && max == NULL_TREE);
295 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
298 vr->type = t;
299 vr->min = min;
300 vr->max = max;
302 /* Since updating the equivalence set involves deep copying the
303 bitmaps, only do it if absolutely necessary.
305 All equivalence bitmaps are allocated from the same obstack. So
306 we can use the obstack associated with EQUIV to allocate vr->equiv. */
307 if (vr->equiv == NULL
308 && equiv != NULL)
309 vr->equiv = BITMAP_ALLOC (equiv->obstack);
311 if (equiv != vr->equiv)
313 if (equiv && !bitmap_empty_p (equiv))
314 bitmap_copy (vr->equiv, equiv);
315 else
316 bitmap_clear (vr->equiv);
321 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
322 This means adjusting T, MIN and MAX representing the case of a
323 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
324 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
325 In corner cases where MAX+1 or MIN-1 wraps this will fall back
326 to varying.
327 This routine exists to ease canonicalization in the case where we
328 extract ranges from var + CST op limit. */
330 void
331 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
332 tree min, tree max, bitmap equiv)
334 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
335 if (t == VR_UNDEFINED)
337 set_value_range_to_undefined (vr);
338 return;
340 else if (t == VR_VARYING)
342 set_value_range_to_varying (vr);
343 return;
346 /* Nothing to canonicalize for symbolic ranges. */
347 if (TREE_CODE (min) != INTEGER_CST
348 || TREE_CODE (max) != INTEGER_CST)
350 set_value_range (vr, t, min, max, equiv);
351 return;
354 /* Wrong order for min and max, to swap them and the VR type we need
355 to adjust them. */
356 if (tree_int_cst_lt (max, min))
358 tree one, tmp;
360 /* For one bit precision if max < min, then the swapped
361 range covers all values, so for VR_RANGE it is varying and
362 for VR_ANTI_RANGE empty range, so drop to varying as well. */
363 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
365 set_value_range_to_varying (vr);
366 return;
369 one = build_int_cst (TREE_TYPE (min), 1);
370 tmp = int_const_binop (PLUS_EXPR, max, one);
371 max = int_const_binop (MINUS_EXPR, min, one);
372 min = tmp;
374 /* There's one corner case, if we had [C+1, C] before we now have
375 that again. But this represents an empty value range, so drop
376 to varying in this case. */
377 if (tree_int_cst_lt (max, min))
379 set_value_range_to_varying (vr);
380 return;
383 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
386 /* Anti-ranges that can be represented as ranges should be so. */
387 if (t == VR_ANTI_RANGE)
389 bool is_min = vrp_val_is_min (min);
390 bool is_max = vrp_val_is_max (max);
392 if (is_min && is_max)
394 /* We cannot deal with empty ranges, drop to varying.
395 ??? This could be VR_UNDEFINED instead. */
396 set_value_range_to_varying (vr);
397 return;
399 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
400 && (is_min || is_max))
402 /* Non-empty boolean ranges can always be represented
403 as a singleton range. */
404 if (is_min)
405 min = max = vrp_val_max (TREE_TYPE (min));
406 else
407 min = max = vrp_val_min (TREE_TYPE (min));
408 t = VR_RANGE;
410 else if (is_min
411 /* As a special exception preserve non-null ranges. */
412 && !(TYPE_UNSIGNED (TREE_TYPE (min))
413 && integer_zerop (max)))
415 tree one = build_int_cst (TREE_TYPE (max), 1);
416 min = int_const_binop (PLUS_EXPR, max, one);
417 max = vrp_val_max (TREE_TYPE (max));
418 t = VR_RANGE;
420 else if (is_max)
422 tree one = build_int_cst (TREE_TYPE (min), 1);
423 max = int_const_binop (MINUS_EXPR, min, one);
424 min = vrp_val_min (TREE_TYPE (min));
425 t = VR_RANGE;
429 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
430 to make sure VRP iteration terminates, otherwise we can get into
431 oscillations. */
433 set_value_range (vr, t, min, max, equiv);
436 /* Copy value range FROM into value range TO. */
438 void
439 copy_value_range (value_range *to, value_range *from)
441 set_value_range (to, from->type, from->min, from->max, from->equiv);
444 /* Set value range VR to a single value. This function is only called
445 with values we get from statements, and exists to clear the
446 TREE_OVERFLOW flag. */
448 void
449 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
451 gcc_assert (is_gimple_min_invariant (val));
452 if (TREE_OVERFLOW_P (val))
453 val = drop_tree_overflow (val);
454 set_value_range (vr, VR_RANGE, val, val, equiv);
457 /* Set value range VR to a non-NULL range of type TYPE. */
459 void
460 set_value_range_to_nonnull (value_range *vr, tree type)
462 tree zero = build_int_cst (type, 0);
463 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
467 /* Set value range VR to a NULL range of type TYPE. */
469 void
470 set_value_range_to_null (value_range *vr, tree type)
472 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
476 /* If abs (min) < abs (max), set VR to [-max, max], if
477 abs (min) >= abs (max), set VR to [-min, min]. */
479 static void
480 abs_extent_range (value_range *vr, tree min, tree max)
482 int cmp;
484 gcc_assert (TREE_CODE (min) == INTEGER_CST);
485 gcc_assert (TREE_CODE (max) == INTEGER_CST);
486 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
487 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
488 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
489 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
490 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
492 set_value_range_to_varying (vr);
493 return;
495 cmp = compare_values (min, max);
496 if (cmp == -1)
497 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
498 else if (cmp == 0 || cmp == 1)
500 max = min;
501 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
503 else
505 set_value_range_to_varying (vr);
506 return;
508 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
511 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
513 bool
514 vrp_operand_equal_p (const_tree val1, const_tree val2)
516 if (val1 == val2)
517 return true;
518 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
519 return false;
520 return true;
523 /* Return true, if the bitmaps B1 and B2 are equal. */
525 bool
526 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
528 return (b1 == b2
529 || ((!b1 || bitmap_empty_p (b1))
530 && (!b2 || bitmap_empty_p (b2)))
531 || (b1 && b2
532 && bitmap_equal_p (b1, b2)));
535 /* Return true if VR is ~[0, 0]. */
537 bool
538 range_is_nonnull (value_range *vr)
540 return vr->type == VR_ANTI_RANGE
541 && integer_zerop (vr->min)
542 && integer_zerop (vr->max);
546 /* Return true if VR is [0, 0]. */
548 static inline bool
549 range_is_null (value_range *vr)
551 return vr->type == VR_RANGE
552 && integer_zerop (vr->min)
553 && integer_zerop (vr->max);
556 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
557 a singleton. */
559 bool
560 range_int_cst_p (value_range *vr)
562 return (vr->type == VR_RANGE
563 && TREE_CODE (vr->max) == INTEGER_CST
564 && TREE_CODE (vr->min) == INTEGER_CST);
567 /* Return true if VR is a INTEGER_CST singleton. */
569 bool
570 range_int_cst_singleton_p (value_range *vr)
572 return (range_int_cst_p (vr)
573 && tree_int_cst_equal (vr->min, vr->max));
576 /* Return true if value range VR involves at least one symbol. */
578 bool
579 symbolic_range_p (value_range *vr)
581 return (!is_gimple_min_invariant (vr->min)
582 || !is_gimple_min_invariant (vr->max));
585 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
586 otherwise. We only handle additive operations and set NEG to true if the
587 symbol is negated and INV to the invariant part, if any. */
589 tree
590 get_single_symbol (tree t, bool *neg, tree *inv)
592 bool neg_;
593 tree inv_;
595 *inv = NULL_TREE;
596 *neg = false;
598 if (TREE_CODE (t) == PLUS_EXPR
599 || TREE_CODE (t) == POINTER_PLUS_EXPR
600 || TREE_CODE (t) == MINUS_EXPR)
602 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
604 neg_ = (TREE_CODE (t) == MINUS_EXPR);
605 inv_ = TREE_OPERAND (t, 0);
606 t = TREE_OPERAND (t, 1);
608 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
610 neg_ = false;
611 inv_ = TREE_OPERAND (t, 1);
612 t = TREE_OPERAND (t, 0);
614 else
615 return NULL_TREE;
617 else
619 neg_ = false;
620 inv_ = NULL_TREE;
623 if (TREE_CODE (t) == NEGATE_EXPR)
625 t = TREE_OPERAND (t, 0);
626 neg_ = !neg_;
629 if (TREE_CODE (t) != SSA_NAME)
630 return NULL_TREE;
632 if (inv_ && TREE_OVERFLOW_P (inv_))
633 inv_ = drop_tree_overflow (inv_);
635 *neg = neg_;
636 *inv = inv_;
637 return t;
640 /* The reverse operation: build a symbolic expression with TYPE
641 from symbol SYM, negated according to NEG, and invariant INV. */
643 static tree
644 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
646 const bool pointer_p = POINTER_TYPE_P (type);
647 tree t = sym;
649 if (neg)
650 t = build1 (NEGATE_EXPR, type, t);
652 if (integer_zerop (inv))
653 return t;
655 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
658 /* Return
659 1 if VAL < VAL2
660 0 if !(VAL < VAL2)
661 -2 if those are incomparable. */
663 operand_less_p (tree val, tree val2)
665 /* LT is folded faster than GE and others. Inline the common case. */
666 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
667 return tree_int_cst_lt (val, val2);
668 else
670 tree tcmp;
672 fold_defer_overflow_warnings ();
674 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
676 fold_undefer_and_ignore_overflow_warnings ();
678 if (!tcmp
679 || TREE_CODE (tcmp) != INTEGER_CST)
680 return -2;
682 if (!integer_zerop (tcmp))
683 return 1;
686 return 0;
689 /* Compare two values VAL1 and VAL2. Return
691 -2 if VAL1 and VAL2 cannot be compared at compile-time,
692 -1 if VAL1 < VAL2,
693 0 if VAL1 == VAL2,
694 +1 if VAL1 > VAL2, and
695 +2 if VAL1 != VAL2
697 This is similar to tree_int_cst_compare but supports pointer values
698 and values that cannot be compared at compile time.
700 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
701 true if the return value is only valid if we assume that signed
702 overflow is undefined. */
705 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
707 if (val1 == val2)
708 return 0;
710 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
711 both integers. */
712 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
713 == POINTER_TYPE_P (TREE_TYPE (val2)));
715 /* Convert the two values into the same type. This is needed because
716 sizetype causes sign extension even for unsigned types. */
717 val2 = fold_convert (TREE_TYPE (val1), val2);
718 STRIP_USELESS_TYPE_CONVERSION (val2);
720 const bool overflow_undefined
721 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
722 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
723 tree inv1, inv2;
724 bool neg1, neg2;
725 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
726 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
728 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
729 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
730 if (sym1 && sym2)
732 /* Both values must use the same name with the same sign. */
733 if (sym1 != sym2 || neg1 != neg2)
734 return -2;
736 /* [-]NAME + CST == [-]NAME + CST. */
737 if (inv1 == inv2)
738 return 0;
740 /* If overflow is defined we cannot simplify more. */
741 if (!overflow_undefined)
742 return -2;
744 if (strict_overflow_p != NULL
745 /* Symbolic range building sets TREE_NO_WARNING to declare
746 that overflow doesn't happen. */
747 && (!inv1 || !TREE_NO_WARNING (val1))
748 && (!inv2 || !TREE_NO_WARNING (val2)))
749 *strict_overflow_p = true;
751 if (!inv1)
752 inv1 = build_int_cst (TREE_TYPE (val1), 0);
753 if (!inv2)
754 inv2 = build_int_cst (TREE_TYPE (val2), 0);
756 return wi::cmp (wi::to_wide (inv1), wi::to_wide (inv2),
757 TYPE_SIGN (TREE_TYPE (val1)));
760 const bool cst1 = is_gimple_min_invariant (val1);
761 const bool cst2 = is_gimple_min_invariant (val2);
763 /* If one is of the form '[-]NAME + CST' and the other is constant, then
764 it might be possible to say something depending on the constants. */
765 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
767 if (!overflow_undefined)
768 return -2;
770 if (strict_overflow_p != NULL
771 /* Symbolic range building sets TREE_NO_WARNING to declare
772 that overflow doesn't happen. */
773 && (!sym1 || !TREE_NO_WARNING (val1))
774 && (!sym2 || !TREE_NO_WARNING (val2)))
775 *strict_overflow_p = true;
777 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
778 tree cst = cst1 ? val1 : val2;
779 tree inv = cst1 ? inv2 : inv1;
781 /* Compute the difference between the constants. If it overflows or
782 underflows, this means that we can trivially compare the NAME with
783 it and, consequently, the two values with each other. */
784 wide_int diff = wi::to_wide (cst) - wi::to_wide (inv);
785 if (wi::cmp (0, wi::to_wide (inv), sgn)
786 != wi::cmp (diff, wi::to_wide (cst), sgn))
788 const int res = wi::cmp (wi::to_wide (cst), wi::to_wide (inv), sgn);
789 return cst1 ? res : -res;
792 return -2;
795 /* We cannot say anything more for non-constants. */
796 if (!cst1 || !cst2)
797 return -2;
799 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
801 /* We cannot compare overflowed values. */
802 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
803 return -2;
805 if (TREE_CODE (val1) == INTEGER_CST
806 && TREE_CODE (val2) == INTEGER_CST)
807 return tree_int_cst_compare (val1, val2);
809 if (poly_int_tree_p (val1) && poly_int_tree_p (val2))
811 if (known_eq (wi::to_poly_widest (val1),
812 wi::to_poly_widest (val2)))
813 return 0;
814 if (known_lt (wi::to_poly_widest (val1),
815 wi::to_poly_widest (val2)))
816 return -1;
817 if (known_gt (wi::to_poly_widest (val1),
818 wi::to_poly_widest (val2)))
819 return 1;
822 return -2;
824 else
826 tree t;
828 /* First see if VAL1 and VAL2 are not the same. */
829 if (val1 == val2 || operand_equal_p (val1, val2, 0))
830 return 0;
832 /* If VAL1 is a lower address than VAL2, return -1. */
833 if (operand_less_p (val1, val2) == 1)
834 return -1;
836 /* If VAL1 is a higher address than VAL2, return +1. */
837 if (operand_less_p (val2, val1) == 1)
838 return 1;
840 /* If VAL1 is different than VAL2, return +2.
841 For integer constants we either have already returned -1 or 1
842 or they are equivalent. We still might succeed in proving
843 something about non-trivial operands. */
844 if (TREE_CODE (val1) != INTEGER_CST
845 || TREE_CODE (val2) != INTEGER_CST)
847 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
848 if (t && integer_onep (t))
849 return 2;
852 return -2;
856 /* Compare values like compare_values_warnv. */
859 compare_values (tree val1, tree val2)
861 bool sop;
862 return compare_values_warnv (val1, val2, &sop);
866 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
867 0 if VAL is not inside [MIN, MAX],
868 -2 if we cannot tell either way.
870 Benchmark compile/20001226-1.c compilation time after changing this
871 function. */
874 value_inside_range (tree val, tree min, tree max)
876 int cmp1, cmp2;
878 cmp1 = operand_less_p (val, min);
879 if (cmp1 == -2)
880 return -2;
881 if (cmp1 == 1)
882 return 0;
884 cmp2 = operand_less_p (max, val);
885 if (cmp2 == -2)
886 return -2;
888 return !cmp2;
892 /* Return true if value ranges VR0 and VR1 have a non-empty
893 intersection.
895 Benchmark compile/20001226-1.c compilation time after changing this
896 function.
899 static inline bool
900 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
902 /* The value ranges do not intersect if the maximum of the first range is
903 less than the minimum of the second range or vice versa.
904 When those relations are unknown, we can't do any better. */
905 if (operand_less_p (vr0->max, vr1->min) != 0)
906 return false;
907 if (operand_less_p (vr1->max, vr0->min) != 0)
908 return false;
909 return true;
913 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
914 include the value zero, -2 if we cannot tell. */
917 range_includes_zero_p (tree min, tree max)
919 tree zero = build_int_cst (TREE_TYPE (min), 0);
920 return value_inside_range (zero, min, max);
923 /* Return true if *VR is know to only contain nonnegative values. */
925 static inline bool
926 value_range_nonnegative_p (value_range *vr)
928 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
929 which would return a useful value should be encoded as a
930 VR_RANGE. */
931 if (vr->type == VR_RANGE)
933 int result = compare_values (vr->min, integer_zero_node);
934 return (result == 0 || result == 1);
937 return false;
940 /* If *VR has a value rante that is a single constant value return that,
941 otherwise return NULL_TREE. */
943 tree
944 value_range_constant_singleton (value_range *vr)
946 if (vr->type == VR_RANGE
947 && vrp_operand_equal_p (vr->min, vr->max)
948 && is_gimple_min_invariant (vr->min))
949 return vr->min;
951 return NULL_TREE;
954 /* Wrapper around int_const_binop. Return true if we can compute the
955 result; i.e. if the operation doesn't overflow or if the overflow is
956 undefined. In the latter case (if the operation overflows and
957 overflow is undefined), then adjust the result to be -INF or +INF
958 depending on CODE, VAL1 and VAL2. Return the value in *RES.
960 Return false for division by zero, for which the result is
961 indeterminate. */
963 static bool
964 vrp_int_const_binop (enum tree_code code, tree val1, tree val2, wide_int *res)
966 bool overflow = false;
967 signop sign = TYPE_SIGN (TREE_TYPE (val1));
969 switch (code)
971 case RSHIFT_EXPR:
972 case LSHIFT_EXPR:
974 wide_int wval2 = wi::to_wide (val2, TYPE_PRECISION (TREE_TYPE (val1)));
975 if (wi::neg_p (wval2))
977 wval2 = -wval2;
978 if (code == RSHIFT_EXPR)
979 code = LSHIFT_EXPR;
980 else
981 code = RSHIFT_EXPR;
984 if (code == RSHIFT_EXPR)
985 /* It's unclear from the C standard whether shifts can overflow.
986 The following code ignores overflow; perhaps a C standard
987 interpretation ruling is needed. */
988 *res = wi::rshift (wi::to_wide (val1), wval2, sign);
989 else
990 *res = wi::lshift (wi::to_wide (val1), wval2);
991 break;
994 case MULT_EXPR:
995 *res = wi::mul (wi::to_wide (val1),
996 wi::to_wide (val2), sign, &overflow);
997 break;
999 case TRUNC_DIV_EXPR:
1000 case EXACT_DIV_EXPR:
1001 if (val2 == 0)
1002 return false;
1003 else
1004 *res = wi::div_trunc (wi::to_wide (val1),
1005 wi::to_wide (val2), sign, &overflow);
1006 break;
1008 case FLOOR_DIV_EXPR:
1009 if (val2 == 0)
1010 return false;
1011 *res = wi::div_floor (wi::to_wide (val1),
1012 wi::to_wide (val2), sign, &overflow);
1013 break;
1015 case CEIL_DIV_EXPR:
1016 if (val2 == 0)
1017 return false;
1018 *res = wi::div_ceil (wi::to_wide (val1),
1019 wi::to_wide (val2), sign, &overflow);
1020 break;
1022 case ROUND_DIV_EXPR:
1023 if (val2 == 0)
1024 return false;
1025 *res = wi::div_round (wi::to_wide (val1),
1026 wi::to_wide (val2), sign, &overflow);
1027 break;
1029 default:
1030 gcc_unreachable ();
1033 if (overflow
1034 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1036 /* If the operation overflowed return -INF or +INF depending
1037 on the operation and the combination of signs of the operands. */
1038 int sgn1 = tree_int_cst_sgn (val1);
1039 int sgn2 = tree_int_cst_sgn (val2);
1041 /* Notice that we only need to handle the restricted set of
1042 operations handled by extract_range_from_binary_expr.
1043 Among them, only multiplication, addition and subtraction
1044 can yield overflow without overflown operands because we
1045 are working with integral types only... except in the
1046 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1047 for division too. */
1049 /* For multiplication, the sign of the overflow is given
1050 by the comparison of the signs of the operands. */
1051 if ((code == MULT_EXPR && sgn1 == sgn2)
1052 /* For addition, the operands must be of the same sign
1053 to yield an overflow. Its sign is therefore that
1054 of one of the operands, for example the first. */
1055 || (code == PLUS_EXPR && sgn1 >= 0)
1056 /* For subtraction, operands must be of
1057 different signs to yield an overflow. Its sign is
1058 therefore that of the first operand or the opposite of
1059 that of the second operand. A first operand of 0 counts
1060 as positive here, for the corner case 0 - (-INF), which
1061 overflows, but must yield +INF. */
1062 || (code == MINUS_EXPR && sgn1 >= 0)
1063 /* For division, the only case is -INF / -1 = +INF. */
1064 || code == TRUNC_DIV_EXPR
1065 || code == FLOOR_DIV_EXPR
1066 || code == CEIL_DIV_EXPR
1067 || code == EXACT_DIV_EXPR
1068 || code == ROUND_DIV_EXPR)
1069 *res = wi::max_value (TYPE_PRECISION (TREE_TYPE (val1)),
1070 TYPE_SIGN (TREE_TYPE (val1)));
1071 else
1072 *res = wi::min_value (TYPE_PRECISION (TREE_TYPE (val1)),
1073 TYPE_SIGN (TREE_TYPE (val1)));
1074 return true;
1077 return !overflow;
1081 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1082 bitmask if some bit is unset, it means for all numbers in the range
1083 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1084 bitmask if some bit is set, it means for all numbers in the range
1085 the bit is 1, otherwise it might be 0 or 1. */
1087 bool
1088 zero_nonzero_bits_from_vr (const tree expr_type,
1089 value_range *vr,
1090 wide_int *may_be_nonzero,
1091 wide_int *must_be_nonzero)
1093 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1094 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1095 if (!range_int_cst_p (vr))
1096 return false;
1098 if (range_int_cst_singleton_p (vr))
1100 *may_be_nonzero = wi::to_wide (vr->min);
1101 *must_be_nonzero = *may_be_nonzero;
1103 else if (tree_int_cst_sgn (vr->min) >= 0
1104 || tree_int_cst_sgn (vr->max) < 0)
1106 wide_int xor_mask = wi::to_wide (vr->min) ^ wi::to_wide (vr->max);
1107 *may_be_nonzero = wi::to_wide (vr->min) | wi::to_wide (vr->max);
1108 *must_be_nonzero = wi::to_wide (vr->min) & wi::to_wide (vr->max);
1109 if (xor_mask != 0)
1111 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1112 may_be_nonzero->get_precision ());
1113 *may_be_nonzero = *may_be_nonzero | mask;
1114 *must_be_nonzero = wi::bit_and_not (*must_be_nonzero, mask);
1118 return true;
1121 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1122 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1123 false otherwise. If *AR can be represented with a single range
1124 *VR1 will be VR_UNDEFINED. */
1126 static bool
1127 ranges_from_anti_range (value_range *ar,
1128 value_range *vr0, value_range *vr1)
1130 tree type = TREE_TYPE (ar->min);
1132 vr0->type = VR_UNDEFINED;
1133 vr1->type = VR_UNDEFINED;
1135 if (ar->type != VR_ANTI_RANGE
1136 || TREE_CODE (ar->min) != INTEGER_CST
1137 || TREE_CODE (ar->max) != INTEGER_CST
1138 || !vrp_val_min (type)
1139 || !vrp_val_max (type))
1140 return false;
1142 if (!vrp_val_is_min (ar->min))
1144 vr0->type = VR_RANGE;
1145 vr0->min = vrp_val_min (type);
1146 vr0->max = wide_int_to_tree (type, wi::to_wide (ar->min) - 1);
1148 if (!vrp_val_is_max (ar->max))
1150 vr1->type = VR_RANGE;
1151 vr1->min = wide_int_to_tree (type, wi::to_wide (ar->max) + 1);
1152 vr1->max = vrp_val_max (type);
1154 if (vr0->type == VR_UNDEFINED)
1156 *vr0 = *vr1;
1157 vr1->type = VR_UNDEFINED;
1160 return vr0->type != VR_UNDEFINED;
1163 /* Helper to extract a value-range *VR for a multiplicative operation
1164 *VR0 CODE *VR1. */
1166 static void
1167 extract_range_from_multiplicative_op_1 (value_range *vr,
1168 enum tree_code code,
1169 value_range *vr0, value_range *vr1)
1171 enum value_range_type rtype;
1172 wide_int val, min, max;
1173 tree type;
1175 /* Multiplications, divisions and shifts are a bit tricky to handle,
1176 depending on the mix of signs we have in the two ranges, we
1177 need to operate on different values to get the minimum and
1178 maximum values for the new range. One approach is to figure
1179 out all the variations of range combinations and do the
1180 operations.
1182 However, this involves several calls to compare_values and it
1183 is pretty convoluted. It's simpler to do the 4 operations
1184 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1185 MAX1) and then figure the smallest and largest values to form
1186 the new range. */
1187 gcc_assert (code == MULT_EXPR
1188 || code == TRUNC_DIV_EXPR
1189 || code == FLOOR_DIV_EXPR
1190 || code == CEIL_DIV_EXPR
1191 || code == EXACT_DIV_EXPR
1192 || code == ROUND_DIV_EXPR
1193 || code == RSHIFT_EXPR
1194 || code == LSHIFT_EXPR);
1195 gcc_assert (vr0->type == VR_RANGE
1196 && vr0->type == vr1->type);
1198 rtype = vr0->type;
1199 type = TREE_TYPE (vr0->min);
1200 signop sgn = TYPE_SIGN (type);
1202 /* Compute the 4 cross operations and their minimum and maximum value. */
1203 if (!vrp_int_const_binop (code, vr0->min, vr1->min, &val))
1205 set_value_range_to_varying (vr);
1206 return;
1208 min = max = val;
1210 if (vr1->max != vr1->min)
1212 if (!vrp_int_const_binop (code, vr0->min, vr1->max, &val))
1214 set_value_range_to_varying (vr);
1215 return;
1217 if (wi::lt_p (val, min, sgn))
1218 min = val;
1219 else if (wi::gt_p (val, max, sgn))
1220 max = val;
1223 if (vr0->max != vr0->min)
1225 if (!vrp_int_const_binop (code, vr0->max, vr1->min, &val))
1227 set_value_range_to_varying (vr);
1228 return;
1230 if (wi::lt_p (val, min, sgn))
1231 min = val;
1232 else if (wi::gt_p (val, max, sgn))
1233 max = val;
1236 if (vr0->min != vr0->max && vr1->min != vr1->max)
1238 if (!vrp_int_const_binop (code, vr0->max, vr1->max, &val))
1240 set_value_range_to_varying (vr);
1241 return;
1243 if (wi::lt_p (val, min, sgn))
1244 min = val;
1245 else if (wi::gt_p (val, max, sgn))
1246 max = val;
1249 /* If the new range has its limits swapped around (MIN > MAX),
1250 then the operation caused one of them to wrap around, mark
1251 the new range VARYING. */
1252 if (wi::gt_p (min, max, sgn))
1254 set_value_range_to_varying (vr);
1255 return;
1258 /* We punt for [-INF, +INF].
1259 We learn nothing when we have INF on both sides.
1260 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
1261 if (wi::eq_p (min, wi::min_value (TYPE_PRECISION (type), sgn))
1262 && wi::eq_p (max, wi::max_value (TYPE_PRECISION (type), sgn)))
1264 set_value_range_to_varying (vr);
1265 return;
1268 set_value_range (vr, rtype,
1269 wide_int_to_tree (type, min),
1270 wide_int_to_tree (type, max), NULL);
1273 /* Extract range information from a binary operation CODE based on
1274 the ranges of each of its operands *VR0 and *VR1 with resulting
1275 type EXPR_TYPE. The resulting range is stored in *VR. */
1277 void
1278 extract_range_from_binary_expr_1 (value_range *vr,
1279 enum tree_code code, tree expr_type,
1280 value_range *vr0_, value_range *vr1_)
1282 value_range vr0 = *vr0_, vr1 = *vr1_;
1283 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
1284 enum value_range_type type;
1285 tree min = NULL_TREE, max = NULL_TREE;
1286 int cmp;
1288 if (!INTEGRAL_TYPE_P (expr_type)
1289 && !POINTER_TYPE_P (expr_type))
1291 set_value_range_to_varying (vr);
1292 return;
1295 /* Not all binary expressions can be applied to ranges in a
1296 meaningful way. Handle only arithmetic operations. */
1297 if (code != PLUS_EXPR
1298 && code != MINUS_EXPR
1299 && code != POINTER_PLUS_EXPR
1300 && code != MULT_EXPR
1301 && code != TRUNC_DIV_EXPR
1302 && code != FLOOR_DIV_EXPR
1303 && code != CEIL_DIV_EXPR
1304 && code != EXACT_DIV_EXPR
1305 && code != ROUND_DIV_EXPR
1306 && code != TRUNC_MOD_EXPR
1307 && code != RSHIFT_EXPR
1308 && code != LSHIFT_EXPR
1309 && code != MIN_EXPR
1310 && code != MAX_EXPR
1311 && code != BIT_AND_EXPR
1312 && code != BIT_IOR_EXPR
1313 && code != BIT_XOR_EXPR)
1315 set_value_range_to_varying (vr);
1316 return;
1319 /* If both ranges are UNDEFINED, so is the result. */
1320 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
1322 set_value_range_to_undefined (vr);
1323 return;
1325 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
1326 code. At some point we may want to special-case operations that
1327 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
1328 operand. */
1329 else if (vr0.type == VR_UNDEFINED)
1330 set_value_range_to_varying (&vr0);
1331 else if (vr1.type == VR_UNDEFINED)
1332 set_value_range_to_varying (&vr1);
1334 /* We get imprecise results from ranges_from_anti_range when
1335 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
1336 range, but then we also need to hack up vrp_meet. It's just
1337 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
1338 if (code == EXACT_DIV_EXPR
1339 && vr0.type == VR_ANTI_RANGE
1340 && vr0.min == vr0.max
1341 && integer_zerop (vr0.min))
1343 set_value_range_to_nonnull (vr, expr_type);
1344 return;
1347 /* Now canonicalize anti-ranges to ranges when they are not symbolic
1348 and express ~[] op X as ([]' op X) U ([]'' op X). */
1349 if (vr0.type == VR_ANTI_RANGE
1350 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
1352 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
1353 if (vrtem1.type != VR_UNDEFINED)
1355 value_range vrres = VR_INITIALIZER;
1356 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
1357 &vrtem1, vr1_);
1358 vrp_meet (vr, &vrres);
1360 return;
1362 /* Likewise for X op ~[]. */
1363 if (vr1.type == VR_ANTI_RANGE
1364 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
1366 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
1367 if (vrtem1.type != VR_UNDEFINED)
1369 value_range vrres = VR_INITIALIZER;
1370 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
1371 vr0_, &vrtem1);
1372 vrp_meet (vr, &vrres);
1374 return;
1377 /* The type of the resulting value range defaults to VR0.TYPE. */
1378 type = vr0.type;
1380 /* Refuse to operate on VARYING ranges, ranges of different kinds
1381 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
1382 because we may be able to derive a useful range even if one of
1383 the operands is VR_VARYING or symbolic range. Similarly for
1384 divisions, MIN/MAX and PLUS/MINUS.
1386 TODO, we may be able to derive anti-ranges in some cases. */
1387 if (code != BIT_AND_EXPR
1388 && code != BIT_IOR_EXPR
1389 && code != TRUNC_DIV_EXPR
1390 && code != FLOOR_DIV_EXPR
1391 && code != CEIL_DIV_EXPR
1392 && code != EXACT_DIV_EXPR
1393 && code != ROUND_DIV_EXPR
1394 && code != TRUNC_MOD_EXPR
1395 && code != MIN_EXPR
1396 && code != MAX_EXPR
1397 && code != PLUS_EXPR
1398 && code != MINUS_EXPR
1399 && code != RSHIFT_EXPR
1400 && (vr0.type == VR_VARYING
1401 || vr1.type == VR_VARYING
1402 || vr0.type != vr1.type
1403 || symbolic_range_p (&vr0)
1404 || symbolic_range_p (&vr1)))
1406 set_value_range_to_varying (vr);
1407 return;
1410 /* Now evaluate the expression to determine the new range. */
1411 if (POINTER_TYPE_P (expr_type))
1413 if (code == MIN_EXPR || code == MAX_EXPR)
1415 /* For MIN/MAX expressions with pointers, we only care about
1416 nullness, if both are non null, then the result is nonnull.
1417 If both are null, then the result is null. Otherwise they
1418 are varying. */
1419 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
1420 set_value_range_to_nonnull (vr, expr_type);
1421 else if (range_is_null (&vr0) && range_is_null (&vr1))
1422 set_value_range_to_null (vr, expr_type);
1423 else
1424 set_value_range_to_varying (vr);
1426 else if (code == POINTER_PLUS_EXPR)
1428 /* For pointer types, we are really only interested in asserting
1429 whether the expression evaluates to non-NULL. */
1430 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
1431 set_value_range_to_nonnull (vr, expr_type);
1432 else if (range_is_null (&vr0) && range_is_null (&vr1))
1433 set_value_range_to_null (vr, expr_type);
1434 else
1435 set_value_range_to_varying (vr);
1437 else if (code == BIT_AND_EXPR)
1439 /* For pointer types, we are really only interested in asserting
1440 whether the expression evaluates to non-NULL. */
1441 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
1442 set_value_range_to_nonnull (vr, expr_type);
1443 else if (range_is_null (&vr0) || range_is_null (&vr1))
1444 set_value_range_to_null (vr, expr_type);
1445 else
1446 set_value_range_to_varying (vr);
1448 else
1449 set_value_range_to_varying (vr);
1451 return;
1454 /* For integer ranges, apply the operation to each end of the
1455 range and see what we end up with. */
1456 if (code == PLUS_EXPR || code == MINUS_EXPR)
1458 const bool minus_p = (code == MINUS_EXPR);
1459 tree min_op0 = vr0.min;
1460 tree min_op1 = minus_p ? vr1.max : vr1.min;
1461 tree max_op0 = vr0.max;
1462 tree max_op1 = minus_p ? vr1.min : vr1.max;
1463 tree sym_min_op0 = NULL_TREE;
1464 tree sym_min_op1 = NULL_TREE;
1465 tree sym_max_op0 = NULL_TREE;
1466 tree sym_max_op1 = NULL_TREE;
1467 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
1469 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
1470 single-symbolic ranges, try to compute the precise resulting range,
1471 but only if we know that this resulting range will also be constant
1472 or single-symbolic. */
1473 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
1474 && (TREE_CODE (min_op0) == INTEGER_CST
1475 || (sym_min_op0
1476 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
1477 && (TREE_CODE (min_op1) == INTEGER_CST
1478 || (sym_min_op1
1479 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
1480 && (!(sym_min_op0 && sym_min_op1)
1481 || (sym_min_op0 == sym_min_op1
1482 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
1483 && (TREE_CODE (max_op0) == INTEGER_CST
1484 || (sym_max_op0
1485 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
1486 && (TREE_CODE (max_op1) == INTEGER_CST
1487 || (sym_max_op1
1488 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
1489 && (!(sym_max_op0 && sym_max_op1)
1490 || (sym_max_op0 == sym_max_op1
1491 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
1493 const signop sgn = TYPE_SIGN (expr_type);
1494 const unsigned int prec = TYPE_PRECISION (expr_type);
1495 wide_int type_min, type_max, wmin, wmax;
1496 int min_ovf = 0;
1497 int max_ovf = 0;
1499 /* Get the lower and upper bounds of the type. */
1500 if (TYPE_OVERFLOW_WRAPS (expr_type))
1502 type_min = wi::min_value (prec, sgn);
1503 type_max = wi::max_value (prec, sgn);
1505 else
1507 type_min = wi::to_wide (vrp_val_min (expr_type));
1508 type_max = wi::to_wide (vrp_val_max (expr_type));
1511 /* Combine the lower bounds, if any. */
1512 if (min_op0 && min_op1)
1514 if (minus_p)
1516 wmin = wi::to_wide (min_op0) - wi::to_wide (min_op1);
1518 /* Check for overflow. */
1519 if (wi::cmp (0, wi::to_wide (min_op1), sgn)
1520 != wi::cmp (wmin, wi::to_wide (min_op0), sgn))
1521 min_ovf = wi::cmp (wi::to_wide (min_op0),
1522 wi::to_wide (min_op1), sgn);
1524 else
1526 wmin = wi::to_wide (min_op0) + wi::to_wide (min_op1);
1528 /* Check for overflow. */
1529 if (wi::cmp (wi::to_wide (min_op1), 0, sgn)
1530 != wi::cmp (wmin, wi::to_wide (min_op0), sgn))
1531 min_ovf = wi::cmp (wi::to_wide (min_op0), wmin, sgn);
1534 else if (min_op0)
1535 wmin = wi::to_wide (min_op0);
1536 else if (min_op1)
1538 if (minus_p)
1540 wmin = -wi::to_wide (min_op1);
1542 /* Check for overflow. */
1543 if (sgn == SIGNED
1544 && wi::neg_p (wi::to_wide (min_op1))
1545 && wi::neg_p (wmin))
1546 min_ovf = 1;
1547 else if (sgn == UNSIGNED && wi::to_wide (min_op1) != 0)
1548 min_ovf = -1;
1550 else
1551 wmin = wi::to_wide (min_op1);
1553 else
1554 wmin = wi::shwi (0, prec);
1556 /* Combine the upper bounds, if any. */
1557 if (max_op0 && max_op1)
1559 if (minus_p)
1561 wmax = wi::to_wide (max_op0) - wi::to_wide (max_op1);
1563 /* Check for overflow. */
1564 if (wi::cmp (0, wi::to_wide (max_op1), sgn)
1565 != wi::cmp (wmax, wi::to_wide (max_op0), sgn))
1566 max_ovf = wi::cmp (wi::to_wide (max_op0),
1567 wi::to_wide (max_op1), sgn);
1569 else
1571 wmax = wi::to_wide (max_op0) + wi::to_wide (max_op1);
1573 if (wi::cmp (wi::to_wide (max_op1), 0, sgn)
1574 != wi::cmp (wmax, wi::to_wide (max_op0), sgn))
1575 max_ovf = wi::cmp (wi::to_wide (max_op0), wmax, sgn);
1578 else if (max_op0)
1579 wmax = wi::to_wide (max_op0);
1580 else if (max_op1)
1582 if (minus_p)
1584 wmax = -wi::to_wide (max_op1);
1586 /* Check for overflow. */
1587 if (sgn == SIGNED
1588 && wi::neg_p (wi::to_wide (max_op1))
1589 && wi::neg_p (wmax))
1590 max_ovf = 1;
1591 else if (sgn == UNSIGNED && wi::to_wide (max_op1) != 0)
1592 max_ovf = -1;
1594 else
1595 wmax = wi::to_wide (max_op1);
1597 else
1598 wmax = wi::shwi (0, prec);
1600 /* Check for type overflow. */
1601 if (min_ovf == 0)
1603 if (wi::cmp (wmin, type_min, sgn) == -1)
1604 min_ovf = -1;
1605 else if (wi::cmp (wmin, type_max, sgn) == 1)
1606 min_ovf = 1;
1608 if (max_ovf == 0)
1610 if (wi::cmp (wmax, type_min, sgn) == -1)
1611 max_ovf = -1;
1612 else if (wi::cmp (wmax, type_max, sgn) == 1)
1613 max_ovf = 1;
1616 /* If we have overflow for the constant part and the resulting
1617 range will be symbolic, drop to VR_VARYING. */
1618 if ((min_ovf && sym_min_op0 != sym_min_op1)
1619 || (max_ovf && sym_max_op0 != sym_max_op1))
1621 set_value_range_to_varying (vr);
1622 return;
1625 if (TYPE_OVERFLOW_WRAPS (expr_type))
1627 /* If overflow wraps, truncate the values and adjust the
1628 range kind and bounds appropriately. */
1629 wide_int tmin = wide_int::from (wmin, prec, sgn);
1630 wide_int tmax = wide_int::from (wmax, prec, sgn);
1631 if (min_ovf == max_ovf)
1633 /* No overflow or both overflow or underflow. The
1634 range kind stays VR_RANGE. */
1635 min = wide_int_to_tree (expr_type, tmin);
1636 max = wide_int_to_tree (expr_type, tmax);
1638 else if ((min_ovf == -1 && max_ovf == 0)
1639 || (max_ovf == 1 && min_ovf == 0))
1641 /* Min underflow or max overflow. The range kind
1642 changes to VR_ANTI_RANGE. */
1643 bool covers = false;
1644 wide_int tem = tmin;
1645 type = VR_ANTI_RANGE;
1646 tmin = tmax + 1;
1647 if (wi::cmp (tmin, tmax, sgn) < 0)
1648 covers = true;
1649 tmax = tem - 1;
1650 if (wi::cmp (tmax, tem, sgn) > 0)
1651 covers = true;
1652 /* If the anti-range would cover nothing, drop to varying.
1653 Likewise if the anti-range bounds are outside of the
1654 types values. */
1655 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
1657 set_value_range_to_varying (vr);
1658 return;
1660 min = wide_int_to_tree (expr_type, tmin);
1661 max = wide_int_to_tree (expr_type, tmax);
1663 else
1665 /* Other underflow and/or overflow, drop to VR_VARYING. */
1666 set_value_range_to_varying (vr);
1667 return;
1670 else
1672 /* If overflow does not wrap, saturate to the types min/max
1673 value. */
1674 if (min_ovf == -1)
1675 min = wide_int_to_tree (expr_type, type_min);
1676 else if (min_ovf == 1)
1677 min = wide_int_to_tree (expr_type, type_max);
1678 else
1679 min = wide_int_to_tree (expr_type, wmin);
1681 if (max_ovf == -1)
1682 max = wide_int_to_tree (expr_type, type_min);
1683 else if (max_ovf == 1)
1684 max = wide_int_to_tree (expr_type, type_max);
1685 else
1686 max = wide_int_to_tree (expr_type, wmax);
1689 /* If the result lower bound is constant, we're done;
1690 otherwise, build the symbolic lower bound. */
1691 if (sym_min_op0 == sym_min_op1)
1693 else if (sym_min_op0)
1694 min = build_symbolic_expr (expr_type, sym_min_op0,
1695 neg_min_op0, min);
1696 else if (sym_min_op1)
1698 /* We may not negate if that might introduce
1699 undefined overflow. */
1700 if (! minus_p
1701 || neg_min_op1
1702 || TYPE_OVERFLOW_WRAPS (expr_type))
1703 min = build_symbolic_expr (expr_type, sym_min_op1,
1704 neg_min_op1 ^ minus_p, min);
1705 else
1706 min = NULL_TREE;
1709 /* Likewise for the upper bound. */
1710 if (sym_max_op0 == sym_max_op1)
1712 else if (sym_max_op0)
1713 max = build_symbolic_expr (expr_type, sym_max_op0,
1714 neg_max_op0, max);
1715 else if (sym_max_op1)
1717 /* We may not negate if that might introduce
1718 undefined overflow. */
1719 if (! minus_p
1720 || neg_max_op1
1721 || TYPE_OVERFLOW_WRAPS (expr_type))
1722 max = build_symbolic_expr (expr_type, sym_max_op1,
1723 neg_max_op1 ^ minus_p, max);
1724 else
1725 max = NULL_TREE;
1728 else
1730 /* For other cases, for example if we have a PLUS_EXPR with two
1731 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
1732 to compute a precise range for such a case.
1733 ??? General even mixed range kind operations can be expressed
1734 by for example transforming ~[3, 5] + [1, 2] to range-only
1735 operations and a union primitive:
1736 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
1737 [-INF+1, 4] U [6, +INF(OVF)]
1738 though usually the union is not exactly representable with
1739 a single range or anti-range as the above is
1740 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
1741 but one could use a scheme similar to equivalences for this. */
1742 set_value_range_to_varying (vr);
1743 return;
1746 else if (code == MIN_EXPR
1747 || code == MAX_EXPR)
1749 if (vr0.type == VR_RANGE
1750 && !symbolic_range_p (&vr0))
1752 type = VR_RANGE;
1753 if (vr1.type == VR_RANGE
1754 && !symbolic_range_p (&vr1))
1756 /* For operations that make the resulting range directly
1757 proportional to the original ranges, apply the operation to
1758 the same end of each range. */
1759 min = int_const_binop (code, vr0.min, vr1.min);
1760 max = int_const_binop (code, vr0.max, vr1.max);
1762 else if (code == MIN_EXPR)
1764 min = vrp_val_min (expr_type);
1765 max = vr0.max;
1767 else if (code == MAX_EXPR)
1769 min = vr0.min;
1770 max = vrp_val_max (expr_type);
1773 else if (vr1.type == VR_RANGE
1774 && !symbolic_range_p (&vr1))
1776 type = VR_RANGE;
1777 if (code == MIN_EXPR)
1779 min = vrp_val_min (expr_type);
1780 max = vr1.max;
1782 else if (code == MAX_EXPR)
1784 min = vr1.min;
1785 max = vrp_val_max (expr_type);
1788 else
1790 set_value_range_to_varying (vr);
1791 return;
1794 else if (code == MULT_EXPR)
1796 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
1797 drop to varying. This test requires 2*prec bits if both
1798 operands are signed and 2*prec + 2 bits if either is not. */
1800 signop sign = TYPE_SIGN (expr_type);
1801 unsigned int prec = TYPE_PRECISION (expr_type);
1803 if (!range_int_cst_p (&vr0)
1804 || !range_int_cst_p (&vr1))
1806 set_value_range_to_varying (vr);
1807 return;
1810 if (TYPE_OVERFLOW_WRAPS (expr_type))
1812 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
1813 typedef generic_wide_int
1814 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
1815 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
1816 vrp_int size = sizem1 + 1;
1818 /* Extend the values using the sign of the result to PREC2.
1819 From here on out, everthing is just signed math no matter
1820 what the input types were. */
1821 vrp_int min0 = vrp_int_cst (vr0.min);
1822 vrp_int max0 = vrp_int_cst (vr0.max);
1823 vrp_int min1 = vrp_int_cst (vr1.min);
1824 vrp_int max1 = vrp_int_cst (vr1.max);
1825 /* Canonicalize the intervals. */
1826 if (sign == UNSIGNED)
1828 if (wi::ltu_p (size, min0 + max0))
1830 min0 -= size;
1831 max0 -= size;
1834 if (wi::ltu_p (size, min1 + max1))
1836 min1 -= size;
1837 max1 -= size;
1841 vrp_int prod0 = min0 * min1;
1842 vrp_int prod1 = min0 * max1;
1843 vrp_int prod2 = max0 * min1;
1844 vrp_int prod3 = max0 * max1;
1846 /* Sort the 4 products so that min is in prod0 and max is in
1847 prod3. */
1848 /* min0min1 > max0max1 */
1849 if (prod0 > prod3)
1850 std::swap (prod0, prod3);
1852 /* min0max1 > max0min1 */
1853 if (prod1 > prod2)
1854 std::swap (prod1, prod2);
1856 if (prod0 > prod1)
1857 std::swap (prod0, prod1);
1859 if (prod2 > prod3)
1860 std::swap (prod2, prod3);
1862 /* diff = max - min. */
1863 prod2 = prod3 - prod0;
1864 if (wi::geu_p (prod2, sizem1))
1866 /* the range covers all values. */
1867 set_value_range_to_varying (vr);
1868 return;
1871 /* The following should handle the wrapping and selecting
1872 VR_ANTI_RANGE for us. */
1873 min = wide_int_to_tree (expr_type, prod0);
1874 max = wide_int_to_tree (expr_type, prod3);
1875 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
1876 return;
1879 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
1880 drop to VR_VARYING. It would take more effort to compute a
1881 precise range for such a case. For example, if we have
1882 op0 == 65536 and op1 == 65536 with their ranges both being
1883 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
1884 we cannot claim that the product is in ~[0,0]. Note that we
1885 are guaranteed to have vr0.type == vr1.type at this
1886 point. */
1887 if (vr0.type == VR_ANTI_RANGE
1888 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
1890 set_value_range_to_varying (vr);
1891 return;
1894 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
1895 return;
1897 else if (code == RSHIFT_EXPR
1898 || code == LSHIFT_EXPR)
1900 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
1901 then drop to VR_VARYING. Outside of this range we get undefined
1902 behavior from the shift operation. We cannot even trust
1903 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
1904 shifts, and the operation at the tree level may be widened. */
1905 if (range_int_cst_p (&vr1)
1906 && compare_tree_int (vr1.min, 0) >= 0
1907 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
1909 if (code == RSHIFT_EXPR)
1911 /* Even if vr0 is VARYING or otherwise not usable, we can derive
1912 useful ranges just from the shift count. E.g.
1913 x >> 63 for signed 64-bit x is always [-1, 0]. */
1914 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
1916 vr0.type = type = VR_RANGE;
1917 vr0.min = vrp_val_min (expr_type);
1918 vr0.max = vrp_val_max (expr_type);
1920 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
1921 return;
1923 /* We can map lshifts by constants to MULT_EXPR handling. */
1924 else if (code == LSHIFT_EXPR
1925 && range_int_cst_singleton_p (&vr1))
1927 bool saved_flag_wrapv;
1928 value_range vr1p = VR_INITIALIZER;
1929 vr1p.type = VR_RANGE;
1930 vr1p.min = (wide_int_to_tree
1931 (expr_type,
1932 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
1933 TYPE_PRECISION (expr_type))));
1934 vr1p.max = vr1p.min;
1935 /* We have to use a wrapping multiply though as signed overflow
1936 on lshifts is implementation defined in C89. */
1937 saved_flag_wrapv = flag_wrapv;
1938 flag_wrapv = 1;
1939 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
1940 &vr0, &vr1p);
1941 flag_wrapv = saved_flag_wrapv;
1942 return;
1944 else if (code == LSHIFT_EXPR
1945 && range_int_cst_p (&vr0))
1947 int prec = TYPE_PRECISION (expr_type);
1948 int overflow_pos = prec;
1949 int bound_shift;
1950 wide_int low_bound, high_bound;
1951 bool uns = TYPE_UNSIGNED (expr_type);
1952 bool in_bounds = false;
1954 if (!uns)
1955 overflow_pos -= 1;
1957 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
1958 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
1959 overflow. However, for that to happen, vr1.max needs to be
1960 zero, which means vr1 is a singleton range of zero, which
1961 means it should be handled by the previous LSHIFT_EXPR
1962 if-clause. */
1963 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
1964 wide_int complement = ~(bound - 1);
1966 if (uns)
1968 low_bound = bound;
1969 high_bound = complement;
1970 if (wi::ltu_p (wi::to_wide (vr0.max), low_bound))
1972 /* [5, 6] << [1, 2] == [10, 24]. */
1973 /* We're shifting out only zeroes, the value increases
1974 monotonically. */
1975 in_bounds = true;
1977 else if (wi::ltu_p (high_bound, wi::to_wide (vr0.min)))
1979 /* [0xffffff00, 0xffffffff] << [1, 2]
1980 == [0xfffffc00, 0xfffffffe]. */
1981 /* We're shifting out only ones, the value decreases
1982 monotonically. */
1983 in_bounds = true;
1986 else
1988 /* [-1, 1] << [1, 2] == [-4, 4]. */
1989 low_bound = complement;
1990 high_bound = bound;
1991 if (wi::lts_p (wi::to_wide (vr0.max), high_bound)
1992 && wi::lts_p (low_bound, wi::to_wide (vr0.min)))
1994 /* For non-negative numbers, we're shifting out only
1995 zeroes, the value increases monotonically.
1996 For negative numbers, we're shifting out only ones, the
1997 value decreases monotomically. */
1998 in_bounds = true;
2002 if (in_bounds)
2004 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2005 return;
2009 set_value_range_to_varying (vr);
2010 return;
2012 else if (code == TRUNC_DIV_EXPR
2013 || code == FLOOR_DIV_EXPR
2014 || code == CEIL_DIV_EXPR
2015 || code == EXACT_DIV_EXPR
2016 || code == ROUND_DIV_EXPR)
2018 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2020 /* For division, if op1 has VR_RANGE but op0 does not, something
2021 can be deduced just from that range. Say [min, max] / [4, max]
2022 gives [min / 4, max / 4] range. */
2023 if (vr1.type == VR_RANGE
2024 && !symbolic_range_p (&vr1)
2025 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2027 vr0.type = type = VR_RANGE;
2028 vr0.min = vrp_val_min (expr_type);
2029 vr0.max = vrp_val_max (expr_type);
2031 else
2033 set_value_range_to_varying (vr);
2034 return;
2038 /* For divisions, if flag_non_call_exceptions is true, we must
2039 not eliminate a division by zero. */
2040 if (cfun->can_throw_non_call_exceptions
2041 && (vr1.type != VR_RANGE
2042 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2044 set_value_range_to_varying (vr);
2045 return;
2048 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2049 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2050 include 0. */
2051 if (vr0.type == VR_RANGE
2052 && (vr1.type != VR_RANGE
2053 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2055 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2056 int cmp;
2058 min = NULL_TREE;
2059 max = NULL_TREE;
2060 if (TYPE_UNSIGNED (expr_type)
2061 || value_range_nonnegative_p (&vr1))
2063 /* For unsigned division or when divisor is known
2064 to be non-negative, the range has to cover
2065 all numbers from 0 to max for positive max
2066 and all numbers from min to 0 for negative min. */
2067 cmp = compare_values (vr0.max, zero);
2068 if (cmp == -1)
2070 /* When vr0.max < 0, vr1.min != 0 and value
2071 ranges for dividend and divisor are available. */
2072 if (vr1.type == VR_RANGE
2073 && !symbolic_range_p (&vr0)
2074 && !symbolic_range_p (&vr1)
2075 && compare_values (vr1.min, zero) != 0)
2076 max = int_const_binop (code, vr0.max, vr1.min);
2077 else
2078 max = zero;
2080 else if (cmp == 0 || cmp == 1)
2081 max = vr0.max;
2082 else
2083 type = VR_VARYING;
2084 cmp = compare_values (vr0.min, zero);
2085 if (cmp == 1)
2087 /* For unsigned division when value ranges for dividend
2088 and divisor are available. */
2089 if (vr1.type == VR_RANGE
2090 && !symbolic_range_p (&vr0)
2091 && !symbolic_range_p (&vr1)
2092 && compare_values (vr1.max, zero) != 0)
2093 min = int_const_binop (code, vr0.min, vr1.max);
2094 else
2095 min = zero;
2097 else if (cmp == 0 || cmp == -1)
2098 min = vr0.min;
2099 else
2100 type = VR_VARYING;
2102 else
2104 /* Otherwise the range is -max .. max or min .. -min
2105 depending on which bound is bigger in absolute value,
2106 as the division can change the sign. */
2107 abs_extent_range (vr, vr0.min, vr0.max);
2108 return;
2110 if (type == VR_VARYING)
2112 set_value_range_to_varying (vr);
2113 return;
2116 else if (range_int_cst_p (&vr0) && range_int_cst_p (&vr1))
2118 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2119 return;
2122 else if (code == TRUNC_MOD_EXPR)
2124 if (range_is_null (&vr1))
2126 set_value_range_to_undefined (vr);
2127 return;
2129 /* ABS (A % B) < ABS (B) and either
2130 0 <= A % B <= A or A <= A % B <= 0. */
2131 type = VR_RANGE;
2132 signop sgn = TYPE_SIGN (expr_type);
2133 unsigned int prec = TYPE_PRECISION (expr_type);
2134 wide_int wmin, wmax, tmp;
2135 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
2137 wmax = wi::to_wide (vr1.max) - 1;
2138 if (sgn == SIGNED)
2140 tmp = -1 - wi::to_wide (vr1.min);
2141 wmax = wi::smax (wmax, tmp);
2144 else
2146 wmax = wi::max_value (prec, sgn);
2147 /* X % INT_MIN may be INT_MAX. */
2148 if (sgn == UNSIGNED)
2149 wmax = wmax - 1;
2152 if (sgn == UNSIGNED)
2153 wmin = wi::zero (prec);
2154 else
2156 wmin = -wmax;
2157 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
2159 tmp = wi::to_wide (vr0.min);
2160 if (wi::gts_p (tmp, 0))
2161 tmp = wi::zero (prec);
2162 wmin = wi::smax (wmin, tmp);
2166 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
2168 tmp = wi::to_wide (vr0.max);
2169 if (sgn == SIGNED && wi::neg_p (tmp))
2170 tmp = wi::zero (prec);
2171 wmax = wi::min (wmax, tmp, sgn);
2174 min = wide_int_to_tree (expr_type, wmin);
2175 max = wide_int_to_tree (expr_type, wmax);
2177 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
2179 bool int_cst_range0, int_cst_range1;
2180 wide_int may_be_nonzero0, may_be_nonzero1;
2181 wide_int must_be_nonzero0, must_be_nonzero1;
2183 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
2184 &may_be_nonzero0,
2185 &must_be_nonzero0);
2186 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
2187 &may_be_nonzero1,
2188 &must_be_nonzero1);
2190 if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR)
2192 value_range *vr0p = NULL, *vr1p = NULL;
2193 if (range_int_cst_singleton_p (&vr1))
2195 vr0p = &vr0;
2196 vr1p = &vr1;
2198 else if (range_int_cst_singleton_p (&vr0))
2200 vr0p = &vr1;
2201 vr1p = &vr0;
2203 /* For op & or | attempt to optimize:
2204 [x, y] op z into [x op z, y op z]
2205 if z is a constant which (for op | its bitwise not) has n
2206 consecutive least significant bits cleared followed by m 1
2207 consecutive bits set immediately above it and either
2208 m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2209 The least significant n bits of all the values in the range are
2210 cleared or set, the m bits above it are preserved and any bits
2211 above these are required to be the same for all values in the
2212 range. */
2213 if (vr0p && range_int_cst_p (vr0p))
2215 wide_int w = wi::to_wide (vr1p->min);
2216 int m = 0, n = 0;
2217 if (code == BIT_IOR_EXPR)
2218 w = ~w;
2219 if (wi::eq_p (w, 0))
2220 n = TYPE_PRECISION (expr_type);
2221 else
2223 n = wi::ctz (w);
2224 w = ~(w | wi::mask (n, false, w.get_precision ()));
2225 if (wi::eq_p (w, 0))
2226 m = TYPE_PRECISION (expr_type) - n;
2227 else
2228 m = wi::ctz (w) - n;
2230 wide_int mask = wi::mask (m + n, true, w.get_precision ());
2231 if ((mask & wi::to_wide (vr0p->min))
2232 == (mask & wi::to_wide (vr0p->max)))
2234 min = int_const_binop (code, vr0p->min, vr1p->min);
2235 max = int_const_binop (code, vr0p->max, vr1p->min);
2240 type = VR_RANGE;
2241 if (min && max)
2242 /* Optimized above already. */;
2243 else if (code == BIT_AND_EXPR)
2245 min = wide_int_to_tree (expr_type,
2246 must_be_nonzero0 & must_be_nonzero1);
2247 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
2248 /* If both input ranges contain only negative values we can
2249 truncate the result range maximum to the minimum of the
2250 input range maxima. */
2251 if (int_cst_range0 && int_cst_range1
2252 && tree_int_cst_sgn (vr0.max) < 0
2253 && tree_int_cst_sgn (vr1.max) < 0)
2255 wmax = wi::min (wmax, wi::to_wide (vr0.max),
2256 TYPE_SIGN (expr_type));
2257 wmax = wi::min (wmax, wi::to_wide (vr1.max),
2258 TYPE_SIGN (expr_type));
2260 /* If either input range contains only non-negative values
2261 we can truncate the result range maximum to the respective
2262 maximum of the input range. */
2263 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
2264 wmax = wi::min (wmax, wi::to_wide (vr0.max),
2265 TYPE_SIGN (expr_type));
2266 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
2267 wmax = wi::min (wmax, wi::to_wide (vr1.max),
2268 TYPE_SIGN (expr_type));
2269 max = wide_int_to_tree (expr_type, wmax);
2270 cmp = compare_values (min, max);
2271 /* PR68217: In case of signed & sign-bit-CST should
2272 result in [-INF, 0] instead of [-INF, INF]. */
2273 if (cmp == -2 || cmp == 1)
2275 wide_int sign_bit
2276 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type) - 1,
2277 TYPE_PRECISION (expr_type));
2278 if (!TYPE_UNSIGNED (expr_type)
2279 && ((int_cst_range0
2280 && value_range_constant_singleton (&vr0)
2281 && !wi::cmps (wi::to_wide (vr0.min), sign_bit))
2282 || (int_cst_range1
2283 && value_range_constant_singleton (&vr1)
2284 && !wi::cmps (wi::to_wide (vr1.min), sign_bit))))
2286 min = TYPE_MIN_VALUE (expr_type);
2287 max = build_int_cst (expr_type, 0);
2291 else if (code == BIT_IOR_EXPR)
2293 max = wide_int_to_tree (expr_type,
2294 may_be_nonzero0 | may_be_nonzero1);
2295 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
2296 /* If the input ranges contain only positive values we can
2297 truncate the minimum of the result range to the maximum
2298 of the input range minima. */
2299 if (int_cst_range0 && int_cst_range1
2300 && tree_int_cst_sgn (vr0.min) >= 0
2301 && tree_int_cst_sgn (vr1.min) >= 0)
2303 wmin = wi::max (wmin, wi::to_wide (vr0.min),
2304 TYPE_SIGN (expr_type));
2305 wmin = wi::max (wmin, wi::to_wide (vr1.min),
2306 TYPE_SIGN (expr_type));
2308 /* If either input range contains only negative values
2309 we can truncate the minimum of the result range to the
2310 respective minimum range. */
2311 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
2312 wmin = wi::max (wmin, wi::to_wide (vr0.min),
2313 TYPE_SIGN (expr_type));
2314 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
2315 wmin = wi::max (wmin, wi::to_wide (vr1.min),
2316 TYPE_SIGN (expr_type));
2317 min = wide_int_to_tree (expr_type, wmin);
2319 else if (code == BIT_XOR_EXPR)
2321 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
2322 | ~(may_be_nonzero0 | may_be_nonzero1));
2323 wide_int result_one_bits
2324 = (wi::bit_and_not (must_be_nonzero0, may_be_nonzero1)
2325 | wi::bit_and_not (must_be_nonzero1, may_be_nonzero0));
2326 max = wide_int_to_tree (expr_type, ~result_zero_bits);
2327 min = wide_int_to_tree (expr_type, result_one_bits);
2328 /* If the range has all positive or all negative values the
2329 result is better than VARYING. */
2330 if (tree_int_cst_sgn (min) < 0
2331 || tree_int_cst_sgn (max) >= 0)
2333 else
2334 max = min = NULL_TREE;
2337 else
2338 gcc_unreachable ();
2340 /* If either MIN or MAX overflowed, then set the resulting range to
2341 VARYING. */
2342 if (min == NULL_TREE
2343 || TREE_OVERFLOW_P (min)
2344 || max == NULL_TREE
2345 || TREE_OVERFLOW_P (max))
2347 set_value_range_to_varying (vr);
2348 return;
2351 /* We punt for [-INF, +INF].
2352 We learn nothing when we have INF on both sides.
2353 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
2354 if (vrp_val_is_min (min) && vrp_val_is_max (max))
2356 set_value_range_to_varying (vr);
2357 return;
2360 cmp = compare_values (min, max);
2361 if (cmp == -2 || cmp == 1)
2363 /* If the new range has its limits swapped around (MIN > MAX),
2364 then the operation caused one of them to wrap around, mark
2365 the new range VARYING. */
2366 set_value_range_to_varying (vr);
2368 else
2369 set_value_range (vr, type, min, max, NULL);
2372 /* Extract range information from a unary operation CODE based on
2373 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
2374 The resulting range is stored in *VR. */
2376 void
2377 extract_range_from_unary_expr (value_range *vr,
2378 enum tree_code code, tree type,
2379 value_range *vr0_, tree op0_type)
2381 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2383 /* VRP only operates on integral and pointer types. */
2384 if (!(INTEGRAL_TYPE_P (op0_type)
2385 || POINTER_TYPE_P (op0_type))
2386 || !(INTEGRAL_TYPE_P (type)
2387 || POINTER_TYPE_P (type)))
2389 set_value_range_to_varying (vr);
2390 return;
2393 /* If VR0 is UNDEFINED, so is the result. */
2394 if (vr0.type == VR_UNDEFINED)
2396 set_value_range_to_undefined (vr);
2397 return;
2400 /* Handle operations that we express in terms of others. */
2401 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
2403 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
2404 copy_value_range (vr, &vr0);
2405 return;
2407 else if (code == NEGATE_EXPR)
2409 /* -X is simply 0 - X, so re-use existing code that also handles
2410 anti-ranges fine. */
2411 value_range zero = VR_INITIALIZER;
2412 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
2413 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
2414 return;
2416 else if (code == BIT_NOT_EXPR)
2418 /* ~X is simply -1 - X, so re-use existing code that also handles
2419 anti-ranges fine. */
2420 value_range minusone = VR_INITIALIZER;
2421 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
2422 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
2423 type, &minusone, &vr0);
2424 return;
2427 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2428 and express op ~[] as (op []') U (op []''). */
2429 if (vr0.type == VR_ANTI_RANGE
2430 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2432 extract_range_from_unary_expr (vr, code, type, &vrtem0, op0_type);
2433 if (vrtem1.type != VR_UNDEFINED)
2435 value_range vrres = VR_INITIALIZER;
2436 extract_range_from_unary_expr (&vrres, code, type,
2437 &vrtem1, op0_type);
2438 vrp_meet (vr, &vrres);
2440 return;
2443 if (CONVERT_EXPR_CODE_P (code))
2445 tree inner_type = op0_type;
2446 tree outer_type = type;
2448 /* If the expression evaluates to a pointer, we are only interested in
2449 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
2450 if (POINTER_TYPE_P (type))
2452 if (range_is_nonnull (&vr0))
2453 set_value_range_to_nonnull (vr, type);
2454 else if (range_is_null (&vr0))
2455 set_value_range_to_null (vr, type);
2456 else
2457 set_value_range_to_varying (vr);
2458 return;
2461 /* If VR0 is varying and we increase the type precision, assume
2462 a full range for the following transformation. */
2463 if (vr0.type == VR_VARYING
2464 && INTEGRAL_TYPE_P (inner_type)
2465 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
2467 vr0.type = VR_RANGE;
2468 vr0.min = TYPE_MIN_VALUE (inner_type);
2469 vr0.max = TYPE_MAX_VALUE (inner_type);
2472 /* If VR0 is a constant range or anti-range and the conversion is
2473 not truncating we can convert the min and max values and
2474 canonicalize the resulting range. Otherwise we can do the
2475 conversion if the size of the range is less than what the
2476 precision of the target type can represent and the range is
2477 not an anti-range. */
2478 if ((vr0.type == VR_RANGE
2479 || vr0.type == VR_ANTI_RANGE)
2480 && TREE_CODE (vr0.min) == INTEGER_CST
2481 && TREE_CODE (vr0.max) == INTEGER_CST
2482 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
2483 || (vr0.type == VR_RANGE
2484 && integer_zerop (int_const_binop (RSHIFT_EXPR,
2485 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
2486 size_int (TYPE_PRECISION (outer_type)))))))
2488 tree new_min, new_max;
2489 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
2490 0, false);
2491 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
2492 0, false);
2493 set_and_canonicalize_value_range (vr, vr0.type,
2494 new_min, new_max, NULL);
2495 return;
2498 set_value_range_to_varying (vr);
2499 return;
2501 else if (code == ABS_EXPR)
2503 tree min, max;
2504 int cmp;
2506 /* Pass through vr0 in the easy cases. */
2507 if (TYPE_UNSIGNED (type)
2508 || value_range_nonnegative_p (&vr0))
2510 copy_value_range (vr, &vr0);
2511 return;
2514 /* For the remaining varying or symbolic ranges we can't do anything
2515 useful. */
2516 if (vr0.type == VR_VARYING
2517 || symbolic_range_p (&vr0))
2519 set_value_range_to_varying (vr);
2520 return;
2523 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
2524 useful range. */
2525 if (!TYPE_OVERFLOW_UNDEFINED (type)
2526 && ((vr0.type == VR_RANGE
2527 && vrp_val_is_min (vr0.min))
2528 || (vr0.type == VR_ANTI_RANGE
2529 && !vrp_val_is_min (vr0.min))))
2531 set_value_range_to_varying (vr);
2532 return;
2535 /* ABS_EXPR may flip the range around, if the original range
2536 included negative values. */
2537 if (!vrp_val_is_min (vr0.min))
2538 min = fold_unary_to_constant (code, type, vr0.min);
2539 else
2540 min = TYPE_MAX_VALUE (type);
2542 if (!vrp_val_is_min (vr0.max))
2543 max = fold_unary_to_constant (code, type, vr0.max);
2544 else
2545 max = TYPE_MAX_VALUE (type);
2547 cmp = compare_values (min, max);
2549 /* If a VR_ANTI_RANGEs contains zero, then we have
2550 ~[-INF, min(MIN, MAX)]. */
2551 if (vr0.type == VR_ANTI_RANGE)
2553 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
2555 /* Take the lower of the two values. */
2556 if (cmp != 1)
2557 max = min;
2559 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
2560 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
2561 flag_wrapv is set and the original anti-range doesn't include
2562 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
2563 if (TYPE_OVERFLOW_WRAPS (type))
2565 tree type_min_value = TYPE_MIN_VALUE (type);
2567 min = (vr0.min != type_min_value
2568 ? int_const_binop (PLUS_EXPR, type_min_value,
2569 build_int_cst (TREE_TYPE (type_min_value), 1))
2570 : type_min_value);
2572 else
2573 min = TYPE_MIN_VALUE (type);
2575 else
2577 /* All else has failed, so create the range [0, INF], even for
2578 flag_wrapv since TYPE_MIN_VALUE is in the original
2579 anti-range. */
2580 vr0.type = VR_RANGE;
2581 min = build_int_cst (type, 0);
2582 max = TYPE_MAX_VALUE (type);
2586 /* If the range contains zero then we know that the minimum value in the
2587 range will be zero. */
2588 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
2590 if (cmp == 1)
2591 max = min;
2592 min = build_int_cst (type, 0);
2594 else
2596 /* If the range was reversed, swap MIN and MAX. */
2597 if (cmp == 1)
2598 std::swap (min, max);
2601 cmp = compare_values (min, max);
2602 if (cmp == -2 || cmp == 1)
2604 /* If the new range has its limits swapped around (MIN > MAX),
2605 then the operation caused one of them to wrap around, mark
2606 the new range VARYING. */
2607 set_value_range_to_varying (vr);
2609 else
2610 set_value_range (vr, vr0.type, min, max, NULL);
2611 return;
2614 /* For unhandled operations fall back to varying. */
2615 set_value_range_to_varying (vr);
2616 return;
2619 /* Debugging dumps. */
2621 void dump_value_range (FILE *, const value_range *);
2622 void debug_value_range (value_range *);
2623 void dump_all_value_ranges (FILE *);
2624 void dump_vr_equiv (FILE *, bitmap);
2625 void debug_vr_equiv (bitmap);
2628 /* Dump value range VR to FILE. */
2630 void
2631 dump_value_range (FILE *file, const value_range *vr)
2633 if (vr == NULL)
2634 fprintf (file, "[]");
2635 else if (vr->type == VR_UNDEFINED)
2636 fprintf (file, "UNDEFINED");
2637 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
2639 tree type = TREE_TYPE (vr->min);
2641 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
2643 if (INTEGRAL_TYPE_P (type)
2644 && !TYPE_UNSIGNED (type)
2645 && vrp_val_is_min (vr->min))
2646 fprintf (file, "-INF");
2647 else
2648 print_generic_expr (file, vr->min);
2650 fprintf (file, ", ");
2652 if (INTEGRAL_TYPE_P (type)
2653 && vrp_val_is_max (vr->max))
2654 fprintf (file, "+INF");
2655 else
2656 print_generic_expr (file, vr->max);
2658 fprintf (file, "]");
2660 if (vr->equiv)
2662 bitmap_iterator bi;
2663 unsigned i, c = 0;
2665 fprintf (file, " EQUIVALENCES: { ");
2667 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
2669 print_generic_expr (file, ssa_name (i));
2670 fprintf (file, " ");
2671 c++;
2674 fprintf (file, "} (%u elements)", c);
2677 else if (vr->type == VR_VARYING)
2678 fprintf (file, "VARYING");
2679 else
2680 fprintf (file, "INVALID RANGE");
2684 /* Dump value range VR to stderr. */
2686 DEBUG_FUNCTION void
2687 debug_value_range (value_range *vr)
2689 dump_value_range (stderr, vr);
2690 fprintf (stderr, "\n");
2694 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2695 create a new SSA name N and return the assertion assignment
2696 'N = ASSERT_EXPR <V, V OP W>'. */
2698 static gimple *
2699 build_assert_expr_for (tree cond, tree v)
2701 tree a;
2702 gassign *assertion;
2704 gcc_assert (TREE_CODE (v) == SSA_NAME
2705 && COMPARISON_CLASS_P (cond));
2707 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
2708 assertion = gimple_build_assign (NULL_TREE, a);
2710 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
2711 operand of the ASSERT_EXPR. Create it so the new name and the old one
2712 are registered in the replacement table so that we can fix the SSA web
2713 after adding all the ASSERT_EXPRs. */
2714 tree new_def = create_new_def_for (v, assertion, NULL);
2715 /* Make sure we preserve abnormalness throughout an ASSERT_EXPR chain
2716 given we have to be able to fully propagate those out to re-create
2717 valid SSA when removing the asserts. */
2718 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (v))
2719 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_def) = 1;
2721 return assertion;
2725 /* Return false if EXPR is a predicate expression involving floating
2726 point values. */
2728 static inline bool
2729 fp_predicate (gimple *stmt)
2731 GIMPLE_CHECK (stmt, GIMPLE_COND);
2733 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
2736 /* If the range of values taken by OP can be inferred after STMT executes,
2737 return the comparison code (COMP_CODE_P) and value (VAL_P) that
2738 describes the inferred range. Return true if a range could be
2739 inferred. */
2741 bool
2742 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
2744 *val_p = NULL_TREE;
2745 *comp_code_p = ERROR_MARK;
2747 /* Do not attempt to infer anything in names that flow through
2748 abnormal edges. */
2749 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
2750 return false;
2752 /* If STMT is the last statement of a basic block with no normal
2753 successors, there is no point inferring anything about any of its
2754 operands. We would not be able to find a proper insertion point
2755 for the assertion, anyway. */
2756 if (stmt_ends_bb_p (stmt))
2758 edge_iterator ei;
2759 edge e;
2761 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
2762 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
2763 break;
2764 if (e == NULL)
2765 return false;
2768 if (infer_nonnull_range (stmt, op))
2770 *val_p = build_int_cst (TREE_TYPE (op), 0);
2771 *comp_code_p = NE_EXPR;
2772 return true;
2775 return false;
2779 void dump_asserts_for (FILE *, tree);
2780 void debug_asserts_for (tree);
2781 void dump_all_asserts (FILE *);
2782 void debug_all_asserts (void);
2784 /* Dump all the registered assertions for NAME to FILE. */
2786 void
2787 dump_asserts_for (FILE *file, tree name)
2789 assert_locus *loc;
2791 fprintf (file, "Assertions to be inserted for ");
2792 print_generic_expr (file, name);
2793 fprintf (file, "\n");
2795 loc = asserts_for[SSA_NAME_VERSION (name)];
2796 while (loc)
2798 fprintf (file, "\t");
2799 print_gimple_stmt (file, gsi_stmt (loc->si), 0);
2800 fprintf (file, "\n\tBB #%d", loc->bb->index);
2801 if (loc->e)
2803 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
2804 loc->e->dest->index);
2805 dump_edge_info (file, loc->e, dump_flags, 0);
2807 fprintf (file, "\n\tPREDICATE: ");
2808 print_generic_expr (file, loc->expr);
2809 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
2810 print_generic_expr (file, loc->val);
2811 fprintf (file, "\n\n");
2812 loc = loc->next;
2815 fprintf (file, "\n");
2819 /* Dump all the registered assertions for NAME to stderr. */
2821 DEBUG_FUNCTION void
2822 debug_asserts_for (tree name)
2824 dump_asserts_for (stderr, name);
2828 /* Dump all the registered assertions for all the names to FILE. */
2830 void
2831 dump_all_asserts (FILE *file)
2833 unsigned i;
2834 bitmap_iterator bi;
2836 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
2837 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
2838 dump_asserts_for (file, ssa_name (i));
2839 fprintf (file, "\n");
2843 /* Dump all the registered assertions for all the names to stderr. */
2845 DEBUG_FUNCTION void
2846 debug_all_asserts (void)
2848 dump_all_asserts (stderr);
2851 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
2853 static void
2854 add_assert_info (vec<assert_info> &asserts,
2855 tree name, tree expr, enum tree_code comp_code, tree val)
2857 assert_info info;
2858 info.comp_code = comp_code;
2859 info.name = name;
2860 if (TREE_OVERFLOW_P (val))
2861 val = drop_tree_overflow (val);
2862 info.val = val;
2863 info.expr = expr;
2864 asserts.safe_push (info);
2867 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2868 'EXPR COMP_CODE VAL' at a location that dominates block BB or
2869 E->DEST, then register this location as a possible insertion point
2870 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
2872 BB, E and SI provide the exact insertion point for the new
2873 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2874 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2875 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2876 must not be NULL. */
2878 static void
2879 register_new_assert_for (tree name, tree expr,
2880 enum tree_code comp_code,
2881 tree val,
2882 basic_block bb,
2883 edge e,
2884 gimple_stmt_iterator si)
2886 assert_locus *n, *loc, *last_loc;
2887 basic_block dest_bb;
2889 gcc_checking_assert (bb == NULL || e == NULL);
2891 if (e == NULL)
2892 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
2893 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
2895 /* Never build an assert comparing against an integer constant with
2896 TREE_OVERFLOW set. This confuses our undefined overflow warning
2897 machinery. */
2898 if (TREE_OVERFLOW_P (val))
2899 val = drop_tree_overflow (val);
2901 /* The new assertion A will be inserted at BB or E. We need to
2902 determine if the new location is dominated by a previously
2903 registered location for A. If we are doing an edge insertion,
2904 assume that A will be inserted at E->DEST. Note that this is not
2905 necessarily true.
2907 If E is a critical edge, it will be split. But even if E is
2908 split, the new block will dominate the same set of blocks that
2909 E->DEST dominates.
2911 The reverse, however, is not true, blocks dominated by E->DEST
2912 will not be dominated by the new block created to split E. So,
2913 if the insertion location is on a critical edge, we will not use
2914 the new location to move another assertion previously registered
2915 at a block dominated by E->DEST. */
2916 dest_bb = (bb) ? bb : e->dest;
2918 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
2919 VAL at a block dominating DEST_BB, then we don't need to insert a new
2920 one. Similarly, if the same assertion already exists at a block
2921 dominated by DEST_BB and the new location is not on a critical
2922 edge, then update the existing location for the assertion (i.e.,
2923 move the assertion up in the dominance tree).
2925 Note, this is implemented as a simple linked list because there
2926 should not be more than a handful of assertions registered per
2927 name. If this becomes a performance problem, a table hashed by
2928 COMP_CODE and VAL could be implemented. */
2929 loc = asserts_for[SSA_NAME_VERSION (name)];
2930 last_loc = loc;
2931 while (loc)
2933 if (loc->comp_code == comp_code
2934 && (loc->val == val
2935 || operand_equal_p (loc->val, val, 0))
2936 && (loc->expr == expr
2937 || operand_equal_p (loc->expr, expr, 0)))
2939 /* If E is not a critical edge and DEST_BB
2940 dominates the existing location for the assertion, move
2941 the assertion up in the dominance tree by updating its
2942 location information. */
2943 if ((e == NULL || !EDGE_CRITICAL_P (e))
2944 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
2946 loc->bb = dest_bb;
2947 loc->e = e;
2948 loc->si = si;
2949 return;
2953 /* Update the last node of the list and move to the next one. */
2954 last_loc = loc;
2955 loc = loc->next;
2958 /* If we didn't find an assertion already registered for
2959 NAME COMP_CODE VAL, add a new one at the end of the list of
2960 assertions associated with NAME. */
2961 n = XNEW (struct assert_locus);
2962 n->bb = dest_bb;
2963 n->e = e;
2964 n->si = si;
2965 n->comp_code = comp_code;
2966 n->val = val;
2967 n->expr = expr;
2968 n->next = NULL;
2970 if (last_loc)
2971 last_loc->next = n;
2972 else
2973 asserts_for[SSA_NAME_VERSION (name)] = n;
2975 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
2978 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
2979 Extract a suitable test code and value and store them into *CODE_P and
2980 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
2982 If no extraction was possible, return FALSE, otherwise return TRUE.
2984 If INVERT is true, then we invert the result stored into *CODE_P. */
2986 static bool
2987 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
2988 tree cond_op0, tree cond_op1,
2989 bool invert, enum tree_code *code_p,
2990 tree *val_p)
2992 enum tree_code comp_code;
2993 tree val;
2995 /* Otherwise, we have a comparison of the form NAME COMP VAL
2996 or VAL COMP NAME. */
2997 if (name == cond_op1)
2999 /* If the predicate is of the form VAL COMP NAME, flip
3000 COMP around because we need to register NAME as the
3001 first operand in the predicate. */
3002 comp_code = swap_tree_comparison (cond_code);
3003 val = cond_op0;
3005 else if (name == cond_op0)
3007 /* The comparison is of the form NAME COMP VAL, so the
3008 comparison code remains unchanged. */
3009 comp_code = cond_code;
3010 val = cond_op1;
3012 else
3013 gcc_unreachable ();
3015 /* Invert the comparison code as necessary. */
3016 if (invert)
3017 comp_code = invert_tree_comparison (comp_code, 0);
3019 /* VRP only handles integral and pointer types. */
3020 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
3021 && ! POINTER_TYPE_P (TREE_TYPE (val)))
3022 return false;
3024 /* Do not register always-false predicates.
3025 FIXME: this works around a limitation in fold() when dealing with
3026 enumerations. Given 'enum { N1, N2 } x;', fold will not
3027 fold 'if (x > N2)' to 'if (0)'. */
3028 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
3029 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
3031 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
3032 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
3034 if (comp_code == GT_EXPR
3035 && (!max
3036 || compare_values (val, max) == 0))
3037 return false;
3039 if (comp_code == LT_EXPR
3040 && (!min
3041 || compare_values (val, min) == 0))
3042 return false;
3044 *code_p = comp_code;
3045 *val_p = val;
3046 return true;
3049 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
3050 (otherwise return VAL). VAL and MASK must be zero-extended for
3051 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
3052 (to transform signed values into unsigned) and at the end xor
3053 SGNBIT back. */
3055 static wide_int
3056 masked_increment (const wide_int &val_in, const wide_int &mask,
3057 const wide_int &sgnbit, unsigned int prec)
3059 wide_int bit = wi::one (prec), res;
3060 unsigned int i;
3062 wide_int val = val_in ^ sgnbit;
3063 for (i = 0; i < prec; i++, bit += bit)
3065 res = mask;
3066 if ((res & bit) == 0)
3067 continue;
3068 res = bit - 1;
3069 res = wi::bit_and_not (val + bit, res);
3070 res &= mask;
3071 if (wi::gtu_p (res, val))
3072 return res ^ sgnbit;
3074 return val ^ sgnbit;
3077 /* Helper for overflow_comparison_p
3079 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
3080 OP1's defining statement to see if it ultimately has the form
3081 OP0 CODE (OP0 PLUS INTEGER_CST)
3083 If so, return TRUE indicating this is an overflow test and store into
3084 *NEW_CST an updated constant that can be used in a narrowed range test.
3086 REVERSED indicates if the comparison was originally:
3088 OP1 CODE' OP0.
3090 This affects how we build the updated constant. */
3092 static bool
3093 overflow_comparison_p_1 (enum tree_code code, tree op0, tree op1,
3094 bool follow_assert_exprs, bool reversed, tree *new_cst)
3096 /* See if this is a relational operation between two SSA_NAMES with
3097 unsigned, overflow wrapping values. If so, check it more deeply. */
3098 if ((code == LT_EXPR || code == LE_EXPR
3099 || code == GE_EXPR || code == GT_EXPR)
3100 && TREE_CODE (op0) == SSA_NAME
3101 && TREE_CODE (op1) == SSA_NAME
3102 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
3103 && TYPE_UNSIGNED (TREE_TYPE (op0))
3104 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
3106 gimple *op1_def = SSA_NAME_DEF_STMT (op1);
3108 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
3109 if (follow_assert_exprs)
3111 while (gimple_assign_single_p (op1_def)
3112 && TREE_CODE (gimple_assign_rhs1 (op1_def)) == ASSERT_EXPR)
3114 op1 = TREE_OPERAND (gimple_assign_rhs1 (op1_def), 0);
3115 if (TREE_CODE (op1) != SSA_NAME)
3116 break;
3117 op1_def = SSA_NAME_DEF_STMT (op1);
3121 /* Now look at the defining statement of OP1 to see if it adds
3122 or subtracts a nonzero constant from another operand. */
3123 if (op1_def
3124 && is_gimple_assign (op1_def)
3125 && gimple_assign_rhs_code (op1_def) == PLUS_EXPR
3126 && TREE_CODE (gimple_assign_rhs2 (op1_def)) == INTEGER_CST
3127 && !integer_zerop (gimple_assign_rhs2 (op1_def)))
3129 tree target = gimple_assign_rhs1 (op1_def);
3131 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
3132 for one where TARGET appears on the RHS. */
3133 if (follow_assert_exprs)
3135 /* Now see if that "other operand" is op0, following the chain
3136 of ASSERT_EXPRs if necessary. */
3137 gimple *op0_def = SSA_NAME_DEF_STMT (op0);
3138 while (op0 != target
3139 && gimple_assign_single_p (op0_def)
3140 && TREE_CODE (gimple_assign_rhs1 (op0_def)) == ASSERT_EXPR)
3142 op0 = TREE_OPERAND (gimple_assign_rhs1 (op0_def), 0);
3143 if (TREE_CODE (op0) != SSA_NAME)
3144 break;
3145 op0_def = SSA_NAME_DEF_STMT (op0);
3149 /* If we did not find our target SSA_NAME, then this is not
3150 an overflow test. */
3151 if (op0 != target)
3152 return false;
3154 tree type = TREE_TYPE (op0);
3155 wide_int max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
3156 tree inc = gimple_assign_rhs2 (op1_def);
3157 if (reversed)
3158 *new_cst = wide_int_to_tree (type, max + wi::to_wide (inc));
3159 else
3160 *new_cst = wide_int_to_tree (type, max - wi::to_wide (inc));
3161 return true;
3164 return false;
3167 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
3168 OP1's defining statement to see if it ultimately has the form
3169 OP0 CODE (OP0 PLUS INTEGER_CST)
3171 If so, return TRUE indicating this is an overflow test and store into
3172 *NEW_CST an updated constant that can be used in a narrowed range test.
3174 These statements are left as-is in the IL to facilitate discovery of
3175 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
3176 the alternate range representation is often useful within VRP. */
3178 bool
3179 overflow_comparison_p (tree_code code, tree name, tree val,
3180 bool use_equiv_p, tree *new_cst)
3182 if (overflow_comparison_p_1 (code, name, val, use_equiv_p, false, new_cst))
3183 return true;
3184 return overflow_comparison_p_1 (swap_tree_comparison (code), val, name,
3185 use_equiv_p, true, new_cst);
3189 /* Try to register an edge assertion for SSA name NAME on edge E for
3190 the condition COND contributing to the conditional jump pointed to by BSI.
3191 Invert the condition COND if INVERT is true. */
3193 static void
3194 register_edge_assert_for_2 (tree name, edge e,
3195 enum tree_code cond_code,
3196 tree cond_op0, tree cond_op1, bool invert,
3197 vec<assert_info> &asserts)
3199 tree val;
3200 enum tree_code comp_code;
3202 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
3203 cond_op0,
3204 cond_op1,
3205 invert, &comp_code, &val))
3206 return;
3208 /* Queue the assert. */
3209 tree x;
3210 if (overflow_comparison_p (comp_code, name, val, false, &x))
3212 enum tree_code new_code = ((comp_code == GT_EXPR || comp_code == GE_EXPR)
3213 ? GT_EXPR : LE_EXPR);
3214 add_assert_info (asserts, name, name, new_code, x);
3216 add_assert_info (asserts, name, name, comp_code, val);
3218 /* In the case of NAME <= CST and NAME being defined as
3219 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
3220 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
3221 This catches range and anti-range tests. */
3222 if ((comp_code == LE_EXPR
3223 || comp_code == GT_EXPR)
3224 && TREE_CODE (val) == INTEGER_CST
3225 && TYPE_UNSIGNED (TREE_TYPE (val)))
3227 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
3228 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
3230 /* Extract CST2 from the (optional) addition. */
3231 if (is_gimple_assign (def_stmt)
3232 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
3234 name2 = gimple_assign_rhs1 (def_stmt);
3235 cst2 = gimple_assign_rhs2 (def_stmt);
3236 if (TREE_CODE (name2) == SSA_NAME
3237 && TREE_CODE (cst2) == INTEGER_CST)
3238 def_stmt = SSA_NAME_DEF_STMT (name2);
3241 /* Extract NAME2 from the (optional) sign-changing cast. */
3242 if (gimple_assign_cast_p (def_stmt))
3244 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
3245 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
3246 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
3247 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
3248 name3 = gimple_assign_rhs1 (def_stmt);
3251 /* If name3 is used later, create an ASSERT_EXPR for it. */
3252 if (name3 != NULL_TREE
3253 && TREE_CODE (name3) == SSA_NAME
3254 && (cst2 == NULL_TREE
3255 || TREE_CODE (cst2) == INTEGER_CST)
3256 && INTEGRAL_TYPE_P (TREE_TYPE (name3)))
3258 tree tmp;
3260 /* Build an expression for the range test. */
3261 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
3262 if (cst2 != NULL_TREE)
3263 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
3265 if (dump_file)
3267 fprintf (dump_file, "Adding assert for ");
3268 print_generic_expr (dump_file, name3);
3269 fprintf (dump_file, " from ");
3270 print_generic_expr (dump_file, tmp);
3271 fprintf (dump_file, "\n");
3274 add_assert_info (asserts, name3, tmp, comp_code, val);
3277 /* If name2 is used later, create an ASSERT_EXPR for it. */
3278 if (name2 != NULL_TREE
3279 && TREE_CODE (name2) == SSA_NAME
3280 && TREE_CODE (cst2) == INTEGER_CST
3281 && INTEGRAL_TYPE_P (TREE_TYPE (name2)))
3283 tree tmp;
3285 /* Build an expression for the range test. */
3286 tmp = name2;
3287 if (TREE_TYPE (name) != TREE_TYPE (name2))
3288 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
3289 if (cst2 != NULL_TREE)
3290 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
3292 if (dump_file)
3294 fprintf (dump_file, "Adding assert for ");
3295 print_generic_expr (dump_file, name2);
3296 fprintf (dump_file, " from ");
3297 print_generic_expr (dump_file, tmp);
3298 fprintf (dump_file, "\n");
3301 add_assert_info (asserts, name2, tmp, comp_code, val);
3305 /* In the case of post-in/decrement tests like if (i++) ... and uses
3306 of the in/decremented value on the edge the extra name we want to
3307 assert for is not on the def chain of the name compared. Instead
3308 it is in the set of use stmts.
3309 Similar cases happen for conversions that were simplified through
3310 fold_{sign_changed,widened}_comparison. */
3311 if ((comp_code == NE_EXPR
3312 || comp_code == EQ_EXPR)
3313 && TREE_CODE (val) == INTEGER_CST)
3315 imm_use_iterator ui;
3316 gimple *use_stmt;
3317 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
3319 if (!is_gimple_assign (use_stmt))
3320 continue;
3322 /* Cut off to use-stmts that are dominating the predecessor. */
3323 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
3324 continue;
3326 tree name2 = gimple_assign_lhs (use_stmt);
3327 if (TREE_CODE (name2) != SSA_NAME)
3328 continue;
3330 enum tree_code code = gimple_assign_rhs_code (use_stmt);
3331 tree cst;
3332 if (code == PLUS_EXPR
3333 || code == MINUS_EXPR)
3335 cst = gimple_assign_rhs2 (use_stmt);
3336 if (TREE_CODE (cst) != INTEGER_CST)
3337 continue;
3338 cst = int_const_binop (code, val, cst);
3340 else if (CONVERT_EXPR_CODE_P (code))
3342 /* For truncating conversions we cannot record
3343 an inequality. */
3344 if (comp_code == NE_EXPR
3345 && (TYPE_PRECISION (TREE_TYPE (name2))
3346 < TYPE_PRECISION (TREE_TYPE (name))))
3347 continue;
3348 cst = fold_convert (TREE_TYPE (name2), val);
3350 else
3351 continue;
3353 if (TREE_OVERFLOW_P (cst))
3354 cst = drop_tree_overflow (cst);
3355 add_assert_info (asserts, name2, name2, comp_code, cst);
3359 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
3360 && TREE_CODE (val) == INTEGER_CST)
3362 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
3363 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
3364 tree val2 = NULL_TREE;
3365 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
3366 wide_int mask = wi::zero (prec);
3367 unsigned int nprec = prec;
3368 enum tree_code rhs_code = ERROR_MARK;
3370 if (is_gimple_assign (def_stmt))
3371 rhs_code = gimple_assign_rhs_code (def_stmt);
3373 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
3374 assert that A != CST1 -+ CST2. */
3375 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
3376 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
3378 tree op0 = gimple_assign_rhs1 (def_stmt);
3379 tree op1 = gimple_assign_rhs2 (def_stmt);
3380 if (TREE_CODE (op0) == SSA_NAME
3381 && TREE_CODE (op1) == INTEGER_CST)
3383 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
3384 ? MINUS_EXPR : PLUS_EXPR);
3385 op1 = int_const_binop (reverse_op, val, op1);
3386 if (TREE_OVERFLOW (op1))
3387 op1 = drop_tree_overflow (op1);
3388 add_assert_info (asserts, op0, op0, comp_code, op1);
3392 /* Add asserts for NAME cmp CST and NAME being defined
3393 as NAME = (int) NAME2. */
3394 if (!TYPE_UNSIGNED (TREE_TYPE (val))
3395 && (comp_code == LE_EXPR || comp_code == LT_EXPR
3396 || comp_code == GT_EXPR || comp_code == GE_EXPR)
3397 && gimple_assign_cast_p (def_stmt))
3399 name2 = gimple_assign_rhs1 (def_stmt);
3400 if (CONVERT_EXPR_CODE_P (rhs_code)
3401 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
3402 && TYPE_UNSIGNED (TREE_TYPE (name2))
3403 && prec == TYPE_PRECISION (TREE_TYPE (name2))
3404 && (comp_code == LE_EXPR || comp_code == GT_EXPR
3405 || !tree_int_cst_equal (val,
3406 TYPE_MIN_VALUE (TREE_TYPE (val)))))
3408 tree tmp, cst;
3409 enum tree_code new_comp_code = comp_code;
3411 cst = fold_convert (TREE_TYPE (name2),
3412 TYPE_MIN_VALUE (TREE_TYPE (val)));
3413 /* Build an expression for the range test. */
3414 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
3415 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
3416 fold_convert (TREE_TYPE (name2), val));
3417 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
3419 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
3420 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
3421 build_int_cst (TREE_TYPE (name2), 1));
3424 if (dump_file)
3426 fprintf (dump_file, "Adding assert for ");
3427 print_generic_expr (dump_file, name2);
3428 fprintf (dump_file, " from ");
3429 print_generic_expr (dump_file, tmp);
3430 fprintf (dump_file, "\n");
3433 add_assert_info (asserts, name2, tmp, new_comp_code, cst);
3437 /* Add asserts for NAME cmp CST and NAME being defined as
3438 NAME = NAME2 >> CST2.
3440 Extract CST2 from the right shift. */
3441 if (rhs_code == RSHIFT_EXPR)
3443 name2 = gimple_assign_rhs1 (def_stmt);
3444 cst2 = gimple_assign_rhs2 (def_stmt);
3445 if (TREE_CODE (name2) == SSA_NAME
3446 && tree_fits_uhwi_p (cst2)
3447 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
3448 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
3449 && type_has_mode_precision_p (TREE_TYPE (val)))
3451 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
3452 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
3455 if (val2 != NULL_TREE
3456 && TREE_CODE (val2) == INTEGER_CST
3457 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
3458 TREE_TYPE (val),
3459 val2, cst2), val))
3461 enum tree_code new_comp_code = comp_code;
3462 tree tmp, new_val;
3464 tmp = name2;
3465 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
3467 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
3469 tree type = build_nonstandard_integer_type (prec, 1);
3470 tmp = build1 (NOP_EXPR, type, name2);
3471 val2 = fold_convert (type, val2);
3473 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
3474 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
3475 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
3477 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
3479 wide_int minval
3480 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
3481 new_val = val2;
3482 if (minval == wi::to_wide (new_val))
3483 new_val = NULL_TREE;
3485 else
3487 wide_int maxval
3488 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
3489 mask |= wi::to_wide (val2);
3490 if (wi::eq_p (mask, maxval))
3491 new_val = NULL_TREE;
3492 else
3493 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
3496 if (new_val)
3498 if (dump_file)
3500 fprintf (dump_file, "Adding assert for ");
3501 print_generic_expr (dump_file, name2);
3502 fprintf (dump_file, " from ");
3503 print_generic_expr (dump_file, tmp);
3504 fprintf (dump_file, "\n");
3507 add_assert_info (asserts, name2, tmp, new_comp_code, new_val);
3511 /* Add asserts for NAME cmp CST and NAME being defined as
3512 NAME = NAME2 & CST2.
3514 Extract CST2 from the and.
3516 Also handle
3517 NAME = (unsigned) NAME2;
3518 casts where NAME's type is unsigned and has smaller precision
3519 than NAME2's type as if it was NAME = NAME2 & MASK. */
3520 names[0] = NULL_TREE;
3521 names[1] = NULL_TREE;
3522 cst2 = NULL_TREE;
3523 if (rhs_code == BIT_AND_EXPR
3524 || (CONVERT_EXPR_CODE_P (rhs_code)
3525 && INTEGRAL_TYPE_P (TREE_TYPE (val))
3526 && TYPE_UNSIGNED (TREE_TYPE (val))
3527 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
3528 > prec))
3530 name2 = gimple_assign_rhs1 (def_stmt);
3531 if (rhs_code == BIT_AND_EXPR)
3532 cst2 = gimple_assign_rhs2 (def_stmt);
3533 else
3535 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
3536 nprec = TYPE_PRECISION (TREE_TYPE (name2));
3538 if (TREE_CODE (name2) == SSA_NAME
3539 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
3540 && TREE_CODE (cst2) == INTEGER_CST
3541 && !integer_zerop (cst2)
3542 && (nprec > 1
3543 || TYPE_UNSIGNED (TREE_TYPE (val))))
3545 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
3546 if (gimple_assign_cast_p (def_stmt2))
3548 names[1] = gimple_assign_rhs1 (def_stmt2);
3549 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
3550 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
3551 || (TYPE_PRECISION (TREE_TYPE (name2))
3552 != TYPE_PRECISION (TREE_TYPE (names[1]))))
3553 names[1] = NULL_TREE;
3555 names[0] = name2;
3558 if (names[0] || names[1])
3560 wide_int minv, maxv, valv, cst2v;
3561 wide_int tem, sgnbit;
3562 bool valid_p = false, valn, cst2n;
3563 enum tree_code ccode = comp_code;
3565 valv = wide_int::from (wi::to_wide (val), nprec, UNSIGNED);
3566 cst2v = wide_int::from (wi::to_wide (cst2), nprec, UNSIGNED);
3567 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
3568 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
3569 /* If CST2 doesn't have most significant bit set,
3570 but VAL is negative, we have comparison like
3571 if ((x & 0x123) > -4) (always true). Just give up. */
3572 if (!cst2n && valn)
3573 ccode = ERROR_MARK;
3574 if (cst2n)
3575 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
3576 else
3577 sgnbit = wi::zero (nprec);
3578 minv = valv & cst2v;
3579 switch (ccode)
3581 case EQ_EXPR:
3582 /* Minimum unsigned value for equality is VAL & CST2
3583 (should be equal to VAL, otherwise we probably should
3584 have folded the comparison into false) and
3585 maximum unsigned value is VAL | ~CST2. */
3586 maxv = valv | ~cst2v;
3587 valid_p = true;
3588 break;
3590 case NE_EXPR:
3591 tem = valv | ~cst2v;
3592 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
3593 if (valv == 0)
3595 cst2n = false;
3596 sgnbit = wi::zero (nprec);
3597 goto gt_expr;
3599 /* If (VAL | ~CST2) is all ones, handle it as
3600 (X & CST2) < VAL. */
3601 if (tem == -1)
3603 cst2n = false;
3604 valn = false;
3605 sgnbit = wi::zero (nprec);
3606 goto lt_expr;
3608 if (!cst2n && wi::neg_p (cst2v))
3609 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
3610 if (sgnbit != 0)
3612 if (valv == sgnbit)
3614 cst2n = true;
3615 valn = true;
3616 goto gt_expr;
3618 if (tem == wi::mask (nprec - 1, false, nprec))
3620 cst2n = true;
3621 goto lt_expr;
3623 if (!cst2n)
3624 sgnbit = wi::zero (nprec);
3626 break;
3628 case GE_EXPR:
3629 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
3630 is VAL and maximum unsigned value is ~0. For signed
3631 comparison, if CST2 doesn't have most significant bit
3632 set, handle it similarly. If CST2 has MSB set,
3633 the minimum is the same, and maximum is ~0U/2. */
3634 if (minv != valv)
3636 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
3637 VAL. */
3638 minv = masked_increment (valv, cst2v, sgnbit, nprec);
3639 if (minv == valv)
3640 break;
3642 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
3643 valid_p = true;
3644 break;
3646 case GT_EXPR:
3647 gt_expr:
3648 /* Find out smallest MINV where MINV > VAL
3649 && (MINV & CST2) == MINV, if any. If VAL is signed and
3650 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
3651 minv = masked_increment (valv, cst2v, sgnbit, nprec);
3652 if (minv == valv)
3653 break;
3654 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
3655 valid_p = true;
3656 break;
3658 case LE_EXPR:
3659 /* Minimum unsigned value for <= is 0 and maximum
3660 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
3661 Otherwise, find smallest VAL2 where VAL2 > VAL
3662 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3663 as maximum.
3664 For signed comparison, if CST2 doesn't have most
3665 significant bit set, handle it similarly. If CST2 has
3666 MSB set, the maximum is the same and minimum is INT_MIN. */
3667 if (minv == valv)
3668 maxv = valv;
3669 else
3671 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
3672 if (maxv == valv)
3673 break;
3674 maxv -= 1;
3676 maxv |= ~cst2v;
3677 minv = sgnbit;
3678 valid_p = true;
3679 break;
3681 case LT_EXPR:
3682 lt_expr:
3683 /* Minimum unsigned value for < is 0 and maximum
3684 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
3685 Otherwise, find smallest VAL2 where VAL2 > VAL
3686 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3687 as maximum.
3688 For signed comparison, if CST2 doesn't have most
3689 significant bit set, handle it similarly. If CST2 has
3690 MSB set, the maximum is the same and minimum is INT_MIN. */
3691 if (minv == valv)
3693 if (valv == sgnbit)
3694 break;
3695 maxv = valv;
3697 else
3699 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
3700 if (maxv == valv)
3701 break;
3703 maxv -= 1;
3704 maxv |= ~cst2v;
3705 minv = sgnbit;
3706 valid_p = true;
3707 break;
3709 default:
3710 break;
3712 if (valid_p
3713 && (maxv - minv) != -1)
3715 tree tmp, new_val, type;
3716 int i;
3718 for (i = 0; i < 2; i++)
3719 if (names[i])
3721 wide_int maxv2 = maxv;
3722 tmp = names[i];
3723 type = TREE_TYPE (names[i]);
3724 if (!TYPE_UNSIGNED (type))
3726 type = build_nonstandard_integer_type (nprec, 1);
3727 tmp = build1 (NOP_EXPR, type, names[i]);
3729 if (minv != 0)
3731 tmp = build2 (PLUS_EXPR, type, tmp,
3732 wide_int_to_tree (type, -minv));
3733 maxv2 = maxv - minv;
3735 new_val = wide_int_to_tree (type, maxv2);
3737 if (dump_file)
3739 fprintf (dump_file, "Adding assert for ");
3740 print_generic_expr (dump_file, names[i]);
3741 fprintf (dump_file, " from ");
3742 print_generic_expr (dump_file, tmp);
3743 fprintf (dump_file, "\n");
3746 add_assert_info (asserts, names[i], tmp, LE_EXPR, new_val);
3753 /* OP is an operand of a truth value expression which is known to have
3754 a particular value. Register any asserts for OP and for any
3755 operands in OP's defining statement.
3757 If CODE is EQ_EXPR, then we want to register OP is zero (false),
3758 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
3760 static void
3761 register_edge_assert_for_1 (tree op, enum tree_code code,
3762 edge e, vec<assert_info> &asserts)
3764 gimple *op_def;
3765 tree val;
3766 enum tree_code rhs_code;
3768 /* We only care about SSA_NAMEs. */
3769 if (TREE_CODE (op) != SSA_NAME)
3770 return;
3772 /* We know that OP will have a zero or nonzero value. */
3773 val = build_int_cst (TREE_TYPE (op), 0);
3774 add_assert_info (asserts, op, op, code, val);
3776 /* Now look at how OP is set. If it's set from a comparison,
3777 a truth operation or some bit operations, then we may be able
3778 to register information about the operands of that assignment. */
3779 op_def = SSA_NAME_DEF_STMT (op);
3780 if (gimple_code (op_def) != GIMPLE_ASSIGN)
3781 return;
3783 rhs_code = gimple_assign_rhs_code (op_def);
3785 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
3787 bool invert = (code == EQ_EXPR ? true : false);
3788 tree op0 = gimple_assign_rhs1 (op_def);
3789 tree op1 = gimple_assign_rhs2 (op_def);
3791 if (TREE_CODE (op0) == SSA_NAME)
3792 register_edge_assert_for_2 (op0, e, rhs_code, op0, op1, invert, asserts);
3793 if (TREE_CODE (op1) == SSA_NAME)
3794 register_edge_assert_for_2 (op1, e, rhs_code, op0, op1, invert, asserts);
3796 else if ((code == NE_EXPR
3797 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
3798 || (code == EQ_EXPR
3799 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
3801 /* Recurse on each operand. */
3802 tree op0 = gimple_assign_rhs1 (op_def);
3803 tree op1 = gimple_assign_rhs2 (op_def);
3804 if (TREE_CODE (op0) == SSA_NAME
3805 && has_single_use (op0))
3806 register_edge_assert_for_1 (op0, code, e, asserts);
3807 if (TREE_CODE (op1) == SSA_NAME
3808 && has_single_use (op1))
3809 register_edge_assert_for_1 (op1, code, e, asserts);
3811 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
3812 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
3814 /* Recurse, flipping CODE. */
3815 code = invert_tree_comparison (code, false);
3816 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
3818 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
3820 /* Recurse through the copy. */
3821 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
3823 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
3825 /* Recurse through the type conversion, unless it is a narrowing
3826 conversion or conversion from non-integral type. */
3827 tree rhs = gimple_assign_rhs1 (op_def);
3828 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
3829 && (TYPE_PRECISION (TREE_TYPE (rhs))
3830 <= TYPE_PRECISION (TREE_TYPE (op))))
3831 register_edge_assert_for_1 (rhs, code, e, asserts);
3835 /* Check if comparison
3836 NAME COND_OP INTEGER_CST
3837 has a form of
3838 (X & 11...100..0) COND_OP XX...X00...0
3839 Such comparison can yield assertions like
3840 X >= XX...X00...0
3841 X <= XX...X11...1
3842 in case of COND_OP being NE_EXPR or
3843 X < XX...X00...0
3844 X > XX...X11...1
3845 in case of EQ_EXPR. */
3847 static bool
3848 is_masked_range_test (tree name, tree valt, enum tree_code cond_code,
3849 tree *new_name, tree *low, enum tree_code *low_code,
3850 tree *high, enum tree_code *high_code)
3852 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
3854 if (!is_gimple_assign (def_stmt)
3855 || gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
3856 return false;
3858 tree t = gimple_assign_rhs1 (def_stmt);
3859 tree maskt = gimple_assign_rhs2 (def_stmt);
3860 if (TREE_CODE (t) != SSA_NAME || TREE_CODE (maskt) != INTEGER_CST)
3861 return false;
3863 wi::tree_to_wide_ref mask = wi::to_wide (maskt);
3864 wide_int inv_mask = ~mask;
3865 /* Assume VALT is INTEGER_CST. */
3866 wi::tree_to_wide_ref val = wi::to_wide (valt);
3868 if ((inv_mask & (inv_mask + 1)) != 0
3869 || (val & mask) != val)
3870 return false;
3872 bool is_range = cond_code == EQ_EXPR;
3874 tree type = TREE_TYPE (t);
3875 wide_int min = wi::min_value (type),
3876 max = wi::max_value (type);
3878 if (is_range)
3880 *low_code = val == min ? ERROR_MARK : GE_EXPR;
3881 *high_code = val == max ? ERROR_MARK : LE_EXPR;
3883 else
3885 /* We can still generate assertion if one of alternatives
3886 is known to always be false. */
3887 if (val == min)
3889 *low_code = (enum tree_code) 0;
3890 *high_code = GT_EXPR;
3892 else if ((val | inv_mask) == max)
3894 *low_code = LT_EXPR;
3895 *high_code = (enum tree_code) 0;
3897 else
3898 return false;
3901 *new_name = t;
3902 *low = wide_int_to_tree (type, val);
3903 *high = wide_int_to_tree (type, val | inv_mask);
3905 if (wi::neg_p (val, TYPE_SIGN (type)))
3906 std::swap (*low, *high);
3908 return true;
3911 /* Try to register an edge assertion for SSA name NAME on edge E for
3912 the condition COND contributing to the conditional jump pointed to by
3913 SI. */
3915 void
3916 register_edge_assert_for (tree name, edge e,
3917 enum tree_code cond_code, tree cond_op0,
3918 tree cond_op1, vec<assert_info> &asserts)
3920 tree val;
3921 enum tree_code comp_code;
3922 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
3924 /* Do not attempt to infer anything in names that flow through
3925 abnormal edges. */
3926 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
3927 return;
3929 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
3930 cond_op0, cond_op1,
3931 is_else_edge,
3932 &comp_code, &val))
3933 return;
3935 /* Register ASSERT_EXPRs for name. */
3936 register_edge_assert_for_2 (name, e, cond_code, cond_op0,
3937 cond_op1, is_else_edge, asserts);
3940 /* If COND is effectively an equality test of an SSA_NAME against
3941 the value zero or one, then we may be able to assert values
3942 for SSA_NAMEs which flow into COND. */
3944 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
3945 statement of NAME we can assert both operands of the BIT_AND_EXPR
3946 have nonzero value. */
3947 if (((comp_code == EQ_EXPR && integer_onep (val))
3948 || (comp_code == NE_EXPR && integer_zerop (val))))
3950 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
3952 if (is_gimple_assign (def_stmt)
3953 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
3955 tree op0 = gimple_assign_rhs1 (def_stmt);
3956 tree op1 = gimple_assign_rhs2 (def_stmt);
3957 register_edge_assert_for_1 (op0, NE_EXPR, e, asserts);
3958 register_edge_assert_for_1 (op1, NE_EXPR, e, asserts);
3962 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
3963 statement of NAME we can assert both operands of the BIT_IOR_EXPR
3964 have zero value. */
3965 if (((comp_code == EQ_EXPR && integer_zerop (val))
3966 || (comp_code == NE_EXPR && integer_onep (val))))
3968 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
3970 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
3971 necessarily zero value, or if type-precision is one. */
3972 if (is_gimple_assign (def_stmt)
3973 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
3974 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
3975 || comp_code == EQ_EXPR)))
3977 tree op0 = gimple_assign_rhs1 (def_stmt);
3978 tree op1 = gimple_assign_rhs2 (def_stmt);
3979 register_edge_assert_for_1 (op0, EQ_EXPR, e, asserts);
3980 register_edge_assert_for_1 (op1, EQ_EXPR, e, asserts);
3984 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
3985 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
3986 && TREE_CODE (val) == INTEGER_CST)
3988 enum tree_code low_code, high_code;
3989 tree low, high;
3990 if (is_masked_range_test (name, val, comp_code, &name, &low,
3991 &low_code, &high, &high_code))
3993 if (low_code != ERROR_MARK)
3994 register_edge_assert_for_2 (name, e, low_code, name,
3995 low, /*invert*/false, asserts);
3996 if (high_code != ERROR_MARK)
3997 register_edge_assert_for_2 (name, e, high_code, name,
3998 high, /*invert*/false, asserts);
4003 /* Finish found ASSERTS for E and register them at GSI. */
4005 static void
4006 finish_register_edge_assert_for (edge e, gimple_stmt_iterator gsi,
4007 vec<assert_info> &asserts)
4009 for (unsigned i = 0; i < asserts.length (); ++i)
4010 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4011 reachable from E. */
4012 if (live_on_edge (e, asserts[i].name))
4013 register_new_assert_for (asserts[i].name, asserts[i].expr,
4014 asserts[i].comp_code, asserts[i].val,
4015 NULL, e, gsi);
4020 /* Determine whether the outgoing edges of BB should receive an
4021 ASSERT_EXPR for each of the operands of BB's LAST statement.
4022 The last statement of BB must be a COND_EXPR.
4024 If any of the sub-graphs rooted at BB have an interesting use of
4025 the predicate operands, an assert location node is added to the
4026 list of assertions for the corresponding operands. */
4028 static void
4029 find_conditional_asserts (basic_block bb, gcond *last)
4031 gimple_stmt_iterator bsi;
4032 tree op;
4033 edge_iterator ei;
4034 edge e;
4035 ssa_op_iter iter;
4037 bsi = gsi_for_stmt (last);
4039 /* Look for uses of the operands in each of the sub-graphs
4040 rooted at BB. We need to check each of the outgoing edges
4041 separately, so that we know what kind of ASSERT_EXPR to
4042 insert. */
4043 FOR_EACH_EDGE (e, ei, bb->succs)
4045 if (e->dest == bb)
4046 continue;
4048 /* Register the necessary assertions for each operand in the
4049 conditional predicate. */
4050 auto_vec<assert_info, 8> asserts;
4051 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
4052 register_edge_assert_for (op, e,
4053 gimple_cond_code (last),
4054 gimple_cond_lhs (last),
4055 gimple_cond_rhs (last), asserts);
4056 finish_register_edge_assert_for (e, bsi, asserts);
4060 struct case_info
4062 tree expr;
4063 basic_block bb;
4066 /* Compare two case labels sorting first by the destination bb index
4067 and then by the case value. */
4069 static int
4070 compare_case_labels (const void *p1, const void *p2)
4072 const struct case_info *ci1 = (const struct case_info *) p1;
4073 const struct case_info *ci2 = (const struct case_info *) p2;
4074 int idx1 = ci1->bb->index;
4075 int idx2 = ci2->bb->index;
4077 if (idx1 < idx2)
4078 return -1;
4079 else if (idx1 == idx2)
4081 /* Make sure the default label is first in a group. */
4082 if (!CASE_LOW (ci1->expr))
4083 return -1;
4084 else if (!CASE_LOW (ci2->expr))
4085 return 1;
4086 else
4087 return tree_int_cst_compare (CASE_LOW (ci1->expr),
4088 CASE_LOW (ci2->expr));
4090 else
4091 return 1;
4094 /* Determine whether the outgoing edges of BB should receive an
4095 ASSERT_EXPR for each of the operands of BB's LAST statement.
4096 The last statement of BB must be a SWITCH_EXPR.
4098 If any of the sub-graphs rooted at BB have an interesting use of
4099 the predicate operands, an assert location node is added to the
4100 list of assertions for the corresponding operands. */
4102 static void
4103 find_switch_asserts (basic_block bb, gswitch *last)
4105 gimple_stmt_iterator bsi;
4106 tree op;
4107 edge e;
4108 struct case_info *ci;
4109 size_t n = gimple_switch_num_labels (last);
4110 #if GCC_VERSION >= 4000
4111 unsigned int idx;
4112 #else
4113 /* Work around GCC 3.4 bug (PR 37086). */
4114 volatile unsigned int idx;
4115 #endif
4117 bsi = gsi_for_stmt (last);
4118 op = gimple_switch_index (last);
4119 if (TREE_CODE (op) != SSA_NAME)
4120 return;
4122 /* Build a vector of case labels sorted by destination label. */
4123 ci = XNEWVEC (struct case_info, n);
4124 for (idx = 0; idx < n; ++idx)
4126 ci[idx].expr = gimple_switch_label (last, idx);
4127 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
4129 edge default_edge = find_edge (bb, ci[0].bb);
4130 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
4132 for (idx = 0; idx < n; ++idx)
4134 tree min, max;
4135 tree cl = ci[idx].expr;
4136 basic_block cbb = ci[idx].bb;
4138 min = CASE_LOW (cl);
4139 max = CASE_HIGH (cl);
4141 /* If there are multiple case labels with the same destination
4142 we need to combine them to a single value range for the edge. */
4143 if (idx + 1 < n && cbb == ci[idx + 1].bb)
4145 /* Skip labels until the last of the group. */
4146 do {
4147 ++idx;
4148 } while (idx < n && cbb == ci[idx].bb);
4149 --idx;
4151 /* Pick up the maximum of the case label range. */
4152 if (CASE_HIGH (ci[idx].expr))
4153 max = CASE_HIGH (ci[idx].expr);
4154 else
4155 max = CASE_LOW (ci[idx].expr);
4158 /* Can't extract a useful assertion out of a range that includes the
4159 default label. */
4160 if (min == NULL_TREE)
4161 continue;
4163 /* Find the edge to register the assert expr on. */
4164 e = find_edge (bb, cbb);
4166 /* Register the necessary assertions for the operand in the
4167 SWITCH_EXPR. */
4168 auto_vec<assert_info, 8> asserts;
4169 register_edge_assert_for (op, e,
4170 max ? GE_EXPR : EQ_EXPR,
4171 op, fold_convert (TREE_TYPE (op), min),
4172 asserts);
4173 if (max)
4174 register_edge_assert_for (op, e, LE_EXPR, op,
4175 fold_convert (TREE_TYPE (op), max),
4176 asserts);
4177 finish_register_edge_assert_for (e, bsi, asserts);
4180 XDELETEVEC (ci);
4182 if (!live_on_edge (default_edge, op))
4183 return;
4185 /* Now register along the default label assertions that correspond to the
4186 anti-range of each label. */
4187 int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS);
4188 if (insertion_limit == 0)
4189 return;
4191 /* We can't do this if the default case shares a label with another case. */
4192 tree default_cl = gimple_switch_default_label (last);
4193 for (idx = 1; idx < n; idx++)
4195 tree min, max;
4196 tree cl = gimple_switch_label (last, idx);
4197 if (CASE_LABEL (cl) == CASE_LABEL (default_cl))
4198 continue;
4200 min = CASE_LOW (cl);
4201 max = CASE_HIGH (cl);
4203 /* Combine contiguous case ranges to reduce the number of assertions
4204 to insert. */
4205 for (idx = idx + 1; idx < n; idx++)
4207 tree next_min, next_max;
4208 tree next_cl = gimple_switch_label (last, idx);
4209 if (CASE_LABEL (next_cl) == CASE_LABEL (default_cl))
4210 break;
4212 next_min = CASE_LOW (next_cl);
4213 next_max = CASE_HIGH (next_cl);
4215 wide_int difference = (wi::to_wide (next_min)
4216 - wi::to_wide (max ? max : min));
4217 if (wi::eq_p (difference, 1))
4218 max = next_max ? next_max : next_min;
4219 else
4220 break;
4222 idx--;
4224 if (max == NULL_TREE)
4226 /* Register the assertion OP != MIN. */
4227 auto_vec<assert_info, 8> asserts;
4228 min = fold_convert (TREE_TYPE (op), min);
4229 register_edge_assert_for (op, default_edge, NE_EXPR, op, min,
4230 asserts);
4231 finish_register_edge_assert_for (default_edge, bsi, asserts);
4233 else
4235 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
4236 which will give OP the anti-range ~[MIN,MAX]. */
4237 tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op);
4238 min = fold_convert (TREE_TYPE (uop), min);
4239 max = fold_convert (TREE_TYPE (uop), max);
4241 tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min);
4242 tree rhs = int_const_binop (MINUS_EXPR, max, min);
4243 register_new_assert_for (op, lhs, GT_EXPR, rhs,
4244 NULL, default_edge, bsi);
4247 if (--insertion_limit == 0)
4248 break;
4253 /* Traverse all the statements in block BB looking for statements that
4254 may generate useful assertions for the SSA names in their operand.
4255 If a statement produces a useful assertion A for name N_i, then the
4256 list of assertions already generated for N_i is scanned to
4257 determine if A is actually needed.
4259 If N_i already had the assertion A at a location dominating the
4260 current location, then nothing needs to be done. Otherwise, the
4261 new location for A is recorded instead.
4263 1- For every statement S in BB, all the variables used by S are
4264 added to bitmap FOUND_IN_SUBGRAPH.
4266 2- If statement S uses an operand N in a way that exposes a known
4267 value range for N, then if N was not already generated by an
4268 ASSERT_EXPR, create a new assert location for N. For instance,
4269 if N is a pointer and the statement dereferences it, we can
4270 assume that N is not NULL.
4272 3- COND_EXPRs are a special case of #2. We can derive range
4273 information from the predicate but need to insert different
4274 ASSERT_EXPRs for each of the sub-graphs rooted at the
4275 conditional block. If the last statement of BB is a conditional
4276 expression of the form 'X op Y', then
4278 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
4280 b) If the conditional is the only entry point to the sub-graph
4281 corresponding to the THEN_CLAUSE, recurse into it. On
4282 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
4283 an ASSERT_EXPR is added for the corresponding variable.
4285 c) Repeat step (b) on the ELSE_CLAUSE.
4287 d) Mark X and Y in FOUND_IN_SUBGRAPH.
4289 For instance,
4291 if (a == 9)
4292 b = a;
4293 else
4294 b = c + 1;
4296 In this case, an assertion on the THEN clause is useful to
4297 determine that 'a' is always 9 on that edge. However, an assertion
4298 on the ELSE clause would be unnecessary.
4300 4- If BB does not end in a conditional expression, then we recurse
4301 into BB's dominator children.
4303 At the end of the recursive traversal, every SSA name will have a
4304 list of locations where ASSERT_EXPRs should be added. When a new
4305 location for name N is found, it is registered by calling
4306 register_new_assert_for. That function keeps track of all the
4307 registered assertions to prevent adding unnecessary assertions.
4308 For instance, if a pointer P_4 is dereferenced more than once in a
4309 dominator tree, only the location dominating all the dereference of
4310 P_4 will receive an ASSERT_EXPR. */
4312 static void
4313 find_assert_locations_1 (basic_block bb, sbitmap live)
4315 gimple *last;
4317 last = last_stmt (bb);
4319 /* If BB's last statement is a conditional statement involving integer
4320 operands, determine if we need to add ASSERT_EXPRs. */
4321 if (last
4322 && gimple_code (last) == GIMPLE_COND
4323 && !fp_predicate (last)
4324 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
4325 find_conditional_asserts (bb, as_a <gcond *> (last));
4327 /* If BB's last statement is a switch statement involving integer
4328 operands, determine if we need to add ASSERT_EXPRs. */
4329 if (last
4330 && gimple_code (last) == GIMPLE_SWITCH
4331 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
4332 find_switch_asserts (bb, as_a <gswitch *> (last));
4334 /* Traverse all the statements in BB marking used names and looking
4335 for statements that may infer assertions for their used operands. */
4336 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
4337 gsi_prev (&si))
4339 gimple *stmt;
4340 tree op;
4341 ssa_op_iter i;
4343 stmt = gsi_stmt (si);
4345 if (is_gimple_debug (stmt))
4346 continue;
4348 /* See if we can derive an assertion for any of STMT's operands. */
4349 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
4351 tree value;
4352 enum tree_code comp_code;
4354 /* If op is not live beyond this stmt, do not bother to insert
4355 asserts for it. */
4356 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
4357 continue;
4359 /* If OP is used in such a way that we can infer a value
4360 range for it, and we don't find a previous assertion for
4361 it, create a new assertion location node for OP. */
4362 if (infer_value_range (stmt, op, &comp_code, &value))
4364 /* If we are able to infer a nonzero value range for OP,
4365 then walk backwards through the use-def chain to see if OP
4366 was set via a typecast.
4368 If so, then we can also infer a nonzero value range
4369 for the operand of the NOP_EXPR. */
4370 if (comp_code == NE_EXPR && integer_zerop (value))
4372 tree t = op;
4373 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
4375 while (is_gimple_assign (def_stmt)
4376 && CONVERT_EXPR_CODE_P
4377 (gimple_assign_rhs_code (def_stmt))
4378 && TREE_CODE
4379 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
4380 && POINTER_TYPE_P
4381 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
4383 t = gimple_assign_rhs1 (def_stmt);
4384 def_stmt = SSA_NAME_DEF_STMT (t);
4386 /* Note we want to register the assert for the
4387 operand of the NOP_EXPR after SI, not after the
4388 conversion. */
4389 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
4390 register_new_assert_for (t, t, comp_code, value,
4391 bb, NULL, si);
4395 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
4399 /* Update live. */
4400 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
4401 bitmap_set_bit (live, SSA_NAME_VERSION (op));
4402 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
4403 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
4406 /* Traverse all PHI nodes in BB, updating live. */
4407 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
4408 gsi_next (&si))
4410 use_operand_p arg_p;
4411 ssa_op_iter i;
4412 gphi *phi = si.phi ();
4413 tree res = gimple_phi_result (phi);
4415 if (virtual_operand_p (res))
4416 continue;
4418 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
4420 tree arg = USE_FROM_PTR (arg_p);
4421 if (TREE_CODE (arg) == SSA_NAME)
4422 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
4425 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
4429 /* Do an RPO walk over the function computing SSA name liveness
4430 on-the-fly and deciding on assert expressions to insert. */
4432 static void
4433 find_assert_locations (void)
4435 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
4436 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
4437 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
4438 int rpo_cnt, i;
4440 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
4441 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
4442 for (i = 0; i < rpo_cnt; ++i)
4443 bb_rpo[rpo[i]] = i;
4445 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
4446 the order we compute liveness and insert asserts we otherwise
4447 fail to insert asserts into the loop latch. */
4448 loop_p loop;
4449 FOR_EACH_LOOP (loop, 0)
4451 i = loop->latch->index;
4452 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
4453 for (gphi_iterator gsi = gsi_start_phis (loop->header);
4454 !gsi_end_p (gsi); gsi_next (&gsi))
4456 gphi *phi = gsi.phi ();
4457 if (virtual_operand_p (gimple_phi_result (phi)))
4458 continue;
4459 tree arg = gimple_phi_arg_def (phi, j);
4460 if (TREE_CODE (arg) == SSA_NAME)
4462 if (live[i] == NULL)
4464 live[i] = sbitmap_alloc (num_ssa_names);
4465 bitmap_clear (live[i]);
4467 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
4472 for (i = rpo_cnt - 1; i >= 0; --i)
4474 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
4475 edge e;
4476 edge_iterator ei;
4478 if (!live[rpo[i]])
4480 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
4481 bitmap_clear (live[rpo[i]]);
4484 /* Process BB and update the live information with uses in
4485 this block. */
4486 find_assert_locations_1 (bb, live[rpo[i]]);
4488 /* Merge liveness into the predecessor blocks and free it. */
4489 if (!bitmap_empty_p (live[rpo[i]]))
4491 int pred_rpo = i;
4492 FOR_EACH_EDGE (e, ei, bb->preds)
4494 int pred = e->src->index;
4495 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
4496 continue;
4498 if (!live[pred])
4500 live[pred] = sbitmap_alloc (num_ssa_names);
4501 bitmap_clear (live[pred]);
4503 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
4505 if (bb_rpo[pred] < pred_rpo)
4506 pred_rpo = bb_rpo[pred];
4509 /* Record the RPO number of the last visited block that needs
4510 live information from this block. */
4511 last_rpo[rpo[i]] = pred_rpo;
4513 else
4515 sbitmap_free (live[rpo[i]]);
4516 live[rpo[i]] = NULL;
4519 /* We can free all successors live bitmaps if all their
4520 predecessors have been visited already. */
4521 FOR_EACH_EDGE (e, ei, bb->succs)
4522 if (last_rpo[e->dest->index] == i
4523 && live[e->dest->index])
4525 sbitmap_free (live[e->dest->index]);
4526 live[e->dest->index] = NULL;
4530 XDELETEVEC (rpo);
4531 XDELETEVEC (bb_rpo);
4532 XDELETEVEC (last_rpo);
4533 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
4534 if (live[i])
4535 sbitmap_free (live[i]);
4536 XDELETEVEC (live);
4539 /* Create an ASSERT_EXPR for NAME and insert it in the location
4540 indicated by LOC. Return true if we made any edge insertions. */
4542 static bool
4543 process_assert_insertions_for (tree name, assert_locus *loc)
4545 /* Build the comparison expression NAME_i COMP_CODE VAL. */
4546 gimple *stmt;
4547 tree cond;
4548 gimple *assert_stmt;
4549 edge_iterator ei;
4550 edge e;
4552 /* If we have X <=> X do not insert an assert expr for that. */
4553 if (loc->expr == loc->val)
4554 return false;
4556 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
4557 assert_stmt = build_assert_expr_for (cond, name);
4558 if (loc->e)
4560 /* We have been asked to insert the assertion on an edge. This
4561 is used only by COND_EXPR and SWITCH_EXPR assertions. */
4562 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
4563 || (gimple_code (gsi_stmt (loc->si))
4564 == GIMPLE_SWITCH));
4566 gsi_insert_on_edge (loc->e, assert_stmt);
4567 return true;
4570 /* If the stmt iterator points at the end then this is an insertion
4571 at the beginning of a block. */
4572 if (gsi_end_p (loc->si))
4574 gimple_stmt_iterator si = gsi_after_labels (loc->bb);
4575 gsi_insert_before (&si, assert_stmt, GSI_SAME_STMT);
4576 return false;
4579 /* Otherwise, we can insert right after LOC->SI iff the
4580 statement must not be the last statement in the block. */
4581 stmt = gsi_stmt (loc->si);
4582 if (!stmt_ends_bb_p (stmt))
4584 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
4585 return false;
4588 /* If STMT must be the last statement in BB, we can only insert new
4589 assertions on the non-abnormal edge out of BB. Note that since
4590 STMT is not control flow, there may only be one non-abnormal/eh edge
4591 out of BB. */
4592 FOR_EACH_EDGE (e, ei, loc->bb->succs)
4593 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
4595 gsi_insert_on_edge (e, assert_stmt);
4596 return true;
4599 gcc_unreachable ();
4602 /* Qsort helper for sorting assert locations. If stable is true, don't
4603 use iterative_hash_expr because it can be unstable for -fcompare-debug,
4604 on the other side some pointers might be NULL. */
4606 template <bool stable>
4607 static int
4608 compare_assert_loc (const void *pa, const void *pb)
4610 assert_locus * const a = *(assert_locus * const *)pa;
4611 assert_locus * const b = *(assert_locus * const *)pb;
4613 /* If stable, some asserts might be optimized away already, sort
4614 them last. */
4615 if (stable)
4617 if (a == NULL)
4618 return b != NULL;
4619 else if (b == NULL)
4620 return -1;
4623 if (a->e == NULL && b->e != NULL)
4624 return 1;
4625 else if (a->e != NULL && b->e == NULL)
4626 return -1;
4628 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
4629 no need to test both a->e and b->e. */
4631 /* Sort after destination index. */
4632 if (a->e == NULL)
4634 else if (a->e->dest->index > b->e->dest->index)
4635 return 1;
4636 else if (a->e->dest->index < b->e->dest->index)
4637 return -1;
4639 /* Sort after comp_code. */
4640 if (a->comp_code > b->comp_code)
4641 return 1;
4642 else if (a->comp_code < b->comp_code)
4643 return -1;
4645 hashval_t ha, hb;
4647 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
4648 uses DECL_UID of the VAR_DECL, so sorting might differ between
4649 -g and -g0. When doing the removal of redundant assert exprs
4650 and commonization to successors, this does not matter, but for
4651 the final sort needs to be stable. */
4652 if (stable)
4654 ha = 0;
4655 hb = 0;
4657 else
4659 ha = iterative_hash_expr (a->expr, iterative_hash_expr (a->val, 0));
4660 hb = iterative_hash_expr (b->expr, iterative_hash_expr (b->val, 0));
4663 /* Break the tie using hashing and source/bb index. */
4664 if (ha == hb)
4665 return (a->e != NULL
4666 ? a->e->src->index - b->e->src->index
4667 : a->bb->index - b->bb->index);
4668 return ha > hb ? 1 : -1;
4671 /* Process all the insertions registered for every name N_i registered
4672 in NEED_ASSERT_FOR. The list of assertions to be inserted are
4673 found in ASSERTS_FOR[i]. */
4675 static void
4676 process_assert_insertions (void)
4678 unsigned i;
4679 bitmap_iterator bi;
4680 bool update_edges_p = false;
4681 int num_asserts = 0;
4683 if (dump_file && (dump_flags & TDF_DETAILS))
4684 dump_all_asserts (dump_file);
4686 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4688 assert_locus *loc = asserts_for[i];
4689 gcc_assert (loc);
4691 auto_vec<assert_locus *, 16> asserts;
4692 for (; loc; loc = loc->next)
4693 asserts.safe_push (loc);
4694 asserts.qsort (compare_assert_loc<false>);
4696 /* Push down common asserts to successors and remove redundant ones. */
4697 unsigned ecnt = 0;
4698 assert_locus *common = NULL;
4699 unsigned commonj = 0;
4700 for (unsigned j = 0; j < asserts.length (); ++j)
4702 loc = asserts[j];
4703 if (! loc->e)
4704 common = NULL;
4705 else if (! common
4706 || loc->e->dest != common->e->dest
4707 || loc->comp_code != common->comp_code
4708 || ! operand_equal_p (loc->val, common->val, 0)
4709 || ! operand_equal_p (loc->expr, common->expr, 0))
4711 commonj = j;
4712 common = loc;
4713 ecnt = 1;
4715 else if (loc->e == asserts[j-1]->e)
4717 /* Remove duplicate asserts. */
4718 if (commonj == j - 1)
4720 commonj = j;
4721 common = loc;
4723 free (asserts[j-1]);
4724 asserts[j-1] = NULL;
4726 else
4728 ecnt++;
4729 if (EDGE_COUNT (common->e->dest->preds) == ecnt)
4731 /* We have the same assertion on all incoming edges of a BB.
4732 Insert it at the beginning of that block. */
4733 loc->bb = loc->e->dest;
4734 loc->e = NULL;
4735 loc->si = gsi_none ();
4736 common = NULL;
4737 /* Clear asserts commoned. */
4738 for (; commonj != j; ++commonj)
4739 if (asserts[commonj])
4741 free (asserts[commonj]);
4742 asserts[commonj] = NULL;
4748 /* The asserts vector sorting above might be unstable for
4749 -fcompare-debug, sort again to ensure a stable sort. */
4750 asserts.qsort (compare_assert_loc<true>);
4751 for (unsigned j = 0; j < asserts.length (); ++j)
4753 loc = asserts[j];
4754 if (! loc)
4755 break;
4756 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
4757 num_asserts++;
4758 free (loc);
4762 if (update_edges_p)
4763 gsi_commit_edge_inserts ();
4765 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
4766 num_asserts);
4770 /* Traverse the flowgraph looking for conditional jumps to insert range
4771 expressions. These range expressions are meant to provide information
4772 to optimizations that need to reason in terms of value ranges. They
4773 will not be expanded into RTL. For instance, given:
4775 x = ...
4776 y = ...
4777 if (x < y)
4778 y = x - 2;
4779 else
4780 x = y + 3;
4782 this pass will transform the code into:
4784 x = ...
4785 y = ...
4786 if (x < y)
4788 x = ASSERT_EXPR <x, x < y>
4789 y = x - 2
4791 else
4793 y = ASSERT_EXPR <y, x >= y>
4794 x = y + 3
4797 The idea is that once copy and constant propagation have run, other
4798 optimizations will be able to determine what ranges of values can 'x'
4799 take in different paths of the code, simply by checking the reaching
4800 definition of 'x'. */
4802 static void
4803 insert_range_assertions (void)
4805 need_assert_for = BITMAP_ALLOC (NULL);
4806 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
4808 calculate_dominance_info (CDI_DOMINATORS);
4810 find_assert_locations ();
4811 if (!bitmap_empty_p (need_assert_for))
4813 process_assert_insertions ();
4814 update_ssa (TODO_update_ssa_no_phi);
4817 if (dump_file && (dump_flags & TDF_DETAILS))
4819 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
4820 dump_function_to_file (current_function_decl, dump_file, dump_flags);
4823 free (asserts_for);
4824 BITMAP_FREE (need_assert_for);
4827 class vrp_prop : public ssa_propagation_engine
4829 public:
4830 enum ssa_prop_result visit_stmt (gimple *, edge *, tree *) FINAL OVERRIDE;
4831 enum ssa_prop_result visit_phi (gphi *) FINAL OVERRIDE;
4833 void vrp_initialize (void);
4834 void vrp_finalize (bool);
4835 void check_all_array_refs (void);
4836 void check_array_ref (location_t, tree, bool);
4837 void search_for_addr_array (tree, location_t);
4839 class vr_values vr_values;
4840 /* Temporary delegator to minimize code churn. */
4841 value_range *get_value_range (const_tree op)
4842 { return vr_values.get_value_range (op); }
4843 void set_defs_to_varying (gimple *stmt)
4844 { return vr_values.set_defs_to_varying (stmt); }
4845 void extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
4846 tree *output_p, value_range *vr)
4847 { vr_values.extract_range_from_stmt (stmt, taken_edge_p, output_p, vr); }
4848 bool update_value_range (const_tree op, value_range *vr)
4849 { return vr_values.update_value_range (op, vr); }
4850 void extract_range_basic (value_range *vr, gimple *stmt)
4851 { vr_values.extract_range_basic (vr, stmt); }
4852 void extract_range_from_phi_node (gphi *phi, value_range *vr)
4853 { vr_values.extract_range_from_phi_node (phi, vr); }
4855 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
4856 and "struct" hacks. If VRP can determine that the
4857 array subscript is a constant, check if it is outside valid
4858 range. If the array subscript is a RANGE, warn if it is
4859 non-overlapping with valid range.
4860 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
4862 void
4863 vrp_prop::check_array_ref (location_t location, tree ref,
4864 bool ignore_off_by_one)
4866 value_range *vr = NULL;
4867 tree low_sub, up_sub;
4868 tree low_bound, up_bound, up_bound_p1;
4870 if (TREE_NO_WARNING (ref))
4871 return;
4873 low_sub = up_sub = TREE_OPERAND (ref, 1);
4874 up_bound = array_ref_up_bound (ref);
4876 if (!up_bound
4877 || TREE_CODE (up_bound) != INTEGER_CST
4878 || (warn_array_bounds < 2
4879 && array_at_struct_end_p (ref)))
4881 /* Accesses to trailing arrays via pointers may access storage
4882 beyond the types array bounds. For such arrays, or for flexible
4883 array members, as well as for other arrays of an unknown size,
4884 replace the upper bound with a more permissive one that assumes
4885 the size of the largest object is PTRDIFF_MAX. */
4886 tree eltsize = array_ref_element_size (ref);
4888 if (TREE_CODE (eltsize) != INTEGER_CST
4889 || integer_zerop (eltsize))
4891 up_bound = NULL_TREE;
4892 up_bound_p1 = NULL_TREE;
4894 else
4896 tree maxbound = TYPE_MAX_VALUE (ptrdiff_type_node);
4897 tree arg = TREE_OPERAND (ref, 0);
4898 poly_int64 off;
4900 if (get_addr_base_and_unit_offset (arg, &off) && known_gt (off, 0))
4901 maxbound = wide_int_to_tree (sizetype,
4902 wi::sub (wi::to_wide (maxbound),
4903 off));
4904 else
4905 maxbound = fold_convert (sizetype, maxbound);
4907 up_bound_p1 = int_const_binop (TRUNC_DIV_EXPR, maxbound, eltsize);
4909 up_bound = int_const_binop (MINUS_EXPR, up_bound_p1,
4910 build_int_cst (ptrdiff_type_node, 1));
4913 else
4914 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
4915 build_int_cst (TREE_TYPE (up_bound), 1));
4917 low_bound = array_ref_low_bound (ref);
4919 tree artype = TREE_TYPE (TREE_OPERAND (ref, 0));
4921 /* Empty array. */
4922 if (up_bound && tree_int_cst_equal (low_bound, up_bound_p1))
4924 warning_at (location, OPT_Warray_bounds,
4925 "array subscript %E is above array bounds of %qT",
4926 low_bound, artype);
4927 TREE_NO_WARNING (ref) = 1;
4930 if (TREE_CODE (low_sub) == SSA_NAME)
4932 vr = get_value_range (low_sub);
4933 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4935 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
4936 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
4940 if (vr && vr->type == VR_ANTI_RANGE)
4942 if (up_bound
4943 && TREE_CODE (up_sub) == INTEGER_CST
4944 && (ignore_off_by_one
4945 ? tree_int_cst_lt (up_bound, up_sub)
4946 : tree_int_cst_le (up_bound, up_sub))
4947 && TREE_CODE (low_sub) == INTEGER_CST
4948 && tree_int_cst_le (low_sub, low_bound))
4950 warning_at (location, OPT_Warray_bounds,
4951 "array subscript [%E, %E] is outside array bounds of %qT",
4952 low_sub, up_sub, artype);
4953 TREE_NO_WARNING (ref) = 1;
4956 else if (up_bound
4957 && TREE_CODE (up_sub) == INTEGER_CST
4958 && (ignore_off_by_one
4959 ? !tree_int_cst_le (up_sub, up_bound_p1)
4960 : !tree_int_cst_le (up_sub, up_bound)))
4962 if (dump_file && (dump_flags & TDF_DETAILS))
4964 fprintf (dump_file, "Array bound warning for ");
4965 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
4966 fprintf (dump_file, "\n");
4968 warning_at (location, OPT_Warray_bounds,
4969 "array subscript %E is above array bounds of %qT",
4970 up_sub, artype);
4971 TREE_NO_WARNING (ref) = 1;
4973 else if (TREE_CODE (low_sub) == INTEGER_CST
4974 && tree_int_cst_lt (low_sub, low_bound))
4976 if (dump_file && (dump_flags & TDF_DETAILS))
4978 fprintf (dump_file, "Array bound warning for ");
4979 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
4980 fprintf (dump_file, "\n");
4982 warning_at (location, OPT_Warray_bounds,
4983 "array subscript %E is below array bounds of %qT",
4984 low_sub, artype);
4985 TREE_NO_WARNING (ref) = 1;
4989 /* Searches if the expr T, located at LOCATION computes
4990 address of an ARRAY_REF, and call check_array_ref on it. */
4992 void
4993 vrp_prop::search_for_addr_array (tree t, location_t location)
4995 /* Check each ARRAY_REFs in the reference chain. */
4998 if (TREE_CODE (t) == ARRAY_REF)
4999 check_array_ref (location, t, true /*ignore_off_by_one*/);
5001 t = TREE_OPERAND (t, 0);
5003 while (handled_component_p (t));
5005 if (TREE_CODE (t) == MEM_REF
5006 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
5007 && !TREE_NO_WARNING (t))
5009 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
5010 tree low_bound, up_bound, el_sz;
5011 offset_int idx;
5012 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
5013 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
5014 || !TYPE_DOMAIN (TREE_TYPE (tem)))
5015 return;
5017 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
5018 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
5019 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
5020 if (!low_bound
5021 || TREE_CODE (low_bound) != INTEGER_CST
5022 || !up_bound
5023 || TREE_CODE (up_bound) != INTEGER_CST
5024 || !el_sz
5025 || TREE_CODE (el_sz) != INTEGER_CST)
5026 return;
5028 if (!mem_ref_offset (t).is_constant (&idx))
5029 return;
5031 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
5032 if (idx < 0)
5034 if (dump_file && (dump_flags & TDF_DETAILS))
5036 fprintf (dump_file, "Array bound warning for ");
5037 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
5038 fprintf (dump_file, "\n");
5040 warning_at (location, OPT_Warray_bounds,
5041 "array subscript %wi is below array bounds of %qT",
5042 idx.to_shwi (), TREE_TYPE (tem));
5043 TREE_NO_WARNING (t) = 1;
5045 else if (idx > (wi::to_offset (up_bound)
5046 - wi::to_offset (low_bound) + 1))
5048 if (dump_file && (dump_flags & TDF_DETAILS))
5050 fprintf (dump_file, "Array bound warning for ");
5051 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
5052 fprintf (dump_file, "\n");
5054 warning_at (location, OPT_Warray_bounds,
5055 "array subscript %wu is above array bounds of %qT",
5056 idx.to_uhwi (), TREE_TYPE (tem));
5057 TREE_NO_WARNING (t) = 1;
5062 /* walk_tree() callback that checks if *TP is
5063 an ARRAY_REF inside an ADDR_EXPR (in which an array
5064 subscript one outside the valid range is allowed). Call
5065 check_array_ref for each ARRAY_REF found. The location is
5066 passed in DATA. */
5068 static tree
5069 check_array_bounds (tree *tp, int *walk_subtree, void *data)
5071 tree t = *tp;
5072 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
5073 location_t location;
5075 if (EXPR_HAS_LOCATION (t))
5076 location = EXPR_LOCATION (t);
5077 else
5078 location = gimple_location (wi->stmt);
5080 *walk_subtree = TRUE;
5082 vrp_prop *vrp_prop = (class vrp_prop *)wi->info;
5083 if (TREE_CODE (t) == ARRAY_REF)
5084 vrp_prop->check_array_ref (location, t, false /*ignore_off_by_one*/);
5086 else if (TREE_CODE (t) == ADDR_EXPR)
5088 vrp_prop->search_for_addr_array (t, location);
5089 *walk_subtree = FALSE;
5092 return NULL_TREE;
5095 /* A dom_walker subclass for use by vrp_prop::check_all_array_refs,
5096 to walk over all statements of all reachable BBs and call
5097 check_array_bounds on them. */
5099 class check_array_bounds_dom_walker : public dom_walker
5101 public:
5102 check_array_bounds_dom_walker (vrp_prop *prop)
5103 : dom_walker (CDI_DOMINATORS,
5104 /* Discover non-executable edges, preserving EDGE_EXECUTABLE
5105 flags, so that we can merge in information on
5106 non-executable edges from vrp_folder . */
5107 REACHABLE_BLOCKS_PRESERVING_FLAGS),
5108 m_prop (prop) {}
5109 ~check_array_bounds_dom_walker () {}
5111 edge before_dom_children (basic_block) FINAL OVERRIDE;
5113 private:
5114 vrp_prop *m_prop;
5117 /* Implementation of dom_walker::before_dom_children.
5119 Walk over all statements of BB and call check_array_bounds on them,
5120 and determine if there's a unique successor edge. */
5122 edge
5123 check_array_bounds_dom_walker::before_dom_children (basic_block bb)
5125 gimple_stmt_iterator si;
5126 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5128 gimple *stmt = gsi_stmt (si);
5129 struct walk_stmt_info wi;
5130 if (!gimple_has_location (stmt)
5131 || is_gimple_debug (stmt))
5132 continue;
5134 memset (&wi, 0, sizeof (wi));
5136 wi.info = m_prop;
5138 walk_gimple_op (stmt, check_array_bounds, &wi);
5141 /* Determine if there's a unique successor edge, and if so, return
5142 that back to dom_walker, ensuring that we don't visit blocks that
5143 became unreachable during the VRP propagation
5144 (PR tree-optimization/83312). */
5145 return find_taken_edge (bb, NULL_TREE);
5148 /* Walk over all statements of all reachable BBs and call check_array_bounds
5149 on them. */
5151 void
5152 vrp_prop::check_all_array_refs ()
5154 check_array_bounds_dom_walker w (this);
5155 w.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5158 /* Return true if all imm uses of VAR are either in STMT, or
5159 feed (optionally through a chain of single imm uses) GIMPLE_COND
5160 in basic block COND_BB. */
5162 static bool
5163 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
5165 use_operand_p use_p, use2_p;
5166 imm_use_iterator iter;
5168 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
5169 if (USE_STMT (use_p) != stmt)
5171 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
5172 if (is_gimple_debug (use_stmt))
5173 continue;
5174 while (is_gimple_assign (use_stmt)
5175 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
5176 && single_imm_use (gimple_assign_lhs (use_stmt),
5177 &use2_p, &use_stmt2))
5178 use_stmt = use_stmt2;
5179 if (gimple_code (use_stmt) != GIMPLE_COND
5180 || gimple_bb (use_stmt) != cond_bb)
5181 return false;
5183 return true;
5186 /* Handle
5187 _4 = x_3 & 31;
5188 if (_4 != 0)
5189 goto <bb 6>;
5190 else
5191 goto <bb 7>;
5192 <bb 6>:
5193 __builtin_unreachable ();
5194 <bb 7>:
5195 x_5 = ASSERT_EXPR <x_3, ...>;
5196 If x_3 has no other immediate uses (checked by caller),
5197 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
5198 from the non-zero bitmask. */
5200 void
5201 maybe_set_nonzero_bits (edge e, tree var)
5203 basic_block cond_bb = e->src;
5204 gimple *stmt = last_stmt (cond_bb);
5205 tree cst;
5207 if (stmt == NULL
5208 || gimple_code (stmt) != GIMPLE_COND
5209 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
5210 ? EQ_EXPR : NE_EXPR)
5211 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
5212 || !integer_zerop (gimple_cond_rhs (stmt)))
5213 return;
5215 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
5216 if (!is_gimple_assign (stmt)
5217 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
5218 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
5219 return;
5220 if (gimple_assign_rhs1 (stmt) != var)
5222 gimple *stmt2;
5224 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
5225 return;
5226 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
5227 if (!gimple_assign_cast_p (stmt2)
5228 || gimple_assign_rhs1 (stmt2) != var
5229 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
5230 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
5231 != TYPE_PRECISION (TREE_TYPE (var))))
5232 return;
5234 cst = gimple_assign_rhs2 (stmt);
5235 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var),
5236 wi::to_wide (cst)));
5239 /* Convert range assertion expressions into the implied copies and
5240 copy propagate away the copies. Doing the trivial copy propagation
5241 here avoids the need to run the full copy propagation pass after
5242 VRP.
5244 FIXME, this will eventually lead to copy propagation removing the
5245 names that had useful range information attached to them. For
5246 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
5247 then N_i will have the range [3, +INF].
5249 However, by converting the assertion into the implied copy
5250 operation N_i = N_j, we will then copy-propagate N_j into the uses
5251 of N_i and lose the range information. We may want to hold on to
5252 ASSERT_EXPRs a little while longer as the ranges could be used in
5253 things like jump threading.
5255 The problem with keeping ASSERT_EXPRs around is that passes after
5256 VRP need to handle them appropriately.
5258 Another approach would be to make the range information a first
5259 class property of the SSA_NAME so that it can be queried from
5260 any pass. This is made somewhat more complex by the need for
5261 multiple ranges to be associated with one SSA_NAME. */
5263 static void
5264 remove_range_assertions (void)
5266 basic_block bb;
5267 gimple_stmt_iterator si;
5268 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
5269 a basic block preceeded by GIMPLE_COND branching to it and
5270 __builtin_trap, -1 if not yet checked, 0 otherwise. */
5271 int is_unreachable;
5273 /* Note that the BSI iterator bump happens at the bottom of the
5274 loop and no bump is necessary if we're removing the statement
5275 referenced by the current BSI. */
5276 FOR_EACH_BB_FN (bb, cfun)
5277 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
5279 gimple *stmt = gsi_stmt (si);
5281 if (is_gimple_assign (stmt)
5282 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
5284 tree lhs = gimple_assign_lhs (stmt);
5285 tree rhs = gimple_assign_rhs1 (stmt);
5286 tree var;
5288 var = ASSERT_EXPR_VAR (rhs);
5290 if (TREE_CODE (var) == SSA_NAME
5291 && !POINTER_TYPE_P (TREE_TYPE (lhs))
5292 && SSA_NAME_RANGE_INFO (lhs))
5294 if (is_unreachable == -1)
5296 is_unreachable = 0;
5297 if (single_pred_p (bb)
5298 && assert_unreachable_fallthru_edge_p
5299 (single_pred_edge (bb)))
5300 is_unreachable = 1;
5302 /* Handle
5303 if (x_7 >= 10 && x_7 < 20)
5304 __builtin_unreachable ();
5305 x_8 = ASSERT_EXPR <x_7, ...>;
5306 if the only uses of x_7 are in the ASSERT_EXPR and
5307 in the condition. In that case, we can copy the
5308 range info from x_8 computed in this pass also
5309 for x_7. */
5310 if (is_unreachable
5311 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
5312 single_pred (bb)))
5314 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
5315 SSA_NAME_RANGE_INFO (lhs)->get_min (),
5316 SSA_NAME_RANGE_INFO (lhs)->get_max ());
5317 maybe_set_nonzero_bits (single_pred_edge (bb), var);
5321 /* Propagate the RHS into every use of the LHS. For SSA names
5322 also propagate abnormals as it merely restores the original
5323 IL in this case (an replace_uses_by would assert). */
5324 if (TREE_CODE (var) == SSA_NAME)
5326 imm_use_iterator iter;
5327 use_operand_p use_p;
5328 gimple *use_stmt;
5329 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
5330 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
5331 SET_USE (use_p, var);
5333 else
5334 replace_uses_by (lhs, var);
5336 /* And finally, remove the copy, it is not needed. */
5337 gsi_remove (&si, true);
5338 release_defs (stmt);
5340 else
5342 if (!is_gimple_debug (gsi_stmt (si)))
5343 is_unreachable = 0;
5344 gsi_next (&si);
5349 /* Return true if STMT is interesting for VRP. */
5351 bool
5352 stmt_interesting_for_vrp (gimple *stmt)
5354 if (gimple_code (stmt) == GIMPLE_PHI)
5356 tree res = gimple_phi_result (stmt);
5357 return (!virtual_operand_p (res)
5358 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
5359 || POINTER_TYPE_P (TREE_TYPE (res))));
5361 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
5363 tree lhs = gimple_get_lhs (stmt);
5365 /* In general, assignments with virtual operands are not useful
5366 for deriving ranges, with the obvious exception of calls to
5367 builtin functions. */
5368 if (lhs && TREE_CODE (lhs) == SSA_NAME
5369 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
5370 || POINTER_TYPE_P (TREE_TYPE (lhs)))
5371 && (is_gimple_call (stmt)
5372 || !gimple_vuse (stmt)))
5373 return true;
5374 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
5375 switch (gimple_call_internal_fn (stmt))
5377 case IFN_ADD_OVERFLOW:
5378 case IFN_SUB_OVERFLOW:
5379 case IFN_MUL_OVERFLOW:
5380 case IFN_ATOMIC_COMPARE_EXCHANGE:
5381 /* These internal calls return _Complex integer type,
5382 but are interesting to VRP nevertheless. */
5383 if (lhs && TREE_CODE (lhs) == SSA_NAME)
5384 return true;
5385 break;
5386 default:
5387 break;
5390 else if (gimple_code (stmt) == GIMPLE_COND
5391 || gimple_code (stmt) == GIMPLE_SWITCH)
5392 return true;
5394 return false;
5397 /* Initialization required by ssa_propagate engine. */
5399 void
5400 vrp_prop::vrp_initialize ()
5402 basic_block bb;
5404 FOR_EACH_BB_FN (bb, cfun)
5406 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
5407 gsi_next (&si))
5409 gphi *phi = si.phi ();
5410 if (!stmt_interesting_for_vrp (phi))
5412 tree lhs = PHI_RESULT (phi);
5413 set_value_range_to_varying (get_value_range (lhs));
5414 prop_set_simulate_again (phi, false);
5416 else
5417 prop_set_simulate_again (phi, true);
5420 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
5421 gsi_next (&si))
5423 gimple *stmt = gsi_stmt (si);
5425 /* If the statement is a control insn, then we do not
5426 want to avoid simulating the statement once. Failure
5427 to do so means that those edges will never get added. */
5428 if (stmt_ends_bb_p (stmt))
5429 prop_set_simulate_again (stmt, true);
5430 else if (!stmt_interesting_for_vrp (stmt))
5432 set_defs_to_varying (stmt);
5433 prop_set_simulate_again (stmt, false);
5435 else
5436 prop_set_simulate_again (stmt, true);
5441 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
5442 that includes the value VAL. The search is restricted to the range
5443 [START_IDX, n - 1] where n is the size of VEC.
5445 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
5446 returned.
5448 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
5449 it is placed in IDX and false is returned.
5451 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
5452 returned. */
5454 bool
5455 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
5457 size_t n = gimple_switch_num_labels (stmt);
5458 size_t low, high;
5460 /* Find case label for minimum of the value range or the next one.
5461 At each iteration we are searching in [low, high - 1]. */
5463 for (low = start_idx, high = n; high != low; )
5465 tree t;
5466 int cmp;
5467 /* Note that i != high, so we never ask for n. */
5468 size_t i = (high + low) / 2;
5469 t = gimple_switch_label (stmt, i);
5471 /* Cache the result of comparing CASE_LOW and val. */
5472 cmp = tree_int_cst_compare (CASE_LOW (t), val);
5474 if (cmp == 0)
5476 /* Ranges cannot be empty. */
5477 *idx = i;
5478 return true;
5480 else if (cmp > 0)
5481 high = i;
5482 else
5484 low = i + 1;
5485 if (CASE_HIGH (t) != NULL
5486 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
5488 *idx = i;
5489 return true;
5494 *idx = high;
5495 return false;
5498 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
5499 for values between MIN and MAX. The first index is placed in MIN_IDX. The
5500 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
5501 then MAX_IDX < MIN_IDX.
5502 Returns true if the default label is not needed. */
5504 bool
5505 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
5506 size_t *max_idx)
5508 size_t i, j;
5509 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
5510 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
5512 if (i == j
5513 && min_take_default
5514 && max_take_default)
5516 /* Only the default case label reached.
5517 Return an empty range. */
5518 *min_idx = 1;
5519 *max_idx = 0;
5520 return false;
5522 else
5524 bool take_default = min_take_default || max_take_default;
5525 tree low, high;
5526 size_t k;
5528 if (max_take_default)
5529 j--;
5531 /* If the case label range is continuous, we do not need
5532 the default case label. Verify that. */
5533 high = CASE_LOW (gimple_switch_label (stmt, i));
5534 if (CASE_HIGH (gimple_switch_label (stmt, i)))
5535 high = CASE_HIGH (gimple_switch_label (stmt, i));
5536 for (k = i + 1; k <= j; ++k)
5538 low = CASE_LOW (gimple_switch_label (stmt, k));
5539 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
5541 take_default = true;
5542 break;
5544 high = low;
5545 if (CASE_HIGH (gimple_switch_label (stmt, k)))
5546 high = CASE_HIGH (gimple_switch_label (stmt, k));
5549 *min_idx = i;
5550 *max_idx = j;
5551 return !take_default;
5555 /* Evaluate statement STMT. If the statement produces a useful range,
5556 return SSA_PROP_INTERESTING and record the SSA name with the
5557 interesting range into *OUTPUT_P.
5559 If STMT is a conditional branch and we can determine its truth
5560 value, the taken edge is recorded in *TAKEN_EDGE_P.
5562 If STMT produces a varying value, return SSA_PROP_VARYING. */
5564 enum ssa_prop_result
5565 vrp_prop::visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
5567 value_range vr = VR_INITIALIZER;
5568 tree lhs = gimple_get_lhs (stmt);
5569 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr);
5571 if (*output_p)
5573 if (update_value_range (*output_p, &vr))
5575 if (dump_file && (dump_flags & TDF_DETAILS))
5577 fprintf (dump_file, "Found new range for ");
5578 print_generic_expr (dump_file, *output_p);
5579 fprintf (dump_file, ": ");
5580 dump_value_range (dump_file, &vr);
5581 fprintf (dump_file, "\n");
5584 if (vr.type == VR_VARYING)
5585 return SSA_PROP_VARYING;
5587 return SSA_PROP_INTERESTING;
5589 return SSA_PROP_NOT_INTERESTING;
5592 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
5593 switch (gimple_call_internal_fn (stmt))
5595 case IFN_ADD_OVERFLOW:
5596 case IFN_SUB_OVERFLOW:
5597 case IFN_MUL_OVERFLOW:
5598 case IFN_ATOMIC_COMPARE_EXCHANGE:
5599 /* These internal calls return _Complex integer type,
5600 which VRP does not track, but the immediate uses
5601 thereof might be interesting. */
5602 if (lhs && TREE_CODE (lhs) == SSA_NAME)
5604 imm_use_iterator iter;
5605 use_operand_p use_p;
5606 enum ssa_prop_result res = SSA_PROP_VARYING;
5608 set_value_range_to_varying (get_value_range (lhs));
5610 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
5612 gimple *use_stmt = USE_STMT (use_p);
5613 if (!is_gimple_assign (use_stmt))
5614 continue;
5615 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
5616 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
5617 continue;
5618 tree rhs1 = gimple_assign_rhs1 (use_stmt);
5619 tree use_lhs = gimple_assign_lhs (use_stmt);
5620 if (TREE_CODE (rhs1) != rhs_code
5621 || TREE_OPERAND (rhs1, 0) != lhs
5622 || TREE_CODE (use_lhs) != SSA_NAME
5623 || !stmt_interesting_for_vrp (use_stmt)
5624 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
5625 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
5626 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
5627 continue;
5629 /* If there is a change in the value range for any of the
5630 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
5631 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
5632 or IMAGPART_EXPR immediate uses, but none of them have
5633 a change in their value ranges, return
5634 SSA_PROP_NOT_INTERESTING. If there are no
5635 {REAL,IMAG}PART_EXPR uses at all,
5636 return SSA_PROP_VARYING. */
5637 value_range new_vr = VR_INITIALIZER;
5638 extract_range_basic (&new_vr, use_stmt);
5639 value_range *old_vr = get_value_range (use_lhs);
5640 if (old_vr->type != new_vr.type
5641 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
5642 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
5643 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
5644 res = SSA_PROP_INTERESTING;
5645 else
5646 res = SSA_PROP_NOT_INTERESTING;
5647 BITMAP_FREE (new_vr.equiv);
5648 if (res == SSA_PROP_INTERESTING)
5650 *output_p = lhs;
5651 return res;
5655 return res;
5657 break;
5658 default:
5659 break;
5662 /* All other statements produce nothing of interest for VRP, so mark
5663 their outputs varying and prevent further simulation. */
5664 set_defs_to_varying (stmt);
5666 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
5669 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5670 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5671 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5672 possible such range. The resulting range is not canonicalized. */
5674 static void
5675 union_ranges (enum value_range_type *vr0type,
5676 tree *vr0min, tree *vr0max,
5677 enum value_range_type vr1type,
5678 tree vr1min, tree vr1max)
5680 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
5681 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
5683 /* [] is vr0, () is vr1 in the following classification comments. */
5684 if (mineq && maxeq)
5686 /* [( )] */
5687 if (*vr0type == vr1type)
5688 /* Nothing to do for equal ranges. */
5690 else if ((*vr0type == VR_RANGE
5691 && vr1type == VR_ANTI_RANGE)
5692 || (*vr0type == VR_ANTI_RANGE
5693 && vr1type == VR_RANGE))
5695 /* For anti-range with range union the result is varying. */
5696 goto give_up;
5698 else
5699 gcc_unreachable ();
5701 else if (operand_less_p (*vr0max, vr1min) == 1
5702 || operand_less_p (vr1max, *vr0min) == 1)
5704 /* [ ] ( ) or ( ) [ ]
5705 If the ranges have an empty intersection, result of the union
5706 operation is the anti-range or if both are anti-ranges
5707 it covers all. */
5708 if (*vr0type == VR_ANTI_RANGE
5709 && vr1type == VR_ANTI_RANGE)
5710 goto give_up;
5711 else if (*vr0type == VR_ANTI_RANGE
5712 && vr1type == VR_RANGE)
5714 else if (*vr0type == VR_RANGE
5715 && vr1type == VR_ANTI_RANGE)
5717 *vr0type = vr1type;
5718 *vr0min = vr1min;
5719 *vr0max = vr1max;
5721 else if (*vr0type == VR_RANGE
5722 && vr1type == VR_RANGE)
5724 /* The result is the convex hull of both ranges. */
5725 if (operand_less_p (*vr0max, vr1min) == 1)
5727 /* If the result can be an anti-range, create one. */
5728 if (TREE_CODE (*vr0max) == INTEGER_CST
5729 && TREE_CODE (vr1min) == INTEGER_CST
5730 && vrp_val_is_min (*vr0min)
5731 && vrp_val_is_max (vr1max))
5733 tree min = int_const_binop (PLUS_EXPR,
5734 *vr0max,
5735 build_int_cst (TREE_TYPE (*vr0max), 1));
5736 tree max = int_const_binop (MINUS_EXPR,
5737 vr1min,
5738 build_int_cst (TREE_TYPE (vr1min), 1));
5739 if (!operand_less_p (max, min))
5741 *vr0type = VR_ANTI_RANGE;
5742 *vr0min = min;
5743 *vr0max = max;
5745 else
5746 *vr0max = vr1max;
5748 else
5749 *vr0max = vr1max;
5751 else
5753 /* If the result can be an anti-range, create one. */
5754 if (TREE_CODE (vr1max) == INTEGER_CST
5755 && TREE_CODE (*vr0min) == INTEGER_CST
5756 && vrp_val_is_min (vr1min)
5757 && vrp_val_is_max (*vr0max))
5759 tree min = int_const_binop (PLUS_EXPR,
5760 vr1max,
5761 build_int_cst (TREE_TYPE (vr1max), 1));
5762 tree max = int_const_binop (MINUS_EXPR,
5763 *vr0min,
5764 build_int_cst (TREE_TYPE (*vr0min), 1));
5765 if (!operand_less_p (max, min))
5767 *vr0type = VR_ANTI_RANGE;
5768 *vr0min = min;
5769 *vr0max = max;
5771 else
5772 *vr0min = vr1min;
5774 else
5775 *vr0min = vr1min;
5778 else
5779 gcc_unreachable ();
5781 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
5782 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
5784 /* [ ( ) ] or [( ) ] or [ ( )] */
5785 if (*vr0type == VR_RANGE
5786 && vr1type == VR_RANGE)
5788 else if (*vr0type == VR_ANTI_RANGE
5789 && vr1type == VR_ANTI_RANGE)
5791 *vr0type = vr1type;
5792 *vr0min = vr1min;
5793 *vr0max = vr1max;
5795 else if (*vr0type == VR_ANTI_RANGE
5796 && vr1type == VR_RANGE)
5798 /* Arbitrarily choose the right or left gap. */
5799 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
5800 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
5801 build_int_cst (TREE_TYPE (vr1min), 1));
5802 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
5803 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
5804 build_int_cst (TREE_TYPE (vr1max), 1));
5805 else
5806 goto give_up;
5808 else if (*vr0type == VR_RANGE
5809 && vr1type == VR_ANTI_RANGE)
5810 /* The result covers everything. */
5811 goto give_up;
5812 else
5813 gcc_unreachable ();
5815 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
5816 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
5818 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5819 if (*vr0type == VR_RANGE
5820 && vr1type == VR_RANGE)
5822 *vr0type = vr1type;
5823 *vr0min = vr1min;
5824 *vr0max = vr1max;
5826 else if (*vr0type == VR_ANTI_RANGE
5827 && vr1type == VR_ANTI_RANGE)
5829 else if (*vr0type == VR_RANGE
5830 && vr1type == VR_ANTI_RANGE)
5832 *vr0type = VR_ANTI_RANGE;
5833 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
5835 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
5836 build_int_cst (TREE_TYPE (*vr0min), 1));
5837 *vr0min = vr1min;
5839 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
5841 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
5842 build_int_cst (TREE_TYPE (*vr0max), 1));
5843 *vr0max = vr1max;
5845 else
5846 goto give_up;
5848 else if (*vr0type == VR_ANTI_RANGE
5849 && vr1type == VR_RANGE)
5850 /* The result covers everything. */
5851 goto give_up;
5852 else
5853 gcc_unreachable ();
5855 else if ((operand_less_p (vr1min, *vr0max) == 1
5856 || operand_equal_p (vr1min, *vr0max, 0))
5857 && operand_less_p (*vr0min, vr1min) == 1
5858 && operand_less_p (*vr0max, vr1max) == 1)
5860 /* [ ( ] ) or [ ]( ) */
5861 if (*vr0type == VR_RANGE
5862 && vr1type == VR_RANGE)
5863 *vr0max = vr1max;
5864 else if (*vr0type == VR_ANTI_RANGE
5865 && vr1type == VR_ANTI_RANGE)
5866 *vr0min = vr1min;
5867 else if (*vr0type == VR_ANTI_RANGE
5868 && vr1type == VR_RANGE)
5870 if (TREE_CODE (vr1min) == INTEGER_CST)
5871 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
5872 build_int_cst (TREE_TYPE (vr1min), 1));
5873 else
5874 goto give_up;
5876 else if (*vr0type == VR_RANGE
5877 && vr1type == VR_ANTI_RANGE)
5879 if (TREE_CODE (*vr0max) == INTEGER_CST)
5881 *vr0type = vr1type;
5882 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
5883 build_int_cst (TREE_TYPE (*vr0max), 1));
5884 *vr0max = vr1max;
5886 else
5887 goto give_up;
5889 else
5890 gcc_unreachable ();
5892 else if ((operand_less_p (*vr0min, vr1max) == 1
5893 || operand_equal_p (*vr0min, vr1max, 0))
5894 && operand_less_p (vr1min, *vr0min) == 1
5895 && operand_less_p (vr1max, *vr0max) == 1)
5897 /* ( [ ) ] or ( )[ ] */
5898 if (*vr0type == VR_RANGE
5899 && vr1type == VR_RANGE)
5900 *vr0min = vr1min;
5901 else if (*vr0type == VR_ANTI_RANGE
5902 && vr1type == VR_ANTI_RANGE)
5903 *vr0max = vr1max;
5904 else if (*vr0type == VR_ANTI_RANGE
5905 && vr1type == VR_RANGE)
5907 if (TREE_CODE (vr1max) == INTEGER_CST)
5908 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
5909 build_int_cst (TREE_TYPE (vr1max), 1));
5910 else
5911 goto give_up;
5913 else if (*vr0type == VR_RANGE
5914 && vr1type == VR_ANTI_RANGE)
5916 if (TREE_CODE (*vr0min) == INTEGER_CST)
5918 *vr0type = vr1type;
5919 *vr0min = vr1min;
5920 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
5921 build_int_cst (TREE_TYPE (*vr0min), 1));
5923 else
5924 goto give_up;
5926 else
5927 gcc_unreachable ();
5929 else
5930 goto give_up;
5932 return;
5934 give_up:
5935 *vr0type = VR_VARYING;
5936 *vr0min = NULL_TREE;
5937 *vr0max = NULL_TREE;
5940 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5941 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5942 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5943 possible such range. The resulting range is not canonicalized. */
5945 static void
5946 intersect_ranges (enum value_range_type *vr0type,
5947 tree *vr0min, tree *vr0max,
5948 enum value_range_type vr1type,
5949 tree vr1min, tree vr1max)
5951 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
5952 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
5954 /* [] is vr0, () is vr1 in the following classification comments. */
5955 if (mineq && maxeq)
5957 /* [( )] */
5958 if (*vr0type == vr1type)
5959 /* Nothing to do for equal ranges. */
5961 else if ((*vr0type == VR_RANGE
5962 && vr1type == VR_ANTI_RANGE)
5963 || (*vr0type == VR_ANTI_RANGE
5964 && vr1type == VR_RANGE))
5966 /* For anti-range with range intersection the result is empty. */
5967 *vr0type = VR_UNDEFINED;
5968 *vr0min = NULL_TREE;
5969 *vr0max = NULL_TREE;
5971 else
5972 gcc_unreachable ();
5974 else if (operand_less_p (*vr0max, vr1min) == 1
5975 || operand_less_p (vr1max, *vr0min) == 1)
5977 /* [ ] ( ) or ( ) [ ]
5978 If the ranges have an empty intersection, the result of the
5979 intersect operation is the range for intersecting an
5980 anti-range with a range or empty when intersecting two ranges. */
5981 if (*vr0type == VR_RANGE
5982 && vr1type == VR_ANTI_RANGE)
5984 else if (*vr0type == VR_ANTI_RANGE
5985 && vr1type == VR_RANGE)
5987 *vr0type = vr1type;
5988 *vr0min = vr1min;
5989 *vr0max = vr1max;
5991 else if (*vr0type == VR_RANGE
5992 && vr1type == VR_RANGE)
5994 *vr0type = VR_UNDEFINED;
5995 *vr0min = NULL_TREE;
5996 *vr0max = NULL_TREE;
5998 else if (*vr0type == VR_ANTI_RANGE
5999 && vr1type == VR_ANTI_RANGE)
6001 /* If the anti-ranges are adjacent to each other merge them. */
6002 if (TREE_CODE (*vr0max) == INTEGER_CST
6003 && TREE_CODE (vr1min) == INTEGER_CST
6004 && operand_less_p (*vr0max, vr1min) == 1
6005 && integer_onep (int_const_binop (MINUS_EXPR,
6006 vr1min, *vr0max)))
6007 *vr0max = vr1max;
6008 else if (TREE_CODE (vr1max) == INTEGER_CST
6009 && TREE_CODE (*vr0min) == INTEGER_CST
6010 && operand_less_p (vr1max, *vr0min) == 1
6011 && integer_onep (int_const_binop (MINUS_EXPR,
6012 *vr0min, vr1max)))
6013 *vr0min = vr1min;
6014 /* Else arbitrarily take VR0. */
6017 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
6018 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
6020 /* [ ( ) ] or [( ) ] or [ ( )] */
6021 if (*vr0type == VR_RANGE
6022 && vr1type == VR_RANGE)
6024 /* If both are ranges the result is the inner one. */
6025 *vr0type = vr1type;
6026 *vr0min = vr1min;
6027 *vr0max = vr1max;
6029 else if (*vr0type == VR_RANGE
6030 && vr1type == VR_ANTI_RANGE)
6032 /* Choose the right gap if the left one is empty. */
6033 if (mineq)
6035 if (TREE_CODE (vr1max) != INTEGER_CST)
6036 *vr0min = vr1max;
6037 else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1
6038 && !TYPE_UNSIGNED (TREE_TYPE (vr1max)))
6039 *vr0min
6040 = int_const_binop (MINUS_EXPR, vr1max,
6041 build_int_cst (TREE_TYPE (vr1max), -1));
6042 else
6043 *vr0min
6044 = int_const_binop (PLUS_EXPR, vr1max,
6045 build_int_cst (TREE_TYPE (vr1max), 1));
6047 /* Choose the left gap if the right one is empty. */
6048 else if (maxeq)
6050 if (TREE_CODE (vr1min) != INTEGER_CST)
6051 *vr0max = vr1min;
6052 else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1
6053 && !TYPE_UNSIGNED (TREE_TYPE (vr1min)))
6054 *vr0max
6055 = int_const_binop (PLUS_EXPR, vr1min,
6056 build_int_cst (TREE_TYPE (vr1min), -1));
6057 else
6058 *vr0max
6059 = int_const_binop (MINUS_EXPR, vr1min,
6060 build_int_cst (TREE_TYPE (vr1min), 1));
6062 /* Choose the anti-range if the range is effectively varying. */
6063 else if (vrp_val_is_min (*vr0min)
6064 && vrp_val_is_max (*vr0max))
6066 *vr0type = vr1type;
6067 *vr0min = vr1min;
6068 *vr0max = vr1max;
6070 /* Else choose the range. */
6072 else if (*vr0type == VR_ANTI_RANGE
6073 && vr1type == VR_ANTI_RANGE)
6074 /* If both are anti-ranges the result is the outer one. */
6076 else if (*vr0type == VR_ANTI_RANGE
6077 && vr1type == VR_RANGE)
6079 /* The intersection is empty. */
6080 *vr0type = VR_UNDEFINED;
6081 *vr0min = NULL_TREE;
6082 *vr0max = NULL_TREE;
6084 else
6085 gcc_unreachable ();
6087 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
6088 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
6090 /* ( [ ] ) or ([ ] ) or ( [ ]) */
6091 if (*vr0type == VR_RANGE
6092 && vr1type == VR_RANGE)
6093 /* Choose the inner range. */
6095 else if (*vr0type == VR_ANTI_RANGE
6096 && vr1type == VR_RANGE)
6098 /* Choose the right gap if the left is empty. */
6099 if (mineq)
6101 *vr0type = VR_RANGE;
6102 if (TREE_CODE (*vr0max) != INTEGER_CST)
6103 *vr0min = *vr0max;
6104 else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1
6105 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max)))
6106 *vr0min
6107 = int_const_binop (MINUS_EXPR, *vr0max,
6108 build_int_cst (TREE_TYPE (*vr0max), -1));
6109 else
6110 *vr0min
6111 = int_const_binop (PLUS_EXPR, *vr0max,
6112 build_int_cst (TREE_TYPE (*vr0max), 1));
6113 *vr0max = vr1max;
6115 /* Choose the left gap if the right is empty. */
6116 else if (maxeq)
6118 *vr0type = VR_RANGE;
6119 if (TREE_CODE (*vr0min) != INTEGER_CST)
6120 *vr0max = *vr0min;
6121 else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1
6122 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min)))
6123 *vr0max
6124 = int_const_binop (PLUS_EXPR, *vr0min,
6125 build_int_cst (TREE_TYPE (*vr0min), -1));
6126 else
6127 *vr0max
6128 = int_const_binop (MINUS_EXPR, *vr0min,
6129 build_int_cst (TREE_TYPE (*vr0min), 1));
6130 *vr0min = vr1min;
6132 /* Choose the anti-range if the range is effectively varying. */
6133 else if (vrp_val_is_min (vr1min)
6134 && vrp_val_is_max (vr1max))
6136 /* Choose the anti-range if it is ~[0,0], that range is special
6137 enough to special case when vr1's range is relatively wide.
6138 At least for types bigger than int - this covers pointers
6139 and arguments to functions like ctz. */
6140 else if (*vr0min == *vr0max
6141 && integer_zerop (*vr0min)
6142 && ((TYPE_PRECISION (TREE_TYPE (*vr0min))
6143 >= TYPE_PRECISION (integer_type_node))
6144 || POINTER_TYPE_P (TREE_TYPE (*vr0min)))
6145 && TREE_CODE (vr1max) == INTEGER_CST
6146 && TREE_CODE (vr1min) == INTEGER_CST
6147 && (wi::clz (wi::to_wide (vr1max) - wi::to_wide (vr1min))
6148 < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2))
6150 /* Else choose the range. */
6151 else
6153 *vr0type = vr1type;
6154 *vr0min = vr1min;
6155 *vr0max = vr1max;
6158 else if (*vr0type == VR_ANTI_RANGE
6159 && vr1type == VR_ANTI_RANGE)
6161 /* If both are anti-ranges the result is the outer one. */
6162 *vr0type = vr1type;
6163 *vr0min = vr1min;
6164 *vr0max = vr1max;
6166 else if (vr1type == VR_ANTI_RANGE
6167 && *vr0type == VR_RANGE)
6169 /* The intersection is empty. */
6170 *vr0type = VR_UNDEFINED;
6171 *vr0min = NULL_TREE;
6172 *vr0max = NULL_TREE;
6174 else
6175 gcc_unreachable ();
6177 else if ((operand_less_p (vr1min, *vr0max) == 1
6178 || operand_equal_p (vr1min, *vr0max, 0))
6179 && operand_less_p (*vr0min, vr1min) == 1)
6181 /* [ ( ] ) or [ ]( ) */
6182 if (*vr0type == VR_ANTI_RANGE
6183 && vr1type == VR_ANTI_RANGE)
6184 *vr0max = vr1max;
6185 else if (*vr0type == VR_RANGE
6186 && vr1type == VR_RANGE)
6187 *vr0min = vr1min;
6188 else if (*vr0type == VR_RANGE
6189 && vr1type == VR_ANTI_RANGE)
6191 if (TREE_CODE (vr1min) == INTEGER_CST)
6192 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
6193 build_int_cst (TREE_TYPE (vr1min), 1));
6194 else
6195 *vr0max = vr1min;
6197 else if (*vr0type == VR_ANTI_RANGE
6198 && vr1type == VR_RANGE)
6200 *vr0type = VR_RANGE;
6201 if (TREE_CODE (*vr0max) == INTEGER_CST)
6202 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
6203 build_int_cst (TREE_TYPE (*vr0max), 1));
6204 else
6205 *vr0min = *vr0max;
6206 *vr0max = vr1max;
6208 else
6209 gcc_unreachable ();
6211 else if ((operand_less_p (*vr0min, vr1max) == 1
6212 || operand_equal_p (*vr0min, vr1max, 0))
6213 && operand_less_p (vr1min, *vr0min) == 1)
6215 /* ( [ ) ] or ( )[ ] */
6216 if (*vr0type == VR_ANTI_RANGE
6217 && vr1type == VR_ANTI_RANGE)
6218 *vr0min = vr1min;
6219 else if (*vr0type == VR_RANGE
6220 && vr1type == VR_RANGE)
6221 *vr0max = vr1max;
6222 else if (*vr0type == VR_RANGE
6223 && vr1type == VR_ANTI_RANGE)
6225 if (TREE_CODE (vr1max) == INTEGER_CST)
6226 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
6227 build_int_cst (TREE_TYPE (vr1max), 1));
6228 else
6229 *vr0min = vr1max;
6231 else if (*vr0type == VR_ANTI_RANGE
6232 && vr1type == VR_RANGE)
6234 *vr0type = VR_RANGE;
6235 if (TREE_CODE (*vr0min) == INTEGER_CST)
6236 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
6237 build_int_cst (TREE_TYPE (*vr0min), 1));
6238 else
6239 *vr0max = *vr0min;
6240 *vr0min = vr1min;
6242 else
6243 gcc_unreachable ();
6246 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
6247 result for the intersection. That's always a conservative
6248 correct estimate unless VR1 is a constant singleton range
6249 in which case we choose that. */
6250 if (vr1type == VR_RANGE
6251 && is_gimple_min_invariant (vr1min)
6252 && vrp_operand_equal_p (vr1min, vr1max))
6254 *vr0type = vr1type;
6255 *vr0min = vr1min;
6256 *vr0max = vr1max;
6259 return;
6263 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
6264 in *VR0. This may not be the smallest possible such range. */
6266 static void
6267 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
6269 value_range saved;
6271 /* If either range is VR_VARYING the other one wins. */
6272 if (vr1->type == VR_VARYING)
6273 return;
6274 if (vr0->type == VR_VARYING)
6276 copy_value_range (vr0, vr1);
6277 return;
6280 /* When either range is VR_UNDEFINED the resulting range is
6281 VR_UNDEFINED, too. */
6282 if (vr0->type == VR_UNDEFINED)
6283 return;
6284 if (vr1->type == VR_UNDEFINED)
6286 set_value_range_to_undefined (vr0);
6287 return;
6290 /* Save the original vr0 so we can return it as conservative intersection
6291 result when our worker turns things to varying. */
6292 saved = *vr0;
6293 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
6294 vr1->type, vr1->min, vr1->max);
6295 /* Make sure to canonicalize the result though as the inversion of a
6296 VR_RANGE can still be a VR_RANGE. */
6297 set_and_canonicalize_value_range (vr0, vr0->type,
6298 vr0->min, vr0->max, vr0->equiv);
6299 /* If that failed, use the saved original VR0. */
6300 if (vr0->type == VR_VARYING)
6302 *vr0 = saved;
6303 return;
6305 /* If the result is VR_UNDEFINED there is no need to mess with
6306 the equivalencies. */
6307 if (vr0->type == VR_UNDEFINED)
6308 return;
6310 /* The resulting set of equivalences for range intersection is the union of
6311 the two sets. */
6312 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
6313 bitmap_ior_into (vr0->equiv, vr1->equiv);
6314 else if (vr1->equiv && !vr0->equiv)
6316 /* All equivalence bitmaps are allocated from the same obstack. So
6317 we can use the obstack associated with VR to allocate vr0->equiv. */
6318 vr0->equiv = BITMAP_ALLOC (vr1->equiv->obstack);
6319 bitmap_copy (vr0->equiv, vr1->equiv);
6323 void
6324 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
6326 if (dump_file && (dump_flags & TDF_DETAILS))
6328 fprintf (dump_file, "Intersecting\n ");
6329 dump_value_range (dump_file, vr0);
6330 fprintf (dump_file, "\nand\n ");
6331 dump_value_range (dump_file, vr1);
6332 fprintf (dump_file, "\n");
6334 vrp_intersect_ranges_1 (vr0, vr1);
6335 if (dump_file && (dump_flags & TDF_DETAILS))
6337 fprintf (dump_file, "to\n ");
6338 dump_value_range (dump_file, vr0);
6339 fprintf (dump_file, "\n");
6343 /* Meet operation for value ranges. Given two value ranges VR0 and
6344 VR1, store in VR0 a range that contains both VR0 and VR1. This
6345 may not be the smallest possible such range. */
6347 static void
6348 vrp_meet_1 (value_range *vr0, const value_range *vr1)
6350 value_range saved;
6352 if (vr0->type == VR_UNDEFINED)
6354 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
6355 return;
6358 if (vr1->type == VR_UNDEFINED)
6360 /* VR0 already has the resulting range. */
6361 return;
6364 if (vr0->type == VR_VARYING)
6366 /* Nothing to do. VR0 already has the resulting range. */
6367 return;
6370 if (vr1->type == VR_VARYING)
6372 set_value_range_to_varying (vr0);
6373 return;
6376 saved = *vr0;
6377 union_ranges (&vr0->type, &vr0->min, &vr0->max,
6378 vr1->type, vr1->min, vr1->max);
6379 if (vr0->type == VR_VARYING)
6381 /* Failed to find an efficient meet. Before giving up and setting
6382 the result to VARYING, see if we can at least derive a useful
6383 anti-range. FIXME, all this nonsense about distinguishing
6384 anti-ranges from ranges is necessary because of the odd
6385 semantics of range_includes_zero_p and friends. */
6386 if (((saved.type == VR_RANGE
6387 && range_includes_zero_p (saved.min, saved.max) == 0)
6388 || (saved.type == VR_ANTI_RANGE
6389 && range_includes_zero_p (saved.min, saved.max) == 1))
6390 && ((vr1->type == VR_RANGE
6391 && range_includes_zero_p (vr1->min, vr1->max) == 0)
6392 || (vr1->type == VR_ANTI_RANGE
6393 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
6395 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
6397 /* Since this meet operation did not result from the meeting of
6398 two equivalent names, VR0 cannot have any equivalences. */
6399 if (vr0->equiv)
6400 bitmap_clear (vr0->equiv);
6401 return;
6404 set_value_range_to_varying (vr0);
6405 return;
6407 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
6408 vr0->equiv);
6409 if (vr0->type == VR_VARYING)
6410 return;
6412 /* The resulting set of equivalences is always the intersection of
6413 the two sets. */
6414 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
6415 bitmap_and_into (vr0->equiv, vr1->equiv);
6416 else if (vr0->equiv && !vr1->equiv)
6417 bitmap_clear (vr0->equiv);
6420 void
6421 vrp_meet (value_range *vr0, const value_range *vr1)
6423 if (dump_file && (dump_flags & TDF_DETAILS))
6425 fprintf (dump_file, "Meeting\n ");
6426 dump_value_range (dump_file, vr0);
6427 fprintf (dump_file, "\nand\n ");
6428 dump_value_range (dump_file, vr1);
6429 fprintf (dump_file, "\n");
6431 vrp_meet_1 (vr0, vr1);
6432 if (dump_file && (dump_flags & TDF_DETAILS))
6434 fprintf (dump_file, "to\n ");
6435 dump_value_range (dump_file, vr0);
6436 fprintf (dump_file, "\n");
6441 /* Visit all arguments for PHI node PHI that flow through executable
6442 edges. If a valid value range can be derived from all the incoming
6443 value ranges, set a new range for the LHS of PHI. */
6445 enum ssa_prop_result
6446 vrp_prop::visit_phi (gphi *phi)
6448 tree lhs = PHI_RESULT (phi);
6449 value_range vr_result = VR_INITIALIZER;
6450 extract_range_from_phi_node (phi, &vr_result);
6451 if (update_value_range (lhs, &vr_result))
6453 if (dump_file && (dump_flags & TDF_DETAILS))
6455 fprintf (dump_file, "Found new range for ");
6456 print_generic_expr (dump_file, lhs);
6457 fprintf (dump_file, ": ");
6458 dump_value_range (dump_file, &vr_result);
6459 fprintf (dump_file, "\n");
6462 if (vr_result.type == VR_VARYING)
6463 return SSA_PROP_VARYING;
6465 return SSA_PROP_INTERESTING;
6468 /* Nothing changed, don't add outgoing edges. */
6469 return SSA_PROP_NOT_INTERESTING;
6472 class vrp_folder : public substitute_and_fold_engine
6474 public:
6475 tree get_value (tree) FINAL OVERRIDE;
6476 bool fold_stmt (gimple_stmt_iterator *) FINAL OVERRIDE;
6477 bool fold_predicate_in (gimple_stmt_iterator *);
6479 class vr_values *vr_values;
6481 /* Delegators. */
6482 tree vrp_evaluate_conditional (tree_code code, tree op0,
6483 tree op1, gimple *stmt)
6484 { return vr_values->vrp_evaluate_conditional (code, op0, op1, stmt); }
6485 bool simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
6486 { return vr_values->simplify_stmt_using_ranges (gsi); }
6487 tree op_with_constant_singleton_value_range (tree op)
6488 { return vr_values->op_with_constant_singleton_value_range (op); }
6491 /* If the statement pointed by SI has a predicate whose value can be
6492 computed using the value range information computed by VRP, compute
6493 its value and return true. Otherwise, return false. */
6495 bool
6496 vrp_folder::fold_predicate_in (gimple_stmt_iterator *si)
6498 bool assignment_p = false;
6499 tree val;
6500 gimple *stmt = gsi_stmt (*si);
6502 if (is_gimple_assign (stmt)
6503 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
6505 assignment_p = true;
6506 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
6507 gimple_assign_rhs1 (stmt),
6508 gimple_assign_rhs2 (stmt),
6509 stmt);
6511 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
6512 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
6513 gimple_cond_lhs (cond_stmt),
6514 gimple_cond_rhs (cond_stmt),
6515 stmt);
6516 else
6517 return false;
6519 if (val)
6521 if (assignment_p)
6522 val = fold_convert (gimple_expr_type (stmt), val);
6524 if (dump_file)
6526 fprintf (dump_file, "Folding predicate ");
6527 print_gimple_expr (dump_file, stmt, 0);
6528 fprintf (dump_file, " to ");
6529 print_generic_expr (dump_file, val);
6530 fprintf (dump_file, "\n");
6533 if (is_gimple_assign (stmt))
6534 gimple_assign_set_rhs_from_tree (si, val);
6535 else
6537 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
6538 gcond *cond_stmt = as_a <gcond *> (stmt);
6539 if (integer_zerop (val))
6540 gimple_cond_make_false (cond_stmt);
6541 else if (integer_onep (val))
6542 gimple_cond_make_true (cond_stmt);
6543 else
6544 gcc_unreachable ();
6547 return true;
6550 return false;
6553 /* Callback for substitute_and_fold folding the stmt at *SI. */
6555 bool
6556 vrp_folder::fold_stmt (gimple_stmt_iterator *si)
6558 if (fold_predicate_in (si))
6559 return true;
6561 return simplify_stmt_using_ranges (si);
6564 /* If OP has a value range with a single constant value return that,
6565 otherwise return NULL_TREE. This returns OP itself if OP is a
6566 constant.
6568 Implemented as a pure wrapper right now, but this will change. */
6570 tree
6571 vrp_folder::get_value (tree op)
6573 return op_with_constant_singleton_value_range (op);
6576 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
6577 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
6578 BB. If no such ASSERT_EXPR is found, return OP. */
6580 static tree
6581 lhs_of_dominating_assert (tree op, basic_block bb, gimple *stmt)
6583 imm_use_iterator imm_iter;
6584 gimple *use_stmt;
6585 use_operand_p use_p;
6587 if (TREE_CODE (op) == SSA_NAME)
6589 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
6591 use_stmt = USE_STMT (use_p);
6592 if (use_stmt != stmt
6593 && gimple_assign_single_p (use_stmt)
6594 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
6595 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
6596 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
6597 return gimple_assign_lhs (use_stmt);
6600 return op;
6603 /* A hack. */
6604 static class vr_values *x_vr_values;
6606 /* A trivial wrapper so that we can present the generic jump threading
6607 code with a simple API for simplifying statements. STMT is the
6608 statement we want to simplify, WITHIN_STMT provides the location
6609 for any overflow warnings. */
6611 static tree
6612 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
6613 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED,
6614 basic_block bb)
6616 /* First see if the conditional is in the hash table. */
6617 tree cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, false, true);
6618 if (cached_lhs && is_gimple_min_invariant (cached_lhs))
6619 return cached_lhs;
6621 vr_values *vr_values = x_vr_values;
6622 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
6624 tree op0 = gimple_cond_lhs (cond_stmt);
6625 op0 = lhs_of_dominating_assert (op0, bb, stmt);
6627 tree op1 = gimple_cond_rhs (cond_stmt);
6628 op1 = lhs_of_dominating_assert (op1, bb, stmt);
6630 return vr_values->vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
6631 op0, op1, within_stmt);
6634 /* We simplify a switch statement by trying to determine which case label
6635 will be taken. If we are successful then we return the corresponding
6636 CASE_LABEL_EXPR. */
6637 if (gswitch *switch_stmt = dyn_cast <gswitch *> (stmt))
6639 tree op = gimple_switch_index (switch_stmt);
6640 if (TREE_CODE (op) != SSA_NAME)
6641 return NULL_TREE;
6643 op = lhs_of_dominating_assert (op, bb, stmt);
6645 value_range *vr = vr_values->get_value_range (op);
6646 if ((vr->type != VR_RANGE && vr->type != VR_ANTI_RANGE)
6647 || symbolic_range_p (vr))
6648 return NULL_TREE;
6650 if (vr->type == VR_RANGE)
6652 size_t i, j;
6653 /* Get the range of labels that contain a part of the operand's
6654 value range. */
6655 find_case_label_range (switch_stmt, vr->min, vr->max, &i, &j);
6657 /* Is there only one such label? */
6658 if (i == j)
6660 tree label = gimple_switch_label (switch_stmt, i);
6662 /* The i'th label will be taken only if the value range of the
6663 operand is entirely within the bounds of this label. */
6664 if (CASE_HIGH (label) != NULL_TREE
6665 ? (tree_int_cst_compare (CASE_LOW (label), vr->min) <= 0
6666 && tree_int_cst_compare (CASE_HIGH (label), vr->max) >= 0)
6667 : (tree_int_cst_equal (CASE_LOW (label), vr->min)
6668 && tree_int_cst_equal (vr->min, vr->max)))
6669 return label;
6672 /* If there are no such labels then the default label will be
6673 taken. */
6674 if (i > j)
6675 return gimple_switch_label (switch_stmt, 0);
6678 if (vr->type == VR_ANTI_RANGE)
6680 unsigned n = gimple_switch_num_labels (switch_stmt);
6681 tree min_label = gimple_switch_label (switch_stmt, 1);
6682 tree max_label = gimple_switch_label (switch_stmt, n - 1);
6684 /* The default label will be taken only if the anti-range of the
6685 operand is entirely outside the bounds of all the (non-default)
6686 case labels. */
6687 if (tree_int_cst_compare (vr->min, CASE_LOW (min_label)) <= 0
6688 && (CASE_HIGH (max_label) != NULL_TREE
6689 ? tree_int_cst_compare (vr->max, CASE_HIGH (max_label)) >= 0
6690 : tree_int_cst_compare (vr->max, CASE_LOW (max_label)) >= 0))
6691 return gimple_switch_label (switch_stmt, 0);
6694 return NULL_TREE;
6697 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
6699 tree lhs = gimple_assign_lhs (assign_stmt);
6700 if (TREE_CODE (lhs) == SSA_NAME
6701 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6702 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6703 && stmt_interesting_for_vrp (stmt))
6705 edge dummy_e;
6706 tree dummy_tree;
6707 value_range new_vr = VR_INITIALIZER;
6708 vr_values->extract_range_from_stmt (stmt, &dummy_e,
6709 &dummy_tree, &new_vr);
6710 if (range_int_cst_singleton_p (&new_vr))
6711 return new_vr.min;
6715 return NULL_TREE;
6718 class vrp_dom_walker : public dom_walker
6720 public:
6721 vrp_dom_walker (cdi_direction direction,
6722 class const_and_copies *const_and_copies,
6723 class avail_exprs_stack *avail_exprs_stack)
6724 : dom_walker (direction, REACHABLE_BLOCKS),
6725 m_const_and_copies (const_and_copies),
6726 m_avail_exprs_stack (avail_exprs_stack),
6727 m_dummy_cond (NULL) {}
6729 virtual edge before_dom_children (basic_block);
6730 virtual void after_dom_children (basic_block);
6732 class vr_values *vr_values;
6734 private:
6735 class const_and_copies *m_const_and_copies;
6736 class avail_exprs_stack *m_avail_exprs_stack;
6738 gcond *m_dummy_cond;
6742 /* Called before processing dominator children of BB. We want to look
6743 at ASSERT_EXPRs and record information from them in the appropriate
6744 tables.
6746 We could look at other statements here. It's not seen as likely
6747 to significantly increase the jump threads we discover. */
6749 edge
6750 vrp_dom_walker::before_dom_children (basic_block bb)
6752 gimple_stmt_iterator gsi;
6754 m_avail_exprs_stack->push_marker ();
6755 m_const_and_copies->push_marker ();
6756 for (gsi = gsi_start_nondebug_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6758 gimple *stmt = gsi_stmt (gsi);
6759 if (gimple_assign_single_p (stmt)
6760 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
6762 tree rhs1 = gimple_assign_rhs1 (stmt);
6763 tree cond = TREE_OPERAND (rhs1, 1);
6764 tree inverted = invert_truthvalue (cond);
6765 vec<cond_equivalence> p;
6766 p.create (3);
6767 record_conditions (&p, cond, inverted);
6768 for (unsigned int i = 0; i < p.length (); i++)
6769 m_avail_exprs_stack->record_cond (&p[i]);
6771 tree lhs = gimple_assign_lhs (stmt);
6772 m_const_and_copies->record_const_or_copy (lhs,
6773 TREE_OPERAND (rhs1, 0));
6774 p.release ();
6775 continue;
6777 break;
6779 return NULL;
6782 /* Called after processing dominator children of BB. This is where we
6783 actually call into the threader. */
6784 void
6785 vrp_dom_walker::after_dom_children (basic_block bb)
6787 if (!m_dummy_cond)
6788 m_dummy_cond = gimple_build_cond (NE_EXPR,
6789 integer_zero_node, integer_zero_node,
6790 NULL, NULL);
6792 x_vr_values = vr_values;
6793 thread_outgoing_edges (bb, m_dummy_cond, m_const_and_copies,
6794 m_avail_exprs_stack, NULL,
6795 simplify_stmt_for_jump_threading);
6796 x_vr_values = NULL;
6798 m_avail_exprs_stack->pop_to_marker ();
6799 m_const_and_copies->pop_to_marker ();
6802 /* Blocks which have more than one predecessor and more than
6803 one successor present jump threading opportunities, i.e.,
6804 when the block is reached from a specific predecessor, we
6805 may be able to determine which of the outgoing edges will
6806 be traversed. When this optimization applies, we are able
6807 to avoid conditionals at runtime and we may expose secondary
6808 optimization opportunities.
6810 This routine is effectively a driver for the generic jump
6811 threading code. It basically just presents the generic code
6812 with edges that may be suitable for jump threading.
6814 Unlike DOM, we do not iterate VRP if jump threading was successful.
6815 While iterating may expose new opportunities for VRP, it is expected
6816 those opportunities would be very limited and the compile time cost
6817 to expose those opportunities would be significant.
6819 As jump threading opportunities are discovered, they are registered
6820 for later realization. */
6822 static void
6823 identify_jump_threads (class vr_values *vr_values)
6825 int i;
6826 edge e;
6828 /* Ugh. When substituting values earlier in this pass we can
6829 wipe the dominance information. So rebuild the dominator
6830 information as we need it within the jump threading code. */
6831 calculate_dominance_info (CDI_DOMINATORS);
6833 /* We do not allow VRP information to be used for jump threading
6834 across a back edge in the CFG. Otherwise it becomes too
6835 difficult to avoid eliminating loop exit tests. Of course
6836 EDGE_DFS_BACK is not accurate at this time so we have to
6837 recompute it. */
6838 mark_dfs_back_edges ();
6840 /* Do not thread across edges we are about to remove. Just marking
6841 them as EDGE_IGNORE will do. */
6842 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
6843 e->flags |= EDGE_IGNORE;
6845 /* Allocate our unwinder stack to unwind any temporary equivalences
6846 that might be recorded. */
6847 const_and_copies *equiv_stack = new const_and_copies ();
6849 hash_table<expr_elt_hasher> *avail_exprs
6850 = new hash_table<expr_elt_hasher> (1024);
6851 avail_exprs_stack *avail_exprs_stack
6852 = new class avail_exprs_stack (avail_exprs);
6854 vrp_dom_walker walker (CDI_DOMINATORS, equiv_stack, avail_exprs_stack);
6855 walker.vr_values = vr_values;
6856 walker.walk (cfun->cfg->x_entry_block_ptr);
6858 /* Clear EDGE_IGNORE. */
6859 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
6860 e->flags &= ~EDGE_IGNORE;
6862 /* We do not actually update the CFG or SSA graphs at this point as
6863 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
6864 handle ASSERT_EXPRs gracefully. */
6865 delete equiv_stack;
6866 delete avail_exprs;
6867 delete avail_exprs_stack;
6870 /* Traverse all the blocks folding conditionals with known ranges. */
6872 void
6873 vrp_prop::vrp_finalize (bool warn_array_bounds_p)
6875 size_t i;
6877 /* We have completed propagating through the lattice. */
6878 vr_values.set_lattice_propagation_complete ();
6880 if (dump_file)
6882 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
6883 vr_values.dump_all_value_ranges (dump_file);
6884 fprintf (dump_file, "\n");
6887 /* Set value range to non pointer SSA_NAMEs. */
6888 for (i = 0; i < num_ssa_names; i++)
6890 tree name = ssa_name (i);
6891 if (!name)
6892 continue;
6894 value_range *vr = get_value_range (name);
6895 if (!name
6896 || (vr->type == VR_VARYING)
6897 || (vr->type == VR_UNDEFINED)
6898 || (TREE_CODE (vr->min) != INTEGER_CST)
6899 || (TREE_CODE (vr->max) != INTEGER_CST))
6900 continue;
6902 if (POINTER_TYPE_P (TREE_TYPE (name))
6903 && ((vr->type == VR_RANGE
6904 && range_includes_zero_p (vr->min, vr->max) == 0)
6905 || (vr->type == VR_ANTI_RANGE
6906 && range_includes_zero_p (vr->min, vr->max) == 1)))
6907 set_ptr_nonnull (name);
6908 else if (!POINTER_TYPE_P (TREE_TYPE (name)))
6909 set_range_info (name, vr->type,
6910 wi::to_wide (vr->min),
6911 wi::to_wide (vr->max));
6914 /* If we're checking array refs, we want to merge information on
6915 the executability of each edge between vrp_folder and the
6916 check_array_bounds_dom_walker: each can clear the
6917 EDGE_EXECUTABLE flag on edges, in different ways.
6919 Hence, if we're going to call check_all_array_refs, set
6920 the flag on every edge now, rather than in
6921 check_array_bounds_dom_walker's ctor; vrp_folder may clear
6922 it from some edges. */
6923 if (warn_array_bounds && warn_array_bounds_p)
6924 set_all_edges_as_executable (cfun);
6926 class vrp_folder vrp_folder;
6927 vrp_folder.vr_values = &vr_values;
6928 vrp_folder.substitute_and_fold ();
6930 if (warn_array_bounds && warn_array_bounds_p)
6931 check_all_array_refs ();
6934 /* Main entry point to VRP (Value Range Propagation). This pass is
6935 loosely based on J. R. C. Patterson, ``Accurate Static Branch
6936 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
6937 Programming Language Design and Implementation, pp. 67-78, 1995.
6938 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
6940 This is essentially an SSA-CCP pass modified to deal with ranges
6941 instead of constants.
6943 While propagating ranges, we may find that two or more SSA name
6944 have equivalent, though distinct ranges. For instance,
6946 1 x_9 = p_3->a;
6947 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
6948 3 if (p_4 == q_2)
6949 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
6950 5 endif
6951 6 if (q_2)
6953 In the code above, pointer p_5 has range [q_2, q_2], but from the
6954 code we can also determine that p_5 cannot be NULL and, if q_2 had
6955 a non-varying range, p_5's range should also be compatible with it.
6957 These equivalences are created by two expressions: ASSERT_EXPR and
6958 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
6959 result of another assertion, then we can use the fact that p_5 and
6960 p_4 are equivalent when evaluating p_5's range.
6962 Together with value ranges, we also propagate these equivalences
6963 between names so that we can take advantage of information from
6964 multiple ranges when doing final replacement. Note that this
6965 equivalency relation is transitive but not symmetric.
6967 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
6968 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
6969 in contexts where that assertion does not hold (e.g., in line 6).
6971 TODO, the main difference between this pass and Patterson's is that
6972 we do not propagate edge probabilities. We only compute whether
6973 edges can be taken or not. That is, instead of having a spectrum
6974 of jump probabilities between 0 and 1, we only deal with 0, 1 and
6975 DON'T KNOW. In the future, it may be worthwhile to propagate
6976 probabilities to aid branch prediction. */
6978 static unsigned int
6979 execute_vrp (bool warn_array_bounds_p)
6981 int i;
6982 edge e;
6983 switch_update *su;
6985 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
6986 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
6987 scev_initialize ();
6989 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
6990 Inserting assertions may split edges which will invalidate
6991 EDGE_DFS_BACK. */
6992 insert_range_assertions ();
6994 to_remove_edges.create (10);
6995 to_update_switch_stmts.create (5);
6996 threadedge_initialize_values ();
6998 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
6999 mark_dfs_back_edges ();
7001 class vrp_prop vrp_prop;
7002 vrp_prop.vrp_initialize ();
7003 vrp_prop.ssa_propagate ();
7004 vrp_prop.vrp_finalize (warn_array_bounds_p);
7006 /* We must identify jump threading opportunities before we release
7007 the datastructures built by VRP. */
7008 identify_jump_threads (&vrp_prop.vr_values);
7010 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
7011 was set by a type conversion can often be rewritten to use the
7012 RHS of the type conversion.
7014 However, doing so inhibits jump threading through the comparison.
7015 So that transformation is not performed until after jump threading
7016 is complete. */
7017 basic_block bb;
7018 FOR_EACH_BB_FN (bb, cfun)
7020 gimple *last = last_stmt (bb);
7021 if (last && gimple_code (last) == GIMPLE_COND)
7022 vrp_prop.vr_values.simplify_cond_using_ranges_2 (as_a <gcond *> (last));
7025 free_numbers_of_iterations_estimates (cfun);
7027 /* ASSERT_EXPRs must be removed before finalizing jump threads
7028 as finalizing jump threads calls the CFG cleanup code which
7029 does not properly handle ASSERT_EXPRs. */
7030 remove_range_assertions ();
7032 /* If we exposed any new variables, go ahead and put them into
7033 SSA form now, before we handle jump threading. This simplifies
7034 interactions between rewriting of _DECL nodes into SSA form
7035 and rewriting SSA_NAME nodes into SSA form after block
7036 duplication and CFG manipulation. */
7037 update_ssa (TODO_update_ssa);
7039 /* We identified all the jump threading opportunities earlier, but could
7040 not transform the CFG at that time. This routine transforms the
7041 CFG and arranges for the dominator tree to be rebuilt if necessary.
7043 Note the SSA graph update will occur during the normal TODO
7044 processing by the pass manager. */
7045 thread_through_all_blocks (false);
7047 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
7048 CFG in a broken state and requires a cfg_cleanup run. */
7049 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
7050 remove_edge (e);
7051 /* Update SWITCH_EXPR case label vector. */
7052 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
7054 size_t j;
7055 size_t n = TREE_VEC_LENGTH (su->vec);
7056 tree label;
7057 gimple_switch_set_num_labels (su->stmt, n);
7058 for (j = 0; j < n; j++)
7059 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
7060 /* As we may have replaced the default label with a regular one
7061 make sure to make it a real default label again. This ensures
7062 optimal expansion. */
7063 label = gimple_switch_label (su->stmt, 0);
7064 CASE_LOW (label) = NULL_TREE;
7065 CASE_HIGH (label) = NULL_TREE;
7068 if (to_remove_edges.length () > 0)
7070 free_dominance_info (CDI_DOMINATORS);
7071 loops_state_set (LOOPS_NEED_FIXUP);
7074 to_remove_edges.release ();
7075 to_update_switch_stmts.release ();
7076 threadedge_finalize_values ();
7078 scev_finalize ();
7079 loop_optimizer_finalize ();
7080 return 0;
7083 namespace {
7085 const pass_data pass_data_vrp =
7087 GIMPLE_PASS, /* type */
7088 "vrp", /* name */
7089 OPTGROUP_NONE, /* optinfo_flags */
7090 TV_TREE_VRP, /* tv_id */
7091 PROP_ssa, /* properties_required */
7092 0, /* properties_provided */
7093 0, /* properties_destroyed */
7094 0, /* todo_flags_start */
7095 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
7098 class pass_vrp : public gimple_opt_pass
7100 public:
7101 pass_vrp (gcc::context *ctxt)
7102 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
7105 /* opt_pass methods: */
7106 opt_pass * clone () { return new pass_vrp (m_ctxt); }
7107 void set_pass_param (unsigned int n, bool param)
7109 gcc_assert (n == 0);
7110 warn_array_bounds_p = param;
7112 virtual bool gate (function *) { return flag_tree_vrp != 0; }
7113 virtual unsigned int execute (function *)
7114 { return execute_vrp (warn_array_bounds_p); }
7116 private:
7117 bool warn_array_bounds_p;
7118 }; // class pass_vrp
7120 } // anon namespace
7122 gimple_opt_pass *
7123 make_pass_vrp (gcc::context *ctxt)
7125 return new pass_vrp (ctxt);