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