Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 1 | //===- LoopVR.cpp - Value Range analysis driven by loop information -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 9 | // |
| 10 | // FIXME: What does this do? |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 13 | |
| 14 | #define DEBUG_TYPE "loopvr" |
| 15 | #include "llvm/Analysis/LoopVR.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/Instructions.h" |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 18 | #include "llvm/LLVMContext.h" |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 20 | #include "llvm/Assembly/Writer.h" |
| 21 | #include "llvm/Support/CFG.h" |
| 22 | #include "llvm/Support/Debug.h" |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | char LoopVR::ID = 0; |
Dan Gohman | 2ba682c | 2009-03-23 15:50:52 +0000 | [diff] [blame] | 27 | static RegisterPass<LoopVR> X("loopvr", "Loop Value Ranges", false, true); |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 28 | |
| 29 | /// getRange - determine the range for a particular SCEV within a given Loop |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame^] | 30 | ConstantRange LoopVR::getRange(const SCEV *S, Loop *L, ScalarEvolution &SE) { |
| 31 | const SCEV *T = SE.getBackedgeTakenCount(L); |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 32 | if (isa<SCEVCouldNotCompute>(T)) |
| 33 | return ConstantRange(cast<IntegerType>(S->getType())->getBitWidth(), true); |
| 34 | |
| 35 | T = SE.getTruncateOrZeroExtend(T, S->getType()); |
| 36 | return getRange(S, T, SE); |
| 37 | } |
| 38 | |
| 39 | /// getRange - determine the range for a particular SCEV with a given trip count |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame^] | 40 | ConstantRange LoopVR::getRange(const SCEV *S, const SCEV *T, ScalarEvolution &SE){ |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 41 | |
Dan Gohman | b40c236 | 2009-04-18 17:57:20 +0000 | [diff] [blame] | 42 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 43 | return ConstantRange(C->getValue()->getValue()); |
| 44 | |
| 45 | ConstantRange FullSet(cast<IntegerType>(S->getType())->getBitWidth(), true); |
| 46 | |
| 47 | // {x,+,y,+,...z}. We detect overflow by checking the size of the set after |
| 48 | // summing the upper and lower. |
Dan Gohman | b40c236 | 2009-04-18 17:57:20 +0000 | [diff] [blame] | 49 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 50 | ConstantRange X = getRange(Add->getOperand(0), T, SE); |
| 51 | if (X.isFullSet()) return FullSet; |
| 52 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) { |
| 53 | ConstantRange Y = getRange(Add->getOperand(i), T, SE); |
| 54 | if (Y.isFullSet()) return FullSet; |
| 55 | |
| 56 | APInt Spread_X = X.getSetSize(), Spread_Y = Y.getSetSize(); |
| 57 | APInt NewLower = X.getLower() + Y.getLower(); |
| 58 | APInt NewUpper = X.getUpper() + Y.getUpper() - 1; |
| 59 | if (NewLower == NewUpper) |
| 60 | return FullSet; |
| 61 | |
| 62 | X = ConstantRange(NewLower, NewUpper); |
| 63 | if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y)) |
| 64 | return FullSet; // we've wrapped, therefore, full set. |
| 65 | } |
| 66 | return X; |
| 67 | } |
| 68 | |
| 69 | // {x,*,y,*,...,z}. In order to detect overflow, we use k*bitwidth where |
| 70 | // k is the number of terms being multiplied. |
Dan Gohman | b40c236 | 2009-04-18 17:57:20 +0000 | [diff] [blame] | 71 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 72 | ConstantRange X = getRange(Mul->getOperand(0), T, SE); |
| 73 | if (X.isFullSet()) return FullSet; |
| 74 | |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 75 | const IntegerType *Ty = Context->getIntegerType(X.getBitWidth()); |
| 76 | const IntegerType *ExTy = Context->getIntegerType(X.getBitWidth() * |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 77 | Mul->getNumOperands()); |
| 78 | ConstantRange XExt = X.zeroExtend(ExTy->getBitWidth()); |
| 79 | |
| 80 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) { |
| 81 | ConstantRange Y = getRange(Mul->getOperand(i), T, SE); |
| 82 | if (Y.isFullSet()) return FullSet; |
| 83 | |
| 84 | ConstantRange YExt = Y.zeroExtend(ExTy->getBitWidth()); |
| 85 | XExt = ConstantRange(XExt.getLower() * YExt.getLower(), |
| 86 | ((XExt.getUpper()-1) * (YExt.getUpper()-1)) + 1); |
| 87 | } |
| 88 | return XExt.truncate(Ty->getBitWidth()); |
| 89 | } |
| 90 | |
| 91 | // X smax Y smax ... Z is: range(smax(X_smin, Y_smin, ..., Z_smin), |
| 92 | // smax(X_smax, Y_smax, ..., Z_smax)) |
| 93 | // It doesn't matter if one of the SCEVs has FullSet because we're taking |
| 94 | // a maximum of the minimums across all of them. |
Dan Gohman | b40c236 | 2009-04-18 17:57:20 +0000 | [diff] [blame] | 95 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 96 | ConstantRange X = getRange(SMax->getOperand(0), T, SE); |
| 97 | if (X.isFullSet()) return FullSet; |
| 98 | |
| 99 | APInt smin = X.getSignedMin(), smax = X.getSignedMax(); |
| 100 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) { |
| 101 | ConstantRange Y = getRange(SMax->getOperand(i), T, SE); |
| 102 | smin = APIntOps::smax(smin, Y.getSignedMin()); |
| 103 | smax = APIntOps::smax(smax, Y.getSignedMax()); |
| 104 | } |
| 105 | if (smax + 1 == smin) return FullSet; |
| 106 | return ConstantRange(smin, smax + 1); |
| 107 | } |
| 108 | |
| 109 | // X umax Y umax ... Z is: range(umax(X_umin, Y_umin, ..., Z_umin), |
| 110 | // umax(X_umax, Y_umax, ..., Z_umax)) |
| 111 | // It doesn't matter if one of the SCEVs has FullSet because we're taking |
| 112 | // a maximum of the minimums across all of them. |
Dan Gohman | b40c236 | 2009-04-18 17:57:20 +0000 | [diff] [blame] | 113 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 114 | ConstantRange X = getRange(UMax->getOperand(0), T, SE); |
| 115 | if (X.isFullSet()) return FullSet; |
| 116 | |
| 117 | APInt umin = X.getUnsignedMin(), umax = X.getUnsignedMax(); |
| 118 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) { |
| 119 | ConstantRange Y = getRange(UMax->getOperand(i), T, SE); |
| 120 | umin = APIntOps::umax(umin, Y.getUnsignedMin()); |
| 121 | umax = APIntOps::umax(umax, Y.getUnsignedMax()); |
| 122 | } |
| 123 | if (umax + 1 == umin) return FullSet; |
| 124 | return ConstantRange(umin, umax + 1); |
| 125 | } |
| 126 | |
| 127 | // L udiv R. Luckily, there's only ever 2 sides to a udiv. |
Dan Gohman | b40c236 | 2009-04-18 17:57:20 +0000 | [diff] [blame] | 128 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 129 | ConstantRange L = getRange(UDiv->getLHS(), T, SE); |
| 130 | ConstantRange R = getRange(UDiv->getRHS(), T, SE); |
| 131 | if (L.isFullSet() && R.isFullSet()) return FullSet; |
| 132 | |
| 133 | if (R.getUnsignedMax() == 0) { |
| 134 | // RHS must be single-element zero. Return an empty set. |
| 135 | return ConstantRange(R.getBitWidth(), false); |
| 136 | } |
| 137 | |
| 138 | APInt Lower = L.getUnsignedMin().udiv(R.getUnsignedMax()); |
| 139 | |
| 140 | APInt Upper; |
| 141 | |
| 142 | if (R.getUnsignedMin() == 0) { |
| 143 | // Just because it contains zero, doesn't mean it will also contain one. |
| 144 | // Use maximalIntersectWith to get the right behaviour. |
| 145 | ConstantRange NotZero(APInt(L.getBitWidth(), 1), |
| 146 | APInt::getNullValue(L.getBitWidth())); |
| 147 | R = R.maximalIntersectWith(NotZero); |
| 148 | } |
| 149 | |
| 150 | // But, the maximal intersection might still include zero. If it does, then |
| 151 | // we know it also included one. |
| 152 | if (R.contains(APInt::getNullValue(L.getBitWidth()))) |
| 153 | Upper = L.getUnsignedMax(); |
| 154 | else |
| 155 | Upper = L.getUnsignedMax().udiv(R.getUnsignedMin()); |
| 156 | |
| 157 | return ConstantRange(Lower, Upper); |
| 158 | } |
| 159 | |
| 160 | // ConstantRange already implements the cast operators. |
| 161 | |
Dan Gohman | b40c236 | 2009-04-18 17:57:20 +0000 | [diff] [blame] | 162 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 163 | T = SE.getTruncateOrZeroExtend(T, ZExt->getOperand()->getType()); |
| 164 | ConstantRange X = getRange(ZExt->getOperand(), T, SE); |
| 165 | return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth()); |
| 166 | } |
| 167 | |
Dan Gohman | b40c236 | 2009-04-18 17:57:20 +0000 | [diff] [blame] | 168 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 169 | T = SE.getTruncateOrZeroExtend(T, SExt->getOperand()->getType()); |
| 170 | ConstantRange X = getRange(SExt->getOperand(), T, SE); |
| 171 | return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth()); |
| 172 | } |
| 173 | |
Dan Gohman | b40c236 | 2009-04-18 17:57:20 +0000 | [diff] [blame] | 174 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 175 | T = SE.getTruncateOrZeroExtend(T, Trunc->getOperand()->getType()); |
| 176 | ConstantRange X = getRange(Trunc->getOperand(), T, SE); |
| 177 | if (X.isFullSet()) return FullSet; |
| 178 | return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth()); |
| 179 | } |
| 180 | |
Dan Gohman | b40c236 | 2009-04-18 17:57:20 +0000 | [diff] [blame] | 181 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { |
| 182 | const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T); |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 183 | if (!Trip) return FullSet; |
| 184 | |
| 185 | if (AddRec->isAffine()) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame^] | 186 | const SCEV *StartHandle = AddRec->getStart(); |
| 187 | const SCEV *StepHandle = AddRec->getOperand(1); |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 188 | |
Dan Gohman | b40c236 | 2009-04-18 17:57:20 +0000 | [diff] [blame] | 189 | const SCEVConstant *Step = dyn_cast<SCEVConstant>(StepHandle); |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 190 | if (!Step) return FullSet; |
| 191 | |
| 192 | uint32_t ExWidth = 2 * Trip->getValue()->getBitWidth(); |
| 193 | APInt TripExt = Trip->getValue()->getValue(); TripExt.zext(ExWidth); |
| 194 | APInt StepExt = Step->getValue()->getValue(); StepExt.zext(ExWidth); |
| 195 | if ((TripExt * StepExt).ugt(APInt::getLowBitsSet(ExWidth, ExWidth >> 1))) |
| 196 | return FullSet; |
| 197 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame^] | 198 | const SCEV *EndHandle = SE.getAddExpr(StartHandle, |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 199 | SE.getMulExpr(T, StepHandle)); |
Dan Gohman | b40c236 | 2009-04-18 17:57:20 +0000 | [diff] [blame] | 200 | const SCEVConstant *Start = dyn_cast<SCEVConstant>(StartHandle); |
| 201 | const SCEVConstant *End = dyn_cast<SCEVConstant>(EndHandle); |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 202 | if (!Start || !End) return FullSet; |
| 203 | |
| 204 | const APInt &StartInt = Start->getValue()->getValue(); |
| 205 | const APInt &EndInt = End->getValue()->getValue(); |
| 206 | const APInt &StepInt = Step->getValue()->getValue(); |
| 207 | |
| 208 | if (StepInt.isNegative()) { |
| 209 | if (EndInt == StartInt + 1) return FullSet; |
| 210 | return ConstantRange(EndInt, StartInt + 1); |
| 211 | } else { |
| 212 | if (StartInt == EndInt + 1) return FullSet; |
| 213 | return ConstantRange(StartInt, EndInt + 1); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // TODO: non-affine addrec, udiv, SCEVUnknown (narrowed from elsewhere)? |
| 219 | |
| 220 | return FullSet; |
| 221 | } |
| 222 | |
| 223 | bool LoopVR::runOnFunction(Function &F) { Map.clear(); return false; } |
| 224 | |
| 225 | void LoopVR::print(std::ostream &os, const Module *) const { |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 226 | raw_os_ostream OS(os); |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 227 | for (std::map<Value *, ConstantRange *>::const_iterator I = Map.begin(), |
| 228 | E = Map.end(); I != E; ++I) { |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 229 | OS << *I->first << ": " << *I->second << '\n'; |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | void LoopVR::releaseMemory() { |
| 234 | for (std::map<Value *, ConstantRange *>::iterator I = Map.begin(), |
| 235 | E = Map.end(); I != E; ++I) { |
| 236 | delete I->second; |
| 237 | } |
| 238 | |
| 239 | Map.clear(); |
| 240 | } |
| 241 | |
| 242 | ConstantRange LoopVR::compute(Value *V) { |
| 243 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 244 | return ConstantRange(CI->getValue()); |
| 245 | |
| 246 | Instruction *I = dyn_cast<Instruction>(V); |
| 247 | if (!I) |
| 248 | return ConstantRange(cast<IntegerType>(V->getType())->getBitWidth(), false); |
| 249 | |
| 250 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 251 | |
| 252 | Loop *L = LI.getLoopFor(I->getParent()); |
Torok Edwin | c83889a | 2008-10-27 10:18:45 +0000 | [diff] [blame] | 253 | if (!L || L->isLoopInvariant(I)) |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 254 | return ConstantRange(cast<IntegerType>(V->getType())->getBitWidth(), false); |
| 255 | |
Torok Edwin | c83889a | 2008-10-27 10:18:45 +0000 | [diff] [blame] | 256 | ScalarEvolution &SE = getAnalysis<ScalarEvolution>(); |
| 257 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame^] | 258 | const SCEV *S = SE.getSCEV(I); |
Nick Lewycky | a11e2eb | 2008-06-30 00:04:21 +0000 | [diff] [blame] | 259 | if (isa<SCEVUnknown>(S) || isa<SCEVCouldNotCompute>(S)) |
| 260 | return ConstantRange(cast<IntegerType>(V->getType())->getBitWidth(), false); |
| 261 | |
| 262 | return ConstantRange(getRange(S, L, SE)); |
| 263 | } |
| 264 | |
| 265 | ConstantRange LoopVR::get(Value *V) { |
| 266 | std::map<Value *, ConstantRange *>::iterator I = Map.find(V); |
| 267 | if (I == Map.end()) { |
| 268 | ConstantRange *CR = new ConstantRange(compute(V)); |
| 269 | Map[V] = CR; |
| 270 | return *CR; |
| 271 | } |
| 272 | |
| 273 | return *I->second; |
| 274 | } |
| 275 | |
| 276 | void LoopVR::remove(Value *V) { |
| 277 | std::map<Value *, ConstantRange *>::iterator I = Map.find(V); |
| 278 | if (I != Map.end()) { |
| 279 | delete I->second; |
| 280 | Map.erase(I); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void LoopVR::narrow(Value *V, const ConstantRange &CR) { |
| 285 | if (CR.isFullSet()) return; |
| 286 | |
| 287 | std::map<Value *, ConstantRange *>::iterator I = Map.find(V); |
| 288 | if (I == Map.end()) |
| 289 | Map[V] = new ConstantRange(CR); |
| 290 | else |
| 291 | Map[V] = new ConstantRange(Map[V]->maximalIntersectWith(CR)); |
| 292 | } |