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