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" |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 27 | #include "llvm/Instructions.h" |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 28 | #include "llvm/Type.h" |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Streams.h" |
| 30 | #include <ostream> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 33 | static ConstantInt *getMaxValue(const Type *Ty, bool isSigned = false) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 34 | if (Ty == Type::BoolTy) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 35 | return ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 36 | if (Ty->isInteger()) { |
| 37 | if (isSigned) { |
| 38 | // Calculate 011111111111111... |
Reid Spencer | e7ca042 | 2007-01-08 01:26:33 +0000 | [diff] [blame] | 39 | unsigned TypeBits = Ty->getPrimitiveSizeInBits(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 40 | 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 Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 45 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 46 | return 0; |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // Static constructor to create the minimum constant for an integral type... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 50 | static ConstantInt *getMinValue(const Type *Ty, bool isSigned = false) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 51 | if (Ty == Type::BoolTy) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 52 | return ConstantInt::getFalse(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 53 | if (Ty->isInteger()) { |
| 54 | if (isSigned) { |
| 55 | // Calculate 1111111111000000000000 |
Reid Spencer | e7ca042 | 2007-01-08 01:26:33 +0000 | [diff] [blame] | 56 | unsigned TypeBits = Ty->getPrimitiveSizeInBits(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 57 | 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 Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 62 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 63 | return 0; |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 64 | } |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 65 | static ConstantInt *Next(ConstantInt *CI) { |
| 66 | if (CI->getType() == Type::BoolTy) |
| 67 | return ConstantInt::get(!CI->getBoolValue()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 68 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 69 | Constant *Result = ConstantExpr::getAdd(CI, |
| 70 | ConstantInt::get(CI->getType(), 1)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 71 | return cast<ConstantInt>(Result); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 74 | static bool LT(ConstantInt *A, ConstantInt *B, bool isSigned) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 75 | Constant *C = ConstantExpr::getICmp( |
| 76 | (isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT), A, B); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 77 | assert(isa<ConstantInt>(C) && "Constant folding of integrals not impl??"); |
| 78 | return cast<ConstantInt>(C)->getBoolValue(); |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 81 | static bool LTE(ConstantInt *A, ConstantInt *B, bool isSigned) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 82 | Constant *C = ConstantExpr::getICmp( |
| 83 | (isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE), A, B); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 84 | assert(isa<ConstantInt>(C) && "Constant folding of integrals not impl??"); |
| 85 | return cast<ConstantInt>(C)->getBoolValue(); |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 88 | static bool GT(ConstantInt *A, ConstantInt *B, bool isSigned) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 89 | return LT(B, A, isSigned); } |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 90 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 91 | static ConstantInt *Min(ConstantInt *A, ConstantInt *B, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 92 | bool isSigned) { |
| 93 | return LT(A, B, isSigned) ? A : B; |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 94 | } |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 95 | static ConstantInt *Max(ConstantInt *A, ConstantInt *B, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 96 | bool isSigned) { |
| 97 | return GT(A, B, isSigned) ? A : B; |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 100 | /// Initialize a full (the default) or empty set for the specified type. |
| 101 | /// |
| 102 | ConstantRange::ConstantRange(const Type *Ty, bool Full) { |
| 103 | assert(Ty->isIntegral() && |
| 104 | "Cannot make constant range of non-integral type!"); |
| 105 | if (Full) |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 106 | Lower = Upper = getMaxValue(Ty); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 107 | else |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 108 | Lower = Upper = getMinValue(Ty); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 111 | /// Initialize a range to hold the single specified value. |
| 112 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 113 | ConstantRange::ConstantRange(Constant *V) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 114 | : Lower(cast<ConstantInt>(V)), Upper(Next(cast<ConstantInt>(V))) { } |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 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) |
| 119 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 120 | ConstantRange::ConstantRange(Constant *L, Constant *U) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 121 | : Lower(cast<ConstantInt>(L)), Upper(cast<ConstantInt>(U)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 122 | assert(Lower->getType() == Upper->getType() && |
| 123 | "Incompatible types for ConstantRange!"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 125 | // 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] | 126 | assert((L != U || (L == getMaxValue(L->getType()) || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 127 | L == getMinValue(L->getType()))) |
| 128 | && "Lower == Upper, but they aren't min or max for type!"); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 131 | /// Initialize a set of values that all satisfy the condition with C. |
| 132 | /// |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 133 | ConstantRange::ConstantRange(unsigned short ICmpOpcode, ConstantInt *C) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 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 Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 139 | Lower = getMinValue(C->getType()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 140 | Upper = C; |
| 141 | return; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 142 | case ICmpInst::ICMP_SLT: |
| 143 | Lower = getMinValue(C->getType(), true); |
| 144 | Upper = C; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 145 | return; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 146 | 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 Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 155 | Lower = getMinValue(C->getType()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 156 | Upper = Next(C); |
| 157 | return; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 158 | case ICmpInst::ICMP_SLE: |
| 159 | Lower = getMinValue(C->getType(), true); |
| 160 | Upper = Next(C); |
| 161 | return; |
| 162 | case ICmpInst::ICMP_UGE: |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 163 | Lower = C; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 164 | 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 Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /// getType - Return the LLVM data type of this range. |
| 174 | /// |
| 175 | const 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 |
| 179 | bool ConstantRange::isFullSet() const { |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 180 | return Lower == Upper && Lower == getMaxValue(getType()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 181 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 183 | /// isEmptySet - Return true if this set contains no members. |
| 184 | /// |
| 185 | bool ConstantRange::isEmptySet() const { |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 186 | return Lower == Upper && Lower == getMinValue(getType()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /// isWrappedSet - Return true if this set wraps around the top of the range, |
| 190 | /// for example: [100, 8) |
| 191 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 192 | bool ConstantRange::isWrappedSet(bool isSigned) const { |
| 193 | return GT(Lower, Upper, isSigned); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 196 | /// getSingleElement - If this set contains a single element, return it, |
| 197 | /// otherwise return null. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 198 | ConstantInt *ConstantRange::getSingleElement() const { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 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 | /// |
| 206 | uint64_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 Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 214 | // Simply subtract the bounds... |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 215 | Constant *Result = ConstantExpr::getSub(Upper, Lower); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 216 | return cast<ConstantInt>(Result)->getZExtValue(); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 219 | /// contains - Return true if the specified value is in the set. |
| 220 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 221 | bool ConstantRange::contains(ConstantInt *Val, bool isSigned) const { |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 222 | if (Lower == Upper) { |
| 223 | if (isFullSet()) return true; |
| 224 | return false; |
| 225 | } |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 226 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 227 | if (!isWrappedSet(isSigned)) |
| 228 | return LTE(Lower, Val, isSigned) && LT(Val, Upper, isSigned); |
| 229 | return LTE(Lower, Val, isSigned) || LT(Val, Upper, isSigned); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 232 | /// subtract - Subtract the specified constant from the endpoints of this |
| 233 | /// constant range. |
| 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)); |
| 241 | } |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 242 | |
| 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 | // |
| 247 | static ConstantRange intersect1Wrapped(const ConstantRange &LHS, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 248 | const ConstantRange &RHS, |
| 249 | bool isSigned) { |
| 250 | assert(LHS.isWrappedSet(isSigned) && !RHS.isWrappedSet(isSigned)); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 251 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 252 | // Check to see if we overlap on the Left side of RHS... |
| 253 | // |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 254 | if (LT(RHS.getLower(), LHS.getUpper(), isSigned)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 255 | // We do overlap on the left side of RHS, see if we overlap on the right of |
| 256 | // RHS... |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 257 | if (GT(RHS.getUpper(), LHS.getLower(), isSigned)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 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... |
| 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 Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 270 | } else { |
| 271 | // We don't overlap on the left side of RHS, see if we overlap on the right |
| 272 | // of RHS... |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 273 | if (GT(RHS.getUpper(), LHS.getLower(), isSigned)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 274 | // 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 Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 283 | /// intersect - Return the range that results from the intersection of this |
| 284 | /// range with another range. |
| 285 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 286 | ConstantRange ConstantRange::intersectWith(const ConstantRange &CR, |
| 287 | bool isSigned) const { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 288 | assert(getType() == CR.getType() && "ConstantRange types don't agree!"); |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 289 | // Handle common special cases |
| 290 | if (isEmptySet() || CR.isFullSet()) return *this; |
| 291 | if (isFullSet() || CR.isEmptySet()) return CR; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 292 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 293 | if (!isWrappedSet(isSigned)) { |
| 294 | if (!CR.isWrappedSet(isSigned)) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 295 | ConstantInt *L = Max(Lower, CR.Lower, isSigned); |
| 296 | ConstantInt *U = Min(Upper, CR.Upper, isSigned); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 297 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 298 | if (LT(L, U, isSigned)) // If range isn't empty... |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 299 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 300 | else |
| 301 | return ConstantRange(getType(), false); // Otherwise, return empty set |
| 302 | } else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 303 | return intersect1Wrapped(CR, *this, isSigned); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 304 | } else { // We know "this" is wrapped... |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 305 | if (!CR.isWrappedSet(isSigned)) |
| 306 | return intersect1Wrapped(*this, CR, isSigned); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 307 | else { |
| 308 | // Both ranges are wrapped... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 309 | ConstantInt *L = Max(Lower, CR.Lower, isSigned); |
| 310 | ConstantInt *U = Min(Upper, CR.Upper, isSigned); |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 311 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 312 | } |
| 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 Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 323 | ConstantRange ConstantRange::unionWith(const ConstantRange &CR, |
| 324 | bool isSigned) const { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 325 | assert(getType() == CR.getType() && "ConstantRange types don't agree!"); |
| 326 | |
| 327 | assert(0 && "Range union not implemented yet!"); |
| 328 | |
| 329 | return *this; |
| 330 | } |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 331 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 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 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 334 | /// correspond to the possible range of values as if the source range had been |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 335 | /// zero extended. |
| 336 | ConstantRange ConstantRange::zeroExtend(const Type *Ty) const { |
Reid Spencer | e7ca042 | 2007-01-08 01:26:33 +0000 | [diff] [blame] | 337 | unsigned SrcTySize = getLower()->getType()->getPrimitiveSizeInBits(); |
| 338 | assert(SrcTySize < Ty->getPrimitiveSizeInBits() && "Not a value extension"); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 339 | if (isFullSet()) { |
| 340 | // Change a source full set into [0, 1 << 8*numbytes) |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 341 | return ConstantRange(Constant::getNullValue(Ty), |
Reid Spencer | e7ca042 | 2007-01-08 01:26:33 +0000 | [diff] [blame] | 342 | ConstantInt::get(Ty, 1ULL << SrcTySize)); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | Constant *Lower = getLower(); |
| 346 | Constant *Upper = getUpper(); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 347 | |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 348 | return ConstantRange(ConstantExpr::getZExt(Lower, Ty), |
| 349 | ConstantExpr::getZExt(Upper, Ty)); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 350 | } |
| 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 Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 354 | /// correspond to the possible range of values as if the source range had been |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 355 | /// truncated to the specified type. |
| 356 | ConstantRange ConstantRange::truncate(const Type *Ty) const { |
Reid Spencer | e7ca042 | 2007-01-08 01:26:33 +0000 | [diff] [blame] | 357 | unsigned SrcTySize = getLower()->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | ca7ad89 | 2007-01-08 05:34:39 +0000 | [diff] [blame] | 358 | assert(SrcTySize > Ty->getPrimitiveSizeInBits() && "Not a value truncation"); |
Reid Spencer | e7ca042 | 2007-01-08 01:26:33 +0000 | [diff] [blame] | 359 | uint64_t Size = 1ULL << Ty->getPrimitiveSizeInBits(); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 360 | if (isFullSet() || getSetSize() >= Size) |
| 361 | return ConstantRange(getType()); |
| 362 | |
Reid Spencer | 575d95c | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 363 | return ConstantRange( |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 364 | ConstantExpr::getTrunc(getLower(), Ty), |
| 365 | ConstantExpr::getTrunc(getUpper(), Ty)); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 368 | /// print - Print out the bounds to a stream... |
| 369 | /// |
| 370 | void ConstantRange::print(std::ostream &OS) const { |
Chris Lattner | ce36d55 | 2004-07-15 01:29:12 +0000 | [diff] [blame] | 371 | OS << "[" << *Lower << "," << *Upper << " )"; |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /// dump - Allow printing from a debugger easily... |
| 375 | /// |
| 376 | void ConstantRange::dump() const { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 377 | print(cerr); |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 378 | } |