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 "regalloc"
17 #include "LiveRangeEdit.h"
18 #include "VirtRegMap.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/CodeGen/CalcSpillWeights.h"
21 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
22 #include "llvm/CodeGen/MachineDominators.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Target/TargetInstrInfo.h"
29 #include "llvm/Target/TargetMachine.h"
34 AllowSplit("spiller-splits-edges",
35 cl::desc("Allow critical edge splitting during spilling"));
37 STATISTIC(NumFinished, "Number of splits finished");
38 STATISTIC(NumSimple, "Number of splits that were simple");
40 //===----------------------------------------------------------------------===//
42 //===----------------------------------------------------------------------===//
44 SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm,
45 const LiveIntervals &lis,
46 const MachineLoopInfo &mli)
47 : MF(vrm.getMachineFunction()),
51 TII(*MF.getTarget().getInstrInfo()),
54 void SplitAnalysis::clear() {
62 bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) {
63 MachineBasicBlock *T, *F;
64 SmallVector<MachineOperand, 4> Cond;
65 return !TII.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond);
68 /// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
69 void SplitAnalysis::analyzeUses() {
70 const MachineRegisterInfo &MRI = MF.getRegInfo();
71 for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(CurLI->reg),
72 E = MRI.reg_end(); I != E; ++I) {
73 MachineOperand &MO = I.getOperand();
74 if (MO.isUse() && MO.isUndef())
76 MachineInstr *MI = MO.getParent();
77 if (MI->isDebugValue() || !UsingInstrs.insert(MI))
79 UseSlots.push_back(LIS.getInstructionIndex(MI).getDefIndex());
80 MachineBasicBlock *MBB = MI->getParent();
83 array_pod_sort(UseSlots.begin(), UseSlots.end());
85 // Compute per-live block info.
86 if (!calcLiveBlockInfo()) {
87 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
88 // I am looking at you, SimpleRegisterCoalescing!
89 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
90 const_cast<LiveIntervals&>(LIS)
91 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
93 bool fixed = calcLiveBlockInfo();
95 assert(fixed && "Couldn't fix broken live interval");
98 DEBUG(dbgs() << " counted "
99 << UsingInstrs.size() << " instrs, "
100 << UsingBlocks.size() << " blocks.\n");
103 /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
104 /// where CurLI is live.
105 bool SplitAnalysis::calcLiveBlockInfo() {
109 LiveInterval::const_iterator LVI = CurLI->begin();
110 LiveInterval::const_iterator LVE = CurLI->end();
112 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
113 UseI = UseSlots.begin();
114 UseE = UseSlots.end();
116 // Loop over basic blocks where CurLI is live.
117 MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
121 tie(BI.Start, BI.Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
123 // The last split point is the latest possible insertion point that dominates
124 // all successor blocks. If interference reaches LastSplitPoint, it is not
125 // possible to insert a split or reload that makes CurLI live in the
127 MachineBasicBlock::iterator LSP = LIS.getLastSplitPoint(*CurLI, BI.MBB);
128 if (LSP == BI.MBB->end())
129 BI.LastSplitPoint = BI.Stop;
131 BI.LastSplitPoint = LIS.getInstructionIndex(LSP);
133 // LVI is the first live segment overlapping MBB.
134 BI.LiveIn = LVI->start <= BI.Start;
138 // Find the first and last uses in the block.
139 BI.Uses = hasUses(MFI);
140 if (BI.Uses && UseI != UseE) {
142 assert(BI.FirstUse >= BI.Start);
144 while (UseI != UseE && *UseI < BI.Stop);
145 BI.LastUse = UseI[-1];
146 assert(BI.LastUse < BI.Stop);
149 // Look for gaps in the live range.
152 while (LVI->end < BI.Stop) {
153 SlotIndex LastStop = LVI->end;
154 if (++LVI == LVE || LVI->start >= BI.Stop) {
159 if (LastStop < LVI->start) {
166 // Don't set LiveThrough when the block has a gap.
167 BI.LiveThrough = !hasGap && BI.LiveIn && BI.LiveOut;
168 LiveBlocks.push_back(BI);
170 // FIXME: This should never happen. The live range stops or starts without a
171 // corresponding use. An earlier pass did something wrong.
172 if (!BI.LiveThrough && !BI.Uses)
175 // LVI is now at LVE or LVI->end >= Stop.
179 // Live segment ends exactly at Stop. Move to the next segment.
180 if (LVI->end == BI.Stop && ++LVI == LVE)
183 // Pick the next basic block.
184 if (LVI->start < BI.Stop)
187 MFI = LIS.getMBBFromIndex(LVI->start);
192 bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
193 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
194 const LiveInterval &Orig = LIS.getInterval(OrigReg);
195 assert(!Orig.empty() && "Splitting empty interval?");
196 LiveInterval::const_iterator I = Orig.find(Idx);
198 // Range containing Idx should begin at Idx.
199 if (I != Orig.end() && I->start <= Idx)
200 return I->start == Idx;
202 // Range does not contain Idx, previous must end at Idx.
203 return I != Orig.begin() && (--I)->end == Idx;
206 void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const {
207 for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) {
208 unsigned count = UsingBlocks.lookup(*I);
209 OS << " BB#" << (*I)->getNumber();
211 OS << '(' << count << ')';
215 void SplitAnalysis::analyze(const LiveInterval *li) {
222 //===----------------------------------------------------------------------===//
224 //===----------------------------------------------------------------------===//
226 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
227 SplitEditor::SplitEditor(SplitAnalysis &sa,
230 MachineDominatorTree &mdt)
231 : SA(sa), LIS(lis), VRM(vrm),
232 MRI(vrm.getMachineFunction().getRegInfo()),
234 TII(*vrm.getMachineFunction().getTarget().getInstrInfo()),
235 TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
241 void SplitEditor::reset(LiveRangeEdit &lre) {
247 // We don't need to clear LiveOutCache, only LiveOutSeen entries are read.
250 // We don't need an AliasAnalysis since we will only be performing
251 // cheap-as-a-copy remats anyway.
252 Edit->anyRematerializable(LIS, TII, 0);
255 void SplitEditor::dump() const {
256 if (RegAssign.empty()) {
257 dbgs() << " empty\n";
261 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
262 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
266 VNInfo *SplitEditor::defValue(unsigned RegIdx,
267 const VNInfo *ParentVNI,
269 assert(ParentVNI && "Mapping NULL value");
270 assert(Idx.isValid() && "Invalid SlotIndex");
271 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
272 LiveInterval *LI = Edit->get(RegIdx);
274 // Create a new value.
275 VNInfo *VNI = LI->getNextValue(Idx, 0, LIS.getVNInfoAllocator());
277 // Use insert for lookup, so we can add missing values with a second lookup.
278 std::pair<ValueMap::iterator, bool> InsP =
279 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), VNI));
281 // This was the first time (RegIdx, ParentVNI) was mapped.
282 // Keep it as a simple def without any liveness.
286 // If the previous value was a simple mapping, add liveness for it now.
287 if (VNInfo *OldVNI = InsP.first->second) {
288 SlotIndex Def = OldVNI->def;
289 LI->addRange(LiveRange(Def, Def.getNextSlot(), OldVNI));
290 // No longer a simple mapping.
291 InsP.first->second = 0;
294 // This is a complex mapping, add liveness for VNI
295 SlotIndex Def = VNI->def;
296 LI->addRange(LiveRange(Def, Def.getNextSlot(), VNI));
301 void SplitEditor::markComplexMapped(unsigned RegIdx, const VNInfo *ParentVNI) {
302 assert(ParentVNI && "Mapping NULL value");
303 VNInfo *&VNI = Values[std::make_pair(RegIdx, ParentVNI->id)];
305 // ParentVNI was either unmapped or already complex mapped. Either way.
309 // This was previously a single mapping. Make sure the old def is represented
310 // by a trivial live range.
311 SlotIndex Def = VNI->def;
312 Edit->get(RegIdx)->addRange(LiveRange(Def, Def.getNextSlot(), VNI));
316 // extendRange - Extend the live range to reach Idx.
317 // Potentially create phi-def values.
318 void SplitEditor::extendRange(unsigned RegIdx, SlotIndex Idx) {
319 assert(Idx.isValid() && "Invalid SlotIndex");
320 MachineBasicBlock *IdxMBB = LIS.getMBBFromIndex(Idx);
321 assert(IdxMBB && "No MBB at Idx");
322 LiveInterval *LI = Edit->get(RegIdx);
324 // Is there a def in the same MBB we can extend?
325 if (LI->extendInBlock(LIS.getMBBStartIdx(IdxMBB), Idx))
328 // Now for the fun part. We know that ParentVNI potentially has multiple defs,
329 // and we may need to create even more phi-defs to preserve VNInfo SSA form.
330 // Perform a search for all predecessor blocks where we know the dominating
331 // VNInfo. Insert phi-def VNInfos along the path back to IdxMBB.
333 // Initialize the live-out cache the first time it is needed.
334 if (LiveOutSeen.empty()) {
335 unsigned N = VRM.getMachineFunction().getNumBlockIDs();
336 LiveOutSeen.resize(N);
337 LiveOutCache.resize(N);
340 // Blocks where LI should be live-in.
341 SmallVector<MachineDomTreeNode*, 16> LiveIn;
342 LiveIn.push_back(MDT[IdxMBB]);
344 // Remember if we have seen more than one value.
345 bool UniqueVNI = true;
348 // Using LiveOutCache as a visited set, perform a BFS for all reaching defs.
349 for (unsigned i = 0; i != LiveIn.size(); ++i) {
350 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
351 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
352 PE = MBB->pred_end(); PI != PE; ++PI) {
353 MachineBasicBlock *Pred = *PI;
354 LiveOutPair &LOP = LiveOutCache[Pred];
356 // Is this a known live-out block?
357 if (LiveOutSeen.test(Pred->getNumber())) {
358 if (VNInfo *VNI = LOP.first) {
359 if (IdxVNI && IdxVNI != VNI)
366 // First time. LOP is garbage and must be cleared below.
367 LiveOutSeen.set(Pred->getNumber());
369 // Does Pred provide a live-out value?
370 SlotIndex Start, Last;
371 tie(Start, Last) = LIS.getSlotIndexes()->getMBBRange(Pred);
372 Last = Last.getPrevSlot();
373 VNInfo *VNI = LI->extendInBlock(Start, Last);
376 LOP.second = MDT[LIS.getMBBFromIndex(VNI->def)];
377 if (IdxVNI && IdxVNI != VNI)
384 // No, we need a live-in value for Pred as well
386 LiveIn.push_back(MDT[Pred]);
388 UniqueVNI = false; // Loopback to IdxMBB, ask updateSSA() for help.
392 // We may need to add phi-def values to preserve the SSA form.
394 LiveOutPair LOP(IdxVNI, MDT[LIS.getMBBFromIndex(IdxVNI->def)]);
395 // Update LiveOutCache, but skip IdxMBB at LiveIn[0].
396 for (unsigned i = 1, e = LiveIn.size(); i != e; ++i)
397 LiveOutCache[LiveIn[i]->getBlock()] = LOP;
399 IdxVNI = updateSSA(RegIdx, LiveIn, Idx, IdxMBB);
401 // Since we went through the trouble of a full BFS visiting all reaching defs,
402 // the values in LiveIn are now accurate. No more phi-defs are needed
403 // for these blocks, so we can color the live ranges.
404 for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) {
405 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
406 SlotIndex Start = LIS.getMBBStartIdx(MBB);
407 VNInfo *VNI = LiveOutCache[MBB].first;
409 // Anything in LiveIn other than IdxMBB is live-through.
410 // In IdxMBB, we should stop at Idx unless the same value is live-out.
411 if (MBB == IdxMBB && IdxVNI != VNI)
412 LI->addRange(LiveRange(Start, Idx.getNextSlot(), IdxVNI));
414 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
418 VNInfo *SplitEditor::updateSSA(unsigned RegIdx,
419 SmallVectorImpl<MachineDomTreeNode*> &LiveIn,
421 const MachineBasicBlock *IdxMBB) {
422 // This is essentially the same iterative algorithm that SSAUpdater uses,
423 // except we already have a dominator tree, so we don't have to recompute it.
424 LiveInterval *LI = Edit->get(RegIdx);
429 // Propagate live-out values down the dominator tree, inserting phi-defs
430 // when necessary. Since LiveIn was created by a BFS, going backwards makes
431 // it more likely for us to visit immediate dominators before their
433 for (unsigned i = LiveIn.size(); i; --i) {
434 MachineDomTreeNode *Node = LiveIn[i-1];
435 MachineBasicBlock *MBB = Node->getBlock();
436 MachineDomTreeNode *IDom = Node->getIDom();
437 LiveOutPair IDomValue;
439 // We need a live-in value to a block with no immediate dominator?
440 // This is probably an unreachable block that has survived somehow.
441 bool needPHI = !IDom || !LiveOutSeen.test(IDom->getBlock()->getNumber());
443 // IDom dominates all of our predecessors, but it may not be the immediate
444 // dominator. Check if any of them have live-out values that are properly
445 // dominated by IDom. If so, we need a phi-def here.
447 IDomValue = LiveOutCache[IDom->getBlock()];
448 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
449 PE = MBB->pred_end(); PI != PE; ++PI) {
450 LiveOutPair Value = LiveOutCache[*PI];
451 if (!Value.first || Value.first == IDomValue.first)
453 // This predecessor is carrying something other than IDomValue.
454 // It could be because IDomValue hasn't propagated yet, or it could be
455 // because MBB is in the dominance frontier of that value.
456 if (MDT.dominates(IDom, Value.second)) {
463 // Create a phi-def if required.
466 SlotIndex Start = LIS.getMBBStartIdx(MBB);
467 VNInfo *VNI = LI->getNextValue(Start, 0, LIS.getVNInfoAllocator());
468 VNI->setIsPHIDef(true);
469 // We no longer need LI to be live-in.
470 LiveIn.erase(LiveIn.begin()+(i-1));
471 // Blocks in LiveIn are either IdxMBB, or have a value live-through.
474 // Check if we need to update live-out info.
475 LiveOutPair &LOP = LiveOutCache[MBB];
476 if (LOP.second == Node || !LiveOutSeen.test(MBB->getNumber())) {
477 // We already have a live-out defined in MBB, so this must be IdxMBB.
478 assert(MBB == IdxMBB && "Adding phi-def to known live-out");
479 LI->addRange(LiveRange(Start, Idx.getNextSlot(), VNI));
481 // This phi-def is also live-out, so color the whole block.
482 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
483 LOP = LiveOutPair(VNI, Node);
485 } else if (IDomValue.first) {
486 // No phi-def here. Remember incoming value for IdxMBB.
488 IdxVNI = IDomValue.first;
489 // IdxMBB need not be live-out.
490 if (!LiveOutSeen.test(MBB->getNumber()))
493 assert(LiveOutSeen.test(MBB->getNumber()) && "Expected live-out block");
494 // Propagate IDomValue if needed:
495 // MBB is live-out and doesn't define its own value.
496 LiveOutPair &LOP = LiveOutCache[MBB];
497 if (LOP.second != Node && LOP.first != IDomValue.first) {
505 assert(IdxVNI && "Didn't find value for Idx");
509 VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
512 MachineBasicBlock &MBB,
513 MachineBasicBlock::iterator I) {
514 MachineInstr *CopyMI = 0;
516 LiveInterval *LI = Edit->get(RegIdx);
518 // Attempt cheap-as-a-copy rematerialization.
519 LiveRangeEdit::Remat RM(ParentVNI);
520 if (Edit->canRematerializeAt(RM, UseIdx, true, LIS)) {
521 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, LIS, TII, TRI);
523 // Can't remat, just insert a copy from parent.
524 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
525 .addReg(Edit->getReg());
526 Def = LIS.InsertMachineInstrInMaps(CopyMI).getDefIndex();
529 // Define the value in Reg.
530 VNInfo *VNI = defValue(RegIdx, ParentVNI, Def);
531 VNI->setCopy(CopyMI);
535 /// Create a new virtual register and live interval.
536 void SplitEditor::openIntv() {
537 assert(!OpenIdx && "Previous LI not closed before openIntv");
539 // Create the complement as index 0.
541 Edit->create(MRI, LIS, VRM);
543 // Create the open interval.
544 OpenIdx = Edit->size();
545 Edit->create(MRI, LIS, VRM);
548 SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
549 assert(OpenIdx && "openIntv not called before enterIntvBefore");
550 DEBUG(dbgs() << " enterIntvBefore " << Idx);
551 Idx = Idx.getBaseIndex();
552 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
554 DEBUG(dbgs() << ": not live\n");
557 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
558 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
559 assert(MI && "enterIntvBefore called with invalid index");
561 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
565 SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
566 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
567 SlotIndex End = LIS.getMBBEndIdx(&MBB);
568 SlotIndex Last = End.getPrevSlot();
569 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
570 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
572 DEBUG(dbgs() << ": not live\n");
575 DEBUG(dbgs() << ": valno " << ParentVNI->id);
576 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
577 LIS.getLastSplitPoint(Edit->getParent(), &MBB));
578 RegAssign.insert(VNI->def, End, OpenIdx);
583 /// useIntv - indicate that all instructions in MBB should use OpenLI.
584 void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
585 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
588 void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
589 assert(OpenIdx && "openIntv not called before useIntv");
590 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
591 RegAssign.insert(Start, End, OpenIdx);
595 SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
596 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
597 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
599 // The interval must be live beyond the instruction at Idx.
600 Idx = Idx.getBoundaryIndex();
601 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
603 DEBUG(dbgs() << ": not live\n");
604 return Idx.getNextSlot();
606 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
608 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
609 assert(MI && "No instruction at index");
610 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(),
611 llvm::next(MachineBasicBlock::iterator(MI)));
615 SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
616 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
617 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
619 // The interval must be live into the instruction at Idx.
620 Idx = Idx.getBoundaryIndex();
621 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
623 DEBUG(dbgs() << ": not live\n");
624 return Idx.getNextSlot();
626 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
628 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
629 assert(MI && "No instruction at index");
630 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
634 SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
635 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
636 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
637 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
639 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
641 DEBUG(dbgs() << ": not live\n");
645 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
646 MBB.SkipPHIsAndLabels(MBB.begin()));
647 RegAssign.insert(Start, VNI->def, OpenIdx);
652 void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
653 assert(OpenIdx && "openIntv not called before overlapIntv");
654 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
655 assert(ParentVNI == Edit->getParent().getVNInfoAt(End.getPrevSlot()) &&
656 "Parent changes value in extended range");
657 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
658 "Range cannot span basic blocks");
660 // The complement interval will be extended as needed by extendRange().
661 markComplexMapped(0, ParentVNI);
662 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
663 RegAssign.insert(Start, End, OpenIdx);
667 /// closeIntv - Indicate that we are done editing the currently open
668 /// LiveInterval, and ranges can be trimmed.
669 void SplitEditor::closeIntv() {
670 assert(OpenIdx && "openIntv not called before closeIntv");
674 /// transferSimpleValues - Transfer all simply defined values to the new live
676 /// Values that were rematerialized or that have multiple defs are left alone.
677 bool SplitEditor::transferSimpleValues() {
678 bool Skipped = false;
679 RegAssignMap::const_iterator AssignI = RegAssign.begin();
680 for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(),
681 ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) {
682 DEBUG(dbgs() << " blit " << *ParentI << ':');
683 VNInfo *ParentVNI = ParentI->valno;
684 // RegAssign has holes where RegIdx 0 should be used.
685 SlotIndex Start = ParentI->start;
686 AssignI.advanceTo(Start);
689 SlotIndex End = ParentI->end;
690 if (!AssignI.valid()) {
692 } else if (AssignI.start() <= Start) {
693 RegIdx = AssignI.value();
694 if (AssignI.stop() < End) {
695 End = AssignI.stop();
700 End = std::min(End, AssignI.start());
702 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
703 if (VNInfo *VNI = Values.lookup(std::make_pair(RegIdx, ParentVNI->id))) {
704 DEBUG(dbgs() << ':' << VNI->id);
705 Edit->get(RegIdx)->addRange(LiveRange(Start, End, VNI));
709 } while (Start != ParentI->end);
710 DEBUG(dbgs() << '\n');
715 void SplitEditor::extendPHIKillRanges() {
716 // Extend live ranges to be live-out for successor PHI values.
717 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
718 E = Edit->getParent().vni_end(); I != E; ++I) {
719 const VNInfo *PHIVNI = *I;
720 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
722 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
723 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
724 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
725 PE = MBB->pred_end(); PI != PE; ++PI) {
726 SlotIndex End = LIS.getMBBEndIdx(*PI).getPrevSlot();
727 // The predecessor may not have a live-out value. That is OK, like an
728 // undef PHI operand.
729 if (Edit->getParent().liveAt(End)) {
730 assert(RegAssign.lookup(End) == RegIdx &&
731 "Different register assignment in phi predecessor");
732 extendRange(RegIdx, End);
738 /// rewriteAssigned - Rewrite all uses of Edit->getReg().
739 void SplitEditor::rewriteAssigned(bool ExtendRanges) {
740 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
741 RE = MRI.reg_end(); RI != RE;) {
742 MachineOperand &MO = RI.getOperand();
743 MachineInstr *MI = MO.getParent();
745 // LiveDebugVariables should have handled all DBG_VALUE instructions.
746 if (MI->isDebugValue()) {
747 DEBUG(dbgs() << "Zapping " << *MI);
752 // <undef> operands don't really read the register, so just assign them to
754 if (MO.isUse() && MO.isUndef()) {
755 MO.setReg(Edit->get(0)->reg);
759 SlotIndex Idx = LIS.getInstructionIndex(MI);
760 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
762 // Rewrite to the mapped register at Idx.
763 unsigned RegIdx = RegAssign.lookup(Idx);
764 MO.setReg(Edit->get(RegIdx)->reg);
765 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
766 << Idx << ':' << RegIdx << '\t' << *MI);
768 // Extend liveness to Idx.
770 extendRange(RegIdx, Idx);
774 void SplitEditor::deleteRematVictims() {
775 SmallVector<MachineInstr*, 8> Dead;
776 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
777 E = Edit->getParent().vni_end(); I != E; ++I) {
778 const VNInfo *VNI = *I;
779 // Was VNI rematted anywhere?
780 if (VNI->isUnused() || VNI->isPHIDef() || !Edit->didRematerialize(VNI))
782 unsigned RegIdx = RegAssign.lookup(VNI->def);
783 LiveInterval *LI = Edit->get(RegIdx);
784 LiveInterval::const_iterator LII = LI->FindLiveRangeContaining(VNI->def);
785 assert(LII != LI->end() && "Missing live range for rematted def");
787 // Is this a dead def?
788 if (LII->end != VNI->def.getNextSlot())
791 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
792 assert(MI && "Missing instruction for dead def");
793 MI->addRegisterDead(LI->reg, &TRI);
795 if (!MI->allDefsAreDead())
798 DEBUG(dbgs() << "All defs dead: " << *MI);
805 Edit->eliminateDeadDefs(Dead, LIS, TII);
808 void SplitEditor::finish() {
809 assert(OpenIdx == 0 && "Previous LI not closed before rewrite");
812 // At this point, the live intervals in Edit contain VNInfos corresponding to
813 // the inserted copies.
815 // Add the original defs from the parent interval.
816 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
817 E = Edit->getParent().vni_end(); I != E; ++I) {
818 const VNInfo *ParentVNI = *I;
819 if (ParentVNI->isUnused())
821 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
822 VNInfo *VNI = defValue(RegIdx, ParentVNI, ParentVNI->def);
823 VNI->setIsPHIDef(ParentVNI->isPHIDef());
824 VNI->setCopy(ParentVNI->getCopy());
826 // Mark rematted values as complex everywhere to force liveness computation.
827 // The new live ranges may be truncated.
828 if (Edit->didRematerialize(ParentVNI))
829 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
830 markComplexMapped(i, ParentVNI);
834 // Every new interval must have a def by now, otherwise the split is bogus.
835 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I)
836 assert((*I)->hasAtLeastOneValue() && "Split interval has no value");
839 // Transfer the simply mapped values, check if any are complex.
840 bool Complex = transferSimpleValues();
842 extendPHIKillRanges();
846 // Rewrite virtual registers, possibly extending ranges.
847 rewriteAssigned(Complex);
849 // Delete defs that were rematted everywhere.
851 deleteRematVictims();
853 // Get rid of unused values and set phi-kill flags.
854 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I)
855 (*I)->RenumberValues(LIS);
857 // Now check if any registers were separated into multiple components.
858 ConnectedVNInfoEqClasses ConEQ(LIS);
859 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
860 // Don't use iterators, they are invalidated by create() below.
861 LiveInterval *li = Edit->get(i);
862 unsigned NumComp = ConEQ.Classify(li);
865 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
866 SmallVector<LiveInterval*, 8> dups;
868 for (unsigned i = 1; i != NumComp; ++i)
869 dups.push_back(&Edit->create(MRI, LIS, VRM));
870 ConEQ.Distribute(&dups[0], MRI);
873 // Calculate spill weight and allocation hints for new intervals.
874 VirtRegAuxInfo vrai(VRM.getMachineFunction(), LIS, SA.Loops);
875 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
876 LiveInterval &li = **I;
877 vrai.CalculateRegClass(li.reg);
878 vrai.CalculateWeightAndHint(li);
879 DEBUG(dbgs() << " new interval " << MRI.getRegClass(li.reg)->getName()
880 << ":" << li << '\n');
885 //===----------------------------------------------------------------------===//
886 // Single Block Splitting
887 //===----------------------------------------------------------------------===//
889 /// getMultiUseBlocks - if CurLI has more than one use in a basic block, it
890 /// may be an advantage to split CurLI for the duration of the block.
891 bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
892 // If CurLI is local to one block, there is no point to splitting it.
893 if (LiveBlocks.size() <= 1)
895 // Add blocks with multiple uses.
896 for (unsigned i = 0, e = LiveBlocks.size(); i != e; ++i) {
897 const BlockInfo &BI = LiveBlocks[i];
900 unsigned Instrs = UsingBlocks.lookup(BI.MBB);
903 if (Instrs == 2 && BI.LiveIn && BI.LiveOut && !BI.LiveThrough)
905 Blocks.insert(BI.MBB);
907 return !Blocks.empty();
910 /// splitSingleBlocks - Split CurLI into a separate live interval inside each
911 /// basic block in Blocks.
912 void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
913 DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n");
915 for (unsigned i = 0, e = SA.LiveBlocks.size(); i != e; ++i) {
916 const SplitAnalysis::BlockInfo &BI = SA.LiveBlocks[i];
917 if (!BI.Uses || !Blocks.count(BI.MBB))
921 SlotIndex SegStart = enterIntvBefore(BI.FirstUse);
922 if (!BI.LiveOut || BI.LastUse < BI.LastSplitPoint) {
923 useIntv(SegStart, leaveIntvAfter(BI.LastUse));
925 // The last use is after the last valid split point.
926 SlotIndex SegStop = leaveIntvBefore(BI.LastSplitPoint);
927 useIntv(SegStart, SegStop);
928 overlapIntv(SegStop, BI.LastUse);