Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1 | //===- LoopStrengthReduce.cpp - Strength Reduce IVs in Loops --------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass performs a strength reduction on array references inside loops that |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 11 | // have as one or more of their components the loop induction variable. |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 12 | // |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | be3e521 | 2005-08-03 23:30:08 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "loop-reduce" |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Scalar.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/Instructions.h" |
Dan Gohman | e5b01be | 2007-05-04 14:59:09 +0000 | [diff] [blame] | 19 | #include "llvm/IntrinsicInst.h" |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 20 | #include "llvm/Type.h" |
Jeff Cohen | 2f3c9b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 21 | #include "llvm/DerivedTypes.h" |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/Dominators.h" |
| 23 | #include "llvm/Analysis/LoopInfo.h" |
Devang Patel | 0f54dcb | 2007-03-06 21:14:09 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/LoopPass.h" |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/AddrModeMatcher.h" |
Chris Lattner | e0391be | 2005-08-12 22:06:11 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/Local.h" |
Jeff Cohen | 2f3c9b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetData.h" |
Evan Cheng | 168a66b | 2007-10-26 23:08:19 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CFG.h" |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Compiler.h" |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | d277f2c | 2006-03-13 23:14:23 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetLowering.h" |
Jeff Cohen | cfb1d42 | 2005-07-30 18:22:27 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Dan Gohman | 13317bc | 2009-04-16 16:46:01 +0000 | [diff] [blame] | 40 | STATISTIC(NumReduced , "Number of IV uses strength reduced"); |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 41 | STATISTIC(NumInserted, "Number of PHIs inserted"); |
| 42 | STATISTIC(NumVariable, "Number of PHIs with variable strides"); |
Devang Patel | 5415327 | 2008-08-27 17:50:18 +0000 | [diff] [blame] | 43 | STATISTIC(NumEliminated, "Number of strides eliminated"); |
| 44 | STATISTIC(NumShadow, "Number of Shadow IVs optimized"); |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 45 | STATISTIC(NumImmSunk, "Number of common expr immediates sunk into uses"); |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 46 | |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 47 | static cl::opt<bool> EnableFullLSRMode("enable-full-lsr", |
| 48 | cl::init(false), |
| 49 | cl::Hidden); |
| 50 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 51 | namespace { |
Dale Johannesen | dc42f48 | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 52 | |
Jeff Cohen | c01a530 | 2007-03-20 20:43:18 +0000 | [diff] [blame] | 53 | struct BasedUser; |
Dale Johannesen | dc42f48 | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 54 | |
Chris Lattner | ec3fb63 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 55 | /// IVStrideUse - Keep track of one use of a strided induction variable, where |
| 56 | /// the stride is stored externally. The Offset member keeps track of the |
Dan Gohman | 9330c3a | 2007-10-29 19:32:39 +0000 | [diff] [blame] | 57 | /// offset from the IV, User is the actual user of the operand, and |
| 58 | /// 'OperandValToReplace' is the operand of the User that is the use. |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 59 | struct VISIBILITY_HIDDEN IVStrideUse { |
Chris Lattner | ec3fb63 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 60 | SCEVHandle Offset; |
| 61 | Instruction *User; |
| 62 | Value *OperandValToReplace; |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 63 | |
| 64 | // isUseOfPostIncrementedValue - True if this should use the |
| 65 | // post-incremented version of this IV, not the preincremented version. |
| 66 | // This can only be set in special cases, such as the terminating setcc |
Chris Lattner | c6bae65 | 2005-09-12 06:04:47 +0000 | [diff] [blame] | 67 | // instruction for a loop or uses dominated by the loop. |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 68 | bool isUseOfPostIncrementedValue; |
Chris Lattner | ec3fb63 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 69 | |
| 70 | IVStrideUse(const SCEVHandle &Offs, Instruction *U, Value *O) |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 71 | : Offset(Offs), User(U), OperandValToReplace(O), |
| 72 | isUseOfPostIncrementedValue(false) {} |
Chris Lattner | ec3fb63 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /// IVUsersOfOneStride - This structure keeps track of all instructions that |
| 76 | /// have an operand that is based on the trip count multiplied by some stride. |
| 77 | /// The stride for all of these users is common and kept external to this |
| 78 | /// structure. |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 79 | struct VISIBILITY_HIDDEN IVUsersOfOneStride { |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 80 | /// Users - Keep track of all of the users of this stride as well as the |
Chris Lattner | ec3fb63 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 81 | /// initial value and the operand that uses the IV. |
| 82 | std::vector<IVStrideUse> Users; |
| 83 | |
| 84 | void addUser(const SCEVHandle &Offset,Instruction *User, Value *Operand) { |
| 85 | Users.push_back(IVStrideUse(Offset, User, Operand)); |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 86 | } |
| 87 | }; |
| 88 | |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 89 | /// IVInfo - This structure keeps track of one IV expression inserted during |
Evan Cheng | 2149577 | 2006-03-18 08:03:12 +0000 | [diff] [blame] | 90 | /// StrengthReduceStridedIVUsers. It contains the stride, the common base, as |
| 91 | /// well as the PHI node and increment value created for rewrite. |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 92 | struct VISIBILITY_HIDDEN IVExpr { |
Evan Cheng | 2149577 | 2006-03-18 08:03:12 +0000 | [diff] [blame] | 93 | SCEVHandle Stride; |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 94 | SCEVHandle Base; |
| 95 | PHINode *PHI; |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 96 | |
Dan Gohman | 9d10086 | 2009-03-09 22:04:01 +0000 | [diff] [blame] | 97 | IVExpr(const SCEVHandle &stride, const SCEVHandle &base, PHINode *phi) |
| 98 | : Stride(stride), Base(base), PHI(phi) {} |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | /// IVsOfOneStride - This structure keeps track of all IV expression inserted |
| 102 | /// during StrengthReduceStridedIVUsers for a particular stride of the IV. |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 103 | struct VISIBILITY_HIDDEN IVsOfOneStride { |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 104 | std::vector<IVExpr> IVs; |
| 105 | |
Dan Gohman | 9d10086 | 2009-03-09 22:04:01 +0000 | [diff] [blame] | 106 | void addIV(const SCEVHandle &Stride, const SCEVHandle &Base, PHINode *PHI) { |
| 107 | IVs.push_back(IVExpr(Stride, Base, PHI)); |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 108 | } |
| 109 | }; |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 110 | |
Devang Patel | 0f54dcb | 2007-03-06 21:14:09 +0000 | [diff] [blame] | 111 | class VISIBILITY_HIDDEN LoopStrengthReduce : public LoopPass { |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 112 | LoopInfo *LI; |
Devang Patel | b7d9dfc | 2007-06-07 21:42:15 +0000 | [diff] [blame] | 113 | DominatorTree *DT; |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 114 | ScalarEvolution *SE; |
| 115 | const TargetData *TD; |
| 116 | const Type *UIntPtrTy; |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 117 | bool Changed; |
Chris Lattner | 7e608bb | 2005-08-02 02:52:02 +0000 | [diff] [blame] | 118 | |
Nate Begeman | 1699748 | 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 | 50fad70 | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 121 | std::map<SCEVHandle, IVUsersOfOneStride> IVUsesByStride; |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 122 | |
Evan Cheng | d1d6b5c | 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 | 7305ae2 | 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. |
Evan Cheng | 8392772 | 2007-10-30 22:27:26 +0000 | [diff] [blame] | 130 | SmallVector<SCEVHandle, 16> StrideOrder; |
Chris Lattner | 7305ae2 | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 131 | |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 132 | /// DeadInsts - Keep track of instructions we may have made dead, so that |
| 133 | /// we can remove them after we are done working. |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 134 | SmallVector<Instruction*, 16> DeadInsts; |
Evan Cheng | d277f2c | 2006-03-13 23:14:23 +0000 | [diff] [blame] | 135 | |
| 136 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 137 | /// transformation profitability. |
| 138 | const TargetLowering *TLI; |
| 139 | |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 140 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 141 | static char ID; // Pass ID, replacement for typeid |
Dan Gohman | c2bbfc1 | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 142 | explicit LoopStrengthReduce(const TargetLowering *tli = NULL) : |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 143 | LoopPass(&ID), TLI(tli) { |
Jeff Cohen | 2f3c9b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Devang Patel | 0f54dcb | 2007-03-06 21:14:09 +0000 | [diff] [blame] | 146 | bool runOnLoop(Loop *L, LPPassManager &LPM); |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 147 | |
| 148 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | aa96ae7 | 2005-08-17 06:35:16 +0000 | [diff] [blame] | 149 | // We split critical edges, so we change the CFG. However, we do update |
| 150 | // many analyses if they are around. |
| 151 | AU.addPreservedID(LoopSimplifyID); |
| 152 | AU.addPreserved<LoopInfo>(); |
Chris Lattner | aa96ae7 | 2005-08-17 06:35:16 +0000 | [diff] [blame] | 153 | AU.addPreserved<DominanceFrontier>(); |
| 154 | AU.addPreserved<DominatorTree>(); |
| 155 | |
Jeff Cohen | f465db6 | 2005-02-27 19:37:07 +0000 | [diff] [blame] | 156 | AU.addRequiredID(LoopSimplifyID); |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 157 | AU.addRequired<LoopInfo>(); |
Devang Patel | b7d9dfc | 2007-06-07 21:42:15 +0000 | [diff] [blame] | 158 | AU.addRequired<DominatorTree>(); |
Jeff Cohen | 2f3c9b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 159 | AU.addRequired<TargetData>(); |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 160 | AU.addRequired<ScalarEvolution>(); |
Devang Patel | a0b3909 | 2008-08-26 17:57:54 +0000 | [diff] [blame] | 161 | AU.addPreserved<ScalarEvolution>(); |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 162 | } |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 49f72e6 | 2005-08-04 01:19:13 +0000 | [diff] [blame] | 164 | private: |
Chris Lattner | 3416e5f | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 165 | bool AddUsersIfInteresting(Instruction *I, Loop *L, |
Evan Cheng | 168a66b | 2007-10-26 23:08:19 +0000 | [diff] [blame] | 166 | SmallPtrSet<Instruction*,16> &Processed); |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 167 | ICmpInst *ChangeCompareStride(Loop *L, ICmpInst *Cond, |
| 168 | IVStrideUse* &CondUse, |
| 169 | const SCEVHandle* &CondStride); |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 170 | void OptimizeIndvars(Loop *L); |
Devang Patel | a0b3909 | 2008-08-26 17:57:54 +0000 | [diff] [blame] | 171 | |
| 172 | /// OptimizeShadowIV - If IV is used in a int-to-float cast |
| 173 | /// inside the loop then try to eliminate the cast opeation. |
| 174 | void OptimizeShadowIV(Loop *L); |
| 175 | |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 176 | /// OptimizeSMax - Rewrite the loop's terminating condition |
| 177 | /// if it uses an smax computation. |
| 178 | ICmpInst *OptimizeSMax(Loop *L, ICmpInst *Cond, |
| 179 | IVStrideUse* &CondUse); |
| 180 | |
Devang Patel | c677de2 | 2008-08-13 20:31:11 +0000 | [diff] [blame] | 181 | bool FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse, |
Devang Patel | a0b3909 | 2008-08-26 17:57:54 +0000 | [diff] [blame] | 182 | const SCEVHandle *&CondStride); |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 183 | bool RequiresTypeConversion(const Type *Ty, const Type *NewTy); |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 184 | SCEVHandle CheckForIVReuse(bool, bool, bool, const SCEVHandle&, |
Dan Gohman | 02e4fa7 | 2007-10-22 20:40:42 +0000 | [diff] [blame] | 185 | IVExpr&, const Type*, |
Dale Johannesen | dc42f48 | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 186 | const std::vector<BasedUser>& UsersToProcess); |
Dan Gohman | 02e4fa7 | 2007-10-22 20:40:42 +0000 | [diff] [blame] | 187 | bool ValidStride(bool, int64_t, |
| 188 | const std::vector<BasedUser>& UsersToProcess); |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 189 | SCEVHandle CollectIVUsers(const SCEVHandle &Stride, |
| 190 | IVUsersOfOneStride &Uses, |
| 191 | Loop *L, |
| 192 | bool &AllUsesAreAddresses, |
Dale Johannesen | b039062 | 2008-12-16 22:16:28 +0000 | [diff] [blame] | 193 | bool &AllUsesAreOutsideLoop, |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 194 | std::vector<BasedUser> &UsersToProcess); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 195 | bool ShouldUseFullStrengthReductionMode( |
| 196 | const std::vector<BasedUser> &UsersToProcess, |
| 197 | const Loop *L, |
| 198 | bool AllUsesAreAddresses, |
| 199 | SCEVHandle Stride); |
| 200 | void PrepareToStrengthReduceFully( |
| 201 | std::vector<BasedUser> &UsersToProcess, |
| 202 | SCEVHandle Stride, |
| 203 | SCEVHandle CommonExprs, |
| 204 | const Loop *L, |
| 205 | SCEVExpander &PreheaderRewriter); |
| 206 | void PrepareToStrengthReduceFromSmallerStride( |
| 207 | std::vector<BasedUser> &UsersToProcess, |
| 208 | Value *CommonBaseV, |
| 209 | const IVExpr &ReuseIV, |
| 210 | Instruction *PreInsertPt); |
| 211 | void PrepareToStrengthReduceWithNewPhi( |
| 212 | std::vector<BasedUser> &UsersToProcess, |
| 213 | SCEVHandle Stride, |
| 214 | SCEVHandle CommonExprs, |
| 215 | Value *CommonBaseV, |
| 216 | const Loop *L, |
| 217 | SCEVExpander &PreheaderRewriter); |
Chris Lattner | 50fad70 | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 218 | void StrengthReduceStridedIVUsers(const SCEVHandle &Stride, |
| 219 | IVUsersOfOneStride &Uses, |
Dan Gohman | 9f4ac31 | 2009-03-09 20:41:15 +0000 | [diff] [blame] | 220 | Loop *L); |
Chris Lattner | a68d4ca | 2008-12-01 06:14:28 +0000 | [diff] [blame] | 221 | void DeleteTriviallyDeadInstructions(); |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 222 | }; |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 225 | char LoopStrengthReduce::ID = 0; |
| 226 | static RegisterPass<LoopStrengthReduce> |
| 227 | X("loop-reduce", "Loop Strength Reduction"); |
| 228 | |
Daniel Dunbar | 394f044 | 2008-10-22 23:32:42 +0000 | [diff] [blame] | 229 | Pass *llvm::createLoopStrengthReducePass(const TargetLowering *TLI) { |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 230 | return new LoopStrengthReduce(TLI); |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /// DeleteTriviallyDeadInstructions - If any of the instructions is the |
| 234 | /// specified set are trivially dead, delete them and see if this makes any of |
| 235 | /// their operands subsequently dead. |
Chris Lattner | a68d4ca | 2008-12-01 06:14:28 +0000 | [diff] [blame] | 236 | void LoopStrengthReduce::DeleteTriviallyDeadInstructions() { |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 237 | if (DeadInsts.empty()) return; |
| 238 | |
| 239 | // Sort the deadinsts list so that we can trivially eliminate duplicates as we |
| 240 | // go. The code below never adds a non-dead instruction to the worklist, but |
| 241 | // callers may not be so careful. |
Chris Lattner | 99d0015 | 2008-12-01 06:49:59 +0000 | [diff] [blame] | 242 | array_pod_sort(DeadInsts.begin(), DeadInsts.end()); |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 243 | |
| 244 | // Drop duplicate instructions and those with uses. |
| 245 | for (unsigned i = 0, e = DeadInsts.size()-1; i < e; ++i) { |
| 246 | Instruction *I = DeadInsts[i]; |
| 247 | if (!I->use_empty()) DeadInsts[i] = 0; |
Chris Lattner | 46a879e | 2008-12-09 04:47:21 +0000 | [diff] [blame] | 248 | while (i != e && DeadInsts[i+1] == I) |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 249 | DeadInsts[++i] = 0; |
| 250 | } |
| 251 | |
Chris Lattner | a68d4ca | 2008-12-01 06:14:28 +0000 | [diff] [blame] | 252 | while (!DeadInsts.empty()) { |
| 253 | Instruction *I = DeadInsts.back(); |
| 254 | DeadInsts.pop_back(); |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 255 | |
| 256 | if (I == 0 || !isInstructionTriviallyDead(I)) |
Chris Lattner | bfcee36 | 2008-12-01 06:11:32 +0000 | [diff] [blame] | 257 | continue; |
| 258 | |
| 259 | SE->deleteValueFromRecords(I); |
| 260 | |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 261 | for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) { |
| 262 | if (Instruction *U = dyn_cast<Instruction>(*OI)) { |
| 263 | *OI = 0; |
Chris Lattner | bfcee36 | 2008-12-01 06:11:32 +0000 | [diff] [blame] | 264 | if (U->use_empty()) |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 265 | DeadInsts.push_back(U); |
Bill Wendling | 411052b | 2008-11-29 03:43:04 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
Chris Lattner | bfcee36 | 2008-12-01 06:11:32 +0000 | [diff] [blame] | 268 | |
| 269 | I->eraseFromParent(); |
| 270 | Changed = true; |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 274 | /// containsAddRecFromDifferentLoop - Determine whether expression S involves a |
| 275 | /// subexpression that is an AddRec from a loop other than L. An outer loop |
| 276 | /// of L is OK, but not an inner loop nor a disjoint loop. |
| 277 | static bool containsAddRecFromDifferentLoop(SCEVHandle S, Loop *L) { |
| 278 | // This is very common, put it first. |
| 279 | if (isa<SCEVConstant>(S)) |
| 280 | return false; |
| 281 | if (SCEVCommutativeExpr *AE = dyn_cast<SCEVCommutativeExpr>(S)) { |
| 282 | for (unsigned int i=0; i< AE->getNumOperands(); i++) |
| 283 | if (containsAddRecFromDifferentLoop(AE->getOperand(i), L)) |
| 284 | return true; |
| 285 | return false; |
| 286 | } |
| 287 | if (SCEVAddRecExpr *AE = dyn_cast<SCEVAddRecExpr>(S)) { |
| 288 | if (const Loop *newLoop = AE->getLoop()) { |
| 289 | if (newLoop == L) |
| 290 | return false; |
| 291 | // if newLoop is an outer loop of L, this is OK. |
| 292 | if (!LoopInfoBase<BasicBlock>::isNotAlreadyContainedIn(L, newLoop)) |
| 293 | return false; |
| 294 | } |
| 295 | return true; |
| 296 | } |
| 297 | if (SCEVUDivExpr *DE = dyn_cast<SCEVUDivExpr>(S)) |
| 298 | return containsAddRecFromDifferentLoop(DE->getLHS(), L) || |
| 299 | containsAddRecFromDifferentLoop(DE->getRHS(), L); |
| 300 | #if 0 |
| 301 | // SCEVSDivExpr has been backed out temporarily, but will be back; we'll |
| 302 | // need this when it is. |
| 303 | if (SCEVSDivExpr *DE = dyn_cast<SCEVSDivExpr>(S)) |
| 304 | return containsAddRecFromDifferentLoop(DE->getLHS(), L) || |
| 305 | containsAddRecFromDifferentLoop(DE->getRHS(), L); |
| 306 | #endif |
| 307 | if (SCEVTruncateExpr *TE = dyn_cast<SCEVTruncateExpr>(S)) |
| 308 | return containsAddRecFromDifferentLoop(TE->getOperand(), L); |
| 309 | if (SCEVZeroExtendExpr *ZE = dyn_cast<SCEVZeroExtendExpr>(S)) |
| 310 | return containsAddRecFromDifferentLoop(ZE->getOperand(), L); |
| 311 | if (SCEVSignExtendExpr *SE = dyn_cast<SCEVSignExtendExpr>(S)) |
| 312 | return containsAddRecFromDifferentLoop(SE->getOperand(), L); |
| 313 | return false; |
| 314 | } |
| 315 | |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 316 | /// getSCEVStartAndStride - Compute the start and stride of this expression, |
| 317 | /// returning false if the expression is not a start/stride pair, or true if it |
| 318 | /// is. The stride must be a loop invariant expression, but the start may be |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 319 | /// a mix of loop invariant and loop variant expressions. The start cannot, |
| 320 | /// however, contain an AddRec from a different loop, unless that loop is an |
| 321 | /// outer loop of the current loop. |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 322 | static bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 323 | SCEVHandle &Start, SCEVHandle &Stride, |
Evan Cheng | 8f40afe | 2009-02-15 06:06:15 +0000 | [diff] [blame] | 324 | ScalarEvolution *SE, DominatorTree *DT) { |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 325 | SCEVHandle TheAddRec = Start; // Initialize to zero. |
| 326 | |
| 327 | // If the outer level is an AddExpr, the operands are all start values except |
| 328 | // for a nested AddRecExpr. |
| 329 | if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) { |
| 330 | for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i) |
| 331 | if (SCEVAddRecExpr *AddRec = |
| 332 | dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) { |
| 333 | if (AddRec->getLoop() == L) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 334 | TheAddRec = SE->getAddExpr(AddRec, TheAddRec); |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 335 | else |
| 336 | return false; // Nested IV of some sort? |
| 337 | } else { |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 338 | Start = SE->getAddExpr(Start, AE->getOperand(i)); |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 341 | } else if (isa<SCEVAddRecExpr>(SH)) { |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 342 | TheAddRec = SH; |
| 343 | } else { |
| 344 | return false; // not analyzable. |
| 345 | } |
| 346 | |
| 347 | SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec); |
| 348 | if (!AddRec || AddRec->getLoop() != L) return false; |
| 349 | |
| 350 | // FIXME: Generalize to non-affine IV's. |
| 351 | if (!AddRec->isAffine()) return false; |
| 352 | |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 353 | // If Start contains an SCEVAddRecExpr from a different loop, other than an |
Dale Johannesen | 1de17d5 | 2009-02-09 22:14:15 +0000 | [diff] [blame] | 354 | // outer loop of the current loop, reject it. SCEV has no concept of |
Dan Gohman | fd03399 | 2009-03-04 20:50:23 +0000 | [diff] [blame] | 355 | // operating on more than one loop at a time so don't confuse it with such |
| 356 | // expressions. |
Dan Gohman | 9194e8b | 2009-02-13 03:58:31 +0000 | [diff] [blame] | 357 | if (containsAddRecFromDifferentLoop(AddRec->getOperand(0), L)) |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 358 | return false; |
| 359 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 360 | Start = SE->getAddExpr(Start, AddRec->getOperand(0)); |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 361 | |
Evan Cheng | 8f40afe | 2009-02-15 06:06:15 +0000 | [diff] [blame] | 362 | if (!isa<SCEVConstant>(AddRec->getOperand(1))) { |
Evan Cheng | 5a6c1a8 | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 363 | // If stride is an instruction, make sure it dominates the loop preheader. |
Evan Cheng | 8f40afe | 2009-02-15 06:06:15 +0000 | [diff] [blame] | 364 | // Otherwise we could end up with a use before def situation. |
Evan Cheng | 5a6c1a8 | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 365 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 366 | if (!AddRec->getOperand(1)->dominates(Preheader, DT)) |
| 367 | return false; |
Evan Cheng | 8f40afe | 2009-02-15 06:06:15 +0000 | [diff] [blame] | 368 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 369 | DOUT << "[" << L->getHeader()->getName() |
| 370 | << "] Variable stride: " << *AddRec << "\n"; |
Evan Cheng | 8f40afe | 2009-02-15 06:06:15 +0000 | [diff] [blame] | 371 | } |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 372 | |
Chris Lattner | 50fad70 | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 373 | Stride = AddRec->getOperand(1); |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 374 | return true; |
| 375 | } |
| 376 | |
Chris Lattner | 0ae33eb | 2005-10-03 01:04:44 +0000 | [diff] [blame] | 377 | /// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression |
| 378 | /// and now we need to decide whether the user should use the preinc or post-inc |
| 379 | /// value. If this user should use the post-inc version of the IV, return true. |
| 380 | /// |
| 381 | /// Choosing wrong here can break dominance properties (if we choose to use the |
| 382 | /// post-inc value when we cannot) or it can end up adding extra live-ranges to |
| 383 | /// the loop, resulting in reg-reg copies (if we use the pre-inc value when we |
| 384 | /// should use the post-inc value). |
| 385 | static bool IVUseShouldUsePostIncValue(Instruction *User, Instruction *IV, |
Evan Cheng | 0e0014d | 2007-10-30 23:45:15 +0000 | [diff] [blame] | 386 | Loop *L, DominatorTree *DT, Pass *P, |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 387 | SmallVectorImpl<Instruction*> &DeadInsts){ |
Chris Lattner | 0ae33eb | 2005-10-03 01:04:44 +0000 | [diff] [blame] | 388 | // If the user is in the loop, use the preinc value. |
| 389 | if (L->contains(User->getParent())) return false; |
| 390 | |
Chris Lattner | 5e8ca66 | 2005-10-03 02:50:05 +0000 | [diff] [blame] | 391 | BasicBlock *LatchBlock = L->getLoopLatch(); |
| 392 | |
| 393 | // Ok, the user is outside of the loop. If it is dominated by the latch |
| 394 | // block, use the post-inc value. |
Devang Patel | b7d9dfc | 2007-06-07 21:42:15 +0000 | [diff] [blame] | 395 | if (DT->dominates(LatchBlock, User->getParent())) |
Chris Lattner | 5e8ca66 | 2005-10-03 02:50:05 +0000 | [diff] [blame] | 396 | return true; |
| 397 | |
| 398 | // There is one case we have to be careful of: PHI nodes. These little guys |
| 399 | // can live in blocks that do not dominate the latch block, but (since their |
| 400 | // uses occur in the predecessor block, not the block the PHI lives in) should |
| 401 | // still use the post-inc value. Check for this case now. |
| 402 | PHINode *PN = dyn_cast<PHINode>(User); |
| 403 | if (!PN) return false; // not a phi, not dominated by latch block. |
| 404 | |
| 405 | // Look at all of the uses of IV by the PHI node. If any use corresponds to |
| 406 | // a block that is not dominated by the latch block, give up and use the |
| 407 | // preincremented value. |
| 408 | unsigned NumUses = 0; |
| 409 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 410 | if (PN->getIncomingValue(i) == IV) { |
| 411 | ++NumUses; |
Devang Patel | b7d9dfc | 2007-06-07 21:42:15 +0000 | [diff] [blame] | 412 | if (!DT->dominates(LatchBlock, PN->getIncomingBlock(i))) |
Chris Lattner | 5e8ca66 | 2005-10-03 02:50:05 +0000 | [diff] [blame] | 413 | return false; |
| 414 | } |
| 415 | |
| 416 | // Okay, all uses of IV by PN are in predecessor blocks that really are |
| 417 | // dominated by the latch block. Split the critical edges and use the |
| 418 | // post-incremented value. |
| 419 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 420 | if (PN->getIncomingValue(i) == IV) { |
Evan Cheng | 8392772 | 2007-10-30 22:27:26 +0000 | [diff] [blame] | 421 | SplitCriticalEdge(PN->getIncomingBlock(i), PN->getParent(), P, false); |
Chris Lattner | 1b9c8e7 | 2006-10-28 00:59:20 +0000 | [diff] [blame] | 422 | // Splitting the critical edge can reduce the number of entries in this |
| 423 | // PHI. |
| 424 | e = PN->getNumIncomingValues(); |
Chris Lattner | 5e8ca66 | 2005-10-03 02:50:05 +0000 | [diff] [blame] | 425 | if (--NumUses == 0) break; |
| 426 | } |
Evan Cheng | 0e0014d | 2007-10-30 23:45:15 +0000 | [diff] [blame] | 427 | |
| 428 | // PHI node might have become a constant value after SplitCriticalEdge. |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 429 | DeadInsts.push_back(User); |
Chris Lattner | 5e8ca66 | 2005-10-03 02:50:05 +0000 | [diff] [blame] | 430 | |
| 431 | return true; |
Chris Lattner | 0ae33eb | 2005-10-03 01:04:44 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Dan Gohman | f284ce2 | 2009-02-18 00:08:39 +0000 | [diff] [blame] | 434 | /// isAddressUse - Returns true if the specified instruction is using the |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 435 | /// specified value as an address. |
| 436 | static bool isAddressUse(Instruction *Inst, Value *OperandVal) { |
| 437 | bool isAddress = isa<LoadInst>(Inst); |
| 438 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 439 | if (SI->getOperand(1) == OperandVal) |
| 440 | isAddress = true; |
| 441 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
| 442 | // Addressing modes can also be folded into prefetches and a variety |
| 443 | // of intrinsics. |
| 444 | switch (II->getIntrinsicID()) { |
| 445 | default: break; |
| 446 | case Intrinsic::prefetch: |
| 447 | case Intrinsic::x86_sse2_loadu_dq: |
| 448 | case Intrinsic::x86_sse2_loadu_pd: |
| 449 | case Intrinsic::x86_sse_loadu_ps: |
| 450 | case Intrinsic::x86_sse_storeu_ps: |
| 451 | case Intrinsic::x86_sse2_storeu_pd: |
| 452 | case Intrinsic::x86_sse2_storeu_dq: |
| 453 | case Intrinsic::x86_sse2_storel_dq: |
| 454 | if (II->getOperand(1) == OperandVal) |
| 455 | isAddress = true; |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | return isAddress; |
| 460 | } |
Chris Lattner | 0ae33eb | 2005-10-03 01:04:44 +0000 | [diff] [blame] | 461 | |
Dan Gohman | 21e7722 | 2009-03-09 21:01:17 +0000 | [diff] [blame] | 462 | /// getAccessType - Return the type of the memory being accessed. |
| 463 | static const Type *getAccessType(const Instruction *Inst) { |
| 464 | const Type *UseTy = Inst->getType(); |
| 465 | if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) |
| 466 | UseTy = SI->getOperand(0)->getType(); |
| 467 | else if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
| 468 | // Addressing modes can also be folded into prefetches and a variety |
| 469 | // of intrinsics. |
| 470 | switch (II->getIntrinsicID()) { |
| 471 | default: break; |
| 472 | case Intrinsic::x86_sse_storeu_ps: |
| 473 | case Intrinsic::x86_sse2_storeu_pd: |
| 474 | case Intrinsic::x86_sse2_storeu_dq: |
| 475 | case Intrinsic::x86_sse2_storel_dq: |
| 476 | UseTy = II->getOperand(1)->getType(); |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | return UseTy; |
| 481 | } |
| 482 | |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 483 | /// AddUsersIfInteresting - Inspect the specified instruction. If it is a |
| 484 | /// reducible SCEV, recursively add its users to the IVUsesByStride set and |
| 485 | /// return true. Otherwise, return false. |
Chris Lattner | 3416e5f | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 486 | bool LoopStrengthReduce::AddUsersIfInteresting(Instruction *I, Loop *L, |
Evan Cheng | 168a66b | 2007-10-26 23:08:19 +0000 | [diff] [blame] | 487 | SmallPtrSet<Instruction*,16> &Processed) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 488 | if (!I->getType()->isInteger() && !isa<PointerType>(I->getType())) |
Dan Gohman | 4a9a3e5 | 2008-04-14 18:26:16 +0000 | [diff] [blame] | 489 | return false; // Void and FP expressions cannot be reduced. |
Chris Lattner | b7e64ac | 2009-03-17 23:58:30 +0000 | [diff] [blame] | 490 | |
| 491 | // LSR is not APInt clean, do not touch integers bigger than 64-bits. |
Dan Gohman | 65e05b6 | 2009-04-16 16:49:48 +0000 | [diff] [blame] | 492 | if (TD->getTypeSizeInBits(I->getType()) > 64) |
Chris Lattner | b7e64ac | 2009-03-17 23:58:30 +0000 | [diff] [blame] | 493 | return false; |
| 494 | |
Evan Cheng | 168a66b | 2007-10-26 23:08:19 +0000 | [diff] [blame] | 495 | if (!Processed.insert(I)) |
Chris Lattner | 3416e5f | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 496 | return true; // Instruction already handled. |
| 497 | |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 498 | // Get the symbolic expression for this instruction. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 499 | SCEVHandle ISE = SE->getSCEV(I); |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 500 | if (isa<SCEVCouldNotCompute>(ISE)) return false; |
Chris Lattner | 3416e5f | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 501 | |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 502 | // Get the start and stride for this expression. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 503 | SCEVHandle Start = SE->getIntegerSCEV(0, ISE->getType()); |
Chris Lattner | 50fad70 | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 504 | SCEVHandle Stride = Start; |
Evan Cheng | 8f40afe | 2009-02-15 06:06:15 +0000 | [diff] [blame] | 505 | if (!getSCEVStartAndStride(ISE, L, Start, Stride, SE, DT)) |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 506 | return false; // Non-reducible symbolic expression, bail out. |
Devang Patel | 4fe2658 | 2007-03-09 21:19:53 +0000 | [diff] [blame] | 507 | |
Devang Patel | 2a5fa18 | 2007-04-23 22:42:03 +0000 | [diff] [blame] | 508 | std::vector<Instruction *> IUsers; |
| 509 | // Collect all I uses now because IVUseShouldUsePostIncValue may |
| 510 | // invalidate use_iterator. |
| 511 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI) |
| 512 | IUsers.push_back(cast<Instruction>(*UI)); |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 513 | |
Devang Patel | 2a5fa18 | 2007-04-23 22:42:03 +0000 | [diff] [blame] | 514 | for (unsigned iused_index = 0, iused_size = IUsers.size(); |
| 515 | iused_index != iused_size; ++iused_index) { |
| 516 | |
| 517 | Instruction *User = IUsers[iused_index]; |
Devang Patel | 4fe2658 | 2007-03-09 21:19:53 +0000 | [diff] [blame] | 518 | |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 519 | // Do not infinitely recurse on PHI nodes. |
Chris Lattner | 396b2ba | 2005-09-13 02:09:55 +0000 | [diff] [blame] | 520 | if (isa<PHINode>(User) && Processed.count(User)) |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 521 | continue; |
| 522 | |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 523 | // Descend recursively, but not into PHI nodes outside the current loop. |
| 524 | // It's important to see the entire expression outside the loop to get |
| 525 | // choices that depend on addressing mode use right, although we won't |
| 526 | // consider references ouside the loop in all cases. |
| 527 | // If User is already in Processed, we don't want to recurse into it again, |
| 528 | // but do want to record a second reference in the same instruction. |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 529 | bool AddUserToIVUsers = false; |
Chris Lattner | f918659 | 2005-08-04 00:14:11 +0000 | [diff] [blame] | 530 | if (LI->getLoopFor(User->getParent()) != L) { |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 531 | if (isa<PHINode>(User) || Processed.count(User) || |
| 532 | !AddUsersIfInteresting(User, L, Processed)) { |
| 533 | DOUT << "FOUND USER in other loop: " << *User |
| 534 | << " OF SCEV: " << *ISE << "\n"; |
| 535 | AddUserToIVUsers = true; |
| 536 | } |
| 537 | } else if (Processed.count(User) || |
| 538 | !AddUsersIfInteresting(User, L, Processed)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 539 | DOUT << "FOUND USER: " << *User |
| 540 | << " OF SCEV: " << *ISE << "\n"; |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 541 | AddUserToIVUsers = true; |
| 542 | } |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 544 | if (AddUserToIVUsers) { |
Chris Lattner | 7305ae2 | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 545 | IVUsersOfOneStride &StrideUses = IVUsesByStride[Stride]; |
Dale Johannesen | b039062 | 2008-12-16 22:16:28 +0000 | [diff] [blame] | 546 | if (StrideUses.Users.empty()) // First occurrence of this stride? |
Chris Lattner | 7305ae2 | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 547 | StrideOrder.push_back(Stride); |
| 548 | |
Chris Lattner | a4479ad | 2005-08-04 00:40:47 +0000 | [diff] [blame] | 549 | // Okay, we found a user that we cannot reduce. Analyze the instruction |
Chris Lattner | c6bae65 | 2005-09-12 06:04:47 +0000 | [diff] [blame] | 550 | // and decide what to do with it. If we are a use inside of the loop, use |
| 551 | // the value before incrementation, otherwise use it after incrementation. |
Evan Cheng | 0e0014d | 2007-10-30 23:45:15 +0000 | [diff] [blame] | 552 | if (IVUseShouldUsePostIncValue(User, I, L, DT, this, DeadInsts)) { |
Chris Lattner | c6bae65 | 2005-09-12 06:04:47 +0000 | [diff] [blame] | 553 | // The value used will be incremented by the stride more than we are |
| 554 | // expecting, so subtract this off. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 555 | SCEVHandle NewStart = SE->getMinusSCEV(Start, Stride); |
Chris Lattner | 7305ae2 | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 556 | StrideUses.addUser(NewStart, User, I); |
| 557 | StrideUses.Users.back().isUseOfPostIncrementedValue = true; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 558 | DOUT << " USING POSTINC SCEV, START=" << *NewStart<< "\n"; |
Chris Lattner | 0ae33eb | 2005-10-03 01:04:44 +0000 | [diff] [blame] | 559 | } else { |
Chris Lattner | 7305ae2 | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 560 | StrideUses.addUser(Start, User, I); |
Chris Lattner | c6bae65 | 2005-09-12 06:04:47 +0000 | [diff] [blame] | 561 | } |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | return true; |
| 565 | } |
| 566 | |
| 567 | namespace { |
| 568 | /// BasedUser - For a particular base value, keep information about how we've |
| 569 | /// partitioned the expression so far. |
| 570 | struct BasedUser { |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 571 | /// SE - The current ScalarEvolution object. |
| 572 | ScalarEvolution *SE; |
| 573 | |
Chris Lattner | a553b0c | 2005-08-08 22:56:21 +0000 | [diff] [blame] | 574 | /// Base - The Base value for the PHI node that needs to be inserted for |
| 575 | /// this use. As the use is processed, information gets moved from this |
| 576 | /// field to the Imm field (below). BasedUser values are sorted by this |
| 577 | /// field. |
| 578 | SCEVHandle Base; |
| 579 | |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 580 | /// Inst - The instruction using the induction variable. |
| 581 | Instruction *Inst; |
| 582 | |
Chris Lattner | ec3fb63 | 2005-08-03 22:21:05 +0000 | [diff] [blame] | 583 | /// OperandValToReplace - The operand value of Inst to replace with the |
| 584 | /// EmittedBase. |
| 585 | Value *OperandValToReplace; |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 586 | |
| 587 | /// Imm - The immediate value that should be added to the base immediately |
| 588 | /// before Inst, because it will be folded into the imm field of the |
Dan Gohman | 33e3a36 | 2009-02-20 20:29:04 +0000 | [diff] [blame] | 589 | /// instruction. This is also sometimes used for loop-variant values that |
| 590 | /// must be added inside the loop. |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 591 | SCEVHandle Imm; |
| 592 | |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 593 | /// Phi - The induction variable that performs the striding that |
| 594 | /// should be used for this user. |
Dan Gohman | 9d10086 | 2009-03-09 22:04:01 +0000 | [diff] [blame] | 595 | PHINode *Phi; |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 597 | // isUseOfPostIncrementedValue - True if this should use the |
| 598 | // post-incremented version of this IV, not the preincremented version. |
| 599 | // This can only be set in special cases, such as the terminating setcc |
Chris Lattner | c6bae65 | 2005-09-12 06:04:47 +0000 | [diff] [blame] | 600 | // instruction for a loop and uses outside the loop that are dominated by |
| 601 | // the loop. |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 602 | bool isUseOfPostIncrementedValue; |
Chris Lattner | a553b0c | 2005-08-08 22:56:21 +0000 | [diff] [blame] | 603 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 604 | BasedUser(IVStrideUse &IVSU, ScalarEvolution *se) |
| 605 | : SE(se), Base(IVSU.Offset), Inst(IVSU.User), |
Chris Lattner | a553b0c | 2005-08-08 22:56:21 +0000 | [diff] [blame] | 606 | OperandValToReplace(IVSU.OperandValToReplace), |
Dale Johannesen | 308f24d | 2008-12-03 22:43:56 +0000 | [diff] [blame] | 607 | Imm(SE->getIntegerSCEV(0, Base->getType())), |
Chris Lattner | a553b0c | 2005-08-08 22:56:21 +0000 | [diff] [blame] | 608 | isUseOfPostIncrementedValue(IVSU.isUseOfPostIncrementedValue) {} |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 609 | |
Chris Lattner | 2114b27 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 610 | // Once we rewrite the code to insert the new IVs we want, update the |
| 611 | // operands of Inst to use the new expression 'NewBase', with 'Imm' added |
| 612 | // to it. |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 613 | void RewriteInstructionToUseNewBase(const SCEVHandle &NewBase, |
Dan Gohman | f20d70d | 2008-05-15 23:26:57 +0000 | [diff] [blame] | 614 | Instruction *InsertPt, |
Evan Cheng | 0e0014d | 2007-10-30 23:45:15 +0000 | [diff] [blame] | 615 | SCEVExpander &Rewriter, Loop *L, Pass *P, |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 616 | SmallVectorImpl<Instruction*> &DeadInsts); |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 617 | |
| 618 | Value *InsertCodeForBaseAtPosition(const SCEVHandle &NewBase, |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 619 | const Type *Ty, |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 620 | SCEVExpander &Rewriter, |
| 621 | Instruction *IP, Loop *L); |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 622 | void dump() const; |
| 623 | }; |
| 624 | } |
| 625 | |
| 626 | void BasedUser::dump() const { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 627 | cerr << " Base=" << *Base; |
| 628 | cerr << " Imm=" << *Imm; |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 629 | cerr << " Inst: " << *Inst; |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 632 | Value *BasedUser::InsertCodeForBaseAtPosition(const SCEVHandle &NewBase, |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 633 | const Type *Ty, |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 634 | SCEVExpander &Rewriter, |
| 635 | Instruction *IP, Loop *L) { |
| 636 | // Figure out where we *really* want to insert this code. In particular, if |
| 637 | // the user is inside of a loop that is nested inside of L, we really don't |
| 638 | // want to insert this expression before the user, we'd rather pull it out as |
| 639 | // many loops as possible. |
| 640 | LoopInfo &LI = Rewriter.getLoopInfo(); |
| 641 | Instruction *BaseInsertPt = IP; |
| 642 | |
| 643 | // Figure out the most-nested loop that IP is in. |
| 644 | Loop *InsertLoop = LI.getLoopFor(IP->getParent()); |
| 645 | |
| 646 | // If InsertLoop is not L, and InsertLoop is nested inside of L, figure out |
| 647 | // the preheader of the outer-most loop where NewBase is not loop invariant. |
Dale Johannesen | eccdd08 | 2008-12-02 18:40:09 +0000 | [diff] [blame] | 648 | if (L->contains(IP->getParent())) |
| 649 | while (InsertLoop && NewBase->isLoopInvariant(InsertLoop)) { |
| 650 | BaseInsertPt = InsertLoop->getLoopPreheader()->getTerminator(); |
| 651 | InsertLoop = InsertLoop->getParentLoop(); |
| 652 | } |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 653 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 654 | Value *Base = Rewriter.expandCodeFor(NewBase, Ty, BaseInsertPt); |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 656 | // If there is no immediate value, skip the next part. |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 657 | if (Imm->isZero()) |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 658 | return Base; |
Chris Lattner | b47f612 | 2007-06-06 01:23:55 +0000 | [diff] [blame] | 659 | |
| 660 | // If we are inserting the base and imm values in the same block, make sure to |
| 661 | // adjust the IP position if insertion reused a result. |
| 662 | if (IP == BaseInsertPt) |
| 663 | IP = Rewriter.getInsertionPoint(); |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 664 | |
| 665 | // Always emit the immediate (if non-zero) into the same block as the user. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 666 | SCEVHandle NewValSCEV = SE->getAddExpr(SE->getUnknown(Base), Imm); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 667 | return Rewriter.expandCodeFor(NewValSCEV, Ty, IP); |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | |
Chris Lattner | 2114b27 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 671 | // Once we rewrite the code to insert the new IVs we want, update the |
| 672 | // operands of Inst to use the new expression 'NewBase', with 'Imm' added |
Dan Gohman | f20d70d | 2008-05-15 23:26:57 +0000 | [diff] [blame] | 673 | // to it. NewBasePt is the last instruction which contributes to the |
| 674 | // value of NewBase in the case that it's a diffferent instruction from |
| 675 | // the PHI that NewBase is computed from, or null otherwise. |
| 676 | // |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 677 | void BasedUser::RewriteInstructionToUseNewBase(const SCEVHandle &NewBase, |
Dan Gohman | f20d70d | 2008-05-15 23:26:57 +0000 | [diff] [blame] | 678 | Instruction *NewBasePt, |
Evan Cheng | 0e0014d | 2007-10-30 23:45:15 +0000 | [diff] [blame] | 679 | SCEVExpander &Rewriter, Loop *L, Pass *P, |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 680 | SmallVectorImpl<Instruction*> &DeadInsts){ |
Chris Lattner | 2114b27 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 681 | if (!isa<PHINode>(Inst)) { |
Chris Lattner | c5494af | 2007-04-13 20:42:26 +0000 | [diff] [blame] | 682 | // By default, insert code at the user instruction. |
| 683 | BasicBlock::iterator InsertPt = Inst; |
| 684 | |
| 685 | // However, if the Operand is itself an instruction, the (potentially |
| 686 | // complex) inserted code may be shared by many users. Because of this, we |
| 687 | // want to emit code for the computation of the operand right before its old |
| 688 | // computation. This is usually safe, because we obviously used to use the |
| 689 | // computation when it was computed in its current block. However, in some |
| 690 | // cases (e.g. use of a post-incremented induction variable) the NewBase |
| 691 | // value will be pinned to live somewhere after the original computation. |
| 692 | // In this case, we have to back off. |
Chris Lattner | f8828eb | 2008-12-02 04:52:26 +0000 | [diff] [blame] | 693 | // |
| 694 | // If this is a use outside the loop (which means after, since it is based |
| 695 | // on a loop indvar) we use the post-incremented value, so that we don't |
| 696 | // artificially make the preinc value live out the bottom of the loop. |
Dale Johannesen | 589bf08 | 2008-12-01 22:00:01 +0000 | [diff] [blame] | 697 | if (!isUseOfPostIncrementedValue && L->contains(Inst->getParent())) { |
Dan Gohman | ca756ae | 2008-05-20 03:01:48 +0000 | [diff] [blame] | 698 | if (NewBasePt && isa<PHINode>(OperandValToReplace)) { |
Dan Gohman | f20d70d | 2008-05-15 23:26:57 +0000 | [diff] [blame] | 699 | InsertPt = NewBasePt; |
| 700 | ++InsertPt; |
Gabor Greif | 6725cb5 | 2008-06-11 21:38:51 +0000 | [diff] [blame] | 701 | } else if (Instruction *OpInst |
| 702 | = dyn_cast<Instruction>(OperandValToReplace)) { |
Chris Lattner | c5494af | 2007-04-13 20:42:26 +0000 | [diff] [blame] | 703 | InsertPt = OpInst; |
| 704 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 705 | } |
| 706 | } |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 707 | Value *NewVal = InsertCodeForBaseAtPosition(NewBase, |
| 708 | OperandValToReplace->getType(), |
| 709 | Rewriter, InsertPt, L); |
Chris Lattner | 2114b27 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 710 | // Replace the use of the operand Value with the new Phi we just created. |
| 711 | Inst->replaceUsesOfWith(OperandValToReplace, NewVal); |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 712 | |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 713 | DOUT << " Replacing with "; |
Dan Gohman | 4a359ea | 2009-02-19 19:32:06 +0000 | [diff] [blame] | 714 | DEBUG(WriteAsOperand(*DOUT, NewVal, /*PrintType=*/false)); |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 715 | DOUT << ", which has value " << *NewBase << " plus IMM " << *Imm << "\n"; |
Chris Lattner | 2114b27 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 716 | return; |
| 717 | } |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 718 | |
Chris Lattner | 2114b27 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 719 | // PHI nodes are more complex. We have to insert one copy of the NewBase+Imm |
Chris Lattner | c41e345 | 2005-08-10 00:35:32 +0000 | [diff] [blame] | 720 | // expression into each operand block that uses it. Note that PHI nodes can |
| 721 | // have multiple entries for the same predecessor. We use a map to make sure |
| 722 | // that a PHI node only has a single Value* for each predecessor (which also |
| 723 | // prevents us from inserting duplicate code in some blocks). |
Evan Cheng | 8392772 | 2007-10-30 22:27:26 +0000 | [diff] [blame] | 724 | DenseMap<BasicBlock*, Value*> InsertedCode; |
Chris Lattner | 2114b27 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 725 | PHINode *PN = cast<PHINode>(Inst); |
| 726 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 727 | if (PN->getIncomingValue(i) == OperandValToReplace) { |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 728 | // If the original expression is outside the loop, put the replacement |
| 729 | // code in the same place as the original expression, |
| 730 | // which need not be an immediate predecessor of this PHI. This way we |
| 731 | // need only one copy of it even if it is referenced multiple times in |
| 732 | // the PHI. We don't do this when the original expression is inside the |
Dale Johannesen | 1de17d5 | 2009-02-09 22:14:15 +0000 | [diff] [blame] | 733 | // loop because multiple copies sometimes do useful sinking of code in |
| 734 | // that case(?). |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 735 | Instruction *OldLoc = dyn_cast<Instruction>(OperandValToReplace); |
| 736 | if (L->contains(OldLoc->getParent())) { |
Dale Johannesen | 1de17d5 | 2009-02-09 22:14:15 +0000 | [diff] [blame] | 737 | // If this is a critical edge, split the edge so that we do not insert |
| 738 | // the code on all predecessor/successor paths. We do this unless this |
| 739 | // is the canonical backedge for this loop, as this can make some |
| 740 | // inserted code be in an illegal position. |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 741 | BasicBlock *PHIPred = PN->getIncomingBlock(i); |
| 742 | if (e != 1 && PHIPred->getTerminator()->getNumSuccessors() > 1 && |
| 743 | (PN->getParent() != L->getHeader() || !L->contains(PHIPred))) { |
Dale Johannesen | f6727b0 | 2008-12-23 23:21:35 +0000 | [diff] [blame] | 744 | |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 745 | // First step, split the critical edge. |
| 746 | SplitCriticalEdge(PHIPred, PN->getParent(), P, false); |
| 747 | |
| 748 | // Next step: move the basic block. In particular, if the PHI node |
| 749 | // is outside of the loop, and PredTI is in the loop, we want to |
| 750 | // move the block to be immediately before the PHI block, not |
| 751 | // immediately after PredTI. |
| 752 | if (L->contains(PHIPred) && !L->contains(PN->getParent())) { |
| 753 | BasicBlock *NewBB = PN->getIncomingBlock(i); |
| 754 | NewBB->moveBefore(PN->getParent()); |
| 755 | } |
| 756 | |
| 757 | // Splitting the edge can reduce the number of PHI entries we have. |
| 758 | e = PN->getNumIncomingValues(); |
| 759 | } |
| 760 | } |
Chris Lattner | c41e345 | 2005-08-10 00:35:32 +0000 | [diff] [blame] | 761 | Value *&Code = InsertedCode[PN->getIncomingBlock(i)]; |
| 762 | if (!Code) { |
| 763 | // Insert the code into the end of the predecessor block. |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 764 | Instruction *InsertPt = (L->contains(OldLoc->getParent())) ? |
| 765 | PN->getIncomingBlock(i)->getTerminator() : |
| 766 | OldLoc->getParent()->getTerminator(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 767 | Code = InsertCodeForBaseAtPosition(NewBase, PN->getType(), |
| 768 | Rewriter, InsertPt, L); |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 769 | |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 770 | DOUT << " Changing PHI use to "; |
Dan Gohman | 4a359ea | 2009-02-19 19:32:06 +0000 | [diff] [blame] | 771 | DEBUG(WriteAsOperand(*DOUT, Code, /*PrintType=*/false)); |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 772 | DOUT << ", which has value " << *NewBase << " plus IMM " << *Imm << "\n"; |
Chris Lattner | c41e345 | 2005-08-10 00:35:32 +0000 | [diff] [blame] | 773 | } |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 774 | |
Chris Lattner | 2114b27 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 775 | // Replace the use of the operand Value with the new Phi we just created. |
Chris Lattner | c41e345 | 2005-08-10 00:35:32 +0000 | [diff] [blame] | 776 | PN->setIncomingValue(i, Code); |
Chris Lattner | 2114b27 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 777 | Rewriter.clear(); |
| 778 | } |
| 779 | } |
Evan Cheng | 0e0014d | 2007-10-30 23:45:15 +0000 | [diff] [blame] | 780 | |
| 781 | // PHI node might have become a constant value after SplitCriticalEdge. |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 782 | DeadInsts.push_back(Inst); |
Chris Lattner | 2114b27 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 786 | /// fitsInAddressMode - Return true if V can be subsumed within an addressing |
| 787 | /// mode, and does not need to be put in a register first. |
| 788 | static bool fitsInAddressMode(const SCEVHandle &V, const Type *UseTy, |
| 789 | const TargetLowering *TLI, bool HasBaseReg) { |
Chris Lattner | 3821e47 | 2005-08-08 06:25:50 +0000 | [diff] [blame] | 790 | if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) { |
Evan Cheng | 5eef2d2 | 2007-03-12 23:27:37 +0000 | [diff] [blame] | 791 | int64_t VC = SC->getValue()->getSExtValue(); |
Chris Lattner | 579633c | 2007-04-09 22:20:14 +0000 | [diff] [blame] | 792 | if (TLI) { |
| 793 | TargetLowering::AddrMode AM; |
| 794 | AM.BaseOffs = VC; |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 795 | AM.HasBaseReg = HasBaseReg; |
Chris Lattner | 579633c | 2007-04-09 22:20:14 +0000 | [diff] [blame] | 796 | return TLI->isLegalAddressingMode(AM, UseTy); |
| 797 | } else { |
Evan Cheng | d277f2c | 2006-03-13 23:14:23 +0000 | [diff] [blame] | 798 | // Defaults to PPC. PPC allows a sign-extended 16-bit immediate field. |
Evan Cheng | 5eef2d2 | 2007-03-12 23:27:37 +0000 | [diff] [blame] | 799 | return (VC > -(1 << 16) && VC < (1 << 16)-1); |
Chris Lattner | 579633c | 2007-04-09 22:20:14 +0000 | [diff] [blame] | 800 | } |
Chris Lattner | 3821e47 | 2005-08-08 06:25:50 +0000 | [diff] [blame] | 801 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 802 | |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 803 | if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 804 | if (GlobalValue *GV = dyn_cast<GlobalValue>(SU->getValue())) { |
| 805 | TargetLowering::AddrMode AM; |
| 806 | AM.BaseGV = GV; |
| 807 | AM.HasBaseReg = HasBaseReg; |
| 808 | return TLI->isLegalAddressingMode(AM, UseTy); |
| 809 | } |
| 810 | |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 811 | return false; |
| 812 | } |
| 813 | |
Dale Johannesen | 544e0d0 | 2008-12-03 20:56:12 +0000 | [diff] [blame] | 814 | /// MoveLoopVariantsToImmediateField - Move any subexpressions from Val that are |
Chris Lattner | 44b807e | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 815 | /// loop varying to the Imm operand. |
Dale Johannesen | 544e0d0 | 2008-12-03 20:56:12 +0000 | [diff] [blame] | 816 | static void MoveLoopVariantsToImmediateField(SCEVHandle &Val, SCEVHandle &Imm, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 817 | Loop *L, ScalarEvolution *SE) { |
Chris Lattner | 44b807e | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 818 | if (Val->isLoopInvariant(L)) return; // Nothing to do. |
| 819 | |
| 820 | if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) { |
| 821 | std::vector<SCEVHandle> NewOps; |
| 822 | NewOps.reserve(SAE->getNumOperands()); |
| 823 | |
| 824 | for (unsigned i = 0; i != SAE->getNumOperands(); ++i) |
| 825 | if (!SAE->getOperand(i)->isLoopInvariant(L)) { |
| 826 | // If this is a loop-variant expression, it must stay in the immediate |
| 827 | // field of the expression. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 828 | Imm = SE->getAddExpr(Imm, SAE->getOperand(i)); |
Chris Lattner | 44b807e | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 829 | } else { |
| 830 | NewOps.push_back(SAE->getOperand(i)); |
| 831 | } |
| 832 | |
| 833 | if (NewOps.empty()) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 834 | Val = SE->getIntegerSCEV(0, Val->getType()); |
Chris Lattner | 44b807e | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 835 | else |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 836 | Val = SE->getAddExpr(NewOps); |
Chris Lattner | 44b807e | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 837 | } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) { |
| 838 | // Try to pull immediates out of the start value of nested addrec's. |
| 839 | SCEVHandle Start = SARE->getStart(); |
Dale Johannesen | 544e0d0 | 2008-12-03 20:56:12 +0000 | [diff] [blame] | 840 | MoveLoopVariantsToImmediateField(Start, Imm, L, SE); |
Chris Lattner | 44b807e | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 841 | |
| 842 | std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end()); |
| 843 | Ops[0] = Start; |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 844 | Val = SE->getAddRecExpr(Ops, SARE->getLoop()); |
Chris Lattner | 44b807e | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 845 | } else { |
| 846 | // Otherwise, all of Val is variant, move the whole thing over. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 847 | Imm = SE->getAddExpr(Imm, Val); |
| 848 | Val = SE->getIntegerSCEV(0, Val->getType()); |
Chris Lattner | 44b807e | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
| 852 | |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 853 | /// MoveImmediateValues - Look at Val, and pull out any additions of constants |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 854 | /// that can fit into the immediate field of instructions in the target. |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 855 | /// Accumulate these immediate values into the Imm value. |
Evan Cheng | d277f2c | 2006-03-13 23:14:23 +0000 | [diff] [blame] | 856 | static void MoveImmediateValues(const TargetLowering *TLI, |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 857 | const Type *UseTy, |
Evan Cheng | d277f2c | 2006-03-13 23:14:23 +0000 | [diff] [blame] | 858 | SCEVHandle &Val, SCEVHandle &Imm, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 859 | bool isAddress, Loop *L, |
| 860 | ScalarEvolution *SE) { |
Chris Lattner | 7a65839 | 2005-08-03 23:44:42 +0000 | [diff] [blame] | 861 | if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) { |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 862 | std::vector<SCEVHandle> NewOps; |
| 863 | NewOps.reserve(SAE->getNumOperands()); |
| 864 | |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 865 | for (unsigned i = 0; i != SAE->getNumOperands(); ++i) { |
| 866 | SCEVHandle NewOp = SAE->getOperand(i); |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 867 | MoveImmediateValues(TLI, UseTy, NewOp, Imm, isAddress, L, SE); |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 868 | |
| 869 | if (!NewOp->isLoopInvariant(L)) { |
Chris Lattner | 7db543f | 2005-08-04 19:08:16 +0000 | [diff] [blame] | 870 | // If this is a loop-variant expression, it must stay in the immediate |
| 871 | // field of the expression. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 872 | Imm = SE->getAddExpr(Imm, NewOp); |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 873 | } else { |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 874 | NewOps.push_back(NewOp); |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 875 | } |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 876 | } |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 877 | |
| 878 | if (NewOps.empty()) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 879 | Val = SE->getIntegerSCEV(0, Val->getType()); |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 880 | else |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 881 | Val = SE->getAddExpr(NewOps); |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 882 | return; |
Chris Lattner | 7a65839 | 2005-08-03 23:44:42 +0000 | [diff] [blame] | 883 | } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) { |
| 884 | // Try to pull immediates out of the start value of nested addrec's. |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 885 | SCEVHandle Start = SARE->getStart(); |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 886 | MoveImmediateValues(TLI, UseTy, Start, Imm, isAddress, L, SE); |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 887 | |
| 888 | if (Start != SARE->getStart()) { |
| 889 | std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end()); |
| 890 | Ops[0] = Start; |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 891 | Val = SE->getAddRecExpr(Ops, SARE->getLoop()); |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 892 | } |
| 893 | return; |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 894 | } else if (SCEVMulExpr *SME = dyn_cast<SCEVMulExpr>(Val)) { |
| 895 | // Transform "8 * (4 + v)" -> "32 + 8*V" if "32" fits in the immed field. |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 896 | if (isAddress && fitsInAddressMode(SME->getOperand(0), UseTy, TLI, false) && |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 897 | SME->getNumOperands() == 2 && SME->isLoopInvariant(L)) { |
| 898 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 899 | SCEVHandle SubImm = SE->getIntegerSCEV(0, Val->getType()); |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 900 | SCEVHandle NewOp = SME->getOperand(1); |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 901 | MoveImmediateValues(TLI, UseTy, NewOp, SubImm, isAddress, L, SE); |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 902 | |
| 903 | // If we extracted something out of the subexpressions, see if we can |
| 904 | // simplify this! |
| 905 | if (NewOp != SME->getOperand(1)) { |
| 906 | // Scale SubImm up by "8". If the result is a target constant, we are |
| 907 | // good. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 908 | SubImm = SE->getMulExpr(SubImm, SME->getOperand(0)); |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 909 | if (fitsInAddressMode(SubImm, UseTy, TLI, false)) { |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 910 | // Accumulate the immediate. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 911 | Imm = SE->getAddExpr(Imm, SubImm); |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 912 | |
| 913 | // Update what is left of 'Val'. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 914 | Val = SE->getMulExpr(SME->getOperand(0), NewOp); |
Chris Lattner | 221fc3c | 2006-02-04 07:36:50 +0000 | [diff] [blame] | 915 | return; |
| 916 | } |
| 917 | } |
| 918 | } |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 921 | // Loop-variant expressions must stay in the immediate field of the |
| 922 | // expression. |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 923 | if ((isAddress && fitsInAddressMode(Val, UseTy, TLI, false)) || |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 924 | !Val->isLoopInvariant(L)) { |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 925 | Imm = SE->getAddExpr(Imm, Val); |
| 926 | Val = SE->getIntegerSCEV(0, Val->getType()); |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 927 | return; |
Chris Lattner | 7a2ca56 | 2005-08-04 19:26:19 +0000 | [diff] [blame] | 928 | } |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 929 | |
| 930 | // Otherwise, no immediates to move. |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 933 | static void MoveImmediateValues(const TargetLowering *TLI, |
| 934 | Instruction *User, |
| 935 | SCEVHandle &Val, SCEVHandle &Imm, |
| 936 | bool isAddress, Loop *L, |
| 937 | ScalarEvolution *SE) { |
Dan Gohman | 21e7722 | 2009-03-09 21:01:17 +0000 | [diff] [blame] | 938 | const Type *UseTy = getAccessType(User); |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 939 | MoveImmediateValues(TLI, UseTy, Val, Imm, isAddress, L, SE); |
| 940 | } |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 941 | |
Chris Lattner | 7e79b38 | 2006-08-03 06:34:50 +0000 | [diff] [blame] | 942 | /// SeparateSubExprs - Decompose Expr into all of the subexpressions that are |
| 943 | /// added together. This is used to reassociate common addition subexprs |
| 944 | /// together for maximal sharing when rewriting bases. |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 945 | static void SeparateSubExprs(std::vector<SCEVHandle> &SubExprs, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 946 | SCEVHandle Expr, |
| 947 | ScalarEvolution *SE) { |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 948 | if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(Expr)) { |
| 949 | for (unsigned j = 0, e = AE->getNumOperands(); j != e; ++j) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 950 | SeparateSubExprs(SubExprs, AE->getOperand(j), SE); |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 951 | } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Expr)) { |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 952 | SCEVHandle Zero = SE->getIntegerSCEV(0, Expr->getType()); |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 953 | if (SARE->getOperand(0) == Zero) { |
| 954 | SubExprs.push_back(Expr); |
| 955 | } else { |
| 956 | // Compute the addrec with zero as its base. |
| 957 | std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end()); |
| 958 | Ops[0] = Zero; // Start with zero base. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 959 | SubExprs.push_back(SE->getAddRecExpr(Ops, SARE->getLoop())); |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 960 | |
| 961 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 962 | SeparateSubExprs(SubExprs, SARE->getOperand(0), SE); |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 963 | } |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 964 | } else if (!Expr->isZero()) { |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 965 | // Do not add zero. |
| 966 | SubExprs.push_back(Expr); |
| 967 | } |
| 968 | } |
| 969 | |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 970 | // This is logically local to the following function, but C++ says we have |
| 971 | // to make it file scope. |
| 972 | struct SubExprUseData { unsigned Count; bool notAllUsesAreFree; }; |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 973 | |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 974 | /// RemoveCommonExpressionsFromUseBases - Look through all of the Bases of all |
| 975 | /// the Uses, removing any common subexpressions, except that if all such |
| 976 | /// subexpressions can be folded into an addressing mode for all uses inside |
| 977 | /// the loop (this case is referred to as "free" in comments herein) we do |
| 978 | /// not remove anything. This looks for things like (a+b+c) and |
Chris Lattner | f8828eb | 2008-12-02 04:52:26 +0000 | [diff] [blame] | 979 | /// (a+c+d) and computes the common (a+c) subexpression. The common expression |
| 980 | /// is *removed* from the Bases and returned. |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 981 | static SCEVHandle |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 982 | RemoveCommonExpressionsFromUseBases(std::vector<BasedUser> &Uses, |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 983 | ScalarEvolution *SE, Loop *L, |
| 984 | const TargetLowering *TLI) { |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 985 | unsigned NumUses = Uses.size(); |
| 986 | |
Chris Lattner | f8828eb | 2008-12-02 04:52:26 +0000 | [diff] [blame] | 987 | // Only one use? This is a very common case, so we handle it specially and |
| 988 | // cheaply. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 989 | SCEVHandle Zero = SE->getIntegerSCEV(0, Uses[0].Base->getType()); |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 990 | SCEVHandle Result = Zero; |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 991 | SCEVHandle FreeResult = Zero; |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 992 | if (NumUses == 1) { |
Chris Lattner | f8828eb | 2008-12-02 04:52:26 +0000 | [diff] [blame] | 993 | // If the use is inside the loop, use its base, regardless of what it is: |
| 994 | // it is clearly shared across all the IV's. If the use is outside the loop |
| 995 | // (which means after it) we don't want to factor anything *into* the loop, |
| 996 | // so just use 0 as the base. |
Dale Johannesen | 589bf08 | 2008-12-01 22:00:01 +0000 | [diff] [blame] | 997 | if (L->contains(Uses[0].Inst->getParent())) |
| 998 | std::swap(Result, Uses[0].Base); |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 999 | return Result; |
| 1000 | } |
| 1001 | |
| 1002 | // To find common subexpressions, count how many of Uses use each expression. |
| 1003 | // If any subexpressions are used Uses.size() times, they are common. |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 1004 | // Also track whether all uses of each expression can be moved into an |
| 1005 | // an addressing mode "for free"; such expressions are left within the loop. |
| 1006 | // struct SubExprUseData { unsigned Count; bool notAllUsesAreFree; }; |
| 1007 | std::map<SCEVHandle, SubExprUseData> SubExpressionUseData; |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1008 | |
Chris Lattner | d6155e9 | 2005-10-11 18:41:04 +0000 | [diff] [blame] | 1009 | // UniqueSubExprs - Keep track of all of the subexpressions we see in the |
| 1010 | // order we see them. |
| 1011 | std::vector<SCEVHandle> UniqueSubExprs; |
| 1012 | |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 1013 | std::vector<SCEVHandle> SubExprs; |
Chris Lattner | f8828eb | 2008-12-02 04:52:26 +0000 | [diff] [blame] | 1014 | unsigned NumUsesInsideLoop = 0; |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 1015 | for (unsigned i = 0; i != NumUses; ++i) { |
Chris Lattner | f8828eb | 2008-12-02 04:52:26 +0000 | [diff] [blame] | 1016 | // If the user is outside the loop, just ignore it for base computation. |
| 1017 | // Since the user is outside the loop, it must be *after* the loop (if it |
| 1018 | // were before, it could not be based on the loop IV). We don't want users |
| 1019 | // after the loop to affect base computation of values *inside* the loop, |
| 1020 | // because we can always add their offsets to the result IV after the loop |
| 1021 | // is done, ensuring we get good code inside the loop. |
Dale Johannesen | 589bf08 | 2008-12-01 22:00:01 +0000 | [diff] [blame] | 1022 | if (!L->contains(Uses[i].Inst->getParent())) |
| 1023 | continue; |
| 1024 | NumUsesInsideLoop++; |
| 1025 | |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 1026 | // If the base is zero (which is common), return zero now, there are no |
| 1027 | // CSEs we can find. |
| 1028 | if (Uses[i].Base == Zero) return Zero; |
| 1029 | |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 1030 | // If this use is as an address we may be able to put CSEs in the addressing |
| 1031 | // mode rather than hoisting them. |
| 1032 | bool isAddrUse = isAddressUse(Uses[i].Inst, Uses[i].OperandValToReplace); |
| 1033 | // We may need the UseTy below, but only when isAddrUse, so compute it |
| 1034 | // only in that case. |
| 1035 | const Type *UseTy = 0; |
Dan Gohman | 21e7722 | 2009-03-09 21:01:17 +0000 | [diff] [blame] | 1036 | if (isAddrUse) |
| 1037 | UseTy = getAccessType(Uses[i].Inst); |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 1038 | |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 1039 | // Split the expression into subexprs. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1040 | SeparateSubExprs(SubExprs, Uses[i].Base, SE); |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 1041 | // Add one to SubExpressionUseData.Count for each subexpr present, and |
| 1042 | // if the subexpr is not a valid immediate within an addressing mode use, |
| 1043 | // set SubExpressionUseData.notAllUsesAreFree. We definitely want to |
| 1044 | // hoist these out of the loop (if they are common to all uses). |
| 1045 | for (unsigned j = 0, e = SubExprs.size(); j != e; ++j) { |
| 1046 | if (++SubExpressionUseData[SubExprs[j]].Count == 1) |
Chris Lattner | d6155e9 | 2005-10-11 18:41:04 +0000 | [diff] [blame] | 1047 | UniqueSubExprs.push_back(SubExprs[j]); |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 1048 | if (!isAddrUse || !fitsInAddressMode(SubExprs[j], UseTy, TLI, false)) |
| 1049 | SubExpressionUseData[SubExprs[j]].notAllUsesAreFree = true; |
| 1050 | } |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 1051 | SubExprs.clear(); |
| 1052 | } |
| 1053 | |
Chris Lattner | d6155e9 | 2005-10-11 18:41:04 +0000 | [diff] [blame] | 1054 | // Now that we know how many times each is used, build Result. Iterate over |
| 1055 | // UniqueSubexprs so that we have a stable ordering. |
| 1056 | for (unsigned i = 0, e = UniqueSubExprs.size(); i != e; ++i) { |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 1057 | std::map<SCEVHandle, SubExprUseData>::iterator I = |
| 1058 | SubExpressionUseData.find(UniqueSubExprs[i]); |
| 1059 | assert(I != SubExpressionUseData.end() && "Entry not found?"); |
| 1060 | if (I->second.Count == NumUsesInsideLoop) { // Found CSE! |
| 1061 | if (I->second.notAllUsesAreFree) |
| 1062 | Result = SE->getAddExpr(Result, I->first); |
| 1063 | else |
| 1064 | FreeResult = SE->getAddExpr(FreeResult, I->first); |
| 1065 | } else |
| 1066 | // Remove non-cse's from SubExpressionUseData. |
| 1067 | SubExpressionUseData.erase(I); |
Chris Lattner | d6155e9 | 2005-10-11 18:41:04 +0000 | [diff] [blame] | 1068 | } |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 1069 | |
| 1070 | if (FreeResult != Zero) { |
| 1071 | // We have some subexpressions that can be subsumed into addressing |
| 1072 | // modes in every use inside the loop. However, it's possible that |
| 1073 | // there are so many of them that the combined FreeResult cannot |
| 1074 | // be subsumed, or that the target cannot handle both a FreeResult |
| 1075 | // and a Result in the same instruction (for example because it would |
| 1076 | // require too many registers). Check this. |
| 1077 | for (unsigned i=0; i<NumUses; ++i) { |
| 1078 | if (!L->contains(Uses[i].Inst->getParent())) |
| 1079 | continue; |
| 1080 | // We know this is an addressing mode use; if there are any uses that |
| 1081 | // are not, FreeResult would be Zero. |
Dan Gohman | 21e7722 | 2009-03-09 21:01:17 +0000 | [diff] [blame] | 1082 | const Type *UseTy = getAccessType(Uses[i].Inst); |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 1083 | if (!fitsInAddressMode(FreeResult, UseTy, TLI, Result!=Zero)) { |
| 1084 | // FIXME: could split up FreeResult into pieces here, some hoisted |
Dale Johannesen | b039062 | 2008-12-16 22:16:28 +0000 | [diff] [blame] | 1085 | // and some not. There is no obvious advantage to this. |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 1086 | Result = SE->getAddExpr(Result, FreeResult); |
| 1087 | FreeResult = Zero; |
| 1088 | break; |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1093 | // If we found no CSE's, return now. |
| 1094 | if (Result == Zero) return Result; |
| 1095 | |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 1096 | // If we still have a FreeResult, remove its subexpressions from |
| 1097 | // SubExpressionUseData. This means they will remain in the use Bases. |
| 1098 | if (FreeResult != Zero) { |
| 1099 | SeparateSubExprs(SubExprs, FreeResult, SE); |
| 1100 | for (unsigned j = 0, e = SubExprs.size(); j != e; ++j) { |
| 1101 | std::map<SCEVHandle, SubExprUseData>::iterator I = |
| 1102 | SubExpressionUseData.find(SubExprs[j]); |
| 1103 | SubExpressionUseData.erase(I); |
| 1104 | } |
| 1105 | SubExprs.clear(); |
| 1106 | } |
| 1107 | |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1108 | // Otherwise, remove all of the CSE's we found from each of the base values. |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 1109 | for (unsigned i = 0; i != NumUses; ++i) { |
Dale Johannesen | fb10cd4 | 2008-12-02 21:17:11 +0000 | [diff] [blame] | 1110 | // Uses outside the loop don't necessarily include the common base, but |
| 1111 | // the final IV value coming into those uses does. Instead of trying to |
| 1112 | // remove the pieces of the common base, which might not be there, |
| 1113 | // subtract off the base to compensate for this. |
| 1114 | if (!L->contains(Uses[i].Inst->getParent())) { |
| 1115 | Uses[i].Base = SE->getMinusSCEV(Uses[i].Base, Result); |
Dale Johannesen | 589bf08 | 2008-12-01 22:00:01 +0000 | [diff] [blame] | 1116 | continue; |
Dale Johannesen | fb10cd4 | 2008-12-02 21:17:11 +0000 | [diff] [blame] | 1117 | } |
Dale Johannesen | 589bf08 | 2008-12-01 22:00:01 +0000 | [diff] [blame] | 1118 | |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 1119 | // Split the expression into subexprs. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1120 | SeparateSubExprs(SubExprs, Uses[i].Base, SE); |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 1121 | |
| 1122 | // Remove any common subexpressions. |
| 1123 | for (unsigned j = 0, e = SubExprs.size(); j != e; ++j) |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 1124 | if (SubExpressionUseData.count(SubExprs[j])) { |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 1125 | SubExprs.erase(SubExprs.begin()+j); |
| 1126 | --j; --e; |
| 1127 | } |
| 1128 | |
Chris Lattner | f8828eb | 2008-12-02 04:52:26 +0000 | [diff] [blame] | 1129 | // Finally, add the non-shared expressions together. |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 1130 | if (SubExprs.empty()) |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1131 | Uses[i].Base = Zero; |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 1132 | else |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1133 | Uses[i].Base = SE->getAddExpr(SubExprs); |
Chris Lattner | 27e5142 | 2005-08-13 07:42:01 +0000 | [diff] [blame] | 1134 | SubExprs.clear(); |
Chris Lattner | 934520a | 2005-08-13 07:27:18 +0000 | [diff] [blame] | 1135 | } |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1136 | |
| 1137 | return Result; |
| 1138 | } |
| 1139 | |
Dale Johannesen | dc42f48 | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 1140 | /// ValidStride - Check whether the given Scale is valid for all loads and |
Chris Lattner | 579633c | 2007-04-09 22:20:14 +0000 | [diff] [blame] | 1141 | /// stores in UsersToProcess. |
Dale Johannesen | dc42f48 | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 1142 | /// |
Dan Gohman | 02e4fa7 | 2007-10-22 20:40:42 +0000 | [diff] [blame] | 1143 | bool LoopStrengthReduce::ValidStride(bool HasBaseReg, |
| 1144 | int64_t Scale, |
Dale Johannesen | dc42f48 | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 1145 | const std::vector<BasedUser>& UsersToProcess) { |
Evan Cheng | d6b62a5 | 2007-12-19 23:33:23 +0000 | [diff] [blame] | 1146 | if (!TLI) |
| 1147 | return true; |
| 1148 | |
Dale Johannesen | 8e59e16 | 2007-03-20 21:54:54 +0000 | [diff] [blame] | 1149 | for (unsigned i=0, e = UsersToProcess.size(); i!=e; ++i) { |
Chris Lattner | 1ebd89e | 2007-04-02 06:34:44 +0000 | [diff] [blame] | 1150 | // If this is a load or other access, pass the type of the access in. |
| 1151 | const Type *AccessTy = Type::VoidTy; |
Dan Gohman | 21e7722 | 2009-03-09 21:01:17 +0000 | [diff] [blame] | 1152 | if (isAddressUse(UsersToProcess[i].Inst, |
| 1153 | UsersToProcess[i].OperandValToReplace)) |
| 1154 | AccessTy = getAccessType(UsersToProcess[i].Inst); |
Evan Cheng | 55e641b | 2008-03-19 22:02:26 +0000 | [diff] [blame] | 1155 | else if (isa<PHINode>(UsersToProcess[i].Inst)) |
| 1156 | continue; |
Chris Lattner | 1ebd89e | 2007-04-02 06:34:44 +0000 | [diff] [blame] | 1157 | |
Chris Lattner | 579633c | 2007-04-09 22:20:14 +0000 | [diff] [blame] | 1158 | TargetLowering::AddrMode AM; |
| 1159 | if (SCEVConstant *SC = dyn_cast<SCEVConstant>(UsersToProcess[i].Imm)) |
| 1160 | AM.BaseOffs = SC->getValue()->getSExtValue(); |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1161 | AM.HasBaseReg = HasBaseReg || !UsersToProcess[i].Base->isZero(); |
Chris Lattner | 579633c | 2007-04-09 22:20:14 +0000 | [diff] [blame] | 1162 | AM.Scale = Scale; |
| 1163 | |
| 1164 | // If load[imm+r*scale] is illegal, bail out. |
Evan Cheng | d6b62a5 | 2007-12-19 23:33:23 +0000 | [diff] [blame] | 1165 | if (!TLI->isLegalAddressingMode(AM, AccessTy)) |
Dale Johannesen | dc42f48 | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 1166 | return false; |
Dale Johannesen | 8e59e16 | 2007-03-20 21:54:54 +0000 | [diff] [blame] | 1167 | } |
Dale Johannesen | dc42f48 | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 1168 | return true; |
| 1169 | } |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1170 | |
Dale Johannesen | 1de17d5 | 2009-02-09 22:14:15 +0000 | [diff] [blame] | 1171 | /// RequiresTypeConversion - Returns true if converting Ty1 to Ty2 is not |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1172 | /// a nop. |
Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 1173 | bool LoopStrengthReduce::RequiresTypeConversion(const Type *Ty1, |
| 1174 | const Type *Ty2) { |
| 1175 | if (Ty1 == Ty2) |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1176 | return false; |
Dale Johannesen | 1de17d5 | 2009-02-09 22:14:15 +0000 | [diff] [blame] | 1177 | if (Ty1->canLosslesslyBitCastTo(Ty2)) |
| 1178 | return false; |
Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 1179 | if (TLI && TLI->isTruncateFree(Ty1, Ty2)) |
| 1180 | return false; |
Dale Johannesen | 1de17d5 | 2009-02-09 22:14:15 +0000 | [diff] [blame] | 1181 | if (isa<PointerType>(Ty2) && Ty1->canLosslesslyBitCastTo(UIntPtrTy)) |
| 1182 | return false; |
| 1183 | if (isa<PointerType>(Ty1) && Ty2->canLosslesslyBitCastTo(UIntPtrTy)) |
| 1184 | return false; |
| 1185 | return true; |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Evan Cheng | eb8f9e2 | 2006-03-17 19:52:23 +0000 | [diff] [blame] | 1188 | /// CheckForIVReuse - Returns the multiple if the stride is the multiple |
| 1189 | /// of a previous stride and it is a legal value for the target addressing |
Dan Gohman | 02e4fa7 | 2007-10-22 20:40:42 +0000 | [diff] [blame] | 1190 | /// mode scale component and optional base reg. This allows the users of |
| 1191 | /// this stride to be rewritten as prev iv * factor. It returns 0 if no |
Dale Johannesen | b039062 | 2008-12-16 22:16:28 +0000 | [diff] [blame] | 1192 | /// reuse is possible. Factors can be negative on same targets, e.g. ARM. |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 1193 | /// |
| 1194 | /// If all uses are outside the loop, we don't require that all multiplies |
| 1195 | /// be folded into the addressing mode, nor even that the factor be constant; |
| 1196 | /// a multiply (executed once) outside the loop is better than another IV |
| 1197 | /// within. Well, usually. |
| 1198 | SCEVHandle LoopStrengthReduce::CheckForIVReuse(bool HasBaseReg, |
Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 1199 | bool AllUsesAreAddresses, |
Dale Johannesen | b039062 | 2008-12-16 22:16:28 +0000 | [diff] [blame] | 1200 | bool AllUsesAreOutsideLoop, |
Dan Gohman | 02e4fa7 | 2007-10-22 20:40:42 +0000 | [diff] [blame] | 1201 | const SCEVHandle &Stride, |
Dale Johannesen | dc42f48 | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 1202 | IVExpr &IV, const Type *Ty, |
| 1203 | const std::vector<BasedUser>& UsersToProcess) { |
Evan Cheng | eb8f9e2 | 2006-03-17 19:52:23 +0000 | [diff] [blame] | 1204 | if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Stride)) { |
Reid Spencer | 502db93 | 2007-03-02 23:37:53 +0000 | [diff] [blame] | 1205 | int64_t SInt = SC->getValue()->getSExtValue(); |
Dale Johannesen | b51b4b5 | 2007-11-17 02:48:01 +0000 | [diff] [blame] | 1206 | for (unsigned NewStride = 0, e = StrideOrder.size(); NewStride != e; |
| 1207 | ++NewStride) { |
| 1208 | std::map<SCEVHandle, IVsOfOneStride>::iterator SI = |
| 1209 | IVsByStride.find(StrideOrder[NewStride]); |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 1210 | if (SI == IVsByStride.end() || !isa<SCEVConstant>(SI->first)) |
Dale Johannesen | b51b4b5 | 2007-11-17 02:48:01 +0000 | [diff] [blame] | 1211 | continue; |
Evan Cheng | 5eef2d2 | 2007-03-12 23:27:37 +0000 | [diff] [blame] | 1212 | int64_t SSInt = cast<SCEVConstant>(SI->first)->getValue()->getSExtValue(); |
Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 1213 | if (SI->first != Stride && |
Chris Lattner | 1d31290 | 2007-04-02 22:51:58 +0000 | [diff] [blame] | 1214 | (unsigned(abs(SInt)) < SSInt || (SInt % SSInt) != 0)) |
Evan Cheng | eb8f9e2 | 2006-03-17 19:52:23 +0000 | [diff] [blame] | 1215 | continue; |
Evan Cheng | 5eef2d2 | 2007-03-12 23:27:37 +0000 | [diff] [blame] | 1216 | int64_t Scale = SInt / SSInt; |
Dale Johannesen | dc42f48 | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 1217 | // Check that this stride is valid for all the types used for loads and |
| 1218 | // stores; if it can be used for some and not others, we might as well use |
| 1219 | // the original stride everywhere, since we have to create the IV for it |
Dan Gohman | aa34331 | 2007-10-29 19:23:53 +0000 | [diff] [blame] | 1220 | // anyway. If the scale is 1, then we don't need to worry about folding |
| 1221 | // multiplications. |
| 1222 | if (Scale == 1 || |
| 1223 | (AllUsesAreAddresses && |
| 1224 | ValidStride(HasBaseReg, Scale, UsersToProcess))) |
Evan Cheng | 5eef2d2 | 2007-03-12 23:27:37 +0000 | [diff] [blame] | 1225 | for (std::vector<IVExpr>::iterator II = SI->second.IVs.begin(), |
| 1226 | IE = SI->second.IVs.end(); II != IE; ++II) |
| 1227 | // FIXME: Only handle base == 0 for now. |
| 1228 | // Only reuse previous IV if it would not require a type conversion. |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1229 | if (II->Base->isZero() && |
Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 1230 | !RequiresTypeConversion(II->Base->getType(), Ty)) { |
Evan Cheng | 5eef2d2 | 2007-03-12 23:27:37 +0000 | [diff] [blame] | 1231 | IV = *II; |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 1232 | return SE->getIntegerSCEV(Scale, Stride->getType()); |
Evan Cheng | 5eef2d2 | 2007-03-12 23:27:37 +0000 | [diff] [blame] | 1233 | } |
Evan Cheng | eb8f9e2 | 2006-03-17 19:52:23 +0000 | [diff] [blame] | 1234 | } |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 1235 | } else if (AllUsesAreOutsideLoop) { |
| 1236 | // Accept nonconstant strides here; it is really really right to substitute |
| 1237 | // an existing IV if we can. |
| 1238 | for (unsigned NewStride = 0, e = StrideOrder.size(); NewStride != e; |
| 1239 | ++NewStride) { |
| 1240 | std::map<SCEVHandle, IVsOfOneStride>::iterator SI = |
| 1241 | IVsByStride.find(StrideOrder[NewStride]); |
| 1242 | if (SI == IVsByStride.end() || !isa<SCEVConstant>(SI->first)) |
| 1243 | continue; |
| 1244 | int64_t SSInt = cast<SCEVConstant>(SI->first)->getValue()->getSExtValue(); |
| 1245 | if (SI->first != Stride && SSInt != 1) |
| 1246 | continue; |
| 1247 | for (std::vector<IVExpr>::iterator II = SI->second.IVs.begin(), |
| 1248 | IE = SI->second.IVs.end(); II != IE; ++II) |
| 1249 | // Accept nonzero base here. |
| 1250 | // Only reuse previous IV if it would not require a type conversion. |
| 1251 | if (!RequiresTypeConversion(II->Base->getType(), Ty)) { |
| 1252 | IV = *II; |
| 1253 | return Stride; |
| 1254 | } |
| 1255 | } |
| 1256 | // Special case, old IV is -1*x and this one is x. Can treat this one as |
| 1257 | // -1*old. |
| 1258 | for (unsigned NewStride = 0, e = StrideOrder.size(); NewStride != e; |
| 1259 | ++NewStride) { |
| 1260 | std::map<SCEVHandle, IVsOfOneStride>::iterator SI = |
| 1261 | IVsByStride.find(StrideOrder[NewStride]); |
| 1262 | if (SI == IVsByStride.end()) |
| 1263 | continue; |
| 1264 | if (SCEVMulExpr *ME = dyn_cast<SCEVMulExpr>(SI->first)) |
| 1265 | if (SCEVConstant *SC = dyn_cast<SCEVConstant>(ME->getOperand(0))) |
| 1266 | if (Stride == ME->getOperand(1) && |
| 1267 | SC->getValue()->getSExtValue() == -1LL) |
| 1268 | for (std::vector<IVExpr>::iterator II = SI->second.IVs.begin(), |
| 1269 | IE = SI->second.IVs.end(); II != IE; ++II) |
| 1270 | // Accept nonzero base here. |
| 1271 | // Only reuse previous IV if it would not require type conversion. |
| 1272 | if (!RequiresTypeConversion(II->Base->getType(), Ty)) { |
| 1273 | IV = *II; |
| 1274 | return SE->getIntegerSCEV(-1LL, Stride->getType()); |
| 1275 | } |
| 1276 | } |
Evan Cheng | eb8f9e2 | 2006-03-17 19:52:23 +0000 | [diff] [blame] | 1277 | } |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 1278 | return SE->getIntegerSCEV(0, Stride->getType()); |
Evan Cheng | eb8f9e2 | 2006-03-17 19:52:23 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Chris Lattner | 7e79b38 | 2006-08-03 06:34:50 +0000 | [diff] [blame] | 1281 | /// PartitionByIsUseOfPostIncrementedValue - Simple boolean predicate that |
| 1282 | /// returns true if Val's isUseOfPostIncrementedValue is true. |
| 1283 | static bool PartitionByIsUseOfPostIncrementedValue(const BasedUser &Val) { |
| 1284 | return Val.isUseOfPostIncrementedValue; |
| 1285 | } |
Evan Cheng | eb8f9e2 | 2006-03-17 19:52:23 +0000 | [diff] [blame] | 1286 | |
Dan Gohman | 4a9a3e5 | 2008-04-14 18:26:16 +0000 | [diff] [blame] | 1287 | /// isNonConstantNegative - Return true if the specified scev is negated, but |
Chris Lattner | fb3e119 | 2007-05-19 01:22:21 +0000 | [diff] [blame] | 1288 | /// not a constant. |
| 1289 | static bool isNonConstantNegative(const SCEVHandle &Expr) { |
| 1290 | SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Expr); |
| 1291 | if (!Mul) return false; |
| 1292 | |
| 1293 | // If there is a constant factor, it will be first. |
| 1294 | SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0)); |
| 1295 | if (!SC) return false; |
| 1296 | |
| 1297 | // Return true if the value is negative, this matches things like (-42 * V). |
| 1298 | return SC->getValue()->getValue().isNegative(); |
| 1299 | } |
| 1300 | |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1301 | // CollectIVUsers - Transform our list of users and offsets to a bit more |
Dan Gohman | 73b43b9 | 2008-06-23 22:11:52 +0000 | [diff] [blame] | 1302 | // complex table. In this new vector, each 'BasedUser' contains 'Base', the base |
| 1303 | // of the strided accesses, as well as the old information from Uses. We |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1304 | // progressively move information from the Base field to the Imm field, until |
| 1305 | // we eventually have the full access expression to rewrite the use. |
| 1306 | SCEVHandle LoopStrengthReduce::CollectIVUsers(const SCEVHandle &Stride, |
| 1307 | IVUsersOfOneStride &Uses, |
| 1308 | Loop *L, |
| 1309 | bool &AllUsesAreAddresses, |
Dale Johannesen | b039062 | 2008-12-16 22:16:28 +0000 | [diff] [blame] | 1310 | bool &AllUsesAreOutsideLoop, |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1311 | std::vector<BasedUser> &UsersToProcess) { |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1312 | UsersToProcess.reserve(Uses.Users.size()); |
Chris Lattner | a553b0c | 2005-08-08 22:56:21 +0000 | [diff] [blame] | 1313 | for (unsigned i = 0, e = Uses.Users.size(); i != e; ++i) { |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1314 | UsersToProcess.push_back(BasedUser(Uses.Users[i], SE)); |
Chris Lattner | a553b0c | 2005-08-08 22:56:21 +0000 | [diff] [blame] | 1315 | |
Dale Johannesen | 67c7989 | 2008-12-03 19:25:46 +0000 | [diff] [blame] | 1316 | // Move any loop variant operands from the offset field to the immediate |
Chris Lattner | a553b0c | 2005-08-08 22:56:21 +0000 | [diff] [blame] | 1317 | // field of the use, so that we don't try to use something before it is |
| 1318 | // computed. |
Dale Johannesen | 544e0d0 | 2008-12-03 20:56:12 +0000 | [diff] [blame] | 1319 | MoveLoopVariantsToImmediateField(UsersToProcess.back().Base, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1320 | UsersToProcess.back().Imm, L, SE); |
Chris Lattner | a553b0c | 2005-08-08 22:56:21 +0000 | [diff] [blame] | 1321 | assert(UsersToProcess.back().Base->isLoopInvariant(L) && |
Chris Lattner | 26d91f1 | 2005-08-04 22:34:05 +0000 | [diff] [blame] | 1322 | "Base value is not loop invariant!"); |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1323 | } |
Evan Cheng | eb8f9e2 | 2006-03-17 19:52:23 +0000 | [diff] [blame] | 1324 | |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1325 | // We now have a whole bunch of uses of like-strided induction variables, but |
| 1326 | // they might all have different bases. We want to emit one PHI node for this |
| 1327 | // stride which we fold as many common expressions (between the IVs) into as |
| 1328 | // possible. Start by identifying the common expressions in the base values |
| 1329 | // for the strides (e.g. if we have "A+C+B" and "A+B+D" as our bases, find |
| 1330 | // "A+B"), emit it to the preheader, then remove the expression from the |
| 1331 | // UsersToProcess base values. |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 1332 | SCEVHandle CommonExprs = |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 1333 | RemoveCommonExpressionsFromUseBases(UsersToProcess, SE, L, TLI); |
Dan Gohman | 02e4fa7 | 2007-10-22 20:40:42 +0000 | [diff] [blame] | 1334 | |
Chris Lattner | 44b807e | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 1335 | // Next, figure out what we can represent in the immediate fields of |
| 1336 | // instructions. If we can represent anything there, move it to the imm |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1337 | // fields of the BasedUsers. We do this so that it increases the commonality |
| 1338 | // of the remaining uses. |
Evan Cheng | 32e4c7c | 2007-12-20 02:20:53 +0000 | [diff] [blame] | 1339 | unsigned NumPHI = 0; |
Evan Cheng | d33cec1 | 2009-02-20 22:16:49 +0000 | [diff] [blame] | 1340 | bool HasAddress = false; |
Chris Lattner | 44b807e | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 1341 | for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) { |
Chris Lattner | 80b32b3 | 2005-08-16 00:38:11 +0000 | [diff] [blame] | 1342 | // If the user is not in the current loop, this means it is using the exit |
| 1343 | // value of the IV. Do not put anything in the base, make sure it's all in |
| 1344 | // the immediate field to allow as much factoring as possible. |
| 1345 | if (!L->contains(UsersToProcess[i].Inst->getParent())) { |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1346 | UsersToProcess[i].Imm = SE->getAddExpr(UsersToProcess[i].Imm, |
| 1347 | UsersToProcess[i].Base); |
Chris Lattner | 8385e51 | 2005-08-17 21:22:41 +0000 | [diff] [blame] | 1348 | UsersToProcess[i].Base = |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1349 | SE->getIntegerSCEV(0, UsersToProcess[i].Base->getType()); |
Chris Lattner | 80b32b3 | 2005-08-16 00:38:11 +0000 | [diff] [blame] | 1350 | } else { |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 1351 | // Not all uses are outside the loop. |
| 1352 | AllUsesAreOutsideLoop = false; |
| 1353 | |
Chris Lattner | 80b32b3 | 2005-08-16 00:38:11 +0000 | [diff] [blame] | 1354 | // Addressing modes can be folded into loads and stores. Be careful that |
| 1355 | // the store is through the expression, not of the expression though. |
Evan Cheng | 32e4c7c | 2007-12-20 02:20:53 +0000 | [diff] [blame] | 1356 | bool isPHI = false; |
Evan Cheng | d6b62a5 | 2007-12-19 23:33:23 +0000 | [diff] [blame] | 1357 | bool isAddress = isAddressUse(UsersToProcess[i].Inst, |
| 1358 | UsersToProcess[i].OperandValToReplace); |
| 1359 | if (isa<PHINode>(UsersToProcess[i].Inst)) { |
Evan Cheng | 32e4c7c | 2007-12-20 02:20:53 +0000 | [diff] [blame] | 1360 | isPHI = true; |
| 1361 | ++NumPHI; |
Dan Gohman | 2acc760 | 2007-05-03 23:20:33 +0000 | [diff] [blame] | 1362 | } |
Dan Gohman | 02e4fa7 | 2007-10-22 20:40:42 +0000 | [diff] [blame] | 1363 | |
Evan Cheng | d33cec1 | 2009-02-20 22:16:49 +0000 | [diff] [blame] | 1364 | if (isAddress) |
| 1365 | HasAddress = true; |
Dale Johannesen | b039062 | 2008-12-16 22:16:28 +0000 | [diff] [blame] | 1366 | |
Dan Gohman | 02e4fa7 | 2007-10-22 20:40:42 +0000 | [diff] [blame] | 1367 | // If this use isn't an address, then not all uses are addresses. |
Evan Cheng | 55e641b | 2008-03-19 22:02:26 +0000 | [diff] [blame] | 1368 | if (!isAddress && !isPHI) |
Dan Gohman | 02e4fa7 | 2007-10-22 20:40:42 +0000 | [diff] [blame] | 1369 | AllUsesAreAddresses = false; |
Chris Lattner | 80b32b3 | 2005-08-16 00:38:11 +0000 | [diff] [blame] | 1370 | |
Evan Cheng | 1d95816 | 2007-03-13 20:34:37 +0000 | [diff] [blame] | 1371 | MoveImmediateValues(TLI, UsersToProcess[i].Inst, UsersToProcess[i].Base, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1372 | UsersToProcess[i].Imm, isAddress, L, SE); |
Chris Lattner | 80b32b3 | 2005-08-16 00:38:11 +0000 | [diff] [blame] | 1373 | } |
Chris Lattner | 44b807e | 2005-08-08 22:32:34 +0000 | [diff] [blame] | 1374 | } |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 1375 | |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 1376 | // If one of the use is a PHI node and all other uses are addresses, still |
Evan Cheng | 32e4c7c | 2007-12-20 02:20:53 +0000 | [diff] [blame] | 1377 | // allow iv reuse. Essentially we are trading one constant multiplication |
| 1378 | // for one fewer iv. |
| 1379 | if (NumPHI > 1) |
| 1380 | AllUsesAreAddresses = false; |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 1381 | |
Evan Cheng | d33cec1 | 2009-02-20 22:16:49 +0000 | [diff] [blame] | 1382 | // There are no in-loop address uses. |
| 1383 | if (AllUsesAreAddresses && (!HasAddress && !AllUsesAreOutsideLoop)) |
| 1384 | AllUsesAreAddresses = false; |
| 1385 | |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1386 | return CommonExprs; |
| 1387 | } |
| 1388 | |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1389 | /// ShouldUseFullStrengthReductionMode - Test whether full strength-reduction |
| 1390 | /// is valid and profitable for the given set of users of a stride. In |
| 1391 | /// full strength-reduction mode, all addresses at the current stride are |
| 1392 | /// strength-reduced all the way down to pointer arithmetic. |
| 1393 | /// |
| 1394 | bool LoopStrengthReduce::ShouldUseFullStrengthReductionMode( |
| 1395 | const std::vector<BasedUser> &UsersToProcess, |
| 1396 | const Loop *L, |
| 1397 | bool AllUsesAreAddresses, |
| 1398 | SCEVHandle Stride) { |
| 1399 | if (!EnableFullLSRMode) |
| 1400 | return false; |
| 1401 | |
| 1402 | // The heuristics below aim to avoid increasing register pressure, but |
| 1403 | // fully strength-reducing all the addresses increases the number of |
| 1404 | // add instructions, so don't do this when optimizing for size. |
| 1405 | // TODO: If the loop is large, the savings due to simpler addresses |
| 1406 | // may oughtweight the costs of the extra increment instructions. |
| 1407 | if (L->getHeader()->getParent()->hasFnAttr(Attribute::OptimizeForSize)) |
| 1408 | return false; |
| 1409 | |
| 1410 | // TODO: For now, don't do full strength reduction if there could |
| 1411 | // potentially be greater-stride multiples of the current stride |
| 1412 | // which could reuse the current stride IV. |
| 1413 | if (StrideOrder.back() != Stride) |
| 1414 | return false; |
| 1415 | |
| 1416 | // Iterate through the uses to find conditions that automatically rule out |
| 1417 | // full-lsr mode. |
| 1418 | for (unsigned i = 0, e = UsersToProcess.size(); i != e; ) { |
| 1419 | SCEV *Base = UsersToProcess[i].Base; |
| 1420 | SCEV *Imm = UsersToProcess[i].Imm; |
| 1421 | // If any users have a loop-variant component, they can't be fully |
| 1422 | // strength-reduced. |
| 1423 | if (Imm && !Imm->isLoopInvariant(L)) |
| 1424 | return false; |
| 1425 | // If there are to users with the same base and the difference between |
| 1426 | // the two Imm values can't be folded into the address, full |
| 1427 | // strength reduction would increase register pressure. |
| 1428 | do { |
| 1429 | SCEV *CurImm = UsersToProcess[i].Imm; |
Dan Gohman | a04af43 | 2009-02-22 16:40:52 +0000 | [diff] [blame] | 1430 | if ((CurImm || Imm) && CurImm != Imm) { |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1431 | if (!CurImm) CurImm = SE->getIntegerSCEV(0, Stride->getType()); |
| 1432 | if (!Imm) Imm = SE->getIntegerSCEV(0, Stride->getType()); |
| 1433 | const Instruction *Inst = UsersToProcess[i].Inst; |
Dan Gohman | 21e7722 | 2009-03-09 21:01:17 +0000 | [diff] [blame] | 1434 | const Type *UseTy = getAccessType(Inst); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1435 | SCEVHandle Diff = SE->getMinusSCEV(UsersToProcess[i].Imm, Imm); |
| 1436 | if (!Diff->isZero() && |
| 1437 | (!AllUsesAreAddresses || |
| 1438 | !fitsInAddressMode(Diff, UseTy, TLI, /*HasBaseReg=*/true))) |
| 1439 | return false; |
| 1440 | } |
| 1441 | } while (++i != e && Base == UsersToProcess[i].Base); |
| 1442 | } |
| 1443 | |
| 1444 | // If there's exactly one user in this stride, fully strength-reducing it |
| 1445 | // won't increase register pressure. If it's starting from a non-zero base, |
| 1446 | // it'll be simpler this way. |
| 1447 | if (UsersToProcess.size() == 1 && !UsersToProcess[0].Base->isZero()) |
| 1448 | return true; |
| 1449 | |
| 1450 | // Otherwise, if there are any users in this stride that don't require |
| 1451 | // a register for their base, full strength-reduction will increase |
| 1452 | // register pressure. |
| 1453 | for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) |
Dan Gohman | f0baa6e | 2009-02-20 21:05:23 +0000 | [diff] [blame] | 1454 | if (UsersToProcess[i].Base->isZero()) |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1455 | return false; |
| 1456 | |
| 1457 | // Otherwise, go for it. |
| 1458 | return true; |
| 1459 | } |
| 1460 | |
| 1461 | /// InsertAffinePhi Create and insert a PHI node for an induction variable |
| 1462 | /// with the specified start and step values in the specified loop. |
| 1463 | /// |
| 1464 | /// If NegateStride is true, the stride should be negated by using a |
| 1465 | /// subtract instead of an add. |
| 1466 | /// |
Dan Gohman | 9d10086 | 2009-03-09 22:04:01 +0000 | [diff] [blame] | 1467 | /// Return the created phi node. |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1468 | /// |
| 1469 | static PHINode *InsertAffinePhi(SCEVHandle Start, SCEVHandle Step, |
| 1470 | const Loop *L, |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1471 | const TargetData *TD, |
Dan Gohman | 9d10086 | 2009-03-09 22:04:01 +0000 | [diff] [blame] | 1472 | SCEVExpander &Rewriter) { |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1473 | assert(Start->isLoopInvariant(L) && "New PHI start is not loop invariant!"); |
| 1474 | assert(Step->isLoopInvariant(L) && "New PHI stride is not loop invariant!"); |
| 1475 | |
| 1476 | BasicBlock *Header = L->getHeader(); |
| 1477 | BasicBlock *Preheader = L->getLoopPreheader(); |
Dan Gohman | 0daeed2 | 2009-03-09 21:14:16 +0000 | [diff] [blame] | 1478 | BasicBlock *LatchBlock = L->getLoopLatch(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1479 | const Type *Ty = Start->getType(); |
| 1480 | if (isa<PointerType>(Ty)) Ty = TD->getIntPtrType(); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1481 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1482 | PHINode *PN = PHINode::Create(Ty, "lsr.iv", Header->begin()); |
| 1483 | PN->addIncoming(Rewriter.expandCodeFor(Start, Ty, Preheader->getTerminator()), |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1484 | Preheader); |
| 1485 | |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1486 | // If the stride is negative, insert a sub instead of an add for the |
| 1487 | // increment. |
| 1488 | bool isNegative = isNonConstantNegative(Step); |
| 1489 | SCEVHandle IncAmount = Step; |
| 1490 | if (isNegative) |
| 1491 | IncAmount = Rewriter.SE.getNegativeSCEV(Step); |
| 1492 | |
| 1493 | // Insert an add instruction right before the terminator corresponding |
| 1494 | // to the back-edge. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1495 | Value *StepV = Rewriter.expandCodeFor(IncAmount, Ty, |
| 1496 | Preheader->getTerminator()); |
Dan Gohman | 9d10086 | 2009-03-09 22:04:01 +0000 | [diff] [blame] | 1497 | Instruction *IncV; |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1498 | if (isNegative) { |
| 1499 | IncV = BinaryOperator::CreateSub(PN, StepV, "lsr.iv.next", |
Dan Gohman | 0daeed2 | 2009-03-09 21:14:16 +0000 | [diff] [blame] | 1500 | LatchBlock->getTerminator()); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1501 | } else { |
| 1502 | IncV = BinaryOperator::CreateAdd(PN, StepV, "lsr.iv.next", |
Dan Gohman | 0daeed2 | 2009-03-09 21:14:16 +0000 | [diff] [blame] | 1503 | LatchBlock->getTerminator()); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1504 | } |
| 1505 | if (!isa<ConstantInt>(StepV)) ++NumVariable; |
| 1506 | |
Dan Gohman | 0daeed2 | 2009-03-09 21:14:16 +0000 | [diff] [blame] | 1507 | PN->addIncoming(IncV, LatchBlock); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1508 | |
| 1509 | ++NumInserted; |
| 1510 | return PN; |
| 1511 | } |
| 1512 | |
| 1513 | static void SortUsersToProcess(std::vector<BasedUser> &UsersToProcess) { |
| 1514 | // We want to emit code for users inside the loop first. To do this, we |
| 1515 | // rearrange BasedUser so that the entries at the end have |
| 1516 | // isUseOfPostIncrementedValue = false, because we pop off the end of the |
| 1517 | // vector (so we handle them first). |
| 1518 | std::partition(UsersToProcess.begin(), UsersToProcess.end(), |
| 1519 | PartitionByIsUseOfPostIncrementedValue); |
| 1520 | |
| 1521 | // Sort this by base, so that things with the same base are handled |
| 1522 | // together. By partitioning first and stable-sorting later, we are |
| 1523 | // guaranteed that within each base we will pop off users from within the |
| 1524 | // loop before users outside of the loop with a particular base. |
| 1525 | // |
| 1526 | // We would like to use stable_sort here, but we can't. The problem is that |
| 1527 | // SCEVHandle's don't have a deterministic ordering w.r.t to each other, so |
| 1528 | // we don't have anything to do a '<' comparison on. Because we think the |
| 1529 | // number of uses is small, do a horrible bubble sort which just relies on |
| 1530 | // ==. |
| 1531 | for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) { |
| 1532 | // Get a base value. |
| 1533 | SCEVHandle Base = UsersToProcess[i].Base; |
| 1534 | |
| 1535 | // Compact everything with this base to be consecutive with this one. |
| 1536 | for (unsigned j = i+1; j != e; ++j) { |
| 1537 | if (UsersToProcess[j].Base == Base) { |
| 1538 | std::swap(UsersToProcess[i+1], UsersToProcess[j]); |
| 1539 | ++i; |
| 1540 | } |
| 1541 | } |
| 1542 | } |
| 1543 | } |
| 1544 | |
Dan Gohman | 6b38e29 | 2009-02-20 21:06:57 +0000 | [diff] [blame] | 1545 | /// PrepareToStrengthReduceFully - Prepare to fully strength-reduce |
| 1546 | /// UsersToProcess, meaning lowering addresses all the way down to direct |
| 1547 | /// pointer arithmetic. |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1548 | /// |
| 1549 | void |
| 1550 | LoopStrengthReduce::PrepareToStrengthReduceFully( |
| 1551 | std::vector<BasedUser> &UsersToProcess, |
| 1552 | SCEVHandle Stride, |
| 1553 | SCEVHandle CommonExprs, |
| 1554 | const Loop *L, |
| 1555 | SCEVExpander &PreheaderRewriter) { |
| 1556 | DOUT << " Fully reducing all users\n"; |
| 1557 | |
| 1558 | // Rewrite the UsersToProcess records, creating a separate PHI for each |
| 1559 | // unique Base value. |
| 1560 | for (unsigned i = 0, e = UsersToProcess.size(); i != e; ) { |
| 1561 | // TODO: The uses are grouped by base, but not sorted. We arbitrarily |
| 1562 | // pick the first Imm value here to start with, and adjust it for the |
| 1563 | // other uses. |
| 1564 | SCEVHandle Imm = UsersToProcess[i].Imm; |
| 1565 | SCEVHandle Base = UsersToProcess[i].Base; |
| 1566 | SCEVHandle Start = SE->getAddExpr(CommonExprs, Base, Imm); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1567 | PHINode *Phi = InsertAffinePhi(Start, Stride, L, TD, |
Dan Gohman | 9d10086 | 2009-03-09 22:04:01 +0000 | [diff] [blame] | 1568 | PreheaderRewriter); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1569 | // Loop over all the users with the same base. |
| 1570 | do { |
| 1571 | UsersToProcess[i].Base = SE->getIntegerSCEV(0, Stride->getType()); |
| 1572 | UsersToProcess[i].Imm = SE->getMinusSCEV(UsersToProcess[i].Imm, Imm); |
| 1573 | UsersToProcess[i].Phi = Phi; |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1574 | assert(UsersToProcess[i].Imm->isLoopInvariant(L) && |
| 1575 | "ShouldUseFullStrengthReductionMode should reject this!"); |
| 1576 | } while (++i != e && Base == UsersToProcess[i].Base); |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | /// PrepareToStrengthReduceWithNewPhi - Insert a new induction variable for the |
| 1581 | /// given users to share. |
| 1582 | /// |
| 1583 | void |
| 1584 | LoopStrengthReduce::PrepareToStrengthReduceWithNewPhi( |
| 1585 | std::vector<BasedUser> &UsersToProcess, |
| 1586 | SCEVHandle Stride, |
| 1587 | SCEVHandle CommonExprs, |
| 1588 | Value *CommonBaseV, |
| 1589 | const Loop *L, |
| 1590 | SCEVExpander &PreheaderRewriter) { |
| 1591 | DOUT << " Inserting new PHI:\n"; |
| 1592 | |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1593 | PHINode *Phi = InsertAffinePhi(SE->getUnknown(CommonBaseV), |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1594 | Stride, L, TD, |
Dan Gohman | 9d10086 | 2009-03-09 22:04:01 +0000 | [diff] [blame] | 1595 | PreheaderRewriter); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1596 | |
| 1597 | // Remember this in case a later stride is multiple of this. |
Dan Gohman | 9d10086 | 2009-03-09 22:04:01 +0000 | [diff] [blame] | 1598 | IVsByStride[Stride].addIV(Stride, CommonExprs, Phi); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1599 | |
| 1600 | // All the users will share this new IV. |
Dan Gohman | 9d10086 | 2009-03-09 22:04:01 +0000 | [diff] [blame] | 1601 | for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1602 | UsersToProcess[i].Phi = Phi; |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1603 | |
| 1604 | DOUT << " IV="; |
| 1605 | DEBUG(WriteAsOperand(*DOUT, Phi, /*PrintType=*/false)); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1606 | DOUT << "\n"; |
| 1607 | } |
| 1608 | |
| 1609 | /// PrepareToStrengthReduceWithNewPhi - Prepare for the given users to reuse |
| 1610 | /// an induction variable with a stride that is a factor of the current |
| 1611 | /// induction variable. |
| 1612 | /// |
| 1613 | void |
| 1614 | LoopStrengthReduce::PrepareToStrengthReduceFromSmallerStride( |
| 1615 | std::vector<BasedUser> &UsersToProcess, |
| 1616 | Value *CommonBaseV, |
| 1617 | const IVExpr &ReuseIV, |
| 1618 | Instruction *PreInsertPt) { |
| 1619 | DOUT << " Rewriting in terms of existing IV of STRIDE " << *ReuseIV.Stride |
| 1620 | << " and BASE " << *ReuseIV.Base << "\n"; |
| 1621 | |
| 1622 | // All the users will share the reused IV. |
Dan Gohman | 9d10086 | 2009-03-09 22:04:01 +0000 | [diff] [blame] | 1623 | for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1624 | UsersToProcess[i].Phi = ReuseIV.PHI; |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1625 | |
| 1626 | Constant *C = dyn_cast<Constant>(CommonBaseV); |
| 1627 | if (C && |
| 1628 | (!C->isNullValue() && |
| 1629 | !fitsInAddressMode(SE->getUnknown(CommonBaseV), CommonBaseV->getType(), |
| 1630 | TLI, false))) |
| 1631 | // We want the common base emitted into the preheader! This is just |
| 1632 | // using cast as a copy so BitCast (no-op cast) is appropriate |
| 1633 | CommonBaseV = new BitCastInst(CommonBaseV, CommonBaseV->getType(), |
| 1634 | "commonbase", PreInsertPt); |
| 1635 | } |
| 1636 | |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 1637 | static bool IsImmFoldedIntoAddrMode(GlobalValue *GV, int64_t Offset, |
Dan Gohman | 53f2ae2 | 2009-03-09 21:04:19 +0000 | [diff] [blame] | 1638 | const Type *AccessTy, |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 1639 | std::vector<BasedUser> &UsersToProcess, |
| 1640 | const TargetLowering *TLI) { |
| 1641 | SmallVector<Instruction*, 16> AddrModeInsts; |
| 1642 | for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) { |
| 1643 | if (UsersToProcess[i].isUseOfPostIncrementedValue) |
| 1644 | continue; |
| 1645 | ExtAddrMode AddrMode = |
| 1646 | AddressingModeMatcher::Match(UsersToProcess[i].OperandValToReplace, |
Dan Gohman | 53f2ae2 | 2009-03-09 21:04:19 +0000 | [diff] [blame] | 1647 | AccessTy, UsersToProcess[i].Inst, |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 1648 | AddrModeInsts, *TLI); |
| 1649 | if (GV && GV != AddrMode.BaseGV) |
| 1650 | return false; |
| 1651 | if (Offset && !AddrMode.BaseOffs) |
| 1652 | // FIXME: How to accurate check it's immediate offset is folded. |
| 1653 | return false; |
| 1654 | AddrModeInsts.clear(); |
| 1655 | } |
| 1656 | return true; |
| 1657 | } |
| 1658 | |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1659 | /// StrengthReduceStridedIVUsers - Strength reduce all of the users of a single |
| 1660 | /// stride of IV. All of the users may have different starting values, and this |
Dan Gohman | 9f4ac31 | 2009-03-09 20:41:15 +0000 | [diff] [blame] | 1661 | /// may not be the only stride. |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1662 | void LoopStrengthReduce::StrengthReduceStridedIVUsers(const SCEVHandle &Stride, |
| 1663 | IVUsersOfOneStride &Uses, |
Dan Gohman | 9f4ac31 | 2009-03-09 20:41:15 +0000 | [diff] [blame] | 1664 | Loop *L) { |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1665 | // If all the users are moved to another stride, then there is nothing to do. |
Dan Gohman | 3035959 | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 1666 | if (Uses.Users.empty()) |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1667 | return; |
| 1668 | |
| 1669 | // Keep track if every use in UsersToProcess is an address. If they all are, |
| 1670 | // we may be able to rewrite the entire collection of them in terms of a |
| 1671 | // smaller-stride IV. |
| 1672 | bool AllUsesAreAddresses = true; |
| 1673 | |
Dale Johannesen | b039062 | 2008-12-16 22:16:28 +0000 | [diff] [blame] | 1674 | // Keep track if every use of a single stride is outside the loop. If so, |
| 1675 | // we want to be more aggressive about reusing a smaller-stride IV; a |
| 1676 | // multiply outside the loop is better than another IV inside. Well, usually. |
| 1677 | bool AllUsesAreOutsideLoop = true; |
| 1678 | |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1679 | // Transform our list of users and offsets to a bit more complex table. In |
| 1680 | // this new vector, each 'BasedUser' contains 'Base' the base of the |
| 1681 | // strided accessas well as the old information from Uses. We progressively |
| 1682 | // move information from the Base field to the Imm field, until we eventually |
| 1683 | // have the full access expression to rewrite the use. |
| 1684 | std::vector<BasedUser> UsersToProcess; |
| 1685 | SCEVHandle CommonExprs = CollectIVUsers(Stride, Uses, L, AllUsesAreAddresses, |
Dale Johannesen | b039062 | 2008-12-16 22:16:28 +0000 | [diff] [blame] | 1686 | AllUsesAreOutsideLoop, |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1687 | UsersToProcess); |
| 1688 | |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1689 | // Sort the UsersToProcess array so that users with common bases are |
| 1690 | // next to each other. |
| 1691 | SortUsersToProcess(UsersToProcess); |
| 1692 | |
Evan Cheng | 5f8ebaa | 2007-10-25 22:45:20 +0000 | [diff] [blame] | 1693 | // If we managed to find some expressions in common, we'll need to carry |
| 1694 | // their value in a register and add it in for each use. This will take up |
| 1695 | // a register operand, which potentially restricts what stride values are |
| 1696 | // valid. |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1697 | bool HaveCommonExprs = !CommonExprs->isZero(); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1698 | |
Chris Lattner | fe35555 | 2007-04-01 22:21:39 +0000 | [diff] [blame] | 1699 | const Type *ReplacedTy = CommonExprs->getType(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1700 | if (isa<PointerType>(ReplacedTy)) ReplacedTy = TD->getIntPtrType(); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1701 | |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 1702 | // If all uses are addresses, consider sinking the immediate part of the |
| 1703 | // common expression back into uses if they can fit in the immediate fields. |
Evan Cheng | 3cd389d | 2009-02-22 07:31:19 +0000 | [diff] [blame] | 1704 | if (TLI && HaveCommonExprs && AllUsesAreAddresses) { |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 1705 | SCEVHandle NewCommon = CommonExprs; |
| 1706 | SCEVHandle Imm = SE->getIntegerSCEV(0, ReplacedTy); |
Dan Gohman | 3cfe6a4 | 2009-03-09 21:22:12 +0000 | [diff] [blame] | 1707 | MoveImmediateValues(TLI, Type::VoidTy, NewCommon, Imm, true, L, SE); |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 1708 | if (!Imm->isZero()) { |
| 1709 | bool DoSink = true; |
| 1710 | |
| 1711 | // If the immediate part of the common expression is a GV, check if it's |
| 1712 | // possible to fold it into the target addressing mode. |
| 1713 | GlobalValue *GV = 0; |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1714 | if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(Imm)) |
| 1715 | GV = dyn_cast<GlobalValue>(SU->getValue()); |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 1716 | int64_t Offset = 0; |
| 1717 | if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Imm)) |
| 1718 | Offset = SC->getValue()->getSExtValue(); |
| 1719 | if (GV || Offset) |
Dan Gohman | 53f2ae2 | 2009-03-09 21:04:19 +0000 | [diff] [blame] | 1720 | // Pass VoidTy as the AccessTy to be conservative, because |
| 1721 | // there could be multiple access types among all the uses. |
| 1722 | DoSink = IsImmFoldedIntoAddrMode(GV, Offset, Type::VoidTy, |
Evan Cheng | d9fb712 | 2009-02-21 02:06:47 +0000 | [diff] [blame] | 1723 | UsersToProcess, TLI); |
| 1724 | |
| 1725 | if (DoSink) { |
| 1726 | DOUT << " Sinking " << *Imm << " back down into uses\n"; |
| 1727 | for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) |
| 1728 | UsersToProcess[i].Imm = SE->getAddExpr(UsersToProcess[i].Imm, Imm); |
| 1729 | CommonExprs = NewCommon; |
| 1730 | HaveCommonExprs = !CommonExprs->isZero(); |
| 1731 | ++NumImmSunk; |
| 1732 | } |
| 1733 | } |
| 1734 | } |
| 1735 | |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1736 | // Now that we know what we need to do, insert the PHI node itself. |
| 1737 | // |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 1738 | DOUT << "LSR: Examining IVs of TYPE " << *ReplacedTy << " of STRIDE " |
| 1739 | << *Stride << ":\n" |
| 1740 | << " Common base: " << *CommonExprs << "\n"; |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 1741 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1742 | SCEVExpander Rewriter(*SE, *LI, *TD); |
| 1743 | SCEVExpander PreheaderRewriter(*SE, *LI, *TD); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1744 | |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1745 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 1746 | Instruction *PreInsertPt = Preheader->getTerminator(); |
Chris Lattner | 12b5041 | 2005-09-12 17:11:27 +0000 | [diff] [blame] | 1747 | BasicBlock *LatchBlock = L->getLoopLatch(); |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 1748 | |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1749 | Value *CommonBaseV = ConstantInt::get(ReplacedTy, 0); |
Evan Cheng | eb8f9e2 | 2006-03-17 19:52:23 +0000 | [diff] [blame] | 1750 | |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1751 | SCEVHandle RewriteFactor = SE->getIntegerSCEV(0, ReplacedTy); |
| 1752 | IVExpr ReuseIV(SE->getIntegerSCEV(0, Type::Int32Ty), |
| 1753 | SE->getIntegerSCEV(0, Type::Int32Ty), |
Dan Gohman | 9d10086 | 2009-03-09 22:04:01 +0000 | [diff] [blame] | 1754 | 0); |
Evan Cheng | eb8f9e2 | 2006-03-17 19:52:23 +0000 | [diff] [blame] | 1755 | |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1756 | /// Choose a strength-reduction strategy and prepare for it by creating |
| 1757 | /// the necessary PHIs and adjusting the bookkeeping. |
| 1758 | if (ShouldUseFullStrengthReductionMode(UsersToProcess, L, |
| 1759 | AllUsesAreAddresses, Stride)) { |
| 1760 | PrepareToStrengthReduceFully(UsersToProcess, Stride, CommonExprs, L, |
| 1761 | PreheaderRewriter); |
Evan Cheng | eb8f9e2 | 2006-03-17 19:52:23 +0000 | [diff] [blame] | 1762 | } else { |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1763 | // Emit the initial base value into the loop preheader. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1764 | CommonBaseV = PreheaderRewriter.expandCodeFor(CommonExprs, ReplacedTy, |
| 1765 | PreInsertPt); |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 1766 | |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1767 | // If all uses are addresses, check if it is possible to reuse an IV with a |
| 1768 | // stride that is a factor of this stride. And that the multiple is a number |
| 1769 | // that can be encoded in the scale field of the target addressing mode. And |
Dan Gohman | 6b38e29 | 2009-02-20 21:06:57 +0000 | [diff] [blame] | 1770 | // that we will have a valid instruction after this substition, including |
| 1771 | // the immediate field, if any. |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1772 | RewriteFactor = CheckForIVReuse(HaveCommonExprs, AllUsesAreAddresses, |
| 1773 | AllUsesAreOutsideLoop, |
Dan Gohman | bb5b49c | 2009-03-09 21:19:58 +0000 | [diff] [blame] | 1774 | Stride, ReuseIV, ReplacedTy, |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1775 | UsersToProcess); |
| 1776 | if (isa<SCEVConstant>(RewriteFactor) && |
| 1777 | cast<SCEVConstant>(RewriteFactor)->isZero()) |
| 1778 | PrepareToStrengthReduceWithNewPhi(UsersToProcess, Stride, CommonExprs, |
| 1779 | CommonBaseV, L, PreheaderRewriter); |
| 1780 | else |
| 1781 | PrepareToStrengthReduceFromSmallerStride(UsersToProcess, CommonBaseV, |
| 1782 | ReuseIV, PreInsertPt); |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 1783 | } |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1784 | |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1785 | // Process all the users now, replacing their strided uses with |
| 1786 | // strength-reduced forms. This outer loop handles all bases, the inner |
Chris Lattner | 7e79b38 | 2006-08-03 06:34:50 +0000 | [diff] [blame] | 1787 | // loop handles all users of a particular base. |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1788 | while (!UsersToProcess.empty()) { |
Chris Lattner | 7b445c5 | 2005-10-11 18:30:57 +0000 | [diff] [blame] | 1789 | SCEVHandle Base = UsersToProcess.back().Base; |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 1790 | Instruction *Inst = UsersToProcess.back().Inst; |
Chris Lattner | be3e521 | 2005-08-03 23:30:08 +0000 | [diff] [blame] | 1791 | |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1792 | // Emit the code for Base into the preheader. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1793 | Value *BaseV = 0; |
| 1794 | if (!Base->isZero()) { |
| 1795 | BaseV = PreheaderRewriter.expandCodeFor(Base, Base->getType(), |
| 1796 | PreInsertPt); |
Chris Lattner | 7d8ed8a | 2007-05-11 22:40:34 +0000 | [diff] [blame] | 1797 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1798 | DOUT << " INSERTING code for BASE = " << *Base << ":"; |
| 1799 | if (BaseV->hasName()) |
| 1800 | DOUT << " Result value name = %" << BaseV->getNameStr(); |
| 1801 | DOUT << "\n"; |
Chris Lattner | 7d8ed8a | 2007-05-11 22:40:34 +0000 | [diff] [blame] | 1802 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1803 | // If BaseV is a non-zero constant, make sure that it gets inserted into |
| 1804 | // the preheader, instead of being forward substituted into the uses. We |
| 1805 | // do this by forcing a BitCast (noop cast) to be inserted into the |
| 1806 | // preheader in this case. |
| 1807 | if (!fitsInAddressMode(Base, getAccessType(Inst), TLI, false)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1808 | // We want this constant emitted into the preheader! This is just |
| 1809 | // using cast as a copy so BitCast (no-op cast) is appropriate |
| 1810 | BaseV = new BitCastInst(BaseV, BaseV->getType(), "preheaderinsert", |
Dan Gohman | 4a9a3e5 | 2008-04-14 18:26:16 +0000 | [diff] [blame] | 1811 | PreInsertPt); |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1812 | } |
Chris Lattner | 7e79b38 | 2006-08-03 06:34:50 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1815 | // Emit the code to add the immediate offset to the Phi value, just before |
Chris Lattner | 2351aba | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 1816 | // the instructions that we identified as using this stride and base. |
Chris Lattner | 7b445c5 | 2005-10-11 18:30:57 +0000 | [diff] [blame] | 1817 | do { |
Chris Lattner | 7e79b38 | 2006-08-03 06:34:50 +0000 | [diff] [blame] | 1818 | // FIXME: Use emitted users to emit other users. |
Chris Lattner | 7b445c5 | 2005-10-11 18:30:57 +0000 | [diff] [blame] | 1819 | BasedUser &User = UsersToProcess.back(); |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 1820 | |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 1821 | DOUT << " Examining use "; |
Dan Gohman | 4a359ea | 2009-02-19 19:32:06 +0000 | [diff] [blame] | 1822 | DEBUG(WriteAsOperand(*DOUT, UsersToProcess.back().OperandValToReplace, |
| 1823 | /*PrintType=*/false)); |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 1824 | DOUT << " in Inst: " << *Inst; |
Dan Gohman | 2f09f51 | 2009-02-19 19:23:27 +0000 | [diff] [blame] | 1825 | |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1826 | // If this instruction wants to use the post-incremented value, move it |
| 1827 | // after the post-inc and use its value instead of the PHI. |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1828 | Value *RewriteOp = User.Phi; |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1829 | if (User.isUseOfPostIncrementedValue) { |
Dan Gohman | 9d10086 | 2009-03-09 22:04:01 +0000 | [diff] [blame] | 1830 | RewriteOp = User.Phi->getIncomingValueForBlock(LatchBlock); |
Chris Lattner | c6bae65 | 2005-09-12 06:04:47 +0000 | [diff] [blame] | 1831 | |
| 1832 | // If this user is in the loop, make sure it is the last thing in the |
| 1833 | // loop to ensure it is dominated by the increment. |
| 1834 | if (L->contains(User.Inst->getParent())) |
| 1835 | User.Inst->moveBefore(LatchBlock->getTerminator()); |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1836 | } |
Evan Cheng | 86c75d3 | 2006-06-09 00:12:42 +0000 | [diff] [blame] | 1837 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1838 | SCEVHandle RewriteExpr = SE->getUnknown(RewriteOp); |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1839 | |
Dan Gohman | e616bf3 | 2009-04-16 15:47:35 +0000 | [diff] [blame] | 1840 | if (TD->getTypeSizeInBits(RewriteOp->getType()) != |
| 1841 | TD->getTypeSizeInBits(ReplacedTy)) { |
| 1842 | assert(TD->getTypeSizeInBits(RewriteOp->getType()) > |
| 1843 | TD->getTypeSizeInBits(ReplacedTy) && |
| 1844 | "Unexpected widening cast!"); |
| 1845 | RewriteExpr = SE->getTruncateExpr(RewriteExpr, ReplacedTy); |
| 1846 | } |
| 1847 | |
Dale Johannesen | b039062 | 2008-12-16 22:16:28 +0000 | [diff] [blame] | 1848 | // If we had to insert new instructions for RewriteOp, we have to |
Dan Gohman | f20d70d | 2008-05-15 23:26:57 +0000 | [diff] [blame] | 1849 | // consider that they may not have been able to end up immediately |
| 1850 | // next to RewriteOp, because non-PHI instructions may never precede |
| 1851 | // PHI instructions in a block. In this case, remember where the last |
Dan Gohman | ca756ae | 2008-05-20 03:01:48 +0000 | [diff] [blame] | 1852 | // instruction was inserted so that if we're replacing a different |
| 1853 | // PHI node, we can use the later point to expand the final |
| 1854 | // RewriteExpr. |
Dan Gohman | f20d70d | 2008-05-15 23:26:57 +0000 | [diff] [blame] | 1855 | Instruction *NewBasePt = dyn_cast<Instruction>(RewriteOp); |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 1856 | if (RewriteOp == User.Phi) NewBasePt = 0; |
Dan Gohman | f20d70d | 2008-05-15 23:26:57 +0000 | [diff] [blame] | 1857 | |
Chris Lattner | 2351aba | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 1858 | // Clear the SCEVExpander's expression map so that we are guaranteed |
| 1859 | // to have the code emitted where we expect it. |
| 1860 | Rewriter.clear(); |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 1861 | |
| 1862 | // If we are reusing the iv, then it must be multiplied by a constant |
Dale Johannesen | 1de17d5 | 2009-02-09 22:14:15 +0000 | [diff] [blame] | 1863 | // factor to take advantage of the addressing mode scale component. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1864 | if (!RewriteFactor->isZero()) { |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 1865 | // If we're reusing an IV with a nonzero base (currently this happens |
| 1866 | // only when all reuses are outside the loop) subtract that base here. |
| 1867 | // The base has been used to initialize the PHI node but we don't want |
| 1868 | // it here. |
Dale Johannesen | 1de17d5 | 2009-02-09 22:14:15 +0000 | [diff] [blame] | 1869 | if (!ReuseIV.Base->isZero()) { |
| 1870 | SCEVHandle typedBase = ReuseIV.Base; |
Dan Gohman | 84fc33e | 2009-04-16 22:35:57 +0000 | [diff] [blame^] | 1871 | if (TD->getTypeSizeInBits(RewriteExpr->getType()) != |
| 1872 | TD->getTypeSizeInBits(ReuseIV.Base->getType())) { |
Dale Johannesen | 1de17d5 | 2009-02-09 22:14:15 +0000 | [diff] [blame] | 1873 | // It's possible the original IV is a larger type than the new IV, |
| 1874 | // in which case we have to truncate the Base. We checked in |
| 1875 | // RequiresTypeConversion that this is valid. |
Dan Gohman | 84fc33e | 2009-04-16 22:35:57 +0000 | [diff] [blame^] | 1876 | assert(TD->getTypeSizeInBits(RewriteExpr->getType()) < |
| 1877 | TD->getTypeSizeInBits(ReuseIV.Base->getType()) && |
| 1878 | "Unexpected lengthening conversion!"); |
Dale Johannesen | 1de17d5 | 2009-02-09 22:14:15 +0000 | [diff] [blame] | 1879 | typedBase = SE->getTruncateExpr(ReuseIV.Base, |
| 1880 | RewriteExpr->getType()); |
| 1881 | } |
| 1882 | RewriteExpr = SE->getMinusSCEV(RewriteExpr, typedBase); |
| 1883 | } |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 1884 | |
| 1885 | // Multiply old variable, with base removed, by new scale factor. |
| 1886 | RewriteExpr = SE->getMulExpr(RewriteFactor, |
Evan Cheng | 8392772 | 2007-10-30 22:27:26 +0000 | [diff] [blame] | 1887 | RewriteExpr); |
Evan Cheng | eb8f9e2 | 2006-03-17 19:52:23 +0000 | [diff] [blame] | 1888 | |
| 1889 | // The common base is emitted in the loop preheader. But since we |
| 1890 | // are reusing an IV, it has not been used to initialize the PHI node. |
| 1891 | // Add it to the expression used to rewrite the uses. |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 1892 | // When this use is outside the loop, we earlier subtracted the |
| 1893 | // common base, and are adding it back here. Use the same expression |
| 1894 | // as before, rather than CommonBaseV, so DAGCombiner will zap it. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1895 | if (!CommonExprs->isZero()) { |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 1896 | if (L->contains(User.Inst->getParent())) |
| 1897 | RewriteExpr = SE->getAddExpr(RewriteExpr, |
Dale Johannesen | b039062 | 2008-12-16 22:16:28 +0000 | [diff] [blame] | 1898 | SE->getUnknown(CommonBaseV)); |
Dale Johannesen | 2f46bb8 | 2009-01-14 02:35:31 +0000 | [diff] [blame] | 1899 | else |
| 1900 | RewriteExpr = SE->getAddExpr(RewriteExpr, CommonExprs); |
| 1901 | } |
Evan Cheng | eb8f9e2 | 2006-03-17 19:52:23 +0000 | [diff] [blame] | 1902 | } |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | 2114b27 | 2005-08-04 20:03:32 +0000 | [diff] [blame] | 1904 | // Now that we know what we need to do, insert code before User for the |
| 1905 | // immediate and any loop-variant expressions. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1906 | if (BaseV) |
Chris Lattner | 1bbae0c | 2005-08-09 00:18:09 +0000 | [diff] [blame] | 1907 | // Add BaseV to the PHI value if needed. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1908 | RewriteExpr = SE->getAddExpr(RewriteExpr, SE->getUnknown(BaseV)); |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 1909 | |
Dan Gohman | f20d70d | 2008-05-15 23:26:57 +0000 | [diff] [blame] | 1910 | User.RewriteInstructionToUseNewBase(RewriteExpr, NewBasePt, |
| 1911 | Rewriter, L, this, |
Evan Cheng | 0e0014d | 2007-10-30 23:45:15 +0000 | [diff] [blame] | 1912 | DeadInsts); |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 1913 | |
Chris Lattner | a68d4ca | 2008-12-01 06:14:28 +0000 | [diff] [blame] | 1914 | // Mark old value we replaced as possibly dead, so that it is eliminated |
Chris Lattner | 2351aba | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 1915 | // if we just replaced the last use of that value. |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 1916 | DeadInsts.push_back(cast<Instruction>(User.OperandValToReplace)); |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1917 | |
Chris Lattner | 7b445c5 | 2005-10-11 18:30:57 +0000 | [diff] [blame] | 1918 | UsersToProcess.pop_back(); |
Chris Lattner | 2351aba | 2005-08-03 22:51:21 +0000 | [diff] [blame] | 1919 | ++NumReduced; |
Chris Lattner | 7b445c5 | 2005-10-11 18:30:57 +0000 | [diff] [blame] | 1920 | |
Chris Lattner | 7e79b38 | 2006-08-03 06:34:50 +0000 | [diff] [blame] | 1921 | // If there are any more users to process with the same base, process them |
| 1922 | // now. We sorted by base above, so we just have to check the last elt. |
Chris Lattner | 7b445c5 | 2005-10-11 18:30:57 +0000 | [diff] [blame] | 1923 | } while (!UsersToProcess.empty() && UsersToProcess.back().Base == Base); |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1924 | // TODO: Next, find out which base index is the most common, pull it out. |
| 1925 | } |
| 1926 | |
| 1927 | // IMPORTANT TODO: Figure out how to partition the IV's with this stride, but |
| 1928 | // different starting values, into different PHIs. |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Devang Patel | c677de2 | 2008-08-13 20:31:11 +0000 | [diff] [blame] | 1931 | /// FindIVUserForCond - If Cond has an operand that is an expression of an IV, |
Chris Lattner | aed01d1 | 2007-04-03 05:11:24 +0000 | [diff] [blame] | 1932 | /// set the IV user and stride information and return true, otherwise return |
| 1933 | /// false. |
Devang Patel | c677de2 | 2008-08-13 20:31:11 +0000 | [diff] [blame] | 1934 | bool LoopStrengthReduce::FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse, |
Chris Lattner | aed01d1 | 2007-04-03 05:11:24 +0000 | [diff] [blame] | 1935 | const SCEVHandle *&CondStride) { |
| 1936 | for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e && !CondUse; |
| 1937 | ++Stride) { |
| 1938 | std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI = |
| 1939 | IVUsesByStride.find(StrideOrder[Stride]); |
| 1940 | assert(SI != IVUsesByStride.end() && "Stride doesn't exist!"); |
| 1941 | |
| 1942 | for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(), |
| 1943 | E = SI->second.Users.end(); UI != E; ++UI) |
| 1944 | if (UI->User == Cond) { |
| 1945 | // NOTE: we could handle setcc instructions with multiple uses here, but |
| 1946 | // InstCombine does it as well for simple uses, it's not clear that it |
| 1947 | // occurs enough in real life to handle. |
| 1948 | CondUse = &*UI; |
| 1949 | CondStride = &SI->first; |
| 1950 | return true; |
| 1951 | } |
| 1952 | } |
| 1953 | return false; |
| 1954 | } |
| 1955 | |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 1956 | namespace { |
| 1957 | // Constant strides come first which in turns are sorted by their absolute |
| 1958 | // values. If absolute values are the same, then positive strides comes first. |
| 1959 | // e.g. |
| 1960 | // 4, -1, X, 1, 2 ==> 1, -1, 2, 4, X |
| 1961 | struct StrideCompare { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1962 | const TargetData *TD; |
| 1963 | explicit StrideCompare(const TargetData *td) : TD(td) {} |
| 1964 | |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 1965 | bool operator()(const SCEVHandle &LHS, const SCEVHandle &RHS) { |
| 1966 | SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS); |
| 1967 | SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS); |
| 1968 | if (LHSC && RHSC) { |
| 1969 | int64_t LV = LHSC->getValue()->getSExtValue(); |
| 1970 | int64_t RV = RHSC->getValue()->getSExtValue(); |
| 1971 | uint64_t ALV = (LV < 0) ? -LV : LV; |
| 1972 | uint64_t ARV = (RV < 0) ? -RV : RV; |
Dan Gohman | bc51172 | 2009-02-13 00:26:43 +0000 | [diff] [blame] | 1973 | if (ALV == ARV) { |
| 1974 | if (LV != RV) |
| 1975 | return LV > RV; |
| 1976 | } else { |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 1977 | return ALV < ARV; |
Dan Gohman | bc51172 | 2009-02-13 00:26:43 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | // If it's the same value but different type, sort by bit width so |
| 1981 | // that we emit larger induction variables before smaller |
| 1982 | // ones, letting the smaller be re-written in terms of larger ones. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1983 | return TD->getTypeSizeInBits(RHS->getType()) < |
| 1984 | TD->getTypeSizeInBits(LHS->getType()); |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 1985 | } |
Dan Gohman | bc51172 | 2009-02-13 00:26:43 +0000 | [diff] [blame] | 1986 | return LHSC && !RHSC; |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 1987 | } |
| 1988 | }; |
| 1989 | } |
| 1990 | |
| 1991 | /// ChangeCompareStride - If a loop termination compare instruction is the |
| 1992 | /// only use of its stride, and the compaison is against a constant value, |
| 1993 | /// try eliminate the stride by moving the compare instruction to another |
| 1994 | /// stride and change its constant operand accordingly. e.g. |
| 1995 | /// |
| 1996 | /// loop: |
| 1997 | /// ... |
| 1998 | /// v1 = v1 + 3 |
| 1999 | /// v2 = v2 + 1 |
| 2000 | /// if (v2 < 10) goto loop |
| 2001 | /// => |
| 2002 | /// loop: |
| 2003 | /// ... |
| 2004 | /// v1 = v1 + 3 |
| 2005 | /// if (v1 < 30) goto loop |
| 2006 | ICmpInst *LoopStrengthReduce::ChangeCompareStride(Loop *L, ICmpInst *Cond, |
Evan Cheng | 0e0014d | 2007-10-30 23:45:15 +0000 | [diff] [blame] | 2007 | IVStrideUse* &CondUse, |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2008 | const SCEVHandle* &CondStride) { |
| 2009 | if (StrideOrder.size() < 2 || |
| 2010 | IVUsesByStride[*CondStride].Users.size() != 1) |
| 2011 | return Cond; |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2012 | const SCEVConstant *SC = dyn_cast<SCEVConstant>(*CondStride); |
| 2013 | if (!SC) return Cond; |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2014 | |
| 2015 | ICmpInst::Predicate Predicate = Cond->getPredicate(); |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2016 | int64_t CmpSSInt = SC->getValue()->getSExtValue(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2017 | unsigned BitWidth = TD->getTypeSizeInBits((*CondStride)->getType()); |
Evan Cheng | 168a66b | 2007-10-26 23:08:19 +0000 | [diff] [blame] | 2018 | uint64_t SignBit = 1ULL << (BitWidth-1); |
Dan Gohman | c34fea3 | 2009-02-24 01:58:00 +0000 | [diff] [blame] | 2019 | const Type *CmpTy = Cond->getOperand(0)->getType(); |
Evan Cheng | 168a66b | 2007-10-26 23:08:19 +0000 | [diff] [blame] | 2020 | const Type *NewCmpTy = NULL; |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2021 | unsigned TyBits = TD->getTypeSizeInBits(CmpTy); |
Evan Cheng | af62c09 | 2007-10-29 22:07:18 +0000 | [diff] [blame] | 2022 | unsigned NewTyBits = 0; |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2023 | SCEVHandle *NewStride = NULL; |
Dan Gohman | ff518c8 | 2009-02-20 21:27:23 +0000 | [diff] [blame] | 2024 | Value *NewCmpLHS = NULL; |
| 2025 | Value *NewCmpRHS = NULL; |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2026 | int64_t Scale = 1; |
Dan Gohman | c34fea3 | 2009-02-24 01:58:00 +0000 | [diff] [blame] | 2027 | SCEVHandle NewOffset = SE->getIntegerSCEV(0, UIntPtrTy); |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2028 | |
Dan Gohman | c34fea3 | 2009-02-24 01:58:00 +0000 | [diff] [blame] | 2029 | if (ConstantInt *C = dyn_cast<ConstantInt>(Cond->getOperand(1))) { |
| 2030 | int64_t CmpVal = C->getValue().getSExtValue(); |
Evan Cheng | 168a66b | 2007-10-26 23:08:19 +0000 | [diff] [blame] | 2031 | |
Dan Gohman | c34fea3 | 2009-02-24 01:58:00 +0000 | [diff] [blame] | 2032 | // Check stride constant and the comparision constant signs to detect |
| 2033 | // overflow. |
| 2034 | if ((CmpVal & SignBit) != (CmpSSInt & SignBit)) |
| 2035 | return Cond; |
Evan Cheng | 168a66b | 2007-10-26 23:08:19 +0000 | [diff] [blame] | 2036 | |
Dan Gohman | c34fea3 | 2009-02-24 01:58:00 +0000 | [diff] [blame] | 2037 | // Look for a suitable stride / iv as replacement. |
| 2038 | for (unsigned i = 0, e = StrideOrder.size(); i != e; ++i) { |
| 2039 | std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI = |
| 2040 | IVUsesByStride.find(StrideOrder[i]); |
| 2041 | if (!isa<SCEVConstant>(SI->first)) |
Dan Gohman | ff518c8 | 2009-02-20 21:27:23 +0000 | [diff] [blame] | 2042 | continue; |
Dan Gohman | c34fea3 | 2009-02-24 01:58:00 +0000 | [diff] [blame] | 2043 | int64_t SSInt = cast<SCEVConstant>(SI->first)->getValue()->getSExtValue(); |
| 2044 | if (abs(SSInt) <= abs(CmpSSInt) || (SSInt % CmpSSInt) != 0) |
| 2045 | continue; |
| 2046 | |
| 2047 | Scale = SSInt / CmpSSInt; |
| 2048 | int64_t NewCmpVal = CmpVal * Scale; |
| 2049 | APInt Mul = APInt(BitWidth, NewCmpVal); |
| 2050 | // Check for overflow. |
| 2051 | if (Mul.getSExtValue() != NewCmpVal) |
| 2052 | continue; |
| 2053 | |
| 2054 | // Watch out for overflow. |
| 2055 | if (ICmpInst::isSignedPredicate(Predicate) && |
| 2056 | (CmpVal & SignBit) != (NewCmpVal & SignBit)) |
| 2057 | continue; |
| 2058 | |
| 2059 | if (NewCmpVal == CmpVal) |
| 2060 | continue; |
| 2061 | // Pick the best iv to use trying to avoid a cast. |
| 2062 | NewCmpLHS = NULL; |
| 2063 | for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(), |
| 2064 | E = SI->second.Users.end(); UI != E; ++UI) { |
| 2065 | NewCmpLHS = UI->OperandValToReplace; |
| 2066 | if (NewCmpLHS->getType() == CmpTy) |
| 2067 | break; |
| 2068 | } |
| 2069 | if (!NewCmpLHS) |
| 2070 | continue; |
| 2071 | |
| 2072 | NewCmpTy = NewCmpLHS->getType(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2073 | NewTyBits = TD->getTypeSizeInBits(NewCmpTy); |
Dan Gohman | c34fea3 | 2009-02-24 01:58:00 +0000 | [diff] [blame] | 2074 | if (RequiresTypeConversion(NewCmpTy, CmpTy)) { |
| 2075 | // Check if it is possible to rewrite it using |
| 2076 | // an iv / stride of a smaller integer type. |
Dan Gohman | 65e05b6 | 2009-04-16 16:49:48 +0000 | [diff] [blame] | 2077 | unsigned Bits = NewTyBits; |
| 2078 | if (ICmpInst::isSignedPredicate(Predicate)) |
| 2079 | --Bits; |
| 2080 | uint64_t Mask = (1ULL << Bits) - 1; |
| 2081 | if (((uint64_t)NewCmpVal & Mask) != (uint64_t)NewCmpVal) |
Dan Gohman | c34fea3 | 2009-02-24 01:58:00 +0000 | [diff] [blame] | 2082 | continue; |
| 2083 | } |
| 2084 | |
| 2085 | // Don't rewrite if use offset is non-constant and the new type is |
| 2086 | // of a different type. |
| 2087 | // FIXME: too conservative? |
| 2088 | if (NewTyBits != TyBits && !isa<SCEVConstant>(CondUse->Offset)) |
| 2089 | continue; |
| 2090 | |
| 2091 | bool AllUsesAreAddresses = true; |
| 2092 | bool AllUsesAreOutsideLoop = true; |
| 2093 | std::vector<BasedUser> UsersToProcess; |
| 2094 | SCEVHandle CommonExprs = CollectIVUsers(SI->first, SI->second, L, |
| 2095 | AllUsesAreAddresses, |
| 2096 | AllUsesAreOutsideLoop, |
| 2097 | UsersToProcess); |
| 2098 | // Avoid rewriting the compare instruction with an iv of new stride |
| 2099 | // if it's likely the new stride uses will be rewritten using the |
| 2100 | // stride of the compare instruction. |
| 2101 | if (AllUsesAreAddresses && |
| 2102 | ValidStride(!CommonExprs->isZero(), Scale, UsersToProcess)) |
| 2103 | continue; |
| 2104 | |
| 2105 | // If scale is negative, use swapped predicate unless it's testing |
| 2106 | // for equality. |
| 2107 | if (Scale < 0 && !Cond->isEquality()) |
| 2108 | Predicate = ICmpInst::getSwappedPredicate(Predicate); |
| 2109 | |
| 2110 | NewStride = &StrideOrder[i]; |
| 2111 | if (!isa<PointerType>(NewCmpTy)) |
| 2112 | NewCmpRHS = ConstantInt::get(NewCmpTy, NewCmpVal); |
| 2113 | else { |
Dan Gohman | 798d392 | 2009-04-16 15:48:38 +0000 | [diff] [blame] | 2114 | ConstantInt *CI = ConstantInt::get(UIntPtrTy, NewCmpVal); |
| 2115 | NewCmpRHS = ConstantExpr::getIntToPtr(CI, NewCmpTy); |
Dan Gohman | c34fea3 | 2009-02-24 01:58:00 +0000 | [diff] [blame] | 2116 | } |
| 2117 | NewOffset = TyBits == NewTyBits |
| 2118 | ? SE->getMulExpr(CondUse->Offset, |
| 2119 | SE->getConstant(ConstantInt::get(CmpTy, Scale))) |
| 2120 | : SE->getConstant(ConstantInt::get(NewCmpTy, |
| 2121 | cast<SCEVConstant>(CondUse->Offset)->getValue()->getSExtValue()*Scale)); |
| 2122 | break; |
Dan Gohman | ff518c8 | 2009-02-20 21:27:23 +0000 | [diff] [blame] | 2123 | } |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Dan Gohman | 9b93dd1 | 2008-06-16 22:34:15 +0000 | [diff] [blame] | 2126 | // Forgo this transformation if it the increment happens to be |
| 2127 | // unfortunately positioned after the condition, and the condition |
| 2128 | // has multiple uses which prevent it from being moved immediately |
| 2129 | // before the branch. See |
| 2130 | // test/Transforms/LoopStrengthReduce/change-compare-stride-trickiness-*.ll |
| 2131 | // for an example of this situation. |
Devang Patel | d16aba2 | 2008-08-13 02:05:14 +0000 | [diff] [blame] | 2132 | if (!Cond->hasOneUse()) { |
Dan Gohman | 9b93dd1 | 2008-06-16 22:34:15 +0000 | [diff] [blame] | 2133 | for (BasicBlock::iterator I = Cond, E = Cond->getParent()->end(); |
| 2134 | I != E; ++I) |
Dan Gohman | ff518c8 | 2009-02-20 21:27:23 +0000 | [diff] [blame] | 2135 | if (I == NewCmpLHS) |
Dan Gohman | 9b93dd1 | 2008-06-16 22:34:15 +0000 | [diff] [blame] | 2136 | return Cond; |
Devang Patel | d16aba2 | 2008-08-13 02:05:14 +0000 | [diff] [blame] | 2137 | } |
Dan Gohman | 9b93dd1 | 2008-06-16 22:34:15 +0000 | [diff] [blame] | 2138 | |
Dan Gohman | ff518c8 | 2009-02-20 21:27:23 +0000 | [diff] [blame] | 2139 | if (NewCmpRHS) { |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2140 | // Create a new compare instruction using new stride / iv. |
| 2141 | ICmpInst *OldCond = Cond; |
Evan Cheng | 168a66b | 2007-10-26 23:08:19 +0000 | [diff] [blame] | 2142 | // Insert new compare instruction. |
Dan Gohman | ff518c8 | 2009-02-20 21:27:23 +0000 | [diff] [blame] | 2143 | Cond = new ICmpInst(Predicate, NewCmpLHS, NewCmpRHS, |
Dan Gohman | e562b17 | 2008-06-13 21:43:41 +0000 | [diff] [blame] | 2144 | L->getHeader()->getName() + ".termcond", |
| 2145 | OldCond); |
Evan Cheng | 168a66b | 2007-10-26 23:08:19 +0000 | [diff] [blame] | 2146 | |
| 2147 | // Remove the old compare instruction. The old indvar is probably dead too. |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 2148 | DeadInsts.push_back(cast<Instruction>(CondUse->OperandValToReplace)); |
Evan Cheng | 168a66b | 2007-10-26 23:08:19 +0000 | [diff] [blame] | 2149 | SE->deleteValueFromRecords(OldCond); |
Dan Gohman | 010ee2d | 2008-05-21 00:54:12 +0000 | [diff] [blame] | 2150 | OldCond->replaceAllUsesWith(Cond); |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2151 | OldCond->eraseFromParent(); |
Evan Cheng | 168a66b | 2007-10-26 23:08:19 +0000 | [diff] [blame] | 2152 | |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2153 | IVUsesByStride[*CondStride].Users.pop_back(); |
Dan Gohman | ff518c8 | 2009-02-20 21:27:23 +0000 | [diff] [blame] | 2154 | IVUsesByStride[*NewStride].addUser(NewOffset, Cond, NewCmpLHS); |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2155 | CondUse = &IVUsesByStride[*NewStride].Users.back(); |
| 2156 | CondStride = NewStride; |
| 2157 | ++NumEliminated; |
| 2158 | } |
| 2159 | |
| 2160 | return Cond; |
| 2161 | } |
| 2162 | |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 2163 | /// OptimizeSMax - Rewrite the loop's terminating condition if it uses |
| 2164 | /// an smax computation. |
| 2165 | /// |
| 2166 | /// This is a narrow solution to a specific, but acute, problem. For loops |
| 2167 | /// like this: |
| 2168 | /// |
| 2169 | /// i = 0; |
| 2170 | /// do { |
| 2171 | /// p[i] = 0.0; |
| 2172 | /// } while (++i < n); |
| 2173 | /// |
| 2174 | /// where the comparison is signed, the trip count isn't just 'n', because |
| 2175 | /// 'n' could be negative. And unfortunately this can come up even for loops |
| 2176 | /// where the user didn't use a C do-while loop. For example, seemingly |
| 2177 | /// well-behaved top-test loops will commonly be lowered like this: |
| 2178 | // |
| 2179 | /// if (n > 0) { |
| 2180 | /// i = 0; |
| 2181 | /// do { |
| 2182 | /// p[i] = 0.0; |
| 2183 | /// } while (++i < n); |
| 2184 | /// } |
| 2185 | /// |
| 2186 | /// and then it's possible for subsequent optimization to obscure the if |
| 2187 | /// test in such a way that indvars can't find it. |
| 2188 | /// |
| 2189 | /// When indvars can't find the if test in loops like this, it creates a |
| 2190 | /// signed-max expression, which allows it to give the loop a canonical |
| 2191 | /// induction variable: |
| 2192 | /// |
| 2193 | /// i = 0; |
| 2194 | /// smax = n < 1 ? 1 : n; |
| 2195 | /// do { |
| 2196 | /// p[i] = 0.0; |
| 2197 | /// } while (++i != smax); |
| 2198 | /// |
| 2199 | /// Canonical induction variables are necessary because the loop passes |
| 2200 | /// are designed around them. The most obvious example of this is the |
| 2201 | /// LoopInfo analysis, which doesn't remember trip count values. It |
| 2202 | /// expects to be able to rediscover the trip count each time it is |
| 2203 | /// needed, and it does this using a simple analyis that only succeeds if |
| 2204 | /// the loop has a canonical induction variable. |
| 2205 | /// |
| 2206 | /// However, when it comes time to generate code, the maximum operation |
| 2207 | /// can be quite costly, especially if it's inside of an outer loop. |
| 2208 | /// |
| 2209 | /// This function solves this problem by detecting this type of loop and |
| 2210 | /// rewriting their conditions from ICMP_NE back to ICMP_SLT, and deleting |
| 2211 | /// the instructions for the maximum computation. |
| 2212 | /// |
| 2213 | ICmpInst *LoopStrengthReduce::OptimizeSMax(Loop *L, ICmpInst *Cond, |
| 2214 | IVStrideUse* &CondUse) { |
| 2215 | // Check that the loop matches the pattern we're looking for. |
| 2216 | if (Cond->getPredicate() != CmpInst::ICMP_EQ && |
| 2217 | Cond->getPredicate() != CmpInst::ICMP_NE) |
| 2218 | return Cond; |
| 2219 | |
| 2220 | SelectInst *Sel = dyn_cast<SelectInst>(Cond->getOperand(1)); |
| 2221 | if (!Sel || !Sel->hasOneUse()) return Cond; |
| 2222 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2223 | SCEVHandle BackedgeTakenCount = SE->getBackedgeTakenCount(L); |
| 2224 | if (isa<SCEVCouldNotCompute>(BackedgeTakenCount)) |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 2225 | return Cond; |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2226 | SCEVHandle One = SE->getIntegerSCEV(1, BackedgeTakenCount->getType()); |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 2227 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2228 | // Add one to the backedge-taken count to get the trip count. |
| 2229 | SCEVHandle IterationCount = SE->getAddExpr(BackedgeTakenCount, One); |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 2230 | |
| 2231 | // Check for a max calculation that matches the pattern. |
| 2232 | SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(IterationCount); |
| 2233 | if (!SMax || SMax != SE->getSCEV(Sel)) return Cond; |
| 2234 | |
| 2235 | SCEVHandle SMaxLHS = SMax->getOperand(0); |
| 2236 | SCEVHandle SMaxRHS = SMax->getOperand(1); |
| 2237 | if (!SMaxLHS || SMaxLHS != One) return Cond; |
| 2238 | |
| 2239 | // Check the relevant induction variable for conformance to |
| 2240 | // the pattern. |
| 2241 | SCEVHandle IV = SE->getSCEV(Cond->getOperand(0)); |
| 2242 | SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(IV); |
| 2243 | if (!AR || !AR->isAffine() || |
| 2244 | AR->getStart() != One || |
| 2245 | AR->getStepRecurrence(*SE) != One) |
| 2246 | return Cond; |
| 2247 | |
Dan Gohman | bc10b8c | 2009-03-04 20:49:01 +0000 | [diff] [blame] | 2248 | assert(AR->getLoop() == L && |
| 2249 | "Loop condition operand is an addrec in a different loop!"); |
| 2250 | |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 2251 | // Check the right operand of the select, and remember it, as it will |
| 2252 | // be used in the new comparison instruction. |
| 2253 | Value *NewRHS = 0; |
| 2254 | if (SE->getSCEV(Sel->getOperand(1)) == SMaxRHS) |
| 2255 | NewRHS = Sel->getOperand(1); |
| 2256 | else if (SE->getSCEV(Sel->getOperand(2)) == SMaxRHS) |
| 2257 | NewRHS = Sel->getOperand(2); |
| 2258 | if (!NewRHS) return Cond; |
| 2259 | |
| 2260 | // Ok, everything looks ok to change the condition into an SLT or SGE and |
| 2261 | // delete the max calculation. |
| 2262 | ICmpInst *NewCond = |
| 2263 | new ICmpInst(Cond->getPredicate() == CmpInst::ICMP_NE ? |
| 2264 | CmpInst::ICMP_SLT : |
| 2265 | CmpInst::ICMP_SGE, |
| 2266 | Cond->getOperand(0), NewRHS, "scmp", Cond); |
| 2267 | |
| 2268 | // Delete the max calculation instructions. |
Dan Gohman | 586b7b7 | 2008-10-01 02:02:03 +0000 | [diff] [blame] | 2269 | SE->deleteValueFromRecords(Cond); |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 2270 | Cond->replaceAllUsesWith(NewCond); |
| 2271 | Cond->eraseFromParent(); |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 2272 | Instruction *Cmp = cast<Instruction>(Sel->getOperand(0)); |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 2273 | SE->deleteValueFromRecords(Sel); |
Dan Gohman | 586b7b7 | 2008-10-01 02:02:03 +0000 | [diff] [blame] | 2274 | Sel->eraseFromParent(); |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 2275 | if (Cmp->use_empty()) { |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 2276 | SE->deleteValueFromRecords(Cmp); |
Dan Gohman | 586b7b7 | 2008-10-01 02:02:03 +0000 | [diff] [blame] | 2277 | Cmp->eraseFromParent(); |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 2278 | } |
| 2279 | CondUse->User = NewCond; |
| 2280 | return NewCond; |
| 2281 | } |
| 2282 | |
Devang Patel | a0b3909 | 2008-08-26 17:57:54 +0000 | [diff] [blame] | 2283 | /// OptimizeShadowIV - If IV is used in a int-to-float cast |
| 2284 | /// inside the loop then try to eliminate the cast opeation. |
| 2285 | void LoopStrengthReduce::OptimizeShadowIV(Loop *L) { |
| 2286 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2287 | SCEVHandle BackedgeTakenCount = SE->getBackedgeTakenCount(L); |
| 2288 | if (isa<SCEVCouldNotCompute>(BackedgeTakenCount)) |
Devang Patel | a0b3909 | 2008-08-26 17:57:54 +0000 | [diff] [blame] | 2289 | return; |
| 2290 | |
| 2291 | for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e; |
| 2292 | ++Stride) { |
| 2293 | std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI = |
| 2294 | IVUsesByStride.find(StrideOrder[Stride]); |
| 2295 | assert(SI != IVUsesByStride.end() && "Stride doesn't exist!"); |
| 2296 | if (!isa<SCEVConstant>(SI->first)) |
| 2297 | continue; |
| 2298 | |
| 2299 | for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(), |
| 2300 | E = SI->second.Users.end(); UI != E; /* empty */) { |
| 2301 | std::vector<IVStrideUse>::iterator CandidateUI = UI; |
Devang Patel | 5415327 | 2008-08-27 17:50:18 +0000 | [diff] [blame] | 2302 | ++UI; |
Devang Patel | a0b3909 | 2008-08-26 17:57:54 +0000 | [diff] [blame] | 2303 | Instruction *ShadowUse = CandidateUI->User; |
| 2304 | const Type *DestTy = NULL; |
| 2305 | |
| 2306 | /* If shadow use is a int->float cast then insert a second IV |
Devang Patel | 5415327 | 2008-08-27 17:50:18 +0000 | [diff] [blame] | 2307 | to eliminate this cast. |
Devang Patel | a0b3909 | 2008-08-26 17:57:54 +0000 | [diff] [blame] | 2308 | |
| 2309 | for (unsigned i = 0; i < n; ++i) |
| 2310 | foo((double)i); |
| 2311 | |
Devang Patel | 5415327 | 2008-08-27 17:50:18 +0000 | [diff] [blame] | 2312 | is transformed into |
Devang Patel | a0b3909 | 2008-08-26 17:57:54 +0000 | [diff] [blame] | 2313 | |
| 2314 | double d = 0.0; |
| 2315 | for (unsigned i = 0; i < n; ++i, ++d) |
| 2316 | foo(d); |
| 2317 | */ |
Devang Patel | 5415327 | 2008-08-27 17:50:18 +0000 | [diff] [blame] | 2318 | if (UIToFPInst *UCast = dyn_cast<UIToFPInst>(CandidateUI->User)) |
Devang Patel | a0b3909 | 2008-08-26 17:57:54 +0000 | [diff] [blame] | 2319 | DestTy = UCast->getDestTy(); |
Devang Patel | 5415327 | 2008-08-27 17:50:18 +0000 | [diff] [blame] | 2320 | else if (SIToFPInst *SCast = dyn_cast<SIToFPInst>(CandidateUI->User)) |
Devang Patel | a0b3909 | 2008-08-26 17:57:54 +0000 | [diff] [blame] | 2321 | DestTy = SCast->getDestTy(); |
Devang Patel | 18bb278 | 2008-08-27 20:55:23 +0000 | [diff] [blame] | 2322 | if (!DestTy) continue; |
| 2323 | |
| 2324 | if (TLI) { |
| 2325 | /* If target does not support DestTy natively then do not apply |
| 2326 | this transformation. */ |
| 2327 | MVT DVT = TLI->getValueType(DestTy); |
| 2328 | if (!TLI->isTypeLegal(DVT)) continue; |
| 2329 | } |
| 2330 | |
Devang Patel | a0b3909 | 2008-08-26 17:57:54 +0000 | [diff] [blame] | 2331 | PHINode *PH = dyn_cast<PHINode>(ShadowUse->getOperand(0)); |
| 2332 | if (!PH) continue; |
| 2333 | if (PH->getNumIncomingValues() != 2) continue; |
| 2334 | |
| 2335 | const Type *SrcTy = PH->getType(); |
| 2336 | int Mantissa = DestTy->getFPMantissaWidth(); |
| 2337 | if (Mantissa == -1) continue; |
| 2338 | if ((int)TD->getTypeSizeInBits(SrcTy) > Mantissa) |
| 2339 | continue; |
| 2340 | |
| 2341 | unsigned Entry, Latch; |
| 2342 | if (PH->getIncomingBlock(0) == L->getLoopPreheader()) { |
| 2343 | Entry = 0; |
| 2344 | Latch = 1; |
| 2345 | } else { |
| 2346 | Entry = 1; |
| 2347 | Latch = 0; |
| 2348 | } |
| 2349 | |
| 2350 | ConstantInt *Init = dyn_cast<ConstantInt>(PH->getIncomingValue(Entry)); |
| 2351 | if (!Init) continue; |
| 2352 | ConstantFP *NewInit = ConstantFP::get(DestTy, Init->getZExtValue()); |
| 2353 | |
| 2354 | BinaryOperator *Incr = |
| 2355 | dyn_cast<BinaryOperator>(PH->getIncomingValue(Latch)); |
| 2356 | if (!Incr) continue; |
| 2357 | if (Incr->getOpcode() != Instruction::Add |
| 2358 | && Incr->getOpcode() != Instruction::Sub) |
| 2359 | continue; |
| 2360 | |
| 2361 | /* Initialize new IV, double d = 0.0 in above example. */ |
| 2362 | ConstantInt *C = NULL; |
| 2363 | if (Incr->getOperand(0) == PH) |
| 2364 | C = dyn_cast<ConstantInt>(Incr->getOperand(1)); |
| 2365 | else if (Incr->getOperand(1) == PH) |
| 2366 | C = dyn_cast<ConstantInt>(Incr->getOperand(0)); |
| 2367 | else |
| 2368 | continue; |
| 2369 | |
| 2370 | if (!C) continue; |
| 2371 | |
| 2372 | /* Add new PHINode. */ |
| 2373 | PHINode *NewPH = PHINode::Create(DestTy, "IV.S.", PH); |
| 2374 | |
Devang Patel | 5415327 | 2008-08-27 17:50:18 +0000 | [diff] [blame] | 2375 | /* create new increment. '++d' in above example. */ |
Devang Patel | a0b3909 | 2008-08-26 17:57:54 +0000 | [diff] [blame] | 2376 | ConstantFP *CFP = ConstantFP::get(DestTy, C->getZExtValue()); |
| 2377 | BinaryOperator *NewIncr = |
| 2378 | BinaryOperator::Create(Incr->getOpcode(), |
| 2379 | NewPH, CFP, "IV.S.next.", Incr); |
| 2380 | |
| 2381 | NewPH->addIncoming(NewInit, PH->getIncomingBlock(Entry)); |
| 2382 | NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch)); |
| 2383 | |
| 2384 | /* Remove cast operation */ |
| 2385 | SE->deleteValueFromRecords(ShadowUse); |
| 2386 | ShadowUse->replaceAllUsesWith(NewPH); |
| 2387 | ShadowUse->eraseFromParent(); |
| 2388 | SI->second.Users.erase(CandidateUI); |
| 2389 | NumShadow++; |
| 2390 | break; |
| 2391 | } |
| 2392 | } |
| 2393 | } |
| 2394 | |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2395 | // OptimizeIndvars - Now that IVUsesByStride is set up with all of the indvar |
| 2396 | // uses in the loop, look to see if we can eliminate some, in favor of using |
| 2397 | // common indvars for the different uses. |
| 2398 | void LoopStrengthReduce::OptimizeIndvars(Loop *L) { |
| 2399 | // TODO: implement optzns here. |
| 2400 | |
Devang Patel | a0b3909 | 2008-08-26 17:57:54 +0000 | [diff] [blame] | 2401 | OptimizeShadowIV(L); |
| 2402 | |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2403 | // Finally, get the terminating condition for the loop if possible. If we |
| 2404 | // can, we want to change it to use a post-incremented version of its |
Chris Lattner | 98d9811 | 2006-03-24 07:14:34 +0000 | [diff] [blame] | 2405 | // induction variable, to allow coalescing the live ranges for the IV into |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2406 | // one register value. |
| 2407 | PHINode *SomePHI = cast<PHINode>(L->getHeader()->begin()); |
| 2408 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 2409 | BasicBlock *LatchBlock = |
| 2410 | SomePHI->getIncomingBlock(SomePHI->getIncomingBlock(0) == Preheader); |
| 2411 | BranchInst *TermBr = dyn_cast<BranchInst>(LatchBlock->getTerminator()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2412 | if (!TermBr || TermBr->isUnconditional() || |
| 2413 | !isa<ICmpInst>(TermBr->getCondition())) |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2414 | return; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2415 | ICmpInst *Cond = cast<ICmpInst>(TermBr->getCondition()); |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2416 | |
| 2417 | // Search IVUsesByStride to find Cond's IVUse if there is one. |
| 2418 | IVStrideUse *CondUse = 0; |
Chris Lattner | 50fad70 | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 2419 | const SCEVHandle *CondStride = 0; |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2420 | |
Devang Patel | c677de2 | 2008-08-13 20:31:11 +0000 | [diff] [blame] | 2421 | if (!FindIVUserForCond(Cond, CondUse, CondStride)) |
Chris Lattner | aed01d1 | 2007-04-03 05:11:24 +0000 | [diff] [blame] | 2422 | return; // setcc doesn't use the IV. |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2423 | |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 2424 | // If the trip count is computed in terms of an smax (due to ScalarEvolution |
| 2425 | // being unable to find a sufficient guard, for example), change the loop |
| 2426 | // comparison to use SLT instead of NE. |
| 2427 | Cond = OptimizeSMax(L, Cond, CondUse); |
| 2428 | |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 2429 | // If possible, change stride and operands of the compare instruction to |
| 2430 | // eliminate one stride. |
| 2431 | Cond = ChangeCompareStride(L, Cond, CondUse, CondStride); |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2432 | |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2433 | // It's possible for the setcc instruction to be anywhere in the loop, and |
| 2434 | // possible for it to have multiple users. If it is not immediately before |
| 2435 | // the latch block branch, move it. |
| 2436 | if (&*++BasicBlock::iterator(Cond) != (Instruction*)TermBr) { |
| 2437 | if (Cond->hasOneUse()) { // Condition has a single use, just move it. |
| 2438 | Cond->moveBefore(TermBr); |
| 2439 | } else { |
| 2440 | // Otherwise, clone the terminating condition and insert into the loopend. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2441 | Cond = cast<ICmpInst>(Cond->clone()); |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2442 | Cond->setName(L->getHeader()->getName() + ".termcond"); |
| 2443 | LatchBlock->getInstList().insert(TermBr, Cond); |
| 2444 | |
| 2445 | // Clone the IVUse, as the old use still exists! |
Chris Lattner | 50fad70 | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 2446 | IVUsesByStride[*CondStride].addUser(CondUse->Offset, Cond, |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2447 | CondUse->OperandValToReplace); |
Chris Lattner | 50fad70 | 2005-08-10 00:45:21 +0000 | [diff] [blame] | 2448 | CondUse = &IVUsesByStride[*CondStride].Users.back(); |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | // If we get to here, we know that we can transform the setcc instruction to |
Chris Lattner | 98d9811 | 2006-03-24 07:14:34 +0000 | [diff] [blame] | 2453 | // use the post-incremented version of the IV, allowing us to coalesce the |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2454 | // live ranges for the IV correctly. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 2455 | CondUse->Offset = SE->getMinusSCEV(CondUse->Offset, *CondStride); |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2456 | CondUse->isUseOfPostIncrementedValue = true; |
Evan Cheng | 1ce75dc | 2008-07-07 19:51:32 +0000 | [diff] [blame] | 2457 | Changed = true; |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2458 | } |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 2459 | |
Devang Patel | 0f54dcb | 2007-03-06 21:14:09 +0000 | [diff] [blame] | 2460 | bool LoopStrengthReduce::runOnLoop(Loop *L, LPPassManager &LPM) { |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 2461 | |
Devang Patel | 0f54dcb | 2007-03-06 21:14:09 +0000 | [diff] [blame] | 2462 | LI = &getAnalysis<LoopInfo>(); |
Devang Patel | b7d9dfc | 2007-06-07 21:42:15 +0000 | [diff] [blame] | 2463 | DT = &getAnalysis<DominatorTree>(); |
Devang Patel | 0f54dcb | 2007-03-06 21:14:09 +0000 | [diff] [blame] | 2464 | SE = &getAnalysis<ScalarEvolution>(); |
| 2465 | TD = &getAnalysis<TargetData>(); |
| 2466 | UIntPtrTy = TD->getIntPtrType(); |
Dan Gohman | 3fea643 | 2008-07-14 17:55:01 +0000 | [diff] [blame] | 2467 | Changed = false; |
Devang Patel | 0f54dcb | 2007-03-06 21:14:09 +0000 | [diff] [blame] | 2468 | |
Dale Johannesen | b039062 | 2008-12-16 22:16:28 +0000 | [diff] [blame] | 2469 | // Find all uses of induction variables in this loop, and categorize |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 2470 | // them by stride. Start by finding all of the PHI nodes in the header for |
| 2471 | // this loop. If they are induction variables, inspect their uses. |
Evan Cheng | 168a66b | 2007-10-26 23:08:19 +0000 | [diff] [blame] | 2472 | SmallPtrSet<Instruction*,16> Processed; // Don't reprocess instructions. |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 2473 | for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) |
Chris Lattner | 3416e5f | 2005-08-04 17:40:30 +0000 | [diff] [blame] | 2474 | AddUsersIfInteresting(I, L, Processed); |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 2475 | |
Evan Cheng | 1ce75dc | 2008-07-07 19:51:32 +0000 | [diff] [blame] | 2476 | if (!IVUsesByStride.empty()) { |
Dan Gohman | 80b0f8c | 2009-03-09 20:34:59 +0000 | [diff] [blame] | 2477 | #ifndef NDEBUG |
| 2478 | DOUT << "\nLSR on \"" << L->getHeader()->getParent()->getNameStart() |
| 2479 | << "\" "; |
| 2480 | DEBUG(L->dump()); |
| 2481 | #endif |
| 2482 | |
Dan Gohman | f7912df | 2009-03-09 20:46:50 +0000 | [diff] [blame] | 2483 | // Sort the StrideOrder so we process larger strides first. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2484 | std::stable_sort(StrideOrder.begin(), StrideOrder.end(), StrideCompare(TD)); |
Dan Gohman | f7912df | 2009-03-09 20:46:50 +0000 | [diff] [blame] | 2485 | |
Evan Cheng | 1ce75dc | 2008-07-07 19:51:32 +0000 | [diff] [blame] | 2486 | // Optimize induction variables. Some indvar uses can be transformed to use |
| 2487 | // strides that will be needed for other purposes. A common example of this |
| 2488 | // is the exit test for the loop, which can often be rewritten to use the |
| 2489 | // computation of some other indvar to decide when to terminate the loop. |
| 2490 | OptimizeIndvars(L); |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2491 | |
Evan Cheng | 1ce75dc | 2008-07-07 19:51:32 +0000 | [diff] [blame] | 2492 | // FIXME: We can widen subreg IV's here for RISC targets. e.g. instead of |
| 2493 | // doing computation in byte values, promote to 32-bit values if safe. |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 2494 | |
Evan Cheng | 1ce75dc | 2008-07-07 19:51:32 +0000 | [diff] [blame] | 2495 | // FIXME: Attempt to reuse values across multiple IV's. In particular, we |
| 2496 | // could have something like "for(i) { foo(i*8); bar(i*16) }", which should |
| 2497 | // be codegened as "for (j = 0;; j+=8) { foo(j); bar(j+j); }" on X86/PPC. |
| 2498 | // Need to be careful that IV's are all the same type. Only works for |
| 2499 | // intptr_t indvars. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2500 | |
Evan Cheng | 1ce75dc | 2008-07-07 19:51:32 +0000 | [diff] [blame] | 2501 | // IVsByStride keeps IVs for one particular loop. |
| 2502 | assert(IVsByStride.empty() && "Stale entries in IVsByStride?"); |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 2503 | |
Evan Cheng | 1ce75dc | 2008-07-07 19:51:32 +0000 | [diff] [blame] | 2504 | // Note: this processes each stride/type pair individually. All users |
| 2505 | // passed into StrengthReduceStridedIVUsers have the same type AND stride. |
| 2506 | // Also, note that we iterate over IVUsesByStride indirectly by using |
| 2507 | // StrideOrder. This extra layer of indirection makes the ordering of |
| 2508 | // strides deterministic - not dependent on map order. |
| 2509 | for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e; ++Stride) { |
| 2510 | std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI = |
| 2511 | IVUsesByStride.find(StrideOrder[Stride]); |
| 2512 | assert(SI != IVUsesByStride.end() && "Stride doesn't exist!"); |
Dan Gohman | 9f4ac31 | 2009-03-09 20:41:15 +0000 | [diff] [blame] | 2513 | StrengthReduceStridedIVUsers(SI->first, SI->second, L); |
Evan Cheng | 1ce75dc | 2008-07-07 19:51:32 +0000 | [diff] [blame] | 2514 | } |
Chris Lattner | 7305ae2 | 2005-10-09 06:20:55 +0000 | [diff] [blame] | 2515 | } |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 2516 | |
Dan Gohman | 010ee2d | 2008-05-21 00:54:12 +0000 | [diff] [blame] | 2517 | // We're done analyzing this loop; release all the state we built up for it. |
Dan Gohman | 010ee2d | 2008-05-21 00:54:12 +0000 | [diff] [blame] | 2518 | IVUsesByStride.clear(); |
| 2519 | IVsByStride.clear(); |
| 2520 | StrideOrder.clear(); |
| 2521 | |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 2522 | // Clean up after ourselves |
| 2523 | if (!DeadInsts.empty()) { |
Chris Lattner | a68d4ca | 2008-12-01 06:14:28 +0000 | [diff] [blame] | 2524 | DeleteTriviallyDeadInstructions(); |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 2525 | |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 2526 | BasicBlock::iterator I = L->getHeader()->begin(); |
Dan Gohman | cbfe5bb | 2008-06-22 20:44:02 +0000 | [diff] [blame] | 2527 | while (PHINode *PN = dyn_cast<PHINode>(I++)) { |
| 2528 | // At this point, we know that we have killed one or more IV users. |
Chris Lattner | bfcee36 | 2008-12-01 06:11:32 +0000 | [diff] [blame] | 2529 | // It is worth checking to see if the cannonical indvar is also |
Dan Gohman | cbfe5bb | 2008-06-22 20:44:02 +0000 | [diff] [blame] | 2530 | // dead, so that we can remove it as well. |
| 2531 | // |
| 2532 | // We can remove a PHI if it is on a cycle in the def-use graph |
| 2533 | // where each node in the cycle has degree one, i.e. only one use, |
| 2534 | // and is an instruction with no side effects. |
| 2535 | // |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 2536 | // FIXME: this needs to eliminate an induction variable even if it's being |
| 2537 | // compared against some value to decide loop termination. |
Chris Lattner | a0d4486 | 2008-11-27 23:00:20 +0000 | [diff] [blame] | 2538 | if (!PN->hasOneUse()) |
| 2539 | continue; |
| 2540 | |
| 2541 | SmallPtrSet<PHINode *, 4> PHIs; |
| 2542 | for (Instruction *J = dyn_cast<Instruction>(*PN->use_begin()); |
| 2543 | J && J->hasOneUse() && !J->mayWriteToMemory(); |
| 2544 | J = dyn_cast<Instruction>(*J->use_begin())) { |
| 2545 | // If we find the original PHI, we've discovered a cycle. |
| 2546 | if (J == PN) { |
| 2547 | // Break the cycle and mark the PHI for deletion. |
| 2548 | SE->deleteValueFromRecords(PN); |
| 2549 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
Chris Lattner | 09fb7da | 2008-12-01 06:27:41 +0000 | [diff] [blame] | 2550 | DeadInsts.push_back(PN); |
Chris Lattner | a0d4486 | 2008-11-27 23:00:20 +0000 | [diff] [blame] | 2551 | Changed = true; |
| 2552 | break; |
Chris Lattner | 7e608bb | 2005-08-02 02:52:02 +0000 | [diff] [blame] | 2553 | } |
Chris Lattner | a0d4486 | 2008-11-27 23:00:20 +0000 | [diff] [blame] | 2554 | // If we find a PHI more than once, we're on a cycle that |
| 2555 | // won't prove fruitful. |
| 2556 | if (isa<PHINode>(J) && !PHIs.insert(cast<PHINode>(J))) |
| 2557 | break; |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 2558 | } |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 2559 | } |
Chris Lattner | a68d4ca | 2008-12-01 06:14:28 +0000 | [diff] [blame] | 2560 | DeleteTriviallyDeadInstructions(); |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 2561 | } |
Evan Cheng | 1ce75dc | 2008-07-07 19:51:32 +0000 | [diff] [blame] | 2562 | return Changed; |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 2563 | } |