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