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