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