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 : 4610988190 : 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 : 9568743 : bool operator!= (const vrange &r) const { return !(*this == r); }
113 : : void dump (FILE *) const;
114 : : protected:
115 : 5918759830 : 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 : 5680376436 : irange_bitmask () { /* uninitialized */ }
137 : 0 : irange_bitmask (unsigned prec) { set_unknown (prec); }
138 : : irange_bitmask (const wide_int &value, const wide_int &mask);
139 : : irange_bitmask (tree type, const wide_int &min, const wide_int &max);
140 : :
141 : 833679276 : wide_int value () const { return m_value; }
142 : 1014592219 : wide_int mask () const { return m_mask; }
143 : : void set_unknown (unsigned prec);
144 : : bool unknown_p () const;
145 : : unsigned get_precision () const;
146 : : void union_ (const irange_bitmask &src);
147 : : void intersect (const irange_bitmask &src);
148 : : bool operator== (const irange_bitmask &src) const;
149 : : bool operator!= (const irange_bitmask &src) const { return !(*this == src); }
150 : : void verify_mask () const;
151 : : void dump (FILE *) const;
152 : :
153 : : bool member_p (const wide_int &val) const;
154 : :
155 : : // Convenience functions for nonzero bitmask compatibility.
156 : : wide_int get_nonzero_bits () const;
157 : : void set_nonzero_bits (const wide_int &bits);
158 : : private:
159 : : wide_int m_value;
160 : : wide_int m_mask;
161 : : };
162 : :
163 : : inline void
164 : 2814594954 : irange_bitmask::set_unknown (unsigned prec)
165 : : {
166 : 2814594954 : m_value = wi::zero (prec);
167 : 2814594954 : m_mask = wi::minus_one (prec);
168 : 2814594954 : if (flag_checking)
169 : 2814582057 : verify_mask ();
170 : 2814594954 : }
171 : :
172 : : // Return TRUE if THIS does not have any meaningful information.
173 : :
174 : : inline bool
175 : 3771452677 : irange_bitmask::unknown_p () const
176 : : {
177 : 1767545405 : return m_mask == -1;
178 : : }
179 : :
180 : : inline
181 : 1167028557 : irange_bitmask::irange_bitmask (const wide_int &value, const wide_int &mask)
182 : : {
183 : 1167028557 : m_value = value;
184 : 1167028557 : m_mask = mask;
185 : 1167028557 : if (flag_checking)
186 : 1167024496 : verify_mask ();
187 : 1167028557 : }
188 : :
189 : : inline unsigned
190 : : irange_bitmask::get_precision () const
191 : : {
192 : : return m_mask.get_precision ();
193 : : }
194 : :
195 : : // The following two functions are meant for backwards compatability
196 : : // with the nonzero bitmask. A cleared bit means the value must be 0.
197 : : // A set bit means we have no information for the bit.
198 : :
199 : : // Return the nonzero bits.
200 : : inline wide_int
201 : 79797545 : irange_bitmask::get_nonzero_bits () const
202 : : {
203 : 79797545 : return m_value | m_mask;
204 : : }
205 : :
206 : : // Set the bitmask to the nonzero bits in BITS.
207 : : inline void
208 : 5323299 : irange_bitmask::set_nonzero_bits (const wide_int &bits)
209 : : {
210 : 5323299 : m_value = wi::zero (bits.get_precision ());
211 : 5323299 : m_mask = bits;
212 : 5323299 : if (flag_checking)
213 : 5323200 : verify_mask ();
214 : 5323299 : }
215 : :
216 : : // Return TRUE if val could be a valid value with this bitmask.
217 : :
218 : : inline bool
219 : 244801920 : irange_bitmask::member_p (const wide_int &val) const
220 : : {
221 : 244801920 : if (unknown_p ())
222 : : return true;
223 : 37274853 : wide_int res = m_mask & val;
224 : 37274853 : if (m_value != 0)
225 : 1103732 : res |= ~m_mask & m_value;
226 : 37274853 : return res == val;
227 : 37274853 : }
228 : :
229 : : inline bool
230 : 868501217 : irange_bitmask::operator== (const irange_bitmask &src) const
231 : : {
232 : 868501217 : bool unknown1 = unknown_p ();
233 : 868501217 : bool unknown2 = src.unknown_p ();
234 : 868501217 : if (unknown1 || unknown2)
235 : 676981905 : return unknown1 == unknown2;
236 : 191519312 : return m_value == src.m_value && m_mask == src.m_mask;
237 : : }
238 : :
239 : : inline void
240 : 21998085 : irange_bitmask::union_ (const irange_bitmask &src)
241 : : {
242 : 21998145 : m_mask = (m_mask | src.m_mask) | (m_value ^ src.m_value);
243 : 21998085 : m_value = m_value & src.m_value;
244 : 21998085 : if (flag_checking)
245 : 21997986 : verify_mask ();
246 : 21998085 : }
247 : :
248 : : inline void
249 : 546809477 : irange_bitmask::intersect (const irange_bitmask &src)
250 : : {
251 : : // If we have two known bits that are incompatible, the resulting
252 : : // bit is undefined. It is unclear whether we should set the entire
253 : : // range to UNDEFINED, or just a subset of it. For now, set the
254 : : // entire bitmask to unknown (VARYING).
255 : 1093620850 : if (wi::bit_and (~(m_mask | src.m_mask),
256 : 1640428431 : m_value ^ src.m_value) != 0)
257 : : {
258 : 6006 : unsigned prec = m_mask.get_precision ();
259 : 6006 : m_mask = wi::minus_one (prec);
260 : 6006 : m_value = wi::zero (prec);
261 : : }
262 : : else
263 : : {
264 : 546803471 : m_mask = m_mask & src.m_mask;
265 : 546804419 : m_value = m_value | src.m_value;
266 : : }
267 : 546809477 : if (flag_checking)
268 : 546807169 : verify_mask ();
269 : 546809477 : }
270 : :
271 : : // An integer range without any storage.
272 : :
273 : 9115488910 : class irange : public vrange
274 : : {
275 : : friend class irange_storage;
276 : : friend class vrange_printer;
277 : : public:
278 : : // In-place setters.
279 : : void set (tree type, const wide_int &, const wide_int &,
280 : : value_range_kind = VR_RANGE);
281 : : virtual void set_nonzero (tree type) override;
282 : : virtual void set_zero (tree type) override;
283 : : virtual void set_nonnegative (tree type) override;
284 : : virtual void set_varying (tree type) override;
285 : : virtual void set_undefined () override;
286 : :
287 : : // Range types.
288 : : static bool supports_p (const_tree type);
289 : : virtual bool supports_type_p (const_tree type) const override;
290 : : virtual tree type () const override;
291 : :
292 : : // Iteration over sub-ranges.
293 : : unsigned num_pairs () const;
294 : : wide_int lower_bound (unsigned = 0) const;
295 : : wide_int upper_bound (unsigned) const;
296 : : wide_int upper_bound () const;
297 : : virtual tree lbound () const override;
298 : : virtual tree ubound () const override;
299 : :
300 : : // Predicates.
301 : : virtual bool zero_p () const override;
302 : : virtual bool nonzero_p () const override;
303 : : virtual bool singleton_p (tree *result = NULL) const override;
304 : : bool singleton_p (wide_int &) const;
305 : : bool contains_p (const wide_int &) const;
306 : : bool nonnegative_p () const;
307 : : bool nonpositive_p () const;
308 : :
309 : : // In-place operators.
310 : : virtual bool union_ (const vrange &) override;
311 : : virtual bool intersect (const vrange &) override;
312 : : void invert ();
313 : :
314 : : // Operator overloads.
315 : : irange& operator= (const irange &);
316 : : bool operator== (const irange &) const;
317 : : bool operator!= (const irange &r) const { return !(*this == r); }
318 : :
319 : : // Misc methods.
320 : : virtual bool fits_p (const vrange &r) const override;
321 : : virtual void accept (const vrange_visitor &v) const override;
322 : :
323 : : virtual void update_bitmask (const class irange_bitmask &) override;
324 : : virtual irange_bitmask get_bitmask () const override;
325 : :
326 : : protected:
327 : : void maybe_resize (int needed);
328 : : virtual void set (tree, tree, value_range_kind = VR_RANGE) override;
329 : : virtual bool contains_p (tree cst) const override;
330 : : irange (wide_int *, unsigned nranges, bool resizable);
331 : :
332 : : // In-place operators.
333 : : bool irange_contains_p (const irange &) const;
334 : : bool irange_single_pair_union (const irange &r);
335 : :
336 : : void normalize_kind ();
337 : :
338 : : void verify_range ();
339 : :
340 : : // Hard limit on max ranges allowed.
341 : : static const int HARD_MAX_RANGES = 255;
342 : : private:
343 : : bool varying_compatible_p () const;
344 : : bool intersect_bitmask (const irange &r);
345 : : bool union_bitmask (const irange &r);
346 : : bool set_range_from_bitmask ();
347 : : bool snap_subranges ();
348 : : bool snap (const wide_int &, const wide_int &, wide_int &, wide_int &);
349 : :
350 : : bool intersect (const wide_int& lb, const wide_int& ub);
351 : : bool union_append (const irange &r);
352 : : unsigned char m_num_ranges;
353 : : bool m_resizable;
354 : : unsigned char m_max_ranges;
355 : : tree m_type;
356 : : irange_bitmask m_bitmask;
357 : : protected:
358 : : wide_int *m_base;
359 : : };
360 : :
361 : : // Here we describe an irange with N pairs of ranges. The storage for
362 : : // the pairs is embedded in the class as an array.
363 : : //
364 : : // If RESIZABLE is true, the storage will be resized on the heap when
365 : : // the number of ranges needed goes past N up to a max of
366 : : // HARD_MAX_RANGES. This new storage is freed upon destruction.
367 : :
368 : : template<unsigned N, bool RESIZABLE = false>
369 : : class int_range final : public irange
370 : : {
371 : : public:
372 : : int_range ();
373 : : int_range (tree type, const wide_int &, const wide_int &,
374 : : value_range_kind = VR_RANGE);
375 : : int_range (tree type);
376 : : int_range (const int_range &);
377 : : int_range (const irange &);
378 : : ~int_range () final override;
379 : : int_range& operator= (const int_range &);
380 : : protected:
381 : : int_range (tree, tree, value_range_kind = VR_RANGE);
382 : : private:
383 : : wide_int m_ranges[N*2];
384 : : };
385 : :
386 : : class prange final : public vrange
387 : : {
388 : : friend class prange_storage;
389 : : friend class vrange_printer;
390 : : public:
391 : : prange ();
392 : : prange (const prange &);
393 : : prange (tree type);
394 : : prange (tree type, const wide_int &, const wide_int &,
395 : : value_range_kind = VR_RANGE);
396 : : static bool supports_p (const_tree type);
397 : : virtual bool supports_type_p (const_tree type) const final override;
398 : : virtual void accept (const vrange_visitor &v) const final override;
399 : : virtual void set_undefined () final override;
400 : : virtual void set_varying (tree type) final override;
401 : : virtual void set_nonzero (tree type) final override;
402 : : virtual void set_zero (tree type) final override;
403 : : virtual void set_nonnegative (tree type) final override;
404 : : virtual bool contains_p (tree cst) const final override;
405 : : virtual bool fits_p (const vrange &v) const final override;
406 : : virtual bool singleton_p (tree *result = NULL) const final override;
407 : : virtual bool zero_p () const final override;
408 : : virtual bool nonzero_p () const final override;
409 : : virtual void set (tree, tree, value_range_kind = VR_RANGE) final override;
410 : : virtual tree type () const final override;
411 : : virtual bool union_ (const vrange &v) final override;
412 : : virtual bool intersect (const vrange &v) final override;
413 : : virtual tree lbound () const final override;
414 : : virtual tree ubound () const final override;
415 : :
416 : : prange& operator= (const prange &);
417 : : bool operator== (const prange &) const;
418 : : void set (tree type, const wide_int &, const wide_int &,
419 : : value_range_kind = VR_RANGE);
420 : : void invert ();
421 : : bool contains_p (const wide_int &) const;
422 : : wide_int lower_bound () const;
423 : : wide_int upper_bound () const;
424 : : void verify_range () const;
425 : : irange_bitmask get_bitmask () const final override;
426 : : void update_bitmask (const irange_bitmask &) final override;
427 : : protected:
428 : : bool varying_compatible_p () const;
429 : :
430 : : tree m_type;
431 : : wide_int m_min;
432 : : wide_int m_max;
433 : : irange_bitmask m_bitmask;
434 : : };
435 : :
436 : : // Unsupported temporaries may be created by ranger before it's known
437 : : // they're unsupported, or by vr_values::get_value_range.
438 : :
439 : 0 : class unsupported_range : public vrange
440 : : {
441 : : public:
442 : 73569059 : unsupported_range ()
443 : 73569059 : : vrange (VR_UNKNOWN)
444 : : {
445 : 73569059 : set_undefined ();
446 : : }
447 : 913422 : unsupported_range (const unsupported_range &src)
448 : 913422 : : vrange (VR_UNKNOWN)
449 : : {
450 : 913422 : unsupported_range::operator= (src);
451 : : }
452 : : void set (tree min, tree, value_range_kind = VR_RANGE) final override;
453 : : tree type () const final override;
454 : : bool supports_type_p (const_tree) const final override;
455 : : void set_varying (tree) final override;
456 : : void set_undefined () final override;
457 : : void accept (const vrange_visitor &v) const final override;
458 : : bool union_ (const vrange &r) final override;
459 : : bool intersect (const vrange &r) final override;
460 : : bool singleton_p (tree * = NULL) const final override;
461 : : bool contains_p (tree) const final override;
462 : : bool zero_p () const final override;
463 : : bool nonzero_p () const final override;
464 : : void set_nonzero (tree type) final override;
465 : : void set_zero (tree type) final override;
466 : : void set_nonnegative (tree type) final override;
467 : : bool fits_p (const vrange &) const final override;
468 : : unsupported_range& operator= (const unsupported_range &r);
469 : : tree lbound () const final override;
470 : : tree ubound () const final override;
471 : : };
472 : :
473 : : // The NAN state as an opaque object.
474 : :
475 : : class nan_state
476 : : {
477 : : public:
478 : : nan_state (bool);
479 : : nan_state (bool pos_nan, bool neg_nan);
480 : : bool neg_p () const;
481 : : bool pos_p () const;
482 : : private:
483 : : bool m_pos_nan;
484 : : bool m_neg_nan;
485 : : };
486 : :
487 : : // Set NAN state to +-NAN if NAN_P is true. Otherwise set NAN state
488 : : // to false.
489 : :
490 : : inline
491 : 65645392 : nan_state::nan_state (bool nan_p)
492 : : {
493 : 65645392 : m_pos_nan = nan_p;
494 : 61252563 : m_neg_nan = nan_p;
495 : : }
496 : :
497 : : // Constructor initializing the object to +NAN if POS_NAN is set, -NAN
498 : : // if NEG_NAN is set, or +-NAN if both are set. Otherwise POS_NAN and
499 : : // NEG_NAN are clear, and the object cannot be a NAN.
500 : :
501 : : inline
502 : 4032554 : nan_state::nan_state (bool pos_nan, bool neg_nan)
503 : : {
504 : 4032554 : m_pos_nan = pos_nan;
505 : 4013492 : m_neg_nan = neg_nan;
506 : : }
507 : :
508 : : // Return if +NAN is possible.
509 : :
510 : : inline bool
511 : 36477563 : nan_state::pos_p () const
512 : : {
513 : 36477539 : return m_pos_nan;
514 : : }
515 : :
516 : : // Return if -NAN is possible.
517 : :
518 : : inline bool
519 : 36338304 : nan_state::neg_p () const
520 : : {
521 : 36338280 : return m_neg_nan;
522 : : }
523 : :
524 : : // A floating point range.
525 : : //
526 : : // The representation is a type with a couple of endpoints, unioned
527 : : // with the set of { -NAN, +Nan }.
528 : :
529 : 51125357 : class frange final : public vrange
530 : : {
531 : : friend class frange_storage;
532 : : friend class vrange_printer;
533 : : public:
534 : : frange ();
535 : : frange (const frange &);
536 : : frange (tree, tree, value_range_kind = VR_RANGE);
537 : : frange (tree type);
538 : : frange (tree type, const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
539 : : value_range_kind = VR_RANGE);
540 : 635662739 : static bool supports_p (const_tree type)
541 : : {
542 : : // ?? Decimal floats can have multiple representations for the
543 : : // same number. Supporting them may be as simple as just
544 : : // disabling them in singleton_p. No clue.
545 : 635662739 : return SCALAR_FLOAT_TYPE_P (type) && !DECIMAL_FLOAT_TYPE_P (type);
546 : : }
547 : : virtual tree type () const override;
548 : : void set (tree type, const REAL_VALUE_TYPE &, const REAL_VALUE_TYPE &,
549 : : value_range_kind = VR_RANGE);
550 : : void set (tree type, const REAL_VALUE_TYPE &, const REAL_VALUE_TYPE &,
551 : : const nan_state &, value_range_kind = VR_RANGE);
552 : : void set_nan (tree type);
553 : : void set_nan (tree type, bool sign);
554 : : void set_nan (tree type, const nan_state &);
555 : : virtual void set_varying (tree type) override;
556 : : virtual void set_undefined () override;
557 : : virtual bool union_ (const vrange &) override;
558 : : virtual bool intersect (const vrange &) override;
559 : : bool contains_p (const REAL_VALUE_TYPE &) const;
560 : : virtual bool singleton_p (tree *result = NULL) const override;
561 : : bool singleton_p (REAL_VALUE_TYPE &r) const;
562 : : virtual bool supports_type_p (const_tree type) const override;
563 : : virtual void accept (const vrange_visitor &v) const override;
564 : : virtual bool zero_p () const override;
565 : : virtual bool nonzero_p () const override;
566 : : virtual void set_nonzero (tree type) override;
567 : : virtual void set_zero (tree type) override;
568 : : virtual void set_nonnegative (tree type) override;
569 : : virtual bool fits_p (const vrange &) const override;
570 : : frange& operator= (const frange &);
571 : : bool operator== (const frange &) const;
572 : 12 : bool operator!= (const frange &r) const { return !(*this == r); }
573 : : const REAL_VALUE_TYPE &lower_bound () const;
574 : : const REAL_VALUE_TYPE &upper_bound () const;
575 : : virtual tree lbound () const override;
576 : : virtual tree ubound () const override;
577 : : nan_state get_nan_state () const;
578 : : void update_nan ();
579 : : void update_nan (bool sign);
580 : : void update_nan (tree) = delete; // Disallow silent conversion to bool.
581 : : void update_nan (const nan_state &);
582 : : void clear_nan ();
583 : : void flush_denormals_to_zero ();
584 : :
585 : : // fpclassify like API
586 : : bool known_isfinite () const;
587 : : bool known_isnan () const;
588 : : bool known_isinf () const;
589 : : bool maybe_isnan () const;
590 : : bool maybe_isnan (bool sign) const;
591 : : bool maybe_isinf () const;
592 : : bool signbit_p (bool &signbit) const;
593 : : bool nan_signbit_p (bool &signbit) const;
594 : : bool known_isnormal () const;
595 : : bool known_isdenormal_or_zero () const;
596 : :
597 : : protected:
598 : : virtual bool contains_p (tree cst) const override;
599 : : virtual void set (tree, tree, value_range_kind = VR_RANGE) override;
600 : :
601 : : private:
602 : : bool internal_singleton_p (REAL_VALUE_TYPE * = NULL) const;
603 : : void verify_range ();
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 : 21654172 : frange::lower_bound () const
618 : : {
619 : 21654172 : gcc_checking_assert (!undefined_p () && !known_isnan ());
620 : 21654172 : return m_min;
621 : : }
622 : :
623 : : inline const REAL_VALUE_TYPE &
624 : 20301703 : frange::upper_bound () const
625 : : {
626 : 20301703 : gcc_checking_assert (!undefined_p () && !known_isnan ());
627 : 20301703 : return m_max;
628 : : }
629 : :
630 : : // Return the NAN state.
631 : :
632 : : inline nan_state
633 : 1909548 : frange::get_nan_state () const
634 : : {
635 : 1909548 : 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 : 3882162981 : is_a (const vrange &v)
652 : : {
653 : : // Reuse is_a <vrange> to implement the const version.
654 : 4057883251 : const T &derived = static_cast<const T &> (v);
655 : 1825296168 : return is_a <T> (const_cast<T &> (derived));
656 : : }
657 : :
658 : : template <typename T>
659 : : inline T &
660 : 2203438502 : as_a (vrange &v)
661 : : {
662 : 0 : gcc_checking_assert (is_a <T> (v));
663 : 2203438502 : return static_cast <T &> (v);
664 : : }
665 : :
666 : : template <typename T>
667 : : inline const T &
668 : 3292349289 : as_a (const vrange &v)
669 : : {
670 : 0 : gcc_checking_assert (is_a <T> (v));
671 : 3292349289 : return static_cast <const T &> (v);
672 : : }
673 : :
674 : : // Specializations for the different range types.
675 : :
676 : : template <>
677 : : inline bool
678 : 6607090602 : is_a <irange> (vrange &v)
679 : : {
680 : 6485584699 : return v.m_discriminator == VR_IRANGE;
681 : : }
682 : :
683 : : template <>
684 : : inline bool
685 : 910068850 : is_a <prange> (vrange &v)
686 : : {
687 : 910068850 : return v.m_discriminator == VR_PRANGE;
688 : : }
689 : :
690 : : template <>
691 : : inline bool
692 : 192549243 : is_a <frange> (vrange &v)
693 : : {
694 : 192549243 : return v.m_discriminator == VR_FRANGE;
695 : : }
696 : :
697 : : template <>
698 : : inline bool
699 : 913422 : is_a <unsupported_range> (vrange &v)
700 : : {
701 : 913422 : 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 : 1188207772 : irange::maybe_resize (int needed)
709 : : {
710 : 1188207772 : if (!m_resizable || m_max_ranges == HARD_MAX_RANGES)
711 : : return;
712 : :
713 : 809750649 : if (needed > m_max_ranges)
714 : : {
715 : 45302076 : m_max_ranges = HARD_MAX_RANGES;
716 : 23149360836 : wide_int *newmem = new wide_int[m_max_ranges * 2];
717 : 45302076 : unsigned n = num_pairs () * 2;
718 : 266227798 : for (unsigned i = 0; i < n; ++i)
719 : 220925722 : newmem[i] = m_base[i];
720 : 45302076 : m_base = newmem;
721 : : }
722 : : }
723 : :
724 : : template<unsigned N, bool RESIZABLE>
725 : : inline
726 : 4557744455 : int_range<N, RESIZABLE>::~int_range ()
727 : : {
728 : 3388496153 : if (RESIZABLE && m_base != m_ranges)
729 : 23149360836 : delete[] m_base;
730 : 28107117679 : }
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 : 10741 : 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 : 2731766 : tree type () { return m_vrange->type (); }
781 : 146799971 : bool varying_p () const { return m_vrange->varying_p (); }
782 : 244063541 : bool undefined_p () const { return m_vrange->undefined_p (); }
783 : 289297034 : void set_varying (tree type) { init (type); m_vrange->set_varying (type); }
784 : 18146203 : void set_undefined () { m_vrange->set_undefined (); }
785 : 11509843 : bool union_ (const vrange &r) { return m_vrange->union_ (r); }
786 : 108989598 : bool intersect (const vrange &r) { return m_vrange->intersect (r); }
787 : 1094639 : bool contains_p (tree cst) const { return m_vrange->contains_p (cst); }
788 : 308541274 : bool singleton_p (tree *result = NULL) const
789 : 308541274 : { 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 : 239154 : bool nonzero_p () const { return m_vrange->nonzero_p (); }
794 : 2433 : bool zero_p () const { return m_vrange->zero_p (); }
795 : 10525631 : tree lbound () const { return m_vrange->lbound (); }
796 : 328676 : tree ubound () const { return m_vrange->ubound (); }
797 : 469122 : irange_bitmask get_bitmask () const { return m_vrange->get_bitmask (); }
798 : 1345360 : void update_bitmask (const class irange_bitmask &bm)
799 : 1345360 : { return m_vrange->update_bitmask (bm); }
800 : 3261 : void accept (const vrange_visitor &v) const { m_vrange->accept (v); }
801 : : private:
802 : : void init (tree type);
803 : : void init (const vrange &);
804 : :
805 : : vrange *m_vrange;
806 : : union buffer_type {
807 : : int_range_max ints;
808 : : frange floats;
809 : : unsupported_range unsupported;
810 : : prange pointers;
811 : 6468716754 : buffer_type () { }
812 : 6585533562 : ~buffer_type () { }
813 : : } m_buffer;
814 : : };
815 : :
816 : : // The default constructor is uninitialized and must be initialized
817 : : // with either set_type() or with an assignment into it.
818 : :
819 : : inline
820 : 3674342423 : value_range::value_range ()
821 : 3674342423 : : m_buffer ()
822 : : {
823 : 3674342423 : m_vrange = NULL;
824 : : }
825 : :
826 : : // Copy constructor.
827 : :
828 : : inline
829 : 11506851 : value_range::value_range (const value_range &r)
830 : : {
831 : 11506851 : init (*r.m_vrange);
832 : : }
833 : :
834 : : // Copy constructor from a vrange.
835 : :
836 : : inline
837 : 76756657 : value_range::value_range (const vrange &r)
838 : : {
839 : 76756657 : init (r);
840 : : }
841 : :
842 : : // Construct an UNDEFINED range that can hold ranges of TYPE. If TYPE
843 : : // is not supported, default to unsupported_range.
844 : :
845 : : inline
846 : 2706110808 : value_range::value_range (tree type)
847 : : {
848 : 2544476351 : init (type);
849 : 4711212 : }
850 : :
851 : : // Construct a range that can hold a range of [MIN, MAX], where MIN
852 : : // and MAX are trees.
853 : :
854 : : inline
855 : 15 : value_range::value_range (tree min, tree max, value_range_kind kind)
856 : : {
857 : 15 : init (TREE_TYPE (min));
858 : 15 : m_vrange->set (min, max, kind);
859 : 15 : }
860 : :
861 : : inline
862 : 6585533562 : value_range::~value_range ()
863 : : {
864 : 6585533562 : if (m_vrange)
865 : 3036218211 : m_vrange->~vrange ();
866 : 6585533562 : }
867 : :
868 : : // Initialize object to an UNDEFINED range that can hold ranges of
869 : : // TYPE. Clean-up memory if there was a previous object.
870 : :
871 : : inline void
872 : 87694177 : value_range::set_type (tree type)
873 : : {
874 : 87694177 : if (m_vrange)
875 : 8445018 : m_vrange->~vrange ();
876 : 87694177 : init (type);
877 : 87694177 : }
878 : :
879 : : // Initialize object to an UNDEFINED range that can hold ranges of
880 : : // TYPE.
881 : :
882 : : inline void
883 : 3083102034 : value_range::init (tree type)
884 : : {
885 : 3083102034 : gcc_checking_assert (TYPE_P (type));
886 : :
887 : 3083102034 : if (irange::supports_p (type))
888 : 2171872205 : m_vrange = new (&m_buffer.ints) int_range_max ();
889 : 911229829 : else if (prange::supports_p (type))
890 : 727526473 : m_vrange = new (&m_buffer.pointers) prange ();
891 : 183703356 : else if (frange::supports_p (type))
892 : 110134297 : m_vrange = new (&m_buffer.floats) frange ();
893 : : else
894 : 73569059 : m_vrange = new (&m_buffer.unsupported) unsupported_range ();
895 : 3083102034 : }
896 : :
897 : : // Initialize object with a copy of R.
898 : :
899 : : inline void
900 : 121074025 : value_range::init (const vrange &r)
901 : : {
902 : 121074025 : if (is_a <irange> (r))
903 : 70617523 : m_vrange = new (&m_buffer.ints) int_range_max (as_a <irange> (r));
904 : 50456502 : else if (is_a <prange> (r))
905 : 98740996 : m_vrange = new (&m_buffer.pointers) prange (as_a <prange> (r));
906 : 1086004 : else if (is_a <frange> (r))
907 : 345164 : m_vrange = new (&m_buffer.floats) frange (as_a <frange> (r));
908 : : else
909 : 1826844 : m_vrange = new (&m_buffer.unsupported)
910 : 913422 : unsupported_range (as_a <unsupported_range> (r));
911 : 121074025 : }
912 : :
913 : : // Assignment operator. Copying incompatible types is allowed. That
914 : : // is, assigning an frange to an object holding an irange does the
915 : : // right thing.
916 : :
917 : : inline vrange &
918 : 32810517 : value_range::operator= (const vrange &r)
919 : : {
920 : 32810517 : if (m_vrange)
921 : 9041569 : m_vrange->~vrange ();
922 : 32810517 : init (r);
923 : 32810517 : return *m_vrange;
924 : : }
925 : :
926 : : inline value_range &
927 : 9041562 : value_range::operator= (const value_range &r)
928 : : {
929 : : // No need to call the m_vrange destructor here, as we will do so in
930 : : // the assignment below.
931 : 1013703 : *this = *r.m_vrange;
932 : 9041562 : return *this;
933 : : }
934 : :
935 : : inline bool
936 : 24437531 : value_range::operator== (const value_range &r) const
937 : : {
938 : 24437531 : return *m_vrange == *r.m_vrange;
939 : : }
940 : :
941 : : inline bool
942 : 9111365 : value_range::operator!= (const value_range &r) const
943 : : {
944 : 9111365 : return *m_vrange != *r.m_vrange;
945 : : }
946 : :
947 : : inline
948 : 4299923168 : value_range::operator vrange &()
949 : : {
950 : 4025768490 : return *m_vrange;
951 : : }
952 : :
953 : : inline
954 : 44093851 : value_range::operator const vrange &() const
955 : : {
956 : 44093851 : return *m_vrange;
957 : : }
958 : :
959 : : // Return TRUE if TYPE is supported by the vrange infrastructure.
960 : :
961 : : inline bool
962 : 5532953653 : value_range::supports_type_p (const_tree type)
963 : : {
964 : 5532953653 : return irange::supports_p (type)
965 : 1489727544 : || prange::supports_p (type)
966 : 251495379 : || frange::supports_p (type);
967 : : }
968 : :
969 : : extern value_range_kind get_legacy_range (const vrange &, tree &min, tree &max);
970 : : extern void dump_value_range (FILE *, const vrange *);
971 : : extern bool vrp_operand_equal_p (const_tree, const_tree);
972 : : inline REAL_VALUE_TYPE frange_val_min (const_tree type);
973 : : inline REAL_VALUE_TYPE frange_val_max (const_tree type);
974 : :
975 : : // Number of sub-ranges in a range.
976 : :
977 : : inline unsigned
978 : 27604732992 : irange::num_pairs () const
979 : : {
980 : 8520836838 : return m_num_ranges;
981 : : }
982 : :
983 : : inline tree
984 : 10280138134 : irange::type () const
985 : : {
986 : 10280138134 : gcc_checking_assert (m_num_ranges > 0);
987 : 10280138134 : return m_type;
988 : : }
989 : :
990 : : inline bool
991 : 4190288522 : irange::varying_compatible_p () const
992 : : {
993 : 4190288522 : if (m_num_ranges != 1)
994 : : return false;
995 : :
996 : 3221110221 : const wide_int &l = m_base[0];
997 : 3221110221 : const wide_int &u = m_base[1];
998 : 3221110221 : tree t = m_type;
999 : :
1000 : 3221110221 : if (m_kind == VR_VARYING)
1001 : : return true;
1002 : :
1003 : 2941099360 : unsigned prec = TYPE_PRECISION (t);
1004 : 2941099360 : signop sign = TYPE_SIGN (t);
1005 : 2941099360 : if (INTEGRAL_TYPE_P (t) || POINTER_TYPE_P (t))
1006 : 5882198720 : return (l == wi::min_value (prec, sign)
1007 : 3890760401 : && u == wi::max_value (prec, sign)
1008 : 2984828166 : && m_bitmask.unknown_p ());
1009 : : return true;
1010 : : }
1011 : :
1012 : : inline bool
1013 : 953894516 : vrange::varying_p () const
1014 : : {
1015 : 950410154 : return m_kind == VR_VARYING;
1016 : : }
1017 : :
1018 : : inline bool
1019 : 16545282507 : vrange::undefined_p () const
1020 : : {
1021 : 9641492377 : return m_kind == VR_UNDEFINED;
1022 : : }
1023 : :
1024 : : inline bool
1025 : 199618013 : irange::zero_p () const
1026 : : {
1027 : 188943339 : return (m_kind == VR_RANGE && m_num_ranges == 1
1028 : 367949662 : && lower_bound (0) == 0
1029 : 388561370 : && upper_bound (0) == 0);
1030 : : }
1031 : :
1032 : : inline bool
1033 : 8 : irange::nonzero_p () const
1034 : : {
1035 : 8 : if (undefined_p ())
1036 : : return false;
1037 : :
1038 : 8 : wide_int zero = wi::zero (TYPE_PRECISION (type ()));
1039 : 8 : return *this == int_range<2> (type (), zero, zero, VR_ANTI_RANGE);
1040 : 8 : }
1041 : :
1042 : : inline bool
1043 : 13469832330 : irange::supports_p (const_tree type)
1044 : : {
1045 : 12908998702 : return INTEGRAL_TYPE_P (type);
1046 : : }
1047 : :
1048 : : inline bool
1049 : 64284098 : irange::contains_p (tree cst) const
1050 : : {
1051 : 64284098 : return contains_p (wi::to_wide (cst));
1052 : : }
1053 : :
1054 : : inline bool
1055 : 32971779 : range_includes_zero_p (const vrange &vr)
1056 : : {
1057 : 32971779 : if (vr.undefined_p ())
1058 : : return false;
1059 : :
1060 : 32971779 : if (vr.varying_p ())
1061 : : return true;
1062 : :
1063 : 25076039 : return vr.contains_p (build_zero_cst (vr.type ()));
1064 : : }
1065 : :
1066 : : // Constructors for irange
1067 : :
1068 : : inline
1069 : 4664254798 : irange::irange (wide_int *base, unsigned nranges, bool resizable)
1070 : : : vrange (VR_IRANGE),
1071 : 4664254798 : m_resizable (resizable),
1072 : 4664254798 : m_max_ranges (nranges)
1073 : : {
1074 : 4664254798 : m_base = base;
1075 : 4664254798 : set_undefined ();
1076 : : }
1077 : :
1078 : : // Constructors for int_range<>.
1079 : :
1080 : : template<unsigned N, bool RESIZABLE>
1081 : : inline
1082 : 3494805788 : int_range<N, RESIZABLE>::int_range ()
1083 : 23788898204 : : irange (m_ranges, N, RESIZABLE)
1084 : : {
1085 : 3494805788 : }
1086 : :
1087 : : template<unsigned N, bool RESIZABLE>
1088 : 148218 : int_range<N, RESIZABLE>::int_range (const int_range &other)
1089 : 1037518 : : irange (m_ranges, N, RESIZABLE)
1090 : : {
1091 : 148218 : irange::operator= (other);
1092 : 148218 : }
1093 : :
1094 : : template<unsigned N, bool RESIZABLE>
1095 : : int_range<N, RESIZABLE>::int_range (tree min, tree max, value_range_kind kind)
1096 : : : irange (m_ranges, N, RESIZABLE)
1097 : : {
1098 : : irange::set (min, max, kind);
1099 : : }
1100 : :
1101 : : template<unsigned N, bool RESIZABLE>
1102 : 147510037 : int_range<N, RESIZABLE>::int_range (tree type)
1103 : 580066923 : : irange (m_ranges, N, RESIZABLE)
1104 : : {
1105 : 147510037 : set_varying (type);
1106 : 147510037 : }
1107 : :
1108 : : template<unsigned N, bool RESIZABLE>
1109 : 675461952 : int_range<N, RESIZABLE>::int_range (tree type, const wide_int &wmin, const wide_int &wmax,
1110 : : value_range_kind kind)
1111 : 2226646018 : : irange (m_ranges, N, RESIZABLE)
1112 : : {
1113 : 675461952 : set (type, wmin, wmax, kind);
1114 : 675461952 : }
1115 : :
1116 : : template<unsigned N, bool RESIZABLE>
1117 : 346328803 : int_range<N, RESIZABLE>::int_range (const irange &other)
1118 : 2210739341 : : irange (m_ranges, N, RESIZABLE)
1119 : : {
1120 : 346328803 : irange::operator= (other);
1121 : 346328803 : }
1122 : :
1123 : : template<unsigned N, bool RESIZABLE>
1124 : : int_range<N, RESIZABLE>&
1125 : 84299856 : int_range<N, RESIZABLE>::operator= (const int_range &src)
1126 : : {
1127 : 84292314 : irange::operator= (src);
1128 : 0 : return *this;
1129 : : }
1130 : :
1131 : : inline void
1132 : 5000950540 : irange::set_undefined ()
1133 : : {
1134 : 5000950540 : m_kind = VR_UNDEFINED;
1135 : 4670326402 : m_num_ranges = 0;
1136 : 336695738 : }
1137 : :
1138 : : inline void
1139 : 1146656462 : irange::set_varying (tree type)
1140 : : {
1141 : 1146656462 : m_kind = VR_VARYING;
1142 : 1146656462 : m_num_ranges = 1;
1143 : 1146656462 : m_bitmask.set_unknown (TYPE_PRECISION (type));
1144 : :
1145 : 1146656462 : if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1146 : : {
1147 : 1146656462 : m_type = type;
1148 : : // Strict enum's require varying to be not TYPE_MIN/MAX, but rather
1149 : : // min_value and max_value.
1150 : 1146656462 : m_base[0] = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1151 : 1146667615 : m_base[1] = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1152 : : }
1153 : : else
1154 : 0 : m_type = error_mark_node;
1155 : 1146656462 : }
1156 : :
1157 : : // Return the lower bound of a sub-range. PAIR is the sub-range in
1158 : : // question.
1159 : :
1160 : : inline wide_int
1161 : 9511868283 : irange::lower_bound (unsigned pair) const
1162 : : {
1163 : 9511868283 : gcc_checking_assert (m_num_ranges > 0);
1164 : 9511868283 : gcc_checking_assert (pair + 1 <= num_pairs ());
1165 : 9511868283 : return m_base[pair * 2];
1166 : : }
1167 : :
1168 : : // Return the upper bound of a sub-range. PAIR is the sub-range in
1169 : : // question.
1170 : :
1171 : : inline wide_int
1172 : 10762825109 : irange::upper_bound (unsigned pair) const
1173 : : {
1174 : 10762825109 : gcc_checking_assert (m_num_ranges > 0);
1175 : 10762825109 : gcc_checking_assert (pair + 1 <= num_pairs ());
1176 : 10762825109 : return m_base[pair * 2 + 1];
1177 : : }
1178 : :
1179 : : // Return the highest bound of a range.
1180 : :
1181 : : inline wide_int
1182 : 2951674805 : irange::upper_bound () const
1183 : : {
1184 : 2951674805 : unsigned pairs = num_pairs ();
1185 : 2951674805 : gcc_checking_assert (pairs > 0);
1186 : 2951674805 : return upper_bound (pairs - 1);
1187 : : }
1188 : :
1189 : : // Set value range VR to a nonzero range of type TYPE.
1190 : :
1191 : : inline void
1192 : 3461898 : irange::set_nonzero (tree type)
1193 : : {
1194 : 3461898 : unsigned prec = TYPE_PRECISION (type);
1195 : :
1196 : 3461898 : if (TYPE_UNSIGNED (type))
1197 : : {
1198 : 3134241 : m_type = type;
1199 : 3134241 : m_kind = VR_RANGE;
1200 : 3134241 : m_base[0] = wi::one (prec);
1201 : 3134241 : m_base[1] = wi::minus_one (prec);
1202 : 3134241 : m_bitmask.set_unknown (prec);
1203 : 3134241 : m_num_ranges = 1;
1204 : :
1205 : 3134241 : if (flag_checking)
1206 : 3134241 : verify_range ();
1207 : : }
1208 : : else
1209 : : {
1210 : 327657 : wide_int zero = wi::zero (prec);
1211 : 327657 : set (type, zero, zero, VR_ANTI_RANGE);
1212 : 327657 : }
1213 : 3461898 : }
1214 : :
1215 : : // Set value range VR to a ZERO range of type TYPE.
1216 : :
1217 : : inline void
1218 : 8531110 : irange::set_zero (tree type)
1219 : : {
1220 : 8531110 : wide_int zero = wi::zero (TYPE_PRECISION (type));
1221 : 8531110 : set (type, zero, zero);
1222 : 8531110 : }
1223 : :
1224 : : // Normalize a range to VARYING or UNDEFINED if possible.
1225 : :
1226 : : inline void
1227 : 395577450 : irange::normalize_kind ()
1228 : : {
1229 : 395577450 : if (m_num_ranges == 0)
1230 : 1 : set_undefined ();
1231 : 395577449 : else if (varying_compatible_p ())
1232 : : {
1233 : 26806797 : if (m_kind == VR_RANGE)
1234 : 6564180 : m_kind = VR_VARYING;
1235 : 20242617 : else if (m_kind == VR_ANTI_RANGE)
1236 : 0 : set_undefined ();
1237 : : }
1238 : 395577450 : if (flag_checking)
1239 : 395576890 : verify_range ();
1240 : 395577450 : }
1241 : :
1242 : : inline bool
1243 : 30730967 : contains_zero_p (const irange &r)
1244 : : {
1245 : 30730967 : if (r.undefined_p ())
1246 : : return false;
1247 : :
1248 : 30730967 : wide_int zero = wi::zero (TYPE_PRECISION (r.type ()));
1249 : 30730967 : return r.contains_p (zero);
1250 : 30730967 : }
1251 : :
1252 : : inline wide_int
1253 : 81176581 : irange_val_min (const_tree type)
1254 : : {
1255 : 81176581 : gcc_checking_assert (irange::supports_p (type));
1256 : 81176581 : return wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1257 : : }
1258 : :
1259 : : inline wide_int
1260 : 78332382 : irange_val_max (const_tree type)
1261 : : {
1262 : 78332382 : gcc_checking_assert (irange::supports_p (type));
1263 : 78332382 : return wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
1264 : : }
1265 : :
1266 : : inline
1267 : 886152126 : prange::prange ()
1268 : 886152126 : : vrange (VR_PRANGE)
1269 : : {
1270 : 886152126 : set_undefined ();
1271 : : }
1272 : :
1273 : : inline
1274 : 117276606 : prange::prange (const prange &r)
1275 : 117276606 : : vrange (VR_PRANGE)
1276 : : {
1277 : 117276606 : *this = r;
1278 : : }
1279 : :
1280 : : inline
1281 : 9573246 : prange::prange (tree type)
1282 : 9573246 : : vrange (VR_PRANGE)
1283 : : {
1284 : 9573246 : set_varying (type);
1285 : : }
1286 : :
1287 : : inline
1288 : 3040588 : prange::prange (tree type, const wide_int &lb, const wide_int &ub,
1289 : 3040588 : value_range_kind kind)
1290 : 3040588 : : vrange (VR_PRANGE)
1291 : : {
1292 : 3040588 : set (type, lb, ub, kind);
1293 : : }
1294 : :
1295 : : inline bool
1296 : 3381231447 : prange::supports_p (const_tree type)
1297 : : {
1298 : 3381231447 : return POINTER_TYPE_P (type);
1299 : : }
1300 : :
1301 : : inline bool
1302 : 400289725 : prange::supports_type_p (const_tree type) const
1303 : : {
1304 : 400289725 : return POINTER_TYPE_P (type);
1305 : : }
1306 : :
1307 : : inline void
1308 : 1013927079 : prange::set_undefined ()
1309 : : {
1310 : 1012864960 : m_kind = VR_UNDEFINED;
1311 : 127400213 : }
1312 : :
1313 : : inline void
1314 : 419467945 : prange::set_varying (tree type)
1315 : : {
1316 : 419467945 : m_kind = VR_VARYING;
1317 : 419467945 : m_type = type;
1318 : 419467945 : m_min = wi::zero (TYPE_PRECISION (type));
1319 : 419467945 : m_max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
1320 : 419467945 : m_bitmask.set_unknown (TYPE_PRECISION (type));
1321 : :
1322 : 419467945 : if (flag_checking)
1323 : 419467921 : verify_range ();
1324 : 419467945 : }
1325 : :
1326 : : inline void
1327 : 64437642 : prange::set_nonzero (tree type)
1328 : : {
1329 : 64437642 : m_kind = VR_RANGE;
1330 : 64437642 : m_type = type;
1331 : 64437642 : m_min = wi::one (TYPE_PRECISION (type));
1332 : 64437642 : m_max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
1333 : 64437642 : m_bitmask.set_unknown (TYPE_PRECISION (type));
1334 : :
1335 : 64437642 : if (flag_checking)
1336 : 64437519 : verify_range ();
1337 : 64437642 : }
1338 : :
1339 : : inline void
1340 : 1564 : prange::set_zero (tree type)
1341 : : {
1342 : 1564 : m_kind = VR_RANGE;
1343 : 1564 : m_type = type;
1344 : 1564 : wide_int zero = wi::zero (TYPE_PRECISION (type));
1345 : 1564 : m_min = m_max = zero;
1346 : 1564 : m_bitmask = irange_bitmask (zero, zero);
1347 : :
1348 : 1564 : if (flag_checking)
1349 : 1564 : verify_range ();
1350 : 1564 : }
1351 : :
1352 : : inline bool
1353 : 440752 : prange::contains_p (tree cst) const
1354 : : {
1355 : 440752 : return contains_p (wi::to_wide (cst));
1356 : : }
1357 : :
1358 : : inline bool
1359 : 3351096 : prange::zero_p () const
1360 : : {
1361 : 3351096 : return m_kind == VR_RANGE && m_min == 0 && m_max == 0;
1362 : : }
1363 : :
1364 : : inline bool
1365 : 51891981 : prange::nonzero_p () const
1366 : : {
1367 : 51891981 : return m_kind == VR_RANGE && m_min == 1 && m_max == -1;
1368 : : }
1369 : :
1370 : : inline tree
1371 : 1809845334 : prange::type () const
1372 : : {
1373 : 1809845334 : gcc_checking_assert (!undefined_p ());
1374 : 1809845334 : return m_type;
1375 : : }
1376 : :
1377 : : inline wide_int
1378 : 329934683 : prange::lower_bound () const
1379 : : {
1380 : 329934683 : gcc_checking_assert (!undefined_p ());
1381 : 329934683 : return m_min;
1382 : : }
1383 : :
1384 : : inline wide_int
1385 : 321173637 : prange::upper_bound () const
1386 : : {
1387 : 321173637 : gcc_checking_assert (!undefined_p ());
1388 : 321173637 : return m_max;
1389 : : }
1390 : :
1391 : : inline bool
1392 : 1028526972 : prange::varying_compatible_p () const
1393 : : {
1394 : 1028526972 : return (!undefined_p ()
1395 : 1495150535 : && m_min == 0 && m_max == -1 && get_bitmask ().unknown_p ());
1396 : : }
1397 : :
1398 : : inline irange_bitmask
1399 : 514860911 : prange::get_bitmask () const
1400 : : {
1401 : 514860911 : return m_bitmask;
1402 : : }
1403 : :
1404 : : inline bool
1405 : 0 : prange::fits_p (const vrange &) const
1406 : : {
1407 : 0 : return true;
1408 : : }
1409 : :
1410 : :
1411 : : inline
1412 : 112811008 : frange::frange ()
1413 : 112811008 : : vrange (VR_FRANGE)
1414 : : {
1415 : 112810972 : set_undefined ();
1416 : : }
1417 : :
1418 : : inline
1419 : 2942176 : frange::frange (const frange &src)
1420 : 2942176 : : vrange (VR_FRANGE)
1421 : : {
1422 : 2512845 : *this = src;
1423 : : }
1424 : :
1425 : : inline
1426 : 761101 : frange::frange (tree type)
1427 : 761101 : : vrange (VR_FRANGE)
1428 : : {
1429 : 761101 : set_varying (type);
1430 : : }
1431 : :
1432 : : // frange constructor from REAL_VALUE_TYPE endpoints.
1433 : :
1434 : : inline
1435 : 47465700 : frange::frange (tree type,
1436 : : const REAL_VALUE_TYPE &min, const REAL_VALUE_TYPE &max,
1437 : 47465192 : value_range_kind kind)
1438 : 47465700 : : vrange (VR_FRANGE)
1439 : : {
1440 : 47465700 : set (type, min, max, kind);
1441 : : }
1442 : :
1443 : : // frange constructor from trees.
1444 : :
1445 : : inline
1446 : : frange::frange (tree min, tree max, value_range_kind kind)
1447 : : : vrange (VR_FRANGE)
1448 : : {
1449 : : set (min, max, kind);
1450 : : }
1451 : :
1452 : : inline tree
1453 : 74023347 : frange::type () const
1454 : : {
1455 : 74023347 : gcc_checking_assert (!undefined_p ());
1456 : 74023347 : return m_type;
1457 : : }
1458 : :
1459 : : inline void
1460 : 68698991 : frange::set_varying (tree type)
1461 : : {
1462 : 68698991 : m_kind = VR_VARYING;
1463 : 68698991 : m_type = type;
1464 : 68698991 : m_min = frange_val_min (type);
1465 : 68698991 : m_max = frange_val_max (type);
1466 : 68698991 : if (HONOR_NANS (m_type))
1467 : : {
1468 : 64752490 : m_pos_nan = true;
1469 : 64752490 : m_neg_nan = true;
1470 : : }
1471 : : else
1472 : : {
1473 : 3946501 : m_pos_nan = false;
1474 : 3946501 : m_neg_nan = false;
1475 : : }
1476 : 68698991 : }
1477 : :
1478 : : inline void
1479 : 130469831 : frange::set_undefined ()
1480 : : {
1481 : 130469831 : m_kind = VR_UNDEFINED;
1482 : 130469831 : m_type = NULL;
1483 : 130469831 : m_pos_nan = false;
1484 : 130469831 : m_neg_nan = false;
1485 : : // m_min and m_min are uninitialized as they are REAL_VALUE_TYPE ??.
1486 : 130469831 : if (flag_checking)
1487 : 130469831 : verify_range ();
1488 : 130469831 : }
1489 : :
1490 : : // Set the NAN bits to NAN and adjust the range.
1491 : :
1492 : : inline void
1493 : 6311108 : frange::update_nan (const nan_state &nan)
1494 : : {
1495 : 6311108 : gcc_checking_assert (!undefined_p ());
1496 : 6311108 : if (HONOR_NANS (m_type))
1497 : : {
1498 : 6310604 : m_pos_nan = nan.pos_p ();
1499 : 6310604 : m_neg_nan = nan.neg_p ();
1500 : 6310604 : normalize_kind ();
1501 : 6310604 : if (flag_checking)
1502 : 6310604 : verify_range ();
1503 : : }
1504 : 6311108 : }
1505 : :
1506 : : // Set the NAN bit to +-NAN.
1507 : :
1508 : : inline void
1509 : 4353945 : frange::update_nan ()
1510 : : {
1511 : 4353945 : gcc_checking_assert (!undefined_p ());
1512 : 4353945 : nan_state nan (true);
1513 : 4353945 : update_nan (nan);
1514 : 4353945 : }
1515 : :
1516 : : // Like above, but set the sign of the NAN.
1517 : :
1518 : : inline void
1519 : 1957163 : frange::update_nan (bool sign)
1520 : : {
1521 : 1957163 : gcc_checking_assert (!undefined_p ());
1522 : 1957163 : nan_state nan (/*pos=*/!sign, /*neg=*/sign);
1523 : 1957163 : update_nan (nan);
1524 : 1957163 : }
1525 : :
1526 : : inline bool
1527 : 0 : frange::contains_p (tree cst) const
1528 : : {
1529 : 0 : return contains_p (*TREE_REAL_CST_PTR (cst));
1530 : : }
1531 : :
1532 : : // Clear the NAN bit and adjust the range.
1533 : :
1534 : : inline void
1535 : 14411097 : frange::clear_nan ()
1536 : : {
1537 : 14411097 : gcc_checking_assert (!undefined_p ());
1538 : 14411097 : m_pos_nan = false;
1539 : 14411097 : m_neg_nan = false;
1540 : 14411097 : normalize_kind ();
1541 : 14411097 : if (flag_checking)
1542 : 14411097 : verify_range ();
1543 : 14411097 : }
1544 : :
1545 : : // Set R to maximum representable value for TYPE.
1546 : :
1547 : : inline REAL_VALUE_TYPE
1548 : 23317935 : real_max_representable (const_tree type)
1549 : : {
1550 : 23317935 : REAL_VALUE_TYPE r;
1551 : 23317935 : char buf[128];
1552 : 23317935 : get_max_float (REAL_MODE_FORMAT (TYPE_MODE (type)),
1553 : : buf, sizeof (buf), false);
1554 : 23317935 : int res = real_from_string (&r, buf);
1555 : 23317935 : gcc_checking_assert (!res);
1556 : 23317935 : return r;
1557 : : }
1558 : :
1559 : : // Return the minimum representable value for TYPE.
1560 : :
1561 : : inline REAL_VALUE_TYPE
1562 : 11982239 : real_min_representable (const_tree type)
1563 : : {
1564 : 11982239 : REAL_VALUE_TYPE r = real_max_representable (type);
1565 : 11982239 : r = real_value_negate (&r);
1566 : 11982239 : return r;
1567 : : }
1568 : :
1569 : : // Return the minimum value for TYPE.
1570 : :
1571 : : inline REAL_VALUE_TYPE
1572 : 178939672 : frange_val_min (const_tree type)
1573 : : {
1574 : 178939672 : if (HONOR_INFINITIES (type))
1575 : 168439649 : return dconstninf;
1576 : : else
1577 : 10500023 : return real_min_representable (type);
1578 : : }
1579 : :
1580 : : // Return the maximum value for TYPE.
1581 : :
1582 : : inline REAL_VALUE_TYPE
1583 : 126137835 : frange_val_max (const_tree type)
1584 : : {
1585 : 126137835 : if (HONOR_INFINITIES (type))
1586 : 116365880 : return dconstinf;
1587 : : else
1588 : 9771955 : return real_max_representable (type);
1589 : : }
1590 : :
1591 : : // Return TRUE if R is the minimum value for TYPE.
1592 : :
1593 : : inline bool
1594 : 107169760 : frange_val_is_min (const REAL_VALUE_TYPE &r, const_tree type)
1595 : : {
1596 : 107169760 : REAL_VALUE_TYPE min = frange_val_min (type);
1597 : 107169760 : return real_identical (&min, &r);
1598 : : }
1599 : :
1600 : : // Return TRUE if R is the max value for TYPE.
1601 : :
1602 : : inline bool
1603 : 54013888 : frange_val_is_max (const REAL_VALUE_TYPE &r, const_tree type)
1604 : : {
1605 : 54013888 : REAL_VALUE_TYPE max = frange_val_max (type);
1606 : 54013888 : return real_identical (&max, &r);
1607 : : }
1608 : :
1609 : : // Build a NAN with a state of NAN.
1610 : :
1611 : : inline void
1612 : 287367 : frange::set_nan (tree type, const nan_state &nan)
1613 : : {
1614 : 287367 : gcc_checking_assert (nan.pos_p () || nan.neg_p ());
1615 : 287367 : if (HONOR_NANS (type))
1616 : : {
1617 : 287366 : m_kind = VR_NAN;
1618 : 287366 : m_type = type;
1619 : 287366 : m_neg_nan = nan.neg_p ();
1620 : 287366 : m_pos_nan = nan.pos_p ();
1621 : 287366 : if (flag_checking)
1622 : 287366 : verify_range ();
1623 : : }
1624 : : else
1625 : 1 : set_undefined ();
1626 : 287367 : }
1627 : :
1628 : : // Build a signless NAN of type TYPE.
1629 : :
1630 : : inline void
1631 : 123329 : frange::set_nan (tree type)
1632 : : {
1633 : 123329 : nan_state nan (true);
1634 : 123317 : set_nan (type, nan);
1635 : 101745 : }
1636 : :
1637 : : // Build a NAN of type TYPE with SIGN.
1638 : :
1639 : : inline void
1640 : 164035 : frange::set_nan (tree type, bool sign)
1641 : : {
1642 : 164035 : nan_state nan (/*pos=*/!sign, /*neg=*/sign);
1643 : 164031 : set_nan (type, nan);
1644 : 16044 : }
1645 : :
1646 : : // Return TRUE if range is known to be finite.
1647 : :
1648 : : inline bool
1649 : 0 : frange::known_isfinite () const
1650 : : {
1651 : 0 : if (undefined_p () || varying_p () || m_kind == VR_ANTI_RANGE)
1652 : : return false;
1653 : 0 : return (!maybe_isnan () && !real_isinf (&m_min) && !real_isinf (&m_max));
1654 : : }
1655 : :
1656 : : // Return TRUE if range is known to be normal.
1657 : :
1658 : : inline bool
1659 : 0 : frange::known_isnormal () const
1660 : : {
1661 : 0 : if (!known_isfinite ())
1662 : : return false;
1663 : :
1664 : 0 : machine_mode mode = TYPE_MODE (type ());
1665 : 0 : return (!real_isdenormal (&m_min, mode) && !real_isdenormal (&m_max, mode)
1666 : 0 : && !real_iszero (&m_min) && !real_iszero (&m_max)
1667 : 0 : && (!real_isneg (&m_min) || real_isneg (&m_max)));
1668 : : }
1669 : :
1670 : : // Return TRUE if range is known to be denormal.
1671 : :
1672 : : inline bool
1673 : 0 : frange::known_isdenormal_or_zero () const
1674 : : {
1675 : 0 : if (!known_isfinite ())
1676 : : return false;
1677 : :
1678 : 0 : machine_mode mode = TYPE_MODE (type ());
1679 : 0 : return ((real_isdenormal (&m_min, mode) || real_iszero (&m_min))
1680 : 0 : && (real_isdenormal (&m_max, mode) || real_iszero (&m_max)));
1681 : : }
1682 : :
1683 : : // Return TRUE if range may be infinite.
1684 : :
1685 : : inline bool
1686 : 33632 : frange::maybe_isinf () const
1687 : : {
1688 : 33632 : if (undefined_p () || m_kind == VR_ANTI_RANGE || m_kind == VR_NAN)
1689 : : return false;
1690 : 33632 : if (varying_p ())
1691 : : return true;
1692 : 31687 : return real_isinf (&m_min) || real_isinf (&m_max);
1693 : : }
1694 : :
1695 : : // Return TRUE if range is known to be the [-INF,-INF] or [+INF,+INF].
1696 : :
1697 : : inline bool
1698 : 6217058 : frange::known_isinf () const
1699 : : {
1700 : 6217058 : return (m_kind == VR_RANGE
1701 : 8921923 : && !maybe_isnan ()
1702 : 1352425 : && real_identical (&m_min, &m_max)
1703 : 6234513 : && real_isinf (&m_min));
1704 : : }
1705 : :
1706 : : // Return TRUE if range is possibly a NAN.
1707 : :
1708 : : inline bool
1709 : 23584447 : frange::maybe_isnan () const
1710 : : {
1711 : 21762269 : if (undefined_p ())
1712 : : return false;
1713 : 23581420 : return m_pos_nan || m_neg_nan;
1714 : : }
1715 : :
1716 : : // Return TRUE if range is possibly a NAN with SIGN.
1717 : :
1718 : : inline bool
1719 : 160434 : frange::maybe_isnan (bool sign) const
1720 : : {
1721 : 160434 : if (undefined_p ())
1722 : : return false;
1723 : 160434 : if (sign)
1724 : 160434 : return m_neg_nan;
1725 : : return m_pos_nan;
1726 : : }
1727 : :
1728 : : // Return TRUE if range is a +NAN or -NAN.
1729 : :
1730 : : inline bool
1731 : 17537081 : frange::known_isnan () const
1732 : : {
1733 : 13468247 : return m_kind == VR_NAN;
1734 : : }
1735 : :
1736 : : // If the signbit for the range is known, set it in SIGNBIT and return
1737 : : // TRUE.
1738 : :
1739 : : inline bool
1740 : 1891350 : frange::signbit_p (bool &signbit) const
1741 : : {
1742 : 1891350 : if (undefined_p ())
1743 : : return false;
1744 : :
1745 : : // NAN with unknown sign.
1746 : 1891332 : if (m_pos_nan && m_neg_nan)
1747 : : return false;
1748 : : // No NAN.
1749 : 123501 : if (!m_pos_nan && !m_neg_nan)
1750 : : {
1751 : 103137 : if (m_min.sign == m_max.sign)
1752 : : {
1753 : 9756 : signbit = m_min.sign;
1754 : 9756 : return true;
1755 : : }
1756 : : return false;
1757 : : }
1758 : : // NAN with known sign.
1759 : 20364 : bool nan_sign = m_neg_nan;
1760 : 20364 : if (known_isnan ()
1761 : 20364 : || (nan_sign == m_min.sign && nan_sign == m_max.sign))
1762 : : {
1763 : 19728 : signbit = nan_sign;
1764 : 19728 : return true;
1765 : : }
1766 : : return false;
1767 : : }
1768 : :
1769 : : // If range has a NAN with a known sign, set it in SIGNBIT and return
1770 : : // TRUE.
1771 : :
1772 : : inline bool
1773 : 51639 : frange::nan_signbit_p (bool &signbit) const
1774 : : {
1775 : 51639 : if (undefined_p ())
1776 : : return false;
1777 : :
1778 : 51639 : if (m_pos_nan == m_neg_nan)
1779 : : return false;
1780 : :
1781 : 2420 : signbit = m_neg_nan;
1782 : 2420 : return true;
1783 : : }
1784 : :
1785 : : void frange_nextafter (enum machine_mode, REAL_VALUE_TYPE &,
1786 : : const REAL_VALUE_TYPE &);
1787 : : void frange_arithmetic (enum tree_code, tree, REAL_VALUE_TYPE &,
1788 : : const REAL_VALUE_TYPE &, const REAL_VALUE_TYPE &,
1789 : : const REAL_VALUE_TYPE &);
1790 : :
1791 : : // Return true if TYPE1 and TYPE2 are compatible range types.
1792 : :
1793 : : inline bool
1794 : 1401256806 : range_compatible_p (tree type1, tree type2)
1795 : : {
1796 : : // types_compatible_p requires conversion in both directions to be useless.
1797 : : // GIMPLE only requires a cast one way in order to be compatible.
1798 : : // Ranges really only need the sign and precision to be the same.
1799 : 1401256806 : return (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1800 : 1401256806 : && TYPE_SIGN (type1) == TYPE_SIGN (type2));
1801 : : }
1802 : : #endif // GCC_VALUE_RANGE_H
|