1 //===-- PreAllocSplitting.cpp - Pre-allocation Interval Spltting Pass. ----===//
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 implements the machine instruction level pre-register allocation
11 // live interval splitting pass. It finds live interval barriers, i.e.
12 // instructions which will kill all physical registers in certain register
13 // classes, and split all live intervals which cross the barrier.
15 //===----------------------------------------------------------------------===//
17 #define DEBUG_TYPE "pre-alloc-split"
18 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineLoopInfo.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/CodeGen/RegisterCoalescer.h"
25 #include "llvm/Target/TargetInstrInfo.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetOptions.h"
28 #include "llvm/Target/TargetRegisterInfo.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/ADT/Statistic.h"
36 static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden);
38 STATISTIC(NumSplits, "Number of intervals split");
41 class VISIBILITY_HIDDEN PreAllocSplitting : public MachineFunctionPass {
42 MachineFunction *CurMF;
43 const TargetMachine *TM;
44 const TargetInstrInfo *TII;
45 MachineFrameInfo *MFI;
46 MachineRegisterInfo *MRI;
49 // Barrier - Current barrier being processed.
50 MachineInstr *Barrier;
52 // BarrierMBB - Basic block where the barrier resides in.
53 MachineBasicBlock *BarrierMBB;
55 // Barrier - Current barrier index.
58 // CurrLI - Current live interval being split.
61 // LIValNoSSMap - A map from live interval and val# pairs to spill slots.
62 // This records what live interval's val# has been split and what spill
64 std::map<std::pair<unsigned, unsigned>, int> LIValNoSSMap;
66 // RestoreMIs - All the restores inserted due to live interval splitting.
67 SmallPtrSet<MachineInstr*, 8> RestoreMIs;
71 PreAllocSplitting() : MachineFunctionPass(&ID) {}
73 virtual bool runOnMachineFunction(MachineFunction &MF);
75 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
76 AU.addRequired<LiveIntervals>();
77 AU.addPreserved<LiveIntervals>();
78 AU.addPreserved<RegisterCoalescer>();
80 AU.addPreservedID(StrongPHIEliminationID);
82 AU.addPreservedID(PHIEliminationID);
83 MachineFunctionPass::getAnalysisUsage(AU);
86 virtual void releaseMemory() {
91 virtual const char *getPassName() const {
92 return "Pre-Register Allocaton Live Interval Splitting";
95 /// print - Implement the dump method.
96 virtual void print(std::ostream &O, const Module* M = 0) const {
100 void print(std::ostream *O, const Module* M = 0) const {
105 MachineBasicBlock::iterator
106 findNextEmptySlot(MachineBasicBlock*, MachineInstr*,
109 MachineBasicBlock::iterator
110 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
111 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
113 MachineBasicBlock::iterator
114 findRestorePoint(MachineBasicBlock*, MachineInstr*, unsigned,
115 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
117 void RecordSplit(unsigned, unsigned, unsigned, int);
119 bool isAlreadySplit(unsigned, unsigned, int&);
121 void UpdateIntervalForSplit(VNInfo*, unsigned, unsigned);
123 bool ShrinkWrapToLastUse(MachineBasicBlock*,
124 SmallVector<MachineOperand*, 4>&,
125 SmallPtrSet<MachineInstr*, 4>&);
127 void ShrinkWrapLiveInterval(VNInfo*, MachineBasicBlock*, MachineBasicBlock*,
128 MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 8>&,
129 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >&,
130 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >&,
131 SmallVector<MachineBasicBlock*, 4>&);
133 bool SplitRegLiveInterval(LiveInterval*);
135 bool SplitRegLiveIntervals(const TargetRegisterClass **);
137 } // end anonymous namespace
139 char PreAllocSplitting::ID = 0;
141 static RegisterPass<PreAllocSplitting>
142 X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
144 const PassInfo *const llvm::PreAllocSplittingID = &X;
147 /// findNextEmptySlot - Find a gap after the given machine instruction in the
148 /// instruction index map. If there isn't one, return end().
149 MachineBasicBlock::iterator
150 PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
151 unsigned &SpotIndex) {
152 MachineBasicBlock::iterator MII = MI;
153 if (++MII != MBB->end()) {
154 unsigned Index = LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
163 /// findSpillPoint - Find a gap as far away from the given MI that's suitable
164 /// for spilling the current live interval. The index must be before any
165 /// defs and uses of the live interval register in the mbb. Return begin() if
167 MachineBasicBlock::iterator
168 PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
170 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
171 unsigned &SpillIndex) {
172 MachineBasicBlock::iterator Pt = MBB->begin();
174 // Go top down if RefsInMBB is empty.
175 if (RefsInMBB.empty() && !DefMI) {
176 MachineBasicBlock::iterator MII = MBB->begin();
177 MachineBasicBlock::iterator EndPt = MI;
180 unsigned Index = LIs->getInstructionIndex(MII);
181 unsigned Gap = LIs->findGapBeforeInstr(Index);
187 } while (MII != EndPt);
189 MachineBasicBlock::iterator MII = MI;
190 MachineBasicBlock::iterator EndPt = DefMI
191 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
192 while (MII != EndPt && !RefsInMBB.count(MII)) {
193 unsigned Index = LIs->getInstructionIndex(MII);
194 if (LIs->hasGapBeforeInstr(Index)) {
196 SpillIndex = LIs->findGapBeforeInstr(Index, true);
205 /// findRestorePoint - Find a gap in the instruction index map that's suitable
206 /// for restoring the current live interval value. The index must be before any
207 /// uses of the live interval register in the mbb. Return end() if none is
209 MachineBasicBlock::iterator
210 PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
212 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
213 unsigned &RestoreIndex) {
214 MachineBasicBlock::iterator Pt = MBB->end();
215 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
217 // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond
218 // the last index in the live range.
219 if (RefsInMBB.empty() && LastIdx >= EndIdx) {
220 MachineBasicBlock::iterator MII = MBB->end();
221 MachineBasicBlock::iterator EndPt = MI;
224 unsigned Index = LIs->getInstructionIndex(MII);
225 unsigned Gap = LIs->findGapBeforeInstr(Index);
231 } while (MII != EndPt);
233 MachineBasicBlock::iterator MII = MI;
235 // FIXME: Limit the number of instructions to examine to reduce
237 while (MII != MBB->end()) {
238 unsigned Index = LIs->getInstructionIndex(MII);
241 unsigned Gap = LIs->findGapBeforeInstr(Index);
246 if (RefsInMBB.count(MII))
255 /// RecordSplit - Given a register live interval is split, remember the spill
256 /// slot where the val#s are in.
257 void PreAllocSplitting::RecordSplit(unsigned Reg, unsigned SpillIndex,
258 unsigned RestoreIndex, int SS) {
259 const LiveRange *LR = NULL;
261 LR = CurrLI->getLiveRangeContaining(LIs->getUseIndex(SpillIndex));
262 LIValNoSSMap.insert(std::make_pair(std::make_pair(CurrLI->reg,
263 LR->valno->id), SS));
265 LR = CurrLI->getLiveRangeContaining(LIs->getDefIndex(RestoreIndex));
266 LIValNoSSMap.insert(std::make_pair(std::make_pair(CurrLI->reg,
267 LR->valno->id), SS));
270 /// isAlreadySplit - Return if a given val# of a register live interval is already
271 /// split. Also return by reference the spill stock where the value is.
272 bool PreAllocSplitting::isAlreadySplit(unsigned Reg, unsigned ValNoId, int &SS){
273 std::map<std::pair<unsigned, unsigned>, int>::iterator I =
274 LIValNoSSMap.find(std::make_pair(Reg, ValNoId));
275 if (I == LIValNoSSMap.end())
281 /// UpdateIntervalForSplit - Given the specified val# of the current live
282 /// interval is being split, and the split and rejoin indices, update the live
283 /// interval accordingly.
285 PreAllocSplitting::UpdateIntervalForSplit(VNInfo *ValNo, unsigned SplitIndex,
286 unsigned JoinIndex) {
287 SmallVector<std::pair<unsigned,unsigned>, 4> Before;
288 SmallVector<std::pair<unsigned,unsigned>, 4> After;
289 SmallVector<unsigned, 4> BeforeKills;
290 SmallVector<unsigned, 4> AfterKills;
291 SmallPtrSet<const LiveRange*, 4> Processed;
293 // First, let's figure out which parts of the live interval is now defined
294 // by the restore, which are defined by the original definition.
295 const LiveRange *LR = CurrLI->getLiveRangeContaining(JoinIndex);
296 After.push_back(std::make_pair(JoinIndex, LR->end));
297 if (CurrLI->isKill(ValNo, LR->end))
298 AfterKills.push_back(LR->end);
300 assert(LR->contains(SplitIndex));
301 if (SplitIndex > LR->start) {
302 Before.push_back(std::make_pair(LR->start, SplitIndex));
303 BeforeKills.push_back(SplitIndex);
305 Processed.insert(LR);
307 SmallVector<MachineBasicBlock*, 4> WorkList;
308 MachineBasicBlock *MBB = LIs->getMBBFromIndex(LR->end-1);
309 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
310 SE = MBB->succ_end(); SI != SE; ++SI)
311 WorkList.push_back(*SI);
313 while (!WorkList.empty()) {
314 MBB = WorkList.back();
316 unsigned Idx = LIs->getMBBStartIdx(MBB);
317 LR = CurrLI->getLiveRangeContaining(Idx);
318 if (LR && LR->valno == ValNo && !Processed.count(LR)) {
319 After.push_back(std::make_pair(LR->start, LR->end));
320 if (CurrLI->isKill(ValNo, LR->end))
321 AfterKills.push_back(LR->end);
322 Idx = LIs->getMBBEndIdx(MBB);
324 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
325 SE = MBB->succ_end(); SI != SE; ++SI)
326 WorkList.push_back(*SI);
327 if (LR->end > Idx+1) {
328 MBB = LIs->getMBBFromIndex(LR->end-1);
329 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
330 SE = MBB->succ_end(); SI != SE; ++SI)
331 WorkList.push_back(*SI);
334 Processed.insert(LR);
338 for (LiveInterval::iterator I = CurrLI->begin(), E = CurrLI->end();
341 if (LR->valno == ValNo && !Processed.count(LR)) {
342 Before.push_back(std::make_pair(LR->start, LR->end));
343 if (CurrLI->isKill(ValNo, LR->end))
344 BeforeKills.push_back(LR->end);
348 // Now create new val#s to represent the live ranges defined by the old def
349 // those defined by the restore.
350 unsigned AfterDef = ValNo->def;
351 MachineInstr *AfterCopy = ValNo->copy;
352 bool HasPHIKill = ValNo->hasPHIKill;
353 CurrLI->removeValNo(ValNo);
354 VNInfo *BValNo = (Before.empty())
356 : CurrLI->getNextValue(AfterDef, AfterCopy, LIs->getVNInfoAllocator());
358 CurrLI->addKills(BValNo, BeforeKills);
360 VNInfo *AValNo = (After.empty())
362 : CurrLI->getNextValue(JoinIndex,0, LIs->getVNInfoAllocator());
364 AValNo->hasPHIKill = HasPHIKill;
365 CurrLI->addKills(AValNo, AfterKills);
368 for (unsigned i = 0, e = Before.size(); i != e; ++i) {
369 unsigned Start = Before[i].first;
370 unsigned End = Before[i].second;
371 CurrLI->addRange(LiveRange(Start, End, BValNo));
373 for (unsigned i = 0, e = After.size(); i != e; ++i) {
374 unsigned Start = After[i].first;
375 unsigned End = After[i].second;
376 CurrLI->addRange(LiveRange(Start, End, AValNo));
380 /// ShrinkWrapToLastUse - There are uses of the current live interval in the
381 /// given block, shrink wrap the live interval to the last use (i.e. remove
382 /// from last use to the end of the mbb). In case mbb is the where the barrier
383 /// is, remove from the last use to the barrier.
385 PreAllocSplitting::ShrinkWrapToLastUse(MachineBasicBlock *MBB,
386 SmallVector<MachineOperand*, 4> &Uses,
387 SmallPtrSet<MachineInstr*, 4> &UseMIs) {
388 MachineOperand *LastMO = 0;
389 MachineInstr *LastMI = 0;
390 if (MBB != BarrierMBB && Uses.size() == 1) {
391 // Single use, no need to traverse the block. We can't assume this for the
392 // barrier bb though since the use is probably below the barrier.
394 LastMI = LastMO->getParent();
396 MachineBasicBlock::iterator MEE = MBB->begin();
397 MachineBasicBlock::iterator MII;
398 if (MBB == BarrierMBB)
402 while (--MII != MEE) {
403 MachineInstr *UseMI = &*MII;
404 if (!UseMIs.count(UseMI))
406 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
407 MachineOperand &MO = UseMI->getOperand(i);
408 if (MO.isReg() && MO.getReg() == CurrLI->reg) {
418 // Cut off live range from last use (or beginning of the mbb if there
419 // are no uses in it) to the end of the mbb.
420 unsigned RangeStart, RangeEnd = LIs->getMBBEndIdx(MBB)+1;
422 RangeStart = LIs->getUseIndex(LIs->getInstructionIndex(LastMI))+1;
423 assert(!LastMO->isKill() && "Last use already terminates the interval?");
426 assert(MBB == BarrierMBB);
427 RangeStart = LIs->getMBBStartIdx(MBB);
429 if (MBB == BarrierMBB)
430 RangeEnd = LIs->getUseIndex(BarrierIdx)+1;
431 CurrLI->removeRange(RangeStart, RangeEnd);
433 // Return true if the last use becomes a new kill.
437 /// ShrinkWrapLiveInterval - Recursively traverse the predecessor
438 /// chain to find the new 'kills' and shrink wrap the live interval to the
439 /// new kill indices.
441 PreAllocSplitting::ShrinkWrapLiveInterval(VNInfo *ValNo, MachineBasicBlock *MBB,
442 MachineBasicBlock *SuccMBB, MachineBasicBlock *DefMBB,
443 SmallPtrSet<MachineBasicBlock*, 8> &Visited,
444 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > &Uses,
445 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > &UseMIs,
446 SmallVector<MachineBasicBlock*, 4> &UseMBBs) {
447 if (Visited.count(MBB))
450 // If live interval is live in another successor path, then we can't process
451 // this block. But we may able to do so after all the successors have been
453 if (MBB != BarrierMBB) {
454 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
455 SE = MBB->succ_end(); SI != SE; ++SI) {
456 MachineBasicBlock *SMBB = *SI;
459 if (CurrLI->liveAt(LIs->getMBBStartIdx(SMBB)))
466 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
467 UMII = Uses.find(MBB);
468 if (UMII != Uses.end()) {
469 // At least one use in this mbb, lets look for the kill.
470 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
471 UMII2 = UseMIs.find(MBB);
472 if (ShrinkWrapToLastUse(MBB, UMII->second, UMII2->second))
473 // Found a kill, shrink wrapping of this path ends here.
475 } else if (MBB == DefMBB) {
476 assert(LIValNoSSMap.find(std::make_pair(CurrLI->reg, ValNo->id)) !=
477 LIValNoSSMap.end() && "Why wasn't def spilled?");
478 // There are no uses after the def.
479 MachineInstr *DefMI = LIs->getInstructionFromIndex(ValNo->def);
480 assert(RestoreMIs.count(DefMI) && "Not defined by a join?");
481 if (UseMBBs.empty()) {
482 // The only use must be below barrier in the barrier block. It's safe to
484 LIs->RemoveMachineInstrFromMaps(DefMI);
485 DefMI->eraseFromParent();
486 CurrLI->removeRange(ValNo->def, LIs->getMBBEndIdx(MBB)+1);
488 } else if (MBB == BarrierMBB) {
489 // Remove entire live range from start of mbb to barrier.
490 CurrLI->removeRange(LIs->getMBBStartIdx(MBB),
491 LIs->getUseIndex(BarrierIdx)+1);
493 // Remove entire live range of the mbb out of the live interval.
494 CurrLI->removeRange(LIs->getMBBStartIdx(MBB), LIs->getMBBEndIdx(MBB)+1);
498 // Reached the def mbb, stop traversing this path further.
501 // Traverse the pathes up the predecessor chains further.
502 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
503 PE = MBB->pred_end(); PI != PE; ++PI) {
504 MachineBasicBlock *Pred = *PI;
507 if (Pred == DefMBB && ValNo->hasPHIKill)
508 // Pred is the def bb and the def reaches other val#s, we must
509 // allow the value to be live out of the bb.
511 ShrinkWrapLiveInterval(ValNo, Pred, MBB, DefMBB, Visited,
512 Uses, UseMIs, UseMBBs);
518 /// SplitRegLiveInterval - Split (spill and restore) the given live interval
519 /// so it would not cross the barrier that's being processed. Shrink wrap
520 /// (minimize) the live interval to the last uses.
521 bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
524 // Find live range where current interval cross the barrier.
525 LiveInterval::iterator LR =
526 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
527 VNInfo *ValNo = LR->valno;
529 if (ValNo->def == ~1U) {
530 // Defined by a dead def? How can this be?
531 assert(0 && "Val# is defined by a dead def?");
535 // FIXME: For now, if definition is rematerializable, do not split.
536 MachineInstr *DefMI = (ValNo->def != ~0U)
537 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
538 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
541 // Find all references in the barrier mbb.
542 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
543 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
544 E = MRI->reg_end(); I != E; ++I) {
545 MachineInstr *RefMI = &*I;
546 if (RefMI->getParent() == BarrierMBB)
547 RefsInMBB.insert(RefMI);
550 // Find a point to restore the value after the barrier.
551 unsigned RestoreIndex;
552 MachineBasicBlock::iterator RestorePt =
553 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
554 if (RestorePt == BarrierMBB->end())
557 // Add a spill either before the barrier or after the definition.
558 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
559 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
560 unsigned SpillIndex = 0;
561 MachineInstr *SpillMI = NULL;
563 bool PrevSpilled = isAlreadySplit(CurrLI->reg, ValNo->id, SS);
564 if (ValNo->def == ~0U) {
565 // If it's defined by a phi, we must split just before the barrier.
566 MachineBasicBlock::iterator SpillPt =
567 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
568 if (SpillPt == BarrierMBB->begin())
569 return false; // No gap to insert spill.
572 // If previously split, reuse the spill slot.
573 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
574 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
575 SpillMI = prior(SpillPt);
576 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
577 } else if (!PrevSpilled) {
579 // Def is dead. Do nothing.
581 // If it's already split, just restore the value. There is no need to spill
583 // Check if it's possible to insert a spill after the def MI.
584 MachineBasicBlock::iterator SpillPt;
585 if (DefMBB == BarrierMBB) {
586 // Add spill after the def and the last use before the barrier.
587 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI, RefsInMBB, SpillIndex);
588 if (SpillPt == DefMBB->begin())
589 return false; // No gap to insert spill.
591 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
592 if (SpillPt == DefMBB->end())
593 return false; // No gap to insert spill.
595 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
597 // Add spill. The store instruction kills the register if def is before
598 // the barrier in the barrier block.
599 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
600 DefMBB == BarrierMBB, SS, RC);
601 SpillMI = prior(SpillPt);
602 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
606 // FIXME: Create live interval for stack slot.
607 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
608 MachineInstr *LoadMI = prior(RestorePt);
609 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
610 RestoreMIs.insert(LoadMI);
612 // If live interval is spilled in the same block as the barrier, just
613 // create a hole in the interval.
615 (SpillMI && SpillMI->getParent() == BarrierMBB)) {
616 UpdateIntervalForSplit(ValNo, LIs->getUseIndex(SpillIndex)+1,
617 LIs->getDefIndex(RestoreIndex));
619 // Record val# values are in the specific spill slot.
620 RecordSplit(CurrLI->reg, SpillIndex, RestoreIndex, SS);
626 // Shrink wrap the live interval by walking up the CFG and find the
628 // Now let's find all the uses of the val#.
629 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > Uses;
630 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > UseMIs;
631 SmallPtrSet<MachineBasicBlock*, 4> Seen;
632 SmallVector<MachineBasicBlock*, 4> UseMBBs;
633 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(CurrLI->reg),
634 UE = MRI->use_end(); UI != UE; ++UI) {
635 MachineOperand &UseMO = UI.getOperand();
636 MachineInstr *UseMI = UseMO.getParent();
637 unsigned UseIdx = LIs->getInstructionIndex(UseMI);
638 LiveInterval::iterator ULR = CurrLI->FindLiveRangeContaining(UseIdx);
639 if (ULR->valno != ValNo)
641 MachineBasicBlock *UseMBB = UseMI->getParent();
642 // Remember which other mbb's use this val#.
643 if (Seen.insert(UseMBB) && UseMBB != BarrierMBB)
644 UseMBBs.push_back(UseMBB);
645 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
646 UMII = Uses.find(UseMBB);
647 if (UMII != Uses.end()) {
648 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
649 UMII2 = UseMIs.find(UseMBB);
650 UMII->second.push_back(&UseMO);
651 UMII2->second.insert(UseMI);
653 SmallVector<MachineOperand*, 4> Ops;
654 Ops.push_back(&UseMO);
655 Uses.insert(std::make_pair(UseMBB, Ops));
656 SmallPtrSet<MachineInstr*, 4> MIs;
658 UseMIs.insert(std::make_pair(UseMBB, MIs));
662 // Walk up the predecessor chains.
663 SmallPtrSet<MachineBasicBlock*, 8> Visited;
664 ShrinkWrapLiveInterval(ValNo, BarrierMBB, NULL, DefMBB, Visited,
665 Uses, UseMIs, UseMBBs);
667 // Remove live range from barrier to the restore. FIXME: Find a better
668 // point to re-start the live interval.
669 UpdateIntervalForSplit(ValNo, LIs->getUseIndex(BarrierIdx)+1,
670 LIs->getDefIndex(RestoreIndex));
671 // Record val# values are in the specific spill slot.
672 RecordSplit(CurrLI->reg, SpillIndex, RestoreIndex, SS);
678 /// SplitRegLiveIntervals - Split all register live intervals that cross the
679 /// barrier that's being processed.
681 PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs) {
682 // First find all the virtual registers whose live intervals are intercepted
683 // by the current barrier.
684 SmallVector<LiveInterval*, 8> Intervals;
685 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
686 if (TII->IgnoreRegisterClassBarriers(*RC))
688 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
689 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
690 unsigned Reg = VRs[i];
691 if (!LIs->hasInterval(Reg))
693 LiveInterval *LI = &LIs->getInterval(Reg);
694 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
695 // Virtual register live interval is intercepted by the barrier. We
696 // should split and shrink wrap its interval if possible.
697 Intervals.push_back(LI);
701 // Process the affected live intervals.
703 while (!Intervals.empty()) {
704 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
706 LiveInterval *LI = Intervals.back();
707 Intervals.pop_back();
708 Change |= SplitRegLiveInterval(LI);
714 bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
716 TM = &MF.getTarget();
717 TII = TM->getInstrInfo();
718 MFI = MF.getFrameInfo();
719 MRI = &MF.getRegInfo();
720 LIs = &getAnalysis<LiveIntervals>();
722 bool MadeChange = false;
724 // Make sure blocks are numbered in order.
727 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
730 for (MachineBasicBlock::reverse_iterator II = BarrierMBB->rbegin(),
731 EE = BarrierMBB->rend(); II != EE; ++II) {
733 const TargetRegisterClass **BarrierRCs =
734 Barrier->getDesc().getRegClassBarriers();
737 BarrierIdx = LIs->getInstructionIndex(Barrier);
738 MadeChange |= SplitRegLiveIntervals(BarrierRCs);