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