Debian Codegraph @

gcc/value-range.h

raw
1 /* Support routines for value ranges.
2 Copyright (C) 2019-2025 Free Software Foundation, Inc.
3 Contributed by Aldy Hernandez <[email protected]> and
4 Andrew Macleod <[email protected]>.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22 #ifndef GCC_VALUE_RANGE_H
23 #define GCC_VALUE_RANGE_H
24
25 class irange;
26
27 // Types of value ranges.
28 enum value_range_kind
29 {
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
42 };
43
44 // Discriminator between different vrange types.
45
46 enum value_range_discriminator
47 {
48 // Range holds an integer or pointer.
49 VR_IRANGE,
50 // Pointer range.
51 VR_PRANGE,
52 // Floating point range.
53 VR_FRANGE,
54 // Range holds an unsupported type.
55 VR_UNKNOWN
56 };
57
58 // Abstract class for ranges of any of the supported types.
59 //
60 // To query what types ranger and the entire ecosystem can support,
61 // use value_range::supports_type_p(tree type). This is a static
62 // method available independently of any vrange object.
63 //
64 // To query what a given vrange variant can support, use:
65 // irange::supports_p ()
66 // frange::supports_p ()
67 // etc
68 //
69 // To query what a range object can support, use:
70 // void foo (vrange &v, irange &i, frange &f)
71 // {
72 // if (v.supports_type_p (type)) ...
73 // if (i.supports_type_p (type)) ...
74 // if (f.supports_type_p (type)) ...
75 // }
76
77 class vrange
78 {
79 template <typename T> friend bool is_a (vrange &);
80 friend class value_range;
81 friend void streamer_write_vrange (struct output_block *, const vrange &);
82 friend class range_op_handler;
83 public:
84 virtual void accept (const class vrange_visitor &v) const = 0;
85 virtual void set (tree, tree, value_range_kind = VR_RANGE) = 0;
86 virtual tree type () const = 0;
87 virtual bool supports_type_p (const_tree type) const = 0;
88 virtual void set_varying (tree type) = 0;
89 virtual void set_undefined () = 0;
90 virtual bool union_ (const vrange &) = 0;
91 virtual bool intersect (const vrange &) = 0;
92 virtual bool singleton_p (tree *result = NULL) const = 0;
93 virtual bool contains_p (tree cst) const = 0;
94 virtual bool zero_p () const = 0;
95 virtual bool nonzero_p () const = 0;
96 virtual void set_nonzero (tree type) = 0;
97 virtual void set_zero (tree type) = 0;
98 virtual void set_nonnegative (tree type) = 0;
99 virtual bool fits_p (const vrange &r) const = 0;
100 virtual ~vrange () { }
101 virtual tree lbound () const = 0;
102 virtual tree ubound () const = 0;
103 virtual void update_bitmask (const class irange_bitmask &);
104 virtual irange_bitmask get_bitmask () const;
105 wide_int get_nonzero_bits () const;
106 void set_nonzero_bits (const wide_int &bits);
107
108 bool varying_p () const;
109 bool undefined_p () const;
110 vrange& operator= (const vrange &);
111 bool operator== (const vrange &) const;
112 bool operator!= (const vrange &r) const { return !(*this == r); }
113 void dump (FILE *) const;
114 protected:
115 vrange (enum value_range_discriminator d) : m_discriminator (d) { }
116 ENUM_BITFIELD(value_range_kind) m_kind : 8;
117 const ENUM_BITFIELD(value_range_discriminator) m_discriminator : 4;
118 };
119
120 namespace inchash
121 {
122 extern void add_vrange (const vrange &, hash &, unsigned flags = 0);
123 }
124
125 // A pair of values representing the known bits in a range. Zero bits
126 // in MASK cover constant values. Set bits in MASK cover unknown
127 // values. VALUE are the known bits.
128 //
129 // Set bits in MASK (no meaningful information) must have their
130 // corresponding bits in VALUE cleared, as this speeds up union and
131 // intersect.
132
133 class irange_bitmask
134 {
135 public:
136 irange_bitmask () { /* uninitialized */ }
137 irange_bitmask (unsigned prec) { set_unknown (prec); }
138 irange_bitmask (const wide_int &value, const wide_int &mask);
139 wide_int value () const { return m_value; }
140 wide_int mask () const { return m_mask; }
141 void set_unknown (unsigned prec);
142 bool unknown_p () const;
143 unsigned get_precision () const;
144 void union_ (const irange_bitmask &src);
145 void intersect (const irange_bitmask &src);
146 bool operator== (const irange_bitmask &src) const;
147 bool operator!= (const irange_bitmask &src) const { return !(*this == src); }
148 void verify_mask () const;
149 void dump (FILE *) const;
150
151 bool member_p (const wide_int &val) const;
152
153 // Convenience functions for nonzero bitmask compatibility.
154 wide_int get_nonzero_bits () const;
155 void set_nonzero_bits (const wide_int &bits);
156 private:
157 wide_int m_value;
158 wide_int m_mask;
159 };
160
161 inline void
162 irange_bitmask::set_unknown (unsigned prec)
163 {
164 m_value = wi::zero (prec);
165 m_mask = wi::minus_one (prec);
166 if (flag_checking)
167 verify_mask ();
168 }
169
170 // Return TRUE if THIS does not have any meaningful information.
171
172 inline bool
173 irange_bitmask::unknown_p () const
174 {
175 return m_mask == -1;
176 }
177
178 inline
179 irange_bitmask::irange_bitmask (const wide_int &value, const wide_int &mask)
180 {
181 m_value = value;
182 m_mask = mask;
183 if (flag_checking)
184 verify_mask ();
185 }
186
187 inline unsigned
188 irange_bitmask::get_precision () const
189 {
190 return m_mask.get_precision ();
191 }
192
193 // The following two functions are meant for backwards compatability
194 // with the nonzero bitmask. A cleared bit means the value must be 0.
195 // A set bit means we have no information for the bit.
196
197 // Return the nonzero bits.
198 inline wide_int
199 irange_bitmask::get_nonzero_bits () const
200 {
201 return m_value | m_mask;
202 }
203
204 // Set the bitmask to the nonzero bits in BITS.
205 inline void
206 irange_bitmask::set_nonzero_bits (const wide_int &bits)
207 {
208 m_value = wi::zero (bits.get_precision ());
209 m_mask = bits;
210 if (flag_checking)
211 verify_mask ();
212 }
213
214 // Return TRUE if val could be a valid value with this bitmask.
215
216 inline bool
217 irange_bitmask::member_p (const wide_int &val) const
218 {
219 if (unknown_p ())
220 return true;
221 wide_int res = m_mask & val;
222 if (m_value != 0)
223 res |= ~m_mask & m_value;
224 return res == val;
225 }
226
227 inline bool
228 irange_bitmask::operator== (const irange_bitmask &src) const
229 {
230 bool unknown1 = unknown_p ();
231 bool unknown2 = src.unknown_p ();
232 if (unknown1 || unknown2)
233 return unknown1 == unknown2;
234 return m_value == src.m_value && m_mask == src.m_mask;
235 }
236
237 inline void
238 irange_bitmask::union_ (const irange_bitmask &src)
239 {
240 m_mask = (m_mask | src.m_mask) | (m_value ^ src.m_value);
241 m_value = m_value & src.m_value;
242 if (flag_checking)
243 verify_mask ();
244 }
245
246 inline void
247 irange_bitmask::intersect (const irange_bitmask &src)
248 {
249 // If we have two known bits that are incompatible, the resulting
250 // bit is undefined. It is unclear whether we should set the entire
251 // range to UNDEFINED, or just a subset of it. For now, set the
252 // entire bitmask to unknown (VARYING).
253 if (wi::bit_and (~(m_mask | src.m_mask),
254 m_value ^ src.m_value) != 0)
255 {
256 unsigned prec = m_mask.get_precision ();
257 m_mask = wi::minus_one (prec);
258 m_value = wi::zero (prec);
259 }
260 else
261 {
262 m_mask = m_mask & src.m_mask;
263 m_value = m_value | src.m_value;
264 }
265 if (flag_checking)
266 verify_mask ();
267 }
268
269 // An integer range without any storage.
270
271 class irange : public vrange
272 {
273 friend class irange_storage;
274 friend class vrange_printer;
275 public:
276 // In-place setters.
277 void set (tree type, const wide_int &, const wide_int &,
278 value_range_kind = VR_RANGE);
279 virtual void set_nonzero (tree type) override;
280 virtual void set_zero (tree type) override;
281 virtual void set_nonnegative (tree type) override;
282 virtual void set_varying (tree type) override;
283 virtual void set_undefined () override;
284
285 // Range types.
286 static bool supports_p (const_tree type);
287 virtual bool supports_type_p (const_tree type) const override;
288 virtual tree type () const override;
289
290 // Iteration over sub-ranges.
291 unsigned num_pairs () const;
292 wide_int lower_bound (unsigned = 0) const;
293 wide_int upper_bound (unsigned) const;
294 wide_int upper_bound () const;
295 virtual tree lbound () const override;
296 virtual tree ubound () const override;
297
298 // Predicates.
299 virtual bool zero_p () const override;
300 virtual bool nonzero_p () const override;
301 virtual bool singleton_p (tree *result = NULL) const override;
302 bool singleton_p (wide_int &) const;
303 bool contains_p (const wide_int &) const;
304 bool nonnegative_p () const;
305 bool nonpositive_p () const;
306
307 // In-place operators.
308 virtual bool union_ (const vrange &) override;
309 virtual bool intersect (const vrange &) override;
310 void invert ();
311
312 // Operator overloads.
313 irange& operator= (const irange &);
314 bool operator== (const irange &) const;
315 bool operator!= (const irange &r) const { return !(*this == r); }
316
317 // Misc methods.
318 virtual bool fits_p (const vrange &r) const override;
319 virtual void accept (const vrange_visitor &v) const override;
320
321 virtual void update_bitmask (const class irange_bitmask &) override;
322 virtual irange_bitmask get_bitmask () const override;
323
324 protected:
325 void maybe_resize (int needed);
326 virtual void set (tree, tree, value_range_kind = VR_RANGE) override;
327 virtual bool contains_p (tree cst) const override;
328 irange (wide_int *, unsigned nranges, bool resizable);
329
330 // In-place operators.
331 bool irange_contains_p (const irange &) const;
332 bool irange_single_pair_union (const irange &r);
333
334 void normalize_kind ();
335
336 void verify_range ();
337
338 // Hard limit on max ranges allowed.
339 static const int HARD_MAX_RANGES = 255;
340 private:
341 bool varying_compatible_p () const;
342 bool intersect_bitmask (const irange &r);
343 bool union_bitmask (const irange &r);
344 bool set_range_from_bitmask ();
345
346 bool intersect (const wide_int& lb, const wide_int& ub);
347 bool union_append (const irange &r);
348 unsigned char m_num_ranges;
349 bool m_resizable;
350 unsigned char m_max_ranges;
351 tree m_type;
352 irange_bitmask m_bitmask;
353 protected:
354 wide_int *m_base;
355 };
356
357 // Here we describe an irange with N pairs of ranges. The storage for
358 // the pairs is embedded in the class as an array.
359 //
360 // If RESIZABLE is true, the storage will be resized on the heap when
361 // the number of ranges needed goes past N up to a max of
362 // HARD_MAX_RANGES. This new storage is freed upon destruction.
363
364 template<unsigned N, bool RESIZABLE = false>
365 class int_range final : public irange
366 {
367 public:
368 int_range ();
369 int_range (tree type, const wide_int &, const wide_int &,
370 value_range_kind = VR_RANGE);
371 int_range (tree type);
372 int_range (const int_range &);
373 int_range (const irange &);
374 ~int_range () final override;
375 int_range& operator= (const int_range &);
376 protected:
377 int_range (tree, tree, value_range_kind = VR_RANGE);
378 private:
379 wide_int m_ranges[N*2];
380 };
381
382 class prange final : public vrange
383 {
384 friend class prange_storage;
385 friend class vrange_printer;
386 public:
387 prange ();
388 prange (const prange &);
389 prange (tree type);
390 prange (tree type, const wide_int &, const wide_int &,
391 value_range_kind = VR_RANGE);
392 static bool supports_p (const_tree type);
393 virtual bool supports_type_p (const_tree type) const final override;
394 virtual void accept (const vrange_visitor &v) const final override;
395 virtual void set_undefined () final override;
396 virtual void set_varying (tree type) final override;
397 virtual void set_nonzero (tree type) final override;
398 virtual void set_zero (tree type) final override;
399 virtual void set_nonnegative (tree type) final override;
400 virtual bool contains_p (tree cst) const final override;
401 virtual bool fits_p (const vrange &v) const final override;
402 virtual bool singleton_p (tree *result = NULL) const final override;
403 virtual bool zero_p () const final override;
404 virtual bool nonzero_p () const final override;
405 virtual void set (tree, tree, value_range_kind = VR_RANGE) final override;
406 virtual tree type () const final override;
407 virtual bool union_ (const vrange &v) final override;
408 virtual bool intersect (const vrange &v) final override;
409 virtual tree lbound () const final override;
410 virtual tree ubound () const final override;
411
412 prange& operator= (const prange &);
413 bool operator== (const prange &) const;
414 void set (tree type, const wide_int &, const wide_int &,
415 value_range_kind = VR_RANGE);
416 void invert ();
417 bool contains_p (const wide_int &) const;
418 wide_int lower_bound () const;
419 wide_int upper_bound () const;
420 void verify_range () const;
421 irange_bitmask get_bitmask () const final override;
422 void update_bitmask (const irange_bitmask &) final override;
423 protected:
424 bool varying_compatible_p () const;
425
426 tree m_type;
427 wide_int m_min;
428 wide_int m_max;
429 irange_bitmask m_bitmask;
430 };
431
432 // Unsupported temporaries may be created by ranger before it's known
433 // they're unsupported, or by vr_values::get_value_range.
434
435 class unsupported_range : public vrange
436 {
437 public:
438 unsupported_range ()
439 : vrange (VR_UNKNOWN)
440 {
441 set_undefined ();
442 }
443 unsupported_range (const unsupported_range &src)
444 : vrange (VR_UNKNOWN)
445 {
446 unsupported_range::operator= (src);
447 }
448 void set (tree min, tree, value_range_kind = VR_RANGE) final override;
449 tree type () const final override;
450 bool supports_type_p (const_tree) const final override;
451 void set_varying (tree) final override;
452 void set_undefined () final override;
453 void accept (const vrange_visitor &v) const final override;
454 bool union_ (const vrange &r) final override;
455 bool intersect (const vrange &r) final override;
456 bool singleton_p (tree * = NULL) const final override;
457 bool contains_p (tree) const final override;
458 bool zero_p () const final override;
459 bool nonzero_p () const final override;
460 void set_nonzero (tree type) final override;
461 void set_zero (tree type) final override;
462 void set_nonnegative (tree type) final override;
463 bool fits_p (const vrange &) const final override;
464 unsupported_range& operator= (const unsupported_range &r);
465 tree lbound () const final override;
466 tree ubound () const final override;
467 };
468
469 // The NAN state as an opaque object.
470
471 class nan_state
472 {
473 public:
474 nan_state (bool);
475 nan_state (bool pos_nan, bool neg_nan);
476 bool neg_p () const;
477 bool pos_p () const;
478 private:
479 bool m_pos_nan;
480 bool m_neg_nan;
481 };
482
483 // Set NAN state to +-NAN if NAN_P is true. Otherwise set NAN state
484 // to false.
485
486 inline
487 nan_state::nan_state (bool nan_p)
488 {
489 m_pos_nan = nan_p;
490 m_neg_nan = nan_p;
491 }
492
493 // Constructor initializing the object to +NAN if POS_NAN is set, -NAN
494 // if NEG_NAN is set, or +-NAN if both are set. Otherwise POS_NAN and
495 // NEG_NAN are clear, and the object cannot be a NAN.
496
497 inline
498 nan_state::nan_state (bool pos_nan, bool neg_nan)
499 {
500 m_pos_nan = pos_nan;
501 m_neg_nan = neg_nan;
502 }
503
504 // Return if +NAN is possible.
505
506 inline bool
507 nan_state::pos_p () const
508 {
509 return m_pos_nan;
510 }
511
512 // Return if -NAN is possible.
513
514 inline bool
515 nan_state::neg_p () const
516 {
517 return m_neg_nan;
518 }
519
520 // A floating point range.
521 //
522 // The representation is a type with a couple of endpoints, unioned
523 // with the set of { -NAN, +Nan }.
524
525 class frange final : public vrange
526 {
527 friend class frange_storage;
528 friend class vrange_printer;
529 public:
530 frange ();
531 frange (const frange &);
532 frange (tree, tree, value_range_kind = VR_RANGE);
533 frange (tree type);
534 frange (tree type, const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
535 value_range_kind = VR_RANGE);
536 static bool supports_p (const_tree type)
537 {
538 // ?? Decimal floats can have multiple representations for the
539 // same number. Supporting them may be as simple as just
540 // disabling them in singleton_p. No clue.
541 return SCALAR_FLOAT_TYPE_P (type) && !DECIMAL_FLOAT_TYPE_P (type);
542 }
543 virtual tree type () const override;
544 void set (tree type, const REAL_VALUE_TYPE &, const REAL_VALUE_TYPE &,
545 value_range_kind = VR_RANGE);
546 void set (tree type, const REAL_VALUE_TYPE &, const REAL_VALUE_TYPE &,
547 const nan_state &, value_range_kind = VR_RANGE);
548 void set_nan (tree type);
549 void set_nan (tree type, bool sign);
550 void set_nan (tree type, const nan_state &);
551 virtual void set_varying (tree type) override;
552 virtual void set_undefined () override;
553 virtual bool union_ (const vrange &) override;
554 virtual bool intersect (const vrange &) override;
555 bool contains_p (const REAL_VALUE_TYPE &) const;
556 virtual bool singleton_p (tree *result = NULL) const override;
557 bool singleton_p (REAL_VALUE_TYPE &r) const;
558 virtual bool supports_type_p (const_tree type) const override;
559 virtual void accept (const vrange_visitor &v) const override;
560 virtual bool zero_p () const override;
561 virtual bool nonzero_p () const override;
562 virtual void set_nonzero (tree type) override;
563 virtual void set_zero (tree type) override;
564 virtual void set_nonnegative (tree type) override;
565 virtual bool fits_p (const vrange &) const override;
566 frange& operator= (const frange &);
567 bool operator== (const frange &) const;
568 bool operator!= (const frange &r) const { return !(*this == r); }
569 const REAL_VALUE_TYPE &lower_bound () const;
570 const REAL_VALUE_TYPE &upper_bound () const;
571 virtual tree lbound () const override;
572 virtual tree ubound () const override;
573 nan_state get_nan_state () const;
574 void update_nan ();
575 void update_nan (bool sign);
576 void update_nan (tree) = delete; // Disallow silent conversion to bool.
577 void update_nan (const nan_state &);
578 void clear_nan ();
579 void flush_denormals_to_zero ();
580
581 // fpclassify like API
582 bool known_isfinite () const;
583 bool known_isnan () const;
584 bool known_isinf () const;
585 bool maybe_isnan () const;
586 bool maybe_isnan (bool sign) const;
587 bool maybe_isinf () const;
588 bool signbit_p (bool &signbit) const;
589 bool nan_signbit_p (bool &signbit) const;
590 bool known_isnormal () const;
591 bool known_isdenormal_or_zero () const;
592
593 protected:
594 virtual bool contains_p (tree cst) const override;
595 virtual void set (tree, tree, value_range_kind = VR_RANGE) override;
596
597 private:
598 bool internal_singleton_p (REAL_VALUE_TYPE * = NULL) const;
599 void verify_range ();
600 bool normalize_kind ();
601 bool union_nans (const frange &);
602 bool intersect_nans (const frange &);
603 bool combine_zeros (const frange &, bool union_p);
604
605 tree m_type;
606 REAL_VALUE_TYPE m_min;
607 REAL_VALUE_TYPE m_max;
608 bool m_pos_nan;
609 bool m_neg_nan;
610 };
611
612 inline const REAL_VALUE_TYPE &
613 frange::lower_bound () const
614 {
615 gcc_checking_assert (!undefined_p () && !known_isnan ());
616 return m_min;
617 }
618
619 inline const REAL_VALUE_TYPE &
620 frange::upper_bound () const
621 {
622 gcc_checking_assert (!undefined_p () && !known_isnan ());
623 return m_max;
624 }
625
626 // Return the NAN state.
627
628 inline nan_state
629 frange::get_nan_state () const
630 {
631 return nan_state (m_pos_nan, m_neg_nan);
632 }
633
634 // is_a<> and as_a<> implementation for vrange.
635
636 // Anything we haven't specialized is a hard fail.
637 template <typename T>
638 inline bool
639 is_a (vrange &)
640 {
641 gcc_unreachable ();
642 return false;
643 }
644
645 template <typename T>
646 inline bool
647 is_a (const vrange &v)
648 {
649 // Reuse is_a <vrange> to implement the const version.
650 const T &derived = static_cast<const T &> (v);
651 return is_a <T> (const_cast<T &> (derived));
652 }
653
654 template <typename T>
655 inline T &
656 as_a (vrange &v)
657 {
658 gcc_checking_assert (is_a <T> (v));
659 return static_cast <T &> (v);
660 }
661
662 template <typename T>
663 inline const T &
664 as_a (const vrange &v)
665 {
666 gcc_checking_assert (is_a <T> (v));
667 return static_cast <const T &> (v);
668 }
669
670 // Specializations for the different range types.
671
672 template <>
673 inline bool
674 is_a <irange> (vrange &v)
675 {
676 return v.m_discriminator == VR_IRANGE;
677 }
678
679 template <>
680 inline bool
681 is_a <prange> (vrange &v)
682 {
683 return v.m_discriminator == VR_PRANGE;
684 }
685
686 template <>
687 inline bool
688 is_a <frange> (vrange &v)
689 {
690 return v.m_discriminator == VR_FRANGE;
691 }
692
693 template <>
694 inline bool
695 is_a <unsupported_range> (vrange &v)
696 {
697 return v.m_discriminator == VR_UNKNOWN;
698 }
699
700 // For resizable ranges, resize the range up to HARD_MAX_RANGES if the
701 // NEEDED pairs is greater than the current capacity of the range.
702
703 inline void
704 irange::maybe_resize (int needed)
705 {
706 if (!m_resizable || m_max_ranges == HARD_MAX_RANGES)
707 return;
708
709 if (needed > m_max_ranges)
710 {
711 m_max_ranges = HARD_MAX_RANGES;
712 wide_int *newmem = new wide_int[m_max_ranges * 2];
713 unsigned n = num_pairs () * 2;
714 for (unsigned i = 0; i < n; ++i)
715 newmem[i] = m_base[i];
716 m_base = newmem;
717 }
718 }
719
720 template<unsigned N, bool RESIZABLE>
721 inline
722 int_range<N, RESIZABLE>::~int_range ()
723 {
724 if (RESIZABLE && m_base != m_ranges)
725 delete[] m_base;
726 }
727
728 // This is an "infinite" precision irange for use in temporary
729 // calculations. It starts with a sensible default covering 99% of
730 // uses, and goes up to HARD_MAX_RANGES when needed. Any allocated
731 // storage is freed upon destruction.
732 typedef int_range<3, /*RESIZABLE=*/true> int_range_max;
733
734 class vrange_visitor
735 {
736 public:
737 virtual void visit (const irange &) const { }
738 virtual void visit (const prange &) const { }
739 virtual void visit (const frange &) const { }
740 virtual void visit (const unsupported_range &) const { }
741 };
742
743 // This is an "infinite" precision range object for use in temporary
744 // calculations for any of the handled types. The object can be
745 // transparently used as a vrange.
746 //
747 // Using any of the various constructors initializes the object
748 // appropriately, but the default constructor is uninitialized and
749 // must be initialized either with set_type() or by assigning into it.
750 //
751 // Assigning between incompatible types is allowed. For example if a
752 // temporary holds an irange, you can assign an frange into it, and
753 // all the right things will happen. However, before passing this
754 // object to a function accepting a vrange, the correct type must be
755 // set. If it isn't, you can do so with set_type().
756
757 class value_range
758 {
759 public:
760 value_range ();
761 value_range (const vrange &r);
762 value_range (tree type);
763 value_range (tree, tree, value_range_kind kind = VR_RANGE);
764 value_range (const value_range &);
765 ~value_range ();
766 void set_type (tree type);
767 vrange& operator= (const vrange &);
768 value_range& operator= (const value_range &);
769 bool operator== (const value_range &r) const;
770 bool operator!= (const value_range &r) const;
771 operator vrange &();
772 operator const vrange &() const;
773 void dump (FILE *) const;
774 static bool supports_type_p (const_tree type);
775
776 tree type () { return m_vrange->type (); }
777 bool varying_p () const { return m_vrange->varying_p (); }
778 bool undefined_p () const { return m_vrange->undefined_p (); }
779 void set_varying (tree type) { init (type); m_vrange->set_varying (type); }
780 void set_undefined () { m_vrange->set_undefined (); }
781 bool union_ (const vrange &r) { return m_vrange->union_ (r); }
782 bool intersect (const vrange &r) { return m_vrange->intersect (r); }
783 bool contains_p (tree cst) const { return m_vrange->contains_p (cst); }
784 bool singleton_p (tree *result = NULL) const
785 { return m_vrange->singleton_p (result); }
786 void set_zero (tree type) { init (type); return m_vrange->set_zero (type); }
787 void set_nonzero (tree type)
788 { init (type); return m_vrange->set_nonzero (type); }
789 bool nonzero_p () const { return m_vrange->nonzero_p (); }
790 bool zero_p () const { return m_vrange->zero_p (); }
791 tree lbound () const { return m_vrange->lbound (); }
792 tree ubound () const { return m_vrange->ubound (); }
793 irange_bitmask get_bitmask () const { return m_vrange->get_bitmask (); }
794 void update_bitmask (const class irange_bitmask &bm)
795 { return m_vrange->update_bitmask (bm); }
796 void accept (const vrange_visitor &v) const { m_vrange->accept (v); }
797 private:
798 void init (tree type);
799 void init (const vrange &);
800
801 vrange *m_vrange;
802 union buffer_type {
803 int_range_max ints;
804 frange floats;
805 unsupported_range unsupported;
806 prange pointers;
807 buffer_type () { }
808 ~buffer_type () { }
809 } m_buffer;
810 };
811
812 // The default constructor is uninitialized and must be initialized
813 // with either set_type() or with an assignment into it.
814
815 inline
816 value_range::value_range ()
817 : m_buffer ()
818 {
819 m_vrange = NULL;
820 }
821
822 // Copy constructor.
823
824 inline
825 value_range::value_range (const value_range &r)
826 {
827 init (*r.m_vrange);
828 }
829
830 // Copy constructor from a vrange.
831
832 inline
833 value_range::value_range (const vrange &r)
834 {
835 init (r);
836 }
837
838 // Construct an UNDEFINED range that can hold ranges of TYPE. If TYPE
839 // is not supported, default to unsupported_range.
840
841 inline
842 value_range::value_range (tree type)
843 {
844 init (type);
845 }
846
847 // Construct a range that can hold a range of [MIN, MAX], where MIN
848 // and MAX are trees.
849
850 inline
851 value_range::value_range (tree min, tree max, value_range_kind kind)
852 {
853 init (TREE_TYPE (min));
854 m_vrange->set (min, max, kind);
855 }
856
857 inline
858 value_range::~value_range ()
859 {
860 if (m_vrange)
861 m_vrange->~vrange ();
862 }
863
864 // Initialize object to an UNDEFINED range that can hold ranges of
865 // TYPE. Clean-up memory if there was a previous object.
866
867 inline void
868 value_range::set_type (tree type)
869 {
870 if (m_vrange)
871 m_vrange->~vrange ();
872 init (type);
873 }
874
875 // Initialize object to an UNDEFINED range that can hold ranges of
876 // TYPE.
877
878 inline void
879 value_range::init (tree type)
880 {
881 gcc_checking_assert (TYPE_P (type));
882
883 if (irange::supports_p (type))
884 m_vrange = new (&m_buffer.ints) int_range_max ();
885 else if (prange::supports_p (type))
886 m_vrange = new (&m_buffer.pointers) prange ();
887 else if (frange::supports_p (type))
888 m_vrange = new (&m_buffer.floats) frange ();
889 else
890 m_vrange = new (&m_buffer.unsupported) unsupported_range ();
891 }
892
893 // Initialize object with a copy of R.
894
895 inline void
896 value_range::init (const vrange &r)
897 {
898 if (is_a <irange> (r))
899 m_vrange = new (&m_buffer.ints) int_range_max (as_a <irange> (r));
900 else if (is_a <prange> (r))
901 m_vrange = new (&m_buffer.pointers) prange (as_a <prange> (r));
902 else if (is_a <frange> (r))
903 m_vrange = new (&m_buffer.floats) frange (as_a <frange> (r));
904 else
905 m_vrange = new (&m_buffer.unsupported)
906 unsupported_range (as_a <unsupported_range> (r));
907 }
908
909 // Assignment operator. Copying incompatible types is allowed. That
910 // is, assigning an frange to an object holding an irange does the
911 // right thing.
912
913 inline vrange &
914 value_range::operator= (const vrange &r)
915 {
916 if (m_vrange)
917 m_vrange->~vrange ();
918 init (r);
919 return *m_vrange;
920 }
921
922 inline value_range &
923 value_range::operator= (const value_range &r)
924 {
925 // No need to call the m_vrange destructor here, as we will do so in
926 // the assignment below.
927 *this = *r.m_vrange;
928 return *this;
929 }
930
931 inline bool
932 value_range::operator== (const value_range &r) const
933 {
934 return *m_vrange == *r.m_vrange;
935 }
936
937 inline bool
938 value_range::operator!= (const value_range &r) const
939 {
940 return *m_vrange != *r.m_vrange;
941 }
942
943 inline
944 value_range::operator vrange &()
945 {
946 return *m_vrange;
947 }
948
949 inline
950 value_range::operator const vrange &() const
951 {
952 return *m_vrange;
953 }
954
955 // Return TRUE if TYPE is supported by the vrange infrastructure.
956
957 inline bool
958 value_range::supports_type_p (const_tree type)
959 {
960 return irange::supports_p (type)
961 || prange::supports_p (type)
962 || frange::supports_p (type);
963 }
964
965 extern value_range_kind get_legacy_range (const vrange &, tree &min, tree &max);
966 extern void dump_value_range (FILE *, const vrange *);
967 extern bool vrp_operand_equal_p (const_tree, const_tree);
968 inline REAL_VALUE_TYPE frange_val_min (const_tree type);
969 inline REAL_VALUE_TYPE frange_val_max (const_tree type);
970
971 // Number of sub-ranges in a range.
972
973 inline unsigned
974 irange::num_pairs () const
975 {
976 return m_num_ranges;
977 }
978
979 inline tree
980 irange::type () const
981 {
982 gcc_checking_assert (m_num_ranges > 0);
983 return m_type;
984 }
985
986 inline bool
987 irange::varying_compatible_p () const
988 {
989 if (m_num_ranges != 1)
990 return false;
991
992 const wide_int &l = m_base[0];
993 const wide_int &u = m_base[1];
994 tree t = m_type;
995
996 if (m_kind == VR_VARYING)
997 return true;
998
999 unsigned prec = TYPE_PRECISION (t);
1000 signop sign = TYPE_SIGN (t);
1001 if (INTEGRAL_TYPE_P (t) || POINTER_TYPE_P (t))
1002 return (l == wi::min_value (prec, sign)
1003 && u == wi::max_value (prec, sign)
1004 && m_bitmask.unknown_p ());
1005 return true;
1006 }
1007
1008 inline bool
1009 vrange::varying_p () const
1010 {
1011 return m_kind == VR_VARYING;
1012 }
1013
1014 inline bool
1015 vrange::undefined_p () const
1016 {
1017 return m_kind == VR_UNDEFINED;
1018 }
1019
1020 inline bool
1021 irange::zero_p () const
1022 {
1023 return (m_kind == VR_RANGE && m_num_ranges == 1
1024 && lower_bound (0) == 0
1025 && upper_bound (0) == 0);
1026 }
1027
1028 inline bool
1029 irange::nonzero_p () const
1030 {
1031 if (undefined_p ())
1032 return false;
1033
1034 wide_int zero = wi::zero (TYPE_PRECISION (type ()));
1035 return *this == int_range<2> (type (), zero, zero, VR_ANTI_RANGE);
1036 }
1037
1038 inline bool
1039 irange::supports_p (const_tree type)
1040 {
1041 return INTEGRAL_TYPE_P (type);
1042 }
1043
1044 inline bool
1045 irange::contains_p (tree cst) const
1046 {
1047 return contains_p (wi::to_wide (cst));
1048 }
1049
1050 inline bool
1051 range_includes_zero_p (const vrange &vr)
1052 {
1053 if (vr.undefined_p ())
1054 return false;
1055
1056 if (vr.varying_p ())
1057 return true;
1058
1059 return vr.contains_p (build_zero_cst (vr.type ()));
1060 }
1061
1062 // Constructors for irange
1063
1064 inline
1065 irange::irange (wide_int *base, unsigned nranges, bool resizable)
1066 : vrange (VR_IRANGE),
1067 m_resizable (resizable),
1068 m_max_ranges (nranges)
1069 {
1070 m_base = base;
1071 set_undefined ();
1072 }
1073
1074 // Constructors for int_range<>.
1075
1076 template<unsigned N, bool RESIZABLE>
1077 inline
1078 int_range<N, RESIZABLE>::int_range ()
1079 : irange (m_ranges, N, RESIZABLE)
1080 {
1081 }
1082
1083 template<unsigned N, bool RESIZABLE>
1084 int_range<N, RESIZABLE>::int_range (const int_range &other)
1085 : irange (m_ranges, N, RESIZABLE)
1086 {
1087 irange::operator= (other);
1088 }
1089
1090 template<unsigned N, bool RESIZABLE>
1091 int_range<N, RESIZABLE>::int_range (tree min, tree max, value_range_kind kind)
1092 : irange (m_ranges, N, RESIZABLE)
1093 {
1094 irange::set (min, max, kind);
1095 }
1096
1097 template<unsigned N, bool RESIZABLE>
1098 int_range<N, RESIZABLE>::int_range (tree type)
1099 : irange (m_ranges, N, RESIZABLE)
1100 {
1101 set_varying (type);
1102 }
1103
1104 template<unsigned N, bool RESIZABLE>
1105 int_range<N, RESIZABLE>::int_range (tree type, const wide_int &wmin, const wide_int &wmax,
1106 value_range_kind kind)
1107 : irange (m_ranges, N, RESIZABLE)
1108 {
1109 set (type, wmin, wmax, kind);
1110 }
1111
1112 template<unsigned N, bool RESIZABLE>
1113 int_range<N, RESIZABLE>::int_range (const irange &other)
1114 : irange (m_ranges, N, RESIZABLE)
1115 {
1116 irange::operator= (other);
1117 }
1118
1119 template<unsigned N, bool RESIZABLE>
1120 int_range<N, RESIZABLE>&
1121 int_range<N, RESIZABLE>::operator= (const int_range &src)
1122 {
1123 irange::operator= (src);
1124 return *this;
1125 }
1126
1127 inline void
1128 irange::set_undefined ()
1129 {
1130 m_kind = VR_UNDEFINED;
1131 m_num_ranges = 0;
1132 }
1133
1134 inline void
1135 irange::set_varying (tree type)
1136 {
1137 m_kind = VR_VARYING;
1138 m_num_ranges = 1;
1139 m_bitmask.set_unknown (TYPE_PRECISION (type));
1140
1141 if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1142 {
1143 m_type = type;
1144 // Strict enum's require varying to be not TYPE_MIN/MAX, but rather
1145 // min_value and max_value.
1146 m_base[0] = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1147 m_base[1] = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1148 }
1149 else
1150 m_type = error_mark_node;
1151 }
1152
1153 // Return the lower bound of a sub-range. PAIR is the sub-range in
1154 // question.
1155
1156 inline wide_int
1157 irange::lower_bound (unsigned pair) const
1158 {
1159 gcc_checking_assert (m_num_ranges > 0);
1160 gcc_checking_assert (pair + 1 <= num_pairs ());
1161 return m_base[pair * 2];
1162 }
1163
1164 // Return the upper bound of a sub-range. PAIR is the sub-range in
1165 // question.
1166
1167 inline wide_int
1168 irange::upper_bound (unsigned pair) const
1169 {
1170 gcc_checking_assert (m_num_ranges > 0);
1171 gcc_checking_assert (pair + 1 <= num_pairs ());
1172 return m_base[pair * 2 + 1];
1173 }
1174
1175 // Return the highest bound of a range.
1176
1177 inline wide_int
1178 irange::upper_bound () const
1179 {
1180 unsigned pairs = num_pairs ();
1181 gcc_checking_assert (pairs > 0);
1182 return upper_bound (pairs - 1);
1183 }
1184
1185 // Set value range VR to a nonzero range of type TYPE.
1186
1187 inline void
1188 irange::set_nonzero (tree type)
1189 {
1190 unsigned prec = TYPE_PRECISION (type);
1191
1192 if (TYPE_UNSIGNED (type))
1193 {
1194 m_type = type;
1195 m_kind = VR_RANGE;
1196 m_base[0] = wi::one (prec);
1197 m_base[1] = wi::minus_one (prec);
1198 m_bitmask.set_unknown (prec);
1199 m_num_ranges = 1;
1200
1201 if (flag_checking)
1202 verify_range ();
1203 }
1204 else
1205 {
1206 wide_int zero = wi::zero (prec);
1207 set (type, zero, zero, VR_ANTI_RANGE);
1208 }
1209 }
1210
1211 // Set value range VR to a ZERO range of type TYPE.
1212
1213 inline void
1214 irange::set_zero (tree type)
1215 {
1216 wide_int zero = wi::zero (TYPE_PRECISION (type));
1217 set (type, zero, zero);
1218 }
1219
1220 // Normalize a range to VARYING or UNDEFINED if possible.
1221
1222 inline void
1223 irange::normalize_kind ()
1224 {
1225 if (m_num_ranges == 0)
1226 set_undefined ();
1227 else if (varying_compatible_p ())
1228 {
1229 if (m_kind == VR_RANGE)
1230 m_kind = VR_VARYING;
1231 else if (m_kind == VR_ANTI_RANGE)
1232 set_undefined ();
1233 }
1234 if (flag_checking)
1235 verify_range ();
1236 }
1237
1238 inline bool
1239 contains_zero_p (const irange &r)
1240 {
1241 if (r.undefined_p ())
1242 return false;
1243
1244 wide_int zero = wi::zero (TYPE_PRECISION (r.type ()));
1245 return r.contains_p (zero);
1246 }
1247
1248 inline wide_int
1249 irange_val_min (const_tree type)
1250 {
1251 gcc_checking_assert (irange::supports_p (type));
1252 return wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1253 }
1254
1255 inline wide_int
1256 irange_val_max (const_tree type)
1257 {
1258 gcc_checking_assert (irange::supports_p (type));
1259 return wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1260 }
1261
1262 inline
1263 prange::prange ()
1264 : vrange (VR_PRANGE)
1265 {
1266 set_undefined ();
1267 }
1268
1269 inline
1270 prange::prange (const prange &r)
1271 : vrange (VR_PRANGE)
1272 {
1273 *this = r;
1274 }
1275
1276 inline
1277 prange::prange (tree type)
1278 : vrange (VR_PRANGE)
1279 {
1280 set_varying (type);
1281 }
1282
1283 inline
1284 prange::prange (tree type, const wide_int &lb, const wide_int &ub,
1285 value_range_kind kind)
1286 : vrange (VR_PRANGE)
1287 {
1288 set (type, lb, ub, kind);
1289 }
1290
1291 inline bool
1292 prange::supports_p (const_tree type)
1293 {
1294 return POINTER_TYPE_P (type);
1295 }
1296
1297 inline bool
1298 prange::supports_type_p (const_tree type) const
1299 {
1300 return POINTER_TYPE_P (type);
1301 }
1302
1303 inline void
1304 prange::set_undefined ()
1305 {
1306 m_kind = VR_UNDEFINED;
1307 }
1308
1309 inline void
1310 prange::set_varying (tree type)
1311 {
1312 m_kind = VR_VARYING;
1313 m_type = type;
1314 m_min = wi::zero (TYPE_PRECISION (type));
1315 m_max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
1316 m_bitmask.set_unknown (TYPE_PRECISION (type));
1317
1318 if (flag_checking)
1319 verify_range ();
1320 }
1321
1322 inline void
1323 prange::set_nonzero (tree type)
1324 {
1325 m_kind = VR_RANGE;
1326 m_type = type;
1327 m_min = wi::one (TYPE_PRECISION (type));
1328 m_max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
1329 m_bitmask.set_unknown (TYPE_PRECISION (type));
1330
1331 if (flag_checking)
1332 verify_range ();
1333 }
1334
1335 inline void
1336 prange::set_zero (tree type)
1337 {
1338 m_kind = VR_RANGE;
1339 m_type = type;
1340 wide_int zero = wi::zero (TYPE_PRECISION (type));
1341 m_min = m_max = zero;
1342 m_bitmask = irange_bitmask (zero, zero);
1343
1344 if (flag_checking)
1345 verify_range ();
1346 }
1347
1348 inline bool
1349 prange::contains_p (tree cst) const
1350 {
1351 return contains_p (wi::to_wide (cst));
1352 }
1353
1354 inline bool
1355 prange::zero_p () const
1356 {
1357 return m_kind == VR_RANGE && m_min == 0 && m_max == 0;
1358 }
1359
1360 inline bool
1361 prange::nonzero_p () const
1362 {
1363 return m_kind == VR_RANGE && m_min == 1 && m_max == -1;
1364 }
1365
1366 inline tree
1367 prange::type () const
1368 {
1369 gcc_checking_assert (!undefined_p ());
1370 return m_type;
1371 }
1372
1373 inline wide_int
1374 prange::lower_bound () const
1375 {
1376 gcc_checking_assert (!undefined_p ());
1377 return m_min;
1378 }
1379
1380 inline wide_int
1381 prange::upper_bound () const
1382 {
1383 gcc_checking_assert (!undefined_p ());
1384 return m_max;
1385 }
1386
1387 inline bool
1388 prange::varying_compatible_p () const
1389 {
1390 return (!undefined_p ()
1391 && m_min == 0 && m_max == -1 && get_bitmask ().unknown_p ());
1392 }
1393
1394 inline irange_bitmask
1395 prange::get_bitmask () const
1396 {
1397 return m_bitmask;
1398 }
1399
1400 inline bool
1401 prange::fits_p (const vrange &) const
1402 {
1403 return true;
1404 }
1405
1406
1407 inline
1408 frange::frange ()
1409 : vrange (VR_FRANGE)
1410 {
1411 set_undefined ();
1412 }
1413
1414 inline
1415 frange::frange (const frange &src)
1416 : vrange (VR_FRANGE)
1417 {
1418 *this = src;
1419 }
1420
1421 inline
1422 frange::frange (tree type)
1423 : vrange (VR_FRANGE)
1424 {
1425 set_varying (type);
1426 }
1427
1428 // frange constructor from REAL_VALUE_TYPE endpoints.
1429
1430 inline
1431 frange::frange (tree type,
1432 const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
1433 value_range_kind kind)
1434 : vrange (VR_FRANGE)
1435 {
1436 set (type, min, max, kind);
1437 }
1438
1439 // frange constructor from trees.
1440
1441 inline
1442 frange::frange (tree min, tree max, value_range_kind kind)
1443 : vrange (VR_FRANGE)
1444 {
1445 set (min, max, kind);
1446 }
1447
1448 inline tree
1449 frange::type () const
1450 {
1451 gcc_checking_assert (!undefined_p ());
1452 return m_type;
1453 }
1454
1455 inline void
1456 frange::set_varying (tree type)
1457 {
1458 m_kind = VR_VARYING;
1459 m_type = type;
1460 m_min = frange_val_min (type);
1461 m_max = frange_val_max (type);
1462 if (HONOR_NANS (m_type))
1463 {
1464 m_pos_nan = true;
1465 m_neg_nan = true;
1466 }
1467 else
1468 {
1469 m_pos_nan = false;
1470 m_neg_nan = false;
1471 }
1472 }
1473
1474 inline void
1475 frange::set_undefined ()
1476 {
1477 m_kind = VR_UNDEFINED;
1478 m_type = NULL;
1479 m_pos_nan = false;
1480 m_neg_nan = false;
1481 // m_min and m_min are uninitialized as they are REAL_VALUE_TYPE ??.
1482 if (flag_checking)
1483 verify_range ();
1484 }
1485
1486 // Set the NAN bits to NAN and adjust the range.
1487
1488 inline void
1489 frange::update_nan (const nan_state &nan)
1490 {
1491 gcc_checking_assert (!undefined_p ());
1492 if (HONOR_NANS (m_type))
1493 {
1494 m_pos_nan = nan.pos_p ();
1495 m_neg_nan = nan.neg_p ();
1496 normalize_kind ();
1497 if (flag_checking)
1498 verify_range ();
1499 }
1500 }
1501
1502 // Set the NAN bit to +-NAN.
1503
1504 inline void
1505 frange::update_nan ()
1506 {
1507 gcc_checking_assert (!undefined_p ());
1508 nan_state nan (true);
1509 update_nan (nan);
1510 }
1511
1512 // Like above, but set the sign of the NAN.
1513
1514 inline void
1515 frange::update_nan (bool sign)
1516 {
1517 gcc_checking_assert (!undefined_p ());
1518 nan_state nan (/*pos=*/!sign, /*neg=*/sign);
1519 update_nan (nan);
1520 }
1521
1522 inline bool
1523 frange::contains_p (tree cst) const
1524 {
1525 return contains_p (*TREE_REAL_CST_PTR (cst));
1526 }
1527
1528 // Clear the NAN bit and adjust the range.
1529
1530 inline void
1531 frange::clear_nan ()
1532 {
1533 gcc_checking_assert (!undefined_p ());
1534 m_pos_nan = false;
1535 m_neg_nan = false;
1536 normalize_kind ();
1537 if (flag_checking)
1538 verify_range ();
1539 }
1540
1541 // Set R to maximum representable value for TYPE.
1542
1543 inline REAL_VALUE_TYPE
1544 real_max_representable (const_tree type)
1545 {
1546 REAL_VALUE_TYPE r;
1547 char buf[128];
1548 get_max_float (REAL_MODE_FORMAT (TYPE_MODE (type)),
1549 buf, sizeof (buf), false);
1550 int res = real_from_string (&r, buf);
1551 gcc_checking_assert (!res);
1552 return r;
1553 }
1554
1555 // Return the minimum representable value for TYPE.
1556
1557 inline REAL_VALUE_TYPE
1558 real_min_representable (const_tree type)
1559 {
1560 REAL_VALUE_TYPE r = real_max_representable (type);
1561 r = real_value_negate (&r);
1562 return r;
1563 }
1564
1565 // Return the minimum value for TYPE.
1566
1567 inline REAL_VALUE_TYPE
1568 frange_val_min (const_tree type)
1569 {
1570 if (HONOR_INFINITIES (type))
1571 return dconstninf;
1572 else
1573 return real_min_representable (type);
1574 }
1575
1576 // Return the maximum value for TYPE.
1577
1578 inline REAL_VALUE_TYPE
1579 frange_val_max (const_tree type)
1580 {
1581 if (HONOR_INFINITIES (type))
1582 return dconstinf;
1583 else
1584 return real_max_representable (type);
1585 }
1586
1587 // Return TRUE if R is the minimum value for TYPE.
1588
1589 inline bool
1590 frange_val_is_min (const REAL_VALUE_TYPE &r, const_tree type)
1591 {
1592 REAL_VALUE_TYPE min = frange_val_min (type);
1593 return real_identical (&min, &r);
1594 }
1595
1596 // Return TRUE if R is the max value for TYPE.
1597
1598 inline bool
1599 frange_val_is_max (const REAL_VALUE_TYPE &r, const_tree type)
1600 {
1601 REAL_VALUE_TYPE max = frange_val_max (type);
1602 return real_identical (&max, &r);
1603 }
1604
1605 // Build a NAN with a state of NAN.
1606
1607 inline void
1608 frange::set_nan (tree type, const nan_state &nan)
1609 {
1610 gcc_checking_assert (nan.pos_p () || nan.neg_p ());
1611 if (HONOR_NANS (type))
1612 {
1613 m_kind = VR_NAN;
1614 m_type = type;
1615 m_neg_nan = nan.neg_p ();
1616 m_pos_nan = nan.pos_p ();
1617 if (flag_checking)
1618 verify_range ();
1619 }
1620 else
1621 set_undefined ();
1622 }
1623
1624 // Build a signless NAN of type TYPE.
1625
1626 inline void
1627 frange::set_nan (tree type)
1628 {
1629 nan_state nan (true);
1630 set_nan (type, nan);
1631 }
1632
1633 // Build a NAN of type TYPE with SIGN.
1634
1635 inline void
1636 frange::set_nan (tree type, bool sign)
1637 {
1638 nan_state nan (/*pos=*/!sign, /*neg=*/sign);
1639 set_nan (type, nan);
1640 }
1641
1642 // Return TRUE if range is known to be finite.
1643
1644 inline bool
1645 frange::known_isfinite () const
1646 {
1647 if (undefined_p () || varying_p () || m_kind == VR_ANTI_RANGE)
1648 return false;
1649 return (!maybe_isnan () && !real_isinf (&m_min) && !real_isinf (&m_max));
1650 }
1651
1652 // Return TRUE if range is known to be normal.
1653
1654 inline bool
1655 frange::known_isnormal () const
1656 {
1657 if (!known_isfinite ())
1658 return false;
1659
1660 machine_mode mode = TYPE_MODE (type ());
1661 return (!real_isdenormal (&m_min, mode) && !real_isdenormal (&m_max, mode)
1662 && !real_iszero (&m_min) && !real_iszero (&m_max)
1663 && (!real_isneg (&m_min) || real_isneg (&m_max)));
1664 }
1665
1666 // Return TRUE if range is known to be denormal.
1667
1668 inline bool
1669 frange::known_isdenormal_or_zero () const
1670 {
1671 if (!known_isfinite ())
1672 return false;
1673
1674 machine_mode mode = TYPE_MODE (type ());
1675 return ((real_isdenormal (&m_min, mode) || real_iszero (&m_min))
1676 && (real_isdenormal (&m_max, mode) || real_iszero (&m_max)));
1677 }
1678
1679 // Return TRUE if range may be infinite.
1680
1681 inline bool
1682 frange::maybe_isinf () const
1683 {
1684 if (undefined_p () || m_kind == VR_ANTI_RANGE || m_kind == VR_NAN)
1685 return false;
1686 if (varying_p ())
1687 return true;
1688 return real_isinf (&m_min) || real_isinf (&m_max);
1689 }
1690
1691 // Return TRUE if range is known to be the [-INF,-INF] or [+INF,+INF].
1692
1693 inline bool
1694 frange::known_isinf () const
1695 {
1696 return (m_kind == VR_RANGE
1697 && !maybe_isnan ()
1698 && real_identical (&m_min, &m_max)
1699 && real_isinf (&m_min));
1700 }
1701
1702 // Return TRUE if range is possibly a NAN.
1703
1704 inline bool
1705 frange::maybe_isnan () const
1706 {
1707 if (undefined_p ())
1708 return false;
1709 return m_pos_nan || m_neg_nan;
1710 }
1711
1712 // Return TRUE if range is possibly a NAN with SIGN.
1713
1714 inline bool
1715 frange::maybe_isnan (bool sign) const
1716 {
1717 if (undefined_p ())
1718 return false;
1719 if (sign)
1720 return m_neg_nan;
1721 return m_pos_nan;
1722 }
1723
1724 // Return TRUE if range is a +NAN or -NAN.
1725
1726 inline bool
1727 frange::known_isnan () const
1728 {
1729 return m_kind == VR_NAN;
1730 }
1731
1732 // If the signbit for the range is known, set it in SIGNBIT and return
1733 // TRUE.
1734
1735 inline bool
1736 frange::signbit_p (bool &signbit) const
1737 {
1738 if (undefined_p ())
1739 return false;
1740
1741 // NAN with unknown sign.
1742 if (m_pos_nan && m_neg_nan)
1743 return false;
1744 // No NAN.
1745 if (!m_pos_nan && !m_neg_nan)
1746 {
1747 if (m_min.sign == m_max.sign)
1748 {
1749 signbit = m_min.sign;
1750 return true;
1751 }
1752 return false;
1753 }
1754 // NAN with known sign.
1755 bool nan_sign = m_neg_nan;
1756 if (known_isnan ()
1757 || (nan_sign == m_min.sign && nan_sign == m_max.sign))
1758 {
1759 signbit = nan_sign;
1760 return true;
1761 }
1762 return false;
1763 }
1764
1765 // If range has a NAN with a known sign, set it in SIGNBIT and return
1766 // TRUE.
1767
1768 inline bool
1769 frange::nan_signbit_p (bool &signbit) const
1770 {
1771 if (undefined_p ())
1772 return false;
1773
1774 if (m_pos_nan == m_neg_nan)
1775 return false;
1776
1777 signbit = m_neg_nan;
1778 return true;
1779 }
1780
1781 void frange_nextafter (enum machine_mode, REAL_VALUE_TYPE &,
1782 const REAL_VALUE_TYPE &);
1783 void frange_arithmetic (enum tree_code, tree, REAL_VALUE_TYPE &,
1784 const REAL_VALUE_TYPE &, const REAL_VALUE_TYPE &,
1785 const REAL_VALUE_TYPE &);
1786
1787 // Return true if TYPE1 and TYPE2 are compatible range types.
1788
1789 inline bool
1790 range_compatible_p (tree type1, tree type2)
1791 {
1792 // types_compatible_p requires conversion in both directions to be useless.
1793 // GIMPLE only requires a cast one way in order to be compatible.
1794 // Ranges really only need the sign and precision to be the same.
1795 return (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1796 && TYPE_SIGN (type1) == TYPE_SIGN (type2));
1797 }
1798 #endif // GCC_VALUE_RANGE_H