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