blob: a8ffa5813e728352826ddf7542d133173752ce01 [file] [log] [blame]
Chris Lattner645e00d2002-09-01 23:53:36 +00001//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner645e00d2002-09-01 23:53:36 +00009//
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):
16//
17// [F, F) = {} = Empty set
18// [T, F) = {T}
19// [F, T) = {F}
20// [T, T) = {F, T} = Full set
21//
22//===----------------------------------------------------------------------===//
23
24#include "llvm/Support/ConstantRange.h"
Chris Lattner67bb7602004-01-12 20:13:04 +000025#include "llvm/Constants.h"
Chris Lattner645e00d2002-09-01 23:53:36 +000026#include "llvm/Instruction.h"
Reid Spencere4d87aa2006-12-23 06:05:41 +000027#include "llvm/Instructions.h"
Chris Lattner67bb7602004-01-12 20:13:04 +000028#include "llvm/Type.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000029#include "llvm/Support/Streams.h"
30#include <ostream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Reid Spencere4d87aa2006-12-23 06:05:41 +000033static ConstantIntegral *getMaxValue(const Type *Ty, bool isSigned = false) {
34 if (Ty == Type::BoolTy)
35 return ConstantBool::getTrue();
36 if (Ty->isInteger()) {
37 if (isSigned) {
38 // Calculate 011111111111111...
Reid Spencere7ca0422007-01-08 01:26:33 +000039 unsigned TypeBits = Ty->getPrimitiveSizeInBits();
Reid Spencere4d87aa2006-12-23 06:05:41 +000040 int64_t Val = INT64_MAX; // All ones
41 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
42 return ConstantInt::get(Ty, Val);
43 }
44 return ConstantInt::getAllOnesValue(Ty);
Reid Spencerc6bf4bf2006-12-06 20:45:15 +000045 }
Reid Spencere4d87aa2006-12-23 06:05:41 +000046 return 0;
Reid Spencerc6bf4bf2006-12-06 20:45:15 +000047}
48
49// Static constructor to create the minimum constant for an integral type...
Reid Spencere4d87aa2006-12-23 06:05:41 +000050static ConstantIntegral *getMinValue(const Type *Ty, bool isSigned = false) {
51 if (Ty == Type::BoolTy)
52 return ConstantBool::getFalse();
53 if (Ty->isInteger()) {
54 if (isSigned) {
55 // Calculate 1111111111000000000000
Reid Spencere7ca0422007-01-08 01:26:33 +000056 unsigned TypeBits = Ty->getPrimitiveSizeInBits();
Reid Spencere4d87aa2006-12-23 06:05:41 +000057 int64_t Val = -1; // All ones
58 Val <<= TypeBits-1; // Shift over to the right spot
59 return ConstantInt::get(Ty, Val);
60 }
61 return ConstantInt::get(Ty, 0);
Reid Spencerc6bf4bf2006-12-06 20:45:15 +000062 }
Reid Spencere4d87aa2006-12-23 06:05:41 +000063 return 0;
Reid Spencerc6bf4bf2006-12-06 20:45:15 +000064}
Chris Lattnerfc33d302004-03-30 00:20:08 +000065static ConstantIntegral *Next(ConstantIntegral *CI) {
Chris Lattner193c2d82006-09-28 23:14:29 +000066 if (ConstantBool *CB = dyn_cast<ConstantBool>(CI))
67 return ConstantBool::get(!CB->getValue());
Misha Brukmanfd939082005-04-21 23:48:37 +000068
Chris Lattnerfc33d302004-03-30 00:20:08 +000069 Constant *Result = ConstantExpr::getAdd(CI,
70 ConstantInt::get(CI->getType(), 1));
71 return cast<ConstantIntegral>(Result);
72}
73
Reid Spencere4d87aa2006-12-23 06:05:41 +000074static bool LT(ConstantIntegral *A, ConstantIntegral *B, bool isSigned) {
75 Constant *C = ConstantExpr::getICmp(
76 (isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT), A, B);
Chris Lattner67bb7602004-01-12 20:13:04 +000077 assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
78 return cast<ConstantBool>(C)->getValue();
79}
80
Reid Spencere4d87aa2006-12-23 06:05:41 +000081static bool LTE(ConstantIntegral *A, ConstantIntegral *B, bool isSigned) {
82 Constant *C = ConstantExpr::getICmp(
83 (isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE), A, B);
Chris Lattner67bb7602004-01-12 20:13:04 +000084 assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
85 return cast<ConstantBool>(C)->getValue();
86}
87
Reid Spencere4d87aa2006-12-23 06:05:41 +000088static bool GT(ConstantIntegral *A, ConstantIntegral *B, bool isSigned) {
89 return LT(B, A, isSigned); }
Chris Lattnerfc33d302004-03-30 00:20:08 +000090
Reid Spencere4d87aa2006-12-23 06:05:41 +000091static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B,
92 bool isSigned) {
93 return LT(A, B, isSigned) ? A : B;
Chris Lattner67bb7602004-01-12 20:13:04 +000094}
Reid Spencere4d87aa2006-12-23 06:05:41 +000095static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B,
96 bool isSigned) {
97 return GT(A, B, isSigned) ? A : B;
Chris Lattner67bb7602004-01-12 20:13:04 +000098}
99
Chris Lattner645e00d2002-09-01 23:53:36 +0000100/// Initialize a full (the default) or empty set for the specified type.
101///
102ConstantRange::ConstantRange(const Type *Ty, bool Full) {
103 assert(Ty->isIntegral() &&
104 "Cannot make constant range of non-integral type!");
105 if (Full)
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000106 Lower = Upper = getMaxValue(Ty);
Chris Lattner645e00d2002-09-01 23:53:36 +0000107 else
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000108 Lower = Upper = getMinValue(Ty);
Chris Lattner645e00d2002-09-01 23:53:36 +0000109}
110
Chris Lattnerfc33d302004-03-30 00:20:08 +0000111/// Initialize a range to hold the single specified value.
112///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000113ConstantRange::ConstantRange(Constant *V)
114 : Lower(cast<ConstantIntegral>(V)), Upper(Next(cast<ConstantIntegral>(V))) { }
Chris Lattnerfc33d302004-03-30 00:20:08 +0000115
Chris Lattner645e00d2002-09-01 23:53:36 +0000116/// 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)
119///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000120ConstantRange::ConstantRange(Constant *L, Constant *U)
Chris Lattnerdb813952004-03-29 20:42:49 +0000121 : Lower(cast<ConstantIntegral>(L)), Upper(cast<ConstantIntegral>(U)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000122 assert(Lower->getType() == Upper->getType() &&
123 "Incompatible types for ConstantRange!");
Misha Brukmanfd939082005-04-21 23:48:37 +0000124
Chris Lattner645e00d2002-09-01 23:53:36 +0000125 // Make sure that if L & U are equal that they are either Min or Max...
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000126 assert((L != U || (L == getMaxValue(L->getType()) ||
Reid Spencere4d87aa2006-12-23 06:05:41 +0000127 L == getMinValue(L->getType())))
128 && "Lower == Upper, but they aren't min or max for type!");
Chris Lattner645e00d2002-09-01 23:53:36 +0000129}
130
Chris Lattner645e00d2002-09-01 23:53:36 +0000131/// Initialize a set of values that all satisfy the condition with C.
132///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000133ConstantRange::ConstantRange(unsigned short ICmpOpcode, ConstantIntegral *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:
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000139 Lower = getMinValue(C->getType());
Chris Lattner645e00d2002-09-01 23:53:36 +0000140 Upper = C;
141 return;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000142 case ICmpInst::ICMP_SLT:
143 Lower = getMinValue(C->getType(), true);
144 Upper = C;
Chris Lattner645e00d2002-09-01 23:53:36 +0000145 return;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000146 case ICmpInst::ICMP_UGT:
147 Lower = Next(C);
148 Upper = getMinValue(C->getType()); // Min = Next(Max)
149 return;
150 case ICmpInst::ICMP_SGT:
151 Lower = Next(C);
152 Upper = getMinValue(C->getType(), true); // Min = Next(Max)
153 return;
154 case ICmpInst::ICMP_ULE:
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000155 Lower = getMinValue(C->getType());
Chris Lattner645e00d2002-09-01 23:53:36 +0000156 Upper = Next(C);
157 return;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000158 case ICmpInst::ICMP_SLE:
159 Lower = getMinValue(C->getType(), true);
160 Upper = Next(C);
161 return;
162 case ICmpInst::ICMP_UGE:
Chris Lattner645e00d2002-09-01 23:53:36 +0000163 Lower = C;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000164 Upper = getMinValue(C->getType()); // Min = Next(Max)
165 return;
166 case ICmpInst::ICMP_SGE:
167 Lower = C;
168 Upper = getMinValue(C->getType(), true); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +0000169 return;
170 }
171}
172
173/// getType - Return the LLVM data type of this range.
174///
175const Type *ConstantRange::getType() const { return Lower->getType(); }
176
177/// isFullSet - Return true if this set contains all of the elements possible
178/// for this data-type
179bool ConstantRange::isFullSet() const {
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000180 return Lower == Upper && Lower == getMaxValue(getType());
Chris Lattner645e00d2002-09-01 23:53:36 +0000181}
Misha Brukmanfd939082005-04-21 23:48:37 +0000182
Chris Lattner645e00d2002-09-01 23:53:36 +0000183/// isEmptySet - Return true if this set contains no members.
184///
185bool ConstantRange::isEmptySet() const {
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000186 return Lower == Upper && Lower == getMinValue(getType());
Chris Lattner645e00d2002-09-01 23:53:36 +0000187}
188
189/// isWrappedSet - Return true if this set wraps around the top of the range,
190/// for example: [100, 8)
191///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000192bool ConstantRange::isWrappedSet(bool isSigned) const {
193 return GT(Lower, Upper, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000194}
195
Chris Lattner645e00d2002-09-01 23:53:36 +0000196/// getSingleElement - If this set contains a single element, return it,
197/// otherwise return null.
198ConstantIntegral *ConstantRange::getSingleElement() const {
199 if (Upper == Next(Lower)) // Is it a single element range?
200 return Lower;
201 return 0;
202}
203
204/// getSetSize - Return the number of elements in this set.
205///
206uint64_t ConstantRange::getSetSize() const {
207 if (isEmptySet()) return 0;
208 if (getType() == Type::BoolTy) {
209 if (Lower != Upper) // One of T or F in the set...
210 return 1;
211 return 2; // Must be full set...
212 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000213
Chris Lattner645e00d2002-09-01 23:53:36 +0000214 // Simply subtract the bounds...
Chris Lattnerfc33d302004-03-30 00:20:08 +0000215 Constant *Result = ConstantExpr::getSub(Upper, Lower);
Reid Spencerb83eb642006-10-20 07:07:24 +0000216 return cast<ConstantInt>(Result)->getZExtValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000217}
218
Chris Lattnerfc33d302004-03-30 00:20:08 +0000219/// contains - Return true if the specified value is in the set.
220///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000221bool ConstantRange::contains(ConstantInt *Val, bool isSigned) const {
Chris Lattnerfc33d302004-03-30 00:20:08 +0000222 if (Lower == Upper) {
223 if (isFullSet()) return true;
224 return false;
225 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000226
Reid Spencere4d87aa2006-12-23 06:05:41 +0000227 if (!isWrappedSet(isSigned))
228 return LTE(Lower, Val, isSigned) && LT(Val, Upper, isSigned);
229 return LTE(Lower, Val, isSigned) || LT(Val, Upper, isSigned);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000230}
231
Chris Lattnerfc33d302004-03-30 00:20:08 +0000232/// subtract - Subtract the specified constant from the endpoints of this
233/// constant range.
234ConstantRange 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));
241}
Chris Lattner645e00d2002-09-01 23:53:36 +0000242
243
244// intersect1Wrapped - This helper function is used to intersect two ranges when
245// it is known that LHS is wrapped and RHS isn't.
246//
247static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000248 const ConstantRange &RHS,
249 bool isSigned) {
250 assert(LHS.isWrappedSet(isSigned) && !RHS.isWrappedSet(isSigned));
Chris Lattner645e00d2002-09-01 23:53:36 +0000251
Chris Lattner645e00d2002-09-01 23:53:36 +0000252 // Check to see if we overlap on the Left side of RHS...
253 //
Reid Spencere4d87aa2006-12-23 06:05:41 +0000254 if (LT(RHS.getLower(), LHS.getUpper(), isSigned)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000255 // We do overlap on the left side of RHS, see if we overlap on the right of
256 // RHS...
Reid Spencere4d87aa2006-12-23 06:05:41 +0000257 if (GT(RHS.getUpper(), LHS.getLower(), isSigned)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000258 // 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...
260 //
261 if (LHS.getSetSize() < RHS.getSetSize())
262 return LHS;
263 else
264 return RHS;
265
266 } else {
267 // No overlap on the right, just on the left.
268 return ConstantRange(RHS.getLower(), LHS.getUpper());
269 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000270 } else {
271 // We don't overlap on the left side of RHS, see if we overlap on the right
272 // of RHS...
Reid Spencere4d87aa2006-12-23 06:05:41 +0000273 if (GT(RHS.getUpper(), LHS.getLower(), isSigned)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000274 // Simple overlap...
275 return ConstantRange(LHS.getLower(), RHS.getUpper());
276 } else {
277 // No overlap...
278 return ConstantRange(LHS.getType(), false);
279 }
280 }
281}
282
Chris Lattner645e00d2002-09-01 23:53:36 +0000283/// intersect - Return the range that results from the intersection of this
284/// range with another range.
285///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000286ConstantRange ConstantRange::intersectWith(const ConstantRange &CR,
287 bool isSigned) const {
Chris Lattner645e00d2002-09-01 23:53:36 +0000288 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000289 // Handle common special cases
290 if (isEmptySet() || CR.isFullSet()) return *this;
291 if (isFullSet() || CR.isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000292
Reid Spencere4d87aa2006-12-23 06:05:41 +0000293 if (!isWrappedSet(isSigned)) {
294 if (!CR.isWrappedSet(isSigned)) {
295 ConstantIntegral *L = Max(Lower, CR.Lower, isSigned);
296 ConstantIntegral *U = Min(Upper, CR.Upper, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000297
Reid Spencere4d87aa2006-12-23 06:05:41 +0000298 if (LT(L, U, isSigned)) // If range isn't empty...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000299 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000300 else
301 return ConstantRange(getType(), false); // Otherwise, return empty set
302 } else
Reid Spencere4d87aa2006-12-23 06:05:41 +0000303 return intersect1Wrapped(CR, *this, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000304 } else { // We know "this" is wrapped...
Reid Spencere4d87aa2006-12-23 06:05:41 +0000305 if (!CR.isWrappedSet(isSigned))
306 return intersect1Wrapped(*this, CR, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000307 else {
308 // Both ranges are wrapped...
Reid Spencere4d87aa2006-12-23 06:05:41 +0000309 ConstantIntegral *L = Max(Lower, CR.Lower, isSigned);
310 ConstantIntegral *U = Min(Upper, CR.Upper, isSigned);
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000311 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000312 }
313 }
314 return *this;
315}
316
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
321/// before.
322///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000323ConstantRange ConstantRange::unionWith(const ConstantRange &CR,
324 bool isSigned) const {
Chris Lattner645e00d2002-09-01 23:53:36 +0000325 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
326
327 assert(0 && "Range union not implemented yet!");
328
329 return *this;
330}
Chris Lattner96f9d722002-09-02 00:18:22 +0000331
Chris Lattnerfc33d302004-03-30 00:20:08 +0000332/// zeroExtend - Return a new range in the specified integer type, which must
333/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000334/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000335/// zero extended.
336ConstantRange ConstantRange::zeroExtend(const Type *Ty) const {
Reid Spencere7ca0422007-01-08 01:26:33 +0000337 unsigned SrcTySize = getLower()->getType()->getPrimitiveSizeInBits();
338 assert(SrcTySize < Ty->getPrimitiveSizeInBits() && "Not a value extension");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000339 if (isFullSet()) {
340 // Change a source full set into [0, 1 << 8*numbytes)
Chris Lattnerfc33d302004-03-30 00:20:08 +0000341 return ConstantRange(Constant::getNullValue(Ty),
Reid Spencere7ca0422007-01-08 01:26:33 +0000342 ConstantInt::get(Ty, 1ULL << SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000343 }
344
345 Constant *Lower = getLower();
346 Constant *Upper = getUpper();
Chris Lattnerfc33d302004-03-30 00:20:08 +0000347
Reid Spencerd977d862006-12-12 23:36:14 +0000348 return ConstantRange(ConstantExpr::getZExt(Lower, Ty),
349 ConstantExpr::getZExt(Upper, Ty));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000350}
351
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
Reid Spencere4d87aa2006-12-23 06:05:41 +0000354/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000355/// truncated to the specified type.
356ConstantRange ConstantRange::truncate(const Type *Ty) const {
Reid Spencere7ca0422007-01-08 01:26:33 +0000357 unsigned SrcTySize = getLower()->getType()->getPrimitiveSizeInBits();
Reid Spencerca7ad892007-01-08 05:34:39 +0000358 assert(SrcTySize > Ty->getPrimitiveSizeInBits() && "Not a value truncation");
Reid Spencere7ca0422007-01-08 01:26:33 +0000359 uint64_t Size = 1ULL << Ty->getPrimitiveSizeInBits();
Chris Lattnerfc33d302004-03-30 00:20:08 +0000360 if (isFullSet() || getSetSize() >= Size)
361 return ConstantRange(getType());
362
Reid Spencer575d95c2006-12-04 02:46:44 +0000363 return ConstantRange(
Reid Spencerd977d862006-12-12 23:36:14 +0000364 ConstantExpr::getTrunc(getLower(), Ty),
365 ConstantExpr::getTrunc(getUpper(), Ty));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000366}
367
Chris Lattner96f9d722002-09-02 00:18:22 +0000368/// print - Print out the bounds to a stream...
369///
370void ConstantRange::print(std::ostream &OS) const {
Chris Lattnerce36d552004-07-15 01:29:12 +0000371 OS << "[" << *Lower << "," << *Upper << " )";
Chris Lattner96f9d722002-09-02 00:18:22 +0000372}
373
374/// dump - Allow printing from a debugger easily...
375///
376void ConstantRange::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000377 print(cerr);
Chris Lattner96f9d722002-09-02 00:18:22 +0000378}