]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/value-range.cc
Don't build readline/libreadline.a, when --with-system-readline is supplied
[thirdparty/gcc.git] / gcc / value-range.cc
1 /* Support routines for value ranges.
2 Copyright (C) 2019-2022 Free Software Foundation, Inc.
3 Major hacks by Aldy Hernandez <aldyh@redhat.com> and
4 Andrew MacLeod <amacleod@redhat.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "ssa.h"
29 #include "tree-pretty-print.h"
30 #include "value-range-pretty-print.h"
31 #include "fold-const.h"
32 #include "gimple-range.h"
33
34 void
35 irange::accept (const vrange_visitor &v) const
36 {
37 v.visit (*this);
38 }
39
40 void
41 unsupported_range::accept (const vrange_visitor &v) const
42 {
43 v.visit (*this);
44 }
45
46 // Convenience function only available for integers and pointers.
47
48 wide_int
49 Value_Range::lower_bound () const
50 {
51 if (is_a <irange> (*m_vrange))
52 return as_a <irange> (*m_vrange).lower_bound ();
53 gcc_unreachable ();
54 }
55
56 // Convenience function only available for integers and pointers.
57
58 wide_int
59 Value_Range::upper_bound () const
60 {
61 if (is_a <irange> (*m_vrange))
62 return as_a <irange> (*m_vrange).upper_bound ();
63 gcc_unreachable ();
64 }
65
66 void
67 Value_Range::dump (FILE *out) const
68 {
69 if (m_vrange)
70 m_vrange->dump (out);
71 else
72 fprintf (out, "NULL");
73 }
74
75 DEBUG_FUNCTION void
76 debug (const Value_Range &r)
77 {
78 r.dump (stderr);
79 fprintf (stderr, "\n");
80 }
81
82 // Default vrange definitions.
83
84 bool
85 vrange::contains_p (tree) const
86 {
87 return varying_p ();
88 }
89
90 bool
91 vrange::singleton_p (tree *) const
92 {
93 return false;
94 }
95
96 void
97 vrange::set (tree min, tree, value_range_kind)
98 {
99 set_varying (TREE_TYPE (min));
100 }
101
102 tree
103 vrange::type () const
104 {
105 return void_type_node;
106 }
107
108 bool
109 vrange::supports_type_p (const_tree) const
110 {
111 return false;
112 }
113
114 void
115 vrange::set_undefined ()
116 {
117 m_kind = VR_UNDEFINED;
118 }
119
120 void
121 vrange::set_varying (tree)
122 {
123 m_kind = VR_VARYING;
124 }
125
126 bool
127 vrange::union_ (const vrange &r)
128 {
129 if (r.undefined_p () || varying_p ())
130 return false;
131 if (undefined_p () || r.varying_p ())
132 {
133 operator= (r);
134 return true;
135 }
136 gcc_unreachable ();
137 return false;
138 }
139
140 bool
141 vrange::intersect (const vrange &r)
142 {
143 if (undefined_p () || r.varying_p ())
144 return false;
145 if (r.undefined_p ())
146 {
147 set_undefined ();
148 return true;
149 }
150 if (varying_p ())
151 {
152 operator= (r);
153 return true;
154 }
155 gcc_unreachable ();
156 return false;
157 }
158
159 bool
160 vrange::zero_p () const
161 {
162 return false;
163 }
164
165 bool
166 vrange::nonzero_p () const
167 {
168 return false;
169 }
170
171 void
172 vrange::set_nonzero (tree type)
173 {
174 set_varying (type);
175 }
176
177 void
178 vrange::set_zero (tree type)
179 {
180 set_varying (type);
181 }
182
183 void
184 vrange::set_nonnegative (tree type)
185 {
186 set_varying (type);
187 }
188
189 bool
190 vrange::fits_p (const vrange &) const
191 {
192 return true;
193 }
194
195 // Assignment operator for generic ranges. Copying incompatible types
196 // is not allowed.
197
198 vrange &
199 vrange::operator= (const vrange &src)
200 {
201 if (is_a <irange> (src))
202 as_a <irange> (*this) = as_a <irange> (src);
203 else if (is_a <frange> (src))
204 as_a <frange> (*this) = as_a <frange> (src);
205 else
206 gcc_unreachable ();
207 return *this;
208 }
209
210 // Equality operator for generic ranges.
211
212 bool
213 vrange::operator== (const vrange &src) const
214 {
215 if (is_a <irange> (src))
216 return as_a <irange> (*this) == as_a <irange> (src);
217 if (is_a <frange> (src))
218 return as_a <frange> (*this) == as_a <frange> (src);
219 gcc_unreachable ();
220 }
221
222 // Wrapper for vrange_printer to dump a range to a file.
223
224 void
225 vrange::dump (FILE *file) const
226 {
227 pretty_printer buffer;
228 pp_needs_newline (&buffer) = true;
229 buffer.buffer->stream = file;
230 vrange_printer vrange_pp (&buffer);
231 this->accept (vrange_pp);
232 pp_flush (&buffer);
233 }
234
235 bool
236 irange::supports_type_p (const_tree type) const
237 {
238 return supports_p (type);
239 }
240
241 // Return TRUE if R fits in THIS.
242
243 bool
244 irange::fits_p (const vrange &r) const
245 {
246 return m_max_ranges >= as_a <irange> (r).num_pairs ();
247 }
248
249 void
250 irange::set_nonnegative (tree type)
251 {
252 set (build_int_cst (type, 0), TYPE_MAX_VALUE (type));
253 }
254
255 void
256 frange::accept (const vrange_visitor &v) const
257 {
258 v.visit (*this);
259 }
260
261 // Flush denormal endpoints to the appropriate 0.0.
262
263 void
264 frange::flush_denormals_to_zero ()
265 {
266 if (undefined_p () || known_isnan ())
267 return;
268
269 // Flush [x, -DENORMAL] to [x, -0.0].
270 if (real_isdenormal (&m_max) && real_isneg (&m_max))
271 {
272 m_max = dconst0;
273 if (HONOR_SIGNED_ZEROS (m_type))
274 m_max.sign = 1;
275 }
276 // Flush [+DENORMAL, x] to [+0.0, x].
277 if (real_isdenormal (&m_min) && !real_isneg (&m_min))
278 m_min = dconst0;
279 }
280
281 // Setter for franges.
282
283 void
284 frange::set (tree type,
285 const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
286 value_range_kind kind)
287 {
288 switch (kind)
289 {
290 case VR_UNDEFINED:
291 set_undefined ();
292 return;
293 case VR_VARYING:
294 case VR_ANTI_RANGE:
295 set_varying (type);
296 return;
297 case VR_RANGE:
298 break;
299 default:
300 gcc_unreachable ();
301 }
302
303 // Handle NANs.
304 if (real_isnan (&min) || real_isnan (&max))
305 {
306 gcc_checking_assert (real_identical (&min, &max));
307 bool sign = real_isneg (&min);
308 set_nan (type, sign);
309 return;
310 }
311
312 m_kind = kind;
313 m_type = type;
314 m_min = min;
315 m_max = max;
316 if (HONOR_NANS (m_type))
317 {
318 m_pos_nan = true;
319 m_neg_nan = true;
320 }
321 else
322 {
323 m_pos_nan = false;
324 m_neg_nan = false;
325 }
326
327 if (!MODE_HAS_SIGNED_ZEROS (TYPE_MODE (m_type)))
328 {
329 if (real_iszero (&m_min, 1))
330 m_min.sign = 0;
331 if (real_iszero (&m_max, 1))
332 m_max.sign = 0;
333 }
334 else if (!HONOR_SIGNED_ZEROS (m_type))
335 {
336 if (real_iszero (&m_max, 1))
337 m_max.sign = 0;
338 if (real_iszero (&m_min, 0))
339 m_min.sign = 1;
340 }
341
342 // For -ffinite-math-only we can drop ranges outside the
343 // representable numbers to min/max for the type.
344 if (flag_finite_math_only)
345 {
346 REAL_VALUE_TYPE min_repr = frange_val_min (m_type);
347 REAL_VALUE_TYPE max_repr = frange_val_max (m_type);
348 if (real_less (&m_min, &min_repr))
349 m_min = min_repr;
350 else if (real_less (&max_repr, &m_min))
351 m_min = max_repr;
352 if (real_less (&max_repr, &m_max))
353 m_max = max_repr;
354 else if (real_less (&m_max, &min_repr))
355 m_max = min_repr;
356 }
357
358 // Check for swapped ranges.
359 gcc_checking_assert (real_compare (LE_EXPR, &min, &max));
360
361 normalize_kind ();
362
363 flush_denormals_to_zero ();
364
365 if (flag_checking)
366 verify_range ();
367 }
368
369 void
370 frange::set (tree min, tree max, value_range_kind kind)
371 {
372 set (TREE_TYPE (min),
373 *TREE_REAL_CST_PTR (min), *TREE_REAL_CST_PTR (max), kind);
374 }
375
376 // Normalize range to VARYING or UNDEFINED, or vice versa. Return
377 // TRUE if anything changed.
378 //
379 // A range with no known properties can be dropped to VARYING.
380 // Similarly, a VARYING with any properties should be dropped to a
381 // VR_RANGE. Normalizing ranges upon changing them ensures there is
382 // only one representation for a given range.
383
384 bool
385 frange::normalize_kind ()
386 {
387 if (m_kind == VR_RANGE
388 && frange_val_is_min (m_min, m_type)
389 && frange_val_is_max (m_max, m_type))
390 {
391 if (!HONOR_NANS (m_type) || (m_pos_nan && m_neg_nan))
392 {
393 set_varying (m_type);
394 return true;
395 }
396 }
397 else if (m_kind == VR_VARYING)
398 {
399 if (HONOR_NANS (m_type) && (!m_pos_nan || !m_neg_nan))
400 {
401 m_kind = VR_RANGE;
402 m_min = frange_val_min (m_type);
403 m_max = frange_val_max (m_type);
404 return true;
405 }
406 }
407 else if (m_kind == VR_NAN && !m_pos_nan && !m_neg_nan)
408 set_undefined ();
409 return false;
410 }
411
412 // Union or intersect the zero endpoints of two ranges. For example:
413 // [-0, x] U [+0, x] => [-0, x]
414 // [ x, -0] U [ x, +0] => [ x, +0]
415 // [-0, x] ^ [+0, x] => [+0, x]
416 // [ x, -0] ^ [ x, +0] => [ x, -0]
417 //
418 // UNION_P is true when performing a union, or false when intersecting.
419
420 bool
421 frange::combine_zeros (const frange &r, bool union_p)
422 {
423 gcc_checking_assert (!undefined_p () && !known_isnan ());
424
425 bool changed = false;
426 if (real_iszero (&m_min) && real_iszero (&r.m_min)
427 && real_isneg (&m_min) != real_isneg (&r.m_min))
428 {
429 m_min.sign = union_p;
430 changed = true;
431 }
432 if (real_iszero (&m_max) && real_iszero (&r.m_max)
433 && real_isneg (&m_max) != real_isneg (&r.m_max))
434 {
435 m_max.sign = !union_p;
436 changed = true;
437 }
438 // If the signs are swapped, the resulting range is empty.
439 if (m_min.sign == 0 && m_max.sign == 1)
440 {
441 if (maybe_isnan ())
442 m_kind = VR_NAN;
443 else
444 set_undefined ();
445 changed = true;
446 }
447 return changed;
448 }
449
450 // Union two ranges when one is known to be a NAN.
451
452 bool
453 frange::union_nans (const frange &r)
454 {
455 gcc_checking_assert (known_isnan () || r.known_isnan ());
456
457 if (known_isnan ())
458 {
459 m_kind = r.m_kind;
460 m_min = r.m_min;
461 m_max = r.m_max;
462 }
463 m_pos_nan |= r.m_pos_nan;
464 m_neg_nan |= r.m_neg_nan;
465 normalize_kind ();
466 if (flag_checking)
467 verify_range ();
468 return true;
469 }
470
471 bool
472 frange::union_ (const vrange &v)
473 {
474 const frange &r = as_a <frange> (v);
475
476 if (r.undefined_p () || varying_p ())
477 return false;
478 if (undefined_p () || r.varying_p ())
479 {
480 *this = r;
481 return true;
482 }
483
484 // Combine NAN info.
485 if (known_isnan () || r.known_isnan ())
486 return union_nans (r);
487 bool changed = false;
488 if (m_pos_nan != r.m_pos_nan || m_neg_nan != r.m_neg_nan)
489 {
490 m_pos_nan |= r.m_pos_nan;
491 m_neg_nan |= r.m_neg_nan;
492 changed = true;
493 }
494
495 // Combine endpoints.
496 if (real_less (&r.m_min, &m_min))
497 {
498 m_min = r.m_min;
499 changed = true;
500 }
501 if (real_less (&m_max, &r.m_max))
502 {
503 m_max = r.m_max;
504 changed = true;
505 }
506
507 if (HONOR_SIGNED_ZEROS (m_type))
508 changed |= combine_zeros (r, true);
509
510 changed |= normalize_kind ();
511 if (flag_checking)
512 verify_range ();
513 return changed;
514 }
515
516 // Intersect two ranges when one is known to be a NAN.
517
518 bool
519 frange::intersect_nans (const frange &r)
520 {
521 gcc_checking_assert (known_isnan () || r.known_isnan ());
522
523 m_pos_nan &= r.m_pos_nan;
524 m_neg_nan &= r.m_neg_nan;
525 if (maybe_isnan ())
526 m_kind = VR_NAN;
527 else
528 set_undefined ();
529 if (flag_checking)
530 verify_range ();
531 return true;
532 }
533
534 bool
535 frange::intersect (const vrange &v)
536 {
537 const frange &r = as_a <frange> (v);
538
539 if (undefined_p () || r.varying_p ())
540 return false;
541 if (r.undefined_p ())
542 {
543 set_undefined ();
544 return true;
545 }
546 if (varying_p ())
547 {
548 *this = r;
549 return true;
550 }
551
552 // Combine NAN info.
553 if (known_isnan () || r.known_isnan ())
554 return intersect_nans (r);
555 bool changed = false;
556 if (m_pos_nan != r.m_pos_nan || m_neg_nan != r.m_neg_nan)
557 {
558 m_pos_nan &= r.m_pos_nan;
559 m_neg_nan &= r.m_neg_nan;
560 changed = true;
561 }
562
563 // Combine endpoints.
564 if (real_less (&m_min, &r.m_min))
565 {
566 m_min = r.m_min;
567 changed = true;
568 }
569 if (real_less (&r.m_max, &m_max))
570 {
571 m_max = r.m_max;
572 changed = true;
573 }
574 // If the endpoints are swapped, the resulting range is empty.
575 if (real_less (&m_max, &m_min))
576 {
577 if (maybe_isnan ())
578 m_kind = VR_NAN;
579 else
580 set_undefined ();
581 if (flag_checking)
582 verify_range ();
583 return true;
584 }
585
586 if (HONOR_SIGNED_ZEROS (m_type))
587 changed |= combine_zeros (r, false);
588
589 changed |= normalize_kind ();
590 if (flag_checking)
591 verify_range ();
592 return changed;
593 }
594
595 frange &
596 frange::operator= (const frange &src)
597 {
598 m_kind = src.m_kind;
599 m_type = src.m_type;
600 m_min = src.m_min;
601 m_max = src.m_max;
602 m_pos_nan = src.m_pos_nan;
603 m_neg_nan = src.m_neg_nan;
604
605 if (flag_checking)
606 verify_range ();
607 return *this;
608 }
609
610 bool
611 frange::operator== (const frange &src) const
612 {
613 if (m_kind == src.m_kind)
614 {
615 if (undefined_p ())
616 return true;
617
618 if (varying_p ())
619 return types_compatible_p (m_type, src.m_type);
620
621 if (known_isnan () || src.known_isnan ())
622 return false;
623
624 return (real_identical (&m_min, &src.m_min)
625 && real_identical (&m_max, &src.m_max)
626 && m_pos_nan == src.m_pos_nan
627 && m_neg_nan == src.m_neg_nan
628 && types_compatible_p (m_type, src.m_type));
629 }
630 return false;
631 }
632
633 // Return TRUE if range contains the TREE_REAL_CST_PTR in CST.
634
635 bool
636 frange::contains_p (tree cst) const
637 {
638 gcc_checking_assert (m_kind != VR_ANTI_RANGE);
639 const REAL_VALUE_TYPE *rv = TREE_REAL_CST_PTR (cst);
640
641 if (undefined_p ())
642 return false;
643
644 if (varying_p ())
645 return true;
646
647 if (real_isnan (rv))
648 {
649 // No NAN in range.
650 if (!m_pos_nan && !m_neg_nan)
651 return false;
652 // Both +NAN and -NAN are present.
653 if (m_pos_nan && m_neg_nan)
654 return true;
655 return m_neg_nan == rv->sign;
656 }
657 if (known_isnan ())
658 return false;
659
660 if (real_compare (GE_EXPR, rv, &m_min) && real_compare (LE_EXPR, rv, &m_max))
661 {
662 // Make sure the signs are equal for signed zeros.
663 if (HONOR_SIGNED_ZEROS (m_type) && real_iszero (rv))
664 return m_min.sign == m_max.sign && m_min.sign == rv->sign;
665 return true;
666 }
667 return false;
668 }
669
670 // If range is a singleton, place it in RESULT and return TRUE. If
671 // RESULT is NULL, just return TRUE.
672 //
673 // A NAN can never be a singleton.
674
675 bool
676 frange::singleton_p (tree *result) const
677 {
678 if (m_kind == VR_RANGE && real_identical (&m_min, &m_max))
679 {
680 // Return false for any singleton that may be a NAN.
681 if (HONOR_NANS (m_type) && maybe_isnan ())
682 return false;
683
684 if (MODE_COMPOSITE_P (TYPE_MODE (m_type)))
685 {
686 // For IBM long doubles, if the value is +-Inf or is exactly
687 // representable in double, the other double could be +0.0
688 // or -0.0. Since this means there is more than one way to
689 // represent a value, return false to avoid propagating it.
690 // See libgcc/config/rs6000/ibm-ldouble-format for details.
691 if (real_isinf (&m_min))
692 return false;
693 REAL_VALUE_TYPE r;
694 real_convert (&r, DFmode, &m_min);
695 if (real_identical (&r, &m_min))
696 return false;
697 }
698
699 if (result)
700 *result = build_real (m_type, m_min);
701 return true;
702 }
703 return false;
704 }
705
706 bool
707 frange::supports_type_p (const_tree type) const
708 {
709 return supports_p (type);
710 }
711
712 void
713 frange::verify_range ()
714 {
715 if (flag_finite_math_only)
716 gcc_checking_assert (!maybe_isnan ());
717 switch (m_kind)
718 {
719 case VR_UNDEFINED:
720 gcc_checking_assert (!m_type);
721 return;
722 case VR_VARYING:
723 if (flag_finite_math_only)
724 gcc_checking_assert (!m_pos_nan && !m_neg_nan);
725 else
726 gcc_checking_assert (m_pos_nan && m_neg_nan);
727 gcc_checking_assert (m_type);
728 gcc_checking_assert (frange_val_is_min (m_min, m_type));
729 gcc_checking_assert (frange_val_is_max (m_max, m_type));
730 return;
731 case VR_RANGE:
732 gcc_checking_assert (m_type);
733 break;
734 case VR_NAN:
735 gcc_checking_assert (m_type);
736 gcc_checking_assert (m_pos_nan || m_neg_nan);
737 return;
738 default:
739 gcc_unreachable ();
740 }
741
742 // NANs cannot appear in the endpoints of a range.
743 gcc_checking_assert (!real_isnan (&m_min) && !real_isnan (&m_max));
744
745 // Make sure we don't have swapped ranges.
746 gcc_checking_assert (!real_less (&m_max, &m_min));
747
748 // [ +0.0, -0.0 ] is nonsensical.
749 gcc_checking_assert (!(real_iszero (&m_min, 0) && real_iszero (&m_max, 1)));
750
751 // If all the properties are clear, we better not span the entire
752 // domain, because that would make us varying.
753 if (m_pos_nan && m_neg_nan)
754 gcc_checking_assert (!frange_val_is_min (m_min, m_type)
755 || !frange_val_is_max (m_max, m_type));
756 }
757
758 // We can't do much with nonzeros yet.
759 void
760 frange::set_nonzero (tree type)
761 {
762 set_varying (type);
763 }
764
765 // We can't do much with nonzeros yet.
766 bool
767 frange::nonzero_p () const
768 {
769 return false;
770 }
771
772 // Set range to [+0.0, +0.0] if honoring signed zeros, or [0.0, 0.0]
773 // otherwise.
774
775 void
776 frange::set_zero (tree type)
777 {
778 if (HONOR_SIGNED_ZEROS (type))
779 {
780 REAL_VALUE_TYPE dconstm0 = dconst0;
781 dconstm0.sign = 1;
782 set (type, dconstm0, dconst0);
783 clear_nan ();
784 }
785 else
786 set (type, dconst0, dconst0);
787 }
788
789 // Return TRUE for any zero regardless of sign.
790
791 bool
792 frange::zero_p () const
793 {
794 return (m_kind == VR_RANGE
795 && real_iszero (&m_min)
796 && real_iszero (&m_max));
797 }
798
799 void
800 frange::set_nonnegative (tree type)
801 {
802 set (type, dconst0, frange_val_max (type));
803
804 // Set +NAN as the only possibility.
805 if (HONOR_NANS (type))
806 update_nan (/*sign=*/false);
807 }
808
809 // Here we copy between any two irange's. The ranges can be legacy or
810 // multi-ranges, and copying between any combination works correctly.
811
812 irange &
813 irange::operator= (const irange &src)
814 {
815 if (legacy_mode_p ())
816 {
817 copy_to_legacy (src);
818 return *this;
819 }
820 if (src.legacy_mode_p ())
821 {
822 copy_legacy_to_multi_range (src);
823 return *this;
824 }
825
826 unsigned x;
827 unsigned lim = src.m_num_ranges;
828 if (lim > m_max_ranges)
829 lim = m_max_ranges;
830
831 for (x = 0; x < lim * 2; ++x)
832 m_base[x] = src.m_base[x];
833
834 // If the range didn't fit, the last range should cover the rest.
835 if (lim != src.m_num_ranges)
836 m_base[x - 1] = src.m_base[src.m_num_ranges * 2 - 1];
837
838 m_num_ranges = lim;
839 m_kind = src.m_kind;
840 m_nonzero_mask = src.m_nonzero_mask;
841 if (flag_checking)
842 verify_range ();
843 return *this;
844 }
845
846 // Return TRUE if range is a multi-range that can be represented as a
847 // VR_ANTI_RANGE.
848
849 bool
850 irange::maybe_anti_range () const
851 {
852 tree ttype = type ();
853 unsigned int precision = TYPE_PRECISION (ttype);
854 signop sign = TYPE_SIGN (ttype);
855 return (num_pairs () > 1
856 && precision > 1
857 && lower_bound () == wi::min_value (precision, sign)
858 && upper_bound () == wi::max_value (precision, sign));
859 }
860
861 void
862 irange::copy_legacy_to_multi_range (const irange &src)
863 {
864 gcc_checking_assert (src.legacy_mode_p ());
865 gcc_checking_assert (!legacy_mode_p ());
866 if (src.undefined_p ())
867 set_undefined ();
868 else if (src.varying_p ())
869 set_varying (src.type ());
870 else
871 {
872 if (range_has_numeric_bounds_p (&src))
873 set (src.min (), src.max (), src.kind ());
874 else
875 {
876 value_range cst (src);
877 cst.normalize_symbolics ();
878 gcc_checking_assert (cst.varying_p () || cst.kind () == VR_RANGE);
879 set (cst.min (), cst.max ());
880 }
881 }
882 }
883
884 // Copy any type of irange into a legacy.
885
886 void
887 irange::copy_to_legacy (const irange &src)
888 {
889 gcc_checking_assert (legacy_mode_p ());
890 // Handle legacy to legacy and other things that are easy to copy.
891 if (src.legacy_mode_p () || src.varying_p () || src.undefined_p ())
892 {
893 m_num_ranges = src.m_num_ranges;
894 m_base[0] = src.m_base[0];
895 m_base[1] = src.m_base[1];
896 m_kind = src.m_kind;
897 m_nonzero_mask = src.m_nonzero_mask;
898 return;
899 }
900 // Copy multi-range to legacy.
901 if (src.maybe_anti_range ())
902 {
903 int_range<3> r (src);
904 r.invert ();
905 // Use tree variants to save on tree -> wi -> tree conversions.
906 set (r.tree_lower_bound (0), r.tree_upper_bound (0), VR_ANTI_RANGE);
907 }
908 else
909 set (src.tree_lower_bound (), src.tree_upper_bound ());
910 }
911
912 // Swap MIN/MAX if they are out of order and adjust KIND appropriately.
913
914 static void
915 swap_out_of_order_endpoints (tree &min, tree &max, value_range_kind &kind)
916 {
917 gcc_checking_assert (kind != VR_UNDEFINED);
918 if (kind == VR_VARYING)
919 return;
920 /* Wrong order for min and max, to swap them and the VR type we need
921 to adjust them. */
922 if (tree_int_cst_lt (max, min))
923 {
924 tree one, tmp;
925
926 /* For one bit precision if max < min, then the swapped
927 range covers all values, so for VR_RANGE it is varying and
928 for VR_ANTI_RANGE empty range, so drop to varying as well. */
929 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
930 {
931 kind = VR_VARYING;
932 return;
933 }
934
935 one = build_int_cst (TREE_TYPE (min), 1);
936 tmp = int_const_binop (PLUS_EXPR, max, one);
937 max = int_const_binop (MINUS_EXPR, min, one);
938 min = tmp;
939
940 /* There's one corner case, if we had [C+1, C] before we now have
941 that again. But this represents an empty value range, so drop
942 to varying in this case. */
943 if (tree_int_cst_lt (max, min))
944 {
945 kind = VR_VARYING;
946 return;
947 }
948 kind = kind == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
949 }
950 }
951
952 void
953 irange::irange_set (tree min, tree max)
954 {
955 gcc_checking_assert (!POLY_INT_CST_P (min));
956 gcc_checking_assert (!POLY_INT_CST_P (max));
957
958 m_base[0] = min;
959 m_base[1] = max;
960 m_num_ranges = 1;
961 m_kind = VR_RANGE;
962 m_nonzero_mask = NULL;
963 normalize_kind ();
964
965 if (flag_checking)
966 verify_range ();
967 }
968
969 void
970 irange::irange_set_1bit_anti_range (tree min, tree max)
971 {
972 tree type = TREE_TYPE (min);
973 gcc_checking_assert (TYPE_PRECISION (type) == 1);
974
975 if (operand_equal_p (min, max))
976 {
977 // Since these are 1-bit quantities, they can only be [MIN,MIN]
978 // or [MAX,MAX].
979 if (vrp_val_is_min (min))
980 min = max = vrp_val_max (type);
981 else
982 min = max = vrp_val_min (type);
983 set (min, max);
984 }
985 else
986 {
987 // The only alternative is [MIN,MAX], which is the empty range.
988 gcc_checking_assert (vrp_val_is_min (min));
989 gcc_checking_assert (vrp_val_is_max (max));
990 set_undefined ();
991 }
992 if (flag_checking)
993 verify_range ();
994 }
995
996 void
997 irange::irange_set_anti_range (tree min, tree max)
998 {
999 gcc_checking_assert (!POLY_INT_CST_P (min));
1000 gcc_checking_assert (!POLY_INT_CST_P (max));
1001
1002 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
1003 {
1004 irange_set_1bit_anti_range (min, max);
1005 return;
1006 }
1007
1008 // set an anti-range
1009 tree type = TREE_TYPE (min);
1010 signop sign = TYPE_SIGN (type);
1011 int_range<2> type_range (type);
1012 // Calculate INVERSE([I,J]) as [-MIN, I-1][J+1, +MAX].
1013 m_num_ranges = 0;
1014 wi::overflow_type ovf;
1015
1016 wide_int w_min = wi::to_wide (min);
1017 if (wi::ne_p (w_min, type_range.lower_bound ()))
1018 {
1019 wide_int lim1 = wi::sub (w_min, 1, sign, &ovf);
1020 gcc_checking_assert (ovf != wi::OVF_OVERFLOW);
1021 m_base[0] = type_range.tree_lower_bound (0);
1022 m_base[1] = wide_int_to_tree (type, lim1);
1023 m_num_ranges = 1;
1024 }
1025 wide_int w_max = wi::to_wide (max);
1026 if (wi::ne_p (w_max, type_range.upper_bound ()))
1027 {
1028 wide_int lim2 = wi::add (w_max, 1, sign, &ovf);
1029 gcc_checking_assert (ovf != wi::OVF_OVERFLOW);
1030 m_base[m_num_ranges * 2] = wide_int_to_tree (type, lim2);
1031 m_base[m_num_ranges * 2 + 1] = type_range.tree_upper_bound (0);
1032 ++m_num_ranges;
1033 }
1034
1035 m_kind = VR_RANGE;
1036 m_nonzero_mask = NULL;
1037 normalize_kind ();
1038
1039 if (flag_checking)
1040 verify_range ();
1041 }
1042
1043 /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
1044 This means adjusting VRTYPE, MIN and MAX representing the case of a
1045 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
1046 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
1047 In corner cases where MAX+1 or MIN-1 wraps this will fall back
1048 to varying.
1049 This routine exists to ease canonicalization in the case where we
1050 extract ranges from var + CST op limit. */
1051
1052 void
1053 irange::set (tree min, tree max, value_range_kind kind)
1054 {
1055 if (kind == VR_UNDEFINED)
1056 {
1057 irange::set_undefined ();
1058 return;
1059 }
1060
1061 if (kind == VR_VARYING
1062 || POLY_INT_CST_P (min)
1063 || POLY_INT_CST_P (max))
1064 {
1065 set_varying (TREE_TYPE (min));
1066 return;
1067 }
1068
1069 if (TREE_OVERFLOW_P (min))
1070 min = drop_tree_overflow (min);
1071 if (TREE_OVERFLOW_P (max))
1072 max = drop_tree_overflow (max);
1073
1074 if (!legacy_mode_p ())
1075 {
1076 if (kind == VR_RANGE)
1077 irange_set (min, max);
1078 else
1079 {
1080 gcc_checking_assert (kind == VR_ANTI_RANGE);
1081 irange_set_anti_range (min, max);
1082 }
1083 return;
1084 }
1085 // Nothing to canonicalize for symbolic ranges.
1086 if (TREE_CODE (min) != INTEGER_CST
1087 || TREE_CODE (max) != INTEGER_CST)
1088 {
1089 m_kind = kind;
1090 m_base[0] = min;
1091 m_base[1] = max;
1092 m_num_ranges = 1;
1093 m_nonzero_mask = NULL;
1094 return;
1095 }
1096
1097 swap_out_of_order_endpoints (min, max, kind);
1098 if (kind == VR_VARYING)
1099 {
1100 set_varying (TREE_TYPE (min));
1101 return;
1102 }
1103
1104 // Anti-ranges that can be represented as ranges should be so.
1105 if (kind == VR_ANTI_RANGE)
1106 {
1107 bool is_min = vrp_val_is_min (min);
1108 bool is_max = vrp_val_is_max (max);
1109
1110 if (is_min && is_max)
1111 {
1112 // Fall through. This will either be normalized as
1113 // VR_UNDEFINED if the anti-range spans the entire
1114 // precision, or it will remain an VR_ANTI_RANGE in the case
1115 // of an -fstrict-enum where [MIN,MAX] is less than the span
1116 // of underlying precision.
1117 }
1118 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
1119 {
1120 irange_set_1bit_anti_range (min, max);
1121 return;
1122 }
1123 else if (is_min)
1124 {
1125 tree one = build_int_cst (TREE_TYPE (max), 1);
1126 min = int_const_binop (PLUS_EXPR, max, one);
1127 max = vrp_val_max (TREE_TYPE (max));
1128 kind = VR_RANGE;
1129 }
1130 else if (is_max)
1131 {
1132 tree one = build_int_cst (TREE_TYPE (min), 1);
1133 max = int_const_binop (MINUS_EXPR, min, one);
1134 min = vrp_val_min (TREE_TYPE (min));
1135 kind = VR_RANGE;
1136 }
1137 }
1138
1139 m_kind = kind;
1140 m_base[0] = min;
1141 m_base[1] = max;
1142 m_num_ranges = 1;
1143 m_nonzero_mask = NULL;
1144 normalize_kind ();
1145 if (flag_checking)
1146 verify_range ();
1147 }
1148
1149 // Check the validity of the range.
1150
1151 void
1152 irange::verify_range ()
1153 {
1154 gcc_checking_assert (m_discriminator == VR_IRANGE);
1155 if (m_kind == VR_UNDEFINED)
1156 {
1157 gcc_checking_assert (m_num_ranges == 0);
1158 return;
1159 }
1160 if (m_kind == VR_VARYING)
1161 {
1162 gcc_checking_assert (!m_nonzero_mask
1163 || wi::to_wide (m_nonzero_mask) == -1);
1164 gcc_checking_assert (m_num_ranges == 1);
1165 gcc_checking_assert (varying_compatible_p ());
1166 return;
1167 }
1168 if (!legacy_mode_p ())
1169 {
1170 gcc_checking_assert (m_num_ranges != 0);
1171 gcc_checking_assert (!varying_compatible_p ());
1172 for (unsigned i = 0; i < m_num_ranges; ++i)
1173 {
1174 tree lb = tree_lower_bound (i);
1175 tree ub = tree_upper_bound (i);
1176 int c = compare_values (lb, ub);
1177 gcc_checking_assert (c == 0 || c == -1);
1178 }
1179 return;
1180 }
1181 if (m_kind == VR_RANGE || m_kind == VR_ANTI_RANGE)
1182 {
1183 gcc_checking_assert (m_num_ranges == 1);
1184 int cmp = compare_values (tree_lower_bound (0), tree_upper_bound (0));
1185 gcc_checking_assert (cmp == 0 || cmp == -1 || cmp == -2);
1186 }
1187 }
1188
1189 // Return the lower bound for a sub-range. PAIR is the sub-range in
1190 // question.
1191
1192 wide_int
1193 irange::legacy_lower_bound (unsigned pair) const
1194 {
1195 gcc_checking_assert (legacy_mode_p ());
1196 if (symbolic_p ())
1197 {
1198 value_range numeric_range (*this);
1199 numeric_range.normalize_symbolics ();
1200 return numeric_range.legacy_lower_bound (pair);
1201 }
1202 gcc_checking_assert (m_num_ranges > 0);
1203 gcc_checking_assert (pair + 1 <= num_pairs ());
1204 if (m_kind == VR_ANTI_RANGE)
1205 {
1206 tree typ = type (), t;
1207 if (pair == 1 || vrp_val_is_min (min ()))
1208 t = wide_int_to_tree (typ, wi::to_wide (max ()) + 1);
1209 else
1210 t = vrp_val_min (typ);
1211 return wi::to_wide (t);
1212 }
1213 return wi::to_wide (tree_lower_bound (pair));
1214 }
1215
1216 // Return the upper bound for a sub-range. PAIR is the sub-range in
1217 // question.
1218
1219 wide_int
1220 irange::legacy_upper_bound (unsigned pair) const
1221 {
1222 gcc_checking_assert (legacy_mode_p ());
1223 if (symbolic_p ())
1224 {
1225 value_range numeric_range (*this);
1226 numeric_range.normalize_symbolics ();
1227 return numeric_range.legacy_upper_bound (pair);
1228 }
1229 gcc_checking_assert (m_num_ranges > 0);
1230 gcc_checking_assert (pair + 1 <= num_pairs ());
1231 if (m_kind == VR_ANTI_RANGE)
1232 {
1233 tree typ = type (), t;
1234 if (pair == 1 || vrp_val_is_min (min ()))
1235 t = vrp_val_max (typ);
1236 else
1237 t = wide_int_to_tree (typ, wi::to_wide (min ()) - 1);
1238 return wi::to_wide (t);
1239 }
1240 return wi::to_wide (tree_upper_bound (pair));
1241 }
1242
1243 bool
1244 irange::legacy_equal_p (const irange &other) const
1245 {
1246 gcc_checking_assert (legacy_mode_p () && other.legacy_mode_p ());
1247
1248 if (m_kind != other.m_kind)
1249 return false;
1250 if (m_kind == VR_UNDEFINED)
1251 return true;
1252 if (m_kind == VR_VARYING)
1253 return range_compatible_p (type (), other.type ());
1254 return (vrp_operand_equal_p (tree_lower_bound (0),
1255 other.tree_lower_bound (0))
1256 && vrp_operand_equal_p (tree_upper_bound (0),
1257 other.tree_upper_bound (0))
1258 && get_nonzero_bits () == other.get_nonzero_bits ());
1259 }
1260
1261 bool
1262 irange::operator== (const irange &other) const
1263 {
1264 if (legacy_mode_p ())
1265 {
1266 if (other.legacy_mode_p ())
1267 return legacy_equal_p (other);
1268 value_range tmp (other);
1269 return legacy_equal_p (tmp);
1270 }
1271 if (other.legacy_mode_p ())
1272 {
1273 value_range tmp2 (*this);
1274 return tmp2.legacy_equal_p (other);
1275 }
1276
1277 if (m_num_ranges != other.m_num_ranges)
1278 return false;
1279
1280 if (m_num_ranges == 0)
1281 return true;
1282
1283 for (unsigned i = 0; i < m_num_ranges; ++i)
1284 {
1285 tree lb = tree_lower_bound (i);
1286 tree ub = tree_upper_bound (i);
1287 tree lb_other = other.tree_lower_bound (i);
1288 tree ub_other = other.tree_upper_bound (i);
1289 if (!operand_equal_p (lb, lb_other, 0)
1290 || !operand_equal_p (ub, ub_other, 0))
1291 return false;
1292 }
1293 return get_nonzero_bits () == other.get_nonzero_bits ();
1294 }
1295
1296 /* Return TRUE if this is a symbolic range. */
1297
1298 bool
1299 irange::symbolic_p () const
1300 {
1301 return (m_num_ranges > 0
1302 && (!is_gimple_min_invariant (min ())
1303 || !is_gimple_min_invariant (max ())));
1304 }
1305
1306 /* Return TRUE if this is a constant range. */
1307
1308 bool
1309 irange::constant_p () const
1310 {
1311 return (m_num_ranges > 0
1312 && TREE_CODE (min ()) == INTEGER_CST
1313 && TREE_CODE (max ()) == INTEGER_CST);
1314 }
1315
1316 /* If range is a singleton, place it in RESULT and return TRUE.
1317 Note: A singleton can be any gimple invariant, not just constants.
1318 So, [&x, &x] counts as a singleton. */
1319
1320 bool
1321 irange::singleton_p (tree *result) const
1322 {
1323 if (!legacy_mode_p ())
1324 {
1325 if (num_pairs () == 1 && (wi::to_wide (tree_lower_bound ())
1326 == wi::to_wide (tree_upper_bound ())))
1327 {
1328 if (result)
1329 *result = tree_lower_bound ();
1330 return true;
1331 }
1332 return false;
1333 }
1334 if (m_kind == VR_ANTI_RANGE)
1335 {
1336 if (nonzero_p ())
1337 {
1338 if (TYPE_PRECISION (type ()) == 1)
1339 {
1340 if (result)
1341 *result = max ();
1342 return true;
1343 }
1344 return false;
1345 }
1346 if (num_pairs () == 1)
1347 {
1348 value_range vr0, vr1;
1349 ranges_from_anti_range ((const value_range *) this, &vr0, &vr1);
1350 return vr0.singleton_p (result);
1351 }
1352 }
1353 // Catches non-numeric extremes as well.
1354 if (m_kind == VR_RANGE
1355 && vrp_operand_equal_p (min (), max ())
1356 && is_gimple_min_invariant (min ()))
1357 {
1358 if (result)
1359 *result = min ();
1360 return true;
1361 }
1362 return false;
1363 }
1364
1365 /* Return 1 if VAL is inside value range.
1366 0 if VAL is not inside value range.
1367 -2 if we cannot tell either way.
1368
1369 Benchmark compile/20001226-1.c compilation time after changing this
1370 function. */
1371
1372 int
1373 irange::value_inside_range (tree val) const
1374 {
1375 if (varying_p ())
1376 return 1;
1377
1378 if (undefined_p ())
1379 return 0;
1380
1381 if (!legacy_mode_p () && TREE_CODE (val) == INTEGER_CST)
1382 return contains_p (val);
1383
1384 int cmp1 = operand_less_p (val, min ());
1385 if (cmp1 == -2)
1386 return -2;
1387 if (cmp1 == 1)
1388 return m_kind != VR_RANGE;
1389
1390 int cmp2 = operand_less_p (max (), val);
1391 if (cmp2 == -2)
1392 return -2;
1393
1394 if (m_kind == VR_RANGE)
1395 return !cmp2;
1396 else
1397 return !!cmp2;
1398 }
1399
1400 /* Return TRUE if it is possible that range contains VAL. */
1401
1402 bool
1403 irange::may_contain_p (tree val) const
1404 {
1405 return value_inside_range (val) != 0;
1406 }
1407
1408 /* Return TRUE if range contains INTEGER_CST. */
1409 /* Return 1 if VAL is inside value range.
1410 0 if VAL is not inside value range.
1411
1412 Benchmark compile/20001226-1.c compilation time after changing this
1413 function. */
1414
1415
1416 bool
1417 irange::contains_p (tree cst) const
1418 {
1419 if (undefined_p ())
1420 return false;
1421
1422 if (legacy_mode_p ())
1423 {
1424 gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST);
1425 if (symbolic_p ())
1426 {
1427 value_range numeric_range (*this);
1428 numeric_range.normalize_symbolics ();
1429 return numeric_range.contains_p (cst);
1430 }
1431 return value_inside_range (cst) == 1;
1432 }
1433
1434 gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST);
1435
1436 // See if we can exclude CST based on the nonzero bits.
1437 if (m_nonzero_mask)
1438 {
1439 wide_int cstw = wi::to_wide (cst);
1440 if (cstw != 0 && wi::bit_and (wi::to_wide (m_nonzero_mask), cstw) == 0)
1441 return false;
1442 }
1443
1444 signop sign = TYPE_SIGN (TREE_TYPE (cst));
1445 wide_int v = wi::to_wide (cst);
1446 for (unsigned r = 0; r < m_num_ranges; ++r)
1447 {
1448 if (wi::lt_p (v, lower_bound (r), sign))
1449 return false;
1450 if (wi::le_p (v, upper_bound (r), sign))
1451 return true;
1452 }
1453
1454 return false;
1455 }
1456
1457
1458 /* Normalize addresses into constants. */
1459
1460 void
1461 irange::normalize_addresses ()
1462 {
1463 if (undefined_p ())
1464 return;
1465
1466 if (!POINTER_TYPE_P (type ()) || range_has_numeric_bounds_p (this))
1467 return;
1468
1469 if (!range_includes_zero_p (this))
1470 {
1471 gcc_checking_assert (TREE_CODE (min ()) == ADDR_EXPR
1472 || TREE_CODE (max ()) == ADDR_EXPR);
1473 set_nonzero (type ());
1474 return;
1475 }
1476 set_varying (type ());
1477 }
1478
1479 /* Normalize symbolics and addresses into constants. */
1480
1481 void
1482 irange::normalize_symbolics ()
1483 {
1484 if (varying_p () || undefined_p ())
1485 return;
1486
1487 tree ttype = type ();
1488 bool min_symbolic = !is_gimple_min_invariant (min ());
1489 bool max_symbolic = !is_gimple_min_invariant (max ());
1490 if (!min_symbolic && !max_symbolic)
1491 {
1492 normalize_addresses ();
1493 return;
1494 }
1495
1496 // [SYM, SYM] -> VARYING
1497 if (min_symbolic && max_symbolic)
1498 {
1499 set_varying (ttype);
1500 return;
1501 }
1502 if (kind () == VR_RANGE)
1503 {
1504 // [SYM, NUM] -> [-MIN, NUM]
1505 if (min_symbolic)
1506 {
1507 set (vrp_val_min (ttype), max ());
1508 return;
1509 }
1510 // [NUM, SYM] -> [NUM, +MAX]
1511 set (min (), vrp_val_max (ttype));
1512 return;
1513 }
1514 gcc_checking_assert (kind () == VR_ANTI_RANGE);
1515 // ~[SYM, NUM] -> [NUM + 1, +MAX]
1516 if (min_symbolic)
1517 {
1518 if (!vrp_val_is_max (max ()))
1519 {
1520 tree n = wide_int_to_tree (ttype, wi::to_wide (max ()) + 1);
1521 set (n, vrp_val_max (ttype));
1522 return;
1523 }
1524 set_varying (ttype);
1525 return;
1526 }
1527 // ~[NUM, SYM] -> [-MIN, NUM - 1]
1528 if (!vrp_val_is_min (min ()))
1529 {
1530 tree n = wide_int_to_tree (ttype, wi::to_wide (min ()) - 1);
1531 set (vrp_val_min (ttype), n);
1532 return;
1533 }
1534 set_varying (ttype);
1535 }
1536
1537 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
1538 { VR1TYPE, VR0MIN, VR0MAX } and store the result
1539 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
1540 possible such range. The resulting range is not canonicalized. */
1541
1542 static void
1543 intersect_ranges (enum value_range_kind *vr0type,
1544 tree *vr0min, tree *vr0max,
1545 enum value_range_kind vr1type,
1546 tree vr1min, tree vr1max)
1547 {
1548 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
1549 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
1550
1551 /* [] is vr0, () is vr1 in the following classification comments. */
1552 if (mineq && maxeq)
1553 {
1554 /* [( )] */
1555 if (*vr0type == vr1type)
1556 /* Nothing to do for equal ranges. */
1557 ;
1558 else if ((*vr0type == VR_RANGE
1559 && vr1type == VR_ANTI_RANGE)
1560 || (*vr0type == VR_ANTI_RANGE
1561 && vr1type == VR_RANGE))
1562 {
1563 /* For anti-range with range intersection the result is empty. */
1564 *vr0type = VR_UNDEFINED;
1565 *vr0min = NULL_TREE;
1566 *vr0max = NULL_TREE;
1567 }
1568 else
1569 gcc_unreachable ();
1570 }
1571 else if (operand_less_p (*vr0max, vr1min) == 1
1572 || operand_less_p (vr1max, *vr0min) == 1)
1573 {
1574 /* [ ] ( ) or ( ) [ ]
1575 If the ranges have an empty intersection, the result of the
1576 intersect operation is the range for intersecting an
1577 anti-range with a range or empty when intersecting two ranges. */
1578 if (*vr0type == VR_RANGE
1579 && vr1type == VR_ANTI_RANGE)
1580 ;
1581 else if (*vr0type == VR_ANTI_RANGE
1582 && vr1type == VR_RANGE)
1583 {
1584 *vr0type = vr1type;
1585 *vr0min = vr1min;
1586 *vr0max = vr1max;
1587 }
1588 else if (*vr0type == VR_RANGE
1589 && vr1type == VR_RANGE)
1590 {
1591 *vr0type = VR_UNDEFINED;
1592 *vr0min = NULL_TREE;
1593 *vr0max = NULL_TREE;
1594 }
1595 else if (*vr0type == VR_ANTI_RANGE
1596 && vr1type == VR_ANTI_RANGE)
1597 {
1598 /* If the anti-ranges are adjacent to each other merge them. */
1599 if (TREE_CODE (*vr0max) == INTEGER_CST
1600 && TREE_CODE (vr1min) == INTEGER_CST
1601 && operand_less_p (*vr0max, vr1min) == 1
1602 && integer_onep (int_const_binop (MINUS_EXPR,
1603 vr1min, *vr0max)))
1604 *vr0max = vr1max;
1605 else if (TREE_CODE (vr1max) == INTEGER_CST
1606 && TREE_CODE (*vr0min) == INTEGER_CST
1607 && operand_less_p (vr1max, *vr0min) == 1
1608 && integer_onep (int_const_binop (MINUS_EXPR,
1609 *vr0min, vr1max)))
1610 *vr0min = vr1min;
1611 /* Else arbitrarily take VR0. */
1612 }
1613 }
1614 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
1615 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
1616 {
1617 /* [ ( ) ] or [( ) ] or [ ( )] */
1618 if (*vr0type == VR_RANGE
1619 && vr1type == VR_RANGE)
1620 {
1621 /* If both are ranges the result is the inner one. */
1622 *vr0type = vr1type;
1623 *vr0min = vr1min;
1624 *vr0max = vr1max;
1625 }
1626 else if (*vr0type == VR_RANGE
1627 && vr1type == VR_ANTI_RANGE)
1628 {
1629 /* Choose the right gap if the left one is empty. */
1630 if (mineq)
1631 {
1632 if (TREE_CODE (vr1max) != INTEGER_CST)
1633 *vr0min = vr1max;
1634 else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1
1635 && !TYPE_UNSIGNED (TREE_TYPE (vr1max)))
1636 *vr0min
1637 = int_const_binop (MINUS_EXPR, vr1max,
1638 build_int_cst (TREE_TYPE (vr1max), -1));
1639 else
1640 *vr0min
1641 = int_const_binop (PLUS_EXPR, vr1max,
1642 build_int_cst (TREE_TYPE (vr1max), 1));
1643 }
1644 /* Choose the left gap if the right one is empty. */
1645 else if (maxeq)
1646 {
1647 if (TREE_CODE (vr1min) != INTEGER_CST)
1648 *vr0max = vr1min;
1649 else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1
1650 && !TYPE_UNSIGNED (TREE_TYPE (vr1min)))
1651 *vr0max
1652 = int_const_binop (PLUS_EXPR, vr1min,
1653 build_int_cst (TREE_TYPE (vr1min), -1));
1654 else
1655 *vr0max
1656 = int_const_binop (MINUS_EXPR, vr1min,
1657 build_int_cst (TREE_TYPE (vr1min), 1));
1658 }
1659 /* Choose the anti-range if the range is effectively varying. */
1660 else if (vrp_val_is_min (*vr0min)
1661 && vrp_val_is_max (*vr0max))
1662 {
1663 *vr0type = vr1type;
1664 *vr0min = vr1min;
1665 *vr0max = vr1max;
1666 }
1667 /* Else choose the range. */
1668 }
1669 else if (*vr0type == VR_ANTI_RANGE
1670 && vr1type == VR_ANTI_RANGE)
1671 /* If both are anti-ranges the result is the outer one. */
1672 ;
1673 else if (*vr0type == VR_ANTI_RANGE
1674 && vr1type == VR_RANGE)
1675 {
1676 /* The intersection is empty. */
1677 *vr0type = VR_UNDEFINED;
1678 *vr0min = NULL_TREE;
1679 *vr0max = NULL_TREE;
1680 }
1681 else
1682 gcc_unreachable ();
1683 }
1684 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
1685 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
1686 {
1687 /* ( [ ] ) or ([ ] ) or ( [ ]) */
1688 if (*vr0type == VR_RANGE
1689 && vr1type == VR_RANGE)
1690 /* Choose the inner range. */
1691 ;
1692 else if (*vr0type == VR_ANTI_RANGE
1693 && vr1type == VR_RANGE)
1694 {
1695 /* Choose the right gap if the left is empty. */
1696 if (mineq)
1697 {
1698 *vr0type = VR_RANGE;
1699 if (TREE_CODE (*vr0max) != INTEGER_CST)
1700 *vr0min = *vr0max;
1701 else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1
1702 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max)))
1703 *vr0min
1704 = int_const_binop (MINUS_EXPR, *vr0max,
1705 build_int_cst (TREE_TYPE (*vr0max), -1));
1706 else
1707 *vr0min
1708 = int_const_binop (PLUS_EXPR, *vr0max,
1709 build_int_cst (TREE_TYPE (*vr0max), 1));
1710 *vr0max = vr1max;
1711 }
1712 /* Choose the left gap if the right is empty. */
1713 else if (maxeq)
1714 {
1715 *vr0type = VR_RANGE;
1716 if (TREE_CODE (*vr0min) != INTEGER_CST)
1717 *vr0max = *vr0min;
1718 else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1
1719 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min)))
1720 *vr0max
1721 = int_const_binop (PLUS_EXPR, *vr0min,
1722 build_int_cst (TREE_TYPE (*vr0min), -1));
1723 else
1724 *vr0max
1725 = int_const_binop (MINUS_EXPR, *vr0min,
1726 build_int_cst (TREE_TYPE (*vr0min), 1));
1727 *vr0min = vr1min;
1728 }
1729 /* Choose the anti-range if the range is effectively varying. */
1730 else if (vrp_val_is_min (vr1min)
1731 && vrp_val_is_max (vr1max))
1732 ;
1733 /* Choose the anti-range if it is ~[0,0], that range is special
1734 enough to special case when vr1's range is relatively wide.
1735 At least for types bigger than int - this covers pointers
1736 and arguments to functions like ctz. */
1737 else if (*vr0min == *vr0max
1738 && integer_zerop (*vr0min)
1739 && ((TYPE_PRECISION (TREE_TYPE (*vr0min))
1740 >= TYPE_PRECISION (integer_type_node))
1741 || POINTER_TYPE_P (TREE_TYPE (*vr0min)))
1742 && TREE_CODE (vr1max) == INTEGER_CST
1743 && TREE_CODE (vr1min) == INTEGER_CST
1744 && (wi::clz (wi::to_wide (vr1max) - wi::to_wide (vr1min))
1745 < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2))
1746 ;
1747 /* Else choose the range. */
1748 else
1749 {
1750 *vr0type = vr1type;
1751 *vr0min = vr1min;
1752 *vr0max = vr1max;
1753 }
1754 }
1755 else if (*vr0type == VR_ANTI_RANGE
1756 && vr1type == VR_ANTI_RANGE)
1757 {
1758 /* If both are anti-ranges the result is the outer one. */
1759 *vr0type = vr1type;
1760 *vr0min = vr1min;
1761 *vr0max = vr1max;
1762 }
1763 else if (vr1type == VR_ANTI_RANGE
1764 && *vr0type == VR_RANGE)
1765 {
1766 /* The intersection is empty. */
1767 *vr0type = VR_UNDEFINED;
1768 *vr0min = NULL_TREE;
1769 *vr0max = NULL_TREE;
1770 }
1771 else
1772 gcc_unreachable ();
1773 }
1774 else if ((operand_less_p (vr1min, *vr0max) == 1
1775 || operand_equal_p (vr1min, *vr0max, 0))
1776 && operand_less_p (*vr0min, vr1min) == 1
1777 && operand_less_p (*vr0max, vr1max) == 1)
1778 {
1779 /* [ ( ] ) or [ ]( ) */
1780 if (*vr0type == VR_ANTI_RANGE
1781 && vr1type == VR_ANTI_RANGE)
1782 *vr0max = vr1max;
1783 else if (*vr0type == VR_RANGE
1784 && vr1type == VR_RANGE)
1785 *vr0min = vr1min;
1786 else if (*vr0type == VR_RANGE
1787 && vr1type == VR_ANTI_RANGE)
1788 {
1789 if (TREE_CODE (vr1min) == INTEGER_CST)
1790 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
1791 build_int_cst (TREE_TYPE (vr1min), 1));
1792 else
1793 *vr0max = vr1min;
1794 }
1795 else if (*vr0type == VR_ANTI_RANGE
1796 && vr1type == VR_RANGE)
1797 {
1798 *vr0type = VR_RANGE;
1799 if (TREE_CODE (*vr0max) == INTEGER_CST)
1800 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
1801 build_int_cst (TREE_TYPE (*vr0max), 1));
1802 else
1803 *vr0min = *vr0max;
1804 *vr0max = vr1max;
1805 }
1806 else
1807 gcc_unreachable ();
1808 }
1809 else if ((operand_less_p (*vr0min, vr1max) == 1
1810 || operand_equal_p (*vr0min, vr1max, 0))
1811 && operand_less_p (vr1min, *vr0min) == 1
1812 && operand_less_p (vr1max, *vr0max) == 1)
1813 {
1814 /* ( [ ) ] or ( )[ ] */
1815 if (*vr0type == VR_ANTI_RANGE
1816 && vr1type == VR_ANTI_RANGE)
1817 *vr0min = vr1min;
1818 else if (*vr0type == VR_RANGE
1819 && vr1type == VR_RANGE)
1820 *vr0max = vr1max;
1821 else if (*vr0type == VR_RANGE
1822 && vr1type == VR_ANTI_RANGE)
1823 {
1824 if (TREE_CODE (vr1max) == INTEGER_CST)
1825 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
1826 build_int_cst (TREE_TYPE (vr1max), 1));
1827 else
1828 *vr0min = vr1max;
1829 }
1830 else if (*vr0type == VR_ANTI_RANGE
1831 && vr1type == VR_RANGE)
1832 {
1833 *vr0type = VR_RANGE;
1834 if (TREE_CODE (*vr0min) == INTEGER_CST)
1835 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
1836 build_int_cst (TREE_TYPE (*vr0min), 1));
1837 else
1838 *vr0max = *vr0min;
1839 *vr0min = vr1min;
1840 }
1841 else
1842 gcc_unreachable ();
1843 }
1844
1845 /* If we know the intersection is empty, there's no need to
1846 conservatively add anything else to the set. */
1847 if (*vr0type == VR_UNDEFINED)
1848 return;
1849
1850 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
1851 result for the intersection. That's always a conservative
1852 correct estimate unless VR1 is a constant singleton range
1853 in which case we choose that. */
1854 if (vr1type == VR_RANGE
1855 && is_gimple_min_invariant (vr1min)
1856 && vrp_operand_equal_p (vr1min, vr1max))
1857 {
1858 *vr0type = vr1type;
1859 *vr0min = vr1min;
1860 *vr0max = vr1max;
1861 }
1862 }
1863
1864 /* Helper for the intersection operation for value ranges. Given two
1865 ranges VR0 and VR1, set VR0 to the intersection of both ranges.
1866 This may not be the smallest possible such range. */
1867
1868 void
1869 irange::legacy_intersect (irange *vr0, const irange *vr1)
1870 {
1871 gcc_checking_assert (vr0->legacy_mode_p ());
1872 gcc_checking_assert (vr1->legacy_mode_p ());
1873 /* If either range is VR_VARYING the other one wins. */
1874 if (vr1->varying_p ())
1875 return;
1876 if (vr0->varying_p ())
1877 {
1878 vr0->set (vr1->min (), vr1->max (), vr1->kind ());
1879 return;
1880 }
1881
1882 /* When either range is VR_UNDEFINED the resulting range is
1883 VR_UNDEFINED, too. */
1884 if (vr0->undefined_p ())
1885 return;
1886 if (vr1->undefined_p ())
1887 {
1888 vr0->set_undefined ();
1889 return;
1890 }
1891
1892 value_range_kind vr0kind = vr0->kind ();
1893 tree vr0min = vr0->min ();
1894 tree vr0max = vr0->max ();
1895
1896 intersect_ranges (&vr0kind, &vr0min, &vr0max,
1897 vr1->kind (), vr1->min (), vr1->max ());
1898
1899 /* Make sure to canonicalize the result though as the inversion of a
1900 VR_RANGE can still be a VR_RANGE. */
1901 if (vr0kind == VR_UNDEFINED)
1902 vr0->set_undefined ();
1903 else if (vr0kind == VR_VARYING)
1904 {
1905 /* If we failed, use the original VR0. */
1906 return;
1907 }
1908 else
1909 vr0->set (vr0min, vr0max, vr0kind);
1910 }
1911
1912 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
1913 { VR1TYPE, VR0MIN, VR0MAX } and store the result
1914 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
1915 possible such range. The resulting range is not canonicalized. */
1916
1917 static void
1918 union_ranges (enum value_range_kind *vr0type,
1919 tree *vr0min, tree *vr0max,
1920 enum value_range_kind vr1type,
1921 tree vr1min, tree vr1max)
1922 {
1923 int cmpmin = compare_values (*vr0min, vr1min);
1924 int cmpmax = compare_values (*vr0max, vr1max);
1925 bool mineq = cmpmin == 0;
1926 bool maxeq = cmpmax == 0;
1927
1928 /* [] is vr0, () is vr1 in the following classification comments. */
1929 if (mineq && maxeq)
1930 {
1931 /* [( )] */
1932 if (*vr0type == vr1type)
1933 /* Nothing to do for equal ranges. */
1934 ;
1935 else if ((*vr0type == VR_RANGE
1936 && vr1type == VR_ANTI_RANGE)
1937 || (*vr0type == VR_ANTI_RANGE
1938 && vr1type == VR_RANGE))
1939 {
1940 /* For anti-range with range union the result is varying. */
1941 goto give_up;
1942 }
1943 else
1944 gcc_unreachable ();
1945 }
1946 else if (operand_less_p (*vr0max, vr1min) == 1
1947 || operand_less_p (vr1max, *vr0min) == 1)
1948 {
1949 /* [ ] ( ) or ( ) [ ]
1950 If the ranges have an empty intersection, result of the union
1951 operation is the anti-range or if both are anti-ranges
1952 it covers all. */
1953 if (*vr0type == VR_ANTI_RANGE
1954 && vr1type == VR_ANTI_RANGE)
1955 goto give_up;
1956 else if (*vr0type == VR_ANTI_RANGE
1957 && vr1type == VR_RANGE)
1958 ;
1959 else if (*vr0type == VR_RANGE
1960 && vr1type == VR_ANTI_RANGE)
1961 {
1962 *vr0type = vr1type;
1963 *vr0min = vr1min;
1964 *vr0max = vr1max;
1965 }
1966 else if (*vr0type == VR_RANGE
1967 && vr1type == VR_RANGE)
1968 {
1969 /* The result is the convex hull of both ranges. */
1970 if (operand_less_p (*vr0max, vr1min) == 1)
1971 {
1972 /* If the result can be an anti-range, create one. */
1973 if (TREE_CODE (*vr0max) == INTEGER_CST
1974 && TREE_CODE (vr1min) == INTEGER_CST
1975 && vrp_val_is_min (*vr0min)
1976 && vrp_val_is_max (vr1max))
1977 {
1978 tree min = int_const_binop (PLUS_EXPR,
1979 *vr0max,
1980 build_int_cst (TREE_TYPE (*vr0max), 1));
1981 tree max = int_const_binop (MINUS_EXPR,
1982 vr1min,
1983 build_int_cst (TREE_TYPE (vr1min), 1));
1984 if (!operand_less_p (max, min))
1985 {
1986 *vr0type = VR_ANTI_RANGE;
1987 *vr0min = min;
1988 *vr0max = max;
1989 }
1990 else
1991 *vr0max = vr1max;
1992 }
1993 else
1994 *vr0max = vr1max;
1995 }
1996 else
1997 {
1998 /* If the result can be an anti-range, create one. */
1999 if (TREE_CODE (vr1max) == INTEGER_CST
2000 && TREE_CODE (*vr0min) == INTEGER_CST
2001 && vrp_val_is_min (vr1min)
2002 && vrp_val_is_max (*vr0max))
2003 {
2004 tree min = int_const_binop (PLUS_EXPR,
2005 vr1max,
2006 build_int_cst (TREE_TYPE (vr1max), 1));
2007 tree max = int_const_binop (MINUS_EXPR,
2008 *vr0min,
2009 build_int_cst (TREE_TYPE (*vr0min), 1));
2010 if (!operand_less_p (max, min))
2011 {
2012 *vr0type = VR_ANTI_RANGE;
2013 *vr0min = min;
2014 *vr0max = max;
2015 }
2016 else
2017 *vr0min = vr1min;
2018 }
2019 else
2020 *vr0min = vr1min;
2021 }
2022 }
2023 else
2024 gcc_unreachable ();
2025 }
2026 else if ((maxeq || cmpmax == 1)
2027 && (mineq || cmpmin == -1))
2028 {
2029 /* [ ( ) ] or [( ) ] or [ ( )] */
2030 if (*vr0type == VR_RANGE
2031 && vr1type == VR_RANGE)
2032 ;
2033 else if (*vr0type == VR_ANTI_RANGE
2034 && vr1type == VR_ANTI_RANGE)
2035 {
2036 *vr0type = vr1type;
2037 *vr0min = vr1min;
2038 *vr0max = vr1max;
2039 }
2040 else if (*vr0type == VR_ANTI_RANGE
2041 && vr1type == VR_RANGE)
2042 {
2043 /* Arbitrarily choose the right or left gap. */
2044 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
2045 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
2046 build_int_cst (TREE_TYPE (vr1min), 1));
2047 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
2048 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
2049 build_int_cst (TREE_TYPE (vr1max), 1));
2050 else
2051 goto give_up;
2052 }
2053 else if (*vr0type == VR_RANGE
2054 && vr1type == VR_ANTI_RANGE)
2055 /* The result covers everything. */
2056 goto give_up;
2057 else
2058 gcc_unreachable ();
2059 }
2060 else if ((maxeq || cmpmax == -1)
2061 && (mineq || cmpmin == 1))
2062 {
2063 /* ( [ ] ) or ([ ] ) or ( [ ]) */
2064 if (*vr0type == VR_RANGE
2065 && vr1type == VR_RANGE)
2066 {
2067 *vr0type = vr1type;
2068 *vr0min = vr1min;
2069 *vr0max = vr1max;
2070 }
2071 else if (*vr0type == VR_ANTI_RANGE
2072 && vr1type == VR_ANTI_RANGE)
2073 ;
2074 else if (*vr0type == VR_RANGE
2075 && vr1type == VR_ANTI_RANGE)
2076 {
2077 *vr0type = VR_ANTI_RANGE;
2078 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
2079 {
2080 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
2081 build_int_cst (TREE_TYPE (*vr0min), 1));
2082 *vr0min = vr1min;
2083 }
2084 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
2085 {
2086 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
2087 build_int_cst (TREE_TYPE (*vr0max), 1));
2088 *vr0max = vr1max;
2089 }
2090 else
2091 goto give_up;
2092 }
2093 else if (*vr0type == VR_ANTI_RANGE
2094 && vr1type == VR_RANGE)
2095 /* The result covers everything. */
2096 goto give_up;
2097 else
2098 gcc_unreachable ();
2099 }
2100 else if (cmpmin == -1
2101 && cmpmax == -1
2102 && (operand_less_p (vr1min, *vr0max) == 1
2103 || operand_equal_p (vr1min, *vr0max, 0)))
2104 {
2105 /* [ ( ] ) or [ ]( ) */
2106 if (*vr0type == VR_RANGE
2107 && vr1type == VR_RANGE)
2108 *vr0max = vr1max;
2109 else if (*vr0type == VR_ANTI_RANGE
2110 && vr1type == VR_ANTI_RANGE)
2111 *vr0min = vr1min;
2112 else if (*vr0type == VR_ANTI_RANGE
2113 && vr1type == VR_RANGE)
2114 {
2115 if (TREE_CODE (vr1min) == INTEGER_CST)
2116 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
2117 build_int_cst (TREE_TYPE (vr1min), 1));
2118 else
2119 goto give_up;
2120 }
2121 else if (*vr0type == VR_RANGE
2122 && vr1type == VR_ANTI_RANGE)
2123 {
2124 if (TREE_CODE (*vr0max) == INTEGER_CST)
2125 {
2126 *vr0type = vr1type;
2127 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
2128 build_int_cst (TREE_TYPE (*vr0max), 1));
2129 *vr0max = vr1max;
2130 }
2131 else
2132 goto give_up;
2133 }
2134 else
2135 gcc_unreachable ();
2136 }
2137 else if (cmpmin == 1
2138 && cmpmax == 1
2139 && (operand_less_p (*vr0min, vr1max) == 1
2140 || operand_equal_p (*vr0min, vr1max, 0)))
2141 {
2142 /* ( [ ) ] or ( )[ ] */
2143 if (*vr0type == VR_RANGE
2144 && vr1type == VR_RANGE)
2145 *vr0min = vr1min;
2146 else if (*vr0type == VR_ANTI_RANGE
2147 && vr1type == VR_ANTI_RANGE)
2148 *vr0max = vr1max;
2149 else if (*vr0type == VR_ANTI_RANGE
2150 && vr1type == VR_RANGE)
2151 {
2152 if (TREE_CODE (vr1max) == INTEGER_CST)
2153 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
2154 build_int_cst (TREE_TYPE (vr1max), 1));
2155 else
2156 goto give_up;
2157 }
2158 else if (*vr0type == VR_RANGE
2159 && vr1type == VR_ANTI_RANGE)
2160 {
2161 if (TREE_CODE (*vr0min) == INTEGER_CST)
2162 {
2163 *vr0type = vr1type;
2164 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
2165 build_int_cst (TREE_TYPE (*vr0min), 1));
2166 *vr0min = vr1min;
2167 }
2168 else
2169 goto give_up;
2170 }
2171 else
2172 gcc_unreachable ();
2173 }
2174 else
2175 goto give_up;
2176
2177 return;
2178
2179 give_up:
2180 *vr0type = VR_VARYING;
2181 *vr0min = NULL_TREE;
2182 *vr0max = NULL_TREE;
2183 }
2184
2185 /* Helper for meet operation for value ranges. Given two ranges VR0
2186 and VR1, set VR0 to the union of both ranges. This may not be the
2187 smallest possible such range. */
2188
2189 void
2190 irange::legacy_union (irange *vr0, const irange *vr1)
2191 {
2192 gcc_checking_assert (vr0->legacy_mode_p ());
2193 gcc_checking_assert (vr1->legacy_mode_p ());
2194
2195 /* VR0 has the resulting range if VR1 is undefined or VR0 is varying. */
2196 if (vr1->undefined_p ()
2197 || vr0->varying_p ())
2198 return;
2199
2200 /* VR1 has the resulting range if VR0 is undefined or VR1 is varying. */
2201 if (vr0->undefined_p ())
2202 {
2203 vr0->set (vr1->min (), vr1->max (), vr1->kind ());
2204 return;
2205 }
2206
2207 if (vr1->varying_p ())
2208 {
2209 vr0->set_varying (vr1->type ());
2210 return;
2211 }
2212
2213 value_range_kind vr0kind = vr0->kind ();
2214 tree vr0min = vr0->min ();
2215 tree vr0max = vr0->max ();
2216
2217 union_ranges (&vr0kind, &vr0min, &vr0max,
2218 vr1->kind (), vr1->min (), vr1->max ());
2219
2220 if (vr0kind == VR_UNDEFINED)
2221 vr0->set_undefined ();
2222 else if (vr0kind == VR_VARYING)
2223 {
2224 /* Failed to find an efficient meet. Before giving up and
2225 setting the result to VARYING, see if we can at least derive
2226 a non-zero range. */
2227 if (range_includes_zero_p (vr0) == 0
2228 && range_includes_zero_p (vr1) == 0)
2229 vr0->set_nonzero (vr0->type ());
2230 else
2231 vr0->set_varying (vr0->type ());
2232 }
2233 else
2234 vr0->set (vr0min, vr0max, vr0kind);
2235 }
2236
2237 /* Meet operation for value ranges. Given two value ranges VR0 and
2238 VR1, store in VR0 a range that contains both VR0 and VR1. This
2239 may not be the smallest possible such range.
2240 Return TRUE if the original value changes. */
2241
2242 bool
2243 irange::legacy_verbose_union_ (const irange *other)
2244 {
2245 if (legacy_mode_p ())
2246 {
2247 if (!other->legacy_mode_p ())
2248 {
2249 int_range<1> tmp = *other;
2250 legacy_union (this, &tmp);
2251 return true;
2252 }
2253 if (dump_file && (dump_flags & TDF_DETAILS))
2254 {
2255 fprintf (dump_file, "Meeting\n ");
2256 dump_value_range (dump_file, this);
2257 fprintf (dump_file, "\nand\n ");
2258 dump_value_range (dump_file, other);
2259 fprintf (dump_file, "\n");
2260 }
2261
2262 legacy_union (this, other);
2263
2264 if (dump_file && (dump_flags & TDF_DETAILS))
2265 {
2266 fprintf (dump_file, "to\n ");
2267 dump_value_range (dump_file, this);
2268 fprintf (dump_file, "\n");
2269 }
2270 return true;
2271 }
2272
2273 if (other->legacy_mode_p ())
2274 {
2275 int_range<2> wider = *other;
2276 return irange_union (wider);
2277 }
2278 else
2279 return irange_union (*other);
2280 }
2281
2282 bool
2283 irange::legacy_verbose_intersect (const irange *other)
2284 {
2285 if (legacy_mode_p ())
2286 {
2287 if (!other->legacy_mode_p ())
2288 {
2289 int_range<1> tmp = *other;
2290 legacy_intersect (this, &tmp);
2291 return true;
2292 }
2293 if (dump_file && (dump_flags & TDF_DETAILS))
2294 {
2295 fprintf (dump_file, "Intersecting\n ");
2296 dump_value_range (dump_file, this);
2297 fprintf (dump_file, "\nand\n ");
2298 dump_value_range (dump_file, other);
2299 fprintf (dump_file, "\n");
2300 }
2301
2302 legacy_intersect (this, other);
2303
2304 if (dump_file && (dump_flags & TDF_DETAILS))
2305 {
2306 fprintf (dump_file, "to\n ");
2307 dump_value_range (dump_file, this);
2308 fprintf (dump_file, "\n");
2309 }
2310 return true;
2311 }
2312
2313 if (other->legacy_mode_p ())
2314 {
2315 int_range<2> wider;
2316 wider = *other;
2317 return irange_intersect (wider);
2318 }
2319 else
2320 return irange_intersect (*other);
2321 }
2322
2323 // Perform an efficient union with R when both ranges have only a single pair.
2324 // Excluded are VARYING and UNDEFINED ranges.
2325
2326 bool
2327 irange::irange_single_pair_union (const irange &r)
2328 {
2329 gcc_checking_assert (!undefined_p () && !varying_p ());
2330 gcc_checking_assert (!r.undefined_p () && !varying_p ());
2331
2332 signop sign = TYPE_SIGN (TREE_TYPE (m_base[0]));
2333 // Check if current lower bound is also the new lower bound.
2334 if (wi::le_p (wi::to_wide (m_base[0]), wi::to_wide (r.m_base[0]), sign))
2335 {
2336 // If current upper bound is new upper bound, we're done.
2337 if (wi::le_p (wi::to_wide (r.m_base[1]), wi::to_wide (m_base[1]), sign))
2338 return union_nonzero_bits (r);
2339 // Otherwise R has the new upper bound.
2340 // Check for overlap/touching ranges, or single target range.
2341 if (m_max_ranges == 1
2342 || wi::to_widest (m_base[1]) + 1 >= wi::to_widest (r.m_base[0]))
2343 m_base[1] = r.m_base[1];
2344 else
2345 {
2346 // This is a dual range result.
2347 m_base[2] = r.m_base[0];
2348 m_base[3] = r.m_base[1];
2349 m_num_ranges = 2;
2350 }
2351 union_nonzero_bits (r);
2352 return true;
2353 }
2354
2355 // Set the new lower bound to R's lower bound.
2356 tree lb = m_base[0];
2357 m_base[0] = r.m_base[0];
2358
2359 // If R fully contains THIS range, just set the upper bound.
2360 if (wi::ge_p (wi::to_wide (r.m_base[1]), wi::to_wide (m_base[1]), sign))
2361 m_base[1] = r.m_base[1];
2362 // Check for overlapping ranges, or target limited to a single range.
2363 else if (m_max_ranges == 1
2364 || wi::to_widest (r.m_base[1]) + 1 >= wi::to_widest (lb))
2365 ;
2366 else
2367 {
2368 // Left with 2 pairs.
2369 m_num_ranges = 2;
2370 m_base[2] = lb;
2371 m_base[3] = m_base[1];
2372 m_base[1] = r.m_base[1];
2373 }
2374 union_nonzero_bits (r);
2375 return true;
2376 }
2377
2378 // union_ for multi-ranges.
2379
2380 bool
2381 irange::irange_union (const irange &r)
2382 {
2383 gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ());
2384
2385 if (r.undefined_p ())
2386 return false;
2387
2388 if (undefined_p ())
2389 {
2390 operator= (r);
2391 if (flag_checking)
2392 verify_range ();
2393 return true;
2394 }
2395
2396 if (varying_p ())
2397 return false;
2398
2399 if (r.varying_p ())
2400 {
2401 set_varying (type ());
2402 return true;
2403 }
2404
2405 // Special case one range union one range.
2406 if (m_num_ranges == 1 && r.m_num_ranges == 1)
2407 return irange_single_pair_union (r);
2408
2409 // If this ranges fully contains R, then we need do nothing.
2410 if (irange_contains_p (r))
2411 return union_nonzero_bits (r);
2412
2413 // Do not worry about merging and such by reserving twice as many
2414 // pairs as needed, and then simply sort the 2 ranges into this
2415 // intermediate form.
2416 //
2417 // The intermediate result will have the property that the beginning
2418 // of each range is <= the beginning of the next range. There may
2419 // be overlapping ranges at this point. I.e. this would be valid
2420 // [-20, 10], [-10, 0], [0, 20], [40, 90] as it satisfies this
2421 // contraint : -20 < -10 < 0 < 40. When the range is rebuilt into r,
2422 // the merge is performed.
2423 //
2424 // [Xi,Yi]..[Xn,Yn] U [Xj,Yj]..[Xm,Ym] --> [Xk,Yk]..[Xp,Yp]
2425 auto_vec<tree, 20> res (m_num_ranges * 2 + r.m_num_ranges * 2);
2426 unsigned i = 0, j = 0, k = 0;
2427
2428 while (i < m_num_ranges * 2 && j < r.m_num_ranges * 2)
2429 {
2430 // lower of Xi and Xj is the lowest point.
2431 if (wi::to_widest (m_base[i]) <= wi::to_widest (r.m_base[j]))
2432 {
2433 res.quick_push (m_base[i]);
2434 res.quick_push (m_base[i + 1]);
2435 k += 2;
2436 i += 2;
2437 }
2438 else
2439 {
2440 res.quick_push (r.m_base[j]);
2441 res.quick_push (r.m_base[j + 1]);
2442 k += 2;
2443 j += 2;
2444 }
2445 }
2446 for ( ; i < m_num_ranges * 2; i += 2)
2447 {
2448 res.quick_push (m_base[i]);
2449 res.quick_push (m_base[i + 1]);
2450 k += 2;
2451 }
2452 for ( ; j < r.m_num_ranges * 2; j += 2)
2453 {
2454 res.quick_push (r.m_base[j]);
2455 res.quick_push (r.m_base[j + 1]);
2456 k += 2;
2457 }
2458
2459 // Now normalize the vector removing any overlaps.
2460 i = 2;
2461 for (j = 2; j < k ; j += 2)
2462 {
2463 // Current upper+1 is >= lower bound next pair, then we merge ranges.
2464 if (wi::to_widest (res[i - 1]) + 1 >= wi::to_widest (res[j]))
2465 {
2466 // New upper bounds is greater of current or the next one.
2467 if (wi::to_widest (res[j + 1]) > wi::to_widest (res[i - 1]))
2468 res[i - 1] = res[j + 1];
2469 }
2470 else
2471 {
2472 // This is a new distinct range, but no point in copying it
2473 // if it is already in the right place.
2474 if (i != j)
2475 {
2476 res[i++] = res[j];
2477 res[i++] = res[j + 1];
2478 }
2479 else
2480 i += 2;
2481 }
2482 }
2483
2484 // At this point, the vector should have i ranges, none overlapping.
2485 // Now it simply needs to be copied, and if there are too many
2486 // ranges, merge some. We wont do any analysis as to what the
2487 // "best" merges are, simply combine the final ranges into one.
2488 if (i > m_max_ranges * 2)
2489 {
2490 res[m_max_ranges * 2 - 1] = res[i - 1];
2491 i = m_max_ranges * 2;
2492 }
2493
2494 for (j = 0; j < i ; j++)
2495 m_base[j] = res [j];
2496 m_num_ranges = i / 2;
2497
2498 m_kind = VR_RANGE;
2499 union_nonzero_bits (r);
2500 return true;
2501 }
2502
2503 // Return TRUE if THIS fully contains R. No undefined or varying cases.
2504
2505 bool
2506 irange::irange_contains_p (const irange &r) const
2507 {
2508 gcc_checking_assert (!undefined_p () && !varying_p ());
2509 gcc_checking_assert (!r.undefined_p () && !varying_p ());
2510
2511 // In order for THIS to fully contain R, all of the pairs within R must
2512 // be fully contained by the pairs in this object.
2513 signop sign = TYPE_SIGN (TREE_TYPE(m_base[0]));
2514 unsigned ri = 0;
2515 unsigned i = 0;
2516 tree rl = r.m_base[0];
2517 tree ru = r.m_base[1];
2518 tree l = m_base[0];
2519 tree u = m_base[1];
2520 while (1)
2521 {
2522 // If r is contained within this range, move to the next R
2523 if (wi::ge_p (wi::to_wide (rl), wi::to_wide (l), sign)
2524 && wi::le_p (wi::to_wide (ru), wi::to_wide (u), sign))
2525 {
2526 // This pair is OK, Either done, or bump to the next.
2527 if (++ri >= r.num_pairs ())
2528 return true;
2529 rl = r.m_base[ri * 2];
2530 ru = r.m_base[ri * 2 + 1];
2531 continue;
2532 }
2533 // Otherwise, check if this's pair occurs before R's.
2534 if (wi::lt_p (wi::to_wide (u), wi::to_wide (rl), sign))
2535 {
2536 // There's still at least one pair of R left.
2537 if (++i >= num_pairs ())
2538 return false;
2539 l = m_base[i * 2];
2540 u = m_base[i * 2 + 1];
2541 continue;
2542 }
2543 return false;
2544 }
2545 return false;
2546 }
2547
2548
2549 // Intersect for multi-ranges. Return TRUE if anything changes.
2550
2551 bool
2552 irange::irange_intersect (const irange &r)
2553 {
2554 gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ());
2555 gcc_checking_assert (undefined_p () || r.undefined_p ()
2556 || range_compatible_p (type (), r.type ()));
2557
2558 if (undefined_p ())
2559 return false;
2560 if (r.undefined_p ())
2561 {
2562 set_undefined ();
2563 return true;
2564 }
2565 if (r.varying_p ())
2566 return false;
2567 if (varying_p ())
2568 {
2569 operator= (r);
2570 return true;
2571 }
2572
2573 if (r.num_pairs () == 1)
2574 {
2575 bool res = intersect (r.lower_bound (), r.upper_bound ());
2576 if (undefined_p ())
2577 return true;
2578
2579 res |= intersect_nonzero_bits (r);
2580 return res;
2581 }
2582
2583 // If R fully contains this, then intersection will change nothing.
2584 if (r.irange_contains_p (*this))
2585 return intersect_nonzero_bits (r);
2586
2587 signop sign = TYPE_SIGN (TREE_TYPE(m_base[0]));
2588 unsigned bld_pair = 0;
2589 unsigned bld_lim = m_max_ranges;
2590 int_range_max r2 (*this);
2591 unsigned r2_lim = r2.num_pairs ();
2592 unsigned i2 = 0;
2593 for (unsigned i = 0; i < r.num_pairs (); )
2594 {
2595 // If r1's upper is < r2's lower, we can skip r1's pair.
2596 tree ru = r.m_base[i * 2 + 1];
2597 tree r2l = r2.m_base[i2 * 2];
2598 if (wi::lt_p (wi::to_wide (ru), wi::to_wide (r2l), sign))
2599 {
2600 i++;
2601 continue;
2602 }
2603 // Likewise, skip r2's pair if its excluded.
2604 tree r2u = r2.m_base[i2 * 2 + 1];
2605 tree rl = r.m_base[i * 2];
2606 if (wi::lt_p (wi::to_wide (r2u), wi::to_wide (rl), sign))
2607 {
2608 i2++;
2609 if (i2 < r2_lim)
2610 continue;
2611 // No more r2, break.
2612 break;
2613 }
2614
2615 // Must be some overlap. Find the highest of the lower bounds,
2616 // and set it, unless the build limits lower bounds is already
2617 // set.
2618 if (bld_pair < bld_lim)
2619 {
2620 if (wi::ge_p (wi::to_wide (rl), wi::to_wide (r2l), sign))
2621 m_base[bld_pair * 2] = rl;
2622 else
2623 m_base[bld_pair * 2] = r2l;
2624 }
2625 else
2626 // Decrease and set a new upper.
2627 bld_pair--;
2628
2629 // ...and choose the lower of the upper bounds.
2630 if (wi::le_p (wi::to_wide (ru), wi::to_wide (r2u), sign))
2631 {
2632 m_base[bld_pair * 2 + 1] = ru;
2633 bld_pair++;
2634 // Move past the r1 pair and keep trying.
2635 i++;
2636 continue;
2637 }
2638 else
2639 {
2640 m_base[bld_pair * 2 + 1] = r2u;
2641 bld_pair++;
2642 i2++;
2643 if (i2 < r2_lim)
2644 continue;
2645 // No more r2, break.
2646 break;
2647 }
2648 // r2 has the higher lower bound.
2649 }
2650
2651 // At the exit of this loop, it is one of 2 things:
2652 // ran out of r1, or r2, but either means we are done.
2653 m_num_ranges = bld_pair;
2654 if (m_num_ranges == 0)
2655 {
2656 set_undefined ();
2657 return true;
2658 }
2659
2660 m_kind = VR_RANGE;
2661 intersect_nonzero_bits (r);
2662 return true;
2663 }
2664
2665
2666 // Multirange intersect for a specified wide_int [lb, ub] range.
2667 // Return TRUE if intersect changed anything.
2668 //
2669 // NOTE: It is the caller's responsibility to intersect the nonzero masks.
2670
2671 bool
2672 irange::intersect (const wide_int& lb, const wide_int& ub)
2673 {
2674 // Undefined remains undefined.
2675 if (undefined_p ())
2676 return false;
2677
2678 if (legacy_mode_p ())
2679 {
2680 intersect (int_range<1> (type (), lb, ub));
2681 return true;
2682 }
2683
2684 tree range_type = type();
2685 signop sign = TYPE_SIGN (range_type);
2686
2687 gcc_checking_assert (TYPE_PRECISION (range_type) == wi::get_precision (lb));
2688 gcc_checking_assert (TYPE_PRECISION (range_type) == wi::get_precision (ub));
2689
2690 // If this range is fuly contained, then intersection will do nothing.
2691 if (wi::ge_p (lower_bound (), lb, sign)
2692 && wi::le_p (upper_bound (), ub, sign))
2693 return false;
2694
2695 unsigned bld_index = 0;
2696 unsigned pair_lim = num_pairs ();
2697 for (unsigned i = 0; i < pair_lim; i++)
2698 {
2699 tree pairl = m_base[i * 2];
2700 tree pairu = m_base[i * 2 + 1];
2701 // Once UB is less than a pairs lower bound, we're done.
2702 if (wi::lt_p (ub, wi::to_wide (pairl), sign))
2703 break;
2704 // if LB is greater than this pairs upper, this pair is excluded.
2705 if (wi::lt_p (wi::to_wide (pairu), lb, sign))
2706 continue;
2707
2708 // Must be some overlap. Find the highest of the lower bounds,
2709 // and set it
2710 if (wi::gt_p (lb, wi::to_wide (pairl), sign))
2711 m_base[bld_index * 2] = wide_int_to_tree (range_type, lb);
2712 else
2713 m_base[bld_index * 2] = pairl;
2714
2715 // ...and choose the lower of the upper bounds and if the base pair
2716 // has the lower upper bound, need to check next pair too.
2717 if (wi::lt_p (ub, wi::to_wide (pairu), sign))
2718 {
2719 m_base[bld_index++ * 2 + 1] = wide_int_to_tree (range_type, ub);
2720 break;
2721 }
2722 else
2723 m_base[bld_index++ * 2 + 1] = pairu;
2724 }
2725
2726 m_num_ranges = bld_index;
2727 if (m_num_ranges == 0)
2728 {
2729 set_undefined ();
2730 return true;
2731 }
2732
2733 m_kind = VR_RANGE;
2734 // No need to call normalize_kind(), as the caller will do this
2735 // while intersecting the nonzero mask.
2736 if (flag_checking)
2737 verify_range ();
2738 return true;
2739 }
2740
2741
2742 // Signed 1-bits are strange. You can't subtract 1, because you can't
2743 // represent the number 1. This works around that for the invert routine.
2744
2745 static wide_int inline
2746 subtract_one (const wide_int &x, tree type, wi::overflow_type &overflow)
2747 {
2748 if (TYPE_SIGN (type) == SIGNED)
2749 return wi::add (x, -1, SIGNED, &overflow);
2750 else
2751 return wi::sub (x, 1, UNSIGNED, &overflow);
2752 }
2753
2754 // The analogous function for adding 1.
2755
2756 static wide_int inline
2757 add_one (const wide_int &x, tree type, wi::overflow_type &overflow)
2758 {
2759 if (TYPE_SIGN (type) == SIGNED)
2760 return wi::sub (x, -1, SIGNED, &overflow);
2761 else
2762 return wi::add (x, 1, UNSIGNED, &overflow);
2763 }
2764
2765 // Return the inverse of a range.
2766
2767 void
2768 irange::invert ()
2769 {
2770 if (legacy_mode_p ())
2771 {
2772 // We can't just invert VR_RANGE and VR_ANTI_RANGE because we may
2773 // create non-canonical ranges. Use the constructors instead.
2774 if (m_kind == VR_RANGE)
2775 *this = value_range (min (), max (), VR_ANTI_RANGE);
2776 else if (m_kind == VR_ANTI_RANGE)
2777 *this = value_range (min (), max ());
2778 else
2779 gcc_unreachable ();
2780 return;
2781 }
2782
2783 gcc_checking_assert (!undefined_p () && !varying_p ());
2784
2785 // We always need one more set of bounds to represent an inverse, so
2786 // if we're at the limit, we can't properly represent things.
2787 //
2788 // For instance, to represent the inverse of a 2 sub-range set
2789 // [5, 10][20, 30], we would need a 3 sub-range set
2790 // [-MIN, 4][11, 19][31, MAX].
2791 //
2792 // In this case, return the most conservative thing.
2793 //
2794 // However, if any of the extremes of the range are -MIN/+MAX, we
2795 // know we will not need an extra bound. For example:
2796 //
2797 // INVERT([-MIN,20][30,40]) => [21,29][41,+MAX]
2798 // INVERT([-MIN,20][30,MAX]) => [21,29]
2799 tree ttype = type ();
2800 unsigned prec = TYPE_PRECISION (ttype);
2801 signop sign = TYPE_SIGN (ttype);
2802 wide_int type_min = wi::min_value (prec, sign);
2803 wide_int type_max = wi::max_value (prec, sign);
2804 m_nonzero_mask = NULL;
2805 if (m_num_ranges == m_max_ranges
2806 && lower_bound () != type_min
2807 && upper_bound () != type_max)
2808 {
2809 m_base[1] = wide_int_to_tree (ttype, type_max);
2810 m_num_ranges = 1;
2811 return;
2812 }
2813 // The algorithm is as follows. To calculate INVERT ([a,b][c,d]), we
2814 // generate [-MIN, a-1][b+1, c-1][d+1, MAX].
2815 //
2816 // If there is an over/underflow in the calculation for any
2817 // sub-range, we eliminate that subrange. This allows us to easily
2818 // calculate INVERT([-MIN, 5]) with: [-MIN, -MIN-1][6, MAX]. And since
2819 // we eliminate the underflow, only [6, MAX] remains.
2820 unsigned i = 0;
2821 wi::overflow_type ovf;
2822 // Construct leftmost range.
2823 int_range_max orig_range (*this);
2824 unsigned nitems = 0;
2825 wide_int tmp;
2826 // If this is going to underflow on the MINUS 1, don't even bother
2827 // checking. This also handles subtracting one from an unsigned 0,
2828 // which doesn't set the underflow bit.
2829 if (type_min != orig_range.lower_bound ())
2830 {
2831 m_base[nitems++] = wide_int_to_tree (ttype, type_min);
2832 tmp = subtract_one (orig_range.lower_bound (), ttype, ovf);
2833 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
2834 if (ovf)
2835 nitems = 0;
2836 }
2837 i++;
2838 // Construct middle ranges if applicable.
2839 if (orig_range.num_pairs () > 1)
2840 {
2841 unsigned j = i;
2842 for (; j < (orig_range.num_pairs () * 2) - 1; j += 2)
2843 {
2844 // The middle ranges cannot have MAX/MIN, so there's no need
2845 // to check for unsigned overflow on the +1 and -1 here.
2846 tmp = wi::add (wi::to_wide (orig_range.m_base[j]), 1, sign, &ovf);
2847 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
2848 tmp = subtract_one (wi::to_wide (orig_range.m_base[j + 1]),
2849 ttype, ovf);
2850 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
2851 if (ovf)
2852 nitems -= 2;
2853 }
2854 i = j;
2855 }
2856 // Construct rightmost range.
2857 //
2858 // However, if this will overflow on the PLUS 1, don't even bother.
2859 // This also handles adding one to an unsigned MAX, which doesn't
2860 // set the overflow bit.
2861 if (type_max != wi::to_wide (orig_range.m_base[i]))
2862 {
2863 tmp = add_one (wi::to_wide (orig_range.m_base[i]), ttype, ovf);
2864 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
2865 m_base[nitems++] = wide_int_to_tree (ttype, type_max);
2866 if (ovf)
2867 nitems -= 2;
2868 }
2869 m_num_ranges = nitems / 2;
2870
2871 // We disallow undefined or varying coming in, so the result can
2872 // only be a VR_RANGE.
2873 gcc_checking_assert (m_kind == VR_RANGE);
2874
2875 if (flag_checking)
2876 verify_range ();
2877 }
2878
2879 // Return the nonzero bits inherent in the range.
2880
2881 wide_int
2882 irange::get_nonzero_bits_from_range () const
2883 {
2884 // For legacy symbolics.
2885 if (!constant_p ())
2886 return wi::shwi (-1, TYPE_PRECISION (type ()));
2887
2888 wide_int min = lower_bound ();
2889 wide_int max = upper_bound ();
2890 wide_int xorv = min ^ max;
2891 if (xorv != 0)
2892 {
2893 unsigned prec = TYPE_PRECISION (type ());
2894 xorv = wi::mask (prec - wi::clz (xorv), false, prec);
2895 }
2896 return min | xorv;
2897 }
2898
2899 // If the the nonzero mask can be trivially converted to a range, do
2900 // so and return TRUE.
2901
2902 bool
2903 irange::set_range_from_nonzero_bits ()
2904 {
2905 gcc_checking_assert (!undefined_p ());
2906 if (!m_nonzero_mask)
2907 return false;
2908 unsigned popcount = wi::popcount (wi::to_wide (m_nonzero_mask));
2909
2910 // If we have only one bit set in the mask, we can figure out the
2911 // range immediately.
2912 if (popcount == 1)
2913 {
2914 // Make sure we don't pessimize the range.
2915 if (!contains_p (m_nonzero_mask))
2916 return false;
2917
2918 bool has_zero = contains_p (build_zero_cst (type ()));
2919 tree nz = m_nonzero_mask;
2920 set (nz, nz);
2921 m_nonzero_mask = nz;
2922 if (has_zero)
2923 {
2924 int_range<2> zero;
2925 zero.set_zero (type ());
2926 union_ (zero);
2927 }
2928 return true;
2929 }
2930 else if (popcount == 0)
2931 {
2932 set_zero (type ());
2933 return true;
2934 }
2935 return false;
2936 }
2937
2938 void
2939 irange::set_nonzero_bits (const wide_int_ref &bits)
2940 {
2941 gcc_checking_assert (!undefined_p ());
2942 unsigned prec = TYPE_PRECISION (type ());
2943
2944 if (bits == -1)
2945 {
2946 m_nonzero_mask = NULL;
2947 normalize_kind ();
2948 if (flag_checking)
2949 verify_range ();
2950 return;
2951 }
2952
2953 // Drop VARYINGs with a nonzero mask to a plain range.
2954 if (m_kind == VR_VARYING && bits != -1)
2955 m_kind = VR_RANGE;
2956
2957 wide_int nz = wide_int::from (bits, prec, TYPE_SIGN (type ()));
2958 m_nonzero_mask = wide_int_to_tree (type (), nz);
2959 if (set_range_from_nonzero_bits ())
2960 return;
2961
2962 normalize_kind ();
2963 if (flag_checking)
2964 verify_range ();
2965 }
2966
2967 // Return the nonzero bitmask. This will return the nonzero bits plus
2968 // the nonzero bits inherent in the range.
2969
2970 wide_int
2971 irange::get_nonzero_bits () const
2972 {
2973 gcc_checking_assert (!undefined_p ());
2974 // The nonzero mask inherent in the range is calculated on-demand.
2975 // For example, [0,255] does not have a 0xff nonzero mask by default
2976 // (unless manually set). This saves us considerable time, because
2977 // setting it at creation incurs a large penalty for irange::set.
2978 // At the time of writing there was a 5% slowdown in VRP if we kept
2979 // the mask precisely up to date at all times. Instead, we default
2980 // to -1 and set it when explicitly requested. However, this
2981 // function will always return the correct mask.
2982 if (m_nonzero_mask)
2983 return wi::to_wide (m_nonzero_mask) & get_nonzero_bits_from_range ();
2984 else
2985 return get_nonzero_bits_from_range ();
2986 }
2987
2988 // Convert tree mask to wide_int. Returns -1 for NULL masks.
2989
2990 inline wide_int
2991 mask_to_wi (tree mask, tree type)
2992 {
2993 if (mask)
2994 return wi::to_wide (mask);
2995 else
2996 return wi::shwi (-1, TYPE_PRECISION (type));
2997 }
2998
2999 // Intersect the nonzero bits in R into THIS and normalize the range.
3000 // Return TRUE if the intersection changed anything.
3001
3002 bool
3003 irange::intersect_nonzero_bits (const irange &r)
3004 {
3005 gcc_checking_assert (!undefined_p () && !r.undefined_p ());
3006
3007 if (!m_nonzero_mask && !r.m_nonzero_mask)
3008 {
3009 normalize_kind ();
3010 if (flag_checking)
3011 verify_range ();
3012 return false;
3013 }
3014
3015 bool changed = false;
3016 tree t = type ();
3017 if (mask_to_wi (m_nonzero_mask, t) != mask_to_wi (r.m_nonzero_mask, t))
3018 {
3019 wide_int nz = get_nonzero_bits () & r.get_nonzero_bits ();
3020 m_nonzero_mask = wide_int_to_tree (t, nz);
3021 if (set_range_from_nonzero_bits ())
3022 return true;
3023 changed = true;
3024 }
3025 normalize_kind ();
3026 if (flag_checking)
3027 verify_range ();
3028 return changed;
3029 }
3030
3031 // Union the nonzero bits in R into THIS and normalize the range.
3032 // Return TRUE if the union changed anything.
3033
3034 bool
3035 irange::union_nonzero_bits (const irange &r)
3036 {
3037 gcc_checking_assert (!undefined_p () && !r.undefined_p ());
3038
3039 if (!m_nonzero_mask && !r.m_nonzero_mask)
3040 {
3041 normalize_kind ();
3042 if (flag_checking)
3043 verify_range ();
3044 return false;
3045 }
3046
3047 bool changed = false;
3048 tree t = type ();
3049 if (mask_to_wi (m_nonzero_mask, t) != mask_to_wi (r.m_nonzero_mask, t))
3050 {
3051 wide_int nz = get_nonzero_bits () | r.get_nonzero_bits ();
3052 m_nonzero_mask = wide_int_to_tree (t, nz);
3053 // No need to call set_range_from_nonzero_bits, because we'll
3054 // never narrow the range. Besides, it would cause endless
3055 // recursion because of the union_ in
3056 // set_range_from_nonzero_bits.
3057 changed = true;
3058 }
3059 normalize_kind ();
3060 if (flag_checking)
3061 verify_range ();
3062 return changed;
3063 }
3064
3065 void
3066 dump_value_range (FILE *file, const vrange *vr)
3067 {
3068 vr->dump (file);
3069 }
3070
3071 DEBUG_FUNCTION void
3072 debug (const vrange *vr)
3073 {
3074 dump_value_range (stderr, vr);
3075 fprintf (stderr, "\n");
3076 }
3077
3078 DEBUG_FUNCTION void
3079 debug (const vrange &vr)
3080 {
3081 debug (&vr);
3082 }
3083
3084 DEBUG_FUNCTION void
3085 debug (const value_range *vr)
3086 {
3087 dump_value_range (stderr, vr);
3088 fprintf (stderr, "\n");
3089 }
3090
3091 DEBUG_FUNCTION void
3092 debug (const value_range &vr)
3093 {
3094 dump_value_range (stderr, &vr);
3095 fprintf (stderr, "\n");
3096 }
3097
3098 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
3099 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
3100 false otherwise. If *AR can be represented with a single range
3101 *VR1 will be VR_UNDEFINED. */
3102
3103 bool
3104 ranges_from_anti_range (const value_range *ar,
3105 value_range *vr0, value_range *vr1)
3106 {
3107 tree type = ar->type ();
3108
3109 vr0->set_undefined ();
3110 vr1->set_undefined ();
3111
3112 /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U
3113 [A+1, +INF]. Not sure if this helps in practice, though. */
3114
3115 if (ar->kind () != VR_ANTI_RANGE
3116 || TREE_CODE (ar->min ()) != INTEGER_CST
3117 || TREE_CODE (ar->max ()) != INTEGER_CST
3118 || !vrp_val_min (type)
3119 || !vrp_val_max (type))
3120 return false;
3121
3122 if (tree_int_cst_lt (vrp_val_min (type), ar->min ()))
3123 vr0->set (vrp_val_min (type),
3124 wide_int_to_tree (type, wi::to_wide (ar->min ()) - 1));
3125 if (tree_int_cst_lt (ar->max (), vrp_val_max (type)))
3126 vr1->set (wide_int_to_tree (type, wi::to_wide (ar->max ()) + 1),
3127 vrp_val_max (type));
3128 if (vr0->undefined_p ())
3129 {
3130 *vr0 = *vr1;
3131 vr1->set_undefined ();
3132 }
3133
3134 return !vr0->undefined_p ();
3135 }
3136
3137 bool
3138 range_has_numeric_bounds_p (const irange *vr)
3139 {
3140 return (!vr->undefined_p ()
3141 && TREE_CODE (vr->min ()) == INTEGER_CST
3142 && TREE_CODE (vr->max ()) == INTEGER_CST);
3143 }
3144
3145 /* Return whether VAL is equal to the maximum value of its type.
3146 We can't do a simple equality comparison with TYPE_MAX_VALUE because
3147 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
3148 is not == to the integer constant with the same value in the type. */
3149
3150 bool
3151 vrp_val_is_max (const_tree val)
3152 {
3153 tree type_max = vrp_val_max (TREE_TYPE (val));
3154 return (val == type_max
3155 || (type_max != NULL_TREE
3156 && operand_equal_p (val, type_max, 0)));
3157 }
3158
3159 /* Return whether VAL is equal to the minimum value of its type. */
3160
3161 bool
3162 vrp_val_is_min (const_tree val)
3163 {
3164 tree type_min = vrp_val_min (TREE_TYPE (val));
3165 return (val == type_min
3166 || (type_min != NULL_TREE
3167 && operand_equal_p (val, type_min, 0)));
3168 }
3169
3170 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
3171
3172 bool
3173 vrp_operand_equal_p (const_tree val1, const_tree val2)
3174 {
3175 if (val1 == val2)
3176 return true;
3177 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
3178 return false;
3179 return true;
3180 }
3181
3182 // ?? These stubs are for ipa-prop.cc which use a value_range in a
3183 // hash_traits. hash-traits.h defines an extern of gt_ggc_mx (T &)
3184 // instead of picking up the gt_ggc_mx (T *) version.
3185 void
3186 gt_pch_nx (int_range<1> *&x)
3187 {
3188 return gt_pch_nx ((irange *) x);
3189 }
3190
3191 void
3192 gt_ggc_mx (int_range<1> *&x)
3193 {
3194 return gt_ggc_mx ((irange *) x);
3195 }
3196
3197 #define DEFINE_INT_RANGE_INSTANCE(N) \
3198 template int_range<N>::int_range(tree, tree, value_range_kind); \
3199 template int_range<N>::int_range(tree_node *, \
3200 const wide_int &, \
3201 const wide_int &, \
3202 value_range_kind); \
3203 template int_range<N>::int_range(tree); \
3204 template int_range<N>::int_range(const irange &); \
3205 template int_range<N>::int_range(const int_range &); \
3206 template int_range<N>& int_range<N>::operator= (const int_range &);
3207
3208 DEFINE_INT_RANGE_INSTANCE(1)
3209 DEFINE_INT_RANGE_INSTANCE(2)
3210 DEFINE_INT_RANGE_INSTANCE(3)
3211 DEFINE_INT_RANGE_INSTANCE(255)
3212
3213 #if CHECKING_P
3214 #include "selftest.h"
3215
3216 namespace selftest
3217 {
3218 #define INT(N) build_int_cst (integer_type_node, (N))
3219 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
3220 #define UINT128(N) build_int_cstu (u128_type, (N))
3221 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
3222 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
3223
3224 static int_range<3>
3225 build_range3 (int a, int b, int c, int d, int e, int f)
3226 {
3227 int_range<3> i1 (INT (a), INT (b));
3228 int_range<3> i2 (INT (c), INT (d));
3229 int_range<3> i3 (INT (e), INT (f));
3230 i1.union_ (i2);
3231 i1.union_ (i3);
3232 return i1;
3233 }
3234
3235 static void
3236 range_tests_irange3 ()
3237 {
3238 typedef int_range<3> int_range3;
3239 int_range3 r0, r1, r2;
3240 int_range3 i1, i2, i3;
3241
3242 // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20].
3243 r0 = int_range3 (INT (10), INT (20));
3244 r1 = int_range3 (INT (5), INT (8));
3245 r0.union_ (r1);
3246 r1 = int_range3 (INT (1), INT (3));
3247 r0.union_ (r1);
3248 ASSERT_TRUE (r0 == build_range3 (1, 3, 5, 8, 10, 20));
3249
3250 // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20].
3251 r1 = int_range3 (INT (-5), INT (0));
3252 r0.union_ (r1);
3253 ASSERT_TRUE (r0 == build_range3 (-5, 3, 5, 8, 10, 20));
3254
3255 // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60].
3256 r1 = int_range3 (INT (50), INT (60));
3257 r0 = int_range3 (INT (10), INT (20));
3258 r0.union_ (int_range3 (INT (30), INT (40)));
3259 r0.union_ (r1);
3260 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
3261 // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80].
3262 r1 = int_range3 (INT (70), INT (80));
3263 r0.union_ (r1);
3264
3265 r2 = build_range3 (10, 20, 30, 40, 50, 60);
3266 r2.union_ (int_range3 (INT (70), INT (80)));
3267 ASSERT_TRUE (r0 == r2);
3268
3269 // [10,20][30,40][50,60] U [6,35] => [6,40][50,60].
3270 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3271 r1 = int_range3 (INT (6), INT (35));
3272 r0.union_ (r1);
3273 r1 = int_range3 (INT (6), INT (40));
3274 r1.union_ (int_range3 (INT (50), INT (60)));
3275 ASSERT_TRUE (r0 == r1);
3276
3277 // [10,20][30,40][50,60] U [6,60] => [6,60].
3278 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3279 r1 = int_range3 (INT (6), INT (60));
3280 r0.union_ (r1);
3281 ASSERT_TRUE (r0 == int_range3 (INT (6), INT (60)));
3282
3283 // [10,20][30,40][50,60] U [6,70] => [6,70].
3284 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3285 r1 = int_range3 (INT (6), INT (70));
3286 r0.union_ (r1);
3287 ASSERT_TRUE (r0 == int_range3 (INT (6), INT (70)));
3288
3289 // [10,20][30,40][50,60] U [35,70] => [10,20][30,70].
3290 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3291 r1 = int_range3 (INT (35), INT (70));
3292 r0.union_ (r1);
3293 r1 = int_range3 (INT (10), INT (20));
3294 r1.union_ (int_range3 (INT (30), INT (70)));
3295 ASSERT_TRUE (r0 == r1);
3296
3297 // [10,20][30,40][50,60] U [15,35] => [10,40][50,60].
3298 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3299 r1 = int_range3 (INT (15), INT (35));
3300 r0.union_ (r1);
3301 r1 = int_range3 (INT (10), INT (40));
3302 r1.union_ (int_range3 (INT (50), INT (60)));
3303 ASSERT_TRUE (r0 == r1);
3304
3305 // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60].
3306 r0 = build_range3 (10, 20, 30, 40, 50, 60);
3307 r1 = int_range3 (INT (35), INT (35));
3308 r0.union_ (r1);
3309 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
3310 }
3311
3312 static void
3313 range_tests_int_range_max ()
3314 {
3315 int_range_max big;
3316 unsigned int nrange;
3317
3318 // Build a huge multi-range range.
3319 for (nrange = 0; nrange < 50; ++nrange)
3320 {
3321 int_range<1> tmp (INT (nrange*10), INT (nrange*10 + 5));
3322 big.union_ (tmp);
3323 }
3324 ASSERT_TRUE (big.num_pairs () == nrange);
3325
3326 // Verify that we can copy it without loosing precision.
3327 int_range_max copy (big);
3328 ASSERT_TRUE (copy.num_pairs () == nrange);
3329
3330 // Inverting it should produce one more sub-range.
3331 big.invert ();
3332 ASSERT_TRUE (big.num_pairs () == nrange + 1);
3333
3334 int_range<1> tmp (INT (5), INT (37));
3335 big.intersect (tmp);
3336 ASSERT_TRUE (big.num_pairs () == 4);
3337
3338 // Test that [10,10][20,20] does NOT contain 15.
3339 {
3340 int_range_max i1 (build_int_cst (integer_type_node, 10),
3341 build_int_cst (integer_type_node, 10));
3342 int_range_max i2 (build_int_cst (integer_type_node, 20),
3343 build_int_cst (integer_type_node, 20));
3344 i1.union_ (i2);
3345 ASSERT_FALSE (i1.contains_p (build_int_cst (integer_type_node, 15)));
3346 }
3347 }
3348
3349 static void
3350 range_tests_legacy ()
3351 {
3352 // Test truncating copy to int_range<1>.
3353 int_range<3> big = build_range3 (10, 20, 30, 40, 50, 60);
3354 int_range<1> small = big;
3355 ASSERT_TRUE (small == int_range<1> (INT (10), INT (60)));
3356
3357 // Test truncating copy to int_range<2>.
3358 int_range<2> medium = big;
3359 ASSERT_TRUE (!medium.undefined_p ());
3360
3361 // Test that a truncating copy of [MIN,20][22,40][80,MAX]
3362 // ends up as a conservative anti-range of ~[21,21].
3363 big = int_range<3> (vrp_val_min (integer_type_node), INT (20));
3364 big.union_ (int_range<1> (INT (22), INT (40)));
3365 big.union_ (int_range<1> (INT (80), vrp_val_max (integer_type_node)));
3366 small = big;
3367 ASSERT_TRUE (small == int_range<1> (INT (21), INT (21), VR_ANTI_RANGE));
3368
3369 // Copying a legacy symbolic to an int_range should normalize the
3370 // symbolic at copy time.
3371 {
3372 tree ssa = make_ssa_name (integer_type_node);
3373 value_range legacy_range (ssa, INT (25));
3374 int_range<2> copy = legacy_range;
3375 ASSERT_TRUE (copy == int_range<2> (vrp_val_min (integer_type_node),
3376 INT (25)));
3377
3378 // Test that copying ~[abc_23, abc_23] to a multi-range yields varying.
3379 legacy_range = value_range (ssa, ssa, VR_ANTI_RANGE);
3380 copy = legacy_range;
3381 ASSERT_TRUE (copy.varying_p ());
3382 }
3383
3384 // VARYING of different sizes should not be equal.
3385 tree big_type = build_nonstandard_integer_type (32, 1);
3386 tree small_type = build_nonstandard_integer_type (16, 1);
3387 int_range_max r0 (big_type);
3388 int_range_max r1 (small_type);
3389 ASSERT_TRUE (r0 != r1);
3390 value_range vr0 (big_type);
3391 int_range_max vr1 (small_type);
3392 ASSERT_TRUE (vr0 != vr1);
3393 }
3394
3395 // Simulate -fstrict-enums where the domain of a type is less than the
3396 // underlying type.
3397
3398 static void
3399 range_tests_strict_enum ()
3400 {
3401 // The enum can only hold [0, 3].
3402 tree rtype = copy_node (unsigned_type_node);
3403 TYPE_MIN_VALUE (rtype) = build_int_cstu (rtype, 0);
3404 TYPE_MAX_VALUE (rtype) = build_int_cstu (rtype, 3);
3405
3406 // Test that even though vr1 covers the strict enum domain ([0, 3]),
3407 // it does not cover the domain of the underlying type.
3408 int_range<1> vr1 (build_int_cstu (rtype, 0), build_int_cstu (rtype, 1));
3409 int_range<1> vr2 (build_int_cstu (rtype, 2), build_int_cstu (rtype, 3));
3410 vr1.union_ (vr2);
3411 ASSERT_TRUE (vr1 == int_range<1> (build_int_cstu (rtype, 0),
3412 build_int_cstu (rtype, 3)));
3413 ASSERT_FALSE (vr1.varying_p ());
3414
3415 // Test that copying to a multi-range does not change things.
3416 int_range<2> ir1 (vr1);
3417 ASSERT_TRUE (ir1 == vr1);
3418 ASSERT_FALSE (ir1.varying_p ());
3419
3420 // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3].
3421 vr1 = int_range<1> (TYPE_MIN_VALUE (rtype), TYPE_MAX_VALUE (rtype));
3422 ir1 = vr1;
3423 ASSERT_TRUE (ir1 == vr1);
3424 ASSERT_FALSE (ir1.varying_p ());
3425 }
3426
3427 static void
3428 range_tests_misc ()
3429 {
3430 tree u128_type = build_nonstandard_integer_type (128, /*unsigned=*/1);
3431 int_range<1> i1, i2, i3;
3432 int_range<1> r0, r1, rold;
3433
3434 // Test 1-bit signed integer union.
3435 // [-1,-1] U [0,0] = VARYING.
3436 tree one_bit_type = build_nonstandard_integer_type (1, 0);
3437 tree one_bit_min = vrp_val_min (one_bit_type);
3438 tree one_bit_max = vrp_val_max (one_bit_type);
3439 {
3440 int_range<2> min (one_bit_min, one_bit_min);
3441 int_range<2> max (one_bit_max, one_bit_max);
3442 max.union_ (min);
3443 ASSERT_TRUE (max.varying_p ());
3444 }
3445 // Test that we can set a range of true+false for a 1-bit signed int.
3446 r0 = range_true_and_false (one_bit_type);
3447
3448 // Test inversion of 1-bit signed integers.
3449 {
3450 int_range<2> min (one_bit_min, one_bit_min);
3451 int_range<2> max (one_bit_max, one_bit_max);
3452 int_range<2> t;
3453 t = min;
3454 t.invert ();
3455 ASSERT_TRUE (t == max);
3456 t = max;
3457 t.invert ();
3458 ASSERT_TRUE (t == min);
3459 }
3460
3461 // Test that NOT(255) is [0..254] in 8-bit land.
3462 int_range<1> not_255 (UCHAR (255), UCHAR (255), VR_ANTI_RANGE);
3463 ASSERT_TRUE (not_255 == int_range<1> (UCHAR (0), UCHAR (254)));
3464
3465 // Test that NOT(0) is [1..255] in 8-bit land.
3466 int_range<1> not_zero = range_nonzero (unsigned_char_type_node);
3467 ASSERT_TRUE (not_zero == int_range<1> (UCHAR (1), UCHAR (255)));
3468
3469 // Check that [0,127][0x..ffffff80,0x..ffffff]
3470 // => ~[128, 0x..ffffff7f].
3471 r0 = int_range<1> (UINT128 (0), UINT128 (127));
3472 tree high = build_minus_one_cst (u128_type);
3473 // low = -1 - 127 => 0x..ffffff80.
3474 tree low = fold_build2 (MINUS_EXPR, u128_type, high, UINT128(127));
3475 r1 = int_range<1> (low, high); // [0x..ffffff80, 0x..ffffffff]
3476 // r0 = [0,127][0x..ffffff80,0x..fffffff].
3477 r0.union_ (r1);
3478 // r1 = [128, 0x..ffffff7f].
3479 r1 = int_range<1> (UINT128(128),
3480 fold_build2 (MINUS_EXPR, u128_type,
3481 build_minus_one_cst (u128_type),
3482 UINT128(128)));
3483 r0.invert ();
3484 ASSERT_TRUE (r0 == r1);
3485
3486 r0.set_varying (integer_type_node);
3487 tree minint = wide_int_to_tree (integer_type_node, r0.lower_bound ());
3488 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
3489
3490 r0.set_varying (short_integer_type_node);
3491
3492 r0.set_varying (unsigned_type_node);
3493 tree maxuint = wide_int_to_tree (unsigned_type_node, r0.upper_bound ());
3494
3495 // Check that ~[0,5] => [6,MAX] for unsigned int.
3496 r0 = int_range<1> (UINT (0), UINT (5));
3497 r0.invert ();
3498 ASSERT_TRUE (r0 == int_range<1> (UINT(6), maxuint));
3499
3500 // Check that ~[10,MAX] => [0,9] for unsigned int.
3501 r0 = int_range<1> (UINT(10), maxuint);
3502 r0.invert ();
3503 ASSERT_TRUE (r0 == int_range<1> (UINT (0), UINT (9)));
3504
3505 // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers.
3506 r0 = int_range<1> (UINT128 (0), UINT128 (5), VR_ANTI_RANGE);
3507 r1 = int_range<1> (UINT128(6), build_minus_one_cst (u128_type));
3508 ASSERT_TRUE (r0 == r1);
3509
3510 // Check that [~5] is really [-MIN,4][6,MAX].
3511 r0 = int_range<1> (INT (5), INT (5), VR_ANTI_RANGE);
3512 r1 = int_range<1> (minint, INT (4));
3513 r1.union_ (int_range<1> (INT (6), maxint));
3514 ASSERT_FALSE (r1.undefined_p ());
3515 ASSERT_TRUE (r0 == r1);
3516
3517 r1 = int_range<1> (INT (5), INT (5));
3518 int_range<1> r2 (r1);
3519 ASSERT_TRUE (r1 == r2);
3520
3521 r1 = int_range<1> (INT (5), INT (10));
3522
3523 r1 = int_range<1> (integer_type_node,
3524 wi::to_wide (INT (5)), wi::to_wide (INT (10)));
3525 ASSERT_TRUE (r1.contains_p (INT (7)));
3526
3527 r1 = int_range<1> (SCHAR (0), SCHAR (20));
3528 ASSERT_TRUE (r1.contains_p (SCHAR(15)));
3529 ASSERT_FALSE (r1.contains_p (SCHAR(300)));
3530
3531 // NOT([10,20]) ==> [-MIN,9][21,MAX].
3532 r0 = r1 = int_range<1> (INT (10), INT (20));
3533 r2 = int_range<1> (minint, INT(9));
3534 r2.union_ (int_range<1> (INT(21), maxint));
3535 ASSERT_FALSE (r2.undefined_p ());
3536 r1.invert ();
3537 ASSERT_TRUE (r1 == r2);
3538 // Test that NOT(NOT(x)) == x.
3539 r2.invert ();
3540 ASSERT_TRUE (r0 == r2);
3541
3542 // Test that booleans and their inverse work as expected.
3543 r0 = range_zero (boolean_type_node);
3544 ASSERT_TRUE (r0 == int_range<1> (build_zero_cst (boolean_type_node),
3545 build_zero_cst (boolean_type_node)));
3546 r0.invert ();
3547 ASSERT_TRUE (r0 == int_range<1> (build_one_cst (boolean_type_node),
3548 build_one_cst (boolean_type_node)));
3549
3550 // Make sure NULL and non-NULL of pointer types work, and that
3551 // inverses of them are consistent.
3552 tree voidp = build_pointer_type (void_type_node);
3553 r0 = range_zero (voidp);
3554 r1 = r0;
3555 r0.invert ();
3556 r0.invert ();
3557 ASSERT_TRUE (r0 == r1);
3558
3559 // [10,20] U [15, 30] => [10, 30].
3560 r0 = int_range<1> (INT (10), INT (20));
3561 r1 = int_range<1> (INT (15), INT (30));
3562 r0.union_ (r1);
3563 ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (30)));
3564
3565 // [15,40] U [] => [15,40].
3566 r0 = int_range<1> (INT (15), INT (40));
3567 r1.set_undefined ();
3568 r0.union_ (r1);
3569 ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (40)));
3570
3571 // [10,20] U [10,10] => [10,20].
3572 r0 = int_range<1> (INT (10), INT (20));
3573 r1 = int_range<1> (INT (10), INT (10));
3574 r0.union_ (r1);
3575 ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (20)));
3576
3577 // [10,20] U [9,9] => [9,20].
3578 r0 = int_range<1> (INT (10), INT (20));
3579 r1 = int_range<1> (INT (9), INT (9));
3580 r0.union_ (r1);
3581 ASSERT_TRUE (r0 == int_range<1> (INT (9), INT (20)));
3582
3583 // [10,20] ^ [15,30] => [15,20].
3584 r0 = int_range<1> (INT (10), INT (20));
3585 r1 = int_range<1> (INT (15), INT (30));
3586 r0.intersect (r1);
3587 ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (20)));
3588
3589 // Test the internal sanity of wide_int's wrt HWIs.
3590 ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node),
3591 TYPE_SIGN (boolean_type_node))
3592 == wi::uhwi (1, TYPE_PRECISION (boolean_type_node)));
3593
3594 // Test zero_p().
3595 r0 = int_range<1> (INT (0), INT (0));
3596 ASSERT_TRUE (r0.zero_p ());
3597
3598 // Test nonzero_p().
3599 r0 = int_range<1> (INT (0), INT (0));
3600 r0.invert ();
3601 ASSERT_TRUE (r0.nonzero_p ());
3602
3603 // test legacy interaction
3604 // r0 = ~[1,1]
3605 r0 = int_range<1> (UINT (1), UINT (1), VR_ANTI_RANGE);
3606 // r1 = ~[3,3]
3607 r1 = int_range<1> (UINT (3), UINT (3), VR_ANTI_RANGE);
3608
3609 // vv = [0,0][2,2][4, MAX]
3610 int_range<3> vv = r0;
3611 vv.intersect (r1);
3612
3613 ASSERT_TRUE (vv.contains_p (UINT (2)));
3614 ASSERT_TRUE (vv.num_pairs () == 3);
3615
3616 // create r0 as legacy [1,1]
3617 r0 = int_range<1> (UINT (1), UINT (1));
3618 // And union it with [0,0][2,2][4,MAX] multi range
3619 r0.union_ (vv);
3620 // The result should be [0,2][4,MAX], or ~[3,3] but it must contain 2
3621 ASSERT_TRUE (r0.contains_p (UINT (2)));
3622 }
3623
3624 static void
3625 range_tests_nonzero_bits ()
3626 {
3627 int_range<2> r0, r1;
3628
3629 // Adding nonzero bits to a varying drops the varying.
3630 r0.set_varying (integer_type_node);
3631 r0.set_nonzero_bits (255);
3632 ASSERT_TRUE (!r0.varying_p ());
3633 // Dropping the nonzero bits brings us back to varying.
3634 r0.set_nonzero_bits (-1);
3635 ASSERT_TRUE (r0.varying_p ());
3636
3637 // Test contains_p with nonzero bits.
3638 r0.set_zero (integer_type_node);
3639 ASSERT_TRUE (r0.contains_p (INT (0)));
3640 ASSERT_FALSE (r0.contains_p (INT (1)));
3641 r0.set_nonzero_bits (0xfe);
3642 ASSERT_FALSE (r0.contains_p (INT (0x100)));
3643 ASSERT_FALSE (r0.contains_p (INT (0x3)));
3644
3645 // Union of nonzero bits.
3646 r0.set_varying (integer_type_node);
3647 r0.set_nonzero_bits (0xf0);
3648 r1.set_varying (integer_type_node);
3649 r1.set_nonzero_bits (0xf);
3650 r0.union_ (r1);
3651 ASSERT_TRUE (r0.get_nonzero_bits () == 0xff);
3652
3653 // Intersect of nonzero bits.
3654 r0.set (INT (0), INT (255));
3655 r0.set_nonzero_bits (0xfe);
3656 r1.set_varying (integer_type_node);
3657 r1.set_nonzero_bits (0xf0);
3658 r0.intersect (r1);
3659 ASSERT_TRUE (r0.get_nonzero_bits () == 0xf0);
3660
3661 // Intersect where the mask of nonzero bits is implicit from the range.
3662 r0.set_varying (integer_type_node);
3663 r1.set (INT (0), INT (255));
3664 r0.intersect (r1);
3665 ASSERT_TRUE (r0.get_nonzero_bits () == 0xff);
3666
3667 // The union of a mask of 0xff..ffff00 with a mask of 0xff spans the
3668 // entire domain, and makes the range a varying.
3669 r0.set_varying (integer_type_node);
3670 wide_int x = wi::shwi (0xff, TYPE_PRECISION (integer_type_node));
3671 x = wi::bit_not (x);
3672 r0.set_nonzero_bits (x); // 0xff..ff00
3673 r1.set_varying (integer_type_node);
3674 r1.set_nonzero_bits (0xff);
3675 r0.union_ (r1);
3676 ASSERT_TRUE (r0.varying_p ());
3677
3678 // Test that setting a nonzero bit of 1 does not pessimize the range.
3679 r0.set_zero (integer_type_node);
3680 r0.set_nonzero_bits (1);
3681 ASSERT_TRUE (r0.zero_p ());
3682 }
3683
3684 // Build an frange from string endpoints.
3685
3686 static inline frange
3687 frange_float (const char *lb, const char *ub, tree type = float_type_node)
3688 {
3689 REAL_VALUE_TYPE min, max;
3690 gcc_assert (real_from_string (&min, lb) == 0);
3691 gcc_assert (real_from_string (&max, ub) == 0);
3692 return frange (type, min, max);
3693 }
3694
3695 static void
3696 range_tests_nan ()
3697 {
3698 frange r0, r1;
3699 REAL_VALUE_TYPE q, r;
3700 bool signbit;
3701
3702 // Equal ranges but with differing NAN bits are not equal.
3703 if (HONOR_NANS (float_type_node))
3704 {
3705 r1 = frange_float ("10", "12");
3706 r0 = r1;
3707 ASSERT_EQ (r0, r1);
3708 r0.clear_nan ();
3709 ASSERT_NE (r0, r1);
3710 r0.update_nan ();
3711 ASSERT_EQ (r0, r1);
3712
3713 // [10, 20] NAN ^ [30, 40] NAN = NAN.
3714 r0 = frange_float ("10", "20");
3715 r1 = frange_float ("30", "40");
3716 r0.intersect (r1);
3717 ASSERT_TRUE (r0.known_isnan ());
3718
3719 // [3,5] U [5,10] NAN = ... NAN
3720 r0 = frange_float ("3", "5");
3721 r0.clear_nan ();
3722 r1 = frange_float ("5", "10");
3723 r0.union_ (r1);
3724 ASSERT_TRUE (r0.maybe_isnan ());
3725 }
3726
3727 // NAN ranges are not equal to each other.
3728 r0.set_nan (float_type_node);
3729 r1 = r0;
3730 ASSERT_FALSE (r0 == r1);
3731 ASSERT_FALSE (r0 == r0);
3732 ASSERT_TRUE (r0 != r0);
3733
3734 // [5,6] U NAN = [5,6] NAN.
3735 r0 = frange_float ("5", "6");
3736 r0.clear_nan ();
3737 r1.set_nan (float_type_node);
3738 r0.union_ (r1);
3739 real_from_string (&q, "5");
3740 real_from_string (&r, "6");
3741 ASSERT_TRUE (real_identical (&q, &r0.lower_bound ()));
3742 ASSERT_TRUE (real_identical (&r, &r0.upper_bound ()));
3743 ASSERT_TRUE (r0.maybe_isnan ());
3744
3745 // NAN U NAN = NAN
3746 r0.set_nan (float_type_node);
3747 r1.set_nan (float_type_node);
3748 r0.union_ (r1);
3749 ASSERT_TRUE (r0.known_isnan ());
3750
3751 // [INF, INF] NAN ^ NAN = NAN
3752 r0.set_nan (float_type_node);
3753 r1 = frange_float ("+Inf", "+Inf");
3754 if (!HONOR_NANS (float_type_node))
3755 r1.update_nan ();
3756 r0.intersect (r1);
3757 ASSERT_TRUE (r0.known_isnan ());
3758
3759 // NAN ^ NAN = NAN
3760 r0.set_nan (float_type_node);
3761 r1.set_nan (float_type_node);
3762 r0.intersect (r1);
3763 ASSERT_TRUE (r0.known_isnan ());
3764
3765 // +NAN ^ -NAN = UNDEFINED
3766 r0.set_nan (float_type_node, false);
3767 r1.set_nan (float_type_node, true);
3768 r0.intersect (r1);
3769 ASSERT_TRUE (r0.undefined_p ());
3770
3771 // VARYING ^ NAN = NAN.
3772 r0.set_nan (float_type_node);
3773 r1.set_varying (float_type_node);
3774 r0.intersect (r1);
3775 ASSERT_TRUE (r0.known_isnan ());
3776
3777 // [3,4] ^ NAN = UNDEFINED.
3778 r0 = frange_float ("3", "4");
3779 r0.clear_nan ();
3780 r1.set_nan (float_type_node);
3781 r0.intersect (r1);
3782 ASSERT_TRUE (r0.undefined_p ());
3783
3784 // [-3, 5] ^ NAN = UNDEFINED
3785 r0 = frange_float ("-3", "5");
3786 r0.clear_nan ();
3787 r1.set_nan (float_type_node);
3788 r0.intersect (r1);
3789 ASSERT_TRUE (r0.undefined_p ());
3790
3791 // Setting the NAN bit to yes does not make us a known NAN.
3792 r0.set_varying (float_type_node);
3793 r0.update_nan ();
3794 ASSERT_FALSE (r0.known_isnan ());
3795
3796 // NAN is in a VARYING.
3797 r0.set_varying (float_type_node);
3798 real_nan (&r, "", 1, TYPE_MODE (float_type_node));
3799 tree nan = build_real (float_type_node, r);
3800 ASSERT_TRUE (r0.contains_p (nan));
3801
3802 // -NAN is in a VARYING.
3803 r0.set_varying (float_type_node);
3804 q = real_value_negate (&r);
3805 tree neg_nan = build_real (float_type_node, q);
3806 ASSERT_TRUE (r0.contains_p (neg_nan));
3807
3808 // Clearing the NAN on a [] NAN is the empty set.
3809 r0.set_nan (float_type_node);
3810 r0.clear_nan ();
3811 ASSERT_TRUE (r0.undefined_p ());
3812
3813 // [10,20] NAN ^ [21,25] NAN = [NAN]
3814 r0 = frange_float ("10", "20");
3815 r0.update_nan ();
3816 r1 = frange_float ("21", "25");
3817 r1.update_nan ();
3818 r0.intersect (r1);
3819 ASSERT_TRUE (r0.known_isnan ());
3820
3821 // NAN U [5,6] should be [5,6] +-NAN.
3822 r0.set_nan (float_type_node);
3823 r1 = frange_float ("5", "6");
3824 r1.clear_nan ();
3825 r0.union_ (r1);
3826 real_from_string (&q, "5");
3827 real_from_string (&r, "6");
3828 ASSERT_TRUE (real_identical (&q, &r0.lower_bound ()));
3829 ASSERT_TRUE (real_identical (&r, &r0.upper_bound ()));
3830 ASSERT_TRUE (!r0.signbit_p (signbit));
3831 ASSERT_TRUE (r0.maybe_isnan ());
3832 }
3833
3834 static void
3835 range_tests_signed_zeros ()
3836 {
3837 tree zero = build_zero_cst (float_type_node);
3838 tree neg_zero = fold_build1 (NEGATE_EXPR, float_type_node, zero);
3839 frange r0, r1;
3840 bool signbit;
3841
3842 // [0,0] contains [0,0] but not [-0,-0] and vice versa.
3843 r0 = frange (zero, zero);
3844 r1 = frange (neg_zero, neg_zero);
3845 ASSERT_TRUE (r0.contains_p (zero));
3846 ASSERT_TRUE (!r0.contains_p (neg_zero));
3847 ASSERT_TRUE (r1.contains_p (neg_zero));
3848 ASSERT_TRUE (!r1.contains_p (zero));
3849
3850 // Test contains_p() when we know the sign of the zero.
3851 r0 = frange (zero, zero);
3852 ASSERT_TRUE (r0.contains_p (zero));
3853 ASSERT_FALSE (r0.contains_p (neg_zero));
3854 r0 = frange (neg_zero, neg_zero);
3855 ASSERT_TRUE (r0.contains_p (neg_zero));
3856 ASSERT_FALSE (r0.contains_p (zero));
3857
3858 // The intersection of zeros that differ in sign is a NAN (or
3859 // undefined if not honoring NANs).
3860 r0 = frange (neg_zero, neg_zero);
3861 r1 = frange (zero, zero);
3862 r0.intersect (r1);
3863 if (HONOR_NANS (float_type_node))
3864 ASSERT_TRUE (r0.known_isnan ());
3865 else
3866 ASSERT_TRUE (r0.undefined_p ());
3867
3868 // The union of zeros that differ in sign is a zero with unknown sign.
3869 r0 = frange (zero, zero);
3870 r1 = frange (neg_zero, neg_zero);
3871 r0.union_ (r1);
3872 ASSERT_TRUE (r0.zero_p () && !r0.signbit_p (signbit));
3873
3874 // [-0, +0] has an unknown sign.
3875 r0 = frange (neg_zero, zero);
3876 ASSERT_TRUE (r0.zero_p () && !r0.signbit_p (signbit));
3877
3878 // [-0, +0] ^ [0, 0] is [0, 0]
3879 r0 = frange (neg_zero, zero);
3880 r1 = frange (zero, zero);
3881 r0.intersect (r1);
3882 ASSERT_TRUE (r0.zero_p ());
3883
3884 r0 = frange_float ("+0", "5");
3885 r0.clear_nan ();
3886 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
3887
3888 r0 = frange_float ("-0", "5");
3889 r0.clear_nan ();
3890 ASSERT_TRUE (!r0.signbit_p (signbit));
3891
3892 r0 = frange_float ("-0", "10");
3893 r1 = frange_float ("0", "5");
3894 r0.intersect (r1);
3895 ASSERT_TRUE (real_iszero (&r0.lower_bound (), false));
3896
3897 r0 = frange_float ("-0", "5");
3898 r1 = frange_float ("0", "5");
3899 r0.union_ (r1);
3900 ASSERT_TRUE (real_iszero (&r0.lower_bound (), true));
3901
3902 r0 = frange_float ("-5", "-0");
3903 r0.update_nan ();
3904 r1 = frange_float ("0", "0");
3905 r1.update_nan ();
3906 r0.intersect (r1);
3907 if (HONOR_NANS (float_type_node))
3908 ASSERT_TRUE (r0.known_isnan ());
3909 else
3910 ASSERT_TRUE (r0.undefined_p ());
3911
3912 r0.set_nonnegative (float_type_node);
3913 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
3914 if (HONOR_NANS (float_type_node))
3915 ASSERT_TRUE (r0.maybe_isnan ());
3916 }
3917
3918 static void
3919 range_tests_signbit ()
3920 {
3921 frange r0, r1;
3922 bool signbit;
3923
3924 // Negative numbers should have the SIGNBIT set.
3925 r0 = frange_float ("-5", "-1");
3926 r0.clear_nan ();
3927 ASSERT_TRUE (r0.signbit_p (signbit) && signbit);
3928 // Positive numbers should have the SIGNBIT clear.
3929 r0 = frange_float ("1", "10");
3930 r0.clear_nan ();
3931 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
3932 // Numbers containing zero should have an unknown SIGNBIT.
3933 r0 = frange_float ("0", "10");
3934 r0.clear_nan ();
3935 ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
3936 // Numbers spanning both positive and negative should have an
3937 // unknown SIGNBIT.
3938 r0 = frange_float ("-10", "10");
3939 r0.clear_nan ();
3940 ASSERT_TRUE (!r0.signbit_p (signbit));
3941 r0.set_varying (float_type_node);
3942 ASSERT_TRUE (!r0.signbit_p (signbit));
3943 }
3944
3945 static void
3946 range_tests_floats ()
3947 {
3948 frange r0, r1;
3949
3950 if (HONOR_NANS (float_type_node))
3951 range_tests_nan ();
3952 range_tests_signbit ();
3953
3954 if (HONOR_SIGNED_ZEROS (float_type_node))
3955 range_tests_signed_zeros ();
3956
3957 // A range of [-INF,+INF] is actually VARYING if no other properties
3958 // are set.
3959 r0 = frange_float ("-Inf", "+Inf");
3960 if (r0.maybe_isnan ())
3961 ASSERT_TRUE (r0.varying_p ());
3962 // ...unless it has some special property...
3963 r0.clear_nan ();
3964 ASSERT_FALSE (r0.varying_p ());
3965
3966 // For most architectures, where float and double are different
3967 // sizes, having the same endpoints does not necessarily mean the
3968 // ranges are equal.
3969 if (!types_compatible_p (float_type_node, double_type_node))
3970 {
3971 r0 = frange_float ("3.0", "3.0", float_type_node);
3972 r1 = frange_float ("3.0", "3.0", double_type_node);
3973 ASSERT_NE (r0, r1);
3974 }
3975
3976 // [3,5] U [10,12] = [3,12].
3977 r0 = frange_float ("3", "5");
3978 r1 = frange_float ("10", "12");
3979 r0.union_ (r1);
3980 ASSERT_EQ (r0, frange_float ("3", "12"));
3981
3982 // [5,10] U [4,8] = [4,10]
3983 r0 = frange_float ("5", "10");
3984 r1 = frange_float ("4", "8");
3985 r0.union_ (r1);
3986 ASSERT_EQ (r0, frange_float ("4", "10"));
3987
3988 // [3,5] U [4,10] = [3,10]
3989 r0 = frange_float ("3", "5");
3990 r1 = frange_float ("4", "10");
3991 r0.union_ (r1);
3992 ASSERT_EQ (r0, frange_float ("3", "10"));
3993
3994 // [4,10] U [5,11] = [4,11]
3995 r0 = frange_float ("4", "10");
3996 r1 = frange_float ("5", "11");
3997 r0.union_ (r1);
3998 ASSERT_EQ (r0, frange_float ("4", "11"));
3999
4000 // [3,12] ^ [10,12] = [10,12].
4001 r0 = frange_float ("3", "12");
4002 r1 = frange_float ("10", "12");
4003 r0.intersect (r1);
4004 ASSERT_EQ (r0, frange_float ("10", "12"));
4005
4006 // [10,12] ^ [11,11] = [11,11]
4007 r0 = frange_float ("10", "12");
4008 r1 = frange_float ("11", "11");
4009 r0.intersect (r1);
4010 ASSERT_EQ (r0, frange_float ("11", "11"));
4011
4012 // [10,20] ^ [5,15] = [10,15]
4013 r0 = frange_float ("10", "20");
4014 r1 = frange_float ("5", "15");
4015 r0.intersect (r1);
4016 ASSERT_EQ (r0, frange_float ("10", "15"));
4017
4018 // [10,20] ^ [15,25] = [15,20]
4019 r0 = frange_float ("10", "20");
4020 r1 = frange_float ("15", "25");
4021 r0.intersect (r1);
4022 ASSERT_EQ (r0, frange_float ("15", "20"));
4023
4024 // [10,20] ^ [21,25] = []
4025 r0 = frange_float ("10", "20");
4026 r0.clear_nan ();
4027 r1 = frange_float ("21", "25");
4028 r1.clear_nan ();
4029 r0.intersect (r1);
4030 ASSERT_TRUE (r0.undefined_p ());
4031
4032 if (!flag_finite_math_only)
4033 {
4034 // Make sure [-Inf, -Inf] doesn't get normalized.
4035 r0 = frange_float ("-Inf", "-Inf");
4036 ASSERT_TRUE (real_isinf (&r0.lower_bound (), true));
4037 ASSERT_TRUE (real_isinf (&r0.upper_bound (), true));
4038 }
4039 }
4040
4041 void
4042 range_tests ()
4043 {
4044 range_tests_legacy ();
4045 range_tests_irange3 ();
4046 range_tests_int_range_max ();
4047 range_tests_strict_enum ();
4048 range_tests_nonzero_bits ();
4049 range_tests_floats ();
4050 range_tests_misc ();
4051 }
4052
4053 } // namespace selftest
4054
4055 #endif // CHECKING_P