Suppress -fstack-protector warning on hppa.
[official-gcc.git] / gcc / value-range.cc
blob34fac636cada2cc96be26f4818c693316f18cb21
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 machine_mode mode = TYPE_MODE (type ());
270 // Flush [x, -DENORMAL] to [x, -0.0].
271 if (real_isdenormal (&m_max, mode) && real_isneg (&m_max))
273 m_max = dconst0;
274 if (HONOR_SIGNED_ZEROS (m_type))
275 m_max.sign = 1;
277 // Flush [+DENORMAL, x] to [+0.0, x].
278 if (real_isdenormal (&m_min, mode) && !real_isneg (&m_min))
279 m_min = dconst0;
282 // Setter for franges.
284 void
285 frange::set (tree type,
286 const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
287 value_range_kind kind)
289 switch (kind)
291 case VR_UNDEFINED:
292 set_undefined ();
293 return;
294 case VR_VARYING:
295 case VR_ANTI_RANGE:
296 set_varying (type);
297 return;
298 case VR_RANGE:
299 break;
300 default:
301 gcc_unreachable ();
304 // Handle NANs.
305 if (real_isnan (&min) || real_isnan (&max))
307 gcc_checking_assert (real_identical (&min, &max));
308 bool sign = real_isneg (&min);
309 set_nan (type, sign);
310 return;
313 m_kind = kind;
314 m_type = type;
315 m_min = min;
316 m_max = max;
317 if (HONOR_NANS (m_type))
319 m_pos_nan = true;
320 m_neg_nan = true;
322 else
324 m_pos_nan = false;
325 m_neg_nan = false;
328 if (!MODE_HAS_SIGNED_ZEROS (TYPE_MODE (m_type)))
330 if (real_iszero (&m_min, 1))
331 m_min.sign = 0;
332 if (real_iszero (&m_max, 1))
333 m_max.sign = 0;
335 else if (!HONOR_SIGNED_ZEROS (m_type))
337 if (real_iszero (&m_max, 1))
338 m_max.sign = 0;
339 if (real_iszero (&m_min, 0))
340 m_min.sign = 1;
343 // For -ffinite-math-only we can drop ranges outside the
344 // representable numbers to min/max for the type.
345 if (!HONOR_INFINITIES (m_type))
347 REAL_VALUE_TYPE min_repr = frange_val_min (m_type);
348 REAL_VALUE_TYPE max_repr = frange_val_max (m_type);
349 if (real_less (&m_min, &min_repr))
350 m_min = min_repr;
351 else if (real_less (&max_repr, &m_min))
352 m_min = max_repr;
353 if (real_less (&max_repr, &m_max))
354 m_max = max_repr;
355 else if (real_less (&m_max, &min_repr))
356 m_max = min_repr;
359 // Check for swapped ranges.
360 gcc_checking_assert (real_compare (LE_EXPR, &min, &max));
362 normalize_kind ();
364 flush_denormals_to_zero ();
366 if (flag_checking)
367 verify_range ();
370 void
371 frange::set (tree min, tree max, value_range_kind kind)
373 set (TREE_TYPE (min),
374 *TREE_REAL_CST_PTR (min), *TREE_REAL_CST_PTR (max), kind);
377 // Normalize range to VARYING or UNDEFINED, or vice versa. Return
378 // TRUE if anything changed.
380 // A range with no known properties can be dropped to VARYING.
381 // Similarly, a VARYING with any properties should be dropped to a
382 // VR_RANGE. Normalizing ranges upon changing them ensures there is
383 // only one representation for a given range.
385 bool
386 frange::normalize_kind ()
388 if (m_kind == VR_RANGE
389 && frange_val_is_min (m_min, m_type)
390 && frange_val_is_max (m_max, m_type))
392 if (!HONOR_NANS (m_type) || (m_pos_nan && m_neg_nan))
394 set_varying (m_type);
395 return true;
398 else if (m_kind == VR_VARYING)
400 if (HONOR_NANS (m_type) && (!m_pos_nan || !m_neg_nan))
402 m_kind = VR_RANGE;
403 m_min = frange_val_min (m_type);
404 m_max = frange_val_max (m_type);
405 return true;
408 else if (m_kind == VR_NAN && !m_pos_nan && !m_neg_nan)
409 set_undefined ();
410 return false;
413 // Union or intersect the zero endpoints of two ranges. For example:
414 // [-0, x] U [+0, x] => [-0, x]
415 // [ x, -0] U [ x, +0] => [ x, +0]
416 // [-0, x] ^ [+0, x] => [+0, x]
417 // [ x, -0] ^ [ x, +0] => [ x, -0]
419 // UNION_P is true when performing a union, or false when intersecting.
421 bool
422 frange::combine_zeros (const frange &r, bool union_p)
424 gcc_checking_assert (!undefined_p () && !known_isnan ());
426 bool changed = false;
427 if (real_iszero (&m_min) && real_iszero (&r.m_min)
428 && real_isneg (&m_min) != real_isneg (&r.m_min))
430 m_min.sign = union_p;
431 changed = true;
433 if (real_iszero (&m_max) && real_iszero (&r.m_max)
434 && real_isneg (&m_max) != real_isneg (&r.m_max))
436 m_max.sign = !union_p;
437 changed = true;
439 // If the signs are swapped, the resulting range is empty.
440 if (m_min.sign == 0 && m_max.sign == 1)
442 if (maybe_isnan ())
443 m_kind = VR_NAN;
444 else
445 set_undefined ();
446 changed = true;
448 return changed;
451 // Union two ranges when one is known to be a NAN.
453 bool
454 frange::union_nans (const frange &r)
456 gcc_checking_assert (known_isnan () || r.known_isnan ());
458 if (known_isnan ())
460 m_kind = r.m_kind;
461 m_min = r.m_min;
462 m_max = r.m_max;
464 m_pos_nan |= r.m_pos_nan;
465 m_neg_nan |= r.m_neg_nan;
466 normalize_kind ();
467 if (flag_checking)
468 verify_range ();
469 return true;
472 bool
473 frange::union_ (const vrange &v)
475 const frange &r = as_a <frange> (v);
477 if (r.undefined_p () || varying_p ())
478 return false;
479 if (undefined_p () || r.varying_p ())
481 *this = r;
482 return true;
485 // Combine NAN info.
486 if (known_isnan () || r.known_isnan ())
487 return union_nans (r);
488 bool changed = false;
489 if (m_pos_nan != r.m_pos_nan || m_neg_nan != r.m_neg_nan)
491 m_pos_nan |= r.m_pos_nan;
492 m_neg_nan |= r.m_neg_nan;
493 changed = true;
496 // Combine endpoints.
497 if (real_less (&r.m_min, &m_min))
499 m_min = r.m_min;
500 changed = true;
502 if (real_less (&m_max, &r.m_max))
504 m_max = r.m_max;
505 changed = true;
508 if (HONOR_SIGNED_ZEROS (m_type))
509 changed |= combine_zeros (r, true);
511 changed |= normalize_kind ();
512 if (flag_checking)
513 verify_range ();
514 return changed;
517 // Intersect two ranges when one is known to be a NAN.
519 bool
520 frange::intersect_nans (const frange &r)
522 gcc_checking_assert (known_isnan () || r.known_isnan ());
524 m_pos_nan &= r.m_pos_nan;
525 m_neg_nan &= r.m_neg_nan;
526 if (maybe_isnan ())
527 m_kind = VR_NAN;
528 else
529 set_undefined ();
530 if (flag_checking)
531 verify_range ();
532 return true;
535 bool
536 frange::intersect (const vrange &v)
538 const frange &r = as_a <frange> (v);
540 if (undefined_p () || r.varying_p ())
541 return false;
542 if (r.undefined_p ())
544 set_undefined ();
545 return true;
547 if (varying_p ())
549 *this = r;
550 return true;
553 // Combine NAN info.
554 if (known_isnan () || r.known_isnan ())
555 return intersect_nans (r);
556 bool changed = false;
557 if (m_pos_nan != r.m_pos_nan || m_neg_nan != r.m_neg_nan)
559 m_pos_nan &= r.m_pos_nan;
560 m_neg_nan &= r.m_neg_nan;
561 changed = true;
564 // Combine endpoints.
565 if (real_less (&m_min, &r.m_min))
567 m_min = r.m_min;
568 changed = true;
570 if (real_less (&r.m_max, &m_max))
572 m_max = r.m_max;
573 changed = true;
575 // If the endpoints are swapped, the resulting range is empty.
576 if (real_less (&m_max, &m_min))
578 if (maybe_isnan ())
579 m_kind = VR_NAN;
580 else
581 set_undefined ();
582 if (flag_checking)
583 verify_range ();
584 return true;
587 if (HONOR_SIGNED_ZEROS (m_type))
588 changed |= combine_zeros (r, false);
590 changed |= normalize_kind ();
591 if (flag_checking)
592 verify_range ();
593 return changed;
596 frange &
597 frange::operator= (const frange &src)
599 m_kind = src.m_kind;
600 m_type = src.m_type;
601 m_min = src.m_min;
602 m_max = src.m_max;
603 m_pos_nan = src.m_pos_nan;
604 m_neg_nan = src.m_neg_nan;
606 if (flag_checking)
607 verify_range ();
608 return *this;
611 bool
612 frange::operator== (const frange &src) const
614 if (m_kind == src.m_kind)
616 if (undefined_p ())
617 return true;
619 if (varying_p ())
620 return types_compatible_p (m_type, src.m_type);
622 if (known_isnan () || src.known_isnan ())
623 return false;
625 return (real_identical (&m_min, &src.m_min)
626 && real_identical (&m_max, &src.m_max)
627 && m_pos_nan == src.m_pos_nan
628 && m_neg_nan == src.m_neg_nan
629 && types_compatible_p (m_type, src.m_type));
631 return false;
634 // Return TRUE if range contains the TREE_REAL_CST_PTR in CST.
636 bool
637 frange::contains_p (tree cst) const
639 gcc_checking_assert (m_kind != VR_ANTI_RANGE);
640 const REAL_VALUE_TYPE *rv = TREE_REAL_CST_PTR (cst);
642 if (undefined_p ())
643 return false;
645 if (varying_p ())
646 return true;
648 if (real_isnan (rv))
650 // No NAN in range.
651 if (!m_pos_nan && !m_neg_nan)
652 return false;
653 // Both +NAN and -NAN are present.
654 if (m_pos_nan && m_neg_nan)
655 return true;
656 return m_neg_nan == rv->sign;
658 if (known_isnan ())
659 return false;
661 if (real_compare (GE_EXPR, rv, &m_min) && real_compare (LE_EXPR, rv, &m_max))
663 // Make sure the signs are equal for signed zeros.
664 if (HONOR_SIGNED_ZEROS (m_type) && real_iszero (rv))
665 return rv->sign == m_min.sign || rv->sign == m_max.sign;
666 return true;
668 return false;
671 // If range is a singleton, place it in RESULT and return TRUE. If
672 // RESULT is NULL, just return TRUE.
674 // A NAN can never be a singleton.
676 bool
677 frange::singleton_p (tree *result) const
679 if (m_kind == VR_RANGE && real_identical (&m_min, &m_max))
681 // Return false for any singleton that may be a NAN.
682 if (HONOR_NANS (m_type) && maybe_isnan ())
683 return false;
685 if (MODE_COMPOSITE_P (TYPE_MODE (m_type)))
687 // For IBM long doubles, if the value is +-Inf or is exactly
688 // representable in double, the other double could be +0.0
689 // or -0.0. Since this means there is more than one way to
690 // represent a value, return false to avoid propagating it.
691 // See libgcc/config/rs6000/ibm-ldouble-format for details.
692 if (real_isinf (&m_min))
693 return false;
694 REAL_VALUE_TYPE r;
695 real_convert (&r, DFmode, &m_min);
696 if (real_identical (&r, &m_min))
697 return false;
700 if (result)
701 *result = build_real (m_type, m_min);
702 return true;
704 return false;
707 bool
708 frange::supports_type_p (const_tree type) const
710 return supports_p (type);
713 void
714 frange::verify_range ()
716 if (!undefined_p ())
717 gcc_checking_assert (HONOR_NANS (m_type) || !maybe_isnan ());
718 switch (m_kind)
720 case VR_UNDEFINED:
721 gcc_checking_assert (!m_type);
722 return;
723 case VR_VARYING:
724 gcc_checking_assert (m_type);
725 gcc_checking_assert (frange_val_is_min (m_min, m_type));
726 gcc_checking_assert (frange_val_is_max (m_max, m_type));
727 if (HONOR_NANS (m_type))
728 gcc_checking_assert (m_pos_nan && m_neg_nan);
729 else
730 gcc_checking_assert (!m_pos_nan && !m_neg_nan);
731 return;
732 case VR_RANGE:
733 gcc_checking_assert (m_type);
734 break;
735 case VR_NAN:
736 gcc_checking_assert (m_type);
737 gcc_checking_assert (m_pos_nan || m_neg_nan);
738 return;
739 default:
740 gcc_unreachable ();
743 // NANs cannot appear in the endpoints of a range.
744 gcc_checking_assert (!real_isnan (&m_min) && !real_isnan (&m_max));
746 // Make sure we don't have swapped ranges.
747 gcc_checking_assert (!real_less (&m_max, &m_min));
749 // [ +0.0, -0.0 ] is nonsensical.
750 gcc_checking_assert (!(real_iszero (&m_min, 0) && real_iszero (&m_max, 1)));
752 // If all the properties are clear, we better not span the entire
753 // domain, because that would make us varying.
754 if (m_pos_nan && m_neg_nan)
755 gcc_checking_assert (!frange_val_is_min (m_min, m_type)
756 || !frange_val_is_max (m_max, m_type));
759 // We can't do much with nonzeros yet.
760 void
761 frange::set_nonzero (tree type)
763 set_varying (type);
766 // We can't do much with nonzeros yet.
767 bool
768 frange::nonzero_p () const
770 return false;
773 // Set range to [+0.0, +0.0] if honoring signed zeros, or [0.0, 0.0]
774 // otherwise.
776 void
777 frange::set_zero (tree type)
779 if (HONOR_SIGNED_ZEROS (type))
781 REAL_VALUE_TYPE dconstm0 = dconst0;
782 dconstm0.sign = 1;
783 set (type, dconstm0, dconst0);
784 clear_nan ();
786 else
787 set (type, dconst0, dconst0);
790 // Return TRUE for any zero regardless of sign.
792 bool
793 frange::zero_p () const
795 return (m_kind == VR_RANGE
796 && real_iszero (&m_min)
797 && real_iszero (&m_max));
800 // Set the range to non-negative numbers, that is [+0.0, +INF].
802 // The NAN in the resulting range (if HONOR_NANS) has a varying sign
803 // as there are no guarantees in IEEE 754 wrt to the sign of a NAN,
804 // except for copy, abs, and copysign. It is the responsibility of
805 // the caller to set the NAN's sign if desired.
807 void
808 frange::set_nonnegative (tree type)
810 set (type, dconst0, frange_val_max (type));
813 // Here we copy between any two irange's. The ranges can be legacy or
814 // multi-ranges, and copying between any combination works correctly.
816 irange &
817 irange::operator= (const irange &src)
819 if (legacy_mode_p ())
821 copy_to_legacy (src);
822 return *this;
824 if (src.legacy_mode_p ())
826 copy_legacy_to_multi_range (src);
827 return *this;
830 unsigned x;
831 unsigned lim = src.m_num_ranges;
832 if (lim > m_max_ranges)
833 lim = m_max_ranges;
835 for (x = 0; x < lim * 2; ++x)
836 m_base[x] = src.m_base[x];
838 // If the range didn't fit, the last range should cover the rest.
839 if (lim != src.m_num_ranges)
840 m_base[x - 1] = src.m_base[src.m_num_ranges * 2 - 1];
842 m_num_ranges = lim;
843 m_kind = src.m_kind;
844 m_nonzero_mask = src.m_nonzero_mask;
845 if (flag_checking)
846 verify_range ();
847 return *this;
850 // Return TRUE if range is a multi-range that can be represented as a
851 // VR_ANTI_RANGE.
853 bool
854 irange::maybe_anti_range () const
856 tree ttype = type ();
857 unsigned int precision = TYPE_PRECISION (ttype);
858 signop sign = TYPE_SIGN (ttype);
859 return (num_pairs () > 1
860 && precision > 1
861 && lower_bound () == wi::min_value (precision, sign)
862 && upper_bound () == wi::max_value (precision, sign));
865 void
866 irange::copy_legacy_to_multi_range (const irange &src)
868 gcc_checking_assert (src.legacy_mode_p ());
869 gcc_checking_assert (!legacy_mode_p ());
870 if (src.undefined_p ())
871 set_undefined ();
872 else if (src.varying_p ())
873 set_varying (src.type ());
874 else
876 if (range_has_numeric_bounds_p (&src))
877 set (src.min (), src.max (), src.kind ());
878 else
880 value_range cst (src);
881 cst.normalize_symbolics ();
882 gcc_checking_assert (cst.varying_p () || cst.kind () == VR_RANGE);
883 set (cst.min (), cst.max ());
888 // Copy any type of irange into a legacy.
890 void
891 irange::copy_to_legacy (const irange &src)
893 gcc_checking_assert (legacy_mode_p ());
894 // Handle legacy to legacy and other things that are easy to copy.
895 if (src.legacy_mode_p () || src.varying_p () || src.undefined_p ())
897 m_num_ranges = src.m_num_ranges;
898 m_base[0] = src.m_base[0];
899 m_base[1] = src.m_base[1];
900 m_kind = src.m_kind;
901 m_nonzero_mask = src.m_nonzero_mask;
902 return;
904 // Copy multi-range to legacy.
905 if (src.maybe_anti_range ())
907 int_range<3> r (src);
908 r.invert ();
909 // Use tree variants to save on tree -> wi -> tree conversions.
910 set (r.tree_lower_bound (0), r.tree_upper_bound (0), VR_ANTI_RANGE);
912 else
913 set (src.tree_lower_bound (), src.tree_upper_bound ());
916 // Swap MIN/MAX if they are out of order and adjust KIND appropriately.
918 static void
919 swap_out_of_order_endpoints (tree &min, tree &max, value_range_kind &kind)
921 gcc_checking_assert (kind != VR_UNDEFINED);
922 if (kind == VR_VARYING)
923 return;
924 /* Wrong order for min and max, to swap them and the VR type we need
925 to adjust them. */
926 if (tree_int_cst_lt (max, min))
928 tree one, tmp;
930 /* For one bit precision if max < min, then the swapped
931 range covers all values, so for VR_RANGE it is varying and
932 for VR_ANTI_RANGE empty range, so drop to varying as well. */
933 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
935 kind = VR_VARYING;
936 return;
939 one = build_int_cst (TREE_TYPE (min), 1);
940 tmp = int_const_binop (PLUS_EXPR, max, one);
941 max = int_const_binop (MINUS_EXPR, min, one);
942 min = tmp;
944 /* There's one corner case, if we had [C+1, C] before we now have
945 that again. But this represents an empty value range, so drop
946 to varying in this case. */
947 if (tree_int_cst_lt (max, min))
949 kind = VR_VARYING;
950 return;
952 kind = kind == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
956 void
957 irange::irange_set (tree min, tree max)
959 gcc_checking_assert (!POLY_INT_CST_P (min));
960 gcc_checking_assert (!POLY_INT_CST_P (max));
962 m_base[0] = min;
963 m_base[1] = max;
964 m_num_ranges = 1;
965 m_kind = VR_RANGE;
966 m_nonzero_mask = NULL;
967 normalize_kind ();
969 if (flag_checking)
970 verify_range ();
973 void
974 irange::irange_set_1bit_anti_range (tree min, tree max)
976 tree type = TREE_TYPE (min);
977 gcc_checking_assert (TYPE_PRECISION (type) == 1);
979 if (operand_equal_p (min, max))
981 // Since these are 1-bit quantities, they can only be [MIN,MIN]
982 // or [MAX,MAX].
983 if (vrp_val_is_min (min))
984 min = max = vrp_val_max (type);
985 else
986 min = max = vrp_val_min (type);
987 set (min, max);
989 else
991 // The only alternative is [MIN,MAX], which is the empty range.
992 gcc_checking_assert (vrp_val_is_min (min));
993 gcc_checking_assert (vrp_val_is_max (max));
994 set_undefined ();
996 if (flag_checking)
997 verify_range ();
1000 void
1001 irange::irange_set_anti_range (tree min, tree max)
1003 gcc_checking_assert (!POLY_INT_CST_P (min));
1004 gcc_checking_assert (!POLY_INT_CST_P (max));
1006 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
1008 irange_set_1bit_anti_range (min, max);
1009 return;
1012 // set an anti-range
1013 tree type = TREE_TYPE (min);
1014 signop sign = TYPE_SIGN (type);
1015 int_range<2> type_range (type);
1016 // Calculate INVERSE([I,J]) as [-MIN, I-1][J+1, +MAX].
1017 m_num_ranges = 0;
1018 wi::overflow_type ovf;
1020 wide_int w_min = wi::to_wide (min);
1021 if (wi::ne_p (w_min, type_range.lower_bound ()))
1023 wide_int lim1 = wi::sub (w_min, 1, sign, &ovf);
1024 gcc_checking_assert (ovf != wi::OVF_OVERFLOW);
1025 m_base[0] = type_range.tree_lower_bound (0);
1026 m_base[1] = wide_int_to_tree (type, lim1);
1027 m_num_ranges = 1;
1029 wide_int w_max = wi::to_wide (max);
1030 if (wi::ne_p (w_max, type_range.upper_bound ()))
1032 wide_int lim2 = wi::add (w_max, 1, sign, &ovf);
1033 gcc_checking_assert (ovf != wi::OVF_OVERFLOW);
1034 m_base[m_num_ranges * 2] = wide_int_to_tree (type, lim2);
1035 m_base[m_num_ranges * 2 + 1] = type_range.tree_upper_bound (0);
1036 ++m_num_ranges;
1039 m_kind = VR_RANGE;
1040 m_nonzero_mask = NULL;
1041 normalize_kind ();
1043 if (flag_checking)
1044 verify_range ();
1047 /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
1048 This means adjusting VRTYPE, MIN and MAX representing the case of a
1049 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
1050 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
1051 In corner cases where MAX+1 or MIN-1 wraps this will fall back
1052 to varying.
1053 This routine exists to ease canonicalization in the case where we
1054 extract ranges from var + CST op limit. */
1056 void
1057 irange::set (tree min, tree max, value_range_kind kind)
1059 if (kind == VR_UNDEFINED)
1061 irange::set_undefined ();
1062 return;
1065 if (kind == VR_VARYING
1066 || POLY_INT_CST_P (min)
1067 || POLY_INT_CST_P (max))
1069 set_varying (TREE_TYPE (min));
1070 return;
1073 if (TREE_OVERFLOW_P (min))
1074 min = drop_tree_overflow (min);
1075 if (TREE_OVERFLOW_P (max))
1076 max = drop_tree_overflow (max);
1078 if (!legacy_mode_p ())
1080 if (kind == VR_RANGE)
1081 irange_set (min, max);
1082 else
1084 gcc_checking_assert (kind == VR_ANTI_RANGE);
1085 irange_set_anti_range (min, max);
1087 return;
1089 // Nothing to canonicalize for symbolic ranges.
1090 if (TREE_CODE (min) != INTEGER_CST
1091 || TREE_CODE (max) != INTEGER_CST)
1093 m_kind = kind;
1094 m_base[0] = min;
1095 m_base[1] = max;
1096 m_num_ranges = 1;
1097 m_nonzero_mask = NULL;
1098 return;
1101 swap_out_of_order_endpoints (min, max, kind);
1102 if (kind == VR_VARYING)
1104 set_varying (TREE_TYPE (min));
1105 return;
1108 // Anti-ranges that can be represented as ranges should be so.
1109 if (kind == VR_ANTI_RANGE)
1111 bool is_min = vrp_val_is_min (min);
1112 bool is_max = vrp_val_is_max (max);
1114 if (is_min && is_max)
1116 // Fall through. This will either be normalized as
1117 // VR_UNDEFINED if the anti-range spans the entire
1118 // precision, or it will remain an VR_ANTI_RANGE in the case
1119 // of an -fstrict-enum where [MIN,MAX] is less than the span
1120 // of underlying precision.
1122 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
1124 irange_set_1bit_anti_range (min, max);
1125 return;
1127 else if (is_min)
1129 tree one = build_int_cst (TREE_TYPE (max), 1);
1130 min = int_const_binop (PLUS_EXPR, max, one);
1131 max = vrp_val_max (TREE_TYPE (max));
1132 kind = VR_RANGE;
1134 else if (is_max)
1136 tree one = build_int_cst (TREE_TYPE (min), 1);
1137 max = int_const_binop (MINUS_EXPR, min, one);
1138 min = vrp_val_min (TREE_TYPE (min));
1139 kind = VR_RANGE;
1143 m_kind = kind;
1144 m_base[0] = min;
1145 m_base[1] = max;
1146 m_num_ranges = 1;
1147 m_nonzero_mask = NULL;
1148 normalize_kind ();
1149 if (flag_checking)
1150 verify_range ();
1153 // Check the validity of the range.
1155 void
1156 irange::verify_range ()
1158 gcc_checking_assert (m_discriminator == VR_IRANGE);
1159 if (m_kind == VR_UNDEFINED)
1161 gcc_checking_assert (m_num_ranges == 0);
1162 return;
1164 if (m_kind == VR_VARYING)
1166 gcc_checking_assert (!m_nonzero_mask
1167 || wi::to_wide (m_nonzero_mask) == -1);
1168 gcc_checking_assert (m_num_ranges == 1);
1169 gcc_checking_assert (varying_compatible_p ());
1170 return;
1172 if (!legacy_mode_p ())
1174 gcc_checking_assert (m_num_ranges != 0);
1175 gcc_checking_assert (!varying_compatible_p ());
1176 for (unsigned i = 0; i < m_num_ranges; ++i)
1178 tree lb = tree_lower_bound (i);
1179 tree ub = tree_upper_bound (i);
1180 int c = compare_values (lb, ub);
1181 gcc_checking_assert (c == 0 || c == -1);
1183 return;
1185 if (m_kind == VR_RANGE || m_kind == VR_ANTI_RANGE)
1187 gcc_checking_assert (m_num_ranges == 1);
1188 int cmp = compare_values (tree_lower_bound (0), tree_upper_bound (0));
1189 gcc_checking_assert (cmp == 0 || cmp == -1 || cmp == -2);
1193 // Return the lower bound for a sub-range. PAIR is the sub-range in
1194 // question.
1196 wide_int
1197 irange::legacy_lower_bound (unsigned pair) const
1199 gcc_checking_assert (legacy_mode_p ());
1200 if (symbolic_p ())
1202 value_range numeric_range (*this);
1203 numeric_range.normalize_symbolics ();
1204 return numeric_range.legacy_lower_bound (pair);
1206 gcc_checking_assert (m_num_ranges > 0);
1207 gcc_checking_assert (pair + 1 <= num_pairs ());
1208 if (m_kind == VR_ANTI_RANGE)
1210 tree typ = type (), t;
1211 if (pair == 1 || vrp_val_is_min (min ()))
1212 t = wide_int_to_tree (typ, wi::to_wide (max ()) + 1);
1213 else
1214 t = vrp_val_min (typ);
1215 return wi::to_wide (t);
1217 return wi::to_wide (tree_lower_bound (pair));
1220 // Return the upper bound for a sub-range. PAIR is the sub-range in
1221 // question.
1223 wide_int
1224 irange::legacy_upper_bound (unsigned pair) const
1226 gcc_checking_assert (legacy_mode_p ());
1227 if (symbolic_p ())
1229 value_range numeric_range (*this);
1230 numeric_range.normalize_symbolics ();
1231 return numeric_range.legacy_upper_bound (pair);
1233 gcc_checking_assert (m_num_ranges > 0);
1234 gcc_checking_assert (pair + 1 <= num_pairs ());
1235 if (m_kind == VR_ANTI_RANGE)
1237 tree typ = type (), t;
1238 if (pair == 1 || vrp_val_is_min (min ()))
1239 t = vrp_val_max (typ);
1240 else
1241 t = wide_int_to_tree (typ, wi::to_wide (min ()) - 1);
1242 return wi::to_wide (t);
1244 return wi::to_wide (tree_upper_bound (pair));
1247 bool
1248 irange::legacy_equal_p (const irange &other) const
1250 gcc_checking_assert (legacy_mode_p () && other.legacy_mode_p ());
1252 if (m_kind != other.m_kind)
1253 return false;
1254 if (m_kind == VR_UNDEFINED)
1255 return true;
1256 if (m_kind == VR_VARYING)
1257 return range_compatible_p (type (), other.type ());
1258 return (vrp_operand_equal_p (tree_lower_bound (0),
1259 other.tree_lower_bound (0))
1260 && vrp_operand_equal_p (tree_upper_bound (0),
1261 other.tree_upper_bound (0))
1262 && get_nonzero_bits () == other.get_nonzero_bits ());
1265 bool
1266 irange::operator== (const irange &other) const
1268 if (legacy_mode_p ())
1270 if (other.legacy_mode_p ())
1271 return legacy_equal_p (other);
1272 value_range tmp (other);
1273 return legacy_equal_p (tmp);
1275 if (other.legacy_mode_p ())
1277 value_range tmp2 (*this);
1278 return tmp2.legacy_equal_p (other);
1281 if (m_num_ranges != other.m_num_ranges)
1282 return false;
1284 if (m_num_ranges == 0)
1285 return true;
1287 for (unsigned i = 0; i < m_num_ranges; ++i)
1289 tree lb = tree_lower_bound (i);
1290 tree ub = tree_upper_bound (i);
1291 tree lb_other = other.tree_lower_bound (i);
1292 tree ub_other = other.tree_upper_bound (i);
1293 if (!operand_equal_p (lb, lb_other, 0)
1294 || !operand_equal_p (ub, ub_other, 0))
1295 return false;
1297 return get_nonzero_bits () == other.get_nonzero_bits ();
1300 /* Return TRUE if this is a symbolic range. */
1302 bool
1303 irange::symbolic_p () const
1305 return (m_num_ranges > 0
1306 && (!is_gimple_min_invariant (min ())
1307 || !is_gimple_min_invariant (max ())));
1310 /* Return TRUE if this is a constant range. */
1312 bool
1313 irange::constant_p () const
1315 return (m_num_ranges > 0
1316 && TREE_CODE (min ()) == INTEGER_CST
1317 && TREE_CODE (max ()) == INTEGER_CST);
1320 /* If range is a singleton, place it in RESULT and return TRUE.
1321 Note: A singleton can be any gimple invariant, not just constants.
1322 So, [&x, &x] counts as a singleton. */
1324 bool
1325 irange::singleton_p (tree *result) const
1327 if (!legacy_mode_p ())
1329 if (num_pairs () == 1 && (wi::to_wide (tree_lower_bound ())
1330 == wi::to_wide (tree_upper_bound ())))
1332 if (result)
1333 *result = tree_lower_bound ();
1334 return true;
1336 return false;
1338 if (m_kind == VR_ANTI_RANGE)
1340 if (nonzero_p ())
1342 if (TYPE_PRECISION (type ()) == 1)
1344 if (result)
1345 *result = max ();
1346 return true;
1348 return false;
1350 if (num_pairs () == 1)
1352 value_range vr0, vr1;
1353 ranges_from_anti_range ((const value_range *) this, &vr0, &vr1);
1354 return vr0.singleton_p (result);
1357 // Catches non-numeric extremes as well.
1358 if (m_kind == VR_RANGE
1359 && vrp_operand_equal_p (min (), max ())
1360 && is_gimple_min_invariant (min ()))
1362 if (result)
1363 *result = min ();
1364 return true;
1366 return false;
1369 /* Return 1 if VAL is inside value range.
1370 0 if VAL is not inside value range.
1371 -2 if we cannot tell either way.
1373 Benchmark compile/20001226-1.c compilation time after changing this
1374 function. */
1377 irange::value_inside_range (tree val) const
1379 if (varying_p ())
1380 return 1;
1382 if (undefined_p ())
1383 return 0;
1385 if (!legacy_mode_p () && TREE_CODE (val) == INTEGER_CST)
1386 return contains_p (val);
1388 int cmp1 = operand_less_p (val, min ());
1389 if (cmp1 == -2)
1390 return -2;
1391 if (cmp1 == 1)
1392 return m_kind != VR_RANGE;
1394 int cmp2 = operand_less_p (max (), val);
1395 if (cmp2 == -2)
1396 return -2;
1398 if (m_kind == VR_RANGE)
1399 return !cmp2;
1400 else
1401 return !!cmp2;
1404 /* Return TRUE if it is possible that range contains VAL. */
1406 bool
1407 irange::may_contain_p (tree val) const
1409 return value_inside_range (val) != 0;
1412 /* Return TRUE if range contains INTEGER_CST. */
1413 /* Return 1 if VAL is inside value range.
1414 0 if VAL is not inside value range.
1416 Benchmark compile/20001226-1.c compilation time after changing this
1417 function. */
1420 bool
1421 irange::contains_p (tree cst) const
1423 if (undefined_p ())
1424 return false;
1426 if (legacy_mode_p ())
1428 gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST);
1429 if (symbolic_p ())
1431 value_range numeric_range (*this);
1432 numeric_range.normalize_symbolics ();
1433 return numeric_range.contains_p (cst);
1435 return value_inside_range (cst) == 1;
1438 gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST);
1440 // See if we can exclude CST based on the nonzero bits.
1441 if (m_nonzero_mask)
1443 wide_int cstw = wi::to_wide (cst);
1444 if (cstw != 0 && wi::bit_and (wi::to_wide (m_nonzero_mask), cstw) == 0)
1445 return false;
1448 signop sign = TYPE_SIGN (TREE_TYPE (cst));
1449 wide_int v = wi::to_wide (cst);
1450 for (unsigned r = 0; r < m_num_ranges; ++r)
1452 if (wi::lt_p (v, lower_bound (r), sign))
1453 return false;
1454 if (wi::le_p (v, upper_bound (r), sign))
1455 return true;
1458 return false;
1462 /* Normalize addresses into constants. */
1464 void
1465 irange::normalize_addresses ()
1467 if (undefined_p ())
1468 return;
1470 if (!POINTER_TYPE_P (type ()) || range_has_numeric_bounds_p (this))
1471 return;
1473 if (!range_includes_zero_p (this))
1475 gcc_checking_assert (TREE_CODE (min ()) == ADDR_EXPR
1476 || TREE_CODE (max ()) == ADDR_EXPR);
1477 set_nonzero (type ());
1478 return;
1480 set_varying (type ());
1483 /* Normalize symbolics and addresses into constants. */
1485 void
1486 irange::normalize_symbolics ()
1488 if (varying_p () || undefined_p ())
1489 return;
1491 tree ttype = type ();
1492 bool min_symbolic = !is_gimple_min_invariant (min ());
1493 bool max_symbolic = !is_gimple_min_invariant (max ());
1494 if (!min_symbolic && !max_symbolic)
1496 normalize_addresses ();
1497 return;
1500 // [SYM, SYM] -> VARYING
1501 if (min_symbolic && max_symbolic)
1503 set_varying (ttype);
1504 return;
1506 if (kind () == VR_RANGE)
1508 // [SYM, NUM] -> [-MIN, NUM]
1509 if (min_symbolic)
1511 set (vrp_val_min (ttype), max ());
1512 return;
1514 // [NUM, SYM] -> [NUM, +MAX]
1515 set (min (), vrp_val_max (ttype));
1516 return;
1518 gcc_checking_assert (kind () == VR_ANTI_RANGE);
1519 // ~[SYM, NUM] -> [NUM + 1, +MAX]
1520 if (min_symbolic)
1522 if (!vrp_val_is_max (max ()))
1524 tree n = wide_int_to_tree (ttype, wi::to_wide (max ()) + 1);
1525 set (n, vrp_val_max (ttype));
1526 return;
1528 set_varying (ttype);
1529 return;
1531 // ~[NUM, SYM] -> [-MIN, NUM - 1]
1532 if (!vrp_val_is_min (min ()))
1534 tree n = wide_int_to_tree (ttype, wi::to_wide (min ()) - 1);
1535 set (vrp_val_min (ttype), n);
1536 return;
1538 set_varying (ttype);
1541 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
1542 { VR1TYPE, VR0MIN, VR0MAX } and store the result
1543 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
1544 possible such range. The resulting range is not canonicalized. */
1546 static void
1547 intersect_ranges (enum value_range_kind *vr0type,
1548 tree *vr0min, tree *vr0max,
1549 enum value_range_kind vr1type,
1550 tree vr1min, tree vr1max)
1552 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
1553 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
1555 /* [] is vr0, () is vr1 in the following classification comments. */
1556 if (mineq && maxeq)
1558 /* [( )] */
1559 if (*vr0type == vr1type)
1560 /* Nothing to do for equal ranges. */
1562 else if ((*vr0type == VR_RANGE
1563 && vr1type == VR_ANTI_RANGE)
1564 || (*vr0type == VR_ANTI_RANGE
1565 && vr1type == VR_RANGE))
1567 /* For anti-range with range intersection the result is empty. */
1568 *vr0type = VR_UNDEFINED;
1569 *vr0min = NULL_TREE;
1570 *vr0max = NULL_TREE;
1572 else
1573 gcc_unreachable ();
1575 else if (operand_less_p (*vr0max, vr1min) == 1
1576 || operand_less_p (vr1max, *vr0min) == 1)
1578 /* [ ] ( ) or ( ) [ ]
1579 If the ranges have an empty intersection, the result of the
1580 intersect operation is the range for intersecting an
1581 anti-range with a range or empty when intersecting two ranges. */
1582 if (*vr0type == VR_RANGE
1583 && vr1type == VR_ANTI_RANGE)
1585 else if (*vr0type == VR_ANTI_RANGE
1586 && vr1type == VR_RANGE)
1588 *vr0type = vr1type;
1589 *vr0min = vr1min;
1590 *vr0max = vr1max;
1592 else if (*vr0type == VR_RANGE
1593 && vr1type == VR_RANGE)
1595 *vr0type = VR_UNDEFINED;
1596 *vr0min = NULL_TREE;
1597 *vr0max = NULL_TREE;
1599 else if (*vr0type == VR_ANTI_RANGE
1600 && vr1type == VR_ANTI_RANGE)
1602 /* If the anti-ranges are adjacent to each other merge them. */
1603 if (TREE_CODE (*vr0max) == INTEGER_CST
1604 && TREE_CODE (vr1min) == INTEGER_CST
1605 && operand_less_p (*vr0max, vr1min) == 1
1606 && integer_onep (int_const_binop (MINUS_EXPR,
1607 vr1min, *vr0max)))
1608 *vr0max = vr1max;
1609 else if (TREE_CODE (vr1max) == INTEGER_CST
1610 && TREE_CODE (*vr0min) == INTEGER_CST
1611 && operand_less_p (vr1max, *vr0min) == 1
1612 && integer_onep (int_const_binop (MINUS_EXPR,
1613 *vr0min, vr1max)))
1614 *vr0min = vr1min;
1615 /* Else arbitrarily take VR0. */
1618 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
1619 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
1621 /* [ ( ) ] or [( ) ] or [ ( )] */
1622 if (*vr0type == VR_RANGE
1623 && vr1type == VR_RANGE)
1625 /* If both are ranges the result is the inner one. */
1626 *vr0type = vr1type;
1627 *vr0min = vr1min;
1628 *vr0max = vr1max;
1630 else if (*vr0type == VR_RANGE
1631 && vr1type == VR_ANTI_RANGE)
1633 /* Choose the right gap if the left one is empty. */
1634 if (mineq)
1636 if (TREE_CODE (vr1max) != INTEGER_CST)
1637 *vr0min = vr1max;
1638 else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1
1639 && !TYPE_UNSIGNED (TREE_TYPE (vr1max)))
1640 *vr0min
1641 = int_const_binop (MINUS_EXPR, vr1max,
1642 build_int_cst (TREE_TYPE (vr1max), -1));
1643 else
1644 *vr0min
1645 = int_const_binop (PLUS_EXPR, vr1max,
1646 build_int_cst (TREE_TYPE (vr1max), 1));
1648 /* Choose the left gap if the right one is empty. */
1649 else if (maxeq)
1651 if (TREE_CODE (vr1min) != INTEGER_CST)
1652 *vr0max = vr1min;
1653 else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1
1654 && !TYPE_UNSIGNED (TREE_TYPE (vr1min)))
1655 *vr0max
1656 = int_const_binop (PLUS_EXPR, vr1min,
1657 build_int_cst (TREE_TYPE (vr1min), -1));
1658 else
1659 *vr0max
1660 = int_const_binop (MINUS_EXPR, vr1min,
1661 build_int_cst (TREE_TYPE (vr1min), 1));
1663 /* Choose the anti-range if the range is effectively varying. */
1664 else if (vrp_val_is_min (*vr0min)
1665 && vrp_val_is_max (*vr0max))
1667 *vr0type = vr1type;
1668 *vr0min = vr1min;
1669 *vr0max = vr1max;
1671 /* Else choose the range. */
1673 else if (*vr0type == VR_ANTI_RANGE
1674 && vr1type == VR_ANTI_RANGE)
1675 /* If both are anti-ranges the result is the outer one. */
1677 else if (*vr0type == VR_ANTI_RANGE
1678 && vr1type == VR_RANGE)
1680 /* The intersection is empty. */
1681 *vr0type = VR_UNDEFINED;
1682 *vr0min = NULL_TREE;
1683 *vr0max = NULL_TREE;
1685 else
1686 gcc_unreachable ();
1688 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
1689 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
1691 /* ( [ ] ) or ([ ] ) or ( [ ]) */
1692 if (*vr0type == VR_RANGE
1693 && vr1type == VR_RANGE)
1694 /* Choose the inner range. */
1696 else if (*vr0type == VR_ANTI_RANGE
1697 && vr1type == VR_RANGE)
1699 /* Choose the right gap if the left is empty. */
1700 if (mineq)
1702 *vr0type = VR_RANGE;
1703 if (TREE_CODE (*vr0max) != INTEGER_CST)
1704 *vr0min = *vr0max;
1705 else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1
1706 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max)))
1707 *vr0min
1708 = int_const_binop (MINUS_EXPR, *vr0max,
1709 build_int_cst (TREE_TYPE (*vr0max), -1));
1710 else
1711 *vr0min
1712 = int_const_binop (PLUS_EXPR, *vr0max,
1713 build_int_cst (TREE_TYPE (*vr0max), 1));
1714 *vr0max = vr1max;
1716 /* Choose the left gap if the right is empty. */
1717 else if (maxeq)
1719 *vr0type = VR_RANGE;
1720 if (TREE_CODE (*vr0min) != INTEGER_CST)
1721 *vr0max = *vr0min;
1722 else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1
1723 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min)))
1724 *vr0max
1725 = int_const_binop (PLUS_EXPR, *vr0min,
1726 build_int_cst (TREE_TYPE (*vr0min), -1));
1727 else
1728 *vr0max
1729 = int_const_binop (MINUS_EXPR, *vr0min,
1730 build_int_cst (TREE_TYPE (*vr0min), 1));
1731 *vr0min = vr1min;
1733 /* Choose the anti-range if the range is effectively varying. */
1734 else if (vrp_val_is_min (vr1min)
1735 && vrp_val_is_max (vr1max))
1737 /* Choose the anti-range if it is ~[0,0], that range is special
1738 enough to special case when vr1's range is relatively wide.
1739 At least for types bigger than int - this covers pointers
1740 and arguments to functions like ctz. */
1741 else if (*vr0min == *vr0max
1742 && integer_zerop (*vr0min)
1743 && ((TYPE_PRECISION (TREE_TYPE (*vr0min))
1744 >= TYPE_PRECISION (integer_type_node))
1745 || POINTER_TYPE_P (TREE_TYPE (*vr0min)))
1746 && TREE_CODE (vr1max) == INTEGER_CST
1747 && TREE_CODE (vr1min) == INTEGER_CST
1748 && (wi::clz (wi::to_wide (vr1max) - wi::to_wide (vr1min))
1749 < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2))
1751 /* Else choose the range. */
1752 else
1754 *vr0type = vr1type;
1755 *vr0min = vr1min;
1756 *vr0max = vr1max;
1759 else if (*vr0type == VR_ANTI_RANGE
1760 && vr1type == VR_ANTI_RANGE)
1762 /* If both are anti-ranges the result is the outer one. */
1763 *vr0type = vr1type;
1764 *vr0min = vr1min;
1765 *vr0max = vr1max;
1767 else if (vr1type == VR_ANTI_RANGE
1768 && *vr0type == VR_RANGE)
1770 /* The intersection is empty. */
1771 *vr0type = VR_UNDEFINED;
1772 *vr0min = NULL_TREE;
1773 *vr0max = NULL_TREE;
1775 else
1776 gcc_unreachable ();
1778 else if ((operand_less_p (vr1min, *vr0max) == 1
1779 || operand_equal_p (vr1min, *vr0max, 0))
1780 && operand_less_p (*vr0min, vr1min) == 1
1781 && operand_less_p (*vr0max, vr1max) == 1)
1783 /* [ ( ] ) or [ ]( ) */
1784 if (*vr0type == VR_ANTI_RANGE
1785 && vr1type == VR_ANTI_RANGE)
1786 *vr0max = vr1max;
1787 else if (*vr0type == VR_RANGE
1788 && vr1type == VR_RANGE)
1789 *vr0min = vr1min;
1790 else if (*vr0type == VR_RANGE
1791 && vr1type == VR_ANTI_RANGE)
1793 if (TREE_CODE (vr1min) == INTEGER_CST)
1794 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
1795 build_int_cst (TREE_TYPE (vr1min), 1));
1796 else
1797 *vr0max = vr1min;
1799 else if (*vr0type == VR_ANTI_RANGE
1800 && vr1type == VR_RANGE)
1802 *vr0type = VR_RANGE;
1803 if (TREE_CODE (*vr0max) == INTEGER_CST)
1804 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
1805 build_int_cst (TREE_TYPE (*vr0max), 1));
1806 else
1807 *vr0min = *vr0max;
1808 *vr0max = vr1max;
1810 else
1811 gcc_unreachable ();
1813 else if ((operand_less_p (*vr0min, vr1max) == 1
1814 || operand_equal_p (*vr0min, vr1max, 0))
1815 && operand_less_p (vr1min, *vr0min) == 1
1816 && operand_less_p (vr1max, *vr0max) == 1)
1818 /* ( [ ) ] or ( )[ ] */
1819 if (*vr0type == VR_ANTI_RANGE
1820 && vr1type == VR_ANTI_RANGE)
1821 *vr0min = vr1min;
1822 else if (*vr0type == VR_RANGE
1823 && vr1type == VR_RANGE)
1824 *vr0max = vr1max;
1825 else if (*vr0type == VR_RANGE
1826 && vr1type == VR_ANTI_RANGE)
1828 if (TREE_CODE (vr1max) == INTEGER_CST)
1829 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
1830 build_int_cst (TREE_TYPE (vr1max), 1));
1831 else
1832 *vr0min = vr1max;
1834 else if (*vr0type == VR_ANTI_RANGE
1835 && vr1type == VR_RANGE)
1837 *vr0type = VR_RANGE;
1838 if (TREE_CODE (*vr0min) == INTEGER_CST)
1839 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
1840 build_int_cst (TREE_TYPE (*vr0min), 1));
1841 else
1842 *vr0max = *vr0min;
1843 *vr0min = vr1min;
1845 else
1846 gcc_unreachable ();
1849 /* If we know the intersection is empty, there's no need to
1850 conservatively add anything else to the set. */
1851 if (*vr0type == VR_UNDEFINED)
1852 return;
1854 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
1855 result for the intersection. That's always a conservative
1856 correct estimate unless VR1 is a constant singleton range
1857 in which case we choose that. */
1858 if (vr1type == VR_RANGE
1859 && is_gimple_min_invariant (vr1min)
1860 && vrp_operand_equal_p (vr1min, vr1max))
1862 *vr0type = vr1type;
1863 *vr0min = vr1min;
1864 *vr0max = vr1max;
1868 /* Helper for the intersection operation for value ranges. Given two
1869 ranges VR0 and VR1, set VR0 to the intersection of both ranges.
1870 This may not be the smallest possible such range. */
1872 void
1873 irange::legacy_intersect (irange *vr0, const irange *vr1)
1875 gcc_checking_assert (vr0->legacy_mode_p ());
1876 gcc_checking_assert (vr1->legacy_mode_p ());
1877 /* If either range is VR_VARYING the other one wins. */
1878 if (vr1->varying_p ())
1879 return;
1880 if (vr0->varying_p ())
1882 vr0->set (vr1->min (), vr1->max (), vr1->kind ());
1883 return;
1886 /* When either range is VR_UNDEFINED the resulting range is
1887 VR_UNDEFINED, too. */
1888 if (vr0->undefined_p ())
1889 return;
1890 if (vr1->undefined_p ())
1892 vr0->set_undefined ();
1893 return;
1896 value_range_kind vr0kind = vr0->kind ();
1897 tree vr0min = vr0->min ();
1898 tree vr0max = vr0->max ();
1900 intersect_ranges (&vr0kind, &vr0min, &vr0max,
1901 vr1->kind (), vr1->min (), vr1->max ());
1903 /* Make sure to canonicalize the result though as the inversion of a
1904 VR_RANGE can still be a VR_RANGE. */
1905 if (vr0kind == VR_UNDEFINED)
1906 vr0->set_undefined ();
1907 else if (vr0kind == VR_VARYING)
1909 /* If we failed, use the original VR0. */
1910 return;
1912 else
1913 vr0->set (vr0min, vr0max, vr0kind);
1916 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
1917 { VR1TYPE, VR0MIN, VR0MAX } and store the result
1918 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
1919 possible such range. The resulting range is not canonicalized. */
1921 static void
1922 union_ranges (enum value_range_kind *vr0type,
1923 tree *vr0min, tree *vr0max,
1924 enum value_range_kind vr1type,
1925 tree vr1min, tree vr1max)
1927 int cmpmin = compare_values (*vr0min, vr1min);
1928 int cmpmax = compare_values (*vr0max, vr1max);
1929 bool mineq = cmpmin == 0;
1930 bool maxeq = cmpmax == 0;
1932 /* [] is vr0, () is vr1 in the following classification comments. */
1933 if (mineq && maxeq)
1935 /* [( )] */
1936 if (*vr0type == vr1type)
1937 /* Nothing to do for equal ranges. */
1939 else if ((*vr0type == VR_RANGE
1940 && vr1type == VR_ANTI_RANGE)
1941 || (*vr0type == VR_ANTI_RANGE
1942 && vr1type == VR_RANGE))
1944 /* For anti-range with range union the result is varying. */
1945 goto give_up;
1947 else
1948 gcc_unreachable ();
1950 else if (operand_less_p (*vr0max, vr1min) == 1
1951 || operand_less_p (vr1max, *vr0min) == 1)
1953 /* [ ] ( ) or ( ) [ ]
1954 If the ranges have an empty intersection, result of the union
1955 operation is the anti-range or if both are anti-ranges
1956 it covers all. */
1957 if (*vr0type == VR_ANTI_RANGE
1958 && vr1type == VR_ANTI_RANGE)
1959 goto give_up;
1960 else if (*vr0type == VR_ANTI_RANGE
1961 && vr1type == VR_RANGE)
1963 else if (*vr0type == VR_RANGE
1964 && vr1type == VR_ANTI_RANGE)
1966 *vr0type = vr1type;
1967 *vr0min = vr1min;
1968 *vr0max = vr1max;
1970 else if (*vr0type == VR_RANGE
1971 && vr1type == VR_RANGE)
1973 /* The result is the convex hull of both ranges. */
1974 if (operand_less_p (*vr0max, vr1min) == 1)
1976 /* If the result can be an anti-range, create one. */
1977 if (TREE_CODE (*vr0max) == INTEGER_CST
1978 && TREE_CODE (vr1min) == INTEGER_CST
1979 && vrp_val_is_min (*vr0min)
1980 && vrp_val_is_max (vr1max))
1982 tree min = int_const_binop (PLUS_EXPR,
1983 *vr0max,
1984 build_int_cst (TREE_TYPE (*vr0max), 1));
1985 tree max = int_const_binop (MINUS_EXPR,
1986 vr1min,
1987 build_int_cst (TREE_TYPE (vr1min), 1));
1988 if (!operand_less_p (max, min))
1990 *vr0type = VR_ANTI_RANGE;
1991 *vr0min = min;
1992 *vr0max = max;
1994 else
1995 *vr0max = vr1max;
1997 else
1998 *vr0max = vr1max;
2000 else
2002 /* If the result can be an anti-range, create one. */
2003 if (TREE_CODE (vr1max) == INTEGER_CST
2004 && TREE_CODE (*vr0min) == INTEGER_CST
2005 && vrp_val_is_min (vr1min)
2006 && vrp_val_is_max (*vr0max))
2008 tree min = int_const_binop (PLUS_EXPR,
2009 vr1max,
2010 build_int_cst (TREE_TYPE (vr1max), 1));
2011 tree max = int_const_binop (MINUS_EXPR,
2012 *vr0min,
2013 build_int_cst (TREE_TYPE (*vr0min), 1));
2014 if (!operand_less_p (max, min))
2016 *vr0type = VR_ANTI_RANGE;
2017 *vr0min = min;
2018 *vr0max = max;
2020 else
2021 *vr0min = vr1min;
2023 else
2024 *vr0min = vr1min;
2027 else
2028 gcc_unreachable ();
2030 else if ((maxeq || cmpmax == 1)
2031 && (mineq || cmpmin == -1))
2033 /* [ ( ) ] or [( ) ] or [ ( )] */
2034 if (*vr0type == VR_RANGE
2035 && vr1type == VR_RANGE)
2037 else if (*vr0type == VR_ANTI_RANGE
2038 && vr1type == VR_ANTI_RANGE)
2040 *vr0type = vr1type;
2041 *vr0min = vr1min;
2042 *vr0max = vr1max;
2044 else if (*vr0type == VR_ANTI_RANGE
2045 && vr1type == VR_RANGE)
2047 /* Arbitrarily choose the right or left gap. */
2048 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
2049 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
2050 build_int_cst (TREE_TYPE (vr1min), 1));
2051 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
2052 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
2053 build_int_cst (TREE_TYPE (vr1max), 1));
2054 else
2055 goto give_up;
2057 else if (*vr0type == VR_RANGE
2058 && vr1type == VR_ANTI_RANGE)
2059 /* The result covers everything. */
2060 goto give_up;
2061 else
2062 gcc_unreachable ();
2064 else if ((maxeq || cmpmax == -1)
2065 && (mineq || cmpmin == 1))
2067 /* ( [ ] ) or ([ ] ) or ( [ ]) */
2068 if (*vr0type == VR_RANGE
2069 && vr1type == VR_RANGE)
2071 *vr0type = vr1type;
2072 *vr0min = vr1min;
2073 *vr0max = vr1max;
2075 else if (*vr0type == VR_ANTI_RANGE
2076 && vr1type == VR_ANTI_RANGE)
2078 else if (*vr0type == VR_RANGE
2079 && vr1type == VR_ANTI_RANGE)
2081 *vr0type = VR_ANTI_RANGE;
2082 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
2084 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
2085 build_int_cst (TREE_TYPE (*vr0min), 1));
2086 *vr0min = vr1min;
2088 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
2090 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
2091 build_int_cst (TREE_TYPE (*vr0max), 1));
2092 *vr0max = vr1max;
2094 else
2095 goto give_up;
2097 else if (*vr0type == VR_ANTI_RANGE
2098 && vr1type == VR_RANGE)
2099 /* The result covers everything. */
2100 goto give_up;
2101 else
2102 gcc_unreachable ();
2104 else if (cmpmin == -1
2105 && cmpmax == -1
2106 && (operand_less_p (vr1min, *vr0max) == 1
2107 || operand_equal_p (vr1min, *vr0max, 0)))
2109 /* [ ( ] ) or [ ]( ) */
2110 if (*vr0type == VR_RANGE
2111 && vr1type == VR_RANGE)
2112 *vr0max = vr1max;
2113 else if (*vr0type == VR_ANTI_RANGE
2114 && vr1type == VR_ANTI_RANGE)
2115 *vr0min = vr1min;
2116 else if (*vr0type == VR_ANTI_RANGE
2117 && vr1type == VR_RANGE)
2119 if (TREE_CODE (vr1min) == INTEGER_CST)
2120 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
2121 build_int_cst (TREE_TYPE (vr1min), 1));
2122 else
2123 goto give_up;
2125 else if (*vr0type == VR_RANGE
2126 && vr1type == VR_ANTI_RANGE)
2128 if (TREE_CODE (*vr0max) == INTEGER_CST)
2130 *vr0type = vr1type;
2131 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
2132 build_int_cst (TREE_TYPE (*vr0max), 1));
2133 *vr0max = vr1max;
2135 else
2136 goto give_up;
2138 else
2139 gcc_unreachable ();
2141 else if (cmpmin == 1
2142 && cmpmax == 1
2143 && (operand_less_p (*vr0min, vr1max) == 1
2144 || operand_equal_p (*vr0min, vr1max, 0)))
2146 /* ( [ ) ] or ( )[ ] */
2147 if (*vr0type == VR_RANGE
2148 && vr1type == VR_RANGE)
2149 *vr0min = vr1min;
2150 else if (*vr0type == VR_ANTI_RANGE
2151 && vr1type == VR_ANTI_RANGE)
2152 *vr0max = vr1max;
2153 else if (*vr0type == VR_ANTI_RANGE
2154 && vr1type == VR_RANGE)
2156 if (TREE_CODE (vr1max) == INTEGER_CST)
2157 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
2158 build_int_cst (TREE_TYPE (vr1max), 1));
2159 else
2160 goto give_up;
2162 else if (*vr0type == VR_RANGE
2163 && vr1type == VR_ANTI_RANGE)
2165 if (TREE_CODE (*vr0min) == INTEGER_CST)
2167 *vr0type = vr1type;
2168 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
2169 build_int_cst (TREE_TYPE (*vr0min), 1));
2170 *vr0min = vr1min;
2172 else
2173 goto give_up;
2175 else
2176 gcc_unreachable ();
2178 else
2179 goto give_up;
2181 return;
2183 give_up:
2184 *vr0type = VR_VARYING;
2185 *vr0min = NULL_TREE;
2186 *vr0max = NULL_TREE;
2189 /* Helper for meet operation for value ranges. Given two ranges VR0
2190 and VR1, set VR0 to the union of both ranges. This may not be the
2191 smallest possible such range. */
2193 void
2194 irange::legacy_union (irange *vr0, const irange *vr1)
2196 gcc_checking_assert (vr0->legacy_mode_p ());
2197 gcc_checking_assert (vr1->legacy_mode_p ());
2199 /* VR0 has the resulting range if VR1 is undefined or VR0 is varying. */
2200 if (vr1->undefined_p ()
2201 || vr0->varying_p ())
2202 return;
2204 /* VR1 has the resulting range if VR0 is undefined or VR1 is varying. */
2205 if (vr0->undefined_p ())
2207 vr0->set (vr1->min (), vr1->max (), vr1->kind ());
2208 return;
2211 if (vr1->varying_p ())
2213 vr0->set_varying (vr1->type ());
2214 return;
2217 value_range_kind vr0kind = vr0->kind ();
2218 tree vr0min = vr0->min ();
2219 tree vr0max = vr0->max ();
2221 union_ranges (&vr0kind, &vr0min, &vr0max,
2222 vr1->kind (), vr1->min (), vr1->max ());
2224 if (vr0kind == VR_UNDEFINED)
2225 vr0->set_undefined ();
2226 else if (vr0kind == VR_VARYING)
2228 /* Failed to find an efficient meet. Before giving up and
2229 setting the result to VARYING, see if we can at least derive
2230 a non-zero range. */
2231 if (range_includes_zero_p (vr0) == 0
2232 && range_includes_zero_p (vr1) == 0)
2233 vr0->set_nonzero (vr0->type ());
2234 else
2235 vr0->set_varying (vr0->type ());
2237 else
2238 vr0->set (vr0min, vr0max, vr0kind);
2241 /* Meet operation for value ranges. Given two value ranges VR0 and
2242 VR1, store in VR0 a range that contains both VR0 and VR1. This
2243 may not be the smallest possible such range.
2244 Return TRUE if the original value changes. */
2246 bool
2247 irange::legacy_verbose_union_ (const irange *other)
2249 if (legacy_mode_p ())
2251 if (!other->legacy_mode_p ())
2253 int_range<1> tmp = *other;
2254 legacy_union (this, &tmp);
2255 return true;
2257 if (dump_file && (dump_flags & TDF_DETAILS))
2259 fprintf (dump_file, "Meeting\n ");
2260 dump_value_range (dump_file, this);
2261 fprintf (dump_file, "\nand\n ");
2262 dump_value_range (dump_file, other);
2263 fprintf (dump_file, "\n");
2266 legacy_union (this, other);
2268 if (dump_file && (dump_flags & TDF_DETAILS))
2270 fprintf (dump_file, "to\n ");
2271 dump_value_range (dump_file, this);
2272 fprintf (dump_file, "\n");
2274 return true;
2277 if (other->legacy_mode_p ())
2279 int_range<2> wider = *other;
2280 return irange_union (wider);
2282 else
2283 return irange_union (*other);
2286 bool
2287 irange::legacy_verbose_intersect (const irange *other)
2289 if (legacy_mode_p ())
2291 if (!other->legacy_mode_p ())
2293 int_range<1> tmp = *other;
2294 legacy_intersect (this, &tmp);
2295 return true;
2297 if (dump_file && (dump_flags & TDF_DETAILS))
2299 fprintf (dump_file, "Intersecting\n ");
2300 dump_value_range (dump_file, this);
2301 fprintf (dump_file, "\nand\n ");
2302 dump_value_range (dump_file, other);
2303 fprintf (dump_file, "\n");
2306 legacy_intersect (this, other);
2308 if (dump_file && (dump_flags & TDF_DETAILS))
2310 fprintf (dump_file, "to\n ");
2311 dump_value_range (dump_file, this);
2312 fprintf (dump_file, "\n");
2314 return true;
2317 if (other->legacy_mode_p ())
2319 int_range<2> wider;
2320 wider = *other;
2321 return irange_intersect (wider);
2323 else
2324 return irange_intersect (*other);
2327 // Perform an efficient union with R when both ranges have only a single pair.
2328 // Excluded are VARYING and UNDEFINED ranges.
2330 bool
2331 irange::irange_single_pair_union (const irange &r)
2333 gcc_checking_assert (!undefined_p () && !varying_p ());
2334 gcc_checking_assert (!r.undefined_p () && !varying_p ());
2336 signop sign = TYPE_SIGN (TREE_TYPE (m_base[0]));
2337 // Check if current lower bound is also the new lower bound.
2338 if (wi::le_p (wi::to_wide (m_base[0]), wi::to_wide (r.m_base[0]), sign))
2340 // If current upper bound is new upper bound, we're done.
2341 if (wi::le_p (wi::to_wide (r.m_base[1]), wi::to_wide (m_base[1]), sign))
2342 return union_nonzero_bits (r);
2343 // Otherwise R has the new upper bound.
2344 // Check for overlap/touching ranges, or single target range.
2345 if (m_max_ranges == 1
2346 || wi::to_widest (m_base[1]) + 1 >= wi::to_widest (r.m_base[0]))
2347 m_base[1] = r.m_base[1];
2348 else
2350 // This is a dual range result.
2351 m_base[2] = r.m_base[0];
2352 m_base[3] = r.m_base[1];
2353 m_num_ranges = 2;
2355 union_nonzero_bits (r);
2356 return true;
2359 // Set the new lower bound to R's lower bound.
2360 tree lb = m_base[0];
2361 m_base[0] = r.m_base[0];
2363 // If R fully contains THIS range, just set the upper bound.
2364 if (wi::ge_p (wi::to_wide (r.m_base[1]), wi::to_wide (m_base[1]), sign))
2365 m_base[1] = r.m_base[1];
2366 // Check for overlapping ranges, or target limited to a single range.
2367 else if (m_max_ranges == 1
2368 || wi::to_widest (r.m_base[1]) + 1 >= wi::to_widest (lb))
2370 else
2372 // Left with 2 pairs.
2373 m_num_ranges = 2;
2374 m_base[2] = lb;
2375 m_base[3] = m_base[1];
2376 m_base[1] = r.m_base[1];
2378 union_nonzero_bits (r);
2379 return true;
2382 // union_ for multi-ranges.
2384 bool
2385 irange::irange_union (const irange &r)
2387 gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ());
2389 if (r.undefined_p ())
2390 return false;
2392 if (undefined_p ())
2394 operator= (r);
2395 if (flag_checking)
2396 verify_range ();
2397 return true;
2400 if (varying_p ())
2401 return false;
2403 if (r.varying_p ())
2405 set_varying (type ());
2406 return true;
2409 // Special case one range union one range.
2410 if (m_num_ranges == 1 && r.m_num_ranges == 1)
2411 return irange_single_pair_union (r);
2413 // If this ranges fully contains R, then we need do nothing.
2414 if (irange_contains_p (r))
2415 return union_nonzero_bits (r);
2417 // Do not worry about merging and such by reserving twice as many
2418 // pairs as needed, and then simply sort the 2 ranges into this
2419 // intermediate form.
2421 // The intermediate result will have the property that the beginning
2422 // of each range is <= the beginning of the next range. There may
2423 // be overlapping ranges at this point. I.e. this would be valid
2424 // [-20, 10], [-10, 0], [0, 20], [40, 90] as it satisfies this
2425 // contraint : -20 < -10 < 0 < 40. When the range is rebuilt into r,
2426 // the merge is performed.
2428 // [Xi,Yi]..[Xn,Yn] U [Xj,Yj]..[Xm,Ym] --> [Xk,Yk]..[Xp,Yp]
2429 auto_vec<tree, 20> res (m_num_ranges * 2 + r.m_num_ranges * 2);
2430 unsigned i = 0, j = 0, k = 0;
2432 while (i < m_num_ranges * 2 && j < r.m_num_ranges * 2)
2434 // lower of Xi and Xj is the lowest point.
2435 if (wi::to_widest (m_base[i]) <= wi::to_widest (r.m_base[j]))
2437 res.quick_push (m_base[i]);
2438 res.quick_push (m_base[i + 1]);
2439 k += 2;
2440 i += 2;
2442 else
2444 res.quick_push (r.m_base[j]);
2445 res.quick_push (r.m_base[j + 1]);
2446 k += 2;
2447 j += 2;
2450 for ( ; i < m_num_ranges * 2; i += 2)
2452 res.quick_push (m_base[i]);
2453 res.quick_push (m_base[i + 1]);
2454 k += 2;
2456 for ( ; j < r.m_num_ranges * 2; j += 2)
2458 res.quick_push (r.m_base[j]);
2459 res.quick_push (r.m_base[j + 1]);
2460 k += 2;
2463 // Now normalize the vector removing any overlaps.
2464 i = 2;
2465 for (j = 2; j < k ; j += 2)
2467 // Current upper+1 is >= lower bound next pair, then we merge ranges.
2468 if (wi::to_widest (res[i - 1]) + 1 >= wi::to_widest (res[j]))
2470 // New upper bounds is greater of current or the next one.
2471 if (wi::to_widest (res[j + 1]) > wi::to_widest (res[i - 1]))
2472 res[i - 1] = res[j + 1];
2474 else
2476 // This is a new distinct range, but no point in copying it
2477 // if it is already in the right place.
2478 if (i != j)
2480 res[i++] = res[j];
2481 res[i++] = res[j + 1];
2483 else
2484 i += 2;
2488 // At this point, the vector should have i ranges, none overlapping.
2489 // Now it simply needs to be copied, and if there are too many
2490 // ranges, merge some. We wont do any analysis as to what the
2491 // "best" merges are, simply combine the final ranges into one.
2492 if (i > m_max_ranges * 2)
2494 res[m_max_ranges * 2 - 1] = res[i - 1];
2495 i = m_max_ranges * 2;
2498 for (j = 0; j < i ; j++)
2499 m_base[j] = res [j];
2500 m_num_ranges = i / 2;
2502 m_kind = VR_RANGE;
2503 union_nonzero_bits (r);
2504 return true;
2507 // Return TRUE if THIS fully contains R. No undefined or varying cases.
2509 bool
2510 irange::irange_contains_p (const irange &r) const
2512 gcc_checking_assert (!undefined_p () && !varying_p ());
2513 gcc_checking_assert (!r.undefined_p () && !varying_p ());
2515 // In order for THIS to fully contain R, all of the pairs within R must
2516 // be fully contained by the pairs in this object.
2517 signop sign = TYPE_SIGN (TREE_TYPE(m_base[0]));
2518 unsigned ri = 0;
2519 unsigned i = 0;
2520 tree rl = r.m_base[0];
2521 tree ru = r.m_base[1];
2522 tree l = m_base[0];
2523 tree u = m_base[1];
2524 while (1)
2526 // If r is contained within this range, move to the next R
2527 if (wi::ge_p (wi::to_wide (rl), wi::to_wide (l), sign)
2528 && wi::le_p (wi::to_wide (ru), wi::to_wide (u), sign))
2530 // This pair is OK, Either done, or bump to the next.
2531 if (++ri >= r.num_pairs ())
2532 return true;
2533 rl = r.m_base[ri * 2];
2534 ru = r.m_base[ri * 2 + 1];
2535 continue;
2537 // Otherwise, check if this's pair occurs before R's.
2538 if (wi::lt_p (wi::to_wide (u), wi::to_wide (rl), sign))
2540 // There's still at least one pair of R left.
2541 if (++i >= num_pairs ())
2542 return false;
2543 l = m_base[i * 2];
2544 u = m_base[i * 2 + 1];
2545 continue;
2547 return false;
2549 return false;
2553 // Intersect for multi-ranges. Return TRUE if anything changes.
2555 bool
2556 irange::irange_intersect (const irange &r)
2558 gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ());
2559 gcc_checking_assert (undefined_p () || r.undefined_p ()
2560 || range_compatible_p (type (), r.type ()));
2562 if (undefined_p ())
2563 return false;
2564 if (r.undefined_p ())
2566 set_undefined ();
2567 return true;
2569 if (r.varying_p ())
2570 return false;
2571 if (varying_p ())
2573 operator= (r);
2574 return true;
2577 if (r.num_pairs () == 1)
2579 bool res = intersect (r.lower_bound (), r.upper_bound ());
2580 if (undefined_p ())
2581 return true;
2583 res |= intersect_nonzero_bits (r);
2584 return res;
2587 // If R fully contains this, then intersection will change nothing.
2588 if (r.irange_contains_p (*this))
2589 return intersect_nonzero_bits (r);
2591 signop sign = TYPE_SIGN (TREE_TYPE(m_base[0]));
2592 unsigned bld_pair = 0;
2593 unsigned bld_lim = m_max_ranges;
2594 int_range_max r2 (*this);
2595 unsigned r2_lim = r2.num_pairs ();
2596 unsigned i2 = 0;
2597 for (unsigned i = 0; i < r.num_pairs (); )
2599 // If r1's upper is < r2's lower, we can skip r1's pair.
2600 tree ru = r.m_base[i * 2 + 1];
2601 tree r2l = r2.m_base[i2 * 2];
2602 if (wi::lt_p (wi::to_wide (ru), wi::to_wide (r2l), sign))
2604 i++;
2605 continue;
2607 // Likewise, skip r2's pair if its excluded.
2608 tree r2u = r2.m_base[i2 * 2 + 1];
2609 tree rl = r.m_base[i * 2];
2610 if (wi::lt_p (wi::to_wide (r2u), wi::to_wide (rl), sign))
2612 i2++;
2613 if (i2 < r2_lim)
2614 continue;
2615 // No more r2, break.
2616 break;
2619 // Must be some overlap. Find the highest of the lower bounds,
2620 // and set it, unless the build limits lower bounds is already
2621 // set.
2622 if (bld_pair < bld_lim)
2624 if (wi::ge_p (wi::to_wide (rl), wi::to_wide (r2l), sign))
2625 m_base[bld_pair * 2] = rl;
2626 else
2627 m_base[bld_pair * 2] = r2l;
2629 else
2630 // Decrease and set a new upper.
2631 bld_pair--;
2633 // ...and choose the lower of the upper bounds.
2634 if (wi::le_p (wi::to_wide (ru), wi::to_wide (r2u), sign))
2636 m_base[bld_pair * 2 + 1] = ru;
2637 bld_pair++;
2638 // Move past the r1 pair and keep trying.
2639 i++;
2640 continue;
2642 else
2644 m_base[bld_pair * 2 + 1] = r2u;
2645 bld_pair++;
2646 i2++;
2647 if (i2 < r2_lim)
2648 continue;
2649 // No more r2, break.
2650 break;
2652 // r2 has the higher lower bound.
2655 // At the exit of this loop, it is one of 2 things:
2656 // ran out of r1, or r2, but either means we are done.
2657 m_num_ranges = bld_pair;
2658 if (m_num_ranges == 0)
2660 set_undefined ();
2661 return true;
2664 m_kind = VR_RANGE;
2665 intersect_nonzero_bits (r);
2666 return true;
2670 // Multirange intersect for a specified wide_int [lb, ub] range.
2671 // Return TRUE if intersect changed anything.
2673 // NOTE: It is the caller's responsibility to intersect the nonzero masks.
2675 bool
2676 irange::intersect (const wide_int& lb, const wide_int& ub)
2678 // Undefined remains undefined.
2679 if (undefined_p ())
2680 return false;
2682 if (legacy_mode_p ())
2684 intersect (int_range<1> (type (), lb, ub));
2685 return true;
2688 tree range_type = type();
2689 signop sign = TYPE_SIGN (range_type);
2691 gcc_checking_assert (TYPE_PRECISION (range_type) == wi::get_precision (lb));
2692 gcc_checking_assert (TYPE_PRECISION (range_type) == wi::get_precision (ub));
2694 // If this range is fuly contained, then intersection will do nothing.
2695 if (wi::ge_p (lower_bound (), lb, sign)
2696 && wi::le_p (upper_bound (), ub, sign))
2697 return false;
2699 unsigned bld_index = 0;
2700 unsigned pair_lim = num_pairs ();
2701 for (unsigned i = 0; i < pair_lim; i++)
2703 tree pairl = m_base[i * 2];
2704 tree pairu = m_base[i * 2 + 1];
2705 // Once UB is less than a pairs lower bound, we're done.
2706 if (wi::lt_p (ub, wi::to_wide (pairl), sign))
2707 break;
2708 // if LB is greater than this pairs upper, this pair is excluded.
2709 if (wi::lt_p (wi::to_wide (pairu), lb, sign))
2710 continue;
2712 // Must be some overlap. Find the highest of the lower bounds,
2713 // and set it
2714 if (wi::gt_p (lb, wi::to_wide (pairl), sign))
2715 m_base[bld_index * 2] = wide_int_to_tree (range_type, lb);
2716 else
2717 m_base[bld_index * 2] = pairl;
2719 // ...and choose the lower of the upper bounds and if the base pair
2720 // has the lower upper bound, need to check next pair too.
2721 if (wi::lt_p (ub, wi::to_wide (pairu), sign))
2723 m_base[bld_index++ * 2 + 1] = wide_int_to_tree (range_type, ub);
2724 break;
2726 else
2727 m_base[bld_index++ * 2 + 1] = pairu;
2730 m_num_ranges = bld_index;
2731 if (m_num_ranges == 0)
2733 set_undefined ();
2734 return true;
2737 m_kind = VR_RANGE;
2738 // No need to call normalize_kind(), as the caller will do this
2739 // while intersecting the nonzero mask.
2740 if (flag_checking)
2741 verify_range ();
2742 return true;
2746 // Signed 1-bits are strange. You can't subtract 1, because you can't
2747 // represent the number 1. This works around that for the invert routine.
2749 static wide_int inline
2750 subtract_one (const wide_int &x, tree type, wi::overflow_type &overflow)
2752 if (TYPE_SIGN (type) == SIGNED)
2753 return wi::add (x, -1, SIGNED, &overflow);
2754 else
2755 return wi::sub (x, 1, UNSIGNED, &overflow);
2758 // The analogous function for adding 1.
2760 static wide_int inline
2761 add_one (const wide_int &x, tree type, wi::overflow_type &overflow)
2763 if (TYPE_SIGN (type) == SIGNED)
2764 return wi::sub (x, -1, SIGNED, &overflow);
2765 else
2766 return wi::add (x, 1, UNSIGNED, &overflow);
2769 // Return the inverse of a range.
2771 void
2772 irange::invert ()
2774 if (legacy_mode_p ())
2776 // We can't just invert VR_RANGE and VR_ANTI_RANGE because we may
2777 // create non-canonical ranges. Use the constructors instead.
2778 if (m_kind == VR_RANGE)
2779 *this = value_range (min (), max (), VR_ANTI_RANGE);
2780 else if (m_kind == VR_ANTI_RANGE)
2781 *this = value_range (min (), max ());
2782 else
2783 gcc_unreachable ();
2784 return;
2787 gcc_checking_assert (!undefined_p () && !varying_p ());
2789 // We always need one more set of bounds to represent an inverse, so
2790 // if we're at the limit, we can't properly represent things.
2792 // For instance, to represent the inverse of a 2 sub-range set
2793 // [5, 10][20, 30], we would need a 3 sub-range set
2794 // [-MIN, 4][11, 19][31, MAX].
2796 // In this case, return the most conservative thing.
2798 // However, if any of the extremes of the range are -MIN/+MAX, we
2799 // know we will not need an extra bound. For example:
2801 // INVERT([-MIN,20][30,40]) => [21,29][41,+MAX]
2802 // INVERT([-MIN,20][30,MAX]) => [21,29]
2803 tree ttype = type ();
2804 unsigned prec = TYPE_PRECISION (ttype);
2805 signop sign = TYPE_SIGN (ttype);
2806 wide_int type_min = wi::min_value (prec, sign);
2807 wide_int type_max = wi::max_value (prec, sign);
2808 m_nonzero_mask = NULL;
2809 if (m_num_ranges == m_max_ranges
2810 && lower_bound () != type_min
2811 && upper_bound () != type_max)
2813 m_base[1] = wide_int_to_tree (ttype, type_max);
2814 m_num_ranges = 1;
2815 return;
2817 // The algorithm is as follows. To calculate INVERT ([a,b][c,d]), we
2818 // generate [-MIN, a-1][b+1, c-1][d+1, MAX].
2820 // If there is an over/underflow in the calculation for any
2821 // sub-range, we eliminate that subrange. This allows us to easily
2822 // calculate INVERT([-MIN, 5]) with: [-MIN, -MIN-1][6, MAX]. And since
2823 // we eliminate the underflow, only [6, MAX] remains.
2824 unsigned i = 0;
2825 wi::overflow_type ovf;
2826 // Construct leftmost range.
2827 int_range_max orig_range (*this);
2828 unsigned nitems = 0;
2829 wide_int tmp;
2830 // If this is going to underflow on the MINUS 1, don't even bother
2831 // checking. This also handles subtracting one from an unsigned 0,
2832 // which doesn't set the underflow bit.
2833 if (type_min != orig_range.lower_bound ())
2835 m_base[nitems++] = wide_int_to_tree (ttype, type_min);
2836 tmp = subtract_one (orig_range.lower_bound (), ttype, ovf);
2837 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
2838 if (ovf)
2839 nitems = 0;
2841 i++;
2842 // Construct middle ranges if applicable.
2843 if (orig_range.num_pairs () > 1)
2845 unsigned j = i;
2846 for (; j < (orig_range.num_pairs () * 2) - 1; j += 2)
2848 // The middle ranges cannot have MAX/MIN, so there's no need
2849 // to check for unsigned overflow on the +1 and -1 here.
2850 tmp = wi::add (wi::to_wide (orig_range.m_base[j]), 1, sign, &ovf);
2851 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
2852 tmp = subtract_one (wi::to_wide (orig_range.m_base[j + 1]),
2853 ttype, ovf);
2854 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
2855 if (ovf)
2856 nitems -= 2;
2858 i = j;
2860 // Construct rightmost range.
2862 // However, if this will overflow on the PLUS 1, don't even bother.
2863 // This also handles adding one to an unsigned MAX, which doesn't
2864 // set the overflow bit.
2865 if (type_max != wi::to_wide (orig_range.m_base[i]))
2867 tmp = add_one (wi::to_wide (orig_range.m_base[i]), ttype, ovf);
2868 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
2869 m_base[nitems++] = wide_int_to_tree (ttype, type_max);
2870 if (ovf)
2871 nitems -= 2;
2873 m_num_ranges = nitems / 2;
2875 // We disallow undefined or varying coming in, so the result can
2876 // only be a VR_RANGE.
2877 gcc_checking_assert (m_kind == VR_RANGE);
2879 if (flag_checking)
2880 verify_range ();
2883 // Return the nonzero bits inherent in the range.
2885 wide_int
2886 irange::get_nonzero_bits_from_range () const
2888 // For legacy symbolics.
2889 if (!constant_p ())
2890 return wi::shwi (-1, TYPE_PRECISION (type ()));
2892 wide_int min = lower_bound ();
2893 wide_int max = upper_bound ();
2894 wide_int xorv = min ^ max;
2895 if (xorv != 0)
2897 unsigned prec = TYPE_PRECISION (type ());
2898 xorv = wi::mask (prec - wi::clz (xorv), false, prec);
2900 return min | xorv;
2903 // If the the nonzero mask can be trivially converted to a range, do
2904 // so and return TRUE.
2906 bool
2907 irange::set_range_from_nonzero_bits ()
2909 gcc_checking_assert (!undefined_p ());
2910 if (!m_nonzero_mask)
2911 return false;
2912 unsigned popcount = wi::popcount (wi::to_wide (m_nonzero_mask));
2914 // If we have only one bit set in the mask, we can figure out the
2915 // range immediately.
2916 if (popcount == 1)
2918 // Make sure we don't pessimize the range.
2919 if (!contains_p (m_nonzero_mask))
2920 return false;
2922 bool has_zero = contains_p (build_zero_cst (type ()));
2923 tree nz = m_nonzero_mask;
2924 set (nz, nz);
2925 m_nonzero_mask = nz;
2926 if (has_zero)
2928 int_range<2> zero;
2929 zero.set_zero (type ());
2930 union_ (zero);
2932 return true;
2934 else if (popcount == 0)
2936 set_zero (type ());
2937 return true;
2939 return false;
2942 void
2943 irange::set_nonzero_bits (const wide_int_ref &bits)
2945 gcc_checking_assert (!undefined_p ());
2946 unsigned prec = TYPE_PRECISION (type ());
2948 if (bits == -1)
2950 m_nonzero_mask = NULL;
2951 normalize_kind ();
2952 if (flag_checking)
2953 verify_range ();
2954 return;
2957 // Drop VARYINGs with a nonzero mask to a plain range.
2958 if (m_kind == VR_VARYING && bits != -1)
2959 m_kind = VR_RANGE;
2961 wide_int nz = wide_int::from (bits, prec, TYPE_SIGN (type ()));
2962 m_nonzero_mask = wide_int_to_tree (type (), nz);
2963 if (set_range_from_nonzero_bits ())
2964 return;
2966 normalize_kind ();
2967 if (flag_checking)
2968 verify_range ();
2971 // Return the nonzero bitmask. This will return the nonzero bits plus
2972 // the nonzero bits inherent in the range.
2974 wide_int
2975 irange::get_nonzero_bits () const
2977 gcc_checking_assert (!undefined_p ());
2978 // The nonzero mask inherent in the range is calculated on-demand.
2979 // For example, [0,255] does not have a 0xff nonzero mask by default
2980 // (unless manually set). This saves us considerable time, because
2981 // setting it at creation incurs a large penalty for irange::set.
2982 // At the time of writing there was a 5% slowdown in VRP if we kept
2983 // the mask precisely up to date at all times. Instead, we default
2984 // to -1 and set it when explicitly requested. However, this
2985 // function will always return the correct mask.
2986 if (m_nonzero_mask)
2987 return wi::to_wide (m_nonzero_mask) & get_nonzero_bits_from_range ();
2988 else
2989 return get_nonzero_bits_from_range ();
2992 // Convert tree mask to wide_int. Returns -1 for NULL masks.
2994 inline wide_int
2995 mask_to_wi (tree mask, tree type)
2997 if (mask)
2998 return wi::to_wide (mask);
2999 else
3000 return wi::shwi (-1, TYPE_PRECISION (type));
3003 // Intersect the nonzero bits in R into THIS and normalize the range.
3004 // Return TRUE if the intersection changed anything.
3006 bool
3007 irange::intersect_nonzero_bits (const irange &r)
3009 gcc_checking_assert (!undefined_p () && !r.undefined_p ());
3011 if (!m_nonzero_mask && !r.m_nonzero_mask)
3013 normalize_kind ();
3014 if (flag_checking)
3015 verify_range ();
3016 return false;
3019 bool changed = false;
3020 tree t = type ();
3021 if (mask_to_wi (m_nonzero_mask, t) != mask_to_wi (r.m_nonzero_mask, t))
3023 wide_int nz = get_nonzero_bits () & r.get_nonzero_bits ();
3024 // If the nonzero bits did not change, return false.
3025 if (nz == get_nonzero_bits ())
3026 return false;
3028 m_nonzero_mask = wide_int_to_tree (t, nz);
3029 if (set_range_from_nonzero_bits ())
3030 return true;
3031 changed = true;
3033 normalize_kind ();
3034 if (flag_checking)
3035 verify_range ();
3036 return changed;
3039 // Union the nonzero bits in R into THIS and normalize the range.
3040 // Return TRUE if the union changed anything.
3042 bool
3043 irange::union_nonzero_bits (const irange &r)
3045 gcc_checking_assert (!undefined_p () && !r.undefined_p ());
3047 if (!m_nonzero_mask && !r.m_nonzero_mask)
3049 normalize_kind ();
3050 if (flag_checking)
3051 verify_range ();
3052 return false;
3055 bool changed = false;
3056 tree t = type ();
3057 if (mask_to_wi (m_nonzero_mask, t) != mask_to_wi (r.m_nonzero_mask, t))
3059 wide_int nz = get_nonzero_bits () | r.get_nonzero_bits ();
3060 m_nonzero_mask = wide_int_to_tree (t, nz);
3061 // No need to call set_range_from_nonzero_bits, because we'll
3062 // never narrow the range. Besides, it would cause endless
3063 // recursion because of the union_ in
3064 // set_range_from_nonzero_bits.
3065 changed = true;
3067 normalize_kind ();
3068 if (flag_checking)
3069 verify_range ();
3070 return changed;
3073 void
3074 dump_value_range (FILE *file, const vrange *vr)
3076 vr->dump (file);
3079 DEBUG_FUNCTION void
3080 debug (const vrange *vr)
3082 dump_value_range (stderr, vr);
3083 fprintf (stderr, "\n");
3086 DEBUG_FUNCTION void
3087 debug (const vrange &vr)
3089 debug (&vr);
3092 DEBUG_FUNCTION void
3093 debug (const value_range *vr)
3095 dump_value_range (stderr, vr);
3096 fprintf (stderr, "\n");
3099 DEBUG_FUNCTION void
3100 debug (const value_range &vr)
3102 dump_value_range (stderr, &vr);
3103 fprintf (stderr, "\n");
3106 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
3107 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
3108 false otherwise. If *AR can be represented with a single range
3109 *VR1 will be VR_UNDEFINED. */
3111 bool
3112 ranges_from_anti_range (const value_range *ar,
3113 value_range *vr0, value_range *vr1)
3115 tree type = ar->type ();
3117 vr0->set_undefined ();
3118 vr1->set_undefined ();
3120 /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U
3121 [A+1, +INF]. Not sure if this helps in practice, though. */
3123 if (ar->kind () != VR_ANTI_RANGE
3124 || TREE_CODE (ar->min ()) != INTEGER_CST
3125 || TREE_CODE (ar->max ()) != INTEGER_CST
3126 || !vrp_val_min (type)
3127 || !vrp_val_max (type))
3128 return false;
3130 if (tree_int_cst_lt (vrp_val_min (type), ar->min ()))
3131 vr0->set (vrp_val_min (type),
3132 wide_int_to_tree (type, wi::to_wide (ar->min ()) - 1));
3133 if (tree_int_cst_lt (ar->max (), vrp_val_max (type)))
3134 vr1->set (wide_int_to_tree (type, wi::to_wide (ar->max ()) + 1),
3135 vrp_val_max (type));
3136 if (vr0->undefined_p ())
3138 *vr0 = *vr1;
3139 vr1->set_undefined ();
3142 return !vr0->undefined_p ();
3145 bool
3146 range_has_numeric_bounds_p (const irange *vr)
3148 return (!vr->undefined_p ()
3149 && TREE_CODE (vr->min ()) == INTEGER_CST
3150 && TREE_CODE (vr->max ()) == INTEGER_CST);
3153 /* Return whether VAL is equal to the maximum value of its type.
3154 We can't do a simple equality comparison with TYPE_MAX_VALUE because
3155 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
3156 is not == to the integer constant with the same value in the type. */
3158 bool
3159 vrp_val_is_max (const_tree val)
3161 tree type_max = vrp_val_max (TREE_TYPE (val));
3162 return (val == type_max
3163 || (type_max != NULL_TREE
3164 && operand_equal_p (val, type_max, 0)));
3167 /* Return whether VAL is equal to the minimum value of its type. */
3169 bool
3170 vrp_val_is_min (const_tree val)
3172 tree type_min = vrp_val_min (TREE_TYPE (val));
3173 return (val == type_min
3174 || (type_min != NULL_TREE
3175 && operand_equal_p (val, type_min, 0)));
3178 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
3180 bool
3181 vrp_operand_equal_p (const_tree val1, const_tree val2)
3183 if (val1 == val2)
3184 return true;
3185 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
3186 return false;
3187 return true;
3190 // ?? These stubs are for ipa-prop.cc which use a value_range in a
3191 // hash_traits. hash-traits.h defines an extern of gt_ggc_mx (T &)
3192 // instead of picking up the gt_ggc_mx (T *) version.
3193 void
3194 gt_pch_nx (int_range<1> *&x)
3196 return gt_pch_nx ((irange *) x);
3199 void
3200 gt_ggc_mx (int_range<1> *&x)
3202 return gt_ggc_mx ((irange *) x);
3205 #define DEFINE_INT_RANGE_INSTANCE(N) \
3206 template int_range<N>::int_range(tree, tree, value_range_kind); \
3207 template int_range<N>::int_range(tree_node *, \
3208 const wide_int &, \
3209 const wide_int &, \
3210 value_range_kind); \
3211 template int_range<N>::int_range(tree); \
3212 template int_range<N>::int_range(const irange &); \
3213 template int_range<N>::int_range(const int_range &); \
3214 template int_range<N>& int_range<N>::operator= (const int_range &);
3216 DEFINE_INT_RANGE_INSTANCE(1)
3217 DEFINE_INT_RANGE_INSTANCE(2)
3218 DEFINE_INT_RANGE_INSTANCE(3)
3219 DEFINE_INT_RANGE_INSTANCE(255)
3221 #if CHECKING_P
3222 #include "selftest.h"
3224 namespace selftest
3226 #define INT(N) build_int_cst (integer_type_node, (N))
3227 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
3228 #define UINT128(N) build_int_cstu (u128_type, (N))
3229 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
3230 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
3232 static int_range<3>
3233 build_range3 (int a, int b, int c, int d, int e, int f)
3235 int_range<3> i1 (INT (a), INT (b));
3236 int_range<3> i2 (INT (c), INT (d));
3237 int_range<3> i3 (INT (e), INT (f));
3238 i1.union_ (i2);
3239 i1.union_ (i3);
3240 return i1;
3243 static void
3244 range_tests_irange3 ()
3246 typedef int_range<3> int_range3;
3247 int_range3 r0, r1, r2;
3248 int_range3 i1, i2, i3;
3250 // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20].
3251 r0 = int_range3 (INT (10), INT (20));
3252 r1 = int_range3 (INT (5), INT (8));
3253 r0.union_ (r1);
3254 r1 = int_range3 (INT (1), INT (3));
3255 r0.union_ (r1);
3256 ASSERT_TRUE (r0 == build_range3 (1, 3, 5, 8, 10, 20));
3258 // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20].
3259 r1 = int_range3 (INT (-5), INT (0));
3260 r0.union_ (r1);
3261 ASSERT_TRUE (r0 == build_range3 (-5, 3, 5, 8, 10, 20));
3263 // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60].
3264 r1 = int_range3 (INT (50), INT (60));
3265 r0 = int_range3 (INT (10), INT (20));
3266 r0.union_ (int_range3 (INT (30), INT (40)));
3267 r0.union_ (r1);
3268 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
3269 // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80].
3270 r1 = int_range3 (INT (70), INT (80));
3271 r0.union_ (r1);
3273 r2 = build_range3 (10, 20, 30, 40, 50, 60);
3274 r2.union_ (int_range3 (INT (70), INT (80)));
3275 ASSERT_TRUE (r0 == r2);
3277 // [10,20][30,40][50,60] U [6,35] => [6,40][50,60].
3278 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3279 r1 = int_range3 (INT (6), INT (35));
3280 r0.union_ (r1);
3281 r1 = int_range3 (INT (6), INT (40));
3282 r1.union_ (int_range3 (INT (50), INT (60)));
3283 ASSERT_TRUE (r0 == r1);
3285 // [10,20][30,40][50,60] U [6,60] => [6,60].
3286 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3287 r1 = int_range3 (INT (6), INT (60));
3288 r0.union_ (r1);
3289 ASSERT_TRUE (r0 == int_range3 (INT (6), INT (60)));
3291 // [10,20][30,40][50,60] U [6,70] => [6,70].
3292 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3293 r1 = int_range3 (INT (6), INT (70));
3294 r0.union_ (r1);
3295 ASSERT_TRUE (r0 == int_range3 (INT (6), INT (70)));
3297 // [10,20][30,40][50,60] U [35,70] => [10,20][30,70].
3298 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3299 r1 = int_range3 (INT (35), INT (70));
3300 r0.union_ (r1);
3301 r1 = int_range3 (INT (10), INT (20));
3302 r1.union_ (int_range3 (INT (30), INT (70)));
3303 ASSERT_TRUE (r0 == r1);
3305 // [10,20][30,40][50,60] U [15,35] => [10,40][50,60].
3306 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3307 r1 = int_range3 (INT (15), INT (35));
3308 r0.union_ (r1);
3309 r1 = int_range3 (INT (10), INT (40));
3310 r1.union_ (int_range3 (INT (50), INT (60)));
3311 ASSERT_TRUE (r0 == r1);
3313 // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60].
3314 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3315 r1 = int_range3 (INT (35), INT (35));
3316 r0.union_ (r1);
3317 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
3320 static void
3321 range_tests_int_range_max ()
3323 int_range_max big;
3324 unsigned int nrange;
3326 // Build a huge multi-range range.
3327 for (nrange = 0; nrange < 50; ++nrange)
3329 int_range<1> tmp (INT (nrange*10), INT (nrange*10 + 5));
3330 big.union_ (tmp);
3332 ASSERT_TRUE (big.num_pairs () == nrange);
3334 // Verify that we can copy it without loosing precision.
3335 int_range_max copy (big);
3336 ASSERT_TRUE (copy.num_pairs () == nrange);
3338 // Inverting it should produce one more sub-range.
3339 big.invert ();
3340 ASSERT_TRUE (big.num_pairs () == nrange + 1);
3342 int_range<1> tmp (INT (5), INT (37));
3343 big.intersect (tmp);
3344 ASSERT_TRUE (big.num_pairs () == 4);
3346 // Test that [10,10][20,20] does NOT contain 15.
3348 int_range_max i1 (build_int_cst (integer_type_node, 10),
3349 build_int_cst (integer_type_node, 10));
3350 int_range_max i2 (build_int_cst (integer_type_node, 20),
3351 build_int_cst (integer_type_node, 20));
3352 i1.union_ (i2);
3353 ASSERT_FALSE (i1.contains_p (build_int_cst (integer_type_node, 15)));
3357 static void
3358 range_tests_legacy ()
3360 // Test truncating copy to int_range<1>.
3361 int_range<3> big = build_range3 (10, 20, 30, 40, 50, 60);
3362 int_range<1> small = big;
3363 ASSERT_TRUE (small == int_range<1> (INT (10), INT (60)));
3365 // Test truncating copy to int_range<2>.
3366 int_range<2> medium = big;
3367 ASSERT_TRUE (!medium.undefined_p ());
3369 // Test that a truncating copy of [MIN,20][22,40][80,MAX]
3370 // ends up as a conservative anti-range of ~[21,21].
3371 big = int_range<3> (vrp_val_min (integer_type_node), INT (20));
3372 big.union_ (int_range<1> (INT (22), INT (40)));
3373 big.union_ (int_range<1> (INT (80), vrp_val_max (integer_type_node)));
3374 small = big;
3375 ASSERT_TRUE (small == int_range<1> (INT (21), INT (21), VR_ANTI_RANGE));
3377 // Copying a legacy symbolic to an int_range should normalize the
3378 // symbolic at copy time.
3380 tree ssa = make_ssa_name (integer_type_node);
3381 value_range legacy_range (ssa, INT (25));
3382 int_range<2> copy = legacy_range;
3383 ASSERT_TRUE (copy == int_range<2> (vrp_val_min (integer_type_node),
3384 INT (25)));
3386 // Test that copying ~[abc_23, abc_23] to a multi-range yields varying.
3387 legacy_range = value_range (ssa, ssa, VR_ANTI_RANGE);
3388 copy = legacy_range;
3389 ASSERT_TRUE (copy.varying_p ());
3392 // VARYING of different sizes should not be equal.
3393 tree big_type = build_nonstandard_integer_type (32, 1);
3394 tree small_type = build_nonstandard_integer_type (16, 1);
3395 int_range_max r0 (big_type);
3396 int_range_max r1 (small_type);
3397 ASSERT_TRUE (r0 != r1);
3398 value_range vr0 (big_type);
3399 int_range_max vr1 (small_type);
3400 ASSERT_TRUE (vr0 != vr1);
3403 // Simulate -fstrict-enums where the domain of a type is less than the
3404 // underlying type.
3406 static void
3407 range_tests_strict_enum ()
3409 // The enum can only hold [0, 3].
3410 tree rtype = copy_node (unsigned_type_node);
3411 TYPE_MIN_VALUE (rtype) = build_int_cstu (rtype, 0);
3412 TYPE_MAX_VALUE (rtype) = build_int_cstu (rtype, 3);
3414 // Test that even though vr1 covers the strict enum domain ([0, 3]),
3415 // it does not cover the domain of the underlying type.
3416 int_range<1> vr1 (build_int_cstu (rtype, 0), build_int_cstu (rtype, 1));
3417 int_range<1> vr2 (build_int_cstu (rtype, 2), build_int_cstu (rtype, 3));
3418 vr1.union_ (vr2);
3419 ASSERT_TRUE (vr1 == int_range<1> (build_int_cstu (rtype, 0),
3420 build_int_cstu (rtype, 3)));
3421 ASSERT_FALSE (vr1.varying_p ());
3423 // Test that copying to a multi-range does not change things.
3424 int_range<2> ir1 (vr1);
3425 ASSERT_TRUE (ir1 == vr1);
3426 ASSERT_FALSE (ir1.varying_p ());
3428 // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3].
3429 vr1 = int_range<1> (TYPE_MIN_VALUE (rtype), TYPE_MAX_VALUE (rtype));
3430 ir1 = vr1;
3431 ASSERT_TRUE (ir1 == vr1);
3432 ASSERT_FALSE (ir1.varying_p ());
3435 static void
3436 range_tests_misc ()
3438 tree u128_type = build_nonstandard_integer_type (128, /*unsigned=*/1);
3439 int_range<1> i1, i2, i3;
3440 int_range<1> r0, r1, rold;
3442 // Test 1-bit signed integer union.
3443 // [-1,-1] U [0,0] = VARYING.
3444 tree one_bit_type = build_nonstandard_integer_type (1, 0);
3445 tree one_bit_min = vrp_val_min (one_bit_type);
3446 tree one_bit_max = vrp_val_max (one_bit_type);
3448 int_range<2> min (one_bit_min, one_bit_min);
3449 int_range<2> max (one_bit_max, one_bit_max);
3450 max.union_ (min);
3451 ASSERT_TRUE (max.varying_p ());
3453 // Test that we can set a range of true+false for a 1-bit signed int.
3454 r0 = range_true_and_false (one_bit_type);
3456 // Test inversion of 1-bit signed integers.
3458 int_range<2> min (one_bit_min, one_bit_min);
3459 int_range<2> max (one_bit_max, one_bit_max);
3460 int_range<2> t;
3461 t = min;
3462 t.invert ();
3463 ASSERT_TRUE (t == max);
3464 t = max;
3465 t.invert ();
3466 ASSERT_TRUE (t == min);
3469 // Test that NOT(255) is [0..254] in 8-bit land.
3470 int_range<1> not_255 (UCHAR (255), UCHAR (255), VR_ANTI_RANGE);
3471 ASSERT_TRUE (not_255 == int_range<1> (UCHAR (0), UCHAR (254)));
3473 // Test that NOT(0) is [1..255] in 8-bit land.
3474 int_range<1> not_zero = range_nonzero (unsigned_char_type_node);
3475 ASSERT_TRUE (not_zero == int_range<1> (UCHAR (1), UCHAR (255)));
3477 // Check that [0,127][0x..ffffff80,0x..ffffff]
3478 // => ~[128, 0x..ffffff7f].
3479 r0 = int_range<1> (UINT128 (0), UINT128 (127));
3480 tree high = build_minus_one_cst (u128_type);
3481 // low = -1 - 127 => 0x..ffffff80.
3482 tree low = fold_build2 (MINUS_EXPR, u128_type, high, UINT128(127));
3483 r1 = int_range<1> (low, high); // [0x..ffffff80, 0x..ffffffff]
3484 // r0 = [0,127][0x..ffffff80,0x..fffffff].
3485 r0.union_ (r1);
3486 // r1 = [128, 0x..ffffff7f].
3487 r1 = int_range<1> (UINT128(128),
3488 fold_build2 (MINUS_EXPR, u128_type,
3489 build_minus_one_cst (u128_type),
3490 UINT128(128)));
3491 r0.invert ();
3492 ASSERT_TRUE (r0 == r1);
3494 r0.set_varying (integer_type_node);
3495 tree minint = wide_int_to_tree (integer_type_node, r0.lower_bound ());
3496 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
3498 r0.set_varying (short_integer_type_node);
3500 r0.set_varying (unsigned_type_node);
3501 tree maxuint = wide_int_to_tree (unsigned_type_node, r0.upper_bound ());
3503 // Check that ~[0,5] => [6,MAX] for unsigned int.
3504 r0 = int_range<1> (UINT (0), UINT (5));
3505 r0.invert ();
3506 ASSERT_TRUE (r0 == int_range<1> (UINT(6), maxuint));
3508 // Check that ~[10,MAX] => [0,9] for unsigned int.
3509 r0 = int_range<1> (UINT(10), maxuint);
3510 r0.invert ();
3511 ASSERT_TRUE (r0 == int_range<1> (UINT (0), UINT (9)));
3513 // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers.
3514 r0 = int_range<1> (UINT128 (0), UINT128 (5), VR_ANTI_RANGE);
3515 r1 = int_range<1> (UINT128(6), build_minus_one_cst (u128_type));
3516 ASSERT_TRUE (r0 == r1);
3518 // Check that [~5] is really [-MIN,4][6,MAX].
3519 r0 = int_range<1> (INT (5), INT (5), VR_ANTI_RANGE);
3520 r1 = int_range<1> (minint, INT (4));
3521 r1.union_ (int_range<1> (INT (6), maxint));
3522 ASSERT_FALSE (r1.undefined_p ());
3523 ASSERT_TRUE (r0 == r1);
3525 r1 = int_range<1> (INT (5), INT (5));
3526 int_range<1> r2 (r1);
3527 ASSERT_TRUE (r1 == r2);
3529 r1 = int_range<1> (INT (5), INT (10));
3531 r1 = int_range<1> (integer_type_node,
3532 wi::to_wide (INT (5)), wi::to_wide (INT (10)));
3533 ASSERT_TRUE (r1.contains_p (INT (7)));
3535 r1 = int_range<1> (SCHAR (0), SCHAR (20));
3536 ASSERT_TRUE (r1.contains_p (SCHAR(15)));
3537 ASSERT_FALSE (r1.contains_p (SCHAR(300)));
3539 // NOT([10,20]) ==> [-MIN,9][21,MAX].
3540 r0 = r1 = int_range<1> (INT (10), INT (20));
3541 r2 = int_range<1> (minint, INT(9));
3542 r2.union_ (int_range<1> (INT(21), maxint));
3543 ASSERT_FALSE (r2.undefined_p ());
3544 r1.invert ();
3545 ASSERT_TRUE (r1 == r2);
3546 // Test that NOT(NOT(x)) == x.
3547 r2.invert ();
3548 ASSERT_TRUE (r0 == r2);
3550 // Test that booleans and their inverse work as expected.
3551 r0 = range_zero (boolean_type_node);
3552 ASSERT_TRUE (r0 == int_range<1> (build_zero_cst (boolean_type_node),
3553 build_zero_cst (boolean_type_node)));
3554 r0.invert ();
3555 ASSERT_TRUE (r0 == int_range<1> (build_one_cst (boolean_type_node),
3556 build_one_cst (boolean_type_node)));
3558 // Make sure NULL and non-NULL of pointer types work, and that
3559 // inverses of them are consistent.
3560 tree voidp = build_pointer_type (void_type_node);
3561 r0 = range_zero (voidp);
3562 r1 = r0;
3563 r0.invert ();
3564 r0.invert ();
3565 ASSERT_TRUE (r0 == r1);
3567 // [10,20] U [15, 30] => [10, 30].
3568 r0 = int_range<1> (INT (10), INT (20));
3569 r1 = int_range<1> (INT (15), INT (30));
3570 r0.union_ (r1);
3571 ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (30)));
3573 // [15,40] U [] => [15,40].
3574 r0 = int_range<1> (INT (15), INT (40));
3575 r1.set_undefined ();
3576 r0.union_ (r1);
3577 ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (40)));
3579 // [10,20] U [10,10] => [10,20].
3580 r0 = int_range<1> (INT (10), INT (20));
3581 r1 = int_range<1> (INT (10), INT (10));
3582 r0.union_ (r1);
3583 ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (20)));
3585 // [10,20] U [9,9] => [9,20].
3586 r0 = int_range<1> (INT (10), INT (20));
3587 r1 = int_range<1> (INT (9), INT (9));
3588 r0.union_ (r1);
3589 ASSERT_TRUE (r0 == int_range<1> (INT (9), INT (20)));
3591 // [10,20] ^ [15,30] => [15,20].
3592 r0 = int_range<1> (INT (10), INT (20));
3593 r1 = int_range<1> (INT (15), INT (30));
3594 r0.intersect (r1);
3595 ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (20)));
3597 // Test the internal sanity of wide_int's wrt HWIs.
3598 ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node),
3599 TYPE_SIGN (boolean_type_node))
3600 == wi::uhwi (1, TYPE_PRECISION (boolean_type_node)));
3602 // Test zero_p().
3603 r0 = int_range<1> (INT (0), INT (0));
3604 ASSERT_TRUE (r0.zero_p ());
3606 // Test nonzero_p().
3607 r0 = int_range<1> (INT (0), INT (0));
3608 r0.invert ();
3609 ASSERT_TRUE (r0.nonzero_p ());
3611 // test legacy interaction
3612 // r0 = ~[1,1]
3613 r0 = int_range<1> (UINT (1), UINT (1), VR_ANTI_RANGE);
3614 // r1 = ~[3,3]
3615 r1 = int_range<1> (UINT (3), UINT (3), VR_ANTI_RANGE);
3617 // vv = [0,0][2,2][4, MAX]
3618 int_range<3> vv = r0;
3619 vv.intersect (r1);
3621 ASSERT_TRUE (vv.contains_p (UINT (2)));
3622 ASSERT_TRUE (vv.num_pairs () == 3);
3624 // create r0 as legacy [1,1]
3625 r0 = int_range<1> (UINT (1), UINT (1));
3626 // And union it with [0,0][2,2][4,MAX] multi range
3627 r0.union_ (vv);
3628 // The result should be [0,2][4,MAX], or ~[3,3] but it must contain 2
3629 ASSERT_TRUE (r0.contains_p (UINT (2)));
3632 static void
3633 range_tests_nonzero_bits ()
3635 int_range<2> r0, r1;
3637 // Adding nonzero bits to a varying drops the varying.
3638 r0.set_varying (integer_type_node);
3639 r0.set_nonzero_bits (255);
3640 ASSERT_TRUE (!r0.varying_p ());
3641 // Dropping the nonzero bits brings us back to varying.
3642 r0.set_nonzero_bits (-1);
3643 ASSERT_TRUE (r0.varying_p ());
3645 // Test contains_p with nonzero bits.
3646 r0.set_zero (integer_type_node);
3647 ASSERT_TRUE (r0.contains_p (INT (0)));
3648 ASSERT_FALSE (r0.contains_p (INT (1)));
3649 r0.set_nonzero_bits (0xfe);
3650 ASSERT_FALSE (r0.contains_p (INT (0x100)));
3651 ASSERT_FALSE (r0.contains_p (INT (0x3)));
3653 // Union of nonzero bits.
3654 r0.set_varying (integer_type_node);
3655 r0.set_nonzero_bits (0xf0);
3656 r1.set_varying (integer_type_node);
3657 r1.set_nonzero_bits (0xf);
3658 r0.union_ (r1);
3659 ASSERT_TRUE (r0.get_nonzero_bits () == 0xff);
3661 // Intersect of nonzero bits.
3662 r0.set (INT (0), INT (255));
3663 r0.set_nonzero_bits (0xfe);
3664 r1.set_varying (integer_type_node);
3665 r1.set_nonzero_bits (0xf0);
3666 r0.intersect (r1);
3667 ASSERT_TRUE (r0.get_nonzero_bits () == 0xf0);
3669 // Intersect where the mask of nonzero bits is implicit from the range.
3670 r0.set_varying (integer_type_node);
3671 r1.set (INT (0), INT (255));
3672 r0.intersect (r1);
3673 ASSERT_TRUE (r0.get_nonzero_bits () == 0xff);
3675 // The union of a mask of 0xff..ffff00 with a mask of 0xff spans the
3676 // entire domain, and makes the range a varying.
3677 r0.set_varying (integer_type_node);
3678 wide_int x = wi::shwi (0xff, TYPE_PRECISION (integer_type_node));
3679 x = wi::bit_not (x);
3680 r0.set_nonzero_bits (x); // 0xff..ff00
3681 r1.set_varying (integer_type_node);
3682 r1.set_nonzero_bits (0xff);
3683 r0.union_ (r1);
3684 ASSERT_TRUE (r0.varying_p ());
3686 // Test that setting a nonzero bit of 1 does not pessimize the range.
3687 r0.set_zero (integer_type_node);
3688 r0.set_nonzero_bits (1);
3689 ASSERT_TRUE (r0.zero_p ());
3692 // Build an frange from string endpoints.
3694 static inline frange
3695 frange_float (const char *lb, const char *ub, tree type = float_type_node)
3697 REAL_VALUE_TYPE min, max;
3698 gcc_assert (real_from_string (&min, lb) == 0);
3699 gcc_assert (real_from_string (&max, ub) == 0);
3700 return frange (type, min, max);
3703 static void
3704 range_tests_nan ()
3706 frange r0, r1;
3707 REAL_VALUE_TYPE q, r;
3708 bool signbit;
3710 // Equal ranges but with differing NAN bits are not equal.
3711 if (HONOR_NANS (float_type_node))
3713 r1 = frange_float ("10", "12");
3714 r0 = r1;
3715 ASSERT_EQ (r0, r1);
3716 r0.clear_nan ();
3717 ASSERT_NE (r0, r1);
3718 r0.update_nan ();
3719 ASSERT_EQ (r0, r1);
3721 // [10, 20] NAN ^ [30, 40] NAN = NAN.
3722 r0 = frange_float ("10", "20");
3723 r1 = frange_float ("30", "40");
3724 r0.intersect (r1);
3725 ASSERT_TRUE (r0.known_isnan ());
3727 // [3,5] U [5,10] NAN = ... NAN
3728 r0 = frange_float ("3", "5");
3729 r0.clear_nan ();
3730 r1 = frange_float ("5", "10");
3731 r0.union_ (r1);
3732 ASSERT_TRUE (r0.maybe_isnan ());
3735 // NAN ranges are not equal to each other.
3736 r0.set_nan (float_type_node);
3737 r1 = r0;
3738 ASSERT_FALSE (r0 == r1);
3739 ASSERT_FALSE (r0 == r0);
3740 ASSERT_TRUE (r0 != r0);
3742 // [5,6] U NAN = [5,6] NAN.
3743 r0 = frange_float ("5", "6");
3744 r0.clear_nan ();
3745 r1.set_nan (float_type_node);
3746 r0.union_ (r1);
3747 real_from_string (&q, "5");
3748 real_from_string (&r, "6");
3749 ASSERT_TRUE (real_identical (&q, &r0.lower_bound ()));
3750 ASSERT_TRUE (real_identical (&r, &r0.upper_bound ()));
3751 ASSERT_TRUE (r0.maybe_isnan ());
3753 // NAN U NAN = NAN
3754 r0.set_nan (float_type_node);
3755 r1.set_nan (float_type_node);
3756 r0.union_ (r1);
3757 ASSERT_TRUE (r0.known_isnan ());
3759 // [INF, INF] NAN ^ NAN = NAN
3760 r0.set_nan (float_type_node);
3761 r1 = frange_float ("+Inf", "+Inf");
3762 if (!HONOR_NANS (float_type_node))
3763 r1.update_nan ();
3764 r0.intersect (r1);
3765 ASSERT_TRUE (r0.known_isnan ());
3767 // NAN ^ NAN = NAN
3768 r0.set_nan (float_type_node);
3769 r1.set_nan (float_type_node);
3770 r0.intersect (r1);
3771 ASSERT_TRUE (r0.known_isnan ());
3773 // +NAN ^ -NAN = UNDEFINED
3774 r0.set_nan (float_type_node, false);
3775 r1.set_nan (float_type_node, true);
3776 r0.intersect (r1);
3777 ASSERT_TRUE (r0.undefined_p ());
3779 // VARYING ^ NAN = NAN.
3780 r0.set_nan (float_type_node);
3781 r1.set_varying (float_type_node);
3782 r0.intersect (r1);
3783 ASSERT_TRUE (r0.known_isnan ());
3785 // [3,4] ^ NAN = UNDEFINED.
3786 r0 = frange_float ("3", "4");
3787 r0.clear_nan ();
3788 r1.set_nan (float_type_node);
3789 r0.intersect (r1);
3790 ASSERT_TRUE (r0.undefined_p ());
3792 // [-3, 5] ^ NAN = UNDEFINED
3793 r0 = frange_float ("-3", "5");
3794 r0.clear_nan ();
3795 r1.set_nan (float_type_node);
3796 r0.intersect (r1);
3797 ASSERT_TRUE (r0.undefined_p ());
3799 // Setting the NAN bit to yes does not make us a known NAN.
3800 r0.set_varying (float_type_node);
3801 r0.update_nan ();
3802 ASSERT_FALSE (r0.known_isnan ());
3804 // NAN is in a VARYING.
3805 r0.set_varying (float_type_node);
3806 real_nan (&r, "", 1, TYPE_MODE (float_type_node));
3807 tree nan = build_real (float_type_node, r);
3808 ASSERT_TRUE (r0.contains_p (nan));
3810 // -NAN is in a VARYING.
3811 r0.set_varying (float_type_node);
3812 q = real_value_negate (&r);
3813 tree neg_nan = build_real (float_type_node, q);
3814 ASSERT_TRUE (r0.contains_p (neg_nan));
3816 // Clearing the NAN on a [] NAN is the empty set.
3817 r0.set_nan (float_type_node);
3818 r0.clear_nan ();
3819 ASSERT_TRUE (r0.undefined_p ());
3821 // [10,20] NAN ^ [21,25] NAN = [NAN]
3822 r0 = frange_float ("10", "20");
3823 r0.update_nan ();
3824 r1 = frange_float ("21", "25");
3825 r1.update_nan ();
3826 r0.intersect (r1);
3827 ASSERT_TRUE (r0.known_isnan ());
3829 // NAN U [5,6] should be [5,6] +-NAN.
3830 r0.set_nan (float_type_node);
3831 r1 = frange_float ("5", "6");
3832 r1.clear_nan ();
3833 r0.union_ (r1);
3834 real_from_string (&q, "5");
3835 real_from_string (&r, "6");
3836 ASSERT_TRUE (real_identical (&q, &r0.lower_bound ()));
3837 ASSERT_TRUE (real_identical (&r, &r0.upper_bound ()));
3838 ASSERT_TRUE (!r0.signbit_p (signbit));
3839 ASSERT_TRUE (r0.maybe_isnan ());
3842 static void
3843 range_tests_signed_zeros ()
3845 tree zero = build_zero_cst (float_type_node);
3846 tree neg_zero = fold_build1 (NEGATE_EXPR, float_type_node, zero);
3847 frange r0, r1;
3848 bool signbit;
3850 // [0,0] contains [0,0] but not [-0,-0] and vice versa.
3851 r0 = frange (zero, zero);
3852 r1 = frange (neg_zero, neg_zero);
3853 ASSERT_TRUE (r0.contains_p (zero));
3854 ASSERT_TRUE (!r0.contains_p (neg_zero));
3855 ASSERT_TRUE (r1.contains_p (neg_zero));
3856 ASSERT_TRUE (!r1.contains_p (zero));
3858 // Test contains_p() when we know the sign of the zero.
3859 r0 = frange (zero, zero);
3860 ASSERT_TRUE (r0.contains_p (zero));
3861 ASSERT_FALSE (r0.contains_p (neg_zero));
3862 r0 = frange (neg_zero, neg_zero);
3863 ASSERT_TRUE (r0.contains_p (neg_zero));
3864 ASSERT_FALSE (r0.contains_p (zero));
3866 r0 = frange (neg_zero, zero);
3867 ASSERT_TRUE (r0.contains_p (neg_zero));
3868 ASSERT_TRUE (r0.contains_p (zero));
3870 r0 = frange_float ("-3", "5");
3871 ASSERT_TRUE (r0.contains_p (neg_zero));
3872 ASSERT_TRUE (r0.contains_p (zero));
3874 // The intersection of zeros that differ in sign is a NAN (or
3875 // undefined if not honoring NANs).
3876 r0 = frange (neg_zero, neg_zero);
3877 r1 = frange (zero, zero);
3878 r0.intersect (r1);
3879 if (HONOR_NANS (float_type_node))
3880 ASSERT_TRUE (r0.known_isnan ());
3881 else
3882 ASSERT_TRUE (r0.undefined_p ());
3884 // The union of zeros that differ in sign is a zero with unknown sign.
3885 r0 = frange (zero, zero);
3886 r1 = frange (neg_zero, neg_zero);
3887 r0.union_ (r1);
3888 ASSERT_TRUE (r0.zero_p () && !r0.signbit_p (signbit));
3890 // [-0, +0] has an unknown sign.
3891 r0 = frange (neg_zero, zero);
3892 ASSERT_TRUE (r0.zero_p () && !r0.signbit_p (signbit));
3894 // [-0, +0] ^ [0, 0] is [0, 0]
3895 r0 = frange (neg_zero, zero);
3896 r1 = frange (zero, zero);
3897 r0.intersect (r1);
3898 ASSERT_TRUE (r0.zero_p ());
3900 r0 = frange_float ("+0", "5");
3901 r0.clear_nan ();
3902 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
3904 r0 = frange_float ("-0", "5");
3905 r0.clear_nan ();
3906 ASSERT_TRUE (!r0.signbit_p (signbit));
3908 r0 = frange_float ("-0", "10");
3909 r1 = frange_float ("0", "5");
3910 r0.intersect (r1);
3911 ASSERT_TRUE (real_iszero (&r0.lower_bound (), false));
3913 r0 = frange_float ("-0", "5");
3914 r1 = frange_float ("0", "5");
3915 r0.union_ (r1);
3916 ASSERT_TRUE (real_iszero (&r0.lower_bound (), true));
3918 r0 = frange_float ("-5", "-0");
3919 r0.update_nan ();
3920 r1 = frange_float ("0", "0");
3921 r1.update_nan ();
3922 r0.intersect (r1);
3923 if (HONOR_NANS (float_type_node))
3924 ASSERT_TRUE (r0.known_isnan ());
3925 else
3926 ASSERT_TRUE (r0.undefined_p ());
3928 r0.set_nonnegative (float_type_node);
3929 if (HONOR_NANS (float_type_node))
3930 ASSERT_TRUE (r0.maybe_isnan ());
3932 // Numbers containing zero should have an unknown SIGNBIT.
3933 r0 = frange_float ("0", "10");
3934 r0.clear_nan ();
3935 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
3938 static void
3939 range_tests_signbit ()
3941 frange r0, r1;
3942 bool signbit;
3944 // Negative numbers should have the SIGNBIT set.
3945 r0 = frange_float ("-5", "-1");
3946 r0.clear_nan ();
3947 ASSERT_TRUE (r0.signbit_p (signbit) && signbit);
3948 // Positive numbers should have the SIGNBIT clear.
3949 r0 = frange_float ("1", "10");
3950 r0.clear_nan ();
3951 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
3952 // Numbers spanning both positive and negative should have an
3953 // unknown SIGNBIT.
3954 r0 = frange_float ("-10", "10");
3955 r0.clear_nan ();
3956 ASSERT_TRUE (!r0.signbit_p (signbit));
3957 r0.set_varying (float_type_node);
3958 ASSERT_TRUE (!r0.signbit_p (signbit));
3961 static void
3962 range_tests_floats ()
3964 frange r0, r1;
3966 if (HONOR_NANS (float_type_node))
3967 range_tests_nan ();
3968 range_tests_signbit ();
3970 if (HONOR_SIGNED_ZEROS (float_type_node))
3971 range_tests_signed_zeros ();
3973 // A range of [-INF,+INF] is actually VARYING if no other properties
3974 // are set.
3975 r0 = frange_float ("-Inf", "+Inf");
3976 ASSERT_TRUE (r0.varying_p ());
3977 // ...unless it has some special property...
3978 if (HONOR_NANS (r0.type ()))
3980 r0.clear_nan ();
3981 ASSERT_FALSE (r0.varying_p ());
3984 // For most architectures, where float and double are different
3985 // sizes, having the same endpoints does not necessarily mean the
3986 // ranges are equal.
3987 if (!types_compatible_p (float_type_node, double_type_node))
3989 r0 = frange_float ("3.0", "3.0", float_type_node);
3990 r1 = frange_float ("3.0", "3.0", double_type_node);
3991 ASSERT_NE (r0, r1);
3994 // [3,5] U [10,12] = [3,12].
3995 r0 = frange_float ("3", "5");
3996 r1 = frange_float ("10", "12");
3997 r0.union_ (r1);
3998 ASSERT_EQ (r0, frange_float ("3", "12"));
4000 // [5,10] U [4,8] = [4,10]
4001 r0 = frange_float ("5", "10");
4002 r1 = frange_float ("4", "8");
4003 r0.union_ (r1);
4004 ASSERT_EQ (r0, frange_float ("4", "10"));
4006 // [3,5] U [4,10] = [3,10]
4007 r0 = frange_float ("3", "5");
4008 r1 = frange_float ("4", "10");
4009 r0.union_ (r1);
4010 ASSERT_EQ (r0, frange_float ("3", "10"));
4012 // [4,10] U [5,11] = [4,11]
4013 r0 = frange_float ("4", "10");
4014 r1 = frange_float ("5", "11");
4015 r0.union_ (r1);
4016 ASSERT_EQ (r0, frange_float ("4", "11"));
4018 // [3,12] ^ [10,12] = [10,12].
4019 r0 = frange_float ("3", "12");
4020 r1 = frange_float ("10", "12");
4021 r0.intersect (r1);
4022 ASSERT_EQ (r0, frange_float ("10", "12"));
4024 // [10,12] ^ [11,11] = [11,11]
4025 r0 = frange_float ("10", "12");
4026 r1 = frange_float ("11", "11");
4027 r0.intersect (r1);
4028 ASSERT_EQ (r0, frange_float ("11", "11"));
4030 // [10,20] ^ [5,15] = [10,15]
4031 r0 = frange_float ("10", "20");
4032 r1 = frange_float ("5", "15");
4033 r0.intersect (r1);
4034 ASSERT_EQ (r0, frange_float ("10", "15"));
4036 // [10,20] ^ [15,25] = [15,20]
4037 r0 = frange_float ("10", "20");
4038 r1 = frange_float ("15", "25");
4039 r0.intersect (r1);
4040 ASSERT_EQ (r0, frange_float ("15", "20"));
4042 // [10,20] ^ [21,25] = []
4043 r0 = frange_float ("10", "20");
4044 r0.clear_nan ();
4045 r1 = frange_float ("21", "25");
4046 r1.clear_nan ();
4047 r0.intersect (r1);
4048 ASSERT_TRUE (r0.undefined_p ());
4050 if (HONOR_INFINITIES (float_type_node))
4052 // Make sure [-Inf, -Inf] doesn't get normalized.
4053 r0 = frange_float ("-Inf", "-Inf");
4054 ASSERT_TRUE (real_isinf (&r0.lower_bound (), true));
4055 ASSERT_TRUE (real_isinf (&r0.upper_bound (), true));
4058 // Test that reading back a global range yields the same result as
4059 // what we wrote into it.
4060 tree ssa = make_temp_ssa_name (float_type_node, NULL, "blah");
4061 r0.set_varying (float_type_node);
4062 r0.clear_nan ();
4063 set_range_info (ssa, r0);
4064 get_global_range_query ()->range_of_expr (r1, ssa);
4065 ASSERT_EQ (r0, r1);
4068 // Run floating range tests for various combinations of NAN and INF
4069 // support.
4071 static void
4072 range_tests_floats_various ()
4074 int save_finite_math_only = flag_finite_math_only;
4076 // Test -ffinite-math-only.
4077 flag_finite_math_only = 1;
4078 range_tests_floats ();
4079 // Test -fno-finite-math-only.
4080 flag_finite_math_only = 0;
4081 range_tests_floats ();
4083 flag_finite_math_only = save_finite_math_only;
4086 void
4087 range_tests ()
4089 range_tests_legacy ();
4090 range_tests_irange3 ();
4091 range_tests_int_range_max ();
4092 range_tests_strict_enum ();
4093 range_tests_nonzero_bits ();
4094 range_tests_floats_various ();
4095 range_tests_misc ();
4098 } // namespace selftest
4100 #endif // CHECKING_P