IBM Z: Build autovec-*-signaling-eq.c tests with exceptions
[gcc.git] / gcc / value-range.cc
1 /* Support routines for value ranges.
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
3 Major hacks by Aldy Hernandez <aldyh@redhat.com> and
4 Andrew MacLeod <amacleod@redhat.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "ssa.h"
29 #include "tree-pretty-print.h"
30 #include "fold-const.h"
31
32 // Here we copy between any two irange's. The ranges can be legacy or
33 // multi-ranges, and copying between any combination works correctly.
34
35 irange &
36 irange::operator= (const irange &src)
37 {
38 if (legacy_mode_p ())
39 {
40 copy_to_legacy (src);
41 return *this;
42 }
43 if (src.legacy_mode_p ())
44 {
45 copy_legacy_to_multi_range (src);
46 return *this;
47 }
48
49 unsigned x;
50 unsigned lim = src.m_num_ranges;
51 if (lim > m_max_ranges)
52 lim = m_max_ranges;
53
54 for (x = 0; x < lim * 2; ++x)
55 m_base[x] = src.m_base[x];
56
57 // If the range didn't fit, the last range should cover the rest.
58 if (lim != src.m_num_ranges)
59 m_base[x - 1] = src.m_base[src.m_num_ranges * 2 - 1];
60
61 m_num_ranges = lim;
62 return *this;
63 }
64
65 // Return TRUE if range is a multi-range that can be represented as a
66 // VR_ANTI_RANGE.
67
68 bool
69 irange::maybe_anti_range () const
70 {
71 tree ttype = type ();
72 unsigned int precision = TYPE_PRECISION (ttype);
73 signop sign = TYPE_SIGN (ttype);
74 return (num_pairs () > 1
75 && precision > 1
76 && lower_bound () == wi::min_value (precision, sign)
77 && upper_bound () == wi::max_value (precision, sign));
78 }
79
80 void
81 irange::copy_legacy_to_multi_range (const irange &src)
82 {
83 gcc_checking_assert (src.legacy_mode_p ());
84 gcc_checking_assert (!legacy_mode_p ());
85 if (src.undefined_p ())
86 set_undefined ();
87 else if (src.varying_p ())
88 set_varying (src.type ());
89 else
90 {
91 if (range_has_numeric_bounds_p (&src))
92 set (src.min (), src.max (), src.kind ());
93 else
94 {
95 value_range cst (src);
96 cst.normalize_symbolics ();
97 gcc_checking_assert (cst.varying_p () || cst.kind () == VR_RANGE);
98 set (cst.min (), cst.max ());
99 }
100 }
101 }
102
103 // Copy any type of irange into a legacy.
104
105 void
106 irange::copy_to_legacy (const irange &src)
107 {
108 gcc_checking_assert (legacy_mode_p ());
109 // Copy legacy to legacy.
110 if (src.legacy_mode_p ())
111 {
112 m_num_ranges = src.m_num_ranges;
113 m_base[0] = src.m_base[0];
114 m_base[1] = src.m_base[1];
115 m_kind = src.m_kind;
116 return;
117 }
118 // Copy multi-range to legacy.
119 if (src.undefined_p ())
120 set_undefined ();
121 else if (src.varying_p ())
122 set_varying (src.type ());
123 else if (src.maybe_anti_range ())
124 {
125 int_range<3> r (src);
126 r.invert ();
127 // Use tree variants to save on tree -> wi -> tree conversions.
128 set (r.tree_lower_bound (0), r.tree_upper_bound (0), VR_ANTI_RANGE);
129 }
130 else
131 set (src.tree_lower_bound (), src.tree_upper_bound ());
132 }
133
134 // Swap MIN/MAX if they are out of order and adjust KIND appropriately.
135
136 static void
137 swap_out_of_order_endpoints (tree &min, tree &max, value_range_kind &kind)
138 {
139 gcc_checking_assert (kind != VR_UNDEFINED);
140 if (kind == VR_VARYING)
141 return;
142 /* Wrong order for min and max, to swap them and the VR type we need
143 to adjust them. */
144 if (tree_int_cst_lt (max, min))
145 {
146 tree one, tmp;
147
148 /* For one bit precision if max < min, then the swapped
149 range covers all values, so for VR_RANGE it is varying and
150 for VR_ANTI_RANGE empty range, so drop to varying as well. */
151 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
152 {
153 kind = VR_VARYING;
154 return;
155 }
156
157 one = build_int_cst (TREE_TYPE (min), 1);
158 tmp = int_const_binop (PLUS_EXPR, max, one);
159 max = int_const_binop (MINUS_EXPR, min, one);
160 min = tmp;
161
162 /* There's one corner case, if we had [C+1, C] before we now have
163 that again. But this represents an empty value range, so drop
164 to varying in this case. */
165 if (tree_int_cst_lt (max, min))
166 {
167 kind = VR_VARYING;
168 return;
169 }
170 kind = kind == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
171 }
172 }
173
174 void
175 irange::irange_set (tree min, tree max)
176 {
177 gcc_checking_assert (!POLY_INT_CST_P (min));
178 gcc_checking_assert (!POLY_INT_CST_P (max));
179
180 m_base[0] = min;
181 m_base[1] = max;
182 m_num_ranges = 1;
183 if (flag_checking)
184 verify_range ();
185 }
186
187 void
188 irange::irange_set_anti_range (tree min, tree max)
189 {
190 gcc_checking_assert (!POLY_INT_CST_P (min));
191 gcc_checking_assert (!POLY_INT_CST_P (max));
192
193 // set an anti-range
194 tree type = TREE_TYPE (min);
195 signop sign = TYPE_SIGN (type);
196 int_range<2> type_range (type);
197 // Calculate INVERSE([I,J]) as [-MIN, I-1][J+1, +MAX].
198 m_num_ranges = 0;
199 wi::overflow_type ovf;
200
201 wide_int w_min = wi::to_wide (min);
202 if (wi::ne_p (w_min, type_range.lower_bound ()))
203 {
204 wide_int lim1 = wi::sub (w_min, 1, sign, &ovf);
205 gcc_checking_assert (ovf != wi::OVF_OVERFLOW);
206 m_base[0] = type_range.tree_lower_bound (0);
207 m_base[1] = wide_int_to_tree (type, lim1);
208 m_num_ranges = 1;
209 }
210 wide_int w_max = wi::to_wide (max);
211 if (wi::ne_p (w_max, type_range.upper_bound ()))
212 {
213 wide_int lim2 = wi::add (w_max, 1, sign, &ovf);
214 gcc_checking_assert (ovf != wi::OVF_OVERFLOW);
215 m_base[m_num_ranges * 2] = wide_int_to_tree (type, lim2);
216 m_base[m_num_ranges * 2 + 1] = type_range.tree_upper_bound (0);
217 ++m_num_ranges;
218 }
219 if (flag_checking)
220 verify_range ();
221 }
222
223 /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
224 This means adjusting VRTYPE, MIN and MAX representing the case of a
225 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
226 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
227 In corner cases where MAX+1 or MIN-1 wraps this will fall back
228 to varying.
229 This routine exists to ease canonicalization in the case where we
230 extract ranges from var + CST op limit. */
231
232 void
233 irange::set (tree min, tree max, value_range_kind kind)
234 {
235 if (!legacy_mode_p ())
236 {
237 if (kind == VR_RANGE)
238 irange_set (min, max);
239 else
240 {
241 gcc_checking_assert (kind == VR_ANTI_RANGE);
242 irange_set_anti_range (min, max);
243 }
244 return;
245 }
246 if (kind == VR_UNDEFINED)
247 {
248 set_undefined ();
249 return;
250 }
251
252 if (kind == VR_VARYING
253 || POLY_INT_CST_P (min)
254 || POLY_INT_CST_P (max))
255 {
256 set_varying (TREE_TYPE (min));
257 return;
258 }
259
260 // Nothing to canonicalize for symbolic ranges.
261 if (TREE_CODE (min) != INTEGER_CST
262 || TREE_CODE (max) != INTEGER_CST)
263 {
264 m_kind = kind;
265 m_base[0] = min;
266 m_base[1] = max;
267 m_num_ranges = 1;
268 return;
269 }
270
271 swap_out_of_order_endpoints (min, max, kind);
272 if (kind == VR_VARYING)
273 {
274 set_varying (TREE_TYPE (min));
275 return;
276 }
277
278 // Anti-ranges that can be represented as ranges should be so.
279 if (kind == VR_ANTI_RANGE)
280 {
281 /* For -fstrict-enums we may receive out-of-range ranges so consider
282 values < -INF and values > INF as -INF/INF as well. */
283 bool is_min = vrp_val_is_min (min);
284 bool is_max = vrp_val_is_max (max);
285 tree type = TREE_TYPE (min);
286
287 if (is_min && is_max)
288 {
289 /* We cannot deal with empty ranges, drop to varying.
290 ??? This could be VR_UNDEFINED instead. */
291 set_varying (type);
292 return;
293 }
294 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
295 && (is_min || is_max))
296 {
297 /* Non-empty boolean ranges can always be represented
298 as a singleton range. */
299 if (is_min)
300 min = max = vrp_val_max (TREE_TYPE (min));
301 else
302 min = max = vrp_val_min (TREE_TYPE (min));
303 kind = VR_RANGE;
304 }
305 else if (is_min)
306 {
307 tree one = build_int_cst (TREE_TYPE (max), 1);
308 min = int_const_binop (PLUS_EXPR, max, one);
309 max = vrp_val_max (TREE_TYPE (max));
310 kind = VR_RANGE;
311 }
312 else if (is_max)
313 {
314 tree one = build_int_cst (TREE_TYPE (min), 1);
315 max = int_const_binop (MINUS_EXPR, min, one);
316 min = vrp_val_min (TREE_TYPE (min));
317 kind = VR_RANGE;
318 }
319 }
320
321 m_kind = kind;
322 m_base[0] = min;
323 m_base[1] = max;
324 m_num_ranges = 1;
325 normalize_min_max ();
326 if (flag_checking)
327 verify_range ();
328 }
329
330 // Check the validity of the range.
331
332 void
333 irange::verify_range ()
334 {
335 if (!legacy_mode_p ())
336 {
337 gcc_checking_assert (m_kind == VR_RANGE);
338 for (unsigned i = 0; i < m_num_ranges; ++i)
339 {
340 tree lb = tree_lower_bound (i);
341 tree ub = tree_upper_bound (i);
342 int c = compare_values (lb, ub);
343 gcc_assert (c == 0 || c == -1);
344 }
345 return;
346 }
347
348 switch (m_kind)
349 {
350 case VR_UNDEFINED:
351 gcc_assert (m_num_ranges == 0);
352 break;
353
354 case VR_VARYING:
355 gcc_assert (m_num_ranges == 1);
356 break;
357
358 case VR_ANTI_RANGE:
359 case VR_RANGE:
360 {
361 gcc_assert (m_num_ranges == 1);
362 int cmp = compare_values (tree_lower_bound (0), tree_upper_bound (0));
363 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
364 return;
365 }
366
367 default:
368 gcc_unreachable ();
369 }
370 }
371
372 unsigned
373 irange::legacy_num_pairs () const
374 {
375 gcc_checking_assert (legacy_mode_p ());
376
377 if (undefined_p ())
378 return 0;
379 if (varying_p ())
380 return 1;
381 // Inlined symbolic_p for performance:
382 if (!is_gimple_min_invariant (min ()) || !is_gimple_min_invariant (max ()))
383 {
384 value_range numeric_range (*this);
385 numeric_range.normalize_symbolics ();
386 return numeric_range.num_pairs ();
387 }
388 if (m_kind == VR_ANTI_RANGE)
389 {
390 // ~[MIN, X] has one sub-range of [X+1, MAX], and
391 // ~[X, MAX] has one sub-range of [MIN, X-1].
392 if (vrp_val_is_min (min ()) || vrp_val_is_max (max ()))
393 return 1;
394 return 2;
395 }
396 gcc_checking_assert (m_num_ranges == 1);
397 return 1;
398 }
399
400 // Return the lower bound for a sub-range. PAIR is the sub-range in
401 // question.
402
403 wide_int
404 irange::legacy_lower_bound (unsigned pair) const
405 {
406 gcc_checking_assert (legacy_mode_p ());
407 if (symbolic_p ())
408 {
409 value_range numeric_range (*this);
410 numeric_range.normalize_symbolics ();
411 return numeric_range.legacy_lower_bound (pair);
412 }
413 gcc_checking_assert (!undefined_p ());
414 gcc_checking_assert (pair + 1 <= num_pairs ());
415 if (m_kind == VR_ANTI_RANGE)
416 {
417 tree typ = type (), t;
418 if (pair == 1 || vrp_val_is_min (min ()))
419 t = wide_int_to_tree (typ, wi::to_wide (max ()) + 1);
420 else
421 t = vrp_val_min (typ);
422 return wi::to_wide (t);
423 }
424 return wi::to_wide (tree_lower_bound (pair));
425 }
426
427 // Return the upper bound for a sub-range. PAIR is the sub-range in
428 // question.
429
430 wide_int
431 irange::legacy_upper_bound (unsigned pair) const
432 {
433 gcc_checking_assert (legacy_mode_p ());
434 if (symbolic_p ())
435 {
436 value_range numeric_range (*this);
437 numeric_range.normalize_symbolics ();
438 return numeric_range.legacy_upper_bound (pair);
439 }
440 gcc_checking_assert (!undefined_p ());
441 gcc_checking_assert (pair + 1 <= num_pairs ());
442 if (m_kind == VR_ANTI_RANGE)
443 {
444 tree typ = type (), t;
445 if (pair == 1 || vrp_val_is_min (min ()))
446 t = vrp_val_max (typ);
447 else
448 t = wide_int_to_tree (typ, wi::to_wide (min ()) - 1);
449 return wi::to_wide (t);
450 }
451 return wi::to_wide (tree_upper_bound (pair));
452 }
453
454 bool
455 irange::legacy_equal_p (const irange &other) const
456 {
457 gcc_checking_assert (legacy_mode_p () && other.legacy_mode_p ());
458
459 if (m_kind != other.m_kind)
460 return false;
461 if (m_kind == VR_UNDEFINED || m_kind == VR_VARYING)
462 return true;
463 return (vrp_operand_equal_p (tree_lower_bound (0),
464 other.tree_lower_bound (0))
465 && vrp_operand_equal_p (tree_upper_bound (0),
466 other.tree_upper_bound (0)));
467 }
468
469 bool
470 irange::equal_p (const irange &other) const
471 {
472 if (legacy_mode_p ())
473 {
474 if (other.legacy_mode_p ())
475 return legacy_equal_p (other);
476 value_range tmp (other);
477 return legacy_equal_p (tmp);
478 }
479 if (other.legacy_mode_p ())
480 {
481 value_range tmp2 (*this);
482 return tmp2.legacy_equal_p (other);
483 }
484
485 if (m_num_ranges != other.m_num_ranges)
486 return false;
487
488 for (unsigned i = 0; i < m_num_ranges; ++i)
489 {
490 tree lb = tree_lower_bound (i);
491 tree ub = tree_upper_bound (i);
492 tree lb_other = other.tree_lower_bound (i);
493 tree ub_other = other.tree_upper_bound (i);
494 if (!operand_equal_p (lb, lb_other, 0)
495 || !operand_equal_p (ub, ub_other, 0))
496 return false;
497 }
498 return true;
499 }
500
501 /* Return TRUE if this is a symbolic range. */
502
503 bool
504 irange::symbolic_p () const
505 {
506 return (!varying_p ()
507 && !undefined_p ()
508 && (!is_gimple_min_invariant (min ())
509 || !is_gimple_min_invariant (max ())));
510 }
511
512 /* NOTE: This is not the inverse of symbolic_p because the range
513 could also be varying or undefined. Ideally they should be inverse
514 of each other, with varying only applying to symbolics. Varying of
515 constants would be represented as [-MIN, +MAX]. */
516
517 bool
518 irange::constant_p () const
519 {
520 return (!varying_p ()
521 && !undefined_p ()
522 && TREE_CODE (min ()) == INTEGER_CST
523 && TREE_CODE (max ()) == INTEGER_CST);
524 }
525
526 /* If range is a singleton, place it in RESULT and return TRUE.
527 Note: A singleton can be any gimple invariant, not just constants.
528 So, [&x, &x] counts as a singleton. */
529
530 bool
531 irange::singleton_p (tree *result) const
532 {
533 if (!legacy_mode_p ())
534 {
535 if (num_pairs () == 1 && (wi::to_wide (tree_lower_bound ())
536 == wi::to_wide (tree_upper_bound ())))
537 {
538 if (result)
539 *result = tree_lower_bound ();
540 return true;
541 }
542 return false;
543 }
544 if (m_kind == VR_ANTI_RANGE)
545 {
546 if (nonzero_p ())
547 {
548 if (TYPE_PRECISION (type ()) == 1)
549 {
550 if (result)
551 *result = max ();
552 return true;
553 }
554 return false;
555 }
556 if (num_pairs () == 1)
557 {
558 value_range vr0, vr1;
559 ranges_from_anti_range ((const value_range *) this, &vr0, &vr1);
560 return vr0.singleton_p (result);
561 }
562 }
563 // Catches non-numeric extremes as well.
564 if (m_kind == VR_RANGE
565 && vrp_operand_equal_p (min (), max ())
566 && is_gimple_min_invariant (min ()))
567 {
568 if (result)
569 *result = min ();
570 return true;
571 }
572 return false;
573 }
574
575 /* Return 1 if VAL is inside value range.
576 0 if VAL is not inside value range.
577 -2 if we cannot tell either way.
578
579 Benchmark compile/20001226-1.c compilation time after changing this
580 function. */
581
582 int
583 irange::value_inside_range (tree val) const
584 {
585 if (varying_p ())
586 return 1;
587
588 if (undefined_p ())
589 return 0;
590
591 if (!legacy_mode_p () && TREE_CODE (val) == INTEGER_CST)
592 return contains_p (val);
593
594 int cmp1 = operand_less_p (val, min ());
595 if (cmp1 == -2)
596 return -2;
597 if (cmp1 == 1)
598 return m_kind != VR_RANGE;
599
600 int cmp2 = operand_less_p (max (), val);
601 if (cmp2 == -2)
602 return -2;
603
604 if (m_kind == VR_RANGE)
605 return !cmp2;
606 else
607 return !!cmp2;
608 }
609
610 /* Return TRUE if it is possible that range contains VAL. */
611
612 bool
613 irange::may_contain_p (tree val) const
614 {
615 return value_inside_range (val) != 0;
616 }
617
618 /* Return TRUE if range contains INTEGER_CST. */
619 /* Return 1 if VAL is inside value range.
620 0 if VAL is not inside value range.
621
622 Benchmark compile/20001226-1.c compilation time after changing this
623 function. */
624
625
626 bool
627 irange::contains_p (tree cst) const
628 {
629 if (undefined_p ())
630 return false;
631
632 if (legacy_mode_p ())
633 {
634 gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST);
635 if (symbolic_p ())
636 {
637 value_range numeric_range (*this);
638 numeric_range.normalize_symbolics ();
639 return numeric_range.contains_p (cst);
640 }
641 return value_inside_range (cst) == 1;
642 }
643
644 gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST);
645 signop sign = TYPE_SIGN (TREE_TYPE (cst));
646 wide_int v = wi::to_wide (cst);
647 for (unsigned r = 0; r < m_num_ranges; ++r)
648 {
649 if (wi::lt_p (v, lower_bound (r), sign))
650 return false;
651 if (wi::le_p (v, upper_bound (r), sign))
652 return true;
653 }
654
655 return false;
656 }
657
658
659 /* Normalize addresses into constants. */
660
661 void
662 irange::normalize_addresses ()
663 {
664 if (undefined_p ())
665 return;
666
667 if (!POINTER_TYPE_P (type ()) || range_has_numeric_bounds_p (this))
668 return;
669
670 if (!range_includes_zero_p (this))
671 {
672 gcc_checking_assert (TREE_CODE (min ()) == ADDR_EXPR
673 || TREE_CODE (max ()) == ADDR_EXPR);
674 set_nonzero (type ());
675 return;
676 }
677 set_varying (type ());
678 }
679
680 /* Normalize symbolics and addresses into constants. */
681
682 void
683 irange::normalize_symbolics ()
684 {
685 if (varying_p () || undefined_p ())
686 return;
687
688 tree ttype = type ();
689 bool min_symbolic = !is_gimple_min_invariant (min ());
690 bool max_symbolic = !is_gimple_min_invariant (max ());
691 if (!min_symbolic && !max_symbolic)
692 {
693 normalize_addresses ();
694 return;
695 }
696
697 // [SYM, SYM] -> VARYING
698 if (min_symbolic && max_symbolic)
699 {
700 set_varying (ttype);
701 return;
702 }
703 if (kind () == VR_RANGE)
704 {
705 // [SYM, NUM] -> [-MIN, NUM]
706 if (min_symbolic)
707 {
708 set (vrp_val_min (ttype), max ());
709 return;
710 }
711 // [NUM, SYM] -> [NUM, +MAX]
712 set (min (), vrp_val_max (ttype));
713 return;
714 }
715 gcc_checking_assert (kind () == VR_ANTI_RANGE);
716 // ~[SYM, NUM] -> [NUM + 1, +MAX]
717 if (min_symbolic)
718 {
719 if (!vrp_val_is_max (max ()))
720 {
721 tree n = wide_int_to_tree (ttype, wi::to_wide (max ()) + 1);
722 set (n, vrp_val_max (ttype));
723 return;
724 }
725 set_varying (ttype);
726 return;
727 }
728 // ~[NUM, SYM] -> [-MIN, NUM - 1]
729 if (!vrp_val_is_min (min ()))
730 {
731 tree n = wide_int_to_tree (ttype, wi::to_wide (min ()) - 1);
732 set (vrp_val_min (ttype), n);
733 return;
734 }
735 set_varying (ttype);
736 }
737
738 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
739 { VR1TYPE, VR0MIN, VR0MAX } and store the result
740 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
741 possible such range. The resulting range is not canonicalized. */
742
743 static void
744 intersect_ranges (enum value_range_kind *vr0type,
745 tree *vr0min, tree *vr0max,
746 enum value_range_kind vr1type,
747 tree vr1min, tree vr1max)
748 {
749 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
750 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
751
752 /* [] is vr0, () is vr1 in the following classification comments. */
753 if (mineq && maxeq)
754 {
755 /* [( )] */
756 if (*vr0type == vr1type)
757 /* Nothing to do for equal ranges. */
758 ;
759 else if ((*vr0type == VR_RANGE
760 && vr1type == VR_ANTI_RANGE)
761 || (*vr0type == VR_ANTI_RANGE
762 && vr1type == VR_RANGE))
763 {
764 /* For anti-range with range intersection the result is empty. */
765 *vr0type = VR_UNDEFINED;
766 *vr0min = NULL_TREE;
767 *vr0max = NULL_TREE;
768 }
769 else
770 gcc_unreachable ();
771 }
772 else if (operand_less_p (*vr0max, vr1min) == 1
773 || operand_less_p (vr1max, *vr0min) == 1)
774 {
775 /* [ ] ( ) or ( ) [ ]
776 If the ranges have an empty intersection, the result of the
777 intersect operation is the range for intersecting an
778 anti-range with a range or empty when intersecting two ranges. */
779 if (*vr0type == VR_RANGE
780 && vr1type == VR_ANTI_RANGE)
781 ;
782 else if (*vr0type == VR_ANTI_RANGE
783 && vr1type == VR_RANGE)
784 {
785 *vr0type = vr1type;
786 *vr0min = vr1min;
787 *vr0max = vr1max;
788 }
789 else if (*vr0type == VR_RANGE
790 && vr1type == VR_RANGE)
791 {
792 *vr0type = VR_UNDEFINED;
793 *vr0min = NULL_TREE;
794 *vr0max = NULL_TREE;
795 }
796 else if (*vr0type == VR_ANTI_RANGE
797 && vr1type == VR_ANTI_RANGE)
798 {
799 /* If the anti-ranges are adjacent to each other merge them. */
800 if (TREE_CODE (*vr0max) == INTEGER_CST
801 && TREE_CODE (vr1min) == INTEGER_CST
802 && operand_less_p (*vr0max, vr1min) == 1
803 && integer_onep (int_const_binop (MINUS_EXPR,
804 vr1min, *vr0max)))
805 *vr0max = vr1max;
806 else if (TREE_CODE (vr1max) == INTEGER_CST
807 && TREE_CODE (*vr0min) == INTEGER_CST
808 && operand_less_p (vr1max, *vr0min) == 1
809 && integer_onep (int_const_binop (MINUS_EXPR,
810 *vr0min, vr1max)))
811 *vr0min = vr1min;
812 /* Else arbitrarily take VR0. */
813 }
814 }
815 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
816 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
817 {
818 /* [ ( ) ] or [( ) ] or [ ( )] */
819 if (*vr0type == VR_RANGE
820 && vr1type == VR_RANGE)
821 {
822 /* If both are ranges the result is the inner one. */
823 *vr0type = vr1type;
824 *vr0min = vr1min;
825 *vr0max = vr1max;
826 }
827 else if (*vr0type == VR_RANGE
828 && vr1type == VR_ANTI_RANGE)
829 {
830 /* Choose the right gap if the left one is empty. */
831 if (mineq)
832 {
833 if (TREE_CODE (vr1max) != INTEGER_CST)
834 *vr0min = vr1max;
835 else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1
836 && !TYPE_UNSIGNED (TREE_TYPE (vr1max)))
837 *vr0min
838 = int_const_binop (MINUS_EXPR, vr1max,
839 build_int_cst (TREE_TYPE (vr1max), -1));
840 else
841 *vr0min
842 = int_const_binop (PLUS_EXPR, vr1max,
843 build_int_cst (TREE_TYPE (vr1max), 1));
844 }
845 /* Choose the left gap if the right one is empty. */
846 else if (maxeq)
847 {
848 if (TREE_CODE (vr1min) != INTEGER_CST)
849 *vr0max = vr1min;
850 else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1
851 && !TYPE_UNSIGNED (TREE_TYPE (vr1min)))
852 *vr0max
853 = int_const_binop (PLUS_EXPR, vr1min,
854 build_int_cst (TREE_TYPE (vr1min), -1));
855 else
856 *vr0max
857 = int_const_binop (MINUS_EXPR, vr1min,
858 build_int_cst (TREE_TYPE (vr1min), 1));
859 }
860 /* Choose the anti-range if the range is effectively varying. */
861 else if (vrp_val_is_min (*vr0min)
862 && vrp_val_is_max (*vr0max))
863 {
864 *vr0type = vr1type;
865 *vr0min = vr1min;
866 *vr0max = vr1max;
867 }
868 /* Else choose the range. */
869 }
870 else if (*vr0type == VR_ANTI_RANGE
871 && vr1type == VR_ANTI_RANGE)
872 /* If both are anti-ranges the result is the outer one. */
873 ;
874 else if (*vr0type == VR_ANTI_RANGE
875 && vr1type == VR_RANGE)
876 {
877 /* The intersection is empty. */
878 *vr0type = VR_UNDEFINED;
879 *vr0min = NULL_TREE;
880 *vr0max = NULL_TREE;
881 }
882 else
883 gcc_unreachable ();
884 }
885 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
886 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
887 {
888 /* ( [ ] ) or ([ ] ) or ( [ ]) */
889 if (*vr0type == VR_RANGE
890 && vr1type == VR_RANGE)
891 /* Choose the inner range. */
892 ;
893 else if (*vr0type == VR_ANTI_RANGE
894 && vr1type == VR_RANGE)
895 {
896 /* Choose the right gap if the left is empty. */
897 if (mineq)
898 {
899 *vr0type = VR_RANGE;
900 if (TREE_CODE (*vr0max) != INTEGER_CST)
901 *vr0min = *vr0max;
902 else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1
903 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max)))
904 *vr0min
905 = int_const_binop (MINUS_EXPR, *vr0max,
906 build_int_cst (TREE_TYPE (*vr0max), -1));
907 else
908 *vr0min
909 = int_const_binop (PLUS_EXPR, *vr0max,
910 build_int_cst (TREE_TYPE (*vr0max), 1));
911 *vr0max = vr1max;
912 }
913 /* Choose the left gap if the right is empty. */
914 else if (maxeq)
915 {
916 *vr0type = VR_RANGE;
917 if (TREE_CODE (*vr0min) != INTEGER_CST)
918 *vr0max = *vr0min;
919 else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1
920 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min)))
921 *vr0max
922 = int_const_binop (PLUS_EXPR, *vr0min,
923 build_int_cst (TREE_TYPE (*vr0min), -1));
924 else
925 *vr0max
926 = int_const_binop (MINUS_EXPR, *vr0min,
927 build_int_cst (TREE_TYPE (*vr0min), 1));
928 *vr0min = vr1min;
929 }
930 /* Choose the anti-range if the range is effectively varying. */
931 else if (vrp_val_is_min (vr1min)
932 && vrp_val_is_max (vr1max))
933 ;
934 /* Choose the anti-range if it is ~[0,0], that range is special
935 enough to special case when vr1's range is relatively wide.
936 At least for types bigger than int - this covers pointers
937 and arguments to functions like ctz. */
938 else if (*vr0min == *vr0max
939 && integer_zerop (*vr0min)
940 && ((TYPE_PRECISION (TREE_TYPE (*vr0min))
941 >= TYPE_PRECISION (integer_type_node))
942 || POINTER_TYPE_P (TREE_TYPE (*vr0min)))
943 && TREE_CODE (vr1max) == INTEGER_CST
944 && TREE_CODE (vr1min) == INTEGER_CST
945 && (wi::clz (wi::to_wide (vr1max) - wi::to_wide (vr1min))
946 < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2))
947 ;
948 /* Else choose the range. */
949 else
950 {
951 *vr0type = vr1type;
952 *vr0min = vr1min;
953 *vr0max = vr1max;
954 }
955 }
956 else if (*vr0type == VR_ANTI_RANGE
957 && vr1type == VR_ANTI_RANGE)
958 {
959 /* If both are anti-ranges the result is the outer one. */
960 *vr0type = vr1type;
961 *vr0min = vr1min;
962 *vr0max = vr1max;
963 }
964 else if (vr1type == VR_ANTI_RANGE
965 && *vr0type == VR_RANGE)
966 {
967 /* The intersection is empty. */
968 *vr0type = VR_UNDEFINED;
969 *vr0min = NULL_TREE;
970 *vr0max = NULL_TREE;
971 }
972 else
973 gcc_unreachable ();
974 }
975 else if ((operand_less_p (vr1min, *vr0max) == 1
976 || operand_equal_p (vr1min, *vr0max, 0))
977 && operand_less_p (*vr0min, vr1min) == 1)
978 {
979 /* [ ( ] ) or [ ]( ) */
980 if (*vr0type == VR_ANTI_RANGE
981 && vr1type == VR_ANTI_RANGE)
982 *vr0max = vr1max;
983 else if (*vr0type == VR_RANGE
984 && vr1type == VR_RANGE)
985 *vr0min = vr1min;
986 else if (*vr0type == VR_RANGE
987 && vr1type == VR_ANTI_RANGE)
988 {
989 if (TREE_CODE (vr1min) == INTEGER_CST)
990 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
991 build_int_cst (TREE_TYPE (vr1min), 1));
992 else
993 *vr0max = vr1min;
994 }
995 else if (*vr0type == VR_ANTI_RANGE
996 && vr1type == VR_RANGE)
997 {
998 *vr0type = VR_RANGE;
999 if (TREE_CODE (*vr0max) == INTEGER_CST)
1000 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
1001 build_int_cst (TREE_TYPE (*vr0max), 1));
1002 else
1003 *vr0min = *vr0max;
1004 *vr0max = vr1max;
1005 }
1006 else
1007 gcc_unreachable ();
1008 }
1009 else if ((operand_less_p (*vr0min, vr1max) == 1
1010 || operand_equal_p (*vr0min, vr1max, 0))
1011 && operand_less_p (vr1min, *vr0min) == 1)
1012 {
1013 /* ( [ ) ] or ( )[ ] */
1014 if (*vr0type == VR_ANTI_RANGE
1015 && vr1type == VR_ANTI_RANGE)
1016 *vr0min = vr1min;
1017 else if (*vr0type == VR_RANGE
1018 && vr1type == VR_RANGE)
1019 *vr0max = vr1max;
1020 else if (*vr0type == VR_RANGE
1021 && vr1type == VR_ANTI_RANGE)
1022 {
1023 if (TREE_CODE (vr1max) == INTEGER_CST)
1024 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
1025 build_int_cst (TREE_TYPE (vr1max), 1));
1026 else
1027 *vr0min = vr1max;
1028 }
1029 else if (*vr0type == VR_ANTI_RANGE
1030 && vr1type == VR_RANGE)
1031 {
1032 *vr0type = VR_RANGE;
1033 if (TREE_CODE (*vr0min) == INTEGER_CST)
1034 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
1035 build_int_cst (TREE_TYPE (*vr0min), 1));
1036 else
1037 *vr0max = *vr0min;
1038 *vr0min = vr1min;
1039 }
1040 else
1041 gcc_unreachable ();
1042 }
1043
1044 /* If we know the intersection is empty, there's no need to
1045 conservatively add anything else to the set. */
1046 if (*vr0type == VR_UNDEFINED)
1047 return;
1048
1049 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
1050 result for the intersection. That's always a conservative
1051 correct estimate unless VR1 is a constant singleton range
1052 in which case we choose that. */
1053 if (vr1type == VR_RANGE
1054 && is_gimple_min_invariant (vr1min)
1055 && vrp_operand_equal_p (vr1min, vr1max))
1056 {
1057 *vr0type = vr1type;
1058 *vr0min = vr1min;
1059 *vr0max = vr1max;
1060 }
1061 }
1062
1063 /* Helper for the intersection operation for value ranges. Given two
1064 ranges VR0 and VR1, set VR0 to the intersection of both ranges.
1065 This may not be the smallest possible such range. */
1066
1067 void
1068 irange::legacy_intersect (irange *vr0, const irange *vr1)
1069 {
1070 gcc_checking_assert (vr0->legacy_mode_p ());
1071 gcc_checking_assert (vr1->legacy_mode_p ());
1072 /* If either range is VR_VARYING the other one wins. */
1073 if (vr1->varying_p ())
1074 return;
1075 if (vr0->varying_p ())
1076 {
1077 vr0->set (vr1->min (), vr1->max (), vr1->kind ());
1078 return;
1079 }
1080
1081 /* When either range is VR_UNDEFINED the resulting range is
1082 VR_UNDEFINED, too. */
1083 if (vr0->undefined_p ())
1084 return;
1085 if (vr1->undefined_p ())
1086 {
1087 vr0->set_undefined ();
1088 return;
1089 }
1090
1091 value_range_kind vr0kind = vr0->kind ();
1092 tree vr0min = vr0->min ();
1093 tree vr0max = vr0->max ();
1094
1095 intersect_ranges (&vr0kind, &vr0min, &vr0max,
1096 vr1->kind (), vr1->min (), vr1->max ());
1097
1098 /* Make sure to canonicalize the result though as the inversion of a
1099 VR_RANGE can still be a VR_RANGE. */
1100 if (vr0kind == VR_UNDEFINED)
1101 vr0->set_undefined ();
1102 else if (vr0kind == VR_VARYING)
1103 {
1104 /* If we failed, use the original VR0. */
1105 return;
1106 }
1107 else
1108 vr0->set (vr0min, vr0max, vr0kind);
1109 }
1110
1111 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
1112 { VR1TYPE, VR0MIN, VR0MAX } and store the result
1113 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
1114 possible such range. The resulting range is not canonicalized. */
1115
1116 static void
1117 union_ranges (enum value_range_kind *vr0type,
1118 tree *vr0min, tree *vr0max,
1119 enum value_range_kind vr1type,
1120 tree vr1min, tree vr1max)
1121 {
1122 int cmpmin = compare_values (*vr0min, vr1min);
1123 int cmpmax = compare_values (*vr0max, vr1max);
1124 bool mineq = cmpmin == 0;
1125 bool maxeq = cmpmax == 0;
1126
1127 /* [] is vr0, () is vr1 in the following classification comments. */
1128 if (mineq && maxeq)
1129 {
1130 /* [( )] */
1131 if (*vr0type == vr1type)
1132 /* Nothing to do for equal ranges. */
1133 ;
1134 else if ((*vr0type == VR_RANGE
1135 && vr1type == VR_ANTI_RANGE)
1136 || (*vr0type == VR_ANTI_RANGE
1137 && vr1type == VR_RANGE))
1138 {
1139 /* For anti-range with range union the result is varying. */
1140 goto give_up;
1141 }
1142 else
1143 gcc_unreachable ();
1144 }
1145 else if (operand_less_p (*vr0max, vr1min) == 1
1146 || operand_less_p (vr1max, *vr0min) == 1)
1147 {
1148 /* [ ] ( ) or ( ) [ ]
1149 If the ranges have an empty intersection, result of the union
1150 operation is the anti-range or if both are anti-ranges
1151 it covers all. */
1152 if (*vr0type == VR_ANTI_RANGE
1153 && vr1type == VR_ANTI_RANGE)
1154 goto give_up;
1155 else if (*vr0type == VR_ANTI_RANGE
1156 && vr1type == VR_RANGE)
1157 ;
1158 else if (*vr0type == VR_RANGE
1159 && vr1type == VR_ANTI_RANGE)
1160 {
1161 *vr0type = vr1type;
1162 *vr0min = vr1min;
1163 *vr0max = vr1max;
1164 }
1165 else if (*vr0type == VR_RANGE
1166 && vr1type == VR_RANGE)
1167 {
1168 /* The result is the convex hull of both ranges. */
1169 if (operand_less_p (*vr0max, vr1min) == 1)
1170 {
1171 /* If the result can be an anti-range, create one. */
1172 if (TREE_CODE (*vr0max) == INTEGER_CST
1173 && TREE_CODE (vr1min) == INTEGER_CST
1174 && vrp_val_is_min (*vr0min)
1175 && vrp_val_is_max (vr1max))
1176 {
1177 tree min = int_const_binop (PLUS_EXPR,
1178 *vr0max,
1179 build_int_cst (TREE_TYPE (*vr0max), 1));
1180 tree max = int_const_binop (MINUS_EXPR,
1181 vr1min,
1182 build_int_cst (TREE_TYPE (vr1min), 1));
1183 if (!operand_less_p (max, min))
1184 {
1185 *vr0type = VR_ANTI_RANGE;
1186 *vr0min = min;
1187 *vr0max = max;
1188 }
1189 else
1190 *vr0max = vr1max;
1191 }
1192 else
1193 *vr0max = vr1max;
1194 }
1195 else
1196 {
1197 /* If the result can be an anti-range, create one. */
1198 if (TREE_CODE (vr1max) == INTEGER_CST
1199 && TREE_CODE (*vr0min) == INTEGER_CST
1200 && vrp_val_is_min (vr1min)
1201 && vrp_val_is_max (*vr0max))
1202 {
1203 tree min = int_const_binop (PLUS_EXPR,
1204 vr1max,
1205 build_int_cst (TREE_TYPE (vr1max), 1));
1206 tree max = int_const_binop (MINUS_EXPR,
1207 *vr0min,
1208 build_int_cst (TREE_TYPE (*vr0min), 1));
1209 if (!operand_less_p (max, min))
1210 {
1211 *vr0type = VR_ANTI_RANGE;
1212 *vr0min = min;
1213 *vr0max = max;
1214 }
1215 else
1216 *vr0min = vr1min;
1217 }
1218 else
1219 *vr0min = vr1min;
1220 }
1221 }
1222 else
1223 gcc_unreachable ();
1224 }
1225 else if ((maxeq || cmpmax == 1)
1226 && (mineq || cmpmin == -1))
1227 {
1228 /* [ ( ) ] or [( ) ] or [ ( )] */
1229 if (*vr0type == VR_RANGE
1230 && vr1type == VR_RANGE)
1231 ;
1232 else if (*vr0type == VR_ANTI_RANGE
1233 && vr1type == VR_ANTI_RANGE)
1234 {
1235 *vr0type = vr1type;
1236 *vr0min = vr1min;
1237 *vr0max = vr1max;
1238 }
1239 else if (*vr0type == VR_ANTI_RANGE
1240 && vr1type == VR_RANGE)
1241 {
1242 /* Arbitrarily choose the right or left gap. */
1243 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
1244 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
1245 build_int_cst (TREE_TYPE (vr1min), 1));
1246 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
1247 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
1248 build_int_cst (TREE_TYPE (vr1max), 1));
1249 else
1250 goto give_up;
1251 }
1252 else if (*vr0type == VR_RANGE
1253 && vr1type == VR_ANTI_RANGE)
1254 /* The result covers everything. */
1255 goto give_up;
1256 else
1257 gcc_unreachable ();
1258 }
1259 else if ((maxeq || cmpmax == -1)
1260 && (mineq || cmpmin == 1))
1261 {
1262 /* ( [ ] ) or ([ ] ) or ( [ ]) */
1263 if (*vr0type == VR_RANGE
1264 && vr1type == VR_RANGE)
1265 {
1266 *vr0type = vr1type;
1267 *vr0min = vr1min;
1268 *vr0max = vr1max;
1269 }
1270 else if (*vr0type == VR_ANTI_RANGE
1271 && vr1type == VR_ANTI_RANGE)
1272 ;
1273 else if (*vr0type == VR_RANGE
1274 && vr1type == VR_ANTI_RANGE)
1275 {
1276 *vr0type = VR_ANTI_RANGE;
1277 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
1278 {
1279 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
1280 build_int_cst (TREE_TYPE (*vr0min), 1));
1281 *vr0min = vr1min;
1282 }
1283 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
1284 {
1285 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
1286 build_int_cst (TREE_TYPE (*vr0max), 1));
1287 *vr0max = vr1max;
1288 }
1289 else
1290 goto give_up;
1291 }
1292 else if (*vr0type == VR_ANTI_RANGE
1293 && vr1type == VR_RANGE)
1294 /* The result covers everything. */
1295 goto give_up;
1296 else
1297 gcc_unreachable ();
1298 }
1299 else if (cmpmin == -1
1300 && cmpmax == -1
1301 && (operand_less_p (vr1min, *vr0max) == 1
1302 || operand_equal_p (vr1min, *vr0max, 0)))
1303 {
1304 /* [ ( ] ) or [ ]( ) */
1305 if (*vr0type == VR_RANGE
1306 && vr1type == VR_RANGE)
1307 *vr0max = vr1max;
1308 else if (*vr0type == VR_ANTI_RANGE
1309 && vr1type == VR_ANTI_RANGE)
1310 *vr0min = vr1min;
1311 else if (*vr0type == VR_ANTI_RANGE
1312 && vr1type == VR_RANGE)
1313 {
1314 if (TREE_CODE (vr1min) == INTEGER_CST)
1315 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
1316 build_int_cst (TREE_TYPE (vr1min), 1));
1317 else
1318 goto give_up;
1319 }
1320 else if (*vr0type == VR_RANGE
1321 && vr1type == VR_ANTI_RANGE)
1322 {
1323 if (TREE_CODE (*vr0max) == INTEGER_CST)
1324 {
1325 *vr0type = vr1type;
1326 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
1327 build_int_cst (TREE_TYPE (*vr0max), 1));
1328 *vr0max = vr1max;
1329 }
1330 else
1331 goto give_up;
1332 }
1333 else
1334 gcc_unreachable ();
1335 }
1336 else if (cmpmin == 1
1337 && cmpmax == 1
1338 && (operand_less_p (*vr0min, vr1max) == 1
1339 || operand_equal_p (*vr0min, vr1max, 0)))
1340 {
1341 /* ( [ ) ] or ( )[ ] */
1342 if (*vr0type == VR_RANGE
1343 && vr1type == VR_RANGE)
1344 *vr0min = vr1min;
1345 else if (*vr0type == VR_ANTI_RANGE
1346 && vr1type == VR_ANTI_RANGE)
1347 *vr0max = vr1max;
1348 else if (*vr0type == VR_ANTI_RANGE
1349 && vr1type == VR_RANGE)
1350 {
1351 if (TREE_CODE (vr1max) == INTEGER_CST)
1352 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
1353 build_int_cst (TREE_TYPE (vr1max), 1));
1354 else
1355 goto give_up;
1356 }
1357 else if (*vr0type == VR_RANGE
1358 && vr1type == VR_ANTI_RANGE)
1359 {
1360 if (TREE_CODE (*vr0min) == INTEGER_CST)
1361 {
1362 *vr0type = vr1type;
1363 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
1364 build_int_cst (TREE_TYPE (*vr0min), 1));
1365 *vr0min = vr1min;
1366 }
1367 else
1368 goto give_up;
1369 }
1370 else
1371 gcc_unreachable ();
1372 }
1373 else
1374 goto give_up;
1375
1376 return;
1377
1378 give_up:
1379 *vr0type = VR_VARYING;
1380 *vr0min = NULL_TREE;
1381 *vr0max = NULL_TREE;
1382 }
1383
1384 /* Helper for meet operation for value ranges. Given two ranges VR0
1385 and VR1, set VR0 to the union of both ranges. This may not be the
1386 smallest possible such range. */
1387
1388 void
1389 irange::legacy_union (irange *vr0, const irange *vr1)
1390 {
1391 gcc_checking_assert (vr0->legacy_mode_p ());
1392 gcc_checking_assert (vr1->legacy_mode_p ());
1393
1394 /* VR0 has the resulting range if VR1 is undefined or VR0 is varying. */
1395 if (vr1->undefined_p ()
1396 || vr0->varying_p ())
1397 return;
1398
1399 /* VR1 has the resulting range if VR0 is undefined or VR1 is varying. */
1400 if (vr0->undefined_p ())
1401 {
1402 vr0->set (vr1->min (), vr1->max (), vr1->kind ());
1403 return;
1404 }
1405
1406 if (vr1->varying_p ())
1407 {
1408 vr0->set_varying (vr1->type ());
1409 return;
1410 }
1411
1412 value_range_kind vr0kind = vr0->kind ();
1413 tree vr0min = vr0->min ();
1414 tree vr0max = vr0->max ();
1415
1416 union_ranges (&vr0kind, &vr0min, &vr0max,
1417 vr1->kind (), vr1->min (), vr1->max ());
1418
1419 if (vr0kind == VR_UNDEFINED)
1420 vr0->set_undefined ();
1421 else if (vr0kind == VR_VARYING)
1422 {
1423 /* Failed to find an efficient meet. Before giving up and
1424 setting the result to VARYING, see if we can at least derive
1425 a non-zero range. */
1426 if (range_includes_zero_p (vr0) == 0
1427 && range_includes_zero_p (vr1) == 0)
1428 vr0->set_nonzero (vr0->type ());
1429 else
1430 vr0->set_varying (vr0->type ());
1431 }
1432 else
1433 vr0->set (vr0min, vr0max, vr0kind);
1434 }
1435
1436 /* Meet operation for value ranges. Given two value ranges VR0 and
1437 VR1, store in VR0 a range that contains both VR0 and VR1. This
1438 may not be the smallest possible such range. */
1439
1440 void
1441 irange::union_ (const irange *other)
1442 {
1443 if (legacy_mode_p ())
1444 {
1445 if (!other->legacy_mode_p ())
1446 {
1447 int_range<1> tmp = *other;
1448 legacy_union (this, &tmp);
1449 return;
1450 }
1451 if (dump_file && (dump_flags & TDF_DETAILS))
1452 {
1453 fprintf (dump_file, "Meeting\n ");
1454 dump_value_range (dump_file, this);
1455 fprintf (dump_file, "\nand\n ");
1456 dump_value_range (dump_file, other);
1457 fprintf (dump_file, "\n");
1458 }
1459
1460 legacy_union (this, other);
1461
1462 if (dump_file && (dump_flags & TDF_DETAILS))
1463 {
1464 fprintf (dump_file, "to\n ");
1465 dump_value_range (dump_file, this);
1466 fprintf (dump_file, "\n");
1467 }
1468 return;
1469 }
1470
1471 if (other->legacy_mode_p ())
1472 {
1473 int_range<2> wider = *other;
1474 irange_union (wider);
1475 }
1476 else
1477 irange_union (*other);
1478 }
1479
1480 void
1481 irange::intersect (const irange *other)
1482 {
1483 if (legacy_mode_p ())
1484 {
1485 if (!other->legacy_mode_p ())
1486 {
1487 int_range<1> tmp = *other;
1488 legacy_intersect (this, &tmp);
1489 return;
1490 }
1491 if (dump_file && (dump_flags & TDF_DETAILS))
1492 {
1493 fprintf (dump_file, "Intersecting\n ");
1494 dump_value_range (dump_file, this);
1495 fprintf (dump_file, "\nand\n ");
1496 dump_value_range (dump_file, other);
1497 fprintf (dump_file, "\n");
1498 }
1499
1500 legacy_intersect (this, other);
1501
1502 if (dump_file && (dump_flags & TDF_DETAILS))
1503 {
1504 fprintf (dump_file, "to\n ");
1505 dump_value_range (dump_file, this);
1506 fprintf (dump_file, "\n");
1507 }
1508 return;
1509 }
1510
1511 if (other->legacy_mode_p ())
1512 {
1513 int_range<2> wider;
1514 wider = *other;
1515 irange_intersect (wider);
1516 }
1517 else
1518 irange_intersect (*other);
1519 }
1520
1521 // union_ for multi-ranges.
1522
1523 void
1524 irange::irange_union (const irange &r)
1525 {
1526 gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ());
1527
1528 if (r.undefined_p () || varying_p ())
1529 return;
1530
1531 if (undefined_p () || r.varying_p ())
1532 {
1533 operator= (r);
1534 return;
1535 }
1536
1537 // Do not worry about merging and such by reserving twice as many
1538 // pairs as needed, and then simply sort the 2 ranges into this
1539 // intermediate form.
1540 //
1541 // The intermediate result will have the property that the beginning
1542 // of each range is <= the beginning of the next range. There may
1543 // be overlapping ranges at this point. I.e. this would be valid
1544 // [-20, 10], [-10, 0], [0, 20], [40, 90] as it satisfies this
1545 // contraint : -20 < -10 < 0 < 40. When the range is rebuilt into r,
1546 // the merge is performed.
1547 //
1548 // [Xi,Yi]..[Xn,Yn] U [Xj,Yj]..[Xm,Ym] --> [Xk,Yk]..[Xp,Yp]
1549 tree ttype = r.type ();
1550 signop sign = TYPE_SIGN (ttype);
1551
1552 auto_vec<tree, 20> res;
1553 wide_int u1 ;
1554 wi::overflow_type ovf;
1555 unsigned i = 0, j = 0, k = 0;
1556
1557 while (i < m_num_ranges * 2 && j < r.m_num_ranges * 2)
1558 {
1559 // lower of Xi and Xj is the lowest point.
1560 if (wi::le_p (wi::to_wide (m_base[i]), wi::to_wide (r.m_base[j]), sign))
1561 {
1562 res.safe_push (m_base[i]);
1563 res.safe_push (m_base[i + 1]);
1564 k += 2;
1565 i += 2;
1566 }
1567 else
1568 {
1569 res.safe_push (r.m_base[j]);
1570 res.safe_push (r.m_base[j + 1]);
1571 k += 2;
1572 j += 2;
1573 }
1574 }
1575 for ( ; i < m_num_ranges * 2; i += 2)
1576 {
1577 res.safe_push (m_base[i]);
1578 res.safe_push (m_base[i + 1]);
1579 k += 2;
1580 }
1581 for ( ; j < r.m_num_ranges * 2; j += 2)
1582 {
1583 res.safe_push (r.m_base[j]);
1584 res.safe_push (r.m_base[j + 1]);
1585 k += 2;
1586 }
1587
1588 // Now normalize the vector removing any overlaps.
1589 i = 2;
1590 int prec = TYPE_PRECISION (ttype);
1591 wide_int max_val = wi::max_value (prec, sign);
1592 for (j = 2; j < k ; j += 2)
1593 {
1594 wide_int val_im1 = wi::to_wide (res[i - 1]);
1595 if (val_im1 == max_val)
1596 break;
1597 u1 = wi::add (val_im1, 1, sign, &ovf);
1598
1599 // Overflow indicates we are at MAX already.
1600 // A wide int bug requires the previous max_val check
1601 // trigger: gcc.c-torture/compile/pr80443.c with -O3
1602 if (ovf == wi::OVF_OVERFLOW)
1603 break;
1604
1605 wide_int val_j = wi::to_wide (res[j]);
1606 wide_int val_jp1 = wi::to_wide (res[j+1]);
1607 // Current upper+1 is >= lower bound next pair, then we merge ranges.
1608 if (wi::ge_p (u1, val_j, sign))
1609 {
1610 // New upper bounds is greater of current or the next one.
1611 if (wi::gt_p (val_jp1, val_im1, sign))
1612 res [i - 1] = res[j + 1];
1613 }
1614 else
1615 {
1616 // This is a new distinct range, but no point in copying it
1617 // if it is already in the right place.
1618 if (i != j)
1619 {
1620 res[i++] = res[j];
1621 res[i++] = res[j + 1];
1622 }
1623 else
1624 i += 2;
1625 }
1626 }
1627
1628 // At this point, the vector should have i ranges, none overlapping.
1629 // Now it simply needs to be copied, and if there are too many
1630 // ranges, merge some. We wont do any analysis as to what the
1631 // "best" merges are, simply combine the final ranges into one.
1632 if (i > m_max_ranges * 2)
1633 {
1634 res[m_max_ranges * 2 - 1] = res[i - 1];
1635 i = m_max_ranges * 2;
1636 }
1637
1638 for (j = 0; j < i ; j++)
1639 m_base[j] = res [j];
1640 m_num_ranges = i / 2;
1641
1642 if (flag_checking)
1643 verify_range ();
1644 }
1645
1646 // intersect for multi-ranges.
1647
1648 void
1649 irange::irange_intersect (const irange &r)
1650 {
1651 gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ());
1652
1653 if (undefined_p () || r.varying_p ())
1654 return;
1655 if (r.undefined_p ())
1656 {
1657 set_undefined ();
1658 return;
1659 }
1660 if (varying_p ())
1661 {
1662 operator= (r);
1663 return;
1664 }
1665
1666 signop sign = TYPE_SIGN (TREE_TYPE(m_base[0]));
1667 unsigned bld_pair = 0;
1668 unsigned bld_lim = m_max_ranges;
1669 int_range_max r2 (*this);
1670 unsigned r2_lim = r2.num_pairs ();
1671 unsigned i2 = 0;
1672 for (unsigned i = 0; i < r.num_pairs (); )
1673 {
1674 // If r1's upper is < r2's lower, we can skip r1's pair.
1675 tree ru = r.m_base[i * 2 + 1];
1676 tree r2l = r2.m_base[i2 * 2];
1677 if (wi::lt_p (wi::to_wide (ru), wi::to_wide (r2l), sign))
1678 {
1679 i++;
1680 continue;
1681 }
1682 // Likewise, skip r2's pair if its excluded.
1683 tree r2u = r2.m_base[i2 * 2 + 1];
1684 tree rl = r.m_base[i * 2];
1685 if (wi::lt_p (wi::to_wide (r2u), wi::to_wide (rl), sign))
1686 {
1687 i2++;
1688 if (i2 < r2_lim)
1689 continue;
1690 // No more r2, break.
1691 break;
1692 }
1693
1694 // Must be some overlap. Find the highest of the lower bounds,
1695 // and set it, unless the build limits lower bounds is already
1696 // set.
1697 if (bld_pair < bld_lim)
1698 {
1699 if (wi::ge_p (wi::to_wide (rl), wi::to_wide (r2l), sign))
1700 m_base[bld_pair * 2] = rl;
1701 else
1702 m_base[bld_pair * 2] = r2l;
1703 }
1704 else
1705 // Decrease and set a new upper.
1706 bld_pair--;
1707
1708 // ...and choose the lower of the upper bounds.
1709 if (wi::le_p (wi::to_wide (ru), wi::to_wide (r2u), sign))
1710 {
1711 m_base[bld_pair * 2 + 1] = ru;
1712 bld_pair++;
1713 // Move past the r1 pair and keep trying.
1714 i++;
1715 continue;
1716 }
1717 else
1718 {
1719 m_base[bld_pair * 2 + 1] = r2u;
1720 bld_pair++;
1721 i2++;
1722 if (i2 < r2_lim)
1723 continue;
1724 // No more r2, break.
1725 break;
1726 }
1727 // r2 has the higher lower bound.
1728 }
1729
1730 // At the exit of this loop, it is one of 2 things:
1731 // ran out of r1, or r2, but either means we are done.
1732 m_num_ranges = bld_pair;
1733 if (flag_checking)
1734 verify_range ();
1735 }
1736
1737 // Signed 1-bits are strange. You can't subtract 1, because you can't
1738 // represent the number 1. This works around that for the invert routine.
1739
1740 static wide_int inline
1741 subtract_one (const wide_int &x, tree type, wi::overflow_type &overflow)
1742 {
1743 if (TYPE_SIGN (type) == SIGNED)
1744 return wi::add (x, -1, SIGNED, &overflow);
1745 else
1746 return wi::sub (x, 1, UNSIGNED, &overflow);
1747 }
1748
1749 // The analogous function for adding 1.
1750
1751 static wide_int inline
1752 add_one (const wide_int &x, tree type, wi::overflow_type &overflow)
1753 {
1754 if (TYPE_SIGN (type) == SIGNED)
1755 return wi::sub (x, -1, SIGNED, &overflow);
1756 else
1757 return wi::add (x, 1, UNSIGNED, &overflow);
1758 }
1759
1760 // Return the inverse of a range.
1761
1762 void
1763 irange::invert ()
1764 {
1765 if (legacy_mode_p ())
1766 {
1767 // We can't just invert VR_RANGE and VR_ANTI_RANGE because we may
1768 // create non-canonical ranges. Use the constructors instead.
1769 if (m_kind == VR_RANGE)
1770 *this = value_range (min (), max (), VR_ANTI_RANGE);
1771 else if (m_kind == VR_ANTI_RANGE)
1772 *this = value_range (min (), max ());
1773 else
1774 gcc_unreachable ();
1775 return;
1776 }
1777
1778 gcc_assert (!undefined_p () && !varying_p ());
1779
1780 // We always need one more set of bounds to represent an inverse, so
1781 // if we're at the limit, we can't properly represent things.
1782 //
1783 // For instance, to represent the inverse of a 2 sub-range set
1784 // [5, 10][20, 30], we would need a 3 sub-range set
1785 // [-MIN, 4][11, 19][31, MAX].
1786 //
1787 // In this case, return the most conservative thing.
1788 //
1789 // However, if any of the extremes of the range are -MIN/+MAX, we
1790 // know we will not need an extra bound. For example:
1791 //
1792 // INVERT([-MIN,20][30,40]) => [21,29][41,+MAX]
1793 // INVERT([-MIN,20][30,MAX]) => [21,29]
1794 tree ttype = type ();
1795 unsigned prec = TYPE_PRECISION (ttype);
1796 signop sign = TYPE_SIGN (ttype);
1797 wide_int type_min = wi::min_value (prec, sign);
1798 wide_int type_max = wi::max_value (prec, sign);
1799 if (m_num_ranges == m_max_ranges
1800 && lower_bound () != type_min
1801 && upper_bound () != type_max)
1802 {
1803 m_base[1] = wide_int_to_tree (ttype, type_max);
1804 m_num_ranges = 1;
1805 return;
1806 }
1807 // The algorithm is as follows. To calculate INVERT ([a,b][c,d]), we
1808 // generate [-MIN, a-1][b+1, c-1][d+1, MAX].
1809 //
1810 // If there is an over/underflow in the calculation for any
1811 // sub-range, we eliminate that subrange. This allows us to easily
1812 // calculate INVERT([-MIN, 5]) with: [-MIN, -MIN-1][6, MAX]. And since
1813 // we eliminate the underflow, only [6, MAX] remains.
1814 unsigned i = 0;
1815 wi::overflow_type ovf;
1816 // Construct leftmost range.
1817 int_range_max orig_range (*this);
1818 unsigned nitems = 0;
1819 wide_int tmp;
1820 // If this is going to underflow on the MINUS 1, don't even bother
1821 // checking. This also handles subtracting one from an unsigned 0,
1822 // which doesn't set the underflow bit.
1823 if (type_min != orig_range.lower_bound ())
1824 {
1825 m_base[nitems++] = wide_int_to_tree (ttype, type_min);
1826 tmp = subtract_one (orig_range.lower_bound (), ttype, ovf);
1827 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
1828 if (ovf)
1829 nitems = 0;
1830 }
1831 i++;
1832 // Construct middle ranges if applicable.
1833 if (orig_range.num_pairs () > 1)
1834 {
1835 unsigned j = i;
1836 for (; j < (orig_range.num_pairs () * 2) - 1; j += 2)
1837 {
1838 // The middle ranges cannot have MAX/MIN, so there's no need
1839 // to check for unsigned overflow on the +1 and -1 here.
1840 tmp = wi::add (wi::to_wide (orig_range.m_base[j]), 1, sign, &ovf);
1841 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
1842 tmp = subtract_one (wi::to_wide (orig_range.m_base[j + 1]),
1843 ttype, ovf);
1844 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
1845 if (ovf)
1846 nitems -= 2;
1847 }
1848 i = j;
1849 }
1850 // Construct rightmost range.
1851 //
1852 // However, if this will overflow on the PLUS 1, don't even bother.
1853 // This also handles adding one to an unsigned MAX, which doesn't
1854 // set the overflow bit.
1855 if (type_max != wi::to_wide (orig_range.m_base[i]))
1856 {
1857 tmp = add_one (wi::to_wide (orig_range.m_base[i]), ttype, ovf);
1858 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
1859 m_base[nitems++] = wide_int_to_tree (ttype, type_max);
1860 if (ovf)
1861 nitems -= 2;
1862 }
1863 m_num_ranges = nitems / 2;
1864
1865 if (flag_checking)
1866 verify_range ();
1867 }
1868
1869 static void
1870 dump_bound_with_infinite_markers (FILE *file, tree bound)
1871 {
1872 tree type = TREE_TYPE (bound);
1873 wide_int type_min = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1874 wide_int type_max = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1875
1876 if (INTEGRAL_TYPE_P (type)
1877 && !TYPE_UNSIGNED (type)
1878 && TREE_CODE (bound) == INTEGER_CST
1879 && wi::to_wide (bound) == type_min
1880 && TYPE_PRECISION (type) != 1)
1881 fprintf (file, "-INF");
1882 else if (TREE_CODE (bound) == INTEGER_CST
1883 && wi::to_wide (bound) == type_max
1884 && TYPE_PRECISION (type) != 1)
1885 fprintf (file, "+INF");
1886 else
1887 print_generic_expr (file, bound);
1888 }
1889
1890 void
1891 irange::dump (FILE *file) const
1892 {
1893 if (undefined_p ())
1894 {
1895 fprintf (file, "UNDEFINED");
1896 return;
1897 }
1898 print_generic_expr (file, type ());
1899 fprintf (file, " ");
1900 if (varying_p ())
1901 {
1902 fprintf (file, "VARYING");
1903 return;
1904 }
1905 if (legacy_mode_p ())
1906 {
1907 fprintf (file, "%s[", (m_kind == VR_ANTI_RANGE) ? "~" : "");
1908 dump_bound_with_infinite_markers (file, min ());
1909 fprintf (file, ", ");
1910 dump_bound_with_infinite_markers (file, max ());
1911 fprintf (file, "]");
1912 return;
1913 }
1914 for (unsigned i = 0; i < m_num_ranges; ++i)
1915 {
1916 tree lb = m_base[i * 2];
1917 tree ub = m_base[i * 2 + 1];
1918 fprintf (file, "[");
1919 dump_bound_with_infinite_markers (file, lb);
1920 fprintf (file, ", ");
1921 dump_bound_with_infinite_markers (file, ub);
1922 fprintf (file, "]");
1923 }
1924 }
1925
1926 void
1927 dump_value_range (FILE *file, const irange *vr)
1928 {
1929 vr->dump (file);
1930 }
1931
1932 DEBUG_FUNCTION void
1933 debug (const irange *vr)
1934 {
1935 dump_value_range (stderr, vr);
1936 fprintf (stderr, "\n");
1937 }
1938
1939 DEBUG_FUNCTION void
1940 debug (const irange &vr)
1941 {
1942 debug (&vr);
1943 }
1944
1945 DEBUG_FUNCTION void
1946 debug (const value_range *vr)
1947 {
1948 dump_value_range (stderr, vr);
1949 fprintf (stderr, "\n");
1950 }
1951
1952 DEBUG_FUNCTION void
1953 debug (const value_range &vr)
1954 {
1955 dump_value_range (stderr, &vr);
1956 fprintf (stderr, "\n");
1957 }
1958
1959 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1960 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1961 false otherwise. If *AR can be represented with a single range
1962 *VR1 will be VR_UNDEFINED. */
1963
1964 bool
1965 ranges_from_anti_range (const value_range *ar,
1966 value_range *vr0, value_range *vr1)
1967 {
1968 tree type = ar->type ();
1969
1970 vr0->set_undefined ();
1971 vr1->set_undefined ();
1972
1973 /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U
1974 [A+1, +INF]. Not sure if this helps in practice, though. */
1975
1976 if (ar->kind () != VR_ANTI_RANGE
1977 || TREE_CODE (ar->min ()) != INTEGER_CST
1978 || TREE_CODE (ar->max ()) != INTEGER_CST
1979 || !vrp_val_min (type)
1980 || !vrp_val_max (type))
1981 return false;
1982
1983 if (tree_int_cst_lt (vrp_val_min (type), ar->min ()))
1984 vr0->set (vrp_val_min (type),
1985 wide_int_to_tree (type, wi::to_wide (ar->min ()) - 1));
1986 if (tree_int_cst_lt (ar->max (), vrp_val_max (type)))
1987 vr1->set (wide_int_to_tree (type, wi::to_wide (ar->max ()) + 1),
1988 vrp_val_max (type));
1989 if (vr0->undefined_p ())
1990 {
1991 *vr0 = *vr1;
1992 vr1->set_undefined ();
1993 }
1994
1995 return !vr0->undefined_p ();
1996 }
1997
1998 bool
1999 range_has_numeric_bounds_p (const irange *vr)
2000 {
2001 return (!vr->undefined_p ()
2002 && TREE_CODE (vr->min ()) == INTEGER_CST
2003 && TREE_CODE (vr->max ()) == INTEGER_CST);
2004 }
2005
2006 /* Return whether VAL is equal to the maximum value of its type.
2007 We can't do a simple equality comparison with TYPE_MAX_VALUE because
2008 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
2009 is not == to the integer constant with the same value in the type. */
2010
2011 bool
2012 vrp_val_is_max (const_tree val)
2013 {
2014 tree type_max = vrp_val_max (TREE_TYPE (val));
2015 return (val == type_max
2016 || (type_max != NULL_TREE
2017 && operand_equal_p (val, type_max, 0)));
2018 }
2019
2020 /* Return whether VAL is equal to the minimum value of its type. */
2021
2022 bool
2023 vrp_val_is_min (const_tree val)
2024 {
2025 tree type_min = vrp_val_min (TREE_TYPE (val));
2026 return (val == type_min
2027 || (type_min != NULL_TREE
2028 && operand_equal_p (val, type_min, 0)));
2029 }
2030
2031 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
2032
2033 bool
2034 vrp_operand_equal_p (const_tree val1, const_tree val2)
2035 {
2036 if (val1 == val2)
2037 return true;
2038 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
2039 return false;
2040 return true;
2041 }
2042
2043 #define DEFINE_INT_RANGE_GC_STUBS(N) \
2044 void \
2045 gt_pch_nx (int_range<N> *&x) \
2046 { \
2047 for (unsigned i = 0; i < N; ++i) \
2048 { \
2049 gt_pch_nx (x->m_ranges[i * 2]); \
2050 gt_pch_nx (x->m_ranges[i * 2 + 1]); \
2051 } \
2052 } \
2053 \
2054 void \
2055 gt_ggc_mx (int_range<N> *&x) \
2056 { \
2057 for (unsigned i = 0; i < N; ++i) \
2058 { \
2059 gt_ggc_mx (x->m_ranges[i * 2]); \
2060 gt_ggc_mx (x->m_ranges[i * 2 + 1]); \
2061 } \
2062 }
2063
2064 #define DEFINE_INT_RANGE_INSTANCE(N) \
2065 template int_range<N>::int_range(tree, tree, value_range_kind); \
2066 template int_range<N>::int_range(tree_node *, \
2067 const wide_int &, \
2068 const wide_int &, \
2069 value_range_kind); \
2070 template int_range<N>::int_range(tree); \
2071 template int_range<N>::int_range(const irange &); \
2072 template int_range<N>::int_range(const int_range &); \
2073 template int_range<N>& int_range<N>::operator= (const int_range &);
2074
2075 DEFINE_INT_RANGE_INSTANCE(1)
2076 DEFINE_INT_RANGE_INSTANCE(2)
2077 DEFINE_INT_RANGE_INSTANCE(3)
2078 DEFINE_INT_RANGE_INSTANCE(255)
2079 DEFINE_INT_RANGE_GC_STUBS(1)
2080
2081 #if CHECKING_P
2082 #include "selftest.h"
2083
2084 namespace selftest
2085 {
2086 #define INT(N) build_int_cst (integer_type_node, (N))
2087 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
2088 #define UINT128(N) build_int_cstu (u128_type, (N))
2089 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
2090 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
2091
2092 static int_range<3>
2093 build_range3 (int a, int b, int c, int d, int e, int f)
2094 {
2095 int_range<3> i1 (INT (a), INT (b));
2096 int_range<3> i2 (INT (c), INT (d));
2097 int_range<3> i3 (INT (e), INT (f));
2098 i1.union_ (i2);
2099 i1.union_ (i3);
2100 return i1;
2101 }
2102
2103 static void
2104 range_tests_irange3 ()
2105 {
2106 typedef int_range<3> int_range3;
2107 int_range3 r0, r1, r2;
2108 int_range3 i1, i2, i3;
2109
2110 // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20].
2111 r0 = int_range3 (INT (10), INT (20));
2112 r1 = int_range3 (INT (5), INT (8));
2113 r0.union_ (r1);
2114 r1 = int_range3 (INT (1), INT (3));
2115 r0.union_ (r1);
2116 ASSERT_TRUE (r0 == build_range3 (1, 3, 5, 8, 10, 20));
2117
2118 // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20].
2119 r1 = int_range3 (INT (-5), INT (0));
2120 r0.union_ (r1);
2121 ASSERT_TRUE (r0 == build_range3 (-5, 3, 5, 8, 10, 20));
2122
2123 // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60].
2124 r1 = int_range3 (INT (50), INT (60));
2125 r0 = int_range3 (INT (10), INT (20));
2126 r0.union_ (int_range3 (INT (30), INT (40)));
2127 r0.union_ (r1);
2128 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
2129 // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80].
2130 r1 = int_range3 (INT (70), INT (80));
2131 r0.union_ (r1);
2132
2133 r2 = build_range3 (10, 20, 30, 40, 50, 60);
2134 r2.union_ (int_range3 (INT (70), INT (80)));
2135 ASSERT_TRUE (r0 == r2);
2136
2137 // [10,20][30,40][50,60] U [6,35] => [6,40][50,60].
2138 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2139 r1 = int_range3 (INT (6), INT (35));
2140 r0.union_ (r1);
2141 r1 = int_range3 (INT (6), INT (40));
2142 r1.union_ (int_range3 (INT (50), INT (60)));
2143 ASSERT_TRUE (r0 == r1);
2144
2145 // [10,20][30,40][50,60] U [6,60] => [6,60].
2146 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2147 r1 = int_range3 (INT (6), INT (60));
2148 r0.union_ (r1);
2149 ASSERT_TRUE (r0 == int_range3 (INT (6), INT (60)));
2150
2151 // [10,20][30,40][50,60] U [6,70] => [6,70].
2152 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2153 r1 = int_range3 (INT (6), INT (70));
2154 r0.union_ (r1);
2155 ASSERT_TRUE (r0 == int_range3 (INT (6), INT (70)));
2156
2157 // [10,20][30,40][50,60] U [35,70] => [10,20][30,70].
2158 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2159 r1 = int_range3 (INT (35), INT (70));
2160 r0.union_ (r1);
2161 r1 = int_range3 (INT (10), INT (20));
2162 r1.union_ (int_range3 (INT (30), INT (70)));
2163 ASSERT_TRUE (r0 == r1);
2164
2165 // [10,20][30,40][50,60] U [15,35] => [10,40][50,60].
2166 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2167 r1 = int_range3 (INT (15), INT (35));
2168 r0.union_ (r1);
2169 r1 = int_range3 (INT (10), INT (40));
2170 r1.union_ (int_range3 (INT (50), INT (60)));
2171 ASSERT_TRUE (r0 == r1);
2172
2173 // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60].
2174 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2175 r1 = int_range3 (INT (35), INT (35));
2176 r0.union_ (r1);
2177 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
2178 }
2179
2180 static void
2181 range_tests_int_range_max ()
2182 {
2183 int_range_max big;
2184 unsigned int nrange;
2185
2186 // Build a huge multi-range range.
2187 for (nrange = 0; nrange < 50; ++nrange)
2188 {
2189 int_range<1> tmp (INT (nrange*10), INT (nrange*10 + 5));
2190 big.union_ (tmp);
2191 }
2192 ASSERT_TRUE (big.num_pairs () == nrange);
2193
2194 // Verify that we can copy it without loosing precision.
2195 int_range_max copy (big);
2196 ASSERT_TRUE (copy.num_pairs () == nrange);
2197
2198 // Inverting it should produce one more sub-range.
2199 big.invert ();
2200 ASSERT_TRUE (big.num_pairs () == nrange + 1);
2201
2202 int_range<1> tmp (INT (5), INT (37));
2203 big.intersect (tmp);
2204 ASSERT_TRUE (big.num_pairs () == 4);
2205
2206 // Test that [10,10][20,20] does NOT contain 15.
2207 {
2208 int_range_max i1 (build_int_cst (integer_type_node, 10),
2209 build_int_cst (integer_type_node, 10));
2210 int_range_max i2 (build_int_cst (integer_type_node, 20),
2211 build_int_cst (integer_type_node, 20));
2212 i1.union_ (i2);
2213 ASSERT_FALSE (i1.contains_p (build_int_cst (integer_type_node, 15)));
2214 }
2215 }
2216
2217 static void
2218 range_tests_legacy ()
2219 {
2220 // Test truncating copy to int_range<1>.
2221 int_range<3> big = build_range3 (10, 20, 30, 40, 50, 60);
2222 int_range<1> small = big;
2223 ASSERT_TRUE (small == int_range<1> (INT (10), INT (60)));
2224
2225 // Test truncating copy to int_range<2>.
2226 int_range<2> medium = big;
2227 ASSERT_TRUE (!medium.undefined_p ());
2228
2229 // Test that a truncating copy of [MIN,20][22,40][80,MAX]
2230 // ends up as a conservative anti-range of ~[21,21].
2231 big = int_range<3> (vrp_val_min (integer_type_node), INT (20));
2232 big.union_ (int_range<1> (INT (22), INT (40)));
2233 big.union_ (int_range<1> (INT (80), vrp_val_max (integer_type_node)));
2234 small = big;
2235 ASSERT_TRUE (small == int_range<1> (INT (21), INT (21), VR_ANTI_RANGE));
2236
2237 // Copying a legacy symbolic to an int_range should normalize the
2238 // symbolic at copy time.
2239 {
2240 tree ssa = make_ssa_name (integer_type_node);
2241 value_range legacy_range (ssa, INT (25));
2242 int_range<2> copy = legacy_range;
2243 ASSERT_TRUE (copy == int_range<2> (vrp_val_min (integer_type_node),
2244 INT (25)));
2245
2246 // Test that copying ~[abc_23, abc_23] to a multi-range yields varying.
2247 legacy_range = value_range (ssa, ssa, VR_ANTI_RANGE);
2248 copy = legacy_range;
2249 ASSERT_TRUE (copy.varying_p ());
2250 }
2251 }
2252
2253 // Simulate -fstrict-enums where the domain of a type is less than the
2254 // underlying type.
2255
2256 static void
2257 range_tests_strict_enum ()
2258 {
2259 // The enum can only hold [0, 3].
2260 tree rtype = copy_node (unsigned_type_node);
2261 TYPE_MIN_VALUE (rtype) = build_int_cstu (rtype, 0);
2262 TYPE_MAX_VALUE (rtype) = build_int_cstu (rtype, 3);
2263
2264 // Test that even though vr1 covers the strict enum domain ([0, 3]),
2265 // it does not cover the domain of the underlying type.
2266 int_range<1> vr1 (build_int_cstu (rtype, 0), build_int_cstu (rtype, 1));
2267 int_range<1> vr2 (build_int_cstu (rtype, 2), build_int_cstu (rtype, 3));
2268 vr1.union_ (vr2);
2269 ASSERT_TRUE (vr1 == int_range<1> (build_int_cstu (rtype, 0),
2270 build_int_cstu (rtype, 3)));
2271 ASSERT_FALSE (vr1.varying_p ());
2272
2273 // Test that copying to a multi-range does not change things.
2274 int_range<2> ir1 (vr1);
2275 ASSERT_TRUE (ir1 == vr1);
2276 ASSERT_FALSE (ir1.varying_p ());
2277
2278 // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3].
2279 vr1 = int_range<1> (TYPE_MIN_VALUE (rtype), TYPE_MAX_VALUE (rtype));
2280 ir1 = vr1;
2281 ASSERT_TRUE (ir1 == vr1);
2282 ASSERT_FALSE (ir1.varying_p ());
2283 }
2284
2285 static void
2286 range_tests_misc ()
2287 {
2288 tree u128_type = build_nonstandard_integer_type (128, /*unsigned=*/1);
2289 int_range<1> i1, i2, i3;
2290 int_range<1> r0, r1, rold;
2291
2292 // Test 1-bit signed integer union.
2293 // [-1,-1] U [0,0] = VARYING.
2294 tree one_bit_type = build_nonstandard_integer_type (1, 0);
2295 tree one_bit_min = vrp_val_min (one_bit_type);
2296 tree one_bit_max = vrp_val_max (one_bit_type);
2297 {
2298 int_range<2> min (one_bit_min, one_bit_min);
2299 int_range<2> max (one_bit_max, one_bit_max);
2300 max.union_ (min);
2301 ASSERT_TRUE (max.varying_p ());
2302 }
2303
2304 // Test inversion of 1-bit signed integers.
2305 {
2306 int_range<2> min (one_bit_min, one_bit_min);
2307 int_range<2> max (one_bit_max, one_bit_max);
2308 int_range<2> t;
2309 t = min;
2310 t.invert ();
2311 ASSERT_TRUE (t == max);
2312 t = max;
2313 t.invert ();
2314 ASSERT_TRUE (t == min);
2315 }
2316
2317 // Test that NOT(255) is [0..254] in 8-bit land.
2318 int_range<1> not_255 (UCHAR (255), UCHAR (255), VR_ANTI_RANGE);
2319 ASSERT_TRUE (not_255 == int_range<1> (UCHAR (0), UCHAR (254)));
2320
2321 // Test that NOT(0) is [1..255] in 8-bit land.
2322 int_range<1> not_zero = range_nonzero (unsigned_char_type_node);
2323 ASSERT_TRUE (not_zero == int_range<1> (UCHAR (1), UCHAR (255)));
2324
2325 // Check that [0,127][0x..ffffff80,0x..ffffff]
2326 // => ~[128, 0x..ffffff7f].
2327 r0 = int_range<1> (UINT128 (0), UINT128 (127));
2328 tree high = build_minus_one_cst (u128_type);
2329 // low = -1 - 127 => 0x..ffffff80.
2330 tree low = fold_build2 (MINUS_EXPR, u128_type, high, UINT128(127));
2331 r1 = int_range<1> (low, high); // [0x..ffffff80, 0x..ffffffff]
2332 // r0 = [0,127][0x..ffffff80,0x..fffffff].
2333 r0.union_ (r1);
2334 // r1 = [128, 0x..ffffff7f].
2335 r1 = int_range<1> (UINT128(128),
2336 fold_build2 (MINUS_EXPR, u128_type,
2337 build_minus_one_cst (u128_type),
2338 UINT128(128)));
2339 r0.invert ();
2340 ASSERT_TRUE (r0 == r1);
2341
2342 r0.set_varying (integer_type_node);
2343 tree minint = wide_int_to_tree (integer_type_node, r0.lower_bound ());
2344 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
2345
2346 r0.set_varying (short_integer_type_node);
2347
2348 r0.set_varying (unsigned_type_node);
2349 tree maxuint = wide_int_to_tree (unsigned_type_node, r0.upper_bound ());
2350
2351 // Check that ~[0,5] => [6,MAX] for unsigned int.
2352 r0 = int_range<1> (UINT (0), UINT (5));
2353 r0.invert ();
2354 ASSERT_TRUE (r0 == int_range<1> (UINT(6), maxuint));
2355
2356 // Check that ~[10,MAX] => [0,9] for unsigned int.
2357 r0 = int_range<1> (UINT(10), maxuint);
2358 r0.invert ();
2359 ASSERT_TRUE (r0 == int_range<1> (UINT (0), UINT (9)));
2360
2361 // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers.
2362 r0 = int_range<1> (UINT128 (0), UINT128 (5), VR_ANTI_RANGE);
2363 r1 = int_range<1> (UINT128(6), build_minus_one_cst (u128_type));
2364 ASSERT_TRUE (r0 == r1);
2365
2366 // Check that [~5] is really [-MIN,4][6,MAX].
2367 r0 = int_range<1> (INT (5), INT (5), VR_ANTI_RANGE);
2368 r1 = int_range<1> (minint, INT (4));
2369 r1.union_ (int_range<1> (INT (6), maxint));
2370 ASSERT_FALSE (r1.undefined_p ());
2371 ASSERT_TRUE (r0 == r1);
2372
2373 r1 = int_range<1> (INT (5), INT (5));
2374 int_range<1> r2 (r1);
2375 ASSERT_TRUE (r1 == r2);
2376
2377 r1 = int_range<1> (INT (5), INT (10));
2378
2379 r1 = int_range<1> (integer_type_node,
2380 wi::to_wide (INT (5)), wi::to_wide (INT (10)));
2381 ASSERT_TRUE (r1.contains_p (INT (7)));
2382
2383 r1 = int_range<1> (SCHAR (0), SCHAR (20));
2384 ASSERT_TRUE (r1.contains_p (SCHAR(15)));
2385 ASSERT_FALSE (r1.contains_p (SCHAR(300)));
2386
2387 // NOT([10,20]) ==> [-MIN,9][21,MAX].
2388 r0 = r1 = int_range<1> (INT (10), INT (20));
2389 r2 = int_range<1> (minint, INT(9));
2390 r2.union_ (int_range<1> (INT(21), maxint));
2391 ASSERT_FALSE (r2.undefined_p ());
2392 r1.invert ();
2393 ASSERT_TRUE (r1 == r2);
2394 // Test that NOT(NOT(x)) == x.
2395 r2.invert ();
2396 ASSERT_TRUE (r0 == r2);
2397
2398 // Test that booleans and their inverse work as expected.
2399 r0 = range_zero (boolean_type_node);
2400 ASSERT_TRUE (r0 == int_range<1> (build_zero_cst (boolean_type_node),
2401 build_zero_cst (boolean_type_node)));
2402 r0.invert ();
2403 ASSERT_TRUE (r0 == int_range<1> (build_one_cst (boolean_type_node),
2404 build_one_cst (boolean_type_node)));
2405
2406 // Make sure NULL and non-NULL of pointer types work, and that
2407 // inverses of them are consistent.
2408 tree voidp = build_pointer_type (void_type_node);
2409 r0 = range_zero (voidp);
2410 r1 = r0;
2411 r0.invert ();
2412 r0.invert ();
2413 ASSERT_TRUE (r0 == r1);
2414
2415 // [10,20] U [15, 30] => [10, 30].
2416 r0 = int_range<1> (INT (10), INT (20));
2417 r1 = int_range<1> (INT (15), INT (30));
2418 r0.union_ (r1);
2419 ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (30)));
2420
2421 // [15,40] U [] => [15,40].
2422 r0 = int_range<1> (INT (15), INT (40));
2423 r1.set_undefined ();
2424 r0.union_ (r1);
2425 ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (40)));
2426
2427 // [10,20] U [10,10] => [10,20].
2428 r0 = int_range<1> (INT (10), INT (20));
2429 r1 = int_range<1> (INT (10), INT (10));
2430 r0.union_ (r1);
2431 ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (20)));
2432
2433 // [10,20] U [9,9] => [9,20].
2434 r0 = int_range<1> (INT (10), INT (20));
2435 r1 = int_range<1> (INT (9), INT (9));
2436 r0.union_ (r1);
2437 ASSERT_TRUE (r0 == int_range<1> (INT (9), INT (20)));
2438
2439 // [10,20] ^ [15,30] => [15,20].
2440 r0 = int_range<1> (INT (10), INT (20));
2441 r1 = int_range<1> (INT (15), INT (30));
2442 r0.intersect (r1);
2443 ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (20)));
2444
2445 // Test the internal sanity of wide_int's wrt HWIs.
2446 ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node),
2447 TYPE_SIGN (boolean_type_node))
2448 == wi::uhwi (1, TYPE_PRECISION (boolean_type_node)));
2449
2450 // Test zero_p().
2451 r0 = int_range<1> (INT (0), INT (0));
2452 ASSERT_TRUE (r0.zero_p ());
2453
2454 // Test nonzero_p().
2455 r0 = int_range<1> (INT (0), INT (0));
2456 r0.invert ();
2457 ASSERT_TRUE (r0.nonzero_p ());
2458
2459 // test legacy interaction
2460 // r0 = ~[1,1]
2461 r0 = int_range<1> (UINT (1), UINT (1), VR_ANTI_RANGE);
2462 // r1 = ~[3,3]
2463 r1 = int_range<1> (UINT (3), UINT (3), VR_ANTI_RANGE);
2464
2465 // vv = [0,0][2,2][4, MAX]
2466 int_range<3> vv = r0;
2467 vv.intersect (r1);
2468
2469 ASSERT_TRUE (vv.contains_p (UINT (2)));
2470 ASSERT_TRUE (vv.num_pairs () == 3);
2471
2472 // create r0 as legacy [1,1]
2473 r0 = int_range<1> (UINT (1), UINT (1));
2474 // And union it with [0,0][2,2][4,MAX] multi range
2475 r0.union_ (vv);
2476 // The result should be [0,2][4,MAX], or ~[3,3] but it must contain 2
2477 ASSERT_TRUE (r0.contains_p (UINT (2)));
2478 }
2479
2480 void
2481 range_tests ()
2482 {
2483 range_tests_legacy ();
2484 range_tests_irange3 ();
2485 range_tests_int_range_max ();
2486 range_tests_strict_enum ();
2487 range_tests_misc ();
2488 }
2489
2490 } // namespace selftest
2491
2492 #endif // CHECKING_P