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" |
Chris Lattner | 4fec86d | 2005-08-12 22:06:11 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Utils/Local.h" |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetData.h" |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
Jeff Cohen | c500991 | 2005-07-30 18:22:27 +0000 | [diff] [blame] | 34 | #include <algorithm> |
Chris Lattner | c597b8a | 2006-01-22 23:32:06 +0000 | [diff] [blame] | 35 | #include <iostream> |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 36 | #include <set> |
| 37 | using namespace llvm; |
| 38 | |
| 39 | namespace { |
| 40 | Statistic<> NumReduced ("loop-reduce", "Number of GEPs strength reduced"); |
Chris Lattner | 45f8b6e | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 41 | Statistic<> NumInserted("loop-reduce", "Number of PHIs inserted"); |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 42 | Statistic<> NumVariable("loop-reduce","Number of PHIs with variable strides"); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 44 | /// IVStrideUse - Keep track of one use of a strided induction variable, where |
| 45 | /// the stride is stored externally. The Offset member keeps track of the |
| 46 | /// offset from the IV, User is the actual user of the operand, and 'Operand' |
| 47 | /// is the operand # of the User that is the use. |
| 48 | struct IVStrideUse { |
| 49 | SCEVHandle Offset; |
| 50 | Instruction *User; |
| 51 | Value *OperandValToReplace; |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 52 | |
| 53 | // isUseOfPostIncrementedValue - True if this should use the |
| 54 | // post-incremented version of this IV, not the preincremented version. |
| 55 | // This can only be set in special cases, such as the terminating setcc |
Chris Lattner | a676483 | 2005-09-12 06:04:47 +0000 | [diff] [blame] | 56 | // instruction for a loop or uses dominated by the loop. |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 57 | bool isUseOfPostIncrementedValue; |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 58 | |
| 59 | IVStrideUse(const SCEVHandle &Offs, Instruction *U, Value *O) |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 60 | : Offset(Offs), User(U), OperandValToReplace(O), |
| 61 | isUseOfPostIncrementedValue(false) {} |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | /// IVUsersOfOneStride - This structure keeps track of all instructions that |
| 65 | /// have an operand that is based on the trip count multiplied by some stride. |
| 66 | /// The stride for all of these users is common and kept external to this |
| 67 | /// structure. |
| 68 | struct IVUsersOfOneStride { |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 69 | /// 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] | 70 | /// initial value and the operand that uses the IV. |
| 71 | std::vector<IVStrideUse> Users; |
| 72 | |
| 73 | void addUser(const SCEVHandle &Offset,Instruction *User, Value *Operand) { |
| 74 | Users.push_back(IVStrideUse(Offset, User, Operand)); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 75 | } |
| 76 | }; |
| 77 | |
| 78 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 79 | class LoopStrengthReduce : public FunctionPass { |
| 80 | LoopInfo *LI; |
Chris Lattner | cb36710 | 2006-01-11 05:10:20 +0000 | [diff] [blame] | 81 | ETForest *EF; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 82 | ScalarEvolution *SE; |
| 83 | const TargetData *TD; |
| 84 | const Type *UIntPtrTy; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 85 | bool Changed; |
Chris Lattner | 75a44e1 | 2005-08-02 02:52:02 +0000 | [diff] [blame] | 86 | |
| 87 | /// MaxTargetAMSize - This is the maximum power-of-two scale value that the |
| 88 | /// target can handle for free with its addressing modes. |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 89 | unsigned MaxTargetAMSize; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 90 | |
| 91 | /// IVUsesByStride - Keep track of all uses of induction variables that we |
| 92 | /// are interested in. The key of the map is the stride of the access. |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 93 | std::map<SCEVHandle, IVUsersOfOneStride> IVUsesByStride; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 4ea0a3e | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 95 | /// StrideOrder - An ordering of the keys in IVUsesByStride that is stable: |
| 96 | /// We use this to iterate over the IVUsesByStride collection without being |
| 97 | /// dependent on random ordering of pointers in the process. |
| 98 | std::vector<SCEVHandle> StrideOrder; |
| 99 | |
Chris Lattner | 6f286b7 | 2005-08-04 01:19:13 +0000 | [diff] [blame] | 100 | /// CastedValues - As we need to cast values to uintptr_t, this keeps track |
| 101 | /// of the casted version of each value. This is accessed by |
| 102 | /// getCastedVersionOf. |
| 103 | std::map<Value*, Value*> CastedPointers; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 104 | |
| 105 | /// DeadInsts - Keep track of instructions we may have made dead, so that |
| 106 | /// we can remove them after we are done working. |
| 107 | std::set<Instruction*> DeadInsts; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 108 | public: |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 109 | LoopStrengthReduce(unsigned MTAMS = 1) |
| 110 | : MaxTargetAMSize(MTAMS) { |
| 111 | } |
| 112 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 113 | virtual bool runOnFunction(Function &) { |
| 114 | LI = &getAnalysis<LoopInfo>(); |
Chris Lattner | cb36710 | 2006-01-11 05:10:20 +0000 | [diff] [blame] | 115 | EF = &getAnalysis<ETForest>(); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 116 | SE = &getAnalysis<ScalarEvolution>(); |
| 117 | TD = &getAnalysis<TargetData>(); |
| 118 | UIntPtrTy = TD->getIntPtrType(); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 119 | Changed = false; |
| 120 | |
| 121 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 122 | runOnLoop(*I); |
Chris Lattner | 6f286b7 | 2005-08-04 01:19:13 +0000 | [diff] [blame] | 123 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 124 | return Changed; |
| 125 | } |
| 126 | |
| 127 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 2bf7cb5 | 2005-08-17 06:35:16 +0000 | [diff] [blame] | 128 | // We split critical edges, so we change the CFG. However, we do update |
| 129 | // many analyses if they are around. |
| 130 | AU.addPreservedID(LoopSimplifyID); |
| 131 | AU.addPreserved<LoopInfo>(); |
| 132 | AU.addPreserved<DominatorSet>(); |
Chris Lattner | cb36710 | 2006-01-11 05:10:20 +0000 | [diff] [blame] | 133 | AU.addPreserved<ETForest>(); |
Chris Lattner | 2bf7cb5 | 2005-08-17 06:35:16 +0000 | [diff] [blame] | 134 | AU.addPreserved<ImmediateDominators>(); |
| 135 | AU.addPreserved<DominanceFrontier>(); |
| 136 | AU.addPreserved<DominatorTree>(); |
| 137 | |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 138 | AU.addRequiredID(LoopSimplifyID); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 139 | AU.addRequired<LoopInfo>(); |
Chris Lattner | cb36710 | 2006-01-11 05:10:20 +0000 | [diff] [blame] | 140 | AU.addRequired<ETForest>(); |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 141 | AU.addRequired<TargetData>(); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 142 | AU.addRequired<ScalarEvolution>(); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 143 | } |
Chris Lattner | 6f286b7 | 2005-08-04 01:19:13 +0000 | [diff] [blame] | 144 | |
| 145 | /// getCastedVersionOf - Return the specified value casted to uintptr_t. |
| 146 | /// |
| 147 | Value *getCastedVersionOf(Value *V); |
| 148 | private: |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 149 | void runOnLoop(Loop *L); |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 150 | bool AddUsersIfInteresting(Instruction *I, Loop *L, |
| 151 | std::set<Instruction*> &Processed); |
| 152 | SCEVHandle GetExpressionSCEV(Instruction *E, Loop *L); |
| 153 | |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 154 | void OptimizeIndvars(Loop *L); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 155 | |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 156 | void StrengthReduceStridedIVUsers(const SCEVHandle &Stride, |
| 157 | IVUsersOfOneStride &Uses, |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 158 | Loop *L, bool isOnlyStride); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 159 | void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts); |
| 160 | }; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 161 | RegisterOpt<LoopStrengthReduce> X("loop-reduce", |
Chris Lattner | 92233d2 | 2005-09-27 21:10:32 +0000 | [diff] [blame] | 162 | "Loop Strength Reduction"); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Jeff Cohen | a2c59b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 165 | FunctionPass *llvm::createLoopStrengthReducePass(unsigned MaxTargetAMSize) { |
| 166 | return new LoopStrengthReduce(MaxTargetAMSize); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Chris Lattner | 6f286b7 | 2005-08-04 01:19:13 +0000 | [diff] [blame] | 169 | /// getCastedVersionOf - Return the specified value casted to uintptr_t. |
| 170 | /// |
| 171 | Value *LoopStrengthReduce::getCastedVersionOf(Value *V) { |
| 172 | if (V->getType() == UIntPtrTy) return V; |
| 173 | if (Constant *CB = dyn_cast<Constant>(V)) |
| 174 | return ConstantExpr::getCast(CB, UIntPtrTy); |
| 175 | |
| 176 | Value *&New = CastedPointers[V]; |
| 177 | if (New) return New; |
| 178 | |
Chris Lattner | d30c499 | 2006-02-04 09:52:43 +0000 | [diff] [blame^] | 179 | New = SCEVExpander::InsertCastOfTo(V, UIntPtrTy); |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 180 | DeadInsts.insert(cast<Instruction>(New)); |
| 181 | return New; |
Chris Lattner | 6f286b7 | 2005-08-04 01:19:13 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 185 | /// DeleteTriviallyDeadInstructions - If any of the instructions is the |
| 186 | /// specified set are trivially dead, delete them and see if this makes any of |
| 187 | /// their operands subsequently dead. |
| 188 | void LoopStrengthReduce:: |
| 189 | DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) { |
| 190 | while (!Insts.empty()) { |
| 191 | Instruction *I = *Insts.begin(); |
| 192 | Insts.erase(Insts.begin()); |
| 193 | if (isInstructionTriviallyDead(I)) { |
Jeff Cohen | 8ea6f9e | 2005-03-01 03:46:11 +0000 | [diff] [blame] | 194 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 195 | if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i))) |
| 196 | Insts.insert(U); |
Chris Lattner | 84e9baa | 2005-08-03 21:36:09 +0000 | [diff] [blame] | 197 | SE->deleteInstructionFromRecords(I); |
| 198 | I->eraseFromParent(); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 199 | Changed = true; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
Jeff Cohen | 39751c3 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 204 | |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 205 | /// GetExpressionSCEV - Compute and return the SCEV for the specified |
| 206 | /// instruction. |
| 207 | SCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp, Loop *L) { |
Chris Lattner | c6c4d99 | 2005-08-09 23:39:36 +0000 | [diff] [blame] | 208 | // Scalar Evolutions doesn't know how to compute SCEV's for GEP instructions. |
| 209 | // If this is a GEP that SE doesn't know about, compute it now and insert it. |
| 210 | // If this is not a GEP, or if we have already done this computation, just let |
| 211 | // SE figure it out. |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 212 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Exp); |
Chris Lattner | c6c4d99 | 2005-08-09 23:39:36 +0000 | [diff] [blame] | 213 | if (!GEP || SE->hasSCEV(GEP)) |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 214 | return SE->getSCEV(Exp); |
| 215 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 216 | // Analyze all of the subscripts of this getelementptr instruction, looking |
| 217 | // for uses that are determined by the trip count of L. First, skip all |
| 218 | // operands the are not dependent on the IV. |
| 219 | |
| 220 | // Build up the base expression. Insert an LLVM cast of the pointer to |
| 221 | // uintptr_t first. |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 222 | SCEVHandle GEPVal = SCEVUnknown::get(getCastedVersionOf(GEP->getOperand(0))); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 223 | |
| 224 | gep_type_iterator GTI = gep_type_begin(GEP); |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 225 | |
| 226 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 227 | // If this is a use of a recurrence that we can analyze, and it comes before |
| 228 | // Op does in the GEP operand list, we will handle this when we process this |
| 229 | // operand. |
| 230 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 231 | const StructLayout *SL = TD->getStructLayout(STy); |
| 232 | unsigned Idx = cast<ConstantUInt>(GEP->getOperand(i))->getValue(); |
| 233 | uint64_t Offset = SL->MemberOffsets[Idx]; |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 234 | GEPVal = SCEVAddExpr::get(GEPVal, |
| 235 | SCEVUnknown::getIntegerSCEV(Offset, UIntPtrTy)); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 236 | } else { |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 237 | Value *OpVal = getCastedVersionOf(GEP->getOperand(i)); |
| 238 | SCEVHandle Idx = SE->getSCEV(OpVal); |
| 239 | |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 240 | uint64_t TypeSize = TD->getTypeSize(GTI.getIndexedType()); |
| 241 | if (TypeSize != 1) |
| 242 | Idx = SCEVMulExpr::get(Idx, |
| 243 | SCEVConstant::get(ConstantUInt::get(UIntPtrTy, |
| 244 | TypeSize))); |
| 245 | GEPVal = SCEVAddExpr::get(GEPVal, Idx); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Chris Lattner | c6c4d99 | 2005-08-09 23:39:36 +0000 | [diff] [blame] | 249 | SE->setSCEV(GEP, GEPVal); |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 250 | return GEPVal; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 253 | /// getSCEVStartAndStride - Compute the start and stride of this expression, |
| 254 | /// returning false if the expression is not a start/stride pair, or true if it |
| 255 | /// is. The stride must be a loop invariant expression, but the start may be |
| 256 | /// a mix of loop invariant and loop variant expressions. |
| 257 | static bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L, |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 258 | SCEVHandle &Start, SCEVHandle &Stride) { |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 259 | SCEVHandle TheAddRec = Start; // Initialize to zero. |
| 260 | |
| 261 | // If the outer level is an AddExpr, the operands are all start values except |
| 262 | // for a nested AddRecExpr. |
| 263 | if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) { |
| 264 | for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i) |
| 265 | if (SCEVAddRecExpr *AddRec = |
| 266 | dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) { |
| 267 | if (AddRec->getLoop() == L) |
| 268 | TheAddRec = SCEVAddExpr::get(AddRec, TheAddRec); |
| 269 | else |
| 270 | return false; // Nested IV of some sort? |
| 271 | } else { |
| 272 | Start = SCEVAddExpr::get(Start, AE->getOperand(i)); |
| 273 | } |
| 274 | |
| 275 | } else if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(SH)) { |
| 276 | TheAddRec = SH; |
| 277 | } else { |
| 278 | return false; // not analyzable. |
| 279 | } |
| 280 | |
| 281 | SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec); |
| 282 | if (!AddRec || AddRec->getLoop() != L) return false; |
| 283 | |
| 284 | // FIXME: Generalize to non-affine IV's. |
| 285 | if (!AddRec->isAffine()) return false; |
| 286 | |
| 287 | Start = SCEVAddExpr::get(Start, AddRec->getOperand(0)); |
| 288 | |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 289 | if (!isa<SCEVConstant>(AddRec->getOperand(1))) |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 290 | DEBUG(std::cerr << "[" << L->getHeader()->getName() |
| 291 | << "] Variable stride: " << *AddRec << "\n"); |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 292 | |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 293 | Stride = AddRec->getOperand(1); |
| 294 | // Check that all constant strides are the unsigned type, we don't want to |
| 295 | // have two IV's one of signed stride 4 and one of unsigned stride 4 to not be |
| 296 | // merged. |
| 297 | assert((!isa<SCEVConstant>(Stride) || Stride->getType()->isUnsigned()) && |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 298 | "Constants should be canonicalized to unsigned!"); |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 299 | |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 300 | return true; |
| 301 | } |
| 302 | |
Chris Lattner | e4ed42a | 2005-10-03 01:04:44 +0000 | [diff] [blame] | 303 | /// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression |
| 304 | /// and now we need to decide whether the user should use the preinc or post-inc |
| 305 | /// value. If this user should use the post-inc version of the IV, return true. |
| 306 | /// |
| 307 | /// Choosing wrong here can break dominance properties (if we choose to use the |
| 308 | /// post-inc value when we cannot) or it can end up adding extra live-ranges to |
| 309 | /// the loop, resulting in reg-reg copies (if we use the pre-inc value when we |
| 310 | /// should use the post-inc value). |
| 311 | static bool IVUseShouldUsePostIncValue(Instruction *User, Instruction *IV, |
Chris Lattner | cb36710 | 2006-01-11 05:10:20 +0000 | [diff] [blame] | 312 | Loop *L, ETForest *EF, Pass *P) { |
Chris Lattner | e4ed42a | 2005-10-03 01:04:44 +0000 | [diff] [blame] | 313 | // If the user is in the loop, use the preinc value. |
| 314 | if (L->contains(User->getParent())) return false; |
| 315 | |
Chris Lattner | f07a587 | 2005-10-03 02:50:05 +0000 | [diff] [blame] | 316 | BasicBlock *LatchBlock = L->getLoopLatch(); |
| 317 | |
| 318 | // Ok, the user is outside of the loop. If it is dominated by the latch |
| 319 | // block, use the post-inc value. |
Chris Lattner | cb36710 | 2006-01-11 05:10:20 +0000 | [diff] [blame] | 320 | if (EF->dominates(LatchBlock, User->getParent())) |
Chris Lattner | f07a587 | 2005-10-03 02:50:05 +0000 | [diff] [blame] | 321 | return true; |
| 322 | |
| 323 | // There is one case we have to be careful of: PHI nodes. These little guys |
| 324 | // can live in blocks that do not dominate the latch block, but (since their |
| 325 | // uses occur in the predecessor block, not the block the PHI lives in) should |
| 326 | // still use the post-inc value. Check for this case now. |
| 327 | PHINode *PN = dyn_cast<PHINode>(User); |
| 328 | if (!PN) return false; // not a phi, not dominated by latch block. |
| 329 | |
| 330 | // Look at all of the uses of IV by the PHI node. If any use corresponds to |
| 331 | // a block that is not dominated by the latch block, give up and use the |
| 332 | // preincremented value. |
| 333 | unsigned NumUses = 0; |
| 334 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 335 | if (PN->getIncomingValue(i) == IV) { |
| 336 | ++NumUses; |
Chris Lattner | cb36710 | 2006-01-11 05:10:20 +0000 | [diff] [blame] | 337 | if (!EF->dominates(LatchBlock, PN->getIncomingBlock(i))) |
Chris Lattner | f07a587 | 2005-10-03 02:50:05 +0000 | [diff] [blame] | 338 | return false; |
| 339 | } |
| 340 | |
| 341 | // Okay, all uses of IV by PN are in predecessor blocks that really are |
| 342 | // dominated by the latch block. Split the critical edges and use the |
| 343 | // post-incremented value. |
| 344 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 345 | if (PN->getIncomingValue(i) == IV) { |
| 346 | SplitCriticalEdge(PN->getIncomingBlock(i), PN->getParent(), P); |
| 347 | if (--NumUses == 0) break; |
| 348 | } |
| 349 | |
| 350 | return true; |
Chris Lattner | e4ed42a | 2005-10-03 01:04:44 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | |
| 354 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 355 | /// AddUsersIfInteresting - Inspect the specified instruction. If it is a |
| 356 | /// reducible SCEV, recursively add its users to the IVUsesByStride set and |
| 357 | /// return true. Otherwise, return false. |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 358 | bool LoopStrengthReduce::AddUsersIfInteresting(Instruction *I, Loop *L, |
| 359 | std::set<Instruction*> &Processed) { |
Chris Lattner | 5df0e36 | 2005-10-21 05:45:41 +0000 | [diff] [blame] | 360 | if (!I->getType()->isInteger() && !isa<PointerType>(I->getType())) |
| 361 | return false; // Void and FP expressions cannot be reduced. |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 362 | if (!Processed.insert(I).second) |
| 363 | return true; // Instruction already handled. |
| 364 | |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 365 | // Get the symbolic expression for this instruction. |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 366 | SCEVHandle ISE = GetExpressionSCEV(I, L); |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 367 | if (isa<SCEVCouldNotCompute>(ISE)) return false; |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 368 | |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 369 | // Get the start and stride for this expression. |
| 370 | SCEVHandle Start = SCEVUnknown::getIntegerSCEV(0, ISE->getType()); |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 371 | SCEVHandle Stride = Start; |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 372 | if (!getSCEVStartAndStride(ISE, L, Start, Stride)) |
| 373 | return false; // Non-reducible symbolic expression, bail out. |
| 374 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 375 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;++UI){ |
| 376 | Instruction *User = cast<Instruction>(*UI); |
| 377 | |
| 378 | // Do not infinitely recurse on PHI nodes. |
Chris Lattner | fd018c8 | 2005-09-13 02:09:55 +0000 | [diff] [blame] | 379 | if (isa<PHINode>(User) && Processed.count(User)) |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 380 | continue; |
| 381 | |
| 382 | // 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] | 383 | // don't recurse into it. |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 384 | bool AddUserToIVUsers = false; |
Chris Lattner | a0102fb | 2005-08-04 00:14:11 +0000 | [diff] [blame] | 385 | if (LI->getLoopFor(User->getParent()) != L) { |
Chris Lattner | fd018c8 | 2005-09-13 02:09:55 +0000 | [diff] [blame] | 386 | DEBUG(std::cerr << "FOUND USER in other loop: " << *User |
Chris Lattner | a0102fb | 2005-08-04 00:14:11 +0000 | [diff] [blame] | 387 | << " OF SCEV: " << *ISE << "\n"); |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 388 | AddUserToIVUsers = true; |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 389 | } else if (!AddUsersIfInteresting(User, L, Processed)) { |
Chris Lattner | 6510749 | 2005-08-04 00:40:47 +0000 | [diff] [blame] | 390 | DEBUG(std::cerr << "FOUND USER: " << *User |
| 391 | << " OF SCEV: " << *ISE << "\n"); |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 392 | AddUserToIVUsers = true; |
| 393 | } |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 394 | |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 395 | if (AddUserToIVUsers) { |
Chris Lattner | 4ea0a3e | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 396 | IVUsersOfOneStride &StrideUses = IVUsesByStride[Stride]; |
| 397 | if (StrideUses.Users.empty()) // First occurance of this stride? |
| 398 | StrideOrder.push_back(Stride); |
| 399 | |
Chris Lattner | 6510749 | 2005-08-04 00:40:47 +0000 | [diff] [blame] | 400 | // Okay, we found a user that we cannot reduce. Analyze the instruction |
Chris Lattner | a676483 | 2005-09-12 06:04:47 +0000 | [diff] [blame] | 401 | // and decide what to do with it. If we are a use inside of the loop, use |
| 402 | // the value before incrementation, otherwise use it after incrementation. |
Chris Lattner | cb36710 | 2006-01-11 05:10:20 +0000 | [diff] [blame] | 403 | if (IVUseShouldUsePostIncValue(User, I, L, EF, this)) { |
Chris Lattner | a676483 | 2005-09-12 06:04:47 +0000 | [diff] [blame] | 404 | // The value used will be incremented by the stride more than we are |
| 405 | // expecting, so subtract this off. |
| 406 | SCEVHandle NewStart = SCEV::getMinusSCEV(Start, Stride); |
Chris Lattner | 4ea0a3e | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 407 | StrideUses.addUser(NewStart, User, I); |
| 408 | StrideUses.Users.back().isUseOfPostIncrementedValue = true; |
Chris Lattner | f07a587 | 2005-10-03 02:50:05 +0000 | [diff] [blame] | 409 | DEBUG(std::cerr << " USING POSTINC SCEV, START=" << *NewStart<< "\n"); |
Chris Lattner | e4ed42a | 2005-10-03 01:04:44 +0000 | [diff] [blame] | 410 | } else { |
Chris Lattner | 4ea0a3e | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 411 | StrideUses.addUser(Start, User, I); |
Chris Lattner | a676483 | 2005-09-12 06:04:47 +0000 | [diff] [blame] | 412 | } |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | namespace { |
| 419 | /// BasedUser - For a particular base value, keep information about how we've |
| 420 | /// partitioned the expression so far. |
| 421 | struct BasedUser { |
Chris Lattner | 37c24cc | 2005-08-08 22:56:21 +0000 | [diff] [blame] | 422 | /// Base - The Base value for the PHI node that needs to be inserted for |
| 423 | /// this use. As the use is processed, information gets moved from this |
| 424 | /// field to the Imm field (below). BasedUser values are sorted by this |
| 425 | /// field. |
| 426 | SCEVHandle Base; |
| 427 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 428 | /// Inst - The instruction using the induction variable. |
| 429 | Instruction *Inst; |
| 430 | |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 431 | /// OperandValToReplace - The operand value of Inst to replace with the |
| 432 | /// EmittedBase. |
| 433 | Value *OperandValToReplace; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 434 | |
| 435 | /// Imm - The immediate value that should be added to the base immediately |
| 436 | /// before Inst, because it will be folded into the imm field of the |
| 437 | /// instruction. |
| 438 | SCEVHandle Imm; |
| 439 | |
| 440 | /// EmittedBase - The actual value* to use for the base value of this |
| 441 | /// operation. This is null if we should just use zero so far. |
| 442 | Value *EmittedBase; |
| 443 | |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 444 | // isUseOfPostIncrementedValue - True if this should use the |
| 445 | // post-incremented version of this IV, not the preincremented version. |
| 446 | // This can only be set in special cases, such as the terminating setcc |
Chris Lattner | a676483 | 2005-09-12 06:04:47 +0000 | [diff] [blame] | 447 | // instruction for a loop and uses outside the loop that are dominated by |
| 448 | // the loop. |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 449 | bool isUseOfPostIncrementedValue; |
Chris Lattner | 37c24cc | 2005-08-08 22:56:21 +0000 | [diff] [blame] | 450 | |
| 451 | BasedUser(IVStrideUse &IVSU) |
| 452 | : Base(IVSU.Offset), Inst(IVSU.User), |
| 453 | OperandValToReplace(IVSU.OperandValToReplace), |
| 454 | Imm(SCEVUnknown::getIntegerSCEV(0, Base->getType())), EmittedBase(0), |
| 455 | isUseOfPostIncrementedValue(IVSU.isUseOfPostIncrementedValue) {} |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 456 | |
Chris Lattner | a6d7c35 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 457 | // Once we rewrite the code to insert the new IVs we want, update the |
| 458 | // operands of Inst to use the new expression 'NewBase', with 'Imm' added |
| 459 | // to it. |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 460 | void RewriteInstructionToUseNewBase(const SCEVHandle &NewBase, |
Chris Lattner | 8447b49 | 2005-08-12 22:22:17 +0000 | [diff] [blame] | 461 | SCEVExpander &Rewriter, Loop *L, |
| 462 | Pass *P); |
Chris Lattner | 2959f00 | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 463 | |
| 464 | Value *InsertCodeForBaseAtPosition(const SCEVHandle &NewBase, |
| 465 | SCEVExpander &Rewriter, |
| 466 | Instruction *IP, Loop *L); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 467 | void dump() const; |
| 468 | }; |
| 469 | } |
| 470 | |
| 471 | void BasedUser::dump() const { |
Chris Lattner | 37c24cc | 2005-08-08 22:56:21 +0000 | [diff] [blame] | 472 | std::cerr << " Base=" << *Base; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 473 | std::cerr << " Imm=" << *Imm; |
| 474 | if (EmittedBase) |
| 475 | std::cerr << " EB=" << *EmittedBase; |
| 476 | |
| 477 | std::cerr << " Inst: " << *Inst; |
| 478 | } |
| 479 | |
Chris Lattner | 2959f00 | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 480 | Value *BasedUser::InsertCodeForBaseAtPosition(const SCEVHandle &NewBase, |
| 481 | SCEVExpander &Rewriter, |
| 482 | Instruction *IP, Loop *L) { |
| 483 | // Figure out where we *really* want to insert this code. In particular, if |
| 484 | // the user is inside of a loop that is nested inside of L, we really don't |
| 485 | // want to insert this expression before the user, we'd rather pull it out as |
| 486 | // many loops as possible. |
| 487 | LoopInfo &LI = Rewriter.getLoopInfo(); |
| 488 | Instruction *BaseInsertPt = IP; |
| 489 | |
| 490 | // Figure out the most-nested loop that IP is in. |
| 491 | Loop *InsertLoop = LI.getLoopFor(IP->getParent()); |
| 492 | |
| 493 | // If InsertLoop is not L, and InsertLoop is nested inside of L, figure out |
| 494 | // the preheader of the outer-most loop where NewBase is not loop invariant. |
| 495 | while (InsertLoop && NewBase->isLoopInvariant(InsertLoop)) { |
| 496 | BaseInsertPt = InsertLoop->getLoopPreheader()->getTerminator(); |
| 497 | InsertLoop = InsertLoop->getParentLoop(); |
| 498 | } |
| 499 | |
| 500 | // If there is no immediate value, skip the next part. |
| 501 | if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Imm)) |
| 502 | if (SC->getValue()->isNullValue()) |
| 503 | return Rewriter.expandCodeFor(NewBase, BaseInsertPt, |
| 504 | OperandValToReplace->getType()); |
| 505 | |
| 506 | Value *Base = Rewriter.expandCodeFor(NewBase, BaseInsertPt); |
| 507 | |
| 508 | // Always emit the immediate (if non-zero) into the same block as the user. |
| 509 | SCEVHandle NewValSCEV = SCEVAddExpr::get(SCEVUnknown::get(Base), Imm); |
| 510 | return Rewriter.expandCodeFor(NewValSCEV, IP, |
| 511 | OperandValToReplace->getType()); |
| 512 | } |
| 513 | |
| 514 | |
Chris Lattner | a6d7c35 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 515 | // Once we rewrite the code to insert the new IVs we want, update the |
| 516 | // operands of Inst to use the new expression 'NewBase', with 'Imm' added |
| 517 | // to it. |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 518 | void BasedUser::RewriteInstructionToUseNewBase(const SCEVHandle &NewBase, |
Chris Lattner | 4fec86d | 2005-08-12 22:06:11 +0000 | [diff] [blame] | 519 | SCEVExpander &Rewriter, |
Chris Lattner | 8447b49 | 2005-08-12 22:22:17 +0000 | [diff] [blame] | 520 | Loop *L, Pass *P) { |
Chris Lattner | a6d7c35 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 521 | if (!isa<PHINode>(Inst)) { |
Chris Lattner | 2959f00 | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 522 | Value *NewVal = InsertCodeForBaseAtPosition(NewBase, Rewriter, Inst, L); |
Chris Lattner | a6d7c35 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 523 | // Replace the use of the operand Value with the new Phi we just created. |
| 524 | Inst->replaceUsesOfWith(OperandValToReplace, NewVal); |
| 525 | DEBUG(std::cerr << " CHANGED: IMM =" << *Imm << " Inst = " << *Inst); |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | // PHI nodes are more complex. We have to insert one copy of the NewBase+Imm |
Chris Lattner | dde7dc5 | 2005-08-10 00:35:32 +0000 | [diff] [blame] | 530 | // expression into each operand block that uses it. Note that PHI nodes can |
| 531 | // have multiple entries for the same predecessor. We use a map to make sure |
| 532 | // that a PHI node only has a single Value* for each predecessor (which also |
| 533 | // prevents us from inserting duplicate code in some blocks). |
| 534 | std::map<BasicBlock*, Value*> InsertedCode; |
Chris Lattner | a6d7c35 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 535 | PHINode *PN = cast<PHINode>(Inst); |
| 536 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 537 | if (PN->getIncomingValue(i) == OperandValToReplace) { |
Chris Lattner | 4fec86d | 2005-08-12 22:06:11 +0000 | [diff] [blame] | 538 | // If this is a critical edge, split the edge so that we do not insert the |
Chris Lattner | fd018c8 | 2005-09-13 02:09:55 +0000 | [diff] [blame] | 539 | // code on all predecessor/successor paths. We do this unless this is the |
| 540 | // canonical backedge for this loop, as this can make some inserted code |
| 541 | // be in an illegal position. |
Chris Lattner | 8fcce17 | 2005-10-03 00:31:52 +0000 | [diff] [blame] | 542 | BasicBlock *PHIPred = PN->getIncomingBlock(i); |
| 543 | if (e != 1 && PHIPred->getTerminator()->getNumSuccessors() > 1 && |
| 544 | (PN->getParent() != L->getHeader() || !L->contains(PHIPred))) { |
Chris Lattner | 8fcce17 | 2005-10-03 00:31:52 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 2bf7cb5 | 2005-08-17 06:35:16 +0000 | [diff] [blame] | 546 | // First step, split the critical edge. |
Chris Lattner | 8fcce17 | 2005-10-03 00:31:52 +0000 | [diff] [blame] | 547 | SplitCriticalEdge(PHIPred, PN->getParent(), P); |
Chris Lattner | 8447b49 | 2005-08-12 22:22:17 +0000 | [diff] [blame] | 548 | |
Chris Lattner | 2bf7cb5 | 2005-08-17 06:35:16 +0000 | [diff] [blame] | 549 | // Next step: move the basic block. In particular, if the PHI node |
| 550 | // is outside of the loop, and PredTI is in the loop, we want to |
| 551 | // move the block to be immediately before the PHI block, not |
| 552 | // immediately after PredTI. |
Chris Lattner | 8fcce17 | 2005-10-03 00:31:52 +0000 | [diff] [blame] | 553 | if (L->contains(PHIPred) && !L->contains(PN->getParent())) { |
Chris Lattner | 2bf7cb5 | 2005-08-17 06:35:16 +0000 | [diff] [blame] | 554 | BasicBlock *NewBB = PN->getIncomingBlock(i); |
| 555 | NewBB->moveBefore(PN->getParent()); |
Chris Lattner | 4fec86d | 2005-08-12 22:06:11 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
Chris Lattner | a6d7c35 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 558 | |
Chris Lattner | dde7dc5 | 2005-08-10 00:35:32 +0000 | [diff] [blame] | 559 | Value *&Code = InsertedCode[PN->getIncomingBlock(i)]; |
| 560 | if (!Code) { |
| 561 | // Insert the code into the end of the predecessor block. |
Chris Lattner | 2959f00 | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 562 | Instruction *InsertPt = PN->getIncomingBlock(i)->getTerminator(); |
| 563 | Code = InsertCodeForBaseAtPosition(NewBase, Rewriter, InsertPt, L); |
Chris Lattner | dde7dc5 | 2005-08-10 00:35:32 +0000 | [diff] [blame] | 564 | } |
Chris Lattner | a6d7c35 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 565 | |
| 566 | // Replace the use of the operand Value with the new Phi we just created. |
Chris Lattner | dde7dc5 | 2005-08-10 00:35:32 +0000 | [diff] [blame] | 567 | PN->setIncomingValue(i, Code); |
Chris Lattner | a6d7c35 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 568 | Rewriter.clear(); |
| 569 | } |
| 570 | } |
| 571 | DEBUG(std::cerr << " CHANGED: IMM =" << *Imm << " Inst = " << *Inst); |
| 572 | } |
| 573 | |
| 574 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 575 | /// isTargetConstant - Return true if the following can be referenced by the |
| 576 | /// immediate field of a target instruction. |
| 577 | static bool isTargetConstant(const SCEVHandle &V) { |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 578 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 579 | // FIXME: Look at the target to decide if &GV is a legal constant immediate. |
Chris Lattner | 14203e8 | 2005-08-08 06:25:50 +0000 | [diff] [blame] | 580 | if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) { |
| 581 | // PPC allows a sign-extended 16-bit immediate field. |
Chris Lattner | 0772007 | 2005-12-05 18:23:57 +0000 | [diff] [blame] | 582 | int64_t V = SC->getValue()->getSExtValue(); |
| 583 | if (V > -(1 << 16) && V < (1 << 16)-1) |
| 584 | return true; |
Chris Lattner | 14203e8 | 2005-08-08 06:25:50 +0000 | [diff] [blame] | 585 | return false; |
| 586 | } |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 587 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 588 | return false; // ENABLE this for x86 |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 589 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 590 | if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) |
| 591 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(SU->getValue())) |
| 592 | if (CE->getOpcode() == Instruction::Cast) |
| 593 | if (isa<GlobalValue>(CE->getOperand(0))) |
| 594 | // FIXME: should check to see that the dest is uintptr_t! |
| 595 | return true; |
| 596 | return false; |
| 597 | } |
| 598 | |
Chris Lattner | 37ed895 | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 599 | /// MoveLoopVariantsToImediateField - Move any subexpressions from Val that are |
| 600 | /// loop varying to the Imm operand. |
| 601 | static void MoveLoopVariantsToImediateField(SCEVHandle &Val, SCEVHandle &Imm, |
| 602 | Loop *L) { |
| 603 | if (Val->isLoopInvariant(L)) return; // Nothing to do. |
| 604 | |
| 605 | if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) { |
| 606 | std::vector<SCEVHandle> NewOps; |
| 607 | NewOps.reserve(SAE->getNumOperands()); |
| 608 | |
| 609 | for (unsigned i = 0; i != SAE->getNumOperands(); ++i) |
| 610 | if (!SAE->getOperand(i)->isLoopInvariant(L)) { |
| 611 | // If this is a loop-variant expression, it must stay in the immediate |
| 612 | // field of the expression. |
| 613 | Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i)); |
| 614 | } else { |
| 615 | NewOps.push_back(SAE->getOperand(i)); |
| 616 | } |
| 617 | |
| 618 | if (NewOps.empty()) |
| 619 | Val = SCEVUnknown::getIntegerSCEV(0, Val->getType()); |
| 620 | else |
| 621 | Val = SCEVAddExpr::get(NewOps); |
| 622 | } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) { |
| 623 | // Try to pull immediates out of the start value of nested addrec's. |
| 624 | SCEVHandle Start = SARE->getStart(); |
| 625 | MoveLoopVariantsToImediateField(Start, Imm, L); |
| 626 | |
| 627 | std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end()); |
| 628 | Ops[0] = Start; |
| 629 | Val = SCEVAddRecExpr::get(Ops, SARE->getLoop()); |
| 630 | } else { |
| 631 | // Otherwise, all of Val is variant, move the whole thing over. |
| 632 | Imm = SCEVAddExpr::get(Imm, Val); |
| 633 | Val = SCEVUnknown::getIntegerSCEV(0, Val->getType()); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | |
Chris Lattner | 45f8b6e | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 638 | /// MoveImmediateValues - Look at Val, and pull out any additions of constants |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 639 | /// that can fit into the immediate field of instructions in the target. |
Chris Lattner | 45f8b6e | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 640 | /// Accumulate these immediate values into the Imm value. |
| 641 | static void MoveImmediateValues(SCEVHandle &Val, SCEVHandle &Imm, |
| 642 | bool isAddress, Loop *L) { |
Chris Lattner | fc62470 | 2005-08-03 23:44:42 +0000 | [diff] [blame] | 643 | if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) { |
Chris Lattner | 45f8b6e | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 644 | std::vector<SCEVHandle> NewOps; |
| 645 | NewOps.reserve(SAE->getNumOperands()); |
| 646 | |
Chris Lattner | 2959f00 | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 647 | for (unsigned i = 0; i != SAE->getNumOperands(); ++i) { |
| 648 | SCEVHandle NewOp = SAE->getOperand(i); |
| 649 | MoveImmediateValues(NewOp, Imm, isAddress, L); |
| 650 | |
| 651 | if (!NewOp->isLoopInvariant(L)) { |
Chris Lattner | acc42c4 | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 652 | // If this is a loop-variant expression, it must stay in the immediate |
| 653 | // field of the expression. |
Chris Lattner | 2959f00 | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 654 | Imm = SCEVAddExpr::get(Imm, NewOp); |
Chris Lattner | 45f8b6e | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 655 | } else { |
Chris Lattner | 2959f00 | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 656 | NewOps.push_back(NewOp); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 657 | } |
Chris Lattner | 2959f00 | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 658 | } |
Chris Lattner | 45f8b6e | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 659 | |
| 660 | if (NewOps.empty()) |
| 661 | Val = SCEVUnknown::getIntegerSCEV(0, Val->getType()); |
| 662 | else |
| 663 | Val = SCEVAddExpr::get(NewOps); |
| 664 | return; |
Chris Lattner | fc62470 | 2005-08-03 23:44:42 +0000 | [diff] [blame] | 665 | } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) { |
| 666 | // Try to pull immediates out of the start value of nested addrec's. |
Chris Lattner | 45f8b6e | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 667 | SCEVHandle Start = SARE->getStart(); |
| 668 | MoveImmediateValues(Start, Imm, isAddress, L); |
| 669 | |
| 670 | if (Start != SARE->getStart()) { |
| 671 | std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end()); |
| 672 | Ops[0] = Start; |
| 673 | Val = SCEVAddRecExpr::get(Ops, SARE->getLoop()); |
| 674 | } |
| 675 | return; |
Chris Lattner | 2959f00 | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 676 | } else if (SCEVMulExpr *SME = dyn_cast<SCEVMulExpr>(Val)) { |
| 677 | // Transform "8 * (4 + v)" -> "32 + 8*V" if "32" fits in the immed field. |
| 678 | if (isAddress && isTargetConstant(SME->getOperand(0)) && |
| 679 | SME->getNumOperands() == 2 && SME->isLoopInvariant(L)) { |
| 680 | |
| 681 | SCEVHandle SubImm = SCEVUnknown::getIntegerSCEV(0, Val->getType()); |
| 682 | SCEVHandle NewOp = SME->getOperand(1); |
| 683 | MoveImmediateValues(NewOp, SubImm, isAddress, L); |
| 684 | |
| 685 | // If we extracted something out of the subexpressions, see if we can |
| 686 | // simplify this! |
| 687 | if (NewOp != SME->getOperand(1)) { |
| 688 | // Scale SubImm up by "8". If the result is a target constant, we are |
| 689 | // good. |
| 690 | SubImm = SCEVMulExpr::get(SubImm, SME->getOperand(0)); |
| 691 | if (isTargetConstant(SubImm)) { |
| 692 | // Accumulate the immediate. |
| 693 | Imm = SCEVAddExpr::get(Imm, SubImm); |
| 694 | |
| 695 | // Update what is left of 'Val'. |
| 696 | Val = SCEVMulExpr::get(SME->getOperand(0), NewOp); |
| 697 | return; |
| 698 | } |
| 699 | } |
| 700 | } |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Chris Lattner | 45f8b6e | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 703 | // Loop-variant expressions must stay in the immediate field of the |
| 704 | // expression. |
| 705 | if ((isAddress && isTargetConstant(Val)) || |
| 706 | !Val->isLoopInvariant(L)) { |
| 707 | Imm = SCEVAddExpr::get(Imm, Val); |
| 708 | Val = SCEVUnknown::getIntegerSCEV(0, Val->getType()); |
| 709 | return; |
Chris Lattner | 0f7c0fa | 2005-08-04 19:26:19 +0000 | [diff] [blame] | 710 | } |
Chris Lattner | 45f8b6e | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 711 | |
| 712 | // Otherwise, no immediates to move. |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Chris Lattner | 5949d49 | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 715 | |
| 716 | /// IncrementAddExprUses - Decompose the specified expression into its added |
| 717 | /// subexpressions, and increment SubExpressionUseCounts for each of these |
| 718 | /// decomposed parts. |
| 719 | static void SeparateSubExprs(std::vector<SCEVHandle> &SubExprs, |
| 720 | SCEVHandle Expr) { |
| 721 | if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(Expr)) { |
| 722 | for (unsigned j = 0, e = AE->getNumOperands(); j != e; ++j) |
| 723 | SeparateSubExprs(SubExprs, AE->getOperand(j)); |
| 724 | } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Expr)) { |
| 725 | SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Expr->getType()); |
| 726 | if (SARE->getOperand(0) == Zero) { |
| 727 | SubExprs.push_back(Expr); |
| 728 | } else { |
| 729 | // Compute the addrec with zero as its base. |
| 730 | std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end()); |
| 731 | Ops[0] = Zero; // Start with zero base. |
| 732 | SubExprs.push_back(SCEVAddRecExpr::get(Ops, SARE->getLoop())); |
| 733 | |
| 734 | |
| 735 | SeparateSubExprs(SubExprs, SARE->getOperand(0)); |
| 736 | } |
| 737 | } else if (!isa<SCEVConstant>(Expr) || |
| 738 | !cast<SCEVConstant>(Expr)->getValue()->isNullValue()) { |
| 739 | // Do not add zero. |
| 740 | SubExprs.push_back(Expr); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 745 | /// RemoveCommonExpressionsFromUseBases - Look through all of the uses in Bases, |
| 746 | /// removing any common subexpressions from it. Anything truly common is |
| 747 | /// removed, accumulated, and returned. This looks for things like (a+b+c) and |
| 748 | /// (a+c+d) -> (a+c). The common expression is *removed* from the Bases. |
| 749 | static SCEVHandle |
| 750 | RemoveCommonExpressionsFromUseBases(std::vector<BasedUser> &Uses) { |
| 751 | unsigned NumUses = Uses.size(); |
| 752 | |
| 753 | // Only one use? Use its base, regardless of what it is! |
| 754 | SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Uses[0].Base->getType()); |
| 755 | SCEVHandle Result = Zero; |
| 756 | if (NumUses == 1) { |
| 757 | std::swap(Result, Uses[0].Base); |
| 758 | return Result; |
| 759 | } |
| 760 | |
| 761 | // To find common subexpressions, count how many of Uses use each expression. |
| 762 | // If any subexpressions are used Uses.size() times, they are common. |
| 763 | std::map<SCEVHandle, unsigned> SubExpressionUseCounts; |
| 764 | |
Chris Lattner | 192cd18 | 2005-10-11 18:41:04 +0000 | [diff] [blame] | 765 | // UniqueSubExprs - Keep track of all of the subexpressions we see in the |
| 766 | // order we see them. |
| 767 | std::vector<SCEVHandle> UniqueSubExprs; |
| 768 | |
Chris Lattner | 5949d49 | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 769 | std::vector<SCEVHandle> SubExprs; |
| 770 | for (unsigned i = 0; i != NumUses; ++i) { |
| 771 | // If the base is zero (which is common), return zero now, there are no |
| 772 | // CSEs we can find. |
| 773 | if (Uses[i].Base == Zero) return Zero; |
| 774 | |
| 775 | // Split the expression into subexprs. |
| 776 | SeparateSubExprs(SubExprs, Uses[i].Base); |
| 777 | // Add one to SubExpressionUseCounts for each subexpr present. |
| 778 | for (unsigned j = 0, e = SubExprs.size(); j != e; ++j) |
Chris Lattner | 192cd18 | 2005-10-11 18:41:04 +0000 | [diff] [blame] | 779 | if (++SubExpressionUseCounts[SubExprs[j]] == 1) |
| 780 | UniqueSubExprs.push_back(SubExprs[j]); |
Chris Lattner | 5949d49 | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 781 | SubExprs.clear(); |
| 782 | } |
| 783 | |
Chris Lattner | 192cd18 | 2005-10-11 18:41:04 +0000 | [diff] [blame] | 784 | // Now that we know how many times each is used, build Result. Iterate over |
| 785 | // UniqueSubexprs so that we have a stable ordering. |
| 786 | for (unsigned i = 0, e = UniqueSubExprs.size(); i != e; ++i) { |
| 787 | std::map<SCEVHandle, unsigned>::iterator I = |
| 788 | SubExpressionUseCounts.find(UniqueSubExprs[i]); |
| 789 | assert(I != SubExpressionUseCounts.end() && "Entry not found?"); |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 790 | if (I->second == NumUses) { // Found CSE! |
| 791 | Result = SCEVAddExpr::get(Result, I->first); |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 792 | } else { |
| 793 | // Remove non-cse's from SubExpressionUseCounts. |
Chris Lattner | 192cd18 | 2005-10-11 18:41:04 +0000 | [diff] [blame] | 794 | SubExpressionUseCounts.erase(I); |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 795 | } |
Chris Lattner | 192cd18 | 2005-10-11 18:41:04 +0000 | [diff] [blame] | 796 | } |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 797 | |
| 798 | // If we found no CSE's, return now. |
| 799 | if (Result == Zero) return Result; |
| 800 | |
| 801 | // Otherwise, remove all of the CSE's we found from each of the base values. |
Chris Lattner | 5949d49 | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 802 | for (unsigned i = 0; i != NumUses; ++i) { |
| 803 | // Split the expression into subexprs. |
| 804 | SeparateSubExprs(SubExprs, Uses[i].Base); |
| 805 | |
| 806 | // Remove any common subexpressions. |
| 807 | for (unsigned j = 0, e = SubExprs.size(); j != e; ++j) |
| 808 | if (SubExpressionUseCounts.count(SubExprs[j])) { |
| 809 | SubExprs.erase(SubExprs.begin()+j); |
| 810 | --j; --e; |
| 811 | } |
| 812 | |
| 813 | // Finally, the non-shared expressions together. |
| 814 | if (SubExprs.empty()) |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 815 | Uses[i].Base = Zero; |
Chris Lattner | 5949d49 | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 816 | else |
| 817 | Uses[i].Base = SCEVAddExpr::get(SubExprs); |
Chris Lattner | 47d3ec3 | 2005-08-13 07:42:01 +0000 | [diff] [blame] | 818 | SubExprs.clear(); |
Chris Lattner | 5949d49 | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 819 | } |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 820 | |
| 821 | return Result; |
| 822 | } |
| 823 | |
| 824 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 825 | /// StrengthReduceStridedIVUsers - Strength reduce all of the users of a single |
| 826 | /// stride of IV. All of the users may have different starting values, and this |
| 827 | /// may not be the only stride (we know it is if isOnlyStride is true). |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 828 | void LoopStrengthReduce::StrengthReduceStridedIVUsers(const SCEVHandle &Stride, |
Chris Lattner | 430d002 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 829 | IVUsersOfOneStride &Uses, |
| 830 | Loop *L, |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 831 | bool isOnlyStride) { |
| 832 | // Transform our list of users and offsets to a bit more complex table. In |
Chris Lattner | 37c24cc | 2005-08-08 22:56:21 +0000 | [diff] [blame] | 833 | // this new vector, each 'BasedUser' contains 'Base' the base of the |
| 834 | // strided accessas well as the old information from Uses. We progressively |
| 835 | // move information from the Base field to the Imm field, until we eventually |
| 836 | // have the full access expression to rewrite the use. |
| 837 | std::vector<BasedUser> UsersToProcess; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 838 | UsersToProcess.reserve(Uses.Users.size()); |
Chris Lattner | 37c24cc | 2005-08-08 22:56:21 +0000 | [diff] [blame] | 839 | for (unsigned i = 0, e = Uses.Users.size(); i != e; ++i) { |
| 840 | UsersToProcess.push_back(Uses.Users[i]); |
| 841 | |
| 842 | // Move any loop invariant operands from the offset field to the immediate |
| 843 | // field of the use, so that we don't try to use something before it is |
| 844 | // computed. |
| 845 | MoveLoopVariantsToImediateField(UsersToProcess.back().Base, |
| 846 | UsersToProcess.back().Imm, L); |
| 847 | assert(UsersToProcess.back().Base->isLoopInvariant(L) && |
Chris Lattner | 45f8b6e | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 848 | "Base value is not loop invariant!"); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 849 | } |
Chris Lattner | 37ed895 | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 850 | |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 851 | // We now have a whole bunch of uses of like-strided induction variables, but |
| 852 | // they might all have different bases. We want to emit one PHI node for this |
| 853 | // stride which we fold as many common expressions (between the IVs) into as |
| 854 | // possible. Start by identifying the common expressions in the base values |
| 855 | // for the strides (e.g. if we have "A+C+B" and "A+B+D" as our bases, find |
| 856 | // "A+B"), emit it to the preheader, then remove the expression from the |
| 857 | // UsersToProcess base values. |
| 858 | SCEVHandle CommonExprs = RemoveCommonExpressionsFromUseBases(UsersToProcess); |
| 859 | |
Chris Lattner | 37ed895 | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 860 | // Next, figure out what we can represent in the immediate fields of |
| 861 | // instructions. If we can represent anything there, move it to the imm |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 862 | // fields of the BasedUsers. We do this so that it increases the commonality |
| 863 | // of the remaining uses. |
Chris Lattner | 37ed895 | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 864 | for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) { |
Chris Lattner | 5cf983e | 2005-08-16 00:38:11 +0000 | [diff] [blame] | 865 | // If the user is not in the current loop, this means it is using the exit |
| 866 | // value of the IV. Do not put anything in the base, make sure it's all in |
| 867 | // the immediate field to allow as much factoring as possible. |
| 868 | if (!L->contains(UsersToProcess[i].Inst->getParent())) { |
Chris Lattner | ea7dfd5 | 2005-08-17 21:22:41 +0000 | [diff] [blame] | 869 | UsersToProcess[i].Imm = SCEVAddExpr::get(UsersToProcess[i].Imm, |
| 870 | UsersToProcess[i].Base); |
| 871 | UsersToProcess[i].Base = |
| 872 | SCEVUnknown::getIntegerSCEV(0, UsersToProcess[i].Base->getType()); |
Chris Lattner | 5cf983e | 2005-08-16 00:38:11 +0000 | [diff] [blame] | 873 | } else { |
| 874 | |
| 875 | // Addressing modes can be folded into loads and stores. Be careful that |
| 876 | // the store is through the expression, not of the expression though. |
| 877 | bool isAddress = isa<LoadInst>(UsersToProcess[i].Inst); |
| 878 | if (StoreInst *SI = dyn_cast<StoreInst>(UsersToProcess[i].Inst)) |
| 879 | if (SI->getOperand(1) == UsersToProcess[i].OperandValToReplace) |
| 880 | isAddress = true; |
| 881 | |
| 882 | MoveImmediateValues(UsersToProcess[i].Base, UsersToProcess[i].Imm, |
| 883 | isAddress, L); |
| 884 | } |
Chris Lattner | 37ed895 | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 887 | // Now that we know what we need to do, insert the PHI node itself. |
| 888 | // |
| 889 | DEBUG(std::cerr << "INSERTING IV of STRIDE " << *Stride << " and BASE " |
| 890 | << *CommonExprs << " :\n"); |
| 891 | |
| 892 | SCEVExpander Rewriter(*SE, *LI); |
| 893 | SCEVExpander PreheaderRewriter(*SE, *LI); |
Chris Lattner | 37ed895 | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 894 | |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 895 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 896 | Instruction *PreInsertPt = Preheader->getTerminator(); |
| 897 | Instruction *PhiInsertBefore = L->getHeader()->begin(); |
Chris Lattner | 37ed895 | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 898 | |
Chris Lattner | 8048b85 | 2005-09-12 17:11:27 +0000 | [diff] [blame] | 899 | BasicBlock *LatchBlock = L->getLoopLatch(); |
Chris Lattner | bb78c97 | 2005-08-03 23:30:08 +0000 | [diff] [blame] | 900 | |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 901 | // Create a new Phi for this base, and stick it in the loop header. |
| 902 | const Type *ReplacedTy = CommonExprs->getType(); |
| 903 | PHINode *NewPHI = new PHINode(ReplacedTy, "iv.", PhiInsertBefore); |
| 904 | ++NumInserted; |
| 905 | |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 906 | // Insert the stride into the preheader. |
| 907 | Value *StrideV = PreheaderRewriter.expandCodeFor(Stride, PreInsertPt, |
| 908 | ReplacedTy); |
| 909 | if (!isa<ConstantInt>(StrideV)) ++NumVariable; |
| 910 | |
| 911 | |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 912 | // Emit the initial base value into the loop preheader, and add it to the |
| 913 | // Phi node. |
| 914 | Value *PHIBaseV = PreheaderRewriter.expandCodeFor(CommonExprs, PreInsertPt, |
| 915 | ReplacedTy); |
| 916 | NewPHI->addIncoming(PHIBaseV, Preheader); |
| 917 | |
| 918 | // Emit the increment of the base value before the terminator of the loop |
| 919 | // latch block, and add it to the Phi node. |
| 920 | SCEVHandle IncExp = SCEVAddExpr::get(SCEVUnknown::get(NewPHI), |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 921 | SCEVUnknown::get(StrideV)); |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 922 | |
| 923 | Value *IncV = Rewriter.expandCodeFor(IncExp, LatchBlock->getTerminator(), |
| 924 | ReplacedTy); |
| 925 | IncV->setName(NewPHI->getName()+".inc"); |
| 926 | NewPHI->addIncoming(IncV, LatchBlock); |
| 927 | |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 928 | // Sort by the base value, so that all IVs with identical bases are next to |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 929 | // each other. |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 930 | while (!UsersToProcess.empty()) { |
Chris Lattner | 5c9d63d | 2005-10-11 18:30:57 +0000 | [diff] [blame] | 931 | SCEVHandle Base = UsersToProcess.back().Base; |
Chris Lattner | bb78c97 | 2005-08-03 23:30:08 +0000 | [diff] [blame] | 932 | |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 933 | DEBUG(std::cerr << " INSERTING code for BASE = " << *Base << ":\n"); |
Chris Lattner | bb78c97 | 2005-08-03 23:30:08 +0000 | [diff] [blame] | 934 | |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 935 | // Emit the code for Base into the preheader. |
Chris Lattner | c70bbc0 | 2005-08-08 05:47:49 +0000 | [diff] [blame] | 936 | Value *BaseV = PreheaderRewriter.expandCodeFor(Base, PreInsertPt, |
| 937 | ReplacedTy); |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 938 | |
| 939 | // If BaseV is a constant other than 0, make sure that it gets inserted into |
| 940 | // the preheader, instead of being forward substituted into the uses. We do |
| 941 | // this by forcing a noop cast to be inserted into the preheader in this |
| 942 | // case. |
| 943 | if (Constant *C = dyn_cast<Constant>(BaseV)) |
Chris Lattner | 530fe6a | 2005-09-10 01:18:45 +0000 | [diff] [blame] | 944 | if (!C->isNullValue() && !isTargetConstant(Base)) { |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 945 | // We want this constant emitted into the preheader! |
| 946 | BaseV = new CastInst(BaseV, BaseV->getType(), "preheaderinsert", |
| 947 | PreInsertPt); |
| 948 | } |
| 949 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 950 | // 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] | 951 | // the instructions that we identified as using this stride and base. |
Chris Lattner | 5c9d63d | 2005-10-11 18:30:57 +0000 | [diff] [blame] | 952 | unsigned ScanPos = 0; |
| 953 | do { |
| 954 | BasedUser &User = UsersToProcess.back(); |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 955 | |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 956 | // If this instruction wants to use the post-incremented value, move it |
| 957 | // after the post-inc and use its value instead of the PHI. |
| 958 | Value *RewriteOp = NewPHI; |
| 959 | if (User.isUseOfPostIncrementedValue) { |
| 960 | RewriteOp = IncV; |
Chris Lattner | a676483 | 2005-09-12 06:04:47 +0000 | [diff] [blame] | 961 | |
| 962 | // If this user is in the loop, make sure it is the last thing in the |
| 963 | // loop to ensure it is dominated by the increment. |
| 964 | if (L->contains(User.Inst->getParent())) |
| 965 | User.Inst->moveBefore(LatchBlock->getTerminator()); |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 966 | } |
| 967 | SCEVHandle RewriteExpr = SCEVUnknown::get(RewriteOp); |
| 968 | |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 969 | // Clear the SCEVExpander's expression map so that we are guaranteed |
| 970 | // to have the code emitted where we expect it. |
| 971 | Rewriter.clear(); |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 972 | |
Chris Lattner | a6d7c35 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 973 | // Now that we know what we need to do, insert code before User for the |
| 974 | // immediate and any loop-variant expressions. |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 975 | if (!isa<ConstantInt>(BaseV) || !cast<ConstantInt>(BaseV)->isNullValue()) |
| 976 | // Add BaseV to the PHI value if needed. |
| 977 | RewriteExpr = SCEVAddExpr::get(RewriteExpr, SCEVUnknown::get(BaseV)); |
| 978 | |
Chris Lattner | 8447b49 | 2005-08-12 22:22:17 +0000 | [diff] [blame] | 979 | User.RewriteInstructionToUseNewBase(RewriteExpr, Rewriter, L, this); |
Jeff Cohen | 546fd59 | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 980 | |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 981 | // Mark old value we replaced as possibly dead, so that it is elminated |
| 982 | // if we just replaced the last use of that value. |
Chris Lattner | a6d7c35 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 983 | DeadInsts.insert(cast<Instruction>(User.OperandValToReplace)); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 984 | |
Chris Lattner | 5c9d63d | 2005-10-11 18:30:57 +0000 | [diff] [blame] | 985 | UsersToProcess.pop_back(); |
Chris Lattner | db23c74 | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 986 | ++NumReduced; |
Chris Lattner | 5c9d63d | 2005-10-11 18:30:57 +0000 | [diff] [blame] | 987 | |
| 988 | // If there are any more users to process with the same base, move one of |
| 989 | // them to the end of the list so that we will process it. |
| 990 | if (!UsersToProcess.empty()) { |
| 991 | for (unsigned e = UsersToProcess.size(); ScanPos != e; ++ScanPos) |
| 992 | if (UsersToProcess[ScanPos].Base == Base) { |
| 993 | std::swap(UsersToProcess[ScanPos], UsersToProcess.back()); |
| 994 | break; |
| 995 | } |
| 996 | } |
| 997 | } while (!UsersToProcess.empty() && UsersToProcess.back().Base == Base); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 998 | // TODO: Next, find out which base index is the most common, pull it out. |
| 999 | } |
| 1000 | |
| 1001 | // IMPORTANT TODO: Figure out how to partition the IV's with this stride, but |
| 1002 | // different starting values, into different PHIs. |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 1005 | // OptimizeIndvars - Now that IVUsesByStride is set up with all of the indvar |
| 1006 | // uses in the loop, look to see if we can eliminate some, in favor of using |
| 1007 | // common indvars for the different uses. |
| 1008 | void LoopStrengthReduce::OptimizeIndvars(Loop *L) { |
| 1009 | // TODO: implement optzns here. |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | // Finally, get the terminating condition for the loop if possible. If we |
| 1015 | // can, we want to change it to use a post-incremented version of its |
| 1016 | // induction variable, to allow coallescing the live ranges for the IV into |
| 1017 | // one register value. |
| 1018 | PHINode *SomePHI = cast<PHINode>(L->getHeader()->begin()); |
| 1019 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 1020 | BasicBlock *LatchBlock = |
| 1021 | SomePHI->getIncomingBlock(SomePHI->getIncomingBlock(0) == Preheader); |
| 1022 | BranchInst *TermBr = dyn_cast<BranchInst>(LatchBlock->getTerminator()); |
| 1023 | if (!TermBr || TermBr->isUnconditional() || |
| 1024 | !isa<SetCondInst>(TermBr->getCondition())) |
| 1025 | return; |
| 1026 | SetCondInst *Cond = cast<SetCondInst>(TermBr->getCondition()); |
| 1027 | |
| 1028 | // Search IVUsesByStride to find Cond's IVUse if there is one. |
| 1029 | IVStrideUse *CondUse = 0; |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 1030 | const SCEVHandle *CondStride = 0; |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 1031 | |
Chris Lattner | b7a3894 | 2005-10-11 18:17:57 +0000 | [diff] [blame] | 1032 | for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e && !CondUse; |
| 1033 | ++Stride) { |
| 1034 | std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI = |
| 1035 | IVUsesByStride.find(StrideOrder[Stride]); |
| 1036 | assert(SI != IVUsesByStride.end() && "Stride doesn't exist!"); |
| 1037 | |
| 1038 | for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(), |
| 1039 | E = SI->second.Users.end(); UI != E; ++UI) |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 1040 | if (UI->User == Cond) { |
| 1041 | CondUse = &*UI; |
Chris Lattner | b7a3894 | 2005-10-11 18:17:57 +0000 | [diff] [blame] | 1042 | CondStride = &SI->first; |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 1043 | // NOTE: we could handle setcc instructions with multiple uses here, but |
| 1044 | // InstCombine does it as well for simple uses, it's not clear that it |
| 1045 | // occurs enough in real life to handle. |
| 1046 | break; |
| 1047 | } |
Chris Lattner | b7a3894 | 2005-10-11 18:17:57 +0000 | [diff] [blame] | 1048 | } |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 1049 | if (!CondUse) return; // setcc doesn't use the IV. |
| 1050 | |
| 1051 | // setcc stride is complex, don't mess with users. |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 1052 | // FIXME: Evaluate whether this is a good idea or not. |
| 1053 | if (!isa<SCEVConstant>(*CondStride)) return; |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 1054 | |
| 1055 | // It's possible for the setcc instruction to be anywhere in the loop, and |
| 1056 | // possible for it to have multiple users. If it is not immediately before |
| 1057 | // the latch block branch, move it. |
| 1058 | if (&*++BasicBlock::iterator(Cond) != (Instruction*)TermBr) { |
| 1059 | if (Cond->hasOneUse()) { // Condition has a single use, just move it. |
| 1060 | Cond->moveBefore(TermBr); |
| 1061 | } else { |
| 1062 | // Otherwise, clone the terminating condition and insert into the loopend. |
| 1063 | Cond = cast<SetCondInst>(Cond->clone()); |
| 1064 | Cond->setName(L->getHeader()->getName() + ".termcond"); |
| 1065 | LatchBlock->getInstList().insert(TermBr, Cond); |
| 1066 | |
| 1067 | // Clone the IVUse, as the old use still exists! |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 1068 | IVUsesByStride[*CondStride].addUser(CondUse->Offset, Cond, |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 1069 | CondUse->OperandValToReplace); |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 1070 | CondUse = &IVUsesByStride[*CondStride].Users.back(); |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | // If we get to here, we know that we can transform the setcc instruction to |
| 1075 | // use the post-incremented version of the IV, allowing us to coallesce the |
| 1076 | // live ranges for the IV correctly. |
Chris Lattner | edff91a | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 1077 | CondUse->Offset = SCEV::getMinusSCEV(CondUse->Offset, *CondStride); |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 1078 | CondUse->isUseOfPostIncrementedValue = true; |
| 1079 | } |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1080 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 1081 | void LoopStrengthReduce::runOnLoop(Loop *L) { |
| 1082 | // First step, transform all loops nesting inside of this loop. |
| 1083 | for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 1084 | runOnLoop(*I); |
| 1085 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1086 | // Next, find all uses of induction variables in this loop, and catagorize |
| 1087 | // them by stride. Start by finding all of the PHI nodes in the header for |
| 1088 | // this loop. If they are induction variables, inspect their uses. |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 1089 | std::set<Instruction*> Processed; // Don't reprocess instructions. |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1090 | for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) |
Chris Lattner | eaf2472 | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 1091 | AddUsersIfInteresting(I, L, Processed); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 1092 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1093 | // If we have nothing to do, return. |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 1094 | if (IVUsesByStride.empty()) return; |
| 1095 | |
| 1096 | // Optimize induction variables. Some indvar uses can be transformed to use |
| 1097 | // strides that will be needed for other purposes. A common example of this |
| 1098 | // is the exit test for the loop, which can often be rewritten to use the |
| 1099 | // computation of some other indvar to decide when to terminate the loop. |
| 1100 | OptimizeIndvars(L); |
| 1101 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1102 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1103 | // FIXME: We can widen subreg IV's here for RISC targets. e.g. instead of |
| 1104 | // doing computation in byte values, promote to 32-bit values if safe. |
| 1105 | |
| 1106 | // FIXME: Attempt to reuse values across multiple IV's. In particular, we |
| 1107 | // could have something like "for(i) { foo(i*8); bar(i*16) }", which should be |
| 1108 | // codegened as "for (j = 0;; j+=8) { foo(j); bar(j+j); }" on X86/PPC. Need |
| 1109 | // to be careful that IV's are all the same type. Only works for intptr_t |
| 1110 | // indvars. |
| 1111 | |
| 1112 | // If we only have one stride, we can more aggressively eliminate some things. |
| 1113 | bool HasOneStride = IVUsesByStride.size() == 1; |
Chris Lattner | 4ea0a3e | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 1114 | |
Chris Lattner | a091ff1 | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1115 | // Note: this processes each stride/type pair individually. All users passed |
Chris Lattner | 4ea0a3e | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 1116 | // into StrengthReduceStridedIVUsers have the same type AND stride. Also, |
| 1117 | // node that we iterate over IVUsesByStride indirectly by using StrideOrder. |
| 1118 | // This extra layer of indirection makes the ordering of strides deterministic |
| 1119 | // - not dependent on map order. |
| 1120 | for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e; ++Stride) { |
| 1121 | std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI = |
| 1122 | IVUsesByStride.find(StrideOrder[Stride]); |
| 1123 | assert(SI != IVUsesByStride.end() && "Stride doesn't exist!"); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1124 | StrengthReduceStridedIVUsers(SI->first, SI->second, L, HasOneStride); |
Chris Lattner | 4ea0a3e | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 1125 | } |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 1126 | |
| 1127 | // Clean up after ourselves |
| 1128 | if (!DeadInsts.empty()) { |
| 1129 | DeleteTriviallyDeadInstructions(DeadInsts); |
| 1130 | |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1131 | BasicBlock::iterator I = L->getHeader()->begin(); |
| 1132 | PHINode *PN; |
Chris Lattner | dcce49e | 2005-08-02 02:44:31 +0000 | [diff] [blame] | 1133 | while ((PN = dyn_cast<PHINode>(I))) { |
Chris Lattner | 564900e | 2005-08-02 00:41:11 +0000 | [diff] [blame] | 1134 | ++I; // Preincrement iterator to avoid invalidating it when deleting PN. |
| 1135 | |
Chris Lattner | c6c4d99 | 2005-08-09 23:39:36 +0000 | [diff] [blame] | 1136 | // At this point, we know that we have killed one or more GEP |
| 1137 | // instructions. It is worth checking to see if the cann indvar is also |
| 1138 | // dead, so that we can remove it as well. The requirements for the cann |
| 1139 | // indvar to be considered dead are: |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1140 | // 1. the cann indvar has one use |
| 1141 | // 2. the use is an add instruction |
| 1142 | // 3. the add has one use |
| 1143 | // 4. the add is used by the cann indvar |
| 1144 | // If all four cases above are true, then we can remove both the add and |
| 1145 | // the cann indvar. |
| 1146 | // FIXME: this needs to eliminate an induction variable even if it's being |
| 1147 | // compared against some value to decide loop termination. |
| 1148 | if (PN->hasOneUse()) { |
| 1149 | BinaryOperator *BO = dyn_cast<BinaryOperator>(*(PN->use_begin())); |
Chris Lattner | 75a44e1 | 2005-08-02 02:52:02 +0000 | [diff] [blame] | 1150 | if (BO && BO->hasOneUse()) { |
| 1151 | if (PN == *(BO->use_begin())) { |
| 1152 | DeadInsts.insert(BO); |
| 1153 | // Break the cycle, then delete the PHI. |
| 1154 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
Chris Lattner | 84e9baa | 2005-08-03 21:36:09 +0000 | [diff] [blame] | 1155 | SE->deleteInstructionFromRecords(PN); |
Chris Lattner | 75a44e1 | 2005-08-02 02:52:02 +0000 | [diff] [blame] | 1156 | PN->eraseFromParent(); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 1157 | } |
Chris Lattner | 75a44e1 | 2005-08-02 02:52:02 +0000 | [diff] [blame] | 1158 | } |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1159 | } |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 1160 | } |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1161 | DeleteTriviallyDeadInstructions(DeadInsts); |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 1162 | } |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | 11e7a5e | 2005-08-05 01:30:11 +0000 | [diff] [blame] | 1164 | CastedPointers.clear(); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1165 | IVUsesByStride.clear(); |
Chris Lattner | 4ea0a3e | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 1166 | StrideOrder.clear(); |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1167 | return; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 1168 | } |