Line data Source code
1 : /* Support routines for value ranges.
2 : Copyright (C) 2019-2026 Free Software Foundation, Inc.
3 : Major hacks by Aldy Hernandez <aldyh@redhat.com> and
4 : Andrew MacLeod <amacleod@redhat.com>.
5 :
6 : This file is part of GCC.
7 :
8 : GCC is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3, or (at your option)
11 : any later version.
12 :
13 : GCC is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GCC; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 : #include "config.h"
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "backend.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "ssa.h"
29 : #include "pretty-print.h"
30 : #include "value-range-pretty-print.h"
31 : #include "value-range-storage.h"
32 : #include "fold-const.h"
33 : #include "gimple-range.h"
34 : #include "tree-dfa.h"
35 : #include "tree-affine.h"
36 :
37 : // Return the bitmask inherent in a range : TYPE [MIN, MAX].
38 : // This used to be get_bitmask_from_range ().
39 :
40 1340152191 : irange_bitmask::irange_bitmask (tree type,
41 1340152191 : const wide_int &min, const wide_int &max)
42 : {
43 1340152191 : unsigned prec = TYPE_PRECISION (type);
44 : // All the bits of a singleton are known.
45 1340152191 : if (min == max)
46 : {
47 180636691 : m_mask = wi::zero (prec);
48 180636691 : m_value = min;
49 : }
50 : else
51 : {
52 1159515500 : wide_int xorv = min ^ max;
53 : // Mask will have leading zeros for all leading bits that are
54 : // common, both zeros and ones.
55 1159515500 : m_mask = wi::mask (prec - wi::clz (xorv), false, prec);
56 : // Now set value to those bits which are known, and zero the rest.
57 1159665534 : m_value = ~m_mask & min;
58 1159515500 : }
59 1340152191 : }
60 :
61 : // Return a range in R of TYPE for this bitmask which encompasses
62 : // a set of valid values which are allowable for this bitmask/value
63 : // combination. If false is returned, no range was set.
64 :
65 : bool
66 130322490 : irange_bitmask::range_from_mask (irange &r, tree type) const
67 : {
68 130322490 : if (unknown_p ())
69 : return false;
70 :
71 260528265 : gcc_checking_assert ((value () & mask ()) == 0);
72 130263479 : unsigned popcount = wi::popcount (mask ());
73 :
74 : // For 0, 1 or 2 bits set, create a range with only the allowed values.
75 130263479 : if (popcount <= 2)
76 : {
77 : // VALUE is always a valid range.
78 26693194 : r.set (type, value (), value ());
79 : // If there are bits in mask, (VALUE | MASK) is also valid.
80 26693151 : if (popcount >= 1)
81 11455985 : r.union_ (int_range<1> (type, value () | mask (), value () | mask ()));
82 : // If there are 2 bits set, add the other 2 possible values.
83 11455905 : if (popcount == 2)
84 : {
85 : // Extract the two 1-bit masks into lb and ub.
86 4707776 : wide_int lb = mask () & -mask (); // Lowest set bit.
87 4707776 : wide_int ub = mask () & (mask () - 1); // The other bit.
88 4707776 : r.union_ (int_range<1> (type, value () | lb, value () | lb));
89 4707776 : r.union_ (int_range<1> (type, value () | ub, value () | ub));
90 4707776 : }
91 : return true;
92 : }
93 :
94 : // Otherwise, calculate the valid range allowed by the bitmask.
95 103570328 : int prec = TYPE_PRECISION (type);
96 103571592 : wide_int ub = mask () | value ();
97 103570328 : wide_int sign_bit = wi::one (prec) << (prec - 1);
98 103570328 : wide_int sign_mask = mask () & sign_bit;
99 103570328 : wide_int sign_value = value () & sign_bit;
100 : // Create a lower and upper bound.
101 : // If unsigned, or the sign is known to be positive, create [lb, ub]
102 103570328 : if (TYPE_SIGN (type) == UNSIGNED || (sign_mask == 0 && sign_value == 0))
103 95920573 : r.set (type, value (), mask () | value ());
104 : // If the sign bit is KNOWN to be 1, we have a completely negative range.
105 7654635 : else if (sign_mask == 0 && sign_value != 0)
106 803395 : r.set (type, value (), value () | (mask () & ~sign_bit));
107 : else
108 : {
109 : // Otherwise there are 2 ranges, a negative and positive interval.
110 6851270 : wide_int neg_base = value () | sign_bit;
111 6851309 : wide_int pos_mask = mask () & ~sign_bit;
112 6851270 : r.set (type, neg_base , neg_base | pos_mask);
113 6851387 : r.union_ (int_range<1> (type, value (), value () | pos_mask));
114 6851309 : }
115 :
116 : // If the mask doesn't have a trailing zero, there is nothing else to filter.
117 103570328 : int z = wi::ctz (mask ());
118 103570328 : if (z == 0)
119 : return true;
120 :
121 : // Remove the [0, X] values which the trailing-zero mask rules out.
122 : // For example, if z == 4, the mask is 0xFFF0, and the lowest 4 bits
123 : // define the range [0, 15]. Only (value & low_mask) is allowed.
124 32960413 : ub = (wi::one (prec) << z) - 1; // Upper bound of range.
125 32960115 : int_range<4> mask_range (type, wi::zero (prec), ub);
126 : // Remove the valid value from the excluded range and form an anti-range.
127 32960115 : wide_int allow = value () & ub;
128 32960115 : mask_range.intersect (int_range<2> (type, allow, allow, VR_ANTI_RANGE));
129 32960115 : bool res = mask_range.invert ();
130 32960115 : gcc_checking_assert (res);
131 32960115 : r.intersect (mask_range);
132 :
133 32960115 : if (TYPE_SIGN (type) == SIGNED)
134 : {
135 : // For signed negative values, find the lowest value with trailing zeros.
136 : // This forms a range such as [-512, -1] for z=9.
137 12543964 : wide_int lb = -(wi::one (prec) << z);
138 12543964 : int_range<4> mask_range (type, lb, wi::minus_one (prec));
139 : // Remove the one allowed value from that set.
140 12543964 : wide_int allow = value () | lb;
141 12543964 : mask_range.intersect (int_range<2> (type, allow, allow, VR_ANTI_RANGE));
142 12543964 : res = mask_range.invert ();
143 12543964 : gcc_checking_assert (res);
144 12543964 : r.intersect (mask_range);
145 12544253 : }
146 32960115 : return true;
147 136534235 : }
148 :
149 :
150 : void
151 42024 : irange::accept (const vrange_visitor &v) const
152 : {
153 42024 : v.visit (*this);
154 42024 : }
155 :
156 : void
157 24745 : value_range::dump (FILE *out) const
158 : {
159 24745 : if (m_vrange)
160 24745 : m_vrange->dump (out);
161 : else
162 0 : fprintf (out, "NULL");
163 24745 : }
164 :
165 : void
166 2324 : value_range::print (pretty_printer *pp) const
167 : {
168 2324 : if (m_vrange)
169 : {
170 2324 : vrange_printer vrange_pp (pp);
171 2324 : m_vrange->accept (vrange_pp);
172 : }
173 : else
174 0 : pp_string (pp, "NULL");
175 2324 : }
176 :
177 : DEBUG_FUNCTION void
178 0 : debug (const value_range &r)
179 : {
180 0 : r.dump (stderr);
181 0 : fprintf (stderr, "\n");
182 0 : }
183 :
184 : DEBUG_FUNCTION void
185 0 : debug (const irange_bitmask &bm)
186 : {
187 0 : bm.dump (stderr);
188 0 : fprintf (stderr, "\n");
189 0 : }
190 :
191 : // Definitions for unsupported_range.
192 :
193 : void
194 603 : unsupported_range::accept (const vrange_visitor &v) const
195 : {
196 603 : v.visit (*this);
197 603 : }
198 :
199 : void
200 37 : vrange::update_bitmask (const class irange_bitmask &)
201 : {
202 37 : }
203 :
204 : irange_bitmask
205 0 : vrange::get_bitmask () const
206 : {
207 : // Return all unknown bits for the given precision.
208 0 : return irange_bitmask (TYPE_PRECISION (type ()));
209 : }
210 :
211 : bool
212 0 : unsupported_range::contains_p (tree) const
213 : {
214 0 : return varying_p ();
215 : }
216 :
217 : bool
218 1316922 : unsupported_range::singleton_p (tree *) const
219 : {
220 1316922 : return false;
221 : }
222 :
223 : void
224 0 : unsupported_range::set (tree min, tree, value_range_kind)
225 : {
226 0 : set_varying (TREE_TYPE (min));
227 0 : }
228 :
229 : tree
230 0 : unsupported_range::type () const
231 : {
232 0 : return void_type_node;
233 : }
234 :
235 : bool
236 21050502 : unsupported_range::supports_type_p (const_tree) const
237 : {
238 21050502 : return false;
239 : }
240 :
241 : void
242 115484240 : unsupported_range::set_undefined ()
243 : {
244 115484240 : m_kind = VR_UNDEFINED;
245 115484240 : }
246 :
247 : void
248 3619766 : unsupported_range::set_varying (tree)
249 : {
250 3619766 : m_kind = VR_VARYING;
251 3619766 : }
252 :
253 : bool
254 0 : unsupported_range::union_ (const vrange &v)
255 : {
256 0 : const unsupported_range &r = as_a <unsupported_range> (v);
257 :
258 0 : if (r.undefined_p () || varying_p ())
259 : return false;
260 0 : if (undefined_p () || r.varying_p ())
261 : {
262 0 : operator= (r);
263 0 : return true;
264 : }
265 0 : gcc_unreachable ();
266 : return false;
267 : }
268 :
269 : bool
270 0 : unsupported_range::intersect (const vrange &v)
271 : {
272 0 : const unsupported_range &r = as_a <unsupported_range> (v);
273 :
274 0 : if (undefined_p () || r.varying_p ())
275 : return false;
276 0 : if (r.undefined_p ())
277 : {
278 0 : set_undefined ();
279 0 : return true;
280 : }
281 0 : if (varying_p ())
282 : {
283 0 : operator= (r);
284 0 : return true;
285 : }
286 0 : gcc_unreachable ();
287 : return false;
288 : }
289 :
290 : bool
291 0 : unsupported_range::zero_p () const
292 : {
293 0 : return false;
294 : }
295 :
296 : bool
297 0 : unsupported_range::contains_zero_p () const
298 : {
299 0 : return varying_p ();
300 : }
301 :
302 : void
303 0 : unsupported_range::set_nonzero (tree type)
304 : {
305 0 : set_varying (type);
306 0 : }
307 :
308 : void
309 0 : unsupported_range::set_zero (tree type)
310 : {
311 0 : set_varying (type);
312 0 : }
313 :
314 : void
315 0 : unsupported_range::set_nonnegative (tree type)
316 : {
317 0 : set_varying (type);
318 0 : }
319 :
320 : bool
321 0 : unsupported_range::fits_p (const vrange &) const
322 : {
323 0 : return true;
324 : }
325 :
326 : unsupported_range &
327 655935 : unsupported_range::operator= (const unsupported_range &r)
328 : {
329 655935 : if (r.undefined_p ())
330 655935 : set_undefined ();
331 0 : else if (r.varying_p ())
332 0 : set_varying (void_type_node);
333 : else
334 0 : gcc_unreachable ();
335 655935 : return *this;
336 : }
337 :
338 : tree
339 0 : unsupported_range::lbound () const
340 : {
341 0 : return NULL;
342 : }
343 :
344 : tree
345 0 : unsupported_range::ubound () const
346 : {
347 0 : return NULL;
348 : }
349 :
350 : // Assignment operator for generic ranges. Copying incompatible types
351 : // is not allowed.
352 :
353 : vrange &
354 15029875 : vrange::operator= (const vrange &src)
355 : {
356 15029875 : if (is_a <irange> (src))
357 13391524 : as_a <irange> (*this) = as_a <irange> (src);
358 1638351 : else if (is_a <prange> (src))
359 1194745 : as_a <prange> (*this) = as_a <prange> (src);
360 443606 : else if (is_a <frange> (src))
361 443606 : as_a <frange> (*this) = as_a <frange> (src);
362 : else
363 : {
364 0 : gcc_checking_assert (is_a <unsupported_range> (src));
365 0 : m_kind = src.m_kind;
366 : }
367 15029875 : return *this;
368 : }
369 :
370 : // Equality operator for generic ranges.
371 :
372 : bool
373 188023299 : vrange::operator== (const vrange &src) const
374 : {
375 188023299 : if (is_a <irange> (src))
376 168140219 : return as_a <irange> (*this) == as_a <irange> (src);
377 19883080 : if (is_a <prange> (src))
378 17053722 : return as_a <prange> (*this) == as_a <prange> (src);
379 2829358 : if (is_a <frange> (src))
380 2829358 : return as_a <frange> (*this) == as_a <frange> (src);
381 0 : gcc_unreachable ();
382 : }
383 :
384 : // Wrapper for vrange_printer to dump a range to a file.
385 :
386 : void
387 39470 : vrange::dump (FILE *file) const
388 : {
389 39470 : pretty_printer pp;
390 39470 : pp_needs_newline (&pp) = true;
391 39470 : pp.set_output_stream (file);
392 39470 : vrange_printer vrange_pp (&pp);
393 39470 : this->accept (vrange_pp);
394 39470 : pp_flush (&pp);
395 39470 : }
396 :
397 : void
398 0 : irange_bitmask::dump (FILE *file) const
399 : {
400 0 : char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p;
401 0 : pretty_printer pp;
402 :
403 0 : pp_needs_newline (&pp) = true;
404 0 : pp.set_output_stream (file);
405 0 : pp_string (&pp, "MASK ");
406 0 : unsigned len_mask, len_val;
407 0 : if (print_hex_buf_size (m_mask, &len_mask)
408 0 : | print_hex_buf_size (m_value, &len_val))
409 0 : p = XALLOCAVEC (char, MAX (len_mask, len_val));
410 : else
411 : p = buf;
412 0 : print_hex (m_mask, p);
413 0 : pp_string (&pp, p);
414 0 : pp_string (&pp, " VALUE ");
415 0 : print_hex (m_value, p);
416 0 : pp_string (&pp, p);
417 0 : pp_flush (&pp);
418 0 : }
419 :
420 : namespace inchash
421 : {
422 :
423 : void
424 28615489 : add_vrange (const vrange &v, inchash::hash &hstate,
425 : unsigned int)
426 : {
427 28615489 : if (v.undefined_p ())
428 : {
429 0 : hstate.add_int (VR_UNDEFINED);
430 0 : return;
431 : }
432 : // Types are ignored throughout to inhibit two ranges being equal
433 : // but having different hash values. This can happen when two
434 : // ranges are equal and their types are different (but
435 : // types_compatible_p is true).
436 28615489 : if (is_a <irange> (v))
437 : {
438 12427227 : const irange &r = as_a <irange> (v);
439 12427227 : if (r.varying_p ())
440 0 : hstate.add_int (VR_VARYING);
441 : else
442 12427227 : hstate.add_int (VR_RANGE);
443 26485083 : for (unsigned i = 0; i < r.num_pairs (); ++i)
444 : {
445 14057856 : hstate.add_wide_int (r.lower_bound (i));
446 14058518 : hstate.add_wide_int (r.upper_bound (i));
447 : }
448 12427227 : irange_bitmask bm = r.get_bitmask ();
449 12427227 : hstate.add_wide_int (bm.value ());
450 12427227 : hstate.add_wide_int (bm.mask ());
451 12427227 : return;
452 12427227 : }
453 16188262 : if (is_a <prange> (v))
454 : {
455 16097322 : const prange &r = as_a <prange> (v);
456 16097322 : if (r.varying_p ())
457 519 : hstate.add_int (VR_VARYING);
458 : else
459 : {
460 16096803 : hstate.add_int (VR_RANGE);
461 16096803 : hstate.add_wide_int (r.lower_bound ());
462 16096803 : hstate.add_wide_int (r.upper_bound ());
463 16096803 : irange_bitmask bm = r.get_bitmask ();
464 16096803 : hstate.add_wide_int (bm.value ());
465 16096803 : hstate.add_wide_int (bm.mask ());
466 16096803 : bool flag = false;
467 16096803 : tree tmp = r.addr_known ();
468 : if (tmp)
469 : flag = true;
470 : else
471 2940840 : tmp = r.addr_excluded ();
472 16096803 : hstate.add_ptr (tmp);
473 16096803 : hstate.add_flag (flag);
474 16096803 : }
475 : return;
476 : }
477 90940 : if (is_a <frange> (v))
478 : {
479 90940 : const frange &r = as_a <frange> (v);
480 90940 : if (r.known_isnan ())
481 291 : hstate.add_int (VR_NAN);
482 : else
483 : {
484 90649 : hstate.add_int (r.varying_p () ? VR_VARYING : VR_RANGE);
485 90649 : hstate.add_real_value (r.lower_bound ());
486 90649 : hstate.add_real_value (r.upper_bound ());
487 : }
488 90940 : nan_state nan = r.get_nan_state ();
489 90940 : hstate.add_int (nan.pos_p ());
490 90940 : hstate.add_int (nan.neg_p ());
491 90940 : return;
492 : }
493 0 : gcc_unreachable ();
494 : }
495 :
496 : } //namespace inchash
497 :
498 : bool
499 8823331 : irange::nonnegative_p () const
500 : {
501 8823331 : return wi::ge_p (lower_bound (), 0, TYPE_SIGN (type ()));
502 : }
503 :
504 : bool
505 4117029 : irange::nonpositive_p () const
506 : {
507 4117029 : return wi::le_p (upper_bound (), 0, TYPE_SIGN (type ()));
508 : }
509 :
510 : bool
511 781478451 : irange::supports_type_p (const_tree type) const
512 : {
513 781478451 : return supports_p (type);
514 : }
515 :
516 : // Return TRUE if R fits in THIS.
517 :
518 : bool
519 0 : irange::fits_p (const vrange &r) const
520 : {
521 0 : return m_max_ranges >= as_a <irange> (r).num_pairs ();
522 : }
523 :
524 : void
525 44833 : irange::set_nonnegative (tree type)
526 : {
527 44833 : set (type,
528 89666 : wi::zero (TYPE_PRECISION (type)),
529 44833 : wi::to_wide (TYPE_MAX_VALUE (type)));
530 44833 : }
531 :
532 :
533 : // Set the address info to EXPR if possible. KNOWN_P is TRUE if this range
534 : // is known to be EXPR, and FALSE if it is known to exclude EXPR.
535 :
536 : void
537 15289781 : prange::set_addr (tree expr, bool known_p)
538 : {
539 15289781 : gcc_checking_assert (m_kind != VR_UNDEFINED);
540 15289781 : gcc_checking_assert (!expr || TREE_CODE (expr) != SSA_NAME);
541 :
542 15289781 : m_addr = NULL_TREE;
543 15289781 : m_known_p = false;
544 :
545 : // A zero range means no address info.
546 15289781 : if (zero_p ())
547 : return;
548 :
549 : // No address initially may make this VARYING.
550 15289754 : if (varying_compatible_p ())
551 1395651 : set_varying (type ());
552 : else
553 13894103 : m_kind = VR_RANGE;
554 :
555 15289754 : if (!expr)
556 : return;
557 :
558 15289754 : gcc_checking_assert (TREE_CODE (expr) == ADDR_EXPR);
559 :
560 : // Ensure only constants get through for now.
561 15289754 : if (!is_gimple_min_invariant (expr))
562 : return;
563 :
564 11663041 : m_addr = expr;
565 11663041 : m_known_p = known_p;
566 11663041 : m_kind = VR_RANGE;
567 : }
568 :
569 :
570 : // If addr is present and is an address that is local to its function,
571 : // clear it and return TRUE. otherwise do nothing and retiurn false.
572 :
573 : bool
574 10129392 : prange::clear_function_local_addr ()
575 : {
576 10129392 : if (!m_addr)
577 : return false;
578 :
579 : /* is_gimple_ip_invariant () accepts the address of a LABEL_DECL, but
580 : inlining and cloning could remap it, so remove it as well. */
581 2053979 : if (!is_gimple_ip_invariant (m_addr)
582 2053979 : || TREE_CODE (TREE_OPERAND (m_addr, 0)) == LABEL_DECL)
583 : {
584 1051176 : set_addr_unknown ();
585 1051176 : return true;
586 : }
587 : return false;
588 : }
589 :
590 : // Prange implementation.
591 :
592 : void
593 1975 : prange::accept (const vrange_visitor &v) const
594 : {
595 1975 : v.visit (*this);
596 1975 : }
597 :
598 : void
599 0 : prange::set_nonnegative (tree type)
600 : {
601 0 : set (type,
602 0 : wi::zero (TYPE_PRECISION (type)),
603 0 : wi::max_value (TYPE_PRECISION (type), UNSIGNED));
604 0 : }
605 :
606 : void
607 14298836 : prange::set (tree min, tree max, value_range_kind kind)
608 : {
609 14298836 : return set (TREE_TYPE (min), wi::to_wide (min), wi::to_wide (max), kind);
610 : }
611 :
612 : void
613 41459656 : prange::set (tree type, const wide_int &min, const wide_int &max,
614 : value_range_kind kind)
615 : {
616 41459656 : if (kind == VR_UNDEFINED)
617 : {
618 0 : set_undefined ();
619 0 : return;
620 : }
621 41459656 : if (kind == VR_VARYING)
622 : {
623 0 : set_varying (type);
624 0 : return;
625 : }
626 41459656 : if (kind == VR_ANTI_RANGE)
627 : {
628 0 : gcc_checking_assert (min == 0 && max == 0);
629 0 : set_nonzero (type);
630 0 : return;
631 : }
632 41459656 : m_type = type;
633 41459656 : m_min = min;
634 41459656 : m_max = max;
635 :
636 41459656 : if (m_min == 0 && m_max == -1)
637 : {
638 5037035 : m_kind = VR_VARYING;
639 5037035 : m_bitmask.set_unknown (TYPE_PRECISION (type));
640 5037035 : set_addr_unknown ();
641 5037035 : if (flag_checking)
642 5037035 : verify_range ();
643 : return;
644 : }
645 :
646 36422621 : m_kind = VR_RANGE;
647 36422621 : m_bitmask = irange_bitmask (type, min, max);
648 36422621 : set_addr_unknown ();
649 36422621 : if (flag_checking)
650 36422609 : verify_range ();
651 : }
652 :
653 : bool
654 30159573 : prange::contains_p (const wide_int &w) const
655 : {
656 30159573 : if (undefined_p ())
657 : return false;
658 :
659 30159573 : if (varying_p ())
660 : return true;
661 :
662 18294378 : return (wi::le_p (lower_bound (), w, UNSIGNED)
663 9661352 : && wi::ge_p (upper_bound (), w, UNSIGNED));
664 : }
665 :
666 : bool
667 258111858 : prange::singleton_p (tree *result) const
668 : {
669 359249914 : if (m_kind == VR_RANGE && lower_bound () == upper_bound ())
670 : {
671 341364 : if (result)
672 150040 : *result = wide_int_to_tree (type (), m_min);
673 : return true;
674 : }
675 : return false;
676 : }
677 :
678 : tree
679 4942640 : prange::lbound () const
680 : {
681 4942640 : return wide_int_to_tree (type (), m_min);
682 : }
683 :
684 : tree
685 812548 : prange::ubound () const
686 : {
687 812548 : return wide_int_to_tree (type (), m_max);
688 : }
689 :
690 : bool
691 16461232 : prange::union_ (const vrange &v)
692 : {
693 16461232 : const prange &r = as_a <prange> (v);
694 :
695 16461232 : if (r.undefined_p ())
696 : return false;
697 16317372 : if (undefined_p ())
698 : {
699 8097134 : *this = r;
700 8097134 : if (flag_checking)
701 8097134 : verify_range ();
702 : return true;
703 : }
704 8220238 : if (varying_p ())
705 : return false;
706 4893848 : if (r.varying_p ())
707 : {
708 922419 : set_varying (type ());
709 922419 : return true;
710 : }
711 :
712 3971429 : wide_int new_lb = wi::min (r.lower_bound (), lower_bound (), UNSIGNED);
713 3971429 : wide_int new_ub = wi::max (r.upper_bound (), upper_bound (), UNSIGNED);
714 3971429 : prange new_range (type (), new_lb, new_ub);
715 3971429 : new_range.m_bitmask.union_ (m_bitmask);
716 3971429 : new_range.m_bitmask.union_ (r.m_bitmask);
717 :
718 : // Keep it simple, either both name the same address or both do not,
719 : // or we drop the address info.
720 3971429 : if (addr_equal_p (r))
721 3315221 : new_range.set_addr (*this);
722 :
723 3971429 : if (new_range.varying_compatible_p ())
724 : {
725 506639 : set_varying (type ());
726 506639 : return true;
727 : }
728 3464790 : if (flag_checking)
729 3464790 : new_range.verify_range ();
730 3464790 : if (new_range == *this)
731 : return false;
732 384331 : *this = new_range;
733 384331 : return true;
734 3971429 : }
735 :
736 : bool
737 197444285 : prange::intersect (const vrange &v)
738 : {
739 197444285 : const prange &r = as_a <prange> (v);
740 197444285 : gcc_checking_assert (undefined_p () || r.undefined_p ()
741 : || range_compatible_p (type (), r.type ()));
742 :
743 197444285 : if (undefined_p ())
744 : return false;
745 197322333 : if (r.undefined_p ())
746 : {
747 44006 : set_undefined ();
748 44006 : return true;
749 : }
750 197278327 : if (r.varying_p ())
751 : return false;
752 123135780 : if (varying_p ())
753 : {
754 47453842 : *this = r;
755 47453842 : return true;
756 : }
757 :
758 : // If one knows the address and the other excludes it, the result is
759 : // undefined.
760 75681938 : if (addr_inverted_p (r))
761 : {
762 657 : set_undefined ();
763 657 : return true;
764 : }
765 :
766 75681281 : prange save = *this;
767 75681281 : m_min = wi::max (r.lower_bound (), lower_bound (), UNSIGNED);
768 75681281 : m_max = wi::min (r.upper_bound (), upper_bound (), UNSIGNED);
769 75681281 : if (wi::gt_p (m_min, m_max, UNSIGNED))
770 : {
771 386438 : set_undefined ();
772 386438 : return true;
773 : }
774 :
775 : // Intersect all bitmasks: the old one, the new one, and the other operand's.
776 75294843 : irange_bitmask new_bitmask (m_type, m_min, m_max);
777 75294843 : if (!m_bitmask.intersect (new_bitmask))
778 12 : set_undefined ();
779 75294831 : else if (!m_bitmask.intersect (r.m_bitmask))
780 63 : set_undefined ();
781 : // If only one has an address, that is the intersection.
782 75294768 : else if (addr_unknown_p () && !r.addr_unknown_p ())
783 951263 : set_addr (r);
784 74343505 : else if (!addr_unknown_p () && !r.addr_unknown_p ())
785 : {
786 : // If both have an address, we want to be careful. Without aliasing
787 : // 2 different values can name the same address, so UNDEFINED is
788 : // not appropriate, but we want to keep the rule that intersection
789 : // never becomes larger.
790 : // If the other operand knows an address and this one only excludes
791 : // one, use the known one. Otherwise leave the range as is.
792 1233147 : if (addr_excluded () && r.addr_known ())
793 259618 : set_addr (r);
794 : }
795 :
796 : // If this evolves to zero, clear all address info.
797 75294843 : if (zero_p () && !addr_unknown_p ())
798 40474 : set_addr_unknown ();
799 :
800 75294843 : if (varying_compatible_p ())
801 : {
802 0 : set_varying (type ());
803 0 : return true;
804 : }
805 :
806 75294843 : if (flag_checking)
807 75294717 : verify_range ();
808 75294843 : if (*this == save)
809 70490674 : return false;
810 : return true;
811 75681281 : }
812 :
813 : prange &
814 209751916 : prange::operator= (const prange &src)
815 : {
816 209751916 : m_type = src.m_type;
817 209751916 : m_kind = src.m_kind;
818 209751916 : m_min = src.m_min;
819 209751916 : m_max = src.m_max;
820 209751916 : m_bitmask = src.m_bitmask;
821 209751916 : set_addr (src);
822 209751916 : if (flag_checking)
823 209751742 : verify_range ();
824 209751916 : return *this;
825 : }
826 :
827 : bool
828 95813359 : prange::operator== (const prange &src) const
829 : {
830 95813359 : if (m_kind == src.m_kind)
831 : {
832 90744385 : if (undefined_p ())
833 : return true;
834 :
835 90736227 : if (varying_p ())
836 4150043 : return types_compatible_p (type (), src.type ());
837 :
838 86586184 : if (!addr_equal_p (src))
839 : return false;
840 :
841 166622586 : return (m_min == src.m_min && m_max == src.m_max
842 165739905 : && m_bitmask == src.m_bitmask);
843 : }
844 : return false;
845 : }
846 :
847 :
848 : // Return the inverse of a range. Return false if thre is no invert
849 : // calculatable.
850 :
851 : bool
852 2746506 : prange::invert ()
853 : {
854 2746506 : if (undefined_p () || varying_p ())
855 : return false;
856 :
857 : // Invert the points_to object. If that worked, this is done.
858 2746506 : if (addr_invert ())
859 0 : return true;
860 : else
861 2746506 : set_addr_unknown ();
862 :
863 2746506 : wide_int new_lb, new_ub;
864 2746506 : unsigned prec = TYPE_PRECISION (type ());
865 2746506 : wide_int type_min = wi::zero (prec);
866 2746506 : wide_int type_max = wi::max_value (prec, UNSIGNED);
867 2746506 : wi::overflow_type ovf;
868 :
869 2746506 : if (lower_bound () == type_min)
870 : {
871 2739181 : new_lb = wi::add (upper_bound (), 1, UNSIGNED, &ovf);
872 2739181 : if (ovf)
873 0 : new_lb = type_min;
874 2739181 : new_ub = type_max;
875 2739181 : set (type (), new_lb, new_ub);
876 : }
877 7325 : else if (upper_bound () == type_max)
878 : {
879 3307 : wi::overflow_type ovf;
880 3307 : new_lb = type_min;
881 3307 : new_ub = wi::sub (lower_bound (), 1, UNSIGNED, &ovf);
882 3307 : if (ovf)
883 0 : new_ub = type_max;
884 3307 : set (type (), new_lb, new_ub);
885 : }
886 : else
887 4018 : set_varying (type ());
888 2746506 : return true;
889 2746506 : }
890 :
891 : void
892 1626492464 : prange::verify_range () const
893 : {
894 1626492464 : gcc_checking_assert (m_discriminator == VR_PRANGE);
895 :
896 1626492464 : if (m_kind == VR_UNDEFINED)
897 : {
898 66947 : gcc_checking_assert (addr_unknown_p ());
899 : return;
900 : }
901 :
902 1626425517 : gcc_checking_assert (supports_p (type ()));
903 :
904 1626425517 : if (m_kind == VR_VARYING)
905 : {
906 698802443 : gcc_checking_assert (varying_compatible_p ());
907 : return;
908 : }
909 927623074 : gcc_checking_assert (!varying_compatible_p ());
910 927623074 : gcc_checking_assert (m_kind == VR_RANGE);
911 927623074 : if (!addr_unknown_p ())
912 : {
913 32311206 : gcc_checking_assert (!varying_p ());
914 32311206 : gcc_checking_assert (!undefined_p ());
915 32311206 : gcc_checking_assert (!zero_p ());
916 : }
917 : }
918 :
919 : void
920 30859497 : prange::update_bitmask (const irange_bitmask &bm)
921 : {
922 30859497 : gcc_checking_assert (!undefined_p ());
923 :
924 : // If all the bits are known, this is a singleton.
925 30859497 : if (bm.mask () == 0)
926 : {
927 179680 : set (type (), bm.value (), bm.value ());
928 179680 : return;
929 : }
930 :
931 : // Drop VARYINGs with known bits to a plain range.
932 39411546 : if (m_kind == VR_VARYING && !bm.unknown_p ())
933 1989783 : m_kind = VR_RANGE;
934 :
935 30679817 : m_bitmask = bm;
936 30679817 : if (varying_compatible_p ())
937 6741946 : m_kind = VR_VARYING;
938 :
939 30679817 : if (flag_checking)
940 30679817 : verify_range ();
941 : }
942 :
943 :
944 : // Frange implementation.
945 :
946 : void
947 259 : frange::accept (const vrange_visitor &v) const
948 : {
949 259 : v.visit (*this);
950 259 : }
951 :
952 : bool
953 0 : frange::fits_p (const vrange &) const
954 : {
955 0 : return true;
956 : }
957 :
958 : // Compare two range endpoints.
959 : //
960 : // In IEEE -0.0 and +0.0 equal for comparison purposes, but as endpoints they
961 : // are distinct. Order -0.0 strictly below +0.0 and use this rather than
962 : // real_less/real_compare, and the signed zeros stop needing a special case.
963 :
964 : static int
965 116436082 : frange_cmp (const REAL_VALUE_TYPE &a, const REAL_VALUE_TYPE &b)
966 : {
967 116436082 : gcc_checking_assert (!real_isnan (&a) && !real_isnan (&b));
968 :
969 116436082 : if (real_less (&a, &b))
970 : return -1;
971 52112377 : if (real_less (&b, &a))
972 : return 1;
973 24891145 : if (real_iszero (&a) && real_iszero (&b))
974 : {
975 5224815 : bool nega = real_isneg (&a);
976 5224815 : bool negb = real_isneg (&b);
977 5224815 : if (nega && !negb)
978 : return -1;
979 3901391 : if (!nega && negb)
980 223401 : return 1;
981 : }
982 : return 0;
983 : }
984 :
985 : static inline const REAL_VALUE_TYPE &
986 8110889 : frange_min (const REAL_VALUE_TYPE &a, const REAL_VALUE_TYPE &b)
987 : {
988 8110889 : return frange_cmp (a, b) <= 0 ? a : b;
989 : }
990 :
991 : static inline const REAL_VALUE_TYPE &
992 8110889 : frange_max (const REAL_VALUE_TYPE &a, const REAL_VALUE_TYPE &b)
993 : {
994 8110889 : return frange_cmp (a, b) >= 0 ? a : b;
995 : }
996 :
997 : // Return TRUE if [..., A_MAX] and [B_MIN, ...] can be fused into one interval,
998 : // either because they overlap or because no representable value exists between
999 : // them. The latter is how -0.0 and +0.0 abut: there is nothing in between, so
1000 : // [x, -0.0] U [+0.0, y] is really [x, y].
1001 :
1002 : static bool
1003 10301232 : frange_fusible_p (machine_mode mode, const REAL_VALUE_TYPE &a_max,
1004 : const REAL_VALUE_TYPE &b_min)
1005 : {
1006 10301232 : if (frange_cmp (b_min, a_max) <= 0)
1007 : return true;
1008 9465336 : REAL_VALUE_TYPE next = a_max;
1009 9465336 : frange_nextafter (mode, next, dconstinf);
1010 9465336 : return frange_cmp (b_min, next) <= 0;
1011 : }
1012 :
1013 : // Flush denormal endpoints to the appropriate 0.0.
1014 :
1015 : void
1016 5877754 : frange::flush_denormals_to_zero ()
1017 : {
1018 5877754 : if (undefined_p () || known_isnan ())
1019 0 : return;
1020 :
1021 5877754 : machine_mode mode = TYPE_MODE (type ());
1022 5877754 : frange_pair pairs[MAX_PAIRS];
1023 5877754 : unsigned n = m_num_ranges;
1024 :
1025 : // Flush a denormal endpoint to a zero of the same sign: a +denormal lower
1026 : // bound to +0.0, and a -denormal upper bound to -0.0. Then set_pairs, via
1027 : // canonicalize_zeros, rewrites the sign to whatever the flags make
1028 : // canonical. For example, under !HONOR_SIGNED_ZEROS (-fno-signed-zeros) a
1029 : // range reaching zero must hold both signs of it, so:
1030 : //
1031 : // [ +DENORMAL, 5.0 ] flushes to [ -0.0, 5.0 ]
1032 : //
1033 : // keeping contains_p (-0.0) true; under HONOR_SIGNED_ZEROS the sign stands
1034 : // and it stays [ +0.0, 5.0 ].
1035 11821668 : for (unsigned i = 0; i < n; ++i)
1036 : {
1037 5943914 : pairs[i] = m_pairs[i];
1038 5943914 : if (real_isdenormal (&pairs[i].max, mode) && real_isneg (&pairs[i].max))
1039 4010 : pairs[i].max = dconstm0;
1040 5943914 : if (real_isdenormal (&pairs[i].min, mode) && !real_isneg (&pairs[i].min))
1041 5592 : pairs[i].min = dconst0;
1042 : }
1043 :
1044 5877754 : set_pairs (pairs, n);
1045 : }
1046 :
1047 : // Canonicalize the signed zeros of a sub-range according with what the target
1048 : // and flags want:
1049 : //
1050 : // !MODE_HAS_SIGNED_ZEROS: the mode has no signed zero, so any zero is +0.0.
1051 : //
1052 : // !HONOR_SIGNED_ZEROS: the two zeros are one value, so widen the range to
1053 : // include both signs of it.
1054 : //
1055 : // Otherwise the sign is a real distinction, and we keep it.
1056 :
1057 : void
1058 49335170 : frange::canonicalize_zeros (frange_pair &p)
1059 : {
1060 197340680 : if (!MODE_HAS_SIGNED_ZEROS (TYPE_MODE (m_type)))
1061 : {
1062 0 : if (real_iszero (&p.min, 1))
1063 0 : p.min.sign = 0;
1064 0 : if (real_iszero (&p.max, 1))
1065 0 : p.max.sign = 0;
1066 : }
1067 49335170 : else if (!HONOR_SIGNED_ZEROS (m_type))
1068 : {
1069 1333571 : if (real_iszero (&p.max, 1))
1070 47 : p.max.sign = 0;
1071 1333571 : if (real_iszero (&p.min, 0))
1072 28029 : p.min.sign = 1;
1073 : }
1074 49335170 : }
1075 :
1076 : // Sort, fuse and install the N intervals in PAIRS as this range's sub-ranges.
1077 : //
1078 : // Fusing merges intervals that overlap or abut. If more than MAX_PAIRS still
1079 : // survive, the last slot swallows the surplus.
1080 :
1081 : void
1082 15416423 : frange::set_pairs (frange_pair *pairs, unsigned n)
1083 : {
1084 15416423 : gcc_checking_assert (n > 0);
1085 15416423 : machine_mode mode = TYPE_MODE (m_type);
1086 :
1087 : // Sort by lower bound. N is tiny (at most 2 * MAX_PAIRS).
1088 19751760 : for (unsigned i = 0; i + 1 < n; ++i)
1089 9091418 : for (unsigned j = i + 1; j < n; ++j)
1090 4756081 : if (frange_cmp (pairs[j].min, pairs[i].min) < 0)
1091 727805 : std::swap (pairs[i], pairs[j]);
1092 :
1093 : // Fuse overlapping and abutting intervals.
1094 : unsigned k = 0;
1095 19751760 : for (unsigned i = 1; i < n; ++i)
1096 : {
1097 4335337 : if (frange_fusible_p (mode, pairs[k].max, pairs[i].min))
1098 : {
1099 1148292 : if (frange_cmp (pairs[i].max, pairs[k].max) > 0)
1100 477414 : pairs[k].max = pairs[i].max;
1101 : }
1102 : else
1103 3187045 : pairs[++k] = pairs[i];
1104 : }
1105 15416423 : n = k + 1;
1106 :
1107 : // Only MAX_PAIRS fit. Like irange, keep the first pieces and let the last
1108 : // slot swallow the rest.
1109 15416423 : if (n > MAX_PAIRS)
1110 : {
1111 93439 : pairs[MAX_PAIRS - 1].max = pairs[n - 1].max;
1112 93439 : n = MAX_PAIRS;
1113 : }
1114 :
1115 15416423 : m_kind = VR_RANGE;
1116 15416423 : m_num_ranges = n;
1117 33926356 : for (unsigned i = 0; i < n; ++i)
1118 : {
1119 18509933 : m_pairs[i] = pairs[i];
1120 18509933 : canonicalize_zeros (m_pairs[i]);
1121 : }
1122 :
1123 15416423 : normalize_kind ();
1124 15416423 : if (flag_checking)
1125 15416423 : verify_range ();
1126 15416423 : }
1127 :
1128 : // Set the range to everything except the closed interval [MIN, MAX], which
1129 : // takes two sub-ranges:
1130 : //
1131 : // [-INF, prev (MIN)] U [next (MAX), +INF]
1132 : //
1133 : // Either half falls away when the excluded interval reaches the edge of the
1134 : // domain, and if it covers the entire domain.
1135 :
1136 : void
1137 85765 : frange::set_excluding (tree type, const REAL_VALUE_TYPE &min,
1138 : const REAL_VALUE_TYPE &max, const nan_state &nan)
1139 : {
1140 85765 : gcc_checking_assert (frange_cmp (min, max) <= 0);
1141 :
1142 85765 : machine_mode mode = TYPE_MODE (type);
1143 85765 : REAL_VALUE_TYPE dom_min = frange_val_min (type);
1144 85765 : REAL_VALUE_TYPE dom_max = frange_val_max (type);
1145 85765 : frange_pair pairs[MAX_PAIRS];
1146 85765 : unsigned n = 0;
1147 :
1148 : // PREV is the largest value below MIN, so DOM_MIN <= PREV whenever there is
1149 : // anything below MIN at all. Likewise for NEXT above MAX.
1150 85765 : if (frange_cmp (dom_min, min) < 0)
1151 : {
1152 85096 : REAL_VALUE_TYPE prev = min;
1153 85096 : frange_nextafter (mode, prev, dconstninf);
1154 85096 : pairs[n++] = { dom_min, prev };
1155 : }
1156 85765 : if (frange_cmp (max, dom_max) < 0)
1157 : {
1158 83450 : REAL_VALUE_TYPE next = max;
1159 83450 : frange_nextafter (mode, next, dconstinf);
1160 83450 : pairs[n++] = { next, dom_max };
1161 : }
1162 :
1163 : // The excluded interval covered the entire domain.
1164 85765 : if (n == 0)
1165 : {
1166 0 : if (HONOR_NANS (type) && (nan.pos_p () || nan.neg_p ()))
1167 0 : set_nan (type, nan);
1168 : else
1169 0 : set_undefined ();
1170 0 : return;
1171 : }
1172 :
1173 85765 : set (type, pairs[0].min, pairs[0].max, nan);
1174 85765 : if (n == 2)
1175 : {
1176 82781 : frange tmp;
1177 82781 : tmp.set (type, pairs[1].min, pairs[1].max, nan);
1178 82781 : union_ (tmp);
1179 82781 : }
1180 : }
1181 :
1182 : // Setter for franges.
1183 :
1184 : void
1185 30911002 : frange::set (tree type,
1186 : const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
1187 : const nan_state &nan, value_range_kind kind)
1188 : {
1189 : // VARYING and UNDEFINED go through set_varying() and set_undefined()
1190 : // respectively, like we do for irange.
1191 30911002 : gcc_checking_assert (kind == VR_RANGE || kind == VR_ANTI_RANGE);
1192 30911002 : gcc_checking_assert (!real_isnan (&min) && !real_isnan (&max));
1193 :
1194 30911002 : if (kind == VR_ANTI_RANGE)
1195 : {
1196 85765 : set_excluding (type, min, max, nan);
1197 85765 : return;
1198 : }
1199 :
1200 30825237 : m_kind = kind;
1201 30825237 : m_type = type;
1202 30825237 : m_num_ranges = 1;
1203 30825237 : m_pairs[0].min = min;
1204 30825237 : m_pairs[0].max = max;
1205 30825237 : if (HONOR_NANS (m_type))
1206 : {
1207 29998810 : m_pos_nan = nan.pos_p ();
1208 29998810 : m_neg_nan = nan.neg_p ();
1209 : }
1210 : else
1211 : {
1212 826427 : m_pos_nan = false;
1213 826427 : m_neg_nan = false;
1214 : }
1215 :
1216 30825237 : canonicalize_zeros (m_pairs[0]);
1217 :
1218 : // For -ffinite-math-only we can drop ranges outside the
1219 : // representable numbers to min/max for the type.
1220 30825237 : if (!HONOR_INFINITIES (m_type))
1221 : {
1222 826427 : REAL_VALUE_TYPE min_repr = frange_val_min (m_type);
1223 826427 : REAL_VALUE_TYPE max_repr = frange_val_max (m_type);
1224 826427 : if (real_less (&m_pairs[0].min, &min_repr))
1225 266216 : m_pairs[0].min = min_repr;
1226 560211 : else if (real_less (&max_repr, &m_pairs[0].min))
1227 2 : m_pairs[0].min = max_repr;
1228 826427 : if (real_less (&max_repr, &m_pairs[0].max))
1229 269957 : m_pairs[0].max = max_repr;
1230 556470 : else if (real_less (&m_pairs[0].max, &min_repr))
1231 0 : m_pairs[0].max = min_repr;
1232 : }
1233 :
1234 : // Check for swapped ranges.
1235 30825237 : gcc_checking_assert (real_compare (LE_EXPR, &min, &max));
1236 :
1237 30825237 : normalize_kind ();
1238 : }
1239 :
1240 : // Setter for an frange defaulting the NAN possibility to +-NAN when
1241 : // HONOR_NANS.
1242 :
1243 : void
1244 19148718 : frange::set (tree type,
1245 : const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
1246 : value_range_kind kind)
1247 : {
1248 19148718 : set (type, min, max, nan_state (true), kind);
1249 19148718 : }
1250 :
1251 : void
1252 61 : frange::set (tree min, tree max, value_range_kind kind)
1253 : {
1254 122 : set (TREE_TYPE (min),
1255 61 : *TREE_REAL_CST_PTR (min), *TREE_REAL_CST_PTR (max), kind);
1256 61 : }
1257 :
1258 : // Normalize range to VARYING or UNDEFINED, or vice versa. Return
1259 : // TRUE if anything changed.
1260 : //
1261 : // A range with no known properties can be dropped to VARYING.
1262 : // Similarly, a VARYING with any properties should be dropped to a
1263 : // VR_RANGE. Normalizing ranges upon changing them ensures there is
1264 : // only one representation for a given range.
1265 :
1266 : bool
1267 65150152 : frange::normalize_kind ()
1268 : {
1269 65150152 : if (m_kind == VR_RANGE
1270 60298173 : && m_num_ranges == 1
1271 55974904 : && frange_val_is_min (m_pairs[0].min, m_type)
1272 80886548 : && frange_val_is_max (m_pairs[0].max, m_type))
1273 : {
1274 12015932 : if (!HONOR_NANS (m_type) || (m_pos_nan && m_neg_nan))
1275 : {
1276 10649992 : set_varying (m_type);
1277 10649992 : return true;
1278 : }
1279 : }
1280 53134220 : else if (m_kind == VR_VARYING)
1281 : {
1282 4851776 : if (HONOR_NANS (m_type) && (!m_pos_nan || !m_neg_nan))
1283 : {
1284 1926628 : m_kind = VR_RANGE;
1285 1926628 : m_num_ranges = 1;
1286 1926628 : m_pairs[0].min = frange_val_min (m_type);
1287 1926628 : m_pairs[0].max = frange_val_max (m_type);
1288 1926628 : if (flag_checking)
1289 1926628 : verify_range ();
1290 : return true;
1291 : }
1292 : }
1293 48282444 : else if (m_kind == VR_NAN && !m_pos_nan && !m_neg_nan)
1294 4 : set_undefined ();
1295 : return false;
1296 : }
1297 :
1298 : // Union two ranges when one is known to be a NAN.
1299 :
1300 : bool
1301 157604 : frange::union_nans (const frange &r)
1302 : {
1303 157604 : gcc_checking_assert (known_isnan () || r.known_isnan ());
1304 :
1305 157604 : bool changed = false;
1306 157604 : if (known_isnan () && m_kind != r.m_kind)
1307 : {
1308 42173 : m_kind = r.m_kind;
1309 42173 : m_num_ranges = r.m_num_ranges;
1310 85292 : for (unsigned i = 0; i < r.m_num_ranges; ++i)
1311 43119 : m_pairs[i] = r.m_pairs[i];
1312 : changed = true;
1313 : }
1314 157604 : if (m_pos_nan != r.m_pos_nan || m_neg_nan != r.m_neg_nan)
1315 : {
1316 147103 : m_pos_nan |= r.m_pos_nan;
1317 147103 : m_neg_nan |= r.m_neg_nan;
1318 147103 : changed = true;
1319 : }
1320 157604 : if (changed)
1321 : {
1322 155114 : normalize_kind ();
1323 155114 : return true;
1324 : }
1325 : return false;
1326 : }
1327 :
1328 : bool
1329 27031032 : frange::union_ (const vrange &v)
1330 : {
1331 27031032 : const frange &r = as_a <frange> (v);
1332 :
1333 27031032 : if (r.undefined_p () || varying_p ())
1334 : return false;
1335 26007696 : if (undefined_p () || r.varying_p ())
1336 : {
1337 22591815 : *this = r;
1338 22591815 : return true;
1339 : }
1340 :
1341 : // Combine NAN info.
1342 3415881 : if (known_isnan () || r.known_isnan ())
1343 157604 : return union_nans (r);
1344 :
1345 3258277 : frange save = *this;
1346 3258277 : m_pos_nan |= r.m_pos_nan;
1347 3258277 : m_neg_nan |= r.m_neg_nan;
1348 :
1349 : // Throw both operands' sub-ranges into the pot as set_pairs will
1350 : // canonicalize things and hand us back at most MAX_PAIRS.
1351 : //
1352 : // NOTE: Both operands are already sorted and disjoint, so a merge could
1353 : // combine them in O(n) like irange::union_ rather than have set_pairs
1354 : // re-sort. Not worth it while MAX_PAIRS is tiny; revisit if it grows.
1355 3258277 : frange_pair pairs[2 * MAX_PAIRS];
1356 3258277 : unsigned n = 0;
1357 6851437 : for (unsigned i = 0; i < save.m_num_ranges; ++i)
1358 3593160 : pairs[n++] = save.m_pairs[i];
1359 6574860 : for (unsigned i = 0; i < r.m_num_ranges; ++i)
1360 3316583 : pairs[n++] = r.m_pairs[i];
1361 :
1362 3258277 : set_pairs (pairs, n);
1363 3258277 : return *this != save;
1364 3258277 : }
1365 :
1366 : // Intersect two ranges when one is known to be a NAN.
1367 :
1368 : bool
1369 53230 : frange::intersect_nans (const frange &r)
1370 : {
1371 53230 : gcc_checking_assert (known_isnan () || r.known_isnan ());
1372 :
1373 53230 : m_pos_nan &= r.m_pos_nan;
1374 53230 : m_neg_nan &= r.m_neg_nan;
1375 55474 : if (maybe_isnan ())
1376 51018 : set_nan (m_type, get_nan_state ());
1377 : else
1378 2212 : set_undefined ();
1379 53230 : return true;
1380 : }
1381 :
1382 : bool
1383 21968201 : frange::intersect (const vrange &v)
1384 : {
1385 21968201 : const frange &r = as_a <frange> (v);
1386 :
1387 21968201 : if (undefined_p () || r.varying_p ())
1388 : return false;
1389 8914017 : if (r.undefined_p ())
1390 : {
1391 6291 : set_undefined ();
1392 6291 : return true;
1393 : }
1394 8907726 : if (varying_p ())
1395 : {
1396 2539171 : *this = r;
1397 2539171 : return true;
1398 : }
1399 :
1400 : // Combine NAN info.
1401 6368555 : if (known_isnan () || r.known_isnan ())
1402 53230 : return intersect_nans (r);
1403 :
1404 6315325 : frange save = *this;
1405 6315325 : m_pos_nan &= r.m_pos_nan;
1406 6315325 : m_neg_nan &= r.m_neg_nan;
1407 :
1408 : // Meet every sub-range against every other. Two sorted, disjoint sets of at
1409 : // most MAX_PAIRS each cannot yield more than MAX_PAIRS^2 pieces.
1410 : //
1411 : // NOTE: Since both operands are sorted, a merge-style meet like irange would
1412 : // be O(n) and leave set_pairs nothing to sort. Not worth it while MAX_PAIRS
1413 : // is tiny; revisit if it grows.
1414 6315325 : frange_pair pairs[MAX_PAIRS * MAX_PAIRS];
1415 6315325 : unsigned n = 0;
1416 13333903 : for (unsigned i = 0; i < save.m_num_ranges; ++i)
1417 15129467 : for (unsigned j = 0; j < r.m_num_ranges; ++j)
1418 : {
1419 8110889 : const REAL_VALUE_TYPE &min
1420 8110889 : = frange_max (save.m_pairs[i].min, r.m_pairs[j].min);
1421 8110889 : const REAL_VALUE_TYPE &max
1422 8110889 : = frange_min (save.m_pairs[i].max, r.m_pairs[j].max);
1423 : // A reversed interval means these two do not overlap. This also
1424 : // catches [+0.0, -0.0], which is empty rather than nonsensical.
1425 8110889 : if (frange_cmp (min, max) <= 0)
1426 6898103 : pairs[n++] = { min, max };
1427 : }
1428 :
1429 : // Nothing but a possible NAN survives.
1430 6315325 : if (n == 0)
1431 : {
1432 40588 : if (maybe_isnan ())
1433 29278 : set_nan (m_type, get_nan_state ());
1434 : else
1435 5655 : set_undefined ();
1436 : return true;
1437 : }
1438 :
1439 6280392 : set_pairs (pairs, n);
1440 6280392 : return *this != save;
1441 : }
1442 :
1443 : frange &
1444 38568805 : frange::operator= (const frange &src)
1445 : {
1446 38568805 : m_kind = src.m_kind;
1447 38568805 : m_type = src.m_type;
1448 38568805 : m_num_ranges = src.m_num_ranges;
1449 78782654 : for (unsigned i = 0; i < src.m_num_ranges; ++i)
1450 40213849 : m_pairs[i] = src.m_pairs[i];
1451 38568805 : m_pos_nan = src.m_pos_nan;
1452 38568805 : m_neg_nan = src.m_neg_nan;
1453 :
1454 38568805 : if (flag_checking)
1455 38568805 : verify_range ();
1456 38568805 : return *this;
1457 : }
1458 :
1459 : bool
1460 12433158 : frange::operator== (const frange &src) const
1461 : {
1462 12433158 : if (m_kind == src.m_kind)
1463 : {
1464 11055092 : if (undefined_p ())
1465 : return true;
1466 :
1467 11054926 : if (varying_p ())
1468 295388 : return types_compatible_p (m_type, src.m_type);
1469 :
1470 10759538 : bool nan1 = known_isnan ();
1471 10759538 : bool nan2 = src.known_isnan ();
1472 10759538 : if (nan1 || nan2)
1473 : {
1474 1336 : if (nan1 && nan2)
1475 1336 : return (m_pos_nan == src.m_pos_nan
1476 1336 : && m_neg_nan == src.m_neg_nan);
1477 : return false;
1478 : }
1479 :
1480 10758202 : if (m_num_ranges != src.m_num_ranges)
1481 : return false;
1482 14905314 : for (unsigned i = 0; i < m_num_ranges; ++i)
1483 9219154 : if (!real_identical (&m_pairs[i].min, &src.m_pairs[i].min)
1484 9219154 : || !real_identical (&m_pairs[i].max, &src.m_pairs[i].max))
1485 : return false;
1486 :
1487 5686160 : return (m_pos_nan == src.m_pos_nan
1488 5428623 : && m_neg_nan == src.m_neg_nan
1489 11023385 : && types_compatible_p (m_type, src.m_type));
1490 : }
1491 : return false;
1492 : }
1493 :
1494 : // Return TRUE if range contains R.
1495 :
1496 : bool
1497 1344403 : frange::contains_p (const REAL_VALUE_TYPE &r) const
1498 : {
1499 1344403 : gcc_checking_assert (m_kind != VR_ANTI_RANGE);
1500 :
1501 1344403 : if (undefined_p ())
1502 : return false;
1503 :
1504 1344403 : if (varying_p ())
1505 : return true;
1506 :
1507 982696 : if (real_isnan (&r))
1508 : {
1509 : // No NAN in range.
1510 0 : if (!m_pos_nan && !m_neg_nan)
1511 : return false;
1512 : // Both +NAN and -NAN are present.
1513 0 : if (m_pos_nan && m_neg_nan)
1514 : return true;
1515 0 : return m_neg_nan == r.sign;
1516 : }
1517 982696 : if (known_isnan ())
1518 : return false;
1519 :
1520 1603533 : for (unsigned i = 0; i < m_num_ranges; ++i)
1521 993678 : if (frange_cmp (r, m_pairs[i].min) >= 0
1522 993678 : && frange_cmp (r, m_pairs[i].max) <= 0)
1523 : return true;
1524 :
1525 : return false;
1526 : }
1527 :
1528 : // If range is a singleton, place it in RESULT and return TRUE. If
1529 : // RESULT is NULL, just return TRUE.
1530 : //
1531 : // A NAN can never be a singleton.
1532 :
1533 : bool
1534 27713446 : frange::internal_singleton_p (REAL_VALUE_TYPE *result) const
1535 : {
1536 27713446 : if (m_kind == VR_RANGE
1537 4854814 : && m_num_ranges == 1
1538 31777702 : && real_identical (&m_pairs[0].min, &m_pairs[0].max))
1539 : {
1540 : // Return false for any singleton that may be a NAN.
1541 264999 : if (HONOR_NANS (m_type) && maybe_isnan ())
1542 : return false;
1543 :
1544 920115 : if (MODE_COMPOSITE_P (TYPE_MODE (m_type)))
1545 : {
1546 : // For IBM long doubles, if the value is +-Inf or is exactly
1547 : // representable in double, the other double could be +0.0
1548 : // or -0.0. Since this means there is more than one way to
1549 : // represent a value, return false to avoid propagating it.
1550 : // See libgcc/config/rs6000/ibm-ldouble-format for details.
1551 0 : if (real_isinf (&m_pairs[0].min))
1552 0 : return false;
1553 0 : REAL_VALUE_TYPE r;
1554 0 : real_convert (&r, DFmode, &m_pairs[0].min);
1555 0 : if (real_identical (&r, &m_pairs[0].min))
1556 : return false;
1557 : }
1558 :
1559 131445 : if (result)
1560 0 : *result = m_pairs[0].min;
1561 : return true;
1562 : }
1563 : return false;
1564 : }
1565 :
1566 : bool
1567 27713446 : frange::singleton_p (tree *result) const
1568 : {
1569 27713446 : if (internal_singleton_p ())
1570 : {
1571 131445 : if (result)
1572 17388 : *result = build_real (m_type, m_pairs[0].min);
1573 : return true;
1574 : }
1575 : return false;
1576 : }
1577 :
1578 : bool
1579 0 : frange::singleton_p (REAL_VALUE_TYPE &r) const
1580 : {
1581 0 : return internal_singleton_p (&r);
1582 : }
1583 :
1584 : bool
1585 65312779 : frange::supports_type_p (const_tree type) const
1586 : {
1587 65312779 : return supports_p (type);
1588 : }
1589 :
1590 : void
1591 231257522 : frange::verify_range () const
1592 : {
1593 231257522 : if (!undefined_p ())
1594 76814645 : gcc_checking_assert (HONOR_NANS (m_type) || !maybe_isnan ());
1595 231257522 : switch (m_kind)
1596 : {
1597 156229753 : case VR_UNDEFINED:
1598 156229753 : gcc_checking_assert (!m_type);
1599 : return;
1600 15841023 : case VR_VARYING:
1601 15841023 : gcc_checking_assert (m_type);
1602 15841023 : gcc_checking_assert (m_num_ranges == 1);
1603 15841023 : gcc_checking_assert (frange_val_is_min (m_pairs[0].min, m_type));
1604 15841023 : gcc_checking_assert (frange_val_is_max (m_pairs[0].max, m_type));
1605 15841023 : if (HONOR_NANS (m_type))
1606 15174566 : gcc_checking_assert (m_pos_nan && m_neg_nan);
1607 : else
1608 666457 : gcc_checking_assert (!m_pos_nan && !m_neg_nan);
1609 : return;
1610 58723705 : case VR_RANGE:
1611 58723705 : gcc_checking_assert (m_type);
1612 58723705 : gcc_checking_assert (m_num_ranges >= 1 && m_num_ranges <= MAX_PAIRS);
1613 : break;
1614 463041 : case VR_NAN:
1615 463041 : gcc_checking_assert (m_type);
1616 463041 : gcc_checking_assert (m_pos_nan || m_neg_nan);
1617 : return;
1618 0 : default:
1619 0 : gcc_unreachable ();
1620 : }
1621 :
1622 123413305 : for (unsigned i = 0; i < m_num_ranges; ++i)
1623 : {
1624 : // NANs cannot appear in the endpoints of a range.
1625 64689600 : gcc_checking_assert (!real_isnan (&m_pairs[i].min)
1626 : && !real_isnan (&m_pairs[i].max));
1627 :
1628 : // Make sure we don't have swapped ranges.
1629 : // This also catches [ +0.0, -0.0].
1630 64689600 : gcc_checking_assert (frange_cmp (m_pairs[i].min, m_pairs[i].max) <= 0);
1631 :
1632 : // A zero endpoint must carry its canonical sign. Every producer runs
1633 : // canonicalize_zeros, so a zero bound can only descend from a canonical
1634 : // one.
1635 258758400 : if (!MODE_HAS_SIGNED_ZEROS (TYPE_MODE (m_type)))
1636 0 : gcc_checking_assert (!real_iszero (&m_pairs[i].min, 1)
1637 : && !real_iszero (&m_pairs[i].max, 1));
1638 64689600 : else if (!HONOR_SIGNED_ZEROS (m_type))
1639 1198288 : gcc_checking_assert (!real_iszero (&m_pairs[i].min, 0)
1640 : && !real_iszero (&m_pairs[i].max, 1));
1641 : }
1642 :
1643 : // Sub-ranges are sorted and separated by at least one representable value.
1644 64689600 : for (unsigned i = 1; i < m_num_ranges; ++i)
1645 5965895 : gcc_checking_assert (!frange_fusible_p (TYPE_MODE (m_type),
1646 : m_pairs[i - 1].max,
1647 : m_pairs[i].min));
1648 :
1649 : // If all the properties are clear, we better not span the entire
1650 : // domain, because that would make us varying.
1651 58723705 : if (m_num_ranges == 1 && m_pos_nan && m_neg_nan)
1652 18219901 : gcc_checking_assert (!frange_val_is_min (m_pairs[0].min, m_type)
1653 : || !frange_val_is_max (m_pairs[0].max, m_type));
1654 : }
1655 :
1656 : void
1657 8 : frange::set_nonzero (tree type)
1658 : {
1659 8 : set (type, dconstm0, dconst0, VR_ANTI_RANGE);
1660 8 : }
1661 :
1662 : // Return TRUE if the range contains zero (+0.0 or -0.0).
1663 :
1664 : bool
1665 1001239 : frange::contains_zero_p () const
1666 : {
1667 1001239 : return contains_p (dconst0) || contains_p (dconstm0);
1668 : }
1669 :
1670 : // Set range to [+0.0, +0.0] if honoring signed zeros, or [0.0, 0.0]
1671 : // otherwise.
1672 :
1673 : void
1674 698557 : frange::set_zero (tree type)
1675 : {
1676 698557 : if (HONOR_SIGNED_ZEROS (type))
1677 : {
1678 698488 : set (type, dconstm0, dconst0);
1679 698488 : clear_nan ();
1680 : }
1681 : else
1682 69 : set (type, dconst0, dconst0);
1683 698557 : }
1684 :
1685 : // Return TRUE for any zero regardless of sign.
1686 :
1687 : bool
1688 8234 : frange::zero_p () const
1689 : {
1690 8234 : return (m_kind == VR_RANGE
1691 7223 : && m_num_ranges == 1
1692 6931 : && real_iszero (&m_pairs[0].min)
1693 10150 : && real_iszero (&m_pairs[0].max));
1694 : }
1695 :
1696 : // Set the range to non-negative numbers, that is [+0.0, +INF].
1697 : //
1698 : // The NAN in the resulting range (if HONOR_NANS) has a varying sign
1699 : // as there are no guarantees in IEEE 754 wrt to the sign of a NAN,
1700 : // except for copy, abs, and copysign. It is the responsibility of
1701 : // the caller to set the NAN's sign if desired.
1702 :
1703 : void
1704 39404 : frange::set_nonnegative (tree type)
1705 : {
1706 39404 : set (type, dconst0, frange_val_max (type));
1707 39404 : }
1708 :
1709 : tree
1710 0 : frange::lbound () const
1711 : {
1712 0 : return build_real (type (), lower_bound ());
1713 : }
1714 :
1715 : tree
1716 0 : frange::ubound () const
1717 : {
1718 0 : return build_real (type (), upper_bound ());
1719 : }
1720 :
1721 : /* Widen a single bound of a sub-range by 1ulp (or 0.5ulp) in the direction of
1722 : DIR. */
1723 :
1724 : static REAL_VALUE_TYPE
1725 1047150 : float_widen_bound (tree type, const REAL_VALUE_TYPE &bound,
1726 : const REAL_VALUE_TYPE &dir)
1727 : {
1728 1047150 : REAL_VALUE_TYPE res = bound;
1729 1047150 : if (!real_isfinite (&bound) && real_isneg (&bound) == real_isneg (&dir))
1730 : return res;
1731 643979 : frange_nextafter (TYPE_MODE (type), res, dir);
1732 643979 : if (real_isinf (&res))
1733 : {
1734 : /* For +-DBL_MAX, instead of +-Inf use nexttoward (+-DBL_MAX, +-LDBL_MAX)
1735 : in a hypothetical wider type with the same mantissa precision but
1736 : larger exponent range; it is outside of range of double values, but
1737 : makes it clear it is just one ulp larger rather than infinite amount
1738 : larger. */
1739 90597 : res = real_isneg (&dir) ? dconstm1 : dconst1;
1740 362388 : SET_REAL_EXP (&res, FLOAT_MODE_FORMAT (TYPE_MODE (type))->emax + 1);
1741 : }
1742 643979 : if (!flag_rounding_math
1743 4508278 : && !MODE_COMPOSITE_P (TYPE_MODE (type))
1744 1287862 : && real_isfinite (&bound))
1745 : {
1746 : /* If not -frounding-math nor IBM double double, actually widen
1747 : just by 0.5ulp rather than 1ulp. */
1748 642882 : REAL_VALUE_TYPE tem;
1749 642882 : real_arithmetic (&tem, PLUS_EXPR, &bound, &res);
1750 642882 : real_arithmetic (&res, RDIV_EXPR, &tem, &dconst2);
1751 : }
1752 : return res;
1753 : }
1754 :
1755 : /* Extend the *this range by 1ulp in each direction. For op1_range
1756 : or op2_range of binary operations just computing the inverse
1757 : operation on ranges isn't sufficient. Consider e.g.
1758 : [1., 1.] = op1 + [1., 1.]. op1's range is not [0., 0.], but
1759 : [-0x1.0p-54, 0x1.0p-53] (when not -frounding-math), any value for
1760 : which adding 1. to it results in 1. after rounding to nearest.
1761 : So, for op1_range/op2_range extend the lhs range by 1ulp (or 0.5ulp)
1762 : in each direction. See PR109008 for more details. */
1763 :
1764 : void
1765 473114 : frange::widen (tree type)
1766 : {
1767 473114 : if (known_isnan ())
1768 : return;
1769 : /* Temporarily disable -ffinite-math-only, so that frange::set doesn't
1770 : reduce the range back to real_min_representable (type) as lower bound
1771 : or real_max_representable (type) as upper bound. */
1772 467629 : bool save_flag_finite_math_only = flag_finite_math_only;
1773 467629 : flag_finite_math_only = false;
1774 467629 : unsigned j = 0;
1775 991204 : for (unsigned i = 0; i < num_pairs (); ++i)
1776 : {
1777 523575 : REAL_VALUE_TYPE lb = float_widen_bound (type, lower_bound (i),
1778 : dconstninf);
1779 523575 : REAL_VALUE_TYPE ub = float_widen_bound (type, upper_bound (i),
1780 : dconstinf);
1781 : /* The result of float_widen_bound is often not representable in
1782 : type (could be smaller by 1ulp from representable finite minimum,
1783 : 0.5ulp from some representable finite value or 1ulp larger than
1784 : representable finite maximum). On such values calling e.g.
1785 : frange_nextafter doesn't work properly, so avoid merging the
1786 : pairs with union_ because that calls frange_fusible_p etc.
1787 : This range is often just something that should have the
1788 : real values passed to frange_arithmetic etc. and have the result
1789 : of that converted to something actually representable in the
1790 : type. See PR126641 and PR109008. As lhs should have been
1791 : canonicalized before, the slightly adjusted range should have
1792 : similar properties, just merge pairs where max would be >= than
1793 : min of the next pair. */
1794 523575 : if (j && !real_less (&m_pairs[j - 1].max, &lb))
1795 0 : m_pairs[j - 1].max = ub;
1796 : else
1797 : {
1798 523575 : m_pairs[j].min = lb;
1799 523575 : m_pairs[j].max = ub;
1800 523575 : ++j;
1801 : }
1802 : }
1803 467629 : m_num_ranges = j;
1804 467629 : flag_finite_math_only = save_flag_finite_math_only;
1805 : }
1806 :
1807 : // Here we copy between any two irange's.
1808 :
1809 : irange &
1810 1168120791 : irange::operator= (const irange &src)
1811 : {
1812 1168120791 : int needed = src.num_pairs ();
1813 1168120791 : maybe_resize (needed);
1814 :
1815 1168120791 : unsigned x;
1816 1168120791 : unsigned lim = src.m_num_ranges;
1817 1168120791 : if (lim > m_max_ranges)
1818 668681 : lim = m_max_ranges;
1819 :
1820 3713078837 : for (x = 0; x < lim * 2; ++x)
1821 2544958046 : m_base[x] = src.m_base[x];
1822 :
1823 : // If the range didn't fit, the last range should cover the rest.
1824 1168120791 : if (lim != src.m_num_ranges)
1825 668681 : m_base[x - 1] = src.m_base[src.m_num_ranges * 2 - 1];
1826 :
1827 1168120791 : m_num_ranges = lim;
1828 1168120791 : m_type = src.m_type;
1829 1168120791 : m_kind = src.m_kind;
1830 1168120791 : m_bitmask = src.m_bitmask;
1831 1168120791 : if (m_max_ranges == 1)
1832 21788141 : normalize_kind ();
1833 1168120791 : if (flag_checking)
1834 1168114958 : verify_range ();
1835 1168120791 : return *this;
1836 : }
1837 :
1838 : static value_range_kind
1839 21901680 : get_legacy_range (const irange &r, tree &min, tree &max)
1840 : {
1841 21901680 : if (r.undefined_p ())
1842 : {
1843 103591 : min = NULL_TREE;
1844 103591 : max = NULL_TREE;
1845 103591 : return VR_UNDEFINED;
1846 : }
1847 :
1848 21798089 : tree type = r.type ();
1849 21798089 : if (r.varying_p ())
1850 : {
1851 9331014 : min = wide_int_to_tree (type, r.lower_bound ());
1852 9331014 : max = wide_int_to_tree (type, r.upper_bound ());
1853 9331014 : return VR_VARYING;
1854 : }
1855 :
1856 12467075 : unsigned int precision = TYPE_PRECISION (type);
1857 12467075 : signop sign = TYPE_SIGN (type);
1858 24934150 : if (r.num_pairs () > 1
1859 3323035 : && precision > 1
1860 19113145 : && r.lower_bound () == wi::min_value (precision, sign)
1861 17959140 : && r.upper_bound () == wi::max_value (precision, sign))
1862 : {
1863 586877 : int_range<3> inv (r);
1864 586877 : inv.invert ();
1865 586877 : min = wide_int_to_tree (type, inv.lower_bound (0));
1866 586877 : max = wide_int_to_tree (type, inv.upper_bound (0));
1867 586877 : return VR_ANTI_RANGE;
1868 586877 : }
1869 :
1870 11880198 : min = wide_int_to_tree (type, r.lower_bound ());
1871 11880198 : max = wide_int_to_tree (type, r.upper_bound ());
1872 11880198 : return VR_RANGE;
1873 : }
1874 :
1875 : static value_range_kind
1876 3071405 : get_legacy_range (const prange &r, tree &min, tree &max)
1877 : {
1878 3071405 : if (r.undefined_p ())
1879 : {
1880 0 : min = NULL_TREE;
1881 0 : max = NULL_TREE;
1882 0 : return VR_UNDEFINED;
1883 : }
1884 :
1885 3071405 : tree type = r.type ();
1886 3071405 : if (r.varying_p ())
1887 : {
1888 0 : min = r.lbound ();
1889 0 : max = r.ubound ();
1890 0 : return VR_VARYING;
1891 : }
1892 3071405 : if (r.zero_p ())
1893 : {
1894 2307603 : min = max = r.lbound ();
1895 2307603 : return VR_RANGE;
1896 : }
1897 763802 : prange nonzero (type);
1898 763802 : nonzero.set_nonzero (type);
1899 1527604 : if (r.lower_bound () == nonzero.lower_bound ()
1900 798370 : && r.upper_bound () == nonzero.upper_bound ())
1901 : {
1902 0 : min = max = build_zero_cst (type);
1903 0 : return VR_ANTI_RANGE;
1904 : }
1905 763802 : min = r.lbound ();
1906 763802 : max = r.ubound ();
1907 763802 : return VR_RANGE;
1908 763802 : }
1909 :
1910 : // Given a range in V, return an old-style legacy range consisting of
1911 : // a value_range_kind with a MIN/MAX. This is to maintain
1912 : // compatibility with passes that still depend on VR_ANTI_RANGE, and
1913 : // only works for integers and pointers.
1914 :
1915 : value_range_kind
1916 24973085 : get_legacy_range (const vrange &v, tree &min, tree &max)
1917 : {
1918 24973085 : if (is_a <irange> (v))
1919 21901680 : return get_legacy_range (as_a <irange> (v), min, max);
1920 :
1921 3071405 : return get_legacy_range (as_a <prange> (v), min, max);
1922 : }
1923 :
1924 : /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
1925 : This means adjusting VRTYPE, MIN and MAX representing the case of a
1926 : wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
1927 : as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
1928 : In corner cases where MAX+1 or MIN-1 wraps this will fall back
1929 : to varying.
1930 : This routine exists to ease canonicalization in the case where we
1931 : extract ranges from var + CST op limit. */
1932 :
1933 : void
1934 1491565126 : irange::set (tree type, const wide_int &min, const wide_int &max,
1935 : value_range_kind kind)
1936 : {
1937 1491565126 : unsigned prec = TYPE_PRECISION (type);
1938 1491565126 : signop sign = TYPE_SIGN (type);
1939 1491565126 : wide_int min_value = wi::min_value (prec, sign);
1940 1491565126 : wide_int max_value = wi::max_value (prec, sign);
1941 :
1942 1491565126 : m_type = type;
1943 1491565126 : m_bitmask.set_unknown (prec);
1944 :
1945 1491565126 : if (kind == VR_RANGE)
1946 : {
1947 1428378107 : m_base[0] = min;
1948 1428378107 : m_base[1] = max;
1949 1428378107 : m_num_ranges = 1;
1950 1908667310 : if (min == min_value && max == max_value)
1951 33813806 : m_kind = VR_VARYING;
1952 : else
1953 1394564301 : m_kind = VR_RANGE;
1954 : }
1955 : else
1956 : {
1957 63187019 : gcc_checking_assert (kind == VR_ANTI_RANGE);
1958 63187019 : gcc_checking_assert (m_max_ranges > 1);
1959 :
1960 63187019 : m_kind = VR_UNDEFINED;
1961 63187019 : m_num_ranges = 0;
1962 63187019 : wi::overflow_type ovf;
1963 63187019 : wide_int lim;
1964 63187019 : if (sign == SIGNED)
1965 30273564 : lim = wi::add (min, -1, sign, &ovf);
1966 : else
1967 32914455 : lim = wi::sub (min, 1, sign, &ovf);
1968 :
1969 63187019 : if (!ovf)
1970 : {
1971 45500623 : m_kind = VR_RANGE;
1972 45500623 : m_base[0] = min_value;
1973 45500623 : m_base[1] = lim;
1974 45500623 : ++m_num_ranges;
1975 : }
1976 63187019 : if (sign == SIGNED)
1977 30273564 : lim = wi::sub (max, -1, sign, &ovf);
1978 : else
1979 32914455 : lim = wi::add (max, 1, sign, &ovf);
1980 63187019 : if (!ovf)
1981 : {
1982 63185415 : m_kind = VR_RANGE;
1983 63185415 : m_base[m_num_ranges * 2] = lim;
1984 63185415 : m_base[m_num_ranges * 2 + 1] = max_value;
1985 63185415 : ++m_num_ranges;
1986 : }
1987 63187019 : }
1988 :
1989 1491565126 : if (flag_checking)
1990 1491560181 : verify_range ();
1991 1491565126 : }
1992 :
1993 : void
1994 245739267 : irange::set (tree min, tree max, value_range_kind kind)
1995 : {
1996 245739267 : if (POLY_INT_CST_P (min) || POLY_INT_CST_P (max))
1997 : {
1998 : set_varying (TREE_TYPE (min));
1999 : return;
2000 : }
2001 :
2002 245739267 : gcc_checking_assert (TREE_CODE (min) == INTEGER_CST);
2003 245739267 : gcc_checking_assert (TREE_CODE (max) == INTEGER_CST);
2004 :
2005 245740650 : return set (TREE_TYPE (min), wi::to_wide (min), wi::to_wide (max), kind);
2006 : }
2007 :
2008 : // Check the validity of the range.
2009 :
2010 : void
2011 4974384325 : irange::verify_range () const
2012 : {
2013 4974384325 : gcc_checking_assert (m_discriminator == VR_IRANGE);
2014 4974384325 : if (m_kind == VR_UNDEFINED)
2015 : {
2016 248862 : gcc_checking_assert (m_num_ranges == 0);
2017 : return;
2018 : }
2019 4974135463 : gcc_checking_assert (supports_p (type ()));
2020 4974135463 : gcc_checking_assert (m_num_ranges <= m_max_ranges);
2021 :
2022 : // Legacy allowed these to represent VARYING for unknown types.
2023 : // Leave this in for now, until all users are converted. Eventually
2024 : // we should abort in set_varying.
2025 4974135463 : if (m_kind == VR_VARYING && m_type == error_mark_node)
2026 : return;
2027 :
2028 4974135463 : unsigned prec = TYPE_PRECISION (m_type);
2029 4974135463 : if (m_kind == VR_VARYING)
2030 : {
2031 242767449 : gcc_checking_assert (m_bitmask.unknown_p ());
2032 242767449 : gcc_checking_assert (m_num_ranges == 1);
2033 242767449 : gcc_checking_assert (varying_compatible_p ());
2034 242767449 : gcc_checking_assert (lower_bound ().get_precision () == prec);
2035 242767449 : gcc_checking_assert (upper_bound ().get_precision () == prec);
2036 242767449 : return;
2037 : }
2038 4731368014 : gcc_checking_assert (m_num_ranges != 0);
2039 4731368014 : gcc_checking_assert (!varying_compatible_p ());
2040 12034176949 : for (unsigned i = 0; i < m_num_ranges; ++i)
2041 : {
2042 7302808935 : wide_int lb = lower_bound (i);
2043 7302808935 : wide_int ub = upper_bound (i);
2044 7302808935 : gcc_checking_assert (lb.get_precision () == prec);
2045 7302808935 : gcc_checking_assert (ub.get_precision () == prec);
2046 7302808935 : int c = wi::cmp (lb, ub, TYPE_SIGN (m_type));
2047 7302808935 : gcc_checking_assert (c == 0 || c == -1);
2048 : // Previous UB should be lower than LB
2049 7302808935 : if (i > 0)
2050 5142881842 : gcc_checking_assert (wi::lt_p (upper_bound (i - 1),
2051 : lb,
2052 : TYPE_SIGN (m_type)));
2053 7305088758 : }
2054 4731368014 : m_bitmask.verify_mask ();
2055 : }
2056 :
2057 : bool
2058 293306276 : irange::operator== (const irange &other) const
2059 : {
2060 293306276 : if (m_num_ranges != other.m_num_ranges)
2061 : return false;
2062 :
2063 269646436 : if (m_num_ranges == 0)
2064 : return true;
2065 :
2066 269473182 : signop sign1 = TYPE_SIGN (type ());
2067 269473182 : signop sign2 = TYPE_SIGN (other.type ());
2068 :
2069 358926850 : for (unsigned i = 0; i < m_num_ranges; ++i)
2070 : {
2071 285851492 : widest_int lb = widest_int::from (lower_bound (i), sign1);
2072 285851492 : widest_int ub = widest_int::from (upper_bound (i), sign1);
2073 285851492 : widest_int lb_other = widest_int::from (other.lower_bound (i), sign2);
2074 285851492 : widest_int ub_other = widest_int::from (other.upper_bound (i), sign2);
2075 464279673 : if (lb != lb_other || ub != ub_other)
2076 196397824 : return false;
2077 285852869 : }
2078 :
2079 73075358 : irange_bitmask bm1 = get_bitmask ();
2080 73075358 : irange_bitmask bm2 = other.get_bitmask ();
2081 73075358 : widest_int tmp1 = widest_int::from (bm1.mask (), sign1);
2082 73075358 : widest_int tmp2 = widest_int::from (bm2.mask (), sign2);
2083 73075358 : if (tmp1 != tmp2)
2084 : return false;
2085 73025390 : if (bm1.unknown_p ())
2086 : return true;
2087 41315476 : tmp1 = widest_int::from (bm1.value (), sign1);
2088 41315476 : tmp2 = widest_int::from (bm2.value (), sign2);
2089 41315428 : return tmp1 == tmp2;
2090 73075532 : }
2091 :
2092 : /* If range is a singleton, place it in RESULT and return TRUE. */
2093 :
2094 : bool
2095 765565831 : irange::singleton_p (tree *result) const
2096 : {
2097 1418654884 : if (num_pairs () == 1 && lower_bound () == upper_bound ())
2098 : {
2099 44386726 : if (result)
2100 10166381 : *result = wide_int_to_tree (type (), lower_bound ());
2101 : return true;
2102 : }
2103 : return false;
2104 : }
2105 :
2106 : bool
2107 548115629 : irange::singleton_p (wide_int &w) const
2108 : {
2109 740380653 : if (num_pairs () == 1 && lower_bound () == upper_bound ())
2110 : {
2111 20228286 : w = lower_bound ();
2112 20228286 : return true;
2113 : }
2114 : return false;
2115 : }
2116 :
2117 : /* Return 1 if CST is inside value range.
2118 : 0 if CST is not inside value range.
2119 :
2120 : Benchmark compile/20001226-1.c compilation time after changing this
2121 : function. */
2122 :
2123 : bool
2124 256050005 : irange::contains_p (const wide_int &cst) const
2125 : {
2126 256050005 : if (undefined_p ())
2127 : return false;
2128 :
2129 : // Check if the known bits in bitmask exclude CST.
2130 255970351 : if (!m_bitmask.member_p (cst))
2131 : return false;
2132 :
2133 254987875 : signop sign = TYPE_SIGN (type ());
2134 273088234 : for (unsigned r = 0; r < m_num_ranges; ++r)
2135 : {
2136 272122049 : if (wi::lt_p (cst, lower_bound (r), sign))
2137 : return false;
2138 141509150 : if (wi::le_p (cst, upper_bound (r), sign))
2139 : return true;
2140 : }
2141 :
2142 : return false;
2143 : }
2144 :
2145 : // Perform an efficient union with R when both ranges have only a single pair.
2146 : // Excluded are VARYING and UNDEFINED ranges.
2147 :
2148 : bool
2149 118239123 : irange::irange_single_pair_union (const irange &r)
2150 : {
2151 118239123 : gcc_checking_assert (!undefined_p () && !varying_p ());
2152 118239123 : gcc_checking_assert (!r.undefined_p () && !varying_p ());
2153 :
2154 118239123 : signop sign = TYPE_SIGN (m_type);
2155 : // Check if current lower bound is also the new lower bound.
2156 118239123 : if (wi::le_p (m_base[0], r.m_base[0], sign))
2157 : {
2158 : // If current upper bound is new upper bound, we're done.
2159 104051205 : if (wi::le_p (r.m_base[1], m_base[1], sign))
2160 15308704 : return union_bitmask (r);
2161 : // Otherwise R has the new upper bound.
2162 : // Check for overlap/touching ranges, or single target range.
2163 177485002 : if (m_max_ranges == 1
2164 266227495 : || (widest_int::from (m_base[1], sign) + 1
2165 354969992 : >= widest_int::from (r.m_base[0], TYPE_SIGN (r.m_type))))
2166 26768442 : m_base[1] = r.m_base[1];
2167 : else
2168 : {
2169 : // This is a dual range result.
2170 61974059 : m_base[2] = r.m_base[0];
2171 61974059 : m_base[3] = r.m_base[1];
2172 61974059 : m_num_ranges = 2;
2173 : }
2174 : // The range has been altered, so normalize it even if nothing
2175 : // changed in the mask.
2176 88742501 : if (!union_bitmask (r))
2177 87807370 : normalize_kind ();
2178 88742501 : if (flag_checking)
2179 88742369 : verify_range ();
2180 : return true;
2181 : }
2182 :
2183 : // Set the new lower bound to R's lower bound.
2184 14187918 : wide_int lb = m_base[0];
2185 14187918 : m_base[0] = r.m_base[0];
2186 :
2187 : // If R fully contains THIS range, just set the upper bound.
2188 14187918 : if (wi::ge_p (r.m_base[1], m_base[1], sign))
2189 1440398 : m_base[1] = r.m_base[1];
2190 : // Check for overlapping ranges, or target limited to a single range.
2191 25495040 : else if (m_max_ranges == 1
2192 38242560 : || (widest_int::from (r.m_base[1], TYPE_SIGN (r.m_type)) + 1
2193 50990080 : >= widest_int::from (lb, sign)))
2194 : ;
2195 : else
2196 : {
2197 : // Left with 2 pairs.
2198 6651428 : m_num_ranges = 2;
2199 6651428 : m_base[2] = lb;
2200 6651428 : m_base[3] = m_base[1];
2201 6651428 : m_base[1] = r.m_base[1];
2202 : }
2203 : // The range has been altered, so normalize it even if nothing
2204 : // changed in the mask.
2205 14187918 : if (!union_bitmask (r))
2206 13265609 : normalize_kind ();
2207 14187918 : if (flag_checking)
2208 14187907 : verify_range ();
2209 14187918 : return true;
2210 14187918 : }
2211 :
2212 : // Append R to this range, knowing that R occurs after all of these subranges.
2213 : // Return TRUE as something must have changed.
2214 :
2215 : bool
2216 157601345 : irange::union_append (const irange &r)
2217 : {
2218 : // Check if the first range in R is an immediate successor to the last
2219 : // range, thus requiring a merge.
2220 157601345 : signop sign = TYPE_SIGN (m_type);
2221 157601345 : wide_int lb = r.lower_bound ();
2222 157601345 : wide_int ub = upper_bound ();
2223 157601345 : unsigned start = 0;
2224 472804044 : if (widest_int::from (ub, sign) + 1
2225 472804035 : == widest_int::from (lb, sign))
2226 : {
2227 832262 : m_base[m_num_ranges * 2 - 1] = r.m_base[1];
2228 832262 : start = 1;
2229 : }
2230 157601345 : maybe_resize (m_num_ranges + r.m_num_ranges - start);
2231 472115513 : for ( ; start < r.m_num_ranges; start++)
2232 : {
2233 : // Merge the last ranges if it exceeds the maximum size.
2234 157661917 : if (m_num_ranges + 1 > m_max_ranges)
2235 : {
2236 749094 : m_base[m_max_ranges * 2 - 1] = r.m_base[r.m_num_ranges * 2 - 1];
2237 749094 : break;
2238 : }
2239 156912823 : m_base[m_num_ranges * 2] = r.m_base[start * 2];
2240 156912823 : m_base[m_num_ranges * 2 + 1] = r.m_base[start * 2 + 1];
2241 156912823 : m_num_ranges++;
2242 : }
2243 :
2244 157601345 : if (!union_bitmask (r))
2245 157551558 : normalize_kind ();
2246 157601345 : if (flag_checking)
2247 157601345 : verify_range ();
2248 157601345 : return true;
2249 157601345 : }
2250 :
2251 : // Return TRUE if anything changes.
2252 :
2253 : bool
2254 419973556 : irange::union_ (const vrange &v)
2255 : {
2256 419973556 : const irange &r = as_a <irange> (v);
2257 :
2258 419973556 : if (r.undefined_p ())
2259 : return false;
2260 :
2261 417702662 : if (undefined_p ())
2262 : {
2263 97376426 : operator= (r);
2264 97376426 : if (flag_checking)
2265 97376032 : verify_range ();
2266 : return true;
2267 : }
2268 :
2269 320326236 : if (varying_p ())
2270 : return false;
2271 :
2272 310586269 : if (r.varying_p ())
2273 : {
2274 6797785 : set_varying (type ());
2275 6797785 : return true;
2276 : }
2277 :
2278 : // Special case one range union one range.
2279 303788484 : if (m_num_ranges == 1 && r.m_num_ranges == 1)
2280 118239123 : return irange_single_pair_union (r);
2281 :
2282 185549361 : signop sign = TYPE_SIGN (m_type);
2283 : // Check for an append to the end.
2284 556648083 : if (m_kind == VR_RANGE && wi::gt_p (r.lower_bound (), upper_bound (), sign))
2285 157601345 : return union_append (r);
2286 :
2287 : // If this ranges fully contains R, then we need do nothing.
2288 27948016 : if (irange_contains_p (r))
2289 4523900 : return union_bitmask (r);
2290 :
2291 : // Do not worry about merging and such by reserving twice as many
2292 : // pairs as needed, and then simply sort the 2 ranges into this
2293 : // intermediate form.
2294 : //
2295 : // The intermediate result will have the property that the beginning
2296 : // of each range is <= the beginning of the next range. There may
2297 : // be overlapping ranges at this point. I.e. this would be valid
2298 : // [-20, 10], [-10, 0], [0, 20], [40, 90] as it satisfies this
2299 : // constraint : -20 < -10 < 0 < 40. When the range is rebuilt into r,
2300 : // the merge is performed.
2301 : //
2302 : // [Xi,Yi]..[Xn,Yn] U [Xj,Yj]..[Xm,Ym] --> [Xk,Yk]..[Xp,Yp]
2303 23424116 : auto_vec<wide_int, 20> res (m_num_ranges * 2 + r.m_num_ranges * 2);
2304 23424116 : unsigned i = 0, j = 0, k = 0;
2305 :
2306 103189315 : while (i < m_num_ranges * 2 && j < r.m_num_ranges * 2)
2307 : {
2308 : // lower of Xi and Xj is the lowest point.
2309 112682166 : if (widest_int::from (m_base[i], sign)
2310 169023249 : <= widest_int::from (r.m_base[j], sign))
2311 : {
2312 29832298 : res.quick_push (m_base[i]);
2313 29832298 : res.quick_push (m_base[i + 1]);
2314 29832298 : k += 2;
2315 29832298 : i += 2;
2316 : }
2317 : else
2318 : {
2319 26508785 : res.quick_push (r.m_base[j]);
2320 26508785 : res.quick_push (r.m_base[j + 1]);
2321 26508785 : k += 2;
2322 26508785 : j += 2;
2323 : }
2324 : }
2325 47026516 : for ( ; i < m_num_ranges * 2; i += 2)
2326 : {
2327 23602400 : res.quick_push (m_base[i]);
2328 23602400 : res.quick_push (m_base[i + 1]);
2329 23602400 : k += 2;
2330 : }
2331 30734824 : for ( ; j < r.m_num_ranges * 2; j += 2)
2332 : {
2333 7310708 : res.quick_push (r.m_base[j]);
2334 7310708 : res.quick_push (r.m_base[j + 1]);
2335 7310708 : k += 2;
2336 : }
2337 :
2338 : // Now normalize the vector removing any overlaps.
2339 : i = 2;
2340 87254191 : for (j = 2; j < k ; j += 2)
2341 : {
2342 : // Current upper+1 is >= lower bound next pair, then we merge ranges.
2343 191490239 : if (widest_int::from (res[i - 1], sign) + 1
2344 191490225 : >= widest_int::from (res[j], sign))
2345 : {
2346 : // New upper bounds is greater of current or the next one.
2347 57090038 : if (widest_int::from (res[j + 1], sign)
2348 85635057 : > widest_int::from (res[i - 1], sign))
2349 21317550 : res[i - 1] = res[j + 1];
2350 : }
2351 : else
2352 : {
2353 : // This is a new distinct range, but no point in copying it
2354 : // if it is already in the right place.
2355 35285056 : if (i != j)
2356 : {
2357 12161968 : res[i++] = res[j];
2358 12161968 : res[i++] = res[j + 1];
2359 : }
2360 : else
2361 23123088 : i += 2;
2362 : }
2363 : }
2364 :
2365 : // At this point, the vector should have i ranges, none overlapping.
2366 : // Now it simply needs to be copied, and if there are too many
2367 : // ranges, merge some. We wont do any analysis as to what the
2368 : // "best" merges are, simply combine the final ranges into one.
2369 23424116 : maybe_resize (i / 2);
2370 23424116 : if (i > m_max_ranges * 2)
2371 : {
2372 1814 : res[m_max_ranges * 2 - 1] = res[i - 1];
2373 1814 : i = m_max_ranges * 2;
2374 : }
2375 :
2376 140838832 : for (j = 0; j < i ; j++)
2377 117414716 : m_base[j] = res [j];
2378 23424116 : m_num_ranges = i / 2;
2379 :
2380 23424116 : m_kind = VR_RANGE;
2381 : // The range has been altered, so normalize it even if nothing
2382 : // changed in the mask.
2383 23424116 : if (!union_bitmask (r))
2384 22518427 : normalize_kind ();
2385 23424116 : if (flag_checking)
2386 23424074 : verify_range ();
2387 23424116 : return true;
2388 23424116 : }
2389 :
2390 : // Return TRUE if THIS fully contains R. No undefined or varying cases.
2391 :
2392 : bool
2393 199481571 : irange::irange_contains_p (const irange &r) const
2394 : {
2395 199481571 : gcc_checking_assert (!undefined_p () && !varying_p ());
2396 199481571 : gcc_checking_assert (!r.undefined_p () && !varying_p ());
2397 :
2398 : // Check singletons directly which will include any bitmasks.
2399 199481571 : wide_int rl;
2400 199481571 : if (r.singleton_p (rl))
2401 14375362 : return contains_p (rl);
2402 :
2403 : // In order for THIS to fully contain R, all of the pairs within R must
2404 : // be fully contained by the pairs in this object.
2405 185106209 : signop sign = TYPE_SIGN (m_type);
2406 185106209 : unsigned ri = 0;
2407 185106209 : unsigned i = 0;
2408 185106209 : rl = r.m_base[0];
2409 185106209 : wide_int ru = r.m_base[1];
2410 185106209 : wide_int l = m_base[0];
2411 185106209 : wide_int u = m_base[1];
2412 477995593 : while (1)
2413 : {
2414 : // If r is contained within this range, move to the next R
2415 477995593 : if (wi::ge_p (rl, l, sign)
2416 477995593 : && wi::le_p (ru, u, sign))
2417 : {
2418 : // This pair is OK, Either done, or bump to the next.
2419 222240720 : if (++ri >= r.num_pairs ())
2420 : return true;
2421 144520154 : rl = r.m_base[ri * 2];
2422 144520154 : ru = r.m_base[ri * 2 + 1];
2423 144520154 : continue;
2424 : }
2425 : // Otherwise, check if this's pair occurs before R's.
2426 255754873 : if (wi::lt_p (u, rl, sign))
2427 : {
2428 : // There's still at least one pair of R left.
2429 149260411 : if (++i >= num_pairs ())
2430 : return false;
2431 148369230 : l = m_base[i * 2];
2432 148369230 : u = m_base[i * 2 + 1];
2433 148369230 : continue;
2434 : }
2435 : return false;
2436 : }
2437 : return false;
2438 185111927 : }
2439 :
2440 :
2441 : // Return TRUE if anything changes.
2442 :
2443 : bool
2444 932325200 : irange::intersect (const vrange &v)
2445 : {
2446 932325200 : const irange &r = as_a <irange> (v);
2447 932325200 : gcc_checking_assert (undefined_p () || r.undefined_p ()
2448 : || range_compatible_p (type (), r.type ()));
2449 :
2450 932325200 : if (undefined_p ())
2451 : return false;
2452 931219668 : if (r.undefined_p ())
2453 : {
2454 478870 : set_undefined ();
2455 478870 : return true;
2456 : }
2457 930740798 : if (r.varying_p ())
2458 : return false;
2459 647378901 : if (varying_p ())
2460 : {
2461 91092727 : operator= (r);
2462 91092727 : return true;
2463 : }
2464 :
2465 556286174 : if (r.num_pairs () == 1)
2466 : {
2467 384617769 : bool res = intersect (r.lower_bound (), r.upper_bound ());
2468 384615342 : if (undefined_p ())
2469 : return true;
2470 :
2471 356606031 : res |= intersect_bitmask (r);
2472 356606031 : if (res)
2473 128209691 : normalize_kind ();
2474 : return res;
2475 : }
2476 :
2477 : // If either range is a singleton and the other range does not contain
2478 : // it, the result is undefined.
2479 171670832 : wide_int val;
2480 173377000 : if ((singleton_p (val) && !r.contains_p (val))
2481 173239723 : || (r.singleton_p (val) && !contains_p (val)))
2482 : {
2483 137277 : set_undefined ();
2484 137277 : return true;
2485 : }
2486 :
2487 : // If R fully contains this, then intersection will change nothing.
2488 171533555 : if (r.irange_contains_p (*this))
2489 76417442 : return intersect_bitmask (r);
2490 :
2491 : // ?? We could probably come up with something smarter than the
2492 : // worst case scenario here.
2493 95116113 : int needed = num_pairs () + r.num_pairs ();
2494 95116113 : maybe_resize (needed);
2495 :
2496 95116113 : signop sign = TYPE_SIGN (m_type);
2497 95116113 : unsigned bld_pair = 0;
2498 95116113 : unsigned bld_lim = m_max_ranges;
2499 95116113 : int_range_max r2 (*this);
2500 95116113 : unsigned r2_lim = r2.num_pairs ();
2501 95116113 : unsigned i2 = 0;
2502 95116113 : bool need_snapping = !m_bitmask.unknown_p ();
2503 278849981 : for (unsigned i = 0; i < r.num_pairs (); )
2504 : {
2505 : // If r1's upper is < r2's lower, we can skip r1's pair.
2506 247711512 : wide_int ru = r.m_base[i * 2 + 1];
2507 247711512 : wide_int r2l = r2.m_base[i2 * 2];
2508 247711512 : if (wi::lt_p (ru, r2l, sign))
2509 : {
2510 24204233 : i++;
2511 24204233 : continue;
2512 : }
2513 : // Likewise, skip r2's pair if its excluded.
2514 223507279 : wide_int r2u = r2.m_base[i2 * 2 + 1];
2515 223507279 : wide_int rl = r.m_base[i * 2];
2516 223507279 : if (wi::lt_p (r2u, rl, sign))
2517 : {
2518 23672479 : i2++;
2519 23672479 : if (i2 < r2_lim)
2520 18925002 : continue;
2521 : // No more r2, break.
2522 : break;
2523 : }
2524 :
2525 : // Must be some overlap. Find the highest of the lower bounds,
2526 : // and set it, unless the build limits lower bounds is already
2527 : // set.
2528 199834800 : if (bld_pair < bld_lim)
2529 : {
2530 199468689 : if (wi::ge_p (rl, r2l, sign))
2531 168485431 : m_base[bld_pair * 2] = rl;
2532 : else
2533 30983258 : m_base[bld_pair * 2] = r2l;
2534 : }
2535 : else
2536 : // Decrease the index to use the existing lower bound, and
2537 : // set a new upper for this pair.
2538 366111 : bld_pair--;
2539 :
2540 : // Changes to false if the last value in i2's range is consumed.
2541 199834800 : bool more = true;
2542 : // ...and choose the lower of the upper bounds.
2543 199834800 : if (wi::le_p (ru, r2u, sign))
2544 : {
2545 127026719 : m_base[bld_pair * 2 + 1] = ru;
2546 : // Move past the r1 pair and keep trying.
2547 127026719 : i++;
2548 : }
2549 : else
2550 : {
2551 72808081 : m_base[bld_pair * 2 + 1] = r2u;
2552 72808081 : i2++;
2553 : // No more r2, break the loop when done.
2554 72808081 : if (i2 >= r2_lim)
2555 59230167 : more = false;
2556 : }
2557 : // Now snap these ranges to the bitmask, if there is one.
2558 199834800 : if (need_snapping)
2559 : {
2560 55506723 : bool ovf;
2561 55506723 : wide_int lb, ub;
2562 55506723 : if (snap (m_base[bld_pair * 2], m_base[bld_pair * 2 + 1],
2563 : lb, ub, ovf))
2564 : {
2565 : // If the new subrange does not fit the mask, skip it.
2566 1195481 : if (ovf)
2567 : {
2568 10063 : if (!more)
2569 : break;
2570 10063 : continue;
2571 : }
2572 : // Otherwise adjust the pair.
2573 1185418 : m_base[bld_pair * 2] = lb;
2574 1185418 : m_base[bld_pair * 2 + 1] = ub;
2575 : }
2576 55506723 : }
2577 : // Current pair now satisfies any mask, ready for another pair.
2578 199824737 : bld_pair++;
2579 199824737 : if (!more)
2580 : break;
2581 266652384 : }
2582 :
2583 : // At the exit of this loop, it is one of 2 things:
2584 : // ran out of r1, or r2, but either means we are done.
2585 95116113 : m_num_ranges = bld_pair;
2586 95116113 : if (m_num_ranges == 0)
2587 : {
2588 109678 : set_undefined ();
2589 109678 : return true;
2590 : }
2591 :
2592 95006435 : m_kind = VR_RANGE;
2593 : // The range has been altered, so normalize it even if nothing
2594 : // changed in the mask.
2595 95006435 : if (!intersect_bitmask (r))
2596 87462790 : normalize_kind ();
2597 95006435 : if (flag_checking)
2598 95006413 : verify_range ();
2599 : return true;
2600 266786945 : }
2601 :
2602 :
2603 : // Multirange intersect for a specified wide_int [lb, ub] range.
2604 : // Return TRUE if intersect changed anything.
2605 : //
2606 : // NOTE: It is the caller's responsibility to intersect the mask.
2607 :
2608 : bool
2609 384615342 : irange::intersect (const wide_int& lb, const wide_int& ub)
2610 : {
2611 : // Undefined remains undefined.
2612 384615342 : if (undefined_p ())
2613 : return false;
2614 :
2615 384615342 : tree range_type = type();
2616 384615342 : signop sign = TYPE_SIGN (range_type);
2617 :
2618 384615342 : gcc_checking_assert (TYPE_PRECISION (range_type) == wi::get_precision (lb));
2619 384615342 : gcc_checking_assert (TYPE_PRECISION (range_type) == wi::get_precision (ub));
2620 :
2621 : // If this range is fully contained, then intersection will do nothing.
2622 769230684 : if (wi::ge_p (lower_bound (), lb, sign)
2623 688453883 : && wi::le_p (upper_bound (), ub, sign))
2624 : return false;
2625 :
2626 142716781 : unsigned bld_index = 0;
2627 142716781 : unsigned pair_lim = num_pairs ();
2628 216794453 : for (unsigned i = 0; i < pair_lim; i++)
2629 : {
2630 158515027 : wide_int pairl = m_base[i * 2];
2631 158515027 : wide_int pairu = m_base[i * 2 + 1];
2632 : // Once UB is less than a pairs lower bound, we're done.
2633 158515027 : if (wi::lt_p (ub, pairl, sign))
2634 : break;
2635 : // if LB is greater than this pairs upper, this pair is excluded.
2636 136381021 : if (wi::lt_p (pairu, lb, sign))
2637 19737548 : continue;
2638 :
2639 : // Must be some overlap. Find the highest of the lower bounds,
2640 : // and set it
2641 116643473 : if (wi::gt_p (lb, pairl, sign))
2642 63324253 : m_base[bld_index * 2] = lb;
2643 : else
2644 53319220 : m_base[bld_index * 2] = pairl;
2645 :
2646 : // ...and choose the lower of the upper bounds and if the base pair
2647 : // has the lower upper bound, need to check next pair too.
2648 116643473 : if (wi::lt_p (ub, pairu, sign))
2649 : {
2650 62303349 : m_base[bld_index++ * 2 + 1] = ub;
2651 62303349 : break;
2652 : }
2653 : else
2654 54340124 : m_base[bld_index++ * 2 + 1] = pairu;
2655 158515552 : }
2656 :
2657 142716781 : m_num_ranges = bld_index;
2658 142716781 : if (m_num_ranges == 0)
2659 : {
2660 28009311 : set_undefined ();
2661 28009311 : return true;
2662 : }
2663 :
2664 114707470 : m_kind = VR_RANGE;
2665 : // The caller must normalize and verify the range, as the bitmask
2666 : // still needs to be handled.
2667 114707470 : return true;
2668 : }
2669 :
2670 :
2671 : // Signed 1-bits are strange. You can't subtract 1, because you can't
2672 : // represent the number 1. This works around that for the invert routine.
2673 :
2674 : static wide_int inline
2675 62731279 : subtract_one (const wide_int &x, tree type, wi::overflow_type &overflow)
2676 : {
2677 62731279 : if (TYPE_SIGN (type) == SIGNED)
2678 38406302 : return wi::add (x, -1, SIGNED, &overflow);
2679 : else
2680 24324977 : return wi::sub (x, 1, UNSIGNED, &overflow);
2681 : }
2682 :
2683 : // The analogous function for adding 1.
2684 :
2685 : static wide_int inline
2686 69242965 : add_one (const wide_int &x, tree type, wi::overflow_type &overflow)
2687 : {
2688 69242965 : if (TYPE_SIGN (type) == SIGNED)
2689 32505806 : return wi::sub (x, -1, SIGNED, &overflow);
2690 : else
2691 36737159 : return wi::add (x, 1, UNSIGNED, &overflow);
2692 : }
2693 :
2694 : // Return the inverse of a range. Return false if thre is no invert
2695 : // calculatable.
2696 :
2697 : bool
2698 70225585 : irange::invert ()
2699 : {
2700 : // UNDEFINED cannot be converted to varying because there is no type
2701 : // assocaited. Callers need to handle these cases.
2702 : // Its also ambiguous.. VARYING inverted could also arguably be VARYING
2703 : // in some cases. Likewise with UNDEFINED.
2704 70225585 : if (undefined_p () || varying_p ())
2705 : return false;
2706 :
2707 : // We always need one more set of bounds to represent an inverse, so
2708 : // if we're at the limit, we can't properly represent things.
2709 : //
2710 : // For instance, to represent the inverse of a 2 sub-range set
2711 : // [5, 10][20, 30], we would need a 3 sub-range set
2712 : // [-MIN, 4][11, 19][31, MAX].
2713 : //
2714 : // In this case, return false.
2715 : //
2716 : // However, if any of the extremes of the range are -MIN/+MAX, we
2717 : // know we will not need an extra bound. For example:
2718 : //
2719 : // INVERT([-MIN,20][30,40]) => [21,29][41,+MAX]
2720 : // INVERT([-MIN,20][30,MAX]) => [21,29]
2721 70225585 : tree ttype = type ();
2722 70225585 : unsigned prec = TYPE_PRECISION (ttype);
2723 70225585 : signop sign = TYPE_SIGN (ttype);
2724 70225585 : wide_int type_min = wi::min_value (prec, sign);
2725 70225585 : wide_int type_max = wi::max_value (prec, sign);
2726 70225585 : m_bitmask.set_unknown (prec);
2727 :
2728 : // At this point, we need one extra sub-range to represent the
2729 : // inverse.
2730 70225585 : maybe_resize (m_num_ranges + 1);
2731 :
2732 : // The algorithm is as follows. To calculate INVERT ([a,b][c,d]), we
2733 : // generate [-MIN, a-1][b+1, c-1][d+1, MAX].
2734 : //
2735 : // If there is an over/underflow in the calculation for any
2736 : // sub-range, we eliminate that subrange. This allows us to easily
2737 : // calculate INVERT([-MIN, 5]) with: [-MIN, -MIN-1][6, MAX]. And since
2738 : // we eliminate the underflow, only [6, MAX] remains.
2739 70225585 : unsigned i = 0;
2740 70225585 : wi::overflow_type ovf;
2741 : // Construct leftmost range.
2742 70225585 : int_range_max orig_range (*this);
2743 70225585 : unsigned nitems = 0;
2744 70225585 : wide_int tmp;
2745 : // If this is going to underflow on the MINUS 1, don't even bother
2746 : // checking. This also handles subtracting one from an unsigned 0,
2747 : // which doesn't set the underflow bit.
2748 70226179 : if (type_min != orig_range.lower_bound ())
2749 : {
2750 54594126 : m_base[nitems++] = type_min;
2751 54594720 : tmp = subtract_one (orig_range.lower_bound (), ttype, ovf);
2752 54594126 : m_base[nitems++] = tmp;
2753 54594126 : if (ovf)
2754 15631459 : nitems = 0;
2755 : }
2756 70225585 : i++;
2757 : // Construct middle ranges if applicable.
2758 70225585 : if (orig_range.num_pairs () > 1)
2759 : {
2760 : unsigned j = i;
2761 16261998 : for (; j < (orig_range.num_pairs () * 2) - 1; j += 2)
2762 : {
2763 : // The middle ranges cannot have MAX/MIN, so there's no need
2764 : // to check for unsigned overflow on the +1 and -1 here.
2765 8137153 : tmp = wi::add (orig_range.m_base[j], 1, sign, &ovf);
2766 8137153 : m_base[nitems++] = tmp;
2767 8137153 : tmp = subtract_one (orig_range.m_base[j + 1], ttype, ovf);
2768 8137153 : m_base[nitems++] = tmp;
2769 8137153 : if (ovf)
2770 0 : nitems -= 2;
2771 : }
2772 : i = j;
2773 : }
2774 : // Construct rightmost range.
2775 : //
2776 : // However, if this will overflow on the PLUS 1, don't even bother.
2777 : // This also handles adding one to an unsigned MAX, which doesn't
2778 : // set the overflow bit.
2779 70225585 : if (type_max != orig_range.m_base[i])
2780 : {
2781 69242965 : tmp = add_one (orig_range.m_base[i], ttype, ovf);
2782 69242965 : if (!ovf)
2783 : {
2784 : // Check to see if this inversion is going to work.
2785 69242965 : if (nitems / 2 >= m_max_ranges)
2786 : {
2787 : // No room for the extra field, so revert to the original value
2788 : // and return false.
2789 7 : *this = orig_range;
2790 7 : return false;
2791 : }
2792 69242958 : m_base[nitems++] = tmp;
2793 69242958 : m_base[nitems++] = type_max;
2794 : }
2795 : }
2796 70225578 : m_num_ranges = nitems / 2;
2797 :
2798 : // We disallow undefined or varying coming in, so the result can
2799 : // only be a VR_RANGE.
2800 70225578 : gcc_checking_assert (m_kind == VR_RANGE);
2801 :
2802 70225578 : if (flag_checking)
2803 70225486 : verify_range ();
2804 : return true;
2805 70226773 : }
2806 :
2807 : // This routine will take the bounds [LB, UB], and apply the bitmask to those
2808 : // values such that both bounds satisfy the bitmask. TRUE is returned
2809 : // if either bound changes, and they are returned as [NEW_LB, NEW_UB].
2810 : // If there is an overflow, or if (NEW_UB < NEW_LB), then the entire bound is
2811 : // to be removed as none of the values are valid. This is indicated by
2812 : // teturning TRUE in OVF. False indicates the bounds are fine.
2813 : // ie, [4, 14] MASK 0xFFFE VALUE 0x1
2814 : // means all values must be odd, the new bounds returned will be [5, 13] with
2815 : // OVF set to FALSE.
2816 : // ie, [4, 4] MASK 0xFFFE VALUE 0x1
2817 : // would return TRUE and OVF == TRUE. The entire subrange should be removed.
2818 :
2819 : bool
2820 238831948 : irange::snap (const wide_int &lb, const wide_int &ub,
2821 : wide_int &new_lb, wide_int &new_ub, bool &ovf)
2822 : {
2823 238831948 : ovf = false;
2824 238831948 : int z = wi::ctz (m_bitmask.mask ());
2825 238831948 : if (z == 0)
2826 : return false;
2827 :
2828 : // Shortcircuit check for values that are already good.
2829 285431040 : if ((((lb ^ m_bitmask.value ()) | (ub ^ m_bitmask.value ()))
2830 428140972 : & ~m_bitmask.mask ()) == 0)
2831 : return false;
2832 :
2833 15205379 : const wide_int step = (wi::one (TYPE_PRECISION (type ())) << z);
2834 15205379 : const wide_int match_mask = step - 1;
2835 15205379 : const wide_int value = m_bitmask.value () & match_mask;
2836 :
2837 15205379 : wide_int rem_lb = lb & match_mask;
2838 15205379 : wide_int offset = (value - rem_lb) & match_mask;
2839 15205379 : new_lb = lb + offset;
2840 : // Check for overflows at +INF
2841 15205379 : if (wi::lt_p (new_lb, lb, TYPE_SIGN (type ())))
2842 : {
2843 1470 : ovf = true;
2844 1470 : return true;
2845 : }
2846 :
2847 15203909 : wide_int rem_ub = ub & match_mask;
2848 15203909 : wide_int offset_ub = (rem_ub - value) & match_mask;
2849 15203909 : new_ub = ub - offset_ub;
2850 : // Check for underflows at -INF
2851 15203909 : if (wi::gt_p (new_ub, ub, TYPE_SIGN (type ())))
2852 : {
2853 119260 : ovf = true;
2854 119260 : return true;
2855 : }
2856 :
2857 : // If inverted range is invalid, set overflow to TRUE.
2858 15084649 : if (wi::lt_p (new_ub, new_lb, TYPE_SIGN (type ())))
2859 : {
2860 20992 : ovf = true;
2861 20992 : return true;
2862 : }
2863 27127101 : return (new_lb != lb) || (new_ub != ub);
2864 30409528 : }
2865 :
2866 : // This method loops through the subranges in THIS, and adjusts any bounds
2867 : // to satisfy the constraints of the BITMASK. If a subrange is invalid,
2868 : // it is removed. TRUE is returned if there were any changes.
2869 :
2870 : bool
2871 130322612 : irange::snap_subranges ()
2872 : {
2873 130322612 : bool changed = false;
2874 130322612 : int_range_max invalid;
2875 130322612 : unsigned x;
2876 130322612 : wide_int lb, ub;
2877 313647837 : for (x = 0; x < m_num_ranges; x++)
2878 : {
2879 183325225 : bool ovf;
2880 183332385 : if (snap (lower_bound (x), upper_bound (x), lb, ub, ovf))
2881 : {
2882 13825367 : changed = true;
2883 : // Check if this subrange is to be completely removed.
2884 13825367 : if (ovf)
2885 : {
2886 131659 : int_range<1> tmp (type (), lower_bound (x), upper_bound (x));
2887 131659 : invalid.union_ (tmp);
2888 131659 : continue;
2889 131659 : }
2890 13693756 : if (lower_bound (x) != lb)
2891 2196784 : m_base[x * 2] = lb;
2892 13693756 : if (upper_bound (x) != ub)
2893 12282187 : m_base[x * 2 + 1] = ub;
2894 : }
2895 : }
2896 : // Remove any subranges which are no invalid.
2897 130322612 : if (!invalid.undefined_p ())
2898 : {
2899 128172 : bool res = invalid.invert ();
2900 128172 : gcc_checking_assert (res);
2901 128172 : intersect (invalid);
2902 : }
2903 130322612 : return changed;
2904 130322651 : }
2905 :
2906 : // If the bitmask has a range representation, intersect this range with
2907 : // the bitmasks range. Then ensure all endpoints match the bitmask.
2908 : // Return TRUE if the range changes at all.
2909 :
2910 : bool
2911 130322612 : irange::set_range_from_bitmask ()
2912 : {
2913 130322612 : gcc_checking_assert (!undefined_p ());
2914 : // Snap subranmges when bitmask is first set.
2915 130322612 : snap_subranges ();
2916 130322612 : if (undefined_p ())
2917 : return true;
2918 :
2919 : // Calculate the set of ranges valid for the bitmask.
2920 130322490 : int_range_max allow;
2921 130322490 : if (!m_bitmask.range_from_mask (allow, m_type))
2922 : return false;
2923 : // And intersect that set of ranges with the current set.
2924 130263479 : return intersect (allow);
2925 130322490 : }
2926 :
2927 : void
2928 193168203 : irange::update_bitmask (const irange_bitmask &bm)
2929 : {
2930 193168203 : gcc_checking_assert (!undefined_p ());
2931 :
2932 : // If masks are the same, there is no change.
2933 193168203 : if (m_bitmask == bm)
2934 : return;
2935 :
2936 : // Drop VARYINGs with known bits to a plain range.
2937 90867188 : if (m_kind == VR_VARYING && !bm.unknown_p ())
2938 16677200 : m_kind = VR_RANGE;
2939 :
2940 74189988 : m_bitmask = bm;
2941 74189988 : if (!set_range_from_bitmask ())
2942 43606410 : normalize_kind ();
2943 74189988 : if (flag_checking)
2944 74189878 : verify_range ();
2945 : }
2946 :
2947 : // Return the bitmask of known bits that includes the bitmask inherent
2948 : // in the range.
2949 :
2950 : irange_bitmask
2951 1228434315 : irange::get_bitmask () const
2952 : {
2953 1228434315 : gcc_checking_assert (!undefined_p ());
2954 :
2955 : // The mask inherent in the range is calculated on-demand. For
2956 : // example, [0,255] does not have known bits set by default. This
2957 : // saves us considerable time, because setting it at creation incurs
2958 : // a large penalty for irange::set. At the time of writing there
2959 : // was a 5% slowdown in VRP if we kept the mask precisely up to date
2960 : // at all times. Instead, we default to -1 and set it when
2961 : // explicitly requested. However, this function will always return
2962 : // the correct mask.
2963 : //
2964 : // This also means that the mask may have a finer granularity than
2965 : // the range and thus contradict it. Think of the mask as an
2966 : // enhancement to the range. For example:
2967 : //
2968 : // [3, 1000] MASK 0xfffffffe VALUE 0x0
2969 : //
2970 : // 3 is in the range endpoints, but is excluded per the known 0 bits
2971 : // in the mask.
2972 : //
2973 : // See also the note in irange_bitmask::intersect.
2974 1228510738 : irange_bitmask bm (type (), lower_bound (), upper_bound ());
2975 1228434315 : if (!m_bitmask.unknown_p ())
2976 : {
2977 : // If the new intersection is unknown, it means there are inconsistent
2978 : // bits, so simply return the original bitmask.
2979 538228921 : if (!bm.intersect (m_bitmask))
2980 19221 : return m_bitmask;
2981 : }
2982 1228415094 : return bm;
2983 1228434315 : }
2984 :
2985 : // Set the nonzero bits in R into THIS. Return TRUE and
2986 : // normalize the range if anything changed.
2987 :
2988 : void
2989 783427 : vrange::set_nonzero_bits (const wide_int &bits)
2990 : {
2991 783427 : gcc_checking_assert (!undefined_p ());
2992 783427 : irange_bitmask bm (wi::zero (TYPE_PRECISION (type ())), bits);
2993 783427 : update_bitmask (bm);
2994 783427 : }
2995 :
2996 : // Return the nonzero bits in R.
2997 :
2998 : wide_int
2999 226457146 : vrange::get_nonzero_bits () const
3000 : {
3001 226457146 : gcc_checking_assert (!undefined_p ());
3002 226457146 : irange_bitmask bm = get_bitmask ();
3003 226523295 : return bm.value () | bm.mask ();
3004 226457146 : }
3005 :
3006 : // Intersect the bitmask in R into THIS and normalize the range.
3007 : // Return TRUE if the intersection changed anything.
3008 :
3009 : bool
3010 528029908 : irange::intersect_bitmask (const irange &r)
3011 : {
3012 528029908 : gcc_checking_assert (!undefined_p () && !r.undefined_p ());
3013 :
3014 : // If the bitmasks are the same, do nothing.
3015 528029908 : if (m_bitmask == r.m_bitmask)
3016 : return false;
3017 :
3018 191660993 : irange_bitmask bm = get_bitmask ();
3019 191660993 : irange_bitmask save = bm;
3020 191660993 : if (!bm.intersect (r.get_bitmask ()))
3021 : {
3022 34264 : set_undefined ();
3023 34264 : return true;
3024 : }
3025 :
3026 : // If the new mask is the same, there is no change.
3027 191626729 : if (m_bitmask == bm)
3028 : return false;
3029 :
3030 56132624 : m_bitmask = bm;
3031 56132624 : if (!set_range_from_bitmask ())
3032 55703218 : normalize_kind ();
3033 56132624 : if (flag_checking)
3034 56132459 : verify_range ();
3035 : return true;
3036 191660993 : }
3037 :
3038 : // Union the bitmask in R into THIS. Return TRUE and normalize the
3039 : // range if anything changed.
3040 :
3041 : bool
3042 303788484 : irange::union_bitmask (const irange &r)
3043 : {
3044 303788484 : gcc_checking_assert (!undefined_p () && !r.undefined_p ());
3045 :
3046 303788484 : if (m_bitmask == r.m_bitmask)
3047 : return false;
3048 :
3049 11858031 : irange_bitmask bm = get_bitmask ();
3050 11858031 : irange_bitmask save = bm;
3051 11858031 : bm.union_ (r.get_bitmask ());
3052 20871435 : if (save == bm && (!bm.unknown_p () || m_bitmask.unknown_p ()))
3053 : return false;
3054 :
3055 2844627 : m_bitmask = bm;
3056 :
3057 : // Updating m_bitmask may still yield a semantic bitmask (as
3058 : // returned by get_bitmask) which is functionally equivalent to what
3059 : // we originally had. In which case, there's still no change.
3060 2844627 : if (save == get_bitmask ())
3061 : return false;
3062 :
3063 : // No need to call set_range_from_mask, because we'll never
3064 : // narrow the range. Besides, it would cause endless recursion
3065 : // because of the union_ in set_range_from_mask.
3066 2844627 : normalize_kind ();
3067 2844627 : return true;
3068 11858031 : }
3069 :
3070 : tree
3071 9443934 : irange::lbound () const
3072 : {
3073 9443934 : return wide_int_to_tree (type (), lower_bound ());
3074 : }
3075 :
3076 : tree
3077 311448 : irange::ubound () const
3078 : {
3079 311448 : return wide_int_to_tree (type (), upper_bound ());
3080 : }
3081 :
3082 : void
3083 11119826328 : irange_bitmask::verify_mask () const
3084 : {
3085 11119826328 : gcc_assert (m_value.get_precision () == m_mask.get_precision ());
3086 11119826328 : gcc_checking_assert (wi::bit_and (m_mask, m_value) == 0);
3087 11119826328 : }
3088 :
3089 : void
3090 0 : dump_value_range (FILE *file, const vrange *vr)
3091 : {
3092 0 : vr->dump (file);
3093 0 : }
3094 :
3095 : DEBUG_FUNCTION void
3096 0 : debug (const vrange *vr)
3097 : {
3098 0 : dump_value_range (stderr, vr);
3099 0 : fprintf (stderr, "\n");
3100 0 : }
3101 :
3102 : DEBUG_FUNCTION void
3103 0 : debug (const vrange &vr)
3104 : {
3105 0 : debug (&vr);
3106 0 : }
3107 :
3108 : /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
3109 :
3110 : bool
3111 198074542 : vrp_operand_equal_p (const_tree val1, const_tree val2)
3112 : {
3113 198074542 : if (val1 == val2)
3114 : return true;
3115 82762879 : if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
3116 81257254 : return false;
3117 : return true;
3118 : }
3119 :
3120 : #define DEFINE_INT_RANGE_INSTANCE(N) \
3121 : template int_range<N>::int_range(tree_node *, \
3122 : const wide_int &, \
3123 : const wide_int &, \
3124 : value_range_kind); \
3125 : template int_range<N>::int_range(tree); \
3126 : template int_range<N>::int_range(const irange &); \
3127 : template int_range<N>::int_range(const int_range &); \
3128 : template int_range<N>& int_range<N>::operator= (const int_range &);
3129 :
3130 : DEFINE_INT_RANGE_INSTANCE(1)
3131 : DEFINE_INT_RANGE_INSTANCE(2)
3132 : DEFINE_INT_RANGE_INSTANCE(3)
3133 : DEFINE_INT_RANGE_INSTANCE(255)
3134 :
3135 : #if CHECKING_P
3136 : #include "selftest.h"
3137 :
3138 : #define INT(x) wi::shwi ((x), TYPE_PRECISION (integer_type_node))
3139 : #define UINT(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_type_node))
3140 : #define SCHAR(x) wi::shwi ((x), TYPE_PRECISION (signed_char_type_node))
3141 :
3142 : namespace selftest
3143 : {
3144 :
3145 : static int_range<2>
3146 584 : range (tree type, int a, int b, value_range_kind kind = VR_RANGE)
3147 : {
3148 584 : wide_int w1, w2;
3149 584 : if (TYPE_UNSIGNED (type))
3150 : {
3151 40 : w1 = wi::uhwi (a, TYPE_PRECISION (type));
3152 40 : w2 = wi::uhwi (b, TYPE_PRECISION (type));
3153 : }
3154 : else
3155 : {
3156 544 : w1 = wi::shwi (a, TYPE_PRECISION (type));
3157 544 : w2 = wi::shwi (b, TYPE_PRECISION (type));
3158 : }
3159 584 : return int_range<2> (type, w1, w2, kind);
3160 584 : }
3161 :
3162 : static int_range<2>
3163 540 : range_int (int a, int b, value_range_kind kind = VR_RANGE)
3164 : {
3165 0 : return range (integer_type_node, a, b, kind);
3166 : }
3167 :
3168 : static int_range<2>
3169 8 : range_uint (int a, int b, value_range_kind kind = VR_RANGE)
3170 : {
3171 4 : return range (unsigned_type_node, a, b, kind);
3172 : }
3173 :
3174 : static int_range<2>
3175 8 : range_uint128 (int a, int b, value_range_kind kind = VR_RANGE)
3176 : {
3177 8 : tree u128_type_node = build_nonstandard_integer_type (128, 1);
3178 8 : return range (u128_type_node, a, b, kind);
3179 : }
3180 :
3181 : static int_range<2>
3182 12 : range_uchar (int a, int b, value_range_kind kind = VR_RANGE)
3183 : {
3184 0 : return range (unsigned_char_type_node, a, b, kind);
3185 : }
3186 :
3187 : static int_range<2>
3188 4 : range_char (int a, int b, value_range_kind kind = VR_RANGE)
3189 : {
3190 0 : return range (signed_char_type_node, a, b, kind);
3191 : }
3192 :
3193 : static int_range<3>
3194 44 : build_range3 (int a, int b, int c, int d, int e, int f)
3195 : {
3196 44 : int_range<3> i1 = range_int (a, b);
3197 44 : int_range<3> i2 = range_int (c, d);
3198 44 : int_range<3> i3 = range_int (e, f);
3199 44 : i1.union_ (i2);
3200 44 : i1.union_ (i3);
3201 88 : return i1;
3202 44 : }
3203 :
3204 : static void
3205 4 : range_tests_irange3 ()
3206 : {
3207 4 : int_range<3> r0, r1, r2;
3208 4 : int_range<3> i1, i2, i3;
3209 :
3210 : // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20].
3211 4 : r0 = range_int (10, 20);
3212 4 : r1 = range_int (5, 8);
3213 4 : r0.union_ (r1);
3214 4 : r1 = range_int (1, 3);
3215 4 : r0.union_ (r1);
3216 4 : ASSERT_TRUE (r0 == build_range3 (1, 3, 5, 8, 10, 20));
3217 :
3218 : // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20].
3219 4 : r1 = range_int (-5, 0);
3220 4 : r0.union_ (r1);
3221 4 : ASSERT_TRUE (r0 == build_range3 (-5, 3, 5, 8, 10, 20));
3222 :
3223 : // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60].
3224 4 : r1 = range_int (50, 60);
3225 4 : r0 = range_int (10, 20);
3226 4 : r0.union_ (range_int (30, 40));
3227 4 : r0.union_ (r1);
3228 4 : ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
3229 : // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80].
3230 4 : r1 = range_int (70, 80);
3231 4 : r0.union_ (r1);
3232 :
3233 4 : r2 = build_range3 (10, 20, 30, 40, 50, 60);
3234 4 : r2.union_ (range_int (70, 80));
3235 4 : ASSERT_TRUE (r0 == r2);
3236 :
3237 : // [10,20][30,40][50,60] U [6,35] => [6,40][50,60].
3238 4 : r0 = build_range3 (10, 20, 30, 40, 50, 60);
3239 4 : r1 = range_int (6, 35);
3240 4 : r0.union_ (r1);
3241 4 : r1 = range_int (6, 40);
3242 4 : r1.union_ (range_int (50, 60));
3243 4 : ASSERT_TRUE (r0 == r1);
3244 :
3245 : // [10,20][30,40][50,60] U [6,60] => [6,60].
3246 4 : r0 = build_range3 (10, 20, 30, 40, 50, 60);
3247 4 : r1 = range_int (6, 60);
3248 4 : r0.union_ (r1);
3249 4 : ASSERT_TRUE (r0 == range_int (6, 60));
3250 :
3251 : // [10,20][30,40][50,60] U [6,70] => [6,70].
3252 4 : r0 = build_range3 (10, 20, 30, 40, 50, 60);
3253 4 : r1 = range_int (6, 70);
3254 4 : r0.union_ (r1);
3255 4 : ASSERT_TRUE (r0 == range_int (6, 70));
3256 :
3257 : // [10,20][30,40][50,60] U [35,70] => [10,20][30,70].
3258 4 : r0 = build_range3 (10, 20, 30, 40, 50, 60);
3259 4 : r1 = range_int (35, 70);
3260 4 : r0.union_ (r1);
3261 4 : r1 = range_int (10, 20);
3262 4 : r1.union_ (range_int (30, 70));
3263 4 : ASSERT_TRUE (r0 == r1);
3264 :
3265 : // [10,20][30,40][50,60] U [15,35] => [10,40][50,60].
3266 4 : r0 = build_range3 (10, 20, 30, 40, 50, 60);
3267 4 : r1 = range_int (15, 35);
3268 4 : r0.union_ (r1);
3269 4 : r1 = range_int (10, 40);
3270 4 : r1.union_ (range_int (50, 60));
3271 4 : ASSERT_TRUE (r0 == r1);
3272 :
3273 : // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60].
3274 4 : r0 = build_range3 (10, 20, 30, 40, 50, 60);
3275 4 : r1 = range_int (35, 35);
3276 4 : r0.union_ (r1);
3277 4 : ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60));
3278 4 : }
3279 :
3280 : static void
3281 4 : range_tests_int_range_max ()
3282 : {
3283 4 : int_range_max big;
3284 4 : unsigned int nrange;
3285 :
3286 : // Build a huge multi-range range.
3287 208 : for (nrange = 0; nrange < 50; ++nrange)
3288 : {
3289 200 : int_range<1> tmp = range_int (nrange*10, nrange *10 + 5);
3290 200 : big.union_ (tmp);
3291 200 : }
3292 4 : ASSERT_TRUE (big.num_pairs () == nrange);
3293 :
3294 : // Verify that we can copy it without loosing precision.
3295 4 : int_range_max copy (big);
3296 4 : ASSERT_TRUE (copy.num_pairs () == nrange);
3297 :
3298 : // Inverting it should produce one more sub-range.
3299 4 : big.invert ();
3300 4 : ASSERT_TRUE (big.num_pairs () == nrange + 1);
3301 :
3302 4 : int_range<1> tmp = range_int (5, 37);
3303 4 : big.intersect (tmp);
3304 4 : ASSERT_TRUE (big.num_pairs () == 4);
3305 :
3306 : // Cannot resize tmp, and the invert does not fit,
3307 4 : ASSERT_FALSE (tmp.invert ());
3308 :
3309 : // Test that [10,10][20,20] does NOT contain 15.
3310 4 : {
3311 4 : int_range_max i1 = range_int (10, 10);
3312 4 : int_range_max i2 = range_int (20, 20);
3313 4 : i1.union_ (i2);
3314 4 : ASSERT_FALSE (i1.contains_p (INT (15)));
3315 4 : }
3316 4 : }
3317 :
3318 : // Simulate -fstrict-enums where the domain of a type is less than the
3319 : // underlying type.
3320 :
3321 : static void
3322 4 : range_tests_strict_enum ()
3323 : {
3324 : // The enum can only hold [0, 3].
3325 4 : tree rtype = copy_node (unsigned_type_node);
3326 4 : TYPE_MIN_VALUE (rtype) = build_int_cstu (rtype, 0);
3327 4 : TYPE_MAX_VALUE (rtype) = build_int_cstu (rtype, 3);
3328 :
3329 : // Test that even though vr1 covers the strict enum domain ([0, 3]),
3330 : // it does not cover the domain of the underlying type.
3331 4 : int_range<1> vr1 = range (rtype, 0, 1);
3332 4 : int_range<1> vr2 = range (rtype, 2, 3);
3333 4 : vr1.union_ (vr2);
3334 4 : ASSERT_TRUE (vr1 == range (rtype, 0, 3));
3335 4 : ASSERT_FALSE (vr1.varying_p ());
3336 :
3337 : // Test that copying to a multi-range does not change things.
3338 4 : int_range<2> ir1 (vr1);
3339 4 : ASSERT_TRUE (ir1 == vr1);
3340 4 : ASSERT_FALSE (ir1.varying_p ());
3341 :
3342 : // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3].
3343 8 : vr1 = int_range<2> (rtype,
3344 8 : wi::to_wide (TYPE_MIN_VALUE (rtype)),
3345 12 : wi::to_wide (TYPE_MAX_VALUE (rtype)));
3346 4 : ir1 = vr1;
3347 4 : ASSERT_TRUE (ir1 == vr1);
3348 4 : ASSERT_FALSE (ir1.varying_p ());
3349 4 : }
3350 :
3351 : // Test that range bounds are "snapped" to where they are expected to be.
3352 :
3353 : static void
3354 104 : assert_snap_result (int lb_val, int ub_val,
3355 : int expected_lb, int expected_ub,
3356 : unsigned mask_val, unsigned value_val,
3357 : tree type)
3358 : {
3359 104 : wide_int lb = wi::shwi (lb_val, TYPE_PRECISION (type));
3360 104 : wide_int ub = wi::shwi (ub_val, TYPE_PRECISION (type));
3361 104 : wide_int new_lb, new_ub;
3362 :
3363 208 : irange_bitmask bm (wi::uhwi (value_val, TYPE_PRECISION (type)),
3364 208 : wi::uhwi (mask_val, TYPE_PRECISION (type)));
3365 :
3366 104 : int_range_max r (type);
3367 104 : r.set (type, lb, ub);
3368 104 : r.update_bitmask (bm);
3369 :
3370 104 : if (TYPE_SIGN (type) == SIGNED && expected_ub < expected_lb)
3371 20 : gcc_checking_assert (r.undefined_p ());
3372 84 : else if (TYPE_SIGN (type) == UNSIGNED
3373 84 : && ((unsigned)expected_ub < (unsigned)expected_lb))
3374 16 : gcc_checking_assert (r.undefined_p ());
3375 : else
3376 : {
3377 68 : gcc_checking_assert (wi::eq_p (r.lower_bound (),
3378 : wi::shwi (expected_lb,
3379 : TYPE_PRECISION (type))));
3380 136 : gcc_checking_assert (wi::eq_p (r.upper_bound (),
3381 : wi::shwi (expected_ub,
3382 : TYPE_PRECISION (type))));
3383 : }
3384 104 : }
3385 :
3386 :
3387 : // Run a selection of tests that confirm, bounds are snapped as expected.
3388 : // We only test individual pairs, multiple pairs use the same snapping
3389 : // routine as single pairs.
3390 :
3391 : static void
3392 4 : test_irange_snap_bounds ()
3393 : {
3394 4 : tree u32 = unsigned_type_node;
3395 4 : tree s32 = integer_type_node;
3396 4 : tree s8 = build_nonstandard_integer_type (8, /*unsigned=*/ 0);
3397 4 : tree s1 = build_nonstandard_integer_type (1, /*unsigned=*/ 0);
3398 4 : tree u1 = build_nonstandard_integer_type (1, /*unsigned=*/ 1);
3399 :
3400 : // Basic aligned range: even-only
3401 4 : assert_snap_result (5, 15, 6, 14, 0xE, 0x0, u32);
3402 : // Singleton that doesn't match mask: undefined.
3403 4 : assert_snap_result (7, 7, 1, 0, 0xFFFFFFFE, 0x0, u32);
3404 : // 8-bit signed char, mask 0xF0 (i.e. step of 16).
3405 4 : assert_snap_result (-100, 100, -96, 96, 0xF0, 0x00, s8);
3406 : // Already aligned range: no change.
3407 4 : assert_snap_result (0, 240, 0, 240, 0xF0, 0x00, u32);
3408 : // Negative range, step 16 alignment (s32).
3409 4 : assert_snap_result (-123, -17, -112, -32, 0xFFFFFFF0, 0x00, s32);
3410 : // Negative range, step 16 alignment (trailing-zero aligned mask).
3411 4 : assert_snap_result (-123, -17, -112, -32, 0xFFFFFFF0, 0x00, s32);
3412 : // s8, 16-alignment mask, value = 0 (valid).
3413 4 : assert_snap_result (-50, 10, -48, 0, 0xF0, 0x00, s8);
3414 : // No values in range [-3,2] match alignment except 0.
3415 4 : assert_snap_result (-3, 2, 0, 0, 0xF8, 0x00, s8);
3416 : // No values in range [-3,2] match alignment — undefined.
3417 4 : assert_snap_result (-3, 2, 1, 0, 0xF8, 0x04, s8);
3418 : // Already aligned range: no change.
3419 4 : assert_snap_result (0, 240, 0, 240, 0xF0, 0x00, s32);
3420 : // 1-bit signed: only -1 allowed (0b1).
3421 4 : assert_snap_result (-1, 0, -1, -1, 0x00, 0x01, s1);
3422 : // 1-bit signed: only 0 allowed (0b0).
3423 4 : assert_snap_result (-1, 0, 0, 0, 0x00, 0x00, s1);
3424 : // 1-bit signed: no match (invalid case).
3425 4 : assert_snap_result (-1, -1, 1, 0, 0x00, 0x00, s1);
3426 : // 1-bit signed: no match (invalid case).
3427 4 : assert_snap_result (0, 0, 1, 0, 0x00, 0x01, s1);
3428 : // 1-bit unsigned: only 1 allowed.
3429 4 : assert_snap_result (0, 1, 1, 1, 0x00, 0x01, u1);
3430 : // 1-bit unsigned: only 0 allowed.
3431 4 : assert_snap_result (0, 1, 0, 0, 0x00, 0x00, u1);
3432 : // 1-bit unsigned: no match (invalid case).
3433 4 : assert_snap_result (1, 1, 1, 0, 0x00, 0x00, u1);
3434 : // 1-bit unsigned: no match (invalid case).
3435 4 : assert_snap_result (0, 0, 1, 0, 0x00, 0x01, u1);
3436 : // Unsigned: Near overflow, even alignment.
3437 4 : assert_snap_result (UINT_MAX - 6, UINT_MAX, UINT_MAX - 5, UINT_MAX - 1,
3438 : 0xFFFFFFFE, 0x00, u32);
3439 : // Unsigned: Wraparound-like range — no valid snapped values.
3440 4 : assert_snap_result (UINT_MAX - 5, UINT_MAX, 1, 0, 0xFFFFFFF0, 0x00, u32);
3441 : // Signed: Near INT_MAX, 8-aligned.
3442 4 : assert_snap_result (INT_MAX - 18, INT_MAX, INT_MAX - 15, INT_MAX - 7,
3443 : 0xFFFFFFF8, 0x00, s32);
3444 : // Signed: Near INT_MIN, 16-aligned.
3445 4 : assert_snap_result (INT_MIN, INT_MIN + 30, INT_MIN, INT_MIN + 16,
3446 : 0xFFFFFFF0, 0x00, s32);
3447 : // Signed: Full domain, 4-aligned.
3448 4 : assert_snap_result (-128, 127, -128, 124, 0xFC, 0x00, s8);
3449 : // Singleton at INT_MIN that doesn’t match alignment — undefined
3450 4 : assert_snap_result (INT_MIN, INT_MIN, 1, 0, 0xFFFFFFFE, 0x01, s32);
3451 : // Range at INT_MIN that doesn’t match alignment — undefined.
3452 4 : assert_snap_result (INT_MIN, INT_MIN + 10, 1, 0, 0xFFFFFFF0, 0x0F, s32);
3453 : // Unsigned: Full domain, 256-aligned.
3454 4 : assert_snap_result (0, UINT_MAX, 0, UINT_MAX & ~255, 0xFFFFFF00, 0x00, u32);
3455 4 : }
3456 :
3457 : static void
3458 4 : range_tests_misc ()
3459 : {
3460 4 : bool res;
3461 4 : tree u128_type = build_nonstandard_integer_type (128, /*unsigned=*/1);
3462 4 : int_range<2> i1, i2, i3;
3463 4 : int_range<2> r0, r1, rold;
3464 :
3465 : // Test 1-bit signed integer union.
3466 : // [-1,-1] U [0,0] = VARYING.
3467 4 : tree one_bit_type = build_nonstandard_integer_type (1, 0);
3468 4 : wide_int one_bit_min = irange_val_min (one_bit_type);
3469 4 : wide_int one_bit_max = irange_val_max (one_bit_type);
3470 4 : {
3471 4 : int_range<2> min = int_range<2> (one_bit_type, one_bit_min, one_bit_min);
3472 4 : int_range<2> max = int_range<2> (one_bit_type, one_bit_max, one_bit_max);
3473 4 : max.union_ (min);
3474 4 : ASSERT_TRUE (max.varying_p ());
3475 4 : }
3476 : // Test that we can set a range of true+false for a 1-bit signed int.
3477 4 : r0 = range_true_and_false (one_bit_type);
3478 :
3479 : // Test inversion of 1-bit signed integers.
3480 4 : {
3481 4 : int_range<2> min = int_range<2> (one_bit_type, one_bit_min, one_bit_min);
3482 4 : int_range<2> max = int_range<2> (one_bit_type, one_bit_max, one_bit_max);
3483 4 : int_range<2> t;
3484 4 : t = min;
3485 4 : res = t.invert ();
3486 4 : ASSERT_TRUE (res && t == max);
3487 4 : t = max;
3488 4 : res = t.invert ();
3489 4 : ASSERT_TRUE (res && t == min);
3490 4 : }
3491 :
3492 : // Test that NOT(255) is [0..254] in 8-bit land.
3493 4 : int_range<1> not_255 = range_uchar (255, 255, VR_ANTI_RANGE);
3494 4 : ASSERT_TRUE (not_255 == range_uchar (0, 254));
3495 :
3496 : // Test that NOT(0) is [1..255] in 8-bit land.
3497 4 : int_range<2> not_zero;
3498 4 : not_zero.set_nonzero (unsigned_char_type_node);
3499 4 : ASSERT_TRUE (not_zero == range_uchar (1, 255));
3500 :
3501 : // Check that [0,127][0x..ffffff80,0x..ffffff]
3502 : // => ~[128, 0x..ffffff7f].
3503 4 : r0 = range_uint128 (0, 127);
3504 4 : wide_int high = wi::minus_one (128);
3505 : // low = -1 - 127 => 0x..ffffff80.
3506 4 : wide_int low = wi::sub (high, wi::uhwi (127, 128));
3507 4 : r1 = int_range<1> (u128_type, low, high); // [0x..ffffff80, 0x..ffffffff]
3508 : // r0 = [0,127][0x..ffffff80,0x..fffffff].
3509 4 : r0.union_ (r1);
3510 : // r1 = [128, 0x..ffffff7f].
3511 12 : r1 = int_range<1> (u128_type,
3512 8 : wi::uhwi (128, 128),
3513 8 : wi::sub (wi::minus_one (128), wi::uhwi (128, 128)));
3514 4 : res = r0.invert ();
3515 4 : ASSERT_TRUE (res && r0 == r1);
3516 :
3517 4 : r0.set_varying (integer_type_node);
3518 4 : wide_int minint = r0.lower_bound ();
3519 4 : wide_int maxint = r0.upper_bound ();
3520 :
3521 4 : r0.set_varying (short_integer_type_node);
3522 :
3523 4 : r0.set_varying (unsigned_type_node);
3524 4 : wide_int maxuint = r0.upper_bound ();
3525 :
3526 : // Check that ~[0,5] => [6,MAX] for unsigned int.
3527 4 : r0 = range_uint (0, 5);
3528 4 : res = r0.invert ();
3529 4 : ASSERT_TRUE (res);
3530 4 : ASSERT_TRUE (r0 == int_range<1> (unsigned_type_node,
3531 : wi::uhwi (6, TYPE_PRECISION (unsigned_type_node)),
3532 : maxuint));
3533 :
3534 : // Check that ~[10,MAX] => [0,9] for unsigned int.
3535 8 : r0 = int_range<1> (unsigned_type_node,
3536 4 : wi::uhwi (10, TYPE_PRECISION (unsigned_type_node)),
3537 8 : maxuint);
3538 4 : res = r0.invert ();
3539 8 : ASSERT_TRUE (res && r0 == range_uint (0, 9));
3540 :
3541 : // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers.
3542 4 : r0 = range_uint128 (0, 5, VR_ANTI_RANGE);
3543 4 : r1 = int_range<1> (u128_type, wi::uhwi (6, 128), wi::minus_one (128));
3544 4 : ASSERT_TRUE (r0 == r1);
3545 :
3546 : // Check that [~5] is really [-MIN,4][6,MAX].
3547 4 : r0 = range_int (5, 5, VR_ANTI_RANGE);
3548 4 : r1 = int_range<1> (integer_type_node, minint, INT (4));
3549 4 : r1.union_ (int_range<1> (integer_type_node, INT (6), maxint));
3550 4 : ASSERT_FALSE (r1.undefined_p ());
3551 4 : ASSERT_TRUE (r0 == r1);
3552 :
3553 4 : r1 = range_int (5, 5);
3554 4 : int_range<2> r2 (r1);
3555 4 : ASSERT_TRUE (r1 == r2);
3556 :
3557 4 : r1 = range_int (5, 10);
3558 :
3559 4 : r1 = range_int (5, 10);
3560 4 : ASSERT_TRUE (r1.contains_p (INT (7)));
3561 :
3562 4 : r1 = range_char (0, 20);
3563 4 : ASSERT_TRUE (r1.contains_p (SCHAR(15)));
3564 4 : ASSERT_FALSE (r1.contains_p (SCHAR(300)));
3565 :
3566 : // NOT([10,20]) ==> [-MIN,9][21,MAX].
3567 4 : r0 = r1 = range_int (10, 20);
3568 4 : r2 = int_range<1> (integer_type_node, minint, INT(9));
3569 4 : r2.union_ (int_range<1> (integer_type_node, INT(21), maxint));
3570 4 : ASSERT_FALSE (r2.undefined_p ());
3571 4 : res = r1.invert ();
3572 4 : ASSERT_TRUE (res && r1 == r2);
3573 : // Test that NOT(NOT(x)) == x.
3574 4 : res = r2.invert ();
3575 4 : ASSERT_TRUE (res && r0 == r2);
3576 :
3577 : // Test that booleans and their inverse work as expected.
3578 4 : r0.set_zero (boolean_type_node);
3579 4 : ASSERT_TRUE (r0 == range_false ());
3580 4 : res = r0.invert ();
3581 4 : ASSERT_TRUE (res && r0 == range_true ());
3582 :
3583 : // Make sure NULL and non-NULL of pointer types work, and that
3584 : // inverses of them are consistent.
3585 4 : tree voidp = build_pointer_type (void_type_node);
3586 4 : prange p0;
3587 4 : p0.set_zero (voidp);
3588 4 : prange p1 = p0;
3589 4 : res = p0.invert ();
3590 4 : ASSERT_TRUE (res);
3591 4 : res = p0.invert ();
3592 4 : ASSERT_TRUE (res && p0 == p1);
3593 :
3594 : // The intersection of:
3595 : // [0, +INF] MASK 0xff..00 VALUE 0xf8
3596 : // [0, +INF] MASK 0xff..00 VALUE 0x00
3597 : // is [0, +INF] MASK 0xff..ff VALUE 0x00, which is VARYING.
3598 : // Test that we normalized to VARYING.
3599 4 : unsigned prec = TYPE_PRECISION (voidp);
3600 4 : p0.set_varying (voidp);
3601 4 : wide_int mask = wi::mask (8, true, prec);
3602 4 : wide_int value = wi::uhwi (0xf8, prec);
3603 4 : irange_bitmask bm (wi::uhwi (0xf8, prec), mask);
3604 4 : p0.update_bitmask (bm);
3605 4 : p1.set_varying (voidp);
3606 4 : bm = irange_bitmask (wi::zero (prec), mask);
3607 4 : p1.update_bitmask (bm);
3608 4 : p0.intersect (p1);
3609 :
3610 : // [10,20] U [15, 30] => [10, 30].
3611 4 : r0 = range_int (10, 20);
3612 4 : r1 = range_int (15, 30);
3613 4 : r0.union_ (r1);
3614 4 : ASSERT_TRUE (r0 == range_int (10, 30));
3615 :
3616 : // [15,40] U [] => [15,40].
3617 4 : r0 = range_int (15, 40);
3618 4 : r1.set_undefined ();
3619 4 : r0.union_ (r1);
3620 4 : ASSERT_TRUE (r0 == range_int (15, 40));
3621 :
3622 : // [10,20] U [10,10] => [10,20].
3623 4 : r0 = range_int (10, 20);
3624 4 : r1 = range_int (10, 10);
3625 4 : r0.union_ (r1);
3626 4 : ASSERT_TRUE (r0 == range_int (10, 20));
3627 :
3628 : // [10,20] U [9,9] => [9,20].
3629 4 : r0 = range_int (10, 20);
3630 4 : r1 = range_int (9, 9);
3631 4 : r0.union_ (r1);
3632 4 : ASSERT_TRUE (r0 == range_int (9, 20));
3633 :
3634 : // [10,20] ^ [15,30] => [15,20].
3635 4 : r0 = range_int (10, 20);
3636 4 : r1 = range_int (15, 30);
3637 4 : r0.intersect (r1);
3638 4 : ASSERT_TRUE (r0 == range_int (15, 20));
3639 :
3640 : // Test the internal sanity of wide_int's wrt HWIs.
3641 4 : ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node),
3642 : TYPE_SIGN (boolean_type_node))
3643 : == wi::uhwi (1, TYPE_PRECISION (boolean_type_node)));
3644 :
3645 : // Test zero_p().
3646 4 : r0 = range_int (0, 0);
3647 4 : ASSERT_TRUE (r0.zero_p ());
3648 :
3649 : // Test contains_zero_p().
3650 4 : r0 = range_int (0, 0);
3651 4 : res = r0.invert ();
3652 4 : ASSERT_TRUE (res);
3653 4 : ASSERT_FALSE (r0.contains_zero_p ());
3654 :
3655 : // r0 = ~[1,1]
3656 4 : r0 = range_int (1, 1, VR_ANTI_RANGE);
3657 : // r1 = ~[3,3]
3658 4 : r1 = range_int (3, 3, VR_ANTI_RANGE);
3659 :
3660 : // vv = [0,0][2,2][4, MAX]
3661 4 : int_range<3> vv = r0;
3662 4 : vv.intersect (r1);
3663 :
3664 4 : ASSERT_TRUE (vv.contains_p (UINT (2)));
3665 4 : ASSERT_TRUE (vv.num_pairs () == 3);
3666 :
3667 4 : r0 = range_int (1, 1);
3668 : // And union it with [0,0][2,2][4,MAX] multi range
3669 4 : r0.union_ (vv);
3670 : // The result should be [0,2][4,MAX], or ~[3,3] but it must contain 2
3671 4 : ASSERT_TRUE (r0.contains_p (INT (2)));
3672 4 : }
3673 :
3674 : static void
3675 4 : range_tests_nonzero_bits ()
3676 : {
3677 4 : int_range<8> r0, r1;
3678 :
3679 : // Adding nonzero bits to a varying drops the varying.
3680 4 : r0.set_varying (integer_type_node);
3681 4 : r0.set_nonzero_bits (INT (255));
3682 4 : ASSERT_TRUE (!r0.varying_p ());
3683 :
3684 : // Test contains_p with nonzero bits.
3685 4 : r0.set_zero (integer_type_node);
3686 4 : ASSERT_TRUE (r0.contains_p (INT (0)));
3687 4 : ASSERT_FALSE (r0.contains_p (INT (1)));
3688 4 : r0.set_nonzero_bits (INT (0xfe));
3689 4 : ASSERT_FALSE (r0.contains_p (INT (0x100)));
3690 4 : ASSERT_FALSE (r0.contains_p (INT (0x3)));
3691 :
3692 : // Union of nonzero bits.
3693 4 : r0.set_varying (integer_type_node);
3694 4 : r0.set_nonzero_bits (INT (0xf0));
3695 4 : r1.set_varying (integer_type_node);
3696 4 : r1.set_nonzero_bits (INT (0xf));
3697 4 : r0.union_ (r1);
3698 4 : ASSERT_TRUE (r0.get_nonzero_bits () == 0xff);
3699 :
3700 : // Intersect of nonzero bits.
3701 4 : r0 = range_int (0, 255);
3702 4 : r0.set_nonzero_bits (INT (0xfe));
3703 4 : r1.set_varying (integer_type_node);
3704 4 : r1.set_nonzero_bits (INT (0xf0));
3705 4 : r0.intersect (r1);
3706 4 : ASSERT_TRUE (r0.get_nonzero_bits () == 0xf0);
3707 :
3708 : // Intersect where the mask of nonzero bits is implicit from the range.
3709 4 : r0.set_varying (integer_type_node);
3710 4 : r1 = range_int (0, 255);
3711 4 : r0.intersect (r1);
3712 4 : ASSERT_TRUE (r0.get_nonzero_bits () == 0xff);
3713 :
3714 : // Test that setting a nonzero bit of 1 does not pessimize the range.
3715 4 : r0.set_zero (integer_type_node);
3716 4 : r0.set_nonzero_bits (INT (1));
3717 4 : ASSERT_TRUE (r0.zero_p ());
3718 :
3719 : // Now test that range bounds are snapped to match bitmask alignments.
3720 4 : test_irange_snap_bounds ();
3721 4 : }
3722 :
3723 : // Build an frange from string endpoints.
3724 :
3725 : static inline frange
3726 712 : frange_float (const char *lb, const char *ub, tree type = float_type_node)
3727 : {
3728 712 : REAL_VALUE_TYPE min, max;
3729 712 : gcc_assert (real_from_string (&min, lb) == 0);
3730 712 : gcc_assert (real_from_string (&max, ub) == 0);
3731 712 : return frange (type, min, max);
3732 : }
3733 :
3734 : // Build the REAL_VALUE_TYPE for the string S.
3735 :
3736 : static REAL_VALUE_TYPE
3737 304 : real_from_str (const char *s)
3738 : {
3739 304 : REAL_VALUE_TYPE r;
3740 304 : gcc_assert (real_from_string (&r, s) == 0);
3741 304 : return r;
3742 : }
3743 :
3744 : static void
3745 8 : range_tests_sub_ranges ()
3746 : {
3747 8 : frange r0, r1;
3748 :
3749 : // A union of two disjoint intervals keeps both.
3750 8 : r0 = frange_float ("3", "5");
3751 8 : r1 = frange_float ("10", "12");
3752 8 : r0.union_ (r1);
3753 8 : ASSERT_EQ (r0.num_pairs (), 2);
3754 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("4")));
3755 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("11")));
3756 8 : ASSERT_FALSE (r0.contains_p (real_from_str ("7")));
3757 :
3758 8 : REAL_VALUE_TYPE three = real_from_str ("3");
3759 8 : REAL_VALUE_TYPE twelve = real_from_str ("12");
3760 8 : ASSERT_TRUE (real_identical (&r0.lower_bound (), &three));
3761 8 : ASSERT_TRUE (real_identical (&r0.upper_bound (), &twelve));
3762 :
3763 : // Intersecting away one side leaves a single interval again.
3764 8 : r1 = frange_float ("0", "6");
3765 8 : r0.intersect (r1);
3766 8 : ASSERT_EQ (r0.num_pairs (), 1);
3767 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("4")));
3768 8 : ASSERT_FALSE (r0.contains_p (real_from_str ("11")));
3769 :
3770 : // Overlapping intervals fuse rather than leave a gap.
3771 8 : r0 = frange_float ("3", "8");
3772 8 : r1 = frange_float ("5", "12");
3773 8 : r0.union_ (r1);
3774 8 : ASSERT_EQ (r0.num_pairs (), 1);
3775 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("7")));
3776 :
3777 8 : if (frange::MAX_PAIRS == 2)
3778 : {
3779 : // When more pieces arrive than fit, the last slot swallows the tail:
3780 : // [0,1] stays and [3,4], [100,101] merge into [3,101].
3781 8 : r0 = frange_float ("0", "1");
3782 8 : r1 = frange_float ("100", "101");
3783 8 : r0.union_ (r1);
3784 8 : r1 = frange_float ("3", "4");
3785 8 : r0.union_ (r1);
3786 8 : ASSERT_EQ (r0.num_pairs (), 2);
3787 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("50")));
3788 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("3.5")));
3789 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("100.5")));
3790 : }
3791 :
3792 : // Equality accounts for the sub-ranges.
3793 8 : r0 = frange_float ("3", "5");
3794 8 : r1 = frange_float ("10", "12");
3795 8 : r0.union_ (r1);
3796 8 : r1 = frange_float ("3", "12");
3797 8 : ASSERT_NE (r0, r1);
3798 :
3799 : // Intersecting every piece away, with the NAN cleared, leaves UNDEFINED.
3800 8 : r0 = frange_float ("3", "5");
3801 8 : r1 = frange_float ("10", "12");
3802 8 : r0.union_ (r1);
3803 8 : r0.clear_nan ();
3804 8 : r1 = frange_float ("20", "25");
3805 8 : r1.clear_nan ();
3806 8 : r0.intersect (r1);
3807 8 : ASSERT_TRUE (r0.undefined_p ());
3808 8 : }
3809 :
3810 : // Build a range that excludes the single point C.
3811 :
3812 : static frange
3813 112 : frange_float_excluding (const char *c)
3814 : {
3815 112 : REAL_VALUE_TYPE r = real_from_str (c);
3816 112 : frange f;
3817 112 : f.set (float_type_node, r, r, VR_ANTI_RANGE);
3818 112 : return f;
3819 : }
3820 :
3821 : static void
3822 8 : range_tests_excluding ()
3823 : {
3824 8 : frange r0, r1;
3825 :
3826 : // "x != 1.0" is two sub-ranges with 1.0 missing.
3827 8 : r0 = frange_float_excluding ("1.0");
3828 8 : ASSERT_FALSE (r0.varying_p ());
3829 8 : ASSERT_FALSE (r0.undefined_p ());
3830 8 : ASSERT_EQ (r0.num_pairs (), 2);
3831 8 : ASSERT_FALSE (r0.contains_p (real_from_str ("1.0")));
3832 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("2.0")));
3833 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("0.0")));
3834 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("-1.0")));
3835 8 : ASSERT_FALSE (r0.singleton_p ());
3836 : // A NAN compares unequal to everything, so this says nothing about NANs.
3837 8 : if (HONOR_NANS (float_type_node))
3838 8 : ASSERT_TRUE (r0.maybe_isnan ());
3839 : // The extremes still span the domain.
3840 8 : REAL_VALUE_TYPE dom_min = frange_val_min (float_type_node);
3841 8 : REAL_VALUE_TYPE dom_max = frange_val_max (float_type_node);
3842 8 : ASSERT_TRUE (real_identical (&r0.lower_bound (), &dom_min));
3843 8 : ASSERT_TRUE (real_identical (&r0.upper_bound (), &dom_max));
3844 :
3845 : // Any constant, not just 0.0 or 1.0.
3846 8 : r0 = frange_float_excluding ("5.5");
3847 8 : ASSERT_EQ (r0.num_pairs (), 2);
3848 8 : ASSERT_FALSE (r0.contains_p (real_from_str ("5.5")));
3849 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("5.4")));
3850 :
3851 : // "x != 1.0" met with [1.0, 1.0] is empty.
3852 8 : r0 = frange_float_excluding ("1.0");
3853 8 : r1 = frange_float ("1.0", "1.0");
3854 8 : r1.clear_nan ();
3855 8 : r0.intersect (r1);
3856 8 : ASSERT_TRUE (r0.undefined_p ());
3857 :
3858 : // Excluding a point outside a range changes nothing.
3859 8 : r0 = frange_float ("3.0", "5.0");
3860 8 : r0.clear_nan ();
3861 8 : r1 = frange_float_excluding ("1.0");
3862 8 : r0.intersect (r1);
3863 8 : ASSERT_EQ (r0.num_pairs (), 1);
3864 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("3.0")));
3865 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("5.0")));
3866 :
3867 : // Union puts the point back.
3868 8 : r0 = frange_float_excluding ("1.0");
3869 8 : r1 = frange_float ("1.0", "1.0");
3870 8 : r0.union_ (r1);
3871 8 : ASSERT_TRUE (r0.varying_p ());
3872 :
3873 : // Two different exclusions cannot both be held.
3874 8 : r0 = frange_float_excluding ("1.0");
3875 8 : r1 = frange_float_excluding ("2.0");
3876 8 : r0.union_ (r1);
3877 8 : ASSERT_TRUE (r0.varying_p ());
3878 :
3879 : // Nor can an intersection hold both.
3880 8 : r0 = frange_float_excluding ("1.0");
3881 8 : r1 = frange_float_excluding ("2.0");
3882 8 : r0.intersect (r1);
3883 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("0.0")));
3884 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("3.0")));
3885 :
3886 : // Equality accounts for the gap.
3887 8 : r0 = frange_float_excluding ("1.0");
3888 8 : r1 = frange_float_excluding ("2.0");
3889 8 : ASSERT_NE (r0, r1);
3890 8 : r1 = frange_float_excluding ("1.0");
3891 8 : ASSERT_EQ (r0, r1);
3892 8 : }
3893 :
3894 : static void
3895 8 : range_tests_sub_ranges_zero ()
3896 : {
3897 8 : frange r0, r1;
3898 :
3899 : // "x != 0.0" must exclude BOTH zeros, since -0.0 == 0.0 and so "x != 0.0" is
3900 : // false for either. The seam lands on the denormals either side of zero,
3901 : // which falls out of nextafter with no special case.
3902 8 : r0 = frange_float_excluding ("0.0");
3903 8 : ASSERT_EQ (r0.num_pairs (), 2);
3904 8 : ASSERT_FALSE (r0.contains_p (dconst0));
3905 8 : ASSERT_FALSE (r0.contains_p (dconstm0));
3906 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("1.0")));
3907 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("-1.0")));
3908 :
3909 : // Excluding zero from [-0.0, 5.0] eats the lower end entirely.
3910 8 : r0.set_nonzero (float_type_node);
3911 8 : ASSERT_FALSE (r0.contains_zero_p ());
3912 8 : ASSERT_FALSE (r0.contains_p (dconst0));
3913 8 : ASSERT_FALSE (r0.contains_p (dconstm0));
3914 :
3915 : // A NAN is not a zero, so clearing the NAN leaves the range nonzero.
3916 8 : r0.clear_nan ();
3917 8 : ASSERT_FALSE (r0.contains_zero_p ());
3918 :
3919 : // A range that avoids zero does not contain zero.
3920 8 : r0 = frange_float ("1.0", "10.0");
3921 8 : ASSERT_FALSE (r0.contains_zero_p ());
3922 :
3923 : // Excluding zero from [-0.0, 5.0] leaves (0, 5], which does not contain zero.
3924 8 : r0 = frange_float ("-0.0", "5.0");
3925 8 : r0.clear_nan ();
3926 8 : r1 = frange_float_excluding ("0.0");
3927 8 : r0.intersect (r1);
3928 8 : ASSERT_EQ (r0.num_pairs (), 1);
3929 8 : ASSERT_FALSE (r0.contains_zero_p ());
3930 8 : ASSERT_FALSE (r0.contains_p (dconst0));
3931 8 : ASSERT_FALSE (r0.contains_p (dconstm0));
3932 8 : ASSERT_TRUE (r0.contains_p (real_from_str ("5.0")));
3933 :
3934 : // -0.0 and +0.0 abut: nothing is representable between them, so the two
3935 : // halves fuse into one interval rather than leaving a gap.
3936 8 : r0 = frange_float ("-5", "-0.0");
3937 8 : r0.clear_nan ();
3938 8 : r1 = frange_float ("0.0", "5");
3939 8 : r1.clear_nan ();
3940 8 : r0.union_ (r1);
3941 8 : ASSERT_EQ (r0.num_pairs (), 1);
3942 8 : ASSERT_TRUE (r0.contains_p (dconst0));
3943 8 : ASSERT_TRUE (r0.contains_p (dconstm0));
3944 8 : }
3945 :
3946 : // A cached frange must come back with every sub-range intact.
3947 :
3948 : static void
3949 8 : range_tests_sub_ranges_storage ()
3950 : {
3951 8 : vrange_allocator alloc (false);
3952 :
3953 : // A two-piece range comes back as two pieces, unchanged.
3954 8 : frange r0 = frange_float ("3", "5");
3955 8 : frange r1 = frange_float ("10", "12");
3956 8 : r0.union_ (r1);
3957 8 : ASSERT_EQ (r0.num_pairs (), 2);
3958 :
3959 8 : vrange_storage *slot = alloc.clone (r0);
3960 8 : frange r2;
3961 8 : slot->get_vrange (r2, float_type_node);
3962 8 : ASSERT_EQ (r2.num_pairs (), 2);
3963 8 : ASSERT_EQ (r2, r0);
3964 8 : }
3965 :
3966 : // NANs and sub-ranges: unioning in a NAN keeps the intervals, while
3967 : // intersecting the intervals away collapses to a plain NAN with a single
3968 : // pair.
3969 :
3970 : static void
3971 4 : range_tests_sub_ranges_nan ()
3972 : {
3973 4 : frange r0, r1;
3974 :
3975 : // Union with a NAN keeps both intervals and gains the NAN.
3976 4 : r0 = frange_float ("3", "5");
3977 4 : r1 = frange_float ("10", "12");
3978 4 : r0.union_ (r1);
3979 4 : r0.clear_nan ();
3980 4 : r1.set_nan (float_type_node);
3981 4 : r0.union_ (r1);
3982 4 : ASSERT_EQ (r0.num_pairs (), 2);
3983 8 : ASSERT_TRUE (r0.maybe_isnan ());
3984 :
3985 : // Intersecting the intervals away leaves just the NAN.
3986 4 : r0 = frange_float ("3", "5");
3987 4 : r1 = frange_float ("10", "12");
3988 4 : r0.union_ (r1);
3989 4 : r1 = frange_float ("20", "25");
3990 4 : r0.intersect (r1);
3991 4 : ASSERT_TRUE (r0.known_isnan ());
3992 4 : ASSERT_EQ (r0.num_pairs (), 1);
3993 4 : }
3994 :
3995 : static void
3996 4 : range_tests_nan ()
3997 : {
3998 4 : frange r0, r1;
3999 4 : REAL_VALUE_TYPE q, r;
4000 4 : bool signbit;
4001 :
4002 : // Equal ranges but with differing NAN bits are not equal.
4003 4 : if (HONOR_NANS (float_type_node))
4004 : {
4005 4 : r1 = frange_float ("10", "12");
4006 4 : r0 = r1;
4007 4 : ASSERT_EQ (r0, r1);
4008 4 : r0.clear_nan ();
4009 4 : ASSERT_NE (r0, r1);
4010 4 : r0.update_nan ();
4011 4 : ASSERT_EQ (r0, r1);
4012 :
4013 : // [10, 20] NAN ^ [30, 40] NAN = NAN.
4014 4 : r0 = frange_float ("10", "20");
4015 4 : r1 = frange_float ("30", "40");
4016 4 : r0.intersect (r1);
4017 4 : ASSERT_TRUE (r0.known_isnan ());
4018 :
4019 : // [3,5] U [5,10] NAN = ... NAN
4020 4 : r0 = frange_float ("3", "5");
4021 4 : r0.clear_nan ();
4022 4 : r1 = frange_float ("5", "10");
4023 4 : r0.union_ (r1);
4024 8 : ASSERT_TRUE (r0.maybe_isnan ());
4025 : }
4026 :
4027 : // [5,6] U NAN = [5,6] NAN.
4028 4 : r0 = frange_float ("5", "6");
4029 4 : r0.clear_nan ();
4030 4 : r1.set_nan (float_type_node);
4031 4 : r0.union_ (r1);
4032 4 : real_from_string (&q, "5");
4033 4 : real_from_string (&r, "6");
4034 4 : ASSERT_TRUE (real_identical (&q, &r0.lower_bound ()));
4035 4 : ASSERT_TRUE (real_identical (&r, &r0.upper_bound ()));
4036 8 : ASSERT_TRUE (r0.maybe_isnan ());
4037 :
4038 : // NAN U NAN = NAN
4039 4 : r0.set_nan (float_type_node);
4040 4 : r1.set_nan (float_type_node);
4041 4 : r0.union_ (r1);
4042 4 : ASSERT_TRUE (r0.known_isnan ());
4043 :
4044 : // [INF, INF] NAN ^ NAN = NAN
4045 4 : r0.set_nan (float_type_node);
4046 4 : r1 = frange_float ("+Inf", "+Inf");
4047 4 : if (!HONOR_NANS (float_type_node))
4048 0 : r1.update_nan ();
4049 4 : r0.intersect (r1);
4050 4 : ASSERT_TRUE (r0.known_isnan ());
4051 :
4052 : // NAN ^ NAN = NAN
4053 4 : r0.set_nan (float_type_node);
4054 4 : r1.set_nan (float_type_node);
4055 4 : r0.intersect (r1);
4056 4 : ASSERT_TRUE (r0.known_isnan ());
4057 :
4058 : // +NAN ^ -NAN = UNDEFINED
4059 4 : r0.set_nan (float_type_node, false);
4060 4 : r1.set_nan (float_type_node, true);
4061 4 : r0.intersect (r1);
4062 4 : ASSERT_TRUE (r0.undefined_p ());
4063 :
4064 : // VARYING ^ NAN = NAN.
4065 4 : r0.set_nan (float_type_node);
4066 4 : r1.set_varying (float_type_node);
4067 4 : r0.intersect (r1);
4068 4 : ASSERT_TRUE (r0.known_isnan ());
4069 :
4070 : // [3,4] ^ NAN = UNDEFINED.
4071 4 : r0 = frange_float ("3", "4");
4072 4 : r0.clear_nan ();
4073 4 : r1.set_nan (float_type_node);
4074 4 : r0.intersect (r1);
4075 4 : ASSERT_TRUE (r0.undefined_p ());
4076 :
4077 : // [-3, 5] ^ NAN = UNDEFINED
4078 4 : r0 = frange_float ("-3", "5");
4079 4 : r0.clear_nan ();
4080 4 : r1.set_nan (float_type_node);
4081 4 : r0.intersect (r1);
4082 4 : ASSERT_TRUE (r0.undefined_p ());
4083 :
4084 : // Setting the NAN bit to yes does not make us a known NAN.
4085 4 : r0.set_varying (float_type_node);
4086 4 : r0.update_nan ();
4087 4 : ASSERT_FALSE (r0.known_isnan ());
4088 :
4089 : // NAN is in a VARYING.
4090 4 : r0.set_varying (float_type_node);
4091 4 : real_nan (&r, "", 1, TYPE_MODE (float_type_node));
4092 4 : REAL_VALUE_TYPE nan = r;
4093 4 : ASSERT_TRUE (r0.contains_p (nan));
4094 :
4095 : // -NAN is in a VARYING.
4096 4 : r0.set_varying (float_type_node);
4097 4 : q = real_value_negate (&r);
4098 4 : REAL_VALUE_TYPE neg_nan = q;
4099 4 : ASSERT_TRUE (r0.contains_p (neg_nan));
4100 :
4101 : // Clearing the NAN on a [] NAN is the empty set.
4102 4 : r0.set_nan (float_type_node);
4103 4 : r0.clear_nan ();
4104 4 : ASSERT_TRUE (r0.undefined_p ());
4105 :
4106 : // [10,20] NAN ^ [21,25] NAN = [NAN]
4107 4 : r0 = frange_float ("10", "20");
4108 4 : r0.update_nan ();
4109 4 : r1 = frange_float ("21", "25");
4110 4 : r1.update_nan ();
4111 4 : r0.intersect (r1);
4112 4 : ASSERT_TRUE (r0.known_isnan ());
4113 :
4114 : // NAN U [5,6] should be [5,6] +-NAN.
4115 4 : r0.set_nan (float_type_node);
4116 4 : r1 = frange_float ("5", "6");
4117 4 : r1.clear_nan ();
4118 4 : r0.union_ (r1);
4119 4 : real_from_string (&q, "5");
4120 4 : real_from_string (&r, "6");
4121 4 : ASSERT_TRUE (real_identical (&q, &r0.lower_bound ()));
4122 4 : ASSERT_TRUE (real_identical (&r, &r0.upper_bound ()));
4123 4 : ASSERT_TRUE (!r0.signbit_p (signbit));
4124 8 : ASSERT_TRUE (r0.maybe_isnan ());
4125 :
4126 : // NAN U NAN shouldn't change anything.
4127 4 : r0.set_nan (float_type_node);
4128 4 : r1.set_nan (float_type_node);
4129 4 : ASSERT_FALSE (r0.union_ (r1));
4130 :
4131 : // [3,5] NAN U NAN shouldn't change anything.
4132 4 : r0 = frange_float ("3", "5");
4133 4 : r1.set_nan (float_type_node);
4134 4 : ASSERT_FALSE (r0.union_ (r1));
4135 :
4136 : // [3,5] U NAN *does* trigger a change.
4137 4 : r0 = frange_float ("3", "5");
4138 4 : r0.clear_nan ();
4139 4 : r1.set_nan (float_type_node);
4140 4 : ASSERT_TRUE (r0.union_ (r1));
4141 4 : }
4142 :
4143 : static void
4144 8 : range_tests_signed_zeros ()
4145 : {
4146 8 : REAL_VALUE_TYPE zero = dconst0;
4147 8 : REAL_VALUE_TYPE neg_zero = zero;
4148 8 : neg_zero.sign = 1;
4149 8 : frange r0, r1;
4150 8 : bool signbit;
4151 :
4152 : // [0,0] contains [0,0] but not [-0,-0] and vice versa.
4153 8 : r0 = frange_float ("0.0", "0.0");
4154 8 : r1 = frange_float ("-0.0", "-0.0");
4155 8 : ASSERT_TRUE (r0.contains_p (zero));
4156 8 : ASSERT_TRUE (!r0.contains_p (neg_zero));
4157 8 : ASSERT_TRUE (r1.contains_p (neg_zero));
4158 8 : ASSERT_TRUE (!r1.contains_p (zero));
4159 :
4160 : // Test contains_p() when we know the sign of the zero.
4161 8 : r0 = frange_float ("0.0", "0.0");
4162 8 : ASSERT_TRUE (r0.contains_p (zero));
4163 8 : ASSERT_FALSE (r0.contains_p (neg_zero));
4164 8 : r0 = frange_float ("-0.0", "-0.0");
4165 8 : ASSERT_TRUE (r0.contains_p (neg_zero));
4166 8 : ASSERT_FALSE (r0.contains_p (zero));
4167 :
4168 8 : r0 = frange_float ("-0.0", "0.0");
4169 8 : ASSERT_TRUE (r0.contains_p (neg_zero));
4170 8 : ASSERT_TRUE (r0.contains_p (zero));
4171 :
4172 8 : r0 = frange_float ("-3", "5");
4173 8 : ASSERT_TRUE (r0.contains_p (neg_zero));
4174 8 : ASSERT_TRUE (r0.contains_p (zero));
4175 :
4176 : // The intersection of zeros that differ in sign is a NAN (or
4177 : // undefined if not honoring NANs).
4178 8 : r0 = frange_float ("-0.0", "-0.0");
4179 8 : r1 = frange_float ("0.0", "0.0");
4180 8 : r0.intersect (r1);
4181 8 : if (HONOR_NANS (float_type_node))
4182 4 : ASSERT_TRUE (r0.known_isnan ());
4183 : else
4184 4 : ASSERT_TRUE (r0.undefined_p ());
4185 :
4186 : // The union of zeros that differ in sign is a zero with unknown sign.
4187 8 : r0 = frange_float ("0.0", "0.0");
4188 8 : r1 = frange_float ("-0.0", "-0.0");
4189 8 : r0.union_ (r1);
4190 8 : ASSERT_TRUE (r0.zero_p () && !r0.signbit_p (signbit));
4191 :
4192 : // [-0, +0] has an unknown sign.
4193 8 : r0 = frange_float ("-0.0", "0.0");
4194 8 : ASSERT_TRUE (r0.zero_p () && !r0.signbit_p (signbit));
4195 :
4196 : // [-0, +0] ^ [0, 0] is [0, 0]
4197 8 : r0 = frange_float ("-0.0", "0.0");
4198 8 : r1 = frange_float ("0.0", "0.0");
4199 8 : r0.intersect (r1);
4200 8 : ASSERT_TRUE (r0.zero_p ());
4201 :
4202 8 : r0 = frange_float ("+0", "5");
4203 8 : r0.clear_nan ();
4204 8 : ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
4205 :
4206 8 : r0 = frange_float ("-0", "5");
4207 8 : r0.clear_nan ();
4208 8 : ASSERT_TRUE (!r0.signbit_p (signbit));
4209 :
4210 8 : r0 = frange_float ("-0", "10");
4211 8 : r1 = frange_float ("0", "5");
4212 8 : r0.intersect (r1);
4213 8 : ASSERT_TRUE (real_iszero (&r0.lower_bound (), false));
4214 :
4215 8 : r0 = frange_float ("-0", "5");
4216 8 : r1 = frange_float ("0", "5");
4217 8 : r0.union_ (r1);
4218 8 : ASSERT_TRUE (real_iszero (&r0.lower_bound (), true));
4219 :
4220 8 : r0 = frange_float ("-5", "-0");
4221 8 : r0.update_nan ();
4222 8 : r1 = frange_float ("0", "0");
4223 8 : r1.update_nan ();
4224 8 : r0.intersect (r1);
4225 8 : if (HONOR_NANS (float_type_node))
4226 4 : ASSERT_TRUE (r0.known_isnan ());
4227 : else
4228 4 : ASSERT_TRUE (r0.undefined_p ());
4229 :
4230 8 : r0.set_nonnegative (float_type_node);
4231 8 : if (HONOR_NANS (float_type_node))
4232 8 : ASSERT_TRUE (r0.maybe_isnan ());
4233 :
4234 : // Numbers containing zero should have an unknown SIGNBIT.
4235 8 : r0 = frange_float ("0", "10");
4236 8 : r0.clear_nan ();
4237 8 : ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
4238 8 : }
4239 :
4240 : static void
4241 8 : range_tests_signbit ()
4242 : {
4243 8 : frange r0, r1;
4244 8 : bool signbit;
4245 :
4246 : // Negative numbers should have the SIGNBIT set.
4247 8 : r0 = frange_float ("-5", "-1");
4248 8 : r0.clear_nan ();
4249 8 : ASSERT_TRUE (r0.signbit_p (signbit) && signbit);
4250 : // Positive numbers should have the SIGNBIT clear.
4251 8 : r0 = frange_float ("1", "10");
4252 8 : r0.clear_nan ();
4253 8 : ASSERT_TRUE (r0.signbit_p (signbit) && !signbit);
4254 : // Numbers spanning both positive and negative should have an
4255 : // unknown SIGNBIT.
4256 8 : r0 = frange_float ("-10", "10");
4257 8 : r0.clear_nan ();
4258 8 : ASSERT_TRUE (!r0.signbit_p (signbit));
4259 8 : r0.set_varying (float_type_node);
4260 8 : ASSERT_TRUE (!r0.signbit_p (signbit));
4261 8 : }
4262 :
4263 : static void
4264 8 : range_tests_flush_denormals ()
4265 : {
4266 : // We need -0.0 to exist for any of this to mean anything.
4267 32 : if (!MODE_HAS_SIGNED_ZEROS (TYPE_MODE (float_type_node)))
4268 0 : return;
4269 :
4270 8 : int save_flag = flag_signed_zeros;
4271 8 : flag_signed_zeros = 0;
4272 :
4273 : // Flushing a positive denormal lower bound to zero must canonicalize that
4274 : // zero to -0.0 to agree with set().
4275 8 : frange flushed = frange_float ("1e-40", "5");
4276 8 : flushed.clear_nan ();
4277 8 : flushed.flush_denormals_to_zero ();
4278 :
4279 8 : frange built = frange_float ("0", "5");
4280 8 : built.clear_nan ();
4281 :
4282 8 : ASSERT_TRUE (flushed == built);
4283 8 : ASSERT_TRUE (flushed.contains_p (dconstm0));
4284 8 : ASSERT_TRUE (flushed.contains_p (dconst0));
4285 :
4286 8 : flag_signed_zeros = save_flag;
4287 8 : }
4288 :
4289 : static void
4290 8 : range_tests_floats ()
4291 : {
4292 8 : frange r0, r1;
4293 :
4294 8 : if (HONOR_NANS (float_type_node))
4295 : {
4296 4 : range_tests_nan ();
4297 4 : range_tests_sub_ranges_nan ();
4298 : }
4299 8 : range_tests_signbit ();
4300 8 : range_tests_flush_denormals ();
4301 8 : range_tests_sub_ranges ();
4302 8 : range_tests_sub_ranges_storage ();
4303 8 : range_tests_excluding ();
4304 :
4305 8 : if (HONOR_SIGNED_ZEROS (float_type_node))
4306 : {
4307 8 : range_tests_signed_zeros ();
4308 8 : range_tests_sub_ranges_zero ();
4309 : }
4310 :
4311 : // A range of [-INF,+INF] is actually VARYING if no other properties
4312 : // are set.
4313 8 : r0 = frange_float ("-Inf", "+Inf");
4314 8 : ASSERT_TRUE (r0.varying_p ());
4315 : // ...unless it has some special property...
4316 8 : if (HONOR_NANS (r0.type ()))
4317 : {
4318 4 : r0.clear_nan ();
4319 4 : ASSERT_FALSE (r0.varying_p ());
4320 : }
4321 :
4322 : // For most architectures, where float and double are different
4323 : // sizes, having the same endpoints does not necessarily mean the
4324 : // ranges are equal.
4325 8 : if (!types_compatible_p (float_type_node, double_type_node))
4326 : {
4327 8 : r0 = frange_float ("3.0", "3.0", float_type_node);
4328 8 : r1 = frange_float ("3.0", "3.0", double_type_node);
4329 8 : ASSERT_NE (r0, r1);
4330 : }
4331 :
4332 : // [3,5] U [10,12] = [3,5][10,12]
4333 8 : r0 = frange_float ("3", "5");
4334 8 : r1 = frange_float ("10", "12");
4335 8 : r0.union_ (r1);
4336 8 : ASSERT_EQ (r0.num_pairs (), 2);
4337 8 : ASSERT_NE (r0, frange_float ("3", "12"));
4338 :
4339 : // [5,10] U [4,8] = [4,10]
4340 8 : r0 = frange_float ("5", "10");
4341 8 : r1 = frange_float ("4", "8");
4342 8 : r0.union_ (r1);
4343 8 : ASSERT_EQ (r0, frange_float ("4", "10"));
4344 :
4345 : // [3,5] U [4,10] = [3,10]
4346 8 : r0 = frange_float ("3", "5");
4347 8 : r1 = frange_float ("4", "10");
4348 8 : r0.union_ (r1);
4349 8 : ASSERT_EQ (r0, frange_float ("3", "10"));
4350 :
4351 : // [4,10] U [5,11] = [4,11]
4352 8 : r0 = frange_float ("4", "10");
4353 8 : r1 = frange_float ("5", "11");
4354 8 : r0.union_ (r1);
4355 8 : ASSERT_EQ (r0, frange_float ("4", "11"));
4356 :
4357 : // [3,12] ^ [10,12] = [10,12].
4358 8 : r0 = frange_float ("3", "12");
4359 8 : r1 = frange_float ("10", "12");
4360 8 : r0.intersect (r1);
4361 8 : ASSERT_EQ (r0, frange_float ("10", "12"));
4362 :
4363 : // [10,12] ^ [11,11] = [11,11]
4364 8 : r0 = frange_float ("10", "12");
4365 8 : r1 = frange_float ("11", "11");
4366 8 : r0.intersect (r1);
4367 8 : ASSERT_EQ (r0, frange_float ("11", "11"));
4368 :
4369 : // [10,20] ^ [5,15] = [10,15]
4370 8 : r0 = frange_float ("10", "20");
4371 8 : r1 = frange_float ("5", "15");
4372 8 : r0.intersect (r1);
4373 8 : ASSERT_EQ (r0, frange_float ("10", "15"));
4374 :
4375 : // [10,20] ^ [15,25] = [15,20]
4376 8 : r0 = frange_float ("10", "20");
4377 8 : r1 = frange_float ("15", "25");
4378 8 : r0.intersect (r1);
4379 8 : ASSERT_EQ (r0, frange_float ("15", "20"));
4380 :
4381 : // [10,20] ^ [21,25] = []
4382 8 : r0 = frange_float ("10", "20");
4383 8 : r0.clear_nan ();
4384 8 : r1 = frange_float ("21", "25");
4385 8 : r1.clear_nan ();
4386 8 : r0.intersect (r1);
4387 8 : ASSERT_TRUE (r0.undefined_p ());
4388 :
4389 8 : if (HONOR_INFINITIES (float_type_node))
4390 : {
4391 : // Make sure [-Inf, -Inf] doesn't get normalized.
4392 4 : r0 = frange_float ("-Inf", "-Inf");
4393 4 : ASSERT_TRUE (real_isinf (&r0.lower_bound (), true));
4394 4 : ASSERT_TRUE (real_isinf (&r0.upper_bound (), true));
4395 : }
4396 :
4397 : // Test that reading back a global range yields the same result as
4398 : // what we wrote into it.
4399 8 : tree ssa = make_temp_ssa_name (float_type_node, NULL, "blah");
4400 8 : r0.set_varying (float_type_node);
4401 8 : r0.clear_nan ();
4402 8 : set_range_info (ssa, r0);
4403 8 : get_global_range_query ()->range_of_expr (r1, ssa);
4404 8 : ASSERT_EQ (r0, r1);
4405 8 : }
4406 :
4407 : // Run floating range tests for various combinations of NAN and INF
4408 : // support.
4409 :
4410 : static void
4411 4 : range_tests_floats_various ()
4412 : {
4413 4 : int save_finite_math_only = flag_finite_math_only;
4414 :
4415 : // Test -ffinite-math-only.
4416 4 : flag_finite_math_only = 1;
4417 4 : range_tests_floats ();
4418 : // Test -fno-finite-math-only.
4419 4 : flag_finite_math_only = 0;
4420 4 : range_tests_floats ();
4421 :
4422 4 : flag_finite_math_only = save_finite_math_only;
4423 4 : }
4424 :
4425 : void
4426 4 : range_tests ()
4427 : {
4428 4 : range_tests_irange3 ();
4429 4 : range_tests_int_range_max ();
4430 4 : range_tests_strict_enum ();
4431 4 : range_tests_nonzero_bits ();
4432 4 : range_tests_floats_various ();
4433 4 : range_tests_misc ();
4434 4 : }
4435 :
4436 : } // namespace selftest
4437 :
4438 : #endif // CHECKING_P
|