Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 1 | //===- LoopStrengthReduce.cpp - Strength Reduce GEPs in Loops -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Nate Begeman and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass performs a strength reduction on array references inside loops that |
| 11 | // have as one or more of their components the loop induction variable. This is |
| 12 | // accomplished by creating a new Value to hold the initial value of the array |
| 13 | // access for the first iteration, and then creating a new GEP instruction in |
| 14 | // the loop to increment the value by the appropriate amount. |
| 15 | // |
| 16 | // There are currently several deficiencies in the implementation, marked with |
| 17 | // FIXME in the code. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "llvm/Transforms/Scalar.h" |
| 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/Instructions.h" |
| 24 | #include "llvm/Type.h" |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame^] | 25 | #include "llvm/DerivedTypes.h" |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/Dominators.h" |
| 27 | #include "llvm/Analysis/LoopInfo.h" |
| 28 | #include "llvm/Support/CFG.h" |
| 29 | #include "llvm/Transforms/Utils/Local.h" |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame^] | 30 | #include "llvm/Target/TargetData.h" |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/Statistic.h" |
| 32 | #include <set> |
| 33 | using namespace llvm; |
| 34 | |
| 35 | namespace { |
| 36 | Statistic<> NumReduced ("loop-reduce", "Number of GEPs strength reduced"); |
| 37 | |
| 38 | class LoopStrengthReduce : public FunctionPass { |
| 39 | LoopInfo *LI; |
| 40 | DominatorSet *DS; |
| 41 | bool Changed; |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame^] | 42 | unsigned MaxTargetAMSize; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 43 | public: |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame^] | 44 | LoopStrengthReduce(unsigned MTAMS = 1) |
| 45 | : MaxTargetAMSize(MTAMS) { |
| 46 | } |
| 47 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 48 | virtual bool runOnFunction(Function &) { |
| 49 | LI = &getAnalysis<LoopInfo>(); |
| 50 | DS = &getAnalysis<DominatorSet>(); |
| 51 | Changed = false; |
| 52 | |
| 53 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 54 | runOnLoop(*I); |
| 55 | return Changed; |
| 56 | } |
| 57 | |
| 58 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 59 | AU.setPreservesCFG(); |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 60 | AU.addRequiredID(LoopSimplifyID); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 61 | AU.addRequired<LoopInfo>(); |
| 62 | AU.addRequired<DominatorSet>(); |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame^] | 63 | AU.addRequired<TargetData>(); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 64 | } |
| 65 | private: |
| 66 | void runOnLoop(Loop *L); |
| 67 | void strengthReduceGEP(GetElementPtrInst *GEPI, Loop *L, |
| 68 | Instruction *InsertBefore, |
| 69 | std::set<Instruction*> &DeadInsts); |
| 70 | void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts); |
| 71 | }; |
| 72 | RegisterOpt<LoopStrengthReduce> X("loop-reduce", |
| 73 | "Strength Reduce GEP Uses of Ind. Vars"); |
| 74 | } |
| 75 | |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame^] | 76 | FunctionPass *llvm::createLoopStrengthReducePass(unsigned MaxTargetAMSize) { |
| 77 | return new LoopStrengthReduce(MaxTargetAMSize); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /// DeleteTriviallyDeadInstructions - If any of the instructions is the |
| 81 | /// specified set are trivially dead, delete them and see if this makes any of |
| 82 | /// their operands subsequently dead. |
| 83 | void LoopStrengthReduce:: |
| 84 | DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) { |
| 85 | while (!Insts.empty()) { |
| 86 | Instruction *I = *Insts.begin(); |
| 87 | Insts.erase(Insts.begin()); |
| 88 | if (isInstructionTriviallyDead(I)) { |
Jeff Cohen | 8ea6f9e | 2005-03-01 03:46:11 +0000 | [diff] [blame] | 89 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 90 | if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i))) |
| 91 | Insts.insert(U); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 92 | I->getParent()->getInstList().erase(I); |
| 93 | Changed = true; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void LoopStrengthReduce::strengthReduceGEP(GetElementPtrInst *GEPI, Loop *L, |
| 99 | Instruction *InsertBefore, |
| 100 | std::set<Instruction*> &DeadInsts) { |
| 101 | // We will strength reduce the GEP by splitting it into two parts. The first |
| 102 | // is a GEP to hold the initial value of the non-strength-reduced GEP upon |
| 103 | // entering the loop, which we will insert at the end of the loop preheader. |
| 104 | // The second is a GEP to hold the incremented value of the initial GEP. |
| 105 | // The LoopIndVarSimplify pass guarantees that loop counts start at zero, so |
| 106 | // we will replace the indvar with a constant zero value to create the first |
| 107 | // GEP. |
| 108 | // |
| 109 | // We currently only handle GEP instructions that consist of zero or more |
Jeff Cohen | fd63d3a | 2005-02-27 21:08:04 +0000 | [diff] [blame] | 110 | // constants or loop invariable expressions prior to an instance of the |
| 111 | // canonical induction variable. |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 112 | unsigned indvar = 0; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 113 | std::vector<Value *> pre_op_vector; |
| 114 | std::vector<Value *> inc_op_vector; |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame^] | 115 | const Type *ty = GEPI->getOperand(0)->getType(); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 116 | Value *CanonicalIndVar = L->getCanonicalInductionVariable(); |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 117 | BasicBlock *Header = L->getHeader(); |
Jeff Cohen | 8ea6f9e | 2005-03-01 03:46:11 +0000 | [diff] [blame] | 118 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 119 | bool AllConstantOperands = true; |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 120 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 121 | for (unsigned op = 1, e = GEPI->getNumOperands(); op != e; ++op) { |
| 122 | Value *operand = GEPI->getOperand(op); |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame^] | 123 | if (ty->getTypeID() == Type::StructTyID) { |
| 124 | assert(isa<ConstantUInt>(operand)); |
| 125 | ConstantUInt *c = dyn_cast<ConstantUInt>(operand); |
| 126 | ty = ty->getContainedType(unsigned(c->getValue())); |
| 127 | } else { |
| 128 | ty = ty->getContainedType(0); |
| 129 | } |
| 130 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 131 | if (operand == CanonicalIndVar) { |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 132 | // FIXME: use getCanonicalInductionVariableIncrement to choose between |
| 133 | // one and neg one maybe? We need to support int *foo = GEP base, -1 |
| 134 | const Type *Ty = CanonicalIndVar->getType(); |
| 135 | pre_op_vector.push_back(Constant::getNullValue(Ty)); |
| 136 | inc_op_vector.push_back(ConstantInt::get(Ty, 1)); |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 137 | indvar = op; |
| 138 | break; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 139 | } else if (isa<Constant>(operand)) { |
| 140 | pre_op_vector.push_back(operand); |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 141 | } else if (Instruction *inst = dyn_cast<Instruction>(operand)) { |
Jeff Cohen | 8ea6f9e | 2005-03-01 03:46:11 +0000 | [diff] [blame] | 142 | if (!DS->dominates(inst, Preheader->getTerminator())) |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 143 | return; |
| 144 | pre_op_vector.push_back(operand); |
Jeff Cohen | 8ea6f9e | 2005-03-01 03:46:11 +0000 | [diff] [blame] | 145 | AllConstantOperands = false; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 146 | } else |
| 147 | return; |
| 148 | } |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 149 | assert(indvar > 0 && "Indvar used by GEP not found in operand list"); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 150 | |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 151 | // Ensure the pointer base is loop invariant. While strength reduction |
| 152 | // makes sense even if the pointer changed on every iteration, there is no |
| 153 | // realistic way of handling it unless GEPs were completely decomposed into |
| 154 | // their constituent operations so we have explicit multiplications to work |
| 155 | // with. |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 156 | if (Instruction *GepPtrOp = dyn_cast<Instruction>(GEPI->getOperand(0))) |
Jeff Cohen | 8ea6f9e | 2005-03-01 03:46:11 +0000 | [diff] [blame] | 157 | if (!DS->dominates(GepPtrOp, Preheader->getTerminator())) |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 158 | return; |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame^] | 159 | |
| 160 | // Don't reduced multiplies that the target can handle via addressing modes. |
| 161 | uint64_t sz = getAnalysis<TargetData>().getTypeSize(ty); |
| 162 | for (unsigned i = 1; i <= MaxTargetAMSize; i *= 2) |
| 163 | if (i == sz) |
| 164 | return; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 165 | |
| 166 | // If all operands of the GEP we are going to insert into the preheader |
| 167 | // are constants, generate a GEP ConstantExpr instead. |
| 168 | // |
| 169 | // If there is only one operand after the initial non-constant one, we know |
| 170 | // that it was the induction variable, and has been replaced by a constant |
| 171 | // null value. In this case, replace the GEP with a use of pointer directly. |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 172 | Value *PreGEP; |
Jeff Cohen | 8ea6f9e | 2005-03-01 03:46:11 +0000 | [diff] [blame] | 173 | if (AllConstantOperands && isa<Constant>(GEPI->getOperand(0))) { |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 174 | Constant *C = dyn_cast<Constant>(GEPI->getOperand(0)); |
| 175 | PreGEP = ConstantExpr::getGetElementPtr(C, pre_op_vector); |
| 176 | } else if (pre_op_vector.size() == 1) { |
| 177 | PreGEP = GEPI->getOperand(0); |
| 178 | } else { |
| 179 | PreGEP = new GetElementPtrInst(GEPI->getOperand(0), |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 180 | pre_op_vector, GEPI->getName()+".pre", |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 181 | Preheader->getTerminator()); |
| 182 | } |
| 183 | |
| 184 | // The next step of the strength reduction is to create a PHI that will choose |
| 185 | // between the initial GEP we created and inserted into the preheader, and |
| 186 | // the incremented GEP that we will create below and insert into the loop body |
| 187 | PHINode *NewPHI = new PHINode(PreGEP->getType(), |
| 188 | GEPI->getName()+".str", InsertBefore); |
| 189 | NewPHI->addIncoming(PreGEP, Preheader); |
| 190 | |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 191 | // Now, create the GEP instruction to increment by one the value selected by |
| 192 | // the PHI instruction we just created above, and add it as the second |
| 193 | // incoming Value/BasicBlock pair to the PHINode. It is inserted before the |
| 194 | // increment of the canonical induction variable. |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 195 | Instruction *IncrInst = |
| 196 | const_cast<Instruction*>(L->getCanonicalInductionVariableIncrement()); |
| 197 | GetElementPtrInst *StrGEP = new GetElementPtrInst(NewPHI, inc_op_vector, |
| 198 | GEPI->getName()+".inc", |
| 199 | IncrInst); |
Jeff Cohen | 8ea6f9e | 2005-03-01 03:46:11 +0000 | [diff] [blame] | 200 | pred_iterator PI = pred_begin(Header); |
| 201 | if (*PI == Preheader) |
| 202 | ++PI; |
| 203 | NewPHI->addIncoming(StrGEP, *PI); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 204 | |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 205 | if (GEPI->getNumOperands() - 1 == indvar) { |
| 206 | // If there were no operands following the induction variable, replace all |
| 207 | // uses of the old GEP instruction with the new PHI. |
| 208 | GEPI->replaceAllUsesWith(NewPHI); |
| 209 | } else { |
| 210 | // Create a new GEP instruction using the new PHI as the base. The |
| 211 | // operands of the original GEP past the induction variable become |
| 212 | // operands of this new GEP. |
| 213 | std::vector<Value *> op_vector; |
| 214 | const Type *Ty = CanonicalIndVar->getType(); |
| 215 | op_vector.push_back(Constant::getNullValue(Ty)); |
| 216 | for (unsigned op = indvar + 1; op < GEPI->getNumOperands(); op++) |
| 217 | op_vector.push_back(GEPI->getOperand(op)); |
| 218 | GetElementPtrInst *newGEP = new GetElementPtrInst(NewPHI, op_vector, |
| 219 | GEPI->getName() + ".lsr", |
| 220 | GEPI); |
| 221 | GEPI->replaceAllUsesWith(newGEP); |
| 222 | } |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 223 | |
| 224 | // The old GEP is now dead. |
| 225 | DeadInsts.insert(GEPI); |
| 226 | ++NumReduced; |
| 227 | } |
| 228 | |
| 229 | void LoopStrengthReduce::runOnLoop(Loop *L) { |
| 230 | // First step, transform all loops nesting inside of this loop. |
| 231 | for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 232 | runOnLoop(*I); |
| 233 | |
| 234 | // Next, get the first PHINode since it is guaranteed to be the canonical |
| 235 | // induction variable for the loop by the preceding IndVarSimplify pass. |
| 236 | PHINode *PN = L->getCanonicalInductionVariable(); |
| 237 | if (0 == PN) |
| 238 | return; |
| 239 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 240 | // FIXME: Need to use SCEV to detect GEP uses of the indvar, since indvars |
| 241 | // pass creates code like this, which we can't currently detect: |
| 242 | // %tmp.1 = sub uint 2000, %indvar |
| 243 | // %tmp.8 = getelementptr int* %y, uint %tmp.1 |
| 244 | |
Jeff Cohen | fd63d3a | 2005-02-27 21:08:04 +0000 | [diff] [blame] | 245 | // Strength reduce all GEPs in the Loop. Insert secondary PHI nodes for the |
| 246 | // strength reduced pointers we'll be creating after the canonical induction |
| 247 | // variable's PHI. |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 248 | std::set<Instruction*> DeadInsts; |
| 249 | for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end(); |
| 250 | UI != UE; ++UI) |
| 251 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI)) |
Jeff Cohen | fd63d3a | 2005-02-27 21:08:04 +0000 | [diff] [blame] | 252 | strengthReduceGEP(GEPI, L, PN->getNext(), DeadInsts); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 253 | |
| 254 | // Clean up after ourselves |
| 255 | if (!DeadInsts.empty()) { |
| 256 | DeleteTriviallyDeadInstructions(DeadInsts); |
| 257 | |
| 258 | // At this point, we know that we have killed one or more GEP instructions. |
| 259 | // It is worth checking to see if the cann indvar is also dead, so that we |
| 260 | // can remove it as well. The requirements for the cann indvar to be |
| 261 | // considered dead are: |
| 262 | // 1. the cann indvar has one use |
| 263 | // 2. the use is an add instruction |
| 264 | // 3. the add has one use |
| 265 | // 4. the add is used by the cann indvar |
| 266 | // If all four cases above are true, then we can remove both the add and |
| 267 | // the cann indvar. |
Jeff Cohen | 8ea6f9e | 2005-03-01 03:46:11 +0000 | [diff] [blame] | 268 | // FIXME: this needs to eliminate an induction variable even if it's being |
| 269 | // compared against some value to decide loop termination. |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 270 | if (PN->hasOneUse()) { |
| 271 | BinaryOperator *BO = dyn_cast<BinaryOperator>(*(PN->use_begin())); |
| 272 | if (BO && BO->getOpcode() == Instruction::Add) |
| 273 | if (BO->hasOneUse()) { |
Jeff Cohen | 8ea6f9e | 2005-03-01 03:46:11 +0000 | [diff] [blame] | 274 | if (PN == dyn_cast<PHINode>(*(BO->use_begin()))) { |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 275 | DeadInsts.insert(BO); |
Jeff Cohen | 8ea6f9e | 2005-03-01 03:46:11 +0000 | [diff] [blame] | 276 | // Break the cycle, then delete the PHI. |
| 277 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
| 278 | PN->eraseFromParent(); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 279 | DeleteTriviallyDeadInstructions(DeadInsts); |
| 280 | } |
| 281 | } |
| 282 | } |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 283 | } |
| 284 | } |