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_CONSTANTRANGE_H
33#define LLVM_SUPPORT_CONSTANTRANGE_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
45#if LLVM_HAS_RVALUE_REFERENCES
46  // If we have move semantics, pass APInts by value and move them into place.
47  typedef APInt APIntMoveTy;
48#else
49  // Otherwise pass by const ref to save one copy.
50  typedef const APInt &APIntMoveTy;
51#endif
52
53public:
54  /// Initialize a full (the default) or empty set for the specified bit width.
55  ///
56  explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
57
58  /// Initialize a range to hold the single specified value.
59  ///
60  ConstantRange(APIntMoveTy Value);
61
62  /// @brief Initialize a range of values explicitly. This will assert out if
63  /// Lower==Upper and Lower != Min or Max value for its type. It will also
64  /// assert out if the two APInt's are not the same bit width.
65  ConstantRange(APIntMoveTy Lower, APIntMoveTy Upper);
66
67  /// makeICmpRegion - Produce the smallest range that contains all values that
68  /// might satisfy the comparison specified by Pred when compared to any value
69  /// contained within Other.
70  ///
71  /// Solves for range X in 'for all x in X, there exists a y in Y such that
72  /// icmp op x, y is true'. Every value that might make the comparison true
73  /// is included in the resulting range.
74  static ConstantRange makeICmpRegion(unsigned Pred,
75                                      const ConstantRange &Other);
76
77  /// getLower - Return the lower value for this range...
78  ///
79  const APInt &getLower() const { return Lower; }
80
81  /// getUpper - Return the upper value for this range...
82  ///
83  const APInt &getUpper() const { return Upper; }
84
85  /// getBitWidth - get the bit width of this ConstantRange
86  ///
87  uint32_t getBitWidth() const { return Lower.getBitWidth(); }
88
89  /// isFullSet - Return true if this set contains all of the elements possible
90  /// for this data-type
91  ///
92  bool isFullSet() const;
93
94  /// isEmptySet - Return true if this set contains no members.
95  ///
96  bool isEmptySet() const;
97
98  /// isWrappedSet - Return true if this set wraps around the top of the range,
99  /// for example: [100, 8)
100  ///
101  bool isWrappedSet() const;
102
103  /// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
104  /// its bitwidth, for example: i8 [120, 140).
105  ///
106  bool isSignWrappedSet() const;
107
108  /// contains - Return true if the specified value is in the set.
109  ///
110  bool contains(const APInt &Val) const;
111
112  /// contains - Return true if the other range is a subset of this one.
113  ///
114  bool contains(const ConstantRange &CR) const;
115
116  /// getSingleElement - If this set contains a single element, return it,
117  /// otherwise return null.
118  ///
119  const APInt *getSingleElement() const {
120    if (Upper == Lower + 1)
121      return &Lower;
122    return 0;
123  }
124
125  /// isSingleElement - Return true if this set contains exactly one member.
126  ///
127  bool isSingleElement() const { return getSingleElement() != 0; }
128
129  /// getSetSize - Return the number of elements in this set.
130  ///
131  APInt getSetSize() const;
132
133  /// getUnsignedMax - Return the largest unsigned value contained in the
134  /// ConstantRange.
135  ///
136  APInt getUnsignedMax() const;
137
138  /// getUnsignedMin - Return the smallest unsigned value contained in the
139  /// ConstantRange.
140  ///
141  APInt getUnsignedMin() const;
142
143  /// getSignedMax - Return the largest signed value contained in the
144  /// ConstantRange.
145  ///
146  APInt getSignedMax() const;
147
148  /// getSignedMin - Return the smallest signed value contained in the
149  /// ConstantRange.
150  ///
151  APInt getSignedMin() const;
152
153  /// operator== - Return true if this range is equal to another range.
154  ///
155  bool operator==(const ConstantRange &CR) const {
156    return Lower == CR.Lower && Upper == CR.Upper;
157  }
158  bool operator!=(const ConstantRange &CR) const {
159    return !operator==(CR);
160  }
161
162  /// subtract - Subtract the specified constant from the endpoints of this
163  /// constant range.
164  ConstantRange subtract(const APInt &CI) const;
165
166  /// \brief Subtract the specified range from this range (aka relative
167  /// complement of the sets).
168  ConstantRange difference(const ConstantRange &CR) const;
169
170  /// intersectWith - Return the range that results from the intersection of
171  /// this range with another range.  The resultant range is guaranteed to
172  /// include all elements contained in both input ranges, and to have the
173  /// smallest possible set size that does so.  Because there may be two
174  /// intersections with the same set size, A.intersectWith(B) might not
175  /// be equal to B.intersectWith(A).
176  ///
177  ConstantRange intersectWith(const ConstantRange &CR) const;
178
179  /// unionWith - Return the range that results from the union of this range
180  /// with another range.  The resultant range is guaranteed to include the
181  /// elements of both sets, but may contain more.  For example, [3, 9) union
182  /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
183  /// in either set before.
184  ///
185  ConstantRange unionWith(const ConstantRange &CR) const;
186
187  /// zeroExtend - Return a new range in the specified integer type, which must
188  /// be strictly larger than the current type.  The returned range will
189  /// correspond to the possible range of values if the source range had been
190  /// zero extended to BitWidth.
191  ConstantRange zeroExtend(uint32_t BitWidth) const;
192
193  /// signExtend - Return a new range in the specified integer type, which must
194  /// be strictly larger than the current type.  The returned range will
195  /// correspond to the possible range of values if the source range had been
196  /// sign extended to BitWidth.
197  ConstantRange signExtend(uint32_t BitWidth) const;
198
199  /// truncate - Return a new range in the specified integer type, which must be
200  /// strictly smaller than the current type.  The returned range will
201  /// correspond to the possible range of values if the source range had been
202  /// truncated to the specified type.
203  ConstantRange truncate(uint32_t BitWidth) const;
204
205  /// zextOrTrunc - make this range have the bit width given by \p BitWidth. The
206  /// value is zero extended, truncated, or left alone to make it that width.
207  ConstantRange zextOrTrunc(uint32_t BitWidth) const;
208
209  /// sextOrTrunc - make this range have the bit width given by \p BitWidth. The
210  /// value is sign extended, truncated, or left alone to make it that width.
211  ConstantRange sextOrTrunc(uint32_t BitWidth) const;
212
213  /// add - Return a new range representing the possible values resulting
214  /// from an addition of a value in this range and a value in \p Other.
215  ConstantRange add(const ConstantRange &Other) const;
216
217  /// sub - Return a new range representing the possible values resulting
218  /// from a subtraction of a value in this range and a value in \p Other.
219  ConstantRange sub(const ConstantRange &Other) const;
220
221  /// multiply - Return a new range representing the possible values resulting
222  /// from a multiplication of a value in this range and a value in \p Other.
223  /// TODO: This isn't fully implemented yet.
224  ConstantRange multiply(const ConstantRange &Other) const;
225
226  /// smax - Return a new range representing the possible values resulting
227  /// from a signed maximum of a value in this range and a value in \p Other.
228  ConstantRange smax(const ConstantRange &Other) const;
229
230  /// umax - Return a new range representing the possible values resulting
231  /// from an unsigned maximum of a value in this range and a value in \p Other.
232  ConstantRange umax(const ConstantRange &Other) const;
233
234  /// udiv - Return a new range representing the possible values resulting
235  /// from an unsigned division of a value in this range and a value in
236  /// \p Other.
237  ConstantRange udiv(const ConstantRange &Other) const;
238
239  /// binaryAnd - return a new range representing the possible values resulting
240  /// from a binary-and of a value in this range by a value in \p Other.
241  ConstantRange binaryAnd(const ConstantRange &Other) const;
242
243  /// binaryOr - return a new range representing the possible values resulting
244  /// from a binary-or of a value in this range by a value in \p Other.
245  ConstantRange binaryOr(const ConstantRange &Other) const;
246
247  /// shl - Return a new range representing the possible values resulting
248  /// from a left shift of a value in this range by a value in \p Other.
249  /// TODO: This isn't fully implemented yet.
250  ConstantRange shl(const ConstantRange &Other) const;
251
252  /// lshr - Return a new range representing the possible values resulting
253  /// from a logical right shift of a value in this range and a value in
254  /// \p Other.
255  ConstantRange lshr(const ConstantRange &Other) const;
256
257  /// inverse - Return a new range that is the logical not of the current set.
258  ///
259  ConstantRange inverse() const;
260
261  /// print - Print out the bounds to a stream...
262  ///
263  void print(raw_ostream &OS) const;
264
265  /// dump - Allow printing from a debugger easily...
266  ///
267  void dump() const;
268};
269
270inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
271  CR.print(OS);
272  return OS;
273}
274
275} // End llvm namespace
276
277#endif
278