1 /* Support routines for value ranges.
2 Copyright (C) 2019-2021 Free Software Foundation, Inc.
3 Contributed by Aldy Hernandez <aldyh@redhat.com> and
4 Andrew Macleod <amacleod@redhat.com>.
6 This file is part of GCC.
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)
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.
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/>. */
22 #ifndef GCC_VALUE_RANGE_H
23 #define GCC_VALUE_RANGE_H
25 // Types of value ranges.
30 /* Range spans the entire domain. */
32 /* Range is [MIN, MAX]. */
34 /* Range is ~[MIN, MAX]. */
36 /* Range is a nice guy. */
40 // Range of values that can be associated with an SSA_NAME.
42 // This is the base class without any storage.
46 friend class irange_allocator
;
49 void set (tree
, tree
, value_range_kind
= VR_RANGE
);
50 void set_nonzero (tree
);
52 void set_varying (tree type
);
53 void set_undefined ();
56 static bool supports_type_p (tree
);
59 // Iteration over sub-ranges.
60 unsigned num_pairs () const;
61 wide_int
lower_bound (unsigned = 0) const;
62 wide_int
upper_bound (unsigned) const;
63 wide_int
upper_bound () const;
67 bool nonzero_p () const;
68 bool undefined_p () const;
69 bool varying_p () const;
70 bool singleton_p (tree
*result
= NULL
) const;
71 bool contains_p (tree
) const;
73 // In-place operators.
74 void union_ (const irange
&);
75 void intersect (const irange
&);
78 // Operator overloads.
79 irange
& operator= (const irange
&);
80 bool operator== (const irange
&) const;
81 bool operator!= (const irange
&r
) const { return !(*this == r
); }
84 bool fits_p (const irange
&r
) { return m_max_ranges
>= r
.num_pairs (); }
85 void dump (FILE * = stderr
) const;
87 // Deprecated legacy public methods.
88 enum value_range_kind
kind () const; // DEPRECATED
89 tree
min () const; // DEPRECATED
90 tree
max () const; // DEPRECATED
91 bool symbolic_p () const; // DEPRECATED
92 bool constant_p () const; // DEPRECATED
93 void normalize_symbolics (); // DEPRECATED
94 void normalize_addresses (); // DEPRECATED
95 bool may_contain_p (tree
) const; // DEPRECATED
96 void set (tree
); // DEPRECATED
97 bool equal_p (const irange
&) const; // DEPRECATED
98 void union_ (const class irange
*); // DEPRECATED
99 void intersect (const irange
*); // DEPRECATED
102 irange (tree
*, unsigned);
103 // potential promotion to public?
104 tree
tree_lower_bound (unsigned = 0) const;
105 tree
tree_upper_bound (unsigned) const;
106 tree
tree_upper_bound () const;
108 // In-place operators.
109 void irange_union (const irange
&);
110 void irange_intersect (const irange
&);
111 void irange_set (tree
, tree
);
112 void irange_set_anti_range (tree
, tree
);
114 void normalize_min_max ();
116 bool legacy_mode_p () const;
117 bool legacy_equal_p (const irange
&) const;
118 void legacy_union (irange
*, const irange
*);
119 void legacy_intersect (irange
*, const irange
*);
120 void verify_range ();
121 unsigned legacy_num_pairs () const;
122 wide_int
legacy_lower_bound (unsigned = 0) const;
123 wide_int
legacy_upper_bound (unsigned) const;
124 int value_inside_range (tree
) const;
125 bool maybe_anti_range () const;
126 void copy_to_legacy (const irange
&);
127 void copy_legacy_to_multi_range (const irange
&);
130 unsigned char m_num_ranges
;
131 unsigned char m_max_ranges
;
132 ENUM_BITFIELD(value_range_kind
) m_kind
: 8;
136 // Here we describe an irange with N pairs of ranges. The storage for
137 // the pairs is embedded in the class as an array.
140 class GTY((user
)) int_range
: public irange
144 int_range (tree
, tree
, value_range_kind
= VR_RANGE
);
145 int_range (tree type
, const wide_int
&, const wide_int
&,
146 value_range_kind
= VR_RANGE
);
147 int_range (tree type
);
148 int_range (const int_range
&);
149 int_range (const irange
&);
150 int_range
& operator= (const int_range
&);
152 template <unsigned X
> friend void gt_ggc_mx (int_range
<X
> *);
153 template <unsigned X
> friend void gt_pch_nx (int_range
<X
> *);
154 template <unsigned X
> friend void gt_pch_nx (int_range
<X
> *,
155 gt_pointer_operator
, void *);
156 // ?? hash-traits.h has its own extern for these, which is causing
157 // them to never be picked up by the templates. For now, define
159 //template<unsigned X> friend void gt_ggc_mx (int_range<X> *&);
160 //template<unsigned X> friend void gt_pch_nx (int_range<X> *&);
161 friend void gt_ggc_mx (int_range
<1> *&);
162 friend void gt_pch_nx (int_range
<1> *&);
167 // This is a special int_range<1> with only one pair, plus
168 // VR_ANTI_RANGE magic to describe slightly more than can be described
169 // in one pair. It is described in the code as a "legacy range" (as
170 // opposed to multi-ranges which have multiple sub-ranges). It is
171 // provided for backward compatibility with code that has not been
172 // converted to multi-range irange's.
174 // There are copy operators to seamlessly copy to/fro multi-ranges.
175 typedef int_range
<1> value_range
;
177 // This is an "infinite" precision irange for use in temporary
179 typedef int_range
<255> int_range_max
;
181 // Returns true for an old-school value_range as described above.
183 irange::legacy_mode_p () const
185 return m_max_ranges
== 1;
188 extern bool range_has_numeric_bounds_p (const irange
*);
189 extern bool ranges_from_anti_range (const value_range
*,
190 value_range
*, value_range
*);
191 extern void dump_value_range (FILE *, const irange
*);
192 extern bool vrp_val_is_min (const_tree
);
193 extern bool vrp_val_is_max (const_tree
);
194 extern bool vrp_operand_equal_p (const_tree
, const_tree
);
196 inline value_range_kind
197 irange::kind () const
199 if (legacy_mode_p ())
211 // Number of sub-ranges in a range.
214 irange::num_pairs () const
216 if (!legacy_mode_p ())
219 return legacy_num_pairs ();
223 irange::type () const
225 gcc_checking_assert (!undefined_p ());
226 return TREE_TYPE (m_base
[0]);
229 // Return the lower bound of a sub-range expressed as a tree. PAIR is
230 // the sub-range in question.
233 irange::tree_lower_bound (unsigned pair
) const
235 return m_base
[pair
* 2];
238 // Return the upper bound of a sub-range expressed as a tree. PAIR is
239 // the sub-range in question.
242 irange::tree_upper_bound (unsigned pair
) const
244 return m_base
[pair
* 2 + 1];
247 // Return the highest bound of a range expressed as a tree.
250 irange::tree_upper_bound () const
252 gcc_checking_assert (m_num_ranges
);
253 return tree_upper_bound (m_num_ranges
- 1);
259 return tree_lower_bound (0);
266 return tree_upper_bound ();
272 irange::varying_p () const
274 if (legacy_mode_p ())
275 return m_kind
== VR_VARYING
;
277 if (m_num_ranges
!= 1)
282 tree t
= TREE_TYPE (l
);
283 unsigned prec
= TYPE_PRECISION (t
);
284 signop sign
= TYPE_SIGN (t
);
285 if (INTEGRAL_TYPE_P (t
))
286 return (wi::to_wide (l
) == wi::min_value (prec
, sign
)
287 && wi::to_wide (u
) == wi::max_value (prec
, sign
));
288 if (POINTER_TYPE_P (t
))
289 return (wi::to_wide (l
) == 0
290 && wi::to_wide (u
) == wi::max_value (prec
, sign
));
296 irange::undefined_p () const
298 if (!legacy_mode_p ())
299 return m_num_ranges
== 0;
301 if (CHECKING_P
&& legacy_mode_p ())
303 if (m_kind
== VR_UNDEFINED
)
304 gcc_checking_assert (m_num_ranges
== 0);
306 gcc_checking_assert (m_num_ranges
!= 0);
308 return m_kind
== VR_UNDEFINED
;
312 irange::zero_p () const
314 return (m_kind
== VR_RANGE
&& m_num_ranges
== 1
315 && integer_zerop (tree_lower_bound (0))
316 && integer_zerop (tree_upper_bound (0)));
320 irange::nonzero_p () const
325 tree zero
= build_zero_cst (type ());
326 return *this == int_range
<1> (zero
, zero
, VR_ANTI_RANGE
);
330 irange::supports_type_p (tree type
)
332 if (type
&& (INTEGRAL_TYPE_P (type
) || POINTER_TYPE_P (type
)))
338 range_includes_zero_p (const irange
*vr
)
340 if (vr
->undefined_p ())
343 if (vr
->varying_p ())
346 return vr
->may_contain_p (build_zero_cst (vr
->type ()));
351 gt_ggc_mx (int_range
<N
> *x
)
353 for (unsigned i
= 0; i
< N
; ++i
)
355 gt_ggc_mx (x
->m_ranges
[i
* 2]);
356 gt_ggc_mx (x
->m_ranges
[i
* 2 + 1]);
362 gt_pch_nx (int_range
<N
> *x
)
364 for (unsigned i
= 0; i
< N
; ++i
)
366 gt_pch_nx (x
->m_ranges
[i
* 2]);
367 gt_pch_nx (x
->m_ranges
[i
* 2 + 1]);
373 gt_pch_nx (int_range
<N
> *x
, gt_pointer_operator op
, void *cookie
)
375 for (unsigned i
= 0; i
< N
; ++i
)
377 op (&x
->m_ranges
[i
* 2], cookie
);
378 op (&x
->m_ranges
[i
* 2 + 1], cookie
);
382 // Constructors for irange
385 irange::irange (tree
*base
, unsigned nranges
)
389 m_max_ranges
= nranges
;
390 if (legacy_mode_p ())
391 m_kind
= VR_UNDEFINED
;
396 // Constructors for int_range<>.
400 int_range
<N
>::int_range ()
401 : irange (m_ranges
, N
)
406 int_range
<N
>::int_range (const int_range
&other
)
407 : irange (m_ranges
, N
)
409 irange::operator= (other
);
413 int_range
<N
>::int_range (tree min
, tree max
, value_range_kind kind
)
414 : irange (m_ranges
, N
)
416 irange::set (min
, max
, kind
);
420 int_range
<N
>::int_range (tree type
)
421 : irange (m_ranges
, N
)
427 int_range
<N
>::int_range (tree type
, const wide_int
&wmin
, const wide_int
&wmax
,
428 value_range_kind kind
)
429 : irange (m_ranges
, N
)
431 tree min
= wide_int_to_tree (type
, wmin
);
432 tree max
= wide_int_to_tree (type
, wmax
);
433 set (min
, max
, kind
);
437 int_range
<N
>::int_range (const irange
&other
)
438 : irange (m_ranges
, N
)
440 irange::operator= (other
);
445 int_range
<N
>::operator= (const int_range
&src
)
447 irange::operator= (src
);
452 irange::set (tree val
)
458 irange::set_undefined ()
461 if (legacy_mode_p ())
462 m_kind
= VR_UNDEFINED
;
466 irange::set_varying (tree type
)
468 if (legacy_mode_p ())
472 if (INTEGRAL_TYPE_P (type
))
474 wide_int min
= wi::min_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
475 wide_int max
= wi::max_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
476 m_base
[0] = wide_int_to_tree (type
, min
);
477 m_base
[1] = wide_int_to_tree (type
, max
);
479 else if (POINTER_TYPE_P (type
))
481 m_base
[0] = build_int_cst (type
, 0);
482 m_base
[1] = build_int_cst (type
, -1);
485 m_base
[0] = m_base
[1] = error_mark_node
;
489 irange::operator== (const irange
&r
) const
494 // Return the lower bound of a sub-range. PAIR is the sub-range in
498 irange::lower_bound (unsigned pair
) const
500 if (legacy_mode_p ())
501 return legacy_lower_bound (pair
);
502 gcc_checking_assert (!undefined_p ());
503 gcc_checking_assert (pair
+ 1 <= num_pairs ());
504 return wi::to_wide (tree_lower_bound (pair
));
507 // Return the upper bound of a sub-range. PAIR is the sub-range in
511 irange::upper_bound (unsigned pair
) const
513 if (legacy_mode_p ())
514 return legacy_upper_bound (pair
);
515 gcc_checking_assert (!undefined_p ());
516 gcc_checking_assert (pair
+ 1 <= num_pairs ());
517 return wi::to_wide (tree_upper_bound (pair
));
520 // Return the highest bound of a range.
523 irange::upper_bound () const
525 unsigned pairs
= num_pairs ();
526 gcc_checking_assert (pairs
> 0);
527 return upper_bound (pairs
- 1);
531 irange::union_ (const irange
&r
)
533 dump_flags_t m_flags
= dump_flags
;
534 dump_flags
&= ~TDF_DETAILS
;
536 dump_flags
= m_flags
;
540 irange::intersect (const irange
&r
)
542 dump_flags_t m_flags
= dump_flags
;
543 dump_flags
&= ~TDF_DETAILS
;
544 irange::intersect (&r
);
545 dump_flags
= m_flags
;
548 // Set value range VR to a nonzero range of type TYPE.
551 irange::set_nonzero (tree type
)
553 tree zero
= build_int_cst (type
, 0);
554 if (legacy_mode_p ())
555 set (zero
, zero
, VR_ANTI_RANGE
);
557 irange_set_anti_range (zero
, zero
);
560 // Set value range VR to a ZERO range of type TYPE.
563 irange::set_zero (tree type
)
565 tree z
= build_int_cst (type
, 0);
566 if (legacy_mode_p ())
572 // Normalize a range to VARYING or UNDEFINED if possible.
575 irange::normalize_min_max ()
577 gcc_checking_assert (legacy_mode_p ());
578 gcc_checking_assert (!undefined_p ());
579 unsigned prec
= TYPE_PRECISION (type ());
580 signop sign
= TYPE_SIGN (type ());
581 if (wi::eq_p (wi::to_wide (min ()), wi::min_value (prec
, sign
))
582 && wi::eq_p (wi::to_wide (max ()), wi::max_value (prec
, sign
)))
584 if (m_kind
== VR_RANGE
)
585 set_varying (type ());
586 else if (m_kind
== VR_ANTI_RANGE
)
593 // Return the maximum value for TYPE.
596 vrp_val_max (const_tree type
)
598 if (INTEGRAL_TYPE_P (type
))
599 return TYPE_MAX_VALUE (type
);
600 if (POINTER_TYPE_P (type
))
602 wide_int max
= wi::max_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
603 return wide_int_to_tree (const_cast<tree
> (type
), max
);
608 // Return the minimum value for TYPE.
611 vrp_val_min (const_tree type
)
613 if (INTEGRAL_TYPE_P (type
))
614 return TYPE_MIN_VALUE (type
);
615 if (POINTER_TYPE_P (type
))
616 return build_zero_cst (const_cast<tree
> (type
));
620 // This is the irange storage class. It is used to allocate the
621 // minimum amount of storage needed for a given irange. Storage is
622 // automatically freed at destruction of the storage class.
624 // It is meant for long term storage, as opposed to int_range_max
625 // which is meant for intermediate temporary results on the stack.
627 // The newly allocated irange is initialized to the empty set
628 // (undefined_p() is true).
630 class irange_allocator
634 ~irange_allocator ();
635 // Return a new range with NUM_PAIRS.
636 irange
*allocate (unsigned num_pairs
);
637 // Return a copy of SRC with the minimum amount of sub-ranges needed
639 irange
*allocate (const irange
&src
);
641 DISABLE_COPY_AND_ASSIGN (irange_allocator
);
642 struct obstack m_obstack
;
646 irange_allocator::irange_allocator ()
648 obstack_init (&m_obstack
);
652 irange_allocator::~irange_allocator ()
654 obstack_free (&m_obstack
, NULL
);
657 // Return a new range with NUM_PAIRS.
660 irange_allocator::allocate (unsigned num_pairs
)
662 // Never allocate 0 pairs.
663 // Don't allocate 1 either, or we get legacy value_range's.
667 size_t nbytes
= sizeof (tree
) * 2 * num_pairs
;
669 // Allocate the irange and required memory for the vector.
670 void *r
= obstack_alloc (&m_obstack
, sizeof (irange
));
671 tree
*mem
= (tree
*) obstack_alloc (&m_obstack
, nbytes
);
672 return new (r
) irange (mem
, num_pairs
);
676 irange_allocator::allocate (const irange
&src
)
678 irange
*r
= allocate (src
.num_pairs ());
683 #endif // GCC_VALUE_RANGE_H