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