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 TargetLowering::AddrMode::BaseGV be changed to a ConstantExpr
41 // instead of a GlobalValue?
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/Constants.h"
59 #include "llvm/Instructions.h"
60 #include "llvm/IntrinsicInst.h"
61 #include "llvm/DerivedTypes.h"
62 #include "llvm/Analysis/IVUsers.h"
63 #include "llvm/Analysis/Dominators.h"
64 #include "llvm/Analysis/LoopPass.h"
65 #include "llvm/Analysis/ScalarEvolutionExpander.h"
66 #include "llvm/Assembly/Writer.h"
67 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
68 #include "llvm/Transforms/Utils/Local.h"
69 #include "llvm/ADT/SmallBitVector.h"
70 #include "llvm/ADT/SetVector.h"
71 #include "llvm/ADT/DenseSet.h"
72 #include "llvm/Support/Debug.h"
73 #include "llvm/Support/CommandLine.h"
74 #include "llvm/Support/ValueHandle.h"
75 #include "llvm/Support/raw_ostream.h"
76 #include "llvm/Target/TargetLowering.h"
81 cl::opt<bool> EnableNested(
82 "enable-lsr-nested", cl::Hidden, cl::desc("Enable LSR on nested loops"));
84 cl::opt<bool> EnableRetry(
85 "enable-lsr-retry", cl::Hidden, cl::desc("Enable LSR retry"));
87 // Temporary flag to cleanup congruent phis after LSR phi expansion.
88 // It's currently disabled until we can determine whether it's truly useful or
89 // not. The flag should be removed after the v3.0 release.
90 cl::opt<bool> EnablePhiElim(
91 "enable-lsr-phielim", cl::Hidden, cl::desc("Enable LSR phi elimination"));
96 /// RegSortData - This class holds data which is used to order reuse candidates.
99 /// UsedByIndices - This represents the set of LSRUse indices which reference
100 /// a particular register.
101 SmallBitVector UsedByIndices;
105 void print(raw_ostream &OS) const;
111 void RegSortData::print(raw_ostream &OS) const {
112 OS << "[NumUses=" << UsedByIndices.count() << ']';
115 void RegSortData::dump() const {
116 print(errs()); errs() << '\n';
121 /// RegUseTracker - Map register candidates to information about how they are
123 class RegUseTracker {
124 typedef DenseMap<const SCEV *, RegSortData> RegUsesTy;
126 RegUsesTy RegUsesMap;
127 SmallVector<const SCEV *, 16> RegSequence;
130 void CountRegister(const SCEV *Reg, size_t LUIdx);
131 void DropRegister(const SCEV *Reg, size_t LUIdx);
132 void SwapAndDropUse(size_t LUIdx, size_t LastLUIdx);
134 bool isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const;
136 const SmallBitVector &getUsedByIndices(const SCEV *Reg) const;
140 typedef SmallVectorImpl<const SCEV *>::iterator iterator;
141 typedef SmallVectorImpl<const SCEV *>::const_iterator const_iterator;
142 iterator begin() { return RegSequence.begin(); }
143 iterator end() { return RegSequence.end(); }
144 const_iterator begin() const { return RegSequence.begin(); }
145 const_iterator end() const { return RegSequence.end(); }
151 RegUseTracker::CountRegister(const SCEV *Reg, size_t LUIdx) {
152 std::pair<RegUsesTy::iterator, bool> Pair =
153 RegUsesMap.insert(std::make_pair(Reg, RegSortData()));
154 RegSortData &RSD = Pair.first->second;
156 RegSequence.push_back(Reg);
157 RSD.UsedByIndices.resize(std::max(RSD.UsedByIndices.size(), LUIdx + 1));
158 RSD.UsedByIndices.set(LUIdx);
162 RegUseTracker::DropRegister(const SCEV *Reg, size_t LUIdx) {
163 RegUsesTy::iterator It = RegUsesMap.find(Reg);
164 assert(It != RegUsesMap.end());
165 RegSortData &RSD = It->second;
166 assert(RSD.UsedByIndices.size() > LUIdx);
167 RSD.UsedByIndices.reset(LUIdx);
171 RegUseTracker::SwapAndDropUse(size_t LUIdx, size_t LastLUIdx) {
172 assert(LUIdx <= LastLUIdx);
174 // Update RegUses. The data structure is not optimized for this purpose;
175 // we must iterate through it and update each of the bit vectors.
176 for (RegUsesTy::iterator I = RegUsesMap.begin(), E = RegUsesMap.end();
178 SmallBitVector &UsedByIndices = I->second.UsedByIndices;
179 if (LUIdx < UsedByIndices.size())
180 UsedByIndices[LUIdx] =
181 LastLUIdx < UsedByIndices.size() ? UsedByIndices[LastLUIdx] : 0;
182 UsedByIndices.resize(std::min(UsedByIndices.size(), LastLUIdx));
187 RegUseTracker::isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const {
188 RegUsesTy::const_iterator I = RegUsesMap.find(Reg);
189 if (I == RegUsesMap.end())
191 const SmallBitVector &UsedByIndices = I->second.UsedByIndices;
192 int i = UsedByIndices.find_first();
193 if (i == -1) return false;
194 if ((size_t)i != LUIdx) return true;
195 return UsedByIndices.find_next(i) != -1;
198 const SmallBitVector &RegUseTracker::getUsedByIndices(const SCEV *Reg) const {
199 RegUsesTy::const_iterator I = RegUsesMap.find(Reg);
200 assert(I != RegUsesMap.end() && "Unknown register!");
201 return I->second.UsedByIndices;
204 void RegUseTracker::clear() {
211 /// Formula - This class holds information that describes a formula for
212 /// computing satisfying a use. It may include broken-out immediates and scaled
215 /// AM - This is used to represent complex addressing, as well as other kinds
216 /// of interesting uses.
217 TargetLowering::AddrMode AM;
219 /// BaseRegs - The list of "base" registers for this use. When this is
220 /// non-empty, AM.HasBaseReg should be set to true.
221 SmallVector<const SCEV *, 2> BaseRegs;
223 /// ScaledReg - The 'scaled' register for this use. This should be non-null
224 /// when AM.Scale is not zero.
225 const SCEV *ScaledReg;
227 /// UnfoldedOffset - An additional constant offset which added near the
228 /// use. This requires a temporary register, but the offset itself can
229 /// live in an add immediate field rather than a register.
230 int64_t UnfoldedOffset;
232 Formula() : ScaledReg(0), UnfoldedOffset(0) {}
234 void InitialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE);
236 unsigned getNumRegs() const;
237 Type *getType() const;
239 void DeleteBaseReg(const SCEV *&S);
241 bool referencesReg(const SCEV *S) const;
242 bool hasRegsUsedByUsesOtherThan(size_t LUIdx,
243 const RegUseTracker &RegUses) const;
245 void print(raw_ostream &OS) const;
251 /// DoInitialMatch - Recursion helper for InitialMatch.
252 static void DoInitialMatch(const SCEV *S, Loop *L,
253 SmallVectorImpl<const SCEV *> &Good,
254 SmallVectorImpl<const SCEV *> &Bad,
255 ScalarEvolution &SE) {
256 // Collect expressions which properly dominate the loop header.
257 if (SE.properlyDominates(S, L->getHeader())) {
262 // Look at add operands.
263 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
264 for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
266 DoInitialMatch(*I, L, Good, Bad, SE);
270 // Look at addrec operands.
271 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S))
272 if (!AR->getStart()->isZero()) {
273 DoInitialMatch(AR->getStart(), L, Good, Bad, SE);
274 DoInitialMatch(SE.getAddRecExpr(SE.getConstant(AR->getType(), 0),
275 AR->getStepRecurrence(SE),
276 // FIXME: AR->getNoWrapFlags()
277 AR->getLoop(), SCEV::FlagAnyWrap),
282 // Handle a multiplication by -1 (negation) if it didn't fold.
283 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S))
284 if (Mul->getOperand(0)->isAllOnesValue()) {
285 SmallVector<const SCEV *, 4> Ops(Mul->op_begin()+1, Mul->op_end());
286 const SCEV *NewMul = SE.getMulExpr(Ops);
288 SmallVector<const SCEV *, 4> MyGood;
289 SmallVector<const SCEV *, 4> MyBad;
290 DoInitialMatch(NewMul, L, MyGood, MyBad, SE);
291 const SCEV *NegOne = SE.getSCEV(ConstantInt::getAllOnesValue(
292 SE.getEffectiveSCEVType(NewMul->getType())));
293 for (SmallVectorImpl<const SCEV *>::const_iterator I = MyGood.begin(),
294 E = MyGood.end(); I != E; ++I)
295 Good.push_back(SE.getMulExpr(NegOne, *I));
296 for (SmallVectorImpl<const SCEV *>::const_iterator I = MyBad.begin(),
297 E = MyBad.end(); I != E; ++I)
298 Bad.push_back(SE.getMulExpr(NegOne, *I));
302 // Ok, we can't do anything interesting. Just stuff the whole thing into a
303 // register and hope for the best.
307 /// InitialMatch - Incorporate loop-variant parts of S into this Formula,
308 /// attempting to keep all loop-invariant and loop-computable values in a
309 /// single base register.
310 void Formula::InitialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE) {
311 SmallVector<const SCEV *, 4> Good;
312 SmallVector<const SCEV *, 4> Bad;
313 DoInitialMatch(S, L, Good, Bad, SE);
315 const SCEV *Sum = SE.getAddExpr(Good);
317 BaseRegs.push_back(Sum);
318 AM.HasBaseReg = true;
321 const SCEV *Sum = SE.getAddExpr(Bad);
323 BaseRegs.push_back(Sum);
324 AM.HasBaseReg = true;
328 /// getNumRegs - Return the total number of register operands used by this
329 /// formula. This does not include register uses implied by non-constant
331 unsigned Formula::getNumRegs() const {
332 return !!ScaledReg + BaseRegs.size();
335 /// getType - Return the type of this formula, if it has one, or null
336 /// otherwise. This type is meaningless except for the bit size.
337 Type *Formula::getType() const {
338 return !BaseRegs.empty() ? BaseRegs.front()->getType() :
339 ScaledReg ? ScaledReg->getType() :
340 AM.BaseGV ? AM.BaseGV->getType() :
344 /// DeleteBaseReg - Delete the given base reg from the BaseRegs list.
345 void Formula::DeleteBaseReg(const SCEV *&S) {
346 if (&S != &BaseRegs.back())
347 std::swap(S, BaseRegs.back());
351 /// referencesReg - Test if this formula references the given register.
352 bool Formula::referencesReg(const SCEV *S) const {
353 return S == ScaledReg ||
354 std::find(BaseRegs.begin(), BaseRegs.end(), S) != BaseRegs.end();
357 /// hasRegsUsedByUsesOtherThan - Test whether this formula uses registers
358 /// which are used by uses other than the use with the given index.
359 bool Formula::hasRegsUsedByUsesOtherThan(size_t LUIdx,
360 const RegUseTracker &RegUses) const {
362 if (RegUses.isRegUsedByUsesOtherThan(ScaledReg, LUIdx))
364 for (SmallVectorImpl<const SCEV *>::const_iterator I = BaseRegs.begin(),
365 E = BaseRegs.end(); I != E; ++I)
366 if (RegUses.isRegUsedByUsesOtherThan(*I, LUIdx))
371 void Formula::print(raw_ostream &OS) const {
374 if (!First) OS << " + "; else First = false;
375 WriteAsOperand(OS, AM.BaseGV, /*PrintType=*/false);
377 if (AM.BaseOffs != 0) {
378 if (!First) OS << " + "; else First = false;
381 for (SmallVectorImpl<const SCEV *>::const_iterator I = BaseRegs.begin(),
382 E = BaseRegs.end(); I != E; ++I) {
383 if (!First) OS << " + "; else First = false;
384 OS << "reg(" << **I << ')';
386 if (AM.HasBaseReg && BaseRegs.empty()) {
387 if (!First) OS << " + "; else First = false;
388 OS << "**error: HasBaseReg**";
389 } else if (!AM.HasBaseReg && !BaseRegs.empty()) {
390 if (!First) OS << " + "; else First = false;
391 OS << "**error: !HasBaseReg**";
394 if (!First) OS << " + "; else First = false;
395 OS << AM.Scale << "*reg(";
402 if (UnfoldedOffset != 0) {
403 if (!First) OS << " + "; else First = false;
404 OS << "imm(" << UnfoldedOffset << ')';
408 void Formula::dump() const {
409 print(errs()); errs() << '\n';
412 /// isAddRecSExtable - Return true if the given addrec can be sign-extended
413 /// without changing its value.
414 static bool isAddRecSExtable(const SCEVAddRecExpr *AR, ScalarEvolution &SE) {
416 IntegerType::get(SE.getContext(), SE.getTypeSizeInBits(AR->getType()) + 1);
417 return isa<SCEVAddRecExpr>(SE.getSignExtendExpr(AR, WideTy));
420 /// isAddSExtable - Return true if the given add can be sign-extended
421 /// without changing its value.
422 static bool isAddSExtable(const SCEVAddExpr *A, ScalarEvolution &SE) {
424 IntegerType::get(SE.getContext(), SE.getTypeSizeInBits(A->getType()) + 1);
425 return isa<SCEVAddExpr>(SE.getSignExtendExpr(A, WideTy));
428 /// isMulSExtable - Return true if the given mul can be sign-extended
429 /// without changing its value.
430 static bool isMulSExtable(const SCEVMulExpr *M, ScalarEvolution &SE) {
432 IntegerType::get(SE.getContext(),
433 SE.getTypeSizeInBits(M->getType()) * M->getNumOperands());
434 return isa<SCEVMulExpr>(SE.getSignExtendExpr(M, WideTy));
437 /// getExactSDiv - Return an expression for LHS /s RHS, if it can be determined
438 /// and if the remainder is known to be zero, or null otherwise. If
439 /// IgnoreSignificantBits is true, expressions like (X * Y) /s Y are simplified
440 /// to Y, ignoring that the multiplication may overflow, which is useful when
441 /// the result will be used in a context where the most significant bits are
443 static const SCEV *getExactSDiv(const SCEV *LHS, const SCEV *RHS,
445 bool IgnoreSignificantBits = false) {
446 // Handle the trivial case, which works for any SCEV type.
448 return SE.getConstant(LHS->getType(), 1);
450 // Handle a few RHS special cases.
451 const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS);
453 const APInt &RA = RC->getValue()->getValue();
454 // Handle x /s -1 as x * -1, to give ScalarEvolution a chance to do
456 if (RA.isAllOnesValue())
457 return SE.getMulExpr(LHS, RC);
458 // Handle x /s 1 as x.
463 // Check for a division of a constant by a constant.
464 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(LHS)) {
467 const APInt &LA = C->getValue()->getValue();
468 const APInt &RA = RC->getValue()->getValue();
469 if (LA.srem(RA) != 0)
471 return SE.getConstant(LA.sdiv(RA));
474 // Distribute the sdiv over addrec operands, if the addrec doesn't overflow.
475 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) {
476 if (IgnoreSignificantBits || isAddRecSExtable(AR, SE)) {
477 const SCEV *Step = getExactSDiv(AR->getStepRecurrence(SE), RHS, SE,
478 IgnoreSignificantBits);
480 const SCEV *Start = getExactSDiv(AR->getStart(), RHS, SE,
481 IgnoreSignificantBits);
482 if (!Start) return 0;
483 // FlagNW is independent of the start value, step direction, and is
484 // preserved with smaller magnitude steps.
485 // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
486 return SE.getAddRecExpr(Start, Step, AR->getLoop(), SCEV::FlagAnyWrap);
491 // Distribute the sdiv over add operands, if the add doesn't overflow.
492 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(LHS)) {
493 if (IgnoreSignificantBits || isAddSExtable(Add, SE)) {
494 SmallVector<const SCEV *, 8> Ops;
495 for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
497 const SCEV *Op = getExactSDiv(*I, RHS, SE,
498 IgnoreSignificantBits);
502 return SE.getAddExpr(Ops);
507 // Check for a multiply operand that we can pull RHS out of.
508 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS)) {
509 if (IgnoreSignificantBits || isMulSExtable(Mul, SE)) {
510 SmallVector<const SCEV *, 4> Ops;
512 for (SCEVMulExpr::op_iterator I = Mul->op_begin(), E = Mul->op_end();
516 if (const SCEV *Q = getExactSDiv(S, RHS, SE,
517 IgnoreSignificantBits)) {
523 return Found ? SE.getMulExpr(Ops) : 0;
528 // Otherwise we don't know.
532 /// ExtractImmediate - If S involves the addition of a constant integer value,
533 /// return that integer value, and mutate S to point to a new SCEV with that
535 static int64_t ExtractImmediate(const SCEV *&S, ScalarEvolution &SE) {
536 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
537 if (C->getValue()->getValue().getMinSignedBits() <= 64) {
538 S = SE.getConstant(C->getType(), 0);
539 return C->getValue()->getSExtValue();
541 } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
542 SmallVector<const SCEV *, 8> NewOps(Add->op_begin(), Add->op_end());
543 int64_t Result = ExtractImmediate(NewOps.front(), SE);
545 S = SE.getAddExpr(NewOps);
547 } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
548 SmallVector<const SCEV *, 8> NewOps(AR->op_begin(), AR->op_end());
549 int64_t Result = ExtractImmediate(NewOps.front(), SE);
551 S = SE.getAddRecExpr(NewOps, AR->getLoop(),
552 // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
559 /// ExtractSymbol - If S involves the addition of a GlobalValue address,
560 /// return that symbol, and mutate S to point to a new SCEV with that
562 static GlobalValue *ExtractSymbol(const SCEV *&S, ScalarEvolution &SE) {
563 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
564 if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue())) {
565 S = SE.getConstant(GV->getType(), 0);
568 } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
569 SmallVector<const SCEV *, 8> NewOps(Add->op_begin(), Add->op_end());
570 GlobalValue *Result = ExtractSymbol(NewOps.back(), SE);
572 S = SE.getAddExpr(NewOps);
574 } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
575 SmallVector<const SCEV *, 8> NewOps(AR->op_begin(), AR->op_end());
576 GlobalValue *Result = ExtractSymbol(NewOps.front(), SE);
578 S = SE.getAddRecExpr(NewOps, AR->getLoop(),
579 // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
586 /// isAddressUse - Returns true if the specified instruction is using the
587 /// specified value as an address.
588 static bool isAddressUse(Instruction *Inst, Value *OperandVal) {
589 bool isAddress = isa<LoadInst>(Inst);
590 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
591 if (SI->getOperand(1) == OperandVal)
593 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
594 // Addressing modes can also be folded into prefetches and a variety
596 switch (II->getIntrinsicID()) {
598 case Intrinsic::prefetch:
599 case Intrinsic::x86_sse_storeu_ps:
600 case Intrinsic::x86_sse2_storeu_pd:
601 case Intrinsic::x86_sse2_storeu_dq:
602 case Intrinsic::x86_sse2_storel_dq:
603 if (II->getArgOperand(0) == OperandVal)
611 /// getAccessType - Return the type of the memory being accessed.
612 static Type *getAccessType(const Instruction *Inst) {
613 Type *AccessTy = Inst->getType();
614 if (const StoreInst *SI = dyn_cast<StoreInst>(Inst))
615 AccessTy = SI->getOperand(0)->getType();
616 else if (const 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::x86_sse_storeu_ps:
622 case Intrinsic::x86_sse2_storeu_pd:
623 case Intrinsic::x86_sse2_storeu_dq:
624 case Intrinsic::x86_sse2_storel_dq:
625 AccessTy = II->getArgOperand(0)->getType();
630 // All pointers have the same requirements, so canonicalize them to an
631 // arbitrary pointer type to minimize variation.
632 if (PointerType *PTy = dyn_cast<PointerType>(AccessTy))
633 AccessTy = PointerType::get(IntegerType::get(PTy->getContext(), 1),
634 PTy->getAddressSpace());
639 /// DeleteTriviallyDeadInstructions - If any of the instructions is the
640 /// specified set are trivially dead, delete them and see if this makes any of
641 /// their operands subsequently dead.
643 DeleteTriviallyDeadInstructions(SmallVectorImpl<WeakVH> &DeadInsts) {
644 bool Changed = false;
646 while (!DeadInsts.empty()) {
647 Instruction *I = dyn_cast_or_null<Instruction>(&*DeadInsts.pop_back_val());
649 if (I == 0 || !isInstructionTriviallyDead(I))
652 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
653 if (Instruction *U = dyn_cast<Instruction>(*OI)) {
656 DeadInsts.push_back(U);
659 I->eraseFromParent();
668 /// Cost - This class is used to measure and compare candidate formulae.
670 /// TODO: Some of these could be merged. Also, a lexical ordering
671 /// isn't always optimal.
675 unsigned NumBaseAdds;
681 : NumRegs(0), AddRecCost(0), NumIVMuls(0), NumBaseAdds(0), ImmCost(0),
684 bool operator<(const Cost &Other) const;
689 // Once any of the metrics loses, they must all remain losers.
691 return ((NumRegs | AddRecCost | NumIVMuls | NumBaseAdds
692 | ImmCost | SetupCost) != ~0u)
693 || ((NumRegs & AddRecCost & NumIVMuls & NumBaseAdds
694 & ImmCost & SetupCost) == ~0u);
699 assert(isValid() && "invalid cost");
700 return NumRegs == ~0u;
703 void RateFormula(const Formula &F,
704 SmallPtrSet<const SCEV *, 16> &Regs,
705 const DenseSet<const SCEV *> &VisitedRegs,
707 const SmallVectorImpl<int64_t> &Offsets,
708 ScalarEvolution &SE, DominatorTree &DT);
710 void print(raw_ostream &OS) const;
714 void RateRegister(const SCEV *Reg,
715 SmallPtrSet<const SCEV *, 16> &Regs,
717 ScalarEvolution &SE, DominatorTree &DT);
718 void RatePrimaryRegister(const SCEV *Reg,
719 SmallPtrSet<const SCEV *, 16> &Regs,
721 ScalarEvolution &SE, DominatorTree &DT);
726 /// RateRegister - Tally up interesting quantities from the given register.
727 void Cost::RateRegister(const SCEV *Reg,
728 SmallPtrSet<const SCEV *, 16> &Regs,
730 ScalarEvolution &SE, DominatorTree &DT) {
731 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Reg)) {
732 if (AR->getLoop() == L)
733 AddRecCost += 1; /// TODO: This should be a function of the stride.
735 // If this is an addrec for another loop, don't second-guess its addrec phi
736 // nodes. LSR isn't currently smart enough to reason about more than one
737 // loop at a time. LSR has either already run on inner loops, will not run
738 // on other loops, and cannot be expected to change sibling loops. If the
739 // AddRec exists, consider it's register free and leave it alone. Otherwise,
740 // do not consider this formula at all.
741 // FIXME: why do we need to generate such fomulae?
742 else if (!EnableNested || L->contains(AR->getLoop()) ||
743 (!AR->getLoop()->contains(L) &&
744 DT.dominates(L->getHeader(), AR->getLoop()->getHeader()))) {
745 for (BasicBlock::iterator I = AR->getLoop()->getHeader()->begin();
746 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
747 if (SE.isSCEVable(PN->getType()) &&
748 (SE.getEffectiveSCEVType(PN->getType()) ==
749 SE.getEffectiveSCEVType(AR->getType())) &&
750 SE.getSCEV(PN) == AR)
757 // If this isn't one of the addrecs that the loop already has, it
758 // would require a costly new phi and add. TODO: This isn't
759 // precisely modeled right now.
761 if (!Regs.count(AR->getStart())) {
762 RateRegister(AR->getStart(), Regs, L, SE, DT);
768 // Add the step value register, if it needs one.
769 // TODO: The non-affine case isn't precisely modeled here.
770 if (!AR->isAffine() || !isa<SCEVConstant>(AR->getOperand(1))) {
771 if (!Regs.count(AR->getOperand(1))) {
772 RateRegister(AR->getOperand(1), Regs, L, SE, DT);
780 // Rough heuristic; favor registers which don't require extra setup
781 // instructions in the preheader.
782 if (!isa<SCEVUnknown>(Reg) &&
783 !isa<SCEVConstant>(Reg) &&
784 !(isa<SCEVAddRecExpr>(Reg) &&
785 (isa<SCEVUnknown>(cast<SCEVAddRecExpr>(Reg)->getStart()) ||
786 isa<SCEVConstant>(cast<SCEVAddRecExpr>(Reg)->getStart()))))
789 NumIVMuls += isa<SCEVMulExpr>(Reg) &&
790 SE.hasComputableLoopEvolution(Reg, L);
793 /// RatePrimaryRegister - Record this register in the set. If we haven't seen it
795 void Cost::RatePrimaryRegister(const SCEV *Reg,
796 SmallPtrSet<const SCEV *, 16> &Regs,
798 ScalarEvolution &SE, DominatorTree &DT) {
799 if (Regs.insert(Reg))
800 RateRegister(Reg, Regs, L, SE, DT);
803 void Cost::RateFormula(const Formula &F,
804 SmallPtrSet<const SCEV *, 16> &Regs,
805 const DenseSet<const SCEV *> &VisitedRegs,
807 const SmallVectorImpl<int64_t> &Offsets,
808 ScalarEvolution &SE, DominatorTree &DT) {
809 // Tally up the registers.
810 if (const SCEV *ScaledReg = F.ScaledReg) {
811 if (VisitedRegs.count(ScaledReg)) {
815 RatePrimaryRegister(ScaledReg, Regs, L, SE, DT);
819 for (SmallVectorImpl<const SCEV *>::const_iterator I = F.BaseRegs.begin(),
820 E = F.BaseRegs.end(); I != E; ++I) {
821 const SCEV *BaseReg = *I;
822 if (VisitedRegs.count(BaseReg)) {
826 RatePrimaryRegister(BaseReg, Regs, L, SE, DT);
831 // Determine how many (unfolded) adds we'll need inside the loop.
832 size_t NumBaseParts = F.BaseRegs.size() + (F.UnfoldedOffset != 0);
833 if (NumBaseParts > 1)
834 NumBaseAdds += NumBaseParts - 1;
836 // Tally up the non-zero immediates.
837 for (SmallVectorImpl<int64_t>::const_iterator I = Offsets.begin(),
838 E = Offsets.end(); I != E; ++I) {
839 int64_t Offset = (uint64_t)*I + F.AM.BaseOffs;
841 ImmCost += 64; // Handle symbolic values conservatively.
842 // TODO: This should probably be the pointer size.
843 else if (Offset != 0)
844 ImmCost += APInt(64, Offset, true).getMinSignedBits();
846 assert(isValid() && "invalid cost");
849 /// Loose - Set this cost to a losing value.
859 /// operator< - Choose the lower cost.
860 bool Cost::operator<(const Cost &Other) const {
861 if (NumRegs != Other.NumRegs)
862 return NumRegs < Other.NumRegs;
863 if (AddRecCost != Other.AddRecCost)
864 return AddRecCost < Other.AddRecCost;
865 if (NumIVMuls != Other.NumIVMuls)
866 return NumIVMuls < Other.NumIVMuls;
867 if (NumBaseAdds != Other.NumBaseAdds)
868 return NumBaseAdds < Other.NumBaseAdds;
869 if (ImmCost != Other.ImmCost)
870 return ImmCost < Other.ImmCost;
871 if (SetupCost != Other.SetupCost)
872 return SetupCost < Other.SetupCost;
876 void Cost::print(raw_ostream &OS) const {
877 OS << NumRegs << " reg" << (NumRegs == 1 ? "" : "s");
879 OS << ", with addrec cost " << AddRecCost;
881 OS << ", plus " << NumIVMuls << " IV mul" << (NumIVMuls == 1 ? "" : "s");
882 if (NumBaseAdds != 0)
883 OS << ", plus " << NumBaseAdds << " base add"
884 << (NumBaseAdds == 1 ? "" : "s");
886 OS << ", plus " << ImmCost << " imm cost";
888 OS << ", plus " << SetupCost << " setup cost";
891 void Cost::dump() const {
892 print(errs()); errs() << '\n';
897 /// LSRFixup - An operand value in an instruction which is to be replaced
898 /// with some equivalent, possibly strength-reduced, replacement.
900 /// UserInst - The instruction which will be updated.
901 Instruction *UserInst;
903 /// OperandValToReplace - The operand of the instruction which will
904 /// be replaced. The operand may be used more than once; every instance
905 /// will be replaced.
906 Value *OperandValToReplace;
908 /// PostIncLoops - If this user is to use the post-incremented value of an
909 /// induction variable, this variable is non-null and holds the loop
910 /// associated with the induction variable.
911 PostIncLoopSet PostIncLoops;
913 /// LUIdx - The index of the LSRUse describing the expression which
914 /// this fixup needs, minus an offset (below).
917 /// Offset - A constant offset to be added to the LSRUse expression.
918 /// This allows multiple fixups to share the same LSRUse with different
919 /// offsets, for example in an unrolled loop.
922 bool isUseFullyOutsideLoop(const Loop *L) const;
926 void print(raw_ostream &OS) const;
933 : UserInst(0), OperandValToReplace(0), LUIdx(~size_t(0)), Offset(0) {}
935 /// isUseFullyOutsideLoop - Test whether this fixup always uses its
936 /// value outside of the given loop.
937 bool LSRFixup::isUseFullyOutsideLoop(const Loop *L) const {
938 // PHI nodes use their value in their incoming blocks.
939 if (const PHINode *PN = dyn_cast<PHINode>(UserInst)) {
940 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
941 if (PN->getIncomingValue(i) == OperandValToReplace &&
942 L->contains(PN->getIncomingBlock(i)))
947 return !L->contains(UserInst);
950 void LSRFixup::print(raw_ostream &OS) const {
952 // Store is common and interesting enough to be worth special-casing.
953 if (StoreInst *Store = dyn_cast<StoreInst>(UserInst)) {
955 WriteAsOperand(OS, Store->getOperand(0), /*PrintType=*/false);
956 } else if (UserInst->getType()->isVoidTy())
957 OS << UserInst->getOpcodeName();
959 WriteAsOperand(OS, UserInst, /*PrintType=*/false);
961 OS << ", OperandValToReplace=";
962 WriteAsOperand(OS, OperandValToReplace, /*PrintType=*/false);
964 for (PostIncLoopSet::const_iterator I = PostIncLoops.begin(),
965 E = PostIncLoops.end(); I != E; ++I) {
966 OS << ", PostIncLoop=";
967 WriteAsOperand(OS, (*I)->getHeader(), /*PrintType=*/false);
970 if (LUIdx != ~size_t(0))
971 OS << ", LUIdx=" << LUIdx;
974 OS << ", Offset=" << Offset;
977 void LSRFixup::dump() const {
978 print(errs()); errs() << '\n';
983 /// UniquifierDenseMapInfo - A DenseMapInfo implementation for holding
984 /// DenseMaps and DenseSets of sorted SmallVectors of const SCEV*.
985 struct UniquifierDenseMapInfo {
986 static SmallVector<const SCEV *, 2> getEmptyKey() {
987 SmallVector<const SCEV *, 2> V;
988 V.push_back(reinterpret_cast<const SCEV *>(-1));
992 static SmallVector<const SCEV *, 2> getTombstoneKey() {
993 SmallVector<const SCEV *, 2> V;
994 V.push_back(reinterpret_cast<const SCEV *>(-2));
998 static unsigned getHashValue(const SmallVector<const SCEV *, 2> &V) {
1000 for (SmallVectorImpl<const SCEV *>::const_iterator I = V.begin(),
1001 E = V.end(); I != E; ++I)
1002 Result ^= DenseMapInfo<const SCEV *>::getHashValue(*I);
1006 static bool isEqual(const SmallVector<const SCEV *, 2> &LHS,
1007 const SmallVector<const SCEV *, 2> &RHS) {
1012 /// LSRUse - This class holds the state that LSR keeps for each use in
1013 /// IVUsers, as well as uses invented by LSR itself. It includes information
1014 /// about what kinds of things can be folded into the user, information about
1015 /// the user itself, and information about how the use may be satisfied.
1016 /// TODO: Represent multiple users of the same expression in common?
1018 DenseSet<SmallVector<const SCEV *, 2>, UniquifierDenseMapInfo> Uniquifier;
1021 /// KindType - An enum for a kind of use, indicating what types of
1022 /// scaled and immediate operands it might support.
1024 Basic, ///< A normal use, with no folding.
1025 Special, ///< A special case of basic, allowing -1 scales.
1026 Address, ///< An address use; folding according to TargetLowering
1027 ICmpZero ///< An equality icmp with both operands folded into one.
1028 // TODO: Add a generic icmp too?
1034 SmallVector<int64_t, 8> Offsets;
1038 /// AllFixupsOutsideLoop - This records whether all of the fixups using this
1039 /// LSRUse are outside of the loop, in which case some special-case heuristics
1041 bool AllFixupsOutsideLoop;
1043 /// WidestFixupType - This records the widest use type for any fixup using
1044 /// this LSRUse. FindUseWithSimilarFormula can't consider uses with different
1045 /// max fixup widths to be equivalent, because the narrower one may be relying
1046 /// on the implicit truncation to truncate away bogus bits.
1047 Type *WidestFixupType;
1049 /// Formulae - A list of ways to build a value that can satisfy this user.
1050 /// After the list is populated, one of these is selected heuristically and
1051 /// used to formulate a replacement for OperandValToReplace in UserInst.
1052 SmallVector<Formula, 12> Formulae;
1054 /// Regs - The set of register candidates used by all formulae in this LSRUse.
1055 SmallPtrSet<const SCEV *, 4> Regs;
1057 LSRUse(KindType K, Type *T) : Kind(K), AccessTy(T),
1058 MinOffset(INT64_MAX),
1059 MaxOffset(INT64_MIN),
1060 AllFixupsOutsideLoop(true),
1061 WidestFixupType(0) {}
1063 bool HasFormulaWithSameRegs(const Formula &F) const;
1064 bool InsertFormula(const Formula &F);
1065 void DeleteFormula(Formula &F);
1066 void RecomputeRegs(size_t LUIdx, RegUseTracker &Reguses);
1068 void print(raw_ostream &OS) const;
1074 /// HasFormula - Test whether this use as a formula which has the same
1075 /// registers as the given formula.
1076 bool LSRUse::HasFormulaWithSameRegs(const Formula &F) const {
1077 SmallVector<const SCEV *, 2> Key = F.BaseRegs;
1078 if (F.ScaledReg) Key.push_back(F.ScaledReg);
1079 // Unstable sort by host order ok, because this is only used for uniquifying.
1080 std::sort(Key.begin(), Key.end());
1081 return Uniquifier.count(Key);
1084 /// InsertFormula - If the given formula has not yet been inserted, add it to
1085 /// the list, and return true. Return false otherwise.
1086 bool LSRUse::InsertFormula(const Formula &F) {
1087 SmallVector<const SCEV *, 2> Key = F.BaseRegs;
1088 if (F.ScaledReg) Key.push_back(F.ScaledReg);
1089 // Unstable sort by host order ok, because this is only used for uniquifying.
1090 std::sort(Key.begin(), Key.end());
1092 if (!Uniquifier.insert(Key).second)
1095 // Using a register to hold the value of 0 is not profitable.
1096 assert((!F.ScaledReg || !F.ScaledReg->isZero()) &&
1097 "Zero allocated in a scaled register!");
1099 for (SmallVectorImpl<const SCEV *>::const_iterator I =
1100 F.BaseRegs.begin(), E = F.BaseRegs.end(); I != E; ++I)
1101 assert(!(*I)->isZero() && "Zero allocated in a base register!");
1104 // Add the formula to the list.
1105 Formulae.push_back(F);
1107 // Record registers now being used by this use.
1108 if (F.ScaledReg) Regs.insert(F.ScaledReg);
1109 Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end());
1114 /// DeleteFormula - Remove the given formula from this use's list.
1115 void LSRUse::DeleteFormula(Formula &F) {
1116 if (&F != &Formulae.back())
1117 std::swap(F, Formulae.back());
1118 Formulae.pop_back();
1119 assert(!Formulae.empty() && "LSRUse has no formulae left!");
1122 /// RecomputeRegs - Recompute the Regs field, and update RegUses.
1123 void LSRUse::RecomputeRegs(size_t LUIdx, RegUseTracker &RegUses) {
1124 // Now that we've filtered out some formulae, recompute the Regs set.
1125 SmallPtrSet<const SCEV *, 4> OldRegs = Regs;
1127 for (SmallVectorImpl<Formula>::const_iterator I = Formulae.begin(),
1128 E = Formulae.end(); I != E; ++I) {
1129 const Formula &F = *I;
1130 if (F.ScaledReg) Regs.insert(F.ScaledReg);
1131 Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end());
1134 // Update the RegTracker.
1135 for (SmallPtrSet<const SCEV *, 4>::iterator I = OldRegs.begin(),
1136 E = OldRegs.end(); I != E; ++I)
1137 if (!Regs.count(*I))
1138 RegUses.DropRegister(*I, LUIdx);
1141 void LSRUse::print(raw_ostream &OS) const {
1142 OS << "LSR Use: Kind=";
1144 case Basic: OS << "Basic"; break;
1145 case Special: OS << "Special"; break;
1146 case ICmpZero: OS << "ICmpZero"; break;
1148 OS << "Address of ";
1149 if (AccessTy->isPointerTy())
1150 OS << "pointer"; // the full pointer type could be really verbose
1155 OS << ", Offsets={";
1156 for (SmallVectorImpl<int64_t>::const_iterator I = Offsets.begin(),
1157 E = Offsets.end(); I != E; ++I) {
1159 if (llvm::next(I) != E)
1164 if (AllFixupsOutsideLoop)
1165 OS << ", all-fixups-outside-loop";
1167 if (WidestFixupType)
1168 OS << ", widest fixup type: " << *WidestFixupType;
1171 void LSRUse::dump() const {
1172 print(errs()); errs() << '\n';
1175 /// isLegalUse - Test whether the use described by AM is "legal", meaning it can
1176 /// be completely folded into the user instruction at isel time. This includes
1177 /// address-mode folding and special icmp tricks.
1178 static bool isLegalUse(const TargetLowering::AddrMode &AM,
1179 LSRUse::KindType Kind, Type *AccessTy,
1180 const TargetLowering *TLI) {
1182 case LSRUse::Address:
1183 // If we have low-level target information, ask the target if it can
1184 // completely fold this address.
1185 if (TLI) return TLI->isLegalAddressingMode(AM, AccessTy);
1187 // Otherwise, just guess that reg+reg addressing is legal.
1188 return !AM.BaseGV && AM.BaseOffs == 0 && AM.Scale <= 1;
1190 case LSRUse::ICmpZero:
1191 // There's not even a target hook for querying whether it would be legal to
1192 // fold a GV into an ICmp.
1196 // ICmp only has two operands; don't allow more than two non-trivial parts.
1197 if (AM.Scale != 0 && AM.HasBaseReg && AM.BaseOffs != 0)
1200 // ICmp only supports no scale or a -1 scale, as we can "fold" a -1 scale by
1201 // putting the scaled register in the other operand of the icmp.
1202 if (AM.Scale != 0 && AM.Scale != -1)
1205 // If we have low-level target information, ask the target if it can fold an
1206 // integer immediate on an icmp.
1207 if (AM.BaseOffs != 0) {
1208 if (TLI) return TLI->isLegalICmpImmediate(-(uint64_t)AM.BaseOffs);
1215 // Only handle single-register values.
1216 return !AM.BaseGV && AM.Scale == 0 && AM.BaseOffs == 0;
1218 case LSRUse::Special:
1219 // Only handle -1 scales, or no scale.
1220 return AM.Scale == 0 || AM.Scale == -1;
1226 static bool isLegalUse(TargetLowering::AddrMode AM,
1227 int64_t MinOffset, int64_t MaxOffset,
1228 LSRUse::KindType Kind, Type *AccessTy,
1229 const TargetLowering *TLI) {
1230 // Check for overflow.
1231 if (((int64_t)((uint64_t)AM.BaseOffs + MinOffset) > AM.BaseOffs) !=
1234 AM.BaseOffs = (uint64_t)AM.BaseOffs + MinOffset;
1235 if (isLegalUse(AM, Kind, AccessTy, TLI)) {
1236 AM.BaseOffs = (uint64_t)AM.BaseOffs - MinOffset;
1237 // Check for overflow.
1238 if (((int64_t)((uint64_t)AM.BaseOffs + MaxOffset) > AM.BaseOffs) !=
1241 AM.BaseOffs = (uint64_t)AM.BaseOffs + MaxOffset;
1242 return isLegalUse(AM, Kind, AccessTy, TLI);
1247 static bool isAlwaysFoldable(int64_t BaseOffs,
1248 GlobalValue *BaseGV,
1250 LSRUse::KindType Kind, Type *AccessTy,
1251 const TargetLowering *TLI) {
1252 // Fast-path: zero is always foldable.
1253 if (BaseOffs == 0 && !BaseGV) return true;
1255 // Conservatively, create an address with an immediate and a
1256 // base and a scale.
1257 TargetLowering::AddrMode AM;
1258 AM.BaseOffs = BaseOffs;
1260 AM.HasBaseReg = HasBaseReg;
1261 AM.Scale = Kind == LSRUse::ICmpZero ? -1 : 1;
1263 // Canonicalize a scale of 1 to a base register if the formula doesn't
1264 // already have a base register.
1265 if (!AM.HasBaseReg && AM.Scale == 1) {
1267 AM.HasBaseReg = true;
1270 return isLegalUse(AM, Kind, AccessTy, TLI);
1273 static bool isAlwaysFoldable(const SCEV *S,
1274 int64_t MinOffset, int64_t MaxOffset,
1276 LSRUse::KindType Kind, Type *AccessTy,
1277 const TargetLowering *TLI,
1278 ScalarEvolution &SE) {
1279 // Fast-path: zero is always foldable.
1280 if (S->isZero()) return true;
1282 // Conservatively, create an address with an immediate and a
1283 // base and a scale.
1284 int64_t BaseOffs = ExtractImmediate(S, SE);
1285 GlobalValue *BaseGV = ExtractSymbol(S, SE);
1287 // If there's anything else involved, it's not foldable.
1288 if (!S->isZero()) return false;
1290 // Fast-path: zero is always foldable.
1291 if (BaseOffs == 0 && !BaseGV) return true;
1293 // Conservatively, create an address with an immediate and a
1294 // base and a scale.
1295 TargetLowering::AddrMode AM;
1296 AM.BaseOffs = BaseOffs;
1298 AM.HasBaseReg = HasBaseReg;
1299 AM.Scale = Kind == LSRUse::ICmpZero ? -1 : 1;
1301 return isLegalUse(AM, MinOffset, MaxOffset, Kind, AccessTy, TLI);
1306 /// UseMapDenseMapInfo - A DenseMapInfo implementation for holding
1307 /// DenseMaps and DenseSets of pairs of const SCEV* and LSRUse::Kind.
1308 struct UseMapDenseMapInfo {
1309 static std::pair<const SCEV *, LSRUse::KindType> getEmptyKey() {
1310 return std::make_pair(reinterpret_cast<const SCEV *>(-1), LSRUse::Basic);
1313 static std::pair<const SCEV *, LSRUse::KindType> getTombstoneKey() {
1314 return std::make_pair(reinterpret_cast<const SCEV *>(-2), LSRUse::Basic);
1318 getHashValue(const std::pair<const SCEV *, LSRUse::KindType> &V) {
1319 unsigned Result = DenseMapInfo<const SCEV *>::getHashValue(V.first);
1320 Result ^= DenseMapInfo<unsigned>::getHashValue(unsigned(V.second));
1324 static bool isEqual(const std::pair<const SCEV *, LSRUse::KindType> &LHS,
1325 const std::pair<const SCEV *, LSRUse::KindType> &RHS) {
1330 /// LSRInstance - This class holds state for the main loop strength reduction
1334 ScalarEvolution &SE;
1337 const TargetLowering *const TLI;
1341 /// IVIncInsertPos - This is the insert position that the current loop's
1342 /// induction variable increment should be placed. In simple loops, this is
1343 /// the latch block's terminator. But in more complicated cases, this is a
1344 /// position which will dominate all the in-loop post-increment users.
1345 Instruction *IVIncInsertPos;
1347 /// Factors - Interesting factors between use strides.
1348 SmallSetVector<int64_t, 8> Factors;
1350 /// Types - Interesting use types, to facilitate truncation reuse.
1351 SmallSetVector<Type *, 4> Types;
1353 /// Fixups - The list of operands which are to be replaced.
1354 SmallVector<LSRFixup, 16> Fixups;
1356 /// Uses - The list of interesting uses.
1357 SmallVector<LSRUse, 16> Uses;
1359 /// RegUses - Track which uses use which register candidates.
1360 RegUseTracker RegUses;
1362 void OptimizeShadowIV();
1363 bool FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse);
1364 ICmpInst *OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse);
1365 void OptimizeLoopTermCond();
1367 void CollectInterestingTypesAndFactors();
1368 void CollectFixupsAndInitialFormulae();
1370 LSRFixup &getNewFixup() {
1371 Fixups.push_back(LSRFixup());
1372 return Fixups.back();
1375 // Support for sharing of LSRUses between LSRFixups.
1376 typedef DenseMap<std::pair<const SCEV *, LSRUse::KindType>,
1378 UseMapDenseMapInfo> UseMapTy;
1381 bool reconcileNewOffset(LSRUse &LU, int64_t NewOffset, bool HasBaseReg,
1382 LSRUse::KindType Kind, Type *AccessTy);
1384 std::pair<size_t, int64_t> getUse(const SCEV *&Expr,
1385 LSRUse::KindType Kind,
1388 void DeleteUse(LSRUse &LU, size_t LUIdx);
1390 LSRUse *FindUseWithSimilarFormula(const Formula &F, const LSRUse &OrigLU);
1393 void InsertInitialFormula(const SCEV *S, LSRUse &LU, size_t LUIdx);
1394 void InsertSupplementalFormula(const SCEV *S, LSRUse &LU, size_t LUIdx);
1395 void CountRegisters(const Formula &F, size_t LUIdx);
1396 bool InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F);
1398 void CollectLoopInvariantFixupsAndFormulae();
1400 void GenerateReassociations(LSRUse &LU, unsigned LUIdx, Formula Base,
1401 unsigned Depth = 0);
1402 void GenerateCombinations(LSRUse &LU, unsigned LUIdx, Formula Base);
1403 void GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx, Formula Base);
1404 void GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx, Formula Base);
1405 void GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx, Formula Base);
1406 void GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base);
1407 void GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base);
1408 void GenerateCrossUseConstantOffsets();
1409 void GenerateAllReuseFormulae();
1411 void FilterOutUndesirableDedicatedRegisters();
1413 size_t EstimateSearchSpaceComplexity() const;
1414 void NarrowSearchSpaceByDetectingSupersets();
1415 void NarrowSearchSpaceByCollapsingUnrolledCode();
1416 void NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters();
1417 void NarrowSearchSpaceByPickingWinnerRegs();
1418 void NarrowSearchSpaceUsingHeuristics();
1420 void SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
1422 SmallVectorImpl<const Formula *> &Workspace,
1423 const Cost &CurCost,
1424 const SmallPtrSet<const SCEV *, 16> &CurRegs,
1425 DenseSet<const SCEV *> &VisitedRegs) const;
1426 void Solve(SmallVectorImpl<const Formula *> &Solution) const;
1428 BasicBlock::iterator
1429 HoistInsertPosition(BasicBlock::iterator IP,
1430 const SmallVectorImpl<Instruction *> &Inputs) const;
1431 BasicBlock::iterator AdjustInsertPositionForExpand(BasicBlock::iterator IP,
1433 const LSRUse &LU) const;
1435 Value *Expand(const LSRFixup &LF,
1437 BasicBlock::iterator IP,
1438 SCEVExpander &Rewriter,
1439 SmallVectorImpl<WeakVH> &DeadInsts) const;
1440 void RewriteForPHI(PHINode *PN, const LSRFixup &LF,
1442 SCEVExpander &Rewriter,
1443 SmallVectorImpl<WeakVH> &DeadInsts,
1445 void Rewrite(const LSRFixup &LF,
1447 SCEVExpander &Rewriter,
1448 SmallVectorImpl<WeakVH> &DeadInsts,
1450 void ImplementSolution(const SmallVectorImpl<const Formula *> &Solution,
1453 LSRInstance(const TargetLowering *tli, Loop *l, Pass *P);
1455 bool getChanged() const { return Changed; }
1457 void print_factors_and_types(raw_ostream &OS) const;
1458 void print_fixups(raw_ostream &OS) const;
1459 void print_uses(raw_ostream &OS) const;
1460 void print(raw_ostream &OS) const;
1466 /// OptimizeShadowIV - If IV is used in a int-to-float cast
1467 /// inside the loop then try to eliminate the cast operation.
1468 void LSRInstance::OptimizeShadowIV() {
1469 const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L);
1470 if (isa<SCEVCouldNotCompute>(BackedgeTakenCount))
1473 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end();
1474 UI != E; /* empty */) {
1475 IVUsers::const_iterator CandidateUI = UI;
1477 Instruction *ShadowUse = CandidateUI->getUser();
1478 Type *DestTy = NULL;
1479 bool IsSigned = false;
1481 /* If shadow use is a int->float cast then insert a second IV
1482 to eliminate this cast.
1484 for (unsigned i = 0; i < n; ++i)
1490 for (unsigned i = 0; i < n; ++i, ++d)
1493 if (UIToFPInst *UCast = dyn_cast<UIToFPInst>(CandidateUI->getUser())) {
1495 DestTy = UCast->getDestTy();
1497 else if (SIToFPInst *SCast = dyn_cast<SIToFPInst>(CandidateUI->getUser())) {
1499 DestTy = SCast->getDestTy();
1501 if (!DestTy) continue;
1504 // If target does not support DestTy natively then do not apply
1505 // this transformation.
1506 EVT DVT = TLI->getValueType(DestTy);
1507 if (!TLI->isTypeLegal(DVT)) continue;
1510 PHINode *PH = dyn_cast<PHINode>(ShadowUse->getOperand(0));
1512 if (PH->getNumIncomingValues() != 2) continue;
1514 Type *SrcTy = PH->getType();
1515 int Mantissa = DestTy->getFPMantissaWidth();
1516 if (Mantissa == -1) continue;
1517 if ((int)SE.getTypeSizeInBits(SrcTy) > Mantissa)
1520 unsigned Entry, Latch;
1521 if (PH->getIncomingBlock(0) == L->getLoopPreheader()) {
1529 ConstantInt *Init = dyn_cast<ConstantInt>(PH->getIncomingValue(Entry));
1530 if (!Init) continue;
1531 Constant *NewInit = ConstantFP::get(DestTy, IsSigned ?
1532 (double)Init->getSExtValue() :
1533 (double)Init->getZExtValue());
1535 BinaryOperator *Incr =
1536 dyn_cast<BinaryOperator>(PH->getIncomingValue(Latch));
1537 if (!Incr) continue;
1538 if (Incr->getOpcode() != Instruction::Add
1539 && Incr->getOpcode() != Instruction::Sub)
1542 /* Initialize new IV, double d = 0.0 in above example. */
1543 ConstantInt *C = NULL;
1544 if (Incr->getOperand(0) == PH)
1545 C = dyn_cast<ConstantInt>(Incr->getOperand(1));
1546 else if (Incr->getOperand(1) == PH)
1547 C = dyn_cast<ConstantInt>(Incr->getOperand(0));
1553 // Ignore negative constants, as the code below doesn't handle them
1554 // correctly. TODO: Remove this restriction.
1555 if (!C->getValue().isStrictlyPositive()) continue;
1557 /* Add new PHINode. */
1558 PHINode *NewPH = PHINode::Create(DestTy, 2, "IV.S.", PH);
1560 /* create new increment. '++d' in above example. */
1561 Constant *CFP = ConstantFP::get(DestTy, C->getZExtValue());
1562 BinaryOperator *NewIncr =
1563 BinaryOperator::Create(Incr->getOpcode() == Instruction::Add ?
1564 Instruction::FAdd : Instruction::FSub,
1565 NewPH, CFP, "IV.S.next.", Incr);
1567 NewPH->addIncoming(NewInit, PH->getIncomingBlock(Entry));
1568 NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch));
1570 /* Remove cast operation */
1571 ShadowUse->replaceAllUsesWith(NewPH);
1572 ShadowUse->eraseFromParent();
1578 /// FindIVUserForCond - If Cond has an operand that is an expression of an IV,
1579 /// set the IV user and stride information and return true, otherwise return
1581 bool LSRInstance::FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse) {
1582 for (IVUsers::iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI)
1583 if (UI->getUser() == Cond) {
1584 // NOTE: we could handle setcc instructions with multiple uses here, but
1585 // InstCombine does it as well for simple uses, it's not clear that it
1586 // occurs enough in real life to handle.
1593 /// OptimizeMax - Rewrite the loop's terminating condition if it uses
1594 /// a max computation.
1596 /// This is a narrow solution to a specific, but acute, problem. For loops
1602 /// } while (++i < n);
1604 /// the trip count isn't just 'n', because 'n' might not be positive. And
1605 /// unfortunately this can come up even for loops where the user didn't use
1606 /// a C do-while loop. For example, seemingly well-behaved top-test loops
1607 /// will commonly be lowered like this:
1613 /// } while (++i < n);
1616 /// and then it's possible for subsequent optimization to obscure the if
1617 /// test in such a way that indvars can't find it.
1619 /// When indvars can't find the if test in loops like this, it creates a
1620 /// max expression, which allows it to give the loop a canonical
1621 /// induction variable:
1624 /// max = n < 1 ? 1 : n;
1627 /// } while (++i != max);
1629 /// Canonical induction variables are necessary because the loop passes
1630 /// are designed around them. The most obvious example of this is the
1631 /// LoopInfo analysis, which doesn't remember trip count values. It
1632 /// expects to be able to rediscover the trip count each time it is
1633 /// needed, and it does this using a simple analysis that only succeeds if
1634 /// the loop has a canonical induction variable.
1636 /// However, when it comes time to generate code, the maximum operation
1637 /// can be quite costly, especially if it's inside of an outer loop.
1639 /// This function solves this problem by detecting this type of loop and
1640 /// rewriting their conditions from ICMP_NE back to ICMP_SLT, and deleting
1641 /// the instructions for the maximum computation.
1643 ICmpInst *LSRInstance::OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse) {
1644 // Check that the loop matches the pattern we're looking for.
1645 if (Cond->getPredicate() != CmpInst::ICMP_EQ &&
1646 Cond->getPredicate() != CmpInst::ICMP_NE)
1649 SelectInst *Sel = dyn_cast<SelectInst>(Cond->getOperand(1));
1650 if (!Sel || !Sel->hasOneUse()) return Cond;
1652 const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L);
1653 if (isa<SCEVCouldNotCompute>(BackedgeTakenCount))
1655 const SCEV *One = SE.getConstant(BackedgeTakenCount->getType(), 1);
1657 // Add one to the backedge-taken count to get the trip count.
1658 const SCEV *IterationCount = SE.getAddExpr(One, BackedgeTakenCount);
1659 if (IterationCount != SE.getSCEV(Sel)) return Cond;
1661 // Check for a max calculation that matches the pattern. There's no check
1662 // for ICMP_ULE here because the comparison would be with zero, which
1663 // isn't interesting.
1664 CmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
1665 const SCEVNAryExpr *Max = 0;
1666 if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(BackedgeTakenCount)) {
1667 Pred = ICmpInst::ICMP_SLE;
1669 } else if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(IterationCount)) {
1670 Pred = ICmpInst::ICMP_SLT;
1672 } else if (const SCEVUMaxExpr *U = dyn_cast<SCEVUMaxExpr>(IterationCount)) {
1673 Pred = ICmpInst::ICMP_ULT;
1680 // To handle a max with more than two operands, this optimization would
1681 // require additional checking and setup.
1682 if (Max->getNumOperands() != 2)
1685 const SCEV *MaxLHS = Max->getOperand(0);
1686 const SCEV *MaxRHS = Max->getOperand(1);
1688 // ScalarEvolution canonicalizes constants to the left. For < and >, look
1689 // for a comparison with 1. For <= and >=, a comparison with zero.
1691 (ICmpInst::isTrueWhenEqual(Pred) ? !MaxLHS->isZero() : (MaxLHS != One)))
1694 // Check the relevant induction variable for conformance to
1696 const SCEV *IV = SE.getSCEV(Cond->getOperand(0));
1697 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(IV);
1698 if (!AR || !AR->isAffine() ||
1699 AR->getStart() != One ||
1700 AR->getStepRecurrence(SE) != One)
1703 assert(AR->getLoop() == L &&
1704 "Loop condition operand is an addrec in a different loop!");
1706 // Check the right operand of the select, and remember it, as it will
1707 // be used in the new comparison instruction.
1709 if (ICmpInst::isTrueWhenEqual(Pred)) {
1710 // Look for n+1, and grab n.
1711 if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(1)))
1712 if (isa<ConstantInt>(BO->getOperand(1)) &&
1713 cast<ConstantInt>(BO->getOperand(1))->isOne() &&
1714 SE.getSCEV(BO->getOperand(0)) == MaxRHS)
1715 NewRHS = BO->getOperand(0);
1716 if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(2)))
1717 if (isa<ConstantInt>(BO->getOperand(1)) &&
1718 cast<ConstantInt>(BO->getOperand(1))->isOne() &&
1719 SE.getSCEV(BO->getOperand(0)) == MaxRHS)
1720 NewRHS = BO->getOperand(0);
1723 } else if (SE.getSCEV(Sel->getOperand(1)) == MaxRHS)
1724 NewRHS = Sel->getOperand(1);
1725 else if (SE.getSCEV(Sel->getOperand(2)) == MaxRHS)
1726 NewRHS = Sel->getOperand(2);
1727 else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(MaxRHS))
1728 NewRHS = SU->getValue();
1730 // Max doesn't match expected pattern.
1733 // Determine the new comparison opcode. It may be signed or unsigned,
1734 // and the original comparison may be either equality or inequality.
1735 if (Cond->getPredicate() == CmpInst::ICMP_EQ)
1736 Pred = CmpInst::getInversePredicate(Pred);
1738 // Ok, everything looks ok to change the condition into an SLT or SGE and
1739 // delete the max calculation.
1741 new ICmpInst(Cond, Pred, Cond->getOperand(0), NewRHS, "scmp");
1743 // Delete the max calculation instructions.
1744 Cond->replaceAllUsesWith(NewCond);
1745 CondUse->setUser(NewCond);
1746 Instruction *Cmp = cast<Instruction>(Sel->getOperand(0));
1747 Cond->eraseFromParent();
1748 Sel->eraseFromParent();
1749 if (Cmp->use_empty())
1750 Cmp->eraseFromParent();
1754 /// OptimizeLoopTermCond - Change loop terminating condition to use the
1755 /// postinc iv when possible.
1757 LSRInstance::OptimizeLoopTermCond() {
1758 SmallPtrSet<Instruction *, 4> PostIncs;
1760 BasicBlock *LatchBlock = L->getLoopLatch();
1761 SmallVector<BasicBlock*, 8> ExitingBlocks;
1762 L->getExitingBlocks(ExitingBlocks);
1764 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
1765 BasicBlock *ExitingBlock = ExitingBlocks[i];
1767 // Get the terminating condition for the loop if possible. If we
1768 // can, we want to change it to use a post-incremented version of its
1769 // induction variable, to allow coalescing the live ranges for the IV into
1770 // one register value.
1772 BranchInst *TermBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
1775 // FIXME: Overly conservative, termination condition could be an 'or' etc..
1776 if (TermBr->isUnconditional() || !isa<ICmpInst>(TermBr->getCondition()))
1779 // Search IVUsesByStride to find Cond's IVUse if there is one.
1780 IVStrideUse *CondUse = 0;
1781 ICmpInst *Cond = cast<ICmpInst>(TermBr->getCondition());
1782 if (!FindIVUserForCond(Cond, CondUse))
1785 // If the trip count is computed in terms of a max (due to ScalarEvolution
1786 // being unable to find a sufficient guard, for example), change the loop
1787 // comparison to use SLT or ULT instead of NE.
1788 // One consequence of doing this now is that it disrupts the count-down
1789 // optimization. That's not always a bad thing though, because in such
1790 // cases it may still be worthwhile to avoid a max.
1791 Cond = OptimizeMax(Cond, CondUse);
1793 // If this exiting block dominates the latch block, it may also use
1794 // the post-inc value if it won't be shared with other uses.
1795 // Check for dominance.
1796 if (!DT.dominates(ExitingBlock, LatchBlock))
1799 // Conservatively avoid trying to use the post-inc value in non-latch
1800 // exits if there may be pre-inc users in intervening blocks.
1801 if (LatchBlock != ExitingBlock)
1802 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI)
1803 // Test if the use is reachable from the exiting block. This dominator
1804 // query is a conservative approximation of reachability.
1805 if (&*UI != CondUse &&
1806 !DT.properlyDominates(UI->getUser()->getParent(), ExitingBlock)) {
1807 // Conservatively assume there may be reuse if the quotient of their
1808 // strides could be a legal scale.
1809 const SCEV *A = IU.getStride(*CondUse, L);
1810 const SCEV *B = IU.getStride(*UI, L);
1811 if (!A || !B) continue;
1812 if (SE.getTypeSizeInBits(A->getType()) !=
1813 SE.getTypeSizeInBits(B->getType())) {
1814 if (SE.getTypeSizeInBits(A->getType()) >
1815 SE.getTypeSizeInBits(B->getType()))
1816 B = SE.getSignExtendExpr(B, A->getType());
1818 A = SE.getSignExtendExpr(A, B->getType());
1820 if (const SCEVConstant *D =
1821 dyn_cast_or_null<SCEVConstant>(getExactSDiv(B, A, SE))) {
1822 const ConstantInt *C = D->getValue();
1823 // Stride of one or negative one can have reuse with non-addresses.
1824 if (C->isOne() || C->isAllOnesValue())
1825 goto decline_post_inc;
1826 // Avoid weird situations.
1827 if (C->getValue().getMinSignedBits() >= 64 ||
1828 C->getValue().isMinSignedValue())
1829 goto decline_post_inc;
1830 // Without TLI, assume that any stride might be valid, and so any
1831 // use might be shared.
1833 goto decline_post_inc;
1834 // Check for possible scaled-address reuse.
1835 Type *AccessTy = getAccessType(UI->getUser());
1836 TargetLowering::AddrMode AM;
1837 AM.Scale = C->getSExtValue();
1838 if (TLI->isLegalAddressingMode(AM, AccessTy))
1839 goto decline_post_inc;
1840 AM.Scale = -AM.Scale;
1841 if (TLI->isLegalAddressingMode(AM, AccessTy))
1842 goto decline_post_inc;
1846 DEBUG(dbgs() << " Change loop exiting icmp to use postinc iv: "
1849 // It's possible for the setcc instruction to be anywhere in the loop, and
1850 // possible for it to have multiple users. If it is not immediately before
1851 // the exiting block branch, move it.
1852 if (&*++BasicBlock::iterator(Cond) != TermBr) {
1853 if (Cond->hasOneUse()) {
1854 Cond->moveBefore(TermBr);
1856 // Clone the terminating condition and insert into the loopend.
1857 ICmpInst *OldCond = Cond;
1858 Cond = cast<ICmpInst>(Cond->clone());
1859 Cond->setName(L->getHeader()->getName() + ".termcond");
1860 ExitingBlock->getInstList().insert(TermBr, Cond);
1862 // Clone the IVUse, as the old use still exists!
1863 CondUse = &IU.AddUser(Cond, CondUse->getOperandValToReplace());
1864 TermBr->replaceUsesOfWith(OldCond, Cond);
1868 // If we get to here, we know that we can transform the setcc instruction to
1869 // use the post-incremented version of the IV, allowing us to coalesce the
1870 // live ranges for the IV correctly.
1871 CondUse->transformToPostInc(L);
1874 PostIncs.insert(Cond);
1878 // Determine an insertion point for the loop induction variable increment. It
1879 // must dominate all the post-inc comparisons we just set up, and it must
1880 // dominate the loop latch edge.
1881 IVIncInsertPos = L->getLoopLatch()->getTerminator();
1882 for (SmallPtrSet<Instruction *, 4>::const_iterator I = PostIncs.begin(),
1883 E = PostIncs.end(); I != E; ++I) {
1885 DT.findNearestCommonDominator(IVIncInsertPos->getParent(),
1887 if (BB == (*I)->getParent())
1888 IVIncInsertPos = *I;
1889 else if (BB != IVIncInsertPos->getParent())
1890 IVIncInsertPos = BB->getTerminator();
1894 /// reconcileNewOffset - Determine if the given use can accommodate a fixup
1895 /// at the given offset and other details. If so, update the use and
1898 LSRInstance::reconcileNewOffset(LSRUse &LU, int64_t NewOffset, bool HasBaseReg,
1899 LSRUse::KindType Kind, Type *AccessTy) {
1900 int64_t NewMinOffset = LU.MinOffset;
1901 int64_t NewMaxOffset = LU.MaxOffset;
1902 Type *NewAccessTy = AccessTy;
1904 // Check for a mismatched kind. It's tempting to collapse mismatched kinds to
1905 // something conservative, however this can pessimize in the case that one of
1906 // the uses will have all its uses outside the loop, for example.
1907 if (LU.Kind != Kind)
1909 // Conservatively assume HasBaseReg is true for now.
1910 if (NewOffset < LU.MinOffset) {
1911 if (!isAlwaysFoldable(LU.MaxOffset - NewOffset, 0, HasBaseReg,
1912 Kind, AccessTy, TLI))
1914 NewMinOffset = NewOffset;
1915 } else if (NewOffset > LU.MaxOffset) {
1916 if (!isAlwaysFoldable(NewOffset - LU.MinOffset, 0, HasBaseReg,
1917 Kind, AccessTy, TLI))
1919 NewMaxOffset = NewOffset;
1921 // Check for a mismatched access type, and fall back conservatively as needed.
1922 // TODO: Be less conservative when the type is similar and can use the same
1923 // addressing modes.
1924 if (Kind == LSRUse::Address && AccessTy != LU.AccessTy)
1925 NewAccessTy = Type::getVoidTy(AccessTy->getContext());
1928 LU.MinOffset = NewMinOffset;
1929 LU.MaxOffset = NewMaxOffset;
1930 LU.AccessTy = NewAccessTy;
1931 if (NewOffset != LU.Offsets.back())
1932 LU.Offsets.push_back(NewOffset);
1936 /// getUse - Return an LSRUse index and an offset value for a fixup which
1937 /// needs the given expression, with the given kind and optional access type.
1938 /// Either reuse an existing use or create a new one, as needed.
1939 std::pair<size_t, int64_t>
1940 LSRInstance::getUse(const SCEV *&Expr,
1941 LSRUse::KindType Kind, Type *AccessTy) {
1942 const SCEV *Copy = Expr;
1943 int64_t Offset = ExtractImmediate(Expr, SE);
1945 // Basic uses can't accept any offset, for example.
1946 if (!isAlwaysFoldable(Offset, 0, /*HasBaseReg=*/true, Kind, AccessTy, TLI)) {
1951 std::pair<UseMapTy::iterator, bool> P =
1952 UseMap.insert(std::make_pair(std::make_pair(Expr, Kind), 0));
1954 // A use already existed with this base.
1955 size_t LUIdx = P.first->second;
1956 LSRUse &LU = Uses[LUIdx];
1957 if (reconcileNewOffset(LU, Offset, /*HasBaseReg=*/true, Kind, AccessTy))
1959 return std::make_pair(LUIdx, Offset);
1962 // Create a new use.
1963 size_t LUIdx = Uses.size();
1964 P.first->second = LUIdx;
1965 Uses.push_back(LSRUse(Kind, AccessTy));
1966 LSRUse &LU = Uses[LUIdx];
1968 // We don't need to track redundant offsets, but we don't need to go out
1969 // of our way here to avoid them.
1970 if (LU.Offsets.empty() || Offset != LU.Offsets.back())
1971 LU.Offsets.push_back(Offset);
1973 LU.MinOffset = Offset;
1974 LU.MaxOffset = Offset;
1975 return std::make_pair(LUIdx, Offset);
1978 /// DeleteUse - Delete the given use from the Uses list.
1979 void LSRInstance::DeleteUse(LSRUse &LU, size_t LUIdx) {
1980 if (&LU != &Uses.back())
1981 std::swap(LU, Uses.back());
1985 RegUses.SwapAndDropUse(LUIdx, Uses.size());
1988 /// FindUseWithFormula - Look for a use distinct from OrigLU which is has
1989 /// a formula that has the same registers as the given formula.
1991 LSRInstance::FindUseWithSimilarFormula(const Formula &OrigF,
1992 const LSRUse &OrigLU) {
1993 // Search all uses for the formula. This could be more clever.
1994 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
1995 LSRUse &LU = Uses[LUIdx];
1996 // Check whether this use is close enough to OrigLU, to see whether it's
1997 // worthwhile looking through its formulae.
1998 // Ignore ICmpZero uses because they may contain formulae generated by
1999 // GenerateICmpZeroScales, in which case adding fixup offsets may
2001 if (&LU != &OrigLU &&
2002 LU.Kind != LSRUse::ICmpZero &&
2003 LU.Kind == OrigLU.Kind && OrigLU.AccessTy == LU.AccessTy &&
2004 LU.WidestFixupType == OrigLU.WidestFixupType &&
2005 LU.HasFormulaWithSameRegs(OrigF)) {
2006 // Scan through this use's formulae.
2007 for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(),
2008 E = LU.Formulae.end(); I != E; ++I) {
2009 const Formula &F = *I;
2010 // Check to see if this formula has the same registers and symbols
2012 if (F.BaseRegs == OrigF.BaseRegs &&
2013 F.ScaledReg == OrigF.ScaledReg &&
2014 F.AM.BaseGV == OrigF.AM.BaseGV &&
2015 F.AM.Scale == OrigF.AM.Scale &&
2016 F.UnfoldedOffset == OrigF.UnfoldedOffset) {
2017 if (F.AM.BaseOffs == 0)
2019 // This is the formula where all the registers and symbols matched;
2020 // there aren't going to be any others. Since we declined it, we
2021 // can skip the rest of the formulae and procede to the next LSRUse.
2028 // Nothing looked good.
2032 void LSRInstance::CollectInterestingTypesAndFactors() {
2033 SmallSetVector<const SCEV *, 4> Strides;
2035 // Collect interesting types and strides.
2036 SmallVector<const SCEV *, 4> Worklist;
2037 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) {
2038 const SCEV *Expr = IU.getExpr(*UI);
2040 // Collect interesting types.
2041 Types.insert(SE.getEffectiveSCEVType(Expr->getType()));
2043 // Add strides for mentioned loops.
2044 Worklist.push_back(Expr);
2046 const SCEV *S = Worklist.pop_back_val();
2047 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
2048 Strides.insert(AR->getStepRecurrence(SE));
2049 Worklist.push_back(AR->getStart());
2050 } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
2051 Worklist.append(Add->op_begin(), Add->op_end());
2053 } while (!Worklist.empty());
2056 // Compute interesting factors from the set of interesting strides.
2057 for (SmallSetVector<const SCEV *, 4>::const_iterator
2058 I = Strides.begin(), E = Strides.end(); I != E; ++I)
2059 for (SmallSetVector<const SCEV *, 4>::const_iterator NewStrideIter =
2060 llvm::next(I); NewStrideIter != E; ++NewStrideIter) {
2061 const SCEV *OldStride = *I;
2062 const SCEV *NewStride = *NewStrideIter;
2064 if (SE.getTypeSizeInBits(OldStride->getType()) !=
2065 SE.getTypeSizeInBits(NewStride->getType())) {
2066 if (SE.getTypeSizeInBits(OldStride->getType()) >
2067 SE.getTypeSizeInBits(NewStride->getType()))
2068 NewStride = SE.getSignExtendExpr(NewStride, OldStride->getType());
2070 OldStride = SE.getSignExtendExpr(OldStride, NewStride->getType());
2072 if (const SCEVConstant *Factor =
2073 dyn_cast_or_null<SCEVConstant>(getExactSDiv(NewStride, OldStride,
2075 if (Factor->getValue()->getValue().getMinSignedBits() <= 64)
2076 Factors.insert(Factor->getValue()->getValue().getSExtValue());
2077 } else if (const SCEVConstant *Factor =
2078 dyn_cast_or_null<SCEVConstant>(getExactSDiv(OldStride,
2081 if (Factor->getValue()->getValue().getMinSignedBits() <= 64)
2082 Factors.insert(Factor->getValue()->getValue().getSExtValue());
2086 // If all uses use the same type, don't bother looking for truncation-based
2088 if (Types.size() == 1)
2091 DEBUG(print_factors_and_types(dbgs()));
2094 void LSRInstance::CollectFixupsAndInitialFormulae() {
2095 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI) {
2097 LSRFixup &LF = getNewFixup();
2098 LF.UserInst = UI->getUser();
2099 LF.OperandValToReplace = UI->getOperandValToReplace();
2100 LF.PostIncLoops = UI->getPostIncLoops();
2102 LSRUse::KindType Kind = LSRUse::Basic;
2104 if (isAddressUse(LF.UserInst, LF.OperandValToReplace)) {
2105 Kind = LSRUse::Address;
2106 AccessTy = getAccessType(LF.UserInst);
2109 const SCEV *S = IU.getExpr(*UI);
2111 // Equality (== and !=) ICmps are special. We can rewrite (i == N) as
2112 // (N - i == 0), and this allows (N - i) to be the expression that we work
2113 // with rather than just N or i, so we can consider the register
2114 // requirements for both N and i at the same time. Limiting this code to
2115 // equality icmps is not a problem because all interesting loops use
2116 // equality icmps, thanks to IndVarSimplify.
2117 if (ICmpInst *CI = dyn_cast<ICmpInst>(LF.UserInst))
2118 if (CI->isEquality()) {
2119 // Swap the operands if needed to put the OperandValToReplace on the
2120 // left, for consistency.
2121 Value *NV = CI->getOperand(1);
2122 if (NV == LF.OperandValToReplace) {
2123 CI->setOperand(1, CI->getOperand(0));
2124 CI->setOperand(0, NV);
2125 NV = CI->getOperand(1);
2129 // x == y --> x - y == 0
2130 const SCEV *N = SE.getSCEV(NV);
2131 if (SE.isLoopInvariant(N, L)) {
2132 // S is normalized, so normalize N before folding it into S
2133 // to keep the result normalized.
2134 N = TransformForPostIncUse(Normalize, N, CI, 0,
2135 LF.PostIncLoops, SE, DT);
2136 Kind = LSRUse::ICmpZero;
2137 S = SE.getMinusSCEV(N, S);
2140 // -1 and the negations of all interesting strides (except the negation
2141 // of -1) are now also interesting.
2142 for (size_t i = 0, e = Factors.size(); i != e; ++i)
2143 if (Factors[i] != -1)
2144 Factors.insert(-(uint64_t)Factors[i]);
2148 // Set up the initial formula for this use.
2149 std::pair<size_t, int64_t> P = getUse(S, Kind, AccessTy);
2151 LF.Offset = P.second;
2152 LSRUse &LU = Uses[LF.LUIdx];
2153 LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L);
2154 if (!LU.WidestFixupType ||
2155 SE.getTypeSizeInBits(LU.WidestFixupType) <
2156 SE.getTypeSizeInBits(LF.OperandValToReplace->getType()))
2157 LU.WidestFixupType = LF.OperandValToReplace->getType();
2159 // If this is the first use of this LSRUse, give it a formula.
2160 if (LU.Formulae.empty()) {
2161 InsertInitialFormula(S, LU, LF.LUIdx);
2162 CountRegisters(LU.Formulae.back(), LF.LUIdx);
2166 DEBUG(print_fixups(dbgs()));
2169 /// InsertInitialFormula - Insert a formula for the given expression into
2170 /// the given use, separating out loop-variant portions from loop-invariant
2171 /// and loop-computable portions.
2173 LSRInstance::InsertInitialFormula(const SCEV *S, LSRUse &LU, size_t LUIdx) {
2175 F.InitialMatch(S, L, SE);
2176 bool Inserted = InsertFormula(LU, LUIdx, F);
2177 assert(Inserted && "Initial formula already exists!"); (void)Inserted;
2180 /// InsertSupplementalFormula - Insert a simple single-register formula for
2181 /// the given expression into the given use.
2183 LSRInstance::InsertSupplementalFormula(const SCEV *S,
2184 LSRUse &LU, size_t LUIdx) {
2186 F.BaseRegs.push_back(S);
2187 F.AM.HasBaseReg = true;
2188 bool Inserted = InsertFormula(LU, LUIdx, F);
2189 assert(Inserted && "Supplemental formula already exists!"); (void)Inserted;
2192 /// CountRegisters - Note which registers are used by the given formula,
2193 /// updating RegUses.
2194 void LSRInstance::CountRegisters(const Formula &F, size_t LUIdx) {
2196 RegUses.CountRegister(F.ScaledReg, LUIdx);
2197 for (SmallVectorImpl<const SCEV *>::const_iterator I = F.BaseRegs.begin(),
2198 E = F.BaseRegs.end(); I != E; ++I)
2199 RegUses.CountRegister(*I, LUIdx);
2202 /// InsertFormula - If the given formula has not yet been inserted, add it to
2203 /// the list, and return true. Return false otherwise.
2204 bool LSRInstance::InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F) {
2205 if (!LU.InsertFormula(F))
2208 CountRegisters(F, LUIdx);
2212 /// CollectLoopInvariantFixupsAndFormulae - Check for other uses of
2213 /// loop-invariant values which we're tracking. These other uses will pin these
2214 /// values in registers, making them less profitable for elimination.
2215 /// TODO: This currently misses non-constant addrec step registers.
2216 /// TODO: Should this give more weight to users inside the loop?
2218 LSRInstance::CollectLoopInvariantFixupsAndFormulae() {
2219 SmallVector<const SCEV *, 8> Worklist(RegUses.begin(), RegUses.end());
2220 SmallPtrSet<const SCEV *, 8> Inserted;
2222 while (!Worklist.empty()) {
2223 const SCEV *S = Worklist.pop_back_val();
2225 if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S))
2226 Worklist.append(N->op_begin(), N->op_end());
2227 else if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S))
2228 Worklist.push_back(C->getOperand());
2229 else if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) {
2230 Worklist.push_back(D->getLHS());
2231 Worklist.push_back(D->getRHS());
2232 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2233 if (!Inserted.insert(U)) continue;
2234 const Value *V = U->getValue();
2235 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
2236 // Look for instructions defined outside the loop.
2237 if (L->contains(Inst)) continue;
2238 } else if (isa<UndefValue>(V))
2239 // Undef doesn't have a live range, so it doesn't matter.
2241 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
2243 const Instruction *UserInst = dyn_cast<Instruction>(*UI);
2244 // Ignore non-instructions.
2247 // Ignore instructions in other functions (as can happen with
2249 if (UserInst->getParent()->getParent() != L->getHeader()->getParent())
2251 // Ignore instructions not dominated by the loop.
2252 const BasicBlock *UseBB = !isa<PHINode>(UserInst) ?
2253 UserInst->getParent() :
2254 cast<PHINode>(UserInst)->getIncomingBlock(
2255 PHINode::getIncomingValueNumForOperand(UI.getOperandNo()));
2256 if (!DT.dominates(L->getHeader(), UseBB))
2258 // Ignore uses which are part of other SCEV expressions, to avoid
2259 // analyzing them multiple times.
2260 if (SE.isSCEVable(UserInst->getType())) {
2261 const SCEV *UserS = SE.getSCEV(const_cast<Instruction *>(UserInst));
2262 // If the user is a no-op, look through to its uses.
2263 if (!isa<SCEVUnknown>(UserS))
2267 SE.getUnknown(const_cast<Instruction *>(UserInst)));
2271 // Ignore icmp instructions which are already being analyzed.
2272 if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UserInst)) {
2273 unsigned OtherIdx = !UI.getOperandNo();
2274 Value *OtherOp = const_cast<Value *>(ICI->getOperand(OtherIdx));
2275 if (SE.hasComputableLoopEvolution(SE.getSCEV(OtherOp), L))
2279 LSRFixup &LF = getNewFixup();
2280 LF.UserInst = const_cast<Instruction *>(UserInst);
2281 LF.OperandValToReplace = UI.getUse();
2282 std::pair<size_t, int64_t> P = getUse(S, LSRUse::Basic, 0);
2284 LF.Offset = P.second;
2285 LSRUse &LU = Uses[LF.LUIdx];
2286 LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L);
2287 if (!LU.WidestFixupType ||
2288 SE.getTypeSizeInBits(LU.WidestFixupType) <
2289 SE.getTypeSizeInBits(LF.OperandValToReplace->getType()))
2290 LU.WidestFixupType = LF.OperandValToReplace->getType();
2291 InsertSupplementalFormula(U, LU, LF.LUIdx);
2292 CountRegisters(LU.Formulae.back(), Uses.size() - 1);
2299 /// CollectSubexprs - Split S into subexpressions which can be pulled out into
2300 /// separate registers. If C is non-null, multiply each subexpression by C.
2301 static void CollectSubexprs(const SCEV *S, const SCEVConstant *C,
2302 SmallVectorImpl<const SCEV *> &Ops,
2304 ScalarEvolution &SE) {
2305 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
2306 // Break out add operands.
2307 for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
2309 CollectSubexprs(*I, C, Ops, L, SE);
2311 } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
2312 // Split a non-zero base out of an addrec.
2313 if (!AR->getStart()->isZero()) {
2314 CollectSubexprs(SE.getAddRecExpr(SE.getConstant(AR->getType(), 0),
2315 AR->getStepRecurrence(SE),
2317 //FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
2320 CollectSubexprs(AR->getStart(), C, Ops, L, SE);
2323 } else if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
2324 // Break (C * (a + b + c)) into C*a + C*b + C*c.
2325 if (Mul->getNumOperands() == 2)
2326 if (const SCEVConstant *Op0 =
2327 dyn_cast<SCEVConstant>(Mul->getOperand(0))) {
2328 CollectSubexprs(Mul->getOperand(1),
2329 C ? cast<SCEVConstant>(SE.getMulExpr(C, Op0)) : Op0,
2335 // Otherwise use the value itself, optionally with a scale applied.
2336 Ops.push_back(C ? SE.getMulExpr(C, S) : S);
2339 /// GenerateReassociations - Split out subexpressions from adds and the bases of
2341 void LSRInstance::GenerateReassociations(LSRUse &LU, unsigned LUIdx,
2344 // Arbitrarily cap recursion to protect compile time.
2345 if (Depth >= 3) return;
2347 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) {
2348 const SCEV *BaseReg = Base.BaseRegs[i];
2350 SmallVector<const SCEV *, 8> AddOps;
2351 CollectSubexprs(BaseReg, 0, AddOps, L, SE);
2353 if (AddOps.size() == 1) continue;
2355 for (SmallVectorImpl<const SCEV *>::const_iterator J = AddOps.begin(),
2356 JE = AddOps.end(); J != JE; ++J) {
2358 // Loop-variant "unknown" values are uninteresting; we won't be able to
2359 // do anything meaningful with them.
2360 if (isa<SCEVUnknown>(*J) && !SE.isLoopInvariant(*J, L))
2363 // Don't pull a constant into a register if the constant could be folded
2364 // into an immediate field.
2365 if (isAlwaysFoldable(*J, LU.MinOffset, LU.MaxOffset,
2366 Base.getNumRegs() > 1,
2367 LU.Kind, LU.AccessTy, TLI, SE))
2370 // Collect all operands except *J.
2371 SmallVector<const SCEV *, 8> InnerAddOps
2372 (((const SmallVector<const SCEV *, 8> &)AddOps).begin(), J);
2374 (llvm::next(J), ((const SmallVector<const SCEV *, 8> &)AddOps).end());
2376 // Don't leave just a constant behind in a register if the constant could
2377 // be folded into an immediate field.
2378 if (InnerAddOps.size() == 1 &&
2379 isAlwaysFoldable(InnerAddOps[0], LU.MinOffset, LU.MaxOffset,
2380 Base.getNumRegs() > 1,
2381 LU.Kind, LU.AccessTy, TLI, SE))
2384 const SCEV *InnerSum = SE.getAddExpr(InnerAddOps);
2385 if (InnerSum->isZero())
2389 // Add the remaining pieces of the add back into the new formula.
2390 const SCEVConstant *InnerSumSC = dyn_cast<SCEVConstant>(InnerSum);
2391 if (TLI && InnerSumSC &&
2392 SE.getTypeSizeInBits(InnerSumSC->getType()) <= 64 &&
2393 TLI->isLegalAddImmediate((uint64_t)F.UnfoldedOffset +
2394 InnerSumSC->getValue()->getZExtValue())) {
2395 F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset +
2396 InnerSumSC->getValue()->getZExtValue();
2397 F.BaseRegs.erase(F.BaseRegs.begin() + i);
2399 F.BaseRegs[i] = InnerSum;
2401 // Add J as its own register, or an unfolded immediate.
2402 const SCEVConstant *SC = dyn_cast<SCEVConstant>(*J);
2403 if (TLI && SC && SE.getTypeSizeInBits(SC->getType()) <= 64 &&
2404 TLI->isLegalAddImmediate((uint64_t)F.UnfoldedOffset +
2405 SC->getValue()->getZExtValue()))
2406 F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset +
2407 SC->getValue()->getZExtValue();
2409 F.BaseRegs.push_back(*J);
2411 if (InsertFormula(LU, LUIdx, F))
2412 // If that formula hadn't been seen before, recurse to find more like
2414 GenerateReassociations(LU, LUIdx, LU.Formulae.back(), Depth+1);
2419 /// GenerateCombinations - Generate a formula consisting of all of the
2420 /// loop-dominating registers added into a single register.
2421 void LSRInstance::GenerateCombinations(LSRUse &LU, unsigned LUIdx,
2423 // This method is only interesting on a plurality of registers.
2424 if (Base.BaseRegs.size() <= 1) return;
2428 SmallVector<const SCEV *, 4> Ops;
2429 for (SmallVectorImpl<const SCEV *>::const_iterator
2430 I = Base.BaseRegs.begin(), E = Base.BaseRegs.end(); I != E; ++I) {
2431 const SCEV *BaseReg = *I;
2432 if (SE.properlyDominates(BaseReg, L->getHeader()) &&
2433 !SE.hasComputableLoopEvolution(BaseReg, L))
2434 Ops.push_back(BaseReg);
2436 F.BaseRegs.push_back(BaseReg);
2438 if (Ops.size() > 1) {
2439 const SCEV *Sum = SE.getAddExpr(Ops);
2440 // TODO: If Sum is zero, it probably means ScalarEvolution missed an
2441 // opportunity to fold something. For now, just ignore such cases
2442 // rather than proceed with zero in a register.
2443 if (!Sum->isZero()) {
2444 F.BaseRegs.push_back(Sum);
2445 (void)InsertFormula(LU, LUIdx, F);
2450 /// GenerateSymbolicOffsets - Generate reuse formulae using symbolic offsets.
2451 void LSRInstance::GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx,
2453 // We can't add a symbolic offset if the address already contains one.
2454 if (Base.AM.BaseGV) return;
2456 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) {
2457 const SCEV *G = Base.BaseRegs[i];
2458 GlobalValue *GV = ExtractSymbol(G, SE);
2459 if (G->isZero() || !GV)
2463 if (!isLegalUse(F.AM, LU.MinOffset, LU.MaxOffset,
2464 LU.Kind, LU.AccessTy, TLI))
2467 (void)InsertFormula(LU, LUIdx, F);
2471 /// GenerateConstantOffsets - Generate reuse formulae using symbolic offsets.
2472 void LSRInstance::GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx,
2474 // TODO: For now, just add the min and max offset, because it usually isn't
2475 // worthwhile looking at everything inbetween.
2476 SmallVector<int64_t, 2> Worklist;
2477 Worklist.push_back(LU.MinOffset);
2478 if (LU.MaxOffset != LU.MinOffset)
2479 Worklist.push_back(LU.MaxOffset);
2481 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) {
2482 const SCEV *G = Base.BaseRegs[i];
2484 for (SmallVectorImpl<int64_t>::const_iterator I = Worklist.begin(),
2485 E = Worklist.end(); I != E; ++I) {
2487 F.AM.BaseOffs = (uint64_t)Base.AM.BaseOffs - *I;
2488 if (isLegalUse(F.AM, LU.MinOffset - *I, LU.MaxOffset - *I,
2489 LU.Kind, LU.AccessTy, TLI)) {
2490 // Add the offset to the base register.
2491 const SCEV *NewG = SE.getAddExpr(SE.getConstant(G->getType(), *I), G);
2492 // If it cancelled out, drop the base register, otherwise update it.
2493 if (NewG->isZero()) {
2494 std::swap(F.BaseRegs[i], F.BaseRegs.back());
2495 F.BaseRegs.pop_back();
2497 F.BaseRegs[i] = NewG;
2499 (void)InsertFormula(LU, LUIdx, F);
2503 int64_t Imm = ExtractImmediate(G, SE);
2504 if (G->isZero() || Imm == 0)
2507 F.AM.BaseOffs = (uint64_t)F.AM.BaseOffs + Imm;
2508 if (!isLegalUse(F.AM, LU.MinOffset, LU.MaxOffset,
2509 LU.Kind, LU.AccessTy, TLI))
2512 (void)InsertFormula(LU, LUIdx, F);
2516 /// GenerateICmpZeroScales - For ICmpZero, check to see if we can scale up
2517 /// the comparison. For example, x == y -> x*c == y*c.
2518 void LSRInstance::GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx,
2520 if (LU.Kind != LSRUse::ICmpZero) return;
2522 // Determine the integer type for the base formula.
2523 Type *IntTy = Base.getType();
2525 if (SE.getTypeSizeInBits(IntTy) > 64) return;
2527 // Don't do this if there is more than one offset.
2528 if (LU.MinOffset != LU.MaxOffset) return;
2530 assert(!Base.AM.BaseGV && "ICmpZero use is not legal!");
2532 // Check each interesting stride.
2533 for (SmallSetVector<int64_t, 8>::const_iterator
2534 I = Factors.begin(), E = Factors.end(); I != E; ++I) {
2535 int64_t Factor = *I;
2537 // Check that the multiplication doesn't overflow.
2538 if (Base.AM.BaseOffs == INT64_MIN && Factor == -1)
2540 int64_t NewBaseOffs = (uint64_t)Base.AM.BaseOffs * Factor;
2541 if (NewBaseOffs / Factor != Base.AM.BaseOffs)
2544 // Check that multiplying with the use offset doesn't overflow.
2545 int64_t Offset = LU.MinOffset;
2546 if (Offset == INT64_MIN && Factor == -1)
2548 Offset = (uint64_t)Offset * Factor;
2549 if (Offset / Factor != LU.MinOffset)
2553 F.AM.BaseOffs = NewBaseOffs;
2555 // Check that this scale is legal.
2556 if (!isLegalUse(F.AM, Offset, Offset, LU.Kind, LU.AccessTy, TLI))
2559 // Compensate for the use having MinOffset built into it.
2560 F.AM.BaseOffs = (uint64_t)F.AM.BaseOffs + Offset - LU.MinOffset;
2562 const SCEV *FactorS = SE.getConstant(IntTy, Factor);
2564 // Check that multiplying with each base register doesn't overflow.
2565 for (size_t i = 0, e = F.BaseRegs.size(); i != e; ++i) {
2566 F.BaseRegs[i] = SE.getMulExpr(F.BaseRegs[i], FactorS);
2567 if (getExactSDiv(F.BaseRegs[i], FactorS, SE) != Base.BaseRegs[i])
2571 // Check that multiplying with the scaled register doesn't overflow.
2573 F.ScaledReg = SE.getMulExpr(F.ScaledReg, FactorS);
2574 if (getExactSDiv(F.ScaledReg, FactorS, SE) != Base.ScaledReg)
2578 // Check that multiplying with the unfolded offset doesn't overflow.
2579 if (F.UnfoldedOffset != 0) {
2580 if (F.UnfoldedOffset == INT64_MIN && Factor == -1)
2582 F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset * Factor;
2583 if (F.UnfoldedOffset / Factor != Base.UnfoldedOffset)
2587 // If we make it here and it's legal, add it.
2588 (void)InsertFormula(LU, LUIdx, F);
2593 /// GenerateScales - Generate stride factor reuse formulae by making use of
2594 /// scaled-offset address modes, for example.
2595 void LSRInstance::GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base) {
2596 // Determine the integer type for the base formula.
2597 Type *IntTy = Base.getType();
2600 // If this Formula already has a scaled register, we can't add another one.
2601 if (Base.AM.Scale != 0) return;
2603 // Check each interesting stride.
2604 for (SmallSetVector<int64_t, 8>::const_iterator
2605 I = Factors.begin(), E = Factors.end(); I != E; ++I) {
2606 int64_t Factor = *I;
2608 Base.AM.Scale = Factor;
2609 Base.AM.HasBaseReg = Base.BaseRegs.size() > 1;
2610 // Check whether this scale is going to be legal.
2611 if (!isLegalUse(Base.AM, LU.MinOffset, LU.MaxOffset,
2612 LU.Kind, LU.AccessTy, TLI)) {
2613 // As a special-case, handle special out-of-loop Basic users specially.
2614 // TODO: Reconsider this special case.
2615 if (LU.Kind == LSRUse::Basic &&
2616 isLegalUse(Base.AM, LU.MinOffset, LU.MaxOffset,
2617 LSRUse::Special, LU.AccessTy, TLI) &&
2618 LU.AllFixupsOutsideLoop)
2619 LU.Kind = LSRUse::Special;
2623 // For an ICmpZero, negating a solitary base register won't lead to
2625 if (LU.Kind == LSRUse::ICmpZero &&
2626 !Base.AM.HasBaseReg && Base.AM.BaseOffs == 0 && !Base.AM.BaseGV)
2628 // For each addrec base reg, apply the scale, if possible.
2629 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i)
2630 if (const SCEVAddRecExpr *AR =
2631 dyn_cast<SCEVAddRecExpr>(Base.BaseRegs[i])) {
2632 const SCEV *FactorS = SE.getConstant(IntTy, Factor);
2633 if (FactorS->isZero())
2635 // Divide out the factor, ignoring high bits, since we'll be
2636 // scaling the value back up in the end.
2637 if (const SCEV *Quotient = getExactSDiv(AR, FactorS, SE, true)) {
2638 // TODO: This could be optimized to avoid all the copying.
2640 F.ScaledReg = Quotient;
2641 F.DeleteBaseReg(F.BaseRegs[i]);
2642 (void)InsertFormula(LU, LUIdx, F);
2648 /// GenerateTruncates - Generate reuse formulae from different IV types.
2649 void LSRInstance::GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base) {
2650 // This requires TargetLowering to tell us which truncates are free.
2653 // Don't bother truncating symbolic values.
2654 if (Base.AM.BaseGV) return;
2656 // Determine the integer type for the base formula.
2657 Type *DstTy = Base.getType();
2659 DstTy = SE.getEffectiveSCEVType(DstTy);
2661 for (SmallSetVector<Type *, 4>::const_iterator
2662 I = Types.begin(), E = Types.end(); I != E; ++I) {
2664 if (SrcTy != DstTy && TLI->isTruncateFree(SrcTy, DstTy)) {
2667 if (F.ScaledReg) F.ScaledReg = SE.getAnyExtendExpr(F.ScaledReg, *I);
2668 for (SmallVectorImpl<const SCEV *>::iterator J = F.BaseRegs.begin(),
2669 JE = F.BaseRegs.end(); J != JE; ++J)
2670 *J = SE.getAnyExtendExpr(*J, SrcTy);
2672 // TODO: This assumes we've done basic processing on all uses and
2673 // have an idea what the register usage is.
2674 if (!F.hasRegsUsedByUsesOtherThan(LUIdx, RegUses))
2677 (void)InsertFormula(LU, LUIdx, F);
2684 /// WorkItem - Helper class for GenerateCrossUseConstantOffsets. It's used to
2685 /// defer modifications so that the search phase doesn't have to worry about
2686 /// the data structures moving underneath it.
2690 const SCEV *OrigReg;
2692 WorkItem(size_t LI, int64_t I, const SCEV *R)
2693 : LUIdx(LI), Imm(I), OrigReg(R) {}
2695 void print(raw_ostream &OS) const;
2701 void WorkItem::print(raw_ostream &OS) const {
2702 OS << "in formulae referencing " << *OrigReg << " in use " << LUIdx
2703 << " , add offset " << Imm;
2706 void WorkItem::dump() const {
2707 print(errs()); errs() << '\n';
2710 /// GenerateCrossUseConstantOffsets - Look for registers which are a constant
2711 /// distance apart and try to form reuse opportunities between them.
2712 void LSRInstance::GenerateCrossUseConstantOffsets() {
2713 // Group the registers by their value without any added constant offset.
2714 typedef std::map<int64_t, const SCEV *> ImmMapTy;
2715 typedef DenseMap<const SCEV *, ImmMapTy> RegMapTy;
2717 DenseMap<const SCEV *, SmallBitVector> UsedByIndicesMap;
2718 SmallVector<const SCEV *, 8> Sequence;
2719 for (RegUseTracker::const_iterator I = RegUses.begin(), E = RegUses.end();
2721 const SCEV *Reg = *I;
2722 int64_t Imm = ExtractImmediate(Reg, SE);
2723 std::pair<RegMapTy::iterator, bool> Pair =
2724 Map.insert(std::make_pair(Reg, ImmMapTy()));
2726 Sequence.push_back(Reg);
2727 Pair.first->second.insert(std::make_pair(Imm, *I));
2728 UsedByIndicesMap[Reg] |= RegUses.getUsedByIndices(*I);
2731 // Now examine each set of registers with the same base value. Build up
2732 // a list of work to do and do the work in a separate step so that we're
2733 // not adding formulae and register counts while we're searching.
2734 SmallVector<WorkItem, 32> WorkItems;
2735 SmallSet<std::pair<size_t, int64_t>, 32> UniqueItems;
2736 for (SmallVectorImpl<const SCEV *>::const_iterator I = Sequence.begin(),
2737 E = Sequence.end(); I != E; ++I) {
2738 const SCEV *Reg = *I;
2739 const ImmMapTy &Imms = Map.find(Reg)->second;
2741 // It's not worthwhile looking for reuse if there's only one offset.
2742 if (Imms.size() == 1)
2745 DEBUG(dbgs() << "Generating cross-use offsets for " << *Reg << ':';
2746 for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end();
2748 dbgs() << ' ' << J->first;
2751 // Examine each offset.
2752 for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end();
2754 const SCEV *OrigReg = J->second;
2756 int64_t JImm = J->first;
2757 const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(OrigReg);
2759 if (!isa<SCEVConstant>(OrigReg) &&
2760 UsedByIndicesMap[Reg].count() == 1) {
2761 DEBUG(dbgs() << "Skipping cross-use reuse for " << *OrigReg << '\n');
2765 // Conservatively examine offsets between this orig reg a few selected
2767 ImmMapTy::const_iterator OtherImms[] = {
2768 Imms.begin(), prior(Imms.end()),
2769 Imms.lower_bound((Imms.begin()->first + prior(Imms.end())->first) / 2)
2771 for (size_t i = 0, e = array_lengthof(OtherImms); i != e; ++i) {
2772 ImmMapTy::const_iterator M = OtherImms[i];
2773 if (M == J || M == JE) continue;
2775 // Compute the difference between the two.
2776 int64_t Imm = (uint64_t)JImm - M->first;
2777 for (int LUIdx = UsedByIndices.find_first(); LUIdx != -1;
2778 LUIdx = UsedByIndices.find_next(LUIdx))
2779 // Make a memo of this use, offset, and register tuple.
2780 if (UniqueItems.insert(std::make_pair(LUIdx, Imm)))
2781 WorkItems.push_back(WorkItem(LUIdx, Imm, OrigReg));
2788 UsedByIndicesMap.clear();
2789 UniqueItems.clear();
2791 // Now iterate through the worklist and add new formulae.
2792 for (SmallVectorImpl<WorkItem>::const_iterator I = WorkItems.begin(),
2793 E = WorkItems.end(); I != E; ++I) {
2794 const WorkItem &WI = *I;
2795 size_t LUIdx = WI.LUIdx;
2796 LSRUse &LU = Uses[LUIdx];
2797 int64_t Imm = WI.Imm;
2798 const SCEV *OrigReg = WI.OrigReg;
2800 Type *IntTy = SE.getEffectiveSCEVType(OrigReg->getType());
2801 const SCEV *NegImmS = SE.getSCEV(ConstantInt::get(IntTy, -(uint64_t)Imm));
2802 unsigned BitWidth = SE.getTypeSizeInBits(IntTy);
2804 // TODO: Use a more targeted data structure.
2805 for (size_t L = 0, LE = LU.Formulae.size(); L != LE; ++L) {
2806 const Formula &F = LU.Formulae[L];
2807 // Use the immediate in the scaled register.
2808 if (F.ScaledReg == OrigReg) {
2809 int64_t Offs = (uint64_t)F.AM.BaseOffs +
2810 Imm * (uint64_t)F.AM.Scale;
2811 // Don't create 50 + reg(-50).
2812 if (F.referencesReg(SE.getSCEV(
2813 ConstantInt::get(IntTy, -(uint64_t)Offs))))
2816 NewF.AM.BaseOffs = Offs;
2817 if (!isLegalUse(NewF.AM, LU.MinOffset, LU.MaxOffset,
2818 LU.Kind, LU.AccessTy, TLI))
2820 NewF.ScaledReg = SE.getAddExpr(NegImmS, NewF.ScaledReg);
2822 // If the new scale is a constant in a register, and adding the constant
2823 // value to the immediate would produce a value closer to zero than the
2824 // immediate itself, then the formula isn't worthwhile.
2825 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(NewF.ScaledReg))
2826 if (C->getValue()->isNegative() !=
2827 (NewF.AM.BaseOffs < 0) &&
2828 (C->getValue()->getValue().abs() * APInt(BitWidth, F.AM.Scale))
2829 .ule(abs64(NewF.AM.BaseOffs)))
2833 (void)InsertFormula(LU, LUIdx, NewF);
2835 // Use the immediate in a base register.
2836 for (size_t N = 0, NE = F.BaseRegs.size(); N != NE; ++N) {
2837 const SCEV *BaseReg = F.BaseRegs[N];
2838 if (BaseReg != OrigReg)
2841 NewF.AM.BaseOffs = (uint64_t)NewF.AM.BaseOffs + Imm;
2842 if (!isLegalUse(NewF.AM, LU.MinOffset, LU.MaxOffset,
2843 LU.Kind, LU.AccessTy, TLI)) {
2845 !TLI->isLegalAddImmediate((uint64_t)NewF.UnfoldedOffset + Imm))
2848 NewF.UnfoldedOffset = (uint64_t)NewF.UnfoldedOffset + Imm;
2850 NewF.BaseRegs[N] = SE.getAddExpr(NegImmS, BaseReg);
2852 // If the new formula has a constant in a register, and adding the
2853 // constant value to the immediate would produce a value closer to
2854 // zero than the immediate itself, then the formula isn't worthwhile.
2855 for (SmallVectorImpl<const SCEV *>::const_iterator
2856 J = NewF.BaseRegs.begin(), JE = NewF.BaseRegs.end();
2858 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(*J))
2859 if ((C->getValue()->getValue() + NewF.AM.BaseOffs).abs().slt(
2860 abs64(NewF.AM.BaseOffs)) &&
2861 (C->getValue()->getValue() +
2862 NewF.AM.BaseOffs).countTrailingZeros() >=
2863 CountTrailingZeros_64(NewF.AM.BaseOffs))
2867 (void)InsertFormula(LU, LUIdx, NewF);
2876 /// GenerateAllReuseFormulae - Generate formulae for each use.
2878 LSRInstance::GenerateAllReuseFormulae() {
2879 // This is split into multiple loops so that hasRegsUsedByUsesOtherThan
2880 // queries are more precise.
2881 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
2882 LSRUse &LU = Uses[LUIdx];
2883 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
2884 GenerateReassociations(LU, LUIdx, LU.Formulae[i]);
2885 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
2886 GenerateCombinations(LU, LUIdx, LU.Formulae[i]);
2888 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
2889 LSRUse &LU = Uses[LUIdx];
2890 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
2891 GenerateSymbolicOffsets(LU, LUIdx, LU.Formulae[i]);
2892 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
2893 GenerateConstantOffsets(LU, LUIdx, LU.Formulae[i]);
2894 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
2895 GenerateICmpZeroScales(LU, LUIdx, LU.Formulae[i]);
2896 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
2897 GenerateScales(LU, LUIdx, LU.Formulae[i]);
2899 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
2900 LSRUse &LU = Uses[LUIdx];
2901 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
2902 GenerateTruncates(LU, LUIdx, LU.Formulae[i]);
2905 GenerateCrossUseConstantOffsets();
2907 DEBUG(dbgs() << "\n"
2908 "After generating reuse formulae:\n";
2909 print_uses(dbgs()));
2912 /// If there are multiple formulae with the same set of registers used
2913 /// by other uses, pick the best one and delete the others.
2914 void LSRInstance::FilterOutUndesirableDedicatedRegisters() {
2915 DenseSet<const SCEV *> VisitedRegs;
2916 SmallPtrSet<const SCEV *, 16> Regs;
2918 bool ChangedFormulae = false;
2921 // Collect the best formula for each unique set of shared registers. This
2922 // is reset for each use.
2923 typedef DenseMap<SmallVector<const SCEV *, 2>, size_t, UniquifierDenseMapInfo>
2925 BestFormulaeTy BestFormulae;
2927 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
2928 LSRUse &LU = Uses[LUIdx];
2929 DEBUG(dbgs() << "Filtering for use "; LU.print(dbgs()); dbgs() << '\n');
2932 for (size_t FIdx = 0, NumForms = LU.Formulae.size();
2933 FIdx != NumForms; ++FIdx) {
2934 Formula &F = LU.Formulae[FIdx];
2936 SmallVector<const SCEV *, 2> Key;
2937 for (SmallVectorImpl<const SCEV *>::const_iterator J = F.BaseRegs.begin(),
2938 JE = F.BaseRegs.end(); J != JE; ++J) {
2939 const SCEV *Reg = *J;
2940 if (RegUses.isRegUsedByUsesOtherThan(Reg, LUIdx))
2944 RegUses.isRegUsedByUsesOtherThan(F.ScaledReg, LUIdx))
2945 Key.push_back(F.ScaledReg);
2946 // Unstable sort by host order ok, because this is only used for
2948 std::sort(Key.begin(), Key.end());
2950 std::pair<BestFormulaeTy::const_iterator, bool> P =
2951 BestFormulae.insert(std::make_pair(Key, FIdx));
2953 Formula &Best = LU.Formulae[P.first->second];
2956 CostF.RateFormula(F, Regs, VisitedRegs, L, LU.Offsets, SE, DT);
2959 CostBest.RateFormula(Best, Regs, VisitedRegs, L, LU.Offsets, SE, DT);
2961 if (CostF < CostBest)
2963 DEBUG(dbgs() << " Filtering out formula "; F.print(dbgs());
2965 " in favor of formula "; Best.print(dbgs());
2968 ChangedFormulae = true;
2970 LU.DeleteFormula(F);
2978 // Now that we've filtered out some formulae, recompute the Regs set.
2980 LU.RecomputeRegs(LUIdx, RegUses);
2982 // Reset this to prepare for the next use.
2983 BestFormulae.clear();
2986 DEBUG(if (ChangedFormulae) {
2988 "After filtering out undesirable candidates:\n";
2993 // This is a rough guess that seems to work fairly well.
2994 static const size_t ComplexityLimit = UINT16_MAX;
2996 /// EstimateSearchSpaceComplexity - Estimate the worst-case number of
2997 /// solutions the solver might have to consider. It almost never considers
2998 /// this many solutions because it prune the search space, but the pruning
2999 /// isn't always sufficient.
3000 size_t LSRInstance::EstimateSearchSpaceComplexity() const {
3002 for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(),
3003 E = Uses.end(); I != E; ++I) {
3004 size_t FSize = I->Formulae.size();
3005 if (FSize >= ComplexityLimit) {
3006 Power = ComplexityLimit;
3010 if (Power >= ComplexityLimit)
3016 /// NarrowSearchSpaceByDetectingSupersets - When one formula uses a superset
3017 /// of the registers of another formula, it won't help reduce register
3018 /// pressure (though it may not necessarily hurt register pressure); remove
3019 /// it to simplify the system.
3020 void LSRInstance::NarrowSearchSpaceByDetectingSupersets() {
3021 if (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
3022 DEBUG(dbgs() << "The search space is too complex.\n");
3024 DEBUG(dbgs() << "Narrowing the search space by eliminating formulae "
3025 "which use a superset of registers used by other "
3028 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3029 LSRUse &LU = Uses[LUIdx];
3031 for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
3032 Formula &F = LU.Formulae[i];
3033 // Look for a formula with a constant or GV in a register. If the use
3034 // also has a formula with that same value in an immediate field,
3035 // delete the one that uses a register.
3036 for (SmallVectorImpl<const SCEV *>::const_iterator
3037 I = F.BaseRegs.begin(), E = F.BaseRegs.end(); I != E; ++I) {
3038 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(*I)) {
3040 NewF.AM.BaseOffs += C->getValue()->getSExtValue();
3041 NewF.BaseRegs.erase(NewF.BaseRegs.begin() +
3042 (I - F.BaseRegs.begin()));
3043 if (LU.HasFormulaWithSameRegs(NewF)) {
3044 DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n');
3045 LU.DeleteFormula(F);
3051 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(*I)) {
3052 if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue()))
3055 NewF.AM.BaseGV = GV;
3056 NewF.BaseRegs.erase(NewF.BaseRegs.begin() +
3057 (I - F.BaseRegs.begin()));
3058 if (LU.HasFormulaWithSameRegs(NewF)) {
3059 DEBUG(dbgs() << " Deleting "; F.print(dbgs());
3061 LU.DeleteFormula(F);
3072 LU.RecomputeRegs(LUIdx, RegUses);
3075 DEBUG(dbgs() << "After pre-selection:\n";
3076 print_uses(dbgs()));
3080 /// NarrowSearchSpaceByCollapsingUnrolledCode - When there are many registers
3081 /// for expressions like A, A+1, A+2, etc., allocate a single register for
3083 void LSRInstance::NarrowSearchSpaceByCollapsingUnrolledCode() {
3084 if (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
3085 DEBUG(dbgs() << "The search space is too complex.\n");
3087 DEBUG(dbgs() << "Narrowing the search space by assuming that uses "
3088 "separated by a constant offset will use the same "
3091 // This is especially useful for unrolled loops.
3093 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3094 LSRUse &LU = Uses[LUIdx];
3095 for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(),
3096 E = LU.Formulae.end(); I != E; ++I) {
3097 const Formula &F = *I;
3098 if (F.AM.BaseOffs != 0 && F.AM.Scale == 0) {
3099 if (LSRUse *LUThatHas = FindUseWithSimilarFormula(F, LU)) {
3100 if (reconcileNewOffset(*LUThatHas, F.AM.BaseOffs,
3101 /*HasBaseReg=*/false,
3102 LU.Kind, LU.AccessTy)) {
3103 DEBUG(dbgs() << " Deleting use "; LU.print(dbgs());
3106 LUThatHas->AllFixupsOutsideLoop &= LU.AllFixupsOutsideLoop;
3108 // Update the relocs to reference the new use.
3109 for (SmallVectorImpl<LSRFixup>::iterator I = Fixups.begin(),
3110 E = Fixups.end(); I != E; ++I) {
3111 LSRFixup &Fixup = *I;
3112 if (Fixup.LUIdx == LUIdx) {
3113 Fixup.LUIdx = LUThatHas - &Uses.front();
3114 Fixup.Offset += F.AM.BaseOffs;
3115 // Add the new offset to LUThatHas' offset list.
3116 if (LUThatHas->Offsets.back() != Fixup.Offset) {
3117 LUThatHas->Offsets.push_back(Fixup.Offset);
3118 if (Fixup.Offset > LUThatHas->MaxOffset)
3119 LUThatHas->MaxOffset = Fixup.Offset;
3120 if (Fixup.Offset < LUThatHas->MinOffset)
3121 LUThatHas->MinOffset = Fixup.Offset;
3123 DEBUG(dbgs() << "New fixup has offset "
3124 << Fixup.Offset << '\n');
3126 if (Fixup.LUIdx == NumUses-1)
3127 Fixup.LUIdx = LUIdx;
3130 // Delete formulae from the new use which are no longer legal.
3132 for (size_t i = 0, e = LUThatHas->Formulae.size(); i != e; ++i) {
3133 Formula &F = LUThatHas->Formulae[i];
3134 if (!isLegalUse(F.AM,
3135 LUThatHas->MinOffset, LUThatHas->MaxOffset,
3136 LUThatHas->Kind, LUThatHas->AccessTy, TLI)) {
3137 DEBUG(dbgs() << " Deleting "; F.print(dbgs());
3139 LUThatHas->DeleteFormula(F);
3146 LUThatHas->RecomputeRegs(LUThatHas - &Uses.front(), RegUses);
3148 // Delete the old use.
3149 DeleteUse(LU, LUIdx);
3159 DEBUG(dbgs() << "After pre-selection:\n";
3160 print_uses(dbgs()));
3164 /// NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters - Call
3165 /// FilterOutUndesirableDedicatedRegisters again, if necessary, now that
3166 /// we've done more filtering, as it may be able to find more formulae to
3168 void LSRInstance::NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters(){
3169 if (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
3170 DEBUG(dbgs() << "The search space is too complex.\n");
3172 DEBUG(dbgs() << "Narrowing the search space by re-filtering out "
3173 "undesirable dedicated registers.\n");
3175 FilterOutUndesirableDedicatedRegisters();
3177 DEBUG(dbgs() << "After pre-selection:\n";
3178 print_uses(dbgs()));
3182 /// NarrowSearchSpaceByPickingWinnerRegs - Pick a register which seems likely
3183 /// to be profitable, and then in any use which has any reference to that
3184 /// register, delete all formulae which do not reference that register.
3185 void LSRInstance::NarrowSearchSpaceByPickingWinnerRegs() {
3186 // With all other options exhausted, loop until the system is simple
3187 // enough to handle.
3188 SmallPtrSet<const SCEV *, 4> Taken;
3189 while (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
3190 // Ok, we have too many of formulae on our hands to conveniently handle.
3191 // Use a rough heuristic to thin out the list.
3192 DEBUG(dbgs() << "The search space is too complex.\n");
3194 // Pick the register which is used by the most LSRUses, which is likely
3195 // to be a good reuse register candidate.
3196 const SCEV *Best = 0;
3197 unsigned BestNum = 0;
3198 for (RegUseTracker::const_iterator I = RegUses.begin(), E = RegUses.end();
3200 const SCEV *Reg = *I;
3201 if (Taken.count(Reg))
3206 unsigned Count = RegUses.getUsedByIndices(Reg).count();
3207 if (Count > BestNum) {
3214 DEBUG(dbgs() << "Narrowing the search space by assuming " << *Best
3215 << " will yield profitable reuse.\n");
3218 // In any use with formulae which references this register, delete formulae
3219 // which don't reference it.
3220 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
3221 LSRUse &LU = Uses[LUIdx];
3222 if (!LU.Regs.count(Best)) continue;
3225 for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
3226 Formula &F = LU.Formulae[i];
3227 if (!F.referencesReg(Best)) {
3228 DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n');
3229 LU.DeleteFormula(F);
3233 assert(e != 0 && "Use has no formulae left! Is Regs inconsistent?");
3239 LU.RecomputeRegs(LUIdx, RegUses);
3242 DEBUG(dbgs() << "After pre-selection:\n";
3243 print_uses(dbgs()));
3247 /// NarrowSearchSpaceUsingHeuristics - If there are an extraordinary number of
3248 /// formulae to choose from, use some rough heuristics to prune down the number
3249 /// of formulae. This keeps the main solver from taking an extraordinary amount
3250 /// of time in some worst-case scenarios.
3251 void LSRInstance::NarrowSearchSpaceUsingHeuristics() {
3252 NarrowSearchSpaceByDetectingSupersets();
3253 NarrowSearchSpaceByCollapsingUnrolledCode();
3254 NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters();
3255 NarrowSearchSpaceByPickingWinnerRegs();
3258 /// SolveRecurse - This is the recursive solver.
3259 void LSRInstance::SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
3261 SmallVectorImpl<const Formula *> &Workspace,
3262 const Cost &CurCost,
3263 const SmallPtrSet<const SCEV *, 16> &CurRegs,
3264 DenseSet<const SCEV *> &VisitedRegs) const {
3267 // - use more aggressive filtering
3268 // - sort the formula so that the most profitable solutions are found first
3269 // - sort the uses too
3271 // - don't compute a cost, and then compare. compare while computing a cost
3273 // - track register sets with SmallBitVector
3275 const LSRUse &LU = Uses[Workspace.size()];
3277 // If this use references any register that's already a part of the
3278 // in-progress solution, consider it a requirement that a formula must
3279 // reference that register in order to be considered. This prunes out
3280 // unprofitable searching.
3281 SmallSetVector<const SCEV *, 4> ReqRegs;
3282 for (SmallPtrSet<const SCEV *, 16>::const_iterator I = CurRegs.begin(),
3283 E = CurRegs.end(); I != E; ++I)
3284 if (LU.Regs.count(*I))
3287 bool AnySatisfiedReqRegs = false;
3288 SmallPtrSet<const SCEV *, 16> NewRegs;
3291 for (SmallVectorImpl<Formula>::const_iterator I = LU.Formulae.begin(),
3292 E = LU.Formulae.end(); I != E; ++I) {
3293 const Formula &F = *I;
3295 // Ignore formulae which do not use any of the required registers.
3296 for (SmallSetVector<const SCEV *, 4>::const_iterator J = ReqRegs.begin(),
3297 JE = ReqRegs.end(); J != JE; ++J) {
3298 const SCEV *Reg = *J;
3299 if ((!F.ScaledReg || F.ScaledReg != Reg) &&
3300 std::find(F.BaseRegs.begin(), F.BaseRegs.end(), Reg) ==
3304 AnySatisfiedReqRegs = true;
3306 // Evaluate the cost of the current formula. If it's already worse than
3307 // the current best, prune the search at that point.
3310 NewCost.RateFormula(F, NewRegs, VisitedRegs, L, LU.Offsets, SE, DT);
3311 if (NewCost < SolutionCost) {
3312 Workspace.push_back(&F);
3313 if (Workspace.size() != Uses.size()) {
3314 SolveRecurse(Solution, SolutionCost, Workspace, NewCost,
3315 NewRegs, VisitedRegs);
3316 if (F.getNumRegs() == 1 && Workspace.size() == 1)
3317 VisitedRegs.insert(F.ScaledReg ? F.ScaledReg : F.BaseRegs[0]);
3319 DEBUG(dbgs() << "New best at "; NewCost.print(dbgs());
3320 dbgs() << ". Regs:";
3321 for (SmallPtrSet<const SCEV *, 16>::const_iterator
3322 I = NewRegs.begin(), E = NewRegs.end(); I != E; ++I)
3323 dbgs() << ' ' << **I;
3326 SolutionCost = NewCost;
3327 Solution = Workspace;
3329 Workspace.pop_back();
3334 if (!EnableRetry && !AnySatisfiedReqRegs)
3337 // If none of the formulae had all of the required registers, relax the
3338 // constraint so that we don't exclude all formulae.
3339 if (!AnySatisfiedReqRegs) {
3340 assert(!ReqRegs.empty() && "Solver failed even without required registers");
3346 /// Solve - Choose one formula from each use. Return the results in the given
3347 /// Solution vector.
3348 void LSRInstance::Solve(SmallVectorImpl<const Formula *> &Solution) const {
3349 SmallVector<const Formula *, 8> Workspace;
3351 SolutionCost.Loose();
3353 SmallPtrSet<const SCEV *, 16> CurRegs;
3354 DenseSet<const SCEV *> VisitedRegs;
3355 Workspace.reserve(Uses.size());
3357 // SolveRecurse does all the work.
3358 SolveRecurse(Solution, SolutionCost, Workspace, CurCost,
3359 CurRegs, VisitedRegs);
3360 if (Solution.empty()) {
3361 DEBUG(dbgs() << "\nNo Satisfactory Solution\n");
3365 // Ok, we've now made all our decisions.
3366 DEBUG(dbgs() << "\n"
3367 "The chosen solution requires "; SolutionCost.print(dbgs());
3369 for (size_t i = 0, e = Uses.size(); i != e; ++i) {
3371 Uses[i].print(dbgs());
3374 Solution[i]->print(dbgs());
3378 assert(Solution.size() == Uses.size() && "Malformed solution!");
3381 /// HoistInsertPosition - Helper for AdjustInsertPositionForExpand. Climb up
3382 /// the dominator tree far as we can go while still being dominated by the
3383 /// input positions. This helps canonicalize the insert position, which
3384 /// encourages sharing.
3385 BasicBlock::iterator
3386 LSRInstance::HoistInsertPosition(BasicBlock::iterator IP,
3387 const SmallVectorImpl<Instruction *> &Inputs)
3390 const Loop *IPLoop = LI.getLoopFor(IP->getParent());
3391 unsigned IPLoopDepth = IPLoop ? IPLoop->getLoopDepth() : 0;
3394 for (DomTreeNode *Rung = DT.getNode(IP->getParent()); ; ) {
3395 if (!Rung) return IP;
3396 Rung = Rung->getIDom();
3397 if (!Rung) return IP;
3398 IDom = Rung->getBlock();
3400 // Don't climb into a loop though.
3401 const Loop *IDomLoop = LI.getLoopFor(IDom);
3402 unsigned IDomDepth = IDomLoop ? IDomLoop->getLoopDepth() : 0;
3403 if (IDomDepth <= IPLoopDepth &&
3404 (IDomDepth != IPLoopDepth || IDomLoop == IPLoop))
3408 bool AllDominate = true;
3409 Instruction *BetterPos = 0;
3410 Instruction *Tentative = IDom->getTerminator();
3411 for (SmallVectorImpl<Instruction *>::const_iterator I = Inputs.begin(),
3412 E = Inputs.end(); I != E; ++I) {
3413 Instruction *Inst = *I;
3414 if (Inst == Tentative || !DT.dominates(Inst, Tentative)) {
3415 AllDominate = false;
3418 // Attempt to find an insert position in the middle of the block,
3419 // instead of at the end, so that it can be used for other expansions.
3420 if (IDom == Inst->getParent() &&
3421 (!BetterPos || DT.dominates(BetterPos, Inst)))
3422 BetterPos = llvm::next(BasicBlock::iterator(Inst));
3435 /// AdjustInsertPositionForExpand - Determine an input position which will be
3436 /// dominated by the operands and which will dominate the result.
3437 BasicBlock::iterator
3438 LSRInstance::AdjustInsertPositionForExpand(BasicBlock::iterator IP,
3440 const LSRUse &LU) const {
3441 // Collect some instructions which must be dominated by the
3442 // expanding replacement. These must be dominated by any operands that
3443 // will be required in the expansion.
3444 SmallVector<Instruction *, 4> Inputs;
3445 if (Instruction *I = dyn_cast<Instruction>(LF.OperandValToReplace))
3446 Inputs.push_back(I);
3447 if (LU.Kind == LSRUse::ICmpZero)
3448 if (Instruction *I =
3449 dyn_cast<Instruction>(cast<ICmpInst>(LF.UserInst)->getOperand(1)))
3450 Inputs.push_back(I);
3451 if (LF.PostIncLoops.count(L)) {
3452 if (LF.isUseFullyOutsideLoop(L))
3453 Inputs.push_back(L->getLoopLatch()->getTerminator());
3455 Inputs.push_back(IVIncInsertPos);
3457 // The expansion must also be dominated by the increment positions of any
3458 // loops it for which it is using post-inc mode.
3459 for (PostIncLoopSet::const_iterator I = LF.PostIncLoops.begin(),
3460 E = LF.PostIncLoops.end(); I != E; ++I) {
3461 const Loop *PIL = *I;
3462 if (PIL == L) continue;
3464 // Be dominated by the loop exit.
3465 SmallVector<BasicBlock *, 4> ExitingBlocks;
3466 PIL->getExitingBlocks(ExitingBlocks);
3467 if (!ExitingBlocks.empty()) {
3468 BasicBlock *BB = ExitingBlocks[0];
3469 for (unsigned i = 1, e = ExitingBlocks.size(); i != e; ++i)
3470 BB = DT.findNearestCommonDominator(BB, ExitingBlocks[i]);
3471 Inputs.push_back(BB->getTerminator());
3475 // Then, climb up the immediate dominator tree as far as we can go while
3476 // still being dominated by the input positions.
3477 IP = HoistInsertPosition(IP, Inputs);
3479 // Don't insert instructions before PHI nodes.
3480 while (isa<PHINode>(IP)) ++IP;
3482 // Ignore landingpad instructions.
3483 while (isa<LandingPadInst>(IP)) ++IP;
3485 // Ignore debug intrinsics.
3486 while (isa<DbgInfoIntrinsic>(IP)) ++IP;
3491 /// Expand - Emit instructions for the leading candidate expression for this
3492 /// LSRUse (this is called "expanding").
3493 Value *LSRInstance::Expand(const LSRFixup &LF,
3495 BasicBlock::iterator IP,
3496 SCEVExpander &Rewriter,
3497 SmallVectorImpl<WeakVH> &DeadInsts) const {
3498 const LSRUse &LU = Uses[LF.LUIdx];
3500 // Determine an input position which will be dominated by the operands and
3501 // which will dominate the result.
3502 IP = AdjustInsertPositionForExpand(IP, LF, LU);
3504 // Inform the Rewriter if we have a post-increment use, so that it can
3505 // perform an advantageous expansion.
3506 Rewriter.setPostInc(LF.PostIncLoops);
3508 // This is the type that the user actually needs.
3509 Type *OpTy = LF.OperandValToReplace->getType();
3510 // This will be the type that we'll initially expand to.
3511 Type *Ty = F.getType();
3513 // No type known; just expand directly to the ultimate type.
3515 else if (SE.getEffectiveSCEVType(Ty) == SE.getEffectiveSCEVType(OpTy))
3516 // Expand directly to the ultimate type if it's the right size.
3518 // This is the type to do integer arithmetic in.
3519 Type *IntTy = SE.getEffectiveSCEVType(Ty);
3521 // Build up a list of operands to add together to form the full base.
3522 SmallVector<const SCEV *, 8> Ops;
3524 // Expand the BaseRegs portion.
3525 for (SmallVectorImpl<const SCEV *>::const_iterator I = F.BaseRegs.begin(),
3526 E = F.BaseRegs.end(); I != E; ++I) {
3527 const SCEV *Reg = *I;
3528 assert(!Reg->isZero() && "Zero allocated in a base register!");
3530 // If we're expanding for a post-inc user, make the post-inc adjustment.
3531 PostIncLoopSet &Loops = const_cast<PostIncLoopSet &>(LF.PostIncLoops);
3532 Reg = TransformForPostIncUse(Denormalize, Reg,
3533 LF.UserInst, LF.OperandValToReplace,
3536 Ops.push_back(SE.getUnknown(Rewriter.expandCodeFor(Reg, 0, IP)));
3539 // Flush the operand list to suppress SCEVExpander hoisting.
3541 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, IP);
3543 Ops.push_back(SE.getUnknown(FullV));
3546 // Expand the ScaledReg portion.
3547 Value *ICmpScaledV = 0;
3548 if (F.AM.Scale != 0) {
3549 const SCEV *ScaledS = F.ScaledReg;
3551 // If we're expanding for a post-inc user, make the post-inc adjustment.
3552 PostIncLoopSet &Loops = const_cast<PostIncLoopSet &>(LF.PostIncLoops);
3553 ScaledS = TransformForPostIncUse(Denormalize, ScaledS,
3554 LF.UserInst, LF.OperandValToReplace,
3557 if (LU.Kind == LSRUse::ICmpZero) {
3558 // An interesting way of "folding" with an icmp is to use a negated
3559 // scale, which we'll implement by inserting it into the other operand
3561 assert(F.AM.Scale == -1 &&
3562 "The only scale supported by ICmpZero uses is -1!");
3563 ICmpScaledV = Rewriter.expandCodeFor(ScaledS, 0, IP);
3565 // Otherwise just expand the scaled register and an explicit scale,
3566 // which is expected to be matched as part of the address.
3567 ScaledS = SE.getUnknown(Rewriter.expandCodeFor(ScaledS, 0, IP));
3568 ScaledS = SE.getMulExpr(ScaledS,
3569 SE.getConstant(ScaledS->getType(), F.AM.Scale));
3570 Ops.push_back(ScaledS);
3572 // Flush the operand list to suppress SCEVExpander hoisting.
3573 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, IP);
3575 Ops.push_back(SE.getUnknown(FullV));
3579 // Expand the GV portion.
3581 Ops.push_back(SE.getUnknown(F.AM.BaseGV));
3583 // Flush the operand list to suppress SCEVExpander hoisting.
3584 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, IP);
3586 Ops.push_back(SE.getUnknown(FullV));
3589 // Expand the immediate portion.
3590 int64_t Offset = (uint64_t)F.AM.BaseOffs + LF.Offset;
3592 if (LU.Kind == LSRUse::ICmpZero) {
3593 // The other interesting way of "folding" with an ICmpZero is to use a
3594 // negated immediate.
3596 ICmpScaledV = ConstantInt::get(IntTy, -(uint64_t)Offset);
3598 Ops.push_back(SE.getUnknown(ICmpScaledV));
3599 ICmpScaledV = ConstantInt::get(IntTy, Offset);
3602 // Just add the immediate values. These again are expected to be matched
3603 // as part of the address.
3604 Ops.push_back(SE.getUnknown(ConstantInt::getSigned(IntTy, Offset)));
3608 // Expand the unfolded offset portion.
3609 int64_t UnfoldedOffset = F.UnfoldedOffset;
3610 if (UnfoldedOffset != 0) {
3611 // Just add the immediate values.
3612 Ops.push_back(SE.getUnknown(ConstantInt::getSigned(IntTy,
3616 // Emit instructions summing all the operands.
3617 const SCEV *FullS = Ops.empty() ?
3618 SE.getConstant(IntTy, 0) :
3620 Value *FullV = Rewriter.expandCodeFor(FullS, Ty, IP);
3622 // We're done expanding now, so reset the rewriter.
3623 Rewriter.clearPostInc();
3625 // An ICmpZero Formula represents an ICmp which we're handling as a
3626 // comparison against zero. Now that we've expanded an expression for that
3627 // form, update the ICmp's other operand.
3628 if (LU.Kind == LSRUse::ICmpZero) {
3629 ICmpInst *CI = cast<ICmpInst>(LF.UserInst);
3630 DeadInsts.push_back(CI->getOperand(1));
3631 assert(!F.AM.BaseGV && "ICmp does not support folding a global value and "
3632 "a scale at the same time!");
3633 if (F.AM.Scale == -1) {
3634 if (ICmpScaledV->getType() != OpTy) {
3636 CastInst::Create(CastInst::getCastOpcode(ICmpScaledV, false,
3638 ICmpScaledV, OpTy, "tmp", CI);
3641 CI->setOperand(1, ICmpScaledV);
3643 assert(F.AM.Scale == 0 &&
3644 "ICmp does not support folding a global value and "
3645 "a scale at the same time!");
3646 Constant *C = ConstantInt::getSigned(SE.getEffectiveSCEVType(OpTy),
3648 if (C->getType() != OpTy)
3649 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
3653 CI->setOperand(1, C);
3660 /// RewriteForPHI - Helper for Rewrite. PHI nodes are special because the use
3661 /// of their operands effectively happens in their predecessor blocks, so the
3662 /// expression may need to be expanded in multiple places.
3663 void LSRInstance::RewriteForPHI(PHINode *PN,
3666 SCEVExpander &Rewriter,
3667 SmallVectorImpl<WeakVH> &DeadInsts,
3669 DenseMap<BasicBlock *, Value *> Inserted;
3670 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
3671 if (PN->getIncomingValue(i) == LF.OperandValToReplace) {
3672 BasicBlock *BB = PN->getIncomingBlock(i);
3674 // If this is a critical edge, split the edge so that we do not insert
3675 // the code on all predecessor/successor paths. We do this unless this
3676 // is the canonical backedge for this loop, which complicates post-inc
3678 if (e != 1 && BB->getTerminator()->getNumSuccessors() > 1 &&
3679 !isa<IndirectBrInst>(BB->getTerminator())) {
3680 BasicBlock *Parent = PN->getParent();
3681 Loop *PNLoop = LI.getLoopFor(Parent);
3682 if (!PNLoop || Parent != PNLoop->getHeader()) {
3683 // Split the critical edge.
3684 BasicBlock *NewBB = 0;
3685 if (!Parent->isLandingPad()) {
3686 NewBB = SplitCriticalEdge(BB, Parent, P,
3687 /*MergeIdenticalEdges=*/true,
3688 /*DontDeleteUselessPhis=*/true);
3690 SmallVector<BasicBlock*, 2> NewBBs;
3691 SplitLandingPadPredecessors(Parent, BB, "", "", P, NewBBs);
3695 // If PN is outside of the loop and BB is in the loop, we want to
3696 // move the block to be immediately before the PHI block, not
3697 // immediately after BB.
3698 if (L->contains(BB) && !L->contains(PN))
3699 NewBB->moveBefore(PN->getParent());
3701 // Splitting the edge can reduce the number of PHI entries we have.
3702 e = PN->getNumIncomingValues();
3704 i = PN->getBasicBlockIndex(BB);
3708 std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> Pair =
3709 Inserted.insert(std::make_pair(BB, static_cast<Value *>(0)));
3711 PN->setIncomingValue(i, Pair.first->second);
3713 Value *FullV = Expand(LF, F, BB->getTerminator(), Rewriter, DeadInsts);
3715 // If this is reuse-by-noop-cast, insert the noop cast.
3716 Type *OpTy = LF.OperandValToReplace->getType();
3717 if (FullV->getType() != OpTy)
3719 CastInst::Create(CastInst::getCastOpcode(FullV, false,
3721 FullV, LF.OperandValToReplace->getType(),
3722 "tmp", BB->getTerminator());
3724 PN->setIncomingValue(i, FullV);
3725 Pair.first->second = FullV;
3730 /// Rewrite - Emit instructions for the leading candidate expression for this
3731 /// LSRUse (this is called "expanding"), and update the UserInst to reference
3732 /// the newly expanded value.
3733 void LSRInstance::Rewrite(const LSRFixup &LF,
3735 SCEVExpander &Rewriter,
3736 SmallVectorImpl<WeakVH> &DeadInsts,
3738 // First, find an insertion point that dominates UserInst. For PHI nodes,
3739 // find the nearest block which dominates all the relevant uses.
3740 if (PHINode *PN = dyn_cast<PHINode>(LF.UserInst)) {
3741 RewriteForPHI(PN, LF, F, Rewriter, DeadInsts, P);
3743 Value *FullV = Expand(LF, F, LF.UserInst, Rewriter, DeadInsts);
3745 // If this is reuse-by-noop-cast, insert the noop cast.
3746 Type *OpTy = LF.OperandValToReplace->getType();
3747 if (FullV->getType() != OpTy) {
3749 CastInst::Create(CastInst::getCastOpcode(FullV, false, OpTy, false),
3750 FullV, OpTy, "tmp", LF.UserInst);
3754 // Update the user. ICmpZero is handled specially here (for now) because
3755 // Expand may have updated one of the operands of the icmp already, and
3756 // its new value may happen to be equal to LF.OperandValToReplace, in
3757 // which case doing replaceUsesOfWith leads to replacing both operands
3758 // with the same value. TODO: Reorganize this.
3759 if (Uses[LF.LUIdx].Kind == LSRUse::ICmpZero)
3760 LF.UserInst->setOperand(0, FullV);
3762 LF.UserInst->replaceUsesOfWith(LF.OperandValToReplace, FullV);
3765 DeadInsts.push_back(LF.OperandValToReplace);
3768 /// ImplementSolution - Rewrite all the fixup locations with new values,
3769 /// following the chosen solution.
3771 LSRInstance::ImplementSolution(const SmallVectorImpl<const Formula *> &Solution,
3773 // Keep track of instructions we may have made dead, so that
3774 // we can remove them after we are done working.
3775 SmallVector<WeakVH, 16> DeadInsts;
3777 SCEVExpander Rewriter(SE, "lsr");
3778 Rewriter.disableCanonicalMode();
3779 Rewriter.enableLSRMode();
3780 Rewriter.setIVIncInsertPos(L, IVIncInsertPos);
3782 // Expand the new value definitions and update the users.
3783 for (SmallVectorImpl<LSRFixup>::const_iterator I = Fixups.begin(),
3784 E = Fixups.end(); I != E; ++I) {
3785 const LSRFixup &Fixup = *I;
3787 Rewrite(Fixup, *Solution[Fixup.LUIdx], Rewriter, DeadInsts, P);
3792 // Clean up after ourselves. This must be done before deleting any
3796 Changed |= DeleteTriviallyDeadInstructions(DeadInsts);
3799 LSRInstance::LSRInstance(const TargetLowering *tli, Loop *l, Pass *P)
3800 : IU(P->getAnalysis<IVUsers>()),
3801 SE(P->getAnalysis<ScalarEvolution>()),
3802 DT(P->getAnalysis<DominatorTree>()),
3803 LI(P->getAnalysis<LoopInfo>()),
3804 TLI(tli), L(l), Changed(false), IVIncInsertPos(0) {
3806 // If LoopSimplify form is not available, stay out of trouble.
3807 if (!L->isLoopSimplifyForm()) return;
3809 // If there's no interesting work to be done, bail early.
3810 if (IU.empty()) return;
3812 DEBUG(dbgs() << "\nLSR on loop ";
3813 WriteAsOperand(dbgs(), L->getHeader(), /*PrintType=*/false);
3816 // First, perform some low-level loop optimizations.
3818 OptimizeLoopTermCond();
3820 // If loop preparation eliminates all interesting IV users, bail.
3821 if (IU.empty()) return;
3823 // Skip nested loops until we can model them better with formulae.
3824 if (!EnableNested && !L->empty()) {
3826 if (EnablePhiElim) {
3827 // Remove any extra phis created by processing inner loops.
3828 SmallVector<WeakVH, 16> DeadInsts;
3829 SCEVExpander Rewriter(SE, "lsr");
3830 Changed |= Rewriter.replaceCongruentIVs(L, &DT, DeadInsts);
3831 Changed |= DeleteTriviallyDeadInstructions(DeadInsts);
3833 DEBUG(dbgs() << "LSR skipping outer loop " << *L << "\n");
3837 // Start collecting data and preparing for the solver.
3838 CollectInterestingTypesAndFactors();
3839 CollectFixupsAndInitialFormulae();
3840 CollectLoopInvariantFixupsAndFormulae();
3842 DEBUG(dbgs() << "LSR found " << Uses.size() << " uses:\n";
3843 print_uses(dbgs()));
3845 // Now use the reuse data to generate a bunch of interesting ways
3846 // to formulate the values needed for the uses.
3847 GenerateAllReuseFormulae();
3849 FilterOutUndesirableDedicatedRegisters();
3850 NarrowSearchSpaceUsingHeuristics();
3852 SmallVector<const Formula *, 8> Solution;
3855 // Release memory that is no longer needed.
3860 if (Solution.empty())
3864 // Formulae should be legal.
3865 for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(),
3866 E = Uses.end(); I != E; ++I) {
3867 const LSRUse &LU = *I;
3868 for (SmallVectorImpl<Formula>::const_iterator J = LU.Formulae.begin(),
3869 JE = LU.Formulae.end(); J != JE; ++J)
3870 assert(isLegalUse(J->AM, LU.MinOffset, LU.MaxOffset,
3871 LU.Kind, LU.AccessTy, TLI) &&
3872 "Illegal formula generated!");
3876 // Now that we've decided what we want, make it so.
3877 ImplementSolution(Solution, P);
3879 if (EnablePhiElim) {
3880 // Remove any extra phis created by processing inner loops.
3881 SmallVector<WeakVH, 16> DeadInsts;
3882 SCEVExpander Rewriter(SE, "lsr");
3883 Changed |= Rewriter.replaceCongruentIVs(L, &DT, DeadInsts);
3884 Changed |= DeleteTriviallyDeadInstructions(DeadInsts);
3888 void LSRInstance::print_factors_and_types(raw_ostream &OS) const {
3889 if (Factors.empty() && Types.empty()) return;
3891 OS << "LSR has identified the following interesting factors and types: ";
3894 for (SmallSetVector<int64_t, 8>::const_iterator
3895 I = Factors.begin(), E = Factors.end(); I != E; ++I) {
3896 if (!First) OS << ", ";
3901 for (SmallSetVector<Type *, 4>::const_iterator
3902 I = Types.begin(), E = Types.end(); I != E; ++I) {
3903 if (!First) OS << ", ";
3905 OS << '(' << **I << ')';
3910 void LSRInstance::print_fixups(raw_ostream &OS) const {
3911 OS << "LSR is examining the following fixup sites:\n";
3912 for (SmallVectorImpl<LSRFixup>::const_iterator I = Fixups.begin(),
3913 E = Fixups.end(); I != E; ++I) {
3920 void LSRInstance::print_uses(raw_ostream &OS) const {
3921 OS << "LSR is examining the following uses:\n";
3922 for (SmallVectorImpl<LSRUse>::const_iterator I = Uses.begin(),
3923 E = Uses.end(); I != E; ++I) {
3924 const LSRUse &LU = *I;
3928 for (SmallVectorImpl<Formula>::const_iterator J = LU.Formulae.begin(),
3929 JE = LU.Formulae.end(); J != JE; ++J) {
3937 void LSRInstance::print(raw_ostream &OS) const {
3938 print_factors_and_types(OS);
3943 void LSRInstance::dump() const {
3944 print(errs()); errs() << '\n';
3949 class LoopStrengthReduce : public LoopPass {
3950 /// TLI - Keep a pointer of a TargetLowering to consult for determining
3951 /// transformation profitability.
3952 const TargetLowering *const TLI;
3955 static char ID; // Pass ID, replacement for typeid
3956 explicit LoopStrengthReduce(const TargetLowering *tli = 0);
3959 bool runOnLoop(Loop *L, LPPassManager &LPM);
3960 void getAnalysisUsage(AnalysisUsage &AU) const;
3965 char LoopStrengthReduce::ID = 0;
3966 INITIALIZE_PASS_BEGIN(LoopStrengthReduce, "loop-reduce",
3967 "Loop Strength Reduction", false, false)
3968 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
3969 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
3970 INITIALIZE_PASS_DEPENDENCY(IVUsers)
3971 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
3972 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
3973 INITIALIZE_PASS_END(LoopStrengthReduce, "loop-reduce",
3974 "Loop Strength Reduction", false, false)
3977 Pass *llvm::createLoopStrengthReducePass(const TargetLowering *TLI) {
3978 return new LoopStrengthReduce(TLI);
3981 LoopStrengthReduce::LoopStrengthReduce(const TargetLowering *tli)
3982 : LoopPass(ID), TLI(tli) {
3983 initializeLoopStrengthReducePass(*PassRegistry::getPassRegistry());
3986 void LoopStrengthReduce::getAnalysisUsage(AnalysisUsage &AU) const {
3987 // We split critical edges, so we change the CFG. However, we do update
3988 // many analyses if they are around.
3989 AU.addPreservedID(LoopSimplifyID);
3991 AU.addRequired<LoopInfo>();
3992 AU.addPreserved<LoopInfo>();
3993 AU.addRequiredID(LoopSimplifyID);
3994 AU.addRequired<DominatorTree>();
3995 AU.addPreserved<DominatorTree>();
3996 AU.addRequired<ScalarEvolution>();
3997 AU.addPreserved<ScalarEvolution>();
3998 // Requiring LoopSimplify a second time here prevents IVUsers from running
3999 // twice, since LoopSimplify was invalidated by running ScalarEvolution.
4000 AU.addRequiredID(LoopSimplifyID);
4001 AU.addRequired<IVUsers>();
4002 AU.addPreserved<IVUsers>();
4005 bool LoopStrengthReduce::runOnLoop(Loop *L, LPPassManager & /*LPM*/) {
4006 bool Changed = false;
4008 // Run the main LSR transformation.
4009 Changed |= LSRInstance(TLI, L, this).getChanged();
4011 // At this point, it is worth checking to see if any recurrence PHIs are also
4012 // dead, so that we can remove them as well.
4013 Changed |= DeleteDeadPHIs(L->getHeader());