1 //===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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
15 // ranges (other integral ranges use min/max values for special range values):
17 // [F, F) = {} = Empty set
20 // [T, T) = {F, T} = Full set
22 //===----------------------------------------------------------------------===//
24 #include "llvm/Support/ConstantRange.h"
25 #include "llvm/Constants.h"
26 #include "llvm/Instruction.h"
27 #include "llvm/Instructions.h"
28 #include "llvm/Type.h"
29 #include "llvm/Support/Streams.h"
33 static ConstantInt *getMaxValue(const Type *Ty, bool isSigned = false) {
34 if (Ty == Type::Int1Ty)
35 return ConstantInt::getTrue();
36 if (Ty->isInteger()) {
38 // Calculate 011111111111111...
39 unsigned TypeBits = Ty->getPrimitiveSizeInBits();
40 int64_t Val = INT64_MAX; // All ones
41 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
42 return ConstantInt::get(Ty, Val);
44 return ConstantInt::getAllOnesValue(Ty);
49 // Static constructor to create the minimum constant for an integral type...
50 static ConstantInt *getMinValue(const Type *Ty, bool isSigned = false) {
51 if (Ty == Type::Int1Ty)
52 return ConstantInt::getFalse();
53 if (Ty->isInteger()) {
55 // Calculate 1111111111000000000000
56 unsigned TypeBits = Ty->getPrimitiveSizeInBits();
57 int64_t Val = -1; // All ones
58 Val <<= TypeBits-1; // Shift over to the right spot
59 return ConstantInt::get(Ty, Val);
61 return ConstantInt::get(Ty, 0);
65 static ConstantInt *Next(ConstantInt *CI) {
66 if (CI->getType() == Type::Int1Ty)
67 return ConstantInt::get(!CI->getBoolValue());
69 Constant *Result = ConstantExpr::getAdd(CI,
70 ConstantInt::get(CI->getType(), 1));
71 return cast<ConstantInt>(Result);
74 static bool LT(ConstantInt *A, ConstantInt *B, bool isSigned) {
75 Constant *C = ConstantExpr::getICmp(
76 (isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT), A, B);
77 assert(isa<ConstantInt>(C) && "Constant folding of integrals not impl??");
78 return cast<ConstantInt>(C)->getBoolValue();
81 static bool LTE(ConstantInt *A, ConstantInt *B, bool isSigned) {
82 Constant *C = ConstantExpr::getICmp(
83 (isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE), A, B);
84 assert(isa<ConstantInt>(C) && "Constant folding of integrals not impl??");
85 return cast<ConstantInt>(C)->getBoolValue();
88 static bool GT(ConstantInt *A, ConstantInt *B, bool isSigned) {
89 return LT(B, A, isSigned); }
91 static ConstantInt *Min(ConstantInt *A, ConstantInt *B,
93 return LT(A, B, isSigned) ? A : B;
95 static ConstantInt *Max(ConstantInt *A, ConstantInt *B,
97 return GT(A, B, isSigned) ? A : B;
100 /// Initialize a full (the default) or empty set for the specified type.
102 ConstantRange::ConstantRange(const Type *Ty, bool Full) {
103 assert(Ty->isIntegral() &&
104 "Cannot make constant range of non-integral type!");
106 Lower = Upper = getMaxValue(Ty);
108 Lower = Upper = getMinValue(Ty);
111 /// Initialize a range to hold the single specified value.
113 ConstantRange::ConstantRange(Constant *V)
114 : Lower(cast<ConstantInt>(V)), Upper(Next(cast<ConstantInt>(V))) { }
116 /// Initialize a range of values explicitly... this will assert out if
117 /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
118 /// have different types)
120 ConstantRange::ConstantRange(Constant *L, Constant *U)
121 : Lower(cast<ConstantInt>(L)), Upper(cast<ConstantInt>(U)) {
122 assert(Lower->getType() == Upper->getType() &&
123 "Incompatible types for ConstantRange!");
125 // Make sure that if L & U are equal that they are either Min or Max...
126 assert((L != U || (L == getMaxValue(L->getType()) ||
127 L == getMinValue(L->getType())))
128 && "Lower == Upper, but they aren't min or max for type!");
131 /// Initialize a set of values that all satisfy the condition with C.
133 ConstantRange::ConstantRange(unsigned short ICmpOpcode, ConstantInt *C) {
134 switch (ICmpOpcode) {
135 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
136 case ICmpInst::ICMP_EQ: Lower = C; Upper = Next(C); return;
137 case ICmpInst::ICMP_NE: Upper = C; Lower = Next(C); return;
138 case ICmpInst::ICMP_ULT:
139 Lower = getMinValue(C->getType());
142 case ICmpInst::ICMP_SLT:
143 Lower = getMinValue(C->getType(), true);
146 case ICmpInst::ICMP_UGT:
148 Upper = getMinValue(C->getType()); // Min = Next(Max)
150 case ICmpInst::ICMP_SGT:
152 Upper = getMinValue(C->getType(), true); // Min = Next(Max)
154 case ICmpInst::ICMP_ULE:
155 Lower = getMinValue(C->getType());
158 case ICmpInst::ICMP_SLE:
159 Lower = getMinValue(C->getType(), true);
162 case ICmpInst::ICMP_UGE:
164 Upper = getMinValue(C->getType()); // Min = Next(Max)
166 case ICmpInst::ICMP_SGE:
168 Upper = getMinValue(C->getType(), true); // Min = Next(Max)
173 /// getType - Return the LLVM data type of this range.
175 const Type *ConstantRange::getType() const { return Lower->getType(); }
177 /// isFullSet - Return true if this set contains all of the elements possible
178 /// for this data-type
179 bool ConstantRange::isFullSet() const {
180 return Lower == Upper && Lower == getMaxValue(getType());
183 /// isEmptySet - Return true if this set contains no members.
185 bool ConstantRange::isEmptySet() const {
186 return Lower == Upper && Lower == getMinValue(getType());
189 /// isWrappedSet - Return true if this set wraps around the top of the range,
190 /// for example: [100, 8)
192 bool ConstantRange::isWrappedSet(bool isSigned) const {
193 return GT(Lower, Upper, isSigned);
196 /// getSingleElement - If this set contains a single element, return it,
197 /// otherwise return null.
198 ConstantInt *ConstantRange::getSingleElement() const {
199 if (Upper == Next(Lower)) // Is it a single element range?
204 /// getSetSize - Return the number of elements in this set.
206 uint64_t ConstantRange::getSetSize() const {
207 if (isEmptySet()) return 0;
208 if (getType() == Type::Int1Ty) {
209 if (Lower != Upper) // One of T or F in the set...
211 return 2; // Must be full set...
214 // Simply subtract the bounds...
215 Constant *Result = ConstantExpr::getSub(Upper, Lower);
216 return cast<ConstantInt>(Result)->getZExtValue();
219 /// contains - Return true if the specified value is in the set.
221 bool ConstantRange::contains(ConstantInt *Val, bool isSigned) const {
222 if (Lower == Upper) {
223 if (isFullSet()) return true;
227 if (!isWrappedSet(isSigned))
228 return LTE(Lower, Val, isSigned) && LT(Val, Upper, isSigned);
229 return LTE(Lower, Val, isSigned) || LT(Val, Upper, isSigned);
232 /// subtract - Subtract the specified constant from the endpoints of this
234 ConstantRange ConstantRange::subtract(ConstantInt *CI) const {
235 assert(CI->getType() == getType() && getType()->isInteger() &&
236 "Cannot subtract from different type range or non-integer!");
237 // If the set is empty or full, don't modify the endpoints.
238 if (Lower == Upper) return *this;
239 return ConstantRange(ConstantExpr::getSub(Lower, CI),
240 ConstantExpr::getSub(Upper, CI));
244 // intersect1Wrapped - This helper function is used to intersect two ranges when
245 // it is known that LHS is wrapped and RHS isn't.
247 static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
248 const ConstantRange &RHS,
250 assert(LHS.isWrappedSet(isSigned) && !RHS.isWrappedSet(isSigned));
252 // Check to see if we overlap on the Left side of RHS...
254 if (LT(RHS.getLower(), LHS.getUpper(), isSigned)) {
255 // We do overlap on the left side of RHS, see if we overlap on the right of
257 if (GT(RHS.getUpper(), LHS.getLower(), isSigned)) {
258 // Ok, the result overlaps on both the left and right sides. See if the
259 // resultant interval will be smaller if we wrap or not...
261 if (LHS.getSetSize() < RHS.getSetSize())
267 // No overlap on the right, just on the left.
268 return ConstantRange(RHS.getLower(), LHS.getUpper());
271 // We don't overlap on the left side of RHS, see if we overlap on the right
273 if (GT(RHS.getUpper(), LHS.getLower(), isSigned)) {
275 return ConstantRange(LHS.getLower(), RHS.getUpper());
278 return ConstantRange(LHS.getType(), false);
283 /// intersect - Return the range that results from the intersection of this
284 /// range with another range.
286 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR,
287 bool isSigned) const {
288 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
289 // Handle common special cases
290 if (isEmptySet() || CR.isFullSet()) return *this;
291 if (isFullSet() || CR.isEmptySet()) return CR;
293 if (!isWrappedSet(isSigned)) {
294 if (!CR.isWrappedSet(isSigned)) {
295 ConstantInt *L = Max(Lower, CR.Lower, isSigned);
296 ConstantInt *U = Min(Upper, CR.Upper, isSigned);
298 if (LT(L, U, isSigned)) // If range isn't empty...
299 return ConstantRange(L, U);
301 return ConstantRange(getType(), false); // Otherwise, return empty set
303 return intersect1Wrapped(CR, *this, isSigned);
304 } else { // We know "this" is wrapped...
305 if (!CR.isWrappedSet(isSigned))
306 return intersect1Wrapped(*this, CR, isSigned);
308 // Both ranges are wrapped...
309 ConstantInt *L = Max(Lower, CR.Lower, isSigned);
310 ConstantInt *U = Min(Upper, CR.Upper, isSigned);
311 return ConstantRange(L, U);
317 /// union - Return the range that results from the union of this range with
318 /// another range. The resultant range is guaranteed to include the elements of
319 /// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
320 /// 15), which includes 9, 10, and 11, which were not included in either set
323 ConstantRange ConstantRange::unionWith(const ConstantRange &CR,
324 bool isSigned) const {
325 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
327 assert(0 && "Range union not implemented yet!");
332 /// zeroExtend - Return a new range in the specified integer type, which must
333 /// be strictly larger than the current type. The returned range will
334 /// correspond to the possible range of values as if the source range had been
336 ConstantRange ConstantRange::zeroExtend(const Type *Ty) const {
337 unsigned SrcTySize = getLower()->getType()->getPrimitiveSizeInBits();
338 assert(SrcTySize < Ty->getPrimitiveSizeInBits() && "Not a value extension");
340 // Change a source full set into [0, 1 << 8*numbytes)
341 return ConstantRange(Constant::getNullValue(Ty),
342 ConstantInt::get(Ty, 1ULL << SrcTySize));
345 Constant *Lower = getLower();
346 Constant *Upper = getUpper();
348 return ConstantRange(ConstantExpr::getZExt(Lower, Ty),
349 ConstantExpr::getZExt(Upper, Ty));
352 /// truncate - Return a new range in the specified integer type, which must be
353 /// strictly smaller than the current type. The returned range will
354 /// correspond to the possible range of values as if the source range had been
355 /// truncated to the specified type.
356 ConstantRange ConstantRange::truncate(const Type *Ty) const {
357 unsigned SrcTySize = getLower()->getType()->getPrimitiveSizeInBits();
358 assert(SrcTySize > Ty->getPrimitiveSizeInBits() && "Not a value truncation");
359 uint64_t Size = 1ULL << Ty->getPrimitiveSizeInBits();
360 if (isFullSet() || getSetSize() >= Size)
361 return ConstantRange(getType());
363 return ConstantRange(
364 ConstantExpr::getTrunc(getLower(), Ty),
365 ConstantExpr::getTrunc(getUpper(), Ty));
368 /// print - Print out the bounds to a stream...
370 void ConstantRange::print(std::ostream &OS) const {
371 OS << "[" << *Lower << "," << *Upper << " )";
374 /// dump - Allow printing from a debugger easily...
376 void ConstantRange::dump() const {