fa4235b0cb31b5937fbf990dddd2e4920d0cd6aa
[oota-llvm.git] / include / llvm / Support / ConstantRange.h
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
38 namespace llvm {
39
40 /// ConstantRange - This class represents an range of values.
41 ///
42 class ConstantRange {
43   APInt Lower, Upper;
44   static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
45                                          const ConstantRange &RHS);
46
47 public:
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   /// getLower - Return the lower value for this range...
62   ///
63   const APInt &getLower() const { return Lower; }
64
65   /// getUpper - Return the upper value for this range...
66   ///
67   const APInt &getUpper() const { return Upper; }
68
69   /// getBitWidth - get the bit width of this ConstantRange
70   ///
71   uint32_t getBitWidth() const { return Lower.getBitWidth(); }
72
73   /// isFullSet - Return true if this set contains all of the elements possible
74   /// for this data-type
75   ///
76   bool isFullSet() const;
77
78   /// isEmptySet - Return true if this set contains no members.
79   ///
80   bool isEmptySet() const;
81
82   /// isWrappedSet - Return true if this set wraps around the top of the range,
83   /// for example: [100, 8)
84   ///
85   bool isWrappedSet() const;
86
87   /// contains - Return true if the specified value is in the set.
88   ///
89   bool contains(const APInt &Val) const;
90
91   /// getSingleElement - If this set contains a single element, return it,
92   /// otherwise return null.
93   ///
94   const APInt *getSingleElement() const {
95     if (Upper == Lower + 1)
96       return &Lower;
97     return 0;
98   }
99
100   /// isSingleElement - Return true if this set contains exactly one member.
101   ///
102   bool isSingleElement() const { return getSingleElement() != 0; }
103
104   /// getSetSize - Return the number of elements in this set.
105   ///
106   APInt getSetSize() const;
107
108   /// getUnsignedMax - Return the largest unsigned value contained in the
109   /// ConstantRange.
110   ///
111   APInt getUnsignedMax() const;
112
113   /// getUnsignedMin - Return the smallest unsigned value contained in the
114   /// ConstantRange.
115   ///
116   APInt getUnsignedMin() const;
117
118   /// getSignedMax - Return the largest signed value contained in the
119   /// ConstantRange.
120   ///
121   APInt getSignedMax() const;
122
123   /// getSignedMin - Return the smallest signed value contained in the
124   /// ConstantRange.
125   ///
126   APInt getSignedMin() const;
127
128   /// operator== - Return true if this range is equal to another range.
129   ///
130   bool operator==(const ConstantRange &CR) const {
131     return Lower == CR.Lower && Upper == CR.Upper;
132   }
133   bool operator!=(const ConstantRange &CR) const {
134     return !operator==(CR);
135   }
136
137   /// subtract - Subtract the specified constant from the endpoints of this
138   /// constant range.
139   ConstantRange subtract(const APInt &CI) const;
140
141   /// intersectWith - Return the range that results from the intersection of
142   /// this range with another range.  The resultant range is pruned as much as
143   /// possible, but there may be cases where elements are included that are in
144   /// one of the sets but not the other.  For example: [100, 8) intersect [3,
145   /// 120) yields [3, 120)
146   ///
147   ConstantRange intersectWith(const ConstantRange &CR) const;
148
149   /// maximalIntersectWith - Return the range that results from the intersection
150   /// of this range with another range.  The resultant range is guaranteed to
151   /// include all elements contained in both input ranges, and to have the
152   /// smallest possible set size that does so.  Because there may be two
153   /// intersections with the same set size, A.maximalIntersectWith(B) might not
154   /// be equal to B.maximalIntersectWith(A).
155   ///
156   ConstantRange maximalIntersectWith(const ConstantRange &CR) const;
157
158   /// unionWith - Return the range that results from the union of this range
159   /// with another range.  The resultant range is guaranteed to include the
160   /// elements of both sets, but may contain more.  For example, [3, 9) union
161   /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
162   /// in either set before.
163   ///
164   ConstantRange unionWith(const ConstantRange &CR) const;
165
166   /// zeroExtend - Return a new range in the specified integer type, which must
167   /// be strictly larger than the current type.  The returned range will
168   /// correspond to the possible range of values if the source range had been
169   /// zero extended to BitWidth.
170   ConstantRange zeroExtend(uint32_t BitWidth) const;
171
172   /// signExtend - Return a new range in the specified integer type, which must
173   /// be strictly larger than the current type.  The returned range will
174   /// correspond to the possible range of values if the source range had been
175   /// sign extended to BitWidth.
176   ConstantRange signExtend(uint32_t BitWidth) const;
177
178   /// truncate - Return a new range in the specified integer type, which must be
179   /// strictly smaller than the current type.  The returned range will
180   /// correspond to the possible range of values if the source range had been
181   /// truncated to the specified type.
182   ConstantRange truncate(uint32_t BitWidth) const;
183
184   /// add - Return a new range representing the possible values resulting
185   /// from an addition of a value in this range and a value in Other.
186   ConstantRange add(const ConstantRange &Other) const;
187
188   /// multiply - Return a new range representing the possible values resulting
189   /// from a multiplication of a value in this range and a value in Other.
190   /// TODO: This isn't fully implemented yet.
191   ConstantRange multiply(const ConstantRange &Other) const;
192
193   /// smax - Return a new range representing the possible values resulting
194   /// from a signed maximum of a value in this range and a value in Other.
195   ConstantRange smax(const ConstantRange &Other) const;
196
197   /// umax - Return a new range representing the possible values resulting
198   /// from an unsigned maximum of a value in this range and a value in Other.
199   ConstantRange umax(const ConstantRange &Other) const;
200
201   /// udiv - Return a new range representing the possible values resulting
202   /// from an unsigned division of a value in this range and a value in Other.
203   /// TODO: This isn't fully implemented yet.
204   ConstantRange udiv(const ConstantRange &Other) const;
205
206   /// print - Print out the bounds to a stream...
207   ///
208   void print(raw_ostream &OS) const;
209
210   /// dump - Allow printing from a debugger easily...
211   ///
212   void dump() const;
213 };
214
215 inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
216   CR.print(OS);
217   return OS;
218 }
219
220 std::ostream &operator<<(std::ostream &OS, const ConstantRange &CR);
221
222 } // End llvm namespace
223
224 #endif