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