1 //===- LoopStrengthReduce.cpp - Strength Reduce IVs in Loops --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This transformation analyzes and transforms the induction variables (and
11 // computations derived from them) into forms suitable for efficient execution
14 // This pass performs a strength reduction on array references inside loops that
15 // have as one or more of their components the loop induction variable, it
16 // rewrites expressions to take advantage of scaled-index addressing modes
17 // available on the target, and it performs a variety of other optimizations
18 // related to loop induction variables.
20 // Terminology note: this code has a lot of handling for "post-increment" or
21 // "post-inc" users. This is not talking about post-increment addressing modes;
22 // it is instead talking about code like this:
24 // %i = phi [ 0, %entry ], [ %i.next, %latch ]
26 // %i.next = add %i, 1
27 // %c = icmp eq %i.next, %n
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.
36 // TODO: More sophistication in the way Formulae are generated and filtered.
38 // TODO: Handle multiple loops at a time.
40 // TODO: Should the addressing mode BaseGV be changed to a ConstantExpr instead
43 // TODO: When truncation is free, truncate ICmp users' operands to make it a
44 // smaller encoding (on x86 at least).
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"
54 //===----------------------------------------------------------------------===//
56 #define DEBUG_TYPE "loop-reduce"
57 #include "llvm/Transforms/Scalar.h"
58 #include "llvm/ADT/DenseSet.h"
59 #include "llvm/ADT/SetVector.h"
60 #include "llvm/ADT/SmallBitVector.h"
61 #include "llvm/Analysis/Dominators.h"
62 #include "llvm/Analysis/IVUsers.h"
63 #include "llvm/Analysis/LoopPass.h"
64 #include "llvm/Analysis/ScalarEvolutionExpander.h"
65 #include "llvm/Analysis/TargetTransformInfo.h"
66 #include "llvm/Assembly/Writer.h"
67 #include "llvm/IR/Constants.h"
68 #include "llvm/IR/DerivedTypes.h"
69 #include "llvm/IR/Instructions.h"
70 #include "llvm/IR/IntrinsicInst.h"
71 #include "llvm/Support/CommandLine.h"
72 #include "llvm/Support/Debug.h"
73 #include "llvm/Support/ValueHandle.h"
74 #include "llvm/Support/raw_ostream.h"
75 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
76 #include "llvm/Transforms/Utils/Local.h"
80 /// MaxIVUsers is an arbitrary threshold that provides an early opportunitiy for
81 /// bail out. This threshold is far beyond the number of users that LSR can
82 /// conceivably solve, so it should not affect generated code, but catches the
83 /// worst cases before LSR burns too much compile time and stack space.
84 static const unsigned MaxIVUsers = 200;
86 // Temporary flag to cleanup congruent phis after LSR phi expansion.
87 // It's currently disabled until we can determine whether it's truly useful or
88 // not. The flag should be removed after the v3.0 release.
89 // This is now needed for ivchains.
90 static cl::opt<bool> EnablePhiElim(
91 "enable-lsr-phielim", cl::Hidden, cl::init(true),
92 cl::desc("Enable LSR phi elimination"));
95 // Stress test IV chain generation.
96 static cl::opt<bool> StressIVChain(
97 "stress-ivchain", cl::Hidden, cl::init(false),
98 cl::desc("Stress test LSR IV chains"));
100 static bool StressIVChain = false;
105 /// RegSortData - This class holds data which is used to order reuse candidates.
108 /// UsedByIndices - This represents the set of LSRUse indices which reference
109 /// a particular register.
110 SmallBitVector UsedByIndices;
114 void print(raw_ostream &OS) const;
120 void RegSortData::print(raw_ostream &OS) const {
121 OS << "[NumUses=" << UsedByIndices.count() << ']';
124 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
125 void RegSortData::dump() const {
126 print(errs()); errs() << '\n';
132 /// RegUseTracker - Map register candidates to information about how they are
134 class RegUseTracker {
135 typedef DenseMap<const SCEV *, RegSortData> RegUsesTy;
137 RegUsesTy RegUsesMap;
138 SmallVector<const SCEV *, 16> RegSequence;
141 void CountRegister(const SCEV *Reg, size_t LUIdx);
142 void DropRegister(const SCEV *Reg, size_t LUIdx);
143 void SwapAndDropUse(size_t LUIdx, size_t LastLUIdx);
145 bool isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const;
147 const SmallBitVector &getUsedByIndices(const SCEV *Reg) const;
151 typedef SmallVectorImpl<const SCEV *>::iterator iterator;
152 typedef SmallVectorImpl<const SCEV *>::const_iterator const_iterator;
153 iterator begin() { return RegSequence.begin(); }
154 iterator end() { return RegSequence.end(); }
155 const_iterator begin() const { return RegSequence.begin(); }
156 const_iterator end() const { return RegSequence.end(); }
162 RegUseTracker::CountRegister(const SCEV *Reg, size_t LUIdx) {
163 std::pair<RegUsesTy::iterator, bool> Pair =
164 RegUsesMap.insert(std::make_pair(Reg, RegSortData()));
165 RegSortData &RSD = Pair.first->second;
167 RegSequence.push_back(Reg);
168 RSD.UsedByIndices.resize(std::max(RSD.UsedByIndices.size(), LUIdx + 1));
169 RSD.UsedByIndices.set(LUIdx);
173 RegUseTracker::DropRegister(const SCEV *Reg, size_t LUIdx) {
174 RegUsesTy::iterator It = RegUsesMap.find(Reg);
175 assert(It != RegUsesMap.end());
176 RegSortData &RSD = It->second;
177 assert(RSD.UsedByIndices.size() > LUIdx);
178 RSD.UsedByIndices.reset(LUIdx);
182 RegUseTracker::SwapAndDropUse(size_t LUIdx, size_t LastLUIdx) {
183 assert(LUIdx <= LastLUIdx);
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.
187 for (RegUsesTy::iterator I = RegUsesMap.begin(), E = RegUsesMap.end();
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));
198 RegUseTracker::isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const {
199 RegUsesTy::const_iterator I = RegUsesMap.find(Reg);
200 if (I == RegUsesMap.end())
202 const SmallBitVector &UsedByIndices = I->second.UsedByIndices;
203 int i = UsedByIndices.find_first();
204 if (i == -1) return false;
205 if ((size_t)i != LUIdx) return true;
206 return UsedByIndices.find_next(i) != -1;
209 const SmallBitVector &RegUseTracker::getUsedByIndices(const SCEV *Reg) const {
210 RegUsesTy::const_iterator I = RegUsesMap.find(Reg);
211 assert(I != RegUsesMap.end() && "Unknown register!");
212 return I->second.UsedByIndices;
215 void RegUseTracker::clear() {
222 /// Formula - This class holds information that describes a formula for
223 /// computing satisfying a use. It may include broken-out immediates and scaled
226 /// Global base address used for complex addressing.
229 /// Base offset for complex addressing.
232 /// Whether any complex addressing has a base register.
235 /// The scale of any complex addressing.
238 /// BaseRegs - The list of "base" registers for this use. When this is
240 SmallVector<const SCEV *, 2> BaseRegs;
242 /// ScaledReg - The 'scaled' register for this use. This should be non-null
243 /// when Scale is not zero.
244 const SCEV *ScaledReg;
246 /// UnfoldedOffset - An additional constant offset which added near the
247 /// use. This requires a temporary register, but the offset itself can
248 /// live in an add immediate field rather than a register.
249 int64_t UnfoldedOffset;
252 : BaseGV(0), BaseOffset(0), HasBaseReg(false), Scale(0), ScaledReg(0),
255 void InitialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE);
257 unsigned getNumRegs() const;
258 Type *getType() const;
260 void DeleteBaseReg(const SCEV *&S);
262 bool referencesReg(const SCEV *S) const;
263 bool hasRegsUsedByUsesOtherThan(size_t LUIdx,
264 const RegUseTracker &RegUses) const;
266 void print(raw_ostream &OS) const;
272 /// DoInitialMatch - Recursion helper for InitialMatch.
273 static void DoInitialMatch(const SCEV *S, Loop *L,
274 SmallVectorImpl<const SCEV *> &Good,
275 SmallVectorImpl<const SCEV *> &Bad,
276 ScalarEvolution &SE) {
277 // Collect expressions which properly dominate the loop header.
278 if (SE.properlyDominates(S, L->getHeader())) {
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();
287 DoInitialMatch(*I, L, Good, Bad, SE);
291 // Look at addrec operands.
292 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S))
293 if (!AR->getStart()->isZero()) {
294 DoInitialMatch(AR->getStart(), L, Good, Bad, SE);
295 DoInitialMatch(SE.getAddRecExpr(SE.getConstant(AR->getType(), 0),
296 AR->getStepRecurrence(SE),
297 // FIXME: AR->getNoWrapFlags()
298 AR->getLoop(), SCEV::FlagAnyWrap),
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);
309 SmallVector<const SCEV *, 4> MyGood;
310 SmallVector<const SCEV *, 4> MyBad;
311 DoInitialMatch(NewMul, L, MyGood, MyBad, SE);
312 const SCEV *NegOne = SE.getSCEV(ConstantInt::getAllOnesValue(
313 SE.getEffectiveSCEVType(NewMul->getType())));
314 for (SmallVectorImpl<const SCEV *>::const_iterator I = MyGood.begin(),
315 E = MyGood.end(); I != E; ++I)
316 Good.push_back(SE.getMulExpr(NegOne, *I));
317 for (SmallVectorImpl<const SCEV *>::const_iterator I = MyBad.begin(),
318 E = MyBad.end(); I != E; ++I)
319 Bad.push_back(SE.getMulExpr(NegOne, *I));
323 // Ok, we can't do anything interesting. Just stuff the whole thing into a
324 // register and hope for the best.
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.
331 void Formula::InitialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE) {
332 SmallVector<const SCEV *, 4> Good;
333 SmallVector<const SCEV *, 4> Bad;
334 DoInitialMatch(S, L, Good, Bad, SE);
336 const SCEV *Sum = SE.getAddExpr(Good);
338 BaseRegs.push_back(Sum);
342 const SCEV *Sum = SE.getAddExpr(Bad);
344 BaseRegs.push_back(Sum);
349 /// getNumRegs - Return the total number of register operands used by this
350 /// formula. This does not include register uses implied by non-constant
352 unsigned Formula::getNumRegs() const {
353 return !!ScaledReg + BaseRegs.size();
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.
358 Type *Formula::getType() const {
359 return !BaseRegs.empty() ? BaseRegs.front()->getType() :
360 ScaledReg ? ScaledReg->getType() :
361 BaseGV ? BaseGV->getType() :
365 /// DeleteBaseReg - Delete the given base reg from the BaseRegs list.
366 void Formula::DeleteBaseReg(const SCEV *&S) {
367 if (&S != &BaseRegs.back())
368 std::swap(S, BaseRegs.back());
372 /// referencesReg - Test if this formula references the given register.
373 bool Formula::referencesReg(const SCEV *S) const {
374 return S == ScaledReg ||
375 std::find(BaseRegs.begin(), BaseRegs.end(), S) != BaseRegs.end();
378 /// hasRegsUsedByUsesOtherThan - Test whether this formula uses registers
379 /// which are used by uses other than the use with the given index.
380 bool Formula::hasRegsUsedByUsesOtherThan(size_t LUIdx,
381 const RegUseTracker &RegUses) const {
383 if (RegUses.isRegUsedByUsesOtherThan(ScaledReg, LUIdx))
385 for (SmallVectorImpl<const SCEV *>::const_iterator I = BaseRegs.begin(),
386 E = BaseRegs.end(); I != E; ++I)
387 if (RegUses.isRegUsedByUsesOtherThan(*I, LUIdx))
392 void Formula::print(raw_ostream &OS) const {
395 if (!First) OS << " + "; else First = false;
396 WriteAsOperand(OS, BaseGV, /*PrintType=*/false);
398 if (BaseOffset != 0) {
399 if (!First) OS << " + "; else First = false;
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 << ')';
407 if (HasBaseReg && BaseRegs.empty()) {
408 if (!First) OS << " + "; else First = false;
409 OS << "**error: HasBaseReg**";
410 } else if (!HasBaseReg && !BaseRegs.empty()) {
411 if (!First) OS << " + "; else First = false;
412 OS << "**error: !HasBaseReg**";
415 if (!First) OS << " + "; else First = false;
416 OS << Scale << "*reg(";
423 if (UnfoldedOffset != 0) {
424 if (!First) OS << " + "; else First = false;
425 OS << "imm(" << UnfoldedOffset << ')';
429 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
430 void Formula::dump() const {
431 print(errs()); errs() << '\n';
435 /// isAddRecSExtable - Return true if the given addrec can be sign-extended
436 /// without changing its value.
437 static bool isAddRecSExtable(const SCEVAddRecExpr *AR, ScalarEvolution &SE) {
439 IntegerType::get(SE.getContext(), SE.getTypeSizeInBits(AR->getType()) + 1);
440 return isa<SCEVAddRecExpr>(SE.getSignExtendExpr(AR, WideTy));
443 /// isAddSExtable - Return true if the given add can be sign-extended
444 /// without changing its value.
445 static bool isAddSExtable(const SCEVAddExpr *A, ScalarEvolution &SE) {
447 IntegerType::get(SE.getContext(), SE.getTypeSizeInBits(A->getType()) + 1);
448 return isa<SCEVAddExpr>(SE.getSignExtendExpr(A, WideTy));
451 /// isMulSExtable - Return true if the given mul can be sign-extended
452 /// without changing its value.
453 static bool isMulSExtable(const SCEVMulExpr *M, ScalarEvolution &SE) {
455 IntegerType::get(SE.getContext(),
456 SE.getTypeSizeInBits(M->getType()) * M->getNumOperands());
457 return isa<SCEVMulExpr>(SE.getSignExtendExpr(M, WideTy));
460 /// getExactSDiv - Return an expression for LHS /s RHS, if it can be determined
461 /// and if the remainder is known to be zero, or null otherwise. If
462 /// IgnoreSignificantBits is true, expressions like (X * Y) /s Y are simplified
463 /// to Y, ignoring that the multiplication may overflow, which is useful when
464 /// the result will be used in a context where the most significant bits are
466 static const SCEV *getExactSDiv(const SCEV *LHS, const SCEV *RHS,
468 bool IgnoreSignificantBits = false) {
469 // Handle the trivial case, which works for any SCEV type.
471 return SE.getConstant(LHS->getType(), 1);
473 // Handle a few RHS special cases.
474 const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS);
476 const APInt &RA = RC->getValue()->getValue();
477 // Handle x /s -1 as x * -1, to give ScalarEvolution a chance to do
479 if (RA.isAllOnesValue())
480 return SE.getMulExpr(LHS, RC);
481 // Handle x /s 1 as x.
486 // Check for a division of a constant by a constant.
487 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(LHS)) {
490 const APInt &LA = C->getValue()->getValue();
491 const APInt &RA = RC->getValue()->getValue();
492 if (LA.srem(RA) != 0)
494 return SE.getConstant(LA.sdiv(RA));
497 // Distribute the sdiv over addrec operands, if the addrec doesn't overflow.
498 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) {
499 if (IgnoreSignificantBits || isAddRecSExtable(AR, SE)) {
500 const SCEV *Step = getExactSDiv(AR->getStepRecurrence(SE), RHS, SE,
501 IgnoreSignificantBits);
503 const SCEV *Start = getExactSDiv(AR->getStart(), RHS, SE,
504 IgnoreSignificantBits);
505 if (!Start) return 0;
506 // FlagNW is independent of the start value, step direction, and is
507 // preserved with smaller magnitude steps.
508 // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
509 return SE.getAddRecExpr(Start, Step, AR->getLoop(), SCEV::FlagAnyWrap);
514 // Distribute the sdiv over add operands, if the add doesn't overflow.
515 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(LHS)) {
516 if (IgnoreSignificantBits || isAddSExtable(Add, SE)) {
517 SmallVector<const SCEV *, 8> Ops;
518 for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
520 const SCEV *Op = getExactSDiv(*I, RHS, SE,
521 IgnoreSignificantBits);
525 return SE.getAddExpr(Ops);
530 // Check for a multiply operand that we can pull RHS out of.
531 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS)) {
532 if (IgnoreSignificantBits || isMulSExtable(Mul, SE)) {
533 SmallVector<const SCEV *, 4> Ops;
535 for (SCEVMulExpr::op_iterator I = Mul->op_begin(), E = Mul->op_end();
539 if (const SCEV *Q = getExactSDiv(S, RHS, SE,
540 IgnoreSignificantBits)) {
546 return Found ? SE.getMulExpr(Ops) : 0;
551 // Otherwise we don't know.
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
558 static int64_t ExtractImmediate(const SCEV *&S, ScalarEvolution &SE) {
559 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
560 if (C->getValue()->getValue().getMinSignedBits() <= 64) {
561 S = SE.getConstant(C->getType(), 0);
562 return C->getValue()->getSExtValue();
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);
568 S = SE.getAddExpr(NewOps);
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);
574 S = SE.getAddRecExpr(NewOps, AR->getLoop(),
575 // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
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
585 static GlobalValue *ExtractSymbol(const SCEV *&S, ScalarEvolution &SE) {
586 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
587 if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue())) {
588 S = SE.getConstant(GV->getType(), 0);
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);
595 S = SE.getAddExpr(NewOps);
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);
601 S = SE.getAddRecExpr(NewOps, AR->getLoop(),
602 // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
609 /// isAddressUse - Returns true if the specified instruction is using the
610 /// specified value as an address.
611 static bool isAddressUse(Instruction *Inst, Value *OperandVal) {
612 bool isAddress = isa<LoadInst>(Inst);
613 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
614 if (SI->getOperand(1) == OperandVal)
616 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
617 // Addressing modes can also be folded into prefetches and a variety
619 switch (II->getIntrinsicID()) {
621 case Intrinsic::prefetch:
622 case Intrinsic::x86_sse_storeu_ps:
623 case Intrinsic::x86_sse2_storeu_pd:
624 case Intrinsic::x86_sse2_storeu_dq:
625 case Intrinsic::x86_sse2_storel_dq:
626 if (II->getArgOperand(0) == OperandVal)
634 /// getAccessType - Return the type of the memory being accessed.
635 static Type *getAccessType(const Instruction *Inst) {
636 Type *AccessTy = Inst->getType();
637 if (const StoreInst *SI = dyn_cast<StoreInst>(Inst))
638 AccessTy = SI->getOperand(0)->getType();
639 else if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
640 // Addressing modes can also be folded into prefetches and a variety
642 switch (II->getIntrinsicID()) {
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:
648 AccessTy = II->getArgOperand(0)->getType();
653 // All pointers have the same requirements, so canonicalize them to an
654 // arbitrary pointer type to minimize variation.
655 if (PointerType *PTy = dyn_cast<PointerType>(AccessTy))
656 AccessTy = PointerType::get(IntegerType::get(PTy->getContext(), 1),
657 PTy->getAddressSpace());
662 /// isExistingPhi - Return true if this AddRec is already a phi in its loop.
663 static bool isExistingPhi(const SCEVAddRecExpr *AR, ScalarEvolution &SE) {
664 for (BasicBlock::iterator I = AR->getLoop()->getHeader()->begin();
665 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
666 if (SE.isSCEVable(PN->getType()) &&
667 (SE.getEffectiveSCEVType(PN->getType()) ==
668 SE.getEffectiveSCEVType(AR->getType())) &&
669 SE.getSCEV(PN) == AR)
675 /// Check if expanding this expression is likely to incur significant cost. This
676 /// is tricky because SCEV doesn't track which expressions are actually computed
677 /// by the current IR.
679 /// We currently allow expansion of IV increments that involve adds,
680 /// multiplication by constants, and AddRecs from existing phis.
682 /// TODO: Allow UDivExpr if we can find an existing IV increment that is an
683 /// obvious multiple of the UDivExpr.
684 static bool isHighCostExpansion(const SCEV *S,
685 SmallPtrSet<const SCEV*, 8> &Processed,
686 ScalarEvolution &SE) {
687 // Zero/One operand expressions
688 switch (S->getSCEVType()) {
693 return isHighCostExpansion(cast<SCEVTruncateExpr>(S)->getOperand(),
696 return isHighCostExpansion(cast<SCEVZeroExtendExpr>(S)->getOperand(),
699 return isHighCostExpansion(cast<SCEVSignExtendExpr>(S)->getOperand(),
703 if (!Processed.insert(S))
706 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
707 for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
709 if (isHighCostExpansion(*I, Processed, SE))
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);
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();
727 // If U is a constant, it may be used by a ConstantExpr.
728 Instruction *User = dyn_cast<Instruction>(*UI);
729 if (User && User->getOpcode() == Instruction::Mul
730 && SE.isSCEVable(User->getType())) {
731 return SE.getSCEV(User) == Mul;
738 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
739 if (isExistingPhi(AR, SE))
743 // Fow now, consider any other type of expression (div/mul/min/max) high cost.
747 /// DeleteTriviallyDeadInstructions - If any of the instructions is the
748 /// specified set are trivially dead, delete them and see if this makes any of
749 /// their operands subsequently dead.
751 DeleteTriviallyDeadInstructions(SmallVectorImpl<WeakVH> &DeadInsts) {
752 bool Changed = false;
754 while (!DeadInsts.empty()) {
755 Value *V = DeadInsts.pop_back_val();
756 Instruction *I = dyn_cast_or_null<Instruction>(V);
758 if (I == 0 || !isInstructionTriviallyDead(I))
761 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
762 if (Instruction *U = dyn_cast<Instruction>(*OI)) {
765 DeadInsts.push_back(U);
768 I->eraseFromParent();
777 /// Cost - This class is used to measure and compare candidate formulae.
779 /// TODO: Some of these could be merged. Also, a lexical ordering
780 /// isn't always optimal.
784 unsigned NumBaseAdds;
790 : NumRegs(0), AddRecCost(0), NumIVMuls(0), NumBaseAdds(0), ImmCost(0),
793 bool operator<(const Cost &Other) const;
798 // Once any of the metrics loses, they must all remain losers.
800 return ((NumRegs | AddRecCost | NumIVMuls | NumBaseAdds
801 | ImmCost | SetupCost) != ~0u)
802 || ((NumRegs & AddRecCost & NumIVMuls & NumBaseAdds
803 & ImmCost & SetupCost) == ~0u);
808 assert(isValid() && "invalid cost");
809 return NumRegs == ~0u;
812 void RateFormula(const Formula &F,
813 SmallPtrSet<const SCEV *, 16> &Regs,
814 const DenseSet<const SCEV *> &VisitedRegs,
816 const SmallVectorImpl<int64_t> &Offsets,
817 ScalarEvolution &SE, DominatorTree &DT,
818 SmallPtrSet<const SCEV *, 16> *LoserRegs = 0);
820 void print(raw_ostream &OS) const;
824 void RateRegister(const SCEV *Reg,
825 SmallPtrSet<const SCEV *, 16> &Regs,
827 ScalarEvolution &SE, DominatorTree &DT);
828 void RatePrimaryRegister(const SCEV *Reg,
829 SmallPtrSet<const SCEV *, 16> &Regs,
831 ScalarEvolution &SE, DominatorTree &DT,
832 SmallPtrSet<const SCEV *, 16> *LoserRegs);
837 /// RateRegister - Tally up interesting quantities from the given register.
838 void Cost::RateRegister(const SCEV *Reg,
839 SmallPtrSet<const SCEV *, 16> &Regs,
841 ScalarEvolution &SE, DominatorTree &DT) {
842 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Reg)) {
843 // If this is an addrec for another loop, don't second-guess its addrec phi
844 // nodes. LSR isn't currently smart enough to reason about more than one
845 // loop at a time. LSR has already run on inner loops, will not run on outer
846 // loops, and cannot be expected to change sibling loops.
847 if (AR->getLoop() != L) {
848 // If the AddRec exists, consider it's register free and leave it alone.
849 if (isExistingPhi(AR, SE))
852 // Otherwise, do not consider this formula at all.
856 AddRecCost += 1; /// TODO: This should be a function of the stride.
858 // Add the step value register, if it needs one.
859 // TODO: The non-affine case isn't precisely modeled here.
860 if (!AR->isAffine() || !isa<SCEVConstant>(AR->getOperand(1))) {
861 if (!Regs.count(AR->getOperand(1))) {
862 RateRegister(AR->getOperand(1), Regs, L, SE, DT);
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()))))
879 NumIVMuls += isa<SCEVMulExpr>(Reg) &&
880 SE.hasComputableLoopEvolution(Reg, L);
883 /// RatePrimaryRegister - Record this register in the set. If we haven't seen it
884 /// before, rate it. Optional LoserRegs provides a way to declare any formula
885 /// that refers to one of those regs an instant loser.
886 void Cost::RatePrimaryRegister(const SCEV *Reg,
887 SmallPtrSet<const SCEV *, 16> &Regs,
889 ScalarEvolution &SE, DominatorTree &DT,
890 SmallPtrSet<const SCEV *, 16> *LoserRegs) {
891 if (LoserRegs && LoserRegs->count(Reg)) {
895 if (Regs.insert(Reg)) {
896 RateRegister(Reg, Regs, L, SE, DT);
898 LoserRegs->insert(Reg);
902 void Cost::RateFormula(const Formula &F,
903 SmallPtrSet<const SCEV *, 16> &Regs,
904 const DenseSet<const SCEV *> &VisitedRegs,
906 const SmallVectorImpl<int64_t> &Offsets,
907 ScalarEvolution &SE, DominatorTree &DT,
908 SmallPtrSet<const SCEV *, 16> *LoserRegs) {
909 // Tally up the registers.
910 if (const SCEV *ScaledReg = F.ScaledReg) {
911 if (VisitedRegs.count(ScaledReg)) {
915 RatePrimaryRegister(ScaledReg, Regs, L, SE, DT, LoserRegs);
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)) {
926 RatePrimaryRegister(BaseReg, Regs, L, SE, DT, LoserRegs);
931 // Determine how many (unfolded) adds we'll need inside the loop.
932 size_t NumBaseParts = F.BaseRegs.size() + (F.UnfoldedOffset != 0);
933 if (NumBaseParts > 1)
934 NumBaseAdds += NumBaseParts - 1;
936 // Tally up the non-zero immediates.
937 for (SmallVectorImpl<int64_t>::const_iterator I = Offsets.begin(),
938 E = Offsets.end(); I != E; ++I) {
939 int64_t Offset = (uint64_t)*I + F.BaseOffset;
941 ImmCost += 64; // Handle symbolic values conservatively.
942 // TODO: This should probably be the pointer size.
943 else if (Offset != 0)
944 ImmCost += APInt(64, Offset, true).getMinSignedBits();
946 assert(isValid() && "invalid cost");
949 /// Loose - Set this cost to a losing value.
959 /// operator< - Choose the lower cost.
960 bool Cost::operator<(const Cost &Other) const {
961 if (NumRegs != Other.NumRegs)
962 return NumRegs < Other.NumRegs;
963 if (AddRecCost != Other.AddRecCost)
964 return AddRecCost < Other.AddRecCost;
965 if (NumIVMuls != Other.NumIVMuls)
966 return NumIVMuls < Other.NumIVMuls;
967 if (NumBaseAdds != Other.NumBaseAdds)
968 return NumBaseAdds < Other.NumBaseAdds;
969 if (ImmCost != Other.ImmCost)
970 return ImmCost < Other.ImmCost;
971 if (SetupCost != Other.SetupCost)
972 return SetupCost < Other.SetupCost;
976 void Cost::print(raw_ostream &OS) const {
977 OS << NumRegs << " reg" << (NumRegs == 1 ? "" : "s");
979 OS << ", with addrec cost " << AddRecCost;
981 OS << ", plus " << NumIVMuls << " IV mul" << (NumIVMuls == 1 ? "" : "s");
982 if (NumBaseAdds != 0)
983 OS << ", plus " << NumBaseAdds << " base add"
984 << (NumBaseAdds == 1 ? "" : "s");
986 OS << ", plus " << ImmCost << " imm cost";
988 OS << ", plus " << SetupCost << " setup cost";
991 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
992 void Cost::dump() const {
993 print(errs()); errs() << '\n';
999 /// LSRFixup - An operand value in an instruction which is to be replaced
1000 /// with some equivalent, possibly strength-reduced, replacement.
1002 /// UserInst - The instruction which will be updated.
1003 Instruction *UserInst;
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;
1010 /// PostIncLoops - If this user is to use the post-incremented value of an
1011 /// induction variable, this variable is non-null and holds the loop
1012 /// associated with the induction variable.
1013 PostIncLoopSet PostIncLoops;
1015 /// LUIdx - The index of the LSRUse describing the expression which
1016 /// this fixup needs, minus an offset (below).
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.
1024 bool isUseFullyOutsideLoop(const Loop *L) const;
1028 void print(raw_ostream &OS) const;
1034 LSRFixup::LSRFixup()
1035 : UserInst(0), OperandValToReplace(0), LUIdx(~size_t(0)), Offset(0) {}
1037 /// isUseFullyOutsideLoop - Test whether this fixup always uses its
1038 /// value outside of the given loop.
1039 bool LSRFixup::isUseFullyOutsideLoop(const Loop *L) const {
1040 // PHI nodes use their value in their incoming blocks.
1041 if (const PHINode *PN = dyn_cast<PHINode>(UserInst)) {
1042 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1043 if (PN->getIncomingValue(i) == OperandValToReplace &&
1044 L->contains(PN->getIncomingBlock(i)))
1049 return !L->contains(UserInst);
1052 void LSRFixup::print(raw_ostream &OS) const {
1054 // Store is common and interesting enough to be worth special-casing.
1055 if (StoreInst *Store = dyn_cast<StoreInst>(UserInst)) {
1057 WriteAsOperand(OS, Store->getOperand(0), /*PrintType=*/false);
1058 } else if (UserInst->getType()->isVoidTy())
1059 OS << UserInst->getOpcodeName();
1061 WriteAsOperand(OS, UserInst, /*PrintType=*/false);
1063 OS << ", OperandValToReplace=";
1064 WriteAsOperand(OS, OperandValToReplace, /*PrintType=*/false);
1066 for (PostIncLoopSet::const_iterator I = PostIncLoops.begin(),
1067 E = PostIncLoops.end(); I != E; ++I) {
1068 OS << ", PostIncLoop=";
1069 WriteAsOperand(OS, (*I)->getHeader(), /*PrintType=*/false);
1072 if (LUIdx != ~size_t(0))
1073 OS << ", LUIdx=" << LUIdx;
1076 OS << ", Offset=" << Offset;
1079 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1080 void LSRFixup::dump() const {
1081 print(errs()); errs() << '\n';
1087 /// UniquifierDenseMapInfo - A DenseMapInfo implementation for holding
1088 /// DenseMaps and DenseSets of sorted SmallVectors of const SCEV*.
1089 struct UniquifierDenseMapInfo {
1090 static SmallVector<const SCEV *, 2> getEmptyKey() {
1091 SmallVector<const SCEV *, 2> V;
1092 V.push_back(reinterpret_cast<const SCEV *>(-1));
1096 static SmallVector<const SCEV *, 2> getTombstoneKey() {
1097 SmallVector<const SCEV *, 2> V;
1098 V.push_back(reinterpret_cast<const SCEV *>(-2));
1102 static unsigned getHashValue(const SmallVector<const SCEV *, 2> &V) {
1103 unsigned Result = 0;
1104 for (SmallVectorImpl<const SCEV *>::const_iterator I = V.begin(),
1105 E = V.end(); I != E; ++I)
1106 Result ^= DenseMapInfo<const SCEV *>::getHashValue(*I);
1110 static bool isEqual(const SmallVector<const SCEV *, 2> &LHS,
1111 const SmallVector<const SCEV *, 2> &RHS) {
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?
1122 DenseSet<SmallVector<const SCEV *, 2>, UniquifierDenseMapInfo> Uniquifier;
1125 /// KindType - An enum for a kind of use, indicating what types of
1126 /// scaled and immediate operands it might support.
1128 Basic, ///< A normal use, with no folding.
1129 Special, ///< A special case of basic, allowing -1 scales.
1130 Address, ///< An address use; folding according to TargetLowering
1131 ICmpZero ///< An equality icmp with both operands folded into one.
1132 // TODO: Add a generic icmp too?
1138 SmallVector<int64_t, 8> Offsets;
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
1145 bool AllFixupsOutsideLoop;
1147 /// WidestFixupType - This records the widest use type for any fixup using
1148 /// this LSRUse. FindUseWithSimilarFormula can't consider uses with different
1149 /// max fixup widths to be equivalent, because the narrower one may be relying
1150 /// on the implicit truncation to truncate away bogus bits.
1151 Type *WidestFixupType;
1153 /// Formulae - A list of ways to build a value that can satisfy this user.
1154 /// After the list is populated, one of these is selected heuristically and
1155 /// used to formulate a replacement for OperandValToReplace in UserInst.
1156 SmallVector<Formula, 12> Formulae;
1158 /// Regs - The set of register candidates used by all formulae in this LSRUse.
1159 SmallPtrSet<const SCEV *, 4> Regs;
1161 LSRUse(KindType K, Type *T) : Kind(K), AccessTy(T),
1162 MinOffset(INT64_MAX),
1163 MaxOffset(INT64_MIN),
1164 AllFixupsOutsideLoop(true),
1165 WidestFixupType(0) {}
1167 bool HasFormulaWithSameRegs(const Formula &F) const;
1168 bool InsertFormula(const Formula &F);
1169 void DeleteFormula(Formula &F);
1170 void RecomputeRegs(size_t LUIdx, RegUseTracker &Reguses);
1172 void print(raw_ostream &OS) const;
1178 /// HasFormula - Test whether this use as a formula which has the same
1179 /// registers as the given formula.
1180 bool LSRUse::HasFormulaWithSameRegs(const Formula &F) const {
1181 SmallVector<const SCEV *, 2> Key = F.BaseRegs;
1182 if (F.ScaledReg) Key.push_back(F.ScaledReg);
1183 // Unstable sort by host order ok, because this is only used for uniquifying.
1184 std::sort(Key.begin(), Key.end());
1185 return Uniquifier.count(Key);
1188 /// InsertFormula - If the given formula has not yet been inserted, add it to
1189 /// the list, and return true. Return false otherwise.
1190 bool LSRUse::InsertFormula(const Formula &F) {
1191 SmallVector<const SCEV *, 2> Key = F.BaseRegs;
1192 if (F.ScaledReg) Key.push_back(F.ScaledReg);
1193 // Unstable sort by host order ok, because this is only used for uniquifying.
1194 std::sort(Key.begin(), Key.end());
1196 if (!Uniquifier.insert(Key).second)
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!");
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!");
1208 // Add the formula to the list.
1209 Formulae.push_back(F);
1211 // Record registers now being used by this use.
1212 Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end());
1217 /// DeleteFormula - Remove the given formula from this use's list.
1218 void LSRUse::DeleteFormula(Formula &F) {
1219 if (&F != &Formulae.back())
1220 std::swap(F, Formulae.back());
1221 Formulae.pop_back();
1224 /// RecomputeRegs - Recompute the Regs field, and update RegUses.
1225 void LSRUse::RecomputeRegs(size_t LUIdx, RegUseTracker &RegUses) {
1226 // Now that we've filtered out some formulae, recompute the Regs set.
1227 SmallPtrSet<const SCEV *, 4> OldRegs = Regs;
1229 for (SmallVectorImpl<Formula>::const_iterator I = Formulae.begin(),
1230 E = Formulae.end(); I != E; ++I) {
1231 const Formula &F = *I;
1232 if (F.ScaledReg) Regs.insert(F.ScaledReg);
1233 Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end());
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);
1243 void LSRUse::print(raw_ostream &OS) const {
1244 OS << "LSR Use: Kind=";
1246 case Basic: OS << "Basic"; break;
1247 case Special: OS << "Special"; break;
1248 case ICmpZero: OS << "ICmpZero"; break;
1250 OS << "Address of ";
1251 if (AccessTy->isPointerTy())
1252 OS << "pointer"; // the full pointer type could be really verbose
1257 OS << ", Offsets={";
1258 for (SmallVectorImpl<int64_t>::const_iterator I = Offsets.begin(),
1259 E = Offsets.end(); I != E; ++I) {
1261 if (llvm::next(I) != E)
1266 if (AllFixupsOutsideLoop)
1267 OS << ", all-fixups-outside-loop";
1269 if (WidestFixupType)
1270 OS << ", widest fixup type: " << *WidestFixupType;
1273 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1274 void LSRUse::dump() const {
1275 print(errs()); errs() << '\n';
1279 /// isLegalUse - Test whether the use described by AM is "legal", meaning it can
1280 /// be completely folded into the user instruction at isel time. This includes
1281 /// address-mode folding and special icmp tricks.
1282 static bool isLegalUse(const TargetTransformInfo &TTI, LSRUse::KindType Kind,
1283 Type *AccessTy, GlobalValue *BaseGV, int64_t BaseOffset,
1284 bool HasBaseReg, int64_t Scale) {
1286 case LSRUse::Address:
1287 return TTI.isLegalAddressingMode(AccessTy, BaseGV, BaseOffset, HasBaseReg, Scale);
1289 // Otherwise, just guess that reg+reg addressing is legal.
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.
1298 // ICmp only has two operands; don't allow more than two non-trivial parts.
1299 if (Scale != 0 && HasBaseReg && BaseOffset != 0)
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.
1304 if (Scale != 0 && Scale != -1)
1307 // If we have low-level target information, ask the target if it can fold an
1308 // integer immediate on an icmp.
1309 if (BaseOffset != 0) {
1311 // ICmpZero BaseReg + BaseOffset => ICmp BaseReg, -BaseOffset
1312 // ICmpZero -1*ScaleReg + BaseOffset => ICmp ScaleReg, BaseOffset
1313 // Offs is the ICmp immediate.
1315 // The cast does the right thing with INT64_MIN.
1316 BaseOffset = -(uint64_t)BaseOffset;
1317 return TTI.isLegalICmpImmediate(BaseOffset);
1320 // ICmpZero BaseReg + -1*ScaleReg => ICmp BaseReg, ScaleReg
1324 // Only handle single-register values.
1325 return !BaseGV && Scale == 0 && BaseOffset == 0;
1327 case LSRUse::Special:
1328 // Special case Basic to handle -1 scales.
1329 return !BaseGV && (Scale == 0 || Scale == -1) && BaseOffset == 0;
1332 llvm_unreachable("Invalid LSRUse Kind!");
1335 static bool isLegalUse(const TargetTransformInfo &TTI, int64_t MinOffset,
1336 int64_t MaxOffset, LSRUse::KindType Kind, Type *AccessTy,
1337 GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg,
1339 // Check for overflow.
1340 if (((int64_t)((uint64_t)BaseOffset + MinOffset) > BaseOffset) !=
1343 MinOffset = (uint64_t)BaseOffset + MinOffset;
1344 if (((int64_t)((uint64_t)BaseOffset + MaxOffset) > BaseOffset) !=
1347 MaxOffset = (uint64_t)BaseOffset + MaxOffset;
1349 return isLegalUse(TTI, Kind, AccessTy, BaseGV, MinOffset, HasBaseReg,
1351 isLegalUse(TTI, Kind, AccessTy, BaseGV, MaxOffset, HasBaseReg, Scale);
1354 static bool isLegalUse(const TargetTransformInfo &TTI, int64_t MinOffset,
1355 int64_t MaxOffset, LSRUse::KindType Kind, Type *AccessTy,
1357 return isLegalUse(TTI, MinOffset, MaxOffset, Kind, AccessTy, F.BaseGV,
1358 F.BaseOffset, F.HasBaseReg, F.Scale);
1361 static bool isAlwaysFoldable(const TargetTransformInfo &TTI,
1362 LSRUse::KindType Kind, Type *AccessTy,
1363 GlobalValue *BaseGV, int64_t BaseOffset,
1365 // Fast-path: zero is always foldable.
1366 if (BaseOffset == 0 && !BaseGV) return true;
1368 // Conservatively, create an address with an immediate and a
1369 // base and a scale.
1370 int64_t Scale = Kind == LSRUse::ICmpZero ? -1 : 1;
1372 // Canonicalize a scale of 1 to a base register if the formula doesn't
1373 // already have a base register.
1374 if (!HasBaseReg && Scale == 1) {
1379 return isLegalUse(TTI, Kind, AccessTy, BaseGV, BaseOffset, HasBaseReg, Scale);
1382 static bool isAlwaysFoldable(const TargetTransformInfo &TTI,
1383 ScalarEvolution &SE, int64_t MinOffset,
1384 int64_t MaxOffset, LSRUse::KindType Kind,
1385 Type *AccessTy, const SCEV *S, bool HasBaseReg) {
1386 // Fast-path: zero is always foldable.
1387 if (S->isZero()) return true;
1389 // Conservatively, create an address with an immediate and a
1390 // base and a scale.
1391 int64_t BaseOffset = ExtractImmediate(S, SE);
1392 GlobalValue *BaseGV = ExtractSymbol(S, SE);
1394 // If there's anything else involved, it's not foldable.
1395 if (!S->isZero()) return false;
1397 // Fast-path: zero is always foldable.
1398 if (BaseOffset == 0 && !BaseGV) return true;
1400 // Conservatively, create an address with an immediate and a
1401 // base and a scale.
1402 int64_t Scale = Kind == LSRUse::ICmpZero ? -1 : 1;
1404 return isLegalUse(TTI, MinOffset, MaxOffset, Kind, AccessTy, BaseGV,
1405 BaseOffset, HasBaseReg, Scale);
1410 /// UseMapDenseMapInfo - A DenseMapInfo implementation for holding
1411 /// DenseMaps and DenseSets of pairs of const SCEV* and LSRUse::Kind.
1412 struct UseMapDenseMapInfo {
1413 static std::pair<const SCEV *, LSRUse::KindType> getEmptyKey() {
1414 return std::make_pair(reinterpret_cast<const SCEV *>(-1), LSRUse::Basic);
1417 static std::pair<const SCEV *, LSRUse::KindType> getTombstoneKey() {
1418 return std::make_pair(reinterpret_cast<const SCEV *>(-2), LSRUse::Basic);
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));
1428 static bool isEqual(const std::pair<const SCEV *, LSRUse::KindType> &LHS,
1429 const std::pair<const SCEV *, LSRUse::KindType> &RHS) {
1434 /// IVInc - An individual increment in a Chain of IV increments.
1435 /// Relate an IV user to an expression that computes the IV it uses from the IV
1436 /// used by the previous link in the Chain.
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
1444 Instruction *UserInst;
1446 const SCEV *IncExpr;
1448 IVInc(Instruction *U, Value *O, const SCEV *E):
1449 UserInst(U), IVOperand(O), IncExpr(E) {}
1452 // IVChain - The list of IV increments in program order.
1453 // We typically add the head of a chain without finding subsequent links.
1455 SmallVector<IVInc,1> Incs;
1456 const SCEV *ExprBase;
1458 IVChain() : ExprBase(0) {}
1460 IVChain(const IVInc &Head, const SCEV *Base)
1461 : Incs(1, Head), ExprBase(Base) {}
1463 typedef SmallVectorImpl<IVInc>::const_iterator const_iterator;
1465 // begin - return the first increment in the chain.
1466 const_iterator begin() const {
1467 assert(!Incs.empty());
1468 return llvm::next(Incs.begin());
1470 const_iterator end() const {
1474 // hasIncs - Returns true if this chain contains any increments.
1475 bool hasIncs() const { return Incs.size() >= 2; }
1477 // add - Add an IVInc to the end of this chain.
1478 void add(const IVInc &X) { Incs.push_back(X); }
1480 // tailUserInst - Returns the last UserInst in the chain.
1481 Instruction *tailUserInst() const { return Incs.back().UserInst; }
1483 // isProfitableIncrement - Returns true if IncExpr can be profitably added to
1485 bool isProfitableIncrement(const SCEV *OperExpr,
1486 const SCEV *IncExpr,
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.
1494 SmallPtrSet<Instruction*, 4> FarUsers;
1495 SmallPtrSet<Instruction*, 4> NearUsers;
1498 /// LSRInstance - This class holds state for the main loop strength reduction
1502 ScalarEvolution &SE;
1505 const TargetTransformInfo &TTI;
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;
1515 /// Factors - Interesting factors between use strides.
1516 SmallSetVector<int64_t, 8> Factors;
1518 /// Types - Interesting use types, to facilitate truncation reuse.
1519 SmallSetVector<Type *, 4> Types;
1521 /// Fixups - The list of operands which are to be replaced.
1522 SmallVector<LSRFixup, 16> Fixups;
1524 /// Uses - The list of interesting uses.
1525 SmallVector<LSRUse, 16> Uses;
1527 /// RegUses - Track which uses use which register candidates.
1528 RegUseTracker RegUses;
1530 // Limit the number of chains to avoid quadratic behavior. We don't expect to
1531 // have more than a few IV increment chains in a loop. Missing a Chain falls
1532 // back to normal LSR behavior for those uses.
1533 static const unsigned MaxChains = 8;
1535 /// IVChainVec - IV users can form a chain of IV increments.
1536 SmallVector<IVChain, MaxChains> IVChainVec;
1538 /// IVIncSet - IV users that belong to profitable IVChains.
1539 SmallPtrSet<Use*, MaxChains> IVIncSet;
1541 void OptimizeShadowIV();
1542 bool FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse);
1543 ICmpInst *OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse);
1544 void OptimizeLoopTermCond();
1546 void ChainInstruction(Instruction *UserInst, Instruction *IVOper,
1547 SmallVectorImpl<ChainUsers> &ChainUsersVec);
1548 void FinalizeChain(IVChain &Chain);
1549 void CollectChains();
1550 void GenerateIVChain(const IVChain &Chain, SCEVExpander &Rewriter,
1551 SmallVectorImpl<WeakVH> &DeadInsts);
1553 void CollectInterestingTypesAndFactors();
1554 void CollectFixupsAndInitialFormulae();
1556 LSRFixup &getNewFixup() {
1557 Fixups.push_back(LSRFixup());
1558 return Fixups.back();
1561 // Support for sharing of LSRUses between LSRFixups.
1562 typedef DenseMap<std::pair<const SCEV *, LSRUse::KindType>,
1564 UseMapDenseMapInfo> UseMapTy;
1567 bool reconcileNewOffset(LSRUse &LU, int64_t NewOffset, bool HasBaseReg,
1568 LSRUse::KindType Kind, Type *AccessTy);
1570 std::pair<size_t, int64_t> getUse(const SCEV *&Expr,
1571 LSRUse::KindType Kind,
1574 void DeleteUse(LSRUse &LU, size_t LUIdx);
1576 LSRUse *FindUseWithSimilarFormula(const Formula &F, const LSRUse &OrigLU);
1578 void InsertInitialFormula(const SCEV *S, LSRUse &LU, size_t LUIdx);
1579 void InsertSupplementalFormula(const SCEV *S, LSRUse &LU, size_t LUIdx);
1580 void CountRegisters(const Formula &F, size_t LUIdx);
1581 bool InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F);
1583 void CollectLoopInvariantFixupsAndFormulae();
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();
1596 void FilterOutUndesirableDedicatedRegisters();
1598 size_t EstimateSearchSpaceComplexity() const;
1599 void NarrowSearchSpaceByDetectingSupersets();
1600 void NarrowSearchSpaceByCollapsingUnrolledCode();
1601 void NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters();
1602 void NarrowSearchSpaceByPickingWinnerRegs();
1603 void NarrowSearchSpaceUsingHeuristics();
1605 void SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
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;
1613 BasicBlock::iterator
1614 HoistInsertPosition(BasicBlock::iterator IP,
1615 const SmallVectorImpl<Instruction *> &Inputs) const;
1616 BasicBlock::iterator
1617 AdjustInsertPositionForExpand(BasicBlock::iterator IP,
1620 SCEVExpander &Rewriter) const;
1622 Value *Expand(const LSRFixup &LF,
1624 BasicBlock::iterator IP,
1625 SCEVExpander &Rewriter,
1626 SmallVectorImpl<WeakVH> &DeadInsts) const;
1627 void RewriteForPHI(PHINode *PN, const LSRFixup &LF,
1629 SCEVExpander &Rewriter,
1630 SmallVectorImpl<WeakVH> &DeadInsts,
1632 void Rewrite(const LSRFixup &LF,
1634 SCEVExpander &Rewriter,
1635 SmallVectorImpl<WeakVH> &DeadInsts,
1637 void ImplementSolution(const SmallVectorImpl<const Formula *> &Solution,
1641 LSRInstance(Loop *L, Pass *P);
1643 bool getChanged() const { return Changed; }
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;
1654 /// OptimizeShadowIV - If IV is used in a int-to-float cast
1655 /// inside the loop then try to eliminate the cast operation.
1656 void LSRInstance::OptimizeShadowIV() {
1657 const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L);
1658 if (isa<SCEVCouldNotCompute>(BackedgeTakenCount))
1661 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end();
1662 UI != E; /* empty */) {
1663 IVUsers::const_iterator CandidateUI = UI;
1665 Instruction *ShadowUse = CandidateUI->getUser();
1666 Type *DestTy = NULL;
1667 bool IsSigned = false;
1669 /* If shadow use is a int->float cast then insert a second IV
1670 to eliminate this cast.
1672 for (unsigned i = 0; i < n; ++i)
1678 for (unsigned i = 0; i < n; ++i, ++d)
1681 if (UIToFPInst *UCast = dyn_cast<UIToFPInst>(CandidateUI->getUser())) {
1683 DestTy = UCast->getDestTy();
1685 else if (SIToFPInst *SCast = dyn_cast<SIToFPInst>(CandidateUI->getUser())) {
1687 DestTy = SCast->getDestTy();
1689 if (!DestTy) continue;
1691 // If target does not support DestTy natively then do not apply
1692 // this transformation.
1693 if (!TTI.isTypeLegal(DestTy)) continue;
1695 PHINode *PH = dyn_cast<PHINode>(ShadowUse->getOperand(0));
1697 if (PH->getNumIncomingValues() != 2) continue;
1699 Type *SrcTy = PH->getType();
1700 int Mantissa = DestTy->getFPMantissaWidth();
1701 if (Mantissa == -1) continue;
1702 if ((int)SE.getTypeSizeInBits(SrcTy) > Mantissa)
1705 unsigned Entry, Latch;
1706 if (PH->getIncomingBlock(0) == L->getLoopPreheader()) {
1714 ConstantInt *Init = dyn_cast<ConstantInt>(PH->getIncomingValue(Entry));
1715 if (!Init) continue;
1716 Constant *NewInit = ConstantFP::get(DestTy, IsSigned ?
1717 (double)Init->getSExtValue() :
1718 (double)Init->getZExtValue());
1720 BinaryOperator *Incr =
1721 dyn_cast<BinaryOperator>(PH->getIncomingValue(Latch));
1722 if (!Incr) continue;
1723 if (Incr->getOpcode() != Instruction::Add
1724 && Incr->getOpcode() != Instruction::Sub)
1727 /* Initialize new IV, double d = 0.0 in above example. */
1728 ConstantInt *C = NULL;
1729 if (Incr->getOperand(0) == PH)
1730 C = dyn_cast<ConstantInt>(Incr->getOperand(1));
1731 else if (Incr->getOperand(1) == PH)
1732 C = dyn_cast<ConstantInt>(Incr->getOperand(0));
1738 // Ignore negative constants, as the code below doesn't handle them
1739 // correctly. TODO: Remove this restriction.
1740 if (!C->getValue().isStrictlyPositive()) continue;
1742 /* Add new PHINode. */
1743 PHINode *NewPH = PHINode::Create(DestTy, 2, "IV.S.", PH);
1745 /* create new increment. '++d' in above example. */
1746 Constant *CFP = ConstantFP::get(DestTy, C->getZExtValue());
1747 BinaryOperator *NewIncr =
1748 BinaryOperator::Create(Incr->getOpcode() == Instruction::Add ?
1749 Instruction::FAdd : Instruction::FSub,
1750 NewPH, CFP, "IV.S.next.", Incr);
1752 NewPH->addIncoming(NewInit, PH->getIncomingBlock(Entry));
1753 NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch));
1755 /* Remove cast operation */
1756 ShadowUse->replaceAllUsesWith(NewPH);
1757 ShadowUse->eraseFromParent();
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
1766 bool LSRInstance::FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse) {
1767 for (IVUsers::iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI)
1768 if (UI->getUser() == Cond) {
1769 // NOTE: we could handle setcc instructions with multiple uses here, but
1770 // InstCombine does it as well for simple uses, it's not clear that it
1771 // occurs enough in real life to handle.
1778 /// OptimizeMax - Rewrite the loop's terminating condition if it uses
1779 /// a max computation.
1781 /// This is a narrow solution to a specific, but acute, problem. For loops
1787 /// } while (++i < n);
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:
1798 /// } while (++i < n);
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.
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:
1809 /// max = n < 1 ? 1 : n;
1812 /// } while (++i != max);
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
1818 /// needed, and it does this using a simple analysis that only succeeds if
1819 /// the loop has a canonical induction variable.
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.
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.
1828 ICmpInst *LSRInstance::OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse) {
1829 // Check that the loop matches the pattern we're looking for.
1830 if (Cond->getPredicate() != CmpInst::ICMP_EQ &&
1831 Cond->getPredicate() != CmpInst::ICMP_NE)
1834 SelectInst *Sel = dyn_cast<SelectInst>(Cond->getOperand(1));
1835 if (!Sel || !Sel->hasOneUse()) return Cond;
1837 const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L);
1838 if (isa<SCEVCouldNotCompute>(BackedgeTakenCount))
1840 const SCEV *One = SE.getConstant(BackedgeTakenCount->getType(), 1);
1842 // Add one to the backedge-taken count to get the trip count.
1843 const SCEV *IterationCount = SE.getAddExpr(One, BackedgeTakenCount);
1844 if (IterationCount != SE.getSCEV(Sel)) return Cond;
1846 // Check for a max calculation that matches the pattern. There's no check
1847 // for ICMP_ULE here because the comparison would be with zero, which
1848 // isn't interesting.
1849 CmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
1850 const SCEVNAryExpr *Max = 0;
1851 if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(BackedgeTakenCount)) {
1852 Pred = ICmpInst::ICMP_SLE;
1854 } else if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(IterationCount)) {
1855 Pred = ICmpInst::ICMP_SLT;
1857 } else if (const SCEVUMaxExpr *U = dyn_cast<SCEVUMaxExpr>(IterationCount)) {
1858 Pred = ICmpInst::ICMP_ULT;
1865 // To handle a max with more than two operands, this optimization would
1866 // require additional checking and setup.
1867 if (Max->getNumOperands() != 2)
1870 const SCEV *MaxLHS = Max->getOperand(0);
1871 const SCEV *MaxRHS = Max->getOperand(1);
1873 // ScalarEvolution canonicalizes constants to the left. For < and >, look
1874 // for a comparison with 1. For <= and >=, a comparison with zero.
1876 (ICmpInst::isTrueWhenEqual(Pred) ? !MaxLHS->isZero() : (MaxLHS != One)))
1879 // Check the relevant induction variable for conformance to
1881 const SCEV *IV = SE.getSCEV(Cond->getOperand(0));
1882 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(IV);
1883 if (!AR || !AR->isAffine() ||
1884 AR->getStart() != One ||
1885 AR->getStepRecurrence(SE) != One)
1888 assert(AR->getLoop() == L &&
1889 "Loop condition operand is an addrec in a different loop!");
1891 // Check the right operand of the select, and remember it, as it will
1892 // be used in the new comparison instruction.
1894 if (ICmpInst::isTrueWhenEqual(Pred)) {
1895 // Look for n+1, and grab n.
1896 if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(1)))
1897 if (isa<ConstantInt>(BO->getOperand(1)) &&
1898 cast<ConstantInt>(BO->getOperand(1))->isOne() &&
1899 SE.getSCEV(BO->getOperand(0)) == MaxRHS)
1900 NewRHS = BO->getOperand(0);
1901 if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(2)))
1902 if (isa<ConstantInt>(BO->getOperand(1)) &&
1903 cast<ConstantInt>(BO->getOperand(1))->isOne() &&
1904 SE.getSCEV(BO->getOperand(0)) == MaxRHS)
1905 NewRHS = BO->getOperand(0);
1908 } else if (SE.getSCEV(Sel->getOperand(1)) == MaxRHS)
1909 NewRHS = Sel->getOperand(1);
1910 else if (SE.getSCEV(Sel->getOperand(2)) == MaxRHS)
1911 NewRHS = Sel->getOperand(2);
1912 else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(MaxRHS))
1913 NewRHS = SU->getValue();
1915 // Max doesn't match expected pattern.
1918 // Determine the new comparison opcode. It may be signed or unsigned,
1919 // and the original comparison may be either equality or inequality.
1920 if (Cond->getPredicate() == CmpInst::ICMP_EQ)
1921 Pred = CmpInst::getInversePredicate(Pred);
1923 // Ok, everything looks ok to change the condition into an SLT or SGE and
1924 // delete the max calculation.
1926 new ICmpInst(Cond, Pred, Cond->getOperand(0), NewRHS, "scmp");
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();
1939 /// OptimizeLoopTermCond - Change loop terminating condition to use the
1940 /// postinc iv when possible.
1942 LSRInstance::OptimizeLoopTermCond() {
1943 SmallPtrSet<Instruction *, 4> PostIncs;
1945 BasicBlock *LatchBlock = L->getLoopLatch();
1946 SmallVector<BasicBlock*, 8> ExitingBlocks;
1947 L->getExitingBlocks(ExitingBlocks);
1949 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
1950 BasicBlock *ExitingBlock = ExitingBlocks[i];
1952 // Get the terminating condition for the loop if possible. If we
1953 // can, we want to change it to use a post-incremented version of its
1954 // induction variable, to allow coalescing the live ranges for the IV into
1955 // one register value.
1957 BranchInst *TermBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
1960 // FIXME: Overly conservative, termination condition could be an 'or' etc..
1961 if (TermBr->isUnconditional() || !isa<ICmpInst>(TermBr->getCondition()))
1964 // Search IVUsesByStride to find Cond's IVUse if there is one.
1965 IVStrideUse *CondUse = 0;
1966 ICmpInst *Cond = cast<ICmpInst>(TermBr->getCondition());
1967 if (!FindIVUserForCond(Cond, CondUse))
1970 // If the trip count is computed in terms of a max (due to ScalarEvolution
1971 // being unable to find a sufficient guard, for example), change the loop
1972 // comparison to use SLT or ULT instead of NE.
1973 // One consequence of doing this now is that it disrupts the count-down
1974 // optimization. That's not always a bad thing though, because in such
1975 // cases it may still be worthwhile to avoid a max.
1976 Cond = OptimizeMax(Cond, CondUse);
1978 // If this exiting block dominates the latch block, it may also use
1979 // the post-inc value if it won't be shared with other uses.
1980 // Check for dominance.
1981 if (!DT.dominates(ExitingBlock, LatchBlock))
1984 // Conservatively avoid trying to use the post-inc value in non-latch
1985 // exits if there may be pre-inc users in intervening blocks.
1986 if (LatchBlock != ExitingBlock)
1987 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI)
1988 // Test if the use is reachable from the exiting block. This dominator
1989 // query is a conservative approximation of reachability.
1990 if (&*UI != CondUse &&
1991 !DT.properlyDominates(UI->getUser()->getParent(), ExitingBlock)) {
1992 // Conservatively assume there may be reuse if the quotient of their
1993 // strides could be a legal scale.
1994 const SCEV *A = IU.getStride(*CondUse, L);
1995 const SCEV *B = IU.getStride(*UI, L);
1996 if (!A || !B) continue;
1997 if (SE.getTypeSizeInBits(A->getType()) !=
1998 SE.getTypeSizeInBits(B->getType())) {
1999 if (SE.getTypeSizeInBits(A->getType()) >
2000 SE.getTypeSizeInBits(B->getType()))
2001 B = SE.getSignExtendExpr(B, A->getType());
2003 A = SE.getSignExtendExpr(A, B->getType());
2005 if (const SCEVConstant *D =
2006 dyn_cast_or_null<SCEVConstant>(getExactSDiv(B, A, SE))) {
2007 const ConstantInt *C = D->getValue();
2008 // Stride of one or negative one can have reuse with non-addresses.
2009 if (C->isOne() || C->isAllOnesValue())
2010 goto decline_post_inc;
2011 // Avoid weird situations.
2012 if (C->getValue().getMinSignedBits() >= 64 ||
2013 C->getValue().isMinSignedValue())
2014 goto decline_post_inc;
2015 // Check for possible scaled-address reuse.
2016 Type *AccessTy = getAccessType(UI->getUser());
2017 int64_t Scale = C->getSExtValue();
2018 if (TTI.isLegalAddressingMode(AccessTy, /*BaseGV=*/ 0,
2020 /*HasBaseReg=*/ false, Scale))
2021 goto decline_post_inc;
2023 if (TTI.isLegalAddressingMode(AccessTy, /*BaseGV=*/ 0,
2025 /*HasBaseReg=*/ false, Scale))
2026 goto decline_post_inc;
2030 DEBUG(dbgs() << " Change loop exiting icmp to use postinc iv: "
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.
2036 if (&*++BasicBlock::iterator(Cond) != TermBr) {
2037 if (Cond->hasOneUse()) {
2038 Cond->moveBefore(TermBr);
2040 // Clone the terminating condition and insert into the loopend.
2041 ICmpInst *OldCond = Cond;
2042 Cond = cast<ICmpInst>(Cond->clone());
2043 Cond->setName(L->getHeader()->getName() + ".termcond");
2044 ExitingBlock->getInstList().insert(TermBr, Cond);
2046 // Clone the IVUse, as the old use still exists!
2047 CondUse = &IU.AddUser(Cond, CondUse->getOperandValToReplace());
2048 TermBr->replaceUsesOfWith(OldCond, Cond);
2052 // If we get to here, we know that we can transform the setcc instruction to
2053 // use the post-incremented version of the IV, allowing us to coalesce the
2054 // live ranges for the IV correctly.
2055 CondUse->transformToPostInc(L);
2058 PostIncs.insert(Cond);
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) {
2069 DT.findNearestCommonDominator(IVIncInsertPos->getParent(),
2071 if (BB == (*I)->getParent())
2072 IVIncInsertPos = *I;
2073 else if (BB != IVIncInsertPos->getParent())
2074 IVIncInsertPos = BB->getTerminator();
2078 /// reconcileNewOffset - Determine if the given use can accommodate a fixup
2079 /// at the given offset and other details. If so, update the use and
2082 LSRInstance::reconcileNewOffset(LSRUse &LU, int64_t NewOffset, bool HasBaseReg,
2083 LSRUse::KindType Kind, Type *AccessTy) {
2084 int64_t NewMinOffset = LU.MinOffset;
2085 int64_t NewMaxOffset = LU.MaxOffset;
2086 Type *NewAccessTy = AccessTy;
2088 // Check for a mismatched kind. It's tempting to collapse mismatched kinds to
2089 // something conservative, however this can pessimize in the case that one of
2090 // the uses will have all its uses outside the loop, for example.
2091 if (LU.Kind != Kind)
2093 // Conservatively assume HasBaseReg is true for now.
2094 if (NewOffset < LU.MinOffset) {
2095 if (!isAlwaysFoldable(TTI, Kind, AccessTy, /*BaseGV=*/ 0,
2096 LU.MaxOffset - NewOffset, HasBaseReg))
2098 NewMinOffset = NewOffset;
2099 } else if (NewOffset > LU.MaxOffset) {
2100 if (!isAlwaysFoldable(TTI, Kind, AccessTy, /*BaseGV=*/ 0,
2101 NewOffset - LU.MinOffset, HasBaseReg))
2103 NewMaxOffset = NewOffset;
2105 // Check for a mismatched access type, and fall back conservatively as needed.
2106 // TODO: Be less conservative when the type is similar and can use the same
2107 // addressing modes.
2108 if (Kind == LSRUse::Address && AccessTy != LU.AccessTy)
2109 NewAccessTy = Type::getVoidTy(AccessTy->getContext());
2112 LU.MinOffset = NewMinOffset;
2113 LU.MaxOffset = NewMaxOffset;
2114 LU.AccessTy = NewAccessTy;
2115 if (NewOffset != LU.Offsets.back())
2116 LU.Offsets.push_back(NewOffset);
2120 /// getUse - Return an LSRUse index and an offset value for a fixup which
2121 /// needs the given expression, with the given kind and optional access type.
2122 /// Either reuse an existing use or create a new one, as needed.
2123 std::pair<size_t, int64_t>
2124 LSRInstance::getUse(const SCEV *&Expr,
2125 LSRUse::KindType Kind, Type *AccessTy) {
2126 const SCEV *Copy = Expr;
2127 int64_t Offset = ExtractImmediate(Expr, SE);
2129 // Basic uses can't accept any offset, for example.
2130 if (!isAlwaysFoldable(TTI, Kind, AccessTy, /*BaseGV=*/ 0,
2131 Offset, /*HasBaseReg=*/ true)) {
2136 std::pair<UseMapTy::iterator, bool> P =
2137 UseMap.insert(std::make_pair(std::make_pair(Expr, Kind), 0));
2139 // A use already existed with this base.
2140 size_t LUIdx = P.first->second;
2141 LSRUse &LU = Uses[LUIdx];
2142 if (reconcileNewOffset(LU, Offset, /*HasBaseReg=*/true, Kind, AccessTy))
2144 return std::make_pair(LUIdx, Offset);
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];
2153 // We don't need to track redundant offsets, but we don't need to go out
2154 // of our way here to avoid them.
2155 if (LU.Offsets.empty() || Offset != LU.Offsets.back())
2156 LU.Offsets.push_back(Offset);
2158 LU.MinOffset = Offset;
2159 LU.MaxOffset = Offset;
2160 return std::make_pair(LUIdx, Offset);
2163 /// DeleteUse - Delete the given use from the Uses list.
2164 void LSRInstance::DeleteUse(LSRUse &LU, size_t LUIdx) {
2165 if (&LU != &Uses.back())
2166 std::swap(LU, Uses.back());
2170 RegUses.SwapAndDropUse(LUIdx, Uses.size());
2173 /// FindUseWithFormula - Look for a use distinct from OrigLU which is has
2174 /// a formula that has the same registers as the given formula.
2176 LSRInstance::FindUseWithSimilarFormula(const Formula &OrigF,
2177 const LSRUse &OrigLU) {
2178 // Search all uses for the formula. This could be more clever.
2179 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
2180 LSRUse &LU = Uses[LUIdx];
2181 // Check whether this use is close enough to OrigLU, to see whether it's
2182 // worthwhile looking through its formulae.
2183 // Ignore ICmpZero uses because they may contain formulae generated by
2184 // GenerateICmpZeroScales, in which case adding fixup offsets may
2186 if (&LU != &OrigLU &&
2187 LU.Kind != LSRUse::ICmpZero &&
2188 LU.Kind == OrigLU.Kind && OrigLU.AccessTy == LU.AccessTy &&
2189 LU.WidestFixupType == OrigLU.WidestFixupType &&
2190 LU.HasFormulaWithSameRegs(OrigF)) {
2191 // Scan through this use's formulae.
2192 for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(),
2193 E = LU.Formulae.end(); I != E; ++I) {
2194 const Formula &F = *I;
2195 // Check to see if this formula has the same registers and symbols
2197 if (F.BaseRegs == OrigF.BaseRegs &&
2198 F.ScaledReg == OrigF.ScaledReg &&
2199 F.BaseGV == OrigF.BaseGV &&
2200 F.Scale == OrigF.Scale &&
2201 F.UnfoldedOffset == OrigF.UnfoldedOffset) {
2202 if (F.BaseOffset == 0)
2204 // This is the formula where all the registers and symbols matched;
2205 // there aren't going to be any others. Since we declined it, we
2206 // can skip the rest of the formulae and proceed to the next LSRUse.
2213 // Nothing looked good.
2217 void LSRInstance::CollectInterestingTypesAndFactors() {
2218 SmallSetVector<const SCEV *, 4> Strides;
2220 // Collect interesting types and strides.
2221 SmallVector<const SCEV *, 4> Worklist;
2222 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) {
2223 const SCEV *Expr = IU.getExpr(*UI);
2225 // Collect interesting types.
2226 Types.insert(SE.getEffectiveSCEVType(Expr->getType()));
2228 // Add strides for mentioned loops.
2229 Worklist.push_back(Expr);
2231 const SCEV *S = Worklist.pop_back_val();
2232 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
2233 if (AR->getLoop() == L)
2234 Strides.insert(AR->getStepRecurrence(SE));
2235 Worklist.push_back(AR->getStart());
2236 } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
2237 Worklist.append(Add->op_begin(), Add->op_end());
2239 } while (!Worklist.empty());
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)
2245 for (SmallSetVector<const SCEV *, 4>::const_iterator NewStrideIter =
2246 llvm::next(I); NewStrideIter != E; ++NewStrideIter) {
2247 const SCEV *OldStride = *I;
2248 const SCEV *NewStride = *NewStrideIter;
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());
2256 OldStride = SE.getSignExtendExpr(OldStride, NewStride->getType());
2258 if (const SCEVConstant *Factor =
2259 dyn_cast_or_null<SCEVConstant>(getExactSDiv(NewStride, OldStride,
2261 if (Factor->getValue()->getValue().getMinSignedBits() <= 64)
2262 Factors.insert(Factor->getValue()->getValue().getSExtValue());
2263 } else if (const SCEVConstant *Factor =
2264 dyn_cast_or_null<SCEVConstant>(getExactSDiv(OldStride,
2267 if (Factor->getValue()->getValue().getMinSignedBits() <= 64)
2268 Factors.insert(Factor->getValue()->getValue().getSExtValue());
2272 // If all uses use the same type, don't bother looking for truncation-based
2274 if (Types.size() == 1)
2277 DEBUG(print_factors_and_types(dbgs()));
2280 /// findIVOperand - Helper for CollectChains that finds an IV operand (computed
2281 /// by an AddRec in this loop) within [OI,OE) or returns OE. If IVUsers mapped
2282 /// Instructions to IVStrideUses, we could partially skip this.
2283 static User::op_iterator
2284 findIVOperand(User::op_iterator OI, User::op_iterator OE,
2285 Loop *L, ScalarEvolution &SE) {
2286 for(; OI != OE; ++OI) {
2287 if (Instruction *Oper = dyn_cast<Instruction>(*OI)) {
2288 if (!SE.isSCEVable(Oper->getType()))
2291 if (const SCEVAddRecExpr *AR =
2292 dyn_cast<SCEVAddRecExpr>(SE.getSCEV(Oper))) {
2293 if (AR->getLoop() == L)
2301 /// getWideOperand - IVChain logic must consistenctly peek base TruncInst
2302 /// operands, so wrap it in a convenient helper.
2303 static Value *getWideOperand(Value *Oper) {
2304 if (TruncInst *Trunc = dyn_cast<TruncInst>(Oper))
2305 return Trunc->getOperand(0);
2309 /// isCompatibleIVType - Return true if we allow an IV chain to include both
2311 static bool isCompatibleIVType(Value *LVal, Value *RVal) {
2312 Type *LType = LVal->getType();
2313 Type *RType = RVal->getType();
2314 return (LType == RType) || (LType->isPointerTy() && RType->isPointerTy());
2317 /// getExprBase - Return an approximation of this SCEV expression's "base", or
2318 /// NULL for any constant. Returning the expression itself is
2319 /// conservative. Returning a deeper subexpression is more precise and valid as
2320 /// long as it isn't less complex than another subexpression. For expressions
2321 /// involving multiple unscaled values, we need to return the pointer-type
2322 /// SCEVUnknown. This avoids forming chains across objects, such as:
2323 /// PrevOper==a[i], IVOper==b[i], IVInc==b-a.
2325 /// Since SCEVUnknown is the rightmost type, and pointers are the rightmost
2326 /// SCEVUnknown, we simply return the rightmost SCEV operand.
2327 static const SCEV *getExprBase(const SCEV *S) {
2328 switch (S->getSCEVType()) {
2329 default: // uncluding scUnknown.
2334 return getExprBase(cast<SCEVTruncateExpr>(S)->getOperand());
2336 return getExprBase(cast<SCEVZeroExtendExpr>(S)->getOperand());
2338 return getExprBase(cast<SCEVSignExtendExpr>(S)->getOperand());
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);
2350 if (SubExpr->getSCEVType() != scMulExpr)
2353 return S; // all operands are scaled, be conservative.
2356 return getExprBase(cast<SCEVAddRecExpr>(S)->getStart());
2360 /// Return true if the chain increment is profitable to expand into a loop
2361 /// invariant value, which may require its own register. A profitable chain
2362 /// increment will be an offset relative to the same base. We allow such offsets
2363 /// to potentially be used as chain increment as long as it's not obviously
2364 /// expensive to expand using real instructions.
2365 bool IVChain::isProfitableIncrement(const SCEV *OperExpr,
2366 const SCEV *IncExpr,
2367 ScalarEvolution &SE) {
2368 // Aggressively form chains when -stress-ivchain.
2372 // Do not replace a constant offset from IV head with a nonconstant IV
2374 if (!isa<SCEVConstant>(IncExpr)) {
2375 const SCEV *HeadExpr = SE.getSCEV(getWideOperand(Incs[0].IVOperand));
2376 if (isa<SCEVConstant>(SE.getMinusSCEV(OperExpr, HeadExpr)))
2380 SmallPtrSet<const SCEV*, 8> Processed;
2381 return !isHighCostExpansion(IncExpr, Processed, SE);
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.
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.
2393 /// TODO: Consider IVInc free if it's already used in another chains.
2395 isProfitableChain(IVChain &Chain, SmallPtrSet<Instruction*, 4> &Users,
2396 ScalarEvolution &SE, const TargetTransformInfo &TTI) {
2400 if (!Chain.hasIncs())
2403 if (!Users.empty()) {
2404 DEBUG(dbgs() << "Chain: " << *Chain.Incs[0].UserInst << " users:\n";
2405 for (SmallPtrSet<Instruction*, 4>::const_iterator I = Users.begin(),
2406 E = Users.end(); I != E; ++I) {
2407 dbgs() << " " << **I << "\n";
2411 assert(!Chain.Incs.empty() && "empty IV chains are not allowed");
2413 // The chain itself may require a register, so intialize cost to 1.
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.
2419 if (isa<PHINode>(Chain.tailUserInst())
2420 && SE.getSCEV(Chain.tailUserInst()) == Chain.Incs[0].IncExpr) {
2423 const SCEV *LastIncExpr = 0;
2424 unsigned NumConstIncrements = 0;
2425 unsigned NumVarIncrements = 0;
2426 unsigned NumReusedIncrements = 0;
2427 for (IVChain::const_iterator I = Chain.begin(), E = Chain.end();
2430 if (I->IncExpr->isZero())
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;
2440 if (I->IncExpr == LastIncExpr)
2441 ++NumReusedIncrements;
2445 LastIncExpr = I->IncExpr;
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)
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;
2459 // Reusing variable increments likely saves a register to hold the multiple of
2461 cost -= NumReusedIncrements;
2463 DEBUG(dbgs() << "Chain: " << *Chain.Incs[0].UserInst << " Cost: " << cost
2469 /// ChainInstruction - Add this IV user to an existing chain or make it the head
2471 void LSRInstance::ChainInstruction(Instruction *UserInst, Instruction *IVOper,
2472 SmallVectorImpl<ChainUsers> &ChainUsersVec) {
2473 // When IVs are used as types of varying widths, they are generally converted
2474 // to a wider type with some uses remaining narrow under a (free) trunc.
2475 Value *const NextIV = getWideOperand(IVOper);
2476 const SCEV *const OperExpr = SE.getSCEV(NextIV);
2477 const SCEV *const OperExprBase = getExprBase(OperExpr);
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) {
2484 IVChain &Chain = IVChainVec[ChainIdx];
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)
2493 Value *PrevIV = getWideOperand(Chain.Incs.back().IVOperand);
2494 if (!isCompatibleIVType(PrevIV, NextIV))
2497 // A phi node terminates a chain.
2498 if (isa<PHINode>(UserInst) && isa<PHINode>(Chain.tailUserInst()))
2501 // The increment must be loop-invariant so it can be kept in a register.
2502 const SCEV *PrevExpr = SE.getSCEV(PrevIV);
2503 const SCEV *IncExpr = SE.getMinusSCEV(OperExpr, PrevExpr);
2504 if (!SE.isLoopInvariant(IncExpr, L))
2507 if (Chain.isProfitableIncrement(OperExpr, IncExpr, SE)) {
2508 LastIncExpr = IncExpr;
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))
2517 if (NChains >= MaxChains && !StressIVChain) {
2518 DEBUG(dbgs() << "IV Chain Limit\n");
2521 LastIncExpr = OperExpr;
2522 // IVUsers may have skipped over sign/zero extensions. We don't currently
2523 // attempt to form chains involving extensions unless they can be hoisted
2524 // into this loop's AddRec.
2525 if (!isa<SCEVAddRecExpr>(LastIncExpr))
2528 IVChainVec.push_back(IVChain(IVInc(UserInst, IVOper, LastIncExpr),
2530 ChainUsersVec.resize(NChains);
2531 DEBUG(dbgs() << "IV Chain#" << ChainIdx << " Head: (" << *UserInst
2532 << ") IV=" << *LastIncExpr << "\n");
2534 DEBUG(dbgs() << "IV Chain#" << ChainIdx << " Inc: (" << *UserInst
2535 << ") IV+" << *LastIncExpr << "\n");
2536 // Add this IV user to the end of the chain.
2537 IVChainVec[ChainIdx].add(IVInc(UserInst, IVOper, LastIncExpr));
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(),
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);
2556 if (!OtherUse || OtherUse == UserInst)
2558 if (SE.isSCEVable(OtherUse->getType())
2559 && !isa<SCEVUnknown>(SE.getSCEV(OtherUse))
2560 && IU.isIVUserOrOperand(OtherUse)) {
2563 NearUsers.insert(OtherUse);
2566 // Since this user is part of the chain, it's no longer considered a use
2568 ChainUsersVec[ChainIdx].FarUsers.erase(UserInst);
2571 /// CollectChains - Populate the vector of Chains.
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.
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.
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:
2590 /// TODO: Walk the entire domtree within this loop, not just the path to the
2591 /// loop latch. This will discover chains on side paths, but requires
2592 /// maintaining multiple copies of the Chains state.
2593 void LSRInstance::CollectChains() {
2594 DEBUG(dbgs() << "Collecting IV Chains.\n");
2595 SmallVector<ChainUsers, 8> ChainUsersVec;
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());
2603 LatchPath.push_back(LoopHeader);
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();
2611 // Skip instructions that weren't seen by IVUsers analysis.
2612 if (isa<PHINode>(I) || !IU.isIVUserOrOperand(I))
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)))
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);
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);
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()))
2645 dyn_cast<Instruction>(PN->getIncomingValueForBlock(L->getLoopLatch()));
2647 ChainInstruction(PN, IncV, ChainUsersVec);
2649 // Remove any unprofitable chains.
2650 unsigned ChainIdx = 0;
2651 for (unsigned UsersIdx = 0, NChains = IVChainVec.size();
2652 UsersIdx < NChains; ++UsersIdx) {
2653 if (!isProfitableChain(IVChainVec[UsersIdx],
2654 ChainUsersVec[UsersIdx].FarUsers, SE, TTI))
2656 // Preserve the chain at UsesIdx.
2657 if (ChainIdx != UsersIdx)
2658 IVChainVec[ChainIdx] = IVChainVec[UsersIdx];
2659 FinalizeChain(IVChainVec[ChainIdx]);
2662 IVChainVec.resize(ChainIdx);
2665 void LSRInstance::FinalizeChain(IVChain &Chain) {
2666 assert(!Chain.Incs.empty() && "empty IV chains are not allowed");
2667 DEBUG(dbgs() << "Final Chain: " << *Chain.Incs[0].UserInst << "\n");
2669 for (IVChain::const_iterator I = Chain.begin(), E = Chain.end();
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);
2679 /// Return true if the IVInc can be folded into an addressing mode.
2680 static bool canFoldIVIncExpr(const SCEV *IncExpr, Instruction *UserInst,
2681 Value *Operand, const TargetTransformInfo &TTI) {
2682 const SCEVConstant *IncConst = dyn_cast<SCEVConstant>(IncExpr);
2683 if (!IncConst || !isAddressUse(UserInst, Operand))
2686 if (IncConst->getValue()->getValue().getMinSignedBits() > 64)
2689 int64_t IncOffset = IncConst->getValue()->getSExtValue();
2690 if (!isAlwaysFoldable(TTI, LSRUse::Address,
2691 getAccessType(UserInst), /*BaseGV=*/ 0,
2692 IncOffset, /*HaseBaseReg=*/ false))
2698 /// GenerateIVChains - Generate an add or subtract for each IVInc in a chain to
2699 /// materialize the IV user's operand from the previous IV user's operand.
2700 void LSRInstance::GenerateIVChain(const IVChain &Chain, SCEVExpander &Rewriter,
2701 SmallVectorImpl<WeakVH> &DeadInsts) {
2702 // Find the new IVOperand for the head of the chain. It may have been replaced
2704 const IVInc &Head = Chain.Incs[0];
2705 User::op_iterator IVOpEnd = Head.UserInst->op_end();
2706 User::op_iterator IVOpIter = findIVOperand(Head.UserInst->op_begin(),
2709 while (IVOpIter != IVOpEnd) {
2710 IVSrc = getWideOperand(*IVOpIter);
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.)
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) {
2724 IVOpIter = findIVOperand(llvm::next(IVOpIter), IVOpEnd, L, SE);
2726 if (IVOpIter == IVOpEnd) {
2727 // Gracefully give up on this chain.
2728 DEBUG(dbgs() << "Concealed chain head: " << *Head.UserInst << "\n");
2732 DEBUG(dbgs() << "Generate chain at: " << *IVSrc << "\n");
2733 Type *IVTy = IVSrc->getType();
2734 Type *IntTy = SE.getEffectiveSCEVType(IVTy);
2735 const SCEV *LeftOverExpr = 0;
2736 for (IVChain::const_iterator IncI = Chain.begin(),
2737 IncE = Chain.end(); IncI != IncE; ++IncI) {
2739 Instruction *InsertPt = IncI->UserInst;
2740 if (isa<PHINode>(InsertPt))
2741 InsertPt = L->getLoopLatch()->getTerminator();
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
2749 const SCEV *IncExpr = SE.getNoopOrSignExtend(IncI->IncExpr, IntTy);
2750 LeftOverExpr = LeftOverExpr ?
2751 SE.getAddExpr(LeftOverExpr, IncExpr) : IncExpr;
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);
2761 // If an IV increment can't be folded, use it as the next IV value.
2762 if (!canFoldIVIncExpr(LeftOverExpr, IncI->UserInst, IncI->IVOperand,
2764 assert(IVTy == IVOper->getType() && "inconsistent IV increment type");
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");
2776 IncI->UserInst->replaceUsesOfWith(IncI->IVOperand, IVOper);
2777 DeadInsts.push_back(IncI->IVOperand);
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.
2781 if (isa<PHINode>(Chain.tailUserInst())) {
2782 for (BasicBlock::iterator I = L->getHeader()->begin();
2783 PHINode *Phi = dyn_cast<PHINode>(I); ++I) {
2784 if (!isCompatibleIVType(Phi, IVSrc))
2786 Instruction *PostIncV = dyn_cast<Instruction>(
2787 Phi->getIncomingValueForBlock(L->getLoopLatch()));
2788 if (!PostIncV || (SE.getSCEV(PostIncV) != SE.getSCEV(IVSrc)))
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");
2798 Phi->replaceUsesOfWith(PostIncV, IVOper);
2799 DeadInsts.push_back(PostIncV);
2804 void LSRInstance::CollectFixupsAndInitialFormulae() {
2805 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) {
2806 Instruction *UserInst = UI->getUser();
2807 // Skip IV users that are part of profitable IV Chains.
2808 User::op_iterator UseI = std::find(UserInst->op_begin(), UserInst->op_end(),
2809 UI->getOperandValToReplace());
2810 assert(UseI != UserInst->op_end() && "cannot find IV operand");
2811 if (IVIncSet.count(UseI))
2815 LSRFixup &LF = getNewFixup();
2816 LF.UserInst = UserInst;
2817 LF.OperandValToReplace = UI->getOperandValToReplace();
2818 LF.PostIncLoops = UI->getPostIncLoops();
2820 LSRUse::KindType Kind = LSRUse::Basic;
2822 if (isAddressUse(LF.UserInst, LF.OperandValToReplace)) {
2823 Kind = LSRUse::Address;
2824 AccessTy = getAccessType(LF.UserInst);
2827 const SCEV *S = IU.getExpr(*UI);
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);
2843 NV = CI->getOperand(1);
2847 // x == y --> x - y == 0
2848 const SCEV *N = SE.getSCEV(NV);
2849 if (SE.isLoopInvariant(N, L) && isSafeToExpand(N)) {
2850 // S is normalized, so normalize N before folding it into S
2851 // to keep the result normalized.
2852 N = TransformForPostIncUse(Normalize, N, CI, 0,
2853 LF.PostIncLoops, SE, DT);
2854 Kind = LSRUse::ICmpZero;
2855 S = SE.getMinusSCEV(N, S);
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]);
2866 // Set up the initial formula for this use.
2867 std::pair<size_t, int64_t> P = getUse(S, Kind, AccessTy);
2869 LF.Offset = P.second;
2870 LSRUse &LU = Uses[LF.LUIdx];
2871 LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L);
2872 if (!LU.WidestFixupType ||
2873 SE.getTypeSizeInBits(LU.WidestFixupType) <
2874 SE.getTypeSizeInBits(LF.OperandValToReplace->getType()))
2875 LU.WidestFixupType = LF.OperandValToReplace->getType();
2877 // If this is the first use of this LSRUse, give it a formula.
2878 if (LU.Formulae.empty()) {
2879 InsertInitialFormula(S, LU, LF.LUIdx);
2880 CountRegisters(LU.Formulae.back(), LF.LUIdx);
2884 DEBUG(print_fixups(dbgs()));
2887 /// InsertInitialFormula - Insert a formula for the given expression into
2888 /// the given use, separating out loop-variant portions from loop-invariant
2889 /// and loop-computable portions.
2891 LSRInstance::InsertInitialFormula(const SCEV *S, LSRUse &LU, size_t LUIdx) {
2893 F.InitialMatch(S, L, SE);
2894 bool Inserted = InsertFormula(LU, LUIdx, F);
2895 assert(Inserted && "Initial formula already exists!"); (void)Inserted;
2898 /// InsertSupplementalFormula - Insert a simple single-register formula for
2899 /// the given expression into the given use.
2901 LSRInstance::InsertSupplementalFormula(const SCEV *S,
2902 LSRUse &LU, size_t LUIdx) {
2904 F.BaseRegs.push_back(S);
2905 F.HasBaseReg = true;
2906 bool Inserted = InsertFormula(LU, LUIdx, F);
2907 assert(Inserted && "Supplemental formula already exists!"); (void)Inserted;
2910 /// CountRegisters - Note which registers are used by the given formula,
2911 /// updating RegUses.
2912 void LSRInstance::CountRegisters(const Formula &F, size_t LUIdx) {
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);
2920 /// InsertFormula - If the given formula has not yet been inserted, add it to
2921 /// the list, and return true. Return false otherwise.
2922 bool LSRInstance::InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F) {
2923 if (!LU.InsertFormula(F))
2926 CountRegisters(F, LUIdx);
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?
2936 LSRInstance::CollectLoopInvariantFixupsAndFormulae() {
2937 SmallVector<const SCEV *, 8> Worklist(RegUses.begin(), RegUses.end());
2938 SmallPtrSet<const SCEV *, 8> Inserted;
2940 while (!Worklist.empty()) {
2941 const SCEV *S = Worklist.pop_back_val();
2943 if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S))
2944 Worklist.append(N->op_begin(), N->op_end());
2945 else if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S))
2946 Worklist.push_back(C->getOperand());
2947 else if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) {
2948 Worklist.push_back(D->getLHS());
2949 Worklist.push_back(D->getRHS());
2950 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2951 if (!Inserted.insert(U)) continue;
2952 const Value *V = U->getValue();
2953 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
2954 // Look for instructions defined outside the loop.
2955 if (L->contains(Inst)) continue;
2956 } else if (isa<UndefValue>(V))
2957 // Undef doesn't have a live range, so it doesn't matter.
2959 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
2961 const Instruction *UserInst = dyn_cast<Instruction>(*UI);
2962 // Ignore non-instructions.
2965 // Ignore instructions in other functions (as can happen with
2967 if (UserInst->getParent()->getParent() != L->getHeader()->getParent())
2969 // Ignore instructions not dominated by the loop.
2970 const BasicBlock *UseBB = !isa<PHINode>(UserInst) ?
2971 UserInst->getParent() :
2972 cast<PHINode>(UserInst)->getIncomingBlock(
2973 PHINode::getIncomingValueNumForOperand(UI.getOperandNo()));
2974 if (!DT.dominates(L->getHeader(), UseBB))
2976 // Ignore uses which are part of other SCEV expressions, to avoid
2977 // analyzing them multiple times.
2978 if (SE.isSCEVable(UserInst->getType())) {
2979 const SCEV *UserS = SE.getSCEV(const_cast<Instruction *>(UserInst));
2980 // If the user is a no-op, look through to its uses.
2981 if (!isa<SCEVUnknown>(UserS))
2985 SE.getUnknown(const_cast<Instruction *>(UserInst)));
2989 // Ignore icmp instructions which are already being analyzed.
2990 if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UserInst)) {
2991 unsigned OtherIdx = !UI.getOperandNo();
2992 Value *OtherOp = const_cast<Value *>(ICI->getOperand(OtherIdx));
2993 if (SE.hasComputableLoopEvolution(SE.getSCEV(OtherOp), L))
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);
3002 LF.Offset = P.second;
3003 LSRUse &LU = Uses[LF.LUIdx];
3004 LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L);
3005 if (!LU.WidestFixupType ||
3006 SE.getTypeSizeInBits(LU.WidestFixupType) <
3007 SE.getTypeSizeInBits(LF.OperandValToReplace->getType()))
3008 LU.WidestFixupType = LF.OperandValToReplace->getType();
3009 InsertSupplementalFormula(U, LU, LF.LUIdx);
3010 CountRegisters(LU.Formulae.back(), Uses.size() - 1);
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.
3020 /// Return remainder expression after factoring the subexpressions captured by
3021 /// Ops. If Ops is complete, return NULL.
3022 static const SCEV *CollectSubexprs(const SCEV *S, const SCEVConstant *C,
3023 SmallVectorImpl<const SCEV *> &Ops,
3025 ScalarEvolution &SE,
3026 unsigned Depth = 0) {
3027 // Arbitrarily cap recursion to protect compile time.
3031 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
3032 // Break out add operands.
3033 for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
3035 const SCEV *Remainder = CollectSubexprs(*I, C, Ops, L, SE, Depth+1);
3037 Ops.push_back(C ? SE.getMulExpr(C, Remainder) : Remainder);
3040 } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
3041 // Split a non-zero base out of an addrec.
3042 if (AR->getStart()->isZero())
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);
3053 if (Remainder != AR->getStart()) {
3055 Remainder = SE.getConstant(AR->getType(), 0);
3056 return SE.getAddRecExpr(Remainder,
3057 AR->getStepRecurrence(SE),
3059 //FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
3062 } else if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
3063 // Break (C * (a + b + c)) into C*a + C*b + C*c.
3064 if (Mul->getNumOperands() != 2)
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);
3072 Ops.push_back(SE.getMulExpr(C, Remainder));
3079 /// GenerateReassociations - Split out subexpressions from adds and the bases of
3081 void LSRInstance::GenerateReassociations(LSRUse &LU, unsigned LUIdx,
3084 // Arbitrarily cap recursion to protect compile time.
3085 if (Depth >= 3) return;
3087 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) {
3088 const SCEV *BaseReg = Base.BaseRegs[i];
3090 SmallVector<const SCEV *, 8> AddOps;
3091 const SCEV *Remainder = CollectSubexprs(BaseReg, 0, AddOps, L, SE);
3093 AddOps.push_back(Remainder);
3095 if (AddOps.size() == 1) continue;
3097 for (SmallVectorImpl<const SCEV *>::const_iterator J = AddOps.begin(),
3098 JE = AddOps.end(); J != JE; ++J) {
3100 // Loop-variant "unknown" values are uninteresting; we won't be able to
3101 // do anything meaningful with them.
3102 if (isa<SCEVUnknown>(*J) && !SE.isLoopInvariant(*J, L))
3105 // Don't pull a constant into a register if the constant could be folded
3106 // into an immediate field.
3107 if (isAlwaysFoldable(TTI, SE, LU.MinOffset, LU.MaxOffset, LU.Kind,
3108 LU.AccessTy, *J, Base.getNumRegs() > 1))
3111 // Collect all operands except *J.
3112 SmallVector<const SCEV *, 8> InnerAddOps
3113 (((const SmallVector<const SCEV *, 8> &)AddOps).begin(), J);
3115 (llvm::next(J), ((const SmallVector<const SCEV *, 8> &)AddOps).end());
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 &&
3120 isAlwaysFoldable(TTI, SE, LU.MinOffset, LU.MaxOffset, LU.Kind,
3121 LU.AccessTy, InnerAddOps[0], Base.getNumRegs() > 1))
3124 const SCEV *InnerSum = SE.getAddExpr(InnerAddOps);
3125 if (InnerSum->isZero())
3129 // Add the remaining pieces of the add back into the new formula.
3130 const SCEVConstant *InnerSumSC = dyn_cast<SCEVConstant>(InnerSum);
3132 SE.getTypeSizeInBits(InnerSumSC->getType()) <= 64 &&
3133 TTI.isLegalAddImmediate((uint64_t)F.UnfoldedOffset +
3134 InnerSumSC->getValue()->getZExtValue())) {
3135 F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset +
3136 InnerSumSC->getValue()->getZExtValue();
3137 F.BaseRegs.erase(F.BaseRegs.begin() + i);
3139 F.BaseRegs[i] = InnerSum;
3141 // Add J as its own register, or an unfolded immediate.
3142 const SCEVConstant *SC = dyn_cast<SCEVConstant>(*J);
3143 if (SC && SE.getTypeSizeInBits(SC->getType()) <= 64 &&
3144 TTI.isLegalAddImmediate((uint64_t)F.UnfoldedOffset +
3145 SC->getValue()->getZExtValue()))
3146 F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset +
3147 SC->getValue()->getZExtValue();
3149 F.BaseRegs.push_back(*J);
3151 if (InsertFormula(LU, LUIdx, F))
3152 // If that formula hadn't been seen before, recurse to find more like
3154 GenerateReassociations(LU, LUIdx, LU.Formulae.back(), Depth+1);
3159 /// GenerateCombinations - Generate a formula consisting of all of the
3160 /// loop-dominating registers added into a single register.
3161 void LSRInstance::GenerateCombinations(LSRUse &LU, unsigned LUIdx,
3163 // This method is only interesting on a plurality of registers.
3164 if (Base.BaseRegs.size() <= 1) return;
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;
3172 if (SE.properlyDominates(BaseReg, L->getHeader()) &&
3173 !SE.hasComputableLoopEvolution(BaseReg, L))
3174 Ops.push_back(BaseReg);
3176 F.BaseRegs.push_back(BaseReg);
3178 if (Ops.size() > 1) {
3179 const SCEV *Sum = SE.getAddExpr(Ops);
3180 // TODO: If Sum is zero, it probably means ScalarEvolution missed an
3181 // opportunity to fold something. For now, just ignore such cases
3182 // rather than proceed with zero in a register.
3183 if (!Sum->isZero()) {
3184 F.BaseRegs.push_back(Sum);
3185 (void)InsertFormula(LU, LUIdx, F);
3190 /// GenerateSymbolicOffsets - Generate reuse formulae using symbolic offsets.
3191 void LSRInstance::GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx,
3193 // We can't add a symbolic offset if the address already contains one.
3194 if (Base.BaseGV) return;
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)
3203 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F))
3206 (void)InsertFormula(LU, LUIdx, F);
3210 /// GenerateConstantOffsets - Generate reuse formulae using symbolic offsets.
3211 void LSRInstance::GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx,
3213 // TODO: For now, just add the min and max offset, because it usually isn't
3214 // worthwhile looking at everything inbetween.
3215 SmallVector<int64_t, 2> Worklist;
3216 Worklist.push_back(LU.MinOffset);
3217 if (LU.MaxOffset != LU.MinOffset)
3218 Worklist.push_back(LU.MaxOffset);
3220 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) {
3221 const SCEV *G = Base.BaseRegs[i];
3223 for (SmallVectorImpl<int64_t>::const_iterator I = Worklist.begin(),
3224 E = Worklist.end(); I != E; ++I) {
3226 F.BaseOffset = (uint64_t)Base.BaseOffset - *I;
3227 if (isLegalUse(TTI, LU.MinOffset - *I, LU.MaxOffset - *I, LU.Kind,
3229 // Add the offset to the base register.
3230 const SCEV *NewG = SE.getAddExpr(SE.getConstant(G->getType(), *I), G);
3231 // If it cancelled out, drop the base register, otherwise update it.
3232 if (NewG->isZero()) {
3233 std::swap(F.BaseRegs[i], F.BaseRegs.back());
3234 F.BaseRegs.pop_back();
3236 F.BaseRegs[i] = NewG;
3238 (void)InsertFormula(LU, LUIdx, F);
3242 int64_t Imm = ExtractImmediate(G, SE);
3243 if (G->isZero() || Imm == 0)
3246 F.BaseOffset = (uint64_t)F.BaseOffset + Imm;
3247 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F))
3250 (void)InsertFormula(LU, LUIdx, F);
3254 /// GenerateICmpZeroScales - For ICmpZero, check to see if we can scale up
3255 /// the comparison. For example, x == y -> x*c == y*c.
3256 void LSRInstance::GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx,
3258 if (LU.Kind != LSRUse::ICmpZero) return;
3260 // Determine the integer type for the base formula.
3261 Type *IntTy = Base.getType();
3263 if (SE.getTypeSizeInBits(IntTy) > 64) return;
3265 // Don't do this if there is more than one offset.
3266 if (LU.MinOffset != LU.MaxOffset) return;
3268 assert(!Base.BaseGV && "ICmpZero use is not legal!");
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;
3275 // Check that the multiplication doesn't overflow.
3276 if (Base.BaseOffset == INT64_MIN && Factor == -1)
3278 int64_t NewBaseOffset = (uint64_t)Base.BaseOffset * Factor;
3279 if (NewBaseOffset / Factor != Base.BaseOffset)
3282 // Check that multiplying with the use offset doesn't overflow.
3283 int64_t Offset = LU.MinOffset;
3284 if (Offset == INT64_MIN && Factor == -1)
3286 Offset = (uint64_t)Offset * Factor;
3287 if (Offset / Factor != LU.MinOffset)
3291 F.BaseOffset = NewBaseOffset;
3293 // Check that this scale is legal.
3294 if (!isLegalUse(TTI, Offset, Offset, LU.Kind, LU.AccessTy, F))
3297 // Compensate for the use having MinOffset built into it.
3298 F.BaseOffset = (uint64_t)F.BaseOffset + Offset - LU.MinOffset;
3300 const SCEV *FactorS = SE.getConstant(IntTy, Factor);
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);
3305 if (getExactSDiv(F.BaseRegs[i], FactorS, SE) != Base.BaseRegs[i])
3309 // Check that multiplying with the scaled register doesn't overflow.
3311 F.ScaledReg = SE.getMulExpr(F.ScaledReg, FactorS);
3312 if (getExactSDiv(F.ScaledReg, FactorS, SE) != Base.ScaledReg)
3316 // Check that multiplying with the unfolded offset doesn't overflow.
3317 if (F.UnfoldedOffset != 0) {
3318 if (F.UnfoldedOffset == INT64_MIN && Factor == -1)
3320 F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset * Factor;
3321 if (F.UnfoldedOffset / Factor != Base.UnfoldedOffset)
3325 // If we make it here and it's legal, add it.
3326 (void)InsertFormula(LU, LUIdx, F);
3331 /// GenerateScales - Generate stride factor reuse formulae by making use of
3332 /// scaled-offset address modes, for example.
3333 void LSRInstance::GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base) {
3334 // Determine the integer type for the base formula.
3335 Type *IntTy = Base.getType();
3338 // If this Formula already has a scaled register, we can't add another one.
3339 if (Base.Scale != 0) return;
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;
3346 Base.Scale = Factor;
3347 Base.HasBaseReg = Base.BaseRegs.size() > 1;
3348 // Check whether this scale is going to be legal.
3349 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
3351 // As a special-case, handle special out-of-loop Basic users specially.
3352 // TODO: Reconsider this special case.
3353 if (LU.Kind == LSRUse::Basic &&
3354 isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LSRUse::Special,
3355 LU.AccessTy, Base) &&
3356 LU.AllFixupsOutsideLoop)
3357 LU.Kind = LSRUse::Special;
3361 // For an ICmpZero, negating a solitary base register won't lead to
3363 if (LU.Kind == LSRUse::ICmpZero &&
3364 !Base.HasBaseReg && Base.BaseOffset == 0 && !Base.BaseGV)
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])) {
3370 const SCEV *FactorS = SE.getConstant(IntTy, Factor);
3371 if (FactorS->isZero())
3373 // Divide out the factor, ignoring high bits, since we'll be
3374 // scaling the value back up in the end.
3375 if (const SCEV *Quotient = getExactSDiv(AR, FactorS, SE, true)) {
3376 // TODO: This could be optimized to avoid all the copying.
3378 F.ScaledReg = Quotient;
3379 F.DeleteBaseReg(F.BaseRegs[i]);
3380 (void)InsertFormula(LU, LUIdx, F);
3386 /// GenerateTruncates - Generate reuse formulae from different IV types.
3387 void LSRInstance::GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base) {
3388 // Don't bother truncating symbolic values.
3389 if (Base.BaseGV) return;
3391 // Determine the integer type for the base formula.
3392 Type *DstTy = Base.getType();
3394 DstTy = SE.getEffectiveSCEVType(DstTy);
3396 for (SmallSetVector<Type *, 4>::const_iterator
3397 I = Types.begin(), E = Types.end(); I != E; ++I) {
3399 if (SrcTy != DstTy && TTI.isTruncateFree(SrcTy, DstTy)) {
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);
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))
3412 (void)InsertFormula(LU, LUIdx, F);
3419 /// WorkItem - Helper class for GenerateCrossUseConstantOffsets. It's used to
3420 /// defer modifications so that the search phase doesn't have to worry about
3421 /// the data structures moving underneath it.
3425 const SCEV *OrigReg;
3427 WorkItem(size_t LI, int64_t I, const SCEV *R)
3428 : LUIdx(LI), Imm(I), OrigReg(R) {}
3430 void print(raw_ostream &OS) const;
3436 void WorkItem::print(raw_ostream &OS) const {
3437 OS << "in formulae referencing " << *OrigReg << " in use " << LUIdx
3438 << " , add offset " << Imm;
3441 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3442 void WorkItem::dump() const {
3443 print(errs()); errs() << '\n';
3447 /// GenerateCrossUseConstantOffsets - Look for registers which are a constant
3448 /// distance apart and try to form reuse opportunities between them.
3449 void LSRInstance::GenerateCrossUseConstantOffsets() {
3450 // Group the registers by their value without any added constant offset.
3451 typedef std::map<int64_t, const SCEV *> ImmMapTy;
3452 typedef DenseMap<const SCEV *, ImmMapTy> RegMapTy;
3454 DenseMap<const SCEV *, SmallBitVector> UsedByIndicesMap;
3455 SmallVector<const SCEV *, 8> Sequence;
3456 for (RegUseTracker::const_iterator I = RegUses.begin(), E = RegUses.end();
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()));
3463 Sequence.push_back(Reg);
3464 Pair.first->second.insert(std::make_pair(Imm, *I));
3465 UsedByIndicesMap[Reg] |= RegUses.getUsedByIndices(*I);
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.
3471 SmallVector<WorkItem, 32> WorkItems;
3472 SmallSet<std::pair<size_t, int64_t>, 32> UniqueItems;
3473 for (SmallVectorImpl<const SCEV *>::const_iterator I = Sequence.begin(),
3474 E = Sequence.end(); I != E; ++I) {
3475 const SCEV *Reg = *I;
3476 const ImmMapTy &Imms = Map.find(Reg)->second;
3478 // It's not worthwhile looking for reuse if there's only one offset.
3479 if (Imms.size() == 1)
3482 DEBUG(dbgs() << "Generating cross-use offsets for " << *Reg << ':';
3483 for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end();
3485 dbgs() << ' ' << J->first;
3488 // Examine each offset.
3489 for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end();
3491 const SCEV *OrigReg = J->second;
3493 int64_t JImm = J->first;
3494 const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(OrigReg);
3496 if (!isa<SCEVConstant>(OrigReg) &&
3497 UsedByIndicesMap[Reg].count() == 1) {
3498 DEBUG(dbgs() << "Skipping cross-use reuse for " << *OrigReg << '\n');
3502 // Conservatively examine offsets between this orig reg a few selected
3504 ImmMapTy::const_iterator OtherImms[] = {
3505 Imms.begin(), prior(Imms.end()),
3506 Imms.lower_bound((Imms.begin()->first + prior(Imms.end())->first) / 2)
3508 for (size_t i = 0, e = array_lengthof(OtherImms); i != e; ++i) {
3509 ImmMapTy::const_iterator M = OtherImms[i];
3510 if (M == J || M == JE) continue;
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;
3515 LUIdx = UsedByIndices.find_next(LUIdx))
3516 // Make a memo of this use, offset, and register tuple.
3517 if (UniqueItems.insert(std::make_pair(LUIdx, Imm)))
3518 WorkItems.push_back(WorkItem(LUIdx, Imm, OrigReg));
3525 UsedByIndicesMap.clear();
3526 UniqueItems.clear();
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;
3537 Type *IntTy = SE.getEffectiveSCEVType(OrigReg->getType());
3538 const SCEV *NegImmS = SE.getSCEV(ConstantInt::get(IntTy, -(uint64_t)Imm));
3539 unsigned BitWidth = SE.getTypeSizeInBits(IntTy);
3541 // TODO: Use a more targeted data structure.
3542 for (size_t L = 0, LE = LU.Formulae.size(); L != LE; ++L) {
3543 const Formula &F = LU.Formulae[L];
3544 // Use the immediate in the scaled register.
3545 if (F.ScaledReg == OrigReg) {
3546 int64_t Offset = (uint64_t)F.BaseOffset + Imm * (uint64_t)F.Scale;
3547 // Don't create 50 + reg(-50).
3548 if (F.referencesReg(SE.getSCEV(
3549 ConstantInt::get(IntTy, -(uint64_t)Offset))))
3552 NewF.BaseOffset = Offset;
3553 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
3556 NewF.ScaledReg = SE.getAddExpr(NegImmS, NewF.ScaledReg);
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))
3562 if (C->getValue()->isNegative() !=
3563 (NewF.BaseOffset < 0) &&
3564 (C->getValue()->getValue().abs() * APInt(BitWidth, F.Scale))
3565 .ule(abs64(NewF.BaseOffset)))
3569 (void)InsertFormula(LU, LUIdx, NewF);
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)
3577 NewF.BaseOffset = (uint64_t)NewF.BaseOffset + Imm;
3578 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset,
3579 LU.Kind, LU.AccessTy, NewF)) {
3580 if (!TTI.isLegalAddImmediate((uint64_t)NewF.UnfoldedOffset + Imm))
3583 NewF.UnfoldedOffset = (uint64_t)NewF.UnfoldedOffset + Imm;
3585 NewF.BaseRegs[N] = SE.getAddExpr(NegImmS, BaseReg);
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();
3593 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(*J))
3594 if ((C->getValue()->getValue() + NewF.BaseOffset).abs().slt(
3595 abs64(NewF.BaseOffset)) &&
3596 (C->getValue()->getValue() +
3597 NewF.BaseOffset).countTrailingZeros() >=
3598 CountTrailingZeros_64(NewF.BaseOffset))
3602 (void)InsertFormula(LU, LUIdx, NewF);
3611 /// GenerateAllReuseFormulae - Generate formulae for each use.
3613 LSRInstance::GenerateAllReuseFormulae() {
3614 // This is split into multiple loops so that hasRegsUsedByUsesOtherThan
3615 // queries are more precise.
3616 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3617 LSRUse &LU = Uses[LUIdx];
3618 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
3619 GenerateReassociations(LU, LUIdx, LU.Formulae[i]);
3620 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
3621 GenerateCombinations(LU, LUIdx, LU.Formulae[i]);
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]);
3634 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3635 LSRUse &LU = Uses[LUIdx];
3636 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
3637 GenerateTruncates(LU, LUIdx, LU.Formulae[i]);
3640 GenerateCrossUseConstantOffsets();
3642 DEBUG(dbgs() << "\n"
3643 "After generating reuse formulae:\n";
3644 print_uses(dbgs()));
3647 /// If there are multiple formulae with the same set of registers used
3648 /// by other uses, pick the best one and delete the others.
3649 void LSRInstance::FilterOutUndesirableDedicatedRegisters() {
3650 DenseSet<const SCEV *> VisitedRegs;
3651 SmallPtrSet<const SCEV *, 16> Regs;
3652 SmallPtrSet<const SCEV *, 16> LoserRegs;
3654 bool ChangedFormulae = false;
3657 // Collect the best formula for each unique set of shared registers. This
3658 // is reset for each use.
3659 typedef DenseMap<SmallVector<const SCEV *, 2>, size_t, UniquifierDenseMapInfo>
3661 BestFormulaeTy BestFormulae;
3663 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3664 LSRUse &LU = Uses[LUIdx];
3665 DEBUG(dbgs() << "Filtering for use "; LU.print(dbgs()); dbgs() << '\n');
3668 for (size_t FIdx = 0, NumForms = LU.Formulae.size();
3669 FIdx != NumForms; ++FIdx) {
3670 Formula &F = LU.Formulae[FIdx];
3672 // Some formulas are instant losers. For example, they may depend on
3673 // nonexistent AddRecs from other loops. These need to be filtered
3674 // immediately, otherwise heuristics could choose them over others leading
3675 // to an unsatisfactory solution. Passing LoserRegs into RateFormula here
3676 // avoids the need to recompute this information across formulae using the
3677 // same bad AddRec. Passing LoserRegs is also essential unless we remove
3678 // the corresponding bad register from the Regs set.
3681 CostF.RateFormula(F, Regs, VisitedRegs, L, LU.Offsets, SE, DT,
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());
3694 SmallVector<const SCEV *, 2> Key;
3695 for (SmallVectorImpl<const SCEV *>::const_iterator J = F.BaseRegs.begin(),
3696 JE = F.BaseRegs.end(); J != JE; ++J) {
3697 const SCEV *Reg = *J;
3698 if (RegUses.isRegUsedByUsesOtherThan(Reg, LUIdx))
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
3706 std::sort(Key.begin(), Key.end());
3708 std::pair<BestFormulaeTy::const_iterator, bool> P =
3709 BestFormulae.insert(std::make_pair(Key, FIdx));
3713 Formula &Best = LU.Formulae[P.first->second];
3717 CostBest.RateFormula(Best, Regs, VisitedRegs, L, LU.Offsets, SE, DT);
3718 if (CostF < CostBest)
3720 DEBUG(dbgs() << " Filtering out formula "; F.print(dbgs());
3722 " in favor of formula "; Best.print(dbgs());
3726 ChangedFormulae = true;
3728 LU.DeleteFormula(F);
3734 // Now that we've filtered out some formulae, recompute the Regs set.
3736 LU.RecomputeRegs(LUIdx, RegUses);
3738 // Reset this to prepare for the next use.
3739 BestFormulae.clear();
3742 DEBUG(if (ChangedFormulae) {
3744 "After filtering out undesirable candidates:\n";
3749 // This is a rough guess that seems to work fairly well.
3750 static const size_t ComplexityLimit = UINT16_MAX;
3752 /// EstimateSearchSpaceComplexity - Estimate the worst-case number of
3753 /// solutions the solver might have to consider. It almost never considers
3754 /// this many solutions because it prune the search space, but the pruning
3755 /// isn't always sufficient.
3756 size_t LSRInstance::EstimateSearchSpaceComplexity() const {
3758 for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(),
3759 E = Uses.end(); I != E; ++I) {
3760 size_t FSize = I->Formulae.size();
3761 if (FSize >= ComplexityLimit) {
3762 Power = ComplexityLimit;
3766 if (Power >= ComplexityLimit)
3772 /// NarrowSearchSpaceByDetectingSupersets - When one formula uses a superset
3773 /// of the registers of another formula, it won't help reduce register
3774 /// pressure (though it may not necessarily hurt register pressure); remove
3775 /// it to simplify the system.
3776 void LSRInstance::NarrowSearchSpaceByDetectingSupersets() {
3777 if (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
3778 DEBUG(dbgs() << "The search space is too complex.\n");
3780 DEBUG(dbgs() << "Narrowing the search space by eliminating formulae "
3781 "which use a superset of registers used by other "
3784 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3785 LSRUse &LU = Uses[LUIdx];
3787 for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
3788 Formula &F = LU.Formulae[i];
3789 // Look for a formula with a constant or GV in a register. If the use
3790 // also has a formula with that same value in an immediate field,
3791 // delete the one that uses a register.
3792 for (SmallVectorImpl<const SCEV *>::const_iterator
3793 I = F.BaseRegs.begin(), E = F.BaseRegs.end(); I != E; ++I) {
3794 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(*I)) {
3796 NewF.BaseOffset += C->getValue()->getSExtValue();
3797 NewF.BaseRegs.erase(NewF.BaseRegs.begin() +
3798 (I - F.BaseRegs.begin()));
3799 if (LU.HasFormulaWithSameRegs(NewF)) {
3800 DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n');
3801 LU.DeleteFormula(F);
3807 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(*I)) {
3808 if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue()))
3812 NewF.BaseRegs.erase(NewF.BaseRegs.begin() +
3813 (I - F.BaseRegs.begin()));
3814 if (LU.HasFormulaWithSameRegs(NewF)) {
3815 DEBUG(dbgs() << " Deleting "; F.print(dbgs());
3817 LU.DeleteFormula(F);
3828 LU.RecomputeRegs(LUIdx, RegUses);
3831 DEBUG(dbgs() << "After pre-selection:\n";
3832 print_uses(dbgs()));
3836 /// NarrowSearchSpaceByCollapsingUnrolledCode - When there are many registers
3837 /// for expressions like A, A+1, A+2, etc., allocate a single register for
3839 void LSRInstance::NarrowSearchSpaceByCollapsingUnrolledCode() {
3840 if (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
3841 DEBUG(dbgs() << "The search space is too complex.\n");
3843 DEBUG(dbgs() << "Narrowing the search space by assuming that uses "
3844 "separated by a constant offset will use the same "
3847 // This is especially useful for unrolled loops.
3849 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3850 LSRUse &LU = Uses[LUIdx];
3851 for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(),
3852 E = LU.Formulae.end(); I != E; ++I) {
3853 const Formula &F = *I;
3854 if (F.BaseOffset != 0 && F.Scale == 0) {
3855 if (LSRUse *LUThatHas = FindUseWithSimilarFormula(F, LU)) {
3856 if (reconcileNewOffset(*LUThatHas, F.BaseOffset,
3857 /*HasBaseReg=*/false,
3858 LU.Kind, LU.AccessTy)) {
3859 DEBUG(dbgs() << " Deleting use "; LU.print(dbgs());
3862 LUThatHas->AllFixupsOutsideLoop &= LU.AllFixupsOutsideLoop;
3864 // Update the relocs to reference the new use.
3865 for (SmallVectorImpl<LSRFixup>::iterator I = Fixups.begin(),
3866 E = Fixups.end(); I != E; ++I) {
3867 LSRFixup &Fixup = *I;
3868 if (Fixup.LUIdx == LUIdx) {
3869 Fixup.LUIdx = LUThatHas - &Uses.front();
3870 Fixup.Offset += F.BaseOffset;
3871 // Add the new offset to LUThatHas' offset list.
3872 if (LUThatHas->Offsets.back() != Fixup.Offset) {
3873 LUThatHas->Offsets.push_back(Fixup.Offset);
3874 if (Fixup.Offset > LUThatHas->MaxOffset)
3875 LUThatHas->MaxOffset = Fixup.Offset;
3876 if (Fixup.Offset < LUThatHas->MinOffset)
3877 LUThatHas->MinOffset = Fixup.Offset;
3879 DEBUG(dbgs() << "New fixup has offset "
3880 << Fixup.Offset << '\n');
3882 if (Fixup.LUIdx == NumUses-1)
3883 Fixup.LUIdx = LUIdx;
3886 // Delete formulae from the new use which are no longer legal.
3888 for (size_t i = 0, e = LUThatHas->Formulae.size(); i != e; ++i) {
3889 Formula &F = LUThatHas->Formulae[i];
3890 if (!isLegalUse(TTI, LUThatHas->MinOffset, LUThatHas->MaxOffset,
3891 LUThatHas->Kind, LUThatHas->AccessTy, F)) {
3892 DEBUG(dbgs() << " Deleting "; F.print(dbgs());
3894 LUThatHas->DeleteFormula(F);
3901 LUThatHas->RecomputeRegs(LUThatHas - &Uses.front(), RegUses);
3903 // Delete the old use.
3904 DeleteUse(LU, LUIdx);
3914 DEBUG(dbgs() << "After pre-selection:\n";
3915 print_uses(dbgs()));
3919 /// NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters - Call
3920 /// FilterOutUndesirableDedicatedRegisters again, if necessary, now that
3921 /// we've done more filtering, as it may be able to find more formulae to
3923 void LSRInstance::NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters(){
3924 if (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
3925 DEBUG(dbgs() << "The search space is too complex.\n");
3927 DEBUG(dbgs() << "Narrowing the search space by re-filtering out "
3928 "undesirable dedicated registers.\n");
3930 FilterOutUndesirableDedicatedRegisters();
3932 DEBUG(dbgs() << "After pre-selection:\n";
3933 print_uses(dbgs()));
3937 /// NarrowSearchSpaceByPickingWinnerRegs - Pick a register which seems likely
3938 /// to be profitable, and then in any use which has any reference to that
3939 /// register, delete all formulae which do not reference that register.
3940 void LSRInstance::NarrowSearchSpaceByPickingWinnerRegs() {
3941 // With all other options exhausted, loop until the system is simple
3942 // enough to handle.
3943 SmallPtrSet<const SCEV *, 4> Taken;
3944 while (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
3945 // Ok, we have too many of formulae on our hands to conveniently handle.
3946 // Use a rough heuristic to thin out the list.
3947 DEBUG(dbgs() << "The search space is too complex.\n");
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();
3955 const SCEV *Reg = *I;
3956 if (Taken.count(Reg))
3961 unsigned Count = RegUses.getUsedByIndices(Reg).count();
3962 if (Count > BestNum) {
3969 DEBUG(dbgs() << "Narrowing the search space by assuming " << *Best
3970 << " will yield profitable reuse.\n");
3973 // In any use with formulae which references this register, delete formulae
3974 // which don't reference it.
3975 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3976 LSRUse &LU = Uses[LUIdx];
3977 if (!LU.Regs.count(Best)) continue;
3980 for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
3981 Formula &F = LU.Formulae[i];
3982 if (!F.referencesReg(Best)) {
3983 DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n');
3984 LU.DeleteFormula(F);
3988 assert(e != 0 && "Use has no formulae left! Is Regs inconsistent?");
3994 LU.RecomputeRegs(LUIdx, RegUses);
3997 DEBUG(dbgs() << "After pre-selection:\n";
3998 print_uses(dbgs()));
4002 /// NarrowSearchSpaceUsingHeuristics - If there are an extraordinary number of
4003 /// formulae to choose from, use some rough heuristics to prune down the number
4004 /// of formulae. This keeps the main solver from taking an extraordinary amount
4005 /// of time in some worst-case scenarios.
4006 void LSRInstance::NarrowSearchSpaceUsingHeuristics() {
4007 NarrowSearchSpaceByDetectingSupersets();
4008 NarrowSearchSpaceByCollapsingUnrolledCode();
4009 NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters();
4010 NarrowSearchSpaceByPickingWinnerRegs();
4013 /// SolveRecurse - This is the recursive solver.
4014 void LSRInstance::SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
4016 SmallVectorImpl<const Formula *> &Workspace,
4017 const Cost &CurCost,
4018 const SmallPtrSet<const SCEV *, 16> &CurRegs,
4019 DenseSet<const SCEV *> &VisitedRegs) const {
4022 // - use more aggressive filtering
4023 // - sort the formula so that the most profitable solutions are found first
4024 // - sort the uses too
4026 // - don't compute a cost, and then compare. compare while computing a cost
4028 // - track register sets with SmallBitVector
4030 const LSRUse &LU = Uses[Workspace.size()];
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)
4039 if (LU.Regs.count(*I))
4042 SmallPtrSet<const SCEV *, 16> NewRegs;
4044 for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(),
4045 E = LU.Formulae.end(); I != E; ++I) {
4046 const Formula &F = *I;
4048 // Ignore formulae which do not use any of the required registers.
4049 bool SatisfiedReqReg = true;
4050 for (SmallSetVector<const SCEV *, 4>::const_iterator J = ReqRegs.begin(),
4051 JE = ReqRegs.end(); J != JE; ++J) {
4052 const SCEV *Reg = *J;
4053 if ((!F.ScaledReg || F.ScaledReg != Reg) &&
4054 std::find(F.BaseRegs.begin(), F.BaseRegs.end(), Reg) ==
4056 SatisfiedReqReg = false;
4060 if (!SatisfiedReqReg) {
4061 // If none of the formulae satisfied the required registers, then we could
4062 // clear ReqRegs and try again. Currently, we simply give up in this case.
4066 // Evaluate the cost of the current formula. If it's already worse than
4067 // the current best, prune the search at that point.
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]);
4079 DEBUG(dbgs() << "New best at "; NewCost.print(dbgs());
4080 dbgs() << ".\n Regs:";
4081 for (SmallPtrSet<const SCEV *, 16>::const_iterator
4082 I = NewRegs.begin(), E = NewRegs.end(); I != E; ++I)
4083 dbgs() << ' ' << **I;
4086 SolutionCost = NewCost;
4087 Solution = Workspace;
4089 Workspace.pop_back();
4094 /// Solve - Choose one formula from each use. Return the results in the given
4095 /// Solution vector.
4096 void LSRInstance::Solve(SmallVectorImpl<const Formula *> &Solution) const {
4097 SmallVector<const Formula *, 8> Workspace;
4099 SolutionCost.Loose();
4101 SmallPtrSet<const SCEV *, 16> CurRegs;
4102 DenseSet<const SCEV *> VisitedRegs;
4103 Workspace.reserve(Uses.size());
4105 // SolveRecurse does all the work.
4106 SolveRecurse(Solution, SolutionCost, Workspace, CurCost,
4107 CurRegs, VisitedRegs);
4108 if (Solution.empty()) {
4109 DEBUG(dbgs() << "\nNo Satisfactory Solution\n");
4113 // Ok, we've now made all our decisions.
4114 DEBUG(dbgs() << "\n"
4115 "The chosen solution requires "; SolutionCost.print(dbgs());
4117 for (size_t i = 0, e = Uses.size(); i != e; ++i) {
4119 Uses[i].print(dbgs());
4122 Solution[i]->print(dbgs());
4126 assert(Solution.size() == Uses.size() && "Malformed solution!");
4129 /// HoistInsertPosition - Helper for AdjustInsertPositionForExpand. Climb up
4130 /// the dominator tree far as we can go while still being dominated by the
4131 /// input positions. This helps canonicalize the insert position, which
4132 /// encourages sharing.
4133 BasicBlock::iterator
4134 LSRInstance::HoistInsertPosition(BasicBlock::iterator IP,
4135 const SmallVectorImpl<Instruction *> &Inputs)
4138 const Loop *IPLoop = LI.getLoopFor(IP->getParent());
4139 unsigned IPLoopDepth = IPLoop ? IPLoop->getLoopDepth() : 0;
4142 for (DomTreeNode *Rung = DT.getNode(IP->getParent()); ; ) {
4143 if (!Rung) return IP;
4144 Rung = Rung->getIDom();
4145 if (!Rung) return IP;
4146 IDom = Rung->getBlock();
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))
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;
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() &&
4169 (!BetterPos || !DT.dominates(Inst, BetterPos)))
4170 BetterPos = llvm::next(BasicBlock::iterator(Inst));
4183 /// AdjustInsertPositionForExpand - Determine an input position which will be
4184 /// dominated by the operands and which will dominate the result.
4185 BasicBlock::iterator
4186 LSRInstance::AdjustInsertPositionForExpand(BasicBlock::iterator LowestIP,
4189 SCEVExpander &Rewriter) const {
4190 // Collect some instructions which must be dominated by the
4191 // expanding replacement. These must be dominated by any operands that
4192 // will be required in the expansion.
4193 SmallVector<Instruction *, 4> Inputs;
4194 if (Instruction *I = dyn_cast<Instruction>(LF.OperandValToReplace))
4195 Inputs.push_back(I);
4196 if (LU.Kind == LSRUse::ICmpZero)
4197 if (Instruction *I =
4198 dyn_cast<Instruction>(cast<ICmpInst>(LF.UserInst)->getOperand(1)))
4199 Inputs.push_back(I);
4200 if (LF.PostIncLoops.count(L)) {
4201 if (LF.isUseFullyOutsideLoop(L))
4202 Inputs.push_back(L->getLoopLatch()->getTerminator());
4204 Inputs.push_back(IVIncInsertPos);
4206 // The expansion must also be dominated by the increment positions of any
4207 // loops it for which it is using post-inc mode.
4208 for (PostIncLoopSet::const_iterator I = LF.PostIncLoops.begin(),
4209 E = LF.PostIncLoops.end(); I != E; ++I) {
4210 const Loop *PIL = *I;
4211 if (PIL == L) continue;
4213 // Be dominated by the loop exit.
4214 SmallVector<BasicBlock *, 4> ExitingBlocks;
4215 PIL->getExitingBlocks(ExitingBlocks);
4216 if (!ExitingBlocks.empty()) {
4217 BasicBlock *BB = ExitingBlocks[0];
4218 for (unsigned i = 1, e = ExitingBlocks.size(); i != e; ++i)
4219 BB = DT.findNearestCommonDominator(BB, ExitingBlocks[i]);
4220 Inputs.push_back(BB->getTerminator());
4224 assert(!isa<PHINode>(LowestIP) && !isa<LandingPadInst>(LowestIP)
4225 && !isa<DbgInfoIntrinsic>(LowestIP) &&
4226 "Insertion point must be a normal instruction");
4228 // Then, climb up the immediate dominator tree as far as we can go while
4229 // still being dominated by the input positions.
4230 BasicBlock::iterator IP = HoistInsertPosition(LowestIP, Inputs);
4232 // Don't insert instructions before PHI nodes.
4233 while (isa<PHINode>(IP)) ++IP;
4235 // Ignore landingpad instructions.
4236 while (isa<LandingPadInst>(IP)) ++IP;
4238 // Ignore debug intrinsics.
4239 while (isa<DbgInfoIntrinsic>(IP)) ++IP;
4241 // Set IP below instructions recently inserted by SCEVExpander. This keeps the
4242 // IP consistent across expansions and allows the previously inserted
4243 // instructions to be reused by subsequent expansion.
4244 while (Rewriter.isInsertedInstruction(IP) && IP != LowestIP) ++IP;
4249 /// Expand - Emit instructions for the leading candidate expression for this
4250 /// LSRUse (this is called "expanding").
4251 Value *LSRInstance::Expand(const LSRFixup &LF,
4253 BasicBlock::iterator IP,
4254 SCEVExpander &Rewriter,
4255 SmallVectorImpl<WeakVH> &DeadInsts) const {
4256 const LSRUse &LU = Uses[LF.LUIdx];
4258 // Determine an input position which will be dominated by the operands and
4259 // which will dominate the result.
4260 IP = AdjustInsertPositionForExpand(IP, LF, LU, Rewriter);
4262 // Inform the Rewriter if we have a post-increment use, so that it can
4263 // perform an advantageous expansion.
4264 Rewriter.setPostInc(LF.PostIncLoops);
4266 // This is the type that the user actually needs.
4267 Type *OpTy = LF.OperandValToReplace->getType();
4268 // This will be the type that we'll initially expand to.
4269 Type *Ty = F.getType();
4271 // No type known; just expand directly to the ultimate type.
4273 else if (SE.getEffectiveSCEVType(Ty) == SE.getEffectiveSCEVType(OpTy))
4274 // Expand directly to the ultimate type if it's the right size.
4276 // This is the type to do integer arithmetic in.
4277 Type *IntTy = SE.getEffectiveSCEVType(Ty);
4279 // Build up a list of operands to add together to form the full base.
4280 SmallVector<const SCEV *, 8> Ops;
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!");
4288 // If we're expanding for a post-inc user, make the post-inc adjustment.
4289 PostIncLoopSet &Loops = const_cast<PostIncLoopSet &>(LF.PostIncLoops);
4290 Reg = TransformForPostIncUse(Denormalize, Reg,
4291 LF.UserInst, LF.OperandValToReplace,
4294 Ops.push_back(SE.getUnknown(Rewriter.expandCodeFor(Reg, 0, IP)));
4297 // Expand the ScaledReg portion.
4298 Value *ICmpScaledV = 0;
4300 const SCEV *ScaledS = F.ScaledReg;
4302 // If we're expanding for a post-inc user, make the post-inc adjustment.
4303 PostIncLoopSet &Loops = const_cast<PostIncLoopSet &>(LF.PostIncLoops);
4304 ScaledS = TransformForPostIncUse(Denormalize, ScaledS,
4305 LF.UserInst, LF.OperandValToReplace,
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
4312 assert(F.Scale == -1 &&
4313 "The only scale supported by ICmpZero uses is -1!");
4314 ICmpScaledV = Rewriter.expandCodeFor(ScaledS, 0, IP);
4316 // Otherwise just expand the scaled register and an explicit scale,
4317 // which is expected to be matched as part of the address.
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);
4323 Ops.push_back(SE.getUnknown(FullV));
4325 ScaledS = SE.getUnknown(Rewriter.expandCodeFor(ScaledS, 0, IP));
4326 ScaledS = SE.getMulExpr(ScaledS,
4327 SE.getConstant(ScaledS->getType(), F.Scale));
4328 Ops.push_back(ScaledS);
4332 // Expand the GV portion.
4334 // Flush the operand list to suppress SCEVExpander hoisting.
4336 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, IP);
4338 Ops.push_back(SE.getUnknown(FullV));
4340 Ops.push_back(SE.getUnknown(F.BaseGV));
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.
4346 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, IP);
4348 Ops.push_back(SE.getUnknown(FullV));
4351 // Expand the immediate portion.
4352 int64_t Offset = (uint64_t)F.BaseOffset + LF.Offset;
4354 if (LU.Kind == LSRUse::ICmpZero) {
4355 // The other interesting way of "folding" with an ICmpZero is to use a
4356 // negated immediate.
4358 ICmpScaledV = ConstantInt::get(IntTy, -(uint64_t)Offset);
4360 Ops.push_back(SE.getUnknown(ICmpScaledV));
4361 ICmpScaledV = ConstantInt::get(IntTy, Offset);
4364 // Just add the immediate values. These again are expected to be matched
4365 // as part of the address.
4366 Ops.push_back(SE.getUnknown(ConstantInt::getSigned(IntTy, Offset)));
4370 // Expand the unfolded offset portion.
4371 int64_t UnfoldedOffset = F.UnfoldedOffset;
4372 if (UnfoldedOffset != 0) {
4373 // Just add the immediate values.
4374 Ops.push_back(SE.getUnknown(ConstantInt::getSigned(IntTy,
4378 // Emit instructions summing all the operands.
4379 const SCEV *FullS = Ops.empty() ?
4380 SE.getConstant(IntTy, 0) :
4382 Value *FullV = Rewriter.expandCodeFor(FullS, Ty, IP);
4384 // We're done expanding now, so reset the rewriter.
4385 Rewriter.clearPostInc();
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));
4393 assert(!F.BaseGV && "ICmp does not support folding a global value and "
4394 "a scale at the same time!");
4395 if (F.Scale == -1) {
4396 if (ICmpScaledV->getType() != OpTy) {
4398 CastInst::Create(CastInst::getCastOpcode(ICmpScaledV, false,
4400 ICmpScaledV, OpTy, "tmp", CI);
4403 CI->setOperand(1, ICmpScaledV);
4405 assert(F.Scale == 0 &&
4406 "ICmp does not support folding a global value and "
4407 "a scale at the same time!");
4408 Constant *C = ConstantInt::getSigned(SE.getEffectiveSCEVType(OpTy),
4410 if (C->getType() != OpTy)
4411 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
4415 CI->setOperand(1, C);
4422 /// RewriteForPHI - Helper for Rewrite. PHI nodes are special because the use
4423 /// of their operands effectively happens in their predecessor blocks, so the
4424 /// expression may need to be expanded in multiple places.
4425 void LSRInstance::RewriteForPHI(PHINode *PN,
4428 SCEVExpander &Rewriter,
4429 SmallVectorImpl<WeakVH> &DeadInsts,
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);
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
4440 if (e != 1 && BB->getTerminator()->getNumSuccessors() > 1 &&
4441 !isa<IndirectBrInst>(BB->getTerminator())) {
4442 BasicBlock *Parent = PN->getParent();
4443 Loop *PNLoop = LI.getLoopFor(Parent);
4444 if (!PNLoop || Parent != PNLoop->getHeader()) {
4445 // Split the critical edge.
4446 BasicBlock *NewBB = 0;
4447 if (!Parent->isLandingPad()) {
4448 NewBB = SplitCriticalEdge(BB, Parent, P,
4449 /*MergeIdenticalEdges=*/true,
4450 /*DontDeleteUselessPhis=*/true);
4452 SmallVector<BasicBlock*, 2> NewBBs;
4453 SplitLandingPadPredecessors(Parent, BB, "", "", P, NewBBs);
4456 // If NewBB==NULL, then SplitCriticalEdge refused to split because all
4457 // phi predecessors are identical. The simple thing to do is skip
4458 // splitting in this case rather than complicate the API.
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());
4466 // Splitting the edge can reduce the number of PHI entries we have.
4467 e = PN->getNumIncomingValues();
4469 i = PN->getBasicBlockIndex(BB);
4474 std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> Pair =
4475 Inserted.insert(std::make_pair(BB, static_cast<Value *>(0)));
4477 PN->setIncomingValue(i, Pair.first->second);
4479 Value *FullV = Expand(LF, F, BB->getTerminator(), Rewriter, DeadInsts);
4481 // If this is reuse-by-noop-cast, insert the noop cast.
4482 Type *OpTy = LF.OperandValToReplace->getType();
4483 if (FullV->getType() != OpTy)
4485 CastInst::Create(CastInst::getCastOpcode(FullV, false,
4487 FullV, LF.OperandValToReplace->getType(),
4488 "tmp", BB->getTerminator());
4490 PN->setIncomingValue(i, FullV);
4491 Pair.first->second = FullV;
4496 /// Rewrite - Emit instructions for the leading candidate expression for this
4497 /// LSRUse (this is called "expanding"), and update the UserInst to reference
4498 /// the newly expanded value.
4499 void LSRInstance::Rewrite(const LSRFixup &LF,
4501 SCEVExpander &Rewriter,
4502 SmallVectorImpl<WeakVH> &DeadInsts,
4504 // First, find an insertion point that dominates UserInst. For PHI nodes,
4505 // find the nearest block which dominates all the relevant uses.
4506 if (PHINode *PN = dyn_cast<PHINode>(LF.UserInst)) {
4507 RewriteForPHI(PN, LF, F, Rewriter, DeadInsts, P);
4509 Value *FullV = Expand(LF, F, LF.UserInst, Rewriter, DeadInsts);
4511 // If this is reuse-by-noop-cast, insert the noop cast.
4512 Type *OpTy = LF.OperandValToReplace->getType();
4513 if (FullV->getType() != OpTy) {
4515 CastInst::Create(CastInst::getCastOpcode(FullV, false, OpTy, false),
4516 FullV, OpTy, "tmp", LF.UserInst);
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);
4528 LF.UserInst->replaceUsesOfWith(LF.OperandValToReplace, FullV);
4531 DeadInsts.push_back(LF.OperandValToReplace);
4534 /// ImplementSolution - Rewrite all the fixup locations with new values,
4535 /// following the chosen solution.
4537 LSRInstance::ImplementSolution(const SmallVectorImpl<const Formula *> &Solution,
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;
4543 SCEVExpander Rewriter(SE, "lsr");
4545 Rewriter.setDebugType(DEBUG_TYPE);
4547 Rewriter.disableCanonicalMode();
4548 Rewriter.enableLSRMode();
4549 Rewriter.setIVIncInsertPos(L, IVIncInsertPos);
4551 // Mark phi nodes that terminate chains so the expander tries to reuse them.
4552 for (SmallVectorImpl<IVChain>::const_iterator ChainI = IVChainVec.begin(),
4553 ChainE = IVChainVec.end(); ChainI != ChainE; ++ChainI) {
4554 if (PHINode *PN = dyn_cast<PHINode>(ChainI->tailUserInst()))
4555 Rewriter.setChainedPhi(PN);
4558 // Expand the new value definitions and update the users.
4559 for (SmallVectorImpl<LSRFixup>::const_iterator I = Fixups.begin(),
4560 E = Fixups.end(); I != E; ++I) {
4561 const LSRFixup &Fixup = *I;
4563 Rewrite(Fixup, *Solution[Fixup.LUIdx], Rewriter, DeadInsts, P);
4568 for (SmallVectorImpl<IVChain>::const_iterator ChainI = IVChainVec.begin(),
4569 ChainE = IVChainVec.end(); ChainI != ChainE; ++ChainI) {
4570 GenerateIVChain(*ChainI, Rewriter, DeadInsts);
4573 // Clean up after ourselves. This must be done before deleting any
4577 Changed |= DeleteTriviallyDeadInstructions(DeadInsts);
4580 LSRInstance::LSRInstance(Loop *L, Pass *P)
4581 : IU(P->getAnalysis<IVUsers>()), SE(P->getAnalysis<ScalarEvolution>()),
4582 DT(P->getAnalysis<DominatorTree>()), LI(P->getAnalysis<LoopInfo>()),
4583 TTI(P->getAnalysis<TargetTransformInfo>()), L(L), Changed(false),
4585 // If LoopSimplify form is not available, stay out of trouble.
4586 if (!L->isLoopSimplifyForm())
4589 // If there's no interesting work to be done, bail early.
4590 if (IU.empty()) return;
4592 // If there's too much analysis to be done, bail early. We won't be able to
4593 // model the problem anyway.
4594 unsigned NumUsers = 0;
4595 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) {
4596 if (++NumUsers > MaxIVUsers) {
4597 DEBUG(dbgs() << "LSR skipping loop, too many IV Users in " << *L
4604 // All dominating loops must have preheaders, or SCEVExpander may not be able
4605 // to materialize an AddRecExpr whose Start is an outer AddRecExpr.
4607 // IVUsers analysis should only create users that are dominated by simple loop
4608 // headers. Since this loop should dominate all of its users, its user list
4609 // should be empty if this loop itself is not within a simple loop nest.
4610 for (DomTreeNode *Rung = DT.getNode(L->getLoopPreheader());
4611 Rung; Rung = Rung->getIDom()) {
4612 BasicBlock *BB = Rung->getBlock();
4613 const Loop *DomLoop = LI.getLoopFor(BB);
4614 if (DomLoop && DomLoop->getHeader() == BB) {
4615 assert(DomLoop->getLoopPreheader() && "LSR needs a simplified loop nest");
4620 DEBUG(dbgs() << "\nLSR on loop ";
4621 WriteAsOperand(dbgs(), L->getHeader(), /*PrintType=*/false);
4624 // First, perform some low-level loop optimizations.
4626 OptimizeLoopTermCond();
4628 // If loop preparation eliminates all interesting IV users, bail.
4629 if (IU.empty()) return;
4631 // Skip nested loops until we can model them better with formulae.
4633 DEBUG(dbgs() << "LSR skipping outer loop " << *L << "\n");
4637 // Start collecting data and preparing for the solver.
4639 CollectInterestingTypesAndFactors();
4640 CollectFixupsAndInitialFormulae();
4641 CollectLoopInvariantFixupsAndFormulae();
4643 assert(!Uses.empty() && "IVUsers reported at least one use");
4644 DEBUG(dbgs() << "LSR found " << Uses.size() << " uses:\n";
4645 print_uses(dbgs()));
4647 // Now use the reuse data to generate a bunch of interesting ways
4648 // to formulate the values needed for the uses.
4649 GenerateAllReuseFormulae();
4651 FilterOutUndesirableDedicatedRegisters();
4652 NarrowSearchSpaceUsingHeuristics();
4654 SmallVector<const Formula *, 8> Solution;
4657 // Release memory that is no longer needed.
4662 if (Solution.empty())
4666 // Formulae should be legal.
4667 for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(), E = Uses.end();
4669 const LSRUse &LU = *I;
4670 for (SmallVectorImpl<Formula>::const_iterator J = LU.Formulae.begin(),
4671 JE = LU.Formulae.end();
4673 assert(isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
4674 *J) && "Illegal formula generated!");
4678 // Now that we've decided what we want, make it so.
4679 ImplementSolution(Solution, P);
4682 void LSRInstance::print_factors_and_types(raw_ostream &OS) const {
4683 if (Factors.empty() && Types.empty()) return;
4685 OS << "LSR has identified the following interesting factors and types: ";
4688 for (SmallSetVector<int64_t, 8>::const_iterator
4689 I = Factors.begin(), E = Factors.end(); I != E; ++I) {
4690 if (!First) OS << ", ";
4695 for (SmallSetVector<Type *, 4>::const_iterator
4696 I = Types.begin(), E = Types.end(); I != E; ++I) {
4697 if (!First) OS << ", ";
4699 OS << '(' << **I << ')';
4704 void LSRInstance::print_fixups(raw_ostream &OS) const {
4705 OS << "LSR is examining the following fixup sites:\n";
4706 for (SmallVectorImpl<LSRFixup>::const_iterator I = Fixups.begin(),
4707 E = Fixups.end(); I != E; ++I) {
4714 void LSRInstance::print_uses(raw_ostream &OS) const {
4715 OS << "LSR is examining the following uses:\n";
4716 for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(),
4717 E = Uses.end(); I != E; ++I) {
4718 const LSRUse &LU = *I;
4722 for (SmallVectorImpl<Formula>::const_iterator J = LU.Formulae.begin(),
4723 JE = LU.Formulae.end(); J != JE; ++J) {
4731 void LSRInstance::print(raw_ostream &OS) const {
4732 print_factors_and_types(OS);
4737 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4738 void LSRInstance::dump() const {
4739 print(errs()); errs() << '\n';
4745 class LoopStrengthReduce : public LoopPass {
4747 static char ID; // Pass ID, replacement for typeid
4748 LoopStrengthReduce();
4751 bool runOnLoop(Loop *L, LPPassManager &LPM);
4752 void getAnalysisUsage(AnalysisUsage &AU) const;
4757 char LoopStrengthReduce::ID = 0;
4758 INITIALIZE_PASS_BEGIN(LoopStrengthReduce, "loop-reduce",
4759 "Loop Strength Reduction", false, false)
4760 INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
4761 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
4762 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
4763 INITIALIZE_PASS_DEPENDENCY(IVUsers)
4764 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
4765 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
4766 INITIALIZE_PASS_END(LoopStrengthReduce, "loop-reduce",
4767 "Loop Strength Reduction", false, false)
4770 Pass *llvm::createLoopStrengthReducePass() {
4771 return new LoopStrengthReduce();
4774 LoopStrengthReduce::LoopStrengthReduce() : LoopPass(ID) {
4775 initializeLoopStrengthReducePass(*PassRegistry::getPassRegistry());
4778 void LoopStrengthReduce::getAnalysisUsage(AnalysisUsage &AU) const {
4779 // We split critical edges, so we change the CFG. However, we do update
4780 // many analyses if they are around.
4781 AU.addPreservedID(LoopSimplifyID);
4783 AU.addRequired<LoopInfo>();
4784 AU.addPreserved<LoopInfo>();
4785 AU.addRequiredID(LoopSimplifyID);
4786 AU.addRequired<DominatorTree>();
4787 AU.addPreserved<DominatorTree>();
4788 AU.addRequired<ScalarEvolution>();
4789 AU.addPreserved<ScalarEvolution>();
4790 // Requiring LoopSimplify a second time here prevents IVUsers from running
4791 // twice, since LoopSimplify was invalidated by running ScalarEvolution.
4792 AU.addRequiredID(LoopSimplifyID);
4793 AU.addRequired<IVUsers>();
4794 AU.addPreserved<IVUsers>();
4795 AU.addRequired<TargetTransformInfo>();
4798 bool LoopStrengthReduce::runOnLoop(Loop *L, LPPassManager & /*LPM*/) {
4799 bool Changed = false;
4801 // Run the main LSR transformation.
4802 Changed |= LSRInstance(L, this).getChanged();
4804 // Remove any extra phis created by processing inner loops.
4805 Changed |= DeleteDeadPHIs(L->getHeader());
4806 if (EnablePhiElim && L->isLoopSimplifyForm()) {
4807 SmallVector<WeakVH, 16> DeadInsts;
4808 SCEVExpander Rewriter(getAnalysis<ScalarEvolution>(), "lsr");
4810 Rewriter.setDebugType(DEBUG_TYPE);
4812 unsigned numFolded =
4813 Rewriter.replaceCongruentIVs(L, &getAnalysis<DominatorTree>(),
4815 &getAnalysis<TargetTransformInfo>());
4818 DeleteTriviallyDeadInstructions(DeadInsts);
4819 DeleteDeadPHIs(L->getHeader());