Debian Codegraph @

gcc/value-range.cc

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