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.
44 class GTY((user
)) irange
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_kind ();
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 wide_int
legacy_lower_bound (unsigned = 0) const;
122 wide_int
legacy_upper_bound (unsigned) const;
123 int value_inside_range (tree
) const;
124 bool maybe_anti_range () const;
125 void copy_to_legacy (const irange
&);
126 void copy_legacy_to_multi_range (const irange
&);
129 friend void gt_ggc_mx (irange
*);
130 friend void gt_pch_nx (irange
*);
131 friend void gt_pch_nx (irange
*, gt_pointer_operator
, void *);
133 void irange_set_1bit_anti_range (tree
, tree
);
134 bool varying_compatible_p () const;
136 unsigned char m_num_ranges
;
137 unsigned char m_max_ranges
;
138 ENUM_BITFIELD(value_range_kind
) m_kind
: 8;
142 // Here we describe an irange with N pairs of ranges. The storage for
143 // the pairs is embedded in the class as an array.
146 class GTY((user
)) int_range
: public irange
150 int_range (tree
, tree
, value_range_kind
= VR_RANGE
);
151 int_range (tree type
, const wide_int
&, const wide_int
&,
152 value_range_kind
= VR_RANGE
);
153 int_range (tree type
);
154 int_range (const int_range
&);
155 int_range (const irange
&);
156 int_range
& operator= (const int_range
&);
158 template <unsigned X
> friend void gt_ggc_mx (int_range
<X
> *);
159 template <unsigned X
> friend void gt_pch_nx (int_range
<X
> *);
160 template <unsigned X
> friend void gt_pch_nx (int_range
<X
> *,
161 gt_pointer_operator
, void *);
163 // ?? These stubs are for ipa-prop.c which use a value_range in a
164 // hash_traits. hash-traits.h defines an extern of gt_ggc_mx (T &)
165 // instead of picking up the gt_ggc_mx (T *) version.
166 friend void gt_ggc_mx (int_range
<1> *&);
167 friend void gt_pch_nx (int_range
<1> *&);
172 // This is a special int_range<1> with only one pair, plus
173 // VR_ANTI_RANGE magic to describe slightly more than can be described
174 // in one pair. It is described in the code as a "legacy range" (as
175 // opposed to multi-ranges which have multiple sub-ranges). It is
176 // provided for backward compatibility with code that has not been
177 // converted to multi-range irange's.
179 // There are copy operators to seamlessly copy to/fro multi-ranges.
180 typedef int_range
<1> value_range
;
182 // This is an "infinite" precision irange for use in temporary
184 typedef int_range
<255> int_range_max
;
186 // Returns true for an old-school value_range as described above.
188 irange::legacy_mode_p () const
190 return m_max_ranges
== 1;
193 extern bool range_has_numeric_bounds_p (const irange
*);
194 extern bool ranges_from_anti_range (const value_range
*,
195 value_range
*, value_range
*);
196 extern void dump_value_range (FILE *, const irange
*);
197 extern bool vrp_val_is_min (const_tree
);
198 extern bool vrp_val_is_max (const_tree
);
199 extern bool vrp_operand_equal_p (const_tree
, const_tree
);
201 inline value_range_kind
202 irange::kind () const
207 // Number of sub-ranges in a range.
210 irange::num_pairs () const
212 if (m_kind
== VR_ANTI_RANGE
)
213 return constant_p () ? 2 : 1;
219 irange::type () const
221 gcc_checking_assert (m_num_ranges
> 0);
222 return TREE_TYPE (m_base
[0]);
225 // Return the lower bound of a sub-range expressed as a tree. PAIR is
226 // the sub-range in question.
229 irange::tree_lower_bound (unsigned pair
) const
231 return m_base
[pair
* 2];
234 // Return the upper bound of a sub-range expressed as a tree. PAIR is
235 // the sub-range in question.
238 irange::tree_upper_bound (unsigned pair
) const
240 return m_base
[pair
* 2 + 1];
243 // Return the highest bound of a range expressed as a tree.
246 irange::tree_upper_bound () const
248 gcc_checking_assert (m_num_ranges
);
249 return tree_upper_bound (m_num_ranges
- 1);
255 return tree_lower_bound (0);
262 return tree_upper_bound ();
268 irange::varying_compatible_p () const
270 if (m_num_ranges
!= 1)
275 tree t
= TREE_TYPE (l
);
277 if (m_kind
== VR_VARYING
&& t
== error_mark_node
)
280 unsigned prec
= TYPE_PRECISION (t
);
281 signop sign
= TYPE_SIGN (t
);
282 if (INTEGRAL_TYPE_P (t
))
283 return (wi::to_wide (l
) == wi::min_value (prec
, sign
)
284 && wi::to_wide (u
) == wi::max_value (prec
, sign
));
285 if (POINTER_TYPE_P (t
))
286 return (wi::to_wide (l
) == 0
287 && wi::to_wide (u
) == wi::max_value (prec
, sign
));
292 irange::varying_p () const
294 return m_kind
== VR_VARYING
;
298 irange::undefined_p () const
300 return m_kind
== VR_UNDEFINED
;
304 irange::zero_p () const
306 return (m_kind
== VR_RANGE
&& m_num_ranges
== 1
307 && integer_zerop (tree_lower_bound (0))
308 && integer_zerop (tree_upper_bound (0)));
312 irange::nonzero_p () const
317 tree zero
= build_zero_cst (type ());
318 return *this == int_range
<1> (zero
, zero
, VR_ANTI_RANGE
);
322 irange::supports_type_p (tree type
)
324 if (type
&& (INTEGRAL_TYPE_P (type
) || POINTER_TYPE_P (type
)))
330 range_includes_zero_p (const irange
*vr
)
332 if (vr
->undefined_p ())
335 if (vr
->varying_p ())
338 return vr
->may_contain_p (build_zero_cst (vr
->type ()));
342 gt_ggc_mx (irange
*x
)
344 for (unsigned i
= 0; i
< x
->m_num_ranges
; ++i
)
346 gt_ggc_mx (x
->m_base
[i
* 2]);
347 gt_ggc_mx (x
->m_base
[i
* 2 + 1]);
352 gt_pch_nx (irange
*x
)
354 for (unsigned i
= 0; i
< x
->m_num_ranges
; ++i
)
356 gt_pch_nx (x
->m_base
[i
* 2]);
357 gt_pch_nx (x
->m_base
[i
* 2 + 1]);
362 gt_pch_nx (irange
*x
, gt_pointer_operator op
, void *cookie
)
364 for (unsigned i
= 0; i
< x
->m_num_ranges
; ++i
)
366 op (&x
->m_base
[i
* 2], cookie
);
367 op (&x
->m_base
[i
* 2 + 1], cookie
);
373 gt_ggc_mx (int_range
<N
> *x
)
375 gt_ggc_mx ((irange
*) x
);
380 gt_pch_nx (int_range
<N
> *x
)
382 gt_pch_nx ((irange
*) x
);
387 gt_pch_nx (int_range
<N
> *x
, gt_pointer_operator op
, void *cookie
)
389 gt_pch_nx ((irange
*) x
, op
, cookie
);
392 // Constructors for irange
395 irange::irange (tree
*base
, unsigned nranges
)
399 m_max_ranges
= nranges
;
400 m_kind
= VR_UNDEFINED
;
403 // Constructors for int_range<>.
407 int_range
<N
>::int_range ()
408 : irange (m_ranges
, N
)
413 int_range
<N
>::int_range (const int_range
&other
)
414 : irange (m_ranges
, N
)
416 irange::operator= (other
);
420 int_range
<N
>::int_range (tree min
, tree max
, value_range_kind kind
)
421 : irange (m_ranges
, N
)
423 irange::set (min
, max
, kind
);
427 int_range
<N
>::int_range (tree type
)
428 : irange (m_ranges
, N
)
434 int_range
<N
>::int_range (tree type
, const wide_int
&wmin
, const wide_int
&wmax
,
435 value_range_kind kind
)
436 : irange (m_ranges
, N
)
438 tree min
= wide_int_to_tree (type
, wmin
);
439 tree max
= wide_int_to_tree (type
, wmax
);
440 set (min
, max
, kind
);
444 int_range
<N
>::int_range (const irange
&other
)
445 : irange (m_ranges
, N
)
447 irange::operator= (other
);
452 int_range
<N
>::operator= (const int_range
&src
)
454 irange::operator= (src
);
459 irange::set (tree val
)
465 irange::set_undefined ()
467 m_kind
= VR_UNDEFINED
;
472 irange::set_varying (tree type
)
477 if (INTEGRAL_TYPE_P (type
))
479 wide_int min
= wi::min_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
480 wide_int max
= wi::max_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
481 m_base
[0] = wide_int_to_tree (type
, min
);
482 m_base
[1] = wide_int_to_tree (type
, max
);
484 else if (POINTER_TYPE_P (type
))
486 m_base
[0] = build_int_cst (type
, 0);
487 m_base
[1] = build_int_cst (type
, -1);
490 m_base
[0] = m_base
[1] = error_mark_node
;
494 irange::operator== (const irange
&r
) const
499 // Return the lower bound of a sub-range. PAIR is the sub-range in
503 irange::lower_bound (unsigned pair
) const
505 if (legacy_mode_p ())
506 return legacy_lower_bound (pair
);
507 gcc_checking_assert (m_num_ranges
> 0);
508 gcc_checking_assert (pair
+ 1 <= num_pairs ());
509 return wi::to_wide (tree_lower_bound (pair
));
512 // Return the upper bound of a sub-range. PAIR is the sub-range in
516 irange::upper_bound (unsigned pair
) const
518 if (legacy_mode_p ())
519 return legacy_upper_bound (pair
);
520 gcc_checking_assert (m_num_ranges
> 0);
521 gcc_checking_assert (pair
+ 1 <= num_pairs ());
522 return wi::to_wide (tree_upper_bound (pair
));
525 // Return the highest bound of a range.
528 irange::upper_bound () const
530 unsigned pairs
= num_pairs ();
531 gcc_checking_assert (pairs
> 0);
532 return upper_bound (pairs
- 1);
536 irange::union_ (const irange
&r
)
538 dump_flags_t m_flags
= dump_flags
;
539 dump_flags
&= ~TDF_DETAILS
;
541 dump_flags
= m_flags
;
545 irange::intersect (const irange
&r
)
547 dump_flags_t m_flags
= dump_flags
;
548 dump_flags
&= ~TDF_DETAILS
;
549 irange::intersect (&r
);
550 dump_flags
= m_flags
;
553 // Set value range VR to a nonzero range of type TYPE.
556 irange::set_nonzero (tree type
)
558 tree zero
= build_int_cst (type
, 0);
559 if (legacy_mode_p ())
560 set (zero
, zero
, VR_ANTI_RANGE
);
562 irange_set_anti_range (zero
, zero
);
565 // Set value range VR to a ZERO range of type TYPE.
568 irange::set_zero (tree type
)
570 tree z
= build_int_cst (type
, 0);
571 if (legacy_mode_p ())
577 // Normalize a range to VARYING or UNDEFINED if possible.
580 irange::normalize_kind ()
582 if (m_num_ranges
== 0)
583 m_kind
= VR_UNDEFINED
;
584 else if (varying_compatible_p ())
586 if (m_kind
== VR_RANGE
)
588 else if (m_kind
== VR_ANTI_RANGE
)
595 // Return the maximum value for TYPE.
598 vrp_val_max (const_tree type
)
600 if (INTEGRAL_TYPE_P (type
))
601 return TYPE_MAX_VALUE (type
);
602 if (POINTER_TYPE_P (type
))
604 wide_int max
= wi::max_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
605 return wide_int_to_tree (const_cast<tree
> (type
), max
);
610 // Return the minimum value for TYPE.
613 vrp_val_min (const_tree type
)
615 if (INTEGRAL_TYPE_P (type
))
616 return TYPE_MIN_VALUE (type
);
617 if (POINTER_TYPE_P (type
))
618 return build_zero_cst (const_cast<tree
> (type
));
622 // This is the irange storage class. It is used to allocate the
623 // minimum amount of storage needed for a given irange. Storage is
624 // automatically freed at destruction of the storage class.
626 // It is meant for long term storage, as opposed to int_range_max
627 // which is meant for intermediate temporary results on the stack.
629 // The newly allocated irange is initialized to the empty set
630 // (undefined_p() is true).
632 class irange_allocator
636 ~irange_allocator ();
637 // Return a new range with NUM_PAIRS.
638 irange
*allocate (unsigned num_pairs
);
639 // Return a copy of SRC with the minimum amount of sub-ranges needed
641 irange
*allocate (const irange
&src
);
642 void *get_memory (unsigned num_bytes
);
644 DISABLE_COPY_AND_ASSIGN (irange_allocator
);
645 struct obstack m_obstack
;
649 irange_allocator::irange_allocator ()
651 obstack_init (&m_obstack
);
655 irange_allocator::~irange_allocator ()
657 obstack_free (&m_obstack
, NULL
);
660 // Provide a hunk of memory from the obstack.
662 irange_allocator::get_memory (unsigned num_bytes
)
664 void *r
= obstack_alloc (&m_obstack
, num_bytes
);
668 // Return a new range with NUM_PAIRS.
671 irange_allocator::allocate (unsigned num_pairs
)
673 // Never allocate 0 pairs.
674 // Don't allocate 1 either, or we get legacy value_range's.
678 size_t nbytes
= sizeof (tree
) * 2 * num_pairs
;
680 // Allocate the irange and required memory for the vector.
681 void *r
= obstack_alloc (&m_obstack
, sizeof (irange
));
682 tree
*mem
= (tree
*) obstack_alloc (&m_obstack
, nbytes
);
683 return new (r
) irange (mem
, num_pairs
);
687 irange_allocator::allocate (const irange
&src
)
689 irange
*r
= allocate (src
.num_pairs ());
694 #endif // GCC_VALUE_RANGE_H