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