blob: 923707737ad6ac312044759ed1b6d5c7e446eb72 [file] [log] [blame]
Dan Gohman2d1be872009-04-16 03:18:22 +00001//===- LoopStrengthReduce.cpp - Strength Reduce IVs in Loops --------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Nate Begemaneaa13852004-10-18 21:08:22 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
Nate Begemaneaa13852004-10-18 21:08:22 +00008//===----------------------------------------------------------------------===//
9//
Dan Gohmancec8f9d2009-05-19 20:37:36 +000010// 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 Begemaneaa13852004-10-18 21:08:22 +000014// This pass performs a strength reduction on array references inside loops that
Dan Gohmancec8f9d2009-05-19 20:37:36 +000015// 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 Begemaneaa13852004-10-18 21:08:22 +000019//
Dan Gohman572645c2010-02-12 10:34:29 +000020// Terminology note: this code has a lot of handling for "post-increment" or
21// "post-inc" users. This is not talking about post-increment addressing modes;
22// it is instead talking about code like this:
23//
24// %i = phi [ 0, %entry ], [ %i.next, %latch ]
25// ...
26// %i.next = add %i, 1
27// %c = icmp eq %i.next, %n
28//
29// The SCEV for %i is {0,+,1}<%L>. The SCEV for %i.next is {1,+,1}<%L>, however
30// it's useful to think about these as the same register, with some uses using
31// the value of the register before the add and some using // it after. In this
32// example, the icmp is a post-increment user, since it uses %i.next, which is
33// the value of the induction variable after the increment. The other common
34// case of post-increment users is users outside the loop.
35//
36// TODO: More sophistication in the way Formulae are generated and filtered.
37//
38// TODO: Handle multiple loops at a time.
39//
Chandler Carruthe4ba75f2013-01-07 14:41:08 +000040// TODO: Should the addressing mode BaseGV be changed to a ConstantExpr instead
41// of a GlobalValue?
Dan Gohman572645c2010-02-12 10:34:29 +000042//
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 Begemaneaa13852004-10-18 21:08:22 +000054//===----------------------------------------------------------------------===//
55
Chris Lattnerbe3e5212005-08-03 23:30:08 +000056#define DEBUG_TYPE "loop-reduce"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000057#include "llvm/Transforms/Scalar.h"
58#include "llvm/ADT/DenseSet.h"
59#include "llvm/ADT/SetVector.h"
60#include "llvm/ADT/SmallBitVector.h"
Dan Gohman572645c2010-02-12 10:34:29 +000061#include "llvm/Analysis/Dominators.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000062#include "llvm/Analysis/IVUsers.h"
Devang Patel0f54dcb2007-03-06 21:14:09 +000063#include "llvm/Analysis/LoopPass.h"
Nate Begeman16997482005-07-30 00:15:07 +000064#include "llvm/Analysis/ScalarEvolutionExpander.h"
Chandler Carruthe4ba75f2013-01-07 14:41:08 +000065#include "llvm/Analysis/TargetTransformInfo.h"
Chris Lattner9fc5cdf2011-01-02 22:09:33 +000066#include "llvm/Assembly/Writer.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000067#include "llvm/IR/Constants.h"
68#include "llvm/IR/DerivedTypes.h"
69#include "llvm/IR/Instructions.h"
70#include "llvm/IR/IntrinsicInst.h"
Andrew Trick80ef1b22011-09-27 00:44:14 +000071#include "llvm/Support/CommandLine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000072#include "llvm/Support/Debug.h"
Dan Gohmanafc36a92009-05-02 18:29:22 +000073#include "llvm/Support/ValueHandle.h"
Daniel Dunbar460f6562009-07-26 09:48:23 +000074#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000075#include "llvm/Transforms/Utils/BasicBlockUtils.h"
76#include "llvm/Transforms/Utils/Local.h"
Jeff Cohencfb1d422005-07-30 18:22:27 +000077#include <algorithm>
Nate Begemaneaa13852004-10-18 21:08:22 +000078using namespace llvm;
79
Andrew Trickb5122632012-04-18 04:00:10 +000080/// MaxIVUsers is an arbitrary threshold that provides an early opportunitiy for
81/// bail out. This threshold is far beyond the number of users that LSR can
82/// conceivably solve, so it should not affect generated code, but catches the
83/// worst cases before LSR burns too much compile time and stack space.
84static const unsigned MaxIVUsers = 200;
85
Andrew Tricka02bfce2011-10-11 02:30:45 +000086// Temporary flag to cleanup congruent phis after LSR phi expansion.
87// It's currently disabled until we can determine whether it's truly useful or
88// not. The flag should be removed after the v3.0 release.
Andrew Trick24f670f2012-01-07 07:08:17 +000089// This is now needed for ivchains.
Benjamin Kramer0861f572011-11-26 23:01:57 +000090static cl::opt<bool> EnablePhiElim(
Andrew Trick24f670f2012-01-07 07:08:17 +000091 "enable-lsr-phielim", cl::Hidden, cl::init(true),
92 cl::desc("Enable LSR phi elimination"));
Andrew Trick80ef1b22011-09-27 00:44:14 +000093
Andrew Trick22d20c22012-01-09 21:18:52 +000094#ifndef NDEBUG
95// Stress test IV chain generation.
96static cl::opt<bool> StressIVChain(
97 "stress-ivchain", cl::Hidden, cl::init(false),
98 cl::desc("Stress test LSR IV chains"));
99#else
100static bool StressIVChain = false;
101#endif
102
Dan Gohman572645c2010-02-12 10:34:29 +0000103namespace {
Nate Begemaneaa13852004-10-18 21:08:22 +0000104
Dan Gohman572645c2010-02-12 10:34:29 +0000105/// RegSortData - This class holds data which is used to order reuse candidates.
106class RegSortData {
107public:
108 /// UsedByIndices - This represents the set of LSRUse indices which reference
109 /// a particular register.
110 SmallBitVector UsedByIndices;
111
112 RegSortData() {}
113
114 void print(raw_ostream &OS) const;
115 void dump() const;
116};
117
118}
119
120void RegSortData::print(raw_ostream &OS) const {
121 OS << "[NumUses=" << UsedByIndices.count() << ']';
122}
123
Manman Ren286c4dc2012-09-12 05:06:18 +0000124#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman572645c2010-02-12 10:34:29 +0000125void RegSortData::dump() const {
126 print(errs()); errs() << '\n';
127}
Manman Rencc77eec2012-09-06 19:55:56 +0000128#endif
Dan Gohmanc17e0cf2009-02-20 04:17:46 +0000129
Chris Lattner0e5f4992006-12-19 21:40:18 +0000130namespace {
Dale Johannesendc42f482007-03-20 00:47:50 +0000131
Dan Gohman572645c2010-02-12 10:34:29 +0000132/// RegUseTracker - Map register candidates to information about how they are
133/// used.
134class RegUseTracker {
135 typedef DenseMap<const SCEV *, RegSortData> RegUsesTy;
Dale Johannesendc42f482007-03-20 00:47:50 +0000136
Dan Gohman90bb3552010-05-18 22:33:00 +0000137 RegUsesTy RegUsesMap;
Dan Gohman572645c2010-02-12 10:34:29 +0000138 SmallVector<const SCEV *, 16> RegSequence;
Evan Chengd1d6b5c2006-03-16 21:53:05 +0000139
Dan Gohman572645c2010-02-12 10:34:29 +0000140public:
141 void CountRegister(const SCEV *Reg, size_t LUIdx);
Dan Gohmanb2df4332010-05-18 23:42:37 +0000142 void DropRegister(const SCEV *Reg, size_t LUIdx);
Dan Gohmanc6897702010-10-07 23:33:43 +0000143 void SwapAndDropUse(size_t LUIdx, size_t LastLUIdx);
Dan Gohmana10756e2010-01-21 02:09:26 +0000144
Dan Gohman572645c2010-02-12 10:34:29 +0000145 bool isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const;
Dan Gohmana10756e2010-01-21 02:09:26 +0000146
Dan Gohman572645c2010-02-12 10:34:29 +0000147 const SmallBitVector &getUsedByIndices(const SCEV *Reg) const;
Dan Gohmana10756e2010-01-21 02:09:26 +0000148
Dan Gohman572645c2010-02-12 10:34:29 +0000149 void clear();
Dan Gohmana10756e2010-01-21 02:09:26 +0000150
Dan Gohman572645c2010-02-12 10:34:29 +0000151 typedef SmallVectorImpl<const SCEV *>::iterator iterator;
152 typedef SmallVectorImpl<const SCEV *>::const_iterator const_iterator;
153 iterator begin() { return RegSequence.begin(); }
154 iterator end() { return RegSequence.end(); }
155 const_iterator begin() const { return RegSequence.begin(); }
156 const_iterator end() const { return RegSequence.end(); }
157};
Dan Gohmana10756e2010-01-21 02:09:26 +0000158
Dan Gohmana10756e2010-01-21 02:09:26 +0000159}
160
Dan Gohman572645c2010-02-12 10:34:29 +0000161void
162RegUseTracker::CountRegister(const SCEV *Reg, size_t LUIdx) {
163 std::pair<RegUsesTy::iterator, bool> Pair =
Dan Gohman90bb3552010-05-18 22:33:00 +0000164 RegUsesMap.insert(std::make_pair(Reg, RegSortData()));
Dan Gohman572645c2010-02-12 10:34:29 +0000165 RegSortData &RSD = Pair.first->second;
166 if (Pair.second)
167 RegSequence.push_back(Reg);
168 RSD.UsedByIndices.resize(std::max(RSD.UsedByIndices.size(), LUIdx + 1));
169 RSD.UsedByIndices.set(LUIdx);
Dan Gohmana10756e2010-01-21 02:09:26 +0000170}
171
Dan Gohmanb2df4332010-05-18 23:42:37 +0000172void
173RegUseTracker::DropRegister(const SCEV *Reg, size_t LUIdx) {
174 RegUsesTy::iterator It = RegUsesMap.find(Reg);
175 assert(It != RegUsesMap.end());
176 RegSortData &RSD = It->second;
177 assert(RSD.UsedByIndices.size() > LUIdx);
178 RSD.UsedByIndices.reset(LUIdx);
179}
180
Dan Gohmana2086b32010-05-19 23:43:12 +0000181void
Dan Gohmanc6897702010-10-07 23:33:43 +0000182RegUseTracker::SwapAndDropUse(size_t LUIdx, size_t LastLUIdx) {
183 assert(LUIdx <= LastLUIdx);
184
185 // Update RegUses. The data structure is not optimized for this purpose;
186 // we must iterate through it and update each of the bit vectors.
Dan Gohmana2086b32010-05-19 23:43:12 +0000187 for (RegUsesTy::iterator I = RegUsesMap.begin(), E = RegUsesMap.end();
Dan Gohmanc6897702010-10-07 23:33:43 +0000188 I != E; ++I) {
189 SmallBitVector &UsedByIndices = I->second.UsedByIndices;
190 if (LUIdx < UsedByIndices.size())
191 UsedByIndices[LUIdx] =
192 LastLUIdx < UsedByIndices.size() ? UsedByIndices[LastLUIdx] : 0;
193 UsedByIndices.resize(std::min(UsedByIndices.size(), LastLUIdx));
194 }
Dan Gohmana2086b32010-05-19 23:43:12 +0000195}
196
Dan Gohman572645c2010-02-12 10:34:29 +0000197bool
198RegUseTracker::isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const {
Dan Gohman46fd7a62010-08-29 15:18:49 +0000199 RegUsesTy::const_iterator I = RegUsesMap.find(Reg);
200 if (I == RegUsesMap.end())
201 return false;
202 const SmallBitVector &UsedByIndices = I->second.UsedByIndices;
Dan Gohman572645c2010-02-12 10:34:29 +0000203 int i = UsedByIndices.find_first();
204 if (i == -1) return false;
205 if ((size_t)i != LUIdx) return true;
206 return UsedByIndices.find_next(i) != -1;
207}
Dan Gohmana10756e2010-01-21 02:09:26 +0000208
Dan Gohman572645c2010-02-12 10:34:29 +0000209const SmallBitVector &RegUseTracker::getUsedByIndices(const SCEV *Reg) const {
Dan Gohman90bb3552010-05-18 22:33:00 +0000210 RegUsesTy::const_iterator I = RegUsesMap.find(Reg);
211 assert(I != RegUsesMap.end() && "Unknown register!");
Dan Gohman572645c2010-02-12 10:34:29 +0000212 return I->second.UsedByIndices;
213}
Dan Gohmana10756e2010-01-21 02:09:26 +0000214
Dan Gohman572645c2010-02-12 10:34:29 +0000215void RegUseTracker::clear() {
Dan Gohman90bb3552010-05-18 22:33:00 +0000216 RegUsesMap.clear();
Dan Gohman572645c2010-02-12 10:34:29 +0000217 RegSequence.clear();
218}
Dan Gohmana10756e2010-01-21 02:09:26 +0000219
Dan Gohman572645c2010-02-12 10:34:29 +0000220namespace {
221
222/// Formula - This class holds information that describes a formula for
223/// computing satisfying a use. It may include broken-out immediates and scaled
224/// registers.
225struct Formula {
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000226 /// Global base address used for complex addressing.
227 GlobalValue *BaseGV;
228
229 /// Base offset for complex addressing.
230 int64_t BaseOffset;
231
232 /// Whether any complex addressing has a base register.
233 bool HasBaseReg;
234
235 /// The scale of any complex addressing.
236 int64_t Scale;
Dan Gohman572645c2010-02-12 10:34:29 +0000237
238 /// BaseRegs - The list of "base" registers for this use. When this is
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000239 /// non-empty,
Preston Gurd83474ee2013-02-01 20:41:27 +0000240 SmallVector<const SCEV *, 4> BaseRegs;
Dan Gohman572645c2010-02-12 10:34:29 +0000241
242 /// ScaledReg - The 'scaled' register for this use. This should be non-null
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000243 /// when Scale is not zero.
Dan Gohman572645c2010-02-12 10:34:29 +0000244 const SCEV *ScaledReg;
245
Dan Gohmancca82142011-05-03 00:46:49 +0000246 /// UnfoldedOffset - An additional constant offset which added near the
247 /// use. This requires a temporary register, but the offset itself can
248 /// live in an add immediate field rather than a register.
249 int64_t UnfoldedOffset;
250
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000251 Formula()
252 : BaseGV(0), BaseOffset(0), HasBaseReg(false), Scale(0), ScaledReg(0),
253 UnfoldedOffset(0) {}
Dan Gohman572645c2010-02-12 10:34:29 +0000254
Dan Gohmandc0e8fb2010-11-17 21:41:58 +0000255 void InitialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE);
Dan Gohman572645c2010-02-12 10:34:29 +0000256
257 unsigned getNumRegs() const;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000258 Type *getType() const;
Dan Gohman572645c2010-02-12 10:34:29 +0000259
Dan Gohman5ce6d052010-05-20 15:17:54 +0000260 void DeleteBaseReg(const SCEV *&S);
261
Dan Gohman572645c2010-02-12 10:34:29 +0000262 bool referencesReg(const SCEV *S) const;
263 bool hasRegsUsedByUsesOtherThan(size_t LUIdx,
264 const RegUseTracker &RegUses) const;
265
266 void print(raw_ostream &OS) const;
267 void dump() const;
268};
269
270}
271
Dan Gohman3f46a3a2010-03-01 17:49:51 +0000272/// DoInitialMatch - Recursion helper for InitialMatch.
Dan Gohman572645c2010-02-12 10:34:29 +0000273static void DoInitialMatch(const SCEV *S, Loop *L,
274 SmallVectorImpl<const SCEV *> &Good,
275 SmallVectorImpl<const SCEV *> &Bad,
Dan Gohmandc0e8fb2010-11-17 21:41:58 +0000276 ScalarEvolution &SE) {
Dan Gohman572645c2010-02-12 10:34:29 +0000277 // Collect expressions which properly dominate the loop header.
Dan Gohmandc0e8fb2010-11-17 21:41:58 +0000278 if (SE.properlyDominates(S, L->getHeader())) {
Dan Gohman572645c2010-02-12 10:34:29 +0000279 Good.push_back(S);
280 return;
Dan Gohmana10756e2010-01-21 02:09:26 +0000281 }
Dan Gohman572645c2010-02-12 10:34:29 +0000282
283 // Look at add operands.
284 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
285 for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
286 I != E; ++I)
Dan Gohmandc0e8fb2010-11-17 21:41:58 +0000287 DoInitialMatch(*I, L, Good, Bad, SE);
Dan Gohman572645c2010-02-12 10:34:29 +0000288 return;
289 }
290
291 // Look at addrec operands.
292 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S))
293 if (!AR->getStart()->isZero()) {
Dan Gohmandc0e8fb2010-11-17 21:41:58 +0000294 DoInitialMatch(AR->getStart(), L, Good, Bad, SE);
Dan Gohmandeff6212010-05-03 22:09:21 +0000295 DoInitialMatch(SE.getAddRecExpr(SE.getConstant(AR->getType(), 0),
Dan Gohman572645c2010-02-12 10:34:29 +0000296 AR->getStepRecurrence(SE),
Andrew Trick3228cc22011-03-14 16:50:06 +0000297 // FIXME: AR->getNoWrapFlags()
298 AR->getLoop(), SCEV::FlagAnyWrap),
Dan Gohmandc0e8fb2010-11-17 21:41:58 +0000299 L, Good, Bad, SE);
Dan Gohman572645c2010-02-12 10:34:29 +0000300 return;
301 }
302
303 // Handle a multiplication by -1 (negation) if it didn't fold.
304 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S))
305 if (Mul->getOperand(0)->isAllOnesValue()) {
306 SmallVector<const SCEV *, 4> Ops(Mul->op_begin()+1, Mul->op_end());
307 const SCEV *NewMul = SE.getMulExpr(Ops);
308
309 SmallVector<const SCEV *, 4> MyGood;
310 SmallVector<const SCEV *, 4> MyBad;
Dan Gohmandc0e8fb2010-11-17 21:41:58 +0000311 DoInitialMatch(NewMul, L, MyGood, MyBad, SE);
Dan Gohman572645c2010-02-12 10:34:29 +0000312 const SCEV *NegOne = SE.getSCEV(ConstantInt::getAllOnesValue(
313 SE.getEffectiveSCEVType(NewMul->getType())));
314 for (SmallVectorImpl<const SCEV *>::const_iterator I = MyGood.begin(),
315 E = MyGood.end(); I != E; ++I)
316 Good.push_back(SE.getMulExpr(NegOne, *I));
317 for (SmallVectorImpl<const SCEV *>::const_iterator I = MyBad.begin(),
318 E = MyBad.end(); I != E; ++I)
319 Bad.push_back(SE.getMulExpr(NegOne, *I));
320 return;
321 }
322
323 // Ok, we can't do anything interesting. Just stuff the whole thing into a
324 // register and hope for the best.
325 Bad.push_back(S);
326}
327
328/// InitialMatch - Incorporate loop-variant parts of S into this Formula,
329/// attempting to keep all loop-invariant and loop-computable values in a
330/// single base register.
Dan Gohmandc0e8fb2010-11-17 21:41:58 +0000331void Formula::InitialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE) {
Dan Gohman572645c2010-02-12 10:34:29 +0000332 SmallVector<const SCEV *, 4> Good;
333 SmallVector<const SCEV *, 4> Bad;
Dan Gohmandc0e8fb2010-11-17 21:41:58 +0000334 DoInitialMatch(S, L, Good, Bad, SE);
Dan Gohman572645c2010-02-12 10:34:29 +0000335 if (!Good.empty()) {
Dan Gohmane60bb152010-04-08 23:36:27 +0000336 const SCEV *Sum = SE.getAddExpr(Good);
337 if (!Sum->isZero())
338 BaseRegs.push_back(Sum);
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000339 HasBaseReg = true;
Dan Gohman572645c2010-02-12 10:34:29 +0000340 }
341 if (!Bad.empty()) {
Dan Gohmane60bb152010-04-08 23:36:27 +0000342 const SCEV *Sum = SE.getAddExpr(Bad);
343 if (!Sum->isZero())
344 BaseRegs.push_back(Sum);
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000345 HasBaseReg = true;
Dan Gohman572645c2010-02-12 10:34:29 +0000346 }
347}
348
349/// getNumRegs - Return the total number of register operands used by this
350/// formula. This does not include register uses implied by non-constant
351/// addrec strides.
352unsigned Formula::getNumRegs() const {
353 return !!ScaledReg + BaseRegs.size();
354}
355
356/// getType - Return the type of this formula, if it has one, or null
357/// otherwise. This type is meaningless except for the bit size.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000358Type *Formula::getType() const {
Dan Gohman572645c2010-02-12 10:34:29 +0000359 return !BaseRegs.empty() ? BaseRegs.front()->getType() :
360 ScaledReg ? ScaledReg->getType() :
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000361 BaseGV ? BaseGV->getType() :
Dan Gohman572645c2010-02-12 10:34:29 +0000362 0;
363}
364
Dan Gohman5ce6d052010-05-20 15:17:54 +0000365/// DeleteBaseReg - Delete the given base reg from the BaseRegs list.
366void Formula::DeleteBaseReg(const SCEV *&S) {
367 if (&S != &BaseRegs.back())
368 std::swap(S, BaseRegs.back());
369 BaseRegs.pop_back();
370}
371
Dan Gohman572645c2010-02-12 10:34:29 +0000372/// referencesReg - Test if this formula references the given register.
373bool Formula::referencesReg(const SCEV *S) const {
374 return S == ScaledReg ||
375 std::find(BaseRegs.begin(), BaseRegs.end(), S) != BaseRegs.end();
376}
377
378/// hasRegsUsedByUsesOtherThan - Test whether this formula uses registers
379/// which are used by uses other than the use with the given index.
380bool Formula::hasRegsUsedByUsesOtherThan(size_t LUIdx,
381 const RegUseTracker &RegUses) const {
382 if (ScaledReg)
383 if (RegUses.isRegUsedByUsesOtherThan(ScaledReg, LUIdx))
384 return true;
385 for (SmallVectorImpl<const SCEV *>::const_iterator I = BaseRegs.begin(),
386 E = BaseRegs.end(); I != E; ++I)
387 if (RegUses.isRegUsedByUsesOtherThan(*I, LUIdx))
388 return true;
389 return false;
390}
391
392void Formula::print(raw_ostream &OS) const {
393 bool First = true;
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000394 if (BaseGV) {
Dan Gohman572645c2010-02-12 10:34:29 +0000395 if (!First) OS << " + "; else First = false;
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000396 WriteAsOperand(OS, BaseGV, /*PrintType=*/false);
Dan Gohman572645c2010-02-12 10:34:29 +0000397 }
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000398 if (BaseOffset != 0) {
Dan Gohman572645c2010-02-12 10:34:29 +0000399 if (!First) OS << " + "; else First = false;
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000400 OS << BaseOffset;
Dan Gohman572645c2010-02-12 10:34:29 +0000401 }
402 for (SmallVectorImpl<const SCEV *>::const_iterator I = BaseRegs.begin(),
403 E = BaseRegs.end(); I != E; ++I) {
404 if (!First) OS << " + "; else First = false;
405 OS << "reg(" << **I << ')';
406 }
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000407 if (HasBaseReg && BaseRegs.empty()) {
Dan Gohmanc4cfbaf2010-05-18 22:35:55 +0000408 if (!First) OS << " + "; else First = false;
409 OS << "**error: HasBaseReg**";
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000410 } else if (!HasBaseReg && !BaseRegs.empty()) {
Dan Gohmanc4cfbaf2010-05-18 22:35:55 +0000411 if (!First) OS << " + "; else First = false;
412 OS << "**error: !HasBaseReg**";
413 }
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000414 if (Scale != 0) {
Dan Gohman572645c2010-02-12 10:34:29 +0000415 if (!First) OS << " + "; else First = false;
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000416 OS << Scale << "*reg(";
Dan Gohman572645c2010-02-12 10:34:29 +0000417 if (ScaledReg)
418 OS << *ScaledReg;
419 else
420 OS << "<unknown>";
421 OS << ')';
422 }
Dan Gohmancca82142011-05-03 00:46:49 +0000423 if (UnfoldedOffset != 0) {
424 if (!First) OS << " + "; else First = false;
425 OS << "imm(" << UnfoldedOffset << ')';
426 }
Dan Gohman572645c2010-02-12 10:34:29 +0000427}
428
Manman Ren286c4dc2012-09-12 05:06:18 +0000429#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman572645c2010-02-12 10:34:29 +0000430void Formula::dump() const {
431 print(errs()); errs() << '\n';
432}
Manman Rencc77eec2012-09-06 19:55:56 +0000433#endif
Dan Gohman572645c2010-02-12 10:34:29 +0000434
Dan Gohmanaae01f12010-02-19 19:32:49 +0000435/// isAddRecSExtable - Return true if the given addrec can be sign-extended
436/// without changing its value.
437static bool isAddRecSExtable(const SCEVAddRecExpr *AR, ScalarEvolution &SE) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000438 Type *WideTy =
Dan Gohmanea507f52010-05-20 19:44:23 +0000439 IntegerType::get(SE.getContext(), SE.getTypeSizeInBits(AR->getType()) + 1);
Dan Gohmanaae01f12010-02-19 19:32:49 +0000440 return isa<SCEVAddRecExpr>(SE.getSignExtendExpr(AR, WideTy));
441}
442
443/// isAddSExtable - Return true if the given add can be sign-extended
444/// without changing its value.
445static bool isAddSExtable(const SCEVAddExpr *A, ScalarEvolution &SE) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000446 Type *WideTy =
Dan Gohmanea507f52010-05-20 19:44:23 +0000447 IntegerType::get(SE.getContext(), SE.getTypeSizeInBits(A->getType()) + 1);
Dan Gohmanaae01f12010-02-19 19:32:49 +0000448 return isa<SCEVAddExpr>(SE.getSignExtendExpr(A, WideTy));
449}
450
Dan Gohman473e6352010-06-24 16:45:11 +0000451/// isMulSExtable - Return true if the given mul can be sign-extended
Dan Gohmanaae01f12010-02-19 19:32:49 +0000452/// without changing its value.
Dan Gohman473e6352010-06-24 16:45:11 +0000453static bool isMulSExtable(const SCEVMulExpr *M, ScalarEvolution &SE) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000454 Type *WideTy =
Dan Gohman473e6352010-06-24 16:45:11 +0000455 IntegerType::get(SE.getContext(),
456 SE.getTypeSizeInBits(M->getType()) * M->getNumOperands());
457 return isa<SCEVMulExpr>(SE.getSignExtendExpr(M, WideTy));
Dan Gohmanaae01f12010-02-19 19:32:49 +0000458}
459
Dan Gohmanf09b7122010-02-19 19:35:48 +0000460/// getExactSDiv - Return an expression for LHS /s RHS, if it can be determined
461/// and if the remainder is known to be zero, or null otherwise. If
462/// IgnoreSignificantBits is true, expressions like (X * Y) /s Y are simplified
463/// to Y, ignoring that the multiplication may overflow, which is useful when
464/// the result will be used in a context where the most significant bits are
465/// ignored.
466static const SCEV *getExactSDiv(const SCEV *LHS, const SCEV *RHS,
467 ScalarEvolution &SE,
468 bool IgnoreSignificantBits = false) {
Dan Gohman572645c2010-02-12 10:34:29 +0000469 // Handle the trivial case, which works for any SCEV type.
470 if (LHS == RHS)
Dan Gohmandeff6212010-05-03 22:09:21 +0000471 return SE.getConstant(LHS->getType(), 1);
Dan Gohman572645c2010-02-12 10:34:29 +0000472
Dan Gohmand42819a2010-06-24 16:51:25 +0000473 // Handle a few RHS special cases.
474 const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS);
475 if (RC) {
476 const APInt &RA = RC->getValue()->getValue();
477 // Handle x /s -1 as x * -1, to give ScalarEvolution a chance to do
478 // some folding.
479 if (RA.isAllOnesValue())
480 return SE.getMulExpr(LHS, RC);
481 // Handle x /s 1 as x.
482 if (RA == 1)
483 return LHS;
484 }
Dan Gohman572645c2010-02-12 10:34:29 +0000485
486 // Check for a division of a constant by a constant.
487 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(LHS)) {
Dan Gohman572645c2010-02-12 10:34:29 +0000488 if (!RC)
489 return 0;
Dan Gohmand42819a2010-06-24 16:51:25 +0000490 const APInt &LA = C->getValue()->getValue();
491 const APInt &RA = RC->getValue()->getValue();
492 if (LA.srem(RA) != 0)
Dan Gohman572645c2010-02-12 10:34:29 +0000493 return 0;
Dan Gohmand42819a2010-06-24 16:51:25 +0000494 return SE.getConstant(LA.sdiv(RA));
Dan Gohman572645c2010-02-12 10:34:29 +0000495 }
496
Dan Gohmanaae01f12010-02-19 19:32:49 +0000497 // Distribute the sdiv over addrec operands, if the addrec doesn't overflow.
Dan Gohman572645c2010-02-12 10:34:29 +0000498 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) {
Dan Gohmanaae01f12010-02-19 19:32:49 +0000499 if (IgnoreSignificantBits || isAddRecSExtable(AR, SE)) {
Dan Gohmanf09b7122010-02-19 19:35:48 +0000500 const SCEV *Step = getExactSDiv(AR->getStepRecurrence(SE), RHS, SE,
501 IgnoreSignificantBits);
Dan Gohmanaae01f12010-02-19 19:32:49 +0000502 if (!Step) return 0;
Dan Gohman694a15e2010-08-19 01:02:31 +0000503 const SCEV *Start = getExactSDiv(AR->getStart(), RHS, SE,
504 IgnoreSignificantBits);
505 if (!Start) return 0;
Andrew Trick3228cc22011-03-14 16:50:06 +0000506 // FlagNW is independent of the start value, step direction, and is
507 // preserved with smaller magnitude steps.
508 // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
509 return SE.getAddRecExpr(Start, Step, AR->getLoop(), SCEV::FlagAnyWrap);
Dan Gohmanaae01f12010-02-19 19:32:49 +0000510 }
Dan Gohman2ea09e02010-06-24 16:57:52 +0000511 return 0;
Dan Gohman572645c2010-02-12 10:34:29 +0000512 }
513
Dan Gohmanaae01f12010-02-19 19:32:49 +0000514 // Distribute the sdiv over add operands, if the add doesn't overflow.
Dan Gohman572645c2010-02-12 10:34:29 +0000515 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(LHS)) {
Dan Gohmanaae01f12010-02-19 19:32:49 +0000516 if (IgnoreSignificantBits || isAddSExtable(Add, SE)) {
517 SmallVector<const SCEV *, 8> Ops;
518 for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
519 I != E; ++I) {
Dan Gohmanf09b7122010-02-19 19:35:48 +0000520 const SCEV *Op = getExactSDiv(*I, RHS, SE,
521 IgnoreSignificantBits);
Dan Gohmanaae01f12010-02-19 19:32:49 +0000522 if (!Op) return 0;
523 Ops.push_back(Op);
524 }
525 return SE.getAddExpr(Ops);
Dan Gohman572645c2010-02-12 10:34:29 +0000526 }
Dan Gohman2ea09e02010-06-24 16:57:52 +0000527 return 0;
Dan Gohman572645c2010-02-12 10:34:29 +0000528 }
529
530 // Check for a multiply operand that we can pull RHS out of.
Dan Gohman2ea09e02010-06-24 16:57:52 +0000531 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS)) {
Dan Gohmanaae01f12010-02-19 19:32:49 +0000532 if (IgnoreSignificantBits || isMulSExtable(Mul, SE)) {
Dan Gohman572645c2010-02-12 10:34:29 +0000533 SmallVector<const SCEV *, 4> Ops;
534 bool Found = false;
535 for (SCEVMulExpr::op_iterator I = Mul->op_begin(), E = Mul->op_end();
536 I != E; ++I) {
Dan Gohman47667442010-05-20 16:23:28 +0000537 const SCEV *S = *I;
Dan Gohman572645c2010-02-12 10:34:29 +0000538 if (!Found)
Dan Gohman47667442010-05-20 16:23:28 +0000539 if (const SCEV *Q = getExactSDiv(S, RHS, SE,
Dan Gohmanf09b7122010-02-19 19:35:48 +0000540 IgnoreSignificantBits)) {
Dan Gohman47667442010-05-20 16:23:28 +0000541 S = Q;
Dan Gohman572645c2010-02-12 10:34:29 +0000542 Found = true;
Dan Gohman572645c2010-02-12 10:34:29 +0000543 }
Dan Gohman47667442010-05-20 16:23:28 +0000544 Ops.push_back(S);
Dan Gohman572645c2010-02-12 10:34:29 +0000545 }
546 return Found ? SE.getMulExpr(Ops) : 0;
547 }
Dan Gohman2ea09e02010-06-24 16:57:52 +0000548 return 0;
549 }
Dan Gohman572645c2010-02-12 10:34:29 +0000550
551 // Otherwise we don't know.
552 return 0;
553}
554
555/// ExtractImmediate - If S involves the addition of a constant integer value,
556/// return that integer value, and mutate S to point to a new SCEV with that
557/// value excluded.
558static int64_t ExtractImmediate(const SCEV *&S, ScalarEvolution &SE) {
559 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
560 if (C->getValue()->getValue().getMinSignedBits() <= 64) {
Dan Gohmandeff6212010-05-03 22:09:21 +0000561 S = SE.getConstant(C->getType(), 0);
Dan Gohman572645c2010-02-12 10:34:29 +0000562 return C->getValue()->getSExtValue();
563 }
564 } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
565 SmallVector<const SCEV *, 8> NewOps(Add->op_begin(), Add->op_end());
566 int64_t Result = ExtractImmediate(NewOps.front(), SE);
Dan Gohmane62d5882010-08-13 21:17:19 +0000567 if (Result != 0)
568 S = SE.getAddExpr(NewOps);
Dan Gohman572645c2010-02-12 10:34:29 +0000569 return Result;
570 } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
571 SmallVector<const SCEV *, 8> NewOps(AR->op_begin(), AR->op_end());
572 int64_t Result = ExtractImmediate(NewOps.front(), SE);
Dan Gohmane62d5882010-08-13 21:17:19 +0000573 if (Result != 0)
Andrew Trick3228cc22011-03-14 16:50:06 +0000574 S = SE.getAddRecExpr(NewOps, AR->getLoop(),
575 // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
576 SCEV::FlagAnyWrap);
Dan Gohman572645c2010-02-12 10:34:29 +0000577 return Result;
578 }
579 return 0;
580}
581
582/// ExtractSymbol - If S involves the addition of a GlobalValue address,
583/// return that symbol, and mutate S to point to a new SCEV with that
584/// value excluded.
585static GlobalValue *ExtractSymbol(const SCEV *&S, ScalarEvolution &SE) {
586 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
587 if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue())) {
Dan Gohmandeff6212010-05-03 22:09:21 +0000588 S = SE.getConstant(GV->getType(), 0);
Dan Gohman572645c2010-02-12 10:34:29 +0000589 return GV;
590 }
591 } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
592 SmallVector<const SCEV *, 8> NewOps(Add->op_begin(), Add->op_end());
593 GlobalValue *Result = ExtractSymbol(NewOps.back(), SE);
Dan Gohmane62d5882010-08-13 21:17:19 +0000594 if (Result)
595 S = SE.getAddExpr(NewOps);
Dan Gohman572645c2010-02-12 10:34:29 +0000596 return Result;
597 } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
598 SmallVector<const SCEV *, 8> NewOps(AR->op_begin(), AR->op_end());
599 GlobalValue *Result = ExtractSymbol(NewOps.front(), SE);
Dan Gohmane62d5882010-08-13 21:17:19 +0000600 if (Result)
Andrew Trick3228cc22011-03-14 16:50:06 +0000601 S = SE.getAddRecExpr(NewOps, AR->getLoop(),
602 // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
603 SCEV::FlagAnyWrap);
Dan Gohman572645c2010-02-12 10:34:29 +0000604 return Result;
605 }
606 return 0;
Nate Begemaneaa13852004-10-18 21:08:22 +0000607}
608
Dan Gohmanf284ce22009-02-18 00:08:39 +0000609/// isAddressUse - Returns true if the specified instruction is using the
Dale Johannesen203af582008-12-05 21:47:27 +0000610/// specified value as an address.
611static bool isAddressUse(Instruction *Inst, Value *OperandVal) {
612 bool isAddress = isa<LoadInst>(Inst);
613 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
614 if (SI->getOperand(1) == OperandVal)
615 isAddress = true;
616 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
617 // Addressing modes can also be folded into prefetches and a variety
618 // of intrinsics.
619 switch (II->getIntrinsicID()) {
620 default: break;
621 case Intrinsic::prefetch:
Dale Johannesen203af582008-12-05 21:47:27 +0000622 case Intrinsic::x86_sse_storeu_ps:
623 case Intrinsic::x86_sse2_storeu_pd:
624 case Intrinsic::x86_sse2_storeu_dq:
625 case Intrinsic::x86_sse2_storel_dq:
Gabor Greifad72e732010-06-30 09:15:28 +0000626 if (II->getArgOperand(0) == OperandVal)
Dale Johannesen203af582008-12-05 21:47:27 +0000627 isAddress = true;
628 break;
629 }
630 }
631 return isAddress;
632}
Chris Lattner0ae33eb2005-10-03 01:04:44 +0000633
Dan Gohman21e77222009-03-09 21:01:17 +0000634/// getAccessType - Return the type of the memory being accessed.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000635static Type *getAccessType(const Instruction *Inst) {
636 Type *AccessTy = Inst->getType();
Dan Gohman21e77222009-03-09 21:01:17 +0000637 if (const StoreInst *SI = dyn_cast<StoreInst>(Inst))
Dan Gohmana537bf82009-05-18 16:45:28 +0000638 AccessTy = SI->getOperand(0)->getType();
Dan Gohman21e77222009-03-09 21:01:17 +0000639 else if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
640 // Addressing modes can also be folded into prefetches and a variety
641 // of intrinsics.
642 switch (II->getIntrinsicID()) {
643 default: break;
644 case Intrinsic::x86_sse_storeu_ps:
645 case Intrinsic::x86_sse2_storeu_pd:
646 case Intrinsic::x86_sse2_storeu_dq:
647 case Intrinsic::x86_sse2_storel_dq:
Gabor Greifad72e732010-06-30 09:15:28 +0000648 AccessTy = II->getArgOperand(0)->getType();
Dan Gohman21e77222009-03-09 21:01:17 +0000649 break;
650 }
651 }
Dan Gohman572645c2010-02-12 10:34:29 +0000652
653 // All pointers have the same requirements, so canonicalize them to an
654 // arbitrary pointer type to minimize variation.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000655 if (PointerType *PTy = dyn_cast<PointerType>(AccessTy))
Dan Gohman572645c2010-02-12 10:34:29 +0000656 AccessTy = PointerType::get(IntegerType::get(PTy->getContext(), 1),
657 PTy->getAddressSpace());
658
Dan Gohmana537bf82009-05-18 16:45:28 +0000659 return AccessTy;
Dan Gohman21e77222009-03-09 21:01:17 +0000660}
661
Andrew Trick8a5d7922011-12-06 03:13:31 +0000662/// isExistingPhi - Return true if this AddRec is already a phi in its loop.
663static bool isExistingPhi(const SCEVAddRecExpr *AR, ScalarEvolution &SE) {
664 for (BasicBlock::iterator I = AR->getLoop()->getHeader()->begin();
665 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
666 if (SE.isSCEVable(PN->getType()) &&
667 (SE.getEffectiveSCEVType(PN->getType()) ==
668 SE.getEffectiveSCEVType(AR->getType())) &&
669 SE.getSCEV(PN) == AR)
670 return true;
671 }
672 return false;
673}
674
Andrew Trick64925c52012-01-10 01:45:08 +0000675/// Check if expanding this expression is likely to incur significant cost. This
676/// is tricky because SCEV doesn't track which expressions are actually computed
677/// by the current IR.
678///
679/// We currently allow expansion of IV increments that involve adds,
680/// multiplication by constants, and AddRecs from existing phis.
681///
682/// TODO: Allow UDivExpr if we can find an existing IV increment that is an
683/// obvious multiple of the UDivExpr.
684static bool isHighCostExpansion(const SCEV *S,
685 SmallPtrSet<const SCEV*, 8> &Processed,
686 ScalarEvolution &SE) {
687 // Zero/One operand expressions
688 switch (S->getSCEVType()) {
689 case scUnknown:
690 case scConstant:
691 return false;
692 case scTruncate:
693 return isHighCostExpansion(cast<SCEVTruncateExpr>(S)->getOperand(),
694 Processed, SE);
695 case scZeroExtend:
696 return isHighCostExpansion(cast<SCEVZeroExtendExpr>(S)->getOperand(),
697 Processed, SE);
698 case scSignExtend:
699 return isHighCostExpansion(cast<SCEVSignExtendExpr>(S)->getOperand(),
700 Processed, SE);
701 }
702
703 if (!Processed.insert(S))
704 return false;
705
706 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
707 for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
708 I != E; ++I) {
709 if (isHighCostExpansion(*I, Processed, SE))
710 return true;
711 }
712 return false;
713 }
714
715 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
716 if (Mul->getNumOperands() == 2) {
717 // Multiplication by a constant is ok
718 if (isa<SCEVConstant>(Mul->getOperand(0)))
719 return isHighCostExpansion(Mul->getOperand(1), Processed, SE);
720
721 // If we have the value of one operand, check if an existing
722 // multiplication already generates this expression.
723 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Mul->getOperand(1))) {
724 Value *UVal = U->getValue();
725 for (Value::use_iterator UI = UVal->use_begin(), UE = UVal->use_end();
726 UI != UE; ++UI) {
Andrew Trick05fecbe2012-03-26 20:28:37 +0000727 // If U is a constant, it may be used by a ConstantExpr.
728 Instruction *User = dyn_cast<Instruction>(*UI);
729 if (User && User->getOpcode() == Instruction::Mul
Andrew Trick64925c52012-01-10 01:45:08 +0000730 && SE.isSCEVable(User->getType())) {
731 return SE.getSCEV(User) == Mul;
732 }
733 }
734 }
735 }
736 }
737
738 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
739 if (isExistingPhi(AR, SE))
740 return false;
741 }
742
743 // Fow now, consider any other type of expression (div/mul/min/max) high cost.
744 return true;
745}
746
Dan Gohman572645c2010-02-12 10:34:29 +0000747/// DeleteTriviallyDeadInstructions - If any of the instructions is the
748/// specified set are trivially dead, delete them and see if this makes any of
749/// their operands subsequently dead.
750static bool
751DeleteTriviallyDeadInstructions(SmallVectorImpl<WeakVH> &DeadInsts) {
752 bool Changed = false;
753
754 while (!DeadInsts.empty()) {
Richard Smith875cc5d2012-08-21 20:35:14 +0000755 Value *V = DeadInsts.pop_back_val();
756 Instruction *I = dyn_cast_or_null<Instruction>(V);
Dan Gohman572645c2010-02-12 10:34:29 +0000757
758 if (I == 0 || !isInstructionTriviallyDead(I))
759 continue;
760
761 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
762 if (Instruction *U = dyn_cast<Instruction>(*OI)) {
763 *OI = 0;
764 if (U->use_empty())
765 DeadInsts.push_back(U);
766 }
767
768 I->eraseFromParent();
769 Changed = true;
770 }
771
772 return Changed;
773}
774
Dan Gohman7979b722010-01-22 00:46:49 +0000775namespace {
Jim Grosbach56a1f802009-11-17 17:53:56 +0000776
Dan Gohman572645c2010-02-12 10:34:29 +0000777/// Cost - This class is used to measure and compare candidate formulae.
778class Cost {
779 /// TODO: Some of these could be merged. Also, a lexical ordering
780 /// isn't always optimal.
781 unsigned NumRegs;
782 unsigned AddRecCost;
783 unsigned NumIVMuls;
784 unsigned NumBaseAdds;
785 unsigned ImmCost;
786 unsigned SetupCost;
Nate Begeman16997482005-07-30 00:15:07 +0000787
Dan Gohman572645c2010-02-12 10:34:29 +0000788public:
789 Cost()
790 : NumRegs(0), AddRecCost(0), NumIVMuls(0), NumBaseAdds(0), ImmCost(0),
791 SetupCost(0) {}
Jim Grosbach56a1f802009-11-17 17:53:56 +0000792
Dan Gohman572645c2010-02-12 10:34:29 +0000793 bool operator<(const Cost &Other) const;
Dan Gohman7979b722010-01-22 00:46:49 +0000794
Dan Gohman572645c2010-02-12 10:34:29 +0000795 void Loose();
Dan Gohman7979b722010-01-22 00:46:49 +0000796
Andrew Trick7d11bd82011-09-26 23:11:04 +0000797#ifndef NDEBUG
798 // Once any of the metrics loses, they must all remain losers.
799 bool isValid() {
800 return ((NumRegs | AddRecCost | NumIVMuls | NumBaseAdds
801 | ImmCost | SetupCost) != ~0u)
802 || ((NumRegs & AddRecCost & NumIVMuls & NumBaseAdds
803 & ImmCost & SetupCost) == ~0u);
804 }
805#endif
806
807 bool isLoser() {
808 assert(isValid() && "invalid cost");
809 return NumRegs == ~0u;
810 }
811
Dan Gohman572645c2010-02-12 10:34:29 +0000812 void RateFormula(const Formula &F,
813 SmallPtrSet<const SCEV *, 16> &Regs,
814 const DenseSet<const SCEV *> &VisitedRegs,
815 const Loop *L,
816 const SmallVectorImpl<int64_t> &Offsets,
Andrew Trick8a5d7922011-12-06 03:13:31 +0000817 ScalarEvolution &SE, DominatorTree &DT,
818 SmallPtrSet<const SCEV *, 16> *LoserRegs = 0);
Dan Gohman7979b722010-01-22 00:46:49 +0000819
Dan Gohman572645c2010-02-12 10:34:29 +0000820 void print(raw_ostream &OS) const;
821 void dump() const;
Dan Gohman7979b722010-01-22 00:46:49 +0000822
Dan Gohman572645c2010-02-12 10:34:29 +0000823private:
824 void RateRegister(const SCEV *Reg,
825 SmallPtrSet<const SCEV *, 16> &Regs,
826 const Loop *L,
827 ScalarEvolution &SE, DominatorTree &DT);
Dan Gohman9214b822010-02-13 02:06:02 +0000828 void RatePrimaryRegister(const SCEV *Reg,
829 SmallPtrSet<const SCEV *, 16> &Regs,
830 const Loop *L,
Andrew Trick8a5d7922011-12-06 03:13:31 +0000831 ScalarEvolution &SE, DominatorTree &DT,
832 SmallPtrSet<const SCEV *, 16> *LoserRegs);
Dan Gohman572645c2010-02-12 10:34:29 +0000833};
834
835}
836
837/// RateRegister - Tally up interesting quantities from the given register.
838void Cost::RateRegister(const SCEV *Reg,
839 SmallPtrSet<const SCEV *, 16> &Regs,
840 const Loop *L,
841 ScalarEvolution &SE, DominatorTree &DT) {
Dan Gohman9214b822010-02-13 02:06:02 +0000842 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Reg)) {
Andrew Trick0c01bc32011-09-29 01:33:38 +0000843 // If this is an addrec for another loop, don't second-guess its addrec phi
844 // nodes. LSR isn't currently smart enough to reason about more than one
Andrew Trickbd618f12012-03-22 22:42:45 +0000845 // loop at a time. LSR has already run on inner loops, will not run on outer
846 // loops, and cannot be expected to change sibling loops.
847 if (AR->getLoop() != L) {
848 // If the AddRec exists, consider it's register free and leave it alone.
Andrew Trick8a5d7922011-12-06 03:13:31 +0000849 if (isExistingPhi(AR, SE))
850 return;
851
Andrew Trickbd618f12012-03-22 22:42:45 +0000852 // Otherwise, do not consider this formula at all.
853 Loose();
854 return;
Dan Gohman572645c2010-02-12 10:34:29 +0000855 }
Andrew Trickbd618f12012-03-22 22:42:45 +0000856 AddRecCost += 1; /// TODO: This should be a function of the stride.
Dan Gohman572645c2010-02-12 10:34:29 +0000857
Dan Gohman9214b822010-02-13 02:06:02 +0000858 // Add the step value register, if it needs one.
859 // TODO: The non-affine case isn't precisely modeled here.
Andrew Trick25b689e2011-09-26 23:35:25 +0000860 if (!AR->isAffine() || !isa<SCEVConstant>(AR->getOperand(1))) {
861 if (!Regs.count(AR->getOperand(1))) {
Dan Gohman9214b822010-02-13 02:06:02 +0000862 RateRegister(AR->getOperand(1), Regs, L, SE, DT);
Andrew Trick25b689e2011-09-26 23:35:25 +0000863 if (isLoser())
864 return;
865 }
866 }
Dan Gohman572645c2010-02-12 10:34:29 +0000867 }
Dan Gohman9214b822010-02-13 02:06:02 +0000868 ++NumRegs;
869
870 // Rough heuristic; favor registers which don't require extra setup
871 // instructions in the preheader.
872 if (!isa<SCEVUnknown>(Reg) &&
873 !isa<SCEVConstant>(Reg) &&
874 !(isa<SCEVAddRecExpr>(Reg) &&
875 (isa<SCEVUnknown>(cast<SCEVAddRecExpr>(Reg)->getStart()) ||
876 isa<SCEVConstant>(cast<SCEVAddRecExpr>(Reg)->getStart()))))
877 ++SetupCost;
Dan Gohman23c3fde2010-10-07 23:41:58 +0000878
879 NumIVMuls += isa<SCEVMulExpr>(Reg) &&
Dan Gohman17ead4f2010-11-17 21:23:15 +0000880 SE.hasComputableLoopEvolution(Reg, L);
Dan Gohman9214b822010-02-13 02:06:02 +0000881}
882
883/// RatePrimaryRegister - Record this register in the set. If we haven't seen it
Andrew Trick8a5d7922011-12-06 03:13:31 +0000884/// before, rate it. Optional LoserRegs provides a way to declare any formula
885/// that refers to one of those regs an instant loser.
Dan Gohman9214b822010-02-13 02:06:02 +0000886void Cost::RatePrimaryRegister(const SCEV *Reg,
Dan Gohman7fca2292010-02-16 19:42:34 +0000887 SmallPtrSet<const SCEV *, 16> &Regs,
888 const Loop *L,
Andrew Trick8a5d7922011-12-06 03:13:31 +0000889 ScalarEvolution &SE, DominatorTree &DT,
890 SmallPtrSet<const SCEV *, 16> *LoserRegs) {
891 if (LoserRegs && LoserRegs->count(Reg)) {
892 Loose();
893 return;
894 }
895 if (Regs.insert(Reg)) {
Dan Gohman9214b822010-02-13 02:06:02 +0000896 RateRegister(Reg, Regs, L, SE, DT);
Andrew Trick8a5d7922011-12-06 03:13:31 +0000897 if (isLoser())
898 LoserRegs->insert(Reg);
899 }
Dan Gohman572645c2010-02-12 10:34:29 +0000900}
901
902void Cost::RateFormula(const Formula &F,
903 SmallPtrSet<const SCEV *, 16> &Regs,
904 const DenseSet<const SCEV *> &VisitedRegs,
905 const Loop *L,
906 const SmallVectorImpl<int64_t> &Offsets,
Andrew Trick8a5d7922011-12-06 03:13:31 +0000907 ScalarEvolution &SE, DominatorTree &DT,
908 SmallPtrSet<const SCEV *, 16> *LoserRegs) {
Dan Gohman572645c2010-02-12 10:34:29 +0000909 // Tally up the registers.
910 if (const SCEV *ScaledReg = F.ScaledReg) {
911 if (VisitedRegs.count(ScaledReg)) {
912 Loose();
913 return;
914 }
Andrew Trick8a5d7922011-12-06 03:13:31 +0000915 RatePrimaryRegister(ScaledReg, Regs, L, SE, DT, LoserRegs);
Andrew Trick7d11bd82011-09-26 23:11:04 +0000916 if (isLoser())
917 return;
Dan Gohman572645c2010-02-12 10:34:29 +0000918 }
919 for (SmallVectorImpl<const SCEV *>::const_iterator I = F.BaseRegs.begin(),
920 E = F.BaseRegs.end(); I != E; ++I) {
921 const SCEV *BaseReg = *I;
922 if (VisitedRegs.count(BaseReg)) {
923 Loose();
924 return;
925 }
Andrew Trick8a5d7922011-12-06 03:13:31 +0000926 RatePrimaryRegister(BaseReg, Regs, L, SE, DT, LoserRegs);
Andrew Trick7d11bd82011-09-26 23:11:04 +0000927 if (isLoser())
928 return;
Dan Gohman572645c2010-02-12 10:34:29 +0000929 }
930
Dan Gohmancca82142011-05-03 00:46:49 +0000931 // Determine how many (unfolded) adds we'll need inside the loop.
932 size_t NumBaseParts = F.BaseRegs.size() + (F.UnfoldedOffset != 0);
933 if (NumBaseParts > 1)
934 NumBaseAdds += NumBaseParts - 1;
Dan Gohman572645c2010-02-12 10:34:29 +0000935
936 // Tally up the non-zero immediates.
937 for (SmallVectorImpl<int64_t>::const_iterator I = Offsets.begin(),
938 E = Offsets.end(); I != E; ++I) {
Chandler Carrutha07dcb12013-01-07 15:04:40 +0000939 int64_t Offset = (uint64_t)*I + F.BaseOffset;
940 if (F.BaseGV)
Dan Gohman572645c2010-02-12 10:34:29 +0000941 ImmCost += 64; // Handle symbolic values conservatively.
942 // TODO: This should probably be the pointer size.
943 else if (Offset != 0)
944 ImmCost += APInt(64, Offset, true).getMinSignedBits();
945 }
Andrew Trick7d11bd82011-09-26 23:11:04 +0000946 assert(isValid() && "invalid cost");
Dan Gohman572645c2010-02-12 10:34:29 +0000947}
948
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000949/// Loose - Set this cost to a losing value.
Dan Gohman572645c2010-02-12 10:34:29 +0000950void Cost::Loose() {
951 NumRegs = ~0u;
952 AddRecCost = ~0u;
953 NumIVMuls = ~0u;
954 NumBaseAdds = ~0u;
955 ImmCost = ~0u;
956 SetupCost = ~0u;
957}
958
959/// operator< - Choose the lower cost.
960bool Cost::operator<(const Cost &Other) const {
961 if (NumRegs != Other.NumRegs)
962 return NumRegs < Other.NumRegs;
963 if (AddRecCost != Other.AddRecCost)
964 return AddRecCost < Other.AddRecCost;
965 if (NumIVMuls != Other.NumIVMuls)
966 return NumIVMuls < Other.NumIVMuls;
967 if (NumBaseAdds != Other.NumBaseAdds)
968 return NumBaseAdds < Other.NumBaseAdds;
969 if (ImmCost != Other.ImmCost)
970 return ImmCost < Other.ImmCost;
971 if (SetupCost != Other.SetupCost)
972 return SetupCost < Other.SetupCost;
973 return false;
974}
975
976void Cost::print(raw_ostream &OS) const {
977 OS << NumRegs << " reg" << (NumRegs == 1 ? "" : "s");
978 if (AddRecCost != 0)
979 OS << ", with addrec cost " << AddRecCost;
980 if (NumIVMuls != 0)
981 OS << ", plus " << NumIVMuls << " IV mul" << (NumIVMuls == 1 ? "" : "s");
982 if (NumBaseAdds != 0)
983 OS << ", plus " << NumBaseAdds << " base add"
984 << (NumBaseAdds == 1 ? "" : "s");
985 if (ImmCost != 0)
986 OS << ", plus " << ImmCost << " imm cost";
987 if (SetupCost != 0)
988 OS << ", plus " << SetupCost << " setup cost";
989}
990
Manman Ren286c4dc2012-09-12 05:06:18 +0000991#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman572645c2010-02-12 10:34:29 +0000992void Cost::dump() const {
993 print(errs()); errs() << '\n';
994}
Manman Rencc77eec2012-09-06 19:55:56 +0000995#endif
Dan Gohman572645c2010-02-12 10:34:29 +0000996
997namespace {
998
999/// LSRFixup - An operand value in an instruction which is to be replaced
1000/// with some equivalent, possibly strength-reduced, replacement.
1001struct LSRFixup {
1002 /// UserInst - The instruction which will be updated.
1003 Instruction *UserInst;
1004
1005 /// OperandValToReplace - The operand of the instruction which will
1006 /// be replaced. The operand may be used more than once; every instance
1007 /// will be replaced.
1008 Value *OperandValToReplace;
1009
Dan Gohman448db1c2010-04-07 22:27:08 +00001010 /// PostIncLoops - If this user is to use the post-incremented value of an
Dan Gohman572645c2010-02-12 10:34:29 +00001011 /// induction variable, this variable is non-null and holds the loop
1012 /// associated with the induction variable.
Dan Gohman448db1c2010-04-07 22:27:08 +00001013 PostIncLoopSet PostIncLoops;
Dan Gohman572645c2010-02-12 10:34:29 +00001014
1015 /// LUIdx - The index of the LSRUse describing the expression which
1016 /// this fixup needs, minus an offset (below).
1017 size_t LUIdx;
1018
1019 /// Offset - A constant offset to be added to the LSRUse expression.
1020 /// This allows multiple fixups to share the same LSRUse with different
1021 /// offsets, for example in an unrolled loop.
1022 int64_t Offset;
1023
Dan Gohman448db1c2010-04-07 22:27:08 +00001024 bool isUseFullyOutsideLoop(const Loop *L) const;
1025
Dan Gohman572645c2010-02-12 10:34:29 +00001026 LSRFixup();
1027
1028 void print(raw_ostream &OS) const;
1029 void dump() const;
1030};
1031
1032}
1033
1034LSRFixup::LSRFixup()
Dan Gohmanea507f52010-05-20 19:44:23 +00001035 : UserInst(0), OperandValToReplace(0), LUIdx(~size_t(0)), Offset(0) {}
Dan Gohman572645c2010-02-12 10:34:29 +00001036
Dan Gohman448db1c2010-04-07 22:27:08 +00001037/// isUseFullyOutsideLoop - Test whether this fixup always uses its
1038/// value outside of the given loop.
1039bool LSRFixup::isUseFullyOutsideLoop(const Loop *L) const {
1040 // PHI nodes use their value in their incoming blocks.
1041 if (const PHINode *PN = dyn_cast<PHINode>(UserInst)) {
1042 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1043 if (PN->getIncomingValue(i) == OperandValToReplace &&
1044 L->contains(PN->getIncomingBlock(i)))
1045 return false;
1046 return true;
1047 }
1048
1049 return !L->contains(UserInst);
1050}
1051
Dan Gohman572645c2010-02-12 10:34:29 +00001052void LSRFixup::print(raw_ostream &OS) const {
1053 OS << "UserInst=";
1054 // Store is common and interesting enough to be worth special-casing.
1055 if (StoreInst *Store = dyn_cast<StoreInst>(UserInst)) {
1056 OS << "store ";
1057 WriteAsOperand(OS, Store->getOperand(0), /*PrintType=*/false);
1058 } else if (UserInst->getType()->isVoidTy())
1059 OS << UserInst->getOpcodeName();
1060 else
1061 WriteAsOperand(OS, UserInst, /*PrintType=*/false);
1062
1063 OS << ", OperandValToReplace=";
1064 WriteAsOperand(OS, OperandValToReplace, /*PrintType=*/false);
1065
Dan Gohman448db1c2010-04-07 22:27:08 +00001066 for (PostIncLoopSet::const_iterator I = PostIncLoops.begin(),
1067 E = PostIncLoops.end(); I != E; ++I) {
Dan Gohman572645c2010-02-12 10:34:29 +00001068 OS << ", PostIncLoop=";
Dan Gohman448db1c2010-04-07 22:27:08 +00001069 WriteAsOperand(OS, (*I)->getHeader(), /*PrintType=*/false);
Dan Gohman572645c2010-02-12 10:34:29 +00001070 }
1071
1072 if (LUIdx != ~size_t(0))
1073 OS << ", LUIdx=" << LUIdx;
1074
1075 if (Offset != 0)
1076 OS << ", Offset=" << Offset;
1077}
1078
Manman Ren286c4dc2012-09-12 05:06:18 +00001079#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman572645c2010-02-12 10:34:29 +00001080void LSRFixup::dump() const {
1081 print(errs()); errs() << '\n';
1082}
Manman Rencc77eec2012-09-06 19:55:56 +00001083#endif
Dan Gohman572645c2010-02-12 10:34:29 +00001084
1085namespace {
1086
1087/// UniquifierDenseMapInfo - A DenseMapInfo implementation for holding
1088/// DenseMaps and DenseSets of sorted SmallVectors of const SCEV*.
1089struct UniquifierDenseMapInfo {
Preston Gurd83474ee2013-02-01 20:41:27 +00001090 static SmallVector<const SCEV *, 4> getEmptyKey() {
1091 SmallVector<const SCEV *, 4> V;
Dan Gohman572645c2010-02-12 10:34:29 +00001092 V.push_back(reinterpret_cast<const SCEV *>(-1));
1093 return V;
1094 }
1095
Preston Gurd83474ee2013-02-01 20:41:27 +00001096 static SmallVector<const SCEV *, 4> getTombstoneKey() {
1097 SmallVector<const SCEV *, 4> V;
Dan Gohman572645c2010-02-12 10:34:29 +00001098 V.push_back(reinterpret_cast<const SCEV *>(-2));
1099 return V;
1100 }
1101
Preston Gurd83474ee2013-02-01 20:41:27 +00001102 static unsigned getHashValue(const SmallVector<const SCEV *, 4> &V) {
Dan Gohman572645c2010-02-12 10:34:29 +00001103 unsigned Result = 0;
1104 for (SmallVectorImpl<const SCEV *>::const_iterator I = V.begin(),
1105 E = V.end(); I != E; ++I)
1106 Result ^= DenseMapInfo<const SCEV *>::getHashValue(*I);
1107 return Result;
1108 }
1109
Preston Gurd83474ee2013-02-01 20:41:27 +00001110 static bool isEqual(const SmallVector<const SCEV *, 4> &LHS,
1111 const SmallVector<const SCEV *, 4> &RHS) {
Dan Gohman572645c2010-02-12 10:34:29 +00001112 return LHS == RHS;
1113 }
1114};
1115
1116/// LSRUse - This class holds the state that LSR keeps for each use in
1117/// IVUsers, as well as uses invented by LSR itself. It includes information
1118/// about what kinds of things can be folded into the user, information about
1119/// the user itself, and information about how the use may be satisfied.
1120/// TODO: Represent multiple users of the same expression in common?
1121class LSRUse {
Preston Gurd83474ee2013-02-01 20:41:27 +00001122 DenseSet<SmallVector<const SCEV *, 4>, UniquifierDenseMapInfo> Uniquifier;
Dan Gohman572645c2010-02-12 10:34:29 +00001123
1124public:
1125 /// KindType - An enum for a kind of use, indicating what types of
1126 /// scaled and immediate operands it might support.
1127 enum KindType {
1128 Basic, ///< A normal use, with no folding.
1129 Special, ///< A special case of basic, allowing -1 scales.
Nadav Rotema04a4a72012-10-19 21:28:43 +00001130 Address, ///< An address use; folding according to TargetLowering
Dan Gohman572645c2010-02-12 10:34:29 +00001131 ICmpZero ///< An equality icmp with both operands folded into one.
1132 // TODO: Add a generic icmp too?
Dan Gohman7979b722010-01-22 00:46:49 +00001133 };
Dan Gohman572645c2010-02-12 10:34:29 +00001134
1135 KindType Kind;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001136 Type *AccessTy;
Dan Gohman572645c2010-02-12 10:34:29 +00001137
1138 SmallVector<int64_t, 8> Offsets;
1139 int64_t MinOffset;
1140 int64_t MaxOffset;
1141
1142 /// AllFixupsOutsideLoop - This records whether all of the fixups using this
1143 /// LSRUse are outside of the loop, in which case some special-case heuristics
1144 /// may be used.
1145 bool AllFixupsOutsideLoop;
1146
Dan Gohmana9db1292010-07-15 20:24:58 +00001147 /// WidestFixupType - This records the widest use type for any fixup using
1148 /// this LSRUse. FindUseWithSimilarFormula can't consider uses with different
1149 /// max fixup widths to be equivalent, because the narrower one may be relying
1150 /// on the implicit truncation to truncate away bogus bits.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001151 Type *WidestFixupType;
Dan Gohmana9db1292010-07-15 20:24:58 +00001152
Dan Gohman572645c2010-02-12 10:34:29 +00001153 /// Formulae - A list of ways to build a value that can satisfy this user.
1154 /// After the list is populated, one of these is selected heuristically and
1155 /// used to formulate a replacement for OperandValToReplace in UserInst.
1156 SmallVector<Formula, 12> Formulae;
1157
1158 /// Regs - The set of register candidates used by all formulae in this LSRUse.
1159 SmallPtrSet<const SCEV *, 4> Regs;
1160
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001161 LSRUse(KindType K, Type *T) : Kind(K), AccessTy(T),
Dan Gohman572645c2010-02-12 10:34:29 +00001162 MinOffset(INT64_MAX),
1163 MaxOffset(INT64_MIN),
Dan Gohmana9db1292010-07-15 20:24:58 +00001164 AllFixupsOutsideLoop(true),
1165 WidestFixupType(0) {}
Dan Gohman572645c2010-02-12 10:34:29 +00001166
Dan Gohmana2086b32010-05-19 23:43:12 +00001167 bool HasFormulaWithSameRegs(const Formula &F) const;
Dan Gohman454d26d2010-02-22 04:11:59 +00001168 bool InsertFormula(const Formula &F);
Dan Gohmand69d6282010-05-18 22:39:15 +00001169 void DeleteFormula(Formula &F);
Dan Gohmanb2df4332010-05-18 23:42:37 +00001170 void RecomputeRegs(size_t LUIdx, RegUseTracker &Reguses);
Dan Gohman572645c2010-02-12 10:34:29 +00001171
Dan Gohman572645c2010-02-12 10:34:29 +00001172 void print(raw_ostream &OS) const;
1173 void dump() const;
1174};
1175
Dan Gohmanb6211712010-06-19 21:21:39 +00001176}
1177
Dan Gohmana2086b32010-05-19 23:43:12 +00001178/// HasFormula - Test whether this use as a formula which has the same
1179/// registers as the given formula.
1180bool LSRUse::HasFormulaWithSameRegs(const Formula &F) const {
Preston Gurd83474ee2013-02-01 20:41:27 +00001181 SmallVector<const SCEV *, 4> Key = F.BaseRegs;
Dan Gohmana2086b32010-05-19 23:43:12 +00001182 if (F.ScaledReg) Key.push_back(F.ScaledReg);
1183 // Unstable sort by host order ok, because this is only used for uniquifying.
1184 std::sort(Key.begin(), Key.end());
1185 return Uniquifier.count(Key);
1186}
1187
Dan Gohman572645c2010-02-12 10:34:29 +00001188/// InsertFormula - If the given formula has not yet been inserted, add it to
1189/// the list, and return true. Return false otherwise.
Dan Gohman454d26d2010-02-22 04:11:59 +00001190bool LSRUse::InsertFormula(const Formula &F) {
Preston Gurd83474ee2013-02-01 20:41:27 +00001191 SmallVector<const SCEV *, 4> Key = F.BaseRegs;
Dan Gohman572645c2010-02-12 10:34:29 +00001192 if (F.ScaledReg) Key.push_back(F.ScaledReg);
1193 // Unstable sort by host order ok, because this is only used for uniquifying.
1194 std::sort(Key.begin(), Key.end());
1195
1196 if (!Uniquifier.insert(Key).second)
1197 return false;
1198
1199 // Using a register to hold the value of 0 is not profitable.
1200 assert((!F.ScaledReg || !F.ScaledReg->isZero()) &&
1201 "Zero allocated in a scaled register!");
1202#ifndef NDEBUG
1203 for (SmallVectorImpl<const SCEV *>::const_iterator I =
1204 F.BaseRegs.begin(), E = F.BaseRegs.end(); I != E; ++I)
1205 assert(!(*I)->isZero() && "Zero allocated in a base register!");
1206#endif
1207
1208 // Add the formula to the list.
1209 Formulae.push_back(F);
1210
1211 // Record registers now being used by this use.
Dan Gohman572645c2010-02-12 10:34:29 +00001212 Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end());
1213
1214 return true;
Dan Gohman7979b722010-01-22 00:46:49 +00001215}
1216
Dan Gohmand69d6282010-05-18 22:39:15 +00001217/// DeleteFormula - Remove the given formula from this use's list.
1218void LSRUse::DeleteFormula(Formula &F) {
Dan Gohman5ce6d052010-05-20 15:17:54 +00001219 if (&F != &Formulae.back())
1220 std::swap(F, Formulae.back());
Dan Gohmand69d6282010-05-18 22:39:15 +00001221 Formulae.pop_back();
1222}
1223
Dan Gohmanb2df4332010-05-18 23:42:37 +00001224/// RecomputeRegs - Recompute the Regs field, and update RegUses.
1225void LSRUse::RecomputeRegs(size_t LUIdx, RegUseTracker &RegUses) {
1226 // Now that we've filtered out some formulae, recompute the Regs set.
1227 SmallPtrSet<const SCEV *, 4> OldRegs = Regs;
1228 Regs.clear();
Dan Gohman402d4352010-05-20 20:33:18 +00001229 for (SmallVectorImpl<Formula>::const_iterator I = Formulae.begin(),
1230 E = Formulae.end(); I != E; ++I) {
1231 const Formula &F = *I;
Dan Gohmanb2df4332010-05-18 23:42:37 +00001232 if (F.ScaledReg) Regs.insert(F.ScaledReg);
1233 Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end());
1234 }
1235
1236 // Update the RegTracker.
1237 for (SmallPtrSet<const SCEV *, 4>::iterator I = OldRegs.begin(),
1238 E = OldRegs.end(); I != E; ++I)
1239 if (!Regs.count(*I))
1240 RegUses.DropRegister(*I, LUIdx);
1241}
1242
Dan Gohman572645c2010-02-12 10:34:29 +00001243void LSRUse::print(raw_ostream &OS) const {
1244 OS << "LSR Use: Kind=";
1245 switch (Kind) {
1246 case Basic: OS << "Basic"; break;
1247 case Special: OS << "Special"; break;
1248 case ICmpZero: OS << "ICmpZero"; break;
1249 case Address:
1250 OS << "Address of ";
Duncan Sands1df98592010-02-16 11:11:14 +00001251 if (AccessTy->isPointerTy())
Dan Gohman572645c2010-02-12 10:34:29 +00001252 OS << "pointer"; // the full pointer type could be really verbose
1253 else
1254 OS << *AccessTy;
Evan Chengcdf43b12007-10-25 09:11:16 +00001255 }
1256
Dan Gohman572645c2010-02-12 10:34:29 +00001257 OS << ", Offsets={";
1258 for (SmallVectorImpl<int64_t>::const_iterator I = Offsets.begin(),
1259 E = Offsets.end(); I != E; ++I) {
1260 OS << *I;
Oscar Fuentesee56c422010-08-02 06:00:15 +00001261 if (llvm::next(I) != E)
Dan Gohman572645c2010-02-12 10:34:29 +00001262 OS << ',';
Dan Gohman7979b722010-01-22 00:46:49 +00001263 }
Dan Gohman572645c2010-02-12 10:34:29 +00001264 OS << '}';
Dan Gohman7979b722010-01-22 00:46:49 +00001265
Dan Gohman572645c2010-02-12 10:34:29 +00001266 if (AllFixupsOutsideLoop)
1267 OS << ", all-fixups-outside-loop";
Dan Gohmana9db1292010-07-15 20:24:58 +00001268
1269 if (WidestFixupType)
1270 OS << ", widest fixup type: " << *WidestFixupType;
Dan Gohman7979b722010-01-22 00:46:49 +00001271}
1272
Manman Ren286c4dc2012-09-12 05:06:18 +00001273#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman572645c2010-02-12 10:34:29 +00001274void LSRUse::dump() const {
1275 print(errs()); errs() << '\n';
1276}
Manman Rencc77eec2012-09-06 19:55:56 +00001277#endif
Dan Gohman7979b722010-01-22 00:46:49 +00001278
Dan Gohman572645c2010-02-12 10:34:29 +00001279/// isLegalUse - Test whether the use described by AM is "legal", meaning it can
1280/// be completely folded into the user instruction at isel time. This includes
1281/// address-mode folding and special icmp tricks.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001282static bool isLegalUse(const TargetTransformInfo &TTI, LSRUse::KindType Kind,
1283 Type *AccessTy, GlobalValue *BaseGV, int64_t BaseOffset,
1284 bool HasBaseReg, int64_t Scale) {
Dan Gohman572645c2010-02-12 10:34:29 +00001285 switch (Kind) {
1286 case LSRUse::Address:
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001287 return TTI.isLegalAddressingMode(AccessTy, BaseGV, BaseOffset, HasBaseReg, Scale);
Dan Gohman572645c2010-02-12 10:34:29 +00001288
1289 // Otherwise, just guess that reg+reg addressing is legal.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001290 //return ;
Dan Gohman572645c2010-02-12 10:34:29 +00001291
1292 case LSRUse::ICmpZero:
1293 // There's not even a target hook for querying whether it would be legal to
1294 // fold a GV into an ICmp.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001295 if (BaseGV)
Dan Gohman572645c2010-02-12 10:34:29 +00001296 return false;
1297
1298 // ICmp only has two operands; don't allow more than two non-trivial parts.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001299 if (Scale != 0 && HasBaseReg && BaseOffset != 0)
Dan Gohman572645c2010-02-12 10:34:29 +00001300 return false;
1301
1302 // ICmp only supports no scale or a -1 scale, as we can "fold" a -1 scale by
1303 // putting the scaled register in the other operand of the icmp.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001304 if (Scale != 0 && Scale != -1)
Dan Gohman572645c2010-02-12 10:34:29 +00001305 return false;
1306
1307 // If we have low-level target information, ask the target if it can fold an
1308 // integer immediate on an icmp.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001309 if (BaseOffset != 0) {
Jakob Stoklund Olesen9243c4f2012-04-05 03:10:56 +00001310 // We have one of:
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001311 // ICmpZero BaseReg + BaseOffset => ICmp BaseReg, -BaseOffset
1312 // ICmpZero -1*ScaleReg + BaseOffset => ICmp ScaleReg, BaseOffset
Jakob Stoklund Olesen9243c4f2012-04-05 03:10:56 +00001313 // Offs is the ICmp immediate.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001314 if (Scale == 0)
1315 // The cast does the right thing with INT64_MIN.
1316 BaseOffset = -(uint64_t)BaseOffset;
1317 return TTI.isLegalICmpImmediate(BaseOffset);
Dan Gohman7979b722010-01-22 00:46:49 +00001318 }
Dan Gohman572645c2010-02-12 10:34:29 +00001319
Jakob Stoklund Olesen9243c4f2012-04-05 03:10:56 +00001320 // ICmpZero BaseReg + -1*ScaleReg => ICmp BaseReg, ScaleReg
Dan Gohman572645c2010-02-12 10:34:29 +00001321 return true;
1322
1323 case LSRUse::Basic:
1324 // Only handle single-register values.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001325 return !BaseGV && Scale == 0 && BaseOffset == 0;
Dan Gohman572645c2010-02-12 10:34:29 +00001326
1327 case LSRUse::Special:
Andrew Trick546f2102012-06-15 20:07:26 +00001328 // Special case Basic to handle -1 scales.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001329 return !BaseGV && (Scale == 0 || Scale == -1) && BaseOffset == 0;
Dan Gohman7979b722010-01-22 00:46:49 +00001330 }
1331
David Blaikie4d6ccb52012-01-20 21:51:11 +00001332 llvm_unreachable("Invalid LSRUse Kind!");
Dan Gohman7979b722010-01-22 00:46:49 +00001333}
1334
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001335static bool isLegalUse(const TargetTransformInfo &TTI, int64_t MinOffset,
1336 int64_t MaxOffset, LSRUse::KindType Kind, Type *AccessTy,
1337 GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg,
1338 int64_t Scale) {
Dan Gohman572645c2010-02-12 10:34:29 +00001339 // Check for overflow.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001340 if (((int64_t)((uint64_t)BaseOffset + MinOffset) > BaseOffset) !=
Dan Gohman572645c2010-02-12 10:34:29 +00001341 (MinOffset > 0))
1342 return false;
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001343 MinOffset = (uint64_t)BaseOffset + MinOffset;
1344 if (((int64_t)((uint64_t)BaseOffset + MaxOffset) > BaseOffset) !=
1345 (MaxOffset > 0))
1346 return false;
1347 MaxOffset = (uint64_t)BaseOffset + MaxOffset;
1348
1349 return isLegalUse(TTI, Kind, AccessTy, BaseGV, MinOffset, HasBaseReg,
1350 Scale) &&
1351 isLegalUse(TTI, Kind, AccessTy, BaseGV, MaxOffset, HasBaseReg, Scale);
Dan Gohman7979b722010-01-22 00:46:49 +00001352}
1353
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001354static bool isLegalUse(const TargetTransformInfo &TTI, int64_t MinOffset,
1355 int64_t MaxOffset, LSRUse::KindType Kind, Type *AccessTy,
1356 const Formula &F) {
Chandler Carrutha07dcb12013-01-07 15:04:40 +00001357 return isLegalUse(TTI, MinOffset, MaxOffset, Kind, AccessTy, F.BaseGV,
1358 F.BaseOffset, F.HasBaseReg, F.Scale);
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001359}
1360
1361static bool isAlwaysFoldable(const TargetTransformInfo &TTI,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001362 LSRUse::KindType Kind, Type *AccessTy,
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001363 GlobalValue *BaseGV, int64_t BaseOffset,
1364 bool HasBaseReg) {
Dan Gohman572645c2010-02-12 10:34:29 +00001365 // Fast-path: zero is always foldable.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001366 if (BaseOffset == 0 && !BaseGV) return true;
Dan Gohman7979b722010-01-22 00:46:49 +00001367
Dan Gohman572645c2010-02-12 10:34:29 +00001368 // Conservatively, create an address with an immediate and a
1369 // base and a scale.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001370 int64_t Scale = Kind == LSRUse::ICmpZero ? -1 : 1;
Dan Gohman7979b722010-01-22 00:46:49 +00001371
Dan Gohmana2086b32010-05-19 23:43:12 +00001372 // Canonicalize a scale of 1 to a base register if the formula doesn't
1373 // already have a base register.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001374 if (!HasBaseReg && Scale == 1) {
1375 Scale = 0;
1376 HasBaseReg = true;
Dan Gohmana2086b32010-05-19 23:43:12 +00001377 }
1378
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001379 return isLegalUse(TTI, Kind, AccessTy, BaseGV, BaseOffset, HasBaseReg, Scale);
Dan Gohman7979b722010-01-22 00:46:49 +00001380}
1381
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001382static bool isAlwaysFoldable(const TargetTransformInfo &TTI,
1383 ScalarEvolution &SE, int64_t MinOffset,
1384 int64_t MaxOffset, LSRUse::KindType Kind,
1385 Type *AccessTy, const SCEV *S, bool HasBaseReg) {
Dan Gohman572645c2010-02-12 10:34:29 +00001386 // Fast-path: zero is always foldable.
1387 if (S->isZero()) return true;
1388
1389 // Conservatively, create an address with an immediate and a
1390 // base and a scale.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001391 int64_t BaseOffset = ExtractImmediate(S, SE);
Dan Gohman572645c2010-02-12 10:34:29 +00001392 GlobalValue *BaseGV = ExtractSymbol(S, SE);
1393
1394 // If there's anything else involved, it's not foldable.
1395 if (!S->isZero()) return false;
1396
1397 // Fast-path: zero is always foldable.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001398 if (BaseOffset == 0 && !BaseGV) return true;
Dan Gohman572645c2010-02-12 10:34:29 +00001399
1400 // Conservatively, create an address with an immediate and a
1401 // base and a scale.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001402 int64_t Scale = Kind == LSRUse::ICmpZero ? -1 : 1;
Dan Gohman572645c2010-02-12 10:34:29 +00001403
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001404 return isLegalUse(TTI, MinOffset, MaxOffset, Kind, AccessTy, BaseGV,
1405 BaseOffset, HasBaseReg, Scale);
Dan Gohman7979b722010-01-22 00:46:49 +00001406}
1407
Dan Gohmanb6211712010-06-19 21:21:39 +00001408namespace {
1409
Dan Gohman1e3121c2010-06-19 21:29:59 +00001410/// UseMapDenseMapInfo - A DenseMapInfo implementation for holding
1411/// DenseMaps and DenseSets of pairs of const SCEV* and LSRUse::Kind.
1412struct UseMapDenseMapInfo {
1413 static std::pair<const SCEV *, LSRUse::KindType> getEmptyKey() {
1414 return std::make_pair(reinterpret_cast<const SCEV *>(-1), LSRUse::Basic);
1415 }
1416
1417 static std::pair<const SCEV *, LSRUse::KindType> getTombstoneKey() {
1418 return std::make_pair(reinterpret_cast<const SCEV *>(-2), LSRUse::Basic);
1419 }
1420
1421 static unsigned
1422 getHashValue(const std::pair<const SCEV *, LSRUse::KindType> &V) {
1423 unsigned Result = DenseMapInfo<const SCEV *>::getHashValue(V.first);
1424 Result ^= DenseMapInfo<unsigned>::getHashValue(unsigned(V.second));
1425 return Result;
1426 }
1427
1428 static bool isEqual(const std::pair<const SCEV *, LSRUse::KindType> &LHS,
1429 const std::pair<const SCEV *, LSRUse::KindType> &RHS) {
1430 return LHS == RHS;
1431 }
1432};
1433
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00001434/// IVInc - An individual increment in a Chain of IV increments.
1435/// Relate an IV user to an expression that computes the IV it uses from the IV
1436/// used by the previous link in the Chain.
1437///
1438/// For the head of a chain, IncExpr holds the absolute SCEV expression for the
1439/// original IVOperand. The head of the chain's IVOperand is only valid during
1440/// chain collection, before LSR replaces IV users. During chain generation,
1441/// IncExpr can be used to find the new IVOperand that computes the same
1442/// expression.
1443struct IVInc {
1444 Instruction *UserInst;
1445 Value* IVOperand;
1446 const SCEV *IncExpr;
1447
1448 IVInc(Instruction *U, Value *O, const SCEV *E):
1449 UserInst(U), IVOperand(O), IncExpr(E) {}
1450};
1451
1452// IVChain - The list of IV increments in program order.
1453// We typically add the head of a chain without finding subsequent links.
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00001454struct IVChain {
1455 SmallVector<IVInc,1> Incs;
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00001456 const SCEV *ExprBase;
1457
1458 IVChain() : ExprBase(0) {}
1459
1460 IVChain(const IVInc &Head, const SCEV *Base)
1461 : Incs(1, Head), ExprBase(Base) {}
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00001462
1463 typedef SmallVectorImpl<IVInc>::const_iterator const_iterator;
1464
1465 // begin - return the first increment in the chain.
1466 const_iterator begin() const {
1467 assert(!Incs.empty());
1468 return llvm::next(Incs.begin());
1469 }
1470 const_iterator end() const {
1471 return Incs.end();
1472 }
1473
1474 // hasIncs - Returns true if this chain contains any increments.
1475 bool hasIncs() const { return Incs.size() >= 2; }
1476
1477 // add - Add an IVInc to the end of this chain.
1478 void add(const IVInc &X) { Incs.push_back(X); }
1479
1480 // tailUserInst - Returns the last UserInst in the chain.
1481 Instruction *tailUserInst() const { return Incs.back().UserInst; }
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00001482
1483 // isProfitableIncrement - Returns true if IncExpr can be profitably added to
1484 // this chain.
1485 bool isProfitableIncrement(const SCEV *OperExpr,
1486 const SCEV *IncExpr,
1487 ScalarEvolution&);
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00001488};
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00001489
1490/// ChainUsers - Helper for CollectChains to track multiple IV increment uses.
1491/// Distinguish between FarUsers that definitely cross IV increments and
1492/// NearUsers that may be used between IV increments.
1493struct ChainUsers {
1494 SmallPtrSet<Instruction*, 4> FarUsers;
1495 SmallPtrSet<Instruction*, 4> NearUsers;
1496};
1497
Dan Gohman572645c2010-02-12 10:34:29 +00001498/// LSRInstance - This class holds state for the main loop strength reduction
1499/// logic.
1500class LSRInstance {
1501 IVUsers &IU;
1502 ScalarEvolution &SE;
1503 DominatorTree &DT;
Dan Gohmane5f76872010-04-09 22:07:05 +00001504 LoopInfo &LI;
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001505 const TargetTransformInfo &TTI;
Dan Gohman572645c2010-02-12 10:34:29 +00001506 Loop *const L;
1507 bool Changed;
1508
1509 /// IVIncInsertPos - This is the insert position that the current loop's
1510 /// induction variable increment should be placed. In simple loops, this is
1511 /// the latch block's terminator. But in more complicated cases, this is a
1512 /// position which will dominate all the in-loop post-increment users.
1513 Instruction *IVIncInsertPos;
1514
1515 /// Factors - Interesting factors between use strides.
1516 SmallSetVector<int64_t, 8> Factors;
1517
1518 /// Types - Interesting use types, to facilitate truncation reuse.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001519 SmallSetVector<Type *, 4> Types;
Dan Gohman572645c2010-02-12 10:34:29 +00001520
1521 /// Fixups - The list of operands which are to be replaced.
1522 SmallVector<LSRFixup, 16> Fixups;
1523
1524 /// Uses - The list of interesting uses.
1525 SmallVector<LSRUse, 16> Uses;
1526
1527 /// RegUses - Track which uses use which register candidates.
1528 RegUseTracker RegUses;
1529
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00001530 // Limit the number of chains to avoid quadratic behavior. We don't expect to
1531 // have more than a few IV increment chains in a loop. Missing a Chain falls
1532 // back to normal LSR behavior for those uses.
1533 static const unsigned MaxChains = 8;
1534
1535 /// IVChainVec - IV users can form a chain of IV increments.
1536 SmallVector<IVChain, MaxChains> IVChainVec;
1537
Andrew Trick22d20c22012-01-09 21:18:52 +00001538 /// IVIncSet - IV users that belong to profitable IVChains.
1539 SmallPtrSet<Use*, MaxChains> IVIncSet;
1540
Dan Gohman572645c2010-02-12 10:34:29 +00001541 void OptimizeShadowIV();
1542 bool FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse);
1543 ICmpInst *OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse);
Dan Gohmanc6519f92010-05-20 20:05:31 +00001544 void OptimizeLoopTermCond();
Dan Gohman572645c2010-02-12 10:34:29 +00001545
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00001546 void ChainInstruction(Instruction *UserInst, Instruction *IVOper,
1547 SmallVectorImpl<ChainUsers> &ChainUsersVec);
Andrew Trick22d20c22012-01-09 21:18:52 +00001548 void FinalizeChain(IVChain &Chain);
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00001549 void CollectChains();
Andrew Trick22d20c22012-01-09 21:18:52 +00001550 void GenerateIVChain(const IVChain &Chain, SCEVExpander &Rewriter,
1551 SmallVectorImpl<WeakVH> &DeadInsts);
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00001552
Dan Gohman572645c2010-02-12 10:34:29 +00001553 void CollectInterestingTypesAndFactors();
1554 void CollectFixupsAndInitialFormulae();
1555
1556 LSRFixup &getNewFixup() {
1557 Fixups.push_back(LSRFixup());
1558 return Fixups.back();
1559 }
1560
1561 // Support for sharing of LSRUses between LSRFixups.
Dan Gohman1e3121c2010-06-19 21:29:59 +00001562 typedef DenseMap<std::pair<const SCEV *, LSRUse::KindType>,
1563 size_t,
1564 UseMapDenseMapInfo> UseMapTy;
Dan Gohman572645c2010-02-12 10:34:29 +00001565 UseMapTy UseMap;
1566
Dan Gohman191bd642010-09-01 01:45:53 +00001567 bool reconcileNewOffset(LSRUse &LU, int64_t NewOffset, bool HasBaseReg,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001568 LSRUse::KindType Kind, Type *AccessTy);
Dan Gohman572645c2010-02-12 10:34:29 +00001569
1570 std::pair<size_t, int64_t> getUse(const SCEV *&Expr,
1571 LSRUse::KindType Kind,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001572 Type *AccessTy);
Dan Gohman572645c2010-02-12 10:34:29 +00001573
Dan Gohmanc6897702010-10-07 23:33:43 +00001574 void DeleteUse(LSRUse &LU, size_t LUIdx);
Dan Gohman5ce6d052010-05-20 15:17:54 +00001575
Dan Gohman191bd642010-09-01 01:45:53 +00001576 LSRUse *FindUseWithSimilarFormula(const Formula &F, const LSRUse &OrigLU);
Dan Gohmana2086b32010-05-19 23:43:12 +00001577
Dan Gohman454d26d2010-02-22 04:11:59 +00001578 void InsertInitialFormula(const SCEV *S, LSRUse &LU, size_t LUIdx);
Dan Gohman572645c2010-02-12 10:34:29 +00001579 void InsertSupplementalFormula(const SCEV *S, LSRUse &LU, size_t LUIdx);
1580 void CountRegisters(const Formula &F, size_t LUIdx);
1581 bool InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F);
1582
1583 void CollectLoopInvariantFixupsAndFormulae();
1584
1585 void GenerateReassociations(LSRUse &LU, unsigned LUIdx, Formula Base,
1586 unsigned Depth = 0);
1587 void GenerateCombinations(LSRUse &LU, unsigned LUIdx, Formula Base);
1588 void GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx, Formula Base);
1589 void GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx, Formula Base);
1590 void GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx, Formula Base);
1591 void GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base);
1592 void GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base);
1593 void GenerateCrossUseConstantOffsets();
1594 void GenerateAllReuseFormulae();
1595
1596 void FilterOutUndesirableDedicatedRegisters();
Dan Gohmand079c302010-05-18 22:51:59 +00001597
1598 size_t EstimateSearchSpaceComplexity() const;
Dan Gohman4aa5c2e2010-08-29 16:09:42 +00001599 void NarrowSearchSpaceByDetectingSupersets();
1600 void NarrowSearchSpaceByCollapsingUnrolledCode();
Dan Gohman4f7e18d2010-08-29 16:39:22 +00001601 void NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters();
Dan Gohman4aa5c2e2010-08-29 16:09:42 +00001602 void NarrowSearchSpaceByPickingWinnerRegs();
Dan Gohman572645c2010-02-12 10:34:29 +00001603 void NarrowSearchSpaceUsingHeuristics();
1604
1605 void SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
1606 Cost &SolutionCost,
1607 SmallVectorImpl<const Formula *> &Workspace,
1608 const Cost &CurCost,
1609 const SmallPtrSet<const SCEV *, 16> &CurRegs,
1610 DenseSet<const SCEV *> &VisitedRegs) const;
1611 void Solve(SmallVectorImpl<const Formula *> &Solution) const;
1612
Dan Gohmane5f76872010-04-09 22:07:05 +00001613 BasicBlock::iterator
1614 HoistInsertPosition(BasicBlock::iterator IP,
1615 const SmallVectorImpl<Instruction *> &Inputs) const;
Andrew Trickb5c26ef2012-01-20 07:41:13 +00001616 BasicBlock::iterator
1617 AdjustInsertPositionForExpand(BasicBlock::iterator IP,
1618 const LSRFixup &LF,
1619 const LSRUse &LU,
1620 SCEVExpander &Rewriter) const;
Dan Gohmand96eae82010-04-09 02:00:38 +00001621
Dan Gohman572645c2010-02-12 10:34:29 +00001622 Value *Expand(const LSRFixup &LF,
1623 const Formula &F,
Dan Gohman454d26d2010-02-22 04:11:59 +00001624 BasicBlock::iterator IP,
Dan Gohman572645c2010-02-12 10:34:29 +00001625 SCEVExpander &Rewriter,
Dan Gohman454d26d2010-02-22 04:11:59 +00001626 SmallVectorImpl<WeakVH> &DeadInsts) const;
Dan Gohman3a02cbc2010-02-16 20:25:07 +00001627 void RewriteForPHI(PHINode *PN, const LSRFixup &LF,
1628 const Formula &F,
Dan Gohman3a02cbc2010-02-16 20:25:07 +00001629 SCEVExpander &Rewriter,
1630 SmallVectorImpl<WeakVH> &DeadInsts,
Dan Gohman3a02cbc2010-02-16 20:25:07 +00001631 Pass *P) const;
Dan Gohman572645c2010-02-12 10:34:29 +00001632 void Rewrite(const LSRFixup &LF,
1633 const Formula &F,
Dan Gohman572645c2010-02-12 10:34:29 +00001634 SCEVExpander &Rewriter,
1635 SmallVectorImpl<WeakVH> &DeadInsts,
Dan Gohman572645c2010-02-12 10:34:29 +00001636 Pass *P) const;
1637 void ImplementSolution(const SmallVectorImpl<const Formula *> &Solution,
1638 Pass *P);
1639
Andrew Trickd56ef8d2011-12-13 00:55:33 +00001640public:
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001641 LSRInstance(Loop *L, Pass *P);
Dan Gohman572645c2010-02-12 10:34:29 +00001642
1643 bool getChanged() const { return Changed; }
1644
1645 void print_factors_and_types(raw_ostream &OS) const;
1646 void print_fixups(raw_ostream &OS) const;
1647 void print_uses(raw_ostream &OS) const;
1648 void print(raw_ostream &OS) const;
1649 void dump() const;
1650};
1651
1652}
1653
1654/// OptimizeShadowIV - If IV is used in a int-to-float cast
Dan Gohman3f46a3a2010-03-01 17:49:51 +00001655/// inside the loop then try to eliminate the cast operation.
Dan Gohman572645c2010-02-12 10:34:29 +00001656void LSRInstance::OptimizeShadowIV() {
1657 const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L);
1658 if (isa<SCEVCouldNotCompute>(BackedgeTakenCount))
1659 return;
1660
1661 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end();
1662 UI != E; /* empty */) {
1663 IVUsers::const_iterator CandidateUI = UI;
1664 ++UI;
1665 Instruction *ShadowUse = CandidateUI->getUser();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001666 Type *DestTy = NULL;
Andrew Trickc2c988e2011-07-21 01:05:01 +00001667 bool IsSigned = false;
Dan Gohman572645c2010-02-12 10:34:29 +00001668
1669 /* If shadow use is a int->float cast then insert a second IV
1670 to eliminate this cast.
1671
1672 for (unsigned i = 0; i < n; ++i)
1673 foo((double)i);
1674
1675 is transformed into
1676
1677 double d = 0.0;
1678 for (unsigned i = 0; i < n; ++i, ++d)
1679 foo(d);
1680 */
Andrew Trickc2c988e2011-07-21 01:05:01 +00001681 if (UIToFPInst *UCast = dyn_cast<UIToFPInst>(CandidateUI->getUser())) {
1682 IsSigned = false;
Dan Gohman572645c2010-02-12 10:34:29 +00001683 DestTy = UCast->getDestTy();
Andrew Trickc2c988e2011-07-21 01:05:01 +00001684 }
1685 else if (SIToFPInst *SCast = dyn_cast<SIToFPInst>(CandidateUI->getUser())) {
1686 IsSigned = true;
Dan Gohman572645c2010-02-12 10:34:29 +00001687 DestTy = SCast->getDestTy();
Andrew Trickc2c988e2011-07-21 01:05:01 +00001688 }
Dan Gohman572645c2010-02-12 10:34:29 +00001689 if (!DestTy) continue;
1690
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00001691 // If target does not support DestTy natively then do not apply
1692 // this transformation.
1693 if (!TTI.isTypeLegal(DestTy)) continue;
Dan Gohman572645c2010-02-12 10:34:29 +00001694
1695 PHINode *PH = dyn_cast<PHINode>(ShadowUse->getOperand(0));
1696 if (!PH) continue;
1697 if (PH->getNumIncomingValues() != 2) continue;
1698
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001699 Type *SrcTy = PH->getType();
Dan Gohman572645c2010-02-12 10:34:29 +00001700 int Mantissa = DestTy->getFPMantissaWidth();
1701 if (Mantissa == -1) continue;
1702 if ((int)SE.getTypeSizeInBits(SrcTy) > Mantissa)
1703 continue;
1704
1705 unsigned Entry, Latch;
1706 if (PH->getIncomingBlock(0) == L->getLoopPreheader()) {
1707 Entry = 0;
1708 Latch = 1;
Dan Gohman7979b722010-01-22 00:46:49 +00001709 } else {
Dan Gohman572645c2010-02-12 10:34:29 +00001710 Entry = 1;
1711 Latch = 0;
Dan Gohman7979b722010-01-22 00:46:49 +00001712 }
Dan Gohman7979b722010-01-22 00:46:49 +00001713
Dan Gohman572645c2010-02-12 10:34:29 +00001714 ConstantInt *Init = dyn_cast<ConstantInt>(PH->getIncomingValue(Entry));
1715 if (!Init) continue;
Andrew Trickc2c988e2011-07-21 01:05:01 +00001716 Constant *NewInit = ConstantFP::get(DestTy, IsSigned ?
Andrew Trickc205a092011-07-21 01:45:54 +00001717 (double)Init->getSExtValue() :
1718 (double)Init->getZExtValue());
Dan Gohman7979b722010-01-22 00:46:49 +00001719
Dan Gohman572645c2010-02-12 10:34:29 +00001720 BinaryOperator *Incr =
1721 dyn_cast<BinaryOperator>(PH->getIncomingValue(Latch));
1722 if (!Incr) continue;
1723 if (Incr->getOpcode() != Instruction::Add
1724 && Incr->getOpcode() != Instruction::Sub)
Dan Gohman7979b722010-01-22 00:46:49 +00001725 continue;
Dan Gohman7979b722010-01-22 00:46:49 +00001726
Dan Gohman572645c2010-02-12 10:34:29 +00001727 /* Initialize new IV, double d = 0.0 in above example. */
1728 ConstantInt *C = NULL;
1729 if (Incr->getOperand(0) == PH)
1730 C = dyn_cast<ConstantInt>(Incr->getOperand(1));
1731 else if (Incr->getOperand(1) == PH)
1732 C = dyn_cast<ConstantInt>(Incr->getOperand(0));
Dan Gohman7979b722010-01-22 00:46:49 +00001733 else
Dan Gohman7979b722010-01-22 00:46:49 +00001734 continue;
1735
Dan Gohman572645c2010-02-12 10:34:29 +00001736 if (!C) continue;
Dan Gohman7979b722010-01-22 00:46:49 +00001737
Dan Gohman572645c2010-02-12 10:34:29 +00001738 // Ignore negative constants, as the code below doesn't handle them
1739 // correctly. TODO: Remove this restriction.
1740 if (!C->getValue().isStrictlyPositive()) continue;
Dan Gohman7979b722010-01-22 00:46:49 +00001741
Dan Gohman572645c2010-02-12 10:34:29 +00001742 /* Add new PHINode. */
Jay Foad3ecfc862011-03-30 11:28:46 +00001743 PHINode *NewPH = PHINode::Create(DestTy, 2, "IV.S.", PH);
Dan Gohman7979b722010-01-22 00:46:49 +00001744
Dan Gohman572645c2010-02-12 10:34:29 +00001745 /* create new increment. '++d' in above example. */
1746 Constant *CFP = ConstantFP::get(DestTy, C->getZExtValue());
1747 BinaryOperator *NewIncr =
1748 BinaryOperator::Create(Incr->getOpcode() == Instruction::Add ?
1749 Instruction::FAdd : Instruction::FSub,
1750 NewPH, CFP, "IV.S.next.", Incr);
Dan Gohman7979b722010-01-22 00:46:49 +00001751
Dan Gohman572645c2010-02-12 10:34:29 +00001752 NewPH->addIncoming(NewInit, PH->getIncomingBlock(Entry));
1753 NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch));
Dan Gohman7979b722010-01-22 00:46:49 +00001754
Dan Gohman572645c2010-02-12 10:34:29 +00001755 /* Remove cast operation */
1756 ShadowUse->replaceAllUsesWith(NewPH);
1757 ShadowUse->eraseFromParent();
Dan Gohmanc6519f92010-05-20 20:05:31 +00001758 Changed = true;
Dan Gohman572645c2010-02-12 10:34:29 +00001759 break;
Dan Gohman7979b722010-01-22 00:46:49 +00001760 }
1761}
1762
1763/// FindIVUserForCond - If Cond has an operand that is an expression of an IV,
1764/// set the IV user and stride information and return true, otherwise return
1765/// false.
Dan Gohmanea507f52010-05-20 19:44:23 +00001766bool LSRInstance::FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse) {
Dan Gohman572645c2010-02-12 10:34:29 +00001767 for (IVUsers::iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI)
1768 if (UI->getUser() == Cond) {
1769 // NOTE: we could handle setcc instructions with multiple uses here, but
1770 // InstCombine does it as well for simple uses, it's not clear that it
1771 // occurs enough in real life to handle.
1772 CondUse = UI;
1773 return true;
1774 }
Dan Gohman7979b722010-01-22 00:46:49 +00001775 return false;
Evan Chengcdf43b12007-10-25 09:11:16 +00001776}
1777
Dan Gohman7979b722010-01-22 00:46:49 +00001778/// OptimizeMax - Rewrite the loop's terminating condition if it uses
1779/// a max computation.
1780///
1781/// This is a narrow solution to a specific, but acute, problem. For loops
1782/// like this:
1783///
1784/// i = 0;
1785/// do {
1786/// p[i] = 0.0;
1787/// } while (++i < n);
1788///
1789/// the trip count isn't just 'n', because 'n' might not be positive. And
1790/// unfortunately this can come up even for loops where the user didn't use
1791/// a C do-while loop. For example, seemingly well-behaved top-test loops
1792/// will commonly be lowered like this:
1793//
1794/// if (n > 0) {
1795/// i = 0;
1796/// do {
1797/// p[i] = 0.0;
1798/// } while (++i < n);
1799/// }
1800///
1801/// and then it's possible for subsequent optimization to obscure the if
1802/// test in such a way that indvars can't find it.
1803///
1804/// When indvars can't find the if test in loops like this, it creates a
1805/// max expression, which allows it to give the loop a canonical
1806/// induction variable:
1807///
1808/// i = 0;
1809/// max = n < 1 ? 1 : n;
1810/// do {
1811/// p[i] = 0.0;
1812/// } while (++i != max);
1813///
1814/// Canonical induction variables are necessary because the loop passes
1815/// are designed around them. The most obvious example of this is the
1816/// LoopInfo analysis, which doesn't remember trip count values. It
1817/// expects to be able to rediscover the trip count each time it is
Dan Gohman572645c2010-02-12 10:34:29 +00001818/// needed, and it does this using a simple analysis that only succeeds if
Dan Gohman7979b722010-01-22 00:46:49 +00001819/// the loop has a canonical induction variable.
1820///
1821/// However, when it comes time to generate code, the maximum operation
1822/// can be quite costly, especially if it's inside of an outer loop.
1823///
1824/// This function solves this problem by detecting this type of loop and
1825/// rewriting their conditions from ICMP_NE back to ICMP_SLT, and deleting
1826/// the instructions for the maximum computation.
1827///
Dan Gohman572645c2010-02-12 10:34:29 +00001828ICmpInst *LSRInstance::OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse) {
Dan Gohman7979b722010-01-22 00:46:49 +00001829 // Check that the loop matches the pattern we're looking for.
1830 if (Cond->getPredicate() != CmpInst::ICMP_EQ &&
1831 Cond->getPredicate() != CmpInst::ICMP_NE)
1832 return Cond;
Dan Gohmana10756e2010-01-21 02:09:26 +00001833
Dan Gohman7979b722010-01-22 00:46:49 +00001834 SelectInst *Sel = dyn_cast<SelectInst>(Cond->getOperand(1));
1835 if (!Sel || !Sel->hasOneUse()) return Cond;
Dan Gohmana10756e2010-01-21 02:09:26 +00001836
Dan Gohman572645c2010-02-12 10:34:29 +00001837 const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L);
Dan Gohman7979b722010-01-22 00:46:49 +00001838 if (isa<SCEVCouldNotCompute>(BackedgeTakenCount))
1839 return Cond;
Dan Gohmandeff6212010-05-03 22:09:21 +00001840 const SCEV *One = SE.getConstant(BackedgeTakenCount->getType(), 1);
Dan Gohmana10756e2010-01-21 02:09:26 +00001841
Dan Gohman7979b722010-01-22 00:46:49 +00001842 // Add one to the backedge-taken count to get the trip count.
Dan Gohman4065f602010-08-16 15:39:27 +00001843 const SCEV *IterationCount = SE.getAddExpr(One, BackedgeTakenCount);
Dan Gohman1d367982010-04-24 03:13:44 +00001844 if (IterationCount != SE.getSCEV(Sel)) return Cond;
Dan Gohman7979b722010-01-22 00:46:49 +00001845
Dan Gohman1d367982010-04-24 03:13:44 +00001846 // Check for a max calculation that matches the pattern. There's no check
1847 // for ICMP_ULE here because the comparison would be with zero, which
1848 // isn't interesting.
1849 CmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
1850 const SCEVNAryExpr *Max = 0;
1851 if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(BackedgeTakenCount)) {
1852 Pred = ICmpInst::ICMP_SLE;
1853 Max = S;
1854 } else if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(IterationCount)) {
1855 Pred = ICmpInst::ICMP_SLT;
1856 Max = S;
1857 } else if (const SCEVUMaxExpr *U = dyn_cast<SCEVUMaxExpr>(IterationCount)) {
1858 Pred = ICmpInst::ICMP_ULT;
1859 Max = U;
1860 } else {
1861 // No match; bail.
Dan Gohman7979b722010-01-22 00:46:49 +00001862 return Cond;
Dan Gohman1d367982010-04-24 03:13:44 +00001863 }
Dan Gohman7979b722010-01-22 00:46:49 +00001864
1865 // To handle a max with more than two operands, this optimization would
1866 // require additional checking and setup.
1867 if (Max->getNumOperands() != 2)
1868 return Cond;
1869
1870 const SCEV *MaxLHS = Max->getOperand(0);
1871 const SCEV *MaxRHS = Max->getOperand(1);
Dan Gohman1d367982010-04-24 03:13:44 +00001872
1873 // ScalarEvolution canonicalizes constants to the left. For < and >, look
1874 // for a comparison with 1. For <= and >=, a comparison with zero.
1875 if (!MaxLHS ||
1876 (ICmpInst::isTrueWhenEqual(Pred) ? !MaxLHS->isZero() : (MaxLHS != One)))
1877 return Cond;
1878
Dan Gohman7979b722010-01-22 00:46:49 +00001879 // Check the relevant induction variable for conformance to
1880 // the pattern.
Dan Gohman572645c2010-02-12 10:34:29 +00001881 const SCEV *IV = SE.getSCEV(Cond->getOperand(0));
Dan Gohman7979b722010-01-22 00:46:49 +00001882 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(IV);
1883 if (!AR || !AR->isAffine() ||
1884 AR->getStart() != One ||
Dan Gohman572645c2010-02-12 10:34:29 +00001885 AR->getStepRecurrence(SE) != One)
Dan Gohman7979b722010-01-22 00:46:49 +00001886 return Cond;
1887
1888 assert(AR->getLoop() == L &&
1889 "Loop condition operand is an addrec in a different loop!");
1890
1891 // Check the right operand of the select, and remember it, as it will
1892 // be used in the new comparison instruction.
1893 Value *NewRHS = 0;
Dan Gohman1d367982010-04-24 03:13:44 +00001894 if (ICmpInst::isTrueWhenEqual(Pred)) {
1895 // Look for n+1, and grab n.
1896 if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(1)))
1897 if (isa<ConstantInt>(BO->getOperand(1)) &&
1898 cast<ConstantInt>(BO->getOperand(1))->isOne() &&
1899 SE.getSCEV(BO->getOperand(0)) == MaxRHS)
1900 NewRHS = BO->getOperand(0);
1901 if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(2)))
1902 if (isa<ConstantInt>(BO->getOperand(1)) &&
1903 cast<ConstantInt>(BO->getOperand(1))->isOne() &&
1904 SE.getSCEV(BO->getOperand(0)) == MaxRHS)
1905 NewRHS = BO->getOperand(0);
1906 if (!NewRHS)
1907 return Cond;
1908 } else if (SE.getSCEV(Sel->getOperand(1)) == MaxRHS)
Dan Gohman7979b722010-01-22 00:46:49 +00001909 NewRHS = Sel->getOperand(1);
Dan Gohman572645c2010-02-12 10:34:29 +00001910 else if (SE.getSCEV(Sel->getOperand(2)) == MaxRHS)
Dan Gohman7979b722010-01-22 00:46:49 +00001911 NewRHS = Sel->getOperand(2);
Dan Gohmancaf71ab2010-06-22 23:07:13 +00001912 else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(MaxRHS))
1913 NewRHS = SU->getValue();
Dan Gohman1d367982010-04-24 03:13:44 +00001914 else
Dan Gohmancaf71ab2010-06-22 23:07:13 +00001915 // Max doesn't match expected pattern.
1916 return Cond;
Dan Gohman7979b722010-01-22 00:46:49 +00001917
1918 // Determine the new comparison opcode. It may be signed or unsigned,
1919 // and the original comparison may be either equality or inequality.
Dan Gohman7979b722010-01-22 00:46:49 +00001920 if (Cond->getPredicate() == CmpInst::ICMP_EQ)
1921 Pred = CmpInst::getInversePredicate(Pred);
1922
1923 // Ok, everything looks ok to change the condition into an SLT or SGE and
1924 // delete the max calculation.
1925 ICmpInst *NewCond =
1926 new ICmpInst(Cond, Pred, Cond->getOperand(0), NewRHS, "scmp");
1927
1928 // Delete the max calculation instructions.
1929 Cond->replaceAllUsesWith(NewCond);
1930 CondUse->setUser(NewCond);
1931 Instruction *Cmp = cast<Instruction>(Sel->getOperand(0));
1932 Cond->eraseFromParent();
1933 Sel->eraseFromParent();
1934 if (Cmp->use_empty())
1935 Cmp->eraseFromParent();
1936 return NewCond;
Dan Gohmanad7321f2008-09-15 21:22:06 +00001937}
1938
Jim Grosbach56a1f802009-11-17 17:53:56 +00001939/// OptimizeLoopTermCond - Change loop terminating condition to use the
Evan Cheng586f69a2009-11-12 07:35:05 +00001940/// postinc iv when possible.
Dan Gohmanc6519f92010-05-20 20:05:31 +00001941void
Dan Gohman572645c2010-02-12 10:34:29 +00001942LSRInstance::OptimizeLoopTermCond() {
1943 SmallPtrSet<Instruction *, 4> PostIncs;
1944
Evan Cheng586f69a2009-11-12 07:35:05 +00001945 BasicBlock *LatchBlock = L->getLoopLatch();
Evan Cheng076e0852009-11-17 18:10:11 +00001946 SmallVector<BasicBlock*, 8> ExitingBlocks;
1947 L->getExitingBlocks(ExitingBlocks);
Jim Grosbach56a1f802009-11-17 17:53:56 +00001948
Evan Cheng076e0852009-11-17 18:10:11 +00001949 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
1950 BasicBlock *ExitingBlock = ExitingBlocks[i];
Evan Cheng586f69a2009-11-12 07:35:05 +00001951
Dan Gohman572645c2010-02-12 10:34:29 +00001952 // Get the terminating condition for the loop if possible. If we
Evan Cheng076e0852009-11-17 18:10:11 +00001953 // can, we want to change it to use a post-incremented version of its
1954 // induction variable, to allow coalescing the live ranges for the IV into
1955 // one register value.
Evan Cheng586f69a2009-11-12 07:35:05 +00001956
Evan Cheng076e0852009-11-17 18:10:11 +00001957 BranchInst *TermBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
1958 if (!TermBr)
1959 continue;
1960 // FIXME: Overly conservative, termination condition could be an 'or' etc..
1961 if (TermBr->isUnconditional() || !isa<ICmpInst>(TermBr->getCondition()))
1962 continue;
Evan Cheng586f69a2009-11-12 07:35:05 +00001963
Evan Cheng076e0852009-11-17 18:10:11 +00001964 // Search IVUsesByStride to find Cond's IVUse if there is one.
1965 IVStrideUse *CondUse = 0;
Evan Cheng076e0852009-11-17 18:10:11 +00001966 ICmpInst *Cond = cast<ICmpInst>(TermBr->getCondition());
Dan Gohman572645c2010-02-12 10:34:29 +00001967 if (!FindIVUserForCond(Cond, CondUse))
Evan Cheng076e0852009-11-17 18:10:11 +00001968 continue;
1969
Evan Cheng076e0852009-11-17 18:10:11 +00001970 // If the trip count is computed in terms of a max (due to ScalarEvolution
1971 // being unable to find a sufficient guard, for example), change the loop
1972 // comparison to use SLT or ULT instead of NE.
Dan Gohman572645c2010-02-12 10:34:29 +00001973 // One consequence of doing this now is that it disrupts the count-down
1974 // optimization. That's not always a bad thing though, because in such
1975 // cases it may still be worthwhile to avoid a max.
1976 Cond = OptimizeMax(Cond, CondUse);
Evan Cheng076e0852009-11-17 18:10:11 +00001977
Dan Gohman572645c2010-02-12 10:34:29 +00001978 // If this exiting block dominates the latch block, it may also use
1979 // the post-inc value if it won't be shared with other uses.
1980 // Check for dominance.
1981 if (!DT.dominates(ExitingBlock, LatchBlock))
Dan Gohman7979b722010-01-22 00:46:49 +00001982 continue;
Evan Cheng076e0852009-11-17 18:10:11 +00001983
Dan Gohman572645c2010-02-12 10:34:29 +00001984 // Conservatively avoid trying to use the post-inc value in non-latch
1985 // exits if there may be pre-inc users in intervening blocks.
Dan Gohman590bfe82010-02-14 03:21:49 +00001986 if (LatchBlock != ExitingBlock)
Dan Gohman572645c2010-02-12 10:34:29 +00001987 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI)
1988 // Test if the use is reachable from the exiting block. This dominator
1989 // query is a conservative approximation of reachability.
1990 if (&*UI != CondUse &&
1991 !DT.properlyDominates(UI->getUser()->getParent(), ExitingBlock)) {
1992 // Conservatively assume there may be reuse if the quotient of their
1993 // strides could be a legal scale.
Dan Gohmanc0564542010-04-19 21:48:58 +00001994 const SCEV *A = IU.getStride(*CondUse, L);
1995 const SCEV *B = IU.getStride(*UI, L);
Dan Gohman448db1c2010-04-07 22:27:08 +00001996 if (!A || !B) continue;
Dan Gohman572645c2010-02-12 10:34:29 +00001997 if (SE.getTypeSizeInBits(A->getType()) !=
1998 SE.getTypeSizeInBits(B->getType())) {
1999 if (SE.getTypeSizeInBits(A->getType()) >
2000 SE.getTypeSizeInBits(B->getType()))
2001 B = SE.getSignExtendExpr(B, A->getType());
2002 else
2003 A = SE.getSignExtendExpr(A, B->getType());
2004 }
2005 if (const SCEVConstant *D =
Dan Gohmanf09b7122010-02-19 19:35:48 +00002006 dyn_cast_or_null<SCEVConstant>(getExactSDiv(B, A, SE))) {
Dan Gohman9f383eb2010-05-20 22:25:20 +00002007 const ConstantInt *C = D->getValue();
Dan Gohman572645c2010-02-12 10:34:29 +00002008 // Stride of one or negative one can have reuse with non-addresses.
Dan Gohman9f383eb2010-05-20 22:25:20 +00002009 if (C->isOne() || C->isAllOnesValue())
Dan Gohman572645c2010-02-12 10:34:29 +00002010 goto decline_post_inc;
2011 // Avoid weird situations.
Dan Gohman9f383eb2010-05-20 22:25:20 +00002012 if (C->getValue().getMinSignedBits() >= 64 ||
2013 C->getValue().isMinSignedValue())
Dan Gohman572645c2010-02-12 10:34:29 +00002014 goto decline_post_inc;
2015 // Check for possible scaled-address reuse.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002016 Type *AccessTy = getAccessType(UI->getUser());
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00002017 int64_t Scale = C->getSExtValue();
2018 if (TTI.isLegalAddressingMode(AccessTy, /*BaseGV=*/ 0,
2019 /*BaseOffset=*/ 0,
2020 /*HasBaseReg=*/ false, Scale))
Dan Gohman572645c2010-02-12 10:34:29 +00002021 goto decline_post_inc;
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00002022 Scale = -Scale;
2023 if (TTI.isLegalAddressingMode(AccessTy, /*BaseGV=*/ 0,
2024 /*BaseOffset=*/ 0,
2025 /*HasBaseReg=*/ false, Scale))
Dan Gohman572645c2010-02-12 10:34:29 +00002026 goto decline_post_inc;
2027 }
2028 }
2029
David Greene63c94632009-12-23 22:58:38 +00002030 DEBUG(dbgs() << " Change loop exiting icmp to use postinc iv: "
Dan Gohman572645c2010-02-12 10:34:29 +00002031 << *Cond << '\n');
Evan Cheng076e0852009-11-17 18:10:11 +00002032
2033 // It's possible for the setcc instruction to be anywhere in the loop, and
2034 // possible for it to have multiple users. If it is not immediately before
2035 // the exiting block branch, move it.
Dan Gohman572645c2010-02-12 10:34:29 +00002036 if (&*++BasicBlock::iterator(Cond) != TermBr) {
2037 if (Cond->hasOneUse()) {
Evan Cheng076e0852009-11-17 18:10:11 +00002038 Cond->moveBefore(TermBr);
2039 } else {
Dan Gohman572645c2010-02-12 10:34:29 +00002040 // Clone the terminating condition and insert into the loopend.
2041 ICmpInst *OldCond = Cond;
Evan Cheng076e0852009-11-17 18:10:11 +00002042 Cond = cast<ICmpInst>(Cond->clone());
2043 Cond->setName(L->getHeader()->getName() + ".termcond");
2044 ExitingBlock->getInstList().insert(TermBr, Cond);
2045
2046 // Clone the IVUse, as the old use still exists!
Andrew Trick4417e532011-06-21 15:43:52 +00002047 CondUse = &IU.AddUser(Cond, CondUse->getOperandValToReplace());
Dan Gohman572645c2010-02-12 10:34:29 +00002048 TermBr->replaceUsesOfWith(OldCond, Cond);
Evan Cheng076e0852009-11-17 18:10:11 +00002049 }
Evan Cheng586f69a2009-11-12 07:35:05 +00002050 }
2051
Evan Cheng076e0852009-11-17 18:10:11 +00002052 // If we get to here, we know that we can transform the setcc instruction to
2053 // use the post-incremented version of the IV, allowing us to coalesce the
2054 // live ranges for the IV correctly.
Dan Gohman448db1c2010-04-07 22:27:08 +00002055 CondUse->transformToPostInc(L);
Evan Cheng076e0852009-11-17 18:10:11 +00002056 Changed = true;
2057
Dan Gohman572645c2010-02-12 10:34:29 +00002058 PostIncs.insert(Cond);
2059 decline_post_inc:;
Dan Gohmana10756e2010-01-21 02:09:26 +00002060 }
Dan Gohman572645c2010-02-12 10:34:29 +00002061
2062 // Determine an insertion point for the loop induction variable increment. It
2063 // must dominate all the post-inc comparisons we just set up, and it must
2064 // dominate the loop latch edge.
2065 IVIncInsertPos = L->getLoopLatch()->getTerminator();
2066 for (SmallPtrSet<Instruction *, 4>::const_iterator I = PostIncs.begin(),
2067 E = PostIncs.end(); I != E; ++I) {
2068 BasicBlock *BB =
2069 DT.findNearestCommonDominator(IVIncInsertPos->getParent(),
2070 (*I)->getParent());
2071 if (BB == (*I)->getParent())
2072 IVIncInsertPos = *I;
2073 else if (BB != IVIncInsertPos->getParent())
2074 IVIncInsertPos = BB->getTerminator();
2075 }
Dan Gohmana10756e2010-01-21 02:09:26 +00002076}
2077
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002078/// reconcileNewOffset - Determine if the given use can accommodate a fixup
Dan Gohman76c315a2010-05-20 20:52:00 +00002079/// at the given offset and other details. If so, update the use and
2080/// return true.
Dan Gohman572645c2010-02-12 10:34:29 +00002081bool
Dan Gohman191bd642010-09-01 01:45:53 +00002082LSRInstance::reconcileNewOffset(LSRUse &LU, int64_t NewOffset, bool HasBaseReg,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002083 LSRUse::KindType Kind, Type *AccessTy) {
Dan Gohman191bd642010-09-01 01:45:53 +00002084 int64_t NewMinOffset = LU.MinOffset;
2085 int64_t NewMaxOffset = LU.MaxOffset;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002086 Type *NewAccessTy = AccessTy;
Dan Gohman7979b722010-01-22 00:46:49 +00002087
Dan Gohman572645c2010-02-12 10:34:29 +00002088 // Check for a mismatched kind. It's tempting to collapse mismatched kinds to
2089 // something conservative, however this can pessimize in the case that one of
2090 // the uses will have all its uses outside the loop, for example.
2091 if (LU.Kind != Kind)
Dan Gohman7979b722010-01-22 00:46:49 +00002092 return false;
Dan Gohman572645c2010-02-12 10:34:29 +00002093 // Conservatively assume HasBaseReg is true for now.
Dan Gohman191bd642010-09-01 01:45:53 +00002094 if (NewOffset < LU.MinOffset) {
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00002095 if (!isAlwaysFoldable(TTI, Kind, AccessTy, /*BaseGV=*/ 0,
2096 LU.MaxOffset - NewOffset, HasBaseReg))
Dan Gohman7979b722010-01-22 00:46:49 +00002097 return false;
Dan Gohman191bd642010-09-01 01:45:53 +00002098 NewMinOffset = NewOffset;
2099 } else if (NewOffset > LU.MaxOffset) {
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00002100 if (!isAlwaysFoldable(TTI, Kind, AccessTy, /*BaseGV=*/ 0,
2101 NewOffset - LU.MinOffset, HasBaseReg))
Dan Gohman7979b722010-01-22 00:46:49 +00002102 return false;
Dan Gohman191bd642010-09-01 01:45:53 +00002103 NewMaxOffset = NewOffset;
Dan Gohmana10756e2010-01-21 02:09:26 +00002104 }
Dan Gohman572645c2010-02-12 10:34:29 +00002105 // Check for a mismatched access type, and fall back conservatively as needed.
Dan Gohman74e5ef02010-06-19 21:30:18 +00002106 // TODO: Be less conservative when the type is similar and can use the same
2107 // addressing modes.
Dan Gohman572645c2010-02-12 10:34:29 +00002108 if (Kind == LSRUse::Address && AccessTy != LU.AccessTy)
Dan Gohman191bd642010-09-01 01:45:53 +00002109 NewAccessTy = Type::getVoidTy(AccessTy->getContext());
Dan Gohmana10756e2010-01-21 02:09:26 +00002110
Dan Gohman572645c2010-02-12 10:34:29 +00002111 // Update the use.
Dan Gohman191bd642010-09-01 01:45:53 +00002112 LU.MinOffset = NewMinOffset;
2113 LU.MaxOffset = NewMaxOffset;
2114 LU.AccessTy = NewAccessTy;
2115 if (NewOffset != LU.Offsets.back())
2116 LU.Offsets.push_back(NewOffset);
Dan Gohman8b0ade32010-01-21 22:42:49 +00002117 return true;
2118}
2119
Dan Gohman572645c2010-02-12 10:34:29 +00002120/// getUse - Return an LSRUse index and an offset value for a fixup which
2121/// needs the given expression, with the given kind and optional access type.
Dan Gohman3f46a3a2010-03-01 17:49:51 +00002122/// Either reuse an existing use or create a new one, as needed.
Dan Gohman572645c2010-02-12 10:34:29 +00002123std::pair<size_t, int64_t>
2124LSRInstance::getUse(const SCEV *&Expr,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002125 LSRUse::KindType Kind, Type *AccessTy) {
Dan Gohman572645c2010-02-12 10:34:29 +00002126 const SCEV *Copy = Expr;
2127 int64_t Offset = ExtractImmediate(Expr, SE);
Evan Cheng586f69a2009-11-12 07:35:05 +00002128
Dan Gohman572645c2010-02-12 10:34:29 +00002129 // Basic uses can't accept any offset, for example.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00002130 if (!isAlwaysFoldable(TTI, Kind, AccessTy, /*BaseGV=*/ 0,
2131 Offset, /*HasBaseReg=*/ true)) {
Dan Gohman572645c2010-02-12 10:34:29 +00002132 Expr = Copy;
2133 Offset = 0;
2134 }
2135
2136 std::pair<UseMapTy::iterator, bool> P =
Dan Gohman1e3121c2010-06-19 21:29:59 +00002137 UseMap.insert(std::make_pair(std::make_pair(Expr, Kind), 0));
Dan Gohman572645c2010-02-12 10:34:29 +00002138 if (!P.second) {
2139 // A use already existed with this base.
2140 size_t LUIdx = P.first->second;
2141 LSRUse &LU = Uses[LUIdx];
Dan Gohman191bd642010-09-01 01:45:53 +00002142 if (reconcileNewOffset(LU, Offset, /*HasBaseReg=*/true, Kind, AccessTy))
Dan Gohman572645c2010-02-12 10:34:29 +00002143 // Reuse this use.
2144 return std::make_pair(LUIdx, Offset);
2145 }
2146
2147 // Create a new use.
2148 size_t LUIdx = Uses.size();
2149 P.first->second = LUIdx;
2150 Uses.push_back(LSRUse(Kind, AccessTy));
2151 LSRUse &LU = Uses[LUIdx];
2152
Dan Gohman191bd642010-09-01 01:45:53 +00002153 // We don't need to track redundant offsets, but we don't need to go out
2154 // of our way here to avoid them.
2155 if (LU.Offsets.empty() || Offset != LU.Offsets.back())
2156 LU.Offsets.push_back(Offset);
2157
Dan Gohman572645c2010-02-12 10:34:29 +00002158 LU.MinOffset = Offset;
2159 LU.MaxOffset = Offset;
2160 return std::make_pair(LUIdx, Offset);
2161}
2162
Dan Gohman5ce6d052010-05-20 15:17:54 +00002163/// DeleteUse - Delete the given use from the Uses list.
Dan Gohmanc6897702010-10-07 23:33:43 +00002164void LSRInstance::DeleteUse(LSRUse &LU, size_t LUIdx) {
Dan Gohman191bd642010-09-01 01:45:53 +00002165 if (&LU != &Uses.back())
Dan Gohman5ce6d052010-05-20 15:17:54 +00002166 std::swap(LU, Uses.back());
2167 Uses.pop_back();
Dan Gohmanc6897702010-10-07 23:33:43 +00002168
2169 // Update RegUses.
2170 RegUses.SwapAndDropUse(LUIdx, Uses.size());
Dan Gohman5ce6d052010-05-20 15:17:54 +00002171}
2172
Dan Gohmana2086b32010-05-19 23:43:12 +00002173/// FindUseWithFormula - Look for a use distinct from OrigLU which is has
2174/// a formula that has the same registers as the given formula.
2175LSRUse *
2176LSRInstance::FindUseWithSimilarFormula(const Formula &OrigF,
Dan Gohman191bd642010-09-01 01:45:53 +00002177 const LSRUse &OrigLU) {
2178 // Search all uses for the formula. This could be more clever.
Dan Gohmana2086b32010-05-19 23:43:12 +00002179 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
2180 LSRUse &LU = Uses[LUIdx];
Dan Gohman6a832712010-08-29 15:27:08 +00002181 // Check whether this use is close enough to OrigLU, to see whether it's
2182 // worthwhile looking through its formulae.
2183 // Ignore ICmpZero uses because they may contain formulae generated by
2184 // GenerateICmpZeroScales, in which case adding fixup offsets may
2185 // be invalid.
Dan Gohmana2086b32010-05-19 23:43:12 +00002186 if (&LU != &OrigLU &&
2187 LU.Kind != LSRUse::ICmpZero &&
2188 LU.Kind == OrigLU.Kind && OrigLU.AccessTy == LU.AccessTy &&
Dan Gohmana9db1292010-07-15 20:24:58 +00002189 LU.WidestFixupType == OrigLU.WidestFixupType &&
Dan Gohmana2086b32010-05-19 23:43:12 +00002190 LU.HasFormulaWithSameRegs(OrigF)) {
Dan Gohman6a832712010-08-29 15:27:08 +00002191 // Scan through this use's formulae.
Dan Gohman402d4352010-05-20 20:33:18 +00002192 for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(),
2193 E = LU.Formulae.end(); I != E; ++I) {
2194 const Formula &F = *I;
Dan Gohman6a832712010-08-29 15:27:08 +00002195 // Check to see if this formula has the same registers and symbols
2196 // as OrigF.
Dan Gohmana2086b32010-05-19 23:43:12 +00002197 if (F.BaseRegs == OrigF.BaseRegs &&
2198 F.ScaledReg == OrigF.ScaledReg &&
Chandler Carrutha07dcb12013-01-07 15:04:40 +00002199 F.BaseGV == OrigF.BaseGV &&
2200 F.Scale == OrigF.Scale &&
Dan Gohmancca82142011-05-03 00:46:49 +00002201 F.UnfoldedOffset == OrigF.UnfoldedOffset) {
Chandler Carrutha07dcb12013-01-07 15:04:40 +00002202 if (F.BaseOffset == 0)
Dan Gohmana2086b32010-05-19 23:43:12 +00002203 return &LU;
Dan Gohman6a832712010-08-29 15:27:08 +00002204 // This is the formula where all the registers and symbols matched;
2205 // there aren't going to be any others. Since we declined it, we
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002206 // can skip the rest of the formulae and proceed to the next LSRUse.
Dan Gohmana2086b32010-05-19 23:43:12 +00002207 break;
2208 }
2209 }
2210 }
2211 }
2212
Dan Gohman6a832712010-08-29 15:27:08 +00002213 // Nothing looked good.
Dan Gohmana2086b32010-05-19 23:43:12 +00002214 return 0;
2215}
2216
Dan Gohman572645c2010-02-12 10:34:29 +00002217void LSRInstance::CollectInterestingTypesAndFactors() {
2218 SmallSetVector<const SCEV *, 4> Strides;
2219
Dan Gohman1b7bf182010-02-19 00:05:23 +00002220 // Collect interesting types and strides.
Dan Gohman448db1c2010-04-07 22:27:08 +00002221 SmallVector<const SCEV *, 4> Worklist;
Dan Gohman572645c2010-02-12 10:34:29 +00002222 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) {
Dan Gohmanc0564542010-04-19 21:48:58 +00002223 const SCEV *Expr = IU.getExpr(*UI);
Dan Gohman572645c2010-02-12 10:34:29 +00002224
2225 // Collect interesting types.
Dan Gohman448db1c2010-04-07 22:27:08 +00002226 Types.insert(SE.getEffectiveSCEVType(Expr->getType()));
Dan Gohman572645c2010-02-12 10:34:29 +00002227
Dan Gohman448db1c2010-04-07 22:27:08 +00002228 // Add strides for mentioned loops.
2229 Worklist.push_back(Expr);
2230 do {
2231 const SCEV *S = Worklist.pop_back_val();
2232 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
Andrew Trickbd618f12012-03-22 22:42:45 +00002233 if (AR->getLoop() == L)
Andrew Trickfa1948a2011-12-10 00:25:00 +00002234 Strides.insert(AR->getStepRecurrence(SE));
Dan Gohman448db1c2010-04-07 22:27:08 +00002235 Worklist.push_back(AR->getStart());
2236 } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
Dan Gohman403a8cd2010-06-21 19:47:52 +00002237 Worklist.append(Add->op_begin(), Add->op_end());
Dan Gohman448db1c2010-04-07 22:27:08 +00002238 }
2239 } while (!Worklist.empty());
Dan Gohman1b7bf182010-02-19 00:05:23 +00002240 }
2241
2242 // Compute interesting factors from the set of interesting strides.
2243 for (SmallSetVector<const SCEV *, 4>::const_iterator
2244 I = Strides.begin(), E = Strides.end(); I != E; ++I)
Dan Gohman572645c2010-02-12 10:34:29 +00002245 for (SmallSetVector<const SCEV *, 4>::const_iterator NewStrideIter =
Oscar Fuentesee56c422010-08-02 06:00:15 +00002246 llvm::next(I); NewStrideIter != E; ++NewStrideIter) {
Dan Gohman1b7bf182010-02-19 00:05:23 +00002247 const SCEV *OldStride = *I;
Dan Gohman572645c2010-02-12 10:34:29 +00002248 const SCEV *NewStride = *NewStrideIter;
Dan Gohman572645c2010-02-12 10:34:29 +00002249
2250 if (SE.getTypeSizeInBits(OldStride->getType()) !=
2251 SE.getTypeSizeInBits(NewStride->getType())) {
2252 if (SE.getTypeSizeInBits(OldStride->getType()) >
2253 SE.getTypeSizeInBits(NewStride->getType()))
2254 NewStride = SE.getSignExtendExpr(NewStride, OldStride->getType());
2255 else
2256 OldStride = SE.getSignExtendExpr(OldStride, NewStride->getType());
2257 }
2258 if (const SCEVConstant *Factor =
Dan Gohmanf09b7122010-02-19 19:35:48 +00002259 dyn_cast_or_null<SCEVConstant>(getExactSDiv(NewStride, OldStride,
2260 SE, true))) {
Dan Gohman572645c2010-02-12 10:34:29 +00002261 if (Factor->getValue()->getValue().getMinSignedBits() <= 64)
2262 Factors.insert(Factor->getValue()->getValue().getSExtValue());
2263 } else if (const SCEVConstant *Factor =
Dan Gohman454d26d2010-02-22 04:11:59 +00002264 dyn_cast_or_null<SCEVConstant>(getExactSDiv(OldStride,
2265 NewStride,
Dan Gohmanf09b7122010-02-19 19:35:48 +00002266 SE, true))) {
Dan Gohman572645c2010-02-12 10:34:29 +00002267 if (Factor->getValue()->getValue().getMinSignedBits() <= 64)
2268 Factors.insert(Factor->getValue()->getValue().getSExtValue());
2269 }
2270 }
Dan Gohman572645c2010-02-12 10:34:29 +00002271
2272 // If all uses use the same type, don't bother looking for truncation-based
2273 // reuse.
2274 if (Types.size() == 1)
2275 Types.clear();
2276
2277 DEBUG(print_factors_and_types(dbgs()));
2278}
2279
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002280/// findIVOperand - Helper for CollectChains that finds an IV operand (computed
2281/// by an AddRec in this loop) within [OI,OE) or returns OE. If IVUsers mapped
2282/// Instructions to IVStrideUses, we could partially skip this.
2283static User::op_iterator
2284findIVOperand(User::op_iterator OI, User::op_iterator OE,
2285 Loop *L, ScalarEvolution &SE) {
2286 for(; OI != OE; ++OI) {
2287 if (Instruction *Oper = dyn_cast<Instruction>(*OI)) {
2288 if (!SE.isSCEVable(Oper->getType()))
2289 continue;
2290
2291 if (const SCEVAddRecExpr *AR =
2292 dyn_cast<SCEVAddRecExpr>(SE.getSCEV(Oper))) {
2293 if (AR->getLoop() == L)
2294 break;
2295 }
2296 }
2297 }
2298 return OI;
2299}
2300
2301/// getWideOperand - IVChain logic must consistenctly peek base TruncInst
2302/// operands, so wrap it in a convenient helper.
2303static Value *getWideOperand(Value *Oper) {
2304 if (TruncInst *Trunc = dyn_cast<TruncInst>(Oper))
2305 return Trunc->getOperand(0);
2306 return Oper;
2307}
2308
2309/// isCompatibleIVType - Return true if we allow an IV chain to include both
2310/// types.
2311static bool isCompatibleIVType(Value *LVal, Value *RVal) {
2312 Type *LType = LVal->getType();
2313 Type *RType = RVal->getType();
2314 return (LType == RType) || (LType->isPointerTy() && RType->isPointerTy());
2315}
2316
Andrew Trick64925c52012-01-10 01:45:08 +00002317/// getExprBase - Return an approximation of this SCEV expression's "base", or
2318/// NULL for any constant. Returning the expression itself is
2319/// conservative. Returning a deeper subexpression is more precise and valid as
2320/// long as it isn't less complex than another subexpression. For expressions
2321/// involving multiple unscaled values, we need to return the pointer-type
2322/// SCEVUnknown. This avoids forming chains across objects, such as:
2323/// PrevOper==a[i], IVOper==b[i], IVInc==b-a.
2324///
2325/// Since SCEVUnknown is the rightmost type, and pointers are the rightmost
2326/// SCEVUnknown, we simply return the rightmost SCEV operand.
2327static const SCEV *getExprBase(const SCEV *S) {
2328 switch (S->getSCEVType()) {
2329 default: // uncluding scUnknown.
2330 return S;
2331 case scConstant:
2332 return 0;
2333 case scTruncate:
2334 return getExprBase(cast<SCEVTruncateExpr>(S)->getOperand());
2335 case scZeroExtend:
2336 return getExprBase(cast<SCEVZeroExtendExpr>(S)->getOperand());
2337 case scSignExtend:
2338 return getExprBase(cast<SCEVSignExtendExpr>(S)->getOperand());
2339 case scAddExpr: {
2340 // Skip over scaled operands (scMulExpr) to follow add operands as long as
2341 // there's nothing more complex.
2342 // FIXME: not sure if we want to recognize negation.
2343 const SCEVAddExpr *Add = cast<SCEVAddExpr>(S);
2344 for (std::reverse_iterator<SCEVAddExpr::op_iterator> I(Add->op_end()),
2345 E(Add->op_begin()); I != E; ++I) {
2346 const SCEV *SubExpr = *I;
2347 if (SubExpr->getSCEVType() == scAddExpr)
2348 return getExprBase(SubExpr);
2349
2350 if (SubExpr->getSCEVType() != scMulExpr)
2351 return SubExpr;
2352 }
2353 return S; // all operands are scaled, be conservative.
2354 }
2355 case scAddRecExpr:
2356 return getExprBase(cast<SCEVAddRecExpr>(S)->getStart());
2357 }
2358}
2359
Andrew Trick22d20c22012-01-09 21:18:52 +00002360/// Return true if the chain increment is profitable to expand into a loop
2361/// invariant value, which may require its own register. A profitable chain
2362/// increment will be an offset relative to the same base. We allow such offsets
2363/// to potentially be used as chain increment as long as it's not obviously
2364/// expensive to expand using real instructions.
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00002365bool IVChain::isProfitableIncrement(const SCEV *OperExpr,
2366 const SCEV *IncExpr,
2367 ScalarEvolution &SE) {
2368 // Aggressively form chains when -stress-ivchain.
Andrew Trick22d20c22012-01-09 21:18:52 +00002369 if (StressIVChain)
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00002370 return true;
Andrew Trick22d20c22012-01-09 21:18:52 +00002371
Andrew Trick64925c52012-01-10 01:45:08 +00002372 // Do not replace a constant offset from IV head with a nonconstant IV
2373 // increment.
2374 if (!isa<SCEVConstant>(IncExpr)) {
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00002375 const SCEV *HeadExpr = SE.getSCEV(getWideOperand(Incs[0].IVOperand));
Andrew Trick64925c52012-01-10 01:45:08 +00002376 if (isa<SCEVConstant>(SE.getMinusSCEV(OperExpr, HeadExpr)))
2377 return 0;
2378 }
2379
2380 SmallPtrSet<const SCEV*, 8> Processed;
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00002381 return !isHighCostExpansion(IncExpr, Processed, SE);
Andrew Trick22d20c22012-01-09 21:18:52 +00002382}
2383
2384/// Return true if the number of registers needed for the chain is estimated to
2385/// be less than the number required for the individual IV users. First prohibit
2386/// any IV users that keep the IV live across increments (the Users set should
2387/// be empty). Next count the number and type of increments in the chain.
2388///
2389/// Chaining IVs can lead to considerable code bloat if ISEL doesn't
2390/// effectively use postinc addressing modes. Only consider it profitable it the
2391/// increments can be computed in fewer registers when chained.
2392///
2393/// TODO: Consider IVInc free if it's already used in another chains.
2394static bool
2395isProfitableChain(IVChain &Chain, SmallPtrSet<Instruction*, 4> &Users,
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00002396 ScalarEvolution &SE, const TargetTransformInfo &TTI) {
Andrew Trick22d20c22012-01-09 21:18:52 +00002397 if (StressIVChain)
2398 return true;
2399
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00002400 if (!Chain.hasIncs())
Andrew Trick64925c52012-01-10 01:45:08 +00002401 return false;
2402
2403 if (!Users.empty()) {
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00002404 DEBUG(dbgs() << "Chain: " << *Chain.Incs[0].UserInst << " users:\n";
Andrew Trick64925c52012-01-10 01:45:08 +00002405 for (SmallPtrSet<Instruction*, 4>::const_iterator I = Users.begin(),
2406 E = Users.end(); I != E; ++I) {
2407 dbgs() << " " << **I << "\n";
2408 });
2409 return false;
2410 }
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00002411 assert(!Chain.Incs.empty() && "empty IV chains are not allowed");
Andrew Trick64925c52012-01-10 01:45:08 +00002412
2413 // The chain itself may require a register, so intialize cost to 1.
2414 int cost = 1;
2415
2416 // A complete chain likely eliminates the need for keeping the original IV in
2417 // a register. LSR does not currently know how to form a complete chain unless
2418 // the header phi already exists.
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00002419 if (isa<PHINode>(Chain.tailUserInst())
2420 && SE.getSCEV(Chain.tailUserInst()) == Chain.Incs[0].IncExpr) {
Andrew Trick64925c52012-01-10 01:45:08 +00002421 --cost;
2422 }
2423 const SCEV *LastIncExpr = 0;
2424 unsigned NumConstIncrements = 0;
2425 unsigned NumVarIncrements = 0;
2426 unsigned NumReusedIncrements = 0;
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00002427 for (IVChain::const_iterator I = Chain.begin(), E = Chain.end();
Andrew Trick64925c52012-01-10 01:45:08 +00002428 I != E; ++I) {
2429
2430 if (I->IncExpr->isZero())
2431 continue;
2432
2433 // Incrementing by zero or some constant is neutral. We assume constants can
2434 // be folded into an addressing mode or an add's immediate operand.
2435 if (isa<SCEVConstant>(I->IncExpr)) {
2436 ++NumConstIncrements;
2437 continue;
2438 }
2439
2440 if (I->IncExpr == LastIncExpr)
2441 ++NumReusedIncrements;
2442 else
2443 ++NumVarIncrements;
2444
2445 LastIncExpr = I->IncExpr;
2446 }
2447 // An IV chain with a single increment is handled by LSR's postinc
2448 // uses. However, a chain with multiple increments requires keeping the IV's
2449 // value live longer than it needs to be if chained.
2450 if (NumConstIncrements > 1)
2451 --cost;
2452
2453 // Materializing increment expressions in the preheader that didn't exist in
2454 // the original code may cost a register. For example, sign-extended array
2455 // indices can produce ridiculous increments like this:
2456 // IV + ((sext i32 (2 * %s) to i64) + (-1 * (sext i32 %s to i64)))
2457 cost += NumVarIncrements;
2458
2459 // Reusing variable increments likely saves a register to hold the multiple of
2460 // the stride.
2461 cost -= NumReusedIncrements;
2462
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00002463 DEBUG(dbgs() << "Chain: " << *Chain.Incs[0].UserInst << " Cost: " << cost
2464 << "\n");
Andrew Trick64925c52012-01-10 01:45:08 +00002465
2466 return cost < 0;
Andrew Trick22d20c22012-01-09 21:18:52 +00002467}
2468
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002469/// ChainInstruction - Add this IV user to an existing chain or make it the head
2470/// of a new chain.
2471void LSRInstance::ChainInstruction(Instruction *UserInst, Instruction *IVOper,
2472 SmallVectorImpl<ChainUsers> &ChainUsersVec) {
2473 // When IVs are used as types of varying widths, they are generally converted
2474 // to a wider type with some uses remaining narrow under a (free) trunc.
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00002475 Value *const NextIV = getWideOperand(IVOper);
2476 const SCEV *const OperExpr = SE.getSCEV(NextIV);
2477 const SCEV *const OperExprBase = getExprBase(OperExpr);
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002478
2479 // Visit all existing chains. Check if its IVOper can be computed as a
2480 // profitable loop invariant increment from the last link in the Chain.
2481 unsigned ChainIdx = 0, NChains = IVChainVec.size();
2482 const SCEV *LastIncExpr = 0;
2483 for (; ChainIdx < NChains; ++ChainIdx) {
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00002484 IVChain &Chain = IVChainVec[ChainIdx];
2485
2486 // Prune the solution space aggressively by checking that both IV operands
2487 // are expressions that operate on the same unscaled SCEVUnknown. This
2488 // "base" will be canceled by the subsequent getMinusSCEV call. Checking
2489 // first avoids creating extra SCEV expressions.
2490 if (!StressIVChain && Chain.ExprBase != OperExprBase)
2491 continue;
2492
2493 Value *PrevIV = getWideOperand(Chain.Incs.back().IVOperand);
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002494 if (!isCompatibleIVType(PrevIV, NextIV))
2495 continue;
2496
Andrew Trickd4e46a62012-03-26 20:28:35 +00002497 // A phi node terminates a chain.
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00002498 if (isa<PHINode>(UserInst) && isa<PHINode>(Chain.tailUserInst()))
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002499 continue;
2500
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00002501 // The increment must be loop-invariant so it can be kept in a register.
2502 const SCEV *PrevExpr = SE.getSCEV(PrevIV);
2503 const SCEV *IncExpr = SE.getMinusSCEV(OperExpr, PrevExpr);
2504 if (!SE.isLoopInvariant(IncExpr, L))
2505 continue;
2506
2507 if (Chain.isProfitableIncrement(OperExpr, IncExpr, SE)) {
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002508 LastIncExpr = IncExpr;
2509 break;
2510 }
2511 }
2512 // If we haven't found a chain, create a new one, unless we hit the max. Don't
2513 // bother for phi nodes, because they must be last in the chain.
2514 if (ChainIdx == NChains) {
2515 if (isa<PHINode>(UserInst))
2516 return;
Andrew Trick22d20c22012-01-09 21:18:52 +00002517 if (NChains >= MaxChains && !StressIVChain) {
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002518 DEBUG(dbgs() << "IV Chain Limit\n");
2519 return;
2520 }
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00002521 LastIncExpr = OperExpr;
Andrew Trick0041d4d2012-01-20 21:23:40 +00002522 // IVUsers may have skipped over sign/zero extensions. We don't currently
2523 // attempt to form chains involving extensions unless they can be hoisted
2524 // into this loop's AddRec.
2525 if (!isa<SCEVAddRecExpr>(LastIncExpr))
2526 return;
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002527 ++NChains;
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00002528 IVChainVec.push_back(IVChain(IVInc(UserInst, IVOper, LastIncExpr),
2529 OperExprBase));
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002530 ChainUsersVec.resize(NChains);
Jakob Stoklund Olesen165324c2012-04-25 18:01:32 +00002531 DEBUG(dbgs() << "IV Chain#" << ChainIdx << " Head: (" << *UserInst
2532 << ") IV=" << *LastIncExpr << "\n");
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00002533 } else {
Jakob Stoklund Olesen165324c2012-04-25 18:01:32 +00002534 DEBUG(dbgs() << "IV Chain#" << ChainIdx << " Inc: (" << *UserInst
2535 << ") IV+" << *LastIncExpr << "\n");
Jakob Stoklund Olesenf9f1c7a2012-04-26 23:33:11 +00002536 // Add this IV user to the end of the chain.
2537 IVChainVec[ChainIdx].add(IVInc(UserInst, IVOper, LastIncExpr));
2538 }
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002539
2540 SmallPtrSet<Instruction*,4> &NearUsers = ChainUsersVec[ChainIdx].NearUsers;
2541 // This chain's NearUsers become FarUsers.
2542 if (!LastIncExpr->isZero()) {
2543 ChainUsersVec[ChainIdx].FarUsers.insert(NearUsers.begin(),
2544 NearUsers.end());
2545 NearUsers.clear();
2546 }
2547
2548 // All other uses of IVOperand become near uses of the chain.
2549 // We currently ignore intermediate values within SCEV expressions, assuming
2550 // they will eventually be used be the current chain, or can be computed
2551 // from one of the chain increments. To be more precise we could
2552 // transitively follow its user and only add leaf IV users to the set.
2553 for (Value::use_iterator UseIter = IVOper->use_begin(),
2554 UseEnd = IVOper->use_end(); UseIter != UseEnd; ++UseIter) {
2555 Instruction *OtherUse = dyn_cast<Instruction>(*UseIter);
Andrew Trick81748bc2012-03-26 18:03:16 +00002556 if (!OtherUse || OtherUse == UserInst)
2557 continue;
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002558 if (SE.isSCEVable(OtherUse->getType())
2559 && !isa<SCEVUnknown>(SE.getSCEV(OtherUse))
2560 && IU.isIVUserOrOperand(OtherUse)) {
2561 continue;
2562 }
Andrew Trick81748bc2012-03-26 18:03:16 +00002563 NearUsers.insert(OtherUse);
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002564 }
2565
2566 // Since this user is part of the chain, it's no longer considered a use
2567 // of the chain.
2568 ChainUsersVec[ChainIdx].FarUsers.erase(UserInst);
2569}
2570
2571/// CollectChains - Populate the vector of Chains.
2572///
2573/// This decreases ILP at the architecture level. Targets with ample registers,
2574/// multiple memory ports, and no register renaming probably don't want
2575/// this. However, such targets should probably disable LSR altogether.
2576///
2577/// The job of LSR is to make a reasonable choice of induction variables across
2578/// the loop. Subsequent passes can easily "unchain" computation exposing more
2579/// ILP *within the loop* if the target wants it.
2580///
2581/// Finding the best IV chain is potentially a scheduling problem. Since LSR
2582/// will not reorder memory operations, it will recognize this as a chain, but
2583/// will generate redundant IV increments. Ideally this would be corrected later
2584/// by a smart scheduler:
2585/// = A[i]
2586/// = A[i+x]
2587/// A[i] =
2588/// A[i+x] =
2589///
2590/// TODO: Walk the entire domtree within this loop, not just the path to the
2591/// loop latch. This will discover chains on side paths, but requires
2592/// maintaining multiple copies of the Chains state.
2593void LSRInstance::CollectChains() {
Jakob Stoklund Olesen165324c2012-04-25 18:01:32 +00002594 DEBUG(dbgs() << "Collecting IV Chains.\n");
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002595 SmallVector<ChainUsers, 8> ChainUsersVec;
2596
2597 SmallVector<BasicBlock *,8> LatchPath;
2598 BasicBlock *LoopHeader = L->getHeader();
2599 for (DomTreeNode *Rung = DT.getNode(L->getLoopLatch());
2600 Rung->getBlock() != LoopHeader; Rung = Rung->getIDom()) {
2601 LatchPath.push_back(Rung->getBlock());
2602 }
2603 LatchPath.push_back(LoopHeader);
2604
2605 // Walk the instruction stream from the loop header to the loop latch.
2606 for (SmallVectorImpl<BasicBlock *>::reverse_iterator
2607 BBIter = LatchPath.rbegin(), BBEnd = LatchPath.rend();
2608 BBIter != BBEnd; ++BBIter) {
2609 for (BasicBlock::iterator I = (*BBIter)->begin(), E = (*BBIter)->end();
2610 I != E; ++I) {
2611 // Skip instructions that weren't seen by IVUsers analysis.
2612 if (isa<PHINode>(I) || !IU.isIVUserOrOperand(I))
2613 continue;
2614
2615 // Ignore users that are part of a SCEV expression. This way we only
2616 // consider leaf IV Users. This effectively rediscovers a portion of
2617 // IVUsers analysis but in program order this time.
2618 if (SE.isSCEVable(I->getType()) && !isa<SCEVUnknown>(SE.getSCEV(I)))
2619 continue;
2620
2621 // Remove this instruction from any NearUsers set it may be in.
2622 for (unsigned ChainIdx = 0, NChains = IVChainVec.size();
2623 ChainIdx < NChains; ++ChainIdx) {
2624 ChainUsersVec[ChainIdx].NearUsers.erase(I);
2625 }
2626 // Search for operands that can be chained.
2627 SmallPtrSet<Instruction*, 4> UniqueOperands;
2628 User::op_iterator IVOpEnd = I->op_end();
2629 User::op_iterator IVOpIter = findIVOperand(I->op_begin(), IVOpEnd, L, SE);
2630 while (IVOpIter != IVOpEnd) {
2631 Instruction *IVOpInst = cast<Instruction>(*IVOpIter);
2632 if (UniqueOperands.insert(IVOpInst))
2633 ChainInstruction(I, IVOpInst, ChainUsersVec);
2634 IVOpIter = findIVOperand(llvm::next(IVOpIter), IVOpEnd, L, SE);
2635 }
2636 } // Continue walking down the instructions.
2637 } // Continue walking down the domtree.
2638 // Visit phi backedges to determine if the chain can generate the IV postinc.
2639 for (BasicBlock::iterator I = L->getHeader()->begin();
2640 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
2641 if (!SE.isSCEVable(PN->getType()))
2642 continue;
2643
2644 Instruction *IncV =
2645 dyn_cast<Instruction>(PN->getIncomingValueForBlock(L->getLoopLatch()));
2646 if (IncV)
2647 ChainInstruction(PN, IncV, ChainUsersVec);
2648 }
Andrew Trick22d20c22012-01-09 21:18:52 +00002649 // Remove any unprofitable chains.
2650 unsigned ChainIdx = 0;
2651 for (unsigned UsersIdx = 0, NChains = IVChainVec.size();
2652 UsersIdx < NChains; ++UsersIdx) {
2653 if (!isProfitableChain(IVChainVec[UsersIdx],
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00002654 ChainUsersVec[UsersIdx].FarUsers, SE, TTI))
Andrew Trick22d20c22012-01-09 21:18:52 +00002655 continue;
2656 // Preserve the chain at UsesIdx.
2657 if (ChainIdx != UsersIdx)
2658 IVChainVec[ChainIdx] = IVChainVec[UsersIdx];
2659 FinalizeChain(IVChainVec[ChainIdx]);
2660 ++ChainIdx;
2661 }
2662 IVChainVec.resize(ChainIdx);
2663}
2664
2665void LSRInstance::FinalizeChain(IVChain &Chain) {
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00002666 assert(!Chain.Incs.empty() && "empty IV chains are not allowed");
2667 DEBUG(dbgs() << "Final Chain: " << *Chain.Incs[0].UserInst << "\n");
Andrew Trick22d20c22012-01-09 21:18:52 +00002668
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00002669 for (IVChain::const_iterator I = Chain.begin(), E = Chain.end();
Andrew Trick22d20c22012-01-09 21:18:52 +00002670 I != E; ++I) {
2671 DEBUG(dbgs() << " Inc: " << *I->UserInst << "\n");
2672 User::op_iterator UseI =
2673 std::find(I->UserInst->op_begin(), I->UserInst->op_end(), I->IVOperand);
2674 assert(UseI != I->UserInst->op_end() && "cannot find IV operand");
2675 IVIncSet.insert(UseI);
2676 }
2677}
2678
2679/// Return true if the IVInc can be folded into an addressing mode.
2680static bool canFoldIVIncExpr(const SCEV *IncExpr, Instruction *UserInst,
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00002681 Value *Operand, const TargetTransformInfo &TTI) {
Andrew Trick22d20c22012-01-09 21:18:52 +00002682 const SCEVConstant *IncConst = dyn_cast<SCEVConstant>(IncExpr);
2683 if (!IncConst || !isAddressUse(UserInst, Operand))
2684 return false;
2685
2686 if (IncConst->getValue()->getValue().getMinSignedBits() > 64)
2687 return false;
2688
2689 int64_t IncOffset = IncConst->getValue()->getSExtValue();
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00002690 if (!isAlwaysFoldable(TTI, LSRUse::Address,
2691 getAccessType(UserInst), /*BaseGV=*/ 0,
2692 IncOffset, /*HaseBaseReg=*/ false))
Andrew Trick22d20c22012-01-09 21:18:52 +00002693 return false;
2694
2695 return true;
2696}
2697
2698/// GenerateIVChains - Generate an add or subtract for each IVInc in a chain to
2699/// materialize the IV user's operand from the previous IV user's operand.
2700void LSRInstance::GenerateIVChain(const IVChain &Chain, SCEVExpander &Rewriter,
2701 SmallVectorImpl<WeakVH> &DeadInsts) {
2702 // Find the new IVOperand for the head of the chain. It may have been replaced
2703 // by LSR.
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00002704 const IVInc &Head = Chain.Incs[0];
Andrew Trick22d20c22012-01-09 21:18:52 +00002705 User::op_iterator IVOpEnd = Head.UserInst->op_end();
2706 User::op_iterator IVOpIter = findIVOperand(Head.UserInst->op_begin(),
2707 IVOpEnd, L, SE);
2708 Value *IVSrc = 0;
2709 while (IVOpIter != IVOpEnd) {
2710 IVSrc = getWideOperand(*IVOpIter);
2711
2712 // If this operand computes the expression that the chain needs, we may use
2713 // it. (Check this after setting IVSrc which is used below.)
2714 //
2715 // Note that if Head.IncExpr is wider than IVSrc, then this phi is too
2716 // narrow for the chain, so we can no longer use it. We do allow using a
2717 // wider phi, assuming the LSR checked for free truncation. In that case we
2718 // should already have a truncate on this operand such that
2719 // getSCEV(IVSrc) == IncExpr.
2720 if (SE.getSCEV(*IVOpIter) == Head.IncExpr
2721 || SE.getSCEV(IVSrc) == Head.IncExpr) {
2722 break;
2723 }
2724 IVOpIter = findIVOperand(llvm::next(IVOpIter), IVOpEnd, L, SE);
2725 }
2726 if (IVOpIter == IVOpEnd) {
2727 // Gracefully give up on this chain.
2728 DEBUG(dbgs() << "Concealed chain head: " << *Head.UserInst << "\n");
2729 return;
2730 }
2731
2732 DEBUG(dbgs() << "Generate chain at: " << *IVSrc << "\n");
2733 Type *IVTy = IVSrc->getType();
2734 Type *IntTy = SE.getEffectiveSCEVType(IVTy);
2735 const SCEV *LeftOverExpr = 0;
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00002736 for (IVChain::const_iterator IncI = Chain.begin(),
Andrew Trick22d20c22012-01-09 21:18:52 +00002737 IncE = Chain.end(); IncI != IncE; ++IncI) {
2738
2739 Instruction *InsertPt = IncI->UserInst;
2740 if (isa<PHINode>(InsertPt))
2741 InsertPt = L->getLoopLatch()->getTerminator();
2742
2743 // IVOper will replace the current IV User's operand. IVSrc is the IV
2744 // value currently held in a register.
2745 Value *IVOper = IVSrc;
2746 if (!IncI->IncExpr->isZero()) {
2747 // IncExpr was the result of subtraction of two narrow values, so must
2748 // be signed.
2749 const SCEV *IncExpr = SE.getNoopOrSignExtend(IncI->IncExpr, IntTy);
2750 LeftOverExpr = LeftOverExpr ?
2751 SE.getAddExpr(LeftOverExpr, IncExpr) : IncExpr;
2752 }
2753 if (LeftOverExpr && !LeftOverExpr->isZero()) {
2754 // Expand the IV increment.
2755 Rewriter.clearPostInc();
2756 Value *IncV = Rewriter.expandCodeFor(LeftOverExpr, IntTy, InsertPt);
2757 const SCEV *IVOperExpr = SE.getAddExpr(SE.getUnknown(IVSrc),
2758 SE.getUnknown(IncV));
2759 IVOper = Rewriter.expandCodeFor(IVOperExpr, IVTy, InsertPt);
2760
2761 // If an IV increment can't be folded, use it as the next IV value.
2762 if (!canFoldIVIncExpr(LeftOverExpr, IncI->UserInst, IncI->IVOperand,
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00002763 TTI)) {
Andrew Trick22d20c22012-01-09 21:18:52 +00002764 assert(IVTy == IVOper->getType() && "inconsistent IV increment type");
2765 IVSrc = IVOper;
2766 LeftOverExpr = 0;
2767 }
2768 }
2769 Type *OperTy = IncI->IVOperand->getType();
2770 if (IVTy != OperTy) {
2771 assert(SE.getTypeSizeInBits(IVTy) >= SE.getTypeSizeInBits(OperTy) &&
2772 "cannot extend a chained IV");
2773 IRBuilder<> Builder(InsertPt);
2774 IVOper = Builder.CreateTruncOrBitCast(IVOper, OperTy, "lsr.chain");
2775 }
2776 IncI->UserInst->replaceUsesOfWith(IncI->IVOperand, IVOper);
2777 DeadInsts.push_back(IncI->IVOperand);
2778 }
2779 // If LSR created a new, wider phi, we may also replace its postinc. We only
2780 // do this if we also found a wide value for the head of the chain.
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00002781 if (isa<PHINode>(Chain.tailUserInst())) {
Andrew Trick22d20c22012-01-09 21:18:52 +00002782 for (BasicBlock::iterator I = L->getHeader()->begin();
2783 PHINode *Phi = dyn_cast<PHINode>(I); ++I) {
2784 if (!isCompatibleIVType(Phi, IVSrc))
2785 continue;
2786 Instruction *PostIncV = dyn_cast<Instruction>(
2787 Phi->getIncomingValueForBlock(L->getLoopLatch()));
2788 if (!PostIncV || (SE.getSCEV(PostIncV) != SE.getSCEV(IVSrc)))
2789 continue;
2790 Value *IVOper = IVSrc;
2791 Type *PostIncTy = PostIncV->getType();
2792 if (IVTy != PostIncTy) {
2793 assert(PostIncTy->isPointerTy() && "mixing int/ptr IV types");
2794 IRBuilder<> Builder(L->getLoopLatch()->getTerminator());
2795 Builder.SetCurrentDebugLocation(PostIncV->getDebugLoc());
2796 IVOper = Builder.CreatePointerCast(IVSrc, PostIncTy, "lsr.chain");
2797 }
2798 Phi->replaceUsesOfWith(PostIncV, IVOper);
2799 DeadInsts.push_back(PostIncV);
2800 }
2801 }
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00002802}
2803
Dan Gohman572645c2010-02-12 10:34:29 +00002804void LSRInstance::CollectFixupsAndInitialFormulae() {
2805 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) {
Andrew Trick22d20c22012-01-09 21:18:52 +00002806 Instruction *UserInst = UI->getUser();
2807 // Skip IV users that are part of profitable IV Chains.
2808 User::op_iterator UseI = std::find(UserInst->op_begin(), UserInst->op_end(),
2809 UI->getOperandValToReplace());
2810 assert(UseI != UserInst->op_end() && "cannot find IV operand");
2811 if (IVIncSet.count(UseI))
2812 continue;
2813
Dan Gohman572645c2010-02-12 10:34:29 +00002814 // Record the uses.
2815 LSRFixup &LF = getNewFixup();
Andrew Trick22d20c22012-01-09 21:18:52 +00002816 LF.UserInst = UserInst;
Dan Gohman572645c2010-02-12 10:34:29 +00002817 LF.OperandValToReplace = UI->getOperandValToReplace();
Dan Gohman448db1c2010-04-07 22:27:08 +00002818 LF.PostIncLoops = UI->getPostIncLoops();
Dan Gohman572645c2010-02-12 10:34:29 +00002819
2820 LSRUse::KindType Kind = LSRUse::Basic;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002821 Type *AccessTy = 0;
Dan Gohman572645c2010-02-12 10:34:29 +00002822 if (isAddressUse(LF.UserInst, LF.OperandValToReplace)) {
2823 Kind = LSRUse::Address;
2824 AccessTy = getAccessType(LF.UserInst);
2825 }
2826
Dan Gohmanc0564542010-04-19 21:48:58 +00002827 const SCEV *S = IU.getExpr(*UI);
Dan Gohman572645c2010-02-12 10:34:29 +00002828
2829 // Equality (== and !=) ICmps are special. We can rewrite (i == N) as
2830 // (N - i == 0), and this allows (N - i) to be the expression that we work
2831 // with rather than just N or i, so we can consider the register
2832 // requirements for both N and i at the same time. Limiting this code to
2833 // equality icmps is not a problem because all interesting loops use
2834 // equality icmps, thanks to IndVarSimplify.
2835 if (ICmpInst *CI = dyn_cast<ICmpInst>(LF.UserInst))
2836 if (CI->isEquality()) {
2837 // Swap the operands if needed to put the OperandValToReplace on the
2838 // left, for consistency.
2839 Value *NV = CI->getOperand(1);
2840 if (NV == LF.OperandValToReplace) {
2841 CI->setOperand(1, CI->getOperand(0));
2842 CI->setOperand(0, NV);
Dan Gohmanf182b232010-05-20 19:26:52 +00002843 NV = CI->getOperand(1);
Dan Gohman9da1bf42010-05-20 19:16:03 +00002844 Changed = true;
Dan Gohman572645c2010-02-12 10:34:29 +00002845 }
2846
2847 // x == y --> x - y == 0
2848 const SCEV *N = SE.getSCEV(NV);
Andrew Tricke08c3222012-07-13 23:33:10 +00002849 if (SE.isLoopInvariant(N, L) && isSafeToExpand(N)) {
Dan Gohman673968a2011-05-18 21:02:18 +00002850 // S is normalized, so normalize N before folding it into S
2851 // to keep the result normalized.
2852 N = TransformForPostIncUse(Normalize, N, CI, 0,
2853 LF.PostIncLoops, SE, DT);
Dan Gohman572645c2010-02-12 10:34:29 +00002854 Kind = LSRUse::ICmpZero;
2855 S = SE.getMinusSCEV(N, S);
2856 }
2857
2858 // -1 and the negations of all interesting strides (except the negation
2859 // of -1) are now also interesting.
2860 for (size_t i = 0, e = Factors.size(); i != e; ++i)
2861 if (Factors[i] != -1)
2862 Factors.insert(-(uint64_t)Factors[i]);
2863 Factors.insert(-1);
2864 }
2865
2866 // Set up the initial formula for this use.
2867 std::pair<size_t, int64_t> P = getUse(S, Kind, AccessTy);
2868 LF.LUIdx = P.first;
2869 LF.Offset = P.second;
2870 LSRUse &LU = Uses[LF.LUIdx];
Dan Gohman448db1c2010-04-07 22:27:08 +00002871 LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L);
Dan Gohmana9db1292010-07-15 20:24:58 +00002872 if (!LU.WidestFixupType ||
2873 SE.getTypeSizeInBits(LU.WidestFixupType) <
2874 SE.getTypeSizeInBits(LF.OperandValToReplace->getType()))
2875 LU.WidestFixupType = LF.OperandValToReplace->getType();
Dan Gohman572645c2010-02-12 10:34:29 +00002876
2877 // If this is the first use of this LSRUse, give it a formula.
2878 if (LU.Formulae.empty()) {
Dan Gohman454d26d2010-02-22 04:11:59 +00002879 InsertInitialFormula(S, LU, LF.LUIdx);
Dan Gohman572645c2010-02-12 10:34:29 +00002880 CountRegisters(LU.Formulae.back(), LF.LUIdx);
2881 }
2882 }
2883
2884 DEBUG(print_fixups(dbgs()));
2885}
2886
Dan Gohman76c315a2010-05-20 20:52:00 +00002887/// InsertInitialFormula - Insert a formula for the given expression into
2888/// the given use, separating out loop-variant portions from loop-invariant
2889/// and loop-computable portions.
Dan Gohman572645c2010-02-12 10:34:29 +00002890void
Dan Gohman454d26d2010-02-22 04:11:59 +00002891LSRInstance::InsertInitialFormula(const SCEV *S, LSRUse &LU, size_t LUIdx) {
Dan Gohman572645c2010-02-12 10:34:29 +00002892 Formula F;
Dan Gohmandc0e8fb2010-11-17 21:41:58 +00002893 F.InitialMatch(S, L, SE);
Dan Gohman572645c2010-02-12 10:34:29 +00002894 bool Inserted = InsertFormula(LU, LUIdx, F);
2895 assert(Inserted && "Initial formula already exists!"); (void)Inserted;
2896}
2897
Dan Gohman76c315a2010-05-20 20:52:00 +00002898/// InsertSupplementalFormula - Insert a simple single-register formula for
2899/// the given expression into the given use.
Dan Gohman572645c2010-02-12 10:34:29 +00002900void
2901LSRInstance::InsertSupplementalFormula(const SCEV *S,
2902 LSRUse &LU, size_t LUIdx) {
2903 Formula F;
2904 F.BaseRegs.push_back(S);
Chandler Carrutheab0ba02013-01-12 23:46:04 +00002905 F.HasBaseReg = true;
Dan Gohman572645c2010-02-12 10:34:29 +00002906 bool Inserted = InsertFormula(LU, LUIdx, F);
2907 assert(Inserted && "Supplemental formula already exists!"); (void)Inserted;
2908}
2909
2910/// CountRegisters - Note which registers are used by the given formula,
2911/// updating RegUses.
2912void LSRInstance::CountRegisters(const Formula &F, size_t LUIdx) {
2913 if (F.ScaledReg)
2914 RegUses.CountRegister(F.ScaledReg, LUIdx);
2915 for (SmallVectorImpl<const SCEV *>::const_iterator I = F.BaseRegs.begin(),
2916 E = F.BaseRegs.end(); I != E; ++I)
2917 RegUses.CountRegister(*I, LUIdx);
2918}
2919
2920/// InsertFormula - If the given formula has not yet been inserted, add it to
2921/// the list, and return true. Return false otherwise.
2922bool LSRInstance::InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F) {
Dan Gohman454d26d2010-02-22 04:11:59 +00002923 if (!LU.InsertFormula(F))
Dan Gohman572645c2010-02-12 10:34:29 +00002924 return false;
2925
2926 CountRegisters(F, LUIdx);
2927 return true;
2928}
2929
2930/// CollectLoopInvariantFixupsAndFormulae - Check for other uses of
2931/// loop-invariant values which we're tracking. These other uses will pin these
2932/// values in registers, making them less profitable for elimination.
2933/// TODO: This currently misses non-constant addrec step registers.
2934/// TODO: Should this give more weight to users inside the loop?
2935void
2936LSRInstance::CollectLoopInvariantFixupsAndFormulae() {
2937 SmallVector<const SCEV *, 8> Worklist(RegUses.begin(), RegUses.end());
2938 SmallPtrSet<const SCEV *, 8> Inserted;
2939
2940 while (!Worklist.empty()) {
2941 const SCEV *S = Worklist.pop_back_val();
2942
2943 if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S))
Dan Gohman403a8cd2010-06-21 19:47:52 +00002944 Worklist.append(N->op_begin(), N->op_end());
Dan Gohman572645c2010-02-12 10:34:29 +00002945 else if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S))
2946 Worklist.push_back(C->getOperand());
2947 else if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) {
2948 Worklist.push_back(D->getLHS());
2949 Worklist.push_back(D->getRHS());
2950 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2951 if (!Inserted.insert(U)) continue;
2952 const Value *V = U->getValue();
Dan Gohmana15ec5d2010-06-04 23:16:05 +00002953 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
2954 // Look for instructions defined outside the loop.
Dan Gohman572645c2010-02-12 10:34:29 +00002955 if (L->contains(Inst)) continue;
Dan Gohmana15ec5d2010-06-04 23:16:05 +00002956 } else if (isa<UndefValue>(V))
2957 // Undef doesn't have a live range, so it doesn't matter.
2958 continue;
Gabor Greif60ad7812010-03-25 23:06:16 +00002959 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
Dan Gohman572645c2010-02-12 10:34:29 +00002960 UI != UE; ++UI) {
2961 const Instruction *UserInst = dyn_cast<Instruction>(*UI);
2962 // Ignore non-instructions.
2963 if (!UserInst)
Dan Gohman7979b722010-01-22 00:46:49 +00002964 continue;
Dan Gohman572645c2010-02-12 10:34:29 +00002965 // Ignore instructions in other functions (as can happen with
2966 // Constants).
2967 if (UserInst->getParent()->getParent() != L->getHeader()->getParent())
Dan Gohman7979b722010-01-22 00:46:49 +00002968 continue;
Dan Gohman572645c2010-02-12 10:34:29 +00002969 // Ignore instructions not dominated by the loop.
2970 const BasicBlock *UseBB = !isa<PHINode>(UserInst) ?
2971 UserInst->getParent() :
2972 cast<PHINode>(UserInst)->getIncomingBlock(
2973 PHINode::getIncomingValueNumForOperand(UI.getOperandNo()));
2974 if (!DT.dominates(L->getHeader(), UseBB))
2975 continue;
2976 // Ignore uses which are part of other SCEV expressions, to avoid
2977 // analyzing them multiple times.
Dan Gohman4a2a6832010-04-09 19:12:34 +00002978 if (SE.isSCEVable(UserInst->getType())) {
2979 const SCEV *UserS = SE.getSCEV(const_cast<Instruction *>(UserInst));
2980 // If the user is a no-op, look through to its uses.
2981 if (!isa<SCEVUnknown>(UserS))
2982 continue;
2983 if (UserS == U) {
2984 Worklist.push_back(
2985 SE.getUnknown(const_cast<Instruction *>(UserInst)));
2986 continue;
2987 }
2988 }
Dan Gohman572645c2010-02-12 10:34:29 +00002989 // Ignore icmp instructions which are already being analyzed.
2990 if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UserInst)) {
2991 unsigned OtherIdx = !UI.getOperandNo();
2992 Value *OtherOp = const_cast<Value *>(ICI->getOperand(OtherIdx));
Dan Gohman17ead4f2010-11-17 21:23:15 +00002993 if (SE.hasComputableLoopEvolution(SE.getSCEV(OtherOp), L))
Dan Gohman572645c2010-02-12 10:34:29 +00002994 continue;
2995 }
2996
2997 LSRFixup &LF = getNewFixup();
2998 LF.UserInst = const_cast<Instruction *>(UserInst);
2999 LF.OperandValToReplace = UI.getUse();
3000 std::pair<size_t, int64_t> P = getUse(S, LSRUse::Basic, 0);
3001 LF.LUIdx = P.first;
3002 LF.Offset = P.second;
3003 LSRUse &LU = Uses[LF.LUIdx];
Dan Gohman448db1c2010-04-07 22:27:08 +00003004 LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L);
Dan Gohmana9db1292010-07-15 20:24:58 +00003005 if (!LU.WidestFixupType ||
3006 SE.getTypeSizeInBits(LU.WidestFixupType) <
3007 SE.getTypeSizeInBits(LF.OperandValToReplace->getType()))
3008 LU.WidestFixupType = LF.OperandValToReplace->getType();
Dan Gohman572645c2010-02-12 10:34:29 +00003009 InsertSupplementalFormula(U, LU, LF.LUIdx);
3010 CountRegisters(LU.Formulae.back(), Uses.size() - 1);
3011 break;
3012 }
3013 }
3014 }
3015}
3016
3017/// CollectSubexprs - Split S into subexpressions which can be pulled out into
3018/// separate registers. If C is non-null, multiply each subexpression by C.
Andrew Trick06a27cc2012-07-17 05:30:37 +00003019///
3020/// Return remainder expression after factoring the subexpressions captured by
3021/// Ops. If Ops is complete, return NULL.
3022static const SCEV *CollectSubexprs(const SCEV *S, const SCEVConstant *C,
3023 SmallVectorImpl<const SCEV *> &Ops,
3024 const Loop *L,
3025 ScalarEvolution &SE,
3026 unsigned Depth = 0) {
3027 // Arbitrarily cap recursion to protect compile time.
3028 if (Depth >= 3)
3029 return S;
3030
Dan Gohman572645c2010-02-12 10:34:29 +00003031 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
3032 // Break out add operands.
3033 for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
Andrew Trick06a27cc2012-07-17 05:30:37 +00003034 I != E; ++I) {
3035 const SCEV *Remainder = CollectSubexprs(*I, C, Ops, L, SE, Depth+1);
3036 if (Remainder)
3037 Ops.push_back(C ? SE.getMulExpr(C, Remainder) : Remainder);
3038 }
3039 return NULL;
Dan Gohman572645c2010-02-12 10:34:29 +00003040 } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
3041 // Split a non-zero base out of an addrec.
Andrew Trick06a27cc2012-07-17 05:30:37 +00003042 if (AR->getStart()->isZero())
3043 return S;
3044
3045 const SCEV *Remainder = CollectSubexprs(AR->getStart(),
3046 C, Ops, L, SE, Depth+1);
3047 // Split the non-zero AddRec unless it is part of a nested recurrence that
3048 // does not pertain to this loop.
3049 if (Remainder && (AR->getLoop() == L || !isa<SCEVAddRecExpr>(Remainder))) {
3050 Ops.push_back(C ? SE.getMulExpr(C, Remainder) : Remainder);
3051 Remainder = NULL;
3052 }
3053 if (Remainder != AR->getStart()) {
3054 if (!Remainder)
3055 Remainder = SE.getConstant(AR->getType(), 0);
3056 return SE.getAddRecExpr(Remainder,
3057 AR->getStepRecurrence(SE),
3058 AR->getLoop(),
3059 //FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
3060 SCEV::FlagAnyWrap);
Dan Gohman572645c2010-02-12 10:34:29 +00003061 }
3062 } else if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
3063 // Break (C * (a + b + c)) into C*a + C*b + C*c.
Andrew Trick06a27cc2012-07-17 05:30:37 +00003064 if (Mul->getNumOperands() != 2)
3065 return S;
3066 if (const SCEVConstant *Op0 =
3067 dyn_cast<SCEVConstant>(Mul->getOperand(0))) {
3068 C = C ? cast<SCEVConstant>(SE.getMulExpr(C, Op0)) : Op0;
3069 const SCEV *Remainder =
3070 CollectSubexprs(Mul->getOperand(1), C, Ops, L, SE, Depth+1);
3071 if (Remainder)
3072 Ops.push_back(SE.getMulExpr(C, Remainder));
3073 return NULL;
3074 }
Dan Gohman572645c2010-02-12 10:34:29 +00003075 }
Andrew Trick06a27cc2012-07-17 05:30:37 +00003076 return S;
Dan Gohman572645c2010-02-12 10:34:29 +00003077}
3078
3079/// GenerateReassociations - Split out subexpressions from adds and the bases of
3080/// addrecs.
3081void LSRInstance::GenerateReassociations(LSRUse &LU, unsigned LUIdx,
3082 Formula Base,
3083 unsigned Depth) {
3084 // Arbitrarily cap recursion to protect compile time.
3085 if (Depth >= 3) return;
3086
3087 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) {
3088 const SCEV *BaseReg = Base.BaseRegs[i];
3089
Dan Gohman3e22b7c2010-08-16 15:50:00 +00003090 SmallVector<const SCEV *, 8> AddOps;
Andrew Trick06a27cc2012-07-17 05:30:37 +00003091 const SCEV *Remainder = CollectSubexprs(BaseReg, 0, AddOps, L, SE);
3092 if (Remainder)
3093 AddOps.push_back(Remainder);
Dan Gohman3e3f15b2010-06-25 22:32:18 +00003094
Dan Gohman572645c2010-02-12 10:34:29 +00003095 if (AddOps.size() == 1) continue;
3096
3097 for (SmallVectorImpl<const SCEV *>::const_iterator J = AddOps.begin(),
3098 JE = AddOps.end(); J != JE; ++J) {
Dan Gohman3e22b7c2010-08-16 15:50:00 +00003099
3100 // Loop-variant "unknown" values are uninteresting; we won't be able to
3101 // do anything meaningful with them.
Dan Gohman17ead4f2010-11-17 21:23:15 +00003102 if (isa<SCEVUnknown>(*J) && !SE.isLoopInvariant(*J, L))
Dan Gohman3e22b7c2010-08-16 15:50:00 +00003103 continue;
3104
Dan Gohman572645c2010-02-12 10:34:29 +00003105 // Don't pull a constant into a register if the constant could be folded
3106 // into an immediate field.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003107 if (isAlwaysFoldable(TTI, SE, LU.MinOffset, LU.MaxOffset, LU.Kind,
3108 LU.AccessTy, *J, Base.getNumRegs() > 1))
Dan Gohman572645c2010-02-12 10:34:29 +00003109 continue;
3110
3111 // Collect all operands except *J.
Dan Gohman403a8cd2010-06-21 19:47:52 +00003112 SmallVector<const SCEV *, 8> InnerAddOps
Dan Gohman4eaee282010-08-04 17:43:57 +00003113 (((const SmallVector<const SCEV *, 8> &)AddOps).begin(), J);
Dan Gohman403a8cd2010-06-21 19:47:52 +00003114 InnerAddOps.append
Oscar Fuentesee56c422010-08-02 06:00:15 +00003115 (llvm::next(J), ((const SmallVector<const SCEV *, 8> &)AddOps).end());
Dan Gohman572645c2010-02-12 10:34:29 +00003116
3117 // Don't leave just a constant behind in a register if the constant could
3118 // be folded into an immediate field.
3119 if (InnerAddOps.size() == 1 &&
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003120 isAlwaysFoldable(TTI, SE, LU.MinOffset, LU.MaxOffset, LU.Kind,
3121 LU.AccessTy, InnerAddOps[0], Base.getNumRegs() > 1))
Dan Gohman572645c2010-02-12 10:34:29 +00003122 continue;
3123
Dan Gohmanfafb8902010-04-23 01:55:05 +00003124 const SCEV *InnerSum = SE.getAddExpr(InnerAddOps);
3125 if (InnerSum->isZero())
3126 continue;
Dan Gohman572645c2010-02-12 10:34:29 +00003127 Formula F = Base;
Dan Gohmancca82142011-05-03 00:46:49 +00003128
3129 // Add the remaining pieces of the add back into the new formula.
3130 const SCEVConstant *InnerSumSC = dyn_cast<SCEVConstant>(InnerSum);
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003131 if (InnerSumSC &&
Dan Gohmancca82142011-05-03 00:46:49 +00003132 SE.getTypeSizeInBits(InnerSumSC->getType()) <= 64 &&
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003133 TTI.isLegalAddImmediate((uint64_t)F.UnfoldedOffset +
3134 InnerSumSC->getValue()->getZExtValue())) {
Dan Gohmancca82142011-05-03 00:46:49 +00003135 F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset +
3136 InnerSumSC->getValue()->getZExtValue();
3137 F.BaseRegs.erase(F.BaseRegs.begin() + i);
3138 } else
3139 F.BaseRegs[i] = InnerSum;
3140
3141 // Add J as its own register, or an unfolded immediate.
3142 const SCEVConstant *SC = dyn_cast<SCEVConstant>(*J);
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003143 if (SC && SE.getTypeSizeInBits(SC->getType()) <= 64 &&
3144 TTI.isLegalAddImmediate((uint64_t)F.UnfoldedOffset +
3145 SC->getValue()->getZExtValue()))
Dan Gohmancca82142011-05-03 00:46:49 +00003146 F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset +
3147 SC->getValue()->getZExtValue();
3148 else
3149 F.BaseRegs.push_back(*J);
3150
Dan Gohman572645c2010-02-12 10:34:29 +00003151 if (InsertFormula(LU, LUIdx, F))
3152 // If that formula hadn't been seen before, recurse to find more like
3153 // it.
3154 GenerateReassociations(LU, LUIdx, LU.Formulae.back(), Depth+1);
3155 }
3156 }
3157}
3158
3159/// GenerateCombinations - Generate a formula consisting of all of the
3160/// loop-dominating registers added into a single register.
3161void LSRInstance::GenerateCombinations(LSRUse &LU, unsigned LUIdx,
Dan Gohman441a3892010-02-14 18:51:39 +00003162 Formula Base) {
Dan Gohman3f46a3a2010-03-01 17:49:51 +00003163 // This method is only interesting on a plurality of registers.
Dan Gohman572645c2010-02-12 10:34:29 +00003164 if (Base.BaseRegs.size() <= 1) return;
3165
3166 Formula F = Base;
3167 F.BaseRegs.clear();
3168 SmallVector<const SCEV *, 4> Ops;
3169 for (SmallVectorImpl<const SCEV *>::const_iterator
3170 I = Base.BaseRegs.begin(), E = Base.BaseRegs.end(); I != E; ++I) {
3171 const SCEV *BaseReg = *I;
Dan Gohmandc0e8fb2010-11-17 21:41:58 +00003172 if (SE.properlyDominates(BaseReg, L->getHeader()) &&
Dan Gohman17ead4f2010-11-17 21:23:15 +00003173 !SE.hasComputableLoopEvolution(BaseReg, L))
Dan Gohman572645c2010-02-12 10:34:29 +00003174 Ops.push_back(BaseReg);
3175 else
3176 F.BaseRegs.push_back(BaseReg);
3177 }
3178 if (Ops.size() > 1) {
Dan Gohmance947362010-02-14 18:50:49 +00003179 const SCEV *Sum = SE.getAddExpr(Ops);
3180 // TODO: If Sum is zero, it probably means ScalarEvolution missed an
3181 // opportunity to fold something. For now, just ignore such cases
Dan Gohman3f46a3a2010-03-01 17:49:51 +00003182 // rather than proceed with zero in a register.
Dan Gohmance947362010-02-14 18:50:49 +00003183 if (!Sum->isZero()) {
3184 F.BaseRegs.push_back(Sum);
3185 (void)InsertFormula(LU, LUIdx, F);
3186 }
Dan Gohman572645c2010-02-12 10:34:29 +00003187 }
3188}
3189
3190/// GenerateSymbolicOffsets - Generate reuse formulae using symbolic offsets.
3191void LSRInstance::GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx,
3192 Formula Base) {
3193 // We can't add a symbolic offset if the address already contains one.
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003194 if (Base.BaseGV) return;
Dan Gohman572645c2010-02-12 10:34:29 +00003195
3196 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) {
3197 const SCEV *G = Base.BaseRegs[i];
3198 GlobalValue *GV = ExtractSymbol(G, SE);
3199 if (G->isZero() || !GV)
3200 continue;
3201 Formula F = Base;
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003202 F.BaseGV = GV;
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003203 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F))
Dan Gohman572645c2010-02-12 10:34:29 +00003204 continue;
3205 F.BaseRegs[i] = G;
3206 (void)InsertFormula(LU, LUIdx, F);
3207 }
3208}
3209
3210/// GenerateConstantOffsets - Generate reuse formulae using symbolic offsets.
3211void LSRInstance::GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx,
3212 Formula Base) {
3213 // TODO: For now, just add the min and max offset, because it usually isn't
3214 // worthwhile looking at everything inbetween.
Dan Gohmanc88c1a42010-07-15 15:14:45 +00003215 SmallVector<int64_t, 2> Worklist;
Dan Gohman572645c2010-02-12 10:34:29 +00003216 Worklist.push_back(LU.MinOffset);
3217 if (LU.MaxOffset != LU.MinOffset)
3218 Worklist.push_back(LU.MaxOffset);
3219
3220 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) {
3221 const SCEV *G = Base.BaseRegs[i];
3222
3223 for (SmallVectorImpl<int64_t>::const_iterator I = Worklist.begin(),
3224 E = Worklist.end(); I != E; ++I) {
3225 Formula F = Base;
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003226 F.BaseOffset = (uint64_t)Base.BaseOffset - *I;
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003227 if (isLegalUse(TTI, LU.MinOffset - *I, LU.MaxOffset - *I, LU.Kind,
3228 LU.AccessTy, F)) {
Dan Gohmanc88c1a42010-07-15 15:14:45 +00003229 // Add the offset to the base register.
Dan Gohman4065f602010-08-16 15:39:27 +00003230 const SCEV *NewG = SE.getAddExpr(SE.getConstant(G->getType(), *I), G);
Dan Gohmanc88c1a42010-07-15 15:14:45 +00003231 // If it cancelled out, drop the base register, otherwise update it.
3232 if (NewG->isZero()) {
3233 std::swap(F.BaseRegs[i], F.BaseRegs.back());
3234 F.BaseRegs.pop_back();
3235 } else
3236 F.BaseRegs[i] = NewG;
Dan Gohman572645c2010-02-12 10:34:29 +00003237
3238 (void)InsertFormula(LU, LUIdx, F);
3239 }
3240 }
3241
3242 int64_t Imm = ExtractImmediate(G, SE);
3243 if (G->isZero() || Imm == 0)
3244 continue;
3245 Formula F = Base;
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003246 F.BaseOffset = (uint64_t)F.BaseOffset + Imm;
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003247 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F))
Dan Gohman572645c2010-02-12 10:34:29 +00003248 continue;
3249 F.BaseRegs[i] = G;
3250 (void)InsertFormula(LU, LUIdx, F);
3251 }
3252}
3253
3254/// GenerateICmpZeroScales - For ICmpZero, check to see if we can scale up
3255/// the comparison. For example, x == y -> x*c == y*c.
3256void LSRInstance::GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx,
3257 Formula Base) {
3258 if (LU.Kind != LSRUse::ICmpZero) return;
3259
3260 // Determine the integer type for the base formula.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003261 Type *IntTy = Base.getType();
Dan Gohman572645c2010-02-12 10:34:29 +00003262 if (!IntTy) return;
3263 if (SE.getTypeSizeInBits(IntTy) > 64) return;
3264
3265 // Don't do this if there is more than one offset.
3266 if (LU.MinOffset != LU.MaxOffset) return;
3267
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003268 assert(!Base.BaseGV && "ICmpZero use is not legal!");
Dan Gohman572645c2010-02-12 10:34:29 +00003269
3270 // Check each interesting stride.
3271 for (SmallSetVector<int64_t, 8>::const_iterator
3272 I = Factors.begin(), E = Factors.end(); I != E; ++I) {
3273 int64_t Factor = *I;
Dan Gohman572645c2010-02-12 10:34:29 +00003274
3275 // Check that the multiplication doesn't overflow.
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003276 if (Base.BaseOffset == INT64_MIN && Factor == -1)
Dan Gohman968cb932010-02-17 00:41:53 +00003277 continue;
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003278 int64_t NewBaseOffset = (uint64_t)Base.BaseOffset * Factor;
3279 if (NewBaseOffset / Factor != Base.BaseOffset)
Dan Gohman572645c2010-02-12 10:34:29 +00003280 continue;
3281
3282 // Check that multiplying with the use offset doesn't overflow.
3283 int64_t Offset = LU.MinOffset;
Dan Gohman968cb932010-02-17 00:41:53 +00003284 if (Offset == INT64_MIN && Factor == -1)
3285 continue;
Dan Gohman572645c2010-02-12 10:34:29 +00003286 Offset = (uint64_t)Offset * Factor;
Dan Gohman378c0b32010-02-17 00:42:19 +00003287 if (Offset / Factor != LU.MinOffset)
Dan Gohman572645c2010-02-12 10:34:29 +00003288 continue;
3289
Dan Gohman2ea09e02010-06-24 16:57:52 +00003290 Formula F = Base;
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003291 F.BaseOffset = NewBaseOffset;
Dan Gohman2ea09e02010-06-24 16:57:52 +00003292
Dan Gohman572645c2010-02-12 10:34:29 +00003293 // Check that this scale is legal.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003294 if (!isLegalUse(TTI, Offset, Offset, LU.Kind, LU.AccessTy, F))
Dan Gohman572645c2010-02-12 10:34:29 +00003295 continue;
3296
3297 // Compensate for the use having MinOffset built into it.
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003298 F.BaseOffset = (uint64_t)F.BaseOffset + Offset - LU.MinOffset;
Dan Gohman572645c2010-02-12 10:34:29 +00003299
Dan Gohmandeff6212010-05-03 22:09:21 +00003300 const SCEV *FactorS = SE.getConstant(IntTy, Factor);
Dan Gohman572645c2010-02-12 10:34:29 +00003301
3302 // Check that multiplying with each base register doesn't overflow.
3303 for (size_t i = 0, e = F.BaseRegs.size(); i != e; ++i) {
3304 F.BaseRegs[i] = SE.getMulExpr(F.BaseRegs[i], FactorS);
Dan Gohmanf09b7122010-02-19 19:35:48 +00003305 if (getExactSDiv(F.BaseRegs[i], FactorS, SE) != Base.BaseRegs[i])
Dan Gohman572645c2010-02-12 10:34:29 +00003306 goto next;
3307 }
3308
3309 // Check that multiplying with the scaled register doesn't overflow.
3310 if (F.ScaledReg) {
3311 F.ScaledReg = SE.getMulExpr(F.ScaledReg, FactorS);
Dan Gohmanf09b7122010-02-19 19:35:48 +00003312 if (getExactSDiv(F.ScaledReg, FactorS, SE) != Base.ScaledReg)
Dan Gohman572645c2010-02-12 10:34:29 +00003313 continue;
3314 }
3315
Dan Gohmancca82142011-05-03 00:46:49 +00003316 // Check that multiplying with the unfolded offset doesn't overflow.
3317 if (F.UnfoldedOffset != 0) {
Dan Gohman1b58d452011-05-23 21:07:39 +00003318 if (F.UnfoldedOffset == INT64_MIN && Factor == -1)
3319 continue;
Dan Gohmancca82142011-05-03 00:46:49 +00003320 F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset * Factor;
3321 if (F.UnfoldedOffset / Factor != Base.UnfoldedOffset)
3322 continue;
3323 }
3324
Dan Gohman572645c2010-02-12 10:34:29 +00003325 // If we make it here and it's legal, add it.
3326 (void)InsertFormula(LU, LUIdx, F);
3327 next:;
3328 }
3329}
3330
3331/// GenerateScales - Generate stride factor reuse formulae by making use of
3332/// scaled-offset address modes, for example.
Dan Gohmanea507f52010-05-20 19:44:23 +00003333void LSRInstance::GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base) {
Dan Gohman572645c2010-02-12 10:34:29 +00003334 // Determine the integer type for the base formula.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003335 Type *IntTy = Base.getType();
Dan Gohman572645c2010-02-12 10:34:29 +00003336 if (!IntTy) return;
3337
3338 // If this Formula already has a scaled register, we can't add another one.
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003339 if (Base.Scale != 0) return;
Dan Gohman572645c2010-02-12 10:34:29 +00003340
3341 // Check each interesting stride.
3342 for (SmallSetVector<int64_t, 8>::const_iterator
3343 I = Factors.begin(), E = Factors.end(); I != E; ++I) {
3344 int64_t Factor = *I;
3345
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003346 Base.Scale = Factor;
3347 Base.HasBaseReg = Base.BaseRegs.size() > 1;
Dan Gohman572645c2010-02-12 10:34:29 +00003348 // Check whether this scale is going to be legal.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003349 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
3350 Base)) {
Dan Gohman572645c2010-02-12 10:34:29 +00003351 // As a special-case, handle special out-of-loop Basic users specially.
3352 // TODO: Reconsider this special case.
3353 if (LU.Kind == LSRUse::Basic &&
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003354 isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LSRUse::Special,
3355 LU.AccessTy, Base) &&
Dan Gohman572645c2010-02-12 10:34:29 +00003356 LU.AllFixupsOutsideLoop)
3357 LU.Kind = LSRUse::Special;
3358 else
3359 continue;
3360 }
3361 // For an ICmpZero, negating a solitary base register won't lead to
3362 // new solutions.
3363 if (LU.Kind == LSRUse::ICmpZero &&
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003364 !Base.HasBaseReg && Base.BaseOffset == 0 && !Base.BaseGV)
Dan Gohman572645c2010-02-12 10:34:29 +00003365 continue;
3366 // For each addrec base reg, apply the scale, if possible.
3367 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i)
3368 if (const SCEVAddRecExpr *AR =
3369 dyn_cast<SCEVAddRecExpr>(Base.BaseRegs[i])) {
Dan Gohmandeff6212010-05-03 22:09:21 +00003370 const SCEV *FactorS = SE.getConstant(IntTy, Factor);
Dan Gohman572645c2010-02-12 10:34:29 +00003371 if (FactorS->isZero())
3372 continue;
3373 // Divide out the factor, ignoring high bits, since we'll be
3374 // scaling the value back up in the end.
Dan Gohmanf09b7122010-02-19 19:35:48 +00003375 if (const SCEV *Quotient = getExactSDiv(AR, FactorS, SE, true)) {
Dan Gohman572645c2010-02-12 10:34:29 +00003376 // TODO: This could be optimized to avoid all the copying.
3377 Formula F = Base;
3378 F.ScaledReg = Quotient;
Dan Gohman5ce6d052010-05-20 15:17:54 +00003379 F.DeleteBaseReg(F.BaseRegs[i]);
Dan Gohman572645c2010-02-12 10:34:29 +00003380 (void)InsertFormula(LU, LUIdx, F);
3381 }
3382 }
3383 }
3384}
3385
3386/// GenerateTruncates - Generate reuse formulae from different IV types.
Dan Gohmanea507f52010-05-20 19:44:23 +00003387void LSRInstance::GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base) {
Dan Gohman572645c2010-02-12 10:34:29 +00003388 // Don't bother truncating symbolic values.
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003389 if (Base.BaseGV) return;
Dan Gohman572645c2010-02-12 10:34:29 +00003390
3391 // Determine the integer type for the base formula.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003392 Type *DstTy = Base.getType();
Dan Gohman572645c2010-02-12 10:34:29 +00003393 if (!DstTy) return;
3394 DstTy = SE.getEffectiveSCEVType(DstTy);
3395
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003396 for (SmallSetVector<Type *, 4>::const_iterator
Dan Gohman572645c2010-02-12 10:34:29 +00003397 I = Types.begin(), E = Types.end(); I != E; ++I) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003398 Type *SrcTy = *I;
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003399 if (SrcTy != DstTy && TTI.isTruncateFree(SrcTy, DstTy)) {
Dan Gohman572645c2010-02-12 10:34:29 +00003400 Formula F = Base;
3401
3402 if (F.ScaledReg) F.ScaledReg = SE.getAnyExtendExpr(F.ScaledReg, *I);
3403 for (SmallVectorImpl<const SCEV *>::iterator J = F.BaseRegs.begin(),
3404 JE = F.BaseRegs.end(); J != JE; ++J)
3405 *J = SE.getAnyExtendExpr(*J, SrcTy);
3406
3407 // TODO: This assumes we've done basic processing on all uses and
3408 // have an idea what the register usage is.
3409 if (!F.hasRegsUsedByUsesOtherThan(LUIdx, RegUses))
3410 continue;
3411
3412 (void)InsertFormula(LU, LUIdx, F);
3413 }
3414 }
3415}
3416
3417namespace {
3418
Dan Gohman6020d852010-02-14 18:51:20 +00003419/// WorkItem - Helper class for GenerateCrossUseConstantOffsets. It's used to
Dan Gohman572645c2010-02-12 10:34:29 +00003420/// defer modifications so that the search phase doesn't have to worry about
3421/// the data structures moving underneath it.
3422struct WorkItem {
3423 size_t LUIdx;
3424 int64_t Imm;
3425 const SCEV *OrigReg;
3426
3427 WorkItem(size_t LI, int64_t I, const SCEV *R)
3428 : LUIdx(LI), Imm(I), OrigReg(R) {}
3429
3430 void print(raw_ostream &OS) const;
3431 void dump() const;
3432};
3433
3434}
3435
3436void WorkItem::print(raw_ostream &OS) const {
3437 OS << "in formulae referencing " << *OrigReg << " in use " << LUIdx
3438 << " , add offset " << Imm;
3439}
3440
Manman Ren286c4dc2012-09-12 05:06:18 +00003441#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman572645c2010-02-12 10:34:29 +00003442void WorkItem::dump() const {
3443 print(errs()); errs() << '\n';
3444}
Manman Rencc77eec2012-09-06 19:55:56 +00003445#endif
Dan Gohman572645c2010-02-12 10:34:29 +00003446
3447/// GenerateCrossUseConstantOffsets - Look for registers which are a constant
3448/// distance apart and try to form reuse opportunities between them.
3449void LSRInstance::GenerateCrossUseConstantOffsets() {
3450 // Group the registers by their value without any added constant offset.
3451 typedef std::map<int64_t, const SCEV *> ImmMapTy;
3452 typedef DenseMap<const SCEV *, ImmMapTy> RegMapTy;
3453 RegMapTy Map;
3454 DenseMap<const SCEV *, SmallBitVector> UsedByIndicesMap;
3455 SmallVector<const SCEV *, 8> Sequence;
3456 for (RegUseTracker::const_iterator I = RegUses.begin(), E = RegUses.end();
3457 I != E; ++I) {
3458 const SCEV *Reg = *I;
3459 int64_t Imm = ExtractImmediate(Reg, SE);
3460 std::pair<RegMapTy::iterator, bool> Pair =
3461 Map.insert(std::make_pair(Reg, ImmMapTy()));
3462 if (Pair.second)
3463 Sequence.push_back(Reg);
3464 Pair.first->second.insert(std::make_pair(Imm, *I));
3465 UsedByIndicesMap[Reg] |= RegUses.getUsedByIndices(*I);
3466 }
3467
3468 // Now examine each set of registers with the same base value. Build up
3469 // a list of work to do and do the work in a separate step so that we're
3470 // not adding formulae and register counts while we're searching.
Dan Gohman191bd642010-09-01 01:45:53 +00003471 SmallVector<WorkItem, 32> WorkItems;
3472 SmallSet<std::pair<size_t, int64_t>, 32> UniqueItems;
Dan Gohman572645c2010-02-12 10:34:29 +00003473 for (SmallVectorImpl<const SCEV *>::const_iterator I = Sequence.begin(),
3474 E = Sequence.end(); I != E; ++I) {
3475 const SCEV *Reg = *I;
3476 const ImmMapTy &Imms = Map.find(Reg)->second;
3477
Dan Gohmancd045c02010-02-12 19:20:37 +00003478 // It's not worthwhile looking for reuse if there's only one offset.
3479 if (Imms.size() == 1)
3480 continue;
3481
Dan Gohman572645c2010-02-12 10:34:29 +00003482 DEBUG(dbgs() << "Generating cross-use offsets for " << *Reg << ':';
3483 for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end();
3484 J != JE; ++J)
3485 dbgs() << ' ' << J->first;
3486 dbgs() << '\n');
3487
3488 // Examine each offset.
3489 for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end();
3490 J != JE; ++J) {
3491 const SCEV *OrigReg = J->second;
3492
3493 int64_t JImm = J->first;
3494 const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(OrigReg);
3495
3496 if (!isa<SCEVConstant>(OrigReg) &&
3497 UsedByIndicesMap[Reg].count() == 1) {
3498 DEBUG(dbgs() << "Skipping cross-use reuse for " << *OrigReg << '\n');
3499 continue;
3500 }
3501
3502 // Conservatively examine offsets between this orig reg a few selected
3503 // other orig regs.
3504 ImmMapTy::const_iterator OtherImms[] = {
3505 Imms.begin(), prior(Imms.end()),
Dan Gohmancca82142011-05-03 00:46:49 +00003506 Imms.lower_bound((Imms.begin()->first + prior(Imms.end())->first) / 2)
Dan Gohman572645c2010-02-12 10:34:29 +00003507 };
3508 for (size_t i = 0, e = array_lengthof(OtherImms); i != e; ++i) {
3509 ImmMapTy::const_iterator M = OtherImms[i];
Dan Gohmancd045c02010-02-12 19:20:37 +00003510 if (M == J || M == JE) continue;
Dan Gohman572645c2010-02-12 10:34:29 +00003511
3512 // Compute the difference between the two.
3513 int64_t Imm = (uint64_t)JImm - M->first;
3514 for (int LUIdx = UsedByIndices.find_first(); LUIdx != -1;
Dan Gohman191bd642010-09-01 01:45:53 +00003515 LUIdx = UsedByIndices.find_next(LUIdx))
Dan Gohman572645c2010-02-12 10:34:29 +00003516 // Make a memo of this use, offset, and register tuple.
Dan Gohman191bd642010-09-01 01:45:53 +00003517 if (UniqueItems.insert(std::make_pair(LUIdx, Imm)))
3518 WorkItems.push_back(WorkItem(LUIdx, Imm, OrigReg));
Evan Cheng586f69a2009-11-12 07:35:05 +00003519 }
3520 }
3521 }
3522
Dan Gohman572645c2010-02-12 10:34:29 +00003523 Map.clear();
3524 Sequence.clear();
3525 UsedByIndicesMap.clear();
Dan Gohman191bd642010-09-01 01:45:53 +00003526 UniqueItems.clear();
Dan Gohman572645c2010-02-12 10:34:29 +00003527
3528 // Now iterate through the worklist and add new formulae.
3529 for (SmallVectorImpl<WorkItem>::const_iterator I = WorkItems.begin(),
3530 E = WorkItems.end(); I != E; ++I) {
3531 const WorkItem &WI = *I;
3532 size_t LUIdx = WI.LUIdx;
3533 LSRUse &LU = Uses[LUIdx];
3534 int64_t Imm = WI.Imm;
3535 const SCEV *OrigReg = WI.OrigReg;
3536
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003537 Type *IntTy = SE.getEffectiveSCEVType(OrigReg->getType());
Dan Gohman572645c2010-02-12 10:34:29 +00003538 const SCEV *NegImmS = SE.getSCEV(ConstantInt::get(IntTy, -(uint64_t)Imm));
3539 unsigned BitWidth = SE.getTypeSizeInBits(IntTy);
3540
Dan Gohman3f46a3a2010-03-01 17:49:51 +00003541 // TODO: Use a more targeted data structure.
Dan Gohman572645c2010-02-12 10:34:29 +00003542 for (size_t L = 0, LE = LU.Formulae.size(); L != LE; ++L) {
Dan Gohman9f383eb2010-05-20 22:25:20 +00003543 const Formula &F = LU.Formulae[L];
Dan Gohman572645c2010-02-12 10:34:29 +00003544 // Use the immediate in the scaled register.
3545 if (F.ScaledReg == OrigReg) {
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003546 int64_t Offset = (uint64_t)F.BaseOffset + Imm * (uint64_t)F.Scale;
Dan Gohman572645c2010-02-12 10:34:29 +00003547 // Don't create 50 + reg(-50).
3548 if (F.referencesReg(SE.getSCEV(
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003549 ConstantInt::get(IntTy, -(uint64_t)Offset))))
Dan Gohman572645c2010-02-12 10:34:29 +00003550 continue;
3551 Formula NewF = F;
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003552 NewF.BaseOffset = Offset;
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003553 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
3554 NewF))
Dan Gohman572645c2010-02-12 10:34:29 +00003555 continue;
3556 NewF.ScaledReg = SE.getAddExpr(NegImmS, NewF.ScaledReg);
3557
3558 // If the new scale is a constant in a register, and adding the constant
3559 // value to the immediate would produce a value closer to zero than the
3560 // immediate itself, then the formula isn't worthwhile.
3561 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(NewF.ScaledReg))
Chris Lattnerc73b24d2011-07-15 06:08:15 +00003562 if (C->getValue()->isNegative() !=
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003563 (NewF.BaseOffset < 0) &&
3564 (C->getValue()->getValue().abs() * APInt(BitWidth, F.Scale))
3565 .ule(abs64(NewF.BaseOffset)))
Dan Gohman572645c2010-02-12 10:34:29 +00003566 continue;
3567
3568 // OK, looks good.
3569 (void)InsertFormula(LU, LUIdx, NewF);
3570 } else {
3571 // Use the immediate in a base register.
3572 for (size_t N = 0, NE = F.BaseRegs.size(); N != NE; ++N) {
3573 const SCEV *BaseReg = F.BaseRegs[N];
3574 if (BaseReg != OrigReg)
3575 continue;
3576 Formula NewF = F;
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003577 NewF.BaseOffset = (uint64_t)NewF.BaseOffset + Imm;
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003578 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset,
3579 LU.Kind, LU.AccessTy, NewF)) {
3580 if (!TTI.isLegalAddImmediate((uint64_t)NewF.UnfoldedOffset + Imm))
Dan Gohmancca82142011-05-03 00:46:49 +00003581 continue;
3582 NewF = F;
3583 NewF.UnfoldedOffset = (uint64_t)NewF.UnfoldedOffset + Imm;
3584 }
Dan Gohman572645c2010-02-12 10:34:29 +00003585 NewF.BaseRegs[N] = SE.getAddExpr(NegImmS, BaseReg);
3586
3587 // If the new formula has a constant in a register, and adding the
3588 // constant value to the immediate would produce a value closer to
3589 // zero than the immediate itself, then the formula isn't worthwhile.
3590 for (SmallVectorImpl<const SCEV *>::const_iterator
3591 J = NewF.BaseRegs.begin(), JE = NewF.BaseRegs.end();
3592 J != JE; ++J)
3593 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(*J))
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003594 if ((C->getValue()->getValue() + NewF.BaseOffset).abs().slt(
3595 abs64(NewF.BaseOffset)) &&
Dan Gohman360026f2010-05-18 23:48:08 +00003596 (C->getValue()->getValue() +
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003597 NewF.BaseOffset).countTrailingZeros() >=
3598 CountTrailingZeros_64(NewF.BaseOffset))
Dan Gohman572645c2010-02-12 10:34:29 +00003599 goto skip_formula;
3600
3601 // Ok, looks good.
3602 (void)InsertFormula(LU, LUIdx, NewF);
3603 break;
3604 skip_formula:;
3605 }
3606 }
3607 }
3608 }
Dale Johannesenc1acc3f2009-05-11 17:15:42 +00003609}
3610
Dan Gohman572645c2010-02-12 10:34:29 +00003611/// GenerateAllReuseFormulae - Generate formulae for each use.
3612void
3613LSRInstance::GenerateAllReuseFormulae() {
Dan Gohmanc2385a02010-02-16 01:42:53 +00003614 // This is split into multiple loops so that hasRegsUsedByUsesOtherThan
Dan Gohman572645c2010-02-12 10:34:29 +00003615 // queries are more precise.
3616 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3617 LSRUse &LU = Uses[LUIdx];
3618 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
3619 GenerateReassociations(LU, LUIdx, LU.Formulae[i]);
3620 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
3621 GenerateCombinations(LU, LUIdx, LU.Formulae[i]);
3622 }
3623 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3624 LSRUse &LU = Uses[LUIdx];
3625 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
3626 GenerateSymbolicOffsets(LU, LUIdx, LU.Formulae[i]);
3627 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
3628 GenerateConstantOffsets(LU, LUIdx, LU.Formulae[i]);
3629 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
3630 GenerateICmpZeroScales(LU, LUIdx, LU.Formulae[i]);
3631 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
3632 GenerateScales(LU, LUIdx, LU.Formulae[i]);
Dan Gohmanc2385a02010-02-16 01:42:53 +00003633 }
3634 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3635 LSRUse &LU = Uses[LUIdx];
Dan Gohman572645c2010-02-12 10:34:29 +00003636 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
3637 GenerateTruncates(LU, LUIdx, LU.Formulae[i]);
3638 }
3639
3640 GenerateCrossUseConstantOffsets();
Dan Gohman3902f9f2010-08-29 15:21:38 +00003641
3642 DEBUG(dbgs() << "\n"
3643 "After generating reuse formulae:\n";
3644 print_uses(dbgs()));
Dan Gohman572645c2010-02-12 10:34:29 +00003645}
3646
Dan Gohmanf63d70f2010-10-07 23:43:09 +00003647/// If there are multiple formulae with the same set of registers used
Dan Gohman572645c2010-02-12 10:34:29 +00003648/// by other uses, pick the best one and delete the others.
3649void LSRInstance::FilterOutUndesirableDedicatedRegisters() {
Dan Gohmanfc7744b2010-10-07 23:52:18 +00003650 DenseSet<const SCEV *> VisitedRegs;
3651 SmallPtrSet<const SCEV *, 16> Regs;
Andrew Trick8a5d7922011-12-06 03:13:31 +00003652 SmallPtrSet<const SCEV *, 16> LoserRegs;
Dan Gohman572645c2010-02-12 10:34:29 +00003653#ifndef NDEBUG
Dan Gohmanc6519f92010-05-20 20:05:31 +00003654 bool ChangedFormulae = false;
Dan Gohman572645c2010-02-12 10:34:29 +00003655#endif
3656
3657 // Collect the best formula for each unique set of shared registers. This
3658 // is reset for each use.
Preston Gurd83474ee2013-02-01 20:41:27 +00003659 typedef DenseMap<SmallVector<const SCEV *, 4>, size_t, UniquifierDenseMapInfo>
Dan Gohman572645c2010-02-12 10:34:29 +00003660 BestFormulaeTy;
3661 BestFormulaeTy BestFormulae;
3662
3663 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3664 LSRUse &LU = Uses[LUIdx];
Dan Gohmanea507f52010-05-20 19:44:23 +00003665 DEBUG(dbgs() << "Filtering for use "; LU.print(dbgs()); dbgs() << '\n');
Dan Gohman572645c2010-02-12 10:34:29 +00003666
Dan Gohmanb2df4332010-05-18 23:42:37 +00003667 bool Any = false;
Dan Gohman572645c2010-02-12 10:34:29 +00003668 for (size_t FIdx = 0, NumForms = LU.Formulae.size();
3669 FIdx != NumForms; ++FIdx) {
3670 Formula &F = LU.Formulae[FIdx];
3671
Andrew Trick8a5d7922011-12-06 03:13:31 +00003672 // Some formulas are instant losers. For example, they may depend on
3673 // nonexistent AddRecs from other loops. These need to be filtered
3674 // immediately, otherwise heuristics could choose them over others leading
3675 // to an unsatisfactory solution. Passing LoserRegs into RateFormula here
3676 // avoids the need to recompute this information across formulae using the
3677 // same bad AddRec. Passing LoserRegs is also essential unless we remove
3678 // the corresponding bad register from the Regs set.
3679 Cost CostF;
3680 Regs.clear();
3681 CostF.RateFormula(F, Regs, VisitedRegs, L, LU.Offsets, SE, DT,
3682 &LoserRegs);
3683 if (CostF.isLoser()) {
3684 // During initial formula generation, undesirable formulae are generated
3685 // by uses within other loops that have some non-trivial address mode or
3686 // use the postinc form of the IV. LSR needs to provide these formulae
3687 // as the basis of rediscovering the desired formula that uses an AddRec
3688 // corresponding to the existing phi. Once all formulae have been
3689 // generated, these initial losers may be pruned.
3690 DEBUG(dbgs() << " Filtering loser "; F.print(dbgs());
3691 dbgs() << "\n");
Dan Gohman572645c2010-02-12 10:34:29 +00003692 }
Andrew Trick8a5d7922011-12-06 03:13:31 +00003693 else {
Preston Gurd83474ee2013-02-01 20:41:27 +00003694 SmallVector<const SCEV *, 4> Key;
Andrew Trick8a5d7922011-12-06 03:13:31 +00003695 for (SmallVectorImpl<const SCEV *>::const_iterator J = F.BaseRegs.begin(),
3696 JE = F.BaseRegs.end(); J != JE; ++J) {
3697 const SCEV *Reg = *J;
3698 if (RegUses.isRegUsedByUsesOtherThan(Reg, LUIdx))
3699 Key.push_back(Reg);
3700 }
3701 if (F.ScaledReg &&
3702 RegUses.isRegUsedByUsesOtherThan(F.ScaledReg, LUIdx))
3703 Key.push_back(F.ScaledReg);
3704 // Unstable sort by host order ok, because this is only used for
3705 // uniquifying.
3706 std::sort(Key.begin(), Key.end());
Dan Gohman572645c2010-02-12 10:34:29 +00003707
Andrew Trick8a5d7922011-12-06 03:13:31 +00003708 std::pair<BestFormulaeTy::const_iterator, bool> P =
3709 BestFormulae.insert(std::make_pair(Key, FIdx));
3710 if (P.second)
3711 continue;
3712
Dan Gohman572645c2010-02-12 10:34:29 +00003713 Formula &Best = LU.Formulae[P.first->second];
Dan Gohmanfc7744b2010-10-07 23:52:18 +00003714
Dan Gohmanfc7744b2010-10-07 23:52:18 +00003715 Cost CostBest;
Dan Gohmanfc7744b2010-10-07 23:52:18 +00003716 Regs.clear();
Andrew Trick8a5d7922011-12-06 03:13:31 +00003717 CostBest.RateFormula(Best, Regs, VisitedRegs, L, LU.Offsets, SE, DT);
Dan Gohmanfc7744b2010-10-07 23:52:18 +00003718 if (CostF < CostBest)
Dan Gohman572645c2010-02-12 10:34:29 +00003719 std::swap(F, Best);
Dan Gohman6458ff92010-05-18 22:37:37 +00003720 DEBUG(dbgs() << " Filtering out formula "; F.print(dbgs());
Dan Gohman572645c2010-02-12 10:34:29 +00003721 dbgs() << "\n"
Dan Gohman6458ff92010-05-18 22:37:37 +00003722 " in favor of formula "; Best.print(dbgs());
Dan Gohman572645c2010-02-12 10:34:29 +00003723 dbgs() << '\n');
Dan Gohman572645c2010-02-12 10:34:29 +00003724 }
Andrew Trick8a5d7922011-12-06 03:13:31 +00003725#ifndef NDEBUG
3726 ChangedFormulae = true;
3727#endif
3728 LU.DeleteFormula(F);
3729 --FIdx;
3730 --NumForms;
3731 Any = true;
Dan Gohman59dc6032010-05-07 23:36:59 +00003732 }
3733
Dan Gohman57aaa0b2010-05-18 23:55:57 +00003734 // Now that we've filtered out some formulae, recompute the Regs set.
Dan Gohmanb2df4332010-05-18 23:42:37 +00003735 if (Any)
3736 LU.RecomputeRegs(LUIdx, RegUses);
Dan Gohman59dc6032010-05-07 23:36:59 +00003737
3738 // Reset this to prepare for the next use.
Dan Gohman572645c2010-02-12 10:34:29 +00003739 BestFormulae.clear();
3740 }
3741
Dan Gohmanc6519f92010-05-20 20:05:31 +00003742 DEBUG(if (ChangedFormulae) {
Dan Gohman9214b822010-02-13 02:06:02 +00003743 dbgs() << "\n"
3744 "After filtering out undesirable candidates:\n";
Dan Gohman572645c2010-02-12 10:34:29 +00003745 print_uses(dbgs());
3746 });
3747}
3748
Dan Gohmand079c302010-05-18 22:51:59 +00003749// This is a rough guess that seems to work fairly well.
3750static const size_t ComplexityLimit = UINT16_MAX;
3751
3752/// EstimateSearchSpaceComplexity - Estimate the worst-case number of
3753/// solutions the solver might have to consider. It almost never considers
3754/// this many solutions because it prune the search space, but the pruning
3755/// isn't always sufficient.
3756size_t LSRInstance::EstimateSearchSpaceComplexity() const {
Dan Gohman0d6715a2010-10-07 23:37:58 +00003757 size_t Power = 1;
Dan Gohmand079c302010-05-18 22:51:59 +00003758 for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(),
3759 E = Uses.end(); I != E; ++I) {
3760 size_t FSize = I->Formulae.size();
3761 if (FSize >= ComplexityLimit) {
3762 Power = ComplexityLimit;
3763 break;
3764 }
3765 Power *= FSize;
3766 if (Power >= ComplexityLimit)
3767 break;
3768 }
3769 return Power;
3770}
3771
Dan Gohman4aa5c2e2010-08-29 16:09:42 +00003772/// NarrowSearchSpaceByDetectingSupersets - When one formula uses a superset
3773/// of the registers of another formula, it won't help reduce register
3774/// pressure (though it may not necessarily hurt register pressure); remove
3775/// it to simplify the system.
3776void LSRInstance::NarrowSearchSpaceByDetectingSupersets() {
Dan Gohmana2086b32010-05-19 23:43:12 +00003777 if (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
3778 DEBUG(dbgs() << "The search space is too complex.\n");
3779
3780 DEBUG(dbgs() << "Narrowing the search space by eliminating formulae "
3781 "which use a superset of registers used by other "
3782 "formulae.\n");
3783
3784 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3785 LSRUse &LU = Uses[LUIdx];
3786 bool Any = false;
3787 for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
3788 Formula &F = LU.Formulae[i];
Dan Gohmanf7ff37d2010-05-20 20:00:41 +00003789 // Look for a formula with a constant or GV in a register. If the use
3790 // also has a formula with that same value in an immediate field,
3791 // delete the one that uses a register.
Dan Gohmana2086b32010-05-19 23:43:12 +00003792 for (SmallVectorImpl<const SCEV *>::const_iterator
3793 I = F.BaseRegs.begin(), E = F.BaseRegs.end(); I != E; ++I) {
3794 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(*I)) {
3795 Formula NewF = F;
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003796 NewF.BaseOffset += C->getValue()->getSExtValue();
Dan Gohmana2086b32010-05-19 23:43:12 +00003797 NewF.BaseRegs.erase(NewF.BaseRegs.begin() +
3798 (I - F.BaseRegs.begin()));
3799 if (LU.HasFormulaWithSameRegs(NewF)) {
3800 DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n');
3801 LU.DeleteFormula(F);
3802 --i;
3803 --e;
3804 Any = true;
3805 break;
3806 }
3807 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(*I)) {
3808 if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue()))
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003809 if (!F.BaseGV) {
Dan Gohmana2086b32010-05-19 23:43:12 +00003810 Formula NewF = F;
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003811 NewF.BaseGV = GV;
Dan Gohmana2086b32010-05-19 23:43:12 +00003812 NewF.BaseRegs.erase(NewF.BaseRegs.begin() +
3813 (I - F.BaseRegs.begin()));
3814 if (LU.HasFormulaWithSameRegs(NewF)) {
3815 DEBUG(dbgs() << " Deleting "; F.print(dbgs());
3816 dbgs() << '\n');
3817 LU.DeleteFormula(F);
3818 --i;
3819 --e;
3820 Any = true;
3821 break;
3822 }
3823 }
3824 }
3825 }
3826 }
3827 if (Any)
3828 LU.RecomputeRegs(LUIdx, RegUses);
3829 }
3830
3831 DEBUG(dbgs() << "After pre-selection:\n";
3832 print_uses(dbgs()));
3833 }
Dan Gohman4aa5c2e2010-08-29 16:09:42 +00003834}
Dan Gohmana2086b32010-05-19 23:43:12 +00003835
Dan Gohman4aa5c2e2010-08-29 16:09:42 +00003836/// NarrowSearchSpaceByCollapsingUnrolledCode - When there are many registers
3837/// for expressions like A, A+1, A+2, etc., allocate a single register for
3838/// them.
3839void LSRInstance::NarrowSearchSpaceByCollapsingUnrolledCode() {
Dan Gohmana2086b32010-05-19 23:43:12 +00003840 if (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
3841 DEBUG(dbgs() << "The search space is too complex.\n");
3842
3843 DEBUG(dbgs() << "Narrowing the search space by assuming that uses "
3844 "separated by a constant offset will use the same "
3845 "registers.\n");
3846
Dan Gohmanf7ff37d2010-05-20 20:00:41 +00003847 // This is especially useful for unrolled loops.
3848
Dan Gohmana2086b32010-05-19 23:43:12 +00003849 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3850 LSRUse &LU = Uses[LUIdx];
Dan Gohman402d4352010-05-20 20:33:18 +00003851 for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(),
3852 E = LU.Formulae.end(); I != E; ++I) {
3853 const Formula &F = *I;
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003854 if (F.BaseOffset != 0 && F.Scale == 0) {
Dan Gohman191bd642010-09-01 01:45:53 +00003855 if (LSRUse *LUThatHas = FindUseWithSimilarFormula(F, LU)) {
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003856 if (reconcileNewOffset(*LUThatHas, F.BaseOffset,
Dan Gohmana2086b32010-05-19 23:43:12 +00003857 /*HasBaseReg=*/false,
3858 LU.Kind, LU.AccessTy)) {
3859 DEBUG(dbgs() << " Deleting use "; LU.print(dbgs());
3860 dbgs() << '\n');
3861
3862 LUThatHas->AllFixupsOutsideLoop &= LU.AllFixupsOutsideLoop;
3863
Dan Gohman191bd642010-09-01 01:45:53 +00003864 // Update the relocs to reference the new use.
3865 for (SmallVectorImpl<LSRFixup>::iterator I = Fixups.begin(),
3866 E = Fixups.end(); I != E; ++I) {
3867 LSRFixup &Fixup = *I;
3868 if (Fixup.LUIdx == LUIdx) {
3869 Fixup.LUIdx = LUThatHas - &Uses.front();
Chandler Carrutha07dcb12013-01-07 15:04:40 +00003870 Fixup.Offset += F.BaseOffset;
Dan Gohmandd3db0e2010-10-07 23:36:45 +00003871 // Add the new offset to LUThatHas' offset list.
3872 if (LUThatHas->Offsets.back() != Fixup.Offset) {
3873 LUThatHas->Offsets.push_back(Fixup.Offset);
3874 if (Fixup.Offset > LUThatHas->MaxOffset)
3875 LUThatHas->MaxOffset = Fixup.Offset;
3876 if (Fixup.Offset < LUThatHas->MinOffset)
3877 LUThatHas->MinOffset = Fixup.Offset;
3878 }
Dan Gohman191bd642010-09-01 01:45:53 +00003879 DEBUG(dbgs() << "New fixup has offset "
3880 << Fixup.Offset << '\n');
3881 }
3882 if (Fixup.LUIdx == NumUses-1)
3883 Fixup.LUIdx = LUIdx;
3884 }
3885
Dan Gohmanc2921ea2010-10-08 19:33:26 +00003886 // Delete formulae from the new use which are no longer legal.
3887 bool Any = false;
3888 for (size_t i = 0, e = LUThatHas->Formulae.size(); i != e; ++i) {
3889 Formula &F = LUThatHas->Formulae[i];
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00003890 if (!isLegalUse(TTI, LUThatHas->MinOffset, LUThatHas->MaxOffset,
3891 LUThatHas->Kind, LUThatHas->AccessTy, F)) {
Dan Gohmanc2921ea2010-10-08 19:33:26 +00003892 DEBUG(dbgs() << " Deleting "; F.print(dbgs());
3893 dbgs() << '\n');
3894 LUThatHas->DeleteFormula(F);
3895 --i;
3896 --e;
3897 Any = true;
3898 }
3899 }
3900 if (Any)
3901 LUThatHas->RecomputeRegs(LUThatHas - &Uses.front(), RegUses);
3902
Dan Gohmana2086b32010-05-19 23:43:12 +00003903 // Delete the old use.
Dan Gohmanc6897702010-10-07 23:33:43 +00003904 DeleteUse(LU, LUIdx);
Dan Gohmana2086b32010-05-19 23:43:12 +00003905 --LUIdx;
3906 --NumUses;
3907 break;
3908 }
3909 }
3910 }
3911 }
3912 }
3913
3914 DEBUG(dbgs() << "After pre-selection:\n";
3915 print_uses(dbgs()));
3916 }
Dan Gohman4aa5c2e2010-08-29 16:09:42 +00003917}
Dan Gohmana2086b32010-05-19 23:43:12 +00003918
Andrew Trick3228cc22011-03-14 16:50:06 +00003919/// NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters - Call
Dan Gohman4f7e18d2010-08-29 16:39:22 +00003920/// FilterOutUndesirableDedicatedRegisters again, if necessary, now that
3921/// we've done more filtering, as it may be able to find more formulae to
3922/// eliminate.
3923void LSRInstance::NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters(){
3924 if (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
3925 DEBUG(dbgs() << "The search space is too complex.\n");
3926
3927 DEBUG(dbgs() << "Narrowing the search space by re-filtering out "
3928 "undesirable dedicated registers.\n");
3929
3930 FilterOutUndesirableDedicatedRegisters();
3931
3932 DEBUG(dbgs() << "After pre-selection:\n";
3933 print_uses(dbgs()));
3934 }
3935}
3936
Dan Gohman4aa5c2e2010-08-29 16:09:42 +00003937/// NarrowSearchSpaceByPickingWinnerRegs - Pick a register which seems likely
3938/// to be profitable, and then in any use which has any reference to that
3939/// register, delete all formulae which do not reference that register.
3940void LSRInstance::NarrowSearchSpaceByPickingWinnerRegs() {
Dan Gohman76c315a2010-05-20 20:52:00 +00003941 // With all other options exhausted, loop until the system is simple
3942 // enough to handle.
Dan Gohman572645c2010-02-12 10:34:29 +00003943 SmallPtrSet<const SCEV *, 4> Taken;
Dan Gohmand079c302010-05-18 22:51:59 +00003944 while (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
Dan Gohman572645c2010-02-12 10:34:29 +00003945 // Ok, we have too many of formulae on our hands to conveniently handle.
3946 // Use a rough heuristic to thin out the list.
Dan Gohman0da751b2010-05-18 22:41:32 +00003947 DEBUG(dbgs() << "The search space is too complex.\n");
Dan Gohman572645c2010-02-12 10:34:29 +00003948
3949 // Pick the register which is used by the most LSRUses, which is likely
3950 // to be a good reuse register candidate.
3951 const SCEV *Best = 0;
3952 unsigned BestNum = 0;
3953 for (RegUseTracker::const_iterator I = RegUses.begin(), E = RegUses.end();
3954 I != E; ++I) {
3955 const SCEV *Reg = *I;
3956 if (Taken.count(Reg))
3957 continue;
3958 if (!Best)
3959 Best = Reg;
3960 else {
3961 unsigned Count = RegUses.getUsedByIndices(Reg).count();
3962 if (Count > BestNum) {
3963 Best = Reg;
3964 BestNum = Count;
3965 }
3966 }
3967 }
3968
3969 DEBUG(dbgs() << "Narrowing the search space by assuming " << *Best
Dan Gohman3f46a3a2010-03-01 17:49:51 +00003970 << " will yield profitable reuse.\n");
Dan Gohman572645c2010-02-12 10:34:29 +00003971 Taken.insert(Best);
3972
3973 // In any use with formulae which references this register, delete formulae
3974 // which don't reference it.
Dan Gohmanb2df4332010-05-18 23:42:37 +00003975 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3976 LSRUse &LU = Uses[LUIdx];
Dan Gohman572645c2010-02-12 10:34:29 +00003977 if (!LU.Regs.count(Best)) continue;
3978
Dan Gohmanb2df4332010-05-18 23:42:37 +00003979 bool Any = false;
Dan Gohman572645c2010-02-12 10:34:29 +00003980 for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
3981 Formula &F = LU.Formulae[i];
3982 if (!F.referencesReg(Best)) {
3983 DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n');
Dan Gohmand69d6282010-05-18 22:39:15 +00003984 LU.DeleteFormula(F);
Dan Gohman572645c2010-02-12 10:34:29 +00003985 --e;
3986 --i;
Dan Gohmanb2df4332010-05-18 23:42:37 +00003987 Any = true;
Dan Gohman59dc6032010-05-07 23:36:59 +00003988 assert(e != 0 && "Use has no formulae left! Is Regs inconsistent?");
Dan Gohman572645c2010-02-12 10:34:29 +00003989 continue;
3990 }
Dan Gohman572645c2010-02-12 10:34:29 +00003991 }
Dan Gohmanb2df4332010-05-18 23:42:37 +00003992
3993 if (Any)
3994 LU.RecomputeRegs(LUIdx, RegUses);
Dan Gohman572645c2010-02-12 10:34:29 +00003995 }
3996
3997 DEBUG(dbgs() << "After pre-selection:\n";
3998 print_uses(dbgs()));
3999 }
4000}
4001
Dan Gohman4aa5c2e2010-08-29 16:09:42 +00004002/// NarrowSearchSpaceUsingHeuristics - If there are an extraordinary number of
4003/// formulae to choose from, use some rough heuristics to prune down the number
4004/// of formulae. This keeps the main solver from taking an extraordinary amount
4005/// of time in some worst-case scenarios.
4006void LSRInstance::NarrowSearchSpaceUsingHeuristics() {
4007 NarrowSearchSpaceByDetectingSupersets();
4008 NarrowSearchSpaceByCollapsingUnrolledCode();
Dan Gohman4f7e18d2010-08-29 16:39:22 +00004009 NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters();
Dan Gohman4aa5c2e2010-08-29 16:09:42 +00004010 NarrowSearchSpaceByPickingWinnerRegs();
4011}
4012
Dan Gohman572645c2010-02-12 10:34:29 +00004013/// SolveRecurse - This is the recursive solver.
4014void LSRInstance::SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
4015 Cost &SolutionCost,
4016 SmallVectorImpl<const Formula *> &Workspace,
4017 const Cost &CurCost,
4018 const SmallPtrSet<const SCEV *, 16> &CurRegs,
4019 DenseSet<const SCEV *> &VisitedRegs) const {
4020 // Some ideas:
4021 // - prune more:
4022 // - use more aggressive filtering
4023 // - sort the formula so that the most profitable solutions are found first
4024 // - sort the uses too
4025 // - search faster:
Dan Gohman3f46a3a2010-03-01 17:49:51 +00004026 // - don't compute a cost, and then compare. compare while computing a cost
Dan Gohman572645c2010-02-12 10:34:29 +00004027 // and bail early.
4028 // - track register sets with SmallBitVector
4029
4030 const LSRUse &LU = Uses[Workspace.size()];
4031
4032 // If this use references any register that's already a part of the
4033 // in-progress solution, consider it a requirement that a formula must
4034 // reference that register in order to be considered. This prunes out
4035 // unprofitable searching.
4036 SmallSetVector<const SCEV *, 4> ReqRegs;
4037 for (SmallPtrSet<const SCEV *, 16>::const_iterator I = CurRegs.begin(),
4038 E = CurRegs.end(); I != E; ++I)
Dan Gohman9214b822010-02-13 02:06:02 +00004039 if (LU.Regs.count(*I))
Dan Gohman572645c2010-02-12 10:34:29 +00004040 ReqRegs.insert(*I);
Dan Gohman572645c2010-02-12 10:34:29 +00004041
4042 SmallPtrSet<const SCEV *, 16> NewRegs;
4043 Cost NewCost;
4044 for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(),
4045 E = LU.Formulae.end(); I != E; ++I) {
4046 const Formula &F = *I;
4047
4048 // Ignore formulae which do not use any of the required registers.
Andrew Trickd1944542012-03-22 22:42:51 +00004049 bool SatisfiedReqReg = true;
Dan Gohman572645c2010-02-12 10:34:29 +00004050 for (SmallSetVector<const SCEV *, 4>::const_iterator J = ReqRegs.begin(),
4051 JE = ReqRegs.end(); J != JE; ++J) {
4052 const SCEV *Reg = *J;
4053 if ((!F.ScaledReg || F.ScaledReg != Reg) &&
4054 std::find(F.BaseRegs.begin(), F.BaseRegs.end(), Reg) ==
Andrew Trickd1944542012-03-22 22:42:51 +00004055 F.BaseRegs.end()) {
4056 SatisfiedReqReg = false;
4057 break;
4058 }
Dan Gohman572645c2010-02-12 10:34:29 +00004059 }
Andrew Trickd1944542012-03-22 22:42:51 +00004060 if (!SatisfiedReqReg) {
4061 // If none of the formulae satisfied the required registers, then we could
4062 // clear ReqRegs and try again. Currently, we simply give up in this case.
4063 continue;
4064 }
Dan Gohman572645c2010-02-12 10:34:29 +00004065
4066 // Evaluate the cost of the current formula. If it's already worse than
4067 // the current best, prune the search at that point.
4068 NewCost = CurCost;
4069 NewRegs = CurRegs;
4070 NewCost.RateFormula(F, NewRegs, VisitedRegs, L, LU.Offsets, SE, DT);
4071 if (NewCost < SolutionCost) {
4072 Workspace.push_back(&F);
4073 if (Workspace.size() != Uses.size()) {
4074 SolveRecurse(Solution, SolutionCost, Workspace, NewCost,
4075 NewRegs, VisitedRegs);
4076 if (F.getNumRegs() == 1 && Workspace.size() == 1)
4077 VisitedRegs.insert(F.ScaledReg ? F.ScaledReg : F.BaseRegs[0]);
4078 } else {
4079 DEBUG(dbgs() << "New best at "; NewCost.print(dbgs());
Andrew Trick8bf295b2012-01-09 18:58:16 +00004080 dbgs() << ".\n Regs:";
Dan Gohman572645c2010-02-12 10:34:29 +00004081 for (SmallPtrSet<const SCEV *, 16>::const_iterator
4082 I = NewRegs.begin(), E = NewRegs.end(); I != E; ++I)
4083 dbgs() << ' ' << **I;
4084 dbgs() << '\n');
4085
4086 SolutionCost = NewCost;
4087 Solution = Workspace;
4088 }
4089 Workspace.pop_back();
4090 }
Dan Gohman9214b822010-02-13 02:06:02 +00004091 }
Dan Gohman572645c2010-02-12 10:34:29 +00004092}
4093
Dan Gohman76c315a2010-05-20 20:52:00 +00004094/// Solve - Choose one formula from each use. Return the results in the given
4095/// Solution vector.
Dan Gohman572645c2010-02-12 10:34:29 +00004096void LSRInstance::Solve(SmallVectorImpl<const Formula *> &Solution) const {
4097 SmallVector<const Formula *, 8> Workspace;
4098 Cost SolutionCost;
4099 SolutionCost.Loose();
4100 Cost CurCost;
4101 SmallPtrSet<const SCEV *, 16> CurRegs;
4102 DenseSet<const SCEV *> VisitedRegs;
4103 Workspace.reserve(Uses.size());
4104
Dan Gohmanf7ff37d2010-05-20 20:00:41 +00004105 // SolveRecurse does all the work.
Dan Gohman572645c2010-02-12 10:34:29 +00004106 SolveRecurse(Solution, SolutionCost, Workspace, CurCost,
4107 CurRegs, VisitedRegs);
Andrew Trick80ef1b22011-09-27 00:44:14 +00004108 if (Solution.empty()) {
4109 DEBUG(dbgs() << "\nNo Satisfactory Solution\n");
4110 return;
4111 }
Dan Gohman572645c2010-02-12 10:34:29 +00004112
4113 // Ok, we've now made all our decisions.
4114 DEBUG(dbgs() << "\n"
4115 "The chosen solution requires "; SolutionCost.print(dbgs());
4116 dbgs() << ":\n";
4117 for (size_t i = 0, e = Uses.size(); i != e; ++i) {
4118 dbgs() << " ";
4119 Uses[i].print(dbgs());
4120 dbgs() << "\n"
4121 " ";
4122 Solution[i]->print(dbgs());
4123 dbgs() << '\n';
4124 });
Dan Gohmana5528782010-05-20 20:59:23 +00004125
4126 assert(Solution.size() == Uses.size() && "Malformed solution!");
Dan Gohman572645c2010-02-12 10:34:29 +00004127}
4128
Dan Gohmane5f76872010-04-09 22:07:05 +00004129/// HoistInsertPosition - Helper for AdjustInsertPositionForExpand. Climb up
4130/// the dominator tree far as we can go while still being dominated by the
4131/// input positions. This helps canonicalize the insert position, which
4132/// encourages sharing.
4133BasicBlock::iterator
4134LSRInstance::HoistInsertPosition(BasicBlock::iterator IP,
4135 const SmallVectorImpl<Instruction *> &Inputs)
4136 const {
4137 for (;;) {
4138 const Loop *IPLoop = LI.getLoopFor(IP->getParent());
4139 unsigned IPLoopDepth = IPLoop ? IPLoop->getLoopDepth() : 0;
4140
4141 BasicBlock *IDom;
Dan Gohmand974a0e2010-05-20 20:00:25 +00004142 for (DomTreeNode *Rung = DT.getNode(IP->getParent()); ; ) {
Dan Gohman0fe46d92010-05-20 22:46:54 +00004143 if (!Rung) return IP;
Dan Gohmand974a0e2010-05-20 20:00:25 +00004144 Rung = Rung->getIDom();
4145 if (!Rung) return IP;
4146 IDom = Rung->getBlock();
Dan Gohmane5f76872010-04-09 22:07:05 +00004147
4148 // Don't climb into a loop though.
4149 const Loop *IDomLoop = LI.getLoopFor(IDom);
4150 unsigned IDomDepth = IDomLoop ? IDomLoop->getLoopDepth() : 0;
4151 if (IDomDepth <= IPLoopDepth &&
4152 (IDomDepth != IPLoopDepth || IDomLoop == IPLoop))
4153 break;
4154 }
4155
4156 bool AllDominate = true;
4157 Instruction *BetterPos = 0;
4158 Instruction *Tentative = IDom->getTerminator();
4159 for (SmallVectorImpl<Instruction *>::const_iterator I = Inputs.begin(),
4160 E = Inputs.end(); I != E; ++I) {
4161 Instruction *Inst = *I;
4162 if (Inst == Tentative || !DT.dominates(Inst, Tentative)) {
4163 AllDominate = false;
4164 break;
4165 }
4166 // Attempt to find an insert position in the middle of the block,
4167 // instead of at the end, so that it can be used for other expansions.
4168 if (IDom == Inst->getParent() &&
Rafael Espindola9719cf32012-04-30 03:53:06 +00004169 (!BetterPos || !DT.dominates(Inst, BetterPos)))
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004170 BetterPos = llvm::next(BasicBlock::iterator(Inst));
Dan Gohmane5f76872010-04-09 22:07:05 +00004171 }
4172 if (!AllDominate)
4173 break;
4174 if (BetterPos)
4175 IP = BetterPos;
4176 else
4177 IP = Tentative;
4178 }
4179
4180 return IP;
4181}
4182
4183/// AdjustInsertPositionForExpand - Determine an input position which will be
Dan Gohmand96eae82010-04-09 02:00:38 +00004184/// dominated by the operands and which will dominate the result.
4185BasicBlock::iterator
Andrew Trickb5c26ef2012-01-20 07:41:13 +00004186LSRInstance::AdjustInsertPositionForExpand(BasicBlock::iterator LowestIP,
Dan Gohmane5f76872010-04-09 22:07:05 +00004187 const LSRFixup &LF,
Andrew Trickb5c26ef2012-01-20 07:41:13 +00004188 const LSRUse &LU,
4189 SCEVExpander &Rewriter) const {
Dan Gohmand96eae82010-04-09 02:00:38 +00004190 // Collect some instructions which must be dominated by the
Dan Gohman448db1c2010-04-07 22:27:08 +00004191 // expanding replacement. These must be dominated by any operands that
Dan Gohman572645c2010-02-12 10:34:29 +00004192 // will be required in the expansion.
4193 SmallVector<Instruction *, 4> Inputs;
4194 if (Instruction *I = dyn_cast<Instruction>(LF.OperandValToReplace))
4195 Inputs.push_back(I);
4196 if (LU.Kind == LSRUse::ICmpZero)
4197 if (Instruction *I =
4198 dyn_cast<Instruction>(cast<ICmpInst>(LF.UserInst)->getOperand(1)))
4199 Inputs.push_back(I);
Dan Gohman448db1c2010-04-07 22:27:08 +00004200 if (LF.PostIncLoops.count(L)) {
4201 if (LF.isUseFullyOutsideLoop(L))
Dan Gohman069d6f32010-03-02 01:59:21 +00004202 Inputs.push_back(L->getLoopLatch()->getTerminator());
4203 else
4204 Inputs.push_back(IVIncInsertPos);
4205 }
Dan Gohman701a4ae2010-04-08 05:57:57 +00004206 // The expansion must also be dominated by the increment positions of any
4207 // loops it for which it is using post-inc mode.
4208 for (PostIncLoopSet::const_iterator I = LF.PostIncLoops.begin(),
4209 E = LF.PostIncLoops.end(); I != E; ++I) {
4210 const Loop *PIL = *I;
4211 if (PIL == L) continue;
4212
Dan Gohmane5f76872010-04-09 22:07:05 +00004213 // Be dominated by the loop exit.
Dan Gohman701a4ae2010-04-08 05:57:57 +00004214 SmallVector<BasicBlock *, 4> ExitingBlocks;
4215 PIL->getExitingBlocks(ExitingBlocks);
4216 if (!ExitingBlocks.empty()) {
4217 BasicBlock *BB = ExitingBlocks[0];
4218 for (unsigned i = 1, e = ExitingBlocks.size(); i != e; ++i)
4219 BB = DT.findNearestCommonDominator(BB, ExitingBlocks[i]);
4220 Inputs.push_back(BB->getTerminator());
4221 }
4222 }
Dan Gohman572645c2010-02-12 10:34:29 +00004223
Andrew Trickb5c26ef2012-01-20 07:41:13 +00004224 assert(!isa<PHINode>(LowestIP) && !isa<LandingPadInst>(LowestIP)
4225 && !isa<DbgInfoIntrinsic>(LowestIP) &&
4226 "Insertion point must be a normal instruction");
4227
Dan Gohman572645c2010-02-12 10:34:29 +00004228 // Then, climb up the immediate dominator tree as far as we can go while
4229 // still being dominated by the input positions.
Andrew Trickb5c26ef2012-01-20 07:41:13 +00004230 BasicBlock::iterator IP = HoistInsertPosition(LowestIP, Inputs);
Dan Gohmand96eae82010-04-09 02:00:38 +00004231
4232 // Don't insert instructions before PHI nodes.
Dan Gohman572645c2010-02-12 10:34:29 +00004233 while (isa<PHINode>(IP)) ++IP;
Dan Gohmand96eae82010-04-09 02:00:38 +00004234
Bill Wendlinga4c86ab2011-08-24 21:06:46 +00004235 // Ignore landingpad instructions.
4236 while (isa<LandingPadInst>(IP)) ++IP;
4237
Dan Gohmand96eae82010-04-09 02:00:38 +00004238 // Ignore debug intrinsics.
Dan Gohman449f31c2010-03-26 00:33:27 +00004239 while (isa<DbgInfoIntrinsic>(IP)) ++IP;
Dan Gohman572645c2010-02-12 10:34:29 +00004240
Andrew Trickb5c26ef2012-01-20 07:41:13 +00004241 // Set IP below instructions recently inserted by SCEVExpander. This keeps the
4242 // IP consistent across expansions and allows the previously inserted
4243 // instructions to be reused by subsequent expansion.
4244 while (Rewriter.isInsertedInstruction(IP) && IP != LowestIP) ++IP;
4245
Dan Gohmand96eae82010-04-09 02:00:38 +00004246 return IP;
4247}
4248
Dan Gohman76c315a2010-05-20 20:52:00 +00004249/// Expand - Emit instructions for the leading candidate expression for this
4250/// LSRUse (this is called "expanding").
Dan Gohmand96eae82010-04-09 02:00:38 +00004251Value *LSRInstance::Expand(const LSRFixup &LF,
4252 const Formula &F,
4253 BasicBlock::iterator IP,
4254 SCEVExpander &Rewriter,
4255 SmallVectorImpl<WeakVH> &DeadInsts) const {
4256 const LSRUse &LU = Uses[LF.LUIdx];
4257
4258 // Determine an input position which will be dominated by the operands and
4259 // which will dominate the result.
Andrew Trickb5c26ef2012-01-20 07:41:13 +00004260 IP = AdjustInsertPositionForExpand(IP, LF, LU, Rewriter);
Dan Gohmand96eae82010-04-09 02:00:38 +00004261
Dan Gohman572645c2010-02-12 10:34:29 +00004262 // Inform the Rewriter if we have a post-increment use, so that it can
4263 // perform an advantageous expansion.
Dan Gohman448db1c2010-04-07 22:27:08 +00004264 Rewriter.setPostInc(LF.PostIncLoops);
Dan Gohman572645c2010-02-12 10:34:29 +00004265
4266 // This is the type that the user actually needs.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004267 Type *OpTy = LF.OperandValToReplace->getType();
Dan Gohman572645c2010-02-12 10:34:29 +00004268 // This will be the type that we'll initially expand to.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004269 Type *Ty = F.getType();
Dan Gohman572645c2010-02-12 10:34:29 +00004270 if (!Ty)
4271 // No type known; just expand directly to the ultimate type.
4272 Ty = OpTy;
4273 else if (SE.getEffectiveSCEVType(Ty) == SE.getEffectiveSCEVType(OpTy))
4274 // Expand directly to the ultimate type if it's the right size.
4275 Ty = OpTy;
4276 // This is the type to do integer arithmetic in.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004277 Type *IntTy = SE.getEffectiveSCEVType(Ty);
Dan Gohman572645c2010-02-12 10:34:29 +00004278
4279 // Build up a list of operands to add together to form the full base.
4280 SmallVector<const SCEV *, 8> Ops;
4281
4282 // Expand the BaseRegs portion.
4283 for (SmallVectorImpl<const SCEV *>::const_iterator I = F.BaseRegs.begin(),
4284 E = F.BaseRegs.end(); I != E; ++I) {
4285 const SCEV *Reg = *I;
4286 assert(!Reg->isZero() && "Zero allocated in a base register!");
4287
Dan Gohman448db1c2010-04-07 22:27:08 +00004288 // If we're expanding for a post-inc user, make the post-inc adjustment.
4289 PostIncLoopSet &Loops = const_cast<PostIncLoopSet &>(LF.PostIncLoops);
4290 Reg = TransformForPostIncUse(Denormalize, Reg,
4291 LF.UserInst, LF.OperandValToReplace,
4292 Loops, SE, DT);
Dan Gohman572645c2010-02-12 10:34:29 +00004293
4294 Ops.push_back(SE.getUnknown(Rewriter.expandCodeFor(Reg, 0, IP)));
4295 }
4296
4297 // Expand the ScaledReg portion.
4298 Value *ICmpScaledV = 0;
Chandler Carrutha07dcb12013-01-07 15:04:40 +00004299 if (F.Scale != 0) {
Dan Gohman572645c2010-02-12 10:34:29 +00004300 const SCEV *ScaledS = F.ScaledReg;
4301
Dan Gohman448db1c2010-04-07 22:27:08 +00004302 // If we're expanding for a post-inc user, make the post-inc adjustment.
4303 PostIncLoopSet &Loops = const_cast<PostIncLoopSet &>(LF.PostIncLoops);
4304 ScaledS = TransformForPostIncUse(Denormalize, ScaledS,
4305 LF.UserInst, LF.OperandValToReplace,
4306 Loops, SE, DT);
Dan Gohman572645c2010-02-12 10:34:29 +00004307
4308 if (LU.Kind == LSRUse::ICmpZero) {
4309 // An interesting way of "folding" with an icmp is to use a negated
4310 // scale, which we'll implement by inserting it into the other operand
4311 // of the icmp.
Chandler Carrutha07dcb12013-01-07 15:04:40 +00004312 assert(F.Scale == -1 &&
Dan Gohman572645c2010-02-12 10:34:29 +00004313 "The only scale supported by ICmpZero uses is -1!");
4314 ICmpScaledV = Rewriter.expandCodeFor(ScaledS, 0, IP);
4315 } else {
4316 // Otherwise just expand the scaled register and an explicit scale,
4317 // which is expected to be matched as part of the address.
Andrew Trickb6b5b7b2012-06-15 20:07:29 +00004318
4319 // Flush the operand list to suppress SCEVExpander hoisting address modes.
4320 if (!Ops.empty() && LU.Kind == LSRUse::Address) {
4321 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, IP);
4322 Ops.clear();
4323 Ops.push_back(SE.getUnknown(FullV));
4324 }
Dan Gohman572645c2010-02-12 10:34:29 +00004325 ScaledS = SE.getUnknown(Rewriter.expandCodeFor(ScaledS, 0, IP));
4326 ScaledS = SE.getMulExpr(ScaledS,
Chandler Carrutha07dcb12013-01-07 15:04:40 +00004327 SE.getConstant(ScaledS->getType(), F.Scale));
Dan Gohman572645c2010-02-12 10:34:29 +00004328 Ops.push_back(ScaledS);
4329 }
4330 }
4331
Dan Gohman087bd1e2010-03-03 05:29:13 +00004332 // Expand the GV portion.
Chandler Carrutha07dcb12013-01-07 15:04:40 +00004333 if (F.BaseGV) {
Dan Gohman087bd1e2010-03-03 05:29:13 +00004334 // Flush the operand list to suppress SCEVExpander hoisting.
Andrew Trickb6b5b7b2012-06-15 20:07:29 +00004335 if (!Ops.empty()) {
4336 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, IP);
4337 Ops.clear();
4338 Ops.push_back(SE.getUnknown(FullV));
4339 }
Chandler Carrutha07dcb12013-01-07 15:04:40 +00004340 Ops.push_back(SE.getUnknown(F.BaseGV));
Andrew Trickb6b5b7b2012-06-15 20:07:29 +00004341 }
4342
4343 // Flush the operand list to suppress SCEVExpander hoisting of both folded and
4344 // unfolded offsets. LSR assumes they both live next to their uses.
4345 if (!Ops.empty()) {
Dan Gohman087bd1e2010-03-03 05:29:13 +00004346 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, IP);
4347 Ops.clear();
4348 Ops.push_back(SE.getUnknown(FullV));
4349 }
4350
4351 // Expand the immediate portion.
Chandler Carrutha07dcb12013-01-07 15:04:40 +00004352 int64_t Offset = (uint64_t)F.BaseOffset + LF.Offset;
Dan Gohman572645c2010-02-12 10:34:29 +00004353 if (Offset != 0) {
4354 if (LU.Kind == LSRUse::ICmpZero) {
4355 // The other interesting way of "folding" with an ICmpZero is to use a
4356 // negated immediate.
4357 if (!ICmpScaledV)
Eli Friedmandae36ba2011-10-13 23:48:33 +00004358 ICmpScaledV = ConstantInt::get(IntTy, -(uint64_t)Offset);
Dan Gohman572645c2010-02-12 10:34:29 +00004359 else {
4360 Ops.push_back(SE.getUnknown(ICmpScaledV));
4361 ICmpScaledV = ConstantInt::get(IntTy, Offset);
4362 }
4363 } else {
4364 // Just add the immediate values. These again are expected to be matched
4365 // as part of the address.
Dan Gohman087bd1e2010-03-03 05:29:13 +00004366 Ops.push_back(SE.getUnknown(ConstantInt::getSigned(IntTy, Offset)));
Dan Gohman572645c2010-02-12 10:34:29 +00004367 }
4368 }
4369
Dan Gohmancca82142011-05-03 00:46:49 +00004370 // Expand the unfolded offset portion.
4371 int64_t UnfoldedOffset = F.UnfoldedOffset;
4372 if (UnfoldedOffset != 0) {
4373 // Just add the immediate values.
4374 Ops.push_back(SE.getUnknown(ConstantInt::getSigned(IntTy,
4375 UnfoldedOffset)));
4376 }
4377
Dan Gohman572645c2010-02-12 10:34:29 +00004378 // Emit instructions summing all the operands.
4379 const SCEV *FullS = Ops.empty() ?
Dan Gohmandeff6212010-05-03 22:09:21 +00004380 SE.getConstant(IntTy, 0) :
Dan Gohman572645c2010-02-12 10:34:29 +00004381 SE.getAddExpr(Ops);
4382 Value *FullV = Rewriter.expandCodeFor(FullS, Ty, IP);
4383
4384 // We're done expanding now, so reset the rewriter.
Dan Gohman448db1c2010-04-07 22:27:08 +00004385 Rewriter.clearPostInc();
Dan Gohman572645c2010-02-12 10:34:29 +00004386
4387 // An ICmpZero Formula represents an ICmp which we're handling as a
4388 // comparison against zero. Now that we've expanded an expression for that
4389 // form, update the ICmp's other operand.
4390 if (LU.Kind == LSRUse::ICmpZero) {
4391 ICmpInst *CI = cast<ICmpInst>(LF.UserInst);
4392 DeadInsts.push_back(CI->getOperand(1));
Chandler Carrutha07dcb12013-01-07 15:04:40 +00004393 assert(!F.BaseGV && "ICmp does not support folding a global value and "
Dan Gohman572645c2010-02-12 10:34:29 +00004394 "a scale at the same time!");
Chandler Carrutha07dcb12013-01-07 15:04:40 +00004395 if (F.Scale == -1) {
Dan Gohman572645c2010-02-12 10:34:29 +00004396 if (ICmpScaledV->getType() != OpTy) {
4397 Instruction *Cast =
4398 CastInst::Create(CastInst::getCastOpcode(ICmpScaledV, false,
4399 OpTy, false),
4400 ICmpScaledV, OpTy, "tmp", CI);
4401 ICmpScaledV = Cast;
4402 }
4403 CI->setOperand(1, ICmpScaledV);
4404 } else {
Chandler Carrutha07dcb12013-01-07 15:04:40 +00004405 assert(F.Scale == 0 &&
Dan Gohman572645c2010-02-12 10:34:29 +00004406 "ICmp does not support folding a global value and "
4407 "a scale at the same time!");
4408 Constant *C = ConstantInt::getSigned(SE.getEffectiveSCEVType(OpTy),
4409 -(uint64_t)Offset);
4410 if (C->getType() != OpTy)
4411 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
4412 OpTy, false),
4413 C, OpTy);
4414
4415 CI->setOperand(1, C);
4416 }
4417 }
4418
4419 return FullV;
4420}
4421
Dan Gohman3a02cbc2010-02-16 20:25:07 +00004422/// RewriteForPHI - Helper for Rewrite. PHI nodes are special because the use
4423/// of their operands effectively happens in their predecessor blocks, so the
4424/// expression may need to be expanded in multiple places.
4425void LSRInstance::RewriteForPHI(PHINode *PN,
4426 const LSRFixup &LF,
4427 const Formula &F,
Dan Gohman3a02cbc2010-02-16 20:25:07 +00004428 SCEVExpander &Rewriter,
4429 SmallVectorImpl<WeakVH> &DeadInsts,
Dan Gohman3a02cbc2010-02-16 20:25:07 +00004430 Pass *P) const {
4431 DenseMap<BasicBlock *, Value *> Inserted;
4432 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
4433 if (PN->getIncomingValue(i) == LF.OperandValToReplace) {
4434 BasicBlock *BB = PN->getIncomingBlock(i);
4435
4436 // If this is a critical edge, split the edge so that we do not insert
4437 // the code on all predecessor/successor paths. We do this unless this
4438 // is the canonical backedge for this loop, which complicates post-inc
4439 // users.
4440 if (e != 1 && BB->getTerminator()->getNumSuccessors() > 1 &&
Dan Gohman3ef98382011-02-08 00:55:13 +00004441 !isa<IndirectBrInst>(BB->getTerminator())) {
Bill Wendling89d44112011-08-25 01:08:34 +00004442 BasicBlock *Parent = PN->getParent();
4443 Loop *PNLoop = LI.getLoopFor(Parent);
4444 if (!PNLoop || Parent != PNLoop->getHeader()) {
Dan Gohman3ef98382011-02-08 00:55:13 +00004445 // Split the critical edge.
Bill Wendling8b6af8a2011-08-25 05:55:40 +00004446 BasicBlock *NewBB = 0;
4447 if (!Parent->isLandingPad()) {
Andrew Trickf143b792011-10-04 03:50:44 +00004448 NewBB = SplitCriticalEdge(BB, Parent, P,
4449 /*MergeIdenticalEdges=*/true,
4450 /*DontDeleteUselessPhis=*/true);
Bill Wendling8b6af8a2011-08-25 05:55:40 +00004451 } else {
4452 SmallVector<BasicBlock*, 2> NewBBs;
4453 SplitLandingPadPredecessors(Parent, BB, "", "", P, NewBBs);
4454 NewBB = NewBBs[0];
4455 }
Andrew Trickf08c1152012-09-18 17:51:33 +00004456 // If NewBB==NULL, then SplitCriticalEdge refused to split because all
4457 // phi predecessors are identical. The simple thing to do is skip
4458 // splitting in this case rather than complicate the API.
4459 if (NewBB) {
4460 // If PN is outside of the loop and BB is in the loop, we want to
4461 // move the block to be immediately before the PHI block, not
4462 // immediately after BB.
4463 if (L->contains(BB) && !L->contains(PN))
4464 NewBB->moveBefore(PN->getParent());
Dan Gohman3a02cbc2010-02-16 20:25:07 +00004465
Andrew Trickf08c1152012-09-18 17:51:33 +00004466 // Splitting the edge can reduce the number of PHI entries we have.
4467 e = PN->getNumIncomingValues();
4468 BB = NewBB;
4469 i = PN->getBasicBlockIndex(BB);
4470 }
Dan Gohman3ef98382011-02-08 00:55:13 +00004471 }
Dan Gohman3a02cbc2010-02-16 20:25:07 +00004472 }
4473
4474 std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> Pair =
4475 Inserted.insert(std::make_pair(BB, static_cast<Value *>(0)));
4476 if (!Pair.second)
4477 PN->setIncomingValue(i, Pair.first->second);
4478 else {
Dan Gohman454d26d2010-02-22 04:11:59 +00004479 Value *FullV = Expand(LF, F, BB->getTerminator(), Rewriter, DeadInsts);
Dan Gohman3a02cbc2010-02-16 20:25:07 +00004480
4481 // If this is reuse-by-noop-cast, insert the noop cast.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004482 Type *OpTy = LF.OperandValToReplace->getType();
Dan Gohman3a02cbc2010-02-16 20:25:07 +00004483 if (FullV->getType() != OpTy)
4484 FullV =
4485 CastInst::Create(CastInst::getCastOpcode(FullV, false,
4486 OpTy, false),
4487 FullV, LF.OperandValToReplace->getType(),
4488 "tmp", BB->getTerminator());
4489
4490 PN->setIncomingValue(i, FullV);
4491 Pair.first->second = FullV;
4492 }
4493 }
4494}
4495
Dan Gohman572645c2010-02-12 10:34:29 +00004496/// Rewrite - Emit instructions for the leading candidate expression for this
4497/// LSRUse (this is called "expanding"), and update the UserInst to reference
4498/// the newly expanded value.
4499void LSRInstance::Rewrite(const LSRFixup &LF,
4500 const Formula &F,
Dan Gohman572645c2010-02-12 10:34:29 +00004501 SCEVExpander &Rewriter,
4502 SmallVectorImpl<WeakVH> &DeadInsts,
Dan Gohman572645c2010-02-12 10:34:29 +00004503 Pass *P) const {
Dan Gohman572645c2010-02-12 10:34:29 +00004504 // First, find an insertion point that dominates UserInst. For PHI nodes,
4505 // find the nearest block which dominates all the relevant uses.
4506 if (PHINode *PN = dyn_cast<PHINode>(LF.UserInst)) {
Dan Gohman454d26d2010-02-22 04:11:59 +00004507 RewriteForPHI(PN, LF, F, Rewriter, DeadInsts, P);
Dan Gohman572645c2010-02-12 10:34:29 +00004508 } else {
Dan Gohman454d26d2010-02-22 04:11:59 +00004509 Value *FullV = Expand(LF, F, LF.UserInst, Rewriter, DeadInsts);
Dan Gohman572645c2010-02-12 10:34:29 +00004510
4511 // If this is reuse-by-noop-cast, insert the noop cast.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004512 Type *OpTy = LF.OperandValToReplace->getType();
Dan Gohman572645c2010-02-12 10:34:29 +00004513 if (FullV->getType() != OpTy) {
4514 Instruction *Cast =
4515 CastInst::Create(CastInst::getCastOpcode(FullV, false, OpTy, false),
4516 FullV, OpTy, "tmp", LF.UserInst);
4517 FullV = Cast;
4518 }
4519
4520 // Update the user. ICmpZero is handled specially here (for now) because
4521 // Expand may have updated one of the operands of the icmp already, and
4522 // its new value may happen to be equal to LF.OperandValToReplace, in
4523 // which case doing replaceUsesOfWith leads to replacing both operands
4524 // with the same value. TODO: Reorganize this.
4525 if (Uses[LF.LUIdx].Kind == LSRUse::ICmpZero)
4526 LF.UserInst->setOperand(0, FullV);
4527 else
4528 LF.UserInst->replaceUsesOfWith(LF.OperandValToReplace, FullV);
4529 }
4530
4531 DeadInsts.push_back(LF.OperandValToReplace);
4532}
4533
Dan Gohman76c315a2010-05-20 20:52:00 +00004534/// ImplementSolution - Rewrite all the fixup locations with new values,
4535/// following the chosen solution.
Dan Gohman572645c2010-02-12 10:34:29 +00004536void
4537LSRInstance::ImplementSolution(const SmallVectorImpl<const Formula *> &Solution,
4538 Pass *P) {
4539 // Keep track of instructions we may have made dead, so that
4540 // we can remove them after we are done working.
4541 SmallVector<WeakVH, 16> DeadInsts;
4542
Andrew Trick5e7645b2011-06-28 05:07:32 +00004543 SCEVExpander Rewriter(SE, "lsr");
Andrew Trick8bf295b2012-01-09 18:58:16 +00004544#ifndef NDEBUG
4545 Rewriter.setDebugType(DEBUG_TYPE);
4546#endif
Dan Gohman572645c2010-02-12 10:34:29 +00004547 Rewriter.disableCanonicalMode();
Andrew Trickc5701912011-10-07 23:46:21 +00004548 Rewriter.enableLSRMode();
Dan Gohman572645c2010-02-12 10:34:29 +00004549 Rewriter.setIVIncInsertPos(L, IVIncInsertPos);
4550
Andrew Trick64925c52012-01-10 01:45:08 +00004551 // Mark phi nodes that terminate chains so the expander tries to reuse them.
4552 for (SmallVectorImpl<IVChain>::const_iterator ChainI = IVChainVec.begin(),
4553 ChainE = IVChainVec.end(); ChainI != ChainE; ++ChainI) {
Jakob Stoklund Olesen70a18602012-04-26 23:33:09 +00004554 if (PHINode *PN = dyn_cast<PHINode>(ChainI->tailUserInst()))
Andrew Trick64925c52012-01-10 01:45:08 +00004555 Rewriter.setChainedPhi(PN);
4556 }
4557
Dan Gohman572645c2010-02-12 10:34:29 +00004558 // Expand the new value definitions and update the users.
Dan Gohman402d4352010-05-20 20:33:18 +00004559 for (SmallVectorImpl<LSRFixup>::const_iterator I = Fixups.begin(),
4560 E = Fixups.end(); I != E; ++I) {
4561 const LSRFixup &Fixup = *I;
Dan Gohman572645c2010-02-12 10:34:29 +00004562
Dan Gohman402d4352010-05-20 20:33:18 +00004563 Rewrite(Fixup, *Solution[Fixup.LUIdx], Rewriter, DeadInsts, P);
Dan Gohman572645c2010-02-12 10:34:29 +00004564
4565 Changed = true;
4566 }
4567
Andrew Trick22d20c22012-01-09 21:18:52 +00004568 for (SmallVectorImpl<IVChain>::const_iterator ChainI = IVChainVec.begin(),
4569 ChainE = IVChainVec.end(); ChainI != ChainE; ++ChainI) {
4570 GenerateIVChain(*ChainI, Rewriter, DeadInsts);
4571 Changed = true;
4572 }
Dan Gohman572645c2010-02-12 10:34:29 +00004573 // Clean up after ourselves. This must be done before deleting any
4574 // instructions.
4575 Rewriter.clear();
4576
4577 Changed |= DeleteTriviallyDeadInstructions(DeadInsts);
4578}
4579
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00004580LSRInstance::LSRInstance(Loop *L, Pass *P)
4581 : IU(P->getAnalysis<IVUsers>()), SE(P->getAnalysis<ScalarEvolution>()),
4582 DT(P->getAnalysis<DominatorTree>()), LI(P->getAnalysis<LoopInfo>()),
4583 TTI(P->getAnalysis<TargetTransformInfo>()), L(L), Changed(false),
4584 IVIncInsertPos(0) {
Dan Gohman03e896b2009-11-05 21:11:53 +00004585 // If LoopSimplify form is not available, stay out of trouble.
Andrew Trickacdb4aa2012-01-07 03:16:50 +00004586 if (!L->isLoopSimplifyForm())
4587 return;
Dan Gohman03e896b2009-11-05 21:11:53 +00004588
Andrew Trick75ae2032012-03-16 03:16:56 +00004589 // If there's no interesting work to be done, bail early.
4590 if (IU.empty()) return;
4591
Andrew Trickb5122632012-04-18 04:00:10 +00004592 // If there's too much analysis to be done, bail early. We won't be able to
4593 // model the problem anyway.
4594 unsigned NumUsers = 0;
4595 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) {
4596 if (++NumUsers > MaxIVUsers) {
4597 DEBUG(dbgs() << "LSR skipping loop, too many IV Users in " << *L
4598 << "\n");
4599 return;
4600 }
4601 }
4602
Andrew Trick75ae2032012-03-16 03:16:56 +00004603#ifndef NDEBUG
Andrew Trick0f080912012-01-17 06:45:52 +00004604 // All dominating loops must have preheaders, or SCEVExpander may not be able
4605 // to materialize an AddRecExpr whose Start is an outer AddRecExpr.
4606 //
Andrew Trick75ae2032012-03-16 03:16:56 +00004607 // IVUsers analysis should only create users that are dominated by simple loop
4608 // headers. Since this loop should dominate all of its users, its user list
4609 // should be empty if this loop itself is not within a simple loop nest.
Andrew Trick0f080912012-01-17 06:45:52 +00004610 for (DomTreeNode *Rung = DT.getNode(L->getLoopPreheader());
4611 Rung; Rung = Rung->getIDom()) {
4612 BasicBlock *BB = Rung->getBlock();
4613 const Loop *DomLoop = LI.getLoopFor(BB);
4614 if (DomLoop && DomLoop->getHeader() == BB) {
Andrew Trick75ae2032012-03-16 03:16:56 +00004615 assert(DomLoop->getLoopPreheader() && "LSR needs a simplified loop nest");
Andrew Trick0f080912012-01-17 06:45:52 +00004616 }
Andrew Trickacdb4aa2012-01-07 03:16:50 +00004617 }
Andrew Trick75ae2032012-03-16 03:16:56 +00004618#endif // DEBUG
Dan Gohman80b0f8c2009-03-09 20:34:59 +00004619
Dan Gohman572645c2010-02-12 10:34:29 +00004620 DEBUG(dbgs() << "\nLSR on loop ";
4621 WriteAsOperand(dbgs(), L->getHeader(), /*PrintType=*/false);
4622 dbgs() << ":\n");
Dan Gohmanf7912df2009-03-09 20:46:50 +00004623
Dan Gohman402d4352010-05-20 20:33:18 +00004624 // First, perform some low-level loop optimizations.
Dan Gohman572645c2010-02-12 10:34:29 +00004625 OptimizeShadowIV();
Dan Gohmanc6519f92010-05-20 20:05:31 +00004626 OptimizeLoopTermCond();
Evan Cheng5792f512009-05-11 22:33:01 +00004627
Andrew Trick37eb38d2011-07-21 00:40:04 +00004628 // If loop preparation eliminates all interesting IV users, bail.
4629 if (IU.empty()) return;
4630
Andrew Trick5219f862011-09-29 01:53:08 +00004631 // Skip nested loops until we can model them better with formulae.
Andrew Trickbd618f12012-03-22 22:42:45 +00004632 if (!L->empty()) {
Andrew Trick0c01bc32011-09-29 01:33:38 +00004633 DEBUG(dbgs() << "LSR skipping outer loop " << *L << "\n");
Andrew Trick5219f862011-09-29 01:53:08 +00004634 return;
Andrew Trick0c01bc32011-09-29 01:33:38 +00004635 }
4636
Dan Gohman402d4352010-05-20 20:33:18 +00004637 // Start collecting data and preparing for the solver.
Andrew Trick6c7d0ae2012-01-09 19:50:34 +00004638 CollectChains();
Dan Gohman572645c2010-02-12 10:34:29 +00004639 CollectInterestingTypesAndFactors();
4640 CollectFixupsAndInitialFormulae();
4641 CollectLoopInvariantFixupsAndFormulae();
Chris Lattner010de252005-08-08 05:28:22 +00004642
Andrew Trick22d20c22012-01-09 21:18:52 +00004643 assert(!Uses.empty() && "IVUsers reported at least one use");
Dan Gohman572645c2010-02-12 10:34:29 +00004644 DEBUG(dbgs() << "LSR found " << Uses.size() << " uses:\n";
4645 print_uses(dbgs()));
Misha Brukmanfd939082005-04-21 23:48:37 +00004646
Dan Gohman572645c2010-02-12 10:34:29 +00004647 // Now use the reuse data to generate a bunch of interesting ways
4648 // to formulate the values needed for the uses.
4649 GenerateAllReuseFormulae();
Evan Chengd1d6b5c2006-03-16 21:53:05 +00004650
Dan Gohman572645c2010-02-12 10:34:29 +00004651 FilterOutUndesirableDedicatedRegisters();
4652 NarrowSearchSpaceUsingHeuristics();
Dan Gohman6bec5bb2009-12-18 00:06:20 +00004653
Dan Gohman572645c2010-02-12 10:34:29 +00004654 SmallVector<const Formula *, 8> Solution;
4655 Solve(Solution);
Dan Gohman6bec5bb2009-12-18 00:06:20 +00004656
Dan Gohman572645c2010-02-12 10:34:29 +00004657 // Release memory that is no longer needed.
4658 Factors.clear();
4659 Types.clear();
4660 RegUses.clear();
4661
Andrew Trick80ef1b22011-09-27 00:44:14 +00004662 if (Solution.empty())
4663 return;
4664
Dan Gohman572645c2010-02-12 10:34:29 +00004665#ifndef NDEBUG
4666 // Formulae should be legal.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00004667 for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(), E = Uses.end();
4668 I != E; ++I) {
4669 const LSRUse &LU = *I;
4670 for (SmallVectorImpl<Formula>::const_iterator J = LU.Formulae.begin(),
4671 JE = LU.Formulae.end();
4672 J != JE; ++J)
4673 assert(isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
4674 *J) && "Illegal formula generated!");
Dan Gohman572645c2010-02-12 10:34:29 +00004675 };
4676#endif
4677
4678 // Now that we've decided what we want, make it so.
4679 ImplementSolution(Solution, P);
4680}
4681
4682void LSRInstance::print_factors_and_types(raw_ostream &OS) const {
4683 if (Factors.empty() && Types.empty()) return;
4684
4685 OS << "LSR has identified the following interesting factors and types: ";
4686 bool First = true;
4687
4688 for (SmallSetVector<int64_t, 8>::const_iterator
4689 I = Factors.begin(), E = Factors.end(); I != E; ++I) {
4690 if (!First) OS << ", ";
4691 First = false;
4692 OS << '*' << *I;
Evan Cheng81ebdcf2009-11-10 21:14:05 +00004693 }
Dale Johannesenc1acc3f2009-05-11 17:15:42 +00004694
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004695 for (SmallSetVector<Type *, 4>::const_iterator
Dan Gohman572645c2010-02-12 10:34:29 +00004696 I = Types.begin(), E = Types.end(); I != E; ++I) {
4697 if (!First) OS << ", ";
4698 First = false;
4699 OS << '(' << **I << ')';
4700 }
4701 OS << '\n';
4702}
4703
4704void LSRInstance::print_fixups(raw_ostream &OS) const {
4705 OS << "LSR is examining the following fixup sites:\n";
4706 for (SmallVectorImpl<LSRFixup>::const_iterator I = Fixups.begin(),
4707 E = Fixups.end(); I != E; ++I) {
Dan Gohman572645c2010-02-12 10:34:29 +00004708 dbgs() << " ";
Dan Gohman9f383eb2010-05-20 22:25:20 +00004709 I->print(OS);
Dan Gohman572645c2010-02-12 10:34:29 +00004710 OS << '\n';
4711 }
4712}
4713
4714void LSRInstance::print_uses(raw_ostream &OS) const {
4715 OS << "LSR is examining the following uses:\n";
4716 for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(),
4717 E = Uses.end(); I != E; ++I) {
4718 const LSRUse &LU = *I;
4719 dbgs() << " ";
4720 LU.print(OS);
4721 OS << '\n';
4722 for (SmallVectorImpl<Formula>::const_iterator J = LU.Formulae.begin(),
4723 JE = LU.Formulae.end(); J != JE; ++J) {
4724 OS << " ";
4725 J->print(OS);
4726 OS << '\n';
4727 }
4728 }
4729}
4730
4731void LSRInstance::print(raw_ostream &OS) const {
4732 print_factors_and_types(OS);
4733 print_fixups(OS);
4734 print_uses(OS);
4735}
4736
Manman Ren286c4dc2012-09-12 05:06:18 +00004737#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman572645c2010-02-12 10:34:29 +00004738void LSRInstance::dump() const {
4739 print(errs()); errs() << '\n';
4740}
Manman Rencc77eec2012-09-06 19:55:56 +00004741#endif
Dan Gohman572645c2010-02-12 10:34:29 +00004742
4743namespace {
4744
4745class LoopStrengthReduce : public LoopPass {
Dan Gohman572645c2010-02-12 10:34:29 +00004746public:
4747 static char ID; // Pass ID, replacement for typeid
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00004748 LoopStrengthReduce();
Dan Gohman572645c2010-02-12 10:34:29 +00004749
4750private:
4751 bool runOnLoop(Loop *L, LPPassManager &LPM);
4752 void getAnalysisUsage(AnalysisUsage &AU) const;
4753};
4754
4755}
4756
4757char LoopStrengthReduce::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +00004758INITIALIZE_PASS_BEGIN(LoopStrengthReduce, "loop-reduce",
Owen Andersonce665bd2010-10-07 22:25:06 +00004759 "Loop Strength Reduction", false, false)
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00004760INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
Owen Anderson2ab36d32010-10-12 19:48:12 +00004761INITIALIZE_PASS_DEPENDENCY(DominatorTree)
4762INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
4763INITIALIZE_PASS_DEPENDENCY(IVUsers)
Owen Anderson205942a2010-10-19 20:08:44 +00004764INITIALIZE_PASS_DEPENDENCY(LoopInfo)
4765INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
Owen Anderson2ab36d32010-10-12 19:48:12 +00004766INITIALIZE_PASS_END(LoopStrengthReduce, "loop-reduce",
4767 "Loop Strength Reduction", false, false)
4768
Nadav Rotema04a4a72012-10-19 21:28:43 +00004769
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00004770Pass *llvm::createLoopStrengthReducePass() {
4771 return new LoopStrengthReduce();
Dan Gohman572645c2010-02-12 10:34:29 +00004772}
4773
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00004774LoopStrengthReduce::LoopStrengthReduce() : LoopPass(ID) {
4775 initializeLoopStrengthReducePass(*PassRegistry::getPassRegistry());
4776}
Dan Gohman572645c2010-02-12 10:34:29 +00004777
4778void LoopStrengthReduce::getAnalysisUsage(AnalysisUsage &AU) const {
4779 // We split critical edges, so we change the CFG. However, we do update
4780 // many analyses if they are around.
Eric Christopher6793c492011-02-10 01:48:24 +00004781 AU.addPreservedID(LoopSimplifyID);
Dan Gohman572645c2010-02-12 10:34:29 +00004782
Eric Christopher6793c492011-02-10 01:48:24 +00004783 AU.addRequired<LoopInfo>();
4784 AU.addPreserved<LoopInfo>();
4785 AU.addRequiredID(LoopSimplifyID);
Dan Gohman572645c2010-02-12 10:34:29 +00004786 AU.addRequired<DominatorTree>();
4787 AU.addPreserved<DominatorTree>();
4788 AU.addRequired<ScalarEvolution>();
4789 AU.addPreserved<ScalarEvolution>();
Cameron Zwarich2c2b9332011-02-10 23:53:14 +00004790 // Requiring LoopSimplify a second time here prevents IVUsers from running
4791 // twice, since LoopSimplify was invalidated by running ScalarEvolution.
4792 AU.addRequiredID(LoopSimplifyID);
Dan Gohman572645c2010-02-12 10:34:29 +00004793 AU.addRequired<IVUsers>();
4794 AU.addPreserved<IVUsers>();
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00004795 AU.addRequired<TargetTransformInfo>();
Dan Gohman572645c2010-02-12 10:34:29 +00004796}
4797
4798bool LoopStrengthReduce::runOnLoop(Loop *L, LPPassManager & /*LPM*/) {
4799 bool Changed = false;
4800
4801 // Run the main LSR transformation.
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00004802 Changed |= LSRInstance(L, this).getChanged();
Dan Gohman572645c2010-02-12 10:34:29 +00004803
Andrew Trickf231a6d2012-01-07 01:36:44 +00004804 // Remove any extra phis created by processing inner loops.
Dan Gohman9fff2182010-01-05 16:31:45 +00004805 Changed |= DeleteDeadPHIs(L->getHeader());
Andrew Trickc6b49362013-01-06 05:59:39 +00004806 if (EnablePhiElim && L->isLoopSimplifyForm()) {
Andrew Trickf231a6d2012-01-07 01:36:44 +00004807 SmallVector<WeakVH, 16> DeadInsts;
4808 SCEVExpander Rewriter(getAnalysis<ScalarEvolution>(), "lsr");
4809#ifndef NDEBUG
4810 Rewriter.setDebugType(DEBUG_TYPE);
4811#endif
Chandler Carruthe4ba75f2013-01-07 14:41:08 +00004812 unsigned numFolded =
4813 Rewriter.replaceCongruentIVs(L, &getAnalysis<DominatorTree>(),
4814 DeadInsts,
4815 &getAnalysis<TargetTransformInfo>());
Andrew Trickf231a6d2012-01-07 01:36:44 +00004816 if (numFolded) {
4817 Changed = true;
4818 DeleteTriviallyDeadInstructions(DeadInsts);
4819 DeleteDeadPHIs(L->getHeader());
4820 }
4821 }
Evan Cheng1ce75dc2008-07-07 19:51:32 +00004822 return Changed;
Nate Begemaneaa13852004-10-18 21:08:22 +00004823}