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