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