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" |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 29 | #include "llvm/DerivedTypes.h" |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Streams.h" |
| 31 | #include <ostream> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 33 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 34 | static ConstantInt *getMaxValue(const Type *Ty, bool isSigned = false) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 35 | if (Ty->isInteger()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 36 | if (isSigned) { |
| 37 | // Calculate 011111111111111... |
Reid Spencer | e7ca042 | 2007-01-08 01:26:33 +0000 | [diff] [blame] | 38 | unsigned TypeBits = Ty->getPrimitiveSizeInBits(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 39 | 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 Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 44 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 45 | return 0; |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // Static constructor to create the minimum constant for an integral type... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 49 | static ConstantInt *getMinValue(const Type *Ty, bool isSigned = false) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 50 | if (Ty->isInteger()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 51 | if (isSigned) { |
| 52 | // Calculate 1111111111000000000000 |
Reid Spencer | e7ca042 | 2007-01-08 01:26:33 +0000 | [diff] [blame] | 53 | unsigned TypeBits = Ty->getPrimitiveSizeInBits(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 54 | 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 Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 59 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 60 | return 0; |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 61 | } |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 62 | static ConstantInt *Next(ConstantInt *CI) { |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 63 | Constant *Result = ConstantExpr::getAdd(CI, |
| 64 | ConstantInt::get(CI->getType(), 1)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 65 | return cast<ConstantInt>(Result); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 68 | static bool LT(ConstantInt *A, ConstantInt *B, bool isSigned) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 69 | Constant *C = ConstantExpr::getICmp( |
| 70 | (isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT), A, B); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 71 | assert(isa<ConstantInt>(C) && "Constant folding of integrals not impl??"); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 72 | return cast<ConstantInt>(C)->getZExtValue(); |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 75 | static bool LTE(ConstantInt *A, ConstantInt *B, bool isSigned) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 76 | Constant *C = ConstantExpr::getICmp( |
| 77 | (isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE), A, B); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 78 | assert(isa<ConstantInt>(C) && "Constant folding of integrals not impl??"); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 79 | return cast<ConstantInt>(C)->getZExtValue(); |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 82 | static bool GT(ConstantInt *A, ConstantInt *B, bool isSigned) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 83 | return LT(B, A, isSigned); } |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 84 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 85 | static ConstantInt *Min(ConstantInt *A, ConstantInt *B, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 86 | bool isSigned) { |
| 87 | return LT(A, B, isSigned) ? A : B; |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 88 | } |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 89 | static ConstantInt *Max(ConstantInt *A, ConstantInt *B, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 90 | bool isSigned) { |
| 91 | return GT(A, B, isSigned) ? A : B; |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 94 | /// Initialize a full (the default) or empty set for the specified type. |
| 95 | /// |
| 96 | ConstantRange::ConstantRange(const Type *Ty, bool Full) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 97 | assert(Ty->isInteger() && |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 98 | "Cannot make constant range of non-integral type!"); |
| 99 | if (Full) |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 100 | Lower = Upper = getMaxValue(Ty); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 101 | else |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 102 | Lower = Upper = getMinValue(Ty); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 105 | /// Initialize a range to hold the single specified value. |
| 106 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 107 | ConstantRange::ConstantRange(Constant *V) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 108 | : Lower(cast<ConstantInt>(V)), Upper(Next(cast<ConstantInt>(V))) { } |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 110 | /// 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 Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 114 | ConstantRange::ConstantRange(Constant *L, Constant *U) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 115 | : Lower(cast<ConstantInt>(L)), Upper(cast<ConstantInt>(U)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 116 | assert(Lower->getType() == Upper->getType() && |
| 117 | "Incompatible types for ConstantRange!"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 119 | // 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] | 120 | assert((L != U || (L == getMaxValue(L->getType()) || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 121 | L == getMinValue(L->getType()))) |
| 122 | && "Lower == Upper, but they aren't min or max for type!"); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 125 | /// Initialize a set of values that all satisfy the condition with C. |
| 126 | /// |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 127 | ConstantRange::ConstantRange(unsigned short ICmpOpcode, ConstantInt *C) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 128 | 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 Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 133 | Lower = getMinValue(C->getType()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 134 | Upper = C; |
| 135 | return; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 136 | case ICmpInst::ICMP_SLT: |
| 137 | Lower = getMinValue(C->getType(), true); |
| 138 | Upper = C; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 139 | return; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 140 | 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 Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 149 | Lower = getMinValue(C->getType()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 150 | Upper = Next(C); |
| 151 | return; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 152 | case ICmpInst::ICMP_SLE: |
| 153 | Lower = getMinValue(C->getType(), true); |
| 154 | Upper = Next(C); |
| 155 | return; |
| 156 | case ICmpInst::ICMP_UGE: |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 157 | Lower = C; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 158 | 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 Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /// getType - Return the LLVM data type of this range. |
| 168 | /// |
| 169 | const 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 |
| 173 | bool ConstantRange::isFullSet() const { |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 174 | return Lower == Upper && Lower == getMaxValue(getType()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 175 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 177 | /// isEmptySet - Return true if this set contains no members. |
| 178 | /// |
| 179 | bool ConstantRange::isEmptySet() const { |
Reid Spencer | c6bf4bf | 2006-12-06 20:45:15 +0000 | [diff] [blame] | 180 | return Lower == Upper && Lower == getMinValue(getType()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /// isWrappedSet - Return true if this set wraps around the top of the range, |
| 184 | /// for example: [100, 8) |
| 185 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 186 | bool ConstantRange::isWrappedSet(bool isSigned) const { |
| 187 | return GT(Lower, Upper, isSigned); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 188 | } |
| 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. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 192 | ConstantInt *ConstantRange::getSingleElement() const { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 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; |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 202 | if (getType() == Type::Int1Ty) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 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 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 215 | bool ConstantRange::contains(ConstantInt *Val, bool isSigned) const { |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 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 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 221 | if (!isWrappedSet(isSigned)) |
| 222 | return LTE(Lower, Val, isSigned) && LT(Val, Upper, isSigned); |
| 223 | return LTE(Lower, Val, isSigned) || LT(Val, Upper, isSigned); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 226 | /// subtract - Subtract the specified constant from the endpoints of this |
| 227 | /// constant range. |
| 228 | ConstantRange ConstantRange::subtract(ConstantInt *CI) const { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 229 | assert(CI->getType() == getType() && getType()->isInteger() && |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 230 | "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 Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 236 | |
| 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 | // |
| 241 | static ConstantRange intersect1Wrapped(const ConstantRange &LHS, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 242 | const ConstantRange &RHS, |
| 243 | bool isSigned) { |
| 244 | assert(LHS.isWrappedSet(isSigned) && !RHS.isWrappedSet(isSigned)); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 246 | // Check to see if we overlap on the Left side of RHS... |
| 247 | // |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 248 | if (LT(RHS.getLower(), LHS.getUpper(), isSigned)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 249 | // We do overlap on the left side of RHS, see if we overlap on the right of |
| 250 | // RHS... |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 251 | if (GT(RHS.getUpper(), LHS.getLower(), isSigned)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 252 | // 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 Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 264 | } else { |
| 265 | // We don't overlap on the left side of RHS, see if we overlap on the right |
| 266 | // of RHS... |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 267 | if (GT(RHS.getUpper(), LHS.getLower(), isSigned)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 268 | // 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 Lewycky | 3e05164 | 2007-02-11 00:58:49 +0000 | [diff] [blame] | 277 | /// intersectWith - Return the range that results from the intersection of this |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 278 | /// range with another range. |
| 279 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 280 | ConstantRange ConstantRange::intersectWith(const ConstantRange &CR, |
| 281 | bool isSigned) const { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 282 | assert(getType() == CR.getType() && "ConstantRange types don't agree!"); |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 283 | // Handle common special cases |
| 284 | if (isEmptySet() || CR.isFullSet()) return *this; |
| 285 | if (isFullSet() || CR.isEmptySet()) return CR; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 286 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 287 | if (!isWrappedSet(isSigned)) { |
| 288 | if (!CR.isWrappedSet(isSigned)) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 289 | ConstantInt *L = Max(Lower, CR.Lower, isSigned); |
| 290 | ConstantInt *U = Min(Upper, CR.Upper, isSigned); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 291 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 292 | if (LT(L, U, isSigned)) // If range isn't empty... |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 293 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 294 | else |
| 295 | return ConstantRange(getType(), false); // Otherwise, return empty set |
| 296 | } else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 297 | return intersect1Wrapped(CR, *this, isSigned); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 298 | } else { // We know "this" is wrapped... |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 299 | if (!CR.isWrappedSet(isSigned)) |
| 300 | return intersect1Wrapped(*this, CR, isSigned); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 301 | else { |
| 302 | // Both ranges are wrapped... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 303 | ConstantInt *L = Max(Lower, CR.Lower, isSigned); |
| 304 | ConstantInt *U = Min(Upper, CR.Upper, isSigned); |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 305 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | return *this; |
| 309 | } |
| 310 | |
Nick Lewycky | 3e05164 | 2007-02-11 00:58:49 +0000 | [diff] [blame] | 311 | /// unionWith - Return the range that results from the union of this range with |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 312 | /// 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 Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 317 | ConstantRange ConstantRange::unionWith(const ConstantRange &CR, |
| 318 | bool isSigned) const { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 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 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 328 | /// 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] | 329 | /// zero extended. |
| 330 | ConstantRange ConstantRange::zeroExtend(const Type *Ty) const { |
Reid Spencer | e7ca042 | 2007-01-08 01:26:33 +0000 | [diff] [blame] | 331 | unsigned SrcTySize = getLower()->getType()->getPrimitiveSizeInBits(); |
| 332 | assert(SrcTySize < Ty->getPrimitiveSizeInBits() && "Not a value extension"); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 333 | if (isFullSet()) { |
| 334 | // Change a source full set into [0, 1 << 8*numbytes) |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 335 | return ConstantRange(Constant::getNullValue(Ty), |
Reid Spencer | e7ca042 | 2007-01-08 01:26:33 +0000 | [diff] [blame] | 336 | ConstantInt::get(Ty, 1ULL << SrcTySize)); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | Constant *Lower = getLower(); |
| 340 | Constant *Upper = getUpper(); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 341 | |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 342 | return ConstantRange(ConstantExpr::getZExt(Lower, Ty), |
| 343 | ConstantExpr::getZExt(Upper, Ty)); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 344 | } |
| 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 Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 348 | /// 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] | 349 | /// truncated to the specified type. |
| 350 | ConstantRange ConstantRange::truncate(const Type *Ty) const { |
Reid Spencer | e7ca042 | 2007-01-08 01:26:33 +0000 | [diff] [blame] | 351 | unsigned SrcTySize = getLower()->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | ca7ad89 | 2007-01-08 05:34:39 +0000 | [diff] [blame] | 352 | assert(SrcTySize > Ty->getPrimitiveSizeInBits() && "Not a value truncation"); |
Reid Spencer | e7ca042 | 2007-01-08 01:26:33 +0000 | [diff] [blame] | 353 | uint64_t Size = 1ULL << Ty->getPrimitiveSizeInBits(); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 354 | if (isFullSet() || getSetSize() >= Size) |
| 355 | return ConstantRange(getType()); |
| 356 | |
Reid Spencer | 575d95c | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 357 | return ConstantRange( |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 358 | ConstantExpr::getTrunc(getLower(), Ty), |
| 359 | ConstantExpr::getTrunc(getUpper(), Ty)); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 362 | /// print - Print out the bounds to a stream... |
| 363 | /// |
| 364 | void ConstantRange::print(std::ostream &OS) const { |
Chris Lattner | ce36d55 | 2004-07-15 01:29:12 +0000 | [diff] [blame] | 365 | OS << "[" << *Lower << "," << *Upper << " )"; |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /// dump - Allow printing from a debugger easily... |
| 369 | /// |
| 370 | void ConstantRange::dump() const { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 371 | print(cerr); |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 372 | } |