1 //===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===//
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 file contains the SplitAnalysis class as well as mutator functions for
11 // live range splitting.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "splitter"
17 #include "VirtRegMap.h"
18 #include "llvm/CodeGen/CalcSpillWeights.h"
19 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineLoopInfo.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Target/TargetInstrInfo.h"
28 #include "llvm/Target/TargetMachine.h"
33 AllowSplit("spiller-splits-edges",
34 cl::desc("Allow critical edge splitting during spilling"));
36 //===----------------------------------------------------------------------===//
38 //===----------------------------------------------------------------------===//
40 SplitAnalysis::SplitAnalysis(const MachineFunction &mf,
41 const LiveIntervals &lis,
42 const MachineLoopInfo &mli)
46 tii_(*mf.getTarget().getInstrInfo()),
49 void SplitAnalysis::clear() {
56 bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) {
57 MachineBasicBlock *T, *F;
58 SmallVector<MachineOperand, 4> Cond;
59 return !tii_.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond);
62 /// analyzeUses - Count instructions, basic blocks, and loops using curli.
63 void SplitAnalysis::analyzeUses() {
64 const MachineRegisterInfo &MRI = mf_.getRegInfo();
65 for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(curli_->reg);
66 MachineInstr *MI = I.skipInstruction();) {
67 if (MI->isDebugValue() || !usingInstrs_.insert(MI))
69 MachineBasicBlock *MBB = MI->getParent();
70 if (usingBlocks_[MBB]++)
72 if (MachineLoop *Loop = loops_.getLoopFor(MBB))
75 DEBUG(dbgs() << " counted "
76 << usingInstrs_.size() << " instrs, "
77 << usingBlocks_.size() << " blocks, "
78 << usingLoops_.size() << " loops.\n");
81 /// removeUse - Update statistics by noting that MI no longer uses curli.
82 void SplitAnalysis::removeUse(const MachineInstr *MI) {
83 if (!usingInstrs_.erase(MI))
86 // Decrement MBB count.
87 const MachineBasicBlock *MBB = MI->getParent();
88 BlockCountMap::iterator bi = usingBlocks_.find(MBB);
89 assert(bi != usingBlocks_.end() && "MBB missing");
90 assert(bi->second && "0 count in map");
93 // No more uses in MBB.
94 usingBlocks_.erase(bi);
96 // Decrement loop count.
97 MachineLoop *Loop = loops_.getLoopFor(MBB);
100 LoopCountMap::iterator li = usingLoops_.find(Loop);
101 assert(li != usingLoops_.end() && "Loop missing");
102 assert(li->second && "0 count in map");
105 // No more blocks in Loop.
106 usingLoops_.erase(li);
109 // Get three sets of basic blocks surrounding a loop: Blocks inside the loop,
110 // predecessor blocks, and exit blocks.
111 void SplitAnalysis::getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks) {
114 // Blocks in the loop.
115 Blocks.Loop.insert(Loop->block_begin(), Loop->block_end());
117 // Predecessor blocks.
118 const MachineBasicBlock *Header = Loop->getHeader();
119 for (MachineBasicBlock::const_pred_iterator I = Header->pred_begin(),
120 E = Header->pred_end(); I != E; ++I)
121 if (!Blocks.Loop.count(*I))
122 Blocks.Preds.insert(*I);
125 for (MachineLoop::block_iterator I = Loop->block_begin(),
126 E = Loop->block_end(); I != E; ++I) {
127 const MachineBasicBlock *MBB = *I;
128 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
129 SE = MBB->succ_end(); SI != SE; ++SI)
130 if (!Blocks.Loop.count(*SI))
131 Blocks.Exits.insert(*SI);
135 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
136 /// and around the Loop.
137 SplitAnalysis::LoopPeripheralUse SplitAnalysis::
138 analyzeLoopPeripheralUse(const SplitAnalysis::LoopBlocks &Blocks) {
139 LoopPeripheralUse use = ContainedInLoop;
140 for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
142 const MachineBasicBlock *MBB = I->first;
143 // Is this a peripheral block?
144 if (use < MultiPeripheral &&
145 (Blocks.Preds.count(MBB) || Blocks.Exits.count(MBB))) {
146 if (I->second > 1) use = MultiPeripheral;
147 else use = SinglePeripheral;
150 // Is it a loop block?
151 if (Blocks.Loop.count(MBB))
153 // It must be an unrelated block.
159 /// getCriticalExits - It may be necessary to partially break critical edges
160 /// leaving the loop if an exit block has phi uses of curli. Collect the exit
161 /// blocks that need special treatment into CriticalExits.
162 void SplitAnalysis::getCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
163 BlockPtrSet &CriticalExits) {
164 CriticalExits.clear();
166 // A critical exit block contains a phi def of curli, and has a predecessor
167 // that is not in the loop nor a loop predecessor.
168 // For such an exit block, the edges carrying the new variable must be moved
169 // to a new pre-exit block.
170 for (BlockPtrSet::iterator I = Blocks.Exits.begin(), E = Blocks.Exits.end();
172 const MachineBasicBlock *Succ = *I;
173 SlotIndex SuccIdx = lis_.getMBBStartIdx(Succ);
174 VNInfo *SuccVNI = curli_->getVNInfoAt(SuccIdx);
175 // This exit may not have curli live in at all. No need to split.
178 // If this is not a PHI def, it is either using a value from before the
179 // loop, or a value defined inside the loop. Both are safe.
180 if (!SuccVNI->isPHIDef() || SuccVNI->def.getBaseIndex() != SuccIdx)
182 // This exit block does have a PHI. Does it also have a predecessor that is
183 // not a loop block or loop predecessor?
184 for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(),
185 PE = Succ->pred_end(); PI != PE; ++PI) {
186 const MachineBasicBlock *Pred = *PI;
187 if (Blocks.Loop.count(Pred) || Blocks.Preds.count(Pred))
189 // This is a critical exit block, and we need to split the exit edge.
190 CriticalExits.insert(Succ);
196 /// canSplitCriticalExits - Return true if it is possible to insert new exit
197 /// blocks before the blocks in CriticalExits.
199 SplitAnalysis::canSplitCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
200 BlockPtrSet &CriticalExits) {
201 // If we don't allow critical edge splitting, require no critical exits.
203 return CriticalExits.empty();
205 for (BlockPtrSet::iterator I = CriticalExits.begin(), E = CriticalExits.end();
207 const MachineBasicBlock *Succ = *I;
208 // We want to insert a new pre-exit MBB before Succ, and change all the
209 // in-loop blocks to branch to the pre-exit instead of Succ.
210 // Check that all the in-loop predecessors can be changed.
211 for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(),
212 PE = Succ->pred_end(); PI != PE; ++PI) {
213 const MachineBasicBlock *Pred = *PI;
214 // The external predecessors won't be altered.
215 if (!Blocks.Loop.count(Pred) && !Blocks.Preds.count(Pred))
217 if (!canAnalyzeBranch(Pred))
221 // If Succ's layout predecessor falls through, that too must be analyzable.
222 // We need to insert the pre-exit block in the gap.
223 MachineFunction::const_iterator MFI = Succ;
224 if (MFI == mf_.begin())
226 if (!canAnalyzeBranch(--MFI))
229 // No problems found.
233 void SplitAnalysis::analyze(const LiveInterval *li) {
239 const MachineLoop *SplitAnalysis::getBestSplitLoop() {
240 assert(curli_ && "Call analyze() before getBestSplitLoop");
241 if (usingLoops_.empty())
244 LoopPtrSet Loops, SecondLoops;
246 BlockPtrSet CriticalExits;
248 // Find first-class and second class candidate loops.
249 // We prefer to split around loops where curli is used outside the periphery.
250 for (LoopCountMap::const_iterator I = usingLoops_.begin(),
251 E = usingLoops_.end(); I != E; ++I) {
252 const MachineLoop *Loop = I->first;
253 getLoopBlocks(Loop, Blocks);
255 // FIXME: We need an SSA updater to properly handle multiple exit blocks.
256 if (Blocks.Exits.size() > 1) {
257 DEBUG(dbgs() << " multiple exits from " << *Loop);
262 switch(analyzeLoopPeripheralUse(Blocks)) {
266 case MultiPeripheral:
269 case ContainedInLoop:
270 DEBUG(dbgs() << " contained in " << *Loop);
272 case SinglePeripheral:
273 DEBUG(dbgs() << " single peripheral use in " << *Loop);
276 // Will it be possible to split around this loop?
277 getCriticalExits(Blocks, CriticalExits);
278 DEBUG(dbgs() << " " << CriticalExits.size() << " critical exits from "
280 if (!canSplitCriticalExits(Blocks, CriticalExits))
282 // This is a possible split.
287 DEBUG(dbgs() << " getBestSplitLoop found " << Loops.size() << " + "
288 << SecondLoops.size() << " candidate loops.\n");
290 // If there are no first class loops available, look at second class loops.
297 // Pick the earliest loop.
298 // FIXME: Are there other heuristics to consider?
299 const MachineLoop *Best = 0;
301 for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E;
303 SlotIndex Idx = lis_.getMBBStartIdx((*I)->getHeader());
304 if (!Best || Idx < BestIdx)
305 Best = *I, BestIdx = Idx;
307 DEBUG(dbgs() << " getBestSplitLoop found " << *Best);
311 /// getMultiUseBlocks - if curli has more than one use in a basic block, it
312 /// may be an advantage to split curli for the duration of the block.
313 bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
314 // If curli is local to one block, there is no point to splitting it.
315 if (usingBlocks_.size() <= 1)
317 // Add blocks with multiple uses.
318 for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
325 // It doesn't pay to split a 2-instr block if it redefines curli.
326 VNInfo *VN1 = curli_->getVNInfoAt(lis_.getMBBStartIdx(I->first));
328 curli_->getVNInfoAt(lis_.getMBBEndIdx(I->first).getPrevIndex());
329 // live-in and live-out with a different value.
330 if (VN1 && VN2 && VN1 != VN2)
334 Blocks.insert(I->first);
336 return !Blocks.empty();
339 //===----------------------------------------------------------------------===//
341 //===----------------------------------------------------------------------===//
343 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
344 SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm,
345 std::vector<LiveInterval*> &intervals)
346 : sa_(sa), lis_(lis), vrm_(vrm),
347 mri_(vrm.getMachineFunction().getRegInfo()),
348 tii_(*vrm.getMachineFunction().getTarget().getInstrInfo()),
349 curli_(sa_.getCurLI()),
350 dupli_(0), openli_(0),
351 intervals_(intervals),
352 firstInterval(intervals_.size())
354 assert(curli_ && "SplitEditor created from empty SplitAnalysis");
356 // Make sure curli_ is assigned a stack slot, so all our intervals get the
357 // same slot as curli_.
358 if (vrm_.getStackSlot(curli_->reg) == VirtRegMap::NO_STACK_SLOT)
359 vrm_.assignVirt2StackSlot(curli_->reg);
363 LiveInterval *SplitEditor::createInterval() {
364 unsigned curli = sa_.getCurLI()->reg;
365 unsigned Reg = mri_.createVirtualRegister(mri_.getRegClass(curli));
366 LiveInterval &Intv = lis_.getOrCreateInterval(Reg);
368 vrm_.assignVirt2StackSlot(Reg, vrm_.getStackSlot(curli));
372 LiveInterval *SplitEditor::getDupLI() {
374 // Create an interval for dupli that is a copy of curli.
375 dupli_ = createInterval();
376 dupli_->Copy(*curli_, &mri_, lis_.getVNInfoAllocator());
381 VNInfo *SplitEditor::mapValue(const VNInfo *curliVNI) {
382 VNInfo *&VNI = valueMap_[curliVNI];
384 VNI = openli_->createValueCopy(curliVNI, lis_.getVNInfoAllocator());
388 /// Insert a COPY instruction curli -> li. Allocate a new value from li
389 /// defined by the COPY. Note that rewrite() will deal with the curli
390 /// register, so this function can be used to copy from any interval - openli,
392 VNInfo *SplitEditor::insertCopy(LiveInterval &LI,
393 MachineBasicBlock &MBB,
394 MachineBasicBlock::iterator I) {
395 MachineInstr *MI = BuildMI(MBB, I, DebugLoc(), tii_.get(TargetOpcode::COPY),
396 LI.reg).addReg(curli_->reg);
397 SlotIndex DefIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
398 return LI.getNextValue(DefIdx, MI, true, lis_.getVNInfoAllocator());
401 /// Create a new virtual register and live interval.
402 void SplitEditor::openIntv() {
403 assert(!openli_ && "Previous LI not closed before openIntv");
404 openli_ = createInterval();
405 intervals_.push_back(openli_);
406 liveThrough_ = false;
409 /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
410 /// not live before Idx, a COPY is not inserted.
411 void SplitEditor::enterIntvBefore(SlotIndex Idx) {
412 assert(openli_ && "openIntv not called before enterIntvBefore");
414 // Copy from curli_ if it is live.
415 if (VNInfo *CurVNI = curli_->getVNInfoAt(Idx.getUseIndex())) {
416 MachineInstr *MI = lis_.getInstructionFromIndex(Idx);
417 assert(MI && "enterIntvBefore called with invalid index");
418 VNInfo *VNI = insertCopy(*openli_, *MI->getParent(), MI);
419 openli_->addRange(LiveRange(VNI->def, Idx.getDefIndex(), VNI));
421 // Make sure CurVNI is properly mapped.
422 VNInfo *&mapVNI = valueMap_[CurVNI];
423 // We dont have SSA update yet, so only one entry per value is allowed.
424 assert(!mapVNI && "enterIntvBefore called more than once for the same value");
427 DEBUG(dbgs() << " enterIntvBefore " << Idx << ": " << *openli_ << '\n');
430 /// enterIntvAtEnd - Enter openli at the end of MBB.
431 /// PhiMBB is a successor inside openli where a PHI value is created.
432 /// Currently, all entries must share the same PhiMBB.
433 void SplitEditor::enterIntvAtEnd(MachineBasicBlock &A, MachineBasicBlock &B) {
434 assert(openli_ && "openIntv not called before enterIntvAtEnd");
436 SlotIndex EndA = lis_.getMBBEndIdx(&A);
437 VNInfo *CurVNIA = curli_->getVNInfoAt(EndA.getPrevIndex());
439 DEBUG(dbgs() << " enterIntvAtEnd, curli not live out of BB#"
440 << A.getNumber() << ".\n");
444 // Add a phi kill value and live range out of A.
445 VNInfo *VNIA = insertCopy(*openli_, A, A.getFirstTerminator());
446 openli_->addRange(LiveRange(VNIA->def, EndA, VNIA));
448 // FIXME: If this is the only entry edge, we don't need the extra PHI value.
449 // FIXME: If there are multiple entry blocks (so not a loop), we need proper
452 // Now look at the start of B.
453 SlotIndex StartB = lis_.getMBBStartIdx(&B);
454 SlotIndex EndB = lis_.getMBBEndIdx(&B);
455 const LiveRange *CurB = curli_->getLiveRangeContaining(StartB);
457 DEBUG(dbgs() << " enterIntvAtEnd: curli not live in to BB#"
458 << B.getNumber() << ".\n");
462 VNInfo *VNIB = openli_->getVNInfoAt(StartB);
464 // Create a phi value.
465 VNIB = openli_->getNextValue(SlotIndex(StartB, true), 0, false,
466 lis_.getVNInfoAllocator());
467 VNIB->setIsPHIDef(true);
468 VNInfo *&mapVNI = valueMap_[CurB->valno];
470 // Multiple copies - must create PHI value.
473 // This is the first copy of dupLR. Mark the mapping.
479 DEBUG(dbgs() << " enterIntvAtEnd: " << *openli_ << '\n');
482 /// useIntv - indicate that all instructions in MBB should use openli.
483 void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
484 useIntv(lis_.getMBBStartIdx(&MBB), lis_.getMBBEndIdx(&MBB));
487 void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
488 assert(openli_ && "openIntv not called before useIntv");
490 // Map the curli values from the interval into openli_
491 LiveInterval::const_iterator B = curli_->begin(), E = curli_->end();
492 LiveInterval::const_iterator I = std::lower_bound(B, E, Start);
496 // I begins before Start, but overlaps.
498 openli_->addRange(LiveRange(Start, std::min(End, I->end),
499 mapValue(I->valno)));
503 // The remaining ranges begin after Start.
504 for (;I != E && I->start < End; ++I)
505 openli_->addRange(LiveRange(I->start, std::min(End, I->end),
506 mapValue(I->valno)));
507 DEBUG(dbgs() << " use [" << Start << ';' << End << "): " << *openli_
511 /// leaveIntvAfter - Leave openli after the instruction at Idx.
512 void SplitEditor::leaveIntvAfter(SlotIndex Idx) {
513 assert(openli_ && "openIntv not called before leaveIntvAfter");
515 const LiveRange *CurLR = curli_->getLiveRangeContaining(Idx.getDefIndex());
516 if (!CurLR || CurLR->end <= Idx.getBoundaryIndex()) {
517 DEBUG(dbgs() << " leaveIntvAfter " << Idx << ": not live\n");
521 // Was this value of curli live through openli?
522 if (!openli_->liveAt(CurLR->valno->def)) {
523 DEBUG(dbgs() << " leaveIntvAfter " << Idx << ": using external value\n");
528 // We are going to insert a back copy, so we must have a dupli_.
529 LiveRange *DupLR = getDupLI()->getLiveRangeContaining(Idx.getDefIndex());
530 assert(DupLR && "dupli not live into black, but curli is?");
532 // Insert the COPY instruction.
533 MachineBasicBlock::iterator I = lis_.getInstructionFromIndex(Idx);
534 MachineInstr *MI = BuildMI(*I->getParent(), llvm::next(I), I->getDebugLoc(),
535 tii_.get(TargetOpcode::COPY), dupli_->reg)
536 .addReg(openli_->reg);
537 SlotIndex CopyIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
538 openli_->addRange(LiveRange(Idx.getDefIndex(), CopyIdx,
539 mapValue(CurLR->valno)));
540 DupLR->valno->def = CopyIdx;
541 DEBUG(dbgs() << " leaveIntvAfter " << Idx << ": " << *openli_ << '\n');
544 /// leaveIntvAtTop - Leave the interval at the top of MBB.
545 /// Currently, only one value can leave the interval.
546 void SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
547 assert(openli_ && "openIntv not called before leaveIntvAtTop");
549 SlotIndex Start = lis_.getMBBStartIdx(&MBB);
550 const LiveRange *CurLR = curli_->getLiveRangeContaining(Start);
552 // Is curli even live-in to MBB?
554 DEBUG(dbgs() << " leaveIntvAtTop at " << Start << ": not live\n");
558 // Is curli defined by PHI at the beginning of MBB?
559 bool isPHIDef = CurLR->valno->isPHIDef() &&
560 CurLR->valno->def.getBaseIndex() == Start;
562 // If MBB is using a value of curli that was defined outside the openli range,
563 // we don't want to copy it back here.
564 if (!isPHIDef && !openli_->liveAt(CurLR->valno->def)) {
565 DEBUG(dbgs() << " leaveIntvAtTop at " << Start
566 << ": using external value\n");
571 // We are going to insert a back copy, so we must have a dupli_.
572 LiveRange *DupLR = getDupLI()->getLiveRangeContaining(Start);
573 assert(DupLR && "dupli not live into black, but curli is?");
575 // Insert the COPY instruction.
576 MachineInstr *MI = BuildMI(MBB, MBB.begin(), DebugLoc(),
577 tii_.get(TargetOpcode::COPY), dupli_->reg)
578 .addReg(openli_->reg);
579 SlotIndex Idx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
581 // Adjust dupli and openli values.
583 // dupli was already a PHI on entry to MBB. Simply insert an openli PHI,
584 // and shift the dupli def down to the COPY.
585 VNInfo *VNI = openli_->getNextValue(SlotIndex(Start, true), 0, false,
586 lis_.getVNInfoAllocator());
587 VNI->setIsPHIDef(true);
588 openli_->addRange(LiveRange(VNI->def, Idx, VNI));
590 dupli_->removeRange(Start, Idx);
591 DupLR->valno->def = Idx;
592 DupLR->valno->setIsPHIDef(false);
594 // The dupli value was defined somewhere inside the openli range.
595 DEBUG(dbgs() << " leaveIntvAtTop source value defined at "
596 << DupLR->valno->def << "\n");
597 // FIXME: We may not need a PHI here if all predecessors have the same
599 VNInfo *VNI = openli_->getNextValue(SlotIndex(Start, true), 0, false,
600 lis_.getVNInfoAllocator());
601 VNI->setIsPHIDef(true);
602 openli_->addRange(LiveRange(VNI->def, Idx, VNI));
604 // FIXME: What if DupLR->valno is used by multiple exits? SSA Update.
606 // closeIntv is going to remove the superfluous live ranges.
607 DupLR->valno->def = Idx;
608 DupLR->valno->setIsPHIDef(false);
611 DEBUG(dbgs() << " leaveIntvAtTop at " << Idx << ": " << *openli_ << '\n');
614 /// closeIntv - Indicate that we are done editing the currently open
615 /// LiveInterval, and ranges can be trimmed.
616 void SplitEditor::closeIntv() {
617 assert(openli_ && "openIntv not called before closeIntv");
619 DEBUG(dbgs() << " closeIntv cleaning up\n");
620 DEBUG(dbgs() << " open " << *openli_ << '\n');
623 DEBUG(dbgs() << " value live through region, leaving dupli as is.\n");
625 // live out with copies inserted, or killed by region. Either way we need to
626 // remove the overlapping region from dupli.
628 for (LiveInterval::iterator I = openli_->begin(), E = openli_->end();
630 dupli_->removeRange(I->start, I->end);
632 // FIXME: A block branching to the entry block may also branch elsewhere
633 // curli is live. We need both openli and curli to be live in that case.
634 DEBUG(dbgs() << " dup2 " << *dupli_ << '\n');
640 /// rewrite - after all the new live ranges have been created, rewrite
641 /// instructions using curli to use the new intervals.
642 void SplitEditor::rewrite() {
643 assert(!openli_ && "Previous LI not closed before rewrite");
644 const LiveInterval *curli = sa_.getCurLI();
645 for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(curli->reg),
646 RE = mri_.reg_end(); RI != RE;) {
647 MachineOperand &MO = RI.getOperand();
648 MachineInstr *MI = MO.getParent();
650 if (MI->isDebugValue()) {
651 DEBUG(dbgs() << "Zapping " << *MI);
652 // FIXME: We can do much better with debug values.
656 SlotIndex Idx = lis_.getInstructionIndex(MI);
657 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
658 LiveInterval *LI = dupli_;
659 for (unsigned i = firstInterval, e = intervals_.size(); i != e; ++i) {
660 LiveInterval *testli = intervals_[i];
661 if (testli->liveAt(Idx)) {
669 DEBUG(dbgs() << " rewrite " << Idx << '\t' << *MI);
673 // dupli_ goes in last, after rewriting.
675 if (dupli_->empty()) {
676 DEBUG(dbgs() << " dupli became empty?\n");
677 lis_.removeInterval(dupli_->reg);
680 dupli_->RenumberValues(lis_);
681 intervals_.push_back(dupli_);
685 // Calculate spill weight and allocation hints for new intervals.
686 VirtRegAuxInfo vrai(vrm_.getMachineFunction(), lis_, sa_.loops_);
687 for (unsigned i = firstInterval, e = intervals_.size(); i != e; ++i) {
688 LiveInterval &li = *intervals_[i];
689 vrai.CalculateRegClass(li.reg);
690 vrai.CalculateWeightAndHint(li);
691 DEBUG(dbgs() << " new interval " << mri_.getRegClass(li.reg)->getName()
692 << ":" << li << '\n');
697 //===----------------------------------------------------------------------===//
699 //===----------------------------------------------------------------------===//
701 bool SplitEditor::splitAroundLoop(const MachineLoop *Loop) {
702 SplitAnalysis::LoopBlocks Blocks;
703 sa_.getLoopBlocks(Loop, Blocks);
705 // Break critical edges as needed.
706 SplitAnalysis::BlockPtrSet CriticalExits;
707 sa_.getCriticalExits(Blocks, CriticalExits);
708 assert(CriticalExits.empty() && "Cannot break critical exits yet");
710 // Create new live interval for the loop.
713 // Insert copies in the predecessors.
714 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Preds.begin(),
715 E = Blocks.Preds.end(); I != E; ++I) {
716 MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
717 enterIntvAtEnd(MBB, *Loop->getHeader());
720 // Switch all loop blocks.
721 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Loop.begin(),
722 E = Blocks.Loop.end(); I != E; ++I)
725 // Insert back copies in the exit blocks.
726 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Exits.begin(),
727 E = Blocks.Exits.end(); I != E; ++I) {
728 MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
739 //===----------------------------------------------------------------------===//
740 // Single Block Splitting
741 //===----------------------------------------------------------------------===//
743 /// splitSingleBlocks - Split curli into a separate live interval inside each
744 /// basic block in Blocks. Return true if curli has been completely replaced,
745 /// false if curli is still intact, and needs to be spilled or split further.
746 bool SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
747 DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n");
748 // Determine the first and last instruction using curli in each block.
749 typedef std::pair<SlotIndex,SlotIndex> IndexPair;
750 typedef DenseMap<const MachineBasicBlock*,IndexPair> IndexPairMap;
751 IndexPairMap MBBRange;
752 for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(),
753 E = sa_.usingInstrs_.end(); I != E; ++I) {
754 const MachineBasicBlock *MBB = (*I)->getParent();
755 if (!Blocks.count(MBB))
757 SlotIndex Idx = lis_.getInstructionIndex(*I);
758 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '\t' << Idx << '\t' << **I);
759 IndexPair &IP = MBBRange[MBB];
760 if (!IP.first.isValid() || Idx < IP.first)
762 if (!IP.second.isValid() || Idx > IP.second)
766 // Create a new interval for each block.
767 for (SplitAnalysis::BlockPtrSet::const_iterator I = Blocks.begin(),
768 E = Blocks.end(); I != E; ++I) {
769 IndexPair &IP = MBBRange[*I];
770 DEBUG(dbgs() << " splitting for BB#" << (*I)->getNumber() << ": ["
771 << IP.first << ';' << IP.second << ")\n");
772 assert(IP.first.isValid() && IP.second.isValid());
775 enterIntvBefore(IP.first);
776 useIntv(IP.first.getBaseIndex(), IP.second.getBoundaryIndex());
777 leaveIntvAfter(IP.second);
785 //===----------------------------------------------------------------------===//
786 // Sub Block Splitting
787 //===----------------------------------------------------------------------===//
789 /// getBlockForInsideSplit - If curli is contained inside a single basic block,
790 /// and it wou pay to subdivide the interval inside that block, return it.
791 /// Otherwise return NULL. The returned block can be passed to
792 /// SplitEditor::splitInsideBlock.
793 const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() {
794 // The interval must be exclusive to one block.
795 if (usingBlocks_.size() != 1)
797 // Don't to this for less than 4 instructions. We want to be sure that
798 // splitting actually reduces the instruction count per interval.
799 if (usingInstrs_.size() < 4)
801 return usingBlocks_.begin()->first;
804 /// splitInsideBlock - Split curli into multiple intervals inside MBB. Return
805 /// true if curli has been completely replaced, false if curli is still
806 /// intact, and needs to be spilled or split further.
807 bool SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) {
808 SmallVector<SlotIndex, 32> Uses;
809 Uses.reserve(sa_.usingInstrs_.size());
810 for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(),
811 E = sa_.usingInstrs_.end(); I != E; ++I)
812 if ((*I)->getParent() == MBB)
813 Uses.push_back(lis_.getInstructionIndex(*I));
814 DEBUG(dbgs() << " splitInsideBlock BB#" << MBB->getNumber() << " for "
815 << Uses.size() << " instructions.\n");
816 assert(Uses.size() >= 3 && "Need at least 3 instructions");
817 array_pod_sort(Uses.begin(), Uses.end());
819 // Simple algorithm: Find the largest gap between uses as determined by slot
820 // indices. Create new intervals for instructions before the gap and after the
822 unsigned bestPos = 0;
824 DEBUG(dbgs() << " dist (" << Uses[0]);
825 for (unsigned i = 1, e = Uses.size(); i != e; ++i) {
826 int g = Uses[i-1].distance(Uses[i]);
827 DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]);
829 bestPos = i, bestGap = g;
831 DEBUG(dbgs() << "), best: -" << bestGap << "-\n");
833 // bestPos points to the first use after the best gap.
834 assert(bestPos > 0 && "Invalid gap");
836 // FIXME: Don't create intervals for low densities.
838 // First interval before the gap. Don't create single-instr intervals.
841 enterIntvBefore(Uses.front());
842 useIntv(Uses.front().getBaseIndex(), Uses[bestPos-1].getBoundaryIndex());
843 leaveIntvAfter(Uses[bestPos-1]);
847 // Second interval after the gap.
848 if (bestPos < Uses.size()-1) {
850 enterIntvBefore(Uses[bestPos]);
851 useIntv(Uses[bestPos].getBaseIndex(), Uses.back().getBoundaryIndex());
852 leaveIntvAfter(Uses.back());