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 | // |
Dan Gohman | cec8f9d | 2009-05-19 20:37:36 +0000 | [diff] [blame] | 10 | // This transformation analyzes and transforms the induction variables (and |
| 11 | // computations derived from them) into forms suitable for efficient execution |
| 12 | // on the target. |
| 13 | // |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 14 | // This pass performs a strength reduction on array references inside loops that |
Dan Gohman | cec8f9d | 2009-05-19 20:37:36 +0000 | [diff] [blame] | 15 | // have as one or more of their components the loop induction variable, it |
| 16 | // rewrites expressions to take advantage of scaled-index addressing modes |
| 17 | // available on the target, and it performs a variety of other optimizations |
| 18 | // related to loop induction variables. |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 19 | // |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 20 | // Terminology note: this code has a lot of handling for "post-increment" or |
| 21 | // "post-inc" users. This is not talking about post-increment addressing modes; |
| 22 | // it is instead talking about code like this: |
| 23 | // |
| 24 | // %i = phi [ 0, %entry ], [ %i.next, %latch ] |
| 25 | // ... |
| 26 | // %i.next = add %i, 1 |
| 27 | // %c = icmp eq %i.next, %n |
| 28 | // |
| 29 | // The SCEV for %i is {0,+,1}<%L>. The SCEV for %i.next is {1,+,1}<%L>, however |
| 30 | // it's useful to think about these as the same register, with some uses using |
| 31 | // the value of the register before the add and some using // it after. In this |
| 32 | // example, the icmp is a post-increment user, since it uses %i.next, which is |
| 33 | // the value of the induction variable after the increment. The other common |
| 34 | // case of post-increment users is users outside the loop. |
| 35 | // |
| 36 | // TODO: More sophistication in the way Formulae are generated and filtered. |
| 37 | // |
| 38 | // TODO: Handle multiple loops at a time. |
| 39 | // |
| 40 | // TODO: Should TargetLowering::AddrMode::BaseGV be changed to a ConstantExpr |
| 41 | // instead of a GlobalValue? |
| 42 | // |
| 43 | // TODO: When truncation is free, truncate ICmp users' operands to make it a |
| 44 | // smaller encoding (on x86 at least). |
| 45 | // |
| 46 | // TODO: When a negated register is used by an add (such as in a list of |
| 47 | // multiple base registers, or as the increment expression in an addrec), |
| 48 | // we may not actually need both reg and (-1 * reg) in registers; the |
| 49 | // negation can be implemented by using a sub instead of an add. The |
| 50 | // lack of support for taking this into consideration when making |
| 51 | // register pressure decisions is partly worked around by the "Special" |
| 52 | // use kind. |
| 53 | // |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
| 55 | |
Chris Lattner | be3e521 | 2005-08-03 23:30:08 +0000 | [diff] [blame] | 56 | #define DEBUG_TYPE "loop-reduce" |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 57 | #include "llvm/Transforms/Scalar.h" |
| 58 | #include "llvm/Constants.h" |
| 59 | #include "llvm/Instructions.h" |
Dan Gohman | e5b01be | 2007-05-04 14:59:09 +0000 | [diff] [blame] | 60 | #include "llvm/IntrinsicInst.h" |
Jeff Cohen | 2f3c9b7 | 2005-03-04 04:04:26 +0000 | [diff] [blame] | 61 | #include "llvm/DerivedTypes.h" |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 62 | #include "llvm/Analysis/IVUsers.h" |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 63 | #include "llvm/Analysis/Dominators.h" |
Devang Patel | 0f54dcb | 2007-03-06 21:14:09 +0000 | [diff] [blame] | 64 | #include "llvm/Analysis/LoopPass.h" |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 65 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Chris Lattner | 9fc5cdf | 2011-01-02 22:09:33 +0000 | [diff] [blame] | 66 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | e0391be | 2005-08-12 22:06:11 +0000 | [diff] [blame] | 67 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 68 | #include "llvm/Transforms/Utils/Local.h" |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 69 | #include "llvm/ADT/SmallBitVector.h" |
| 70 | #include "llvm/ADT/SetVector.h" |
| 71 | #include "llvm/ADT/DenseSet.h" |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 72 | #include "llvm/Support/Debug.h" |
Andrew Trick | 80ef1b2 | 2011-09-27 00:44:14 +0000 | [diff] [blame] | 73 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 74 | #include "llvm/Support/ValueHandle.h" |
Daniel Dunbar | 460f656 | 2009-07-26 09:48:23 +0000 | [diff] [blame] | 75 | #include "llvm/Support/raw_ostream.h" |
Evan Cheng | d277f2c | 2006-03-13 23:14:23 +0000 | [diff] [blame] | 76 | #include "llvm/Target/TargetLowering.h" |
Jeff Cohen | cfb1d42 | 2005-07-30 18:22:27 +0000 | [diff] [blame] | 77 | #include <algorithm> |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 78 | using namespace llvm; |
| 79 | |
Benjamin Kramer | 0861f57 | 2011-11-26 23:01:57 +0000 | [diff] [blame] | 80 | static cl::opt<bool> EnableNested( |
Andrew Trick | 0c01bc3 | 2011-09-29 01:33:38 +0000 | [diff] [blame] | 81 | "enable-lsr-nested", cl::Hidden, cl::desc("Enable LSR on nested loops")); |
| 82 | |
Benjamin Kramer | 0861f57 | 2011-11-26 23:01:57 +0000 | [diff] [blame] | 83 | static cl::opt<bool> EnableRetry( |
| 84 | "enable-lsr-retry", cl::Hidden, cl::desc("Enable LSR retry")); |
Andrew Trick | a02bfce | 2011-10-11 02:30:45 +0000 | [diff] [blame] | 85 | |
| 86 | // Temporary flag to cleanup congruent phis after LSR phi expansion. |
| 87 | // It's currently disabled until we can determine whether it's truly useful or |
| 88 | // not. The flag should be removed after the v3.0 release. |
Benjamin Kramer | 0861f57 | 2011-11-26 23:01:57 +0000 | [diff] [blame] | 89 | static cl::opt<bool> EnablePhiElim( |
| 90 | "enable-lsr-phielim", cl::Hidden, cl::desc("Enable LSR phi elimination")); |
Andrew Trick | 80ef1b2 | 2011-09-27 00:44:14 +0000 | [diff] [blame] | 91 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 92 | namespace { |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 93 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 94 | /// RegSortData - This class holds data which is used to order reuse candidates. |
| 95 | class RegSortData { |
| 96 | public: |
| 97 | /// UsedByIndices - This represents the set of LSRUse indices which reference |
| 98 | /// a particular register. |
| 99 | SmallBitVector UsedByIndices; |
| 100 | |
| 101 | RegSortData() {} |
| 102 | |
| 103 | void print(raw_ostream &OS) const; |
| 104 | void dump() const; |
| 105 | }; |
| 106 | |
| 107 | } |
| 108 | |
| 109 | void RegSortData::print(raw_ostream &OS) const { |
| 110 | OS << "[NumUses=" << UsedByIndices.count() << ']'; |
| 111 | } |
| 112 | |
| 113 | void RegSortData::dump() const { |
| 114 | print(errs()); errs() << '\n'; |
| 115 | } |
Dan Gohman | c17e0cf | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 117 | namespace { |
Dale Johannesen | dc42f48 | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 118 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 119 | /// RegUseTracker - Map register candidates to information about how they are |
| 120 | /// used. |
| 121 | class RegUseTracker { |
| 122 | typedef DenseMap<const SCEV *, RegSortData> RegUsesTy; |
Dale Johannesen | dc42f48 | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 123 | |
Dan Gohman | 90bb355 | 2010-05-18 22:33:00 +0000 | [diff] [blame] | 124 | RegUsesTy RegUsesMap; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 125 | SmallVector<const SCEV *, 16> RegSequence; |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 126 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 127 | public: |
| 128 | void CountRegister(const SCEV *Reg, size_t LUIdx); |
Dan Gohman | b2df433 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 129 | void DropRegister(const SCEV *Reg, size_t LUIdx); |
Dan Gohman | c689770 | 2010-10-07 23:33:43 +0000 | [diff] [blame] | 130 | void SwapAndDropUse(size_t LUIdx, size_t LastLUIdx); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 131 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 132 | bool isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 133 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 134 | const SmallBitVector &getUsedByIndices(const SCEV *Reg) const; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 135 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 136 | void clear(); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 137 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 138 | typedef SmallVectorImpl<const SCEV *>::iterator iterator; |
| 139 | typedef SmallVectorImpl<const SCEV *>::const_iterator const_iterator; |
| 140 | iterator begin() { return RegSequence.begin(); } |
| 141 | iterator end() { return RegSequence.end(); } |
| 142 | const_iterator begin() const { return RegSequence.begin(); } |
| 143 | const_iterator end() const { return RegSequence.end(); } |
| 144 | }; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 145 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 148 | void |
| 149 | RegUseTracker::CountRegister(const SCEV *Reg, size_t LUIdx) { |
| 150 | std::pair<RegUsesTy::iterator, bool> Pair = |
Dan Gohman | 90bb355 | 2010-05-18 22:33:00 +0000 | [diff] [blame] | 151 | RegUsesMap.insert(std::make_pair(Reg, RegSortData())); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 152 | RegSortData &RSD = Pair.first->second; |
| 153 | if (Pair.second) |
| 154 | RegSequence.push_back(Reg); |
| 155 | RSD.UsedByIndices.resize(std::max(RSD.UsedByIndices.size(), LUIdx + 1)); |
| 156 | RSD.UsedByIndices.set(LUIdx); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Dan Gohman | b2df433 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 159 | void |
| 160 | RegUseTracker::DropRegister(const SCEV *Reg, size_t LUIdx) { |
| 161 | RegUsesTy::iterator It = RegUsesMap.find(Reg); |
| 162 | assert(It != RegUsesMap.end()); |
| 163 | RegSortData &RSD = It->second; |
| 164 | assert(RSD.UsedByIndices.size() > LUIdx); |
| 165 | RSD.UsedByIndices.reset(LUIdx); |
| 166 | } |
| 167 | |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 168 | void |
Dan Gohman | c689770 | 2010-10-07 23:33:43 +0000 | [diff] [blame] | 169 | RegUseTracker::SwapAndDropUse(size_t LUIdx, size_t LastLUIdx) { |
| 170 | assert(LUIdx <= LastLUIdx); |
| 171 | |
| 172 | // Update RegUses. The data structure is not optimized for this purpose; |
| 173 | // we must iterate through it and update each of the bit vectors. |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 174 | for (RegUsesTy::iterator I = RegUsesMap.begin(), E = RegUsesMap.end(); |
Dan Gohman | c689770 | 2010-10-07 23:33:43 +0000 | [diff] [blame] | 175 | I != E; ++I) { |
| 176 | SmallBitVector &UsedByIndices = I->second.UsedByIndices; |
| 177 | if (LUIdx < UsedByIndices.size()) |
| 178 | UsedByIndices[LUIdx] = |
| 179 | LastLUIdx < UsedByIndices.size() ? UsedByIndices[LastLUIdx] : 0; |
| 180 | UsedByIndices.resize(std::min(UsedByIndices.size(), LastLUIdx)); |
| 181 | } |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 184 | bool |
| 185 | RegUseTracker::isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const { |
Dan Gohman | 46fd7a6 | 2010-08-29 15:18:49 +0000 | [diff] [blame] | 186 | RegUsesTy::const_iterator I = RegUsesMap.find(Reg); |
| 187 | if (I == RegUsesMap.end()) |
| 188 | return false; |
| 189 | const SmallBitVector &UsedByIndices = I->second.UsedByIndices; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 190 | int i = UsedByIndices.find_first(); |
| 191 | if (i == -1) return false; |
| 192 | if ((size_t)i != LUIdx) return true; |
| 193 | return UsedByIndices.find_next(i) != -1; |
| 194 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 195 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 196 | const SmallBitVector &RegUseTracker::getUsedByIndices(const SCEV *Reg) const { |
Dan Gohman | 90bb355 | 2010-05-18 22:33:00 +0000 | [diff] [blame] | 197 | RegUsesTy::const_iterator I = RegUsesMap.find(Reg); |
| 198 | assert(I != RegUsesMap.end() && "Unknown register!"); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 199 | return I->second.UsedByIndices; |
| 200 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 201 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 202 | void RegUseTracker::clear() { |
Dan Gohman | 90bb355 | 2010-05-18 22:33:00 +0000 | [diff] [blame] | 203 | RegUsesMap.clear(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 204 | RegSequence.clear(); |
| 205 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 206 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 207 | namespace { |
| 208 | |
| 209 | /// Formula - This class holds information that describes a formula for |
| 210 | /// computing satisfying a use. It may include broken-out immediates and scaled |
| 211 | /// registers. |
| 212 | struct Formula { |
| 213 | /// AM - This is used to represent complex addressing, as well as other kinds |
| 214 | /// of interesting uses. |
| 215 | TargetLowering::AddrMode AM; |
| 216 | |
| 217 | /// BaseRegs - The list of "base" registers for this use. When this is |
| 218 | /// non-empty, AM.HasBaseReg should be set to true. |
| 219 | SmallVector<const SCEV *, 2> BaseRegs; |
| 220 | |
| 221 | /// ScaledReg - The 'scaled' register for this use. This should be non-null |
| 222 | /// when AM.Scale is not zero. |
| 223 | const SCEV *ScaledReg; |
| 224 | |
Dan Gohman | cca8214 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 225 | /// UnfoldedOffset - An additional constant offset which added near the |
| 226 | /// use. This requires a temporary register, but the offset itself can |
| 227 | /// live in an add immediate field rather than a register. |
| 228 | int64_t UnfoldedOffset; |
| 229 | |
| 230 | Formula() : ScaledReg(0), UnfoldedOffset(0) {} |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 231 | |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 232 | void InitialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 233 | |
| 234 | unsigned getNumRegs() const; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 235 | Type *getType() const; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 236 | |
Dan Gohman | 5ce6d05 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 237 | void DeleteBaseReg(const SCEV *&S); |
| 238 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 239 | bool referencesReg(const SCEV *S) const; |
| 240 | bool hasRegsUsedByUsesOtherThan(size_t LUIdx, |
| 241 | const RegUseTracker &RegUses) const; |
| 242 | |
| 243 | void print(raw_ostream &OS) const; |
| 244 | void dump() const; |
| 245 | }; |
| 246 | |
| 247 | } |
| 248 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 249 | /// DoInitialMatch - Recursion helper for InitialMatch. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 250 | static void DoInitialMatch(const SCEV *S, Loop *L, |
| 251 | SmallVectorImpl<const SCEV *> &Good, |
| 252 | SmallVectorImpl<const SCEV *> &Bad, |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 253 | ScalarEvolution &SE) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 254 | // Collect expressions which properly dominate the loop header. |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 255 | if (SE.properlyDominates(S, L->getHeader())) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 256 | Good.push_back(S); |
| 257 | return; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 258 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 259 | |
| 260 | // Look at add operands. |
| 261 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 262 | for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end(); |
| 263 | I != E; ++I) |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 264 | DoInitialMatch(*I, L, Good, Bad, SE); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | |
| 268 | // Look at addrec operands. |
| 269 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) |
| 270 | if (!AR->getStart()->isZero()) { |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 271 | DoInitialMatch(AR->getStart(), L, Good, Bad, SE); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 272 | DoInitialMatch(SE.getAddRecExpr(SE.getConstant(AR->getType(), 0), |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 273 | AR->getStepRecurrence(SE), |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 274 | // FIXME: AR->getNoWrapFlags() |
| 275 | AR->getLoop(), SCEV::FlagAnyWrap), |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 276 | L, Good, Bad, SE); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 277 | return; |
| 278 | } |
| 279 | |
| 280 | // Handle a multiplication by -1 (negation) if it didn't fold. |
| 281 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) |
| 282 | if (Mul->getOperand(0)->isAllOnesValue()) { |
| 283 | SmallVector<const SCEV *, 4> Ops(Mul->op_begin()+1, Mul->op_end()); |
| 284 | const SCEV *NewMul = SE.getMulExpr(Ops); |
| 285 | |
| 286 | SmallVector<const SCEV *, 4> MyGood; |
| 287 | SmallVector<const SCEV *, 4> MyBad; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 288 | DoInitialMatch(NewMul, L, MyGood, MyBad, SE); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 289 | const SCEV *NegOne = SE.getSCEV(ConstantInt::getAllOnesValue( |
| 290 | SE.getEffectiveSCEVType(NewMul->getType()))); |
| 291 | for (SmallVectorImpl<const SCEV *>::const_iterator I = MyGood.begin(), |
| 292 | E = MyGood.end(); I != E; ++I) |
| 293 | Good.push_back(SE.getMulExpr(NegOne, *I)); |
| 294 | for (SmallVectorImpl<const SCEV *>::const_iterator I = MyBad.begin(), |
| 295 | E = MyBad.end(); I != E; ++I) |
| 296 | Bad.push_back(SE.getMulExpr(NegOne, *I)); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | // Ok, we can't do anything interesting. Just stuff the whole thing into a |
| 301 | // register and hope for the best. |
| 302 | Bad.push_back(S); |
| 303 | } |
| 304 | |
| 305 | /// InitialMatch - Incorporate loop-variant parts of S into this Formula, |
| 306 | /// attempting to keep all loop-invariant and loop-computable values in a |
| 307 | /// single base register. |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 308 | void Formula::InitialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 309 | SmallVector<const SCEV *, 4> Good; |
| 310 | SmallVector<const SCEV *, 4> Bad; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 311 | DoInitialMatch(S, L, Good, Bad, SE); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 312 | if (!Good.empty()) { |
Dan Gohman | e60bb15 | 2010-04-08 23:36:27 +0000 | [diff] [blame] | 313 | const SCEV *Sum = SE.getAddExpr(Good); |
| 314 | if (!Sum->isZero()) |
| 315 | BaseRegs.push_back(Sum); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 316 | AM.HasBaseReg = true; |
| 317 | } |
| 318 | if (!Bad.empty()) { |
Dan Gohman | e60bb15 | 2010-04-08 23:36:27 +0000 | [diff] [blame] | 319 | const SCEV *Sum = SE.getAddExpr(Bad); |
| 320 | if (!Sum->isZero()) |
| 321 | BaseRegs.push_back(Sum); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 322 | AM.HasBaseReg = true; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /// getNumRegs - Return the total number of register operands used by this |
| 327 | /// formula. This does not include register uses implied by non-constant |
| 328 | /// addrec strides. |
| 329 | unsigned Formula::getNumRegs() const { |
| 330 | return !!ScaledReg + BaseRegs.size(); |
| 331 | } |
| 332 | |
| 333 | /// getType - Return the type of this formula, if it has one, or null |
| 334 | /// otherwise. This type is meaningless except for the bit size. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 335 | Type *Formula::getType() const { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 336 | return !BaseRegs.empty() ? BaseRegs.front()->getType() : |
| 337 | ScaledReg ? ScaledReg->getType() : |
| 338 | AM.BaseGV ? AM.BaseGV->getType() : |
| 339 | 0; |
| 340 | } |
| 341 | |
Dan Gohman | 5ce6d05 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 342 | /// DeleteBaseReg - Delete the given base reg from the BaseRegs list. |
| 343 | void Formula::DeleteBaseReg(const SCEV *&S) { |
| 344 | if (&S != &BaseRegs.back()) |
| 345 | std::swap(S, BaseRegs.back()); |
| 346 | BaseRegs.pop_back(); |
| 347 | } |
| 348 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 349 | /// referencesReg - Test if this formula references the given register. |
| 350 | bool Formula::referencesReg(const SCEV *S) const { |
| 351 | return S == ScaledReg || |
| 352 | std::find(BaseRegs.begin(), BaseRegs.end(), S) != BaseRegs.end(); |
| 353 | } |
| 354 | |
| 355 | /// hasRegsUsedByUsesOtherThan - Test whether this formula uses registers |
| 356 | /// which are used by uses other than the use with the given index. |
| 357 | bool Formula::hasRegsUsedByUsesOtherThan(size_t LUIdx, |
| 358 | const RegUseTracker &RegUses) const { |
| 359 | if (ScaledReg) |
| 360 | if (RegUses.isRegUsedByUsesOtherThan(ScaledReg, LUIdx)) |
| 361 | return true; |
| 362 | for (SmallVectorImpl<const SCEV *>::const_iterator I = BaseRegs.begin(), |
| 363 | E = BaseRegs.end(); I != E; ++I) |
| 364 | if (RegUses.isRegUsedByUsesOtherThan(*I, LUIdx)) |
| 365 | return true; |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | void Formula::print(raw_ostream &OS) const { |
| 370 | bool First = true; |
| 371 | if (AM.BaseGV) { |
| 372 | if (!First) OS << " + "; else First = false; |
| 373 | WriteAsOperand(OS, AM.BaseGV, /*PrintType=*/false); |
| 374 | } |
| 375 | if (AM.BaseOffs != 0) { |
| 376 | if (!First) OS << " + "; else First = false; |
| 377 | OS << AM.BaseOffs; |
| 378 | } |
| 379 | for (SmallVectorImpl<const SCEV *>::const_iterator I = BaseRegs.begin(), |
| 380 | E = BaseRegs.end(); I != E; ++I) { |
| 381 | if (!First) OS << " + "; else First = false; |
| 382 | OS << "reg(" << **I << ')'; |
| 383 | } |
Dan Gohman | c4cfbaf | 2010-05-18 22:35:55 +0000 | [diff] [blame] | 384 | if (AM.HasBaseReg && BaseRegs.empty()) { |
| 385 | if (!First) OS << " + "; else First = false; |
| 386 | OS << "**error: HasBaseReg**"; |
| 387 | } else if (!AM.HasBaseReg && !BaseRegs.empty()) { |
| 388 | if (!First) OS << " + "; else First = false; |
| 389 | OS << "**error: !HasBaseReg**"; |
| 390 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 391 | if (AM.Scale != 0) { |
| 392 | if (!First) OS << " + "; else First = false; |
| 393 | OS << AM.Scale << "*reg("; |
| 394 | if (ScaledReg) |
| 395 | OS << *ScaledReg; |
| 396 | else |
| 397 | OS << "<unknown>"; |
| 398 | OS << ')'; |
| 399 | } |
Dan Gohman | cca8214 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 400 | if (UnfoldedOffset != 0) { |
| 401 | if (!First) OS << " + "; else First = false; |
| 402 | OS << "imm(" << UnfoldedOffset << ')'; |
| 403 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void Formula::dump() const { |
| 407 | print(errs()); errs() << '\n'; |
| 408 | } |
| 409 | |
Dan Gohman | aae01f1 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 410 | /// isAddRecSExtable - Return true if the given addrec can be sign-extended |
| 411 | /// without changing its value. |
| 412 | static bool isAddRecSExtable(const SCEVAddRecExpr *AR, ScalarEvolution &SE) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 413 | Type *WideTy = |
Dan Gohman | ea507f5 | 2010-05-20 19:44:23 +0000 | [diff] [blame] | 414 | IntegerType::get(SE.getContext(), SE.getTypeSizeInBits(AR->getType()) + 1); |
Dan Gohman | aae01f1 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 415 | return isa<SCEVAddRecExpr>(SE.getSignExtendExpr(AR, WideTy)); |
| 416 | } |
| 417 | |
| 418 | /// isAddSExtable - Return true if the given add can be sign-extended |
| 419 | /// without changing its value. |
| 420 | static bool isAddSExtable(const SCEVAddExpr *A, ScalarEvolution &SE) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 421 | Type *WideTy = |
Dan Gohman | ea507f5 | 2010-05-20 19:44:23 +0000 | [diff] [blame] | 422 | IntegerType::get(SE.getContext(), SE.getTypeSizeInBits(A->getType()) + 1); |
Dan Gohman | aae01f1 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 423 | return isa<SCEVAddExpr>(SE.getSignExtendExpr(A, WideTy)); |
| 424 | } |
| 425 | |
Dan Gohman | 473e635 | 2010-06-24 16:45:11 +0000 | [diff] [blame] | 426 | /// isMulSExtable - Return true if the given mul can be sign-extended |
Dan Gohman | aae01f1 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 427 | /// without changing its value. |
Dan Gohman | 473e635 | 2010-06-24 16:45:11 +0000 | [diff] [blame] | 428 | static bool isMulSExtable(const SCEVMulExpr *M, ScalarEvolution &SE) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 429 | Type *WideTy = |
Dan Gohman | 473e635 | 2010-06-24 16:45:11 +0000 | [diff] [blame] | 430 | IntegerType::get(SE.getContext(), |
| 431 | SE.getTypeSizeInBits(M->getType()) * M->getNumOperands()); |
| 432 | return isa<SCEVMulExpr>(SE.getSignExtendExpr(M, WideTy)); |
Dan Gohman | aae01f1 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Dan Gohman | f09b712 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 435 | /// getExactSDiv - Return an expression for LHS /s RHS, if it can be determined |
| 436 | /// and if the remainder is known to be zero, or null otherwise. If |
| 437 | /// IgnoreSignificantBits is true, expressions like (X * Y) /s Y are simplified |
| 438 | /// to Y, ignoring that the multiplication may overflow, which is useful when |
| 439 | /// the result will be used in a context where the most significant bits are |
| 440 | /// ignored. |
| 441 | static const SCEV *getExactSDiv(const SCEV *LHS, const SCEV *RHS, |
| 442 | ScalarEvolution &SE, |
| 443 | bool IgnoreSignificantBits = false) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 444 | // Handle the trivial case, which works for any SCEV type. |
| 445 | if (LHS == RHS) |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 446 | return SE.getConstant(LHS->getType(), 1); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 447 | |
Dan Gohman | d42819a | 2010-06-24 16:51:25 +0000 | [diff] [blame] | 448 | // Handle a few RHS special cases. |
| 449 | const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS); |
| 450 | if (RC) { |
| 451 | const APInt &RA = RC->getValue()->getValue(); |
| 452 | // Handle x /s -1 as x * -1, to give ScalarEvolution a chance to do |
| 453 | // some folding. |
| 454 | if (RA.isAllOnesValue()) |
| 455 | return SE.getMulExpr(LHS, RC); |
| 456 | // Handle x /s 1 as x. |
| 457 | if (RA == 1) |
| 458 | return LHS; |
| 459 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 460 | |
| 461 | // Check for a division of a constant by a constant. |
| 462 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(LHS)) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 463 | if (!RC) |
| 464 | return 0; |
Dan Gohman | d42819a | 2010-06-24 16:51:25 +0000 | [diff] [blame] | 465 | const APInt &LA = C->getValue()->getValue(); |
| 466 | const APInt &RA = RC->getValue()->getValue(); |
| 467 | if (LA.srem(RA) != 0) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 468 | return 0; |
Dan Gohman | d42819a | 2010-06-24 16:51:25 +0000 | [diff] [blame] | 469 | return SE.getConstant(LA.sdiv(RA)); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Dan Gohman | aae01f1 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 472 | // Distribute the sdiv over addrec operands, if the addrec doesn't overflow. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 473 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) { |
Dan Gohman | aae01f1 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 474 | if (IgnoreSignificantBits || isAddRecSExtable(AR, SE)) { |
Dan Gohman | f09b712 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 475 | const SCEV *Step = getExactSDiv(AR->getStepRecurrence(SE), RHS, SE, |
| 476 | IgnoreSignificantBits); |
Dan Gohman | aae01f1 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 477 | if (!Step) return 0; |
Dan Gohman | 694a15e | 2010-08-19 01:02:31 +0000 | [diff] [blame] | 478 | const SCEV *Start = getExactSDiv(AR->getStart(), RHS, SE, |
| 479 | IgnoreSignificantBits); |
| 480 | if (!Start) return 0; |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 481 | // FlagNW is independent of the start value, step direction, and is |
| 482 | // preserved with smaller magnitude steps. |
| 483 | // FIXME: AR->getNoWrapFlags(SCEV::FlagNW) |
| 484 | return SE.getAddRecExpr(Start, Step, AR->getLoop(), SCEV::FlagAnyWrap); |
Dan Gohman | aae01f1 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 485 | } |
Dan Gohman | 2ea09e0 | 2010-06-24 16:57:52 +0000 | [diff] [blame] | 486 | return 0; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Dan Gohman | aae01f1 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 489 | // Distribute the sdiv over add operands, if the add doesn't overflow. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 490 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(LHS)) { |
Dan Gohman | aae01f1 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 491 | if (IgnoreSignificantBits || isAddSExtable(Add, SE)) { |
| 492 | SmallVector<const SCEV *, 8> Ops; |
| 493 | for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end(); |
| 494 | I != E; ++I) { |
Dan Gohman | f09b712 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 495 | const SCEV *Op = getExactSDiv(*I, RHS, SE, |
| 496 | IgnoreSignificantBits); |
Dan Gohman | aae01f1 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 497 | if (!Op) return 0; |
| 498 | Ops.push_back(Op); |
| 499 | } |
| 500 | return SE.getAddExpr(Ops); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 501 | } |
Dan Gohman | 2ea09e0 | 2010-06-24 16:57:52 +0000 | [diff] [blame] | 502 | return 0; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | // Check for a multiply operand that we can pull RHS out of. |
Dan Gohman | 2ea09e0 | 2010-06-24 16:57:52 +0000 | [diff] [blame] | 506 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS)) { |
Dan Gohman | aae01f1 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 507 | if (IgnoreSignificantBits || isMulSExtable(Mul, SE)) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 508 | SmallVector<const SCEV *, 4> Ops; |
| 509 | bool Found = false; |
| 510 | for (SCEVMulExpr::op_iterator I = Mul->op_begin(), E = Mul->op_end(); |
| 511 | I != E; ++I) { |
Dan Gohman | 4766744 | 2010-05-20 16:23:28 +0000 | [diff] [blame] | 512 | const SCEV *S = *I; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 513 | if (!Found) |
Dan Gohman | 4766744 | 2010-05-20 16:23:28 +0000 | [diff] [blame] | 514 | if (const SCEV *Q = getExactSDiv(S, RHS, SE, |
Dan Gohman | f09b712 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 515 | IgnoreSignificantBits)) { |
Dan Gohman | 4766744 | 2010-05-20 16:23:28 +0000 | [diff] [blame] | 516 | S = Q; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 517 | Found = true; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 518 | } |
Dan Gohman | 4766744 | 2010-05-20 16:23:28 +0000 | [diff] [blame] | 519 | Ops.push_back(S); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 520 | } |
| 521 | return Found ? SE.getMulExpr(Ops) : 0; |
| 522 | } |
Dan Gohman | 2ea09e0 | 2010-06-24 16:57:52 +0000 | [diff] [blame] | 523 | return 0; |
| 524 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 525 | |
| 526 | // Otherwise we don't know. |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | /// ExtractImmediate - If S involves the addition of a constant integer value, |
| 531 | /// return that integer value, and mutate S to point to a new SCEV with that |
| 532 | /// value excluded. |
| 533 | static int64_t ExtractImmediate(const SCEV *&S, ScalarEvolution &SE) { |
| 534 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { |
| 535 | if (C->getValue()->getValue().getMinSignedBits() <= 64) { |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 536 | S = SE.getConstant(C->getType(), 0); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 537 | return C->getValue()->getSExtValue(); |
| 538 | } |
| 539 | } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 540 | SmallVector<const SCEV *, 8> NewOps(Add->op_begin(), Add->op_end()); |
| 541 | int64_t Result = ExtractImmediate(NewOps.front(), SE); |
Dan Gohman | e62d588 | 2010-08-13 21:17:19 +0000 | [diff] [blame] | 542 | if (Result != 0) |
| 543 | S = SE.getAddExpr(NewOps); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 544 | return Result; |
| 545 | } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 546 | SmallVector<const SCEV *, 8> NewOps(AR->op_begin(), AR->op_end()); |
| 547 | int64_t Result = ExtractImmediate(NewOps.front(), SE); |
Dan Gohman | e62d588 | 2010-08-13 21:17:19 +0000 | [diff] [blame] | 548 | if (Result != 0) |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 549 | S = SE.getAddRecExpr(NewOps, AR->getLoop(), |
| 550 | // FIXME: AR->getNoWrapFlags(SCEV::FlagNW) |
| 551 | SCEV::FlagAnyWrap); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 552 | return Result; |
| 553 | } |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | /// ExtractSymbol - If S involves the addition of a GlobalValue address, |
| 558 | /// return that symbol, and mutate S to point to a new SCEV with that |
| 559 | /// value excluded. |
| 560 | static GlobalValue *ExtractSymbol(const SCEV *&S, ScalarEvolution &SE) { |
| 561 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 562 | if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue())) { |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 563 | S = SE.getConstant(GV->getType(), 0); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 564 | return GV; |
| 565 | } |
| 566 | } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 567 | SmallVector<const SCEV *, 8> NewOps(Add->op_begin(), Add->op_end()); |
| 568 | GlobalValue *Result = ExtractSymbol(NewOps.back(), SE); |
Dan Gohman | e62d588 | 2010-08-13 21:17:19 +0000 | [diff] [blame] | 569 | if (Result) |
| 570 | S = SE.getAddExpr(NewOps); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 571 | return Result; |
| 572 | } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 573 | SmallVector<const SCEV *, 8> NewOps(AR->op_begin(), AR->op_end()); |
| 574 | GlobalValue *Result = ExtractSymbol(NewOps.front(), SE); |
Dan Gohman | e62d588 | 2010-08-13 21:17:19 +0000 | [diff] [blame] | 575 | if (Result) |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 576 | S = SE.getAddRecExpr(NewOps, AR->getLoop(), |
| 577 | // FIXME: AR->getNoWrapFlags(SCEV::FlagNW) |
| 578 | SCEV::FlagAnyWrap); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 579 | return Result; |
| 580 | } |
| 581 | return 0; |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Dan Gohman | f284ce2 | 2009-02-18 00:08:39 +0000 | [diff] [blame] | 584 | /// isAddressUse - Returns true if the specified instruction is using the |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 585 | /// specified value as an address. |
| 586 | static bool isAddressUse(Instruction *Inst, Value *OperandVal) { |
| 587 | bool isAddress = isa<LoadInst>(Inst); |
| 588 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 589 | if (SI->getOperand(1) == OperandVal) |
| 590 | isAddress = true; |
| 591 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
| 592 | // Addressing modes can also be folded into prefetches and a variety |
| 593 | // of intrinsics. |
| 594 | switch (II->getIntrinsicID()) { |
| 595 | default: break; |
| 596 | case Intrinsic::prefetch: |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 597 | case Intrinsic::x86_sse_storeu_ps: |
| 598 | case Intrinsic::x86_sse2_storeu_pd: |
| 599 | case Intrinsic::x86_sse2_storeu_dq: |
| 600 | case Intrinsic::x86_sse2_storel_dq: |
Gabor Greif | ad72e73 | 2010-06-30 09:15:28 +0000 | [diff] [blame] | 601 | if (II->getArgOperand(0) == OperandVal) |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 602 | isAddress = true; |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | return isAddress; |
| 607 | } |
Chris Lattner | 0ae33eb | 2005-10-03 01:04:44 +0000 | [diff] [blame] | 608 | |
Dan Gohman | 21e7722 | 2009-03-09 21:01:17 +0000 | [diff] [blame] | 609 | /// getAccessType - Return the type of the memory being accessed. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 610 | static Type *getAccessType(const Instruction *Inst) { |
| 611 | Type *AccessTy = Inst->getType(); |
Dan Gohman | 21e7722 | 2009-03-09 21:01:17 +0000 | [diff] [blame] | 612 | if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) |
Dan Gohman | a537bf8 | 2009-05-18 16:45:28 +0000 | [diff] [blame] | 613 | AccessTy = SI->getOperand(0)->getType(); |
Dan Gohman | 21e7722 | 2009-03-09 21:01:17 +0000 | [diff] [blame] | 614 | else if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
| 615 | // Addressing modes can also be folded into prefetches and a variety |
| 616 | // of intrinsics. |
| 617 | switch (II->getIntrinsicID()) { |
| 618 | default: break; |
| 619 | case Intrinsic::x86_sse_storeu_ps: |
| 620 | case Intrinsic::x86_sse2_storeu_pd: |
| 621 | case Intrinsic::x86_sse2_storeu_dq: |
| 622 | case Intrinsic::x86_sse2_storel_dq: |
Gabor Greif | ad72e73 | 2010-06-30 09:15:28 +0000 | [diff] [blame] | 623 | AccessTy = II->getArgOperand(0)->getType(); |
Dan Gohman | 21e7722 | 2009-03-09 21:01:17 +0000 | [diff] [blame] | 624 | break; |
| 625 | } |
| 626 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 627 | |
| 628 | // All pointers have the same requirements, so canonicalize them to an |
| 629 | // arbitrary pointer type to minimize variation. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 630 | if (PointerType *PTy = dyn_cast<PointerType>(AccessTy)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 631 | AccessTy = PointerType::get(IntegerType::get(PTy->getContext(), 1), |
| 632 | PTy->getAddressSpace()); |
| 633 | |
Dan Gohman | a537bf8 | 2009-05-18 16:45:28 +0000 | [diff] [blame] | 634 | return AccessTy; |
Dan Gohman | 21e7722 | 2009-03-09 21:01:17 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 637 | /// isExistingPhi - Return true if this AddRec is already a phi in its loop. |
| 638 | static bool isExistingPhi(const SCEVAddRecExpr *AR, ScalarEvolution &SE) { |
| 639 | for (BasicBlock::iterator I = AR->getLoop()->getHeader()->begin(); |
| 640 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 641 | if (SE.isSCEVable(PN->getType()) && |
| 642 | (SE.getEffectiveSCEVType(PN->getType()) == |
| 643 | SE.getEffectiveSCEVType(AR->getType())) && |
| 644 | SE.getSCEV(PN) == AR) |
| 645 | return true; |
| 646 | } |
| 647 | return false; |
| 648 | } |
| 649 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 650 | /// DeleteTriviallyDeadInstructions - If any of the instructions is the |
| 651 | /// specified set are trivially dead, delete them and see if this makes any of |
| 652 | /// their operands subsequently dead. |
| 653 | static bool |
| 654 | DeleteTriviallyDeadInstructions(SmallVectorImpl<WeakVH> &DeadInsts) { |
| 655 | bool Changed = false; |
| 656 | |
| 657 | while (!DeadInsts.empty()) { |
Gabor Greif | f097b59 | 2010-09-18 11:55:34 +0000 | [diff] [blame] | 658 | Instruction *I = dyn_cast_or_null<Instruction>(&*DeadInsts.pop_back_val()); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 659 | |
| 660 | if (I == 0 || !isInstructionTriviallyDead(I)) |
| 661 | continue; |
| 662 | |
| 663 | for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) |
| 664 | if (Instruction *U = dyn_cast<Instruction>(*OI)) { |
| 665 | *OI = 0; |
| 666 | if (U->use_empty()) |
| 667 | DeadInsts.push_back(U); |
| 668 | } |
| 669 | |
| 670 | I->eraseFromParent(); |
| 671 | Changed = true; |
| 672 | } |
| 673 | |
| 674 | return Changed; |
| 675 | } |
| 676 | |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 677 | namespace { |
Jim Grosbach | 56a1f80 | 2009-11-17 17:53:56 +0000 | [diff] [blame] | 678 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 679 | /// Cost - This class is used to measure and compare candidate formulae. |
| 680 | class Cost { |
| 681 | /// TODO: Some of these could be merged. Also, a lexical ordering |
| 682 | /// isn't always optimal. |
| 683 | unsigned NumRegs; |
| 684 | unsigned AddRecCost; |
| 685 | unsigned NumIVMuls; |
| 686 | unsigned NumBaseAdds; |
| 687 | unsigned ImmCost; |
| 688 | unsigned SetupCost; |
Nate Begeman | 1699748 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 689 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 690 | public: |
| 691 | Cost() |
| 692 | : NumRegs(0), AddRecCost(0), NumIVMuls(0), NumBaseAdds(0), ImmCost(0), |
| 693 | SetupCost(0) {} |
Jim Grosbach | 56a1f80 | 2009-11-17 17:53:56 +0000 | [diff] [blame] | 694 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 695 | bool operator<(const Cost &Other) const; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 696 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 697 | void Loose(); |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 698 | |
Andrew Trick | 7d11bd8 | 2011-09-26 23:11:04 +0000 | [diff] [blame] | 699 | #ifndef NDEBUG |
| 700 | // Once any of the metrics loses, they must all remain losers. |
| 701 | bool isValid() { |
| 702 | return ((NumRegs | AddRecCost | NumIVMuls | NumBaseAdds |
| 703 | | ImmCost | SetupCost) != ~0u) |
| 704 | || ((NumRegs & AddRecCost & NumIVMuls & NumBaseAdds |
| 705 | & ImmCost & SetupCost) == ~0u); |
| 706 | } |
| 707 | #endif |
| 708 | |
| 709 | bool isLoser() { |
| 710 | assert(isValid() && "invalid cost"); |
| 711 | return NumRegs == ~0u; |
| 712 | } |
| 713 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 714 | void RateFormula(const Formula &F, |
| 715 | SmallPtrSet<const SCEV *, 16> &Regs, |
| 716 | const DenseSet<const SCEV *> &VisitedRegs, |
| 717 | const Loop *L, |
| 718 | const SmallVectorImpl<int64_t> &Offsets, |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 719 | ScalarEvolution &SE, DominatorTree &DT, |
| 720 | SmallPtrSet<const SCEV *, 16> *LoserRegs = 0); |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 721 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 722 | void print(raw_ostream &OS) const; |
| 723 | void dump() const; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 724 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 725 | private: |
| 726 | void RateRegister(const SCEV *Reg, |
| 727 | SmallPtrSet<const SCEV *, 16> &Regs, |
| 728 | const Loop *L, |
| 729 | ScalarEvolution &SE, DominatorTree &DT); |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 730 | void RatePrimaryRegister(const SCEV *Reg, |
| 731 | SmallPtrSet<const SCEV *, 16> &Regs, |
| 732 | const Loop *L, |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 733 | ScalarEvolution &SE, DominatorTree &DT, |
| 734 | SmallPtrSet<const SCEV *, 16> *LoserRegs); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 735 | }; |
| 736 | |
| 737 | } |
| 738 | |
| 739 | /// RateRegister - Tally up interesting quantities from the given register. |
| 740 | void Cost::RateRegister(const SCEV *Reg, |
| 741 | SmallPtrSet<const SCEV *, 16> &Regs, |
| 742 | const Loop *L, |
| 743 | ScalarEvolution &SE, DominatorTree &DT) { |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 744 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Reg)) { |
| 745 | if (AR->getLoop() == L) |
| 746 | AddRecCost += 1; /// TODO: This should be a function of the stride. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 747 | |
Andrew Trick | 0c01bc3 | 2011-09-29 01:33:38 +0000 | [diff] [blame] | 748 | // If this is an addrec for another loop, don't second-guess its addrec phi |
| 749 | // nodes. LSR isn't currently smart enough to reason about more than one |
| 750 | // loop at a time. LSR has either already run on inner loops, will not run |
| 751 | // on other loops, and cannot be expected to change sibling loops. If the |
| 752 | // AddRec exists, consider it's register free and leave it alone. Otherwise, |
| 753 | // do not consider this formula at all. |
Andrew Trick | 0c01bc3 | 2011-09-29 01:33:38 +0000 | [diff] [blame] | 754 | else if (!EnableNested || L->contains(AR->getLoop()) || |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 755 | (!AR->getLoop()->contains(L) && |
| 756 | DT.dominates(L->getHeader(), AR->getLoop()->getHeader()))) { |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 757 | if (isExistingPhi(AR, SE)) |
| 758 | return; |
| 759 | |
| 760 | // For !EnableNested, never rewrite IVs in other loops. |
Andrew Trick | 0c01bc3 | 2011-09-29 01:33:38 +0000 | [diff] [blame] | 761 | if (!EnableNested) { |
| 762 | Loose(); |
| 763 | return; |
| 764 | } |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 765 | // If this isn't one of the addrecs that the loop already has, it |
| 766 | // would require a costly new phi and add. TODO: This isn't |
| 767 | // precisely modeled right now. |
| 768 | ++NumBaseAdds; |
Andrew Trick | 7d11bd8 | 2011-09-26 23:11:04 +0000 | [diff] [blame] | 769 | if (!Regs.count(AR->getStart())) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 770 | RateRegister(AR->getStart(), Regs, L, SE, DT); |
Andrew Trick | 7d11bd8 | 2011-09-26 23:11:04 +0000 | [diff] [blame] | 771 | if (isLoser()) |
| 772 | return; |
| 773 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 774 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 775 | |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 776 | // Add the step value register, if it needs one. |
| 777 | // TODO: The non-affine case isn't precisely modeled here. |
Andrew Trick | 25b689e | 2011-09-26 23:35:25 +0000 | [diff] [blame] | 778 | if (!AR->isAffine() || !isa<SCEVConstant>(AR->getOperand(1))) { |
| 779 | if (!Regs.count(AR->getOperand(1))) { |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 780 | RateRegister(AR->getOperand(1), Regs, L, SE, DT); |
Andrew Trick | 25b689e | 2011-09-26 23:35:25 +0000 | [diff] [blame] | 781 | if (isLoser()) |
| 782 | return; |
| 783 | } |
| 784 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 785 | } |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 786 | ++NumRegs; |
| 787 | |
| 788 | // Rough heuristic; favor registers which don't require extra setup |
| 789 | // instructions in the preheader. |
| 790 | if (!isa<SCEVUnknown>(Reg) && |
| 791 | !isa<SCEVConstant>(Reg) && |
| 792 | !(isa<SCEVAddRecExpr>(Reg) && |
| 793 | (isa<SCEVUnknown>(cast<SCEVAddRecExpr>(Reg)->getStart()) || |
| 794 | isa<SCEVConstant>(cast<SCEVAddRecExpr>(Reg)->getStart())))) |
| 795 | ++SetupCost; |
Dan Gohman | 23c3fde | 2010-10-07 23:41:58 +0000 | [diff] [blame] | 796 | |
| 797 | NumIVMuls += isa<SCEVMulExpr>(Reg) && |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 798 | SE.hasComputableLoopEvolution(Reg, L); |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /// RatePrimaryRegister - Record this register in the set. If we haven't seen it |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 802 | /// before, rate it. Optional LoserRegs provides a way to declare any formula |
| 803 | /// that refers to one of those regs an instant loser. |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 804 | void Cost::RatePrimaryRegister(const SCEV *Reg, |
Dan Gohman | 7fca229 | 2010-02-16 19:42:34 +0000 | [diff] [blame] | 805 | SmallPtrSet<const SCEV *, 16> &Regs, |
| 806 | const Loop *L, |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 807 | ScalarEvolution &SE, DominatorTree &DT, |
| 808 | SmallPtrSet<const SCEV *, 16> *LoserRegs) { |
| 809 | if (LoserRegs && LoserRegs->count(Reg)) { |
| 810 | Loose(); |
| 811 | return; |
| 812 | } |
| 813 | if (Regs.insert(Reg)) { |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 814 | RateRegister(Reg, Regs, L, SE, DT); |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 815 | if (isLoser()) |
| 816 | LoserRegs->insert(Reg); |
| 817 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | void Cost::RateFormula(const Formula &F, |
| 821 | SmallPtrSet<const SCEV *, 16> &Regs, |
| 822 | const DenseSet<const SCEV *> &VisitedRegs, |
| 823 | const Loop *L, |
| 824 | const SmallVectorImpl<int64_t> &Offsets, |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 825 | ScalarEvolution &SE, DominatorTree &DT, |
| 826 | SmallPtrSet<const SCEV *, 16> *LoserRegs) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 827 | // Tally up the registers. |
| 828 | if (const SCEV *ScaledReg = F.ScaledReg) { |
| 829 | if (VisitedRegs.count(ScaledReg)) { |
| 830 | Loose(); |
| 831 | return; |
| 832 | } |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 833 | RatePrimaryRegister(ScaledReg, Regs, L, SE, DT, LoserRegs); |
Andrew Trick | 7d11bd8 | 2011-09-26 23:11:04 +0000 | [diff] [blame] | 834 | if (isLoser()) |
| 835 | return; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 836 | } |
| 837 | for (SmallVectorImpl<const SCEV *>::const_iterator I = F.BaseRegs.begin(), |
| 838 | E = F.BaseRegs.end(); I != E; ++I) { |
| 839 | const SCEV *BaseReg = *I; |
| 840 | if (VisitedRegs.count(BaseReg)) { |
| 841 | Loose(); |
| 842 | return; |
| 843 | } |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 844 | RatePrimaryRegister(BaseReg, Regs, L, SE, DT, LoserRegs); |
Andrew Trick | 7d11bd8 | 2011-09-26 23:11:04 +0000 | [diff] [blame] | 845 | if (isLoser()) |
| 846 | return; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Dan Gohman | cca8214 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 849 | // Determine how many (unfolded) adds we'll need inside the loop. |
| 850 | size_t NumBaseParts = F.BaseRegs.size() + (F.UnfoldedOffset != 0); |
| 851 | if (NumBaseParts > 1) |
| 852 | NumBaseAdds += NumBaseParts - 1; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 853 | |
| 854 | // Tally up the non-zero immediates. |
| 855 | for (SmallVectorImpl<int64_t>::const_iterator I = Offsets.begin(), |
| 856 | E = Offsets.end(); I != E; ++I) { |
| 857 | int64_t Offset = (uint64_t)*I + F.AM.BaseOffs; |
| 858 | if (F.AM.BaseGV) |
| 859 | ImmCost += 64; // Handle symbolic values conservatively. |
| 860 | // TODO: This should probably be the pointer size. |
| 861 | else if (Offset != 0) |
| 862 | ImmCost += APInt(64, Offset, true).getMinSignedBits(); |
| 863 | } |
Andrew Trick | 7d11bd8 | 2011-09-26 23:11:04 +0000 | [diff] [blame] | 864 | assert(isValid() && "invalid cost"); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 867 | /// Loose - Set this cost to a losing value. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 868 | void Cost::Loose() { |
| 869 | NumRegs = ~0u; |
| 870 | AddRecCost = ~0u; |
| 871 | NumIVMuls = ~0u; |
| 872 | NumBaseAdds = ~0u; |
| 873 | ImmCost = ~0u; |
| 874 | SetupCost = ~0u; |
| 875 | } |
| 876 | |
| 877 | /// operator< - Choose the lower cost. |
| 878 | bool Cost::operator<(const Cost &Other) const { |
| 879 | if (NumRegs != Other.NumRegs) |
| 880 | return NumRegs < Other.NumRegs; |
| 881 | if (AddRecCost != Other.AddRecCost) |
| 882 | return AddRecCost < Other.AddRecCost; |
| 883 | if (NumIVMuls != Other.NumIVMuls) |
| 884 | return NumIVMuls < Other.NumIVMuls; |
| 885 | if (NumBaseAdds != Other.NumBaseAdds) |
| 886 | return NumBaseAdds < Other.NumBaseAdds; |
| 887 | if (ImmCost != Other.ImmCost) |
| 888 | return ImmCost < Other.ImmCost; |
| 889 | if (SetupCost != Other.SetupCost) |
| 890 | return SetupCost < Other.SetupCost; |
| 891 | return false; |
| 892 | } |
| 893 | |
| 894 | void Cost::print(raw_ostream &OS) const { |
| 895 | OS << NumRegs << " reg" << (NumRegs == 1 ? "" : "s"); |
| 896 | if (AddRecCost != 0) |
| 897 | OS << ", with addrec cost " << AddRecCost; |
| 898 | if (NumIVMuls != 0) |
| 899 | OS << ", plus " << NumIVMuls << " IV mul" << (NumIVMuls == 1 ? "" : "s"); |
| 900 | if (NumBaseAdds != 0) |
| 901 | OS << ", plus " << NumBaseAdds << " base add" |
| 902 | << (NumBaseAdds == 1 ? "" : "s"); |
| 903 | if (ImmCost != 0) |
| 904 | OS << ", plus " << ImmCost << " imm cost"; |
| 905 | if (SetupCost != 0) |
| 906 | OS << ", plus " << SetupCost << " setup cost"; |
| 907 | } |
| 908 | |
| 909 | void Cost::dump() const { |
| 910 | print(errs()); errs() << '\n'; |
| 911 | } |
| 912 | |
| 913 | namespace { |
| 914 | |
| 915 | /// LSRFixup - An operand value in an instruction which is to be replaced |
| 916 | /// with some equivalent, possibly strength-reduced, replacement. |
| 917 | struct LSRFixup { |
| 918 | /// UserInst - The instruction which will be updated. |
| 919 | Instruction *UserInst; |
| 920 | |
| 921 | /// OperandValToReplace - The operand of the instruction which will |
| 922 | /// be replaced. The operand may be used more than once; every instance |
| 923 | /// will be replaced. |
| 924 | Value *OperandValToReplace; |
| 925 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 926 | /// PostIncLoops - If this user is to use the post-incremented value of an |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 927 | /// induction variable, this variable is non-null and holds the loop |
| 928 | /// associated with the induction variable. |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 929 | PostIncLoopSet PostIncLoops; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 930 | |
| 931 | /// LUIdx - The index of the LSRUse describing the expression which |
| 932 | /// this fixup needs, minus an offset (below). |
| 933 | size_t LUIdx; |
| 934 | |
| 935 | /// Offset - A constant offset to be added to the LSRUse expression. |
| 936 | /// This allows multiple fixups to share the same LSRUse with different |
| 937 | /// offsets, for example in an unrolled loop. |
| 938 | int64_t Offset; |
| 939 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 940 | bool isUseFullyOutsideLoop(const Loop *L) const; |
| 941 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 942 | LSRFixup(); |
| 943 | |
| 944 | void print(raw_ostream &OS) const; |
| 945 | void dump() const; |
| 946 | }; |
| 947 | |
| 948 | } |
| 949 | |
| 950 | LSRFixup::LSRFixup() |
Dan Gohman | ea507f5 | 2010-05-20 19:44:23 +0000 | [diff] [blame] | 951 | : UserInst(0), OperandValToReplace(0), LUIdx(~size_t(0)), Offset(0) {} |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 952 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 953 | /// isUseFullyOutsideLoop - Test whether this fixup always uses its |
| 954 | /// value outside of the given loop. |
| 955 | bool LSRFixup::isUseFullyOutsideLoop(const Loop *L) const { |
| 956 | // PHI nodes use their value in their incoming blocks. |
| 957 | if (const PHINode *PN = dyn_cast<PHINode>(UserInst)) { |
| 958 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 959 | if (PN->getIncomingValue(i) == OperandValToReplace && |
| 960 | L->contains(PN->getIncomingBlock(i))) |
| 961 | return false; |
| 962 | return true; |
| 963 | } |
| 964 | |
| 965 | return !L->contains(UserInst); |
| 966 | } |
| 967 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 968 | void LSRFixup::print(raw_ostream &OS) const { |
| 969 | OS << "UserInst="; |
| 970 | // Store is common and interesting enough to be worth special-casing. |
| 971 | if (StoreInst *Store = dyn_cast<StoreInst>(UserInst)) { |
| 972 | OS << "store "; |
| 973 | WriteAsOperand(OS, Store->getOperand(0), /*PrintType=*/false); |
| 974 | } else if (UserInst->getType()->isVoidTy()) |
| 975 | OS << UserInst->getOpcodeName(); |
| 976 | else |
| 977 | WriteAsOperand(OS, UserInst, /*PrintType=*/false); |
| 978 | |
| 979 | OS << ", OperandValToReplace="; |
| 980 | WriteAsOperand(OS, OperandValToReplace, /*PrintType=*/false); |
| 981 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 982 | for (PostIncLoopSet::const_iterator I = PostIncLoops.begin(), |
| 983 | E = PostIncLoops.end(); I != E; ++I) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 984 | OS << ", PostIncLoop="; |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 985 | WriteAsOperand(OS, (*I)->getHeader(), /*PrintType=*/false); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | if (LUIdx != ~size_t(0)) |
| 989 | OS << ", LUIdx=" << LUIdx; |
| 990 | |
| 991 | if (Offset != 0) |
| 992 | OS << ", Offset=" << Offset; |
| 993 | } |
| 994 | |
| 995 | void LSRFixup::dump() const { |
| 996 | print(errs()); errs() << '\n'; |
| 997 | } |
| 998 | |
| 999 | namespace { |
| 1000 | |
| 1001 | /// UniquifierDenseMapInfo - A DenseMapInfo implementation for holding |
| 1002 | /// DenseMaps and DenseSets of sorted SmallVectors of const SCEV*. |
| 1003 | struct UniquifierDenseMapInfo { |
| 1004 | static SmallVector<const SCEV *, 2> getEmptyKey() { |
| 1005 | SmallVector<const SCEV *, 2> V; |
| 1006 | V.push_back(reinterpret_cast<const SCEV *>(-1)); |
| 1007 | return V; |
| 1008 | } |
| 1009 | |
| 1010 | static SmallVector<const SCEV *, 2> getTombstoneKey() { |
| 1011 | SmallVector<const SCEV *, 2> V; |
| 1012 | V.push_back(reinterpret_cast<const SCEV *>(-2)); |
| 1013 | return V; |
| 1014 | } |
| 1015 | |
| 1016 | static unsigned getHashValue(const SmallVector<const SCEV *, 2> &V) { |
| 1017 | unsigned Result = 0; |
| 1018 | for (SmallVectorImpl<const SCEV *>::const_iterator I = V.begin(), |
| 1019 | E = V.end(); I != E; ++I) |
| 1020 | Result ^= DenseMapInfo<const SCEV *>::getHashValue(*I); |
| 1021 | return Result; |
| 1022 | } |
| 1023 | |
| 1024 | static bool isEqual(const SmallVector<const SCEV *, 2> &LHS, |
| 1025 | const SmallVector<const SCEV *, 2> &RHS) { |
| 1026 | return LHS == RHS; |
| 1027 | } |
| 1028 | }; |
| 1029 | |
| 1030 | /// LSRUse - This class holds the state that LSR keeps for each use in |
| 1031 | /// IVUsers, as well as uses invented by LSR itself. It includes information |
| 1032 | /// about what kinds of things can be folded into the user, information about |
| 1033 | /// the user itself, and information about how the use may be satisfied. |
| 1034 | /// TODO: Represent multiple users of the same expression in common? |
| 1035 | class LSRUse { |
| 1036 | DenseSet<SmallVector<const SCEV *, 2>, UniquifierDenseMapInfo> Uniquifier; |
| 1037 | |
| 1038 | public: |
| 1039 | /// KindType - An enum for a kind of use, indicating what types of |
| 1040 | /// scaled and immediate operands it might support. |
| 1041 | enum KindType { |
| 1042 | Basic, ///< A normal use, with no folding. |
| 1043 | Special, ///< A special case of basic, allowing -1 scales. |
| 1044 | Address, ///< An address use; folding according to TargetLowering |
| 1045 | ICmpZero ///< An equality icmp with both operands folded into one. |
| 1046 | // TODO: Add a generic icmp too? |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1047 | }; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1048 | |
| 1049 | KindType Kind; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1050 | Type *AccessTy; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1051 | |
| 1052 | SmallVector<int64_t, 8> Offsets; |
| 1053 | int64_t MinOffset; |
| 1054 | int64_t MaxOffset; |
| 1055 | |
| 1056 | /// AllFixupsOutsideLoop - This records whether all of the fixups using this |
| 1057 | /// LSRUse are outside of the loop, in which case some special-case heuristics |
| 1058 | /// may be used. |
| 1059 | bool AllFixupsOutsideLoop; |
| 1060 | |
Dan Gohman | a9db129 | 2010-07-15 20:24:58 +0000 | [diff] [blame] | 1061 | /// WidestFixupType - This records the widest use type for any fixup using |
| 1062 | /// this LSRUse. FindUseWithSimilarFormula can't consider uses with different |
| 1063 | /// max fixup widths to be equivalent, because the narrower one may be relying |
| 1064 | /// on the implicit truncation to truncate away bogus bits. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1065 | Type *WidestFixupType; |
Dan Gohman | a9db129 | 2010-07-15 20:24:58 +0000 | [diff] [blame] | 1066 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1067 | /// Formulae - A list of ways to build a value that can satisfy this user. |
| 1068 | /// After the list is populated, one of these is selected heuristically and |
| 1069 | /// used to formulate a replacement for OperandValToReplace in UserInst. |
| 1070 | SmallVector<Formula, 12> Formulae; |
| 1071 | |
| 1072 | /// Regs - The set of register candidates used by all formulae in this LSRUse. |
| 1073 | SmallPtrSet<const SCEV *, 4> Regs; |
| 1074 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1075 | LSRUse(KindType K, Type *T) : Kind(K), AccessTy(T), |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1076 | MinOffset(INT64_MAX), |
| 1077 | MaxOffset(INT64_MIN), |
Dan Gohman | a9db129 | 2010-07-15 20:24:58 +0000 | [diff] [blame] | 1078 | AllFixupsOutsideLoop(true), |
| 1079 | WidestFixupType(0) {} |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1080 | |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 1081 | bool HasFormulaWithSameRegs(const Formula &F) const; |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1082 | bool InsertFormula(const Formula &F); |
Dan Gohman | d69d628 | 2010-05-18 22:39:15 +0000 | [diff] [blame] | 1083 | void DeleteFormula(Formula &F); |
Dan Gohman | b2df433 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 1084 | void RecomputeRegs(size_t LUIdx, RegUseTracker &Reguses); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1085 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1086 | void print(raw_ostream &OS) const; |
| 1087 | void dump() const; |
| 1088 | }; |
| 1089 | |
Dan Gohman | b621171 | 2010-06-19 21:21:39 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 1092 | /// HasFormula - Test whether this use as a formula which has the same |
| 1093 | /// registers as the given formula. |
| 1094 | bool LSRUse::HasFormulaWithSameRegs(const Formula &F) const { |
| 1095 | SmallVector<const SCEV *, 2> Key = F.BaseRegs; |
| 1096 | if (F.ScaledReg) Key.push_back(F.ScaledReg); |
| 1097 | // Unstable sort by host order ok, because this is only used for uniquifying. |
| 1098 | std::sort(Key.begin(), Key.end()); |
| 1099 | return Uniquifier.count(Key); |
| 1100 | } |
| 1101 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1102 | /// InsertFormula - If the given formula has not yet been inserted, add it to |
| 1103 | /// the list, and return true. Return false otherwise. |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1104 | bool LSRUse::InsertFormula(const Formula &F) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1105 | SmallVector<const SCEV *, 2> Key = F.BaseRegs; |
| 1106 | if (F.ScaledReg) Key.push_back(F.ScaledReg); |
| 1107 | // Unstable sort by host order ok, because this is only used for uniquifying. |
| 1108 | std::sort(Key.begin(), Key.end()); |
| 1109 | |
| 1110 | if (!Uniquifier.insert(Key).second) |
| 1111 | return false; |
| 1112 | |
| 1113 | // Using a register to hold the value of 0 is not profitable. |
| 1114 | assert((!F.ScaledReg || !F.ScaledReg->isZero()) && |
| 1115 | "Zero allocated in a scaled register!"); |
| 1116 | #ifndef NDEBUG |
| 1117 | for (SmallVectorImpl<const SCEV *>::const_iterator I = |
| 1118 | F.BaseRegs.begin(), E = F.BaseRegs.end(); I != E; ++I) |
| 1119 | assert(!(*I)->isZero() && "Zero allocated in a base register!"); |
| 1120 | #endif |
| 1121 | |
| 1122 | // Add the formula to the list. |
| 1123 | Formulae.push_back(F); |
| 1124 | |
| 1125 | // Record registers now being used by this use. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1126 | Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end()); |
| 1127 | |
| 1128 | return true; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Dan Gohman | d69d628 | 2010-05-18 22:39:15 +0000 | [diff] [blame] | 1131 | /// DeleteFormula - Remove the given formula from this use's list. |
| 1132 | void LSRUse::DeleteFormula(Formula &F) { |
Dan Gohman | 5ce6d05 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 1133 | if (&F != &Formulae.back()) |
| 1134 | std::swap(F, Formulae.back()); |
Dan Gohman | d69d628 | 2010-05-18 22:39:15 +0000 | [diff] [blame] | 1135 | Formulae.pop_back(); |
| 1136 | } |
| 1137 | |
Dan Gohman | b2df433 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 1138 | /// RecomputeRegs - Recompute the Regs field, and update RegUses. |
| 1139 | void LSRUse::RecomputeRegs(size_t LUIdx, RegUseTracker &RegUses) { |
| 1140 | // Now that we've filtered out some formulae, recompute the Regs set. |
| 1141 | SmallPtrSet<const SCEV *, 4> OldRegs = Regs; |
| 1142 | Regs.clear(); |
Dan Gohman | 402d435 | 2010-05-20 20:33:18 +0000 | [diff] [blame] | 1143 | for (SmallVectorImpl<Formula>::const_iterator I = Formulae.begin(), |
| 1144 | E = Formulae.end(); I != E; ++I) { |
| 1145 | const Formula &F = *I; |
Dan Gohman | b2df433 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 1146 | if (F.ScaledReg) Regs.insert(F.ScaledReg); |
| 1147 | Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end()); |
| 1148 | } |
| 1149 | |
| 1150 | // Update the RegTracker. |
| 1151 | for (SmallPtrSet<const SCEV *, 4>::iterator I = OldRegs.begin(), |
| 1152 | E = OldRegs.end(); I != E; ++I) |
| 1153 | if (!Regs.count(*I)) |
| 1154 | RegUses.DropRegister(*I, LUIdx); |
| 1155 | } |
| 1156 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1157 | void LSRUse::print(raw_ostream &OS) const { |
| 1158 | OS << "LSR Use: Kind="; |
| 1159 | switch (Kind) { |
| 1160 | case Basic: OS << "Basic"; break; |
| 1161 | case Special: OS << "Special"; break; |
| 1162 | case ICmpZero: OS << "ICmpZero"; break; |
| 1163 | case Address: |
| 1164 | OS << "Address of "; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1165 | if (AccessTy->isPointerTy()) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1166 | OS << "pointer"; // the full pointer type could be really verbose |
| 1167 | else |
| 1168 | OS << *AccessTy; |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1171 | OS << ", Offsets={"; |
| 1172 | for (SmallVectorImpl<int64_t>::const_iterator I = Offsets.begin(), |
| 1173 | E = Offsets.end(); I != E; ++I) { |
| 1174 | OS << *I; |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 1175 | if (llvm::next(I) != E) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1176 | OS << ','; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1177 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1178 | OS << '}'; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1179 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1180 | if (AllFixupsOutsideLoop) |
| 1181 | OS << ", all-fixups-outside-loop"; |
Dan Gohman | a9db129 | 2010-07-15 20:24:58 +0000 | [diff] [blame] | 1182 | |
| 1183 | if (WidestFixupType) |
| 1184 | OS << ", widest fixup type: " << *WidestFixupType; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1187 | void LSRUse::dump() const { |
| 1188 | print(errs()); errs() << '\n'; |
| 1189 | } |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1190 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1191 | /// isLegalUse - Test whether the use described by AM is "legal", meaning it can |
| 1192 | /// be completely folded into the user instruction at isel time. This includes |
| 1193 | /// address-mode folding and special icmp tricks. |
| 1194 | static bool isLegalUse(const TargetLowering::AddrMode &AM, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1195 | LSRUse::KindType Kind, Type *AccessTy, |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1196 | const TargetLowering *TLI) { |
| 1197 | switch (Kind) { |
| 1198 | case LSRUse::Address: |
| 1199 | // If we have low-level target information, ask the target if it can |
| 1200 | // completely fold this address. |
| 1201 | if (TLI) return TLI->isLegalAddressingMode(AM, AccessTy); |
| 1202 | |
| 1203 | // Otherwise, just guess that reg+reg addressing is legal. |
| 1204 | return !AM.BaseGV && AM.BaseOffs == 0 && AM.Scale <= 1; |
| 1205 | |
| 1206 | case LSRUse::ICmpZero: |
| 1207 | // There's not even a target hook for querying whether it would be legal to |
| 1208 | // fold a GV into an ICmp. |
| 1209 | if (AM.BaseGV) |
| 1210 | return false; |
| 1211 | |
| 1212 | // ICmp only has two operands; don't allow more than two non-trivial parts. |
| 1213 | if (AM.Scale != 0 && AM.HasBaseReg && AM.BaseOffs != 0) |
| 1214 | return false; |
| 1215 | |
| 1216 | // ICmp only supports no scale or a -1 scale, as we can "fold" a -1 scale by |
| 1217 | // putting the scaled register in the other operand of the icmp. |
| 1218 | if (AM.Scale != 0 && AM.Scale != -1) |
| 1219 | return false; |
| 1220 | |
| 1221 | // If we have low-level target information, ask the target if it can fold an |
| 1222 | // integer immediate on an icmp. |
| 1223 | if (AM.BaseOffs != 0) { |
Eli Friedman | dae36ba | 2011-10-13 23:48:33 +0000 | [diff] [blame] | 1224 | if (TLI) return TLI->isLegalICmpImmediate(-(uint64_t)AM.BaseOffs); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1225 | return false; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1226 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1227 | |
| 1228 | return true; |
| 1229 | |
| 1230 | case LSRUse::Basic: |
| 1231 | // Only handle single-register values. |
| 1232 | return !AM.BaseGV && AM.Scale == 0 && AM.BaseOffs == 0; |
| 1233 | |
| 1234 | case LSRUse::Special: |
| 1235 | // Only handle -1 scales, or no scale. |
| 1236 | return AM.Scale == 0 || AM.Scale == -1; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1239 | return false; |
| 1240 | } |
| 1241 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1242 | static bool isLegalUse(TargetLowering::AddrMode AM, |
| 1243 | int64_t MinOffset, int64_t MaxOffset, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1244 | LSRUse::KindType Kind, Type *AccessTy, |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1245 | const TargetLowering *TLI) { |
| 1246 | // Check for overflow. |
| 1247 | if (((int64_t)((uint64_t)AM.BaseOffs + MinOffset) > AM.BaseOffs) != |
| 1248 | (MinOffset > 0)) |
| 1249 | return false; |
| 1250 | AM.BaseOffs = (uint64_t)AM.BaseOffs + MinOffset; |
| 1251 | if (isLegalUse(AM, Kind, AccessTy, TLI)) { |
| 1252 | AM.BaseOffs = (uint64_t)AM.BaseOffs - MinOffset; |
| 1253 | // Check for overflow. |
| 1254 | if (((int64_t)((uint64_t)AM.BaseOffs + MaxOffset) > AM.BaseOffs) != |
| 1255 | (MaxOffset > 0)) |
| 1256 | return false; |
| 1257 | AM.BaseOffs = (uint64_t)AM.BaseOffs + MaxOffset; |
| 1258 | return isLegalUse(AM, Kind, AccessTy, TLI); |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1259 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1260 | return false; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1263 | static bool isAlwaysFoldable(int64_t BaseOffs, |
| 1264 | GlobalValue *BaseGV, |
| 1265 | bool HasBaseReg, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1266 | LSRUse::KindType Kind, Type *AccessTy, |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1267 | const TargetLowering *TLI) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1268 | // Fast-path: zero is always foldable. |
| 1269 | if (BaseOffs == 0 && !BaseGV) return true; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1270 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1271 | // Conservatively, create an address with an immediate and a |
| 1272 | // base and a scale. |
| 1273 | TargetLowering::AddrMode AM; |
| 1274 | AM.BaseOffs = BaseOffs; |
| 1275 | AM.BaseGV = BaseGV; |
| 1276 | AM.HasBaseReg = HasBaseReg; |
| 1277 | AM.Scale = Kind == LSRUse::ICmpZero ? -1 : 1; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1278 | |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 1279 | // Canonicalize a scale of 1 to a base register if the formula doesn't |
| 1280 | // already have a base register. |
| 1281 | if (!AM.HasBaseReg && AM.Scale == 1) { |
| 1282 | AM.Scale = 0; |
| 1283 | AM.HasBaseReg = true; |
| 1284 | } |
| 1285 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1286 | return isLegalUse(AM, Kind, AccessTy, TLI); |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1289 | static bool isAlwaysFoldable(const SCEV *S, |
| 1290 | int64_t MinOffset, int64_t MaxOffset, |
| 1291 | bool HasBaseReg, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1292 | LSRUse::KindType Kind, Type *AccessTy, |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1293 | const TargetLowering *TLI, |
| 1294 | ScalarEvolution &SE) { |
| 1295 | // Fast-path: zero is always foldable. |
| 1296 | if (S->isZero()) return true; |
| 1297 | |
| 1298 | // Conservatively, create an address with an immediate and a |
| 1299 | // base and a scale. |
| 1300 | int64_t BaseOffs = ExtractImmediate(S, SE); |
| 1301 | GlobalValue *BaseGV = ExtractSymbol(S, SE); |
| 1302 | |
| 1303 | // If there's anything else involved, it's not foldable. |
| 1304 | if (!S->isZero()) return false; |
| 1305 | |
| 1306 | // Fast-path: zero is always foldable. |
| 1307 | if (BaseOffs == 0 && !BaseGV) return true; |
| 1308 | |
| 1309 | // Conservatively, create an address with an immediate and a |
| 1310 | // base and a scale. |
| 1311 | TargetLowering::AddrMode AM; |
| 1312 | AM.BaseOffs = BaseOffs; |
| 1313 | AM.BaseGV = BaseGV; |
| 1314 | AM.HasBaseReg = HasBaseReg; |
| 1315 | AM.Scale = Kind == LSRUse::ICmpZero ? -1 : 1; |
| 1316 | |
| 1317 | return isLegalUse(AM, MinOffset, MaxOffset, Kind, AccessTy, TLI); |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Dan Gohman | b621171 | 2010-06-19 21:21:39 +0000 | [diff] [blame] | 1320 | namespace { |
| 1321 | |
Dan Gohman | 1e3121c | 2010-06-19 21:29:59 +0000 | [diff] [blame] | 1322 | /// UseMapDenseMapInfo - A DenseMapInfo implementation for holding |
| 1323 | /// DenseMaps and DenseSets of pairs of const SCEV* and LSRUse::Kind. |
| 1324 | struct UseMapDenseMapInfo { |
| 1325 | static std::pair<const SCEV *, LSRUse::KindType> getEmptyKey() { |
| 1326 | return std::make_pair(reinterpret_cast<const SCEV *>(-1), LSRUse::Basic); |
| 1327 | } |
| 1328 | |
| 1329 | static std::pair<const SCEV *, LSRUse::KindType> getTombstoneKey() { |
| 1330 | return std::make_pair(reinterpret_cast<const SCEV *>(-2), LSRUse::Basic); |
| 1331 | } |
| 1332 | |
| 1333 | static unsigned |
| 1334 | getHashValue(const std::pair<const SCEV *, LSRUse::KindType> &V) { |
| 1335 | unsigned Result = DenseMapInfo<const SCEV *>::getHashValue(V.first); |
| 1336 | Result ^= DenseMapInfo<unsigned>::getHashValue(unsigned(V.second)); |
| 1337 | return Result; |
| 1338 | } |
| 1339 | |
| 1340 | static bool isEqual(const std::pair<const SCEV *, LSRUse::KindType> &LHS, |
| 1341 | const std::pair<const SCEV *, LSRUse::KindType> &RHS) { |
| 1342 | return LHS == RHS; |
| 1343 | } |
| 1344 | }; |
| 1345 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1346 | /// LSRInstance - This class holds state for the main loop strength reduction |
| 1347 | /// logic. |
| 1348 | class LSRInstance { |
| 1349 | IVUsers &IU; |
| 1350 | ScalarEvolution &SE; |
| 1351 | DominatorTree &DT; |
Dan Gohman | e5f7687 | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 1352 | LoopInfo &LI; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1353 | const TargetLowering *const TLI; |
| 1354 | Loop *const L; |
| 1355 | bool Changed; |
| 1356 | |
| 1357 | /// IVIncInsertPos - This is the insert position that the current loop's |
| 1358 | /// induction variable increment should be placed. In simple loops, this is |
| 1359 | /// the latch block's terminator. But in more complicated cases, this is a |
| 1360 | /// position which will dominate all the in-loop post-increment users. |
| 1361 | Instruction *IVIncInsertPos; |
| 1362 | |
| 1363 | /// Factors - Interesting factors between use strides. |
| 1364 | SmallSetVector<int64_t, 8> Factors; |
| 1365 | |
| 1366 | /// Types - Interesting use types, to facilitate truncation reuse. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1367 | SmallSetVector<Type *, 4> Types; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1368 | |
| 1369 | /// Fixups - The list of operands which are to be replaced. |
| 1370 | SmallVector<LSRFixup, 16> Fixups; |
| 1371 | |
| 1372 | /// Uses - The list of interesting uses. |
| 1373 | SmallVector<LSRUse, 16> Uses; |
| 1374 | |
| 1375 | /// RegUses - Track which uses use which register candidates. |
| 1376 | RegUseTracker RegUses; |
| 1377 | |
| 1378 | void OptimizeShadowIV(); |
| 1379 | bool FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse); |
| 1380 | ICmpInst *OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse); |
Dan Gohman | c6519f9 | 2010-05-20 20:05:31 +0000 | [diff] [blame] | 1381 | void OptimizeLoopTermCond(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1382 | |
| 1383 | void CollectInterestingTypesAndFactors(); |
| 1384 | void CollectFixupsAndInitialFormulae(); |
| 1385 | |
| 1386 | LSRFixup &getNewFixup() { |
| 1387 | Fixups.push_back(LSRFixup()); |
| 1388 | return Fixups.back(); |
| 1389 | } |
| 1390 | |
| 1391 | // Support for sharing of LSRUses between LSRFixups. |
Dan Gohman | 1e3121c | 2010-06-19 21:29:59 +0000 | [diff] [blame] | 1392 | typedef DenseMap<std::pair<const SCEV *, LSRUse::KindType>, |
| 1393 | size_t, |
| 1394 | UseMapDenseMapInfo> UseMapTy; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1395 | UseMapTy UseMap; |
| 1396 | |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1397 | bool reconcileNewOffset(LSRUse &LU, int64_t NewOffset, bool HasBaseReg, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1398 | LSRUse::KindType Kind, Type *AccessTy); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1399 | |
| 1400 | std::pair<size_t, int64_t> getUse(const SCEV *&Expr, |
| 1401 | LSRUse::KindType Kind, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1402 | Type *AccessTy); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1403 | |
Dan Gohman | c689770 | 2010-10-07 23:33:43 +0000 | [diff] [blame] | 1404 | void DeleteUse(LSRUse &LU, size_t LUIdx); |
Dan Gohman | 5ce6d05 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 1405 | |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1406 | LSRUse *FindUseWithSimilarFormula(const Formula &F, const LSRUse &OrigLU); |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 1407 | |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1408 | void InsertInitialFormula(const SCEV *S, LSRUse &LU, size_t LUIdx); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1409 | void InsertSupplementalFormula(const SCEV *S, LSRUse &LU, size_t LUIdx); |
| 1410 | void CountRegisters(const Formula &F, size_t LUIdx); |
| 1411 | bool InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F); |
| 1412 | |
| 1413 | void CollectLoopInvariantFixupsAndFormulae(); |
| 1414 | |
| 1415 | void GenerateReassociations(LSRUse &LU, unsigned LUIdx, Formula Base, |
| 1416 | unsigned Depth = 0); |
| 1417 | void GenerateCombinations(LSRUse &LU, unsigned LUIdx, Formula Base); |
| 1418 | void GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx, Formula Base); |
| 1419 | void GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx, Formula Base); |
| 1420 | void GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx, Formula Base); |
| 1421 | void GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base); |
| 1422 | void GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base); |
| 1423 | void GenerateCrossUseConstantOffsets(); |
| 1424 | void GenerateAllReuseFormulae(); |
| 1425 | |
| 1426 | void FilterOutUndesirableDedicatedRegisters(); |
Dan Gohman | d079c30 | 2010-05-18 22:51:59 +0000 | [diff] [blame] | 1427 | |
| 1428 | size_t EstimateSearchSpaceComplexity() const; |
Dan Gohman | 4aa5c2e | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 1429 | void NarrowSearchSpaceByDetectingSupersets(); |
| 1430 | void NarrowSearchSpaceByCollapsingUnrolledCode(); |
Dan Gohman | 4f7e18d | 2010-08-29 16:39:22 +0000 | [diff] [blame] | 1431 | void NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters(); |
Dan Gohman | 4aa5c2e | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 1432 | void NarrowSearchSpaceByPickingWinnerRegs(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1433 | void NarrowSearchSpaceUsingHeuristics(); |
| 1434 | |
| 1435 | void SolveRecurse(SmallVectorImpl<const Formula *> &Solution, |
| 1436 | Cost &SolutionCost, |
| 1437 | SmallVectorImpl<const Formula *> &Workspace, |
| 1438 | const Cost &CurCost, |
| 1439 | const SmallPtrSet<const SCEV *, 16> &CurRegs, |
| 1440 | DenseSet<const SCEV *> &VisitedRegs) const; |
| 1441 | void Solve(SmallVectorImpl<const Formula *> &Solution) const; |
| 1442 | |
Dan Gohman | e5f7687 | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 1443 | BasicBlock::iterator |
| 1444 | HoistInsertPosition(BasicBlock::iterator IP, |
| 1445 | const SmallVectorImpl<Instruction *> &Inputs) const; |
| 1446 | BasicBlock::iterator AdjustInsertPositionForExpand(BasicBlock::iterator IP, |
| 1447 | const LSRFixup &LF, |
| 1448 | const LSRUse &LU) const; |
Dan Gohman | d96eae8 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 1449 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1450 | Value *Expand(const LSRFixup &LF, |
| 1451 | const Formula &F, |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1452 | BasicBlock::iterator IP, |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1453 | SCEVExpander &Rewriter, |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1454 | SmallVectorImpl<WeakVH> &DeadInsts) const; |
Dan Gohman | 3a02cbc | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 1455 | void RewriteForPHI(PHINode *PN, const LSRFixup &LF, |
| 1456 | const Formula &F, |
Dan Gohman | 3a02cbc | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 1457 | SCEVExpander &Rewriter, |
| 1458 | SmallVectorImpl<WeakVH> &DeadInsts, |
Dan Gohman | 3a02cbc | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 1459 | Pass *P) const; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1460 | void Rewrite(const LSRFixup &LF, |
| 1461 | const Formula &F, |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1462 | SCEVExpander &Rewriter, |
| 1463 | SmallVectorImpl<WeakVH> &DeadInsts, |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1464 | Pass *P) const; |
| 1465 | void ImplementSolution(const SmallVectorImpl<const Formula *> &Solution, |
| 1466 | Pass *P); |
| 1467 | |
Andrew Trick | d56ef8d | 2011-12-13 00:55:33 +0000 | [diff] [blame] | 1468 | public: |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1469 | LSRInstance(const TargetLowering *tli, Loop *l, Pass *P); |
| 1470 | |
| 1471 | bool getChanged() const { return Changed; } |
| 1472 | |
| 1473 | void print_factors_and_types(raw_ostream &OS) const; |
| 1474 | void print_fixups(raw_ostream &OS) const; |
| 1475 | void print_uses(raw_ostream &OS) const; |
| 1476 | void print(raw_ostream &OS) const; |
| 1477 | void dump() const; |
| 1478 | }; |
| 1479 | |
| 1480 | } |
| 1481 | |
| 1482 | /// OptimizeShadowIV - If IV is used in a int-to-float cast |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 1483 | /// inside the loop then try to eliminate the cast operation. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1484 | void LSRInstance::OptimizeShadowIV() { |
| 1485 | const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L); |
| 1486 | if (isa<SCEVCouldNotCompute>(BackedgeTakenCount)) |
| 1487 | return; |
| 1488 | |
| 1489 | for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); |
| 1490 | UI != E; /* empty */) { |
| 1491 | IVUsers::const_iterator CandidateUI = UI; |
| 1492 | ++UI; |
| 1493 | Instruction *ShadowUse = CandidateUI->getUser(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1494 | Type *DestTy = NULL; |
Andrew Trick | c2c988e | 2011-07-21 01:05:01 +0000 | [diff] [blame] | 1495 | bool IsSigned = false; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1496 | |
| 1497 | /* If shadow use is a int->float cast then insert a second IV |
| 1498 | to eliminate this cast. |
| 1499 | |
| 1500 | for (unsigned i = 0; i < n; ++i) |
| 1501 | foo((double)i); |
| 1502 | |
| 1503 | is transformed into |
| 1504 | |
| 1505 | double d = 0.0; |
| 1506 | for (unsigned i = 0; i < n; ++i, ++d) |
| 1507 | foo(d); |
| 1508 | */ |
Andrew Trick | c2c988e | 2011-07-21 01:05:01 +0000 | [diff] [blame] | 1509 | if (UIToFPInst *UCast = dyn_cast<UIToFPInst>(CandidateUI->getUser())) { |
| 1510 | IsSigned = false; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1511 | DestTy = UCast->getDestTy(); |
Andrew Trick | c2c988e | 2011-07-21 01:05:01 +0000 | [diff] [blame] | 1512 | } |
| 1513 | else if (SIToFPInst *SCast = dyn_cast<SIToFPInst>(CandidateUI->getUser())) { |
| 1514 | IsSigned = true; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1515 | DestTy = SCast->getDestTy(); |
Andrew Trick | c2c988e | 2011-07-21 01:05:01 +0000 | [diff] [blame] | 1516 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1517 | if (!DestTy) continue; |
| 1518 | |
| 1519 | if (TLI) { |
| 1520 | // If target does not support DestTy natively then do not apply |
| 1521 | // this transformation. |
| 1522 | EVT DVT = TLI->getValueType(DestTy); |
| 1523 | if (!TLI->isTypeLegal(DVT)) continue; |
| 1524 | } |
| 1525 | |
| 1526 | PHINode *PH = dyn_cast<PHINode>(ShadowUse->getOperand(0)); |
| 1527 | if (!PH) continue; |
| 1528 | if (PH->getNumIncomingValues() != 2) continue; |
| 1529 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1530 | Type *SrcTy = PH->getType(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1531 | int Mantissa = DestTy->getFPMantissaWidth(); |
| 1532 | if (Mantissa == -1) continue; |
| 1533 | if ((int)SE.getTypeSizeInBits(SrcTy) > Mantissa) |
| 1534 | continue; |
| 1535 | |
| 1536 | unsigned Entry, Latch; |
| 1537 | if (PH->getIncomingBlock(0) == L->getLoopPreheader()) { |
| 1538 | Entry = 0; |
| 1539 | Latch = 1; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1540 | } else { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1541 | Entry = 1; |
| 1542 | Latch = 0; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1543 | } |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1544 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1545 | ConstantInt *Init = dyn_cast<ConstantInt>(PH->getIncomingValue(Entry)); |
| 1546 | if (!Init) continue; |
Andrew Trick | c2c988e | 2011-07-21 01:05:01 +0000 | [diff] [blame] | 1547 | Constant *NewInit = ConstantFP::get(DestTy, IsSigned ? |
Andrew Trick | c205a09 | 2011-07-21 01:45:54 +0000 | [diff] [blame] | 1548 | (double)Init->getSExtValue() : |
| 1549 | (double)Init->getZExtValue()); |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1550 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1551 | BinaryOperator *Incr = |
| 1552 | dyn_cast<BinaryOperator>(PH->getIncomingValue(Latch)); |
| 1553 | if (!Incr) continue; |
| 1554 | if (Incr->getOpcode() != Instruction::Add |
| 1555 | && Incr->getOpcode() != Instruction::Sub) |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1556 | continue; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1557 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1558 | /* Initialize new IV, double d = 0.0 in above example. */ |
| 1559 | ConstantInt *C = NULL; |
| 1560 | if (Incr->getOperand(0) == PH) |
| 1561 | C = dyn_cast<ConstantInt>(Incr->getOperand(1)); |
| 1562 | else if (Incr->getOperand(1) == PH) |
| 1563 | C = dyn_cast<ConstantInt>(Incr->getOperand(0)); |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1564 | else |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1565 | continue; |
| 1566 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1567 | if (!C) continue; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1568 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1569 | // Ignore negative constants, as the code below doesn't handle them |
| 1570 | // correctly. TODO: Remove this restriction. |
| 1571 | if (!C->getValue().isStrictlyPositive()) continue; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1572 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1573 | /* Add new PHINode. */ |
Jay Foad | 3ecfc86 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 1574 | PHINode *NewPH = PHINode::Create(DestTy, 2, "IV.S.", PH); |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1575 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1576 | /* create new increment. '++d' in above example. */ |
| 1577 | Constant *CFP = ConstantFP::get(DestTy, C->getZExtValue()); |
| 1578 | BinaryOperator *NewIncr = |
| 1579 | BinaryOperator::Create(Incr->getOpcode() == Instruction::Add ? |
| 1580 | Instruction::FAdd : Instruction::FSub, |
| 1581 | NewPH, CFP, "IV.S.next.", Incr); |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1582 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1583 | NewPH->addIncoming(NewInit, PH->getIncomingBlock(Entry)); |
| 1584 | NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch)); |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1585 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1586 | /* Remove cast operation */ |
| 1587 | ShadowUse->replaceAllUsesWith(NewPH); |
| 1588 | ShadowUse->eraseFromParent(); |
Dan Gohman | c6519f9 | 2010-05-20 20:05:31 +0000 | [diff] [blame] | 1589 | Changed = true; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1590 | break; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | /// FindIVUserForCond - If Cond has an operand that is an expression of an IV, |
| 1595 | /// set the IV user and stride information and return true, otherwise return |
| 1596 | /// false. |
Dan Gohman | ea507f5 | 2010-05-20 19:44:23 +0000 | [diff] [blame] | 1597 | bool LSRInstance::FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1598 | for (IVUsers::iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) |
| 1599 | if (UI->getUser() == Cond) { |
| 1600 | // NOTE: we could handle setcc instructions with multiple uses here, but |
| 1601 | // InstCombine does it as well for simple uses, it's not clear that it |
| 1602 | // occurs enough in real life to handle. |
| 1603 | CondUse = UI; |
| 1604 | return true; |
| 1605 | } |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1606 | return false; |
Evan Cheng | cdf43b1 | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1609 | /// OptimizeMax - Rewrite the loop's terminating condition if it uses |
| 1610 | /// a max computation. |
| 1611 | /// |
| 1612 | /// This is a narrow solution to a specific, but acute, problem. For loops |
| 1613 | /// like this: |
| 1614 | /// |
| 1615 | /// i = 0; |
| 1616 | /// do { |
| 1617 | /// p[i] = 0.0; |
| 1618 | /// } while (++i < n); |
| 1619 | /// |
| 1620 | /// the trip count isn't just 'n', because 'n' might not be positive. And |
| 1621 | /// unfortunately this can come up even for loops where the user didn't use |
| 1622 | /// a C do-while loop. For example, seemingly well-behaved top-test loops |
| 1623 | /// will commonly be lowered like this: |
| 1624 | // |
| 1625 | /// if (n > 0) { |
| 1626 | /// i = 0; |
| 1627 | /// do { |
| 1628 | /// p[i] = 0.0; |
| 1629 | /// } while (++i < n); |
| 1630 | /// } |
| 1631 | /// |
| 1632 | /// and then it's possible for subsequent optimization to obscure the if |
| 1633 | /// test in such a way that indvars can't find it. |
| 1634 | /// |
| 1635 | /// When indvars can't find the if test in loops like this, it creates a |
| 1636 | /// max expression, which allows it to give the loop a canonical |
| 1637 | /// induction variable: |
| 1638 | /// |
| 1639 | /// i = 0; |
| 1640 | /// max = n < 1 ? 1 : n; |
| 1641 | /// do { |
| 1642 | /// p[i] = 0.0; |
| 1643 | /// } while (++i != max); |
| 1644 | /// |
| 1645 | /// Canonical induction variables are necessary because the loop passes |
| 1646 | /// are designed around them. The most obvious example of this is the |
| 1647 | /// LoopInfo analysis, which doesn't remember trip count values. It |
| 1648 | /// expects to be able to rediscover the trip count each time it is |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1649 | /// needed, and it does this using a simple analysis that only succeeds if |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1650 | /// the loop has a canonical induction variable. |
| 1651 | /// |
| 1652 | /// However, when it comes time to generate code, the maximum operation |
| 1653 | /// can be quite costly, especially if it's inside of an outer loop. |
| 1654 | /// |
| 1655 | /// This function solves this problem by detecting this type of loop and |
| 1656 | /// rewriting their conditions from ICMP_NE back to ICMP_SLT, and deleting |
| 1657 | /// the instructions for the maximum computation. |
| 1658 | /// |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1659 | ICmpInst *LSRInstance::OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse) { |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1660 | // Check that the loop matches the pattern we're looking for. |
| 1661 | if (Cond->getPredicate() != CmpInst::ICMP_EQ && |
| 1662 | Cond->getPredicate() != CmpInst::ICMP_NE) |
| 1663 | return Cond; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1664 | |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1665 | SelectInst *Sel = dyn_cast<SelectInst>(Cond->getOperand(1)); |
| 1666 | if (!Sel || !Sel->hasOneUse()) return Cond; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1667 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1668 | const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L); |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1669 | if (isa<SCEVCouldNotCompute>(BackedgeTakenCount)) |
| 1670 | return Cond; |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1671 | const SCEV *One = SE.getConstant(BackedgeTakenCount->getType(), 1); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1672 | |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1673 | // Add one to the backedge-taken count to get the trip count. |
Dan Gohman | 4065f60 | 2010-08-16 15:39:27 +0000 | [diff] [blame] | 1674 | const SCEV *IterationCount = SE.getAddExpr(One, BackedgeTakenCount); |
Dan Gohman | 1d36798 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 1675 | if (IterationCount != SE.getSCEV(Sel)) return Cond; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1676 | |
Dan Gohman | 1d36798 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 1677 | // Check for a max calculation that matches the pattern. There's no check |
| 1678 | // for ICMP_ULE here because the comparison would be with zero, which |
| 1679 | // isn't interesting. |
| 1680 | CmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE; |
| 1681 | const SCEVNAryExpr *Max = 0; |
| 1682 | if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(BackedgeTakenCount)) { |
| 1683 | Pred = ICmpInst::ICMP_SLE; |
| 1684 | Max = S; |
| 1685 | } else if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(IterationCount)) { |
| 1686 | Pred = ICmpInst::ICMP_SLT; |
| 1687 | Max = S; |
| 1688 | } else if (const SCEVUMaxExpr *U = dyn_cast<SCEVUMaxExpr>(IterationCount)) { |
| 1689 | Pred = ICmpInst::ICMP_ULT; |
| 1690 | Max = U; |
| 1691 | } else { |
| 1692 | // No match; bail. |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1693 | return Cond; |
Dan Gohman | 1d36798 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 1694 | } |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1695 | |
| 1696 | // To handle a max with more than two operands, this optimization would |
| 1697 | // require additional checking and setup. |
| 1698 | if (Max->getNumOperands() != 2) |
| 1699 | return Cond; |
| 1700 | |
| 1701 | const SCEV *MaxLHS = Max->getOperand(0); |
| 1702 | const SCEV *MaxRHS = Max->getOperand(1); |
Dan Gohman | 1d36798 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 1703 | |
| 1704 | // ScalarEvolution canonicalizes constants to the left. For < and >, look |
| 1705 | // for a comparison with 1. For <= and >=, a comparison with zero. |
| 1706 | if (!MaxLHS || |
| 1707 | (ICmpInst::isTrueWhenEqual(Pred) ? !MaxLHS->isZero() : (MaxLHS != One))) |
| 1708 | return Cond; |
| 1709 | |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1710 | // Check the relevant induction variable for conformance to |
| 1711 | // the pattern. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1712 | const SCEV *IV = SE.getSCEV(Cond->getOperand(0)); |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1713 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(IV); |
| 1714 | if (!AR || !AR->isAffine() || |
| 1715 | AR->getStart() != One || |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1716 | AR->getStepRecurrence(SE) != One) |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1717 | return Cond; |
| 1718 | |
| 1719 | assert(AR->getLoop() == L && |
| 1720 | "Loop condition operand is an addrec in a different loop!"); |
| 1721 | |
| 1722 | // Check the right operand of the select, and remember it, as it will |
| 1723 | // be used in the new comparison instruction. |
| 1724 | Value *NewRHS = 0; |
Dan Gohman | 1d36798 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 1725 | if (ICmpInst::isTrueWhenEqual(Pred)) { |
| 1726 | // Look for n+1, and grab n. |
| 1727 | if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(1))) |
| 1728 | if (isa<ConstantInt>(BO->getOperand(1)) && |
| 1729 | cast<ConstantInt>(BO->getOperand(1))->isOne() && |
| 1730 | SE.getSCEV(BO->getOperand(0)) == MaxRHS) |
| 1731 | NewRHS = BO->getOperand(0); |
| 1732 | if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(2))) |
| 1733 | if (isa<ConstantInt>(BO->getOperand(1)) && |
| 1734 | cast<ConstantInt>(BO->getOperand(1))->isOne() && |
| 1735 | SE.getSCEV(BO->getOperand(0)) == MaxRHS) |
| 1736 | NewRHS = BO->getOperand(0); |
| 1737 | if (!NewRHS) |
| 1738 | return Cond; |
| 1739 | } else if (SE.getSCEV(Sel->getOperand(1)) == MaxRHS) |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1740 | NewRHS = Sel->getOperand(1); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1741 | else if (SE.getSCEV(Sel->getOperand(2)) == MaxRHS) |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1742 | NewRHS = Sel->getOperand(2); |
Dan Gohman | caf71ab | 2010-06-22 23:07:13 +0000 | [diff] [blame] | 1743 | else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(MaxRHS)) |
| 1744 | NewRHS = SU->getValue(); |
Dan Gohman | 1d36798 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 1745 | else |
Dan Gohman | caf71ab | 2010-06-22 23:07:13 +0000 | [diff] [blame] | 1746 | // Max doesn't match expected pattern. |
| 1747 | return Cond; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1748 | |
| 1749 | // Determine the new comparison opcode. It may be signed or unsigned, |
| 1750 | // and the original comparison may be either equality or inequality. |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1751 | if (Cond->getPredicate() == CmpInst::ICMP_EQ) |
| 1752 | Pred = CmpInst::getInversePredicate(Pred); |
| 1753 | |
| 1754 | // Ok, everything looks ok to change the condition into an SLT or SGE and |
| 1755 | // delete the max calculation. |
| 1756 | ICmpInst *NewCond = |
| 1757 | new ICmpInst(Cond, Pred, Cond->getOperand(0), NewRHS, "scmp"); |
| 1758 | |
| 1759 | // Delete the max calculation instructions. |
| 1760 | Cond->replaceAllUsesWith(NewCond); |
| 1761 | CondUse->setUser(NewCond); |
| 1762 | Instruction *Cmp = cast<Instruction>(Sel->getOperand(0)); |
| 1763 | Cond->eraseFromParent(); |
| 1764 | Sel->eraseFromParent(); |
| 1765 | if (Cmp->use_empty()) |
| 1766 | Cmp->eraseFromParent(); |
| 1767 | return NewCond; |
Dan Gohman | ad7321f | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Jim Grosbach | 56a1f80 | 2009-11-17 17:53:56 +0000 | [diff] [blame] | 1770 | /// OptimizeLoopTermCond - Change loop terminating condition to use the |
Evan Cheng | 586f69a | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 1771 | /// postinc iv when possible. |
Dan Gohman | c6519f9 | 2010-05-20 20:05:31 +0000 | [diff] [blame] | 1772 | void |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1773 | LSRInstance::OptimizeLoopTermCond() { |
| 1774 | SmallPtrSet<Instruction *, 4> PostIncs; |
| 1775 | |
Evan Cheng | 586f69a | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 1776 | BasicBlock *LatchBlock = L->getLoopLatch(); |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1777 | SmallVector<BasicBlock*, 8> ExitingBlocks; |
| 1778 | L->getExitingBlocks(ExitingBlocks); |
Jim Grosbach | 56a1f80 | 2009-11-17 17:53:56 +0000 | [diff] [blame] | 1779 | |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1780 | for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { |
| 1781 | BasicBlock *ExitingBlock = ExitingBlocks[i]; |
Evan Cheng | 586f69a | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 1782 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1783 | // Get the terminating condition for the loop if possible. If we |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1784 | // can, we want to change it to use a post-incremented version of its |
| 1785 | // induction variable, to allow coalescing the live ranges for the IV into |
| 1786 | // one register value. |
Evan Cheng | 586f69a | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 1787 | |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1788 | BranchInst *TermBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); |
| 1789 | if (!TermBr) |
| 1790 | continue; |
| 1791 | // FIXME: Overly conservative, termination condition could be an 'or' etc.. |
| 1792 | if (TermBr->isUnconditional() || !isa<ICmpInst>(TermBr->getCondition())) |
| 1793 | continue; |
Evan Cheng | 586f69a | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 1794 | |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1795 | // Search IVUsesByStride to find Cond's IVUse if there is one. |
| 1796 | IVStrideUse *CondUse = 0; |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1797 | ICmpInst *Cond = cast<ICmpInst>(TermBr->getCondition()); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1798 | if (!FindIVUserForCond(Cond, CondUse)) |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1799 | continue; |
| 1800 | |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1801 | // If the trip count is computed in terms of a max (due to ScalarEvolution |
| 1802 | // being unable to find a sufficient guard, for example), change the loop |
| 1803 | // comparison to use SLT or ULT instead of NE. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1804 | // One consequence of doing this now is that it disrupts the count-down |
| 1805 | // optimization. That's not always a bad thing though, because in such |
| 1806 | // cases it may still be worthwhile to avoid a max. |
| 1807 | Cond = OptimizeMax(Cond, CondUse); |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1808 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1809 | // If this exiting block dominates the latch block, it may also use |
| 1810 | // the post-inc value if it won't be shared with other uses. |
| 1811 | // Check for dominance. |
| 1812 | if (!DT.dominates(ExitingBlock, LatchBlock)) |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1813 | continue; |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1814 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1815 | // Conservatively avoid trying to use the post-inc value in non-latch |
| 1816 | // exits if there may be pre-inc users in intervening blocks. |
Dan Gohman | 590bfe8 | 2010-02-14 03:21:49 +0000 | [diff] [blame] | 1817 | if (LatchBlock != ExitingBlock) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1818 | for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) |
| 1819 | // Test if the use is reachable from the exiting block. This dominator |
| 1820 | // query is a conservative approximation of reachability. |
| 1821 | if (&*UI != CondUse && |
| 1822 | !DT.properlyDominates(UI->getUser()->getParent(), ExitingBlock)) { |
| 1823 | // Conservatively assume there may be reuse if the quotient of their |
| 1824 | // strides could be a legal scale. |
Dan Gohman | c056454 | 2010-04-19 21:48:58 +0000 | [diff] [blame] | 1825 | const SCEV *A = IU.getStride(*CondUse, L); |
| 1826 | const SCEV *B = IU.getStride(*UI, L); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1827 | if (!A || !B) continue; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1828 | if (SE.getTypeSizeInBits(A->getType()) != |
| 1829 | SE.getTypeSizeInBits(B->getType())) { |
| 1830 | if (SE.getTypeSizeInBits(A->getType()) > |
| 1831 | SE.getTypeSizeInBits(B->getType())) |
| 1832 | B = SE.getSignExtendExpr(B, A->getType()); |
| 1833 | else |
| 1834 | A = SE.getSignExtendExpr(A, B->getType()); |
| 1835 | } |
| 1836 | if (const SCEVConstant *D = |
Dan Gohman | f09b712 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 1837 | dyn_cast_or_null<SCEVConstant>(getExactSDiv(B, A, SE))) { |
Dan Gohman | 9f383eb | 2010-05-20 22:25:20 +0000 | [diff] [blame] | 1838 | const ConstantInt *C = D->getValue(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1839 | // Stride of one or negative one can have reuse with non-addresses. |
Dan Gohman | 9f383eb | 2010-05-20 22:25:20 +0000 | [diff] [blame] | 1840 | if (C->isOne() || C->isAllOnesValue()) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1841 | goto decline_post_inc; |
| 1842 | // Avoid weird situations. |
Dan Gohman | 9f383eb | 2010-05-20 22:25:20 +0000 | [diff] [blame] | 1843 | if (C->getValue().getMinSignedBits() >= 64 || |
| 1844 | C->getValue().isMinSignedValue()) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1845 | goto decline_post_inc; |
Dan Gohman | 590bfe8 | 2010-02-14 03:21:49 +0000 | [diff] [blame] | 1846 | // Without TLI, assume that any stride might be valid, and so any |
| 1847 | // use might be shared. |
| 1848 | if (!TLI) |
| 1849 | goto decline_post_inc; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1850 | // Check for possible scaled-address reuse. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1851 | Type *AccessTy = getAccessType(UI->getUser()); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1852 | TargetLowering::AddrMode AM; |
Dan Gohman | 9f383eb | 2010-05-20 22:25:20 +0000 | [diff] [blame] | 1853 | AM.Scale = C->getSExtValue(); |
Dan Gohman | 2763dfd | 2010-02-14 02:45:21 +0000 | [diff] [blame] | 1854 | if (TLI->isLegalAddressingMode(AM, AccessTy)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1855 | goto decline_post_inc; |
| 1856 | AM.Scale = -AM.Scale; |
Dan Gohman | 2763dfd | 2010-02-14 02:45:21 +0000 | [diff] [blame] | 1857 | if (TLI->isLegalAddressingMode(AM, AccessTy)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1858 | goto decline_post_inc; |
| 1859 | } |
| 1860 | } |
| 1861 | |
David Greene | 63c9463 | 2009-12-23 22:58:38 +0000 | [diff] [blame] | 1862 | DEBUG(dbgs() << " Change loop exiting icmp to use postinc iv: " |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1863 | << *Cond << '\n'); |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1864 | |
| 1865 | // It's possible for the setcc instruction to be anywhere in the loop, and |
| 1866 | // possible for it to have multiple users. If it is not immediately before |
| 1867 | // the exiting block branch, move it. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1868 | if (&*++BasicBlock::iterator(Cond) != TermBr) { |
| 1869 | if (Cond->hasOneUse()) { |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1870 | Cond->moveBefore(TermBr); |
| 1871 | } else { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1872 | // Clone the terminating condition and insert into the loopend. |
| 1873 | ICmpInst *OldCond = Cond; |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1874 | Cond = cast<ICmpInst>(Cond->clone()); |
| 1875 | Cond->setName(L->getHeader()->getName() + ".termcond"); |
| 1876 | ExitingBlock->getInstList().insert(TermBr, Cond); |
| 1877 | |
| 1878 | // Clone the IVUse, as the old use still exists! |
Andrew Trick | 4417e53 | 2011-06-21 15:43:52 +0000 | [diff] [blame] | 1879 | CondUse = &IU.AddUser(Cond, CondUse->getOperandValToReplace()); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1880 | TermBr->replaceUsesOfWith(OldCond, Cond); |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1881 | } |
Evan Cheng | 586f69a | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1884 | // If we get to here, we know that we can transform the setcc instruction to |
| 1885 | // use the post-incremented version of the IV, allowing us to coalesce the |
| 1886 | // live ranges for the IV correctly. |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1887 | CondUse->transformToPostInc(L); |
Evan Cheng | 076e085 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 1888 | Changed = true; |
| 1889 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1890 | PostIncs.insert(Cond); |
| 1891 | decline_post_inc:; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1892 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1893 | |
| 1894 | // Determine an insertion point for the loop induction variable increment. It |
| 1895 | // must dominate all the post-inc comparisons we just set up, and it must |
| 1896 | // dominate the loop latch edge. |
| 1897 | IVIncInsertPos = L->getLoopLatch()->getTerminator(); |
| 1898 | for (SmallPtrSet<Instruction *, 4>::const_iterator I = PostIncs.begin(), |
| 1899 | E = PostIncs.end(); I != E; ++I) { |
| 1900 | BasicBlock *BB = |
| 1901 | DT.findNearestCommonDominator(IVIncInsertPos->getParent(), |
| 1902 | (*I)->getParent()); |
| 1903 | if (BB == (*I)->getParent()) |
| 1904 | IVIncInsertPos = *I; |
| 1905 | else if (BB != IVIncInsertPos->getParent()) |
| 1906 | IVIncInsertPos = BB->getTerminator(); |
| 1907 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1910 | /// reconcileNewOffset - Determine if the given use can accommodate a fixup |
Dan Gohman | 76c315a | 2010-05-20 20:52:00 +0000 | [diff] [blame] | 1911 | /// at the given offset and other details. If so, update the use and |
| 1912 | /// return true. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1913 | bool |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1914 | LSRInstance::reconcileNewOffset(LSRUse &LU, int64_t NewOffset, bool HasBaseReg, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1915 | LSRUse::KindType Kind, Type *AccessTy) { |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1916 | int64_t NewMinOffset = LU.MinOffset; |
| 1917 | int64_t NewMaxOffset = LU.MaxOffset; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1918 | Type *NewAccessTy = AccessTy; |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1919 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1920 | // Check for a mismatched kind. It's tempting to collapse mismatched kinds to |
| 1921 | // something conservative, however this can pessimize in the case that one of |
| 1922 | // the uses will have all its uses outside the loop, for example. |
| 1923 | if (LU.Kind != Kind) |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1924 | return false; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1925 | // Conservatively assume HasBaseReg is true for now. |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1926 | if (NewOffset < LU.MinOffset) { |
| 1927 | if (!isAlwaysFoldable(LU.MaxOffset - NewOffset, 0, HasBaseReg, |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1928 | Kind, AccessTy, TLI)) |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1929 | return false; |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1930 | NewMinOffset = NewOffset; |
| 1931 | } else if (NewOffset > LU.MaxOffset) { |
| 1932 | if (!isAlwaysFoldable(NewOffset - LU.MinOffset, 0, HasBaseReg, |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1933 | Kind, AccessTy, TLI)) |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1934 | return false; |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1935 | NewMaxOffset = NewOffset; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1936 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1937 | // Check for a mismatched access type, and fall back conservatively as needed. |
Dan Gohman | 74e5ef0 | 2010-06-19 21:30:18 +0000 | [diff] [blame] | 1938 | // TODO: Be less conservative when the type is similar and can use the same |
| 1939 | // addressing modes. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1940 | if (Kind == LSRUse::Address && AccessTy != LU.AccessTy) |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1941 | NewAccessTy = Type::getVoidTy(AccessTy->getContext()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1942 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1943 | // Update the use. |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1944 | LU.MinOffset = NewMinOffset; |
| 1945 | LU.MaxOffset = NewMaxOffset; |
| 1946 | LU.AccessTy = NewAccessTy; |
| 1947 | if (NewOffset != LU.Offsets.back()) |
| 1948 | LU.Offsets.push_back(NewOffset); |
Dan Gohman | 8b0ade3 | 2010-01-21 22:42:49 +0000 | [diff] [blame] | 1949 | return true; |
| 1950 | } |
| 1951 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1952 | /// getUse - Return an LSRUse index and an offset value for a fixup which |
| 1953 | /// needs the given expression, with the given kind and optional access type. |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 1954 | /// Either reuse an existing use or create a new one, as needed. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1955 | std::pair<size_t, int64_t> |
| 1956 | LSRInstance::getUse(const SCEV *&Expr, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1957 | LSRUse::KindType Kind, Type *AccessTy) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1958 | const SCEV *Copy = Expr; |
| 1959 | int64_t Offset = ExtractImmediate(Expr, SE); |
Evan Cheng | 586f69a | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 1960 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1961 | // Basic uses can't accept any offset, for example. |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1962 | if (!isAlwaysFoldable(Offset, 0, /*HasBaseReg=*/true, Kind, AccessTy, TLI)) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1963 | Expr = Copy; |
| 1964 | Offset = 0; |
| 1965 | } |
| 1966 | |
| 1967 | std::pair<UseMapTy::iterator, bool> P = |
Dan Gohman | 1e3121c | 2010-06-19 21:29:59 +0000 | [diff] [blame] | 1968 | UseMap.insert(std::make_pair(std::make_pair(Expr, Kind), 0)); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1969 | if (!P.second) { |
| 1970 | // A use already existed with this base. |
| 1971 | size_t LUIdx = P.first->second; |
| 1972 | LSRUse &LU = Uses[LUIdx]; |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1973 | if (reconcileNewOffset(LU, Offset, /*HasBaseReg=*/true, Kind, AccessTy)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1974 | // Reuse this use. |
| 1975 | return std::make_pair(LUIdx, Offset); |
| 1976 | } |
| 1977 | |
| 1978 | // Create a new use. |
| 1979 | size_t LUIdx = Uses.size(); |
| 1980 | P.first->second = LUIdx; |
| 1981 | Uses.push_back(LSRUse(Kind, AccessTy)); |
| 1982 | LSRUse &LU = Uses[LUIdx]; |
| 1983 | |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1984 | // We don't need to track redundant offsets, but we don't need to go out |
| 1985 | // of our way here to avoid them. |
| 1986 | if (LU.Offsets.empty() || Offset != LU.Offsets.back()) |
| 1987 | LU.Offsets.push_back(Offset); |
| 1988 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1989 | LU.MinOffset = Offset; |
| 1990 | LU.MaxOffset = Offset; |
| 1991 | return std::make_pair(LUIdx, Offset); |
| 1992 | } |
| 1993 | |
Dan Gohman | 5ce6d05 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 1994 | /// DeleteUse - Delete the given use from the Uses list. |
Dan Gohman | c689770 | 2010-10-07 23:33:43 +0000 | [diff] [blame] | 1995 | void LSRInstance::DeleteUse(LSRUse &LU, size_t LUIdx) { |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1996 | if (&LU != &Uses.back()) |
Dan Gohman | 5ce6d05 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 1997 | std::swap(LU, Uses.back()); |
| 1998 | Uses.pop_back(); |
Dan Gohman | c689770 | 2010-10-07 23:33:43 +0000 | [diff] [blame] | 1999 | |
| 2000 | // Update RegUses. |
| 2001 | RegUses.SwapAndDropUse(LUIdx, Uses.size()); |
Dan Gohman | 5ce6d05 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2004 | /// FindUseWithFormula - Look for a use distinct from OrigLU which is has |
| 2005 | /// a formula that has the same registers as the given formula. |
| 2006 | LSRUse * |
| 2007 | LSRInstance::FindUseWithSimilarFormula(const Formula &OrigF, |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 2008 | const LSRUse &OrigLU) { |
| 2009 | // Search all uses for the formula. This could be more clever. |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2010 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 2011 | LSRUse &LU = Uses[LUIdx]; |
Dan Gohman | 6a83271 | 2010-08-29 15:27:08 +0000 | [diff] [blame] | 2012 | // Check whether this use is close enough to OrigLU, to see whether it's |
| 2013 | // worthwhile looking through its formulae. |
| 2014 | // Ignore ICmpZero uses because they may contain formulae generated by |
| 2015 | // GenerateICmpZeroScales, in which case adding fixup offsets may |
| 2016 | // be invalid. |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2017 | if (&LU != &OrigLU && |
| 2018 | LU.Kind != LSRUse::ICmpZero && |
| 2019 | LU.Kind == OrigLU.Kind && OrigLU.AccessTy == LU.AccessTy && |
Dan Gohman | a9db129 | 2010-07-15 20:24:58 +0000 | [diff] [blame] | 2020 | LU.WidestFixupType == OrigLU.WidestFixupType && |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2021 | LU.HasFormulaWithSameRegs(OrigF)) { |
Dan Gohman | 6a83271 | 2010-08-29 15:27:08 +0000 | [diff] [blame] | 2022 | // Scan through this use's formulae. |
Dan Gohman | 402d435 | 2010-05-20 20:33:18 +0000 | [diff] [blame] | 2023 | for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(), |
| 2024 | E = LU.Formulae.end(); I != E; ++I) { |
| 2025 | const Formula &F = *I; |
Dan Gohman | 6a83271 | 2010-08-29 15:27:08 +0000 | [diff] [blame] | 2026 | // Check to see if this formula has the same registers and symbols |
| 2027 | // as OrigF. |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2028 | if (F.BaseRegs == OrigF.BaseRegs && |
| 2029 | F.ScaledReg == OrigF.ScaledReg && |
| 2030 | F.AM.BaseGV == OrigF.AM.BaseGV && |
Dan Gohman | cca8214 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 2031 | F.AM.Scale == OrigF.AM.Scale && |
| 2032 | F.UnfoldedOffset == OrigF.UnfoldedOffset) { |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 2033 | if (F.AM.BaseOffs == 0) |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2034 | return &LU; |
Dan Gohman | 6a83271 | 2010-08-29 15:27:08 +0000 | [diff] [blame] | 2035 | // This is the formula where all the registers and symbols matched; |
| 2036 | // there aren't going to be any others. Since we declined it, we |
| 2037 | // can skip the rest of the formulae and procede to the next LSRUse. |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2038 | break; |
| 2039 | } |
| 2040 | } |
| 2041 | } |
| 2042 | } |
| 2043 | |
Dan Gohman | 6a83271 | 2010-08-29 15:27:08 +0000 | [diff] [blame] | 2044 | // Nothing looked good. |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2045 | return 0; |
| 2046 | } |
| 2047 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2048 | void LSRInstance::CollectInterestingTypesAndFactors() { |
| 2049 | SmallSetVector<const SCEV *, 4> Strides; |
| 2050 | |
Dan Gohman | 1b7bf18 | 2010-02-19 00:05:23 +0000 | [diff] [blame] | 2051 | // Collect interesting types and strides. |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2052 | SmallVector<const SCEV *, 4> Worklist; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2053 | for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) { |
Dan Gohman | c056454 | 2010-04-19 21:48:58 +0000 | [diff] [blame] | 2054 | const SCEV *Expr = IU.getExpr(*UI); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2055 | |
| 2056 | // Collect interesting types. |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2057 | Types.insert(SE.getEffectiveSCEVType(Expr->getType())); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2058 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2059 | // Add strides for mentioned loops. |
| 2060 | Worklist.push_back(Expr); |
| 2061 | do { |
| 2062 | const SCEV *S = Worklist.pop_back_val(); |
| 2063 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
Andrew Trick | fa1948a | 2011-12-10 00:25:00 +0000 | [diff] [blame] | 2064 | if (EnableNested || AR->getLoop() == L) |
| 2065 | Strides.insert(AR->getStepRecurrence(SE)); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2066 | Worklist.push_back(AR->getStart()); |
| 2067 | } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 2068 | Worklist.append(Add->op_begin(), Add->op_end()); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2069 | } |
| 2070 | } while (!Worklist.empty()); |
Dan Gohman | 1b7bf18 | 2010-02-19 00:05:23 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | // Compute interesting factors from the set of interesting strides. |
| 2074 | for (SmallSetVector<const SCEV *, 4>::const_iterator |
| 2075 | I = Strides.begin(), E = Strides.end(); I != E; ++I) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2076 | for (SmallSetVector<const SCEV *, 4>::const_iterator NewStrideIter = |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 2077 | llvm::next(I); NewStrideIter != E; ++NewStrideIter) { |
Dan Gohman | 1b7bf18 | 2010-02-19 00:05:23 +0000 | [diff] [blame] | 2078 | const SCEV *OldStride = *I; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2079 | const SCEV *NewStride = *NewStrideIter; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2080 | |
| 2081 | if (SE.getTypeSizeInBits(OldStride->getType()) != |
| 2082 | SE.getTypeSizeInBits(NewStride->getType())) { |
| 2083 | if (SE.getTypeSizeInBits(OldStride->getType()) > |
| 2084 | SE.getTypeSizeInBits(NewStride->getType())) |
| 2085 | NewStride = SE.getSignExtendExpr(NewStride, OldStride->getType()); |
| 2086 | else |
| 2087 | OldStride = SE.getSignExtendExpr(OldStride, NewStride->getType()); |
| 2088 | } |
| 2089 | if (const SCEVConstant *Factor = |
Dan Gohman | f09b712 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 2090 | dyn_cast_or_null<SCEVConstant>(getExactSDiv(NewStride, OldStride, |
| 2091 | SE, true))) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2092 | if (Factor->getValue()->getValue().getMinSignedBits() <= 64) |
| 2093 | Factors.insert(Factor->getValue()->getValue().getSExtValue()); |
| 2094 | } else if (const SCEVConstant *Factor = |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 2095 | dyn_cast_or_null<SCEVConstant>(getExactSDiv(OldStride, |
| 2096 | NewStride, |
Dan Gohman | f09b712 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 2097 | SE, true))) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2098 | if (Factor->getValue()->getValue().getMinSignedBits() <= 64) |
| 2099 | Factors.insert(Factor->getValue()->getValue().getSExtValue()); |
| 2100 | } |
| 2101 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2102 | |
| 2103 | // If all uses use the same type, don't bother looking for truncation-based |
| 2104 | // reuse. |
| 2105 | if (Types.size() == 1) |
| 2106 | Types.clear(); |
| 2107 | |
| 2108 | DEBUG(print_factors_and_types(dbgs())); |
| 2109 | } |
| 2110 | |
| 2111 | void LSRInstance::CollectFixupsAndInitialFormulae() { |
| 2112 | for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) { |
| 2113 | // Record the uses. |
| 2114 | LSRFixup &LF = getNewFixup(); |
| 2115 | LF.UserInst = UI->getUser(); |
| 2116 | LF.OperandValToReplace = UI->getOperandValToReplace(); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2117 | LF.PostIncLoops = UI->getPostIncLoops(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2118 | |
| 2119 | LSRUse::KindType Kind = LSRUse::Basic; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2120 | Type *AccessTy = 0; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2121 | if (isAddressUse(LF.UserInst, LF.OperandValToReplace)) { |
| 2122 | Kind = LSRUse::Address; |
| 2123 | AccessTy = getAccessType(LF.UserInst); |
| 2124 | } |
| 2125 | |
Dan Gohman | c056454 | 2010-04-19 21:48:58 +0000 | [diff] [blame] | 2126 | const SCEV *S = IU.getExpr(*UI); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2127 | |
| 2128 | // Equality (== and !=) ICmps are special. We can rewrite (i == N) as |
| 2129 | // (N - i == 0), and this allows (N - i) to be the expression that we work |
| 2130 | // with rather than just N or i, so we can consider the register |
| 2131 | // requirements for both N and i at the same time. Limiting this code to |
| 2132 | // equality icmps is not a problem because all interesting loops use |
| 2133 | // equality icmps, thanks to IndVarSimplify. |
| 2134 | if (ICmpInst *CI = dyn_cast<ICmpInst>(LF.UserInst)) |
| 2135 | if (CI->isEquality()) { |
| 2136 | // Swap the operands if needed to put the OperandValToReplace on the |
| 2137 | // left, for consistency. |
| 2138 | Value *NV = CI->getOperand(1); |
| 2139 | if (NV == LF.OperandValToReplace) { |
| 2140 | CI->setOperand(1, CI->getOperand(0)); |
| 2141 | CI->setOperand(0, NV); |
Dan Gohman | f182b23 | 2010-05-20 19:26:52 +0000 | [diff] [blame] | 2142 | NV = CI->getOperand(1); |
Dan Gohman | 9da1bf4 | 2010-05-20 19:16:03 +0000 | [diff] [blame] | 2143 | Changed = true; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | // x == y --> x - y == 0 |
| 2147 | const SCEV *N = SE.getSCEV(NV); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2148 | if (SE.isLoopInvariant(N, L)) { |
Dan Gohman | 673968a | 2011-05-18 21:02:18 +0000 | [diff] [blame] | 2149 | // S is normalized, so normalize N before folding it into S |
| 2150 | // to keep the result normalized. |
| 2151 | N = TransformForPostIncUse(Normalize, N, CI, 0, |
| 2152 | LF.PostIncLoops, SE, DT); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2153 | Kind = LSRUse::ICmpZero; |
| 2154 | S = SE.getMinusSCEV(N, S); |
| 2155 | } |
| 2156 | |
| 2157 | // -1 and the negations of all interesting strides (except the negation |
| 2158 | // of -1) are now also interesting. |
| 2159 | for (size_t i = 0, e = Factors.size(); i != e; ++i) |
| 2160 | if (Factors[i] != -1) |
| 2161 | Factors.insert(-(uint64_t)Factors[i]); |
| 2162 | Factors.insert(-1); |
| 2163 | } |
| 2164 | |
| 2165 | // Set up the initial formula for this use. |
| 2166 | std::pair<size_t, int64_t> P = getUse(S, Kind, AccessTy); |
| 2167 | LF.LUIdx = P.first; |
| 2168 | LF.Offset = P.second; |
| 2169 | LSRUse &LU = Uses[LF.LUIdx]; |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2170 | LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L); |
Dan Gohman | a9db129 | 2010-07-15 20:24:58 +0000 | [diff] [blame] | 2171 | if (!LU.WidestFixupType || |
| 2172 | SE.getTypeSizeInBits(LU.WidestFixupType) < |
| 2173 | SE.getTypeSizeInBits(LF.OperandValToReplace->getType())) |
| 2174 | LU.WidestFixupType = LF.OperandValToReplace->getType(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2175 | |
| 2176 | // If this is the first use of this LSRUse, give it a formula. |
| 2177 | if (LU.Formulae.empty()) { |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 2178 | InsertInitialFormula(S, LU, LF.LUIdx); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2179 | CountRegisters(LU.Formulae.back(), LF.LUIdx); |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | DEBUG(print_fixups(dbgs())); |
| 2184 | } |
| 2185 | |
Dan Gohman | 76c315a | 2010-05-20 20:52:00 +0000 | [diff] [blame] | 2186 | /// InsertInitialFormula - Insert a formula for the given expression into |
| 2187 | /// the given use, separating out loop-variant portions from loop-invariant |
| 2188 | /// and loop-computable portions. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2189 | void |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 2190 | LSRInstance::InsertInitialFormula(const SCEV *S, LSRUse &LU, size_t LUIdx) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2191 | Formula F; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 2192 | F.InitialMatch(S, L, SE); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2193 | bool Inserted = InsertFormula(LU, LUIdx, F); |
| 2194 | assert(Inserted && "Initial formula already exists!"); (void)Inserted; |
| 2195 | } |
| 2196 | |
Dan Gohman | 76c315a | 2010-05-20 20:52:00 +0000 | [diff] [blame] | 2197 | /// InsertSupplementalFormula - Insert a simple single-register formula for |
| 2198 | /// the given expression into the given use. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2199 | void |
| 2200 | LSRInstance::InsertSupplementalFormula(const SCEV *S, |
| 2201 | LSRUse &LU, size_t LUIdx) { |
| 2202 | Formula F; |
| 2203 | F.BaseRegs.push_back(S); |
| 2204 | F.AM.HasBaseReg = true; |
| 2205 | bool Inserted = InsertFormula(LU, LUIdx, F); |
| 2206 | assert(Inserted && "Supplemental formula already exists!"); (void)Inserted; |
| 2207 | } |
| 2208 | |
| 2209 | /// CountRegisters - Note which registers are used by the given formula, |
| 2210 | /// updating RegUses. |
| 2211 | void LSRInstance::CountRegisters(const Formula &F, size_t LUIdx) { |
| 2212 | if (F.ScaledReg) |
| 2213 | RegUses.CountRegister(F.ScaledReg, LUIdx); |
| 2214 | for (SmallVectorImpl<const SCEV *>::const_iterator I = F.BaseRegs.begin(), |
| 2215 | E = F.BaseRegs.end(); I != E; ++I) |
| 2216 | RegUses.CountRegister(*I, LUIdx); |
| 2217 | } |
| 2218 | |
| 2219 | /// InsertFormula - If the given formula has not yet been inserted, add it to |
| 2220 | /// the list, and return true. Return false otherwise. |
| 2221 | bool LSRInstance::InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F) { |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 2222 | if (!LU.InsertFormula(F)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2223 | return false; |
| 2224 | |
| 2225 | CountRegisters(F, LUIdx); |
| 2226 | return true; |
| 2227 | } |
| 2228 | |
| 2229 | /// CollectLoopInvariantFixupsAndFormulae - Check for other uses of |
| 2230 | /// loop-invariant values which we're tracking. These other uses will pin these |
| 2231 | /// values in registers, making them less profitable for elimination. |
| 2232 | /// TODO: This currently misses non-constant addrec step registers. |
| 2233 | /// TODO: Should this give more weight to users inside the loop? |
| 2234 | void |
| 2235 | LSRInstance::CollectLoopInvariantFixupsAndFormulae() { |
| 2236 | SmallVector<const SCEV *, 8> Worklist(RegUses.begin(), RegUses.end()); |
| 2237 | SmallPtrSet<const SCEV *, 8> Inserted; |
| 2238 | |
| 2239 | while (!Worklist.empty()) { |
| 2240 | const SCEV *S = Worklist.pop_back_val(); |
| 2241 | |
| 2242 | if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S)) |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 2243 | Worklist.append(N->op_begin(), N->op_end()); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2244 | else if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S)) |
| 2245 | Worklist.push_back(C->getOperand()); |
| 2246 | else if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) { |
| 2247 | Worklist.push_back(D->getLHS()); |
| 2248 | Worklist.push_back(D->getRHS()); |
| 2249 | } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2250 | if (!Inserted.insert(U)) continue; |
| 2251 | const Value *V = U->getValue(); |
Dan Gohman | a15ec5d | 2010-06-04 23:16:05 +0000 | [diff] [blame] | 2252 | if (const Instruction *Inst = dyn_cast<Instruction>(V)) { |
| 2253 | // Look for instructions defined outside the loop. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2254 | if (L->contains(Inst)) continue; |
Dan Gohman | a15ec5d | 2010-06-04 23:16:05 +0000 | [diff] [blame] | 2255 | } else if (isa<UndefValue>(V)) |
| 2256 | // Undef doesn't have a live range, so it doesn't matter. |
| 2257 | continue; |
Gabor Greif | 60ad781 | 2010-03-25 23:06:16 +0000 | [diff] [blame] | 2258 | for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2259 | UI != UE; ++UI) { |
| 2260 | const Instruction *UserInst = dyn_cast<Instruction>(*UI); |
| 2261 | // Ignore non-instructions. |
| 2262 | if (!UserInst) |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2263 | continue; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2264 | // Ignore instructions in other functions (as can happen with |
| 2265 | // Constants). |
| 2266 | if (UserInst->getParent()->getParent() != L->getHeader()->getParent()) |
Dan Gohman | 7979b72 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2267 | continue; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2268 | // Ignore instructions not dominated by the loop. |
| 2269 | const BasicBlock *UseBB = !isa<PHINode>(UserInst) ? |
| 2270 | UserInst->getParent() : |
| 2271 | cast<PHINode>(UserInst)->getIncomingBlock( |
| 2272 | PHINode::getIncomingValueNumForOperand(UI.getOperandNo())); |
| 2273 | if (!DT.dominates(L->getHeader(), UseBB)) |
| 2274 | continue; |
| 2275 | // Ignore uses which are part of other SCEV expressions, to avoid |
| 2276 | // analyzing them multiple times. |
Dan Gohman | 4a2a683 | 2010-04-09 19:12:34 +0000 | [diff] [blame] | 2277 | if (SE.isSCEVable(UserInst->getType())) { |
| 2278 | const SCEV *UserS = SE.getSCEV(const_cast<Instruction *>(UserInst)); |
| 2279 | // If the user is a no-op, look through to its uses. |
| 2280 | if (!isa<SCEVUnknown>(UserS)) |
| 2281 | continue; |
| 2282 | if (UserS == U) { |
| 2283 | Worklist.push_back( |
| 2284 | SE.getUnknown(const_cast<Instruction *>(UserInst))); |
| 2285 | continue; |
| 2286 | } |
| 2287 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2288 | // Ignore icmp instructions which are already being analyzed. |
| 2289 | if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UserInst)) { |
| 2290 | unsigned OtherIdx = !UI.getOperandNo(); |
| 2291 | Value *OtherOp = const_cast<Value *>(ICI->getOperand(OtherIdx)); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2292 | if (SE.hasComputableLoopEvolution(SE.getSCEV(OtherOp), L)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2293 | continue; |
| 2294 | } |
| 2295 | |
| 2296 | LSRFixup &LF = getNewFixup(); |
| 2297 | LF.UserInst = const_cast<Instruction *>(UserInst); |
| 2298 | LF.OperandValToReplace = UI.getUse(); |
| 2299 | std::pair<size_t, int64_t> P = getUse(S, LSRUse::Basic, 0); |
| 2300 | LF.LUIdx = P.first; |
| 2301 | LF.Offset = P.second; |
| 2302 | LSRUse &LU = Uses[LF.LUIdx]; |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2303 | LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L); |
Dan Gohman | a9db129 | 2010-07-15 20:24:58 +0000 | [diff] [blame] | 2304 | if (!LU.WidestFixupType || |
| 2305 | SE.getTypeSizeInBits(LU.WidestFixupType) < |
| 2306 | SE.getTypeSizeInBits(LF.OperandValToReplace->getType())) |
| 2307 | LU.WidestFixupType = LF.OperandValToReplace->getType(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2308 | InsertSupplementalFormula(U, LU, LF.LUIdx); |
| 2309 | CountRegisters(LU.Formulae.back(), Uses.size() - 1); |
| 2310 | break; |
| 2311 | } |
| 2312 | } |
| 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | /// CollectSubexprs - Split S into subexpressions which can be pulled out into |
| 2317 | /// separate registers. If C is non-null, multiply each subexpression by C. |
| 2318 | static void CollectSubexprs(const SCEV *S, const SCEVConstant *C, |
| 2319 | SmallVectorImpl<const SCEV *> &Ops, |
Dan Gohman | 3e3f15b | 2010-06-25 22:32:18 +0000 | [diff] [blame] | 2320 | const Loop *L, |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2321 | ScalarEvolution &SE) { |
| 2322 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 2323 | // Break out add operands. |
| 2324 | for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end(); |
| 2325 | I != E; ++I) |
Dan Gohman | 3e22b7c | 2010-08-16 15:50:00 +0000 | [diff] [blame] | 2326 | CollectSubexprs(*I, C, Ops, L, SE); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2327 | return; |
| 2328 | } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 2329 | // Split a non-zero base out of an addrec. |
| 2330 | if (!AR->getStart()->isZero()) { |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 2331 | CollectSubexprs(SE.getAddRecExpr(SE.getConstant(AR->getType(), 0), |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2332 | AR->getStepRecurrence(SE), |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2333 | AR->getLoop(), |
| 2334 | //FIXME: AR->getNoWrapFlags(SCEV::FlagNW) |
| 2335 | SCEV::FlagAnyWrap), |
Dan Gohman | 3e22b7c | 2010-08-16 15:50:00 +0000 | [diff] [blame] | 2336 | C, Ops, L, SE); |
| 2337 | CollectSubexprs(AR->getStart(), C, Ops, L, SE); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2338 | return; |
| 2339 | } |
| 2340 | } else if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 2341 | // Break (C * (a + b + c)) into C*a + C*b + C*c. |
| 2342 | if (Mul->getNumOperands() == 2) |
| 2343 | if (const SCEVConstant *Op0 = |
| 2344 | dyn_cast<SCEVConstant>(Mul->getOperand(0))) { |
| 2345 | CollectSubexprs(Mul->getOperand(1), |
| 2346 | C ? cast<SCEVConstant>(SE.getMulExpr(C, Op0)) : Op0, |
Dan Gohman | 3e22b7c | 2010-08-16 15:50:00 +0000 | [diff] [blame] | 2347 | Ops, L, SE); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2348 | return; |
| 2349 | } |
| 2350 | } |
| 2351 | |
Dan Gohman | 3e22b7c | 2010-08-16 15:50:00 +0000 | [diff] [blame] | 2352 | // Otherwise use the value itself, optionally with a scale applied. |
| 2353 | Ops.push_back(C ? SE.getMulExpr(C, S) : S); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
| 2356 | /// GenerateReassociations - Split out subexpressions from adds and the bases of |
| 2357 | /// addrecs. |
| 2358 | void LSRInstance::GenerateReassociations(LSRUse &LU, unsigned LUIdx, |
| 2359 | Formula Base, |
| 2360 | unsigned Depth) { |
| 2361 | // Arbitrarily cap recursion to protect compile time. |
| 2362 | if (Depth >= 3) return; |
| 2363 | |
| 2364 | for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) { |
| 2365 | const SCEV *BaseReg = Base.BaseRegs[i]; |
| 2366 | |
Dan Gohman | 3e22b7c | 2010-08-16 15:50:00 +0000 | [diff] [blame] | 2367 | SmallVector<const SCEV *, 8> AddOps; |
| 2368 | CollectSubexprs(BaseReg, 0, AddOps, L, SE); |
Dan Gohman | 3e3f15b | 2010-06-25 22:32:18 +0000 | [diff] [blame] | 2369 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2370 | if (AddOps.size() == 1) continue; |
| 2371 | |
| 2372 | for (SmallVectorImpl<const SCEV *>::const_iterator J = AddOps.begin(), |
| 2373 | JE = AddOps.end(); J != JE; ++J) { |
Dan Gohman | 3e22b7c | 2010-08-16 15:50:00 +0000 | [diff] [blame] | 2374 | |
| 2375 | // Loop-variant "unknown" values are uninteresting; we won't be able to |
| 2376 | // do anything meaningful with them. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2377 | if (isa<SCEVUnknown>(*J) && !SE.isLoopInvariant(*J, L)) |
Dan Gohman | 3e22b7c | 2010-08-16 15:50:00 +0000 | [diff] [blame] | 2378 | continue; |
| 2379 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2380 | // Don't pull a constant into a register if the constant could be folded |
| 2381 | // into an immediate field. |
| 2382 | if (isAlwaysFoldable(*J, LU.MinOffset, LU.MaxOffset, |
| 2383 | Base.getNumRegs() > 1, |
| 2384 | LU.Kind, LU.AccessTy, TLI, SE)) |
| 2385 | continue; |
| 2386 | |
| 2387 | // Collect all operands except *J. |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 2388 | SmallVector<const SCEV *, 8> InnerAddOps |
Dan Gohman | 4eaee28 | 2010-08-04 17:43:57 +0000 | [diff] [blame] | 2389 | (((const SmallVector<const SCEV *, 8> &)AddOps).begin(), J); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 2390 | InnerAddOps.append |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 2391 | (llvm::next(J), ((const SmallVector<const SCEV *, 8> &)AddOps).end()); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2392 | |
| 2393 | // Don't leave just a constant behind in a register if the constant could |
| 2394 | // be folded into an immediate field. |
| 2395 | if (InnerAddOps.size() == 1 && |
| 2396 | isAlwaysFoldable(InnerAddOps[0], LU.MinOffset, LU.MaxOffset, |
| 2397 | Base.getNumRegs() > 1, |
| 2398 | LU.Kind, LU.AccessTy, TLI, SE)) |
| 2399 | continue; |
| 2400 | |
Dan Gohman | fafb890 | 2010-04-23 01:55:05 +0000 | [diff] [blame] | 2401 | const SCEV *InnerSum = SE.getAddExpr(InnerAddOps); |
| 2402 | if (InnerSum->isZero()) |
| 2403 | continue; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2404 | Formula F = Base; |
Dan Gohman | cca8214 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 2405 | |
| 2406 | // Add the remaining pieces of the add back into the new formula. |
| 2407 | const SCEVConstant *InnerSumSC = dyn_cast<SCEVConstant>(InnerSum); |
| 2408 | if (TLI && InnerSumSC && |
| 2409 | SE.getTypeSizeInBits(InnerSumSC->getType()) <= 64 && |
| 2410 | TLI->isLegalAddImmediate((uint64_t)F.UnfoldedOffset + |
| 2411 | InnerSumSC->getValue()->getZExtValue())) { |
| 2412 | F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset + |
| 2413 | InnerSumSC->getValue()->getZExtValue(); |
| 2414 | F.BaseRegs.erase(F.BaseRegs.begin() + i); |
| 2415 | } else |
| 2416 | F.BaseRegs[i] = InnerSum; |
| 2417 | |
| 2418 | // Add J as its own register, or an unfolded immediate. |
| 2419 | const SCEVConstant *SC = dyn_cast<SCEVConstant>(*J); |
| 2420 | if (TLI && SC && SE.getTypeSizeInBits(SC->getType()) <= 64 && |
| 2421 | TLI->isLegalAddImmediate((uint64_t)F.UnfoldedOffset + |
| 2422 | SC->getValue()->getZExtValue())) |
| 2423 | F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset + |
| 2424 | SC->getValue()->getZExtValue(); |
| 2425 | else |
| 2426 | F.BaseRegs.push_back(*J); |
| 2427 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2428 | if (InsertFormula(LU, LUIdx, F)) |
| 2429 | // If that formula hadn't been seen before, recurse to find more like |
| 2430 | // it. |
| 2431 | GenerateReassociations(LU, LUIdx, LU.Formulae.back(), Depth+1); |
| 2432 | } |
| 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | /// GenerateCombinations - Generate a formula consisting of all of the |
| 2437 | /// loop-dominating registers added into a single register. |
| 2438 | void LSRInstance::GenerateCombinations(LSRUse &LU, unsigned LUIdx, |
Dan Gohman | 441a389 | 2010-02-14 18:51:39 +0000 | [diff] [blame] | 2439 | Formula Base) { |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 2440 | // This method is only interesting on a plurality of registers. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2441 | if (Base.BaseRegs.size() <= 1) return; |
| 2442 | |
| 2443 | Formula F = Base; |
| 2444 | F.BaseRegs.clear(); |
| 2445 | SmallVector<const SCEV *, 4> Ops; |
| 2446 | for (SmallVectorImpl<const SCEV *>::const_iterator |
| 2447 | I = Base.BaseRegs.begin(), E = Base.BaseRegs.end(); I != E; ++I) { |
| 2448 | const SCEV *BaseReg = *I; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 2449 | if (SE.properlyDominates(BaseReg, L->getHeader()) && |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2450 | !SE.hasComputableLoopEvolution(BaseReg, L)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2451 | Ops.push_back(BaseReg); |
| 2452 | else |
| 2453 | F.BaseRegs.push_back(BaseReg); |
| 2454 | } |
| 2455 | if (Ops.size() > 1) { |
Dan Gohman | ce94736 | 2010-02-14 18:50:49 +0000 | [diff] [blame] | 2456 | const SCEV *Sum = SE.getAddExpr(Ops); |
| 2457 | // TODO: If Sum is zero, it probably means ScalarEvolution missed an |
| 2458 | // opportunity to fold something. For now, just ignore such cases |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 2459 | // rather than proceed with zero in a register. |
Dan Gohman | ce94736 | 2010-02-14 18:50:49 +0000 | [diff] [blame] | 2460 | if (!Sum->isZero()) { |
| 2461 | F.BaseRegs.push_back(Sum); |
| 2462 | (void)InsertFormula(LU, LUIdx, F); |
| 2463 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | /// GenerateSymbolicOffsets - Generate reuse formulae using symbolic offsets. |
| 2468 | void LSRInstance::GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx, |
| 2469 | Formula Base) { |
| 2470 | // We can't add a symbolic offset if the address already contains one. |
| 2471 | if (Base.AM.BaseGV) return; |
| 2472 | |
| 2473 | for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) { |
| 2474 | const SCEV *G = Base.BaseRegs[i]; |
| 2475 | GlobalValue *GV = ExtractSymbol(G, SE); |
| 2476 | if (G->isZero() || !GV) |
| 2477 | continue; |
| 2478 | Formula F = Base; |
| 2479 | F.AM.BaseGV = GV; |
| 2480 | if (!isLegalUse(F.AM, LU.MinOffset, LU.MaxOffset, |
| 2481 | LU.Kind, LU.AccessTy, TLI)) |
| 2482 | continue; |
| 2483 | F.BaseRegs[i] = G; |
| 2484 | (void)InsertFormula(LU, LUIdx, F); |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | /// GenerateConstantOffsets - Generate reuse formulae using symbolic offsets. |
| 2489 | void LSRInstance::GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx, |
| 2490 | Formula Base) { |
| 2491 | // TODO: For now, just add the min and max offset, because it usually isn't |
| 2492 | // worthwhile looking at everything inbetween. |
Dan Gohman | c88c1a4 | 2010-07-15 15:14:45 +0000 | [diff] [blame] | 2493 | SmallVector<int64_t, 2> Worklist; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2494 | Worklist.push_back(LU.MinOffset); |
| 2495 | if (LU.MaxOffset != LU.MinOffset) |
| 2496 | Worklist.push_back(LU.MaxOffset); |
| 2497 | |
| 2498 | for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) { |
| 2499 | const SCEV *G = Base.BaseRegs[i]; |
| 2500 | |
| 2501 | for (SmallVectorImpl<int64_t>::const_iterator I = Worklist.begin(), |
| 2502 | E = Worklist.end(); I != E; ++I) { |
| 2503 | Formula F = Base; |
| 2504 | F.AM.BaseOffs = (uint64_t)Base.AM.BaseOffs - *I; |
| 2505 | if (isLegalUse(F.AM, LU.MinOffset - *I, LU.MaxOffset - *I, |
| 2506 | LU.Kind, LU.AccessTy, TLI)) { |
Dan Gohman | c88c1a4 | 2010-07-15 15:14:45 +0000 | [diff] [blame] | 2507 | // Add the offset to the base register. |
Dan Gohman | 4065f60 | 2010-08-16 15:39:27 +0000 | [diff] [blame] | 2508 | const SCEV *NewG = SE.getAddExpr(SE.getConstant(G->getType(), *I), G); |
Dan Gohman | c88c1a4 | 2010-07-15 15:14:45 +0000 | [diff] [blame] | 2509 | // If it cancelled out, drop the base register, otherwise update it. |
| 2510 | if (NewG->isZero()) { |
| 2511 | std::swap(F.BaseRegs[i], F.BaseRegs.back()); |
| 2512 | F.BaseRegs.pop_back(); |
| 2513 | } else |
| 2514 | F.BaseRegs[i] = NewG; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2515 | |
| 2516 | (void)InsertFormula(LU, LUIdx, F); |
| 2517 | } |
| 2518 | } |
| 2519 | |
| 2520 | int64_t Imm = ExtractImmediate(G, SE); |
| 2521 | if (G->isZero() || Imm == 0) |
| 2522 | continue; |
| 2523 | Formula F = Base; |
| 2524 | F.AM.BaseOffs = (uint64_t)F.AM.BaseOffs + Imm; |
| 2525 | if (!isLegalUse(F.AM, LU.MinOffset, LU.MaxOffset, |
| 2526 | LU.Kind, LU.AccessTy, TLI)) |
| 2527 | continue; |
| 2528 | F.BaseRegs[i] = G; |
| 2529 | (void)InsertFormula(LU, LUIdx, F); |
| 2530 | } |
| 2531 | } |
| 2532 | |
| 2533 | /// GenerateICmpZeroScales - For ICmpZero, check to see if we can scale up |
| 2534 | /// the comparison. For example, x == y -> x*c == y*c. |
| 2535 | void LSRInstance::GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx, |
| 2536 | Formula Base) { |
| 2537 | if (LU.Kind != LSRUse::ICmpZero) return; |
| 2538 | |
| 2539 | // Determine the integer type for the base formula. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2540 | Type *IntTy = Base.getType(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2541 | if (!IntTy) return; |
| 2542 | if (SE.getTypeSizeInBits(IntTy) > 64) return; |
| 2543 | |
| 2544 | // Don't do this if there is more than one offset. |
| 2545 | if (LU.MinOffset != LU.MaxOffset) return; |
| 2546 | |
| 2547 | assert(!Base.AM.BaseGV && "ICmpZero use is not legal!"); |
| 2548 | |
| 2549 | // Check each interesting stride. |
| 2550 | for (SmallSetVector<int64_t, 8>::const_iterator |
| 2551 | I = Factors.begin(), E = Factors.end(); I != E; ++I) { |
| 2552 | int64_t Factor = *I; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2553 | |
| 2554 | // Check that the multiplication doesn't overflow. |
Dan Gohman | 2ea09e0 | 2010-06-24 16:57:52 +0000 | [diff] [blame] | 2555 | if (Base.AM.BaseOffs == INT64_MIN && Factor == -1) |
Dan Gohman | 968cb93 | 2010-02-17 00:41:53 +0000 | [diff] [blame] | 2556 | continue; |
Dan Gohman | 2ea09e0 | 2010-06-24 16:57:52 +0000 | [diff] [blame] | 2557 | int64_t NewBaseOffs = (uint64_t)Base.AM.BaseOffs * Factor; |
| 2558 | if (NewBaseOffs / Factor != Base.AM.BaseOffs) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2559 | continue; |
| 2560 | |
| 2561 | // Check that multiplying with the use offset doesn't overflow. |
| 2562 | int64_t Offset = LU.MinOffset; |
Dan Gohman | 968cb93 | 2010-02-17 00:41:53 +0000 | [diff] [blame] | 2563 | if (Offset == INT64_MIN && Factor == -1) |
| 2564 | continue; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2565 | Offset = (uint64_t)Offset * Factor; |
Dan Gohman | 378c0b3 | 2010-02-17 00:42:19 +0000 | [diff] [blame] | 2566 | if (Offset / Factor != LU.MinOffset) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2567 | continue; |
| 2568 | |
Dan Gohman | 2ea09e0 | 2010-06-24 16:57:52 +0000 | [diff] [blame] | 2569 | Formula F = Base; |
| 2570 | F.AM.BaseOffs = NewBaseOffs; |
| 2571 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2572 | // Check that this scale is legal. |
| 2573 | if (!isLegalUse(F.AM, Offset, Offset, LU.Kind, LU.AccessTy, TLI)) |
| 2574 | continue; |
| 2575 | |
| 2576 | // Compensate for the use having MinOffset built into it. |
| 2577 | F.AM.BaseOffs = (uint64_t)F.AM.BaseOffs + Offset - LU.MinOffset; |
| 2578 | |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 2579 | const SCEV *FactorS = SE.getConstant(IntTy, Factor); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2580 | |
| 2581 | // Check that multiplying with each base register doesn't overflow. |
| 2582 | for (size_t i = 0, e = F.BaseRegs.size(); i != e; ++i) { |
| 2583 | F.BaseRegs[i] = SE.getMulExpr(F.BaseRegs[i], FactorS); |
Dan Gohman | f09b712 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 2584 | if (getExactSDiv(F.BaseRegs[i], FactorS, SE) != Base.BaseRegs[i]) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2585 | goto next; |
| 2586 | } |
| 2587 | |
| 2588 | // Check that multiplying with the scaled register doesn't overflow. |
| 2589 | if (F.ScaledReg) { |
| 2590 | F.ScaledReg = SE.getMulExpr(F.ScaledReg, FactorS); |
Dan Gohman | f09b712 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 2591 | if (getExactSDiv(F.ScaledReg, FactorS, SE) != Base.ScaledReg) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2592 | continue; |
| 2593 | } |
| 2594 | |
Dan Gohman | cca8214 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 2595 | // Check that multiplying with the unfolded offset doesn't overflow. |
| 2596 | if (F.UnfoldedOffset != 0) { |
Dan Gohman | 1b58d45 | 2011-05-23 21:07:39 +0000 | [diff] [blame] | 2597 | if (F.UnfoldedOffset == INT64_MIN && Factor == -1) |
| 2598 | continue; |
Dan Gohman | cca8214 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 2599 | F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset * Factor; |
| 2600 | if (F.UnfoldedOffset / Factor != Base.UnfoldedOffset) |
| 2601 | continue; |
| 2602 | } |
| 2603 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2604 | // If we make it here and it's legal, add it. |
| 2605 | (void)InsertFormula(LU, LUIdx, F); |
| 2606 | next:; |
| 2607 | } |
| 2608 | } |
| 2609 | |
| 2610 | /// GenerateScales - Generate stride factor reuse formulae by making use of |
| 2611 | /// scaled-offset address modes, for example. |
Dan Gohman | ea507f5 | 2010-05-20 19:44:23 +0000 | [diff] [blame] | 2612 | void LSRInstance::GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2613 | // Determine the integer type for the base formula. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2614 | Type *IntTy = Base.getType(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2615 | if (!IntTy) return; |
| 2616 | |
| 2617 | // If this Formula already has a scaled register, we can't add another one. |
| 2618 | if (Base.AM.Scale != 0) return; |
| 2619 | |
| 2620 | // Check each interesting stride. |
| 2621 | for (SmallSetVector<int64_t, 8>::const_iterator |
| 2622 | I = Factors.begin(), E = Factors.end(); I != E; ++I) { |
| 2623 | int64_t Factor = *I; |
| 2624 | |
| 2625 | Base.AM.Scale = Factor; |
| 2626 | Base.AM.HasBaseReg = Base.BaseRegs.size() > 1; |
| 2627 | // Check whether this scale is going to be legal. |
| 2628 | if (!isLegalUse(Base.AM, LU.MinOffset, LU.MaxOffset, |
| 2629 | LU.Kind, LU.AccessTy, TLI)) { |
| 2630 | // As a special-case, handle special out-of-loop Basic users specially. |
| 2631 | // TODO: Reconsider this special case. |
| 2632 | if (LU.Kind == LSRUse::Basic && |
| 2633 | isLegalUse(Base.AM, LU.MinOffset, LU.MaxOffset, |
| 2634 | LSRUse::Special, LU.AccessTy, TLI) && |
| 2635 | LU.AllFixupsOutsideLoop) |
| 2636 | LU.Kind = LSRUse::Special; |
| 2637 | else |
| 2638 | continue; |
| 2639 | } |
| 2640 | // For an ICmpZero, negating a solitary base register won't lead to |
| 2641 | // new solutions. |
| 2642 | if (LU.Kind == LSRUse::ICmpZero && |
| 2643 | !Base.AM.HasBaseReg && Base.AM.BaseOffs == 0 && !Base.AM.BaseGV) |
| 2644 | continue; |
| 2645 | // For each addrec base reg, apply the scale, if possible. |
| 2646 | for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) |
| 2647 | if (const SCEVAddRecExpr *AR = |
| 2648 | dyn_cast<SCEVAddRecExpr>(Base.BaseRegs[i])) { |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 2649 | const SCEV *FactorS = SE.getConstant(IntTy, Factor); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2650 | if (FactorS->isZero()) |
| 2651 | continue; |
| 2652 | // Divide out the factor, ignoring high bits, since we'll be |
| 2653 | // scaling the value back up in the end. |
Dan Gohman | f09b712 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 2654 | if (const SCEV *Quotient = getExactSDiv(AR, FactorS, SE, true)) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2655 | // TODO: This could be optimized to avoid all the copying. |
| 2656 | Formula F = Base; |
| 2657 | F.ScaledReg = Quotient; |
Dan Gohman | 5ce6d05 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 2658 | F.DeleteBaseReg(F.BaseRegs[i]); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2659 | (void)InsertFormula(LU, LUIdx, F); |
| 2660 | } |
| 2661 | } |
| 2662 | } |
| 2663 | } |
| 2664 | |
| 2665 | /// GenerateTruncates - Generate reuse formulae from different IV types. |
Dan Gohman | ea507f5 | 2010-05-20 19:44:23 +0000 | [diff] [blame] | 2666 | void LSRInstance::GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2667 | // This requires TargetLowering to tell us which truncates are free. |
| 2668 | if (!TLI) return; |
| 2669 | |
| 2670 | // Don't bother truncating symbolic values. |
| 2671 | if (Base.AM.BaseGV) return; |
| 2672 | |
| 2673 | // Determine the integer type for the base formula. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2674 | Type *DstTy = Base.getType(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2675 | if (!DstTy) return; |
| 2676 | DstTy = SE.getEffectiveSCEVType(DstTy); |
| 2677 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2678 | for (SmallSetVector<Type *, 4>::const_iterator |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2679 | I = Types.begin(), E = Types.end(); I != E; ++I) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2680 | Type *SrcTy = *I; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2681 | if (SrcTy != DstTy && TLI->isTruncateFree(SrcTy, DstTy)) { |
| 2682 | Formula F = Base; |
| 2683 | |
| 2684 | if (F.ScaledReg) F.ScaledReg = SE.getAnyExtendExpr(F.ScaledReg, *I); |
| 2685 | for (SmallVectorImpl<const SCEV *>::iterator J = F.BaseRegs.begin(), |
| 2686 | JE = F.BaseRegs.end(); J != JE; ++J) |
| 2687 | *J = SE.getAnyExtendExpr(*J, SrcTy); |
| 2688 | |
| 2689 | // TODO: This assumes we've done basic processing on all uses and |
| 2690 | // have an idea what the register usage is. |
| 2691 | if (!F.hasRegsUsedByUsesOtherThan(LUIdx, RegUses)) |
| 2692 | continue; |
| 2693 | |
| 2694 | (void)InsertFormula(LU, LUIdx, F); |
| 2695 | } |
| 2696 | } |
| 2697 | } |
| 2698 | |
| 2699 | namespace { |
| 2700 | |
Dan Gohman | 6020d85 | 2010-02-14 18:51:20 +0000 | [diff] [blame] | 2701 | /// WorkItem - Helper class for GenerateCrossUseConstantOffsets. It's used to |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2702 | /// defer modifications so that the search phase doesn't have to worry about |
| 2703 | /// the data structures moving underneath it. |
| 2704 | struct WorkItem { |
| 2705 | size_t LUIdx; |
| 2706 | int64_t Imm; |
| 2707 | const SCEV *OrigReg; |
| 2708 | |
| 2709 | WorkItem(size_t LI, int64_t I, const SCEV *R) |
| 2710 | : LUIdx(LI), Imm(I), OrigReg(R) {} |
| 2711 | |
| 2712 | void print(raw_ostream &OS) const; |
| 2713 | void dump() const; |
| 2714 | }; |
| 2715 | |
| 2716 | } |
| 2717 | |
| 2718 | void WorkItem::print(raw_ostream &OS) const { |
| 2719 | OS << "in formulae referencing " << *OrigReg << " in use " << LUIdx |
| 2720 | << " , add offset " << Imm; |
| 2721 | } |
| 2722 | |
| 2723 | void WorkItem::dump() const { |
| 2724 | print(errs()); errs() << '\n'; |
| 2725 | } |
| 2726 | |
| 2727 | /// GenerateCrossUseConstantOffsets - Look for registers which are a constant |
| 2728 | /// distance apart and try to form reuse opportunities between them. |
| 2729 | void LSRInstance::GenerateCrossUseConstantOffsets() { |
| 2730 | // Group the registers by their value without any added constant offset. |
| 2731 | typedef std::map<int64_t, const SCEV *> ImmMapTy; |
| 2732 | typedef DenseMap<const SCEV *, ImmMapTy> RegMapTy; |
| 2733 | RegMapTy Map; |
| 2734 | DenseMap<const SCEV *, SmallBitVector> UsedByIndicesMap; |
| 2735 | SmallVector<const SCEV *, 8> Sequence; |
| 2736 | for (RegUseTracker::const_iterator I = RegUses.begin(), E = RegUses.end(); |
| 2737 | I != E; ++I) { |
| 2738 | const SCEV *Reg = *I; |
| 2739 | int64_t Imm = ExtractImmediate(Reg, SE); |
| 2740 | std::pair<RegMapTy::iterator, bool> Pair = |
| 2741 | Map.insert(std::make_pair(Reg, ImmMapTy())); |
| 2742 | if (Pair.second) |
| 2743 | Sequence.push_back(Reg); |
| 2744 | Pair.first->second.insert(std::make_pair(Imm, *I)); |
| 2745 | UsedByIndicesMap[Reg] |= RegUses.getUsedByIndices(*I); |
| 2746 | } |
| 2747 | |
| 2748 | // Now examine each set of registers with the same base value. Build up |
| 2749 | // a list of work to do and do the work in a separate step so that we're |
| 2750 | // not adding formulae and register counts while we're searching. |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 2751 | SmallVector<WorkItem, 32> WorkItems; |
| 2752 | SmallSet<std::pair<size_t, int64_t>, 32> UniqueItems; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2753 | for (SmallVectorImpl<const SCEV *>::const_iterator I = Sequence.begin(), |
| 2754 | E = Sequence.end(); I != E; ++I) { |
| 2755 | const SCEV *Reg = *I; |
| 2756 | const ImmMapTy &Imms = Map.find(Reg)->second; |
| 2757 | |
Dan Gohman | cd045c0 | 2010-02-12 19:20:37 +0000 | [diff] [blame] | 2758 | // It's not worthwhile looking for reuse if there's only one offset. |
| 2759 | if (Imms.size() == 1) |
| 2760 | continue; |
| 2761 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2762 | DEBUG(dbgs() << "Generating cross-use offsets for " << *Reg << ':'; |
| 2763 | for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end(); |
| 2764 | J != JE; ++J) |
| 2765 | dbgs() << ' ' << J->first; |
| 2766 | dbgs() << '\n'); |
| 2767 | |
| 2768 | // Examine each offset. |
| 2769 | for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end(); |
| 2770 | J != JE; ++J) { |
| 2771 | const SCEV *OrigReg = J->second; |
| 2772 | |
| 2773 | int64_t JImm = J->first; |
| 2774 | const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(OrigReg); |
| 2775 | |
| 2776 | if (!isa<SCEVConstant>(OrigReg) && |
| 2777 | UsedByIndicesMap[Reg].count() == 1) { |
| 2778 | DEBUG(dbgs() << "Skipping cross-use reuse for " << *OrigReg << '\n'); |
| 2779 | continue; |
| 2780 | } |
| 2781 | |
| 2782 | // Conservatively examine offsets between this orig reg a few selected |
| 2783 | // other orig regs. |
| 2784 | ImmMapTy::const_iterator OtherImms[] = { |
| 2785 | Imms.begin(), prior(Imms.end()), |
Dan Gohman | cca8214 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 2786 | Imms.lower_bound((Imms.begin()->first + prior(Imms.end())->first) / 2) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2787 | }; |
| 2788 | for (size_t i = 0, e = array_lengthof(OtherImms); i != e; ++i) { |
| 2789 | ImmMapTy::const_iterator M = OtherImms[i]; |
Dan Gohman | cd045c0 | 2010-02-12 19:20:37 +0000 | [diff] [blame] | 2790 | if (M == J || M == JE) continue; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2791 | |
| 2792 | // Compute the difference between the two. |
| 2793 | int64_t Imm = (uint64_t)JImm - M->first; |
| 2794 | for (int LUIdx = UsedByIndices.find_first(); LUIdx != -1; |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 2795 | LUIdx = UsedByIndices.find_next(LUIdx)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2796 | // Make a memo of this use, offset, and register tuple. |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 2797 | if (UniqueItems.insert(std::make_pair(LUIdx, Imm))) |
| 2798 | WorkItems.push_back(WorkItem(LUIdx, Imm, OrigReg)); |
Evan Cheng | 586f69a | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 2799 | } |
| 2800 | } |
| 2801 | } |
| 2802 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2803 | Map.clear(); |
| 2804 | Sequence.clear(); |
| 2805 | UsedByIndicesMap.clear(); |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 2806 | UniqueItems.clear(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2807 | |
| 2808 | // Now iterate through the worklist and add new formulae. |
| 2809 | for (SmallVectorImpl<WorkItem>::const_iterator I = WorkItems.begin(), |
| 2810 | E = WorkItems.end(); I != E; ++I) { |
| 2811 | const WorkItem &WI = *I; |
| 2812 | size_t LUIdx = WI.LUIdx; |
| 2813 | LSRUse &LU = Uses[LUIdx]; |
| 2814 | int64_t Imm = WI.Imm; |
| 2815 | const SCEV *OrigReg = WI.OrigReg; |
| 2816 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2817 | Type *IntTy = SE.getEffectiveSCEVType(OrigReg->getType()); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2818 | const SCEV *NegImmS = SE.getSCEV(ConstantInt::get(IntTy, -(uint64_t)Imm)); |
| 2819 | unsigned BitWidth = SE.getTypeSizeInBits(IntTy); |
| 2820 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 2821 | // TODO: Use a more targeted data structure. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2822 | for (size_t L = 0, LE = LU.Formulae.size(); L != LE; ++L) { |
Dan Gohman | 9f383eb | 2010-05-20 22:25:20 +0000 | [diff] [blame] | 2823 | const Formula &F = LU.Formulae[L]; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2824 | // Use the immediate in the scaled register. |
| 2825 | if (F.ScaledReg == OrigReg) { |
| 2826 | int64_t Offs = (uint64_t)F.AM.BaseOffs + |
| 2827 | Imm * (uint64_t)F.AM.Scale; |
| 2828 | // Don't create 50 + reg(-50). |
| 2829 | if (F.referencesReg(SE.getSCEV( |
| 2830 | ConstantInt::get(IntTy, -(uint64_t)Offs)))) |
| 2831 | continue; |
| 2832 | Formula NewF = F; |
| 2833 | NewF.AM.BaseOffs = Offs; |
| 2834 | if (!isLegalUse(NewF.AM, LU.MinOffset, LU.MaxOffset, |
| 2835 | LU.Kind, LU.AccessTy, TLI)) |
| 2836 | continue; |
| 2837 | NewF.ScaledReg = SE.getAddExpr(NegImmS, NewF.ScaledReg); |
| 2838 | |
| 2839 | // If the new scale is a constant in a register, and adding the constant |
| 2840 | // value to the immediate would produce a value closer to zero than the |
| 2841 | // immediate itself, then the formula isn't worthwhile. |
| 2842 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(NewF.ScaledReg)) |
Chris Lattner | c73b24d | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 2843 | if (C->getValue()->isNegative() != |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2844 | (NewF.AM.BaseOffs < 0) && |
| 2845 | (C->getValue()->getValue().abs() * APInt(BitWidth, F.AM.Scale)) |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 2846 | .ule(abs64(NewF.AM.BaseOffs))) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2847 | continue; |
| 2848 | |
| 2849 | // OK, looks good. |
| 2850 | (void)InsertFormula(LU, LUIdx, NewF); |
| 2851 | } else { |
| 2852 | // Use the immediate in a base register. |
| 2853 | for (size_t N = 0, NE = F.BaseRegs.size(); N != NE; ++N) { |
| 2854 | const SCEV *BaseReg = F.BaseRegs[N]; |
| 2855 | if (BaseReg != OrigReg) |
| 2856 | continue; |
| 2857 | Formula NewF = F; |
| 2858 | NewF.AM.BaseOffs = (uint64_t)NewF.AM.BaseOffs + Imm; |
| 2859 | if (!isLegalUse(NewF.AM, LU.MinOffset, LU.MaxOffset, |
Dan Gohman | cca8214 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 2860 | LU.Kind, LU.AccessTy, TLI)) { |
| 2861 | if (!TLI || |
| 2862 | !TLI->isLegalAddImmediate((uint64_t)NewF.UnfoldedOffset + Imm)) |
| 2863 | continue; |
| 2864 | NewF = F; |
| 2865 | NewF.UnfoldedOffset = (uint64_t)NewF.UnfoldedOffset + Imm; |
| 2866 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2867 | NewF.BaseRegs[N] = SE.getAddExpr(NegImmS, BaseReg); |
| 2868 | |
| 2869 | // If the new formula has a constant in a register, and adding the |
| 2870 | // constant value to the immediate would produce a value closer to |
| 2871 | // zero than the immediate itself, then the formula isn't worthwhile. |
| 2872 | for (SmallVectorImpl<const SCEV *>::const_iterator |
| 2873 | J = NewF.BaseRegs.begin(), JE = NewF.BaseRegs.end(); |
| 2874 | J != JE; ++J) |
| 2875 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(*J)) |
Dan Gohman | 360026f | 2010-05-18 23:48:08 +0000 | [diff] [blame] | 2876 | if ((C->getValue()->getValue() + NewF.AM.BaseOffs).abs().slt( |
| 2877 | abs64(NewF.AM.BaseOffs)) && |
| 2878 | (C->getValue()->getValue() + |
| 2879 | NewF.AM.BaseOffs).countTrailingZeros() >= |
| 2880 | CountTrailingZeros_64(NewF.AM.BaseOffs)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2881 | goto skip_formula; |
| 2882 | |
| 2883 | // Ok, looks good. |
| 2884 | (void)InsertFormula(LU, LUIdx, NewF); |
| 2885 | break; |
| 2886 | skip_formula:; |
| 2887 | } |
| 2888 | } |
| 2889 | } |
| 2890 | } |
Dale Johannesen | c1acc3f | 2009-05-11 17:15:42 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2893 | /// GenerateAllReuseFormulae - Generate formulae for each use. |
| 2894 | void |
| 2895 | LSRInstance::GenerateAllReuseFormulae() { |
Dan Gohman | c2385a0 | 2010-02-16 01:42:53 +0000 | [diff] [blame] | 2896 | // This is split into multiple loops so that hasRegsUsedByUsesOtherThan |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2897 | // queries are more precise. |
| 2898 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 2899 | LSRUse &LU = Uses[LUIdx]; |
| 2900 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 2901 | GenerateReassociations(LU, LUIdx, LU.Formulae[i]); |
| 2902 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 2903 | GenerateCombinations(LU, LUIdx, LU.Formulae[i]); |
| 2904 | } |
| 2905 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 2906 | LSRUse &LU = Uses[LUIdx]; |
| 2907 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 2908 | GenerateSymbolicOffsets(LU, LUIdx, LU.Formulae[i]); |
| 2909 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 2910 | GenerateConstantOffsets(LU, LUIdx, LU.Formulae[i]); |
| 2911 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 2912 | GenerateICmpZeroScales(LU, LUIdx, LU.Formulae[i]); |
| 2913 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 2914 | GenerateScales(LU, LUIdx, LU.Formulae[i]); |
Dan Gohman | c2385a0 | 2010-02-16 01:42:53 +0000 | [diff] [blame] | 2915 | } |
| 2916 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 2917 | LSRUse &LU = Uses[LUIdx]; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2918 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 2919 | GenerateTruncates(LU, LUIdx, LU.Formulae[i]); |
| 2920 | } |
| 2921 | |
| 2922 | GenerateCrossUseConstantOffsets(); |
Dan Gohman | 3902f9f | 2010-08-29 15:21:38 +0000 | [diff] [blame] | 2923 | |
| 2924 | DEBUG(dbgs() << "\n" |
| 2925 | "After generating reuse formulae:\n"; |
| 2926 | print_uses(dbgs())); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
Dan Gohman | f63d70f | 2010-10-07 23:43:09 +0000 | [diff] [blame] | 2929 | /// If there are multiple formulae with the same set of registers used |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2930 | /// by other uses, pick the best one and delete the others. |
| 2931 | void LSRInstance::FilterOutUndesirableDedicatedRegisters() { |
Dan Gohman | fc7744b | 2010-10-07 23:52:18 +0000 | [diff] [blame] | 2932 | DenseSet<const SCEV *> VisitedRegs; |
| 2933 | SmallPtrSet<const SCEV *, 16> Regs; |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 2934 | SmallPtrSet<const SCEV *, 16> LoserRegs; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2935 | #ifndef NDEBUG |
Dan Gohman | c6519f9 | 2010-05-20 20:05:31 +0000 | [diff] [blame] | 2936 | bool ChangedFormulae = false; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2937 | #endif |
| 2938 | |
| 2939 | // Collect the best formula for each unique set of shared registers. This |
| 2940 | // is reset for each use. |
| 2941 | typedef DenseMap<SmallVector<const SCEV *, 2>, size_t, UniquifierDenseMapInfo> |
| 2942 | BestFormulaeTy; |
| 2943 | BestFormulaeTy BestFormulae; |
| 2944 | |
| 2945 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 2946 | LSRUse &LU = Uses[LUIdx]; |
Dan Gohman | ea507f5 | 2010-05-20 19:44:23 +0000 | [diff] [blame] | 2947 | DEBUG(dbgs() << "Filtering for use "; LU.print(dbgs()); dbgs() << '\n'); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2948 | |
Dan Gohman | b2df433 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 2949 | bool Any = false; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2950 | for (size_t FIdx = 0, NumForms = LU.Formulae.size(); |
| 2951 | FIdx != NumForms; ++FIdx) { |
| 2952 | Formula &F = LU.Formulae[FIdx]; |
| 2953 | |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 2954 | // Some formulas are instant losers. For example, they may depend on |
| 2955 | // nonexistent AddRecs from other loops. These need to be filtered |
| 2956 | // immediately, otherwise heuristics could choose them over others leading |
| 2957 | // to an unsatisfactory solution. Passing LoserRegs into RateFormula here |
| 2958 | // avoids the need to recompute this information across formulae using the |
| 2959 | // same bad AddRec. Passing LoserRegs is also essential unless we remove |
| 2960 | // the corresponding bad register from the Regs set. |
| 2961 | Cost CostF; |
| 2962 | Regs.clear(); |
| 2963 | CostF.RateFormula(F, Regs, VisitedRegs, L, LU.Offsets, SE, DT, |
| 2964 | &LoserRegs); |
| 2965 | if (CostF.isLoser()) { |
| 2966 | // During initial formula generation, undesirable formulae are generated |
| 2967 | // by uses within other loops that have some non-trivial address mode or |
| 2968 | // use the postinc form of the IV. LSR needs to provide these formulae |
| 2969 | // as the basis of rediscovering the desired formula that uses an AddRec |
| 2970 | // corresponding to the existing phi. Once all formulae have been |
| 2971 | // generated, these initial losers may be pruned. |
| 2972 | DEBUG(dbgs() << " Filtering loser "; F.print(dbgs()); |
| 2973 | dbgs() << "\n"); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2974 | } |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 2975 | else { |
| 2976 | SmallVector<const SCEV *, 2> Key; |
| 2977 | for (SmallVectorImpl<const SCEV *>::const_iterator J = F.BaseRegs.begin(), |
| 2978 | JE = F.BaseRegs.end(); J != JE; ++J) { |
| 2979 | const SCEV *Reg = *J; |
| 2980 | if (RegUses.isRegUsedByUsesOtherThan(Reg, LUIdx)) |
| 2981 | Key.push_back(Reg); |
| 2982 | } |
| 2983 | if (F.ScaledReg && |
| 2984 | RegUses.isRegUsedByUsesOtherThan(F.ScaledReg, LUIdx)) |
| 2985 | Key.push_back(F.ScaledReg); |
| 2986 | // Unstable sort by host order ok, because this is only used for |
| 2987 | // uniquifying. |
| 2988 | std::sort(Key.begin(), Key.end()); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2989 | |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 2990 | std::pair<BestFormulaeTy::const_iterator, bool> P = |
| 2991 | BestFormulae.insert(std::make_pair(Key, FIdx)); |
| 2992 | if (P.second) |
| 2993 | continue; |
| 2994 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2995 | Formula &Best = LU.Formulae[P.first->second]; |
Dan Gohman | fc7744b | 2010-10-07 23:52:18 +0000 | [diff] [blame] | 2996 | |
Dan Gohman | fc7744b | 2010-10-07 23:52:18 +0000 | [diff] [blame] | 2997 | Cost CostBest; |
Dan Gohman | fc7744b | 2010-10-07 23:52:18 +0000 | [diff] [blame] | 2998 | Regs.clear(); |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 2999 | CostBest.RateFormula(Best, Regs, VisitedRegs, L, LU.Offsets, SE, DT); |
Dan Gohman | fc7744b | 2010-10-07 23:52:18 +0000 | [diff] [blame] | 3000 | if (CostF < CostBest) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3001 | std::swap(F, Best); |
Dan Gohman | 6458ff9 | 2010-05-18 22:37:37 +0000 | [diff] [blame] | 3002 | DEBUG(dbgs() << " Filtering out formula "; F.print(dbgs()); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3003 | dbgs() << "\n" |
Dan Gohman | 6458ff9 | 2010-05-18 22:37:37 +0000 | [diff] [blame] | 3004 | " in favor of formula "; Best.print(dbgs()); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3005 | dbgs() << '\n'); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3006 | } |
Andrew Trick | 8a5d792 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 3007 | #ifndef NDEBUG |
| 3008 | ChangedFormulae = true; |
| 3009 | #endif |
| 3010 | LU.DeleteFormula(F); |
| 3011 | --FIdx; |
| 3012 | --NumForms; |
| 3013 | Any = true; |
Dan Gohman | 59dc603 | 2010-05-07 23:36:59 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
Dan Gohman | 57aaa0b | 2010-05-18 23:55:57 +0000 | [diff] [blame] | 3016 | // Now that we've filtered out some formulae, recompute the Regs set. |
Dan Gohman | b2df433 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 3017 | if (Any) |
| 3018 | LU.RecomputeRegs(LUIdx, RegUses); |
Dan Gohman | 59dc603 | 2010-05-07 23:36:59 +0000 | [diff] [blame] | 3019 | |
| 3020 | // Reset this to prepare for the next use. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3021 | BestFormulae.clear(); |
| 3022 | } |
| 3023 | |
Dan Gohman | c6519f9 | 2010-05-20 20:05:31 +0000 | [diff] [blame] | 3024 | DEBUG(if (ChangedFormulae) { |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 3025 | dbgs() << "\n" |
| 3026 | "After filtering out undesirable candidates:\n"; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3027 | print_uses(dbgs()); |
| 3028 | }); |
| 3029 | } |
| 3030 | |
Dan Gohman | d079c30 | 2010-05-18 22:51:59 +0000 | [diff] [blame] | 3031 | // This is a rough guess that seems to work fairly well. |
| 3032 | static const size_t ComplexityLimit = UINT16_MAX; |
| 3033 | |
| 3034 | /// EstimateSearchSpaceComplexity - Estimate the worst-case number of |
| 3035 | /// solutions the solver might have to consider. It almost never considers |
| 3036 | /// this many solutions because it prune the search space, but the pruning |
| 3037 | /// isn't always sufficient. |
| 3038 | size_t LSRInstance::EstimateSearchSpaceComplexity() const { |
Dan Gohman | 0d6715a | 2010-10-07 23:37:58 +0000 | [diff] [blame] | 3039 | size_t Power = 1; |
Dan Gohman | d079c30 | 2010-05-18 22:51:59 +0000 | [diff] [blame] | 3040 | for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(), |
| 3041 | E = Uses.end(); I != E; ++I) { |
| 3042 | size_t FSize = I->Formulae.size(); |
| 3043 | if (FSize >= ComplexityLimit) { |
| 3044 | Power = ComplexityLimit; |
| 3045 | break; |
| 3046 | } |
| 3047 | Power *= FSize; |
| 3048 | if (Power >= ComplexityLimit) |
| 3049 | break; |
| 3050 | } |
| 3051 | return Power; |
| 3052 | } |
| 3053 | |
Dan Gohman | 4aa5c2e | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 3054 | /// NarrowSearchSpaceByDetectingSupersets - When one formula uses a superset |
| 3055 | /// of the registers of another formula, it won't help reduce register |
| 3056 | /// pressure (though it may not necessarily hurt register pressure); remove |
| 3057 | /// it to simplify the system. |
| 3058 | void LSRInstance::NarrowSearchSpaceByDetectingSupersets() { |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 3059 | if (EstimateSearchSpaceComplexity() >= ComplexityLimit) { |
| 3060 | DEBUG(dbgs() << "The search space is too complex.\n"); |
| 3061 | |
| 3062 | DEBUG(dbgs() << "Narrowing the search space by eliminating formulae " |
| 3063 | "which use a superset of registers used by other " |
| 3064 | "formulae.\n"); |
| 3065 | |
| 3066 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 3067 | LSRUse &LU = Uses[LUIdx]; |
| 3068 | bool Any = false; |
| 3069 | for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) { |
| 3070 | Formula &F = LU.Formulae[i]; |
Dan Gohman | f7ff37d | 2010-05-20 20:00:41 +0000 | [diff] [blame] | 3071 | // Look for a formula with a constant or GV in a register. If the use |
| 3072 | // also has a formula with that same value in an immediate field, |
| 3073 | // delete the one that uses a register. |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 3074 | for (SmallVectorImpl<const SCEV *>::const_iterator |
| 3075 | I = F.BaseRegs.begin(), E = F.BaseRegs.end(); I != E; ++I) { |
| 3076 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(*I)) { |
| 3077 | Formula NewF = F; |
| 3078 | NewF.AM.BaseOffs += C->getValue()->getSExtValue(); |
| 3079 | NewF.BaseRegs.erase(NewF.BaseRegs.begin() + |
| 3080 | (I - F.BaseRegs.begin())); |
| 3081 | if (LU.HasFormulaWithSameRegs(NewF)) { |
| 3082 | DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n'); |
| 3083 | LU.DeleteFormula(F); |
| 3084 | --i; |
| 3085 | --e; |
| 3086 | Any = true; |
| 3087 | break; |
| 3088 | } |
| 3089 | } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(*I)) { |
| 3090 | if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue())) |
| 3091 | if (!F.AM.BaseGV) { |
| 3092 | Formula NewF = F; |
| 3093 | NewF.AM.BaseGV = GV; |
| 3094 | NewF.BaseRegs.erase(NewF.BaseRegs.begin() + |
| 3095 | (I - F.BaseRegs.begin())); |
| 3096 | if (LU.HasFormulaWithSameRegs(NewF)) { |
| 3097 | DEBUG(dbgs() << " Deleting "; F.print(dbgs()); |
| 3098 | dbgs() << '\n'); |
| 3099 | LU.DeleteFormula(F); |
| 3100 | --i; |
| 3101 | --e; |
| 3102 | Any = true; |
| 3103 | break; |
| 3104 | } |
| 3105 | } |
| 3106 | } |
| 3107 | } |
| 3108 | } |
| 3109 | if (Any) |
| 3110 | LU.RecomputeRegs(LUIdx, RegUses); |
| 3111 | } |
| 3112 | |
| 3113 | DEBUG(dbgs() << "After pre-selection:\n"; |
| 3114 | print_uses(dbgs())); |
| 3115 | } |
Dan Gohman | 4aa5c2e | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 3116 | } |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 3117 | |
Dan Gohman | 4aa5c2e | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 3118 | /// NarrowSearchSpaceByCollapsingUnrolledCode - When there are many registers |
| 3119 | /// for expressions like A, A+1, A+2, etc., allocate a single register for |
| 3120 | /// them. |
| 3121 | void LSRInstance::NarrowSearchSpaceByCollapsingUnrolledCode() { |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 3122 | if (EstimateSearchSpaceComplexity() >= ComplexityLimit) { |
| 3123 | DEBUG(dbgs() << "The search space is too complex.\n"); |
| 3124 | |
| 3125 | DEBUG(dbgs() << "Narrowing the search space by assuming that uses " |
| 3126 | "separated by a constant offset will use the same " |
| 3127 | "registers.\n"); |
| 3128 | |
Dan Gohman | f7ff37d | 2010-05-20 20:00:41 +0000 | [diff] [blame] | 3129 | // This is especially useful for unrolled loops. |
| 3130 | |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 3131 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 3132 | LSRUse &LU = Uses[LUIdx]; |
Dan Gohman | 402d435 | 2010-05-20 20:33:18 +0000 | [diff] [blame] | 3133 | for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(), |
| 3134 | E = LU.Formulae.end(); I != E; ++I) { |
| 3135 | const Formula &F = *I; |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 3136 | if (F.AM.BaseOffs != 0 && F.AM.Scale == 0) { |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 3137 | if (LSRUse *LUThatHas = FindUseWithSimilarFormula(F, LU)) { |
| 3138 | if (reconcileNewOffset(*LUThatHas, F.AM.BaseOffs, |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 3139 | /*HasBaseReg=*/false, |
| 3140 | LU.Kind, LU.AccessTy)) { |
| 3141 | DEBUG(dbgs() << " Deleting use "; LU.print(dbgs()); |
| 3142 | dbgs() << '\n'); |
| 3143 | |
| 3144 | LUThatHas->AllFixupsOutsideLoop &= LU.AllFixupsOutsideLoop; |
| 3145 | |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 3146 | // Update the relocs to reference the new use. |
| 3147 | for (SmallVectorImpl<LSRFixup>::iterator I = Fixups.begin(), |
| 3148 | E = Fixups.end(); I != E; ++I) { |
| 3149 | LSRFixup &Fixup = *I; |
| 3150 | if (Fixup.LUIdx == LUIdx) { |
| 3151 | Fixup.LUIdx = LUThatHas - &Uses.front(); |
| 3152 | Fixup.Offset += F.AM.BaseOffs; |
Dan Gohman | dd3db0e | 2010-10-07 23:36:45 +0000 | [diff] [blame] | 3153 | // Add the new offset to LUThatHas' offset list. |
| 3154 | if (LUThatHas->Offsets.back() != Fixup.Offset) { |
| 3155 | LUThatHas->Offsets.push_back(Fixup.Offset); |
| 3156 | if (Fixup.Offset > LUThatHas->MaxOffset) |
| 3157 | LUThatHas->MaxOffset = Fixup.Offset; |
| 3158 | if (Fixup.Offset < LUThatHas->MinOffset) |
| 3159 | LUThatHas->MinOffset = Fixup.Offset; |
| 3160 | } |
Dan Gohman | 191bd64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 3161 | DEBUG(dbgs() << "New fixup has offset " |
| 3162 | << Fixup.Offset << '\n'); |
| 3163 | } |
| 3164 | if (Fixup.LUIdx == NumUses-1) |
| 3165 | Fixup.LUIdx = LUIdx; |
| 3166 | } |
| 3167 | |
Dan Gohman | c2921ea | 2010-10-08 19:33:26 +0000 | [diff] [blame] | 3168 | // Delete formulae from the new use which are no longer legal. |
| 3169 | bool Any = false; |
| 3170 | for (size_t i = 0, e = LUThatHas->Formulae.size(); i != e; ++i) { |
| 3171 | Formula &F = LUThatHas->Formulae[i]; |
| 3172 | if (!isLegalUse(F.AM, |
| 3173 | LUThatHas->MinOffset, LUThatHas->MaxOffset, |
| 3174 | LUThatHas->Kind, LUThatHas->AccessTy, TLI)) { |
| 3175 | DEBUG(dbgs() << " Deleting "; F.print(dbgs()); |
| 3176 | dbgs() << '\n'); |
| 3177 | LUThatHas->DeleteFormula(F); |
| 3178 | --i; |
| 3179 | --e; |
| 3180 | Any = true; |
| 3181 | } |
| 3182 | } |
| 3183 | if (Any) |
| 3184 | LUThatHas->RecomputeRegs(LUThatHas - &Uses.front(), RegUses); |
| 3185 | |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 3186 | // Delete the old use. |
Dan Gohman | c689770 | 2010-10-07 23:33:43 +0000 | [diff] [blame] | 3187 | DeleteUse(LU, LUIdx); |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 3188 | --LUIdx; |
| 3189 | --NumUses; |
| 3190 | break; |
| 3191 | } |
| 3192 | } |
| 3193 | } |
| 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | DEBUG(dbgs() << "After pre-selection:\n"; |
| 3198 | print_uses(dbgs())); |
| 3199 | } |
Dan Gohman | 4aa5c2e | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 3200 | } |
Dan Gohman | a2086b3 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 3201 | |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 3202 | /// NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters - Call |
Dan Gohman | 4f7e18d | 2010-08-29 16:39:22 +0000 | [diff] [blame] | 3203 | /// FilterOutUndesirableDedicatedRegisters again, if necessary, now that |
| 3204 | /// we've done more filtering, as it may be able to find more formulae to |
| 3205 | /// eliminate. |
| 3206 | void LSRInstance::NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters(){ |
| 3207 | if (EstimateSearchSpaceComplexity() >= ComplexityLimit) { |
| 3208 | DEBUG(dbgs() << "The search space is too complex.\n"); |
| 3209 | |
| 3210 | DEBUG(dbgs() << "Narrowing the search space by re-filtering out " |
| 3211 | "undesirable dedicated registers.\n"); |
| 3212 | |
| 3213 | FilterOutUndesirableDedicatedRegisters(); |
| 3214 | |
| 3215 | DEBUG(dbgs() << "After pre-selection:\n"; |
| 3216 | print_uses(dbgs())); |
| 3217 | } |
| 3218 | } |
| 3219 | |
Dan Gohman | 4aa5c2e | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 3220 | /// NarrowSearchSpaceByPickingWinnerRegs - Pick a register which seems likely |
| 3221 | /// to be profitable, and then in any use which has any reference to that |
| 3222 | /// register, delete all formulae which do not reference that register. |
| 3223 | void LSRInstance::NarrowSearchSpaceByPickingWinnerRegs() { |
Dan Gohman | 76c315a | 2010-05-20 20:52:00 +0000 | [diff] [blame] | 3224 | // With all other options exhausted, loop until the system is simple |
| 3225 | // enough to handle. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3226 | SmallPtrSet<const SCEV *, 4> Taken; |
Dan Gohman | d079c30 | 2010-05-18 22:51:59 +0000 | [diff] [blame] | 3227 | while (EstimateSearchSpaceComplexity() >= ComplexityLimit) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3228 | // Ok, we have too many of formulae on our hands to conveniently handle. |
| 3229 | // Use a rough heuristic to thin out the list. |
Dan Gohman | 0da751b | 2010-05-18 22:41:32 +0000 | [diff] [blame] | 3230 | DEBUG(dbgs() << "The search space is too complex.\n"); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3231 | |
| 3232 | // Pick the register which is used by the most LSRUses, which is likely |
| 3233 | // to be a good reuse register candidate. |
| 3234 | const SCEV *Best = 0; |
| 3235 | unsigned BestNum = 0; |
| 3236 | for (RegUseTracker::const_iterator I = RegUses.begin(), E = RegUses.end(); |
| 3237 | I != E; ++I) { |
| 3238 | const SCEV *Reg = *I; |
| 3239 | if (Taken.count(Reg)) |
| 3240 | continue; |
| 3241 | if (!Best) |
| 3242 | Best = Reg; |
| 3243 | else { |
| 3244 | unsigned Count = RegUses.getUsedByIndices(Reg).count(); |
| 3245 | if (Count > BestNum) { |
| 3246 | Best = Reg; |
| 3247 | BestNum = Count; |
| 3248 | } |
| 3249 | } |
| 3250 | } |
| 3251 | |
| 3252 | DEBUG(dbgs() << "Narrowing the search space by assuming " << *Best |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3253 | << " will yield profitable reuse.\n"); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3254 | Taken.insert(Best); |
| 3255 | |
| 3256 | // In any use with formulae which references this register, delete formulae |
| 3257 | // which don't reference it. |
Dan Gohman | b2df433 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 3258 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 3259 | LSRUse &LU = Uses[LUIdx]; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3260 | if (!LU.Regs.count(Best)) continue; |
| 3261 | |
Dan Gohman | b2df433 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 3262 | bool Any = false; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3263 | for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) { |
| 3264 | Formula &F = LU.Formulae[i]; |
| 3265 | if (!F.referencesReg(Best)) { |
| 3266 | DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n'); |
Dan Gohman | d69d628 | 2010-05-18 22:39:15 +0000 | [diff] [blame] | 3267 | LU.DeleteFormula(F); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3268 | --e; |
| 3269 | --i; |
Dan Gohman | b2df433 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 3270 | Any = true; |
Dan Gohman | 59dc603 | 2010-05-07 23:36:59 +0000 | [diff] [blame] | 3271 | assert(e != 0 && "Use has no formulae left! Is Regs inconsistent?"); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3272 | continue; |
| 3273 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3274 | } |
Dan Gohman | b2df433 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 3275 | |
| 3276 | if (Any) |
| 3277 | LU.RecomputeRegs(LUIdx, RegUses); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | DEBUG(dbgs() << "After pre-selection:\n"; |
| 3281 | print_uses(dbgs())); |
| 3282 | } |
| 3283 | } |
| 3284 | |
Dan Gohman | 4aa5c2e | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 3285 | /// NarrowSearchSpaceUsingHeuristics - If there are an extraordinary number of |
| 3286 | /// formulae to choose from, use some rough heuristics to prune down the number |
| 3287 | /// of formulae. This keeps the main solver from taking an extraordinary amount |
| 3288 | /// of time in some worst-case scenarios. |
| 3289 | void LSRInstance::NarrowSearchSpaceUsingHeuristics() { |
| 3290 | NarrowSearchSpaceByDetectingSupersets(); |
| 3291 | NarrowSearchSpaceByCollapsingUnrolledCode(); |
Dan Gohman | 4f7e18d | 2010-08-29 16:39:22 +0000 | [diff] [blame] | 3292 | NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters(); |
Dan Gohman | 4aa5c2e | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 3293 | NarrowSearchSpaceByPickingWinnerRegs(); |
| 3294 | } |
| 3295 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3296 | /// SolveRecurse - This is the recursive solver. |
| 3297 | void LSRInstance::SolveRecurse(SmallVectorImpl<const Formula *> &Solution, |
| 3298 | Cost &SolutionCost, |
| 3299 | SmallVectorImpl<const Formula *> &Workspace, |
| 3300 | const Cost &CurCost, |
| 3301 | const SmallPtrSet<const SCEV *, 16> &CurRegs, |
| 3302 | DenseSet<const SCEV *> &VisitedRegs) const { |
| 3303 | // Some ideas: |
| 3304 | // - prune more: |
| 3305 | // - use more aggressive filtering |
| 3306 | // - sort the formula so that the most profitable solutions are found first |
| 3307 | // - sort the uses too |
| 3308 | // - search faster: |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3309 | // - don't compute a cost, and then compare. compare while computing a cost |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3310 | // and bail early. |
| 3311 | // - track register sets with SmallBitVector |
| 3312 | |
| 3313 | const LSRUse &LU = Uses[Workspace.size()]; |
| 3314 | |
| 3315 | // If this use references any register that's already a part of the |
| 3316 | // in-progress solution, consider it a requirement that a formula must |
| 3317 | // reference that register in order to be considered. This prunes out |
| 3318 | // unprofitable searching. |
| 3319 | SmallSetVector<const SCEV *, 4> ReqRegs; |
| 3320 | for (SmallPtrSet<const SCEV *, 16>::const_iterator I = CurRegs.begin(), |
| 3321 | E = CurRegs.end(); I != E; ++I) |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 3322 | if (LU.Regs.count(*I)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3323 | ReqRegs.insert(*I); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3324 | |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 3325 | bool AnySatisfiedReqRegs = false; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3326 | SmallPtrSet<const SCEV *, 16> NewRegs; |
| 3327 | Cost NewCost; |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 3328 | retry: |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3329 | for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(), |
| 3330 | E = LU.Formulae.end(); I != E; ++I) { |
| 3331 | const Formula &F = *I; |
| 3332 | |
| 3333 | // Ignore formulae which do not use any of the required registers. |
| 3334 | for (SmallSetVector<const SCEV *, 4>::const_iterator J = ReqRegs.begin(), |
| 3335 | JE = ReqRegs.end(); J != JE; ++J) { |
| 3336 | const SCEV *Reg = *J; |
| 3337 | if ((!F.ScaledReg || F.ScaledReg != Reg) && |
| 3338 | std::find(F.BaseRegs.begin(), F.BaseRegs.end(), Reg) == |
| 3339 | F.BaseRegs.end()) |
| 3340 | goto skip; |
| 3341 | } |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 3342 | AnySatisfiedReqRegs = true; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3343 | |
| 3344 | // Evaluate the cost of the current formula. If it's already worse than |
| 3345 | // the current best, prune the search at that point. |
| 3346 | NewCost = CurCost; |
| 3347 | NewRegs = CurRegs; |
| 3348 | NewCost.RateFormula(F, NewRegs, VisitedRegs, L, LU.Offsets, SE, DT); |
| 3349 | if (NewCost < SolutionCost) { |
| 3350 | Workspace.push_back(&F); |
| 3351 | if (Workspace.size() != Uses.size()) { |
| 3352 | SolveRecurse(Solution, SolutionCost, Workspace, NewCost, |
| 3353 | NewRegs, VisitedRegs); |
| 3354 | if (F.getNumRegs() == 1 && Workspace.size() == 1) |
| 3355 | VisitedRegs.insert(F.ScaledReg ? F.ScaledReg : F.BaseRegs[0]); |
| 3356 | } else { |
| 3357 | DEBUG(dbgs() << "New best at "; NewCost.print(dbgs()); |
| 3358 | dbgs() << ". Regs:"; |
| 3359 | for (SmallPtrSet<const SCEV *, 16>::const_iterator |
| 3360 | I = NewRegs.begin(), E = NewRegs.end(); I != E; ++I) |
| 3361 | dbgs() << ' ' << **I; |
| 3362 | dbgs() << '\n'); |
| 3363 | |
| 3364 | SolutionCost = NewCost; |
| 3365 | Solution = Workspace; |
| 3366 | } |
| 3367 | Workspace.pop_back(); |
| 3368 | } |
| 3369 | skip:; |
| 3370 | } |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 3371 | |
Andrew Trick | 80ef1b2 | 2011-09-27 00:44:14 +0000 | [diff] [blame] | 3372 | if (!EnableRetry && !AnySatisfiedReqRegs) |
| 3373 | return; |
| 3374 | |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 3375 | // If none of the formulae had all of the required registers, relax the |
| 3376 | // constraint so that we don't exclude all formulae. |
| 3377 | if (!AnySatisfiedReqRegs) { |
Dan Gohman | 59dc603 | 2010-05-07 23:36:59 +0000 | [diff] [blame] | 3378 | assert(!ReqRegs.empty() && "Solver failed even without required registers"); |
Dan Gohman | 9214b82 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 3379 | ReqRegs.clear(); |
| 3380 | goto retry; |
| 3381 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
Dan Gohman | 76c315a | 2010-05-20 20:52:00 +0000 | [diff] [blame] | 3384 | /// Solve - Choose one formula from each use. Return the results in the given |
| 3385 | /// Solution vector. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3386 | void LSRInstance::Solve(SmallVectorImpl<const Formula *> &Solution) const { |
| 3387 | SmallVector<const Formula *, 8> Workspace; |
| 3388 | Cost SolutionCost; |
| 3389 | SolutionCost.Loose(); |
| 3390 | Cost CurCost; |
| 3391 | SmallPtrSet<const SCEV *, 16> CurRegs; |
| 3392 | DenseSet<const SCEV *> VisitedRegs; |
| 3393 | Workspace.reserve(Uses.size()); |
| 3394 | |
Dan Gohman | f7ff37d | 2010-05-20 20:00:41 +0000 | [diff] [blame] | 3395 | // SolveRecurse does all the work. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3396 | SolveRecurse(Solution, SolutionCost, Workspace, CurCost, |
| 3397 | CurRegs, VisitedRegs); |
Andrew Trick | 80ef1b2 | 2011-09-27 00:44:14 +0000 | [diff] [blame] | 3398 | if (Solution.empty()) { |
| 3399 | DEBUG(dbgs() << "\nNo Satisfactory Solution\n"); |
| 3400 | return; |
| 3401 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3402 | |
| 3403 | // Ok, we've now made all our decisions. |
| 3404 | DEBUG(dbgs() << "\n" |
| 3405 | "The chosen solution requires "; SolutionCost.print(dbgs()); |
| 3406 | dbgs() << ":\n"; |
| 3407 | for (size_t i = 0, e = Uses.size(); i != e; ++i) { |
| 3408 | dbgs() << " "; |
| 3409 | Uses[i].print(dbgs()); |
| 3410 | dbgs() << "\n" |
| 3411 | " "; |
| 3412 | Solution[i]->print(dbgs()); |
| 3413 | dbgs() << '\n'; |
| 3414 | }); |
Dan Gohman | a552878 | 2010-05-20 20:59:23 +0000 | [diff] [blame] | 3415 | |
| 3416 | assert(Solution.size() == Uses.size() && "Malformed solution!"); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3417 | } |
| 3418 | |
Dan Gohman | e5f7687 | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 3419 | /// HoistInsertPosition - Helper for AdjustInsertPositionForExpand. Climb up |
| 3420 | /// the dominator tree far as we can go while still being dominated by the |
| 3421 | /// input positions. This helps canonicalize the insert position, which |
| 3422 | /// encourages sharing. |
| 3423 | BasicBlock::iterator |
| 3424 | LSRInstance::HoistInsertPosition(BasicBlock::iterator IP, |
| 3425 | const SmallVectorImpl<Instruction *> &Inputs) |
| 3426 | const { |
| 3427 | for (;;) { |
| 3428 | const Loop *IPLoop = LI.getLoopFor(IP->getParent()); |
| 3429 | unsigned IPLoopDepth = IPLoop ? IPLoop->getLoopDepth() : 0; |
| 3430 | |
| 3431 | BasicBlock *IDom; |
Dan Gohman | d974a0e | 2010-05-20 20:00:25 +0000 | [diff] [blame] | 3432 | for (DomTreeNode *Rung = DT.getNode(IP->getParent()); ; ) { |
Dan Gohman | 0fe46d9 | 2010-05-20 22:46:54 +0000 | [diff] [blame] | 3433 | if (!Rung) return IP; |
Dan Gohman | d974a0e | 2010-05-20 20:00:25 +0000 | [diff] [blame] | 3434 | Rung = Rung->getIDom(); |
| 3435 | if (!Rung) return IP; |
| 3436 | IDom = Rung->getBlock(); |
Dan Gohman | e5f7687 | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 3437 | |
| 3438 | // Don't climb into a loop though. |
| 3439 | const Loop *IDomLoop = LI.getLoopFor(IDom); |
| 3440 | unsigned IDomDepth = IDomLoop ? IDomLoop->getLoopDepth() : 0; |
| 3441 | if (IDomDepth <= IPLoopDepth && |
| 3442 | (IDomDepth != IPLoopDepth || IDomLoop == IPLoop)) |
| 3443 | break; |
| 3444 | } |
| 3445 | |
| 3446 | bool AllDominate = true; |
| 3447 | Instruction *BetterPos = 0; |
| 3448 | Instruction *Tentative = IDom->getTerminator(); |
| 3449 | for (SmallVectorImpl<Instruction *>::const_iterator I = Inputs.begin(), |
| 3450 | E = Inputs.end(); I != E; ++I) { |
| 3451 | Instruction *Inst = *I; |
| 3452 | if (Inst == Tentative || !DT.dominates(Inst, Tentative)) { |
| 3453 | AllDominate = false; |
| 3454 | break; |
| 3455 | } |
| 3456 | // Attempt to find an insert position in the middle of the block, |
| 3457 | // instead of at the end, so that it can be used for other expansions. |
| 3458 | if (IDom == Inst->getParent() && |
| 3459 | (!BetterPos || DT.dominates(BetterPos, Inst))) |
Douglas Gregor | 7d9663c | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 3460 | BetterPos = llvm::next(BasicBlock::iterator(Inst)); |
Dan Gohman | e5f7687 | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 3461 | } |
| 3462 | if (!AllDominate) |
| 3463 | break; |
| 3464 | if (BetterPos) |
| 3465 | IP = BetterPos; |
| 3466 | else |
| 3467 | IP = Tentative; |
| 3468 | } |
| 3469 | |
| 3470 | return IP; |
| 3471 | } |
| 3472 | |
| 3473 | /// AdjustInsertPositionForExpand - Determine an input position which will be |
Dan Gohman | d96eae8 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 3474 | /// dominated by the operands and which will dominate the result. |
| 3475 | BasicBlock::iterator |
Dan Gohman | e5f7687 | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 3476 | LSRInstance::AdjustInsertPositionForExpand(BasicBlock::iterator IP, |
| 3477 | const LSRFixup &LF, |
| 3478 | const LSRUse &LU) const { |
Dan Gohman | d96eae8 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 3479 | // Collect some instructions which must be dominated by the |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 3480 | // expanding replacement. These must be dominated by any operands that |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3481 | // will be required in the expansion. |
| 3482 | SmallVector<Instruction *, 4> Inputs; |
| 3483 | if (Instruction *I = dyn_cast<Instruction>(LF.OperandValToReplace)) |
| 3484 | Inputs.push_back(I); |
| 3485 | if (LU.Kind == LSRUse::ICmpZero) |
| 3486 | if (Instruction *I = |
| 3487 | dyn_cast<Instruction>(cast<ICmpInst>(LF.UserInst)->getOperand(1))) |
| 3488 | Inputs.push_back(I); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 3489 | if (LF.PostIncLoops.count(L)) { |
| 3490 | if (LF.isUseFullyOutsideLoop(L)) |
Dan Gohman | 069d6f3 | 2010-03-02 01:59:21 +0000 | [diff] [blame] | 3491 | Inputs.push_back(L->getLoopLatch()->getTerminator()); |
| 3492 | else |
| 3493 | Inputs.push_back(IVIncInsertPos); |
| 3494 | } |
Dan Gohman | 701a4ae | 2010-04-08 05:57:57 +0000 | [diff] [blame] | 3495 | // The expansion must also be dominated by the increment positions of any |
| 3496 | // loops it for which it is using post-inc mode. |
| 3497 | for (PostIncLoopSet::const_iterator I = LF.PostIncLoops.begin(), |
| 3498 | E = LF.PostIncLoops.end(); I != E; ++I) { |
| 3499 | const Loop *PIL = *I; |
| 3500 | if (PIL == L) continue; |
| 3501 | |
Dan Gohman | e5f7687 | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 3502 | // Be dominated by the loop exit. |
Dan Gohman | 701a4ae | 2010-04-08 05:57:57 +0000 | [diff] [blame] | 3503 | SmallVector<BasicBlock *, 4> ExitingBlocks; |
| 3504 | PIL->getExitingBlocks(ExitingBlocks); |
| 3505 | if (!ExitingBlocks.empty()) { |
| 3506 | BasicBlock *BB = ExitingBlocks[0]; |
| 3507 | for (unsigned i = 1, e = ExitingBlocks.size(); i != e; ++i) |
| 3508 | BB = DT.findNearestCommonDominator(BB, ExitingBlocks[i]); |
| 3509 | Inputs.push_back(BB->getTerminator()); |
| 3510 | } |
| 3511 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3512 | |
| 3513 | // Then, climb up the immediate dominator tree as far as we can go while |
| 3514 | // still being dominated by the input positions. |
Dan Gohman | e5f7687 | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 3515 | IP = HoistInsertPosition(IP, Inputs); |
Dan Gohman | d96eae8 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 3516 | |
| 3517 | // Don't insert instructions before PHI nodes. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3518 | while (isa<PHINode>(IP)) ++IP; |
Dan Gohman | d96eae8 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 3519 | |
Bill Wendling | a4c86ab | 2011-08-24 21:06:46 +0000 | [diff] [blame] | 3520 | // Ignore landingpad instructions. |
| 3521 | while (isa<LandingPadInst>(IP)) ++IP; |
| 3522 | |
Dan Gohman | d96eae8 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 3523 | // Ignore debug intrinsics. |
Dan Gohman | 449f31c | 2010-03-26 00:33:27 +0000 | [diff] [blame] | 3524 | while (isa<DbgInfoIntrinsic>(IP)) ++IP; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3525 | |
Dan Gohman | d96eae8 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 3526 | return IP; |
| 3527 | } |
| 3528 | |
Dan Gohman | 76c315a | 2010-05-20 20:52:00 +0000 | [diff] [blame] | 3529 | /// Expand - Emit instructions for the leading candidate expression for this |
| 3530 | /// LSRUse (this is called "expanding"). |
Dan Gohman | d96eae8 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 3531 | Value *LSRInstance::Expand(const LSRFixup &LF, |
| 3532 | const Formula &F, |
| 3533 | BasicBlock::iterator IP, |
| 3534 | SCEVExpander &Rewriter, |
| 3535 | SmallVectorImpl<WeakVH> &DeadInsts) const { |
| 3536 | const LSRUse &LU = Uses[LF.LUIdx]; |
| 3537 | |
| 3538 | // Determine an input position which will be dominated by the operands and |
| 3539 | // which will dominate the result. |
Dan Gohman | e5f7687 | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 3540 | IP = AdjustInsertPositionForExpand(IP, LF, LU); |
Dan Gohman | d96eae8 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 3541 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3542 | // Inform the Rewriter if we have a post-increment use, so that it can |
| 3543 | // perform an advantageous expansion. |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 3544 | Rewriter.setPostInc(LF.PostIncLoops); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3545 | |
| 3546 | // This is the type that the user actually needs. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3547 | Type *OpTy = LF.OperandValToReplace->getType(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3548 | // This will be the type that we'll initially expand to. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3549 | Type *Ty = F.getType(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3550 | if (!Ty) |
| 3551 | // No type known; just expand directly to the ultimate type. |
| 3552 | Ty = OpTy; |
| 3553 | else if (SE.getEffectiveSCEVType(Ty) == SE.getEffectiveSCEVType(OpTy)) |
| 3554 | // Expand directly to the ultimate type if it's the right size. |
| 3555 | Ty = OpTy; |
| 3556 | // This is the type to do integer arithmetic in. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3557 | Type *IntTy = SE.getEffectiveSCEVType(Ty); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3558 | |
| 3559 | // Build up a list of operands to add together to form the full base. |
| 3560 | SmallVector<const SCEV *, 8> Ops; |
| 3561 | |
| 3562 | // Expand the BaseRegs portion. |
| 3563 | for (SmallVectorImpl<const SCEV *>::const_iterator I = F.BaseRegs.begin(), |
| 3564 | E = F.BaseRegs.end(); I != E; ++I) { |
| 3565 | const SCEV *Reg = *I; |
| 3566 | assert(!Reg->isZero() && "Zero allocated in a base register!"); |
| 3567 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 3568 | // If we're expanding for a post-inc user, make the post-inc adjustment. |
| 3569 | PostIncLoopSet &Loops = const_cast<PostIncLoopSet &>(LF.PostIncLoops); |
| 3570 | Reg = TransformForPostIncUse(Denormalize, Reg, |
| 3571 | LF.UserInst, LF.OperandValToReplace, |
| 3572 | Loops, SE, DT); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3573 | |
| 3574 | Ops.push_back(SE.getUnknown(Rewriter.expandCodeFor(Reg, 0, IP))); |
| 3575 | } |
| 3576 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 3577 | // Flush the operand list to suppress SCEVExpander hoisting. |
| 3578 | if (!Ops.empty()) { |
| 3579 | Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, IP); |
| 3580 | Ops.clear(); |
| 3581 | Ops.push_back(SE.getUnknown(FullV)); |
| 3582 | } |
| 3583 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3584 | // Expand the ScaledReg portion. |
| 3585 | Value *ICmpScaledV = 0; |
| 3586 | if (F.AM.Scale != 0) { |
| 3587 | const SCEV *ScaledS = F.ScaledReg; |
| 3588 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 3589 | // If we're expanding for a post-inc user, make the post-inc adjustment. |
| 3590 | PostIncLoopSet &Loops = const_cast<PostIncLoopSet &>(LF.PostIncLoops); |
| 3591 | ScaledS = TransformForPostIncUse(Denormalize, ScaledS, |
| 3592 | LF.UserInst, LF.OperandValToReplace, |
| 3593 | Loops, SE, DT); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3594 | |
| 3595 | if (LU.Kind == LSRUse::ICmpZero) { |
| 3596 | // An interesting way of "folding" with an icmp is to use a negated |
| 3597 | // scale, which we'll implement by inserting it into the other operand |
| 3598 | // of the icmp. |
| 3599 | assert(F.AM.Scale == -1 && |
| 3600 | "The only scale supported by ICmpZero uses is -1!"); |
| 3601 | ICmpScaledV = Rewriter.expandCodeFor(ScaledS, 0, IP); |
| 3602 | } else { |
| 3603 | // Otherwise just expand the scaled register and an explicit scale, |
| 3604 | // which is expected to be matched as part of the address. |
| 3605 | ScaledS = SE.getUnknown(Rewriter.expandCodeFor(ScaledS, 0, IP)); |
| 3606 | ScaledS = SE.getMulExpr(ScaledS, |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 3607 | SE.getConstant(ScaledS->getType(), F.AM.Scale)); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3608 | Ops.push_back(ScaledS); |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 3609 | |
| 3610 | // Flush the operand list to suppress SCEVExpander hoisting. |
| 3611 | Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, IP); |
| 3612 | Ops.clear(); |
| 3613 | Ops.push_back(SE.getUnknown(FullV)); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3614 | } |
| 3615 | } |
| 3616 | |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 3617 | // Expand the GV portion. |
| 3618 | if (F.AM.BaseGV) { |
| 3619 | Ops.push_back(SE.getUnknown(F.AM.BaseGV)); |
| 3620 | |
| 3621 | // Flush the operand list to suppress SCEVExpander hoisting. |
| 3622 | Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, IP); |
| 3623 | Ops.clear(); |
| 3624 | Ops.push_back(SE.getUnknown(FullV)); |
| 3625 | } |
| 3626 | |
| 3627 | // Expand the immediate portion. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3628 | int64_t Offset = (uint64_t)F.AM.BaseOffs + LF.Offset; |
| 3629 | if (Offset != 0) { |
| 3630 | if (LU.Kind == LSRUse::ICmpZero) { |
| 3631 | // The other interesting way of "folding" with an ICmpZero is to use a |
| 3632 | // negated immediate. |
| 3633 | if (!ICmpScaledV) |
Eli Friedman | dae36ba | 2011-10-13 23:48:33 +0000 | [diff] [blame] | 3634 | ICmpScaledV = ConstantInt::get(IntTy, -(uint64_t)Offset); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3635 | else { |
| 3636 | Ops.push_back(SE.getUnknown(ICmpScaledV)); |
| 3637 | ICmpScaledV = ConstantInt::get(IntTy, Offset); |
| 3638 | } |
| 3639 | } else { |
| 3640 | // Just add the immediate values. These again are expected to be matched |
| 3641 | // as part of the address. |
Dan Gohman | 087bd1e | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 3642 | Ops.push_back(SE.getUnknown(ConstantInt::getSigned(IntTy, Offset))); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3643 | } |
| 3644 | } |
| 3645 | |
Dan Gohman | cca8214 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 3646 | // Expand the unfolded offset portion. |
| 3647 | int64_t UnfoldedOffset = F.UnfoldedOffset; |
| 3648 | if (UnfoldedOffset != 0) { |
| 3649 | // Just add the immediate values. |
| 3650 | Ops.push_back(SE.getUnknown(ConstantInt::getSigned(IntTy, |
| 3651 | UnfoldedOffset))); |
| 3652 | } |
| 3653 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3654 | // Emit instructions summing all the operands. |
| 3655 | const SCEV *FullS = Ops.empty() ? |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 3656 | SE.getConstant(IntTy, 0) : |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3657 | SE.getAddExpr(Ops); |
| 3658 | Value *FullV = Rewriter.expandCodeFor(FullS, Ty, IP); |
| 3659 | |
| 3660 | // We're done expanding now, so reset the rewriter. |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 3661 | Rewriter.clearPostInc(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3662 | |
| 3663 | // An ICmpZero Formula represents an ICmp which we're handling as a |
| 3664 | // comparison against zero. Now that we've expanded an expression for that |
| 3665 | // form, update the ICmp's other operand. |
| 3666 | if (LU.Kind == LSRUse::ICmpZero) { |
| 3667 | ICmpInst *CI = cast<ICmpInst>(LF.UserInst); |
| 3668 | DeadInsts.push_back(CI->getOperand(1)); |
| 3669 | assert(!F.AM.BaseGV && "ICmp does not support folding a global value and " |
| 3670 | "a scale at the same time!"); |
| 3671 | if (F.AM.Scale == -1) { |
| 3672 | if (ICmpScaledV->getType() != OpTy) { |
| 3673 | Instruction *Cast = |
| 3674 | CastInst::Create(CastInst::getCastOpcode(ICmpScaledV, false, |
| 3675 | OpTy, false), |
| 3676 | ICmpScaledV, OpTy, "tmp", CI); |
| 3677 | ICmpScaledV = Cast; |
| 3678 | } |
| 3679 | CI->setOperand(1, ICmpScaledV); |
| 3680 | } else { |
| 3681 | assert(F.AM.Scale == 0 && |
| 3682 | "ICmp does not support folding a global value and " |
| 3683 | "a scale at the same time!"); |
| 3684 | Constant *C = ConstantInt::getSigned(SE.getEffectiveSCEVType(OpTy), |
| 3685 | -(uint64_t)Offset); |
| 3686 | if (C->getType() != OpTy) |
| 3687 | C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 3688 | OpTy, false), |
| 3689 | C, OpTy); |
| 3690 | |
| 3691 | CI->setOperand(1, C); |
| 3692 | } |
| 3693 | } |
| 3694 | |
| 3695 | return FullV; |
| 3696 | } |
| 3697 | |
Dan Gohman | 3a02cbc | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 3698 | /// RewriteForPHI - Helper for Rewrite. PHI nodes are special because the use |
| 3699 | /// of their operands effectively happens in their predecessor blocks, so the |
| 3700 | /// expression may need to be expanded in multiple places. |
| 3701 | void LSRInstance::RewriteForPHI(PHINode *PN, |
| 3702 | const LSRFixup &LF, |
| 3703 | const Formula &F, |
Dan Gohman | 3a02cbc | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 3704 | SCEVExpander &Rewriter, |
| 3705 | SmallVectorImpl<WeakVH> &DeadInsts, |
Dan Gohman | 3a02cbc | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 3706 | Pass *P) const { |
| 3707 | DenseMap<BasicBlock *, Value *> Inserted; |
| 3708 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 3709 | if (PN->getIncomingValue(i) == LF.OperandValToReplace) { |
| 3710 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 3711 | |
| 3712 | // If this is a critical edge, split the edge so that we do not insert |
| 3713 | // the code on all predecessor/successor paths. We do this unless this |
| 3714 | // is the canonical backedge for this loop, which complicates post-inc |
| 3715 | // users. |
| 3716 | if (e != 1 && BB->getTerminator()->getNumSuccessors() > 1 && |
Dan Gohman | 3ef9838 | 2011-02-08 00:55:13 +0000 | [diff] [blame] | 3717 | !isa<IndirectBrInst>(BB->getTerminator())) { |
Bill Wendling | 89d4411 | 2011-08-25 01:08:34 +0000 | [diff] [blame] | 3718 | BasicBlock *Parent = PN->getParent(); |
| 3719 | Loop *PNLoop = LI.getLoopFor(Parent); |
| 3720 | if (!PNLoop || Parent != PNLoop->getHeader()) { |
Dan Gohman | 3ef9838 | 2011-02-08 00:55:13 +0000 | [diff] [blame] | 3721 | // Split the critical edge. |
Bill Wendling | 8b6af8a | 2011-08-25 05:55:40 +0000 | [diff] [blame] | 3722 | BasicBlock *NewBB = 0; |
| 3723 | if (!Parent->isLandingPad()) { |
Andrew Trick | f143b79 | 2011-10-04 03:50:44 +0000 | [diff] [blame] | 3724 | NewBB = SplitCriticalEdge(BB, Parent, P, |
| 3725 | /*MergeIdenticalEdges=*/true, |
| 3726 | /*DontDeleteUselessPhis=*/true); |
Bill Wendling | 8b6af8a | 2011-08-25 05:55:40 +0000 | [diff] [blame] | 3727 | } else { |
| 3728 | SmallVector<BasicBlock*, 2> NewBBs; |
| 3729 | SplitLandingPadPredecessors(Parent, BB, "", "", P, NewBBs); |
| 3730 | NewBB = NewBBs[0]; |
| 3731 | } |
Dan Gohman | 3a02cbc | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 3732 | |
Dan Gohman | 3ef9838 | 2011-02-08 00:55:13 +0000 | [diff] [blame] | 3733 | // If PN is outside of the loop and BB is in the loop, we want to |
| 3734 | // move the block to be immediately before the PHI block, not |
| 3735 | // immediately after BB. |
| 3736 | if (L->contains(BB) && !L->contains(PN)) |
| 3737 | NewBB->moveBefore(PN->getParent()); |
Dan Gohman | 3a02cbc | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 3738 | |
Dan Gohman | 3ef9838 | 2011-02-08 00:55:13 +0000 | [diff] [blame] | 3739 | // Splitting the edge can reduce the number of PHI entries we have. |
| 3740 | e = PN->getNumIncomingValues(); |
| 3741 | BB = NewBB; |
| 3742 | i = PN->getBasicBlockIndex(BB); |
| 3743 | } |
Dan Gohman | 3a02cbc | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> Pair = |
| 3747 | Inserted.insert(std::make_pair(BB, static_cast<Value *>(0))); |
| 3748 | if (!Pair.second) |
| 3749 | PN->setIncomingValue(i, Pair.first->second); |
| 3750 | else { |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 3751 | Value *FullV = Expand(LF, F, BB->getTerminator(), Rewriter, DeadInsts); |
Dan Gohman | 3a02cbc | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 3752 | |
| 3753 | // If this is reuse-by-noop-cast, insert the noop cast. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3754 | Type *OpTy = LF.OperandValToReplace->getType(); |
Dan Gohman | 3a02cbc | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 3755 | if (FullV->getType() != OpTy) |
| 3756 | FullV = |
| 3757 | CastInst::Create(CastInst::getCastOpcode(FullV, false, |
| 3758 | OpTy, false), |
| 3759 | FullV, LF.OperandValToReplace->getType(), |
| 3760 | "tmp", BB->getTerminator()); |
| 3761 | |
| 3762 | PN->setIncomingValue(i, FullV); |
| 3763 | Pair.first->second = FullV; |
| 3764 | } |
| 3765 | } |
| 3766 | } |
| 3767 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3768 | /// Rewrite - Emit instructions for the leading candidate expression for this |
| 3769 | /// LSRUse (this is called "expanding"), and update the UserInst to reference |
| 3770 | /// the newly expanded value. |
| 3771 | void LSRInstance::Rewrite(const LSRFixup &LF, |
| 3772 | const Formula &F, |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3773 | SCEVExpander &Rewriter, |
| 3774 | SmallVectorImpl<WeakVH> &DeadInsts, |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3775 | Pass *P) const { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3776 | // First, find an insertion point that dominates UserInst. For PHI nodes, |
| 3777 | // find the nearest block which dominates all the relevant uses. |
| 3778 | if (PHINode *PN = dyn_cast<PHINode>(LF.UserInst)) { |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 3779 | RewriteForPHI(PN, LF, F, Rewriter, DeadInsts, P); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3780 | } else { |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 3781 | Value *FullV = Expand(LF, F, LF.UserInst, Rewriter, DeadInsts); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3782 | |
| 3783 | // If this is reuse-by-noop-cast, insert the noop cast. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3784 | Type *OpTy = LF.OperandValToReplace->getType(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3785 | if (FullV->getType() != OpTy) { |
| 3786 | Instruction *Cast = |
| 3787 | CastInst::Create(CastInst::getCastOpcode(FullV, false, OpTy, false), |
| 3788 | FullV, OpTy, "tmp", LF.UserInst); |
| 3789 | FullV = Cast; |
| 3790 | } |
| 3791 | |
| 3792 | // Update the user. ICmpZero is handled specially here (for now) because |
| 3793 | // Expand may have updated one of the operands of the icmp already, and |
| 3794 | // its new value may happen to be equal to LF.OperandValToReplace, in |
| 3795 | // which case doing replaceUsesOfWith leads to replacing both operands |
| 3796 | // with the same value. TODO: Reorganize this. |
| 3797 | if (Uses[LF.LUIdx].Kind == LSRUse::ICmpZero) |
| 3798 | LF.UserInst->setOperand(0, FullV); |
| 3799 | else |
| 3800 | LF.UserInst->replaceUsesOfWith(LF.OperandValToReplace, FullV); |
| 3801 | } |
| 3802 | |
| 3803 | DeadInsts.push_back(LF.OperandValToReplace); |
| 3804 | } |
| 3805 | |
Dan Gohman | 76c315a | 2010-05-20 20:52:00 +0000 | [diff] [blame] | 3806 | /// ImplementSolution - Rewrite all the fixup locations with new values, |
| 3807 | /// following the chosen solution. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3808 | void |
| 3809 | LSRInstance::ImplementSolution(const SmallVectorImpl<const Formula *> &Solution, |
| 3810 | Pass *P) { |
| 3811 | // Keep track of instructions we may have made dead, so that |
| 3812 | // we can remove them after we are done working. |
| 3813 | SmallVector<WeakVH, 16> DeadInsts; |
| 3814 | |
Andrew Trick | 5e7645b | 2011-06-28 05:07:32 +0000 | [diff] [blame] | 3815 | SCEVExpander Rewriter(SE, "lsr"); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3816 | Rewriter.disableCanonicalMode(); |
Andrew Trick | c570191 | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 3817 | Rewriter.enableLSRMode(); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3818 | Rewriter.setIVIncInsertPos(L, IVIncInsertPos); |
| 3819 | |
| 3820 | // Expand the new value definitions and update the users. |
Dan Gohman | 402d435 | 2010-05-20 20:33:18 +0000 | [diff] [blame] | 3821 | for (SmallVectorImpl<LSRFixup>::const_iterator I = Fixups.begin(), |
| 3822 | E = Fixups.end(); I != E; ++I) { |
| 3823 | const LSRFixup &Fixup = *I; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3824 | |
Dan Gohman | 402d435 | 2010-05-20 20:33:18 +0000 | [diff] [blame] | 3825 | Rewrite(Fixup, *Solution[Fixup.LUIdx], Rewriter, DeadInsts, P); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3826 | |
| 3827 | Changed = true; |
| 3828 | } |
| 3829 | |
| 3830 | // Clean up after ourselves. This must be done before deleting any |
| 3831 | // instructions. |
| 3832 | Rewriter.clear(); |
| 3833 | |
| 3834 | Changed |= DeleteTriviallyDeadInstructions(DeadInsts); |
| 3835 | } |
| 3836 | |
| 3837 | LSRInstance::LSRInstance(const TargetLowering *tli, Loop *l, Pass *P) |
| 3838 | : IU(P->getAnalysis<IVUsers>()), |
| 3839 | SE(P->getAnalysis<ScalarEvolution>()), |
| 3840 | DT(P->getAnalysis<DominatorTree>()), |
Dan Gohman | e5f7687 | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 3841 | LI(P->getAnalysis<LoopInfo>()), |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3842 | TLI(tli), L(l), Changed(false), IVIncInsertPos(0) { |
Devang Patel | 0f54dcb | 2007-03-06 21:14:09 +0000 | [diff] [blame] | 3843 | |
Dan Gohman | 03e896b | 2009-11-05 21:11:53 +0000 | [diff] [blame] | 3844 | // If LoopSimplify form is not available, stay out of trouble. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3845 | if (!L->isLoopSimplifyForm()) return; |
Dan Gohman | 03e896b | 2009-11-05 21:11:53 +0000 | [diff] [blame] | 3846 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3847 | // If there's no interesting work to be done, bail early. |
| 3848 | if (IU.empty()) return; |
Dan Gohman | 80b0f8c | 2009-03-09 20:34:59 +0000 | [diff] [blame] | 3849 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3850 | DEBUG(dbgs() << "\nLSR on loop "; |
| 3851 | WriteAsOperand(dbgs(), L->getHeader(), /*PrintType=*/false); |
| 3852 | dbgs() << ":\n"); |
Dan Gohman | f7912df | 2009-03-09 20:46:50 +0000 | [diff] [blame] | 3853 | |
Dan Gohman | 402d435 | 2010-05-20 20:33:18 +0000 | [diff] [blame] | 3854 | // First, perform some low-level loop optimizations. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3855 | OptimizeShadowIV(); |
Dan Gohman | c6519f9 | 2010-05-20 20:05:31 +0000 | [diff] [blame] | 3856 | OptimizeLoopTermCond(); |
Evan Cheng | 5792f51 | 2009-05-11 22:33:01 +0000 | [diff] [blame] | 3857 | |
Andrew Trick | 37eb38d | 2011-07-21 00:40:04 +0000 | [diff] [blame] | 3858 | // If loop preparation eliminates all interesting IV users, bail. |
| 3859 | if (IU.empty()) return; |
| 3860 | |
Andrew Trick | 5219f86 | 2011-09-29 01:53:08 +0000 | [diff] [blame] | 3861 | // Skip nested loops until we can model them better with formulae. |
Andrew Trick | 0c01bc3 | 2011-09-29 01:33:38 +0000 | [diff] [blame] | 3862 | if (!EnableNested && !L->empty()) { |
| 3863 | DEBUG(dbgs() << "LSR skipping outer loop " << *L << "\n"); |
Andrew Trick | 5219f86 | 2011-09-29 01:53:08 +0000 | [diff] [blame] | 3864 | return; |
Andrew Trick | 0c01bc3 | 2011-09-29 01:33:38 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
Dan Gohman | 402d435 | 2010-05-20 20:33:18 +0000 | [diff] [blame] | 3867 | // Start collecting data and preparing for the solver. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3868 | CollectInterestingTypesAndFactors(); |
| 3869 | CollectFixupsAndInitialFormulae(); |
| 3870 | CollectLoopInvariantFixupsAndFormulae(); |
Chris Lattner | 010de25 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 3871 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3872 | DEBUG(dbgs() << "LSR found " << Uses.size() << " uses:\n"; |
| 3873 | print_uses(dbgs())); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3874 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3875 | // Now use the reuse data to generate a bunch of interesting ways |
| 3876 | // to formulate the values needed for the uses. |
| 3877 | GenerateAllReuseFormulae(); |
Evan Cheng | d1d6b5c | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 3878 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3879 | FilterOutUndesirableDedicatedRegisters(); |
| 3880 | NarrowSearchSpaceUsingHeuristics(); |
Dan Gohman | 6bec5bb | 2009-12-18 00:06:20 +0000 | [diff] [blame] | 3881 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3882 | SmallVector<const Formula *, 8> Solution; |
| 3883 | Solve(Solution); |
Dan Gohman | 6bec5bb | 2009-12-18 00:06:20 +0000 | [diff] [blame] | 3884 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3885 | // Release memory that is no longer needed. |
| 3886 | Factors.clear(); |
| 3887 | Types.clear(); |
| 3888 | RegUses.clear(); |
| 3889 | |
Andrew Trick | 80ef1b2 | 2011-09-27 00:44:14 +0000 | [diff] [blame] | 3890 | if (Solution.empty()) |
| 3891 | return; |
| 3892 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3893 | #ifndef NDEBUG |
| 3894 | // Formulae should be legal. |
| 3895 | for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(), |
| 3896 | E = Uses.end(); I != E; ++I) { |
| 3897 | const LSRUse &LU = *I; |
| 3898 | for (SmallVectorImpl<Formula>::const_iterator J = LU.Formulae.begin(), |
| 3899 | JE = LU.Formulae.end(); J != JE; ++J) |
| 3900 | assert(isLegalUse(J->AM, LU.MinOffset, LU.MaxOffset, |
| 3901 | LU.Kind, LU.AccessTy, TLI) && |
| 3902 | "Illegal formula generated!"); |
| 3903 | }; |
| 3904 | #endif |
| 3905 | |
| 3906 | // Now that we've decided what we want, make it so. |
| 3907 | ImplementSolution(Solution, P); |
| 3908 | } |
| 3909 | |
| 3910 | void LSRInstance::print_factors_and_types(raw_ostream &OS) const { |
| 3911 | if (Factors.empty() && Types.empty()) return; |
| 3912 | |
| 3913 | OS << "LSR has identified the following interesting factors and types: "; |
| 3914 | bool First = true; |
| 3915 | |
| 3916 | for (SmallSetVector<int64_t, 8>::const_iterator |
| 3917 | I = Factors.begin(), E = Factors.end(); I != E; ++I) { |
| 3918 | if (!First) OS << ", "; |
| 3919 | First = false; |
| 3920 | OS << '*' << *I; |
Evan Cheng | 81ebdcf | 2009-11-10 21:14:05 +0000 | [diff] [blame] | 3921 | } |
Dale Johannesen | c1acc3f | 2009-05-11 17:15:42 +0000 | [diff] [blame] | 3922 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3923 | for (SmallSetVector<Type *, 4>::const_iterator |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3924 | I = Types.begin(), E = Types.end(); I != E; ++I) { |
| 3925 | if (!First) OS << ", "; |
| 3926 | First = false; |
| 3927 | OS << '(' << **I << ')'; |
| 3928 | } |
| 3929 | OS << '\n'; |
| 3930 | } |
| 3931 | |
| 3932 | void LSRInstance::print_fixups(raw_ostream &OS) const { |
| 3933 | OS << "LSR is examining the following fixup sites:\n"; |
| 3934 | for (SmallVectorImpl<LSRFixup>::const_iterator I = Fixups.begin(), |
| 3935 | E = Fixups.end(); I != E; ++I) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3936 | dbgs() << " "; |
Dan Gohman | 9f383eb | 2010-05-20 22:25:20 +0000 | [diff] [blame] | 3937 | I->print(OS); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3938 | OS << '\n'; |
| 3939 | } |
| 3940 | } |
| 3941 | |
| 3942 | void LSRInstance::print_uses(raw_ostream &OS) const { |
| 3943 | OS << "LSR is examining the following uses:\n"; |
| 3944 | for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(), |
| 3945 | E = Uses.end(); I != E; ++I) { |
| 3946 | const LSRUse &LU = *I; |
| 3947 | dbgs() << " "; |
| 3948 | LU.print(OS); |
| 3949 | OS << '\n'; |
| 3950 | for (SmallVectorImpl<Formula>::const_iterator J = LU.Formulae.begin(), |
| 3951 | JE = LU.Formulae.end(); J != JE; ++J) { |
| 3952 | OS << " "; |
| 3953 | J->print(OS); |
| 3954 | OS << '\n'; |
| 3955 | } |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | void LSRInstance::print(raw_ostream &OS) const { |
| 3960 | print_factors_and_types(OS); |
| 3961 | print_fixups(OS); |
| 3962 | print_uses(OS); |
| 3963 | } |
| 3964 | |
| 3965 | void LSRInstance::dump() const { |
| 3966 | print(errs()); errs() << '\n'; |
| 3967 | } |
| 3968 | |
| 3969 | namespace { |
| 3970 | |
| 3971 | class LoopStrengthReduce : public LoopPass { |
| 3972 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 3973 | /// transformation profitability. |
| 3974 | const TargetLowering *const TLI; |
| 3975 | |
| 3976 | public: |
| 3977 | static char ID; // Pass ID, replacement for typeid |
| 3978 | explicit LoopStrengthReduce(const TargetLowering *tli = 0); |
| 3979 | |
| 3980 | private: |
| 3981 | bool runOnLoop(Loop *L, LPPassManager &LPM); |
| 3982 | void getAnalysisUsage(AnalysisUsage &AU) const; |
| 3983 | }; |
| 3984 | |
| 3985 | } |
| 3986 | |
| 3987 | char LoopStrengthReduce::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 3988 | INITIALIZE_PASS_BEGIN(LoopStrengthReduce, "loop-reduce", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 3989 | "Loop Strength Reduction", false, false) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 3990 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 3991 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 3992 | INITIALIZE_PASS_DEPENDENCY(IVUsers) |
Owen Anderson | 205942a | 2010-10-19 20:08:44 +0000 | [diff] [blame] | 3993 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 3994 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 3995 | INITIALIZE_PASS_END(LoopStrengthReduce, "loop-reduce", |
| 3996 | "Loop Strength Reduction", false, false) |
| 3997 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3998 | |
| 3999 | Pass *llvm::createLoopStrengthReducePass(const TargetLowering *TLI) { |
| 4000 | return new LoopStrengthReduce(TLI); |
| 4001 | } |
| 4002 | |
| 4003 | LoopStrengthReduce::LoopStrengthReduce(const TargetLowering *tli) |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 4004 | : LoopPass(ID), TLI(tli) { |
| 4005 | initializeLoopStrengthReducePass(*PassRegistry::getPassRegistry()); |
| 4006 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4007 | |
| 4008 | void LoopStrengthReduce::getAnalysisUsage(AnalysisUsage &AU) const { |
| 4009 | // We split critical edges, so we change the CFG. However, we do update |
| 4010 | // many analyses if they are around. |
Eric Christopher | 6793c49 | 2011-02-10 01:48:24 +0000 | [diff] [blame] | 4011 | AU.addPreservedID(LoopSimplifyID); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4012 | |
Eric Christopher | 6793c49 | 2011-02-10 01:48:24 +0000 | [diff] [blame] | 4013 | AU.addRequired<LoopInfo>(); |
| 4014 | AU.addPreserved<LoopInfo>(); |
| 4015 | AU.addRequiredID(LoopSimplifyID); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4016 | AU.addRequired<DominatorTree>(); |
| 4017 | AU.addPreserved<DominatorTree>(); |
| 4018 | AU.addRequired<ScalarEvolution>(); |
| 4019 | AU.addPreserved<ScalarEvolution>(); |
Cameron Zwarich | 2c2b933 | 2011-02-10 23:53:14 +0000 | [diff] [blame] | 4020 | // Requiring LoopSimplify a second time here prevents IVUsers from running |
| 4021 | // twice, since LoopSimplify was invalidated by running ScalarEvolution. |
| 4022 | AU.addRequiredID(LoopSimplifyID); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4023 | AU.addRequired<IVUsers>(); |
| 4024 | AU.addPreserved<IVUsers>(); |
| 4025 | } |
| 4026 | |
| 4027 | bool LoopStrengthReduce::runOnLoop(Loop *L, LPPassManager & /*LPM*/) { |
| 4028 | bool Changed = false; |
| 4029 | |
| 4030 | // Run the main LSR transformation. |
| 4031 | Changed |= LSRInstance(TLI, L, this).getChanged(); |
| 4032 | |
Andrew Trick | f231a6d | 2012-01-07 01:36:44 +0000 | [diff] [blame^] | 4033 | // Remove any extra phis created by processing inner loops. |
Dan Gohman | 9fff218 | 2010-01-05 16:31:45 +0000 | [diff] [blame] | 4034 | Changed |= DeleteDeadPHIs(L->getHeader()); |
Andrew Trick | f231a6d | 2012-01-07 01:36:44 +0000 | [diff] [blame^] | 4035 | if (EnablePhiElim) { |
| 4036 | SmallVector<WeakVH, 16> DeadInsts; |
| 4037 | SCEVExpander Rewriter(getAnalysis<ScalarEvolution>(), "lsr"); |
| 4038 | #ifndef NDEBUG |
| 4039 | Rewriter.setDebugType(DEBUG_TYPE); |
| 4040 | #endif |
| 4041 | unsigned numFolded = Rewriter. |
| 4042 | replaceCongruentIVs(L, &getAnalysis<DominatorTree>(), DeadInsts, TLI); |
| 4043 | if (numFolded) { |
| 4044 | Changed = true; |
| 4045 | DeleteTriviallyDeadInstructions(DeadInsts); |
| 4046 | DeleteDeadPHIs(L->getHeader()); |
| 4047 | } |
| 4048 | } |
Evan Cheng | 1ce75dc | 2008-07-07 19:51:32 +0000 | [diff] [blame] | 4049 | return Changed; |
Nate Begeman | eaa1385 | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 4050 | } |