1 /* Support routines for value ranges.
2 Copyright (C) 2019-2022 Free Software Foundation, Inc.
3 Major hacks by Aldy Hernandez <aldyh@redhat.com> and
4 Andrew MacLeod <amacleod@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "tree-pretty-print.h"
30 #include "value-range-pretty-print.h"
31 #include "fold-const.h"
32 #include "gimple-range.h"
35 irange::accept (const vrange_visitor
&v
) const
41 unsupported_range::accept (const vrange_visitor
&v
) const
46 // Convenience function only available for integers and pointers.
49 Value_Range::lower_bound () const
51 if (is_a
<irange
> (*m_vrange
))
52 return as_a
<irange
> (*m_vrange
).lower_bound ();
56 // Convenience function only available for integers and pointers.
59 Value_Range::upper_bound () const
61 if (is_a
<irange
> (*m_vrange
))
62 return as_a
<irange
> (*m_vrange
).upper_bound ();
67 Value_Range::dump (FILE *out
) const
72 fprintf (out
, "NULL");
76 debug (const Value_Range
&r
)
79 fprintf (stderr
, "\n");
82 // Default vrange definitions.
85 vrange::contains_p (tree
) const
91 vrange::singleton_p (tree
*) const
97 vrange::set (tree min
, tree
, value_range_kind
)
99 set_varying (TREE_TYPE (min
));
103 vrange::type () const
105 return void_type_node
;
109 vrange::supports_type_p (const_tree
) const
115 vrange::set_undefined ()
117 m_kind
= VR_UNDEFINED
;
121 vrange::set_varying (tree
)
127 vrange::union_ (const vrange
&r
)
129 if (r
.undefined_p () || varying_p ())
131 if (undefined_p () || r
.varying_p ())
141 vrange::intersect (const vrange
&r
)
143 if (undefined_p () || r
.varying_p ())
145 if (r
.undefined_p ())
160 vrange::zero_p () const
166 vrange::nonzero_p () const
172 vrange::set_nonzero (tree type
)
178 vrange::set_zero (tree type
)
184 vrange::set_nonnegative (tree type
)
190 vrange::fits_p (const vrange
&) const
195 // Assignment operator for generic ranges. Copying incompatible types
199 vrange::operator= (const vrange
&src
)
201 if (is_a
<irange
> (src
))
202 as_a
<irange
> (*this) = as_a
<irange
> (src
);
203 else if (is_a
<frange
> (src
))
204 as_a
<frange
> (*this) = as_a
<frange
> (src
);
210 // Equality operator for generic ranges.
213 vrange::operator== (const vrange
&src
) const
215 if (is_a
<irange
> (src
))
216 return as_a
<irange
> (*this) == as_a
<irange
> (src
);
217 if (is_a
<frange
> (src
))
218 return as_a
<frange
> (*this) == as_a
<frange
> (src
);
222 // Wrapper for vrange_printer to dump a range to a file.
225 vrange::dump (FILE *file
) const
227 pretty_printer buffer
;
228 pp_needs_newline (&buffer
) = true;
229 buffer
.buffer
->stream
= file
;
230 vrange_printer
vrange_pp (&buffer
);
231 this->accept (vrange_pp
);
236 irange::supports_type_p (const_tree type
) const
238 return supports_p (type
);
241 // Return TRUE if R fits in THIS.
244 irange::fits_p (const vrange
&r
) const
246 return m_max_ranges
>= as_a
<irange
> (r
).num_pairs ();
250 irange::set_nonnegative (tree type
)
252 set (build_int_cst (type
, 0), TYPE_MAX_VALUE (type
));
256 frange::accept (const vrange_visitor
&v
) const
261 // Flush denormal endpoints to the appropriate 0.0.
264 frange::flush_denormals_to_zero ()
266 if (undefined_p () || known_isnan ())
269 // Flush [x, -DENORMAL] to [x, -0.0].
270 if (real_isdenormal (&m_max
) && real_isneg (&m_max
))
273 if (HONOR_SIGNED_ZEROS (m_type
))
276 // Flush [+DENORMAL, x] to [+0.0, x].
277 if (real_isdenormal (&m_min
) && !real_isneg (&m_min
))
281 // Setter for franges.
284 frange::set (tree type
,
285 const REAL_VALUE_TYPE
&min
, const REAL_VALUE_TYPE
&max
,
286 value_range_kind kind
)
304 if (real_isnan (&min
) || real_isnan (&max
))
306 gcc_checking_assert (real_identical (&min
, &max
));
307 bool sign
= real_isneg (&min
);
308 set_nan (type
, sign
);
316 if (HONOR_NANS (m_type
))
327 // For -ffinite-math-only we can drop ranges outside the
328 // representable numbers to min/max for the type.
329 if (flag_finite_math_only
)
331 REAL_VALUE_TYPE min_repr
= frange_val_min (m_type
);
332 REAL_VALUE_TYPE max_repr
= frange_val_max (m_type
);
333 if (real_less (&m_min
, &min_repr
))
335 if (real_less (&max_repr
, &m_max
))
339 // Check for swapped ranges.
340 gcc_checking_assert (real_compare (LE_EXPR
, &min
, &max
));
344 flush_denormals_to_zero ();
351 frange::set (tree min
, tree max
, value_range_kind kind
)
353 set (TREE_TYPE (min
),
354 *TREE_REAL_CST_PTR (min
), *TREE_REAL_CST_PTR (max
), kind
);
357 // Normalize range to VARYING or UNDEFINED, or vice versa. Return
358 // TRUE if anything changed.
360 // A range with no known properties can be dropped to VARYING.
361 // Similarly, a VARYING with any properties should be dropped to a
362 // VR_RANGE. Normalizing ranges upon changing them ensures there is
363 // only one representation for a given range.
366 frange::normalize_kind ()
368 if (m_kind
== VR_RANGE
369 && frange_val_is_min (m_min
, m_type
)
370 && frange_val_is_max (m_max
, m_type
))
372 if (m_pos_nan
&& m_neg_nan
)
374 set_varying (m_type
);
378 else if (m_kind
== VR_VARYING
)
380 if (!m_pos_nan
|| !m_neg_nan
)
383 m_min
= frange_val_min (m_type
);
384 m_max
= frange_val_max (m_type
);
388 else if (m_kind
== VR_NAN
&& !m_pos_nan
&& !m_neg_nan
)
393 // Union or intersect the zero endpoints of two ranges. For example:
394 // [-0, x] U [+0, x] => [-0, x]
395 // [ x, -0] U [ x, +0] => [ x, +0]
396 // [-0, x] ^ [+0, x] => [+0, x]
397 // [ x, -0] ^ [ x, +0] => [ x, -0]
399 // UNION_P is true when performing a union, or false when intersecting.
402 frange::combine_zeros (const frange
&r
, bool union_p
)
404 gcc_checking_assert (!undefined_p () && !known_isnan ());
406 bool changed
= false;
407 if (real_iszero (&m_min
) && real_iszero (&r
.m_min
)
408 && real_isneg (&m_min
) != real_isneg (&r
.m_min
))
410 m_min
.sign
= union_p
;
413 if (real_iszero (&m_max
) && real_iszero (&r
.m_max
)
414 && real_isneg (&m_max
) != real_isneg (&r
.m_max
))
416 m_max
.sign
= !union_p
;
419 // If the signs are swapped, the resulting range is empty.
420 if (m_min
.sign
== 0 && m_max
.sign
== 1)
431 // Union two ranges when one is known to be a NAN.
434 frange::union_nans (const frange
&r
)
436 gcc_checking_assert (known_isnan () || r
.known_isnan ());
444 m_pos_nan
|= r
.m_pos_nan
;
445 m_neg_nan
|= r
.m_neg_nan
;
453 frange::union_ (const vrange
&v
)
455 const frange
&r
= as_a
<frange
> (v
);
457 if (r
.undefined_p () || varying_p ())
459 if (undefined_p () || r
.varying_p ())
466 if (known_isnan () || r
.known_isnan ())
467 return union_nans (r
);
468 bool changed
= false;
469 if (m_pos_nan
!= r
.m_pos_nan
|| m_neg_nan
!= r
.m_neg_nan
)
471 m_pos_nan
|= r
.m_pos_nan
;
472 m_neg_nan
|= r
.m_neg_nan
;
476 // Combine endpoints.
477 if (real_less (&r
.m_min
, &m_min
))
482 if (real_less (&m_max
, &r
.m_max
))
488 if (HONOR_SIGNED_ZEROS (m_type
))
489 changed
|= combine_zeros (r
, true);
491 changed
|= normalize_kind ();
497 // Intersect two ranges when one is known to be a NAN.
500 frange::intersect_nans (const frange
&r
)
502 gcc_checking_assert (known_isnan () || r
.known_isnan ());
504 m_pos_nan
&= r
.m_pos_nan
;
505 m_neg_nan
&= r
.m_neg_nan
;
516 frange::intersect (const vrange
&v
)
518 const frange
&r
= as_a
<frange
> (v
);
520 if (undefined_p () || r
.varying_p ())
522 if (r
.undefined_p ())
534 if (known_isnan () || r
.known_isnan ())
535 return intersect_nans (r
);
536 bool changed
= false;
537 if (m_pos_nan
!= r
.m_pos_nan
|| m_neg_nan
!= r
.m_neg_nan
)
539 m_pos_nan
&= r
.m_pos_nan
;
540 m_neg_nan
&= r
.m_neg_nan
;
544 // Combine endpoints.
545 if (real_less (&m_min
, &r
.m_min
))
550 if (real_less (&r
.m_max
, &m_max
))
555 // If the endpoints are swapped, the resulting range is empty.
556 if (real_less (&m_max
, &m_min
))
567 if (HONOR_SIGNED_ZEROS (m_type
))
568 changed
|= combine_zeros (r
, false);
570 changed
|= normalize_kind ();
577 frange::operator= (const frange
&src
)
583 m_pos_nan
= src
.m_pos_nan
;
584 m_neg_nan
= src
.m_neg_nan
;
592 frange::operator== (const frange
&src
) const
594 if (m_kind
== src
.m_kind
)
600 return types_compatible_p (m_type
, src
.m_type
);
602 if (known_isnan () || src
.known_isnan ())
605 return (real_identical (&m_min
, &src
.m_min
)
606 && real_identical (&m_max
, &src
.m_max
)
607 && m_pos_nan
== src
.m_pos_nan
608 && m_neg_nan
== src
.m_neg_nan
609 && types_compatible_p (m_type
, src
.m_type
));
614 // Return TRUE if range contains the TREE_REAL_CST_PTR in CST.
617 frange::contains_p (tree cst
) const
619 gcc_checking_assert (m_kind
!= VR_ANTI_RANGE
);
620 const REAL_VALUE_TYPE
*rv
= TREE_REAL_CST_PTR (cst
);
631 if (!m_pos_nan
&& !m_neg_nan
)
633 // Both +NAN and -NAN are present.
634 if (m_pos_nan
&& m_neg_nan
)
636 return m_neg_nan
== rv
->sign
;
641 if (real_compare (GE_EXPR
, rv
, &m_min
) && real_compare (LE_EXPR
, rv
, &m_max
))
643 // Make sure the signs are equal for signed zeros.
644 if (HONOR_SIGNED_ZEROS (m_type
) && real_iszero (rv
))
645 return m_min
.sign
== m_max
.sign
&& m_min
.sign
== rv
->sign
;
651 // If range is a singleton, place it in RESULT and return TRUE. If
652 // RESULT is NULL, just return TRUE.
654 // A NAN can never be a singleton.
657 frange::singleton_p (tree
*result
) const
659 if (m_kind
== VR_RANGE
&& real_identical (&m_min
, &m_max
))
661 // Return false for any singleton that may be a NAN.
662 if (HONOR_NANS (m_type
) && maybe_isnan ())
665 if (MODE_COMPOSITE_P (TYPE_MODE (m_type
)))
667 // For IBM long doubles, if the value is +-Inf or is exactly
668 // representable in double, the other double could be +0.0
669 // or -0.0. Since this means there is more than one way to
670 // represent a value, return false to avoid propagating it.
671 // See libgcc/config/rs6000/ibm-ldouble-format for details.
672 if (real_isinf (&m_min
))
675 real_convert (&r
, DFmode
, &m_min
);
676 if (real_identical (&r
, &m_min
))
681 *result
= build_real (m_type
, m_min
);
688 frange::supports_type_p (const_tree type
) const
690 return supports_p (type
);
694 frange::verify_range ()
699 gcc_checking_assert (!m_type
);
702 gcc_checking_assert (m_type
);
703 gcc_checking_assert (m_pos_nan
&& m_neg_nan
);
704 gcc_checking_assert (frange_val_is_min (m_min
, m_type
));
705 gcc_checking_assert (frange_val_is_max (m_max
, m_type
));
708 gcc_checking_assert (m_type
);
711 gcc_checking_assert (m_type
);
712 gcc_checking_assert (m_pos_nan
|| m_neg_nan
);
718 // NANs cannot appear in the endpoints of a range.
719 gcc_checking_assert (!real_isnan (&m_min
) && !real_isnan (&m_max
));
721 // Make sure we don't have swapped ranges.
722 gcc_checking_assert (!real_less (&m_max
, &m_min
));
724 // [ +0.0, -0.0 ] is nonsensical.
725 gcc_checking_assert (!(real_iszero (&m_min
, 0) && real_iszero (&m_max
, 1)));
727 // If all the properties are clear, we better not span the entire
728 // domain, because that would make us varying.
729 if (m_pos_nan
&& m_neg_nan
)
730 gcc_checking_assert (!frange_val_is_min (m_min
, m_type
)
731 || !frange_val_is_max (m_max
, m_type
));
734 // We can't do much with nonzeros yet.
736 frange::set_nonzero (tree type
)
741 // We can't do much with nonzeros yet.
743 frange::nonzero_p () const
748 // Set range to [+0.0, +0.0] if honoring signed zeros, or [0.0, 0.0]
752 frange::set_zero (tree type
)
754 if (HONOR_SIGNED_ZEROS (type
))
756 REAL_VALUE_TYPE dconstm0
= dconst0
;
758 set (type
, dconstm0
, dconst0
);
762 set (type
, dconst0
, dconst0
);
765 // Return TRUE for any zero regardless of sign.
768 frange::zero_p () const
770 return (m_kind
== VR_RANGE
771 && real_iszero (&m_min
)
772 && real_iszero (&m_max
));
776 frange::set_nonnegative (tree type
)
778 set (type
, dconst0
, frange_val_max (type
));
780 // Set +NAN as the only possibility.
781 if (HONOR_NANS (type
))
782 update_nan (/*sign=*/0);
785 // Here we copy between any two irange's. The ranges can be legacy or
786 // multi-ranges, and copying between any combination works correctly.
789 irange::operator= (const irange
&src
)
791 if (legacy_mode_p ())
793 copy_to_legacy (src
);
796 if (src
.legacy_mode_p ())
798 copy_legacy_to_multi_range (src
);
803 unsigned lim
= src
.m_num_ranges
;
804 if (lim
> m_max_ranges
)
807 for (x
= 0; x
< lim
* 2; ++x
)
808 m_base
[x
] = src
.m_base
[x
];
810 // If the range didn't fit, the last range should cover the rest.
811 if (lim
!= src
.m_num_ranges
)
812 m_base
[x
- 1] = src
.m_base
[src
.m_num_ranges
* 2 - 1];
816 m_nonzero_mask
= src
.m_nonzero_mask
;
822 // Return TRUE if range is a multi-range that can be represented as a
826 irange::maybe_anti_range () const
828 tree ttype
= type ();
829 unsigned int precision
= TYPE_PRECISION (ttype
);
830 signop sign
= TYPE_SIGN (ttype
);
831 return (num_pairs () > 1
833 && lower_bound () == wi::min_value (precision
, sign
)
834 && upper_bound () == wi::max_value (precision
, sign
));
838 irange::copy_legacy_to_multi_range (const irange
&src
)
840 gcc_checking_assert (src
.legacy_mode_p ());
841 gcc_checking_assert (!legacy_mode_p ());
842 if (src
.undefined_p ())
844 else if (src
.varying_p ())
845 set_varying (src
.type ());
848 if (range_has_numeric_bounds_p (&src
))
849 set (src
.min (), src
.max (), src
.kind ());
852 value_range
cst (src
);
853 cst
.normalize_symbolics ();
854 gcc_checking_assert (cst
.varying_p () || cst
.kind () == VR_RANGE
);
855 set (cst
.min (), cst
.max ());
860 // Copy any type of irange into a legacy.
863 irange::copy_to_legacy (const irange
&src
)
865 gcc_checking_assert (legacy_mode_p ());
866 // Handle legacy to legacy and other things that are easy to copy.
867 if (src
.legacy_mode_p () || src
.varying_p () || src
.undefined_p ())
869 m_num_ranges
= src
.m_num_ranges
;
870 m_base
[0] = src
.m_base
[0];
871 m_base
[1] = src
.m_base
[1];
873 m_nonzero_mask
= src
.m_nonzero_mask
;
876 // Copy multi-range to legacy.
877 if (src
.maybe_anti_range ())
879 int_range
<3> r (src
);
881 // Use tree variants to save on tree -> wi -> tree conversions.
882 set (r
.tree_lower_bound (0), r
.tree_upper_bound (0), VR_ANTI_RANGE
);
885 set (src
.tree_lower_bound (), src
.tree_upper_bound ());
888 // Swap MIN/MAX if they are out of order and adjust KIND appropriately.
891 swap_out_of_order_endpoints (tree
&min
, tree
&max
, value_range_kind
&kind
)
893 gcc_checking_assert (kind
!= VR_UNDEFINED
);
894 if (kind
== VR_VARYING
)
896 /* Wrong order for min and max, to swap them and the VR type we need
898 if (tree_int_cst_lt (max
, min
))
902 /* For one bit precision if max < min, then the swapped
903 range covers all values, so for VR_RANGE it is varying and
904 for VR_ANTI_RANGE empty range, so drop to varying as well. */
905 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
911 one
= build_int_cst (TREE_TYPE (min
), 1);
912 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
913 max
= int_const_binop (MINUS_EXPR
, min
, one
);
916 /* There's one corner case, if we had [C+1, C] before we now have
917 that again. But this represents an empty value range, so drop
918 to varying in this case. */
919 if (tree_int_cst_lt (max
, min
))
924 kind
= kind
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
929 irange::irange_set (tree min
, tree max
)
931 gcc_checking_assert (!POLY_INT_CST_P (min
));
932 gcc_checking_assert (!POLY_INT_CST_P (max
));
938 m_nonzero_mask
= NULL
;
946 irange::irange_set_1bit_anti_range (tree min
, tree max
)
948 tree type
= TREE_TYPE (min
);
949 gcc_checking_assert (TYPE_PRECISION (type
) == 1);
951 if (operand_equal_p (min
, max
))
953 // Since these are 1-bit quantities, they can only be [MIN,MIN]
955 if (vrp_val_is_min (min
))
956 min
= max
= vrp_val_max (type
);
958 min
= max
= vrp_val_min (type
);
963 // The only alternative is [MIN,MAX], which is the empty range.
964 gcc_checking_assert (vrp_val_is_min (min
));
965 gcc_checking_assert (vrp_val_is_max (max
));
973 irange::irange_set_anti_range (tree min
, tree max
)
975 gcc_checking_assert (!POLY_INT_CST_P (min
));
976 gcc_checking_assert (!POLY_INT_CST_P (max
));
978 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
980 irange_set_1bit_anti_range (min
, max
);
985 tree type
= TREE_TYPE (min
);
986 signop sign
= TYPE_SIGN (type
);
987 int_range
<2> type_range (type
);
988 // Calculate INVERSE([I,J]) as [-MIN, I-1][J+1, +MAX].
990 wi::overflow_type ovf
;
992 wide_int w_min
= wi::to_wide (min
);
993 if (wi::ne_p (w_min
, type_range
.lower_bound ()))
995 wide_int lim1
= wi::sub (w_min
, 1, sign
, &ovf
);
996 gcc_checking_assert (ovf
!= wi::OVF_OVERFLOW
);
997 m_base
[0] = type_range
.tree_lower_bound (0);
998 m_base
[1] = wide_int_to_tree (type
, lim1
);
1001 wide_int w_max
= wi::to_wide (max
);
1002 if (wi::ne_p (w_max
, type_range
.upper_bound ()))
1004 wide_int lim2
= wi::add (w_max
, 1, sign
, &ovf
);
1005 gcc_checking_assert (ovf
!= wi::OVF_OVERFLOW
);
1006 m_base
[m_num_ranges
* 2] = wide_int_to_tree (type
, lim2
);
1007 m_base
[m_num_ranges
* 2 + 1] = type_range
.tree_upper_bound (0);
1012 m_nonzero_mask
= NULL
;
1019 /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
1020 This means adjusting VRTYPE, MIN and MAX representing the case of a
1021 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
1022 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
1023 In corner cases where MAX+1 or MIN-1 wraps this will fall back
1025 This routine exists to ease canonicalization in the case where we
1026 extract ranges from var + CST op limit. */
1029 irange::set (tree min
, tree max
, value_range_kind kind
)
1031 if (kind
== VR_UNDEFINED
)
1033 irange::set_undefined ();
1037 if (kind
== VR_VARYING
1038 || POLY_INT_CST_P (min
)
1039 || POLY_INT_CST_P (max
))
1041 set_varying (TREE_TYPE (min
));
1045 if (TREE_OVERFLOW_P (min
))
1046 min
= drop_tree_overflow (min
);
1047 if (TREE_OVERFLOW_P (max
))
1048 max
= drop_tree_overflow (max
);
1050 if (!legacy_mode_p ())
1052 if (kind
== VR_RANGE
)
1053 irange_set (min
, max
);
1056 gcc_checking_assert (kind
== VR_ANTI_RANGE
);
1057 irange_set_anti_range (min
, max
);
1061 // Nothing to canonicalize for symbolic ranges.
1062 if (TREE_CODE (min
) != INTEGER_CST
1063 || TREE_CODE (max
) != INTEGER_CST
)
1069 m_nonzero_mask
= NULL
;
1073 swap_out_of_order_endpoints (min
, max
, kind
);
1074 if (kind
== VR_VARYING
)
1076 set_varying (TREE_TYPE (min
));
1080 // Anti-ranges that can be represented as ranges should be so.
1081 if (kind
== VR_ANTI_RANGE
)
1083 bool is_min
= vrp_val_is_min (min
);
1084 bool is_max
= vrp_val_is_max (max
);
1086 if (is_min
&& is_max
)
1088 // Fall through. This will either be normalized as
1089 // VR_UNDEFINED if the anti-range spans the entire
1090 // precision, or it will remain an VR_ANTI_RANGE in the case
1091 // of an -fstrict-enum where [MIN,MAX] is less than the span
1092 // of underlying precision.
1094 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
1096 irange_set_1bit_anti_range (min
, max
);
1101 tree one
= build_int_cst (TREE_TYPE (max
), 1);
1102 min
= int_const_binop (PLUS_EXPR
, max
, one
);
1103 max
= vrp_val_max (TREE_TYPE (max
));
1108 tree one
= build_int_cst (TREE_TYPE (min
), 1);
1109 max
= int_const_binop (MINUS_EXPR
, min
, one
);
1110 min
= vrp_val_min (TREE_TYPE (min
));
1119 m_nonzero_mask
= NULL
;
1125 // Check the validity of the range.
1128 irange::verify_range ()
1130 gcc_checking_assert (m_discriminator
== VR_IRANGE
);
1131 if (m_kind
== VR_UNDEFINED
)
1133 gcc_checking_assert (m_num_ranges
== 0);
1136 if (m_kind
== VR_VARYING
)
1138 gcc_checking_assert (!m_nonzero_mask
1139 || wi::to_wide (m_nonzero_mask
) == -1);
1140 gcc_checking_assert (m_num_ranges
== 1);
1141 gcc_checking_assert (varying_compatible_p ());
1144 if (!legacy_mode_p ())
1146 gcc_checking_assert (m_num_ranges
!= 0);
1147 gcc_checking_assert (!varying_compatible_p ());
1148 for (unsigned i
= 0; i
< m_num_ranges
; ++i
)
1150 tree lb
= tree_lower_bound (i
);
1151 tree ub
= tree_upper_bound (i
);
1152 int c
= compare_values (lb
, ub
);
1153 gcc_checking_assert (c
== 0 || c
== -1);
1157 if (m_kind
== VR_RANGE
|| m_kind
== VR_ANTI_RANGE
)
1159 gcc_checking_assert (m_num_ranges
== 1);
1160 int cmp
= compare_values (tree_lower_bound (0), tree_upper_bound (0));
1161 gcc_checking_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
1165 // Return the lower bound for a sub-range. PAIR is the sub-range in
1169 irange::legacy_lower_bound (unsigned pair
) const
1171 gcc_checking_assert (legacy_mode_p ());
1174 value_range
numeric_range (*this);
1175 numeric_range
.normalize_symbolics ();
1176 return numeric_range
.legacy_lower_bound (pair
);
1178 gcc_checking_assert (m_num_ranges
> 0);
1179 gcc_checking_assert (pair
+ 1 <= num_pairs ());
1180 if (m_kind
== VR_ANTI_RANGE
)
1182 tree typ
= type (), t
;
1183 if (pair
== 1 || vrp_val_is_min (min ()))
1184 t
= wide_int_to_tree (typ
, wi::to_wide (max ()) + 1);
1186 t
= vrp_val_min (typ
);
1187 return wi::to_wide (t
);
1189 return wi::to_wide (tree_lower_bound (pair
));
1192 // Return the upper bound for a sub-range. PAIR is the sub-range in
1196 irange::legacy_upper_bound (unsigned pair
) const
1198 gcc_checking_assert (legacy_mode_p ());
1201 value_range
numeric_range (*this);
1202 numeric_range
.normalize_symbolics ();
1203 return numeric_range
.legacy_upper_bound (pair
);
1205 gcc_checking_assert (m_num_ranges
> 0);
1206 gcc_checking_assert (pair
+ 1 <= num_pairs ());
1207 if (m_kind
== VR_ANTI_RANGE
)
1209 tree typ
= type (), t
;
1210 if (pair
== 1 || vrp_val_is_min (min ()))
1211 t
= vrp_val_max (typ
);
1213 t
= wide_int_to_tree (typ
, wi::to_wide (min ()) - 1);
1214 return wi::to_wide (t
);
1216 return wi::to_wide (tree_upper_bound (pair
));
1220 irange::legacy_equal_p (const irange
&other
) const
1222 gcc_checking_assert (legacy_mode_p () && other
.legacy_mode_p ());
1224 if (m_kind
!= other
.m_kind
)
1226 if (m_kind
== VR_UNDEFINED
)
1228 if (m_kind
== VR_VARYING
)
1229 return range_compatible_p (type (), other
.type ());
1230 return (vrp_operand_equal_p (tree_lower_bound (0),
1231 other
.tree_lower_bound (0))
1232 && vrp_operand_equal_p (tree_upper_bound (0),
1233 other
.tree_upper_bound (0))
1234 && get_nonzero_bits () == other
.get_nonzero_bits ());
1238 irange::operator== (const irange
&other
) const
1240 if (legacy_mode_p ())
1242 if (other
.legacy_mode_p ())
1243 return legacy_equal_p (other
);
1244 value_range
tmp (other
);
1245 return legacy_equal_p (tmp
);
1247 if (other
.legacy_mode_p ())
1249 value_range
tmp2 (*this);
1250 return tmp2
.legacy_equal_p (other
);
1253 if (m_num_ranges
!= other
.m_num_ranges
)
1256 if (m_num_ranges
== 0)
1259 for (unsigned i
= 0; i
< m_num_ranges
; ++i
)
1261 tree lb
= tree_lower_bound (i
);
1262 tree ub
= tree_upper_bound (i
);
1263 tree lb_other
= other
.tree_lower_bound (i
);
1264 tree ub_other
= other
.tree_upper_bound (i
);
1265 if (!operand_equal_p (lb
, lb_other
, 0)
1266 || !operand_equal_p (ub
, ub_other
, 0))
1269 return get_nonzero_bits () == other
.get_nonzero_bits ();
1272 /* Return TRUE if this is a symbolic range. */
1275 irange::symbolic_p () const
1277 return (m_num_ranges
> 0
1278 && (!is_gimple_min_invariant (min ())
1279 || !is_gimple_min_invariant (max ())));
1282 /* Return TRUE if this is a constant range. */
1285 irange::constant_p () const
1287 return (m_num_ranges
> 0
1288 && TREE_CODE (min ()) == INTEGER_CST
1289 && TREE_CODE (max ()) == INTEGER_CST
);
1292 /* If range is a singleton, place it in RESULT and return TRUE.
1293 Note: A singleton can be any gimple invariant, not just constants.
1294 So, [&x, &x] counts as a singleton. */
1297 irange::singleton_p (tree
*result
) const
1299 if (!legacy_mode_p ())
1301 if (num_pairs () == 1 && (wi::to_wide (tree_lower_bound ())
1302 == wi::to_wide (tree_upper_bound ())))
1305 *result
= tree_lower_bound ();
1310 if (m_kind
== VR_ANTI_RANGE
)
1314 if (TYPE_PRECISION (type ()) == 1)
1322 if (num_pairs () == 1)
1324 value_range vr0
, vr1
;
1325 ranges_from_anti_range ((const value_range
*) this, &vr0
, &vr1
);
1326 return vr0
.singleton_p (result
);
1329 // Catches non-numeric extremes as well.
1330 if (m_kind
== VR_RANGE
1331 && vrp_operand_equal_p (min (), max ())
1332 && is_gimple_min_invariant (min ()))
1341 /* Return 1 if VAL is inside value range.
1342 0 if VAL is not inside value range.
1343 -2 if we cannot tell either way.
1345 Benchmark compile/20001226-1.c compilation time after changing this
1349 irange::value_inside_range (tree val
) const
1357 if (!legacy_mode_p () && TREE_CODE (val
) == INTEGER_CST
)
1358 return contains_p (val
);
1360 int cmp1
= operand_less_p (val
, min ());
1364 return m_kind
!= VR_RANGE
;
1366 int cmp2
= operand_less_p (max (), val
);
1370 if (m_kind
== VR_RANGE
)
1376 /* Return TRUE if it is possible that range contains VAL. */
1379 irange::may_contain_p (tree val
) const
1381 return value_inside_range (val
) != 0;
1384 /* Return TRUE if range contains INTEGER_CST. */
1385 /* Return 1 if VAL is inside value range.
1386 0 if VAL is not inside value range.
1388 Benchmark compile/20001226-1.c compilation time after changing this
1393 irange::contains_p (tree cst
) const
1398 if (legacy_mode_p ())
1400 gcc_checking_assert (TREE_CODE (cst
) == INTEGER_CST
);
1403 value_range
numeric_range (*this);
1404 numeric_range
.normalize_symbolics ();
1405 return numeric_range
.contains_p (cst
);
1407 return value_inside_range (cst
) == 1;
1410 gcc_checking_assert (TREE_CODE (cst
) == INTEGER_CST
);
1412 // See if we can exclude CST based on the nonzero bits.
1415 wide_int cstw
= wi::to_wide (cst
);
1416 if (cstw
!= 0 && wi::bit_and (wi::to_wide (m_nonzero_mask
), cstw
) == 0)
1420 signop sign
= TYPE_SIGN (TREE_TYPE (cst
));
1421 wide_int v
= wi::to_wide (cst
);
1422 for (unsigned r
= 0; r
< m_num_ranges
; ++r
)
1424 if (wi::lt_p (v
, lower_bound (r
), sign
))
1426 if (wi::le_p (v
, upper_bound (r
), sign
))
1434 /* Normalize addresses into constants. */
1437 irange::normalize_addresses ()
1442 if (!POINTER_TYPE_P (type ()) || range_has_numeric_bounds_p (this))
1445 if (!range_includes_zero_p (this))
1447 gcc_checking_assert (TREE_CODE (min ()) == ADDR_EXPR
1448 || TREE_CODE (max ()) == ADDR_EXPR
);
1449 set_nonzero (type ());
1452 set_varying (type ());
1455 /* Normalize symbolics and addresses into constants. */
1458 irange::normalize_symbolics ()
1460 if (varying_p () || undefined_p ())
1463 tree ttype
= type ();
1464 bool min_symbolic
= !is_gimple_min_invariant (min ());
1465 bool max_symbolic
= !is_gimple_min_invariant (max ());
1466 if (!min_symbolic
&& !max_symbolic
)
1468 normalize_addresses ();
1472 // [SYM, SYM] -> VARYING
1473 if (min_symbolic
&& max_symbolic
)
1475 set_varying (ttype
);
1478 if (kind () == VR_RANGE
)
1480 // [SYM, NUM] -> [-MIN, NUM]
1483 set (vrp_val_min (ttype
), max ());
1486 // [NUM, SYM] -> [NUM, +MAX]
1487 set (min (), vrp_val_max (ttype
));
1490 gcc_checking_assert (kind () == VR_ANTI_RANGE
);
1491 // ~[SYM, NUM] -> [NUM + 1, +MAX]
1494 if (!vrp_val_is_max (max ()))
1496 tree n
= wide_int_to_tree (ttype
, wi::to_wide (max ()) + 1);
1497 set (n
, vrp_val_max (ttype
));
1500 set_varying (ttype
);
1503 // ~[NUM, SYM] -> [-MIN, NUM - 1]
1504 if (!vrp_val_is_min (min ()))
1506 tree n
= wide_int_to_tree (ttype
, wi::to_wide (min ()) - 1);
1507 set (vrp_val_min (ttype
), n
);
1510 set_varying (ttype
);
1513 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
1514 { VR1TYPE, VR0MIN, VR0MAX } and store the result
1515 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
1516 possible such range. The resulting range is not canonicalized. */
1519 intersect_ranges (enum value_range_kind
*vr0type
,
1520 tree
*vr0min
, tree
*vr0max
,
1521 enum value_range_kind vr1type
,
1522 tree vr1min
, tree vr1max
)
1524 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
1525 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
1527 /* [] is vr0, () is vr1 in the following classification comments. */
1531 if (*vr0type
== vr1type
)
1532 /* Nothing to do for equal ranges. */
1534 else if ((*vr0type
== VR_RANGE
1535 && vr1type
== VR_ANTI_RANGE
)
1536 || (*vr0type
== VR_ANTI_RANGE
1537 && vr1type
== VR_RANGE
))
1539 /* For anti-range with range intersection the result is empty. */
1540 *vr0type
= VR_UNDEFINED
;
1541 *vr0min
= NULL_TREE
;
1542 *vr0max
= NULL_TREE
;
1547 else if (operand_less_p (*vr0max
, vr1min
) == 1
1548 || operand_less_p (vr1max
, *vr0min
) == 1)
1550 /* [ ] ( ) or ( ) [ ]
1551 If the ranges have an empty intersection, the result of the
1552 intersect operation is the range for intersecting an
1553 anti-range with a range or empty when intersecting two ranges. */
1554 if (*vr0type
== VR_RANGE
1555 && vr1type
== VR_ANTI_RANGE
)
1557 else if (*vr0type
== VR_ANTI_RANGE
1558 && vr1type
== VR_RANGE
)
1564 else if (*vr0type
== VR_RANGE
1565 && vr1type
== VR_RANGE
)
1567 *vr0type
= VR_UNDEFINED
;
1568 *vr0min
= NULL_TREE
;
1569 *vr0max
= NULL_TREE
;
1571 else if (*vr0type
== VR_ANTI_RANGE
1572 && vr1type
== VR_ANTI_RANGE
)
1574 /* If the anti-ranges are adjacent to each other merge them. */
1575 if (TREE_CODE (*vr0max
) == INTEGER_CST
1576 && TREE_CODE (vr1min
) == INTEGER_CST
1577 && operand_less_p (*vr0max
, vr1min
) == 1
1578 && integer_onep (int_const_binop (MINUS_EXPR
,
1581 else if (TREE_CODE (vr1max
) == INTEGER_CST
1582 && TREE_CODE (*vr0min
) == INTEGER_CST
1583 && operand_less_p (vr1max
, *vr0min
) == 1
1584 && integer_onep (int_const_binop (MINUS_EXPR
,
1587 /* Else arbitrarily take VR0. */
1590 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
1591 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
1593 /* [ ( ) ] or [( ) ] or [ ( )] */
1594 if (*vr0type
== VR_RANGE
1595 && vr1type
== VR_RANGE
)
1597 /* If both are ranges the result is the inner one. */
1602 else if (*vr0type
== VR_RANGE
1603 && vr1type
== VR_ANTI_RANGE
)
1605 /* Choose the right gap if the left one is empty. */
1608 if (TREE_CODE (vr1max
) != INTEGER_CST
)
1610 else if (TYPE_PRECISION (TREE_TYPE (vr1max
)) == 1
1611 && !TYPE_UNSIGNED (TREE_TYPE (vr1max
)))
1613 = int_const_binop (MINUS_EXPR
, vr1max
,
1614 build_int_cst (TREE_TYPE (vr1max
), -1));
1617 = int_const_binop (PLUS_EXPR
, vr1max
,
1618 build_int_cst (TREE_TYPE (vr1max
), 1));
1620 /* Choose the left gap if the right one is empty. */
1623 if (TREE_CODE (vr1min
) != INTEGER_CST
)
1625 else if (TYPE_PRECISION (TREE_TYPE (vr1min
)) == 1
1626 && !TYPE_UNSIGNED (TREE_TYPE (vr1min
)))
1628 = int_const_binop (PLUS_EXPR
, vr1min
,
1629 build_int_cst (TREE_TYPE (vr1min
), -1));
1632 = int_const_binop (MINUS_EXPR
, vr1min
,
1633 build_int_cst (TREE_TYPE (vr1min
), 1));
1635 /* Choose the anti-range if the range is effectively varying. */
1636 else if (vrp_val_is_min (*vr0min
)
1637 && vrp_val_is_max (*vr0max
))
1643 /* Else choose the range. */
1645 else if (*vr0type
== VR_ANTI_RANGE
1646 && vr1type
== VR_ANTI_RANGE
)
1647 /* If both are anti-ranges the result is the outer one. */
1649 else if (*vr0type
== VR_ANTI_RANGE
1650 && vr1type
== VR_RANGE
)
1652 /* The intersection is empty. */
1653 *vr0type
= VR_UNDEFINED
;
1654 *vr0min
= NULL_TREE
;
1655 *vr0max
= NULL_TREE
;
1660 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
1661 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
1663 /* ( [ ] ) or ([ ] ) or ( [ ]) */
1664 if (*vr0type
== VR_RANGE
1665 && vr1type
== VR_RANGE
)
1666 /* Choose the inner range. */
1668 else if (*vr0type
== VR_ANTI_RANGE
1669 && vr1type
== VR_RANGE
)
1671 /* Choose the right gap if the left is empty. */
1674 *vr0type
= VR_RANGE
;
1675 if (TREE_CODE (*vr0max
) != INTEGER_CST
)
1677 else if (TYPE_PRECISION (TREE_TYPE (*vr0max
)) == 1
1678 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max
)))
1680 = int_const_binop (MINUS_EXPR
, *vr0max
,
1681 build_int_cst (TREE_TYPE (*vr0max
), -1));
1684 = int_const_binop (PLUS_EXPR
, *vr0max
,
1685 build_int_cst (TREE_TYPE (*vr0max
), 1));
1688 /* Choose the left gap if the right is empty. */
1691 *vr0type
= VR_RANGE
;
1692 if (TREE_CODE (*vr0min
) != INTEGER_CST
)
1694 else if (TYPE_PRECISION (TREE_TYPE (*vr0min
)) == 1
1695 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min
)))
1697 = int_const_binop (PLUS_EXPR
, *vr0min
,
1698 build_int_cst (TREE_TYPE (*vr0min
), -1));
1701 = int_const_binop (MINUS_EXPR
, *vr0min
,
1702 build_int_cst (TREE_TYPE (*vr0min
), 1));
1705 /* Choose the anti-range if the range is effectively varying. */
1706 else if (vrp_val_is_min (vr1min
)
1707 && vrp_val_is_max (vr1max
))
1709 /* Choose the anti-range if it is ~[0,0], that range is special
1710 enough to special case when vr1's range is relatively wide.
1711 At least for types bigger than int - this covers pointers
1712 and arguments to functions like ctz. */
1713 else if (*vr0min
== *vr0max
1714 && integer_zerop (*vr0min
)
1715 && ((TYPE_PRECISION (TREE_TYPE (*vr0min
))
1716 >= TYPE_PRECISION (integer_type_node
))
1717 || POINTER_TYPE_P (TREE_TYPE (*vr0min
)))
1718 && TREE_CODE (vr1max
) == INTEGER_CST
1719 && TREE_CODE (vr1min
) == INTEGER_CST
1720 && (wi::clz (wi::to_wide (vr1max
) - wi::to_wide (vr1min
))
1721 < TYPE_PRECISION (TREE_TYPE (*vr0min
)) / 2))
1723 /* Else choose the range. */
1731 else if (*vr0type
== VR_ANTI_RANGE
1732 && vr1type
== VR_ANTI_RANGE
)
1734 /* If both are anti-ranges the result is the outer one. */
1739 else if (vr1type
== VR_ANTI_RANGE
1740 && *vr0type
== VR_RANGE
)
1742 /* The intersection is empty. */
1743 *vr0type
= VR_UNDEFINED
;
1744 *vr0min
= NULL_TREE
;
1745 *vr0max
= NULL_TREE
;
1750 else if ((operand_less_p (vr1min
, *vr0max
) == 1
1751 || operand_equal_p (vr1min
, *vr0max
, 0))
1752 && operand_less_p (*vr0min
, vr1min
) == 1
1753 && operand_less_p (*vr0max
, vr1max
) == 1)
1755 /* [ ( ] ) or [ ]( ) */
1756 if (*vr0type
== VR_ANTI_RANGE
1757 && vr1type
== VR_ANTI_RANGE
)
1759 else if (*vr0type
== VR_RANGE
1760 && vr1type
== VR_RANGE
)
1762 else if (*vr0type
== VR_RANGE
1763 && vr1type
== VR_ANTI_RANGE
)
1765 if (TREE_CODE (vr1min
) == INTEGER_CST
)
1766 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
1767 build_int_cst (TREE_TYPE (vr1min
), 1));
1771 else if (*vr0type
== VR_ANTI_RANGE
1772 && vr1type
== VR_RANGE
)
1774 *vr0type
= VR_RANGE
;
1775 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
1776 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
1777 build_int_cst (TREE_TYPE (*vr0max
), 1));
1785 else if ((operand_less_p (*vr0min
, vr1max
) == 1
1786 || operand_equal_p (*vr0min
, vr1max
, 0))
1787 && operand_less_p (vr1min
, *vr0min
) == 1
1788 && operand_less_p (vr1max
, *vr0max
) == 1)
1790 /* ( [ ) ] or ( )[ ] */
1791 if (*vr0type
== VR_ANTI_RANGE
1792 && vr1type
== VR_ANTI_RANGE
)
1794 else if (*vr0type
== VR_RANGE
1795 && vr1type
== VR_RANGE
)
1797 else if (*vr0type
== VR_RANGE
1798 && vr1type
== VR_ANTI_RANGE
)
1800 if (TREE_CODE (vr1max
) == INTEGER_CST
)
1801 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
1802 build_int_cst (TREE_TYPE (vr1max
), 1));
1806 else if (*vr0type
== VR_ANTI_RANGE
1807 && vr1type
== VR_RANGE
)
1809 *vr0type
= VR_RANGE
;
1810 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
1811 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
1812 build_int_cst (TREE_TYPE (*vr0min
), 1));
1821 /* If we know the intersection is empty, there's no need to
1822 conservatively add anything else to the set. */
1823 if (*vr0type
== VR_UNDEFINED
)
1826 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
1827 result for the intersection. That's always a conservative
1828 correct estimate unless VR1 is a constant singleton range
1829 in which case we choose that. */
1830 if (vr1type
== VR_RANGE
1831 && is_gimple_min_invariant (vr1min
)
1832 && vrp_operand_equal_p (vr1min
, vr1max
))
1840 /* Helper for the intersection operation for value ranges. Given two
1841 ranges VR0 and VR1, set VR0 to the intersection of both ranges.
1842 This may not be the smallest possible such range. */
1845 irange::legacy_intersect (irange
*vr0
, const irange
*vr1
)
1847 gcc_checking_assert (vr0
->legacy_mode_p ());
1848 gcc_checking_assert (vr1
->legacy_mode_p ());
1849 /* If either range is VR_VARYING the other one wins. */
1850 if (vr1
->varying_p ())
1852 if (vr0
->varying_p ())
1854 vr0
->set (vr1
->min (), vr1
->max (), vr1
->kind ());
1858 /* When either range is VR_UNDEFINED the resulting range is
1859 VR_UNDEFINED, too. */
1860 if (vr0
->undefined_p ())
1862 if (vr1
->undefined_p ())
1864 vr0
->set_undefined ();
1868 value_range_kind vr0kind
= vr0
->kind ();
1869 tree vr0min
= vr0
->min ();
1870 tree vr0max
= vr0
->max ();
1872 intersect_ranges (&vr0kind
, &vr0min
, &vr0max
,
1873 vr1
->kind (), vr1
->min (), vr1
->max ());
1875 /* Make sure to canonicalize the result though as the inversion of a
1876 VR_RANGE can still be a VR_RANGE. */
1877 if (vr0kind
== VR_UNDEFINED
)
1878 vr0
->set_undefined ();
1879 else if (vr0kind
== VR_VARYING
)
1881 /* If we failed, use the original VR0. */
1885 vr0
->set (vr0min
, vr0max
, vr0kind
);
1888 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
1889 { VR1TYPE, VR0MIN, VR0MAX } and store the result
1890 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
1891 possible such range. The resulting range is not canonicalized. */
1894 union_ranges (enum value_range_kind
*vr0type
,
1895 tree
*vr0min
, tree
*vr0max
,
1896 enum value_range_kind vr1type
,
1897 tree vr1min
, tree vr1max
)
1899 int cmpmin
= compare_values (*vr0min
, vr1min
);
1900 int cmpmax
= compare_values (*vr0max
, vr1max
);
1901 bool mineq
= cmpmin
== 0;
1902 bool maxeq
= cmpmax
== 0;
1904 /* [] is vr0, () is vr1 in the following classification comments. */
1908 if (*vr0type
== vr1type
)
1909 /* Nothing to do for equal ranges. */
1911 else if ((*vr0type
== VR_RANGE
1912 && vr1type
== VR_ANTI_RANGE
)
1913 || (*vr0type
== VR_ANTI_RANGE
1914 && vr1type
== VR_RANGE
))
1916 /* For anti-range with range union the result is varying. */
1922 else if (operand_less_p (*vr0max
, vr1min
) == 1
1923 || operand_less_p (vr1max
, *vr0min
) == 1)
1925 /* [ ] ( ) or ( ) [ ]
1926 If the ranges have an empty intersection, result of the union
1927 operation is the anti-range or if both are anti-ranges
1929 if (*vr0type
== VR_ANTI_RANGE
1930 && vr1type
== VR_ANTI_RANGE
)
1932 else if (*vr0type
== VR_ANTI_RANGE
1933 && vr1type
== VR_RANGE
)
1935 else if (*vr0type
== VR_RANGE
1936 && vr1type
== VR_ANTI_RANGE
)
1942 else if (*vr0type
== VR_RANGE
1943 && vr1type
== VR_RANGE
)
1945 /* The result is the convex hull of both ranges. */
1946 if (operand_less_p (*vr0max
, vr1min
) == 1)
1948 /* If the result can be an anti-range, create one. */
1949 if (TREE_CODE (*vr0max
) == INTEGER_CST
1950 && TREE_CODE (vr1min
) == INTEGER_CST
1951 && vrp_val_is_min (*vr0min
)
1952 && vrp_val_is_max (vr1max
))
1954 tree min
= int_const_binop (PLUS_EXPR
,
1956 build_int_cst (TREE_TYPE (*vr0max
), 1));
1957 tree max
= int_const_binop (MINUS_EXPR
,
1959 build_int_cst (TREE_TYPE (vr1min
), 1));
1960 if (!operand_less_p (max
, min
))
1962 *vr0type
= VR_ANTI_RANGE
;
1974 /* If the result can be an anti-range, create one. */
1975 if (TREE_CODE (vr1max
) == INTEGER_CST
1976 && TREE_CODE (*vr0min
) == INTEGER_CST
1977 && vrp_val_is_min (vr1min
)
1978 && vrp_val_is_max (*vr0max
))
1980 tree min
= int_const_binop (PLUS_EXPR
,
1982 build_int_cst (TREE_TYPE (vr1max
), 1));
1983 tree max
= int_const_binop (MINUS_EXPR
,
1985 build_int_cst (TREE_TYPE (*vr0min
), 1));
1986 if (!operand_less_p (max
, min
))
1988 *vr0type
= VR_ANTI_RANGE
;
2002 else if ((maxeq
|| cmpmax
== 1)
2003 && (mineq
|| cmpmin
== -1))
2005 /* [ ( ) ] or [( ) ] or [ ( )] */
2006 if (*vr0type
== VR_RANGE
2007 && vr1type
== VR_RANGE
)
2009 else if (*vr0type
== VR_ANTI_RANGE
2010 && vr1type
== VR_ANTI_RANGE
)
2016 else if (*vr0type
== VR_ANTI_RANGE
2017 && vr1type
== VR_RANGE
)
2019 /* Arbitrarily choose the right or left gap. */
2020 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
2021 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
2022 build_int_cst (TREE_TYPE (vr1min
), 1));
2023 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
2024 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
2025 build_int_cst (TREE_TYPE (vr1max
), 1));
2029 else if (*vr0type
== VR_RANGE
2030 && vr1type
== VR_ANTI_RANGE
)
2031 /* The result covers everything. */
2036 else if ((maxeq
|| cmpmax
== -1)
2037 && (mineq
|| cmpmin
== 1))
2039 /* ( [ ] ) or ([ ] ) or ( [ ]) */
2040 if (*vr0type
== VR_RANGE
2041 && vr1type
== VR_RANGE
)
2047 else if (*vr0type
== VR_ANTI_RANGE
2048 && vr1type
== VR_ANTI_RANGE
)
2050 else if (*vr0type
== VR_RANGE
2051 && vr1type
== VR_ANTI_RANGE
)
2053 *vr0type
= VR_ANTI_RANGE
;
2054 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
2056 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
2057 build_int_cst (TREE_TYPE (*vr0min
), 1));
2060 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
2062 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
2063 build_int_cst (TREE_TYPE (*vr0max
), 1));
2069 else if (*vr0type
== VR_ANTI_RANGE
2070 && vr1type
== VR_RANGE
)
2071 /* The result covers everything. */
2076 else if (cmpmin
== -1
2078 && (operand_less_p (vr1min
, *vr0max
) == 1
2079 || operand_equal_p (vr1min
, *vr0max
, 0)))
2081 /* [ ( ] ) or [ ]( ) */
2082 if (*vr0type
== VR_RANGE
2083 && vr1type
== VR_RANGE
)
2085 else if (*vr0type
== VR_ANTI_RANGE
2086 && vr1type
== VR_ANTI_RANGE
)
2088 else if (*vr0type
== VR_ANTI_RANGE
2089 && vr1type
== VR_RANGE
)
2091 if (TREE_CODE (vr1min
) == INTEGER_CST
)
2092 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
2093 build_int_cst (TREE_TYPE (vr1min
), 1));
2097 else if (*vr0type
== VR_RANGE
2098 && vr1type
== VR_ANTI_RANGE
)
2100 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
2103 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
2104 build_int_cst (TREE_TYPE (*vr0max
), 1));
2113 else if (cmpmin
== 1
2115 && (operand_less_p (*vr0min
, vr1max
) == 1
2116 || operand_equal_p (*vr0min
, vr1max
, 0)))
2118 /* ( [ ) ] or ( )[ ] */
2119 if (*vr0type
== VR_RANGE
2120 && vr1type
== VR_RANGE
)
2122 else if (*vr0type
== VR_ANTI_RANGE
2123 && vr1type
== VR_ANTI_RANGE
)
2125 else if (*vr0type
== VR_ANTI_RANGE
2126 && vr1type
== VR_RANGE
)
2128 if (TREE_CODE (vr1max
) == INTEGER_CST
)
2129 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
2130 build_int_cst (TREE_TYPE (vr1max
), 1));
2134 else if (*vr0type
== VR_RANGE
2135 && vr1type
== VR_ANTI_RANGE
)
2137 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
2140 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
2141 build_int_cst (TREE_TYPE (*vr0min
), 1));
2156 *vr0type
= VR_VARYING
;
2157 *vr0min
= NULL_TREE
;
2158 *vr0max
= NULL_TREE
;
2161 /* Helper for meet operation for value ranges. Given two ranges VR0
2162 and VR1, set VR0 to the union of both ranges. This may not be the
2163 smallest possible such range. */
2166 irange::legacy_union (irange
*vr0
, const irange
*vr1
)
2168 gcc_checking_assert (vr0
->legacy_mode_p ());
2169 gcc_checking_assert (vr1
->legacy_mode_p ());
2171 /* VR0 has the resulting range if VR1 is undefined or VR0 is varying. */
2172 if (vr1
->undefined_p ()
2173 || vr0
->varying_p ())
2176 /* VR1 has the resulting range if VR0 is undefined or VR1 is varying. */
2177 if (vr0
->undefined_p ())
2179 vr0
->set (vr1
->min (), vr1
->max (), vr1
->kind ());
2183 if (vr1
->varying_p ())
2185 vr0
->set_varying (vr1
->type ());
2189 value_range_kind vr0kind
= vr0
->kind ();
2190 tree vr0min
= vr0
->min ();
2191 tree vr0max
= vr0
->max ();
2193 union_ranges (&vr0kind
, &vr0min
, &vr0max
,
2194 vr1
->kind (), vr1
->min (), vr1
->max ());
2196 if (vr0kind
== VR_UNDEFINED
)
2197 vr0
->set_undefined ();
2198 else if (vr0kind
== VR_VARYING
)
2200 /* Failed to find an efficient meet. Before giving up and
2201 setting the result to VARYING, see if we can at least derive
2202 a non-zero range. */
2203 if (range_includes_zero_p (vr0
) == 0
2204 && range_includes_zero_p (vr1
) == 0)
2205 vr0
->set_nonzero (vr0
->type ());
2207 vr0
->set_varying (vr0
->type ());
2210 vr0
->set (vr0min
, vr0max
, vr0kind
);
2213 /* Meet operation for value ranges. Given two value ranges VR0 and
2214 VR1, store in VR0 a range that contains both VR0 and VR1. This
2215 may not be the smallest possible such range.
2216 Return TRUE if the original value changes. */
2219 irange::legacy_verbose_union_ (const irange
*other
)
2221 if (legacy_mode_p ())
2223 if (!other
->legacy_mode_p ())
2225 int_range
<1> tmp
= *other
;
2226 legacy_union (this, &tmp
);
2229 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2231 fprintf (dump_file
, "Meeting\n ");
2232 dump_value_range (dump_file
, this);
2233 fprintf (dump_file
, "\nand\n ");
2234 dump_value_range (dump_file
, other
);
2235 fprintf (dump_file
, "\n");
2238 legacy_union (this, other
);
2240 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2242 fprintf (dump_file
, "to\n ");
2243 dump_value_range (dump_file
, this);
2244 fprintf (dump_file
, "\n");
2249 if (other
->legacy_mode_p ())
2251 int_range
<2> wider
= *other
;
2252 return irange_union (wider
);
2255 return irange_union (*other
);
2259 irange::legacy_verbose_intersect (const irange
*other
)
2261 if (legacy_mode_p ())
2263 if (!other
->legacy_mode_p ())
2265 int_range
<1> tmp
= *other
;
2266 legacy_intersect (this, &tmp
);
2269 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2271 fprintf (dump_file
, "Intersecting\n ");
2272 dump_value_range (dump_file
, this);
2273 fprintf (dump_file
, "\nand\n ");
2274 dump_value_range (dump_file
, other
);
2275 fprintf (dump_file
, "\n");
2278 legacy_intersect (this, other
);
2280 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2282 fprintf (dump_file
, "to\n ");
2283 dump_value_range (dump_file
, this);
2284 fprintf (dump_file
, "\n");
2289 if (other
->legacy_mode_p ())
2293 return irange_intersect (wider
);
2296 return irange_intersect (*other
);
2299 // Perform an efficient union with R when both ranges have only a single pair.
2300 // Excluded are VARYING and UNDEFINED ranges.
2303 irange::irange_single_pair_union (const irange
&r
)
2305 gcc_checking_assert (!undefined_p () && !varying_p ());
2306 gcc_checking_assert (!r
.undefined_p () && !varying_p ());
2308 signop sign
= TYPE_SIGN (TREE_TYPE (m_base
[0]));
2309 // Check if current lower bound is also the new lower bound.
2310 if (wi::le_p (wi::to_wide (m_base
[0]), wi::to_wide (r
.m_base
[0]), sign
))
2312 // If current upper bound is new upper bound, we're done.
2313 if (wi::le_p (wi::to_wide (r
.m_base
[1]), wi::to_wide (m_base
[1]), sign
))
2314 return union_nonzero_bits (r
);
2315 // Otherwise R has the new upper bound.
2316 // Check for overlap/touching ranges, or single target range.
2317 if (m_max_ranges
== 1
2318 || wi::to_widest (m_base
[1]) + 1 >= wi::to_widest (r
.m_base
[0]))
2319 m_base
[1] = r
.m_base
[1];
2322 // This is a dual range result.
2323 m_base
[2] = r
.m_base
[0];
2324 m_base
[3] = r
.m_base
[1];
2327 union_nonzero_bits (r
);
2331 // Set the new lower bound to R's lower bound.
2332 tree lb
= m_base
[0];
2333 m_base
[0] = r
.m_base
[0];
2335 // If R fully contains THIS range, just set the upper bound.
2336 if (wi::ge_p (wi::to_wide (r
.m_base
[1]), wi::to_wide (m_base
[1]), sign
))
2337 m_base
[1] = r
.m_base
[1];
2338 // Check for overlapping ranges, or target limited to a single range.
2339 else if (m_max_ranges
== 1
2340 || wi::to_widest (r
.m_base
[1]) + 1 >= wi::to_widest (lb
))
2344 // Left with 2 pairs.
2347 m_base
[3] = m_base
[1];
2348 m_base
[1] = r
.m_base
[1];
2350 union_nonzero_bits (r
);
2354 // union_ for multi-ranges.
2357 irange::irange_union (const irange
&r
)
2359 gcc_checking_assert (!legacy_mode_p () && !r
.legacy_mode_p ());
2361 if (r
.undefined_p ())
2377 set_varying (type ());
2381 // Special case one range union one range.
2382 if (m_num_ranges
== 1 && r
.m_num_ranges
== 1)
2383 return irange_single_pair_union (r
);
2385 // If this ranges fully contains R, then we need do nothing.
2386 if (irange_contains_p (r
))
2387 return union_nonzero_bits (r
);
2389 // Do not worry about merging and such by reserving twice as many
2390 // pairs as needed, and then simply sort the 2 ranges into this
2391 // intermediate form.
2393 // The intermediate result will have the property that the beginning
2394 // of each range is <= the beginning of the next range. There may
2395 // be overlapping ranges at this point. I.e. this would be valid
2396 // [-20, 10], [-10, 0], [0, 20], [40, 90] as it satisfies this
2397 // contraint : -20 < -10 < 0 < 40. When the range is rebuilt into r,
2398 // the merge is performed.
2400 // [Xi,Yi]..[Xn,Yn] U [Xj,Yj]..[Xm,Ym] --> [Xk,Yk]..[Xp,Yp]
2401 auto_vec
<tree
, 20> res (m_num_ranges
* 2 + r
.m_num_ranges
* 2);
2402 unsigned i
= 0, j
= 0, k
= 0;
2404 while (i
< m_num_ranges
* 2 && j
< r
.m_num_ranges
* 2)
2406 // lower of Xi and Xj is the lowest point.
2407 if (wi::to_widest (m_base
[i
]) <= wi::to_widest (r
.m_base
[j
]))
2409 res
.quick_push (m_base
[i
]);
2410 res
.quick_push (m_base
[i
+ 1]);
2416 res
.quick_push (r
.m_base
[j
]);
2417 res
.quick_push (r
.m_base
[j
+ 1]);
2422 for ( ; i
< m_num_ranges
* 2; i
+= 2)
2424 res
.quick_push (m_base
[i
]);
2425 res
.quick_push (m_base
[i
+ 1]);
2428 for ( ; j
< r
.m_num_ranges
* 2; j
+= 2)
2430 res
.quick_push (r
.m_base
[j
]);
2431 res
.quick_push (r
.m_base
[j
+ 1]);
2435 // Now normalize the vector removing any overlaps.
2437 for (j
= 2; j
< k
; j
+= 2)
2439 // Current upper+1 is >= lower bound next pair, then we merge ranges.
2440 if (wi::to_widest (res
[i
- 1]) + 1 >= wi::to_widest (res
[j
]))
2442 // New upper bounds is greater of current or the next one.
2443 if (wi::to_widest (res
[j
+ 1]) > wi::to_widest (res
[i
- 1]))
2444 res
[i
- 1] = res
[j
+ 1];
2448 // This is a new distinct range, but no point in copying it
2449 // if it is already in the right place.
2453 res
[i
++] = res
[j
+ 1];
2460 // At this point, the vector should have i ranges, none overlapping.
2461 // Now it simply needs to be copied, and if there are too many
2462 // ranges, merge some. We wont do any analysis as to what the
2463 // "best" merges are, simply combine the final ranges into one.
2464 if (i
> m_max_ranges
* 2)
2466 res
[m_max_ranges
* 2 - 1] = res
[i
- 1];
2467 i
= m_max_ranges
* 2;
2470 for (j
= 0; j
< i
; j
++)
2471 m_base
[j
] = res
[j
];
2472 m_num_ranges
= i
/ 2;
2475 union_nonzero_bits (r
);
2479 // Return TRUE if THIS fully contains R. No undefined or varying cases.
2482 irange::irange_contains_p (const irange
&r
) const
2484 gcc_checking_assert (!undefined_p () && !varying_p ());
2485 gcc_checking_assert (!r
.undefined_p () && !varying_p ());
2487 // In order for THIS to fully contain R, all of the pairs within R must
2488 // be fully contained by the pairs in this object.
2489 signop sign
= TYPE_SIGN (TREE_TYPE(m_base
[0]));
2492 tree rl
= r
.m_base
[0];
2493 tree ru
= r
.m_base
[1];
2498 // If r is contained within this range, move to the next R
2499 if (wi::ge_p (wi::to_wide (rl
), wi::to_wide (l
), sign
)
2500 && wi::le_p (wi::to_wide (ru
), wi::to_wide (u
), sign
))
2502 // This pair is OK, Either done, or bump to the next.
2503 if (++ri
>= r
.num_pairs ())
2505 rl
= r
.m_base
[ri
* 2];
2506 ru
= r
.m_base
[ri
* 2 + 1];
2509 // Otherwise, check if this's pair occurs before R's.
2510 if (wi::lt_p (wi::to_wide (u
), wi::to_wide (rl
), sign
))
2512 // There's still at least one pair of R left.
2513 if (++i
>= num_pairs ())
2516 u
= m_base
[i
* 2 + 1];
2525 // Intersect for multi-ranges. Return TRUE if anything changes.
2528 irange::irange_intersect (const irange
&r
)
2530 gcc_checking_assert (!legacy_mode_p () && !r
.legacy_mode_p ());
2531 gcc_checking_assert (undefined_p () || r
.undefined_p ()
2532 || range_compatible_p (type (), r
.type ()));
2536 if (r
.undefined_p ())
2549 if (r
.num_pairs () == 1)
2551 bool res
= intersect (r
.lower_bound (), r
.upper_bound ());
2555 res
|= intersect_nonzero_bits (r
);
2559 // If R fully contains this, then intersection will change nothing.
2560 if (r
.irange_contains_p (*this))
2561 return intersect_nonzero_bits (r
);
2563 signop sign
= TYPE_SIGN (TREE_TYPE(m_base
[0]));
2564 unsigned bld_pair
= 0;
2565 unsigned bld_lim
= m_max_ranges
;
2566 int_range_max
r2 (*this);
2567 unsigned r2_lim
= r2
.num_pairs ();
2569 for (unsigned i
= 0; i
< r
.num_pairs (); )
2571 // If r1's upper is < r2's lower, we can skip r1's pair.
2572 tree ru
= r
.m_base
[i
* 2 + 1];
2573 tree r2l
= r2
.m_base
[i2
* 2];
2574 if (wi::lt_p (wi::to_wide (ru
), wi::to_wide (r2l
), sign
))
2579 // Likewise, skip r2's pair if its excluded.
2580 tree r2u
= r2
.m_base
[i2
* 2 + 1];
2581 tree rl
= r
.m_base
[i
* 2];
2582 if (wi::lt_p (wi::to_wide (r2u
), wi::to_wide (rl
), sign
))
2587 // No more r2, break.
2591 // Must be some overlap. Find the highest of the lower bounds,
2592 // and set it, unless the build limits lower bounds is already
2594 if (bld_pair
< bld_lim
)
2596 if (wi::ge_p (wi::to_wide (rl
), wi::to_wide (r2l
), sign
))
2597 m_base
[bld_pair
* 2] = rl
;
2599 m_base
[bld_pair
* 2] = r2l
;
2602 // Decrease and set a new upper.
2605 // ...and choose the lower of the upper bounds.
2606 if (wi::le_p (wi::to_wide (ru
), wi::to_wide (r2u
), sign
))
2608 m_base
[bld_pair
* 2 + 1] = ru
;
2610 // Move past the r1 pair and keep trying.
2616 m_base
[bld_pair
* 2 + 1] = r2u
;
2621 // No more r2, break.
2624 // r2 has the higher lower bound.
2627 // At the exit of this loop, it is one of 2 things:
2628 // ran out of r1, or r2, but either means we are done.
2629 m_num_ranges
= bld_pair
;
2630 if (m_num_ranges
== 0)
2637 intersect_nonzero_bits (r
);
2642 // Multirange intersect for a specified wide_int [lb, ub] range.
2643 // Return TRUE if intersect changed anything.
2645 // NOTE: It is the caller's responsibility to intersect the nonzero masks.
2648 irange::intersect (const wide_int
& lb
, const wide_int
& ub
)
2650 // Undefined remains undefined.
2654 if (legacy_mode_p ())
2656 intersect (int_range
<1> (type (), lb
, ub
));
2660 tree range_type
= type();
2661 signop sign
= TYPE_SIGN (range_type
);
2663 gcc_checking_assert (TYPE_PRECISION (range_type
) == wi::get_precision (lb
));
2664 gcc_checking_assert (TYPE_PRECISION (range_type
) == wi::get_precision (ub
));
2666 // If this range is fuly contained, then intersection will do nothing.
2667 if (wi::ge_p (lower_bound (), lb
, sign
)
2668 && wi::le_p (upper_bound (), ub
, sign
))
2671 unsigned bld_index
= 0;
2672 unsigned pair_lim
= num_pairs ();
2673 for (unsigned i
= 0; i
< pair_lim
; i
++)
2675 tree pairl
= m_base
[i
* 2];
2676 tree pairu
= m_base
[i
* 2 + 1];
2677 // Once UB is less than a pairs lower bound, we're done.
2678 if (wi::lt_p (ub
, wi::to_wide (pairl
), sign
))
2680 // if LB is greater than this pairs upper, this pair is excluded.
2681 if (wi::lt_p (wi::to_wide (pairu
), lb
, sign
))
2684 // Must be some overlap. Find the highest of the lower bounds,
2686 if (wi::gt_p (lb
, wi::to_wide (pairl
), sign
))
2687 m_base
[bld_index
* 2] = wide_int_to_tree (range_type
, lb
);
2689 m_base
[bld_index
* 2] = pairl
;
2691 // ...and choose the lower of the upper bounds and if the base pair
2692 // has the lower upper bound, need to check next pair too.
2693 if (wi::lt_p (ub
, wi::to_wide (pairu
), sign
))
2695 m_base
[bld_index
++ * 2 + 1] = wide_int_to_tree (range_type
, ub
);
2699 m_base
[bld_index
++ * 2 + 1] = pairu
;
2702 m_num_ranges
= bld_index
;
2703 if (m_num_ranges
== 0)
2710 // No need to call normalize_kind(), as the caller will do this
2711 // while intersecting the nonzero mask.
2718 // Signed 1-bits are strange. You can't subtract 1, because you can't
2719 // represent the number 1. This works around that for the invert routine.
2721 static wide_int
inline
2722 subtract_one (const wide_int
&x
, tree type
, wi::overflow_type
&overflow
)
2724 if (TYPE_SIGN (type
) == SIGNED
)
2725 return wi::add (x
, -1, SIGNED
, &overflow
);
2727 return wi::sub (x
, 1, UNSIGNED
, &overflow
);
2730 // The analogous function for adding 1.
2732 static wide_int
inline
2733 add_one (const wide_int
&x
, tree type
, wi::overflow_type
&overflow
)
2735 if (TYPE_SIGN (type
) == SIGNED
)
2736 return wi::sub (x
, -1, SIGNED
, &overflow
);
2738 return wi::add (x
, 1, UNSIGNED
, &overflow
);
2741 // Return the inverse of a range.
2746 if (legacy_mode_p ())
2748 // We can't just invert VR_RANGE and VR_ANTI_RANGE because we may
2749 // create non-canonical ranges. Use the constructors instead.
2750 if (m_kind
== VR_RANGE
)
2751 *this = value_range (min (), max (), VR_ANTI_RANGE
);
2752 else if (m_kind
== VR_ANTI_RANGE
)
2753 *this = value_range (min (), max ());
2759 gcc_checking_assert (!undefined_p () && !varying_p ());
2761 // We always need one more set of bounds to represent an inverse, so
2762 // if we're at the limit, we can't properly represent things.
2764 // For instance, to represent the inverse of a 2 sub-range set
2765 // [5, 10][20, 30], we would need a 3 sub-range set
2766 // [-MIN, 4][11, 19][31, MAX].
2768 // In this case, return the most conservative thing.
2770 // However, if any of the extremes of the range are -MIN/+MAX, we
2771 // know we will not need an extra bound. For example:
2773 // INVERT([-MIN,20][30,40]) => [21,29][41,+MAX]
2774 // INVERT([-MIN,20][30,MAX]) => [21,29]
2775 tree ttype
= type ();
2776 unsigned prec
= TYPE_PRECISION (ttype
);
2777 signop sign
= TYPE_SIGN (ttype
);
2778 wide_int type_min
= wi::min_value (prec
, sign
);
2779 wide_int type_max
= wi::max_value (prec
, sign
);
2780 m_nonzero_mask
= NULL
;
2781 if (m_num_ranges
== m_max_ranges
2782 && lower_bound () != type_min
2783 && upper_bound () != type_max
)
2785 m_base
[1] = wide_int_to_tree (ttype
, type_max
);
2789 // The algorithm is as follows. To calculate INVERT ([a,b][c,d]), we
2790 // generate [-MIN, a-1][b+1, c-1][d+1, MAX].
2792 // If there is an over/underflow in the calculation for any
2793 // sub-range, we eliminate that subrange. This allows us to easily
2794 // calculate INVERT([-MIN, 5]) with: [-MIN, -MIN-1][6, MAX]. And since
2795 // we eliminate the underflow, only [6, MAX] remains.
2797 wi::overflow_type ovf
;
2798 // Construct leftmost range.
2799 int_range_max
orig_range (*this);
2800 unsigned nitems
= 0;
2802 // If this is going to underflow on the MINUS 1, don't even bother
2803 // checking. This also handles subtracting one from an unsigned 0,
2804 // which doesn't set the underflow bit.
2805 if (type_min
!= orig_range
.lower_bound ())
2807 m_base
[nitems
++] = wide_int_to_tree (ttype
, type_min
);
2808 tmp
= subtract_one (orig_range
.lower_bound (), ttype
, ovf
);
2809 m_base
[nitems
++] = wide_int_to_tree (ttype
, tmp
);
2814 // Construct middle ranges if applicable.
2815 if (orig_range
.num_pairs () > 1)
2818 for (; j
< (orig_range
.num_pairs () * 2) - 1; j
+= 2)
2820 // The middle ranges cannot have MAX/MIN, so there's no need
2821 // to check for unsigned overflow on the +1 and -1 here.
2822 tmp
= wi::add (wi::to_wide (orig_range
.m_base
[j
]), 1, sign
, &ovf
);
2823 m_base
[nitems
++] = wide_int_to_tree (ttype
, tmp
);
2824 tmp
= subtract_one (wi::to_wide (orig_range
.m_base
[j
+ 1]),
2826 m_base
[nitems
++] = wide_int_to_tree (ttype
, tmp
);
2832 // Construct rightmost range.
2834 // However, if this will overflow on the PLUS 1, don't even bother.
2835 // This also handles adding one to an unsigned MAX, which doesn't
2836 // set the overflow bit.
2837 if (type_max
!= wi::to_wide (orig_range
.m_base
[i
]))
2839 tmp
= add_one (wi::to_wide (orig_range
.m_base
[i
]), ttype
, ovf
);
2840 m_base
[nitems
++] = wide_int_to_tree (ttype
, tmp
);
2841 m_base
[nitems
++] = wide_int_to_tree (ttype
, type_max
);
2845 m_num_ranges
= nitems
/ 2;
2847 // We disallow undefined or varying coming in, so the result can
2848 // only be a VR_RANGE.
2849 gcc_checking_assert (m_kind
== VR_RANGE
);
2855 // Return the nonzero bits inherent in the range.
2858 irange::get_nonzero_bits_from_range () const
2860 // For legacy symbolics.
2862 return wi::shwi (-1, TYPE_PRECISION (type ()));
2864 wide_int min
= lower_bound ();
2865 wide_int max
= upper_bound ();
2866 wide_int xorv
= min
^ max
;
2869 unsigned prec
= TYPE_PRECISION (type ());
2870 xorv
= wi::mask (prec
- wi::clz (xorv
), false, prec
);
2875 // If the the nonzero mask can be trivially converted to a range, do
2876 // so and return TRUE.
2879 irange::set_range_from_nonzero_bits ()
2881 gcc_checking_assert (!undefined_p ());
2882 if (!m_nonzero_mask
)
2884 unsigned popcount
= wi::popcount (wi::to_wide (m_nonzero_mask
));
2886 // If we have only one bit set in the mask, we can figure out the
2887 // range immediately.
2890 // Make sure we don't pessimize the range.
2891 if (!contains_p (m_nonzero_mask
))
2894 bool has_zero
= contains_p (build_zero_cst (type ()));
2895 tree nz
= m_nonzero_mask
;
2897 m_nonzero_mask
= nz
;
2901 zero
.set_zero (type ());
2910 irange::set_nonzero_bits (const wide_int_ref
&bits
)
2912 gcc_checking_assert (!undefined_p ());
2913 unsigned prec
= TYPE_PRECISION (type ());
2917 m_nonzero_mask
= NULL
;
2924 // Drop VARYINGs with a nonzero mask to a plain range.
2925 if (m_kind
== VR_VARYING
&& bits
!= -1)
2928 wide_int nz
= wide_int::from (bits
, prec
, TYPE_SIGN (type ()));
2929 m_nonzero_mask
= wide_int_to_tree (type (), nz
);
2930 if (set_range_from_nonzero_bits ())
2938 // Return the nonzero bitmask. This will return the nonzero bits plus
2939 // the nonzero bits inherent in the range.
2942 irange::get_nonzero_bits () const
2944 gcc_checking_assert (!undefined_p ());
2945 // The nonzero mask inherent in the range is calculated on-demand.
2946 // For example, [0,255] does not have a 0xff nonzero mask by default
2947 // (unless manually set). This saves us considerable time, because
2948 // setting it at creation incurs a large penalty for irange::set.
2949 // At the time of writing there was a 5% slowdown in VRP if we kept
2950 // the mask precisely up to date at all times. Instead, we default
2951 // to -1 and set it when explicitly requested. However, this
2952 // function will always return the correct mask.
2954 return wi::to_wide (m_nonzero_mask
) & get_nonzero_bits_from_range ();
2956 return get_nonzero_bits_from_range ();
2959 // Convert tree mask to wide_int. Returns -1 for NULL masks.
2962 mask_to_wi (tree mask
, tree type
)
2965 return wi::to_wide (mask
);
2967 return wi::shwi (-1, TYPE_PRECISION (type
));
2970 // Intersect the nonzero bits in R into THIS and normalize the range.
2971 // Return TRUE if the intersection changed anything.
2974 irange::intersect_nonzero_bits (const irange
&r
)
2976 gcc_checking_assert (!undefined_p () && !r
.undefined_p ());
2978 if (!m_nonzero_mask
&& !r
.m_nonzero_mask
)
2986 bool changed
= false;
2988 if (mask_to_wi (m_nonzero_mask
, t
) != mask_to_wi (r
.m_nonzero_mask
, t
))
2990 wide_int nz
= get_nonzero_bits () & r
.get_nonzero_bits ();
2991 m_nonzero_mask
= wide_int_to_tree (t
, nz
);
2992 if (set_range_from_nonzero_bits ())
3002 // Union the nonzero bits in R into THIS and normalize the range.
3003 // Return TRUE if the union changed anything.
3006 irange::union_nonzero_bits (const irange
&r
)
3008 gcc_checking_assert (!undefined_p () && !r
.undefined_p ());
3010 if (!m_nonzero_mask
&& !r
.m_nonzero_mask
)
3018 bool changed
= false;
3020 if (mask_to_wi (m_nonzero_mask
, t
) != mask_to_wi (r
.m_nonzero_mask
, t
))
3022 wide_int nz
= get_nonzero_bits () | r
.get_nonzero_bits ();
3023 m_nonzero_mask
= wide_int_to_tree (t
, nz
);
3024 // No need to call set_range_from_nonzero_bits, because we'll
3025 // never narrow the range. Besides, it would cause endless
3026 // recursion because of the union_ in
3027 // set_range_from_nonzero_bits.
3037 dump_value_range (FILE *file
, const vrange
*vr
)
3043 debug (const vrange
*vr
)
3045 dump_value_range (stderr
, vr
);
3046 fprintf (stderr
, "\n");
3050 debug (const vrange
&vr
)
3056 debug (const value_range
*vr
)
3058 dump_value_range (stderr
, vr
);
3059 fprintf (stderr
, "\n");
3063 debug (const value_range
&vr
)
3065 dump_value_range (stderr
, &vr
);
3066 fprintf (stderr
, "\n");
3069 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
3070 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
3071 false otherwise. If *AR can be represented with a single range
3072 *VR1 will be VR_UNDEFINED. */
3075 ranges_from_anti_range (const value_range
*ar
,
3076 value_range
*vr0
, value_range
*vr1
)
3078 tree type
= ar
->type ();
3080 vr0
->set_undefined ();
3081 vr1
->set_undefined ();
3083 /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U
3084 [A+1, +INF]. Not sure if this helps in practice, though. */
3086 if (ar
->kind () != VR_ANTI_RANGE
3087 || TREE_CODE (ar
->min ()) != INTEGER_CST
3088 || TREE_CODE (ar
->max ()) != INTEGER_CST
3089 || !vrp_val_min (type
)
3090 || !vrp_val_max (type
))
3093 if (tree_int_cst_lt (vrp_val_min (type
), ar
->min ()))
3094 vr0
->set (vrp_val_min (type
),
3095 wide_int_to_tree (type
, wi::to_wide (ar
->min ()) - 1));
3096 if (tree_int_cst_lt (ar
->max (), vrp_val_max (type
)))
3097 vr1
->set (wide_int_to_tree (type
, wi::to_wide (ar
->max ()) + 1),
3098 vrp_val_max (type
));
3099 if (vr0
->undefined_p ())
3102 vr1
->set_undefined ();
3105 return !vr0
->undefined_p ();
3109 range_has_numeric_bounds_p (const irange
*vr
)
3111 return (!vr
->undefined_p ()
3112 && TREE_CODE (vr
->min ()) == INTEGER_CST
3113 && TREE_CODE (vr
->max ()) == INTEGER_CST
);
3116 /* Return whether VAL is equal to the maximum value of its type.
3117 We can't do a simple equality comparison with TYPE_MAX_VALUE because
3118 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
3119 is not == to the integer constant with the same value in the type. */
3122 vrp_val_is_max (const_tree val
)
3124 tree type_max
= vrp_val_max (TREE_TYPE (val
));
3125 return (val
== type_max
3126 || (type_max
!= NULL_TREE
3127 && operand_equal_p (val
, type_max
, 0)));
3130 /* Return whether VAL is equal to the minimum value of its type. */
3133 vrp_val_is_min (const_tree val
)
3135 tree type_min
= vrp_val_min (TREE_TYPE (val
));
3136 return (val
== type_min
3137 || (type_min
!= NULL_TREE
3138 && operand_equal_p (val
, type_min
, 0)));
3141 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
3144 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
3148 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
3153 // ?? These stubs are for ipa-prop.cc which use a value_range in a
3154 // hash_traits. hash-traits.h defines an extern of gt_ggc_mx (T &)
3155 // instead of picking up the gt_ggc_mx (T *) version.
3157 gt_pch_nx (int_range
<1> *&x
)
3159 return gt_pch_nx ((irange
*) x
);
3163 gt_ggc_mx (int_range
<1> *&x
)
3165 return gt_ggc_mx ((irange
*) x
);
3168 #define DEFINE_INT_RANGE_INSTANCE(N) \
3169 template int_range<N>::int_range(tree, tree, value_range_kind); \
3170 template int_range<N>::int_range(tree_node *, \
3173 value_range_kind); \
3174 template int_range<N>::int_range(tree); \
3175 template int_range<N>::int_range(const irange &); \
3176 template int_range<N>::int_range(const int_range &); \
3177 template int_range<N>& int_range<N>::operator= (const int_range &);
3179 DEFINE_INT_RANGE_INSTANCE(1)
3180 DEFINE_INT_RANGE_INSTANCE(2)
3181 DEFINE_INT_RANGE_INSTANCE(3)
3182 DEFINE_INT_RANGE_INSTANCE(255)
3185 #include "selftest.h"
3189 #define INT(N) build_int_cst (integer_type_node, (N))
3190 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
3191 #define UINT128(N) build_int_cstu (u128_type, (N))
3192 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
3193 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
3196 build_range3 (int a
, int b
, int c
, int d
, int e
, int f
)
3198 int_range
<3> i1 (INT (a
), INT (b
));
3199 int_range
<3> i2 (INT (c
), INT (d
));
3200 int_range
<3> i3 (INT (e
), INT (f
));
3207 range_tests_irange3 ()
3209 typedef int_range
<3> int_range3
;
3210 int_range3 r0
, r1
, r2
;
3211 int_range3 i1
, i2
, i3
;
3213 // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20].
3214 r0
= int_range3 (INT (10), INT (20));
3215 r1
= int_range3 (INT (5), INT (8));
3217 r1
= int_range3 (INT (1), INT (3));
3219 ASSERT_TRUE (r0
== build_range3 (1, 3, 5, 8, 10, 20));
3221 // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20].
3222 r1
= int_range3 (INT (-5), INT (0));
3224 ASSERT_TRUE (r0
== build_range3 (-5, 3, 5, 8, 10, 20));
3226 // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60].
3227 r1
= int_range3 (INT (50), INT (60));
3228 r0
= int_range3 (INT (10), INT (20));
3229 r0
.union_ (int_range3 (INT (30), INT (40)));
3231 ASSERT_TRUE (r0
== build_range3 (10, 20, 30, 40, 50, 60));
3232 // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80].
3233 r1
= int_range3 (INT (70), INT (80));
3236 r2
= build_range3 (10, 20, 30, 40, 50, 60);
3237 r2
.union_ (int_range3 (INT (70), INT (80)));
3238 ASSERT_TRUE (r0
== r2
);
3240 // [10,20][30,40][50,60] U [6,35] => [6,40][50,60].
3241 r0
= build_range3 (10, 20, 30, 40, 50, 60);
3242 r1
= int_range3 (INT (6), INT (35));
3244 r1
= int_range3 (INT (6), INT (40));
3245 r1
.union_ (int_range3 (INT (50), INT (60)));
3246 ASSERT_TRUE (r0
== r1
);
3248 // [10,20][30,40][50,60] U [6,60] => [6,60].
3249 r0
= build_range3 (10, 20, 30, 40, 50, 60);
3250 r1
= int_range3 (INT (6), INT (60));
3252 ASSERT_TRUE (r0
== int_range3 (INT (6), INT (60)));
3254 // [10,20][30,40][50,60] U [6,70] => [6,70].
3255 r0
= build_range3 (10, 20, 30, 40, 50, 60);
3256 r1
= int_range3 (INT (6), INT (70));
3258 ASSERT_TRUE (r0
== int_range3 (INT (6), INT (70)));
3260 // [10,20][30,40][50,60] U [35,70] => [10,20][30,70].
3261 r0
= build_range3 (10, 20, 30, 40, 50, 60);
3262 r1
= int_range3 (INT (35), INT (70));
3264 r1
= int_range3 (INT (10), INT (20));
3265 r1
.union_ (int_range3 (INT (30), INT (70)));
3266 ASSERT_TRUE (r0
== r1
);
3268 // [10,20][30,40][50,60] U [15,35] => [10,40][50,60].
3269 r0
= build_range3 (10, 20, 30, 40, 50, 60);
3270 r1
= int_range3 (INT (15), INT (35));
3272 r1
= int_range3 (INT (10), INT (40));
3273 r1
.union_ (int_range3 (INT (50), INT (60)));
3274 ASSERT_TRUE (r0
== r1
);
3276 // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60].
3277 r0
= build_range3 (10, 20, 30, 40, 50, 60);
3278 r1
= int_range3 (INT (35), INT (35));
3280 ASSERT_TRUE (r0
== build_range3 (10, 20, 30, 40, 50, 60));
3284 range_tests_int_range_max ()
3287 unsigned int nrange
;
3289 // Build a huge multi-range range.
3290 for (nrange
= 0; nrange
< 50; ++nrange
)
3292 int_range
<1> tmp (INT (nrange
*10), INT (nrange
*10 + 5));
3295 ASSERT_TRUE (big
.num_pairs () == nrange
);
3297 // Verify that we can copy it without loosing precision.
3298 int_range_max
copy (big
);
3299 ASSERT_TRUE (copy
.num_pairs () == nrange
);
3301 // Inverting it should produce one more sub-range.
3303 ASSERT_TRUE (big
.num_pairs () == nrange
+ 1);
3305 int_range
<1> tmp (INT (5), INT (37));
3306 big
.intersect (tmp
);
3307 ASSERT_TRUE (big
.num_pairs () == 4);
3309 // Test that [10,10][20,20] does NOT contain 15.
3311 int_range_max
i1 (build_int_cst (integer_type_node
, 10),
3312 build_int_cst (integer_type_node
, 10));
3313 int_range_max
i2 (build_int_cst (integer_type_node
, 20),
3314 build_int_cst (integer_type_node
, 20));
3316 ASSERT_FALSE (i1
.contains_p (build_int_cst (integer_type_node
, 15)));
3321 range_tests_legacy ()
3323 // Test truncating copy to int_range<1>.
3324 int_range
<3> big
= build_range3 (10, 20, 30, 40, 50, 60);
3325 int_range
<1> small
= big
;
3326 ASSERT_TRUE (small
== int_range
<1> (INT (10), INT (60)));
3328 // Test truncating copy to int_range<2>.
3329 int_range
<2> medium
= big
;
3330 ASSERT_TRUE (!medium
.undefined_p ());
3332 // Test that a truncating copy of [MIN,20][22,40][80,MAX]
3333 // ends up as a conservative anti-range of ~[21,21].
3334 big
= int_range
<3> (vrp_val_min (integer_type_node
), INT (20));
3335 big
.union_ (int_range
<1> (INT (22), INT (40)));
3336 big
.union_ (int_range
<1> (INT (80), vrp_val_max (integer_type_node
)));
3338 ASSERT_TRUE (small
== int_range
<1> (INT (21), INT (21), VR_ANTI_RANGE
));
3340 // Copying a legacy symbolic to an int_range should normalize the
3341 // symbolic at copy time.
3343 tree ssa
= make_ssa_name (integer_type_node
);
3344 value_range
legacy_range (ssa
, INT (25));
3345 int_range
<2> copy
= legacy_range
;
3346 ASSERT_TRUE (copy
== int_range
<2> (vrp_val_min (integer_type_node
),
3349 // Test that copying ~[abc_23, abc_23] to a multi-range yields varying.
3350 legacy_range
= value_range (ssa
, ssa
, VR_ANTI_RANGE
);
3351 copy
= legacy_range
;
3352 ASSERT_TRUE (copy
.varying_p ());
3355 // VARYING of different sizes should not be equal.
3356 tree big_type
= build_nonstandard_integer_type (32, 1);
3357 tree small_type
= build_nonstandard_integer_type (16, 1);
3358 int_range_max
r0 (big_type
);
3359 int_range_max
r1 (small_type
);
3360 ASSERT_TRUE (r0
!= r1
);
3361 value_range
vr0 (big_type
);
3362 int_range_max
vr1 (small_type
);
3363 ASSERT_TRUE (vr0
!= vr1
);
3366 // Simulate -fstrict-enums where the domain of a type is less than the
3370 range_tests_strict_enum ()
3372 // The enum can only hold [0, 3].
3373 tree rtype
= copy_node (unsigned_type_node
);
3374 TYPE_MIN_VALUE (rtype
) = build_int_cstu (rtype
, 0);
3375 TYPE_MAX_VALUE (rtype
) = build_int_cstu (rtype
, 3);
3377 // Test that even though vr1 covers the strict enum domain ([0, 3]),
3378 // it does not cover the domain of the underlying type.
3379 int_range
<1> vr1 (build_int_cstu (rtype
, 0), build_int_cstu (rtype
, 1));
3380 int_range
<1> vr2 (build_int_cstu (rtype
, 2), build_int_cstu (rtype
, 3));
3382 ASSERT_TRUE (vr1
== int_range
<1> (build_int_cstu (rtype
, 0),
3383 build_int_cstu (rtype
, 3)));
3384 ASSERT_FALSE (vr1
.varying_p ());
3386 // Test that copying to a multi-range does not change things.
3387 int_range
<2> ir1 (vr1
);
3388 ASSERT_TRUE (ir1
== vr1
);
3389 ASSERT_FALSE (ir1
.varying_p ());
3391 // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3].
3392 vr1
= int_range
<1> (TYPE_MIN_VALUE (rtype
), TYPE_MAX_VALUE (rtype
));
3394 ASSERT_TRUE (ir1
== vr1
);
3395 ASSERT_FALSE (ir1
.varying_p ());
3401 tree u128_type
= build_nonstandard_integer_type (128, /*unsigned=*/1);
3402 int_range
<1> i1
, i2
, i3
;
3403 int_range
<1> r0
, r1
, rold
;
3405 // Test 1-bit signed integer union.
3406 // [-1,-1] U [0,0] = VARYING.
3407 tree one_bit_type
= build_nonstandard_integer_type (1, 0);
3408 tree one_bit_min
= vrp_val_min (one_bit_type
);
3409 tree one_bit_max
= vrp_val_max (one_bit_type
);
3411 int_range
<2> min (one_bit_min
, one_bit_min
);
3412 int_range
<2> max (one_bit_max
, one_bit_max
);
3414 ASSERT_TRUE (max
.varying_p ());
3417 // Test inversion of 1-bit signed integers.
3419 int_range
<2> min (one_bit_min
, one_bit_min
);
3420 int_range
<2> max (one_bit_max
, one_bit_max
);
3424 ASSERT_TRUE (t
== max
);
3427 ASSERT_TRUE (t
== min
);
3430 // Test that NOT(255) is [0..254] in 8-bit land.
3431 int_range
<1> not_255 (UCHAR (255), UCHAR (255), VR_ANTI_RANGE
);
3432 ASSERT_TRUE (not_255
== int_range
<1> (UCHAR (0), UCHAR (254)));
3434 // Test that NOT(0) is [1..255] in 8-bit land.
3435 int_range
<1> not_zero
= range_nonzero (unsigned_char_type_node
);
3436 ASSERT_TRUE (not_zero
== int_range
<1> (UCHAR (1), UCHAR (255)));
3438 // Check that [0,127][0x..ffffff80,0x..ffffff]
3439 // => ~[128, 0x..ffffff7f].
3440 r0
= int_range
<1> (UINT128 (0), UINT128 (127));
3441 tree high
= build_minus_one_cst (u128_type
);
3442 // low = -1 - 127 => 0x..ffffff80.
3443 tree low
= fold_build2 (MINUS_EXPR
, u128_type
, high
, UINT128(127));
3444 r1
= int_range
<1> (low
, high
); // [0x..ffffff80, 0x..ffffffff]
3445 // r0 = [0,127][0x..ffffff80,0x..fffffff].
3447 // r1 = [128, 0x..ffffff7f].
3448 r1
= int_range
<1> (UINT128(128),
3449 fold_build2 (MINUS_EXPR
, u128_type
,
3450 build_minus_one_cst (u128_type
),
3453 ASSERT_TRUE (r0
== r1
);
3455 r0
.set_varying (integer_type_node
);
3456 tree minint
= wide_int_to_tree (integer_type_node
, r0
.lower_bound ());
3457 tree maxint
= wide_int_to_tree (integer_type_node
, r0
.upper_bound ());
3459 r0
.set_varying (short_integer_type_node
);
3461 r0
.set_varying (unsigned_type_node
);
3462 tree maxuint
= wide_int_to_tree (unsigned_type_node
, r0
.upper_bound ());
3464 // Check that ~[0,5] => [6,MAX] for unsigned int.
3465 r0
= int_range
<1> (UINT (0), UINT (5));
3467 ASSERT_TRUE (r0
== int_range
<1> (UINT(6), maxuint
));
3469 // Check that ~[10,MAX] => [0,9] for unsigned int.
3470 r0
= int_range
<1> (UINT(10), maxuint
);
3472 ASSERT_TRUE (r0
== int_range
<1> (UINT (0), UINT (9)));
3474 // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers.
3475 r0
= int_range
<1> (UINT128 (0), UINT128 (5), VR_ANTI_RANGE
);
3476 r1
= int_range
<1> (UINT128(6), build_minus_one_cst (u128_type
));
3477 ASSERT_TRUE (r0
== r1
);
3479 // Check that [~5] is really [-MIN,4][6,MAX].
3480 r0
= int_range
<1> (INT (5), INT (5), VR_ANTI_RANGE
);
3481 r1
= int_range
<1> (minint
, INT (4));
3482 r1
.union_ (int_range
<1> (INT (6), maxint
));
3483 ASSERT_FALSE (r1
.undefined_p ());
3484 ASSERT_TRUE (r0
== r1
);
3486 r1
= int_range
<1> (INT (5), INT (5));
3487 int_range
<1> r2 (r1
);
3488 ASSERT_TRUE (r1
== r2
);
3490 r1
= int_range
<1> (INT (5), INT (10));
3492 r1
= int_range
<1> (integer_type_node
,
3493 wi::to_wide (INT (5)), wi::to_wide (INT (10)));
3494 ASSERT_TRUE (r1
.contains_p (INT (7)));
3496 r1
= int_range
<1> (SCHAR (0), SCHAR (20));
3497 ASSERT_TRUE (r1
.contains_p (SCHAR(15)));
3498 ASSERT_FALSE (r1
.contains_p (SCHAR(300)));
3500 // NOT([10,20]) ==> [-MIN,9][21,MAX].
3501 r0
= r1
= int_range
<1> (INT (10), INT (20));
3502 r2
= int_range
<1> (minint
, INT(9));
3503 r2
.union_ (int_range
<1> (INT(21), maxint
));
3504 ASSERT_FALSE (r2
.undefined_p ());
3506 ASSERT_TRUE (r1
== r2
);
3507 // Test that NOT(NOT(x)) == x.
3509 ASSERT_TRUE (r0
== r2
);
3511 // Test that booleans and their inverse work as expected.
3512 r0
= range_zero (boolean_type_node
);
3513 ASSERT_TRUE (r0
== int_range
<1> (build_zero_cst (boolean_type_node
),
3514 build_zero_cst (boolean_type_node
)));
3516 ASSERT_TRUE (r0
== int_range
<1> (build_one_cst (boolean_type_node
),
3517 build_one_cst (boolean_type_node
)));
3519 // Make sure NULL and non-NULL of pointer types work, and that
3520 // inverses of them are consistent.
3521 tree voidp
= build_pointer_type (void_type_node
);
3522 r0
= range_zero (voidp
);
3526 ASSERT_TRUE (r0
== r1
);
3528 // [10,20] U [15, 30] => [10, 30].
3529 r0
= int_range
<1> (INT (10), INT (20));
3530 r1
= int_range
<1> (INT (15), INT (30));
3532 ASSERT_TRUE (r0
== int_range
<1> (INT (10), INT (30)));
3534 // [15,40] U [] => [15,40].
3535 r0
= int_range
<1> (INT (15), INT (40));
3536 r1
.set_undefined ();
3538 ASSERT_TRUE (r0
== int_range
<1> (INT (15), INT (40)));
3540 // [10,20] U [10,10] => [10,20].
3541 r0
= int_range
<1> (INT (10), INT (20));
3542 r1
= int_range
<1> (INT (10), INT (10));
3544 ASSERT_TRUE (r0
== int_range
<1> (INT (10), INT (20)));
3546 // [10,20] U [9,9] => [9,20].
3547 r0
= int_range
<1> (INT (10), INT (20));
3548 r1
= int_range
<1> (INT (9), INT (9));
3550 ASSERT_TRUE (r0
== int_range
<1> (INT (9), INT (20)));
3552 // [10,20] ^ [15,30] => [15,20].
3553 r0
= int_range
<1> (INT (10), INT (20));
3554 r1
= int_range
<1> (INT (15), INT (30));
3556 ASSERT_TRUE (r0
== int_range
<1> (INT (15), INT (20)));
3558 // Test the internal sanity of wide_int's wrt HWIs.
3559 ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node
),
3560 TYPE_SIGN (boolean_type_node
))
3561 == wi::uhwi (1, TYPE_PRECISION (boolean_type_node
)));
3564 r0
= int_range
<1> (INT (0), INT (0));
3565 ASSERT_TRUE (r0
.zero_p ());
3567 // Test nonzero_p().
3568 r0
= int_range
<1> (INT (0), INT (0));
3570 ASSERT_TRUE (r0
.nonzero_p ());
3572 // test legacy interaction
3574 r0
= int_range
<1> (UINT (1), UINT (1), VR_ANTI_RANGE
);
3576 r1
= int_range
<1> (UINT (3), UINT (3), VR_ANTI_RANGE
);
3578 // vv = [0,0][2,2][4, MAX]
3579 int_range
<3> vv
= r0
;
3582 ASSERT_TRUE (vv
.contains_p (UINT (2)));
3583 ASSERT_TRUE (vv
.num_pairs () == 3);
3585 // create r0 as legacy [1,1]
3586 r0
= int_range
<1> (UINT (1), UINT (1));
3587 // And union it with [0,0][2,2][4,MAX] multi range
3589 // The result should be [0,2][4,MAX], or ~[3,3] but it must contain 2
3590 ASSERT_TRUE (r0
.contains_p (UINT (2)));
3594 range_tests_nonzero_bits ()
3596 int_range
<2> r0
, r1
;
3598 // Adding nonzero bits to a varying drops the varying.
3599 r0
.set_varying (integer_type_node
);
3600 r0
.set_nonzero_bits (255);
3601 ASSERT_TRUE (!r0
.varying_p ());
3602 // Dropping the nonzero bits brings us back to varying.
3603 r0
.set_nonzero_bits (-1);
3604 ASSERT_TRUE (r0
.varying_p ());
3606 // Test contains_p with nonzero bits.
3607 r0
.set_zero (integer_type_node
);
3608 ASSERT_TRUE (r0
.contains_p (INT (0)));
3609 ASSERT_FALSE (r0
.contains_p (INT (1)));
3610 r0
.set_nonzero_bits (0xfe);
3611 ASSERT_FALSE (r0
.contains_p (INT (0x100)));
3612 ASSERT_FALSE (r0
.contains_p (INT (0x3)));
3614 // Union of nonzero bits.
3615 r0
.set_varying (integer_type_node
);
3616 r0
.set_nonzero_bits (0xf0);
3617 r1
.set_varying (integer_type_node
);
3618 r1
.set_nonzero_bits (0xf);
3620 ASSERT_TRUE (r0
.get_nonzero_bits () == 0xff);
3622 // Intersect of nonzero bits.
3623 r0
.set (INT (0), INT (255));
3624 r0
.set_nonzero_bits (0xfe);
3625 r1
.set_varying (integer_type_node
);
3626 r1
.set_nonzero_bits (0xf0);
3628 ASSERT_TRUE (r0
.get_nonzero_bits () == 0xf0);
3630 // Intersect where the mask of nonzero bits is implicit from the range.
3631 r0
.set_varying (integer_type_node
);
3632 r1
.set (INT (0), INT (255));
3634 ASSERT_TRUE (r0
.get_nonzero_bits () == 0xff);
3636 // The union of a mask of 0xff..ffff00 with a mask of 0xff spans the
3637 // entire domain, and makes the range a varying.
3638 r0
.set_varying (integer_type_node
);
3639 wide_int x
= wi::shwi (0xff, TYPE_PRECISION (integer_type_node
));
3640 x
= wi::bit_not (x
);
3641 r0
.set_nonzero_bits (x
); // 0xff..ff00
3642 r1
.set_varying (integer_type_node
);
3643 r1
.set_nonzero_bits (0xff);
3645 ASSERT_TRUE (r0
.varying_p ());
3647 // Test that setting a nonzero bit of 1 does not pessimize the range.
3648 r0
.set_zero (integer_type_node
);
3649 r0
.set_nonzero_bits (1);
3650 ASSERT_TRUE (r0
.zero_p ());
3653 // Build an frange from string endpoints.
3655 static inline frange
3656 frange_float (const char *lb
, const char *ub
, tree type
= float_type_node
)
3658 REAL_VALUE_TYPE min
, max
;
3659 gcc_assert (real_from_string (&min
, lb
) == 0);
3660 gcc_assert (real_from_string (&max
, ub
) == 0);
3661 return frange (type
, min
, max
);
3668 REAL_VALUE_TYPE q
, r
;
3671 // Equal ranges but with differing NAN bits are not equal.
3672 if (HONOR_NANS (float_type_node
))
3674 r1
= frange_float ("10", "12");
3682 // [10, 20] NAN ^ [30, 40] NAN = NAN.
3683 r0
= frange_float ("10", "20");
3684 r1
= frange_float ("30", "40");
3686 ASSERT_TRUE (r0
.known_isnan ());
3688 // [3,5] U [5,10] NAN = ... NAN
3689 r0
= frange_float ("3", "5");
3691 r1
= frange_float ("5", "10");
3693 ASSERT_TRUE (r0
.maybe_isnan ());
3696 // NAN ranges are not equal to each other.
3697 r0
.set_nan (float_type_node
);
3699 ASSERT_FALSE (r0
== r1
);
3700 ASSERT_FALSE (r0
== r0
);
3701 ASSERT_TRUE (r0
!= r0
);
3703 // [5,6] U NAN = [5,6] NAN.
3704 r0
= frange_float ("5", "6");
3706 r1
.set_nan (float_type_node
);
3708 real_from_string (&q
, "5");
3709 real_from_string (&r
, "6");
3710 ASSERT_TRUE (real_identical (&q
, &r0
.lower_bound ()));
3711 ASSERT_TRUE (real_identical (&r
, &r0
.upper_bound ()));
3712 ASSERT_TRUE (r0
.maybe_isnan ());
3715 r0
.set_nan (float_type_node
);
3716 r1
.set_nan (float_type_node
);
3718 ASSERT_TRUE (r0
.known_isnan ());
3720 // [INF, INF] NAN ^ NAN = NAN
3721 r0
.set_nan (float_type_node
);
3722 r1
= frange_float ("+Inf", "+Inf");
3723 if (!HONOR_NANS (float_type_node
))
3726 ASSERT_TRUE (r0
.known_isnan ());
3729 r0
.set_nan (float_type_node
);
3730 r1
.set_nan (float_type_node
);
3732 ASSERT_TRUE (r0
.known_isnan ());
3734 // +NAN ^ -NAN = UNDEFINED
3735 r0
.set_nan (float_type_node
, false);
3736 r1
.set_nan (float_type_node
, true);
3738 ASSERT_TRUE (r0
.undefined_p ());
3740 // VARYING ^ NAN = NAN.
3741 r0
.set_nan (float_type_node
);
3742 r1
.set_varying (float_type_node
);
3744 ASSERT_TRUE (r0
.known_isnan ());
3746 // [3,4] ^ NAN = UNDEFINED.
3747 r0
= frange_float ("3", "4");
3749 r1
.set_nan (float_type_node
);
3751 ASSERT_TRUE (r0
.undefined_p ());
3753 // [-3, 5] ^ NAN = UNDEFINED
3754 r0
= frange_float ("-3", "5");
3756 r1
.set_nan (float_type_node
);
3758 ASSERT_TRUE (r0
.undefined_p ());
3760 // Setting the NAN bit to yes does not make us a known NAN.
3761 r0
.set_varying (float_type_node
);
3763 ASSERT_FALSE (r0
.known_isnan ());
3765 // NAN is in a VARYING.
3766 r0
.set_varying (float_type_node
);
3767 real_nan (&r
, "", 1, TYPE_MODE (float_type_node
));
3768 tree nan
= build_real (float_type_node
, r
);
3769 ASSERT_TRUE (r0
.contains_p (nan
));
3771 // -NAN is in a VARYING.
3772 r0
.set_varying (float_type_node
);
3773 q
= real_value_negate (&r
);
3774 tree neg_nan
= build_real (float_type_node
, q
);
3775 ASSERT_TRUE (r0
.contains_p (neg_nan
));
3777 // Clearing the NAN on a [] NAN is the empty set.
3778 r0
.set_nan (float_type_node
);
3780 ASSERT_TRUE (r0
.undefined_p ());
3782 // [10,20] NAN ^ [21,25] NAN = [NAN]
3783 r0
= frange_float ("10", "20");
3785 r1
= frange_float ("21", "25");
3788 ASSERT_TRUE (r0
.known_isnan ());
3790 // NAN U [5,6] should be [5,6] +-NAN.
3791 r0
.set_nan (float_type_node
);
3792 r1
= frange_float ("5", "6");
3795 real_from_string (&q
, "5");
3796 real_from_string (&r
, "6");
3797 ASSERT_TRUE (real_identical (&q
, &r0
.lower_bound ()));
3798 ASSERT_TRUE (real_identical (&r
, &r0
.upper_bound ()));
3799 ASSERT_TRUE (!r0
.signbit_p (signbit
));
3800 ASSERT_TRUE (r0
.maybe_isnan ());
3804 range_tests_signed_zeros ()
3806 tree zero
= build_zero_cst (float_type_node
);
3807 tree neg_zero
= fold_build1 (NEGATE_EXPR
, float_type_node
, zero
);
3811 // [0,0] contains [0,0] but not [-0,-0] and vice versa.
3812 r0
= frange (zero
, zero
);
3813 r1
= frange (neg_zero
, neg_zero
);
3814 ASSERT_TRUE (r0
.contains_p (zero
));
3815 ASSERT_TRUE (!r0
.contains_p (neg_zero
));
3816 ASSERT_TRUE (r1
.contains_p (neg_zero
));
3817 ASSERT_TRUE (!r1
.contains_p (zero
));
3819 // Test contains_p() when we know the sign of the zero.
3820 r0
= frange (zero
, zero
);
3821 ASSERT_TRUE (r0
.contains_p (zero
));
3822 ASSERT_FALSE (r0
.contains_p (neg_zero
));
3823 r0
= frange (neg_zero
, neg_zero
);
3824 ASSERT_TRUE (r0
.contains_p (neg_zero
));
3825 ASSERT_FALSE (r0
.contains_p (zero
));
3827 // The intersection of zeros that differ in sign is a NAN (or
3828 // undefined if not honoring NANs).
3829 r0
= frange (neg_zero
, neg_zero
);
3830 r1
= frange (zero
, zero
);
3832 if (HONOR_NANS (float_type_node
))
3833 ASSERT_TRUE (r0
.known_isnan ());
3835 ASSERT_TRUE (r0
.undefined_p ());
3837 // The union of zeros that differ in sign is a zero with unknown sign.
3838 r0
= frange (zero
, zero
);
3839 r1
= frange (neg_zero
, neg_zero
);
3841 ASSERT_TRUE (r0
.zero_p () && !r0
.signbit_p (signbit
));
3843 // [-0, +0] has an unknown sign.
3844 r0
= frange (neg_zero
, zero
);
3845 ASSERT_TRUE (r0
.zero_p () && !r0
.signbit_p (signbit
));
3847 // [-0, +0] ^ [0, 0] is [0, 0]
3848 r0
= frange (neg_zero
, zero
);
3849 r1
= frange (zero
, zero
);
3851 ASSERT_TRUE (r0
.zero_p ());
3853 r0
= frange_float ("+0", "5");
3855 ASSERT_TRUE (r0
.signbit_p (signbit
) && !signbit
);
3857 r0
= frange_float ("-0", "5");
3859 ASSERT_TRUE (!r0
.signbit_p (signbit
));
3861 r0
= frange_float ("-0", "10");
3862 r1
= frange_float ("0", "5");
3864 ASSERT_TRUE (real_iszero (&r0
.lower_bound (), false));
3866 r0
= frange_float ("-0", "5");
3867 r1
= frange_float ("0", "5");
3869 ASSERT_TRUE (real_iszero (&r0
.lower_bound (), true));
3871 r0
= frange_float ("-5", "-0");
3873 r1
= frange_float ("0", "0");
3876 if (HONOR_NANS (float_type_node
))
3877 ASSERT_TRUE (r0
.known_isnan ());
3879 ASSERT_TRUE (r0
.undefined_p ());
3881 r0
.set_nonnegative (float_type_node
);
3882 ASSERT_TRUE (r0
.signbit_p (signbit
) && !signbit
);
3883 if (HONOR_NANS (float_type_node
))
3884 ASSERT_TRUE (r0
.maybe_isnan ());
3888 range_tests_signbit ()
3893 // Negative numbers should have the SIGNBIT set.
3894 r0
= frange_float ("-5", "-1");
3896 ASSERT_TRUE (r0
.signbit_p (signbit
) && signbit
);
3897 // Positive numbers should have the SIGNBIT clear.
3898 r0
= frange_float ("1", "10");
3900 ASSERT_TRUE (r0
.signbit_p (signbit
) && !signbit
);
3901 // Numbers containing zero should have an unknown SIGNBIT.
3902 r0
= frange_float ("0", "10");
3904 ASSERT_TRUE (r0
.signbit_p (signbit
) && !signbit
);
3905 // Numbers spanning both positive and negative should have an
3907 r0
= frange_float ("-10", "10");
3909 ASSERT_TRUE (!r0
.signbit_p (signbit
));
3910 r0
.set_varying (float_type_node
);
3911 ASSERT_TRUE (!r0
.signbit_p (signbit
));
3915 range_tests_floats ()
3919 if (HONOR_NANS (float_type_node
))
3921 range_tests_signbit ();
3923 if (HONOR_SIGNED_ZEROS (float_type_node
))
3924 range_tests_signed_zeros ();
3926 // A range of [-INF,+INF] is actually VARYING if no other properties
3928 r0
= frange_float ("-Inf", "+Inf");
3929 if (r0
.maybe_isnan ())
3930 ASSERT_TRUE (r0
.varying_p ());
3931 // ...unless it has some special property...
3933 ASSERT_FALSE (r0
.varying_p ());
3935 // For most architectures, where float and double are different
3936 // sizes, having the same endpoints does not necessarily mean the
3937 // ranges are equal.
3938 if (!types_compatible_p (float_type_node
, double_type_node
))
3940 r0
= frange_float ("3.0", "3.0", float_type_node
);
3941 r1
= frange_float ("3.0", "3.0", double_type_node
);
3945 // [3,5] U [10,12] = [3,12].
3946 r0
= frange_float ("3", "5");
3947 r1
= frange_float ("10", "12");
3949 ASSERT_EQ (r0
, frange_float ("3", "12"));
3951 // [5,10] U [4,8] = [4,10]
3952 r0
= frange_float ("5", "10");
3953 r1
= frange_float ("4", "8");
3955 ASSERT_EQ (r0
, frange_float ("4", "10"));
3957 // [3,5] U [4,10] = [3,10]
3958 r0
= frange_float ("3", "5");
3959 r1
= frange_float ("4", "10");
3961 ASSERT_EQ (r0
, frange_float ("3", "10"));
3963 // [4,10] U [5,11] = [4,11]
3964 r0
= frange_float ("4", "10");
3965 r1
= frange_float ("5", "11");
3967 ASSERT_EQ (r0
, frange_float ("4", "11"));
3969 // [3,12] ^ [10,12] = [10,12].
3970 r0
= frange_float ("3", "12");
3971 r1
= frange_float ("10", "12");
3973 ASSERT_EQ (r0
, frange_float ("10", "12"));
3975 // [10,12] ^ [11,11] = [11,11]
3976 r0
= frange_float ("10", "12");
3977 r1
= frange_float ("11", "11");
3979 ASSERT_EQ (r0
, frange_float ("11", "11"));
3981 // [10,20] ^ [5,15] = [10,15]
3982 r0
= frange_float ("10", "20");
3983 r1
= frange_float ("5", "15");
3985 ASSERT_EQ (r0
, frange_float ("10", "15"));
3987 // [10,20] ^ [15,25] = [15,20]
3988 r0
= frange_float ("10", "20");
3989 r1
= frange_float ("15", "25");
3991 ASSERT_EQ (r0
, frange_float ("15", "20"));
3993 // [10,20] ^ [21,25] = []
3994 r0
= frange_float ("10", "20");
3996 r1
= frange_float ("21", "25");
3999 ASSERT_TRUE (r0
.undefined_p ());
4005 range_tests_legacy ();
4006 range_tests_irange3 ();
4007 range_tests_int_range_max ();
4008 range_tests_strict_enum ();
4009 range_tests_nonzero_bits ();
4010 range_tests_floats ();
4011 range_tests_misc ();
4014 } // namespace selftest
4016 #endif // CHECKING_P