1 /* Support routines for vrange storage.
2 Copyright (C) 2022-2023 Free Software Foundation, Inc.
3 Contributed by Aldy Hernandez <aldyh@redhat.com>.
5 This file is part of GCC.
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)
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.
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/>. */
23 #include "coretypes.h"
28 #include "tree-pretty-print.h"
29 #include "fold-const.h"
30 #include "gimple-range.h"
31 #include "value-range-storage.h"
33 // Generic memory allocator to share one interface between GC and
34 // obstack allocators.
36 class vrange_internal_alloc
39 vrange_internal_alloc () { }
40 virtual ~vrange_internal_alloc () { }
41 virtual void *alloc (size_t size
) = 0;
42 virtual void free (void *) = 0;
44 DISABLE_COPY_AND_ASSIGN (vrange_internal_alloc
);
47 class vrange_obstack_alloc final
: public vrange_internal_alloc
50 vrange_obstack_alloc ()
52 obstack_init (&m_obstack
);
54 virtual ~vrange_obstack_alloc () final override
56 obstack_free (&m_obstack
, NULL
);
58 virtual void *alloc (size_t size
) final override
60 return obstack_alloc (&m_obstack
, size
);
62 virtual void free (void *) final override
{ }
67 class vrange_ggc_alloc final
: public vrange_internal_alloc
70 vrange_ggc_alloc () { }
71 virtual ~vrange_ggc_alloc () final override
{ }
72 virtual void *alloc (size_t size
) final override
74 return ggc_internal_alloc (size
);
76 virtual void free (void *p
) final override
82 vrange_allocator::vrange_allocator (bool gc
)
85 m_alloc
= new vrange_ggc_alloc
;
87 m_alloc
= new vrange_obstack_alloc
;
90 vrange_allocator::~vrange_allocator ()
96 vrange_allocator::alloc (size_t size
)
98 return m_alloc
->alloc (size
);
102 vrange_allocator::free (void *p
)
107 // Allocate a new vrange_storage object initialized to R and return
111 vrange_allocator::clone (const vrange
&r
)
113 return vrange_storage::alloc (*m_alloc
, r
);
117 vrange_allocator::clone_varying (tree type
)
119 if (irange::supports_p (type
))
120 return irange_storage::alloc (*m_alloc
, int_range
<1> (type
));
121 if (frange::supports_p (type
))
122 return frange_storage::alloc (*m_alloc
, frange (type
));
127 vrange_allocator::clone_undefined (tree type
)
129 if (irange::supports_p (type
))
130 return irange_storage::alloc (*m_alloc
, int_range
<1> ());
131 if (frange::supports_p (type
))
132 return frange_storage::alloc (*m_alloc
, frange ());
136 // Allocate a new vrange_storage object initialized to R and return
137 // it. Return NULL if R is unsupported.
140 vrange_storage::alloc (vrange_internal_alloc
&allocator
, const vrange
&r
)
142 if (is_a
<irange
> (r
))
143 return irange_storage::alloc (allocator
, as_a
<irange
> (r
));
144 if (is_a
<frange
> (r
))
145 return frange_storage::alloc (allocator
, as_a
<frange
> (r
));
152 vrange_storage::set_vrange (const vrange
&r
)
154 if (is_a
<irange
> (r
))
156 irange_storage
*s
= static_cast <irange_storage
*> (this);
157 gcc_checking_assert (s
->fits_p (as_a
<irange
> (r
)));
158 s
->set_irange (as_a
<irange
> (r
));
160 else if (is_a
<frange
> (r
))
162 frange_storage
*s
= static_cast <frange_storage
*> (this);
163 gcc_checking_assert (s
->fits_p (as_a
<frange
> (r
)));
164 s
->set_frange (as_a
<frange
> (r
));
170 // Restore R from storage.
173 vrange_storage::get_vrange (vrange
&r
, tree type
) const
175 if (is_a
<irange
> (r
))
177 const irange_storage
*s
= static_cast <const irange_storage
*> (this);
178 s
->get_irange (as_a
<irange
> (r
), type
);
180 else if (is_a
<frange
> (r
))
182 const frange_storage
*s
= static_cast <const frange_storage
*> (this);
183 s
->get_frange (as_a
<frange
> (r
), type
);
189 // Return TRUE if storage can fit R.
192 vrange_storage::fits_p (const vrange
&r
) const
194 if (is_a
<irange
> (r
))
196 const irange_storage
*s
= static_cast <const irange_storage
*> (this);
197 return s
->fits_p (as_a
<irange
> (r
));
199 if (is_a
<frange
> (r
))
201 const frange_storage
*s
= static_cast <const frange_storage
*> (this);
202 return s
->fits_p (as_a
<frange
> (r
));
208 // Return TRUE if the range in storage is equal to R.
211 vrange_storage::equal_p (const vrange
&r
, tree type
) const
213 if (is_a
<irange
> (r
))
215 const irange_storage
*s
= static_cast <const irange_storage
*> (this);
216 return s
->equal_p (as_a
<irange
> (r
), type
);
218 if (is_a
<frange
> (r
))
220 const frange_storage
*s
= static_cast <const frange_storage
*> (this);
221 return s
->equal_p (as_a
<frange
> (r
), type
);
226 //============================================================================
227 // irange_storage implementation
228 //============================================================================
231 irange_storage::write_lengths_address ()
233 return (unsigned char *)&m_val
[(m_num_ranges
* 2 + 1)
234 * WIDE_INT_MAX_HWIS (m_precision
)];
237 const unsigned char *
238 irange_storage::lengths_address () const
240 return const_cast <irange_storage
*> (this)->write_lengths_address ();
243 // Allocate a new irange_storage object initialized to R.
246 irange_storage::alloc (vrange_internal_alloc
&allocator
, const irange
&r
)
248 size_t size
= irange_storage::size (r
);
249 irange_storage
*p
= static_cast <irange_storage
*> (allocator
.alloc (size
));
250 new (p
) irange_storage (r
);
254 // Initialize the storage with R.
256 irange_storage::irange_storage (const irange
&r
)
257 : m_max_ranges (r
.num_pairs ())
259 m_num_ranges
= m_max_ranges
;
264 write_wide_int (HOST_WIDE_INT
*&val
, unsigned char *&len
, const wide_int
&w
)
267 for (unsigned i
= 0; i
< *len
; ++i
)
272 // Store R into the current storage.
275 irange_storage::set_irange (const irange
&r
)
277 gcc_checking_assert (fits_p (r
));
279 if (r
.undefined_p ())
281 m_kind
= VR_UNDEFINED
;
290 m_precision
= TYPE_PRECISION (r
.type ());
291 m_num_ranges
= r
.num_pairs ();
294 HOST_WIDE_INT
*val
= &m_val
[0];
295 unsigned char *len
= write_lengths_address ();
297 for (unsigned i
= 0; i
< r
.num_pairs (); ++i
)
299 write_wide_int (val
, len
, r
.lower_bound (i
));
300 write_wide_int (val
, len
, r
.upper_bound (i
));
302 write_wide_int (val
, len
, r
.m_nonzero_mask
);
307 get_irange (tmp
, r
.type ());
308 gcc_checking_assert (tmp
== r
);
313 read_wide_int (wide_int
&w
,
314 const HOST_WIDE_INT
*val
, unsigned char len
, unsigned prec
)
316 trailing_wide_int_storage
stow (prec
, &len
,
317 const_cast <HOST_WIDE_INT
*> (val
));
318 w
= trailing_wide_int (stow
);
321 // Restore a range of TYPE from storage into R.
324 irange_storage::get_irange (irange
&r
, tree type
) const
326 if (m_kind
== VR_UNDEFINED
)
331 if (m_kind
== VR_VARYING
)
333 r
.set_varying (type
);
337 gcc_checking_assert (TYPE_PRECISION (type
) == m_precision
);
338 const HOST_WIDE_INT
*val
= &m_val
[0];
339 const unsigned char *len
= lengths_address ();
341 // Handle the common case where R can fit the new range.
342 if (r
.m_max_ranges
>= m_num_ranges
)
345 r
.m_num_ranges
= m_num_ranges
;
347 for (unsigned i
= 0; i
< m_num_ranges
* 2; ++i
)
349 read_wide_int (r
.m_base
[i
], val
, *len
, m_precision
);
353 // Otherwise build the range piecewise.
357 for (unsigned i
= 0; i
< m_num_ranges
; ++i
)
360 read_wide_int (lb
, val
, *len
, m_precision
);
362 read_wide_int (ub
, val
, *len
, m_precision
);
364 int_range
<1> tmp (type
, lb
, ub
);
368 read_wide_int (r
.m_nonzero_mask
, val
, *len
, m_precision
);
369 if (r
.m_kind
== VR_VARYING
)
377 irange_storage::equal_p (const irange
&r
, tree type
) const
379 if (m_kind
== VR_UNDEFINED
|| r
.undefined_p ())
380 return m_kind
== r
.m_kind
;
381 if (m_kind
== VR_VARYING
|| r
.varying_p ())
382 return m_kind
== r
.m_kind
&& types_compatible_p (r
.type (), type
);
384 tree rtype
= r
.type ();
385 if (!types_compatible_p (rtype
, type
))
388 // ?? We could make this faster by doing the comparison in place,
389 // without going through get_irange.
391 get_irange (tmp
, rtype
);
395 // Return the size in bytes to allocate storage that can hold R.
398 irange_storage::size (const irange
&r
)
400 if (r
.undefined_p ())
401 return sizeof (irange_storage
);
403 unsigned prec
= TYPE_PRECISION (r
.type ());
404 unsigned n
= r
.num_pairs () * 2 + 1;
405 unsigned hwi_size
= ((n
* WIDE_INT_MAX_HWIS (prec
) - 1)
406 * sizeof (HOST_WIDE_INT
));
407 unsigned len_size
= n
;
408 return sizeof (irange_storage
) + hwi_size
+ len_size
;
411 // Return TRUE if R fits in the current storage.
414 irange_storage::fits_p (const irange
&r
) const
416 return m_max_ranges
>= r
.num_pairs ();
420 irange_storage::dump () const
422 fprintf (stderr
, "irange_storage (prec=%d, ranges=%d):\n",
423 m_precision
, m_num_ranges
);
425 if (m_num_ranges
== 0)
428 const HOST_WIDE_INT
*val
= &m_val
[0];
429 const unsigned char *len
= lengths_address ();
432 fprintf (stderr
, " lengths = [ ");
433 for (i
= 0; i
< m_num_ranges
* 2 + 1; ++i
)
434 fprintf (stderr
, "%d ", len
[i
]);
435 fprintf (stderr
, "]\n");
437 for (i
= 0; i
< m_num_ranges
; ++i
)
439 for (j
= 0; j
< *len
; ++j
)
440 fprintf (stderr
, " [PAIR %d] LB " HOST_WIDE_INT_PRINT_DEC
"\n", i
,
443 for (j
= 0; j
< *len
; ++j
)
444 fprintf (stderr
, " [PAIR %d] UB " HOST_WIDE_INT_PRINT_DEC
"\n", i
,
448 for (j
= 0; j
< *len
; ++j
)
449 fprintf (stderr
, " [NZ] " HOST_WIDE_INT_PRINT_DEC
"\n", *val
++);
453 debug (const irange_storage
&storage
)
456 fprintf (stderr
, "\n");
459 //============================================================================
460 // frange_storage implementation
461 //============================================================================
463 // Allocate a new frange_storage object initialized to R.
466 frange_storage::alloc (vrange_internal_alloc
&allocator
, const frange
&r
)
468 size_t size
= sizeof (frange_storage
);
469 frange_storage
*p
= static_cast <frange_storage
*> (allocator
.alloc (size
));
470 new (p
) frange_storage (r
);
475 frange_storage::set_frange (const frange
&r
)
477 gcc_checking_assert (fits_p (r
));
482 m_pos_nan
= r
.m_pos_nan
;
483 m_neg_nan
= r
.m_neg_nan
;
487 frange_storage::get_frange (frange
&r
, tree type
) const
489 gcc_checking_assert (r
.supports_type_p (type
));
491 // Handle explicit NANs.
492 if (m_kind
== VR_NAN
)
494 if (HONOR_NANS (type
))
496 if (m_pos_nan
&& m_neg_nan
)
499 r
.set_nan (type
, m_neg_nan
);
505 if (m_kind
== VR_UNDEFINED
)
511 // We use the constructor to create the new range instead of writing
512 // out the bits into the frange directly, because the global range
513 // being read may be being inlined into a function with different
514 // restrictions as when it was originally written. We want to make
515 // sure the resulting range is canonicalized correctly for the new
517 r
= frange (type
, m_min
, m_max
, m_kind
);
519 // The constructor will set the NAN bits for HONOR_NANS, but we must
520 // make sure to set the NAN sign if known.
521 if (HONOR_NANS (type
) && (m_pos_nan
^ m_neg_nan
) == 1)
522 r
.update_nan (m_neg_nan
);
523 else if (!m_pos_nan
&& !m_neg_nan
)
528 frange_storage::equal_p (const frange
&r
, tree type
) const
530 if (r
.undefined_p ())
531 return m_kind
== VR_UNDEFINED
;
534 if (!types_compatible_p (rtype
, type
))
538 get_frange (tmp
, rtype
);
543 frange_storage::fits_p (const frange
&) const
548 static vrange_allocator
ggc_vrange_allocator (true);
550 vrange_storage
*ggc_alloc_vrange_storage (tree type
)
552 return ggc_vrange_allocator
.clone_varying (type
);
555 vrange_storage
*ggc_alloc_vrange_storage (const vrange
&r
)
557 return ggc_vrange_allocator
.clone (r
);