Line data Source code
1 : /* Support routines for vrange storage.
2 : Copyright (C) 2022-2026 Free Software Foundation, Inc.
3 : Contributed by Aldy Hernandez <aldyh@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "tree.h"
26 : #include "gimple.h"
27 : #include "gimplify.h"
28 : #include "ssa.h"
29 : #include "fold-const.h"
30 : #include "gimple-range.h"
31 : #include "value-range-storage.h"
32 :
33 : // Generic memory allocator to share one interface between GC and
34 : // obstack allocators.
35 :
36 : class vrange_internal_alloc
37 : {
38 : public:
39 99387464 : vrange_internal_alloc () { }
40 99387452 : virtual ~vrange_internal_alloc () { }
41 : virtual void *alloc (size_t size) = 0;
42 : virtual void free (void *) = 0;
43 : private:
44 : DISABLE_COPY_AND_ASSIGN (vrange_internal_alloc);
45 : };
46 :
47 : class vrange_obstack_alloc final: public vrange_internal_alloc
48 : {
49 : public:
50 99085656 : vrange_obstack_alloc ()
51 99085656 : {
52 99085656 : obstack_init (&m_obstack);
53 99085656 : }
54 99085644 : virtual ~vrange_obstack_alloc () final override
55 99085644 : {
56 99085644 : obstack_free (&m_obstack, NULL);
57 99085644 : }
58 424380390 : virtual void *alloc (size_t size) final override
59 : {
60 424380390 : return obstack_alloc (&m_obstack, size);
61 : }
62 0 : virtual void free (void *) final override { }
63 : private:
64 : obstack m_obstack;
65 : };
66 :
67 : class vrange_ggc_alloc final: public vrange_internal_alloc
68 : {
69 : public:
70 301808 : vrange_ggc_alloc () { }
71 301808 : virtual ~vrange_ggc_alloc () final override { }
72 42784025 : virtual void *alloc (size_t size) final override
73 : {
74 42784025 : return ggc_internal_alloc (size);
75 : }
76 0 : virtual void free (void *p) final override
77 : {
78 0 : return ggc_free (p);
79 : }
80 : };
81 :
82 99387464 : vrange_allocator::vrange_allocator (bool gc)
83 : {
84 99387464 : if (gc)
85 301808 : m_alloc = new vrange_ggc_alloc;
86 : else
87 99085656 : m_alloc = new vrange_obstack_alloc;
88 99387464 : }
89 :
90 99387452 : vrange_allocator::~vrange_allocator ()
91 : {
92 99387452 : delete m_alloc;
93 99387452 : }
94 :
95 : void *
96 59755218 : vrange_allocator::alloc (size_t size)
97 : {
98 59755218 : return m_alloc->alloc (size);
99 : }
100 :
101 : void
102 0 : vrange_allocator::free (void *p)
103 : {
104 0 : m_alloc->free (p);
105 0 : }
106 :
107 : // Allocate a new vrange_storage object initialized to R and return
108 : // it.
109 :
110 : vrange_storage *
111 335846278 : vrange_allocator::clone (const vrange &r, bool shared_p)
112 : {
113 335846278 : return vrange_storage::alloc (*m_alloc, r, shared_p);
114 : }
115 :
116 : vrange_storage *
117 41744196 : vrange_allocator::clone_varying (tree type)
118 : {
119 41744196 : if (irange::supports_p (type))
120 19865243 : return irange_storage::alloc (*m_alloc, int_range <1> (type));
121 21878953 : if (prange::supports_p (type))
122 21394282 : return prange_storage::alloc (*m_alloc, prange (type));
123 484671 : if (frange::supports_p (type))
124 484671 : return frange_storage::alloc (*m_alloc, frange (type));
125 : return NULL;
126 : }
127 :
128 : vrange_storage *
129 29818723 : vrange_allocator::clone_undefined (tree type)
130 : {
131 29818723 : if (irange::supports_p (type))
132 19763249 : return irange_storage::alloc (*m_alloc, int_range<1> ());
133 10055474 : if (prange::supports_p (type))
134 9574967 : return prange_storage::alloc (*m_alloc, prange ());
135 480507 : if (frange::supports_p (type))
136 480507 : return frange_storage::alloc (*m_alloc, frange ());
137 : return NULL;
138 : }
139 :
140 : // Allocate a new vrange_storage object initialized to R and return
141 : // it. Return NULL if R is unsupported.
142 :
143 : vrange_storage *
144 335846278 : vrange_storage::alloc (vrange_internal_alloc &allocator, const vrange &r,
145 : bool shared_p)
146 : {
147 335846278 : if (is_a <irange> (r))
148 233015649 : return irange_storage::alloc (allocator, as_a <irange> (r));
149 102830629 : if (is_a <prange> (r))
150 90750404 : return prange_storage::alloc (allocator, as_a <prange> (r), shared_p);
151 12080225 : if (is_a <frange> (r))
152 12080225 : return frange_storage::alloc (allocator, as_a <frange> (r));
153 : return NULL;
154 : }
155 :
156 : // Set storage to R.
157 :
158 : void
159 24620047 : vrange_storage::set_vrange (const vrange &r)
160 : {
161 24620047 : if (irange_storage *s = dyn_irange ())
162 : {
163 20249910 : gcc_checking_assert (s->fits_p (as_a <irange> (r)));
164 20249910 : s->set_irange (as_a <irange> (r));
165 : }
166 4370137 : else if (prange_storage *s = dyn_prange ())
167 : {
168 3886848 : gcc_checking_assert (s->fits_p (as_a <prange> (r)));
169 3886848 : s->set_prange (as_a <prange> (r));
170 : }
171 483289 : else if (frange_storage *s = dyn_frange ())
172 : {
173 483289 : gcc_checking_assert (s->fits_p (as_a <frange> (r)));
174 483289 : s->set_frange (as_a <frange> (r));
175 : }
176 : else
177 0 : gcc_unreachable ();
178 :
179 : // Verify that reading back from the cache didn't drop bits.
180 24620047 : if (flag_checking
181 : // FIXME: Avoid checking frange, as it currently pessimizes some ranges:
182 : //
183 : // gfortran.dg/pr49472.f90 pessimizes [0.0, 1.0] into [-0.0, 1.0].
184 24619976 : && !is_a <frange> (r)
185 48756734 : && !r.undefined_p ())
186 : {
187 23979833 : value_range tmp (r);
188 23979833 : get_vrange (tmp, r.type ());
189 23979833 : gcc_checking_assert (tmp == r);
190 23979833 : }
191 24620047 : }
192 :
193 : // Restore R from storage.
194 :
195 : void
196 2040381030 : vrange_storage::get_vrange (vrange &r, tree type) const
197 : {
198 2040381030 : if (const irange_storage *s = dyn_irange ())
199 1492807590 : s->get_irange (as_a <irange> (r), type);
200 547573440 : else if (const prange_storage *s = dyn_prange ())
201 499992505 : s->get_prange (as_a <prange> (r), type);
202 47580935 : else if (const frange_storage *s = dyn_frange ())
203 47580935 : s->get_frange (as_a <frange> (r), type);
204 : else
205 0 : gcc_unreachable ();
206 2040381030 : }
207 :
208 : // Return TRUE if storage can fit R.
209 :
210 : bool
211 29493922 : vrange_storage::fits_p (const vrange &r) const
212 : {
213 29493922 : if (const irange_storage *s = dyn_irange ())
214 24468597 : return s->fits_p (as_a <irange> (r));
215 5025325 : if (const prange_storage *s = dyn_prange ())
216 4494062 : return s->fits_p (as_a <prange> (r));
217 531263 : if (const frange_storage *s = dyn_frange ())
218 531263 : return s->fits_p (as_a <frange> (r));
219 0 : gcc_unreachable ();
220 : }
221 :
222 : // Return TRUE if the range in storage is equal to R. It is the
223 : // caller's responsibility to verify that the type of the range in
224 : // storage matches that of R.
225 :
226 : bool
227 15283598 : vrange_storage::equal_p (const vrange &r) const
228 : {
229 15283598 : if (const irange_storage *s = dyn_irange ())
230 3977501 : return s->equal_p (as_a <irange> (r));
231 11306097 : if (const prange_storage *s = dyn_prange ())
232 11285855 : return s->equal_p (as_a <prange> (r));
233 20242 : if (const frange_storage *s = dyn_frange ())
234 20242 : return s->equal_p (as_a <frange> (r));
235 0 : gcc_unreachable ();
236 : }
237 :
238 : //============================================================================
239 : // irange_storage implementation
240 : //============================================================================
241 :
242 : unsigned short *
243 1165950984 : irange_storage::write_lengths_address ()
244 : {
245 1165950984 : return (unsigned short *)&m_val[(m_num_ranges * 2 + 2)
246 1165950984 : * WIDE_INT_MAX_HWIS (m_precision)];
247 : }
248 :
249 : const unsigned short *
250 1013574104 : irange_storage::lengths_address () const
251 : {
252 1013574104 : return const_cast <irange_storage *> (this)->write_lengths_address ();
253 : }
254 :
255 : // Allocate a new irange_storage object initialized to R.
256 :
257 : irange_storage *
258 272644141 : irange_storage::alloc (vrange_internal_alloc &allocator, const irange &r)
259 : {
260 272644141 : size_t size = irange_storage::size (r);
261 272644141 : irange_storage *p = static_cast <irange_storage *> (allocator.alloc (size));
262 272644141 : new (p) irange_storage (r);
263 272644141 : return p;
264 : }
265 :
266 : // Initialize the storage with R.
267 :
268 272644141 : irange_storage::irange_storage (const irange &r)
269 272644141 : : vrange_storage (VR_IRANGE), m_max_ranges (r.num_pairs ())
270 : {
271 272644141 : m_num_ranges = m_max_ranges;
272 272644141 : set_irange (r);
273 272644141 : }
274 :
275 : static inline void
276 733245306 : write_wide_int (HOST_WIDE_INT *&val, unsigned short *&len, const wide_int &w)
277 : {
278 733245306 : *len = w.get_len ();
279 1467834166 : for (unsigned i = 0; i < *len; ++i)
280 734588860 : *val++ = w.elt (i);
281 733245306 : ++len;
282 733245306 : }
283 :
284 : // Store R into the current storage.
285 :
286 : void
287 292894051 : irange_storage::set_irange (const irange &r)
288 : {
289 292894051 : gcc_checking_assert (fits_p (r));
290 :
291 292894051 : if (r.undefined_p ())
292 : {
293 20211434 : m_kind = VR_UNDEFINED;
294 140517171 : return;
295 : }
296 272682617 : if (r.varying_p ())
297 : {
298 120305737 : m_kind = VR_VARYING;
299 120305737 : return;
300 : }
301 :
302 152376880 : m_precision = TYPE_PRECISION (r.type ());
303 152376880 : m_num_ranges = r.num_pairs ();
304 152376880 : m_kind = VR_RANGE;
305 :
306 152376880 : HOST_WIDE_INT *val = &m_val[0];
307 152376880 : unsigned short *len = write_lengths_address ();
308 :
309 366622653 : for (unsigned i = 0; i < r.num_pairs (); ++i)
310 : {
311 214245773 : write_wide_int (val, len, r.lower_bound (i));
312 214248607 : write_wide_int (val, len, r.upper_bound (i));
313 : }
314 :
315 : // TODO: We could avoid streaming out the value if the mask is -1.
316 152376880 : irange_bitmask bm = r.m_bitmask;
317 152376880 : write_wide_int (val, len, bm.value ());
318 152376880 : write_wide_int (val, len, bm.mask ());
319 152376880 : }
320 :
321 : static inline void
322 4943524222 : read_wide_int (wide_int &w,
323 : const HOST_WIDE_INT *val, unsigned short len, unsigned prec)
324 : {
325 4943524222 : trailing_wide_int_storage stow (prec, &len,
326 1226057502 : const_cast <HOST_WIDE_INT *> (val));
327 3717466720 : w = trailing_wide_int (stow);
328 : }
329 :
330 : // Restore a range of TYPE from storage into R.
331 :
332 : void
333 1496343481 : irange_storage::get_irange (irange &r, tree type) const
334 : {
335 1496343481 : if (m_kind == VR_UNDEFINED)
336 : {
337 10164686 : r.set_undefined ();
338 492934063 : return;
339 : }
340 1486178795 : if (m_kind == VR_VARYING)
341 : {
342 472604691 : r.set_varying (type);
343 472604691 : return;
344 : }
345 :
346 1013574104 : gcc_checking_assert (TYPE_PRECISION (type) == m_precision);
347 1013574104 : const HOST_WIDE_INT *val = &m_val[0];
348 1013574104 : const unsigned short *len = lengths_address ();
349 :
350 : // Handle the common case where R can fit the new range.
351 1013574104 : if (r.m_max_ranges >= m_num_ranges)
352 : {
353 977919338 : r.m_kind = VR_RANGE;
354 977919338 : r.m_num_ranges = m_num_ranges;
355 977919338 : r.m_type = type;
356 3469328556 : for (unsigned i = 0; i < m_num_ranges * 2; ++i)
357 : {
358 2491409218 : read_wide_int (r.m_base[i], val, *len, m_precision);
359 2491409218 : val += *len++;
360 : }
361 : }
362 : // Otherwise build the range piecewise.
363 : else
364 : {
365 35654766 : r.set_undefined ();
366 283792930 : for (unsigned i = 0; i < m_num_ranges; ++i)
367 : {
368 212483398 : wide_int lb, ub;
369 212483398 : read_wide_int (lb, val, *len, m_precision);
370 212483398 : val += *len++;
371 212483398 : read_wide_int (ub, val, *len, m_precision);
372 212483398 : val += *len++;
373 212483398 : int_range<1> tmp (type, lb, ub);
374 212483398 : r.union_ (tmp);
375 213074090 : }
376 : }
377 :
378 1013574104 : wide_int bits_value, bits_mask;
379 1013574104 : read_wide_int (bits_value, val, *len, m_precision);
380 1013574104 : val += *len++;
381 1013574104 : read_wide_int (bits_mask, val, *len, m_precision);
382 1013574104 : r.m_bitmask = irange_bitmask (bits_value, bits_mask);
383 1013574104 : if (r.m_kind == VR_VARYING)
384 0 : r.m_kind = VR_RANGE;
385 :
386 1013574104 : if (flag_checking)
387 1013570616 : r.verify_range ();
388 1013647381 : }
389 :
390 : bool
391 3977501 : irange_storage::equal_p (const irange &r) const
392 : {
393 3977501 : if (m_kind == VR_UNDEFINED || r.undefined_p ())
394 0 : return m_kind == r.m_kind;
395 3977501 : if (m_kind == VR_VARYING || r.varying_p ())
396 441610 : return m_kind == r.m_kind;
397 :
398 : // ?? We could make this faster by doing the comparison in place,
399 : // without going through get_irange.
400 3535891 : int_range_max tmp;
401 3535891 : get_irange (tmp, r.type ());
402 3535891 : return tmp == r;
403 3535891 : }
404 :
405 : // Return the size in bytes to allocate storage that can hold R.
406 :
407 : size_t
408 272644141 : irange_storage::size (const irange &r)
409 : {
410 272644141 : if (r.undefined_p ())
411 : return sizeof (irange_storage);
412 :
413 252581009 : unsigned prec = TYPE_PRECISION (r.type ());
414 252581009 : unsigned n = r.num_pairs () * 2 + 2;
415 252581009 : unsigned hwi_size = ((n * WIDE_INT_MAX_HWIS (prec) - 1)
416 : * sizeof (HOST_WIDE_INT));
417 252581009 : unsigned len_size = n * sizeof (unsigned short);
418 252581009 : return sizeof (irange_storage) + hwi_size + len_size;
419 : }
420 :
421 : // Return TRUE if R fits in the current storage.
422 :
423 : bool
424 337612558 : irange_storage::fits_p (const irange &r) const
425 : {
426 337612558 : return m_max_ranges >= r.num_pairs ();
427 : }
428 :
429 : void
430 0 : irange_storage::dump () const
431 : {
432 0 : fprintf (stderr, "irange_storage (prec=%d, ranges=%d):\n",
433 0 : m_precision, m_num_ranges);
434 :
435 0 : if (m_num_ranges == 0)
436 : return;
437 :
438 0 : const HOST_WIDE_INT *val = &m_val[0];
439 0 : const unsigned short *len = lengths_address ();
440 0 : int i, j;
441 :
442 0 : fprintf (stderr, " lengths = [ ");
443 0 : for (i = 0; i < m_num_ranges * 2 + 2; ++i)
444 0 : fprintf (stderr, "%d ", len[i]);
445 0 : fprintf (stderr, "]\n");
446 :
447 0 : for (i = 0; i < m_num_ranges; ++i)
448 : {
449 0 : for (j = 0; j < *len; ++j)
450 0 : fprintf (stderr, " [PAIR %d] LB " HOST_WIDE_INT_PRINT_DEC "\n", i,
451 0 : *val++);
452 0 : ++len;
453 0 : for (j = 0; j < *len; ++j)
454 0 : fprintf (stderr, " [PAIR %d] UB " HOST_WIDE_INT_PRINT_DEC "\n", i,
455 0 : *val++);
456 0 : ++len;
457 : }
458 :
459 : // Dump value/mask pair.
460 0 : for (j = 0; j < *len; ++j)
461 0 : fprintf (stderr, " [VALUE] " HOST_WIDE_INT_PRINT_DEC "\n", *val++);
462 0 : ++len;
463 0 : for (j = 0; j < *len; ++j)
464 0 : fprintf (stderr, " [MASK] " HOST_WIDE_INT_PRINT_DEC "\n", *val++);
465 : }
466 :
467 : DEBUG_FUNCTION void
468 0 : debug (const irange_storage &storage)
469 : {
470 0 : storage.dump ();
471 0 : fprintf (stderr, "\n");
472 0 : }
473 :
474 : //============================================================================
475 : // frange_storage implementation
476 : //============================================================================
477 :
478 : // Return the number of bytes to allocate for an frange_storage holding R.
479 :
480 : size_t
481 13045403 : frange_storage::size (const frange &r)
482 : {
483 13045403 : return sizeof (frange_storage) + (r.num_pairs () - 1) * sizeof (frange_pair);
484 : }
485 :
486 : // Allocate a new frange_storage object initialized to R.
487 :
488 : frange_storage *
489 13045403 : frange_storage::alloc (vrange_internal_alloc &allocator, const frange &r)
490 : {
491 13045403 : frange_storage *p
492 13045403 : = static_cast <frange_storage *> (allocator.alloc (size (r)));
493 13045403 : new (p) frange_storage (r);
494 13045403 : return p;
495 : }
496 :
497 13045403 : frange_storage::frange_storage (const frange &r)
498 13045403 : : vrange_storage (VR_FRANGE), m_max_ranges (r.num_pairs ())
499 : {
500 13045403 : set_frange (r);
501 13045403 : }
502 :
503 : void
504 13528692 : frange_storage::set_frange (const frange &r)
505 : {
506 13528692 : gcc_checking_assert (fits_p (r));
507 :
508 13528692 : m_kind = r.m_kind;
509 13528692 : m_num_ranges = r.m_num_ranges;
510 27544219 : for (unsigned i = 0; i < r.m_num_ranges; ++i)
511 14015527 : m_pairs[i] = r.m_pairs[i];
512 13528692 : m_pos_nan = r.m_pos_nan;
513 13528692 : m_neg_nan = r.m_neg_nan;
514 13528692 : }
515 :
516 : void
517 47601177 : frange_storage::get_frange (frange &r, tree type) const
518 : {
519 47601177 : gcc_checking_assert (r.supports_type_p (type));
520 :
521 : // Handle explicit NANs.
522 47601177 : if (m_kind == VR_NAN)
523 : {
524 113204 : if (HONOR_NANS (type))
525 : {
526 113204 : if (m_pos_nan && m_neg_nan)
527 107129 : r.set_nan (type);
528 : else
529 6075 : r.set_nan (type, m_neg_nan);
530 : }
531 : else
532 0 : r.set_undefined ();
533 : return;
534 : }
535 47487973 : if (m_kind == VR_UNDEFINED)
536 : {
537 62145 : r.set_undefined ();
538 62145 : return;
539 : }
540 47425828 : if (m_kind == VR_VARYING)
541 : {
542 33790210 : r.set_varying (type);
543 33790210 : return;
544 : }
545 :
546 : // Rebuild piecewise, like irange_storage::get_irange().
547 13635618 : r.set_undefined ();
548 42520570 : for (unsigned i = 0; i < m_num_ranges; ++i)
549 : {
550 15249334 : frange tmp (type, m_pairs[i].min, m_pairs[i].max, m_kind);
551 15249334 : r.union_ (tmp);
552 15249334 : }
553 :
554 : // The constructor will set the NAN bits for HONOR_NANS, but we must
555 : // make sure to set the NAN sign if known.
556 13635618 : if (HONOR_NANS (type) && (m_pos_nan ^ m_neg_nan) == 1)
557 1366940 : r.update_nan (m_neg_nan);
558 12268678 : else if (!m_pos_nan && !m_neg_nan)
559 9774119 : r.clear_nan ();
560 : }
561 :
562 : bool
563 20242 : frange_storage::equal_p (const frange &r) const
564 : {
565 20242 : if (r.undefined_p ())
566 0 : return m_kind == VR_UNDEFINED;
567 :
568 20242 : frange tmp;
569 20242 : get_frange (tmp, r.type ());
570 20242 : return tmp == r;
571 20242 : }
572 :
573 : bool
574 14543244 : frange_storage::fits_p (const frange &r) const
575 : {
576 14543244 : return m_max_ranges >= r.num_pairs ();
577 : }
578 :
579 : //============================================================================
580 : // prange_storage implementation
581 : //============================================================================
582 :
583 : prange_storage *
584 121719653 : prange_storage::alloc (vrange_internal_alloc &allocator, const prange &r,
585 : bool shared_p)
586 : {
587 121719653 : unsigned num_words;
588 121719653 : prange_format (r, num_words);
589 121719653 : size_t extra_size = 0;
590 121719653 : if (num_words)
591 : {
592 5512105 : unsigned short precision = TYPE_PRECISION (r.type ());
593 5512105 : extra_size = trailing_wide_ints<PRANGE_STORAGE_NINTS>
594 5512105 : ::extra_size (precision, num_words);
595 : }
596 :
597 121719653 : size_t size = sizeof (prange_storage) + extra_size;
598 121719653 : prange_storage *p = static_cast <prange_storage *> (allocator.alloc (size));
599 121719653 : new (p) prange_storage (r);
600 121719653 : if (p->m_addr && !shared_p)
601 963318 : p->m_addr = unshare_expr_without_location (p->m_addr);
602 :
603 121719653 : return p;
604 : }
605 :
606 : // Initialize the storage with R.
607 :
608 121719653 : prange_storage::prange_storage (const prange &r) : vrange_storage (VR_PRANGE)
609 : {
610 : // None of these is part of the range, so none is ever copied from one.
611 : // Only the accessors in tree-ssanames.cc ever make them non-zero, and
612 : // range_info_set_range carries them across a reallocation.
613 121719653 : pt = NULL;
614 :
615 121719653 : unsigned num_words;
616 121719653 : enum prange_kind kind = prange_format (r, num_words);
617 121719653 : unsigned short prec = (kind == PR_UNDEFINED) ? 0 : TYPE_PRECISION (r.type ());
618 121719653 : m_trailing_ints.set_precision (prec, num_words);
619 121719653 : set_prange (r);
620 121719653 : }
621 :
622 : // Return TRUE if R is exactly the nonzero set [1, MAX], which prange_storage
623 : // encodes compactly as PR_NONZERO.
624 : //
625 : // Compare the bounds against a fresh set_nonzero () rather than using
626 : // prange::operator==, because operator== also compares the bitmask and
627 : // address info, which are stored separately here, so a non-null pointer that
628 : // also carries e.g. an alignment bitmask still belongs in PR_NONZERO.
629 :
630 : static inline bool
631 158676929 : nonzero_range_p (const prange &r)
632 : {
633 158676929 : prange nonzero (r.type ());
634 158676929 : nonzero.set_nonzero (r.type ());
635 317353858 : return (r.lower_bound () == nonzero.lower_bound ()
636 306007365 : && r.upper_bound () == nonzero.upper_bound ());
637 158676929 : }
638 :
639 : // Return the prange_kind for range R, and the number of words of storage
640 : // it requires in NUM_WORDS.
641 :
642 : enum prange_kind
643 377409589 : prange_storage::prange_format (const prange &r, unsigned &num_words)
644 : {
645 377409589 : num_words = 0;
646 377409589 : if (r.undefined_p ())
647 : return PR_UNDEFINED;
648 :
649 348524159 : if (r.varying_p ())
650 : return PR_VARYING;
651 :
652 149388822 : if (r.zero_p ())
653 : return PR_ZERO;
654 :
655 148052912 : enum prange_kind kind = PR_NONZERO;
656 :
657 148052912 : if (!nonzero_range_p (r))
658 : {
659 12197069 : prange tmp (r.type ());
660 24394138 : if (r.lower_bound () == tmp.lower_bound ()
661 22556592 : && r.upper_bound () == tmp.upper_bound ())
662 : kind = PR_FULL;
663 : else
664 : {
665 : // PR_OTHER requires words of storage for the end points.
666 3151718 : kind = PR_OTHER;
667 3151718 : num_words += 2;
668 : }
669 12197069 : }
670 :
671 : // Bitmasks require 2 words of storage.
672 148052912 : if (!r.get_bitmask ().unknown_p ())
673 16790889 : num_words += 2;
674 :
675 : // PR_FULL must have a bitmask or an address, or it should be PR_VARYING.
676 157098263 : gcc_checking_assert (kind != PR_FULL || !r.get_bitmask ().unknown_p ()
677 : || r.m_addr != NULL_TREE);
678 148052912 : return kind;
679 : }
680 :
681 : void
682 125606501 : prange_storage::set_prange (const prange &r)
683 : {
684 125606501 : unsigned num_words;
685 125606501 : m_kind = prange_format (r, num_words);
686 125606501 : m_has_bitmask = false;
687 125606501 : m_known_p = false;
688 125606501 : m_addr = r.m_addr;
689 :
690 125606501 : unsigned index = 0;
691 :
692 125606501 : switch (m_kind)
693 : {
694 76459215 : case PR_UNDEFINED:
695 76459215 : case PR_VARYING:
696 76459215 : case PR_ZERO:
697 76459215 : return;
698 : case PR_NONZERO:
699 : case PR_FULL:
700 : break;
701 996982 : case PR_OTHER:
702 996982 : set_word (index++, r.lower_bound (), r.type ());
703 996982 : set_word (index++, r.upper_bound (), r.type ());
704 996982 : break;
705 0 : default:
706 0 : gcc_unreachable ();
707 : }
708 :
709 49147286 : m_has_bitmask = !r.get_bitmask ().unknown_p ();
710 49147286 : m_known_p = r.m_known_p;
711 :
712 49147286 : if (m_has_bitmask)
713 : {
714 5408988 : irange_bitmask bm = r.m_bitmask;
715 5408988 : set_word (index++, r.m_bitmask.value (), r.type ());
716 5408988 : set_word (index++, r.m_bitmask.mask (), r.type ());
717 5408988 : }
718 49147286 : gcc_checking_assert (index == num_words);
719 : }
720 :
721 : void
722 499992505 : prange_storage::get_prange (prange &r, tree type) const
723 : {
724 499992505 : gcc_checking_assert (r.supports_type_p (type));
725 499992505 : unsigned index = 0;
726 499992505 : switch (m_kind)
727 : {
728 898025 : case PR_UNDEFINED:
729 898025 : r.set_undefined ();
730 898025 : return;
731 :
732 299953326 : case PR_VARYING:
733 299953326 : r.set_varying (type);
734 299953326 : return;
735 :
736 761289 : case PR_ZERO:
737 761289 : r.set_zero (type);
738 761289 : return;
739 :
740 183598158 : case PR_NONZERO:
741 183598158 : r.set_nonzero (type);
742 183598158 : break;
743 :
744 10891859 : case PR_FULL:
745 10891859 : {
746 10891859 : r.m_kind = VR_RANGE;
747 10891859 : r.m_type = type;
748 10891859 : prange tmp (type);
749 10891859 : r.m_min = tmp.lower_bound ();
750 10891859 : r.m_max = tmp.upper_bound ();
751 10891859 : break;
752 10891859 : }
753 :
754 3889848 : case PR_OTHER:
755 3889848 : {
756 3889848 : gcc_checking_assert (m_kind == PR_OTHER);
757 3889848 : r.m_kind = VR_RANGE;
758 3889848 : r.m_type = type;
759 3889848 : r.m_min = get_word (index++, type);
760 3889848 : r.m_max = get_word (index++, type);
761 3889848 : break;
762 : }
763 0 : default:
764 0 : gcc_unreachable ();
765 : }
766 :
767 198379865 : if (m_has_bitmask)
768 : {
769 32126090 : wide_int value = get_word (index++, type);
770 32126090 : wide_int mask = get_word (index++, type);
771 32126090 : r.m_bitmask = irange_bitmask (value, mask);
772 32126090 : }
773 : else
774 166253775 : r.m_bitmask.set_unknown (TYPE_PRECISION (type));
775 :
776 198379865 : r.m_known_p = m_known_p;
777 198379865 : r.m_addr = m_addr;
778 :
779 198379865 : if (flag_checking)
780 198379667 : r.verify_range ();
781 : }
782 :
783 : bool
784 11285855 : prange_storage::equal_p (const prange &r) const
785 : {
786 11285855 : if (r.undefined_p ())
787 0 : return m_kind == PR_UNDEFINED;
788 :
789 11285855 : unsigned index = 0;
790 11285855 : switch (m_kind)
791 : {
792 8692 : case PR_VARYING:
793 8692 : return r.varying_p ();
794 :
795 102742 : case PR_ZERO:
796 102742 : return r.zero_p ();
797 :
798 10624017 : case PR_NONZERO:
799 10624017 : if (!nonzero_range_p (r))
800 : return false;
801 : break;
802 :
803 516575 : case PR_FULL:
804 516575 : if (r.m_min != wi::zero (TYPE_PRECISION (r.m_type))
805 951345 : || r.m_max != wi::max_value (TYPE_PRECISION (r.m_type),
806 217385 : TYPE_SIGN (r.m_type)))
807 : return false;
808 : break;
809 :
810 33829 : case PR_OTHER:
811 67658 : if (r.m_min != get_word (index++, r.m_type)
812 50364 : || r.m_max != get_word (index++, r.m_type))
813 : return false;
814 : break;
815 :
816 0 : default:
817 0 : gcc_unreachable ();
818 : }
819 :
820 10617631 : if (m_has_bitmask)
821 : {
822 4195418 : wide_int value = get_word (index++, r.m_type);
823 4195418 : wide_int mask = get_word (index++, r.m_type);
824 4195418 : if (r.m_bitmask != irange_bitmask (value, mask))
825 1647451 : return false;
826 4195418 : }
827 : else
828 6422213 : if (!r.m_bitmask.unknown_p ())
829 : return false;
830 :
831 7025337 : if (m_addr != r.m_addr)
832 : return false;
833 : // Storage objects are only equal If they point to the same memory.
834 2545900 : if (m_known_p != r.m_known_p)
835 0 : return false;
836 :
837 : return true;
838 : }
839 :
840 : bool
841 8380910 : prange_storage::fits_p (const prange &r) const
842 : {
843 : // Undefined ranges always fit, because they don't store anything in
844 : // the trailing wide ints.
845 8380910 : if (r.undefined_p ())
846 : return true;
847 :
848 8363782 : unsigned num_words;
849 8363782 : prange_format (r, num_words);
850 8363782 : return num_words <= m_trailing_ints.num_elements ();
851 : }
852 :
853 : // Ensure this range of type TYPE does not contain zero.
854 :
855 : void
856 4702136 : prange_storage::set_nonnull (tree type)
857 : {
858 4702136 : gcc_checking_assert (m_kind != PR_UNDEFINED);
859 4702136 : switch (m_kind)
860 : {
861 1722945 : case PR_VARYING:
862 1722945 : case PR_FULL:
863 1722945 : m_kind = PR_NONZERO;
864 1722945 : return;
865 :
866 : case PR_NONZERO:
867 : return;
868 :
869 0 : case PR_ZERO:
870 0 : m_kind = PR_UNDEFINED;
871 0 : return;
872 :
873 32640 : case PR_OTHER:
874 32640 : if (get_word (0, type) != 0)
875 : return;
876 : // [0,1] -> [1,1] is a special case and requires adjustments.
877 : // otherwise we can simply set the lower bound to 1.
878 0 : if (get_word (1, type) != 1)
879 0 : set_word (0, wi::one (TYPE_PRECISION (type)), type);
880 : else
881 : {
882 : // This is very uncoomon, and rather than trying to cover
883 : // the edge cases (ie, bitmask present, address present)
884 : // simply load the range, remove zero, and resave it.
885 0 : prange pr;
886 0 : this->get_vrange (pr, type);
887 0 : prange nz;
888 0 : nz.set_nonzero (type);
889 0 : if (pr.intersect (nz))
890 0 : this->set_prange (pr);
891 0 : }
892 : return;
893 :
894 0 : default:
895 0 : gcc_unreachable ();
896 : }
897 : }
898 :
899 : irange_bitmask
900 94454542 : prange_storage::get_bitmask (tree type) const
901 : {
902 94454542 : gcc_checking_assert (m_kind != PR_UNDEFINED);
903 94454542 : unsigned prec = TYPE_PRECISION (type);
904 94454542 : unsigned index = 0;
905 :
906 94454542 : switch (m_kind)
907 : {
908 66749139 : case PR_VARYING:
909 66749139 : return irange_bitmask (prec);
910 :
911 412 : case PR_ZERO:
912 412 : return irange_bitmask (type, wi::zero (prec), wi::zero (prec));
913 :
914 : case PR_NONZERO:
915 : case PR_FULL:
916 : break;
917 :
918 332023 : case PR_OTHER:
919 332023 : index = 2;
920 332023 : break;
921 :
922 0 : default:
923 0 : gcc_unreachable ();
924 : }
925 :
926 27704991 : if (m_has_bitmask)
927 : {
928 10339566 : wide_int value = get_word (index++, type);
929 10339566 : wide_int mask = get_word (index++, type);
930 10339566 : return irange_bitmask (value, mask);
931 10339566 : }
932 17365425 : return irange_bitmask (prec);
933 : }
934 :
935 :
936 : static vrange_allocator ggc_vrange_allocator (true);
937 :
938 11817152 : vrange_storage *ggc_alloc_vrange_storage (tree type)
939 : {
940 11817152 : return ggc_vrange_allocator.clone_varying (type);
941 : }
942 :
943 30966873 : vrange_storage *ggc_alloc_vrange_storage (const vrange &r, bool shared_p)
944 : {
945 30966873 : return ggc_vrange_allocator.clone (r, shared_p);
946 : }
|