Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 1 | //===-- ConstantRange.cpp - ConstantRange implementation ------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 9 | // |
| 10 | // Represent a range of possible values that may occur when the program is run |
| 11 | // for an integral value. This keeps track of a lower and upper bound for the |
| 12 | // constant, which MAY wrap around the end of the numeric range. To do this, it |
| 13 | // keeps track of a [lower, upper) bound, which specifies an interval just like |
| 14 | // STL iterators. When used with boolean values, the following are important |
| 15 | // ranges (other integral ranges use min/max values for special range values): |
| 16 | // |
| 17 | // [F, F) = {} = Empty set |
| 18 | // [T, F) = {T} |
| 19 | // [F, T) = {F} |
| 20 | // [T, T) = {F, T} = Full set |
| 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | #include "llvm/Support/ConstantRange.h" |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 25 | #include "llvm/Constants.h" |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 26 | #include "llvm/Instruction.h" |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 27 | #include "llvm/Type.h" |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 30 | static bool LT(ConstantIntegral *A, ConstantIntegral *B) { |
| 31 | Constant *C = ConstantExpr::get(Instruction::SetLT, A, B); |
| 32 | assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??"); |
| 33 | return cast<ConstantBool>(C)->getValue(); |
| 34 | } |
| 35 | |
| 36 | static bool GT(ConstantIntegral *A, ConstantIntegral *B) { |
| 37 | Constant *C = ConstantExpr::get(Instruction::SetGT, A, B); |
| 38 | assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??"); |
| 39 | return cast<ConstantBool>(C)->getValue(); |
| 40 | } |
| 41 | |
| 42 | static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) { |
| 43 | return LT(A, B) ? A : B; |
| 44 | } |
| 45 | static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) { |
| 46 | return GT(A, B) ? A : B; |
| 47 | } |
| 48 | |
| 49 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 50 | /// Initialize a full (the default) or empty set for the specified type. |
| 51 | /// |
| 52 | ConstantRange::ConstantRange(const Type *Ty, bool Full) { |
| 53 | assert(Ty->isIntegral() && |
| 54 | "Cannot make constant range of non-integral type!"); |
| 55 | if (Full) |
| 56 | Lower = Upper = ConstantIntegral::getMaxValue(Ty); |
| 57 | else |
| 58 | Lower = Upper = ConstantIntegral::getMinValue(Ty); |
| 59 | } |
| 60 | |
| 61 | /// Initialize a range of values explicitly... this will assert out if |
| 62 | /// Lower==Upper and Lower != Min or Max for its type (or if the two constants |
| 63 | /// have different types) |
| 64 | /// |
| 65 | ConstantRange::ConstantRange(ConstantIntegral *L, |
| 66 | ConstantIntegral *U) : Lower(L), Upper(U) { |
| 67 | assert(Lower->getType() == Upper->getType() && |
| 68 | "Incompatible types for ConstantRange!"); |
| 69 | |
| 70 | // Make sure that if L & U are equal that they are either Min or Max... |
| 71 | assert((L != U || (L == ConstantIntegral::getMaxValue(L->getType()) || |
| 72 | L == ConstantIntegral::getMinValue(L->getType()))) && |
| 73 | "Lower == Upper, but they aren't min or max for type!"); |
| 74 | } |
| 75 | |
| 76 | static ConstantIntegral *Next(ConstantIntegral *CI) { |
| 77 | if (CI->getType() == Type::BoolTy) |
| 78 | return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True; |
| 79 | |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 80 | Constant *Result = ConstantExpr::get(Instruction::Add, CI, |
| 81 | ConstantInt::get(CI->getType(), 1)); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 82 | return cast<ConstantIntegral>(Result); |
| 83 | } |
| 84 | |
| 85 | /// Initialize a set of values that all satisfy the condition with C. |
| 86 | /// |
| 87 | ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) { |
| 88 | switch (SetCCOpcode) { |
| 89 | default: assert(0 && "Invalid SetCC opcode to ConstantRange ctor!"); |
| 90 | case Instruction::SetEQ: Lower = C; Upper = Next(C); return; |
| 91 | case Instruction::SetNE: Upper = C; Lower = Next(C); return; |
| 92 | case Instruction::SetLT: |
| 93 | Lower = ConstantIntegral::getMinValue(C->getType()); |
| 94 | Upper = C; |
| 95 | return; |
| 96 | case Instruction::SetGT: |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 97 | Lower = Next(C); |
Chris Lattner | 20d4129 | 2002-09-03 23:12:40 +0000 | [diff] [blame] | 98 | Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max) |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 99 | return; |
| 100 | case Instruction::SetLE: |
| 101 | Lower = ConstantIntegral::getMinValue(C->getType()); |
| 102 | Upper = Next(C); |
| 103 | return; |
| 104 | case Instruction::SetGE: |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 105 | Lower = C; |
Chris Lattner | 20d4129 | 2002-09-03 23:12:40 +0000 | [diff] [blame] | 106 | Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max) |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// getType - Return the LLVM data type of this range. |
| 112 | /// |
| 113 | const Type *ConstantRange::getType() const { return Lower->getType(); } |
| 114 | |
| 115 | /// isFullSet - Return true if this set contains all of the elements possible |
| 116 | /// for this data-type |
| 117 | bool ConstantRange::isFullSet() const { |
| 118 | return Lower == Upper && Lower == ConstantIntegral::getMaxValue(getType()); |
| 119 | } |
| 120 | |
| 121 | /// isEmptySet - Return true if this set contains no members. |
| 122 | /// |
| 123 | bool ConstantRange::isEmptySet() const { |
| 124 | return Lower == Upper && Lower == ConstantIntegral::getMinValue(getType()); |
| 125 | } |
| 126 | |
| 127 | /// isWrappedSet - Return true if this set wraps around the top of the range, |
| 128 | /// for example: [100, 8) |
| 129 | /// |
| 130 | bool ConstantRange::isWrappedSet() const { |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 131 | return GT(Lower, Upper); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
| 135 | /// getSingleElement - If this set contains a single element, return it, |
| 136 | /// otherwise return null. |
| 137 | ConstantIntegral *ConstantRange::getSingleElement() const { |
| 138 | if (Upper == Next(Lower)) // Is it a single element range? |
| 139 | return Lower; |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /// getSetSize - Return the number of elements in this set. |
| 144 | /// |
| 145 | uint64_t ConstantRange::getSetSize() const { |
| 146 | if (isEmptySet()) return 0; |
| 147 | if (getType() == Type::BoolTy) { |
| 148 | if (Lower != Upper) // One of T or F in the set... |
| 149 | return 1; |
| 150 | return 2; // Must be full set... |
| 151 | } |
| 152 | |
| 153 | // Simply subtract the bounds... |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 154 | Constant *Result = |
| 155 | ConstantExpr::get(Instruction::Sub, (Constant*)Upper, (Constant*)Lower); |
Chris Lattner | c07736a | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 156 | return cast<ConstantInt>(Result)->getRawValue(); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | // intersect1Wrapped - This helper function is used to intersect two ranges when |
| 163 | // it is known that LHS is wrapped and RHS isn't. |
| 164 | // |
| 165 | static ConstantRange intersect1Wrapped(const ConstantRange &LHS, |
| 166 | const ConstantRange &RHS) { |
| 167 | assert(LHS.isWrappedSet() && !RHS.isWrappedSet()); |
| 168 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 169 | // Check to see if we overlap on the Left side of RHS... |
| 170 | // |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 171 | if (LT(RHS.getLower(), LHS.getUpper())) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 172 | // We do overlap on the left side of RHS, see if we overlap on the right of |
| 173 | // RHS... |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 174 | if (GT(RHS.getUpper(), LHS.getLower())) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 175 | // Ok, the result overlaps on both the left and right sides. See if the |
| 176 | // resultant interval will be smaller if we wrap or not... |
| 177 | // |
| 178 | if (LHS.getSetSize() < RHS.getSetSize()) |
| 179 | return LHS; |
| 180 | else |
| 181 | return RHS; |
| 182 | |
| 183 | } else { |
| 184 | // No overlap on the right, just on the left. |
| 185 | return ConstantRange(RHS.getLower(), LHS.getUpper()); |
| 186 | } |
| 187 | |
| 188 | } else { |
| 189 | // We don't overlap on the left side of RHS, see if we overlap on the right |
| 190 | // of RHS... |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 191 | if (GT(RHS.getUpper(), LHS.getLower())) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 192 | // Simple overlap... |
| 193 | return ConstantRange(LHS.getLower(), RHS.getUpper()); |
| 194 | } else { |
| 195 | // No overlap... |
| 196 | return ConstantRange(LHS.getType(), false); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 201 | /// intersect - Return the range that results from the intersection of this |
| 202 | /// range with another range. |
| 203 | /// |
| 204 | ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { |
| 205 | assert(getType() == CR.getType() && "ConstantRange types don't agree!"); |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 206 | // Handle common special cases |
| 207 | if (isEmptySet() || CR.isFullSet()) return *this; |
| 208 | if (isFullSet() || CR.isEmptySet()) return CR; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 209 | |
| 210 | if (!isWrappedSet()) { |
| 211 | if (!CR.isWrappedSet()) { |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 212 | ConstantIntegral *L = Max(Lower, CR.Lower); |
| 213 | ConstantIntegral *U = Min(Upper, CR.Upper); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 67bb760 | 2004-01-12 20:13:04 +0000 | [diff] [blame] | 215 | if (LT(L, U)) // If range isn't empty... |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 216 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 217 | else |
| 218 | return ConstantRange(getType(), false); // Otherwise, return empty set |
| 219 | } else |
| 220 | return intersect1Wrapped(CR, *this); |
| 221 | } else { // We know "this" is wrapped... |
| 222 | if (!CR.isWrappedSet()) |
| 223 | return intersect1Wrapped(*this, CR); |
| 224 | else { |
| 225 | // Both ranges are wrapped... |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 226 | ConstantIntegral *L = Max(Lower, CR.Lower); |
| 227 | ConstantIntegral *U = Min(Upper, CR.Upper); |
| 228 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | return *this; |
| 232 | } |
| 233 | |
| 234 | /// union - Return the range that results from the union of this range with |
| 235 | /// another range. The resultant range is guaranteed to include the elements of |
| 236 | /// both sets, but may contain more. For example, [3, 9) union [12,15) is [3, |
| 237 | /// 15), which includes 9, 10, and 11, which were not included in either set |
| 238 | /// before. |
| 239 | /// |
| 240 | ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { |
| 241 | assert(getType() == CR.getType() && "ConstantRange types don't agree!"); |
| 242 | |
| 243 | assert(0 && "Range union not implemented yet!"); |
| 244 | |
| 245 | return *this; |
| 246 | } |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 247 | |
| 248 | /// print - Print out the bounds to a stream... |
| 249 | /// |
| 250 | void ConstantRange::print(std::ostream &OS) const { |
| 251 | OS << "[" << Lower << "," << Upper << " )"; |
| 252 | } |
| 253 | |
| 254 | /// dump - Allow printing from a debugger easily... |
| 255 | /// |
| 256 | void ConstantRange::dump() const { |
| 257 | print(std::cerr); |
| 258 | } |