PR27116, Spelling errors found by Debian style checker
[official-gcc.git] / gcc / value-range.h
blob622b68863d26b00b000b47f74be1699b34f8fbed
1 /* Support routines for value ranges.
2 Copyright (C) 2019-2023 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 GTY((user)) 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);
84 virtual tree type () const;
85 virtual bool supports_type_p (const_tree type) const;
86 virtual void set_varying (tree type);
87 virtual void set_undefined ();
88 virtual bool union_ (const vrange &);
89 virtual bool intersect (const vrange &);
90 virtual bool singleton_p (tree *result = NULL) const;
91 virtual bool contains_p (tree cst) const;
92 virtual bool zero_p () const;
93 virtual bool nonzero_p () const;
94 virtual void set_nonzero (tree type);
95 virtual void set_zero (tree type);
96 virtual void set_nonnegative (tree type);
97 virtual bool fits_p (const vrange &r) const;
99 bool varying_p () const;
100 bool undefined_p () const;
101 vrange& operator= (const vrange &);
102 bool operator== (const vrange &) const;
103 bool operator!= (const vrange &r) const { return !(*this == r); }
104 void dump (FILE *) const;
105 protected:
106 vrange (enum value_range_discriminator d) : m_discriminator (d) { }
107 ENUM_BITFIELD(value_range_kind) m_kind : 8;
108 const ENUM_BITFIELD(value_range_discriminator) m_discriminator : 4;
111 namespace inchash
113 extern void add_vrange (const vrange &, hash &, unsigned flags = 0);
116 // A pair of values representing the known bits in a range. Zero bits
117 // in MASK cover constant values. Set bits in MASK cover unknown
118 // values. VALUE are the known bits.
120 // Set bits in MASK (no meaningful information) must have their
121 // corresponding bits in VALUE cleared, as this speeds up union and
122 // intersect.
124 class irange_bitmask
126 public:
127 irange_bitmask () { /* uninitialized */ }
128 irange_bitmask (unsigned prec) { set_unknown (prec); }
129 irange_bitmask (const wide_int &value, const wide_int &mask);
130 wide_int value () const { return m_value; }
131 wide_int mask () const { return m_mask; }
132 void set_unknown (unsigned prec);
133 bool unknown_p () const;
134 unsigned get_precision () const;
135 bool union_ (const irange_bitmask &src);
136 bool intersect (const irange_bitmask &src);
137 bool operator== (const irange_bitmask &src) const;
138 bool operator!= (const irange_bitmask &src) const { return !(*this == src); }
139 void verify_mask () const;
140 void dump (FILE *) const;
142 // Convenience functions for nonzero bitmask compatibility.
143 wide_int get_nonzero_bits () const;
144 void set_nonzero_bits (const wide_int &bits);
145 private:
146 wide_int m_value;
147 wide_int m_mask;
150 inline void
151 irange_bitmask::set_unknown (unsigned prec)
153 m_value = wi::zero (prec);
154 m_mask = wi::minus_one (prec);
155 if (flag_checking)
156 verify_mask ();
159 // Return TRUE if THIS does not have any meaningful information.
161 inline bool
162 irange_bitmask::unknown_p () const
164 return m_mask == -1;
167 inline
168 irange_bitmask::irange_bitmask (const wide_int &value, const wide_int &mask)
170 m_value = value;
171 m_mask = mask;
172 if (flag_checking)
173 verify_mask ();
176 inline unsigned
177 irange_bitmask::get_precision () const
179 return m_mask.get_precision ();
182 // The following two functions are meant for backwards compatability
183 // with the nonzero bitmask. A cleared bit means the value must be 0.
184 // A set bit means we have no information for the bit.
186 // Return the nonzero bits.
187 inline wide_int
188 irange_bitmask::get_nonzero_bits () const
190 return m_value | m_mask;
193 // Set the bitmask to the nonzero bits in BITS.
194 inline void
195 irange_bitmask::set_nonzero_bits (const wide_int &bits)
197 m_value = wi::zero (bits.get_precision ());
198 m_mask = bits;
199 if (flag_checking)
200 verify_mask ();
203 inline bool
204 irange_bitmask::operator== (const irange_bitmask &src) const
206 bool unknown1 = unknown_p ();
207 bool unknown2 = src.unknown_p ();
208 if (unknown1 || unknown2)
209 return unknown1 == unknown2;
210 return m_value == src.m_value && m_mask == src.m_mask;
213 inline bool
214 irange_bitmask::union_ (const irange_bitmask &orig_src)
216 // Normalize mask.
217 irange_bitmask src (orig_src.m_value & ~orig_src.m_mask, orig_src.m_mask);
218 m_value &= ~m_mask;
220 irange_bitmask save (*this);
221 m_mask = (m_mask | src.m_mask) | (m_value ^ src.m_value);
222 m_value = m_value & src.m_value;
223 if (flag_checking)
224 verify_mask ();
225 return *this != save;
228 inline bool
229 irange_bitmask::intersect (const irange_bitmask &orig_src)
231 // Normalize mask.
232 irange_bitmask src (orig_src.m_value & ~orig_src.m_mask, orig_src.m_mask);
233 m_value &= ~m_mask;
235 irange_bitmask save (*this);
236 // If we have two known bits that are incompatible, the resulting
237 // bit is undefined. It is unclear whether we should set the entire
238 // range to UNDEFINED, or just a subset of it. For now, set the
239 // entire bitmask to unknown (VARYING).
240 if (wi::bit_and (~(m_mask | src.m_mask),
241 m_value ^ src.m_value) != 0)
243 unsigned prec = m_mask.get_precision ();
244 m_mask = wi::minus_one (prec);
245 m_value = wi::zero (prec);
247 else
249 m_mask = m_mask & src.m_mask;
250 m_value = m_value | src.m_value;
252 if (flag_checking)
253 verify_mask ();
254 return *this != save;
257 // An integer range without any storage.
259 class GTY((user)) irange : public vrange
261 friend value_range_kind get_legacy_range (const irange &, tree &, tree &);
262 friend class irange_storage;
263 friend class vrange_printer;
264 public:
265 // In-place setters.
266 void set (tree type, const wide_int &, const wide_int &,
267 value_range_kind = VR_RANGE);
268 virtual void set_nonzero (tree type) override;
269 virtual void set_zero (tree type) override;
270 virtual void set_nonnegative (tree type) override;
271 virtual void set_varying (tree type) override;
272 virtual void set_undefined () override;
274 // Range types.
275 static bool supports_p (const_tree type);
276 virtual bool supports_type_p (const_tree type) const override;
277 virtual tree type () const override;
279 // Iteration over sub-ranges.
280 unsigned num_pairs () const;
281 wide_int lower_bound (unsigned = 0) const;
282 wide_int upper_bound (unsigned) const;
283 wide_int upper_bound () const;
285 // Predicates.
286 virtual bool zero_p () const override;
287 virtual bool nonzero_p () const override;
288 virtual bool singleton_p (tree *result = NULL) const override;
289 bool singleton_p (wide_int &) const;
290 bool contains_p (const wide_int &) const;
292 // In-place operators.
293 virtual bool union_ (const vrange &) override;
294 virtual bool intersect (const vrange &) override;
295 void invert ();
297 // Operator overloads.
298 irange& operator= (const irange &);
299 bool operator== (const irange &) const;
300 bool operator!= (const irange &r) const { return !(*this == r); }
302 // Misc methods.
303 virtual bool fits_p (const vrange &r) const override;
304 virtual void accept (const vrange_visitor &v) const override;
306 void update_bitmask (const irange_bitmask &);
307 irange_bitmask get_bitmask () const;
308 // Nonzero masks.
309 wide_int get_nonzero_bits () const;
310 void set_nonzero_bits (const wide_int &bits);
312 protected:
313 void maybe_resize (int needed);
314 virtual void set (tree, tree, value_range_kind = VR_RANGE) override;
315 virtual bool contains_p (tree cst) const override;
316 irange (wide_int *, unsigned nranges, bool resizable);
318 // In-place operators.
319 bool irange_contains_p (const irange &) const;
320 bool irange_single_pair_union (const irange &r);
322 void normalize_kind ();
324 void verify_range ();
326 // Hard limit on max ranges allowed.
327 static const int HARD_MAX_RANGES = 255;
328 private:
329 friend void gt_ggc_mx (irange *);
330 friend void gt_pch_nx (irange *);
331 friend void gt_pch_nx (irange *, gt_pointer_operator, void *);
333 bool varying_compatible_p () const;
334 bool intersect_bitmask (const irange &r);
335 bool union_bitmask (const irange &r);
336 irange_bitmask get_bitmask_from_range () const;
337 bool set_range_from_bitmask ();
339 bool intersect (const wide_int& lb, const wide_int& ub);
340 unsigned char m_num_ranges;
341 bool m_resizable;
342 unsigned char m_max_ranges;
343 tree m_type;
344 irange_bitmask m_bitmask;
345 protected:
346 wide_int *m_base;
349 // Here we describe an irange with N pairs of ranges. The storage for
350 // the pairs is embedded in the class as an array.
352 // If RESIZABLE is true, the storage will be resized on the heap when
353 // the number of ranges needed goes past N up to a max of
354 // HARD_MAX_RANGES. This new storage is freed upon destruction.
356 template<unsigned N, bool RESIZABLE = false>
357 class GTY((user)) int_range : public irange
359 public:
360 int_range ();
361 int_range (tree type, const wide_int &, const wide_int &,
362 value_range_kind = VR_RANGE);
363 int_range (tree type);
364 int_range (const int_range &);
365 int_range (const irange &);
366 virtual ~int_range ();
367 int_range& operator= (const int_range &);
368 protected:
369 int_range (tree, tree, value_range_kind = VR_RANGE);
370 private:
371 wide_int m_ranges[N*2];
374 // Unsupported temporaries may be created by ranger before it's known
375 // they're unsupported, or by vr_values::get_value_range.
377 class unsupported_range : public vrange
379 public:
380 unsupported_range ()
381 : vrange (VR_UNKNOWN)
383 set_undefined ();
385 virtual void set_undefined () final override
387 m_kind = VR_UNDEFINED;
389 virtual void accept (const vrange_visitor &v) const override;
392 // The NAN state as an opaque object.
394 class nan_state
396 public:
397 nan_state (bool);
398 nan_state (bool pos_nan, bool neg_nan);
399 bool neg_p () const;
400 bool pos_p () const;
401 private:
402 bool m_pos_nan;
403 bool m_neg_nan;
406 // Set NAN state to +-NAN if NAN_P is true. Otherwise set NAN state
407 // to false.
409 inline
410 nan_state::nan_state (bool nan_p)
412 m_pos_nan = nan_p;
413 m_neg_nan = nan_p;
416 // Constructor initializing the object to +NAN if POS_NAN is set, -NAN
417 // if NEG_NAN is set, or +-NAN if both are set. Otherwise POS_NAN and
418 // NEG_NAN are clear, and the object cannot be a NAN.
420 inline
421 nan_state::nan_state (bool pos_nan, bool neg_nan)
423 m_pos_nan = pos_nan;
424 m_neg_nan = neg_nan;
427 // Return if +NAN is possible.
429 inline bool
430 nan_state::pos_p () const
432 return m_pos_nan;
435 // Return if -NAN is possible.
437 inline bool
438 nan_state::neg_p () const
440 return m_neg_nan;
443 // A floating point range.
445 // The representation is a type with a couple of endpoints, unioned
446 // with the set of { -NAN, +Nan }.
448 class GTY((user)) frange : public vrange
450 friend class frange_storage;
451 friend class vrange_printer;
452 friend void gt_ggc_mx (frange *);
453 friend void gt_pch_nx (frange *);
454 friend void gt_pch_nx (frange *, gt_pointer_operator, void *);
455 public:
456 frange ();
457 frange (const frange &);
458 frange (tree, tree, value_range_kind = VR_RANGE);
459 frange (tree type);
460 frange (tree type, const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
461 value_range_kind = VR_RANGE);
462 static bool supports_p (const_tree type)
464 // ?? Decimal floats can have multiple representations for the
465 // same number. Supporting them may be as simple as just
466 // disabling them in singleton_p. No clue.
467 return SCALAR_FLOAT_TYPE_P (type) && !DECIMAL_FLOAT_TYPE_P (type);
469 virtual tree type () const override;
470 void set (tree type, const REAL_VALUE_TYPE &, const REAL_VALUE_TYPE &,
471 value_range_kind = VR_RANGE);
472 void set (tree type, const REAL_VALUE_TYPE &, const REAL_VALUE_TYPE &,
473 const nan_state &, value_range_kind = VR_RANGE);
474 void set_nan (tree type);
475 void set_nan (tree type, bool sign);
476 void set_nan (tree type, const nan_state &);
477 virtual void set_varying (tree type) override;
478 virtual void set_undefined () override;
479 virtual bool union_ (const vrange &) override;
480 virtual bool intersect (const vrange &) override;
481 bool contains_p (const REAL_VALUE_TYPE &) const;
482 virtual bool singleton_p (tree *result = NULL) const override;
483 bool singleton_p (REAL_VALUE_TYPE &r) const;
484 virtual bool supports_type_p (const_tree type) const override;
485 virtual void accept (const vrange_visitor &v) const override;
486 virtual bool zero_p () const override;
487 virtual bool nonzero_p () const override;
488 virtual void set_nonzero (tree type) override;
489 virtual void set_zero (tree type) override;
490 virtual void set_nonnegative (tree type) override;
491 frange& operator= (const frange &);
492 bool operator== (const frange &) const;
493 bool operator!= (const frange &r) const { return !(*this == r); }
494 const REAL_VALUE_TYPE &lower_bound () const;
495 const REAL_VALUE_TYPE &upper_bound () const;
496 nan_state get_nan_state () const;
497 void update_nan ();
498 void update_nan (bool sign);
499 void update_nan (tree) = delete; // Disallow silent conversion to bool.
500 void update_nan (const nan_state &);
501 void clear_nan ();
502 void flush_denormals_to_zero ();
504 // fpclassify like API
505 bool known_isfinite () const;
506 bool known_isnan () const;
507 bool known_isinf () const;
508 bool maybe_isnan () const;
509 bool maybe_isnan (bool sign) const;
510 bool maybe_isinf () const;
511 bool signbit_p (bool &signbit) const;
512 bool nan_signbit_p (bool &signbit) const;
514 protected:
515 virtual bool contains_p (tree cst) const override;
516 virtual void set (tree, tree, value_range_kind = VR_RANGE) override;
518 private:
519 bool internal_singleton_p (REAL_VALUE_TYPE * = NULL) const;
520 void verify_range ();
521 bool normalize_kind ();
522 bool union_nans (const frange &);
523 bool intersect_nans (const frange &);
524 bool combine_zeros (const frange &, bool union_p);
526 tree m_type;
527 REAL_VALUE_TYPE m_min;
528 REAL_VALUE_TYPE m_max;
529 bool m_pos_nan;
530 bool m_neg_nan;
533 inline const REAL_VALUE_TYPE &
534 frange::lower_bound () const
536 gcc_checking_assert (!undefined_p () && !known_isnan ());
537 return m_min;
540 inline const REAL_VALUE_TYPE &
541 frange::upper_bound () const
543 gcc_checking_assert (!undefined_p () && !known_isnan ());
544 return m_max;
547 // Return the NAN state.
549 inline nan_state
550 frange::get_nan_state () const
552 return nan_state (m_pos_nan, m_neg_nan);
555 // is_a<> and as_a<> implementation for vrange.
557 // Anything we haven't specialized is a hard fail.
558 template <typename T>
559 inline bool
560 is_a (vrange &)
562 gcc_unreachable ();
563 return false;
566 template <typename T>
567 inline bool
568 is_a (const vrange &v)
570 // Reuse is_a <vrange> to implement the const version.
571 const T &derived = static_cast<const T &> (v);
572 return is_a <T> (const_cast<T &> (derived));
575 template <typename T>
576 inline T &
577 as_a (vrange &v)
579 gcc_checking_assert (is_a <T> (v));
580 return static_cast <T &> (v);
583 template <typename T>
584 inline const T &
585 as_a (const vrange &v)
587 gcc_checking_assert (is_a <T> (v));
588 return static_cast <const T &> (v);
591 // Specializations for the different range types.
593 template <>
594 inline bool
595 is_a <irange> (vrange &v)
597 return v.m_discriminator == VR_IRANGE;
600 template <>
601 inline bool
602 is_a <frange> (vrange &v)
604 return v.m_discriminator == VR_FRANGE;
607 template <>
608 inline bool
609 is_a <unsupported_range> (vrange &v)
611 return v.m_discriminator == VR_UNKNOWN;
614 // For resizable ranges, resize the range up to HARD_MAX_RANGES if the
615 // NEEDED pairs is greater than the current capacity of the range.
617 inline void
618 irange::maybe_resize (int needed)
620 if (!m_resizable || m_max_ranges == HARD_MAX_RANGES)
621 return;
623 if (needed > m_max_ranges)
625 m_max_ranges = HARD_MAX_RANGES;
626 wide_int *newmem = new wide_int[m_max_ranges * 2];
627 memcpy (newmem, m_base, sizeof (wide_int) * num_pairs () * 2);
628 m_base = newmem;
632 template<unsigned N, bool RESIZABLE>
633 inline
634 int_range<N, RESIZABLE>::~int_range ()
636 if (RESIZABLE && m_base != m_ranges)
637 delete[] m_base;
640 // This is an "infinite" precision irange for use in temporary
641 // calculations. It starts with a sensible default covering 99% of
642 // uses, and goes up to HARD_MAX_RANGES when needed. Any allocated
643 // storage is freed upon destruction.
644 typedef int_range<3, /*RESIZABLE=*/true> int_range_max;
646 class vrange_visitor
648 public:
649 virtual void visit (const irange &) const { }
650 virtual void visit (const frange &) const { }
651 virtual void visit (const unsupported_range &) const { }
654 typedef int_range<2> value_range;
656 // This is an "infinite" precision range object for use in temporary
657 // calculations for any of the handled types. The object can be
658 // transparently used as a vrange.
660 class Value_Range
662 public:
663 Value_Range ();
664 Value_Range (const vrange &r);
665 Value_Range (tree type);
666 Value_Range (tree, tree, value_range_kind kind = VR_RANGE);
667 Value_Range (const Value_Range &);
668 void set_type (tree type);
669 vrange& operator= (const vrange &);
670 Value_Range& operator= (const Value_Range &);
671 bool operator== (const Value_Range &r) const;
672 bool operator!= (const Value_Range &r) const;
673 operator vrange &();
674 operator const vrange &() const;
675 void dump (FILE *) const;
676 static bool supports_type_p (const_tree type);
678 // Convenience methods for vrange compatibility.
679 tree type () { return m_vrange->type (); }
680 bool varying_p () const { return m_vrange->varying_p (); }
681 bool undefined_p () const { return m_vrange->undefined_p (); }
682 void set_varying (tree type) { init (type); m_vrange->set_varying (type); }
683 void set_undefined () { m_vrange->set_undefined (); }
684 bool union_ (const vrange &r) { return m_vrange->union_ (r); }
685 bool intersect (const vrange &r) { return m_vrange->intersect (r); }
686 bool contains_p (tree cst) const { return m_vrange->contains_p (cst); }
687 bool singleton_p (tree *result = NULL) const
688 { return m_vrange->singleton_p (result); }
689 void set_zero (tree type) { init (type); return m_vrange->set_zero (type); }
690 void set_nonzero (tree type)
691 { init (type); return m_vrange->set_nonzero (type); }
692 bool nonzero_p () const { return m_vrange->nonzero_p (); }
693 bool zero_p () const { return m_vrange->zero_p (); }
694 wide_int lower_bound () const; // For irange/prange comparability.
695 wide_int upper_bound () const; // For irange/prange comparability.
696 void accept (const vrange_visitor &v) const { m_vrange->accept (v); }
697 private:
698 void init (tree type);
699 unsupported_range m_unsupported;
700 vrange *m_vrange;
701 int_range_max m_irange;
702 frange m_frange;
705 inline
706 Value_Range::Value_Range ()
708 m_vrange = &m_unsupported;
711 // Copy constructor from a vrange.
713 inline
714 Value_Range::Value_Range (const vrange &r)
716 *this = r;
719 // Copy constructor from a TYPE. The range of the temporary is set to
720 // UNDEFINED.
722 inline
723 Value_Range::Value_Range (tree type)
725 init (type);
728 inline
729 Value_Range::Value_Range (tree min, tree max, value_range_kind kind)
731 init (TREE_TYPE (min));
732 m_vrange->set (min, max, kind);
735 inline
736 Value_Range::Value_Range (const Value_Range &r)
738 *this = *r.m_vrange;
741 // Initialize object so it is possible to store temporaries of TYPE
742 // into it.
744 inline void
745 Value_Range::init (tree type)
747 gcc_checking_assert (TYPE_P (type));
749 if (irange::supports_p (type))
750 m_vrange = &m_irange;
751 else if (frange::supports_p (type))
752 m_vrange = &m_frange;
753 else
754 m_vrange = &m_unsupported;
757 // Set the temporary to allow storing temporaries of TYPE. The range
758 // of the temporary is set to UNDEFINED.
760 inline void
761 Value_Range::set_type (tree type)
763 init (type);
764 m_vrange->set_undefined ();
767 // Assignment operator for temporaries. Copying incompatible types is
768 // allowed.
770 inline vrange &
771 Value_Range::operator= (const vrange &r)
773 if (is_a <irange> (r))
775 m_irange = as_a <irange> (r);
776 m_vrange = &m_irange;
778 else if (is_a <frange> (r))
780 m_frange = as_a <frange> (r);
781 m_vrange = &m_frange;
783 else if (is_a <unsupported_range> (r))
785 m_unsupported = as_a <unsupported_range> (r);
786 m_vrange = &m_unsupported;
788 else
789 gcc_unreachable ();
791 return *m_vrange;
794 inline Value_Range &
795 Value_Range::operator= (const Value_Range &r)
797 if (r.m_vrange == &r.m_irange)
799 m_irange = r.m_irange;
800 m_vrange = &m_irange;
802 else if (r.m_vrange == &r.m_frange)
804 m_frange = r.m_frange;
805 m_vrange = &m_frange;
807 else if (r.m_vrange == &r.m_unsupported)
809 m_unsupported = r.m_unsupported;
810 m_vrange = &m_unsupported;
812 else
813 gcc_unreachable ();
815 return *this;
818 inline bool
819 Value_Range::operator== (const Value_Range &r) const
821 return *m_vrange == *r.m_vrange;
824 inline bool
825 Value_Range::operator!= (const Value_Range &r) const
827 return *m_vrange != *r.m_vrange;
830 inline
831 Value_Range::operator vrange &()
833 return *m_vrange;
836 inline
837 Value_Range::operator const vrange &() const
839 return *m_vrange;
842 // Return TRUE if TYPE is supported by the vrange infrastructure.
844 inline bool
845 Value_Range::supports_type_p (const_tree type)
847 return irange::supports_p (type) || frange::supports_p (type);
850 extern value_range_kind get_legacy_range (const irange &, tree &min, tree &max);
851 extern void dump_value_range (FILE *, const vrange *);
852 extern bool vrp_operand_equal_p (const_tree, const_tree);
853 inline REAL_VALUE_TYPE frange_val_min (const_tree type);
854 inline REAL_VALUE_TYPE frange_val_max (const_tree type);
856 // Number of sub-ranges in a range.
858 inline unsigned
859 irange::num_pairs () const
861 return m_num_ranges;
864 inline tree
865 irange::type () const
867 gcc_checking_assert (m_num_ranges > 0);
868 return m_type;
871 inline bool
872 irange::varying_compatible_p () const
874 if (m_num_ranges != 1)
875 return false;
877 const wide_int &l = m_base[0];
878 const wide_int &u = m_base[1];
879 tree t = m_type;
881 if (m_kind == VR_VARYING && t == error_mark_node)
882 return true;
884 unsigned prec = TYPE_PRECISION (t);
885 signop sign = TYPE_SIGN (t);
886 if (INTEGRAL_TYPE_P (t) || POINTER_TYPE_P (t))
887 return (l == wi::min_value (prec, sign)
888 && u == wi::max_value (prec, sign)
889 && m_bitmask.unknown_p ());
890 return true;
893 inline bool
894 vrange::varying_p () const
896 return m_kind == VR_VARYING;
899 inline bool
900 vrange::undefined_p () const
902 return m_kind == VR_UNDEFINED;
905 inline bool
906 irange::zero_p () const
908 return (m_kind == VR_RANGE && m_num_ranges == 1
909 && lower_bound (0) == 0
910 && upper_bound (0) == 0);
913 inline bool
914 irange::nonzero_p () const
916 if (undefined_p ())
917 return false;
919 wide_int zero = wi::zero (TYPE_PRECISION (type ()));
920 return *this == int_range<2> (type (), zero, zero, VR_ANTI_RANGE);
923 inline bool
924 irange::supports_p (const_tree type)
926 return INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type);
929 inline bool
930 irange::contains_p (tree cst) const
932 return contains_p (wi::to_wide (cst));
935 inline bool
936 range_includes_zero_p (const irange *vr)
938 if (vr->undefined_p ())
939 return false;
941 if (vr->varying_p ())
942 return true;
944 wide_int zero = wi::zero (TYPE_PRECISION (vr->type ()));
945 return vr->contains_p (zero);
948 extern void gt_ggc_mx (vrange *);
949 extern void gt_pch_nx (vrange *);
950 extern void gt_pch_nx (vrange *, gt_pointer_operator, void *);
951 extern void gt_ggc_mx (irange *);
952 extern void gt_pch_nx (irange *);
953 extern void gt_pch_nx (irange *, gt_pointer_operator, void *);
954 extern void gt_ggc_mx (frange *);
955 extern void gt_pch_nx (frange *);
956 extern void gt_pch_nx (frange *, gt_pointer_operator, void *);
958 template<unsigned N>
959 inline void
960 gt_ggc_mx (int_range<N> *x)
962 gt_ggc_mx ((irange *) x);
965 template<unsigned N>
966 inline void
967 gt_pch_nx (int_range<N> *x)
969 gt_pch_nx ((irange *) x);
972 template<unsigned N>
973 inline void
974 gt_pch_nx (int_range<N> *x, gt_pointer_operator op, void *cookie)
976 gt_pch_nx ((irange *) x, op, cookie);
979 // Constructors for irange
981 inline
982 irange::irange (wide_int *base, unsigned nranges, bool resizable)
983 : vrange (VR_IRANGE),
984 m_resizable (resizable),
985 m_max_ranges (nranges)
987 m_base = base;
988 set_undefined ();
991 // Constructors for int_range<>.
993 template<unsigned N, bool RESIZABLE>
994 inline
995 int_range<N, RESIZABLE>::int_range ()
996 : irange (m_ranges, N, RESIZABLE)
1000 template<unsigned N, bool RESIZABLE>
1001 int_range<N, RESIZABLE>::int_range (const int_range &other)
1002 : irange (m_ranges, N, RESIZABLE)
1004 irange::operator= (other);
1007 template<unsigned N, bool RESIZABLE>
1008 int_range<N, RESIZABLE>::int_range (tree min, tree max, value_range_kind kind)
1009 : irange (m_ranges, N, RESIZABLE)
1011 irange::set (min, max, kind);
1014 template<unsigned N, bool RESIZABLE>
1015 int_range<N, RESIZABLE>::int_range (tree type)
1016 : irange (m_ranges, N, RESIZABLE)
1018 set_varying (type);
1021 template<unsigned N, bool RESIZABLE>
1022 int_range<N, RESIZABLE>::int_range (tree type, const wide_int &wmin, const wide_int &wmax,
1023 value_range_kind kind)
1024 : irange (m_ranges, N, RESIZABLE)
1026 set (type, wmin, wmax, kind);
1029 template<unsigned N, bool RESIZABLE>
1030 int_range<N, RESIZABLE>::int_range (const irange &other)
1031 : irange (m_ranges, N, RESIZABLE)
1033 irange::operator= (other);
1036 template<unsigned N, bool RESIZABLE>
1037 int_range<N, RESIZABLE>&
1038 int_range<N, RESIZABLE>::operator= (const int_range &src)
1040 irange::operator= (src);
1041 return *this;
1044 inline void
1045 irange::set_undefined ()
1047 m_kind = VR_UNDEFINED;
1048 m_num_ranges = 0;
1051 inline void
1052 irange::set_varying (tree type)
1054 m_kind = VR_VARYING;
1055 m_num_ranges = 1;
1056 m_bitmask.set_unknown (TYPE_PRECISION (type));
1058 if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1060 m_type = type;
1061 // Strict enum's require varying to be not TYPE_MIN/MAX, but rather
1062 // min_value and max_value.
1063 m_base[0] = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1064 m_base[1] = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1066 else
1067 m_type = error_mark_node;
1070 // Return the lower bound of a sub-range. PAIR is the sub-range in
1071 // question.
1073 inline wide_int
1074 irange::lower_bound (unsigned pair) const
1076 gcc_checking_assert (m_num_ranges > 0);
1077 gcc_checking_assert (pair + 1 <= num_pairs ());
1078 return m_base[pair * 2];
1081 // Return the upper bound of a sub-range. PAIR is the sub-range in
1082 // question.
1084 inline wide_int
1085 irange::upper_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 + 1];
1092 // Return the highest bound of a range.
1094 inline wide_int
1095 irange::upper_bound () const
1097 unsigned pairs = num_pairs ();
1098 gcc_checking_assert (pairs > 0);
1099 return upper_bound (pairs - 1);
1102 // Set value range VR to a nonzero range of type TYPE.
1104 inline void
1105 irange::set_nonzero (tree type)
1107 unsigned prec = TYPE_PRECISION (type);
1109 if (TYPE_UNSIGNED (type))
1111 m_type = type;
1112 m_kind = VR_RANGE;
1113 m_base[0] = wi::one (prec);
1114 m_base[1] = wi::minus_one (prec);
1115 m_bitmask.set_unknown (prec);
1116 m_num_ranges = 1;
1118 if (flag_checking)
1119 verify_range ();
1121 else
1123 wide_int zero = wi::zero (prec);
1124 set (type, zero, zero, VR_ANTI_RANGE);
1128 // Set value range VR to a ZERO range of type TYPE.
1130 inline void
1131 irange::set_zero (tree type)
1133 wide_int zero = wi::zero (TYPE_PRECISION (type));
1134 set (type, zero, zero);
1137 // Normalize a range to VARYING or UNDEFINED if possible.
1139 inline void
1140 irange::normalize_kind ()
1142 if (m_num_ranges == 0)
1143 set_undefined ();
1144 else if (varying_compatible_p ())
1146 if (m_kind == VR_RANGE)
1147 m_kind = VR_VARYING;
1148 else if (m_kind == VR_ANTI_RANGE)
1149 set_undefined ();
1151 if (flag_checking)
1152 verify_range ();
1155 inline bool
1156 contains_zero_p (const irange &r)
1158 if (r.undefined_p ())
1159 return true;
1161 wide_int zero = wi::zero (TYPE_PRECISION (r.type ()));
1162 return r.contains_p (zero);
1165 inline wide_int
1166 irange_val_min (const_tree type)
1168 gcc_checking_assert (irange::supports_p (type));
1169 return wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1172 inline wide_int
1173 irange_val_max (const_tree type)
1175 gcc_checking_assert (irange::supports_p (type));
1176 return wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1179 inline
1180 frange::frange ()
1181 : vrange (VR_FRANGE)
1183 set_undefined ();
1186 inline
1187 frange::frange (const frange &src)
1188 : vrange (VR_FRANGE)
1190 *this = src;
1193 inline
1194 frange::frange (tree type)
1195 : vrange (VR_FRANGE)
1197 set_varying (type);
1200 // frange constructor from REAL_VALUE_TYPE endpoints.
1202 inline
1203 frange::frange (tree type,
1204 const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
1205 value_range_kind kind)
1206 : vrange (VR_FRANGE)
1208 set (type, min, max, kind);
1211 // frange constructor from trees.
1213 inline
1214 frange::frange (tree min, tree max, value_range_kind kind)
1215 : vrange (VR_FRANGE)
1217 set (min, max, kind);
1220 inline tree
1221 frange::type () const
1223 gcc_checking_assert (!undefined_p ());
1224 return m_type;
1227 inline void
1228 frange::set_varying (tree type)
1230 m_kind = VR_VARYING;
1231 m_type = type;
1232 m_min = frange_val_min (type);
1233 m_max = frange_val_max (type);
1234 if (HONOR_NANS (m_type))
1236 m_pos_nan = true;
1237 m_neg_nan = true;
1239 else
1241 m_pos_nan = false;
1242 m_neg_nan = false;
1246 inline void
1247 frange::set_undefined ()
1249 m_kind = VR_UNDEFINED;
1250 m_type = NULL;
1251 m_pos_nan = false;
1252 m_neg_nan = false;
1253 // m_min and m_min are uninitialized as they are REAL_VALUE_TYPE ??.
1254 if (flag_checking)
1255 verify_range ();
1258 // Set the NAN bit and adjust the range.
1260 inline void
1261 frange::update_nan ()
1263 gcc_checking_assert (!undefined_p ());
1264 if (HONOR_NANS (m_type))
1266 m_pos_nan = true;
1267 m_neg_nan = true;
1268 normalize_kind ();
1269 if (flag_checking)
1270 verify_range ();
1274 // Like above, but set the sign of the NAN.
1276 inline void
1277 frange::update_nan (bool sign)
1279 gcc_checking_assert (!undefined_p ());
1280 if (HONOR_NANS (m_type))
1282 m_pos_nan = !sign;
1283 m_neg_nan = sign;
1284 normalize_kind ();
1285 if (flag_checking)
1286 verify_range ();
1290 inline bool
1291 frange::contains_p (tree cst) const
1293 return contains_p (*TREE_REAL_CST_PTR (cst));
1296 // Clear the NAN bit and adjust the range.
1298 inline void
1299 frange::clear_nan ()
1301 gcc_checking_assert (!undefined_p ());
1302 m_pos_nan = false;
1303 m_neg_nan = false;
1304 normalize_kind ();
1305 if (flag_checking)
1306 verify_range ();
1309 // Set R to maximum representable value for TYPE.
1311 inline REAL_VALUE_TYPE
1312 real_max_representable (const_tree type)
1314 REAL_VALUE_TYPE r;
1315 char buf[128];
1316 get_max_float (REAL_MODE_FORMAT (TYPE_MODE (type)),
1317 buf, sizeof (buf), false);
1318 int res = real_from_string (&r, buf);
1319 gcc_checking_assert (!res);
1320 return r;
1323 // Return the minimum representable value for TYPE.
1325 inline REAL_VALUE_TYPE
1326 real_min_representable (const_tree type)
1328 REAL_VALUE_TYPE r = real_max_representable (type);
1329 r = real_value_negate (&r);
1330 return r;
1333 // Return the minimum value for TYPE.
1335 inline REAL_VALUE_TYPE
1336 frange_val_min (const_tree type)
1338 if (HONOR_INFINITIES (type))
1339 return dconstninf;
1340 else
1341 return real_min_representable (type);
1344 // Return the maximum value for TYPE.
1346 inline REAL_VALUE_TYPE
1347 frange_val_max (const_tree type)
1349 if (HONOR_INFINITIES (type))
1350 return dconstinf;
1351 else
1352 return real_max_representable (type);
1355 // Return TRUE if R is the minimum value for TYPE.
1357 inline bool
1358 frange_val_is_min (const REAL_VALUE_TYPE &r, const_tree type)
1360 REAL_VALUE_TYPE min = frange_val_min (type);
1361 return real_identical (&min, &r);
1364 // Return TRUE if R is the max value for TYPE.
1366 inline bool
1367 frange_val_is_max (const REAL_VALUE_TYPE &r, const_tree type)
1369 REAL_VALUE_TYPE max = frange_val_max (type);
1370 return real_identical (&max, &r);
1373 // Build a NAN with a state of NAN.
1375 inline void
1376 frange::set_nan (tree type, const nan_state &nan)
1378 gcc_checking_assert (nan.pos_p () || nan.neg_p ());
1379 if (HONOR_NANS (type))
1381 m_kind = VR_NAN;
1382 m_type = type;
1383 m_neg_nan = nan.neg_p ();
1384 m_pos_nan = nan.pos_p ();
1385 if (flag_checking)
1386 verify_range ();
1388 else
1389 set_undefined ();
1392 // Build a signless NAN of type TYPE.
1394 inline void
1395 frange::set_nan (tree type)
1397 nan_state nan (true);
1398 set_nan (type, nan);
1401 // Build a NAN of type TYPE with SIGN.
1403 inline void
1404 frange::set_nan (tree type, bool sign)
1406 nan_state nan (/*pos=*/!sign, /*neg=*/sign);
1407 set_nan (type, nan);
1410 // Return TRUE if range is known to be finite.
1412 inline bool
1413 frange::known_isfinite () const
1415 if (undefined_p () || varying_p () || m_kind == VR_ANTI_RANGE)
1416 return false;
1417 return (!maybe_isnan () && !real_isinf (&m_min) && !real_isinf (&m_max));
1420 // Return TRUE if range may be infinite.
1422 inline bool
1423 frange::maybe_isinf () const
1425 if (undefined_p () || m_kind == VR_ANTI_RANGE || m_kind == VR_NAN)
1426 return false;
1427 if (varying_p ())
1428 return true;
1429 return real_isinf (&m_min) || real_isinf (&m_max);
1432 // Return TRUE if range is known to be the [-INF,-INF] or [+INF,+INF].
1434 inline bool
1435 frange::known_isinf () const
1437 return (m_kind == VR_RANGE
1438 && !maybe_isnan ()
1439 && real_identical (&m_min, &m_max)
1440 && real_isinf (&m_min));
1443 // Return TRUE if range is possibly a NAN.
1445 inline bool
1446 frange::maybe_isnan () const
1448 if (undefined_p ())
1449 return false;
1450 return m_pos_nan || m_neg_nan;
1453 // Return TRUE if range is possibly a NAN with SIGN.
1455 inline bool
1456 frange::maybe_isnan (bool sign) const
1458 if (undefined_p ())
1459 return false;
1460 if (sign)
1461 return m_neg_nan;
1462 return m_pos_nan;
1465 // Return TRUE if range is a +NAN or -NAN.
1467 inline bool
1468 frange::known_isnan () const
1470 return m_kind == VR_NAN;
1473 // If the signbit for the range is known, set it in SIGNBIT and return
1474 // TRUE.
1476 inline bool
1477 frange::signbit_p (bool &signbit) const
1479 if (undefined_p ())
1480 return false;
1482 // NAN with unknown sign.
1483 if (m_pos_nan && m_neg_nan)
1484 return false;
1485 // No NAN.
1486 if (!m_pos_nan && !m_neg_nan)
1488 if (m_min.sign == m_max.sign)
1490 signbit = m_min.sign;
1491 return true;
1493 return false;
1495 // NAN with known sign.
1496 bool nan_sign = m_neg_nan;
1497 if (known_isnan ()
1498 || (nan_sign == m_min.sign && nan_sign == m_max.sign))
1500 signbit = nan_sign;
1501 return true;
1503 return false;
1506 // If range has a NAN with a known sign, set it in SIGNBIT and return
1507 // TRUE.
1509 inline bool
1510 frange::nan_signbit_p (bool &signbit) const
1512 if (undefined_p ())
1513 return false;
1515 if (m_pos_nan == m_neg_nan)
1516 return false;
1518 signbit = m_neg_nan;
1519 return true;
1522 void frange_nextafter (enum machine_mode, REAL_VALUE_TYPE &,
1523 const REAL_VALUE_TYPE &);
1524 void frange_arithmetic (enum tree_code, tree, REAL_VALUE_TYPE &,
1525 const REAL_VALUE_TYPE &, const REAL_VALUE_TYPE &,
1526 const REAL_VALUE_TYPE &);
1528 #endif // GCC_VALUE_RANGE_H