ConstantRange.h revision bf8c7f0adf90c8271c790be9922a2f97d19d4c01
1//===-- llvm/Support/ConstantRange.h - Represent a range --------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Represent a range of possible values that may occur when the program is run
11// for an integral value.  This keeps track of a lower and upper bound for the
12// constant, which MAY wrap around the end of the numeric range.  To do this, it
13// keeps track of a [lower, upper) bound, which specifies an interval just like
14// STL iterators.  When used with boolean values, the following are important
15// ranges: :
16//
17//  [F, F) = {}     = Empty set
18//  [T, F) = {T}
19//  [F, T) = {F}
20//  [T, T) = {F, T} = Full set
21//
22// The other integral ranges use min/max values for special range values. For
23// example, for 8-bit types, it uses:
24// [0, 0)     = {}       = Empty set
25// [255, 255) = {0..255} = Full Set
26//
27// Note that ConstantRange can be used to represent either signed or
28// unsigned ranges.
29//
30//===----------------------------------------------------------------------===//
31
32#ifndef LLVM_SUPPORT_CONSTANT_RANGE_H
33#define LLVM_SUPPORT_CONSTANT_RANGE_H
34
35#include "llvm/ADT/APInt.h"
36#include "llvm/Support/DataTypes.h"
37
38namespace llvm {
39
40/// ConstantRange - This class represents an range of values.
41///
42class ConstantRange {
43  APInt Lower, Upper;
44  static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
45                                         const ConstantRange &RHS);
46
47public:
48  /// Initialize a full (the default) or empty set for the specified bit width.
49  ///
50  explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
51
52  /// Initialize a range to hold the single specified value.
53  ///
54  ConstantRange(const APInt &Value);
55
56  /// @brief Initialize a range of values explicitly. This will assert out if
57  /// Lower==Upper and Lower != Min or Max value for its type. It will also
58  /// assert out if the two APInt's are not the same bit width.
59  ConstantRange(const APInt& Lower, const APInt& Upper);
60
61  /// makeICmpRegion - Return the range of values that a value must be within
62  /// in order for the comparison specified by the predicate against range
63  /// Other to be true.
64  static ConstantRange makeICmpRegion(unsigned Pred,
65                                      const ConstantRange &Other);
66
67  /// getLower - Return the lower value for this range...
68  ///
69  const APInt &getLower() const { return Lower; }
70
71  /// getUpper - Return the upper value for this range...
72  ///
73  const APInt &getUpper() const { return Upper; }
74
75  /// getBitWidth - get the bit width of this ConstantRange
76  ///
77  uint32_t getBitWidth() const { return Lower.getBitWidth(); }
78
79  /// isFullSet - Return true if this set contains all of the elements possible
80  /// for this data-type
81  ///
82  bool isFullSet() const;
83
84  /// isEmptySet - Return true if this set contains no members.
85  ///
86  bool isEmptySet() const;
87
88  /// isWrappedSet - Return true if this set wraps around the top of the range,
89  /// for example: [100, 8)
90  ///
91  bool isWrappedSet() const;
92
93  /// contains - Return true if the specified value is in the set.
94  ///
95  bool contains(const APInt &Val) const;
96
97  /// contains - Return true if the other range is a subset of this one.
98  ///
99  bool contains(const ConstantRange &CR) const;
100
101  /// getSingleElement - If this set contains a single element, return it,
102  /// otherwise return null.
103  ///
104  const APInt *getSingleElement() const {
105    if (Upper == Lower + 1)
106      return &Lower;
107    return 0;
108  }
109
110  /// isSingleElement - Return true if this set contains exactly one member.
111  ///
112  bool isSingleElement() const { return getSingleElement() != 0; }
113
114  /// getSetSize - Return the number of elements in this set.
115  ///
116  APInt getSetSize() const;
117
118  /// getUnsignedMax - Return the largest unsigned value contained in the
119  /// ConstantRange.
120  ///
121  APInt getUnsignedMax() const;
122
123  /// getUnsignedMin - Return the smallest unsigned value contained in the
124  /// ConstantRange.
125  ///
126  APInt getUnsignedMin() const;
127
128  /// getSignedMax - Return the largest signed value contained in the
129  /// ConstantRange.
130  ///
131  APInt getSignedMax() const;
132
133  /// getSignedMin - Return the smallest signed value contained in the
134  /// ConstantRange.
135  ///
136  APInt getSignedMin() const;
137
138  /// operator== - Return true if this range is equal to another range.
139  ///
140  bool operator==(const ConstantRange &CR) const {
141    return Lower == CR.Lower && Upper == CR.Upper;
142  }
143  bool operator!=(const ConstantRange &CR) const {
144    return !operator==(CR);
145  }
146
147  /// subtract - Subtract the specified constant from the endpoints of this
148  /// constant range.
149  ConstantRange subtract(const APInt &CI) const;
150
151  /// intersectWith - Return the range that results from the intersection of
152  /// this range with another range.  The resultant range is pruned as much as
153  /// possible, but there may be cases where elements are included that are in
154  /// one of the sets but not the other.  For example: [100, 8) intersect [3,
155  /// 120) yields [3, 120)
156  ///
157  ConstantRange intersectWith(const ConstantRange &CR) const;
158
159  /// maximalIntersectWith - Return the range that results from the intersection
160  /// of this range with another range.  The resultant range is guaranteed to
161  /// include all elements contained in both input ranges, and to have the
162  /// smallest possible set size that does so.  Because there may be two
163  /// intersections with the same set size, A.maximalIntersectWith(B) might not
164  /// be equal to B.maximalIntersectWith(A).
165  ///
166  ConstantRange maximalIntersectWith(const ConstantRange &CR) const;
167
168  /// unionWith - Return the range that results from the union of this range
169  /// with another range.  The resultant range is guaranteed to include the
170  /// elements of both sets, but may contain more.  For example, [3, 9) union
171  /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
172  /// in either set before.
173  ///
174  ConstantRange unionWith(const ConstantRange &CR) const;
175
176  /// zeroExtend - Return a new range in the specified integer type, which must
177  /// be strictly larger than the current type.  The returned range will
178  /// correspond to the possible range of values if the source range had been
179  /// zero extended to BitWidth.
180  ConstantRange zeroExtend(uint32_t BitWidth) const;
181
182  /// signExtend - Return a new range in the specified integer type, which must
183  /// be strictly larger than the current type.  The returned range will
184  /// correspond to the possible range of values if the source range had been
185  /// sign extended to BitWidth.
186  ConstantRange signExtend(uint32_t BitWidth) const;
187
188  /// truncate - Return a new range in the specified integer type, which must be
189  /// strictly smaller than the current type.  The returned range will
190  /// correspond to the possible range of values if the source range had been
191  /// truncated to the specified type.
192  ConstantRange truncate(uint32_t BitWidth) const;
193
194  /// add - Return a new range representing the possible values resulting
195  /// from an addition of a value in this range and a value in Other.
196  ConstantRange add(const ConstantRange &Other) const;
197
198  /// multiply - Return a new range representing the possible values resulting
199  /// from a multiplication of a value in this range and a value in Other.
200  /// TODO: This isn't fully implemented yet.
201  ConstantRange multiply(const ConstantRange &Other) const;
202
203  /// smax - Return a new range representing the possible values resulting
204  /// from a signed maximum of a value in this range and a value in Other.
205  ConstantRange smax(const ConstantRange &Other) const;
206
207  /// umax - Return a new range representing the possible values resulting
208  /// from an unsigned maximum of a value in this range and a value in Other.
209  ConstantRange umax(const ConstantRange &Other) const;
210
211  /// udiv - Return a new range representing the possible values resulting
212  /// from an unsigned division of a value in this range and a value in Other.
213  /// TODO: This isn't fully implemented yet.
214  ConstantRange udiv(const ConstantRange &Other) const;
215
216  /// print - Print out the bounds to a stream...
217  ///
218  void print(raw_ostream &OS) const;
219
220  /// dump - Allow printing from a debugger easily...
221  ///
222  void dump() const;
223};
224
225inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
226  CR.print(OS);
227  return OS;
228}
229
230std::ostream &operator<<(std::ostream &OS, const ConstantRange &CR);
231
232} // End llvm namespace
233
234#endif
235