1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2018 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
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"
70 #include "wide-int-range.h"
72 /* Set of SSA names found live during the RPO traversal of the function
73 for still active basic-blocks. */
77 value_range_base::set (enum value_range_kind kind
, tree min
, tree max
)
87 value_range::set_equiv (bitmap equiv
)
89 /* Since updating the equivalence set involves deep copying the
90 bitmaps, only do it if absolutely necessary.
92 All equivalence bitmaps are allocated from the same obstack. So
93 we can use the obstack associated with EQUIV to allocate vr->equiv. */
96 m_equiv
= BITMAP_ALLOC (equiv
->obstack
);
100 if (equiv
&& !bitmap_empty_p (equiv
))
101 bitmap_copy (m_equiv
, equiv
);
103 bitmap_clear (m_equiv
);
107 /* Initialize value_range. */
110 value_range::set (enum value_range_kind kind
, tree min
, tree max
,
113 value_range_base::set (kind
, min
, max
);
119 value_range_base::value_range_base (value_range_kind kind
, tree min
, tree max
)
121 set (kind
, min
, max
);
124 value_range::value_range (value_range_kind kind
, tree min
, tree max
,
128 set (kind
, min
, max
, equiv
);
131 value_range::value_range (const value_range_base
&other
)
134 set (other
.kind (), other
.min(), other
.max (), NULL
);
137 /* Like above, but keep the equivalences intact. */
140 value_range::update (value_range_kind kind
, tree min
, tree max
)
142 set (kind
, min
, max
, m_equiv
);
145 /* Copy value_range in FROM into THIS while avoiding bitmap sharing.
147 Note: The code that avoids the bitmap sharing looks at the existing
148 this->m_equiv, so this function cannot be used to initalize an
149 object. Use the constructors for initialization. */
152 value_range::deep_copy (const value_range
*from
)
154 set (from
->m_kind
, from
->min (), from
->max (), from
->m_equiv
);
157 /* Check the validity of the range. */
160 value_range_base::check ()
169 gcc_assert (m_min
&& m_max
);
171 gcc_assert (!TREE_OVERFLOW_P (m_min
) && !TREE_OVERFLOW_P (m_max
));
173 /* Creating ~[-MIN, +MAX] is stupid because that would be
175 if (INTEGRAL_TYPE_P (TREE_TYPE (m_min
)) && m_kind
== VR_ANTI_RANGE
)
176 gcc_assert (!vrp_val_is_min (m_min
) || !vrp_val_is_max (m_max
));
178 cmp
= compare_values (m_min
, m_max
);
179 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
184 gcc_assert (!min () && !max ());
192 value_range::check ()
194 value_range_base::check ();
199 gcc_assert (!m_equiv
|| bitmap_empty_p (m_equiv
));
204 /* Returns TRUE if THIS == OTHER. Ignores the equivalence bitmap if
205 IGNORE_EQUIVS is TRUE. */
208 value_range::equal_p (const value_range
&other
, bool ignore_equivs
) const
210 return (ignore_equivs_equal_p (other
)
212 || vrp_bitmap_equal_p (m_equiv
, other
.m_equiv
)));
215 /* Return equality while ignoring equivalence bitmap. */
218 value_range_base::ignore_equivs_equal_p (const value_range_base
&other
) const
220 return (m_kind
== other
.m_kind
221 && vrp_operand_equal_p (m_min
, other
.m_min
)
222 && vrp_operand_equal_p (m_max
, other
.m_max
));
226 value_range::operator== (const value_range
&other
) const
228 return equal_p (other
, /*ignore_equivs=*/false);
232 value_range::operator!= (const value_range
&other
) const
234 return !(*this == other
);
237 /* Return TRUE if this is a symbolic range. */
240 value_range::symbolic_p () const
242 return (!varying_p ()
244 && (!is_gimple_min_invariant (m_min
)
245 || !is_gimple_min_invariant (m_max
)));
248 /* NOTE: This is not the inverse of symbolic_p because the range
249 could also be varying or undefined. Ideally they should be inverse
250 of each other, with varying only applying to symbolics. Varying of
251 constants would be represented as [-MIN, +MAX]. */
254 value_range::constant_p () const
256 return (!varying_p ()
258 && TREE_CODE (m_min
) == INTEGER_CST
259 && TREE_CODE (m_max
) == INTEGER_CST
);
263 value_range_base::set_undefined ()
265 *this = value_range_base (VR_UNDEFINED
, NULL
, NULL
);
269 value_range::set_undefined ()
272 *this = value_range (VR_UNDEFINED
, NULL
, NULL
, NULL
);
276 value_range_base::set_varying ()
278 *this = value_range_base (VR_VARYING
, NULL
, NULL
);
282 value_range::set_varying ()
285 *this = value_range (VR_VARYING
, NULL
, NULL
, NULL
);
288 /* Return TRUE if it is possible that range contains VAL. */
291 value_range_base::may_contain_p (tree val
) const
299 if (m_kind
== VR_ANTI_RANGE
)
301 int res
= value_inside_range (val
, min (), max ());
302 return res
== 0 || res
== -2;
304 return value_inside_range (val
, min (), max ()) != 0;
308 value_range::equiv_clear ()
311 bitmap_clear (m_equiv
);
314 /* Add VAR and VAR's equivalence set (VAR_VR) to the equivalence
315 bitmap. If no equivalence table has been created, OBSTACK is the
316 obstack to use (NULL for the default obstack).
318 This is the central point where equivalence processing can be
322 value_range::equiv_add (const_tree var
,
323 const value_range
*var_vr
,
324 bitmap_obstack
*obstack
)
327 m_equiv
= BITMAP_ALLOC (obstack
);
328 unsigned ver
= SSA_NAME_VERSION (var
);
329 bitmap_set_bit (m_equiv
, ver
);
330 if (var_vr
&& var_vr
->m_equiv
)
331 bitmap_ior_into (m_equiv
, var_vr
->m_equiv
);
334 /* If range is a singleton, place it in RESULT and return TRUE.
335 Note: A singleton can be any gimple invariant, not just constants.
336 So, [&x, &x] counts as a singleton. */
339 value_range::singleton_p (tree
*result
) const
341 if (m_kind
== VR_RANGE
342 && vrp_operand_equal_p (min (), max ())
343 && is_gimple_min_invariant (min ()))
353 value_range_base::type () const
355 /* Types are only valid for VR_RANGE and VR_ANTI_RANGE, which are
356 known to have non-zero min/max. */
358 return TREE_TYPE (min ());
361 /* Dump value range to FILE. */
364 value_range_base::dump (FILE *file
) const
367 fprintf (file
, "UNDEFINED");
368 else if (m_kind
== VR_RANGE
|| m_kind
== VR_ANTI_RANGE
)
370 tree type
= TREE_TYPE (min ());
372 fprintf (file
, "%s[", (m_kind
== VR_ANTI_RANGE
) ? "~" : "");
374 if (INTEGRAL_TYPE_P (type
)
375 && !TYPE_UNSIGNED (type
)
376 && vrp_val_is_min (min ()))
377 fprintf (file
, "-INF");
379 print_generic_expr (file
, min ());
381 fprintf (file
, ", ");
383 if (INTEGRAL_TYPE_P (type
)
384 && vrp_val_is_max (max ()))
385 fprintf (file
, "+INF");
387 print_generic_expr (file
, max ());
391 else if (varying_p ())
392 fprintf (file
, "VARYING");
394 fprintf (file
, "INVALID RANGE");
398 value_range::dump (FILE *file
) const
400 value_range_base::dump (file
);
401 if ((m_kind
== VR_RANGE
|| m_kind
== VR_ANTI_RANGE
)
407 fprintf (file
, " EQUIVALENCES: { ");
409 EXECUTE_IF_SET_IN_BITMAP (m_equiv
, 0, i
, bi
)
411 print_generic_expr (file
, ssa_name (i
));
416 fprintf (file
, "} (%u elements)", c
);
421 value_range::dump () const
423 dump_value_range (stderr
, this);
424 fprintf (stderr
, "\n");
427 /* Return true if the SSA name NAME is live on the edge E. */
430 live_on_edge (edge e
, tree name
)
432 return (live
[e
->dest
->index
]
433 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
436 /* Location information for ASSERT_EXPRs. Each instance of this
437 structure describes an ASSERT_EXPR for an SSA name. Since a single
438 SSA name may have more than one assertion associated with it, these
439 locations are kept in a linked list attached to the corresponding
443 /* Basic block where the assertion would be inserted. */
446 /* Some assertions need to be inserted on an edge (e.g., assertions
447 generated by COND_EXPRs). In those cases, BB will be NULL. */
450 /* Pointer to the statement that generated this assertion. */
451 gimple_stmt_iterator si
;
453 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
454 enum tree_code comp_code
;
456 /* Value being compared against. */
459 /* Expression to compare. */
462 /* Next node in the linked list. */
466 /* If bit I is present, it means that SSA name N_i has a list of
467 assertions that should be inserted in the IL. */
468 static bitmap need_assert_for
;
470 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
471 holds a list of ASSERT_LOCUS_T nodes that describe where
472 ASSERT_EXPRs for SSA name N_I should be inserted. */
473 static assert_locus
**asserts_for
;
475 /* Return the maximum value for TYPE. */
478 vrp_val_max (const_tree type
)
480 if (!INTEGRAL_TYPE_P (type
))
483 return TYPE_MAX_VALUE (type
);
486 /* Return the minimum value for TYPE. */
489 vrp_val_min (const_tree type
)
491 if (!INTEGRAL_TYPE_P (type
))
494 return TYPE_MIN_VALUE (type
);
497 /* Return whether VAL is equal to the maximum value of its type.
498 We can't do a simple equality comparison with TYPE_MAX_VALUE because
499 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
500 is not == to the integer constant with the same value in the type. */
503 vrp_val_is_max (const_tree val
)
505 tree type_max
= vrp_val_max (TREE_TYPE (val
));
506 return (val
== type_max
507 || (type_max
!= NULL_TREE
508 && operand_equal_p (val
, type_max
, 0)));
511 /* Return whether VAL is equal to the minimum value of its type. */
514 vrp_val_is_min (const_tree val
)
516 tree type_min
= vrp_val_min (TREE_TYPE (val
));
517 return (val
== type_min
518 || (type_min
!= NULL_TREE
519 && operand_equal_p (val
, type_min
, 0)));
522 /* VR_TYPE describes a range with mininum value *MIN and maximum
523 value *MAX. Restrict the range to the set of values that have
524 no bits set outside NONZERO_BITS. Update *MIN and *MAX and
525 return the new range type.
527 SGN gives the sign of the values described by the range. */
529 enum value_range_kind
530 intersect_range_with_nonzero_bits (enum value_range_kind vr_type
,
531 wide_int
*min
, wide_int
*max
,
532 const wide_int
&nonzero_bits
,
535 if (vr_type
== VR_ANTI_RANGE
)
537 /* The VR_ANTI_RANGE is equivalent to the union of the ranges
538 A: [-INF, *MIN) and B: (*MAX, +INF]. First use NONZERO_BITS
539 to create an inclusive upper bound for A and an inclusive lower
541 wide_int a_max
= wi::round_down_for_mask (*min
- 1, nonzero_bits
);
542 wide_int b_min
= wi::round_up_for_mask (*max
+ 1, nonzero_bits
);
544 /* If the calculation of A_MAX wrapped, A is effectively empty
545 and A_MAX is the highest value that satisfies NONZERO_BITS.
546 Likewise if the calculation of B_MIN wrapped, B is effectively
547 empty and B_MIN is the lowest value that satisfies NONZERO_BITS. */
548 bool a_empty
= wi::ge_p (a_max
, *min
, sgn
);
549 bool b_empty
= wi::le_p (b_min
, *max
, sgn
);
551 /* If both A and B are empty, there are no valid values. */
552 if (a_empty
&& b_empty
)
555 /* If exactly one of A or B is empty, return a VR_RANGE for the
557 if (a_empty
|| b_empty
)
561 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
565 /* Update the VR_ANTI_RANGE bounds. */
568 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
570 /* Now check whether the excluded range includes any values that
571 satisfy NONZERO_BITS. If not, switch to a full VR_RANGE. */
572 if (wi::round_up_for_mask (*min
, nonzero_bits
) == b_min
)
574 unsigned int precision
= min
->get_precision ();
575 *min
= wi::min_value (precision
, sgn
);
576 *max
= wi::max_value (precision
, sgn
);
580 if (vr_type
== VR_RANGE
)
582 *max
= wi::round_down_for_mask (*max
, nonzero_bits
);
584 /* Check that the range contains at least one valid value. */
585 if (wi::gt_p (*min
, *max
, sgn
))
588 *min
= wi::round_up_for_mask (*min
, nonzero_bits
);
589 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
594 /* Set value range VR to VR_UNDEFINED. */
597 set_value_range_to_undefined (value_range
*vr
)
599 vr
->set_undefined ();
602 /* Set value range VR to VR_VARYING. */
605 set_value_range_to_varying (value_range
*vr
)
610 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
613 set_value_range (value_range
*vr
, enum value_range_kind kind
,
614 tree min
, tree max
, bitmap equiv
)
616 *vr
= value_range (kind
, min
, max
, equiv
);
620 /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
621 This means adjusting VRTYPE, MIN and MAX representing the case of a
622 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
623 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
624 In corner cases where MAX+1 or MIN-1 wraps this will fall back
626 This routine exists to ease canonicalization in the case where we
627 extract ranges from var + CST op limit. */
630 value_range_base::set_and_canonicalize (enum value_range_kind kind
,
633 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
634 if (kind
== VR_UNDEFINED
)
639 else if (kind
== VR_VARYING
)
645 /* Nothing to canonicalize for symbolic ranges. */
646 if (TREE_CODE (min
) != INTEGER_CST
647 || TREE_CODE (max
) != INTEGER_CST
)
649 set (kind
, min
, max
);
653 /* Wrong order for min and max, to swap them and the VR type we need
655 if (tree_int_cst_lt (max
, min
))
659 /* For one bit precision if max < min, then the swapped
660 range covers all values, so for VR_RANGE it is varying and
661 for VR_ANTI_RANGE empty range, so drop to varying as well. */
662 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
668 one
= build_int_cst (TREE_TYPE (min
), 1);
669 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
670 max
= int_const_binop (MINUS_EXPR
, min
, one
);
673 /* There's one corner case, if we had [C+1, C] before we now have
674 that again. But this represents an empty value range, so drop
675 to varying in this case. */
676 if (tree_int_cst_lt (max
, min
))
682 kind
= kind
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
685 /* Anti-ranges that can be represented as ranges should be so. */
686 if (kind
== VR_ANTI_RANGE
)
688 /* For -fstrict-enums we may receive out-of-range ranges so consider
689 values < -INF and values > INF as -INF/INF as well. */
690 tree type
= TREE_TYPE (min
);
691 bool is_min
= (INTEGRAL_TYPE_P (type
)
692 && tree_int_cst_compare (min
, TYPE_MIN_VALUE (type
)) <= 0);
693 bool is_max
= (INTEGRAL_TYPE_P (type
)
694 && tree_int_cst_compare (max
, TYPE_MAX_VALUE (type
)) >= 0);
696 if (is_min
&& is_max
)
698 /* We cannot deal with empty ranges, drop to varying.
699 ??? This could be VR_UNDEFINED instead. */
703 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
704 && (is_min
|| is_max
))
706 /* Non-empty boolean ranges can always be represented
707 as a singleton range. */
709 min
= max
= vrp_val_max (TREE_TYPE (min
));
711 min
= max
= vrp_val_min (TREE_TYPE (min
));
715 /* As a special exception preserve non-null ranges. */
716 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
717 && integer_zerop (max
)))
719 tree one
= build_int_cst (TREE_TYPE (max
), 1);
720 min
= int_const_binop (PLUS_EXPR
, max
, one
);
721 max
= vrp_val_max (TREE_TYPE (max
));
726 tree one
= build_int_cst (TREE_TYPE (min
), 1);
727 max
= int_const_binop (MINUS_EXPR
, min
, one
);
728 min
= vrp_val_min (TREE_TYPE (min
));
733 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
734 to make sure VRP iteration terminates, otherwise we can get into
737 set (kind
, min
, max
);
741 value_range::set_and_canonicalize (enum value_range_kind kind
,
742 tree min
, tree max
, bitmap equiv
)
744 value_range_base::set_and_canonicalize (kind
, min
, max
);
745 if (this->kind () == VR_RANGE
|| this->kind () == VR_ANTI_RANGE
)
751 /* Set value range VR to a single value. This function is only called
752 with values we get from statements, and exists to clear the
753 TREE_OVERFLOW flag. */
756 set_value_range_to_value (value_range
*vr
, tree val
, bitmap equiv
)
758 gcc_assert (is_gimple_min_invariant (val
));
759 if (TREE_OVERFLOW_P (val
))
760 val
= drop_tree_overflow (val
);
761 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
764 /* Set value range VR to a non-NULL range of type TYPE. */
767 set_value_range_to_nonnull (value_range
*vr
, tree type
)
769 tree zero
= build_int_cst (type
, 0);
770 vr
->update (VR_ANTI_RANGE
, zero
, zero
);
774 /* Set value range VR to a NULL range of type TYPE. */
777 set_value_range_to_null (value_range
*vr
, tree type
)
779 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv ());
782 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
785 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
789 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
794 /* Return true, if the bitmaps B1 and B2 are equal. */
797 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
800 || ((!b1
|| bitmap_empty_p (b1
))
801 && (!b2
|| bitmap_empty_p (b2
)))
803 && bitmap_equal_p (b1
, b2
)));
806 /* Return true if VR is [0, 0]. */
809 range_is_null (const value_range
*vr
)
811 return vr
->zero_p ();
815 range_is_nonnull (const value_range
*vr
)
817 return (vr
->kind () == VR_ANTI_RANGE
818 && vr
->min () == vr
->max ()
819 && integer_zerop (vr
->min ()));
822 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
826 range_int_cst_p (const value_range
*vr
)
828 return (vr
->kind () == VR_RANGE
829 && TREE_CODE (vr
->min ()) == INTEGER_CST
830 && TREE_CODE (vr
->max ()) == INTEGER_CST
);
833 /* Return true if VR is a INTEGER_CST singleton. */
836 range_int_cst_singleton_p (const value_range
*vr
)
838 return (range_int_cst_p (vr
)
839 && tree_int_cst_equal (vr
->min (), vr
->max ()));
842 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
843 otherwise. We only handle additive operations and set NEG to true if the
844 symbol is negated and INV to the invariant part, if any. */
847 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
855 if (TREE_CODE (t
) == PLUS_EXPR
856 || TREE_CODE (t
) == POINTER_PLUS_EXPR
857 || TREE_CODE (t
) == MINUS_EXPR
)
859 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
861 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
862 inv_
= TREE_OPERAND (t
, 0);
863 t
= TREE_OPERAND (t
, 1);
865 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
868 inv_
= TREE_OPERAND (t
, 1);
869 t
= TREE_OPERAND (t
, 0);
880 if (TREE_CODE (t
) == NEGATE_EXPR
)
882 t
= TREE_OPERAND (t
, 0);
886 if (TREE_CODE (t
) != SSA_NAME
)
889 if (inv_
&& TREE_OVERFLOW_P (inv_
))
890 inv_
= drop_tree_overflow (inv_
);
897 /* The reverse operation: build a symbolic expression with TYPE
898 from symbol SYM, negated according to NEG, and invariant INV. */
901 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
903 const bool pointer_p
= POINTER_TYPE_P (type
);
907 t
= build1 (NEGATE_EXPR
, type
, t
);
909 if (integer_zerop (inv
))
912 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
918 -2 if those are incomparable. */
920 operand_less_p (tree val
, tree val2
)
922 /* LT is folded faster than GE and others. Inline the common case. */
923 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
924 return tree_int_cst_lt (val
, val2
);
929 fold_defer_overflow_warnings ();
931 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
933 fold_undefer_and_ignore_overflow_warnings ();
936 || TREE_CODE (tcmp
) != INTEGER_CST
)
939 if (!integer_zerop (tcmp
))
946 /* Compare two values VAL1 and VAL2. Return
948 -2 if VAL1 and VAL2 cannot be compared at compile-time,
951 +1 if VAL1 > VAL2, and
954 This is similar to tree_int_cst_compare but supports pointer values
955 and values that cannot be compared at compile time.
957 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
958 true if the return value is only valid if we assume that signed
959 overflow is undefined. */
962 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
967 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
969 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
970 == POINTER_TYPE_P (TREE_TYPE (val2
)));
972 /* Convert the two values into the same type. This is needed because
973 sizetype causes sign extension even for unsigned types. */
974 val2
= fold_convert (TREE_TYPE (val1
), val2
);
975 STRIP_USELESS_TYPE_CONVERSION (val2
);
977 const bool overflow_undefined
978 = INTEGRAL_TYPE_P (TREE_TYPE (val1
))
979 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
));
982 tree sym1
= get_single_symbol (val1
, &neg1
, &inv1
);
983 tree sym2
= get_single_symbol (val2
, &neg2
, &inv2
);
985 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
986 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
989 /* Both values must use the same name with the same sign. */
990 if (sym1
!= sym2
|| neg1
!= neg2
)
993 /* [-]NAME + CST == [-]NAME + CST. */
997 /* If overflow is defined we cannot simplify more. */
998 if (!overflow_undefined
)
1001 if (strict_overflow_p
!= NULL
1002 /* Symbolic range building sets TREE_NO_WARNING to declare
1003 that overflow doesn't happen. */
1004 && (!inv1
|| !TREE_NO_WARNING (val1
))
1005 && (!inv2
|| !TREE_NO_WARNING (val2
)))
1006 *strict_overflow_p
= true;
1009 inv1
= build_int_cst (TREE_TYPE (val1
), 0);
1011 inv2
= build_int_cst (TREE_TYPE (val2
), 0);
1013 return wi::cmp (wi::to_wide (inv1
), wi::to_wide (inv2
),
1014 TYPE_SIGN (TREE_TYPE (val1
)));
1017 const bool cst1
= is_gimple_min_invariant (val1
);
1018 const bool cst2
= is_gimple_min_invariant (val2
);
1020 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1021 it might be possible to say something depending on the constants. */
1022 if ((sym1
&& inv1
&& cst2
) || (sym2
&& inv2
&& cst1
))
1024 if (!overflow_undefined
)
1027 if (strict_overflow_p
!= NULL
1028 /* Symbolic range building sets TREE_NO_WARNING to declare
1029 that overflow doesn't happen. */
1030 && (!sym1
|| !TREE_NO_WARNING (val1
))
1031 && (!sym2
|| !TREE_NO_WARNING (val2
)))
1032 *strict_overflow_p
= true;
1034 const signop sgn
= TYPE_SIGN (TREE_TYPE (val1
));
1035 tree cst
= cst1
? val1
: val2
;
1036 tree inv
= cst1
? inv2
: inv1
;
1038 /* Compute the difference between the constants. If it overflows or
1039 underflows, this means that we can trivially compare the NAME with
1040 it and, consequently, the two values with each other. */
1041 wide_int diff
= wi::to_wide (cst
) - wi::to_wide (inv
);
1042 if (wi::cmp (0, wi::to_wide (inv
), sgn
)
1043 != wi::cmp (diff
, wi::to_wide (cst
), sgn
))
1045 const int res
= wi::cmp (wi::to_wide (cst
), wi::to_wide (inv
), sgn
);
1046 return cst1
? res
: -res
;
1052 /* We cannot say anything more for non-constants. */
1056 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1058 /* We cannot compare overflowed values. */
1059 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1062 if (TREE_CODE (val1
) == INTEGER_CST
1063 && TREE_CODE (val2
) == INTEGER_CST
)
1064 return tree_int_cst_compare (val1
, val2
);
1066 if (poly_int_tree_p (val1
) && poly_int_tree_p (val2
))
1068 if (known_eq (wi::to_poly_widest (val1
),
1069 wi::to_poly_widest (val2
)))
1071 if (known_lt (wi::to_poly_widest (val1
),
1072 wi::to_poly_widest (val2
)))
1074 if (known_gt (wi::to_poly_widest (val1
),
1075 wi::to_poly_widest (val2
)))
1085 /* First see if VAL1 and VAL2 are not the same. */
1086 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1089 /* If VAL1 is a lower address than VAL2, return -1. */
1090 if (operand_less_p (val1
, val2
) == 1)
1093 /* If VAL1 is a higher address than VAL2, return +1. */
1094 if (operand_less_p (val2
, val1
) == 1)
1097 /* If VAL1 is different than VAL2, return +2.
1098 For integer constants we either have already returned -1 or 1
1099 or they are equivalent. We still might succeed in proving
1100 something about non-trivial operands. */
1101 if (TREE_CODE (val1
) != INTEGER_CST
1102 || TREE_CODE (val2
) != INTEGER_CST
)
1104 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1105 if (t
&& integer_onep (t
))
1113 /* Compare values like compare_values_warnv. */
1116 compare_values (tree val1
, tree val2
)
1119 return compare_values_warnv (val1
, val2
, &sop
);
1123 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1124 0 if VAL is not inside [MIN, MAX],
1125 -2 if we cannot tell either way.
1127 Benchmark compile/20001226-1.c compilation time after changing this
1131 value_inside_range (tree val
, tree min
, tree max
)
1135 cmp1
= operand_less_p (val
, min
);
1141 cmp2
= operand_less_p (max
, val
);
1149 /* Return TRUE if *VR includes the value zero. */
1152 range_includes_zero_p (const value_range_base
*vr
)
1154 if (vr
->varying_p () || vr
->undefined_p ())
1156 tree zero
= build_int_cst (vr
->type (), 0);
1157 return vr
->may_contain_p (zero
);
1160 /* If *VR has a value range that is a single constant value return that,
1161 otherwise return NULL_TREE.
1163 ?? This actually returns TRUE for [&x, &x], so perhaps "constant"
1164 is not the best name. */
1167 value_range_constant_singleton (const value_range
*vr
)
1170 if (vr
->singleton_p (&result
))
1175 /* Value range wrapper for wide_int_range_set_zero_nonzero_bits.
1177 Compute MAY_BE_NONZERO and MUST_BE_NONZERO bit masks for range in VR.
1179 Return TRUE if VR was a constant range and we were able to compute
1183 vrp_set_zero_nonzero_bits (const tree expr_type
,
1184 const value_range
*vr
,
1185 wide_int
*may_be_nonzero
,
1186 wide_int
*must_be_nonzero
)
1188 if (!range_int_cst_p (vr
))
1190 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1191 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1194 wide_int_range_set_zero_nonzero_bits (TYPE_SIGN (expr_type
),
1195 wi::to_wide (vr
->min ()),
1196 wi::to_wide (vr
->max ()),
1197 *may_be_nonzero
, *must_be_nonzero
);
1201 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1202 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1203 false otherwise. If *AR can be represented with a single range
1204 *VR1 will be VR_UNDEFINED. */
1207 ranges_from_anti_range (const value_range
*ar
,
1208 value_range
*vr0
, value_range
*vr1
)
1210 tree type
= ar
->type ();
1212 vr0
->set_undefined ();
1213 vr1
->set_undefined ();
1215 /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U
1216 [A+1, +INF]. Not sure if this helps in practice, though. */
1218 if (ar
->kind () != VR_ANTI_RANGE
1219 || TREE_CODE (ar
->min ()) != INTEGER_CST
1220 || TREE_CODE (ar
->max ()) != INTEGER_CST
1221 || !vrp_val_min (type
)
1222 || !vrp_val_max (type
))
1225 if (!vrp_val_is_min (ar
->min ()))
1226 *vr0
= value_range (VR_RANGE
,
1228 wide_int_to_tree (type
, wi::to_wide (ar
->min ()) - 1));
1229 if (!vrp_val_is_max (ar
->max ()))
1230 *vr1
= value_range (VR_RANGE
,
1231 wide_int_to_tree (type
, wi::to_wide (ar
->max ()) + 1),
1232 vrp_val_max (type
));
1233 if (vr0
->undefined_p ())
1236 vr1
->set_undefined ();
1239 return !vr0
->undefined_p ();
1242 /* Extract the components of a value range into a pair of wide ints in
1245 If the value range is anything but a VR_*RANGE of constants, the
1246 resulting wide ints are set to [-MIN, +MAX] for the type. */
1249 extract_range_into_wide_ints (const value_range
*vr
,
1250 signop sign
, unsigned prec
,
1251 wide_int
&wmin
, wide_int
&wmax
)
1253 gcc_assert (vr
->kind () != VR_ANTI_RANGE
|| vr
->symbolic_p ());
1254 if (range_int_cst_p (vr
))
1256 wmin
= wi::to_wide (vr
->min ());
1257 wmax
= wi::to_wide (vr
->max ());
1261 wmin
= wi::min_value (prec
, sign
);
1262 wmax
= wi::max_value (prec
, sign
);
1266 /* Value range wrapper for wide_int_range_multiplicative_op:
1268 *VR = *VR0 .CODE. *VR1. */
1271 extract_range_from_multiplicative_op (value_range
*vr
,
1272 enum tree_code code
,
1273 const value_range
*vr0
,
1274 const value_range
*vr1
)
1276 gcc_assert (code
== MULT_EXPR
1277 || code
== TRUNC_DIV_EXPR
1278 || code
== FLOOR_DIV_EXPR
1279 || code
== CEIL_DIV_EXPR
1280 || code
== EXACT_DIV_EXPR
1281 || code
== ROUND_DIV_EXPR
1282 || code
== RSHIFT_EXPR
1283 || code
== LSHIFT_EXPR
);
1284 gcc_assert (vr0
->kind () == VR_RANGE
1285 && vr0
->kind () == vr1
->kind ());
1287 tree type
= vr0
->type ();
1288 wide_int res_lb
, res_ub
;
1289 wide_int vr0_lb
= wi::to_wide (vr0
->min ());
1290 wide_int vr0_ub
= wi::to_wide (vr0
->max ());
1291 wide_int vr1_lb
= wi::to_wide (vr1
->min ());
1292 wide_int vr1_ub
= wi::to_wide (vr1
->max ());
1293 bool overflow_undefined
= TYPE_OVERFLOW_UNDEFINED (type
);
1294 unsigned prec
= TYPE_PRECISION (type
);
1296 if (wide_int_range_multiplicative_op (res_lb
, res_ub
,
1297 code
, TYPE_SIGN (type
), prec
,
1298 vr0_lb
, vr0_ub
, vr1_lb
, vr1_ub
,
1299 overflow_undefined
))
1300 vr
->set_and_canonicalize (VR_RANGE
,
1301 wide_int_to_tree (type
, res_lb
),
1302 wide_int_to_tree (type
, res_ub
), NULL
);
1304 set_value_range_to_varying (vr
);
1307 /* If BOUND will include a symbolic bound, adjust it accordingly,
1308 otherwise leave it as is.
1310 CODE is the original operation that combined the bounds (PLUS_EXPR
1313 TYPE is the type of the original operation.
1315 SYM_OPn is the symbolic for OPn if it has a symbolic.
1317 NEG_OPn is TRUE if the OPn was negated. */
1320 adjust_symbolic_bound (tree
&bound
, enum tree_code code
, tree type
,
1321 tree sym_op0
, tree sym_op1
,
1322 bool neg_op0
, bool neg_op1
)
1324 bool minus_p
= (code
== MINUS_EXPR
);
1325 /* If the result bound is constant, we're done; otherwise, build the
1326 symbolic lower bound. */
1327 if (sym_op0
== sym_op1
)
1330 bound
= build_symbolic_expr (type
, sym_op0
,
1334 /* We may not negate if that might introduce
1335 undefined overflow. */
1338 || TYPE_OVERFLOW_WRAPS (type
))
1339 bound
= build_symbolic_expr (type
, sym_op1
,
1340 neg_op1
^ minus_p
, bound
);
1346 /* Combine OP1 and OP1, which are two parts of a bound, into one wide
1347 int bound according to CODE. CODE is the operation combining the
1348 bound (either a PLUS_EXPR or a MINUS_EXPR).
1350 TYPE is the type of the combine operation.
1352 WI is the wide int to store the result.
1354 OVF is -1 if an underflow occurred, +1 if an overflow occurred or 0
1355 if over/underflow occurred. */
1358 combine_bound (enum tree_code code
, wide_int
&wi
, wi::overflow_type
&ovf
,
1359 tree type
, tree op0
, tree op1
)
1361 bool minus_p
= (code
== MINUS_EXPR
);
1362 const signop sgn
= TYPE_SIGN (type
);
1363 const unsigned int prec
= TYPE_PRECISION (type
);
1365 /* Combine the bounds, if any. */
1369 wi
= wi::sub (wi::to_wide (op0
), wi::to_wide (op1
), sgn
, &ovf
);
1371 wi
= wi::add (wi::to_wide (op0
), wi::to_wide (op1
), sgn
, &ovf
);
1374 wi
= wi::to_wide (op0
);
1378 wi
= wi::neg (wi::to_wide (op1
), &ovf
);
1380 wi
= wi::to_wide (op1
);
1383 wi
= wi::shwi (0, prec
);
1386 /* Given a range in [WMIN, WMAX], adjust it for possible overflow and
1387 put the result in VR.
1389 TYPE is the type of the range.
1391 MIN_OVF and MAX_OVF indicate what type of overflow, if any,
1392 occurred while originally calculating WMIN or WMAX. -1 indicates
1393 underflow. +1 indicates overflow. 0 indicates neither. */
1396 set_value_range_with_overflow (value_range_kind
&kind
, tree
&min
, tree
&max
,
1398 const wide_int
&wmin
, const wide_int
&wmax
,
1399 wi::overflow_type min_ovf
,
1400 wi::overflow_type max_ovf
)
1402 const signop sgn
= TYPE_SIGN (type
);
1403 const unsigned int prec
= TYPE_PRECISION (type
);
1405 /* For one bit precision if max < min, then the swapped
1406 range covers all values. */
1407 if (prec
== 1 && wi::lt_p (wmax
, wmin
, sgn
))
1413 if (TYPE_OVERFLOW_WRAPS (type
))
1415 /* If overflow wraps, truncate the values and adjust the
1416 range kind and bounds appropriately. */
1417 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
1418 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
1419 if ((min_ovf
!= wi::OVF_NONE
) == (max_ovf
!= wi::OVF_NONE
))
1421 /* If the limits are swapped, we wrapped around and cover
1422 the entire range. We have a similar check at the end of
1423 extract_range_from_binary_expr_1. */
1424 if (wi::gt_p (tmin
, tmax
, sgn
))
1429 /* No overflow or both overflow or underflow. The
1430 range kind stays VR_RANGE. */
1431 min
= wide_int_to_tree (type
, tmin
);
1432 max
= wide_int_to_tree (type
, tmax
);
1436 else if ((min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_NONE
)
1437 || (max_ovf
== wi::OVF_OVERFLOW
&& min_ovf
== wi::OVF_NONE
))
1439 /* Min underflow or max overflow. The range kind
1440 changes to VR_ANTI_RANGE. */
1441 bool covers
= false;
1442 wide_int tem
= tmin
;
1444 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
1447 if (wi::cmp (tmax
, tem
, sgn
) > 0)
1449 /* If the anti-range would cover nothing, drop to varying.
1450 Likewise if the anti-range bounds are outside of the
1452 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
1457 kind
= VR_ANTI_RANGE
;
1458 min
= wide_int_to_tree (type
, tmin
);
1459 max
= wide_int_to_tree (type
, tmax
);
1464 /* Other underflow and/or overflow, drop to VR_VARYING. */
1471 /* If overflow does not wrap, saturate to the types min/max
1473 wide_int type_min
= wi::min_value (prec
, sgn
);
1474 wide_int type_max
= wi::max_value (prec
, sgn
);
1476 if (min_ovf
== wi::OVF_UNDERFLOW
)
1477 min
= wide_int_to_tree (type
, type_min
);
1478 else if (min_ovf
== wi::OVF_OVERFLOW
)
1479 min
= wide_int_to_tree (type
, type_max
);
1481 min
= wide_int_to_tree (type
, wmin
);
1483 if (max_ovf
== wi::OVF_UNDERFLOW
)
1484 max
= wide_int_to_tree (type
, type_min
);
1485 else if (max_ovf
== wi::OVF_OVERFLOW
)
1486 max
= wide_int_to_tree (type
, type_max
);
1488 max
= wide_int_to_tree (type
, wmax
);
1492 /* Extract range information from a binary operation CODE based on
1493 the ranges of each of its operands *VR0 and *VR1 with resulting
1494 type EXPR_TYPE. The resulting range is stored in *VR. */
1497 extract_range_from_binary_expr_1 (value_range
*vr
,
1498 enum tree_code code
, tree expr_type
,
1499 const value_range
*vr0_
,
1500 const value_range
*vr1_
)
1502 signop sign
= TYPE_SIGN (expr_type
);
1503 unsigned int prec
= TYPE_PRECISION (expr_type
);
1504 value_range vr0
= *vr0_
, vr1
= *vr1_
;
1505 value_range vrtem0
, vrtem1
;
1506 enum value_range_kind type
;
1507 tree min
= NULL_TREE
, max
= NULL_TREE
;
1510 if (!INTEGRAL_TYPE_P (expr_type
)
1511 && !POINTER_TYPE_P (expr_type
))
1513 set_value_range_to_varying (vr
);
1517 /* Not all binary expressions can be applied to ranges in a
1518 meaningful way. Handle only arithmetic operations. */
1519 if (code
!= PLUS_EXPR
1520 && code
!= MINUS_EXPR
1521 && code
!= POINTER_PLUS_EXPR
1522 && code
!= MULT_EXPR
1523 && code
!= TRUNC_DIV_EXPR
1524 && code
!= FLOOR_DIV_EXPR
1525 && code
!= CEIL_DIV_EXPR
1526 && code
!= EXACT_DIV_EXPR
1527 && code
!= ROUND_DIV_EXPR
1528 && code
!= TRUNC_MOD_EXPR
1529 && code
!= RSHIFT_EXPR
1530 && code
!= LSHIFT_EXPR
1533 && code
!= BIT_AND_EXPR
1534 && code
!= BIT_IOR_EXPR
1535 && code
!= BIT_XOR_EXPR
)
1537 set_value_range_to_varying (vr
);
1541 /* If both ranges are UNDEFINED, so is the result. */
1542 if (vr0
.undefined_p () && vr1
.undefined_p ())
1544 set_value_range_to_undefined (vr
);
1547 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
1548 code. At some point we may want to special-case operations that
1549 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
1551 else if (vr0
.undefined_p ())
1552 set_value_range_to_varying (&vr0
);
1553 else if (vr1
.undefined_p ())
1554 set_value_range_to_varying (&vr1
);
1556 /* We get imprecise results from ranges_from_anti_range when
1557 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
1558 range, but then we also need to hack up vrp_union. It's just
1559 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
1560 if (code
== EXACT_DIV_EXPR
&& range_is_nonnull (&vr0
))
1562 set_value_range_to_nonnull (vr
, expr_type
);
1566 /* Now canonicalize anti-ranges to ranges when they are not symbolic
1567 and express ~[] op X as ([]' op X) U ([]'' op X). */
1568 if (vr0
.kind () == VR_ANTI_RANGE
1569 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
1571 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
1572 if (!vrtem1
.undefined_p ())
1575 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
, &vrtem1
, vr1_
);
1576 vr
->union_ (&vrres
);
1580 /* Likewise for X op ~[]. */
1581 if (vr1
.kind () == VR_ANTI_RANGE
1582 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
1584 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
1585 if (!vrtem1
.undefined_p ())
1588 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
1590 vr
->union_ (&vrres
);
1595 /* The type of the resulting value range defaults to VR0.TYPE. */
1598 /* Refuse to operate on VARYING ranges, ranges of different kinds
1599 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
1600 because we may be able to derive a useful range even if one of
1601 the operands is VR_VARYING or symbolic range. Similarly for
1602 divisions, MIN/MAX and PLUS/MINUS.
1604 TODO, we may be able to derive anti-ranges in some cases. */
1605 if (code
!= BIT_AND_EXPR
1606 && code
!= BIT_IOR_EXPR
1607 && code
!= TRUNC_DIV_EXPR
1608 && code
!= FLOOR_DIV_EXPR
1609 && code
!= CEIL_DIV_EXPR
1610 && code
!= EXACT_DIV_EXPR
1611 && code
!= ROUND_DIV_EXPR
1612 && code
!= TRUNC_MOD_EXPR
1615 && code
!= PLUS_EXPR
1616 && code
!= MINUS_EXPR
1617 && code
!= RSHIFT_EXPR
1618 && code
!= POINTER_PLUS_EXPR
1619 && (vr0
.varying_p ()
1621 || vr0
.kind () != vr1
.kind ()
1622 || vr0
.symbolic_p ()
1623 || vr1
.symbolic_p ()))
1625 set_value_range_to_varying (vr
);
1629 /* Now evaluate the expression to determine the new range. */
1630 if (POINTER_TYPE_P (expr_type
))
1632 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
1634 /* For MIN/MAX expressions with pointers, we only care about
1635 nullness, if both are non null, then the result is nonnull.
1636 If both are null, then the result is null. Otherwise they
1638 if (!range_includes_zero_p (&vr0
) && !range_includes_zero_p (&vr1
))
1639 set_value_range_to_nonnull (vr
, expr_type
);
1640 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
1641 set_value_range_to_null (vr
, expr_type
);
1643 set_value_range_to_varying (vr
);
1645 else if (code
== POINTER_PLUS_EXPR
)
1647 /* For pointer types, we are really only interested in asserting
1648 whether the expression evaluates to non-NULL. */
1649 if (!range_includes_zero_p (&vr0
)
1650 || !range_includes_zero_p (&vr1
))
1651 set_value_range_to_nonnull (vr
, expr_type
);
1652 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
1653 set_value_range_to_null (vr
, expr_type
);
1655 set_value_range_to_varying (vr
);
1657 else if (code
== BIT_AND_EXPR
)
1659 /* For pointer types, we are really only interested in asserting
1660 whether the expression evaluates to non-NULL. */
1661 if (!range_includes_zero_p (&vr0
) && !range_includes_zero_p (&vr1
))
1662 set_value_range_to_nonnull (vr
, expr_type
);
1663 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
1664 set_value_range_to_null (vr
, expr_type
);
1666 set_value_range_to_varying (vr
);
1669 set_value_range_to_varying (vr
);
1674 /* For integer ranges, apply the operation to each end of the
1675 range and see what we end up with. */
1676 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
1678 /* This will normalize things such that calculating
1679 [0,0] - VR_VARYING is not dropped to varying, but is
1680 calculated as [MIN+1, MAX]. */
1681 if (vr0
.varying_p ())
1682 vr0
.update (VR_RANGE
,
1683 vrp_val_min (expr_type
),
1684 vrp_val_max (expr_type
));
1685 if (vr1
.varying_p ())
1686 vr1
.update (VR_RANGE
,
1687 vrp_val_min (expr_type
),
1688 vrp_val_max (expr_type
));
1690 const bool minus_p
= (code
== MINUS_EXPR
);
1691 tree min_op0
= vr0
.min ();
1692 tree min_op1
= minus_p
? vr1
.max () : vr1
.min ();
1693 tree max_op0
= vr0
.max ();
1694 tree max_op1
= minus_p
? vr1
.min () : vr1
.max ();
1695 tree sym_min_op0
= NULL_TREE
;
1696 tree sym_min_op1
= NULL_TREE
;
1697 tree sym_max_op0
= NULL_TREE
;
1698 tree sym_max_op1
= NULL_TREE
;
1699 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
1701 neg_min_op0
= neg_min_op1
= neg_max_op0
= neg_max_op1
= false;
1703 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
1704 single-symbolic ranges, try to compute the precise resulting range,
1705 but only if we know that this resulting range will also be constant
1706 or single-symbolic. */
1707 if (vr0
.kind () == VR_RANGE
&& vr1
.kind () == VR_RANGE
1708 && (TREE_CODE (min_op0
) == INTEGER_CST
1710 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
1711 && (TREE_CODE (min_op1
) == INTEGER_CST
1713 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
1714 && (!(sym_min_op0
&& sym_min_op1
)
1715 || (sym_min_op0
== sym_min_op1
1716 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
1717 && (TREE_CODE (max_op0
) == INTEGER_CST
1719 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
1720 && (TREE_CODE (max_op1
) == INTEGER_CST
1722 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
1723 && (!(sym_max_op0
&& sym_max_op1
)
1724 || (sym_max_op0
== sym_max_op1
1725 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
1727 wide_int wmin
, wmax
;
1728 wi::overflow_type min_ovf
= wi::OVF_NONE
;
1729 wi::overflow_type max_ovf
= wi::OVF_NONE
;
1731 /* Build the bounds. */
1732 combine_bound (code
, wmin
, min_ovf
, expr_type
, min_op0
, min_op1
);
1733 combine_bound (code
, wmax
, max_ovf
, expr_type
, max_op0
, max_op1
);
1735 /* If we have overflow for the constant part and the resulting
1736 range will be symbolic, drop to VR_VARYING. */
1737 if (((bool)min_ovf
&& sym_min_op0
!= sym_min_op1
)
1738 || ((bool)max_ovf
&& sym_max_op0
!= sym_max_op1
))
1740 set_value_range_to_varying (vr
);
1744 /* Adjust the range for possible overflow. */
1747 set_value_range_with_overflow (type
, min
, max
, expr_type
,
1748 wmin
, wmax
, min_ovf
, max_ovf
);
1749 if (type
== VR_VARYING
)
1751 set_value_range_to_varying (vr
);
1755 /* Build the symbolic bounds if needed. */
1756 adjust_symbolic_bound (min
, code
, expr_type
,
1757 sym_min_op0
, sym_min_op1
,
1758 neg_min_op0
, neg_min_op1
);
1759 adjust_symbolic_bound (max
, code
, expr_type
,
1760 sym_max_op0
, sym_max_op1
,
1761 neg_max_op0
, neg_max_op1
);
1765 /* For other cases, for example if we have a PLUS_EXPR with two
1766 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
1767 to compute a precise range for such a case.
1768 ??? General even mixed range kind operations can be expressed
1769 by for example transforming ~[3, 5] + [1, 2] to range-only
1770 operations and a union primitive:
1771 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
1772 [-INF+1, 4] U [6, +INF(OVF)]
1773 though usually the union is not exactly representable with
1774 a single range or anti-range as the above is
1775 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
1776 but one could use a scheme similar to equivalences for this. */
1777 set_value_range_to_varying (vr
);
1781 else if (code
== MIN_EXPR
1782 || code
== MAX_EXPR
)
1784 wide_int wmin
, wmax
;
1785 wide_int vr0_min
, vr0_max
;
1786 wide_int vr1_min
, vr1_max
;
1787 extract_range_into_wide_ints (&vr0
, sign
, prec
, vr0_min
, vr0_max
);
1788 extract_range_into_wide_ints (&vr1
, sign
, prec
, vr1_min
, vr1_max
);
1789 if (wide_int_range_min_max (wmin
, wmax
, code
, sign
, prec
,
1790 vr0_min
, vr0_max
, vr1_min
, vr1_max
))
1791 vr
->update (VR_RANGE
, wide_int_to_tree (expr_type
, wmin
),
1792 wide_int_to_tree (expr_type
, wmax
));
1794 set_value_range_to_varying (vr
);
1797 else if (code
== MULT_EXPR
)
1799 if (!range_int_cst_p (&vr0
)
1800 || !range_int_cst_p (&vr1
))
1802 set_value_range_to_varying (vr
);
1805 extract_range_from_multiplicative_op (vr
, code
, &vr0
, &vr1
);
1808 else if (code
== RSHIFT_EXPR
1809 || code
== LSHIFT_EXPR
)
1811 if (range_int_cst_p (&vr1
)
1812 && !wide_int_range_shift_undefined_p
1813 (TYPE_SIGN (TREE_TYPE (vr1
.min ())),
1815 wi::to_wide (vr1
.min ()),
1816 wi::to_wide (vr1
.max ())))
1818 if (code
== RSHIFT_EXPR
)
1820 /* Even if vr0 is VARYING or otherwise not usable, we can derive
1821 useful ranges just from the shift count. E.g.
1822 x >> 63 for signed 64-bit x is always [-1, 0]. */
1823 if (vr0
.kind () != VR_RANGE
|| vr0
.symbolic_p ())
1824 vr0
.update (VR_RANGE
,
1825 vrp_val_min (expr_type
),
1826 vrp_val_max (expr_type
));
1827 extract_range_from_multiplicative_op (vr
, code
, &vr0
, &vr1
);
1830 else if (code
== LSHIFT_EXPR
1831 && range_int_cst_p (&vr0
))
1833 wide_int res_lb
, res_ub
;
1834 if (wide_int_range_lshift (res_lb
, res_ub
, sign
, prec
,
1835 wi::to_wide (vr0
.min ()),
1836 wi::to_wide (vr0
.max ()),
1837 wi::to_wide (vr1
.min ()),
1838 wi::to_wide (vr1
.max ()),
1839 TYPE_OVERFLOW_UNDEFINED (expr_type
)))
1841 min
= wide_int_to_tree (expr_type
, res_lb
);
1842 max
= wide_int_to_tree (expr_type
, res_ub
);
1843 vr
->set_and_canonicalize (VR_RANGE
, min
, max
, NULL
);
1848 set_value_range_to_varying (vr
);
1851 else if (code
== TRUNC_DIV_EXPR
1852 || code
== FLOOR_DIV_EXPR
1853 || code
== CEIL_DIV_EXPR
1854 || code
== EXACT_DIV_EXPR
1855 || code
== ROUND_DIV_EXPR
)
1857 wide_int dividend_min
, dividend_max
, divisor_min
, divisor_max
;
1858 wide_int wmin
, wmax
, extra_min
, extra_max
;
1861 /* Special case explicit division by zero as undefined. */
1862 if (range_is_null (&vr1
))
1864 set_value_range_to_undefined (vr
);
1868 /* First, normalize ranges into constants we can handle. Note
1869 that VR_ANTI_RANGE's of constants were already normalized
1870 before arriving here.
1872 NOTE: As a future improvement, we may be able to do better
1873 with mixed symbolic (anti-)ranges like [0, A]. See note in
1874 ranges_from_anti_range. */
1875 extract_range_into_wide_ints (&vr0
, sign
, prec
,
1876 dividend_min
, dividend_max
);
1877 extract_range_into_wide_ints (&vr1
, sign
, prec
,
1878 divisor_min
, divisor_max
);
1879 if (!wide_int_range_div (wmin
, wmax
, code
, sign
, prec
,
1880 dividend_min
, dividend_max
,
1881 divisor_min
, divisor_max
,
1882 TYPE_OVERFLOW_UNDEFINED (expr_type
),
1883 extra_range_p
, extra_min
, extra_max
))
1885 set_value_range_to_varying (vr
);
1888 set_value_range (vr
, VR_RANGE
,
1889 wide_int_to_tree (expr_type
, wmin
),
1890 wide_int_to_tree (expr_type
, wmax
), NULL
);
1893 value_range extra_range
;
1894 set_value_range (&extra_range
, VR_RANGE
,
1895 wide_int_to_tree (expr_type
, extra_min
),
1896 wide_int_to_tree (expr_type
, extra_max
), NULL
);
1897 vr
->union_ (&extra_range
);
1901 else if (code
== TRUNC_MOD_EXPR
)
1903 if (range_is_null (&vr1
))
1905 set_value_range_to_undefined (vr
);
1908 wide_int wmin
, wmax
, tmp
;
1909 wide_int vr0_min
, vr0_max
, vr1_min
, vr1_max
;
1910 extract_range_into_wide_ints (&vr0
, sign
, prec
, vr0_min
, vr0_max
);
1911 extract_range_into_wide_ints (&vr1
, sign
, prec
, vr1_min
, vr1_max
);
1912 wide_int_range_trunc_mod (wmin
, wmax
, sign
, prec
,
1913 vr0_min
, vr0_max
, vr1_min
, vr1_max
);
1914 min
= wide_int_to_tree (expr_type
, wmin
);
1915 max
= wide_int_to_tree (expr_type
, wmax
);
1916 set_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
1919 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
1921 wide_int may_be_nonzero0
, may_be_nonzero1
;
1922 wide_int must_be_nonzero0
, must_be_nonzero1
;
1923 wide_int wmin
, wmax
;
1924 wide_int vr0_min
, vr0_max
, vr1_min
, vr1_max
;
1925 vrp_set_zero_nonzero_bits (expr_type
, &vr0
,
1926 &may_be_nonzero0
, &must_be_nonzero0
);
1927 vrp_set_zero_nonzero_bits (expr_type
, &vr1
,
1928 &may_be_nonzero1
, &must_be_nonzero1
);
1929 extract_range_into_wide_ints (&vr0
, sign
, prec
, vr0_min
, vr0_max
);
1930 extract_range_into_wide_ints (&vr1
, sign
, prec
, vr1_min
, vr1_max
);
1931 if (code
== BIT_AND_EXPR
)
1933 if (wide_int_range_bit_and (wmin
, wmax
, sign
, prec
,
1941 min
= wide_int_to_tree (expr_type
, wmin
);
1942 max
= wide_int_to_tree (expr_type
, wmax
);
1943 set_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
1946 set_value_range_to_varying (vr
);
1949 else if (code
== BIT_IOR_EXPR
)
1951 if (wide_int_range_bit_ior (wmin
, wmax
, sign
,
1959 min
= wide_int_to_tree (expr_type
, wmin
);
1960 max
= wide_int_to_tree (expr_type
, wmax
);
1961 set_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
1964 set_value_range_to_varying (vr
);
1967 else if (code
== BIT_XOR_EXPR
)
1969 if (wide_int_range_bit_xor (wmin
, wmax
, sign
, prec
,
1975 min
= wide_int_to_tree (expr_type
, wmin
);
1976 max
= wide_int_to_tree (expr_type
, wmax
);
1977 set_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
1980 set_value_range_to_varying (vr
);
1987 /* If either MIN or MAX overflowed, then set the resulting range to
1989 if (min
== NULL_TREE
1990 || TREE_OVERFLOW_P (min
)
1992 || TREE_OVERFLOW_P (max
))
1994 set_value_range_to_varying (vr
);
1998 /* We punt for [-INF, +INF].
1999 We learn nothing when we have INF on both sides.
2000 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
2001 if (vrp_val_is_min (min
) && vrp_val_is_max (max
))
2003 set_value_range_to_varying (vr
);
2007 cmp
= compare_values (min
, max
);
2008 if (cmp
== -2 || cmp
== 1)
2010 /* If the new range has its limits swapped around (MIN > MAX),
2011 then the operation caused one of them to wrap around, mark
2012 the new range VARYING. */
2013 set_value_range_to_varying (vr
);
2016 set_value_range (vr
, type
, min
, max
, NULL
);
2019 /* Extract range information from a unary operation CODE based on
2020 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
2021 The resulting range is stored in *VR. */
2024 extract_range_from_unary_expr (value_range
*vr
,
2025 enum tree_code code
, tree type
,
2026 const value_range
*vr0_
, tree op0_type
)
2028 signop sign
= TYPE_SIGN (type
);
2029 unsigned int prec
= TYPE_PRECISION (type
);
2030 value_range vr0
= *vr0_
;
2031 value_range vrtem0
, vrtem1
;
2033 /* VRP only operates on integral and pointer types. */
2034 if (!(INTEGRAL_TYPE_P (op0_type
)
2035 || POINTER_TYPE_P (op0_type
))
2036 || !(INTEGRAL_TYPE_P (type
)
2037 || POINTER_TYPE_P (type
)))
2039 set_value_range_to_varying (vr
);
2043 /* If VR0 is UNDEFINED, so is the result. */
2044 if (vr0
.undefined_p ())
2046 set_value_range_to_undefined (vr
);
2050 /* Handle operations that we express in terms of others. */
2051 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
2053 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
2054 vr
->deep_copy (&vr0
);
2057 else if (code
== NEGATE_EXPR
)
2059 /* -X is simply 0 - X, so re-use existing code that also handles
2060 anti-ranges fine. */
2062 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
2063 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
2066 else if (code
== BIT_NOT_EXPR
)
2068 /* ~X is simply -1 - X, so re-use existing code that also handles
2069 anti-ranges fine. */
2070 value_range minusone
;
2071 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
2072 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
2073 type
, &minusone
, &vr0
);
2077 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2078 and express op ~[] as (op []') U (op []''). */
2079 if (vr0
.kind () == VR_ANTI_RANGE
2080 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2082 extract_range_from_unary_expr (vr
, code
, type
, &vrtem0
, op0_type
);
2083 if (!vrtem1
.undefined_p ())
2086 extract_range_from_unary_expr (&vrres
, code
, type
,
2088 vr
->union_ (&vrres
);
2093 if (CONVERT_EXPR_CODE_P (code
))
2095 tree inner_type
= op0_type
;
2096 tree outer_type
= type
;
2098 /* If the expression involves a pointer, we are only interested in
2099 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]).
2101 This may lose precision when converting (char *)~[0,2] to
2102 int, because we'll forget that the pointer can also not be 1
2103 or 2. In practice we don't care, as this is some idiot
2104 storing a magic constant to a pointer. */
2105 if (POINTER_TYPE_P (type
) || POINTER_TYPE_P (op0_type
))
2107 if (!range_includes_zero_p (&vr0
))
2108 set_value_range_to_nonnull (vr
, type
);
2109 else if (range_is_null (&vr0
))
2110 set_value_range_to_null (vr
, type
);
2112 set_value_range_to_varying (vr
);
2116 /* The POINTER_TYPE_P code above will have dealt with all
2117 pointer anti-ranges. Any remaining anti-ranges at this point
2118 will be integer conversions from SSA names that will be
2119 normalized into VARYING. For instance: ~[x_55, x_55]. */
2120 gcc_assert (vr0
.kind () != VR_ANTI_RANGE
2121 || TREE_CODE (vr0
.min ()) != INTEGER_CST
);
2123 /* NOTES: Previously we were returning VARYING for all symbolics, but
2124 we can do better by treating them as [-MIN, +MAX]. For
2125 example, converting [SYM, SYM] from INT to LONG UNSIGNED,
2126 we can return: ~[0x8000000, 0xffffffff7fffffff].
2128 We were also failing to convert ~[0,0] from char* to unsigned,
2129 instead choosing to return VR_VARYING. Now we return ~[0,0]. */
2130 wide_int vr0_min
, vr0_max
, wmin
, wmax
;
2131 signop inner_sign
= TYPE_SIGN (inner_type
);
2132 signop outer_sign
= TYPE_SIGN (outer_type
);
2133 unsigned inner_prec
= TYPE_PRECISION (inner_type
);
2134 unsigned outer_prec
= TYPE_PRECISION (outer_type
);
2135 extract_range_into_wide_ints (&vr0
, inner_sign
, inner_prec
,
2137 if (wide_int_range_convert (wmin
, wmax
,
2138 inner_sign
, inner_prec
,
2139 outer_sign
, outer_prec
,
2142 tree min
= wide_int_to_tree (outer_type
, wmin
);
2143 tree max
= wide_int_to_tree (outer_type
, wmax
);
2144 vr
->set_and_canonicalize (VR_RANGE
, min
, max
, NULL
);
2147 set_value_range_to_varying (vr
);
2150 else if (code
== ABS_EXPR
)
2152 wide_int wmin
, wmax
;
2153 wide_int vr0_min
, vr0_max
;
2154 extract_range_into_wide_ints (&vr0
, sign
, prec
, vr0_min
, vr0_max
);
2155 if (wide_int_range_abs (wmin
, wmax
, sign
, prec
, vr0_min
, vr0_max
,
2156 TYPE_OVERFLOW_UNDEFINED (type
)))
2157 set_value_range (vr
, VR_RANGE
,
2158 wide_int_to_tree (type
, wmin
),
2159 wide_int_to_tree (type
, wmax
), NULL
);
2161 set_value_range_to_varying (vr
);
2165 /* For unhandled operations fall back to varying. */
2166 set_value_range_to_varying (vr
);
2170 /* Debugging dumps. */
2172 void dump_value_range (FILE *, const value_range
*);
2173 void debug_value_range (const value_range
*);
2174 void dump_all_value_ranges (FILE *);
2175 void dump_vr_equiv (FILE *, bitmap
);
2176 void debug_vr_equiv (bitmap
);
2179 dump_value_range (FILE *file
, const value_range
*vr
)
2182 fprintf (file
, "[]");
2188 dump_value_range_base (FILE *file
, const value_range_base
*vr
)
2191 fprintf (file
, "[]");
2196 /* Dump value range VR to stderr. */
2199 debug_value_range (const value_range
*vr
)
2205 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2206 create a new SSA name N and return the assertion assignment
2207 'N = ASSERT_EXPR <V, V OP W>'. */
2210 build_assert_expr_for (tree cond
, tree v
)
2215 gcc_assert (TREE_CODE (v
) == SSA_NAME
2216 && COMPARISON_CLASS_P (cond
));
2218 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
2219 assertion
= gimple_build_assign (NULL_TREE
, a
);
2221 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
2222 operand of the ASSERT_EXPR. Create it so the new name and the old one
2223 are registered in the replacement table so that we can fix the SSA web
2224 after adding all the ASSERT_EXPRs. */
2225 tree new_def
= create_new_def_for (v
, assertion
, NULL
);
2226 /* Make sure we preserve abnormalness throughout an ASSERT_EXPR chain
2227 given we have to be able to fully propagate those out to re-create
2228 valid SSA when removing the asserts. */
2229 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (v
))
2230 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_def
) = 1;
2236 /* Return false if EXPR is a predicate expression involving floating
2240 fp_predicate (gimple
*stmt
)
2242 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
2244 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
2247 /* If the range of values taken by OP can be inferred after STMT executes,
2248 return the comparison code (COMP_CODE_P) and value (VAL_P) that
2249 describes the inferred range. Return true if a range could be
2253 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
2256 *comp_code_p
= ERROR_MARK
;
2258 /* Do not attempt to infer anything in names that flow through
2260 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
2263 /* If STMT is the last statement of a basic block with no normal
2264 successors, there is no point inferring anything about any of its
2265 operands. We would not be able to find a proper insertion point
2266 for the assertion, anyway. */
2267 if (stmt_ends_bb_p (stmt
))
2272 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
2273 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
2279 if (infer_nonnull_range (stmt
, op
))
2281 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
2282 *comp_code_p
= NE_EXPR
;
2290 void dump_asserts_for (FILE *, tree
);
2291 void debug_asserts_for (tree
);
2292 void dump_all_asserts (FILE *);
2293 void debug_all_asserts (void);
2295 /* Dump all the registered assertions for NAME to FILE. */
2298 dump_asserts_for (FILE *file
, tree name
)
2302 fprintf (file
, "Assertions to be inserted for ");
2303 print_generic_expr (file
, name
);
2304 fprintf (file
, "\n");
2306 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
2309 fprintf (file
, "\t");
2310 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0);
2311 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
2314 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
2315 loc
->e
->dest
->index
);
2316 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
2318 fprintf (file
, "\n\tPREDICATE: ");
2319 print_generic_expr (file
, loc
->expr
);
2320 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
2321 print_generic_expr (file
, loc
->val
);
2322 fprintf (file
, "\n\n");
2326 fprintf (file
, "\n");
2330 /* Dump all the registered assertions for NAME to stderr. */
2333 debug_asserts_for (tree name
)
2335 dump_asserts_for (stderr
, name
);
2339 /* Dump all the registered assertions for all the names to FILE. */
2342 dump_all_asserts (FILE *file
)
2347 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
2348 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
2349 dump_asserts_for (file
, ssa_name (i
));
2350 fprintf (file
, "\n");
2354 /* Dump all the registered assertions for all the names to stderr. */
2357 debug_all_asserts (void)
2359 dump_all_asserts (stderr
);
2362 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
2365 add_assert_info (vec
<assert_info
> &asserts
,
2366 tree name
, tree expr
, enum tree_code comp_code
, tree val
)
2369 info
.comp_code
= comp_code
;
2371 if (TREE_OVERFLOW_P (val
))
2372 val
= drop_tree_overflow (val
);
2375 asserts
.safe_push (info
);
2376 if (dump_enabled_p ())
2377 dump_printf (MSG_NOTE
| MSG_PRIORITY_INTERNALS
,
2378 "Adding assert for %T from %T %s %T\n",
2379 name
, expr
, op_symbol_code (comp_code
), val
);
2382 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2383 'EXPR COMP_CODE VAL' at a location that dominates block BB or
2384 E->DEST, then register this location as a possible insertion point
2385 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
2387 BB, E and SI provide the exact insertion point for the new
2388 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2389 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2390 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2391 must not be NULL. */
2394 register_new_assert_for (tree name
, tree expr
,
2395 enum tree_code comp_code
,
2399 gimple_stmt_iterator si
)
2401 assert_locus
*n
, *loc
, *last_loc
;
2402 basic_block dest_bb
;
2404 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
2407 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
2408 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
2410 /* Never build an assert comparing against an integer constant with
2411 TREE_OVERFLOW set. This confuses our undefined overflow warning
2413 if (TREE_OVERFLOW_P (val
))
2414 val
= drop_tree_overflow (val
);
2416 /* The new assertion A will be inserted at BB or E. We need to
2417 determine if the new location is dominated by a previously
2418 registered location for A. If we are doing an edge insertion,
2419 assume that A will be inserted at E->DEST. Note that this is not
2422 If E is a critical edge, it will be split. But even if E is
2423 split, the new block will dominate the same set of blocks that
2426 The reverse, however, is not true, blocks dominated by E->DEST
2427 will not be dominated by the new block created to split E. So,
2428 if the insertion location is on a critical edge, we will not use
2429 the new location to move another assertion previously registered
2430 at a block dominated by E->DEST. */
2431 dest_bb
= (bb
) ? bb
: e
->dest
;
2433 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
2434 VAL at a block dominating DEST_BB, then we don't need to insert a new
2435 one. Similarly, if the same assertion already exists at a block
2436 dominated by DEST_BB and the new location is not on a critical
2437 edge, then update the existing location for the assertion (i.e.,
2438 move the assertion up in the dominance tree).
2440 Note, this is implemented as a simple linked list because there
2441 should not be more than a handful of assertions registered per
2442 name. If this becomes a performance problem, a table hashed by
2443 COMP_CODE and VAL could be implemented. */
2444 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
2448 if (loc
->comp_code
== comp_code
2450 || operand_equal_p (loc
->val
, val
, 0))
2451 && (loc
->expr
== expr
2452 || operand_equal_p (loc
->expr
, expr
, 0)))
2454 /* If E is not a critical edge and DEST_BB
2455 dominates the existing location for the assertion, move
2456 the assertion up in the dominance tree by updating its
2457 location information. */
2458 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
2459 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
2468 /* Update the last node of the list and move to the next one. */
2473 /* If we didn't find an assertion already registered for
2474 NAME COMP_CODE VAL, add a new one at the end of the list of
2475 assertions associated with NAME. */
2476 n
= XNEW (struct assert_locus
);
2480 n
->comp_code
= comp_code
;
2488 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
2490 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
2493 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
2494 Extract a suitable test code and value and store them into *CODE_P and
2495 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
2497 If no extraction was possible, return FALSE, otherwise return TRUE.
2499 If INVERT is true, then we invert the result stored into *CODE_P. */
2502 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
2503 tree cond_op0
, tree cond_op1
,
2504 bool invert
, enum tree_code
*code_p
,
2507 enum tree_code comp_code
;
2510 /* Otherwise, we have a comparison of the form NAME COMP VAL
2511 or VAL COMP NAME. */
2512 if (name
== cond_op1
)
2514 /* If the predicate is of the form VAL COMP NAME, flip
2515 COMP around because we need to register NAME as the
2516 first operand in the predicate. */
2517 comp_code
= swap_tree_comparison (cond_code
);
2520 else if (name
== cond_op0
)
2522 /* The comparison is of the form NAME COMP VAL, so the
2523 comparison code remains unchanged. */
2524 comp_code
= cond_code
;
2530 /* Invert the comparison code as necessary. */
2532 comp_code
= invert_tree_comparison (comp_code
, 0);
2534 /* VRP only handles integral and pointer types. */
2535 if (! INTEGRAL_TYPE_P (TREE_TYPE (val
))
2536 && ! POINTER_TYPE_P (TREE_TYPE (val
)))
2539 /* Do not register always-false predicates.
2540 FIXME: this works around a limitation in fold() when dealing with
2541 enumerations. Given 'enum { N1, N2 } x;', fold will not
2542 fold 'if (x > N2)' to 'if (0)'. */
2543 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
2544 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2546 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
2547 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
2549 if (comp_code
== GT_EXPR
2551 || compare_values (val
, max
) == 0))
2554 if (comp_code
== LT_EXPR
2556 || compare_values (val
, min
) == 0))
2559 *code_p
= comp_code
;
2564 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
2565 (otherwise return VAL). VAL and MASK must be zero-extended for
2566 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
2567 (to transform signed values into unsigned) and at the end xor
2571 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
2572 const wide_int
&sgnbit
, unsigned int prec
)
2574 wide_int bit
= wi::one (prec
), res
;
2577 wide_int val
= val_in
^ sgnbit
;
2578 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
2581 if ((res
& bit
) == 0)
2584 res
= wi::bit_and_not (val
+ bit
, res
);
2586 if (wi::gtu_p (res
, val
))
2587 return res
^ sgnbit
;
2589 return val
^ sgnbit
;
2592 /* Helper for overflow_comparison_p
2594 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
2595 OP1's defining statement to see if it ultimately has the form
2596 OP0 CODE (OP0 PLUS INTEGER_CST)
2598 If so, return TRUE indicating this is an overflow test and store into
2599 *NEW_CST an updated constant that can be used in a narrowed range test.
2601 REVERSED indicates if the comparison was originally:
2605 This affects how we build the updated constant. */
2608 overflow_comparison_p_1 (enum tree_code code
, tree op0
, tree op1
,
2609 bool follow_assert_exprs
, bool reversed
, tree
*new_cst
)
2611 /* See if this is a relational operation between two SSA_NAMES with
2612 unsigned, overflow wrapping values. If so, check it more deeply. */
2613 if ((code
== LT_EXPR
|| code
== LE_EXPR
2614 || code
== GE_EXPR
|| code
== GT_EXPR
)
2615 && TREE_CODE (op0
) == SSA_NAME
2616 && TREE_CODE (op1
) == SSA_NAME
2617 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
2618 && TYPE_UNSIGNED (TREE_TYPE (op0
))
2619 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
2621 gimple
*op1_def
= SSA_NAME_DEF_STMT (op1
);
2623 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
2624 if (follow_assert_exprs
)
2626 while (gimple_assign_single_p (op1_def
)
2627 && TREE_CODE (gimple_assign_rhs1 (op1_def
)) == ASSERT_EXPR
)
2629 op1
= TREE_OPERAND (gimple_assign_rhs1 (op1_def
), 0);
2630 if (TREE_CODE (op1
) != SSA_NAME
)
2632 op1_def
= SSA_NAME_DEF_STMT (op1
);
2636 /* Now look at the defining statement of OP1 to see if it adds
2637 or subtracts a nonzero constant from another operand. */
2639 && is_gimple_assign (op1_def
)
2640 && gimple_assign_rhs_code (op1_def
) == PLUS_EXPR
2641 && TREE_CODE (gimple_assign_rhs2 (op1_def
)) == INTEGER_CST
2642 && !integer_zerop (gimple_assign_rhs2 (op1_def
)))
2644 tree target
= gimple_assign_rhs1 (op1_def
);
2646 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
2647 for one where TARGET appears on the RHS. */
2648 if (follow_assert_exprs
)
2650 /* Now see if that "other operand" is op0, following the chain
2651 of ASSERT_EXPRs if necessary. */
2652 gimple
*op0_def
= SSA_NAME_DEF_STMT (op0
);
2653 while (op0
!= target
2654 && gimple_assign_single_p (op0_def
)
2655 && TREE_CODE (gimple_assign_rhs1 (op0_def
)) == ASSERT_EXPR
)
2657 op0
= TREE_OPERAND (gimple_assign_rhs1 (op0_def
), 0);
2658 if (TREE_CODE (op0
) != SSA_NAME
)
2660 op0_def
= SSA_NAME_DEF_STMT (op0
);
2664 /* If we did not find our target SSA_NAME, then this is not
2665 an overflow test. */
2669 tree type
= TREE_TYPE (op0
);
2670 wide_int max
= wi::max_value (TYPE_PRECISION (type
), UNSIGNED
);
2671 tree inc
= gimple_assign_rhs2 (op1_def
);
2673 *new_cst
= wide_int_to_tree (type
, max
+ wi::to_wide (inc
));
2675 *new_cst
= wide_int_to_tree (type
, max
- wi::to_wide (inc
));
2682 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
2683 OP1's defining statement to see if it ultimately has the form
2684 OP0 CODE (OP0 PLUS INTEGER_CST)
2686 If so, return TRUE indicating this is an overflow test and store into
2687 *NEW_CST an updated constant that can be used in a narrowed range test.
2689 These statements are left as-is in the IL to facilitate discovery of
2690 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
2691 the alternate range representation is often useful within VRP. */
2694 overflow_comparison_p (tree_code code
, tree name
, tree val
,
2695 bool use_equiv_p
, tree
*new_cst
)
2697 if (overflow_comparison_p_1 (code
, name
, val
, use_equiv_p
, false, new_cst
))
2699 return overflow_comparison_p_1 (swap_tree_comparison (code
), val
, name
,
2700 use_equiv_p
, true, new_cst
);
2704 /* Try to register an edge assertion for SSA name NAME on edge E for
2705 the condition COND contributing to the conditional jump pointed to by BSI.
2706 Invert the condition COND if INVERT is true. */
2709 register_edge_assert_for_2 (tree name
, edge e
,
2710 enum tree_code cond_code
,
2711 tree cond_op0
, tree cond_op1
, bool invert
,
2712 vec
<assert_info
> &asserts
)
2715 enum tree_code comp_code
;
2717 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
2720 invert
, &comp_code
, &val
))
2723 /* Queue the assert. */
2725 if (overflow_comparison_p (comp_code
, name
, val
, false, &x
))
2727 enum tree_code new_code
= ((comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
2728 ? GT_EXPR
: LE_EXPR
);
2729 add_assert_info (asserts
, name
, name
, new_code
, x
);
2731 add_assert_info (asserts
, name
, name
, comp_code
, val
);
2733 /* In the case of NAME <= CST and NAME being defined as
2734 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
2735 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
2736 This catches range and anti-range tests. */
2737 if ((comp_code
== LE_EXPR
2738 || comp_code
== GT_EXPR
)
2739 && TREE_CODE (val
) == INTEGER_CST
2740 && TYPE_UNSIGNED (TREE_TYPE (val
)))
2742 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
2743 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
2745 /* Extract CST2 from the (optional) addition. */
2746 if (is_gimple_assign (def_stmt
)
2747 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
2749 name2
= gimple_assign_rhs1 (def_stmt
);
2750 cst2
= gimple_assign_rhs2 (def_stmt
);
2751 if (TREE_CODE (name2
) == SSA_NAME
2752 && TREE_CODE (cst2
) == INTEGER_CST
)
2753 def_stmt
= SSA_NAME_DEF_STMT (name2
);
2756 /* Extract NAME2 from the (optional) sign-changing cast. */
2757 if (gimple_assign_cast_p (def_stmt
))
2759 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
2760 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
2761 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
2762 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
2763 name3
= gimple_assign_rhs1 (def_stmt
);
2766 /* If name3 is used later, create an ASSERT_EXPR for it. */
2767 if (name3
!= NULL_TREE
2768 && TREE_CODE (name3
) == SSA_NAME
2769 && (cst2
== NULL_TREE
2770 || TREE_CODE (cst2
) == INTEGER_CST
)
2771 && INTEGRAL_TYPE_P (TREE_TYPE (name3
)))
2775 /* Build an expression for the range test. */
2776 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
2777 if (cst2
!= NULL_TREE
)
2778 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
2779 add_assert_info (asserts
, name3
, tmp
, comp_code
, val
);
2782 /* If name2 is used later, create an ASSERT_EXPR for it. */
2783 if (name2
!= NULL_TREE
2784 && TREE_CODE (name2
) == SSA_NAME
2785 && TREE_CODE (cst2
) == INTEGER_CST
2786 && INTEGRAL_TYPE_P (TREE_TYPE (name2
)))
2790 /* Build an expression for the range test. */
2792 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
2793 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
2794 if (cst2
!= NULL_TREE
)
2795 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
2796 add_assert_info (asserts
, name2
, tmp
, comp_code
, val
);
2800 /* In the case of post-in/decrement tests like if (i++) ... and uses
2801 of the in/decremented value on the edge the extra name we want to
2802 assert for is not on the def chain of the name compared. Instead
2803 it is in the set of use stmts.
2804 Similar cases happen for conversions that were simplified through
2805 fold_{sign_changed,widened}_comparison. */
2806 if ((comp_code
== NE_EXPR
2807 || comp_code
== EQ_EXPR
)
2808 && TREE_CODE (val
) == INTEGER_CST
)
2810 imm_use_iterator ui
;
2812 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
2814 if (!is_gimple_assign (use_stmt
))
2817 /* Cut off to use-stmts that are dominating the predecessor. */
2818 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
2821 tree name2
= gimple_assign_lhs (use_stmt
);
2822 if (TREE_CODE (name2
) != SSA_NAME
)
2825 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
2827 if (code
== PLUS_EXPR
2828 || code
== MINUS_EXPR
)
2830 cst
= gimple_assign_rhs2 (use_stmt
);
2831 if (TREE_CODE (cst
) != INTEGER_CST
)
2833 cst
= int_const_binop (code
, val
, cst
);
2835 else if (CONVERT_EXPR_CODE_P (code
))
2837 /* For truncating conversions we cannot record
2839 if (comp_code
== NE_EXPR
2840 && (TYPE_PRECISION (TREE_TYPE (name2
))
2841 < TYPE_PRECISION (TREE_TYPE (name
))))
2843 cst
= fold_convert (TREE_TYPE (name2
), val
);
2848 if (TREE_OVERFLOW_P (cst
))
2849 cst
= drop_tree_overflow (cst
);
2850 add_assert_info (asserts
, name2
, name2
, comp_code
, cst
);
2854 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
2855 && TREE_CODE (val
) == INTEGER_CST
)
2857 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
2858 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
2859 tree val2
= NULL_TREE
;
2860 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
2861 wide_int mask
= wi::zero (prec
);
2862 unsigned int nprec
= prec
;
2863 enum tree_code rhs_code
= ERROR_MARK
;
2865 if (is_gimple_assign (def_stmt
))
2866 rhs_code
= gimple_assign_rhs_code (def_stmt
);
2868 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
2869 assert that A != CST1 -+ CST2. */
2870 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
2871 && (rhs_code
== PLUS_EXPR
|| rhs_code
== MINUS_EXPR
))
2873 tree op0
= gimple_assign_rhs1 (def_stmt
);
2874 tree op1
= gimple_assign_rhs2 (def_stmt
);
2875 if (TREE_CODE (op0
) == SSA_NAME
2876 && TREE_CODE (op1
) == INTEGER_CST
)
2878 enum tree_code reverse_op
= (rhs_code
== PLUS_EXPR
2879 ? MINUS_EXPR
: PLUS_EXPR
);
2880 op1
= int_const_binop (reverse_op
, val
, op1
);
2881 if (TREE_OVERFLOW (op1
))
2882 op1
= drop_tree_overflow (op1
);
2883 add_assert_info (asserts
, op0
, op0
, comp_code
, op1
);
2887 /* Add asserts for NAME cmp CST and NAME being defined
2888 as NAME = (int) NAME2. */
2889 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
2890 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
2891 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
2892 && gimple_assign_cast_p (def_stmt
))
2894 name2
= gimple_assign_rhs1 (def_stmt
);
2895 if (CONVERT_EXPR_CODE_P (rhs_code
)
2896 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
2897 && TYPE_UNSIGNED (TREE_TYPE (name2
))
2898 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
2899 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
2900 || !tree_int_cst_equal (val
,
2901 TYPE_MIN_VALUE (TREE_TYPE (val
)))))
2904 enum tree_code new_comp_code
= comp_code
;
2906 cst
= fold_convert (TREE_TYPE (name2
),
2907 TYPE_MIN_VALUE (TREE_TYPE (val
)));
2908 /* Build an expression for the range test. */
2909 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
2910 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
2911 fold_convert (TREE_TYPE (name2
), val
));
2912 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
2914 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
2915 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
2916 build_int_cst (TREE_TYPE (name2
), 1));
2918 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, cst
);
2922 /* Add asserts for NAME cmp CST and NAME being defined as
2923 NAME = NAME2 >> CST2.
2925 Extract CST2 from the right shift. */
2926 if (rhs_code
== RSHIFT_EXPR
)
2928 name2
= gimple_assign_rhs1 (def_stmt
);
2929 cst2
= gimple_assign_rhs2 (def_stmt
);
2930 if (TREE_CODE (name2
) == SSA_NAME
2931 && tree_fits_uhwi_p (cst2
)
2932 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
2933 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
2934 && type_has_mode_precision_p (TREE_TYPE (val
)))
2936 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
2937 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
2940 if (val2
!= NULL_TREE
2941 && TREE_CODE (val2
) == INTEGER_CST
2942 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
2946 enum tree_code new_comp_code
= comp_code
;
2950 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
2952 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
2954 tree type
= build_nonstandard_integer_type (prec
, 1);
2955 tmp
= build1 (NOP_EXPR
, type
, name2
);
2956 val2
= fold_convert (type
, val2
);
2958 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
2959 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
2960 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
2962 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
2965 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
2967 if (minval
== wi::to_wide (new_val
))
2968 new_val
= NULL_TREE
;
2973 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
2974 mask
|= wi::to_wide (val2
);
2975 if (wi::eq_p (mask
, maxval
))
2976 new_val
= NULL_TREE
;
2978 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
2982 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, new_val
);
2985 /* Add asserts for NAME cmp CST and NAME being defined as
2986 NAME = NAME2 & CST2.
2988 Extract CST2 from the and.
2991 NAME = (unsigned) NAME2;
2992 casts where NAME's type is unsigned and has smaller precision
2993 than NAME2's type as if it was NAME = NAME2 & MASK. */
2994 names
[0] = NULL_TREE
;
2995 names
[1] = NULL_TREE
;
2997 if (rhs_code
== BIT_AND_EXPR
2998 || (CONVERT_EXPR_CODE_P (rhs_code
)
2999 && INTEGRAL_TYPE_P (TREE_TYPE (val
))
3000 && TYPE_UNSIGNED (TREE_TYPE (val
))
3001 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
3004 name2
= gimple_assign_rhs1 (def_stmt
);
3005 if (rhs_code
== BIT_AND_EXPR
)
3006 cst2
= gimple_assign_rhs2 (def_stmt
);
3009 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
3010 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
3012 if (TREE_CODE (name2
) == SSA_NAME
3013 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
3014 && TREE_CODE (cst2
) == INTEGER_CST
3015 && !integer_zerop (cst2
)
3017 || TYPE_UNSIGNED (TREE_TYPE (val
))))
3019 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
3020 if (gimple_assign_cast_p (def_stmt2
))
3022 names
[1] = gimple_assign_rhs1 (def_stmt2
);
3023 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
3024 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
3025 || (TYPE_PRECISION (TREE_TYPE (name2
))
3026 != TYPE_PRECISION (TREE_TYPE (names
[1]))))
3027 names
[1] = NULL_TREE
;
3032 if (names
[0] || names
[1])
3034 wide_int minv
, maxv
, valv
, cst2v
;
3035 wide_int tem
, sgnbit
;
3036 bool valid_p
= false, valn
, cst2n
;
3037 enum tree_code ccode
= comp_code
;
3039 valv
= wide_int::from (wi::to_wide (val
), nprec
, UNSIGNED
);
3040 cst2v
= wide_int::from (wi::to_wide (cst2
), nprec
, UNSIGNED
);
3041 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
3042 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
3043 /* If CST2 doesn't have most significant bit set,
3044 but VAL is negative, we have comparison like
3045 if ((x & 0x123) > -4) (always true). Just give up. */
3049 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
3051 sgnbit
= wi::zero (nprec
);
3052 minv
= valv
& cst2v
;
3056 /* Minimum unsigned value for equality is VAL & CST2
3057 (should be equal to VAL, otherwise we probably should
3058 have folded the comparison into false) and
3059 maximum unsigned value is VAL | ~CST2. */
3060 maxv
= valv
| ~cst2v
;
3065 tem
= valv
| ~cst2v
;
3066 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
3070 sgnbit
= wi::zero (nprec
);
3073 /* If (VAL | ~CST2) is all ones, handle it as
3074 (X & CST2) < VAL. */
3079 sgnbit
= wi::zero (nprec
);
3082 if (!cst2n
&& wi::neg_p (cst2v
))
3083 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
3092 if (tem
== wi::mask (nprec
- 1, false, nprec
))
3098 sgnbit
= wi::zero (nprec
);
3103 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
3104 is VAL and maximum unsigned value is ~0. For signed
3105 comparison, if CST2 doesn't have most significant bit
3106 set, handle it similarly. If CST2 has MSB set,
3107 the minimum is the same, and maximum is ~0U/2. */
3110 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
3112 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3116 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
3122 /* Find out smallest MINV where MINV > VAL
3123 && (MINV & CST2) == MINV, if any. If VAL is signed and
3124 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
3125 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3128 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
3133 /* Minimum unsigned value for <= is 0 and maximum
3134 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
3135 Otherwise, find smallest VAL2 where VAL2 > VAL
3136 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3138 For signed comparison, if CST2 doesn't have most
3139 significant bit set, handle it similarly. If CST2 has
3140 MSB set, the maximum is the same and minimum is INT_MIN. */
3145 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3157 /* Minimum unsigned value for < is 0 and maximum
3158 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
3159 Otherwise, find smallest VAL2 where VAL2 > VAL
3160 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3162 For signed comparison, if CST2 doesn't have most
3163 significant bit set, handle it similarly. If CST2 has
3164 MSB set, the maximum is the same and minimum is INT_MIN. */
3173 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3187 && (maxv
- minv
) != -1)
3189 tree tmp
, new_val
, type
;
3192 for (i
= 0; i
< 2; i
++)
3195 wide_int maxv2
= maxv
;
3197 type
= TREE_TYPE (names
[i
]);
3198 if (!TYPE_UNSIGNED (type
))
3200 type
= build_nonstandard_integer_type (nprec
, 1);
3201 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
3205 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
3206 wide_int_to_tree (type
, -minv
));
3207 maxv2
= maxv
- minv
;
3209 new_val
= wide_int_to_tree (type
, maxv2
);
3210 add_assert_info (asserts
, names
[i
], tmp
, LE_EXPR
, new_val
);
3217 /* OP is an operand of a truth value expression which is known to have
3218 a particular value. Register any asserts for OP and for any
3219 operands in OP's defining statement.
3221 If CODE is EQ_EXPR, then we want to register OP is zero (false),
3222 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
3225 register_edge_assert_for_1 (tree op
, enum tree_code code
,
3226 edge e
, vec
<assert_info
> &asserts
)
3230 enum tree_code rhs_code
;
3232 /* We only care about SSA_NAMEs. */
3233 if (TREE_CODE (op
) != SSA_NAME
)
3236 /* We know that OP will have a zero or nonzero value. */
3237 val
= build_int_cst (TREE_TYPE (op
), 0);
3238 add_assert_info (asserts
, op
, op
, code
, val
);
3240 /* Now look at how OP is set. If it's set from a comparison,
3241 a truth operation or some bit operations, then we may be able
3242 to register information about the operands of that assignment. */
3243 op_def
= SSA_NAME_DEF_STMT (op
);
3244 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
3247 rhs_code
= gimple_assign_rhs_code (op_def
);
3249 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
3251 bool invert
= (code
== EQ_EXPR
? true : false);
3252 tree op0
= gimple_assign_rhs1 (op_def
);
3253 tree op1
= gimple_assign_rhs2 (op_def
);
3255 if (TREE_CODE (op0
) == SSA_NAME
)
3256 register_edge_assert_for_2 (op0
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
3257 if (TREE_CODE (op1
) == SSA_NAME
)
3258 register_edge_assert_for_2 (op1
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
3260 else if ((code
== NE_EXPR
3261 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
3263 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
3265 /* Recurse on each operand. */
3266 tree op0
= gimple_assign_rhs1 (op_def
);
3267 tree op1
= gimple_assign_rhs2 (op_def
);
3268 if (TREE_CODE (op0
) == SSA_NAME
3269 && has_single_use (op0
))
3270 register_edge_assert_for_1 (op0
, code
, e
, asserts
);
3271 if (TREE_CODE (op1
) == SSA_NAME
3272 && has_single_use (op1
))
3273 register_edge_assert_for_1 (op1
, code
, e
, asserts
);
3275 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
3276 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
3278 /* Recurse, flipping CODE. */
3279 code
= invert_tree_comparison (code
, false);
3280 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
3282 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
3284 /* Recurse through the copy. */
3285 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
3287 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
3289 /* Recurse through the type conversion, unless it is a narrowing
3290 conversion or conversion from non-integral type. */
3291 tree rhs
= gimple_assign_rhs1 (op_def
);
3292 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
3293 && (TYPE_PRECISION (TREE_TYPE (rhs
))
3294 <= TYPE_PRECISION (TREE_TYPE (op
))))
3295 register_edge_assert_for_1 (rhs
, code
, e
, asserts
);
3299 /* Check if comparison
3300 NAME COND_OP INTEGER_CST
3302 (X & 11...100..0) COND_OP XX...X00...0
3303 Such comparison can yield assertions like
3306 in case of COND_OP being EQ_EXPR or
3309 in case of NE_EXPR. */
3312 is_masked_range_test (tree name
, tree valt
, enum tree_code cond_code
,
3313 tree
*new_name
, tree
*low
, enum tree_code
*low_code
,
3314 tree
*high
, enum tree_code
*high_code
)
3316 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3318 if (!is_gimple_assign (def_stmt
)
3319 || gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
3322 tree t
= gimple_assign_rhs1 (def_stmt
);
3323 tree maskt
= gimple_assign_rhs2 (def_stmt
);
3324 if (TREE_CODE (t
) != SSA_NAME
|| TREE_CODE (maskt
) != INTEGER_CST
)
3327 wi::tree_to_wide_ref mask
= wi::to_wide (maskt
);
3328 wide_int inv_mask
= ~mask
;
3329 /* Must have been removed by now so don't bother optimizing. */
3330 if (mask
== 0 || inv_mask
== 0)
3333 /* Assume VALT is INTEGER_CST. */
3334 wi::tree_to_wide_ref val
= wi::to_wide (valt
);
3336 if ((inv_mask
& (inv_mask
+ 1)) != 0
3337 || (val
& mask
) != val
)
3340 bool is_range
= cond_code
== EQ_EXPR
;
3342 tree type
= TREE_TYPE (t
);
3343 wide_int min
= wi::min_value (type
),
3344 max
= wi::max_value (type
);
3348 *low_code
= val
== min
? ERROR_MARK
: GE_EXPR
;
3349 *high_code
= val
== max
? ERROR_MARK
: LE_EXPR
;
3353 /* We can still generate assertion if one of alternatives
3354 is known to always be false. */
3357 *low_code
= (enum tree_code
) 0;
3358 *high_code
= GT_EXPR
;
3360 else if ((val
| inv_mask
) == max
)
3362 *low_code
= LT_EXPR
;
3363 *high_code
= (enum tree_code
) 0;
3370 *low
= wide_int_to_tree (type
, val
);
3371 *high
= wide_int_to_tree (type
, val
| inv_mask
);
3376 /* Try to register an edge assertion for SSA name NAME on edge E for
3377 the condition COND contributing to the conditional jump pointed to by
3381 register_edge_assert_for (tree name
, edge e
,
3382 enum tree_code cond_code
, tree cond_op0
,
3383 tree cond_op1
, vec
<assert_info
> &asserts
)
3386 enum tree_code comp_code
;
3387 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
3389 /* Do not attempt to infer anything in names that flow through
3391 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
3394 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
3400 /* Register ASSERT_EXPRs for name. */
3401 register_edge_assert_for_2 (name
, e
, cond_code
, cond_op0
,
3402 cond_op1
, is_else_edge
, asserts
);
3405 /* If COND is effectively an equality test of an SSA_NAME against
3406 the value zero or one, then we may be able to assert values
3407 for SSA_NAMEs which flow into COND. */
3409 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
3410 statement of NAME we can assert both operands of the BIT_AND_EXPR
3411 have nonzero value. */
3412 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
3413 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
3415 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3417 if (is_gimple_assign (def_stmt
)
3418 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
3420 tree op0
= gimple_assign_rhs1 (def_stmt
);
3421 tree op1
= gimple_assign_rhs2 (def_stmt
);
3422 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, asserts
);
3423 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, asserts
);
3427 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
3428 statement of NAME we can assert both operands of the BIT_IOR_EXPR
3430 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
3431 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
3433 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
3435 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
3436 necessarily zero value, or if type-precision is one. */
3437 if (is_gimple_assign (def_stmt
)
3438 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
3439 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
3440 || comp_code
== EQ_EXPR
)))
3442 tree op0
= gimple_assign_rhs1 (def_stmt
);
3443 tree op1
= gimple_assign_rhs2 (def_stmt
);
3444 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, asserts
);
3445 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, asserts
);
3449 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
3450 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
3451 && TREE_CODE (val
) == INTEGER_CST
)
3453 enum tree_code low_code
, high_code
;
3455 if (is_masked_range_test (name
, val
, comp_code
, &name
, &low
,
3456 &low_code
, &high
, &high_code
))
3458 if (low_code
!= ERROR_MARK
)
3459 register_edge_assert_for_2 (name
, e
, low_code
, name
,
3460 low
, /*invert*/false, asserts
);
3461 if (high_code
!= ERROR_MARK
)
3462 register_edge_assert_for_2 (name
, e
, high_code
, name
,
3463 high
, /*invert*/false, asserts
);
3468 /* Finish found ASSERTS for E and register them at GSI. */
3471 finish_register_edge_assert_for (edge e
, gimple_stmt_iterator gsi
,
3472 vec
<assert_info
> &asserts
)
3474 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
3475 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
3476 reachable from E. */
3477 if (live_on_edge (e
, asserts
[i
].name
))
3478 register_new_assert_for (asserts
[i
].name
, asserts
[i
].expr
,
3479 asserts
[i
].comp_code
, asserts
[i
].val
,
3485 /* Determine whether the outgoing edges of BB should receive an
3486 ASSERT_EXPR for each of the operands of BB's LAST statement.
3487 The last statement of BB must be a COND_EXPR.
3489 If any of the sub-graphs rooted at BB have an interesting use of
3490 the predicate operands, an assert location node is added to the
3491 list of assertions for the corresponding operands. */
3494 find_conditional_asserts (basic_block bb
, gcond
*last
)
3496 gimple_stmt_iterator bsi
;
3502 bsi
= gsi_for_stmt (last
);
3504 /* Look for uses of the operands in each of the sub-graphs
3505 rooted at BB. We need to check each of the outgoing edges
3506 separately, so that we know what kind of ASSERT_EXPR to
3508 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3513 /* Register the necessary assertions for each operand in the
3514 conditional predicate. */
3515 auto_vec
<assert_info
, 8> asserts
;
3516 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
3517 register_edge_assert_for (op
, e
,
3518 gimple_cond_code (last
),
3519 gimple_cond_lhs (last
),
3520 gimple_cond_rhs (last
), asserts
);
3521 finish_register_edge_assert_for (e
, bsi
, asserts
);
3531 /* Compare two case labels sorting first by the destination bb index
3532 and then by the case value. */
3535 compare_case_labels (const void *p1
, const void *p2
)
3537 const struct case_info
*ci1
= (const struct case_info
*) p1
;
3538 const struct case_info
*ci2
= (const struct case_info
*) p2
;
3539 int idx1
= ci1
->bb
->index
;
3540 int idx2
= ci2
->bb
->index
;
3544 else if (idx1
== idx2
)
3546 /* Make sure the default label is first in a group. */
3547 if (!CASE_LOW (ci1
->expr
))
3549 else if (!CASE_LOW (ci2
->expr
))
3552 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
3553 CASE_LOW (ci2
->expr
));
3559 /* Determine whether the outgoing edges of BB should receive an
3560 ASSERT_EXPR for each of the operands of BB's LAST statement.
3561 The last statement of BB must be a SWITCH_EXPR.
3563 If any of the sub-graphs rooted at BB have an interesting use of
3564 the predicate operands, an assert location node is added to the
3565 list of assertions for the corresponding operands. */
3568 find_switch_asserts (basic_block bb
, gswitch
*last
)
3570 gimple_stmt_iterator bsi
;
3573 struct case_info
*ci
;
3574 size_t n
= gimple_switch_num_labels (last
);
3575 #if GCC_VERSION >= 4000
3578 /* Work around GCC 3.4 bug (PR 37086). */
3579 volatile unsigned int idx
;
3582 bsi
= gsi_for_stmt (last
);
3583 op
= gimple_switch_index (last
);
3584 if (TREE_CODE (op
) != SSA_NAME
)
3587 /* Build a vector of case labels sorted by destination label. */
3588 ci
= XNEWVEC (struct case_info
, n
);
3589 for (idx
= 0; idx
< n
; ++idx
)
3591 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
3592 ci
[idx
].bb
= label_to_block (cfun
, CASE_LABEL (ci
[idx
].expr
));
3594 edge default_edge
= find_edge (bb
, ci
[0].bb
);
3595 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
3597 for (idx
= 0; idx
< n
; ++idx
)
3600 tree cl
= ci
[idx
].expr
;
3601 basic_block cbb
= ci
[idx
].bb
;
3603 min
= CASE_LOW (cl
);
3604 max
= CASE_HIGH (cl
);
3606 /* If there are multiple case labels with the same destination
3607 we need to combine them to a single value range for the edge. */
3608 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
3610 /* Skip labels until the last of the group. */
3613 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
3616 /* Pick up the maximum of the case label range. */
3617 if (CASE_HIGH (ci
[idx
].expr
))
3618 max
= CASE_HIGH (ci
[idx
].expr
);
3620 max
= CASE_LOW (ci
[idx
].expr
);
3623 /* Can't extract a useful assertion out of a range that includes the
3625 if (min
== NULL_TREE
)
3628 /* Find the edge to register the assert expr on. */
3629 e
= find_edge (bb
, cbb
);
3631 /* Register the necessary assertions for the operand in the
3633 auto_vec
<assert_info
, 8> asserts
;
3634 register_edge_assert_for (op
, e
,
3635 max
? GE_EXPR
: EQ_EXPR
,
3636 op
, fold_convert (TREE_TYPE (op
), min
),
3639 register_edge_assert_for (op
, e
, LE_EXPR
, op
,
3640 fold_convert (TREE_TYPE (op
), max
),
3642 finish_register_edge_assert_for (e
, bsi
, asserts
);
3647 if (!live_on_edge (default_edge
, op
))
3650 /* Now register along the default label assertions that correspond to the
3651 anti-range of each label. */
3652 int insertion_limit
= PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS
);
3653 if (insertion_limit
== 0)
3656 /* We can't do this if the default case shares a label with another case. */
3657 tree default_cl
= gimple_switch_default_label (last
);
3658 for (idx
= 1; idx
< n
; idx
++)
3661 tree cl
= gimple_switch_label (last
, idx
);
3662 if (CASE_LABEL (cl
) == CASE_LABEL (default_cl
))
3665 min
= CASE_LOW (cl
);
3666 max
= CASE_HIGH (cl
);
3668 /* Combine contiguous case ranges to reduce the number of assertions
3670 for (idx
= idx
+ 1; idx
< n
; idx
++)
3672 tree next_min
, next_max
;
3673 tree next_cl
= gimple_switch_label (last
, idx
);
3674 if (CASE_LABEL (next_cl
) == CASE_LABEL (default_cl
))
3677 next_min
= CASE_LOW (next_cl
);
3678 next_max
= CASE_HIGH (next_cl
);
3680 wide_int difference
= (wi::to_wide (next_min
)
3681 - wi::to_wide (max
? max
: min
));
3682 if (wi::eq_p (difference
, 1))
3683 max
= next_max
? next_max
: next_min
;
3689 if (max
== NULL_TREE
)
3691 /* Register the assertion OP != MIN. */
3692 auto_vec
<assert_info
, 8> asserts
;
3693 min
= fold_convert (TREE_TYPE (op
), min
);
3694 register_edge_assert_for (op
, default_edge
, NE_EXPR
, op
, min
,
3696 finish_register_edge_assert_for (default_edge
, bsi
, asserts
);
3700 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
3701 which will give OP the anti-range ~[MIN,MAX]. */
3702 tree uop
= fold_convert (unsigned_type_for (TREE_TYPE (op
)), op
);
3703 min
= fold_convert (TREE_TYPE (uop
), min
);
3704 max
= fold_convert (TREE_TYPE (uop
), max
);
3706 tree lhs
= fold_build2 (MINUS_EXPR
, TREE_TYPE (uop
), uop
, min
);
3707 tree rhs
= int_const_binop (MINUS_EXPR
, max
, min
);
3708 register_new_assert_for (op
, lhs
, GT_EXPR
, rhs
,
3709 NULL
, default_edge
, bsi
);
3712 if (--insertion_limit
== 0)
3718 /* Traverse all the statements in block BB looking for statements that
3719 may generate useful assertions for the SSA names in their operand.
3720 If a statement produces a useful assertion A for name N_i, then the
3721 list of assertions already generated for N_i is scanned to
3722 determine if A is actually needed.
3724 If N_i already had the assertion A at a location dominating the
3725 current location, then nothing needs to be done. Otherwise, the
3726 new location for A is recorded instead.
3728 1- For every statement S in BB, all the variables used by S are
3729 added to bitmap FOUND_IN_SUBGRAPH.
3731 2- If statement S uses an operand N in a way that exposes a known
3732 value range for N, then if N was not already generated by an
3733 ASSERT_EXPR, create a new assert location for N. For instance,
3734 if N is a pointer and the statement dereferences it, we can
3735 assume that N is not NULL.
3737 3- COND_EXPRs are a special case of #2. We can derive range
3738 information from the predicate but need to insert different
3739 ASSERT_EXPRs for each of the sub-graphs rooted at the
3740 conditional block. If the last statement of BB is a conditional
3741 expression of the form 'X op Y', then
3743 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
3745 b) If the conditional is the only entry point to the sub-graph
3746 corresponding to the THEN_CLAUSE, recurse into it. On
3747 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
3748 an ASSERT_EXPR is added for the corresponding variable.
3750 c) Repeat step (b) on the ELSE_CLAUSE.
3752 d) Mark X and Y in FOUND_IN_SUBGRAPH.
3761 In this case, an assertion on the THEN clause is useful to
3762 determine that 'a' is always 9 on that edge. However, an assertion
3763 on the ELSE clause would be unnecessary.
3765 4- If BB does not end in a conditional expression, then we recurse
3766 into BB's dominator children.
3768 At the end of the recursive traversal, every SSA name will have a
3769 list of locations where ASSERT_EXPRs should be added. When a new
3770 location for name N is found, it is registered by calling
3771 register_new_assert_for. That function keeps track of all the
3772 registered assertions to prevent adding unnecessary assertions.
3773 For instance, if a pointer P_4 is dereferenced more than once in a
3774 dominator tree, only the location dominating all the dereference of
3775 P_4 will receive an ASSERT_EXPR. */
3778 find_assert_locations_1 (basic_block bb
, sbitmap live
)
3782 last
= last_stmt (bb
);
3784 /* If BB's last statement is a conditional statement involving integer
3785 operands, determine if we need to add ASSERT_EXPRs. */
3787 && gimple_code (last
) == GIMPLE_COND
3788 && !fp_predicate (last
)
3789 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
3790 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
3792 /* If BB's last statement is a switch statement involving integer
3793 operands, determine if we need to add ASSERT_EXPRs. */
3795 && gimple_code (last
) == GIMPLE_SWITCH
3796 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
3797 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
3799 /* Traverse all the statements in BB marking used names and looking
3800 for statements that may infer assertions for their used operands. */
3801 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
3808 stmt
= gsi_stmt (si
);
3810 if (is_gimple_debug (stmt
))
3813 /* See if we can derive an assertion for any of STMT's operands. */
3814 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
3817 enum tree_code comp_code
;
3819 /* If op is not live beyond this stmt, do not bother to insert
3821 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
3824 /* If OP is used in such a way that we can infer a value
3825 range for it, and we don't find a previous assertion for
3826 it, create a new assertion location node for OP. */
3827 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
3829 /* If we are able to infer a nonzero value range for OP,
3830 then walk backwards through the use-def chain to see if OP
3831 was set via a typecast.
3833 If so, then we can also infer a nonzero value range
3834 for the operand of the NOP_EXPR. */
3835 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
3838 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
3840 while (is_gimple_assign (def_stmt
)
3841 && CONVERT_EXPR_CODE_P
3842 (gimple_assign_rhs_code (def_stmt
))
3844 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
3846 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
3848 t
= gimple_assign_rhs1 (def_stmt
);
3849 def_stmt
= SSA_NAME_DEF_STMT (t
);
3851 /* Note we want to register the assert for the
3852 operand of the NOP_EXPR after SI, not after the
3854 if (bitmap_bit_p (live
, SSA_NAME_VERSION (t
)))
3855 register_new_assert_for (t
, t
, comp_code
, value
,
3860 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
3865 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
3866 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
3867 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
3868 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
3871 /* Traverse all PHI nodes in BB, updating live. */
3872 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
3875 use_operand_p arg_p
;
3877 gphi
*phi
= si
.phi ();
3878 tree res
= gimple_phi_result (phi
);
3880 if (virtual_operand_p (res
))
3883 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
3885 tree arg
= USE_FROM_PTR (arg_p
);
3886 if (TREE_CODE (arg
) == SSA_NAME
)
3887 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
3890 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
3894 /* Do an RPO walk over the function computing SSA name liveness
3895 on-the-fly and deciding on assert expressions to insert. */
3898 find_assert_locations (void)
3900 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
3901 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
3902 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
3905 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
3906 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
3907 for (i
= 0; i
< rpo_cnt
; ++i
)
3910 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
3911 the order we compute liveness and insert asserts we otherwise
3912 fail to insert asserts into the loop latch. */
3914 FOR_EACH_LOOP (loop
, 0)
3916 i
= loop
->latch
->index
;
3917 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
3918 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
3919 !gsi_end_p (gsi
); gsi_next (&gsi
))
3921 gphi
*phi
= gsi
.phi ();
3922 if (virtual_operand_p (gimple_phi_result (phi
)))
3924 tree arg
= gimple_phi_arg_def (phi
, j
);
3925 if (TREE_CODE (arg
) == SSA_NAME
)
3927 if (live
[i
] == NULL
)
3929 live
[i
] = sbitmap_alloc (num_ssa_names
);
3930 bitmap_clear (live
[i
]);
3932 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
3937 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
3939 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
3945 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
3946 bitmap_clear (live
[rpo
[i
]]);
3949 /* Process BB and update the live information with uses in
3951 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
3953 /* Merge liveness into the predecessor blocks and free it. */
3954 if (!bitmap_empty_p (live
[rpo
[i
]]))
3957 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3959 int pred
= e
->src
->index
;
3960 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
3965 live
[pred
] = sbitmap_alloc (num_ssa_names
);
3966 bitmap_clear (live
[pred
]);
3968 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
3970 if (bb_rpo
[pred
] < pred_rpo
)
3971 pred_rpo
= bb_rpo
[pred
];
3974 /* Record the RPO number of the last visited block that needs
3975 live information from this block. */
3976 last_rpo
[rpo
[i
]] = pred_rpo
;
3980 sbitmap_free (live
[rpo
[i
]]);
3981 live
[rpo
[i
]] = NULL
;
3984 /* We can free all successors live bitmaps if all their
3985 predecessors have been visited already. */
3986 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3987 if (last_rpo
[e
->dest
->index
] == i
3988 && live
[e
->dest
->index
])
3990 sbitmap_free (live
[e
->dest
->index
]);
3991 live
[e
->dest
->index
] = NULL
;
3996 XDELETEVEC (bb_rpo
);
3997 XDELETEVEC (last_rpo
);
3998 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
4000 sbitmap_free (live
[i
]);
4004 /* Create an ASSERT_EXPR for NAME and insert it in the location
4005 indicated by LOC. Return true if we made any edge insertions. */
4008 process_assert_insertions_for (tree name
, assert_locus
*loc
)
4010 /* Build the comparison expression NAME_i COMP_CODE VAL. */
4013 gimple
*assert_stmt
;
4017 /* If we have X <=> X do not insert an assert expr for that. */
4018 if (loc
->expr
== loc
->val
)
4021 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
4022 assert_stmt
= build_assert_expr_for (cond
, name
);
4025 /* We have been asked to insert the assertion on an edge. This
4026 is used only by COND_EXPR and SWITCH_EXPR assertions. */
4027 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
4028 || (gimple_code (gsi_stmt (loc
->si
))
4031 gsi_insert_on_edge (loc
->e
, assert_stmt
);
4035 /* If the stmt iterator points at the end then this is an insertion
4036 at the beginning of a block. */
4037 if (gsi_end_p (loc
->si
))
4039 gimple_stmt_iterator si
= gsi_after_labels (loc
->bb
);
4040 gsi_insert_before (&si
, assert_stmt
, GSI_SAME_STMT
);
4044 /* Otherwise, we can insert right after LOC->SI iff the
4045 statement must not be the last statement in the block. */
4046 stmt
= gsi_stmt (loc
->si
);
4047 if (!stmt_ends_bb_p (stmt
))
4049 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
4053 /* If STMT must be the last statement in BB, we can only insert new
4054 assertions on the non-abnormal edge out of BB. Note that since
4055 STMT is not control flow, there may only be one non-abnormal/eh edge
4057 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
4058 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
4060 gsi_insert_on_edge (e
, assert_stmt
);
4067 /* Qsort helper for sorting assert locations. If stable is true, don't
4068 use iterative_hash_expr because it can be unstable for -fcompare-debug,
4069 on the other side some pointers might be NULL. */
4071 template <bool stable
>
4073 compare_assert_loc (const void *pa
, const void *pb
)
4075 assert_locus
* const a
= *(assert_locus
* const *)pa
;
4076 assert_locus
* const b
= *(assert_locus
* const *)pb
;
4078 /* If stable, some asserts might be optimized away already, sort
4088 if (a
->e
== NULL
&& b
->e
!= NULL
)
4090 else if (a
->e
!= NULL
&& b
->e
== NULL
)
4093 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
4094 no need to test both a->e and b->e. */
4096 /* Sort after destination index. */
4099 else if (a
->e
->dest
->index
> b
->e
->dest
->index
)
4101 else if (a
->e
->dest
->index
< b
->e
->dest
->index
)
4104 /* Sort after comp_code. */
4105 if (a
->comp_code
> b
->comp_code
)
4107 else if (a
->comp_code
< b
->comp_code
)
4112 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
4113 uses DECL_UID of the VAR_DECL, so sorting might differ between
4114 -g and -g0. When doing the removal of redundant assert exprs
4115 and commonization to successors, this does not matter, but for
4116 the final sort needs to be stable. */
4124 ha
= iterative_hash_expr (a
->expr
, iterative_hash_expr (a
->val
, 0));
4125 hb
= iterative_hash_expr (b
->expr
, iterative_hash_expr (b
->val
, 0));
4128 /* Break the tie using hashing and source/bb index. */
4130 return (a
->e
!= NULL
4131 ? a
->e
->src
->index
- b
->e
->src
->index
4132 : a
->bb
->index
- b
->bb
->index
);
4133 return ha
> hb
? 1 : -1;
4136 /* Process all the insertions registered for every name N_i registered
4137 in NEED_ASSERT_FOR. The list of assertions to be inserted are
4138 found in ASSERTS_FOR[i]. */
4141 process_assert_insertions (void)
4145 bool update_edges_p
= false;
4146 int num_asserts
= 0;
4148 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4149 dump_all_asserts (dump_file
);
4151 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4153 assert_locus
*loc
= asserts_for
[i
];
4156 auto_vec
<assert_locus
*, 16> asserts
;
4157 for (; loc
; loc
= loc
->next
)
4158 asserts
.safe_push (loc
);
4159 asserts
.qsort (compare_assert_loc
<false>);
4161 /* Push down common asserts to successors and remove redundant ones. */
4163 assert_locus
*common
= NULL
;
4164 unsigned commonj
= 0;
4165 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
4171 || loc
->e
->dest
!= common
->e
->dest
4172 || loc
->comp_code
!= common
->comp_code
4173 || ! operand_equal_p (loc
->val
, common
->val
, 0)
4174 || ! operand_equal_p (loc
->expr
, common
->expr
, 0))
4180 else if (loc
->e
== asserts
[j
-1]->e
)
4182 /* Remove duplicate asserts. */
4183 if (commonj
== j
- 1)
4188 free (asserts
[j
-1]);
4189 asserts
[j
-1] = NULL
;
4194 if (EDGE_COUNT (common
->e
->dest
->preds
) == ecnt
)
4196 /* We have the same assertion on all incoming edges of a BB.
4197 Insert it at the beginning of that block. */
4198 loc
->bb
= loc
->e
->dest
;
4200 loc
->si
= gsi_none ();
4202 /* Clear asserts commoned. */
4203 for (; commonj
!= j
; ++commonj
)
4204 if (asserts
[commonj
])
4206 free (asserts
[commonj
]);
4207 asserts
[commonj
] = NULL
;
4213 /* The asserts vector sorting above might be unstable for
4214 -fcompare-debug, sort again to ensure a stable sort. */
4215 asserts
.qsort (compare_assert_loc
<true>);
4216 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
4221 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
4228 gsi_commit_edge_inserts ();
4230 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
4235 /* Traverse the flowgraph looking for conditional jumps to insert range
4236 expressions. These range expressions are meant to provide information
4237 to optimizations that need to reason in terms of value ranges. They
4238 will not be expanded into RTL. For instance, given:
4247 this pass will transform the code into:
4253 x = ASSERT_EXPR <x, x < y>
4258 y = ASSERT_EXPR <y, x >= y>
4262 The idea is that once copy and constant propagation have run, other
4263 optimizations will be able to determine what ranges of values can 'x'
4264 take in different paths of the code, simply by checking the reaching
4265 definition of 'x'. */
4268 insert_range_assertions (void)
4270 need_assert_for
= BITMAP_ALLOC (NULL
);
4271 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
4273 calculate_dominance_info (CDI_DOMINATORS
);
4275 find_assert_locations ();
4276 if (!bitmap_empty_p (need_assert_for
))
4278 process_assert_insertions ();
4279 update_ssa (TODO_update_ssa_no_phi
);
4282 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4284 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
4285 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
4289 BITMAP_FREE (need_assert_for
);
4292 class vrp_prop
: public ssa_propagation_engine
4295 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) FINAL OVERRIDE
;
4296 enum ssa_prop_result
visit_phi (gphi
*) FINAL OVERRIDE
;
4298 void vrp_initialize (void);
4299 void vrp_finalize (bool);
4300 void check_all_array_refs (void);
4301 void check_array_ref (location_t
, tree
, bool);
4302 void check_mem_ref (location_t
, tree
, bool);
4303 void search_for_addr_array (tree
, location_t
);
4305 class vr_values vr_values
;
4306 /* Temporary delegator to minimize code churn. */
4307 value_range
*get_value_range (const_tree op
)
4308 { return vr_values
.get_value_range (op
); }
4309 void set_defs_to_varying (gimple
*stmt
)
4310 { return vr_values
.set_defs_to_varying (stmt
); }
4311 void extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
4312 tree
*output_p
, value_range
*vr
)
4313 { vr_values
.extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, vr
); }
4314 bool update_value_range (const_tree op
, value_range
*vr
)
4315 { return vr_values
.update_value_range (op
, vr
); }
4316 void extract_range_basic (value_range
*vr
, gimple
*stmt
)
4317 { vr_values
.extract_range_basic (vr
, stmt
); }
4318 void extract_range_from_phi_node (gphi
*phi
, value_range
*vr
)
4319 { vr_values
.extract_range_from_phi_node (phi
, vr
); }
4321 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
4322 and "struct" hacks. If VRP can determine that the
4323 array subscript is a constant, check if it is outside valid
4324 range. If the array subscript is a RANGE, warn if it is
4325 non-overlapping with valid range.
4326 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
4329 vrp_prop::check_array_ref (location_t location
, tree ref
,
4330 bool ignore_off_by_one
)
4332 const value_range
*vr
= NULL
;
4333 tree low_sub
, up_sub
;
4334 tree low_bound
, up_bound
, up_bound_p1
;
4336 if (TREE_NO_WARNING (ref
))
4339 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
4340 up_bound
= array_ref_up_bound (ref
);
4343 || TREE_CODE (up_bound
) != INTEGER_CST
4344 || (warn_array_bounds
< 2
4345 && array_at_struct_end_p (ref
)))
4347 /* Accesses to trailing arrays via pointers may access storage
4348 beyond the types array bounds. For such arrays, or for flexible
4349 array members, as well as for other arrays of an unknown size,
4350 replace the upper bound with a more permissive one that assumes
4351 the size of the largest object is PTRDIFF_MAX. */
4352 tree eltsize
= array_ref_element_size (ref
);
4354 if (TREE_CODE (eltsize
) != INTEGER_CST
4355 || integer_zerop (eltsize
))
4357 up_bound
= NULL_TREE
;
4358 up_bound_p1
= NULL_TREE
;
4362 tree maxbound
= TYPE_MAX_VALUE (ptrdiff_type_node
);
4363 tree arg
= TREE_OPERAND (ref
, 0);
4366 if (get_addr_base_and_unit_offset (arg
, &off
) && known_gt (off
, 0))
4367 maxbound
= wide_int_to_tree (sizetype
,
4368 wi::sub (wi::to_wide (maxbound
),
4371 maxbound
= fold_convert (sizetype
, maxbound
);
4373 up_bound_p1
= int_const_binop (TRUNC_DIV_EXPR
, maxbound
, eltsize
);
4375 up_bound
= int_const_binop (MINUS_EXPR
, up_bound_p1
,
4376 build_int_cst (ptrdiff_type_node
, 1));
4380 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
4381 build_int_cst (TREE_TYPE (up_bound
), 1));
4383 low_bound
= array_ref_low_bound (ref
);
4385 tree artype
= TREE_TYPE (TREE_OPERAND (ref
, 0));
4387 bool warned
= false;
4390 if (up_bound
&& tree_int_cst_equal (low_bound
, up_bound_p1
))
4391 warned
= warning_at (location
, OPT_Warray_bounds
,
4392 "array subscript %E is above array bounds of %qT",
4395 if (TREE_CODE (low_sub
) == SSA_NAME
)
4397 vr
= get_value_range (low_sub
);
4398 if (!vr
->undefined_p () && !vr
->varying_p ())
4400 low_sub
= vr
->kind () == VR_RANGE
? vr
->max () : vr
->min ();
4401 up_sub
= vr
->kind () == VR_RANGE
? vr
->min () : vr
->max ();
4405 if (vr
&& vr
->kind () == VR_ANTI_RANGE
)
4408 && TREE_CODE (up_sub
) == INTEGER_CST
4409 && (ignore_off_by_one
4410 ? tree_int_cst_lt (up_bound
, up_sub
)
4411 : tree_int_cst_le (up_bound
, up_sub
))
4412 && TREE_CODE (low_sub
) == INTEGER_CST
4413 && tree_int_cst_le (low_sub
, low_bound
))
4414 warned
= warning_at (location
, OPT_Warray_bounds
,
4415 "array subscript [%E, %E] is outside "
4416 "array bounds of %qT",
4417 low_sub
, up_sub
, artype
);
4420 && TREE_CODE (up_sub
) == INTEGER_CST
4421 && (ignore_off_by_one
4422 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
4423 : !tree_int_cst_le (up_sub
, up_bound
)))
4425 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4427 fprintf (dump_file
, "Array bound warning for ");
4428 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
4429 fprintf (dump_file
, "\n");
4431 warned
= warning_at (location
, OPT_Warray_bounds
,
4432 "array subscript %E is above array bounds of %qT",
4435 else if (TREE_CODE (low_sub
) == INTEGER_CST
4436 && tree_int_cst_lt (low_sub
, low_bound
))
4438 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4440 fprintf (dump_file
, "Array bound warning for ");
4441 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
4442 fprintf (dump_file
, "\n");
4444 warned
= warning_at (location
, OPT_Warray_bounds
,
4445 "array subscript %E is below array bounds of %qT",
4451 ref
= TREE_OPERAND (ref
, 0);
4454 inform (DECL_SOURCE_LOCATION (ref
), "while referencing %qD", ref
);
4456 TREE_NO_WARNING (ref
) = 1;
4460 /* Checks one MEM_REF in REF, located at LOCATION, for out-of-bounds
4461 references to string constants. If VRP can determine that the array
4462 subscript is a constant, check if it is outside valid range.
4463 If the array subscript is a RANGE, warn if it is non-overlapping
4465 IGNORE_OFF_BY_ONE is true if the MEM_REF is inside an ADDR_EXPR
4466 (used to allow one-past-the-end indices for code that takes
4467 the address of the just-past-the-end element of an array). */
4470 vrp_prop::check_mem_ref (location_t location
, tree ref
,
4471 bool ignore_off_by_one
)
4473 if (TREE_NO_WARNING (ref
))
4476 tree arg
= TREE_OPERAND (ref
, 0);
4477 /* The constant and variable offset of the reference. */
4478 tree cstoff
= TREE_OPERAND (ref
, 1);
4479 tree varoff
= NULL_TREE
;
4481 const offset_int maxobjsize
= tree_to_shwi (max_object_size ());
4483 /* The array or string constant bounds in bytes. Initially set
4484 to [-MAXOBJSIZE - 1, MAXOBJSIZE] until a tighter bound is
4486 offset_int arrbounds
[2] = { -maxobjsize
- 1, maxobjsize
};
4488 /* The minimum and maximum intermediate offset. For a reference
4489 to be valid, not only does the final offset/subscript must be
4490 in bounds but all intermediate offsets should be as well.
4491 GCC may be able to deal gracefully with such out-of-bounds
4492 offsets so the checking is only enbaled at -Warray-bounds=2
4493 where it may help detect bugs in uses of the intermediate
4494 offsets that could otherwise not be detectable. */
4495 offset_int ioff
= wi::to_offset (fold_convert (ptrdiff_type_node
, cstoff
));
4496 offset_int extrema
[2] = { 0, wi::abs (ioff
) };
4498 /* The range of the byte offset into the reference. */
4499 offset_int offrange
[2] = { 0, 0 };
4501 const value_range
*vr
= NULL
;
4503 /* Determine the offsets and increment OFFRANGE for the bounds of each.
4504 The loop computes the the range of the final offset for expressions
4505 such as (A + i0 + ... + iN)[CSTOFF] where i0 through iN are SSA_NAMEs
4507 while (TREE_CODE (arg
) == SSA_NAME
)
4509 gimple
*def
= SSA_NAME_DEF_STMT (arg
);
4510 if (!is_gimple_assign (def
))
4513 tree_code code
= gimple_assign_rhs_code (def
);
4514 if (code
== POINTER_PLUS_EXPR
)
4516 arg
= gimple_assign_rhs1 (def
);
4517 varoff
= gimple_assign_rhs2 (def
);
4519 else if (code
== ASSERT_EXPR
)
4521 arg
= TREE_OPERAND (gimple_assign_rhs1 (def
), 0);
4527 /* VAROFF should always be a SSA_NAME here (and not even
4528 INTEGER_CST) but there's no point in taking chances. */
4529 if (TREE_CODE (varoff
) != SSA_NAME
)
4532 vr
= get_value_range (varoff
);
4533 if (!vr
|| vr
->undefined_p () || vr
->varying_p ())
4536 if (!vr
->constant_p ())
4539 if (vr
->kind () == VR_RANGE
)
4541 if (tree_int_cst_lt (vr
->min (), vr
->max ()))
4544 = wi::to_offset (fold_convert (ptrdiff_type_node
, vr
->min ()));
4546 = wi::to_offset (fold_convert (ptrdiff_type_node
, vr
->max ()));
4560 /* Conservatively add [-MAXOBJSIZE -1, MAXOBJSIZE]
4562 offrange
[0] += arrbounds
[0];
4563 offrange
[1] += arrbounds
[1];
4568 /* For an anti-range, analogously to the above, conservatively
4569 add [-MAXOBJSIZE -1, MAXOBJSIZE] to OFFRANGE. */
4570 offrange
[0] += arrbounds
[0];
4571 offrange
[1] += arrbounds
[1];
4574 /* Keep track of the minimum and maximum offset. */
4575 if (offrange
[1] < 0 && offrange
[1] < extrema
[0])
4576 extrema
[0] = offrange
[1];
4577 if (offrange
[0] > 0 && offrange
[0] > extrema
[1])
4578 extrema
[1] = offrange
[0];
4580 if (offrange
[0] < arrbounds
[0])
4581 offrange
[0] = arrbounds
[0];
4583 if (offrange
[1] > arrbounds
[1])
4584 offrange
[1] = arrbounds
[1];
4587 if (TREE_CODE (arg
) == ADDR_EXPR
)
4589 arg
= TREE_OPERAND (arg
, 0);
4590 if (TREE_CODE (arg
) != STRING_CST
4591 && TREE_CODE (arg
) != VAR_DECL
)
4597 /* The type of the object being referred to. It can be an array,
4598 string literal, or a non-array type when the MEM_REF represents
4599 a reference/subscript via a pointer to an object that is not
4600 an element of an array. References to members of structs and
4601 unions are excluded because MEM_REF doesn't make it possible
4602 to identify the member where the reference originated.
4603 Incomplete types are excluded as well because their size is
4605 tree reftype
= TREE_TYPE (arg
);
4606 if (POINTER_TYPE_P (reftype
)
4607 || !COMPLETE_TYPE_P (reftype
)
4608 || TREE_CODE (TYPE_SIZE_UNIT (reftype
)) != INTEGER_CST
4609 || RECORD_OR_UNION_TYPE_P (reftype
))
4613 if (TREE_CODE (reftype
) == ARRAY_TYPE
)
4615 eltsize
= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (reftype
)));
4617 if (tree dom
= TYPE_DOMAIN (reftype
))
4619 tree bnds
[] = { TYPE_MIN_VALUE (dom
), TYPE_MAX_VALUE (dom
) };
4620 if (array_at_struct_end_p (arg
)
4621 || !bnds
[0] || !bnds
[1])
4624 arrbounds
[1] = wi::lrshift (maxobjsize
, wi::floor_log2 (eltsize
));
4628 arrbounds
[0] = wi::to_offset (bnds
[0]) * eltsize
;
4629 arrbounds
[1] = (wi::to_offset (bnds
[1]) + 1) * eltsize
;
4635 arrbounds
[1] = wi::lrshift (maxobjsize
, wi::floor_log2 (eltsize
));
4638 if (TREE_CODE (ref
) == MEM_REF
)
4640 /* For MEM_REF determine a tighter bound of the non-array
4642 tree eltype
= TREE_TYPE (reftype
);
4643 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
4644 eltype
= TREE_TYPE (eltype
);
4645 eltsize
= wi::to_offset (TYPE_SIZE_UNIT (eltype
));
4652 arrbounds
[1] = wi::to_offset (TYPE_SIZE_UNIT (reftype
));
4655 offrange
[0] += ioff
;
4656 offrange
[1] += ioff
;
4658 /* Compute the more permissive upper bound when IGNORE_OFF_BY_ONE
4659 is set (when taking the address of the one-past-last element
4660 of an array) but always use the stricter bound in diagnostics. */
4661 offset_int ubound
= arrbounds
[1];
4662 if (ignore_off_by_one
)
4665 if (offrange
[0] >= ubound
|| offrange
[1] < arrbounds
[0])
4667 /* Treat a reference to a non-array object as one to an array
4668 of a single element. */
4669 if (TREE_CODE (reftype
) != ARRAY_TYPE
)
4670 reftype
= build_array_type_nelts (reftype
, 1);
4672 if (TREE_CODE (ref
) == MEM_REF
)
4674 /* Extract the element type out of MEM_REF and use its size
4675 to compute the index to print in the diagnostic; arrays
4676 in MEM_REF don't mean anything. */
4677 tree type
= TREE_TYPE (ref
);
4678 while (TREE_CODE (type
) == ARRAY_TYPE
)
4679 type
= TREE_TYPE (type
);
4680 tree size
= TYPE_SIZE_UNIT (type
);
4681 offrange
[0] = offrange
[0] / wi::to_offset (size
);
4682 offrange
[1] = offrange
[1] / wi::to_offset (size
);
4686 /* For anything other than MEM_REF, compute the index to
4687 print in the diagnostic as the offset over element size. */
4688 offrange
[0] = offrange
[0] / eltsize
;
4689 offrange
[1] = offrange
[1] / eltsize
;
4693 if (offrange
[0] == offrange
[1])
4694 warned
= warning_at (location
, OPT_Warray_bounds
,
4695 "array subscript %wi is outside array bounds "
4697 offrange
[0].to_shwi (), reftype
);
4699 warned
= warning_at (location
, OPT_Warray_bounds
,
4700 "array subscript [%wi, %wi] is outside "
4701 "array bounds of %qT",
4702 offrange
[0].to_shwi (),
4703 offrange
[1].to_shwi (), reftype
);
4704 if (warned
&& DECL_P (arg
))
4705 inform (DECL_SOURCE_LOCATION (arg
), "while referencing %qD", arg
);
4707 TREE_NO_WARNING (ref
) = 1;
4711 if (warn_array_bounds
< 2)
4714 /* At level 2 check also intermediate offsets. */
4716 if (extrema
[i
] < -arrbounds
[1] || extrema
[i
= 1] > ubound
)
4718 HOST_WIDE_INT tmpidx
= extrema
[i
].to_shwi () / eltsize
.to_shwi ();
4720 warning_at (location
, OPT_Warray_bounds
,
4721 "intermediate array offset %wi is outside array bounds "
4724 TREE_NO_WARNING (ref
) = 1;
4728 /* Searches if the expr T, located at LOCATION computes
4729 address of an ARRAY_REF, and call check_array_ref on it. */
4732 vrp_prop::search_for_addr_array (tree t
, location_t location
)
4734 /* Check each ARRAY_REF and MEM_REF in the reference chain. */
4737 if (TREE_CODE (t
) == ARRAY_REF
)
4738 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
4739 else if (TREE_CODE (t
) == MEM_REF
)
4740 check_mem_ref (location
, t
, true /*ignore_off_by_one*/);
4742 t
= TREE_OPERAND (t
, 0);
4744 while (handled_component_p (t
) || TREE_CODE (t
) == MEM_REF
);
4746 if (TREE_CODE (t
) != MEM_REF
4747 || TREE_CODE (TREE_OPERAND (t
, 0)) != ADDR_EXPR
4748 || TREE_NO_WARNING (t
))
4751 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
4752 tree low_bound
, up_bound
, el_sz
;
4753 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
4754 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
4755 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
4758 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
4759 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
4760 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
4762 || TREE_CODE (low_bound
) != INTEGER_CST
4764 || TREE_CODE (up_bound
) != INTEGER_CST
4766 || TREE_CODE (el_sz
) != INTEGER_CST
)
4770 if (!mem_ref_offset (t
).is_constant (&idx
))
4773 bool warned
= false;
4774 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
4777 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4779 fprintf (dump_file
, "Array bound warning for ");
4780 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
4781 fprintf (dump_file
, "\n");
4783 warned
= warning_at (location
, OPT_Warray_bounds
,
4784 "array subscript %wi is below "
4785 "array bounds of %qT",
4786 idx
.to_shwi (), TREE_TYPE (tem
));
4788 else if (idx
> (wi::to_offset (up_bound
)
4789 - wi::to_offset (low_bound
) + 1))
4791 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4793 fprintf (dump_file
, "Array bound warning for ");
4794 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
4795 fprintf (dump_file
, "\n");
4797 warned
= warning_at (location
, OPT_Warray_bounds
,
4798 "array subscript %wu is above "
4799 "array bounds of %qT",
4800 idx
.to_uhwi (), TREE_TYPE (tem
));
4806 inform (DECL_SOURCE_LOCATION (t
), "while referencing %qD", t
);
4808 TREE_NO_WARNING (t
) = 1;
4812 /* walk_tree() callback that checks if *TP is
4813 an ARRAY_REF inside an ADDR_EXPR (in which an array
4814 subscript one outside the valid range is allowed). Call
4815 check_array_ref for each ARRAY_REF found. The location is
4819 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
4822 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
4823 location_t location
;
4825 if (EXPR_HAS_LOCATION (t
))
4826 location
= EXPR_LOCATION (t
);
4828 location
= gimple_location (wi
->stmt
);
4830 *walk_subtree
= TRUE
;
4832 vrp_prop
*vrp_prop
= (class vrp_prop
*)wi
->info
;
4833 if (TREE_CODE (t
) == ARRAY_REF
)
4834 vrp_prop
->check_array_ref (location
, t
, false /*ignore_off_by_one*/);
4835 else if (TREE_CODE (t
) == MEM_REF
)
4836 vrp_prop
->check_mem_ref (location
, t
, false /*ignore_off_by_one*/);
4837 else if (TREE_CODE (t
) == ADDR_EXPR
)
4839 vrp_prop
->search_for_addr_array (t
, location
);
4840 *walk_subtree
= FALSE
;
4846 /* A dom_walker subclass for use by vrp_prop::check_all_array_refs,
4847 to walk over all statements of all reachable BBs and call
4848 check_array_bounds on them. */
4850 class check_array_bounds_dom_walker
: public dom_walker
4853 check_array_bounds_dom_walker (vrp_prop
*prop
)
4854 : dom_walker (CDI_DOMINATORS
,
4855 /* Discover non-executable edges, preserving EDGE_EXECUTABLE
4856 flags, so that we can merge in information on
4857 non-executable edges from vrp_folder . */
4858 REACHABLE_BLOCKS_PRESERVING_FLAGS
),
4860 ~check_array_bounds_dom_walker () {}
4862 edge
before_dom_children (basic_block
) FINAL OVERRIDE
;
4868 /* Implementation of dom_walker::before_dom_children.
4870 Walk over all statements of BB and call check_array_bounds on them,
4871 and determine if there's a unique successor edge. */
4874 check_array_bounds_dom_walker::before_dom_children (basic_block bb
)
4876 gimple_stmt_iterator si
;
4877 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
4879 gimple
*stmt
= gsi_stmt (si
);
4880 struct walk_stmt_info wi
;
4881 if (!gimple_has_location (stmt
)
4882 || is_gimple_debug (stmt
))
4885 memset (&wi
, 0, sizeof (wi
));
4889 walk_gimple_op (stmt
, check_array_bounds
, &wi
);
4892 /* Determine if there's a unique successor edge, and if so, return
4893 that back to dom_walker, ensuring that we don't visit blocks that
4894 became unreachable during the VRP propagation
4895 (PR tree-optimization/83312). */
4896 return find_taken_edge (bb
, NULL_TREE
);
4899 /* Walk over all statements of all reachable BBs and call check_array_bounds
4903 vrp_prop::check_all_array_refs ()
4905 check_array_bounds_dom_walker
w (this);
4906 w
.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
4909 /* Return true if all imm uses of VAR are either in STMT, or
4910 feed (optionally through a chain of single imm uses) GIMPLE_COND
4911 in basic block COND_BB. */
4914 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
, basic_block cond_bb
)
4916 use_operand_p use_p
, use2_p
;
4917 imm_use_iterator iter
;
4919 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
4920 if (USE_STMT (use_p
) != stmt
)
4922 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
4923 if (is_gimple_debug (use_stmt
))
4925 while (is_gimple_assign (use_stmt
)
4926 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
4927 && single_imm_use (gimple_assign_lhs (use_stmt
),
4928 &use2_p
, &use_stmt2
))
4929 use_stmt
= use_stmt2
;
4930 if (gimple_code (use_stmt
) != GIMPLE_COND
4931 || gimple_bb (use_stmt
) != cond_bb
)
4944 __builtin_unreachable ();
4946 x_5 = ASSERT_EXPR <x_3, ...>;
4947 If x_3 has no other immediate uses (checked by caller),
4948 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
4949 from the non-zero bitmask. */
4952 maybe_set_nonzero_bits (edge e
, tree var
)
4954 basic_block cond_bb
= e
->src
;
4955 gimple
*stmt
= last_stmt (cond_bb
);
4959 || gimple_code (stmt
) != GIMPLE_COND
4960 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
4961 ? EQ_EXPR
: NE_EXPR
)
4962 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
4963 || !integer_zerop (gimple_cond_rhs (stmt
)))
4966 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
4967 if (!is_gimple_assign (stmt
)
4968 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
4969 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
4971 if (gimple_assign_rhs1 (stmt
) != var
)
4975 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
4977 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
4978 if (!gimple_assign_cast_p (stmt2
)
4979 || gimple_assign_rhs1 (stmt2
) != var
4980 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
4981 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
4982 != TYPE_PRECISION (TREE_TYPE (var
))))
4985 cst
= gimple_assign_rhs2 (stmt
);
4986 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
),
4987 wi::to_wide (cst
)));
4990 /* Convert range assertion expressions into the implied copies and
4991 copy propagate away the copies. Doing the trivial copy propagation
4992 here avoids the need to run the full copy propagation pass after
4995 FIXME, this will eventually lead to copy propagation removing the
4996 names that had useful range information attached to them. For
4997 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
4998 then N_i will have the range [3, +INF].
5000 However, by converting the assertion into the implied copy
5001 operation N_i = N_j, we will then copy-propagate N_j into the uses
5002 of N_i and lose the range information. We may want to hold on to
5003 ASSERT_EXPRs a little while longer as the ranges could be used in
5004 things like jump threading.
5006 The problem with keeping ASSERT_EXPRs around is that passes after
5007 VRP need to handle them appropriately.
5009 Another approach would be to make the range information a first
5010 class property of the SSA_NAME so that it can be queried from
5011 any pass. This is made somewhat more complex by the need for
5012 multiple ranges to be associated with one SSA_NAME. */
5015 remove_range_assertions (void)
5018 gimple_stmt_iterator si
;
5019 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
5020 a basic block preceeded by GIMPLE_COND branching to it and
5021 __builtin_trap, -1 if not yet checked, 0 otherwise. */
5024 /* Note that the BSI iterator bump happens at the bottom of the
5025 loop and no bump is necessary if we're removing the statement
5026 referenced by the current BSI. */
5027 FOR_EACH_BB_FN (bb
, cfun
)
5028 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
5030 gimple
*stmt
= gsi_stmt (si
);
5032 if (is_gimple_assign (stmt
)
5033 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
5035 tree lhs
= gimple_assign_lhs (stmt
);
5036 tree rhs
= gimple_assign_rhs1 (stmt
);
5039 var
= ASSERT_EXPR_VAR (rhs
);
5041 if (TREE_CODE (var
) == SSA_NAME
5042 && !POINTER_TYPE_P (TREE_TYPE (lhs
))
5043 && SSA_NAME_RANGE_INFO (lhs
))
5045 if (is_unreachable
== -1)
5048 if (single_pred_p (bb
)
5049 && assert_unreachable_fallthru_edge_p
5050 (single_pred_edge (bb
)))
5054 if (x_7 >= 10 && x_7 < 20)
5055 __builtin_unreachable ();
5056 x_8 = ASSERT_EXPR <x_7, ...>;
5057 if the only uses of x_7 are in the ASSERT_EXPR and
5058 in the condition. In that case, we can copy the
5059 range info from x_8 computed in this pass also
5062 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
5065 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
5066 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
5067 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
5068 maybe_set_nonzero_bits (single_pred_edge (bb
), var
);
5072 /* Propagate the RHS into every use of the LHS. For SSA names
5073 also propagate abnormals as it merely restores the original
5074 IL in this case (an replace_uses_by would assert). */
5075 if (TREE_CODE (var
) == SSA_NAME
)
5077 imm_use_iterator iter
;
5078 use_operand_p use_p
;
5080 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
5081 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
5082 SET_USE (use_p
, var
);
5085 replace_uses_by (lhs
, var
);
5087 /* And finally, remove the copy, it is not needed. */
5088 gsi_remove (&si
, true);
5089 release_defs (stmt
);
5093 if (!is_gimple_debug (gsi_stmt (si
)))
5100 /* Return true if STMT is interesting for VRP. */
5103 stmt_interesting_for_vrp (gimple
*stmt
)
5105 if (gimple_code (stmt
) == GIMPLE_PHI
)
5107 tree res
= gimple_phi_result (stmt
);
5108 return (!virtual_operand_p (res
)
5109 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
5110 || POINTER_TYPE_P (TREE_TYPE (res
))));
5112 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
5114 tree lhs
= gimple_get_lhs (stmt
);
5116 /* In general, assignments with virtual operands are not useful
5117 for deriving ranges, with the obvious exception of calls to
5118 builtin functions. */
5119 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
5120 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
5121 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
5122 && (is_gimple_call (stmt
)
5123 || !gimple_vuse (stmt
)))
5125 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
5126 switch (gimple_call_internal_fn (stmt
))
5128 case IFN_ADD_OVERFLOW
:
5129 case IFN_SUB_OVERFLOW
:
5130 case IFN_MUL_OVERFLOW
:
5131 case IFN_ATOMIC_COMPARE_EXCHANGE
:
5132 /* These internal calls return _Complex integer type,
5133 but are interesting to VRP nevertheless. */
5134 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
5141 else if (gimple_code (stmt
) == GIMPLE_COND
5142 || gimple_code (stmt
) == GIMPLE_SWITCH
)
5148 /* Initialization required by ssa_propagate engine. */
5151 vrp_prop::vrp_initialize ()
5155 FOR_EACH_BB_FN (bb
, cfun
)
5157 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
5160 gphi
*phi
= si
.phi ();
5161 if (!stmt_interesting_for_vrp (phi
))
5163 tree lhs
= PHI_RESULT (phi
);
5164 set_value_range_to_varying (get_value_range (lhs
));
5165 prop_set_simulate_again (phi
, false);
5168 prop_set_simulate_again (phi
, true);
5171 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
5174 gimple
*stmt
= gsi_stmt (si
);
5176 /* If the statement is a control insn, then we do not
5177 want to avoid simulating the statement once. Failure
5178 to do so means that those edges will never get added. */
5179 if (stmt_ends_bb_p (stmt
))
5180 prop_set_simulate_again (stmt
, true);
5181 else if (!stmt_interesting_for_vrp (stmt
))
5183 set_defs_to_varying (stmt
);
5184 prop_set_simulate_again (stmt
, false);
5187 prop_set_simulate_again (stmt
, true);
5192 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
5193 that includes the value VAL. The search is restricted to the range
5194 [START_IDX, n - 1] where n is the size of VEC.
5196 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
5199 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
5200 it is placed in IDX and false is returned.
5202 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
5206 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
5208 size_t n
= gimple_switch_num_labels (stmt
);
5211 /* Find case label for minimum of the value range or the next one.
5212 At each iteration we are searching in [low, high - 1]. */
5214 for (low
= start_idx
, high
= n
; high
!= low
; )
5218 /* Note that i != high, so we never ask for n. */
5219 size_t i
= (high
+ low
) / 2;
5220 t
= gimple_switch_label (stmt
, i
);
5222 /* Cache the result of comparing CASE_LOW and val. */
5223 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
5227 /* Ranges cannot be empty. */
5236 if (CASE_HIGH (t
) != NULL
5237 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
5249 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
5250 for values between MIN and MAX. The first index is placed in MIN_IDX. The
5251 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
5252 then MAX_IDX < MIN_IDX.
5253 Returns true if the default label is not needed. */
5256 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
5260 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
5261 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
5265 && max_take_default
)
5267 /* Only the default case label reached.
5268 Return an empty range. */
5275 bool take_default
= min_take_default
|| max_take_default
;
5279 if (max_take_default
)
5282 /* If the case label range is continuous, we do not need
5283 the default case label. Verify that. */
5284 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
5285 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
5286 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
5287 for (k
= i
+ 1; k
<= j
; ++k
)
5289 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
5290 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
5292 take_default
= true;
5296 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
5297 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
5302 return !take_default
;
5306 /* Evaluate statement STMT. If the statement produces a useful range,
5307 return SSA_PROP_INTERESTING and record the SSA name with the
5308 interesting range into *OUTPUT_P.
5310 If STMT is a conditional branch and we can determine its truth
5311 value, the taken edge is recorded in *TAKEN_EDGE_P.
5313 If STMT produces a varying value, return SSA_PROP_VARYING. */
5315 enum ssa_prop_result
5316 vrp_prop::visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
5318 tree lhs
= gimple_get_lhs (stmt
);
5320 extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, &vr
);
5324 if (update_value_range (*output_p
, &vr
))
5326 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
5328 fprintf (dump_file
, "Found new range for ");
5329 print_generic_expr (dump_file
, *output_p
);
5330 fprintf (dump_file
, ": ");
5331 dump_value_range (dump_file
, &vr
);
5332 fprintf (dump_file
, "\n");
5335 if (vr
.varying_p ())
5336 return SSA_PROP_VARYING
;
5338 return SSA_PROP_INTERESTING
;
5340 return SSA_PROP_NOT_INTERESTING
;
5343 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
5344 switch (gimple_call_internal_fn (stmt
))
5346 case IFN_ADD_OVERFLOW
:
5347 case IFN_SUB_OVERFLOW
:
5348 case IFN_MUL_OVERFLOW
:
5349 case IFN_ATOMIC_COMPARE_EXCHANGE
:
5350 /* These internal calls return _Complex integer type,
5351 which VRP does not track, but the immediate uses
5352 thereof might be interesting. */
5353 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
5355 imm_use_iterator iter
;
5356 use_operand_p use_p
;
5357 enum ssa_prop_result res
= SSA_PROP_VARYING
;
5359 set_value_range_to_varying (get_value_range (lhs
));
5361 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
5363 gimple
*use_stmt
= USE_STMT (use_p
);
5364 if (!is_gimple_assign (use_stmt
))
5366 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
5367 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
5369 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
5370 tree use_lhs
= gimple_assign_lhs (use_stmt
);
5371 if (TREE_CODE (rhs1
) != rhs_code
5372 || TREE_OPERAND (rhs1
, 0) != lhs
5373 || TREE_CODE (use_lhs
) != SSA_NAME
5374 || !stmt_interesting_for_vrp (use_stmt
)
5375 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
5376 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
5377 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
5380 /* If there is a change in the value range for any of the
5381 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
5382 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
5383 or IMAGPART_EXPR immediate uses, but none of them have
5384 a change in their value ranges, return
5385 SSA_PROP_NOT_INTERESTING. If there are no
5386 {REAL,IMAG}PART_EXPR uses at all,
5387 return SSA_PROP_VARYING. */
5389 extract_range_basic (&new_vr
, use_stmt
);
5390 const value_range
*old_vr
= get_value_range (use_lhs
);
5391 if (*old_vr
!= new_vr
)
5392 res
= SSA_PROP_INTERESTING
;
5394 res
= SSA_PROP_NOT_INTERESTING
;
5395 new_vr
.equiv_clear ();
5396 if (res
== SSA_PROP_INTERESTING
)
5410 /* All other statements produce nothing of interest for VRP, so mark
5411 their outputs varying and prevent further simulation. */
5412 set_defs_to_varying (stmt
);
5414 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
5417 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5418 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5419 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5420 possible such range. The resulting range is not canonicalized. */
5423 union_ranges (enum value_range_kind
*vr0type
,
5424 tree
*vr0min
, tree
*vr0max
,
5425 enum value_range_kind vr1type
,
5426 tree vr1min
, tree vr1max
)
5428 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
5429 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
5431 /* [] is vr0, () is vr1 in the following classification comments. */
5435 if (*vr0type
== vr1type
)
5436 /* Nothing to do for equal ranges. */
5438 else if ((*vr0type
== VR_RANGE
5439 && vr1type
== VR_ANTI_RANGE
)
5440 || (*vr0type
== VR_ANTI_RANGE
5441 && vr1type
== VR_RANGE
))
5443 /* For anti-range with range union the result is varying. */
5449 else if (operand_less_p (*vr0max
, vr1min
) == 1
5450 || operand_less_p (vr1max
, *vr0min
) == 1)
5452 /* [ ] ( ) or ( ) [ ]
5453 If the ranges have an empty intersection, result of the union
5454 operation is the anti-range or if both are anti-ranges
5456 if (*vr0type
== VR_ANTI_RANGE
5457 && vr1type
== VR_ANTI_RANGE
)
5459 else if (*vr0type
== VR_ANTI_RANGE
5460 && vr1type
== VR_RANGE
)
5462 else if (*vr0type
== VR_RANGE
5463 && vr1type
== VR_ANTI_RANGE
)
5469 else if (*vr0type
== VR_RANGE
5470 && vr1type
== VR_RANGE
)
5472 /* The result is the convex hull of both ranges. */
5473 if (operand_less_p (*vr0max
, vr1min
) == 1)
5475 /* If the result can be an anti-range, create one. */
5476 if (TREE_CODE (*vr0max
) == INTEGER_CST
5477 && TREE_CODE (vr1min
) == INTEGER_CST
5478 && vrp_val_is_min (*vr0min
)
5479 && vrp_val_is_max (vr1max
))
5481 tree min
= int_const_binop (PLUS_EXPR
,
5483 build_int_cst (TREE_TYPE (*vr0max
), 1));
5484 tree max
= int_const_binop (MINUS_EXPR
,
5486 build_int_cst (TREE_TYPE (vr1min
), 1));
5487 if (!operand_less_p (max
, min
))
5489 *vr0type
= VR_ANTI_RANGE
;
5501 /* If the result can be an anti-range, create one. */
5502 if (TREE_CODE (vr1max
) == INTEGER_CST
5503 && TREE_CODE (*vr0min
) == INTEGER_CST
5504 && vrp_val_is_min (vr1min
)
5505 && vrp_val_is_max (*vr0max
))
5507 tree min
= int_const_binop (PLUS_EXPR
,
5509 build_int_cst (TREE_TYPE (vr1max
), 1));
5510 tree max
= int_const_binop (MINUS_EXPR
,
5512 build_int_cst (TREE_TYPE (*vr0min
), 1));
5513 if (!operand_less_p (max
, min
))
5515 *vr0type
= VR_ANTI_RANGE
;
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 else if (*vr0type
== VR_ANTI_RANGE
5537 && vr1type
== VR_ANTI_RANGE
)
5543 else if (*vr0type
== VR_ANTI_RANGE
5544 && vr1type
== VR_RANGE
)
5546 /* Arbitrarily choose the right or left gap. */
5547 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
5548 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
5549 build_int_cst (TREE_TYPE (vr1min
), 1));
5550 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
5551 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
5552 build_int_cst (TREE_TYPE (vr1max
), 1));
5556 else if (*vr0type
== VR_RANGE
5557 && vr1type
== VR_ANTI_RANGE
)
5558 /* The result covers everything. */
5563 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
5564 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
5566 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5567 if (*vr0type
== VR_RANGE
5568 && vr1type
== VR_RANGE
)
5574 else if (*vr0type
== VR_ANTI_RANGE
5575 && vr1type
== VR_ANTI_RANGE
)
5577 else if (*vr0type
== VR_RANGE
5578 && vr1type
== VR_ANTI_RANGE
)
5580 *vr0type
= VR_ANTI_RANGE
;
5581 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
5583 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
5584 build_int_cst (TREE_TYPE (*vr0min
), 1));
5587 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
5589 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
5590 build_int_cst (TREE_TYPE (*vr0max
), 1));
5596 else if (*vr0type
== VR_ANTI_RANGE
5597 && vr1type
== VR_RANGE
)
5598 /* The result covers everything. */
5603 else if ((operand_less_p (vr1min
, *vr0max
) == 1
5604 || operand_equal_p (vr1min
, *vr0max
, 0))
5605 && operand_less_p (*vr0min
, vr1min
) == 1
5606 && operand_less_p (*vr0max
, vr1max
) == 1)
5608 /* [ ( ] ) or [ ]( ) */
5609 if (*vr0type
== VR_RANGE
5610 && vr1type
== VR_RANGE
)
5612 else if (*vr0type
== VR_ANTI_RANGE
5613 && vr1type
== VR_ANTI_RANGE
)
5615 else if (*vr0type
== VR_ANTI_RANGE
5616 && vr1type
== VR_RANGE
)
5618 if (TREE_CODE (vr1min
) == INTEGER_CST
)
5619 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
5620 build_int_cst (TREE_TYPE (vr1min
), 1));
5624 else if (*vr0type
== VR_RANGE
5625 && vr1type
== VR_ANTI_RANGE
)
5627 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
5630 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
5631 build_int_cst (TREE_TYPE (*vr0max
), 1));
5640 else if ((operand_less_p (*vr0min
, vr1max
) == 1
5641 || operand_equal_p (*vr0min
, vr1max
, 0))
5642 && operand_less_p (vr1min
, *vr0min
) == 1
5643 && operand_less_p (vr1max
, *vr0max
) == 1)
5645 /* ( [ ) ] or ( )[ ] */
5646 if (*vr0type
== VR_RANGE
5647 && vr1type
== VR_RANGE
)
5649 else if (*vr0type
== VR_ANTI_RANGE
5650 && vr1type
== VR_ANTI_RANGE
)
5652 else if (*vr0type
== VR_ANTI_RANGE
5653 && vr1type
== VR_RANGE
)
5655 if (TREE_CODE (vr1max
) == INTEGER_CST
)
5656 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
5657 build_int_cst (TREE_TYPE (vr1max
), 1));
5661 else if (*vr0type
== VR_RANGE
5662 && vr1type
== VR_ANTI_RANGE
)
5664 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
5667 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
5668 build_int_cst (TREE_TYPE (*vr0min
), 1));
5683 *vr0type
= VR_VARYING
;
5684 *vr0min
= NULL_TREE
;
5685 *vr0max
= NULL_TREE
;
5688 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5689 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5690 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5691 possible such range. The resulting range is not canonicalized. */
5694 intersect_ranges (enum value_range_kind
*vr0type
,
5695 tree
*vr0min
, tree
*vr0max
,
5696 enum value_range_kind vr1type
,
5697 tree vr1min
, tree vr1max
)
5699 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
5700 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
5702 /* [] is vr0, () is vr1 in the following classification comments. */
5706 if (*vr0type
== vr1type
)
5707 /* Nothing to do for equal ranges. */
5709 else if ((*vr0type
== VR_RANGE
5710 && vr1type
== VR_ANTI_RANGE
)
5711 || (*vr0type
== VR_ANTI_RANGE
5712 && vr1type
== VR_RANGE
))
5714 /* For anti-range with range intersection the result is empty. */
5715 *vr0type
= VR_UNDEFINED
;
5716 *vr0min
= NULL_TREE
;
5717 *vr0max
= NULL_TREE
;
5722 else if (operand_less_p (*vr0max
, vr1min
) == 1
5723 || operand_less_p (vr1max
, *vr0min
) == 1)
5725 /* [ ] ( ) or ( ) [ ]
5726 If the ranges have an empty intersection, the result of the
5727 intersect operation is the range for intersecting an
5728 anti-range with a range or empty when intersecting two ranges. */
5729 if (*vr0type
== VR_RANGE
5730 && vr1type
== VR_ANTI_RANGE
)
5732 else if (*vr0type
== VR_ANTI_RANGE
5733 && vr1type
== VR_RANGE
)
5739 else if (*vr0type
== VR_RANGE
5740 && vr1type
== VR_RANGE
)
5742 *vr0type
= VR_UNDEFINED
;
5743 *vr0min
= NULL_TREE
;
5744 *vr0max
= NULL_TREE
;
5746 else if (*vr0type
== VR_ANTI_RANGE
5747 && vr1type
== VR_ANTI_RANGE
)
5749 /* If the anti-ranges are adjacent to each other merge them. */
5750 if (TREE_CODE (*vr0max
) == INTEGER_CST
5751 && TREE_CODE (vr1min
) == INTEGER_CST
5752 && operand_less_p (*vr0max
, vr1min
) == 1
5753 && integer_onep (int_const_binop (MINUS_EXPR
,
5756 else if (TREE_CODE (vr1max
) == INTEGER_CST
5757 && TREE_CODE (*vr0min
) == INTEGER_CST
5758 && operand_less_p (vr1max
, *vr0min
) == 1
5759 && integer_onep (int_const_binop (MINUS_EXPR
,
5762 /* Else arbitrarily take VR0. */
5765 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
5766 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
5768 /* [ ( ) ] or [( ) ] or [ ( )] */
5769 if (*vr0type
== VR_RANGE
5770 && vr1type
== VR_RANGE
)
5772 /* If both are ranges the result is the inner one. */
5777 else if (*vr0type
== VR_RANGE
5778 && vr1type
== VR_ANTI_RANGE
)
5780 /* Choose the right gap if the left one is empty. */
5783 if (TREE_CODE (vr1max
) != INTEGER_CST
)
5785 else if (TYPE_PRECISION (TREE_TYPE (vr1max
)) == 1
5786 && !TYPE_UNSIGNED (TREE_TYPE (vr1max
)))
5788 = int_const_binop (MINUS_EXPR
, vr1max
,
5789 build_int_cst (TREE_TYPE (vr1max
), -1));
5792 = int_const_binop (PLUS_EXPR
, vr1max
,
5793 build_int_cst (TREE_TYPE (vr1max
), 1));
5795 /* Choose the left gap if the right one is empty. */
5798 if (TREE_CODE (vr1min
) != INTEGER_CST
)
5800 else if (TYPE_PRECISION (TREE_TYPE (vr1min
)) == 1
5801 && !TYPE_UNSIGNED (TREE_TYPE (vr1min
)))
5803 = int_const_binop (PLUS_EXPR
, vr1min
,
5804 build_int_cst (TREE_TYPE (vr1min
), -1));
5807 = int_const_binop (MINUS_EXPR
, vr1min
,
5808 build_int_cst (TREE_TYPE (vr1min
), 1));
5810 /* Choose the anti-range if the range is effectively varying. */
5811 else if (vrp_val_is_min (*vr0min
)
5812 && vrp_val_is_max (*vr0max
))
5818 /* Else choose the range. */
5820 else if (*vr0type
== VR_ANTI_RANGE
5821 && vr1type
== VR_ANTI_RANGE
)
5822 /* If both are anti-ranges the result is the outer one. */
5824 else if (*vr0type
== VR_ANTI_RANGE
5825 && vr1type
== VR_RANGE
)
5827 /* The intersection is empty. */
5828 *vr0type
= VR_UNDEFINED
;
5829 *vr0min
= NULL_TREE
;
5830 *vr0max
= NULL_TREE
;
5835 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
5836 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
5838 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5839 if (*vr0type
== VR_RANGE
5840 && vr1type
== VR_RANGE
)
5841 /* Choose the inner range. */
5843 else if (*vr0type
== VR_ANTI_RANGE
5844 && vr1type
== VR_RANGE
)
5846 /* Choose the right gap if the left is empty. */
5849 *vr0type
= VR_RANGE
;
5850 if (TREE_CODE (*vr0max
) != INTEGER_CST
)
5852 else if (TYPE_PRECISION (TREE_TYPE (*vr0max
)) == 1
5853 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max
)))
5855 = int_const_binop (MINUS_EXPR
, *vr0max
,
5856 build_int_cst (TREE_TYPE (*vr0max
), -1));
5859 = int_const_binop (PLUS_EXPR
, *vr0max
,
5860 build_int_cst (TREE_TYPE (*vr0max
), 1));
5863 /* Choose the left gap if the right is empty. */
5866 *vr0type
= VR_RANGE
;
5867 if (TREE_CODE (*vr0min
) != INTEGER_CST
)
5869 else if (TYPE_PRECISION (TREE_TYPE (*vr0min
)) == 1
5870 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min
)))
5872 = int_const_binop (PLUS_EXPR
, *vr0min
,
5873 build_int_cst (TREE_TYPE (*vr0min
), -1));
5876 = int_const_binop (MINUS_EXPR
, *vr0min
,
5877 build_int_cst (TREE_TYPE (*vr0min
), 1));
5880 /* Choose the anti-range if the range is effectively varying. */
5881 else if (vrp_val_is_min (vr1min
)
5882 && vrp_val_is_max (vr1max
))
5884 /* Choose the anti-range if it is ~[0,0], that range is special
5885 enough to special case when vr1's range is relatively wide.
5886 At least for types bigger than int - this covers pointers
5887 and arguments to functions like ctz. */
5888 else if (*vr0min
== *vr0max
5889 && integer_zerop (*vr0min
)
5890 && ((TYPE_PRECISION (TREE_TYPE (*vr0min
))
5891 >= TYPE_PRECISION (integer_type_node
))
5892 || POINTER_TYPE_P (TREE_TYPE (*vr0min
)))
5893 && TREE_CODE (vr1max
) == INTEGER_CST
5894 && TREE_CODE (vr1min
) == INTEGER_CST
5895 && (wi::clz (wi::to_wide (vr1max
) - wi::to_wide (vr1min
))
5896 < TYPE_PRECISION (TREE_TYPE (*vr0min
)) / 2))
5898 /* Else choose the range. */
5906 else if (*vr0type
== VR_ANTI_RANGE
5907 && vr1type
== VR_ANTI_RANGE
)
5909 /* If both are anti-ranges the result is the outer one. */
5914 else if (vr1type
== VR_ANTI_RANGE
5915 && *vr0type
== VR_RANGE
)
5917 /* The intersection is empty. */
5918 *vr0type
= VR_UNDEFINED
;
5919 *vr0min
= NULL_TREE
;
5920 *vr0max
= NULL_TREE
;
5925 else if ((operand_less_p (vr1min
, *vr0max
) == 1
5926 || operand_equal_p (vr1min
, *vr0max
, 0))
5927 && operand_less_p (*vr0min
, vr1min
) == 1)
5929 /* [ ( ] ) or [ ]( ) */
5930 if (*vr0type
== VR_ANTI_RANGE
5931 && vr1type
== VR_ANTI_RANGE
)
5933 else if (*vr0type
== VR_RANGE
5934 && vr1type
== VR_RANGE
)
5936 else if (*vr0type
== VR_RANGE
5937 && vr1type
== VR_ANTI_RANGE
)
5939 if (TREE_CODE (vr1min
) == INTEGER_CST
)
5940 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
5941 build_int_cst (TREE_TYPE (vr1min
), 1));
5945 else if (*vr0type
== VR_ANTI_RANGE
5946 && vr1type
== VR_RANGE
)
5948 *vr0type
= VR_RANGE
;
5949 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
5950 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
5951 build_int_cst (TREE_TYPE (*vr0max
), 1));
5959 else if ((operand_less_p (*vr0min
, vr1max
) == 1
5960 || operand_equal_p (*vr0min
, vr1max
, 0))
5961 && operand_less_p (vr1min
, *vr0min
) == 1)
5963 /* ( [ ) ] or ( )[ ] */
5964 if (*vr0type
== VR_ANTI_RANGE
5965 && vr1type
== VR_ANTI_RANGE
)
5967 else if (*vr0type
== VR_RANGE
5968 && vr1type
== VR_RANGE
)
5970 else if (*vr0type
== VR_RANGE
5971 && vr1type
== VR_ANTI_RANGE
)
5973 if (TREE_CODE (vr1max
) == INTEGER_CST
)
5974 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
5975 build_int_cst (TREE_TYPE (vr1max
), 1));
5979 else if (*vr0type
== VR_ANTI_RANGE
5980 && vr1type
== VR_RANGE
)
5982 *vr0type
= VR_RANGE
;
5983 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
5984 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
5985 build_int_cst (TREE_TYPE (*vr0min
), 1));
5994 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
5995 result for the intersection. That's always a conservative
5996 correct estimate unless VR1 is a constant singleton range
5997 in which case we choose that. */
5998 if (vr1type
== VR_RANGE
5999 && is_gimple_min_invariant (vr1min
)
6000 && vrp_operand_equal_p (vr1min
, vr1max
))
6009 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
6010 in *VR0. This may not be the smallest possible such range. */
6013 value_range::intersect_helper (value_range
*vr0
, const value_range
*vr1
)
6015 /* If either range is VR_VARYING the other one wins. */
6016 if (vr1
->varying_p ())
6018 if (vr0
->varying_p ())
6020 vr0
->deep_copy (vr1
);
6024 /* When either range is VR_UNDEFINED the resulting range is
6025 VR_UNDEFINED, too. */
6026 if (vr0
->undefined_p ())
6028 if (vr1
->undefined_p ())
6030 set_value_range_to_undefined (vr0
);
6034 /* Save the original vr0 so we can return it as conservative intersection
6035 result when our worker turns things to varying. */
6036 value_range
saved (*vr0
);
6038 value_range_kind vr0type
= vr0
->kind ();
6039 tree vr0min
= vr0
->min ();
6040 tree vr0max
= vr0
->max ();
6041 intersect_ranges (&vr0type
, &vr0min
, &vr0max
,
6042 vr1
->kind (), vr1
->min (), vr1
->max ());
6043 /* Make sure to canonicalize the result though as the inversion of a
6044 VR_RANGE can still be a VR_RANGE. */
6045 vr0
->set_and_canonicalize (vr0type
, vr0min
, vr0max
, vr0
->m_equiv
);
6046 /* If that failed, use the saved original VR0. */
6047 if (vr0
->varying_p ())
6052 /* If the result is VR_UNDEFINED there is no need to mess with
6053 the equivalencies. */
6054 if (vr0
->undefined_p ())
6057 /* The resulting set of equivalences for range intersection is the union of
6059 if (vr0
->m_equiv
&& vr1
->m_equiv
&& vr0
->m_equiv
!= vr1
->m_equiv
)
6060 bitmap_ior_into (vr0
->m_equiv
, vr1
->m_equiv
);
6061 else if (vr1
->m_equiv
&& !vr0
->m_equiv
)
6063 /* All equivalence bitmaps are allocated from the same obstack. So
6064 we can use the obstack associated with VR to allocate vr0->equiv. */
6065 vr0
->m_equiv
= BITMAP_ALLOC (vr1
->m_equiv
->obstack
);
6066 bitmap_copy (m_equiv
, vr1
->m_equiv
);
6071 value_range::intersect (const value_range
*other
)
6073 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6075 fprintf (dump_file
, "Intersecting\n ");
6076 dump_value_range (dump_file
, this);
6077 fprintf (dump_file
, "\nand\n ");
6078 dump_value_range (dump_file
, other
);
6079 fprintf (dump_file
, "\n");
6081 intersect_helper (this, other
);
6082 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6084 fprintf (dump_file
, "to\n ");
6085 dump_value_range (dump_file
, this);
6086 fprintf (dump_file
, "\n");
6090 /* Meet operation for value ranges. Given two value ranges VR0 and
6091 VR1, store in VR0 a range that contains both VR0 and VR1. This
6092 may not be the smallest possible such range. */
6095 value_range_base::union_ (const value_range_base
*other
)
6097 if (other
->undefined_p ())
6099 /* this already has the resulting range. */
6103 if (this->undefined_p ())
6109 if (this->varying_p ())
6111 /* Nothing to do. VR0 already has the resulting range. */
6115 if (other
->varying_p ())
6117 this->set_varying ();
6121 value_range
saved (*this);
6122 value_range_kind vr0type
= this->kind ();
6123 tree vr0min
= this->min ();
6124 tree vr0max
= this->max ();
6125 union_ranges (&vr0type
, &vr0min
, &vr0max
,
6126 other
->kind (), other
->min (), other
->max ());
6127 *this = value_range_base (vr0type
, vr0min
, vr0max
);
6128 if (this->varying_p ())
6130 /* Failed to find an efficient meet. Before giving up and setting
6131 the result to VARYING, see if we can at least derive a useful
6133 if (range_includes_zero_p (&saved
) == 0
6134 && range_includes_zero_p (other
) == 0)
6136 tree zero
= build_int_cst (saved
.type (), 0);
6137 *this = value_range_base (VR_ANTI_RANGE
, zero
, zero
);
6141 this->set_varying ();
6144 this->set_and_canonicalize (this->kind (), this->min (), this->max ());
6147 /* Meet operation for value ranges. Given two value ranges VR0 and
6148 VR1, store in VR0 a range that contains both VR0 and VR1. This
6149 may not be the smallest possible such range. */
6152 value_range::union_helper (value_range
*vr0
, const value_range
*vr1
)
6154 if (vr1
->undefined_p ())
6156 /* VR0 already has the resulting range. */
6160 if (vr0
->undefined_p ())
6162 vr0
->deep_copy (vr1
);
6166 if (vr0
->varying_p ())
6168 /* Nothing to do. VR0 already has the resulting range. */
6172 if (vr1
->varying_p ())
6174 set_value_range_to_varying (vr0
);
6178 value_range
saved (*vr0
);
6179 value_range_kind vr0type
= vr0
->kind ();
6180 tree vr0min
= vr0
->min ();
6181 tree vr0max
= vr0
->max ();
6182 union_ranges (&vr0type
, &vr0min
, &vr0max
,
6183 vr1
->kind (), vr1
->min (), vr1
->max ());
6184 *vr0
= value_range (vr0type
, vr0min
, vr0max
);
6185 if (vr0
->varying_p ())
6187 /* Failed to find an efficient meet. Before giving up and setting
6188 the result to VARYING, see if we can at least derive a useful
6190 if (range_includes_zero_p (&saved
) == 0
6191 && range_includes_zero_p (vr1
) == 0)
6193 set_value_range_to_nonnull (vr0
, saved
.type ());
6195 /* Since this meet operation did not result from the meeting of
6196 two equivalent names, VR0 cannot have any equivalences. */
6198 bitmap_clear (vr0
->m_equiv
);
6202 set_value_range_to_varying (vr0
);
6205 vr0
->set_and_canonicalize (vr0
->kind (), vr0
->min (), vr0
->max (),
6207 if (vr0
->varying_p ())
6210 /* The resulting set of equivalences is always the intersection of
6212 if (vr0
->m_equiv
&& vr1
->m_equiv
&& vr0
->m_equiv
!= vr1
->m_equiv
)
6213 bitmap_and_into (vr0
->m_equiv
, vr1
->m_equiv
);
6214 else if (vr0
->m_equiv
&& !vr1
->m_equiv
)
6215 bitmap_clear (vr0
->m_equiv
);
6219 value_range::union_ (const value_range
*other
)
6221 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6223 fprintf (dump_file
, "Meeting\n ");
6224 dump_value_range (dump_file
, this);
6225 fprintf (dump_file
, "\nand\n ");
6226 dump_value_range (dump_file
, other
);
6227 fprintf (dump_file
, "\n");
6229 union_helper (this, other
);
6230 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6232 fprintf (dump_file
, "to\n ");
6233 dump_value_range (dump_file
, this);
6234 fprintf (dump_file
, "\n");
6238 /* Visit all arguments for PHI node PHI that flow through executable
6239 edges. If a valid value range can be derived from all the incoming
6240 value ranges, set a new range for the LHS of PHI. */
6242 enum ssa_prop_result
6243 vrp_prop::visit_phi (gphi
*phi
)
6245 tree lhs
= PHI_RESULT (phi
);
6246 value_range vr_result
;
6247 extract_range_from_phi_node (phi
, &vr_result
);
6248 if (update_value_range (lhs
, &vr_result
))
6250 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6252 fprintf (dump_file
, "Found new range for ");
6253 print_generic_expr (dump_file
, lhs
);
6254 fprintf (dump_file
, ": ");
6255 dump_value_range (dump_file
, &vr_result
);
6256 fprintf (dump_file
, "\n");
6259 if (vr_result
.varying_p ())
6260 return SSA_PROP_VARYING
;
6262 return SSA_PROP_INTERESTING
;
6265 /* Nothing changed, don't add outgoing edges. */
6266 return SSA_PROP_NOT_INTERESTING
;
6269 class vrp_folder
: public substitute_and_fold_engine
6272 tree
get_value (tree
) FINAL OVERRIDE
;
6273 bool fold_stmt (gimple_stmt_iterator
*) FINAL OVERRIDE
;
6274 bool fold_predicate_in (gimple_stmt_iterator
*);
6276 class vr_values
*vr_values
;
6279 tree
vrp_evaluate_conditional (tree_code code
, tree op0
,
6280 tree op1
, gimple
*stmt
)
6281 { return vr_values
->vrp_evaluate_conditional (code
, op0
, op1
, stmt
); }
6282 bool simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
6283 { return vr_values
->simplify_stmt_using_ranges (gsi
); }
6284 tree
op_with_constant_singleton_value_range (tree op
)
6285 { return vr_values
->op_with_constant_singleton_value_range (op
); }
6288 /* If the statement pointed by SI has a predicate whose value can be
6289 computed using the value range information computed by VRP, compute
6290 its value and return true. Otherwise, return false. */
6293 vrp_folder::fold_predicate_in (gimple_stmt_iterator
*si
)
6295 bool assignment_p
= false;
6297 gimple
*stmt
= gsi_stmt (*si
);
6299 if (is_gimple_assign (stmt
)
6300 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
6302 assignment_p
= true;
6303 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
6304 gimple_assign_rhs1 (stmt
),
6305 gimple_assign_rhs2 (stmt
),
6308 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
6309 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
6310 gimple_cond_lhs (cond_stmt
),
6311 gimple_cond_rhs (cond_stmt
),
6319 val
= fold_convert (gimple_expr_type (stmt
), val
);
6323 fprintf (dump_file
, "Folding predicate ");
6324 print_gimple_expr (dump_file
, stmt
, 0);
6325 fprintf (dump_file
, " to ");
6326 print_generic_expr (dump_file
, val
);
6327 fprintf (dump_file
, "\n");
6330 if (is_gimple_assign (stmt
))
6331 gimple_assign_set_rhs_from_tree (si
, val
);
6334 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
6335 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
6336 if (integer_zerop (val
))
6337 gimple_cond_make_false (cond_stmt
);
6338 else if (integer_onep (val
))
6339 gimple_cond_make_true (cond_stmt
);
6350 /* Callback for substitute_and_fold folding the stmt at *SI. */
6353 vrp_folder::fold_stmt (gimple_stmt_iterator
*si
)
6355 if (fold_predicate_in (si
))
6358 return simplify_stmt_using_ranges (si
);
6361 /* If OP has a value range with a single constant value return that,
6362 otherwise return NULL_TREE. This returns OP itself if OP is a
6365 Implemented as a pure wrapper right now, but this will change. */
6368 vrp_folder::get_value (tree op
)
6370 return op_with_constant_singleton_value_range (op
);
6373 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
6374 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
6375 BB. If no such ASSERT_EXPR is found, return OP. */
6378 lhs_of_dominating_assert (tree op
, basic_block bb
, gimple
*stmt
)
6380 imm_use_iterator imm_iter
;
6382 use_operand_p use_p
;
6384 if (TREE_CODE (op
) == SSA_NAME
)
6386 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, op
)
6388 use_stmt
= USE_STMT (use_p
);
6389 if (use_stmt
!= stmt
6390 && gimple_assign_single_p (use_stmt
)
6391 && TREE_CODE (gimple_assign_rhs1 (use_stmt
)) == ASSERT_EXPR
6392 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt
), 0) == op
6393 && dominated_by_p (CDI_DOMINATORS
, bb
, gimple_bb (use_stmt
)))
6394 return gimple_assign_lhs (use_stmt
);
6401 static class vr_values
*x_vr_values
;
6403 /* A trivial wrapper so that we can present the generic jump threading
6404 code with a simple API for simplifying statements. STMT is the
6405 statement we want to simplify, WITHIN_STMT provides the location
6406 for any overflow warnings. */
6409 simplify_stmt_for_jump_threading (gimple
*stmt
, gimple
*within_stmt
,
6410 class avail_exprs_stack
*avail_exprs_stack ATTRIBUTE_UNUSED
,
6413 /* First see if the conditional is in the hash table. */
6414 tree cached_lhs
= avail_exprs_stack
->lookup_avail_expr (stmt
, false, true);
6415 if (cached_lhs
&& is_gimple_min_invariant (cached_lhs
))
6418 vr_values
*vr_values
= x_vr_values
;
6419 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
6421 tree op0
= gimple_cond_lhs (cond_stmt
);
6422 op0
= lhs_of_dominating_assert (op0
, bb
, stmt
);
6424 tree op1
= gimple_cond_rhs (cond_stmt
);
6425 op1
= lhs_of_dominating_assert (op1
, bb
, stmt
);
6427 return vr_values
->vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
6428 op0
, op1
, within_stmt
);
6431 /* We simplify a switch statement by trying to determine which case label
6432 will be taken. If we are successful then we return the corresponding
6434 if (gswitch
*switch_stmt
= dyn_cast
<gswitch
*> (stmt
))
6436 tree op
= gimple_switch_index (switch_stmt
);
6437 if (TREE_CODE (op
) != SSA_NAME
)
6440 op
= lhs_of_dominating_assert (op
, bb
, stmt
);
6442 const value_range
*vr
= vr_values
->get_value_range (op
);
6443 if (vr
->undefined_p ()
6445 || vr
->symbolic_p ())
6448 if (vr
->kind () == VR_RANGE
)
6451 /* Get the range of labels that contain a part of the operand's
6453 find_case_label_range (switch_stmt
, vr
->min (), vr
->max (), &i
, &j
);
6455 /* Is there only one such label? */
6458 tree label
= gimple_switch_label (switch_stmt
, i
);
6460 /* The i'th label will be taken only if the value range of the
6461 operand is entirely within the bounds of this label. */
6462 if (CASE_HIGH (label
) != NULL_TREE
6463 ? (tree_int_cst_compare (CASE_LOW (label
), vr
->min ()) <= 0
6464 && tree_int_cst_compare (CASE_HIGH (label
),
6466 : (tree_int_cst_equal (CASE_LOW (label
), vr
->min ())
6467 && tree_int_cst_equal (vr
->min (), vr
->max ())))
6471 /* If there are no such labels then the default label will be
6474 return gimple_switch_label (switch_stmt
, 0);
6477 if (vr
->kind () == VR_ANTI_RANGE
)
6479 unsigned n
= gimple_switch_num_labels (switch_stmt
);
6480 tree min_label
= gimple_switch_label (switch_stmt
, 1);
6481 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
6483 /* The default label will be taken only if the anti-range of the
6484 operand is entirely outside the bounds of all the (non-default)
6486 if (tree_int_cst_compare (vr
->min (), CASE_LOW (min_label
)) <= 0
6487 && (CASE_HIGH (max_label
) != NULL_TREE
6488 ? tree_int_cst_compare (vr
->max (),
6489 CASE_HIGH (max_label
)) >= 0
6490 : tree_int_cst_compare (vr
->max (),
6491 CASE_LOW (max_label
)) >= 0))
6492 return gimple_switch_label (switch_stmt
, 0);
6498 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
6500 tree lhs
= gimple_assign_lhs (assign_stmt
);
6501 if (TREE_CODE (lhs
) == SSA_NAME
6502 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6503 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6504 && stmt_interesting_for_vrp (stmt
))
6509 vr_values
->extract_range_from_stmt (stmt
, &dummy_e
,
6510 &dummy_tree
, &new_vr
);
6512 if (new_vr
.singleton_p (&singleton
))
6520 class vrp_dom_walker
: public dom_walker
6523 vrp_dom_walker (cdi_direction direction
,
6524 class const_and_copies
*const_and_copies
,
6525 class avail_exprs_stack
*avail_exprs_stack
)
6526 : dom_walker (direction
, REACHABLE_BLOCKS
),
6527 m_const_and_copies (const_and_copies
),
6528 m_avail_exprs_stack (avail_exprs_stack
),
6529 m_dummy_cond (NULL
) {}
6531 virtual edge
before_dom_children (basic_block
);
6532 virtual void after_dom_children (basic_block
);
6534 class vr_values
*vr_values
;
6537 class const_and_copies
*m_const_and_copies
;
6538 class avail_exprs_stack
*m_avail_exprs_stack
;
6540 gcond
*m_dummy_cond
;
6544 /* Called before processing dominator children of BB. We want to look
6545 at ASSERT_EXPRs and record information from them in the appropriate
6548 We could look at other statements here. It's not seen as likely
6549 to significantly increase the jump threads we discover. */
6552 vrp_dom_walker::before_dom_children (basic_block bb
)
6554 gimple_stmt_iterator gsi
;
6556 m_avail_exprs_stack
->push_marker ();
6557 m_const_and_copies
->push_marker ();
6558 for (gsi
= gsi_start_nondebug_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6560 gimple
*stmt
= gsi_stmt (gsi
);
6561 if (gimple_assign_single_p (stmt
)
6562 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
6564 tree rhs1
= gimple_assign_rhs1 (stmt
);
6565 tree cond
= TREE_OPERAND (rhs1
, 1);
6566 tree inverted
= invert_truthvalue (cond
);
6567 vec
<cond_equivalence
> p
;
6569 record_conditions (&p
, cond
, inverted
);
6570 for (unsigned int i
= 0; i
< p
.length (); i
++)
6571 m_avail_exprs_stack
->record_cond (&p
[i
]);
6573 tree lhs
= gimple_assign_lhs (stmt
);
6574 m_const_and_copies
->record_const_or_copy (lhs
,
6575 TREE_OPERAND (rhs1
, 0));
6584 /* Called after processing dominator children of BB. This is where we
6585 actually call into the threader. */
6587 vrp_dom_walker::after_dom_children (basic_block bb
)
6590 m_dummy_cond
= gimple_build_cond (NE_EXPR
,
6591 integer_zero_node
, integer_zero_node
,
6594 x_vr_values
= vr_values
;
6595 thread_outgoing_edges (bb
, m_dummy_cond
, m_const_and_copies
,
6596 m_avail_exprs_stack
, NULL
,
6597 simplify_stmt_for_jump_threading
);
6600 m_avail_exprs_stack
->pop_to_marker ();
6601 m_const_and_copies
->pop_to_marker ();
6604 /* Blocks which have more than one predecessor and more than
6605 one successor present jump threading opportunities, i.e.,
6606 when the block is reached from a specific predecessor, we
6607 may be able to determine which of the outgoing edges will
6608 be traversed. When this optimization applies, we are able
6609 to avoid conditionals at runtime and we may expose secondary
6610 optimization opportunities.
6612 This routine is effectively a driver for the generic jump
6613 threading code. It basically just presents the generic code
6614 with edges that may be suitable for jump threading.
6616 Unlike DOM, we do not iterate VRP if jump threading was successful.
6617 While iterating may expose new opportunities for VRP, it is expected
6618 those opportunities would be very limited and the compile time cost
6619 to expose those opportunities would be significant.
6621 As jump threading opportunities are discovered, they are registered
6622 for later realization. */
6625 identify_jump_threads (class vr_values
*vr_values
)
6627 /* Ugh. When substituting values earlier in this pass we can
6628 wipe the dominance information. So rebuild the dominator
6629 information as we need it within the jump threading code. */
6630 calculate_dominance_info (CDI_DOMINATORS
);
6632 /* We do not allow VRP information to be used for jump threading
6633 across a back edge in the CFG. Otherwise it becomes too
6634 difficult to avoid eliminating loop exit tests. Of course
6635 EDGE_DFS_BACK is not accurate at this time so we have to
6637 mark_dfs_back_edges ();
6639 /* Allocate our unwinder stack to unwind any temporary equivalences
6640 that might be recorded. */
6641 const_and_copies
*equiv_stack
= new const_and_copies ();
6643 hash_table
<expr_elt_hasher
> *avail_exprs
6644 = new hash_table
<expr_elt_hasher
> (1024);
6645 avail_exprs_stack
*avail_exprs_stack
6646 = new class avail_exprs_stack (avail_exprs
);
6648 vrp_dom_walker
walker (CDI_DOMINATORS
, equiv_stack
, avail_exprs_stack
);
6649 walker
.vr_values
= vr_values
;
6650 walker
.walk (cfun
->cfg
->x_entry_block_ptr
);
6652 /* We do not actually update the CFG or SSA graphs at this point as
6653 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
6654 handle ASSERT_EXPRs gracefully. */
6657 delete avail_exprs_stack
;
6660 /* Traverse all the blocks folding conditionals with known ranges. */
6663 vrp_prop::vrp_finalize (bool warn_array_bounds_p
)
6667 /* We have completed propagating through the lattice. */
6668 vr_values
.set_lattice_propagation_complete ();
6672 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
6673 vr_values
.dump_all_value_ranges (dump_file
);
6674 fprintf (dump_file
, "\n");
6677 /* Set value range to non pointer SSA_NAMEs. */
6678 for (i
= 0; i
< num_ssa_names
; i
++)
6680 tree name
= ssa_name (i
);
6684 const value_range
*vr
= get_value_range (name
);
6685 if (!name
|| !vr
->constant_p ())
6688 if (POINTER_TYPE_P (TREE_TYPE (name
))
6689 && range_includes_zero_p (vr
) == 0)
6690 set_ptr_nonnull (name
);
6691 else if (!POINTER_TYPE_P (TREE_TYPE (name
)))
6692 set_range_info (name
, *vr
);
6695 /* If we're checking array refs, we want to merge information on
6696 the executability of each edge between vrp_folder and the
6697 check_array_bounds_dom_walker: each can clear the
6698 EDGE_EXECUTABLE flag on edges, in different ways.
6700 Hence, if we're going to call check_all_array_refs, set
6701 the flag on every edge now, rather than in
6702 check_array_bounds_dom_walker's ctor; vrp_folder may clear
6703 it from some edges. */
6704 if (warn_array_bounds
&& warn_array_bounds_p
)
6705 set_all_edges_as_executable (cfun
);
6707 class vrp_folder vrp_folder
;
6708 vrp_folder
.vr_values
= &vr_values
;
6709 vrp_folder
.substitute_and_fold ();
6711 if (warn_array_bounds
&& warn_array_bounds_p
)
6712 check_all_array_refs ();
6715 /* Main entry point to VRP (Value Range Propagation). This pass is
6716 loosely based on J. R. C. Patterson, ``Accurate Static Branch
6717 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
6718 Programming Language Design and Implementation, pp. 67-78, 1995.
6719 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
6721 This is essentially an SSA-CCP pass modified to deal with ranges
6722 instead of constants.
6724 While propagating ranges, we may find that two or more SSA name
6725 have equivalent, though distinct ranges. For instance,
6728 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
6730 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
6734 In the code above, pointer p_5 has range [q_2, q_2], but from the
6735 code we can also determine that p_5 cannot be NULL and, if q_2 had
6736 a non-varying range, p_5's range should also be compatible with it.
6738 These equivalences are created by two expressions: ASSERT_EXPR and
6739 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
6740 result of another assertion, then we can use the fact that p_5 and
6741 p_4 are equivalent when evaluating p_5's range.
6743 Together with value ranges, we also propagate these equivalences
6744 between names so that we can take advantage of information from
6745 multiple ranges when doing final replacement. Note that this
6746 equivalency relation is transitive but not symmetric.
6748 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
6749 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
6750 in contexts where that assertion does not hold (e.g., in line 6).
6752 TODO, the main difference between this pass and Patterson's is that
6753 we do not propagate edge probabilities. We only compute whether
6754 edges can be taken or not. That is, instead of having a spectrum
6755 of jump probabilities between 0 and 1, we only deal with 0, 1 and
6756 DON'T KNOW. In the future, it may be worthwhile to propagate
6757 probabilities to aid branch prediction. */
6760 execute_vrp (bool warn_array_bounds_p
)
6763 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
6764 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
6767 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
6768 Inserting assertions may split edges which will invalidate
6770 insert_range_assertions ();
6772 threadedge_initialize_values ();
6774 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
6775 mark_dfs_back_edges ();
6777 class vrp_prop vrp_prop
;
6778 vrp_prop
.vrp_initialize ();
6779 vrp_prop
.ssa_propagate ();
6780 vrp_prop
.vrp_finalize (warn_array_bounds_p
);
6782 /* We must identify jump threading opportunities before we release
6783 the datastructures built by VRP. */
6784 identify_jump_threads (&vrp_prop
.vr_values
);
6786 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
6787 was set by a type conversion can often be rewritten to use the
6788 RHS of the type conversion.
6790 However, doing so inhibits jump threading through the comparison.
6791 So that transformation is not performed until after jump threading
6794 FOR_EACH_BB_FN (bb
, cfun
)
6796 gimple
*last
= last_stmt (bb
);
6797 if (last
&& gimple_code (last
) == GIMPLE_COND
)
6798 vrp_prop
.vr_values
.simplify_cond_using_ranges_2 (as_a
<gcond
*> (last
));
6801 free_numbers_of_iterations_estimates (cfun
);
6803 /* ASSERT_EXPRs must be removed before finalizing jump threads
6804 as finalizing jump threads calls the CFG cleanup code which
6805 does not properly handle ASSERT_EXPRs. */
6806 remove_range_assertions ();
6808 /* If we exposed any new variables, go ahead and put them into
6809 SSA form now, before we handle jump threading. This simplifies
6810 interactions between rewriting of _DECL nodes into SSA form
6811 and rewriting SSA_NAME nodes into SSA form after block
6812 duplication and CFG manipulation. */
6813 update_ssa (TODO_update_ssa
);
6815 /* We identified all the jump threading opportunities earlier, but could
6816 not transform the CFG at that time. This routine transforms the
6817 CFG and arranges for the dominator tree to be rebuilt if necessary.
6819 Note the SSA graph update will occur during the normal TODO
6820 processing by the pass manager. */
6821 thread_through_all_blocks (false);
6823 vrp_prop
.vr_values
.cleanup_edges_and_switches ();
6824 threadedge_finalize_values ();
6827 loop_optimizer_finalize ();
6833 const pass_data pass_data_vrp
=
6835 GIMPLE_PASS
, /* type */
6837 OPTGROUP_NONE
, /* optinfo_flags */
6838 TV_TREE_VRP
, /* tv_id */
6839 PROP_ssa
, /* properties_required */
6840 0, /* properties_provided */
6841 0, /* properties_destroyed */
6842 0, /* todo_flags_start */
6843 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
6846 class pass_vrp
: public gimple_opt_pass
6849 pass_vrp (gcc::context
*ctxt
)
6850 : gimple_opt_pass (pass_data_vrp
, ctxt
), warn_array_bounds_p (false)
6853 /* opt_pass methods: */
6854 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
6855 void set_pass_param (unsigned int n
, bool param
)
6857 gcc_assert (n
== 0);
6858 warn_array_bounds_p
= param
;
6860 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
6861 virtual unsigned int execute (function
*)
6862 { return execute_vrp (warn_array_bounds_p
); }
6865 bool warn_array_bounds_p
;
6866 }; // class pass_vrp
6871 make_pass_vrp (gcc::context
*ctxt
)
6873 return new pass_vrp (ctxt
);
6877 /* Worker for determine_value_range. */
6880 determine_value_range_1 (value_range
*vr
, tree expr
)
6882 if (BINARY_CLASS_P (expr
))
6884 value_range vr0
, vr1
;
6885 determine_value_range_1 (&vr0
, TREE_OPERAND (expr
, 0));
6886 determine_value_range_1 (&vr1
, TREE_OPERAND (expr
, 1));
6887 extract_range_from_binary_expr_1 (vr
, TREE_CODE (expr
), TREE_TYPE (expr
),
6890 else if (UNARY_CLASS_P (expr
))
6893 determine_value_range_1 (&vr0
, TREE_OPERAND (expr
, 0));
6894 extract_range_from_unary_expr (vr
, TREE_CODE (expr
), TREE_TYPE (expr
),
6895 &vr0
, TREE_TYPE (TREE_OPERAND (expr
, 0)));
6897 else if (TREE_CODE (expr
) == INTEGER_CST
)
6898 set_value_range_to_value (vr
, expr
, NULL
);
6901 value_range_kind kind
;
6903 /* For SSA names try to extract range info computed by VRP. Otherwise
6904 fall back to varying. */
6905 if (TREE_CODE (expr
) == SSA_NAME
6906 && INTEGRAL_TYPE_P (TREE_TYPE (expr
))
6907 && (kind
= get_range_info (expr
, &min
, &max
)) != VR_VARYING
)
6908 set_value_range (vr
, kind
, wide_int_to_tree (TREE_TYPE (expr
), min
),
6909 wide_int_to_tree (TREE_TYPE (expr
), max
), NULL
);
6911 set_value_range_to_varying (vr
);
6915 /* Compute a value-range for EXPR and set it in *MIN and *MAX. Return
6916 the determined range type. */
6919 determine_value_range (tree expr
, wide_int
*min
, wide_int
*max
)
6922 determine_value_range_1 (&vr
, expr
);
6923 if (vr
.constant_p ())
6925 *min
= wi::to_wide (vr
.min ());
6926 *max
= wi::to_wide (vr
.max ());