c++: Implement modules ABI for vtable emissions
[official-gcc.git] / gcc / value-range.h
blobf1c638f8cd004f3f0e31d418a752d17fc1caa843
1 /* Support routines for value ranges.
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
3 Contributed by Aldy Hernandez <aldyh@redhat.com> and
4 Andrew Macleod <amacleod@redhat.com>.
6 This file is part of GCC.
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.
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.
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/>. */
22 #ifndef GCC_VALUE_RANGE_H
23 #define GCC_VALUE_RANGE_H
25 class irange;
27 // Types of value ranges.
28 enum value_range_kind
30 /* Empty range. */
31 VR_UNDEFINED,
32 /* Range spans the entire domain. */
33 VR_VARYING,
34 /* Range is [MIN, MAX]. */
35 VR_RANGE,
36 /* Range is ~[MIN, MAX]. */
37 VR_ANTI_RANGE,
38 /* Range is a NAN. */
39 VR_NAN,
40 /* Range is a nice guy. */
41 VR_LAST
44 // Discriminator between different vrange types.
46 enum value_range_discriminator
48 // Range holds an integer or pointer.
49 VR_IRANGE,
50 // Floating point range.
51 VR_FRANGE,
52 // Range holds an unsupported type.
53 VR_UNKNOWN
56 // Abstract class for ranges of any of the supported types.
58 // To query what types ranger and the entire ecosystem can support,
59 // use Value_Range::supports_type_p(tree type). This is a static
60 // method available independently of any vrange object.
62 // To query what a given vrange variant can support, use:
63 // irange::supports_p ()
64 // frange::supports_p ()
65 // etc
67 // To query what a range object can support, use:
68 // void foo (vrange &v, irange &i, frange &f)
69 // {
70 // if (v.supports_type_p (type)) ...
71 // if (i.supports_type_p (type)) ...
72 // if (f.supports_type_p (type)) ...
73 // }
75 class vrange
77 template <typename T> friend bool is_a (vrange &);
78 friend class Value_Range;
79 friend void streamer_write_vrange (struct output_block *, const vrange &);
80 friend class range_op_handler;
81 public:
82 virtual void accept (const class vrange_visitor &v) const = 0;
83 virtual void set (tree, tree, value_range_kind = VR_RANGE) = 0;
84 virtual tree type () const = 0;
85 virtual bool supports_type_p (const_tree type) const = 0;
86 virtual void set_varying (tree type) = 0;
87 virtual void set_undefined () = 0;
88 virtual bool union_ (const vrange &) = 0;
89 virtual bool intersect (const vrange &) = 0;
90 virtual bool singleton_p (tree *result = NULL) const = 0;
91 virtual bool contains_p (tree cst) const = 0;
92 virtual bool zero_p () const = 0;
93 virtual bool nonzero_p () const = 0;
94 virtual void set_nonzero (tree type) = 0;
95 virtual void set_zero (tree type) = 0;
96 virtual void set_nonnegative (tree type) = 0;
97 virtual bool fits_p (const vrange &r) const = 0;
98 virtual ~vrange () { }
99 virtual tree lbound () const = 0;
100 virtual tree ubound () const = 0;
101 virtual void update_bitmask (const class irange_bitmask &);
102 virtual irange_bitmask get_bitmask () const;
103 wide_int get_nonzero_bits () const;
104 void set_nonzero_bits (const wide_int &bits);
106 bool varying_p () const;
107 bool undefined_p () const;
108 vrange& operator= (const vrange &);
109 bool operator== (const vrange &) const;
110 bool operator!= (const vrange &r) const { return !(*this == r); }
111 void dump (FILE *) const;
112 protected:
113 vrange (enum value_range_discriminator d) : m_discriminator (d) { }
114 ENUM_BITFIELD(value_range_kind) m_kind : 8;
115 const ENUM_BITFIELD(value_range_discriminator) m_discriminator : 4;
118 namespace inchash
120 extern void add_vrange (const vrange &, hash &, unsigned flags = 0);
123 // A pair of values representing the known bits in a range. Zero bits
124 // in MASK cover constant values. Set bits in MASK cover unknown
125 // values. VALUE are the known bits.
127 // Set bits in MASK (no meaningful information) must have their
128 // corresponding bits in VALUE cleared, as this speeds up union and
129 // intersect.
131 class irange_bitmask
133 public:
134 irange_bitmask () { /* uninitialized */ }
135 irange_bitmask (unsigned prec) { set_unknown (prec); }
136 irange_bitmask (const wide_int &value, const wide_int &mask);
137 wide_int value () const { return m_value; }
138 wide_int mask () const { return m_mask; }
139 void set_unknown (unsigned prec);
140 bool unknown_p () const;
141 unsigned get_precision () const;
142 void union_ (const irange_bitmask &src);
143 void intersect (const irange_bitmask &src);
144 bool operator== (const irange_bitmask &src) const;
145 bool operator!= (const irange_bitmask &src) const { return !(*this == src); }
146 void verify_mask () const;
147 void dump (FILE *) const;
149 bool member_p (const wide_int &val) const;
150 void adjust_range (irange &r) const;
152 // Convenience functions for nonzero bitmask compatibility.
153 wide_int get_nonzero_bits () const;
154 void set_nonzero_bits (const wide_int &bits);
155 private:
156 wide_int m_value;
157 wide_int m_mask;
160 inline void
161 irange_bitmask::set_unknown (unsigned prec)
163 m_value = wi::zero (prec);
164 m_mask = wi::minus_one (prec);
165 if (flag_checking)
166 verify_mask ();
169 // Return TRUE if THIS does not have any meaningful information.
171 inline bool
172 irange_bitmask::unknown_p () const
174 return m_mask == -1;
177 inline
178 irange_bitmask::irange_bitmask (const wide_int &value, const wide_int &mask)
180 m_value = value;
181 m_mask = mask;
182 if (flag_checking)
183 verify_mask ();
186 inline unsigned
187 irange_bitmask::get_precision () const
189 return m_mask.get_precision ();
192 // The following two functions are meant for backwards compatability
193 // with the nonzero bitmask. A cleared bit means the value must be 0.
194 // A set bit means we have no information for the bit.
196 // Return the nonzero bits.
197 inline wide_int
198 irange_bitmask::get_nonzero_bits () const
200 return m_value | m_mask;
203 // Set the bitmask to the nonzero bits in BITS.
204 inline void
205 irange_bitmask::set_nonzero_bits (const wide_int &bits)
207 m_value = wi::zero (bits.get_precision ());
208 m_mask = bits;
209 if (flag_checking)
210 verify_mask ();
213 // Return TRUE if val could be a valid value with this bitmask.
215 inline bool
216 irange_bitmask::member_p (const wide_int &val) const
218 if (unknown_p ())
219 return true;
220 wide_int res = m_mask & val;
221 if (m_value != 0)
222 res |= ~m_mask & m_value;
223 return res == val;
226 inline bool
227 irange_bitmask::operator== (const irange_bitmask &src) const
229 bool unknown1 = unknown_p ();
230 bool unknown2 = src.unknown_p ();
231 if (unknown1 || unknown2)
232 return unknown1 == unknown2;
233 return m_value == src.m_value && m_mask == src.m_mask;
236 inline void
237 irange_bitmask::union_ (const irange_bitmask &src)
239 m_mask = (m_mask | src.m_mask) | (m_value ^ src.m_value);
240 m_value = m_value & src.m_value;
241 if (flag_checking)
242 verify_mask ();
245 inline void
246 irange_bitmask::intersect (const irange_bitmask &src)
248 // If we have two known bits that are incompatible, the resulting
249 // bit is undefined. It is unclear whether we should set the entire
250 // range to UNDEFINED, or just a subset of it. For now, set the
251 // entire bitmask to unknown (VARYING).
252 if (wi::bit_and (~(m_mask | src.m_mask),
253 m_value ^ src.m_value) != 0)
255 unsigned prec = m_mask.get_precision ();
256 m_mask = wi::minus_one (prec);
257 m_value = wi::zero (prec);
259 else
261 m_mask = m_mask & src.m_mask;
262 m_value = m_value | src.m_value;
264 if (flag_checking)
265 verify_mask ();
268 // An integer range without any storage.
270 class irange : public vrange
272 friend class irange_storage;
273 friend class vrange_printer;
274 public:
275 // In-place setters.
276 void set (tree type, const wide_int &, const wide_int &,
277 value_range_kind = VR_RANGE);
278 virtual void set_nonzero (tree type) override;
279 virtual void set_zero (tree type) override;
280 virtual void set_nonnegative (tree type) override;
281 virtual void set_varying (tree type) override;
282 virtual void set_undefined () override;
284 // Range types.
285 static bool supports_p (const_tree type);
286 virtual bool supports_type_p (const_tree type) const override;
287 virtual tree type () const override;
289 // Iteration over sub-ranges.
290 unsigned num_pairs () const;
291 wide_int lower_bound (unsigned = 0) const;
292 wide_int upper_bound (unsigned) const;
293 wide_int upper_bound () const;
294 virtual tree lbound () const override;
295 virtual tree ubound () const override;
297 // Predicates.
298 virtual bool zero_p () const override;
299 virtual bool nonzero_p () const override;
300 virtual bool singleton_p (tree *result = NULL) const override;
301 bool singleton_p (wide_int &) const;
302 bool contains_p (const wide_int &) const;
303 bool nonnegative_p () const;
304 bool nonpositive_p () const;
306 // In-place operators.
307 virtual bool union_ (const vrange &) override;
308 virtual bool intersect (const vrange &) override;
309 void invert ();
311 // Operator overloads.
312 irange& operator= (const irange &);
313 bool operator== (const irange &) const;
314 bool operator!= (const irange &r) const { return !(*this == r); }
316 // Misc methods.
317 virtual bool fits_p (const vrange &r) const override;
318 virtual void accept (const vrange_visitor &v) const override;
320 virtual void update_bitmask (const class irange_bitmask &) override;
321 virtual irange_bitmask get_bitmask () const override;
323 protected:
324 void maybe_resize (int needed);
325 virtual void set (tree, tree, value_range_kind = VR_RANGE) override;
326 virtual bool contains_p (tree cst) const override;
327 irange (wide_int *, unsigned nranges, bool resizable);
329 // In-place operators.
330 bool irange_contains_p (const irange &) const;
331 bool irange_single_pair_union (const irange &r);
333 void normalize_kind ();
335 void verify_range ();
337 // Hard limit on max ranges allowed.
338 static const int HARD_MAX_RANGES = 255;
339 private:
340 bool varying_compatible_p () const;
341 bool intersect_bitmask (const irange &r);
342 bool union_bitmask (const irange &r);
343 bool set_range_from_bitmask ();
345 bool intersect (const wide_int& lb, const wide_int& ub);
346 bool union_append (const irange &r);
347 unsigned char m_num_ranges;
348 bool m_resizable;
349 unsigned char m_max_ranges;
350 tree m_type;
351 irange_bitmask m_bitmask;
352 protected:
353 wide_int *m_base;
356 // Here we describe an irange with N pairs of ranges. The storage for
357 // the pairs is embedded in the class as an array.
359 // If RESIZABLE is true, the storage will be resized on the heap when
360 // the number of ranges needed goes past N up to a max of
361 // HARD_MAX_RANGES. This new storage is freed upon destruction.
363 template<unsigned N, bool RESIZABLE = false>
364 class int_range : public irange
366 public:
367 int_range ();
368 int_range (tree type, const wide_int &, const wide_int &,
369 value_range_kind = VR_RANGE);
370 int_range (tree type);
371 int_range (const int_range &);
372 int_range (const irange &);
373 ~int_range () final override;
374 int_range& operator= (const int_range &);
375 protected:
376 int_range (tree, tree, value_range_kind = VR_RANGE);
377 private:
378 wide_int m_ranges[N*2];
381 // Unsupported temporaries may be created by ranger before it's known
382 // they're unsupported, or by vr_values::get_value_range.
384 class unsupported_range : public vrange
386 public:
387 unsupported_range ()
388 : vrange (VR_UNKNOWN)
390 set_undefined ();
392 unsupported_range (const unsupported_range &src)
393 : vrange (VR_UNKNOWN)
395 unsupported_range::operator= (src);
397 void set (tree min, tree, value_range_kind = VR_RANGE) final override;
398 tree type () const final override;
399 bool supports_type_p (const_tree) const final override;
400 void set_varying (tree) final override;
401 void set_undefined () final override;
402 void accept (const vrange_visitor &v) const final override;
403 bool union_ (const vrange &r) final override;
404 bool intersect (const vrange &r) final override;
405 bool singleton_p (tree * = NULL) const final override;
406 bool contains_p (tree) const final override;
407 bool zero_p () const final override;
408 bool nonzero_p () const final override;
409 void set_nonzero (tree type) final override;
410 void set_zero (tree type) final override;
411 void set_nonnegative (tree type) final override;
412 bool fits_p (const vrange &) const final override;
413 unsupported_range& operator= (const unsupported_range &r);
414 tree lbound () const final override;
415 tree ubound () const final override;
418 // The NAN state as an opaque object.
420 class nan_state
422 public:
423 nan_state (bool);
424 nan_state (bool pos_nan, bool neg_nan);
425 bool neg_p () const;
426 bool pos_p () const;
427 private:
428 bool m_pos_nan;
429 bool m_neg_nan;
432 // Set NAN state to +-NAN if NAN_P is true. Otherwise set NAN state
433 // to false.
435 inline
436 nan_state::nan_state (bool nan_p)
438 m_pos_nan = nan_p;
439 m_neg_nan = nan_p;
442 // Constructor initializing the object to +NAN if POS_NAN is set, -NAN
443 // if NEG_NAN is set, or +-NAN if both are set. Otherwise POS_NAN and
444 // NEG_NAN are clear, and the object cannot be a NAN.
446 inline
447 nan_state::nan_state (bool pos_nan, bool neg_nan)
449 m_pos_nan = pos_nan;
450 m_neg_nan = neg_nan;
453 // Return if +NAN is possible.
455 inline bool
456 nan_state::pos_p () const
458 return m_pos_nan;
461 // Return if -NAN is possible.
463 inline bool
464 nan_state::neg_p () const
466 return m_neg_nan;
469 // A floating point range.
471 // The representation is a type with a couple of endpoints, unioned
472 // with the set of { -NAN, +Nan }.
474 class frange : public vrange
476 friend class frange_storage;
477 friend class vrange_printer;
478 public:
479 frange ();
480 frange (const frange &);
481 frange (tree, tree, value_range_kind = VR_RANGE);
482 frange (tree type);
483 frange (tree type, const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
484 value_range_kind = VR_RANGE);
485 static bool supports_p (const_tree type)
487 // ?? Decimal floats can have multiple representations for the
488 // same number. Supporting them may be as simple as just
489 // disabling them in singleton_p. No clue.
490 return SCALAR_FLOAT_TYPE_P (type) && !DECIMAL_FLOAT_TYPE_P (type);
492 virtual tree type () const override;
493 void set (tree type, const REAL_VALUE_TYPE &, const REAL_VALUE_TYPE &,
494 value_range_kind = VR_RANGE);
495 void set (tree type, const REAL_VALUE_TYPE &, const REAL_VALUE_TYPE &,
496 const nan_state &, value_range_kind = VR_RANGE);
497 void set_nan (tree type);
498 void set_nan (tree type, bool sign);
499 void set_nan (tree type, const nan_state &);
500 virtual void set_varying (tree type) override;
501 virtual void set_undefined () override;
502 virtual bool union_ (const vrange &) override;
503 virtual bool intersect (const vrange &) override;
504 bool contains_p (const REAL_VALUE_TYPE &) const;
505 virtual bool singleton_p (tree *result = NULL) const override;
506 bool singleton_p (REAL_VALUE_TYPE &r) const;
507 virtual bool supports_type_p (const_tree type) const override;
508 virtual void accept (const vrange_visitor &v) const override;
509 virtual bool zero_p () const override;
510 virtual bool nonzero_p () const override;
511 virtual void set_nonzero (tree type) override;
512 virtual void set_zero (tree type) override;
513 virtual void set_nonnegative (tree type) override;
514 virtual bool fits_p (const vrange &) const override;
515 frange& operator= (const frange &);
516 bool operator== (const frange &) const;
517 bool operator!= (const frange &r) const { return !(*this == r); }
518 const REAL_VALUE_TYPE &lower_bound () const;
519 const REAL_VALUE_TYPE &upper_bound () const;
520 virtual tree lbound () const override;
521 virtual tree ubound () const override;
522 nan_state get_nan_state () const;
523 void update_nan ();
524 void update_nan (bool sign);
525 void update_nan (tree) = delete; // Disallow silent conversion to bool.
526 void update_nan (const nan_state &);
527 void clear_nan ();
528 void flush_denormals_to_zero ();
530 // fpclassify like API
531 bool known_isfinite () const;
532 bool known_isnan () const;
533 bool known_isinf () const;
534 bool maybe_isnan () const;
535 bool maybe_isnan (bool sign) const;
536 bool maybe_isinf () const;
537 bool signbit_p (bool &signbit) const;
538 bool nan_signbit_p (bool &signbit) const;
540 protected:
541 virtual bool contains_p (tree cst) const override;
542 virtual void set (tree, tree, value_range_kind = VR_RANGE) override;
544 private:
545 bool internal_singleton_p (REAL_VALUE_TYPE * = NULL) const;
546 void verify_range ();
547 bool normalize_kind ();
548 bool union_nans (const frange &);
549 bool intersect_nans (const frange &);
550 bool combine_zeros (const frange &, bool union_p);
552 tree m_type;
553 REAL_VALUE_TYPE m_min;
554 REAL_VALUE_TYPE m_max;
555 bool m_pos_nan;
556 bool m_neg_nan;
559 inline const REAL_VALUE_TYPE &
560 frange::lower_bound () const
562 gcc_checking_assert (!undefined_p () && !known_isnan ());
563 return m_min;
566 inline const REAL_VALUE_TYPE &
567 frange::upper_bound () const
569 gcc_checking_assert (!undefined_p () && !known_isnan ());
570 return m_max;
573 // Return the NAN state.
575 inline nan_state
576 frange::get_nan_state () const
578 return nan_state (m_pos_nan, m_neg_nan);
581 // is_a<> and as_a<> implementation for vrange.
583 // Anything we haven't specialized is a hard fail.
584 template <typename T>
585 inline bool
586 is_a (vrange &)
588 gcc_unreachable ();
589 return false;
592 template <typename T>
593 inline bool
594 is_a (const vrange &v)
596 // Reuse is_a <vrange> to implement the const version.
597 const T &derived = static_cast<const T &> (v);
598 return is_a <T> (const_cast<T &> (derived));
601 template <typename T>
602 inline T &
603 as_a (vrange &v)
605 gcc_checking_assert (is_a <T> (v));
606 return static_cast <T &> (v);
609 template <typename T>
610 inline const T &
611 as_a (const vrange &v)
613 gcc_checking_assert (is_a <T> (v));
614 return static_cast <const T &> (v);
617 // Specializations for the different range types.
619 template <>
620 inline bool
621 is_a <irange> (vrange &v)
623 return v.m_discriminator == VR_IRANGE;
626 template <>
627 inline bool
628 is_a <frange> (vrange &v)
630 return v.m_discriminator == VR_FRANGE;
633 template <>
634 inline bool
635 is_a <unsupported_range> (vrange &v)
637 return v.m_discriminator == VR_UNKNOWN;
640 // For resizable ranges, resize the range up to HARD_MAX_RANGES if the
641 // NEEDED pairs is greater than the current capacity of the range.
643 inline void
644 irange::maybe_resize (int needed)
646 if (!m_resizable || m_max_ranges == HARD_MAX_RANGES)
647 return;
649 if (needed > m_max_ranges)
651 m_max_ranges = HARD_MAX_RANGES;
652 wide_int *newmem = new wide_int[m_max_ranges * 2];
653 unsigned n = num_pairs () * 2;
654 for (unsigned i = 0; i < n; ++i)
655 newmem[i] = m_base[i];
656 m_base = newmem;
660 template<unsigned N, bool RESIZABLE>
661 inline
662 int_range<N, RESIZABLE>::~int_range ()
664 if (RESIZABLE && m_base != m_ranges)
665 delete[] m_base;
668 // This is an "infinite" precision irange for use in temporary
669 // calculations. It starts with a sensible default covering 99% of
670 // uses, and goes up to HARD_MAX_RANGES when needed. Any allocated
671 // storage is freed upon destruction.
672 typedef int_range<3, /*RESIZABLE=*/true> int_range_max;
674 class vrange_visitor
676 public:
677 virtual void visit (const irange &) const { }
678 virtual void visit (const frange &) const { }
679 virtual void visit (const unsupported_range &) const { }
682 typedef int_range<2> value_range;
684 // This is an "infinite" precision range object for use in temporary
685 // calculations for any of the handled types. The object can be
686 // transparently used as a vrange.
688 // Using any of the various constructors initializes the object
689 // appropriately, but the default constructor is uninitialized and
690 // must be initialized either with set_type() or by assigning into it.
692 // Assigning between incompatible types is allowed. For example if a
693 // temporary holds an irange, you can assign an frange into it, and
694 // all the right things will happen. However, before passing this
695 // object to a function accepting a vrange, the correct type must be
696 // set. If it isn't, you can do so with set_type().
698 class Value_Range
700 public:
701 Value_Range ();
702 Value_Range (const vrange &r);
703 Value_Range (tree type);
704 Value_Range (tree, tree, value_range_kind kind = VR_RANGE);
705 Value_Range (const Value_Range &);
706 ~Value_Range ();
707 void set_type (tree type);
708 vrange& operator= (const vrange &);
709 Value_Range& operator= (const Value_Range &);
710 bool operator== (const Value_Range &r) const;
711 bool operator!= (const Value_Range &r) const;
712 operator vrange &();
713 operator const vrange &() const;
714 void dump (FILE *) const;
715 static bool supports_type_p (const_tree type);
717 tree type () { return m_vrange->type (); }
718 bool varying_p () const { return m_vrange->varying_p (); }
719 bool undefined_p () const { return m_vrange->undefined_p (); }
720 void set_varying (tree type) { init (type); m_vrange->set_varying (type); }
721 void set_undefined () { m_vrange->set_undefined (); }
722 bool union_ (const vrange &r) { return m_vrange->union_ (r); }
723 bool intersect (const vrange &r) { return m_vrange->intersect (r); }
724 bool contains_p (tree cst) const { return m_vrange->contains_p (cst); }
725 bool singleton_p (tree *result = NULL) const
726 { return m_vrange->singleton_p (result); }
727 void set_zero (tree type) { init (type); return m_vrange->set_zero (type); }
728 void set_nonzero (tree type)
729 { init (type); return m_vrange->set_nonzero (type); }
730 bool nonzero_p () const { return m_vrange->nonzero_p (); }
731 bool zero_p () const { return m_vrange->zero_p (); }
732 tree lbound () const { return m_vrange->lbound (); }
733 tree ubound () const { return m_vrange->ubound (); }
734 irange_bitmask get_bitmask () const { return m_vrange->get_bitmask (); }
735 void update_bitmask (const class irange_bitmask &bm)
736 { return m_vrange->update_bitmask (bm); }
737 void accept (const vrange_visitor &v) const { m_vrange->accept (v); }
738 private:
739 void init (tree type);
740 void init (const vrange &);
742 vrange *m_vrange;
743 // The buffer must be at least the size of the largest range.
744 static_assert (sizeof (int_range_max) > sizeof (frange));
745 char m_buffer[sizeof (int_range_max)];
748 // The default constructor is uninitialized and must be initialized
749 // with either set_type() or with an assignment into it.
751 inline
752 Value_Range::Value_Range ()
754 m_vrange = NULL;
757 // Copy constructor.
759 inline
760 Value_Range::Value_Range (const Value_Range &r)
762 init (*r.m_vrange);
765 // Copy constructor from a vrange.
767 inline
768 Value_Range::Value_Range (const vrange &r)
770 init (r);
773 // Construct an UNDEFINED range that can hold ranges of TYPE. If TYPE
774 // is not supported, default to unsupported_range.
776 inline
777 Value_Range::Value_Range (tree type)
779 init (type);
782 // Construct a range that can hold a range of [MIN, MAX], where MIN
783 // and MAX are trees.
785 inline
786 Value_Range::Value_Range (tree min, tree max, value_range_kind kind)
788 init (TREE_TYPE (min));
789 m_vrange->set (min, max, kind);
792 inline
793 Value_Range::~Value_Range ()
795 if (m_vrange)
796 m_vrange->~vrange ();
799 // Initialize object to an UNDEFINED range that can hold ranges of
800 // TYPE. Clean-up memory if there was a previous object.
802 inline void
803 Value_Range::set_type (tree type)
805 if (m_vrange)
806 m_vrange->~vrange ();
807 init (type);
810 // Initialize object to an UNDEFINED range that can hold ranges of
811 // TYPE.
813 inline void
814 Value_Range::init (tree type)
816 gcc_checking_assert (TYPE_P (type));
818 if (irange::supports_p (type))
819 m_vrange = new (&m_buffer) int_range_max ();
820 else if (frange::supports_p (type))
821 m_vrange = new (&m_buffer) frange ();
822 else
823 m_vrange = new (&m_buffer) unsupported_range ();
826 // Initialize object with a copy of R.
828 inline void
829 Value_Range::init (const vrange &r)
831 if (is_a <irange> (r))
832 m_vrange = new (&m_buffer) int_range_max (as_a <irange> (r));
833 else if (is_a <frange> (r))
834 m_vrange = new (&m_buffer) frange (as_a <frange> (r));
835 else
836 m_vrange = new (&m_buffer) unsupported_range (as_a <unsupported_range> (r));
839 // Assignment operator. Copying incompatible types is allowed. That
840 // is, assigning an frange to an object holding an irange does the
841 // right thing.
843 inline vrange &
844 Value_Range::operator= (const vrange &r)
846 if (m_vrange)
847 m_vrange->~vrange ();
848 init (r);
849 return *m_vrange;
852 inline Value_Range &
853 Value_Range::operator= (const Value_Range &r)
855 // No need to call the m_vrange destructor here, as we will do so in
856 // the assignment below.
857 *this = *r.m_vrange;
858 return *this;
861 inline bool
862 Value_Range::operator== (const Value_Range &r) const
864 return *m_vrange == *r.m_vrange;
867 inline bool
868 Value_Range::operator!= (const Value_Range &r) const
870 return *m_vrange != *r.m_vrange;
873 inline
874 Value_Range::operator vrange &()
876 return *m_vrange;
879 inline
880 Value_Range::operator const vrange &() const
882 return *m_vrange;
885 // Return TRUE if TYPE is supported by the vrange infrastructure.
887 inline bool
888 Value_Range::supports_type_p (const_tree type)
890 return irange::supports_p (type) || frange::supports_p (type);
893 extern value_range_kind get_legacy_range (const vrange &, tree &min, tree &max);
894 extern void dump_value_range (FILE *, const vrange *);
895 extern bool vrp_operand_equal_p (const_tree, const_tree);
896 inline REAL_VALUE_TYPE frange_val_min (const_tree type);
897 inline REAL_VALUE_TYPE frange_val_max (const_tree type);
899 // Number of sub-ranges in a range.
901 inline unsigned
902 irange::num_pairs () const
904 return m_num_ranges;
907 inline tree
908 irange::type () const
910 gcc_checking_assert (m_num_ranges > 0);
911 return m_type;
914 inline bool
915 irange::varying_compatible_p () const
917 if (m_num_ranges != 1)
918 return false;
920 const wide_int &l = m_base[0];
921 const wide_int &u = m_base[1];
922 tree t = m_type;
924 if (m_kind == VR_VARYING && t == error_mark_node)
925 return true;
927 unsigned prec = TYPE_PRECISION (t);
928 signop sign = TYPE_SIGN (t);
929 if (INTEGRAL_TYPE_P (t) || POINTER_TYPE_P (t))
930 return (l == wi::min_value (prec, sign)
931 && u == wi::max_value (prec, sign)
932 && m_bitmask.unknown_p ());
933 return true;
936 inline bool
937 vrange::varying_p () const
939 return m_kind == VR_VARYING;
942 inline bool
943 vrange::undefined_p () const
945 return m_kind == VR_UNDEFINED;
948 inline bool
949 irange::zero_p () const
951 return (m_kind == VR_RANGE && m_num_ranges == 1
952 && lower_bound (0) == 0
953 && upper_bound (0) == 0);
956 inline bool
957 irange::nonzero_p () const
959 if (undefined_p ())
960 return false;
962 wide_int zero = wi::zero (TYPE_PRECISION (type ()));
963 return *this == int_range<2> (type (), zero, zero, VR_ANTI_RANGE);
966 inline bool
967 irange::supports_p (const_tree type)
969 return INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type);
972 inline bool
973 irange::contains_p (tree cst) const
975 return contains_p (wi::to_wide (cst));
978 inline bool
979 range_includes_zero_p (const vrange &vr)
981 if (vr.undefined_p ())
982 return false;
984 if (vr.varying_p ())
985 return true;
987 return vr.contains_p (build_zero_cst (vr.type ()));
990 // Constructors for irange
992 inline
993 irange::irange (wide_int *base, unsigned nranges, bool resizable)
994 : vrange (VR_IRANGE),
995 m_resizable (resizable),
996 m_max_ranges (nranges)
998 m_base = base;
999 set_undefined ();
1002 // Constructors for int_range<>.
1004 template<unsigned N, bool RESIZABLE>
1005 inline
1006 int_range<N, RESIZABLE>::int_range ()
1007 : irange (m_ranges, N, RESIZABLE)
1011 template<unsigned N, bool RESIZABLE>
1012 int_range<N, RESIZABLE>::int_range (const int_range &other)
1013 : irange (m_ranges, N, RESIZABLE)
1015 irange::operator= (other);
1018 template<unsigned N, bool RESIZABLE>
1019 int_range<N, RESIZABLE>::int_range (tree min, tree max, value_range_kind kind)
1020 : irange (m_ranges, N, RESIZABLE)
1022 irange::set (min, max, kind);
1025 template<unsigned N, bool RESIZABLE>
1026 int_range<N, RESIZABLE>::int_range (tree type)
1027 : irange (m_ranges, N, RESIZABLE)
1029 set_varying (type);
1032 template<unsigned N, bool RESIZABLE>
1033 int_range<N, RESIZABLE>::int_range (tree type, const wide_int &wmin, const wide_int &wmax,
1034 value_range_kind kind)
1035 : irange (m_ranges, N, RESIZABLE)
1037 set (type, wmin, wmax, kind);
1040 template<unsigned N, bool RESIZABLE>
1041 int_range<N, RESIZABLE>::int_range (const irange &other)
1042 : irange (m_ranges, N, RESIZABLE)
1044 irange::operator= (other);
1047 template<unsigned N, bool RESIZABLE>
1048 int_range<N, RESIZABLE>&
1049 int_range<N, RESIZABLE>::operator= (const int_range &src)
1051 irange::operator= (src);
1052 return *this;
1055 inline void
1056 irange::set_undefined ()
1058 m_kind = VR_UNDEFINED;
1059 m_num_ranges = 0;
1062 inline void
1063 irange::set_varying (tree type)
1065 m_kind = VR_VARYING;
1066 m_num_ranges = 1;
1067 m_bitmask.set_unknown (TYPE_PRECISION (type));
1069 if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1071 m_type = type;
1072 // Strict enum's require varying to be not TYPE_MIN/MAX, but rather
1073 // min_value and max_value.
1074 m_base[0] = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1075 m_base[1] = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1077 else
1078 m_type = error_mark_node;
1081 // Return the lower bound of a sub-range. PAIR is the sub-range in
1082 // question.
1084 inline wide_int
1085 irange::lower_bound (unsigned pair) const
1087 gcc_checking_assert (m_num_ranges > 0);
1088 gcc_checking_assert (pair + 1 <= num_pairs ());
1089 return m_base[pair * 2];
1092 // Return the upper bound of a sub-range. PAIR is the sub-range in
1093 // question.
1095 inline wide_int
1096 irange::upper_bound (unsigned pair) const
1098 gcc_checking_assert (m_num_ranges > 0);
1099 gcc_checking_assert (pair + 1 <= num_pairs ());
1100 return m_base[pair * 2 + 1];
1103 // Return the highest bound of a range.
1105 inline wide_int
1106 irange::upper_bound () const
1108 unsigned pairs = num_pairs ();
1109 gcc_checking_assert (pairs > 0);
1110 return upper_bound (pairs - 1);
1113 // Set value range VR to a nonzero range of type TYPE.
1115 inline void
1116 irange::set_nonzero (tree type)
1118 unsigned prec = TYPE_PRECISION (type);
1120 if (TYPE_UNSIGNED (type))
1122 m_type = type;
1123 m_kind = VR_RANGE;
1124 m_base[0] = wi::one (prec);
1125 m_base[1] = wi::minus_one (prec);
1126 m_bitmask.set_unknown (prec);
1127 m_num_ranges = 1;
1129 if (flag_checking)
1130 verify_range ();
1132 else
1134 wide_int zero = wi::zero (prec);
1135 set (type, zero, zero, VR_ANTI_RANGE);
1139 // Set value range VR to a ZERO range of type TYPE.
1141 inline void
1142 irange::set_zero (tree type)
1144 wide_int zero = wi::zero (TYPE_PRECISION (type));
1145 set (type, zero, zero);
1148 // Normalize a range to VARYING or UNDEFINED if possible.
1150 inline void
1151 irange::normalize_kind ()
1153 if (m_num_ranges == 0)
1154 set_undefined ();
1155 else if (varying_compatible_p ())
1157 if (m_kind == VR_RANGE)
1158 m_kind = VR_VARYING;
1159 else if (m_kind == VR_ANTI_RANGE)
1160 set_undefined ();
1162 if (flag_checking)
1163 verify_range ();
1166 inline bool
1167 contains_zero_p (const irange &r)
1169 if (r.undefined_p ())
1170 return false;
1172 wide_int zero = wi::zero (TYPE_PRECISION (r.type ()));
1173 return r.contains_p (zero);
1176 inline wide_int
1177 irange_val_min (const_tree type)
1179 gcc_checking_assert (irange::supports_p (type));
1180 return wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1183 inline wide_int
1184 irange_val_max (const_tree type)
1186 gcc_checking_assert (irange::supports_p (type));
1187 return wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1190 inline
1191 frange::frange ()
1192 : vrange (VR_FRANGE)
1194 set_undefined ();
1197 inline
1198 frange::frange (const frange &src)
1199 : vrange (VR_FRANGE)
1201 *this = src;
1204 inline
1205 frange::frange (tree type)
1206 : vrange (VR_FRANGE)
1208 set_varying (type);
1211 // frange constructor from REAL_VALUE_TYPE endpoints.
1213 inline
1214 frange::frange (tree type,
1215 const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
1216 value_range_kind kind)
1217 : vrange (VR_FRANGE)
1219 set (type, min, max, kind);
1222 // frange constructor from trees.
1224 inline
1225 frange::frange (tree min, tree max, value_range_kind kind)
1226 : vrange (VR_FRANGE)
1228 set (min, max, kind);
1231 inline tree
1232 frange::type () const
1234 gcc_checking_assert (!undefined_p ());
1235 return m_type;
1238 inline void
1239 frange::set_varying (tree type)
1241 m_kind = VR_VARYING;
1242 m_type = type;
1243 m_min = frange_val_min (type);
1244 m_max = frange_val_max (type);
1245 if (HONOR_NANS (m_type))
1247 m_pos_nan = true;
1248 m_neg_nan = true;
1250 else
1252 m_pos_nan = false;
1253 m_neg_nan = false;
1257 inline void
1258 frange::set_undefined ()
1260 m_kind = VR_UNDEFINED;
1261 m_type = NULL;
1262 m_pos_nan = false;
1263 m_neg_nan = false;
1264 // m_min and m_min are uninitialized as they are REAL_VALUE_TYPE ??.
1265 if (flag_checking)
1266 verify_range ();
1269 // Set the NAN bits to NAN and adjust the range.
1271 inline void
1272 frange::update_nan (const nan_state &nan)
1274 gcc_checking_assert (!undefined_p ());
1275 if (HONOR_NANS (m_type))
1277 m_pos_nan = nan.pos_p ();
1278 m_neg_nan = nan.neg_p ();
1279 normalize_kind ();
1280 if (flag_checking)
1281 verify_range ();
1285 // Set the NAN bit to +-NAN.
1287 inline void
1288 frange::update_nan ()
1290 gcc_checking_assert (!undefined_p ());
1291 nan_state nan (true);
1292 update_nan (nan);
1295 // Like above, but set the sign of the NAN.
1297 inline void
1298 frange::update_nan (bool sign)
1300 gcc_checking_assert (!undefined_p ());
1301 nan_state nan (/*pos=*/!sign, /*neg=*/sign);
1302 update_nan (nan);
1305 inline bool
1306 frange::contains_p (tree cst) const
1308 return contains_p (*TREE_REAL_CST_PTR (cst));
1311 // Clear the NAN bit and adjust the range.
1313 inline void
1314 frange::clear_nan ()
1316 gcc_checking_assert (!undefined_p ());
1317 m_pos_nan = false;
1318 m_neg_nan = false;
1319 normalize_kind ();
1320 if (flag_checking)
1321 verify_range ();
1324 // Set R to maximum representable value for TYPE.
1326 inline REAL_VALUE_TYPE
1327 real_max_representable (const_tree type)
1329 REAL_VALUE_TYPE r;
1330 char buf[128];
1331 get_max_float (REAL_MODE_FORMAT (TYPE_MODE (type)),
1332 buf, sizeof (buf), false);
1333 int res = real_from_string (&r, buf);
1334 gcc_checking_assert (!res);
1335 return r;
1338 // Return the minimum representable value for TYPE.
1340 inline REAL_VALUE_TYPE
1341 real_min_representable (const_tree type)
1343 REAL_VALUE_TYPE r = real_max_representable (type);
1344 r = real_value_negate (&r);
1345 return r;
1348 // Return the minimum value for TYPE.
1350 inline REAL_VALUE_TYPE
1351 frange_val_min (const_tree type)
1353 if (HONOR_INFINITIES (type))
1354 return dconstninf;
1355 else
1356 return real_min_representable (type);
1359 // Return the maximum value for TYPE.
1361 inline REAL_VALUE_TYPE
1362 frange_val_max (const_tree type)
1364 if (HONOR_INFINITIES (type))
1365 return dconstinf;
1366 else
1367 return real_max_representable (type);
1370 // Return TRUE if R is the minimum value for TYPE.
1372 inline bool
1373 frange_val_is_min (const REAL_VALUE_TYPE &r, const_tree type)
1375 REAL_VALUE_TYPE min = frange_val_min (type);
1376 return real_identical (&min, &r);
1379 // Return TRUE if R is the max value for TYPE.
1381 inline bool
1382 frange_val_is_max (const REAL_VALUE_TYPE &r, const_tree type)
1384 REAL_VALUE_TYPE max = frange_val_max (type);
1385 return real_identical (&max, &r);
1388 // Build a NAN with a state of NAN.
1390 inline void
1391 frange::set_nan (tree type, const nan_state &nan)
1393 gcc_checking_assert (nan.pos_p () || nan.neg_p ());
1394 if (HONOR_NANS (type))
1396 m_kind = VR_NAN;
1397 m_type = type;
1398 m_neg_nan = nan.neg_p ();
1399 m_pos_nan = nan.pos_p ();
1400 if (flag_checking)
1401 verify_range ();
1403 else
1404 set_undefined ();
1407 // Build a signless NAN of type TYPE.
1409 inline void
1410 frange::set_nan (tree type)
1412 nan_state nan (true);
1413 set_nan (type, nan);
1416 // Build a NAN of type TYPE with SIGN.
1418 inline void
1419 frange::set_nan (tree type, bool sign)
1421 nan_state nan (/*pos=*/!sign, /*neg=*/sign);
1422 set_nan (type, nan);
1425 // Return TRUE if range is known to be finite.
1427 inline bool
1428 frange::known_isfinite () const
1430 if (undefined_p () || varying_p () || m_kind == VR_ANTI_RANGE)
1431 return false;
1432 return (!maybe_isnan () && !real_isinf (&m_min) && !real_isinf (&m_max));
1435 // Return TRUE if range may be infinite.
1437 inline bool
1438 frange::maybe_isinf () const
1440 if (undefined_p () || m_kind == VR_ANTI_RANGE || m_kind == VR_NAN)
1441 return false;
1442 if (varying_p ())
1443 return true;
1444 return real_isinf (&m_min) || real_isinf (&m_max);
1447 // Return TRUE if range is known to be the [-INF,-INF] or [+INF,+INF].
1449 inline bool
1450 frange::known_isinf () const
1452 return (m_kind == VR_RANGE
1453 && !maybe_isnan ()
1454 && real_identical (&m_min, &m_max)
1455 && real_isinf (&m_min));
1458 // Return TRUE if range is possibly a NAN.
1460 inline bool
1461 frange::maybe_isnan () const
1463 if (undefined_p ())
1464 return false;
1465 return m_pos_nan || m_neg_nan;
1468 // Return TRUE if range is possibly a NAN with SIGN.
1470 inline bool
1471 frange::maybe_isnan (bool sign) const
1473 if (undefined_p ())
1474 return false;
1475 if (sign)
1476 return m_neg_nan;
1477 return m_pos_nan;
1480 // Return TRUE if range is a +NAN or -NAN.
1482 inline bool
1483 frange::known_isnan () const
1485 return m_kind == VR_NAN;
1488 // If the signbit for the range is known, set it in SIGNBIT and return
1489 // TRUE.
1491 inline bool
1492 frange::signbit_p (bool &signbit) const
1494 if (undefined_p ())
1495 return false;
1497 // NAN with unknown sign.
1498 if (m_pos_nan && m_neg_nan)
1499 return false;
1500 // No NAN.
1501 if (!m_pos_nan && !m_neg_nan)
1503 if (m_min.sign == m_max.sign)
1505 signbit = m_min.sign;
1506 return true;
1508 return false;
1510 // NAN with known sign.
1511 bool nan_sign = m_neg_nan;
1512 if (known_isnan ()
1513 || (nan_sign == m_min.sign && nan_sign == m_max.sign))
1515 signbit = nan_sign;
1516 return true;
1518 return false;
1521 // If range has a NAN with a known sign, set it in SIGNBIT and return
1522 // TRUE.
1524 inline bool
1525 frange::nan_signbit_p (bool &signbit) const
1527 if (undefined_p ())
1528 return false;
1530 if (m_pos_nan == m_neg_nan)
1531 return false;
1533 signbit = m_neg_nan;
1534 return true;
1537 void frange_nextafter (enum machine_mode, REAL_VALUE_TYPE &,
1538 const REAL_VALUE_TYPE &);
1539 void frange_arithmetic (enum tree_code, tree, REAL_VALUE_TYPE &,
1540 const REAL_VALUE_TYPE &, const REAL_VALUE_TYPE &,
1541 const REAL_VALUE_TYPE &);
1543 // Return true if TYPE1 and TYPE2 are compatible range types.
1545 inline bool
1546 range_compatible_p (tree type1, tree type2)
1548 // types_compatible_p requires conversion in both directions to be useless.
1549 // GIMPLE only requires a cast one way in order to be compatible.
1550 // Ranges really only need the sign and precision to be the same.
1551 return TYPE_SIGN (type1) == TYPE_SIGN (type2)
1552 && (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1553 // FIXME: As PR112788 shows, for now on rs6000 _Float128 has
1554 // type precision 128 while long double has type precision 127
1555 // but both have the same mode so their precision is actually
1556 // the same, workaround it temporarily.
1557 || (SCALAR_FLOAT_TYPE_P (type1)
1558 && TYPE_MODE (type1) == TYPE_MODE (type2)));
1560 #endif // GCC_VALUE_RANGE_H