Revise -mdisable-fpregs option and add new -msoft-mult option
[official-gcc.git] / gcc / value-range.cc
blobcaef249895942b875c45eb11c43df62943178e52
1 /* Support routines for value ranges.
2 Copyright (C) 2019-2021 Free Software Foundation, Inc.
3 Major hacks by Aldy Hernandez <aldyh@redhat.com> and
4 Andrew MacLeod <amacleod@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "ssa.h"
29 #include "tree-pretty-print.h"
30 #include "fold-const.h"
31 #include "gimple-range.h"
33 // Here we copy between any two irange's. The ranges can be legacy or
34 // multi-ranges, and copying between any combination works correctly.
36 irange &
37 irange::operator= (const irange &src)
39 if (legacy_mode_p ())
41 copy_to_legacy (src);
42 return *this;
44 if (src.legacy_mode_p ())
46 copy_legacy_to_multi_range (src);
47 return *this;
50 unsigned x;
51 unsigned lim = src.m_num_ranges;
52 if (lim > m_max_ranges)
53 lim = m_max_ranges;
55 for (x = 0; x < lim * 2; ++x)
56 m_base[x] = src.m_base[x];
58 // If the range didn't fit, the last range should cover the rest.
59 if (lim != src.m_num_ranges)
60 m_base[x - 1] = src.m_base[src.m_num_ranges * 2 - 1];
62 m_num_ranges = lim;
63 m_kind = src.m_kind;
64 return *this;
67 // Return TRUE if range is a multi-range that can be represented as a
68 // VR_ANTI_RANGE.
70 bool
71 irange::maybe_anti_range () const
73 tree ttype = type ();
74 unsigned int precision = TYPE_PRECISION (ttype);
75 signop sign = TYPE_SIGN (ttype);
76 return (num_pairs () > 1
77 && precision > 1
78 && lower_bound () == wi::min_value (precision, sign)
79 && upper_bound () == wi::max_value (precision, sign));
82 void
83 irange::copy_legacy_to_multi_range (const irange &src)
85 gcc_checking_assert (src.legacy_mode_p ());
86 gcc_checking_assert (!legacy_mode_p ());
87 if (src.undefined_p ())
88 set_undefined ();
89 else if (src.varying_p ())
90 set_varying (src.type ());
91 else
93 if (range_has_numeric_bounds_p (&src))
94 set (src.min (), src.max (), src.kind ());
95 else
97 value_range cst (src);
98 cst.normalize_symbolics ();
99 gcc_checking_assert (cst.varying_p () || cst.kind () == VR_RANGE);
100 set (cst.min (), cst.max ());
105 // Copy any type of irange into a legacy.
107 void
108 irange::copy_to_legacy (const irange &src)
110 gcc_checking_assert (legacy_mode_p ());
111 // Handle legacy to legacy and other things that are easy to copy.
112 if (src.legacy_mode_p () || src.varying_p () || src.undefined_p ())
114 m_num_ranges = src.m_num_ranges;
115 m_base[0] = src.m_base[0];
116 m_base[1] = src.m_base[1];
117 m_kind = src.m_kind;
118 return;
120 // Copy multi-range to legacy.
121 if (src.maybe_anti_range ())
123 int_range<3> r (src);
124 r.invert ();
125 // Use tree variants to save on tree -> wi -> tree conversions.
126 set (r.tree_lower_bound (0), r.tree_upper_bound (0), VR_ANTI_RANGE);
128 else
129 set (src.tree_lower_bound (), src.tree_upper_bound ());
132 // Swap MIN/MAX if they are out of order and adjust KIND appropriately.
134 static void
135 swap_out_of_order_endpoints (tree &min, tree &max, value_range_kind &kind)
137 gcc_checking_assert (kind != VR_UNDEFINED);
138 if (kind == VR_VARYING)
139 return;
140 /* Wrong order for min and max, to swap them and the VR type we need
141 to adjust them. */
142 if (tree_int_cst_lt (max, min))
144 tree one, tmp;
146 /* For one bit precision if max < min, then the swapped
147 range covers all values, so for VR_RANGE it is varying and
148 for VR_ANTI_RANGE empty range, so drop to varying as well. */
149 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
151 kind = VR_VARYING;
152 return;
155 one = build_int_cst (TREE_TYPE (min), 1);
156 tmp = int_const_binop (PLUS_EXPR, max, one);
157 max = int_const_binop (MINUS_EXPR, min, one);
158 min = tmp;
160 /* There's one corner case, if we had [C+1, C] before we now have
161 that again. But this represents an empty value range, so drop
162 to varying in this case. */
163 if (tree_int_cst_lt (max, min))
165 kind = VR_VARYING;
166 return;
168 kind = kind == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
172 void
173 irange::irange_set (tree min, tree max)
175 gcc_checking_assert (!POLY_INT_CST_P (min));
176 gcc_checking_assert (!POLY_INT_CST_P (max));
178 m_base[0] = min;
179 m_base[1] = max;
180 m_num_ranges = 1;
181 m_kind = VR_RANGE;
182 normalize_kind ();
184 if (flag_checking)
185 verify_range ();
188 void
189 irange::irange_set_1bit_anti_range (tree min, tree max)
191 tree type = TREE_TYPE (min);
192 gcc_checking_assert (TYPE_PRECISION (type) == 1);
194 if (operand_equal_p (min, max))
196 // Since these are 1-bit quantities, they can only be [MIN,MIN]
197 // or [MAX,MAX].
198 if (vrp_val_is_min (min))
199 min = max = vrp_val_max (type);
200 else
201 min = max = vrp_val_min (type);
202 set (min, max);
204 else
206 // The only alternative is [MIN,MAX], which is the empty range.
207 gcc_checking_assert (vrp_val_is_min (min));
208 gcc_checking_assert (vrp_val_is_max (max));
209 set_undefined ();
211 if (flag_checking)
212 verify_range ();
215 void
216 irange::irange_set_anti_range (tree min, tree max)
218 gcc_checking_assert (!POLY_INT_CST_P (min));
219 gcc_checking_assert (!POLY_INT_CST_P (max));
221 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
223 irange_set_1bit_anti_range (min, max);
224 return;
227 // set an anti-range
228 tree type = TREE_TYPE (min);
229 signop sign = TYPE_SIGN (type);
230 int_range<2> type_range (type);
231 // Calculate INVERSE([I,J]) as [-MIN, I-1][J+1, +MAX].
232 m_num_ranges = 0;
233 wi::overflow_type ovf;
235 wide_int w_min = wi::to_wide (min);
236 if (wi::ne_p (w_min, type_range.lower_bound ()))
238 wide_int lim1 = wi::sub (w_min, 1, sign, &ovf);
239 gcc_checking_assert (ovf != wi::OVF_OVERFLOW);
240 m_base[0] = type_range.tree_lower_bound (0);
241 m_base[1] = wide_int_to_tree (type, lim1);
242 m_num_ranges = 1;
244 wide_int w_max = wi::to_wide (max);
245 if (wi::ne_p (w_max, type_range.upper_bound ()))
247 wide_int lim2 = wi::add (w_max, 1, sign, &ovf);
248 gcc_checking_assert (ovf != wi::OVF_OVERFLOW);
249 m_base[m_num_ranges * 2] = wide_int_to_tree (type, lim2);
250 m_base[m_num_ranges * 2 + 1] = type_range.tree_upper_bound (0);
251 ++m_num_ranges;
254 m_kind = VR_RANGE;
255 normalize_kind ();
257 if (flag_checking)
258 verify_range ();
261 /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
262 This means adjusting VRTYPE, MIN and MAX representing the case of a
263 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
264 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
265 In corner cases where MAX+1 or MIN-1 wraps this will fall back
266 to varying.
267 This routine exists to ease canonicalization in the case where we
268 extract ranges from var + CST op limit. */
270 void
271 irange::set (tree min, tree max, value_range_kind kind)
273 if (!legacy_mode_p ())
275 if (kind == VR_RANGE)
276 irange_set (min, max);
277 else
279 gcc_checking_assert (kind == VR_ANTI_RANGE);
280 irange_set_anti_range (min, max);
282 return;
284 if (kind == VR_UNDEFINED)
286 set_undefined ();
287 return;
290 if (kind == VR_VARYING
291 || POLY_INT_CST_P (min)
292 || POLY_INT_CST_P (max))
294 set_varying (TREE_TYPE (min));
295 return;
298 // Nothing to canonicalize for symbolic ranges.
299 if (TREE_CODE (min) != INTEGER_CST
300 || TREE_CODE (max) != INTEGER_CST)
302 m_kind = kind;
303 m_base[0] = min;
304 m_base[1] = max;
305 m_num_ranges = 1;
306 return;
309 swap_out_of_order_endpoints (min, max, kind);
310 if (kind == VR_VARYING)
312 set_varying (TREE_TYPE (min));
313 return;
316 // Anti-ranges that can be represented as ranges should be so.
317 if (kind == VR_ANTI_RANGE)
319 bool is_min = vrp_val_is_min (min);
320 bool is_max = vrp_val_is_max (max);
322 if (is_min && is_max)
324 // Fall through. This will either be normalized as
325 // VR_UNDEFINED if the anti-range spans the entire
326 // precision, or it will remain an VR_ANTI_RANGE in the case
327 // of an -fstrict-enum where [MIN,MAX] is less than the span
328 // of underlying precision.
330 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
332 irange_set_1bit_anti_range (min, max);
333 return;
335 else if (is_min)
337 tree one = build_int_cst (TREE_TYPE (max), 1);
338 min = int_const_binop (PLUS_EXPR, max, one);
339 max = vrp_val_max (TREE_TYPE (max));
340 kind = VR_RANGE;
342 else if (is_max)
344 tree one = build_int_cst (TREE_TYPE (min), 1);
345 max = int_const_binop (MINUS_EXPR, min, one);
346 min = vrp_val_min (TREE_TYPE (min));
347 kind = VR_RANGE;
351 m_kind = kind;
352 m_base[0] = min;
353 m_base[1] = max;
354 m_num_ranges = 1;
355 normalize_kind ();
356 if (flag_checking)
357 verify_range ();
360 // Check the validity of the range.
362 void
363 irange::verify_range ()
365 if (m_kind == VR_UNDEFINED)
367 gcc_checking_assert (m_num_ranges == 0);
368 return;
370 if (m_kind == VR_VARYING)
372 gcc_checking_assert (m_num_ranges == 1);
373 gcc_checking_assert (varying_compatible_p ());
374 return;
376 if (!legacy_mode_p ())
378 gcc_checking_assert (m_num_ranges != 0);
379 gcc_checking_assert (!varying_compatible_p ());
380 for (unsigned i = 0; i < m_num_ranges; ++i)
382 tree lb = tree_lower_bound (i);
383 tree ub = tree_upper_bound (i);
384 int c = compare_values (lb, ub);
385 gcc_checking_assert (c == 0 || c == -1);
387 return;
389 if (m_kind == VR_RANGE || m_kind == VR_ANTI_RANGE)
391 gcc_checking_assert (m_num_ranges == 1);
392 int cmp = compare_values (tree_lower_bound (0), tree_upper_bound (0));
393 gcc_checking_assert (cmp == 0 || cmp == -1 || cmp == -2);
397 // Return the lower bound for a sub-range. PAIR is the sub-range in
398 // question.
400 wide_int
401 irange::legacy_lower_bound (unsigned pair) const
403 gcc_checking_assert (legacy_mode_p ());
404 if (symbolic_p ())
406 value_range numeric_range (*this);
407 numeric_range.normalize_symbolics ();
408 return numeric_range.legacy_lower_bound (pair);
410 gcc_checking_assert (m_num_ranges > 0);
411 gcc_checking_assert (pair + 1 <= num_pairs ());
412 if (m_kind == VR_ANTI_RANGE)
414 tree typ = type (), t;
415 if (pair == 1 || vrp_val_is_min (min ()))
416 t = wide_int_to_tree (typ, wi::to_wide (max ()) + 1);
417 else
418 t = vrp_val_min (typ);
419 return wi::to_wide (t);
421 return wi::to_wide (tree_lower_bound (pair));
424 // Return the upper bound for a sub-range. PAIR is the sub-range in
425 // question.
427 wide_int
428 irange::legacy_upper_bound (unsigned pair) const
430 gcc_checking_assert (legacy_mode_p ());
431 if (symbolic_p ())
433 value_range numeric_range (*this);
434 numeric_range.normalize_symbolics ();
435 return numeric_range.legacy_upper_bound (pair);
437 gcc_checking_assert (m_num_ranges > 0);
438 gcc_checking_assert (pair + 1 <= num_pairs ());
439 if (m_kind == VR_ANTI_RANGE)
441 tree typ = type (), t;
442 if (pair == 1 || vrp_val_is_min (min ()))
443 t = vrp_val_max (typ);
444 else
445 t = wide_int_to_tree (typ, wi::to_wide (min ()) - 1);
446 return wi::to_wide (t);
448 return wi::to_wide (tree_upper_bound (pair));
451 bool
452 irange::legacy_equal_p (const irange &other) const
454 gcc_checking_assert (legacy_mode_p () && other.legacy_mode_p ());
456 if (m_kind != other.m_kind)
457 return false;
458 if (m_kind == VR_UNDEFINED)
459 return true;
460 if (m_kind == VR_VARYING)
461 return range_compatible_p (type (), other.type ());
462 return (vrp_operand_equal_p (tree_lower_bound (0),
463 other.tree_lower_bound (0))
464 && vrp_operand_equal_p (tree_upper_bound (0),
465 other.tree_upper_bound (0)));
468 bool
469 irange::equal_p (const irange &other) const
471 if (legacy_mode_p ())
473 if (other.legacy_mode_p ())
474 return legacy_equal_p (other);
475 value_range tmp (other);
476 return legacy_equal_p (tmp);
478 if (other.legacy_mode_p ())
480 value_range tmp2 (*this);
481 return tmp2.legacy_equal_p (other);
484 if (m_num_ranges != other.m_num_ranges)
485 return false;
487 for (unsigned i = 0; i < m_num_ranges; ++i)
489 tree lb = tree_lower_bound (i);
490 tree ub = tree_upper_bound (i);
491 tree lb_other = other.tree_lower_bound (i);
492 tree ub_other = other.tree_upper_bound (i);
493 if (!operand_equal_p (lb, lb_other, 0)
494 || !operand_equal_p (ub, ub_other, 0))
495 return false;
497 return true;
500 /* Return TRUE if this is a symbolic range. */
502 bool
503 irange::symbolic_p () const
505 return (m_num_ranges > 0
506 && (!is_gimple_min_invariant (min ())
507 || !is_gimple_min_invariant (max ())));
510 /* Return TRUE if this is a constant range. */
512 bool
513 irange::constant_p () const
515 return (m_num_ranges > 0
516 && TREE_CODE (min ()) == INTEGER_CST
517 && TREE_CODE (max ()) == INTEGER_CST);
520 /* If range is a singleton, place it in RESULT and return TRUE.
521 Note: A singleton can be any gimple invariant, not just constants.
522 So, [&x, &x] counts as a singleton. */
524 bool
525 irange::singleton_p (tree *result) const
527 if (!legacy_mode_p ())
529 if (num_pairs () == 1 && (wi::to_wide (tree_lower_bound ())
530 == wi::to_wide (tree_upper_bound ())))
532 if (result)
533 *result = tree_lower_bound ();
534 return true;
536 return false;
538 if (m_kind == VR_ANTI_RANGE)
540 if (nonzero_p ())
542 if (TYPE_PRECISION (type ()) == 1)
544 if (result)
545 *result = max ();
546 return true;
548 return false;
550 if (num_pairs () == 1)
552 value_range vr0, vr1;
553 ranges_from_anti_range ((const value_range *) this, &vr0, &vr1);
554 return vr0.singleton_p (result);
557 // Catches non-numeric extremes as well.
558 if (m_kind == VR_RANGE
559 && vrp_operand_equal_p (min (), max ())
560 && is_gimple_min_invariant (min ()))
562 if (result)
563 *result = min ();
564 return true;
566 return false;
569 /* Return 1 if VAL is inside value range.
570 0 if VAL is not inside value range.
571 -2 if we cannot tell either way.
573 Benchmark compile/20001226-1.c compilation time after changing this
574 function. */
577 irange::value_inside_range (tree val) const
579 if (varying_p ())
580 return 1;
582 if (undefined_p ())
583 return 0;
585 if (!legacy_mode_p () && TREE_CODE (val) == INTEGER_CST)
586 return contains_p (val);
588 int cmp1 = operand_less_p (val, min ());
589 if (cmp1 == -2)
590 return -2;
591 if (cmp1 == 1)
592 return m_kind != VR_RANGE;
594 int cmp2 = operand_less_p (max (), val);
595 if (cmp2 == -2)
596 return -2;
598 if (m_kind == VR_RANGE)
599 return !cmp2;
600 else
601 return !!cmp2;
604 /* Return TRUE if it is possible that range contains VAL. */
606 bool
607 irange::may_contain_p (tree val) const
609 return value_inside_range (val) != 0;
612 /* Return TRUE if range contains INTEGER_CST. */
613 /* Return 1 if VAL is inside value range.
614 0 if VAL is not inside value range.
616 Benchmark compile/20001226-1.c compilation time after changing this
617 function. */
620 bool
621 irange::contains_p (tree cst) const
623 if (undefined_p ())
624 return false;
626 if (legacy_mode_p ())
628 gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST);
629 if (symbolic_p ())
631 value_range numeric_range (*this);
632 numeric_range.normalize_symbolics ();
633 return numeric_range.contains_p (cst);
635 return value_inside_range (cst) == 1;
638 gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST);
639 signop sign = TYPE_SIGN (TREE_TYPE (cst));
640 wide_int v = wi::to_wide (cst);
641 for (unsigned r = 0; r < m_num_ranges; ++r)
643 if (wi::lt_p (v, lower_bound (r), sign))
644 return false;
645 if (wi::le_p (v, upper_bound (r), sign))
646 return true;
649 return false;
653 /* Normalize addresses into constants. */
655 void
656 irange::normalize_addresses ()
658 if (undefined_p ())
659 return;
661 if (!POINTER_TYPE_P (type ()) || range_has_numeric_bounds_p (this))
662 return;
664 if (!range_includes_zero_p (this))
666 gcc_checking_assert (TREE_CODE (min ()) == ADDR_EXPR
667 || TREE_CODE (max ()) == ADDR_EXPR);
668 set_nonzero (type ());
669 return;
671 set_varying (type ());
674 /* Normalize symbolics and addresses into constants. */
676 void
677 irange::normalize_symbolics ()
679 if (varying_p () || undefined_p ())
680 return;
682 tree ttype = type ();
683 bool min_symbolic = !is_gimple_min_invariant (min ());
684 bool max_symbolic = !is_gimple_min_invariant (max ());
685 if (!min_symbolic && !max_symbolic)
687 normalize_addresses ();
688 return;
691 // [SYM, SYM] -> VARYING
692 if (min_symbolic && max_symbolic)
694 set_varying (ttype);
695 return;
697 if (kind () == VR_RANGE)
699 // [SYM, NUM] -> [-MIN, NUM]
700 if (min_symbolic)
702 set (vrp_val_min (ttype), max ());
703 return;
705 // [NUM, SYM] -> [NUM, +MAX]
706 set (min (), vrp_val_max (ttype));
707 return;
709 gcc_checking_assert (kind () == VR_ANTI_RANGE);
710 // ~[SYM, NUM] -> [NUM + 1, +MAX]
711 if (min_symbolic)
713 if (!vrp_val_is_max (max ()))
715 tree n = wide_int_to_tree (ttype, wi::to_wide (max ()) + 1);
716 set (n, vrp_val_max (ttype));
717 return;
719 set_varying (ttype);
720 return;
722 // ~[NUM, SYM] -> [-MIN, NUM - 1]
723 if (!vrp_val_is_min (min ()))
725 tree n = wide_int_to_tree (ttype, wi::to_wide (min ()) - 1);
726 set (vrp_val_min (ttype), n);
727 return;
729 set_varying (ttype);
732 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
733 { VR1TYPE, VR0MIN, VR0MAX } and store the result
734 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
735 possible such range. The resulting range is not canonicalized. */
737 static void
738 intersect_ranges (enum value_range_kind *vr0type,
739 tree *vr0min, tree *vr0max,
740 enum value_range_kind vr1type,
741 tree vr1min, tree vr1max)
743 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
744 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
746 /* [] is vr0, () is vr1 in the following classification comments. */
747 if (mineq && maxeq)
749 /* [( )] */
750 if (*vr0type == vr1type)
751 /* Nothing to do for equal ranges. */
753 else if ((*vr0type == VR_RANGE
754 && vr1type == VR_ANTI_RANGE)
755 || (*vr0type == VR_ANTI_RANGE
756 && vr1type == VR_RANGE))
758 /* For anti-range with range intersection the result is empty. */
759 *vr0type = VR_UNDEFINED;
760 *vr0min = NULL_TREE;
761 *vr0max = NULL_TREE;
763 else
764 gcc_unreachable ();
766 else if (operand_less_p (*vr0max, vr1min) == 1
767 || operand_less_p (vr1max, *vr0min) == 1)
769 /* [ ] ( ) or ( ) [ ]
770 If the ranges have an empty intersection, the result of the
771 intersect operation is the range for intersecting an
772 anti-range with a range or empty when intersecting two ranges. */
773 if (*vr0type == VR_RANGE
774 && vr1type == VR_ANTI_RANGE)
776 else if (*vr0type == VR_ANTI_RANGE
777 && vr1type == VR_RANGE)
779 *vr0type = vr1type;
780 *vr0min = vr1min;
781 *vr0max = vr1max;
783 else if (*vr0type == VR_RANGE
784 && vr1type == VR_RANGE)
786 *vr0type = VR_UNDEFINED;
787 *vr0min = NULL_TREE;
788 *vr0max = NULL_TREE;
790 else if (*vr0type == VR_ANTI_RANGE
791 && vr1type == VR_ANTI_RANGE)
793 /* If the anti-ranges are adjacent to each other merge them. */
794 if (TREE_CODE (*vr0max) == INTEGER_CST
795 && TREE_CODE (vr1min) == INTEGER_CST
796 && operand_less_p (*vr0max, vr1min) == 1
797 && integer_onep (int_const_binop (MINUS_EXPR,
798 vr1min, *vr0max)))
799 *vr0max = vr1max;
800 else if (TREE_CODE (vr1max) == INTEGER_CST
801 && TREE_CODE (*vr0min) == INTEGER_CST
802 && operand_less_p (vr1max, *vr0min) == 1
803 && integer_onep (int_const_binop (MINUS_EXPR,
804 *vr0min, vr1max)))
805 *vr0min = vr1min;
806 /* Else arbitrarily take VR0. */
809 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
810 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
812 /* [ ( ) ] or [( ) ] or [ ( )] */
813 if (*vr0type == VR_RANGE
814 && vr1type == VR_RANGE)
816 /* If both are ranges the result is the inner one. */
817 *vr0type = vr1type;
818 *vr0min = vr1min;
819 *vr0max = vr1max;
821 else if (*vr0type == VR_RANGE
822 && vr1type == VR_ANTI_RANGE)
824 /* Choose the right gap if the left one is empty. */
825 if (mineq)
827 if (TREE_CODE (vr1max) != INTEGER_CST)
828 *vr0min = vr1max;
829 else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1
830 && !TYPE_UNSIGNED (TREE_TYPE (vr1max)))
831 *vr0min
832 = int_const_binop (MINUS_EXPR, vr1max,
833 build_int_cst (TREE_TYPE (vr1max), -1));
834 else
835 *vr0min
836 = int_const_binop (PLUS_EXPR, vr1max,
837 build_int_cst (TREE_TYPE (vr1max), 1));
839 /* Choose the left gap if the right one is empty. */
840 else if (maxeq)
842 if (TREE_CODE (vr1min) != INTEGER_CST)
843 *vr0max = vr1min;
844 else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1
845 && !TYPE_UNSIGNED (TREE_TYPE (vr1min)))
846 *vr0max
847 = int_const_binop (PLUS_EXPR, vr1min,
848 build_int_cst (TREE_TYPE (vr1min), -1));
849 else
850 *vr0max
851 = int_const_binop (MINUS_EXPR, vr1min,
852 build_int_cst (TREE_TYPE (vr1min), 1));
854 /* Choose the anti-range if the range is effectively varying. */
855 else if (vrp_val_is_min (*vr0min)
856 && vrp_val_is_max (*vr0max))
858 *vr0type = vr1type;
859 *vr0min = vr1min;
860 *vr0max = vr1max;
862 /* Else choose the range. */
864 else if (*vr0type == VR_ANTI_RANGE
865 && vr1type == VR_ANTI_RANGE)
866 /* If both are anti-ranges the result is the outer one. */
868 else if (*vr0type == VR_ANTI_RANGE
869 && vr1type == VR_RANGE)
871 /* The intersection is empty. */
872 *vr0type = VR_UNDEFINED;
873 *vr0min = NULL_TREE;
874 *vr0max = NULL_TREE;
876 else
877 gcc_unreachable ();
879 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
880 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
882 /* ( [ ] ) or ([ ] ) or ( [ ]) */
883 if (*vr0type == VR_RANGE
884 && vr1type == VR_RANGE)
885 /* Choose the inner range. */
887 else if (*vr0type == VR_ANTI_RANGE
888 && vr1type == VR_RANGE)
890 /* Choose the right gap if the left is empty. */
891 if (mineq)
893 *vr0type = VR_RANGE;
894 if (TREE_CODE (*vr0max) != INTEGER_CST)
895 *vr0min = *vr0max;
896 else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1
897 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max)))
898 *vr0min
899 = int_const_binop (MINUS_EXPR, *vr0max,
900 build_int_cst (TREE_TYPE (*vr0max), -1));
901 else
902 *vr0min
903 = int_const_binop (PLUS_EXPR, *vr0max,
904 build_int_cst (TREE_TYPE (*vr0max), 1));
905 *vr0max = vr1max;
907 /* Choose the left gap if the right is empty. */
908 else if (maxeq)
910 *vr0type = VR_RANGE;
911 if (TREE_CODE (*vr0min) != INTEGER_CST)
912 *vr0max = *vr0min;
913 else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1
914 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min)))
915 *vr0max
916 = int_const_binop (PLUS_EXPR, *vr0min,
917 build_int_cst (TREE_TYPE (*vr0min), -1));
918 else
919 *vr0max
920 = int_const_binop (MINUS_EXPR, *vr0min,
921 build_int_cst (TREE_TYPE (*vr0min), 1));
922 *vr0min = vr1min;
924 /* Choose the anti-range if the range is effectively varying. */
925 else if (vrp_val_is_min (vr1min)
926 && vrp_val_is_max (vr1max))
928 /* Choose the anti-range if it is ~[0,0], that range is special
929 enough to special case when vr1's range is relatively wide.
930 At least for types bigger than int - this covers pointers
931 and arguments to functions like ctz. */
932 else if (*vr0min == *vr0max
933 && integer_zerop (*vr0min)
934 && ((TYPE_PRECISION (TREE_TYPE (*vr0min))
935 >= TYPE_PRECISION (integer_type_node))
936 || POINTER_TYPE_P (TREE_TYPE (*vr0min)))
937 && TREE_CODE (vr1max) == INTEGER_CST
938 && TREE_CODE (vr1min) == INTEGER_CST
939 && (wi::clz (wi::to_wide (vr1max) - wi::to_wide (vr1min))
940 < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2))
942 /* Else choose the range. */
943 else
945 *vr0type = vr1type;
946 *vr0min = vr1min;
947 *vr0max = vr1max;
950 else if (*vr0type == VR_ANTI_RANGE
951 && vr1type == VR_ANTI_RANGE)
953 /* If both are anti-ranges the result is the outer one. */
954 *vr0type = vr1type;
955 *vr0min = vr1min;
956 *vr0max = vr1max;
958 else if (vr1type == VR_ANTI_RANGE
959 && *vr0type == VR_RANGE)
961 /* The intersection is empty. */
962 *vr0type = VR_UNDEFINED;
963 *vr0min = NULL_TREE;
964 *vr0max = NULL_TREE;
966 else
967 gcc_unreachable ();
969 else if ((operand_less_p (vr1min, *vr0max) == 1
970 || operand_equal_p (vr1min, *vr0max, 0))
971 && operand_less_p (*vr0min, vr1min) == 1
972 && operand_less_p (*vr0max, vr1max) == 1)
974 /* [ ( ] ) or [ ]( ) */
975 if (*vr0type == VR_ANTI_RANGE
976 && vr1type == VR_ANTI_RANGE)
977 *vr0max = vr1max;
978 else if (*vr0type == VR_RANGE
979 && vr1type == VR_RANGE)
980 *vr0min = vr1min;
981 else if (*vr0type == VR_RANGE
982 && vr1type == VR_ANTI_RANGE)
984 if (TREE_CODE (vr1min) == INTEGER_CST)
985 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
986 build_int_cst (TREE_TYPE (vr1min), 1));
987 else
988 *vr0max = vr1min;
990 else if (*vr0type == VR_ANTI_RANGE
991 && vr1type == VR_RANGE)
993 *vr0type = VR_RANGE;
994 if (TREE_CODE (*vr0max) == INTEGER_CST)
995 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
996 build_int_cst (TREE_TYPE (*vr0max), 1));
997 else
998 *vr0min = *vr0max;
999 *vr0max = vr1max;
1001 else
1002 gcc_unreachable ();
1004 else if ((operand_less_p (*vr0min, vr1max) == 1
1005 || operand_equal_p (*vr0min, vr1max, 0))
1006 && operand_less_p (vr1min, *vr0min) == 1
1007 && operand_less_p (vr1max, *vr0max) == 1)
1009 /* ( [ ) ] or ( )[ ] */
1010 if (*vr0type == VR_ANTI_RANGE
1011 && vr1type == VR_ANTI_RANGE)
1012 *vr0min = vr1min;
1013 else if (*vr0type == VR_RANGE
1014 && vr1type == VR_RANGE)
1015 *vr0max = vr1max;
1016 else if (*vr0type == VR_RANGE
1017 && vr1type == VR_ANTI_RANGE)
1019 if (TREE_CODE (vr1max) == INTEGER_CST)
1020 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
1021 build_int_cst (TREE_TYPE (vr1max), 1));
1022 else
1023 *vr0min = vr1max;
1025 else if (*vr0type == VR_ANTI_RANGE
1026 && vr1type == VR_RANGE)
1028 *vr0type = VR_RANGE;
1029 if (TREE_CODE (*vr0min) == INTEGER_CST)
1030 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
1031 build_int_cst (TREE_TYPE (*vr0min), 1));
1032 else
1033 *vr0max = *vr0min;
1034 *vr0min = vr1min;
1036 else
1037 gcc_unreachable ();
1040 /* If we know the intersection is empty, there's no need to
1041 conservatively add anything else to the set. */
1042 if (*vr0type == VR_UNDEFINED)
1043 return;
1045 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
1046 result for the intersection. That's always a conservative
1047 correct estimate unless VR1 is a constant singleton range
1048 in which case we choose that. */
1049 if (vr1type == VR_RANGE
1050 && is_gimple_min_invariant (vr1min)
1051 && vrp_operand_equal_p (vr1min, vr1max))
1053 *vr0type = vr1type;
1054 *vr0min = vr1min;
1055 *vr0max = vr1max;
1059 /* Helper for the intersection operation for value ranges. Given two
1060 ranges VR0 and VR1, set VR0 to the intersection of both ranges.
1061 This may not be the smallest possible such range. */
1063 void
1064 irange::legacy_intersect (irange *vr0, const irange *vr1)
1066 gcc_checking_assert (vr0->legacy_mode_p ());
1067 gcc_checking_assert (vr1->legacy_mode_p ());
1068 /* If either range is VR_VARYING the other one wins. */
1069 if (vr1->varying_p ())
1070 return;
1071 if (vr0->varying_p ())
1073 vr0->set (vr1->min (), vr1->max (), vr1->kind ());
1074 return;
1077 /* When either range is VR_UNDEFINED the resulting range is
1078 VR_UNDEFINED, too. */
1079 if (vr0->undefined_p ())
1080 return;
1081 if (vr1->undefined_p ())
1083 vr0->set_undefined ();
1084 return;
1087 value_range_kind vr0kind = vr0->kind ();
1088 tree vr0min = vr0->min ();
1089 tree vr0max = vr0->max ();
1091 intersect_ranges (&vr0kind, &vr0min, &vr0max,
1092 vr1->kind (), vr1->min (), vr1->max ());
1094 /* Make sure to canonicalize the result though as the inversion of a
1095 VR_RANGE can still be a VR_RANGE. */
1096 if (vr0kind == VR_UNDEFINED)
1097 vr0->set_undefined ();
1098 else if (vr0kind == VR_VARYING)
1100 /* If we failed, use the original VR0. */
1101 return;
1103 else
1104 vr0->set (vr0min, vr0max, vr0kind);
1107 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
1108 { VR1TYPE, VR0MIN, VR0MAX } and store the result
1109 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
1110 possible such range. The resulting range is not canonicalized. */
1112 static void
1113 union_ranges (enum value_range_kind *vr0type,
1114 tree *vr0min, tree *vr0max,
1115 enum value_range_kind vr1type,
1116 tree vr1min, tree vr1max)
1118 int cmpmin = compare_values (*vr0min, vr1min);
1119 int cmpmax = compare_values (*vr0max, vr1max);
1120 bool mineq = cmpmin == 0;
1121 bool maxeq = cmpmax == 0;
1123 /* [] is vr0, () is vr1 in the following classification comments. */
1124 if (mineq && maxeq)
1126 /* [( )] */
1127 if (*vr0type == vr1type)
1128 /* Nothing to do for equal ranges. */
1130 else if ((*vr0type == VR_RANGE
1131 && vr1type == VR_ANTI_RANGE)
1132 || (*vr0type == VR_ANTI_RANGE
1133 && vr1type == VR_RANGE))
1135 /* For anti-range with range union the result is varying. */
1136 goto give_up;
1138 else
1139 gcc_unreachable ();
1141 else if (operand_less_p (*vr0max, vr1min) == 1
1142 || operand_less_p (vr1max, *vr0min) == 1)
1144 /* [ ] ( ) or ( ) [ ]
1145 If the ranges have an empty intersection, result of the union
1146 operation is the anti-range or if both are anti-ranges
1147 it covers all. */
1148 if (*vr0type == VR_ANTI_RANGE
1149 && vr1type == VR_ANTI_RANGE)
1150 goto give_up;
1151 else if (*vr0type == VR_ANTI_RANGE
1152 && vr1type == VR_RANGE)
1154 else if (*vr0type == VR_RANGE
1155 && vr1type == VR_ANTI_RANGE)
1157 *vr0type = vr1type;
1158 *vr0min = vr1min;
1159 *vr0max = vr1max;
1161 else if (*vr0type == VR_RANGE
1162 && vr1type == VR_RANGE)
1164 /* The result is the convex hull of both ranges. */
1165 if (operand_less_p (*vr0max, vr1min) == 1)
1167 /* If the result can be an anti-range, create one. */
1168 if (TREE_CODE (*vr0max) == INTEGER_CST
1169 && TREE_CODE (vr1min) == INTEGER_CST
1170 && vrp_val_is_min (*vr0min)
1171 && vrp_val_is_max (vr1max))
1173 tree min = int_const_binop (PLUS_EXPR,
1174 *vr0max,
1175 build_int_cst (TREE_TYPE (*vr0max), 1));
1176 tree max = int_const_binop (MINUS_EXPR,
1177 vr1min,
1178 build_int_cst (TREE_TYPE (vr1min), 1));
1179 if (!operand_less_p (max, min))
1181 *vr0type = VR_ANTI_RANGE;
1182 *vr0min = min;
1183 *vr0max = max;
1185 else
1186 *vr0max = vr1max;
1188 else
1189 *vr0max = vr1max;
1191 else
1193 /* If the result can be an anti-range, create one. */
1194 if (TREE_CODE (vr1max) == INTEGER_CST
1195 && TREE_CODE (*vr0min) == INTEGER_CST
1196 && vrp_val_is_min (vr1min)
1197 && vrp_val_is_max (*vr0max))
1199 tree min = int_const_binop (PLUS_EXPR,
1200 vr1max,
1201 build_int_cst (TREE_TYPE (vr1max), 1));
1202 tree max = int_const_binop (MINUS_EXPR,
1203 *vr0min,
1204 build_int_cst (TREE_TYPE (*vr0min), 1));
1205 if (!operand_less_p (max, min))
1207 *vr0type = VR_ANTI_RANGE;
1208 *vr0min = min;
1209 *vr0max = max;
1211 else
1212 *vr0min = vr1min;
1214 else
1215 *vr0min = vr1min;
1218 else
1219 gcc_unreachable ();
1221 else if ((maxeq || cmpmax == 1)
1222 && (mineq || cmpmin == -1))
1224 /* [ ( ) ] or [( ) ] or [ ( )] */
1225 if (*vr0type == VR_RANGE
1226 && vr1type == VR_RANGE)
1228 else if (*vr0type == VR_ANTI_RANGE
1229 && vr1type == VR_ANTI_RANGE)
1231 *vr0type = vr1type;
1232 *vr0min = vr1min;
1233 *vr0max = vr1max;
1235 else if (*vr0type == VR_ANTI_RANGE
1236 && vr1type == VR_RANGE)
1238 /* Arbitrarily choose the right or left gap. */
1239 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
1240 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
1241 build_int_cst (TREE_TYPE (vr1min), 1));
1242 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
1243 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
1244 build_int_cst (TREE_TYPE (vr1max), 1));
1245 else
1246 goto give_up;
1248 else if (*vr0type == VR_RANGE
1249 && vr1type == VR_ANTI_RANGE)
1250 /* The result covers everything. */
1251 goto give_up;
1252 else
1253 gcc_unreachable ();
1255 else if ((maxeq || cmpmax == -1)
1256 && (mineq || cmpmin == 1))
1258 /* ( [ ] ) or ([ ] ) or ( [ ]) */
1259 if (*vr0type == VR_RANGE
1260 && vr1type == VR_RANGE)
1262 *vr0type = vr1type;
1263 *vr0min = vr1min;
1264 *vr0max = vr1max;
1266 else if (*vr0type == VR_ANTI_RANGE
1267 && vr1type == VR_ANTI_RANGE)
1269 else if (*vr0type == VR_RANGE
1270 && vr1type == VR_ANTI_RANGE)
1272 *vr0type = VR_ANTI_RANGE;
1273 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
1275 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
1276 build_int_cst (TREE_TYPE (*vr0min), 1));
1277 *vr0min = vr1min;
1279 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
1281 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
1282 build_int_cst (TREE_TYPE (*vr0max), 1));
1283 *vr0max = vr1max;
1285 else
1286 goto give_up;
1288 else if (*vr0type == VR_ANTI_RANGE
1289 && vr1type == VR_RANGE)
1290 /* The result covers everything. */
1291 goto give_up;
1292 else
1293 gcc_unreachable ();
1295 else if (cmpmin == -1
1296 && cmpmax == -1
1297 && (operand_less_p (vr1min, *vr0max) == 1
1298 || operand_equal_p (vr1min, *vr0max, 0)))
1300 /* [ ( ] ) or [ ]( ) */
1301 if (*vr0type == VR_RANGE
1302 && vr1type == VR_RANGE)
1303 *vr0max = vr1max;
1304 else if (*vr0type == VR_ANTI_RANGE
1305 && vr1type == VR_ANTI_RANGE)
1306 *vr0min = vr1min;
1307 else if (*vr0type == VR_ANTI_RANGE
1308 && vr1type == VR_RANGE)
1310 if (TREE_CODE (vr1min) == INTEGER_CST)
1311 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
1312 build_int_cst (TREE_TYPE (vr1min), 1));
1313 else
1314 goto give_up;
1316 else if (*vr0type == VR_RANGE
1317 && vr1type == VR_ANTI_RANGE)
1319 if (TREE_CODE (*vr0max) == INTEGER_CST)
1321 *vr0type = vr1type;
1322 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
1323 build_int_cst (TREE_TYPE (*vr0max), 1));
1324 *vr0max = vr1max;
1326 else
1327 goto give_up;
1329 else
1330 gcc_unreachable ();
1332 else if (cmpmin == 1
1333 && cmpmax == 1
1334 && (operand_less_p (*vr0min, vr1max) == 1
1335 || operand_equal_p (*vr0min, vr1max, 0)))
1337 /* ( [ ) ] or ( )[ ] */
1338 if (*vr0type == VR_RANGE
1339 && vr1type == VR_RANGE)
1340 *vr0min = vr1min;
1341 else if (*vr0type == VR_ANTI_RANGE
1342 && vr1type == VR_ANTI_RANGE)
1343 *vr0max = vr1max;
1344 else if (*vr0type == VR_ANTI_RANGE
1345 && vr1type == VR_RANGE)
1347 if (TREE_CODE (vr1max) == INTEGER_CST)
1348 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
1349 build_int_cst (TREE_TYPE (vr1max), 1));
1350 else
1351 goto give_up;
1353 else if (*vr0type == VR_RANGE
1354 && vr1type == VR_ANTI_RANGE)
1356 if (TREE_CODE (*vr0min) == INTEGER_CST)
1358 *vr0type = vr1type;
1359 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
1360 build_int_cst (TREE_TYPE (*vr0min), 1));
1361 *vr0min = vr1min;
1363 else
1364 goto give_up;
1366 else
1367 gcc_unreachable ();
1369 else
1370 goto give_up;
1372 return;
1374 give_up:
1375 *vr0type = VR_VARYING;
1376 *vr0min = NULL_TREE;
1377 *vr0max = NULL_TREE;
1380 /* Helper for meet operation for value ranges. Given two ranges VR0
1381 and VR1, set VR0 to the union of both ranges. This may not be the
1382 smallest possible such range. */
1384 void
1385 irange::legacy_union (irange *vr0, const irange *vr1)
1387 gcc_checking_assert (vr0->legacy_mode_p ());
1388 gcc_checking_assert (vr1->legacy_mode_p ());
1390 /* VR0 has the resulting range if VR1 is undefined or VR0 is varying. */
1391 if (vr1->undefined_p ()
1392 || vr0->varying_p ())
1393 return;
1395 /* VR1 has the resulting range if VR0 is undefined or VR1 is varying. */
1396 if (vr0->undefined_p ())
1398 vr0->set (vr1->min (), vr1->max (), vr1->kind ());
1399 return;
1402 if (vr1->varying_p ())
1404 vr0->set_varying (vr1->type ());
1405 return;
1408 value_range_kind vr0kind = vr0->kind ();
1409 tree vr0min = vr0->min ();
1410 tree vr0max = vr0->max ();
1412 union_ranges (&vr0kind, &vr0min, &vr0max,
1413 vr1->kind (), vr1->min (), vr1->max ());
1415 if (vr0kind == VR_UNDEFINED)
1416 vr0->set_undefined ();
1417 else if (vr0kind == VR_VARYING)
1419 /* Failed to find an efficient meet. Before giving up and
1420 setting the result to VARYING, see if we can at least derive
1421 a non-zero range. */
1422 if (range_includes_zero_p (vr0) == 0
1423 && range_includes_zero_p (vr1) == 0)
1424 vr0->set_nonzero (vr0->type ());
1425 else
1426 vr0->set_varying (vr0->type ());
1428 else
1429 vr0->set (vr0min, vr0max, vr0kind);
1432 /* Meet operation for value ranges. Given two value ranges VR0 and
1433 VR1, store in VR0 a range that contains both VR0 and VR1. This
1434 may not be the smallest possible such range. */
1436 void
1437 irange::union_ (const irange *other)
1439 if (legacy_mode_p ())
1441 if (!other->legacy_mode_p ())
1443 int_range<1> tmp = *other;
1444 legacy_union (this, &tmp);
1445 return;
1447 if (dump_file && (dump_flags & TDF_DETAILS))
1449 fprintf (dump_file, "Meeting\n ");
1450 dump_value_range (dump_file, this);
1451 fprintf (dump_file, "\nand\n ");
1452 dump_value_range (dump_file, other);
1453 fprintf (dump_file, "\n");
1456 legacy_union (this, other);
1458 if (dump_file && (dump_flags & TDF_DETAILS))
1460 fprintf (dump_file, "to\n ");
1461 dump_value_range (dump_file, this);
1462 fprintf (dump_file, "\n");
1464 return;
1467 if (other->legacy_mode_p ())
1469 int_range<2> wider = *other;
1470 irange_union (wider);
1472 else
1473 irange_union (*other);
1476 void
1477 irange::intersect (const irange *other)
1479 if (legacy_mode_p ())
1481 if (!other->legacy_mode_p ())
1483 int_range<1> tmp = *other;
1484 legacy_intersect (this, &tmp);
1485 return;
1487 if (dump_file && (dump_flags & TDF_DETAILS))
1489 fprintf (dump_file, "Intersecting\n ");
1490 dump_value_range (dump_file, this);
1491 fprintf (dump_file, "\nand\n ");
1492 dump_value_range (dump_file, other);
1493 fprintf (dump_file, "\n");
1496 legacy_intersect (this, other);
1498 if (dump_file && (dump_flags & TDF_DETAILS))
1500 fprintf (dump_file, "to\n ");
1501 dump_value_range (dump_file, this);
1502 fprintf (dump_file, "\n");
1504 return;
1507 if (other->legacy_mode_p ())
1509 int_range<2> wider;
1510 wider = *other;
1511 irange_intersect (wider);
1513 else
1514 irange_intersect (*other);
1517 // union_ for multi-ranges.
1519 void
1520 irange::irange_union (const irange &r)
1522 gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ());
1524 if (r.undefined_p () || varying_p ())
1525 return;
1527 if (undefined_p () || r.varying_p ())
1529 operator= (r);
1530 return;
1533 // Do not worry about merging and such by reserving twice as many
1534 // pairs as needed, and then simply sort the 2 ranges into this
1535 // intermediate form.
1537 // The intermediate result will have the property that the beginning
1538 // of each range is <= the beginning of the next range. There may
1539 // be overlapping ranges at this point. I.e. this would be valid
1540 // [-20, 10], [-10, 0], [0, 20], [40, 90] as it satisfies this
1541 // contraint : -20 < -10 < 0 < 40. When the range is rebuilt into r,
1542 // the merge is performed.
1544 // [Xi,Yi]..[Xn,Yn] U [Xj,Yj]..[Xm,Ym] --> [Xk,Yk]..[Xp,Yp]
1545 tree ttype = r.type ();
1546 signop sign = TYPE_SIGN (ttype);
1548 auto_vec<tree, 20> res;
1549 wide_int u1 ;
1550 wi::overflow_type ovf;
1551 unsigned i = 0, j = 0, k = 0;
1553 while (i < m_num_ranges * 2 && j < r.m_num_ranges * 2)
1555 // lower of Xi and Xj is the lowest point.
1556 if (wi::le_p (wi::to_wide (m_base[i]), wi::to_wide (r.m_base[j]), sign))
1558 res.safe_push (m_base[i]);
1559 res.safe_push (m_base[i + 1]);
1560 k += 2;
1561 i += 2;
1563 else
1565 res.safe_push (r.m_base[j]);
1566 res.safe_push (r.m_base[j + 1]);
1567 k += 2;
1568 j += 2;
1571 for ( ; i < m_num_ranges * 2; i += 2)
1573 res.safe_push (m_base[i]);
1574 res.safe_push (m_base[i + 1]);
1575 k += 2;
1577 for ( ; j < r.m_num_ranges * 2; j += 2)
1579 res.safe_push (r.m_base[j]);
1580 res.safe_push (r.m_base[j + 1]);
1581 k += 2;
1584 // Now normalize the vector removing any overlaps.
1585 i = 2;
1586 int prec = TYPE_PRECISION (ttype);
1587 wide_int max_val = wi::max_value (prec, sign);
1588 for (j = 2; j < k ; j += 2)
1590 wide_int val_im1 = wi::to_wide (res[i - 1]);
1591 if (val_im1 == max_val)
1592 break;
1593 u1 = wi::add (val_im1, 1, sign, &ovf);
1595 // Overflow indicates we are at MAX already.
1596 // A wide int bug requires the previous max_val check
1597 // trigger: gcc.c-torture/compile/pr80443.c with -O3
1598 if (ovf == wi::OVF_OVERFLOW)
1599 break;
1601 wide_int val_j = wi::to_wide (res[j]);
1602 wide_int val_jp1 = wi::to_wide (res[j+1]);
1603 // Current upper+1 is >= lower bound next pair, then we merge ranges.
1604 if (wi::ge_p (u1, val_j, sign))
1606 // New upper bounds is greater of current or the next one.
1607 if (wi::gt_p (val_jp1, val_im1, sign))
1608 res [i - 1] = res[j + 1];
1610 else
1612 // This is a new distinct range, but no point in copying it
1613 // if it is already in the right place.
1614 if (i != j)
1616 res[i++] = res[j];
1617 res[i++] = res[j + 1];
1619 else
1620 i += 2;
1624 // At this point, the vector should have i ranges, none overlapping.
1625 // Now it simply needs to be copied, and if there are too many
1626 // ranges, merge some. We wont do any analysis as to what the
1627 // "best" merges are, simply combine the final ranges into one.
1628 if (i > m_max_ranges * 2)
1630 res[m_max_ranges * 2 - 1] = res[i - 1];
1631 i = m_max_ranges * 2;
1634 for (j = 0; j < i ; j++)
1635 m_base[j] = res [j];
1636 m_num_ranges = i / 2;
1638 m_kind = VR_RANGE;
1639 normalize_kind ();
1641 if (flag_checking)
1642 verify_range ();
1645 // intersect for multi-ranges.
1647 void
1648 irange::irange_intersect (const irange &r)
1650 gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ());
1651 gcc_checking_assert (undefined_p () || r.undefined_p ()
1652 || range_compatible_p (type (), r.type ()));
1654 if (undefined_p () || r.varying_p ())
1655 return;
1656 if (r.undefined_p ())
1658 set_undefined ();
1659 return;
1661 if (varying_p ())
1663 operator= (r);
1664 return;
1667 if (r.num_pairs () == 1)
1669 // R cannot be undefined, use more efficent pair routine.
1670 intersect (r.lower_bound(), r.upper_bound ());
1671 return;
1674 signop sign = TYPE_SIGN (TREE_TYPE(m_base[0]));
1675 unsigned bld_pair = 0;
1676 unsigned bld_lim = m_max_ranges;
1677 int_range_max r2 (*this);
1678 unsigned r2_lim = r2.num_pairs ();
1679 unsigned i2 = 0;
1680 for (unsigned i = 0; i < r.num_pairs (); )
1682 // If r1's upper is < r2's lower, we can skip r1's pair.
1683 tree ru = r.m_base[i * 2 + 1];
1684 tree r2l = r2.m_base[i2 * 2];
1685 if (wi::lt_p (wi::to_wide (ru), wi::to_wide (r2l), sign))
1687 i++;
1688 continue;
1690 // Likewise, skip r2's pair if its excluded.
1691 tree r2u = r2.m_base[i2 * 2 + 1];
1692 tree rl = r.m_base[i * 2];
1693 if (wi::lt_p (wi::to_wide (r2u), wi::to_wide (rl), sign))
1695 i2++;
1696 if (i2 < r2_lim)
1697 continue;
1698 // No more r2, break.
1699 break;
1702 // Must be some overlap. Find the highest of the lower bounds,
1703 // and set it, unless the build limits lower bounds is already
1704 // set.
1705 if (bld_pair < bld_lim)
1707 if (wi::ge_p (wi::to_wide (rl), wi::to_wide (r2l), sign))
1708 m_base[bld_pair * 2] = rl;
1709 else
1710 m_base[bld_pair * 2] = r2l;
1712 else
1713 // Decrease and set a new upper.
1714 bld_pair--;
1716 // ...and choose the lower of the upper bounds.
1717 if (wi::le_p (wi::to_wide (ru), wi::to_wide (r2u), sign))
1719 m_base[bld_pair * 2 + 1] = ru;
1720 bld_pair++;
1721 // Move past the r1 pair and keep trying.
1722 i++;
1723 continue;
1725 else
1727 m_base[bld_pair * 2 + 1] = r2u;
1728 bld_pair++;
1729 i2++;
1730 if (i2 < r2_lim)
1731 continue;
1732 // No more r2, break.
1733 break;
1735 // r2 has the higher lower bound.
1738 // At the exit of this loop, it is one of 2 things:
1739 // ran out of r1, or r2, but either means we are done.
1740 m_num_ranges = bld_pair;
1742 m_kind = VR_RANGE;
1743 normalize_kind ();
1745 if (flag_checking)
1746 verify_range ();
1749 // Multirange intersect for a specified wide_int [lb, ub] range.
1751 void
1752 irange::intersect (const wide_int& lb, const wide_int& ub)
1754 // Undefined remains undefined.
1755 if (undefined_p ())
1756 return;
1758 if (legacy_mode_p ())
1760 intersect (int_range<1> (type (), lb, ub));
1761 return;
1764 tree range_type = type();
1765 signop sign = TYPE_SIGN (range_type);
1767 gcc_checking_assert (TYPE_PRECISION (range_type) == wi::get_precision (lb));
1768 gcc_checking_assert (TYPE_PRECISION (range_type) == wi::get_precision (ub));
1770 unsigned bld_index = 0;
1771 unsigned pair_lim = num_pairs ();
1772 for (unsigned i = 0; i < pair_lim; i++)
1774 tree pairl = m_base[i * 2];
1775 tree pairu = m_base[i * 2 + 1];
1776 // Once UB is less than a pairs lower bound, we're done.
1777 if (wi::lt_p (ub, wi::to_wide (pairl), sign))
1778 break;
1779 // if LB is greater than this pairs upper, this pair is excluded.
1780 if (wi::lt_p (wi::to_wide (pairu), lb, sign))
1781 continue;
1783 // Must be some overlap. Find the highest of the lower bounds,
1784 // and set it
1785 if (wi::gt_p (lb, wi::to_wide (pairl), sign))
1786 m_base[bld_index * 2] = wide_int_to_tree (range_type, lb);
1787 else
1788 m_base[bld_index * 2] = pairl;
1790 // ...and choose the lower of the upper bounds and if the base pair
1791 // has the lower upper bound, need to check next pair too.
1792 if (wi::lt_p (ub, wi::to_wide (pairu), sign))
1794 m_base[bld_index++ * 2 + 1] = wide_int_to_tree (range_type, ub);
1795 break;
1797 else
1798 m_base[bld_index++ * 2 + 1] = pairu;
1801 m_num_ranges = bld_index;
1803 m_kind = VR_RANGE;
1804 normalize_kind ();
1806 if (flag_checking)
1807 verify_range ();
1809 // Signed 1-bits are strange. You can't subtract 1, because you can't
1810 // represent the number 1. This works around that for the invert routine.
1812 static wide_int inline
1813 subtract_one (const wide_int &x, tree type, wi::overflow_type &overflow)
1815 if (TYPE_SIGN (type) == SIGNED)
1816 return wi::add (x, -1, SIGNED, &overflow);
1817 else
1818 return wi::sub (x, 1, UNSIGNED, &overflow);
1821 // The analogous function for adding 1.
1823 static wide_int inline
1824 add_one (const wide_int &x, tree type, wi::overflow_type &overflow)
1826 if (TYPE_SIGN (type) == SIGNED)
1827 return wi::sub (x, -1, SIGNED, &overflow);
1828 else
1829 return wi::add (x, 1, UNSIGNED, &overflow);
1832 // Return the inverse of a range.
1834 void
1835 irange::invert ()
1837 if (legacy_mode_p ())
1839 // We can't just invert VR_RANGE and VR_ANTI_RANGE because we may
1840 // create non-canonical ranges. Use the constructors instead.
1841 if (m_kind == VR_RANGE)
1842 *this = value_range (min (), max (), VR_ANTI_RANGE);
1843 else if (m_kind == VR_ANTI_RANGE)
1844 *this = value_range (min (), max ());
1845 else
1846 gcc_unreachable ();
1847 return;
1850 gcc_checking_assert (!undefined_p () && !varying_p ());
1852 // We always need one more set of bounds to represent an inverse, so
1853 // if we're at the limit, we can't properly represent things.
1855 // For instance, to represent the inverse of a 2 sub-range set
1856 // [5, 10][20, 30], we would need a 3 sub-range set
1857 // [-MIN, 4][11, 19][31, MAX].
1859 // In this case, return the most conservative thing.
1861 // However, if any of the extremes of the range are -MIN/+MAX, we
1862 // know we will not need an extra bound. For example:
1864 // INVERT([-MIN,20][30,40]) => [21,29][41,+MAX]
1865 // INVERT([-MIN,20][30,MAX]) => [21,29]
1866 tree ttype = type ();
1867 unsigned prec = TYPE_PRECISION (ttype);
1868 signop sign = TYPE_SIGN (ttype);
1869 wide_int type_min = wi::min_value (prec, sign);
1870 wide_int type_max = wi::max_value (prec, sign);
1871 if (m_num_ranges == m_max_ranges
1872 && lower_bound () != type_min
1873 && upper_bound () != type_max)
1875 m_base[1] = wide_int_to_tree (ttype, type_max);
1876 m_num_ranges = 1;
1877 return;
1879 // The algorithm is as follows. To calculate INVERT ([a,b][c,d]), we
1880 // generate [-MIN, a-1][b+1, c-1][d+1, MAX].
1882 // If there is an over/underflow in the calculation for any
1883 // sub-range, we eliminate that subrange. This allows us to easily
1884 // calculate INVERT([-MIN, 5]) with: [-MIN, -MIN-1][6, MAX]. And since
1885 // we eliminate the underflow, only [6, MAX] remains.
1886 unsigned i = 0;
1887 wi::overflow_type ovf;
1888 // Construct leftmost range.
1889 int_range_max orig_range (*this);
1890 unsigned nitems = 0;
1891 wide_int tmp;
1892 // If this is going to underflow on the MINUS 1, don't even bother
1893 // checking. This also handles subtracting one from an unsigned 0,
1894 // which doesn't set the underflow bit.
1895 if (type_min != orig_range.lower_bound ())
1897 m_base[nitems++] = wide_int_to_tree (ttype, type_min);
1898 tmp = subtract_one (orig_range.lower_bound (), ttype, ovf);
1899 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
1900 if (ovf)
1901 nitems = 0;
1903 i++;
1904 // Construct middle ranges if applicable.
1905 if (orig_range.num_pairs () > 1)
1907 unsigned j = i;
1908 for (; j < (orig_range.num_pairs () * 2) - 1; j += 2)
1910 // The middle ranges cannot have MAX/MIN, so there's no need
1911 // to check for unsigned overflow on the +1 and -1 here.
1912 tmp = wi::add (wi::to_wide (orig_range.m_base[j]), 1, sign, &ovf);
1913 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
1914 tmp = subtract_one (wi::to_wide (orig_range.m_base[j + 1]),
1915 ttype, ovf);
1916 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
1917 if (ovf)
1918 nitems -= 2;
1920 i = j;
1922 // Construct rightmost range.
1924 // However, if this will overflow on the PLUS 1, don't even bother.
1925 // This also handles adding one to an unsigned MAX, which doesn't
1926 // set the overflow bit.
1927 if (type_max != wi::to_wide (orig_range.m_base[i]))
1929 tmp = add_one (wi::to_wide (orig_range.m_base[i]), ttype, ovf);
1930 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
1931 m_base[nitems++] = wide_int_to_tree (ttype, type_max);
1932 if (ovf)
1933 nitems -= 2;
1935 m_num_ranges = nitems / 2;
1937 // We disallow undefined or varying coming in, so the result can
1938 // only be a VR_RANGE.
1939 gcc_checking_assert (m_kind == VR_RANGE);
1941 if (flag_checking)
1942 verify_range ();
1945 static void
1946 dump_bound_with_infinite_markers (FILE *file, tree bound)
1948 tree type = TREE_TYPE (bound);
1949 wide_int type_min = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1950 wide_int type_max = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1952 if (INTEGRAL_TYPE_P (type)
1953 && !TYPE_UNSIGNED (type)
1954 && TREE_CODE (bound) == INTEGER_CST
1955 && wi::to_wide (bound) == type_min
1956 && TYPE_PRECISION (type) != 1)
1957 fprintf (file, "-INF");
1958 else if (TREE_CODE (bound) == INTEGER_CST
1959 && wi::to_wide (bound) == type_max
1960 && TYPE_PRECISION (type) != 1)
1961 fprintf (file, "+INF");
1962 else
1963 print_generic_expr (file, bound);
1966 void
1967 irange::dump (FILE *file) const
1969 if (undefined_p ())
1971 fprintf (file, "UNDEFINED");
1972 return;
1974 print_generic_expr (file, type ());
1975 fprintf (file, " ");
1976 if (varying_p ())
1978 fprintf (file, "VARYING");
1979 return;
1981 if (legacy_mode_p ())
1983 fprintf (file, "%s[", (m_kind == VR_ANTI_RANGE) ? "~" : "");
1984 dump_bound_with_infinite_markers (file, min ());
1985 fprintf (file, ", ");
1986 dump_bound_with_infinite_markers (file, max ());
1987 fprintf (file, "]");
1988 return;
1990 for (unsigned i = 0; i < m_num_ranges; ++i)
1992 tree lb = m_base[i * 2];
1993 tree ub = m_base[i * 2 + 1];
1994 fprintf (file, "[");
1995 dump_bound_with_infinite_markers (file, lb);
1996 fprintf (file, ", ");
1997 dump_bound_with_infinite_markers (file, ub);
1998 fprintf (file, "]");
2002 void
2003 irange::debug () const
2005 dump (stderr);
2006 fprintf (stderr, "\n");
2009 void
2010 dump_value_range (FILE *file, const irange *vr)
2012 vr->dump (file);
2015 DEBUG_FUNCTION void
2016 debug (const irange *vr)
2018 dump_value_range (stderr, vr);
2019 fprintf (stderr, "\n");
2022 DEBUG_FUNCTION void
2023 debug (const irange &vr)
2025 debug (&vr);
2028 DEBUG_FUNCTION void
2029 debug (const value_range *vr)
2031 dump_value_range (stderr, vr);
2032 fprintf (stderr, "\n");
2035 DEBUG_FUNCTION void
2036 debug (const value_range &vr)
2038 dump_value_range (stderr, &vr);
2039 fprintf (stderr, "\n");
2042 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2043 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2044 false otherwise. If *AR can be represented with a single range
2045 *VR1 will be VR_UNDEFINED. */
2047 bool
2048 ranges_from_anti_range (const value_range *ar,
2049 value_range *vr0, value_range *vr1)
2051 tree type = ar->type ();
2053 vr0->set_undefined ();
2054 vr1->set_undefined ();
2056 /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U
2057 [A+1, +INF]. Not sure if this helps in practice, though. */
2059 if (ar->kind () != VR_ANTI_RANGE
2060 || TREE_CODE (ar->min ()) != INTEGER_CST
2061 || TREE_CODE (ar->max ()) != INTEGER_CST
2062 || !vrp_val_min (type)
2063 || !vrp_val_max (type))
2064 return false;
2066 if (tree_int_cst_lt (vrp_val_min (type), ar->min ()))
2067 vr0->set (vrp_val_min (type),
2068 wide_int_to_tree (type, wi::to_wide (ar->min ()) - 1));
2069 if (tree_int_cst_lt (ar->max (), vrp_val_max (type)))
2070 vr1->set (wide_int_to_tree (type, wi::to_wide (ar->max ()) + 1),
2071 vrp_val_max (type));
2072 if (vr0->undefined_p ())
2074 *vr0 = *vr1;
2075 vr1->set_undefined ();
2078 return !vr0->undefined_p ();
2081 bool
2082 range_has_numeric_bounds_p (const irange *vr)
2084 return (!vr->undefined_p ()
2085 && TREE_CODE (vr->min ()) == INTEGER_CST
2086 && TREE_CODE (vr->max ()) == INTEGER_CST);
2089 /* Return whether VAL is equal to the maximum value of its type.
2090 We can't do a simple equality comparison with TYPE_MAX_VALUE because
2091 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
2092 is not == to the integer constant with the same value in the type. */
2094 bool
2095 vrp_val_is_max (const_tree val)
2097 tree type_max = vrp_val_max (TREE_TYPE (val));
2098 return (val == type_max
2099 || (type_max != NULL_TREE
2100 && operand_equal_p (val, type_max, 0)));
2103 /* Return whether VAL is equal to the minimum value of its type. */
2105 bool
2106 vrp_val_is_min (const_tree val)
2108 tree type_min = vrp_val_min (TREE_TYPE (val));
2109 return (val == type_min
2110 || (type_min != NULL_TREE
2111 && operand_equal_p (val, type_min, 0)));
2114 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
2116 bool
2117 vrp_operand_equal_p (const_tree val1, const_tree val2)
2119 if (val1 == val2)
2120 return true;
2121 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
2122 return false;
2123 return true;
2126 // ?? These stubs are for ipa-prop.c which use a value_range in a
2127 // hash_traits. hash-traits.h defines an extern of gt_ggc_mx (T &)
2128 // instead of picking up the gt_ggc_mx (T *) version.
2129 void
2130 gt_pch_nx (int_range<1> *&x)
2132 return gt_pch_nx ((irange *) x);
2135 void
2136 gt_ggc_mx (int_range<1> *&x)
2138 return gt_ggc_mx ((irange *) x);
2141 #define DEFINE_INT_RANGE_INSTANCE(N) \
2142 template int_range<N>::int_range(tree, tree, value_range_kind); \
2143 template int_range<N>::int_range(tree_node *, \
2144 const wide_int &, \
2145 const wide_int &, \
2146 value_range_kind); \
2147 template int_range<N>::int_range(tree); \
2148 template int_range<N>::int_range(const irange &); \
2149 template int_range<N>::int_range(const int_range &); \
2150 template int_range<N>& int_range<N>::operator= (const int_range &);
2152 DEFINE_INT_RANGE_INSTANCE(1)
2153 DEFINE_INT_RANGE_INSTANCE(2)
2154 DEFINE_INT_RANGE_INSTANCE(3)
2155 DEFINE_INT_RANGE_INSTANCE(255)
2157 #if CHECKING_P
2158 #include "selftest.h"
2160 namespace selftest
2162 #define INT(N) build_int_cst (integer_type_node, (N))
2163 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
2164 #define UINT128(N) build_int_cstu (u128_type, (N))
2165 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
2166 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
2168 static int_range<3>
2169 build_range3 (int a, int b, int c, int d, int e, int f)
2171 int_range<3> i1 (INT (a), INT (b));
2172 int_range<3> i2 (INT (c), INT (d));
2173 int_range<3> i3 (INT (e), INT (f));
2174 i1.union_ (i2);
2175 i1.union_ (i3);
2176 return i1;
2179 static void
2180 range_tests_irange3 ()
2182 typedef int_range<3> int_range3;
2183 int_range3 r0, r1, r2;
2184 int_range3 i1, i2, i3;
2186 // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20].
2187 r0 = int_range3 (INT (10), INT (20));
2188 r1 = int_range3 (INT (5), INT (8));
2189 r0.union_ (r1);
2190 r1 = int_range3 (INT (1), INT (3));
2191 r0.union_ (r1);
2192 ASSERT_TRUE (r0 == build_range3 (1, 3, 5, 8, 10, 20));
2194 // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20].
2195 r1 = int_range3 (INT (-5), INT (0));
2196 r0.union_ (r1);
2197 ASSERT_TRUE (r0 == build_range3 (-5, 3, 5, 8, 10, 20));
2199 // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60].
2200 r1 = int_range3 (INT (50), INT (60));
2201 r0 = int_range3 (INT (10), INT (20));
2202 r0.union_ (int_range3 (INT (30), INT (40)));
2203 r0.union_ (r1);
2204 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
2205 // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80].
2206 r1 = int_range3 (INT (70), INT (80));
2207 r0.union_ (r1);
2209 r2 = build_range3 (10, 20, 30, 40, 50, 60);
2210 r2.union_ (int_range3 (INT (70), INT (80)));
2211 ASSERT_TRUE (r0 == r2);
2213 // [10,20][30,40][50,60] U [6,35] => [6,40][50,60].
2214 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2215 r1 = int_range3 (INT (6), INT (35));
2216 r0.union_ (r1);
2217 r1 = int_range3 (INT (6), INT (40));
2218 r1.union_ (int_range3 (INT (50), INT (60)));
2219 ASSERT_TRUE (r0 == r1);
2221 // [10,20][30,40][50,60] U [6,60] => [6,60].
2222 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2223 r1 = int_range3 (INT (6), INT (60));
2224 r0.union_ (r1);
2225 ASSERT_TRUE (r0 == int_range3 (INT (6), INT (60)));
2227 // [10,20][30,40][50,60] U [6,70] => [6,70].
2228 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2229 r1 = int_range3 (INT (6), INT (70));
2230 r0.union_ (r1);
2231 ASSERT_TRUE (r0 == int_range3 (INT (6), INT (70)));
2233 // [10,20][30,40][50,60] U [35,70] => [10,20][30,70].
2234 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2235 r1 = int_range3 (INT (35), INT (70));
2236 r0.union_ (r1);
2237 r1 = int_range3 (INT (10), INT (20));
2238 r1.union_ (int_range3 (INT (30), INT (70)));
2239 ASSERT_TRUE (r0 == r1);
2241 // [10,20][30,40][50,60] U [15,35] => [10,40][50,60].
2242 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2243 r1 = int_range3 (INT (15), INT (35));
2244 r0.union_ (r1);
2245 r1 = int_range3 (INT (10), INT (40));
2246 r1.union_ (int_range3 (INT (50), INT (60)));
2247 ASSERT_TRUE (r0 == r1);
2249 // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60].
2250 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2251 r1 = int_range3 (INT (35), INT (35));
2252 r0.union_ (r1);
2253 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
2256 static void
2257 range_tests_int_range_max ()
2259 int_range_max big;
2260 unsigned int nrange;
2262 // Build a huge multi-range range.
2263 for (nrange = 0; nrange < 50; ++nrange)
2265 int_range<1> tmp (INT (nrange*10), INT (nrange*10 + 5));
2266 big.union_ (tmp);
2268 ASSERT_TRUE (big.num_pairs () == nrange);
2270 // Verify that we can copy it without loosing precision.
2271 int_range_max copy (big);
2272 ASSERT_TRUE (copy.num_pairs () == nrange);
2274 // Inverting it should produce one more sub-range.
2275 big.invert ();
2276 ASSERT_TRUE (big.num_pairs () == nrange + 1);
2278 int_range<1> tmp (INT (5), INT (37));
2279 big.intersect (tmp);
2280 ASSERT_TRUE (big.num_pairs () == 4);
2282 // Test that [10,10][20,20] does NOT contain 15.
2284 int_range_max i1 (build_int_cst (integer_type_node, 10),
2285 build_int_cst (integer_type_node, 10));
2286 int_range_max i2 (build_int_cst (integer_type_node, 20),
2287 build_int_cst (integer_type_node, 20));
2288 i1.union_ (i2);
2289 ASSERT_FALSE (i1.contains_p (build_int_cst (integer_type_node, 15)));
2293 static void
2294 range_tests_legacy ()
2296 // Test truncating copy to int_range<1>.
2297 int_range<3> big = build_range3 (10, 20, 30, 40, 50, 60);
2298 int_range<1> small = big;
2299 ASSERT_TRUE (small == int_range<1> (INT (10), INT (60)));
2301 // Test truncating copy to int_range<2>.
2302 int_range<2> medium = big;
2303 ASSERT_TRUE (!medium.undefined_p ());
2305 // Test that a truncating copy of [MIN,20][22,40][80,MAX]
2306 // ends up as a conservative anti-range of ~[21,21].
2307 big = int_range<3> (vrp_val_min (integer_type_node), INT (20));
2308 big.union_ (int_range<1> (INT (22), INT (40)));
2309 big.union_ (int_range<1> (INT (80), vrp_val_max (integer_type_node)));
2310 small = big;
2311 ASSERT_TRUE (small == int_range<1> (INT (21), INT (21), VR_ANTI_RANGE));
2313 // Copying a legacy symbolic to an int_range should normalize the
2314 // symbolic at copy time.
2316 tree ssa = make_ssa_name (integer_type_node);
2317 value_range legacy_range (ssa, INT (25));
2318 int_range<2> copy = legacy_range;
2319 ASSERT_TRUE (copy == int_range<2> (vrp_val_min (integer_type_node),
2320 INT (25)));
2322 // Test that copying ~[abc_23, abc_23] to a multi-range yields varying.
2323 legacy_range = value_range (ssa, ssa, VR_ANTI_RANGE);
2324 copy = legacy_range;
2325 ASSERT_TRUE (copy.varying_p ());
2328 // VARYING of different sizes should not be equal.
2329 tree big_type = build_nonstandard_integer_type (32, 1);
2330 tree small_type = build_nonstandard_integer_type (16, 1);
2331 int_range_max r0 (big_type);
2332 int_range_max r1 (small_type);
2333 ASSERT_TRUE (r0 != r1);
2334 value_range vr0 (big_type);
2335 int_range_max vr1 (small_type);
2336 ASSERT_TRUE (vr0 != vr1);
2339 // Simulate -fstrict-enums where the domain of a type is less than the
2340 // underlying type.
2342 static void
2343 range_tests_strict_enum ()
2345 // The enum can only hold [0, 3].
2346 tree rtype = copy_node (unsigned_type_node);
2347 TYPE_MIN_VALUE (rtype) = build_int_cstu (rtype, 0);
2348 TYPE_MAX_VALUE (rtype) = build_int_cstu (rtype, 3);
2350 // Test that even though vr1 covers the strict enum domain ([0, 3]),
2351 // it does not cover the domain of the underlying type.
2352 int_range<1> vr1 (build_int_cstu (rtype, 0), build_int_cstu (rtype, 1));
2353 int_range<1> vr2 (build_int_cstu (rtype, 2), build_int_cstu (rtype, 3));
2354 vr1.union_ (vr2);
2355 ASSERT_TRUE (vr1 == int_range<1> (build_int_cstu (rtype, 0),
2356 build_int_cstu (rtype, 3)));
2357 ASSERT_FALSE (vr1.varying_p ());
2359 // Test that copying to a multi-range does not change things.
2360 int_range<2> ir1 (vr1);
2361 ASSERT_TRUE (ir1 == vr1);
2362 ASSERT_FALSE (ir1.varying_p ());
2364 // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3].
2365 vr1 = int_range<1> (TYPE_MIN_VALUE (rtype), TYPE_MAX_VALUE (rtype));
2366 ir1 = vr1;
2367 ASSERT_TRUE (ir1 == vr1);
2368 ASSERT_FALSE (ir1.varying_p ());
2371 static void
2372 range_tests_misc ()
2374 tree u128_type = build_nonstandard_integer_type (128, /*unsigned=*/1);
2375 int_range<1> i1, i2, i3;
2376 int_range<1> r0, r1, rold;
2378 // Test 1-bit signed integer union.
2379 // [-1,-1] U [0,0] = VARYING.
2380 tree one_bit_type = build_nonstandard_integer_type (1, 0);
2381 tree one_bit_min = vrp_val_min (one_bit_type);
2382 tree one_bit_max = vrp_val_max (one_bit_type);
2384 int_range<2> min (one_bit_min, one_bit_min);
2385 int_range<2> max (one_bit_max, one_bit_max);
2386 max.union_ (min);
2387 ASSERT_TRUE (max.varying_p ());
2390 // Test inversion of 1-bit signed integers.
2392 int_range<2> min (one_bit_min, one_bit_min);
2393 int_range<2> max (one_bit_max, one_bit_max);
2394 int_range<2> t;
2395 t = min;
2396 t.invert ();
2397 ASSERT_TRUE (t == max);
2398 t = max;
2399 t.invert ();
2400 ASSERT_TRUE (t == min);
2403 // Test that NOT(255) is [0..254] in 8-bit land.
2404 int_range<1> not_255 (UCHAR (255), UCHAR (255), VR_ANTI_RANGE);
2405 ASSERT_TRUE (not_255 == int_range<1> (UCHAR (0), UCHAR (254)));
2407 // Test that NOT(0) is [1..255] in 8-bit land.
2408 int_range<1> not_zero = range_nonzero (unsigned_char_type_node);
2409 ASSERT_TRUE (not_zero == int_range<1> (UCHAR (1), UCHAR (255)));
2411 // Check that [0,127][0x..ffffff80,0x..ffffff]
2412 // => ~[128, 0x..ffffff7f].
2413 r0 = int_range<1> (UINT128 (0), UINT128 (127));
2414 tree high = build_minus_one_cst (u128_type);
2415 // low = -1 - 127 => 0x..ffffff80.
2416 tree low = fold_build2 (MINUS_EXPR, u128_type, high, UINT128(127));
2417 r1 = int_range<1> (low, high); // [0x..ffffff80, 0x..ffffffff]
2418 // r0 = [0,127][0x..ffffff80,0x..fffffff].
2419 r0.union_ (r1);
2420 // r1 = [128, 0x..ffffff7f].
2421 r1 = int_range<1> (UINT128(128),
2422 fold_build2 (MINUS_EXPR, u128_type,
2423 build_minus_one_cst (u128_type),
2424 UINT128(128)));
2425 r0.invert ();
2426 ASSERT_TRUE (r0 == r1);
2428 r0.set_varying (integer_type_node);
2429 tree minint = wide_int_to_tree (integer_type_node, r0.lower_bound ());
2430 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
2432 r0.set_varying (short_integer_type_node);
2434 r0.set_varying (unsigned_type_node);
2435 tree maxuint = wide_int_to_tree (unsigned_type_node, r0.upper_bound ());
2437 // Check that ~[0,5] => [6,MAX] for unsigned int.
2438 r0 = int_range<1> (UINT (0), UINT (5));
2439 r0.invert ();
2440 ASSERT_TRUE (r0 == int_range<1> (UINT(6), maxuint));
2442 // Check that ~[10,MAX] => [0,9] for unsigned int.
2443 r0 = int_range<1> (UINT(10), maxuint);
2444 r0.invert ();
2445 ASSERT_TRUE (r0 == int_range<1> (UINT (0), UINT (9)));
2447 // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers.
2448 r0 = int_range<1> (UINT128 (0), UINT128 (5), VR_ANTI_RANGE);
2449 r1 = int_range<1> (UINT128(6), build_minus_one_cst (u128_type));
2450 ASSERT_TRUE (r0 == r1);
2452 // Check that [~5] is really [-MIN,4][6,MAX].
2453 r0 = int_range<1> (INT (5), INT (5), VR_ANTI_RANGE);
2454 r1 = int_range<1> (minint, INT (4));
2455 r1.union_ (int_range<1> (INT (6), maxint));
2456 ASSERT_FALSE (r1.undefined_p ());
2457 ASSERT_TRUE (r0 == r1);
2459 r1 = int_range<1> (INT (5), INT (5));
2460 int_range<1> r2 (r1);
2461 ASSERT_TRUE (r1 == r2);
2463 r1 = int_range<1> (INT (5), INT (10));
2465 r1 = int_range<1> (integer_type_node,
2466 wi::to_wide (INT (5)), wi::to_wide (INT (10)));
2467 ASSERT_TRUE (r1.contains_p (INT (7)));
2469 r1 = int_range<1> (SCHAR (0), SCHAR (20));
2470 ASSERT_TRUE (r1.contains_p (SCHAR(15)));
2471 ASSERT_FALSE (r1.contains_p (SCHAR(300)));
2473 // NOT([10,20]) ==> [-MIN,9][21,MAX].
2474 r0 = r1 = int_range<1> (INT (10), INT (20));
2475 r2 = int_range<1> (minint, INT(9));
2476 r2.union_ (int_range<1> (INT(21), maxint));
2477 ASSERT_FALSE (r2.undefined_p ());
2478 r1.invert ();
2479 ASSERT_TRUE (r1 == r2);
2480 // Test that NOT(NOT(x)) == x.
2481 r2.invert ();
2482 ASSERT_TRUE (r0 == r2);
2484 // Test that booleans and their inverse work as expected.
2485 r0 = range_zero (boolean_type_node);
2486 ASSERT_TRUE (r0 == int_range<1> (build_zero_cst (boolean_type_node),
2487 build_zero_cst (boolean_type_node)));
2488 r0.invert ();
2489 ASSERT_TRUE (r0 == int_range<1> (build_one_cst (boolean_type_node),
2490 build_one_cst (boolean_type_node)));
2492 // Make sure NULL and non-NULL of pointer types work, and that
2493 // inverses of them are consistent.
2494 tree voidp = build_pointer_type (void_type_node);
2495 r0 = range_zero (voidp);
2496 r1 = r0;
2497 r0.invert ();
2498 r0.invert ();
2499 ASSERT_TRUE (r0 == r1);
2501 // [10,20] U [15, 30] => [10, 30].
2502 r0 = int_range<1> (INT (10), INT (20));
2503 r1 = int_range<1> (INT (15), INT (30));
2504 r0.union_ (r1);
2505 ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (30)));
2507 // [15,40] U [] => [15,40].
2508 r0 = int_range<1> (INT (15), INT (40));
2509 r1.set_undefined ();
2510 r0.union_ (r1);
2511 ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (40)));
2513 // [10,20] U [10,10] => [10,20].
2514 r0 = int_range<1> (INT (10), INT (20));
2515 r1 = int_range<1> (INT (10), INT (10));
2516 r0.union_ (r1);
2517 ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (20)));
2519 // [10,20] U [9,9] => [9,20].
2520 r0 = int_range<1> (INT (10), INT (20));
2521 r1 = int_range<1> (INT (9), INT (9));
2522 r0.union_ (r1);
2523 ASSERT_TRUE (r0 == int_range<1> (INT (9), INT (20)));
2525 // [10,20] ^ [15,30] => [15,20].
2526 r0 = int_range<1> (INT (10), INT (20));
2527 r1 = int_range<1> (INT (15), INT (30));
2528 r0.intersect (r1);
2529 ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (20)));
2531 // Test the internal sanity of wide_int's wrt HWIs.
2532 ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node),
2533 TYPE_SIGN (boolean_type_node))
2534 == wi::uhwi (1, TYPE_PRECISION (boolean_type_node)));
2536 // Test zero_p().
2537 r0 = int_range<1> (INT (0), INT (0));
2538 ASSERT_TRUE (r0.zero_p ());
2540 // Test nonzero_p().
2541 r0 = int_range<1> (INT (0), INT (0));
2542 r0.invert ();
2543 ASSERT_TRUE (r0.nonzero_p ());
2545 // test legacy interaction
2546 // r0 = ~[1,1]
2547 r0 = int_range<1> (UINT (1), UINT (1), VR_ANTI_RANGE);
2548 // r1 = ~[3,3]
2549 r1 = int_range<1> (UINT (3), UINT (3), VR_ANTI_RANGE);
2551 // vv = [0,0][2,2][4, MAX]
2552 int_range<3> vv = r0;
2553 vv.intersect (r1);
2555 ASSERT_TRUE (vv.contains_p (UINT (2)));
2556 ASSERT_TRUE (vv.num_pairs () == 3);
2558 // create r0 as legacy [1,1]
2559 r0 = int_range<1> (UINT (1), UINT (1));
2560 // And union it with [0,0][2,2][4,MAX] multi range
2561 r0.union_ (vv);
2562 // The result should be [0,2][4,MAX], or ~[3,3] but it must contain 2
2563 ASSERT_TRUE (r0.contains_p (UINT (2)));
2566 void
2567 range_tests ()
2569 range_tests_legacy ();
2570 range_tests_irange3 ();
2571 range_tests_int_range_max ();
2572 range_tests_strict_enum ();
2573 range_tests_misc ();
2576 } // namespace selftest
2578 #endif // CHECKING_P