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