1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2019 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)
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/>. */
23 #include "coretypes.h"
25 #include "insn-codes.h"
30 #include "tree-pass.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
40 #include "gimple-fold.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.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"
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"
61 #include "case-cfn-macros.h"
63 #include "alloc-pool.h"
65 #include "tree-cfgcleanup.h"
66 #include "stringpool.h"
68 #include "vr-values.h"
73 ranges_from_anti_range (const value_range_base *ar,
74 value_range_base *vr0, value_range_base *vr1,
75 bool handle_pointers = false);
77 /* Set of SSA names found live during the RPO traversal of the function
78 for still active basic-blocks. */
82 value_range::set_equiv (bitmap equiv)
84 if (undefined_p () || varying_p ())
86 /* Since updating the equivalence set involves deep copying the
87 bitmaps, only do it if absolutely necessary.
89 All equivalence bitmaps are allocated from the same obstack. So
90 we can use the obstack associated with EQUIV to allocate vr->equiv. */
93 m_equiv = BITMAP_ALLOC (equiv->obstack);
97 if (equiv && !bitmap_empty_p (equiv))
98 bitmap_copy (m_equiv, equiv);
100 bitmap_clear (m_equiv);
104 /* Initialize value_range. */
107 value_range::set (enum value_range_kind kind, tree min, tree max,
110 value_range_base::set (kind, min, max);
116 value_range_base::value_range_base (value_range_kind kind, tree min, tree max)
118 set (kind, min, max);
121 value_range::value_range (value_range_kind kind, tree min, tree max,
125 set (kind, min, max, equiv);
128 value_range::value_range (const value_range_base &other)
131 set (other.kind (), other.min(), other.max (), NULL);
134 value_range_base::value_range_base (tree type)
139 value_range_base::value_range_base (enum value_range_kind kind,
141 const wide_int &wmin,
142 const wide_int &wmax)
144 tree min = wide_int_to_tree (type, wmin);
145 tree max = wide_int_to_tree (type, wmax);
146 gcc_checking_assert (kind == VR_RANGE || kind == VR_ANTI_RANGE);
147 set (kind, min, max);
150 value_range_base::value_range_base (tree type,
151 const wide_int &wmin,
152 const wide_int &wmax)
154 tree min = wide_int_to_tree (type, wmin);
155 tree max = wide_int_to_tree (type, wmax);
156 set (VR_RANGE, min, max);
159 value_range_base::value_range_base (tree min, tree max)
161 set (VR_RANGE, min, max);
164 /* Like set, but keep the equivalences in place. */
167 value_range::update (value_range_kind kind, tree min, tree max)
170 (kind != VR_UNDEFINED && kind != VR_VARYING) ? m_equiv : NULL);
173 /* Copy value_range in FROM into THIS while avoiding bitmap sharing.
175 Note: The code that avoids the bitmap sharing looks at the existing
176 this->m_equiv, so this function cannot be used to initalize an
177 object. Use the constructors for initialization. */
180 value_range::deep_copy (const value_range *from)
182 set (from->m_kind, from->min (), from->max (), from->m_equiv);
186 value_range::move (value_range *from)
188 set (from->m_kind, from->min (), from->max ());
189 m_equiv = from->m_equiv;
190 from->m_equiv = NULL;
193 /* Check the validity of the range. */
196 value_range_base::check ()
205 gcc_assert (m_min && m_max);
207 gcc_assert (!TREE_OVERFLOW_P (m_min) && !TREE_OVERFLOW_P (m_max));
209 /* Creating ~[-MIN, +MAX] is stupid because that would be
211 if (INTEGRAL_TYPE_P (TREE_TYPE (m_min)) && m_kind == VR_ANTI_RANGE)
212 gcc_assert (!vrp_val_is_min (m_min) || !vrp_val_is_max (m_max));
214 cmp = compare_values (m_min, m_max);
215 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
219 gcc_assert (!min () && !max ());
222 gcc_assert (m_min && m_max);
230 value_range::check ()
232 value_range_base::check ();
237 gcc_assert (!m_equiv || bitmap_empty_p (m_equiv));
242 /* Equality operator. We purposely do not overload ==, to avoid
243 confusion with the equality bitmap in the derived value_range
247 value_range_base::equal_p (const value_range_base &other) const
249 /* Ignore types for undefined. All undefines are equal. */
251 return m_kind == other.m_kind;
253 return (m_kind == other.m_kind
254 && vrp_operand_equal_p (m_min, other.m_min)
255 && vrp_operand_equal_p (m_max, other.m_max));
258 /* Returns TRUE if THIS == OTHER. Ignores the equivalence bitmap if
259 IGNORE_EQUIVS is TRUE. */
262 value_range::equal_p (const value_range &other, bool ignore_equivs) const
264 return (value_range_base::equal_p (other)
266 || vrp_bitmap_equal_p (m_equiv, other.m_equiv)));
269 /* Return TRUE if this is a symbolic range. */
272 value_range_base::symbolic_p () const
274 return (!varying_p ()
276 && (!is_gimple_min_invariant (m_min)
277 || !is_gimple_min_invariant (m_max)));
280 /* NOTE: This is not the inverse of symbolic_p because the range
281 could also be varying or undefined. Ideally they should be inverse
282 of each other, with varying only applying to symbolics. Varying of
283 constants would be represented as [-MIN, +MAX]. */
286 value_range_base::constant_p () const
288 return (!varying_p ()
290 && TREE_CODE (m_min) == INTEGER_CST
291 && TREE_CODE (m_max) == INTEGER_CST);
295 value_range_base::set_undefined ()
297 m_kind = VR_UNDEFINED;
298 m_min = m_max = NULL;
302 value_range::set_undefined ()
304 set (VR_UNDEFINED, NULL, NULL, NULL);
308 value_range_base::set_varying (tree type)
311 if (supports_type_p (type))
313 m_min = vrp_val_min (type, true);
314 m_max = vrp_val_max (type, true);
317 /* We can't do anything range-wise with these types. */
318 m_min = m_max = error_mark_node;
322 value_range::set_varying (tree type)
324 value_range_base::set_varying (type);
328 /* Return TRUE if it is possible that range contains VAL. */
331 value_range_base::may_contain_p (tree val) const
333 return value_inside_range (val) != 0;
337 value_range::equiv_clear ()
340 bitmap_clear (m_equiv);
343 /* Add VAR and VAR's equivalence set (VAR_VR) to the equivalence
344 bitmap. If no equivalence table has been created, OBSTACK is the
345 obstack to use (NULL for the default obstack).
347 This is the central point where equivalence processing can be
351 value_range::equiv_add (const_tree var,
352 const value_range *var_vr,
353 bitmap_obstack *obstack)
356 m_equiv = BITMAP_ALLOC (obstack);
357 unsigned ver = SSA_NAME_VERSION (var);
358 bitmap_set_bit (m_equiv, ver);
359 if (var_vr && var_vr->m_equiv)
360 bitmap_ior_into (m_equiv, var_vr->m_equiv);
363 /* If range is a singleton, place it in RESULT and return TRUE.
364 Note: A singleton can be any gimple invariant, not just constants.
365 So, [&x, &x] counts as a singleton. */
368 value_range_base::singleton_p (tree *result) const
370 if (m_kind == VR_ANTI_RANGE)
374 if (TYPE_PRECISION (type ()) == 1)
383 /* An anti-range that includes an extreme, is just a range with
384 one sub-range. Use the one sub-range. */
385 if (vrp_val_is_min (m_min, true) || vrp_val_is_max (m_max, true))
387 value_range_base vr0, vr1;
388 ranges_from_anti_range (this, &vr0, &vr1, true);
389 return vr0.singleton_p (result);
392 if (m_kind == VR_RANGE
393 && vrp_operand_equal_p (min (), max ())
394 && is_gimple_min_invariant (min ()))
404 value_range_base::type () const
406 gcc_checking_assert (m_min);
407 return TREE_TYPE (min ());
411 value_range_base::dump (FILE *file) const
414 fprintf (file, "UNDEFINED");
415 else if (m_kind == VR_RANGE || m_kind == VR_ANTI_RANGE)
417 tree ttype = type ();
419 print_generic_expr (file, ttype);
422 fprintf (file, "%s[", (m_kind == VR_ANTI_RANGE) ? "~" : "");
424 if (INTEGRAL_TYPE_P (ttype)
425 && !TYPE_UNSIGNED (ttype)
426 && vrp_val_is_min (min ())
427 && TYPE_PRECISION (ttype) != 1)
428 fprintf (file, "-INF");
430 print_generic_expr (file, min ());
432 fprintf (file, ", ");
434 if (INTEGRAL_TYPE_P (ttype)
435 && vrp_val_is_max (max ())
436 && TYPE_PRECISION (ttype) != 1)
437 fprintf (file, "+INF");
439 print_generic_expr (file, max ());
443 else if (varying_p ())
445 print_generic_expr (file, type ());
446 fprintf (file, " VARYING");
453 value_range_base::dump () const
459 value_range::dump (FILE *file) const
461 value_range_base::dump (file);
462 if ((m_kind == VR_RANGE || m_kind == VR_ANTI_RANGE)
468 fprintf (file, " EQUIVALENCES: { ");
470 EXECUTE_IF_SET_IN_BITMAP (m_equiv, 0, i, bi)
472 print_generic_expr (file, ssa_name (i));
477 fprintf (file, "} (%u elements)", c);
482 value_range::dump () const
488 dump_value_range (FILE *file, const value_range *vr)
491 fprintf (file, "[]");
497 dump_value_range (FILE *file, const value_range_base *vr)
500 fprintf (file, "[]");
506 debug (const value_range_base *vr)
508 dump_value_range (stderr, vr);
512 debug (const value_range_base &vr)
514 dump_value_range (stderr, &vr);
518 debug (const value_range *vr)
520 dump_value_range (stderr, vr);
524 debug (const value_range &vr)
526 dump_value_range (stderr, &vr);
529 /* Return true if the SSA name NAME is live on the edge E. */
532 live_on_edge (edge e, tree name)
534 return (live[e->dest->index]
535 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
538 /* Location information for ASSERT_EXPRs. Each instance of this
539 structure describes an ASSERT_EXPR for an SSA name. Since a single
540 SSA name may have more than one assertion associated with it, these
541 locations are kept in a linked list attached to the corresponding
545 /* Basic block where the assertion would be inserted. */
548 /* Some assertions need to be inserted on an edge (e.g., assertions
549 generated by COND_EXPRs). In those cases, BB will be NULL. */
552 /* Pointer to the statement that generated this assertion. */
553 gimple_stmt_iterator si;
555 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
556 enum tree_code comp_code;
558 /* Value being compared against. */
561 /* Expression to compare. */
564 /* Next node in the linked list. */
568 /* If bit I is present, it means that SSA name N_i has a list of
569 assertions that should be inserted in the IL. */
570 static bitmap need_assert_for;
572 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
573 holds a list of ASSERT_LOCUS_T nodes that describe where
574 ASSERT_EXPRs for SSA name N_I should be inserted. */
575 static assert_locus **asserts_for;
577 /* Return the maximum value for TYPE. */
580 vrp_val_max (const_tree type, bool handle_pointers)
582 if (INTEGRAL_TYPE_P (type))
583 return TYPE_MAX_VALUE (type);
584 if (POINTER_TYPE_P (type) && handle_pointers)
586 wide_int max = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
587 return wide_int_to_tree (const_cast<tree> (type), max);
592 /* Return the minimum value for TYPE. */
595 vrp_val_min (const_tree type, bool handle_pointers)
597 if (INTEGRAL_TYPE_P (type))
598 return TYPE_MIN_VALUE (type);
599 if (POINTER_TYPE_P (type) && handle_pointers)
600 return build_zero_cst (const_cast<tree> (type));
604 /* Return whether VAL is equal to the maximum value of its type.
605 We can't do a simple equality comparison with TYPE_MAX_VALUE because
606 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
607 is not == to the integer constant with the same value in the type. */
610 vrp_val_is_max (const_tree val, bool handle_pointers)
612 tree type_max = vrp_val_max (TREE_TYPE (val), handle_pointers);
613 return (val == type_max
614 || (type_max != NULL_TREE
615 && operand_equal_p (val, type_max, 0)));
618 /* Return whether VAL is equal to the minimum value of its type. */
621 vrp_val_is_min (const_tree val, bool handle_pointers)
623 tree type_min = vrp_val_min (TREE_TYPE (val), handle_pointers);
624 return (val == type_min
625 || (type_min != NULL_TREE
626 && operand_equal_p (val, type_min, 0)));
629 /* VR_TYPE describes a range with mininum value *MIN and maximum
630 value *MAX. Restrict the range to the set of values that have
631 no bits set outside NONZERO_BITS. Update *MIN and *MAX and
632 return the new range type.
634 SGN gives the sign of the values described by the range. */
636 enum value_range_kind
637 intersect_range_with_nonzero_bits (enum value_range_kind vr_type,
638 wide_int *min, wide_int *max,
639 const wide_int &nonzero_bits,
642 if (vr_type == VR_ANTI_RANGE)
644 /* The VR_ANTI_RANGE is equivalent to the union of the ranges
645 A: [-INF, *MIN) and B: (*MAX, +INF]. First use NONZERO_BITS
646 to create an inclusive upper bound for A and an inclusive lower
648 wide_int a_max = wi::round_down_for_mask (*min - 1, nonzero_bits);
649 wide_int b_min = wi::round_up_for_mask (*max + 1, nonzero_bits);
651 /* If the calculation of A_MAX wrapped, A is effectively empty
652 and A_MAX is the highest value that satisfies NONZERO_BITS.
653 Likewise if the calculation of B_MIN wrapped, B is effectively
654 empty and B_MIN is the lowest value that satisfies NONZERO_BITS. */
655 bool a_empty = wi::ge_p (a_max, *min, sgn);
656 bool b_empty = wi::le_p (b_min, *max, sgn);
658 /* If both A and B are empty, there are no valid values. */
659 if (a_empty && b_empty)
662 /* If exactly one of A or B is empty, return a VR_RANGE for the
664 if (a_empty || b_empty)
668 gcc_checking_assert (wi::le_p (*min, *max, sgn));
672 /* Update the VR_ANTI_RANGE bounds. */
675 gcc_checking_assert (wi::le_p (*min, *max, sgn));
677 /* Now check whether the excluded range includes any values that
678 satisfy NONZERO_BITS. If not, switch to a full VR_RANGE. */
679 if (wi::round_up_for_mask (*min, nonzero_bits) == b_min)
681 unsigned int precision = min->get_precision ();
682 *min = wi::min_value (precision, sgn);
683 *max = wi::max_value (precision, sgn);
687 if (vr_type == VR_RANGE)
689 *max = wi::round_down_for_mask (*max, nonzero_bits);
691 /* Check that the range contains at least one valid value. */
692 if (wi::gt_p (*min, *max, sgn))
695 *min = wi::round_up_for_mask (*min, nonzero_bits);
696 gcc_checking_assert (wi::le_p (*min, *max, sgn));
702 /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
703 This means adjusting VRTYPE, MIN and MAX representing the case of a
704 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
705 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
706 In corner cases where MAX+1 or MIN-1 wraps this will fall back
708 This routine exists to ease canonicalization in the case where we
709 extract ranges from var + CST op limit. */
712 value_range_base::set (enum value_range_kind kind, tree min, tree max)
714 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
715 if (kind == VR_UNDEFINED)
720 else if (kind == VR_VARYING)
722 gcc_assert (TREE_TYPE (min) == TREE_TYPE (max));
723 tree typ = TREE_TYPE (min);
724 if (supports_type_p (typ))
726 gcc_assert (vrp_val_min (typ, true));
727 gcc_assert (vrp_val_max (typ, true));
733 /* Nothing to canonicalize for symbolic ranges. */
734 if (TREE_CODE (min) != INTEGER_CST
735 || TREE_CODE (max) != INTEGER_CST)
743 /* Wrong order for min and max, to swap them and the VR type we need
745 if (tree_int_cst_lt (max, min))
749 /* For one bit precision if max < min, then the swapped
750 range covers all values, so for VR_RANGE it is varying and
751 for VR_ANTI_RANGE empty range, so drop to varying as well. */
752 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
754 set_varying (TREE_TYPE (min));
758 one = build_int_cst (TREE_TYPE (min), 1);
759 tmp = int_const_binop (PLUS_EXPR, max, one);
760 max = int_const_binop (MINUS_EXPR, min, one);
763 /* There's one corner case, if we had [C+1, C] before we now have
764 that again. But this represents an empty value range, so drop
765 to varying in this case. */
766 if (tree_int_cst_lt (max, min))
768 set_varying (TREE_TYPE (min));
772 kind = kind == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
775 tree type = TREE_TYPE (min);
777 /* Anti-ranges that can be represented as ranges should be so. */
778 if (kind == VR_ANTI_RANGE)
780 /* For -fstrict-enums we may receive out-of-range ranges so consider
781 values < -INF and values > INF as -INF/INF as well. */
782 bool is_min = (INTEGRAL_TYPE_P (type)
783 && tree_int_cst_compare (min, TYPE_MIN_VALUE (type)) <= 0);
784 bool is_max = (INTEGRAL_TYPE_P (type)
785 && tree_int_cst_compare (max, TYPE_MAX_VALUE (type)) >= 0);
787 if (is_min && is_max)
789 /* We cannot deal with empty ranges, drop to varying.
790 ??? This could be VR_UNDEFINED instead. */
794 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
795 && (is_min || is_max))
797 /* Non-empty boolean ranges can always be represented
798 as a singleton range. */
800 min = max = vrp_val_max (TREE_TYPE (min));
802 min = max = vrp_val_min (TREE_TYPE (min));
806 /* As a special exception preserve non-null ranges. */
807 && !(TYPE_UNSIGNED (TREE_TYPE (min))
808 && integer_zerop (max)))
810 tree one = build_int_cst (TREE_TYPE (max), 1);
811 min = int_const_binop (PLUS_EXPR, max, one);
812 max = vrp_val_max (TREE_TYPE (max));
817 tree one = build_int_cst (TREE_TYPE (min), 1);
818 max = int_const_binop (MINUS_EXPR, min, one);
819 min = vrp_val_min (TREE_TYPE (min));
824 /* Normalize [MIN, MAX] into VARYING and ~[MIN, MAX] into UNDEFINED.
826 Avoid using TYPE_{MIN,MAX}_VALUE because -fstrict-enums can
827 restrict those to a subset of what actually fits in the type.
828 Instead use the extremes of the type precision which will allow
829 compare_range_with_value() to check if a value is inside a range,
830 whereas if we used TYPE_*_VAL, said function would just punt
831 upon seeing a VARYING. */
832 unsigned prec = TYPE_PRECISION (type);
833 signop sign = TYPE_SIGN (type);
834 if (wi::eq_p (wi::to_wide (min), wi::min_value (prec, sign))
835 && wi::eq_p (wi::to_wide (max), wi::max_value (prec, sign)))
837 if (kind == VR_RANGE)
839 else if (kind == VR_ANTI_RANGE)
846 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
847 to make sure VRP iteration terminates, otherwise we can get into
858 value_range_base::set (tree val)
860 gcc_assert (TREE_CODE (val) == SSA_NAME || is_gimple_min_invariant (val));
861 if (TREE_OVERFLOW_P (val))
862 val = drop_tree_overflow (val);
863 set (VR_RANGE, val, val);
867 value_range::set (tree val)
869 gcc_assert (TREE_CODE (val) == SSA_NAME || is_gimple_min_invariant (val));
870 if (TREE_OVERFLOW_P (val))
871 val = drop_tree_overflow (val);
872 set (VR_RANGE, val, val, NULL);
875 /* Set value range VR to a nonzero range of type TYPE. */
878 value_range_base::set_nonzero (tree type)
880 tree zero = build_int_cst (type, 0);
881 set (VR_ANTI_RANGE, zero, zero);
884 /* Set value range VR to a ZERO range of type TYPE. */
887 value_range_base::set_zero (tree type)
889 set (build_int_cst (type, 0));
892 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
895 vrp_operand_equal_p (const_tree val1, const_tree val2)
899 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
904 /* Return true, if the bitmaps B1 and B2 are equal. */
907 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
910 || ((!b1 || bitmap_empty_p (b1))
911 && (!b2 || bitmap_empty_p (b2)))
913 && bitmap_equal_p (b1, b2)));
916 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
920 range_int_cst_p (const value_range_base *vr)
922 return (vr->kind () == VR_RANGE
923 && TREE_CODE (vr->min ()) == INTEGER_CST
924 && TREE_CODE (vr->max ()) == INTEGER_CST);
927 /* Return true if VR is a INTEGER_CST singleton. */
930 range_int_cst_singleton_p (const value_range_base *vr)
932 return (range_int_cst_p (vr)
933 && tree_int_cst_equal (vr->min (), vr->max ()));
936 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
937 otherwise. We only handle additive operations and set NEG to true if the
938 symbol is negated and INV to the invariant part, if any. */
941 get_single_symbol (tree t, bool *neg, tree *inv)
949 if (TREE_CODE (t) == PLUS_EXPR
950 || TREE_CODE (t) == POINTER_PLUS_EXPR
951 || TREE_CODE (t) == MINUS_EXPR)
953 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
955 neg_ = (TREE_CODE (t) == MINUS_EXPR);
956 inv_ = TREE_OPERAND (t, 0);
957 t = TREE_OPERAND (t, 1);
959 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
962 inv_ = TREE_OPERAND (t, 1);
963 t = TREE_OPERAND (t, 0);
974 if (TREE_CODE (t) == NEGATE_EXPR)
976 t = TREE_OPERAND (t, 0);
980 if (TREE_CODE (t) != SSA_NAME)
983 if (inv_ && TREE_OVERFLOW_P (inv_))
984 inv_ = drop_tree_overflow (inv_);
991 /* The reverse operation: build a symbolic expression with TYPE
992 from symbol SYM, negated according to NEG, and invariant INV. */
995 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
997 const bool pointer_p = POINTER_TYPE_P (type);
1001 t = build1 (NEGATE_EXPR, type, t);
1003 if (integer_zerop (inv))
1006 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
1012 -2 if those are incomparable. */
1014 operand_less_p (tree val, tree val2)
1016 /* LT is folded faster than GE and others. Inline the common case. */
1017 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1018 return tree_int_cst_lt (val, val2);
1019 else if (TREE_CODE (val) == SSA_NAME && TREE_CODE (val2) == SSA_NAME)
1020 return val == val2 ? 0 : -2;
1023 int cmp = compare_values (val, val2);
1026 else if (cmp == 0 || cmp == 1)
1035 /* Compare two values VAL1 and VAL2. Return
1037 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1040 +1 if VAL1 > VAL2, and
1043 This is similar to tree_int_cst_compare but supports pointer values
1044 and values that cannot be compared at compile time.
1046 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1047 true if the return value is only valid if we assume that signed
1048 overflow is undefined. */
1051 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1056 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1058 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1059 == POINTER_TYPE_P (TREE_TYPE (val2)));
1061 /* Convert the two values into the same type. This is needed because
1062 sizetype causes sign extension even for unsigned types. */
1063 if (!useless_type_conversion_p (TREE_TYPE (val1), TREE_TYPE (val2)))
1064 val2 = fold_convert (TREE_TYPE (val1), val2);
1066 const bool overflow_undefined
1067 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
1068 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
1071 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
1072 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
1074 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1075 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1078 /* Both values must use the same name with the same sign. */
1079 if (sym1 != sym2 || neg1 != neg2)
1082 /* [-]NAME + CST == [-]NAME + CST. */
1086 /* If overflow is defined we cannot simplify more. */
1087 if (!overflow_undefined)
1090 if (strict_overflow_p != NULL
1091 /* Symbolic range building sets TREE_NO_WARNING to declare
1092 that overflow doesn't happen. */
1093 && (!inv1 || !TREE_NO_WARNING (val1))
1094 && (!inv2 || !TREE_NO_WARNING (val2)))
1095 *strict_overflow_p = true;
1098 inv1 = build_int_cst (TREE_TYPE (val1), 0);
1100 inv2 = build_int_cst (TREE_TYPE (val2), 0);
1102 return wi::cmp (wi::to_wide (inv1), wi::to_wide (inv2),
1103 TYPE_SIGN (TREE_TYPE (val1)));
1106 const bool cst1 = is_gimple_min_invariant (val1);
1107 const bool cst2 = is_gimple_min_invariant (val2);
1109 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1110 it might be possible to say something depending on the constants. */
1111 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
1113 if (!overflow_undefined)
1116 if (strict_overflow_p != NULL
1117 /* Symbolic range building sets TREE_NO_WARNING to declare
1118 that overflow doesn't happen. */
1119 && (!sym1 || !TREE_NO_WARNING (val1))
1120 && (!sym2 || !TREE_NO_WARNING (val2)))
1121 *strict_overflow_p = true;
1123 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
1124 tree cst = cst1 ? val1 : val2;
1125 tree inv = cst1 ? inv2 : inv1;
1127 /* Compute the difference between the constants. If it overflows or
1128 underflows, this means that we can trivially compare the NAME with
1129 it and, consequently, the two values with each other. */
1130 wide_int diff = wi::to_wide (cst) - wi::to_wide (inv);
1131 if (wi::cmp (0, wi::to_wide (inv), sgn)
1132 != wi::cmp (diff, wi::to_wide (cst), sgn))
1134 const int res = wi::cmp (wi::to_wide (cst), wi::to_wide (inv), sgn);
1135 return cst1 ? res : -res;
1141 /* We cannot say anything more for non-constants. */
1145 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1147 /* We cannot compare overflowed values. */
1148 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1151 if (TREE_CODE (val1) == INTEGER_CST
1152 && TREE_CODE (val2) == INTEGER_CST)
1153 return tree_int_cst_compare (val1, val2);
1155 if (poly_int_tree_p (val1) && poly_int_tree_p (val2))
1157 if (known_eq (wi::to_poly_widest (val1),
1158 wi::to_poly_widest (val2)))
1160 if (known_lt (wi::to_poly_widest (val1),
1161 wi::to_poly_widest (val2)))
1163 if (known_gt (wi::to_poly_widest (val1),
1164 wi::to_poly_widest (val2)))
1172 if (TREE_CODE (val1) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1174 /* We cannot compare overflowed values. */
1175 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1178 return tree_int_cst_compare (val1, val2);
1181 /* First see if VAL1 and VAL2 are not the same. */
1182 if (operand_equal_p (val1, val2, 0))
1185 fold_defer_overflow_warnings ();
1187 /* If VAL1 is a lower address than VAL2, return -1. */
1188 tree t = fold_binary_to_constant (LT_EXPR, boolean_type_node, val1, val2);
1189 if (t && integer_onep (t))
1191 fold_undefer_and_ignore_overflow_warnings ();
1195 /* If VAL1 is a higher address than VAL2, return +1. */
1196 t = fold_binary_to_constant (LT_EXPR, boolean_type_node, val2, val1);
1197 if (t && integer_onep (t))
1199 fold_undefer_and_ignore_overflow_warnings ();
1203 /* If VAL1 is different than VAL2, return +2. */
1204 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1205 fold_undefer_and_ignore_overflow_warnings ();
1206 if (t && integer_onep (t))
1213 /* Compare values like compare_values_warnv. */
1216 compare_values (tree val1, tree val2)
1219 return compare_values_warnv (val1, val2, &sop);
1223 /* Return 1 if VAL is inside value range.
1224 0 if VAL is not inside value range.
1225 -2 if we cannot tell either way.
1227 Benchmark compile/20001226-1.c compilation time after changing this
1231 value_range_base::value_inside_range (tree val) const
1241 cmp1 = operand_less_p (val, m_min);
1245 return m_kind != VR_RANGE;
1247 cmp2 = operand_less_p (m_max, val);
1251 if (m_kind == VR_RANGE)
1257 /* For range [LB, UB] compute two wide_int bit masks.
1259 In the MAY_BE_NONZERO bit mask, if some bit is unset, it means that
1260 for all numbers in the range the bit is 0, otherwise it might be 0
1263 In the MUST_BE_NONZERO bit mask, if some bit is set, it means that
1264 for all numbers in the range the bit is 1, otherwise it might be 0
1268 wide_int_range_set_zero_nonzero_bits (signop sign,
1269 const wide_int &lb, const wide_int &ub,
1270 wide_int &may_be_nonzero,
1271 wide_int &must_be_nonzero)
1273 may_be_nonzero = wi::minus_one (lb.get_precision ());
1274 must_be_nonzero = wi::zero (lb.get_precision ());
1276 if (wi::eq_p (lb, ub))
1278 may_be_nonzero = lb;
1279 must_be_nonzero = may_be_nonzero;
1281 else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
1283 wide_int xor_mask = lb ^ ub;
1284 may_be_nonzero = lb | ub;
1285 must_be_nonzero = lb & ub;
1288 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1289 may_be_nonzero.get_precision ());
1290 may_be_nonzero = may_be_nonzero | mask;
1291 must_be_nonzero = wi::bit_and_not (must_be_nonzero, mask);
1296 /* value_range wrapper for wide_int_range_set_zero_nonzero_bits above.
1298 Return TRUE if VR was a constant range and we were able to compute
1302 vrp_set_zero_nonzero_bits (const tree expr_type,
1303 const value_range_base *vr,
1304 wide_int *may_be_nonzero,
1305 wide_int *must_be_nonzero)
1307 if (!range_int_cst_p (vr))
1309 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1310 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1313 wide_int_range_set_zero_nonzero_bits (TYPE_SIGN (expr_type),
1314 wi::to_wide (vr->min ()),
1315 wi::to_wide (vr->max ()),
1316 *may_be_nonzero, *must_be_nonzero);
1320 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1321 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1322 false otherwise. If *AR can be represented with a single range
1323 *VR1 will be VR_UNDEFINED. */
1326 ranges_from_anti_range (const value_range_base *ar,
1327 value_range_base *vr0, value_range_base *vr1,
1328 bool handle_pointers)
1330 tree type = ar->type ();
1332 vr0->set_undefined ();
1333 vr1->set_undefined ();
1335 /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U
1336 [A+1, +INF]. Not sure if this helps in practice, though. */
1338 if (ar->kind () != VR_ANTI_RANGE
1339 || TREE_CODE (ar->min ()) != INTEGER_CST
1340 || TREE_CODE (ar->max ()) != INTEGER_CST
1341 || !vrp_val_min (type, handle_pointers)
1342 || !vrp_val_max (type, handle_pointers))
1345 if (tree_int_cst_lt (vrp_val_min (type, handle_pointers), ar->min ()))
1347 vrp_val_min (type, handle_pointers),
1348 wide_int_to_tree (type, wi::to_wide (ar->min ()) - 1));
1349 if (tree_int_cst_lt (ar->max (), vrp_val_max (type, handle_pointers)))
1351 wide_int_to_tree (type, wi::to_wide (ar->max ()) + 1),
1352 vrp_val_max (type, handle_pointers));
1353 if (vr0->undefined_p ())
1356 vr1->set_undefined ();
1359 return !vr0->undefined_p ();
1362 /* If BOUND will include a symbolic bound, adjust it accordingly,
1363 otherwise leave it as is.
1365 CODE is the original operation that combined the bounds (PLUS_EXPR
1368 TYPE is the type of the original operation.
1370 SYM_OPn is the symbolic for OPn if it has a symbolic.
1372 NEG_OPn is TRUE if the OPn was negated. */
1375 adjust_symbolic_bound (tree &bound, enum tree_code code, tree type,
1376 tree sym_op0, tree sym_op1,
1377 bool neg_op0, bool neg_op1)
1379 bool minus_p = (code == MINUS_EXPR);
1380 /* If the result bound is constant, we're done; otherwise, build the
1381 symbolic lower bound. */
1382 if (sym_op0 == sym_op1)
1385 bound = build_symbolic_expr (type, sym_op0,
1389 /* We may not negate if that might introduce
1390 undefined overflow. */
1393 || TYPE_OVERFLOW_WRAPS (type))
1394 bound = build_symbolic_expr (type, sym_op1,
1395 neg_op1 ^ minus_p, bound);
1401 /* Combine OP1 and OP1, which are two parts of a bound, into one wide
1402 int bound according to CODE. CODE is the operation combining the
1403 bound (either a PLUS_EXPR or a MINUS_EXPR).
1405 TYPE is the type of the combine operation.
1407 WI is the wide int to store the result.
1409 OVF is -1 if an underflow occurred, +1 if an overflow occurred or 0
1410 if over/underflow occurred. */
1413 combine_bound (enum tree_code code, wide_int &wi, wi::overflow_type &ovf,
1414 tree type, tree op0, tree op1)
1416 bool minus_p = (code == MINUS_EXPR);
1417 const signop sgn = TYPE_SIGN (type);
1418 const unsigned int prec = TYPE_PRECISION (type);
1420 /* Combine the bounds, if any. */
1424 wi = wi::sub (wi::to_wide (op0), wi::to_wide (op1), sgn, &ovf);
1426 wi = wi::add (wi::to_wide (op0), wi::to_wide (op1), sgn, &ovf);
1429 wi = wi::to_wide (op0);
1433 wi = wi::neg (wi::to_wide (op1), &ovf);
1435 wi = wi::to_wide (op1);
1438 wi = wi::shwi (0, prec);
1441 /* Given a range in [WMIN, WMAX], adjust it for possible overflow and
1442 put the result in VR.
1444 TYPE is the type of the range.
1446 MIN_OVF and MAX_OVF indicate what type of overflow, if any,
1447 occurred while originally calculating WMIN or WMAX. -1 indicates
1448 underflow. +1 indicates overflow. 0 indicates neither. */
1451 set_value_range_with_overflow (value_range_kind &kind, tree &min, tree &max,
1453 const wide_int &wmin, const wide_int &wmax,
1454 wi::overflow_type min_ovf,
1455 wi::overflow_type max_ovf)
1457 const signop sgn = TYPE_SIGN (type);
1458 const unsigned int prec = TYPE_PRECISION (type);
1460 /* For one bit precision if max < min, then the swapped
1461 range covers all values. */
1462 if (prec == 1 && wi::lt_p (wmax, wmin, sgn))
1468 if (TYPE_OVERFLOW_WRAPS (type))
1470 /* If overflow wraps, truncate the values and adjust the
1471 range kind and bounds appropriately. */
1472 wide_int tmin = wide_int::from (wmin, prec, sgn);
1473 wide_int tmax = wide_int::from (wmax, prec, sgn);
1474 if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
1476 /* If the limits are swapped, we wrapped around and cover
1477 the entire range. */
1478 if (wi::gt_p (tmin, tmax, sgn))
1483 /* No overflow or both overflow or underflow. The
1484 range kind stays VR_RANGE. */
1485 min = wide_int_to_tree (type, tmin);
1486 max = wide_int_to_tree (type, tmax);
1490 else if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE)
1491 || (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE))
1493 /* Min underflow or max overflow. The range kind
1494 changes to VR_ANTI_RANGE. */
1495 bool covers = false;
1496 wide_int tem = tmin;
1498 if (wi::cmp (tmin, tmax, sgn) < 0)
1501 if (wi::cmp (tmax, tem, sgn) > 0)
1503 /* If the anti-range would cover nothing, drop to varying.
1504 Likewise if the anti-range bounds are outside of the
1506 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
1511 kind = VR_ANTI_RANGE;
1512 min = wide_int_to_tree (type, tmin);
1513 max = wide_int_to_tree (type, tmax);
1518 /* Other underflow and/or overflow, drop to VR_VARYING. */
1525 /* If overflow does not wrap, saturate to the types min/max
1527 wide_int type_min = wi::min_value (prec, sgn);
1528 wide_int type_max = wi::max_value (prec, sgn);
1530 if (min_ovf == wi::OVF_UNDERFLOW)
1531 min = wide_int_to_tree (type, type_min);
1532 else if (min_ovf == wi::OVF_OVERFLOW)
1533 min = wide_int_to_tree (type, type_max);
1535 min = wide_int_to_tree (type, wmin);
1537 if (max_ovf == wi::OVF_UNDERFLOW)
1538 max = wide_int_to_tree (type, type_min);
1539 else if (max_ovf == wi::OVF_OVERFLOW)
1540 max = wide_int_to_tree (type, type_max);
1542 max = wide_int_to_tree (type, wmax);
1546 /* Fold two value range's of a POINTER_PLUS_EXPR into VR. */
1549 extract_range_from_pointer_plus_expr (value_range_base *vr,
1550 enum tree_code code,
1552 const value_range_base *vr0,
1553 const value_range_base *vr1)
1555 gcc_checking_assert (POINTER_TYPE_P (expr_type)
1556 && code == POINTER_PLUS_EXPR);
1557 /* For pointer types, we are really only interested in asserting
1558 whether the expression evaluates to non-NULL.
1559 With -fno-delete-null-pointer-checks we need to be more
1560 conservative. As some object might reside at address 0,
1561 then some offset could be added to it and the same offset
1562 subtracted again and the result would be NULL.
1564 static int a[12]; where &a[0] is NULL and
1567 ptr will be NULL here, even when there is POINTER_PLUS_EXPR
1568 where the first range doesn't include zero and the second one
1569 doesn't either. As the second operand is sizetype (unsigned),
1570 consider all ranges where the MSB could be set as possible
1571 subtractions where the result might be NULL. */
1572 if ((!range_includes_zero_p (vr0)
1573 || !range_includes_zero_p (vr1))
1574 && !TYPE_OVERFLOW_WRAPS (expr_type)
1575 && (flag_delete_null_pointer_checks
1576 || (range_int_cst_p (vr1)
1577 && !tree_int_cst_sign_bit (vr1->max ()))))
1578 vr->set_nonzero (expr_type);
1579 else if (vr0->zero_p () && vr1->zero_p ())
1580 vr->set_zero (expr_type);
1582 vr->set_varying (expr_type);
1585 /* Extract range information from a PLUS/MINUS_EXPR and store the
1589 extract_range_from_plus_minus_expr (value_range_base *vr,
1590 enum tree_code code,
1592 const value_range_base *vr0_,
1593 const value_range_base *vr1_)
1595 gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR);
1597 value_range_base vr0 = *vr0_, vr1 = *vr1_;
1598 value_range_base vrtem0, vrtem1;
1600 /* Now canonicalize anti-ranges to ranges when they are not symbolic
1601 and express ~[] op X as ([]' op X) U ([]'' op X). */
1602 if (vr0.kind () == VR_ANTI_RANGE
1603 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
1605 extract_range_from_plus_minus_expr (vr, code, expr_type, &vrtem0, vr1_);
1606 if (!vrtem1.undefined_p ())
1608 value_range_base vrres;
1609 extract_range_from_plus_minus_expr (&vrres, code, expr_type,
1611 vr->union_ (&vrres);
1615 /* Likewise for X op ~[]. */
1616 if (vr1.kind () == VR_ANTI_RANGE
1617 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
1619 extract_range_from_plus_minus_expr (vr, code, expr_type, vr0_, &vrtem0);
1620 if (!vrtem1.undefined_p ())
1622 value_range_base vrres;
1623 extract_range_from_plus_minus_expr (&vrres, code, expr_type,
1625 vr->union_ (&vrres);
1630 value_range_kind kind;
1631 value_range_kind vr0_kind = vr0.kind (), vr1_kind = vr1.kind ();
1632 tree vr0_min = vr0.min (), vr0_max = vr0.max ();
1633 tree vr1_min = vr1.min (), vr1_max = vr1.max ();
1634 tree min = NULL, max = NULL;
1636 /* This will normalize things such that calculating
1637 [0,0] - VR_VARYING is not dropped to varying, but is
1638 calculated as [MIN+1, MAX]. */
1639 if (vr0.varying_p ())
1641 vr0_kind = VR_RANGE;
1642 vr0_min = vrp_val_min (expr_type);
1643 vr0_max = vrp_val_max (expr_type);
1645 if (vr1.varying_p ())
1647 vr1_kind = VR_RANGE;
1648 vr1_min = vrp_val_min (expr_type);
1649 vr1_max = vrp_val_max (expr_type);
1652 const bool minus_p = (code == MINUS_EXPR);
1653 tree min_op0 = vr0_min;
1654 tree min_op1 = minus_p ? vr1_max : vr1_min;
1655 tree max_op0 = vr0_max;
1656 tree max_op1 = minus_p ? vr1_min : vr1_max;
1657 tree sym_min_op0 = NULL_TREE;
1658 tree sym_min_op1 = NULL_TREE;
1659 tree sym_max_op0 = NULL_TREE;
1660 tree sym_max_op1 = NULL_TREE;
1661 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
1663 neg_min_op0 = neg_min_op1 = neg_max_op0 = neg_max_op1 = false;
1665 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
1666 single-symbolic ranges, try to compute the precise resulting range,
1667 but only if we know that this resulting range will also be constant
1668 or single-symbolic. */
1669 if (vr0_kind == VR_RANGE && vr1_kind == VR_RANGE
1670 && (TREE_CODE (min_op0) == INTEGER_CST
1672 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
1673 && (TREE_CODE (min_op1) == INTEGER_CST
1675 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
1676 && (!(sym_min_op0 && sym_min_op1)
1677 || (sym_min_op0 == sym_min_op1
1678 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
1679 && (TREE_CODE (max_op0) == INTEGER_CST
1681 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
1682 && (TREE_CODE (max_op1) == INTEGER_CST
1684 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
1685 && (!(sym_max_op0 && sym_max_op1)
1686 || (sym_max_op0 == sym_max_op1
1687 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
1689 wide_int wmin, wmax;
1690 wi::overflow_type min_ovf = wi::OVF_NONE;
1691 wi::overflow_type max_ovf = wi::OVF_NONE;
1693 /* Build the bounds. */
1694 combine_bound (code, wmin, min_ovf, expr_type, min_op0, min_op1);
1695 combine_bound (code, wmax, max_ovf, expr_type, max_op0, max_op1);
1697 /* If we have overflow for the constant part and the resulting
1698 range will be symbolic, drop to VR_VARYING. */
1699 if (((bool)min_ovf && sym_min_op0 != sym_min_op1)
1700 || ((bool)max_ovf && sym_max_op0 != sym_max_op1))
1702 vr->set_varying (expr_type);
1706 /* Adjust the range for possible overflow. */
1709 set_value_range_with_overflow (kind, min, max, expr_type,
1710 wmin, wmax, min_ovf, max_ovf);
1711 if (kind == VR_VARYING)
1713 vr->set_varying (expr_type);
1717 /* Build the symbolic bounds if needed. */
1718 adjust_symbolic_bound (min, code, expr_type,
1719 sym_min_op0, sym_min_op1,
1720 neg_min_op0, neg_min_op1);
1721 adjust_symbolic_bound (max, code, expr_type,
1722 sym_max_op0, sym_max_op1,
1723 neg_max_op0, neg_max_op1);
1727 /* For other cases, for example if we have a PLUS_EXPR with two
1728 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
1729 to compute a precise range for such a case.
1730 ??? General even mixed range kind operations can be expressed
1731 by for example transforming ~[3, 5] + [1, 2] to range-only
1732 operations and a union primitive:
1733 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
1734 [-INF+1, 4] U [6, +INF(OVF)]
1735 though usually the union is not exactly representable with
1736 a single range or anti-range as the above is
1737 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
1738 but one could use a scheme similar to equivalences for this. */
1739 vr->set_varying (expr_type);
1743 /* If either MIN or MAX overflowed, then set the resulting range to
1745 if (min == NULL_TREE
1746 || TREE_OVERFLOW_P (min)
1748 || TREE_OVERFLOW_P (max))
1750 vr->set_varying (expr_type);
1754 int cmp = compare_values (min, max);
1755 if (cmp == -2 || cmp == 1)
1757 /* If the new range has its limits swapped around (MIN > MAX),
1758 then the operation caused one of them to wrap around, mark
1759 the new range VARYING. */
1760 vr->set_varying (expr_type);
1763 vr->set (kind, min, max);
1766 /* Normalize a value_range for use in range_ops and return it. */
1768 static value_range_base
1769 normalize_for_range_ops (const value_range_base &vr)
1771 tree type = vr.type ();
1773 /* This will return ~[0,0] for [&var, &var]. */
1774 if (POINTER_TYPE_P (type) && !range_includes_zero_p (&vr))
1776 value_range_base temp;
1777 temp.set_nonzero (type);
1780 if (vr.symbolic_p ())
1781 return normalize_for_range_ops (vr.normalize_symbolics ());
1782 if (TREE_CODE (vr.min ()) == INTEGER_CST
1783 && TREE_CODE (vr.max ()) == INTEGER_CST)
1785 /* Anything not strictly numeric at this point becomes varying. */
1786 return value_range_base (vr.type ());
1789 /* Fold a binary expression of two value_range's with range-ops. */
1792 range_fold_binary_expr (value_range_base *vr,
1793 enum tree_code code,
1795 const value_range_base *vr0_,
1796 const value_range_base *vr1_)
1798 if (!value_range_base::supports_type_p (expr_type)
1799 || (!vr0_->undefined_p ()
1800 && !value_range_base::supports_type_p (vr0_->type ()))
1801 || (!vr1_->undefined_p ()
1802 && !value_range_base::supports_type_p (vr1_->type ())))
1804 vr->set_varying (expr_type);
1807 if (vr0_->undefined_p () && vr1_->undefined_p ())
1809 vr->set_undefined ();
1812 range_operator *op = range_op_handler (code, expr_type);
1815 vr->set_varying (expr_type);
1819 /* Mimic any behavior users of extract_range_from_binary_expr may
1821 value_range_base vr0 = *vr0_, vr1 = *vr1_;
1822 if (vr0.undefined_p ())
1823 vr0.set_varying (expr_type);
1824 else if (vr1.undefined_p ())
1825 vr1.set_varying (expr_type);
1827 /* Handle symbolics. */
1828 if (vr0.symbolic_p () || vr1.symbolic_p ())
1830 if ((code == PLUS_EXPR || code == MINUS_EXPR))
1832 extract_range_from_plus_minus_expr (vr, code, expr_type,
1836 if (POINTER_TYPE_P (expr_type) && code == POINTER_PLUS_EXPR)
1838 extract_range_from_pointer_plus_expr (vr, code, expr_type,
1844 /* Do the range-ops dance. */
1845 value_range_base n0 = normalize_for_range_ops (vr0);
1846 value_range_base n1 = normalize_for_range_ops (vr1);
1847 *vr = op->fold_range (expr_type, n0, n1);
1850 /* Fold a unary expression of a value_range with range-ops. */
1853 range_fold_unary_expr (value_range_base *vr,
1854 enum tree_code code, tree expr_type,
1855 const value_range_base *vr0,
1858 /* Mimic any behavior users of extract_range_from_unary_expr may
1860 if (!value_range_base::supports_type_p (expr_type)
1861 || !value_range_base::supports_type_p (vr0_type))
1863 vr->set_varying (expr_type);
1866 if (vr0->undefined_p ())
1868 vr->set_undefined ();
1871 range_operator *op = range_op_handler (code, expr_type);
1874 vr->set_varying (expr_type);
1878 /* Handle symbolics. */
1879 if (vr0->symbolic_p ())
1881 if (code == NEGATE_EXPR)
1883 /* -X is simply 0 - X. */
1884 value_range_base zero;
1885 zero.set_zero (vr0->type ());
1886 range_fold_binary_expr (vr, MINUS_EXPR, expr_type, &zero, vr0);
1889 if (code == BIT_NOT_EXPR)
1891 /* ~X is simply -1 - X. */
1892 value_range_base minusone;
1893 minusone.set (build_int_cst (vr0->type (), -1));
1894 range_fold_binary_expr (vr, MINUS_EXPR, expr_type, &minusone, vr0);
1897 *vr = op->fold_range (expr_type,
1898 normalize_for_range_ops (*vr0),
1899 value_range_base (expr_type));
1902 if (CONVERT_EXPR_CODE_P (code) && (POINTER_TYPE_P (expr_type)
1903 || POINTER_TYPE_P (vr0->type ())))
1905 /* This handles symbolic conversions such such as [25, x_4]. */
1906 if (!range_includes_zero_p (vr0))
1907 vr->set_nonzero (expr_type);
1908 else if (vr0->zero_p ())
1909 vr->set_zero (expr_type);
1911 vr->set_varying (expr_type);
1915 /* Do the range-ops dance. */
1916 value_range_base n0 = normalize_for_range_ops (*vr0);
1917 value_range_base n1 (expr_type);
1918 *vr = op->fold_range (expr_type, n0, n1);
1921 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
1922 create a new SSA name N and return the assertion assignment
1923 'N = ASSERT_EXPR <V, V OP W>'. */
1926 build_assert_expr_for (tree cond, tree v)
1931 gcc_assert (TREE_CODE (v) == SSA_NAME
1932 && COMPARISON_CLASS_P (cond));
1934 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
1935 assertion = gimple_build_assign (NULL_TREE, a);
1937 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
1938 operand of the ASSERT_EXPR. Create it so the new name and the old one
1939 are registered in the replacement table so that we can fix the SSA web
1940 after adding all the ASSERT_EXPRs. */
1941 tree new_def = create_new_def_for (v, assertion, NULL);
1942 /* Make sure we preserve abnormalness throughout an ASSERT_EXPR chain
1943 given we have to be able to fully propagate those out to re-create
1944 valid SSA when removing the asserts. */
1945 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (v))
1946 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_def) = 1;
1952 /* Return false if EXPR is a predicate expression involving floating
1956 fp_predicate (gimple *stmt)
1958 GIMPLE_CHECK (stmt, GIMPLE_COND);
1960 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
1963 /* If the range of values taken by OP can be inferred after STMT executes,
1964 return the comparison code (COMP_CODE_P) and value (VAL_P) that
1965 describes the inferred range. Return true if a range could be
1969 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
1972 *comp_code_p = ERROR_MARK;
1974 /* Do not attempt to infer anything in names that flow through
1976 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
1979 /* If STMT is the last statement of a basic block with no normal
1980 successors, there is no point inferring anything about any of its
1981 operands. We would not be able to find a proper insertion point
1982 for the assertion, anyway. */
1983 if (stmt_ends_bb_p (stmt))
1988 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
1989 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
1995 if (infer_nonnull_range (stmt, op))
1997 *val_p = build_int_cst (TREE_TYPE (op), 0);
1998 *comp_code_p = NE_EXPR;
2006 void dump_asserts_for (FILE *, tree);
2007 void debug_asserts_for (tree);
2008 void dump_all_asserts (FILE *);
2009 void debug_all_asserts (void);
2011 /* Dump all the registered assertions for NAME to FILE. */
2014 dump_asserts_for (FILE *file, tree name)
2018 fprintf (file, "Assertions to be inserted for ");
2019 print_generic_expr (file, name);
2020 fprintf (file, "\n");
2022 loc = asserts_for[SSA_NAME_VERSION (name)];
2025 fprintf (file, "\t");
2026 print_gimple_stmt (file, gsi_stmt (loc->si), 0);
2027 fprintf (file, "\n\tBB #%d", loc->bb->index);
2030 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
2031 loc->e->dest->index);
2032 dump_edge_info (file, loc->e, dump_flags, 0);
2034 fprintf (file, "\n\tPREDICATE: ");
2035 print_generic_expr (file, loc->expr);
2036 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
2037 print_generic_expr (file, loc->val);
2038 fprintf (file, "\n\n");
2042 fprintf (file, "\n");
2046 /* Dump all the registered assertions for NAME to stderr. */
2049 debug_asserts_for (tree name)
2051 dump_asserts_for (stderr, name);
2055 /* Dump all the registered assertions for all the names to FILE. */
2058 dump_all_asserts (FILE *file)
2063 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
2064 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
2065 dump_asserts_for (file, ssa_name (i));
2066 fprintf (file, "\n");
2070 /* Dump all the registered assertions for all the names to stderr. */
2073 debug_all_asserts (void)
2075 dump_all_asserts (stderr);
2078 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
2081 add_assert_info (vec<assert_info> &asserts,
2082 tree name, tree expr, enum tree_code comp_code, tree val)
2085 info.comp_code = comp_code;
2087 if (TREE_OVERFLOW_P (val))
2088 val = drop_tree_overflow (val);
2091 asserts.safe_push (info);
2092 if (dump_enabled_p ())
2093 dump_printf (MSG_NOTE | MSG_PRIORITY_INTERNALS,
2094 "Adding assert for %T from %T %s %T\n",
2095 name, expr, op_symbol_code (comp_code), val);
2098 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2099 'EXPR COMP_CODE VAL' at a location that dominates block BB or
2100 E->DEST, then register this location as a possible insertion point
2101 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
2103 BB, E and SI provide the exact insertion point for the new
2104 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2105 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2106 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2107 must not be NULL. */
2110 register_new_assert_for (tree name, tree expr,
2111 enum tree_code comp_code,
2115 gimple_stmt_iterator si)
2117 assert_locus *n, *loc, *last_loc;
2118 basic_block dest_bb;
2120 gcc_checking_assert (bb == NULL || e == NULL);
2123 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
2124 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
2126 /* Never build an assert comparing against an integer constant with
2127 TREE_OVERFLOW set. This confuses our undefined overflow warning
2129 if (TREE_OVERFLOW_P (val))
2130 val = drop_tree_overflow (val);
2132 /* The new assertion A will be inserted at BB or E. We need to
2133 determine if the new location is dominated by a previously
2134 registered location for A. If we are doing an edge insertion,
2135 assume that A will be inserted at E->DEST. Note that this is not
2138 If E is a critical edge, it will be split. But even if E is
2139 split, the new block will dominate the same set of blocks that
2142 The reverse, however, is not true, blocks dominated by E->DEST
2143 will not be dominated by the new block created to split E. So,
2144 if the insertion location is on a critical edge, we will not use
2145 the new location to move another assertion previously registered
2146 at a block dominated by E->DEST. */
2147 dest_bb = (bb) ? bb : e->dest;
2149 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
2150 VAL at a block dominating DEST_BB, then we don't need to insert a new
2151 one. Similarly, if the same assertion already exists at a block
2152 dominated by DEST_BB and the new location is not on a critical
2153 edge, then update the existing location for the assertion (i.e.,
2154 move the assertion up in the dominance tree).
2156 Note, this is implemented as a simple linked list because there
2157 should not be more than a handful of assertions registered per
2158 name. If this becomes a performance problem, a table hashed by
2159 COMP_CODE and VAL could be implemented. */
2160 loc = asserts_for[SSA_NAME_VERSION (name)];
2164 if (loc->comp_code == comp_code
2166 || operand_equal_p (loc->val, val, 0))
2167 && (loc->expr == expr
2168 || operand_equal_p (loc->expr, expr, 0)))
2170 /* If E is not a critical edge and DEST_BB
2171 dominates the existing location for the assertion, move
2172 the assertion up in the dominance tree by updating its
2173 location information. */
2174 if ((e == NULL || !EDGE_CRITICAL_P (e))
2175 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
2184 /* Update the last node of the list and move to the next one. */
2189 /* If we didn't find an assertion already registered for
2190 NAME COMP_CODE VAL, add a new one at the end of the list of
2191 assertions associated with NAME. */
2192 n = XNEW (struct assert_locus);
2196 n->comp_code = comp_code;
2204 asserts_for[SSA_NAME_VERSION (name)] = n;
2206 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
2209 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
2210 Extract a suitable test code and value and store them into *CODE_P and
2211 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
2213 If no extraction was possible, return FALSE, otherwise return TRUE.
2215 If INVERT is true, then we invert the result stored into *CODE_P. */
2218 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
2219 tree cond_op0, tree cond_op1,
2220 bool invert, enum tree_code *code_p,
2223 enum tree_code comp_code;
2226 /* Otherwise, we have a comparison of the form NAME COMP VAL
2227 or VAL COMP NAME. */
2228 if (name == cond_op1)
2230 /* If the predicate is of the form VAL COMP NAME, flip
2231 COMP around because we need to register NAME as the
2232 first operand in the predicate. */
2233 comp_code = swap_tree_comparison (cond_code);
2236 else if (name == cond_op0)
2238 /* The comparison is of the form NAME COMP VAL, so the
2239 comparison code remains unchanged. */
2240 comp_code = cond_code;
2246 /* Invert the comparison code as necessary. */
2248 comp_code = invert_tree_comparison (comp_code, 0);
2250 /* VRP only handles integral and pointer types. */
2251 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
2252 && ! POINTER_TYPE_P (TREE_TYPE (val)))
2255 /* Do not register always-false predicates.
2256 FIXME: this works around a limitation in fold() when dealing with
2257 enumerations. Given 'enum { N1, N2 } x;', fold will not
2258 fold 'if (x > N2)' to 'if (0)'. */
2259 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
2260 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2262 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
2263 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
2265 if (comp_code == GT_EXPR
2267 || compare_values (val, max) == 0))
2270 if (comp_code == LT_EXPR
2272 || compare_values (val, min) == 0))
2275 *code_p = comp_code;
2280 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
2281 (otherwise return VAL). VAL and MASK must be zero-extended for
2282 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
2283 (to transform signed values into unsigned) and at the end xor
2287 masked_increment (const wide_int &val_in, const wide_int &mask,
2288 const wide_int &sgnbit, unsigned int prec)
2290 wide_int bit = wi::one (prec), res;
2293 wide_int val = val_in ^ sgnbit;
2294 for (i = 0; i < prec; i++, bit += bit)
2297 if ((res & bit) == 0)
2300 res = wi::bit_and_not (val + bit, res);
2302 if (wi::gtu_p (res, val))
2303 return res ^ sgnbit;
2305 return val ^ sgnbit;
2308 /* Helper for overflow_comparison_p
2310 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
2311 OP1's defining statement to see if it ultimately has the form
2312 OP0 CODE (OP0 PLUS INTEGER_CST)
2314 If so, return TRUE indicating this is an overflow test and store into
2315 *NEW_CST an updated constant that can be used in a narrowed range test.
2317 REVERSED indicates if the comparison was originally:
2321 This affects how we build the updated constant. */
2324 overflow_comparison_p_1 (enum tree_code code, tree op0, tree op1,
2325 bool follow_assert_exprs, bool reversed, tree *new_cst)
2327 /* See if this is a relational operation between two SSA_NAMES with
2328 unsigned, overflow wrapping values. If so, check it more deeply. */
2329 if ((code == LT_EXPR || code == LE_EXPR
2330 || code == GE_EXPR || code == GT_EXPR)
2331 && TREE_CODE (op0) == SSA_NAME
2332 && TREE_CODE (op1) == SSA_NAME
2333 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
2334 && TYPE_UNSIGNED (TREE_TYPE (op0))
2335 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
2337 gimple *op1_def = SSA_NAME_DEF_STMT (op1);
2339 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
2340 if (follow_assert_exprs)
2342 while (gimple_assign_single_p (op1_def)
2343 && TREE_CODE (gimple_assign_rhs1 (op1_def)) == ASSERT_EXPR)
2345 op1 = TREE_OPERAND (gimple_assign_rhs1 (op1_def), 0);
2346 if (TREE_CODE (op1) != SSA_NAME)
2348 op1_def = SSA_NAME_DEF_STMT (op1);
2352 /* Now look at the defining statement of OP1 to see if it adds
2353 or subtracts a nonzero constant from another operand. */
2355 && is_gimple_assign (op1_def)
2356 && gimple_assign_rhs_code (op1_def) == PLUS_EXPR
2357 && TREE_CODE (gimple_assign_rhs2 (op1_def)) == INTEGER_CST
2358 && !integer_zerop (gimple_assign_rhs2 (op1_def)))
2360 tree target = gimple_assign_rhs1 (op1_def);
2362 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
2363 for one where TARGET appears on the RHS. */
2364 if (follow_assert_exprs)
2366 /* Now see if that "other operand" is op0, following the chain
2367 of ASSERT_EXPRs if necessary. */
2368 gimple *op0_def = SSA_NAME_DEF_STMT (op0);
2369 while (op0 != target
2370 && gimple_assign_single_p (op0_def)
2371 && TREE_CODE (gimple_assign_rhs1 (op0_def)) == ASSERT_EXPR)
2373 op0 = TREE_OPERAND (gimple_assign_rhs1 (op0_def), 0);
2374 if (TREE_CODE (op0) != SSA_NAME)
2376 op0_def = SSA_NAME_DEF_STMT (op0);
2380 /* If we did not find our target SSA_NAME, then this is not
2381 an overflow test. */
2385 tree type = TREE_TYPE (op0);
2386 wide_int max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
2387 tree inc = gimple_assign_rhs2 (op1_def);
2389 *new_cst = wide_int_to_tree (type, max + wi::to_wide (inc));
2391 *new_cst = wide_int_to_tree (type, max - wi::to_wide (inc));
2398 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
2399 OP1's defining statement to see if it ultimately has the form
2400 OP0 CODE (OP0 PLUS INTEGER_CST)
2402 If so, return TRUE indicating this is an overflow test and store into
2403 *NEW_CST an updated constant that can be used in a narrowed range test.
2405 These statements are left as-is in the IL to facilitate discovery of
2406 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
2407 the alternate range representation is often useful within VRP. */
2410 overflow_comparison_p (tree_code code, tree name, tree val,
2411 bool use_equiv_p, tree *new_cst)
2413 if (overflow_comparison_p_1 (code, name, val, use_equiv_p, false, new_cst))
2415 return overflow_comparison_p_1 (swap_tree_comparison (code), val, name,
2416 use_equiv_p, true, new_cst);
2420 /* Try to register an edge assertion for SSA name NAME on edge E for
2421 the condition COND contributing to the conditional jump pointed to by BSI.
2422 Invert the condition COND if INVERT is true. */
2425 register_edge_assert_for_2 (tree name, edge e,
2426 enum tree_code cond_code,
2427 tree cond_op0, tree cond_op1, bool invert,
2428 vec<assert_info> &asserts)
2431 enum tree_code comp_code;
2433 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
2436 invert, &comp_code, &val))
2439 /* Queue the assert. */
2441 if (overflow_comparison_p (comp_code, name, val, false, &x))
2443 enum tree_code new_code = ((comp_code == GT_EXPR || comp_code == GE_EXPR)
2444 ? GT_EXPR : LE_EXPR);
2445 add_assert_info (asserts, name, name, new_code, x);
2447 add_assert_info (asserts, name, name, comp_code, val);
2449 /* In the case of NAME <= CST and NAME being defined as
2450 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
2451 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
2452 This catches range and anti-range tests. */
2453 if ((comp_code == LE_EXPR
2454 || comp_code == GT_EXPR)
2455 && TREE_CODE (val) == INTEGER_CST
2456 && TYPE_UNSIGNED (TREE_TYPE (val)))
2458 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
2459 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
2461 /* Extract CST2 from the (optional) addition. */
2462 if (is_gimple_assign (def_stmt)
2463 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
2465 name2 = gimple_assign_rhs1 (def_stmt);
2466 cst2 = gimple_assign_rhs2 (def_stmt);
2467 if (TREE_CODE (name2) == SSA_NAME
2468 && TREE_CODE (cst2) == INTEGER_CST)
2469 def_stmt = SSA_NAME_DEF_STMT (name2);
2472 /* Extract NAME2 from the (optional) sign-changing cast. */
2473 if (gimple_assign_cast_p (def_stmt))
2475 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
2476 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
2477 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
2478 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
2479 name3 = gimple_assign_rhs1 (def_stmt);
2482 /* If name3 is used later, create an ASSERT_EXPR for it. */
2483 if (name3 != NULL_TREE
2484 && TREE_CODE (name3) == SSA_NAME
2485 && (cst2 == NULL_TREE
2486 || TREE_CODE (cst2) == INTEGER_CST)
2487 && INTEGRAL_TYPE_P (TREE_TYPE (name3)))
2491 /* Build an expression for the range test. */
2492 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
2493 if (cst2 != NULL_TREE)
2494 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
2495 add_assert_info (asserts, name3, tmp, comp_code, val);
2498 /* If name2 is used later, create an ASSERT_EXPR for it. */
2499 if (name2 != NULL_TREE
2500 && TREE_CODE (name2) == SSA_NAME
2501 && TREE_CODE (cst2) == INTEGER_CST
2502 && INTEGRAL_TYPE_P (TREE_TYPE (name2)))
2506 /* Build an expression for the range test. */
2508 if (TREE_TYPE (name) != TREE_TYPE (name2))
2509 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
2510 if (cst2 != NULL_TREE)
2511 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
2512 add_assert_info (asserts, name2, tmp, comp_code, val);
2516 /* In the case of post-in/decrement tests like if (i++) ... and uses
2517 of the in/decremented value on the edge the extra name we want to
2518 assert for is not on the def chain of the name compared. Instead
2519 it is in the set of use stmts.
2520 Similar cases happen for conversions that were simplified through
2521 fold_{sign_changed,widened}_comparison. */
2522 if ((comp_code == NE_EXPR
2523 || comp_code == EQ_EXPR)
2524 && TREE_CODE (val) == INTEGER_CST)
2526 imm_use_iterator ui;
2528 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
2530 if (!is_gimple_assign (use_stmt))
2533 /* Cut off to use-stmts that are dominating the predecessor. */
2534 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
2537 tree name2 = gimple_assign_lhs (use_stmt);
2538 if (TREE_CODE (name2) != SSA_NAME)
2541 enum tree_code code = gimple_assign_rhs_code (use_stmt);
2543 if (code == PLUS_EXPR
2544 || code == MINUS_EXPR)
2546 cst = gimple_assign_rhs2 (use_stmt);
2547 if (TREE_CODE (cst) != INTEGER_CST)
2549 cst = int_const_binop (code, val, cst);
2551 else if (CONVERT_EXPR_CODE_P (code))
2553 /* For truncating conversions we cannot record
2555 if (comp_code == NE_EXPR
2556 && (TYPE_PRECISION (TREE_TYPE (name2))
2557 < TYPE_PRECISION (TREE_TYPE (name))))
2559 cst = fold_convert (TREE_TYPE (name2), val);
2564 if (TREE_OVERFLOW_P (cst))
2565 cst = drop_tree_overflow (cst);
2566 add_assert_info (asserts, name2, name2, comp_code, cst);
2570 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
2571 && TREE_CODE (val) == INTEGER_CST)
2573 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
2574 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
2575 tree val2 = NULL_TREE;
2576 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
2577 wide_int mask = wi::zero (prec);
2578 unsigned int nprec = prec;
2579 enum tree_code rhs_code = ERROR_MARK;
2581 if (is_gimple_assign (def_stmt))
2582 rhs_code = gimple_assign_rhs_code (def_stmt);
2584 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
2585 assert that A != CST1 -+ CST2. */
2586 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
2587 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
2589 tree op0 = gimple_assign_rhs1 (def_stmt);
2590 tree op1 = gimple_assign_rhs2 (def_stmt);
2591 if (TREE_CODE (op0) == SSA_NAME
2592 && TREE_CODE (op1) == INTEGER_CST)
2594 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
2595 ? MINUS_EXPR : PLUS_EXPR);
2596 op1 = int_const_binop (reverse_op, val, op1);
2597 if (TREE_OVERFLOW (op1))
2598 op1 = drop_tree_overflow (op1);
2599 add_assert_info (asserts, op0, op0, comp_code, op1);
2603 /* Add asserts for NAME cmp CST and NAME being defined
2604 as NAME = (int) NAME2. */
2605 if (!TYPE_UNSIGNED (TREE_TYPE (val))
2606 && (comp_code == LE_EXPR || comp_code == LT_EXPR
2607 || comp_code == GT_EXPR || comp_code == GE_EXPR)
2608 && gimple_assign_cast_p (def_stmt))
2610 name2 = gimple_assign_rhs1 (def_stmt);
2611 if (CONVERT_EXPR_CODE_P (rhs_code)
2612 && TREE_CODE (name2) == SSA_NAME
2613 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
2614 && TYPE_UNSIGNED (TREE_TYPE (name2))
2615 && prec == TYPE_PRECISION (TREE_TYPE (name2))
2616 && (comp_code == LE_EXPR || comp_code == GT_EXPR
2617 || !tree_int_cst_equal (val,
2618 TYPE_MIN_VALUE (TREE_TYPE (val)))))
2621 enum tree_code new_comp_code = comp_code;
2623 cst = fold_convert (TREE_TYPE (name2),
2624 TYPE_MIN_VALUE (TREE_TYPE (val)));
2625 /* Build an expression for the range test. */
2626 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
2627 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
2628 fold_convert (TREE_TYPE (name2), val));
2629 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
2631 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
2632 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
2633 build_int_cst (TREE_TYPE (name2), 1));
2635 add_assert_info (asserts, name2, tmp, new_comp_code, cst);
2639 /* Add asserts for NAME cmp CST and NAME being defined as
2640 NAME = NAME2 >> CST2.
2642 Extract CST2 from the right shift. */
2643 if (rhs_code == RSHIFT_EXPR)
2645 name2 = gimple_assign_rhs1 (def_stmt);
2646 cst2 = gimple_assign_rhs2 (def_stmt);
2647 if (TREE_CODE (name2) == SSA_NAME
2648 && tree_fits_uhwi_p (cst2)
2649 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
2650 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
2651 && type_has_mode_precision_p (TREE_TYPE (val)))
2653 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
2654 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
2657 if (val2 != NULL_TREE
2658 && TREE_CODE (val2) == INTEGER_CST
2659 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
2663 enum tree_code new_comp_code = comp_code;
2667 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
2669 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
2671 tree type = build_nonstandard_integer_type (prec, 1);
2672 tmp = build1 (NOP_EXPR, type, name2);
2673 val2 = fold_convert (type, val2);
2675 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
2676 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
2677 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
2679 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
2682 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
2684 if (minval == wi::to_wide (new_val))
2685 new_val = NULL_TREE;
2690 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
2691 mask |= wi::to_wide (val2);
2692 if (wi::eq_p (mask, maxval))
2693 new_val = NULL_TREE;
2695 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
2699 add_assert_info (asserts, name2, tmp, new_comp_code, new_val);
2702 /* If we have a conversion that doesn't change the value of the source
2703 simply register the same assert for it. */
2704 if (CONVERT_EXPR_CODE_P (rhs_code))
2706 wide_int rmin, rmax;
2707 tree rhs1 = gimple_assign_rhs1 (def_stmt);
2708 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2709 && TREE_CODE (rhs1) == SSA_NAME
2710 /* Make sure the relation preserves the upper/lower boundary of
2711 the range conservatively. */
2712 && (comp_code == NE_EXPR
2713 || comp_code == EQ_EXPR
2714 || (TYPE_SIGN (TREE_TYPE (name))
2715 == TYPE_SIGN (TREE_TYPE (rhs1)))
2716 || ((comp_code == LE_EXPR
2717 || comp_code == LT_EXPR)
2718 && !TYPE_UNSIGNED (TREE_TYPE (rhs1)))
2719 || ((comp_code == GE_EXPR
2720 || comp_code == GT_EXPR)
2721 && TYPE_UNSIGNED (TREE_TYPE (rhs1))))
2722 /* And the conversion does not alter the value we compare
2723 against and all values in rhs1 can be represented in
2724 the converted to type. */
2725 && int_fits_type_p (val, TREE_TYPE (rhs1))
2726 && ((TYPE_PRECISION (TREE_TYPE (name))
2727 > TYPE_PRECISION (TREE_TYPE (rhs1)))
2728 || (get_range_info (rhs1, &rmin, &rmax) == VR_RANGE
2729 && wi::fits_to_tree_p (rmin, TREE_TYPE (name))
2730 && wi::fits_to_tree_p (rmax, TREE_TYPE (name)))))
2731 add_assert_info (asserts, rhs1, rhs1,
2732 comp_code, fold_convert (TREE_TYPE (rhs1), val));
2735 /* Add asserts for NAME cmp CST and NAME being defined as
2736 NAME = NAME2 & CST2.
2738 Extract CST2 from the and.
2741 NAME = (unsigned) NAME2;
2742 casts where NAME's type is unsigned and has smaller precision
2743 than NAME2's type as if it was NAME = NAME2 & MASK. */
2744 names[0] = NULL_TREE;
2745 names[1] = NULL_TREE;
2747 if (rhs_code == BIT_AND_EXPR
2748 || (CONVERT_EXPR_CODE_P (rhs_code)
2749 && INTEGRAL_TYPE_P (TREE_TYPE (val))
2750 && TYPE_UNSIGNED (TREE_TYPE (val))
2751 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
2754 name2 = gimple_assign_rhs1 (def_stmt);
2755 if (rhs_code == BIT_AND_EXPR)
2756 cst2 = gimple_assign_rhs2 (def_stmt);
2759 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
2760 nprec = TYPE_PRECISION (TREE_TYPE (name2));
2762 if (TREE_CODE (name2) == SSA_NAME
2763 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
2764 && TREE_CODE (cst2) == INTEGER_CST
2765 && !integer_zerop (cst2)
2767 || TYPE_UNSIGNED (TREE_TYPE (val))))
2769 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
2770 if (gimple_assign_cast_p (def_stmt2))
2772 names[1] = gimple_assign_rhs1 (def_stmt2);
2773 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
2774 || TREE_CODE (names[1]) != SSA_NAME
2775 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
2776 || (TYPE_PRECISION (TREE_TYPE (name2))
2777 != TYPE_PRECISION (TREE_TYPE (names[1]))))
2778 names[1] = NULL_TREE;
2783 if (names[0] || names[1])
2785 wide_int minv, maxv, valv, cst2v;
2786 wide_int tem, sgnbit;
2787 bool valid_p = false, valn, cst2n;
2788 enum tree_code ccode = comp_code;
2790 valv = wide_int::from (wi::to_wide (val), nprec, UNSIGNED);
2791 cst2v = wide_int::from (wi::to_wide (cst2), nprec, UNSIGNED);
2792 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
2793 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
2794 /* If CST2 doesn't have most significant bit set,
2795 but VAL is negative, we have comparison like
2796 if ((x & 0x123) > -4) (always true). Just give up. */
2800 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
2802 sgnbit = wi::zero (nprec);
2803 minv = valv & cst2v;
2807 /* Minimum unsigned value for equality is VAL & CST2
2808 (should be equal to VAL, otherwise we probably should
2809 have folded the comparison into false) and
2810 maximum unsigned value is VAL | ~CST2. */
2811 maxv = valv | ~cst2v;
2816 tem = valv | ~cst2v;
2817 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
2821 sgnbit = wi::zero (nprec);
2824 /* If (VAL | ~CST2) is all ones, handle it as
2825 (X & CST2) < VAL. */
2830 sgnbit = wi::zero (nprec);
2833 if (!cst2n && wi::neg_p (cst2v))
2834 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
2843 if (tem == wi::mask (nprec - 1, false, nprec))
2849 sgnbit = wi::zero (nprec);
2854 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
2855 is VAL and maximum unsigned value is ~0. For signed
2856 comparison, if CST2 doesn't have most significant bit
2857 set, handle it similarly. If CST2 has MSB set,
2858 the minimum is the same, and maximum is ~0U/2. */
2861 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
2863 minv = masked_increment (valv, cst2v, sgnbit, nprec);
2867 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
2873 /* Find out smallest MINV where MINV > VAL
2874 && (MINV & CST2) == MINV, if any. If VAL is signed and
2875 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
2876 minv = masked_increment (valv, cst2v, sgnbit, nprec);
2879 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
2884 /* Minimum unsigned value for <= is 0 and maximum
2885 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
2886 Otherwise, find smallest VAL2 where VAL2 > VAL
2887 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2889 For signed comparison, if CST2 doesn't have most
2890 significant bit set, handle it similarly. If CST2 has
2891 MSB set, the maximum is the same and minimum is INT_MIN. */
2896 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
2908 /* Minimum unsigned value for < is 0 and maximum
2909 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
2910 Otherwise, find smallest VAL2 where VAL2 > VAL
2911 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2913 For signed comparison, if CST2 doesn't have most
2914 significant bit set, handle it similarly. If CST2 has
2915 MSB set, the maximum is the same and minimum is INT_MIN. */
2924 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
2938 && (maxv - minv) != -1)
2940 tree tmp, new_val, type;
2943 for (i = 0; i < 2; i++)
2946 wide_int maxv2 = maxv;
2948 type = TREE_TYPE (names[i]);
2949 if (!TYPE_UNSIGNED (type))
2951 type = build_nonstandard_integer_type (nprec, 1);
2952 tmp = build1 (NOP_EXPR, type, names[i]);
2956 tmp = build2 (PLUS_EXPR, type, tmp,
2957 wide_int_to_tree (type, -minv));
2958 maxv2 = maxv - minv;
2960 new_val = wide_int_to_tree (type, maxv2);
2961 add_assert_info (asserts, names[i], tmp, LE_EXPR, new_val);
2968 /* OP is an operand of a truth value expression which is known to have
2969 a particular value. Register any asserts for OP and for any
2970 operands in OP's defining statement.
2972 If CODE is EQ_EXPR, then we want to register OP is zero (false),
2973 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
2976 register_edge_assert_for_1 (tree op, enum tree_code code,
2977 edge e, vec<assert_info> &asserts)
2981 enum tree_code rhs_code;
2983 /* We only care about SSA_NAMEs. */
2984 if (TREE_CODE (op) != SSA_NAME)
2987 /* We know that OP will have a zero or nonzero value. */
2988 val = build_int_cst (TREE_TYPE (op), 0);
2989 add_assert_info (asserts, op, op, code, val);
2991 /* Now look at how OP is set. If it's set from a comparison,
2992 a truth operation or some bit operations, then we may be able
2993 to register information about the operands of that assignment. */
2994 op_def = SSA_NAME_DEF_STMT (op);
2995 if (gimple_code (op_def) != GIMPLE_ASSIGN)
2998 rhs_code = gimple_assign_rhs_code (op_def);
3000 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
3002 bool invert = (code == EQ_EXPR ? true : false);
3003 tree op0 = gimple_assign_rhs1 (op_def);
3004 tree op1 = gimple_assign_rhs2 (op_def);
3006 if (TREE_CODE (op0) == SSA_NAME)
3007 register_edge_assert_for_2 (op0, e, rhs_code, op0, op1, invert, asserts);
3008 if (TREE_CODE (op1) == SSA_NAME)
3009 register_edge_assert_for_2 (op1, e, rhs_code, op0, op1, invert, asserts);
3011 else if ((code == NE_EXPR
3012 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
3014 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
3016 /* Recurse on each operand. */
3017 tree op0 = gimple_assign_rhs1 (op_def);
3018 tree op1 = gimple_assign_rhs2 (op_def);
3019 if (TREE_CODE (op0) == SSA_NAME
3020 && has_single_use (op0))
3021 register_edge_assert_for_1 (op0, code, e, asserts);
3022 if (TREE_CODE (op1) == SSA_NAME
3023 && has_single_use (op1))
3024 register_edge_assert_for_1 (op1, code, e, asserts);
3026 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
3027 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
3029 /* Recurse, flipping CODE. */
3030 code = invert_tree_comparison (code, false);
3031 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
3033 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
3035 /* Recurse through the copy. */
3036 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
3038 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
3040 /* Recurse through the type conversion, unless it is a narrowing
3041 conversion or conversion from non-integral type. */
3042 tree rhs = gimple_assign_rhs1 (op_def);
3043 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
3044 && (TYPE_PRECISION (TREE_TYPE (rhs))
3045 <= TYPE_PRECISION (TREE_TYPE (op))))
3046 register_edge_assert_for_1 (rhs, code, e, asserts);
3050 /* Check if comparison
3051 NAME COND_OP INTEGER_CST
3053 (X & 11...100..0) COND_OP XX...X00...0
3054 Such comparison can yield assertions like
3057 in case of COND_OP being EQ_EXPR or
3060 in case of NE_EXPR. */
3063 is_masked_range_test (tree name, tree valt, enum tree_code cond_code,
3064 tree *new_name, tree *low, enum tree_code *low_code,
3065 tree *high, enum tree_code *high_code)
3067 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
3069 if (!is_gimple_assign (def_stmt)
3070 || gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
3073 tree t = gimple_assign_rhs1 (def_stmt);
3074 tree maskt = gimple_assign_rhs2 (def_stmt);
3075 if (TREE_CODE (t) != SSA_NAME || TREE_CODE (maskt) != INTEGER_CST)
3078 wi::tree_to_wide_ref mask = wi::to_wide (maskt);
3079 wide_int inv_mask = ~mask;
3080 /* Must have been removed by now so don't bother optimizing. */
3081 if (mask == 0 || inv_mask == 0)
3084 /* Assume VALT is INTEGER_CST. */
3085 wi::tree_to_wide_ref val = wi::to_wide (valt);
3087 if ((inv_mask & (inv_mask + 1)) != 0
3088 || (val & mask) != val)
3091 bool is_range = cond_code == EQ_EXPR;
3093 tree type = TREE_TYPE (t);
3094 wide_int min = wi::min_value (type),
3095 max = wi::max_value (type);
3099 *low_code = val == min ? ERROR_MARK : GE_EXPR;
3100 *high_code = val == max ? ERROR_MARK : LE_EXPR;
3104 /* We can still generate assertion if one of alternatives
3105 is known to always be false. */
3108 *low_code = (enum tree_code) 0;
3109 *high_code = GT_EXPR;
3111 else if ((val | inv_mask) == max)
3113 *low_code = LT_EXPR;
3114 *high_code = (enum tree_code) 0;
3121 *low = wide_int_to_tree (type, val);
3122 *high = wide_int_to_tree (type, val | inv_mask);
3127 /* Try to register an edge assertion for SSA name NAME on edge E for
3128 the condition COND contributing to the conditional jump pointed to by
3132 register_edge_assert_for (tree name, edge e,
3133 enum tree_code cond_code, tree cond_op0,
3134 tree cond_op1, vec<assert_info> &asserts)
3137 enum tree_code comp_code;
3138 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
3140 /* Do not attempt to infer anything in names that flow through
3142 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
3145 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
3151 /* Register ASSERT_EXPRs for name. */
3152 register_edge_assert_for_2 (name, e, cond_code, cond_op0,
3153 cond_op1, is_else_edge, asserts);
3156 /* If COND is effectively an equality test of an SSA_NAME against
3157 the value zero or one, then we may be able to assert values
3158 for SSA_NAMEs which flow into COND. */
3160 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
3161 statement of NAME we can assert both operands of the BIT_AND_EXPR
3162 have nonzero value. */
3163 if (((comp_code == EQ_EXPR && integer_onep (val))
3164 || (comp_code == NE_EXPR && integer_zerop (val))))
3166 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
3168 if (is_gimple_assign (def_stmt)
3169 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
3171 tree op0 = gimple_assign_rhs1 (def_stmt);
3172 tree op1 = gimple_assign_rhs2 (def_stmt);
3173 register_edge_assert_for_1 (op0, NE_EXPR, e, asserts);
3174 register_edge_assert_for_1 (op1, NE_EXPR, e, asserts);
3178 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
3179 statement of NAME we can assert both operands of the BIT_IOR_EXPR
3181 if (((comp_code == EQ_EXPR && integer_zerop (val))
3182 || (comp_code == NE_EXPR && integer_onep (val))))
3184 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
3186 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
3187 necessarily zero value, or if type-precision is one. */
3188 if (is_gimple_assign (def_stmt)
3189 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
3190 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
3191 || comp_code == EQ_EXPR)))
3193 tree op0 = gimple_assign_rhs1 (def_stmt);
3194 tree op1 = gimple_assign_rhs2 (def_stmt);
3195 register_edge_assert_for_1 (op0, EQ_EXPR, e, asserts);
3196 register_edge_assert_for_1 (op1, EQ_EXPR, e, asserts);
3200 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
3201 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
3202 && TREE_CODE (val) == INTEGER_CST)
3204 enum tree_code low_code, high_code;
3206 if (is_masked_range_test (name, val, comp_code, &name, &low,
3207 &low_code, &high, &high_code))
3209 if (low_code != ERROR_MARK)
3210 register_edge_assert_for_2 (name, e, low_code, name,
3211 low, /*invert*/false, asserts);
3212 if (high_code != ERROR_MARK)
3213 register_edge_assert_for_2 (name, e, high_code, name,
3214 high, /*invert*/false, asserts);
3219 /* Finish found ASSERTS for E and register them at GSI. */
3222 finish_register_edge_assert_for (edge e, gimple_stmt_iterator gsi,
3223 vec<assert_info> &asserts)
3225 for (unsigned i = 0; i < asserts.length (); ++i)
3226 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
3227 reachable from E. */
3228 if (live_on_edge (e, asserts[i].name))
3229 register_new_assert_for (asserts[i].name, asserts[i].expr,
3230 asserts[i].comp_code, asserts[i].val,
3236 /* Determine whether the outgoing edges of BB should receive an
3237 ASSERT_EXPR for each of the operands of BB's LAST statement.
3238 The last statement of BB must be a COND_EXPR.
3240 If any of the sub-graphs rooted at BB have an interesting use of
3241 the predicate operands, an assert location node is added to the
3242 list of assertions for the corresponding operands. */
3245 find_conditional_asserts (basic_block bb, gcond *last)
3247 gimple_stmt_iterator bsi;
3253 bsi = gsi_for_stmt (last);
3255 /* Look for uses of the operands in each of the sub-graphs
3256 rooted at BB. We need to check each of the outgoing edges
3257 separately, so that we know what kind of ASSERT_EXPR to
3259 FOR_EACH_EDGE (e, ei, bb->succs)
3264 /* Register the necessary assertions for each operand in the
3265 conditional predicate. */
3266 auto_vec<assert_info, 8> asserts;
3267 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3268 register_edge_assert_for (op, e,
3269 gimple_cond_code (last),
3270 gimple_cond_lhs (last),
3271 gimple_cond_rhs (last), asserts);
3272 finish_register_edge_assert_for (e, bsi, asserts);
3282 /* Compare two case labels sorting first by the destination bb index
3283 and then by the case value. */
3286 compare_case_labels (const void *p1, const void *p2)
3288 const struct case_info *ci1 = (const struct case_info *) p1;
3289 const struct case_info *ci2 = (const struct case_info *) p2;
3290 int idx1 = ci1->bb->index;
3291 int idx2 = ci2->bb->index;
3295 else if (idx1 == idx2)
3297 /* Make sure the default label is first in a group. */
3298 if (!CASE_LOW (ci1->expr))
3300 else if (!CASE_LOW (ci2->expr))
3303 return tree_int_cst_compare (CASE_LOW (ci1->expr),
3304 CASE_LOW (ci2->expr));
3310 /* Determine whether the outgoing edges of BB should receive an
3311 ASSERT_EXPR for each of the operands of BB's LAST statement.
3312 The last statement of BB must be a SWITCH_EXPR.
3314 If any of the sub-graphs rooted at BB have an interesting use of
3315 the predicate operands, an assert location node is added to the
3316 list of assertions for the corresponding operands. */
3319 find_switch_asserts (basic_block bb, gswitch *last)
3321 gimple_stmt_iterator bsi;
3324 struct case_info *ci;
3325 size_t n = gimple_switch_num_labels (last);
3326 #if GCC_VERSION >= 4000
3329 /* Work around GCC 3.4 bug (PR 37086). */
3330 volatile unsigned int idx;
3333 bsi = gsi_for_stmt (last);
3334 op = gimple_switch_index (last);
3335 if (TREE_CODE (op) != SSA_NAME)
3338 /* Build a vector of case labels sorted by destination label. */
3339 ci = XNEWVEC (struct case_info, n);
3340 for (idx = 0; idx < n; ++idx)
3342 ci[idx].expr = gimple_switch_label (last, idx);
3343 ci[idx].bb = label_to_block (cfun, CASE_LABEL (ci[idx].expr));
3345 edge default_edge = find_edge (bb, ci[0].bb);
3346 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
3348 for (idx = 0; idx < n; ++idx)
3351 tree cl = ci[idx].expr;
3352 basic_block cbb = ci[idx].bb;
3354 min = CASE_LOW (cl);
3355 max = CASE_HIGH (cl);
3357 /* If there are multiple case labels with the same destination
3358 we need to combine them to a single value range for the edge. */
3359 if (idx + 1 < n && cbb == ci[idx + 1].bb)
3361 /* Skip labels until the last of the group. */
3364 } while (idx < n && cbb == ci[idx].bb);
3367 /* Pick up the maximum of the case label range. */
3368 if (CASE_HIGH (ci[idx].expr))
3369 max = CASE_HIGH (ci[idx].expr);
3371 max = CASE_LOW (ci[idx].expr);
3374 /* Can't extract a useful assertion out of a range that includes the
3376 if (min == NULL_TREE)
3379 /* Find the edge to register the assert expr on. */
3380 e = find_edge (bb, cbb);
3382 /* Register the necessary assertions for the operand in the
3384 auto_vec<assert_info, 8> asserts;
3385 register_edge_assert_for (op, e,
3386 max ? GE_EXPR : EQ_EXPR,
3387 op, fold_convert (TREE_TYPE (op), min),
3390 register_edge_assert_for (op, e, LE_EXPR, op,
3391 fold_convert (TREE_TYPE (op), max),
3393 finish_register_edge_assert_for (e, bsi, asserts);
3398 if (!live_on_edge (default_edge, op))
3401 /* Now register along the default label assertions that correspond to the
3402 anti-range of each label. */
3403 int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS);
3404 if (insertion_limit == 0)
3407 /* We can't do this if the default case shares a label with another case. */
3408 tree default_cl = gimple_switch_default_label (last);
3409 for (idx = 1; idx < n; idx++)
3412 tree cl = gimple_switch_label (last, idx);
3413 if (CASE_LABEL (cl) == CASE_LABEL (default_cl))
3416 min = CASE_LOW (cl);
3417 max = CASE_HIGH (cl);
3419 /* Combine contiguous case ranges to reduce the number of assertions
3421 for (idx = idx + 1; idx < n; idx++)
3423 tree next_min, next_max;
3424 tree next_cl = gimple_switch_label (last, idx);
3425 if (CASE_LABEL (next_cl) == CASE_LABEL (default_cl))
3428 next_min = CASE_LOW (next_cl);
3429 next_max = CASE_HIGH (next_cl);
3431 wide_int difference = (wi::to_wide (next_min)
3432 - wi::to_wide (max ? max : min));
3433 if (wi::eq_p (difference, 1))
3434 max = next_max ? next_max : next_min;
3440 if (max == NULL_TREE)
3442 /* Register the assertion OP != MIN. */
3443 auto_vec<assert_info, 8> asserts;
3444 min = fold_convert (TREE_TYPE (op), min);
3445 register_edge_assert_for (op, default_edge, NE_EXPR, op, min,
3447 finish_register_edge_assert_for (default_edge, bsi, asserts);
3451 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
3452 which will give OP the anti-range ~[MIN,MAX]. */
3453 tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op);
3454 min = fold_convert (TREE_TYPE (uop), min);
3455 max = fold_convert (TREE_TYPE (uop), max);
3457 tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min);
3458 tree rhs = int_const_binop (MINUS_EXPR, max, min);
3459 register_new_assert_for (op, lhs, GT_EXPR, rhs,
3460 NULL, default_edge, bsi);
3463 if (--insertion_limit == 0)
3469 /* Traverse all the statements in block BB looking for statements that
3470 may generate useful assertions for the SSA names in their operand.
3471 If a statement produces a useful assertion A for name N_i, then the
3472 list of assertions already generated for N_i is scanned to
3473 determine if A is actually needed.
3475 If N_i already had the assertion A at a location dominating the
3476 current location, then nothing needs to be done. Otherwise, the
3477 new location for A is recorded instead.
3479 1- For every statement S in BB, all the variables used by S are
3480 added to bitmap FOUND_IN_SUBGRAPH.
3482 2- If statement S uses an operand N in a way that exposes a known
3483 value range for N, then if N was not already generated by an
3484 ASSERT_EXPR, create a new assert location for N. For instance,
3485 if N is a pointer and the statement dereferences it, we can
3486 assume that N is not NULL.
3488 3- COND_EXPRs are a special case of #2. We can derive range
3489 information from the predicate but need to insert different
3490 ASSERT_EXPRs for each of the sub-graphs rooted at the
3491 conditional block. If the last statement of BB is a conditional
3492 expression of the form 'X op Y', then
3494 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
3496 b) If the conditional is the only entry point to the sub-graph
3497 corresponding to the THEN_CLAUSE, recurse into it. On
3498 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
3499 an ASSERT_EXPR is added for the corresponding variable.
3501 c) Repeat step (b) on the ELSE_CLAUSE.
3503 d) Mark X and Y in FOUND_IN_SUBGRAPH.
3512 In this case, an assertion on the THEN clause is useful to
3513 determine that 'a' is always 9 on that edge. However, an assertion
3514 on the ELSE clause would be unnecessary.
3516 4- If BB does not end in a conditional expression, then we recurse
3517 into BB's dominator children.
3519 At the end of the recursive traversal, every SSA name will have a
3520 list of locations where ASSERT_EXPRs should be added. When a new
3521 location for name N is found, it is registered by calling
3522 register_new_assert_for. That function keeps track of all the
3523 registered assertions to prevent adding unnecessary assertions.
3524 For instance, if a pointer P_4 is dereferenced more than once in a
3525 dominator tree, only the location dominating all the dereference of
3526 P_4 will receive an ASSERT_EXPR. */
3529 find_assert_locations_1 (basic_block bb, sbitmap live)
3533 last = last_stmt (bb);
3535 /* If BB's last statement is a conditional statement involving integer
3536 operands, determine if we need to add ASSERT_EXPRs. */
3538 && gimple_code (last) == GIMPLE_COND
3539 && !fp_predicate (last)
3540 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
3541 find_conditional_asserts (bb, as_a <gcond *> (last));
3543 /* If BB's last statement is a switch statement involving integer
3544 operands, determine if we need to add ASSERT_EXPRs. */
3546 && gimple_code (last) == GIMPLE_SWITCH
3547 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
3548 find_switch_asserts (bb, as_a <gswitch *> (last));
3550 /* Traverse all the statements in BB marking used names and looking
3551 for statements that may infer assertions for their used operands. */
3552 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
3559 stmt = gsi_stmt (si);
3561 if (is_gimple_debug (stmt))
3564 /* See if we can derive an assertion for any of STMT's operands. */
3565 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
3568 enum tree_code comp_code;
3570 /* If op is not live beyond this stmt, do not bother to insert
3572 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
3575 /* If OP is used in such a way that we can infer a value
3576 range for it, and we don't find a previous assertion for
3577 it, create a new assertion location node for OP. */
3578 if (infer_value_range (stmt, op, &comp_code, &value))
3580 /* If we are able to infer a nonzero value range for OP,
3581 then walk backwards through the use-def chain to see if OP
3582 was set via a typecast.
3584 If so, then we can also infer a nonzero value range
3585 for the operand of the NOP_EXPR. */
3586 if (comp_code == NE_EXPR && integer_zerop (value))
3589 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
3591 while (is_gimple_assign (def_stmt)
3592 && CONVERT_EXPR_CODE_P
3593 (gimple_assign_rhs_code (def_stmt))
3595 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
3597 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
3599 t = gimple_assign_rhs1 (def_stmt);
3600 def_stmt = SSA_NAME_DEF_STMT (t);
3602 /* Note we want to register the assert for the
3603 operand of the NOP_EXPR after SI, not after the
3605 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
3606 register_new_assert_for (t, t, comp_code, value,
3611 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
3616 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
3617 bitmap_set_bit (live, SSA_NAME_VERSION (op));
3618 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
3619 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
3622 /* Traverse all PHI nodes in BB, updating live. */
3623 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
3626 use_operand_p arg_p;
3628 gphi *phi = si.phi ();
3629 tree res = gimple_phi_result (phi);
3631 if (virtual_operand_p (res))
3634 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
3636 tree arg = USE_FROM_PTR (arg_p);
3637 if (TREE_CODE (arg) == SSA_NAME)
3638 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
3641 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
3645 /* Do an RPO walk over the function computing SSA name liveness
3646 on-the-fly and deciding on assert expressions to insert. */
3649 find_assert_locations (void)
3651 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
3652 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
3653 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
3656 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
3657 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
3658 for (i = 0; i < rpo_cnt; ++i)
3661 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
3662 the order we compute liveness and insert asserts we otherwise
3663 fail to insert asserts into the loop latch. */
3665 FOR_EACH_LOOP (loop, 0)
3667 i = loop->latch->index;
3668 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
3669 for (gphi_iterator gsi = gsi_start_phis (loop->header);
3670 !gsi_end_p (gsi); gsi_next (&gsi))
3672 gphi *phi = gsi.phi ();
3673 if (virtual_operand_p (gimple_phi_result (phi)))
3675 tree arg = gimple_phi_arg_def (phi, j);
3676 if (TREE_CODE (arg) == SSA_NAME)
3678 if (live[i] == NULL)
3680 live[i] = sbitmap_alloc (num_ssa_names);
3681 bitmap_clear (live[i]);
3683 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
3688 for (i = rpo_cnt - 1; i >= 0; --i)
3690 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
3696 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
3697 bitmap_clear (live[rpo[i]]);
3700 /* Process BB and update the live information with uses in
3702 find_assert_locations_1 (bb, live[rpo[i]]);
3704 /* Merge liveness into the predecessor blocks and free it. */
3705 if (!bitmap_empty_p (live[rpo[i]]))
3708 FOR_EACH_EDGE (e, ei, bb->preds)
3710 int pred = e->src->index;
3711 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
3716 live[pred] = sbitmap_alloc (num_ssa_names);
3717 bitmap_clear (live[pred]);
3719 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
3721 if (bb_rpo[pred] < pred_rpo)
3722 pred_rpo = bb_rpo[pred];
3725 /* Record the RPO number of the last visited block that needs
3726 live information from this block. */
3727 last_rpo[rpo[i]] = pred_rpo;
3731 sbitmap_free (live[rpo[i]]);
3732 live[rpo[i]] = NULL;
3735 /* We can free all successors live bitmaps if all their
3736 predecessors have been visited already. */
3737 FOR_EACH_EDGE (e, ei, bb->succs)
3738 if (last_rpo[e->dest->index] == i
3739 && live[e->dest->index])
3741 sbitmap_free (live[e->dest->index]);
3742 live[e->dest->index] = NULL;
3747 XDELETEVEC (bb_rpo);
3748 XDELETEVEC (last_rpo);
3749 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
3751 sbitmap_free (live[i]);
3755 /* Create an ASSERT_EXPR for NAME and insert it in the location
3756 indicated by LOC. Return true if we made any edge insertions. */
3759 process_assert_insertions_for (tree name, assert_locus *loc)
3761 /* Build the comparison expression NAME_i COMP_CODE VAL. */
3764 gimple *assert_stmt;
3768 /* If we have X <=> X do not insert an assert expr for that. */
3769 if (loc->expr == loc->val)
3772 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
3773 assert_stmt = build_assert_expr_for (cond, name);
3776 /* We have been asked to insert the assertion on an edge. This
3777 is used only by COND_EXPR and SWITCH_EXPR assertions. */
3778 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
3779 || (gimple_code (gsi_stmt (loc->si))
3782 gsi_insert_on_edge (loc->e, assert_stmt);
3786 /* If the stmt iterator points at the end then this is an insertion
3787 at the beginning of a block. */
3788 if (gsi_end_p (loc->si))
3790 gimple_stmt_iterator si = gsi_after_labels (loc->bb);
3791 gsi_insert_before (&si, assert_stmt, GSI_SAME_STMT);
3795 /* Otherwise, we can insert right after LOC->SI iff the
3796 statement must not be the last statement in the block. */
3797 stmt = gsi_stmt (loc->si);
3798 if (!stmt_ends_bb_p (stmt))
3800 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
3804 /* If STMT must be the last statement in BB, we can only insert new
3805 assertions on the non-abnormal edge out of BB. Note that since
3806 STMT is not control flow, there may only be one non-abnormal/eh edge
3808 FOR_EACH_EDGE (e, ei, loc->bb->succs)
3809 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
3811 gsi_insert_on_edge (e, assert_stmt);
3818 /* Qsort helper for sorting assert locations. If stable is true, don't
3819 use iterative_hash_expr because it can be unstable for -fcompare-debug,
3820 on the other side some pointers might be NULL. */
3822 template <bool stable>
3824 compare_assert_loc (const void *pa, const void *pb)
3826 assert_locus * const a = *(assert_locus * const *)pa;
3827 assert_locus * const b = *(assert_locus * const *)pb;
3829 /* If stable, some asserts might be optimized away already, sort
3839 if (a->e == NULL && b->e != NULL)
3841 else if (a->e != NULL && b->e == NULL)
3844 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
3845 no need to test both a->e and b->e. */
3847 /* Sort after destination index. */
3850 else if (a->e->dest->index > b->e->dest->index)
3852 else if (a->e->dest->index < b->e->dest->index)
3855 /* Sort after comp_code. */
3856 if (a->comp_code > b->comp_code)
3858 else if (a->comp_code < b->comp_code)
3863 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
3864 uses DECL_UID of the VAR_DECL, so sorting might differ between
3865 -g and -g0. When doing the removal of redundant assert exprs
3866 and commonization to successors, this does not matter, but for
3867 the final sort needs to be stable. */
3875 ha = iterative_hash_expr (a->expr, iterative_hash_expr (a->val, 0));
3876 hb = iterative_hash_expr (b->expr, iterative_hash_expr (b->val, 0));
3879 /* Break the tie using hashing and source/bb index. */
3881 return (a->e != NULL
3882 ? a->e->src->index - b->e->src->index
3883 : a->bb->index - b->bb->index);
3884 return ha > hb ? 1 : -1;
3887 /* Process all the insertions registered for every name N_i registered
3888 in NEED_ASSERT_FOR. The list of assertions to be inserted are
3889 found in ASSERTS_FOR[i]. */
3892 process_assert_insertions (void)
3896 bool update_edges_p = false;
3897 int num_asserts = 0;
3899 if (dump_file && (dump_flags & TDF_DETAILS))
3900 dump_all_asserts (dump_file);
3902 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
3904 assert_locus *loc = asserts_for[i];
3907 auto_vec<assert_locus *, 16> asserts;
3908 for (; loc; loc = loc->next)
3909 asserts.safe_push (loc);
3910 asserts.qsort (compare_assert_loc<false>);
3912 /* Push down common asserts to successors and remove redundant ones. */
3914 assert_locus *common = NULL;
3915 unsigned commonj = 0;
3916 for (unsigned j = 0; j < asserts.length (); ++j)
3922 || loc->e->dest != common->e->dest
3923 || loc->comp_code != common->comp_code
3924 || ! operand_equal_p (loc->val, common->val, 0)
3925 || ! operand_equal_p (loc->expr, common->expr, 0))
3931 else if (loc->e == asserts[j-1]->e)
3933 /* Remove duplicate asserts. */
3934 if (commonj == j - 1)
3939 free (asserts[j-1]);
3940 asserts[j-1] = NULL;
3945 if (EDGE_COUNT (common->e->dest->preds) == ecnt)
3947 /* We have the same assertion on all incoming edges of a BB.
3948 Insert it at the beginning of that block. */
3949 loc->bb = loc->e->dest;
3951 loc->si = gsi_none ();
3953 /* Clear asserts commoned. */
3954 for (; commonj != j; ++commonj)
3955 if (asserts[commonj])
3957 free (asserts[commonj]);
3958 asserts[commonj] = NULL;
3964 /* The asserts vector sorting above might be unstable for
3965 -fcompare-debug, sort again to ensure a stable sort. */
3966 asserts.qsort (compare_assert_loc<true>);
3967 for (unsigned j = 0; j < asserts.length (); ++j)
3972 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
3979 gsi_commit_edge_inserts ();
3981 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
3986 /* Traverse the flowgraph looking for conditional jumps to insert range
3987 expressions. These range expressions are meant to provide information
3988 to optimizations that need to reason in terms of value ranges. They
3989 will not be expanded into RTL. For instance, given:
3998 this pass will transform the code into:
4004 x = ASSERT_EXPR <x, x < y>
4009 y = ASSERT_EXPR <y, x >= y>
4013 The idea is that once copy and constant propagation have run, other
4014 optimizations will be able to determine what ranges of values can 'x'
4015 take in different paths of the code, simply by checking the reaching
4016 definition of 'x'. */
4019 insert_range_assertions (void)
4021 need_assert_for = BITMAP_ALLOC (NULL);
4022 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
4024 calculate_dominance_info (CDI_DOMINATORS);
4026 find_assert_locations ();
4027 if (!bitmap_empty_p (need_assert_for))
4029 process_assert_insertions ();
4030 update_ssa (TODO_update_ssa_no_phi);
4033 if (dump_file && (dump_flags & TDF_DETAILS))
4035 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
4036 dump_function_to_file (current_function_decl, dump_file, dump_flags);
4040 BITMAP_FREE (need_assert_for);
4043 class vrp_prop : public ssa_propagation_engine
4046 enum ssa_prop_result visit_stmt (gimple *, edge *, tree *) FINAL OVERRIDE;
4047 enum ssa_prop_result visit_phi (gphi *) FINAL OVERRIDE;
4049 void vrp_initialize (void);
4050 void vrp_finalize (bool);
4051 void check_all_array_refs (void);
4052 bool check_array_ref (location_t, tree, bool);
4053 bool check_mem_ref (location_t, tree, bool);
4054 void search_for_addr_array (tree, location_t);
4056 class vr_values vr_values;
4057 /* Temporary delegator to minimize code churn. */
4058 const value_range *get_value_range (const_tree op)
4059 { return vr_values.get_value_range (op); }
4060 void set_def_to_varying (const_tree def)
4061 { vr_values.set_def_to_varying (def); }
4062 void set_defs_to_varying (gimple *stmt)
4063 { vr_values.set_defs_to_varying (stmt); }
4064 void extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
4065 tree *output_p, value_range *vr)
4066 { vr_values.extract_range_from_stmt (stmt, taken_edge_p, output_p, vr); }
4067 bool update_value_range (const_tree op, value_range *vr)
4068 { return vr_values.update_value_range (op, vr); }
4069 void extract_range_basic (value_range *vr, gimple *stmt)
4070 { vr_values.extract_range_basic (vr, stmt); }
4071 void extract_range_from_phi_node (gphi *phi, value_range *vr)
4072 { vr_values.extract_range_from_phi_node (phi, vr); }
4074 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
4075 and "struct" hacks. If VRP can determine that the
4076 array subscript is a constant, check if it is outside valid
4077 range. If the array subscript is a RANGE, warn if it is
4078 non-overlapping with valid range.
4079 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR.
4080 Returns true if a warning has been issued. */
4083 vrp_prop::check_array_ref (location_t location, tree ref,
4084 bool ignore_off_by_one)
4086 const value_range *vr = NULL;
4087 tree low_sub, up_sub;
4088 tree low_bound, up_bound, up_bound_p1;
4090 if (TREE_NO_WARNING (ref))
4093 low_sub = up_sub = TREE_OPERAND (ref, 1);
4094 up_bound = array_ref_up_bound (ref);
4097 || TREE_CODE (up_bound) != INTEGER_CST
4098 || (warn_array_bounds < 2
4099 && array_at_struct_end_p (ref)))
4101 /* Accesses to trailing arrays via pointers may access storage
4102 beyond the types array bounds. For such arrays, or for flexible
4103 array members, as well as for other arrays of an unknown size,
4104 replace the upper bound with a more permissive one that assumes
4105 the size of the largest object is PTRDIFF_MAX. */
4106 tree eltsize = array_ref_element_size (ref);
4108 if (TREE_CODE (eltsize) != INTEGER_CST
4109 || integer_zerop (eltsize))
4111 up_bound = NULL_TREE;
4112 up_bound_p1 = NULL_TREE;
4116 tree maxbound = TYPE_MAX_VALUE (ptrdiff_type_node);
4117 tree arg = TREE_OPERAND (ref, 0);
4120 if (get_addr_base_and_unit_offset (arg, &off) && known_gt (off, 0))
4121 maxbound = wide_int_to_tree (sizetype,
4122 wi::sub (wi::to_wide (maxbound),
4125 maxbound = fold_convert (sizetype, maxbound);
4127 up_bound_p1 = int_const_binop (TRUNC_DIV_EXPR, maxbound, eltsize);
4129 up_bound = int_const_binop (MINUS_EXPR, up_bound_p1,
4130 build_int_cst (ptrdiff_type_node, 1));
4134 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
4135 build_int_cst (TREE_TYPE (up_bound), 1));
4137 low_bound = array_ref_low_bound (ref);
4139 tree artype = TREE_TYPE (TREE_OPERAND (ref, 0));
4141 bool warned = false;
4144 if (up_bound && tree_int_cst_equal (low_bound, up_bound_p1))
4145 warned = warning_at (location, OPT_Warray_bounds,
4146 "array subscript %E is above array bounds of %qT",
4149 if (TREE_CODE (low_sub) == SSA_NAME)
4151 vr = get_value_range (low_sub);
4152 if (!vr->undefined_p () && !vr->varying_p ())
4154 low_sub = vr->kind () == VR_RANGE ? vr->max () : vr->min ();
4155 up_sub = vr->kind () == VR_RANGE ? vr->min () : vr->max ();
4159 if (vr && vr->kind () == VR_ANTI_RANGE)
4162 && TREE_CODE (up_sub) == INTEGER_CST
4163 && (ignore_off_by_one
4164 ? tree_int_cst_lt (up_bound, up_sub)
4165 : tree_int_cst_le (up_bound, up_sub))
4166 && TREE_CODE (low_sub) == INTEGER_CST
4167 && tree_int_cst_le (low_sub, low_bound))
4168 warned = warning_at (location, OPT_Warray_bounds,
4169 "array subscript [%E, %E] is outside "
4170 "array bounds of %qT",
4171 low_sub, up_sub, artype);
4174 && TREE_CODE (up_sub) == INTEGER_CST
4175 && (ignore_off_by_one
4176 ? !tree_int_cst_le (up_sub, up_bound_p1)
4177 : !tree_int_cst_le (up_sub, up_bound)))
4179 if (dump_file && (dump_flags & TDF_DETAILS))
4181 fprintf (dump_file, "Array bound warning for ");
4182 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
4183 fprintf (dump_file, "\n");
4185 warned = warning_at (location, OPT_Warray_bounds,
4186 "array subscript %E is above array bounds of %qT",
4189 else if (TREE_CODE (low_sub) == INTEGER_CST
4190 && tree_int_cst_lt (low_sub, low_bound))
4192 if (dump_file && (dump_flags & TDF_DETAILS))
4194 fprintf (dump_file, "Array bound warning for ");
4195 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
4196 fprintf (dump_file, "\n");
4198 warned = warning_at (location, OPT_Warray_bounds,
4199 "array subscript %E is below array bounds of %qT",
4205 ref = TREE_OPERAND (ref, 0);
4206 if (TREE_CODE (ref) == COMPONENT_REF)
4207 ref = TREE_OPERAND (ref, 1);
4210 inform (DECL_SOURCE_LOCATION (ref), "while referencing %qD", ref);
4212 TREE_NO_WARNING (ref) = 1;
4218 /* Checks one MEM_REF in REF, located at LOCATION, for out-of-bounds
4219 references to string constants. If VRP can determine that the array
4220 subscript is a constant, check if it is outside valid range.
4221 If the array subscript is a RANGE, warn if it is non-overlapping
4223 IGNORE_OFF_BY_ONE is true if the MEM_REF is inside an ADDR_EXPR
4224 (used to allow one-past-the-end indices for code that takes
4225 the address of the just-past-the-end element of an array).
4226 Returns true if a warning has been issued. */
4229 vrp_prop::check_mem_ref (location_t location, tree ref,
4230 bool ignore_off_by_one)
4232 if (TREE_NO_WARNING (ref))
4235 tree arg = TREE_OPERAND (ref, 0);
4236 /* The constant and variable offset of the reference. */
4237 tree cstoff = TREE_OPERAND (ref, 1);
4238 tree varoff = NULL_TREE;
4240 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
4242 /* The array or string constant bounds in bytes. Initially set
4243 to [-MAXOBJSIZE - 1, MAXOBJSIZE] until a tighter bound is
4245 offset_int arrbounds[2] = { -maxobjsize - 1, maxobjsize };
4247 /* The minimum and maximum intermediate offset. For a reference
4248 to be valid, not only does the final offset/subscript must be
4249 in bounds but all intermediate offsets should be as well.
4250 GCC may be able to deal gracefully with such out-of-bounds
4251 offsets so the checking is only enbaled at -Warray-bounds=2
4252 where it may help detect bugs in uses of the intermediate
4253 offsets that could otherwise not be detectable. */
4254 offset_int ioff = wi::to_offset (fold_convert (ptrdiff_type_node, cstoff));
4255 offset_int extrema[2] = { 0, wi::abs (ioff) };
4257 /* The range of the byte offset into the reference. */
4258 offset_int offrange[2] = { 0, 0 };
4260 const value_range *vr = NULL;
4262 /* Determine the offsets and increment OFFRANGE for the bounds of each.
4263 The loop computes the range of the final offset for expressions such
4264 as (A + i0 + ... + iN)[CSTOFF] where i0 through iN are SSA_NAMEs in
4266 const unsigned limit = PARAM_VALUE (PARAM_SSA_NAME_DEF_CHAIN_LIMIT);
4267 for (unsigned n = 0; TREE_CODE (arg) == SSA_NAME && n < limit; ++n)
4269 gimple *def = SSA_NAME_DEF_STMT (arg);
4270 if (!is_gimple_assign (def))
4273 tree_code code = gimple_assign_rhs_code (def);
4274 if (code == POINTER_PLUS_EXPR)
4276 arg = gimple_assign_rhs1 (def);
4277 varoff = gimple_assign_rhs2 (def);
4279 else if (code == ASSERT_EXPR)
4281 arg = TREE_OPERAND (gimple_assign_rhs1 (def), 0);
4287 /* VAROFF should always be a SSA_NAME here (and not even
4288 INTEGER_CST) but there's no point in taking chances. */
4289 if (TREE_CODE (varoff) != SSA_NAME)
4292 vr = get_value_range (varoff);
4293 if (!vr || vr->undefined_p () || vr->varying_p ())
4296 if (!vr->constant_p ())
4299 if (vr->kind () == VR_RANGE)
4302 = wi::to_offset (fold_convert (ptrdiff_type_node, vr->min ()));
4304 = wi::to_offset (fold_convert (ptrdiff_type_node, vr->max ()));
4312 /* When MIN >= MAX, the offset is effectively in a union
4313 of two ranges: [-MAXOBJSIZE -1, MAX] and [MIN, MAXOBJSIZE].
4314 Since there is no way to represent such a range across
4315 additions, conservatively add [-MAXOBJSIZE -1, MAXOBJSIZE]
4317 offrange[0] += arrbounds[0];
4318 offrange[1] += arrbounds[1];
4323 /* For an anti-range, analogously to the above, conservatively
4324 add [-MAXOBJSIZE -1, MAXOBJSIZE] to OFFRANGE. */
4325 offrange[0] += arrbounds[0];
4326 offrange[1] += arrbounds[1];
4329 /* Keep track of the minimum and maximum offset. */
4330 if (offrange[1] < 0 && offrange[1] < extrema[0])
4331 extrema[0] = offrange[1];
4332 if (offrange[0] > 0 && offrange[0] > extrema[1])
4333 extrema[1] = offrange[0];
4335 if (offrange[0] < arrbounds[0])
4336 offrange[0] = arrbounds[0];
4338 if (offrange[1] > arrbounds[1])
4339 offrange[1] = arrbounds[1];
4342 if (TREE_CODE (arg) == ADDR_EXPR)
4344 arg = TREE_OPERAND (arg, 0);
4345 if (TREE_CODE (arg) != STRING_CST
4346 && TREE_CODE (arg) != VAR_DECL)
4352 /* The type of the object being referred to. It can be an array,
4353 string literal, or a non-array type when the MEM_REF represents
4354 a reference/subscript via a pointer to an object that is not
4355 an element of an array. References to members of structs and
4356 unions are excluded because MEM_REF doesn't make it possible
4357 to identify the member where the reference originated.
4358 Incomplete types are excluded as well because their size is
4360 tree reftype = TREE_TYPE (arg);
4361 if (POINTER_TYPE_P (reftype)
4362 || !COMPLETE_TYPE_P (reftype)
4363 || TREE_CODE (TYPE_SIZE_UNIT (reftype)) != INTEGER_CST
4364 || RECORD_OR_UNION_TYPE_P (reftype))
4370 if (TREE_CODE (reftype) == ARRAY_TYPE)
4372 eltsize = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (reftype)));
4373 if (tree dom = TYPE_DOMAIN (reftype))
4375 tree bnds[] = { TYPE_MIN_VALUE (dom), TYPE_MAX_VALUE (dom) };
4376 if (array_at_struct_end_p (arg) || !bnds[0] || !bnds[1])
4377 arrbounds[1] = wi::lrshift (maxobjsize, wi::floor_log2 (eltsize));
4379 arrbounds[1] = (wi::to_offset (bnds[1]) - wi::to_offset (bnds[0])
4383 arrbounds[1] = wi::lrshift (maxobjsize, wi::floor_log2 (eltsize));
4385 if (TREE_CODE (ref) == MEM_REF)
4387 /* For MEM_REF determine a tighter bound of the non-array
4389 tree eltype = TREE_TYPE (reftype);
4390 while (TREE_CODE (eltype) == ARRAY_TYPE)
4391 eltype = TREE_TYPE (eltype);
4392 eltsize = wi::to_offset (TYPE_SIZE_UNIT (eltype));
4398 arrbounds[1] = wi::to_offset (TYPE_SIZE_UNIT (reftype));
4401 offrange[0] += ioff;
4402 offrange[1] += ioff;
4404 /* Compute the more permissive upper bound when IGNORE_OFF_BY_ONE
4405 is set (when taking the address of the one-past-last element
4406 of an array) but always use the stricter bound in diagnostics. */
4407 offset_int ubound = arrbounds[1];
4408 if (ignore_off_by_one)
4411 if (offrange[0] >= ubound || offrange[1] < arrbounds[0])
4413 /* Treat a reference to a non-array object as one to an array
4414 of a single element. */
4415 if (TREE_CODE (reftype) != ARRAY_TYPE)
4416 reftype = build_array_type_nelts (reftype, 1);
4418 if (TREE_CODE (ref) == MEM_REF)
4420 /* Extract the element type out of MEM_REF and use its size
4421 to compute the index to print in the diagnostic; arrays
4422 in MEM_REF don't mean anything. A type with no size like
4423 void is as good as having a size of 1. */
4424 tree type = TREE_TYPE (ref);
4425 while (TREE_CODE (type) == ARRAY_TYPE)
4426 type = TREE_TYPE (type);
4427 if (tree size = TYPE_SIZE_UNIT (type))
4429 offrange[0] = offrange[0] / wi::to_offset (size);
4430 offrange[1] = offrange[1] / wi::to_offset (size);
4435 /* For anything other than MEM_REF, compute the index to
4436 print in the diagnostic as the offset over element size. */
4437 offrange[0] = offrange[0] / eltsize;
4438 offrange[1] = offrange[1] / eltsize;
4442 if (offrange[0] == offrange[1])
4443 warned = warning_at (location, OPT_Warray_bounds,
4444 "array subscript %wi is outside array bounds "
4446 offrange[0].to_shwi (), reftype);
4448 warned = warning_at (location, OPT_Warray_bounds,
4449 "array subscript [%wi, %wi] is outside "
4450 "array bounds of %qT",
4451 offrange[0].to_shwi (),
4452 offrange[1].to_shwi (), reftype);
4453 if (warned && DECL_P (arg))
4454 inform (DECL_SOURCE_LOCATION (arg), "while referencing %qD", arg);
4457 TREE_NO_WARNING (ref) = 1;
4461 if (warn_array_bounds < 2)
4464 /* At level 2 check also intermediate offsets. */
4466 if (extrema[i] < -arrbounds[1] || extrema[i = 1] > ubound)
4468 HOST_WIDE_INT tmpidx = extrema[i].to_shwi () / eltsize.to_shwi ();
4470 if (warning_at (location, OPT_Warray_bounds,
4471 "intermediate array offset %wi is outside array bounds "
4472 "of %qT", tmpidx, reftype))
4474 TREE_NO_WARNING (ref) = 1;
4482 /* Searches if the expr T, located at LOCATION computes
4483 address of an ARRAY_REF, and call check_array_ref on it. */
4486 vrp_prop::search_for_addr_array (tree t, location_t location)
4488 /* Check each ARRAY_REF and MEM_REF in the reference chain. */
4491 bool warned = false;
4492 if (TREE_CODE (t) == ARRAY_REF)
4493 warned = check_array_ref (location, t, true /*ignore_off_by_one*/);
4494 else if (TREE_CODE (t) == MEM_REF)
4495 warned = check_mem_ref (location, t, true /*ignore_off_by_one*/);
4498 TREE_NO_WARNING (t) = true;
4500 t = TREE_OPERAND (t, 0);
4502 while (handled_component_p (t) || TREE_CODE (t) == MEM_REF);
4504 if (TREE_CODE (t) != MEM_REF
4505 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR
4506 || TREE_NO_WARNING (t))
4509 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
4510 tree low_bound, up_bound, el_sz;
4511 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
4512 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
4513 || !TYPE_DOMAIN (TREE_TYPE (tem)))
4516 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
4517 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
4518 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
4520 || TREE_CODE (low_bound) != INTEGER_CST
4522 || TREE_CODE (up_bound) != INTEGER_CST
4524 || TREE_CODE (el_sz) != INTEGER_CST)
4528 if (!mem_ref_offset (t).is_constant (&idx))
4531 bool warned = false;
4532 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
4535 if (dump_file && (dump_flags & TDF_DETAILS))
4537 fprintf (dump_file, "Array bound warning for ");
4538 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
4539 fprintf (dump_file, "\n");
4541 warned = warning_at (location, OPT_Warray_bounds,
4542 "array subscript %wi is below "
4543 "array bounds of %qT",
4544 idx.to_shwi (), TREE_TYPE (tem));
4546 else if (idx > (wi::to_offset (up_bound)
4547 - wi::to_offset (low_bound) + 1))
4549 if (dump_file && (dump_flags & TDF_DETAILS))
4551 fprintf (dump_file, "Array bound warning for ");
4552 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
4553 fprintf (dump_file, "\n");
4555 warned = warning_at (location, OPT_Warray_bounds,
4556 "array subscript %wu is above "
4557 "array bounds of %qT",
4558 idx.to_uhwi (), TREE_TYPE (tem));
4564 inform (DECL_SOURCE_LOCATION (t), "while referencing %qD", t);
4566 TREE_NO_WARNING (t) = 1;
4570 /* walk_tree() callback that checks if *TP is
4571 an ARRAY_REF inside an ADDR_EXPR (in which an array
4572 subscript one outside the valid range is allowed). Call
4573 check_array_ref for each ARRAY_REF found. The location is
4577 check_array_bounds (tree *tp, int *walk_subtree, void *data)
4580 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
4581 location_t location;
4583 if (EXPR_HAS_LOCATION (t))
4584 location = EXPR_LOCATION (t);
4586 location = gimple_location (wi->stmt);
4588 *walk_subtree = TRUE;
4590 bool warned = false;
4591 vrp_prop *vrp_prop = (class vrp_prop *)wi->info;
4592 if (TREE_CODE (t) == ARRAY_REF)
4593 warned = vrp_prop->check_array_ref (location, t, false/*ignore_off_by_one*/);
4594 else if (TREE_CODE (t) == MEM_REF)
4595 warned = vrp_prop->check_mem_ref (location, t, false /*ignore_off_by_one*/);
4596 else if (TREE_CODE (t) == ADDR_EXPR)
4598 vrp_prop->search_for_addr_array (t, location);
4599 *walk_subtree = FALSE;
4601 /* Propagate the no-warning bit to the outer expression. */
4603 TREE_NO_WARNING (t) = true;
4608 /* A dom_walker subclass for use by vrp_prop::check_all_array_refs,
4609 to walk over all statements of all reachable BBs and call
4610 check_array_bounds on them. */
4612 class check_array_bounds_dom_walker : public dom_walker
4615 check_array_bounds_dom_walker (vrp_prop *prop)
4616 : dom_walker (CDI_DOMINATORS,
4617 /* Discover non-executable edges, preserving EDGE_EXECUTABLE
4618 flags, so that we can merge in information on
4619 non-executable edges from vrp_folder . */
4620 REACHABLE_BLOCKS_PRESERVING_FLAGS),
4622 ~check_array_bounds_dom_walker () {}
4624 edge before_dom_children (basic_block) FINAL OVERRIDE;
4630 /* Implementation of dom_walker::before_dom_children.
4632 Walk over all statements of BB and call check_array_bounds on them,
4633 and determine if there's a unique successor edge. */
4636 check_array_bounds_dom_walker::before_dom_children (basic_block bb)
4638 gimple_stmt_iterator si;
4639 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
4641 gimple *stmt = gsi_stmt (si);
4642 struct walk_stmt_info wi;
4643 if (!gimple_has_location (stmt)
4644 || is_gimple_debug (stmt))
4647 memset (&wi, 0, sizeof (wi));
4651 walk_gimple_op (stmt, check_array_bounds, &wi);
4654 /* Determine if there's a unique successor edge, and if so, return
4655 that back to dom_walker, ensuring that we don't visit blocks that
4656 became unreachable during the VRP propagation
4657 (PR tree-optimization/83312). */
4658 return find_taken_edge (bb, NULL_TREE);
4661 /* Walk over all statements of all reachable BBs and call check_array_bounds
4665 vrp_prop::check_all_array_refs ()
4667 check_array_bounds_dom_walker w (this);
4668 w.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
4671 /* Return true if all imm uses of VAR are either in STMT, or
4672 feed (optionally through a chain of single imm uses) GIMPLE_COND
4673 in basic block COND_BB. */
4676 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
4678 use_operand_p use_p, use2_p;
4679 imm_use_iterator iter;
4681 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
4682 if (USE_STMT (use_p) != stmt)
4684 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
4685 if (is_gimple_debug (use_stmt))
4687 while (is_gimple_assign (use_stmt)
4688 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
4689 && single_imm_use (gimple_assign_lhs (use_stmt),
4690 &use2_p, &use_stmt2))
4691 use_stmt = use_stmt2;
4692 if (gimple_code (use_stmt) != GIMPLE_COND
4693 || gimple_bb (use_stmt) != cond_bb)
4706 __builtin_unreachable ();
4708 x_5 = ASSERT_EXPR <x_3, ...>;
4709 If x_3 has no other immediate uses (checked by caller),
4710 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
4711 from the non-zero bitmask. */
4714 maybe_set_nonzero_bits (edge e, tree var)
4716 basic_block cond_bb = e->src;
4717 gimple *stmt = last_stmt (cond_bb);
4721 || gimple_code (stmt) != GIMPLE_COND
4722 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
4723 ? EQ_EXPR : NE_EXPR)
4724 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
4725 || !integer_zerop (gimple_cond_rhs (stmt)))
4728 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
4729 if (!is_gimple_assign (stmt)
4730 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
4731 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
4733 if (gimple_assign_rhs1 (stmt) != var)
4737 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
4739 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
4740 if (!gimple_assign_cast_p (stmt2)
4741 || gimple_assign_rhs1 (stmt2) != var
4742 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
4743 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
4744 != TYPE_PRECISION (TREE_TYPE (var))))
4747 cst = gimple_assign_rhs2 (stmt);
4748 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var),
4749 wi::to_wide (cst)));
4752 /* Convert range assertion expressions into the implied copies and
4753 copy propagate away the copies. Doing the trivial copy propagation
4754 here avoids the need to run the full copy propagation pass after
4757 FIXME, this will eventually lead to copy propagation removing the
4758 names that had useful range information attached to them. For
4759 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
4760 then N_i will have the range [3, +INF].
4762 However, by converting the assertion into the implied copy
4763 operation N_i = N_j, we will then copy-propagate N_j into the uses
4764 of N_i and lose the range information. We may want to hold on to
4765 ASSERT_EXPRs a little while longer as the ranges could be used in
4766 things like jump threading.
4768 The problem with keeping ASSERT_EXPRs around is that passes after
4769 VRP need to handle them appropriately.
4771 Another approach would be to make the range information a first
4772 class property of the SSA_NAME so that it can be queried from
4773 any pass. This is made somewhat more complex by the need for
4774 multiple ranges to be associated with one SSA_NAME. */
4777 remove_range_assertions (void)
4780 gimple_stmt_iterator si;
4781 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
4782 a basic block preceeded by GIMPLE_COND branching to it and
4783 __builtin_trap, -1 if not yet checked, 0 otherwise. */
4786 /* Note that the BSI iterator bump happens at the bottom of the
4787 loop and no bump is necessary if we're removing the statement
4788 referenced by the current BSI. */
4789 FOR_EACH_BB_FN (bb, cfun)
4790 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
4792 gimple *stmt = gsi_stmt (si);
4794 if (is_gimple_assign (stmt)
4795 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
4797 tree lhs = gimple_assign_lhs (stmt);
4798 tree rhs = gimple_assign_rhs1 (stmt);
4801 var = ASSERT_EXPR_VAR (rhs);
4803 if (TREE_CODE (var) == SSA_NAME
4804 && !POINTER_TYPE_P (TREE_TYPE (lhs))
4805 && SSA_NAME_RANGE_INFO (lhs))
4807 if (is_unreachable == -1)
4810 if (single_pred_p (bb)
4811 && assert_unreachable_fallthru_edge_p
4812 (single_pred_edge (bb)))
4816 if (x_7 >= 10 && x_7 < 20)
4817 __builtin_unreachable ();
4818 x_8 = ASSERT_EXPR <x_7, ...>;
4819 if the only uses of x_7 are in the ASSERT_EXPR and
4820 in the condition. In that case, we can copy the
4821 range info from x_8 computed in this pass also
4824 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
4827 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
4828 SSA_NAME_RANGE_INFO (lhs)->get_min (),
4829 SSA_NAME_RANGE_INFO (lhs)->get_max ());
4830 maybe_set_nonzero_bits (single_pred_edge (bb), var);
4834 /* Propagate the RHS into every use of the LHS. For SSA names
4835 also propagate abnormals as it merely restores the original
4836 IL in this case (an replace_uses_by would assert). */
4837 if (TREE_CODE (var) == SSA_NAME)
4839 imm_use_iterator iter;
4840 use_operand_p use_p;
4842 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
4843 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
4844 SET_USE (use_p, var);
4847 replace_uses_by (lhs, var);
4849 /* And finally, remove the copy, it is not needed. */
4850 gsi_remove (&si, true);
4851 release_defs (stmt);
4855 if (!is_gimple_debug (gsi_stmt (si)))
4862 /* Return true if STMT is interesting for VRP. */
4865 stmt_interesting_for_vrp (gimple *stmt)
4867 if (gimple_code (stmt) == GIMPLE_PHI)
4869 tree res = gimple_phi_result (stmt);
4870 return (!virtual_operand_p (res)
4871 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
4872 || POINTER_TYPE_P (TREE_TYPE (res))));
4874 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
4876 tree lhs = gimple_get_lhs (stmt);
4878 /* In general, assignments with virtual operands are not useful
4879 for deriving ranges, with the obvious exception of calls to
4880 builtin functions. */
4881 if (lhs && TREE_CODE (lhs) == SSA_NAME
4882 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
4883 || POINTER_TYPE_P (TREE_TYPE (lhs)))
4884 && (is_gimple_call (stmt)
4885 || !gimple_vuse (stmt)))
4887 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
4888 switch (gimple_call_internal_fn (stmt))
4890 case IFN_ADD_OVERFLOW:
4891 case IFN_SUB_OVERFLOW:
4892 case IFN_MUL_OVERFLOW:
4893 case IFN_ATOMIC_COMPARE_EXCHANGE:
4894 /* These internal calls return _Complex integer type,
4895 but are interesting to VRP nevertheless. */
4896 if (lhs && TREE_CODE (lhs) == SSA_NAME)
4903 else if (gimple_code (stmt) == GIMPLE_COND
4904 || gimple_code (stmt) == GIMPLE_SWITCH)
4910 /* Initialization required by ssa_propagate engine. */
4913 vrp_prop::vrp_initialize ()
4917 FOR_EACH_BB_FN (bb, cfun)
4919 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
4922 gphi *phi = si.phi ();
4923 if (!stmt_interesting_for_vrp (phi))
4925 tree lhs = PHI_RESULT (phi);
4926 set_def_to_varying (lhs);
4927 prop_set_simulate_again (phi, false);
4930 prop_set_simulate_again (phi, true);
4933 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
4936 gimple *stmt = gsi_stmt (si);
4938 /* If the statement is a control insn, then we do not
4939 want to avoid simulating the statement once. Failure
4940 to do so means that those edges will never get added. */
4941 if (stmt_ends_bb_p (stmt))
4942 prop_set_simulate_again (stmt, true);
4943 else if (!stmt_interesting_for_vrp (stmt))
4945 set_defs_to_varying (stmt);
4946 prop_set_simulate_again (stmt, false);
4949 prop_set_simulate_again (stmt, true);
4954 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
4955 that includes the value VAL. The search is restricted to the range
4956 [START_IDX, n - 1] where n is the size of VEC.
4958 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
4961 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
4962 it is placed in IDX and false is returned.
4964 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
4968 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
4970 size_t n = gimple_switch_num_labels (stmt);
4973 /* Find case label for minimum of the value range or the next one.
4974 At each iteration we are searching in [low, high - 1]. */
4976 for (low = start_idx, high = n; high != low; )
4980 /* Note that i != high, so we never ask for n. */
4981 size_t i = (high + low) / 2;
4982 t = gimple_switch_label (stmt, i);
4984 /* Cache the result of comparing CASE_LOW and val. */
4985 cmp = tree_int_cst_compare (CASE_LOW (t), val);
4989 /* Ranges cannot be empty. */
4998 if (CASE_HIGH (t) != NULL
4999 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
5011 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
5012 for values between MIN and MAX. The first index is placed in MIN_IDX. The
5013 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
5014 then MAX_IDX < MIN_IDX.
5015 Returns true if the default label is not needed. */
5018 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
5022 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
5023 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
5027 && max_take_default)
5029 /* Only the default case label reached.
5030 Return an empty range. */
5037 bool take_default = min_take_default || max_take_default;
5041 if (max_take_default)
5044 /* If the case label range is continuous, we do not need
5045 the default case label. Verify that. */
5046 high = CASE_LOW (gimple_switch_label (stmt, i));
5047 if (CASE_HIGH (gimple_switch_label (stmt, i)))
5048 high = CASE_HIGH (gimple_switch_label (stmt, i));
5049 for (k = i + 1; k <= j; ++k)
5051 low = CASE_LOW (gimple_switch_label (stmt, k));
5052 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
5054 take_default = true;
5058 if (CASE_HIGH (gimple_switch_label (stmt, k)))
5059 high = CASE_HIGH (gimple_switch_label (stmt, k));
5064 return !take_default;
5068 /* Evaluate statement STMT. If the statement produces a useful range,
5069 return SSA_PROP_INTERESTING and record the SSA name with the
5070 interesting range into *OUTPUT_P.
5072 If STMT is a conditional branch and we can determine its truth
5073 value, the taken edge is recorded in *TAKEN_EDGE_P.
5075 If STMT produces a varying value, return SSA_PROP_VARYING. */
5077 enum ssa_prop_result
5078 vrp_prop::visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
5080 tree lhs = gimple_get_lhs (stmt);
5082 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr);
5086 if (update_value_range (*output_p, &vr))
5088 if (dump_file && (dump_flags & TDF_DETAILS))
5090 fprintf (dump_file, "Found new range for ");
5091 print_generic_expr (dump_file, *output_p);
5092 fprintf (dump_file, ": ");
5093 dump_value_range (dump_file, &vr);
5094 fprintf (dump_file, "\n");
5097 if (vr.varying_p ())
5098 return SSA_PROP_VARYING;
5100 return SSA_PROP_INTERESTING;
5102 return SSA_PROP_NOT_INTERESTING;
5105 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
5106 switch (gimple_call_internal_fn (stmt))
5108 case IFN_ADD_OVERFLOW:
5109 case IFN_SUB_OVERFLOW:
5110 case IFN_MUL_OVERFLOW:
5111 case IFN_ATOMIC_COMPARE_EXCHANGE:
5112 /* These internal calls return _Complex integer type,
5113 which VRP does not track, but the immediate uses
5114 thereof might be interesting. */
5115 if (lhs && TREE_CODE (lhs) == SSA_NAME)
5117 imm_use_iterator iter;
5118 use_operand_p use_p;
5119 enum ssa_prop_result res = SSA_PROP_VARYING;
5121 set_def_to_varying (lhs);
5123 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
5125 gimple *use_stmt = USE_STMT (use_p);
5126 if (!is_gimple_assign (use_stmt))
5128 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
5129 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
5131 tree rhs1 = gimple_assign_rhs1 (use_stmt);
5132 tree use_lhs = gimple_assign_lhs (use_stmt);
5133 if (TREE_CODE (rhs1) != rhs_code
5134 || TREE_OPERAND (rhs1, 0) != lhs
5135 || TREE_CODE (use_lhs) != SSA_NAME
5136 || !stmt_interesting_for_vrp (use_stmt)
5137 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
5138 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
5139 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
5142 /* If there is a change in the value range for any of the
5143 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
5144 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
5145 or IMAGPART_EXPR immediate uses, but none of them have
5146 a change in their value ranges, return
5147 SSA_PROP_NOT_INTERESTING. If there are no
5148 {REAL,IMAG}PART_EXPR uses at all,
5149 return SSA_PROP_VARYING. */
5151 extract_range_basic (&new_vr, use_stmt);
5152 const value_range *old_vr = get_value_range (use_lhs);
5153 if (!old_vr->equal_p (new_vr, /*ignore_equivs=*/false))
5154 res = SSA_PROP_INTERESTING;
5156 res = SSA_PROP_NOT_INTERESTING;
5157 new_vr.equiv_clear ();
5158 if (res == SSA_PROP_INTERESTING)
5172 /* All other statements produce nothing of interest for VRP, so mark
5173 their outputs varying and prevent further simulation. */
5174 set_defs_to_varying (stmt);
5176 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
5179 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5180 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5181 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5182 possible such range. The resulting range is not canonicalized. */
5185 union_ranges (enum value_range_kind *vr0type,
5186 tree *vr0min, tree *vr0max,
5187 enum value_range_kind vr1type,
5188 tree vr1min, tree vr1max)
5190 int cmpmin = compare_values (*vr0min, vr1min);
5191 int cmpmax = compare_values (*vr0max, vr1max);
5192 bool mineq = cmpmin == 0;
5193 bool maxeq = cmpmax == 0;
5195 /* [] is vr0, () is vr1 in the following classification comments. */
5199 if (*vr0type == vr1type)
5200 /* Nothing to do for equal ranges. */
5202 else if ((*vr0type == VR_RANGE
5203 && vr1type == VR_ANTI_RANGE)
5204 || (*vr0type == VR_ANTI_RANGE
5205 && vr1type == VR_RANGE))
5207 /* For anti-range with range union the result is varying. */
5213 else if (operand_less_p (*vr0max, vr1min) == 1
5214 || operand_less_p (vr1max, *vr0min) == 1)
5216 /* [ ] ( ) or ( ) [ ]
5217 If the ranges have an empty intersection, result of the union
5218 operation is the anti-range or if both are anti-ranges
5220 if (*vr0type == VR_ANTI_RANGE
5221 && vr1type == VR_ANTI_RANGE)
5223 else if (*vr0type == VR_ANTI_RANGE
5224 && vr1type == VR_RANGE)
5226 else if (*vr0type == VR_RANGE
5227 && vr1type == VR_ANTI_RANGE)
5233 else if (*vr0type == VR_RANGE
5234 && vr1type == VR_RANGE)
5236 /* The result is the convex hull of both ranges. */
5237 if (operand_less_p (*vr0max, vr1min) == 1)
5239 /* If the result can be an anti-range, create one. */
5240 if (TREE_CODE (*vr0max) == INTEGER_CST
5241 && TREE_CODE (vr1min) == INTEGER_CST
5242 && vrp_val_is_min (*vr0min)
5243 && vrp_val_is_max (vr1max))
5245 tree min = int_const_binop (PLUS_EXPR,
5247 build_int_cst (TREE_TYPE (*vr0max), 1));
5248 tree max = int_const_binop (MINUS_EXPR,
5250 build_int_cst (TREE_TYPE (vr1min), 1));
5251 if (!operand_less_p (max, min))
5253 *vr0type = VR_ANTI_RANGE;
5265 /* If the result can be an anti-range, create one. */
5266 if (TREE_CODE (vr1max) == INTEGER_CST
5267 && TREE_CODE (*vr0min) == INTEGER_CST
5268 && vrp_val_is_min (vr1min)
5269 && vrp_val_is_max (*vr0max))
5271 tree min = int_const_binop (PLUS_EXPR,
5273 build_int_cst (TREE_TYPE (vr1max), 1));
5274 tree max = int_const_binop (MINUS_EXPR,
5276 build_int_cst (TREE_TYPE (*vr0min), 1));
5277 if (!operand_less_p (max, min))
5279 *vr0type = VR_ANTI_RANGE;
5293 else if ((maxeq || cmpmax == 1)
5294 && (mineq || cmpmin == -1))
5296 /* [ ( ) ] or [( ) ] or [ ( )] */
5297 if (*vr0type == VR_RANGE
5298 && vr1type == VR_RANGE)
5300 else if (*vr0type == VR_ANTI_RANGE
5301 && vr1type == VR_ANTI_RANGE)
5307 else if (*vr0type == VR_ANTI_RANGE
5308 && vr1type == VR_RANGE)
5310 /* Arbitrarily choose the right or left gap. */
5311 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
5312 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
5313 build_int_cst (TREE_TYPE (vr1min), 1));
5314 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
5315 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
5316 build_int_cst (TREE_TYPE (vr1max), 1));
5320 else if (*vr0type == VR_RANGE
5321 && vr1type == VR_ANTI_RANGE)
5322 /* The result covers everything. */
5327 else if ((maxeq || cmpmax == -1)
5328 && (mineq || cmpmin == 1))
5330 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5331 if (*vr0type == VR_RANGE
5332 && vr1type == VR_RANGE)
5338 else if (*vr0type == VR_ANTI_RANGE
5339 && vr1type == VR_ANTI_RANGE)
5341 else if (*vr0type == VR_RANGE
5342 && vr1type == VR_ANTI_RANGE)
5344 *vr0type = VR_ANTI_RANGE;
5345 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
5347 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
5348 build_int_cst (TREE_TYPE (*vr0min), 1));
5351 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
5353 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
5354 build_int_cst (TREE_TYPE (*vr0max), 1));
5360 else if (*vr0type == VR_ANTI_RANGE
5361 && vr1type == VR_RANGE)
5362 /* The result covers everything. */
5367 else if (cmpmin == -1
5369 && (operand_less_p (vr1min, *vr0max) == 1
5370 || operand_equal_p (vr1min, *vr0max, 0)))
5372 /* [ ( ] ) or [ ]( ) */
5373 if (*vr0type == VR_RANGE
5374 && vr1type == VR_RANGE)
5376 else if (*vr0type == VR_ANTI_RANGE
5377 && vr1type == VR_ANTI_RANGE)
5379 else if (*vr0type == VR_ANTI_RANGE
5380 && vr1type == VR_RANGE)
5382 if (TREE_CODE (vr1min) == INTEGER_CST)
5383 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
5384 build_int_cst (TREE_TYPE (vr1min), 1));
5388 else if (*vr0type == VR_RANGE
5389 && vr1type == VR_ANTI_RANGE)
5391 if (TREE_CODE (*vr0max) == INTEGER_CST)
5394 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
5395 build_int_cst (TREE_TYPE (*vr0max), 1));
5404 else if (cmpmin == 1
5406 && (operand_less_p (*vr0min, vr1max) == 1
5407 || operand_equal_p (*vr0min, vr1max, 0)))
5409 /* ( [ ) ] or ( )[ ] */
5410 if (*vr0type == VR_RANGE
5411 && vr1type == VR_RANGE)
5413 else if (*vr0type == VR_ANTI_RANGE
5414 && vr1type == VR_ANTI_RANGE)
5416 else if (*vr0type == VR_ANTI_RANGE
5417 && vr1type == VR_RANGE)
5419 if (TREE_CODE (vr1max) == INTEGER_CST)
5420 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
5421 build_int_cst (TREE_TYPE (vr1max), 1));
5425 else if (*vr0type == VR_RANGE
5426 && vr1type == VR_ANTI_RANGE)
5428 if (TREE_CODE (*vr0min) == INTEGER_CST)
5431 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
5432 build_int_cst (TREE_TYPE (*vr0min), 1));
5447 *vr0type = VR_VARYING;
5448 *vr0min = NULL_TREE;
5449 *vr0max = NULL_TREE;
5452 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5453 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5454 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5455 possible such range. The resulting range is not canonicalized. */
5458 intersect_ranges (enum value_range_kind *vr0type,
5459 tree *vr0min, tree *vr0max,
5460 enum value_range_kind vr1type,
5461 tree vr1min, tree vr1max)
5463 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
5464 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
5466 /* [] is vr0, () is vr1 in the following classification comments. */
5470 if (*vr0type == vr1type)
5471 /* Nothing to do for equal ranges. */
5473 else if ((*vr0type == VR_RANGE
5474 && vr1type == VR_ANTI_RANGE)
5475 || (*vr0type == VR_ANTI_RANGE
5476 && vr1type == VR_RANGE))
5478 /* For anti-range with range intersection the result is empty. */
5479 *vr0type = VR_UNDEFINED;
5480 *vr0min = NULL_TREE;
5481 *vr0max = NULL_TREE;
5486 else if (operand_less_p (*vr0max, vr1min) == 1
5487 || operand_less_p (vr1max, *vr0min) == 1)
5489 /* [ ] ( ) or ( ) [ ]
5490 If the ranges have an empty intersection, the result of the
5491 intersect operation is the range for intersecting an
5492 anti-range with a range or empty when intersecting two ranges. */
5493 if (*vr0type == VR_RANGE
5494 && vr1type == VR_ANTI_RANGE)
5496 else if (*vr0type == VR_ANTI_RANGE
5497 && vr1type == VR_RANGE)
5503 else if (*vr0type == VR_RANGE
5504 && vr1type == VR_RANGE)
5506 *vr0type = VR_UNDEFINED;
5507 *vr0min = NULL_TREE;
5508 *vr0max = NULL_TREE;
5510 else if (*vr0type == VR_ANTI_RANGE
5511 && vr1type == VR_ANTI_RANGE)
5513 /* If the anti-ranges are adjacent to each other merge them. */
5514 if (TREE_CODE (*vr0max) == INTEGER_CST
5515 && TREE_CODE (vr1min) == INTEGER_CST
5516 && operand_less_p (*vr0max, vr1min) == 1
5517 && integer_onep (int_const_binop (MINUS_EXPR,
5520 else if (TREE_CODE (vr1max) == INTEGER_CST
5521 && TREE_CODE (*vr0min) == INTEGER_CST
5522 && operand_less_p (vr1max, *vr0min) == 1
5523 && integer_onep (int_const_binop (MINUS_EXPR,
5526 /* Else arbitrarily take VR0. */
5529 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
5530 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
5532 /* [ ( ) ] or [( ) ] or [ ( )] */
5533 if (*vr0type == VR_RANGE
5534 && vr1type == VR_RANGE)
5536 /* If both are ranges the result is the inner one. */
5541 else if (*vr0type == VR_RANGE
5542 && vr1type == VR_ANTI_RANGE)
5544 /* Choose the right gap if the left one is empty. */
5547 if (TREE_CODE (vr1max) != INTEGER_CST)
5549 else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1
5550 && !TYPE_UNSIGNED (TREE_TYPE (vr1max)))
5552 = int_const_binop (MINUS_EXPR, vr1max,
5553 build_int_cst (TREE_TYPE (vr1max), -1));
5556 = int_const_binop (PLUS_EXPR, vr1max,
5557 build_int_cst (TREE_TYPE (vr1max), 1));
5559 /* Choose the left gap if the right one is empty. */
5562 if (TREE_CODE (vr1min) != INTEGER_CST)
5564 else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1
5565 && !TYPE_UNSIGNED (TREE_TYPE (vr1min)))
5567 = int_const_binop (PLUS_EXPR, vr1min,
5568 build_int_cst (TREE_TYPE (vr1min), -1));
5571 = int_const_binop (MINUS_EXPR, vr1min,
5572 build_int_cst (TREE_TYPE (vr1min), 1));
5574 /* Choose the anti-range if the range is effectively varying. */
5575 else if (vrp_val_is_min (*vr0min)
5576 && vrp_val_is_max (*vr0max))
5582 /* Else choose the range. */
5584 else if (*vr0type == VR_ANTI_RANGE
5585 && vr1type == VR_ANTI_RANGE)
5586 /* If both are anti-ranges the result is the outer one. */
5588 else if (*vr0type == VR_ANTI_RANGE
5589 && vr1type == VR_RANGE)
5591 /* The intersection is empty. */
5592 *vr0type = VR_UNDEFINED;
5593 *vr0min = NULL_TREE;
5594 *vr0max = NULL_TREE;
5599 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
5600 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
5602 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5603 if (*vr0type == VR_RANGE
5604 && vr1type == VR_RANGE)
5605 /* Choose the inner range. */
5607 else if (*vr0type == VR_ANTI_RANGE
5608 && vr1type == VR_RANGE)
5610 /* Choose the right gap if the left is empty. */
5613 *vr0type = VR_RANGE;
5614 if (TREE_CODE (*vr0max) != INTEGER_CST)
5616 else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1
5617 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max)))
5619 = int_const_binop (MINUS_EXPR, *vr0max,
5620 build_int_cst (TREE_TYPE (*vr0max), -1));
5623 = int_const_binop (PLUS_EXPR, *vr0max,
5624 build_int_cst (TREE_TYPE (*vr0max), 1));
5627 /* Choose the left gap if the right is empty. */
5630 *vr0type = VR_RANGE;
5631 if (TREE_CODE (*vr0min) != INTEGER_CST)
5633 else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1
5634 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min)))
5636 = int_const_binop (PLUS_EXPR, *vr0min,
5637 build_int_cst (TREE_TYPE (*vr0min), -1));
5640 = int_const_binop (MINUS_EXPR, *vr0min,
5641 build_int_cst (TREE_TYPE (*vr0min), 1));
5644 /* Choose the anti-range if the range is effectively varying. */
5645 else if (vrp_val_is_min (vr1min)
5646 && vrp_val_is_max (vr1max))
5648 /* Choose the anti-range if it is ~[0,0], that range is special
5649 enough to special case when vr1's range is relatively wide.
5650 At least for types bigger than int - this covers pointers
5651 and arguments to functions like ctz. */
5652 else if (*vr0min == *vr0max
5653 && integer_zerop (*vr0min)
5654 && ((TYPE_PRECISION (TREE_TYPE (*vr0min))
5655 >= TYPE_PRECISION (integer_type_node))
5656 || POINTER_TYPE_P (TREE_TYPE (*vr0min)))
5657 && TREE_CODE (vr1max) == INTEGER_CST
5658 && TREE_CODE (vr1min) == INTEGER_CST
5659 && (wi::clz (wi::to_wide (vr1max) - wi::to_wide (vr1min))
5660 < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2))
5662 /* Else choose the range. */
5670 else if (*vr0type == VR_ANTI_RANGE
5671 && vr1type == VR_ANTI_RANGE)
5673 /* If both are anti-ranges the result is the outer one. */
5678 else if (vr1type == VR_ANTI_RANGE
5679 && *vr0type == VR_RANGE)
5681 /* The intersection is empty. */
5682 *vr0type = VR_UNDEFINED;
5683 *vr0min = NULL_TREE;
5684 *vr0max = NULL_TREE;
5689 else if ((operand_less_p (vr1min, *vr0max) == 1
5690 || operand_equal_p (vr1min, *vr0max, 0))
5691 && operand_less_p (*vr0min, vr1min) == 1)
5693 /* [ ( ] ) or [ ]( ) */
5694 if (*vr0type == VR_ANTI_RANGE
5695 && vr1type == VR_ANTI_RANGE)
5697 else if (*vr0type == VR_RANGE
5698 && vr1type == VR_RANGE)
5700 else if (*vr0type == VR_RANGE
5701 && vr1type == VR_ANTI_RANGE)
5703 if (TREE_CODE (vr1min) == INTEGER_CST)
5704 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
5705 build_int_cst (TREE_TYPE (vr1min), 1));
5709 else if (*vr0type == VR_ANTI_RANGE
5710 && vr1type == VR_RANGE)
5712 *vr0type = VR_RANGE;
5713 if (TREE_CODE (*vr0max) == INTEGER_CST)
5714 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
5715 build_int_cst (TREE_TYPE (*vr0max), 1));
5723 else if ((operand_less_p (*vr0min, vr1max) == 1
5724 || operand_equal_p (*vr0min, vr1max, 0))
5725 && operand_less_p (vr1min, *vr0min) == 1)
5727 /* ( [ ) ] or ( )[ ] */
5728 if (*vr0type == VR_ANTI_RANGE
5729 && vr1type == VR_ANTI_RANGE)
5731 else if (*vr0type == VR_RANGE
5732 && vr1type == VR_RANGE)
5734 else if (*vr0type == VR_RANGE
5735 && vr1type == VR_ANTI_RANGE)
5737 if (TREE_CODE (vr1max) == INTEGER_CST)
5738 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
5739 build_int_cst (TREE_TYPE (vr1max), 1));
5743 else if (*vr0type == VR_ANTI_RANGE
5744 && vr1type == VR_RANGE)
5746 *vr0type = VR_RANGE;
5747 if (TREE_CODE (*vr0min) == INTEGER_CST)
5748 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
5749 build_int_cst (TREE_TYPE (*vr0min), 1));
5758 /* If we know the intersection is empty, there's no need to
5759 conservatively add anything else to the set. */
5760 if (*vr0type == VR_UNDEFINED)
5763 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
5764 result for the intersection. That's always a conservative
5765 correct estimate unless VR1 is a constant singleton range
5766 in which case we choose that. */
5767 if (vr1type == VR_RANGE
5768 && is_gimple_min_invariant (vr1min)
5769 && vrp_operand_equal_p (vr1min, vr1max))
5778 /* Helper for the intersection operation for value ranges. Given two
5779 value ranges VR0 and VR1, return the intersection of the two
5780 ranges. This may not be the smallest possible such range. */
5783 value_range_base::intersect_helper (const value_range_base *vr0,
5784 const value_range_base *vr1)
5786 /* If either range is VR_VARYING the other one wins. */
5787 if (vr1->varying_p ())
5789 if (vr0->varying_p ())
5792 /* When either range is VR_UNDEFINED the resulting range is
5793 VR_UNDEFINED, too. */
5794 if (vr0->undefined_p ())
5796 if (vr1->undefined_p ())
5799 value_range_kind vr0type = vr0->kind ();
5800 tree vr0min = vr0->min ();
5801 tree vr0max = vr0->max ();
5802 intersect_ranges (&vr0type, &vr0min, &vr0max,
5803 vr1->kind (), vr1->min (), vr1->max ());
5804 /* Make sure to canonicalize the result though as the inversion of a
5805 VR_RANGE can still be a VR_RANGE. Work on a temporary so we can
5806 fall back to vr0 when this turns things to varying. */
5807 value_range_base tem;
5808 if (vr0type == VR_UNDEFINED)
5809 tem.set_undefined ();
5810 else if (vr0type == VR_VARYING)
5811 tem.set_varying (vr0->type ());
5813 tem.set (vr0type, vr0min, vr0max);
5814 /* If that failed, use the saved original VR0. */
5815 if (tem.varying_p ())
5822 value_range_base::intersect (const value_range_base *other)
5824 if (dump_file && (dump_flags & TDF_DETAILS))
5826 fprintf (dump_file, "Intersecting\n ");
5827 dump_value_range (dump_file, this);
5828 fprintf (dump_file, "\nand\n ");
5829 dump_value_range (dump_file, other);
5830 fprintf (dump_file, "\n");
5833 *this = intersect_helper (this, other);
5835 if (dump_file && (dump_flags & TDF_DETAILS))
5837 fprintf (dump_file, "to\n ");
5838 dump_value_range (dump_file, this);
5839 fprintf (dump_file, "\n");
5844 value_range::intersect (const value_range *other)
5846 if (dump_file && (dump_flags & TDF_DETAILS))
5848 fprintf (dump_file, "Intersecting\n ");
5849 dump_value_range (dump_file, this);
5850 fprintf (dump_file, "\nand\n ");
5851 dump_value_range (dump_file, other);
5852 fprintf (dump_file, "\n");
5855 /* If THIS is varying we want to pick up equivalences from OTHER.
5856 Just special-case this here rather than trying to fixup after the
5858 if (this->varying_p ())
5859 this->deep_copy (other);
5862 value_range_base tem = intersect_helper (this, other);
5863 this->update (tem.kind (), tem.min (), tem.max ());
5865 /* If the result is VR_UNDEFINED there is no need to mess with
5867 if (!undefined_p ())
5869 /* The resulting set of equivalences for range intersection
5870 is the union of the two sets. */
5871 if (m_equiv && other->m_equiv && m_equiv != other->m_equiv)
5872 bitmap_ior_into (m_equiv, other->m_equiv);
5873 else if (other->m_equiv && !m_equiv)
5875 /* All equivalence bitmaps are allocated from the same
5876 obstack. So we can use the obstack associated with
5877 VR to allocate this->m_equiv. */
5878 m_equiv = BITMAP_ALLOC (other->m_equiv->obstack);
5879 bitmap_copy (m_equiv, other->m_equiv);
5884 if (dump_file && (dump_flags & TDF_DETAILS))
5886 fprintf (dump_file, "to\n ");
5887 dump_value_range (dump_file, this);
5888 fprintf (dump_file, "\n");
5892 /* Helper for meet operation for value ranges. Given two value ranges VR0 and
5893 VR1, return a range that contains both VR0 and VR1. This may not be the
5894 smallest possible such range. */
5897 value_range_base::union_helper (const value_range_base *vr0,
5898 const value_range_base *vr1)
5900 /* VR0 has the resulting range if VR1 is undefined or VR0 is varying. */
5901 if (vr1->undefined_p ()
5902 || vr0->varying_p ())
5905 /* VR1 has the resulting range if VR0 is undefined or VR1 is varying. */
5906 if (vr0->undefined_p ()
5907 || vr1->varying_p ())
5910 value_range_kind vr0type = vr0->kind ();
5911 tree vr0min = vr0->min ();
5912 tree vr0max = vr0->max ();
5913 union_ranges (&vr0type, &vr0min, &vr0max,
5914 vr1->kind (), vr1->min (), vr1->max ());
5916 /* Work on a temporary so we can still use vr0 when union returns varying. */
5917 value_range_base tem;
5918 if (vr0type == VR_UNDEFINED)
5919 tem.set_undefined ();
5920 else if (vr0type == VR_VARYING)
5921 tem.set_varying (vr0->type ());
5923 tem.set (vr0type, vr0min, vr0max);
5925 /* Failed to find an efficient meet. Before giving up and setting
5926 the result to VARYING, see if we can at least derive a useful
5928 if (tem.varying_p ()
5929 && range_includes_zero_p (vr0) == 0
5930 && range_includes_zero_p (vr1) == 0)
5932 tem.set_nonzero (vr0->type ());
5940 /* Meet operation for value ranges. Given two value ranges VR0 and
5941 VR1, store in VR0 a range that contains both VR0 and VR1. This
5942 may not be the smallest possible such range. */
5945 value_range_base::union_ (const value_range_base *other)
5947 if (dump_file && (dump_flags & TDF_DETAILS))
5949 fprintf (dump_file, "Meeting\n ");
5950 dump_value_range (dump_file, this);
5951 fprintf (dump_file, "\nand\n ");
5952 dump_value_range (dump_file, other);
5953 fprintf (dump_file, "\n");
5956 *this = union_helper (this, other);
5958 if (dump_file && (dump_flags & TDF_DETAILS))
5960 fprintf (dump_file, "to\n ");
5961 dump_value_range (dump_file, this);
5962 fprintf (dump_file, "\n");
5967 value_range::union_ (const value_range *other)
5969 if (dump_file && (dump_flags & TDF_DETAILS))
5971 fprintf (dump_file, "Meeting\n ");
5972 dump_value_range (dump_file, this);
5973 fprintf (dump_file, "\nand\n ");
5974 dump_value_range (dump_file, other);
5975 fprintf (dump_file, "\n");
5978 /* If THIS is undefined we want to pick up equivalences from OTHER.
5979 Just special-case this here rather than trying to fixup after the fact. */
5980 if (this->undefined_p ())
5981 this->deep_copy (other);
5984 value_range_base tem = union_helper (this, other);
5985 this->update (tem.kind (), tem.min (), tem.max ());
5987 /* The resulting set of equivalences is always the intersection of
5989 if (this->m_equiv && other->m_equiv && this->m_equiv != other->m_equiv)
5990 bitmap_and_into (this->m_equiv, other->m_equiv);
5991 else if (this->m_equiv && !other->m_equiv)
5992 bitmap_clear (this->m_equiv);
5995 if (dump_file && (dump_flags & TDF_DETAILS))
5997 fprintf (dump_file, "to\n ");
5998 dump_value_range (dump_file, this);
5999 fprintf (dump_file, "\n");
6003 /* Normalize symbolics into constants. */
6006 value_range_base::normalize_symbolics () const
6008 if (varying_p () || undefined_p ())
6010 tree ttype = type ();
6011 bool min_symbolic = !is_gimple_min_invariant (min ());
6012 bool max_symbolic = !is_gimple_min_invariant (max ());
6013 if (!min_symbolic && !max_symbolic)
6016 // [SYM, SYM] -> VARYING
6017 if (min_symbolic && max_symbolic)
6019 value_range_base var;
6020 var.set_varying (ttype);
6023 if (kind () == VR_RANGE)
6025 // [SYM, NUM] -> [-MIN, NUM]
6027 return value_range_base (VR_RANGE, vrp_val_min (ttype, true), max ());
6028 // [NUM, SYM] -> [NUM, +MAX]
6029 return value_range_base (VR_RANGE, min (), vrp_val_max (ttype, true));
6031 gcc_checking_assert (kind () == VR_ANTI_RANGE);
6032 // ~[SYM, NUM] -> [NUM + 1, +MAX]
6035 if (!vrp_val_is_max (max ()))
6037 tree n = wide_int_to_tree (ttype, wi::to_wide (max ()) + 1);
6038 return value_range_base (VR_RANGE, n, vrp_val_max (ttype, true));
6040 value_range_base var;
6041 var.set_varying (ttype);
6044 // ~[NUM, SYM] -> [-MIN, NUM - 1]
6045 if (!vrp_val_is_min (min ()))
6047 tree n = wide_int_to_tree (ttype, wi::to_wide (min ()) - 1);
6048 return value_range_base (VR_RANGE, vrp_val_min (ttype, true), n);
6050 value_range_base var;
6051 var.set_varying (ttype);
6055 /* Return the number of sub-ranges in a range. */
6058 value_range_base::num_pairs () const
6065 return normalize_symbolics ().num_pairs ();
6066 if (m_kind == VR_ANTI_RANGE)
6068 // ~[MIN, X] has one sub-range of [X+1, MAX], and
6069 // ~[X, MAX] has one sub-range of [MIN, X-1].
6070 if (vrp_val_is_min (m_min, true) || vrp_val_is_max (m_max, true))
6077 /* Return the lower bound for a sub-range. PAIR is the sub-range in
6081 value_range_base::lower_bound (unsigned pair) const
6084 return normalize_symbolics ().lower_bound (pair);
6086 gcc_checking_assert (!undefined_p ());
6087 gcc_checking_assert (pair + 1 <= num_pairs ());
6089 if (m_kind == VR_ANTI_RANGE)
6092 if (pair == 1 || vrp_val_is_min (m_min, true))
6093 t = wide_int_to_tree (typ, wi::to_wide (m_max) + 1);
6095 t = vrp_val_min (typ, true);
6099 return wi::to_wide (t);
6102 /* Return the upper bound for a sub-range. PAIR is the sub-range in
6106 value_range_base::upper_bound (unsigned pair) const
6109 return normalize_symbolics ().upper_bound (pair);
6111 gcc_checking_assert (!undefined_p ());
6112 gcc_checking_assert (pair + 1 <= num_pairs ());
6114 if (m_kind == VR_ANTI_RANGE)
6117 if (pair == 1 || vrp_val_is_min (m_min, true))
6118 t = vrp_val_max (typ, true);
6120 t = wide_int_to_tree (typ, wi::to_wide (m_min) - 1);
6124 return wi::to_wide (t);
6127 /* Return the highest bound in a range. */
6130 value_range_base::upper_bound () const
6132 unsigned pairs = num_pairs ();
6133 gcc_checking_assert (pairs > 0);
6134 return upper_bound (pairs - 1);
6137 /* Return TRUE if range contains INTEGER_CST. */
6140 value_range_base::contains_p (tree cst) const
6142 gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST);
6144 return normalize_symbolics ().contains_p (cst);
6145 return value_inside_range (cst) == 1;
6148 /* Return the inverse of a range. */
6151 value_range_base::invert ()
6153 if (m_kind == VR_RANGE)
6154 m_kind = VR_ANTI_RANGE;
6155 else if (m_kind == VR_ANTI_RANGE)
6161 /* Range union, but for references. */
6164 value_range_base::union_ (const value_range_base &r)
6166 /* Disable details for now, because it makes the ranger dump
6167 unnecessarily verbose. */
6168 bool details = dump_flags & TDF_DETAILS;
6170 dump_flags &= ~TDF_DETAILS;
6173 dump_flags |= TDF_DETAILS;
6176 /* Range intersect, but for references. */
6179 value_range_base::intersect (const value_range_base &r)
6181 /* Disable details for now, because it makes the ranger dump
6182 unnecessarily verbose. */
6183 bool details = dump_flags & TDF_DETAILS;
6185 dump_flags &= ~TDF_DETAILS;
6188 dump_flags |= TDF_DETAILS;
6191 /* Return TRUE if two types are compatible for range operations. */
6194 range_compatible_p (tree t1, tree t2)
6196 if (POINTER_TYPE_P (t1) && POINTER_TYPE_P (t2))
6199 return types_compatible_p (t1, t2);
6203 value_range_base::operator== (const value_range_base &r) const
6206 return r.undefined_p ();
6208 if (num_pairs () != r.num_pairs ()
6209 || !range_compatible_p (type (), r.type ()))
6212 for (unsigned p = 0; p < num_pairs (); p++)
6213 if (wi::ne_p (lower_bound (p), r.lower_bound (p))
6214 || wi::ne_p (upper_bound (p), r.upper_bound (p)))
6220 /* Visit all arguments for PHI node PHI that flow through executable
6221 edges. If a valid value range can be derived from all the incoming
6222 value ranges, set a new range for the LHS of PHI. */
6224 enum ssa_prop_result
6225 vrp_prop::visit_phi (gphi *phi)
6227 tree lhs = PHI_RESULT (phi);
6228 value_range vr_result;
6229 extract_range_from_phi_node (phi, &vr_result);
6230 if (update_value_range (lhs, &vr_result))
6232 if (dump_file && (dump_flags & TDF_DETAILS))
6234 fprintf (dump_file, "Found new range for ");
6235 print_generic_expr (dump_file, lhs);
6236 fprintf (dump_file, ": ");
6237 dump_value_range (dump_file, &vr_result);
6238 fprintf (dump_file, "\n");
6241 if (vr_result.varying_p ())
6242 return SSA_PROP_VARYING;
6244 return SSA_PROP_INTERESTING;
6247 /* Nothing changed, don't add outgoing edges. */
6248 return SSA_PROP_NOT_INTERESTING;
6251 class vrp_folder : public substitute_and_fold_engine
6254 vrp_folder () : substitute_and_fold_engine (/* Fold all stmts. */ true) { }
6255 tree get_value (tree) FINAL OVERRIDE;
6256 bool fold_stmt (gimple_stmt_iterator *) FINAL OVERRIDE;
6257 bool fold_predicate_in (gimple_stmt_iterator *);
6259 class vr_values *vr_values;
6262 tree vrp_evaluate_conditional (tree_code code, tree op0,
6263 tree op1, gimple *stmt)
6264 { return vr_values->vrp_evaluate_conditional (code, op0, op1, stmt); }
6265 bool simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
6266 { return vr_values->simplify_stmt_using_ranges (gsi); }
6267 tree op_with_constant_singleton_value_range (tree op)
6268 { return vr_values->op_with_constant_singleton_value_range (op); }
6271 /* If the statement pointed by SI has a predicate whose value can be
6272 computed using the value range information computed by VRP, compute
6273 its value and return true. Otherwise, return false. */
6276 vrp_folder::fold_predicate_in (gimple_stmt_iterator *si)
6278 bool assignment_p = false;
6280 gimple *stmt = gsi_stmt (*si);
6282 if (is_gimple_assign (stmt)
6283 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
6285 assignment_p = true;
6286 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
6287 gimple_assign_rhs1 (stmt),
6288 gimple_assign_rhs2 (stmt),
6291 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
6292 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
6293 gimple_cond_lhs (cond_stmt),
6294 gimple_cond_rhs (cond_stmt),
6302 val = fold_convert (gimple_expr_type (stmt), val);
6306 fprintf (dump_file, "Folding predicate ");
6307 print_gimple_expr (dump_file, stmt, 0);
6308 fprintf (dump_file, " to ");
6309 print_generic_expr (dump_file, val);
6310 fprintf (dump_file, "\n");
6313 if (is_gimple_assign (stmt))
6314 gimple_assign_set_rhs_from_tree (si, val);
6317 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
6318 gcond *cond_stmt = as_a <gcond *> (stmt);
6319 if (integer_zerop (val))
6320 gimple_cond_make_false (cond_stmt);
6321 else if (integer_onep (val))
6322 gimple_cond_make_true (cond_stmt);
6333 /* Callback for substitute_and_fold folding the stmt at *SI. */
6336 vrp_folder::fold_stmt (gimple_stmt_iterator *si)
6338 if (fold_predicate_in (si))
6341 return simplify_stmt_using_ranges (si);
6344 /* If OP has a value range with a single constant value return that,
6345 otherwise return NULL_TREE. This returns OP itself if OP is a
6348 Implemented as a pure wrapper right now, but this will change. */
6351 vrp_folder::get_value (tree op)
6353 return op_with_constant_singleton_value_range (op);
6356 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
6357 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
6358 BB. If no such ASSERT_EXPR is found, return OP. */
6361 lhs_of_dominating_assert (tree op, basic_block bb, gimple *stmt)
6363 imm_use_iterator imm_iter;
6365 use_operand_p use_p;
6367 if (TREE_CODE (op) == SSA_NAME)
6369 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
6371 use_stmt = USE_STMT (use_p);
6372 if (use_stmt != stmt
6373 && gimple_assign_single_p (use_stmt)
6374 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
6375 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
6376 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
6377 return gimple_assign_lhs (use_stmt);
6384 static class vr_values *x_vr_values;
6386 /* A trivial wrapper so that we can present the generic jump threading
6387 code with a simple API for simplifying statements. STMT is the
6388 statement we want to simplify, WITHIN_STMT provides the location
6389 for any overflow warnings. */
6392 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
6393 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED,
6396 /* First see if the conditional is in the hash table. */
6397 tree cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, false, true);
6398 if (cached_lhs && is_gimple_min_invariant (cached_lhs))
6401 vr_values *vr_values = x_vr_values;
6402 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
6404 tree op0 = gimple_cond_lhs (cond_stmt);
6405 op0 = lhs_of_dominating_assert (op0, bb, stmt);
6407 tree op1 = gimple_cond_rhs (cond_stmt);
6408 op1 = lhs_of_dominating_assert (op1, bb, stmt);
6410 return vr_values->vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
6411 op0, op1, within_stmt);
6414 /* We simplify a switch statement by trying to determine which case label
6415 will be taken. If we are successful then we return the corresponding
6417 if (gswitch *switch_stmt = dyn_cast <gswitch *> (stmt))
6419 tree op = gimple_switch_index (switch_stmt);
6420 if (TREE_CODE (op) != SSA_NAME)
6423 op = lhs_of_dominating_assert (op, bb, stmt);
6425 const value_range *vr = vr_values->get_value_range (op);
6426 if (vr->undefined_p ()
6428 || vr->symbolic_p ())
6431 if (vr->kind () == VR_RANGE)
6434 /* Get the range of labels that contain a part of the operand's
6436 find_case_label_range (switch_stmt, vr->min (), vr->max (), &i, &j);
6438 /* Is there only one such label? */
6441 tree label = gimple_switch_label (switch_stmt, i);
6443 /* The i'th label will be taken only if the value range of the
6444 operand is entirely within the bounds of this label. */
6445 if (CASE_HIGH (label) != NULL_TREE
6446 ? (tree_int_cst_compare (CASE_LOW (label), vr->min ()) <= 0
6447 && tree_int_cst_compare (CASE_HIGH (label),
6449 : (tree_int_cst_equal (CASE_LOW (label), vr->min ())
6450 && tree_int_cst_equal (vr->min (), vr->max ())))
6454 /* If there are no such labels then the default label will be
6457 return gimple_switch_label (switch_stmt, 0);
6460 if (vr->kind () == VR_ANTI_RANGE)
6462 unsigned n = gimple_switch_num_labels (switch_stmt);
6463 tree min_label = gimple_switch_label (switch_stmt, 1);
6464 tree max_label = gimple_switch_label (switch_stmt, n - 1);
6466 /* The default label will be taken only if the anti-range of the
6467 operand is entirely outside the bounds of all the (non-default)
6469 if (tree_int_cst_compare (vr->min (), CASE_LOW (min_label)) <= 0
6470 && (CASE_HIGH (max_label) != NULL_TREE
6471 ? tree_int_cst_compare (vr->max (),
6472 CASE_HIGH (max_label)) >= 0
6473 : tree_int_cst_compare (vr->max (),
6474 CASE_LOW (max_label)) >= 0))
6475 return gimple_switch_label (switch_stmt, 0);
6481 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
6483 tree lhs = gimple_assign_lhs (assign_stmt);
6484 if (TREE_CODE (lhs) == SSA_NAME
6485 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6486 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6487 && stmt_interesting_for_vrp (stmt))
6492 vr_values->extract_range_from_stmt (stmt, &dummy_e,
6493 &dummy_tree, &new_vr);
6495 if (new_vr.singleton_p (&singleton))
6503 class vrp_dom_walker : public dom_walker
6506 vrp_dom_walker (cdi_direction direction,
6507 class const_and_copies *const_and_copies,
6508 class avail_exprs_stack *avail_exprs_stack)
6509 : dom_walker (direction, REACHABLE_BLOCKS),
6510 m_const_and_copies (const_and_copies),
6511 m_avail_exprs_stack (avail_exprs_stack),
6512 m_dummy_cond (NULL) {}
6514 virtual edge before_dom_children (basic_block);
6515 virtual void after_dom_children (basic_block);
6517 class vr_values *vr_values;
6520 class const_and_copies *m_const_and_copies;
6521 class avail_exprs_stack *m_avail_exprs_stack;
6523 gcond *m_dummy_cond;
6527 /* Called before processing dominator children of BB. We want to look
6528 at ASSERT_EXPRs and record information from them in the appropriate
6531 We could look at other statements here. It's not seen as likely
6532 to significantly increase the jump threads we discover. */
6535 vrp_dom_walker::before_dom_children (basic_block bb)
6537 gimple_stmt_iterator gsi;
6539 m_avail_exprs_stack->push_marker ();
6540 m_const_and_copies->push_marker ();
6541 for (gsi = gsi_start_nondebug_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6543 gimple *stmt = gsi_stmt (gsi);
6544 if (gimple_assign_single_p (stmt)
6545 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
6547 tree rhs1 = gimple_assign_rhs1 (stmt);
6548 tree cond = TREE_OPERAND (rhs1, 1);
6549 tree inverted = invert_truthvalue (cond);
6550 vec<cond_equivalence> p;
6552 record_conditions (&p, cond, inverted);
6553 for (unsigned int i = 0; i < p.length (); i++)
6554 m_avail_exprs_stack->record_cond (&p[i]);
6556 tree lhs = gimple_assign_lhs (stmt);
6557 m_const_and_copies->record_const_or_copy (lhs,
6558 TREE_OPERAND (rhs1, 0));
6567 /* Called after processing dominator children of BB. This is where we
6568 actually call into the threader. */
6570 vrp_dom_walker::after_dom_children (basic_block bb)
6573 m_dummy_cond = gimple_build_cond (NE_EXPR,
6574 integer_zero_node, integer_zero_node,
6577 x_vr_values = vr_values;
6578 thread_outgoing_edges (bb, m_dummy_cond, m_const_and_copies,
6579 m_avail_exprs_stack, NULL,
6580 simplify_stmt_for_jump_threading);
6583 m_avail_exprs_stack->pop_to_marker ();
6584 m_const_and_copies->pop_to_marker ();
6587 /* Blocks which have more than one predecessor and more than
6588 one successor present jump threading opportunities, i.e.,
6589 when the block is reached from a specific predecessor, we
6590 may be able to determine which of the outgoing edges will
6591 be traversed. When this optimization applies, we are able
6592 to avoid conditionals at runtime and we may expose secondary
6593 optimization opportunities.
6595 This routine is effectively a driver for the generic jump
6596 threading code. It basically just presents the generic code
6597 with edges that may be suitable for jump threading.
6599 Unlike DOM, we do not iterate VRP if jump threading was successful.
6600 While iterating may expose new opportunities for VRP, it is expected
6601 those opportunities would be very limited and the compile time cost
6602 to expose those opportunities would be significant.
6604 As jump threading opportunities are discovered, they are registered
6605 for later realization. */
6608 identify_jump_threads (class vr_values *vr_values)
6610 /* Ugh. When substituting values earlier in this pass we can
6611 wipe the dominance information. So rebuild the dominator
6612 information as we need it within the jump threading code. */
6613 calculate_dominance_info (CDI_DOMINATORS);
6615 /* We do not allow VRP information to be used for jump threading
6616 across a back edge in the CFG. Otherwise it becomes too
6617 difficult to avoid eliminating loop exit tests. Of course
6618 EDGE_DFS_BACK is not accurate at this time so we have to
6620 mark_dfs_back_edges ();
6622 /* Allocate our unwinder stack to unwind any temporary equivalences
6623 that might be recorded. */
6624 const_and_copies *equiv_stack = new const_and_copies ();
6626 hash_table<expr_elt_hasher> *avail_exprs
6627 = new hash_table<expr_elt_hasher> (1024);
6628 avail_exprs_stack *avail_exprs_stack
6629 = new class avail_exprs_stack (avail_exprs);
6631 vrp_dom_walker walker (CDI_DOMINATORS, equiv_stack, avail_exprs_stack);
6632 walker.vr_values = vr_values;
6633 walker.walk (cfun->cfg->x_entry_block_ptr);
6635 /* We do not actually update the CFG or SSA graphs at this point as
6636 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
6637 handle ASSERT_EXPRs gracefully. */
6640 delete avail_exprs_stack;
6643 /* Traverse all the blocks folding conditionals with known ranges. */
6646 vrp_prop::vrp_finalize (bool warn_array_bounds_p)
6650 /* We have completed propagating through the lattice. */
6651 vr_values.set_lattice_propagation_complete ();
6655 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
6656 vr_values.dump_all_value_ranges (dump_file);
6657 fprintf (dump_file, "\n");
6660 /* Set value range to non pointer SSA_NAMEs. */
6661 for (i = 0; i < num_ssa_names; i++)
6663 tree name = ssa_name (i);
6667 const value_range *vr = get_value_range (name);
6668 if (!name || !vr->constant_p ())
6671 if (POINTER_TYPE_P (TREE_TYPE (name))
6672 && range_includes_zero_p (vr) == 0)
6673 set_ptr_nonnull (name);
6674 else if (!POINTER_TYPE_P (TREE_TYPE (name)))
6675 set_range_info (name, *vr);
6678 /* If we're checking array refs, we want to merge information on
6679 the executability of each edge between vrp_folder and the
6680 check_array_bounds_dom_walker: each can clear the
6681 EDGE_EXECUTABLE flag on edges, in different ways.
6683 Hence, if we're going to call check_all_array_refs, set
6684 the flag on every edge now, rather than in
6685 check_array_bounds_dom_walker's ctor; vrp_folder may clear
6686 it from some edges. */
6687 if (warn_array_bounds && warn_array_bounds_p)
6688 set_all_edges_as_executable (cfun);
6690 class vrp_folder vrp_folder;
6691 vrp_folder.vr_values = &vr_values;
6692 vrp_folder.substitute_and_fold ();
6694 if (warn_array_bounds && warn_array_bounds_p)
6695 check_all_array_refs ();
6698 /* Main entry point to VRP (Value Range Propagation). This pass is
6699 loosely based on J. R. C. Patterson, ``Accurate Static Branch
6700 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
6701 Programming Language Design and Implementation, pp. 67-78, 1995.
6702 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
6704 This is essentially an SSA-CCP pass modified to deal with ranges
6705 instead of constants.
6707 While propagating ranges, we may find that two or more SSA name
6708 have equivalent, though distinct ranges. For instance,
6711 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
6713 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
6717 In the code above, pointer p_5 has range [q_2, q_2], but from the
6718 code we can also determine that p_5 cannot be NULL and, if q_2 had
6719 a non-varying range, p_5's range should also be compatible with it.
6721 These equivalences are created by two expressions: ASSERT_EXPR and
6722 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
6723 result of another assertion, then we can use the fact that p_5 and
6724 p_4 are equivalent when evaluating p_5's range.
6726 Together with value ranges, we also propagate these equivalences
6727 between names so that we can take advantage of information from
6728 multiple ranges when doing final replacement. Note that this
6729 equivalency relation is transitive but not symmetric.
6731 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
6732 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
6733 in contexts where that assertion does not hold (e.g., in line 6).
6735 TODO, the main difference between this pass and Patterson's is that
6736 we do not propagate edge probabilities. We only compute whether
6737 edges can be taken or not. That is, instead of having a spectrum
6738 of jump probabilities between 0 and 1, we only deal with 0, 1 and
6739 DON'T KNOW. In the future, it may be worthwhile to propagate
6740 probabilities to aid branch prediction. */
6743 execute_vrp (bool warn_array_bounds_p)
6746 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
6747 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
6750 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
6751 Inserting assertions may split edges which will invalidate
6753 insert_range_assertions ();
6755 threadedge_initialize_values ();
6757 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
6758 mark_dfs_back_edges ();
6760 class vrp_prop vrp_prop;
6761 vrp_prop.vrp_initialize ();
6762 vrp_prop.ssa_propagate ();
6763 vrp_prop.vrp_finalize (warn_array_bounds_p);
6765 /* We must identify jump threading opportunities before we release
6766 the datastructures built by VRP. */
6767 identify_jump_threads (&vrp_prop.vr_values);
6769 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
6770 was set by a type conversion can often be rewritten to use the
6771 RHS of the type conversion.
6773 However, doing so inhibits jump threading through the comparison.
6774 So that transformation is not performed until after jump threading
6777 FOR_EACH_BB_FN (bb, cfun)
6779 gimple *last = last_stmt (bb);
6780 if (last && gimple_code (last) == GIMPLE_COND)
6781 vrp_prop.vr_values.simplify_cond_using_ranges_2 (as_a <gcond *> (last));
6784 free_numbers_of_iterations_estimates (cfun);
6786 /* ASSERT_EXPRs must be removed before finalizing jump threads
6787 as finalizing jump threads calls the CFG cleanup code which
6788 does not properly handle ASSERT_EXPRs. */
6789 remove_range_assertions ();
6791 /* If we exposed any new variables, go ahead and put them into
6792 SSA form now, before we handle jump threading. This simplifies
6793 interactions between rewriting of _DECL nodes into SSA form
6794 and rewriting SSA_NAME nodes into SSA form after block
6795 duplication and CFG manipulation. */
6796 update_ssa (TODO_update_ssa);
6798 /* We identified all the jump threading opportunities earlier, but could
6799 not transform the CFG at that time. This routine transforms the
6800 CFG and arranges for the dominator tree to be rebuilt if necessary.
6802 Note the SSA graph update will occur during the normal TODO
6803 processing by the pass manager. */
6804 thread_through_all_blocks (false);
6806 vrp_prop.vr_values.cleanup_edges_and_switches ();
6807 threadedge_finalize_values ();
6810 loop_optimizer_finalize ();
6816 const pass_data pass_data_vrp =
6818 GIMPLE_PASS, /* type */
6820 OPTGROUP_NONE, /* optinfo_flags */
6821 TV_TREE_VRP, /* tv_id */
6822 PROP_ssa, /* properties_required */
6823 0, /* properties_provided */
6824 0, /* properties_destroyed */
6825 0, /* todo_flags_start */
6826 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
6829 class pass_vrp : public gimple_opt_pass
6832 pass_vrp (gcc::context *ctxt)
6833 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
6836 /* opt_pass methods: */
6837 opt_pass * clone () { return new pass_vrp (m_ctxt); }
6838 void set_pass_param (unsigned int n, bool param)
6840 gcc_assert (n == 0);
6841 warn_array_bounds_p = param;
6843 virtual bool gate (function *) { return flag_tree_vrp != 0; }
6844 virtual unsigned int execute (function *)
6845 { return execute_vrp (warn_array_bounds_p); }
6848 bool warn_array_bounds_p;
6849 }; // class pass_vrp
6854 make_pass_vrp (gcc::context *ctxt)
6856 return new pass_vrp (ctxt);
6860 /* Worker for determine_value_range. */
6863 determine_value_range_1 (value_range_base *vr, tree expr)
6865 if (BINARY_CLASS_P (expr))
6867 value_range_base vr0, vr1;
6868 determine_value_range_1 (&vr0, TREE_OPERAND (expr, 0));
6869 determine_value_range_1 (&vr1, TREE_OPERAND (expr, 1));
6870 range_fold_binary_expr (vr, TREE_CODE (expr), TREE_TYPE (expr),
6873 else if (UNARY_CLASS_P (expr))
6875 value_range_base vr0;
6876 determine_value_range_1 (&vr0, TREE_OPERAND (expr, 0));
6877 range_fold_unary_expr (vr, TREE_CODE (expr), TREE_TYPE (expr),
6878 &vr0, TREE_TYPE (TREE_OPERAND (expr, 0)));
6880 else if (TREE_CODE (expr) == INTEGER_CST)
6884 value_range_kind kind;
6886 /* For SSA names try to extract range info computed by VRP. Otherwise
6887 fall back to varying. */
6888 if (TREE_CODE (expr) == SSA_NAME
6889 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
6890 && (kind = get_range_info (expr, &min, &max)) != VR_VARYING)
6891 vr->set (kind, wide_int_to_tree (TREE_TYPE (expr), min),
6892 wide_int_to_tree (TREE_TYPE (expr), max));
6894 vr->set_varying (TREE_TYPE (expr));
6898 /* Compute a value-range for EXPR and set it in *MIN and *MAX. Return
6899 the determined range type. */
6902 determine_value_range (tree expr, wide_int *min, wide_int *max)
6904 value_range_base vr;
6905 determine_value_range_1 (&vr, expr);
6906 if (vr.constant_p ())
6908 *min = wi::to_wide (vr.min ());
6909 *max = wi::to_wide (vr.max ());