1 /* Support routines for value ranges.
2 Copyright (C) 2019-2022 Free Software Foundation, Inc.
3 Major hacks by Aldy Hernandez <aldyh@redhat.com> and
4 Andrew MacLeod <amacleod@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
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/>. */
24 #include "coretypes.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.
37 irange::operator= (const irange
&src
)
44 if (src
.legacy_mode_p ())
46 copy_legacy_to_multi_range (src
);
51 unsigned lim
= src
.m_num_ranges
;
52 if (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];
67 // Return TRUE if range is a multi-range that can be represented as a
71 irange::maybe_anti_range () const
74 unsigned int precision
= TYPE_PRECISION (ttype
);
75 signop sign
= TYPE_SIGN (ttype
);
76 return (num_pairs () > 1
78 && lower_bound () == wi::min_value (precision
, sign
)
79 && upper_bound () == wi::max_value (precision
, sign
));
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 ())
89 else if (src
.varying_p ())
90 set_varying (src
.type ());
93 if (range_has_numeric_bounds_p (&src
))
94 set (src
.min (), src
.max (), src
.kind ());
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.
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];
120 // Copy multi-range to legacy.
121 if (src
.maybe_anti_range ())
123 int_range
<3> r (src
);
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
);
129 set (src
.tree_lower_bound (), src
.tree_upper_bound ());
132 // Swap MIN/MAX if they are out of order and adjust KIND appropriately.
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
)
140 /* Wrong order for min and max, to swap them and the VR type we need
142 if (tree_int_cst_lt (max
, min
))
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)
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
);
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
))
168 kind
= kind
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
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
));
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]
198 if (vrp_val_is_min (min
))
199 min
= max
= vrp_val_max (type
);
201 min
= max
= vrp_val_min (type
);
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
));
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
);
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].
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
);
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);
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
267 This routine exists to ease canonicalization in the case where we
268 extract ranges from var + CST op limit. */
271 irange::set (tree min
, tree max
, value_range_kind kind
)
273 if (kind
!= VR_UNDEFINED
)
275 if (TREE_OVERFLOW_P (min
))
276 min
= drop_tree_overflow (min
);
277 if (TREE_OVERFLOW_P (max
))
278 max
= drop_tree_overflow (max
);
281 if (!legacy_mode_p ())
283 if (kind
== VR_RANGE
)
284 irange_set (min
, max
);
287 gcc_checking_assert (kind
== VR_ANTI_RANGE
);
288 irange_set_anti_range (min
, max
);
292 if (kind
== VR_UNDEFINED
)
298 if (kind
== VR_VARYING
299 || POLY_INT_CST_P (min
)
300 || POLY_INT_CST_P (max
))
302 set_varying (TREE_TYPE (min
));
306 // Nothing to canonicalize for symbolic ranges.
307 if (TREE_CODE (min
) != INTEGER_CST
308 || TREE_CODE (max
) != INTEGER_CST
)
317 swap_out_of_order_endpoints (min
, max
, kind
);
318 if (kind
== VR_VARYING
)
320 set_varying (TREE_TYPE (min
));
324 // Anti-ranges that can be represented as ranges should be so.
325 if (kind
== VR_ANTI_RANGE
)
327 bool is_min
= vrp_val_is_min (min
);
328 bool is_max
= vrp_val_is_max (max
);
330 if (is_min
&& is_max
)
332 // Fall through. This will either be normalized as
333 // VR_UNDEFINED if the anti-range spans the entire
334 // precision, or it will remain an VR_ANTI_RANGE in the case
335 // of an -fstrict-enum where [MIN,MAX] is less than the span
336 // of underlying precision.
338 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
340 irange_set_1bit_anti_range (min
, max
);
345 tree one
= build_int_cst (TREE_TYPE (max
), 1);
346 min
= int_const_binop (PLUS_EXPR
, max
, one
);
347 max
= vrp_val_max (TREE_TYPE (max
));
352 tree one
= build_int_cst (TREE_TYPE (min
), 1);
353 max
= int_const_binop (MINUS_EXPR
, min
, one
);
354 min
= vrp_val_min (TREE_TYPE (min
));
368 // Check the validity of the range.
371 irange::verify_range ()
373 if (m_kind
== VR_UNDEFINED
)
375 gcc_checking_assert (m_num_ranges
== 0);
378 if (m_kind
== VR_VARYING
)
380 gcc_checking_assert (m_num_ranges
== 1);
381 gcc_checking_assert (varying_compatible_p ());
384 if (!legacy_mode_p ())
386 gcc_checking_assert (m_num_ranges
!= 0);
387 gcc_checking_assert (!varying_compatible_p ());
388 for (unsigned i
= 0; i
< m_num_ranges
; ++i
)
390 tree lb
= tree_lower_bound (i
);
391 tree ub
= tree_upper_bound (i
);
392 int c
= compare_values (lb
, ub
);
393 gcc_checking_assert (c
== 0 || c
== -1);
397 if (m_kind
== VR_RANGE
|| m_kind
== VR_ANTI_RANGE
)
399 gcc_checking_assert (m_num_ranges
== 1);
400 int cmp
= compare_values (tree_lower_bound (0), tree_upper_bound (0));
401 gcc_checking_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
405 // Return the lower bound for a sub-range. PAIR is the sub-range in
409 irange::legacy_lower_bound (unsigned pair
) const
411 gcc_checking_assert (legacy_mode_p ());
414 value_range
numeric_range (*this);
415 numeric_range
.normalize_symbolics ();
416 return numeric_range
.legacy_lower_bound (pair
);
418 gcc_checking_assert (m_num_ranges
> 0);
419 gcc_checking_assert (pair
+ 1 <= num_pairs ());
420 if (m_kind
== VR_ANTI_RANGE
)
422 tree typ
= type (), t
;
423 if (pair
== 1 || vrp_val_is_min (min ()))
424 t
= wide_int_to_tree (typ
, wi::to_wide (max ()) + 1);
426 t
= vrp_val_min (typ
);
427 return wi::to_wide (t
);
429 return wi::to_wide (tree_lower_bound (pair
));
432 // Return the upper bound for a sub-range. PAIR is the sub-range in
436 irange::legacy_upper_bound (unsigned pair
) const
438 gcc_checking_assert (legacy_mode_p ());
441 value_range
numeric_range (*this);
442 numeric_range
.normalize_symbolics ();
443 return numeric_range
.legacy_upper_bound (pair
);
445 gcc_checking_assert (m_num_ranges
> 0);
446 gcc_checking_assert (pair
+ 1 <= num_pairs ());
447 if (m_kind
== VR_ANTI_RANGE
)
449 tree typ
= type (), t
;
450 if (pair
== 1 || vrp_val_is_min (min ()))
451 t
= vrp_val_max (typ
);
453 t
= wide_int_to_tree (typ
, wi::to_wide (min ()) - 1);
454 return wi::to_wide (t
);
456 return wi::to_wide (tree_upper_bound (pair
));
460 irange::legacy_equal_p (const irange
&other
) const
462 gcc_checking_assert (legacy_mode_p () && other
.legacy_mode_p ());
464 if (m_kind
!= other
.m_kind
)
466 if (m_kind
== VR_UNDEFINED
)
468 if (m_kind
== VR_VARYING
)
469 return range_compatible_p (type (), other
.type ());
470 return (vrp_operand_equal_p (tree_lower_bound (0),
471 other
.tree_lower_bound (0))
472 && vrp_operand_equal_p (tree_upper_bound (0),
473 other
.tree_upper_bound (0)));
477 irange::equal_p (const irange
&other
) const
479 if (legacy_mode_p ())
481 if (other
.legacy_mode_p ())
482 return legacy_equal_p (other
);
483 value_range
tmp (other
);
484 return legacy_equal_p (tmp
);
486 if (other
.legacy_mode_p ())
488 value_range
tmp2 (*this);
489 return tmp2
.legacy_equal_p (other
);
492 if (m_num_ranges
!= other
.m_num_ranges
)
495 for (unsigned i
= 0; i
< m_num_ranges
; ++i
)
497 tree lb
= tree_lower_bound (i
);
498 tree ub
= tree_upper_bound (i
);
499 tree lb_other
= other
.tree_lower_bound (i
);
500 tree ub_other
= other
.tree_upper_bound (i
);
501 if (!operand_equal_p (lb
, lb_other
, 0)
502 || !operand_equal_p (ub
, ub_other
, 0))
508 /* Return TRUE if this is a symbolic range. */
511 irange::symbolic_p () const
513 return (m_num_ranges
> 0
514 && (!is_gimple_min_invariant (min ())
515 || !is_gimple_min_invariant (max ())));
518 /* Return TRUE if this is a constant range. */
521 irange::constant_p () const
523 return (m_num_ranges
> 0
524 && TREE_CODE (min ()) == INTEGER_CST
525 && TREE_CODE (max ()) == INTEGER_CST
);
528 /* If range is a singleton, place it in RESULT and return TRUE.
529 Note: A singleton can be any gimple invariant, not just constants.
530 So, [&x, &x] counts as a singleton. */
533 irange::singleton_p (tree
*result
) const
535 if (!legacy_mode_p ())
537 if (num_pairs () == 1 && (wi::to_wide (tree_lower_bound ())
538 == wi::to_wide (tree_upper_bound ())))
541 *result
= tree_lower_bound ();
546 if (m_kind
== VR_ANTI_RANGE
)
550 if (TYPE_PRECISION (type ()) == 1)
558 if (num_pairs () == 1)
560 value_range vr0
, vr1
;
561 ranges_from_anti_range ((const value_range
*) this, &vr0
, &vr1
);
562 return vr0
.singleton_p (result
);
565 // Catches non-numeric extremes as well.
566 if (m_kind
== VR_RANGE
567 && vrp_operand_equal_p (min (), max ())
568 && is_gimple_min_invariant (min ()))
577 /* Return 1 if VAL is inside value range.
578 0 if VAL is not inside value range.
579 -2 if we cannot tell either way.
581 Benchmark compile/20001226-1.c compilation time after changing this
585 irange::value_inside_range (tree val
) const
593 if (!legacy_mode_p () && TREE_CODE (val
) == INTEGER_CST
)
594 return contains_p (val
);
596 int cmp1
= operand_less_p (val
, min ());
600 return m_kind
!= VR_RANGE
;
602 int cmp2
= operand_less_p (max (), val
);
606 if (m_kind
== VR_RANGE
)
612 /* Return TRUE if it is possible that range contains VAL. */
615 irange::may_contain_p (tree val
) const
617 return value_inside_range (val
) != 0;
620 /* Return TRUE if range contains INTEGER_CST. */
621 /* Return 1 if VAL is inside value range.
622 0 if VAL is not inside value range.
624 Benchmark compile/20001226-1.c compilation time after changing this
629 irange::contains_p (tree cst
) const
634 if (legacy_mode_p ())
636 gcc_checking_assert (TREE_CODE (cst
) == INTEGER_CST
);
639 value_range
numeric_range (*this);
640 numeric_range
.normalize_symbolics ();
641 return numeric_range
.contains_p (cst
);
643 return value_inside_range (cst
) == 1;
646 gcc_checking_assert (TREE_CODE (cst
) == INTEGER_CST
);
647 signop sign
= TYPE_SIGN (TREE_TYPE (cst
));
648 wide_int v
= wi::to_wide (cst
);
649 for (unsigned r
= 0; r
< m_num_ranges
; ++r
)
651 if (wi::lt_p (v
, lower_bound (r
), sign
))
653 if (wi::le_p (v
, upper_bound (r
), sign
))
661 /* Normalize addresses into constants. */
664 irange::normalize_addresses ()
669 if (!POINTER_TYPE_P (type ()) || range_has_numeric_bounds_p (this))
672 if (!range_includes_zero_p (this))
674 gcc_checking_assert (TREE_CODE (min ()) == ADDR_EXPR
675 || TREE_CODE (max ()) == ADDR_EXPR
);
676 set_nonzero (type ());
679 set_varying (type ());
682 /* Normalize symbolics and addresses into constants. */
685 irange::normalize_symbolics ()
687 if (varying_p () || undefined_p ())
690 tree ttype
= type ();
691 bool min_symbolic
= !is_gimple_min_invariant (min ());
692 bool max_symbolic
= !is_gimple_min_invariant (max ());
693 if (!min_symbolic
&& !max_symbolic
)
695 normalize_addresses ();
699 // [SYM, SYM] -> VARYING
700 if (min_symbolic
&& max_symbolic
)
705 if (kind () == VR_RANGE
)
707 // [SYM, NUM] -> [-MIN, NUM]
710 set (vrp_val_min (ttype
), max ());
713 // [NUM, SYM] -> [NUM, +MAX]
714 set (min (), vrp_val_max (ttype
));
717 gcc_checking_assert (kind () == VR_ANTI_RANGE
);
718 // ~[SYM, NUM] -> [NUM + 1, +MAX]
721 if (!vrp_val_is_max (max ()))
723 tree n
= wide_int_to_tree (ttype
, wi::to_wide (max ()) + 1);
724 set (n
, vrp_val_max (ttype
));
730 // ~[NUM, SYM] -> [-MIN, NUM - 1]
731 if (!vrp_val_is_min (min ()))
733 tree n
= wide_int_to_tree (ttype
, wi::to_wide (min ()) - 1);
734 set (vrp_val_min (ttype
), n
);
740 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
741 { VR1TYPE, VR0MIN, VR0MAX } and store the result
742 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
743 possible such range. The resulting range is not canonicalized. */
746 intersect_ranges (enum value_range_kind
*vr0type
,
747 tree
*vr0min
, tree
*vr0max
,
748 enum value_range_kind vr1type
,
749 tree vr1min
, tree vr1max
)
751 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
752 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
754 /* [] is vr0, () is vr1 in the following classification comments. */
758 if (*vr0type
== vr1type
)
759 /* Nothing to do for equal ranges. */
761 else if ((*vr0type
== VR_RANGE
762 && vr1type
== VR_ANTI_RANGE
)
763 || (*vr0type
== VR_ANTI_RANGE
764 && vr1type
== VR_RANGE
))
766 /* For anti-range with range intersection the result is empty. */
767 *vr0type
= VR_UNDEFINED
;
774 else if (operand_less_p (*vr0max
, vr1min
) == 1
775 || operand_less_p (vr1max
, *vr0min
) == 1)
777 /* [ ] ( ) or ( ) [ ]
778 If the ranges have an empty intersection, the result of the
779 intersect operation is the range for intersecting an
780 anti-range with a range or empty when intersecting two ranges. */
781 if (*vr0type
== VR_RANGE
782 && vr1type
== VR_ANTI_RANGE
)
784 else if (*vr0type
== VR_ANTI_RANGE
785 && vr1type
== VR_RANGE
)
791 else if (*vr0type
== VR_RANGE
792 && vr1type
== VR_RANGE
)
794 *vr0type
= VR_UNDEFINED
;
798 else if (*vr0type
== VR_ANTI_RANGE
799 && vr1type
== VR_ANTI_RANGE
)
801 /* If the anti-ranges are adjacent to each other merge them. */
802 if (TREE_CODE (*vr0max
) == INTEGER_CST
803 && TREE_CODE (vr1min
) == INTEGER_CST
804 && operand_less_p (*vr0max
, vr1min
) == 1
805 && integer_onep (int_const_binop (MINUS_EXPR
,
808 else if (TREE_CODE (vr1max
) == INTEGER_CST
809 && TREE_CODE (*vr0min
) == INTEGER_CST
810 && operand_less_p (vr1max
, *vr0min
) == 1
811 && integer_onep (int_const_binop (MINUS_EXPR
,
814 /* Else arbitrarily take VR0. */
817 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
818 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
820 /* [ ( ) ] or [( ) ] or [ ( )] */
821 if (*vr0type
== VR_RANGE
822 && vr1type
== VR_RANGE
)
824 /* If both are ranges the result is the inner one. */
829 else if (*vr0type
== VR_RANGE
830 && vr1type
== VR_ANTI_RANGE
)
832 /* Choose the right gap if the left one is empty. */
835 if (TREE_CODE (vr1max
) != INTEGER_CST
)
837 else if (TYPE_PRECISION (TREE_TYPE (vr1max
)) == 1
838 && !TYPE_UNSIGNED (TREE_TYPE (vr1max
)))
840 = int_const_binop (MINUS_EXPR
, vr1max
,
841 build_int_cst (TREE_TYPE (vr1max
), -1));
844 = int_const_binop (PLUS_EXPR
, vr1max
,
845 build_int_cst (TREE_TYPE (vr1max
), 1));
847 /* Choose the left gap if the right one is empty. */
850 if (TREE_CODE (vr1min
) != INTEGER_CST
)
852 else if (TYPE_PRECISION (TREE_TYPE (vr1min
)) == 1
853 && !TYPE_UNSIGNED (TREE_TYPE (vr1min
)))
855 = int_const_binop (PLUS_EXPR
, vr1min
,
856 build_int_cst (TREE_TYPE (vr1min
), -1));
859 = int_const_binop (MINUS_EXPR
, vr1min
,
860 build_int_cst (TREE_TYPE (vr1min
), 1));
862 /* Choose the anti-range if the range is effectively varying. */
863 else if (vrp_val_is_min (*vr0min
)
864 && vrp_val_is_max (*vr0max
))
870 /* Else choose the range. */
872 else if (*vr0type
== VR_ANTI_RANGE
873 && vr1type
== VR_ANTI_RANGE
)
874 /* If both are anti-ranges the result is the outer one. */
876 else if (*vr0type
== VR_ANTI_RANGE
877 && vr1type
== VR_RANGE
)
879 /* The intersection is empty. */
880 *vr0type
= VR_UNDEFINED
;
887 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
888 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
890 /* ( [ ] ) or ([ ] ) or ( [ ]) */
891 if (*vr0type
== VR_RANGE
892 && vr1type
== VR_RANGE
)
893 /* Choose the inner range. */
895 else if (*vr0type
== VR_ANTI_RANGE
896 && vr1type
== VR_RANGE
)
898 /* Choose the right gap if the left is empty. */
902 if (TREE_CODE (*vr0max
) != INTEGER_CST
)
904 else if (TYPE_PRECISION (TREE_TYPE (*vr0max
)) == 1
905 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max
)))
907 = int_const_binop (MINUS_EXPR
, *vr0max
,
908 build_int_cst (TREE_TYPE (*vr0max
), -1));
911 = int_const_binop (PLUS_EXPR
, *vr0max
,
912 build_int_cst (TREE_TYPE (*vr0max
), 1));
915 /* Choose the left gap if the right is empty. */
919 if (TREE_CODE (*vr0min
) != INTEGER_CST
)
921 else if (TYPE_PRECISION (TREE_TYPE (*vr0min
)) == 1
922 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min
)))
924 = int_const_binop (PLUS_EXPR
, *vr0min
,
925 build_int_cst (TREE_TYPE (*vr0min
), -1));
928 = int_const_binop (MINUS_EXPR
, *vr0min
,
929 build_int_cst (TREE_TYPE (*vr0min
), 1));
932 /* Choose the anti-range if the range is effectively varying. */
933 else if (vrp_val_is_min (vr1min
)
934 && vrp_val_is_max (vr1max
))
936 /* Choose the anti-range if it is ~[0,0], that range is special
937 enough to special case when vr1's range is relatively wide.
938 At least for types bigger than int - this covers pointers
939 and arguments to functions like ctz. */
940 else if (*vr0min
== *vr0max
941 && integer_zerop (*vr0min
)
942 && ((TYPE_PRECISION (TREE_TYPE (*vr0min
))
943 >= TYPE_PRECISION (integer_type_node
))
944 || POINTER_TYPE_P (TREE_TYPE (*vr0min
)))
945 && TREE_CODE (vr1max
) == INTEGER_CST
946 && TREE_CODE (vr1min
) == INTEGER_CST
947 && (wi::clz (wi::to_wide (vr1max
) - wi::to_wide (vr1min
))
948 < TYPE_PRECISION (TREE_TYPE (*vr0min
)) / 2))
950 /* Else choose the range. */
958 else if (*vr0type
== VR_ANTI_RANGE
959 && vr1type
== VR_ANTI_RANGE
)
961 /* If both are anti-ranges the result is the outer one. */
966 else if (vr1type
== VR_ANTI_RANGE
967 && *vr0type
== VR_RANGE
)
969 /* The intersection is empty. */
970 *vr0type
= VR_UNDEFINED
;
977 else if ((operand_less_p (vr1min
, *vr0max
) == 1
978 || operand_equal_p (vr1min
, *vr0max
, 0))
979 && operand_less_p (*vr0min
, vr1min
) == 1
980 && operand_less_p (*vr0max
, vr1max
) == 1)
982 /* [ ( ] ) or [ ]( ) */
983 if (*vr0type
== VR_ANTI_RANGE
984 && vr1type
== VR_ANTI_RANGE
)
986 else if (*vr0type
== VR_RANGE
987 && vr1type
== VR_RANGE
)
989 else if (*vr0type
== VR_RANGE
990 && vr1type
== VR_ANTI_RANGE
)
992 if (TREE_CODE (vr1min
) == INTEGER_CST
)
993 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
994 build_int_cst (TREE_TYPE (vr1min
), 1));
998 else if (*vr0type
== VR_ANTI_RANGE
999 && vr1type
== VR_RANGE
)
1001 *vr0type
= VR_RANGE
;
1002 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
1003 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
1004 build_int_cst (TREE_TYPE (*vr0max
), 1));
1012 else if ((operand_less_p (*vr0min
, vr1max
) == 1
1013 || operand_equal_p (*vr0min
, vr1max
, 0))
1014 && operand_less_p (vr1min
, *vr0min
) == 1
1015 && operand_less_p (vr1max
, *vr0max
) == 1)
1017 /* ( [ ) ] or ( )[ ] */
1018 if (*vr0type
== VR_ANTI_RANGE
1019 && vr1type
== VR_ANTI_RANGE
)
1021 else if (*vr0type
== VR_RANGE
1022 && vr1type
== VR_RANGE
)
1024 else if (*vr0type
== VR_RANGE
1025 && vr1type
== VR_ANTI_RANGE
)
1027 if (TREE_CODE (vr1max
) == INTEGER_CST
)
1028 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
1029 build_int_cst (TREE_TYPE (vr1max
), 1));
1033 else if (*vr0type
== VR_ANTI_RANGE
1034 && vr1type
== VR_RANGE
)
1036 *vr0type
= VR_RANGE
;
1037 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
1038 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
1039 build_int_cst (TREE_TYPE (*vr0min
), 1));
1048 /* If we know the intersection is empty, there's no need to
1049 conservatively add anything else to the set. */
1050 if (*vr0type
== VR_UNDEFINED
)
1053 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
1054 result for the intersection. That's always a conservative
1055 correct estimate unless VR1 is a constant singleton range
1056 in which case we choose that. */
1057 if (vr1type
== VR_RANGE
1058 && is_gimple_min_invariant (vr1min
)
1059 && vrp_operand_equal_p (vr1min
, vr1max
))
1067 /* Helper for the intersection operation for value ranges. Given two
1068 ranges VR0 and VR1, set VR0 to the intersection of both ranges.
1069 This may not be the smallest possible such range. */
1072 irange::legacy_intersect (irange
*vr0
, const irange
*vr1
)
1074 gcc_checking_assert (vr0
->legacy_mode_p ());
1075 gcc_checking_assert (vr1
->legacy_mode_p ());
1076 /* If either range is VR_VARYING the other one wins. */
1077 if (vr1
->varying_p ())
1079 if (vr0
->varying_p ())
1081 vr0
->set (vr1
->min (), vr1
->max (), vr1
->kind ());
1085 /* When either range is VR_UNDEFINED the resulting range is
1086 VR_UNDEFINED, too. */
1087 if (vr0
->undefined_p ())
1089 if (vr1
->undefined_p ())
1091 vr0
->set_undefined ();
1095 value_range_kind vr0kind
= vr0
->kind ();
1096 tree vr0min
= vr0
->min ();
1097 tree vr0max
= vr0
->max ();
1099 intersect_ranges (&vr0kind
, &vr0min
, &vr0max
,
1100 vr1
->kind (), vr1
->min (), vr1
->max ());
1102 /* Make sure to canonicalize the result though as the inversion of a
1103 VR_RANGE can still be a VR_RANGE. */
1104 if (vr0kind
== VR_UNDEFINED
)
1105 vr0
->set_undefined ();
1106 else if (vr0kind
== VR_VARYING
)
1108 /* If we failed, use the original VR0. */
1112 vr0
->set (vr0min
, vr0max
, vr0kind
);
1115 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
1116 { VR1TYPE, VR0MIN, VR0MAX } and store the result
1117 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
1118 possible such range. The resulting range is not canonicalized. */
1121 union_ranges (enum value_range_kind
*vr0type
,
1122 tree
*vr0min
, tree
*vr0max
,
1123 enum value_range_kind vr1type
,
1124 tree vr1min
, tree vr1max
)
1126 int cmpmin
= compare_values (*vr0min
, vr1min
);
1127 int cmpmax
= compare_values (*vr0max
, vr1max
);
1128 bool mineq
= cmpmin
== 0;
1129 bool maxeq
= cmpmax
== 0;
1131 /* [] is vr0, () is vr1 in the following classification comments. */
1135 if (*vr0type
== vr1type
)
1136 /* Nothing to do for equal ranges. */
1138 else if ((*vr0type
== VR_RANGE
1139 && vr1type
== VR_ANTI_RANGE
)
1140 || (*vr0type
== VR_ANTI_RANGE
1141 && vr1type
== VR_RANGE
))
1143 /* For anti-range with range union the result is varying. */
1149 else if (operand_less_p (*vr0max
, vr1min
) == 1
1150 || operand_less_p (vr1max
, *vr0min
) == 1)
1152 /* [ ] ( ) or ( ) [ ]
1153 If the ranges have an empty intersection, result of the union
1154 operation is the anti-range or if both are anti-ranges
1156 if (*vr0type
== VR_ANTI_RANGE
1157 && vr1type
== VR_ANTI_RANGE
)
1159 else if (*vr0type
== VR_ANTI_RANGE
1160 && vr1type
== VR_RANGE
)
1162 else if (*vr0type
== VR_RANGE
1163 && vr1type
== VR_ANTI_RANGE
)
1169 else if (*vr0type
== VR_RANGE
1170 && vr1type
== VR_RANGE
)
1172 /* The result is the convex hull of both ranges. */
1173 if (operand_less_p (*vr0max
, vr1min
) == 1)
1175 /* If the result can be an anti-range, create one. */
1176 if (TREE_CODE (*vr0max
) == INTEGER_CST
1177 && TREE_CODE (vr1min
) == INTEGER_CST
1178 && vrp_val_is_min (*vr0min
)
1179 && vrp_val_is_max (vr1max
))
1181 tree min
= int_const_binop (PLUS_EXPR
,
1183 build_int_cst (TREE_TYPE (*vr0max
), 1));
1184 tree max
= int_const_binop (MINUS_EXPR
,
1186 build_int_cst (TREE_TYPE (vr1min
), 1));
1187 if (!operand_less_p (max
, min
))
1189 *vr0type
= VR_ANTI_RANGE
;
1201 /* If the result can be an anti-range, create one. */
1202 if (TREE_CODE (vr1max
) == INTEGER_CST
1203 && TREE_CODE (*vr0min
) == INTEGER_CST
1204 && vrp_val_is_min (vr1min
)
1205 && vrp_val_is_max (*vr0max
))
1207 tree min
= int_const_binop (PLUS_EXPR
,
1209 build_int_cst (TREE_TYPE (vr1max
), 1));
1210 tree max
= int_const_binop (MINUS_EXPR
,
1212 build_int_cst (TREE_TYPE (*vr0min
), 1));
1213 if (!operand_less_p (max
, min
))
1215 *vr0type
= VR_ANTI_RANGE
;
1229 else if ((maxeq
|| cmpmax
== 1)
1230 && (mineq
|| cmpmin
== -1))
1232 /* [ ( ) ] or [( ) ] or [ ( )] */
1233 if (*vr0type
== VR_RANGE
1234 && vr1type
== VR_RANGE
)
1236 else if (*vr0type
== VR_ANTI_RANGE
1237 && vr1type
== VR_ANTI_RANGE
)
1243 else if (*vr0type
== VR_ANTI_RANGE
1244 && vr1type
== VR_RANGE
)
1246 /* Arbitrarily choose the right or left gap. */
1247 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
1248 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
1249 build_int_cst (TREE_TYPE (vr1min
), 1));
1250 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
1251 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
1252 build_int_cst (TREE_TYPE (vr1max
), 1));
1256 else if (*vr0type
== VR_RANGE
1257 && vr1type
== VR_ANTI_RANGE
)
1258 /* The result covers everything. */
1263 else if ((maxeq
|| cmpmax
== -1)
1264 && (mineq
|| cmpmin
== 1))
1266 /* ( [ ] ) or ([ ] ) or ( [ ]) */
1267 if (*vr0type
== VR_RANGE
1268 && vr1type
== VR_RANGE
)
1274 else if (*vr0type
== VR_ANTI_RANGE
1275 && vr1type
== VR_ANTI_RANGE
)
1277 else if (*vr0type
== VR_RANGE
1278 && vr1type
== VR_ANTI_RANGE
)
1280 *vr0type
= VR_ANTI_RANGE
;
1281 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
1283 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
1284 build_int_cst (TREE_TYPE (*vr0min
), 1));
1287 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
1289 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
1290 build_int_cst (TREE_TYPE (*vr0max
), 1));
1296 else if (*vr0type
== VR_ANTI_RANGE
1297 && vr1type
== VR_RANGE
)
1298 /* The result covers everything. */
1303 else if (cmpmin
== -1
1305 && (operand_less_p (vr1min
, *vr0max
) == 1
1306 || operand_equal_p (vr1min
, *vr0max
, 0)))
1308 /* [ ( ] ) or [ ]( ) */
1309 if (*vr0type
== VR_RANGE
1310 && vr1type
== VR_RANGE
)
1312 else if (*vr0type
== VR_ANTI_RANGE
1313 && vr1type
== VR_ANTI_RANGE
)
1315 else if (*vr0type
== VR_ANTI_RANGE
1316 && vr1type
== VR_RANGE
)
1318 if (TREE_CODE (vr1min
) == INTEGER_CST
)
1319 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
1320 build_int_cst (TREE_TYPE (vr1min
), 1));
1324 else if (*vr0type
== VR_RANGE
1325 && vr1type
== VR_ANTI_RANGE
)
1327 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
1330 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
1331 build_int_cst (TREE_TYPE (*vr0max
), 1));
1340 else if (cmpmin
== 1
1342 && (operand_less_p (*vr0min
, vr1max
) == 1
1343 || operand_equal_p (*vr0min
, vr1max
, 0)))
1345 /* ( [ ) ] or ( )[ ] */
1346 if (*vr0type
== VR_RANGE
1347 && vr1type
== VR_RANGE
)
1349 else if (*vr0type
== VR_ANTI_RANGE
1350 && vr1type
== VR_ANTI_RANGE
)
1352 else if (*vr0type
== VR_ANTI_RANGE
1353 && vr1type
== VR_RANGE
)
1355 if (TREE_CODE (vr1max
) == INTEGER_CST
)
1356 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
1357 build_int_cst (TREE_TYPE (vr1max
), 1));
1361 else if (*vr0type
== VR_RANGE
1362 && vr1type
== VR_ANTI_RANGE
)
1364 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
1367 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
1368 build_int_cst (TREE_TYPE (*vr0min
), 1));
1383 *vr0type
= VR_VARYING
;
1384 *vr0min
= NULL_TREE
;
1385 *vr0max
= NULL_TREE
;
1388 /* Helper for meet operation for value ranges. Given two ranges VR0
1389 and VR1, set VR0 to the union of both ranges. This may not be the
1390 smallest possible such range. */
1393 irange::legacy_union (irange
*vr0
, const irange
*vr1
)
1395 gcc_checking_assert (vr0
->legacy_mode_p ());
1396 gcc_checking_assert (vr1
->legacy_mode_p ());
1398 /* VR0 has the resulting range if VR1 is undefined or VR0 is varying. */
1399 if (vr1
->undefined_p ()
1400 || vr0
->varying_p ())
1403 /* VR1 has the resulting range if VR0 is undefined or VR1 is varying. */
1404 if (vr0
->undefined_p ())
1406 vr0
->set (vr1
->min (), vr1
->max (), vr1
->kind ());
1410 if (vr1
->varying_p ())
1412 vr0
->set_varying (vr1
->type ());
1416 value_range_kind vr0kind
= vr0
->kind ();
1417 tree vr0min
= vr0
->min ();
1418 tree vr0max
= vr0
->max ();
1420 union_ranges (&vr0kind
, &vr0min
, &vr0max
,
1421 vr1
->kind (), vr1
->min (), vr1
->max ());
1423 if (vr0kind
== VR_UNDEFINED
)
1424 vr0
->set_undefined ();
1425 else if (vr0kind
== VR_VARYING
)
1427 /* Failed to find an efficient meet. Before giving up and
1428 setting the result to VARYING, see if we can at least derive
1429 a non-zero range. */
1430 if (range_includes_zero_p (vr0
) == 0
1431 && range_includes_zero_p (vr1
) == 0)
1432 vr0
->set_nonzero (vr0
->type ());
1434 vr0
->set_varying (vr0
->type ());
1437 vr0
->set (vr0min
, vr0max
, vr0kind
);
1440 /* Meet operation for value ranges. Given two value ranges VR0 and
1441 VR1, store in VR0 a range that contains both VR0 and VR1. This
1442 may not be the smallest possible such range.
1443 Return TRUE if the original value changes. */
1446 irange::legacy_verbose_union_ (const irange
*other
)
1448 if (legacy_mode_p ())
1450 if (!other
->legacy_mode_p ())
1452 int_range
<1> tmp
= *other
;
1453 legacy_union (this, &tmp
);
1456 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1458 fprintf (dump_file
, "Meeting\n ");
1459 dump_value_range (dump_file
, this);
1460 fprintf (dump_file
, "\nand\n ");
1461 dump_value_range (dump_file
, other
);
1462 fprintf (dump_file
, "\n");
1465 legacy_union (this, other
);
1467 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1469 fprintf (dump_file
, "to\n ");
1470 dump_value_range (dump_file
, this);
1471 fprintf (dump_file
, "\n");
1476 if (other
->legacy_mode_p ())
1478 int_range
<2> wider
= *other
;
1479 return irange_union (wider
);
1482 return irange_union (*other
);
1486 irange::legacy_verbose_intersect (const irange
*other
)
1488 if (legacy_mode_p ())
1490 if (!other
->legacy_mode_p ())
1492 int_range
<1> tmp
= *other
;
1493 legacy_intersect (this, &tmp
);
1496 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1498 fprintf (dump_file
, "Intersecting\n ");
1499 dump_value_range (dump_file
, this);
1500 fprintf (dump_file
, "\nand\n ");
1501 dump_value_range (dump_file
, other
);
1502 fprintf (dump_file
, "\n");
1505 legacy_intersect (this, other
);
1507 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1509 fprintf (dump_file
, "to\n ");
1510 dump_value_range (dump_file
, this);
1511 fprintf (dump_file
, "\n");
1516 if (other
->legacy_mode_p ())
1520 return irange_intersect (wider
);
1523 return irange_intersect (*other
);
1526 // Perform an efficient union with R when both ranges have only a single pair.
1527 // Excluded are VARYING and UNDEFINED ranges.
1530 irange::irange_single_pair_union (const irange
&r
)
1532 gcc_checking_assert (!undefined_p () && !varying_p ());
1533 gcc_checking_assert (!r
.undefined_p () && !varying_p ());
1535 signop sign
= TYPE_SIGN (TREE_TYPE (m_base
[0]));
1536 // Check if current lower bound is also the new lower bound.
1537 if (wi::le_p (wi::to_wide (m_base
[0]), wi::to_wide (r
.m_base
[0]), sign
))
1539 // If current upper bound is new upper bound, we're done.
1540 if (wi::le_p (wi::to_wide (r
.m_base
[1]), wi::to_wide (m_base
[1]), sign
))
1542 // Otherwise R has the new upper bound.
1543 // Check for overlap/touching ranges, or single target range.
1544 if (m_max_ranges
== 1
1545 || wi::to_widest (m_base
[1]) + 1 >= wi::to_widest (r
.m_base
[0]))
1547 m_base
[1] = r
.m_base
[1];
1548 if (varying_compatible_p ())
1549 m_kind
= VR_VARYING
;
1553 // This is a dual range result.
1554 m_base
[2] = r
.m_base
[0];
1555 m_base
[3] = r
.m_base
[1];
1563 // Set the new lower bound to R's lower bound.
1564 tree lb
= m_base
[0];
1565 m_base
[0] = r
.m_base
[0];
1567 // If R fully contains THIS range, just set the upper bound.
1568 if (wi::ge_p (wi::to_wide (r
.m_base
[1]), wi::to_wide (m_base
[1]), sign
))
1569 m_base
[1] = r
.m_base
[1];
1570 // Check for overlapping ranges, or target limited to a single range.
1571 else if (m_max_ranges
== 1
1572 || wi::to_widest (r
.m_base
[1]) + 1 >= wi::to_widest (lb
))
1574 // This has the new upper bound, just check for varying.
1575 if (varying_compatible_p ())
1576 m_kind
= VR_VARYING
;
1580 // Left with 2 pairs.
1583 m_base
[3] = m_base
[1];
1584 m_base
[1] = r
.m_base
[1];
1591 // union_ for multi-ranges.
1594 irange::irange_union (const irange
&r
)
1596 gcc_checking_assert (!legacy_mode_p () && !r
.legacy_mode_p ());
1598 if (r
.undefined_p () || varying_p ())
1601 if (undefined_p () || r
.varying_p ())
1607 // Special case one range union one range.
1608 if (m_num_ranges
== 1 && r
.m_num_ranges
== 1)
1609 return irange_single_pair_union (r
);
1611 // If this ranges fully contains R, then we need do nothing.
1612 if (irange_contains_p (r
))
1615 // Do not worry about merging and such by reserving twice as many
1616 // pairs as needed, and then simply sort the 2 ranges into this
1617 // intermediate form.
1619 // The intermediate result will have the property that the beginning
1620 // of each range is <= the beginning of the next range. There may
1621 // be overlapping ranges at this point. I.e. this would be valid
1622 // [-20, 10], [-10, 0], [0, 20], [40, 90] as it satisfies this
1623 // contraint : -20 < -10 < 0 < 40. When the range is rebuilt into r,
1624 // the merge is performed.
1626 // [Xi,Yi]..[Xn,Yn] U [Xj,Yj]..[Xm,Ym] --> [Xk,Yk]..[Xp,Yp]
1627 auto_vec
<tree
, 20> res (m_num_ranges
* 2 + r
.m_num_ranges
* 2);
1628 unsigned i
= 0, j
= 0, k
= 0;
1630 while (i
< m_num_ranges
* 2 && j
< r
.m_num_ranges
* 2)
1632 // lower of Xi and Xj is the lowest point.
1633 if (wi::to_widest (m_base
[i
]) <= wi::to_widest (r
.m_base
[j
]))
1635 res
.quick_push (m_base
[i
]);
1636 res
.quick_push (m_base
[i
+ 1]);
1642 res
.quick_push (r
.m_base
[j
]);
1643 res
.quick_push (r
.m_base
[j
+ 1]);
1648 for ( ; i
< m_num_ranges
* 2; i
+= 2)
1650 res
.quick_push (m_base
[i
]);
1651 res
.quick_push (m_base
[i
+ 1]);
1654 for ( ; j
< r
.m_num_ranges
* 2; j
+= 2)
1656 res
.quick_push (r
.m_base
[j
]);
1657 res
.quick_push (r
.m_base
[j
+ 1]);
1661 // Now normalize the vector removing any overlaps.
1663 for (j
= 2; j
< k
; j
+= 2)
1665 // Current upper+1 is >= lower bound next pair, then we merge ranges.
1666 if (wi::to_widest (res
[i
- 1]) + 1 >= wi::to_widest (res
[j
]))
1668 // New upper bounds is greater of current or the next one.
1669 if (wi::to_widest (res
[j
+ 1]) > wi::to_widest (res
[i
- 1]))
1670 res
[i
- 1] = res
[j
+ 1];
1674 // This is a new distinct range, but no point in copying it
1675 // if it is already in the right place.
1679 res
[i
++] = res
[j
+ 1];
1686 // At this point, the vector should have i ranges, none overlapping.
1687 // Now it simply needs to be copied, and if there are too many
1688 // ranges, merge some. We wont do any analysis as to what the
1689 // "best" merges are, simply combine the final ranges into one.
1690 if (i
> m_max_ranges
* 2)
1692 res
[m_max_ranges
* 2 - 1] = res
[i
- 1];
1693 i
= m_max_ranges
* 2;
1696 for (j
= 0; j
< i
; j
++)
1697 m_base
[j
] = res
[j
];
1698 m_num_ranges
= i
/ 2;
1708 // Return TRUE if THIS fully contains R. No undefined or varying cases.
1711 irange::irange_contains_p (const irange
&r
) const
1713 gcc_checking_assert (!undefined_p () && !varying_p ());
1714 gcc_checking_assert (!r
.undefined_p () && !varying_p ());
1716 // In order for THIS to fully contain R, all of the pairs within R must
1717 // be fully contained by the pairs in this object.
1718 signop sign
= TYPE_SIGN (TREE_TYPE(m_base
[0]));
1721 tree rl
= r
.m_base
[0];
1722 tree ru
= r
.m_base
[1];
1727 // If r is contained within this range, move to the next R
1728 if (wi::ge_p (wi::to_wide (rl
), wi::to_wide (l
), sign
)
1729 && wi::le_p (wi::to_wide (ru
), wi::to_wide (u
), sign
))
1731 // This pair is OK, Either done, or bump to the next.
1732 if (++ri
>= r
.num_pairs ())
1734 rl
= r
.m_base
[ri
* 2];
1735 ru
= r
.m_base
[ri
* 2 + 1];
1738 // Otherwise, check if this's pair occurs before R's.
1739 if (wi::lt_p (wi::to_wide (u
), wi::to_wide (rl
), sign
))
1741 // THere's still at leats one pair of R left.
1742 if (++i
>= num_pairs ())
1745 u
= m_base
[i
* 2 + 1];
1754 // Intersect for multi-ranges. Return TRUE if anything changes.
1757 irange::irange_intersect (const irange
&r
)
1759 gcc_checking_assert (!legacy_mode_p () && !r
.legacy_mode_p ());
1760 gcc_checking_assert (undefined_p () || r
.undefined_p ()
1761 || range_compatible_p (type (), r
.type ()));
1763 if (undefined_p () || r
.varying_p ())
1765 if (r
.undefined_p ())
1776 if (r
.num_pairs () == 1)
1777 return intersect (r
.lower_bound (), r
.upper_bound ());
1779 // If R fully contains this, then intersection will change nothing.
1780 if (r
.irange_contains_p (*this))
1783 signop sign
= TYPE_SIGN (TREE_TYPE(m_base
[0]));
1784 unsigned bld_pair
= 0;
1785 unsigned bld_lim
= m_max_ranges
;
1786 int_range_max
r2 (*this);
1787 unsigned r2_lim
= r2
.num_pairs ();
1789 for (unsigned i
= 0; i
< r
.num_pairs (); )
1791 // If r1's upper is < r2's lower, we can skip r1's pair.
1792 tree ru
= r
.m_base
[i
* 2 + 1];
1793 tree r2l
= r2
.m_base
[i2
* 2];
1794 if (wi::lt_p (wi::to_wide (ru
), wi::to_wide (r2l
), sign
))
1799 // Likewise, skip r2's pair if its excluded.
1800 tree r2u
= r2
.m_base
[i2
* 2 + 1];
1801 tree rl
= r
.m_base
[i
* 2];
1802 if (wi::lt_p (wi::to_wide (r2u
), wi::to_wide (rl
), sign
))
1807 // No more r2, break.
1811 // Must be some overlap. Find the highest of the lower bounds,
1812 // and set it, unless the build limits lower bounds is already
1814 if (bld_pair
< bld_lim
)
1816 if (wi::ge_p (wi::to_wide (rl
), wi::to_wide (r2l
), sign
))
1817 m_base
[bld_pair
* 2] = rl
;
1819 m_base
[bld_pair
* 2] = r2l
;
1822 // Decrease and set a new upper.
1825 // ...and choose the lower of the upper bounds.
1826 if (wi::le_p (wi::to_wide (ru
), wi::to_wide (r2u
), sign
))
1828 m_base
[bld_pair
* 2 + 1] = ru
;
1830 // Move past the r1 pair and keep trying.
1836 m_base
[bld_pair
* 2 + 1] = r2u
;
1841 // No more r2, break.
1844 // r2 has the higher lower bound.
1847 // At the exit of this loop, it is one of 2 things:
1848 // ran out of r1, or r2, but either means we are done.
1849 m_num_ranges
= bld_pair
;
1861 // Multirange intersect for a specified wide_int [lb, ub] range.
1862 // Return TRUE if intersect changed anything.
1865 irange::intersect (const wide_int
& lb
, const wide_int
& ub
)
1867 // Undefined remains undefined.
1871 if (legacy_mode_p ())
1873 intersect (int_range
<1> (type (), lb
, ub
));
1877 tree range_type
= type();
1878 signop sign
= TYPE_SIGN (range_type
);
1880 gcc_checking_assert (TYPE_PRECISION (range_type
) == wi::get_precision (lb
));
1881 gcc_checking_assert (TYPE_PRECISION (range_type
) == wi::get_precision (ub
));
1883 // If this range is fuly contained, then intersection will do nothing.
1884 if (wi::ge_p (lower_bound (), lb
, sign
)
1885 && wi::le_p (upper_bound (), ub
, sign
))
1888 unsigned bld_index
= 0;
1889 unsigned pair_lim
= num_pairs ();
1890 for (unsigned i
= 0; i
< pair_lim
; i
++)
1892 tree pairl
= m_base
[i
* 2];
1893 tree pairu
= m_base
[i
* 2 + 1];
1894 // Once UB is less than a pairs lower bound, we're done.
1895 if (wi::lt_p (ub
, wi::to_wide (pairl
), sign
))
1897 // if LB is greater than this pairs upper, this pair is excluded.
1898 if (wi::lt_p (wi::to_wide (pairu
), lb
, sign
))
1901 // Must be some overlap. Find the highest of the lower bounds,
1903 if (wi::gt_p (lb
, wi::to_wide (pairl
), sign
))
1904 m_base
[bld_index
* 2] = wide_int_to_tree (range_type
, lb
);
1906 m_base
[bld_index
* 2] = pairl
;
1908 // ...and choose the lower of the upper bounds and if the base pair
1909 // has the lower upper bound, need to check next pair too.
1910 if (wi::lt_p (ub
, wi::to_wide (pairu
), sign
))
1912 m_base
[bld_index
++ * 2 + 1] = wide_int_to_tree (range_type
, ub
);
1916 m_base
[bld_index
++ * 2 + 1] = pairu
;
1919 m_num_ranges
= bld_index
;
1930 // Signed 1-bits are strange. You can't subtract 1, because you can't
1931 // represent the number 1. This works around that for the invert routine.
1933 static wide_int
inline
1934 subtract_one (const wide_int
&x
, tree type
, wi::overflow_type
&overflow
)
1936 if (TYPE_SIGN (type
) == SIGNED
)
1937 return wi::add (x
, -1, SIGNED
, &overflow
);
1939 return wi::sub (x
, 1, UNSIGNED
, &overflow
);
1942 // The analogous function for adding 1.
1944 static wide_int
inline
1945 add_one (const wide_int
&x
, tree type
, wi::overflow_type
&overflow
)
1947 if (TYPE_SIGN (type
) == SIGNED
)
1948 return wi::sub (x
, -1, SIGNED
, &overflow
);
1950 return wi::add (x
, 1, UNSIGNED
, &overflow
);
1953 // Return the inverse of a range.
1958 if (legacy_mode_p ())
1960 // We can't just invert VR_RANGE and VR_ANTI_RANGE because we may
1961 // create non-canonical ranges. Use the constructors instead.
1962 if (m_kind
== VR_RANGE
)
1963 *this = value_range (min (), max (), VR_ANTI_RANGE
);
1964 else if (m_kind
== VR_ANTI_RANGE
)
1965 *this = value_range (min (), max ());
1971 gcc_checking_assert (!undefined_p () && !varying_p ());
1973 // We always need one more set of bounds to represent an inverse, so
1974 // if we're at the limit, we can't properly represent things.
1976 // For instance, to represent the inverse of a 2 sub-range set
1977 // [5, 10][20, 30], we would need a 3 sub-range set
1978 // [-MIN, 4][11, 19][31, MAX].
1980 // In this case, return the most conservative thing.
1982 // However, if any of the extremes of the range are -MIN/+MAX, we
1983 // know we will not need an extra bound. For example:
1985 // INVERT([-MIN,20][30,40]) => [21,29][41,+MAX]
1986 // INVERT([-MIN,20][30,MAX]) => [21,29]
1987 tree ttype
= type ();
1988 unsigned prec
= TYPE_PRECISION (ttype
);
1989 signop sign
= TYPE_SIGN (ttype
);
1990 wide_int type_min
= wi::min_value (prec
, sign
);
1991 wide_int type_max
= wi::max_value (prec
, sign
);
1992 if (m_num_ranges
== m_max_ranges
1993 && lower_bound () != type_min
1994 && upper_bound () != type_max
)
1996 m_base
[1] = wide_int_to_tree (ttype
, type_max
);
2000 // The algorithm is as follows. To calculate INVERT ([a,b][c,d]), we
2001 // generate [-MIN, a-1][b+1, c-1][d+1, MAX].
2003 // If there is an over/underflow in the calculation for any
2004 // sub-range, we eliminate that subrange. This allows us to easily
2005 // calculate INVERT([-MIN, 5]) with: [-MIN, -MIN-1][6, MAX]. And since
2006 // we eliminate the underflow, only [6, MAX] remains.
2008 wi::overflow_type ovf
;
2009 // Construct leftmost range.
2010 int_range_max
orig_range (*this);
2011 unsigned nitems
= 0;
2013 // If this is going to underflow on the MINUS 1, don't even bother
2014 // checking. This also handles subtracting one from an unsigned 0,
2015 // which doesn't set the underflow bit.
2016 if (type_min
!= orig_range
.lower_bound ())
2018 m_base
[nitems
++] = wide_int_to_tree (ttype
, type_min
);
2019 tmp
= subtract_one (orig_range
.lower_bound (), ttype
, ovf
);
2020 m_base
[nitems
++] = wide_int_to_tree (ttype
, tmp
);
2025 // Construct middle ranges if applicable.
2026 if (orig_range
.num_pairs () > 1)
2029 for (; j
< (orig_range
.num_pairs () * 2) - 1; j
+= 2)
2031 // The middle ranges cannot have MAX/MIN, so there's no need
2032 // to check for unsigned overflow on the +1 and -1 here.
2033 tmp
= wi::add (wi::to_wide (orig_range
.m_base
[j
]), 1, sign
, &ovf
);
2034 m_base
[nitems
++] = wide_int_to_tree (ttype
, tmp
);
2035 tmp
= subtract_one (wi::to_wide (orig_range
.m_base
[j
+ 1]),
2037 m_base
[nitems
++] = wide_int_to_tree (ttype
, tmp
);
2043 // Construct rightmost range.
2045 // However, if this will overflow on the PLUS 1, don't even bother.
2046 // This also handles adding one to an unsigned MAX, which doesn't
2047 // set the overflow bit.
2048 if (type_max
!= wi::to_wide (orig_range
.m_base
[i
]))
2050 tmp
= add_one (wi::to_wide (orig_range
.m_base
[i
]), ttype
, ovf
);
2051 m_base
[nitems
++] = wide_int_to_tree (ttype
, tmp
);
2052 m_base
[nitems
++] = wide_int_to_tree (ttype
, type_max
);
2056 m_num_ranges
= nitems
/ 2;
2058 // We disallow undefined or varying coming in, so the result can
2059 // only be a VR_RANGE.
2060 gcc_checking_assert (m_kind
== VR_RANGE
);
2067 dump_bound_with_infinite_markers (FILE *file
, tree bound
)
2069 tree type
= TREE_TYPE (bound
);
2070 wide_int type_min
= wi::min_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
2071 wide_int type_max
= wi::max_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
2073 if (INTEGRAL_TYPE_P (type
)
2074 && !TYPE_UNSIGNED (type
)
2075 && TREE_CODE (bound
) == INTEGER_CST
2076 && wi::to_wide (bound
) == type_min
2077 && TYPE_PRECISION (type
) != 1)
2078 fprintf (file
, "-INF");
2079 else if (TREE_CODE (bound
) == INTEGER_CST
2080 && wi::to_wide (bound
) == type_max
2081 && TYPE_PRECISION (type
) != 1)
2082 fprintf (file
, "+INF");
2084 print_generic_expr (file
, bound
);
2088 irange::dump (FILE *file
) const
2092 fprintf (file
, "UNDEFINED");
2095 print_generic_expr (file
, type ());
2096 fprintf (file
, " ");
2099 fprintf (file
, "VARYING");
2102 if (legacy_mode_p ())
2104 fprintf (file
, "%s[", (m_kind
== VR_ANTI_RANGE
) ? "~" : "");
2105 dump_bound_with_infinite_markers (file
, min ());
2106 fprintf (file
, ", ");
2107 dump_bound_with_infinite_markers (file
, max ());
2108 fprintf (file
, "]");
2111 for (unsigned i
= 0; i
< m_num_ranges
; ++i
)
2113 tree lb
= m_base
[i
* 2];
2114 tree ub
= m_base
[i
* 2 + 1];
2115 fprintf (file
, "[");
2116 dump_bound_with_infinite_markers (file
, lb
);
2117 fprintf (file
, ", ");
2118 dump_bound_with_infinite_markers (file
, ub
);
2119 fprintf (file
, "]");
2124 irange::debug () const
2127 fprintf (stderr
, "\n");
2131 dump_value_range (FILE *file
, const irange
*vr
)
2137 debug (const irange
*vr
)
2139 dump_value_range (stderr
, vr
);
2140 fprintf (stderr
, "\n");
2144 debug (const irange
&vr
)
2150 debug (const value_range
*vr
)
2152 dump_value_range (stderr
, vr
);
2153 fprintf (stderr
, "\n");
2157 debug (const value_range
&vr
)
2159 dump_value_range (stderr
, &vr
);
2160 fprintf (stderr
, "\n");
2163 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2164 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2165 false otherwise. If *AR can be represented with a single range
2166 *VR1 will be VR_UNDEFINED. */
2169 ranges_from_anti_range (const value_range
*ar
,
2170 value_range
*vr0
, value_range
*vr1
)
2172 tree type
= ar
->type ();
2174 vr0
->set_undefined ();
2175 vr1
->set_undefined ();
2177 /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U
2178 [A+1, +INF]. Not sure if this helps in practice, though. */
2180 if (ar
->kind () != VR_ANTI_RANGE
2181 || TREE_CODE (ar
->min ()) != INTEGER_CST
2182 || TREE_CODE (ar
->max ()) != INTEGER_CST
2183 || !vrp_val_min (type
)
2184 || !vrp_val_max (type
))
2187 if (tree_int_cst_lt (vrp_val_min (type
), ar
->min ()))
2188 vr0
->set (vrp_val_min (type
),
2189 wide_int_to_tree (type
, wi::to_wide (ar
->min ()) - 1));
2190 if (tree_int_cst_lt (ar
->max (), vrp_val_max (type
)))
2191 vr1
->set (wide_int_to_tree (type
, wi::to_wide (ar
->max ()) + 1),
2192 vrp_val_max (type
));
2193 if (vr0
->undefined_p ())
2196 vr1
->set_undefined ();
2199 return !vr0
->undefined_p ();
2203 range_has_numeric_bounds_p (const irange
*vr
)
2205 return (!vr
->undefined_p ()
2206 && TREE_CODE (vr
->min ()) == INTEGER_CST
2207 && TREE_CODE (vr
->max ()) == INTEGER_CST
);
2210 /* Return whether VAL is equal to the maximum value of its type.
2211 We can't do a simple equality comparison with TYPE_MAX_VALUE because
2212 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
2213 is not == to the integer constant with the same value in the type. */
2216 vrp_val_is_max (const_tree val
)
2218 tree type_max
= vrp_val_max (TREE_TYPE (val
));
2219 return (val
== type_max
2220 || (type_max
!= NULL_TREE
2221 && operand_equal_p (val
, type_max
, 0)));
2224 /* Return whether VAL is equal to the minimum value of its type. */
2227 vrp_val_is_min (const_tree val
)
2229 tree type_min
= vrp_val_min (TREE_TYPE (val
));
2230 return (val
== type_min
2231 || (type_min
!= NULL_TREE
2232 && operand_equal_p (val
, type_min
, 0)));
2235 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
2238 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
2242 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
2247 // ?? These stubs are for ipa-prop.cc which use a value_range in a
2248 // hash_traits. hash-traits.h defines an extern of gt_ggc_mx (T &)
2249 // instead of picking up the gt_ggc_mx (T *) version.
2251 gt_pch_nx (int_range
<1> *&x
)
2253 return gt_pch_nx ((irange
*) x
);
2257 gt_ggc_mx (int_range
<1> *&x
)
2259 return gt_ggc_mx ((irange
*) x
);
2262 #define DEFINE_INT_RANGE_INSTANCE(N) \
2263 template int_range<N>::int_range(tree, tree, value_range_kind); \
2264 template int_range<N>::int_range(tree_node *, \
2267 value_range_kind); \
2268 template int_range<N>::int_range(tree); \
2269 template int_range<N>::int_range(const irange &); \
2270 template int_range<N>::int_range(const int_range &); \
2271 template int_range<N>& int_range<N>::operator= (const int_range &);
2273 DEFINE_INT_RANGE_INSTANCE(1)
2274 DEFINE_INT_RANGE_INSTANCE(2)
2275 DEFINE_INT_RANGE_INSTANCE(3)
2276 DEFINE_INT_RANGE_INSTANCE(255)
2279 #include "selftest.h"
2283 #define INT(N) build_int_cst (integer_type_node, (N))
2284 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
2285 #define UINT128(N) build_int_cstu (u128_type, (N))
2286 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
2287 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
2290 build_range3 (int a
, int b
, int c
, int d
, int e
, int f
)
2292 int_range
<3> i1 (INT (a
), INT (b
));
2293 int_range
<3> i2 (INT (c
), INT (d
));
2294 int_range
<3> i3 (INT (e
), INT (f
));
2301 range_tests_irange3 ()
2303 typedef int_range
<3> int_range3
;
2304 int_range3 r0
, r1
, r2
;
2305 int_range3 i1
, i2
, i3
;
2307 // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20].
2308 r0
= int_range3 (INT (10), INT (20));
2309 r1
= int_range3 (INT (5), INT (8));
2311 r1
= int_range3 (INT (1), INT (3));
2313 ASSERT_TRUE (r0
== build_range3 (1, 3, 5, 8, 10, 20));
2315 // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20].
2316 r1
= int_range3 (INT (-5), INT (0));
2318 ASSERT_TRUE (r0
== build_range3 (-5, 3, 5, 8, 10, 20));
2320 // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60].
2321 r1
= int_range3 (INT (50), INT (60));
2322 r0
= int_range3 (INT (10), INT (20));
2323 r0
.union_ (int_range3 (INT (30), INT (40)));
2325 ASSERT_TRUE (r0
== build_range3 (10, 20, 30, 40, 50, 60));
2326 // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80].
2327 r1
= int_range3 (INT (70), INT (80));
2330 r2
= build_range3 (10, 20, 30, 40, 50, 60);
2331 r2
.union_ (int_range3 (INT (70), INT (80)));
2332 ASSERT_TRUE (r0
== r2
);
2334 // [10,20][30,40][50,60] U [6,35] => [6,40][50,60].
2335 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2336 r1
= int_range3 (INT (6), INT (35));
2338 r1
= int_range3 (INT (6), INT (40));
2339 r1
.union_ (int_range3 (INT (50), INT (60)));
2340 ASSERT_TRUE (r0
== r1
);
2342 // [10,20][30,40][50,60] U [6,60] => [6,60].
2343 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2344 r1
= int_range3 (INT (6), INT (60));
2346 ASSERT_TRUE (r0
== int_range3 (INT (6), INT (60)));
2348 // [10,20][30,40][50,60] U [6,70] => [6,70].
2349 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2350 r1
= int_range3 (INT (6), INT (70));
2352 ASSERT_TRUE (r0
== int_range3 (INT (6), INT (70)));
2354 // [10,20][30,40][50,60] U [35,70] => [10,20][30,70].
2355 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2356 r1
= int_range3 (INT (35), INT (70));
2358 r1
= int_range3 (INT (10), INT (20));
2359 r1
.union_ (int_range3 (INT (30), INT (70)));
2360 ASSERT_TRUE (r0
== r1
);
2362 // [10,20][30,40][50,60] U [15,35] => [10,40][50,60].
2363 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2364 r1
= int_range3 (INT (15), INT (35));
2366 r1
= int_range3 (INT (10), INT (40));
2367 r1
.union_ (int_range3 (INT (50), INT (60)));
2368 ASSERT_TRUE (r0
== r1
);
2370 // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60].
2371 r0
= build_range3 (10, 20, 30, 40, 50, 60);
2372 r1
= int_range3 (INT (35), INT (35));
2374 ASSERT_TRUE (r0
== build_range3 (10, 20, 30, 40, 50, 60));
2378 range_tests_int_range_max ()
2381 unsigned int nrange
;
2383 // Build a huge multi-range range.
2384 for (nrange
= 0; nrange
< 50; ++nrange
)
2386 int_range
<1> tmp (INT (nrange
*10), INT (nrange
*10 + 5));
2389 ASSERT_TRUE (big
.num_pairs () == nrange
);
2391 // Verify that we can copy it without loosing precision.
2392 int_range_max
copy (big
);
2393 ASSERT_TRUE (copy
.num_pairs () == nrange
);
2395 // Inverting it should produce one more sub-range.
2397 ASSERT_TRUE (big
.num_pairs () == nrange
+ 1);
2399 int_range
<1> tmp (INT (5), INT (37));
2400 big
.intersect (tmp
);
2401 ASSERT_TRUE (big
.num_pairs () == 4);
2403 // Test that [10,10][20,20] does NOT contain 15.
2405 int_range_max
i1 (build_int_cst (integer_type_node
, 10),
2406 build_int_cst (integer_type_node
, 10));
2407 int_range_max
i2 (build_int_cst (integer_type_node
, 20),
2408 build_int_cst (integer_type_node
, 20));
2410 ASSERT_FALSE (i1
.contains_p (build_int_cst (integer_type_node
, 15)));
2415 range_tests_legacy ()
2417 // Test truncating copy to int_range<1>.
2418 int_range
<3> big
= build_range3 (10, 20, 30, 40, 50, 60);
2419 int_range
<1> small
= big
;
2420 ASSERT_TRUE (small
== int_range
<1> (INT (10), INT (60)));
2422 // Test truncating copy to int_range<2>.
2423 int_range
<2> medium
= big
;
2424 ASSERT_TRUE (!medium
.undefined_p ());
2426 // Test that a truncating copy of [MIN,20][22,40][80,MAX]
2427 // ends up as a conservative anti-range of ~[21,21].
2428 big
= int_range
<3> (vrp_val_min (integer_type_node
), INT (20));
2429 big
.union_ (int_range
<1> (INT (22), INT (40)));
2430 big
.union_ (int_range
<1> (INT (80), vrp_val_max (integer_type_node
)));
2432 ASSERT_TRUE (small
== int_range
<1> (INT (21), INT (21), VR_ANTI_RANGE
));
2434 // Copying a legacy symbolic to an int_range should normalize the
2435 // symbolic at copy time.
2437 tree ssa
= make_ssa_name (integer_type_node
);
2438 value_range
legacy_range (ssa
, INT (25));
2439 int_range
<2> copy
= legacy_range
;
2440 ASSERT_TRUE (copy
== int_range
<2> (vrp_val_min (integer_type_node
),
2443 // Test that copying ~[abc_23, abc_23] to a multi-range yields varying.
2444 legacy_range
= value_range (ssa
, ssa
, VR_ANTI_RANGE
);
2445 copy
= legacy_range
;
2446 ASSERT_TRUE (copy
.varying_p ());
2449 // VARYING of different sizes should not be equal.
2450 tree big_type
= build_nonstandard_integer_type (32, 1);
2451 tree small_type
= build_nonstandard_integer_type (16, 1);
2452 int_range_max
r0 (big_type
);
2453 int_range_max
r1 (small_type
);
2454 ASSERT_TRUE (r0
!= r1
);
2455 value_range
vr0 (big_type
);
2456 int_range_max
vr1 (small_type
);
2457 ASSERT_TRUE (vr0
!= vr1
);
2460 // Simulate -fstrict-enums where the domain of a type is less than the
2464 range_tests_strict_enum ()
2466 // The enum can only hold [0, 3].
2467 tree rtype
= copy_node (unsigned_type_node
);
2468 TYPE_MIN_VALUE (rtype
) = build_int_cstu (rtype
, 0);
2469 TYPE_MAX_VALUE (rtype
) = build_int_cstu (rtype
, 3);
2471 // Test that even though vr1 covers the strict enum domain ([0, 3]),
2472 // it does not cover the domain of the underlying type.
2473 int_range
<1> vr1 (build_int_cstu (rtype
, 0), build_int_cstu (rtype
, 1));
2474 int_range
<1> vr2 (build_int_cstu (rtype
, 2), build_int_cstu (rtype
, 3));
2476 ASSERT_TRUE (vr1
== int_range
<1> (build_int_cstu (rtype
, 0),
2477 build_int_cstu (rtype
, 3)));
2478 ASSERT_FALSE (vr1
.varying_p ());
2480 // Test that copying to a multi-range does not change things.
2481 int_range
<2> ir1 (vr1
);
2482 ASSERT_TRUE (ir1
== vr1
);
2483 ASSERT_FALSE (ir1
.varying_p ());
2485 // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3].
2486 vr1
= int_range
<1> (TYPE_MIN_VALUE (rtype
), TYPE_MAX_VALUE (rtype
));
2488 ASSERT_TRUE (ir1
== vr1
);
2489 ASSERT_FALSE (ir1
.varying_p ());
2495 tree u128_type
= build_nonstandard_integer_type (128, /*unsigned=*/1);
2496 int_range
<1> i1
, i2
, i3
;
2497 int_range
<1> r0
, r1
, rold
;
2499 // Test 1-bit signed integer union.
2500 // [-1,-1] U [0,0] = VARYING.
2501 tree one_bit_type
= build_nonstandard_integer_type (1, 0);
2502 tree one_bit_min
= vrp_val_min (one_bit_type
);
2503 tree one_bit_max
= vrp_val_max (one_bit_type
);
2505 int_range
<2> min (one_bit_min
, one_bit_min
);
2506 int_range
<2> max (one_bit_max
, one_bit_max
);
2508 ASSERT_TRUE (max
.varying_p ());
2511 // Test inversion of 1-bit signed integers.
2513 int_range
<2> min (one_bit_min
, one_bit_min
);
2514 int_range
<2> max (one_bit_max
, one_bit_max
);
2518 ASSERT_TRUE (t
== max
);
2521 ASSERT_TRUE (t
== min
);
2524 // Test that NOT(255) is [0..254] in 8-bit land.
2525 int_range
<1> not_255 (UCHAR (255), UCHAR (255), VR_ANTI_RANGE
);
2526 ASSERT_TRUE (not_255
== int_range
<1> (UCHAR (0), UCHAR (254)));
2528 // Test that NOT(0) is [1..255] in 8-bit land.
2529 int_range
<1> not_zero
= range_nonzero (unsigned_char_type_node
);
2530 ASSERT_TRUE (not_zero
== int_range
<1> (UCHAR (1), UCHAR (255)));
2532 // Check that [0,127][0x..ffffff80,0x..ffffff]
2533 // => ~[128, 0x..ffffff7f].
2534 r0
= int_range
<1> (UINT128 (0), UINT128 (127));
2535 tree high
= build_minus_one_cst (u128_type
);
2536 // low = -1 - 127 => 0x..ffffff80.
2537 tree low
= fold_build2 (MINUS_EXPR
, u128_type
, high
, UINT128(127));
2538 r1
= int_range
<1> (low
, high
); // [0x..ffffff80, 0x..ffffffff]
2539 // r0 = [0,127][0x..ffffff80,0x..fffffff].
2541 // r1 = [128, 0x..ffffff7f].
2542 r1
= int_range
<1> (UINT128(128),
2543 fold_build2 (MINUS_EXPR
, u128_type
,
2544 build_minus_one_cst (u128_type
),
2547 ASSERT_TRUE (r0
== r1
);
2549 r0
.set_varying (integer_type_node
);
2550 tree minint
= wide_int_to_tree (integer_type_node
, r0
.lower_bound ());
2551 tree maxint
= wide_int_to_tree (integer_type_node
, r0
.upper_bound ());
2553 r0
.set_varying (short_integer_type_node
);
2555 r0
.set_varying (unsigned_type_node
);
2556 tree maxuint
= wide_int_to_tree (unsigned_type_node
, r0
.upper_bound ());
2558 // Check that ~[0,5] => [6,MAX] for unsigned int.
2559 r0
= int_range
<1> (UINT (0), UINT (5));
2561 ASSERT_TRUE (r0
== int_range
<1> (UINT(6), maxuint
));
2563 // Check that ~[10,MAX] => [0,9] for unsigned int.
2564 r0
= int_range
<1> (UINT(10), maxuint
);
2566 ASSERT_TRUE (r0
== int_range
<1> (UINT (0), UINT (9)));
2568 // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers.
2569 r0
= int_range
<1> (UINT128 (0), UINT128 (5), VR_ANTI_RANGE
);
2570 r1
= int_range
<1> (UINT128(6), build_minus_one_cst (u128_type
));
2571 ASSERT_TRUE (r0
== r1
);
2573 // Check that [~5] is really [-MIN,4][6,MAX].
2574 r0
= int_range
<1> (INT (5), INT (5), VR_ANTI_RANGE
);
2575 r1
= int_range
<1> (minint
, INT (4));
2576 r1
.union_ (int_range
<1> (INT (6), maxint
));
2577 ASSERT_FALSE (r1
.undefined_p ());
2578 ASSERT_TRUE (r0
== r1
);
2580 r1
= int_range
<1> (INT (5), INT (5));
2581 int_range
<1> r2 (r1
);
2582 ASSERT_TRUE (r1
== r2
);
2584 r1
= int_range
<1> (INT (5), INT (10));
2586 r1
= int_range
<1> (integer_type_node
,
2587 wi::to_wide (INT (5)), wi::to_wide (INT (10)));
2588 ASSERT_TRUE (r1
.contains_p (INT (7)));
2590 r1
= int_range
<1> (SCHAR (0), SCHAR (20));
2591 ASSERT_TRUE (r1
.contains_p (SCHAR(15)));
2592 ASSERT_FALSE (r1
.contains_p (SCHAR(300)));
2594 // NOT([10,20]) ==> [-MIN,9][21,MAX].
2595 r0
= r1
= int_range
<1> (INT (10), INT (20));
2596 r2
= int_range
<1> (minint
, INT(9));
2597 r2
.union_ (int_range
<1> (INT(21), maxint
));
2598 ASSERT_FALSE (r2
.undefined_p ());
2600 ASSERT_TRUE (r1
== r2
);
2601 // Test that NOT(NOT(x)) == x.
2603 ASSERT_TRUE (r0
== r2
);
2605 // Test that booleans and their inverse work as expected.
2606 r0
= range_zero (boolean_type_node
);
2607 ASSERT_TRUE (r0
== int_range
<1> (build_zero_cst (boolean_type_node
),
2608 build_zero_cst (boolean_type_node
)));
2610 ASSERT_TRUE (r0
== int_range
<1> (build_one_cst (boolean_type_node
),
2611 build_one_cst (boolean_type_node
)));
2613 // Make sure NULL and non-NULL of pointer types work, and that
2614 // inverses of them are consistent.
2615 tree voidp
= build_pointer_type (void_type_node
);
2616 r0
= range_zero (voidp
);
2620 ASSERT_TRUE (r0
== r1
);
2622 // [10,20] U [15, 30] => [10, 30].
2623 r0
= int_range
<1> (INT (10), INT (20));
2624 r1
= int_range
<1> (INT (15), INT (30));
2626 ASSERT_TRUE (r0
== int_range
<1> (INT (10), INT (30)));
2628 // [15,40] U [] => [15,40].
2629 r0
= int_range
<1> (INT (15), INT (40));
2630 r1
.set_undefined ();
2632 ASSERT_TRUE (r0
== int_range
<1> (INT (15), INT (40)));
2634 // [10,20] U [10,10] => [10,20].
2635 r0
= int_range
<1> (INT (10), INT (20));
2636 r1
= int_range
<1> (INT (10), INT (10));
2638 ASSERT_TRUE (r0
== int_range
<1> (INT (10), INT (20)));
2640 // [10,20] U [9,9] => [9,20].
2641 r0
= int_range
<1> (INT (10), INT (20));
2642 r1
= int_range
<1> (INT (9), INT (9));
2644 ASSERT_TRUE (r0
== int_range
<1> (INT (9), INT (20)));
2646 // [10,20] ^ [15,30] => [15,20].
2647 r0
= int_range
<1> (INT (10), INT (20));
2648 r1
= int_range
<1> (INT (15), INT (30));
2650 ASSERT_TRUE (r0
== int_range
<1> (INT (15), INT (20)));
2652 // Test the internal sanity of wide_int's wrt HWIs.
2653 ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node
),
2654 TYPE_SIGN (boolean_type_node
))
2655 == wi::uhwi (1, TYPE_PRECISION (boolean_type_node
)));
2658 r0
= int_range
<1> (INT (0), INT (0));
2659 ASSERT_TRUE (r0
.zero_p ());
2661 // Test nonzero_p().
2662 r0
= int_range
<1> (INT (0), INT (0));
2664 ASSERT_TRUE (r0
.nonzero_p ());
2666 // test legacy interaction
2668 r0
= int_range
<1> (UINT (1), UINT (1), VR_ANTI_RANGE
);
2670 r1
= int_range
<1> (UINT (3), UINT (3), VR_ANTI_RANGE
);
2672 // vv = [0,0][2,2][4, MAX]
2673 int_range
<3> vv
= r0
;
2676 ASSERT_TRUE (vv
.contains_p (UINT (2)));
2677 ASSERT_TRUE (vv
.num_pairs () == 3);
2679 // create r0 as legacy [1,1]
2680 r0
= int_range
<1> (UINT (1), UINT (1));
2681 // And union it with [0,0][2,2][4,MAX] multi range
2683 // The result should be [0,2][4,MAX], or ~[3,3] but it must contain 2
2684 ASSERT_TRUE (r0
.contains_p (UINT (2)));
2690 range_tests_legacy ();
2691 range_tests_irange3 ();
2692 range_tests_int_range_max ();
2693 range_tests_strict_enum ();
2694 range_tests_misc ();
2697 } // namespace selftest
2699 #endif // CHECKING_P