]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/value-range.cc
Update gcc sv.po.
[thirdparty/gcc.git] / gcc / value-range.cc
CommitLineData
cca78449 1/* Support routines for value ranges.
99dee823 2 Copyright (C) 2019-2021 Free Software Foundation, Inc.
4ba9fb0a
AH
3 Major hacks by Aldy Hernandez <aldyh@redhat.com> and
4 Andrew MacLeod <amacleod@redhat.com>.
cca78449
AH
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along 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
4ba9fb0a
AH
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
35irange &
36irange::operator= (const irange &src)
37{
5bcd7de6 38 if (legacy_mode_p ())
4ba9fb0a 39 {
5bcd7de6 40 copy_to_legacy (src);
4ba9fb0a
AH
41 return *this;
42 }
5bcd7de6 43 if (src.legacy_mode_p ())
4ba9fb0a 44 {
5bcd7de6 45 copy_legacy_to_multi_range (src);
4ba9fb0a
AH
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
68bool
69irange::maybe_anti_range () const
cca78449 70{
4ba9fb0a
AH
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));
cca78449
AH
78}
79
4ba9fb0a 80void
5bcd7de6 81irange::copy_legacy_to_multi_range (const irange &src)
cca78449 82{
5bcd7de6
AH
83 gcc_checking_assert (src.legacy_mode_p ());
84 gcc_checking_assert (!legacy_mode_p ());
4ba9fb0a
AH
85 if (src.undefined_p ())
86 set_undefined ();
87 else if (src.varying_p ())
88 set_varying (src.type ());
4ba9fb0a 89 else
80cbca32 90 {
5bcd7de6
AH
91 if (range_has_numeric_bounds_p (&src))
92 set (src.min (), src.max (), src.kind ());
93 else
80cbca32
AH
94 {
95 value_range cst (src);
96 cst.normalize_symbolics ();
5bcd7de6 97 gcc_checking_assert (cst.varying_p () || cst.kind () == VR_RANGE);
80cbca32 98 set (cst.min (), cst.max ());
80cbca32 99 }
80cbca32 100 }
cca78449
AH
101}
102
5bcd7de6
AH
103// Copy any type of irange into a legacy.
104
105void
106irange::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
4e85ad79 134// Swap MIN/MAX if they are out of order and adjust KIND appropriately.
4ba9fb0a 135
4e85ad79
AH
136static void
137swap_out_of_order_endpoints (tree &min, tree &max, value_range_kind &kind)
cca78449 138{
4e85ad79
AH
139 gcc_checking_assert (kind != VR_UNDEFINED);
140 if (kind == VR_VARYING)
141 return;
4ba9fb0a
AH
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 {
4e85ad79
AH
153 kind = VR_VARYING;
154 return;
4ba9fb0a
AH
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 {
4e85ad79
AH
167 kind = VR_VARYING;
168 return;
4ba9fb0a
AH
169 }
170 kind = kind == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
171 }
cca78449
AH
172}
173
174void
4ba9fb0a 175irange::irange_set (tree min, tree max)
cca78449 176{
4ba9fb0a
AH
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 ();
cca78449
AH
185}
186
187void
4ba9fb0a 188irange::irange_set_anti_range (tree min, tree max)
cca78449 189{
4ba9fb0a
AH
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 ()))
cca78449 203 {
4ba9fb0a
AH
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;
cca78449 209 }
4ba9fb0a
AH
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 ();
cca78449
AH
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
232void
4ba9fb0a 233irange::set (tree min, tree max, value_range_kind kind)
cca78449 234{
4ba9fb0a
AH
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 }
cca78449
AH
246 if (kind == VR_UNDEFINED)
247 {
248 set_undefined ();
249 return;
250 }
54ef7701 251
c76c23a0
AH
252 if (kind == VR_VARYING
253 || POLY_INT_CST_P (min)
254 || POLY_INT_CST_P (max))
5e41e7f0
AH
255 {
256 set_varying (TREE_TYPE (min));
257 return;
258 }
54ef7701 259
4ba9fb0a 260 // Nothing to canonicalize for symbolic ranges.
cca78449
AH
261 if (TREE_CODE (min) != INTEGER_CST
262 || TREE_CODE (max) != INTEGER_CST)
263 {
264 m_kind = kind;
4ba9fb0a
AH
265 m_base[0] = min;
266 m_base[1] = max;
267 m_num_ranges = 1;
cca78449
AH
268 return;
269 }
4e85ad79
AH
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 }
cca78449 277
4ba9fb0a 278 // Anti-ranges that can be represented as ranges should be so.
cca78449
AH
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);
4e85ad79 285 tree type = TREE_TYPE (min);
cca78449
AH
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 }
cca78449 320
4e85ad79
AH
321 m_kind = kind;
322 m_base[0] = min;
323 m_base[1] = max;
324 m_num_ranges = 1;
325 normalize_min_max ();
cca78449 326 if (flag_checking)
4ba9fb0a 327 verify_range ();
cca78449
AH
328}
329
4e85ad79 330// Check the validity of the range.
cca78449
AH
331
332void
4ba9fb0a 333irange::verify_range ()
cca78449 334{
4ba9fb0a 335 if (!legacy_mode_p ())
cca78449 336 {
4ba9fb0a
AH
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 }
cca78449 347
4ba9fb0a
AH
348 switch (m_kind)
349 {
cca78449 350 case VR_UNDEFINED:
4ba9fb0a 351 gcc_assert (m_num_ranges == 0);
cca78449 352 break;
4ba9fb0a 353
cca78449 354 case VR_VARYING:
4ba9fb0a 355 gcc_assert (m_num_ranges == 1);
cca78449 356 break;
4ba9fb0a
AH
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
cca78449
AH
367 default:
368 gcc_unreachable ();
369 }
370}
371
cca78449 372unsigned
4ba9fb0a 373irange::legacy_num_pairs () const
cca78449 374{
4ba9fb0a
AH
375 gcc_checking_assert (legacy_mode_p ());
376
cca78449
AH
377 if (undefined_p ())
378 return 0;
379 if (varying_p ())
380 return 1;
4ba9fb0a
AH
381 // Inlined symbolic_p for performance:
382 if (!is_gimple_min_invariant (min ()) || !is_gimple_min_invariant (max ()))
6ee86466
AH
383 {
384 value_range numeric_range (*this);
385 numeric_range.normalize_symbolics ();
386 return numeric_range.num_pairs ();
387 }
cca78449
AH
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].
4ba9fb0a 392 if (vrp_val_is_min (min ()) || vrp_val_is_max (max ()))
cca78449
AH
393 return 1;
394 return 2;
395 }
4ba9fb0a 396 gcc_checking_assert (m_num_ranges == 1);
cca78449
AH
397 return 1;
398}
399
4ba9fb0a
AH
400// Return the lower bound for a sub-range. PAIR is the sub-range in
401// question.
cca78449
AH
402
403wide_int
4ba9fb0a 404irange::legacy_lower_bound (unsigned pair) const
cca78449 405{
4ba9fb0a 406 gcc_checking_assert (legacy_mode_p ());
cca78449 407 if (symbolic_p ())
6ee86466
AH
408 {
409 value_range numeric_range (*this);
410 numeric_range.normalize_symbolics ();
4ba9fb0a 411 return numeric_range.legacy_lower_bound (pair);
6ee86466 412 }
cca78449
AH
413 gcc_checking_assert (!undefined_p ());
414 gcc_checking_assert (pair + 1 <= num_pairs ());
cca78449
AH
415 if (m_kind == VR_ANTI_RANGE)
416 {
4ba9fb0a
AH
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);
cca78449
AH
420 else
421 t = vrp_val_min (typ);
4ba9fb0a 422 return wi::to_wide (t);
cca78449 423 }
4ba9fb0a 424 return wi::to_wide (tree_lower_bound (pair));
cca78449
AH
425}
426
4ba9fb0a
AH
427// Return the upper bound for a sub-range. PAIR is the sub-range in
428// question.
cca78449
AH
429
430wide_int
4ba9fb0a 431irange::legacy_upper_bound (unsigned pair) const
cca78449 432{
4ba9fb0a 433 gcc_checking_assert (legacy_mode_p ());
cca78449 434 if (symbolic_p ())
6ee86466
AH
435 {
436 value_range numeric_range (*this);
437 numeric_range.normalize_symbolics ();
4ba9fb0a 438 return numeric_range.legacy_upper_bound (pair);
6ee86466 439 }
cca78449
AH
440 gcc_checking_assert (!undefined_p ());
441 gcc_checking_assert (pair + 1 <= num_pairs ());
cca78449
AH
442 if (m_kind == VR_ANTI_RANGE)
443 {
4ba9fb0a
AH
444 tree typ = type (), t;
445 if (pair == 1 || vrp_val_is_min (min ()))
cca78449
AH
446 t = vrp_val_max (typ);
447 else
4ba9fb0a
AH
448 t = wide_int_to_tree (typ, wi::to_wide (min ()) - 1);
449 return wi::to_wide (t);
cca78449 450 }
4ba9fb0a 451 return wi::to_wide (tree_upper_bound (pair));
cca78449
AH
452}
453
454bool
4ba9fb0a 455irange::legacy_equal_p (const irange &other) const
cca78449 456{
4ba9fb0a 457 gcc_checking_assert (legacy_mode_p () && other.legacy_mode_p ());
cca78449 458
4ba9fb0a
AH
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)));
cca78449
AH
467}
468
469bool
4ba9fb0a 470irange::equal_p (const irange &other) const
cca78449 471{
4ba9fb0a
AH
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;
cca78449
AH
499}
500
cca78449
AH
501/* Return TRUE if this is a symbolic range. */
502
503bool
4ba9fb0a 504irange::symbolic_p () const
cca78449
AH
505{
506 return (!varying_p ()
507 && !undefined_p ()
4ba9fb0a
AH
508 && (!is_gimple_min_invariant (min ())
509 || !is_gimple_min_invariant (max ())));
cca78449
AH
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
517bool
4ba9fb0a 518irange::constant_p () const
cca78449
AH
519{
520 return (!varying_p ()
521 && !undefined_p ()
4ba9fb0a
AH
522 && TREE_CODE (min ()) == INTEGER_CST
523 && TREE_CODE (max ()) == INTEGER_CST);
cca78449
AH
524}
525
4ba9fb0a
AH
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
cca78449 530bool
4ba9fb0a 531irange::singleton_p (tree *result) const
cca78449 532{
4ba9fb0a
AH
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 }
cca78449
AH
544 if (m_kind == VR_ANTI_RANGE)
545 {
546 if (nonzero_p ())
547 {
548 if (TYPE_PRECISION (type ()) == 1)
549 {
550 if (result)
4ba9fb0a 551 *result = max ();
cca78449
AH
552 return true;
553 }
554 return false;
555 }
556 if (num_pairs () == 1)
557 {
558 value_range vr0, vr1;
4ba9fb0a 559 ranges_from_anti_range ((const value_range *) this, &vr0, &vr1);
cca78449
AH
560 return vr0.singleton_p (result);
561 }
562 }
4ba9fb0a 563 // Catches non-numeric extremes as well.
cca78449
AH
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.
4ba9fb0a 576 0 if VAL is not inside value range.
cca78449
AH
577 -2 if we cannot tell either way.
578
579 Benchmark compile/20001226-1.c compilation time after changing this
580 function. */
581
582int
4ba9fb0a 583irange::value_inside_range (tree val) const
cca78449 584{
cca78449
AH
585 if (varying_p ())
586 return 1;
587
588 if (undefined_p ())
589 return 0;
590
4ba9fb0a
AH
591 if (!legacy_mode_p () && TREE_CODE (val) == INTEGER_CST)
592 return contains_p (val);
593
594 int cmp1 = operand_less_p (val, min ());
cca78449
AH
595 if (cmp1 == -2)
596 return -2;
597 if (cmp1 == 1)
598 return m_kind != VR_RANGE;
599
4ba9fb0a 600 int cmp2 = operand_less_p (max (), val);
cca78449
AH
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
612bool
4ba9fb0a 613irange::may_contain_p (tree val) const
cca78449
AH
614{
615 return value_inside_range (val) != 0;
616}
617
618/* Return TRUE if range contains INTEGER_CST. */
4ba9fb0a
AH
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
cca78449
AH
625
626bool
4ba9fb0a 627irange::contains_p (tree cst) const
cca78449 628{
4ba9fb0a
AH
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
cca78449 644 gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST);
4ba9fb0a
AH
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)
6ee86466 648 {
4ba9fb0a
AH
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;
6ee86466 653 }
4ba9fb0a
AH
654
655 return false;
cca78449
AH
656}
657
4ba9fb0a 658
cca78449
AH
659/* Normalize addresses into constants. */
660
6ee86466 661void
4ba9fb0a 662irange::normalize_addresses ()
cca78449
AH
663{
664 if (undefined_p ())
6ee86466 665 return;
cca78449
AH
666
667 if (!POINTER_TYPE_P (type ()) || range_has_numeric_bounds_p (this))
6ee86466 668 return;
cca78449
AH
669
670 if (!range_includes_zero_p (this))
671 {
4ba9fb0a
AH
672 gcc_checking_assert (TREE_CODE (min ()) == ADDR_EXPR
673 || TREE_CODE (max ()) == ADDR_EXPR);
6ee86466
AH
674 set_nonzero (type ());
675 return;
cca78449 676 }
6ee86466 677 set_varying (type ());
cca78449
AH
678}
679
680/* Normalize symbolics and addresses into constants. */
681
6ee86466 682void
4ba9fb0a 683irange::normalize_symbolics ()
cca78449
AH
684{
685 if (varying_p () || undefined_p ())
6ee86466
AH
686 return;
687
cca78449
AH
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)
6ee86466
AH
692 {
693 normalize_addresses ();
694 return;
695 }
cca78449
AH
696
697 // [SYM, SYM] -> VARYING
698 if (min_symbolic && max_symbolic)
699 {
6ee86466
AH
700 set_varying (ttype);
701 return;
cca78449
AH
702 }
703 if (kind () == VR_RANGE)
704 {
705 // [SYM, NUM] -> [-MIN, NUM]
706 if (min_symbolic)
6ee86466
AH
707 {
708 set (vrp_val_min (ttype), max ());
709 return;
710 }
cca78449 711 // [NUM, SYM] -> [NUM, +MAX]
6ee86466
AH
712 set (min (), vrp_val_max (ttype));
713 return;
cca78449
AH
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);
6ee86466
AH
722 set (n, vrp_val_max (ttype));
723 return;
cca78449 724 }
6ee86466
AH
725 set_varying (ttype);
726 return;
cca78449
AH
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);
6ee86466
AH
732 set (vrp_val_min (ttype), n);
733 return;
cca78449 734 }
6ee86466 735 set_varying (ttype);
cca78449
AH
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
743static void
744intersect_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))
a05cc70a
RB
977 && operand_less_p (*vr0min, vr1min) == 1
978 && operand_less_p (*vr0max, vr1max) == 1)
cca78449
AH
979 {
980 /* [ ( ] ) or [ ]( ) */
981 if (*vr0type == VR_ANTI_RANGE
982 && vr1type == VR_ANTI_RANGE)
983 *vr0max = vr1max;
984 else if (*vr0type == VR_RANGE
985 && vr1type == VR_RANGE)
986 *vr0min = vr1min;
987 else if (*vr0type == VR_RANGE
988 && vr1type == VR_ANTI_RANGE)
989 {
990 if (TREE_CODE (vr1min) == INTEGER_CST)
991 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
992 build_int_cst (TREE_TYPE (vr1min), 1));
993 else
994 *vr0max = vr1min;
995 }
996 else if (*vr0type == VR_ANTI_RANGE
997 && vr1type == VR_RANGE)
998 {
999 *vr0type = VR_RANGE;
1000 if (TREE_CODE (*vr0max) == INTEGER_CST)
1001 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
1002 build_int_cst (TREE_TYPE (*vr0max), 1));
1003 else
1004 *vr0min = *vr0max;
1005 *vr0max = vr1max;
1006 }
1007 else
1008 gcc_unreachable ();
1009 }
1010 else if ((operand_less_p (*vr0min, vr1max) == 1
1011 || operand_equal_p (*vr0min, vr1max, 0))
a05cc70a
RB
1012 && operand_less_p (vr1min, *vr0min) == 1
1013 && operand_less_p (vr1max, *vr0max) == 1)
cca78449
AH
1014 {
1015 /* ( [ ) ] or ( )[ ] */
1016 if (*vr0type == VR_ANTI_RANGE
1017 && vr1type == VR_ANTI_RANGE)
1018 *vr0min = vr1min;
1019 else if (*vr0type == VR_RANGE
1020 && vr1type == VR_RANGE)
1021 *vr0max = vr1max;
1022 else if (*vr0type == VR_RANGE
1023 && vr1type == VR_ANTI_RANGE)
1024 {
1025 if (TREE_CODE (vr1max) == INTEGER_CST)
1026 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
1027 build_int_cst (TREE_TYPE (vr1max), 1));
1028 else
1029 *vr0min = vr1max;
1030 }
1031 else if (*vr0type == VR_ANTI_RANGE
1032 && vr1type == VR_RANGE)
1033 {
1034 *vr0type = VR_RANGE;
1035 if (TREE_CODE (*vr0min) == INTEGER_CST)
1036 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
1037 build_int_cst (TREE_TYPE (*vr0min), 1));
1038 else
1039 *vr0max = *vr0min;
1040 *vr0min = vr1min;
1041 }
1042 else
1043 gcc_unreachable ();
1044 }
1045
1046 /* If we know the intersection is empty, there's no need to
1047 conservatively add anything else to the set. */
1048 if (*vr0type == VR_UNDEFINED)
1049 return;
1050
1051 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
1052 result for the intersection. That's always a conservative
1053 correct estimate unless VR1 is a constant singleton range
1054 in which case we choose that. */
1055 if (vr1type == VR_RANGE
1056 && is_gimple_min_invariant (vr1min)
1057 && vrp_operand_equal_p (vr1min, vr1max))
1058 {
1059 *vr0type = vr1type;
1060 *vr0min = vr1min;
1061 *vr0max = vr1max;
1062 }
1063}
1064
1065/* Helper for the intersection operation for value ranges. Given two
4ba9fb0a
AH
1066 ranges VR0 and VR1, set VR0 to the intersection of both ranges.
1067 This may not be the smallest possible such range. */
cca78449 1068
4ba9fb0a
AH
1069void
1070irange::legacy_intersect (irange *vr0, const irange *vr1)
cca78449 1071{
ea6da7f5
AH
1072 gcc_checking_assert (vr0->legacy_mode_p ());
1073 gcc_checking_assert (vr1->legacy_mode_p ());
cca78449
AH
1074 /* If either range is VR_VARYING the other one wins. */
1075 if (vr1->varying_p ())
4ba9fb0a 1076 return;
cca78449 1077 if (vr0->varying_p ())
4ba9fb0a 1078 {
ea6da7f5 1079 vr0->set (vr1->min (), vr1->max (), vr1->kind ());
4ba9fb0a
AH
1080 return;
1081 }
cca78449
AH
1082
1083 /* When either range is VR_UNDEFINED the resulting range is
1084 VR_UNDEFINED, too. */
1085 if (vr0->undefined_p ())
4ba9fb0a 1086 return;
cca78449 1087 if (vr1->undefined_p ())
4ba9fb0a
AH
1088 {
1089 vr0->set_undefined ();
1090 return;
1091 }
cca78449
AH
1092
1093 value_range_kind vr0kind = vr0->kind ();
1094 tree vr0min = vr0->min ();
1095 tree vr0max = vr0->max ();
ea6da7f5
AH
1096
1097 intersect_ranges (&vr0kind, &vr0min, &vr0max,
1098 vr1->kind (), vr1->min (), vr1->max ());
4ba9fb0a 1099
cca78449 1100 /* Make sure to canonicalize the result though as the inversion of a
4ba9fb0a 1101 VR_RANGE can still be a VR_RANGE. */
cca78449 1102 if (vr0kind == VR_UNDEFINED)
4ba9fb0a 1103 vr0->set_undefined ();
cca78449 1104 else if (vr0kind == VR_VARYING)
4ba9fb0a
AH
1105 {
1106 /* If we failed, use the original VR0. */
1107 return;
1108 }
cca78449 1109 else
4ba9fb0a 1110 vr0->set (vr0min, vr0max, vr0kind);
cca78449
AH
1111}
1112
1113/* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
1114 { VR1TYPE, VR0MIN, VR0MAX } and store the result
1115 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
1116 possible such range. The resulting range is not canonicalized. */
1117
1118static void
1119union_ranges (enum value_range_kind *vr0type,
1120 tree *vr0min, tree *vr0max,
1121 enum value_range_kind vr1type,
1122 tree vr1min, tree vr1max)
1123{
1124 int cmpmin = compare_values (*vr0min, vr1min);
1125 int cmpmax = compare_values (*vr0max, vr1max);
1126 bool mineq = cmpmin == 0;
1127 bool maxeq = cmpmax == 0;
1128
1129 /* [] is vr0, () is vr1 in the following classification comments. */
1130 if (mineq && maxeq)
1131 {
1132 /* [( )] */
1133 if (*vr0type == vr1type)
1134 /* Nothing to do for equal ranges. */
1135 ;
1136 else if ((*vr0type == VR_RANGE
1137 && vr1type == VR_ANTI_RANGE)
1138 || (*vr0type == VR_ANTI_RANGE
1139 && vr1type == VR_RANGE))
1140 {
1141 /* For anti-range with range union the result is varying. */
1142 goto give_up;
1143 }
1144 else
1145 gcc_unreachable ();
1146 }
1147 else if (operand_less_p (*vr0max, vr1min) == 1
1148 || operand_less_p (vr1max, *vr0min) == 1)
1149 {
1150 /* [ ] ( ) or ( ) [ ]
1151 If the ranges have an empty intersection, result of the union
1152 operation is the anti-range or if both are anti-ranges
1153 it covers all. */
1154 if (*vr0type == VR_ANTI_RANGE
1155 && vr1type == VR_ANTI_RANGE)
1156 goto give_up;
1157 else if (*vr0type == VR_ANTI_RANGE
1158 && vr1type == VR_RANGE)
1159 ;
1160 else if (*vr0type == VR_RANGE
1161 && vr1type == VR_ANTI_RANGE)
1162 {
1163 *vr0type = vr1type;
1164 *vr0min = vr1min;
1165 *vr0max = vr1max;
1166 }
1167 else if (*vr0type == VR_RANGE
1168 && vr1type == VR_RANGE)
1169 {
1170 /* The result is the convex hull of both ranges. */
1171 if (operand_less_p (*vr0max, vr1min) == 1)
1172 {
1173 /* If the result can be an anti-range, create one. */
1174 if (TREE_CODE (*vr0max) == INTEGER_CST
1175 && TREE_CODE (vr1min) == INTEGER_CST
1176 && vrp_val_is_min (*vr0min)
1177 && vrp_val_is_max (vr1max))
1178 {
1179 tree min = int_const_binop (PLUS_EXPR,
1180 *vr0max,
1181 build_int_cst (TREE_TYPE (*vr0max), 1));
1182 tree max = int_const_binop (MINUS_EXPR,
1183 vr1min,
1184 build_int_cst (TREE_TYPE (vr1min), 1));
1185 if (!operand_less_p (max, min))
1186 {
1187 *vr0type = VR_ANTI_RANGE;
1188 *vr0min = min;
1189 *vr0max = max;
1190 }
1191 else
1192 *vr0max = vr1max;
1193 }
1194 else
1195 *vr0max = vr1max;
1196 }
1197 else
1198 {
1199 /* If the result can be an anti-range, create one. */
1200 if (TREE_CODE (vr1max) == INTEGER_CST
1201 && TREE_CODE (*vr0min) == INTEGER_CST
1202 && vrp_val_is_min (vr1min)
1203 && vrp_val_is_max (*vr0max))
1204 {
1205 tree min = int_const_binop (PLUS_EXPR,
1206 vr1max,
1207 build_int_cst (TREE_TYPE (vr1max), 1));
1208 tree max = int_const_binop (MINUS_EXPR,
1209 *vr0min,
1210 build_int_cst (TREE_TYPE (*vr0min), 1));
1211 if (!operand_less_p (max, min))
1212 {
1213 *vr0type = VR_ANTI_RANGE;
1214 *vr0min = min;
1215 *vr0max = max;
1216 }
1217 else
1218 *vr0min = vr1min;
1219 }
1220 else
1221 *vr0min = vr1min;
1222 }
1223 }
1224 else
1225 gcc_unreachable ();
1226 }
1227 else if ((maxeq || cmpmax == 1)
1228 && (mineq || cmpmin == -1))
1229 {
1230 /* [ ( ) ] or [( ) ] or [ ( )] */
1231 if (*vr0type == VR_RANGE
1232 && vr1type == VR_RANGE)
1233 ;
1234 else if (*vr0type == VR_ANTI_RANGE
1235 && vr1type == VR_ANTI_RANGE)
1236 {
1237 *vr0type = vr1type;
1238 *vr0min = vr1min;
1239 *vr0max = vr1max;
1240 }
1241 else if (*vr0type == VR_ANTI_RANGE
1242 && vr1type == VR_RANGE)
1243 {
1244 /* Arbitrarily choose the right or left gap. */
1245 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
1246 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
1247 build_int_cst (TREE_TYPE (vr1min), 1));
1248 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
1249 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
1250 build_int_cst (TREE_TYPE (vr1max), 1));
1251 else
1252 goto give_up;
1253 }
1254 else if (*vr0type == VR_RANGE
1255 && vr1type == VR_ANTI_RANGE)
1256 /* The result covers everything. */
1257 goto give_up;
1258 else
1259 gcc_unreachable ();
1260 }
1261 else if ((maxeq || cmpmax == -1)
1262 && (mineq || cmpmin == 1))
1263 {
1264 /* ( [ ] ) or ([ ] ) or ( [ ]) */
1265 if (*vr0type == VR_RANGE
1266 && vr1type == VR_RANGE)
1267 {
1268 *vr0type = vr1type;
1269 *vr0min = vr1min;
1270 *vr0max = vr1max;
1271 }
1272 else if (*vr0type == VR_ANTI_RANGE
1273 && vr1type == VR_ANTI_RANGE)
1274 ;
1275 else if (*vr0type == VR_RANGE
1276 && vr1type == VR_ANTI_RANGE)
1277 {
1278 *vr0type = VR_ANTI_RANGE;
1279 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
1280 {
1281 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
1282 build_int_cst (TREE_TYPE (*vr0min), 1));
1283 *vr0min = vr1min;
1284 }
1285 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
1286 {
1287 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
1288 build_int_cst (TREE_TYPE (*vr0max), 1));
1289 *vr0max = vr1max;
1290 }
1291 else
1292 goto give_up;
1293 }
1294 else if (*vr0type == VR_ANTI_RANGE
1295 && vr1type == VR_RANGE)
1296 /* The result covers everything. */
1297 goto give_up;
1298 else
1299 gcc_unreachable ();
1300 }
1301 else if (cmpmin == -1
1302 && cmpmax == -1
1303 && (operand_less_p (vr1min, *vr0max) == 1
1304 || operand_equal_p (vr1min, *vr0max, 0)))
1305 {
1306 /* [ ( ] ) or [ ]( ) */
1307 if (*vr0type == VR_RANGE
1308 && vr1type == VR_RANGE)
1309 *vr0max = vr1max;
1310 else if (*vr0type == VR_ANTI_RANGE
1311 && vr1type == VR_ANTI_RANGE)
1312 *vr0min = vr1min;
1313 else if (*vr0type == VR_ANTI_RANGE
1314 && vr1type == VR_RANGE)
1315 {
1316 if (TREE_CODE (vr1min) == INTEGER_CST)
1317 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
1318 build_int_cst (TREE_TYPE (vr1min), 1));
1319 else
1320 goto give_up;
1321 }
1322 else if (*vr0type == VR_RANGE
1323 && vr1type == VR_ANTI_RANGE)
1324 {
1325 if (TREE_CODE (*vr0max) == INTEGER_CST)
1326 {
1327 *vr0type = vr1type;
1328 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
1329 build_int_cst (TREE_TYPE (*vr0max), 1));
1330 *vr0max = vr1max;
1331 }
1332 else
1333 goto give_up;
1334 }
1335 else
1336 gcc_unreachable ();
1337 }
1338 else if (cmpmin == 1
1339 && cmpmax == 1
1340 && (operand_less_p (*vr0min, vr1max) == 1
1341 || operand_equal_p (*vr0min, vr1max, 0)))
1342 {
1343 /* ( [ ) ] or ( )[ ] */
1344 if (*vr0type == VR_RANGE
1345 && vr1type == VR_RANGE)
1346 *vr0min = vr1min;
1347 else if (*vr0type == VR_ANTI_RANGE
1348 && vr1type == VR_ANTI_RANGE)
1349 *vr0max = vr1max;
1350 else if (*vr0type == VR_ANTI_RANGE
1351 && vr1type == VR_RANGE)
1352 {
1353 if (TREE_CODE (vr1max) == INTEGER_CST)
1354 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
1355 build_int_cst (TREE_TYPE (vr1max), 1));
1356 else
1357 goto give_up;
1358 }
1359 else if (*vr0type == VR_RANGE
1360 && vr1type == VR_ANTI_RANGE)
1361 {
1362 if (TREE_CODE (*vr0min) == INTEGER_CST)
1363 {
1364 *vr0type = vr1type;
1365 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
1366 build_int_cst (TREE_TYPE (*vr0min), 1));
1367 *vr0min = vr1min;
1368 }
1369 else
1370 goto give_up;
1371 }
1372 else
1373 gcc_unreachable ();
1374 }
1375 else
1376 goto give_up;
1377
1378 return;
1379
1380give_up:
1381 *vr0type = VR_VARYING;
1382 *vr0min = NULL_TREE;
1383 *vr0max = NULL_TREE;
1384}
1385
4ba9fb0a
AH
1386/* Helper for meet operation for value ranges. Given two ranges VR0
1387 and VR1, set VR0 to the union of both ranges. This may not be the
cca78449
AH
1388 smallest possible such range. */
1389
4ba9fb0a
AH
1390void
1391irange::legacy_union (irange *vr0, const irange *vr1)
cca78449 1392{
ea6da7f5
AH
1393 gcc_checking_assert (vr0->legacy_mode_p ());
1394 gcc_checking_assert (vr1->legacy_mode_p ());
1395
cca78449
AH
1396 /* VR0 has the resulting range if VR1 is undefined or VR0 is varying. */
1397 if (vr1->undefined_p ()
1398 || vr0->varying_p ())
4ba9fb0a 1399 return;
cca78449
AH
1400
1401 /* VR1 has the resulting range if VR0 is undefined or VR1 is varying. */
4ba9fb0a
AH
1402 if (vr0->undefined_p ())
1403 {
ea6da7f5 1404 vr0->set (vr1->min (), vr1->max (), vr1->kind ());
4ba9fb0a
AH
1405 return;
1406 }
ea6da7f5 1407
4ba9fb0a
AH
1408 if (vr1->varying_p ())
1409 {
1410 vr0->set_varying (vr1->type ());
1411 return;
1412 }
cca78449
AH
1413
1414 value_range_kind vr0kind = vr0->kind ();
1415 tree vr0min = vr0->min ();
1416 tree vr0max = vr0->max ();
ea6da7f5
AH
1417
1418 union_ranges (&vr0kind, &vr0min, &vr0max,
1419 vr1->kind (), vr1->min (), vr1->max ());
cca78449 1420
cca78449 1421 if (vr0kind == VR_UNDEFINED)
4ba9fb0a 1422 vr0->set_undefined ();
cca78449 1423 else if (vr0kind == VR_VARYING)
cca78449 1424 {
4ba9fb0a
AH
1425 /* Failed to find an efficient meet. Before giving up and
1426 setting the result to VARYING, see if we can at least derive
1427 a non-zero range. */
1428 if (range_includes_zero_p (vr0) == 0
1429 && range_includes_zero_p (vr1) == 0)
1430 vr0->set_nonzero (vr0->type ());
1431 else
1432 vr0->set_varying (vr0->type ());
cca78449 1433 }
4ba9fb0a
AH
1434 else
1435 vr0->set (vr0min, vr0max, vr0kind);
cca78449
AH
1436}
1437
1438/* Meet operation for value ranges. Given two value ranges VR0 and
1439 VR1, store in VR0 a range that contains both VR0 and VR1. This
1440 may not be the smallest possible such range. */
1441
1442void
4ba9fb0a 1443irange::union_ (const irange *other)
cca78449 1444{
4ba9fb0a 1445 if (legacy_mode_p ())
cca78449 1446 {
ea6da7f5
AH
1447 if (!other->legacy_mode_p ())
1448 {
1449 int_range<1> tmp = *other;
1450 legacy_union (this, &tmp);
1451 return;
1452 }
4ba9fb0a
AH
1453 if (dump_file && (dump_flags & TDF_DETAILS))
1454 {
1455 fprintf (dump_file, "Meeting\n ");
1456 dump_value_range (dump_file, this);
1457 fprintf (dump_file, "\nand\n ");
1458 dump_value_range (dump_file, other);
1459 fprintf (dump_file, "\n");
1460 }
cca78449 1461
4ba9fb0a 1462 legacy_union (this, other);
cca78449 1463
4ba9fb0a
AH
1464 if (dump_file && (dump_flags & TDF_DETAILS))
1465 {
1466 fprintf (dump_file, "to\n ");
1467 dump_value_range (dump_file, this);
1468 fprintf (dump_file, "\n");
1469 }
1470 return;
1471 }
1472
1473 if (other->legacy_mode_p ())
cca78449 1474 {
ea6da7f5 1475 int_range<2> wider = *other;
4ba9fb0a 1476 irange_union (wider);
cca78449 1477 }
4ba9fb0a
AH
1478 else
1479 irange_union (*other);
cca78449
AH
1480}
1481
cca78449 1482void
4ba9fb0a 1483irange::intersect (const irange *other)
cca78449 1484{
4ba9fb0a
AH
1485 if (legacy_mode_p ())
1486 {
ea6da7f5
AH
1487 if (!other->legacy_mode_p ())
1488 {
1489 int_range<1> tmp = *other;
1490 legacy_intersect (this, &tmp);
1491 return;
1492 }
4ba9fb0a
AH
1493 if (dump_file && (dump_flags & TDF_DETAILS))
1494 {
1495 fprintf (dump_file, "Intersecting\n ");
1496 dump_value_range (dump_file, this);
1497 fprintf (dump_file, "\nand\n ");
1498 dump_value_range (dump_file, other);
1499 fprintf (dump_file, "\n");
1500 }
1501
1502 legacy_intersect (this, other);
1503
1504 if (dump_file && (dump_flags & TDF_DETAILS))
1505 {
1506 fprintf (dump_file, "to\n ");
1507 dump_value_range (dump_file, this);
1508 fprintf (dump_file, "\n");
1509 }
1510 return;
1511 }
1512
1513 if (other->legacy_mode_p ())
1514 {
1515 int_range<2> wider;
1516 wider = *other;
1517 irange_intersect (wider);
1518 }
1519 else
1520 irange_intersect (*other);
cca78449
AH
1521}
1522
4ba9fb0a
AH
1523// union_ for multi-ranges.
1524
cca78449 1525void
4ba9fb0a 1526irange::irange_union (const irange &r)
cca78449 1527{
4ba9fb0a
AH
1528 gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ());
1529
1530 if (r.undefined_p () || varying_p ())
1531 return;
1532
1533 if (undefined_p () || r.varying_p ())
cca78449 1534 {
4ba9fb0a
AH
1535 operator= (r);
1536 return;
cca78449
AH
1537 }
1538
4ba9fb0a
AH
1539 // Do not worry about merging and such by reserving twice as many
1540 // pairs as needed, and then simply sort the 2 ranges into this
1541 // intermediate form.
1542 //
1543 // The intermediate result will have the property that the beginning
1544 // of each range is <= the beginning of the next range. There may
1545 // be overlapping ranges at this point. I.e. this would be valid
1546 // [-20, 10], [-10, 0], [0, 20], [40, 90] as it satisfies this
1547 // contraint : -20 < -10 < 0 < 40. When the range is rebuilt into r,
1548 // the merge is performed.
1549 //
1550 // [Xi,Yi]..[Xn,Yn] U [Xj,Yj]..[Xm,Ym] --> [Xk,Yk]..[Xp,Yp]
1551 tree ttype = r.type ();
1552 signop sign = TYPE_SIGN (ttype);
1553
1554 auto_vec<tree, 20> res;
1555 wide_int u1 ;
1556 wi::overflow_type ovf;
1557 unsigned i = 0, j = 0, k = 0;
1558
1559 while (i < m_num_ranges * 2 && j < r.m_num_ranges * 2)
1560 {
1561 // lower of Xi and Xj is the lowest point.
1562 if (wi::le_p (wi::to_wide (m_base[i]), wi::to_wide (r.m_base[j]), sign))
1563 {
1564 res.safe_push (m_base[i]);
1565 res.safe_push (m_base[i + 1]);
1566 k += 2;
1567 i += 2;
1568 }
1569 else
1570 {
1571 res.safe_push (r.m_base[j]);
1572 res.safe_push (r.m_base[j + 1]);
1573 k += 2;
1574 j += 2;
1575 }
1576 }
1577 for ( ; i < m_num_ranges * 2; i += 2)
1578 {
1579 res.safe_push (m_base[i]);
1580 res.safe_push (m_base[i + 1]);
1581 k += 2;
1582 }
1583 for ( ; j < r.m_num_ranges * 2; j += 2)
1584 {
1585 res.safe_push (r.m_base[j]);
1586 res.safe_push (r.m_base[j + 1]);
1587 k += 2;
1588 }
cca78449 1589
4ba9fb0a
AH
1590 // Now normalize the vector removing any overlaps.
1591 i = 2;
1592 int prec = TYPE_PRECISION (ttype);
1593 wide_int max_val = wi::max_value (prec, sign);
1594 for (j = 2; j < k ; j += 2)
cca78449 1595 {
4ba9fb0a
AH
1596 wide_int val_im1 = wi::to_wide (res[i - 1]);
1597 if (val_im1 == max_val)
1598 break;
1599 u1 = wi::add (val_im1, 1, sign, &ovf);
1600
1601 // Overflow indicates we are at MAX already.
1602 // A wide int bug requires the previous max_val check
1603 // trigger: gcc.c-torture/compile/pr80443.c with -O3
1604 if (ovf == wi::OVF_OVERFLOW)
1605 break;
1606
1607 wide_int val_j = wi::to_wide (res[j]);
1608 wide_int val_jp1 = wi::to_wide (res[j+1]);
1609 // Current upper+1 is >= lower bound next pair, then we merge ranges.
1610 if (wi::ge_p (u1, val_j, sign))
1611 {
1612 // New upper bounds is greater of current or the next one.
1613 if (wi::gt_p (val_jp1, val_im1, sign))
1614 res [i - 1] = res[j + 1];
1615 }
1616 else
1617 {
1618 // This is a new distinct range, but no point in copying it
1619 // if it is already in the right place.
1620 if (i != j)
1621 {
1622 res[i++] = res[j];
1623 res[i++] = res[j + 1];
1624 }
1625 else
1626 i += 2;
1627 }
cca78449 1628 }
4ba9fb0a
AH
1629
1630 // At this point, the vector should have i ranges, none overlapping.
1631 // Now it simply needs to be copied, and if there are too many
1632 // ranges, merge some. We wont do any analysis as to what the
1633 // "best" merges are, simply combine the final ranges into one.
1634 if (i > m_max_ranges * 2)
1635 {
1636 res[m_max_ranges * 2 - 1] = res[i - 1];
1637 i = m_max_ranges * 2;
1638 }
1639
1640 for (j = 0; j < i ; j++)
1641 m_base[j] = res [j];
1642 m_num_ranges = i / 2;
1643
1644 if (flag_checking)
1645 verify_range ();
cca78449
AH
1646}
1647
4ba9fb0a 1648// intersect for multi-ranges.
cca78449
AH
1649
1650void
4ba9fb0a
AH
1651irange::irange_intersect (const irange &r)
1652{
1653 gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ());
1654
1655 if (undefined_p () || r.varying_p ())
1656 return;
1657 if (r.undefined_p ())
1658 {
1659 set_undefined ();
1660 return;
1661 }
1662 if (varying_p ())
1663 {
1664 operator= (r);
1665 return;
1666 }
1667
1668 signop sign = TYPE_SIGN (TREE_TYPE(m_base[0]));
1669 unsigned bld_pair = 0;
1670 unsigned bld_lim = m_max_ranges;
c5a6c223 1671 int_range_max r2 (*this);
4ba9fb0a
AH
1672 unsigned r2_lim = r2.num_pairs ();
1673 unsigned i2 = 0;
1674 for (unsigned i = 0; i < r.num_pairs (); )
1675 {
1676 // If r1's upper is < r2's lower, we can skip r1's pair.
1677 tree ru = r.m_base[i * 2 + 1];
1678 tree r2l = r2.m_base[i2 * 2];
1679 if (wi::lt_p (wi::to_wide (ru), wi::to_wide (r2l), sign))
1680 {
1681 i++;
1682 continue;
1683 }
1684 // Likewise, skip r2's pair if its excluded.
1685 tree r2u = r2.m_base[i2 * 2 + 1];
1686 tree rl = r.m_base[i * 2];
1687 if (wi::lt_p (wi::to_wide (r2u), wi::to_wide (rl), sign))
1688 {
1689 i2++;
1690 if (i2 < r2_lim)
1691 continue;
1692 // No more r2, break.
1693 break;
1694 }
1695
1696 // Must be some overlap. Find the highest of the lower bounds,
1697 // and set it, unless the build limits lower bounds is already
1698 // set.
1699 if (bld_pair < bld_lim)
1700 {
1701 if (wi::ge_p (wi::to_wide (rl), wi::to_wide (r2l), sign))
1702 m_base[bld_pair * 2] = rl;
1703 else
1704 m_base[bld_pair * 2] = r2l;
1705 }
1706 else
1707 // Decrease and set a new upper.
1708 bld_pair--;
1709
1710 // ...and choose the lower of the upper bounds.
1711 if (wi::le_p (wi::to_wide (ru), wi::to_wide (r2u), sign))
1712 {
1713 m_base[bld_pair * 2 + 1] = ru;
1714 bld_pair++;
1715 // Move past the r1 pair and keep trying.
1716 i++;
1717 continue;
1718 }
1719 else
1720 {
1721 m_base[bld_pair * 2 + 1] = r2u;
1722 bld_pair++;
1723 i2++;
1724 if (i2 < r2_lim)
1725 continue;
1726 // No more r2, break.
1727 break;
1728 }
1729 // r2 has the higher lower bound.
1730 }
1731
1732 // At the exit of this loop, it is one of 2 things:
1733 // ran out of r1, or r2, but either means we are done.
1734 m_num_ranges = bld_pair;
1735 if (flag_checking)
1736 verify_range ();
1737}
1738
2118438f
AH
1739// Signed 1-bits are strange. You can't subtract 1, because you can't
1740// represent the number 1. This works around that for the invert routine.
1741
4ba9fb0a
AH
1742static wide_int inline
1743subtract_one (const wide_int &x, tree type, wi::overflow_type &overflow)
cca78449 1744{
4ba9fb0a 1745 if (TYPE_SIGN (type) == SIGNED)
2118438f 1746 return wi::add (x, -1, SIGNED, &overflow);
4ba9fb0a
AH
1747 else
1748 return wi::sub (x, 1, UNSIGNED, &overflow);
cca78449
AH
1749}
1750
2118438f
AH
1751// The analogous function for adding 1.
1752
1753static wide_int inline
1754add_one (const wide_int &x, tree type, wi::overflow_type &overflow)
1755{
1756 if (TYPE_SIGN (type) == SIGNED)
1757 return wi::sub (x, -1, SIGNED, &overflow);
1758 else
1759 return wi::add (x, 1, UNSIGNED, &overflow);
1760}
1761
1762// Return the inverse of a range.
cca78449
AH
1763
1764void
4ba9fb0a 1765irange::invert ()
cca78449 1766{
4ba9fb0a
AH
1767 if (legacy_mode_p ())
1768 {
1769 // We can't just invert VR_RANGE and VR_ANTI_RANGE because we may
1770 // create non-canonical ranges. Use the constructors instead.
1771 if (m_kind == VR_RANGE)
1772 *this = value_range (min (), max (), VR_ANTI_RANGE);
1773 else if (m_kind == VR_ANTI_RANGE)
1774 *this = value_range (min (), max ());
1775 else
1776 gcc_unreachable ();
1777 return;
1778 }
1779
1780 gcc_assert (!undefined_p () && !varying_p ());
1781
1782 // We always need one more set of bounds to represent an inverse, so
1783 // if we're at the limit, we can't properly represent things.
1784 //
1785 // For instance, to represent the inverse of a 2 sub-range set
1786 // [5, 10][20, 30], we would need a 3 sub-range set
1787 // [-MIN, 4][11, 19][31, MAX].
1788 //
1789 // In this case, return the most conservative thing.
1790 //
1791 // However, if any of the extremes of the range are -MIN/+MAX, we
1792 // know we will not need an extra bound. For example:
1793 //
1794 // INVERT([-MIN,20][30,40]) => [21,29][41,+MAX]
1795 // INVERT([-MIN,20][30,MAX]) => [21,29]
1796 tree ttype = type ();
1797 unsigned prec = TYPE_PRECISION (ttype);
1798 signop sign = TYPE_SIGN (ttype);
1799 wide_int type_min = wi::min_value (prec, sign);
1800 wide_int type_max = wi::max_value (prec, sign);
1801 if (m_num_ranges == m_max_ranges
1802 && lower_bound () != type_min
1803 && upper_bound () != type_max)
1804 {
1805 m_base[1] = wide_int_to_tree (ttype, type_max);
1806 m_num_ranges = 1;
1807 return;
1808 }
1809 // The algorithm is as follows. To calculate INVERT ([a,b][c,d]), we
1810 // generate [-MIN, a-1][b+1, c-1][d+1, MAX].
1811 //
1812 // If there is an over/underflow in the calculation for any
1813 // sub-range, we eliminate that subrange. This allows us to easily
1814 // calculate INVERT([-MIN, 5]) with: [-MIN, -MIN-1][6, MAX]. And since
1815 // we eliminate the underflow, only [6, MAX] remains.
1816 unsigned i = 0;
1817 wi::overflow_type ovf;
1818 // Construct leftmost range.
c5a6c223 1819 int_range_max orig_range (*this);
4ba9fb0a
AH
1820 unsigned nitems = 0;
1821 wide_int tmp;
1822 // If this is going to underflow on the MINUS 1, don't even bother
1823 // checking. This also handles subtracting one from an unsigned 0,
1824 // which doesn't set the underflow bit.
1825 if (type_min != orig_range.lower_bound ())
1826 {
1827 m_base[nitems++] = wide_int_to_tree (ttype, type_min);
1828 tmp = subtract_one (orig_range.lower_bound (), ttype, ovf);
1829 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
1830 if (ovf)
1831 nitems = 0;
1832 }
1833 i++;
1834 // Construct middle ranges if applicable.
1835 if (orig_range.num_pairs () > 1)
1836 {
1837 unsigned j = i;
1838 for (; j < (orig_range.num_pairs () * 2) - 1; j += 2)
1839 {
1840 // The middle ranges cannot have MAX/MIN, so there's no need
1841 // to check for unsigned overflow on the +1 and -1 here.
1842 tmp = wi::add (wi::to_wide (orig_range.m_base[j]), 1, sign, &ovf);
1843 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
1844 tmp = subtract_one (wi::to_wide (orig_range.m_base[j + 1]),
1845 ttype, ovf);
1846 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
1847 if (ovf)
1848 nitems -= 2;
1849 }
1850 i = j;
1851 }
1852 // Construct rightmost range.
1853 //
1854 // However, if this will overflow on the PLUS 1, don't even bother.
1855 // This also handles adding one to an unsigned MAX, which doesn't
1856 // set the overflow bit.
1857 if (type_max != wi::to_wide (orig_range.m_base[i]))
1858 {
2118438f 1859 tmp = add_one (wi::to_wide (orig_range.m_base[i]), ttype, ovf);
4ba9fb0a
AH
1860 m_base[nitems++] = wide_int_to_tree (ttype, tmp);
1861 m_base[nitems++] = wide_int_to_tree (ttype, type_max);
1862 if (ovf)
1863 nitems -= 2;
1864 }
1865 m_num_ranges = nitems / 2;
1866
1867 if (flag_checking)
1868 verify_range ();
1869}
1870
1871static void
1872dump_bound_with_infinite_markers (FILE *file, tree bound)
1873{
1874 tree type = TREE_TYPE (bound);
3d3470e2
AH
1875 wide_int type_min = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1876 wide_int type_max = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1877
4ba9fb0a
AH
1878 if (INTEGRAL_TYPE_P (type)
1879 && !TYPE_UNSIGNED (type)
3d3470e2
AH
1880 && TREE_CODE (bound) == INTEGER_CST
1881 && wi::to_wide (bound) == type_min
4ba9fb0a
AH
1882 && TYPE_PRECISION (type) != 1)
1883 fprintf (file, "-INF");
3d3470e2
AH
1884 else if (TREE_CODE (bound) == INTEGER_CST
1885 && wi::to_wide (bound) == type_max
4ba9fb0a
AH
1886 && TYPE_PRECISION (type) != 1)
1887 fprintf (file, "+INF");
cca78449 1888 else
4ba9fb0a 1889 print_generic_expr (file, bound);
cca78449
AH
1890}
1891
1892void
4ba9fb0a 1893irange::dump (FILE *file) const
cca78449
AH
1894{
1895 if (undefined_p ())
cca78449 1896 {
4ba9fb0a
AH
1897 fprintf (file, "UNDEFINED");
1898 return;
1899 }
1900 print_generic_expr (file, type ());
1901 fprintf (file, " ");
1902 if (varying_p ())
1903 {
1904 fprintf (file, "VARYING");
1905 return;
1906 }
1907 if (legacy_mode_p ())
1908 {
cca78449 1909 fprintf (file, "%s[", (m_kind == VR_ANTI_RANGE) ? "~" : "");
4ba9fb0a 1910 dump_bound_with_infinite_markers (file, min ());
cca78449 1911 fprintf (file, ", ");
4ba9fb0a 1912 dump_bound_with_infinite_markers (file, max ());
cca78449 1913 fprintf (file, "]");
4ba9fb0a 1914 return;
cca78449 1915 }
4ba9fb0a 1916 for (unsigned i = 0; i < m_num_ranges; ++i)
cca78449 1917 {
4ba9fb0a
AH
1918 tree lb = m_base[i * 2];
1919 tree ub = m_base[i * 2 + 1];
1920 fprintf (file, "[");
1921 dump_bound_with_infinite_markers (file, lb);
1922 fprintf (file, ", ");
1923 dump_bound_with_infinite_markers (file, ub);
1924 fprintf (file, "]");
cca78449 1925 }
cca78449
AH
1926}
1927
1928void
4ba9fb0a 1929dump_value_range (FILE *file, const irange *vr)
cca78449 1930{
4ba9fb0a 1931 vr->dump (file);
cca78449
AH
1932}
1933
4ba9fb0a
AH
1934DEBUG_FUNCTION void
1935debug (const irange *vr)
cca78449 1936{
4ba9fb0a
AH
1937 dump_value_range (stderr, vr);
1938 fprintf (stderr, "\n");
1939}
1940
1941DEBUG_FUNCTION void
1942debug (const irange &vr)
1943{
1944 debug (&vr);
cca78449
AH
1945}
1946
1947DEBUG_FUNCTION void
1948debug (const value_range *vr)
1949{
1950 dump_value_range (stderr, vr);
4ba9fb0a 1951 fprintf (stderr, "\n");
cca78449
AH
1952}
1953
1954DEBUG_FUNCTION void
1955debug (const value_range &vr)
1956{
1957 dump_value_range (stderr, &vr);
4ba9fb0a 1958 fprintf (stderr, "\n");
cca78449
AH
1959}
1960
1961/* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1962 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1963 false otherwise. If *AR can be represented with a single range
1964 *VR1 will be VR_UNDEFINED. */
1965
1966bool
1967ranges_from_anti_range (const value_range *ar,
1968 value_range *vr0, value_range *vr1)
1969{
1970 tree type = ar->type ();
1971
1972 vr0->set_undefined ();
1973 vr1->set_undefined ();
1974
1975 /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U
1976 [A+1, +INF]. Not sure if this helps in practice, though. */
1977
1978 if (ar->kind () != VR_ANTI_RANGE
1979 || TREE_CODE (ar->min ()) != INTEGER_CST
1980 || TREE_CODE (ar->max ()) != INTEGER_CST
1981 || !vrp_val_min (type)
1982 || !vrp_val_max (type))
1983 return false;
1984
1985 if (tree_int_cst_lt (vrp_val_min (type), ar->min ()))
1986 vr0->set (vrp_val_min (type),
1987 wide_int_to_tree (type, wi::to_wide (ar->min ()) - 1));
1988 if (tree_int_cst_lt (ar->max (), vrp_val_max (type)))
1989 vr1->set (wide_int_to_tree (type, wi::to_wide (ar->max ()) + 1),
1990 vrp_val_max (type));
1991 if (vr0->undefined_p ())
1992 {
1993 *vr0 = *vr1;
1994 vr1->set_undefined ();
1995 }
1996
1997 return !vr0->undefined_p ();
1998}
1999
2000bool
4ba9fb0a 2001range_has_numeric_bounds_p (const irange *vr)
cca78449 2002{
4ba9fb0a 2003 return (!vr->undefined_p ()
cca78449
AH
2004 && TREE_CODE (vr->min ()) == INTEGER_CST
2005 && TREE_CODE (vr->max ()) == INTEGER_CST);
2006}
2007
cca78449
AH
2008/* Return whether VAL is equal to the maximum value of its type.
2009 We can't do a simple equality comparison with TYPE_MAX_VALUE because
2010 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
2011 is not == to the integer constant with the same value in the type. */
2012
2013bool
2014vrp_val_is_max (const_tree val)
2015{
2016 tree type_max = vrp_val_max (TREE_TYPE (val));
2017 return (val == type_max
2018 || (type_max != NULL_TREE
2019 && operand_equal_p (val, type_max, 0)));
2020}
2021
2022/* Return whether VAL is equal to the minimum value of its type. */
2023
2024bool
2025vrp_val_is_min (const_tree val)
2026{
2027 tree type_min = vrp_val_min (TREE_TYPE (val));
2028 return (val == type_min
2029 || (type_min != NULL_TREE
2030 && operand_equal_p (val, type_min, 0)));
2031}
2032
2033/* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
2034
2035bool
2036vrp_operand_equal_p (const_tree val1, const_tree val2)
2037{
2038 if (val1 == val2)
2039 return true;
2040 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
2041 return false;
2042 return true;
2043}
4ba9fb0a
AH
2044
2045#define DEFINE_INT_RANGE_GC_STUBS(N) \
2046 void \
2047 gt_pch_nx (int_range<N> *&x) \
2048 { \
2049 for (unsigned i = 0; i < N; ++i) \
2050 { \
2051 gt_pch_nx (x->m_ranges[i * 2]); \
2052 gt_pch_nx (x->m_ranges[i * 2 + 1]); \
2053 } \
2054 } \
2055 \
2056 void \
2057 gt_ggc_mx (int_range<N> *&x) \
2058 { \
2059 for (unsigned i = 0; i < N; ++i) \
2060 { \
2061 gt_ggc_mx (x->m_ranges[i * 2]); \
2062 gt_ggc_mx (x->m_ranges[i * 2 + 1]); \
2063 } \
2064 }
2065
2066#define DEFINE_INT_RANGE_INSTANCE(N) \
2067 template int_range<N>::int_range(tree, tree, value_range_kind); \
2068 template int_range<N>::int_range(tree_node *, \
2069 const wide_int &, \
2070 const wide_int &, \
2071 value_range_kind); \
2072 template int_range<N>::int_range(tree); \
2073 template int_range<N>::int_range(const irange &); \
2074 template int_range<N>::int_range(const int_range &); \
2075 template int_range<N>& int_range<N>::operator= (const int_range &);
2076
2077DEFINE_INT_RANGE_INSTANCE(1)
2078DEFINE_INT_RANGE_INSTANCE(2)
2079DEFINE_INT_RANGE_INSTANCE(3)
2080DEFINE_INT_RANGE_INSTANCE(255)
2081DEFINE_INT_RANGE_GC_STUBS(1)
b5cff0db
AH
2082
2083#if CHECKING_P
2084#include "selftest.h"
2085
2086namespace selftest
2087{
2088#define INT(N) build_int_cst (integer_type_node, (N))
2089#define UINT(N) build_int_cstu (unsigned_type_node, (N))
2090#define UINT128(N) build_int_cstu (u128_type, (N))
2091#define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
2092#define SCHAR(N) build_int_cst (signed_char_type_node, (N))
2093
2094static int_range<3>
2095build_range3 (int a, int b, int c, int d, int e, int f)
2096{
2097 int_range<3> i1 (INT (a), INT (b));
2098 int_range<3> i2 (INT (c), INT (d));
2099 int_range<3> i3 (INT (e), INT (f));
2100 i1.union_ (i2);
2101 i1.union_ (i3);
2102 return i1;
2103}
2104
2105static void
2106range_tests_irange3 ()
2107{
2108 typedef int_range<3> int_range3;
2109 int_range3 r0, r1, r2;
2110 int_range3 i1, i2, i3;
2111
2112 // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20].
2113 r0 = int_range3 (INT (10), INT (20));
2114 r1 = int_range3 (INT (5), INT (8));
2115 r0.union_ (r1);
2116 r1 = int_range3 (INT (1), INT (3));
2117 r0.union_ (r1);
2118 ASSERT_TRUE (r0 == build_range3 (1, 3, 5, 8, 10, 20));
2119
2120 // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20].
2121 r1 = int_range3 (INT (-5), INT (0));
2122 r0.union_ (r1);
2123 ASSERT_TRUE (r0 == build_range3 (-5, 3, 5, 8, 10, 20));
2124
2125 // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60].
2126 r1 = int_range3 (INT (50), INT (60));
2127 r0 = int_range3 (INT (10), INT (20));
2128 r0.union_ (int_range3 (INT (30), INT (40)));
2129 r0.union_ (r1);
2130 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
2131 // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80].
2132 r1 = int_range3 (INT (70), INT (80));
2133 r0.union_ (r1);
2134
2135 r2 = build_range3 (10, 20, 30, 40, 50, 60);
2136 r2.union_ (int_range3 (INT (70), INT (80)));
2137 ASSERT_TRUE (r0 == r2);
2138
2139 // [10,20][30,40][50,60] U [6,35] => [6,40][50,60].
2140 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2141 r1 = int_range3 (INT (6), INT (35));
2142 r0.union_ (r1);
2143 r1 = int_range3 (INT (6), INT (40));
2144 r1.union_ (int_range3 (INT (50), INT (60)));
2145 ASSERT_TRUE (r0 == r1);
2146
2147 // [10,20][30,40][50,60] U [6,60] => [6,60].
2148 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2149 r1 = int_range3 (INT (6), INT (60));
2150 r0.union_ (r1);
2151 ASSERT_TRUE (r0 == int_range3 (INT (6), INT (60)));
2152
2153 // [10,20][30,40][50,60] U [6,70] => [6,70].
2154 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2155 r1 = int_range3 (INT (6), INT (70));
2156 r0.union_ (r1);
2157 ASSERT_TRUE (r0 == int_range3 (INT (6), INT (70)));
2158
2159 // [10,20][30,40][50,60] U [35,70] => [10,20][30,70].
2160 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2161 r1 = int_range3 (INT (35), INT (70));
2162 r0.union_ (r1);
2163 r1 = int_range3 (INT (10), INT (20));
2164 r1.union_ (int_range3 (INT (30), INT (70)));
2165 ASSERT_TRUE (r0 == r1);
2166
2167 // [10,20][30,40][50,60] U [15,35] => [10,40][50,60].
2168 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2169 r1 = int_range3 (INT (15), INT (35));
2170 r0.union_ (r1);
2171 r1 = int_range3 (INT (10), INT (40));
2172 r1.union_ (int_range3 (INT (50), INT (60)));
2173 ASSERT_TRUE (r0 == r1);
2174
2175 // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60].
2176 r0 = build_range3 (10, 20, 30, 40, 50, 60);
2177 r1 = int_range3 (INT (35), INT (35));
2178 r0.union_ (r1);
2179 ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
2180}
2181
2182static void
2183range_tests_int_range_max ()
2184{
2185 int_range_max big;
2186 unsigned int nrange;
2187
2188 // Build a huge multi-range range.
2189 for (nrange = 0; nrange < 50; ++nrange)
2190 {
2191 int_range<1> tmp (INT (nrange*10), INT (nrange*10 + 5));
2192 big.union_ (tmp);
2193 }
2194 ASSERT_TRUE (big.num_pairs () == nrange);
2195
2196 // Verify that we can copy it without loosing precision.
2197 int_range_max copy (big);
2198 ASSERT_TRUE (copy.num_pairs () == nrange);
2199
2200 // Inverting it should produce one more sub-range.
2201 big.invert ();
2202 ASSERT_TRUE (big.num_pairs () == nrange + 1);
2203
2204 int_range<1> tmp (INT (5), INT (37));
2205 big.intersect (tmp);
2206 ASSERT_TRUE (big.num_pairs () == 4);
2207
2208 // Test that [10,10][20,20] does NOT contain 15.
2209 {
2210 int_range_max i1 (build_int_cst (integer_type_node, 10),
2211 build_int_cst (integer_type_node, 10));
2212 int_range_max i2 (build_int_cst (integer_type_node, 20),
2213 build_int_cst (integer_type_node, 20));
2214 i1.union_ (i2);
2215 ASSERT_FALSE (i1.contains_p (build_int_cst (integer_type_node, 15)));
2216 }
2217}
2218
2219static void
2220range_tests_legacy ()
2221{
2222 // Test truncating copy to int_range<1>.
2223 int_range<3> big = build_range3 (10, 20, 30, 40, 50, 60);
2224 int_range<1> small = big;
2225 ASSERT_TRUE (small == int_range<1> (INT (10), INT (60)));
2226
2227 // Test truncating copy to int_range<2>.
2228 int_range<2> medium = big;
2229 ASSERT_TRUE (!medium.undefined_p ());
2230
2231 // Test that a truncating copy of [MIN,20][22,40][80,MAX]
2232 // ends up as a conservative anti-range of ~[21,21].
2233 big = int_range<3> (vrp_val_min (integer_type_node), INT (20));
2234 big.union_ (int_range<1> (INT (22), INT (40)));
2235 big.union_ (int_range<1> (INT (80), vrp_val_max (integer_type_node)));
2236 small = big;
2237 ASSERT_TRUE (small == int_range<1> (INT (21), INT (21), VR_ANTI_RANGE));
2238
2239 // Copying a legacy symbolic to an int_range should normalize the
2240 // symbolic at copy time.
2241 {
2242 tree ssa = make_ssa_name (integer_type_node);
2243 value_range legacy_range (ssa, INT (25));
2244 int_range<2> copy = legacy_range;
2245 ASSERT_TRUE (copy == int_range<2> (vrp_val_min (integer_type_node),
2246 INT (25)));
2247
2248 // Test that copying ~[abc_23, abc_23] to a multi-range yields varying.
2249 legacy_range = value_range (ssa, ssa, VR_ANTI_RANGE);
2250 copy = legacy_range;
2251 ASSERT_TRUE (copy.varying_p ());
2252 }
2253}
2254
3d3470e2
AH
2255// Simulate -fstrict-enums where the domain of a type is less than the
2256// underlying type.
2257
2258static void
2259range_tests_strict_enum ()
2260{
2261 // The enum can only hold [0, 3].
2262 tree rtype = copy_node (unsigned_type_node);
2263 TYPE_MIN_VALUE (rtype) = build_int_cstu (rtype, 0);
2264 TYPE_MAX_VALUE (rtype) = build_int_cstu (rtype, 3);
2265
2266 // Test that even though vr1 covers the strict enum domain ([0, 3]),
2267 // it does not cover the domain of the underlying type.
2268 int_range<1> vr1 (build_int_cstu (rtype, 0), build_int_cstu (rtype, 1));
2269 int_range<1> vr2 (build_int_cstu (rtype, 2), build_int_cstu (rtype, 3));
2270 vr1.union_ (vr2);
2271 ASSERT_TRUE (vr1 == int_range<1> (build_int_cstu (rtype, 0),
2272 build_int_cstu (rtype, 3)));
2273 ASSERT_FALSE (vr1.varying_p ());
2274
2275 // Test that copying to a multi-range does not change things.
2276 int_range<2> ir1 (vr1);
2277 ASSERT_TRUE (ir1 == vr1);
2278 ASSERT_FALSE (ir1.varying_p ());
2279
2280 // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3].
2281 vr1 = int_range<1> (TYPE_MIN_VALUE (rtype), TYPE_MAX_VALUE (rtype));
2282 ir1 = vr1;
2283 ASSERT_TRUE (ir1 == vr1);
2284 ASSERT_FALSE (ir1.varying_p ());
2285}
2286
b5cff0db
AH
2287static void
2288range_tests_misc ()
2289{
2290 tree u128_type = build_nonstandard_integer_type (128, /*unsigned=*/1);
2291 int_range<1> i1, i2, i3;
2292 int_range<1> r0, r1, rold;
2293
2294 // Test 1-bit signed integer union.
2295 // [-1,-1] U [0,0] = VARYING.
2296 tree one_bit_type = build_nonstandard_integer_type (1, 0);
2297 tree one_bit_min = vrp_val_min (one_bit_type);
2298 tree one_bit_max = vrp_val_max (one_bit_type);
2299 {
2300 int_range<2> min (one_bit_min, one_bit_min);
2301 int_range<2> max (one_bit_max, one_bit_max);
2302 max.union_ (min);
2303 ASSERT_TRUE (max.varying_p ());
2304 }
2305
2306 // Test inversion of 1-bit signed integers.
2307 {
2308 int_range<2> min (one_bit_min, one_bit_min);
2309 int_range<2> max (one_bit_max, one_bit_max);
2310 int_range<2> t;
2311 t = min;
2312 t.invert ();
2313 ASSERT_TRUE (t == max);
2314 t = max;
2315 t.invert ();
2316 ASSERT_TRUE (t == min);
2317 }
2318
2319 // Test that NOT(255) is [0..254] in 8-bit land.
2320 int_range<1> not_255 (UCHAR (255), UCHAR (255), VR_ANTI_RANGE);
2321 ASSERT_TRUE (not_255 == int_range<1> (UCHAR (0), UCHAR (254)));
2322
2323 // Test that NOT(0) is [1..255] in 8-bit land.
2324 int_range<1> not_zero = range_nonzero (unsigned_char_type_node);
2325 ASSERT_TRUE (not_zero == int_range<1> (UCHAR (1), UCHAR (255)));
2326
2327 // Check that [0,127][0x..ffffff80,0x..ffffff]
2328 // => ~[128, 0x..ffffff7f].
2329 r0 = int_range<1> (UINT128 (0), UINT128 (127));
2330 tree high = build_minus_one_cst (u128_type);
2331 // low = -1 - 127 => 0x..ffffff80.
2332 tree low = fold_build2 (MINUS_EXPR, u128_type, high, UINT128(127));
2333 r1 = int_range<1> (low, high); // [0x..ffffff80, 0x..ffffffff]
2334 // r0 = [0,127][0x..ffffff80,0x..fffffff].
2335 r0.union_ (r1);
2336 // r1 = [128, 0x..ffffff7f].
2337 r1 = int_range<1> (UINT128(128),
2338 fold_build2 (MINUS_EXPR, u128_type,
2339 build_minus_one_cst (u128_type),
2340 UINT128(128)));
2341 r0.invert ();
2342 ASSERT_TRUE (r0 == r1);
2343
2344 r0.set_varying (integer_type_node);
2345 tree minint = wide_int_to_tree (integer_type_node, r0.lower_bound ());
2346 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
2347
2348 r0.set_varying (short_integer_type_node);
2349
2350 r0.set_varying (unsigned_type_node);
2351 tree maxuint = wide_int_to_tree (unsigned_type_node, r0.upper_bound ());
2352
2353 // Check that ~[0,5] => [6,MAX] for unsigned int.
2354 r0 = int_range<1> (UINT (0), UINT (5));
2355 r0.invert ();
2356 ASSERT_TRUE (r0 == int_range<1> (UINT(6), maxuint));
2357
2358 // Check that ~[10,MAX] => [0,9] for unsigned int.
2359 r0 = int_range<1> (UINT(10), maxuint);
2360 r0.invert ();
2361 ASSERT_TRUE (r0 == int_range<1> (UINT (0), UINT (9)));
2362
2363 // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers.
2364 r0 = int_range<1> (UINT128 (0), UINT128 (5), VR_ANTI_RANGE);
2365 r1 = int_range<1> (UINT128(6), build_minus_one_cst (u128_type));
2366 ASSERT_TRUE (r0 == r1);
2367
2368 // Check that [~5] is really [-MIN,4][6,MAX].
2369 r0 = int_range<1> (INT (5), INT (5), VR_ANTI_RANGE);
2370 r1 = int_range<1> (minint, INT (4));
2371 r1.union_ (int_range<1> (INT (6), maxint));
2372 ASSERT_FALSE (r1.undefined_p ());
2373 ASSERT_TRUE (r0 == r1);
2374
2375 r1 = int_range<1> (INT (5), INT (5));
2376 int_range<1> r2 (r1);
2377 ASSERT_TRUE (r1 == r2);
2378
2379 r1 = int_range<1> (INT (5), INT (10));
2380
2381 r1 = int_range<1> (integer_type_node,
2382 wi::to_wide (INT (5)), wi::to_wide (INT (10)));
2383 ASSERT_TRUE (r1.contains_p (INT (7)));
2384
2385 r1 = int_range<1> (SCHAR (0), SCHAR (20));
2386 ASSERT_TRUE (r1.contains_p (SCHAR(15)));
2387 ASSERT_FALSE (r1.contains_p (SCHAR(300)));
2388
2389 // NOT([10,20]) ==> [-MIN,9][21,MAX].
2390 r0 = r1 = int_range<1> (INT (10), INT (20));
2391 r2 = int_range<1> (minint, INT(9));
2392 r2.union_ (int_range<1> (INT(21), maxint));
2393 ASSERT_FALSE (r2.undefined_p ());
2394 r1.invert ();
2395 ASSERT_TRUE (r1 == r2);
2396 // Test that NOT(NOT(x)) == x.
2397 r2.invert ();
2398 ASSERT_TRUE (r0 == r2);
2399
2400 // Test that booleans and their inverse work as expected.
2401 r0 = range_zero (boolean_type_node);
2402 ASSERT_TRUE (r0 == int_range<1> (build_zero_cst (boolean_type_node),
2403 build_zero_cst (boolean_type_node)));
2404 r0.invert ();
2405 ASSERT_TRUE (r0 == int_range<1> (build_one_cst (boolean_type_node),
2406 build_one_cst (boolean_type_node)));
2407
2408 // Make sure NULL and non-NULL of pointer types work, and that
2409 // inverses of them are consistent.
2410 tree voidp = build_pointer_type (void_type_node);
2411 r0 = range_zero (voidp);
2412 r1 = r0;
2413 r0.invert ();
2414 r0.invert ();
2415 ASSERT_TRUE (r0 == r1);
2416
2417 // [10,20] U [15, 30] => [10, 30].
2418 r0 = int_range<1> (INT (10), INT (20));
2419 r1 = int_range<1> (INT (15), INT (30));
2420 r0.union_ (r1);
2421 ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (30)));
2422
2423 // [15,40] U [] => [15,40].
2424 r0 = int_range<1> (INT (15), INT (40));
2425 r1.set_undefined ();
2426 r0.union_ (r1);
2427 ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (40)));
2428
2429 // [10,20] U [10,10] => [10,20].
2430 r0 = int_range<1> (INT (10), INT (20));
2431 r1 = int_range<1> (INT (10), INT (10));
2432 r0.union_ (r1);
2433 ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (20)));
2434
2435 // [10,20] U [9,9] => [9,20].
2436 r0 = int_range<1> (INT (10), INT (20));
2437 r1 = int_range<1> (INT (9), INT (9));
2438 r0.union_ (r1);
2439 ASSERT_TRUE (r0 == int_range<1> (INT (9), INT (20)));
2440
2441 // [10,20] ^ [15,30] => [15,20].
2442 r0 = int_range<1> (INT (10), INT (20));
2443 r1 = int_range<1> (INT (15), INT (30));
2444 r0.intersect (r1);
2445 ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (20)));
2446
2447 // Test the internal sanity of wide_int's wrt HWIs.
2448 ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node),
2449 TYPE_SIGN (boolean_type_node))
2450 == wi::uhwi (1, TYPE_PRECISION (boolean_type_node)));
2451
2452 // Test zero_p().
2453 r0 = int_range<1> (INT (0), INT (0));
2454 ASSERT_TRUE (r0.zero_p ());
2455
2456 // Test nonzero_p().
2457 r0 = int_range<1> (INT (0), INT (0));
2458 r0.invert ();
2459 ASSERT_TRUE (r0.nonzero_p ());
2460
2461 // test legacy interaction
2462 // r0 = ~[1,1]
2463 r0 = int_range<1> (UINT (1), UINT (1), VR_ANTI_RANGE);
2464 // r1 = ~[3,3]
2465 r1 = int_range<1> (UINT (3), UINT (3), VR_ANTI_RANGE);
2466
2467 // vv = [0,0][2,2][4, MAX]
2468 int_range<3> vv = r0;
2469 vv.intersect (r1);
2470
2471 ASSERT_TRUE (vv.contains_p (UINT (2)));
2472 ASSERT_TRUE (vv.num_pairs () == 3);
2473
2474 // create r0 as legacy [1,1]
2475 r0 = int_range<1> (UINT (1), UINT (1));
2476 // And union it with [0,0][2,2][4,MAX] multi range
2477 r0.union_ (vv);
2478 // The result should be [0,2][4,MAX], or ~[3,3] but it must contain 2
2479 ASSERT_TRUE (r0.contains_p (UINT (2)));
2480}
2481
2482void
2483range_tests ()
2484{
2485 range_tests_legacy ();
2486 range_tests_irange3 ();
2487 range_tests_int_range_max ();
3d3470e2 2488 range_tests_strict_enum ();
b5cff0db
AH
2489 range_tests_misc ();
2490}
2491
2492} // namespace selftest
2493
2494#endif // CHECKING_P