]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/value-range.cc
A singleton irange has all known bits.
[thirdparty/gcc.git] / gcc / value-range.cc
1 /* Support routines for value ranges.
2 Copyright (C) 2019-2023 Free Software Foundation, Inc.
3 Major hacks by Aldy Hernandez <aldyh@redhat.com> and
4 Andrew MacLeod <amacleod@redhat.com>.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
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"
33
34 void
35 irange::accept (const vrange_visitor &v) const
36 {
37 v.visit (*this);
38 }
39
40 void
41 unsupported_range::accept (const vrange_visitor &v) const
42 {
43 v.visit (*this);
44 }
45
46 // Convenience function only available for integers and pointers.
47
48 wide_int
49 Value_Range::lower_bound () const
50 {
51 if (is_a <irange> (*m_vrange))
52 return as_a <irange> (*m_vrange).lower_bound ();
53 gcc_unreachable ();
54 }
55
56 // Convenience function only available for integers and pointers.
57
58 wide_int
59 Value_Range::upper_bound () const
60 {
61 if (is_a <irange> (*m_vrange))
62 return as_a <irange> (*m_vrange).upper_bound ();
63 gcc_unreachable ();
64 }
65
66 void
67 Value_Range::dump (FILE *out) const
68 {
69 if (m_vrange)
70 m_vrange->dump (out);
71 else
72 fprintf (out, "NULL");
73 }
74
75 DEBUG_FUNCTION void
76 debug (const Value_Range &r)
77 {
78 r.dump (stderr);
79 fprintf (stderr, "\n");
80 }
81
82 DEBUG_FUNCTION void
83 debug (const irange_bitmask &bm)
84 {
85 bm.dump (stderr);
86 fprintf (stderr, "\n");
87 }
88
89 // Default vrange definitions.
90
91 bool
92 vrange::contains_p (tree) const
93 {
94 return varying_p ();
95 }
96
97 bool
98 vrange::singleton_p (tree *) const
99 {
100 return false;
101 }
102
103 void
104 vrange::set (tree min, tree, value_range_kind)
105 {
106 set_varying (TREE_TYPE (min));
107 }
108
109 tree
110 vrange::type () const
111 {
112 return void_type_node;
113 }
114
115 bool
116 vrange::supports_type_p (const_tree) const
117 {
118 return false;
119 }
120
121 void
122 vrange::set_undefined ()
123 {
124 m_kind = VR_UNDEFINED;
125 }
126
127 void
128 vrange::set_varying (tree)
129 {
130 m_kind = VR_VARYING;
131 }
132
133 bool
134 vrange::union_ (const vrange &r)
135 {
136 if (r.undefined_p () || varying_p ())
137 return false;
138 if (undefined_p () || r.varying_p ())
139 {
140 operator= (r);
141 return true;
142 }
143 gcc_unreachable ();
144 return false;
145 }
146
147 bool
148 vrange::intersect (const vrange &r)
149 {
150 if (undefined_p () || r.varying_p ())
151 return false;
152 if (r.undefined_p ())
153 {
154 set_undefined ();
155 return true;
156 }
157 if (varying_p ())
158 {
159 operator= (r);
160 return true;
161 }
162 gcc_unreachable ();
163 return false;
164 }
165
166 bool
167 vrange::zero_p () const
168 {
169 return false;
170 }
171
172 bool
173 vrange::nonzero_p () const
174 {
175 return false;
176 }
177
178 void
179 vrange::set_nonzero (tree type)
180 {
181 set_varying (type);
182 }
183
184 void
185 vrange::set_zero (tree type)
186 {
187 set_varying (type);
188 }
189
190 void
191 vrange::set_nonnegative (tree type)
192 {
193 set_varying (type);
194 }
195
196 bool
197 vrange::fits_p (const vrange &) const
198 {
199 return true;
200 }
201
202 // Assignment operator for generic ranges. Copying incompatible types
203 // is not allowed.
204
205 vrange &
206 vrange::operator= (const vrange &src)
207 {
208 if (is_a <irange> (src))
209 as_a <irange> (*this) = as_a <irange> (src);
210 else if (is_a <frange> (src))
211 as_a <frange> (*this) = as_a <frange> (src);
212 else
213 {
214 gcc_checking_assert (is_a <unsupported_range> (src));
215 m_kind = src.m_kind;
216 }
217 return *this;
218 }
219
220 // Equality operator for generic ranges.
221
222 bool
223 vrange::operator== (const vrange &src) const
224 {
225 if (is_a <irange> (src))
226 return as_a <irange> (*this) == as_a <irange> (src);
227 if (is_a <frange> (src))
228 return as_a <frange> (*this) == as_a <frange> (src);
229 gcc_unreachable ();
230 }
231
232 // Wrapper for vrange_printer to dump a range to a file.
233
234 void
235 vrange::dump (FILE *file) const
236 {
237 pretty_printer buffer;
238 pp_needs_newline (&buffer) = true;
239 buffer.buffer->stream = file;
240 vrange_printer vrange_pp (&buffer);
241 this->accept (vrange_pp);
242 pp_flush (&buffer);
243 }
244
245 void
246 irange_bitmask::dump (FILE *file) const
247 {
248 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
249 pretty_printer buffer;
250
251 pp_needs_newline (&buffer) = true;
252 buffer.buffer->stream = file;
253 pp_string (&buffer, "MASK ");
254 print_hex (m_mask, buf);
255 pp_string (&buffer, buf);
256 pp_string (&buffer, " VALUE ");
257 print_hex (m_value, buf);
258 pp_string (&buffer, buf);
259 pp_flush (&buffer);
260 }
261
262 namespace inchash
263 {
264
265 void
266 add_vrange (const vrange &v, inchash::hash &hstate,
267 unsigned int)
268 {
269 if (v.undefined_p ())
270 {
271 hstate.add_int (VR_UNDEFINED);
272 return;
273 }
274 // Types are ignored throughout to inhibit two ranges being equal
275 // but having different hash values. This can happen when two
276 // ranges are equal and their types are different (but
277 // types_compatible_p is true).
278 if (is_a <irange> (v))
279 {
280 const irange &r = as_a <irange> (v);
281 if (r.varying_p ())
282 hstate.add_int (VR_VARYING);
283 else
284 hstate.add_int (VR_RANGE);
285 for (unsigned i = 0; i < r.num_pairs (); ++i)
286 {
287 hstate.add_wide_int (r.lower_bound (i));
288 hstate.add_wide_int (r.upper_bound (i));
289 }
290 irange_bitmask bm = r.get_bitmask ();
291 hstate.add_wide_int (bm.value ());
292 hstate.add_wide_int (bm.mask ());
293 return;
294 }
295 if (is_a <frange> (v))
296 {
297 const frange &r = as_a <frange> (v);
298 if (r.known_isnan ())
299 hstate.add_int (VR_NAN);
300 else
301 {
302 hstate.add_int (r.varying_p () ? VR_VARYING : VR_RANGE);
303 hstate.add_real_value (r.lower_bound ());
304 hstate.add_real_value (r.upper_bound ());
305 }
306 nan_state nan = r.get_nan_state ();
307 hstate.add_int (nan.pos_p ());
308 hstate.add_int (nan.neg_p ());
309 return;
310 }
311 gcc_unreachable ();
312 }
313
314 } //namespace inchash
315
316 bool
317 irange::supports_type_p (const_tree type) const
318 {
319 return supports_p (type);
320 }
321
322 // Return TRUE if R fits in THIS.
323
324 bool
325 irange::fits_p (const vrange &r) const
326 {
327 return m_max_ranges >= as_a <irange> (r).num_pairs ();
328 }
329
330 void
331 irange::set_nonnegative (tree type)
332 {
333 set (type,
334 wi::zero (TYPE_PRECISION (type)),
335 wi::to_wide (TYPE_MAX_VALUE (type)));
336 }
337
338 void
339 frange::accept (const vrange_visitor &v) const
340 {
341 v.visit (*this);
342 }
343
344 // Flush denormal endpoints to the appropriate 0.0.
345
346 void
347 frange::flush_denormals_to_zero ()
348 {
349 if (undefined_p () || known_isnan ())
350 return;
351
352 machine_mode mode = TYPE_MODE (type ());
353 // Flush [x, -DENORMAL] to [x, -0.0].
354 if (real_isdenormal (&m_max, mode) && real_isneg (&m_max))
355 {
356 if (HONOR_SIGNED_ZEROS (m_type))
357 m_max = dconstm0;
358 else
359 m_max = dconst0;
360 }
361 // Flush [+DENORMAL, x] to [+0.0, x].
362 if (real_isdenormal (&m_min, mode) && !real_isneg (&m_min))
363 m_min = dconst0;
364 }
365
366 // Setter for franges.
367
368 void
369 frange::set (tree type,
370 const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
371 const nan_state &nan, value_range_kind kind)
372 {
373 switch (kind)
374 {
375 case VR_UNDEFINED:
376 set_undefined ();
377 return;
378 case VR_VARYING:
379 case VR_ANTI_RANGE:
380 set_varying (type);
381 return;
382 case VR_RANGE:
383 break;
384 default:
385 gcc_unreachable ();
386 }
387
388 gcc_checking_assert (!real_isnan (&min) && !real_isnan (&max));
389
390 m_kind = kind;
391 m_type = type;
392 m_min = min;
393 m_max = max;
394 if (HONOR_NANS (m_type))
395 {
396 m_pos_nan = nan.pos_p ();
397 m_neg_nan = nan.neg_p ();
398 }
399 else
400 {
401 m_pos_nan = false;
402 m_neg_nan = false;
403 }
404
405 if (!MODE_HAS_SIGNED_ZEROS (TYPE_MODE (m_type)))
406 {
407 if (real_iszero (&m_min, 1))
408 m_min.sign = 0;
409 if (real_iszero (&m_max, 1))
410 m_max.sign = 0;
411 }
412 else if (!HONOR_SIGNED_ZEROS (m_type))
413 {
414 if (real_iszero (&m_max, 1))
415 m_max.sign = 0;
416 if (real_iszero (&m_min, 0))
417 m_min.sign = 1;
418 }
419
420 // For -ffinite-math-only we can drop ranges outside the
421 // representable numbers to min/max for the type.
422 if (!HONOR_INFINITIES (m_type))
423 {
424 REAL_VALUE_TYPE min_repr = frange_val_min (m_type);
425 REAL_VALUE_TYPE max_repr = frange_val_max (m_type);
426 if (real_less (&m_min, &min_repr))
427 m_min = min_repr;
428 else if (real_less (&max_repr, &m_min))
429 m_min = max_repr;
430 if (real_less (&max_repr, &m_max))
431 m_max = max_repr;
432 else if (real_less (&m_max, &min_repr))
433 m_max = min_repr;
434 }
435
436 // Check for swapped ranges.
437 gcc_checking_assert (real_compare (LE_EXPR, &min, &max));
438
439 normalize_kind ();
440 }
441
442 // Setter for an frange defaulting the NAN possibility to +-NAN when
443 // HONOR_NANS.
444
445 void
446 frange::set (tree type,
447 const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
448 value_range_kind kind)
449 {
450 set (type, min, max, nan_state (true), kind);
451 }
452
453 void
454 frange::set (tree min, tree max, value_range_kind kind)
455 {
456 set (TREE_TYPE (min),
457 *TREE_REAL_CST_PTR (min), *TREE_REAL_CST_PTR (max), kind);
458 }
459
460 // Normalize range to VARYING or UNDEFINED, or vice versa. Return
461 // TRUE if anything changed.
462 //
463 // A range with no known properties can be dropped to VARYING.
464 // Similarly, a VARYING with any properties should be dropped to a
465 // VR_RANGE. Normalizing ranges upon changing them ensures there is
466 // only one representation for a given range.
467
468 bool
469 frange::normalize_kind ()
470 {
471 if (m_kind == VR_RANGE
472 && frange_val_is_min (m_min, m_type)
473 && frange_val_is_max (m_max, m_type))
474 {
475 if (!HONOR_NANS (m_type) || (m_pos_nan && m_neg_nan))
476 {
477 set_varying (m_type);
478 return true;
479 }
480 }
481 else if (m_kind == VR_VARYING)
482 {
483 if (HONOR_NANS (m_type) && (!m_pos_nan || !m_neg_nan))
484 {
485 m_kind = VR_RANGE;
486 m_min = frange_val_min (m_type);
487 m_max = frange_val_max (m_type);
488 if (flag_checking)
489 verify_range ();
490 return true;
491 }
492 }
493 else if (m_kind == VR_NAN && !m_pos_nan && !m_neg_nan)
494 set_undefined ();
495 return false;
496 }
497
498 // Union or intersect the zero endpoints of two ranges. For example:
499 // [-0, x] U [+0, x] => [-0, x]
500 // [ x, -0] U [ x, +0] => [ x, +0]
501 // [-0, x] ^ [+0, x] => [+0, x]
502 // [ x, -0] ^ [ x, +0] => [ x, -0]
503 //
504 // UNION_P is true when performing a union, or false when intersecting.
505
506 bool
507 frange::combine_zeros (const frange &r, bool union_p)
508 {
509 gcc_checking_assert (!undefined_p () && !known_isnan ());
510
511 bool changed = false;
512 if (real_iszero (&m_min) && real_iszero (&r.m_min)
513 && real_isneg (&m_min) != real_isneg (&r.m_min))
514 {
515 m_min.sign = union_p;
516 changed = true;
517 }
518 if (real_iszero (&m_max) && real_iszero (&r.m_max)
519 && real_isneg (&m_max) != real_isneg (&r.m_max))
520 {
521 m_max.sign = !union_p;
522 changed = true;
523 }
524 // If the signs are swapped, the resulting range is empty.
525 if (m_min.sign == 0 && m_max.sign == 1)
526 {
527 if (maybe_isnan ())
528 m_kind = VR_NAN;
529 else
530 set_undefined ();
531 changed = true;
532 }
533 return changed;
534 }
535
536 // Union two ranges when one is known to be a NAN.
537
538 bool
539 frange::union_nans (const frange &r)
540 {
541 gcc_checking_assert (known_isnan () || r.known_isnan ());
542
543 if (known_isnan ())
544 {
545 m_kind = r.m_kind;
546 m_min = r.m_min;
547 m_max = r.m_max;
548 }
549 m_pos_nan |= r.m_pos_nan;
550 m_neg_nan |= r.m_neg_nan;
551 normalize_kind ();
552 return true;
553 }
554
555 bool
556 frange::union_ (const vrange &v)
557 {
558 const frange &r = as_a <frange> (v);
559
560 if (r.undefined_p () || varying_p ())
561 return false;
562 if (undefined_p () || r.varying_p ())
563 {
564 *this = r;
565 return true;
566 }
567
568 // Combine NAN info.
569 if (known_isnan () || r.known_isnan ())
570 return union_nans (r);
571 bool changed = false;
572 if (m_pos_nan != r.m_pos_nan || m_neg_nan != r.m_neg_nan)
573 {
574 m_pos_nan |= r.m_pos_nan;
575 m_neg_nan |= r.m_neg_nan;
576 changed = true;
577 }
578
579 // Combine endpoints.
580 if (real_less (&r.m_min, &m_min))
581 {
582 m_min = r.m_min;
583 changed = true;
584 }
585 if (real_less (&m_max, &r.m_max))
586 {
587 m_max = r.m_max;
588 changed = true;
589 }
590
591 if (HONOR_SIGNED_ZEROS (m_type))
592 changed |= combine_zeros (r, true);
593
594 changed |= normalize_kind ();
595 return changed;
596 }
597
598 // Intersect two ranges when one is known to be a NAN.
599
600 bool
601 frange::intersect_nans (const frange &r)
602 {
603 gcc_checking_assert (known_isnan () || r.known_isnan ());
604
605 m_pos_nan &= r.m_pos_nan;
606 m_neg_nan &= r.m_neg_nan;
607 if (maybe_isnan ())
608 m_kind = VR_NAN;
609 else
610 set_undefined ();
611 if (flag_checking)
612 verify_range ();
613 return true;
614 }
615
616 bool
617 frange::intersect (const vrange &v)
618 {
619 const frange &r = as_a <frange> (v);
620
621 if (undefined_p () || r.varying_p ())
622 return false;
623 if (r.undefined_p ())
624 {
625 set_undefined ();
626 return true;
627 }
628 if (varying_p ())
629 {
630 *this = r;
631 return true;
632 }
633
634 // Combine NAN info.
635 if (known_isnan () || r.known_isnan ())
636 return intersect_nans (r);
637 bool changed = false;
638 if (m_pos_nan != r.m_pos_nan || m_neg_nan != r.m_neg_nan)
639 {
640 m_pos_nan &= r.m_pos_nan;
641 m_neg_nan &= r.m_neg_nan;
642 changed = true;
643 }
644
645 // Combine endpoints.
646 if (real_less (&m_min, &r.m_min))
647 {
648 m_min = r.m_min;
649 changed = true;
650 }
651 if (real_less (&r.m_max, &m_max))
652 {
653 m_max = r.m_max;
654 changed = true;
655 }
656 // If the endpoints are swapped, the resulting range is empty.
657 if (real_less (&m_max, &m_min))
658 {
659 if (maybe_isnan ())
660 m_kind = VR_NAN;
661 else
662 set_undefined ();
663 if (flag_checking)
664 verify_range ();
665 return true;
666 }
667
668 if (HONOR_SIGNED_ZEROS (m_type))
669 changed |= combine_zeros (r, false);
670
671 changed |= normalize_kind ();
672 return changed;
673 }
674
675 frange &
676 frange::operator= (const frange &src)
677 {
678 m_kind = src.m_kind;
679 m_type = src.m_type;
680 m_min = src.m_min;
681 m_max = src.m_max;
682 m_pos_nan = src.m_pos_nan;
683 m_neg_nan = src.m_neg_nan;
684
685 if (flag_checking)
686 verify_range ();
687 return *this;
688 }
689
690 bool
691 frange::operator== (const frange &src) const
692 {
693 if (m_kind == src.m_kind)
694 {
695 if (undefined_p ())
696 return true;
697
698 if (varying_p ())
699 return types_compatible_p (m_type, src.m_type);
700
701 bool nan1 = known_isnan ();
702 bool nan2 = src.known_isnan ();
703 if (nan1 || nan2)
704 {
705 if (nan1 && nan2)
706 return (m_pos_nan == src.m_pos_nan
707 && m_neg_nan == src.m_neg_nan);
708 return false;
709 }
710
711 return (real_identical (&m_min, &src.m_min)
712 && real_identical (&m_max, &src.m_max)
713 && m_pos_nan == src.m_pos_nan
714 && m_neg_nan == src.m_neg_nan
715 && types_compatible_p (m_type, src.m_type));
716 }
717 return false;
718 }
719
720 // Return TRUE if range contains R.
721
722 bool
723 frange::contains_p (const REAL_VALUE_TYPE &r) const
724 {
725 gcc_checking_assert (m_kind != VR_ANTI_RANGE);
726
727 if (undefined_p ())
728 return false;
729
730 if (varying_p ())
731 return true;
732
733 if (real_isnan (&r))
734 {
735 // No NAN in range.
736 if (!m_pos_nan && !m_neg_nan)
737 return false;
738 // Both +NAN and -NAN are present.
739 if (m_pos_nan && m_neg_nan)
740 return true;
741 return m_neg_nan == r.sign;
742 }
743 if (known_isnan ())
744 return false;
745
746 if (real_compare (GE_EXPR, &r, &m_min) && real_compare (LE_EXPR, &r, &m_max))
747 {
748 // Make sure the signs are equal for signed zeros.
749 if (HONOR_SIGNED_ZEROS (m_type) && real_iszero (&r))
750 return r.sign == m_min.sign || r.sign == m_max.sign;
751 return true;
752 }
753 return false;
754 }
755
756 // If range is a singleton, place it in RESULT and return TRUE. If
757 // RESULT is NULL, just return TRUE.
758 //
759 // A NAN can never be a singleton.
760
761 bool
762 frange::internal_singleton_p (REAL_VALUE_TYPE *result) const
763 {
764 if (m_kind == VR_RANGE && real_identical (&m_min, &m_max))
765 {
766 // Return false for any singleton that may be a NAN.
767 if (HONOR_NANS (m_type) && maybe_isnan ())
768 return false;
769
770 if (MODE_COMPOSITE_P (TYPE_MODE (m_type)))
771 {
772 // For IBM long doubles, if the value is +-Inf or is exactly
773 // representable in double, the other double could be +0.0
774 // or -0.0. Since this means there is more than one way to
775 // represent a value, return false to avoid propagating it.
776 // See libgcc/config/rs6000/ibm-ldouble-format for details.
777 if (real_isinf (&m_min))
778 return false;
779 REAL_VALUE_TYPE r;
780 real_convert (&r, DFmode, &m_min);
781 if (real_identical (&r, &m_min))
782 return false;
783 }
784
785 if (result)
786 *result = m_min;
787 return true;
788 }
789 return false;
790 }
791
792 bool
793 frange::singleton_p (tree *result) const
794 {
795 if (internal_singleton_p ())
796 {
797 if (result)
798 *result = build_real (m_type, m_min);
799 return true;
800 }
801 return false;
802 }
803
804 bool
805 frange::singleton_p (REAL_VALUE_TYPE &r) const
806 {
807 return internal_singleton_p (&r);
808 }
809
810 bool
811 frange::supports_type_p (const_tree type) const
812 {
813 return supports_p (type);
814 }
815
816 void
817 frange::verify_range ()
818 {
819 if (!undefined_p ())
820 gcc_checking_assert (HONOR_NANS (m_type) || !maybe_isnan ());
821 switch (m_kind)
822 {
823 case VR_UNDEFINED:
824 gcc_checking_assert (!m_type);
825 return;
826 case VR_VARYING:
827 gcc_checking_assert (m_type);
828 gcc_checking_assert (frange_val_is_min (m_min, m_type));
829 gcc_checking_assert (frange_val_is_max (m_max, m_type));
830 if (HONOR_NANS (m_type))
831 gcc_checking_assert (m_pos_nan && m_neg_nan);
832 else
833 gcc_checking_assert (!m_pos_nan && !m_neg_nan);
834 return;
835 case VR_RANGE:
836 gcc_checking_assert (m_type);
837 break;
838 case VR_NAN:
839 gcc_checking_assert (m_type);
840 gcc_checking_assert (m_pos_nan || m_neg_nan);
841 return;
842 default:
843 gcc_unreachable ();
844 }
845
846 // NANs cannot appear in the endpoints of a range.
847 gcc_checking_assert (!real_isnan (&m_min) && !real_isnan (&m_max));
848
849 // Make sure we don't have swapped ranges.
850 gcc_checking_assert (!real_less (&m_max, &m_min));
851
852 // [ +0.0, -0.0 ] is nonsensical.
853 gcc_checking_assert (!(real_iszero (&m_min, 0) && real_iszero (&m_max, 1)));
854
855 // If all the properties are clear, we better not span the entire
856 // domain, because that would make us varying.
857 if (m_pos_nan && m_neg_nan)
858 gcc_checking_assert (!frange_val_is_min (m_min, m_type)
859 || !frange_val_is_max (m_max, m_type));
860 }
861
862 // We can't do much with nonzeros yet.
863 void
864 frange::set_nonzero (tree type)
865 {
866 set_varying (type);
867 }
868
869 // We can't do much with nonzeros yet.
870 bool
871 frange::nonzero_p () const
872 {
873 return false;
874 }
875
876 // Set range to [+0.0, +0.0] if honoring signed zeros, or [0.0, 0.0]
877 // otherwise.
878
879 void
880 frange::set_zero (tree type)
881 {
882 if (HONOR_SIGNED_ZEROS (type))
883 {
884 set (type, dconstm0, dconst0);
885 clear_nan ();
886 }
887 else
888 set (type, dconst0, dconst0);
889 }
890
891 // Return TRUE for any zero regardless of sign.
892
893 bool
894 frange::zero_p () const
895 {
896 return (m_kind == VR_RANGE
897 && real_iszero (&m_min)
898 && real_iszero (&m_max));
899 }
900
901 // Set the range to non-negative numbers, that is [+0.0, +INF].
902 //
903 // The NAN in the resulting range (if HONOR_NANS) has a varying sign
904 // as there are no guarantees in IEEE 754 wrt to the sign of a NAN,
905 // except for copy, abs, and copysign. It is the responsibility of
906 // the caller to set the NAN's sign if desired.
907
908 void
909 frange::set_nonnegative (tree type)
910 {
911 set (type, dconst0, frange_val_max (type));
912 }
913
914 // Here we copy between any two irange's.
915
916 irange &
917 irange::operator= (const irange &src)
918 {
919 int needed = src.num_pairs ();
920 maybe_resize (needed);
921
922 unsigned x;
923 unsigned lim = src.m_num_ranges;
924 if (lim > m_max_ranges)
925 lim = m_max_ranges;
926
927 for (x = 0; x < lim * 2; ++x)
928 m_base[x] = src.m_base[x];
929
930 // If the range didn't fit, the last range should cover the rest.
931 if (lim != src.m_num_ranges)
932 m_base[x - 1] = src.m_base[src.m_num_ranges * 2 - 1];
933
934 m_num_ranges = lim;
935 m_type = src.m_type;
936 m_kind = src.m_kind;
937 m_bitmask = src.m_bitmask;
938 if (m_max_ranges == 1)
939 normalize_kind ();
940 if (flag_checking)
941 verify_range ();
942 return *this;
943 }
944
945 value_range_kind
946 get_legacy_range (const irange &r, tree &min, tree &max)
947 {
948 if (r.undefined_p ())
949 {
950 min = NULL_TREE;
951 max = NULL_TREE;
952 return VR_UNDEFINED;
953 }
954
955 tree type = r.type ();
956 if (r.varying_p ())
957 {
958 min = wide_int_to_tree (type, r.lower_bound ());
959 max = wide_int_to_tree (type, r.upper_bound ());
960 return VR_VARYING;
961 }
962
963 unsigned int precision = TYPE_PRECISION (type);
964 signop sign = TYPE_SIGN (type);
965 if (r.num_pairs () > 1
966 && precision > 1
967 && r.lower_bound () == wi::min_value (precision, sign)
968 && r.upper_bound () == wi::max_value (precision, sign))
969 {
970 int_range<3> inv (r);
971 inv.invert ();
972 min = wide_int_to_tree (type, inv.lower_bound (0));
973 max = wide_int_to_tree (type, inv.upper_bound (0));
974 return VR_ANTI_RANGE;
975 }
976
977 min = wide_int_to_tree (type, r.lower_bound ());
978 max = wide_int_to_tree (type, r.upper_bound ());
979 return VR_RANGE;
980 }
981
982 /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
983 This means adjusting VRTYPE, MIN and MAX representing the case of a
984 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
985 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
986 In corner cases where MAX+1 or MIN-1 wraps this will fall back
987 to varying.
988 This routine exists to ease canonicalization in the case where we
989 extract ranges from var + CST op limit. */
990
991 void
992 irange::set (tree type, const wide_int &min, const wide_int &max,
993 value_range_kind kind)
994 {
995 unsigned prec = TYPE_PRECISION (type);
996 signop sign = TYPE_SIGN (type);
997 wide_int min_value = wi::min_value (prec, sign);
998 wide_int max_value = wi::max_value (prec, sign);
999
1000 m_type = type;
1001 m_bitmask.set_unknown (prec);
1002
1003 if (kind == VR_RANGE)
1004 {
1005 m_base[0] = min;
1006 m_base[1] = max;
1007 m_num_ranges = 1;
1008 if (min == min_value && max == max_value)
1009 m_kind = VR_VARYING;
1010 else
1011 m_kind = VR_RANGE;
1012 }
1013 else
1014 {
1015 gcc_checking_assert (kind == VR_ANTI_RANGE);
1016 gcc_checking_assert (m_max_ranges > 1);
1017
1018 m_kind = VR_UNDEFINED;
1019 m_num_ranges = 0;
1020 wi::overflow_type ovf;
1021 wide_int lim;
1022 if (sign == SIGNED)
1023 lim = wi::add (min, -1, sign, &ovf);
1024 else
1025 lim = wi::sub (min, 1, sign, &ovf);
1026
1027 if (!ovf)
1028 {
1029 m_kind = VR_RANGE;
1030 m_base[0] = min_value;
1031 m_base[1] = lim;
1032 ++m_num_ranges;
1033 }
1034 if (sign == SIGNED)
1035 lim = wi::sub (max, -1, sign, &ovf);
1036 else
1037 lim = wi::add (max, 1, sign, &ovf);
1038 if (!ovf)
1039 {
1040 m_kind = VR_RANGE;
1041 m_base[m_num_ranges * 2] = lim;
1042 m_base[m_num_ranges * 2 + 1] = max_value;
1043 ++m_num_ranges;
1044 }
1045 }
1046
1047 if (flag_checking)
1048 verify_range ();
1049 }
1050
1051 void
1052 irange::set (tree min, tree max, value_range_kind kind)
1053 {
1054 if (POLY_INT_CST_P (min) || POLY_INT_CST_P (max))
1055 {
1056 set_varying (TREE_TYPE (min));
1057 return;
1058 }
1059
1060 gcc_checking_assert (TREE_CODE (min) == INTEGER_CST);
1061 gcc_checking_assert (TREE_CODE (max) == INTEGER_CST);
1062
1063 return set (TREE_TYPE (min), wi::to_wide (min), wi::to_wide (max), kind);
1064 }
1065
1066 // Check the validity of the range.
1067
1068 void
1069 irange::verify_range ()
1070 {
1071 gcc_checking_assert (m_discriminator == VR_IRANGE);
1072 if (m_kind == VR_UNDEFINED)
1073 {
1074 gcc_checking_assert (m_num_ranges == 0);
1075 return;
1076 }
1077 gcc_checking_assert (m_num_ranges <= m_max_ranges);
1078
1079 // Legacy allowed these to represent VARYING for unknown types.
1080 // Leave this in for now, until all users are converted. Eventually
1081 // we should abort in set_varying.
1082 if (m_kind == VR_VARYING && m_type == error_mark_node)
1083 return;
1084
1085 unsigned prec = TYPE_PRECISION (m_type);
1086 if (m_kind == VR_VARYING)
1087 {
1088 gcc_checking_assert (m_bitmask.unknown_p ());
1089 gcc_checking_assert (m_num_ranges == 1);
1090 gcc_checking_assert (varying_compatible_p ());
1091 gcc_checking_assert (lower_bound ().get_precision () == prec);
1092 gcc_checking_assert (upper_bound ().get_precision () == prec);
1093 return;
1094 }
1095 gcc_checking_assert (m_num_ranges != 0);
1096 gcc_checking_assert (!varying_compatible_p ());
1097 for (unsigned i = 0; i < m_num_ranges; ++i)
1098 {
1099 wide_int lb = lower_bound (i);
1100 wide_int ub = upper_bound (i);
1101 gcc_checking_assert (lb.get_precision () == prec);
1102 gcc_checking_assert (ub.get_precision () == prec);
1103 int c = wi::cmp (lb, ub, TYPE_SIGN (m_type));
1104 gcc_checking_assert (c == 0 || c == -1);
1105 }
1106 m_bitmask.verify_mask ();
1107 }
1108
1109 bool
1110 irange::operator== (const irange &other) const
1111 {
1112 if (m_num_ranges != other.m_num_ranges)
1113 return false;
1114
1115 if (m_num_ranges == 0)
1116 return true;
1117
1118 signop sign1 = TYPE_SIGN (type ());
1119 signop sign2 = TYPE_SIGN (other.type ());
1120
1121 for (unsigned i = 0; i < m_num_ranges; ++i)
1122 {
1123 widest_int lb = widest_int::from (lower_bound (i), sign1);
1124 widest_int ub = widest_int::from (upper_bound (i), sign1);
1125 widest_int lb_other = widest_int::from (other.lower_bound (i), sign2);
1126 widest_int ub_other = widest_int::from (other.upper_bound (i), sign2);
1127 if (lb != lb_other || ub != ub_other)
1128 return false;
1129 }
1130
1131 irange_bitmask bm1 = get_bitmask ();
1132 irange_bitmask bm2 = other.get_bitmask ();
1133 widest_int tmp1 = widest_int::from (bm1.mask (), sign1);
1134 widest_int tmp2 = widest_int::from (bm2.mask (), sign2);
1135 if (tmp1 != tmp2)
1136 return false;
1137 if (bm1.unknown_p ())
1138 return true;
1139 tmp1 = widest_int::from (bm1.value (), sign1);
1140 tmp2 = widest_int::from (bm2.value (), sign2);
1141 return tmp1 == tmp2;
1142 }
1143
1144 /* If range is a singleton, place it in RESULT and return TRUE. */
1145
1146 bool
1147 irange::singleton_p (tree *result) const
1148 {
1149 if (num_pairs () == 1 && lower_bound () == upper_bound ())
1150 {
1151 if (result)
1152 *result = wide_int_to_tree (type (), lower_bound ());
1153 return true;
1154 }
1155 return false;
1156 }
1157
1158 bool
1159 irange::singleton_p (wide_int &w) const
1160 {
1161 if (num_pairs () == 1 && lower_bound () == upper_bound ())
1162 {
1163 w = lower_bound ();
1164 return true;
1165 }
1166 return false;
1167 }
1168
1169 /* Return 1 if CST is inside value range.
1170 0 if CST is not inside value range.
1171
1172 Benchmark compile/20001226-1.c compilation time after changing this
1173 function. */
1174
1175 bool
1176 irange::contains_p (const wide_int &cst) const
1177 {
1178 if (undefined_p ())
1179 return false;
1180
1181 // See if we can exclude CST based on the known 0 bits.
1182 if (!m_bitmask.unknown_p ()
1183 && cst != 0
1184 && wi::bit_and (m_bitmask.get_nonzero_bits (), cst) == 0)
1185 return false;
1186
1187 signop sign = TYPE_SIGN (type ());
1188 for (unsigned r = 0; r < m_num_ranges; ++r)
1189 {
1190 if (wi::lt_p (cst, lower_bound (r), sign))
1191 return false;
1192 if (wi::le_p (cst, upper_bound (r), sign))
1193 return true;
1194 }
1195
1196 return false;
1197 }
1198
1199 // Perform an efficient union with R when both ranges have only a single pair.
1200 // Excluded are VARYING and UNDEFINED ranges.
1201
1202 bool
1203 irange::irange_single_pair_union (const irange &r)
1204 {
1205 gcc_checking_assert (!undefined_p () && !varying_p ());
1206 gcc_checking_assert (!r.undefined_p () && !varying_p ());
1207
1208 signop sign = TYPE_SIGN (m_type);
1209 // Check if current lower bound is also the new lower bound.
1210 if (wi::le_p (m_base[0], r.m_base[0], sign))
1211 {
1212 // If current upper bound is new upper bound, we're done.
1213 if (wi::le_p (r.m_base[1], m_base[1], sign))
1214 return union_bitmask (r);
1215 // Otherwise R has the new upper bound.
1216 // Check for overlap/touching ranges, or single target range.
1217 if (m_max_ranges == 1
1218 || (widest_int::from (m_base[1], sign) + 1
1219 >= widest_int::from (r.m_base[0], TYPE_SIGN (r.m_type))))
1220 m_base[1] = r.m_base[1];
1221 else
1222 {
1223 // This is a dual range result.
1224 m_base[2] = r.m_base[0];
1225 m_base[3] = r.m_base[1];
1226 m_num_ranges = 2;
1227 }
1228 // The range has been altered, so normalize it even if nothing
1229 // changed in the mask.
1230 if (!union_bitmask (r))
1231 normalize_kind ();
1232 if (flag_checking)
1233 verify_range ();
1234 return true;
1235 }
1236
1237 // Set the new lower bound to R's lower bound.
1238 wide_int lb = m_base[0];
1239 m_base[0] = r.m_base[0];
1240
1241 // If R fully contains THIS range, just set the upper bound.
1242 if (wi::ge_p (r.m_base[1], m_base[1], sign))
1243 m_base[1] = r.m_base[1];
1244 // Check for overlapping ranges, or target limited to a single range.
1245 else if (m_max_ranges == 1
1246 || (widest_int::from (r.m_base[1], TYPE_SIGN (r.m_type)) + 1
1247 >= widest_int::from (lb, sign)))
1248 ;
1249 else
1250 {
1251 // Left with 2 pairs.
1252 m_num_ranges = 2;
1253 m_base[2] = lb;
1254 m_base[3] = m_base[1];
1255 m_base[1] = r.m_base[1];
1256 }
1257 // The range has been altered, so normalize it even if nothing
1258 // changed in the mask.
1259 if (!union_bitmask (r))
1260 normalize_kind ();
1261 if (flag_checking)
1262 verify_range ();
1263 return true;
1264 }
1265
1266 // Return TRUE if anything changes.
1267
1268 bool
1269 irange::union_ (const vrange &v)
1270 {
1271 const irange &r = as_a <irange> (v);
1272
1273 if (r.undefined_p ())
1274 return false;
1275
1276 if (undefined_p ())
1277 {
1278 operator= (r);
1279 if (flag_checking)
1280 verify_range ();
1281 return true;
1282 }
1283
1284 if (varying_p ())
1285 return false;
1286
1287 if (r.varying_p ())
1288 {
1289 set_varying (type ());
1290 return true;
1291 }
1292
1293 // Special case one range union one range.
1294 if (m_num_ranges == 1 && r.m_num_ranges == 1)
1295 return irange_single_pair_union (r);
1296
1297 // If this ranges fully contains R, then we need do nothing.
1298 if (irange_contains_p (r))
1299 return union_bitmask (r);
1300
1301 // Do not worry about merging and such by reserving twice as many
1302 // pairs as needed, and then simply sort the 2 ranges into this
1303 // intermediate form.
1304 //
1305 // The intermediate result will have the property that the beginning
1306 // of each range is <= the beginning of the next range. There may
1307 // be overlapping ranges at this point. I.e. this would be valid
1308 // [-20, 10], [-10, 0], [0, 20], [40, 90] as it satisfies this
1309 // constraint : -20 < -10 < 0 < 40. When the range is rebuilt into r,
1310 // the merge is performed.
1311 //
1312 // [Xi,Yi]..[Xn,Yn] U [Xj,Yj]..[Xm,Ym] --> [Xk,Yk]..[Xp,Yp]
1313 auto_vec<wide_int, 20> res (m_num_ranges * 2 + r.m_num_ranges * 2);
1314 unsigned i = 0, j = 0, k = 0;
1315 signop sign = TYPE_SIGN (m_type);
1316
1317 while (i < m_num_ranges * 2 && j < r.m_num_ranges * 2)
1318 {
1319 // lower of Xi and Xj is the lowest point.
1320 if (widest_int::from (m_base[i], sign)
1321 <= widest_int::from (r.m_base[j], sign))
1322 {
1323 res.quick_push (m_base[i]);
1324 res.quick_push (m_base[i + 1]);
1325 k += 2;
1326 i += 2;
1327 }
1328 else
1329 {
1330 res.quick_push (r.m_base[j]);
1331 res.quick_push (r.m_base[j + 1]);
1332 k += 2;
1333 j += 2;
1334 }
1335 }
1336 for ( ; i < m_num_ranges * 2; i += 2)
1337 {
1338 res.quick_push (m_base[i]);
1339 res.quick_push (m_base[i + 1]);
1340 k += 2;
1341 }
1342 for ( ; j < r.m_num_ranges * 2; j += 2)
1343 {
1344 res.quick_push (r.m_base[j]);
1345 res.quick_push (r.m_base[j + 1]);
1346 k += 2;
1347 }
1348
1349 // Now normalize the vector removing any overlaps.
1350 i = 2;
1351 for (j = 2; j < k ; j += 2)
1352 {
1353 // Current upper+1 is >= lower bound next pair, then we merge ranges.
1354 if (widest_int::from (res[i - 1], sign) + 1
1355 >= widest_int::from (res[j], sign))
1356 {
1357 // New upper bounds is greater of current or the next one.
1358 if (widest_int::from (res[j + 1], sign)
1359 > widest_int::from (res[i - 1], sign))
1360 res[i - 1] = res[j + 1];
1361 }
1362 else
1363 {
1364 // This is a new distinct range, but no point in copying it
1365 // if it is already in the right place.
1366 if (i != j)
1367 {
1368 res[i++] = res[j];
1369 res[i++] = res[j + 1];
1370 }
1371 else
1372 i += 2;
1373 }
1374 }
1375
1376 // At this point, the vector should have i ranges, none overlapping.
1377 // Now it simply needs to be copied, and if there are too many
1378 // ranges, merge some. We wont do any analysis as to what the
1379 // "best" merges are, simply combine the final ranges into one.
1380 maybe_resize (i / 2);
1381 if (i > m_max_ranges * 2)
1382 {
1383 res[m_max_ranges * 2 - 1] = res[i - 1];
1384 i = m_max_ranges * 2;
1385 }
1386
1387 for (j = 0; j < i ; j++)
1388 m_base[j] = res [j];
1389 m_num_ranges = i / 2;
1390
1391 m_kind = VR_RANGE;
1392 // The range has been altered, so normalize it even if nothing
1393 // changed in the mask.
1394 if (!union_bitmask (r))
1395 normalize_kind ();
1396 if (flag_checking)
1397 verify_range ();
1398 return true;
1399 }
1400
1401 // Return TRUE if THIS fully contains R. No undefined or varying cases.
1402
1403 bool
1404 irange::irange_contains_p (const irange &r) const
1405 {
1406 gcc_checking_assert (!undefined_p () && !varying_p ());
1407 gcc_checking_assert (!r.undefined_p () && !varying_p ());
1408
1409 // In order for THIS to fully contain R, all of the pairs within R must
1410 // be fully contained by the pairs in this object.
1411 signop sign = TYPE_SIGN (m_type);
1412 unsigned ri = 0;
1413 unsigned i = 0;
1414 wide_int rl = r.m_base[0];
1415 wide_int ru = r.m_base[1];
1416 wide_int l = m_base[0];
1417 wide_int u = m_base[1];
1418 while (1)
1419 {
1420 // If r is contained within this range, move to the next R
1421 if (wi::ge_p (rl, l, sign)
1422 && wi::le_p (ru, u, sign))
1423 {
1424 // This pair is OK, Either done, or bump to the next.
1425 if (++ri >= r.num_pairs ())
1426 return true;
1427 rl = r.m_base[ri * 2];
1428 ru = r.m_base[ri * 2 + 1];
1429 continue;
1430 }
1431 // Otherwise, check if this's pair occurs before R's.
1432 if (wi::lt_p (u, rl, sign))
1433 {
1434 // There's still at least one pair of R left.
1435 if (++i >= num_pairs ())
1436 return false;
1437 l = m_base[i * 2];
1438 u = m_base[i * 2 + 1];
1439 continue;
1440 }
1441 return false;
1442 }
1443 return false;
1444 }
1445
1446
1447 // Return TRUE if anything changes.
1448
1449 bool
1450 irange::intersect (const vrange &v)
1451 {
1452 const irange &r = as_a <irange> (v);
1453 gcc_checking_assert (undefined_p () || r.undefined_p ()
1454 || range_compatible_p (type (), r.type ()));
1455
1456 if (undefined_p ())
1457 return false;
1458 if (r.undefined_p ())
1459 {
1460 set_undefined ();
1461 return true;
1462 }
1463 if (r.varying_p ())
1464 return false;
1465 if (varying_p ())
1466 {
1467 operator= (r);
1468 return true;
1469 }
1470
1471 if (r.num_pairs () == 1)
1472 {
1473 bool res = intersect (r.lower_bound (), r.upper_bound ());
1474 if (undefined_p ())
1475 return true;
1476
1477 res |= intersect_bitmask (r);
1478 if (res)
1479 normalize_kind ();
1480 return res;
1481 }
1482
1483 // If R fully contains this, then intersection will change nothing.
1484 if (r.irange_contains_p (*this))
1485 return intersect_bitmask (r);
1486
1487 // ?? We could probably come up with something smarter than the
1488 // worst case scenario here.
1489 int needed = num_pairs () + r.num_pairs ();
1490 maybe_resize (needed);
1491
1492 signop sign = TYPE_SIGN (m_type);
1493 unsigned bld_pair = 0;
1494 unsigned bld_lim = m_max_ranges;
1495 int_range_max r2 (*this);
1496 unsigned r2_lim = r2.num_pairs ();
1497 unsigned i2 = 0;
1498 for (unsigned i = 0; i < r.num_pairs (); )
1499 {
1500 // If r1's upper is < r2's lower, we can skip r1's pair.
1501 wide_int ru = r.m_base[i * 2 + 1];
1502 wide_int r2l = r2.m_base[i2 * 2];
1503 if (wi::lt_p (ru, r2l, sign))
1504 {
1505 i++;
1506 continue;
1507 }
1508 // Likewise, skip r2's pair if its excluded.
1509 wide_int r2u = r2.m_base[i2 * 2 + 1];
1510 wide_int rl = r.m_base[i * 2];
1511 if (wi::lt_p (r2u, rl, sign))
1512 {
1513 i2++;
1514 if (i2 < r2_lim)
1515 continue;
1516 // No more r2, break.
1517 break;
1518 }
1519
1520 // Must be some overlap. Find the highest of the lower bounds,
1521 // and set it, unless the build limits lower bounds is already
1522 // set.
1523 if (bld_pair < bld_lim)
1524 {
1525 if (wi::ge_p (rl, r2l, sign))
1526 m_base[bld_pair * 2] = rl;
1527 else
1528 m_base[bld_pair * 2] = r2l;
1529 }
1530 else
1531 // Decrease and set a new upper.
1532 bld_pair--;
1533
1534 // ...and choose the lower of the upper bounds.
1535 if (wi::le_p (ru, r2u, sign))
1536 {
1537 m_base[bld_pair * 2 + 1] = ru;
1538 bld_pair++;
1539 // Move past the r1 pair and keep trying.
1540 i++;
1541 continue;
1542 }
1543 else
1544 {
1545 m_base[bld_pair * 2 + 1] = r2u;
1546 bld_pair++;
1547 i2++;
1548 if (i2 < r2_lim)
1549 continue;
1550 // No more r2, break.
1551 break;
1552 }
1553 // r2 has the higher lower bound.
1554 }
1555
1556 // At the exit of this loop, it is one of 2 things:
1557 // ran out of r1, or r2, but either means we are done.
1558 m_num_ranges = bld_pair;
1559 if (m_num_ranges == 0)
1560 {
1561 set_undefined ();
1562 return true;
1563 }
1564
1565 m_kind = VR_RANGE;
1566 // The range has been altered, so normalize it even if nothing
1567 // changed in the mask.
1568 if (!intersect_bitmask (r))
1569 normalize_kind ();
1570 if (flag_checking)
1571 verify_range ();
1572 return true;
1573 }
1574
1575
1576 // Multirange intersect for a specified wide_int [lb, ub] range.
1577 // Return TRUE if intersect changed anything.
1578 //
1579 // NOTE: It is the caller's responsibility to intersect the mask.
1580
1581 bool
1582 irange::intersect (const wide_int& lb, const wide_int& ub)
1583 {
1584 // Undefined remains undefined.
1585 if (undefined_p ())
1586 return false;
1587
1588 tree range_type = type();
1589 signop sign = TYPE_SIGN (range_type);
1590
1591 gcc_checking_assert (TYPE_PRECISION (range_type) == wi::get_precision (lb));
1592 gcc_checking_assert (TYPE_PRECISION (range_type) == wi::get_precision (ub));
1593
1594 // If this range is fully contained, then intersection will do nothing.
1595 if (wi::ge_p (lower_bound (), lb, sign)
1596 && wi::le_p (upper_bound (), ub, sign))
1597 return false;
1598
1599 unsigned bld_index = 0;
1600 unsigned pair_lim = num_pairs ();
1601 for (unsigned i = 0; i < pair_lim; i++)
1602 {
1603 wide_int pairl = m_base[i * 2];
1604 wide_int pairu = m_base[i * 2 + 1];
1605 // Once UB is less than a pairs lower bound, we're done.
1606 if (wi::lt_p (ub, pairl, sign))
1607 break;
1608 // if LB is greater than this pairs upper, this pair is excluded.
1609 if (wi::lt_p (pairu, lb, sign))
1610 continue;
1611
1612 // Must be some overlap. Find the highest of the lower bounds,
1613 // and set it
1614 if (wi::gt_p (lb, pairl, sign))
1615 m_base[bld_index * 2] = lb;
1616 else
1617 m_base[bld_index * 2] = pairl;
1618
1619 // ...and choose the lower of the upper bounds and if the base pair
1620 // has the lower upper bound, need to check next pair too.
1621 if (wi::lt_p (ub, pairu, sign))
1622 {
1623 m_base[bld_index++ * 2 + 1] = ub;
1624 break;
1625 }
1626 else
1627 m_base[bld_index++ * 2 + 1] = pairu;
1628 }
1629
1630 m_num_ranges = bld_index;
1631 if (m_num_ranges == 0)
1632 {
1633 set_undefined ();
1634 return true;
1635 }
1636
1637 m_kind = VR_RANGE;
1638 // The caller must normalize and verify the range, as the bitmask
1639 // still needs to be handled.
1640 return true;
1641 }
1642
1643
1644 // Signed 1-bits are strange. You can't subtract 1, because you can't
1645 // represent the number 1. This works around that for the invert routine.
1646
1647 static wide_int inline
1648 subtract_one (const wide_int &x, tree type, wi::overflow_type &overflow)
1649 {
1650 if (TYPE_SIGN (type) == SIGNED)
1651 return wi::add (x, -1, SIGNED, &overflow);
1652 else
1653 return wi::sub (x, 1, UNSIGNED, &overflow);
1654 }
1655
1656 // The analogous function for adding 1.
1657
1658 static wide_int inline
1659 add_one (const wide_int &x, tree type, wi::overflow_type &overflow)
1660 {
1661 if (TYPE_SIGN (type) == SIGNED)
1662 return wi::sub (x, -1, SIGNED, &overflow);
1663 else
1664 return wi::add (x, 1, UNSIGNED, &overflow);
1665 }
1666
1667 // Return the inverse of a range.
1668
1669 void
1670 irange::invert ()
1671 {
1672 gcc_checking_assert (!undefined_p () && !varying_p ());
1673
1674 // We always need one more set of bounds to represent an inverse, so
1675 // if we're at the limit, we can't properly represent things.
1676 //
1677 // For instance, to represent the inverse of a 2 sub-range set
1678 // [5, 10][20, 30], we would need a 3 sub-range set
1679 // [-MIN, 4][11, 19][31, MAX].
1680 //
1681 // In this case, return the most conservative thing.
1682 //
1683 // However, if any of the extremes of the range are -MIN/+MAX, we
1684 // know we will not need an extra bound. For example:
1685 //
1686 // INVERT([-MIN,20][30,40]) => [21,29][41,+MAX]
1687 // INVERT([-MIN,20][30,MAX]) => [21,29]
1688 tree ttype = type ();
1689 unsigned prec = TYPE_PRECISION (ttype);
1690 signop sign = TYPE_SIGN (ttype);
1691 wide_int type_min = wi::min_value (prec, sign);
1692 wide_int type_max = wi::max_value (prec, sign);
1693 m_bitmask.set_unknown (prec);
1694
1695 // At this point, we need one extra sub-range to represent the
1696 // inverse.
1697 maybe_resize (m_num_ranges + 1);
1698
1699 // The algorithm is as follows. To calculate INVERT ([a,b][c,d]), we
1700 // generate [-MIN, a-1][b+1, c-1][d+1, MAX].
1701 //
1702 // If there is an over/underflow in the calculation for any
1703 // sub-range, we eliminate that subrange. This allows us to easily
1704 // calculate INVERT([-MIN, 5]) with: [-MIN, -MIN-1][6, MAX]. And since
1705 // we eliminate the underflow, only [6, MAX] remains.
1706 unsigned i = 0;
1707 wi::overflow_type ovf;
1708 // Construct leftmost range.
1709 int_range_max orig_range (*this);
1710 unsigned nitems = 0;
1711 wide_int tmp;
1712 // If this is going to underflow on the MINUS 1, don't even bother
1713 // checking. This also handles subtracting one from an unsigned 0,
1714 // which doesn't set the underflow bit.
1715 if (type_min != orig_range.lower_bound ())
1716 {
1717 m_base[nitems++] = type_min;
1718 tmp = subtract_one (orig_range.lower_bound (), ttype, ovf);
1719 m_base[nitems++] = tmp;
1720 if (ovf)
1721 nitems = 0;
1722 }
1723 i++;
1724 // Construct middle ranges if applicable.
1725 if (orig_range.num_pairs () > 1)
1726 {
1727 unsigned j = i;
1728 for (; j < (orig_range.num_pairs () * 2) - 1; j += 2)
1729 {
1730 // The middle ranges cannot have MAX/MIN, so there's no need
1731 // to check for unsigned overflow on the +1 and -1 here.
1732 tmp = wi::add (orig_range.m_base[j], 1, sign, &ovf);
1733 m_base[nitems++] = tmp;
1734 tmp = subtract_one (orig_range.m_base[j + 1], ttype, ovf);
1735 m_base[nitems++] = tmp;
1736 if (ovf)
1737 nitems -= 2;
1738 }
1739 i = j;
1740 }
1741 // Construct rightmost range.
1742 //
1743 // However, if this will overflow on the PLUS 1, don't even bother.
1744 // This also handles adding one to an unsigned MAX, which doesn't
1745 // set the overflow bit.
1746 if (type_max != orig_range.m_base[i])
1747 {
1748 tmp = add_one (orig_range.m_base[i], ttype, ovf);
1749 m_base[nitems++] = tmp;
1750 m_base[nitems++] = type_max;
1751 if (ovf)
1752 nitems -= 2;
1753 }
1754 m_num_ranges = nitems / 2;
1755
1756 // We disallow undefined or varying coming in, so the result can
1757 // only be a VR_RANGE.
1758 gcc_checking_assert (m_kind == VR_RANGE);
1759
1760 if (flag_checking)
1761 verify_range ();
1762 }
1763
1764 // Return the bitmask inherent in the range.
1765
1766 irange_bitmask
1767 irange::get_bitmask_from_range () const
1768 {
1769 unsigned prec = TYPE_PRECISION (type ());
1770 wide_int min = lower_bound ();
1771 wide_int max = upper_bound ();
1772
1773 // All the bits of a singleton are known.
1774 if (min == max)
1775 {
1776 wide_int mask = wi::zero (prec);
1777 wide_int value = lower_bound ();
1778 return irange_bitmask (value, mask);
1779 }
1780
1781 wide_int xorv = min ^ max;
1782
1783 if (xorv != 0)
1784 xorv = wi::mask (prec - wi::clz (xorv), false, prec);
1785
1786 return irange_bitmask (wi::zero (prec), min | xorv);
1787 }
1788
1789 // If the the mask can be trivially converted to a range, do so and
1790 // return TRUE.
1791
1792 bool
1793 irange::set_range_from_bitmask ()
1794 {
1795 gcc_checking_assert (!undefined_p ());
1796 if (m_bitmask.unknown_p ())
1797 return false;
1798
1799 // If all the bits are known, this is a singleton.
1800 if (m_bitmask.mask () == 0)
1801 {
1802 set (m_type, m_bitmask.value (), m_bitmask.value ());
1803 return true;
1804 }
1805
1806 unsigned popcount = wi::popcount (m_bitmask.get_nonzero_bits ());
1807
1808 // If we have only one bit set in the mask, we can figure out the
1809 // range immediately.
1810 if (popcount == 1)
1811 {
1812 // Make sure we don't pessimize the range.
1813 if (!contains_p (m_bitmask.get_nonzero_bits ()))
1814 return false;
1815
1816 bool has_zero = contains_zero_p (*this);
1817 wide_int nz = m_bitmask.get_nonzero_bits ();
1818 set (m_type, nz, nz);
1819 m_bitmask.set_nonzero_bits (nz);
1820 if (has_zero)
1821 {
1822 int_range<2> zero;
1823 zero.set_zero (type ());
1824 union_ (zero);
1825 }
1826 if (flag_checking)
1827 verify_range ();
1828 return true;
1829 }
1830 else if (popcount == 0)
1831 {
1832 set_zero (type ());
1833 return true;
1834 }
1835 return false;
1836 }
1837
1838 void
1839 irange::update_bitmask (const irange_bitmask &bm)
1840 {
1841 gcc_checking_assert (!undefined_p ());
1842
1843 // Drop VARYINGs with known bits to a plain range.
1844 if (m_kind == VR_VARYING && !bm.unknown_p ())
1845 m_kind = VR_RANGE;
1846
1847 m_bitmask = bm;
1848 if (!set_range_from_bitmask ())
1849 normalize_kind ();
1850 if (flag_checking)
1851 verify_range ();
1852 }
1853
1854 // Return the bitmask of known bits that includes the bitmask inherent
1855 // in the range.
1856
1857 irange_bitmask
1858 irange::get_bitmask () const
1859 {
1860 gcc_checking_assert (!undefined_p ());
1861
1862 // The mask inherent in the range is calculated on-demand. For
1863 // example, [0,255] does not have known bits set by default. This
1864 // saves us considerable time, because setting it at creation incurs
1865 // a large penalty for irange::set. At the time of writing there
1866 // was a 5% slowdown in VRP if we kept the mask precisely up to date
1867 // at all times. Instead, we default to -1 and set it when
1868 // explicitly requested. However, this function will always return
1869 // the correct mask.
1870 //
1871 // This also means that the mask may have a finer granularity than
1872 // the range and thus contradict it. Think of the mask as an
1873 // enhancement to the range. For example:
1874 //
1875 // [3, 1000] MASK 0xfffffffe VALUE 0x0
1876 //
1877 // 3 is in the range endpoints, but is excluded per the known 0 bits
1878 // in the mask.
1879 irange_bitmask bm = get_bitmask_from_range ();
1880 if (!m_bitmask.unknown_p ())
1881 bm.intersect (m_bitmask);
1882 return bm;
1883 }
1884
1885 // Set the nonzero bits in R into THIS. Return TRUE and
1886 // normalize the range if anything changed.
1887
1888 void
1889 irange::set_nonzero_bits (const wide_int &bits)
1890 {
1891 gcc_checking_assert (!undefined_p ());
1892 irange_bitmask bm (wi::zero (TYPE_PRECISION (type ())), bits);
1893 update_bitmask (bm);
1894 }
1895
1896 // Return the nonzero bits in R.
1897
1898 wide_int
1899 irange::get_nonzero_bits () const
1900 {
1901 gcc_checking_assert (!undefined_p ());
1902 irange_bitmask bm = get_bitmask ();
1903 return bm.value () | bm.mask ();
1904 }
1905
1906 // Intersect the bitmask in R into THIS and normalize the range.
1907 // Return TRUE if the intersection changed anything.
1908
1909 bool
1910 irange::intersect_bitmask (const irange &r)
1911 {
1912 gcc_checking_assert (!undefined_p () && !r.undefined_p ());
1913
1914 if (m_bitmask == r.m_bitmask)
1915 return false;
1916
1917 irange_bitmask bm = get_bitmask ();
1918 if (!bm.intersect (r.get_bitmask ()))
1919 return false;
1920
1921 m_bitmask = bm;
1922 if (!set_range_from_bitmask ())
1923 normalize_kind ();
1924 if (flag_checking)
1925 verify_range ();
1926 return true;
1927 }
1928
1929 // Union the bitmask in R into THIS. Return TRUE and normalize the
1930 // range if anything changed.
1931
1932 bool
1933 irange::union_bitmask (const irange &r)
1934 {
1935 gcc_checking_assert (!undefined_p () && !r.undefined_p ());
1936
1937 if (m_bitmask == r.m_bitmask)
1938 return false;
1939
1940 irange_bitmask bm = get_bitmask ();
1941 if (!bm.union_ (r.get_bitmask ()))
1942 return false;
1943
1944 m_bitmask = bm;
1945 // No need to call set_range_from_mask, because we'll never
1946 // narrow the range. Besides, it would cause endless recursion
1947 // because of the union_ in set_range_from_mask.
1948 normalize_kind ();
1949 return true;
1950 }
1951
1952 void
1953 irange_bitmask::verify_mask () const
1954 {
1955 gcc_assert (m_value.get_precision () == m_mask.get_precision ());
1956 // Unknown bits must have their corresponding value bits cleared as
1957 // it simplifies union and intersect.
1958 gcc_assert (wi::bit_and (m_mask, m_value) == 0);
1959 }
1960
1961 void
1962 dump_value_range (FILE *file, const vrange *vr)
1963 {
1964 vr->dump (file);
1965 }
1966
1967 DEBUG_FUNCTION void
1968 debug (const vrange *vr)
1969 {
1970 dump_value_range (stderr, vr);
1971 fprintf (stderr, "\n");
1972 }
1973
1974 DEBUG_FUNCTION void
1975 debug (const vrange &vr)
1976 {
1977 debug (&vr);
1978 }
1979
1980 DEBUG_FUNCTION void
1981 debug (const value_range *vr)
1982 {
1983 dump_value_range (stderr, vr);
1984 fprintf (stderr, "\n");
1985 }
1986
1987 DEBUG_FUNCTION void
1988 debug (const value_range &vr)
1989 {
1990 dump_value_range (stderr, &vr);
1991 fprintf (stderr, "\n");
1992 }
1993
1994 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
1995
1996 bool
1997 vrp_operand_equal_p (const_tree val1, const_tree val2)
1998 {
1999 if (val1 == val2)
2000 return true;
2001 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
2002 return false;
2003 return true;
2004 }
2005
2006 void
2007 gt_ggc_mx (irange *x)
2008 {
2009 if (!x->undefined_p ())
2010 gt_ggc_mx (x->m_type);
2011 }
2012
2013 void
2014 gt_pch_nx (irange *x)
2015 {
2016 if (!x->undefined_p ())
2017 gt_pch_nx (x->m_type);
2018 }
2019
2020 void
2021 gt_pch_nx (irange *x, gt_pointer_operator op, void *cookie)
2022 {
2023 for (unsigned i = 0; i < x->m_num_ranges; ++i)
2024 {
2025 op (&x->m_base[i * 2], NULL, cookie);
2026 op (&x->m_base[i * 2 + 1], NULL, cookie);
2027 }
2028 }
2029
2030 void
2031 gt_ggc_mx (frange *x)
2032 {
2033 gt_ggc_mx (x->m_type);
2034 }
2035
2036 void
2037 gt_pch_nx (frange *x)
2038 {
2039 gt_pch_nx (x->m_type);
2040 }
2041
2042 void
2043 gt_pch_nx (frange *x, gt_pointer_operator op, void *cookie)
2044 {
2045 op (&x->m_type, NULL, cookie);
2046 }
2047
2048 void
2049 gt_ggc_mx (vrange *x)
2050 {
2051 if (is_a <irange> (*x))
2052 return gt_ggc_mx ((irange *) x);
2053 if (is_a <frange> (*x))
2054 return gt_ggc_mx ((frange *) x);
2055 gcc_unreachable ();
2056 }
2057
2058 void
2059 gt_pch_nx (vrange *x)
2060 {
2061 if (is_a <irange> (*x))
2062 return gt_pch_nx ((irange *) x);
2063 if (is_a <frange> (*x))
2064 return gt_pch_nx ((frange *) x);
2065 gcc_unreachable ();
2066 }
2067
2068 void
2069 gt_pch_nx (vrange *x, gt_pointer_operator op, void *cookie)
2070 {
2071 if (is_a <irange> (*x))
2072 gt_pch_nx ((irange *) x, op, cookie);
2073 else if (is_a <frange> (*x))
2074 gt_pch_nx ((frange *) x, op, cookie);
2075 else
2076 gcc_unreachable ();
2077 }
2078
2079 #define DEFINE_INT_RANGE_INSTANCE(N) \
2080 template int_range<N>::int_range(tree_node *, \
2081 const wide_int &, \
2082 const wide_int &, \
2083 value_range_kind); \
2084 template int_range<N>::int_range(tree); \
2085 template int_range<N>::int_range(const irange &); \
2086 template int_range<N>::int_range(const int_range &); \
2087 template int_range<N>& int_range<N>::operator= (const int_range &);
2088
2089 DEFINE_INT_RANGE_INSTANCE(1)
2090 DEFINE_INT_RANGE_INSTANCE(2)
2091 DEFINE_INT_RANGE_INSTANCE(3)
2092 DEFINE_INT_RANGE_INSTANCE(255)
2093
2094 #if CHECKING_P
2095 #include "selftest.h"
2096
2097 #define INT(x) wi::shwi ((x), TYPE_PRECISION (integer_type_node))
2098 #define UINT(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_type_node))
2099 #define SCHAR(x) wi::shwi ((x), TYPE_PRECISION (signed_char_type_node))
2100
2101 namespace selftest
2102 {
2103
2104 static int_range<2>
2105 range (tree type, int a, int b, value_range_kind kind = VR_RANGE)
2106 {
2107 wide_int w1, w2;
2108 if (TYPE_UNSIGNED (type))
2109 {
2110 w1 = wi::uhwi (a, TYPE_PRECISION (type));
2111 w2 = wi::uhwi (b, TYPE_PRECISION (type));
2112 }
2113 else
2114 {
2115 w1 = wi::shwi (a, TYPE_PRECISION (type));
2116 w2 = wi::shwi (b, TYPE_PRECISION (type));
2117 }
2118 return int_range<2> (type, w1, w2, kind);
2119 }
2120
2121 static int_range<2>
2122 range_int (int a, int b, value_range_kind kind = VR_RANGE)
2123 {
2124 return range (integer_type_node, a, b, kind);
2125 }
2126
2127 static int_range<2>
2128 range_uint (int a, int b, value_range_kind kind = VR_RANGE)
2129 {
2130 return range (unsigned_type_node, a, b, kind);
2131 }
2132
2133 static int_range<2>
2134 range_uint128 (int a, int b, value_range_kind kind = VR_RANGE)
2135 {
2136 tree u128_type_node = build_nonstandard_integer_type (128, 1);
2137 return range (u128_type_node, a, b, kind);
2138 }
2139
2140 static int_range<2>
2141 range_uchar (int a, int b, value_range_kind kind = VR_RANGE)
2142 {
2143 return range (unsigned_char_type_node, a, b, kind);
2144 }
2145
2146 static int_range<2>
2147 range_char (int a, int b, value_range_kind kind = VR_RANGE)
2148 {
2149 return range (signed_char_type_node, a, b, kind);
2150 }
2151
2152 static int_range<3>
2153 build_range3 (int a, int b, int c, int d, int e, int f)
2154 {
2155 int_range<3> i1 = range_int (a, b);
2156 int_range<3> i2 = range_int (c, d);
2157 int_range<3> i3 = range_int (e, f);
2158 i1.union_ (i2);
2159 i1.union_ (i3);
2160 return i1;
2161 }
2162
2163 static void
2164 range_tests_irange3 ()
2165 {
2166 int_range<3> r0, r1, r2;
2167 int_range<3> i1, i2, i3;
2168
2169 // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20].
2170 r0 = range_int (10, 20);
2171 r1 = range_int (5, 8);
2172 r0.union_ (r1);
2173 r1 = range_int (1, 3);
2174 r0.union_ (r1);
2175 ASSERT_TRUE (r0 == build_range3 (1, 3, 5, 8, 10, 20));
2176
2177 // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20].
2178 r1 = range_int (-5, 0);
2179 r0.union_ (r1);
2180 ASSERT_TRUE (r0 == build_range3 (-5, 3, 5, 8, 10, 20));
2181
2182 // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60].
2183 r1 = range_int (50, 60);
2184 r0 = range_int (10, 20);
2185 r0.union_ (range_int (30, 40));
2186 r0.union_ (r1);
2187 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
2188 // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80].
2189 r1 = range_int (70, 80);
2190 r0.union_ (r1);
2191
2192 r2 = build_range3 (10, 20, 30, 40, 50, 60);
2193 r2.union_ (range_int (70, 80));
2194 ASSERT_TRUE (r0 == r2);
2195
2196 // [10,20][30,40][50,60] U [6,35] => [6,40][50,60].
2197 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2198 r1 = range_int (6, 35);
2199 r0.union_ (r1);
2200 r1 = range_int (6, 40);
2201 r1.union_ (range_int (50, 60));
2202 ASSERT_TRUE (r0 == r1);
2203
2204 // [10,20][30,40][50,60] U [6,60] => [6,60].
2205 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2206 r1 = range_int (6, 60);
2207 r0.union_ (r1);
2208 ASSERT_TRUE (r0 == range_int (6, 60));
2209
2210 // [10,20][30,40][50,60] U [6,70] => [6,70].
2211 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2212 r1 = range_int (6, 70);
2213 r0.union_ (r1);
2214 ASSERT_TRUE (r0 == range_int (6, 70));
2215
2216 // [10,20][30,40][50,60] U [35,70] => [10,20][30,70].
2217 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2218 r1 = range_int (35, 70);
2219 r0.union_ (r1);
2220 r1 = range_int (10, 20);
2221 r1.union_ (range_int (30, 70));
2222 ASSERT_TRUE (r0 == r1);
2223
2224 // [10,20][30,40][50,60] U [15,35] => [10,40][50,60].
2225 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2226 r1 = range_int (15, 35);
2227 r0.union_ (r1);
2228 r1 = range_int (10, 40);
2229 r1.union_ (range_int (50, 60));
2230 ASSERT_TRUE (r0 == r1);
2231
2232 // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60].
2233 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2234 r1 = range_int (35, 35);
2235 r0.union_ (r1);
2236 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
2237 }
2238
2239 static void
2240 range_tests_int_range_max ()
2241 {
2242 int_range_max big;
2243 unsigned int nrange;
2244
2245 // Build a huge multi-range range.
2246 for (nrange = 0; nrange < 50; ++nrange)
2247 {
2248 int_range<1> tmp = range_int (nrange*10, nrange *10 + 5);
2249 big.union_ (tmp);
2250 }
2251 ASSERT_TRUE (big.num_pairs () == nrange);
2252
2253 // Verify that we can copy it without loosing precision.
2254 int_range_max copy (big);
2255 ASSERT_TRUE (copy.num_pairs () == nrange);
2256
2257 // Inverting it should produce one more sub-range.
2258 big.invert ();
2259 ASSERT_TRUE (big.num_pairs () == nrange + 1);
2260
2261 int_range<1> tmp = range_int (5, 37);
2262 big.intersect (tmp);
2263 ASSERT_TRUE (big.num_pairs () == 4);
2264
2265 // Test that [10,10][20,20] does NOT contain 15.
2266 {
2267 int_range_max i1 = range_int (10, 10);
2268 int_range_max i2 = range_int (20, 20);
2269 i1.union_ (i2);
2270 ASSERT_FALSE (i1.contains_p (INT (15)));
2271 }
2272 }
2273
2274 // Simulate -fstrict-enums where the domain of a type is less than the
2275 // underlying type.
2276
2277 static void
2278 range_tests_strict_enum ()
2279 {
2280 // The enum can only hold [0, 3].
2281 tree rtype = copy_node (unsigned_type_node);
2282 TYPE_MIN_VALUE (rtype) = build_int_cstu (rtype, 0);
2283 TYPE_MAX_VALUE (rtype) = build_int_cstu (rtype, 3);
2284
2285 // Test that even though vr1 covers the strict enum domain ([0, 3]),
2286 // it does not cover the domain of the underlying type.
2287 int_range<1> vr1 = range (rtype, 0, 1);
2288 int_range<1> vr2 = range (rtype, 2, 3);
2289 vr1.union_ (vr2);
2290 ASSERT_TRUE (vr1 == range (rtype, 0, 3));
2291 ASSERT_FALSE (vr1.varying_p ());
2292
2293 // Test that copying to a multi-range does not change things.
2294 int_range<2> ir1 (vr1);
2295 ASSERT_TRUE (ir1 == vr1);
2296 ASSERT_FALSE (ir1.varying_p ());
2297
2298 // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3].
2299 vr1 = int_range<2> (rtype,
2300 wi::to_wide (TYPE_MIN_VALUE (rtype)),
2301 wi::to_wide (TYPE_MAX_VALUE (rtype)));
2302 ir1 = vr1;
2303 ASSERT_TRUE (ir1 == vr1);
2304 ASSERT_FALSE (ir1.varying_p ());
2305 }
2306
2307 static void
2308 range_tests_misc ()
2309 {
2310 tree u128_type = build_nonstandard_integer_type (128, /*unsigned=*/1);
2311 int_range<2> i1, i2, i3;
2312 int_range<2> r0, r1, rold;
2313
2314 // Test 1-bit signed integer union.
2315 // [-1,-1] U [0,0] = VARYING.
2316 tree one_bit_type = build_nonstandard_integer_type (1, 0);
2317 wide_int one_bit_min = irange_val_min (one_bit_type);
2318 wide_int one_bit_max = irange_val_max (one_bit_type);
2319 {
2320 int_range<2> min = int_range<2> (one_bit_type, one_bit_min, one_bit_min);
2321 int_range<2> max = int_range<2> (one_bit_type, one_bit_max, one_bit_max);
2322 max.union_ (min);
2323 ASSERT_TRUE (max.varying_p ());
2324 }
2325 // Test that we can set a range of true+false for a 1-bit signed int.
2326 r0 = range_true_and_false (one_bit_type);
2327
2328 // Test inversion of 1-bit signed integers.
2329 {
2330 int_range<2> min = int_range<2> (one_bit_type, one_bit_min, one_bit_min);
2331 int_range<2> max = int_range<2> (one_bit_type, one_bit_max, one_bit_max);
2332 int_range<2> t;
2333 t = min;
2334 t.invert ();
2335 ASSERT_TRUE (t == max);
2336 t = max;
2337 t.invert ();
2338 ASSERT_TRUE (t == min);
2339 }
2340
2341 // Test that NOT(255) is [0..254] in 8-bit land.
2342 int_range<1> not_255 = range_uchar (255, 255, VR_ANTI_RANGE);
2343 ASSERT_TRUE (not_255 == range_uchar (0, 254));
2344
2345 // Test that NOT(0) is [1..255] in 8-bit land.
2346 int_range<2> not_zero = range_nonzero (unsigned_char_type_node);
2347 ASSERT_TRUE (not_zero == range_uchar (1, 255));
2348
2349 // Check that [0,127][0x..ffffff80,0x..ffffff]
2350 // => ~[128, 0x..ffffff7f].
2351 r0 = range_uint128 (0, 127);
2352 wide_int high = wi::minus_one (128);
2353 // low = -1 - 127 => 0x..ffffff80.
2354 wide_int low = wi::sub (high, wi::uhwi (127, 128));
2355 r1 = int_range<1> (u128_type, low, high); // [0x..ffffff80, 0x..ffffffff]
2356 // r0 = [0,127][0x..ffffff80,0x..fffffff].
2357 r0.union_ (r1);
2358 // r1 = [128, 0x..ffffff7f].
2359 r1 = int_range<1> (u128_type,
2360 wi::uhwi (128, 128),
2361 wi::sub (wi::minus_one (128), wi::uhwi (128, 128)));
2362 r0.invert ();
2363 ASSERT_TRUE (r0 == r1);
2364
2365 r0.set_varying (integer_type_node);
2366 wide_int minint = r0.lower_bound ();
2367 wide_int maxint = r0.upper_bound ();
2368
2369 r0.set_varying (short_integer_type_node);
2370
2371 r0.set_varying (unsigned_type_node);
2372 wide_int maxuint = r0.upper_bound ();
2373
2374 // Check that ~[0,5] => [6,MAX] for unsigned int.
2375 r0 = range_uint (0, 5);
2376 r0.invert ();
2377 ASSERT_TRUE (r0 == int_range<1> (unsigned_type_node,
2378 wi::uhwi (6, TYPE_PRECISION (unsigned_type_node)),
2379 maxuint));
2380
2381 // Check that ~[10,MAX] => [0,9] for unsigned int.
2382 r0 = int_range<1> (unsigned_type_node,
2383 wi::uhwi (10, TYPE_PRECISION (unsigned_type_node)),
2384 maxuint);
2385 r0.invert ();
2386 ASSERT_TRUE (r0 == range_uint (0, 9));
2387
2388 // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers.
2389 r0 = range_uint128 (0, 5, VR_ANTI_RANGE);
2390 r1 = int_range<1> (u128_type, wi::uhwi (6, 128), wi::minus_one (128));
2391 ASSERT_TRUE (r0 == r1);
2392
2393 // Check that [~5] is really [-MIN,4][6,MAX].
2394 r0 = range_int (5, 5, VR_ANTI_RANGE);
2395 r1 = int_range<1> (integer_type_node, minint, INT (4));
2396 r1.union_ (int_range<1> (integer_type_node, INT (6), maxint));
2397 ASSERT_FALSE (r1.undefined_p ());
2398 ASSERT_TRUE (r0 == r1);
2399
2400 r1 = range_int (5, 5);
2401 int_range<2> r2 (r1);
2402 ASSERT_TRUE (r1 == r2);
2403
2404 r1 = range_int (5, 10);
2405
2406 r1 = range_int (5, 10);
2407 ASSERT_TRUE (r1.contains_p (INT (7)));
2408
2409 r1 = range_char (0, 20);
2410 ASSERT_TRUE (r1.contains_p (SCHAR(15)));
2411 ASSERT_FALSE (r1.contains_p (SCHAR(300)));
2412
2413 // NOT([10,20]) ==> [-MIN,9][21,MAX].
2414 r0 = r1 = range_int (10, 20);
2415 r2 = int_range<1> (integer_type_node, minint, INT(9));
2416 r2.union_ (int_range<1> (integer_type_node, INT(21), maxint));
2417 ASSERT_FALSE (r2.undefined_p ());
2418 r1.invert ();
2419 ASSERT_TRUE (r1 == r2);
2420 // Test that NOT(NOT(x)) == x.
2421 r2.invert ();
2422 ASSERT_TRUE (r0 == r2);
2423
2424 // Test that booleans and their inverse work as expected.
2425 r0 = range_zero (boolean_type_node);
2426 ASSERT_TRUE (r0 == range_false ());
2427 r0.invert ();
2428 ASSERT_TRUE (r0 == range_true ());
2429
2430 // Make sure NULL and non-NULL of pointer types work, and that
2431 // inverses of them are consistent.
2432 tree voidp = build_pointer_type (void_type_node);
2433 r0 = range_zero (voidp);
2434 r1 = r0;
2435 r0.invert ();
2436 r0.invert ();
2437 ASSERT_TRUE (r0 == r1);
2438
2439 // [10,20] U [15, 30] => [10, 30].
2440 r0 = range_int (10, 20);
2441 r1 = range_int (15, 30);
2442 r0.union_ (r1);
2443 ASSERT_TRUE (r0 == range_int (10, 30));
2444
2445 // [15,40] U [] => [15,40].
2446 r0 = range_int (15, 40);
2447 r1.set_undefined ();
2448 r0.union_ (r1);
2449 ASSERT_TRUE (r0 == range_int (15, 40));
2450
2451 // [10,20] U [10,10] => [10,20].
2452 r0 = range_int (10, 20);
2453 r1 = range_int (10, 10);
2454 r0.union_ (r1);
2455 ASSERT_TRUE (r0 == range_int (10, 20));
2456
2457 // [10,20] U [9,9] => [9,20].
2458 r0 = range_int (10, 20);
2459 r1 = range_int (9, 9);
2460 r0.union_ (r1);
2461 ASSERT_TRUE (r0 == range_int (9, 20));
2462
2463 // [10,20] ^ [15,30] => [15,20].
2464 r0 = range_int (10, 20);
2465 r1 = range_int (15, 30);
2466 r0.intersect (r1);
2467 ASSERT_TRUE (r0 == range_int (15, 20));
2468
2469 // Test the internal sanity of wide_int's wrt HWIs.
2470 ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node),
2471 TYPE_SIGN (boolean_type_node))
2472 == wi::uhwi (1, TYPE_PRECISION (boolean_type_node)));
2473
2474 // Test zero_p().
2475 r0 = range_int (0, 0);
2476 ASSERT_TRUE (r0.zero_p ());
2477
2478 // Test nonzero_p().
2479 r0 = range_int (0, 0);
2480 r0.invert ();
2481 ASSERT_TRUE (r0.nonzero_p ());
2482
2483 // r0 = ~[1,1]
2484 r0 = range_int (1, 1, VR_ANTI_RANGE);
2485 // r1 = ~[3,3]
2486 r1 = range_int (3, 3, VR_ANTI_RANGE);
2487
2488 // vv = [0,0][2,2][4, MAX]
2489 int_range<3> vv = r0;
2490 vv.intersect (r1);
2491
2492 ASSERT_TRUE (vv.contains_p (UINT (2)));
2493 ASSERT_TRUE (vv.num_pairs () == 3);
2494
2495 r0 = range_int (1, 1);
2496 // And union it with [0,0][2,2][4,MAX] multi range
2497 r0.union_ (vv);
2498 // The result should be [0,2][4,MAX], or ~[3,3] but it must contain 2
2499 ASSERT_TRUE (r0.contains_p (INT (2)));
2500 }
2501
2502 static void
2503 range_tests_nonzero_bits ()
2504 {
2505 int_range<2> r0, r1;
2506
2507 // Adding nonzero bits to a varying drops the varying.
2508 r0.set_varying (integer_type_node);
2509 r0.set_nonzero_bits (INT (255));
2510 ASSERT_TRUE (!r0.varying_p ());
2511 // Dropping the nonzero bits brings us back to varying.
2512 r0.set_nonzero_bits (INT (-1));
2513 ASSERT_TRUE (r0.varying_p ());
2514
2515 // Test contains_p with nonzero bits.
2516 r0.set_zero (integer_type_node);
2517 ASSERT_TRUE (r0.contains_p (INT (0)));
2518 ASSERT_FALSE (r0.contains_p (INT (1)));
2519 r0.set_nonzero_bits (INT (0xfe));
2520 ASSERT_FALSE (r0.contains_p (INT (0x100)));
2521 ASSERT_FALSE (r0.contains_p (INT (0x3)));
2522
2523 // Union of nonzero bits.
2524 r0.set_varying (integer_type_node);
2525 r0.set_nonzero_bits (INT (0xf0));
2526 r1.set_varying (integer_type_node);
2527 r1.set_nonzero_bits (INT (0xf));
2528 r0.union_ (r1);
2529 ASSERT_TRUE (r0.get_nonzero_bits () == 0xff);
2530
2531 // Intersect of nonzero bits.
2532 r0 = range_int (0, 255);
2533 r0.set_nonzero_bits (INT (0xfe));
2534 r1.set_varying (integer_type_node);
2535 r1.set_nonzero_bits (INT (0xf0));
2536 r0.intersect (r1);
2537 ASSERT_TRUE (r0.get_nonzero_bits () == 0xf0);
2538
2539 // Intersect where the mask of nonzero bits is implicit from the range.
2540 r0.set_varying (integer_type_node);
2541 r1 = range_int (0, 255);
2542 r0.intersect (r1);
2543 ASSERT_TRUE (r0.get_nonzero_bits () == 0xff);
2544
2545 // The union of a mask of 0xff..ffff00 with a mask of 0xff spans the
2546 // entire domain, and makes the range a varying.
2547 r0.set_varying (integer_type_node);
2548 wide_int x = wi::shwi (0xff, TYPE_PRECISION (integer_type_node));
2549 x = wi::bit_not (x);
2550 r0.set_nonzero_bits (x); // 0xff..ff00
2551 r1.set_varying (integer_type_node);
2552 r1.set_nonzero_bits (INT (0xff));
2553 r0.union_ (r1);
2554 ASSERT_TRUE (r0.varying_p ());
2555
2556 // Test that setting a nonzero bit of 1 does not pessimize the range.
2557 r0.set_zero (integer_type_node);
2558 r0.set_nonzero_bits (INT (1));
2559 ASSERT_TRUE (r0.zero_p ());
2560 }
2561
2562 // Build an frange from string endpoints.
2563
2564 static inline frange
2565 frange_float (const char *lb, const char *ub, tree type = float_type_node)
2566 {
2567 REAL_VALUE_TYPE min, max;
2568 gcc_assert (real_from_string (&min, lb) == 0);
2569 gcc_assert (real_from_string (&max, ub) == 0);
2570 return frange (type, min, max);
2571 }
2572
2573 static void
2574 range_tests_nan ()
2575 {
2576 frange r0, r1;
2577 REAL_VALUE_TYPE q, r;
2578 bool signbit;
2579
2580 // Equal ranges but with differing NAN bits are not equal.
2581 if (HONOR_NANS (float_type_node))
2582 {
2583 r1 = frange_float ("10", "12");
2584 r0 = r1;
2585 ASSERT_EQ (r0, r1);
2586 r0.clear_nan ();
2587 ASSERT_NE (r0, r1);
2588 r0.update_nan ();
2589 ASSERT_EQ (r0, r1);
2590
2591 // [10, 20] NAN ^ [30, 40] NAN = NAN.
2592 r0 = frange_float ("10", "20");
2593 r1 = frange_float ("30", "40");
2594 r0.intersect (r1);
2595 ASSERT_TRUE (r0.known_isnan ());
2596
2597 // [3,5] U [5,10] NAN = ... NAN
2598 r0 = frange_float ("3", "5");
2599 r0.clear_nan ();
2600 r1 = frange_float ("5", "10");
2601 r0.union_ (r1);
2602 ASSERT_TRUE (r0.maybe_isnan ());
2603 }
2604
2605 // [5,6] U NAN = [5,6] NAN.
2606 r0 = frange_float ("5", "6");
2607 r0.clear_nan ();
2608 r1.set_nan (float_type_node);
2609 r0.union_ (r1);
2610 real_from_string (&q, "5");
2611 real_from_string (&r, "6");
2612 ASSERT_TRUE (real_identical (&q, &r0.lower_bound ()));
2613 ASSERT_TRUE (real_identical (&r, &r0.upper_bound ()));
2614 ASSERT_TRUE (r0.maybe_isnan ());
2615
2616 // NAN U NAN = NAN
2617 r0.set_nan (float_type_node);
2618 r1.set_nan (float_type_node);
2619 r0.union_ (r1);
2620 ASSERT_TRUE (r0.known_isnan ());
2621
2622 // [INF, INF] NAN ^ NAN = NAN
2623 r0.set_nan (float_type_node);
2624 r1 = frange_float ("+Inf", "+Inf");
2625 if (!HONOR_NANS (float_type_node))
2626 r1.update_nan ();
2627 r0.intersect (r1);
2628 ASSERT_TRUE (r0.known_isnan ());
2629
2630 // NAN ^ NAN = NAN
2631 r0.set_nan (float_type_node);
2632 r1.set_nan (float_type_node);
2633 r0.intersect (r1);
2634 ASSERT_TRUE (r0.known_isnan ());
2635
2636 // +NAN ^ -NAN = UNDEFINED
2637 r0.set_nan (float_type_node, false);
2638 r1.set_nan (float_type_node, true);
2639 r0.intersect (r1);
2640 ASSERT_TRUE (r0.undefined_p ());
2641
2642 // VARYING ^ NAN = NAN.
2643 r0.set_nan (float_type_node);
2644 r1.set_varying (float_type_node);
2645 r0.intersect (r1);
2646 ASSERT_TRUE (r0.known_isnan ());
2647
2648 // [3,4] ^ NAN = UNDEFINED.
2649 r0 = frange_float ("3", "4");
2650 r0.clear_nan ();
2651 r1.set_nan (float_type_node);
2652 r0.intersect (r1);
2653 ASSERT_TRUE (r0.undefined_p ());
2654
2655 // [-3, 5] ^ NAN = UNDEFINED
2656 r0 = frange_float ("-3", "5");
2657 r0.clear_nan ();
2658 r1.set_nan (float_type_node);
2659 r0.intersect (r1);
2660 ASSERT_TRUE (r0.undefined_p ());
2661
2662 // Setting the NAN bit to yes does not make us a known NAN.
2663 r0.set_varying (float_type_node);
2664 r0.update_nan ();
2665 ASSERT_FALSE (r0.known_isnan ());
2666
2667 // NAN is in a VARYING.
2668 r0.set_varying (float_type_node);
2669 real_nan (&r, "", 1, TYPE_MODE (float_type_node));
2670 REAL_VALUE_TYPE nan = r;
2671 ASSERT_TRUE (r0.contains_p (nan));
2672
2673 // -NAN is in a VARYING.
2674 r0.set_varying (float_type_node);
2675 q = real_value_negate (&r);
2676 REAL_VALUE_TYPE neg_nan = q;
2677 ASSERT_TRUE (r0.contains_p (neg_nan));
2678
2679 // Clearing the NAN on a [] NAN is the empty set.
2680 r0.set_nan (float_type_node);
2681 r0.clear_nan ();
2682 ASSERT_TRUE (r0.undefined_p ());
2683
2684 // [10,20] NAN ^ [21,25] NAN = [NAN]
2685 r0 = frange_float ("10", "20");
2686 r0.update_nan ();
2687 r1 = frange_float ("21", "25");
2688 r1.update_nan ();
2689 r0.intersect (r1);
2690 ASSERT_TRUE (r0.known_isnan ());
2691
2692 // NAN U [5,6] should be [5,6] +-NAN.
2693 r0.set_nan (float_type_node);
2694 r1 = frange_float ("5", "6");
2695 r1.clear_nan ();
2696 r0.union_ (r1);
2697 real_from_string (&q, "5");
2698 real_from_string (&r, "6");
2699 ASSERT_TRUE (real_identical (&q, &r0.lower_bound ()));
2700 ASSERT_TRUE (real_identical (&r, &r0.upper_bound ()));
2701 ASSERT_TRUE (!r0.signbit_p (signbit));
2702 ASSERT_TRUE (r0.maybe_isnan ());
2703 }
2704
2705 static void
2706 range_tests_signed_zeros ()
2707 {
2708 REAL_VALUE_TYPE zero = dconst0;
2709 REAL_VALUE_TYPE neg_zero = zero;
2710 neg_zero.sign = 1;
2711 frange r0, r1;
2712 bool signbit;
2713
2714 // [0,0] contains [0,0] but not [-0,-0] and vice versa.
2715 r0 = frange_float ("0.0", "0.0");
2716 r1 = frange_float ("-0.0", "-0.0");
2717 ASSERT_TRUE (r0.contains_p (zero));
2718 ASSERT_TRUE (!r0.contains_p (neg_zero));
2719 ASSERT_TRUE (r1.contains_p (neg_zero));
2720 ASSERT_TRUE (!r1.contains_p (zero));
2721
2722 // Test contains_p() when we know the sign of the zero.
2723 r0 = frange_float ("0.0", "0.0");
2724 ASSERT_TRUE (r0.contains_p (zero));
2725 ASSERT_FALSE (r0.contains_p (neg_zero));
2726 r0 = frange_float ("-0.0", "-0.0");
2727 ASSERT_TRUE (r0.contains_p (neg_zero));
2728 ASSERT_FALSE (r0.contains_p (zero));
2729
2730 r0 = frange_float ("-0.0", "0.0");
2731 ASSERT_TRUE (r0.contains_p (neg_zero));
2732 ASSERT_TRUE (r0.contains_p (zero));
2733
2734 r0 = frange_float ("-3", "5");
2735 ASSERT_TRUE (r0.contains_p (neg_zero));
2736 ASSERT_TRUE (r0.contains_p (zero));
2737
2738 // The intersection of zeros that differ in sign is a NAN (or
2739 // undefined if not honoring NANs).
2740 r0 = frange_float ("-0.0", "-0.0");
2741 r1 = frange_float ("0.0", "0.0");
2742 r0.intersect (r1);
2743 if (HONOR_NANS (float_type_node))
2744 ASSERT_TRUE (r0.known_isnan ());
2745 else
2746 ASSERT_TRUE (r0.undefined_p ());
2747
2748 // The union of zeros that differ in sign is a zero with unknown sign.
2749 r0 = frange_float ("0.0", "0.0");
2750 r1 = frange_float ("-0.0", "-0.0");
2751 r0.union_ (r1);
2752 ASSERT_TRUE (r0.zero_p () && !r0.signbit_p (signbit));
2753
2754 // [-0, +0] has an unknown sign.
2755 r0 = frange_float ("-0.0", "0.0");
2756 ASSERT_TRUE (r0.zero_p () && !r0.signbit_p (signbit));
2757
2758 // [-0, +0] ^ [0, 0] is [0, 0]
2759 r0 = frange_float ("-0.0", "0.0");
2760 r1 = frange_float ("0.0", "0.0");
2761 r0.intersect (r1);
2762 ASSERT_TRUE (r0.zero_p ());
2763
2764 r0 = frange_float ("+0", "5");
2765 r0.clear_nan ();
2766 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
2767
2768 r0 = frange_float ("-0", "5");
2769 r0.clear_nan ();
2770 ASSERT_TRUE (!r0.signbit_p (signbit));
2771
2772 r0 = frange_float ("-0", "10");
2773 r1 = frange_float ("0", "5");
2774 r0.intersect (r1);
2775 ASSERT_TRUE (real_iszero (&r0.lower_bound (), false));
2776
2777 r0 = frange_float ("-0", "5");
2778 r1 = frange_float ("0", "5");
2779 r0.union_ (r1);
2780 ASSERT_TRUE (real_iszero (&r0.lower_bound (), true));
2781
2782 r0 = frange_float ("-5", "-0");
2783 r0.update_nan ();
2784 r1 = frange_float ("0", "0");
2785 r1.update_nan ();
2786 r0.intersect (r1);
2787 if (HONOR_NANS (float_type_node))
2788 ASSERT_TRUE (r0.known_isnan ());
2789 else
2790 ASSERT_TRUE (r0.undefined_p ());
2791
2792 r0.set_nonnegative (float_type_node);
2793 if (HONOR_NANS (float_type_node))
2794 ASSERT_TRUE (r0.maybe_isnan ());
2795
2796 // Numbers containing zero should have an unknown SIGNBIT.
2797 r0 = frange_float ("0", "10");
2798 r0.clear_nan ();
2799 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
2800 }
2801
2802 static void
2803 range_tests_signbit ()
2804 {
2805 frange r0, r1;
2806 bool signbit;
2807
2808 // Negative numbers should have the SIGNBIT set.
2809 r0 = frange_float ("-5", "-1");
2810 r0.clear_nan ();
2811 ASSERT_TRUE (r0.signbit_p (signbit) && signbit);
2812 // Positive numbers should have the SIGNBIT clear.
2813 r0 = frange_float ("1", "10");
2814 r0.clear_nan ();
2815 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
2816 // Numbers spanning both positive and negative should have an
2817 // unknown SIGNBIT.
2818 r0 = frange_float ("-10", "10");
2819 r0.clear_nan ();
2820 ASSERT_TRUE (!r0.signbit_p (signbit));
2821 r0.set_varying (float_type_node);
2822 ASSERT_TRUE (!r0.signbit_p (signbit));
2823 }
2824
2825 static void
2826 range_tests_floats ()
2827 {
2828 frange r0, r1;
2829
2830 if (HONOR_NANS (float_type_node))
2831 range_tests_nan ();
2832 range_tests_signbit ();
2833
2834 if (HONOR_SIGNED_ZEROS (float_type_node))
2835 range_tests_signed_zeros ();
2836
2837 // A range of [-INF,+INF] is actually VARYING if no other properties
2838 // are set.
2839 r0 = frange_float ("-Inf", "+Inf");
2840 ASSERT_TRUE (r0.varying_p ());
2841 // ...unless it has some special property...
2842 if (HONOR_NANS (r0.type ()))
2843 {
2844 r0.clear_nan ();
2845 ASSERT_FALSE (r0.varying_p ());
2846 }
2847
2848 // For most architectures, where float and double are different
2849 // sizes, having the same endpoints does not necessarily mean the
2850 // ranges are equal.
2851 if (!types_compatible_p (float_type_node, double_type_node))
2852 {
2853 r0 = frange_float ("3.0", "3.0", float_type_node);
2854 r1 = frange_float ("3.0", "3.0", double_type_node);
2855 ASSERT_NE (r0, r1);
2856 }
2857
2858 // [3,5] U [10,12] = [3,12].
2859 r0 = frange_float ("3", "5");
2860 r1 = frange_float ("10", "12");
2861 r0.union_ (r1);
2862 ASSERT_EQ (r0, frange_float ("3", "12"));
2863
2864 // [5,10] U [4,8] = [4,10]
2865 r0 = frange_float ("5", "10");
2866 r1 = frange_float ("4", "8");
2867 r0.union_ (r1);
2868 ASSERT_EQ (r0, frange_float ("4", "10"));
2869
2870 // [3,5] U [4,10] = [3,10]
2871 r0 = frange_float ("3", "5");
2872 r1 = frange_float ("4", "10");
2873 r0.union_ (r1);
2874 ASSERT_EQ (r0, frange_float ("3", "10"));
2875
2876 // [4,10] U [5,11] = [4,11]
2877 r0 = frange_float ("4", "10");
2878 r1 = frange_float ("5", "11");
2879 r0.union_ (r1);
2880 ASSERT_EQ (r0, frange_float ("4", "11"));
2881
2882 // [3,12] ^ [10,12] = [10,12].
2883 r0 = frange_float ("3", "12");
2884 r1 = frange_float ("10", "12");
2885 r0.intersect (r1);
2886 ASSERT_EQ (r0, frange_float ("10", "12"));
2887
2888 // [10,12] ^ [11,11] = [11,11]
2889 r0 = frange_float ("10", "12");
2890 r1 = frange_float ("11", "11");
2891 r0.intersect (r1);
2892 ASSERT_EQ (r0, frange_float ("11", "11"));
2893
2894 // [10,20] ^ [5,15] = [10,15]
2895 r0 = frange_float ("10", "20");
2896 r1 = frange_float ("5", "15");
2897 r0.intersect (r1);
2898 ASSERT_EQ (r0, frange_float ("10", "15"));
2899
2900 // [10,20] ^ [15,25] = [15,20]
2901 r0 = frange_float ("10", "20");
2902 r1 = frange_float ("15", "25");
2903 r0.intersect (r1);
2904 ASSERT_EQ (r0, frange_float ("15", "20"));
2905
2906 // [10,20] ^ [21,25] = []
2907 r0 = frange_float ("10", "20");
2908 r0.clear_nan ();
2909 r1 = frange_float ("21", "25");
2910 r1.clear_nan ();
2911 r0.intersect (r1);
2912 ASSERT_TRUE (r0.undefined_p ());
2913
2914 if (HONOR_INFINITIES (float_type_node))
2915 {
2916 // Make sure [-Inf, -Inf] doesn't get normalized.
2917 r0 = frange_float ("-Inf", "-Inf");
2918 ASSERT_TRUE (real_isinf (&r0.lower_bound (), true));
2919 ASSERT_TRUE (real_isinf (&r0.upper_bound (), true));
2920 }
2921
2922 // Test that reading back a global range yields the same result as
2923 // what we wrote into it.
2924 tree ssa = make_temp_ssa_name (float_type_node, NULL, "blah");
2925 r0.set_varying (float_type_node);
2926 r0.clear_nan ();
2927 set_range_info (ssa, r0);
2928 get_global_range_query ()->range_of_expr (r1, ssa);
2929 ASSERT_EQ (r0, r1);
2930 }
2931
2932 // Run floating range tests for various combinations of NAN and INF
2933 // support.
2934
2935 static void
2936 range_tests_floats_various ()
2937 {
2938 int save_finite_math_only = flag_finite_math_only;
2939
2940 // Test -ffinite-math-only.
2941 flag_finite_math_only = 1;
2942 range_tests_floats ();
2943 // Test -fno-finite-math-only.
2944 flag_finite_math_only = 0;
2945 range_tests_floats ();
2946
2947 flag_finite_math_only = save_finite_math_only;
2948 }
2949
2950 void
2951 range_tests ()
2952 {
2953 range_tests_irange3 ();
2954 range_tests_int_range_max ();
2955 range_tests_strict_enum ();
2956 range_tests_nonzero_bits ();
2957 range_tests_floats_various ();
2958 range_tests_misc ();
2959 }
2960
2961 } // namespace selftest
2962
2963 #endif // CHECKING_P