Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 1 | //===- LoopStrengthReduce.cpp - Strength Reduce GEPs in Loops -------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 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. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 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 | // |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Chris Lattner | bb78c97 | 2005-08-03 23:30:08 +0000 | [diff] [blame] | 18 | #define DEBUG_TYPE "loop-reduce" |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Scalar.h" |
| 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/Instructions.h" |
| 22 | #include "llvm/Type.h" |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 23 | #include "llvm/DerivedTypes.h" |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/Dominators.h" |
| 25 | #include "llvm/Analysis/LoopInfo.h" |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CFG.h" |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 28 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 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" |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
Jeff Cohen | c500991 | 2005-07-30 18:22:27 +0000 | [diff] [blame] | 33 | #include <algorithm> |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 34 | #include <set> |
| 35 | using namespace llvm; |
| 36 | |
| 37 | namespace { |
| 38 | Statistic<> NumReduced ("loop-reduce", "Number of GEPs strength reduced"); |
| 39 | |
Chris Lattner | d3874fa | 2005-03-06 21:58:22 +0000 | [diff] [blame] | 40 | class GEPCache { |
Jeff Cohen | be37fa0 | 2005-03-05 22:40:34 +0000 | [diff] [blame] | 41 | public: |
| 42 | GEPCache() : CachedPHINode(0), Map() {} |
| 43 | |
Chris Lattner | d3874fa | 2005-03-06 21:58:22 +0000 | [diff] [blame] | 44 | GEPCache *get(Value *v) { |
Jeff Cohen | be37fa0 | 2005-03-05 22:40:34 +0000 | [diff] [blame] | 45 | std::map<Value *, GEPCache>::iterator I = Map.find(v); |
| 46 | if (I == Map.end()) |
| 47 | I = Map.insert(std::pair<Value *, GEPCache>(v, GEPCache())).first; |
Chris Lattner | d3874fa | 2005-03-06 21:58:22 +0000 | [diff] [blame] | 48 | return &I->second; |
Jeff Cohen | be37fa0 | 2005-03-05 22:40:34 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | PHINode *CachedPHINode; |
| 52 | std::map<Value *, GEPCache> Map; |
| 53 | }; |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 54 | |
| 55 | /// IVStrideUse - Keep track of one use of a strided induction variable, where |
| 56 | /// the stride is stored externally. The Offset member keeps track of the |
| 57 | /// offset from the IV, User is the actual user of the operand, and 'Operand' |
| 58 | /// is the operand # of the User that is the use. |
| 59 | struct IVStrideUse { |
| 60 | SCEVHandle Offset; |
| 61 | Instruction *User; |
| 62 | Value *OperandValToReplace; |
| 63 | |
| 64 | IVStrideUse(const SCEVHandle &Offs, Instruction *U, Value *O) |
| 65 | : Offset(Offs), User(U), OperandValToReplace(O) {} |
| 66 | }; |
| 67 | |
| 68 | /// IVUsersOfOneStride - This structure keeps track of all instructions that |
| 69 | /// have an operand that is based on the trip count multiplied by some stride. |
| 70 | /// The stride for all of these users is common and kept external to this |
| 71 | /// structure. |
| 72 | struct IVUsersOfOneStride { |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 73 | /// Users - Keep track of all of the users of this stride as well as the |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 74 | /// initial value and the operand that uses the IV. |
| 75 | std::vector<IVStrideUse> Users; |
| 76 | |
| 77 | void addUser(const SCEVHandle &Offset,Instruction *User, Value *Operand) { |
| 78 | Users.push_back(IVStrideUse(Offset, User, Operand)); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | |
| 82 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 83 | class LoopStrengthReduce : public FunctionPass { |
| 84 | LoopInfo *LI; |
| 85 | DominatorSet *DS; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 86 | ScalarEvolution *SE; |
| 87 | const TargetData *TD; |
| 88 | const Type *UIntPtrTy; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 89 | bool Changed; |
Chris Lattner | 75a44e1 | 2005-08-02 02:52:02 +0000 | [diff] [blame] | 90 | |
| 91 | /// MaxTargetAMSize - This is the maximum power-of-two scale value that the |
| 92 | /// target can handle for free with its addressing modes. |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 93 | unsigned MaxTargetAMSize; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 94 | |
| 95 | /// IVUsesByStride - Keep track of all uses of induction variables that we |
| 96 | /// are interested in. The key of the map is the stride of the access. |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 97 | std::map<Value*, IVUsersOfOneStride> IVUsesByStride; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 6f286b7 | 2005-08-04 01:19:13 +0000 | [diff] [blame] | 99 | /// CastedValues - As we need to cast values to uintptr_t, this keeps track |
| 100 | /// of the casted version of each value. This is accessed by |
| 101 | /// getCastedVersionOf. |
| 102 | std::map<Value*, Value*> CastedPointers; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 103 | |
| 104 | /// DeadInsts - Keep track of instructions we may have made dead, so that |
| 105 | /// we can remove them after we are done working. |
| 106 | std::set<Instruction*> DeadInsts; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 107 | public: |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 108 | LoopStrengthReduce(unsigned MTAMS = 1) |
| 109 | : MaxTargetAMSize(MTAMS) { |
| 110 | } |
| 111 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 112 | virtual bool runOnFunction(Function &) { |
| 113 | LI = &getAnalysis<LoopInfo>(); |
| 114 | DS = &getAnalysis<DominatorSet>(); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 115 | SE = &getAnalysis<ScalarEvolution>(); |
| 116 | TD = &getAnalysis<TargetData>(); |
| 117 | UIntPtrTy = TD->getIntPtrType(); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 118 | Changed = false; |
| 119 | |
| 120 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 121 | runOnLoop(*I); |
Chris Lattner | 6f286b7 | 2005-08-04 01:19:13 +0000 | [diff] [blame] | 122 | |
| 123 | CastedPointers.clear(); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 124 | return Changed; |
| 125 | } |
| 126 | |
| 127 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 128 | AU.setPreservesCFG(); |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 129 | AU.addRequiredID(LoopSimplifyID); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 130 | AU.addRequired<LoopInfo>(); |
| 131 | AU.addRequired<DominatorSet>(); |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 132 | AU.addRequired<TargetData>(); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 133 | AU.addRequired<ScalarEvolution>(); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 134 | } |
Chris Lattner | 6f286b7 | 2005-08-04 01:19:13 +0000 | [diff] [blame] | 135 | |
| 136 | /// getCastedVersionOf - Return the specified value casted to uintptr_t. |
| 137 | /// |
| 138 | Value *getCastedVersionOf(Value *V); |
| 139 | private: |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 140 | void runOnLoop(Loop *L); |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame^] | 141 | bool AddUsersIfInteresting(Instruction *I, Loop *L, |
| 142 | std::set<Instruction*> &Processed); |
| 143 | SCEVHandle GetExpressionSCEV(Instruction *E, Loop *L); |
| 144 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 146 | void StrengthReduceStridedIVUsers(Value *Stride, IVUsersOfOneStride &Uses, |
| 147 | Loop *L, bool isOnlyStride); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 148 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 149 | void strengthReduceGEP(GetElementPtrInst *GEPI, Loop *L, |
Jeff Cohen | be37fa0 | 2005-03-05 22:40:34 +0000 | [diff] [blame] | 150 | GEPCache* GEPCache, |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 151 | Instruction *InsertBefore, |
| 152 | std::set<Instruction*> &DeadInsts); |
| 153 | void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts); |
| 154 | }; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 155 | RegisterOpt<LoopStrengthReduce> X("loop-reduce", |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 156 | "Strength Reduce GEP Uses of Ind. Vars"); |
| 157 | } |
| 158 | |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 159 | FunctionPass *llvm::createLoopStrengthReducePass(unsigned MaxTargetAMSize) { |
| 160 | return new LoopStrengthReduce(MaxTargetAMSize); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Chris Lattner | 6f286b7 | 2005-08-04 01:19:13 +0000 | [diff] [blame] | 163 | /// getCastedVersionOf - Return the specified value casted to uintptr_t. |
| 164 | /// |
| 165 | Value *LoopStrengthReduce::getCastedVersionOf(Value *V) { |
| 166 | if (V->getType() == UIntPtrTy) return V; |
| 167 | if (Constant *CB = dyn_cast<Constant>(V)) |
| 168 | return ConstantExpr::getCast(CB, UIntPtrTy); |
| 169 | |
| 170 | Value *&New = CastedPointers[V]; |
| 171 | if (New) return New; |
| 172 | |
| 173 | BasicBlock::iterator InsertPt; |
| 174 | if (Argument *Arg = dyn_cast<Argument>(V)) { |
| 175 | // Insert into the entry of the function, after any allocas. |
| 176 | InsertPt = Arg->getParent()->begin()->begin(); |
| 177 | while (isa<AllocaInst>(InsertPt)) ++InsertPt; |
| 178 | } else { |
| 179 | if (InvokeInst *II = dyn_cast<InvokeInst>(V)) { |
| 180 | InsertPt = II->getNormalDest()->begin(); |
| 181 | } else { |
| 182 | InsertPt = cast<Instruction>(V); |
| 183 | ++InsertPt; |
| 184 | } |
| 185 | |
| 186 | // Do not insert casts into the middle of PHI node blocks. |
| 187 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 188 | } |
| 189 | |
| 190 | return New = new CastInst(V, UIntPtrTy, V->getName(), InsertPt); |
| 191 | } |
| 192 | |
| 193 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 194 | /// DeleteTriviallyDeadInstructions - If any of the instructions is the |
| 195 | /// specified set are trivially dead, delete them and see if this makes any of |
| 196 | /// their operands subsequently dead. |
| 197 | void LoopStrengthReduce:: |
| 198 | DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) { |
| 199 | while (!Insts.empty()) { |
| 200 | Instruction *I = *Insts.begin(); |
| 201 | Insts.erase(Insts.begin()); |
| 202 | if (isInstructionTriviallyDead(I)) { |
Jeff Cohen | 8ea6f9e | 2005-03-01 03:46:11 +0000 | [diff] [blame] | 203 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 204 | if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i))) |
| 205 | Insts.insert(U); |
Chris Lattner | 84e9baa | 2005-08-03 21:36:09 +0000 | [diff] [blame] | 206 | SE->deleteInstructionFromRecords(I); |
| 207 | I->eraseFromParent(); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 208 | Changed = true; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 213 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 214 | /// CanReduceSCEV - Return true if we can strength reduce this scalar evolution |
| 215 | /// in the specified loop. |
| 216 | static bool CanReduceSCEV(const SCEVHandle &SH, Loop *L) { |
| 217 | SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(SH); |
| 218 | if (!AddRec || AddRec->getLoop() != L) return false; |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 219 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 220 | // FIXME: Generalize to non-affine IV's. |
| 221 | if (!AddRec->isAffine()) return false; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 222 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 223 | // FIXME: generalize to IV's with more complex strides (must emit stride |
| 224 | // expression outside of loop!) |
| 225 | if (isa<SCEVConstant>(AddRec->getOperand(1))) |
| 226 | return true; |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 227 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 228 | // We handle steps by unsigned values, because we know we won't have to insert |
| 229 | // a cast for them. |
| 230 | if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(AddRec->getOperand(1))) |
| 231 | if (SU->getValue()->getType()->isUnsigned()) |
| 232 | return true; |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 233 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 234 | // Otherwise, no, we can't handle it yet. |
| 235 | return false; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame^] | 238 | /// GetExpressionSCEV - Compute and return the SCEV for the specified |
| 239 | /// instruction. |
| 240 | SCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp, Loop *L) { |
| 241 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Exp); |
| 242 | if (!GEP) |
| 243 | return SE->getSCEV(Exp); |
| 244 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 245 | // Analyze all of the subscripts of this getelementptr instruction, looking |
| 246 | // for uses that are determined by the trip count of L. First, skip all |
| 247 | // operands the are not dependent on the IV. |
| 248 | |
| 249 | // Build up the base expression. Insert an LLVM cast of the pointer to |
| 250 | // uintptr_t first. |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame^] | 251 | SCEVHandle GEPVal = SCEVUnknown::get(getCastedVersionOf(GEP->getOperand(0))); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 252 | |
| 253 | gep_type_iterator GTI = gep_type_begin(GEP); |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame^] | 254 | |
| 255 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 256 | // If this is a use of a recurrence that we can analyze, and it comes before |
| 257 | // Op does in the GEP operand list, we will handle this when we process this |
| 258 | // operand. |
| 259 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 260 | const StructLayout *SL = TD->getStructLayout(STy); |
| 261 | unsigned Idx = cast<ConstantUInt>(GEP->getOperand(i))->getValue(); |
| 262 | uint64_t Offset = SL->MemberOffsets[Idx]; |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame^] | 263 | GEPVal = SCEVAddExpr::get(GEPVal, |
| 264 | SCEVUnknown::getIntegerSCEV(Offset, UIntPtrTy)); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 265 | } else { |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame^] | 266 | SCEVHandle Idx = SE->getSCEV(getCastedVersionOf(GEP->getOperand(i))); |
| 267 | uint64_t TypeSize = TD->getTypeSize(GTI.getIndexedType()); |
| 268 | if (TypeSize != 1) |
| 269 | Idx = SCEVMulExpr::get(Idx, |
| 270 | SCEVConstant::get(ConstantUInt::get(UIntPtrTy, |
| 271 | TypeSize))); |
| 272 | GEPVal = SCEVAddExpr::get(GEPVal, Idx); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame^] | 276 | //assert(CanReduceSCEV(GEPVal, L) && "Cannot reduce this use of IV?"); |
| 277 | return GEPVal; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /// AddUsersIfInteresting - Inspect the specified instruction. If it is a |
| 281 | /// reducible SCEV, recursively add its users to the IVUsesByStride set and |
| 282 | /// return true. Otherwise, return false. |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame^] | 283 | bool LoopStrengthReduce::AddUsersIfInteresting(Instruction *I, Loop *L, |
| 284 | std::set<Instruction*> &Processed) { |
Nate Begeman | 17a0e2af | 2005-07-30 00:21:31 +0000 | [diff] [blame] | 285 | if (I->getType() == Type::VoidTy) return false; |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame^] | 286 | if (!Processed.insert(I).second) |
| 287 | return true; // Instruction already handled. |
| 288 | |
| 289 | SCEVHandle ISE = GetExpressionSCEV(I, L); |
| 290 | if (!CanReduceSCEV(ISE, L)) |
| 291 | return false; // Non-analyzable expression, e.g. a rem instr. |
| 292 | |
| 293 | // NOT SAFE with generalized EXPRS |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 294 | SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(ISE); |
| 295 | SCEVHandle Start = AR->getStart(); |
| 296 | |
| 297 | // Get the step value, canonicalizing to an unsigned integer type so that |
| 298 | // lookups in the map will match. |
| 299 | Value *Step = 0; // Step of ISE. |
| 300 | if (SCEVConstant *SC = dyn_cast<SCEVConstant>(AR->getOperand(1))) |
| 301 | /// Always get the step value as an unsigned value. |
| 302 | Step = ConstantExpr::getCast(SC->getValue(), |
| 303 | SC->getValue()->getType()->getUnsignedVersion()); |
| 304 | else |
| 305 | Step = cast<SCEVUnknown>(AR->getOperand(1))->getValue(); |
| 306 | assert(Step->getType()->isUnsigned() && "Bad step value!"); |
| 307 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 308 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;++UI){ |
| 309 | Instruction *User = cast<Instruction>(*UI); |
| 310 | |
| 311 | // Do not infinitely recurse on PHI nodes. |
| 312 | if (isa<PHINode>(User) && User->getParent() == L->getHeader()) |
| 313 | continue; |
| 314 | |
| 315 | // If this is an instruction defined in a nested loop, or outside this loop, |
Chris Lattner | a0102fb | 2005-08-04 00:14:11 +0000 | [diff] [blame] | 316 | // don't recurse into it. |
| 317 | if (LI->getLoopFor(User->getParent()) != L) { |
| 318 | DEBUG(std::cerr << "FOUND USER in nested loop: " << *User |
| 319 | << " OF SCEV: " << *ISE << "\n"); |
| 320 | |
| 321 | // Okay, we found a user that we cannot reduce. Analyze the instruction |
| 322 | // and decide what to do with it. |
| 323 | IVUsesByStride[Step].addUser(Start, User, I); |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame^] | 324 | } else if (!AddUsersIfInteresting(User, L, Processed)) { |
Chris Lattner | 6510749 | 2005-08-04 00:40:47 +0000 | [diff] [blame] | 325 | DEBUG(std::cerr << "FOUND USER: " << *User |
| 326 | << " OF SCEV: " << *ISE << "\n"); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 6510749 | 2005-08-04 00:40:47 +0000 | [diff] [blame] | 328 | // Okay, we found a user that we cannot reduce. Analyze the instruction |
| 329 | // and decide what to do with it. |
| 330 | IVUsesByStride[Step].addUser(Start, User, I); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | namespace { |
| 337 | /// BasedUser - For a particular base value, keep information about how we've |
| 338 | /// partitioned the expression so far. |
| 339 | struct BasedUser { |
| 340 | /// Inst - The instruction using the induction variable. |
| 341 | Instruction *Inst; |
| 342 | |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 343 | /// OperandValToReplace - The operand value of Inst to replace with the |
| 344 | /// EmittedBase. |
| 345 | Value *OperandValToReplace; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 346 | |
| 347 | /// Imm - The immediate value that should be added to the base immediately |
| 348 | /// before Inst, because it will be folded into the imm field of the |
| 349 | /// instruction. |
| 350 | SCEVHandle Imm; |
| 351 | |
| 352 | /// EmittedBase - The actual value* to use for the base value of this |
| 353 | /// operation. This is null if we should just use zero so far. |
| 354 | Value *EmittedBase; |
| 355 | |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 356 | BasedUser(Instruction *I, Value *Op, const SCEVHandle &IMM) |
| 357 | : Inst(I), OperandValToReplace(Op), Imm(IMM), EmittedBase(0) {} |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 358 | |
| 359 | |
| 360 | // No need to compare these. |
| 361 | bool operator<(const BasedUser &BU) const { return 0; } |
| 362 | |
| 363 | void dump() const; |
| 364 | }; |
| 365 | } |
| 366 | |
| 367 | void BasedUser::dump() const { |
| 368 | std::cerr << " Imm=" << *Imm; |
| 369 | if (EmittedBase) |
| 370 | std::cerr << " EB=" << *EmittedBase; |
| 371 | |
| 372 | std::cerr << " Inst: " << *Inst; |
| 373 | } |
| 374 | |
| 375 | /// isTargetConstant - Return true if the following can be referenced by the |
| 376 | /// immediate field of a target instruction. |
| 377 | static bool isTargetConstant(const SCEVHandle &V) { |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 378 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 379 | // FIXME: Look at the target to decide if &GV is a legal constant immediate. |
| 380 | if (isa<SCEVConstant>(V)) return true; |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 381 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 382 | return false; // ENABLE this for x86 |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 383 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 384 | if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) |
| 385 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(SU->getValue())) |
| 386 | if (CE->getOpcode() == Instruction::Cast) |
| 387 | if (isa<GlobalValue>(CE->getOperand(0))) |
| 388 | // FIXME: should check to see that the dest is uintptr_t! |
| 389 | return true; |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | /// GetImmediateValues - Look at Val, and pull out any additions of constants |
| 394 | /// that can fit into the immediate field of instructions in the target. |
| 395 | static SCEVHandle GetImmediateValues(SCEVHandle Val, bool isAddress) { |
| 396 | if (!isAddress) |
| 397 | return SCEVUnknown::getIntegerSCEV(0, Val->getType()); |
| 398 | if (isTargetConstant(Val)) |
| 399 | return Val; |
| 400 | |
Chris Lattner | fc62470 | 2005-08-03 23:44:42 +0000 | [diff] [blame] | 401 | if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) { |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 402 | unsigned i = 0; |
| 403 | for (; i != SAE->getNumOperands(); ++i) |
| 404 | if (isTargetConstant(SAE->getOperand(i))) { |
| 405 | SCEVHandle ImmVal = SAE->getOperand(i); |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 406 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 407 | // If there are any other immediates that we can handle here, pull them |
| 408 | // out too. |
| 409 | for (++i; i != SAE->getNumOperands(); ++i) |
| 410 | if (isTargetConstant(SAE->getOperand(i))) |
| 411 | ImmVal = SCEVAddExpr::get(ImmVal, SAE->getOperand(i)); |
| 412 | return ImmVal; |
| 413 | } |
Chris Lattner | fc62470 | 2005-08-03 23:44:42 +0000 | [diff] [blame] | 414 | } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) { |
| 415 | // Try to pull immediates out of the start value of nested addrec's. |
| 416 | return GetImmediateValues(SARE->getStart(), isAddress); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | return SCEVUnknown::getIntegerSCEV(0, Val->getType()); |
| 420 | } |
| 421 | |
| 422 | /// StrengthReduceStridedIVUsers - Strength reduce all of the users of a single |
| 423 | /// stride of IV. All of the users may have different starting values, and this |
| 424 | /// may not be the only stride (we know it is if isOnlyStride is true). |
| 425 | void LoopStrengthReduce::StrengthReduceStridedIVUsers(Value *Stride, |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 426 | IVUsersOfOneStride &Uses, |
| 427 | Loop *L, |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 428 | bool isOnlyStride) { |
| 429 | // Transform our list of users and offsets to a bit more complex table. In |
| 430 | // this new vector, the first entry for each element is the base of the |
| 431 | // strided access, and the second is the BasedUser object for the use. We |
| 432 | // progressively move information from the first to the second entry, until we |
| 433 | // eventually emit the object. |
| 434 | std::vector<std::pair<SCEVHandle, BasedUser> > UsersToProcess; |
| 435 | UsersToProcess.reserve(Uses.Users.size()); |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 436 | |
| 437 | SCEVHandle ZeroBase = SCEVUnknown::getIntegerSCEV(0, |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 438 | Uses.Users[0].Offset->getType()); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 439 | |
| 440 | for (unsigned i = 0, e = Uses.Users.size(); i != e; ++i) |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 441 | UsersToProcess.push_back(std::make_pair(Uses.Users[i].Offset, |
| 442 | BasedUser(Uses.Users[i].User, |
| 443 | Uses.Users[i].OperandValToReplace, |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 444 | ZeroBase))); |
| 445 | |
| 446 | // First pass, figure out what we can represent in the immediate fields of |
| 447 | // instructions. If we can represent anything there, move it to the imm |
| 448 | // fields of the BasedUsers. |
| 449 | for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) { |
| 450 | bool isAddress = isa<LoadInst>(UsersToProcess[i].second.Inst) || |
| 451 | isa<StoreInst>(UsersToProcess[i].second.Inst); |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 452 | UsersToProcess[i].second.Imm = GetImmediateValues(UsersToProcess[i].first, |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 453 | isAddress); |
| 454 | UsersToProcess[i].first = SCEV::getMinusSCEV(UsersToProcess[i].first, |
| 455 | UsersToProcess[i].second.Imm); |
| 456 | |
| 457 | DEBUG(std::cerr << "BASE: " << *UsersToProcess[i].first); |
| 458 | DEBUG(UsersToProcess[i].second.dump()); |
| 459 | } |
| 460 | |
| 461 | SCEVExpander Rewriter(*SE, *LI); |
| 462 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 463 | Instruction *PreInsertPt = Preheader->getTerminator(); |
| 464 | Instruction *PhiInsertBefore = L->getHeader()->begin(); |
| 465 | |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 466 | assert(isa<PHINode>(PhiInsertBefore) && |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 467 | "How could this loop have IV's without any phis?"); |
| 468 | PHINode *SomeLoopPHI = cast<PHINode>(PhiInsertBefore); |
| 469 | assert(SomeLoopPHI->getNumIncomingValues() == 2 && |
| 470 | "This loop isn't canonicalized right"); |
| 471 | BasicBlock *LatchBlock = |
| 472 | SomeLoopPHI->getIncomingBlock(SomeLoopPHI->getIncomingBlock(0) == Preheader); |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 473 | |
Chris Lattner | bb78c97 | 2005-08-03 23:30:08 +0000 | [diff] [blame] | 474 | DEBUG(std::cerr << "INSERTING IVs of STRIDE " << *Stride << ":\n"); |
| 475 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 476 | // FIXME: This loop needs increasing levels of intelligence. |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 477 | // STAGE 0: just emit everything as its own base. |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 478 | // STAGE 1: factor out common vars from bases, and try and push resulting |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 479 | // constants into Imm field. <-- We are here |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 480 | // STAGE 2: factor out large constants to try and make more constants |
| 481 | // acceptable for target loads and stores. |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 482 | |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 483 | // Sort by the base value, so that all IVs with identical bases are next to |
| 484 | // each other. |
| 485 | std::sort(UsersToProcess.begin(), UsersToProcess.end()); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 486 | while (!UsersToProcess.empty()) { |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 487 | SCEVHandle Base = UsersToProcess.front().first; |
Chris Lattner | bb78c97 | 2005-08-03 23:30:08 +0000 | [diff] [blame] | 488 | |
| 489 | DEBUG(std::cerr << " INSERTING PHI with BASE = " << *Base << ":\n"); |
| 490 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 491 | // Create a new Phi for this base, and stick it in the loop header. |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 492 | const Type *ReplacedTy = Base->getType(); |
| 493 | PHINode *NewPHI = new PHINode(ReplacedTy, "iv.", PhiInsertBefore); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 494 | |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 495 | // Emit the initial base value into the loop preheader, and add it to the |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 496 | // Phi node. |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 497 | Value *BaseV = Rewriter.expandCodeFor(Base, PreInsertPt, ReplacedTy); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 498 | NewPHI->addIncoming(BaseV, Preheader); |
| 499 | |
| 500 | // Emit the increment of the base value before the terminator of the loop |
| 501 | // latch block, and add it to the Phi node. |
| 502 | SCEVHandle Inc = SCEVAddExpr::get(SCEVUnknown::get(NewPHI), |
| 503 | SCEVUnknown::get(Stride)); |
| 504 | |
| 505 | Value *IncV = Rewriter.expandCodeFor(Inc, LatchBlock->getTerminator(), |
| 506 | ReplacedTy); |
| 507 | IncV->setName(NewPHI->getName()+".inc"); |
| 508 | NewPHI->addIncoming(IncV, LatchBlock); |
| 509 | |
| 510 | // Emit the code to add the immediate offset to the Phi value, just before |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 511 | // the instructions that we identified as using this stride and base. |
| 512 | while (!UsersToProcess.empty() && UsersToProcess.front().first == Base) { |
| 513 | BasedUser &User = UsersToProcess.front().second; |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 514 | |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 515 | // Clear the SCEVExpander's expression map so that we are guaranteed |
| 516 | // to have the code emitted where we expect it. |
| 517 | Rewriter.clear(); |
| 518 | SCEVHandle NewValSCEV = SCEVAddExpr::get(SCEVUnknown::get(NewPHI), |
| 519 | User.Imm); |
Chris Lattner | bb78c97 | 2005-08-03 23:30:08 +0000 | [diff] [blame] | 520 | Value *Replaced = User.OperandValToReplace; |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 521 | Value *newVal = Rewriter.expandCodeFor(NewValSCEV, User.Inst, |
| 522 | Replaced->getType()); |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 523 | |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 524 | // Replace the use of the operand Value with the new Phi we just created. |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 525 | User.Inst->replaceUsesOfWith(Replaced, newVal); |
Chris Lattner | bb78c97 | 2005-08-03 23:30:08 +0000 | [diff] [blame] | 526 | DEBUG(std::cerr << " CHANGED: IMM =" << *User.Imm << " Inst = " |
| 527 | << *User.Inst); |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 528 | |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 529 | // Mark old value we replaced as possibly dead, so that it is elminated |
| 530 | // if we just replaced the last use of that value. |
| 531 | DeadInsts.insert(cast<Instruction>(Replaced)); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 532 | |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 533 | UsersToProcess.erase(UsersToProcess.begin()); |
| 534 | ++NumReduced; |
| 535 | } |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 536 | // TODO: Next, find out which base index is the most common, pull it out. |
| 537 | } |
| 538 | |
| 539 | // IMPORTANT TODO: Figure out how to partition the IV's with this stride, but |
| 540 | // different starting values, into different PHIs. |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 541 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 542 | // BEFORE writing this, it's probably useful to handle GEP's. |
| 543 | |
| 544 | // NOTE: pull all constants together, for REG+IMM addressing, include &GV in |
| 545 | // 'IMM' if the target supports it. |
| 546 | } |
| 547 | |
| 548 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 549 | void LoopStrengthReduce::runOnLoop(Loop *L) { |
| 550 | // First step, transform all loops nesting inside of this loop. |
| 551 | for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 552 | runOnLoop(*I); |
| 553 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 554 | // Next, find all uses of induction variables in this loop, and catagorize |
| 555 | // them by stride. Start by finding all of the PHI nodes in the header for |
| 556 | // this loop. If they are induction variables, inspect their uses. |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame^] | 557 | std::set<Instruction*> Processed; // Don't reprocess instructions. |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 558 | for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame^] | 559 | AddUsersIfInteresting(I, L, Processed); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 560 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 561 | // If we have nothing to do, return. |
| 562 | //if (IVUsesByStride.empty()) return; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 563 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 564 | // FIXME: We can widen subreg IV's here for RISC targets. e.g. instead of |
| 565 | // doing computation in byte values, promote to 32-bit values if safe. |
| 566 | |
| 567 | // FIXME: Attempt to reuse values across multiple IV's. In particular, we |
| 568 | // could have something like "for(i) { foo(i*8); bar(i*16) }", which should be |
| 569 | // codegened as "for (j = 0;; j+=8) { foo(j); bar(j+j); }" on X86/PPC. Need |
| 570 | // to be careful that IV's are all the same type. Only works for intptr_t |
| 571 | // indvars. |
| 572 | |
| 573 | // If we only have one stride, we can more aggressively eliminate some things. |
| 574 | bool HasOneStride = IVUsesByStride.size() == 1; |
| 575 | |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 576 | for (std::map<Value*, IVUsersOfOneStride>::iterator SI |
| 577 | = IVUsesByStride.begin(), E = IVUsesByStride.end(); SI != E; ++SI) |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 578 | StrengthReduceStridedIVUsers(SI->first, SI->second, L, HasOneStride); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 579 | |
| 580 | // Clean up after ourselves |
| 581 | if (!DeadInsts.empty()) { |
| 582 | DeleteTriviallyDeadInstructions(DeadInsts); |
| 583 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 584 | BasicBlock::iterator I = L->getHeader()->begin(); |
| 585 | PHINode *PN; |
Chris Lattner | dcce49e | 2005-08-02 02:44:31 +0000 | [diff] [blame] | 586 | while ((PN = dyn_cast<PHINode>(I))) { |
Chris Lattner | 564900e | 2005-08-02 00:41:11 +0000 | [diff] [blame] | 587 | ++I; // Preincrement iterator to avoid invalidating it when deleting PN. |
| 588 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 589 | // At this point, we know that we have killed one or more GEP instructions. |
| 590 | // It is worth checking to see if the cann indvar is also dead, so that we |
| 591 | // can remove it as well. The requirements for the cann indvar to be |
| 592 | // considered dead are: |
| 593 | // 1. the cann indvar has one use |
| 594 | // 2. the use is an add instruction |
| 595 | // 3. the add has one use |
| 596 | // 4. the add is used by the cann indvar |
| 597 | // If all four cases above are true, then we can remove both the add and |
| 598 | // the cann indvar. |
| 599 | // FIXME: this needs to eliminate an induction variable even if it's being |
| 600 | // compared against some value to decide loop termination. |
| 601 | if (PN->hasOneUse()) { |
| 602 | BinaryOperator *BO = dyn_cast<BinaryOperator>(*(PN->use_begin())); |
Chris Lattner | 75a44e1 | 2005-08-02 02:52:02 +0000 | [diff] [blame] | 603 | if (BO && BO->hasOneUse()) { |
| 604 | if (PN == *(BO->use_begin())) { |
| 605 | DeadInsts.insert(BO); |
| 606 | // Break the cycle, then delete the PHI. |
| 607 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
Chris Lattner | 84e9baa | 2005-08-03 21:36:09 +0000 | [diff] [blame] | 608 | SE->deleteInstructionFromRecords(PN); |
Chris Lattner | 75a44e1 | 2005-08-02 02:52:02 +0000 | [diff] [blame] | 609 | PN->eraseFromParent(); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 610 | } |
Chris Lattner | 75a44e1 | 2005-08-02 02:52:02 +0000 | [diff] [blame] | 611 | } |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 612 | } |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 613 | } |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 614 | DeleteTriviallyDeadInstructions(DeadInsts); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 615 | } |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 616 | |
| 617 | IVUsesByStride.clear(); |
| 618 | return; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 619 | } |