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)
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
27 // Types of value ranges.
32 /* Range spans the entire domain. */
34 /* Range is [MIN, MAX]. */
36 /* Range is ~[MIN, MAX]. */
40 /* Range is a nice guy. */
44 // Discriminator between different vrange types.
46 enum value_range_discriminator
48 // Range holds an integer or pointer.
50 // Floating point range.
52 // Range holds an unsupported type.
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 ()
67 // To query what a range object can support, use:
68 // void foo (vrange &v, irange &i, frange &f)
70 // if (v.supports_type_p (type)) ...
71 // if (i.supports_type_p (type)) ...
72 // if (f.supports_type_p (type)) ...
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
;
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;
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;
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
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
);
161 irange_bitmask::set_unknown (unsigned prec
)
163 m_value
= wi::zero (prec
);
164 m_mask
= wi::minus_one (prec
);
169 // Return TRUE if THIS does not have any meaningful information.
172 irange_bitmask::unknown_p () const
178 irange_bitmask::irange_bitmask (const wide_int
&value
, const wide_int
&mask
)
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.
198 irange_bitmask::get_nonzero_bits () const
200 return m_value
| m_mask
;
203 // Set the bitmask to the nonzero bits in BITS.
205 irange_bitmask::set_nonzero_bits (const wide_int
&bits
)
207 m_value
= wi::zero (bits
.get_precision ());
213 // Return TRUE if val could be a valid value with this bitmask.
216 irange_bitmask::member_p (const wide_int
&val
) const
220 wide_int res
= m_mask
& val
;
222 res
|= ~m_mask
& m_value
;
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
;
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
;
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
);
261 m_mask
= m_mask
& src
.m_mask
;
262 m_value
= m_value
| src
.m_value
;
268 // An integer range without any storage.
270 class irange
: public vrange
272 friend class irange_storage
;
273 friend class vrange_printer
;
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
;
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
;
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
;
311 // Operator overloads.
312 irange
& operator= (const irange
&);
313 bool operator== (const irange
&) const;
314 bool operator!= (const irange
&r
) const { return !(*this == r
); }
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
;
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;
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
;
349 unsigned char m_max_ranges
;
351 irange_bitmask m_bitmask
;
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
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
&);
376 int_range (tree
, tree
, value_range_kind
= VR_RANGE
);
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
388 : vrange (VR_UNKNOWN
)
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.
424 nan_state (bool pos_nan
, bool neg_nan
);
432 // Set NAN state to +-NAN if NAN_P is true. Otherwise set NAN state
436 nan_state::nan_state (bool 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.
447 nan_state::nan_state (bool pos_nan
, bool neg_nan
)
453 // Return if +NAN is possible.
456 nan_state::pos_p () const
461 // Return if -NAN is possible.
464 nan_state::neg_p () const
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
;
480 frange (const frange
&);
481 frange (tree
, tree
, value_range_kind
= VR_RANGE
);
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;
524 void update_nan (bool sign
);
525 void update_nan (tree
) = delete; // Disallow silent conversion to bool.
526 void update_nan (const nan_state
&);
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;
541 virtual bool contains_p (tree cst
) const override
;
542 virtual void set (tree
, tree
, value_range_kind
= VR_RANGE
) override
;
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
);
553 REAL_VALUE_TYPE m_min
;
554 REAL_VALUE_TYPE m_max
;
559 inline const REAL_VALUE_TYPE
&
560 frange::lower_bound () const
562 gcc_checking_assert (!undefined_p () && !known_isnan ());
566 inline const REAL_VALUE_TYPE
&
567 frange::upper_bound () const
569 gcc_checking_assert (!undefined_p () && !known_isnan ());
573 // Return the 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
>
592 template <typename T
>
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
>
605 gcc_checking_assert (is_a
<T
> (v
));
606 return static_cast <T
&> (v
);
609 template <typename 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.
621 is_a
<irange
> (vrange
&v
)
623 return v
.m_discriminator
== VR_IRANGE
;
628 is_a
<frange
> (vrange
&v
)
630 return v
.m_discriminator
== VR_FRANGE
;
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.
644 irange::maybe_resize (int needed
)
646 if (!m_resizable
|| m_max_ranges
== HARD_MAX_RANGES
)
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
];
660 template<unsigned N
, bool RESIZABLE
>
662 int_range
<N
, RESIZABLE
>::~int_range ()
664 if (RESIZABLE
&& m_base
!= m_ranges
)
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
;
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().
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
&);
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;
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
); }
739 void init (tree type
);
740 void init (const 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.
752 Value_Range::Value_Range ()
760 Value_Range::Value_Range (const Value_Range
&r
)
765 // Copy constructor from a vrange.
768 Value_Range::Value_Range (const vrange
&r
)
773 // Construct an UNDEFINED range that can hold ranges of TYPE. If TYPE
774 // is not supported, default to unsupported_range.
777 Value_Range::Value_Range (tree type
)
782 // Construct a range that can hold a range of [MIN, MAX], where MIN
783 // and MAX are trees.
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
);
793 Value_Range::~Value_Range ()
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.
803 Value_Range::set_type (tree type
)
806 m_vrange
->~vrange ();
810 // Initialize object to an UNDEFINED range that can hold ranges of
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 ();
823 m_vrange
= new (&m_buffer
) unsupported_range ();
826 // Initialize object with a copy of R.
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
));
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
844 Value_Range::operator= (const vrange
&r
)
847 m_vrange
->~vrange ();
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.
862 Value_Range::operator== (const Value_Range
&r
) const
864 return *m_vrange
== *r
.m_vrange
;
868 Value_Range::operator!= (const Value_Range
&r
) const
870 return *m_vrange
!= *r
.m_vrange
;
874 Value_Range::operator vrange
&()
880 Value_Range::operator const vrange
&() const
885 // Return TRUE if TYPE is supported by the vrange infrastructure.
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.
902 irange::num_pairs () const
908 irange::type () const
910 gcc_checking_assert (m_num_ranges
> 0);
915 irange::varying_compatible_p () const
917 if (m_num_ranges
!= 1)
920 const wide_int
&l
= m_base
[0];
921 const wide_int
&u
= m_base
[1];
924 if (m_kind
== VR_VARYING
&& t
== error_mark_node
)
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 ());
937 vrange::varying_p () const
939 return m_kind
== VR_VARYING
;
943 vrange::undefined_p () const
945 return m_kind
== VR_UNDEFINED
;
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);
957 irange::nonzero_p () const
962 wide_int zero
= wi::zero (TYPE_PRECISION (type ()));
963 return *this == int_range
<2> (type (), zero
, zero
, VR_ANTI_RANGE
);
967 irange::supports_p (const_tree type
)
969 return INTEGRAL_TYPE_P (type
) || POINTER_TYPE_P (type
);
973 irange::contains_p (tree cst
) const
975 return contains_p (wi::to_wide (cst
));
979 range_includes_zero_p (const vrange
&vr
)
981 if (vr
.undefined_p ())
987 return vr
.contains_p (build_zero_cst (vr
.type ()));
990 // Constructors for irange
993 irange::irange (wide_int
*base
, unsigned nranges
, bool resizable
)
994 : vrange (VR_IRANGE
),
995 m_resizable (resizable
),
996 m_max_ranges (nranges
)
1002 // Constructors for int_range<>.
1004 template<unsigned N
, bool RESIZABLE
>
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
)
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
);
1056 irange::set_undefined ()
1058 m_kind
= VR_UNDEFINED
;
1063 irange::set_varying (tree type
)
1065 m_kind
= VR_VARYING
;
1067 m_bitmask
.set_unknown (TYPE_PRECISION (type
));
1069 if (INTEGRAL_TYPE_P (type
) || POINTER_TYPE_P (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
));
1078 m_type
= error_mark_node
;
1081 // Return the lower bound of a sub-range. PAIR is the sub-range in
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
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.
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.
1116 irange::set_nonzero (tree type
)
1118 unsigned prec
= TYPE_PRECISION (type
);
1120 if (TYPE_UNSIGNED (type
))
1124 m_base
[0] = wi::one (prec
);
1125 m_base
[1] = wi::minus_one (prec
);
1126 m_bitmask
.set_unknown (prec
);
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.
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.
1151 irange::normalize_kind ()
1153 if (m_num_ranges
== 0)
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
)
1167 contains_zero_p (const irange
&r
)
1169 if (r
.undefined_p ())
1172 wide_int zero
= wi::zero (TYPE_PRECISION (r
.type ()));
1173 return r
.contains_p (zero
);
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
));
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
));
1192 : vrange (VR_FRANGE
)
1198 frange::frange (const frange
&src
)
1199 : vrange (VR_FRANGE
)
1205 frange::frange (tree type
)
1206 : vrange (VR_FRANGE
)
1211 // frange constructor from REAL_VALUE_TYPE endpoints.
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.
1225 frange::frange (tree min
, tree max
, value_range_kind kind
)
1226 : vrange (VR_FRANGE
)
1228 set (min
, max
, kind
);
1232 frange::type () const
1234 gcc_checking_assert (!undefined_p ());
1239 frange::set_varying (tree type
)
1241 m_kind
= VR_VARYING
;
1243 m_min
= frange_val_min (type
);
1244 m_max
= frange_val_max (type
);
1245 if (HONOR_NANS (m_type
))
1258 frange::set_undefined ()
1260 m_kind
= VR_UNDEFINED
;
1264 // m_min and m_min are uninitialized as they are REAL_VALUE_TYPE ??.
1269 // Set the NAN bits to NAN and adjust the range.
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 ();
1285 // Set the NAN bit to +-NAN.
1288 frange::update_nan ()
1290 gcc_checking_assert (!undefined_p ());
1291 nan_state
nan (true);
1295 // Like above, but set the sign of the NAN.
1298 frange::update_nan (bool sign
)
1300 gcc_checking_assert (!undefined_p ());
1301 nan_state
nan (/*pos=*/!sign
, /*neg=*/sign
);
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.
1314 frange::clear_nan ()
1316 gcc_checking_assert (!undefined_p ());
1324 // Set R to maximum representable value for TYPE.
1326 inline REAL_VALUE_TYPE
1327 real_max_representable (const_tree type
)
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
);
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
);
1348 // Return the minimum value for TYPE.
1350 inline REAL_VALUE_TYPE
1351 frange_val_min (const_tree type
)
1353 if (HONOR_INFINITIES (type
))
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
))
1367 return real_max_representable (type
);
1370 // Return TRUE if R is the minimum value for TYPE.
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.
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.
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
))
1398 m_neg_nan
= nan
.neg_p ();
1399 m_pos_nan
= nan
.pos_p ();
1407 // Build a signless NAN of type TYPE.
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.
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.
1428 frange::known_isfinite () const
1430 if (undefined_p () || varying_p () || m_kind
== VR_ANTI_RANGE
)
1432 return (!maybe_isnan () && !real_isinf (&m_min
) && !real_isinf (&m_max
));
1435 // Return TRUE if range may be infinite.
1438 frange::maybe_isinf () const
1440 if (undefined_p () || m_kind
== VR_ANTI_RANGE
|| m_kind
== VR_NAN
)
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].
1450 frange::known_isinf () const
1452 return (m_kind
== VR_RANGE
1454 && real_identical (&m_min
, &m_max
)
1455 && real_isinf (&m_min
));
1458 // Return TRUE if range is possibly a NAN.
1461 frange::maybe_isnan () const
1465 return m_pos_nan
|| m_neg_nan
;
1468 // Return TRUE if range is possibly a NAN with SIGN.
1471 frange::maybe_isnan (bool sign
) const
1480 // Return TRUE if range is a +NAN or -NAN.
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
1492 frange::signbit_p (bool &signbit
) const
1497 // NAN with unknown sign.
1498 if (m_pos_nan
&& m_neg_nan
)
1501 if (!m_pos_nan
&& !m_neg_nan
)
1503 if (m_min
.sign
== m_max
.sign
)
1505 signbit
= m_min
.sign
;
1510 // NAN with known sign.
1511 bool nan_sign
= m_neg_nan
;
1513 || (nan_sign
== m_min
.sign
&& nan_sign
== m_max
.sign
))
1521 // If range has a NAN with a known sign, set it in SIGNBIT and return
1525 frange::nan_signbit_p (bool &signbit
) const
1530 if (m_pos_nan
== m_neg_nan
)
1533 signbit
= m_neg_nan
;
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.
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