Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 1 | //===-- ConstantRange.cpp - ConstantRange implementation ------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // 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 Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 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 (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 Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 25 | #include "llvm/Constants.h" |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 26 | #include "llvm/Instruction.h" |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 27 | #include "llvm/Type.h" |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Streams.h" |
| 29 | #include <ostream> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 32 | static ConstantIntegral *getMaxValue(const Type *Ty) { |
| 33 | switch (Ty->getTypeID()) { |
| 34 | case Type::BoolTyID: return ConstantBool::getTrue(); |
| 35 | case Type::SByteTyID: |
| 36 | case Type::ShortTyID: |
| 37 | case Type::IntTyID: |
| 38 | case Type::LongTyID: { |
| 39 | // Calculate 011111111111111... |
| 40 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 41 | int64_t Val = INT64_MAX; // All ones |
| 42 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 43 | return ConstantInt::get(Ty, Val); |
| 44 | } |
| 45 | |
| 46 | case Type::UByteTyID: |
| 47 | case Type::UShortTyID: |
| 48 | case Type::UIntTyID: |
| 49 | case Type::ULongTyID: return ConstantInt::getAllOnesValue(Ty); |
| 50 | |
| 51 | default: return 0; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Static constructor to create the minimum constant for an integral type... |
| 56 | static ConstantIntegral *getMinValue(const Type *Ty) { |
| 57 | switch (Ty->getTypeID()) { |
| 58 | case Type::BoolTyID: return ConstantBool::getFalse(); |
| 59 | case Type::SByteTyID: |
| 60 | case Type::ShortTyID: |
| 61 | case Type::IntTyID: |
| 62 | case Type::LongTyID: { |
| 63 | // Calculate 1111111111000000000000 |
| 64 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 65 | int64_t Val = -1; // All ones |
| 66 | Val <<= TypeBits-1; // Shift over to the right spot |
| 67 | return ConstantInt::get(Ty, Val); |
| 68 | } |
| 69 | |
| 70 | case Type::UByteTyID: |
| 71 | case Type::UShortTyID: |
| 72 | case Type::UIntTyID: |
| 73 | case Type::ULongTyID: return ConstantInt::get(Ty, 0); |
| 74 | |
| 75 | default: return 0; |
| 76 | } |
| 77 | } |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 78 | static ConstantIntegral *Next(ConstantIntegral *CI) { |
Chris Lattner | 193c2d8 | 2006-09-28 23:14:29 +0000 | [diff] [blame] | 79 | if (ConstantBool *CB = dyn_cast<ConstantBool>(CI)) |
| 80 | return ConstantBool::get(!CB->getValue()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 81 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 82 | Constant *Result = ConstantExpr::getAdd(CI, |
| 83 | ConstantInt::get(CI->getType(), 1)); |
| 84 | return cast<ConstantIntegral>(Result); |
| 85 | } |
| 86 | |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 87 | static bool LT(ConstantIntegral *A, ConstantIntegral *B) { |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 88 | Constant *C = ConstantExpr::getSetLT(A, B); |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 89 | assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??"); |
| 90 | return cast<ConstantBool>(C)->getValue(); |
| 91 | } |
| 92 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 93 | static bool LTE(ConstantIntegral *A, ConstantIntegral *B) { |
| 94 | Constant *C = ConstantExpr::getSetLE(A, B); |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 95 | assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??"); |
| 96 | return cast<ConstantBool>(C)->getValue(); |
| 97 | } |
| 98 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 99 | static bool GT(ConstantIntegral *A, ConstantIntegral *B) { return LT(B, A); } |
| 100 | |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 101 | static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) { |
| 102 | return LT(A, B) ? A : B; |
| 103 | } |
| 104 | static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) { |
| 105 | return GT(A, B) ? A : B; |
| 106 | } |
| 107 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 108 | /// Initialize a full (the default) or empty set for the specified type. |
| 109 | /// |
| 110 | ConstantRange::ConstantRange(const Type *Ty, bool Full) { |
| 111 | assert(Ty->isIntegral() && |
| 112 | "Cannot make constant range of non-integral type!"); |
| 113 | if (Full) |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 114 | Lower = Upper = getMaxValue(Ty); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 115 | else |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 116 | Lower = Upper = getMinValue(Ty); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 119 | /// Initialize a range to hold the single specified value. |
| 120 | /// |
| 121 | ConstantRange::ConstantRange(Constant *V) |
| 122 | : Lower(cast<ConstantIntegral>(V)), Upper(Next(cast<ConstantIntegral>(V))) { |
| 123 | } |
| 124 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 125 | /// Initialize a range of values explicitly... this will assert out if |
| 126 | /// Lower==Upper and Lower != Min or Max for its type (or if the two constants |
| 127 | /// have different types) |
| 128 | /// |
Chris Lattner | db81395 | 2004-03-29 20:42:49 +0000 | [diff] [blame] | 129 | ConstantRange::ConstantRange(Constant *L, Constant *U) |
| 130 | : Lower(cast<ConstantIntegral>(L)), Upper(cast<ConstantIntegral>(U)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 131 | assert(Lower->getType() == Upper->getType() && |
| 132 | "Incompatible types for ConstantRange!"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 134 | // Make sure that if L & U are equal that they are either Min or Max... |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 135 | assert((L != U || (L == getMaxValue(L->getType()) || |
| 136 | L == getMinValue(L->getType()))) && |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 137 | "Lower == Upper, but they aren't min or max for type!"); |
| 138 | } |
| 139 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 140 | /// Initialize a set of values that all satisfy the condition with C. |
| 141 | /// |
| 142 | ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) { |
| 143 | switch (SetCCOpcode) { |
| 144 | default: assert(0 && "Invalid SetCC opcode to ConstantRange ctor!"); |
| 145 | case Instruction::SetEQ: Lower = C; Upper = Next(C); return; |
| 146 | case Instruction::SetNE: Upper = C; Lower = Next(C); return; |
| 147 | case Instruction::SetLT: |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 148 | Lower = getMinValue(C->getType()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 149 | Upper = C; |
| 150 | return; |
| 151 | case Instruction::SetGT: |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 152 | Lower = Next(C); |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 153 | Upper = getMinValue(C->getType()); // Min = Next(Max) |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 154 | return; |
| 155 | case Instruction::SetLE: |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 156 | Lower = getMinValue(C->getType()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 157 | Upper = Next(C); |
| 158 | return; |
| 159 | case Instruction::SetGE: |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 160 | Lower = C; |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 161 | Upper = getMinValue(C->getType()); // Min = Next(Max) |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /// getType - Return the LLVM data type of this range. |
| 167 | /// |
| 168 | const Type *ConstantRange::getType() const { return Lower->getType(); } |
| 169 | |
| 170 | /// isFullSet - Return true if this set contains all of the elements possible |
| 171 | /// for this data-type |
| 172 | bool ConstantRange::isFullSet() const { |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 173 | return Lower == Upper && Lower == getMaxValue(getType()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 174 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 176 | /// isEmptySet - Return true if this set contains no members. |
| 177 | /// |
| 178 | bool ConstantRange::isEmptySet() const { |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 179 | return Lower == Upper && Lower == getMinValue(getType()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /// isWrappedSet - Return true if this set wraps around the top of the range, |
| 183 | /// for example: [100, 8) |
| 184 | /// |
| 185 | bool ConstantRange::isWrappedSet() const { |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 186 | return GT(Lower, Upper); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 190 | /// getSingleElement - If this set contains a single element, return it, |
| 191 | /// otherwise return null. |
| 192 | ConstantIntegral *ConstantRange::getSingleElement() const { |
| 193 | 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 | /// |
| 200 | uint64_t ConstantRange::getSetSize() const { |
| 201 | if (isEmptySet()) return 0; |
| 202 | if (getType() == Type::BoolTy) { |
| 203 | if (Lower != Upper) // One of T or F in the set... |
| 204 | return 1; |
| 205 | return 2; // Must be full set... |
| 206 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 208 | // Simply subtract the bounds... |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 209 | Constant *Result = ConstantExpr::getSub(Upper, Lower); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 210 | return cast<ConstantInt>(Result)->getZExtValue(); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 213 | /// contains - Return true if the specified value is in the set. |
| 214 | /// |
| 215 | bool ConstantRange::contains(ConstantInt *Val) const { |
| 216 | if (Lower == Upper) { |
| 217 | if (isFullSet()) return true; |
| 218 | return false; |
| 219 | } |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 220 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 221 | if (!isWrappedSet()) |
| 222 | return LTE(Lower, Val) && LT(Val, Upper); |
| 223 | return LTE(Lower, Val) || LT(Val, Upper); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | |
| 228 | /// subtract - Subtract the specified constant from the endpoints of this |
| 229 | /// constant range. |
| 230 | ConstantRange ConstantRange::subtract(ConstantInt *CI) const { |
| 231 | assert(CI->getType() == getType() && getType()->isInteger() && |
| 232 | "Cannot subtract from different type range or non-integer!"); |
| 233 | // If the set is empty or full, don't modify the endpoints. |
| 234 | if (Lower == Upper) return *this; |
| 235 | return ConstantRange(ConstantExpr::getSub(Lower, CI), |
| 236 | ConstantExpr::getSub(Upper, CI)); |
| 237 | } |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 238 | |
| 239 | |
| 240 | // intersect1Wrapped - This helper function is used to intersect two ranges when |
| 241 | // it is known that LHS is wrapped and RHS isn't. |
| 242 | // |
| 243 | static ConstantRange intersect1Wrapped(const ConstantRange &LHS, |
| 244 | const ConstantRange &RHS) { |
| 245 | assert(LHS.isWrappedSet() && !RHS.isWrappedSet()); |
| 246 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 247 | // Check to see if we overlap on the Left side of RHS... |
| 248 | // |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 249 | if (LT(RHS.getLower(), LHS.getUpper())) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 250 | // We do overlap on the left side of RHS, see if we overlap on the right of |
| 251 | // RHS... |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 252 | if (GT(RHS.getUpper(), LHS.getLower())) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 253 | // Ok, the result overlaps on both the left and right sides. See if the |
| 254 | // resultant interval will be smaller if we wrap or not... |
| 255 | // |
| 256 | if (LHS.getSetSize() < RHS.getSetSize()) |
| 257 | return LHS; |
| 258 | else |
| 259 | return RHS; |
| 260 | |
| 261 | } else { |
| 262 | // No overlap on the right, just on the left. |
| 263 | return ConstantRange(RHS.getLower(), LHS.getUpper()); |
| 264 | } |
| 265 | |
| 266 | } else { |
| 267 | // We don't overlap on the left side of RHS, see if we overlap on the right |
| 268 | // of RHS... |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 269 | if (GT(RHS.getUpper(), LHS.getLower())) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 270 | // Simple overlap... |
| 271 | return ConstantRange(LHS.getLower(), RHS.getUpper()); |
| 272 | } else { |
| 273 | // No overlap... |
| 274 | return ConstantRange(LHS.getType(), false); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 279 | /// intersect - Return the range that results from the intersection of this |
| 280 | /// range with another range. |
| 281 | /// |
| 282 | ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { |
| 283 | assert(getType() == CR.getType() && "ConstantRange types don't agree!"); |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 284 | // Handle common special cases |
| 285 | if (isEmptySet() || CR.isFullSet()) return *this; |
| 286 | if (isFullSet() || CR.isEmptySet()) return CR; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 287 | |
| 288 | if (!isWrappedSet()) { |
| 289 | if (!CR.isWrappedSet()) { |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 290 | ConstantIntegral *L = Max(Lower, CR.Lower); |
| 291 | ConstantIntegral *U = Min(Upper, CR.Upper); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 293 | if (LT(L, U)) // If range isn't empty... |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 294 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 295 | else |
| 296 | return ConstantRange(getType(), false); // Otherwise, return empty set |
| 297 | } else |
| 298 | return intersect1Wrapped(CR, *this); |
| 299 | } else { // We know "this" is wrapped... |
| 300 | if (!CR.isWrappedSet()) |
| 301 | return intersect1Wrapped(*this, CR); |
| 302 | else { |
| 303 | // Both ranges are wrapped... |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 304 | ConstantIntegral *L = Max(Lower, CR.Lower); |
| 305 | ConstantIntegral *U = Min(Upper, CR.Upper); |
| 306 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | return *this; |
| 310 | } |
| 311 | |
| 312 | /// union - Return the range that results from the union of this range with |
| 313 | /// another range. The resultant range is guaranteed to include the elements of |
| 314 | /// both sets, but may contain more. For example, [3, 9) union [12,15) is [3, |
| 315 | /// 15), which includes 9, 10, and 11, which were not included in either set |
| 316 | /// before. |
| 317 | /// |
| 318 | ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { |
| 319 | assert(getType() == CR.getType() && "ConstantRange types don't agree!"); |
| 320 | |
| 321 | assert(0 && "Range union not implemented yet!"); |
| 322 | |
| 323 | return *this; |
| 324 | } |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 325 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 326 | /// zeroExtend - Return a new range in the specified integer type, which must |
| 327 | /// be strictly larger than the current type. The returned range will |
| 328 | /// correspond to the possible range of values if the source range had been |
| 329 | /// zero extended. |
| 330 | ConstantRange ConstantRange::zeroExtend(const Type *Ty) const { |
| 331 | assert(getLower()->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() && |
| 332 | "Not a value extension"); |
| 333 | if (isFullSet()) { |
| 334 | // Change a source full set into [0, 1 << 8*numbytes) |
| 335 | unsigned SrcTySize = getLower()->getType()->getPrimitiveSize(); |
| 336 | return ConstantRange(Constant::getNullValue(Ty), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 337 | ConstantInt::get(Ty, 1ULL << SrcTySize*8)); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | Constant *Lower = getLower(); |
| 341 | Constant *Upper = getUpper(); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 342 | |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame^] | 343 | return ConstantRange(ConstantExpr::getZExt(Lower, Ty), |
| 344 | ConstantExpr::getZExt(Upper, Ty)); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /// truncate - Return a new range in the specified integer type, which must be |
| 348 | /// strictly smaller than the current type. The returned range will |
| 349 | /// correspond to the possible range of values if the source range had been |
| 350 | /// truncated to the specified type. |
| 351 | ConstantRange ConstantRange::truncate(const Type *Ty) const { |
| 352 | assert(getLower()->getType()->getPrimitiveSize() > Ty->getPrimitiveSize() && |
| 353 | "Not a value truncation"); |
| 354 | uint64_t Size = 1ULL << Ty->getPrimitiveSize()*8; |
| 355 | if (isFullSet() || getSetSize() >= Size) |
| 356 | return ConstantRange(getType()); |
| 357 | |
Reid Spencer | 575d95c | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 358 | return ConstantRange( |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame^] | 359 | ConstantExpr::getTrunc(getLower(), Ty), |
| 360 | ConstantExpr::getTrunc(getUpper(), Ty)); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 364 | /// print - Print out the bounds to a stream... |
| 365 | /// |
| 366 | void ConstantRange::print(std::ostream &OS) const { |
Chris Lattner | ce36d55 | 2004-07-15 01:29:12 +0000 | [diff] [blame] | 367 | OS << "[" << *Lower << "," << *Upper << " )"; |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /// dump - Allow printing from a debugger easily... |
| 371 | /// |
| 372 | void ConstantRange::dump() const { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 373 | print(cerr); |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 374 | } |