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");
83 debug (const irange_bitmask
&bm
)
86 fprintf (stderr
, "\n");
89 // Default vrange definitions.
92 vrange::contains_p (tree
) const
98 vrange::singleton_p (tree
*) const
104 vrange::set (tree min
, tree
, value_range_kind
)
106 set_varying (TREE_TYPE (min
));
110 vrange::type () const
112 return void_type_node
;
116 vrange::supports_type_p (const_tree
) const
122 vrange::set_undefined ()
124 m_kind
= VR_UNDEFINED
;
128 vrange::set_varying (tree
)
134 vrange::union_ (const vrange
&r
)
136 if (r
.undefined_p () || varying_p ())
138 if (undefined_p () || r
.varying_p ())
148 vrange::intersect (const vrange
&r
)
150 if (undefined_p () || r
.varying_p ())
152 if (r
.undefined_p ())
167 vrange::zero_p () const
173 vrange::nonzero_p () const
179 vrange::set_nonzero (tree type
)
185 vrange::set_zero (tree type
)
191 vrange::set_nonnegative (tree type
)
197 vrange::fits_p (const vrange
&) const
202 // Assignment operator for generic ranges. Copying incompatible types
206 vrange::operator= (const vrange
&src
)
208 if (is_a
<irange
> (src
))
209 as_a
<irange
> (*this) = as_a
<irange
> (src
);
210 else if (is_a
<frange
> (src
))
211 as_a
<frange
> (*this) = as_a
<frange
> (src
);
214 gcc_checking_assert (is_a
<unsupported_range
> (src
));
220 // Equality operator for generic ranges.
223 vrange::operator== (const vrange
&src
) const
225 if (is_a
<irange
> (src
))
226 return as_a
<irange
> (*this) == as_a
<irange
> (src
);
227 if (is_a
<frange
> (src
))
228 return as_a
<frange
> (*this) == as_a
<frange
> (src
);
232 // Wrapper for vrange_printer to dump a range to a file.
235 vrange::dump (FILE *file
) const
237 pretty_printer buffer
;
238 pp_needs_newline (&buffer
) = true;
239 buffer
.buffer
->stream
= file
;
240 vrange_printer
vrange_pp (&buffer
);
241 this->accept (vrange_pp
);
246 irange_bitmask::dump (FILE *file
) const
248 char buf
[WIDE_INT_PRINT_BUFFER_SIZE
], *p
;
249 pretty_printer buffer
;
251 pp_needs_newline (&buffer
) = true;
252 buffer
.buffer
->stream
= file
;
253 pp_string (&buffer
, "MASK ");
254 unsigned len_mask
, len_val
;
255 if (print_hex_buf_size (m_mask
, &len_mask
)
256 | print_hex_buf_size (m_value
, &len_val
))
257 p
= XALLOCAVEC (char, MAX (len_mask
, len_val
));
260 print_hex (m_mask
, p
);
261 pp_string (&buffer
, p
);
262 pp_string (&buffer
, " VALUE ");
263 print_hex (m_value
, p
);
264 pp_string (&buffer
, p
);
272 add_vrange (const vrange
&v
, inchash::hash
&hstate
,
275 if (v
.undefined_p ())
277 hstate
.add_int (VR_UNDEFINED
);
280 // Types are ignored throughout to inhibit two ranges being equal
281 // but having different hash values. This can happen when two
282 // ranges are equal and their types are different (but
283 // types_compatible_p is true).
284 if (is_a
<irange
> (v
))
286 const irange
&r
= as_a
<irange
> (v
);
288 hstate
.add_int (VR_VARYING
);
290 hstate
.add_int (VR_RANGE
);
291 for (unsigned i
= 0; i
< r
.num_pairs (); ++i
)
293 hstate
.add_wide_int (r
.lower_bound (i
));
294 hstate
.add_wide_int (r
.upper_bound (i
));
296 irange_bitmask bm
= r
.get_bitmask ();
297 hstate
.add_wide_int (bm
.value ());
298 hstate
.add_wide_int (bm
.mask ());
301 if (is_a
<frange
> (v
))
303 const frange
&r
= as_a
<frange
> (v
);
304 if (r
.known_isnan ())
305 hstate
.add_int (VR_NAN
);
308 hstate
.add_int (r
.varying_p () ? VR_VARYING
: VR_RANGE
);
309 hstate
.add_real_value (r
.lower_bound ());
310 hstate
.add_real_value (r
.upper_bound ());
312 nan_state nan
= r
.get_nan_state ();
313 hstate
.add_int (nan
.pos_p ());
314 hstate
.add_int (nan
.neg_p ());
320 } //namespace inchash
323 irange::nonnegative_p () const
325 return wi::ge_p (lower_bound (), 0, TYPE_SIGN (type ()));
329 irange::nonpositive_p () const
331 return wi::le_p (upper_bound (), 0, TYPE_SIGN (type ()));
335 irange::supports_type_p (const_tree type
) const
337 return supports_p (type
);
340 // Return TRUE if R fits in THIS.
343 irange::fits_p (const vrange
&r
) const
345 return m_max_ranges
>= as_a
<irange
> (r
).num_pairs ();
349 irange::set_nonnegative (tree type
)
352 wi::zero (TYPE_PRECISION (type
)),
353 wi::to_wide (TYPE_MAX_VALUE (type
)));
357 frange::accept (const vrange_visitor
&v
) const
362 // Flush denormal endpoints to the appropriate 0.0.
365 frange::flush_denormals_to_zero ()
367 if (undefined_p () || known_isnan ())
370 machine_mode mode
= TYPE_MODE (type ());
371 // Flush [x, -DENORMAL] to [x, -0.0].
372 if (real_isdenormal (&m_max
, mode
) && real_isneg (&m_max
))
374 if (HONOR_SIGNED_ZEROS (m_type
))
379 // Flush [+DENORMAL, x] to [+0.0, x].
380 if (real_isdenormal (&m_min
, mode
) && !real_isneg (&m_min
))
384 // Setter for franges.
387 frange::set (tree type
,
388 const REAL_VALUE_TYPE
&min
, const REAL_VALUE_TYPE
&max
,
389 const nan_state
&nan
, value_range_kind kind
)
406 gcc_checking_assert (!real_isnan (&min
) && !real_isnan (&max
));
412 if (HONOR_NANS (m_type
))
414 m_pos_nan
= nan
.pos_p ();
415 m_neg_nan
= nan
.neg_p ();
423 if (!MODE_HAS_SIGNED_ZEROS (TYPE_MODE (m_type
)))
425 if (real_iszero (&m_min
, 1))
427 if (real_iszero (&m_max
, 1))
430 else if (!HONOR_SIGNED_ZEROS (m_type
))
432 if (real_iszero (&m_max
, 1))
434 if (real_iszero (&m_min
, 0))
438 // For -ffinite-math-only we can drop ranges outside the
439 // representable numbers to min/max for the type.
440 if (!HONOR_INFINITIES (m_type
))
442 REAL_VALUE_TYPE min_repr
= frange_val_min (m_type
);
443 REAL_VALUE_TYPE max_repr
= frange_val_max (m_type
);
444 if (real_less (&m_min
, &min_repr
))
446 else if (real_less (&max_repr
, &m_min
))
448 if (real_less (&max_repr
, &m_max
))
450 else if (real_less (&m_max
, &min_repr
))
454 // Check for swapped ranges.
455 gcc_checking_assert (real_compare (LE_EXPR
, &min
, &max
));
460 // Setter for an frange defaulting the NAN possibility to +-NAN when
464 frange::set (tree type
,
465 const REAL_VALUE_TYPE
&min
, const REAL_VALUE_TYPE
&max
,
466 value_range_kind kind
)
468 set (type
, min
, max
, nan_state (true), kind
);
472 frange::set (tree min
, tree max
, value_range_kind kind
)
474 set (TREE_TYPE (min
),
475 *TREE_REAL_CST_PTR (min
), *TREE_REAL_CST_PTR (max
), kind
);
478 // Normalize range to VARYING or UNDEFINED, or vice versa. Return
479 // TRUE if anything changed.
481 // A range with no known properties can be dropped to VARYING.
482 // Similarly, a VARYING with any properties should be dropped to a
483 // VR_RANGE. Normalizing ranges upon changing them ensures there is
484 // only one representation for a given range.
487 frange::normalize_kind ()
489 if (m_kind
== VR_RANGE
490 && frange_val_is_min (m_min
, m_type
)
491 && frange_val_is_max (m_max
, m_type
))
493 if (!HONOR_NANS (m_type
) || (m_pos_nan
&& m_neg_nan
))
495 set_varying (m_type
);
499 else if (m_kind
== VR_VARYING
)
501 if (HONOR_NANS (m_type
) && (!m_pos_nan
|| !m_neg_nan
))
504 m_min
= frange_val_min (m_type
);
505 m_max
= frange_val_max (m_type
);
511 else if (m_kind
== VR_NAN
&& !m_pos_nan
&& !m_neg_nan
)
516 // Union or intersect the zero endpoints of two ranges. For example:
517 // [-0, x] U [+0, x] => [-0, x]
518 // [ x, -0] U [ x, +0] => [ x, +0]
519 // [-0, x] ^ [+0, x] => [+0, x]
520 // [ x, -0] ^ [ x, +0] => [ x, -0]
522 // UNION_P is true when performing a union, or false when intersecting.
525 frange::combine_zeros (const frange
&r
, bool union_p
)
527 gcc_checking_assert (!undefined_p () && !known_isnan ());
529 bool changed
= false;
530 if (real_iszero (&m_min
) && real_iszero (&r
.m_min
)
531 && real_isneg (&m_min
) != real_isneg (&r
.m_min
))
533 m_min
.sign
= union_p
;
536 if (real_iszero (&m_max
) && real_iszero (&r
.m_max
)
537 && real_isneg (&m_max
) != real_isneg (&r
.m_max
))
539 m_max
.sign
= !union_p
;
542 // If the signs are swapped, the resulting range is empty.
543 if (m_min
.sign
== 0 && m_max
.sign
== 1)
554 // Union two ranges when one is known to be a NAN.
557 frange::union_nans (const frange
&r
)
559 gcc_checking_assert (known_isnan () || r
.known_isnan ());
561 bool changed
= false;
562 if (known_isnan () && m_kind
!= r
.m_kind
)
569 if (m_pos_nan
!= r
.m_pos_nan
|| m_neg_nan
!= r
.m_neg_nan
)
571 m_pos_nan
|= r
.m_pos_nan
;
572 m_neg_nan
|= r
.m_neg_nan
;
584 frange::union_ (const vrange
&v
)
586 const frange
&r
= as_a
<frange
> (v
);
588 if (r
.undefined_p () || varying_p ())
590 if (undefined_p () || r
.varying_p ())
597 if (known_isnan () || r
.known_isnan ())
598 return union_nans (r
);
599 bool changed
= false;
600 if (m_pos_nan
!= r
.m_pos_nan
|| m_neg_nan
!= r
.m_neg_nan
)
602 m_pos_nan
|= r
.m_pos_nan
;
603 m_neg_nan
|= r
.m_neg_nan
;
607 // Combine endpoints.
608 if (real_less (&r
.m_min
, &m_min
))
613 if (real_less (&m_max
, &r
.m_max
))
619 if (HONOR_SIGNED_ZEROS (m_type
))
620 changed
|= combine_zeros (r
, true);
622 changed
|= normalize_kind ();
626 // Intersect two ranges when one is known to be a NAN.
629 frange::intersect_nans (const frange
&r
)
631 gcc_checking_assert (known_isnan () || r
.known_isnan ());
633 m_pos_nan
&= r
.m_pos_nan
;
634 m_neg_nan
&= r
.m_neg_nan
;
645 frange::intersect (const vrange
&v
)
647 const frange
&r
= as_a
<frange
> (v
);
649 if (undefined_p () || r
.varying_p ())
651 if (r
.undefined_p ())
663 if (known_isnan () || r
.known_isnan ())
664 return intersect_nans (r
);
665 bool changed
= false;
666 if (m_pos_nan
!= r
.m_pos_nan
|| m_neg_nan
!= r
.m_neg_nan
)
668 m_pos_nan
&= r
.m_pos_nan
;
669 m_neg_nan
&= r
.m_neg_nan
;
673 // Combine endpoints.
674 if (real_less (&m_min
, &r
.m_min
))
679 if (real_less (&r
.m_max
, &m_max
))
684 // If the endpoints are swapped, the resulting range is empty.
685 if (real_less (&m_max
, &m_min
))
696 if (HONOR_SIGNED_ZEROS (m_type
))
697 changed
|= combine_zeros (r
, false);
699 changed
|= normalize_kind ();
704 frange::operator= (const frange
&src
)
710 m_pos_nan
= src
.m_pos_nan
;
711 m_neg_nan
= src
.m_neg_nan
;
719 frange::operator== (const frange
&src
) const
721 if (m_kind
== src
.m_kind
)
727 return types_compatible_p (m_type
, src
.m_type
);
729 bool nan1
= known_isnan ();
730 bool nan2
= src
.known_isnan ();
734 return (m_pos_nan
== src
.m_pos_nan
735 && m_neg_nan
== src
.m_neg_nan
);
739 return (real_identical (&m_min
, &src
.m_min
)
740 && real_identical (&m_max
, &src
.m_max
)
741 && m_pos_nan
== src
.m_pos_nan
742 && m_neg_nan
== src
.m_neg_nan
743 && types_compatible_p (m_type
, src
.m_type
));
748 // Return TRUE if range contains R.
751 frange::contains_p (const REAL_VALUE_TYPE
&r
) const
753 gcc_checking_assert (m_kind
!= VR_ANTI_RANGE
);
764 if (!m_pos_nan
&& !m_neg_nan
)
766 // Both +NAN and -NAN are present.
767 if (m_pos_nan
&& m_neg_nan
)
769 return m_neg_nan
== r
.sign
;
774 if (real_compare (GE_EXPR
, &r
, &m_min
) && real_compare (LE_EXPR
, &r
, &m_max
))
776 // Make sure the signs are equal for signed zeros.
777 if (HONOR_SIGNED_ZEROS (m_type
) && real_iszero (&r
))
778 return r
.sign
== m_min
.sign
|| r
.sign
== m_max
.sign
;
784 // If range is a singleton, place it in RESULT and return TRUE. If
785 // RESULT is NULL, just return TRUE.
787 // A NAN can never be a singleton.
790 frange::internal_singleton_p (REAL_VALUE_TYPE
*result
) const
792 if (m_kind
== VR_RANGE
&& real_identical (&m_min
, &m_max
))
794 // Return false for any singleton that may be a NAN.
795 if (HONOR_NANS (m_type
) && maybe_isnan ())
798 if (MODE_COMPOSITE_P (TYPE_MODE (m_type
)))
800 // For IBM long doubles, if the value is +-Inf or is exactly
801 // representable in double, the other double could be +0.0
802 // or -0.0. Since this means there is more than one way to
803 // represent a value, return false to avoid propagating it.
804 // See libgcc/config/rs6000/ibm-ldouble-format for details.
805 if (real_isinf (&m_min
))
808 real_convert (&r
, DFmode
, &m_min
);
809 if (real_identical (&r
, &m_min
))
821 frange::singleton_p (tree
*result
) const
823 if (internal_singleton_p ())
826 *result
= build_real (m_type
, m_min
);
833 frange::singleton_p (REAL_VALUE_TYPE
&r
) const
835 return internal_singleton_p (&r
);
839 frange::supports_type_p (const_tree type
) const
841 return supports_p (type
);
845 frange::verify_range ()
848 gcc_checking_assert (HONOR_NANS (m_type
) || !maybe_isnan ());
852 gcc_checking_assert (!m_type
);
855 gcc_checking_assert (m_type
);
856 gcc_checking_assert (frange_val_is_min (m_min
, m_type
));
857 gcc_checking_assert (frange_val_is_max (m_max
, m_type
));
858 if (HONOR_NANS (m_type
))
859 gcc_checking_assert (m_pos_nan
&& m_neg_nan
);
861 gcc_checking_assert (!m_pos_nan
&& !m_neg_nan
);
864 gcc_checking_assert (m_type
);
867 gcc_checking_assert (m_type
);
868 gcc_checking_assert (m_pos_nan
|| m_neg_nan
);
874 // NANs cannot appear in the endpoints of a range.
875 gcc_checking_assert (!real_isnan (&m_min
) && !real_isnan (&m_max
));
877 // Make sure we don't have swapped ranges.
878 gcc_checking_assert (!real_less (&m_max
, &m_min
));
880 // [ +0.0, -0.0 ] is nonsensical.
881 gcc_checking_assert (!(real_iszero (&m_min
, 0) && real_iszero (&m_max
, 1)));
883 // If all the properties are clear, we better not span the entire
884 // domain, because that would make us varying.
885 if (m_pos_nan
&& m_neg_nan
)
886 gcc_checking_assert (!frange_val_is_min (m_min
, m_type
)
887 || !frange_val_is_max (m_max
, m_type
));
890 // We can't do much with nonzeros yet.
892 frange::set_nonzero (tree type
)
897 // We can't do much with nonzeros yet.
899 frange::nonzero_p () const
904 // Set range to [+0.0, +0.0] if honoring signed zeros, or [0.0, 0.0]
908 frange::set_zero (tree type
)
910 if (HONOR_SIGNED_ZEROS (type
))
912 set (type
, dconstm0
, dconst0
);
916 set (type
, dconst0
, dconst0
);
919 // Return TRUE for any zero regardless of sign.
922 frange::zero_p () const
924 return (m_kind
== VR_RANGE
925 && real_iszero (&m_min
)
926 && real_iszero (&m_max
));
929 // Set the range to non-negative numbers, that is [+0.0, +INF].
931 // The NAN in the resulting range (if HONOR_NANS) has a varying sign
932 // as there are no guarantees in IEEE 754 wrt to the sign of a NAN,
933 // except for copy, abs, and copysign. It is the responsibility of
934 // the caller to set the NAN's sign if desired.
937 frange::set_nonnegative (tree type
)
939 set (type
, dconst0
, frange_val_max (type
));
942 // Here we copy between any two irange's.
945 irange::operator= (const irange
&src
)
947 int needed
= src
.num_pairs ();
948 maybe_resize (needed
);
951 unsigned lim
= src
.m_num_ranges
;
952 if (lim
> m_max_ranges
)
955 for (x
= 0; x
< lim
* 2; ++x
)
956 m_base
[x
] = src
.m_base
[x
];
958 // If the range didn't fit, the last range should cover the rest.
959 if (lim
!= src
.m_num_ranges
)
960 m_base
[x
- 1] = src
.m_base
[src
.m_num_ranges
* 2 - 1];
965 m_bitmask
= src
.m_bitmask
;
966 if (m_max_ranges
== 1)
974 get_legacy_range (const irange
&r
, tree
&min
, tree
&max
)
976 if (r
.undefined_p ())
983 tree type
= r
.type ();
986 min
= wide_int_to_tree (type
, r
.lower_bound ());
987 max
= wide_int_to_tree (type
, r
.upper_bound ());
991 unsigned int precision
= TYPE_PRECISION (type
);
992 signop sign
= TYPE_SIGN (type
);
993 if (r
.num_pairs () > 1
995 && r
.lower_bound () == wi::min_value (precision
, sign
)
996 && r
.upper_bound () == wi::max_value (precision
, sign
))
998 int_range
<3> inv (r
);
1000 min
= wide_int_to_tree (type
, inv
.lower_bound (0));
1001 max
= wide_int_to_tree (type
, inv
.upper_bound (0));
1002 return VR_ANTI_RANGE
;
1005 min
= wide_int_to_tree (type
, r
.lower_bound ());
1006 max
= wide_int_to_tree (type
, r
.upper_bound ());
1010 /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
1011 This means adjusting VRTYPE, MIN and MAX representing the case of a
1012 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
1013 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
1014 In corner cases where MAX+1 or MIN-1 wraps this will fall back
1016 This routine exists to ease canonicalization in the case where we
1017 extract ranges from var + CST op limit. */
1020 irange::set (tree type
, const wide_int
&min
, const wide_int
&max
,
1021 value_range_kind kind
)
1023 unsigned prec
= TYPE_PRECISION (type
);
1024 signop sign
= TYPE_SIGN (type
);
1025 wide_int min_value
= wi::min_value (prec
, sign
);
1026 wide_int max_value
= wi::max_value (prec
, sign
);
1029 m_bitmask
.set_unknown (prec
);
1031 if (kind
== VR_RANGE
)
1036 if (min
== min_value
&& max
== max_value
)
1037 m_kind
= VR_VARYING
;
1043 gcc_checking_assert (kind
== VR_ANTI_RANGE
);
1044 gcc_checking_assert (m_max_ranges
> 1);
1046 m_kind
= VR_UNDEFINED
;
1048 wi::overflow_type ovf
;
1051 lim
= wi::add (min
, -1, sign
, &ovf
);
1053 lim
= wi::sub (min
, 1, sign
, &ovf
);
1058 m_base
[0] = min_value
;
1063 lim
= wi::sub (max
, -1, sign
, &ovf
);
1065 lim
= wi::add (max
, 1, sign
, &ovf
);
1069 m_base
[m_num_ranges
* 2] = lim
;
1070 m_base
[m_num_ranges
* 2 + 1] = max_value
;
1080 irange::set (tree min
, tree max
, value_range_kind kind
)
1082 if (POLY_INT_CST_P (min
) || POLY_INT_CST_P (max
))
1084 set_varying (TREE_TYPE (min
));
1088 gcc_checking_assert (TREE_CODE (min
) == INTEGER_CST
);
1089 gcc_checking_assert (TREE_CODE (max
) == INTEGER_CST
);
1091 return set (TREE_TYPE (min
), wi::to_wide (min
), wi::to_wide (max
), kind
);
1094 // Check the validity of the range.
1097 irange::verify_range ()
1099 gcc_checking_assert (m_discriminator
== VR_IRANGE
);
1100 if (m_kind
== VR_UNDEFINED
)
1102 gcc_checking_assert (m_num_ranges
== 0);
1105 gcc_checking_assert (m_num_ranges
<= m_max_ranges
);
1107 // Legacy allowed these to represent VARYING for unknown types.
1108 // Leave this in for now, until all users are converted. Eventually
1109 // we should abort in set_varying.
1110 if (m_kind
== VR_VARYING
&& m_type
== error_mark_node
)
1113 unsigned prec
= TYPE_PRECISION (m_type
);
1114 if (m_kind
== VR_VARYING
)
1116 gcc_checking_assert (m_bitmask
.unknown_p ());
1117 gcc_checking_assert (m_num_ranges
== 1);
1118 gcc_checking_assert (varying_compatible_p ());
1119 gcc_checking_assert (lower_bound ().get_precision () == prec
);
1120 gcc_checking_assert (upper_bound ().get_precision () == prec
);
1123 gcc_checking_assert (m_num_ranges
!= 0);
1124 gcc_checking_assert (!varying_compatible_p ());
1125 for (unsigned i
= 0; i
< m_num_ranges
; ++i
)
1127 wide_int lb
= lower_bound (i
);
1128 wide_int ub
= upper_bound (i
);
1129 gcc_checking_assert (lb
.get_precision () == prec
);
1130 gcc_checking_assert (ub
.get_precision () == prec
);
1131 int c
= wi::cmp (lb
, ub
, TYPE_SIGN (m_type
));
1132 gcc_checking_assert (c
== 0 || c
== -1);
1134 m_bitmask
.verify_mask ();
1138 irange::operator== (const irange
&other
) const
1140 if (m_num_ranges
!= other
.m_num_ranges
)
1143 if (m_num_ranges
== 0)
1146 signop sign1
= TYPE_SIGN (type ());
1147 signop sign2
= TYPE_SIGN (other
.type ());
1149 for (unsigned i
= 0; i
< m_num_ranges
; ++i
)
1151 widest_int lb
= widest_int::from (lower_bound (i
), sign1
);
1152 widest_int ub
= widest_int::from (upper_bound (i
), sign1
);
1153 widest_int lb_other
= widest_int::from (other
.lower_bound (i
), sign2
);
1154 widest_int ub_other
= widest_int::from (other
.upper_bound (i
), sign2
);
1155 if (lb
!= lb_other
|| ub
!= ub_other
)
1159 irange_bitmask bm1
= get_bitmask ();
1160 irange_bitmask bm2
= other
.get_bitmask ();
1161 widest_int tmp1
= widest_int::from (bm1
.mask (), sign1
);
1162 widest_int tmp2
= widest_int::from (bm2
.mask (), sign2
);
1165 if (bm1
.unknown_p ())
1167 tmp1
= widest_int::from (bm1
.value (), sign1
);
1168 tmp2
= widest_int::from (bm2
.value (), sign2
);
1169 return tmp1
== tmp2
;
1172 /* If range is a singleton, place it in RESULT and return TRUE. */
1175 irange::singleton_p (tree
*result
) const
1177 if (num_pairs () == 1 && lower_bound () == upper_bound ())
1180 *result
= wide_int_to_tree (type (), lower_bound ());
1187 irange::singleton_p (wide_int
&w
) const
1189 if (num_pairs () == 1 && lower_bound () == upper_bound ())
1197 /* Return 1 if CST is inside value range.
1198 0 if CST is not inside value range.
1200 Benchmark compile/20001226-1.c compilation time after changing this
1204 irange::contains_p (const wide_int
&cst
) const
1209 // See if we can exclude CST based on the known 0 bits.
1210 if (!m_bitmask
.unknown_p ()
1212 && wi::bit_and (m_bitmask
.get_nonzero_bits (), cst
) == 0)
1215 signop sign
= TYPE_SIGN (type ());
1216 for (unsigned r
= 0; r
< m_num_ranges
; ++r
)
1218 if (wi::lt_p (cst
, lower_bound (r
), sign
))
1220 if (wi::le_p (cst
, upper_bound (r
), sign
))
1227 // Perform an efficient union with R when both ranges have only a single pair.
1228 // Excluded are VARYING and UNDEFINED ranges.
1231 irange::irange_single_pair_union (const irange
&r
)
1233 gcc_checking_assert (!undefined_p () && !varying_p ());
1234 gcc_checking_assert (!r
.undefined_p () && !varying_p ());
1236 signop sign
= TYPE_SIGN (m_type
);
1237 // Check if current lower bound is also the new lower bound.
1238 if (wi::le_p (m_base
[0], r
.m_base
[0], sign
))
1240 // If current upper bound is new upper bound, we're done.
1241 if (wi::le_p (r
.m_base
[1], m_base
[1], sign
))
1242 return union_bitmask (r
);
1243 // Otherwise R has the new upper bound.
1244 // Check for overlap/touching ranges, or single target range.
1245 if (m_max_ranges
== 1
1246 || (widest_int::from (m_base
[1], sign
) + 1
1247 >= widest_int::from (r
.m_base
[0], TYPE_SIGN (r
.m_type
))))
1248 m_base
[1] = r
.m_base
[1];
1251 // This is a dual range result.
1252 m_base
[2] = r
.m_base
[0];
1253 m_base
[3] = r
.m_base
[1];
1256 // The range has been altered, so normalize it even if nothing
1257 // changed in the mask.
1258 if (!union_bitmask (r
))
1265 // Set the new lower bound to R's lower bound.
1266 wide_int lb
= m_base
[0];
1267 m_base
[0] = r
.m_base
[0];
1269 // If R fully contains THIS range, just set the upper bound.
1270 if (wi::ge_p (r
.m_base
[1], m_base
[1], sign
))
1271 m_base
[1] = r
.m_base
[1];
1272 // Check for overlapping ranges, or target limited to a single range.
1273 else if (m_max_ranges
== 1
1274 || (widest_int::from (r
.m_base
[1], TYPE_SIGN (r
.m_type
)) + 1
1275 >= widest_int::from (lb
, sign
)))
1279 // Left with 2 pairs.
1282 m_base
[3] = m_base
[1];
1283 m_base
[1] = r
.m_base
[1];
1285 // The range has been altered, so normalize it even if nothing
1286 // changed in the mask.
1287 if (!union_bitmask (r
))
1294 // Return TRUE if anything changes.
1297 irange::union_ (const vrange
&v
)
1299 const irange
&r
= as_a
<irange
> (v
);
1301 if (r
.undefined_p ())
1317 set_varying (type ());
1321 // Special case one range union one range.
1322 if (m_num_ranges
== 1 && r
.m_num_ranges
== 1)
1323 return irange_single_pair_union (r
);
1325 // If this ranges fully contains R, then we need do nothing.
1326 if (irange_contains_p (r
))
1327 return union_bitmask (r
);
1329 // Do not worry about merging and such by reserving twice as many
1330 // pairs as needed, and then simply sort the 2 ranges into this
1331 // intermediate form.
1333 // The intermediate result will have the property that the beginning
1334 // of each range is <= the beginning of the next range. There may
1335 // be overlapping ranges at this point. I.e. this would be valid
1336 // [-20, 10], [-10, 0], [0, 20], [40, 90] as it satisfies this
1337 // constraint : -20 < -10 < 0 < 40. When the range is rebuilt into r,
1338 // the merge is performed.
1340 // [Xi,Yi]..[Xn,Yn] U [Xj,Yj]..[Xm,Ym] --> [Xk,Yk]..[Xp,Yp]
1341 auto_vec
<wide_int
, 20> res (m_num_ranges
* 2 + r
.m_num_ranges
* 2);
1342 unsigned i
= 0, j
= 0, k
= 0;
1343 signop sign
= TYPE_SIGN (m_type
);
1345 while (i
< m_num_ranges
* 2 && j
< r
.m_num_ranges
* 2)
1347 // lower of Xi and Xj is the lowest point.
1348 if (widest_int::from (m_base
[i
], sign
)
1349 <= widest_int::from (r
.m_base
[j
], sign
))
1351 res
.quick_push (m_base
[i
]);
1352 res
.quick_push (m_base
[i
+ 1]);
1358 res
.quick_push (r
.m_base
[j
]);
1359 res
.quick_push (r
.m_base
[j
+ 1]);
1364 for ( ; i
< m_num_ranges
* 2; i
+= 2)
1366 res
.quick_push (m_base
[i
]);
1367 res
.quick_push (m_base
[i
+ 1]);
1370 for ( ; j
< r
.m_num_ranges
* 2; j
+= 2)
1372 res
.quick_push (r
.m_base
[j
]);
1373 res
.quick_push (r
.m_base
[j
+ 1]);
1377 // Now normalize the vector removing any overlaps.
1379 for (j
= 2; j
< k
; j
+= 2)
1381 // Current upper+1 is >= lower bound next pair, then we merge ranges.
1382 if (widest_int::from (res
[i
- 1], sign
) + 1
1383 >= widest_int::from (res
[j
], sign
))
1385 // New upper bounds is greater of current or the next one.
1386 if (widest_int::from (res
[j
+ 1], sign
)
1387 > widest_int::from (res
[i
- 1], sign
))
1388 res
[i
- 1] = res
[j
+ 1];
1392 // This is a new distinct range, but no point in copying it
1393 // if it is already in the right place.
1397 res
[i
++] = res
[j
+ 1];
1404 // At this point, the vector should have i ranges, none overlapping.
1405 // Now it simply needs to be copied, and if there are too many
1406 // ranges, merge some. We wont do any analysis as to what the
1407 // "best" merges are, simply combine the final ranges into one.
1408 maybe_resize (i
/ 2);
1409 if (i
> m_max_ranges
* 2)
1411 res
[m_max_ranges
* 2 - 1] = res
[i
- 1];
1412 i
= m_max_ranges
* 2;
1415 for (j
= 0; j
< i
; j
++)
1416 m_base
[j
] = res
[j
];
1417 m_num_ranges
= i
/ 2;
1420 // The range has been altered, so normalize it even if nothing
1421 // changed in the mask.
1422 if (!union_bitmask (r
))
1429 // Return TRUE if THIS fully contains R. No undefined or varying cases.
1432 irange::irange_contains_p (const irange
&r
) const
1434 gcc_checking_assert (!undefined_p () && !varying_p ());
1435 gcc_checking_assert (!r
.undefined_p () && !varying_p ());
1437 // In order for THIS to fully contain R, all of the pairs within R must
1438 // be fully contained by the pairs in this object.
1439 signop sign
= TYPE_SIGN (m_type
);
1442 wide_int rl
= r
.m_base
[0];
1443 wide_int ru
= r
.m_base
[1];
1444 wide_int l
= m_base
[0];
1445 wide_int u
= m_base
[1];
1448 // If r is contained within this range, move to the next R
1449 if (wi::ge_p (rl
, l
, sign
)
1450 && wi::le_p (ru
, u
, sign
))
1452 // This pair is OK, Either done, or bump to the next.
1453 if (++ri
>= r
.num_pairs ())
1455 rl
= r
.m_base
[ri
* 2];
1456 ru
= r
.m_base
[ri
* 2 + 1];
1459 // Otherwise, check if this's pair occurs before R's.
1460 if (wi::lt_p (u
, rl
, sign
))
1462 // There's still at least one pair of R left.
1463 if (++i
>= num_pairs ())
1466 u
= m_base
[i
* 2 + 1];
1475 // Return TRUE if anything changes.
1478 irange::intersect (const vrange
&v
)
1480 const irange
&r
= as_a
<irange
> (v
);
1481 gcc_checking_assert (undefined_p () || r
.undefined_p ()
1482 || range_compatible_p (type (), r
.type ()));
1486 if (r
.undefined_p ())
1499 if (r
.num_pairs () == 1)
1501 bool res
= intersect (r
.lower_bound (), r
.upper_bound ());
1505 res
|= intersect_bitmask (r
);
1511 // If R fully contains this, then intersection will change nothing.
1512 if (r
.irange_contains_p (*this))
1513 return intersect_bitmask (r
);
1515 // ?? We could probably come up with something smarter than the
1516 // worst case scenario here.
1517 int needed
= num_pairs () + r
.num_pairs ();
1518 maybe_resize (needed
);
1520 signop sign
= TYPE_SIGN (m_type
);
1521 unsigned bld_pair
= 0;
1522 unsigned bld_lim
= m_max_ranges
;
1523 int_range_max
r2 (*this);
1524 unsigned r2_lim
= r2
.num_pairs ();
1526 for (unsigned i
= 0; i
< r
.num_pairs (); )
1528 // If r1's upper is < r2's lower, we can skip r1's pair.
1529 wide_int ru
= r
.m_base
[i
* 2 + 1];
1530 wide_int r2l
= r2
.m_base
[i2
* 2];
1531 if (wi::lt_p (ru
, r2l
, sign
))
1536 // Likewise, skip r2's pair if its excluded.
1537 wide_int r2u
= r2
.m_base
[i2
* 2 + 1];
1538 wide_int rl
= r
.m_base
[i
* 2];
1539 if (wi::lt_p (r2u
, rl
, sign
))
1544 // No more r2, break.
1548 // Must be some overlap. Find the highest of the lower bounds,
1549 // and set it, unless the build limits lower bounds is already
1551 if (bld_pair
< bld_lim
)
1553 if (wi::ge_p (rl
, r2l
, sign
))
1554 m_base
[bld_pair
* 2] = rl
;
1556 m_base
[bld_pair
* 2] = r2l
;
1559 // Decrease and set a new upper.
1562 // ...and choose the lower of the upper bounds.
1563 if (wi::le_p (ru
, r2u
, sign
))
1565 m_base
[bld_pair
* 2 + 1] = ru
;
1567 // Move past the r1 pair and keep trying.
1573 m_base
[bld_pair
* 2 + 1] = r2u
;
1578 // No more r2, break.
1581 // r2 has the higher lower bound.
1584 // At the exit of this loop, it is one of 2 things:
1585 // ran out of r1, or r2, but either means we are done.
1586 m_num_ranges
= bld_pair
;
1587 if (m_num_ranges
== 0)
1594 // The range has been altered, so normalize it even if nothing
1595 // changed in the mask.
1596 if (!intersect_bitmask (r
))
1604 // Multirange intersect for a specified wide_int [lb, ub] range.
1605 // Return TRUE if intersect changed anything.
1607 // NOTE: It is the caller's responsibility to intersect the mask.
1610 irange::intersect (const wide_int
& lb
, const wide_int
& ub
)
1612 // Undefined remains undefined.
1616 tree range_type
= type();
1617 signop sign
= TYPE_SIGN (range_type
);
1619 gcc_checking_assert (TYPE_PRECISION (range_type
) == wi::get_precision (lb
));
1620 gcc_checking_assert (TYPE_PRECISION (range_type
) == wi::get_precision (ub
));
1622 // If this range is fully contained, then intersection will do nothing.
1623 if (wi::ge_p (lower_bound (), lb
, sign
)
1624 && wi::le_p (upper_bound (), ub
, sign
))
1627 unsigned bld_index
= 0;
1628 unsigned pair_lim
= num_pairs ();
1629 for (unsigned i
= 0; i
< pair_lim
; i
++)
1631 wide_int pairl
= m_base
[i
* 2];
1632 wide_int pairu
= m_base
[i
* 2 + 1];
1633 // Once UB is less than a pairs lower bound, we're done.
1634 if (wi::lt_p (ub
, pairl
, sign
))
1636 // if LB is greater than this pairs upper, this pair is excluded.
1637 if (wi::lt_p (pairu
, lb
, sign
))
1640 // Must be some overlap. Find the highest of the lower bounds,
1642 if (wi::gt_p (lb
, pairl
, sign
))
1643 m_base
[bld_index
* 2] = lb
;
1645 m_base
[bld_index
* 2] = pairl
;
1647 // ...and choose the lower of the upper bounds and if the base pair
1648 // has the lower upper bound, need to check next pair too.
1649 if (wi::lt_p (ub
, pairu
, sign
))
1651 m_base
[bld_index
++ * 2 + 1] = ub
;
1655 m_base
[bld_index
++ * 2 + 1] = pairu
;
1658 m_num_ranges
= bld_index
;
1659 if (m_num_ranges
== 0)
1666 // The caller must normalize and verify the range, as the bitmask
1667 // still needs to be handled.
1672 // Signed 1-bits are strange. You can't subtract 1, because you can't
1673 // represent the number 1. This works around that for the invert routine.
1675 static wide_int
inline
1676 subtract_one (const wide_int
&x
, tree type
, wi::overflow_type
&overflow
)
1678 if (TYPE_SIGN (type
) == SIGNED
)
1679 return wi::add (x
, -1, SIGNED
, &overflow
);
1681 return wi::sub (x
, 1, UNSIGNED
, &overflow
);
1684 // The analogous function for adding 1.
1686 static wide_int
inline
1687 add_one (const wide_int
&x
, tree type
, wi::overflow_type
&overflow
)
1689 if (TYPE_SIGN (type
) == SIGNED
)
1690 return wi::sub (x
, -1, SIGNED
, &overflow
);
1692 return wi::add (x
, 1, UNSIGNED
, &overflow
);
1695 // Return the inverse of a range.
1700 gcc_checking_assert (!undefined_p () && !varying_p ());
1702 // We always need one more set of bounds to represent an inverse, so
1703 // if we're at the limit, we can't properly represent things.
1705 // For instance, to represent the inverse of a 2 sub-range set
1706 // [5, 10][20, 30], we would need a 3 sub-range set
1707 // [-MIN, 4][11, 19][31, MAX].
1709 // In this case, return the most conservative thing.
1711 // However, if any of the extremes of the range are -MIN/+MAX, we
1712 // know we will not need an extra bound. For example:
1714 // INVERT([-MIN,20][30,40]) => [21,29][41,+MAX]
1715 // INVERT([-MIN,20][30,MAX]) => [21,29]
1716 tree ttype
= type ();
1717 unsigned prec
= TYPE_PRECISION (ttype
);
1718 signop sign
= TYPE_SIGN (ttype
);
1719 wide_int type_min
= wi::min_value (prec
, sign
);
1720 wide_int type_max
= wi::max_value (prec
, sign
);
1721 m_bitmask
.set_unknown (prec
);
1723 // At this point, we need one extra sub-range to represent the
1725 maybe_resize (m_num_ranges
+ 1);
1727 // The algorithm is as follows. To calculate INVERT ([a,b][c,d]), we
1728 // generate [-MIN, a-1][b+1, c-1][d+1, MAX].
1730 // If there is an over/underflow in the calculation for any
1731 // sub-range, we eliminate that subrange. This allows us to easily
1732 // calculate INVERT([-MIN, 5]) with: [-MIN, -MIN-1][6, MAX]. And since
1733 // we eliminate the underflow, only [6, MAX] remains.
1735 wi::overflow_type ovf
;
1736 // Construct leftmost range.
1737 int_range_max
orig_range (*this);
1738 unsigned nitems
= 0;
1740 // If this is going to underflow on the MINUS 1, don't even bother
1741 // checking. This also handles subtracting one from an unsigned 0,
1742 // which doesn't set the underflow bit.
1743 if (type_min
!= orig_range
.lower_bound ())
1745 m_base
[nitems
++] = type_min
;
1746 tmp
= subtract_one (orig_range
.lower_bound (), ttype
, ovf
);
1747 m_base
[nitems
++] = tmp
;
1752 // Construct middle ranges if applicable.
1753 if (orig_range
.num_pairs () > 1)
1756 for (; j
< (orig_range
.num_pairs () * 2) - 1; j
+= 2)
1758 // The middle ranges cannot have MAX/MIN, so there's no need
1759 // to check for unsigned overflow on the +1 and -1 here.
1760 tmp
= wi::add (orig_range
.m_base
[j
], 1, sign
, &ovf
);
1761 m_base
[nitems
++] = tmp
;
1762 tmp
= subtract_one (orig_range
.m_base
[j
+ 1], ttype
, ovf
);
1763 m_base
[nitems
++] = tmp
;
1769 // Construct rightmost range.
1771 // However, if this will overflow on the PLUS 1, don't even bother.
1772 // This also handles adding one to an unsigned MAX, which doesn't
1773 // set the overflow bit.
1774 if (type_max
!= orig_range
.m_base
[i
])
1776 tmp
= add_one (orig_range
.m_base
[i
], ttype
, ovf
);
1777 m_base
[nitems
++] = tmp
;
1778 m_base
[nitems
++] = type_max
;
1782 m_num_ranges
= nitems
/ 2;
1784 // We disallow undefined or varying coming in, so the result can
1785 // only be a VR_RANGE.
1786 gcc_checking_assert (m_kind
== VR_RANGE
);
1792 // Return the bitmask inherent in the range.
1795 irange::get_bitmask_from_range () const
1797 unsigned prec
= TYPE_PRECISION (type ());
1798 wide_int min
= lower_bound ();
1799 wide_int max
= upper_bound ();
1801 // All the bits of a singleton are known.
1804 wide_int mask
= wi::zero (prec
);
1805 wide_int value
= lower_bound ();
1806 return irange_bitmask (value
, mask
);
1809 wide_int xorv
= min
^ max
;
1812 xorv
= wi::mask (prec
- wi::clz (xorv
), false, prec
);
1814 return irange_bitmask (wi::zero (prec
), min
| xorv
);
1817 // If the the mask can be trivially converted to a range, do so and
1821 irange::set_range_from_bitmask ()
1823 gcc_checking_assert (!undefined_p ());
1824 if (m_bitmask
.unknown_p ())
1827 // If all the bits are known, this is a singleton.
1828 if (m_bitmask
.mask () == 0)
1830 set (m_type
, m_bitmask
.value (), m_bitmask
.value ());
1834 unsigned popcount
= wi::popcount (m_bitmask
.get_nonzero_bits ());
1836 // If we have only one bit set in the mask, we can figure out the
1837 // range immediately.
1840 // Make sure we don't pessimize the range.
1841 if (!contains_p (m_bitmask
.get_nonzero_bits ()))
1844 bool has_zero
= contains_zero_p (*this);
1845 wide_int nz
= m_bitmask
.get_nonzero_bits ();
1846 set (m_type
, nz
, nz
);
1847 m_bitmask
.set_nonzero_bits (nz
);
1851 zero
.set_zero (type ());
1858 else if (popcount
== 0)
1867 irange::update_bitmask (const irange_bitmask
&bm
)
1869 gcc_checking_assert (!undefined_p ());
1871 // Drop VARYINGs with known bits to a plain range.
1872 if (m_kind
== VR_VARYING
&& !bm
.unknown_p ())
1876 if (!set_range_from_bitmask ())
1882 // Return the bitmask of known bits that includes the bitmask inherent
1886 irange::get_bitmask () const
1888 gcc_checking_assert (!undefined_p ());
1890 // The mask inherent in the range is calculated on-demand. For
1891 // example, [0,255] does not have known bits set by default. This
1892 // saves us considerable time, because setting it at creation incurs
1893 // a large penalty for irange::set. At the time of writing there
1894 // was a 5% slowdown in VRP if we kept the mask precisely up to date
1895 // at all times. Instead, we default to -1 and set it when
1896 // explicitly requested. However, this function will always return
1897 // the correct mask.
1899 // This also means that the mask may have a finer granularity than
1900 // the range and thus contradict it. Think of the mask as an
1901 // enhancement to the range. For example:
1903 // [3, 1000] MASK 0xfffffffe VALUE 0x0
1905 // 3 is in the range endpoints, but is excluded per the known 0 bits
1908 // See also the note in irange_bitmask::intersect.
1909 irange_bitmask bm
= get_bitmask_from_range ();
1910 if (!m_bitmask
.unknown_p ())
1911 bm
.intersect (m_bitmask
);
1915 // Set the nonzero bits in R into THIS. Return TRUE and
1916 // normalize the range if anything changed.
1919 irange::set_nonzero_bits (const wide_int
&bits
)
1921 gcc_checking_assert (!undefined_p ());
1922 irange_bitmask
bm (wi::zero (TYPE_PRECISION (type ())), bits
);
1923 update_bitmask (bm
);
1926 // Return the nonzero bits in R.
1929 irange::get_nonzero_bits () const
1931 gcc_checking_assert (!undefined_p ());
1932 irange_bitmask bm
= get_bitmask ();
1933 return bm
.value () | bm
.mask ();
1936 // Intersect the bitmask in R into THIS and normalize the range.
1937 // Return TRUE if the intersection changed anything.
1940 irange::intersect_bitmask (const irange
&r
)
1942 gcc_checking_assert (!undefined_p () && !r
.undefined_p ());
1944 if (m_bitmask
== r
.m_bitmask
)
1947 irange_bitmask bm
= get_bitmask ();
1948 irange_bitmask save
= bm
;
1949 if (!bm
.intersect (r
.get_bitmask ()))
1954 // Updating m_bitmask may still yield a semantic bitmask (as
1955 // returned by get_bitmask) which is functionally equivalent to what
1956 // we originally had. In which case, there's still no change.
1957 if (save
== get_bitmask ())
1960 if (!set_range_from_bitmask ())
1967 // Union the bitmask in R into THIS. Return TRUE and normalize the
1968 // range if anything changed.
1971 irange::union_bitmask (const irange
&r
)
1973 gcc_checking_assert (!undefined_p () && !r
.undefined_p ());
1975 if (m_bitmask
== r
.m_bitmask
)
1978 irange_bitmask bm
= get_bitmask ();
1979 irange_bitmask save
= bm
;
1980 if (!bm
.union_ (r
.get_bitmask ()))
1985 // Updating m_bitmask may still yield a semantic bitmask (as
1986 // returned by get_bitmask) which is functionally equivalent to what
1987 // we originally had. In which case, there's still no change.
1988 if (save
== get_bitmask ())
1991 // No need to call set_range_from_mask, because we'll never
1992 // narrow the range. Besides, it would cause endless recursion
1993 // because of the union_ in set_range_from_mask.
1999 irange_bitmask::verify_mask () const
2001 gcc_assert (m_value
.get_precision () == m_mask
.get_precision ());
2005 dump_value_range (FILE *file
, const vrange
*vr
)
2011 debug (const vrange
*vr
)
2013 dump_value_range (stderr
, vr
);
2014 fprintf (stderr
, "\n");
2018 debug (const vrange
&vr
)
2024 debug (const value_range
*vr
)
2026 dump_value_range (stderr
, vr
);
2027 fprintf (stderr
, "\n");
2031 debug (const value_range
&vr
)
2033 dump_value_range (stderr
, &vr
);
2034 fprintf (stderr
, "\n");
2037 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
2040 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
2044 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
2050 gt_ggc_mx (irange
*x
)
2052 if (!x
->undefined_p ())
2053 gt_ggc_mx (x
->m_type
);
2057 gt_pch_nx (irange
*x
)
2059 if (!x
->undefined_p ())
2060 gt_pch_nx (x
->m_type
);
2064 gt_pch_nx (irange
*x
, gt_pointer_operator op
, void *cookie
)
2066 for (unsigned i
= 0; i
< x
->m_num_ranges
; ++i
)
2068 op (&x
->m_base
[i
* 2], NULL
, cookie
);
2069 op (&x
->m_base
[i
* 2 + 1], NULL
, cookie
);
2074 gt_ggc_mx (frange
*x
)
2076 gt_ggc_mx (x
->m_type
);
2080 gt_pch_nx (frange
*x
)
2082 gt_pch_nx (x
->m_type
);
2086 gt_pch_nx (frange
*x
, gt_pointer_operator op
, void *cookie
)
2088 op (&x
->m_type
, NULL
, cookie
);
2092 gt_ggc_mx (vrange
*x
)
2094 if (is_a
<irange
> (*x
))
2095 return gt_ggc_mx ((irange
*) x
);
2096 if (is_a
<frange
> (*x
))
2097 return gt_ggc_mx ((frange
*) x
);
2102 gt_pch_nx (vrange
*x
)
2104 if (is_a
<irange
> (*x
))
2105 return gt_pch_nx ((irange
*) x
);
2106 if (is_a
<frange
> (*x
))
2107 return gt_pch_nx ((frange
*) x
);
2112 gt_pch_nx (vrange
*x
, gt_pointer_operator op
, void *cookie
)
2114 if (is_a
<irange
> (*x
))
2115 gt_pch_nx ((irange
*) x
, op
, cookie
);
2116 else if (is_a
<frange
> (*x
))
2117 gt_pch_nx ((frange
*) x
, op
, cookie
);
2122 #define DEFINE_INT_RANGE_INSTANCE(N) \
2123 template int_range<N>::int_range(tree_node *, \
2126 value_range_kind); \
2127 template int_range<N>::int_range(tree); \
2128 template int_range<N>::int_range(const irange &); \
2129 template int_range<N>::int_range(const int_range &); \
2130 template int_range<N>& int_range<N>::operator= (const int_range &);
2132 DEFINE_INT_RANGE_INSTANCE(1)
2133 DEFINE_INT_RANGE_INSTANCE(2)
2134 DEFINE_INT_RANGE_INSTANCE(3)
2135 DEFINE_INT_RANGE_INSTANCE(255)
2138 #include "selftest.h"
2140 #define INT(x) wi::shwi ((x), TYPE_PRECISION (integer_type_node))
2141 #define UINT(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_type_node))
2142 #define SCHAR(x) wi::shwi ((x), TYPE_PRECISION (signed_char_type_node))
2148 range (tree type
, int a
, int b
, value_range_kind kind
= VR_RANGE
)
2151 if (TYPE_UNSIGNED (type
))
2153 w1
= wi::uhwi (a
, TYPE_PRECISION (type
));
2154 w2
= wi::uhwi (b
, TYPE_PRECISION (type
));
2158 w1
= wi::shwi (a
, TYPE_PRECISION (type
));
2159 w2
= wi::shwi (b
, TYPE_PRECISION (type
));
2161 return int_range
<2> (type
, w1
, w2
, kind
);
2165 range_int (int a
, int b
, value_range_kind kind
= VR_RANGE
)
2167 return range (integer_type_node
, a
, b
, kind
);
2171 range_uint (int a
, int b
, value_range_kind kind
= VR_RANGE
)
2173 return range (unsigned_type_node
, a
, b
, kind
);
2177 range_uint128 (int a
, int b
, value_range_kind kind
= VR_RANGE
)
2179 tree u128_type_node
= build_nonstandard_integer_type (128, 1);
2180 return range (u128_type_node
, a
, b
, kind
);
2184 range_uchar (int a
, int b
, value_range_kind kind
= VR_RANGE
)
2186 return range (unsigned_char_type_node
, a
, b
, kind
);
2190 range_char (int a
, int b
, value_range_kind kind
= VR_RANGE
)
2192 return range (signed_char_type_node
, a
, b
, kind
);
2196 build_range3 (int a
, int b
, int c
, int d
, int e
, int f
)
2198 int_range
<3> i1
= range_int (a
, b
);
2199 int_range
<3> i2
= range_int (c
, d
);
2200 int_range
<3> i3
= range_int (e
, f
);
2207 range_tests_irange3 ()
2209 int_range
<3> r0
, r1
, r2
;
2210 int_range
<3> i1
, i2
, i3
;
2212 // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20].
2213 r0
= range_int (10, 20);
2214 r1
= range_int (5, 8);
2216 r1
= range_int (1, 3);
2218 ASSERT_TRUE (r0
== build_range3 (1, 3, 5, 8, 10, 20));
2220 // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20].
2221 r1
= range_int (-5, 0);
2223 ASSERT_TRUE (r0
== build_range3 (-5, 3, 5, 8, 10, 20));
2225 // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60].
2226 r1
= range_int (50, 60);
2227 r0
= range_int (10, 20);
2228 r0
.union_ (range_int (30, 40));
2230 ASSERT_TRUE (r0
== build_range3 (10, 20, 30, 40, 50, 60));
2231 // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80].
2232 r1
= range_int (70, 80);
2235 r2
= build_range3 (10, 20, 30, 40, 50, 60);
2236 r2
.union_ (range_int (70, 80));
2237 ASSERT_TRUE (r0
== r2
);
2239 // [10,20][30,40][50,60] U [6,35] => [6,40][50,60].
2240 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2241 r1
= range_int (6, 35);
2243 r1
= range_int (6, 40);
2244 r1
.union_ (range_int (50, 60));
2245 ASSERT_TRUE (r0
== r1
);
2247 // [10,20][30,40][50,60] U [6,60] => [6,60].
2248 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2249 r1
= range_int (6, 60);
2251 ASSERT_TRUE (r0
== range_int (6, 60));
2253 // [10,20][30,40][50,60] U [6,70] => [6,70].
2254 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2255 r1
= range_int (6, 70);
2257 ASSERT_TRUE (r0
== range_int (6, 70));
2259 // [10,20][30,40][50,60] U [35,70] => [10,20][30,70].
2260 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2261 r1
= range_int (35, 70);
2263 r1
= range_int (10, 20);
2264 r1
.union_ (range_int (30, 70));
2265 ASSERT_TRUE (r0
== r1
);
2267 // [10,20][30,40][50,60] U [15,35] => [10,40][50,60].
2268 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2269 r1
= range_int (15, 35);
2271 r1
= range_int (10, 40);
2272 r1
.union_ (range_int (50, 60));
2273 ASSERT_TRUE (r0
== r1
);
2275 // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60].
2276 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2277 r1
= range_int (35, 35);
2279 ASSERT_TRUE (r0
== build_range3 (10, 20, 30, 40, 50, 60));
2283 range_tests_int_range_max ()
2286 unsigned int nrange
;
2288 // Build a huge multi-range range.
2289 for (nrange
= 0; nrange
< 50; ++nrange
)
2291 int_range
<1> tmp
= range_int (nrange
*10, nrange
*10 + 5);
2294 ASSERT_TRUE (big
.num_pairs () == nrange
);
2296 // Verify that we can copy it without loosing precision.
2297 int_range_max
copy (big
);
2298 ASSERT_TRUE (copy
.num_pairs () == nrange
);
2300 // Inverting it should produce one more sub-range.
2302 ASSERT_TRUE (big
.num_pairs () == nrange
+ 1);
2304 int_range
<1> tmp
= range_int (5, 37);
2305 big
.intersect (tmp
);
2306 ASSERT_TRUE (big
.num_pairs () == 4);
2308 // Test that [10,10][20,20] does NOT contain 15.
2310 int_range_max i1
= range_int (10, 10);
2311 int_range_max i2
= range_int (20, 20);
2313 ASSERT_FALSE (i1
.contains_p (INT (15)));
2317 // Simulate -fstrict-enums where the domain of a type is less than the
2321 range_tests_strict_enum ()
2323 // The enum can only hold [0, 3].
2324 tree rtype
= copy_node (unsigned_type_node
);
2325 TYPE_MIN_VALUE (rtype
) = build_int_cstu (rtype
, 0);
2326 TYPE_MAX_VALUE (rtype
) = build_int_cstu (rtype
, 3);
2328 // Test that even though vr1 covers the strict enum domain ([0, 3]),
2329 // it does not cover the domain of the underlying type.
2330 int_range
<1> vr1
= range (rtype
, 0, 1);
2331 int_range
<1> vr2
= range (rtype
, 2, 3);
2333 ASSERT_TRUE (vr1
== range (rtype
, 0, 3));
2334 ASSERT_FALSE (vr1
.varying_p ());
2336 // Test that copying to a multi-range does not change things.
2337 int_range
<2> ir1 (vr1
);
2338 ASSERT_TRUE (ir1
== vr1
);
2339 ASSERT_FALSE (ir1
.varying_p ());
2341 // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3].
2342 vr1
= int_range
<2> (rtype
,
2343 wi::to_wide (TYPE_MIN_VALUE (rtype
)),
2344 wi::to_wide (TYPE_MAX_VALUE (rtype
)));
2346 ASSERT_TRUE (ir1
== vr1
);
2347 ASSERT_FALSE (ir1
.varying_p ());
2353 tree u128_type
= build_nonstandard_integer_type (128, /*unsigned=*/1);
2354 int_range
<2> i1
, i2
, i3
;
2355 int_range
<2> r0
, r1
, rold
;
2357 // Test 1-bit signed integer union.
2358 // [-1,-1] U [0,0] = VARYING.
2359 tree one_bit_type
= build_nonstandard_integer_type (1, 0);
2360 wide_int one_bit_min
= irange_val_min (one_bit_type
);
2361 wide_int one_bit_max
= irange_val_max (one_bit_type
);
2363 int_range
<2> min
= int_range
<2> (one_bit_type
, one_bit_min
, one_bit_min
);
2364 int_range
<2> max
= int_range
<2> (one_bit_type
, one_bit_max
, one_bit_max
);
2366 ASSERT_TRUE (max
.varying_p ());
2368 // Test that we can set a range of true+false for a 1-bit signed int.
2369 r0
= range_true_and_false (one_bit_type
);
2371 // Test inversion of 1-bit signed integers.
2373 int_range
<2> min
= int_range
<2> (one_bit_type
, one_bit_min
, one_bit_min
);
2374 int_range
<2> max
= int_range
<2> (one_bit_type
, one_bit_max
, one_bit_max
);
2378 ASSERT_TRUE (t
== max
);
2381 ASSERT_TRUE (t
== min
);
2384 // Test that NOT(255) is [0..254] in 8-bit land.
2385 int_range
<1> not_255
= range_uchar (255, 255, VR_ANTI_RANGE
);
2386 ASSERT_TRUE (not_255
== range_uchar (0, 254));
2388 // Test that NOT(0) is [1..255] in 8-bit land.
2389 int_range
<2> not_zero
= range_nonzero (unsigned_char_type_node
);
2390 ASSERT_TRUE (not_zero
== range_uchar (1, 255));
2392 // Check that [0,127][0x..ffffff80,0x..ffffff]
2393 // => ~[128, 0x..ffffff7f].
2394 r0
= range_uint128 (0, 127);
2395 wide_int high
= wi::minus_one (128);
2396 // low = -1 - 127 => 0x..ffffff80.
2397 wide_int low
= wi::sub (high
, wi::uhwi (127, 128));
2398 r1
= int_range
<1> (u128_type
, low
, high
); // [0x..ffffff80, 0x..ffffffff]
2399 // r0 = [0,127][0x..ffffff80,0x..fffffff].
2401 // r1 = [128, 0x..ffffff7f].
2402 r1
= int_range
<1> (u128_type
,
2403 wi::uhwi (128, 128),
2404 wi::sub (wi::minus_one (128), wi::uhwi (128, 128)));
2406 ASSERT_TRUE (r0
== r1
);
2408 r0
.set_varying (integer_type_node
);
2409 wide_int minint
= r0
.lower_bound ();
2410 wide_int maxint
= r0
.upper_bound ();
2412 r0
.set_varying (short_integer_type_node
);
2414 r0
.set_varying (unsigned_type_node
);
2415 wide_int maxuint
= r0
.upper_bound ();
2417 // Check that ~[0,5] => [6,MAX] for unsigned int.
2418 r0
= range_uint (0, 5);
2420 ASSERT_TRUE (r0
== int_range
<1> (unsigned_type_node
,
2421 wi::uhwi (6, TYPE_PRECISION (unsigned_type_node
)),
2424 // Check that ~[10,MAX] => [0,9] for unsigned int.
2425 r0
= int_range
<1> (unsigned_type_node
,
2426 wi::uhwi (10, TYPE_PRECISION (unsigned_type_node
)),
2429 ASSERT_TRUE (r0
== range_uint (0, 9));
2431 // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers.
2432 r0
= range_uint128 (0, 5, VR_ANTI_RANGE
);
2433 r1
= int_range
<1> (u128_type
, wi::uhwi (6, 128), wi::minus_one (128));
2434 ASSERT_TRUE (r0
== r1
);
2436 // Check that [~5] is really [-MIN,4][6,MAX].
2437 r0
= range_int (5, 5, VR_ANTI_RANGE
);
2438 r1
= int_range
<1> (integer_type_node
, minint
, INT (4));
2439 r1
.union_ (int_range
<1> (integer_type_node
, INT (6), maxint
));
2440 ASSERT_FALSE (r1
.undefined_p ());
2441 ASSERT_TRUE (r0
== r1
);
2443 r1
= range_int (5, 5);
2444 int_range
<2> r2 (r1
);
2445 ASSERT_TRUE (r1
== r2
);
2447 r1
= range_int (5, 10);
2449 r1
= range_int (5, 10);
2450 ASSERT_TRUE (r1
.contains_p (INT (7)));
2452 r1
= range_char (0, 20);
2453 ASSERT_TRUE (r1
.contains_p (SCHAR(15)));
2454 ASSERT_FALSE (r1
.contains_p (SCHAR(300)));
2456 // NOT([10,20]) ==> [-MIN,9][21,MAX].
2457 r0
= r1
= range_int (10, 20);
2458 r2
= int_range
<1> (integer_type_node
, minint
, INT(9));
2459 r2
.union_ (int_range
<1> (integer_type_node
, INT(21), maxint
));
2460 ASSERT_FALSE (r2
.undefined_p ());
2462 ASSERT_TRUE (r1
== r2
);
2463 // Test that NOT(NOT(x)) == x.
2465 ASSERT_TRUE (r0
== r2
);
2467 // Test that booleans and their inverse work as expected.
2468 r0
= range_zero (boolean_type_node
);
2469 ASSERT_TRUE (r0
== range_false ());
2471 ASSERT_TRUE (r0
== range_true ());
2473 // Make sure NULL and non-NULL of pointer types work, and that
2474 // inverses of them are consistent.
2475 tree voidp
= build_pointer_type (void_type_node
);
2476 r0
= range_zero (voidp
);
2480 ASSERT_TRUE (r0
== r1
);
2482 // [10,20] U [15, 30] => [10, 30].
2483 r0
= range_int (10, 20);
2484 r1
= range_int (15, 30);
2486 ASSERT_TRUE (r0
== range_int (10, 30));
2488 // [15,40] U [] => [15,40].
2489 r0
= range_int (15, 40);
2490 r1
.set_undefined ();
2492 ASSERT_TRUE (r0
== range_int (15, 40));
2494 // [10,20] U [10,10] => [10,20].
2495 r0
= range_int (10, 20);
2496 r1
= range_int (10, 10);
2498 ASSERT_TRUE (r0
== range_int (10, 20));
2500 // [10,20] U [9,9] => [9,20].
2501 r0
= range_int (10, 20);
2502 r1
= range_int (9, 9);
2504 ASSERT_TRUE (r0
== range_int (9, 20));
2506 // [10,20] ^ [15,30] => [15,20].
2507 r0
= range_int (10, 20);
2508 r1
= range_int (15, 30);
2510 ASSERT_TRUE (r0
== range_int (15, 20));
2512 // Test the internal sanity of wide_int's wrt HWIs.
2513 ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node
),
2514 TYPE_SIGN (boolean_type_node
))
2515 == wi::uhwi (1, TYPE_PRECISION (boolean_type_node
)));
2518 r0
= range_int (0, 0);
2519 ASSERT_TRUE (r0
.zero_p ());
2521 // Test nonzero_p().
2522 r0
= range_int (0, 0);
2524 ASSERT_TRUE (r0
.nonzero_p ());
2527 r0
= range_int (1, 1, VR_ANTI_RANGE
);
2529 r1
= range_int (3, 3, VR_ANTI_RANGE
);
2531 // vv = [0,0][2,2][4, MAX]
2532 int_range
<3> vv
= r0
;
2535 ASSERT_TRUE (vv
.contains_p (UINT (2)));
2536 ASSERT_TRUE (vv
.num_pairs () == 3);
2538 r0
= range_int (1, 1);
2539 // And union it with [0,0][2,2][4,MAX] multi range
2541 // The result should be [0,2][4,MAX], or ~[3,3] but it must contain 2
2542 ASSERT_TRUE (r0
.contains_p (INT (2)));
2546 range_tests_nonzero_bits ()
2548 int_range
<2> r0
, r1
;
2550 // Adding nonzero bits to a varying drops the varying.
2551 r0
.set_varying (integer_type_node
);
2552 r0
.set_nonzero_bits (INT (255));
2553 ASSERT_TRUE (!r0
.varying_p ());
2554 // Dropping the nonzero bits brings us back to varying.
2555 r0
.set_nonzero_bits (INT (-1));
2556 ASSERT_TRUE (r0
.varying_p ());
2558 // Test contains_p with nonzero bits.
2559 r0
.set_zero (integer_type_node
);
2560 ASSERT_TRUE (r0
.contains_p (INT (0)));
2561 ASSERT_FALSE (r0
.contains_p (INT (1)));
2562 r0
.set_nonzero_bits (INT (0xfe));
2563 ASSERT_FALSE (r0
.contains_p (INT (0x100)));
2564 ASSERT_FALSE (r0
.contains_p (INT (0x3)));
2566 // Union of nonzero bits.
2567 r0
.set_varying (integer_type_node
);
2568 r0
.set_nonzero_bits (INT (0xf0));
2569 r1
.set_varying (integer_type_node
);
2570 r1
.set_nonzero_bits (INT (0xf));
2572 ASSERT_TRUE (r0
.get_nonzero_bits () == 0xff);
2574 // Intersect of nonzero bits.
2575 r0
= range_int (0, 255);
2576 r0
.set_nonzero_bits (INT (0xfe));
2577 r1
.set_varying (integer_type_node
);
2578 r1
.set_nonzero_bits (INT (0xf0));
2580 ASSERT_TRUE (r0
.get_nonzero_bits () == 0xf0);
2582 // Intersect where the mask of nonzero bits is implicit from the range.
2583 r0
.set_varying (integer_type_node
);
2584 r1
= range_int (0, 255);
2586 ASSERT_TRUE (r0
.get_nonzero_bits () == 0xff);
2588 // The union of a mask of 0xff..ffff00 with a mask of 0xff spans the
2589 // entire domain, and makes the range a varying.
2590 r0
.set_varying (integer_type_node
);
2591 wide_int x
= wi::shwi (0xff, TYPE_PRECISION (integer_type_node
));
2592 x
= wi::bit_not (x
);
2593 r0
.set_nonzero_bits (x
); // 0xff..ff00
2594 r1
.set_varying (integer_type_node
);
2595 r1
.set_nonzero_bits (INT (0xff));
2597 ASSERT_TRUE (r0
.varying_p ());
2599 // Test that setting a nonzero bit of 1 does not pessimize the range.
2600 r0
.set_zero (integer_type_node
);
2601 r0
.set_nonzero_bits (INT (1));
2602 ASSERT_TRUE (r0
.zero_p ());
2605 // Build an frange from string endpoints.
2607 static inline frange
2608 frange_float (const char *lb
, const char *ub
, tree type
= float_type_node
)
2610 REAL_VALUE_TYPE min
, max
;
2611 gcc_assert (real_from_string (&min
, lb
) == 0);
2612 gcc_assert (real_from_string (&max
, ub
) == 0);
2613 return frange (type
, min
, max
);
2620 REAL_VALUE_TYPE q
, r
;
2623 // Equal ranges but with differing NAN bits are not equal.
2624 if (HONOR_NANS (float_type_node
))
2626 r1
= frange_float ("10", "12");
2634 // [10, 20] NAN ^ [30, 40] NAN = NAN.
2635 r0
= frange_float ("10", "20");
2636 r1
= frange_float ("30", "40");
2638 ASSERT_TRUE (r0
.known_isnan ());
2640 // [3,5] U [5,10] NAN = ... NAN
2641 r0
= frange_float ("3", "5");
2643 r1
= frange_float ("5", "10");
2645 ASSERT_TRUE (r0
.maybe_isnan ());
2648 // [5,6] U NAN = [5,6] NAN.
2649 r0
= frange_float ("5", "6");
2651 r1
.set_nan (float_type_node
);
2653 real_from_string (&q
, "5");
2654 real_from_string (&r
, "6");
2655 ASSERT_TRUE (real_identical (&q
, &r0
.lower_bound ()));
2656 ASSERT_TRUE (real_identical (&r
, &r0
.upper_bound ()));
2657 ASSERT_TRUE (r0
.maybe_isnan ());
2660 r0
.set_nan (float_type_node
);
2661 r1
.set_nan (float_type_node
);
2663 ASSERT_TRUE (r0
.known_isnan ());
2665 // [INF, INF] NAN ^ NAN = NAN
2666 r0
.set_nan (float_type_node
);
2667 r1
= frange_float ("+Inf", "+Inf");
2668 if (!HONOR_NANS (float_type_node
))
2671 ASSERT_TRUE (r0
.known_isnan ());
2674 r0
.set_nan (float_type_node
);
2675 r1
.set_nan (float_type_node
);
2677 ASSERT_TRUE (r0
.known_isnan ());
2679 // +NAN ^ -NAN = UNDEFINED
2680 r0
.set_nan (float_type_node
, false);
2681 r1
.set_nan (float_type_node
, true);
2683 ASSERT_TRUE (r0
.undefined_p ());
2685 // VARYING ^ NAN = NAN.
2686 r0
.set_nan (float_type_node
);
2687 r1
.set_varying (float_type_node
);
2689 ASSERT_TRUE (r0
.known_isnan ());
2691 // [3,4] ^ NAN = UNDEFINED.
2692 r0
= frange_float ("3", "4");
2694 r1
.set_nan (float_type_node
);
2696 ASSERT_TRUE (r0
.undefined_p ());
2698 // [-3, 5] ^ NAN = UNDEFINED
2699 r0
= frange_float ("-3", "5");
2701 r1
.set_nan (float_type_node
);
2703 ASSERT_TRUE (r0
.undefined_p ());
2705 // Setting the NAN bit to yes does not make us a known NAN.
2706 r0
.set_varying (float_type_node
);
2708 ASSERT_FALSE (r0
.known_isnan ());
2710 // NAN is in a VARYING.
2711 r0
.set_varying (float_type_node
);
2712 real_nan (&r
, "", 1, TYPE_MODE (float_type_node
));
2713 REAL_VALUE_TYPE nan
= r
;
2714 ASSERT_TRUE (r0
.contains_p (nan
));
2716 // -NAN is in a VARYING.
2717 r0
.set_varying (float_type_node
);
2718 q
= real_value_negate (&r
);
2719 REAL_VALUE_TYPE neg_nan
= q
;
2720 ASSERT_TRUE (r0
.contains_p (neg_nan
));
2722 // Clearing the NAN on a [] NAN is the empty set.
2723 r0
.set_nan (float_type_node
);
2725 ASSERT_TRUE (r0
.undefined_p ());
2727 // [10,20] NAN ^ [21,25] NAN = [NAN]
2728 r0
= frange_float ("10", "20");
2730 r1
= frange_float ("21", "25");
2733 ASSERT_TRUE (r0
.known_isnan ());
2735 // NAN U [5,6] should be [5,6] +-NAN.
2736 r0
.set_nan (float_type_node
);
2737 r1
= frange_float ("5", "6");
2740 real_from_string (&q
, "5");
2741 real_from_string (&r
, "6");
2742 ASSERT_TRUE (real_identical (&q
, &r0
.lower_bound ()));
2743 ASSERT_TRUE (real_identical (&r
, &r0
.upper_bound ()));
2744 ASSERT_TRUE (!r0
.signbit_p (signbit
));
2745 ASSERT_TRUE (r0
.maybe_isnan ());
2747 // NAN U NAN shouldn't change anything.
2748 r0
.set_nan (float_type_node
);
2749 r1
.set_nan (float_type_node
);
2750 ASSERT_FALSE (r0
.union_ (r1
));
2752 // [3,5] NAN U NAN shouldn't change anything.
2753 r0
= frange_float ("3", "5");
2754 r1
.set_nan (float_type_node
);
2755 ASSERT_FALSE (r0
.union_ (r1
));
2757 // [3,5] U NAN *does* trigger a change.
2758 r0
= frange_float ("3", "5");
2760 r1
.set_nan (float_type_node
);
2761 ASSERT_TRUE (r0
.union_ (r1
));
2765 range_tests_signed_zeros ()
2767 REAL_VALUE_TYPE zero
= dconst0
;
2768 REAL_VALUE_TYPE neg_zero
= zero
;
2773 // [0,0] contains [0,0] but not [-0,-0] and vice versa.
2774 r0
= frange_float ("0.0", "0.0");
2775 r1
= frange_float ("-0.0", "-0.0");
2776 ASSERT_TRUE (r0
.contains_p (zero
));
2777 ASSERT_TRUE (!r0
.contains_p (neg_zero
));
2778 ASSERT_TRUE (r1
.contains_p (neg_zero
));
2779 ASSERT_TRUE (!r1
.contains_p (zero
));
2781 // Test contains_p() when we know the sign of the zero.
2782 r0
= frange_float ("0.0", "0.0");
2783 ASSERT_TRUE (r0
.contains_p (zero
));
2784 ASSERT_FALSE (r0
.contains_p (neg_zero
));
2785 r0
= frange_float ("-0.0", "-0.0");
2786 ASSERT_TRUE (r0
.contains_p (neg_zero
));
2787 ASSERT_FALSE (r0
.contains_p (zero
));
2789 r0
= frange_float ("-0.0", "0.0");
2790 ASSERT_TRUE (r0
.contains_p (neg_zero
));
2791 ASSERT_TRUE (r0
.contains_p (zero
));
2793 r0
= frange_float ("-3", "5");
2794 ASSERT_TRUE (r0
.contains_p (neg_zero
));
2795 ASSERT_TRUE (r0
.contains_p (zero
));
2797 // The intersection of zeros that differ in sign is a NAN (or
2798 // undefined if not honoring NANs).
2799 r0
= frange_float ("-0.0", "-0.0");
2800 r1
= frange_float ("0.0", "0.0");
2802 if (HONOR_NANS (float_type_node
))
2803 ASSERT_TRUE (r0
.known_isnan ());
2805 ASSERT_TRUE (r0
.undefined_p ());
2807 // The union of zeros that differ in sign is a zero with unknown sign.
2808 r0
= frange_float ("0.0", "0.0");
2809 r1
= frange_float ("-0.0", "-0.0");
2811 ASSERT_TRUE (r0
.zero_p () && !r0
.signbit_p (signbit
));
2813 // [-0, +0] has an unknown sign.
2814 r0
= frange_float ("-0.0", "0.0");
2815 ASSERT_TRUE (r0
.zero_p () && !r0
.signbit_p (signbit
));
2817 // [-0, +0] ^ [0, 0] is [0, 0]
2818 r0
= frange_float ("-0.0", "0.0");
2819 r1
= frange_float ("0.0", "0.0");
2821 ASSERT_TRUE (r0
.zero_p ());
2823 r0
= frange_float ("+0", "5");
2825 ASSERT_TRUE (r0
.signbit_p (signbit
) && !signbit
);
2827 r0
= frange_float ("-0", "5");
2829 ASSERT_TRUE (!r0
.signbit_p (signbit
));
2831 r0
= frange_float ("-0", "10");
2832 r1
= frange_float ("0", "5");
2834 ASSERT_TRUE (real_iszero (&r0
.lower_bound (), false));
2836 r0
= frange_float ("-0", "5");
2837 r1
= frange_float ("0", "5");
2839 ASSERT_TRUE (real_iszero (&r0
.lower_bound (), true));
2841 r0
= frange_float ("-5", "-0");
2843 r1
= frange_float ("0", "0");
2846 if (HONOR_NANS (float_type_node
))
2847 ASSERT_TRUE (r0
.known_isnan ());
2849 ASSERT_TRUE (r0
.undefined_p ());
2851 r0
.set_nonnegative (float_type_node
);
2852 if (HONOR_NANS (float_type_node
))
2853 ASSERT_TRUE (r0
.maybe_isnan ());
2855 // Numbers containing zero should have an unknown SIGNBIT.
2856 r0
= frange_float ("0", "10");
2858 ASSERT_TRUE (r0
.signbit_p (signbit
) && !signbit
);
2862 range_tests_signbit ()
2867 // Negative numbers should have the SIGNBIT set.
2868 r0
= frange_float ("-5", "-1");
2870 ASSERT_TRUE (r0
.signbit_p (signbit
) && signbit
);
2871 // Positive numbers should have the SIGNBIT clear.
2872 r0
= frange_float ("1", "10");
2874 ASSERT_TRUE (r0
.signbit_p (signbit
) && !signbit
);
2875 // Numbers spanning both positive and negative should have an
2877 r0
= frange_float ("-10", "10");
2879 ASSERT_TRUE (!r0
.signbit_p (signbit
));
2880 r0
.set_varying (float_type_node
);
2881 ASSERT_TRUE (!r0
.signbit_p (signbit
));
2885 range_tests_floats ()
2889 if (HONOR_NANS (float_type_node
))
2891 range_tests_signbit ();
2893 if (HONOR_SIGNED_ZEROS (float_type_node
))
2894 range_tests_signed_zeros ();
2896 // A range of [-INF,+INF] is actually VARYING if no other properties
2898 r0
= frange_float ("-Inf", "+Inf");
2899 ASSERT_TRUE (r0
.varying_p ());
2900 // ...unless it has some special property...
2901 if (HONOR_NANS (r0
.type ()))
2904 ASSERT_FALSE (r0
.varying_p ());
2907 // For most architectures, where float and double are different
2908 // sizes, having the same endpoints does not necessarily mean the
2909 // ranges are equal.
2910 if (!types_compatible_p (float_type_node
, double_type_node
))
2912 r0
= frange_float ("3.0", "3.0", float_type_node
);
2913 r1
= frange_float ("3.0", "3.0", double_type_node
);
2917 // [3,5] U [10,12] = [3,12].
2918 r0
= frange_float ("3", "5");
2919 r1
= frange_float ("10", "12");
2921 ASSERT_EQ (r0
, frange_float ("3", "12"));
2923 // [5,10] U [4,8] = [4,10]
2924 r0
= frange_float ("5", "10");
2925 r1
= frange_float ("4", "8");
2927 ASSERT_EQ (r0
, frange_float ("4", "10"));
2929 // [3,5] U [4,10] = [3,10]
2930 r0
= frange_float ("3", "5");
2931 r1
= frange_float ("4", "10");
2933 ASSERT_EQ (r0
, frange_float ("3", "10"));
2935 // [4,10] U [5,11] = [4,11]
2936 r0
= frange_float ("4", "10");
2937 r1
= frange_float ("5", "11");
2939 ASSERT_EQ (r0
, frange_float ("4", "11"));
2941 // [3,12] ^ [10,12] = [10,12].
2942 r0
= frange_float ("3", "12");
2943 r1
= frange_float ("10", "12");
2945 ASSERT_EQ (r0
, frange_float ("10", "12"));
2947 // [10,12] ^ [11,11] = [11,11]
2948 r0
= frange_float ("10", "12");
2949 r1
= frange_float ("11", "11");
2951 ASSERT_EQ (r0
, frange_float ("11", "11"));
2953 // [10,20] ^ [5,15] = [10,15]
2954 r0
= frange_float ("10", "20");
2955 r1
= frange_float ("5", "15");
2957 ASSERT_EQ (r0
, frange_float ("10", "15"));
2959 // [10,20] ^ [15,25] = [15,20]
2960 r0
= frange_float ("10", "20");
2961 r1
= frange_float ("15", "25");
2963 ASSERT_EQ (r0
, frange_float ("15", "20"));
2965 // [10,20] ^ [21,25] = []
2966 r0
= frange_float ("10", "20");
2968 r1
= frange_float ("21", "25");
2971 ASSERT_TRUE (r0
.undefined_p ());
2973 if (HONOR_INFINITIES (float_type_node
))
2975 // Make sure [-Inf, -Inf] doesn't get normalized.
2976 r0
= frange_float ("-Inf", "-Inf");
2977 ASSERT_TRUE (real_isinf (&r0
.lower_bound (), true));
2978 ASSERT_TRUE (real_isinf (&r0
.upper_bound (), true));
2981 // Test that reading back a global range yields the same result as
2982 // what we wrote into it.
2983 tree ssa
= make_temp_ssa_name (float_type_node
, NULL
, "blah");
2984 r0
.set_varying (float_type_node
);
2986 set_range_info (ssa
, r0
);
2987 get_global_range_query ()->range_of_expr (r1
, ssa
);
2991 // Run floating range tests for various combinations of NAN and INF
2995 range_tests_floats_various ()
2997 int save_finite_math_only
= flag_finite_math_only
;
2999 // Test -ffinite-math-only.
3000 flag_finite_math_only
= 1;
3001 range_tests_floats ();
3002 // Test -fno-finite-math-only.
3003 flag_finite_math_only
= 0;
3004 range_tests_floats ();
3006 flag_finite_math_only
= save_finite_math_only
;
3012 range_tests_irange3 ();
3013 range_tests_int_range_max ();
3014 range_tests_strict_enum ();
3015 range_tests_nonzero_bits ();
3016 range_tests_floats_various ();
3017 range_tests_misc ();
3020 } // namespace selftest
3022 #endif // CHECKING_P