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