blob: c6948576f8ba13a7c3e6367d54352d8621054468 [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"
Reid Spencerc1030572007-01-19 21:13:56 +000029#include "llvm/DerivedTypes.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000030#include "llvm/Support/Streams.h"
31#include <ostream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000034static ConstantInt *getMaxValue(const Type *Ty, bool isSigned = false) {
Chris Lattner42a75512007-01-15 02:27:26 +000035 if (Ty->isInteger()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +000036 if (isSigned) {
37 // Calculate 011111111111111...
Reid Spencere7ca0422007-01-08 01:26:33 +000038 unsigned TypeBits = Ty->getPrimitiveSizeInBits();
Reid Spencere4d87aa2006-12-23 06:05:41 +000039 int64_t Val = INT64_MAX; // All ones
40 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
41 return ConstantInt::get(Ty, Val);
42 }
43 return ConstantInt::getAllOnesValue(Ty);
Reid Spencerc6bf4bf2006-12-06 20:45:15 +000044 }
Reid Spencere4d87aa2006-12-23 06:05:41 +000045 return 0;
Reid Spencerc6bf4bf2006-12-06 20:45:15 +000046}
47
48// Static constructor to create the minimum constant for an integral type...
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000049static ConstantInt *getMinValue(const Type *Ty, bool isSigned = false) {
Chris Lattner42a75512007-01-15 02:27:26 +000050 if (Ty->isInteger()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +000051 if (isSigned) {
52 // Calculate 1111111111000000000000
Reid Spencere7ca0422007-01-08 01:26:33 +000053 unsigned TypeBits = Ty->getPrimitiveSizeInBits();
Reid Spencere4d87aa2006-12-23 06:05:41 +000054 int64_t Val = -1; // All ones
55 Val <<= TypeBits-1; // Shift over to the right spot
56 return ConstantInt::get(Ty, Val);
57 }
58 return ConstantInt::get(Ty, 0);
Reid Spencerc6bf4bf2006-12-06 20:45:15 +000059 }
Reid Spencere4d87aa2006-12-23 06:05:41 +000060 return 0;
Reid Spencerc6bf4bf2006-12-06 20:45:15 +000061}
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000062static ConstantInt *Next(ConstantInt *CI) {
Chris Lattnerfc33d302004-03-30 00:20:08 +000063 Constant *Result = ConstantExpr::getAdd(CI,
64 ConstantInt::get(CI->getType(), 1));
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000065 return cast<ConstantInt>(Result);
Chris Lattnerfc33d302004-03-30 00:20:08 +000066}
67
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000068static bool LT(ConstantInt *A, ConstantInt *B, bool isSigned) {
Reid Spencere4d87aa2006-12-23 06:05:41 +000069 Constant *C = ConstantExpr::getICmp(
70 (isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT), A, B);
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000071 assert(isa<ConstantInt>(C) && "Constant folding of integrals not impl??");
Reid Spencer579dca12007-01-12 04:24:46 +000072 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattner67bb7602004-01-12 20:13:04 +000073}
74
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000075static bool LTE(ConstantInt *A, ConstantInt *B, bool isSigned) {
Reid Spencere4d87aa2006-12-23 06:05:41 +000076 Constant *C = ConstantExpr::getICmp(
77 (isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE), A, B);
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000078 assert(isa<ConstantInt>(C) && "Constant folding of integrals not impl??");
Reid Spencer579dca12007-01-12 04:24:46 +000079 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattner67bb7602004-01-12 20:13:04 +000080}
81
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000082static bool GT(ConstantInt *A, ConstantInt *B, bool isSigned) {
Reid Spencere4d87aa2006-12-23 06:05:41 +000083 return LT(B, A, isSigned); }
Chris Lattnerfc33d302004-03-30 00:20:08 +000084
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000085static ConstantInt *Min(ConstantInt *A, ConstantInt *B,
Reid Spencere4d87aa2006-12-23 06:05:41 +000086 bool isSigned) {
87 return LT(A, B, isSigned) ? A : B;
Chris Lattner67bb7602004-01-12 20:13:04 +000088}
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000089static ConstantInt *Max(ConstantInt *A, ConstantInt *B,
Reid Spencere4d87aa2006-12-23 06:05:41 +000090 bool isSigned) {
91 return GT(A, B, isSigned) ? A : B;
Chris Lattner67bb7602004-01-12 20:13:04 +000092}
93
Chris Lattner645e00d2002-09-01 23:53:36 +000094/// Initialize a full (the default) or empty set for the specified type.
95///
96ConstantRange::ConstantRange(const Type *Ty, bool Full) {
Chris Lattner42a75512007-01-15 02:27:26 +000097 assert(Ty->isInteger() &&
Chris Lattner645e00d2002-09-01 23:53:36 +000098 "Cannot make constant range of non-integral type!");
99 if (Full)
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000100 Lower = Upper = getMaxValue(Ty);
Chris Lattner645e00d2002-09-01 23:53:36 +0000101 else
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000102 Lower = Upper = getMinValue(Ty);
Chris Lattner645e00d2002-09-01 23:53:36 +0000103}
104
Chris Lattnerfc33d302004-03-30 00:20:08 +0000105/// Initialize a range to hold the single specified value.
106///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000107ConstantRange::ConstantRange(Constant *V)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000108 : Lower(cast<ConstantInt>(V)), Upper(Next(cast<ConstantInt>(V))) { }
Chris Lattnerfc33d302004-03-30 00:20:08 +0000109
Chris Lattner645e00d2002-09-01 23:53:36 +0000110/// Initialize a range of values explicitly... this will assert out if
111/// Lower==Upper and Lower != Min or Max for its type (or if the two constants
112/// have different types)
113///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000114ConstantRange::ConstantRange(Constant *L, Constant *U)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000115 : Lower(cast<ConstantInt>(L)), Upper(cast<ConstantInt>(U)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000116 assert(Lower->getType() == Upper->getType() &&
117 "Incompatible types for ConstantRange!");
Misha Brukmanfd939082005-04-21 23:48:37 +0000118
Chris Lattner645e00d2002-09-01 23:53:36 +0000119 // Make sure that if L & U are equal that they are either Min or Max...
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000120 assert((L != U || (L == getMaxValue(L->getType()) ||
Reid Spencere4d87aa2006-12-23 06:05:41 +0000121 L == getMinValue(L->getType())))
122 && "Lower == Upper, but they aren't min or max for type!");
Chris Lattner645e00d2002-09-01 23:53:36 +0000123}
124
Chris Lattner645e00d2002-09-01 23:53:36 +0000125/// Initialize a set of values that all satisfy the condition with C.
126///
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000127ConstantRange::ConstantRange(unsigned short ICmpOpcode, ConstantInt *C) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000128 switch (ICmpOpcode) {
129 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
130 case ICmpInst::ICMP_EQ: Lower = C; Upper = Next(C); return;
131 case ICmpInst::ICMP_NE: Upper = C; Lower = Next(C); return;
132 case ICmpInst::ICMP_ULT:
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000133 Lower = getMinValue(C->getType());
Chris Lattner645e00d2002-09-01 23:53:36 +0000134 Upper = C;
135 return;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000136 case ICmpInst::ICMP_SLT:
137 Lower = getMinValue(C->getType(), true);
138 Upper = C;
Chris Lattner645e00d2002-09-01 23:53:36 +0000139 return;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000140 case ICmpInst::ICMP_UGT:
141 Lower = Next(C);
142 Upper = getMinValue(C->getType()); // Min = Next(Max)
143 return;
144 case ICmpInst::ICMP_SGT:
145 Lower = Next(C);
146 Upper = getMinValue(C->getType(), true); // Min = Next(Max)
147 return;
148 case ICmpInst::ICMP_ULE:
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000149 Lower = getMinValue(C->getType());
Chris Lattner645e00d2002-09-01 23:53:36 +0000150 Upper = Next(C);
151 return;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000152 case ICmpInst::ICMP_SLE:
153 Lower = getMinValue(C->getType(), true);
154 Upper = Next(C);
155 return;
156 case ICmpInst::ICMP_UGE:
Chris Lattner645e00d2002-09-01 23:53:36 +0000157 Lower = C;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000158 Upper = getMinValue(C->getType()); // Min = Next(Max)
159 return;
160 case ICmpInst::ICMP_SGE:
161 Lower = C;
162 Upper = getMinValue(C->getType(), true); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +0000163 return;
164 }
165}
166
167/// getType - Return the LLVM data type of this range.
168///
169const Type *ConstantRange::getType() const { return Lower->getType(); }
170
171/// isFullSet - Return true if this set contains all of the elements possible
172/// for this data-type
173bool ConstantRange::isFullSet() const {
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000174 return Lower == Upper && Lower == getMaxValue(getType());
Chris Lattner645e00d2002-09-01 23:53:36 +0000175}
Misha Brukmanfd939082005-04-21 23:48:37 +0000176
Chris Lattner645e00d2002-09-01 23:53:36 +0000177/// isEmptySet - Return true if this set contains no members.
178///
179bool ConstantRange::isEmptySet() const {
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000180 return Lower == Upper && Lower == getMinValue(getType());
Chris Lattner645e00d2002-09-01 23:53:36 +0000181}
182
183/// isWrappedSet - Return true if this set wraps around the top of the range,
184/// for example: [100, 8)
185///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000186bool ConstantRange::isWrappedSet(bool isSigned) const {
187 return GT(Lower, Upper, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000188}
189
Chris Lattner645e00d2002-09-01 23:53:36 +0000190/// getSingleElement - If this set contains a single element, return it,
191/// otherwise return null.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000192ConstantInt *ConstantRange::getSingleElement() const {
Chris Lattner645e00d2002-09-01 23:53:36 +0000193 if (Upper == Next(Lower)) // Is it a single element range?
194 return Lower;
195 return 0;
196}
197
198/// getSetSize - Return the number of elements in this set.
199///
200uint64_t ConstantRange::getSetSize() const {
201 if (isEmptySet()) return 0;
Reid Spencer4fe16d62007-01-11 18:21:29 +0000202 if (getType() == Type::Int1Ty) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000203 if (Lower != Upper) // One of T or F in the set...
204 return 1;
205 return 2; // Must be full set...
206 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000207
Chris Lattner645e00d2002-09-01 23:53:36 +0000208 // Simply subtract the bounds...
Chris Lattnerfc33d302004-03-30 00:20:08 +0000209 Constant *Result = ConstantExpr::getSub(Upper, Lower);
Reid Spencerb83eb642006-10-20 07:07:24 +0000210 return cast<ConstantInt>(Result)->getZExtValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000211}
212
Chris Lattnerfc33d302004-03-30 00:20:08 +0000213/// contains - Return true if the specified value is in the set.
214///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000215bool ConstantRange::contains(ConstantInt *Val, bool isSigned) const {
Chris Lattnerfc33d302004-03-30 00:20:08 +0000216 if (Lower == Upper) {
217 if (isFullSet()) return true;
218 return false;
219 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000220
Reid Spencere4d87aa2006-12-23 06:05:41 +0000221 if (!isWrappedSet(isSigned))
222 return LTE(Lower, Val, isSigned) && LT(Val, Upper, isSigned);
223 return LTE(Lower, Val, isSigned) || LT(Val, Upper, isSigned);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000224}
225
Chris Lattnerfc33d302004-03-30 00:20:08 +0000226/// subtract - Subtract the specified constant from the endpoints of this
227/// constant range.
228ConstantRange ConstantRange::subtract(ConstantInt *CI) const {
Chris Lattner42a75512007-01-15 02:27:26 +0000229 assert(CI->getType() == getType() && getType()->isInteger() &&
Chris Lattnerfc33d302004-03-30 00:20:08 +0000230 "Cannot subtract from different type range or non-integer!");
231 // If the set is empty or full, don't modify the endpoints.
232 if (Lower == Upper) return *this;
233 return ConstantRange(ConstantExpr::getSub(Lower, CI),
234 ConstantExpr::getSub(Upper, CI));
235}
Chris Lattner645e00d2002-09-01 23:53:36 +0000236
237
238// intersect1Wrapped - This helper function is used to intersect two ranges when
239// it is known that LHS is wrapped and RHS isn't.
240//
241static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000242 const ConstantRange &RHS,
243 bool isSigned) {
244 assert(LHS.isWrappedSet(isSigned) && !RHS.isWrappedSet(isSigned));
Chris Lattner645e00d2002-09-01 23:53:36 +0000245
Chris Lattner645e00d2002-09-01 23:53:36 +0000246 // Check to see if we overlap on the Left side of RHS...
247 //
Reid Spencere4d87aa2006-12-23 06:05:41 +0000248 if (LT(RHS.getLower(), LHS.getUpper(), isSigned)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000249 // We do overlap on the left side of RHS, see if we overlap on the right of
250 // RHS...
Reid Spencere4d87aa2006-12-23 06:05:41 +0000251 if (GT(RHS.getUpper(), LHS.getLower(), isSigned)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000252 // Ok, the result overlaps on both the left and right sides. See if the
253 // resultant interval will be smaller if we wrap or not...
254 //
255 if (LHS.getSetSize() < RHS.getSetSize())
256 return LHS;
257 else
258 return RHS;
259
260 } else {
261 // No overlap on the right, just on the left.
262 return ConstantRange(RHS.getLower(), LHS.getUpper());
263 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000264 } else {
265 // We don't overlap on the left side of RHS, see if we overlap on the right
266 // of RHS...
Reid Spencere4d87aa2006-12-23 06:05:41 +0000267 if (GT(RHS.getUpper(), LHS.getLower(), isSigned)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000268 // Simple overlap...
269 return ConstantRange(LHS.getLower(), RHS.getUpper());
270 } else {
271 // No overlap...
272 return ConstantRange(LHS.getType(), false);
273 }
274 }
275}
276
Nick Lewycky3e051642007-02-11 00:58:49 +0000277/// intersectWith - Return the range that results from the intersection of this
Chris Lattner645e00d2002-09-01 23:53:36 +0000278/// range with another range.
279///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000280ConstantRange ConstantRange::intersectWith(const ConstantRange &CR,
281 bool isSigned) const {
Chris Lattner645e00d2002-09-01 23:53:36 +0000282 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000283 // Handle common special cases
284 if (isEmptySet() || CR.isFullSet()) return *this;
285 if (isFullSet() || CR.isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000286
Reid Spencere4d87aa2006-12-23 06:05:41 +0000287 if (!isWrappedSet(isSigned)) {
288 if (!CR.isWrappedSet(isSigned)) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000289 ConstantInt *L = Max(Lower, CR.Lower, isSigned);
290 ConstantInt *U = Min(Upper, CR.Upper, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000291
Reid Spencere4d87aa2006-12-23 06:05:41 +0000292 if (LT(L, U, isSigned)) // If range isn't empty...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000293 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000294 else
295 return ConstantRange(getType(), false); // Otherwise, return empty set
296 } else
Reid Spencere4d87aa2006-12-23 06:05:41 +0000297 return intersect1Wrapped(CR, *this, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000298 } else { // We know "this" is wrapped...
Reid Spencere4d87aa2006-12-23 06:05:41 +0000299 if (!CR.isWrappedSet(isSigned))
300 return intersect1Wrapped(*this, CR, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000301 else {
302 // Both ranges are wrapped...
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000303 ConstantInt *L = Max(Lower, CR.Lower, isSigned);
304 ConstantInt *U = Min(Upper, CR.Upper, isSigned);
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000305 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000306 }
307 }
308 return *this;
309}
310
Nick Lewycky3e051642007-02-11 00:58:49 +0000311/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000312/// another range. The resultant range is guaranteed to include the elements of
313/// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
314/// 15), which includes 9, 10, and 11, which were not included in either set
315/// before.
316///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000317ConstantRange ConstantRange::unionWith(const ConstantRange &CR,
318 bool isSigned) const {
Chris Lattner645e00d2002-09-01 23:53:36 +0000319 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
320
321 assert(0 && "Range union not implemented yet!");
322
323 return *this;
324}
Chris Lattner96f9d722002-09-02 00:18:22 +0000325
Chris Lattnerfc33d302004-03-30 00:20:08 +0000326/// zeroExtend - Return a new range in the specified integer type, which must
327/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000328/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000329/// zero extended.
330ConstantRange ConstantRange::zeroExtend(const Type *Ty) const {
Reid Spencere7ca0422007-01-08 01:26:33 +0000331 unsigned SrcTySize = getLower()->getType()->getPrimitiveSizeInBits();
332 assert(SrcTySize < Ty->getPrimitiveSizeInBits() && "Not a value extension");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000333 if (isFullSet()) {
334 // Change a source full set into [0, 1 << 8*numbytes)
Chris Lattnerfc33d302004-03-30 00:20:08 +0000335 return ConstantRange(Constant::getNullValue(Ty),
Reid Spencere7ca0422007-01-08 01:26:33 +0000336 ConstantInt::get(Ty, 1ULL << SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000337 }
338
339 Constant *Lower = getLower();
340 Constant *Upper = getUpper();
Chris Lattnerfc33d302004-03-30 00:20:08 +0000341
Reid Spencerd977d862006-12-12 23:36:14 +0000342 return ConstantRange(ConstantExpr::getZExt(Lower, Ty),
343 ConstantExpr::getZExt(Upper, Ty));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000344}
345
346/// truncate - Return a new range in the specified integer type, which must be
347/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000348/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000349/// truncated to the specified type.
350ConstantRange ConstantRange::truncate(const Type *Ty) const {
Reid Spencere7ca0422007-01-08 01:26:33 +0000351 unsigned SrcTySize = getLower()->getType()->getPrimitiveSizeInBits();
Reid Spencerca7ad892007-01-08 05:34:39 +0000352 assert(SrcTySize > Ty->getPrimitiveSizeInBits() && "Not a value truncation");
Reid Spencere7ca0422007-01-08 01:26:33 +0000353 uint64_t Size = 1ULL << Ty->getPrimitiveSizeInBits();
Chris Lattnerfc33d302004-03-30 00:20:08 +0000354 if (isFullSet() || getSetSize() >= Size)
355 return ConstantRange(getType());
356
Reid Spencer575d95c2006-12-04 02:46:44 +0000357 return ConstantRange(
Reid Spencerd977d862006-12-12 23:36:14 +0000358 ConstantExpr::getTrunc(getLower(), Ty),
359 ConstantExpr::getTrunc(getUpper(), Ty));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000360}
361
Chris Lattner96f9d722002-09-02 00:18:22 +0000362/// print - Print out the bounds to a stream...
363///
364void ConstantRange::print(std::ostream &OS) const {
Chris Lattnerce36d552004-07-15 01:29:12 +0000365 OS << "[" << *Lower << "," << *Upper << " )";
Chris Lattner96f9d722002-09-02 00:18:22 +0000366}
367
368/// dump - Allow printing from a debugger easily...
369///
370void ConstantRange::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000371 print(cerr);
Chris Lattner96f9d722002-09-02 00:18:22 +0000372}