1 /* Support routines for value ranges.
2 Copyright (C) 2019-2023 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
);
207 gcc_checking_assert (is_a
<unsupported_range
> (src
));
213 // Equality operator for generic ranges.
216 vrange::operator== (const vrange
&src
) const
218 if (is_a
<irange
> (src
))
219 return as_a
<irange
> (*this) == as_a
<irange
> (src
);
220 if (is_a
<frange
> (src
))
221 return as_a
<frange
> (*this) == as_a
<frange
> (src
);
225 // Wrapper for vrange_printer to dump a range to a file.
228 vrange::dump (FILE *file
) const
230 pretty_printer buffer
;
231 pp_needs_newline (&buffer
) = true;
232 buffer
.buffer
->stream
= file
;
233 vrange_printer
vrange_pp (&buffer
);
234 this->accept (vrange_pp
);
242 add_vrange (const vrange
&v
, inchash::hash
&hstate
,
245 if (v
.undefined_p ())
247 hstate
.add_int (VR_UNDEFINED
);
250 // Types are ignored throughout to inhibit two ranges being equal
251 // but having different hash values. This can happen when two
252 // ranges are equal and their types are different (but
253 // types_compatible_p is true).
254 if (is_a
<irange
> (v
))
256 const irange
&r
= as_a
<irange
> (v
);
258 hstate
.add_int (VR_VARYING
);
260 hstate
.add_int (VR_RANGE
);
261 for (unsigned i
= 0; i
< r
.num_pairs (); ++i
)
263 hstate
.add_wide_int (r
.lower_bound (i
));
264 hstate
.add_wide_int (r
.upper_bound (i
));
266 hstate
.add_wide_int (r
.get_nonzero_bits ());
269 if (is_a
<frange
> (v
))
271 const frange
&r
= as_a
<frange
> (v
);
272 if (r
.known_isnan ())
273 hstate
.add_int (VR_NAN
);
276 hstate
.add_int (r
.varying_p () ? VR_VARYING
: VR_RANGE
);
277 hstate
.add_real_value (r
.lower_bound ());
278 hstate
.add_real_value (r
.upper_bound ());
280 nan_state nan
= r
.get_nan_state ();
281 hstate
.add_int (nan
.pos_p ());
282 hstate
.add_int (nan
.neg_p ());
288 } //namespace inchash
291 irange::supports_type_p (const_tree type
) const
293 return supports_p (type
);
296 // Return TRUE if R fits in THIS.
299 irange::fits_p (const vrange
&r
) const
301 return m_max_ranges
>= as_a
<irange
> (r
).num_pairs ();
305 irange::set_nonnegative (tree type
)
308 wi::zero (TYPE_PRECISION (type
)),
309 wi::to_wide (TYPE_MAX_VALUE (type
)));
313 frange::accept (const vrange_visitor
&v
) const
318 // Flush denormal endpoints to the appropriate 0.0.
321 frange::flush_denormals_to_zero ()
323 if (undefined_p () || known_isnan ())
326 machine_mode mode
= TYPE_MODE (type ());
327 // Flush [x, -DENORMAL] to [x, -0.0].
328 if (real_isdenormal (&m_max
, mode
) && real_isneg (&m_max
))
330 if (HONOR_SIGNED_ZEROS (m_type
))
335 // Flush [+DENORMAL, x] to [+0.0, x].
336 if (real_isdenormal (&m_min
, mode
) && !real_isneg (&m_min
))
340 // Setter for franges.
343 frange::set (tree type
,
344 const REAL_VALUE_TYPE
&min
, const REAL_VALUE_TYPE
&max
,
345 const nan_state
&nan
, value_range_kind kind
)
362 gcc_checking_assert (!real_isnan (&min
) && !real_isnan (&max
));
368 if (HONOR_NANS (m_type
))
370 m_pos_nan
= nan
.pos_p ();
371 m_neg_nan
= nan
.neg_p ();
379 if (!MODE_HAS_SIGNED_ZEROS (TYPE_MODE (m_type
)))
381 if (real_iszero (&m_min
, 1))
383 if (real_iszero (&m_max
, 1))
386 else if (!HONOR_SIGNED_ZEROS (m_type
))
388 if (real_iszero (&m_max
, 1))
390 if (real_iszero (&m_min
, 0))
394 // For -ffinite-math-only we can drop ranges outside the
395 // representable numbers to min/max for the type.
396 if (!HONOR_INFINITIES (m_type
))
398 REAL_VALUE_TYPE min_repr
= frange_val_min (m_type
);
399 REAL_VALUE_TYPE max_repr
= frange_val_max (m_type
);
400 if (real_less (&m_min
, &min_repr
))
402 else if (real_less (&max_repr
, &m_min
))
404 if (real_less (&max_repr
, &m_max
))
406 else if (real_less (&m_max
, &min_repr
))
410 // Check for swapped ranges.
411 gcc_checking_assert (real_compare (LE_EXPR
, &min
, &max
));
419 // Setter for an frange defaulting the NAN possibility to +-NAN when
423 frange::set (tree type
,
424 const REAL_VALUE_TYPE
&min
, const REAL_VALUE_TYPE
&max
,
425 value_range_kind kind
)
427 set (type
, min
, max
, nan_state (true), kind
);
431 frange::set (tree min
, tree max
, value_range_kind kind
)
433 set (TREE_TYPE (min
),
434 *TREE_REAL_CST_PTR (min
), *TREE_REAL_CST_PTR (max
), kind
);
437 // Normalize range to VARYING or UNDEFINED, or vice versa. Return
438 // TRUE if anything changed.
440 // A range with no known properties can be dropped to VARYING.
441 // Similarly, a VARYING with any properties should be dropped to a
442 // VR_RANGE. Normalizing ranges upon changing them ensures there is
443 // only one representation for a given range.
446 frange::normalize_kind ()
448 if (m_kind
== VR_RANGE
449 && frange_val_is_min (m_min
, m_type
)
450 && frange_val_is_max (m_max
, m_type
))
452 if (!HONOR_NANS (m_type
) || (m_pos_nan
&& m_neg_nan
))
454 set_varying (m_type
);
458 else if (m_kind
== VR_VARYING
)
460 if (HONOR_NANS (m_type
) && (!m_pos_nan
|| !m_neg_nan
))
463 m_min
= frange_val_min (m_type
);
464 m_max
= frange_val_max (m_type
);
468 else if (m_kind
== VR_NAN
&& !m_pos_nan
&& !m_neg_nan
)
473 // Union or intersect the zero endpoints of two ranges. For example:
474 // [-0, x] U [+0, x] => [-0, x]
475 // [ x, -0] U [ x, +0] => [ x, +0]
476 // [-0, x] ^ [+0, x] => [+0, x]
477 // [ x, -0] ^ [ x, +0] => [ x, -0]
479 // UNION_P is true when performing a union, or false when intersecting.
482 frange::combine_zeros (const frange
&r
, bool union_p
)
484 gcc_checking_assert (!undefined_p () && !known_isnan ());
486 bool changed
= false;
487 if (real_iszero (&m_min
) && real_iszero (&r
.m_min
)
488 && real_isneg (&m_min
) != real_isneg (&r
.m_min
))
490 m_min
.sign
= union_p
;
493 if (real_iszero (&m_max
) && real_iszero (&r
.m_max
)
494 && real_isneg (&m_max
) != real_isneg (&r
.m_max
))
496 m_max
.sign
= !union_p
;
499 // If the signs are swapped, the resulting range is empty.
500 if (m_min
.sign
== 0 && m_max
.sign
== 1)
511 // Union two ranges when one is known to be a NAN.
514 frange::union_nans (const frange
&r
)
516 gcc_checking_assert (known_isnan () || r
.known_isnan ());
524 m_pos_nan
|= r
.m_pos_nan
;
525 m_neg_nan
|= r
.m_neg_nan
;
533 frange::union_ (const vrange
&v
)
535 const frange
&r
= as_a
<frange
> (v
);
537 if (r
.undefined_p () || varying_p ())
539 if (undefined_p () || r
.varying_p ())
546 if (known_isnan () || r
.known_isnan ())
547 return union_nans (r
);
548 bool changed
= false;
549 if (m_pos_nan
!= r
.m_pos_nan
|| m_neg_nan
!= r
.m_neg_nan
)
551 m_pos_nan
|= r
.m_pos_nan
;
552 m_neg_nan
|= r
.m_neg_nan
;
556 // Combine endpoints.
557 if (real_less (&r
.m_min
, &m_min
))
562 if (real_less (&m_max
, &r
.m_max
))
568 if (HONOR_SIGNED_ZEROS (m_type
))
569 changed
|= combine_zeros (r
, true);
571 changed
|= normalize_kind ();
577 // Intersect two ranges when one is known to be a NAN.
580 frange::intersect_nans (const frange
&r
)
582 gcc_checking_assert (known_isnan () || r
.known_isnan ());
584 m_pos_nan
&= r
.m_pos_nan
;
585 m_neg_nan
&= r
.m_neg_nan
;
596 frange::intersect (const vrange
&v
)
598 const frange
&r
= as_a
<frange
> (v
);
600 if (undefined_p () || r
.varying_p ())
602 if (r
.undefined_p ())
614 if (known_isnan () || r
.known_isnan ())
615 return intersect_nans (r
);
616 bool changed
= false;
617 if (m_pos_nan
!= r
.m_pos_nan
|| m_neg_nan
!= r
.m_neg_nan
)
619 m_pos_nan
&= r
.m_pos_nan
;
620 m_neg_nan
&= r
.m_neg_nan
;
624 // Combine endpoints.
625 if (real_less (&m_min
, &r
.m_min
))
630 if (real_less (&r
.m_max
, &m_max
))
635 // If the endpoints are swapped, the resulting range is empty.
636 if (real_less (&m_max
, &m_min
))
647 if (HONOR_SIGNED_ZEROS (m_type
))
648 changed
|= combine_zeros (r
, false);
650 changed
|= normalize_kind ();
657 frange::operator= (const frange
&src
)
663 m_pos_nan
= src
.m_pos_nan
;
664 m_neg_nan
= src
.m_neg_nan
;
672 frange::operator== (const frange
&src
) const
674 if (m_kind
== src
.m_kind
)
680 return types_compatible_p (m_type
, src
.m_type
);
682 bool nan1
= known_isnan ();
683 bool nan2
= src
.known_isnan ();
687 return (m_pos_nan
== src
.m_pos_nan
688 && m_neg_nan
== src
.m_neg_nan
);
692 return (real_identical (&m_min
, &src
.m_min
)
693 && real_identical (&m_max
, &src
.m_max
)
694 && m_pos_nan
== src
.m_pos_nan
695 && m_neg_nan
== src
.m_neg_nan
696 && types_compatible_p (m_type
, src
.m_type
));
701 // Return TRUE if range contains R.
704 frange::contains_p (const REAL_VALUE_TYPE
&r
) const
706 gcc_checking_assert (m_kind
!= VR_ANTI_RANGE
);
717 if (!m_pos_nan
&& !m_neg_nan
)
719 // Both +NAN and -NAN are present.
720 if (m_pos_nan
&& m_neg_nan
)
722 return m_neg_nan
== r
.sign
;
727 if (real_compare (GE_EXPR
, &r
, &m_min
) && real_compare (LE_EXPR
, &r
, &m_max
))
729 // Make sure the signs are equal for signed zeros.
730 if (HONOR_SIGNED_ZEROS (m_type
) && real_iszero (&r
))
731 return r
.sign
== m_min
.sign
|| r
.sign
== m_max
.sign
;
737 // If range is a singleton, place it in RESULT and return TRUE. If
738 // RESULT is NULL, just return TRUE.
740 // A NAN can never be a singleton.
743 frange::internal_singleton_p (REAL_VALUE_TYPE
*result
) const
745 if (m_kind
== VR_RANGE
&& real_identical (&m_min
, &m_max
))
747 // Return false for any singleton that may be a NAN.
748 if (HONOR_NANS (m_type
) && maybe_isnan ())
751 if (MODE_COMPOSITE_P (TYPE_MODE (m_type
)))
753 // For IBM long doubles, if the value is +-Inf or is exactly
754 // representable in double, the other double could be +0.0
755 // or -0.0. Since this means there is more than one way to
756 // represent a value, return false to avoid propagating it.
757 // See libgcc/config/rs6000/ibm-ldouble-format for details.
758 if (real_isinf (&m_min
))
761 real_convert (&r
, DFmode
, &m_min
);
762 if (real_identical (&r
, &m_min
))
774 frange::singleton_p (tree
*result
) const
776 if (internal_singleton_p ())
779 *result
= build_real (m_type
, m_min
);
786 frange::singleton_p (REAL_VALUE_TYPE
&r
) const
788 return internal_singleton_p (&r
);
792 frange::supports_type_p (const_tree type
) const
794 return supports_p (type
);
798 frange::verify_range ()
801 gcc_checking_assert (HONOR_NANS (m_type
) || !maybe_isnan ());
805 gcc_checking_assert (!m_type
);
808 gcc_checking_assert (m_type
);
809 gcc_checking_assert (frange_val_is_min (m_min
, m_type
));
810 gcc_checking_assert (frange_val_is_max (m_max
, m_type
));
811 if (HONOR_NANS (m_type
))
812 gcc_checking_assert (m_pos_nan
&& m_neg_nan
);
814 gcc_checking_assert (!m_pos_nan
&& !m_neg_nan
);
817 gcc_checking_assert (m_type
);
820 gcc_checking_assert (m_type
);
821 gcc_checking_assert (m_pos_nan
|| m_neg_nan
);
827 // NANs cannot appear in the endpoints of a range.
828 gcc_checking_assert (!real_isnan (&m_min
) && !real_isnan (&m_max
));
830 // Make sure we don't have swapped ranges.
831 gcc_checking_assert (!real_less (&m_max
, &m_min
));
833 // [ +0.0, -0.0 ] is nonsensical.
834 gcc_checking_assert (!(real_iszero (&m_min
, 0) && real_iszero (&m_max
, 1)));
836 // If all the properties are clear, we better not span the entire
837 // domain, because that would make us varying.
838 if (m_pos_nan
&& m_neg_nan
)
839 gcc_checking_assert (!frange_val_is_min (m_min
, m_type
)
840 || !frange_val_is_max (m_max
, m_type
));
843 // We can't do much with nonzeros yet.
845 frange::set_nonzero (tree type
)
850 // We can't do much with nonzeros yet.
852 frange::nonzero_p () const
857 // Set range to [+0.0, +0.0] if honoring signed zeros, or [0.0, 0.0]
861 frange::set_zero (tree type
)
863 if (HONOR_SIGNED_ZEROS (type
))
865 set (type
, dconstm0
, dconst0
);
869 set (type
, dconst0
, dconst0
);
872 // Return TRUE for any zero regardless of sign.
875 frange::zero_p () const
877 return (m_kind
== VR_RANGE
878 && real_iszero (&m_min
)
879 && real_iszero (&m_max
));
882 // Set the range to non-negative numbers, that is [+0.0, +INF].
884 // The NAN in the resulting range (if HONOR_NANS) has a varying sign
885 // as there are no guarantees in IEEE 754 wrt to the sign of a NAN,
886 // except for copy, abs, and copysign. It is the responsibility of
887 // the caller to set the NAN's sign if desired.
890 frange::set_nonnegative (tree type
)
892 set (type
, dconst0
, frange_val_max (type
));
895 // Here we copy between any two irange's.
898 irange::operator= (const irange
&src
)
900 int needed
= src
.num_pairs ();
901 maybe_resize (needed
);
904 unsigned lim
= src
.m_num_ranges
;
905 if (lim
> m_max_ranges
)
908 for (x
= 0; x
< lim
* 2; ++x
)
909 m_base
[x
] = src
.m_base
[x
];
911 // If the range didn't fit, the last range should cover the rest.
912 if (lim
!= src
.m_num_ranges
)
913 m_base
[x
- 1] = src
.m_base
[src
.m_num_ranges
* 2 - 1];
918 m_nonzero_mask
= src
.m_nonzero_mask
;
919 if (m_max_ranges
== 1)
927 get_legacy_range (const irange
&r
, tree
&min
, tree
&max
)
929 if (r
.undefined_p ())
936 tree type
= r
.type ();
939 min
= wide_int_to_tree (type
, r
.lower_bound ());
940 max
= wide_int_to_tree (type
, r
.upper_bound ());
944 unsigned int precision
= TYPE_PRECISION (type
);
945 signop sign
= TYPE_SIGN (type
);
946 if (r
.num_pairs () > 1
948 && r
.lower_bound () == wi::min_value (precision
, sign
)
949 && r
.upper_bound () == wi::max_value (precision
, sign
))
951 int_range
<3> inv (r
);
953 min
= wide_int_to_tree (type
, inv
.lower_bound (0));
954 max
= wide_int_to_tree (type
, inv
.upper_bound (0));
955 return VR_ANTI_RANGE
;
958 min
= wide_int_to_tree (type
, r
.lower_bound ());
959 max
= wide_int_to_tree (type
, r
.upper_bound ());
963 /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
964 This means adjusting VRTYPE, MIN and MAX representing the case of a
965 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
966 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
967 In corner cases where MAX+1 or MIN-1 wraps this will fall back
969 This routine exists to ease canonicalization in the case where we
970 extract ranges from var + CST op limit. */
973 irange::set (tree type
, const wide_int
&min
, const wide_int
&max
,
974 value_range_kind kind
)
976 unsigned prec
= TYPE_PRECISION (type
);
977 signop sign
= TYPE_SIGN (type
);
978 wide_int min_value
= wi::min_value (prec
, sign
);
979 wide_int max_value
= wi::max_value (prec
, sign
);
982 m_nonzero_mask
= wi::minus_one (prec
);
984 if (kind
== VR_RANGE
)
989 if (min
== min_value
&& max
== max_value
)
996 gcc_checking_assert (kind
== VR_ANTI_RANGE
);
997 gcc_checking_assert (m_max_ranges
> 1);
999 m_kind
= VR_UNDEFINED
;
1001 wi::overflow_type ovf
;
1004 lim
= wi::add (min
, -1, sign
, &ovf
);
1006 lim
= wi::sub (min
, 1, sign
, &ovf
);
1011 m_base
[0] = min_value
;
1016 lim
= wi::sub (max
, -1, sign
, &ovf
);
1018 lim
= wi::add (max
, 1, sign
, &ovf
);
1022 m_base
[m_num_ranges
* 2] = lim
;
1023 m_base
[m_num_ranges
* 2 + 1] = max_value
;
1033 irange::set (tree min
, tree max
, value_range_kind kind
)
1035 if (POLY_INT_CST_P (min
) || POLY_INT_CST_P (max
))
1037 set_varying (TREE_TYPE (min
));
1041 gcc_checking_assert (TREE_CODE (min
) == INTEGER_CST
);
1042 gcc_checking_assert (TREE_CODE (max
) == INTEGER_CST
);
1044 return set (TREE_TYPE (min
), wi::to_wide (min
), wi::to_wide (max
), kind
);
1047 // Check the validity of the range.
1050 irange::verify_range ()
1052 gcc_checking_assert (m_discriminator
== VR_IRANGE
);
1053 if (m_kind
== VR_UNDEFINED
)
1055 gcc_checking_assert (m_num_ranges
== 0);
1058 gcc_checking_assert (m_num_ranges
<= m_max_ranges
);
1060 // Legacy allowed these to represent VARYING for unknown types.
1061 // Leave this in for now, until all users are converted. Eventually
1062 // we should abort in set_varying.
1063 if (m_kind
== VR_VARYING
&& m_type
== error_mark_node
)
1066 unsigned prec
= TYPE_PRECISION (m_type
);
1067 if (m_kind
== VR_VARYING
)
1069 gcc_checking_assert (m_nonzero_mask
== -1);
1070 gcc_checking_assert (m_num_ranges
== 1);
1071 gcc_checking_assert (varying_compatible_p ());
1072 gcc_checking_assert (lower_bound ().get_precision () == prec
);
1073 gcc_checking_assert (upper_bound ().get_precision () == prec
);
1076 gcc_checking_assert (m_num_ranges
!= 0);
1077 gcc_checking_assert (!varying_compatible_p ());
1078 for (unsigned i
= 0; i
< m_num_ranges
; ++i
)
1080 wide_int lb
= lower_bound (i
);
1081 wide_int ub
= upper_bound (i
);
1082 gcc_checking_assert (lb
.get_precision () == prec
);
1083 gcc_checking_assert (ub
.get_precision () == prec
);
1084 int c
= wi::cmp (lb
, ub
, TYPE_SIGN (m_type
));
1085 gcc_checking_assert (c
== 0 || c
== -1);
1087 gcc_checking_assert (m_nonzero_mask
.get_precision () == prec
);
1091 irange::operator== (const irange
&other
) const
1093 if (m_num_ranges
!= other
.m_num_ranges
)
1096 if (m_num_ranges
== 0)
1099 signop sign1
= TYPE_SIGN (type ());
1100 signop sign2
= TYPE_SIGN (other
.type ());
1102 for (unsigned i
= 0; i
< m_num_ranges
; ++i
)
1104 widest_int lb
= widest_int::from (lower_bound (i
), sign1
);
1105 widest_int ub
= widest_int::from (upper_bound (i
), sign1
);
1106 widest_int lb_other
= widest_int::from (other
.lower_bound (i
), sign2
);
1107 widest_int ub_other
= widest_int::from (other
.upper_bound (i
), sign2
);
1108 if (lb
!= lb_other
|| ub
!= ub_other
)
1111 widest_int nz1
= widest_int::from (get_nonzero_bits (), sign1
);
1112 widest_int nz2
= widest_int::from (other
.get_nonzero_bits (), sign2
);
1116 /* If range is a singleton, place it in RESULT and return TRUE. */
1119 irange::singleton_p (tree
*result
) const
1121 if (num_pairs () == 1 && lower_bound () == upper_bound ())
1124 *result
= wide_int_to_tree (type (), lower_bound ());
1131 irange::singleton_p (wide_int
&w
) const
1133 if (num_pairs () == 1 && lower_bound () == upper_bound ())
1141 /* Return 1 if CST is inside value range.
1142 0 if CST is not inside value range.
1144 Benchmark compile/20001226-1.c compilation time after changing this
1148 irange::contains_p (const wide_int
&cst
) const
1153 // See if we can exclude CST based on the nonzero bits.
1154 if (m_nonzero_mask
!= -1
1156 && wi::bit_and (m_nonzero_mask
, cst
) == 0)
1159 signop sign
= TYPE_SIGN (type ());
1160 for (unsigned r
= 0; r
< m_num_ranges
; ++r
)
1162 if (wi::lt_p (cst
, lower_bound (r
), sign
))
1164 if (wi::le_p (cst
, upper_bound (r
), sign
))
1171 // Perform an efficient union with R when both ranges have only a single pair.
1172 // Excluded are VARYING and UNDEFINED ranges.
1175 irange::irange_single_pair_union (const irange
&r
)
1177 gcc_checking_assert (!undefined_p () && !varying_p ());
1178 gcc_checking_assert (!r
.undefined_p () && !varying_p ());
1180 signop sign
= TYPE_SIGN (m_type
);
1181 // Check if current lower bound is also the new lower bound.
1182 if (wi::le_p (m_base
[0], r
.m_base
[0], sign
))
1184 // If current upper bound is new upper bound, we're done.
1185 if (wi::le_p (r
.m_base
[1], m_base
[1], sign
))
1186 return union_nonzero_bits (r
);
1187 // Otherwise R has the new upper bound.
1188 // Check for overlap/touching ranges, or single target range.
1189 if (m_max_ranges
== 1
1190 || (widest_int::from (m_base
[1], sign
) + 1
1191 >= widest_int::from (r
.m_base
[0], TYPE_SIGN (r
.m_type
))))
1192 m_base
[1] = r
.m_base
[1];
1195 // This is a dual range result.
1196 m_base
[2] = r
.m_base
[0];
1197 m_base
[3] = r
.m_base
[1];
1200 union_nonzero_bits (r
);
1204 // Set the new lower bound to R's lower bound.
1205 wide_int lb
= m_base
[0];
1206 m_base
[0] = r
.m_base
[0];
1208 // If R fully contains THIS range, just set the upper bound.
1209 if (wi::ge_p (r
.m_base
[1], m_base
[1], sign
))
1210 m_base
[1] = r
.m_base
[1];
1211 // Check for overlapping ranges, or target limited to a single range.
1212 else if (m_max_ranges
== 1
1213 || (widest_int::from (r
.m_base
[1], TYPE_SIGN (r
.m_type
)) + 1
1214 >= widest_int::from (lb
, sign
)))
1218 // Left with 2 pairs.
1221 m_base
[3] = m_base
[1];
1222 m_base
[1] = r
.m_base
[1];
1224 union_nonzero_bits (r
);
1228 // Return TRUE if anything changes.
1231 irange::union_ (const vrange
&v
)
1233 const irange
&r
= as_a
<irange
> (v
);
1235 if (r
.undefined_p ())
1251 set_varying (type ());
1255 // Special case one range union one range.
1256 if (m_num_ranges
== 1 && r
.m_num_ranges
== 1)
1257 return irange_single_pair_union (r
);
1259 // If this ranges fully contains R, then we need do nothing.
1260 if (irange_contains_p (r
))
1261 return union_nonzero_bits (r
);
1263 // Do not worry about merging and such by reserving twice as many
1264 // pairs as needed, and then simply sort the 2 ranges into this
1265 // intermediate form.
1267 // The intermediate result will have the property that the beginning
1268 // of each range is <= the beginning of the next range. There may
1269 // be overlapping ranges at this point. I.e. this would be valid
1270 // [-20, 10], [-10, 0], [0, 20], [40, 90] as it satisfies this
1271 // constraint : -20 < -10 < 0 < 40. When the range is rebuilt into r,
1272 // the merge is performed.
1274 // [Xi,Yi]..[Xn,Yn] U [Xj,Yj]..[Xm,Ym] --> [Xk,Yk]..[Xp,Yp]
1275 auto_vec
<wide_int
, 20> res (m_num_ranges
* 2 + r
.m_num_ranges
* 2);
1276 unsigned i
= 0, j
= 0, k
= 0;
1277 signop sign
= TYPE_SIGN (m_type
);
1279 while (i
< m_num_ranges
* 2 && j
< r
.m_num_ranges
* 2)
1281 // lower of Xi and Xj is the lowest point.
1282 if (widest_int::from (m_base
[i
], sign
)
1283 <= widest_int::from (r
.m_base
[j
], sign
))
1285 res
.quick_push (m_base
[i
]);
1286 res
.quick_push (m_base
[i
+ 1]);
1292 res
.quick_push (r
.m_base
[j
]);
1293 res
.quick_push (r
.m_base
[j
+ 1]);
1298 for ( ; i
< m_num_ranges
* 2; i
+= 2)
1300 res
.quick_push (m_base
[i
]);
1301 res
.quick_push (m_base
[i
+ 1]);
1304 for ( ; j
< r
.m_num_ranges
* 2; j
+= 2)
1306 res
.quick_push (r
.m_base
[j
]);
1307 res
.quick_push (r
.m_base
[j
+ 1]);
1311 // Now normalize the vector removing any overlaps.
1313 for (j
= 2; j
< k
; j
+= 2)
1315 // Current upper+1 is >= lower bound next pair, then we merge ranges.
1316 if (widest_int::from (res
[i
- 1], sign
) + 1
1317 >= widest_int::from (res
[j
], sign
))
1319 // New upper bounds is greater of current or the next one.
1320 if (widest_int::from (res
[j
+ 1], sign
)
1321 > widest_int::from (res
[i
- 1], sign
))
1322 res
[i
- 1] = res
[j
+ 1];
1326 // This is a new distinct range, but no point in copying it
1327 // if it is already in the right place.
1331 res
[i
++] = res
[j
+ 1];
1338 // At this point, the vector should have i ranges, none overlapping.
1339 // Now it simply needs to be copied, and if there are too many
1340 // ranges, merge some. We wont do any analysis as to what the
1341 // "best" merges are, simply combine the final ranges into one.
1342 maybe_resize (i
/ 2);
1343 if (i
> m_max_ranges
* 2)
1345 res
[m_max_ranges
* 2 - 1] = res
[i
- 1];
1346 i
= m_max_ranges
* 2;
1349 for (j
= 0; j
< i
; j
++)
1350 m_base
[j
] = res
[j
];
1351 m_num_ranges
= i
/ 2;
1354 union_nonzero_bits (r
);
1358 // Return TRUE if THIS fully contains R. No undefined or varying cases.
1361 irange::irange_contains_p (const irange
&r
) const
1363 gcc_checking_assert (!undefined_p () && !varying_p ());
1364 gcc_checking_assert (!r
.undefined_p () && !varying_p ());
1366 // In order for THIS to fully contain R, all of the pairs within R must
1367 // be fully contained by the pairs in this object.
1368 signop sign
= TYPE_SIGN (m_type
);
1371 wide_int rl
= r
.m_base
[0];
1372 wide_int ru
= r
.m_base
[1];
1373 wide_int l
= m_base
[0];
1374 wide_int u
= m_base
[1];
1377 // If r is contained within this range, move to the next R
1378 if (wi::ge_p (rl
, l
, sign
)
1379 && wi::le_p (ru
, u
, sign
))
1381 // This pair is OK, Either done, or bump to the next.
1382 if (++ri
>= r
.num_pairs ())
1384 rl
= r
.m_base
[ri
* 2];
1385 ru
= r
.m_base
[ri
* 2 + 1];
1388 // Otherwise, check if this's pair occurs before R's.
1389 if (wi::lt_p (u
, rl
, sign
))
1391 // There's still at least one pair of R left.
1392 if (++i
>= num_pairs ())
1395 u
= m_base
[i
* 2 + 1];
1404 // Return TRUE if anything changes.
1407 irange::intersect (const vrange
&v
)
1409 const irange
&r
= as_a
<irange
> (v
);
1410 gcc_checking_assert (undefined_p () || r
.undefined_p ()
1411 || range_compatible_p (type (), r
.type ()));
1415 if (r
.undefined_p ())
1428 if (r
.num_pairs () == 1)
1430 bool res
= intersect (r
.lower_bound (), r
.upper_bound ());
1434 res
|= intersect_nonzero_bits (r
);
1438 // If R fully contains this, then intersection will change nothing.
1439 if (r
.irange_contains_p (*this))
1440 return intersect_nonzero_bits (r
);
1442 // ?? We could probably come up with something smarter than the
1443 // worst case scenario here.
1444 int needed
= num_pairs () + r
.num_pairs ();
1445 maybe_resize (needed
);
1447 signop sign
= TYPE_SIGN (m_type
);
1448 unsigned bld_pair
= 0;
1449 unsigned bld_lim
= m_max_ranges
;
1450 int_range_max
r2 (*this);
1451 unsigned r2_lim
= r2
.num_pairs ();
1453 for (unsigned i
= 0; i
< r
.num_pairs (); )
1455 // If r1's upper is < r2's lower, we can skip r1's pair.
1456 wide_int ru
= r
.m_base
[i
* 2 + 1];
1457 wide_int r2l
= r2
.m_base
[i2
* 2];
1458 if (wi::lt_p (ru
, r2l
, sign
))
1463 // Likewise, skip r2's pair if its excluded.
1464 wide_int r2u
= r2
.m_base
[i2
* 2 + 1];
1465 wide_int rl
= r
.m_base
[i
* 2];
1466 if (wi::lt_p (r2u
, rl
, sign
))
1471 // No more r2, break.
1475 // Must be some overlap. Find the highest of the lower bounds,
1476 // and set it, unless the build limits lower bounds is already
1478 if (bld_pair
< bld_lim
)
1480 if (wi::ge_p (rl
, r2l
, sign
))
1481 m_base
[bld_pair
* 2] = rl
;
1483 m_base
[bld_pair
* 2] = r2l
;
1486 // Decrease and set a new upper.
1489 // ...and choose the lower of the upper bounds.
1490 if (wi::le_p (ru
, r2u
, sign
))
1492 m_base
[bld_pair
* 2 + 1] = ru
;
1494 // Move past the r1 pair and keep trying.
1500 m_base
[bld_pair
* 2 + 1] = r2u
;
1505 // No more r2, break.
1508 // r2 has the higher lower bound.
1511 // At the exit of this loop, it is one of 2 things:
1512 // ran out of r1, or r2, but either means we are done.
1513 m_num_ranges
= bld_pair
;
1514 if (m_num_ranges
== 0)
1521 intersect_nonzero_bits (r
);
1526 // Multirange intersect for a specified wide_int [lb, ub] range.
1527 // Return TRUE if intersect changed anything.
1529 // NOTE: It is the caller's responsibility to intersect the nonzero masks.
1532 irange::intersect (const wide_int
& lb
, const wide_int
& ub
)
1534 // Undefined remains undefined.
1538 tree range_type
= type();
1539 signop sign
= TYPE_SIGN (range_type
);
1541 gcc_checking_assert (TYPE_PRECISION (range_type
) == wi::get_precision (lb
));
1542 gcc_checking_assert (TYPE_PRECISION (range_type
) == wi::get_precision (ub
));
1544 // If this range is fully contained, then intersection will do nothing.
1545 if (wi::ge_p (lower_bound (), lb
, sign
)
1546 && wi::le_p (upper_bound (), ub
, sign
))
1549 unsigned bld_index
= 0;
1550 unsigned pair_lim
= num_pairs ();
1551 for (unsigned i
= 0; i
< pair_lim
; i
++)
1553 wide_int pairl
= m_base
[i
* 2];
1554 wide_int pairu
= m_base
[i
* 2 + 1];
1555 // Once UB is less than a pairs lower bound, we're done.
1556 if (wi::lt_p (ub
, pairl
, sign
))
1558 // if LB is greater than this pairs upper, this pair is excluded.
1559 if (wi::lt_p (pairu
, lb
, sign
))
1562 // Must be some overlap. Find the highest of the lower bounds,
1564 if (wi::gt_p (lb
, pairl
, sign
))
1565 m_base
[bld_index
* 2] = lb
;
1567 m_base
[bld_index
* 2] = pairl
;
1569 // ...and choose the lower of the upper bounds and if the base pair
1570 // has the lower upper bound, need to check next pair too.
1571 if (wi::lt_p (ub
, pairu
, sign
))
1573 m_base
[bld_index
++ * 2 + 1] = ub
;
1577 m_base
[bld_index
++ * 2 + 1] = pairu
;
1580 m_num_ranges
= bld_index
;
1581 if (m_num_ranges
== 0)
1588 // No need to call normalize_kind(), as the caller will do this
1589 // while intersecting the nonzero mask.
1596 // Signed 1-bits are strange. You can't subtract 1, because you can't
1597 // represent the number 1. This works around that for the invert routine.
1599 static wide_int
inline
1600 subtract_one (const wide_int
&x
, tree type
, wi::overflow_type
&overflow
)
1602 if (TYPE_SIGN (type
) == SIGNED
)
1603 return wi::add (x
, -1, SIGNED
, &overflow
);
1605 return wi::sub (x
, 1, UNSIGNED
, &overflow
);
1608 // The analogous function for adding 1.
1610 static wide_int
inline
1611 add_one (const wide_int
&x
, tree type
, wi::overflow_type
&overflow
)
1613 if (TYPE_SIGN (type
) == SIGNED
)
1614 return wi::sub (x
, -1, SIGNED
, &overflow
);
1616 return wi::add (x
, 1, UNSIGNED
, &overflow
);
1619 // Return the inverse of a range.
1624 gcc_checking_assert (!undefined_p () && !varying_p ());
1626 // We always need one more set of bounds to represent an inverse, so
1627 // if we're at the limit, we can't properly represent things.
1629 // For instance, to represent the inverse of a 2 sub-range set
1630 // [5, 10][20, 30], we would need a 3 sub-range set
1631 // [-MIN, 4][11, 19][31, MAX].
1633 // In this case, return the most conservative thing.
1635 // However, if any of the extremes of the range are -MIN/+MAX, we
1636 // know we will not need an extra bound. For example:
1638 // INVERT([-MIN,20][30,40]) => [21,29][41,+MAX]
1639 // INVERT([-MIN,20][30,MAX]) => [21,29]
1640 tree ttype
= type ();
1641 unsigned prec
= TYPE_PRECISION (ttype
);
1642 signop sign
= TYPE_SIGN (ttype
);
1643 wide_int type_min
= wi::min_value (prec
, sign
);
1644 wide_int type_max
= wi::max_value (prec
, sign
);
1645 m_nonzero_mask
= wi::minus_one (prec
);
1647 // At this point, we need one extra sub-range to represent the
1649 maybe_resize (m_num_ranges
+ 1);
1651 // The algorithm is as follows. To calculate INVERT ([a,b][c,d]), we
1652 // generate [-MIN, a-1][b+1, c-1][d+1, MAX].
1654 // If there is an over/underflow in the calculation for any
1655 // sub-range, we eliminate that subrange. This allows us to easily
1656 // calculate INVERT([-MIN, 5]) with: [-MIN, -MIN-1][6, MAX]. And since
1657 // we eliminate the underflow, only [6, MAX] remains.
1659 wi::overflow_type ovf
;
1660 // Construct leftmost range.
1661 int_range_max
orig_range (*this);
1662 unsigned nitems
= 0;
1664 // If this is going to underflow on the MINUS 1, don't even bother
1665 // checking. This also handles subtracting one from an unsigned 0,
1666 // which doesn't set the underflow bit.
1667 if (type_min
!= orig_range
.lower_bound ())
1669 m_base
[nitems
++] = type_min
;
1670 tmp
= subtract_one (orig_range
.lower_bound (), ttype
, ovf
);
1671 m_base
[nitems
++] = tmp
;
1676 // Construct middle ranges if applicable.
1677 if (orig_range
.num_pairs () > 1)
1680 for (; j
< (orig_range
.num_pairs () * 2) - 1; j
+= 2)
1682 // The middle ranges cannot have MAX/MIN, so there's no need
1683 // to check for unsigned overflow on the +1 and -1 here.
1684 tmp
= wi::add (orig_range
.m_base
[j
], 1, sign
, &ovf
);
1685 m_base
[nitems
++] = tmp
;
1686 tmp
= subtract_one (orig_range
.m_base
[j
+ 1], ttype
, ovf
);
1687 m_base
[nitems
++] = tmp
;
1693 // Construct rightmost range.
1695 // However, if this will overflow on the PLUS 1, don't even bother.
1696 // This also handles adding one to an unsigned MAX, which doesn't
1697 // set the overflow bit.
1698 if (type_max
!= orig_range
.m_base
[i
])
1700 tmp
= add_one (orig_range
.m_base
[i
], ttype
, ovf
);
1701 m_base
[nitems
++] = tmp
;
1702 m_base
[nitems
++] = type_max
;
1706 m_num_ranges
= nitems
/ 2;
1708 // We disallow undefined or varying coming in, so the result can
1709 // only be a VR_RANGE.
1710 gcc_checking_assert (m_kind
== VR_RANGE
);
1716 // Return the nonzero bits inherent in the range.
1719 irange::get_nonzero_bits_from_range () const
1721 wide_int min
= lower_bound ();
1722 wide_int max
= upper_bound ();
1723 wide_int xorv
= min
^ max
;
1726 unsigned prec
= TYPE_PRECISION (type ());
1727 xorv
= wi::mask (prec
- wi::clz (xorv
), false, prec
);
1732 // If the the nonzero mask can be trivially converted to a range, do
1733 // so and return TRUE.
1736 irange::set_range_from_nonzero_bits ()
1738 gcc_checking_assert (!undefined_p ());
1739 if (m_nonzero_mask
== -1)
1741 unsigned popcount
= wi::popcount (m_nonzero_mask
);
1743 // If we have only one bit set in the mask, we can figure out the
1744 // range immediately.
1747 // Make sure we don't pessimize the range.
1748 if (!contains_p (m_nonzero_mask
))
1751 bool has_zero
= contains_zero_p (*this);
1752 wide_int nz
= m_nonzero_mask
;
1753 set (m_type
, nz
, nz
);
1754 m_nonzero_mask
= nz
;
1758 zero
.set_zero (type ());
1763 else if (popcount
== 0)
1772 irange::set_nonzero_bits (const wide_int
&bits
)
1774 gcc_checking_assert (!undefined_p ());
1776 // Drop VARYINGs with a nonzero mask to a plain range.
1777 if (m_kind
== VR_VARYING
&& bits
!= -1)
1780 m_nonzero_mask
= bits
;
1781 if (set_range_from_nonzero_bits ())
1789 // Return the nonzero bitmask. This will return the nonzero bits plus
1790 // the nonzero bits inherent in the range.
1793 irange::get_nonzero_bits () const
1795 gcc_checking_assert (!undefined_p ());
1796 // The nonzero mask inherent in the range is calculated on-demand.
1797 // For example, [0,255] does not have a 0xff nonzero mask by default
1798 // (unless manually set). This saves us considerable time, because
1799 // setting it at creation incurs a large penalty for irange::set.
1800 // At the time of writing there was a 5% slowdown in VRP if we kept
1801 // the mask precisely up to date at all times. Instead, we default
1802 // to -1 and set it when explicitly requested. However, this
1803 // function will always return the correct mask.
1804 if (m_nonzero_mask
== -1)
1805 return get_nonzero_bits_from_range ();
1807 return m_nonzero_mask
& get_nonzero_bits_from_range ();
1810 // Intersect the nonzero bits in R into THIS and normalize the range.
1811 // Return TRUE if the intersection changed anything.
1814 irange::intersect_nonzero_bits (const irange
&r
)
1816 gcc_checking_assert (!undefined_p () && !r
.undefined_p ());
1818 if (m_nonzero_mask
== -1 && r
.m_nonzero_mask
== -1)
1826 bool changed
= false;
1827 if (m_nonzero_mask
!= r
.m_nonzero_mask
)
1829 wide_int nz
= get_nonzero_bits () & r
.get_nonzero_bits ();
1830 // If the nonzero bits did not change, return false.
1831 if (nz
== get_nonzero_bits ())
1834 m_nonzero_mask
= nz
;
1835 if (set_range_from_nonzero_bits ())
1845 // Union the nonzero bits in R into THIS and normalize the range.
1846 // Return TRUE if the union changed anything.
1849 irange::union_nonzero_bits (const irange
&r
)
1851 gcc_checking_assert (!undefined_p () && !r
.undefined_p ());
1853 if (m_nonzero_mask
== -1 && r
.m_nonzero_mask
== -1)
1861 bool changed
= false;
1862 if (m_nonzero_mask
!= r
.m_nonzero_mask
)
1864 wide_int save
= get_nonzero_bits ();
1865 m_nonzero_mask
= save
| r
.get_nonzero_bits ();
1866 // No need to call set_range_from_nonzero_bits, because we'll
1867 // never narrow the range. Besides, it would cause endless
1868 // recursion because of the union_ in
1869 // set_range_from_nonzero_bits.
1870 changed
= m_nonzero_mask
!= save
;
1879 dump_value_range (FILE *file
, const vrange
*vr
)
1885 debug (const vrange
*vr
)
1887 dump_value_range (stderr
, vr
);
1888 fprintf (stderr
, "\n");
1892 debug (const vrange
&vr
)
1898 debug (const value_range
*vr
)
1900 dump_value_range (stderr
, vr
);
1901 fprintf (stderr
, "\n");
1905 debug (const value_range
&vr
)
1907 dump_value_range (stderr
, &vr
);
1908 fprintf (stderr
, "\n");
1911 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
1914 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
1918 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
1924 gt_ggc_mx (irange
*x
)
1926 if (!x
->undefined_p ())
1927 gt_ggc_mx (x
->m_type
);
1931 gt_pch_nx (irange
*x
)
1933 if (!x
->undefined_p ())
1934 gt_pch_nx (x
->m_type
);
1938 gt_pch_nx (irange
*x
, gt_pointer_operator op
, void *cookie
)
1940 for (unsigned i
= 0; i
< x
->m_num_ranges
; ++i
)
1942 op (&x
->m_base
[i
* 2], NULL
, cookie
);
1943 op (&x
->m_base
[i
* 2 + 1], NULL
, cookie
);
1948 gt_ggc_mx (frange
*x
)
1950 gt_ggc_mx (x
->m_type
);
1954 gt_pch_nx (frange
*x
)
1956 gt_pch_nx (x
->m_type
);
1960 gt_pch_nx (frange
*x
, gt_pointer_operator op
, void *cookie
)
1962 op (&x
->m_type
, NULL
, cookie
);
1966 gt_ggc_mx (vrange
*x
)
1968 if (is_a
<irange
> (*x
))
1969 return gt_ggc_mx ((irange
*) x
);
1970 if (is_a
<frange
> (*x
))
1971 return gt_ggc_mx ((frange
*) x
);
1976 gt_pch_nx (vrange
*x
)
1978 if (is_a
<irange
> (*x
))
1979 return gt_pch_nx ((irange
*) x
);
1980 if (is_a
<frange
> (*x
))
1981 return gt_pch_nx ((frange
*) x
);
1986 gt_pch_nx (vrange
*x
, gt_pointer_operator op
, void *cookie
)
1988 if (is_a
<irange
> (*x
))
1989 gt_pch_nx ((irange
*) x
, op
, cookie
);
1990 else if (is_a
<frange
> (*x
))
1991 gt_pch_nx ((frange
*) x
, op
, cookie
);
1996 // ?? These stubs are for ipa-prop.cc which use a value_range in a
1997 // hash_traits. hash-traits.h defines an extern of gt_ggc_mx (T &)
1998 // instead of picking up the gt_ggc_mx (T *) version.
2000 gt_pch_nx (int_range
<2> *&x
)
2002 return gt_pch_nx ((irange
*) x
);
2006 gt_ggc_mx (int_range
<2> *&x
)
2008 return gt_ggc_mx ((irange
*) x
);
2011 #define DEFINE_INT_RANGE_INSTANCE(N) \
2012 template int_range<N>::int_range(tree_node *, \
2015 value_range_kind); \
2016 template int_range<N>::int_range(tree); \
2017 template int_range<N>::int_range(const irange &); \
2018 template int_range<N>::int_range(const int_range &); \
2019 template int_range<N>& int_range<N>::operator= (const int_range &);
2021 DEFINE_INT_RANGE_INSTANCE(1)
2022 DEFINE_INT_RANGE_INSTANCE(2)
2023 DEFINE_INT_RANGE_INSTANCE(3)
2024 DEFINE_INT_RANGE_INSTANCE(255)
2027 #include "selftest.h"
2029 #define INT(x) wi::shwi ((x), TYPE_PRECISION (integer_type_node))
2030 #define UINT(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_type_node))
2031 #define SCHAR(x) wi::shwi ((x), TYPE_PRECISION (signed_char_type_node))
2037 range (tree type
, int a
, int b
, value_range_kind kind
= VR_RANGE
)
2040 if (TYPE_UNSIGNED (type
))
2042 w1
= wi::uhwi (a
, TYPE_PRECISION (type
));
2043 w2
= wi::uhwi (b
, TYPE_PRECISION (type
));
2047 w1
= wi::shwi (a
, TYPE_PRECISION (type
));
2048 w2
= wi::shwi (b
, TYPE_PRECISION (type
));
2050 return int_range
<2> (type
, w1
, w2
, kind
);
2054 range_int (int a
, int b
, value_range_kind kind
= VR_RANGE
)
2056 return range (integer_type_node
, a
, b
, kind
);
2060 range_uint (int a
, int b
, value_range_kind kind
= VR_RANGE
)
2062 return range (unsigned_type_node
, a
, b
, kind
);
2066 range_uint128 (int a
, int b
, value_range_kind kind
= VR_RANGE
)
2068 tree u128_type_node
= build_nonstandard_integer_type (128, 1);
2069 return range (u128_type_node
, a
, b
, kind
);
2073 range_uchar (int a
, int b
, value_range_kind kind
= VR_RANGE
)
2075 return range (unsigned_char_type_node
, a
, b
, kind
);
2079 range_char (int a
, int b
, value_range_kind kind
= VR_RANGE
)
2081 return range (signed_char_type_node
, a
, b
, kind
);
2085 build_range3 (int a
, int b
, int c
, int d
, int e
, int f
)
2087 int_range
<3> i1
= range_int (a
, b
);
2088 int_range
<3> i2
= range_int (c
, d
);
2089 int_range
<3> i3
= range_int (e
, f
);
2096 range_tests_irange3 ()
2098 int_range
<3> r0
, r1
, r2
;
2099 int_range
<3> i1
, i2
, i3
;
2101 // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20].
2102 r0
= range_int (10, 20);
2103 r1
= range_int (5, 8);
2105 r1
= range_int (1, 3);
2107 ASSERT_TRUE (r0
== build_range3 (1, 3, 5, 8, 10, 20));
2109 // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20].
2110 r1
= range_int (-5, 0);
2112 ASSERT_TRUE (r0
== build_range3 (-5, 3, 5, 8, 10, 20));
2114 // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60].
2115 r1
= range_int (50, 60);
2116 r0
= range_int (10, 20);
2117 r0
.union_ (range_int (30, 40));
2119 ASSERT_TRUE (r0
== build_range3 (10, 20, 30, 40, 50, 60));
2120 // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80].
2121 r1
= range_int (70, 80);
2124 r2
= build_range3 (10, 20, 30, 40, 50, 60);
2125 r2
.union_ (range_int (70, 80));
2126 ASSERT_TRUE (r0
== r2
);
2128 // [10,20][30,40][50,60] U [6,35] => [6,40][50,60].
2129 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2130 r1
= range_int (6, 35);
2132 r1
= range_int (6, 40);
2133 r1
.union_ (range_int (50, 60));
2134 ASSERT_TRUE (r0
== r1
);
2136 // [10,20][30,40][50,60] U [6,60] => [6,60].
2137 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2138 r1
= range_int (6, 60);
2140 ASSERT_TRUE (r0
== range_int (6, 60));
2142 // [10,20][30,40][50,60] U [6,70] => [6,70].
2143 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2144 r1
= range_int (6, 70);
2146 ASSERT_TRUE (r0
== range_int (6, 70));
2148 // [10,20][30,40][50,60] U [35,70] => [10,20][30,70].
2149 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2150 r1
= range_int (35, 70);
2152 r1
= range_int (10, 20);
2153 r1
.union_ (range_int (30, 70));
2154 ASSERT_TRUE (r0
== r1
);
2156 // [10,20][30,40][50,60] U [15,35] => [10,40][50,60].
2157 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2158 r1
= range_int (15, 35);
2160 r1
= range_int (10, 40);
2161 r1
.union_ (range_int (50, 60));
2162 ASSERT_TRUE (r0
== r1
);
2164 // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60].
2165 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2166 r1
= range_int (35, 35);
2168 ASSERT_TRUE (r0
== build_range3 (10, 20, 30, 40, 50, 60));
2172 range_tests_int_range_max ()
2175 unsigned int nrange
;
2177 // Build a huge multi-range range.
2178 for (nrange
= 0; nrange
< 50; ++nrange
)
2180 int_range
<1> tmp
= range_int (nrange
*10, nrange
*10 + 5);
2183 ASSERT_TRUE (big
.num_pairs () == nrange
);
2185 // Verify that we can copy it without loosing precision.
2186 int_range_max
copy (big
);
2187 ASSERT_TRUE (copy
.num_pairs () == nrange
);
2189 // Inverting it should produce one more sub-range.
2191 ASSERT_TRUE (big
.num_pairs () == nrange
+ 1);
2193 int_range
<1> tmp
= range_int (5, 37);
2194 big
.intersect (tmp
);
2195 ASSERT_TRUE (big
.num_pairs () == 4);
2197 // Test that [10,10][20,20] does NOT contain 15.
2199 int_range_max i1
= range_int (10, 10);
2200 int_range_max i2
= range_int (20, 20);
2202 ASSERT_FALSE (i1
.contains_p (INT (15)));
2206 // Simulate -fstrict-enums where the domain of a type is less than the
2210 range_tests_strict_enum ()
2212 // The enum can only hold [0, 3].
2213 tree rtype
= copy_node (unsigned_type_node
);
2214 TYPE_MIN_VALUE (rtype
) = build_int_cstu (rtype
, 0);
2215 TYPE_MAX_VALUE (rtype
) = build_int_cstu (rtype
, 3);
2217 // Test that even though vr1 covers the strict enum domain ([0, 3]),
2218 // it does not cover the domain of the underlying type.
2219 int_range
<1> vr1
= range (rtype
, 0, 1);
2220 int_range
<1> vr2
= range (rtype
, 2, 3);
2222 ASSERT_TRUE (vr1
== range (rtype
, 0, 3));
2223 ASSERT_FALSE (vr1
.varying_p ());
2225 // Test that copying to a multi-range does not change things.
2226 int_range
<2> ir1 (vr1
);
2227 ASSERT_TRUE (ir1
== vr1
);
2228 ASSERT_FALSE (ir1
.varying_p ());
2230 // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3].
2231 vr1
= int_range
<2> (rtype
,
2232 wi::to_wide (TYPE_MIN_VALUE (rtype
)),
2233 wi::to_wide (TYPE_MAX_VALUE (rtype
)));
2235 ASSERT_TRUE (ir1
== vr1
);
2236 ASSERT_FALSE (ir1
.varying_p ());
2242 tree u128_type
= build_nonstandard_integer_type (128, /*unsigned=*/1);
2243 int_range
<2> i1
, i2
, i3
;
2244 int_range
<2> r0
, r1
, rold
;
2246 // Test 1-bit signed integer union.
2247 // [-1,-1] U [0,0] = VARYING.
2248 tree one_bit_type
= build_nonstandard_integer_type (1, 0);
2249 wide_int one_bit_min
= irange_val_min (one_bit_type
);
2250 wide_int one_bit_max
= irange_val_max (one_bit_type
);
2252 int_range
<2> min
= int_range
<2> (one_bit_type
, one_bit_min
, one_bit_min
);
2253 int_range
<2> max
= int_range
<2> (one_bit_type
, one_bit_max
, one_bit_max
);
2255 ASSERT_TRUE (max
.varying_p ());
2257 // Test that we can set a range of true+false for a 1-bit signed int.
2258 r0
= range_true_and_false (one_bit_type
);
2260 // Test inversion of 1-bit signed integers.
2262 int_range
<2> min
= int_range
<2> (one_bit_type
, one_bit_min
, one_bit_min
);
2263 int_range
<2> max
= int_range
<2> (one_bit_type
, one_bit_max
, one_bit_max
);
2267 ASSERT_TRUE (t
== max
);
2270 ASSERT_TRUE (t
== min
);
2273 // Test that NOT(255) is [0..254] in 8-bit land.
2274 int_range
<1> not_255
= range_uchar (255, 255, VR_ANTI_RANGE
);
2275 ASSERT_TRUE (not_255
== range_uchar (0, 254));
2277 // Test that NOT(0) is [1..255] in 8-bit land.
2278 int_range
<2> not_zero
= range_nonzero (unsigned_char_type_node
);
2279 ASSERT_TRUE (not_zero
== range_uchar (1, 255));
2281 // Check that [0,127][0x..ffffff80,0x..ffffff]
2282 // => ~[128, 0x..ffffff7f].
2283 r0
= range_uint128 (0, 127);
2284 wide_int high
= wi::minus_one (128);
2285 // low = -1 - 127 => 0x..ffffff80.
2286 wide_int low
= wi::sub (high
, wi::uhwi (127, 128));
2287 r1
= int_range
<1> (u128_type
, low
, high
); // [0x..ffffff80, 0x..ffffffff]
2288 // r0 = [0,127][0x..ffffff80,0x..fffffff].
2290 // r1 = [128, 0x..ffffff7f].
2291 r1
= int_range
<1> (u128_type
,
2292 wi::uhwi (128, 128),
2293 wi::sub (wi::minus_one (128), wi::uhwi (128, 128)));
2295 ASSERT_TRUE (r0
== r1
);
2297 r0
.set_varying (integer_type_node
);
2298 wide_int minint
= r0
.lower_bound ();
2299 wide_int maxint
= r0
.upper_bound ();
2301 r0
.set_varying (short_integer_type_node
);
2303 r0
.set_varying (unsigned_type_node
);
2304 wide_int maxuint
= r0
.upper_bound ();
2306 // Check that ~[0,5] => [6,MAX] for unsigned int.
2307 r0
= range_uint (0, 5);
2309 ASSERT_TRUE (r0
== int_range
<1> (unsigned_type_node
,
2310 wi::uhwi (6, TYPE_PRECISION (unsigned_type_node
)),
2313 // Check that ~[10,MAX] => [0,9] for unsigned int.
2314 r0
= int_range
<1> (unsigned_type_node
,
2315 wi::uhwi (10, TYPE_PRECISION (unsigned_type_node
)),
2318 ASSERT_TRUE (r0
== range_uint (0, 9));
2320 // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers.
2321 r0
= range_uint128 (0, 5, VR_ANTI_RANGE
);
2322 r1
= int_range
<1> (u128_type
, wi::uhwi (6, 128), wi::minus_one (128));
2323 ASSERT_TRUE (r0
== r1
);
2325 // Check that [~5] is really [-MIN,4][6,MAX].
2326 r0
= range_int (5, 5, VR_ANTI_RANGE
);
2327 r1
= int_range
<1> (integer_type_node
, minint
, INT (4));
2328 r1
.union_ (int_range
<1> (integer_type_node
, INT (6), maxint
));
2329 ASSERT_FALSE (r1
.undefined_p ());
2330 ASSERT_TRUE (r0
== r1
);
2332 r1
= range_int (5, 5);
2333 int_range
<2> r2 (r1
);
2334 ASSERT_TRUE (r1
== r2
);
2336 r1
= range_int (5, 10);
2338 r1
= range_int (5, 10);
2339 ASSERT_TRUE (r1
.contains_p (INT (7)));
2341 r1
= range_char (0, 20);
2342 ASSERT_TRUE (r1
.contains_p (SCHAR(15)));
2343 ASSERT_FALSE (r1
.contains_p (SCHAR(300)));
2345 // NOT([10,20]) ==> [-MIN,9][21,MAX].
2346 r0
= r1
= range_int (10, 20);
2347 r2
= int_range
<1> (integer_type_node
, minint
, INT(9));
2348 r2
.union_ (int_range
<1> (integer_type_node
, INT(21), maxint
));
2349 ASSERT_FALSE (r2
.undefined_p ());
2351 ASSERT_TRUE (r1
== r2
);
2352 // Test that NOT(NOT(x)) == x.
2354 ASSERT_TRUE (r0
== r2
);
2356 // Test that booleans and their inverse work as expected.
2357 r0
= range_zero (boolean_type_node
);
2358 ASSERT_TRUE (r0
== range_false ());
2360 ASSERT_TRUE (r0
== range_true ());
2362 // Make sure NULL and non-NULL of pointer types work, and that
2363 // inverses of them are consistent.
2364 tree voidp
= build_pointer_type (void_type_node
);
2365 r0
= range_zero (voidp
);
2369 ASSERT_TRUE (r0
== r1
);
2371 // [10,20] U [15, 30] => [10, 30].
2372 r0
= range_int (10, 20);
2373 r1
= range_int (15, 30);
2375 ASSERT_TRUE (r0
== range_int (10, 30));
2377 // [15,40] U [] => [15,40].
2378 r0
= range_int (15, 40);
2379 r1
.set_undefined ();
2381 ASSERT_TRUE (r0
== range_int (15, 40));
2383 // [10,20] U [10,10] => [10,20].
2384 r0
= range_int (10, 20);
2385 r1
= range_int (10, 10);
2387 ASSERT_TRUE (r0
== range_int (10, 20));
2389 // [10,20] U [9,9] => [9,20].
2390 r0
= range_int (10, 20);
2391 r1
= range_int (9, 9);
2393 ASSERT_TRUE (r0
== range_int (9, 20));
2395 // [10,20] ^ [15,30] => [15,20].
2396 r0
= range_int (10, 20);
2397 r1
= range_int (15, 30);
2399 ASSERT_TRUE (r0
== range_int (15, 20));
2401 // Test the internal sanity of wide_int's wrt HWIs.
2402 ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node
),
2403 TYPE_SIGN (boolean_type_node
))
2404 == wi::uhwi (1, TYPE_PRECISION (boolean_type_node
)));
2407 r0
= range_int (0, 0);
2408 ASSERT_TRUE (r0
.zero_p ());
2410 // Test nonzero_p().
2411 r0
= range_int (0, 0);
2413 ASSERT_TRUE (r0
.nonzero_p ());
2416 r0
= range_int (1, 1, VR_ANTI_RANGE
);
2418 r1
= range_int (3, 3, VR_ANTI_RANGE
);
2420 // vv = [0,0][2,2][4, MAX]
2421 int_range
<3> vv
= r0
;
2424 ASSERT_TRUE (vv
.contains_p (UINT (2)));
2425 ASSERT_TRUE (vv
.num_pairs () == 3);
2427 r0
= range_int (1, 1);
2428 // And union it with [0,0][2,2][4,MAX] multi range
2430 // The result should be [0,2][4,MAX], or ~[3,3] but it must contain 2
2431 ASSERT_TRUE (r0
.contains_p (INT (2)));
2435 range_tests_nonzero_bits ()
2437 int_range
<2> r0
, r1
;
2439 // Adding nonzero bits to a varying drops the varying.
2440 r0
.set_varying (integer_type_node
);
2441 r0
.set_nonzero_bits (INT (255));
2442 ASSERT_TRUE (!r0
.varying_p ());
2443 // Dropping the nonzero bits brings us back to varying.
2444 r0
.set_nonzero_bits (INT (-1));
2445 ASSERT_TRUE (r0
.varying_p ());
2447 // Test contains_p with nonzero bits.
2448 r0
.set_zero (integer_type_node
);
2449 ASSERT_TRUE (r0
.contains_p (INT (0)));
2450 ASSERT_FALSE (r0
.contains_p (INT (1)));
2451 r0
.set_nonzero_bits (INT (0xfe));
2452 ASSERT_FALSE (r0
.contains_p (INT (0x100)));
2453 ASSERT_FALSE (r0
.contains_p (INT (0x3)));
2455 // Union of nonzero bits.
2456 r0
.set_varying (integer_type_node
);
2457 r0
.set_nonzero_bits (INT (0xf0));
2458 r1
.set_varying (integer_type_node
);
2459 r1
.set_nonzero_bits (INT (0xf));
2461 ASSERT_TRUE (r0
.get_nonzero_bits () == 0xff);
2463 // Intersect of nonzero bits.
2464 r0
= range_int (0, 255);
2465 r0
.set_nonzero_bits (INT (0xfe));
2466 r1
.set_varying (integer_type_node
);
2467 r1
.set_nonzero_bits (INT (0xf0));
2469 ASSERT_TRUE (r0
.get_nonzero_bits () == 0xf0);
2471 // Intersect where the mask of nonzero bits is implicit from the range.
2472 r0
.set_varying (integer_type_node
);
2473 r1
= range_int (0, 255);
2475 ASSERT_TRUE (r0
.get_nonzero_bits () == 0xff);
2477 // The union of a mask of 0xff..ffff00 with a mask of 0xff spans the
2478 // entire domain, and makes the range a varying.
2479 r0
.set_varying (integer_type_node
);
2480 wide_int x
= wi::shwi (0xff, TYPE_PRECISION (integer_type_node
));
2481 x
= wi::bit_not (x
);
2482 r0
.set_nonzero_bits (x
); // 0xff..ff00
2483 r1
.set_varying (integer_type_node
);
2484 r1
.set_nonzero_bits (INT (0xff));
2486 ASSERT_TRUE (r0
.varying_p ());
2488 // Test that setting a nonzero bit of 1 does not pessimize the range.
2489 r0
.set_zero (integer_type_node
);
2490 r0
.set_nonzero_bits (INT (1));
2491 ASSERT_TRUE (r0
.zero_p ());
2494 // Build an frange from string endpoints.
2496 static inline frange
2497 frange_float (const char *lb
, const char *ub
, tree type
= float_type_node
)
2499 REAL_VALUE_TYPE min
, max
;
2500 gcc_assert (real_from_string (&min
, lb
) == 0);
2501 gcc_assert (real_from_string (&max
, ub
) == 0);
2502 return frange (type
, min
, max
);
2509 REAL_VALUE_TYPE q
, r
;
2512 // Equal ranges but with differing NAN bits are not equal.
2513 if (HONOR_NANS (float_type_node
))
2515 r1
= frange_float ("10", "12");
2523 // [10, 20] NAN ^ [30, 40] NAN = NAN.
2524 r0
= frange_float ("10", "20");
2525 r1
= frange_float ("30", "40");
2527 ASSERT_TRUE (r0
.known_isnan ());
2529 // [3,5] U [5,10] NAN = ... NAN
2530 r0
= frange_float ("3", "5");
2532 r1
= frange_float ("5", "10");
2534 ASSERT_TRUE (r0
.maybe_isnan ());
2537 // [5,6] U NAN = [5,6] NAN.
2538 r0
= frange_float ("5", "6");
2540 r1
.set_nan (float_type_node
);
2542 real_from_string (&q
, "5");
2543 real_from_string (&r
, "6");
2544 ASSERT_TRUE (real_identical (&q
, &r0
.lower_bound ()));
2545 ASSERT_TRUE (real_identical (&r
, &r0
.upper_bound ()));
2546 ASSERT_TRUE (r0
.maybe_isnan ());
2549 r0
.set_nan (float_type_node
);
2550 r1
.set_nan (float_type_node
);
2552 ASSERT_TRUE (r0
.known_isnan ());
2554 // [INF, INF] NAN ^ NAN = NAN
2555 r0
.set_nan (float_type_node
);
2556 r1
= frange_float ("+Inf", "+Inf");
2557 if (!HONOR_NANS (float_type_node
))
2560 ASSERT_TRUE (r0
.known_isnan ());
2563 r0
.set_nan (float_type_node
);
2564 r1
.set_nan (float_type_node
);
2566 ASSERT_TRUE (r0
.known_isnan ());
2568 // +NAN ^ -NAN = UNDEFINED
2569 r0
.set_nan (float_type_node
, false);
2570 r1
.set_nan (float_type_node
, true);
2572 ASSERT_TRUE (r0
.undefined_p ());
2574 // VARYING ^ NAN = NAN.
2575 r0
.set_nan (float_type_node
);
2576 r1
.set_varying (float_type_node
);
2578 ASSERT_TRUE (r0
.known_isnan ());
2580 // [3,4] ^ NAN = UNDEFINED.
2581 r0
= frange_float ("3", "4");
2583 r1
.set_nan (float_type_node
);
2585 ASSERT_TRUE (r0
.undefined_p ());
2587 // [-3, 5] ^ NAN = UNDEFINED
2588 r0
= frange_float ("-3", "5");
2590 r1
.set_nan (float_type_node
);
2592 ASSERT_TRUE (r0
.undefined_p ());
2594 // Setting the NAN bit to yes does not make us a known NAN.
2595 r0
.set_varying (float_type_node
);
2597 ASSERT_FALSE (r0
.known_isnan ());
2599 // NAN is in a VARYING.
2600 r0
.set_varying (float_type_node
);
2601 real_nan (&r
, "", 1, TYPE_MODE (float_type_node
));
2602 REAL_VALUE_TYPE nan
= r
;
2603 ASSERT_TRUE (r0
.contains_p (nan
));
2605 // -NAN is in a VARYING.
2606 r0
.set_varying (float_type_node
);
2607 q
= real_value_negate (&r
);
2608 REAL_VALUE_TYPE neg_nan
= q
;
2609 ASSERT_TRUE (r0
.contains_p (neg_nan
));
2611 // Clearing the NAN on a [] NAN is the empty set.
2612 r0
.set_nan (float_type_node
);
2614 ASSERT_TRUE (r0
.undefined_p ());
2616 // [10,20] NAN ^ [21,25] NAN = [NAN]
2617 r0
= frange_float ("10", "20");
2619 r1
= frange_float ("21", "25");
2622 ASSERT_TRUE (r0
.known_isnan ());
2624 // NAN U [5,6] should be [5,6] +-NAN.
2625 r0
.set_nan (float_type_node
);
2626 r1
= frange_float ("5", "6");
2629 real_from_string (&q
, "5");
2630 real_from_string (&r
, "6");
2631 ASSERT_TRUE (real_identical (&q
, &r0
.lower_bound ()));
2632 ASSERT_TRUE (real_identical (&r
, &r0
.upper_bound ()));
2633 ASSERT_TRUE (!r0
.signbit_p (signbit
));
2634 ASSERT_TRUE (r0
.maybe_isnan ());
2638 range_tests_signed_zeros ()
2640 REAL_VALUE_TYPE zero
= dconst0
;
2641 REAL_VALUE_TYPE neg_zero
= zero
;
2646 // [0,0] contains [0,0] but not [-0,-0] and vice versa.
2647 r0
= frange_float ("0.0", "0.0");
2648 r1
= frange_float ("-0.0", "-0.0");
2649 ASSERT_TRUE (r0
.contains_p (zero
));
2650 ASSERT_TRUE (!r0
.contains_p (neg_zero
));
2651 ASSERT_TRUE (r1
.contains_p (neg_zero
));
2652 ASSERT_TRUE (!r1
.contains_p (zero
));
2654 // Test contains_p() when we know the sign of the zero.
2655 r0
= frange_float ("0.0", "0.0");
2656 ASSERT_TRUE (r0
.contains_p (zero
));
2657 ASSERT_FALSE (r0
.contains_p (neg_zero
));
2658 r0
= frange_float ("-0.0", "-0.0");
2659 ASSERT_TRUE (r0
.contains_p (neg_zero
));
2660 ASSERT_FALSE (r0
.contains_p (zero
));
2662 r0
= frange_float ("-0.0", "0.0");
2663 ASSERT_TRUE (r0
.contains_p (neg_zero
));
2664 ASSERT_TRUE (r0
.contains_p (zero
));
2666 r0
= frange_float ("-3", "5");
2667 ASSERT_TRUE (r0
.contains_p (neg_zero
));
2668 ASSERT_TRUE (r0
.contains_p (zero
));
2670 // The intersection of zeros that differ in sign is a NAN (or
2671 // undefined if not honoring NANs).
2672 r0
= frange_float ("-0.0", "-0.0");
2673 r1
= frange_float ("0.0", "0.0");
2675 if (HONOR_NANS (float_type_node
))
2676 ASSERT_TRUE (r0
.known_isnan ());
2678 ASSERT_TRUE (r0
.undefined_p ());
2680 // The union of zeros that differ in sign is a zero with unknown sign.
2681 r0
= frange_float ("0.0", "0.0");
2682 r1
= frange_float ("-0.0", "-0.0");
2684 ASSERT_TRUE (r0
.zero_p () && !r0
.signbit_p (signbit
));
2686 // [-0, +0] has an unknown sign.
2687 r0
= frange_float ("-0.0", "0.0");
2688 ASSERT_TRUE (r0
.zero_p () && !r0
.signbit_p (signbit
));
2690 // [-0, +0] ^ [0, 0] is [0, 0]
2691 r0
= frange_float ("-0.0", "0.0");
2692 r1
= frange_float ("0.0", "0.0");
2694 ASSERT_TRUE (r0
.zero_p ());
2696 r0
= frange_float ("+0", "5");
2698 ASSERT_TRUE (r0
.signbit_p (signbit
) && !signbit
);
2700 r0
= frange_float ("-0", "5");
2702 ASSERT_TRUE (!r0
.signbit_p (signbit
));
2704 r0
= frange_float ("-0", "10");
2705 r1
= frange_float ("0", "5");
2707 ASSERT_TRUE (real_iszero (&r0
.lower_bound (), false));
2709 r0
= frange_float ("-0", "5");
2710 r1
= frange_float ("0", "5");
2712 ASSERT_TRUE (real_iszero (&r0
.lower_bound (), true));
2714 r0
= frange_float ("-5", "-0");
2716 r1
= frange_float ("0", "0");
2719 if (HONOR_NANS (float_type_node
))
2720 ASSERT_TRUE (r0
.known_isnan ());
2722 ASSERT_TRUE (r0
.undefined_p ());
2724 r0
.set_nonnegative (float_type_node
);
2725 if (HONOR_NANS (float_type_node
))
2726 ASSERT_TRUE (r0
.maybe_isnan ());
2728 // Numbers containing zero should have an unknown SIGNBIT.
2729 r0
= frange_float ("0", "10");
2731 ASSERT_TRUE (r0
.signbit_p (signbit
) && !signbit
);
2735 range_tests_signbit ()
2740 // Negative numbers should have the SIGNBIT set.
2741 r0
= frange_float ("-5", "-1");
2743 ASSERT_TRUE (r0
.signbit_p (signbit
) && signbit
);
2744 // Positive numbers should have the SIGNBIT clear.
2745 r0
= frange_float ("1", "10");
2747 ASSERT_TRUE (r0
.signbit_p (signbit
) && !signbit
);
2748 // Numbers spanning both positive and negative should have an
2750 r0
= frange_float ("-10", "10");
2752 ASSERT_TRUE (!r0
.signbit_p (signbit
));
2753 r0
.set_varying (float_type_node
);
2754 ASSERT_TRUE (!r0
.signbit_p (signbit
));
2758 range_tests_floats ()
2762 if (HONOR_NANS (float_type_node
))
2764 range_tests_signbit ();
2766 if (HONOR_SIGNED_ZEROS (float_type_node
))
2767 range_tests_signed_zeros ();
2769 // A range of [-INF,+INF] is actually VARYING if no other properties
2771 r0
= frange_float ("-Inf", "+Inf");
2772 ASSERT_TRUE (r0
.varying_p ());
2773 // ...unless it has some special property...
2774 if (HONOR_NANS (r0
.type ()))
2777 ASSERT_FALSE (r0
.varying_p ());
2780 // For most architectures, where float and double are different
2781 // sizes, having the same endpoints does not necessarily mean the
2782 // ranges are equal.
2783 if (!types_compatible_p (float_type_node
, double_type_node
))
2785 r0
= frange_float ("3.0", "3.0", float_type_node
);
2786 r1
= frange_float ("3.0", "3.0", double_type_node
);
2790 // [3,5] U [10,12] = [3,12].
2791 r0
= frange_float ("3", "5");
2792 r1
= frange_float ("10", "12");
2794 ASSERT_EQ (r0
, frange_float ("3", "12"));
2796 // [5,10] U [4,8] = [4,10]
2797 r0
= frange_float ("5", "10");
2798 r1
= frange_float ("4", "8");
2800 ASSERT_EQ (r0
, frange_float ("4", "10"));
2802 // [3,5] U [4,10] = [3,10]
2803 r0
= frange_float ("3", "5");
2804 r1
= frange_float ("4", "10");
2806 ASSERT_EQ (r0
, frange_float ("3", "10"));
2808 // [4,10] U [5,11] = [4,11]
2809 r0
= frange_float ("4", "10");
2810 r1
= frange_float ("5", "11");
2812 ASSERT_EQ (r0
, frange_float ("4", "11"));
2814 // [3,12] ^ [10,12] = [10,12].
2815 r0
= frange_float ("3", "12");
2816 r1
= frange_float ("10", "12");
2818 ASSERT_EQ (r0
, frange_float ("10", "12"));
2820 // [10,12] ^ [11,11] = [11,11]
2821 r0
= frange_float ("10", "12");
2822 r1
= frange_float ("11", "11");
2824 ASSERT_EQ (r0
, frange_float ("11", "11"));
2826 // [10,20] ^ [5,15] = [10,15]
2827 r0
= frange_float ("10", "20");
2828 r1
= frange_float ("5", "15");
2830 ASSERT_EQ (r0
, frange_float ("10", "15"));
2832 // [10,20] ^ [15,25] = [15,20]
2833 r0
= frange_float ("10", "20");
2834 r1
= frange_float ("15", "25");
2836 ASSERT_EQ (r0
, frange_float ("15", "20"));
2838 // [10,20] ^ [21,25] = []
2839 r0
= frange_float ("10", "20");
2841 r1
= frange_float ("21", "25");
2844 ASSERT_TRUE (r0
.undefined_p ());
2846 if (HONOR_INFINITIES (float_type_node
))
2848 // Make sure [-Inf, -Inf] doesn't get normalized.
2849 r0
= frange_float ("-Inf", "-Inf");
2850 ASSERT_TRUE (real_isinf (&r0
.lower_bound (), true));
2851 ASSERT_TRUE (real_isinf (&r0
.upper_bound (), true));
2854 // Test that reading back a global range yields the same result as
2855 // what we wrote into it.
2856 tree ssa
= make_temp_ssa_name (float_type_node
, NULL
, "blah");
2857 r0
.set_varying (float_type_node
);
2859 set_range_info (ssa
, r0
);
2860 get_global_range_query ()->range_of_expr (r1
, ssa
);
2864 // Run floating range tests for various combinations of NAN and INF
2868 range_tests_floats_various ()
2870 int save_finite_math_only
= flag_finite_math_only
;
2872 // Test -ffinite-math-only.
2873 flag_finite_math_only
= 1;
2874 range_tests_floats ();
2875 // Test -fno-finite-math-only.
2876 flag_finite_math_only
= 0;
2877 range_tests_floats ();
2879 flag_finite_math_only
= save_finite_math_only
;
2885 range_tests_irange3 ();
2886 range_tests_int_range_max ();
2887 range_tests_strict_enum ();
2888 range_tests_nonzero_bits ();
2889 range_tests_floats_various ();
2890 range_tests_misc ();
2893 } // namespace selftest
2895 #endif // CHECKING_P