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