1 //===-- llvm/Support/ConstantRange.h - Represent a range --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
17 // [F, F) = {} = Empty set
20 // [T, T) = {F, T} = Full set
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
27 // Note that ConstantRange always keeps unsigned values.
28 //===----------------------------------------------------------------------===//
30 #ifndef LLVM_SUPPORT_CONSTANT_RANGE_H
31 #define LLVM_SUPPORT_CONSTANT_RANGE_H
33 #include "llvm/ADT/APInt.h"
34 #include "llvm/Support/DataTypes.h"
40 static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
41 const ConstantRange &RHS);
43 /// Initialize a full (the default) or empty set for the specified bit width.
45 explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
47 /// Initialize a range to hold the single specified value.
49 ConstantRange(const APInt &Value);
51 /// @brief Initialize a range of values explicitly. This will assert out if
52 /// Lower==Upper and Lower != Min or Max value for its type. It will also
53 /// assert out if the two APInt's are not the same bit width.
54 ConstantRange(const APInt& Lower, const APInt& Upper);
56 /// getLower - Return the lower value for this range...
58 const APInt &getLower() const { return Lower; }
60 /// getUpper - Return the upper value for this range...
62 const APInt &getUpper() const { return Upper; }
64 /// getBitWidth - get the bit width of this ConstantRange
66 uint32_t getBitWidth() const { return Lower.getBitWidth(); }
68 /// isFullSet - Return true if this set contains all of the elements possible
69 /// for this data-type
71 bool isFullSet() const;
73 /// isEmptySet - Return true if this set contains no members.
75 bool isEmptySet() const;
77 /// isWrappedSet - Return true if this set wraps around the top of the range,
78 /// for example: [100, 8)
80 bool isWrappedSet() const;
82 /// contains - Return true if the specified value is in the set.
84 bool contains(const APInt &Val) const;
86 /// getSingleElement - If this set contains a single element, return it,
87 /// otherwise return null.
89 const APInt *getSingleElement() const {
90 if (Upper == Lower + 1)
95 /// isSingleElement - Return true if this set contains exactly one member.
97 bool isSingleElement() const { return getSingleElement() != 0; }
99 /// getSetSize - Return the number of elements in this set.
101 APInt getSetSize() const;
103 /// getUnsignedMax - Return the largest unsigned value contained in the
106 APInt getUnsignedMax() const;
108 /// getUnsignedMin - Return the smallest unsigned value contained in the
111 APInt getUnsignedMin() const;
113 /// getSignedMax - Return the largest signed value contained in the
116 APInt getSignedMax() const;
118 /// getSignedMin - Return the smallest signed value contained in the
121 APInt getSignedMin() const;
123 /// operator== - Return true if this range is equal to another range.
125 bool operator==(const ConstantRange &CR) const {
126 return Lower == CR.Lower && Upper == CR.Upper;
128 bool operator!=(const ConstantRange &CR) const {
129 return !operator==(CR);
132 /// subtract - Subtract the specified constant from the endpoints of this
134 ConstantRange subtract(const APInt &CI) const;
136 /// intersectWith - Return the range that results from the intersection of
137 /// this range with another range. The resultant range is pruned as much as
138 /// possible, but there may be cases where elements are included that are in
139 /// one of the sets but not the other. For example: [100, 8) intersect [3,
140 /// 120) yields [3, 120)
142 ConstantRange intersectWith(const ConstantRange &CR) const;
144 /// maximalIntersectWith - Return the range that results from the intersection
145 /// of this range with another range. The resultant range is guaranteed to
146 /// include all elements contained in both input ranges, and to have the
147 /// smallest possible set size that does so. Because there may be two
148 /// intersections with the same set size, A.maximalIntersectWith(B) might not
149 /// be equal to B.maximalIntersectWith(A).
151 ConstantRange maximalIntersectWith(const ConstantRange &CR) const;
153 /// unionWith - Return the range that results from the union of this range
154 /// with another range. The resultant range is guaranteed to include the
155 /// elements of both sets, but may contain more. For example, [3, 9) union
156 /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
157 /// in either set before.
159 ConstantRange unionWith(const ConstantRange &CR) const;
161 /// zeroExtend - Return a new range in the specified integer type, which must
162 /// be strictly larger than the current type. The returned range will
163 /// correspond to the possible range of values if the source range had been
164 /// zero extended to BitWidth.
165 ConstantRange zeroExtend(uint32_t BitWidth) const;
167 /// signExtend - Return a new range in the specified integer type, which must
168 /// be strictly larger than the current type. The returned range will
169 /// correspond to the possible range of values if the source range had been
170 /// sign extended to BitWidth.
171 ConstantRange signExtend(uint32_t BitWidth) const;
173 /// truncate - Return a new range in the specified integer type, which must be
174 /// strictly smaller than the current type. The returned range will
175 /// correspond to the possible range of values if the source range had been
176 /// truncated to the specified type.
177 ConstantRange truncate(uint32_t BitWidth) const;
179 /// print - Print out the bounds to a stream...
181 void print(raw_ostream &OS) const;
183 /// dump - Allow printing from a debugger easily...
188 inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
193 } // End llvm namespace