Add explicit keywords.
[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 always keeps unsigned values.
28 //===----------------------------------------------------------------------===//
29
30 #ifndef LLVM_SUPPORT_CONSTANT_RANGE_H
31 #define LLVM_SUPPORT_CONSTANT_RANGE_H
32
33 #include "llvm/ADT/APInt.h"
34 #include "llvm/Support/DataTypes.h"
35 #include "llvm/Support/Streams.h"
36 #include <iosfwd>
37
38 namespace llvm {
39
40 class ConstantRange {
41   APInt Lower, Upper;
42   static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
43                                          const ConstantRange &RHS);
44  public:
45   /// Initialize a full (the default) or empty set for the specified bit width.
46   ///
47   explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
48
49   /// Initialize a range to hold the single specified value.
50   ///
51   ConstantRange(const APInt &Value);
52
53   /// @brief Initialize a range of values explicitly. This will assert out if
54   /// Lower==Upper and Lower != Min or Max value for its type. It will also
55   /// assert out if the two APInt's are not the same bit width.
56   ConstantRange(const APInt& Lower, const APInt& Upper);
57
58   /// getLower - Return the lower value for this range...
59   ///
60   const APInt &getLower() const { return Lower; }
61
62   /// getUpper - Return the upper value for this range...
63   ///
64   const APInt &getUpper() const { return Upper; } 
65
66   /// getBitWidth - get the bit width of this ConstantRange
67   ///
68   uint32_t getBitWidth() const { return Lower.getBitWidth(); }
69
70   /// isFullSet - Return true if this set contains all of the elements possible
71   /// for this data-type
72   ///
73   bool isFullSet() const;
74
75   /// isEmptySet - Return true if this set contains no members.
76   ///
77   bool isEmptySet() const;
78
79   /// isWrappedSet - Return true if this set wraps around the top of the range,
80   /// for example: [100, 8)
81   ///
82   bool isWrappedSet() const;
83
84   /// contains - Return true if the specified value is in the set.
85   ///
86   bool contains(const APInt &Val) const;
87
88   /// getSingleElement - If this set contains a single element, return it,
89   /// otherwise return null.
90   ///
91   const APInt *getSingleElement() const {
92     if (Upper == Lower + 1)
93       return &Lower;
94     return 0;
95   }
96
97   /// isSingleElement - Return true if this set contains exactly one member.
98   ///
99   bool isSingleElement() const { return getSingleElement() != 0; }
100
101   /// getSetSize - Return the number of elements in this set.
102   ///
103   APInt getSetSize() const;
104
105   /// getUnsignedMax - Return the largest unsigned value contained in the
106   /// ConstantRange.
107   ///
108   APInt getUnsignedMax() const;
109
110   /// getUnsignedMin - Return the smallest unsigned value contained in the
111   /// ConstantRange.
112   ///
113   APInt getUnsignedMin() const;
114
115   /// getSignedMax - Return the largest signed value contained in the
116   /// ConstantRange.
117   ///
118   APInt getSignedMax() const;
119
120   /// getSignedMin - Return the smallest signed value contained in the
121   /// ConstantRange.
122   ///
123   APInt getSignedMin() const;
124
125   /// operator== - Return true if this range is equal to another range.
126   ///
127   bool operator==(const ConstantRange &CR) const {
128     return Lower == CR.Lower && Upper == CR.Upper;
129   }
130   bool operator!=(const ConstantRange &CR) const {
131     return !operator==(CR);
132   }
133
134   /// subtract - Subtract the specified constant from the endpoints of this
135   /// constant range.
136   ConstantRange subtract(const APInt &CI) const;
137
138   /// intersectWith - Return the range that results from the intersection of
139   /// this range with another range.  The resultant range is pruned as much as
140   /// possible, but there may be cases where elements are included that are in
141   /// one of the sets but not the other.  For example: [100, 8) intersect [3,
142   /// 120) yields [3, 120)
143   ///
144   ConstantRange intersectWith(const ConstantRange &CR) const;
145
146   /// maximalIntersectWith - Return the range that results from the intersection
147   /// of this range with another range.  The resultant range is guaranteed to
148   /// include all elements contained in both input ranges, and to have the
149   /// smallest possible set size that does so.  Because there may be two
150   /// intersections with the same set size, A.maximalIntersectWith(B) might not
151   /// be equal to B.maximalIntersectWith(A).
152   ///
153   ConstantRange maximalIntersectWith(const ConstantRange &CR) const;
154
155   /// unionWith - Return the range that results from the union of this range
156   /// with another range.  The resultant range is guaranteed to include the
157   /// elements of both sets, but may contain more.  For example, [3, 9) union
158   /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
159   /// in either set before.
160   ///
161   ConstantRange unionWith(const ConstantRange &CR) const;
162
163   /// zeroExtend - Return a new range in the specified integer type, which must
164   /// be strictly larger than the current type.  The returned range will
165   /// correspond to the possible range of values if the source range had been
166   /// zero extended to BitWidth.
167   ConstantRange zeroExtend(uint32_t BitWidth) const;
168
169   /// signExtend - Return a new range in the specified integer type, which must
170   /// be strictly larger than the current type.  The returned range will
171   /// correspond to the possible range of values if the source range had been
172   /// sign extended to BitWidth.
173   ConstantRange signExtend(uint32_t BitWidth) const;
174
175   /// truncate - Return a new range in the specified integer type, which must be
176   /// strictly smaller than the current type.  The returned range will
177   /// correspond to the possible range of values if the source range had been
178   /// truncated to the specified type.
179   ConstantRange truncate(uint32_t BitWidth) const;
180
181   /// print - Print out the bounds to a stream...
182   ///
183   void print(std::ostream &OS) const;
184   void print(std::ostream *OS) const { if (OS) print(*OS); }
185
186   /// dump - Allow printing from a debugger easily...
187   ///
188   void dump() const;
189 };
190
191 inline std::ostream &operator<<(std::ostream &OS, const ConstantRange &CR) {
192   CR.print(OS);
193   return OS;
194 }
195
196 } // End llvm namespace
197
198 #endif