Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1 | //===- LoopStrengthReduce.cpp - Strength Reduce IVs in Loops --------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Dan Gohman | 97f70ad | 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 | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 14 | // This pass performs a strength reduction on array references inside loops that |
Dan Gohman | 97f70ad | 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 | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 19 | // |
Dan Gohman | 45774ce | 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 |
Sanjoy Das | 7041fb1 | 2015-03-27 06:01:56 +0000 | [diff] [blame] | 31 | // the value of the register before the add and some using it after. In this |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 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 | 26c59fa | 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 | 45774ce | 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 | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
| 55 | |
Dehao Chen | 6132ee8 | 2016-07-18 21:41:50 +0000 | [diff] [blame] | 56 | #include "llvm/Transforms/Scalar/LoopStrengthReduce.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/APInt.h" |
| 58 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | 62fb0cf | 2014-03-15 17:17:48 +0000 | [diff] [blame] | 60 | #include "llvm/ADT/Hashing.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/PointerIntPair.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 62 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 63 | #include "llvm/ADT/SetVector.h" |
| 64 | #include "llvm/ADT/SmallBitVector.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 65 | #include "llvm/ADT/SmallPtrSet.h" |
| 66 | #include "llvm/ADT/SmallSet.h" |
| 67 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 68 | #include "llvm/Analysis/IVUsers.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 69 | #include "llvm/Analysis/LoopInfo.h" |
Devang Patel | b0743b5 | 2007-03-06 21:14:09 +0000 | [diff] [blame] | 70 | #include "llvm/Analysis/LoopPass.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 71 | #include "llvm/Analysis/ScalarEvolution.h" |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 72 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 73 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 74 | #include "llvm/Analysis/ScalarEvolutionNormalization.h" |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 75 | #include "llvm/Analysis/TargetTransformInfo.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 76 | #include "llvm/IR/BasicBlock.h" |
| 77 | #include "llvm/IR/Constant.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 78 | #include "llvm/IR/Constants.h" |
| 79 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 80 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 81 | #include "llvm/IR/GlobalValue.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 82 | #include "llvm/IR/IRBuilder.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 83 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 84 | #include "llvm/IR/Instructions.h" |
| 85 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 86 | #include "llvm/IR/Module.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 87 | #include "llvm/IR/OperandTraits.h" |
| 88 | #include "llvm/IR/Operator.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 89 | #include "llvm/IR/Type.h" |
| 90 | #include "llvm/IR/Value.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 91 | #include "llvm/IR/ValueHandle.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 92 | #include "llvm/Pass.h" |
| 93 | #include "llvm/Support/Casting.h" |
Andrew Trick | 5812439 | 2011-09-27 00:44:14 +0000 | [diff] [blame] | 94 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 95 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 96 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 97 | #include "llvm/Support/ErrorHandling.h" |
| 98 | #include "llvm/Support/MathExtras.h" |
Daniel Dunbar | 6115b39 | 2009-07-26 09:48:23 +0000 | [diff] [blame] | 99 | #include "llvm/Support/raw_ostream.h" |
Dehao Chen | 6132ee8 | 2016-07-18 21:41:50 +0000 | [diff] [blame] | 100 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 101 | #include "llvm/Transforms/Scalar/LoopPassManager.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 102 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 103 | #include "llvm/Transforms/Utils/Local.h" |
Jeff Cohen | c500991 | 2005-07-30 18:22:27 +0000 | [diff] [blame] | 104 | #include <algorithm> |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 105 | #include <cassert> |
| 106 | #include <cstddef> |
| 107 | #include <cstdint> |
| 108 | #include <cstdlib> |
| 109 | #include <iterator> |
| 110 | #include <map> |
| 111 | #include <tuple> |
| 112 | #include <utility> |
| 113 | |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 114 | using namespace llvm; |
| 115 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 116 | #define DEBUG_TYPE "loop-reduce" |
| 117 | |
Andrew Trick | 19f80c1 | 2012-04-18 04:00:10 +0000 | [diff] [blame] | 118 | /// MaxIVUsers is an arbitrary threshold that provides an early opportunitiy for |
| 119 | /// bail out. This threshold is far beyond the number of users that LSR can |
| 120 | /// conceivably solve, so it should not affect generated code, but catches the |
| 121 | /// worst cases before LSR burns too much compile time and stack space. |
| 122 | static const unsigned MaxIVUsers = 200; |
| 123 | |
Andrew Trick | ecbe22b | 2011-10-11 02:30:45 +0000 | [diff] [blame] | 124 | // Temporary flag to cleanup congruent phis after LSR phi expansion. |
| 125 | // It's currently disabled until we can determine whether it's truly useful or |
| 126 | // not. The flag should be removed after the v3.0 release. |
Andrew Trick | 06f6c05 | 2012-01-07 07:08:17 +0000 | [diff] [blame] | 127 | // This is now needed for ivchains. |
Benjamin Kramer | 7ba71be | 2011-11-26 23:01:57 +0000 | [diff] [blame] | 128 | static cl::opt<bool> EnablePhiElim( |
Andrew Trick | 06f6c05 | 2012-01-07 07:08:17 +0000 | [diff] [blame] | 129 | "enable-lsr-phielim", cl::Hidden, cl::init(true), |
| 130 | cl::desc("Enable LSR phi elimination")); |
Andrew Trick | 5812439 | 2011-09-27 00:44:14 +0000 | [diff] [blame] | 131 | |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 132 | #ifndef NDEBUG |
| 133 | // Stress test IV chain generation. |
| 134 | static cl::opt<bool> StressIVChain( |
| 135 | "stress-ivchain", cl::Hidden, cl::init(false), |
| 136 | cl::desc("Stress test LSR IV chains")); |
| 137 | #else |
| 138 | static bool StressIVChain = false; |
| 139 | #endif |
| 140 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 141 | namespace { |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 142 | |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 143 | struct MemAccessTy { |
| 144 | /// Used in situations where the accessed memory type is unknown. |
| 145 | static const unsigned UnknownAddressSpace = ~0u; |
| 146 | |
| 147 | Type *MemTy; |
| 148 | unsigned AddrSpace; |
| 149 | |
| 150 | MemAccessTy() : MemTy(nullptr), AddrSpace(UnknownAddressSpace) {} |
| 151 | |
| 152 | MemAccessTy(Type *Ty, unsigned AS) : |
| 153 | MemTy(Ty), AddrSpace(AS) {} |
| 154 | |
| 155 | bool operator==(MemAccessTy Other) const { |
| 156 | return MemTy == Other.MemTy && AddrSpace == Other.AddrSpace; |
| 157 | } |
| 158 | |
| 159 | bool operator!=(MemAccessTy Other) const { return !(*this == Other); } |
| 160 | |
| 161 | static MemAccessTy getUnknown(LLVMContext &Ctx) { |
| 162 | return MemAccessTy(Type::getVoidTy(Ctx), UnknownAddressSpace); |
| 163 | } |
| 164 | }; |
| 165 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 166 | /// This class holds data which is used to order reuse candidates. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 167 | class RegSortData { |
| 168 | public: |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 169 | /// This represents the set of LSRUse indices which reference |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 170 | /// a particular register. |
| 171 | SmallBitVector UsedByIndices; |
| 172 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 173 | void print(raw_ostream &OS) const; |
| 174 | void dump() const; |
| 175 | }; |
| 176 | |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 177 | } // end anonymous namespace |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 178 | |
| 179 | void RegSortData::print(raw_ostream &OS) const { |
| 180 | OS << "[NumUses=" << UsedByIndices.count() << ']'; |
| 181 | } |
| 182 | |
Davide Italiano | 945d05f | 2015-11-23 02:47:30 +0000 | [diff] [blame] | 183 | LLVM_DUMP_METHOD |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 184 | void RegSortData::dump() const { |
| 185 | print(errs()); errs() << '\n'; |
| 186 | } |
Dan Gohman | 2a12ae7 | 2009-02-20 04:17:46 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 188 | namespace { |
Dale Johannesen | e3a02be | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 189 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 190 | /// Map register candidates to information about how they are used. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 191 | class RegUseTracker { |
| 192 | typedef DenseMap<const SCEV *, RegSortData> RegUsesTy; |
Dale Johannesen | e3a02be | 2007-03-20 00:47:50 +0000 | [diff] [blame] | 193 | |
Dan Gohman | 248c41d | 2010-05-18 22:33:00 +0000 | [diff] [blame] | 194 | RegUsesTy RegUsesMap; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 195 | SmallVector<const SCEV *, 16> RegSequence; |
Evan Cheng | 3df447d | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 196 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 197 | public: |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 198 | void countRegister(const SCEV *Reg, size_t LUIdx); |
| 199 | void dropRegister(const SCEV *Reg, size_t LUIdx); |
| 200 | void swapAndDropUse(size_t LUIdx, size_t LastLUIdx); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 201 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 202 | bool isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const; |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 203 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 204 | const SmallBitVector &getUsedByIndices(const SCEV *Reg) const; |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 205 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 206 | void clear(); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 207 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 208 | typedef SmallVectorImpl<const SCEV *>::iterator iterator; |
| 209 | typedef SmallVectorImpl<const SCEV *>::const_iterator const_iterator; |
| 210 | iterator begin() { return RegSequence.begin(); } |
| 211 | iterator end() { return RegSequence.end(); } |
| 212 | const_iterator begin() const { return RegSequence.begin(); } |
| 213 | const_iterator end() const { return RegSequence.end(); } |
| 214 | }; |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 215 | |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 216 | } // end anonymous namespace |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 217 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 218 | void |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 219 | RegUseTracker::countRegister(const SCEV *Reg, size_t LUIdx) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 220 | std::pair<RegUsesTy::iterator, bool> Pair = |
Dan Gohman | 248c41d | 2010-05-18 22:33:00 +0000 | [diff] [blame] | 221 | RegUsesMap.insert(std::make_pair(Reg, RegSortData())); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 222 | RegSortData &RSD = Pair.first->second; |
| 223 | if (Pair.second) |
| 224 | RegSequence.push_back(Reg); |
| 225 | RSD.UsedByIndices.resize(std::max(RSD.UsedByIndices.size(), LUIdx + 1)); |
| 226 | RSD.UsedByIndices.set(LUIdx); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Dan Gohman | 4cf99b5 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 229 | void |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 230 | RegUseTracker::dropRegister(const SCEV *Reg, size_t LUIdx) { |
Dan Gohman | 4cf99b5 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 231 | RegUsesTy::iterator It = RegUsesMap.find(Reg); |
| 232 | assert(It != RegUsesMap.end()); |
| 233 | RegSortData &RSD = It->second; |
| 234 | assert(RSD.UsedByIndices.size() > LUIdx); |
| 235 | RSD.UsedByIndices.reset(LUIdx); |
| 236 | } |
| 237 | |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 238 | void |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 239 | RegUseTracker::swapAndDropUse(size_t LUIdx, size_t LastLUIdx) { |
Dan Gohman | a7b68d6 | 2010-10-07 23:33:43 +0000 | [diff] [blame] | 240 | assert(LUIdx <= LastLUIdx); |
| 241 | |
| 242 | // Update RegUses. The data structure is not optimized for this purpose; |
| 243 | // we must iterate through it and update each of the bit vectors. |
Craig Topper | 10949ae | 2015-05-23 08:45:10 +0000 | [diff] [blame] | 244 | for (auto &Pair : RegUsesMap) { |
| 245 | SmallBitVector &UsedByIndices = Pair.second.UsedByIndices; |
Dan Gohman | a7b68d6 | 2010-10-07 23:33:43 +0000 | [diff] [blame] | 246 | if (LUIdx < UsedByIndices.size()) |
| 247 | UsedByIndices[LUIdx] = |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 248 | LastLUIdx < UsedByIndices.size() ? UsedByIndices[LastLUIdx] : false; |
Dan Gohman | a7b68d6 | 2010-10-07 23:33:43 +0000 | [diff] [blame] | 249 | UsedByIndices.resize(std::min(UsedByIndices.size(), LastLUIdx)); |
| 250 | } |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 253 | bool |
| 254 | RegUseTracker::isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const { |
Dan Gohman | 4f13bbf | 2010-08-29 15:18:49 +0000 | [diff] [blame] | 255 | RegUsesTy::const_iterator I = RegUsesMap.find(Reg); |
| 256 | if (I == RegUsesMap.end()) |
| 257 | return false; |
| 258 | const SmallBitVector &UsedByIndices = I->second.UsedByIndices; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 259 | int i = UsedByIndices.find_first(); |
| 260 | if (i == -1) return false; |
| 261 | if ((size_t)i != LUIdx) return true; |
| 262 | return UsedByIndices.find_next(i) != -1; |
| 263 | } |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 264 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 265 | const SmallBitVector &RegUseTracker::getUsedByIndices(const SCEV *Reg) const { |
Dan Gohman | 248c41d | 2010-05-18 22:33:00 +0000 | [diff] [blame] | 266 | RegUsesTy::const_iterator I = RegUsesMap.find(Reg); |
| 267 | assert(I != RegUsesMap.end() && "Unknown register!"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 268 | return I->second.UsedByIndices; |
| 269 | } |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 270 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 271 | void RegUseTracker::clear() { |
Dan Gohman | 248c41d | 2010-05-18 22:33:00 +0000 | [diff] [blame] | 272 | RegUsesMap.clear(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 273 | RegSequence.clear(); |
| 274 | } |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 275 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 276 | namespace { |
| 277 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 278 | /// This class holds information that describes a formula for computing |
| 279 | /// satisfying a use. It may include broken-out immediates and scaled registers. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 280 | struct Formula { |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 281 | /// Global base address used for complex addressing. |
| 282 | GlobalValue *BaseGV; |
| 283 | |
| 284 | /// Base offset for complex addressing. |
| 285 | int64_t BaseOffset; |
| 286 | |
| 287 | /// Whether any complex addressing has a base register. |
| 288 | bool HasBaseReg; |
| 289 | |
| 290 | /// The scale of any complex addressing. |
| 291 | int64_t Scale; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 292 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 293 | /// The list of "base" registers for this use. When this is non-empty. The |
| 294 | /// canonical representation of a formula is |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 295 | /// 1. BaseRegs.size > 1 implies ScaledReg != NULL and |
| 296 | /// 2. ScaledReg != NULL implies Scale != 1 || !BaseRegs.empty(). |
| 297 | /// #1 enforces that the scaled register is always used when at least two |
| 298 | /// registers are needed by the formula: e.g., reg1 + reg2 is reg1 + 1 * reg2. |
| 299 | /// #2 enforces that 1 * reg is reg. |
| 300 | /// This invariant can be temporarly broken while building a formula. |
| 301 | /// However, every formula inserted into the LSRInstance must be in canonical |
| 302 | /// form. |
Preston Gurd | 25c3b6a | 2013-02-01 20:41:27 +0000 | [diff] [blame] | 303 | SmallVector<const SCEV *, 4> BaseRegs; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 304 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 305 | /// The 'scaled' register for this use. This should be non-null when Scale is |
| 306 | /// not zero. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 307 | const SCEV *ScaledReg; |
| 308 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 309 | /// An additional constant offset which added near the use. This requires a |
| 310 | /// temporary register, but the offset itself can live in an add immediate |
| 311 | /// field rather than a register. |
Dan Gohman | 6136e94 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 312 | int64_t UnfoldedOffset; |
| 313 | |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 314 | Formula() |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 315 | : BaseGV(nullptr), BaseOffset(0), HasBaseReg(false), Scale(0), |
Sanjoy Das | 215df9e | 2015-08-04 01:52:05 +0000 | [diff] [blame] | 316 | ScaledReg(nullptr), UnfoldedOffset(0) {} |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 317 | |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 318 | void initialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 319 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 320 | bool isCanonical() const; |
| 321 | |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 322 | void canonicalize(); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 323 | |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 324 | bool unscale(); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 325 | |
Adam Nemet | deab6f9 | 2014-04-29 18:25:28 +0000 | [diff] [blame] | 326 | size_t getNumRegs() const; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 327 | Type *getType() const; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 328 | |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 329 | void deleteBaseReg(const SCEV *&S); |
Dan Gohman | 80a9608 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 330 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 331 | bool referencesReg(const SCEV *S) const; |
| 332 | bool hasRegsUsedByUsesOtherThan(size_t LUIdx, |
| 333 | const RegUseTracker &RegUses) const; |
| 334 | |
| 335 | void print(raw_ostream &OS) const; |
| 336 | void dump() const; |
| 337 | }; |
| 338 | |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 339 | } // end anonymous namespace |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 340 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 341 | /// Recursion helper for initialMatch. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 342 | static void DoInitialMatch(const SCEV *S, Loop *L, |
| 343 | SmallVectorImpl<const SCEV *> &Good, |
| 344 | SmallVectorImpl<const SCEV *> &Bad, |
Dan Gohman | 20d9ce2 | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 345 | ScalarEvolution &SE) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 346 | // Collect expressions which properly dominate the loop header. |
Dan Gohman | 20d9ce2 | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 347 | if (SE.properlyDominates(S, L->getHeader())) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 348 | Good.push_back(S); |
| 349 | return; |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 350 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 351 | |
| 352 | // Look at add operands. |
| 353 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 354 | for (const SCEV *S : Add->operands()) |
| 355 | DoInitialMatch(S, L, Good, Bad, SE); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 356 | return; |
| 357 | } |
| 358 | |
| 359 | // Look at addrec operands. |
| 360 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) |
Alexandros Lamprineas | 0ee3ec2 | 2016-11-09 08:53:07 +0000 | [diff] [blame] | 361 | if (!AR->getStart()->isZero() && AR->isAffine()) { |
Dan Gohman | 20d9ce2 | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 362 | DoInitialMatch(AR->getStart(), L, Good, Bad, SE); |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 363 | DoInitialMatch(SE.getAddRecExpr(SE.getConstant(AR->getType(), 0), |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 364 | AR->getStepRecurrence(SE), |
Andrew Trick | 8b55b73 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 365 | // FIXME: AR->getNoWrapFlags() |
| 366 | AR->getLoop(), SCEV::FlagAnyWrap), |
Dan Gohman | 20d9ce2 | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 367 | L, Good, Bad, SE); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 368 | return; |
| 369 | } |
| 370 | |
| 371 | // Handle a multiplication by -1 (negation) if it didn't fold. |
| 372 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) |
| 373 | if (Mul->getOperand(0)->isAllOnesValue()) { |
| 374 | SmallVector<const SCEV *, 4> Ops(Mul->op_begin()+1, Mul->op_end()); |
| 375 | const SCEV *NewMul = SE.getMulExpr(Ops); |
| 376 | |
| 377 | SmallVector<const SCEV *, 4> MyGood; |
| 378 | SmallVector<const SCEV *, 4> MyBad; |
Dan Gohman | 20d9ce2 | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 379 | DoInitialMatch(NewMul, L, MyGood, MyBad, SE); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 380 | const SCEV *NegOne = SE.getSCEV(ConstantInt::getAllOnesValue( |
| 381 | SE.getEffectiveSCEVType(NewMul->getType()))); |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 382 | for (const SCEV *S : MyGood) |
| 383 | Good.push_back(SE.getMulExpr(NegOne, S)); |
| 384 | for (const SCEV *S : MyBad) |
| 385 | Bad.push_back(SE.getMulExpr(NegOne, S)); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | |
| 389 | // Ok, we can't do anything interesting. Just stuff the whole thing into a |
| 390 | // register and hope for the best. |
| 391 | Bad.push_back(S); |
| 392 | } |
| 393 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 394 | /// Incorporate loop-variant parts of S into this Formula, attempting to keep |
| 395 | /// all loop-invariant and loop-computable values in a single base register. |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 396 | void Formula::initialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 397 | SmallVector<const SCEV *, 4> Good; |
| 398 | SmallVector<const SCEV *, 4> Bad; |
Dan Gohman | 20d9ce2 | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 399 | DoInitialMatch(S, L, Good, Bad, SE); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 400 | if (!Good.empty()) { |
Dan Gohman | 9b5d0bb7 | 2010-04-08 23:36:27 +0000 | [diff] [blame] | 401 | const SCEV *Sum = SE.getAddExpr(Good); |
| 402 | if (!Sum->isZero()) |
| 403 | BaseRegs.push_back(Sum); |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 404 | HasBaseReg = true; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 405 | } |
| 406 | if (!Bad.empty()) { |
Dan Gohman | 9b5d0bb7 | 2010-04-08 23:36:27 +0000 | [diff] [blame] | 407 | const SCEV *Sum = SE.getAddExpr(Bad); |
| 408 | if (!Sum->isZero()) |
| 409 | BaseRegs.push_back(Sum); |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 410 | HasBaseReg = true; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 411 | } |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 412 | canonicalize(); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /// \brief Check whether or not this formula statisfies the canonical |
| 416 | /// representation. |
| 417 | /// \see Formula::BaseRegs. |
| 418 | bool Formula::isCanonical() const { |
| 419 | if (ScaledReg) |
| 420 | return Scale != 1 || !BaseRegs.empty(); |
| 421 | return BaseRegs.size() <= 1; |
| 422 | } |
| 423 | |
| 424 | /// \brief Helper method to morph a formula into its canonical representation. |
| 425 | /// \see Formula::BaseRegs. |
| 426 | /// Every formula having more than one base register, must use the ScaledReg |
| 427 | /// field. Otherwise, we would have to do special cases everywhere in LSR |
| 428 | /// to treat reg1 + reg2 + ... the same way as reg1 + 1*reg2 + ... |
| 429 | /// On the other hand, 1*reg should be canonicalized into reg. |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 430 | void Formula::canonicalize() { |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 431 | if (isCanonical()) |
| 432 | return; |
| 433 | // So far we did not need this case. This is easy to implement but it is |
| 434 | // useless to maintain dead code. Beside it could hurt compile time. |
| 435 | assert(!BaseRegs.empty() && "1*reg => reg, should not be needed."); |
| 436 | // Keep the invariant sum in BaseRegs and one of the variant sum in ScaledReg. |
| 437 | ScaledReg = BaseRegs.back(); |
| 438 | BaseRegs.pop_back(); |
| 439 | Scale = 1; |
| 440 | size_t BaseRegsSize = BaseRegs.size(); |
| 441 | size_t Try = 0; |
| 442 | // If ScaledReg is an invariant, try to find a variant expression. |
| 443 | while (Try < BaseRegsSize && !isa<SCEVAddRecExpr>(ScaledReg)) |
| 444 | std::swap(ScaledReg, BaseRegs[Try++]); |
| 445 | } |
| 446 | |
| 447 | /// \brief Get rid of the scale in the formula. |
| 448 | /// In other words, this method morphes reg1 + 1*reg2 into reg1 + reg2. |
| 449 | /// \return true if it was possible to get rid of the scale, false otherwise. |
| 450 | /// \note After this operation the formula may not be in the canonical form. |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 451 | bool Formula::unscale() { |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 452 | if (Scale != 1) |
| 453 | return false; |
| 454 | Scale = 0; |
| 455 | BaseRegs.push_back(ScaledReg); |
| 456 | ScaledReg = nullptr; |
| 457 | return true; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 460 | /// Return the total number of register operands used by this formula. This does |
| 461 | /// not include register uses implied by non-constant addrec strides. |
Adam Nemet | deab6f9 | 2014-04-29 18:25:28 +0000 | [diff] [blame] | 462 | size_t Formula::getNumRegs() const { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 463 | return !!ScaledReg + BaseRegs.size(); |
| 464 | } |
| 465 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 466 | /// Return the type of this formula, if it has one, or null otherwise. This type |
| 467 | /// is meaningless except for the bit size. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 468 | Type *Formula::getType() const { |
Sanjoy Das | 215df9e | 2015-08-04 01:52:05 +0000 | [diff] [blame] | 469 | return !BaseRegs.empty() ? BaseRegs.front()->getType() : |
| 470 | ScaledReg ? ScaledReg->getType() : |
| 471 | BaseGV ? BaseGV->getType() : |
| 472 | nullptr; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 475 | /// Delete the given base reg from the BaseRegs list. |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 476 | void Formula::deleteBaseReg(const SCEV *&S) { |
Dan Gohman | 80a9608 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 477 | if (&S != &BaseRegs.back()) |
| 478 | std::swap(S, BaseRegs.back()); |
| 479 | BaseRegs.pop_back(); |
| 480 | } |
| 481 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 482 | /// Test if this formula references the given register. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 483 | bool Formula::referencesReg(const SCEV *S) const { |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 484 | return S == ScaledReg || is_contained(BaseRegs, S); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 487 | /// Test whether this formula uses registers which are used by uses other than |
| 488 | /// the use with the given index. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 489 | bool Formula::hasRegsUsedByUsesOtherThan(size_t LUIdx, |
| 490 | const RegUseTracker &RegUses) const { |
| 491 | if (ScaledReg) |
| 492 | if (RegUses.isRegUsedByUsesOtherThan(ScaledReg, LUIdx)) |
| 493 | return true; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 494 | for (const SCEV *BaseReg : BaseRegs) |
| 495 | if (RegUses.isRegUsedByUsesOtherThan(BaseReg, LUIdx)) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 496 | return true; |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | void Formula::print(raw_ostream &OS) const { |
| 501 | bool First = true; |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 502 | if (BaseGV) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 503 | if (!First) OS << " + "; else First = false; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 504 | BaseGV->printAsOperand(OS, /*PrintType=*/false); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 505 | } |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 506 | if (BaseOffset != 0) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 507 | if (!First) OS << " + "; else First = false; |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 508 | OS << BaseOffset; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 509 | } |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 510 | for (const SCEV *BaseReg : BaseRegs) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 511 | if (!First) OS << " + "; else First = false; |
Sanjoy Das | 215df9e | 2015-08-04 01:52:05 +0000 | [diff] [blame] | 512 | OS << "reg(" << *BaseReg << ')'; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 513 | } |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 514 | if (HasBaseReg && BaseRegs.empty()) { |
Dan Gohman | 06ab08f | 2010-05-18 22:35:55 +0000 | [diff] [blame] | 515 | if (!First) OS << " + "; else First = false; |
| 516 | OS << "**error: HasBaseReg**"; |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 517 | } else if (!HasBaseReg && !BaseRegs.empty()) { |
Dan Gohman | 06ab08f | 2010-05-18 22:35:55 +0000 | [diff] [blame] | 518 | if (!First) OS << " + "; else First = false; |
| 519 | OS << "**error: !HasBaseReg**"; |
| 520 | } |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 521 | if (Scale != 0) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 522 | if (!First) OS << " + "; else First = false; |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 523 | OS << Scale << "*reg("; |
Sanjoy Das | 215df9e | 2015-08-04 01:52:05 +0000 | [diff] [blame] | 524 | if (ScaledReg) |
| 525 | OS << *ScaledReg; |
| 526 | else |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 527 | OS << "<unknown>"; |
| 528 | OS << ')'; |
| 529 | } |
Dan Gohman | 6136e94 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 530 | if (UnfoldedOffset != 0) { |
Arnaud A. de Grandmaison | 75c9e6d | 2014-03-15 22:13:15 +0000 | [diff] [blame] | 531 | if (!First) OS << " + "; |
Dan Gohman | 6136e94 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 532 | OS << "imm(" << UnfoldedOffset << ')'; |
| 533 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Davide Italiano | 945d05f | 2015-11-23 02:47:30 +0000 | [diff] [blame] | 536 | LLVM_DUMP_METHOD |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 537 | void Formula::dump() const { |
| 538 | print(errs()); errs() << '\n'; |
| 539 | } |
| 540 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 541 | /// Return true if the given addrec can be sign-extended without changing its |
| 542 | /// value. |
Dan Gohman | 85af256 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 543 | static bool isAddRecSExtable(const SCEVAddRecExpr *AR, ScalarEvolution &SE) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 544 | Type *WideTy = |
Dan Gohman | ab5fb7f | 2010-05-20 19:44:23 +0000 | [diff] [blame] | 545 | IntegerType::get(SE.getContext(), SE.getTypeSizeInBits(AR->getType()) + 1); |
Dan Gohman | 85af256 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 546 | return isa<SCEVAddRecExpr>(SE.getSignExtendExpr(AR, WideTy)); |
| 547 | } |
| 548 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 549 | /// Return true if the given add can be sign-extended without changing its |
| 550 | /// value. |
Dan Gohman | 85af256 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 551 | static bool isAddSExtable(const SCEVAddExpr *A, ScalarEvolution &SE) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 552 | Type *WideTy = |
Dan Gohman | ab5fb7f | 2010-05-20 19:44:23 +0000 | [diff] [blame] | 553 | IntegerType::get(SE.getContext(), SE.getTypeSizeInBits(A->getType()) + 1); |
Dan Gohman | 85af256 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 554 | return isa<SCEVAddExpr>(SE.getSignExtendExpr(A, WideTy)); |
| 555 | } |
| 556 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 557 | /// Return true if the given mul can be sign-extended without changing its |
| 558 | /// value. |
Dan Gohman | ab54222 | 2010-06-24 16:45:11 +0000 | [diff] [blame] | 559 | static bool isMulSExtable(const SCEVMulExpr *M, ScalarEvolution &SE) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 560 | Type *WideTy = |
Dan Gohman | ab54222 | 2010-06-24 16:45:11 +0000 | [diff] [blame] | 561 | IntegerType::get(SE.getContext(), |
| 562 | SE.getTypeSizeInBits(M->getType()) * M->getNumOperands()); |
| 563 | return isa<SCEVMulExpr>(SE.getSignExtendExpr(M, WideTy)); |
Dan Gohman | 85af256 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 566 | /// Return an expression for LHS /s RHS, if it can be determined and if the |
| 567 | /// remainder is known to be zero, or null otherwise. If IgnoreSignificantBits |
| 568 | /// is true, expressions like (X * Y) /s Y are simplified to Y, ignoring that |
| 569 | /// the multiplication may overflow, which is useful when the result will be |
| 570 | /// used in a context where the most significant bits are ignored. |
Dan Gohman | 4eebb94 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 571 | static const SCEV *getExactSDiv(const SCEV *LHS, const SCEV *RHS, |
| 572 | ScalarEvolution &SE, |
| 573 | bool IgnoreSignificantBits = false) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 574 | // Handle the trivial case, which works for any SCEV type. |
| 575 | if (LHS == RHS) |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 576 | return SE.getConstant(LHS->getType(), 1); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 577 | |
Dan Gohman | 47ddf76 | 2010-06-24 16:51:25 +0000 | [diff] [blame] | 578 | // Handle a few RHS special cases. |
| 579 | const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS); |
| 580 | if (RC) { |
Sanjoy Das | 0de2fec | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 581 | const APInt &RA = RC->getAPInt(); |
Dan Gohman | 47ddf76 | 2010-06-24 16:51:25 +0000 | [diff] [blame] | 582 | // Handle x /s -1 as x * -1, to give ScalarEvolution a chance to do |
| 583 | // some folding. |
| 584 | if (RA.isAllOnesValue()) |
| 585 | return SE.getMulExpr(LHS, RC); |
| 586 | // Handle x /s 1 as x. |
| 587 | if (RA == 1) |
| 588 | return LHS; |
| 589 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 590 | |
| 591 | // Check for a division of a constant by a constant. |
| 592 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(LHS)) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 593 | if (!RC) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 594 | return nullptr; |
Sanjoy Das | 0de2fec | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 595 | const APInt &LA = C->getAPInt(); |
| 596 | const APInt &RA = RC->getAPInt(); |
Dan Gohman | 47ddf76 | 2010-06-24 16:51:25 +0000 | [diff] [blame] | 597 | if (LA.srem(RA) != 0) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 598 | return nullptr; |
Dan Gohman | 47ddf76 | 2010-06-24 16:51:25 +0000 | [diff] [blame] | 599 | return SE.getConstant(LA.sdiv(RA)); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Dan Gohman | 85af256 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 602 | // Distribute the sdiv over addrec operands, if the addrec doesn't overflow. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 603 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) { |
Alexandros Lamprineas | 0ee3ec2 | 2016-11-09 08:53:07 +0000 | [diff] [blame] | 604 | if ((IgnoreSignificantBits || isAddRecSExtable(AR, SE)) && AR->isAffine()) { |
Dan Gohman | 4eebb94 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 605 | const SCEV *Step = getExactSDiv(AR->getStepRecurrence(SE), RHS, SE, |
| 606 | IgnoreSignificantBits); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 607 | if (!Step) return nullptr; |
Dan Gohman | 129a816 | 2010-08-19 01:02:31 +0000 | [diff] [blame] | 608 | const SCEV *Start = getExactSDiv(AR->getStart(), RHS, SE, |
| 609 | IgnoreSignificantBits); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 610 | if (!Start) return nullptr; |
Andrew Trick | 8b55b73 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 611 | // FlagNW is independent of the start value, step direction, and is |
| 612 | // preserved with smaller magnitude steps. |
| 613 | // FIXME: AR->getNoWrapFlags(SCEV::FlagNW) |
| 614 | return SE.getAddRecExpr(Start, Step, AR->getLoop(), SCEV::FlagAnyWrap); |
Dan Gohman | 85af256 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 615 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 616 | return nullptr; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Dan Gohman | 85af256 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 619 | // Distribute the sdiv over add operands, if the add doesn't overflow. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 620 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(LHS)) { |
Dan Gohman | 85af256 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 621 | if (IgnoreSignificantBits || isAddSExtable(Add, SE)) { |
| 622 | SmallVector<const SCEV *, 8> Ops; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 623 | for (const SCEV *S : Add->operands()) { |
| 624 | const SCEV *Op = getExactSDiv(S, RHS, SE, IgnoreSignificantBits); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 625 | if (!Op) return nullptr; |
Dan Gohman | 85af256 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 626 | Ops.push_back(Op); |
| 627 | } |
| 628 | return SE.getAddExpr(Ops); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 629 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 630 | return nullptr; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | // Check for a multiply operand that we can pull RHS out of. |
Dan Gohman | 963b1c1 | 2010-06-24 16:57:52 +0000 | [diff] [blame] | 634 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS)) { |
Dan Gohman | 85af256 | 2010-02-19 19:32:49 +0000 | [diff] [blame] | 635 | if (IgnoreSignificantBits || isMulSExtable(Mul, SE)) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 636 | SmallVector<const SCEV *, 4> Ops; |
| 637 | bool Found = false; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 638 | for (const SCEV *S : Mul->operands()) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 639 | if (!Found) |
Dan Gohman | 6b733fc | 2010-05-20 16:23:28 +0000 | [diff] [blame] | 640 | if (const SCEV *Q = getExactSDiv(S, RHS, SE, |
Dan Gohman | 4eebb94 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 641 | IgnoreSignificantBits)) { |
Dan Gohman | 6b733fc | 2010-05-20 16:23:28 +0000 | [diff] [blame] | 642 | S = Q; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 643 | Found = true; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 644 | } |
Dan Gohman | 6b733fc | 2010-05-20 16:23:28 +0000 | [diff] [blame] | 645 | Ops.push_back(S); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 646 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 647 | return Found ? SE.getMulExpr(Ops) : nullptr; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 648 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 649 | return nullptr; |
Dan Gohman | 963b1c1 | 2010-06-24 16:57:52 +0000 | [diff] [blame] | 650 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 651 | |
| 652 | // Otherwise we don't know. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 653 | return nullptr; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 656 | /// If S involves the addition of a constant integer value, return that integer |
| 657 | /// value, and mutate S to point to a new SCEV with that value excluded. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 658 | static int64_t ExtractImmediate(const SCEV *&S, ScalarEvolution &SE) { |
| 659 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { |
Sanjoy Das | 0de2fec | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 660 | if (C->getAPInt().getMinSignedBits() <= 64) { |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 661 | S = SE.getConstant(C->getType(), 0); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 662 | return C->getValue()->getSExtValue(); |
| 663 | } |
| 664 | } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 665 | SmallVector<const SCEV *, 8> NewOps(Add->op_begin(), Add->op_end()); |
| 666 | int64_t Result = ExtractImmediate(NewOps.front(), SE); |
Dan Gohman | 081ffcd | 2010-08-13 21:17:19 +0000 | [diff] [blame] | 667 | if (Result != 0) |
| 668 | S = SE.getAddExpr(NewOps); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 669 | return Result; |
| 670 | } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 671 | SmallVector<const SCEV *, 8> NewOps(AR->op_begin(), AR->op_end()); |
| 672 | int64_t Result = ExtractImmediate(NewOps.front(), SE); |
Dan Gohman | 081ffcd | 2010-08-13 21:17:19 +0000 | [diff] [blame] | 673 | if (Result != 0) |
Andrew Trick | 8b55b73 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 674 | S = SE.getAddRecExpr(NewOps, AR->getLoop(), |
| 675 | // FIXME: AR->getNoWrapFlags(SCEV::FlagNW) |
| 676 | SCEV::FlagAnyWrap); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 677 | return Result; |
| 678 | } |
| 679 | return 0; |
| 680 | } |
| 681 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 682 | /// If S involves the addition of a GlobalValue address, return that symbol, and |
| 683 | /// mutate S to point to a new SCEV with that value excluded. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 684 | static GlobalValue *ExtractSymbol(const SCEV *&S, ScalarEvolution &SE) { |
| 685 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 686 | if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue())) { |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 687 | S = SE.getConstant(GV->getType(), 0); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 688 | return GV; |
| 689 | } |
| 690 | } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 691 | SmallVector<const SCEV *, 8> NewOps(Add->op_begin(), Add->op_end()); |
| 692 | GlobalValue *Result = ExtractSymbol(NewOps.back(), SE); |
Dan Gohman | 081ffcd | 2010-08-13 21:17:19 +0000 | [diff] [blame] | 693 | if (Result) |
| 694 | S = SE.getAddExpr(NewOps); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 695 | return Result; |
| 696 | } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 697 | SmallVector<const SCEV *, 8> NewOps(AR->op_begin(), AR->op_end()); |
| 698 | GlobalValue *Result = ExtractSymbol(NewOps.front(), SE); |
Dan Gohman | 081ffcd | 2010-08-13 21:17:19 +0000 | [diff] [blame] | 699 | if (Result) |
Andrew Trick | 8b55b73 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 700 | S = SE.getAddRecExpr(NewOps, AR->getLoop(), |
| 701 | // FIXME: AR->getNoWrapFlags(SCEV::FlagNW) |
| 702 | SCEV::FlagAnyWrap); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 703 | return Result; |
| 704 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 705 | return nullptr; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 708 | /// Returns true if the specified instruction is using the specified value as an |
| 709 | /// address. |
Dale Johannesen | 9efd2ce | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 710 | static bool isAddressUse(Instruction *Inst, Value *OperandVal) { |
| 711 | bool isAddress = isa<LoadInst>(Inst); |
| 712 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 713 | if (SI->getOperand(1) == OperandVal) |
| 714 | isAddress = true; |
| 715 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
| 716 | // Addressing modes can also be folded into prefetches and a variety |
| 717 | // of intrinsics. |
| 718 | switch (II->getIntrinsicID()) { |
| 719 | default: break; |
| 720 | case Intrinsic::prefetch: |
Gabor Greif | 8ae3095 | 2010-06-30 09:15:28 +0000 | [diff] [blame] | 721 | if (II->getArgOperand(0) == OperandVal) |
Dale Johannesen | 9efd2ce | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 722 | isAddress = true; |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | return isAddress; |
| 727 | } |
Chris Lattner | e4ed42a | 2005-10-03 01:04:44 +0000 | [diff] [blame] | 728 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 729 | /// Return the type of the memory being accessed. |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 730 | static MemAccessTy getAccessType(const Instruction *Inst) { |
| 731 | MemAccessTy AccessTy(Inst->getType(), MemAccessTy::UnknownAddressSpace); |
| 732 | if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 733 | AccessTy.MemTy = SI->getOperand(0)->getType(); |
| 734 | AccessTy.AddrSpace = SI->getPointerAddressSpace(); |
| 735 | } else if (const LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
| 736 | AccessTy.AddrSpace = LI->getPointerAddressSpace(); |
Dan Gohman | 917ffe4 | 2009-03-09 21:01:17 +0000 | [diff] [blame] | 737 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 738 | |
| 739 | // All pointers have the same requirements, so canonicalize them to an |
| 740 | // arbitrary pointer type to minimize variation. |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 741 | if (PointerType *PTy = dyn_cast<PointerType>(AccessTy.MemTy)) |
| 742 | AccessTy.MemTy = PointerType::get(IntegerType::get(PTy->getContext(), 1), |
| 743 | PTy->getAddressSpace()); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 744 | |
Dan Gohman | 14d1339 | 2009-05-18 16:45:28 +0000 | [diff] [blame] | 745 | return AccessTy; |
Dan Gohman | 917ffe4 | 2009-03-09 21:01:17 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 748 | /// Return true if this AddRec is already a phi in its loop. |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 749 | static bool isExistingPhi(const SCEVAddRecExpr *AR, ScalarEvolution &SE) { |
| 750 | for (BasicBlock::iterator I = AR->getLoop()->getHeader()->begin(); |
| 751 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 752 | if (SE.isSCEVable(PN->getType()) && |
| 753 | (SE.getEffectiveSCEVType(PN->getType()) == |
| 754 | SE.getEffectiveSCEVType(AR->getType())) && |
| 755 | SE.getSCEV(PN) == AR) |
| 756 | return true; |
| 757 | } |
| 758 | return false; |
| 759 | } |
| 760 | |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 761 | /// Check if expanding this expression is likely to incur significant cost. This |
| 762 | /// is tricky because SCEV doesn't track which expressions are actually computed |
| 763 | /// by the current IR. |
| 764 | /// |
| 765 | /// We currently allow expansion of IV increments that involve adds, |
| 766 | /// multiplication by constants, and AddRecs from existing phis. |
| 767 | /// |
| 768 | /// TODO: Allow UDivExpr if we can find an existing IV increment that is an |
| 769 | /// obvious multiple of the UDivExpr. |
| 770 | static bool isHighCostExpansion(const SCEV *S, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 771 | SmallPtrSetImpl<const SCEV*> &Processed, |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 772 | ScalarEvolution &SE) { |
| 773 | // Zero/One operand expressions |
| 774 | switch (S->getSCEVType()) { |
| 775 | case scUnknown: |
| 776 | case scConstant: |
| 777 | return false; |
| 778 | case scTruncate: |
| 779 | return isHighCostExpansion(cast<SCEVTruncateExpr>(S)->getOperand(), |
| 780 | Processed, SE); |
| 781 | case scZeroExtend: |
| 782 | return isHighCostExpansion(cast<SCEVZeroExtendExpr>(S)->getOperand(), |
| 783 | Processed, SE); |
| 784 | case scSignExtend: |
| 785 | return isHighCostExpansion(cast<SCEVSignExtendExpr>(S)->getOperand(), |
| 786 | Processed, SE); |
| 787 | } |
| 788 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 789 | if (!Processed.insert(S).second) |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 790 | return false; |
| 791 | |
| 792 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 793 | for (const SCEV *S : Add->operands()) { |
| 794 | if (isHighCostExpansion(S, Processed, SE)) |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 795 | return true; |
| 796 | } |
| 797 | return false; |
| 798 | } |
| 799 | |
| 800 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 801 | if (Mul->getNumOperands() == 2) { |
| 802 | // Multiplication by a constant is ok |
| 803 | if (isa<SCEVConstant>(Mul->getOperand(0))) |
| 804 | return isHighCostExpansion(Mul->getOperand(1), Processed, SE); |
| 805 | |
| 806 | // If we have the value of one operand, check if an existing |
| 807 | // multiplication already generates this expression. |
| 808 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Mul->getOperand(1))) { |
| 809 | Value *UVal = U->getValue(); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 810 | for (User *UR : UVal->users()) { |
Andrew Trick | 14779cc | 2012-03-26 20:28:37 +0000 | [diff] [blame] | 811 | // If U is a constant, it may be used by a ConstantExpr. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 812 | Instruction *UI = dyn_cast<Instruction>(UR); |
| 813 | if (UI && UI->getOpcode() == Instruction::Mul && |
| 814 | SE.isSCEVable(UI->getType())) { |
| 815 | return SE.getSCEV(UI) == Mul; |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 823 | if (isExistingPhi(AR, SE)) |
| 824 | return false; |
| 825 | } |
| 826 | |
| 827 | // Fow now, consider any other type of expression (div/mul/min/max) high cost. |
| 828 | return true; |
| 829 | } |
| 830 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 831 | /// If any of the instructions is the specified set are trivially dead, delete |
| 832 | /// them and see if this makes any of their operands subsequently dead. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 833 | static bool |
| 834 | DeleteTriviallyDeadInstructions(SmallVectorImpl<WeakVH> &DeadInsts) { |
| 835 | bool Changed = false; |
| 836 | |
| 837 | while (!DeadInsts.empty()) { |
Richard Smith | ad9c8e8 | 2012-08-21 20:35:14 +0000 | [diff] [blame] | 838 | Value *V = DeadInsts.pop_back_val(); |
| 839 | Instruction *I = dyn_cast_or_null<Instruction>(V); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 840 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 841 | if (!I || !isInstructionTriviallyDead(I)) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 842 | continue; |
| 843 | |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 844 | for (Use &O : I->operands()) |
| 845 | if (Instruction *U = dyn_cast<Instruction>(O)) { |
| 846 | O = nullptr; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 847 | if (U->use_empty()) |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 848 | DeadInsts.emplace_back(U); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | I->eraseFromParent(); |
| 852 | Changed = true; |
| 853 | } |
| 854 | |
| 855 | return Changed; |
| 856 | } |
| 857 | |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 858 | namespace { |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 859 | |
Quentin Colombet | 8aa7abe | 2013-05-31 17:20:29 +0000 | [diff] [blame] | 860 | class LSRUse; |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 861 | |
| 862 | } // end anonymous namespace |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 863 | |
| 864 | /// \brief Check if the addressing mode defined by \p F is completely |
| 865 | /// folded in \p LU at isel time. |
| 866 | /// This includes address-mode folding and special icmp tricks. |
| 867 | /// This function returns true if \p LU can accommodate what \p F |
| 868 | /// defines and up to 1 base + 1 scaled + offset. |
| 869 | /// In other words, if \p F has several base registers, this function may |
| 870 | /// still return true. Therefore, users still need to account for |
| 871 | /// additional base registers and/or unfolded offsets to derive an |
| 872 | /// accurate cost model. |
| 873 | static bool isAMCompletelyFolded(const TargetTransformInfo &TTI, |
| 874 | const LSRUse &LU, const Formula &F); |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 875 | // Get the cost of the scaling factor used in F for LU. |
| 876 | static unsigned getScalingFactorCost(const TargetTransformInfo &TTI, |
| 877 | const LSRUse &LU, const Formula &F); |
Quentin Colombet | 8aa7abe | 2013-05-31 17:20:29 +0000 | [diff] [blame] | 878 | |
| 879 | namespace { |
Jim Grosbach | 60f4854 | 2009-11-17 17:53:56 +0000 | [diff] [blame] | 880 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 881 | /// This class is used to measure and compare candidate formulae. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 882 | class Cost { |
| 883 | /// TODO: Some of these could be merged. Also, a lexical ordering |
| 884 | /// isn't always optimal. |
| 885 | unsigned NumRegs; |
| 886 | unsigned AddRecCost; |
| 887 | unsigned NumIVMuls; |
| 888 | unsigned NumBaseAdds; |
| 889 | unsigned ImmCost; |
| 890 | unsigned SetupCost; |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 891 | unsigned ScaleCost; |
Nate Begeman | e68bcd1 | 2005-07-30 00:15:07 +0000 | [diff] [blame] | 892 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 893 | public: |
| 894 | Cost() |
| 895 | : NumRegs(0), AddRecCost(0), NumIVMuls(0), NumBaseAdds(0), ImmCost(0), |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 896 | SetupCost(0), ScaleCost(0) {} |
Jim Grosbach | 60f4854 | 2009-11-17 17:53:56 +0000 | [diff] [blame] | 897 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 898 | bool operator<(const Cost &Other) const; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 899 | |
Tim Northover | bc6659c | 2014-01-22 13:27:00 +0000 | [diff] [blame] | 900 | void Lose(); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 901 | |
Andrew Trick | 784729d | 2011-09-26 23:11:04 +0000 | [diff] [blame] | 902 | #ifndef NDEBUG |
| 903 | // Once any of the metrics loses, they must all remain losers. |
| 904 | bool isValid() { |
| 905 | return ((NumRegs | AddRecCost | NumIVMuls | NumBaseAdds |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 906 | | ImmCost | SetupCost | ScaleCost) != ~0u) |
Andrew Trick | 784729d | 2011-09-26 23:11:04 +0000 | [diff] [blame] | 907 | || ((NumRegs & AddRecCost & NumIVMuls & NumBaseAdds |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 908 | & ImmCost & SetupCost & ScaleCost) == ~0u); |
Andrew Trick | 784729d | 2011-09-26 23:11:04 +0000 | [diff] [blame] | 909 | } |
| 910 | #endif |
| 911 | |
| 912 | bool isLoser() { |
| 913 | assert(isValid() && "invalid cost"); |
| 914 | return NumRegs == ~0u; |
| 915 | } |
| 916 | |
Quentin Colombet | 8aa7abe | 2013-05-31 17:20:29 +0000 | [diff] [blame] | 917 | void RateFormula(const TargetTransformInfo &TTI, |
| 918 | const Formula &F, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 919 | SmallPtrSetImpl<const SCEV *> &Regs, |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 920 | const DenseSet<const SCEV *> &VisitedRegs, |
| 921 | const Loop *L, |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 922 | ScalarEvolution &SE, DominatorTree &DT, |
Quentin Colombet | 8aa7abe | 2013-05-31 17:20:29 +0000 | [diff] [blame] | 923 | const LSRUse &LU, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 924 | SmallPtrSetImpl<const SCEV *> *LoserRegs = nullptr); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 925 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 926 | void print(raw_ostream &OS) const; |
| 927 | void dump() const; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 928 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 929 | private: |
| 930 | void RateRegister(const SCEV *Reg, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 931 | SmallPtrSetImpl<const SCEV *> &Regs, |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 932 | const Loop *L, |
| 933 | ScalarEvolution &SE, DominatorTree &DT); |
Dan Gohman | 5b18f03 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 934 | void RatePrimaryRegister(const SCEV *Reg, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 935 | SmallPtrSetImpl<const SCEV *> &Regs, |
Dan Gohman | 5b18f03 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 936 | const Loop *L, |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 937 | ScalarEvolution &SE, DominatorTree &DT, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 938 | SmallPtrSetImpl<const SCEV *> *LoserRegs); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 939 | }; |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 940 | |
| 941 | /// An operand value in an instruction which is to be replaced with some |
| 942 | /// equivalent, possibly strength-reduced, replacement. |
| 943 | struct LSRFixup { |
| 944 | /// The instruction which will be updated. |
| 945 | Instruction *UserInst; |
| 946 | |
| 947 | /// The operand of the instruction which will be replaced. The operand may be |
| 948 | /// used more than once; every instance will be replaced. |
| 949 | Value *OperandValToReplace; |
| 950 | |
| 951 | /// If this user is to use the post-incremented value of an induction |
| 952 | /// variable, this variable is non-null and holds the loop associated with the |
| 953 | /// induction variable. |
| 954 | PostIncLoopSet PostIncLoops; |
| 955 | |
| 956 | /// A constant offset to be added to the LSRUse expression. This allows |
| 957 | /// multiple fixups to share the same LSRUse with different offsets, for |
| 958 | /// example in an unrolled loop. |
| 959 | int64_t Offset; |
| 960 | |
| 961 | bool isUseFullyOutsideLoop(const Loop *L) const; |
| 962 | |
| 963 | LSRFixup(); |
| 964 | |
| 965 | void print(raw_ostream &OS) const; |
| 966 | void dump() const; |
| 967 | }; |
| 968 | |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 969 | /// A DenseMapInfo implementation for holding DenseMaps and DenseSets of sorted |
| 970 | /// SmallVectors of const SCEV*. |
| 971 | struct UniquifierDenseMapInfo { |
| 972 | static SmallVector<const SCEV *, 4> getEmptyKey() { |
| 973 | SmallVector<const SCEV *, 4> V; |
| 974 | V.push_back(reinterpret_cast<const SCEV *>(-1)); |
| 975 | return V; |
| 976 | } |
| 977 | |
| 978 | static SmallVector<const SCEV *, 4> getTombstoneKey() { |
| 979 | SmallVector<const SCEV *, 4> V; |
| 980 | V.push_back(reinterpret_cast<const SCEV *>(-2)); |
| 981 | return V; |
| 982 | } |
| 983 | |
| 984 | static unsigned getHashValue(const SmallVector<const SCEV *, 4> &V) { |
| 985 | return static_cast<unsigned>(hash_combine_range(V.begin(), V.end())); |
| 986 | } |
| 987 | |
| 988 | static bool isEqual(const SmallVector<const SCEV *, 4> &LHS, |
| 989 | const SmallVector<const SCEV *, 4> &RHS) { |
| 990 | return LHS == RHS; |
| 991 | } |
| 992 | }; |
| 993 | |
| 994 | /// This class holds the state that LSR keeps for each use in IVUsers, as well |
| 995 | /// as uses invented by LSR itself. It includes information about what kinds of |
| 996 | /// things can be folded into the user, information about the user itself, and |
| 997 | /// information about how the use may be satisfied. TODO: Represent multiple |
| 998 | /// users of the same expression in common? |
| 999 | class LSRUse { |
| 1000 | DenseSet<SmallVector<const SCEV *, 4>, UniquifierDenseMapInfo> Uniquifier; |
| 1001 | |
| 1002 | public: |
| 1003 | /// An enum for a kind of use, indicating what types of scaled and immediate |
| 1004 | /// operands it might support. |
| 1005 | enum KindType { |
| 1006 | Basic, ///< A normal use, with no folding. |
| 1007 | Special, ///< A special case of basic, allowing -1 scales. |
| 1008 | Address, ///< An address use; folding according to TargetLowering |
| 1009 | ICmpZero ///< An equality icmp with both operands folded into one. |
| 1010 | // TODO: Add a generic icmp too? |
| 1011 | }; |
| 1012 | |
| 1013 | typedef PointerIntPair<const SCEV *, 2, KindType> SCEVUseKindPair; |
| 1014 | |
| 1015 | KindType Kind; |
| 1016 | MemAccessTy AccessTy; |
| 1017 | |
| 1018 | /// The list of operands which are to be replaced. |
| 1019 | SmallVector<LSRFixup, 8> Fixups; |
| 1020 | |
| 1021 | /// Keep track of the min and max offsets of the fixups. |
| 1022 | int64_t MinOffset; |
| 1023 | int64_t MaxOffset; |
| 1024 | |
| 1025 | /// This records whether all of the fixups using this LSRUse are outside of |
| 1026 | /// the loop, in which case some special-case heuristics may be used. |
| 1027 | bool AllFixupsOutsideLoop; |
| 1028 | |
| 1029 | /// RigidFormula is set to true to guarantee that this use will be associated |
| 1030 | /// with a single formula--the one that initially matched. Some SCEV |
| 1031 | /// expressions cannot be expanded. This allows LSR to consider the registers |
| 1032 | /// used by those expressions without the need to expand them later after |
| 1033 | /// changing the formula. |
| 1034 | bool RigidFormula; |
| 1035 | |
| 1036 | /// This records the widest use type for any fixup using this |
| 1037 | /// LSRUse. FindUseWithSimilarFormula can't consider uses with different max |
| 1038 | /// fixup widths to be equivalent, because the narrower one may be relying on |
| 1039 | /// the implicit truncation to truncate away bogus bits. |
| 1040 | Type *WidestFixupType; |
| 1041 | |
| 1042 | /// A list of ways to build a value that can satisfy this user. After the |
| 1043 | /// list is populated, one of these is selected heuristically and used to |
| 1044 | /// formulate a replacement for OperandValToReplace in UserInst. |
| 1045 | SmallVector<Formula, 12> Formulae; |
| 1046 | |
| 1047 | /// The set of register candidates used by all formulae in this LSRUse. |
| 1048 | SmallPtrSet<const SCEV *, 4> Regs; |
| 1049 | |
| 1050 | LSRUse(KindType K, MemAccessTy AT) |
| 1051 | : Kind(K), AccessTy(AT), MinOffset(INT64_MAX), MaxOffset(INT64_MIN), |
| 1052 | AllFixupsOutsideLoop(true), RigidFormula(false), |
| 1053 | WidestFixupType(nullptr) {} |
| 1054 | |
| 1055 | LSRFixup &getNewFixup() { |
| 1056 | Fixups.push_back(LSRFixup()); |
| 1057 | return Fixups.back(); |
| 1058 | } |
| 1059 | |
| 1060 | void pushFixup(LSRFixup &f) { |
| 1061 | Fixups.push_back(f); |
| 1062 | if (f.Offset > MaxOffset) |
| 1063 | MaxOffset = f.Offset; |
| 1064 | if (f.Offset < MinOffset) |
| 1065 | MinOffset = f.Offset; |
| 1066 | } |
| 1067 | |
| 1068 | bool HasFormulaWithSameRegs(const Formula &F) const; |
| 1069 | bool InsertFormula(const Formula &F); |
| 1070 | void DeleteFormula(Formula &F); |
| 1071 | void RecomputeRegs(size_t LUIdx, RegUseTracker &Reguses); |
| 1072 | |
| 1073 | void print(raw_ostream &OS) const; |
| 1074 | void dump() const; |
| 1075 | }; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1076 | |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 1077 | } // end anonymous namespace |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1078 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1079 | /// Tally up interesting quantities from the given register. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1080 | void Cost::RateRegister(const SCEV *Reg, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 1081 | SmallPtrSetImpl<const SCEV *> &Regs, |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1082 | const Loop *L, |
| 1083 | ScalarEvolution &SE, DominatorTree &DT) { |
Dan Gohman | 5b18f03 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 1084 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Reg)) { |
Wei Mi | 37c4aaa | 2016-11-15 19:42:05 +0000 | [diff] [blame] | 1085 | // If this is an addrec for another loop, don't second-guess its addrec phi |
| 1086 | // nodes. LSR isn't currently smart enough to reason about more than one |
| 1087 | // loop at a time. LSR has already run on inner loops, will not run on outer |
| 1088 | // loops, and cannot be expected to change sibling loops. |
Andrew Trick | d97b83e | 2012-03-22 22:42:45 +0000 | [diff] [blame] | 1089 | if (AR->getLoop() != L) { |
| 1090 | // If the AddRec exists, consider it's register free and leave it alone. |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 1091 | if (isExistingPhi(AR, SE)) |
| 1092 | return; |
| 1093 | |
Wei Mi | 37c4aaa | 2016-11-15 19:42:05 +0000 | [diff] [blame] | 1094 | // Otherwise, do not consider this formula at all. |
| 1095 | Lose(); |
Andrew Trick | d97b83e | 2012-03-22 22:42:45 +0000 | [diff] [blame] | 1096 | return; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1097 | } |
Andrew Trick | d97b83e | 2012-03-22 22:42:45 +0000 | [diff] [blame] | 1098 | AddRecCost += 1; /// TODO: This should be a function of the stride. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1099 | |
Dan Gohman | 5b18f03 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 1100 | // Add the step value register, if it needs one. |
| 1101 | // TODO: The non-affine case isn't precisely modeled here. |
Andrew Trick | 8868fae | 2011-09-26 23:35:25 +0000 | [diff] [blame] | 1102 | if (!AR->isAffine() || !isa<SCEVConstant>(AR->getOperand(1))) { |
| 1103 | if (!Regs.count(AR->getOperand(1))) { |
Dan Gohman | 5b18f03 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 1104 | RateRegister(AR->getOperand(1), Regs, L, SE, DT); |
Andrew Trick | 8868fae | 2011-09-26 23:35:25 +0000 | [diff] [blame] | 1105 | if (isLoser()) |
| 1106 | return; |
| 1107 | } |
| 1108 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1109 | } |
Dan Gohman | 5b18f03 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 1110 | ++NumRegs; |
| 1111 | |
| 1112 | // Rough heuristic; favor registers which don't require extra setup |
| 1113 | // instructions in the preheader. |
| 1114 | if (!isa<SCEVUnknown>(Reg) && |
| 1115 | !isa<SCEVConstant>(Reg) && |
| 1116 | !(isa<SCEVAddRecExpr>(Reg) && |
| 1117 | (isa<SCEVUnknown>(cast<SCEVAddRecExpr>(Reg)->getStart()) || |
| 1118 | isa<SCEVConstant>(cast<SCEVAddRecExpr>(Reg)->getStart())))) |
| 1119 | ++SetupCost; |
Dan Gohman | 34f37e0 | 2010-10-07 23:41:58 +0000 | [diff] [blame] | 1120 | |
Davide Italiano | 709d418 | 2016-07-07 17:44:38 +0000 | [diff] [blame] | 1121 | NumIVMuls += isa<SCEVMulExpr>(Reg) && |
| 1122 | SE.hasComputableLoopEvolution(Reg, L); |
Dan Gohman | 5b18f03 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1125 | /// Record this register in the set. If we haven't seen it before, rate |
| 1126 | /// it. Optional LoserRegs provides a way to declare any formula that refers to |
| 1127 | /// one of those regs an instant loser. |
Dan Gohman | 5b18f03 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 1128 | void Cost::RatePrimaryRegister(const SCEV *Reg, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 1129 | SmallPtrSetImpl<const SCEV *> &Regs, |
Dan Gohman | 0849ed5 | 2010-02-16 19:42:34 +0000 | [diff] [blame] | 1130 | const Loop *L, |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 1131 | ScalarEvolution &SE, DominatorTree &DT, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 1132 | SmallPtrSetImpl<const SCEV *> *LoserRegs) { |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 1133 | if (LoserRegs && LoserRegs->count(Reg)) { |
Tim Northover | bc6659c | 2014-01-22 13:27:00 +0000 | [diff] [blame] | 1134 | Lose(); |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 1135 | return; |
| 1136 | } |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1137 | if (Regs.insert(Reg).second) { |
Dan Gohman | 5b18f03 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 1138 | RateRegister(Reg, Regs, L, SE, DT); |
Andrew Trick | a1c01ba | 2013-03-19 04:14:57 +0000 | [diff] [blame] | 1139 | if (LoserRegs && isLoser()) |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 1140 | LoserRegs->insert(Reg); |
| 1141 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Quentin Colombet | 8aa7abe | 2013-05-31 17:20:29 +0000 | [diff] [blame] | 1144 | void Cost::RateFormula(const TargetTransformInfo &TTI, |
| 1145 | const Formula &F, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 1146 | SmallPtrSetImpl<const SCEV *> &Regs, |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1147 | const DenseSet<const SCEV *> &VisitedRegs, |
| 1148 | const Loop *L, |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 1149 | ScalarEvolution &SE, DominatorTree &DT, |
Quentin Colombet | 8aa7abe | 2013-05-31 17:20:29 +0000 | [diff] [blame] | 1150 | const LSRUse &LU, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 1151 | SmallPtrSetImpl<const SCEV *> *LoserRegs) { |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1152 | assert(F.isCanonical() && "Cost is accurate only for canonical formula"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1153 | // Tally up the registers. |
| 1154 | if (const SCEV *ScaledReg = F.ScaledReg) { |
| 1155 | if (VisitedRegs.count(ScaledReg)) { |
Tim Northover | bc6659c | 2014-01-22 13:27:00 +0000 | [diff] [blame] | 1156 | Lose(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1157 | return; |
| 1158 | } |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 1159 | RatePrimaryRegister(ScaledReg, Regs, L, SE, DT, LoserRegs); |
Andrew Trick | 784729d | 2011-09-26 23:11:04 +0000 | [diff] [blame] | 1160 | if (isLoser()) |
| 1161 | return; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1162 | } |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 1163 | for (const SCEV *BaseReg : F.BaseRegs) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1164 | if (VisitedRegs.count(BaseReg)) { |
Tim Northover | bc6659c | 2014-01-22 13:27:00 +0000 | [diff] [blame] | 1165 | Lose(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1166 | return; |
| 1167 | } |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 1168 | RatePrimaryRegister(BaseReg, Regs, L, SE, DT, LoserRegs); |
Andrew Trick | 784729d | 2011-09-26 23:11:04 +0000 | [diff] [blame] | 1169 | if (isLoser()) |
| 1170 | return; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Dan Gohman | 6136e94 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 1173 | // Determine how many (unfolded) adds we'll need inside the loop. |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1174 | size_t NumBaseParts = F.getNumRegs(); |
Dan Gohman | 6136e94 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 1175 | if (NumBaseParts > 1) |
Quentin Colombet | 8aa7abe | 2013-05-31 17:20:29 +0000 | [diff] [blame] | 1176 | // Do not count the base and a possible second register if the target |
| 1177 | // allows to fold 2 registers. |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1178 | NumBaseAdds += |
| 1179 | NumBaseParts - (1 + (F.Scale && isAMCompletelyFolded(TTI, LU, F))); |
| 1180 | NumBaseAdds += (F.UnfoldedOffset != 0); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1181 | |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 1182 | // Accumulate non-free scaling amounts. |
| 1183 | ScaleCost += getScalingFactorCost(TTI, LU, F); |
| 1184 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1185 | // Tally up the non-zero immediates. |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 1186 | for (const LSRFixup &Fixup : LU.Fixups) { |
| 1187 | int64_t O = Fixup.Offset; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 1188 | int64_t Offset = (uint64_t)O + F.BaseOffset; |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 1189 | if (F.BaseGV) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1190 | ImmCost += 64; // Handle symbolic values conservatively. |
| 1191 | // TODO: This should probably be the pointer size. |
| 1192 | else if (Offset != 0) |
| 1193 | ImmCost += APInt(64, Offset, true).getMinSignedBits(); |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 1194 | |
| 1195 | // Check with target if this offset with this instruction is |
| 1196 | // specifically not supported. |
| 1197 | if ((isa<LoadInst>(Fixup.UserInst) || isa<StoreInst>(Fixup.UserInst)) && |
| 1198 | !TTI.isFoldableMemAccessOffset(Fixup.UserInst, Offset)) |
| 1199 | NumBaseAdds++; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1200 | } |
Andrew Trick | 784729d | 2011-09-26 23:11:04 +0000 | [diff] [blame] | 1201 | assert(isValid() && "invalid cost"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1204 | /// Set this cost to a losing value. |
Tim Northover | bc6659c | 2014-01-22 13:27:00 +0000 | [diff] [blame] | 1205 | void Cost::Lose() { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1206 | NumRegs = ~0u; |
| 1207 | AddRecCost = ~0u; |
| 1208 | NumIVMuls = ~0u; |
| 1209 | NumBaseAdds = ~0u; |
| 1210 | ImmCost = ~0u; |
| 1211 | SetupCost = ~0u; |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 1212 | ScaleCost = ~0u; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1215 | /// Choose the lower cost. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1216 | bool Cost::operator<(const Cost &Other) const { |
Benjamin Kramer | b2f034b | 2014-03-03 19:58:30 +0000 | [diff] [blame] | 1217 | return std::tie(NumRegs, AddRecCost, NumIVMuls, NumBaseAdds, ScaleCost, |
| 1218 | ImmCost, SetupCost) < |
| 1219 | std::tie(Other.NumRegs, Other.AddRecCost, Other.NumIVMuls, |
| 1220 | Other.NumBaseAdds, Other.ScaleCost, Other.ImmCost, |
| 1221 | Other.SetupCost); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | void Cost::print(raw_ostream &OS) const { |
| 1225 | OS << NumRegs << " reg" << (NumRegs == 1 ? "" : "s"); |
| 1226 | if (AddRecCost != 0) |
| 1227 | OS << ", with addrec cost " << AddRecCost; |
| 1228 | if (NumIVMuls != 0) |
| 1229 | OS << ", plus " << NumIVMuls << " IV mul" << (NumIVMuls == 1 ? "" : "s"); |
| 1230 | if (NumBaseAdds != 0) |
| 1231 | OS << ", plus " << NumBaseAdds << " base add" |
| 1232 | << (NumBaseAdds == 1 ? "" : "s"); |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 1233 | if (ScaleCost != 0) |
| 1234 | OS << ", plus " << ScaleCost << " scale cost"; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1235 | if (ImmCost != 0) |
| 1236 | OS << ", plus " << ImmCost << " imm cost"; |
| 1237 | if (SetupCost != 0) |
| 1238 | OS << ", plus " << SetupCost << " setup cost"; |
| 1239 | } |
| 1240 | |
Davide Italiano | 945d05f | 2015-11-23 02:47:30 +0000 | [diff] [blame] | 1241 | LLVM_DUMP_METHOD |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1242 | void Cost::dump() const { |
| 1243 | print(errs()); errs() << '\n'; |
| 1244 | } |
| 1245 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1246 | LSRFixup::LSRFixup() |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 1247 | : UserInst(nullptr), OperandValToReplace(nullptr), |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1248 | Offset(0) {} |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1249 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1250 | /// Test whether this fixup always uses its value outside of the given loop. |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1251 | bool LSRFixup::isUseFullyOutsideLoop(const Loop *L) const { |
| 1252 | // PHI nodes use their value in their incoming blocks. |
| 1253 | if (const PHINode *PN = dyn_cast<PHINode>(UserInst)) { |
| 1254 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 1255 | if (PN->getIncomingValue(i) == OperandValToReplace && |
| 1256 | L->contains(PN->getIncomingBlock(i))) |
| 1257 | return false; |
| 1258 | return true; |
| 1259 | } |
| 1260 | |
| 1261 | return !L->contains(UserInst); |
| 1262 | } |
| 1263 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1264 | void LSRFixup::print(raw_ostream &OS) const { |
| 1265 | OS << "UserInst="; |
| 1266 | // Store is common and interesting enough to be worth special-casing. |
| 1267 | if (StoreInst *Store = dyn_cast<StoreInst>(UserInst)) { |
| 1268 | OS << "store "; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 1269 | Store->getOperand(0)->printAsOperand(OS, /*PrintType=*/false); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1270 | } else if (UserInst->getType()->isVoidTy()) |
| 1271 | OS << UserInst->getOpcodeName(); |
| 1272 | else |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 1273 | UserInst->printAsOperand(OS, /*PrintType=*/false); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1274 | |
| 1275 | OS << ", OperandValToReplace="; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 1276 | OperandValToReplace->printAsOperand(OS, /*PrintType=*/false); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1277 | |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 1278 | for (const Loop *PIL : PostIncLoops) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1279 | OS << ", PostIncLoop="; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 1280 | PIL->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1283 | if (Offset != 0) |
| 1284 | OS << ", Offset=" << Offset; |
| 1285 | } |
| 1286 | |
Davide Italiano | 945d05f | 2015-11-23 02:47:30 +0000 | [diff] [blame] | 1287 | LLVM_DUMP_METHOD |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1288 | void LSRFixup::dump() const { |
| 1289 | print(errs()); errs() << '\n'; |
| 1290 | } |
| 1291 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1292 | /// Test whether this use as a formula which has the same registers as the given |
| 1293 | /// formula. |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 1294 | bool LSRUse::HasFormulaWithSameRegs(const Formula &F) const { |
Preston Gurd | 25c3b6a | 2013-02-01 20:41:27 +0000 | [diff] [blame] | 1295 | SmallVector<const SCEV *, 4> Key = F.BaseRegs; |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 1296 | if (F.ScaledReg) Key.push_back(F.ScaledReg); |
| 1297 | // Unstable sort by host order ok, because this is only used for uniquifying. |
| 1298 | std::sort(Key.begin(), Key.end()); |
| 1299 | return Uniquifier.count(Key); |
| 1300 | } |
| 1301 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1302 | /// If the given formula has not yet been inserted, add it to the list, and |
| 1303 | /// return true. Return false otherwise. The formula must be in canonical form. |
Dan Gohman | 8c16b38 | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1304 | bool LSRUse::InsertFormula(const Formula &F) { |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1305 | assert(F.isCanonical() && "Invalid canonical representation"); |
| 1306 | |
Andrew Trick | 57243da | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 1307 | if (!Formulae.empty() && RigidFormula) |
| 1308 | return false; |
| 1309 | |
Preston Gurd | 25c3b6a | 2013-02-01 20:41:27 +0000 | [diff] [blame] | 1310 | SmallVector<const SCEV *, 4> Key = F.BaseRegs; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1311 | if (F.ScaledReg) Key.push_back(F.ScaledReg); |
| 1312 | // Unstable sort by host order ok, because this is only used for uniquifying. |
| 1313 | std::sort(Key.begin(), Key.end()); |
| 1314 | |
| 1315 | if (!Uniquifier.insert(Key).second) |
| 1316 | return false; |
| 1317 | |
| 1318 | // Using a register to hold the value of 0 is not profitable. |
| 1319 | assert((!F.ScaledReg || !F.ScaledReg->isZero()) && |
| 1320 | "Zero allocated in a scaled register!"); |
| 1321 | #ifndef NDEBUG |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 1322 | for (const SCEV *BaseReg : F.BaseRegs) |
| 1323 | assert(!BaseReg->isZero() && "Zero allocated in a base register!"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1324 | #endif |
| 1325 | |
| 1326 | // Add the formula to the list. |
| 1327 | Formulae.push_back(F); |
| 1328 | |
| 1329 | // Record registers now being used by this use. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1330 | Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end()); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1331 | if (F.ScaledReg) |
| 1332 | Regs.insert(F.ScaledReg); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1333 | |
| 1334 | return true; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1337 | /// Remove the given formula from this use's list. |
Dan Gohman | f1c7b1b | 2010-05-18 22:39:15 +0000 | [diff] [blame] | 1338 | void LSRUse::DeleteFormula(Formula &F) { |
Dan Gohman | 80a9608 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 1339 | if (&F != &Formulae.back()) |
| 1340 | std::swap(F, Formulae.back()); |
Dan Gohman | f1c7b1b | 2010-05-18 22:39:15 +0000 | [diff] [blame] | 1341 | Formulae.pop_back(); |
| 1342 | } |
| 1343 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1344 | /// Recompute the Regs field, and update RegUses. |
Dan Gohman | 4cf99b5 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 1345 | void LSRUse::RecomputeRegs(size_t LUIdx, RegUseTracker &RegUses) { |
| 1346 | // Now that we've filtered out some formulae, recompute the Regs set. |
Benjamin Kramer | 1c2beed | 2015-02-19 17:19:43 +0000 | [diff] [blame] | 1347 | SmallPtrSet<const SCEV *, 4> OldRegs = std::move(Regs); |
Dan Gohman | 4cf99b5 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 1348 | Regs.clear(); |
Benjamin Kramer | 1c2beed | 2015-02-19 17:19:43 +0000 | [diff] [blame] | 1349 | for (const Formula &F : Formulae) { |
Dan Gohman | 4cf99b5 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 1350 | if (F.ScaledReg) Regs.insert(F.ScaledReg); |
| 1351 | Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end()); |
| 1352 | } |
| 1353 | |
| 1354 | // Update the RegTracker. |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1355 | for (const SCEV *S : OldRegs) |
| 1356 | if (!Regs.count(S)) |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 1357 | RegUses.dropRegister(S, LUIdx); |
Dan Gohman | 4cf99b5 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1360 | void LSRUse::print(raw_ostream &OS) const { |
| 1361 | OS << "LSR Use: Kind="; |
| 1362 | switch (Kind) { |
| 1363 | case Basic: OS << "Basic"; break; |
| 1364 | case Special: OS << "Special"; break; |
| 1365 | case ICmpZero: OS << "ICmpZero"; break; |
| 1366 | case Address: |
| 1367 | OS << "Address of "; |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 1368 | if (AccessTy.MemTy->isPointerTy()) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1369 | OS << "pointer"; // the full pointer type could be really verbose |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 1370 | else { |
| 1371 | OS << *AccessTy.MemTy; |
| 1372 | } |
| 1373 | |
| 1374 | OS << " in addrspace(" << AccessTy.AddrSpace << ')'; |
Evan Cheng | 133694d | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1377 | OS << ", Offsets={"; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 1378 | bool NeedComma = false; |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 1379 | for (const LSRFixup &Fixup : Fixups) { |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 1380 | if (NeedComma) OS << ','; |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 1381 | OS << Fixup.Offset; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 1382 | NeedComma = true; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1383 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1384 | OS << '}'; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1385 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1386 | if (AllFixupsOutsideLoop) |
| 1387 | OS << ", all-fixups-outside-loop"; |
Dan Gohman | 1415208 | 2010-07-15 20:24:58 +0000 | [diff] [blame] | 1388 | |
| 1389 | if (WidestFixupType) |
| 1390 | OS << ", widest fixup type: " << *WidestFixupType; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Davide Italiano | 945d05f | 2015-11-23 02:47:30 +0000 | [diff] [blame] | 1393 | LLVM_DUMP_METHOD |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1394 | void LSRUse::dump() const { |
| 1395 | print(errs()); errs() << '\n'; |
| 1396 | } |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1397 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1398 | static bool isAMCompletelyFolded(const TargetTransformInfo &TTI, |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 1399 | LSRUse::KindType Kind, MemAccessTy AccessTy, |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1400 | GlobalValue *BaseGV, int64_t BaseOffset, |
| 1401 | bool HasBaseReg, int64_t Scale) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1402 | switch (Kind) { |
| 1403 | case LSRUse::Address: |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 1404 | return TTI.isLegalAddressingMode(AccessTy.MemTy, BaseGV, BaseOffset, |
| 1405 | HasBaseReg, Scale, AccessTy.AddrSpace); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1406 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1407 | case LSRUse::ICmpZero: |
| 1408 | // There's not even a target hook for querying whether it would be legal to |
| 1409 | // fold a GV into an ICmp. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1410 | if (BaseGV) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1411 | return false; |
| 1412 | |
| 1413 | // ICmp only has two operands; don't allow more than two non-trivial parts. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1414 | if (Scale != 0 && HasBaseReg && BaseOffset != 0) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1415 | return false; |
| 1416 | |
| 1417 | // ICmp only supports no scale or a -1 scale, as we can "fold" a -1 scale by |
| 1418 | // putting the scaled register in the other operand of the icmp. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1419 | if (Scale != 0 && Scale != -1) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1420 | return false; |
| 1421 | |
| 1422 | // If we have low-level target information, ask the target if it can fold an |
| 1423 | // integer immediate on an icmp. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1424 | if (BaseOffset != 0) { |
Jakob Stoklund Olesen | f2390e8 | 2012-04-05 03:10:56 +0000 | [diff] [blame] | 1425 | // We have one of: |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1426 | // ICmpZero BaseReg + BaseOffset => ICmp BaseReg, -BaseOffset |
| 1427 | // ICmpZero -1*ScaleReg + BaseOffset => ICmp ScaleReg, BaseOffset |
Jakob Stoklund Olesen | f2390e8 | 2012-04-05 03:10:56 +0000 | [diff] [blame] | 1428 | // Offs is the ICmp immediate. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1429 | if (Scale == 0) |
| 1430 | // The cast does the right thing with INT64_MIN. |
| 1431 | BaseOffset = -(uint64_t)BaseOffset; |
| 1432 | return TTI.isLegalICmpImmediate(BaseOffset); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1433 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1434 | |
Jakob Stoklund Olesen | f2390e8 | 2012-04-05 03:10:56 +0000 | [diff] [blame] | 1435 | // ICmpZero BaseReg + -1*ScaleReg => ICmp BaseReg, ScaleReg |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1436 | return true; |
| 1437 | |
| 1438 | case LSRUse::Basic: |
| 1439 | // Only handle single-register values. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1440 | return !BaseGV && Scale == 0 && BaseOffset == 0; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1441 | |
| 1442 | case LSRUse::Special: |
Andrew Trick | aca8fb3 | 2012-06-15 20:07:26 +0000 | [diff] [blame] | 1443 | // Special case Basic to handle -1 scales. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1444 | return !BaseGV && (Scale == 0 || Scale == -1) && BaseOffset == 0; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 1447 | llvm_unreachable("Invalid LSRUse Kind!"); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1450 | static bool isAMCompletelyFolded(const TargetTransformInfo &TTI, |
| 1451 | int64_t MinOffset, int64_t MaxOffset, |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 1452 | LSRUse::KindType Kind, MemAccessTy AccessTy, |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1453 | GlobalValue *BaseGV, int64_t BaseOffset, |
| 1454 | bool HasBaseReg, int64_t Scale) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1455 | // Check for overflow. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1456 | if (((int64_t)((uint64_t)BaseOffset + MinOffset) > BaseOffset) != |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1457 | (MinOffset > 0)) |
| 1458 | return false; |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1459 | MinOffset = (uint64_t)BaseOffset + MinOffset; |
| 1460 | if (((int64_t)((uint64_t)BaseOffset + MaxOffset) > BaseOffset) != |
| 1461 | (MaxOffset > 0)) |
| 1462 | return false; |
| 1463 | MaxOffset = (uint64_t)BaseOffset + MaxOffset; |
| 1464 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1465 | return isAMCompletelyFolded(TTI, Kind, AccessTy, BaseGV, MinOffset, |
| 1466 | HasBaseReg, Scale) && |
| 1467 | isAMCompletelyFolded(TTI, Kind, AccessTy, BaseGV, MaxOffset, |
| 1468 | HasBaseReg, Scale); |
| 1469 | } |
| 1470 | |
| 1471 | static bool isAMCompletelyFolded(const TargetTransformInfo &TTI, |
| 1472 | int64_t MinOffset, int64_t MaxOffset, |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 1473 | LSRUse::KindType Kind, MemAccessTy AccessTy, |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1474 | const Formula &F) { |
| 1475 | // For the purpose of isAMCompletelyFolded either having a canonical formula |
| 1476 | // or a scale not equal to zero is correct. |
| 1477 | // Problems may arise from non canonical formulae having a scale == 0. |
| 1478 | // Strictly speaking it would best to just rely on canonical formulae. |
| 1479 | // However, when we generate the scaled formulae, we first check that the |
| 1480 | // scaling factor is profitable before computing the actual ScaledReg for |
| 1481 | // compile time sake. |
| 1482 | assert((F.isCanonical() || F.Scale != 0)); |
| 1483 | return isAMCompletelyFolded(TTI, MinOffset, MaxOffset, Kind, AccessTy, |
| 1484 | F.BaseGV, F.BaseOffset, F.HasBaseReg, F.Scale); |
| 1485 | } |
| 1486 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1487 | /// Test whether we know how to expand the current formula. |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1488 | static bool isLegalUse(const TargetTransformInfo &TTI, int64_t MinOffset, |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 1489 | int64_t MaxOffset, LSRUse::KindType Kind, |
| 1490 | MemAccessTy AccessTy, GlobalValue *BaseGV, |
| 1491 | int64_t BaseOffset, bool HasBaseReg, int64_t Scale) { |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1492 | // We know how to expand completely foldable formulae. |
| 1493 | return isAMCompletelyFolded(TTI, MinOffset, MaxOffset, Kind, AccessTy, BaseGV, |
| 1494 | BaseOffset, HasBaseReg, Scale) || |
| 1495 | // Or formulae that use a base register produced by a sum of base |
| 1496 | // registers. |
| 1497 | (Scale == 1 && |
| 1498 | isAMCompletelyFolded(TTI, MinOffset, MaxOffset, Kind, AccessTy, |
| 1499 | BaseGV, BaseOffset, true, 0)); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1502 | static bool isLegalUse(const TargetTransformInfo &TTI, int64_t MinOffset, |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 1503 | int64_t MaxOffset, LSRUse::KindType Kind, |
| 1504 | MemAccessTy AccessTy, const Formula &F) { |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 1505 | return isLegalUse(TTI, MinOffset, MaxOffset, Kind, AccessTy, F.BaseGV, |
| 1506 | F.BaseOffset, F.HasBaseReg, F.Scale); |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1509 | static bool isAMCompletelyFolded(const TargetTransformInfo &TTI, |
| 1510 | const LSRUse &LU, const Formula &F) { |
| 1511 | return isAMCompletelyFolded(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, |
| 1512 | LU.AccessTy, F.BaseGV, F.BaseOffset, F.HasBaseReg, |
| 1513 | F.Scale); |
| 1514 | } |
Quentin Colombet | 8aa7abe | 2013-05-31 17:20:29 +0000 | [diff] [blame] | 1515 | |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 1516 | static unsigned getScalingFactorCost(const TargetTransformInfo &TTI, |
| 1517 | const LSRUse &LU, const Formula &F) { |
| 1518 | if (!F.Scale) |
| 1519 | return 0; |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1520 | |
| 1521 | // If the use is not completely folded in that instruction, we will have to |
| 1522 | // pay an extra cost only for scale != 1. |
| 1523 | if (!isAMCompletelyFolded(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, |
| 1524 | LU.AccessTy, F)) |
| 1525 | return F.Scale != 1; |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 1526 | |
| 1527 | switch (LU.Kind) { |
| 1528 | case LSRUse::Address: { |
Quentin Colombet | 145eb97 | 2013-06-19 19:59:41 +0000 | [diff] [blame] | 1529 | // Check the scaling factor cost with both the min and max offsets. |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 1530 | int ScaleCostMinOffset = TTI.getScalingFactorCost( |
| 1531 | LU.AccessTy.MemTy, F.BaseGV, F.BaseOffset + LU.MinOffset, F.HasBaseReg, |
| 1532 | F.Scale, LU.AccessTy.AddrSpace); |
| 1533 | int ScaleCostMaxOffset = TTI.getScalingFactorCost( |
| 1534 | LU.AccessTy.MemTy, F.BaseGV, F.BaseOffset + LU.MaxOffset, F.HasBaseReg, |
| 1535 | F.Scale, LU.AccessTy.AddrSpace); |
Quentin Colombet | 145eb97 | 2013-06-19 19:59:41 +0000 | [diff] [blame] | 1536 | |
| 1537 | assert(ScaleCostMinOffset >= 0 && ScaleCostMaxOffset >= 0 && |
| 1538 | "Legal addressing mode has an illegal cost!"); |
| 1539 | return std::max(ScaleCostMinOffset, ScaleCostMaxOffset); |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 1540 | } |
| 1541 | case LSRUse::ICmpZero: |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 1542 | case LSRUse::Basic: |
| 1543 | case LSRUse::Special: |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1544 | // The use is completely folded, i.e., everything is folded into the |
| 1545 | // instruction. |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 1546 | return 0; |
| 1547 | } |
| 1548 | |
| 1549 | llvm_unreachable("Invalid LSRUse Kind!"); |
| 1550 | } |
| 1551 | |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1552 | static bool isAlwaysFoldable(const TargetTransformInfo &TTI, |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 1553 | LSRUse::KindType Kind, MemAccessTy AccessTy, |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1554 | GlobalValue *BaseGV, int64_t BaseOffset, |
| 1555 | bool HasBaseReg) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1556 | // Fast-path: zero is always foldable. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1557 | if (BaseOffset == 0 && !BaseGV) return true; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1558 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1559 | // Conservatively, create an address with an immediate and a |
| 1560 | // base and a scale. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1561 | int64_t Scale = Kind == LSRUse::ICmpZero ? -1 : 1; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1562 | |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 1563 | // Canonicalize a scale of 1 to a base register if the formula doesn't |
| 1564 | // already have a base register. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1565 | if (!HasBaseReg && Scale == 1) { |
| 1566 | Scale = 0; |
| 1567 | HasBaseReg = true; |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1570 | return isAMCompletelyFolded(TTI, Kind, AccessTy, BaseGV, BaseOffset, |
| 1571 | HasBaseReg, Scale); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1574 | static bool isAlwaysFoldable(const TargetTransformInfo &TTI, |
| 1575 | ScalarEvolution &SE, int64_t MinOffset, |
| 1576 | int64_t MaxOffset, LSRUse::KindType Kind, |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 1577 | MemAccessTy AccessTy, const SCEV *S, |
| 1578 | bool HasBaseReg) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1579 | // Fast-path: zero is always foldable. |
| 1580 | if (S->isZero()) return true; |
| 1581 | |
| 1582 | // Conservatively, create an address with an immediate and a |
| 1583 | // base and a scale. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1584 | int64_t BaseOffset = ExtractImmediate(S, SE); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1585 | GlobalValue *BaseGV = ExtractSymbol(S, SE); |
| 1586 | |
| 1587 | // If there's anything else involved, it's not foldable. |
| 1588 | if (!S->isZero()) return false; |
| 1589 | |
| 1590 | // Fast-path: zero is always foldable. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1591 | if (BaseOffset == 0 && !BaseGV) return true; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1592 | |
| 1593 | // Conservatively, create an address with an immediate and a |
| 1594 | // base and a scale. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1595 | int64_t Scale = Kind == LSRUse::ICmpZero ? -1 : 1; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1596 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1597 | return isAMCompletelyFolded(TTI, MinOffset, MaxOffset, Kind, AccessTy, BaseGV, |
| 1598 | BaseOffset, HasBaseReg, Scale); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Dan Gohman | 297fb8b | 2010-06-19 21:21:39 +0000 | [diff] [blame] | 1601 | namespace { |
| 1602 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1603 | /// An individual increment in a Chain of IV increments. Relate an IV user to |
| 1604 | /// an expression that computes the IV it uses from the IV used by the previous |
| 1605 | /// link in the Chain. |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 1606 | /// |
| 1607 | /// For the head of a chain, IncExpr holds the absolute SCEV expression for the |
| 1608 | /// original IVOperand. The head of the chain's IVOperand is only valid during |
| 1609 | /// chain collection, before LSR replaces IV users. During chain generation, |
| 1610 | /// IncExpr can be used to find the new IVOperand that computes the same |
| 1611 | /// expression. |
| 1612 | struct IVInc { |
| 1613 | Instruction *UserInst; |
| 1614 | Value* IVOperand; |
| 1615 | const SCEV *IncExpr; |
| 1616 | |
| 1617 | IVInc(Instruction *U, Value *O, const SCEV *E): |
| 1618 | UserInst(U), IVOperand(O), IncExpr(E) {} |
| 1619 | }; |
| 1620 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1621 | // The list of IV increments in program order. We typically add the head of a |
| 1622 | // chain without finding subsequent links. |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 1623 | struct IVChain { |
| 1624 | SmallVector<IVInc,1> Incs; |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 1625 | const SCEV *ExprBase; |
| 1626 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1627 | IVChain() : ExprBase(nullptr) {} |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 1628 | |
| 1629 | IVChain(const IVInc &Head, const SCEV *Base) |
| 1630 | : Incs(1, Head), ExprBase(Base) {} |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 1631 | |
| 1632 | typedef SmallVectorImpl<IVInc>::const_iterator const_iterator; |
| 1633 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1634 | // Return the first increment in the chain. |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 1635 | const_iterator begin() const { |
| 1636 | assert(!Incs.empty()); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1637 | return std::next(Incs.begin()); |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 1638 | } |
| 1639 | const_iterator end() const { |
| 1640 | return Incs.end(); |
| 1641 | } |
| 1642 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1643 | // Returns true if this chain contains any increments. |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 1644 | bool hasIncs() const { return Incs.size() >= 2; } |
| 1645 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1646 | // Add an IVInc to the end of this chain. |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 1647 | void add(const IVInc &X) { Incs.push_back(X); } |
| 1648 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1649 | // Returns the last UserInst in the chain. |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 1650 | Instruction *tailUserInst() const { return Incs.back().UserInst; } |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 1651 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1652 | // Returns true if IncExpr can be profitably added to this chain. |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 1653 | bool isProfitableIncrement(const SCEV *OperExpr, |
| 1654 | const SCEV *IncExpr, |
| 1655 | ScalarEvolution&); |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 1656 | }; |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 1657 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1658 | /// Helper for CollectChains to track multiple IV increment uses. Distinguish |
| 1659 | /// between FarUsers that definitely cross IV increments and NearUsers that may |
| 1660 | /// be used between IV increments. |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 1661 | struct ChainUsers { |
| 1662 | SmallPtrSet<Instruction*, 4> FarUsers; |
| 1663 | SmallPtrSet<Instruction*, 4> NearUsers; |
| 1664 | }; |
| 1665 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1666 | /// This class holds state for the main loop strength reduction logic. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1667 | class LSRInstance { |
| 1668 | IVUsers &IU; |
| 1669 | ScalarEvolution &SE; |
| 1670 | DominatorTree &DT; |
Dan Gohman | 607e02b | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 1671 | LoopInfo &LI; |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1672 | const TargetTransformInfo &TTI; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1673 | Loop *const L; |
| 1674 | bool Changed; |
| 1675 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1676 | /// This is the insert position that the current loop's induction variable |
| 1677 | /// increment should be placed. In simple loops, this is the latch block's |
| 1678 | /// terminator. But in more complicated cases, this is a position which will |
| 1679 | /// dominate all the in-loop post-increment users. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1680 | Instruction *IVIncInsertPos; |
| 1681 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1682 | /// Interesting factors between use strides. |
Justin Lebar | 54b0be0 | 2016-11-05 16:47:25 +0000 | [diff] [blame] | 1683 | /// |
| 1684 | /// We explicitly use a SetVector which contains a SmallSet, instead of the |
| 1685 | /// default, a SmallDenseSet, because we need to use the full range of |
| 1686 | /// int64_ts, and there's currently no good way of doing that with |
| 1687 | /// SmallDenseSet. |
| 1688 | SetVector<int64_t, SmallVector<int64_t, 8>, SmallSet<int64_t, 8>> Factors; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1689 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1690 | /// Interesting use types, to facilitate truncation reuse. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1691 | SmallSetVector<Type *, 4> Types; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1692 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1693 | /// The list of interesting uses. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1694 | SmallVector<LSRUse, 16> Uses; |
| 1695 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1696 | /// Track which uses use which register candidates. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1697 | RegUseTracker RegUses; |
| 1698 | |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 1699 | // Limit the number of chains to avoid quadratic behavior. We don't expect to |
| 1700 | // have more than a few IV increment chains in a loop. Missing a Chain falls |
| 1701 | // back to normal LSR behavior for those uses. |
| 1702 | static const unsigned MaxChains = 8; |
| 1703 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1704 | /// IV users can form a chain of IV increments. |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 1705 | SmallVector<IVChain, MaxChains> IVChainVec; |
| 1706 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1707 | /// IV users that belong to profitable IVChains. |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 1708 | SmallPtrSet<Use*, MaxChains> IVIncSet; |
| 1709 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1710 | void OptimizeShadowIV(); |
| 1711 | bool FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse); |
| 1712 | ICmpInst *OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse); |
Dan Gohman | 4c4043c | 2010-05-20 20:05:31 +0000 | [diff] [blame] | 1713 | void OptimizeLoopTermCond(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1714 | |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 1715 | void ChainInstruction(Instruction *UserInst, Instruction *IVOper, |
| 1716 | SmallVectorImpl<ChainUsers> &ChainUsersVec); |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 1717 | void FinalizeChain(IVChain &Chain); |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 1718 | void CollectChains(); |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 1719 | void GenerateIVChain(const IVChain &Chain, SCEVExpander &Rewriter, |
| 1720 | SmallVectorImpl<WeakVH> &DeadInsts); |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 1721 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1722 | void CollectInterestingTypesAndFactors(); |
| 1723 | void CollectFixupsAndInitialFormulae(); |
| 1724 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1725 | // Support for sharing of LSRUses between LSRFixups. |
Benjamin Kramer | 62fb0cf | 2014-03-15 17:17:48 +0000 | [diff] [blame] | 1726 | typedef DenseMap<LSRUse::SCEVUseKindPair, size_t> UseMapTy; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1727 | UseMapTy UseMap; |
| 1728 | |
Dan Gohman | 110ed64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1729 | bool reconcileNewOffset(LSRUse &LU, int64_t NewOffset, bool HasBaseReg, |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 1730 | LSRUse::KindType Kind, MemAccessTy AccessTy); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1731 | |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 1732 | std::pair<size_t, int64_t> getUse(const SCEV *&Expr, LSRUse::KindType Kind, |
| 1733 | MemAccessTy AccessTy); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1734 | |
Dan Gohman | a7b68d6 | 2010-10-07 23:33:43 +0000 | [diff] [blame] | 1735 | void DeleteUse(LSRUse &LU, size_t LUIdx); |
Dan Gohman | 80a9608 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 1736 | |
Dan Gohman | 110ed64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 1737 | LSRUse *FindUseWithSimilarFormula(const Formula &F, const LSRUse &OrigLU); |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 1738 | |
Dan Gohman | 8c16b38 | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1739 | void InsertInitialFormula(const SCEV *S, LSRUse &LU, size_t LUIdx); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1740 | void InsertSupplementalFormula(const SCEV *S, LSRUse &LU, size_t LUIdx); |
| 1741 | void CountRegisters(const Formula &F, size_t LUIdx); |
| 1742 | bool InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F); |
| 1743 | |
| 1744 | void CollectLoopInvariantFixupsAndFormulae(); |
| 1745 | |
| 1746 | void GenerateReassociations(LSRUse &LU, unsigned LUIdx, Formula Base, |
| 1747 | unsigned Depth = 0); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1748 | |
| 1749 | void GenerateReassociationsImpl(LSRUse &LU, unsigned LUIdx, |
| 1750 | const Formula &Base, unsigned Depth, |
| 1751 | size_t Idx, bool IsScaledReg = false); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1752 | void GenerateCombinations(LSRUse &LU, unsigned LUIdx, Formula Base); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1753 | void GenerateSymbolicOffsetsImpl(LSRUse &LU, unsigned LUIdx, |
| 1754 | const Formula &Base, size_t Idx, |
| 1755 | bool IsScaledReg = false); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1756 | void GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx, Formula Base); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 1757 | void GenerateConstantOffsetsImpl(LSRUse &LU, unsigned LUIdx, |
| 1758 | const Formula &Base, |
| 1759 | const SmallVectorImpl<int64_t> &Worklist, |
| 1760 | size_t Idx, bool IsScaledReg = false); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1761 | void GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx, Formula Base); |
| 1762 | void GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx, Formula Base); |
| 1763 | void GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base); |
| 1764 | void GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base); |
| 1765 | void GenerateCrossUseConstantOffsets(); |
| 1766 | void GenerateAllReuseFormulae(); |
| 1767 | |
| 1768 | void FilterOutUndesirableDedicatedRegisters(); |
Dan Gohman | a4eca05 | 2010-05-18 22:51:59 +0000 | [diff] [blame] | 1769 | |
| 1770 | size_t EstimateSearchSpaceComplexity() const; |
Dan Gohman | e9e0873 | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 1771 | void NarrowSearchSpaceByDetectingSupersets(); |
| 1772 | void NarrowSearchSpaceByCollapsingUnrolledCode(); |
Dan Gohman | 002ff89 | 2010-08-29 16:39:22 +0000 | [diff] [blame] | 1773 | void NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters(); |
Dan Gohman | e9e0873 | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 1774 | void NarrowSearchSpaceByPickingWinnerRegs(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1775 | void NarrowSearchSpaceUsingHeuristics(); |
| 1776 | |
| 1777 | void SolveRecurse(SmallVectorImpl<const Formula *> &Solution, |
| 1778 | Cost &SolutionCost, |
| 1779 | SmallVectorImpl<const Formula *> &Workspace, |
| 1780 | const Cost &CurCost, |
| 1781 | const SmallPtrSet<const SCEV *, 16> &CurRegs, |
| 1782 | DenseSet<const SCEV *> &VisitedRegs) const; |
| 1783 | void Solve(SmallVectorImpl<const Formula *> &Solution) const; |
| 1784 | |
Dan Gohman | 607e02b | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 1785 | BasicBlock::iterator |
| 1786 | HoistInsertPosition(BasicBlock::iterator IP, |
| 1787 | const SmallVectorImpl<Instruction *> &Inputs) const; |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 1788 | BasicBlock::iterator |
| 1789 | AdjustInsertPositionForExpand(BasicBlock::iterator IP, |
| 1790 | const LSRFixup &LF, |
| 1791 | const LSRUse &LU, |
| 1792 | SCEVExpander &Rewriter) const; |
Dan Gohman | d2df643 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 1793 | |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 1794 | Value *Expand(const LSRUse &LU, const LSRFixup &LF, |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1795 | const Formula &F, |
Dan Gohman | 8c16b38 | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1796 | BasicBlock::iterator IP, |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1797 | SCEVExpander &Rewriter, |
Dan Gohman | 8c16b38 | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1798 | SmallVectorImpl<WeakVH> &DeadInsts) const; |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 1799 | void RewriteForPHI(PHINode *PN, const LSRUse &LU, const LSRFixup &LF, |
Dan Gohman | 6deab96 | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 1800 | const Formula &F, |
Dan Gohman | 6deab96 | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 1801 | SCEVExpander &Rewriter, |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 1802 | SmallVectorImpl<WeakVH> &DeadInsts) const; |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 1803 | void Rewrite(const LSRUse &LU, const LSRFixup &LF, |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1804 | const Formula &F, |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1805 | SCEVExpander &Rewriter, |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 1806 | SmallVectorImpl<WeakVH> &DeadInsts) const; |
| 1807 | void ImplementSolution(const SmallVectorImpl<const Formula *> &Solution); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1808 | |
Andrew Trick | dc18e38 | 2011-12-13 00:55:33 +0000 | [diff] [blame] | 1809 | public: |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 1810 | LSRInstance(Loop *L, IVUsers &IU, ScalarEvolution &SE, DominatorTree &DT, |
| 1811 | LoopInfo &LI, const TargetTransformInfo &TTI); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1812 | |
| 1813 | bool getChanged() const { return Changed; } |
| 1814 | |
| 1815 | void print_factors_and_types(raw_ostream &OS) const; |
| 1816 | void print_fixups(raw_ostream &OS) const; |
| 1817 | void print_uses(raw_ostream &OS) const; |
| 1818 | void print(raw_ostream &OS) const; |
| 1819 | void dump() const; |
| 1820 | }; |
| 1821 | |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 1822 | } // end anonymous namespace |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1823 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1824 | /// If IV is used in a int-to-float cast inside the loop then try to eliminate |
| 1825 | /// the cast operation. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1826 | void LSRInstance::OptimizeShadowIV() { |
| 1827 | const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L); |
| 1828 | if (isa<SCEVCouldNotCompute>(BackedgeTakenCount)) |
| 1829 | return; |
| 1830 | |
| 1831 | for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); |
| 1832 | UI != E; /* empty */) { |
| 1833 | IVUsers::const_iterator CandidateUI = UI; |
| 1834 | ++UI; |
| 1835 | Instruction *ShadowUse = CandidateUI->getUser(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1836 | Type *DestTy = nullptr; |
Andrew Trick | 858e9f0 | 2011-07-21 01:05:01 +0000 | [diff] [blame] | 1837 | bool IsSigned = false; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1838 | |
| 1839 | /* If shadow use is a int->float cast then insert a second IV |
| 1840 | to eliminate this cast. |
| 1841 | |
| 1842 | for (unsigned i = 0; i < n; ++i) |
| 1843 | foo((double)i); |
| 1844 | |
| 1845 | is transformed into |
| 1846 | |
| 1847 | double d = 0.0; |
| 1848 | for (unsigned i = 0; i < n; ++i, ++d) |
| 1849 | foo(d); |
| 1850 | */ |
Andrew Trick | 858e9f0 | 2011-07-21 01:05:01 +0000 | [diff] [blame] | 1851 | if (UIToFPInst *UCast = dyn_cast<UIToFPInst>(CandidateUI->getUser())) { |
| 1852 | IsSigned = false; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1853 | DestTy = UCast->getDestTy(); |
Andrew Trick | 858e9f0 | 2011-07-21 01:05:01 +0000 | [diff] [blame] | 1854 | } |
| 1855 | else if (SIToFPInst *SCast = dyn_cast<SIToFPInst>(CandidateUI->getUser())) { |
| 1856 | IsSigned = true; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1857 | DestTy = SCast->getDestTy(); |
Andrew Trick | 858e9f0 | 2011-07-21 01:05:01 +0000 | [diff] [blame] | 1858 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1859 | if (!DestTy) continue; |
| 1860 | |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 1861 | // If target does not support DestTy natively then do not apply |
| 1862 | // this transformation. |
| 1863 | if (!TTI.isTypeLegal(DestTy)) continue; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1864 | |
| 1865 | PHINode *PH = dyn_cast<PHINode>(ShadowUse->getOperand(0)); |
| 1866 | if (!PH) continue; |
| 1867 | if (PH->getNumIncomingValues() != 2) continue; |
| 1868 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1869 | Type *SrcTy = PH->getType(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1870 | int Mantissa = DestTy->getFPMantissaWidth(); |
| 1871 | if (Mantissa == -1) continue; |
| 1872 | if ((int)SE.getTypeSizeInBits(SrcTy) > Mantissa) |
| 1873 | continue; |
| 1874 | |
| 1875 | unsigned Entry, Latch; |
| 1876 | if (PH->getIncomingBlock(0) == L->getLoopPreheader()) { |
| 1877 | Entry = 0; |
| 1878 | Latch = 1; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1879 | } else { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1880 | Entry = 1; |
| 1881 | Latch = 0; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1882 | } |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1883 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1884 | ConstantInt *Init = dyn_cast<ConstantInt>(PH->getIncomingValue(Entry)); |
| 1885 | if (!Init) continue; |
Andrew Trick | 858e9f0 | 2011-07-21 01:05:01 +0000 | [diff] [blame] | 1886 | Constant *NewInit = ConstantFP::get(DestTy, IsSigned ? |
Andrew Trick | bd243d0 | 2011-07-21 01:45:54 +0000 | [diff] [blame] | 1887 | (double)Init->getSExtValue() : |
| 1888 | (double)Init->getZExtValue()); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1889 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1890 | BinaryOperator *Incr = |
| 1891 | dyn_cast<BinaryOperator>(PH->getIncomingValue(Latch)); |
| 1892 | if (!Incr) continue; |
| 1893 | if (Incr->getOpcode() != Instruction::Add |
| 1894 | && Incr->getOpcode() != Instruction::Sub) |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1895 | continue; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1896 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1897 | /* Initialize new IV, double d = 0.0 in above example. */ |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1898 | ConstantInt *C = nullptr; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1899 | if (Incr->getOperand(0) == PH) |
| 1900 | C = dyn_cast<ConstantInt>(Incr->getOperand(1)); |
| 1901 | else if (Incr->getOperand(1) == PH) |
| 1902 | C = dyn_cast<ConstantInt>(Incr->getOperand(0)); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1903 | else |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1904 | continue; |
| 1905 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1906 | if (!C) continue; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1907 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1908 | // Ignore negative constants, as the code below doesn't handle them |
| 1909 | // correctly. TODO: Remove this restriction. |
| 1910 | if (!C->getValue().isStrictlyPositive()) continue; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1911 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1912 | /* Add new PHINode. */ |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 1913 | PHINode *NewPH = PHINode::Create(DestTy, 2, "IV.S.", PH); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1914 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1915 | /* create new increment. '++d' in above example. */ |
| 1916 | Constant *CFP = ConstantFP::get(DestTy, C->getZExtValue()); |
| 1917 | BinaryOperator *NewIncr = |
| 1918 | BinaryOperator::Create(Incr->getOpcode() == Instruction::Add ? |
| 1919 | Instruction::FAdd : Instruction::FSub, |
| 1920 | NewPH, CFP, "IV.S.next.", Incr); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1921 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1922 | NewPH->addIncoming(NewInit, PH->getIncomingBlock(Entry)); |
| 1923 | NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch)); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1924 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1925 | /* Remove cast operation */ |
| 1926 | ShadowUse->replaceAllUsesWith(NewPH); |
| 1927 | ShadowUse->eraseFromParent(); |
Dan Gohman | 4c4043c | 2010-05-20 20:05:31 +0000 | [diff] [blame] | 1928 | Changed = true; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1929 | break; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1933 | /// If Cond has an operand that is an expression of an IV, set the IV user and |
| 1934 | /// stride information and return true, otherwise return false. |
Dan Gohman | ab5fb7f | 2010-05-20 19:44:23 +0000 | [diff] [blame] | 1935 | bool LSRInstance::FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse) { |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 1936 | for (IVStrideUse &U : IU) |
| 1937 | if (U.getUser() == Cond) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1938 | // NOTE: we could handle setcc instructions with multiple uses here, but |
| 1939 | // InstCombine does it as well for simple uses, it's not clear that it |
| 1940 | // occurs enough in real life to handle. |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 1941 | CondUse = &U; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1942 | return true; |
| 1943 | } |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1944 | return false; |
Evan Cheng | 133694d | 2007-10-25 09:11:16 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 1947 | /// Rewrite the loop's terminating condition if it uses a max computation. |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1948 | /// |
| 1949 | /// This is a narrow solution to a specific, but acute, problem. For loops |
| 1950 | /// like this: |
| 1951 | /// |
| 1952 | /// i = 0; |
| 1953 | /// do { |
| 1954 | /// p[i] = 0.0; |
| 1955 | /// } while (++i < n); |
| 1956 | /// |
| 1957 | /// the trip count isn't just 'n', because 'n' might not be positive. And |
| 1958 | /// unfortunately this can come up even for loops where the user didn't use |
| 1959 | /// a C do-while loop. For example, seemingly well-behaved top-test loops |
| 1960 | /// will commonly be lowered like this: |
| 1961 | // |
| 1962 | /// if (n > 0) { |
| 1963 | /// i = 0; |
| 1964 | /// do { |
| 1965 | /// p[i] = 0.0; |
| 1966 | /// } while (++i < n); |
| 1967 | /// } |
| 1968 | /// |
| 1969 | /// and then it's possible for subsequent optimization to obscure the if |
| 1970 | /// test in such a way that indvars can't find it. |
| 1971 | /// |
| 1972 | /// When indvars can't find the if test in loops like this, it creates a |
| 1973 | /// max expression, which allows it to give the loop a canonical |
| 1974 | /// induction variable: |
| 1975 | /// |
| 1976 | /// i = 0; |
| 1977 | /// max = n < 1 ? 1 : n; |
| 1978 | /// do { |
| 1979 | /// p[i] = 0.0; |
| 1980 | /// } while (++i != max); |
| 1981 | /// |
| 1982 | /// Canonical induction variables are necessary because the loop passes |
| 1983 | /// are designed around them. The most obvious example of this is the |
| 1984 | /// LoopInfo analysis, which doesn't remember trip count values. It |
| 1985 | /// expects to be able to rediscover the trip count each time it is |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1986 | /// needed, and it does this using a simple analysis that only succeeds if |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1987 | /// the loop has a canonical induction variable. |
| 1988 | /// |
| 1989 | /// However, when it comes time to generate code, the maximum operation |
| 1990 | /// can be quite costly, especially if it's inside of an outer loop. |
| 1991 | /// |
| 1992 | /// This function solves this problem by detecting this type of loop and |
| 1993 | /// rewriting their conditions from ICMP_NE back to ICMP_SLT, and deleting |
| 1994 | /// the instructions for the maximum computation. |
| 1995 | /// |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1996 | ICmpInst *LSRInstance::OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse) { |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 1997 | // Check that the loop matches the pattern we're looking for. |
| 1998 | if (Cond->getPredicate() != CmpInst::ICMP_EQ && |
| 1999 | Cond->getPredicate() != CmpInst::ICMP_NE) |
| 2000 | return Cond; |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2001 | |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2002 | SelectInst *Sel = dyn_cast<SelectInst>(Cond->getOperand(1)); |
| 2003 | if (!Sel || !Sel->hasOneUse()) return Cond; |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2004 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2005 | const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2006 | if (isa<SCEVCouldNotCompute>(BackedgeTakenCount)) |
| 2007 | return Cond; |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 2008 | const SCEV *One = SE.getConstant(BackedgeTakenCount->getType(), 1); |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2009 | |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2010 | // Add one to the backedge-taken count to get the trip count. |
Dan Gohman | 9b7632d | 2010-08-16 15:39:27 +0000 | [diff] [blame] | 2011 | const SCEV *IterationCount = SE.getAddExpr(One, BackedgeTakenCount); |
Dan Gohman | 534ba37 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 2012 | if (IterationCount != SE.getSCEV(Sel)) return Cond; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2013 | |
Dan Gohman | 534ba37 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 2014 | // Check for a max calculation that matches the pattern. There's no check |
| 2015 | // for ICMP_ULE here because the comparison would be with zero, which |
| 2016 | // isn't interesting. |
| 2017 | CmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2018 | const SCEVNAryExpr *Max = nullptr; |
Dan Gohman | 534ba37 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 2019 | if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(BackedgeTakenCount)) { |
| 2020 | Pred = ICmpInst::ICMP_SLE; |
| 2021 | Max = S; |
| 2022 | } else if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(IterationCount)) { |
| 2023 | Pred = ICmpInst::ICMP_SLT; |
| 2024 | Max = S; |
| 2025 | } else if (const SCEVUMaxExpr *U = dyn_cast<SCEVUMaxExpr>(IterationCount)) { |
| 2026 | Pred = ICmpInst::ICMP_ULT; |
| 2027 | Max = U; |
| 2028 | } else { |
| 2029 | // No match; bail. |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2030 | return Cond; |
Dan Gohman | 534ba37 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 2031 | } |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2032 | |
| 2033 | // To handle a max with more than two operands, this optimization would |
| 2034 | // require additional checking and setup. |
| 2035 | if (Max->getNumOperands() != 2) |
| 2036 | return Cond; |
| 2037 | |
| 2038 | const SCEV *MaxLHS = Max->getOperand(0); |
| 2039 | const SCEV *MaxRHS = Max->getOperand(1); |
Dan Gohman | 534ba37 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 2040 | |
| 2041 | // ScalarEvolution canonicalizes constants to the left. For < and >, look |
| 2042 | // for a comparison with 1. For <= and >=, a comparison with zero. |
| 2043 | if (!MaxLHS || |
| 2044 | (ICmpInst::isTrueWhenEqual(Pred) ? !MaxLHS->isZero() : (MaxLHS != One))) |
| 2045 | return Cond; |
| 2046 | |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2047 | // Check the relevant induction variable for conformance to |
| 2048 | // the pattern. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2049 | const SCEV *IV = SE.getSCEV(Cond->getOperand(0)); |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2050 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(IV); |
| 2051 | if (!AR || !AR->isAffine() || |
| 2052 | AR->getStart() != One || |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2053 | AR->getStepRecurrence(SE) != One) |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2054 | return Cond; |
| 2055 | |
| 2056 | assert(AR->getLoop() == L && |
| 2057 | "Loop condition operand is an addrec in a different loop!"); |
| 2058 | |
| 2059 | // Check the right operand of the select, and remember it, as it will |
| 2060 | // be used in the new comparison instruction. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2061 | Value *NewRHS = nullptr; |
Dan Gohman | 534ba37 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 2062 | if (ICmpInst::isTrueWhenEqual(Pred)) { |
| 2063 | // Look for n+1, and grab n. |
| 2064 | if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(1))) |
Jakub Staszak | f6df1e3 | 2013-03-24 09:25:47 +0000 | [diff] [blame] | 2065 | if (ConstantInt *BO1 = dyn_cast<ConstantInt>(BO->getOperand(1))) |
| 2066 | if (BO1->isOne() && SE.getSCEV(BO->getOperand(0)) == MaxRHS) |
| 2067 | NewRHS = BO->getOperand(0); |
Dan Gohman | 534ba37 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 2068 | if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(2))) |
Jakub Staszak | f6df1e3 | 2013-03-24 09:25:47 +0000 | [diff] [blame] | 2069 | if (ConstantInt *BO1 = dyn_cast<ConstantInt>(BO->getOperand(1))) |
| 2070 | if (BO1->isOne() && SE.getSCEV(BO->getOperand(0)) == MaxRHS) |
| 2071 | NewRHS = BO->getOperand(0); |
Dan Gohman | 534ba37 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 2072 | if (!NewRHS) |
| 2073 | return Cond; |
| 2074 | } else if (SE.getSCEV(Sel->getOperand(1)) == MaxRHS) |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2075 | NewRHS = Sel->getOperand(1); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2076 | else if (SE.getSCEV(Sel->getOperand(2)) == MaxRHS) |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2077 | NewRHS = Sel->getOperand(2); |
Dan Gohman | 1081f1a | 2010-06-22 23:07:13 +0000 | [diff] [blame] | 2078 | else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(MaxRHS)) |
| 2079 | NewRHS = SU->getValue(); |
Dan Gohman | 534ba37 | 2010-04-24 03:13:44 +0000 | [diff] [blame] | 2080 | else |
Dan Gohman | 1081f1a | 2010-06-22 23:07:13 +0000 | [diff] [blame] | 2081 | // Max doesn't match expected pattern. |
| 2082 | return Cond; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2083 | |
| 2084 | // Determine the new comparison opcode. It may be signed or unsigned, |
| 2085 | // and the original comparison may be either equality or inequality. |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2086 | if (Cond->getPredicate() == CmpInst::ICMP_EQ) |
| 2087 | Pred = CmpInst::getInversePredicate(Pred); |
| 2088 | |
| 2089 | // Ok, everything looks ok to change the condition into an SLT or SGE and |
| 2090 | // delete the max calculation. |
| 2091 | ICmpInst *NewCond = |
| 2092 | new ICmpInst(Cond, Pred, Cond->getOperand(0), NewRHS, "scmp"); |
| 2093 | |
| 2094 | // Delete the max calculation instructions. |
| 2095 | Cond->replaceAllUsesWith(NewCond); |
| 2096 | CondUse->setUser(NewCond); |
| 2097 | Instruction *Cmp = cast<Instruction>(Sel->getOperand(0)); |
| 2098 | Cond->eraseFromParent(); |
| 2099 | Sel->eraseFromParent(); |
| 2100 | if (Cmp->use_empty()) |
| 2101 | Cmp->eraseFromParent(); |
| 2102 | return NewCond; |
Dan Gohman | 68e7735 | 2008-09-15 21:22:06 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 2105 | /// Change loop terminating condition to use the postinc iv when possible. |
Dan Gohman | 4c4043c | 2010-05-20 20:05:31 +0000 | [diff] [blame] | 2106 | void |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2107 | LSRInstance::OptimizeLoopTermCond() { |
| 2108 | SmallPtrSet<Instruction *, 4> PostIncs; |
| 2109 | |
James Molloy | 196ad08 | 2016-08-15 07:53:03 +0000 | [diff] [blame] | 2110 | // We need a different set of heuristics for rotated and non-rotated loops. |
| 2111 | // If a loop is rotated then the latch is also the backedge, so inserting |
| 2112 | // post-inc expressions just before the latch is ideal. To reduce live ranges |
| 2113 | // it also makes sense to rewrite terminating conditions to use post-inc |
| 2114 | // expressions. |
| 2115 | // |
| 2116 | // If the loop is not rotated then the latch is not a backedge; the latch |
| 2117 | // check is done in the loop head. Adding post-inc expressions before the |
| 2118 | // latch will cause overlapping live-ranges of pre-inc and post-inc expressions |
| 2119 | // in the loop body. In this case we do *not* want to use post-inc expressions |
| 2120 | // in the latch check, and we want to insert post-inc expressions before |
| 2121 | // the backedge. |
Evan Cheng | 85a9f43 | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 2122 | BasicBlock *LatchBlock = L->getLoopLatch(); |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2123 | SmallVector<BasicBlock*, 8> ExitingBlocks; |
| 2124 | L->getExitingBlocks(ExitingBlocks); |
James Molloy | 196ad08 | 2016-08-15 07:53:03 +0000 | [diff] [blame] | 2125 | if (llvm::all_of(ExitingBlocks, [&LatchBlock](const BasicBlock *BB) { |
| 2126 | return LatchBlock != BB; |
| 2127 | })) { |
| 2128 | // The backedge doesn't exit the loop; treat this as a head-tested loop. |
| 2129 | IVIncInsertPos = LatchBlock->getTerminator(); |
| 2130 | return; |
| 2131 | } |
Jim Grosbach | 60f4854 | 2009-11-17 17:53:56 +0000 | [diff] [blame] | 2132 | |
James Molloy | 196ad08 | 2016-08-15 07:53:03 +0000 | [diff] [blame] | 2133 | // Otherwise treat this as a rotated loop. |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2134 | for (BasicBlock *ExitingBlock : ExitingBlocks) { |
Evan Cheng | 85a9f43 | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 2135 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2136 | // Get the terminating condition for the loop if possible. If we |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2137 | // can, we want to change it to use a post-incremented version of its |
| 2138 | // induction variable, to allow coalescing the live ranges for the IV into |
| 2139 | // one register value. |
Evan Cheng | 85a9f43 | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 2140 | |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2141 | BranchInst *TermBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); |
| 2142 | if (!TermBr) |
| 2143 | continue; |
| 2144 | // FIXME: Overly conservative, termination condition could be an 'or' etc.. |
| 2145 | if (TermBr->isUnconditional() || !isa<ICmpInst>(TermBr->getCondition())) |
| 2146 | continue; |
Evan Cheng | 85a9f43 | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 2147 | |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2148 | // Search IVUsesByStride to find Cond's IVUse if there is one. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2149 | IVStrideUse *CondUse = nullptr; |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2150 | ICmpInst *Cond = cast<ICmpInst>(TermBr->getCondition()); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2151 | if (!FindIVUserForCond(Cond, CondUse)) |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2152 | continue; |
| 2153 | |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2154 | // If the trip count is computed in terms of a max (due to ScalarEvolution |
| 2155 | // being unable to find a sufficient guard, for example), change the loop |
| 2156 | // comparison to use SLT or ULT instead of NE. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2157 | // One consequence of doing this now is that it disrupts the count-down |
| 2158 | // optimization. That's not always a bad thing though, because in such |
| 2159 | // cases it may still be worthwhile to avoid a max. |
| 2160 | Cond = OptimizeMax(Cond, CondUse); |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2161 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2162 | // If this exiting block dominates the latch block, it may also use |
| 2163 | // the post-inc value if it won't be shared with other uses. |
| 2164 | // Check for dominance. |
| 2165 | if (!DT.dominates(ExitingBlock, LatchBlock)) |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2166 | continue; |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2167 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2168 | // Conservatively avoid trying to use the post-inc value in non-latch |
| 2169 | // exits if there may be pre-inc users in intervening blocks. |
Dan Gohman | 2d0f96d | 2010-02-14 03:21:49 +0000 | [diff] [blame] | 2170 | if (LatchBlock != ExitingBlock) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2171 | for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) |
| 2172 | // Test if the use is reachable from the exiting block. This dominator |
| 2173 | // query is a conservative approximation of reachability. |
| 2174 | if (&*UI != CondUse && |
| 2175 | !DT.properlyDominates(UI->getUser()->getParent(), ExitingBlock)) { |
| 2176 | // Conservatively assume there may be reuse if the quotient of their |
| 2177 | // strides could be a legal scale. |
Dan Gohman | e637ff5 | 2010-04-19 21:48:58 +0000 | [diff] [blame] | 2178 | const SCEV *A = IU.getStride(*CondUse, L); |
| 2179 | const SCEV *B = IU.getStride(*UI, L); |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2180 | if (!A || !B) continue; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2181 | if (SE.getTypeSizeInBits(A->getType()) != |
| 2182 | SE.getTypeSizeInBits(B->getType())) { |
| 2183 | if (SE.getTypeSizeInBits(A->getType()) > |
| 2184 | SE.getTypeSizeInBits(B->getType())) |
| 2185 | B = SE.getSignExtendExpr(B, A->getType()); |
| 2186 | else |
| 2187 | A = SE.getSignExtendExpr(A, B->getType()); |
| 2188 | } |
| 2189 | if (const SCEVConstant *D = |
Dan Gohman | 4eebb94 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 2190 | dyn_cast_or_null<SCEVConstant>(getExactSDiv(B, A, SE))) { |
Dan Gohman | 86110fa | 2010-05-20 22:25:20 +0000 | [diff] [blame] | 2191 | const ConstantInt *C = D->getValue(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2192 | // Stride of one or negative one can have reuse with non-addresses. |
Dan Gohman | 86110fa | 2010-05-20 22:25:20 +0000 | [diff] [blame] | 2193 | if (C->isOne() || C->isAllOnesValue()) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2194 | goto decline_post_inc; |
| 2195 | // Avoid weird situations. |
Dan Gohman | 86110fa | 2010-05-20 22:25:20 +0000 | [diff] [blame] | 2196 | if (C->getValue().getMinSignedBits() >= 64 || |
| 2197 | C->getValue().isMinSignedValue()) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2198 | goto decline_post_inc; |
| 2199 | // Check for possible scaled-address reuse. |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 2200 | MemAccessTy AccessTy = getAccessType(UI->getUser()); |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 2201 | int64_t Scale = C->getSExtValue(); |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 2202 | if (TTI.isLegalAddressingMode(AccessTy.MemTy, /*BaseGV=*/nullptr, |
| 2203 | /*BaseOffset=*/0, |
| 2204 | /*HasBaseReg=*/false, Scale, |
| 2205 | AccessTy.AddrSpace)) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2206 | goto decline_post_inc; |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 2207 | Scale = -Scale; |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 2208 | if (TTI.isLegalAddressingMode(AccessTy.MemTy, /*BaseGV=*/nullptr, |
| 2209 | /*BaseOffset=*/0, |
| 2210 | /*HasBaseReg=*/false, Scale, |
| 2211 | AccessTy.AddrSpace)) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2212 | goto decline_post_inc; |
| 2213 | } |
| 2214 | } |
| 2215 | |
David Greene | 2330f78 | 2009-12-23 22:58:38 +0000 | [diff] [blame] | 2216 | DEBUG(dbgs() << " Change loop exiting icmp to use postinc iv: " |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2217 | << *Cond << '\n'); |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2218 | |
| 2219 | // It's possible for the setcc instruction to be anywhere in the loop, and |
| 2220 | // possible for it to have multiple users. If it is not immediately before |
| 2221 | // the exiting block branch, move it. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2222 | if (&*++BasicBlock::iterator(Cond) != TermBr) { |
| 2223 | if (Cond->hasOneUse()) { |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2224 | Cond->moveBefore(TermBr); |
| 2225 | } else { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2226 | // Clone the terminating condition and insert into the loopend. |
| 2227 | ICmpInst *OldCond = Cond; |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2228 | Cond = cast<ICmpInst>(Cond->clone()); |
| 2229 | Cond->setName(L->getHeader()->getName() + ".termcond"); |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 2230 | ExitingBlock->getInstList().insert(TermBr->getIterator(), Cond); |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2231 | |
| 2232 | // Clone the IVUse, as the old use still exists! |
Andrew Trick | fc4ccb2 | 2011-06-21 15:43:52 +0000 | [diff] [blame] | 2233 | CondUse = &IU.AddUser(Cond, CondUse->getOperandValToReplace()); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2234 | TermBr->replaceUsesOfWith(OldCond, Cond); |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2235 | } |
Evan Cheng | 85a9f43 | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2238 | // If we get to here, we know that we can transform the setcc instruction to |
| 2239 | // use the post-incremented version of the IV, allowing us to coalesce the |
| 2240 | // live ranges for the IV correctly. |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2241 | CondUse->transformToPostInc(L); |
Evan Cheng | ba4e5da7 | 2009-11-17 18:10:11 +0000 | [diff] [blame] | 2242 | Changed = true; |
| 2243 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2244 | PostIncs.insert(Cond); |
| 2245 | decline_post_inc:; |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2246 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2247 | |
| 2248 | // Determine an insertion point for the loop induction variable increment. It |
| 2249 | // must dominate all the post-inc comparisons we just set up, and it must |
| 2250 | // dominate the loop latch edge. |
| 2251 | IVIncInsertPos = L->getLoopLatch()->getTerminator(); |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 2252 | for (Instruction *Inst : PostIncs) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2253 | BasicBlock *BB = |
| 2254 | DT.findNearestCommonDominator(IVIncInsertPos->getParent(), |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 2255 | Inst->getParent()); |
| 2256 | if (BB == Inst->getParent()) |
| 2257 | IVIncInsertPos = Inst; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2258 | else if (BB != IVIncInsertPos->getParent()) |
| 2259 | IVIncInsertPos = BB->getTerminator(); |
| 2260 | } |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 2263 | /// Determine if the given use can accommodate a fixup at the given offset and |
| 2264 | /// other details. If so, update the use and return true. |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 2265 | bool LSRInstance::reconcileNewOffset(LSRUse &LU, int64_t NewOffset, |
| 2266 | bool HasBaseReg, LSRUse::KindType Kind, |
| 2267 | MemAccessTy AccessTy) { |
Dan Gohman | 110ed64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 2268 | int64_t NewMinOffset = LU.MinOffset; |
| 2269 | int64_t NewMaxOffset = LU.MaxOffset; |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 2270 | MemAccessTy NewAccessTy = AccessTy; |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2271 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2272 | // Check for a mismatched kind. It's tempting to collapse mismatched kinds to |
| 2273 | // something conservative, however this can pessimize in the case that one of |
| 2274 | // the uses will have all its uses outside the loop, for example. |
| 2275 | if (LU.Kind != Kind) |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 2276 | return false; |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 2277 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2278 | // Check for a mismatched access type, and fall back conservatively as needed. |
Dan Gohman | 3265590 | 2010-06-19 21:30:18 +0000 | [diff] [blame] | 2279 | // TODO: Be less conservative when the type is similar and can use the same |
| 2280 | // addressing modes. |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 2281 | if (Kind == LSRUse::Address) { |
| 2282 | if (AccessTy != LU.AccessTy) |
| 2283 | NewAccessTy = MemAccessTy::getUnknown(AccessTy.MemTy->getContext()); |
| 2284 | } |
Dan Gohman | 51ad99d | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2285 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 2286 | // Conservatively assume HasBaseReg is true for now. |
| 2287 | if (NewOffset < LU.MinOffset) { |
| 2288 | if (!isAlwaysFoldable(TTI, Kind, NewAccessTy, /*BaseGV=*/nullptr, |
| 2289 | LU.MaxOffset - NewOffset, HasBaseReg)) |
| 2290 | return false; |
| 2291 | NewMinOffset = NewOffset; |
| 2292 | } else if (NewOffset > LU.MaxOffset) { |
| 2293 | if (!isAlwaysFoldable(TTI, Kind, NewAccessTy, /*BaseGV=*/nullptr, |
| 2294 | NewOffset - LU.MinOffset, HasBaseReg)) |
| 2295 | return false; |
| 2296 | NewMaxOffset = NewOffset; |
| 2297 | } |
| 2298 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2299 | // Update the use. |
Dan Gohman | 110ed64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 2300 | LU.MinOffset = NewMinOffset; |
| 2301 | LU.MaxOffset = NewMaxOffset; |
| 2302 | LU.AccessTy = NewAccessTy; |
Dan Gohman | 29916e0 | 2010-01-21 22:42:49 +0000 | [diff] [blame] | 2303 | return true; |
| 2304 | } |
| 2305 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 2306 | /// Return an LSRUse index and an offset value for a fixup which needs the given |
| 2307 | /// expression, with the given kind and optional access type. Either reuse an |
| 2308 | /// existing use or create a new one, as needed. |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 2309 | std::pair<size_t, int64_t> LSRInstance::getUse(const SCEV *&Expr, |
| 2310 | LSRUse::KindType Kind, |
| 2311 | MemAccessTy AccessTy) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2312 | const SCEV *Copy = Expr; |
| 2313 | int64_t Offset = ExtractImmediate(Expr, SE); |
Evan Cheng | 85a9f43 | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 2314 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2315 | // Basic uses can't accept any offset, for example. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2316 | if (!isAlwaysFoldable(TTI, Kind, AccessTy, /*BaseGV=*/ nullptr, |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 2317 | Offset, /*HasBaseReg=*/ true)) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2318 | Expr = Copy; |
| 2319 | Offset = 0; |
| 2320 | } |
| 2321 | |
| 2322 | std::pair<UseMapTy::iterator, bool> P = |
Benjamin Kramer | 62fb0cf | 2014-03-15 17:17:48 +0000 | [diff] [blame] | 2323 | UseMap.insert(std::make_pair(LSRUse::SCEVUseKindPair(Expr, Kind), 0)); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2324 | if (!P.second) { |
| 2325 | // A use already existed with this base. |
| 2326 | size_t LUIdx = P.first->second; |
| 2327 | LSRUse &LU = Uses[LUIdx]; |
Dan Gohman | 110ed64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 2328 | if (reconcileNewOffset(LU, Offset, /*HasBaseReg=*/true, Kind, AccessTy)) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2329 | // Reuse this use. |
| 2330 | return std::make_pair(LUIdx, Offset); |
| 2331 | } |
| 2332 | |
| 2333 | // Create a new use. |
| 2334 | size_t LUIdx = Uses.size(); |
| 2335 | P.first->second = LUIdx; |
| 2336 | Uses.push_back(LSRUse(Kind, AccessTy)); |
| 2337 | LSRUse &LU = Uses[LUIdx]; |
| 2338 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2339 | LU.MinOffset = Offset; |
| 2340 | LU.MaxOffset = Offset; |
| 2341 | return std::make_pair(LUIdx, Offset); |
| 2342 | } |
| 2343 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 2344 | /// Delete the given use from the Uses list. |
Dan Gohman | a7b68d6 | 2010-10-07 23:33:43 +0000 | [diff] [blame] | 2345 | void LSRInstance::DeleteUse(LSRUse &LU, size_t LUIdx) { |
Dan Gohman | 110ed64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 2346 | if (&LU != &Uses.back()) |
Dan Gohman | 80a9608 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 2347 | std::swap(LU, Uses.back()); |
| 2348 | Uses.pop_back(); |
Dan Gohman | a7b68d6 | 2010-10-07 23:33:43 +0000 | [diff] [blame] | 2349 | |
| 2350 | // Update RegUses. |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 2351 | RegUses.swapAndDropUse(LUIdx, Uses.size()); |
Dan Gohman | 80a9608 | 2010-05-20 15:17:54 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 2354 | /// Look for a use distinct from OrigLU which is has a formula that has the same |
| 2355 | /// registers as the given formula. |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2356 | LSRUse * |
| 2357 | LSRInstance::FindUseWithSimilarFormula(const Formula &OrigF, |
Dan Gohman | 110ed64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 2358 | const LSRUse &OrigLU) { |
| 2359 | // Search all uses for the formula. This could be more clever. |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2360 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 2361 | LSRUse &LU = Uses[LUIdx]; |
Dan Gohman | b6a520d | 2010-08-29 15:27:08 +0000 | [diff] [blame] | 2362 | // Check whether this use is close enough to OrigLU, to see whether it's |
| 2363 | // worthwhile looking through its formulae. |
| 2364 | // Ignore ICmpZero uses because they may contain formulae generated by |
| 2365 | // GenerateICmpZeroScales, in which case adding fixup offsets may |
| 2366 | // be invalid. |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2367 | if (&LU != &OrigLU && |
| 2368 | LU.Kind != LSRUse::ICmpZero && |
| 2369 | LU.Kind == OrigLU.Kind && OrigLU.AccessTy == LU.AccessTy && |
Dan Gohman | 1415208 | 2010-07-15 20:24:58 +0000 | [diff] [blame] | 2370 | LU.WidestFixupType == OrigLU.WidestFixupType && |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2371 | LU.HasFormulaWithSameRegs(OrigF)) { |
Dan Gohman | b6a520d | 2010-08-29 15:27:08 +0000 | [diff] [blame] | 2372 | // Scan through this use's formulae. |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2373 | for (const Formula &F : LU.Formulae) { |
Dan Gohman | b6a520d | 2010-08-29 15:27:08 +0000 | [diff] [blame] | 2374 | // Check to see if this formula has the same registers and symbols |
| 2375 | // as OrigF. |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2376 | if (F.BaseRegs == OrigF.BaseRegs && |
| 2377 | F.ScaledReg == OrigF.ScaledReg && |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 2378 | F.BaseGV == OrigF.BaseGV && |
| 2379 | F.Scale == OrigF.Scale && |
Dan Gohman | 6136e94 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 2380 | F.UnfoldedOffset == OrigF.UnfoldedOffset) { |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 2381 | if (F.BaseOffset == 0) |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2382 | return &LU; |
Dan Gohman | b6a520d | 2010-08-29 15:27:08 +0000 | [diff] [blame] | 2383 | // This is the formula where all the registers and symbols matched; |
| 2384 | // there aren't going to be any others. Since we declined it, we |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 2385 | // can skip the rest of the formulae and proceed to the next LSRUse. |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2386 | break; |
| 2387 | } |
| 2388 | } |
| 2389 | } |
| 2390 | } |
| 2391 | |
Dan Gohman | b6a520d | 2010-08-29 15:27:08 +0000 | [diff] [blame] | 2392 | // Nothing looked good. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2393 | return nullptr; |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2396 | void LSRInstance::CollectInterestingTypesAndFactors() { |
| 2397 | SmallSetVector<const SCEV *, 4> Strides; |
| 2398 | |
Dan Gohman | 2446f57 | 2010-02-19 00:05:23 +0000 | [diff] [blame] | 2399 | // Collect interesting types and strides. |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2400 | SmallVector<const SCEV *, 4> Worklist; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2401 | for (const IVStrideUse &U : IU) { |
| 2402 | const SCEV *Expr = IU.getExpr(U); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2403 | |
| 2404 | // Collect interesting types. |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2405 | Types.insert(SE.getEffectiveSCEVType(Expr->getType())); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2406 | |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2407 | // Add strides for mentioned loops. |
| 2408 | Worklist.push_back(Expr); |
| 2409 | do { |
| 2410 | const SCEV *S = Worklist.pop_back_val(); |
| 2411 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
Andrew Trick | d97b83e | 2012-03-22 22:42:45 +0000 | [diff] [blame] | 2412 | if (AR->getLoop() == L) |
Andrew Trick | e8b4f40 | 2011-12-10 00:25:00 +0000 | [diff] [blame] | 2413 | Strides.insert(AR->getStepRecurrence(SE)); |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2414 | Worklist.push_back(AR->getStart()); |
| 2415 | } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
Dan Gohman | dd41bba | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 2416 | Worklist.append(Add->op_begin(), Add->op_end()); |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 2417 | } |
| 2418 | } while (!Worklist.empty()); |
Dan Gohman | 2446f57 | 2010-02-19 00:05:23 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | // Compute interesting factors from the set of interesting strides. |
| 2422 | for (SmallSetVector<const SCEV *, 4>::const_iterator |
| 2423 | I = Strides.begin(), E = Strides.end(); I != E; ++I) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2424 | for (SmallSetVector<const SCEV *, 4>::const_iterator NewStrideIter = |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 2425 | std::next(I); NewStrideIter != E; ++NewStrideIter) { |
Dan Gohman | 2446f57 | 2010-02-19 00:05:23 +0000 | [diff] [blame] | 2426 | const SCEV *OldStride = *I; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2427 | const SCEV *NewStride = *NewStrideIter; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2428 | |
| 2429 | if (SE.getTypeSizeInBits(OldStride->getType()) != |
| 2430 | SE.getTypeSizeInBits(NewStride->getType())) { |
| 2431 | if (SE.getTypeSizeInBits(OldStride->getType()) > |
| 2432 | SE.getTypeSizeInBits(NewStride->getType())) |
| 2433 | NewStride = SE.getSignExtendExpr(NewStride, OldStride->getType()); |
| 2434 | else |
| 2435 | OldStride = SE.getSignExtendExpr(OldStride, NewStride->getType()); |
| 2436 | } |
| 2437 | if (const SCEVConstant *Factor = |
Dan Gohman | 4eebb94 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 2438 | dyn_cast_or_null<SCEVConstant>(getExactSDiv(NewStride, OldStride, |
| 2439 | SE, true))) { |
Sanjoy Das | 0de2fec | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 2440 | if (Factor->getAPInt().getMinSignedBits() <= 64) |
| 2441 | Factors.insert(Factor->getAPInt().getSExtValue()); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2442 | } else if (const SCEVConstant *Factor = |
Dan Gohman | 8c16b38 | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 2443 | dyn_cast_or_null<SCEVConstant>(getExactSDiv(OldStride, |
| 2444 | NewStride, |
Dan Gohman | 4eebb94 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 2445 | SE, true))) { |
Sanjoy Das | 0de2fec | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 2446 | if (Factor->getAPInt().getMinSignedBits() <= 64) |
| 2447 | Factors.insert(Factor->getAPInt().getSExtValue()); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2448 | } |
| 2449 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2450 | |
| 2451 | // If all uses use the same type, don't bother looking for truncation-based |
| 2452 | // reuse. |
| 2453 | if (Types.size() == 1) |
| 2454 | Types.clear(); |
| 2455 | |
| 2456 | DEBUG(print_factors_and_types(dbgs())); |
| 2457 | } |
| 2458 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 2459 | /// Helper for CollectChains that finds an IV operand (computed by an AddRec in |
| 2460 | /// this loop) within [OI,OE) or returns OE. If IVUsers mapped Instructions to |
| 2461 | /// IVStrideUses, we could partially skip this. |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2462 | static User::op_iterator |
| 2463 | findIVOperand(User::op_iterator OI, User::op_iterator OE, |
| 2464 | Loop *L, ScalarEvolution &SE) { |
| 2465 | for(; OI != OE; ++OI) { |
| 2466 | if (Instruction *Oper = dyn_cast<Instruction>(*OI)) { |
| 2467 | if (!SE.isSCEVable(Oper->getType())) |
| 2468 | continue; |
| 2469 | |
| 2470 | if (const SCEVAddRecExpr *AR = |
| 2471 | dyn_cast<SCEVAddRecExpr>(SE.getSCEV(Oper))) { |
| 2472 | if (AR->getLoop() == L) |
| 2473 | break; |
| 2474 | } |
| 2475 | } |
| 2476 | } |
| 2477 | return OI; |
| 2478 | } |
| 2479 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 2480 | /// IVChain logic must consistenctly peek base TruncInst operands, so wrap it in |
| 2481 | /// a convenient helper. |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2482 | static Value *getWideOperand(Value *Oper) { |
| 2483 | if (TruncInst *Trunc = dyn_cast<TruncInst>(Oper)) |
| 2484 | return Trunc->getOperand(0); |
| 2485 | return Oper; |
| 2486 | } |
| 2487 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 2488 | /// Return true if we allow an IV chain to include both types. |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2489 | static bool isCompatibleIVType(Value *LVal, Value *RVal) { |
| 2490 | Type *LType = LVal->getType(); |
| 2491 | Type *RType = RVal->getType(); |
| 2492 | return (LType == RType) || (LType->isPointerTy() && RType->isPointerTy()); |
| 2493 | } |
| 2494 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 2495 | /// Return an approximation of this SCEV expression's "base", or NULL for any |
| 2496 | /// constant. Returning the expression itself is conservative. Returning a |
| 2497 | /// deeper subexpression is more precise and valid as long as it isn't less |
| 2498 | /// complex than another subexpression. For expressions involving multiple |
| 2499 | /// unscaled values, we need to return the pointer-type SCEVUnknown. This avoids |
| 2500 | /// forming chains across objects, such as: PrevOper==a[i], IVOper==b[i], |
| 2501 | /// IVInc==b-a. |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2502 | /// |
| 2503 | /// Since SCEVUnknown is the rightmost type, and pointers are the rightmost |
| 2504 | /// SCEVUnknown, we simply return the rightmost SCEV operand. |
| 2505 | static const SCEV *getExprBase(const SCEV *S) { |
| 2506 | switch (S->getSCEVType()) { |
| 2507 | default: // uncluding scUnknown. |
| 2508 | return S; |
| 2509 | case scConstant: |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2510 | return nullptr; |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2511 | case scTruncate: |
| 2512 | return getExprBase(cast<SCEVTruncateExpr>(S)->getOperand()); |
| 2513 | case scZeroExtend: |
| 2514 | return getExprBase(cast<SCEVZeroExtendExpr>(S)->getOperand()); |
| 2515 | case scSignExtend: |
| 2516 | return getExprBase(cast<SCEVSignExtendExpr>(S)->getOperand()); |
| 2517 | case scAddExpr: { |
| 2518 | // Skip over scaled operands (scMulExpr) to follow add operands as long as |
| 2519 | // there's nothing more complex. |
| 2520 | // FIXME: not sure if we want to recognize negation. |
| 2521 | const SCEVAddExpr *Add = cast<SCEVAddExpr>(S); |
| 2522 | for (std::reverse_iterator<SCEVAddExpr::op_iterator> I(Add->op_end()), |
| 2523 | E(Add->op_begin()); I != E; ++I) { |
| 2524 | const SCEV *SubExpr = *I; |
| 2525 | if (SubExpr->getSCEVType() == scAddExpr) |
| 2526 | return getExprBase(SubExpr); |
| 2527 | |
| 2528 | if (SubExpr->getSCEVType() != scMulExpr) |
| 2529 | return SubExpr; |
| 2530 | } |
| 2531 | return S; // all operands are scaled, be conservative. |
| 2532 | } |
| 2533 | case scAddRecExpr: |
| 2534 | return getExprBase(cast<SCEVAddRecExpr>(S)->getStart()); |
| 2535 | } |
| 2536 | } |
| 2537 | |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2538 | /// Return true if the chain increment is profitable to expand into a loop |
| 2539 | /// invariant value, which may require its own register. A profitable chain |
| 2540 | /// increment will be an offset relative to the same base. We allow such offsets |
| 2541 | /// to potentially be used as chain increment as long as it's not obviously |
| 2542 | /// expensive to expand using real instructions. |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 2543 | bool IVChain::isProfitableIncrement(const SCEV *OperExpr, |
| 2544 | const SCEV *IncExpr, |
| 2545 | ScalarEvolution &SE) { |
| 2546 | // Aggressively form chains when -stress-ivchain. |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2547 | if (StressIVChain) |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 2548 | return true; |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2549 | |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2550 | // Do not replace a constant offset from IV head with a nonconstant IV |
| 2551 | // increment. |
| 2552 | if (!isa<SCEVConstant>(IncExpr)) { |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 2553 | const SCEV *HeadExpr = SE.getSCEV(getWideOperand(Incs[0].IVOperand)); |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2554 | if (isa<SCEVConstant>(SE.getMinusSCEV(OperExpr, HeadExpr))) |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 2555 | return false; |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
| 2558 | SmallPtrSet<const SCEV*, 8> Processed; |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 2559 | return !isHighCostExpansion(IncExpr, Processed, SE); |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | /// Return true if the number of registers needed for the chain is estimated to |
| 2563 | /// be less than the number required for the individual IV users. First prohibit |
| 2564 | /// any IV users that keep the IV live across increments (the Users set should |
| 2565 | /// be empty). Next count the number and type of increments in the chain. |
| 2566 | /// |
| 2567 | /// Chaining IVs can lead to considerable code bloat if ISEL doesn't |
| 2568 | /// effectively use postinc addressing modes. Only consider it profitable it the |
| 2569 | /// increments can be computed in fewer registers when chained. |
| 2570 | /// |
| 2571 | /// TODO: Consider IVInc free if it's already used in another chains. |
| 2572 | static bool |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 2573 | isProfitableChain(IVChain &Chain, SmallPtrSetImpl<Instruction*> &Users, |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 2574 | ScalarEvolution &SE, const TargetTransformInfo &TTI) { |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2575 | if (StressIVChain) |
| 2576 | return true; |
| 2577 | |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 2578 | if (!Chain.hasIncs()) |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2579 | return false; |
| 2580 | |
| 2581 | if (!Users.empty()) { |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 2582 | DEBUG(dbgs() << "Chain: " << *Chain.Incs[0].UserInst << " users:\n"; |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 2583 | for (Instruction *Inst : Users) { |
| 2584 | dbgs() << " " << *Inst << "\n"; |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2585 | }); |
| 2586 | return false; |
| 2587 | } |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 2588 | assert(!Chain.Incs.empty() && "empty IV chains are not allowed"); |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2589 | |
| 2590 | // The chain itself may require a register, so intialize cost to 1. |
| 2591 | int cost = 1; |
| 2592 | |
| 2593 | // A complete chain likely eliminates the need for keeping the original IV in |
| 2594 | // a register. LSR does not currently know how to form a complete chain unless |
| 2595 | // the header phi already exists. |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 2596 | if (isa<PHINode>(Chain.tailUserInst()) |
| 2597 | && SE.getSCEV(Chain.tailUserInst()) == Chain.Incs[0].IncExpr) { |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2598 | --cost; |
| 2599 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2600 | const SCEV *LastIncExpr = nullptr; |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2601 | unsigned NumConstIncrements = 0; |
| 2602 | unsigned NumVarIncrements = 0; |
| 2603 | unsigned NumReusedIncrements = 0; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2604 | for (const IVInc &Inc : Chain) { |
| 2605 | if (Inc.IncExpr->isZero()) |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2606 | continue; |
| 2607 | |
| 2608 | // Incrementing by zero or some constant is neutral. We assume constants can |
| 2609 | // be folded into an addressing mode or an add's immediate operand. |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2610 | if (isa<SCEVConstant>(Inc.IncExpr)) { |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2611 | ++NumConstIncrements; |
| 2612 | continue; |
| 2613 | } |
| 2614 | |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2615 | if (Inc.IncExpr == LastIncExpr) |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2616 | ++NumReusedIncrements; |
| 2617 | else |
| 2618 | ++NumVarIncrements; |
| 2619 | |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2620 | LastIncExpr = Inc.IncExpr; |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2621 | } |
| 2622 | // An IV chain with a single increment is handled by LSR's postinc |
| 2623 | // uses. However, a chain with multiple increments requires keeping the IV's |
| 2624 | // value live longer than it needs to be if chained. |
| 2625 | if (NumConstIncrements > 1) |
| 2626 | --cost; |
| 2627 | |
| 2628 | // Materializing increment expressions in the preheader that didn't exist in |
| 2629 | // the original code may cost a register. For example, sign-extended array |
| 2630 | // indices can produce ridiculous increments like this: |
| 2631 | // IV + ((sext i32 (2 * %s) to i64) + (-1 * (sext i32 %s to i64))) |
| 2632 | cost += NumVarIncrements; |
| 2633 | |
| 2634 | // Reusing variable increments likely saves a register to hold the multiple of |
| 2635 | // the stride. |
| 2636 | cost -= NumReusedIncrements; |
| 2637 | |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 2638 | DEBUG(dbgs() << "Chain: " << *Chain.Incs[0].UserInst << " Cost: " << cost |
| 2639 | << "\n"); |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 2640 | |
| 2641 | return cost < 0; |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 2644 | /// Add this IV user to an existing chain or make it the head of a new chain. |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2645 | void LSRInstance::ChainInstruction(Instruction *UserInst, Instruction *IVOper, |
| 2646 | SmallVectorImpl<ChainUsers> &ChainUsersVec) { |
| 2647 | // When IVs are used as types of varying widths, they are generally converted |
| 2648 | // to a wider type with some uses remaining narrow under a (free) trunc. |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 2649 | Value *const NextIV = getWideOperand(IVOper); |
| 2650 | const SCEV *const OperExpr = SE.getSCEV(NextIV); |
| 2651 | const SCEV *const OperExprBase = getExprBase(OperExpr); |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2652 | |
| 2653 | // Visit all existing chains. Check if its IVOper can be computed as a |
| 2654 | // profitable loop invariant increment from the last link in the Chain. |
| 2655 | unsigned ChainIdx = 0, NChains = IVChainVec.size(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2656 | const SCEV *LastIncExpr = nullptr; |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2657 | for (; ChainIdx < NChains; ++ChainIdx) { |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 2658 | IVChain &Chain = IVChainVec[ChainIdx]; |
| 2659 | |
| 2660 | // Prune the solution space aggressively by checking that both IV operands |
| 2661 | // are expressions that operate on the same unscaled SCEVUnknown. This |
| 2662 | // "base" will be canceled by the subsequent getMinusSCEV call. Checking |
| 2663 | // first avoids creating extra SCEV expressions. |
| 2664 | if (!StressIVChain && Chain.ExprBase != OperExprBase) |
| 2665 | continue; |
| 2666 | |
| 2667 | Value *PrevIV = getWideOperand(Chain.Incs.back().IVOperand); |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2668 | if (!isCompatibleIVType(PrevIV, NextIV)) |
| 2669 | continue; |
| 2670 | |
Andrew Trick | 356a896 | 2012-03-26 20:28:35 +0000 | [diff] [blame] | 2671 | // A phi node terminates a chain. |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 2672 | if (isa<PHINode>(UserInst) && isa<PHINode>(Chain.tailUserInst())) |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2673 | continue; |
| 2674 | |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 2675 | // The increment must be loop-invariant so it can be kept in a register. |
| 2676 | const SCEV *PrevExpr = SE.getSCEV(PrevIV); |
| 2677 | const SCEV *IncExpr = SE.getMinusSCEV(OperExpr, PrevExpr); |
| 2678 | if (!SE.isLoopInvariant(IncExpr, L)) |
| 2679 | continue; |
| 2680 | |
| 2681 | if (Chain.isProfitableIncrement(OperExpr, IncExpr, SE)) { |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2682 | LastIncExpr = IncExpr; |
| 2683 | break; |
| 2684 | } |
| 2685 | } |
| 2686 | // If we haven't found a chain, create a new one, unless we hit the max. Don't |
| 2687 | // bother for phi nodes, because they must be last in the chain. |
| 2688 | if (ChainIdx == NChains) { |
| 2689 | if (isa<PHINode>(UserInst)) |
| 2690 | return; |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2691 | if (NChains >= MaxChains && !StressIVChain) { |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2692 | DEBUG(dbgs() << "IV Chain Limit\n"); |
| 2693 | return; |
| 2694 | } |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 2695 | LastIncExpr = OperExpr; |
Andrew Trick | b9c822a | 2012-01-20 21:23:40 +0000 | [diff] [blame] | 2696 | // IVUsers may have skipped over sign/zero extensions. We don't currently |
| 2697 | // attempt to form chains involving extensions unless they can be hoisted |
| 2698 | // into this loop's AddRec. |
| 2699 | if (!isa<SCEVAddRecExpr>(LastIncExpr)) |
| 2700 | return; |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2701 | ++NChains; |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 2702 | IVChainVec.push_back(IVChain(IVInc(UserInst, IVOper, LastIncExpr), |
| 2703 | OperExprBase)); |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2704 | ChainUsersVec.resize(NChains); |
Jakob Stoklund Olesen | 293673d | 2012-04-25 18:01:32 +0000 | [diff] [blame] | 2705 | DEBUG(dbgs() << "IV Chain#" << ChainIdx << " Head: (" << *UserInst |
| 2706 | << ") IV=" << *LastIncExpr << "\n"); |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 2707 | } else { |
Jakob Stoklund Olesen | 293673d | 2012-04-25 18:01:32 +0000 | [diff] [blame] | 2708 | DEBUG(dbgs() << "IV Chain#" << ChainIdx << " Inc: (" << *UserInst |
| 2709 | << ") IV+" << *LastIncExpr << "\n"); |
Jakob Stoklund Olesen | c90abc8 | 2012-04-26 23:33:11 +0000 | [diff] [blame] | 2710 | // Add this IV user to the end of the chain. |
| 2711 | IVChainVec[ChainIdx].add(IVInc(UserInst, IVOper, LastIncExpr)); |
| 2712 | } |
Andrew Trick | bc70590 | 2013-02-09 01:11:01 +0000 | [diff] [blame] | 2713 | IVChain &Chain = IVChainVec[ChainIdx]; |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2714 | |
| 2715 | SmallPtrSet<Instruction*,4> &NearUsers = ChainUsersVec[ChainIdx].NearUsers; |
| 2716 | // This chain's NearUsers become FarUsers. |
| 2717 | if (!LastIncExpr->isZero()) { |
| 2718 | ChainUsersVec[ChainIdx].FarUsers.insert(NearUsers.begin(), |
| 2719 | NearUsers.end()); |
| 2720 | NearUsers.clear(); |
| 2721 | } |
| 2722 | |
| 2723 | // All other uses of IVOperand become near uses of the chain. |
| 2724 | // We currently ignore intermediate values within SCEV expressions, assuming |
| 2725 | // they will eventually be used be the current chain, or can be computed |
| 2726 | // from one of the chain increments. To be more precise we could |
| 2727 | // transitively follow its user and only add leaf IV users to the set. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2728 | for (User *U : IVOper->users()) { |
| 2729 | Instruction *OtherUse = dyn_cast<Instruction>(U); |
Andrew Trick | bc70590 | 2013-02-09 01:11:01 +0000 | [diff] [blame] | 2730 | if (!OtherUse) |
Andrew Trick | e51feea | 2012-03-26 18:03:16 +0000 | [diff] [blame] | 2731 | continue; |
Andrew Trick | bc70590 | 2013-02-09 01:11:01 +0000 | [diff] [blame] | 2732 | // Uses in the chain will no longer be uses if the chain is formed. |
| 2733 | // Include the head of the chain in this iteration (not Chain.begin()). |
| 2734 | IVChain::const_iterator IncIter = Chain.Incs.begin(); |
| 2735 | IVChain::const_iterator IncEnd = Chain.Incs.end(); |
| 2736 | for( ; IncIter != IncEnd; ++IncIter) { |
| 2737 | if (IncIter->UserInst == OtherUse) |
| 2738 | break; |
| 2739 | } |
| 2740 | if (IncIter != IncEnd) |
| 2741 | continue; |
| 2742 | |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2743 | if (SE.isSCEVable(OtherUse->getType()) |
| 2744 | && !isa<SCEVUnknown>(SE.getSCEV(OtherUse)) |
| 2745 | && IU.isIVUserOrOperand(OtherUse)) { |
| 2746 | continue; |
| 2747 | } |
Andrew Trick | e51feea | 2012-03-26 18:03:16 +0000 | [diff] [blame] | 2748 | NearUsers.insert(OtherUse); |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | // Since this user is part of the chain, it's no longer considered a use |
| 2752 | // of the chain. |
| 2753 | ChainUsersVec[ChainIdx].FarUsers.erase(UserInst); |
| 2754 | } |
| 2755 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 2756 | /// Populate the vector of Chains. |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2757 | /// |
| 2758 | /// This decreases ILP at the architecture level. Targets with ample registers, |
| 2759 | /// multiple memory ports, and no register renaming probably don't want |
| 2760 | /// this. However, such targets should probably disable LSR altogether. |
| 2761 | /// |
| 2762 | /// The job of LSR is to make a reasonable choice of induction variables across |
| 2763 | /// the loop. Subsequent passes can easily "unchain" computation exposing more |
| 2764 | /// ILP *within the loop* if the target wants it. |
| 2765 | /// |
| 2766 | /// Finding the best IV chain is potentially a scheduling problem. Since LSR |
| 2767 | /// will not reorder memory operations, it will recognize this as a chain, but |
| 2768 | /// will generate redundant IV increments. Ideally this would be corrected later |
| 2769 | /// by a smart scheduler: |
| 2770 | /// = A[i] |
| 2771 | /// = A[i+x] |
| 2772 | /// A[i] = |
| 2773 | /// A[i+x] = |
| 2774 | /// |
| 2775 | /// TODO: Walk the entire domtree within this loop, not just the path to the |
| 2776 | /// loop latch. This will discover chains on side paths, but requires |
| 2777 | /// maintaining multiple copies of the Chains state. |
| 2778 | void LSRInstance::CollectChains() { |
Jakob Stoklund Olesen | 293673d | 2012-04-25 18:01:32 +0000 | [diff] [blame] | 2779 | DEBUG(dbgs() << "Collecting IV Chains.\n"); |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2780 | SmallVector<ChainUsers, 8> ChainUsersVec; |
| 2781 | |
| 2782 | SmallVector<BasicBlock *,8> LatchPath; |
| 2783 | BasicBlock *LoopHeader = L->getHeader(); |
| 2784 | for (DomTreeNode *Rung = DT.getNode(L->getLoopLatch()); |
| 2785 | Rung->getBlock() != LoopHeader; Rung = Rung->getIDom()) { |
| 2786 | LatchPath.push_back(Rung->getBlock()); |
| 2787 | } |
| 2788 | LatchPath.push_back(LoopHeader); |
| 2789 | |
| 2790 | // Walk the instruction stream from the loop header to the loop latch. |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 2791 | for (BasicBlock *BB : reverse(LatchPath)) { |
| 2792 | for (Instruction &I : *BB) { |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2793 | // Skip instructions that weren't seen by IVUsers analysis. |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 2794 | if (isa<PHINode>(I) || !IU.isIVUserOrOperand(&I)) |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2795 | continue; |
| 2796 | |
| 2797 | // Ignore users that are part of a SCEV expression. This way we only |
| 2798 | // consider leaf IV Users. This effectively rediscovers a portion of |
| 2799 | // IVUsers analysis but in program order this time. |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 2800 | if (SE.isSCEVable(I.getType()) && !isa<SCEVUnknown>(SE.getSCEV(&I))) |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2801 | continue; |
| 2802 | |
| 2803 | // Remove this instruction from any NearUsers set it may be in. |
| 2804 | for (unsigned ChainIdx = 0, NChains = IVChainVec.size(); |
| 2805 | ChainIdx < NChains; ++ChainIdx) { |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 2806 | ChainUsersVec[ChainIdx].NearUsers.erase(&I); |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2807 | } |
| 2808 | // Search for operands that can be chained. |
| 2809 | SmallPtrSet<Instruction*, 4> UniqueOperands; |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 2810 | User::op_iterator IVOpEnd = I.op_end(); |
| 2811 | User::op_iterator IVOpIter = findIVOperand(I.op_begin(), IVOpEnd, L, SE); |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2812 | while (IVOpIter != IVOpEnd) { |
| 2813 | Instruction *IVOpInst = cast<Instruction>(*IVOpIter); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 2814 | if (UniqueOperands.insert(IVOpInst).second) |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 2815 | ChainInstruction(&I, IVOpInst, ChainUsersVec); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 2816 | IVOpIter = findIVOperand(std::next(IVOpIter), IVOpEnd, L, SE); |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2817 | } |
| 2818 | } // Continue walking down the instructions. |
| 2819 | } // Continue walking down the domtree. |
| 2820 | // Visit phi backedges to determine if the chain can generate the IV postinc. |
| 2821 | for (BasicBlock::iterator I = L->getHeader()->begin(); |
| 2822 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 2823 | if (!SE.isSCEVable(PN->getType())) |
| 2824 | continue; |
| 2825 | |
| 2826 | Instruction *IncV = |
| 2827 | dyn_cast<Instruction>(PN->getIncomingValueForBlock(L->getLoopLatch())); |
| 2828 | if (IncV) |
| 2829 | ChainInstruction(PN, IncV, ChainUsersVec); |
| 2830 | } |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2831 | // Remove any unprofitable chains. |
| 2832 | unsigned ChainIdx = 0; |
| 2833 | for (unsigned UsersIdx = 0, NChains = IVChainVec.size(); |
| 2834 | UsersIdx < NChains; ++UsersIdx) { |
| 2835 | if (!isProfitableChain(IVChainVec[UsersIdx], |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 2836 | ChainUsersVec[UsersIdx].FarUsers, SE, TTI)) |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2837 | continue; |
| 2838 | // Preserve the chain at UsesIdx. |
| 2839 | if (ChainIdx != UsersIdx) |
| 2840 | IVChainVec[ChainIdx] = IVChainVec[UsersIdx]; |
| 2841 | FinalizeChain(IVChainVec[ChainIdx]); |
| 2842 | ++ChainIdx; |
| 2843 | } |
| 2844 | IVChainVec.resize(ChainIdx); |
| 2845 | } |
| 2846 | |
| 2847 | void LSRInstance::FinalizeChain(IVChain &Chain) { |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 2848 | assert(!Chain.Incs.empty() && "empty IV chains are not allowed"); |
| 2849 | DEBUG(dbgs() << "Final Chain: " << *Chain.Incs[0].UserInst << "\n"); |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2850 | |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2851 | for (const IVInc &Inc : Chain) { |
Evgeny Stupachenko | 8efbe6a | 2016-11-21 21:55:03 +0000 | [diff] [blame] | 2852 | DEBUG(dbgs() << " Inc: " << *Inc.UserInst << "\n"); |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 2853 | auto UseI = find(Inc.UserInst->operands(), Inc.IVOperand); |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2854 | assert(UseI != Inc.UserInst->op_end() && "cannot find IV operand"); |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2855 | IVIncSet.insert(UseI); |
| 2856 | } |
| 2857 | } |
| 2858 | |
| 2859 | /// Return true if the IVInc can be folded into an addressing mode. |
| 2860 | static bool canFoldIVIncExpr(const SCEV *IncExpr, Instruction *UserInst, |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 2861 | Value *Operand, const TargetTransformInfo &TTI) { |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2862 | const SCEVConstant *IncConst = dyn_cast<SCEVConstant>(IncExpr); |
| 2863 | if (!IncConst || !isAddressUse(UserInst, Operand)) |
| 2864 | return false; |
| 2865 | |
Sanjoy Das | 0de2fec | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 2866 | if (IncConst->getAPInt().getMinSignedBits() > 64) |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2867 | return false; |
| 2868 | |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 2869 | MemAccessTy AccessTy = getAccessType(UserInst); |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2870 | int64_t IncOffset = IncConst->getValue()->getSExtValue(); |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 2871 | if (!isAlwaysFoldable(TTI, LSRUse::Address, AccessTy, /*BaseGV=*/nullptr, |
| 2872 | IncOffset, /*HaseBaseReg=*/false)) |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2873 | return false; |
| 2874 | |
| 2875 | return true; |
| 2876 | } |
| 2877 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 2878 | /// Generate an add or subtract for each IVInc in a chain to materialize the IV |
| 2879 | /// user's operand from the previous IV user's operand. |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2880 | void LSRInstance::GenerateIVChain(const IVChain &Chain, SCEVExpander &Rewriter, |
| 2881 | SmallVectorImpl<WeakVH> &DeadInsts) { |
| 2882 | // Find the new IVOperand for the head of the chain. It may have been replaced |
| 2883 | // by LSR. |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 2884 | const IVInc &Head = Chain.Incs[0]; |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2885 | User::op_iterator IVOpEnd = Head.UserInst->op_end(); |
Andrew Trick | f3a2544 | 2013-03-19 05:10:27 +0000 | [diff] [blame] | 2886 | // findIVOperand returns IVOpEnd if it can no longer find a valid IV user. |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2887 | User::op_iterator IVOpIter = findIVOperand(Head.UserInst->op_begin(), |
| 2888 | IVOpEnd, L, SE); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2889 | Value *IVSrc = nullptr; |
Andrew Trick | f3a2544 | 2013-03-19 05:10:27 +0000 | [diff] [blame] | 2890 | while (IVOpIter != IVOpEnd) { |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2891 | IVSrc = getWideOperand(*IVOpIter); |
| 2892 | |
| 2893 | // If this operand computes the expression that the chain needs, we may use |
| 2894 | // it. (Check this after setting IVSrc which is used below.) |
| 2895 | // |
| 2896 | // Note that if Head.IncExpr is wider than IVSrc, then this phi is too |
| 2897 | // narrow for the chain, so we can no longer use it. We do allow using a |
| 2898 | // wider phi, assuming the LSR checked for free truncation. In that case we |
| 2899 | // should already have a truncate on this operand such that |
| 2900 | // getSCEV(IVSrc) == IncExpr. |
| 2901 | if (SE.getSCEV(*IVOpIter) == Head.IncExpr |
| 2902 | || SE.getSCEV(IVSrc) == Head.IncExpr) { |
| 2903 | break; |
| 2904 | } |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 2905 | IVOpIter = findIVOperand(std::next(IVOpIter), IVOpEnd, L, SE); |
Andrew Trick | f3a2544 | 2013-03-19 05:10:27 +0000 | [diff] [blame] | 2906 | } |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2907 | if (IVOpIter == IVOpEnd) { |
| 2908 | // Gracefully give up on this chain. |
| 2909 | DEBUG(dbgs() << "Concealed chain head: " << *Head.UserInst << "\n"); |
| 2910 | return; |
| 2911 | } |
| 2912 | |
| 2913 | DEBUG(dbgs() << "Generate chain at: " << *IVSrc << "\n"); |
| 2914 | Type *IVTy = IVSrc->getType(); |
| 2915 | Type *IntTy = SE.getEffectiveSCEVType(IVTy); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2916 | const SCEV *LeftOverExpr = nullptr; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2917 | for (const IVInc &Inc : Chain) { |
| 2918 | Instruction *InsertPt = Inc.UserInst; |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2919 | if (isa<PHINode>(InsertPt)) |
| 2920 | InsertPt = L->getLoopLatch()->getTerminator(); |
| 2921 | |
| 2922 | // IVOper will replace the current IV User's operand. IVSrc is the IV |
| 2923 | // value currently held in a register. |
| 2924 | Value *IVOper = IVSrc; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2925 | if (!Inc.IncExpr->isZero()) { |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2926 | // IncExpr was the result of subtraction of two narrow values, so must |
| 2927 | // be signed. |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2928 | const SCEV *IncExpr = SE.getNoopOrSignExtend(Inc.IncExpr, IntTy); |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2929 | LeftOverExpr = LeftOverExpr ? |
| 2930 | SE.getAddExpr(LeftOverExpr, IncExpr) : IncExpr; |
| 2931 | } |
| 2932 | if (LeftOverExpr && !LeftOverExpr->isZero()) { |
| 2933 | // Expand the IV increment. |
| 2934 | Rewriter.clearPostInc(); |
| 2935 | Value *IncV = Rewriter.expandCodeFor(LeftOverExpr, IntTy, InsertPt); |
| 2936 | const SCEV *IVOperExpr = SE.getAddExpr(SE.getUnknown(IVSrc), |
| 2937 | SE.getUnknown(IncV)); |
| 2938 | IVOper = Rewriter.expandCodeFor(IVOperExpr, IVTy, InsertPt); |
| 2939 | |
| 2940 | // If an IV increment can't be folded, use it as the next IV value. |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2941 | if (!canFoldIVIncExpr(LeftOverExpr, Inc.UserInst, Inc.IVOperand, TTI)) { |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2942 | assert(IVTy == IVOper->getType() && "inconsistent IV increment type"); |
| 2943 | IVSrc = IVOper; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2944 | LeftOverExpr = nullptr; |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2945 | } |
| 2946 | } |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2947 | Type *OperTy = Inc.IVOperand->getType(); |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2948 | if (IVTy != OperTy) { |
| 2949 | assert(SE.getTypeSizeInBits(IVTy) >= SE.getTypeSizeInBits(OperTy) && |
| 2950 | "cannot extend a chained IV"); |
| 2951 | IRBuilder<> Builder(InsertPt); |
| 2952 | IVOper = Builder.CreateTruncOrBitCast(IVOper, OperTy, "lsr.chain"); |
| 2953 | } |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2954 | Inc.UserInst->replaceUsesOfWith(Inc.IVOperand, IVOper); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 2955 | DeadInsts.emplace_back(Inc.IVOperand); |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2956 | } |
| 2957 | // If LSR created a new, wider phi, we may also replace its postinc. We only |
| 2958 | // do this if we also found a wide value for the head of the chain. |
Jakob Stoklund Olesen | a0337d7 | 2012-04-26 23:33:09 +0000 | [diff] [blame] | 2959 | if (isa<PHINode>(Chain.tailUserInst())) { |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2960 | for (BasicBlock::iterator I = L->getHeader()->begin(); |
| 2961 | PHINode *Phi = dyn_cast<PHINode>(I); ++I) { |
| 2962 | if (!isCompatibleIVType(Phi, IVSrc)) |
| 2963 | continue; |
| 2964 | Instruction *PostIncV = dyn_cast<Instruction>( |
| 2965 | Phi->getIncomingValueForBlock(L->getLoopLatch())); |
| 2966 | if (!PostIncV || (SE.getSCEV(PostIncV) != SE.getSCEV(IVSrc))) |
| 2967 | continue; |
| 2968 | Value *IVOper = IVSrc; |
| 2969 | Type *PostIncTy = PostIncV->getType(); |
| 2970 | if (IVTy != PostIncTy) { |
| 2971 | assert(PostIncTy->isPointerTy() && "mixing int/ptr IV types"); |
| 2972 | IRBuilder<> Builder(L->getLoopLatch()->getTerminator()); |
| 2973 | Builder.SetCurrentDebugLocation(PostIncV->getDebugLoc()); |
| 2974 | IVOper = Builder.CreatePointerCast(IVSrc, PostIncTy, "lsr.chain"); |
| 2975 | } |
| 2976 | Phi->replaceUsesOfWith(PostIncV, IVOper); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 2977 | DeadInsts.emplace_back(PostIncV); |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2978 | } |
| 2979 | } |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2982 | void LSRInstance::CollectFixupsAndInitialFormulae() { |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2983 | for (const IVStrideUse &U : IU) { |
| 2984 | Instruction *UserInst = U.getUser(); |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2985 | // Skip IV users that are part of profitable IV Chains. |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 2986 | User::op_iterator UseI = |
| 2987 | find(UserInst->operands(), U.getOperandValToReplace()); |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 2988 | assert(UseI != UserInst->op_end() && "cannot find IV operand"); |
| 2989 | if (IVIncSet.count(UseI)) |
| 2990 | continue; |
| 2991 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2992 | LSRUse::KindType Kind = LSRUse::Basic; |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 2993 | MemAccessTy AccessTy; |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 2994 | if (isAddressUse(UserInst, U.getOperandValToReplace())) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2995 | Kind = LSRUse::Address; |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 2996 | AccessTy = getAccessType(UserInst); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 2999 | const SCEV *S = IU.getExpr(U); |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 3000 | PostIncLoopSet TmpPostIncLoops = U.getPostIncLoops(); |
| 3001 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3002 | // Equality (== and !=) ICmps are special. We can rewrite (i == N) as |
| 3003 | // (N - i == 0), and this allows (N - i) to be the expression that we work |
| 3004 | // with rather than just N or i, so we can consider the register |
| 3005 | // requirements for both N and i at the same time. Limiting this code to |
| 3006 | // equality icmps is not a problem because all interesting loops use |
| 3007 | // equality icmps, thanks to IndVarSimplify. |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 3008 | if (ICmpInst *CI = dyn_cast<ICmpInst>(UserInst)) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3009 | if (CI->isEquality()) { |
| 3010 | // Swap the operands if needed to put the OperandValToReplace on the |
| 3011 | // left, for consistency. |
| 3012 | Value *NV = CI->getOperand(1); |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 3013 | if (NV == U.getOperandValToReplace()) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3014 | CI->setOperand(1, CI->getOperand(0)); |
| 3015 | CI->setOperand(0, NV); |
Dan Gohman | ee2fea3 | 2010-05-20 19:26:52 +0000 | [diff] [blame] | 3016 | NV = CI->getOperand(1); |
Dan Gohman | fdf9874 | 2010-05-20 19:16:03 +0000 | [diff] [blame] | 3017 | Changed = true; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | // x == y --> x - y == 0 |
| 3021 | const SCEV *N = SE.getSCEV(NV); |
Andrew Trick | 57243da | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 3022 | if (SE.isLoopInvariant(N, L) && isSafeToExpand(N, SE)) { |
Dan Gohman | 3268e4d | 2011-05-18 21:02:18 +0000 | [diff] [blame] | 3023 | // S is normalized, so normalize N before folding it into S |
| 3024 | // to keep the result normalized. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3025 | N = TransformForPostIncUse(Normalize, N, CI, nullptr, |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 3026 | TmpPostIncLoops, SE, DT); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3027 | Kind = LSRUse::ICmpZero; |
| 3028 | S = SE.getMinusSCEV(N, S); |
| 3029 | } |
| 3030 | |
| 3031 | // -1 and the negations of all interesting strides (except the negation |
| 3032 | // of -1) are now also interesting. |
| 3033 | for (size_t i = 0, e = Factors.size(); i != e; ++i) |
| 3034 | if (Factors[i] != -1) |
| 3035 | Factors.insert(-(uint64_t)Factors[i]); |
| 3036 | Factors.insert(-1); |
| 3037 | } |
| 3038 | |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 3039 | // Get or create an LSRUse. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3040 | std::pair<size_t, int64_t> P = getUse(S, Kind, AccessTy); |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 3041 | size_t LUIdx = P.first; |
| 3042 | int64_t Offset = P.second; |
| 3043 | LSRUse &LU = Uses[LUIdx]; |
| 3044 | |
| 3045 | // Record the fixup. |
| 3046 | LSRFixup &LF = LU.getNewFixup(); |
| 3047 | LF.UserInst = UserInst; |
| 3048 | LF.OperandValToReplace = U.getOperandValToReplace(); |
| 3049 | LF.PostIncLoops = TmpPostIncLoops; |
| 3050 | LF.Offset = Offset; |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 3051 | LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L); |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 3052 | |
Dan Gohman | 1415208 | 2010-07-15 20:24:58 +0000 | [diff] [blame] | 3053 | if (!LU.WidestFixupType || |
| 3054 | SE.getTypeSizeInBits(LU.WidestFixupType) < |
| 3055 | SE.getTypeSizeInBits(LF.OperandValToReplace->getType())) |
| 3056 | LU.WidestFixupType = LF.OperandValToReplace->getType(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3057 | |
| 3058 | // If this is the first use of this LSRUse, give it a formula. |
| 3059 | if (LU.Formulae.empty()) { |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 3060 | InsertInitialFormula(S, LU, LUIdx); |
| 3061 | CountRegisters(LU.Formulae.back(), LUIdx); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3062 | } |
| 3063 | } |
| 3064 | |
| 3065 | DEBUG(print_fixups(dbgs())); |
| 3066 | } |
| 3067 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3068 | /// Insert a formula for the given expression into the given use, separating out |
| 3069 | /// loop-variant portions from loop-invariant and loop-computable portions. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3070 | void |
Dan Gohman | 8c16b38 | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 3071 | LSRInstance::InsertInitialFormula(const SCEV *S, LSRUse &LU, size_t LUIdx) { |
Andrew Trick | 57243da | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 3072 | // Mark uses whose expressions cannot be expanded. |
| 3073 | if (!isSafeToExpand(S, SE)) |
| 3074 | LU.RigidFormula = true; |
| 3075 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3076 | Formula F; |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 3077 | F.initialMatch(S, L, SE); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3078 | bool Inserted = InsertFormula(LU, LUIdx, F); |
| 3079 | assert(Inserted && "Initial formula already exists!"); (void)Inserted; |
| 3080 | } |
| 3081 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3082 | /// Insert a simple single-register formula for the given expression into the |
| 3083 | /// given use. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3084 | void |
| 3085 | LSRInstance::InsertSupplementalFormula(const SCEV *S, |
| 3086 | LSRUse &LU, size_t LUIdx) { |
| 3087 | Formula F; |
| 3088 | F.BaseRegs.push_back(S); |
Chandler Carruth | 7e31c8f | 2013-01-12 23:46:04 +0000 | [diff] [blame] | 3089 | F.HasBaseReg = true; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3090 | bool Inserted = InsertFormula(LU, LUIdx, F); |
| 3091 | assert(Inserted && "Supplemental formula already exists!"); (void)Inserted; |
| 3092 | } |
| 3093 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3094 | /// Note which registers are used by the given formula, updating RegUses. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3095 | void LSRInstance::CountRegisters(const Formula &F, size_t LUIdx) { |
| 3096 | if (F.ScaledReg) |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 3097 | RegUses.countRegister(F.ScaledReg, LUIdx); |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3098 | for (const SCEV *BaseReg : F.BaseRegs) |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 3099 | RegUses.countRegister(BaseReg, LUIdx); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3102 | /// If the given formula has not yet been inserted, add it to the list, and |
| 3103 | /// return true. Return false otherwise. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3104 | bool LSRInstance::InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F) { |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3105 | // Do not insert formula that we will not be able to expand. |
| 3106 | assert(isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F) && |
| 3107 | "Formula is illegal"); |
Dan Gohman | 8c16b38 | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 3108 | if (!LU.InsertFormula(F)) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3109 | return false; |
| 3110 | |
| 3111 | CountRegisters(F, LUIdx); |
| 3112 | return true; |
| 3113 | } |
| 3114 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3115 | /// Check for other uses of loop-invariant values which we're tracking. These |
| 3116 | /// other uses will pin these values in registers, making them less profitable |
| 3117 | /// for elimination. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3118 | /// TODO: This currently misses non-constant addrec step registers. |
| 3119 | /// TODO: Should this give more weight to users inside the loop? |
| 3120 | void |
| 3121 | LSRInstance::CollectLoopInvariantFixupsAndFormulae() { |
| 3122 | SmallVector<const SCEV *, 8> Worklist(RegUses.begin(), RegUses.end()); |
Andrew Trick | dd925ad | 2014-10-25 19:59:30 +0000 | [diff] [blame] | 3123 | SmallPtrSet<const SCEV *, 32> Visited; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3124 | |
| 3125 | while (!Worklist.empty()) { |
| 3126 | const SCEV *S = Worklist.pop_back_val(); |
| 3127 | |
Andrew Trick | 9ccbed5 | 2014-10-25 19:42:07 +0000 | [diff] [blame] | 3128 | // Don't process the same SCEV twice |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 3129 | if (!Visited.insert(S).second) |
Andrew Trick | 9ccbed5 | 2014-10-25 19:42:07 +0000 | [diff] [blame] | 3130 | continue; |
| 3131 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3132 | if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S)) |
Dan Gohman | dd41bba | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 3133 | Worklist.append(N->op_begin(), N->op_end()); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3134 | else if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S)) |
| 3135 | Worklist.push_back(C->getOperand()); |
| 3136 | else if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) { |
| 3137 | Worklist.push_back(D->getLHS()); |
| 3138 | Worklist.push_back(D->getRHS()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3139 | } else if (const SCEVUnknown *US = dyn_cast<SCEVUnknown>(S)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3140 | const Value *V = US->getValue(); |
Dan Gohman | 67b4403 | 2010-06-04 23:16:05 +0000 | [diff] [blame] | 3141 | if (const Instruction *Inst = dyn_cast<Instruction>(V)) { |
| 3142 | // Look for instructions defined outside the loop. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3143 | if (L->contains(Inst)) continue; |
Dan Gohman | 67b4403 | 2010-06-04 23:16:05 +0000 | [diff] [blame] | 3144 | } else if (isa<UndefValue>(V)) |
| 3145 | // Undef doesn't have a live range, so it doesn't matter. |
| 3146 | continue; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3147 | for (const Use &U : V->uses()) { |
| 3148 | const Instruction *UserInst = dyn_cast<Instruction>(U.getUser()); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3149 | // Ignore non-instructions. |
| 3150 | if (!UserInst) |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 3151 | continue; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3152 | // Ignore instructions in other functions (as can happen with |
| 3153 | // Constants). |
| 3154 | if (UserInst->getParent()->getParent() != L->getHeader()->getParent()) |
Dan Gohman | 045f819 | 2010-01-22 00:46:49 +0000 | [diff] [blame] | 3155 | continue; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3156 | // Ignore instructions not dominated by the loop. |
| 3157 | const BasicBlock *UseBB = !isa<PHINode>(UserInst) ? |
| 3158 | UserInst->getParent() : |
| 3159 | cast<PHINode>(UserInst)->getIncomingBlock( |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3160 | PHINode::getIncomingValueNumForOperand(U.getOperandNo())); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3161 | if (!DT.dominates(L->getHeader(), UseBB)) |
| 3162 | continue; |
David Majnemer | b222184 | 2015-11-08 05:04:07 +0000 | [diff] [blame] | 3163 | // Don't bother if the instruction is in a BB which ends in an EHPad. |
| 3164 | if (UseBB->getTerminator()->isEHPad()) |
| 3165 | continue; |
David Majnemer | bba1739 | 2017-01-13 22:24:27 +0000 | [diff] [blame] | 3166 | // Don't bother rewriting PHIs in catchswitch blocks. |
| 3167 | if (isa<CatchSwitchInst>(UserInst->getParent()->getTerminator())) |
| 3168 | continue; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3169 | // Ignore uses which are part of other SCEV expressions, to avoid |
| 3170 | // analyzing them multiple times. |
Dan Gohman | 42ec4eb | 2010-04-09 19:12:34 +0000 | [diff] [blame] | 3171 | if (SE.isSCEVable(UserInst->getType())) { |
| 3172 | const SCEV *UserS = SE.getSCEV(const_cast<Instruction *>(UserInst)); |
| 3173 | // If the user is a no-op, look through to its uses. |
| 3174 | if (!isa<SCEVUnknown>(UserS)) |
| 3175 | continue; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3176 | if (UserS == US) { |
Dan Gohman | 42ec4eb | 2010-04-09 19:12:34 +0000 | [diff] [blame] | 3177 | Worklist.push_back( |
| 3178 | SE.getUnknown(const_cast<Instruction *>(UserInst))); |
| 3179 | continue; |
| 3180 | } |
| 3181 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3182 | // Ignore icmp instructions which are already being analyzed. |
| 3183 | if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UserInst)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3184 | unsigned OtherIdx = !U.getOperandNo(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3185 | Value *OtherOp = const_cast<Value *>(ICI->getOperand(OtherIdx)); |
Dan Gohman | afd6db9 | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 3186 | if (SE.hasComputableLoopEvolution(SE.getSCEV(OtherOp), L)) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3187 | continue; |
| 3188 | } |
| 3189 | |
Matt Arsenault | 427a0fd | 2015-08-15 00:53:06 +0000 | [diff] [blame] | 3190 | std::pair<size_t, int64_t> P = getUse( |
| 3191 | S, LSRUse::Basic, MemAccessTy()); |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 3192 | size_t LUIdx = P.first; |
| 3193 | int64_t Offset = P.second; |
| 3194 | LSRUse &LU = Uses[LUIdx]; |
| 3195 | LSRFixup &LF = LU.getNewFixup(); |
| 3196 | LF.UserInst = const_cast<Instruction *>(UserInst); |
| 3197 | LF.OperandValToReplace = U; |
| 3198 | LF.Offset = Offset; |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 3199 | LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L); |
Dan Gohman | 1415208 | 2010-07-15 20:24:58 +0000 | [diff] [blame] | 3200 | if (!LU.WidestFixupType || |
| 3201 | SE.getTypeSizeInBits(LU.WidestFixupType) < |
| 3202 | SE.getTypeSizeInBits(LF.OperandValToReplace->getType())) |
| 3203 | LU.WidestFixupType = LF.OperandValToReplace->getType(); |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 3204 | InsertSupplementalFormula(US, LU, LUIdx); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3205 | CountRegisters(LU.Formulae.back(), Uses.size() - 1); |
| 3206 | break; |
| 3207 | } |
| 3208 | } |
| 3209 | } |
| 3210 | } |
| 3211 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3212 | /// Split S into subexpressions which can be pulled out into separate |
| 3213 | /// registers. If C is non-null, multiply each subexpression by C. |
Andrew Trick | c803706 | 2012-07-17 05:30:37 +0000 | [diff] [blame] | 3214 | /// |
| 3215 | /// Return remainder expression after factoring the subexpressions captured by |
| 3216 | /// Ops. If Ops is complete, return NULL. |
| 3217 | static const SCEV *CollectSubexprs(const SCEV *S, const SCEVConstant *C, |
| 3218 | SmallVectorImpl<const SCEV *> &Ops, |
| 3219 | const Loop *L, |
| 3220 | ScalarEvolution &SE, |
| 3221 | unsigned Depth = 0) { |
| 3222 | // Arbitrarily cap recursion to protect compile time. |
| 3223 | if (Depth >= 3) |
| 3224 | return S; |
| 3225 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3226 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 3227 | // Break out add operands. |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3228 | for (const SCEV *S : Add->operands()) { |
| 3229 | const SCEV *Remainder = CollectSubexprs(S, C, Ops, L, SE, Depth+1); |
Andrew Trick | c803706 | 2012-07-17 05:30:37 +0000 | [diff] [blame] | 3230 | if (Remainder) |
| 3231 | Ops.push_back(C ? SE.getMulExpr(C, Remainder) : Remainder); |
| 3232 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3233 | return nullptr; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3234 | } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 3235 | // Split a non-zero base out of an addrec. |
Alexandros Lamprineas | 0ee3ec2 | 2016-11-09 08:53:07 +0000 | [diff] [blame] | 3236 | if (AR->getStart()->isZero() || !AR->isAffine()) |
Andrew Trick | c803706 | 2012-07-17 05:30:37 +0000 | [diff] [blame] | 3237 | return S; |
| 3238 | |
| 3239 | const SCEV *Remainder = CollectSubexprs(AR->getStart(), |
| 3240 | C, Ops, L, SE, Depth+1); |
| 3241 | // Split the non-zero AddRec unless it is part of a nested recurrence that |
| 3242 | // does not pertain to this loop. |
| 3243 | if (Remainder && (AR->getLoop() == L || !isa<SCEVAddRecExpr>(Remainder))) { |
| 3244 | Ops.push_back(C ? SE.getMulExpr(C, Remainder) : Remainder); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3245 | Remainder = nullptr; |
Andrew Trick | c803706 | 2012-07-17 05:30:37 +0000 | [diff] [blame] | 3246 | } |
| 3247 | if (Remainder != AR->getStart()) { |
| 3248 | if (!Remainder) |
| 3249 | Remainder = SE.getConstant(AR->getType(), 0); |
| 3250 | return SE.getAddRecExpr(Remainder, |
| 3251 | AR->getStepRecurrence(SE), |
| 3252 | AR->getLoop(), |
| 3253 | //FIXME: AR->getNoWrapFlags(SCEV::FlagNW) |
| 3254 | SCEV::FlagAnyWrap); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3255 | } |
| 3256 | } else if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 3257 | // Break (C * (a + b + c)) into C*a + C*b + C*c. |
Andrew Trick | c803706 | 2012-07-17 05:30:37 +0000 | [diff] [blame] | 3258 | if (Mul->getNumOperands() != 2) |
| 3259 | return S; |
| 3260 | if (const SCEVConstant *Op0 = |
| 3261 | dyn_cast<SCEVConstant>(Mul->getOperand(0))) { |
| 3262 | C = C ? cast<SCEVConstant>(SE.getMulExpr(C, Op0)) : Op0; |
| 3263 | const SCEV *Remainder = |
| 3264 | CollectSubexprs(Mul->getOperand(1), C, Ops, L, SE, Depth+1); |
| 3265 | if (Remainder) |
| 3266 | Ops.push_back(SE.getMulExpr(C, Remainder)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3267 | return nullptr; |
Andrew Trick | c803706 | 2012-07-17 05:30:37 +0000 | [diff] [blame] | 3268 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3269 | } |
Andrew Trick | c803706 | 2012-07-17 05:30:37 +0000 | [diff] [blame] | 3270 | return S; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3273 | /// \brief Helper function for LSRInstance::GenerateReassociations. |
| 3274 | void LSRInstance::GenerateReassociationsImpl(LSRUse &LU, unsigned LUIdx, |
| 3275 | const Formula &Base, |
| 3276 | unsigned Depth, size_t Idx, |
| 3277 | bool IsScaledReg) { |
| 3278 | const SCEV *BaseReg = IsScaledReg ? Base.ScaledReg : Base.BaseRegs[Idx]; |
| 3279 | SmallVector<const SCEV *, 8> AddOps; |
| 3280 | const SCEV *Remainder = CollectSubexprs(BaseReg, nullptr, AddOps, L, SE); |
| 3281 | if (Remainder) |
| 3282 | AddOps.push_back(Remainder); |
| 3283 | |
| 3284 | if (AddOps.size() == 1) |
| 3285 | return; |
| 3286 | |
| 3287 | for (SmallVectorImpl<const SCEV *>::const_iterator J = AddOps.begin(), |
| 3288 | JE = AddOps.end(); |
| 3289 | J != JE; ++J) { |
| 3290 | |
| 3291 | // Loop-variant "unknown" values are uninteresting; we won't be able to |
| 3292 | // do anything meaningful with them. |
| 3293 | if (isa<SCEVUnknown>(*J) && !SE.isLoopInvariant(*J, L)) |
| 3294 | continue; |
| 3295 | |
| 3296 | // Don't pull a constant into a register if the constant could be folded |
| 3297 | // into an immediate field. |
| 3298 | if (isAlwaysFoldable(TTI, SE, LU.MinOffset, LU.MaxOffset, LU.Kind, |
| 3299 | LU.AccessTy, *J, Base.getNumRegs() > 1)) |
| 3300 | continue; |
| 3301 | |
| 3302 | // Collect all operands except *J. |
| 3303 | SmallVector<const SCEV *, 8> InnerAddOps( |
| 3304 | ((const SmallVector<const SCEV *, 8> &)AddOps).begin(), J); |
| 3305 | InnerAddOps.append(std::next(J), |
| 3306 | ((const SmallVector<const SCEV *, 8> &)AddOps).end()); |
| 3307 | |
| 3308 | // Don't leave just a constant behind in a register if the constant could |
| 3309 | // be folded into an immediate field. |
| 3310 | if (InnerAddOps.size() == 1 && |
| 3311 | isAlwaysFoldable(TTI, SE, LU.MinOffset, LU.MaxOffset, LU.Kind, |
| 3312 | LU.AccessTy, InnerAddOps[0], Base.getNumRegs() > 1)) |
| 3313 | continue; |
| 3314 | |
| 3315 | const SCEV *InnerSum = SE.getAddExpr(InnerAddOps); |
| 3316 | if (InnerSum->isZero()) |
| 3317 | continue; |
| 3318 | Formula F = Base; |
| 3319 | |
| 3320 | // Add the remaining pieces of the add back into the new formula. |
| 3321 | const SCEVConstant *InnerSumSC = dyn_cast<SCEVConstant>(InnerSum); |
| 3322 | if (InnerSumSC && SE.getTypeSizeInBits(InnerSumSC->getType()) <= 64 && |
| 3323 | TTI.isLegalAddImmediate((uint64_t)F.UnfoldedOffset + |
| 3324 | InnerSumSC->getValue()->getZExtValue())) { |
| 3325 | F.UnfoldedOffset = |
| 3326 | (uint64_t)F.UnfoldedOffset + InnerSumSC->getValue()->getZExtValue(); |
| 3327 | if (IsScaledReg) |
| 3328 | F.ScaledReg = nullptr; |
| 3329 | else |
| 3330 | F.BaseRegs.erase(F.BaseRegs.begin() + Idx); |
| 3331 | } else if (IsScaledReg) |
| 3332 | F.ScaledReg = InnerSum; |
| 3333 | else |
| 3334 | F.BaseRegs[Idx] = InnerSum; |
| 3335 | |
| 3336 | // Add J as its own register, or an unfolded immediate. |
| 3337 | const SCEVConstant *SC = dyn_cast<SCEVConstant>(*J); |
| 3338 | if (SC && SE.getTypeSizeInBits(SC->getType()) <= 64 && |
| 3339 | TTI.isLegalAddImmediate((uint64_t)F.UnfoldedOffset + |
| 3340 | SC->getValue()->getZExtValue())) |
| 3341 | F.UnfoldedOffset = |
| 3342 | (uint64_t)F.UnfoldedOffset + SC->getValue()->getZExtValue(); |
| 3343 | else |
| 3344 | F.BaseRegs.push_back(*J); |
| 3345 | // We may have changed the number of register in base regs, adjust the |
| 3346 | // formula accordingly. |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 3347 | F.canonicalize(); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3348 | |
| 3349 | if (InsertFormula(LU, LUIdx, F)) |
| 3350 | // If that formula hadn't been seen before, recurse to find more like |
| 3351 | // it. |
| 3352 | GenerateReassociations(LU, LUIdx, LU.Formulae.back(), Depth + 1); |
| 3353 | } |
| 3354 | } |
| 3355 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3356 | /// Split out subexpressions from adds and the bases of addrecs. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3357 | void LSRInstance::GenerateReassociations(LSRUse &LU, unsigned LUIdx, |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3358 | Formula Base, unsigned Depth) { |
| 3359 | assert(Base.isCanonical() && "Input must be in the canonical form"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3360 | // Arbitrarily cap recursion to protect compile time. |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3361 | if (Depth >= 3) |
| 3362 | return; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3363 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3364 | for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) |
| 3365 | GenerateReassociationsImpl(LU, LUIdx, Base, Depth, i); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3366 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3367 | if (Base.Scale == 1) |
| 3368 | GenerateReassociationsImpl(LU, LUIdx, Base, Depth, |
| 3369 | /* Idx */ -1, /* IsScaledReg */ true); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3372 | /// Generate a formula consisting of all of the loop-dominating registers added |
| 3373 | /// into a single register. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3374 | void LSRInstance::GenerateCombinations(LSRUse &LU, unsigned LUIdx, |
Dan Gohman | e4e51a6 | 2010-02-14 18:51:39 +0000 | [diff] [blame] | 3375 | Formula Base) { |
Dan Gohman | 8b0a419 | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3376 | // This method is only interesting on a plurality of registers. |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3377 | if (Base.BaseRegs.size() + (Base.Scale == 1) <= 1) |
| 3378 | return; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3379 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3380 | // Flatten the representation, i.e., reg1 + 1*reg2 => reg1 + reg2, before |
| 3381 | // processing the formula. |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 3382 | Base.unscale(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3383 | Formula F = Base; |
| 3384 | F.BaseRegs.clear(); |
| 3385 | SmallVector<const SCEV *, 4> Ops; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3386 | for (const SCEV *BaseReg : Base.BaseRegs) { |
Dan Gohman | 20d9ce2 | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 3387 | if (SE.properlyDominates(BaseReg, L->getHeader()) && |
Dan Gohman | afd6db9 | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 3388 | !SE.hasComputableLoopEvolution(BaseReg, L)) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3389 | Ops.push_back(BaseReg); |
| 3390 | else |
| 3391 | F.BaseRegs.push_back(BaseReg); |
| 3392 | } |
| 3393 | if (Ops.size() > 1) { |
Dan Gohman | bb7d522 | 2010-02-14 18:50:49 +0000 | [diff] [blame] | 3394 | const SCEV *Sum = SE.getAddExpr(Ops); |
| 3395 | // TODO: If Sum is zero, it probably means ScalarEvolution missed an |
| 3396 | // opportunity to fold something. For now, just ignore such cases |
Dan Gohman | 8b0a419 | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3397 | // rather than proceed with zero in a register. |
Dan Gohman | bb7d522 | 2010-02-14 18:50:49 +0000 | [diff] [blame] | 3398 | if (!Sum->isZero()) { |
| 3399 | F.BaseRegs.push_back(Sum); |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 3400 | F.canonicalize(); |
Dan Gohman | bb7d522 | 2010-02-14 18:50:49 +0000 | [diff] [blame] | 3401 | (void)InsertFormula(LU, LUIdx, F); |
| 3402 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3403 | } |
| 3404 | } |
| 3405 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3406 | /// \brief Helper function for LSRInstance::GenerateSymbolicOffsets. |
| 3407 | void LSRInstance::GenerateSymbolicOffsetsImpl(LSRUse &LU, unsigned LUIdx, |
| 3408 | const Formula &Base, size_t Idx, |
| 3409 | bool IsScaledReg) { |
| 3410 | const SCEV *G = IsScaledReg ? Base.ScaledReg : Base.BaseRegs[Idx]; |
| 3411 | GlobalValue *GV = ExtractSymbol(G, SE); |
| 3412 | if (G->isZero() || !GV) |
| 3413 | return; |
| 3414 | Formula F = Base; |
| 3415 | F.BaseGV = GV; |
| 3416 | if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F)) |
| 3417 | return; |
| 3418 | if (IsScaledReg) |
| 3419 | F.ScaledReg = G; |
| 3420 | else |
| 3421 | F.BaseRegs[Idx] = G; |
| 3422 | (void)InsertFormula(LU, LUIdx, F); |
| 3423 | } |
| 3424 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3425 | /// Generate reuse formulae using symbolic offsets. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3426 | void LSRInstance::GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx, |
| 3427 | Formula Base) { |
| 3428 | // We can't add a symbolic offset if the address already contains one. |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 3429 | if (Base.BaseGV) return; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3430 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3431 | for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) |
| 3432 | GenerateSymbolicOffsetsImpl(LU, LUIdx, Base, i); |
| 3433 | if (Base.Scale == 1) |
| 3434 | GenerateSymbolicOffsetsImpl(LU, LUIdx, Base, /* Idx */ -1, |
| 3435 | /* IsScaledReg */ true); |
| 3436 | } |
| 3437 | |
| 3438 | /// \brief Helper function for LSRInstance::GenerateConstantOffsets. |
| 3439 | void LSRInstance::GenerateConstantOffsetsImpl( |
| 3440 | LSRUse &LU, unsigned LUIdx, const Formula &Base, |
| 3441 | const SmallVectorImpl<int64_t> &Worklist, size_t Idx, bool IsScaledReg) { |
| 3442 | const SCEV *G = IsScaledReg ? Base.ScaledReg : Base.BaseRegs[Idx]; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3443 | for (int64_t Offset : Worklist) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3444 | Formula F = Base; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3445 | F.BaseOffset = (uint64_t)Base.BaseOffset - Offset; |
| 3446 | if (isLegalUse(TTI, LU.MinOffset - Offset, LU.MaxOffset - Offset, LU.Kind, |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3447 | LU.AccessTy, F)) { |
| 3448 | // Add the offset to the base register. |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3449 | const SCEV *NewG = SE.getAddExpr(SE.getConstant(G->getType(), Offset), G); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3450 | // If it cancelled out, drop the base register, otherwise update it. |
| 3451 | if (NewG->isZero()) { |
| 3452 | if (IsScaledReg) { |
| 3453 | F.Scale = 0; |
| 3454 | F.ScaledReg = nullptr; |
| 3455 | } else |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 3456 | F.deleteBaseReg(F.BaseRegs[Idx]); |
| 3457 | F.canonicalize(); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3458 | } else if (IsScaledReg) |
| 3459 | F.ScaledReg = NewG; |
| 3460 | else |
| 3461 | F.BaseRegs[Idx] = NewG; |
| 3462 | |
| 3463 | (void)InsertFormula(LU, LUIdx, F); |
| 3464 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3465 | } |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3466 | |
| 3467 | int64_t Imm = ExtractImmediate(G, SE); |
| 3468 | if (G->isZero() || Imm == 0) |
| 3469 | return; |
| 3470 | Formula F = Base; |
| 3471 | F.BaseOffset = (uint64_t)F.BaseOffset + Imm; |
| 3472 | if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F)) |
| 3473 | return; |
| 3474 | if (IsScaledReg) |
| 3475 | F.ScaledReg = G; |
| 3476 | else |
| 3477 | F.BaseRegs[Idx] = G; |
| 3478 | (void)InsertFormula(LU, LUIdx, F); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | /// GenerateConstantOffsets - Generate reuse formulae using symbolic offsets. |
| 3482 | void LSRInstance::GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx, |
| 3483 | Formula Base) { |
| 3484 | // TODO: For now, just add the min and max offset, because it usually isn't |
| 3485 | // worthwhile looking at everything inbetween. |
Dan Gohman | 4afd412 | 2010-07-15 15:14:45 +0000 | [diff] [blame] | 3486 | SmallVector<int64_t, 2> Worklist; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3487 | Worklist.push_back(LU.MinOffset); |
| 3488 | if (LU.MaxOffset != LU.MinOffset) |
| 3489 | Worklist.push_back(LU.MaxOffset); |
| 3490 | |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3491 | for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) |
| 3492 | GenerateConstantOffsetsImpl(LU, LUIdx, Base, Worklist, i); |
| 3493 | if (Base.Scale == 1) |
| 3494 | GenerateConstantOffsetsImpl(LU, LUIdx, Base, Worklist, /* Idx */ -1, |
| 3495 | /* IsScaledReg */ true); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3498 | /// For ICmpZero, check to see if we can scale up the comparison. For example, x |
| 3499 | /// == y -> x*c == y*c. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3500 | void LSRInstance::GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx, |
| 3501 | Formula Base) { |
| 3502 | if (LU.Kind != LSRUse::ICmpZero) return; |
| 3503 | |
| 3504 | // Determine the integer type for the base formula. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3505 | Type *IntTy = Base.getType(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3506 | if (!IntTy) return; |
| 3507 | if (SE.getTypeSizeInBits(IntTy) > 64) return; |
| 3508 | |
| 3509 | // Don't do this if there is more than one offset. |
| 3510 | if (LU.MinOffset != LU.MaxOffset) return; |
| 3511 | |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 3512 | assert(!Base.BaseGV && "ICmpZero use is not legal!"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3513 | |
| 3514 | // Check each interesting stride. |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3515 | for (int64_t Factor : Factors) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3516 | // Check that the multiplication doesn't overflow. |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 3517 | if (Base.BaseOffset == INT64_MIN && Factor == -1) |
Dan Gohman | 5f10d6c | 2010-02-17 00:41:53 +0000 | [diff] [blame] | 3518 | continue; |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 3519 | int64_t NewBaseOffset = (uint64_t)Base.BaseOffset * Factor; |
| 3520 | if (NewBaseOffset / Factor != Base.BaseOffset) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3521 | continue; |
Andrew Trick | 429e9ed | 2014-02-26 16:31:56 +0000 | [diff] [blame] | 3522 | // If the offset will be truncated at this use, check that it is in bounds. |
| 3523 | if (!IntTy->isPointerTy() && |
| 3524 | !ConstantInt::isValueValidForType(IntTy, NewBaseOffset)) |
| 3525 | continue; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3526 | |
| 3527 | // Check that multiplying with the use offset doesn't overflow. |
| 3528 | int64_t Offset = LU.MinOffset; |
Dan Gohman | 5f10d6c | 2010-02-17 00:41:53 +0000 | [diff] [blame] | 3529 | if (Offset == INT64_MIN && Factor == -1) |
| 3530 | continue; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3531 | Offset = (uint64_t)Offset * Factor; |
Dan Gohman | 13ac3b2 | 2010-02-17 00:42:19 +0000 | [diff] [blame] | 3532 | if (Offset / Factor != LU.MinOffset) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3533 | continue; |
Andrew Trick | 429e9ed | 2014-02-26 16:31:56 +0000 | [diff] [blame] | 3534 | // If the offset will be truncated at this use, check that it is in bounds. |
| 3535 | if (!IntTy->isPointerTy() && |
| 3536 | !ConstantInt::isValueValidForType(IntTy, Offset)) |
| 3537 | continue; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3538 | |
Dan Gohman | 963b1c1 | 2010-06-24 16:57:52 +0000 | [diff] [blame] | 3539 | Formula F = Base; |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 3540 | F.BaseOffset = NewBaseOffset; |
Dan Gohman | 963b1c1 | 2010-06-24 16:57:52 +0000 | [diff] [blame] | 3541 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3542 | // Check that this scale is legal. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 3543 | if (!isLegalUse(TTI, Offset, Offset, LU.Kind, LU.AccessTy, F)) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3544 | continue; |
| 3545 | |
| 3546 | // Compensate for the use having MinOffset built into it. |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 3547 | F.BaseOffset = (uint64_t)F.BaseOffset + Offset - LU.MinOffset; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3548 | |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 3549 | const SCEV *FactorS = SE.getConstant(IntTy, Factor); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3550 | |
| 3551 | // Check that multiplying with each base register doesn't overflow. |
| 3552 | for (size_t i = 0, e = F.BaseRegs.size(); i != e; ++i) { |
| 3553 | F.BaseRegs[i] = SE.getMulExpr(F.BaseRegs[i], FactorS); |
Dan Gohman | 4eebb94 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 3554 | if (getExactSDiv(F.BaseRegs[i], FactorS, SE) != Base.BaseRegs[i]) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3555 | goto next; |
| 3556 | } |
| 3557 | |
| 3558 | // Check that multiplying with the scaled register doesn't overflow. |
| 3559 | if (F.ScaledReg) { |
| 3560 | F.ScaledReg = SE.getMulExpr(F.ScaledReg, FactorS); |
Dan Gohman | 4eebb94 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 3561 | if (getExactSDiv(F.ScaledReg, FactorS, SE) != Base.ScaledReg) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3562 | continue; |
| 3563 | } |
| 3564 | |
Dan Gohman | 6136e94 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 3565 | // Check that multiplying with the unfolded offset doesn't overflow. |
| 3566 | if (F.UnfoldedOffset != 0) { |
Dan Gohman | 6c4a319 | 2011-05-23 21:07:39 +0000 | [diff] [blame] | 3567 | if (F.UnfoldedOffset == INT64_MIN && Factor == -1) |
| 3568 | continue; |
Dan Gohman | 6136e94 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 3569 | F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset * Factor; |
| 3570 | if (F.UnfoldedOffset / Factor != Base.UnfoldedOffset) |
| 3571 | continue; |
Andrew Trick | 429e9ed | 2014-02-26 16:31:56 +0000 | [diff] [blame] | 3572 | // If the offset will be truncated, check that it is in bounds. |
| 3573 | if (!IntTy->isPointerTy() && |
| 3574 | !ConstantInt::isValueValidForType(IntTy, F.UnfoldedOffset)) |
| 3575 | continue; |
Dan Gohman | 6136e94 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3578 | // If we make it here and it's legal, add it. |
| 3579 | (void)InsertFormula(LU, LUIdx, F); |
| 3580 | next:; |
| 3581 | } |
| 3582 | } |
| 3583 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3584 | /// Generate stride factor reuse formulae by making use of scaled-offset address |
| 3585 | /// modes, for example. |
Dan Gohman | ab5fb7f | 2010-05-20 19:44:23 +0000 | [diff] [blame] | 3586 | void LSRInstance::GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3587 | // Determine the integer type for the base formula. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3588 | Type *IntTy = Base.getType(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3589 | if (!IntTy) return; |
| 3590 | |
| 3591 | // If this Formula already has a scaled register, we can't add another one. |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3592 | // Try to unscale the formula to generate a better scale. |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 3593 | if (Base.Scale != 0 && !Base.unscale()) |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3594 | return; |
| 3595 | |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 3596 | assert(Base.Scale == 0 && "unscale did not did its job!"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3597 | |
| 3598 | // Check each interesting stride. |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3599 | for (int64_t Factor : Factors) { |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 3600 | Base.Scale = Factor; |
| 3601 | Base.HasBaseReg = Base.BaseRegs.size() > 1; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3602 | // Check whether this scale is going to be legal. |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 3603 | if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, |
| 3604 | Base)) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3605 | // As a special-case, handle special out-of-loop Basic users specially. |
| 3606 | // TODO: Reconsider this special case. |
| 3607 | if (LU.Kind == LSRUse::Basic && |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 3608 | isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LSRUse::Special, |
| 3609 | LU.AccessTy, Base) && |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3610 | LU.AllFixupsOutsideLoop) |
| 3611 | LU.Kind = LSRUse::Special; |
| 3612 | else |
| 3613 | continue; |
| 3614 | } |
| 3615 | // For an ICmpZero, negating a solitary base register won't lead to |
| 3616 | // new solutions. |
| 3617 | if (LU.Kind == LSRUse::ICmpZero && |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 3618 | !Base.HasBaseReg && Base.BaseOffset == 0 && !Base.BaseGV) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3619 | continue; |
| 3620 | // For each addrec base reg, apply the scale, if possible. |
| 3621 | for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) |
| 3622 | if (const SCEVAddRecExpr *AR = |
| 3623 | dyn_cast<SCEVAddRecExpr>(Base.BaseRegs[i])) { |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 3624 | const SCEV *FactorS = SE.getConstant(IntTy, Factor); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3625 | if (FactorS->isZero()) |
| 3626 | continue; |
| 3627 | // Divide out the factor, ignoring high bits, since we'll be |
| 3628 | // scaling the value back up in the end. |
Dan Gohman | 4eebb94 | 2010-02-19 19:35:48 +0000 | [diff] [blame] | 3629 | if (const SCEV *Quotient = getExactSDiv(AR, FactorS, SE, true)) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3630 | // TODO: This could be optimized to avoid all the copying. |
| 3631 | Formula F = Base; |
| 3632 | F.ScaledReg = Quotient; |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 3633 | F.deleteBaseReg(F.BaseRegs[i]); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3634 | // The canonical representation of 1*reg is reg, which is already in |
| 3635 | // Base. In that case, do not try to insert the formula, it will be |
| 3636 | // rejected anyway. |
| 3637 | if (F.Scale == 1 && F.BaseRegs.empty()) |
| 3638 | continue; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3639 | (void)InsertFormula(LU, LUIdx, F); |
| 3640 | } |
| 3641 | } |
| 3642 | } |
| 3643 | } |
| 3644 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3645 | /// Generate reuse formulae from different IV types. |
Dan Gohman | ab5fb7f | 2010-05-20 19:44:23 +0000 | [diff] [blame] | 3646 | void LSRInstance::GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3647 | // Don't bother truncating symbolic values. |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 3648 | if (Base.BaseGV) return; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3649 | |
| 3650 | // Determine the integer type for the base formula. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3651 | Type *DstTy = Base.getType(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3652 | if (!DstTy) return; |
| 3653 | DstTy = SE.getEffectiveSCEVType(DstTy); |
| 3654 | |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3655 | for (Type *SrcTy : Types) { |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 3656 | if (SrcTy != DstTy && TTI.isTruncateFree(SrcTy, DstTy)) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3657 | Formula F = Base; |
| 3658 | |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3659 | if (F.ScaledReg) F.ScaledReg = SE.getAnyExtendExpr(F.ScaledReg, SrcTy); |
| 3660 | for (const SCEV *&BaseReg : F.BaseRegs) |
| 3661 | BaseReg = SE.getAnyExtendExpr(BaseReg, SrcTy); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3662 | |
| 3663 | // TODO: This assumes we've done basic processing on all uses and |
| 3664 | // have an idea what the register usage is. |
| 3665 | if (!F.hasRegsUsedByUsesOtherThan(LUIdx, RegUses)) |
| 3666 | continue; |
| 3667 | |
| 3668 | (void)InsertFormula(LU, LUIdx, F); |
| 3669 | } |
| 3670 | } |
| 3671 | } |
| 3672 | |
| 3673 | namespace { |
| 3674 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3675 | /// Helper class for GenerateCrossUseConstantOffsets. It's used to defer |
| 3676 | /// modifications so that the search phase doesn't have to worry about the data |
| 3677 | /// structures moving underneath it. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3678 | struct WorkItem { |
| 3679 | size_t LUIdx; |
| 3680 | int64_t Imm; |
| 3681 | const SCEV *OrigReg; |
| 3682 | |
| 3683 | WorkItem(size_t LI, int64_t I, const SCEV *R) |
| 3684 | : LUIdx(LI), Imm(I), OrigReg(R) {} |
| 3685 | |
| 3686 | void print(raw_ostream &OS) const; |
| 3687 | void dump() const; |
| 3688 | }; |
| 3689 | |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 3690 | } // end anonymous namespace |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3691 | |
| 3692 | void WorkItem::print(raw_ostream &OS) const { |
| 3693 | OS << "in formulae referencing " << *OrigReg << " in use " << LUIdx |
| 3694 | << " , add offset " << Imm; |
| 3695 | } |
| 3696 | |
Davide Italiano | 945d05f | 2015-11-23 02:47:30 +0000 | [diff] [blame] | 3697 | LLVM_DUMP_METHOD |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3698 | void WorkItem::dump() const { |
| 3699 | print(errs()); errs() << '\n'; |
| 3700 | } |
| 3701 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3702 | /// Look for registers which are a constant distance apart and try to form reuse |
| 3703 | /// opportunities between them. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3704 | void LSRInstance::GenerateCrossUseConstantOffsets() { |
| 3705 | // Group the registers by their value without any added constant offset. |
| 3706 | typedef std::map<int64_t, const SCEV *> ImmMapTy; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3707 | DenseMap<const SCEV *, ImmMapTy> Map; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3708 | DenseMap<const SCEV *, SmallBitVector> UsedByIndicesMap; |
| 3709 | SmallVector<const SCEV *, 8> Sequence; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3710 | for (const SCEV *Use : RegUses) { |
| 3711 | const SCEV *Reg = Use; // Make a copy for ExtractImmediate to modify. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3712 | int64_t Imm = ExtractImmediate(Reg, SE); |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3713 | auto Pair = Map.insert(std::make_pair(Reg, ImmMapTy())); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3714 | if (Pair.second) |
| 3715 | Sequence.push_back(Reg); |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3716 | Pair.first->second.insert(std::make_pair(Imm, Use)); |
| 3717 | UsedByIndicesMap[Reg] |= RegUses.getUsedByIndices(Use); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
| 3720 | // Now examine each set of registers with the same base value. Build up |
| 3721 | // a list of work to do and do the work in a separate step so that we're |
| 3722 | // not adding formulae and register counts while we're searching. |
Dan Gohman | 110ed64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 3723 | SmallVector<WorkItem, 32> WorkItems; |
| 3724 | SmallSet<std::pair<size_t, int64_t>, 32> UniqueItems; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3725 | for (const SCEV *Reg : Sequence) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3726 | const ImmMapTy &Imms = Map.find(Reg)->second; |
| 3727 | |
Dan Gohman | 363f847 | 2010-02-12 19:20:37 +0000 | [diff] [blame] | 3728 | // It's not worthwhile looking for reuse if there's only one offset. |
| 3729 | if (Imms.size() == 1) |
| 3730 | continue; |
| 3731 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3732 | DEBUG(dbgs() << "Generating cross-use offsets for " << *Reg << ':'; |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3733 | for (const auto &Entry : Imms) |
| 3734 | dbgs() << ' ' << Entry.first; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3735 | dbgs() << '\n'); |
| 3736 | |
| 3737 | // Examine each offset. |
| 3738 | for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end(); |
| 3739 | J != JE; ++J) { |
| 3740 | const SCEV *OrigReg = J->second; |
| 3741 | |
| 3742 | int64_t JImm = J->first; |
| 3743 | const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(OrigReg); |
| 3744 | |
| 3745 | if (!isa<SCEVConstant>(OrigReg) && |
| 3746 | UsedByIndicesMap[Reg].count() == 1) { |
| 3747 | DEBUG(dbgs() << "Skipping cross-use reuse for " << *OrigReg << '\n'); |
| 3748 | continue; |
| 3749 | } |
| 3750 | |
| 3751 | // Conservatively examine offsets between this orig reg a few selected |
| 3752 | // other orig regs. |
| 3753 | ImmMapTy::const_iterator OtherImms[] = { |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 3754 | Imms.begin(), std::prev(Imms.end()), |
| 3755 | Imms.lower_bound((Imms.begin()->first + std::prev(Imms.end())->first) / |
| 3756 | 2) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3757 | }; |
| 3758 | for (size_t i = 0, e = array_lengthof(OtherImms); i != e; ++i) { |
| 3759 | ImmMapTy::const_iterator M = OtherImms[i]; |
Dan Gohman | 363f847 | 2010-02-12 19:20:37 +0000 | [diff] [blame] | 3760 | if (M == J || M == JE) continue; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3761 | |
| 3762 | // Compute the difference between the two. |
| 3763 | int64_t Imm = (uint64_t)JImm - M->first; |
| 3764 | for (int LUIdx = UsedByIndices.find_first(); LUIdx != -1; |
Dan Gohman | 110ed64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 3765 | LUIdx = UsedByIndices.find_next(LUIdx)) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3766 | // Make a memo of this use, offset, and register tuple. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 3767 | if (UniqueItems.insert(std::make_pair(LUIdx, Imm)).second) |
Dan Gohman | 110ed64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 3768 | WorkItems.push_back(WorkItem(LUIdx, Imm, OrigReg)); |
Evan Cheng | 85a9f43 | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 3769 | } |
| 3770 | } |
| 3771 | } |
| 3772 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3773 | Map.clear(); |
| 3774 | Sequence.clear(); |
| 3775 | UsedByIndicesMap.clear(); |
Dan Gohman | 110ed64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 3776 | UniqueItems.clear(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3777 | |
| 3778 | // Now iterate through the worklist and add new formulae. |
Craig Topper | 042a392 | 2015-05-25 20:01:18 +0000 | [diff] [blame] | 3779 | for (const WorkItem &WI : WorkItems) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3780 | size_t LUIdx = WI.LUIdx; |
| 3781 | LSRUse &LU = Uses[LUIdx]; |
| 3782 | int64_t Imm = WI.Imm; |
| 3783 | const SCEV *OrigReg = WI.OrigReg; |
| 3784 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3785 | Type *IntTy = SE.getEffectiveSCEVType(OrigReg->getType()); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3786 | const SCEV *NegImmS = SE.getSCEV(ConstantInt::get(IntTy, -(uint64_t)Imm)); |
| 3787 | unsigned BitWidth = SE.getTypeSizeInBits(IntTy); |
| 3788 | |
Dan Gohman | 8b0a419 | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3789 | // TODO: Use a more targeted data structure. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3790 | for (size_t L = 0, LE = LU.Formulae.size(); L != LE; ++L) { |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 3791 | Formula F = LU.Formulae[L]; |
| 3792 | // FIXME: The code for the scaled and unscaled registers looks |
| 3793 | // very similar but slightly different. Investigate if they |
| 3794 | // could be merged. That way, we would not have to unscale the |
| 3795 | // Formula. |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 3796 | F.unscale(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3797 | // Use the immediate in the scaled register. |
| 3798 | if (F.ScaledReg == OrigReg) { |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 3799 | int64_t Offset = (uint64_t)F.BaseOffset + Imm * (uint64_t)F.Scale; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3800 | // Don't create 50 + reg(-50). |
| 3801 | if (F.referencesReg(SE.getSCEV( |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 3802 | ConstantInt::get(IntTy, -(uint64_t)Offset)))) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3803 | continue; |
| 3804 | Formula NewF = F; |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 3805 | NewF.BaseOffset = Offset; |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 3806 | if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, |
| 3807 | NewF)) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3808 | continue; |
| 3809 | NewF.ScaledReg = SE.getAddExpr(NegImmS, NewF.ScaledReg); |
| 3810 | |
| 3811 | // If the new scale is a constant in a register, and adding the constant |
| 3812 | // value to the immediate would produce a value closer to zero than the |
| 3813 | // immediate itself, then the formula isn't worthwhile. |
| 3814 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(NewF.ScaledReg)) |
Sanjoy Das | 0de2fec | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 3815 | if (C->getValue()->isNegative() != (NewF.BaseOffset < 0) && |
| 3816 | (C->getAPInt().abs() * APInt(BitWidth, F.Scale)) |
| 3817 | .ule(std::abs(NewF.BaseOffset))) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3818 | continue; |
| 3819 | |
| 3820 | // OK, looks good. |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 3821 | NewF.canonicalize(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3822 | (void)InsertFormula(LU, LUIdx, NewF); |
| 3823 | } else { |
| 3824 | // Use the immediate in a base register. |
| 3825 | for (size_t N = 0, NE = F.BaseRegs.size(); N != NE; ++N) { |
| 3826 | const SCEV *BaseReg = F.BaseRegs[N]; |
| 3827 | if (BaseReg != OrigReg) |
| 3828 | continue; |
| 3829 | Formula NewF = F; |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 3830 | NewF.BaseOffset = (uint64_t)NewF.BaseOffset + Imm; |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 3831 | if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, |
| 3832 | LU.Kind, LU.AccessTy, NewF)) { |
| 3833 | if (!TTI.isLegalAddImmediate((uint64_t)NewF.UnfoldedOffset + Imm)) |
Dan Gohman | 6136e94 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 3834 | continue; |
| 3835 | NewF = F; |
| 3836 | NewF.UnfoldedOffset = (uint64_t)NewF.UnfoldedOffset + Imm; |
| 3837 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3838 | NewF.BaseRegs[N] = SE.getAddExpr(NegImmS, BaseReg); |
| 3839 | |
| 3840 | // If the new formula has a constant in a register, and adding the |
| 3841 | // constant value to the immediate would produce a value closer to |
| 3842 | // zero than the immediate itself, then the formula isn't worthwhile. |
Craig Topper | 10949ae | 2015-05-23 08:45:10 +0000 | [diff] [blame] | 3843 | for (const SCEV *NewReg : NewF.BaseRegs) |
| 3844 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(NewReg)) |
Sanjoy Das | 0de2fec | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 3845 | if ((C->getAPInt() + NewF.BaseOffset) |
| 3846 | .abs() |
| 3847 | .slt(std::abs(NewF.BaseOffset)) && |
| 3848 | (C->getAPInt() + NewF.BaseOffset).countTrailingZeros() >= |
| 3849 | countTrailingZeros<uint64_t>(NewF.BaseOffset)) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3850 | goto skip_formula; |
| 3851 | |
| 3852 | // Ok, looks good. |
Sanjoy Das | 302bfd0 | 2015-08-16 18:22:43 +0000 | [diff] [blame] | 3853 | NewF.canonicalize(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3854 | (void)InsertFormula(LU, LUIdx, NewF); |
| 3855 | break; |
| 3856 | skip_formula:; |
| 3857 | } |
| 3858 | } |
| 3859 | } |
| 3860 | } |
Dale Johannesen | 02cb2bf | 2009-05-11 17:15:42 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 3863 | /// Generate formulae for each use. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3864 | void |
| 3865 | LSRInstance::GenerateAllReuseFormulae() { |
Dan Gohman | 521efe6 | 2010-02-16 01:42:53 +0000 | [diff] [blame] | 3866 | // This is split into multiple loops so that hasRegsUsedByUsesOtherThan |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3867 | // queries are more precise. |
| 3868 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 3869 | LSRUse &LU = Uses[LUIdx]; |
| 3870 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 3871 | GenerateReassociations(LU, LUIdx, LU.Formulae[i]); |
| 3872 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 3873 | GenerateCombinations(LU, LUIdx, LU.Formulae[i]); |
| 3874 | } |
| 3875 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 3876 | LSRUse &LU = Uses[LUIdx]; |
| 3877 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 3878 | GenerateSymbolicOffsets(LU, LUIdx, LU.Formulae[i]); |
| 3879 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 3880 | GenerateConstantOffsets(LU, LUIdx, LU.Formulae[i]); |
| 3881 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 3882 | GenerateICmpZeroScales(LU, LUIdx, LU.Formulae[i]); |
| 3883 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 3884 | GenerateScales(LU, LUIdx, LU.Formulae[i]); |
Dan Gohman | 521efe6 | 2010-02-16 01:42:53 +0000 | [diff] [blame] | 3885 | } |
| 3886 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 3887 | LSRUse &LU = Uses[LUIdx]; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3888 | for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i) |
| 3889 | GenerateTruncates(LU, LUIdx, LU.Formulae[i]); |
| 3890 | } |
| 3891 | |
| 3892 | GenerateCrossUseConstantOffsets(); |
Dan Gohman | bf673e0 | 2010-08-29 15:21:38 +0000 | [diff] [blame] | 3893 | |
| 3894 | DEBUG(dbgs() << "\n" |
| 3895 | "After generating reuse formulae:\n"; |
| 3896 | print_uses(dbgs())); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3897 | } |
| 3898 | |
Dan Gohman | 1b61fd9 | 2010-10-07 23:43:09 +0000 | [diff] [blame] | 3899 | /// If there are multiple formulae with the same set of registers used |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3900 | /// by other uses, pick the best one and delete the others. |
| 3901 | void LSRInstance::FilterOutUndesirableDedicatedRegisters() { |
Dan Gohman | 5947e16 | 2010-10-07 23:52:18 +0000 | [diff] [blame] | 3902 | DenseSet<const SCEV *> VisitedRegs; |
| 3903 | SmallPtrSet<const SCEV *, 16> Regs; |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 3904 | SmallPtrSet<const SCEV *, 16> LoserRegs; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3905 | #ifndef NDEBUG |
Dan Gohman | 4c4043c | 2010-05-20 20:05:31 +0000 | [diff] [blame] | 3906 | bool ChangedFormulae = false; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3907 | #endif |
| 3908 | |
| 3909 | // Collect the best formula for each unique set of shared registers. This |
| 3910 | // is reset for each use. |
Preston Gurd | 25c3b6a | 2013-02-01 20:41:27 +0000 | [diff] [blame] | 3911 | typedef DenseMap<SmallVector<const SCEV *, 4>, size_t, UniquifierDenseMapInfo> |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3912 | BestFormulaeTy; |
| 3913 | BestFormulaeTy BestFormulae; |
| 3914 | |
| 3915 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 3916 | LSRUse &LU = Uses[LUIdx]; |
Dan Gohman | ab5fb7f | 2010-05-20 19:44:23 +0000 | [diff] [blame] | 3917 | DEBUG(dbgs() << "Filtering for use "; LU.print(dbgs()); dbgs() << '\n'); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3918 | |
Dan Gohman | 4cf99b5 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 3919 | bool Any = false; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3920 | for (size_t FIdx = 0, NumForms = LU.Formulae.size(); |
| 3921 | FIdx != NumForms; ++FIdx) { |
| 3922 | Formula &F = LU.Formulae[FIdx]; |
| 3923 | |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 3924 | // Some formulas are instant losers. For example, they may depend on |
| 3925 | // nonexistent AddRecs from other loops. These need to be filtered |
| 3926 | // immediately, otherwise heuristics could choose them over others leading |
| 3927 | // to an unsatisfactory solution. Passing LoserRegs into RateFormula here |
| 3928 | // avoids the need to recompute this information across formulae using the |
| 3929 | // same bad AddRec. Passing LoserRegs is also essential unless we remove |
| 3930 | // the corresponding bad register from the Regs set. |
| 3931 | Cost CostF; |
| 3932 | Regs.clear(); |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 3933 | CostF.RateFormula(TTI, F, Regs, VisitedRegs, L, SE, DT, LU, &LoserRegs); |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 3934 | if (CostF.isLoser()) { |
| 3935 | // During initial formula generation, undesirable formulae are generated |
| 3936 | // by uses within other loops that have some non-trivial address mode or |
| 3937 | // use the postinc form of the IV. LSR needs to provide these formulae |
| 3938 | // as the basis of rediscovering the desired formula that uses an AddRec |
| 3939 | // corresponding to the existing phi. Once all formulae have been |
| 3940 | // generated, these initial losers may be pruned. |
| 3941 | DEBUG(dbgs() << " Filtering loser "; F.print(dbgs()); |
| 3942 | dbgs() << "\n"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3943 | } |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 3944 | else { |
Preston Gurd | 25c3b6a | 2013-02-01 20:41:27 +0000 | [diff] [blame] | 3945 | SmallVector<const SCEV *, 4> Key; |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 3946 | for (const SCEV *Reg : F.BaseRegs) { |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 3947 | if (RegUses.isRegUsedByUsesOtherThan(Reg, LUIdx)) |
| 3948 | Key.push_back(Reg); |
| 3949 | } |
| 3950 | if (F.ScaledReg && |
| 3951 | RegUses.isRegUsedByUsesOtherThan(F.ScaledReg, LUIdx)) |
| 3952 | Key.push_back(F.ScaledReg); |
| 3953 | // Unstable sort by host order ok, because this is only used for |
| 3954 | // uniquifying. |
| 3955 | std::sort(Key.begin(), Key.end()); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3956 | |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 3957 | std::pair<BestFormulaeTy::const_iterator, bool> P = |
| 3958 | BestFormulae.insert(std::make_pair(Key, FIdx)); |
| 3959 | if (P.second) |
| 3960 | continue; |
| 3961 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3962 | Formula &Best = LU.Formulae[P.first->second]; |
Dan Gohman | 5947e16 | 2010-10-07 23:52:18 +0000 | [diff] [blame] | 3963 | |
Dan Gohman | 5947e16 | 2010-10-07 23:52:18 +0000 | [diff] [blame] | 3964 | Cost CostBest; |
Dan Gohman | 5947e16 | 2010-10-07 23:52:18 +0000 | [diff] [blame] | 3965 | Regs.clear(); |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 3966 | CostBest.RateFormula(TTI, Best, Regs, VisitedRegs, L, SE, DT, LU); |
Dan Gohman | 5947e16 | 2010-10-07 23:52:18 +0000 | [diff] [blame] | 3967 | if (CostF < CostBest) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3968 | std::swap(F, Best); |
Dan Gohman | 8aca7ef | 2010-05-18 22:37:37 +0000 | [diff] [blame] | 3969 | DEBUG(dbgs() << " Filtering out formula "; F.print(dbgs()); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3970 | dbgs() << "\n" |
Dan Gohman | 8aca7ef | 2010-05-18 22:37:37 +0000 | [diff] [blame] | 3971 | " in favor of formula "; Best.print(dbgs()); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3972 | dbgs() << '\n'); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3973 | } |
Andrew Trick | 5df9096 | 2011-12-06 03:13:31 +0000 | [diff] [blame] | 3974 | #ifndef NDEBUG |
| 3975 | ChangedFormulae = true; |
| 3976 | #endif |
| 3977 | LU.DeleteFormula(F); |
| 3978 | --FIdx; |
| 3979 | --NumForms; |
| 3980 | Any = true; |
Dan Gohman | d080024 | 2010-05-07 23:36:59 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
Dan Gohman | beebef4 | 2010-05-18 23:55:57 +0000 | [diff] [blame] | 3983 | // Now that we've filtered out some formulae, recompute the Regs set. |
Dan Gohman | 4cf99b5 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 3984 | if (Any) |
| 3985 | LU.RecomputeRegs(LUIdx, RegUses); |
Dan Gohman | d080024 | 2010-05-07 23:36:59 +0000 | [diff] [blame] | 3986 | |
| 3987 | // Reset this to prepare for the next use. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3988 | BestFormulae.clear(); |
| 3989 | } |
| 3990 | |
Dan Gohman | 4c4043c | 2010-05-20 20:05:31 +0000 | [diff] [blame] | 3991 | DEBUG(if (ChangedFormulae) { |
Dan Gohman | 5b18f03 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 3992 | dbgs() << "\n" |
| 3993 | "After filtering out undesirable candidates:\n"; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 3994 | print_uses(dbgs()); |
| 3995 | }); |
| 3996 | } |
| 3997 | |
Dan Gohman | a4eca05 | 2010-05-18 22:51:59 +0000 | [diff] [blame] | 3998 | // This is a rough guess that seems to work fairly well. |
| 3999 | static const size_t ComplexityLimit = UINT16_MAX; |
| 4000 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4001 | /// Estimate the worst-case number of solutions the solver might have to |
| 4002 | /// consider. It almost never considers this many solutions because it prune the |
| 4003 | /// search space, but the pruning isn't always sufficient. |
Dan Gohman | a4eca05 | 2010-05-18 22:51:59 +0000 | [diff] [blame] | 4004 | size_t LSRInstance::EstimateSearchSpaceComplexity() const { |
Dan Gohman | 49d638b | 2010-10-07 23:37:58 +0000 | [diff] [blame] | 4005 | size_t Power = 1; |
Craig Topper | 10949ae | 2015-05-23 08:45:10 +0000 | [diff] [blame] | 4006 | for (const LSRUse &LU : Uses) { |
| 4007 | size_t FSize = LU.Formulae.size(); |
Dan Gohman | a4eca05 | 2010-05-18 22:51:59 +0000 | [diff] [blame] | 4008 | if (FSize >= ComplexityLimit) { |
| 4009 | Power = ComplexityLimit; |
| 4010 | break; |
| 4011 | } |
| 4012 | Power *= FSize; |
| 4013 | if (Power >= ComplexityLimit) |
| 4014 | break; |
| 4015 | } |
| 4016 | return Power; |
| 4017 | } |
| 4018 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4019 | /// When one formula uses a superset of the registers of another formula, it |
| 4020 | /// won't help reduce register pressure (though it may not necessarily hurt |
| 4021 | /// register pressure); remove it to simplify the system. |
Dan Gohman | e9e0873 | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 4022 | void LSRInstance::NarrowSearchSpaceByDetectingSupersets() { |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4023 | if (EstimateSearchSpaceComplexity() >= ComplexityLimit) { |
| 4024 | DEBUG(dbgs() << "The search space is too complex.\n"); |
| 4025 | |
| 4026 | DEBUG(dbgs() << "Narrowing the search space by eliminating formulae " |
| 4027 | "which use a superset of registers used by other " |
| 4028 | "formulae.\n"); |
| 4029 | |
| 4030 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 4031 | LSRUse &LU = Uses[LUIdx]; |
| 4032 | bool Any = false; |
| 4033 | for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) { |
| 4034 | Formula &F = LU.Formulae[i]; |
Dan Gohman | 8ec018c | 2010-05-20 20:00:41 +0000 | [diff] [blame] | 4035 | // Look for a formula with a constant or GV in a register. If the use |
| 4036 | // also has a formula with that same value in an immediate field, |
| 4037 | // delete the one that uses a register. |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4038 | for (SmallVectorImpl<const SCEV *>::const_iterator |
| 4039 | I = F.BaseRegs.begin(), E = F.BaseRegs.end(); I != E; ++I) { |
| 4040 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(*I)) { |
| 4041 | Formula NewF = F; |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 4042 | NewF.BaseOffset += C->getValue()->getSExtValue(); |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4043 | NewF.BaseRegs.erase(NewF.BaseRegs.begin() + |
| 4044 | (I - F.BaseRegs.begin())); |
| 4045 | if (LU.HasFormulaWithSameRegs(NewF)) { |
| 4046 | DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n'); |
| 4047 | LU.DeleteFormula(F); |
| 4048 | --i; |
| 4049 | --e; |
| 4050 | Any = true; |
| 4051 | break; |
| 4052 | } |
| 4053 | } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(*I)) { |
| 4054 | if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue())) |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 4055 | if (!F.BaseGV) { |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4056 | Formula NewF = F; |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 4057 | NewF.BaseGV = GV; |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4058 | NewF.BaseRegs.erase(NewF.BaseRegs.begin() + |
| 4059 | (I - F.BaseRegs.begin())); |
| 4060 | if (LU.HasFormulaWithSameRegs(NewF)) { |
| 4061 | DEBUG(dbgs() << " Deleting "; F.print(dbgs()); |
| 4062 | dbgs() << '\n'); |
| 4063 | LU.DeleteFormula(F); |
| 4064 | --i; |
| 4065 | --e; |
| 4066 | Any = true; |
| 4067 | break; |
| 4068 | } |
| 4069 | } |
| 4070 | } |
| 4071 | } |
| 4072 | } |
| 4073 | if (Any) |
| 4074 | LU.RecomputeRegs(LUIdx, RegUses); |
| 4075 | } |
| 4076 | |
| 4077 | DEBUG(dbgs() << "After pre-selection:\n"; |
| 4078 | print_uses(dbgs())); |
| 4079 | } |
Dan Gohman | e9e0873 | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 4080 | } |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4081 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4082 | /// When there are many registers for expressions like A, A+1, A+2, etc., |
| 4083 | /// allocate a single register for them. |
Dan Gohman | e9e0873 | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 4084 | void LSRInstance::NarrowSearchSpaceByCollapsingUnrolledCode() { |
Jakub Staszak | 11bd835 | 2013-02-16 16:08:15 +0000 | [diff] [blame] | 4085 | if (EstimateSearchSpaceComplexity() < ComplexityLimit) |
| 4086 | return; |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4087 | |
Jakub Staszak | 11bd835 | 2013-02-16 16:08:15 +0000 | [diff] [blame] | 4088 | DEBUG(dbgs() << "The search space is too complex.\n" |
| 4089 | "Narrowing the search space by assuming that uses separated " |
| 4090 | "by a constant offset will use the same registers.\n"); |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4091 | |
Jakub Staszak | 11bd835 | 2013-02-16 16:08:15 +0000 | [diff] [blame] | 4092 | // This is especially useful for unrolled loops. |
Dan Gohman | 8ec018c | 2010-05-20 20:00:41 +0000 | [diff] [blame] | 4093 | |
Jakub Staszak | 11bd835 | 2013-02-16 16:08:15 +0000 | [diff] [blame] | 4094 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 4095 | LSRUse &LU = Uses[LUIdx]; |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4096 | for (const Formula &F : LU.Formulae) { |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 4097 | if (F.BaseOffset == 0 || (F.Scale != 0 && F.Scale != 1)) |
Jakub Staszak | 11bd835 | 2013-02-16 16:08:15 +0000 | [diff] [blame] | 4098 | continue; |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4099 | |
Jakub Staszak | 11bd835 | 2013-02-16 16:08:15 +0000 | [diff] [blame] | 4100 | LSRUse *LUThatHas = FindUseWithSimilarFormula(F, LU); |
| 4101 | if (!LUThatHas) |
| 4102 | continue; |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4103 | |
Jakub Staszak | 11bd835 | 2013-02-16 16:08:15 +0000 | [diff] [blame] | 4104 | if (!reconcileNewOffset(*LUThatHas, F.BaseOffset, /*HasBaseReg=*/ false, |
| 4105 | LU.Kind, LU.AccessTy)) |
| 4106 | continue; |
Dan Gohman | 110ed64 | 2010-09-01 01:45:53 +0000 | [diff] [blame] | 4107 | |
Jakub Staszak | 11bd835 | 2013-02-16 16:08:15 +0000 | [diff] [blame] | 4108 | DEBUG(dbgs() << " Deleting use "; LU.print(dbgs()); dbgs() << '\n'); |
Dan Gohman | 2fd85d7 | 2010-10-08 19:33:26 +0000 | [diff] [blame] | 4109 | |
Jakub Staszak | 11bd835 | 2013-02-16 16:08:15 +0000 | [diff] [blame] | 4110 | LUThatHas->AllFixupsOutsideLoop &= LU.AllFixupsOutsideLoop; |
| 4111 | |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 4112 | // Transfer the fixups of LU to LUThatHas. |
| 4113 | for (LSRFixup &Fixup : LU.Fixups) { |
| 4114 | Fixup.Offset += F.BaseOffset; |
| 4115 | LUThatHas->pushFixup(Fixup); |
| 4116 | DEBUG(dbgs() << "New fixup has offset " << Fixup.Offset << '\n'); |
Jakub Staszak | 11bd835 | 2013-02-16 16:08:15 +0000 | [diff] [blame] | 4117 | } |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 4118 | |
Jakub Staszak | 11bd835 | 2013-02-16 16:08:15 +0000 | [diff] [blame] | 4119 | // Delete formulae from the new use which are no longer legal. |
| 4120 | bool Any = false; |
| 4121 | for (size_t i = 0, e = LUThatHas->Formulae.size(); i != e; ++i) { |
| 4122 | Formula &F = LUThatHas->Formulae[i]; |
| 4123 | if (!isLegalUse(TTI, LUThatHas->MinOffset, LUThatHas->MaxOffset, |
| 4124 | LUThatHas->Kind, LUThatHas->AccessTy, F)) { |
| 4125 | DEBUG(dbgs() << " Deleting "; F.print(dbgs()); |
| 4126 | dbgs() << '\n'); |
| 4127 | LUThatHas->DeleteFormula(F); |
| 4128 | --i; |
| 4129 | --e; |
| 4130 | Any = true; |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4131 | } |
| 4132 | } |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4133 | |
Jakub Staszak | 11bd835 | 2013-02-16 16:08:15 +0000 | [diff] [blame] | 4134 | if (Any) |
| 4135 | LUThatHas->RecomputeRegs(LUThatHas - &Uses.front(), RegUses); |
| 4136 | |
| 4137 | // Delete the old use. |
| 4138 | DeleteUse(LU, LUIdx); |
| 4139 | --LUIdx; |
| 4140 | --NumUses; |
| 4141 | break; |
| 4142 | } |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4143 | } |
Jakub Staszak | 11bd835 | 2013-02-16 16:08:15 +0000 | [diff] [blame] | 4144 | |
| 4145 | DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs())); |
Dan Gohman | e9e0873 | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 4146 | } |
Dan Gohman | 20fab45 | 2010-05-19 23:43:12 +0000 | [diff] [blame] | 4147 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4148 | /// Call FilterOutUndesirableDedicatedRegisters again, if necessary, now that |
Dan Gohman | 002ff89 | 2010-08-29 16:39:22 +0000 | [diff] [blame] | 4149 | /// we've done more filtering, as it may be able to find more formulae to |
| 4150 | /// eliminate. |
| 4151 | void LSRInstance::NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters(){ |
| 4152 | if (EstimateSearchSpaceComplexity() >= ComplexityLimit) { |
| 4153 | DEBUG(dbgs() << "The search space is too complex.\n"); |
| 4154 | |
| 4155 | DEBUG(dbgs() << "Narrowing the search space by re-filtering out " |
| 4156 | "undesirable dedicated registers.\n"); |
| 4157 | |
| 4158 | FilterOutUndesirableDedicatedRegisters(); |
| 4159 | |
| 4160 | DEBUG(dbgs() << "After pre-selection:\n"; |
| 4161 | print_uses(dbgs())); |
| 4162 | } |
| 4163 | } |
| 4164 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4165 | /// Pick a register which seems likely to be profitable, and then in any use |
| 4166 | /// which has any reference to that register, delete all formulae which do not |
| 4167 | /// reference that register. |
Dan Gohman | e9e0873 | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 4168 | void LSRInstance::NarrowSearchSpaceByPickingWinnerRegs() { |
Dan Gohman | a4ca28a | 2010-05-20 20:52:00 +0000 | [diff] [blame] | 4169 | // With all other options exhausted, loop until the system is simple |
| 4170 | // enough to handle. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4171 | SmallPtrSet<const SCEV *, 4> Taken; |
Dan Gohman | a4eca05 | 2010-05-18 22:51:59 +0000 | [diff] [blame] | 4172 | while (EstimateSearchSpaceComplexity() >= ComplexityLimit) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4173 | // Ok, we have too many of formulae on our hands to conveniently handle. |
| 4174 | // Use a rough heuristic to thin out the list. |
Dan Gohman | 63e9015 | 2010-05-18 22:41:32 +0000 | [diff] [blame] | 4175 | DEBUG(dbgs() << "The search space is too complex.\n"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4176 | |
| 4177 | // Pick the register which is used by the most LSRUses, which is likely |
| 4178 | // to be a good reuse register candidate. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4179 | const SCEV *Best = nullptr; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4180 | unsigned BestNum = 0; |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4181 | for (const SCEV *Reg : RegUses) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4182 | if (Taken.count(Reg)) |
| 4183 | continue; |
Evgeny Stupachenko | 0c4300f | 2016-11-30 22:23:51 +0000 | [diff] [blame] | 4184 | if (!Best) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4185 | Best = Reg; |
Evgeny Stupachenko | 0c4300f | 2016-11-30 22:23:51 +0000 | [diff] [blame] | 4186 | BestNum = RegUses.getUsedByIndices(Reg).count(); |
| 4187 | } else { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4188 | unsigned Count = RegUses.getUsedByIndices(Reg).count(); |
| 4189 | if (Count > BestNum) { |
| 4190 | Best = Reg; |
| 4191 | BestNum = Count; |
| 4192 | } |
| 4193 | } |
| 4194 | } |
| 4195 | |
| 4196 | DEBUG(dbgs() << "Narrowing the search space by assuming " << *Best |
Dan Gohman | 8b0a419 | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 4197 | << " will yield profitable reuse.\n"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4198 | Taken.insert(Best); |
| 4199 | |
| 4200 | // In any use with formulae which references this register, delete formulae |
| 4201 | // which don't reference it. |
Dan Gohman | 4cf99b5 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 4202 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) { |
| 4203 | LSRUse &LU = Uses[LUIdx]; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4204 | if (!LU.Regs.count(Best)) continue; |
| 4205 | |
Dan Gohman | 4cf99b5 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 4206 | bool Any = false; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4207 | for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) { |
| 4208 | Formula &F = LU.Formulae[i]; |
| 4209 | if (!F.referencesReg(Best)) { |
| 4210 | DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n'); |
Dan Gohman | f1c7b1b | 2010-05-18 22:39:15 +0000 | [diff] [blame] | 4211 | LU.DeleteFormula(F); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4212 | --e; |
| 4213 | --i; |
Dan Gohman | 4cf99b5 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 4214 | Any = true; |
Dan Gohman | d080024 | 2010-05-07 23:36:59 +0000 | [diff] [blame] | 4215 | assert(e != 0 && "Use has no formulae left! Is Regs inconsistent?"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4216 | continue; |
| 4217 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4218 | } |
Dan Gohman | 4cf99b5 | 2010-05-18 23:42:37 +0000 | [diff] [blame] | 4219 | |
| 4220 | if (Any) |
| 4221 | LU.RecomputeRegs(LUIdx, RegUses); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | DEBUG(dbgs() << "After pre-selection:\n"; |
| 4225 | print_uses(dbgs())); |
| 4226 | } |
| 4227 | } |
| 4228 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4229 | /// If there are an extraordinary number of formulae to choose from, use some |
| 4230 | /// rough heuristics to prune down the number of formulae. This keeps the main |
| 4231 | /// solver from taking an extraordinary amount of time in some worst-case |
| 4232 | /// scenarios. |
Dan Gohman | e9e0873 | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 4233 | void LSRInstance::NarrowSearchSpaceUsingHeuristics() { |
| 4234 | NarrowSearchSpaceByDetectingSupersets(); |
| 4235 | NarrowSearchSpaceByCollapsingUnrolledCode(); |
Dan Gohman | 002ff89 | 2010-08-29 16:39:22 +0000 | [diff] [blame] | 4236 | NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters(); |
Dan Gohman | e9e0873 | 2010-08-29 16:09:42 +0000 | [diff] [blame] | 4237 | NarrowSearchSpaceByPickingWinnerRegs(); |
| 4238 | } |
| 4239 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4240 | /// This is the recursive solver. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4241 | void LSRInstance::SolveRecurse(SmallVectorImpl<const Formula *> &Solution, |
| 4242 | Cost &SolutionCost, |
| 4243 | SmallVectorImpl<const Formula *> &Workspace, |
| 4244 | const Cost &CurCost, |
| 4245 | const SmallPtrSet<const SCEV *, 16> &CurRegs, |
| 4246 | DenseSet<const SCEV *> &VisitedRegs) const { |
| 4247 | // Some ideas: |
| 4248 | // - prune more: |
| 4249 | // - use more aggressive filtering |
| 4250 | // - sort the formula so that the most profitable solutions are found first |
| 4251 | // - sort the uses too |
| 4252 | // - search faster: |
Dan Gohman | 8b0a419 | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 4253 | // - don't compute a cost, and then compare. compare while computing a cost |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4254 | // and bail early. |
| 4255 | // - track register sets with SmallBitVector |
| 4256 | |
| 4257 | const LSRUse &LU = Uses[Workspace.size()]; |
| 4258 | |
| 4259 | // If this use references any register that's already a part of the |
| 4260 | // in-progress solution, consider it a requirement that a formula must |
| 4261 | // reference that register in order to be considered. This prunes out |
| 4262 | // unprofitable searching. |
| 4263 | SmallSetVector<const SCEV *, 4> ReqRegs; |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 4264 | for (const SCEV *S : CurRegs) |
| 4265 | if (LU.Regs.count(S)) |
| 4266 | ReqRegs.insert(S); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4267 | |
| 4268 | SmallPtrSet<const SCEV *, 16> NewRegs; |
| 4269 | Cost NewCost; |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4270 | for (const Formula &F : LU.Formulae) { |
Adam Nemet | deab6f9 | 2014-04-29 18:25:28 +0000 | [diff] [blame] | 4271 | // Ignore formulae which may not be ideal in terms of register reuse of |
| 4272 | // ReqRegs. The formula should use all required registers before |
| 4273 | // introducing new ones. |
| 4274 | int NumReqRegsToFind = std::min(F.getNumRegs(), ReqRegs.size()); |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4275 | for (const SCEV *Reg : ReqRegs) { |
Adam Nemet | deab6f9 | 2014-04-29 18:25:28 +0000 | [diff] [blame] | 4276 | if ((F.ScaledReg && F.ScaledReg == Reg) || |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 4277 | is_contained(F.BaseRegs, Reg)) { |
Adam Nemet | deab6f9 | 2014-04-29 18:25:28 +0000 | [diff] [blame] | 4278 | --NumReqRegsToFind; |
| 4279 | if (NumReqRegsToFind == 0) |
| 4280 | break; |
Andrew Trick | e3502cb | 2012-03-22 22:42:51 +0000 | [diff] [blame] | 4281 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4282 | } |
Adam Nemet | deab6f9 | 2014-04-29 18:25:28 +0000 | [diff] [blame] | 4283 | if (NumReqRegsToFind != 0) { |
Andrew Trick | e3502cb | 2012-03-22 22:42:51 +0000 | [diff] [blame] | 4284 | // If none of the formulae satisfied the required registers, then we could |
| 4285 | // clear ReqRegs and try again. Currently, we simply give up in this case. |
| 4286 | continue; |
| 4287 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4288 | |
| 4289 | // Evaluate the cost of the current formula. If it's already worse than |
| 4290 | // the current best, prune the search at that point. |
| 4291 | NewCost = CurCost; |
| 4292 | NewRegs = CurRegs; |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 4293 | NewCost.RateFormula(TTI, F, NewRegs, VisitedRegs, L, SE, DT, LU); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4294 | if (NewCost < SolutionCost) { |
| 4295 | Workspace.push_back(&F); |
| 4296 | if (Workspace.size() != Uses.size()) { |
| 4297 | SolveRecurse(Solution, SolutionCost, Workspace, NewCost, |
| 4298 | NewRegs, VisitedRegs); |
| 4299 | if (F.getNumRegs() == 1 && Workspace.size() == 1) |
| 4300 | VisitedRegs.insert(F.ScaledReg ? F.ScaledReg : F.BaseRegs[0]); |
| 4301 | } else { |
| 4302 | DEBUG(dbgs() << "New best at "; NewCost.print(dbgs()); |
Andrew Trick | 4dc3eff | 2012-01-09 18:58:16 +0000 | [diff] [blame] | 4303 | dbgs() << ".\n Regs:"; |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 4304 | for (const SCEV *S : NewRegs) |
| 4305 | dbgs() << ' ' << *S; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4306 | dbgs() << '\n'); |
| 4307 | |
| 4308 | SolutionCost = NewCost; |
| 4309 | Solution = Workspace; |
| 4310 | } |
| 4311 | Workspace.pop_back(); |
| 4312 | } |
Dan Gohman | 5b18f03 | 2010-02-13 02:06:02 +0000 | [diff] [blame] | 4313 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4316 | /// Choose one formula from each use. Return the results in the given Solution |
| 4317 | /// vector. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4318 | void LSRInstance::Solve(SmallVectorImpl<const Formula *> &Solution) const { |
| 4319 | SmallVector<const Formula *, 8> Workspace; |
| 4320 | Cost SolutionCost; |
Tim Northover | bc6659c | 2014-01-22 13:27:00 +0000 | [diff] [blame] | 4321 | SolutionCost.Lose(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4322 | Cost CurCost; |
| 4323 | SmallPtrSet<const SCEV *, 16> CurRegs; |
| 4324 | DenseSet<const SCEV *> VisitedRegs; |
| 4325 | Workspace.reserve(Uses.size()); |
| 4326 | |
Dan Gohman | 8ec018c | 2010-05-20 20:00:41 +0000 | [diff] [blame] | 4327 | // SolveRecurse does all the work. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4328 | SolveRecurse(Solution, SolutionCost, Workspace, CurCost, |
| 4329 | CurRegs, VisitedRegs); |
Andrew Trick | 5812439 | 2011-09-27 00:44:14 +0000 | [diff] [blame] | 4330 | if (Solution.empty()) { |
| 4331 | DEBUG(dbgs() << "\nNo Satisfactory Solution\n"); |
| 4332 | return; |
| 4333 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4334 | |
| 4335 | // Ok, we've now made all our decisions. |
| 4336 | DEBUG(dbgs() << "\n" |
| 4337 | "The chosen solution requires "; SolutionCost.print(dbgs()); |
| 4338 | dbgs() << ":\n"; |
| 4339 | for (size_t i = 0, e = Uses.size(); i != e; ++i) { |
| 4340 | dbgs() << " "; |
| 4341 | Uses[i].print(dbgs()); |
| 4342 | dbgs() << "\n" |
| 4343 | " "; |
| 4344 | Solution[i]->print(dbgs()); |
| 4345 | dbgs() << '\n'; |
| 4346 | }); |
Dan Gohman | 6295f2e | 2010-05-20 20:59:23 +0000 | [diff] [blame] | 4347 | |
| 4348 | assert(Solution.size() == Uses.size() && "Malformed solution!"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4351 | /// Helper for AdjustInsertPositionForExpand. Climb up the dominator tree far as |
| 4352 | /// we can go while still being dominated by the input positions. This helps |
| 4353 | /// canonicalize the insert position, which encourages sharing. |
Dan Gohman | 607e02b | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 4354 | BasicBlock::iterator |
| 4355 | LSRInstance::HoistInsertPosition(BasicBlock::iterator IP, |
| 4356 | const SmallVectorImpl<Instruction *> &Inputs) |
| 4357 | const { |
Geoff Berry | 43e5160 | 2016-06-06 19:10:46 +0000 | [diff] [blame] | 4358 | Instruction *Tentative = &*IP; |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 4359 | while (true) { |
Geoff Berry | 43e5160 | 2016-06-06 19:10:46 +0000 | [diff] [blame] | 4360 | bool AllDominate = true; |
| 4361 | Instruction *BetterPos = nullptr; |
| 4362 | // Don't bother attempting to insert before a catchswitch, their basic block |
| 4363 | // cannot have other non-PHI instructions. |
| 4364 | if (isa<CatchSwitchInst>(Tentative)) |
| 4365 | return IP; |
| 4366 | |
| 4367 | for (Instruction *Inst : Inputs) { |
| 4368 | if (Inst == Tentative || !DT.dominates(Inst, Tentative)) { |
| 4369 | AllDominate = false; |
| 4370 | break; |
| 4371 | } |
| 4372 | // Attempt to find an insert position in the middle of the block, |
| 4373 | // instead of at the end, so that it can be used for other expansions. |
| 4374 | if (Tentative->getParent() == Inst->getParent() && |
| 4375 | (!BetterPos || !DT.dominates(Inst, BetterPos))) |
| 4376 | BetterPos = &*std::next(BasicBlock::iterator(Inst)); |
| 4377 | } |
| 4378 | if (!AllDominate) |
| 4379 | break; |
| 4380 | if (BetterPos) |
| 4381 | IP = BetterPos->getIterator(); |
| 4382 | else |
| 4383 | IP = Tentative->getIterator(); |
| 4384 | |
Dan Gohman | 607e02b | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 4385 | const Loop *IPLoop = LI.getLoopFor(IP->getParent()); |
| 4386 | unsigned IPLoopDepth = IPLoop ? IPLoop->getLoopDepth() : 0; |
| 4387 | |
| 4388 | BasicBlock *IDom; |
Dan Gohman | 8ce95cc | 2010-05-20 20:00:25 +0000 | [diff] [blame] | 4389 | for (DomTreeNode *Rung = DT.getNode(IP->getParent()); ; ) { |
Dan Gohman | 9b48b85 | 2010-05-20 22:46:54 +0000 | [diff] [blame] | 4390 | if (!Rung) return IP; |
Dan Gohman | 8ce95cc | 2010-05-20 20:00:25 +0000 | [diff] [blame] | 4391 | Rung = Rung->getIDom(); |
| 4392 | if (!Rung) return IP; |
| 4393 | IDom = Rung->getBlock(); |
Dan Gohman | 607e02b | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 4394 | |
| 4395 | // Don't climb into a loop though. |
| 4396 | const Loop *IDomLoop = LI.getLoopFor(IDom); |
| 4397 | unsigned IDomDepth = IDomLoop ? IDomLoop->getLoopDepth() : 0; |
| 4398 | if (IDomDepth <= IPLoopDepth && |
| 4399 | (IDomDepth != IPLoopDepth || IDomLoop == IPLoop)) |
| 4400 | break; |
| 4401 | } |
| 4402 | |
Geoff Berry | 43e5160 | 2016-06-06 19:10:46 +0000 | [diff] [blame] | 4403 | Tentative = IDom->getTerminator(); |
Dan Gohman | 607e02b | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | return IP; |
| 4407 | } |
| 4408 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4409 | /// Determine an input position which will be dominated by the operands and |
| 4410 | /// which will dominate the result. |
Dan Gohman | d2df643 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 4411 | BasicBlock::iterator |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 4412 | LSRInstance::AdjustInsertPositionForExpand(BasicBlock::iterator LowestIP, |
Dan Gohman | 607e02b | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 4413 | const LSRFixup &LF, |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 4414 | const LSRUse &LU, |
| 4415 | SCEVExpander &Rewriter) const { |
Dan Gohman | d2df643 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 4416 | // Collect some instructions which must be dominated by the |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 4417 | // expanding replacement. These must be dominated by any operands that |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4418 | // will be required in the expansion. |
| 4419 | SmallVector<Instruction *, 4> Inputs; |
| 4420 | if (Instruction *I = dyn_cast<Instruction>(LF.OperandValToReplace)) |
| 4421 | Inputs.push_back(I); |
| 4422 | if (LU.Kind == LSRUse::ICmpZero) |
| 4423 | if (Instruction *I = |
| 4424 | dyn_cast<Instruction>(cast<ICmpInst>(LF.UserInst)->getOperand(1))) |
| 4425 | Inputs.push_back(I); |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 4426 | if (LF.PostIncLoops.count(L)) { |
| 4427 | if (LF.isUseFullyOutsideLoop(L)) |
Dan Gohman | 52f5563 | 2010-03-02 01:59:21 +0000 | [diff] [blame] | 4428 | Inputs.push_back(L->getLoopLatch()->getTerminator()); |
| 4429 | else |
| 4430 | Inputs.push_back(IVIncInsertPos); |
| 4431 | } |
Dan Gohman | 4506539 | 2010-04-08 05:57:57 +0000 | [diff] [blame] | 4432 | // The expansion must also be dominated by the increment positions of any |
| 4433 | // loops it for which it is using post-inc mode. |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4434 | for (const Loop *PIL : LF.PostIncLoops) { |
Dan Gohman | 4506539 | 2010-04-08 05:57:57 +0000 | [diff] [blame] | 4435 | if (PIL == L) continue; |
| 4436 | |
Dan Gohman | 607e02b | 2010-04-09 22:07:05 +0000 | [diff] [blame] | 4437 | // Be dominated by the loop exit. |
Dan Gohman | 4506539 | 2010-04-08 05:57:57 +0000 | [diff] [blame] | 4438 | SmallVector<BasicBlock *, 4> ExitingBlocks; |
| 4439 | PIL->getExitingBlocks(ExitingBlocks); |
| 4440 | if (!ExitingBlocks.empty()) { |
| 4441 | BasicBlock *BB = ExitingBlocks[0]; |
| 4442 | for (unsigned i = 1, e = ExitingBlocks.size(); i != e; ++i) |
| 4443 | BB = DT.findNearestCommonDominator(BB, ExitingBlocks[i]); |
| 4444 | Inputs.push_back(BB->getTerminator()); |
| 4445 | } |
| 4446 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4447 | |
David Majnemer | ba275f9 | 2015-08-19 19:54:02 +0000 | [diff] [blame] | 4448 | assert(!isa<PHINode>(LowestIP) && !LowestIP->isEHPad() |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 4449 | && !isa<DbgInfoIntrinsic>(LowestIP) && |
| 4450 | "Insertion point must be a normal instruction"); |
| 4451 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4452 | // Then, climb up the immediate dominator tree as far as we can go while |
| 4453 | // still being dominated by the input positions. |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 4454 | BasicBlock::iterator IP = HoistInsertPosition(LowestIP, Inputs); |
Dan Gohman | d2df643 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 4455 | |
| 4456 | // Don't insert instructions before PHI nodes. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4457 | while (isa<PHINode>(IP)) ++IP; |
Dan Gohman | d2df643 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 4458 | |
Bill Wendling | 86c5cbe | 2011-08-24 21:06:46 +0000 | [diff] [blame] | 4459 | // Ignore landingpad instructions. |
David Majnemer | e09d035 | 2016-03-24 21:40:22 +0000 | [diff] [blame] | 4460 | while (IP->isEHPad()) ++IP; |
Bill Wendling | 86c5cbe | 2011-08-24 21:06:46 +0000 | [diff] [blame] | 4461 | |
Dan Gohman | d2df643 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 4462 | // Ignore debug intrinsics. |
Dan Gohman | d42e09d | 2010-03-26 00:33:27 +0000 | [diff] [blame] | 4463 | while (isa<DbgInfoIntrinsic>(IP)) ++IP; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4464 | |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 4465 | // Set IP below instructions recently inserted by SCEVExpander. This keeps the |
| 4466 | // IP consistent across expansions and allows the previously inserted |
| 4467 | // instructions to be reused by subsequent expansion. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 4468 | while (Rewriter.isInsertedInstruction(&*IP) && IP != LowestIP) |
| 4469 | ++IP; |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 4470 | |
Dan Gohman | d2df643 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 4471 | return IP; |
| 4472 | } |
| 4473 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4474 | /// Emit instructions for the leading candidate expression for this LSRUse (this |
| 4475 | /// is called "expanding"). |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 4476 | Value *LSRInstance::Expand(const LSRUse &LU, |
| 4477 | const LSRFixup &LF, |
Dan Gohman | d2df643 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 4478 | const Formula &F, |
| 4479 | BasicBlock::iterator IP, |
| 4480 | SCEVExpander &Rewriter, |
| 4481 | SmallVectorImpl<WeakVH> &DeadInsts) const { |
Andrew Trick | 57243da | 2013-10-25 21:35:56 +0000 | [diff] [blame] | 4482 | if (LU.RigidFormula) |
| 4483 | return LF.OperandValToReplace; |
Dan Gohman | d2df643 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 4484 | |
| 4485 | // Determine an input position which will be dominated by the operands and |
| 4486 | // which will dominate the result. |
Andrew Trick | c908b43 | 2012-01-20 07:41:13 +0000 | [diff] [blame] | 4487 | IP = AdjustInsertPositionForExpand(IP, LF, LU, Rewriter); |
Geoff Berry | d018280 | 2016-08-11 21:05:17 +0000 | [diff] [blame] | 4488 | Rewriter.setInsertPoint(&*IP); |
Dan Gohman | d2df643 | 2010-04-09 02:00:38 +0000 | [diff] [blame] | 4489 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4490 | // Inform the Rewriter if we have a post-increment use, so that it can |
| 4491 | // perform an advantageous expansion. |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 4492 | Rewriter.setPostInc(LF.PostIncLoops); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4493 | |
| 4494 | // This is the type that the user actually needs. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4495 | Type *OpTy = LF.OperandValToReplace->getType(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4496 | // This will be the type that we'll initially expand to. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4497 | Type *Ty = F.getType(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4498 | if (!Ty) |
| 4499 | // No type known; just expand directly to the ultimate type. |
| 4500 | Ty = OpTy; |
| 4501 | else if (SE.getEffectiveSCEVType(Ty) == SE.getEffectiveSCEVType(OpTy)) |
| 4502 | // Expand directly to the ultimate type if it's the right size. |
| 4503 | Ty = OpTy; |
| 4504 | // This is the type to do integer arithmetic in. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4505 | Type *IntTy = SE.getEffectiveSCEVType(Ty); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4506 | |
| 4507 | // Build up a list of operands to add together to form the full base. |
| 4508 | SmallVector<const SCEV *, 8> Ops; |
| 4509 | |
| 4510 | // Expand the BaseRegs portion. |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4511 | for (const SCEV *Reg : F.BaseRegs) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4512 | assert(!Reg->isZero() && "Zero allocated in a base register!"); |
| 4513 | |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 4514 | // If we're expanding for a post-inc user, make the post-inc adjustment. |
| 4515 | PostIncLoopSet &Loops = const_cast<PostIncLoopSet &>(LF.PostIncLoops); |
Sanjoy Das | 215df9e | 2015-08-04 01:52:05 +0000 | [diff] [blame] | 4516 | Reg = TransformForPostIncUse(Denormalize, Reg, |
| 4517 | LF.UserInst, LF.OperandValToReplace, |
| 4518 | Loops, SE, DT); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4519 | |
Geoff Berry | d018280 | 2016-08-11 21:05:17 +0000 | [diff] [blame] | 4520 | Ops.push_back(SE.getUnknown(Rewriter.expandCodeFor(Reg, nullptr))); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | // Expand the ScaledReg portion. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4524 | Value *ICmpScaledV = nullptr; |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 4525 | if (F.Scale != 0) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4526 | const SCEV *ScaledS = F.ScaledReg; |
| 4527 | |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 4528 | // If we're expanding for a post-inc user, make the post-inc adjustment. |
| 4529 | PostIncLoopSet &Loops = const_cast<PostIncLoopSet &>(LF.PostIncLoops); |
Sanjoy Das | 215df9e | 2015-08-04 01:52:05 +0000 | [diff] [blame] | 4530 | ScaledS = TransformForPostIncUse(Denormalize, ScaledS, |
| 4531 | LF.UserInst, LF.OperandValToReplace, |
| 4532 | Loops, SE, DT); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4533 | |
| 4534 | if (LU.Kind == LSRUse::ICmpZero) { |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 4535 | // Expand ScaleReg as if it was part of the base regs. |
| 4536 | if (F.Scale == 1) |
Sanjoy Das | 215df9e | 2015-08-04 01:52:05 +0000 | [diff] [blame] | 4537 | Ops.push_back( |
Geoff Berry | d018280 | 2016-08-11 21:05:17 +0000 | [diff] [blame] | 4538 | SE.getUnknown(Rewriter.expandCodeFor(ScaledS, nullptr))); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 4539 | else { |
| 4540 | // An interesting way of "folding" with an icmp is to use a negated |
| 4541 | // scale, which we'll implement by inserting it into the other operand |
| 4542 | // of the icmp. |
| 4543 | assert(F.Scale == -1 && |
| 4544 | "The only scale supported by ICmpZero uses is -1!"); |
Geoff Berry | d018280 | 2016-08-11 21:05:17 +0000 | [diff] [blame] | 4545 | ICmpScaledV = Rewriter.expandCodeFor(ScaledS, nullptr); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 4546 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4547 | } else { |
| 4548 | // Otherwise just expand the scaled register and an explicit scale, |
| 4549 | // which is expected to be matched as part of the address. |
Andrew Trick | 8370c7c | 2012-06-15 20:07:29 +0000 | [diff] [blame] | 4550 | |
| 4551 | // Flush the operand list to suppress SCEVExpander hoisting address modes. |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 4552 | // Unless the addressing mode will not be folded. |
| 4553 | if (!Ops.empty() && LU.Kind == LSRUse::Address && |
| 4554 | isAMCompletelyFolded(TTI, LU, F)) { |
Geoff Berry | d018280 | 2016-08-11 21:05:17 +0000 | [diff] [blame] | 4555 | Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty); |
Andrew Trick | 8370c7c | 2012-06-15 20:07:29 +0000 | [diff] [blame] | 4556 | Ops.clear(); |
| 4557 | Ops.push_back(SE.getUnknown(FullV)); |
| 4558 | } |
Geoff Berry | d018280 | 2016-08-11 21:05:17 +0000 | [diff] [blame] | 4559 | ScaledS = SE.getUnknown(Rewriter.expandCodeFor(ScaledS, nullptr)); |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 4560 | if (F.Scale != 1) |
| 4561 | ScaledS = |
| 4562 | SE.getMulExpr(ScaledS, SE.getConstant(ScaledS->getType(), F.Scale)); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4563 | Ops.push_back(ScaledS); |
| 4564 | } |
| 4565 | } |
| 4566 | |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 4567 | // Expand the GV portion. |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 4568 | if (F.BaseGV) { |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 4569 | // Flush the operand list to suppress SCEVExpander hoisting. |
Andrew Trick | 8370c7c | 2012-06-15 20:07:29 +0000 | [diff] [blame] | 4570 | if (!Ops.empty()) { |
Geoff Berry | d018280 | 2016-08-11 21:05:17 +0000 | [diff] [blame] | 4571 | Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty); |
Andrew Trick | 8370c7c | 2012-06-15 20:07:29 +0000 | [diff] [blame] | 4572 | Ops.clear(); |
| 4573 | Ops.push_back(SE.getUnknown(FullV)); |
| 4574 | } |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 4575 | Ops.push_back(SE.getUnknown(F.BaseGV)); |
Andrew Trick | 8370c7c | 2012-06-15 20:07:29 +0000 | [diff] [blame] | 4576 | } |
| 4577 | |
| 4578 | // Flush the operand list to suppress SCEVExpander hoisting of both folded and |
| 4579 | // unfolded offsets. LSR assumes they both live next to their uses. |
| 4580 | if (!Ops.empty()) { |
Geoff Berry | d018280 | 2016-08-11 21:05:17 +0000 | [diff] [blame] | 4581 | Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty); |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 4582 | Ops.clear(); |
| 4583 | Ops.push_back(SE.getUnknown(FullV)); |
| 4584 | } |
| 4585 | |
| 4586 | // Expand the immediate portion. |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 4587 | int64_t Offset = (uint64_t)F.BaseOffset + LF.Offset; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4588 | if (Offset != 0) { |
| 4589 | if (LU.Kind == LSRUse::ICmpZero) { |
| 4590 | // The other interesting way of "folding" with an ICmpZero is to use a |
| 4591 | // negated immediate. |
| 4592 | if (!ICmpScaledV) |
Eli Friedman | b46345d | 2011-10-13 23:48:33 +0000 | [diff] [blame] | 4593 | ICmpScaledV = ConstantInt::get(IntTy, -(uint64_t)Offset); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4594 | else { |
| 4595 | Ops.push_back(SE.getUnknown(ICmpScaledV)); |
| 4596 | ICmpScaledV = ConstantInt::get(IntTy, Offset); |
| 4597 | } |
| 4598 | } else { |
| 4599 | // Just add the immediate values. These again are expected to be matched |
| 4600 | // as part of the address. |
Dan Gohman | 29707de | 2010-03-03 05:29:13 +0000 | [diff] [blame] | 4601 | Ops.push_back(SE.getUnknown(ConstantInt::getSigned(IntTy, Offset))); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4602 | } |
| 4603 | } |
| 4604 | |
Dan Gohman | 6136e94 | 2011-05-03 00:46:49 +0000 | [diff] [blame] | 4605 | // Expand the unfolded offset portion. |
| 4606 | int64_t UnfoldedOffset = F.UnfoldedOffset; |
| 4607 | if (UnfoldedOffset != 0) { |
| 4608 | // Just add the immediate values. |
| 4609 | Ops.push_back(SE.getUnknown(ConstantInt::getSigned(IntTy, |
| 4610 | UnfoldedOffset))); |
| 4611 | } |
| 4612 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4613 | // Emit instructions summing all the operands. |
| 4614 | const SCEV *FullS = Ops.empty() ? |
Dan Gohman | 1d2ded7 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 4615 | SE.getConstant(IntTy, 0) : |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4616 | SE.getAddExpr(Ops); |
Geoff Berry | d018280 | 2016-08-11 21:05:17 +0000 | [diff] [blame] | 4617 | Value *FullV = Rewriter.expandCodeFor(FullS, Ty); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4618 | |
| 4619 | // We're done expanding now, so reset the rewriter. |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 4620 | Rewriter.clearPostInc(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4621 | |
| 4622 | // An ICmpZero Formula represents an ICmp which we're handling as a |
| 4623 | // comparison against zero. Now that we've expanded an expression for that |
| 4624 | // form, update the ICmp's other operand. |
| 4625 | if (LU.Kind == LSRUse::ICmpZero) { |
| 4626 | ICmpInst *CI = cast<ICmpInst>(LF.UserInst); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 4627 | DeadInsts.emplace_back(CI->getOperand(1)); |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 4628 | assert(!F.BaseGV && "ICmp does not support folding a global value and " |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4629 | "a scale at the same time!"); |
Chandler Carruth | 6e47932 | 2013-01-07 15:04:40 +0000 | [diff] [blame] | 4630 | if (F.Scale == -1) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4631 | if (ICmpScaledV->getType() != OpTy) { |
| 4632 | Instruction *Cast = |
| 4633 | CastInst::Create(CastInst::getCastOpcode(ICmpScaledV, false, |
| 4634 | OpTy, false), |
| 4635 | ICmpScaledV, OpTy, "tmp", CI); |
| 4636 | ICmpScaledV = Cast; |
| 4637 | } |
| 4638 | CI->setOperand(1, ICmpScaledV); |
| 4639 | } else { |
Quentin Colombet | c88baa5c | 2014-05-20 19:25:04 +0000 | [diff] [blame] | 4640 | // A scale of 1 means that the scale has been expanded as part of the |
| 4641 | // base regs. |
| 4642 | assert((F.Scale == 0 || F.Scale == 1) && |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4643 | "ICmp does not support folding a global value and " |
| 4644 | "a scale at the same time!"); |
| 4645 | Constant *C = ConstantInt::getSigned(SE.getEffectiveSCEVType(OpTy), |
| 4646 | -(uint64_t)Offset); |
| 4647 | if (C->getType() != OpTy) |
| 4648 | C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 4649 | OpTy, false), |
| 4650 | C, OpTy); |
| 4651 | |
| 4652 | CI->setOperand(1, C); |
| 4653 | } |
| 4654 | } |
| 4655 | |
| 4656 | return FullV; |
| 4657 | } |
| 4658 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4659 | /// Helper for Rewrite. PHI nodes are special because the use of their operands |
| 4660 | /// effectively happens in their predecessor blocks, so the expression may need |
| 4661 | /// to be expanded in multiple places. |
Dan Gohman | 6deab96 | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 4662 | void LSRInstance::RewriteForPHI(PHINode *PN, |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 4663 | const LSRUse &LU, |
Dan Gohman | 6deab96 | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 4664 | const LSRFixup &LF, |
| 4665 | const Formula &F, |
Dan Gohman | 6deab96 | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 4666 | SCEVExpander &Rewriter, |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 4667 | SmallVectorImpl<WeakVH> &DeadInsts) const { |
Dan Gohman | 6deab96 | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 4668 | DenseMap<BasicBlock *, Value *> Inserted; |
| 4669 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 4670 | if (PN->getIncomingValue(i) == LF.OperandValToReplace) { |
| 4671 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 4672 | |
| 4673 | // If this is a critical edge, split the edge so that we do not insert |
| 4674 | // the code on all predecessor/successor paths. We do this unless this |
| 4675 | // is the canonical backedge for this loop, which complicates post-inc |
| 4676 | // users. |
| 4677 | if (e != 1 && BB->getTerminator()->getNumSuccessors() > 1 && |
David Majnemer | bba1739 | 2017-01-13 22:24:27 +0000 | [diff] [blame] | 4678 | !isa<IndirectBrInst>(BB->getTerminator()) && |
| 4679 | !isa<CatchSwitchInst>(BB->getTerminator())) { |
Bill Wendling | 07efd6f | 2011-08-25 01:08:34 +0000 | [diff] [blame] | 4680 | BasicBlock *Parent = PN->getParent(); |
| 4681 | Loop *PNLoop = LI.getLoopFor(Parent); |
| 4682 | if (!PNLoop || Parent != PNLoop->getHeader()) { |
Dan Gohman | de7f699 | 2011-02-08 00:55:13 +0000 | [diff] [blame] | 4683 | // Split the critical edge. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4684 | BasicBlock *NewBB = nullptr; |
Bill Wendling | 3fb137f | 2011-08-25 05:55:40 +0000 | [diff] [blame] | 4685 | if (!Parent->isLandingPad()) { |
Chandler Carruth | 37df2cf | 2015-01-19 12:09:11 +0000 | [diff] [blame] | 4686 | NewBB = SplitCriticalEdge(BB, Parent, |
| 4687 | CriticalEdgeSplittingOptions(&DT, &LI) |
| 4688 | .setMergeIdenticalEdges() |
| 4689 | .setDontDeleteUselessPHIs()); |
Bill Wendling | 3fb137f | 2011-08-25 05:55:40 +0000 | [diff] [blame] | 4690 | } else { |
| 4691 | SmallVector<BasicBlock*, 2> NewBBs; |
Chandler Carruth | 96ada25 | 2015-07-22 09:52:54 +0000 | [diff] [blame] | 4692 | SplitLandingPadPredecessors(Parent, BB, "", "", NewBBs, &DT, &LI); |
Bill Wendling | 3fb137f | 2011-08-25 05:55:40 +0000 | [diff] [blame] | 4693 | NewBB = NewBBs[0]; |
| 4694 | } |
Andrew Trick | 402edbb | 2012-09-18 17:51:33 +0000 | [diff] [blame] | 4695 | // If NewBB==NULL, then SplitCriticalEdge refused to split because all |
| 4696 | // phi predecessors are identical. The simple thing to do is skip |
| 4697 | // splitting in this case rather than complicate the API. |
| 4698 | if (NewBB) { |
| 4699 | // If PN is outside of the loop and BB is in the loop, we want to |
| 4700 | // move the block to be immediately before the PHI block, not |
| 4701 | // immediately after BB. |
| 4702 | if (L->contains(BB) && !L->contains(PN)) |
| 4703 | NewBB->moveBefore(PN->getParent()); |
Dan Gohman | 6deab96 | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 4704 | |
Andrew Trick | 402edbb | 2012-09-18 17:51:33 +0000 | [diff] [blame] | 4705 | // Splitting the edge can reduce the number of PHI entries we have. |
| 4706 | e = PN->getNumIncomingValues(); |
| 4707 | BB = NewBB; |
| 4708 | i = PN->getBasicBlockIndex(BB); |
| 4709 | } |
Dan Gohman | de7f699 | 2011-02-08 00:55:13 +0000 | [diff] [blame] | 4710 | } |
Dan Gohman | 6deab96 | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> Pair = |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4714 | Inserted.insert(std::make_pair(BB, static_cast<Value *>(nullptr))); |
Dan Gohman | 6deab96 | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 4715 | if (!Pair.second) |
| 4716 | PN->setIncomingValue(i, Pair.first->second); |
| 4717 | else { |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 4718 | Value *FullV = Expand(LU, LF, F, BB->getTerminator()->getIterator(), |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 4719 | Rewriter, DeadInsts); |
Dan Gohman | 6deab96 | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 4720 | |
| 4721 | // If this is reuse-by-noop-cast, insert the noop cast. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4722 | Type *OpTy = LF.OperandValToReplace->getType(); |
Dan Gohman | 6deab96 | 2010-02-16 20:25:07 +0000 | [diff] [blame] | 4723 | if (FullV->getType() != OpTy) |
| 4724 | FullV = |
| 4725 | CastInst::Create(CastInst::getCastOpcode(FullV, false, |
| 4726 | OpTy, false), |
| 4727 | FullV, LF.OperandValToReplace->getType(), |
| 4728 | "tmp", BB->getTerminator()); |
| 4729 | |
| 4730 | PN->setIncomingValue(i, FullV); |
| 4731 | Pair.first->second = FullV; |
| 4732 | } |
| 4733 | } |
| 4734 | } |
| 4735 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4736 | /// Emit instructions for the leading candidate expression for this LSRUse (this |
| 4737 | /// is called "expanding"), and update the UserInst to reference the newly |
| 4738 | /// expanded value. |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 4739 | void LSRInstance::Rewrite(const LSRUse &LU, |
| 4740 | const LSRFixup &LF, |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4741 | const Formula &F, |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4742 | SCEVExpander &Rewriter, |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 4743 | SmallVectorImpl<WeakVH> &DeadInsts) const { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4744 | // First, find an insertion point that dominates UserInst. For PHI nodes, |
| 4745 | // find the nearest block which dominates all the relevant uses. |
| 4746 | if (PHINode *PN = dyn_cast<PHINode>(LF.UserInst)) { |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 4747 | RewriteForPHI(PN, LU, LF, F, Rewriter, DeadInsts); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4748 | } else { |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 4749 | Value *FullV = |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 4750 | Expand(LU, LF, F, LF.UserInst->getIterator(), Rewriter, DeadInsts); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4751 | |
| 4752 | // If this is reuse-by-noop-cast, insert the noop cast. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4753 | Type *OpTy = LF.OperandValToReplace->getType(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4754 | if (FullV->getType() != OpTy) { |
| 4755 | Instruction *Cast = |
| 4756 | CastInst::Create(CastInst::getCastOpcode(FullV, false, OpTy, false), |
| 4757 | FullV, OpTy, "tmp", LF.UserInst); |
| 4758 | FullV = Cast; |
| 4759 | } |
| 4760 | |
| 4761 | // Update the user. ICmpZero is handled specially here (for now) because |
| 4762 | // Expand may have updated one of the operands of the icmp already, and |
| 4763 | // its new value may happen to be equal to LF.OperandValToReplace, in |
| 4764 | // which case doing replaceUsesOfWith leads to replacing both operands |
| 4765 | // with the same value. TODO: Reorganize this. |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 4766 | if (LU.Kind == LSRUse::ICmpZero) |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4767 | LF.UserInst->setOperand(0, FullV); |
| 4768 | else |
| 4769 | LF.UserInst->replaceUsesOfWith(LF.OperandValToReplace, FullV); |
| 4770 | } |
| 4771 | |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 4772 | DeadInsts.emplace_back(LF.OperandValToReplace); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4773 | } |
| 4774 | |
Sanjoy Das | 94c4aec | 2015-08-16 18:22:46 +0000 | [diff] [blame] | 4775 | /// Rewrite all the fixup locations with new values, following the chosen |
| 4776 | /// solution. |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 4777 | void LSRInstance::ImplementSolution( |
| 4778 | const SmallVectorImpl<const Formula *> &Solution) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4779 | // Keep track of instructions we may have made dead, so that |
| 4780 | // we can remove them after we are done working. |
| 4781 | SmallVector<WeakVH, 16> DeadInsts; |
| 4782 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4783 | SCEVExpander Rewriter(SE, L->getHeader()->getModule()->getDataLayout(), |
| 4784 | "lsr"); |
Andrew Trick | 4dc3eff | 2012-01-09 18:58:16 +0000 | [diff] [blame] | 4785 | #ifndef NDEBUG |
| 4786 | Rewriter.setDebugType(DEBUG_TYPE); |
| 4787 | #endif |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4788 | Rewriter.disableCanonicalMode(); |
Andrew Trick | 7fb669a | 2011-10-07 23:46:21 +0000 | [diff] [blame] | 4789 | Rewriter.enableLSRMode(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4790 | Rewriter.setIVIncInsertPos(L, IVIncInsertPos); |
| 4791 | |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 4792 | // Mark phi nodes that terminate chains so the expander tries to reuse them. |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4793 | for (const IVChain &Chain : IVChainVec) { |
| 4794 | if (PHINode *PN = dyn_cast<PHINode>(Chain.tailUserInst())) |
Andrew Trick | d5d2db9 | 2012-01-10 01:45:08 +0000 | [diff] [blame] | 4795 | Rewriter.setChainedPhi(PN); |
| 4796 | } |
| 4797 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4798 | // Expand the new value definitions and update the users. |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 4799 | for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) |
| 4800 | for (const LSRFixup &Fixup : Uses[LUIdx].Fixups) { |
| 4801 | Rewrite(Uses[LUIdx], Fixup, *Solution[LUIdx], Rewriter, DeadInsts); |
| 4802 | Changed = true; |
| 4803 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4804 | |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4805 | for (const IVChain &Chain : IVChainVec) { |
| 4806 | GenerateIVChain(Chain, Rewriter, DeadInsts); |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 4807 | Changed = true; |
| 4808 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4809 | // Clean up after ourselves. This must be done before deleting any |
| 4810 | // instructions. |
| 4811 | Rewriter.clear(); |
| 4812 | |
| 4813 | Changed |= DeleteTriviallyDeadInstructions(DeadInsts); |
| 4814 | } |
| 4815 | |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 4816 | LSRInstance::LSRInstance(Loop *L, IVUsers &IU, ScalarEvolution &SE, |
| 4817 | DominatorTree &DT, LoopInfo &LI, |
| 4818 | const TargetTransformInfo &TTI) |
| 4819 | : IU(IU), SE(SE), DT(DT), LI(LI), TTI(TTI), L(L), Changed(false), |
| 4820 | IVIncInsertPos(nullptr) { |
Dan Gohman | a83ac2d | 2009-11-05 21:11:53 +0000 | [diff] [blame] | 4821 | // If LoopSimplify form is not available, stay out of trouble. |
Andrew Trick | 732ad80 | 2012-01-07 03:16:50 +0000 | [diff] [blame] | 4822 | if (!L->isLoopSimplifyForm()) |
| 4823 | return; |
Dan Gohman | a83ac2d | 2009-11-05 21:11:53 +0000 | [diff] [blame] | 4824 | |
Andrew Trick | 070e540 | 2012-03-16 03:16:56 +0000 | [diff] [blame] | 4825 | // If there's no interesting work to be done, bail early. |
| 4826 | if (IU.empty()) return; |
| 4827 | |
Andrew Trick | 19f80c1 | 2012-04-18 04:00:10 +0000 | [diff] [blame] | 4828 | // If there's too much analysis to be done, bail early. We won't be able to |
| 4829 | // model the problem anyway. |
| 4830 | unsigned NumUsers = 0; |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4831 | for (const IVStrideUse &U : IU) { |
Andrew Trick | 19f80c1 | 2012-04-18 04:00:10 +0000 | [diff] [blame] | 4832 | if (++NumUsers > MaxIVUsers) { |
Craig Topper | 37d0d86 | 2015-05-23 08:20:33 +0000 | [diff] [blame] | 4833 | (void)U; |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4834 | DEBUG(dbgs() << "LSR skipping loop, too many IV Users in " << U << "\n"); |
Andrew Trick | 19f80c1 | 2012-04-18 04:00:10 +0000 | [diff] [blame] | 4835 | return; |
| 4836 | } |
David Majnemer | a53b5bb | 2016-02-03 21:30:34 +0000 | [diff] [blame] | 4837 | // Bail out if we have a PHI on an EHPad that gets a value from a |
| 4838 | // CatchSwitchInst. Because the CatchSwitchInst cannot be split, there is |
| 4839 | // no good place to stick any instructions. |
| 4840 | if (auto *PN = dyn_cast<PHINode>(U.getUser())) { |
| 4841 | auto *FirstNonPHI = PN->getParent()->getFirstNonPHI(); |
| 4842 | if (isa<FuncletPadInst>(FirstNonPHI) || |
| 4843 | isa<CatchSwitchInst>(FirstNonPHI)) |
| 4844 | for (BasicBlock *PredBB : PN->blocks()) |
| 4845 | if (isa<CatchSwitchInst>(PredBB->getFirstNonPHI())) |
| 4846 | return; |
| 4847 | } |
Andrew Trick | 19f80c1 | 2012-04-18 04:00:10 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
Andrew Trick | 070e540 | 2012-03-16 03:16:56 +0000 | [diff] [blame] | 4850 | #ifndef NDEBUG |
Andrew Trick | 12728f0 | 2012-01-17 06:45:52 +0000 | [diff] [blame] | 4851 | // All dominating loops must have preheaders, or SCEVExpander may not be able |
| 4852 | // to materialize an AddRecExpr whose Start is an outer AddRecExpr. |
| 4853 | // |
Andrew Trick | 070e540 | 2012-03-16 03:16:56 +0000 | [diff] [blame] | 4854 | // IVUsers analysis should only create users that are dominated by simple loop |
| 4855 | // headers. Since this loop should dominate all of its users, its user list |
| 4856 | // should be empty if this loop itself is not within a simple loop nest. |
Andrew Trick | 12728f0 | 2012-01-17 06:45:52 +0000 | [diff] [blame] | 4857 | for (DomTreeNode *Rung = DT.getNode(L->getLoopPreheader()); |
| 4858 | Rung; Rung = Rung->getIDom()) { |
| 4859 | BasicBlock *BB = Rung->getBlock(); |
| 4860 | const Loop *DomLoop = LI.getLoopFor(BB); |
| 4861 | if (DomLoop && DomLoop->getHeader() == BB) { |
Andrew Trick | 070e540 | 2012-03-16 03:16:56 +0000 | [diff] [blame] | 4862 | assert(DomLoop->getLoopPreheader() && "LSR needs a simplified loop nest"); |
Andrew Trick | 12728f0 | 2012-01-17 06:45:52 +0000 | [diff] [blame] | 4863 | } |
Andrew Trick | 732ad80 | 2012-01-07 03:16:50 +0000 | [diff] [blame] | 4864 | } |
Andrew Trick | 070e540 | 2012-03-16 03:16:56 +0000 | [diff] [blame] | 4865 | #endif // DEBUG |
Dan Gohman | 85875f7 | 2009-03-09 20:34:59 +0000 | [diff] [blame] | 4866 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4867 | DEBUG(dbgs() << "\nLSR on loop "; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 4868 | L->getHeader()->printAsOperand(dbgs(), /*PrintType=*/false); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4869 | dbgs() << ":\n"); |
Dan Gohman | e201f8f | 2009-03-09 20:46:50 +0000 | [diff] [blame] | 4870 | |
Dan Gohman | 927bcaa | 2010-05-20 20:33:18 +0000 | [diff] [blame] | 4871 | // First, perform some low-level loop optimizations. |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4872 | OptimizeShadowIV(); |
Dan Gohman | 4c4043c | 2010-05-20 20:05:31 +0000 | [diff] [blame] | 4873 | OptimizeLoopTermCond(); |
Evan Cheng | 78a4eb8 | 2009-05-11 22:33:01 +0000 | [diff] [blame] | 4874 | |
Andrew Trick | 8acb434 | 2011-07-21 00:40:04 +0000 | [diff] [blame] | 4875 | // If loop preparation eliminates all interesting IV users, bail. |
| 4876 | if (IU.empty()) return; |
| 4877 | |
Andrew Trick | 168dfff | 2011-09-29 01:53:08 +0000 | [diff] [blame] | 4878 | // Skip nested loops until we can model them better with formulae. |
Andrew Trick | d97b83e | 2012-03-22 22:42:45 +0000 | [diff] [blame] | 4879 | if (!L->empty()) { |
Andrew Trick | bc6de90 | 2011-09-29 01:33:38 +0000 | [diff] [blame] | 4880 | DEBUG(dbgs() << "LSR skipping outer loop " << *L << "\n"); |
Andrew Trick | 168dfff | 2011-09-29 01:53:08 +0000 | [diff] [blame] | 4881 | return; |
Andrew Trick | bc6de90 | 2011-09-29 01:33:38 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
Dan Gohman | 927bcaa | 2010-05-20 20:33:18 +0000 | [diff] [blame] | 4884 | // Start collecting data and preparing for the solver. |
Andrew Trick | 29fe5f0 | 2012-01-09 19:50:34 +0000 | [diff] [blame] | 4885 | CollectChains(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4886 | CollectInterestingTypesAndFactors(); |
| 4887 | CollectFixupsAndInitialFormulae(); |
| 4888 | CollectLoopInvariantFixupsAndFormulae(); |
Chris Lattner | 9bfa6f8 | 2005-08-08 05:28:22 +0000 | [diff] [blame] | 4889 | |
Andrew Trick | 248d410 | 2012-01-09 21:18:52 +0000 | [diff] [blame] | 4890 | assert(!Uses.empty() && "IVUsers reported at least one use"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4891 | DEBUG(dbgs() << "LSR found " << Uses.size() << " uses:\n"; |
| 4892 | print_uses(dbgs())); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4893 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4894 | // Now use the reuse data to generate a bunch of interesting ways |
| 4895 | // to formulate the values needed for the uses. |
| 4896 | GenerateAllReuseFormulae(); |
Evan Cheng | 3df447d | 2006-03-16 21:53:05 +0000 | [diff] [blame] | 4897 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4898 | FilterOutUndesirableDedicatedRegisters(); |
| 4899 | NarrowSearchSpaceUsingHeuristics(); |
Dan Gohman | 92c3696 | 2009-12-18 00:06:20 +0000 | [diff] [blame] | 4900 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4901 | SmallVector<const Formula *, 8> Solution; |
| 4902 | Solve(Solution); |
Dan Gohman | 92c3696 | 2009-12-18 00:06:20 +0000 | [diff] [blame] | 4903 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4904 | // Release memory that is no longer needed. |
| 4905 | Factors.clear(); |
| 4906 | Types.clear(); |
| 4907 | RegUses.clear(); |
| 4908 | |
Andrew Trick | 5812439 | 2011-09-27 00:44:14 +0000 | [diff] [blame] | 4909 | if (Solution.empty()) |
| 4910 | return; |
| 4911 | |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4912 | #ifndef NDEBUG |
| 4913 | // Formulae should be legal. |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4914 | for (const LSRUse &LU : Uses) { |
| 4915 | for (const Formula &F : LU.Formulae) |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 4916 | assert(isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4917 | F) && "Illegal formula generated!"); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4918 | }; |
| 4919 | #endif |
| 4920 | |
| 4921 | // Now that we've decided what we want, make it so. |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 4922 | ImplementSolution(Solution); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4923 | } |
| 4924 | |
| 4925 | void LSRInstance::print_factors_and_types(raw_ostream &OS) const { |
| 4926 | if (Factors.empty() && Types.empty()) return; |
| 4927 | |
| 4928 | OS << "LSR has identified the following interesting factors and types: "; |
| 4929 | bool First = true; |
| 4930 | |
Craig Topper | 10949ae | 2015-05-23 08:45:10 +0000 | [diff] [blame] | 4931 | for (int64_t Factor : Factors) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4932 | if (!First) OS << ", "; |
| 4933 | First = false; |
Craig Topper | 10949ae | 2015-05-23 08:45:10 +0000 | [diff] [blame] | 4934 | OS << '*' << Factor; |
Evan Cheng | 87fe40b | 2009-11-10 21:14:05 +0000 | [diff] [blame] | 4935 | } |
Dale Johannesen | 02cb2bf | 2009-05-11 17:15:42 +0000 | [diff] [blame] | 4936 | |
Craig Topper | 10949ae | 2015-05-23 08:45:10 +0000 | [diff] [blame] | 4937 | for (Type *Ty : Types) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4938 | if (!First) OS << ", "; |
| 4939 | First = false; |
Craig Topper | 10949ae | 2015-05-23 08:45:10 +0000 | [diff] [blame] | 4940 | OS << '(' << *Ty << ')'; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4941 | } |
| 4942 | OS << '\n'; |
| 4943 | } |
| 4944 | |
| 4945 | void LSRInstance::print_fixups(raw_ostream &OS) const { |
| 4946 | OS << "LSR is examining the following fixup sites:\n"; |
Jonas Paulsson | 7a79422 | 2016-08-17 13:24:19 +0000 | [diff] [blame] | 4947 | for (const LSRUse &LU : Uses) |
| 4948 | for (const LSRFixup &LF : LU.Fixups) { |
| 4949 | dbgs() << " "; |
| 4950 | LF.print(OS); |
| 4951 | OS << '\n'; |
| 4952 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
| 4955 | void LSRInstance::print_uses(raw_ostream &OS) const { |
| 4956 | OS << "LSR is examining the following uses:\n"; |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4957 | for (const LSRUse &LU : Uses) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4958 | dbgs() << " "; |
| 4959 | LU.print(OS); |
| 4960 | OS << '\n'; |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4961 | for (const Formula &F : LU.Formulae) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4962 | OS << " "; |
Craig Topper | 77b9941 | 2015-05-23 08:01:41 +0000 | [diff] [blame] | 4963 | F.print(OS); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4964 | OS << '\n'; |
| 4965 | } |
| 4966 | } |
| 4967 | } |
| 4968 | |
| 4969 | void LSRInstance::print(raw_ostream &OS) const { |
| 4970 | print_factors_and_types(OS); |
| 4971 | print_fixups(OS); |
| 4972 | print_uses(OS); |
| 4973 | } |
| 4974 | |
Davide Italiano | 945d05f | 2015-11-23 02:47:30 +0000 | [diff] [blame] | 4975 | LLVM_DUMP_METHOD |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4976 | void LSRInstance::dump() const { |
| 4977 | print(errs()); errs() << '\n'; |
| 4978 | } |
| 4979 | |
| 4980 | namespace { |
| 4981 | |
| 4982 | class LoopStrengthReduce : public LoopPass { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4983 | public: |
| 4984 | static char ID; // Pass ID, replacement for typeid |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 4985 | |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 4986 | LoopStrengthReduce(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4987 | |
| 4988 | private: |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 4989 | bool runOnLoop(Loop *L, LPPassManager &LPM) override; |
| 4990 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4991 | }; |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4992 | |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 4993 | } // end anonymous namespace |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4994 | |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 4995 | LoopStrengthReduce::LoopStrengthReduce() : LoopPass(ID) { |
| 4996 | initializeLoopStrengthReducePass(*PassRegistry::getPassRegistry()); |
| 4997 | } |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 4998 | |
| 4999 | void LoopStrengthReduce::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5000 | // We split critical edges, so we change the CFG. However, we do update |
| 5001 | // many analyses if they are around. |
Eric Christopher | da6bd45 | 2011-02-10 01:48:24 +0000 | [diff] [blame] | 5002 | AU.addPreservedID(LoopSimplifyID); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 5003 | |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 5004 | AU.addRequired<LoopInfoWrapperPass>(); |
| 5005 | AU.addPreserved<LoopInfoWrapperPass>(); |
Eric Christopher | da6bd45 | 2011-02-10 01:48:24 +0000 | [diff] [blame] | 5006 | AU.addRequiredID(LoopSimplifyID); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 5007 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 5008 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 5009 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
| 5010 | AU.addPreserved<ScalarEvolutionWrapperPass>(); |
Cameron Zwarich | 97dae4d | 2011-02-10 23:53:14 +0000 | [diff] [blame] | 5011 | // Requiring LoopSimplify a second time here prevents IVUsers from running |
| 5012 | // twice, since LoopSimplify was invalidated by running ScalarEvolution. |
| 5013 | AU.addRequiredID(LoopSimplifyID); |
Dehao Chen | 1a44452 | 2016-07-16 22:51:33 +0000 | [diff] [blame] | 5014 | AU.addRequired<IVUsersWrapperPass>(); |
| 5015 | AU.addPreserved<IVUsersWrapperPass>(); |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 5016 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
Dehao Chen | 6132ee8 | 2016-07-18 21:41:50 +0000 | [diff] [blame] | 5019 | static bool ReduceLoopStrength(Loop *L, IVUsers &IU, ScalarEvolution &SE, |
| 5020 | DominatorTree &DT, LoopInfo &LI, |
| 5021 | const TargetTransformInfo &TTI) { |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 5022 | bool Changed = false; |
| 5023 | |
| 5024 | // Run the main LSR transformation. |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 5025 | Changed |= LSRInstance(L, IU, SE, DT, LI, TTI).getChanged(); |
Dan Gohman | 45774ce | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 5026 | |
Andrew Trick | 2ec61a8 | 2012-01-07 01:36:44 +0000 | [diff] [blame] | 5027 | // Remove any extra phis created by processing inner loops. |
Dan Gohman | b535800 | 2010-01-05 16:31:45 +0000 | [diff] [blame] | 5028 | Changed |= DeleteDeadPHIs(L->getHeader()); |
Andrew Trick | f950ce8 | 2013-01-06 05:59:39 +0000 | [diff] [blame] | 5029 | if (EnablePhiElim && L->isLoopSimplifyForm()) { |
Andrew Trick | 2ec61a8 | 2012-01-07 01:36:44 +0000 | [diff] [blame] | 5030 | SmallVector<WeakVH, 16> DeadInsts; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 5031 | const DataLayout &DL = L->getHeader()->getModule()->getDataLayout(); |
Dehao Chen | 6132ee8 | 2016-07-18 21:41:50 +0000 | [diff] [blame] | 5032 | SCEVExpander Rewriter(SE, DL, "lsr"); |
Andrew Trick | 2ec61a8 | 2012-01-07 01:36:44 +0000 | [diff] [blame] | 5033 | #ifndef NDEBUG |
| 5034 | Rewriter.setDebugType(DEBUG_TYPE); |
| 5035 | #endif |
Dehao Chen | 6132ee8 | 2016-07-18 21:41:50 +0000 | [diff] [blame] | 5036 | unsigned numFolded = Rewriter.replaceCongruentIVs(L, &DT, DeadInsts, &TTI); |
Andrew Trick | 2ec61a8 | 2012-01-07 01:36:44 +0000 | [diff] [blame] | 5037 | if (numFolded) { |
| 5038 | Changed = true; |
| 5039 | DeleteTriviallyDeadInstructions(DeadInsts); |
| 5040 | DeleteDeadPHIs(L->getHeader()); |
| 5041 | } |
| 5042 | } |
Evan Cheng | 03001cb | 2008-07-07 19:51:32 +0000 | [diff] [blame] | 5043 | return Changed; |
Nate Begeman | b18121e | 2004-10-18 21:08:22 +0000 | [diff] [blame] | 5044 | } |
Dehao Chen | 6132ee8 | 2016-07-18 21:41:50 +0000 | [diff] [blame] | 5045 | |
| 5046 | bool LoopStrengthReduce::runOnLoop(Loop *L, LPPassManager & /*LPM*/) { |
| 5047 | if (skipLoop(L)) |
| 5048 | return false; |
| 5049 | |
| 5050 | auto &IU = getAnalysis<IVUsersWrapperPass>().getIU(); |
| 5051 | auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 5052 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 5053 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 5054 | const auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI( |
| 5055 | *L->getHeader()->getParent()); |
| 5056 | return ReduceLoopStrength(L, IU, SE, DT, LI, TTI); |
| 5057 | } |
| 5058 | |
Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 5059 | PreservedAnalyses LoopStrengthReducePass::run(Loop &L, LoopAnalysisManager &AM, |
| 5060 | LoopStandardAnalysisResults &AR, |
| 5061 | LPMUpdater &) { |
| 5062 | if (!ReduceLoopStrength(&L, AM.getResult<IVUsersAnalysis>(L, AR), AR.SE, |
| 5063 | AR.DT, AR.LI, AR.TTI)) |
Dehao Chen | 6132ee8 | 2016-07-18 21:41:50 +0000 | [diff] [blame] | 5064 | return PreservedAnalyses::all(); |
| 5065 | |
| 5066 | return getLoopPassPreservedAnalyses(); |
| 5067 | } |
Eugene Zelenko | a3fe70d | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 5068 | |
| 5069 | char LoopStrengthReduce::ID = 0; |
| 5070 | INITIALIZE_PASS_BEGIN(LoopStrengthReduce, "loop-reduce", |
| 5071 | "Loop Strength Reduction", false, false) |
| 5072 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
| 5073 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 5074 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
| 5075 | INITIALIZE_PASS_DEPENDENCY(IVUsersWrapperPass) |
| 5076 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
| 5077 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 5078 | INITIALIZE_PASS_END(LoopStrengthReduce, "loop-reduce", |
| 5079 | "Loop Strength Reduction", false, false) |
| 5080 | |
| 5081 | Pass *llvm::createLoopStrengthReducePass() { return new LoopStrengthReduce(); } |