compiler: only build thunk struct type when it is needed
[official-gcc.git] / gcc / value-range.cc
bloba14f9bc439443bcc5b5b0b2d5fc9514b56a83d58
1 /* Support routines for value ranges.
2 Copyright (C) 2019-2022 Free Software Foundation, Inc.
3 Major hacks by Aldy Hernandez <aldyh@redhat.com> and
4 Andrew MacLeod <amacleod@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "ssa.h"
29 #include "tree-pretty-print.h"
30 #include "value-range-pretty-print.h"
31 #include "fold-const.h"
32 #include "gimple-range.h"
34 void
35 irange::accept (const vrange_visitor &v) const
37 v.visit (*this);
40 void
41 unsupported_range::accept (const vrange_visitor &v) const
43 v.visit (*this);
46 // Convenience function only available for integers and pointers.
48 wide_int
49 Value_Range::lower_bound () const
51 if (is_a <irange> (*m_vrange))
52 return as_a <irange> (*m_vrange).lower_bound ();
53 gcc_unreachable ();
56 // Convenience function only available for integers and pointers.
58 wide_int
59 Value_Range::upper_bound () const
61 if (is_a <irange> (*m_vrange))
62 return as_a <irange> (*m_vrange).upper_bound ();
63 gcc_unreachable ();
66 void
67 Value_Range::dump (FILE *out) const
69 if (m_vrange)
70 m_vrange->dump (out);
71 else
72 fprintf (out, "NULL");
75 DEBUG_FUNCTION void
76 debug (const Value_Range &r)
78 r.dump (stderr);
79 fprintf (stderr, "\n");
82 // Default vrange definitions.
84 bool
85 vrange::contains_p (tree) const
87 return varying_p ();
90 bool
91 vrange::singleton_p (tree *) const
93 return false;
96 void
97 vrange::set (tree min, tree, value_range_kind)
99 set_varying (TREE_TYPE (min));
102 tree
103 vrange::type () const
105 return void_type_node;
108 bool
109 vrange::supports_type_p (const_tree) const
111 return false;
114 void
115 vrange::set_undefined ()
117 m_kind = VR_UNDEFINED;
120 void
121 vrange::set_varying (tree)
123 m_kind = VR_VARYING;
126 bool
127 vrange::union_ (const vrange &r)
129 if (r.undefined_p () || varying_p ())
130 return false;
131 if (undefined_p () || r.varying_p ())
133 operator= (r);
134 return true;
136 gcc_unreachable ();
137 return false;
140 bool
141 vrange::intersect (const vrange &r)
143 if (undefined_p () || r.varying_p ())
144 return false;
145 if (r.undefined_p ())
147 set_undefined ();
148 return true;
150 if (varying_p ())
152 operator= (r);
153 return true;
155 gcc_unreachable ();
156 return false;
159 bool
160 vrange::zero_p () const
162 return false;
165 bool
166 vrange::nonzero_p () const
168 return false;
171 void
172 vrange::set_nonzero (tree type)
174 set_varying (type);
177 void
178 vrange::set_zero (tree type)
180 set_varying (type);
183 void
184 vrange::set_nonnegative (tree type)
186 set_varying (type);
189 bool
190 vrange::fits_p (const vrange &) const
192 return true;
195 // Assignment operator for generic ranges. Copying incompatible types
196 // is not allowed.
198 vrange &
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);
205 else
206 gcc_unreachable ();
207 return *this;
210 // Equality operator for generic ranges.
212 bool
213 vrange::operator== (const vrange &src) const
215 if (is_a <irange> (src))
216 return as_a <irange> (*this) == as_a <irange> (src);
217 if (is_a <frange> (src))
218 return as_a <frange> (*this) == as_a <frange> (src);
219 gcc_unreachable ();
222 // Wrapper for vrange_printer to dump a range to a file.
224 void
225 vrange::dump (FILE *file) const
227 pretty_printer buffer;
228 pp_needs_newline (&buffer) = true;
229 buffer.buffer->stream = file;
230 vrange_printer vrange_pp (&buffer);
231 this->accept (vrange_pp);
232 pp_flush (&buffer);
235 bool
236 irange::supports_type_p (const_tree type) const
238 return supports_p (type);
241 // Return TRUE if R fits in THIS.
243 bool
244 irange::fits_p (const vrange &r) const
246 return m_max_ranges >= as_a <irange> (r).num_pairs ();
249 void
250 irange::set_nonnegative (tree type)
252 set (build_int_cst (type, 0), TYPE_MAX_VALUE (type));
255 void
256 frange::accept (const vrange_visitor &v) const
258 v.visit (*this);
261 // Flush denormal endpoints to the appropriate 0.0.
263 void
264 frange::flush_denormals_to_zero ()
266 if (undefined_p () || known_isnan ())
267 return;
269 // Flush [x, -DENORMAL] to [x, -0.0].
270 if (real_isdenormal (&m_max) && real_isneg (&m_max))
272 m_max = dconst0;
273 if (HONOR_SIGNED_ZEROS (m_type))
274 m_max.sign = 1;
276 // Flush [+DENORMAL, x] to [+0.0, x].
277 if (real_isdenormal (&m_min) && !real_isneg (&m_min))
278 m_min = dconst0;
281 // Setter for franges.
283 void
284 frange::set (tree type,
285 const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
286 value_range_kind kind)
288 switch (kind)
290 case VR_UNDEFINED:
291 set_undefined ();
292 return;
293 case VR_VARYING:
294 case VR_ANTI_RANGE:
295 set_varying (type);
296 return;
297 case VR_RANGE:
298 break;
299 default:
300 gcc_unreachable ();
303 // Handle NANs.
304 if (real_isnan (&min) || real_isnan (&max))
306 gcc_checking_assert (real_identical (&min, &max));
307 bool sign = real_isneg (&min);
308 set_nan (type, sign);
309 return;
312 m_kind = kind;
313 m_type = type;
314 m_min = min;
315 m_max = max;
316 if (HONOR_NANS (m_type))
318 m_pos_nan = true;
319 m_neg_nan = true;
321 else
323 m_pos_nan = false;
324 m_neg_nan = false;
327 // For -ffinite-math-only we can drop ranges outside the
328 // representable numbers to min/max for the type.
329 if (flag_finite_math_only)
331 REAL_VALUE_TYPE min_repr = frange_val_min (m_type);
332 REAL_VALUE_TYPE max_repr = frange_val_max (m_type);
333 if (real_less (&m_min, &min_repr))
334 m_min = min_repr;
335 if (real_less (&max_repr, &m_max))
336 m_max = max_repr;
339 // Check for swapped ranges.
340 gcc_checking_assert (real_compare (LE_EXPR, &min, &max));
342 normalize_kind ();
344 flush_denormals_to_zero ();
346 if (flag_checking)
347 verify_range ();
350 void
351 frange::set (tree min, tree max, value_range_kind kind)
353 set (TREE_TYPE (min),
354 *TREE_REAL_CST_PTR (min), *TREE_REAL_CST_PTR (max), kind);
357 // Normalize range to VARYING or UNDEFINED, or vice versa. Return
358 // TRUE if anything changed.
360 // A range with no known properties can be dropped to VARYING.
361 // Similarly, a VARYING with any properties should be dropped to a
362 // VR_RANGE. Normalizing ranges upon changing them ensures there is
363 // only one representation for a given range.
365 bool
366 frange::normalize_kind ()
368 if (m_kind == VR_RANGE
369 && frange_val_is_min (m_min, m_type)
370 && frange_val_is_max (m_max, m_type))
372 if (m_pos_nan && m_neg_nan)
374 set_varying (m_type);
375 return true;
378 else if (m_kind == VR_VARYING)
380 if (!m_pos_nan || !m_neg_nan)
382 m_kind = VR_RANGE;
383 m_min = frange_val_min (m_type);
384 m_max = frange_val_max (m_type);
385 return true;
388 else if (m_kind == VR_NAN && !m_pos_nan && !m_neg_nan)
389 set_undefined ();
390 return false;
393 // Union or intersect the zero endpoints of two ranges. For example:
394 // [-0, x] U [+0, x] => [-0, x]
395 // [ x, -0] U [ x, +0] => [ x, +0]
396 // [-0, x] ^ [+0, x] => [+0, x]
397 // [ x, -0] ^ [ x, +0] => [ x, -0]
399 // UNION_P is true when performing a union, or false when intersecting.
401 bool
402 frange::combine_zeros (const frange &r, bool union_p)
404 gcc_checking_assert (!undefined_p () && !known_isnan ());
406 bool changed = false;
407 if (real_iszero (&m_min) && real_iszero (&r.m_min)
408 && real_isneg (&m_min) != real_isneg (&r.m_min))
410 m_min.sign = union_p;
411 changed = true;
413 if (real_iszero (&m_max) && real_iszero (&r.m_max)
414 && real_isneg (&m_max) != real_isneg (&r.m_max))
416 m_max.sign = !union_p;
417 changed = true;
419 // If the signs are swapped, the resulting range is empty.
420 if (m_min.sign == 0 && m_max.sign == 1)
422 if (maybe_isnan ())
423 m_kind = VR_NAN;
424 else
425 set_undefined ();
426 changed = true;
428 return changed;
431 // Union two ranges when one is known to be a NAN.
433 bool
434 frange::union_nans (const frange &r)
436 gcc_checking_assert (known_isnan () || r.known_isnan ());
438 if (known_isnan ())
440 m_kind = r.m_kind;
441 m_min = r.m_min;
442 m_max = r.m_max;
444 m_pos_nan |= r.m_pos_nan;
445 m_neg_nan |= r.m_neg_nan;
446 normalize_kind ();
447 if (flag_checking)
448 verify_range ();
449 return true;
452 bool
453 frange::union_ (const vrange &v)
455 const frange &r = as_a <frange> (v);
457 if (r.undefined_p () || varying_p ())
458 return false;
459 if (undefined_p () || r.varying_p ())
461 *this = r;
462 return true;
465 // Combine NAN info.
466 if (known_isnan () || r.known_isnan ())
467 return union_nans (r);
468 bool changed = false;
469 if (m_pos_nan != r.m_pos_nan || m_neg_nan != r.m_neg_nan)
471 m_pos_nan |= r.m_pos_nan;
472 m_neg_nan |= r.m_neg_nan;
473 changed = true;
476 // Combine endpoints.
477 if (real_less (&r.m_min, &m_min))
479 m_min = r.m_min;
480 changed = true;
482 if (real_less (&m_max, &r.m_max))
484 m_max = r.m_max;
485 changed = true;
488 if (HONOR_SIGNED_ZEROS (m_type))
489 changed |= combine_zeros (r, true);
491 changed |= normalize_kind ();
492 if (flag_checking)
493 verify_range ();
494 return changed;
497 // Intersect two ranges when one is known to be a NAN.
499 bool
500 frange::intersect_nans (const frange &r)
502 gcc_checking_assert (known_isnan () || r.known_isnan ());
504 m_pos_nan &= r.m_pos_nan;
505 m_neg_nan &= r.m_neg_nan;
506 if (maybe_isnan ())
507 m_kind = VR_NAN;
508 else
509 set_undefined ();
510 if (flag_checking)
511 verify_range ();
512 return true;
515 bool
516 frange::intersect (const vrange &v)
518 const frange &r = as_a <frange> (v);
520 if (undefined_p () || r.varying_p ())
521 return false;
522 if (r.undefined_p ())
524 set_undefined ();
525 return true;
527 if (varying_p ())
529 *this = r;
530 return true;
533 // Combine NAN info.
534 if (known_isnan () || r.known_isnan ())
535 return intersect_nans (r);
536 bool changed = false;
537 if (m_pos_nan != r.m_pos_nan || m_neg_nan != r.m_neg_nan)
539 m_pos_nan &= r.m_pos_nan;
540 m_neg_nan &= r.m_neg_nan;
541 changed = true;
544 // Combine endpoints.
545 if (real_less (&m_min, &r.m_min))
547 m_min = r.m_min;
548 changed = true;
550 if (real_less (&r.m_max, &m_max))
552 m_max = r.m_max;
553 changed = true;
555 // If the endpoints are swapped, the resulting range is empty.
556 if (real_less (&m_max, &m_min))
558 if (maybe_isnan ())
559 m_kind = VR_NAN;
560 else
561 set_undefined ();
562 if (flag_checking)
563 verify_range ();
564 return true;
567 if (HONOR_SIGNED_ZEROS (m_type))
568 changed |= combine_zeros (r, false);
570 changed |= normalize_kind ();
571 if (flag_checking)
572 verify_range ();
573 return changed;
576 frange &
577 frange::operator= (const frange &src)
579 m_kind = src.m_kind;
580 m_type = src.m_type;
581 m_min = src.m_min;
582 m_max = src.m_max;
583 m_pos_nan = src.m_pos_nan;
584 m_neg_nan = src.m_neg_nan;
586 if (flag_checking)
587 verify_range ();
588 return *this;
591 bool
592 frange::operator== (const frange &src) const
594 if (m_kind == src.m_kind)
596 if (undefined_p ())
597 return true;
599 if (varying_p ())
600 return types_compatible_p (m_type, src.m_type);
602 if (known_isnan () || src.known_isnan ())
603 return false;
605 return (real_identical (&m_min, &src.m_min)
606 && real_identical (&m_max, &src.m_max)
607 && m_pos_nan == src.m_pos_nan
608 && m_neg_nan == src.m_neg_nan
609 && types_compatible_p (m_type, src.m_type));
611 return false;
614 // Return TRUE if range contains the TREE_REAL_CST_PTR in CST.
616 bool
617 frange::contains_p (tree cst) const
619 gcc_checking_assert (m_kind != VR_ANTI_RANGE);
620 const REAL_VALUE_TYPE *rv = TREE_REAL_CST_PTR (cst);
622 if (undefined_p ())
623 return false;
625 if (varying_p ())
626 return true;
628 if (real_isnan (rv))
630 // No NAN in range.
631 if (!m_pos_nan && !m_neg_nan)
632 return false;
633 // Both +NAN and -NAN are present.
634 if (m_pos_nan && m_neg_nan)
635 return true;
636 return m_neg_nan == rv->sign;
638 if (known_isnan ())
639 return false;
641 if (real_compare (GE_EXPR, rv, &m_min) && real_compare (LE_EXPR, rv, &m_max))
643 // Make sure the signs are equal for signed zeros.
644 if (HONOR_SIGNED_ZEROS (m_type) && real_iszero (rv))
645 return m_min.sign == m_max.sign && m_min.sign == rv->sign;
646 return true;
648 return false;
651 // If range is a singleton, place it in RESULT and return TRUE. If
652 // RESULT is NULL, just return TRUE.
654 // A NAN can never be a singleton.
656 bool
657 frange::singleton_p (tree *result) const
659 if (m_kind == VR_RANGE && real_identical (&m_min, &m_max))
661 // Return false for any singleton that may be a NAN.
662 if (HONOR_NANS (m_type) && maybe_isnan ())
663 return false;
665 if (MODE_COMPOSITE_P (TYPE_MODE (m_type)))
667 // For IBM long doubles, if the value is +-Inf or is exactly
668 // representable in double, the other double could be +0.0
669 // or -0.0. Since this means there is more than one way to
670 // represent a value, return false to avoid propagating it.
671 // See libgcc/config/rs6000/ibm-ldouble-format for details.
672 if (real_isinf (&m_min))
673 return false;
674 REAL_VALUE_TYPE r;
675 real_convert (&r, DFmode, &m_min);
676 if (real_identical (&r, &m_min))
677 return false;
680 if (result)
681 *result = build_real (m_type, m_min);
682 return true;
684 return false;
687 bool
688 frange::supports_type_p (const_tree type) const
690 return supports_p (type);
693 void
694 frange::verify_range ()
696 switch (m_kind)
698 case VR_UNDEFINED:
699 gcc_checking_assert (!m_type);
700 return;
701 case VR_VARYING:
702 gcc_checking_assert (m_type);
703 gcc_checking_assert (m_pos_nan && m_neg_nan);
704 gcc_checking_assert (frange_val_is_min (m_min, m_type));
705 gcc_checking_assert (frange_val_is_max (m_max, m_type));
706 return;
707 case VR_RANGE:
708 gcc_checking_assert (m_type);
709 break;
710 case VR_NAN:
711 gcc_checking_assert (m_type);
712 gcc_checking_assert (m_pos_nan || m_neg_nan);
713 return;
714 default:
715 gcc_unreachable ();
718 // NANs cannot appear in the endpoints of a range.
719 gcc_checking_assert (!real_isnan (&m_min) && !real_isnan (&m_max));
721 // Make sure we don't have swapped ranges.
722 gcc_checking_assert (!real_less (&m_max, &m_min));
724 // [ +0.0, -0.0 ] is nonsensical.
725 gcc_checking_assert (!(real_iszero (&m_min, 0) && real_iszero (&m_max, 1)));
727 // If all the properties are clear, we better not span the entire
728 // domain, because that would make us varying.
729 if (m_pos_nan && m_neg_nan)
730 gcc_checking_assert (!frange_val_is_min (m_min, m_type)
731 || !frange_val_is_max (m_max, m_type));
734 // We can't do much with nonzeros yet.
735 void
736 frange::set_nonzero (tree type)
738 set_varying (type);
741 // We can't do much with nonzeros yet.
742 bool
743 frange::nonzero_p () const
745 return false;
748 // Set range to [+0.0, +0.0] if honoring signed zeros, or [0.0, 0.0]
749 // otherwise.
751 void
752 frange::set_zero (tree type)
754 if (HONOR_SIGNED_ZEROS (type))
756 REAL_VALUE_TYPE dconstm0 = dconst0;
757 dconstm0.sign = 1;
758 set (type, dconstm0, dconst0);
759 clear_nan ();
761 else
762 set (type, dconst0, dconst0);
765 // Return TRUE for any zero regardless of sign.
767 bool
768 frange::zero_p () const
770 return (m_kind == VR_RANGE
771 && real_iszero (&m_min)
772 && real_iszero (&m_max));
775 void
776 frange::set_nonnegative (tree type)
778 set (type, dconst0, frange_val_max (type));
780 // Set +NAN as the only possibility.
781 if (HONOR_NANS (type))
782 update_nan (/*sign=*/0);
785 // Here we copy between any two irange's. The ranges can be legacy or
786 // multi-ranges, and copying between any combination works correctly.
788 irange &
789 irange::operator= (const irange &src)
791 if (legacy_mode_p ())
793 copy_to_legacy (src);
794 return *this;
796 if (src.legacy_mode_p ())
798 copy_legacy_to_multi_range (src);
799 return *this;
802 unsigned x;
803 unsigned lim = src.m_num_ranges;
804 if (lim > m_max_ranges)
805 lim = m_max_ranges;
807 for (x = 0; x < lim * 2; ++x)
808 m_base[x] = src.m_base[x];
810 // If the range didn't fit, the last range should cover the rest.
811 if (lim != src.m_num_ranges)
812 m_base[x - 1] = src.m_base[src.m_num_ranges * 2 - 1];
814 m_num_ranges = lim;
815 m_kind = src.m_kind;
816 m_nonzero_mask = src.m_nonzero_mask;
817 if (flag_checking)
818 verify_range ();
819 return *this;
822 // Return TRUE if range is a multi-range that can be represented as a
823 // VR_ANTI_RANGE.
825 bool
826 irange::maybe_anti_range () const
828 tree ttype = type ();
829 unsigned int precision = TYPE_PRECISION (ttype);
830 signop sign = TYPE_SIGN (ttype);
831 return (num_pairs () > 1
832 && precision > 1
833 && lower_bound () == wi::min_value (precision, sign)
834 && upper_bound () == wi::max_value (precision, sign));
837 void
838 irange::copy_legacy_to_multi_range (const irange &src)
840 gcc_checking_assert (src.legacy_mode_p ());
841 gcc_checking_assert (!legacy_mode_p ());
842 if (src.undefined_p ())
843 set_undefined ();
844 else if (src.varying_p ())
845 set_varying (src.type ());
846 else
848 if (range_has_numeric_bounds_p (&src))
849 set (src.min (), src.max (), src.kind ());
850 else
852 value_range cst (src);
853 cst.normalize_symbolics ();
854 gcc_checking_assert (cst.varying_p () || cst.kind () == VR_RANGE);
855 set (cst.min (), cst.max ());
860 // Copy any type of irange into a legacy.
862 void
863 irange::copy_to_legacy (const irange &src)
865 gcc_checking_assert (legacy_mode_p ());
866 // Handle legacy to legacy and other things that are easy to copy.
867 if (src.legacy_mode_p () || src.varying_p () || src.undefined_p ())
869 m_num_ranges = src.m_num_ranges;
870 m_base[0] = src.m_base[0];
871 m_base[1] = src.m_base[1];
872 m_kind = src.m_kind;
873 m_nonzero_mask = src.m_nonzero_mask;
874 return;
876 // Copy multi-range to legacy.
877 if (src.maybe_anti_range ())
879 int_range<3> r (src);
880 r.invert ();
881 // Use tree variants to save on tree -> wi -> tree conversions.
882 set (r.tree_lower_bound (0), r.tree_upper_bound (0), VR_ANTI_RANGE);
884 else
885 set (src.tree_lower_bound (), src.tree_upper_bound ());
888 // Swap MIN/MAX if they are out of order and adjust KIND appropriately.
890 static void
891 swap_out_of_order_endpoints (tree &min, tree &max, value_range_kind &kind)
893 gcc_checking_assert (kind != VR_UNDEFINED);
894 if (kind == VR_VARYING)
895 return;
896 /* Wrong order for min and max, to swap them and the VR type we need
897 to adjust them. */
898 if (tree_int_cst_lt (max, min))
900 tree one, tmp;
902 /* For one bit precision if max < min, then the swapped
903 range covers all values, so for VR_RANGE it is varying and
904 for VR_ANTI_RANGE empty range, so drop to varying as well. */
905 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
907 kind = VR_VARYING;
908 return;
911 one = build_int_cst (TREE_TYPE (min), 1);
912 tmp = int_const_binop (PLUS_EXPR, max, one);
913 max = int_const_binop (MINUS_EXPR, min, one);
914 min = tmp;
916 /* There's one corner case, if we had [C+1, C] before we now have
917 that again. But this represents an empty value range, so drop
918 to varying in this case. */
919 if (tree_int_cst_lt (max, min))
921 kind = VR_VARYING;
922 return;
924 kind = kind == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
928 void
929 irange::irange_set (tree min, tree max)
931 gcc_checking_assert (!POLY_INT_CST_P (min));
932 gcc_checking_assert (!POLY_INT_CST_P (max));
934 m_base[0] = min;
935 m_base[1] = max;
936 m_num_ranges = 1;
937 m_kind = VR_RANGE;
938 m_nonzero_mask = NULL;
939 normalize_kind ();
941 if (flag_checking)
942 verify_range ();
945 void
946 irange::irange_set_1bit_anti_range (tree min, tree max)
948 tree type = TREE_TYPE (min);
949 gcc_checking_assert (TYPE_PRECISION (type) == 1);
951 if (operand_equal_p (min, max))
953 // Since these are 1-bit quantities, they can only be [MIN,MIN]
954 // or [MAX,MAX].
955 if (vrp_val_is_min (min))
956 min = max = vrp_val_max (type);
957 else
958 min = max = vrp_val_min (type);
959 set (min, max);
961 else
963 // The only alternative is [MIN,MAX], which is the empty range.
964 gcc_checking_assert (vrp_val_is_min (min));
965 gcc_checking_assert (vrp_val_is_max (max));
966 set_undefined ();
968 if (flag_checking)
969 verify_range ();
972 void
973 irange::irange_set_anti_range (tree min, tree max)
975 gcc_checking_assert (!POLY_INT_CST_P (min));
976 gcc_checking_assert (!POLY_INT_CST_P (max));
978 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
980 irange_set_1bit_anti_range (min, max);
981 return;
984 // set an anti-range
985 tree type = TREE_TYPE (min);
986 signop sign = TYPE_SIGN (type);
987 int_range<2> type_range (type);
988 // Calculate INVERSE([I,J]) as [-MIN, I-1][J+1, +MAX].
989 m_num_ranges = 0;
990 wi::overflow_type ovf;
992 wide_int w_min = wi::to_wide (min);
993 if (wi::ne_p (w_min, type_range.lower_bound ()))
995 wide_int lim1 = wi::sub (w_min, 1, sign, &ovf);
996 gcc_checking_assert (ovf != wi::OVF_OVERFLOW);
997 m_base[0] = type_range.tree_lower_bound (0);
998 m_base[1] = wide_int_to_tree (type, lim1);
999 m_num_ranges = 1;
1001 wide_int w_max = wi::to_wide (max);
1002 if (wi::ne_p (w_max, type_range.upper_bound ()))
1004 wide_int lim2 = wi::add (w_max, 1, sign, &ovf);
1005 gcc_checking_assert (ovf != wi::OVF_OVERFLOW);
1006 m_base[m_num_ranges * 2] = wide_int_to_tree (type, lim2);
1007 m_base[m_num_ranges * 2 + 1] = type_range.tree_upper_bound (0);
1008 ++m_num_ranges;
1011 m_kind = VR_RANGE;
1012 m_nonzero_mask = NULL;
1013 normalize_kind ();
1015 if (flag_checking)
1016 verify_range ();
1019 /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
1020 This means adjusting VRTYPE, MIN and MAX representing the case of a
1021 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
1022 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
1023 In corner cases where MAX+1 or MIN-1 wraps this will fall back
1024 to varying.
1025 This routine exists to ease canonicalization in the case where we
1026 extract ranges from var + CST op limit. */
1028 void
1029 irange::set (tree min, tree max, value_range_kind kind)
1031 if (kind == VR_UNDEFINED)
1033 irange::set_undefined ();
1034 return;
1037 if (kind == VR_VARYING
1038 || POLY_INT_CST_P (min)
1039 || POLY_INT_CST_P (max))
1041 set_varying (TREE_TYPE (min));
1042 return;
1045 if (TREE_OVERFLOW_P (min))
1046 min = drop_tree_overflow (min);
1047 if (TREE_OVERFLOW_P (max))
1048 max = drop_tree_overflow (max);
1050 if (!legacy_mode_p ())
1052 if (kind == VR_RANGE)
1053 irange_set (min, max);
1054 else
1056 gcc_checking_assert (kind == VR_ANTI_RANGE);
1057 irange_set_anti_range (min, max);
1059 return;
1061 // Nothing to canonicalize for symbolic ranges.
1062 if (TREE_CODE (min) != INTEGER_CST
1063 || TREE_CODE (max) != INTEGER_CST)
1065 m_kind = kind;
1066 m_base[0] = min;
1067 m_base[1] = max;
1068 m_num_ranges = 1;
1069 m_nonzero_mask = NULL;
1070 return;
1073 swap_out_of_order_endpoints (min, max, kind);
1074 if (kind == VR_VARYING)
1076 set_varying (TREE_TYPE (min));
1077 return;
1080 // Anti-ranges that can be represented as ranges should be so.
1081 if (kind == VR_ANTI_RANGE)
1083 bool is_min = vrp_val_is_min (min);
1084 bool is_max = vrp_val_is_max (max);
1086 if (is_min && is_max)
1088 // Fall through. This will either be normalized as
1089 // VR_UNDEFINED if the anti-range spans the entire
1090 // precision, or it will remain an VR_ANTI_RANGE in the case
1091 // of an -fstrict-enum where [MIN,MAX] is less than the span
1092 // of underlying precision.
1094 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
1096 irange_set_1bit_anti_range (min, max);
1097 return;
1099 else if (is_min)
1101 tree one = build_int_cst (TREE_TYPE (max), 1);
1102 min = int_const_binop (PLUS_EXPR, max, one);
1103 max = vrp_val_max (TREE_TYPE (max));
1104 kind = VR_RANGE;
1106 else if (is_max)
1108 tree one = build_int_cst (TREE_TYPE (min), 1);
1109 max = int_const_binop (MINUS_EXPR, min, one);
1110 min = vrp_val_min (TREE_TYPE (min));
1111 kind = VR_RANGE;
1115 m_kind = kind;
1116 m_base[0] = min;
1117 m_base[1] = max;
1118 m_num_ranges = 1;
1119 m_nonzero_mask = NULL;
1120 normalize_kind ();
1121 if (flag_checking)
1122 verify_range ();
1125 // Check the validity of the range.
1127 void
1128 irange::verify_range ()
1130 gcc_checking_assert (m_discriminator == VR_IRANGE);
1131 if (m_kind == VR_UNDEFINED)
1133 gcc_checking_assert (m_num_ranges == 0);
1134 return;
1136 if (m_kind == VR_VARYING)
1138 gcc_checking_assert (!m_nonzero_mask
1139 || wi::to_wide (m_nonzero_mask) == -1);
1140 gcc_checking_assert (m_num_ranges == 1);
1141 gcc_checking_assert (varying_compatible_p ());
1142 return;
1144 if (!legacy_mode_p ())
1146 gcc_checking_assert (m_num_ranges != 0);
1147 gcc_checking_assert (!varying_compatible_p ());
1148 for (unsigned i = 0; i < m_num_ranges; ++i)
1150 tree lb = tree_lower_bound (i);
1151 tree ub = tree_upper_bound (i);
1152 int c = compare_values (lb, ub);
1153 gcc_checking_assert (c == 0 || c == -1);
1155 return;
1157 if (m_kind == VR_RANGE || m_kind == VR_ANTI_RANGE)
1159 gcc_checking_assert (m_num_ranges == 1);
1160 int cmp = compare_values (tree_lower_bound (0), tree_upper_bound (0));
1161 gcc_checking_assert (cmp == 0 || cmp == -1 || cmp == -2);
1165 // Return the lower bound for a sub-range. PAIR is the sub-range in
1166 // question.
1168 wide_int
1169 irange::legacy_lower_bound (unsigned pair) const
1171 gcc_checking_assert (legacy_mode_p ());
1172 if (symbolic_p ())
1174 value_range numeric_range (*this);
1175 numeric_range.normalize_symbolics ();
1176 return numeric_range.legacy_lower_bound (pair);
1178 gcc_checking_assert (m_num_ranges > 0);
1179 gcc_checking_assert (pair + 1 <= num_pairs ());
1180 if (m_kind == VR_ANTI_RANGE)
1182 tree typ = type (), t;
1183 if (pair == 1 || vrp_val_is_min (min ()))
1184 t = wide_int_to_tree (typ, wi::to_wide (max ()) + 1);
1185 else
1186 t = vrp_val_min (typ);
1187 return wi::to_wide (t);
1189 return wi::to_wide (tree_lower_bound (pair));
1192 // Return the upper bound for a sub-range. PAIR is the sub-range in
1193 // question.
1195 wide_int
1196 irange::legacy_upper_bound (unsigned pair) const
1198 gcc_checking_assert (legacy_mode_p ());
1199 if (symbolic_p ())
1201 value_range numeric_range (*this);
1202 numeric_range.normalize_symbolics ();
1203 return numeric_range.legacy_upper_bound (pair);
1205 gcc_checking_assert (m_num_ranges > 0);
1206 gcc_checking_assert (pair + 1 <= num_pairs ());
1207 if (m_kind == VR_ANTI_RANGE)
1209 tree typ = type (), t;
1210 if (pair == 1 || vrp_val_is_min (min ()))
1211 t = vrp_val_max (typ);
1212 else
1213 t = wide_int_to_tree (typ, wi::to_wide (min ()) - 1);
1214 return wi::to_wide (t);
1216 return wi::to_wide (tree_upper_bound (pair));
1219 bool
1220 irange::legacy_equal_p (const irange &other) const
1222 gcc_checking_assert (legacy_mode_p () && other.legacy_mode_p ());
1224 if (m_kind != other.m_kind)
1225 return false;
1226 if (m_kind == VR_UNDEFINED)
1227 return true;
1228 if (m_kind == VR_VARYING)
1229 return range_compatible_p (type (), other.type ());
1230 return (vrp_operand_equal_p (tree_lower_bound (0),
1231 other.tree_lower_bound (0))
1232 && vrp_operand_equal_p (tree_upper_bound (0),
1233 other.tree_upper_bound (0))
1234 && get_nonzero_bits () == other.get_nonzero_bits ());
1237 bool
1238 irange::operator== (const irange &other) const
1240 if (legacy_mode_p ())
1242 if (other.legacy_mode_p ())
1243 return legacy_equal_p (other);
1244 value_range tmp (other);
1245 return legacy_equal_p (tmp);
1247 if (other.legacy_mode_p ())
1249 value_range tmp2 (*this);
1250 return tmp2.legacy_equal_p (other);
1253 if (m_num_ranges != other.m_num_ranges)
1254 return false;
1256 if (m_num_ranges == 0)
1257 return true;
1259 for (unsigned i = 0; i < m_num_ranges; ++i)
1261 tree lb = tree_lower_bound (i);
1262 tree ub = tree_upper_bound (i);
1263 tree lb_other = other.tree_lower_bound (i);
1264 tree ub_other = other.tree_upper_bound (i);
1265 if (!operand_equal_p (lb, lb_other, 0)
1266 || !operand_equal_p (ub, ub_other, 0))
1267 return false;
1269 return get_nonzero_bits () == other.get_nonzero_bits ();
1272 /* Return TRUE if this is a symbolic range. */
1274 bool
1275 irange::symbolic_p () const
1277 return (m_num_ranges > 0
1278 && (!is_gimple_min_invariant (min ())
1279 || !is_gimple_min_invariant (max ())));
1282 /* Return TRUE if this is a constant range. */
1284 bool
1285 irange::constant_p () const
1287 return (m_num_ranges > 0
1288 && TREE_CODE (min ()) == INTEGER_CST
1289 && TREE_CODE (max ()) == INTEGER_CST);
1292 /* If range is a singleton, place it in RESULT and return TRUE.
1293 Note: A singleton can be any gimple invariant, not just constants.
1294 So, [&x, &x] counts as a singleton. */
1296 bool
1297 irange::singleton_p (tree *result) const
1299 if (!legacy_mode_p ())
1301 if (num_pairs () == 1 && (wi::to_wide (tree_lower_bound ())
1302 == wi::to_wide (tree_upper_bound ())))
1304 if (result)
1305 *result = tree_lower_bound ();
1306 return true;
1308 return false;
1310 if (m_kind == VR_ANTI_RANGE)
1312 if (nonzero_p ())
1314 if (TYPE_PRECISION (type ()) == 1)
1316 if (result)
1317 *result = max ();
1318 return true;
1320 return false;
1322 if (num_pairs () == 1)
1324 value_range vr0, vr1;
1325 ranges_from_anti_range ((const value_range *) this, &vr0, &vr1);
1326 return vr0.singleton_p (result);
1329 // Catches non-numeric extremes as well.
1330 if (m_kind == VR_RANGE
1331 && vrp_operand_equal_p (min (), max ())
1332 && is_gimple_min_invariant (min ()))
1334 if (result)
1335 *result = min ();
1336 return true;
1338 return false;
1341 /* Return 1 if VAL is inside value range.
1342 0 if VAL is not inside value range.
1343 -2 if we cannot tell either way.
1345 Benchmark compile/20001226-1.c compilation time after changing this
1346 function. */
1349 irange::value_inside_range (tree val) const
1351 if (varying_p ())
1352 return 1;
1354 if (undefined_p ())
1355 return 0;
1357 if (!legacy_mode_p () && TREE_CODE (val) == INTEGER_CST)
1358 return contains_p (val);
1360 int cmp1 = operand_less_p (val, min ());
1361 if (cmp1 == -2)
1362 return -2;
1363 if (cmp1 == 1)
1364 return m_kind != VR_RANGE;
1366 int cmp2 = operand_less_p (max (), val);
1367 if (cmp2 == -2)
1368 return -2;
1370 if (m_kind == VR_RANGE)
1371 return !cmp2;
1372 else
1373 return !!cmp2;
1376 /* Return TRUE if it is possible that range contains VAL. */
1378 bool
1379 irange::may_contain_p (tree val) const
1381 return value_inside_range (val) != 0;
1384 /* Return TRUE if range contains INTEGER_CST. */
1385 /* Return 1 if VAL is inside value range.
1386 0 if VAL is not inside value range.
1388 Benchmark compile/20001226-1.c compilation time after changing this
1389 function. */
1392 bool
1393 irange::contains_p (tree cst) const
1395 if (undefined_p ())
1396 return false;
1398 if (legacy_mode_p ())
1400 gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST);
1401 if (symbolic_p ())
1403 value_range numeric_range (*this);
1404 numeric_range.normalize_symbolics ();
1405 return numeric_range.contains_p (cst);
1407 return value_inside_range (cst) == 1;
1410 gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST);
1412 // See if we can exclude CST based on the nonzero bits.
1413 if (m_nonzero_mask)
1415 wide_int cstw = wi::to_wide (cst);
1416 if (cstw != 0 && wi::bit_and (wi::to_wide (m_nonzero_mask), cstw) == 0)
1417 return false;
1420 signop sign = TYPE_SIGN (TREE_TYPE (cst));
1421 wide_int v = wi::to_wide (cst);
1422 for (unsigned r = 0; r < m_num_ranges; ++r)
1424 if (wi::lt_p (v, lower_bound (r), sign))
1425 return false;
1426 if (wi::le_p (v, upper_bound (r), sign))
1427 return true;
1430 return false;
1434 /* Normalize addresses into constants. */
1436 void
1437 irange::normalize_addresses ()
1439 if (undefined_p ())
1440 return;
1442 if (!POINTER_TYPE_P (type ()) || range_has_numeric_bounds_p (this))
1443 return;
1445 if (!range_includes_zero_p (this))
1447 gcc_checking_assert (TREE_CODE (min ()) == ADDR_EXPR
1448 || TREE_CODE (max ()) == ADDR_EXPR);
1449 set_nonzero (type ());
1450 return;
1452 set_varying (type ());
1455 /* Normalize symbolics and addresses into constants. */
1457 void
1458 irange::normalize_symbolics ()
1460 if (varying_p () || undefined_p ())
1461 return;
1463 tree ttype = type ();
1464 bool min_symbolic = !is_gimple_min_invariant (min ());
1465 bool max_symbolic = !is_gimple_min_invariant (max ());
1466 if (!min_symbolic && !max_symbolic)
1468 normalize_addresses ();
1469 return;
1472 // [SYM, SYM] -> VARYING
1473 if (min_symbolic && max_symbolic)
1475 set_varying (ttype);
1476 return;
1478 if (kind () == VR_RANGE)
1480 // [SYM, NUM] -> [-MIN, NUM]
1481 if (min_symbolic)
1483 set (vrp_val_min (ttype), max ());
1484 return;
1486 // [NUM, SYM] -> [NUM, +MAX]
1487 set (min (), vrp_val_max (ttype));
1488 return;
1490 gcc_checking_assert (kind () == VR_ANTI_RANGE);
1491 // ~[SYM, NUM] -> [NUM + 1, +MAX]
1492 if (min_symbolic)
1494 if (!vrp_val_is_max (max ()))
1496 tree n = wide_int_to_tree (ttype, wi::to_wide (max ()) + 1);
1497 set (n, vrp_val_max (ttype));
1498 return;
1500 set_varying (ttype);
1501 return;
1503 // ~[NUM, SYM] -> [-MIN, NUM - 1]
1504 if (!vrp_val_is_min (min ()))
1506 tree n = wide_int_to_tree (ttype, wi::to_wide (min ()) - 1);
1507 set (vrp_val_min (ttype), n);
1508 return;
1510 set_varying (ttype);
1513 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
1514 { VR1TYPE, VR0MIN, VR0MAX } and store the result
1515 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
1516 possible such range. The resulting range is not canonicalized. */
1518 static void
1519 intersect_ranges (enum value_range_kind *vr0type,
1520 tree *vr0min, tree *vr0max,
1521 enum value_range_kind vr1type,
1522 tree vr1min, tree vr1max)
1524 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
1525 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
1527 /* [] is vr0, () is vr1 in the following classification comments. */
1528 if (mineq && maxeq)
1530 /* [( )] */
1531 if (*vr0type == vr1type)
1532 /* Nothing to do for equal ranges. */
1534 else if ((*vr0type == VR_RANGE
1535 && vr1type == VR_ANTI_RANGE)
1536 || (*vr0type == VR_ANTI_RANGE
1537 && vr1type == VR_RANGE))
1539 /* For anti-range with range intersection the result is empty. */
1540 *vr0type = VR_UNDEFINED;
1541 *vr0min = NULL_TREE;
1542 *vr0max = NULL_TREE;
1544 else
1545 gcc_unreachable ();
1547 else if (operand_less_p (*vr0max, vr1min) == 1
1548 || operand_less_p (vr1max, *vr0min) == 1)
1550 /* [ ] ( ) or ( ) [ ]
1551 If the ranges have an empty intersection, the result of the
1552 intersect operation is the range for intersecting an
1553 anti-range with a range or empty when intersecting two ranges. */
1554 if (*vr0type == VR_RANGE
1555 && vr1type == VR_ANTI_RANGE)
1557 else if (*vr0type == VR_ANTI_RANGE
1558 && vr1type == VR_RANGE)
1560 *vr0type = vr1type;
1561 *vr0min = vr1min;
1562 *vr0max = vr1max;
1564 else if (*vr0type == VR_RANGE
1565 && vr1type == VR_RANGE)
1567 *vr0type = VR_UNDEFINED;
1568 *vr0min = NULL_TREE;
1569 *vr0max = NULL_TREE;
1571 else if (*vr0type == VR_ANTI_RANGE
1572 && vr1type == VR_ANTI_RANGE)
1574 /* If the anti-ranges are adjacent to each other merge them. */
1575 if (TREE_CODE (*vr0max) == INTEGER_CST
1576 && TREE_CODE (vr1min) == INTEGER_CST
1577 && operand_less_p (*vr0max, vr1min) == 1
1578 && integer_onep (int_const_binop (MINUS_EXPR,
1579 vr1min, *vr0max)))
1580 *vr0max = vr1max;
1581 else if (TREE_CODE (vr1max) == INTEGER_CST
1582 && TREE_CODE (*vr0min) == INTEGER_CST
1583 && operand_less_p (vr1max, *vr0min) == 1
1584 && integer_onep (int_const_binop (MINUS_EXPR,
1585 *vr0min, vr1max)))
1586 *vr0min = vr1min;
1587 /* Else arbitrarily take VR0. */
1590 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
1591 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
1593 /* [ ( ) ] or [( ) ] or [ ( )] */
1594 if (*vr0type == VR_RANGE
1595 && vr1type == VR_RANGE)
1597 /* If both are ranges the result is the inner one. */
1598 *vr0type = vr1type;
1599 *vr0min = vr1min;
1600 *vr0max = vr1max;
1602 else if (*vr0type == VR_RANGE
1603 && vr1type == VR_ANTI_RANGE)
1605 /* Choose the right gap if the left one is empty. */
1606 if (mineq)
1608 if (TREE_CODE (vr1max) != INTEGER_CST)
1609 *vr0min = vr1max;
1610 else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1
1611 && !TYPE_UNSIGNED (TREE_TYPE (vr1max)))
1612 *vr0min
1613 = int_const_binop (MINUS_EXPR, vr1max,
1614 build_int_cst (TREE_TYPE (vr1max), -1));
1615 else
1616 *vr0min
1617 = int_const_binop (PLUS_EXPR, vr1max,
1618 build_int_cst (TREE_TYPE (vr1max), 1));
1620 /* Choose the left gap if the right one is empty. */
1621 else if (maxeq)
1623 if (TREE_CODE (vr1min) != INTEGER_CST)
1624 *vr0max = vr1min;
1625 else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1
1626 && !TYPE_UNSIGNED (TREE_TYPE (vr1min)))
1627 *vr0max
1628 = int_const_binop (PLUS_EXPR, vr1min,
1629 build_int_cst (TREE_TYPE (vr1min), -1));
1630 else
1631 *vr0max
1632 = int_const_binop (MINUS_EXPR, vr1min,
1633 build_int_cst (TREE_TYPE (vr1min), 1));
1635 /* Choose the anti-range if the range is effectively varying. */
1636 else if (vrp_val_is_min (*vr0min)
1637 && vrp_val_is_max (*vr0max))
1639 *vr0type = vr1type;
1640 *vr0min = vr1min;
1641 *vr0max = vr1max;
1643 /* Else choose the range. */
1645 else if (*vr0type == VR_ANTI_RANGE
1646 && vr1type == VR_ANTI_RANGE)
1647 /* If both are anti-ranges the result is the outer one. */
1649 else if (*vr0type == VR_ANTI_RANGE
1650 && vr1type == VR_RANGE)
1652 /* The intersection is empty. */
1653 *vr0type = VR_UNDEFINED;
1654 *vr0min = NULL_TREE;
1655 *vr0max = NULL_TREE;
1657 else
1658 gcc_unreachable ();
1660 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
1661 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
1663 /* ( [ ] ) or ([ ] ) or ( [ ]) */
1664 if (*vr0type == VR_RANGE
1665 && vr1type == VR_RANGE)
1666 /* Choose the inner range. */
1668 else if (*vr0type == VR_ANTI_RANGE
1669 && vr1type == VR_RANGE)
1671 /* Choose the right gap if the left is empty. */
1672 if (mineq)
1674 *vr0type = VR_RANGE;
1675 if (TREE_CODE (*vr0max) != INTEGER_CST)
1676 *vr0min = *vr0max;
1677 else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1
1678 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max)))
1679 *vr0min
1680 = int_const_binop (MINUS_EXPR, *vr0max,
1681 build_int_cst (TREE_TYPE (*vr0max), -1));
1682 else
1683 *vr0min
1684 = int_const_binop (PLUS_EXPR, *vr0max,
1685 build_int_cst (TREE_TYPE (*vr0max), 1));
1686 *vr0max = vr1max;
1688 /* Choose the left gap if the right is empty. */
1689 else if (maxeq)
1691 *vr0type = VR_RANGE;
1692 if (TREE_CODE (*vr0min) != INTEGER_CST)
1693 *vr0max = *vr0min;
1694 else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1
1695 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min)))
1696 *vr0max
1697 = int_const_binop (PLUS_EXPR, *vr0min,
1698 build_int_cst (TREE_TYPE (*vr0min), -1));
1699 else
1700 *vr0max
1701 = int_const_binop (MINUS_EXPR, *vr0min,
1702 build_int_cst (TREE_TYPE (*vr0min), 1));
1703 *vr0min = vr1min;
1705 /* Choose the anti-range if the range is effectively varying. */
1706 else if (vrp_val_is_min (vr1min)
1707 && vrp_val_is_max (vr1max))
1709 /* Choose the anti-range if it is ~[0,0], that range is special
1710 enough to special case when vr1's range is relatively wide.
1711 At least for types bigger than int - this covers pointers
1712 and arguments to functions like ctz. */
1713 else if (*vr0min == *vr0max
1714 && integer_zerop (*vr0min)
1715 && ((TYPE_PRECISION (TREE_TYPE (*vr0min))
1716 >= TYPE_PRECISION (integer_type_node))
1717 || POINTER_TYPE_P (TREE_TYPE (*vr0min)))
1718 && TREE_CODE (vr1max) == INTEGER_CST
1719 && TREE_CODE (vr1min) == INTEGER_CST
1720 && (wi::clz (wi::to_wide (vr1max) - wi::to_wide (vr1min))
1721 < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2))
1723 /* Else choose the range. */
1724 else
1726 *vr0type = vr1type;
1727 *vr0min = vr1min;
1728 *vr0max = vr1max;
1731 else if (*vr0type == VR_ANTI_RANGE
1732 && vr1type == VR_ANTI_RANGE)
1734 /* If both are anti-ranges the result is the outer one. */
1735 *vr0type = vr1type;
1736 *vr0min = vr1min;
1737 *vr0max = vr1max;
1739 else if (vr1type == VR_ANTI_RANGE
1740 && *vr0type == VR_RANGE)
1742 /* The intersection is empty. */
1743 *vr0type = VR_UNDEFINED;
1744 *vr0min = NULL_TREE;
1745 *vr0max = NULL_TREE;
1747 else
1748 gcc_unreachable ();
1750 else if ((operand_less_p (vr1min, *vr0max) == 1
1751 || operand_equal_p (vr1min, *vr0max, 0))
1752 && operand_less_p (*vr0min, vr1min) == 1
1753 && operand_less_p (*vr0max, vr1max) == 1)
1755 /* [ ( ] ) or [ ]( ) */
1756 if (*vr0type == VR_ANTI_RANGE
1757 && vr1type == VR_ANTI_RANGE)
1758 *vr0max = vr1max;
1759 else if (*vr0type == VR_RANGE
1760 && vr1type == VR_RANGE)
1761 *vr0min = vr1min;
1762 else if (*vr0type == VR_RANGE
1763 && vr1type == VR_ANTI_RANGE)
1765 if (TREE_CODE (vr1min) == INTEGER_CST)
1766 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
1767 build_int_cst (TREE_TYPE (vr1min), 1));
1768 else
1769 *vr0max = vr1min;
1771 else if (*vr0type == VR_ANTI_RANGE
1772 && vr1type == VR_RANGE)
1774 *vr0type = VR_RANGE;
1775 if (TREE_CODE (*vr0max) == INTEGER_CST)
1776 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
1777 build_int_cst (TREE_TYPE (*vr0max), 1));
1778 else
1779 *vr0min = *vr0max;
1780 *vr0max = vr1max;
1782 else
1783 gcc_unreachable ();
1785 else if ((operand_less_p (*vr0min, vr1max) == 1
1786 || operand_equal_p (*vr0min, vr1max, 0))
1787 && operand_less_p (vr1min, *vr0min) == 1
1788 && operand_less_p (vr1max, *vr0max) == 1)
1790 /* ( [ ) ] or ( )[ ] */
1791 if (*vr0type == VR_ANTI_RANGE
1792 && vr1type == VR_ANTI_RANGE)
1793 *vr0min = vr1min;
1794 else if (*vr0type == VR_RANGE
1795 && vr1type == VR_RANGE)
1796 *vr0max = vr1max;
1797 else if (*vr0type == VR_RANGE
1798 && vr1type == VR_ANTI_RANGE)
1800 if (TREE_CODE (vr1max) == INTEGER_CST)
1801 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
1802 build_int_cst (TREE_TYPE (vr1max), 1));
1803 else
1804 *vr0min = vr1max;
1806 else if (*vr0type == VR_ANTI_RANGE
1807 && vr1type == VR_RANGE)
1809 *vr0type = VR_RANGE;
1810 if (TREE_CODE (*vr0min) == INTEGER_CST)
1811 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
1812 build_int_cst (TREE_TYPE (*vr0min), 1));
1813 else
1814 *vr0max = *vr0min;
1815 *vr0min = vr1min;
1817 else
1818 gcc_unreachable ();
1821 /* If we know the intersection is empty, there's no need to
1822 conservatively add anything else to the set. */
1823 if (*vr0type == VR_UNDEFINED)
1824 return;
1826 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
1827 result for the intersection. That's always a conservative
1828 correct estimate unless VR1 is a constant singleton range
1829 in which case we choose that. */
1830 if (vr1type == VR_RANGE
1831 && is_gimple_min_invariant (vr1min)
1832 && vrp_operand_equal_p (vr1min, vr1max))
1834 *vr0type = vr1type;
1835 *vr0min = vr1min;
1836 *vr0max = vr1max;
1840 /* Helper for the intersection operation for value ranges. Given two
1841 ranges VR0 and VR1, set VR0 to the intersection of both ranges.
1842 This may not be the smallest possible such range. */
1844 void
1845 irange::legacy_intersect (irange *vr0, const irange *vr1)
1847 gcc_checking_assert (vr0->legacy_mode_p ());
1848 gcc_checking_assert (vr1->legacy_mode_p ());
1849 /* If either range is VR_VARYING the other one wins. */
1850 if (vr1->varying_p ())
1851 return;
1852 if (vr0->varying_p ())
1854 vr0->set (vr1->min (), vr1->max (), vr1->kind ());
1855 return;
1858 /* When either range is VR_UNDEFINED the resulting range is
1859 VR_UNDEFINED, too. */
1860 if (vr0->undefined_p ())
1861 return;
1862 if (vr1->undefined_p ())
1864 vr0->set_undefined ();
1865 return;
1868 value_range_kind vr0kind = vr0->kind ();
1869 tree vr0min = vr0->min ();
1870 tree vr0max = vr0->max ();
1872 intersect_ranges (&vr0kind, &vr0min, &vr0max,
1873 vr1->kind (), vr1->min (), vr1->max ());
1875 /* Make sure to canonicalize the result though as the inversion of a
1876 VR_RANGE can still be a VR_RANGE. */
1877 if (vr0kind == VR_UNDEFINED)
1878 vr0->set_undefined ();
1879 else if (vr0kind == VR_VARYING)
1881 /* If we failed, use the original VR0. */
1882 return;
1884 else
1885 vr0->set (vr0min, vr0max, vr0kind);
1888 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
1889 { VR1TYPE, VR0MIN, VR0MAX } and store the result
1890 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
1891 possible such range. The resulting range is not canonicalized. */
1893 static void
1894 union_ranges (enum value_range_kind *vr0type,
1895 tree *vr0min, tree *vr0max,
1896 enum value_range_kind vr1type,
1897 tree vr1min, tree vr1max)
1899 int cmpmin = compare_values (*vr0min, vr1min);
1900 int cmpmax = compare_values (*vr0max, vr1max);
1901 bool mineq = cmpmin == 0;
1902 bool maxeq = cmpmax == 0;
1904 /* [] is vr0, () is vr1 in the following classification comments. */
1905 if (mineq && maxeq)
1907 /* [( )] */
1908 if (*vr0type == vr1type)
1909 /* Nothing to do for equal ranges. */
1911 else if ((*vr0type == VR_RANGE
1912 && vr1type == VR_ANTI_RANGE)
1913 || (*vr0type == VR_ANTI_RANGE
1914 && vr1type == VR_RANGE))
1916 /* For anti-range with range union the result is varying. */
1917 goto give_up;
1919 else
1920 gcc_unreachable ();
1922 else if (operand_less_p (*vr0max, vr1min) == 1
1923 || operand_less_p (vr1max, *vr0min) == 1)
1925 /* [ ] ( ) or ( ) [ ]
1926 If the ranges have an empty intersection, result of the union
1927 operation is the anti-range or if both are anti-ranges
1928 it covers all. */
1929 if (*vr0type == VR_ANTI_RANGE
1930 && vr1type == VR_ANTI_RANGE)
1931 goto give_up;
1932 else if (*vr0type == VR_ANTI_RANGE
1933 && vr1type == VR_RANGE)
1935 else if (*vr0type == VR_RANGE
1936 && vr1type == VR_ANTI_RANGE)
1938 *vr0type = vr1type;
1939 *vr0min = vr1min;
1940 *vr0max = vr1max;
1942 else if (*vr0type == VR_RANGE
1943 && vr1type == VR_RANGE)
1945 /* The result is the convex hull of both ranges. */
1946 if (operand_less_p (*vr0max, vr1min) == 1)
1948 /* If the result can be an anti-range, create one. */
1949 if (TREE_CODE (*vr0max) == INTEGER_CST
1950 && TREE_CODE (vr1min) == INTEGER_CST
1951 && vrp_val_is_min (*vr0min)
1952 && vrp_val_is_max (vr1max))
1954 tree min = int_const_binop (PLUS_EXPR,
1955 *vr0max,
1956 build_int_cst (TREE_TYPE (*vr0max), 1));
1957 tree max = int_const_binop (MINUS_EXPR,
1958 vr1min,
1959 build_int_cst (TREE_TYPE (vr1min), 1));
1960 if (!operand_less_p (max, min))
1962 *vr0type = VR_ANTI_RANGE;
1963 *vr0min = min;
1964 *vr0max = max;
1966 else
1967 *vr0max = vr1max;
1969 else
1970 *vr0max = vr1max;
1972 else
1974 /* If the result can be an anti-range, create one. */
1975 if (TREE_CODE (vr1max) == INTEGER_CST
1976 && TREE_CODE (*vr0min) == INTEGER_CST
1977 && vrp_val_is_min (vr1min)
1978 && vrp_val_is_max (*vr0max))
1980 tree min = int_const_binop (PLUS_EXPR,
1981 vr1max,
1982 build_int_cst (TREE_TYPE (vr1max), 1));
1983 tree max = int_const_binop (MINUS_EXPR,
1984 *vr0min,
1985 build_int_cst (TREE_TYPE (*vr0min), 1));
1986 if (!operand_less_p (max, min))
1988 *vr0type = VR_ANTI_RANGE;
1989 *vr0min = min;
1990 *vr0max = max;
1992 else
1993 *vr0min = vr1min;
1995 else
1996 *vr0min = vr1min;
1999 else
2000 gcc_unreachable ();
2002 else if ((maxeq || cmpmax == 1)
2003 && (mineq || cmpmin == -1))
2005 /* [ ( ) ] or [( ) ] or [ ( )] */
2006 if (*vr0type == VR_RANGE
2007 && vr1type == VR_RANGE)
2009 else if (*vr0type == VR_ANTI_RANGE
2010 && vr1type == VR_ANTI_RANGE)
2012 *vr0type = vr1type;
2013 *vr0min = vr1min;
2014 *vr0max = vr1max;
2016 else if (*vr0type == VR_ANTI_RANGE
2017 && vr1type == VR_RANGE)
2019 /* Arbitrarily choose the right or left gap. */
2020 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
2021 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
2022 build_int_cst (TREE_TYPE (vr1min), 1));
2023 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
2024 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
2025 build_int_cst (TREE_TYPE (vr1max), 1));
2026 else
2027 goto give_up;
2029 else if (*vr0type == VR_RANGE
2030 && vr1type == VR_ANTI_RANGE)
2031 /* The result covers everything. */
2032 goto give_up;
2033 else
2034 gcc_unreachable ();
2036 else if ((maxeq || cmpmax == -1)
2037 && (mineq || cmpmin == 1))
2039 /* ( [ ] ) or ([ ] ) or ( [ ]) */
2040 if (*vr0type == VR_RANGE
2041 && vr1type == VR_RANGE)
2043 *vr0type = vr1type;
2044 *vr0min = vr1min;
2045 *vr0max = vr1max;
2047 else if (*vr0type == VR_ANTI_RANGE
2048 && vr1type == VR_ANTI_RANGE)
2050 else if (*vr0type == VR_RANGE
2051 && vr1type == VR_ANTI_RANGE)
2053 *vr0type = VR_ANTI_RANGE;
2054 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
2056 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
2057 build_int_cst (TREE_TYPE (*vr0min), 1));
2058 *vr0min = vr1min;
2060 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
2062 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
2063 build_int_cst (TREE_TYPE (*vr0max), 1));
2064 *vr0max = vr1max;
2066 else
2067 goto give_up;
2069 else if (*vr0type == VR_ANTI_RANGE
2070 && vr1type == VR_RANGE)
2071 /* The result covers everything. */
2072 goto give_up;
2073 else
2074 gcc_unreachable ();
2076 else if (cmpmin == -1
2077 && cmpmax == -1
2078 && (operand_less_p (vr1min, *vr0max) == 1
2079 || operand_equal_p (vr1min, *vr0max, 0)))
2081 /* [ ( ] ) or [ ]( ) */
2082 if (*vr0type == VR_RANGE
2083 && vr1type == VR_RANGE)
2084 *vr0max = vr1max;
2085 else if (*vr0type == VR_ANTI_RANGE
2086 && vr1type == VR_ANTI_RANGE)
2087 *vr0min = vr1min;
2088 else if (*vr0type == VR_ANTI_RANGE
2089 && vr1type == VR_RANGE)
2091 if (TREE_CODE (vr1min) == INTEGER_CST)
2092 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
2093 build_int_cst (TREE_TYPE (vr1min), 1));
2094 else
2095 goto give_up;
2097 else if (*vr0type == VR_RANGE
2098 && vr1type == VR_ANTI_RANGE)
2100 if (TREE_CODE (*vr0max) == INTEGER_CST)
2102 *vr0type = vr1type;
2103 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
2104 build_int_cst (TREE_TYPE (*vr0max), 1));
2105 *vr0max = vr1max;
2107 else
2108 goto give_up;
2110 else
2111 gcc_unreachable ();
2113 else if (cmpmin == 1
2114 && cmpmax == 1
2115 && (operand_less_p (*vr0min, vr1max) == 1
2116 || operand_equal_p (*vr0min, vr1max, 0)))
2118 /* ( [ ) ] or ( )[ ] */
2119 if (*vr0type == VR_RANGE
2120 && vr1type == VR_RANGE)
2121 *vr0min = vr1min;
2122 else if (*vr0type == VR_ANTI_RANGE
2123 && vr1type == VR_ANTI_RANGE)
2124 *vr0max = vr1max;
2125 else if (*vr0type == VR_ANTI_RANGE
2126 && vr1type == VR_RANGE)
2128 if (TREE_CODE (vr1max) == INTEGER_CST)
2129 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
2130 build_int_cst (TREE_TYPE (vr1max), 1));
2131 else
2132 goto give_up;
2134 else if (*vr0type == VR_RANGE
2135 && vr1type == VR_ANTI_RANGE)
2137 if (TREE_CODE (*vr0min) == INTEGER_CST)
2139 *vr0type = vr1type;
2140 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
2141 build_int_cst (TREE_TYPE (*vr0min), 1));
2142 *vr0min = vr1min;
2144 else
2145 goto give_up;
2147 else
2148 gcc_unreachable ();
2150 else
2151 goto give_up;
2153 return;
2155 give_up:
2156 *vr0type = VR_VARYING;
2157 *vr0min = NULL_TREE;
2158 *vr0max = NULL_TREE;
2161 /* Helper for meet operation for value ranges. Given two ranges VR0
2162 and VR1, set VR0 to the union of both ranges. This may not be the
2163 smallest possible such range. */
2165 void
2166 irange::legacy_union (irange *vr0, const irange *vr1)
2168 gcc_checking_assert (vr0->legacy_mode_p ());
2169 gcc_checking_assert (vr1->legacy_mode_p ());
2171 /* VR0 has the resulting range if VR1 is undefined or VR0 is varying. */
2172 if (vr1->undefined_p ()
2173 || vr0->varying_p ())
2174 return;
2176 /* VR1 has the resulting range if VR0 is undefined or VR1 is varying. */
2177 if (vr0->undefined_p ())
2179 vr0->set (vr1->min (), vr1->max (), vr1->kind ());
2180 return;
2183 if (vr1->varying_p ())
2185 vr0->set_varying (vr1->type ());
2186 return;
2189 value_range_kind vr0kind = vr0->kind ();
2190 tree vr0min = vr0->min ();
2191 tree vr0max = vr0->max ();
2193 union_ranges (&vr0kind, &vr0min, &vr0max,
2194 vr1->kind (), vr1->min (), vr1->max ());
2196 if (vr0kind == VR_UNDEFINED)
2197 vr0->set_undefined ();
2198 else if (vr0kind == VR_VARYING)
2200 /* Failed to find an efficient meet. Before giving up and
2201 setting the result to VARYING, see if we can at least derive
2202 a non-zero range. */
2203 if (range_includes_zero_p (vr0) == 0
2204 && range_includes_zero_p (vr1) == 0)
2205 vr0->set_nonzero (vr0->type ());
2206 else
2207 vr0->set_varying (vr0->type ());
2209 else
2210 vr0->set (vr0min, vr0max, vr0kind);
2213 /* Meet operation for value ranges. Given two value ranges VR0 and
2214 VR1, store in VR0 a range that contains both VR0 and VR1. This
2215 may not be the smallest possible such range.
2216 Return TRUE if the original value changes. */
2218 bool
2219 irange::legacy_verbose_union_ (const irange *other)
2221 if (legacy_mode_p ())
2223 if (!other->legacy_mode_p ())
2225 int_range<1> tmp = *other;
2226 legacy_union (this, &tmp);
2227 return true;
2229 if (dump_file && (dump_flags & TDF_DETAILS))
2231 fprintf (dump_file, "Meeting\n ");
2232 dump_value_range (dump_file, this);
2233 fprintf (dump_file, "\nand\n ");
2234 dump_value_range (dump_file, other);
2235 fprintf (dump_file, "\n");
2238 legacy_union (this, other);
2240 if (dump_file && (dump_flags & TDF_DETAILS))
2242 fprintf (dump_file, "to\n ");
2243 dump_value_range (dump_file, this);
2244 fprintf (dump_file, "\n");
2246 return true;
2249 if (other->legacy_mode_p ())
2251 int_range<2> wider = *other;
2252 return irange_union (wider);
2254 else
2255 return irange_union (*other);
2258 bool
2259 irange::legacy_verbose_intersect (const irange *other)
2261 if (legacy_mode_p ())
2263 if (!other->legacy_mode_p ())
2265 int_range<1> tmp = *other;
2266 legacy_intersect (this, &tmp);
2267 return true;
2269 if (dump_file && (dump_flags & TDF_DETAILS))
2271 fprintf (dump_file, "Intersecting\n ");
2272 dump_value_range (dump_file, this);
2273 fprintf (dump_file, "\nand\n ");
2274 dump_value_range (dump_file, other);
2275 fprintf (dump_file, "\n");
2278 legacy_intersect (this, other);
2280 if (dump_file && (dump_flags & TDF_DETAILS))
2282 fprintf (dump_file, "to\n ");
2283 dump_value_range (dump_file, this);
2284 fprintf (dump_file, "\n");
2286 return true;
2289 if (other->legacy_mode_p ())
2291 int_range<2> wider;
2292 wider = *other;
2293 return irange_intersect (wider);
2295 else
2296 return irange_intersect (*other);
2299 // Perform an efficient union with R when both ranges have only a single pair.
2300 // Excluded are VARYING and UNDEFINED ranges.
2302 bool
2303 irange::irange_single_pair_union (const irange &r)
2305 gcc_checking_assert (!undefined_p () && !varying_p ());
2306 gcc_checking_assert (!r.undefined_p () && !varying_p ());
2308 signop sign = TYPE_SIGN (TREE_TYPE (m_base[0]));
2309 // Check if current lower bound is also the new lower bound.
2310 if (wi::le_p (wi::to_wide (m_base[0]), wi::to_wide (r.m_base[0]), sign))
2312 // If current upper bound is new upper bound, we're done.
2313 if (wi::le_p (wi::to_wide (r.m_base[1]), wi::to_wide (m_base[1]), sign))
2314 return union_nonzero_bits (r);
2315 // Otherwise R has the new upper bound.
2316 // Check for overlap/touching ranges, or single target range.
2317 if (m_max_ranges == 1
2318 || wi::to_widest (m_base[1]) + 1 >= wi::to_widest (r.m_base[0]))
2319 m_base[1] = r.m_base[1];
2320 else
2322 // This is a dual range result.
2323 m_base[2] = r.m_base[0];
2324 m_base[3] = r.m_base[1];
2325 m_num_ranges = 2;
2327 union_nonzero_bits (r);
2328 return true;
2331 // Set the new lower bound to R's lower bound.
2332 tree lb = m_base[0];
2333 m_base[0] = r.m_base[0];
2335 // If R fully contains THIS range, just set the upper bound.
2336 if (wi::ge_p (wi::to_wide (r.m_base[1]), wi::to_wide (m_base[1]), sign))
2337 m_base[1] = r.m_base[1];
2338 // Check for overlapping ranges, or target limited to a single range.
2339 else if (m_max_ranges == 1
2340 || wi::to_widest (r.m_base[1]) + 1 >= wi::to_widest (lb))
2342 else
2344 // Left with 2 pairs.
2345 m_num_ranges = 2;
2346 m_base[2] = lb;
2347 m_base[3] = m_base[1];
2348 m_base[1] = r.m_base[1];
2350 union_nonzero_bits (r);
2351 return true;
2354 // union_ for multi-ranges.
2356 bool
2357 irange::irange_union (const irange &r)
2359 gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ());
2361 if (r.undefined_p ())
2362 return false;
2364 if (undefined_p ())
2366 operator= (r);
2367 if (flag_checking)
2368 verify_range ();
2369 return true;
2372 if (varying_p ())
2373 return false;
2375 if (r.varying_p ())
2377 set_varying (type ());
2378 return true;
2381 // Special case one range union one range.
2382 if (m_num_ranges == 1 && r.m_num_ranges == 1)
2383 return irange_single_pair_union (r);
2385 // If this ranges fully contains R, then we need do nothing.
2386 if (irange_contains_p (r))
2387 return union_nonzero_bits (r);
2389 // Do not worry about merging and such by reserving twice as many
2390 // pairs as needed, and then simply sort the 2 ranges into this
2391 // intermediate form.
2393 // The intermediate result will have the property that the beginning
2394 // of each range is <= the beginning of the next range. There may
2395 // be overlapping ranges at this point. I.e. this would be valid
2396 // [-20, 10], [-10, 0], [0, 20], [40, 90] as it satisfies this
2397 // contraint : -20 < -10 < 0 < 40. When the range is rebuilt into r,
2398 // the merge is performed.
2400 // [Xi,Yi]..[Xn,Yn] U [Xj,Yj]..[Xm,Ym] --> [Xk,Yk]..[Xp,Yp]
2401 auto_vec<tree, 20> res (m_num_ranges * 2 + r.m_num_ranges * 2);
2402 unsigned i = 0, j = 0, k = 0;
2404 while (i < m_num_ranges * 2 && j < r.m_num_ranges * 2)
2406 // lower of Xi and Xj is the lowest point.
2407 if (wi::to_widest (m_base[i]) <= wi::to_widest (r.m_base[j]))
2409 res.quick_push (m_base[i]);
2410 res.quick_push (m_base[i + 1]);
2411 k += 2;
2412 i += 2;
2414 else
2416 res.quick_push (r.m_base[j]);
2417 res.quick_push (r.m_base[j + 1]);
2418 k += 2;
2419 j += 2;
2422 for ( ; i < m_num_ranges * 2; i += 2)
2424 res.quick_push (m_base[i]);
2425 res.quick_push (m_base[i + 1]);
2426 k += 2;
2428 for ( ; j < r.m_num_ranges * 2; j += 2)
2430 res.quick_push (r.m_base[j]);
2431 res.quick_push (r.m_base[j + 1]);
2432 k += 2;
2435 // Now normalize the vector removing any overlaps.
2436 i = 2;
2437 for (j = 2; j < k ; j += 2)
2439 // Current upper+1 is >= lower bound next pair, then we merge ranges.
2440 if (wi::to_widest (res[i - 1]) + 1 >= wi::to_widest (res[j]))
2442 // New upper bounds is greater of current or the next one.
2443 if (wi::to_widest (res[j + 1]) > wi::to_widest (res[i - 1]))
2444 res[i - 1] = res[j + 1];
2446 else
2448 // This is a new distinct range, but no point in copying it
2449 // if it is already in the right place.
2450 if (i != j)
2452 res[i++] = res[j];
2453 res[i++] = res[j + 1];
2455 else
2456 i += 2;
2460 // At this point, the vector should have i ranges, none overlapping.
2461 // Now it simply needs to be copied, and if there are too many
2462 // ranges, merge some. We wont do any analysis as to what the
2463 // "best" merges are, simply combine the final ranges into one.
2464 if (i > m_max_ranges * 2)
2466 res[m_max_ranges * 2 - 1] = res[i - 1];
2467 i = m_max_ranges * 2;
2470 for (j = 0; j < i ; j++)
2471 m_base[j] = res [j];
2472 m_num_ranges = i / 2;
2474 m_kind = VR_RANGE;
2475 union_nonzero_bits (r);
2476 return true;
2479 // Return TRUE if THIS fully contains R. No undefined or varying cases.
2481 bool
2482 irange::irange_contains_p (const irange &r) const
2484 gcc_checking_assert (!undefined_p () && !varying_p ());
2485 gcc_checking_assert (!r.undefined_p () && !varying_p ());
2487 // In order for THIS to fully contain R, all of the pairs within R must
2488 // be fully contained by the pairs in this object.
2489 signop sign = TYPE_SIGN (TREE_TYPE(m_base[0]));
2490 unsigned ri = 0;
2491 unsigned i = 0;
2492 tree rl = r.m_base[0];
2493 tree ru = r.m_base[1];
2494 tree l = m_base[0];
2495 tree u = m_base[1];
2496 while (1)
2498 // If r is contained within this range, move to the next R
2499 if (wi::ge_p (wi::to_wide (rl), wi::to_wide (l), sign)
2500 && wi::le_p (wi::to_wide (ru), wi::to_wide (u), sign))
2502 // This pair is OK, Either done, or bump to the next.
2503 if (++ri >= r.num_pairs ())
2504 return true;
2505 rl = r.m_base[ri * 2];
2506 ru = r.m_base[ri * 2 + 1];
2507 continue;
2509 // Otherwise, check if this's pair occurs before R's.
2510 if (wi::lt_p (wi::to_wide (u), wi::to_wide (rl), sign))
2512 // There's still at least one pair of R left.
2513 if (++i >= num_pairs ())
2514 return false;
2515 l = m_base[i * 2];
2516 u = m_base[i * 2 + 1];
2517 continue;
2519 return false;
2521 return false;
2525 // Intersect for multi-ranges. Return TRUE if anything changes.
2527 bool
2528 irange::irange_intersect (const irange &r)
2530 gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ());
2531 gcc_checking_assert (undefined_p () || r.undefined_p ()
2532 || range_compatible_p (type (), r.type ()));
2534 if (undefined_p ())
2535 return false;
2536 if (r.undefined_p ())
2538 set_undefined ();
2539 return true;
2541 if (r.varying_p ())
2542 return false;
2543 if (varying_p ())
2545 operator= (r);
2546 return true;
2549 if (r.num_pairs () == 1)
2551 bool res = intersect (r.lower_bound (), r.upper_bound ());
2552 if (undefined_p ())
2553 return true;
2555 res |= intersect_nonzero_bits (r);
2556 return res;
2559 // If R fully contains this, then intersection will change nothing.
2560 if (r.irange_contains_p (*this))
2561 return intersect_nonzero_bits (r);
2563 signop sign = TYPE_SIGN (TREE_TYPE(m_base[0]));
2564 unsigned bld_pair = 0;
2565 unsigned bld_lim = m_max_ranges;
2566 int_range_max r2 (*this);
2567 unsigned r2_lim = r2.num_pairs ();
2568 unsigned i2 = 0;
2569 for (unsigned i = 0; i < r.num_pairs (); )
2571 // If r1's upper is < r2's lower, we can skip r1's pair.
2572 tree ru = r.m_base[i * 2 + 1];
2573 tree r2l = r2.m_base[i2 * 2];
2574 if (wi::lt_p (wi::to_wide (ru), wi::to_wide (r2l), sign))
2576 i++;
2577 continue;
2579 // Likewise, skip r2's pair if its excluded.
2580 tree r2u = r2.m_base[i2 * 2 + 1];
2581 tree rl = r.m_base[i * 2];
2582 if (wi::lt_p (wi::to_wide (r2u), wi::to_wide (rl), sign))
2584 i2++;
2585 if (i2 < r2_lim)
2586 continue;
2587 // No more r2, break.
2588 break;
2591 // Must be some overlap. Find the highest of the lower bounds,
2592 // and set it, unless the build limits lower bounds is already
2593 // set.
2594 if (bld_pair < bld_lim)
2596 if (wi::ge_p (wi::to_wide (rl), wi::to_wide (r2l), sign))
2597 m_base[bld_pair * 2] = rl;
2598 else
2599 m_base[bld_pair * 2] = r2l;
2601 else
2602 // Decrease and set a new upper.
2603 bld_pair--;
2605 // ...and choose the lower of the upper bounds.
2606 if (wi::le_p (wi::to_wide (ru), wi::to_wide (r2u), sign))
2608 m_base[bld_pair * 2 + 1] = ru;
2609 bld_pair++;
2610 // Move past the r1 pair and keep trying.
2611 i++;
2612 continue;
2614 else
2616 m_base[bld_pair * 2 + 1] = r2u;
2617 bld_pair++;
2618 i2++;
2619 if (i2 < r2_lim)
2620 continue;
2621 // No more r2, break.
2622 break;
2624 // r2 has the higher lower bound.
2627 // At the exit of this loop, it is one of 2 things:
2628 // ran out of r1, or r2, but either means we are done.
2629 m_num_ranges = bld_pair;
2630 if (m_num_ranges == 0)
2632 set_undefined ();
2633 return true;
2636 m_kind = VR_RANGE;
2637 intersect_nonzero_bits (r);
2638 return true;
2642 // Multirange intersect for a specified wide_int [lb, ub] range.
2643 // Return TRUE if intersect changed anything.
2645 // NOTE: It is the caller's responsibility to intersect the nonzero masks.
2647 bool
2648 irange::intersect (const wide_int& lb, const wide_int& ub)
2650 // Undefined remains undefined.
2651 if (undefined_p ())
2652 return false;
2654 if (legacy_mode_p ())
2656 intersect (int_range<1> (type (), lb, ub));
2657 return true;
2660 tree range_type = type();
2661 signop sign = TYPE_SIGN (range_type);
2663 gcc_checking_assert (TYPE_PRECISION (range_type) == wi::get_precision (lb));
2664 gcc_checking_assert (TYPE_PRECISION (range_type) == wi::get_precision (ub));
2666 // If this range is fuly contained, then intersection will do nothing.
2667 if (wi::ge_p (lower_bound (), lb, sign)
2668 && wi::le_p (upper_bound (), ub, sign))
2669 return false;
2671 unsigned bld_index = 0;
2672 unsigned pair_lim = num_pairs ();
2673 for (unsigned i = 0; i < pair_lim; i++)
2675 tree pairl = m_base[i * 2];
2676 tree pairu = m_base[i * 2 + 1];
2677 // Once UB is less than a pairs lower bound, we're done.
2678 if (wi::lt_p (ub, wi::to_wide (pairl), sign))
2679 break;
2680 // if LB is greater than this pairs upper, this pair is excluded.
2681 if (wi::lt_p (wi::to_wide (pairu), lb, sign))
2682 continue;
2684 // Must be some overlap. Find the highest of the lower bounds,
2685 // and set it
2686 if (wi::gt_p (lb, wi::to_wide (pairl), sign))
2687 m_base[bld_index * 2] = wide_int_to_tree (range_type, lb);
2688 else
2689 m_base[bld_index * 2] = pairl;
2691 // ...and choose the lower of the upper bounds and if the base pair
2692 // has the lower upper bound, need to check next pair too.
2693 if (wi::lt_p (ub, wi::to_wide (pairu), sign))
2695 m_base[bld_index++ * 2 + 1] = wide_int_to_tree (range_type, ub);
2696 break;
2698 else
2699 m_base[bld_index++ * 2 + 1] = pairu;
2702 m_num_ranges = bld_index;
2703 if (m_num_ranges == 0)
2705 set_undefined ();
2706 return true;
2709 m_kind = VR_RANGE;
2710 // No need to call normalize_kind(), as the caller will do this
2711 // while intersecting the nonzero mask.
2712 if (flag_checking)
2713 verify_range ();
2714 return true;
2718 // Signed 1-bits are strange. You can't subtract 1, because you can't
2719 // represent the number 1. This works around that for the invert routine.
2721 static wide_int inline
2722 subtract_one (const wide_int &x, tree type, wi::overflow_type &overflow)
2724 if (TYPE_SIGN (type) == SIGNED)
2725 return wi::add (x, -1, SIGNED, &overflow);
2726 else
2727 return wi::sub (x, 1, UNSIGNED, &overflow);
2730 // The analogous function for adding 1.
2732 static wide_int inline
2733 add_one (const wide_int &x, tree type, wi::overflow_type &overflow)
2735 if (TYPE_SIGN (type) == SIGNED)
2736 return wi::sub (x, -1, SIGNED, &overflow);
2737 else
2738 return wi::add (x, 1, UNSIGNED, &overflow);
2741 // Return the inverse of a range.
2743 void
2744 irange::invert ()
2746 if (legacy_mode_p ())
2748 // We can't just invert VR_RANGE and VR_ANTI_RANGE because we may
2749 // create non-canonical ranges. Use the constructors instead.
2750 if (m_kind == VR_RANGE)
2751 *this = value_range (min (), max (), VR_ANTI_RANGE);
2752 else if (m_kind == VR_ANTI_RANGE)
2753 *this = value_range (min (), max ());
2754 else
2755 gcc_unreachable ();
2756 return;
2759 gcc_checking_assert (!undefined_p () && !varying_p ());
2761 // We always need one more set of bounds to represent an inverse, so
2762 // if we're at the limit, we can't properly represent things.
2764 // For instance, to represent the inverse of a 2 sub-range set
2765 // [5, 10][20, 30], we would need a 3 sub-range set
2766 // [-MIN, 4][11, 19][31, MAX].
2768 // In this case, return the most conservative thing.
2770 // However, if any of the extremes of the range are -MIN/+MAX, we
2771 // know we will not need an extra bound. For example:
2773 // INVERT([-MIN,20][30,40]) => [21,29][41,+MAX]
2774 // INVERT([-MIN,20][30,MAX]) => [21,29]
2775 tree ttype = type ();
2776 unsigned prec = TYPE_PRECISION (ttype);
2777 signop sign = TYPE_SIGN (ttype);
2778 wide_int type_min = wi::min_value (prec, sign);
2779 wide_int type_max = wi::max_value (prec, sign);
2780 m_nonzero_mask = NULL;
2781 if (m_num_ranges == m_max_ranges
2782 && lower_bound () != type_min
2783 && upper_bound () != type_max)
2785 m_base[1] = wide_int_to_tree (ttype, type_max);
2786 m_num_ranges = 1;
2787 return;
2789 // The algorithm is as follows. To calculate INVERT ([a,b][c,d]), we
2790 // generate [-MIN, a-1][b+1, c-1][d+1, MAX].
2792 // If there is an over/underflow in the calculation for any
2793 // sub-range, we eliminate that subrange. This allows us to easily
2794 // calculate INVERT([-MIN, 5]) with: [-MIN, -MIN-1][6, MAX]. And since
2795 // we eliminate the underflow, only [6, MAX] remains.
2796 unsigned i = 0;
2797 wi::overflow_type ovf;
2798 // Construct leftmost range.
2799 int_range_max orig_range (*this);
2800 unsigned nitems = 0;
2801 wide_int tmp;
2802 // If this is going to underflow on the MINUS 1, don't even bother
2803 // checking. This also handles subtracting one from an unsigned 0,
2804 // which doesn't set the underflow bit.
2805 if (type_min != orig_range.lower_bound ())
2807 m_base[nitems++] = wide_int_to_tree (ttype, type_min);
2808 tmp = subtract_one (orig_range.lower_bound (), ttype, ovf);
2809 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
2810 if (ovf)
2811 nitems = 0;
2813 i++;
2814 // Construct middle ranges if applicable.
2815 if (orig_range.num_pairs () > 1)
2817 unsigned j = i;
2818 for (; j < (orig_range.num_pairs () * 2) - 1; j += 2)
2820 // The middle ranges cannot have MAX/MIN, so there's no need
2821 // to check for unsigned overflow on the +1 and -1 here.
2822 tmp = wi::add (wi::to_wide (orig_range.m_base[j]), 1, sign, &ovf);
2823 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
2824 tmp = subtract_one (wi::to_wide (orig_range.m_base[j + 1]),
2825 ttype, ovf);
2826 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
2827 if (ovf)
2828 nitems -= 2;
2830 i = j;
2832 // Construct rightmost range.
2834 // However, if this will overflow on the PLUS 1, don't even bother.
2835 // This also handles adding one to an unsigned MAX, which doesn't
2836 // set the overflow bit.
2837 if (type_max != wi::to_wide (orig_range.m_base[i]))
2839 tmp = add_one (wi::to_wide (orig_range.m_base[i]), ttype, ovf);
2840 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
2841 m_base[nitems++] = wide_int_to_tree (ttype, type_max);
2842 if (ovf)
2843 nitems -= 2;
2845 m_num_ranges = nitems / 2;
2847 // We disallow undefined or varying coming in, so the result can
2848 // only be a VR_RANGE.
2849 gcc_checking_assert (m_kind == VR_RANGE);
2851 if (flag_checking)
2852 verify_range ();
2855 // Return the nonzero bits inherent in the range.
2857 wide_int
2858 irange::get_nonzero_bits_from_range () const
2860 // For legacy symbolics.
2861 if (!constant_p ())
2862 return wi::shwi (-1, TYPE_PRECISION (type ()));
2864 wide_int min = lower_bound ();
2865 wide_int max = upper_bound ();
2866 wide_int xorv = min ^ max;
2867 if (xorv != 0)
2869 unsigned prec = TYPE_PRECISION (type ());
2870 xorv = wi::mask (prec - wi::clz (xorv), false, prec);
2872 return min | xorv;
2875 // If the the nonzero mask can be trivially converted to a range, do
2876 // so and return TRUE.
2878 bool
2879 irange::set_range_from_nonzero_bits ()
2881 gcc_checking_assert (!undefined_p ());
2882 if (!m_nonzero_mask)
2883 return false;
2884 unsigned popcount = wi::popcount (wi::to_wide (m_nonzero_mask));
2886 // If we have only one bit set in the mask, we can figure out the
2887 // range immediately.
2888 if (popcount == 1)
2890 // Make sure we don't pessimize the range.
2891 if (!contains_p (m_nonzero_mask))
2892 return false;
2894 bool has_zero = contains_p (build_zero_cst (type ()));
2895 tree nz = m_nonzero_mask;
2896 set (nz, nz);
2897 m_nonzero_mask = nz;
2898 if (has_zero)
2900 int_range<2> zero;
2901 zero.set_zero (type ());
2902 union_ (zero);
2904 return true;
2906 return false;
2909 void
2910 irange::set_nonzero_bits (const wide_int_ref &bits)
2912 gcc_checking_assert (!undefined_p ());
2913 unsigned prec = TYPE_PRECISION (type ());
2915 if (bits == -1)
2917 m_nonzero_mask = NULL;
2918 normalize_kind ();
2919 if (flag_checking)
2920 verify_range ();
2921 return;
2924 // Drop VARYINGs with a nonzero mask to a plain range.
2925 if (m_kind == VR_VARYING && bits != -1)
2926 m_kind = VR_RANGE;
2928 wide_int nz = wide_int::from (bits, prec, TYPE_SIGN (type ()));
2929 m_nonzero_mask = wide_int_to_tree (type (), nz);
2930 if (set_range_from_nonzero_bits ())
2931 return;
2933 normalize_kind ();
2934 if (flag_checking)
2935 verify_range ();
2938 // Return the nonzero bitmask. This will return the nonzero bits plus
2939 // the nonzero bits inherent in the range.
2941 wide_int
2942 irange::get_nonzero_bits () const
2944 gcc_checking_assert (!undefined_p ());
2945 // The nonzero mask inherent in the range is calculated on-demand.
2946 // For example, [0,255] does not have a 0xff nonzero mask by default
2947 // (unless manually set). This saves us considerable time, because
2948 // setting it at creation incurs a large penalty for irange::set.
2949 // At the time of writing there was a 5% slowdown in VRP if we kept
2950 // the mask precisely up to date at all times. Instead, we default
2951 // to -1 and set it when explicitly requested. However, this
2952 // function will always return the correct mask.
2953 if (m_nonzero_mask)
2954 return wi::to_wide (m_nonzero_mask) & get_nonzero_bits_from_range ();
2955 else
2956 return get_nonzero_bits_from_range ();
2959 // Convert tree mask to wide_int. Returns -1 for NULL masks.
2961 inline wide_int
2962 mask_to_wi (tree mask, tree type)
2964 if (mask)
2965 return wi::to_wide (mask);
2966 else
2967 return wi::shwi (-1, TYPE_PRECISION (type));
2970 // Intersect the nonzero bits in R into THIS and normalize the range.
2971 // Return TRUE if the intersection changed anything.
2973 bool
2974 irange::intersect_nonzero_bits (const irange &r)
2976 gcc_checking_assert (!undefined_p () && !r.undefined_p ());
2978 if (!m_nonzero_mask && !r.m_nonzero_mask)
2980 normalize_kind ();
2981 if (flag_checking)
2982 verify_range ();
2983 return false;
2986 bool changed = false;
2987 tree t = type ();
2988 if (mask_to_wi (m_nonzero_mask, t) != mask_to_wi (r.m_nonzero_mask, t))
2990 wide_int nz = get_nonzero_bits () & r.get_nonzero_bits ();
2991 m_nonzero_mask = wide_int_to_tree (t, nz);
2992 if (set_range_from_nonzero_bits ())
2993 return true;
2994 changed = true;
2996 normalize_kind ();
2997 if (flag_checking)
2998 verify_range ();
2999 return changed;
3002 // Union the nonzero bits in R into THIS and normalize the range.
3003 // Return TRUE if the union changed anything.
3005 bool
3006 irange::union_nonzero_bits (const irange &r)
3008 gcc_checking_assert (!undefined_p () && !r.undefined_p ());
3010 if (!m_nonzero_mask && !r.m_nonzero_mask)
3012 normalize_kind ();
3013 if (flag_checking)
3014 verify_range ();
3015 return false;
3018 bool changed = false;
3019 tree t = type ();
3020 if (mask_to_wi (m_nonzero_mask, t) != mask_to_wi (r.m_nonzero_mask, t))
3022 wide_int nz = get_nonzero_bits () | r.get_nonzero_bits ();
3023 m_nonzero_mask = wide_int_to_tree (t, nz);
3024 // No need to call set_range_from_nonzero_bits, because we'll
3025 // never narrow the range. Besides, it would cause endless
3026 // recursion because of the union_ in
3027 // set_range_from_nonzero_bits.
3028 changed = true;
3030 normalize_kind ();
3031 if (flag_checking)
3032 verify_range ();
3033 return changed;
3036 void
3037 dump_value_range (FILE *file, const vrange *vr)
3039 vr->dump (file);
3042 DEBUG_FUNCTION void
3043 debug (const vrange *vr)
3045 dump_value_range (stderr, vr);
3046 fprintf (stderr, "\n");
3049 DEBUG_FUNCTION void
3050 debug (const vrange &vr)
3052 debug (&vr);
3055 DEBUG_FUNCTION void
3056 debug (const value_range *vr)
3058 dump_value_range (stderr, vr);
3059 fprintf (stderr, "\n");
3062 DEBUG_FUNCTION void
3063 debug (const value_range &vr)
3065 dump_value_range (stderr, &vr);
3066 fprintf (stderr, "\n");
3069 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
3070 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
3071 false otherwise. If *AR can be represented with a single range
3072 *VR1 will be VR_UNDEFINED. */
3074 bool
3075 ranges_from_anti_range (const value_range *ar,
3076 value_range *vr0, value_range *vr1)
3078 tree type = ar->type ();
3080 vr0->set_undefined ();
3081 vr1->set_undefined ();
3083 /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U
3084 [A+1, +INF]. Not sure if this helps in practice, though. */
3086 if (ar->kind () != VR_ANTI_RANGE
3087 || TREE_CODE (ar->min ()) != INTEGER_CST
3088 || TREE_CODE (ar->max ()) != INTEGER_CST
3089 || !vrp_val_min (type)
3090 || !vrp_val_max (type))
3091 return false;
3093 if (tree_int_cst_lt (vrp_val_min (type), ar->min ()))
3094 vr0->set (vrp_val_min (type),
3095 wide_int_to_tree (type, wi::to_wide (ar->min ()) - 1));
3096 if (tree_int_cst_lt (ar->max (), vrp_val_max (type)))
3097 vr1->set (wide_int_to_tree (type, wi::to_wide (ar->max ()) + 1),
3098 vrp_val_max (type));
3099 if (vr0->undefined_p ())
3101 *vr0 = *vr1;
3102 vr1->set_undefined ();
3105 return !vr0->undefined_p ();
3108 bool
3109 range_has_numeric_bounds_p (const irange *vr)
3111 return (!vr->undefined_p ()
3112 && TREE_CODE (vr->min ()) == INTEGER_CST
3113 && TREE_CODE (vr->max ()) == INTEGER_CST);
3116 /* Return whether VAL is equal to the maximum value of its type.
3117 We can't do a simple equality comparison with TYPE_MAX_VALUE because
3118 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
3119 is not == to the integer constant with the same value in the type. */
3121 bool
3122 vrp_val_is_max (const_tree val)
3124 tree type_max = vrp_val_max (TREE_TYPE (val));
3125 return (val == type_max
3126 || (type_max != NULL_TREE
3127 && operand_equal_p (val, type_max, 0)));
3130 /* Return whether VAL is equal to the minimum value of its type. */
3132 bool
3133 vrp_val_is_min (const_tree val)
3135 tree type_min = vrp_val_min (TREE_TYPE (val));
3136 return (val == type_min
3137 || (type_min != NULL_TREE
3138 && operand_equal_p (val, type_min, 0)));
3141 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
3143 bool
3144 vrp_operand_equal_p (const_tree val1, const_tree val2)
3146 if (val1 == val2)
3147 return true;
3148 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
3149 return false;
3150 return true;
3153 // ?? These stubs are for ipa-prop.cc which use a value_range in a
3154 // hash_traits. hash-traits.h defines an extern of gt_ggc_mx (T &)
3155 // instead of picking up the gt_ggc_mx (T *) version.
3156 void
3157 gt_pch_nx (int_range<1> *&x)
3159 return gt_pch_nx ((irange *) x);
3162 void
3163 gt_ggc_mx (int_range<1> *&x)
3165 return gt_ggc_mx ((irange *) x);
3168 #define DEFINE_INT_RANGE_INSTANCE(N) \
3169 template int_range<N>::int_range(tree, tree, value_range_kind); \
3170 template int_range<N>::int_range(tree_node *, \
3171 const wide_int &, \
3172 const wide_int &, \
3173 value_range_kind); \
3174 template int_range<N>::int_range(tree); \
3175 template int_range<N>::int_range(const irange &); \
3176 template int_range<N>::int_range(const int_range &); \
3177 template int_range<N>& int_range<N>::operator= (const int_range &);
3179 DEFINE_INT_RANGE_INSTANCE(1)
3180 DEFINE_INT_RANGE_INSTANCE(2)
3181 DEFINE_INT_RANGE_INSTANCE(3)
3182 DEFINE_INT_RANGE_INSTANCE(255)
3184 #if CHECKING_P
3185 #include "selftest.h"
3187 namespace selftest
3189 #define INT(N) build_int_cst (integer_type_node, (N))
3190 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
3191 #define UINT128(N) build_int_cstu (u128_type, (N))
3192 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
3193 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
3195 static int_range<3>
3196 build_range3 (int a, int b, int c, int d, int e, int f)
3198 int_range<3> i1 (INT (a), INT (b));
3199 int_range<3> i2 (INT (c), INT (d));
3200 int_range<3> i3 (INT (e), INT (f));
3201 i1.union_ (i2);
3202 i1.union_ (i3);
3203 return i1;
3206 static void
3207 range_tests_irange3 ()
3209 typedef int_range<3> int_range3;
3210 int_range3 r0, r1, r2;
3211 int_range3 i1, i2, i3;
3213 // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20].
3214 r0 = int_range3 (INT (10), INT (20));
3215 r1 = int_range3 (INT (5), INT (8));
3216 r0.union_ (r1);
3217 r1 = int_range3 (INT (1), INT (3));
3218 r0.union_ (r1);
3219 ASSERT_TRUE (r0 == build_range3 (1, 3, 5, 8, 10, 20));
3221 // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20].
3222 r1 = int_range3 (INT (-5), INT (0));
3223 r0.union_ (r1);
3224 ASSERT_TRUE (r0 == build_range3 (-5, 3, 5, 8, 10, 20));
3226 // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60].
3227 r1 = int_range3 (INT (50), INT (60));
3228 r0 = int_range3 (INT (10), INT (20));
3229 r0.union_ (int_range3 (INT (30), INT (40)));
3230 r0.union_ (r1);
3231 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
3232 // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80].
3233 r1 = int_range3 (INT (70), INT (80));
3234 r0.union_ (r1);
3236 r2 = build_range3 (10, 20, 30, 40, 50, 60);
3237 r2.union_ (int_range3 (INT (70), INT (80)));
3238 ASSERT_TRUE (r0 == r2);
3240 // [10,20][30,40][50,60] U [6,35] => [6,40][50,60].
3241 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3242 r1 = int_range3 (INT (6), INT (35));
3243 r0.union_ (r1);
3244 r1 = int_range3 (INT (6), INT (40));
3245 r1.union_ (int_range3 (INT (50), INT (60)));
3246 ASSERT_TRUE (r0 == r1);
3248 // [10,20][30,40][50,60] U [6,60] => [6,60].
3249 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3250 r1 = int_range3 (INT (6), INT (60));
3251 r0.union_ (r1);
3252 ASSERT_TRUE (r0 == int_range3 (INT (6), INT (60)));
3254 // [10,20][30,40][50,60] U [6,70] => [6,70].
3255 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3256 r1 = int_range3 (INT (6), INT (70));
3257 r0.union_ (r1);
3258 ASSERT_TRUE (r0 == int_range3 (INT (6), INT (70)));
3260 // [10,20][30,40][50,60] U [35,70] => [10,20][30,70].
3261 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3262 r1 = int_range3 (INT (35), INT (70));
3263 r0.union_ (r1);
3264 r1 = int_range3 (INT (10), INT (20));
3265 r1.union_ (int_range3 (INT (30), INT (70)));
3266 ASSERT_TRUE (r0 == r1);
3268 // [10,20][30,40][50,60] U [15,35] => [10,40][50,60].
3269 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3270 r1 = int_range3 (INT (15), INT (35));
3271 r0.union_ (r1);
3272 r1 = int_range3 (INT (10), INT (40));
3273 r1.union_ (int_range3 (INT (50), INT (60)));
3274 ASSERT_TRUE (r0 == r1);
3276 // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60].
3277 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3278 r1 = int_range3 (INT (35), INT (35));
3279 r0.union_ (r1);
3280 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
3283 static void
3284 range_tests_int_range_max ()
3286 int_range_max big;
3287 unsigned int nrange;
3289 // Build a huge multi-range range.
3290 for (nrange = 0; nrange < 50; ++nrange)
3292 int_range<1> tmp (INT (nrange*10), INT (nrange*10 + 5));
3293 big.union_ (tmp);
3295 ASSERT_TRUE (big.num_pairs () == nrange);
3297 // Verify that we can copy it without loosing precision.
3298 int_range_max copy (big);
3299 ASSERT_TRUE (copy.num_pairs () == nrange);
3301 // Inverting it should produce one more sub-range.
3302 big.invert ();
3303 ASSERT_TRUE (big.num_pairs () == nrange + 1);
3305 int_range<1> tmp (INT (5), INT (37));
3306 big.intersect (tmp);
3307 ASSERT_TRUE (big.num_pairs () == 4);
3309 // Test that [10,10][20,20] does NOT contain 15.
3311 int_range_max i1 (build_int_cst (integer_type_node, 10),
3312 build_int_cst (integer_type_node, 10));
3313 int_range_max i2 (build_int_cst (integer_type_node, 20),
3314 build_int_cst (integer_type_node, 20));
3315 i1.union_ (i2);
3316 ASSERT_FALSE (i1.contains_p (build_int_cst (integer_type_node, 15)));
3320 static void
3321 range_tests_legacy ()
3323 // Test truncating copy to int_range<1>.
3324 int_range<3> big = build_range3 (10, 20, 30, 40, 50, 60);
3325 int_range<1> small = big;
3326 ASSERT_TRUE (small == int_range<1> (INT (10), INT (60)));
3328 // Test truncating copy to int_range<2>.
3329 int_range<2> medium = big;
3330 ASSERT_TRUE (!medium.undefined_p ());
3332 // Test that a truncating copy of [MIN,20][22,40][80,MAX]
3333 // ends up as a conservative anti-range of ~[21,21].
3334 big = int_range<3> (vrp_val_min (integer_type_node), INT (20));
3335 big.union_ (int_range<1> (INT (22), INT (40)));
3336 big.union_ (int_range<1> (INT (80), vrp_val_max (integer_type_node)));
3337 small = big;
3338 ASSERT_TRUE (small == int_range<1> (INT (21), INT (21), VR_ANTI_RANGE));
3340 // Copying a legacy symbolic to an int_range should normalize the
3341 // symbolic at copy time.
3343 tree ssa = make_ssa_name (integer_type_node);
3344 value_range legacy_range (ssa, INT (25));
3345 int_range<2> copy = legacy_range;
3346 ASSERT_TRUE (copy == int_range<2> (vrp_val_min (integer_type_node),
3347 INT (25)));
3349 // Test that copying ~[abc_23, abc_23] to a multi-range yields varying.
3350 legacy_range = value_range (ssa, ssa, VR_ANTI_RANGE);
3351 copy = legacy_range;
3352 ASSERT_TRUE (copy.varying_p ());
3355 // VARYING of different sizes should not be equal.
3356 tree big_type = build_nonstandard_integer_type (32, 1);
3357 tree small_type = build_nonstandard_integer_type (16, 1);
3358 int_range_max r0 (big_type);
3359 int_range_max r1 (small_type);
3360 ASSERT_TRUE (r0 != r1);
3361 value_range vr0 (big_type);
3362 int_range_max vr1 (small_type);
3363 ASSERT_TRUE (vr0 != vr1);
3366 // Simulate -fstrict-enums where the domain of a type is less than the
3367 // underlying type.
3369 static void
3370 range_tests_strict_enum ()
3372 // The enum can only hold [0, 3].
3373 tree rtype = copy_node (unsigned_type_node);
3374 TYPE_MIN_VALUE (rtype) = build_int_cstu (rtype, 0);
3375 TYPE_MAX_VALUE (rtype) = build_int_cstu (rtype, 3);
3377 // Test that even though vr1 covers the strict enum domain ([0, 3]),
3378 // it does not cover the domain of the underlying type.
3379 int_range<1> vr1 (build_int_cstu (rtype, 0), build_int_cstu (rtype, 1));
3380 int_range<1> vr2 (build_int_cstu (rtype, 2), build_int_cstu (rtype, 3));
3381 vr1.union_ (vr2);
3382 ASSERT_TRUE (vr1 == int_range<1> (build_int_cstu (rtype, 0),
3383 build_int_cstu (rtype, 3)));
3384 ASSERT_FALSE (vr1.varying_p ());
3386 // Test that copying to a multi-range does not change things.
3387 int_range<2> ir1 (vr1);
3388 ASSERT_TRUE (ir1 == vr1);
3389 ASSERT_FALSE (ir1.varying_p ());
3391 // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3].
3392 vr1 = int_range<1> (TYPE_MIN_VALUE (rtype), TYPE_MAX_VALUE (rtype));
3393 ir1 = vr1;
3394 ASSERT_TRUE (ir1 == vr1);
3395 ASSERT_FALSE (ir1.varying_p ());
3398 static void
3399 range_tests_misc ()
3401 tree u128_type = build_nonstandard_integer_type (128, /*unsigned=*/1);
3402 int_range<1> i1, i2, i3;
3403 int_range<1> r0, r1, rold;
3405 // Test 1-bit signed integer union.
3406 // [-1,-1] U [0,0] = VARYING.
3407 tree one_bit_type = build_nonstandard_integer_type (1, 0);
3408 tree one_bit_min = vrp_val_min (one_bit_type);
3409 tree one_bit_max = vrp_val_max (one_bit_type);
3411 int_range<2> min (one_bit_min, one_bit_min);
3412 int_range<2> max (one_bit_max, one_bit_max);
3413 max.union_ (min);
3414 ASSERT_TRUE (max.varying_p ());
3417 // Test inversion of 1-bit signed integers.
3419 int_range<2> min (one_bit_min, one_bit_min);
3420 int_range<2> max (one_bit_max, one_bit_max);
3421 int_range<2> t;
3422 t = min;
3423 t.invert ();
3424 ASSERT_TRUE (t == max);
3425 t = max;
3426 t.invert ();
3427 ASSERT_TRUE (t == min);
3430 // Test that NOT(255) is [0..254] in 8-bit land.
3431 int_range<1> not_255 (UCHAR (255), UCHAR (255), VR_ANTI_RANGE);
3432 ASSERT_TRUE (not_255 == int_range<1> (UCHAR (0), UCHAR (254)));
3434 // Test that NOT(0) is [1..255] in 8-bit land.
3435 int_range<1> not_zero = range_nonzero (unsigned_char_type_node);
3436 ASSERT_TRUE (not_zero == int_range<1> (UCHAR (1), UCHAR (255)));
3438 // Check that [0,127][0x..ffffff80,0x..ffffff]
3439 // => ~[128, 0x..ffffff7f].
3440 r0 = int_range<1> (UINT128 (0), UINT128 (127));
3441 tree high = build_minus_one_cst (u128_type);
3442 // low = -1 - 127 => 0x..ffffff80.
3443 tree low = fold_build2 (MINUS_EXPR, u128_type, high, UINT128(127));
3444 r1 = int_range<1> (low, high); // [0x..ffffff80, 0x..ffffffff]
3445 // r0 = [0,127][0x..ffffff80,0x..fffffff].
3446 r0.union_ (r1);
3447 // r1 = [128, 0x..ffffff7f].
3448 r1 = int_range<1> (UINT128(128),
3449 fold_build2 (MINUS_EXPR, u128_type,
3450 build_minus_one_cst (u128_type),
3451 UINT128(128)));
3452 r0.invert ();
3453 ASSERT_TRUE (r0 == r1);
3455 r0.set_varying (integer_type_node);
3456 tree minint = wide_int_to_tree (integer_type_node, r0.lower_bound ());
3457 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
3459 r0.set_varying (short_integer_type_node);
3461 r0.set_varying (unsigned_type_node);
3462 tree maxuint = wide_int_to_tree (unsigned_type_node, r0.upper_bound ());
3464 // Check that ~[0,5] => [6,MAX] for unsigned int.
3465 r0 = int_range<1> (UINT (0), UINT (5));
3466 r0.invert ();
3467 ASSERT_TRUE (r0 == int_range<1> (UINT(6), maxuint));
3469 // Check that ~[10,MAX] => [0,9] for unsigned int.
3470 r0 = int_range<1> (UINT(10), maxuint);
3471 r0.invert ();
3472 ASSERT_TRUE (r0 == int_range<1> (UINT (0), UINT (9)));
3474 // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers.
3475 r0 = int_range<1> (UINT128 (0), UINT128 (5), VR_ANTI_RANGE);
3476 r1 = int_range<1> (UINT128(6), build_minus_one_cst (u128_type));
3477 ASSERT_TRUE (r0 == r1);
3479 // Check that [~5] is really [-MIN,4][6,MAX].
3480 r0 = int_range<1> (INT (5), INT (5), VR_ANTI_RANGE);
3481 r1 = int_range<1> (minint, INT (4));
3482 r1.union_ (int_range<1> (INT (6), maxint));
3483 ASSERT_FALSE (r1.undefined_p ());
3484 ASSERT_TRUE (r0 == r1);
3486 r1 = int_range<1> (INT (5), INT (5));
3487 int_range<1> r2 (r1);
3488 ASSERT_TRUE (r1 == r2);
3490 r1 = int_range<1> (INT (5), INT (10));
3492 r1 = int_range<1> (integer_type_node,
3493 wi::to_wide (INT (5)), wi::to_wide (INT (10)));
3494 ASSERT_TRUE (r1.contains_p (INT (7)));
3496 r1 = int_range<1> (SCHAR (0), SCHAR (20));
3497 ASSERT_TRUE (r1.contains_p (SCHAR(15)));
3498 ASSERT_FALSE (r1.contains_p (SCHAR(300)));
3500 // NOT([10,20]) ==> [-MIN,9][21,MAX].
3501 r0 = r1 = int_range<1> (INT (10), INT (20));
3502 r2 = int_range<1> (minint, INT(9));
3503 r2.union_ (int_range<1> (INT(21), maxint));
3504 ASSERT_FALSE (r2.undefined_p ());
3505 r1.invert ();
3506 ASSERT_TRUE (r1 == r2);
3507 // Test that NOT(NOT(x)) == x.
3508 r2.invert ();
3509 ASSERT_TRUE (r0 == r2);
3511 // Test that booleans and their inverse work as expected.
3512 r0 = range_zero (boolean_type_node);
3513 ASSERT_TRUE (r0 == int_range<1> (build_zero_cst (boolean_type_node),
3514 build_zero_cst (boolean_type_node)));
3515 r0.invert ();
3516 ASSERT_TRUE (r0 == int_range<1> (build_one_cst (boolean_type_node),
3517 build_one_cst (boolean_type_node)));
3519 // Make sure NULL and non-NULL of pointer types work, and that
3520 // inverses of them are consistent.
3521 tree voidp = build_pointer_type (void_type_node);
3522 r0 = range_zero (voidp);
3523 r1 = r0;
3524 r0.invert ();
3525 r0.invert ();
3526 ASSERT_TRUE (r0 == r1);
3528 // [10,20] U [15, 30] => [10, 30].
3529 r0 = int_range<1> (INT (10), INT (20));
3530 r1 = int_range<1> (INT (15), INT (30));
3531 r0.union_ (r1);
3532 ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (30)));
3534 // [15,40] U [] => [15,40].
3535 r0 = int_range<1> (INT (15), INT (40));
3536 r1.set_undefined ();
3537 r0.union_ (r1);
3538 ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (40)));
3540 // [10,20] U [10,10] => [10,20].
3541 r0 = int_range<1> (INT (10), INT (20));
3542 r1 = int_range<1> (INT (10), INT (10));
3543 r0.union_ (r1);
3544 ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (20)));
3546 // [10,20] U [9,9] => [9,20].
3547 r0 = int_range<1> (INT (10), INT (20));
3548 r1 = int_range<1> (INT (9), INT (9));
3549 r0.union_ (r1);
3550 ASSERT_TRUE (r0 == int_range<1> (INT (9), INT (20)));
3552 // [10,20] ^ [15,30] => [15,20].
3553 r0 = int_range<1> (INT (10), INT (20));
3554 r1 = int_range<1> (INT (15), INT (30));
3555 r0.intersect (r1);
3556 ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (20)));
3558 // Test the internal sanity of wide_int's wrt HWIs.
3559 ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node),
3560 TYPE_SIGN (boolean_type_node))
3561 == wi::uhwi (1, TYPE_PRECISION (boolean_type_node)));
3563 // Test zero_p().
3564 r0 = int_range<1> (INT (0), INT (0));
3565 ASSERT_TRUE (r0.zero_p ());
3567 // Test nonzero_p().
3568 r0 = int_range<1> (INT (0), INT (0));
3569 r0.invert ();
3570 ASSERT_TRUE (r0.nonzero_p ());
3572 // test legacy interaction
3573 // r0 = ~[1,1]
3574 r0 = int_range<1> (UINT (1), UINT (1), VR_ANTI_RANGE);
3575 // r1 = ~[3,3]
3576 r1 = int_range<1> (UINT (3), UINT (3), VR_ANTI_RANGE);
3578 // vv = [0,0][2,2][4, MAX]
3579 int_range<3> vv = r0;
3580 vv.intersect (r1);
3582 ASSERT_TRUE (vv.contains_p (UINT (2)));
3583 ASSERT_TRUE (vv.num_pairs () == 3);
3585 // create r0 as legacy [1,1]
3586 r0 = int_range<1> (UINT (1), UINT (1));
3587 // And union it with [0,0][2,2][4,MAX] multi range
3588 r0.union_ (vv);
3589 // The result should be [0,2][4,MAX], or ~[3,3] but it must contain 2
3590 ASSERT_TRUE (r0.contains_p (UINT (2)));
3593 static void
3594 range_tests_nonzero_bits ()
3596 int_range<2> r0, r1;
3598 // Adding nonzero bits to a varying drops the varying.
3599 r0.set_varying (integer_type_node);
3600 r0.set_nonzero_bits (255);
3601 ASSERT_TRUE (!r0.varying_p ());
3602 // Dropping the nonzero bits brings us back to varying.
3603 r0.set_nonzero_bits (-1);
3604 ASSERT_TRUE (r0.varying_p ());
3606 // Test contains_p with nonzero bits.
3607 r0.set_zero (integer_type_node);
3608 ASSERT_TRUE (r0.contains_p (INT (0)));
3609 ASSERT_FALSE (r0.contains_p (INT (1)));
3610 r0.set_nonzero_bits (0xfe);
3611 ASSERT_FALSE (r0.contains_p (INT (0x100)));
3612 ASSERT_FALSE (r0.contains_p (INT (0x3)));
3614 // Union of nonzero bits.
3615 r0.set_varying (integer_type_node);
3616 r0.set_nonzero_bits (0xf0);
3617 r1.set_varying (integer_type_node);
3618 r1.set_nonzero_bits (0xf);
3619 r0.union_ (r1);
3620 ASSERT_TRUE (r0.get_nonzero_bits () == 0xff);
3622 // Intersect of nonzero bits.
3623 r0.set (INT (0), INT (255));
3624 r0.set_nonzero_bits (0xfe);
3625 r1.set_varying (integer_type_node);
3626 r1.set_nonzero_bits (0xf0);
3627 r0.intersect (r1);
3628 ASSERT_TRUE (r0.get_nonzero_bits () == 0xf0);
3630 // Intersect where the mask of nonzero bits is implicit from the range.
3631 r0.set_varying (integer_type_node);
3632 r1.set (INT (0), INT (255));
3633 r0.intersect (r1);
3634 ASSERT_TRUE (r0.get_nonzero_bits () == 0xff);
3636 // The union of a mask of 0xff..ffff00 with a mask of 0xff spans the
3637 // entire domain, and makes the range a varying.
3638 r0.set_varying (integer_type_node);
3639 wide_int x = wi::shwi (0xff, TYPE_PRECISION (integer_type_node));
3640 x = wi::bit_not (x);
3641 r0.set_nonzero_bits (x); // 0xff..ff00
3642 r1.set_varying (integer_type_node);
3643 r1.set_nonzero_bits (0xff);
3644 r0.union_ (r1);
3645 ASSERT_TRUE (r0.varying_p ());
3647 // Test that setting a nonzero bit of 1 does not pessimize the range.
3648 r0.set_zero (integer_type_node);
3649 r0.set_nonzero_bits (1);
3650 ASSERT_TRUE (r0.zero_p ());
3653 // Build an frange from string endpoints.
3655 static inline frange
3656 frange_float (const char *lb, const char *ub, tree type = float_type_node)
3658 REAL_VALUE_TYPE min, max;
3659 gcc_assert (real_from_string (&min, lb) == 0);
3660 gcc_assert (real_from_string (&max, ub) == 0);
3661 return frange (type, min, max);
3664 static void
3665 range_tests_nan ()
3667 frange r0, r1;
3668 REAL_VALUE_TYPE q, r;
3669 bool signbit;
3671 // Equal ranges but with differing NAN bits are not equal.
3672 if (HONOR_NANS (float_type_node))
3674 r1 = frange_float ("10", "12");
3675 r0 = r1;
3676 ASSERT_EQ (r0, r1);
3677 r0.clear_nan ();
3678 ASSERT_NE (r0, r1);
3679 r0.update_nan ();
3680 ASSERT_EQ (r0, r1);
3682 // [10, 20] NAN ^ [30, 40] NAN = NAN.
3683 r0 = frange_float ("10", "20");
3684 r1 = frange_float ("30", "40");
3685 r0.intersect (r1);
3686 ASSERT_TRUE (r0.known_isnan ());
3688 // [3,5] U [5,10] NAN = ... NAN
3689 r0 = frange_float ("3", "5");
3690 r0.clear_nan ();
3691 r1 = frange_float ("5", "10");
3692 r0.union_ (r1);
3693 ASSERT_TRUE (r0.maybe_isnan ());
3696 // NAN ranges are not equal to each other.
3697 r0.set_nan (float_type_node);
3698 r1 = r0;
3699 ASSERT_FALSE (r0 == r1);
3700 ASSERT_FALSE (r0 == r0);
3701 ASSERT_TRUE (r0 != r0);
3703 // [5,6] U NAN = [5,6] NAN.
3704 r0 = frange_float ("5", "6");
3705 r0.clear_nan ();
3706 r1.set_nan (float_type_node);
3707 r0.union_ (r1);
3708 real_from_string (&q, "5");
3709 real_from_string (&r, "6");
3710 ASSERT_TRUE (real_identical (&q, &r0.lower_bound ()));
3711 ASSERT_TRUE (real_identical (&r, &r0.upper_bound ()));
3712 ASSERT_TRUE (r0.maybe_isnan ());
3714 // NAN U NAN = NAN
3715 r0.set_nan (float_type_node);
3716 r1.set_nan (float_type_node);
3717 r0.union_ (r1);
3718 ASSERT_TRUE (r0.known_isnan ());
3720 // [INF, INF] NAN ^ NAN = NAN
3721 r0.set_nan (float_type_node);
3722 r1 = frange_float ("+Inf", "+Inf");
3723 if (!HONOR_NANS (float_type_node))
3724 r1.update_nan ();
3725 r0.intersect (r1);
3726 ASSERT_TRUE (r0.known_isnan ());
3728 // NAN ^ NAN = NAN
3729 r0.set_nan (float_type_node);
3730 r1.set_nan (float_type_node);
3731 r0.intersect (r1);
3732 ASSERT_TRUE (r0.known_isnan ());
3734 // +NAN ^ -NAN = UNDEFINED
3735 r0.set_nan (float_type_node, false);
3736 r1.set_nan (float_type_node, true);
3737 r0.intersect (r1);
3738 ASSERT_TRUE (r0.undefined_p ());
3740 // VARYING ^ NAN = NAN.
3741 r0.set_nan (float_type_node);
3742 r1.set_varying (float_type_node);
3743 r0.intersect (r1);
3744 ASSERT_TRUE (r0.known_isnan ());
3746 // [3,4] ^ NAN = UNDEFINED.
3747 r0 = frange_float ("3", "4");
3748 r0.clear_nan ();
3749 r1.set_nan (float_type_node);
3750 r0.intersect (r1);
3751 ASSERT_TRUE (r0.undefined_p ());
3753 // [-3, 5] ^ NAN = UNDEFINED
3754 r0 = frange_float ("-3", "5");
3755 r0.clear_nan ();
3756 r1.set_nan (float_type_node);
3757 r0.intersect (r1);
3758 ASSERT_TRUE (r0.undefined_p ());
3760 // Setting the NAN bit to yes does not make us a known NAN.
3761 r0.set_varying (float_type_node);
3762 r0.update_nan ();
3763 ASSERT_FALSE (r0.known_isnan ());
3765 // NAN is in a VARYING.
3766 r0.set_varying (float_type_node);
3767 real_nan (&r, "", 1, TYPE_MODE (float_type_node));
3768 tree nan = build_real (float_type_node, r);
3769 ASSERT_TRUE (r0.contains_p (nan));
3771 // -NAN is in a VARYING.
3772 r0.set_varying (float_type_node);
3773 q = real_value_negate (&r);
3774 tree neg_nan = build_real (float_type_node, q);
3775 ASSERT_TRUE (r0.contains_p (neg_nan));
3777 // Clearing the NAN on a [] NAN is the empty set.
3778 r0.set_nan (float_type_node);
3779 r0.clear_nan ();
3780 ASSERT_TRUE (r0.undefined_p ());
3782 // [10,20] NAN ^ [21,25] NAN = [NAN]
3783 r0 = frange_float ("10", "20");
3784 r0.update_nan ();
3785 r1 = frange_float ("21", "25");
3786 r1.update_nan ();
3787 r0.intersect (r1);
3788 ASSERT_TRUE (r0.known_isnan ());
3790 // NAN U [5,6] should be [5,6] +-NAN.
3791 r0.set_nan (float_type_node);
3792 r1 = frange_float ("5", "6");
3793 r1.clear_nan ();
3794 r0.union_ (r1);
3795 real_from_string (&q, "5");
3796 real_from_string (&r, "6");
3797 ASSERT_TRUE (real_identical (&q, &r0.lower_bound ()));
3798 ASSERT_TRUE (real_identical (&r, &r0.upper_bound ()));
3799 ASSERT_TRUE (!r0.signbit_p (signbit));
3800 ASSERT_TRUE (r0.maybe_isnan ());
3803 static void
3804 range_tests_signed_zeros ()
3806 tree zero = build_zero_cst (float_type_node);
3807 tree neg_zero = fold_build1 (NEGATE_EXPR, float_type_node, zero);
3808 frange r0, r1;
3809 bool signbit;
3811 // [0,0] contains [0,0] but not [-0,-0] and vice versa.
3812 r0 = frange (zero, zero);
3813 r1 = frange (neg_zero, neg_zero);
3814 ASSERT_TRUE (r0.contains_p (zero));
3815 ASSERT_TRUE (!r0.contains_p (neg_zero));
3816 ASSERT_TRUE (r1.contains_p (neg_zero));
3817 ASSERT_TRUE (!r1.contains_p (zero));
3819 // Test contains_p() when we know the sign of the zero.
3820 r0 = frange (zero, zero);
3821 ASSERT_TRUE (r0.contains_p (zero));
3822 ASSERT_FALSE (r0.contains_p (neg_zero));
3823 r0 = frange (neg_zero, neg_zero);
3824 ASSERT_TRUE (r0.contains_p (neg_zero));
3825 ASSERT_FALSE (r0.contains_p (zero));
3827 // The intersection of zeros that differ in sign is a NAN (or
3828 // undefined if not honoring NANs).
3829 r0 = frange (neg_zero, neg_zero);
3830 r1 = frange (zero, zero);
3831 r0.intersect (r1);
3832 if (HONOR_NANS (float_type_node))
3833 ASSERT_TRUE (r0.known_isnan ());
3834 else
3835 ASSERT_TRUE (r0.undefined_p ());
3837 // The union of zeros that differ in sign is a zero with unknown sign.
3838 r0 = frange (zero, zero);
3839 r1 = frange (neg_zero, neg_zero);
3840 r0.union_ (r1);
3841 ASSERT_TRUE (r0.zero_p () && !r0.signbit_p (signbit));
3843 // [-0, +0] has an unknown sign.
3844 r0 = frange (neg_zero, zero);
3845 ASSERT_TRUE (r0.zero_p () && !r0.signbit_p (signbit));
3847 // [-0, +0] ^ [0, 0] is [0, 0]
3848 r0 = frange (neg_zero, zero);
3849 r1 = frange (zero, zero);
3850 r0.intersect (r1);
3851 ASSERT_TRUE (r0.zero_p ());
3853 r0 = frange_float ("+0", "5");
3854 r0.clear_nan ();
3855 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
3857 r0 = frange_float ("-0", "5");
3858 r0.clear_nan ();
3859 ASSERT_TRUE (!r0.signbit_p (signbit));
3861 r0 = frange_float ("-0", "10");
3862 r1 = frange_float ("0", "5");
3863 r0.intersect (r1);
3864 ASSERT_TRUE (real_iszero (&r0.lower_bound (), false));
3866 r0 = frange_float ("-0", "5");
3867 r1 = frange_float ("0", "5");
3868 r0.union_ (r1);
3869 ASSERT_TRUE (real_iszero (&r0.lower_bound (), true));
3871 r0 = frange_float ("-5", "-0");
3872 r0.update_nan ();
3873 r1 = frange_float ("0", "0");
3874 r1.update_nan ();
3875 r0.intersect (r1);
3876 if (HONOR_NANS (float_type_node))
3877 ASSERT_TRUE (r0.known_isnan ());
3878 else
3879 ASSERT_TRUE (r0.undefined_p ());
3881 r0.set_nonnegative (float_type_node);
3882 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
3883 if (HONOR_NANS (float_type_node))
3884 ASSERT_TRUE (r0.maybe_isnan ());
3887 static void
3888 range_tests_signbit ()
3890 frange r0, r1;
3891 bool signbit;
3893 // Negative numbers should have the SIGNBIT set.
3894 r0 = frange_float ("-5", "-1");
3895 r0.clear_nan ();
3896 ASSERT_TRUE (r0.signbit_p (signbit) && signbit);
3897 // Positive numbers should have the SIGNBIT clear.
3898 r0 = frange_float ("1", "10");
3899 r0.clear_nan ();
3900 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
3901 // Numbers containing zero should have an unknown SIGNBIT.
3902 r0 = frange_float ("0", "10");
3903 r0.clear_nan ();
3904 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
3905 // Numbers spanning both positive and negative should have an
3906 // unknown SIGNBIT.
3907 r0 = frange_float ("-10", "10");
3908 r0.clear_nan ();
3909 ASSERT_TRUE (!r0.signbit_p (signbit));
3910 r0.set_varying (float_type_node);
3911 ASSERT_TRUE (!r0.signbit_p (signbit));
3914 static void
3915 range_tests_floats ()
3917 frange r0, r1;
3919 if (HONOR_NANS (float_type_node))
3920 range_tests_nan ();
3921 range_tests_signbit ();
3923 if (HONOR_SIGNED_ZEROS (float_type_node))
3924 range_tests_signed_zeros ();
3926 // A range of [-INF,+INF] is actually VARYING if no other properties
3927 // are set.
3928 r0 = frange_float ("-Inf", "+Inf");
3929 if (r0.maybe_isnan ())
3930 ASSERT_TRUE (r0.varying_p ());
3931 // ...unless it has some special property...
3932 r0.clear_nan ();
3933 ASSERT_FALSE (r0.varying_p ());
3935 // For most architectures, where float and double are different
3936 // sizes, having the same endpoints does not necessarily mean the
3937 // ranges are equal.
3938 if (!types_compatible_p (float_type_node, double_type_node))
3940 r0 = frange_float ("3.0", "3.0", float_type_node);
3941 r1 = frange_float ("3.0", "3.0", double_type_node);
3942 ASSERT_NE (r0, r1);
3945 // [3,5] U [10,12] = [3,12].
3946 r0 = frange_float ("3", "5");
3947 r1 = frange_float ("10", "12");
3948 r0.union_ (r1);
3949 ASSERT_EQ (r0, frange_float ("3", "12"));
3951 // [5,10] U [4,8] = [4,10]
3952 r0 = frange_float ("5", "10");
3953 r1 = frange_float ("4", "8");
3954 r0.union_ (r1);
3955 ASSERT_EQ (r0, frange_float ("4", "10"));
3957 // [3,5] U [4,10] = [3,10]
3958 r0 = frange_float ("3", "5");
3959 r1 = frange_float ("4", "10");
3960 r0.union_ (r1);
3961 ASSERT_EQ (r0, frange_float ("3", "10"));
3963 // [4,10] U [5,11] = [4,11]
3964 r0 = frange_float ("4", "10");
3965 r1 = frange_float ("5", "11");
3966 r0.union_ (r1);
3967 ASSERT_EQ (r0, frange_float ("4", "11"));
3969 // [3,12] ^ [10,12] = [10,12].
3970 r0 = frange_float ("3", "12");
3971 r1 = frange_float ("10", "12");
3972 r0.intersect (r1);
3973 ASSERT_EQ (r0, frange_float ("10", "12"));
3975 // [10,12] ^ [11,11] = [11,11]
3976 r0 = frange_float ("10", "12");
3977 r1 = frange_float ("11", "11");
3978 r0.intersect (r1);
3979 ASSERT_EQ (r0, frange_float ("11", "11"));
3981 // [10,20] ^ [5,15] = [10,15]
3982 r0 = frange_float ("10", "20");
3983 r1 = frange_float ("5", "15");
3984 r0.intersect (r1);
3985 ASSERT_EQ (r0, frange_float ("10", "15"));
3987 // [10,20] ^ [15,25] = [15,20]
3988 r0 = frange_float ("10", "20");
3989 r1 = frange_float ("15", "25");
3990 r0.intersect (r1);
3991 ASSERT_EQ (r0, frange_float ("15", "20"));
3993 // [10,20] ^ [21,25] = []
3994 r0 = frange_float ("10", "20");
3995 r0.clear_nan ();
3996 r1 = frange_float ("21", "25");
3997 r1.clear_nan ();
3998 r0.intersect (r1);
3999 ASSERT_TRUE (r0.undefined_p ());
4002 void
4003 range_tests ()
4005 range_tests_legacy ();
4006 range_tests_irange3 ();
4007 range_tests_int_range_max ();
4008 range_tests_strict_enum ();
4009 range_tests_nonzero_bits ();
4010 range_tests_floats ();
4011 range_tests_misc ();
4014 } // namespace selftest
4016 #endif // CHECKING_P