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