blob: f3a3ac30dd1661ec7880b59f6aae64b226c43e2e [file] [log] [blame]
Evan Cheng09e8ca82008-10-20 21:44:59 +00001//===-- PreAllocSplitting.cpp - Pre-allocation Interval Spltting Pass. ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
14//
15//===----------------------------------------------------------------------===//
16
17#define DEBUG_TYPE "pre-alloc-split"
18#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000020#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"
Evan Chengf5cd4f02008-10-23 20:43:13 +000025#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000026#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"
Evan Cheng09e8ca82008-10-20 21:44:59 +000031#include "llvm/ADT/SmallPtrSet.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000032#include "llvm/ADT/Statistic.h"
33#include <map>
Evan Cheng09e8ca82008-10-20 21:44:59 +000034using namespace llvm;
35
Evan Chengf5cd4f02008-10-23 20:43:13 +000036STATISTIC(NumSplit , "Number of intervals split");
37
Evan Cheng09e8ca82008-10-20 21:44:59 +000038namespace {
39 class VISIBILITY_HIDDEN PreAllocSplitting : public MachineFunctionPass {
Evan Chengf5cd4f02008-10-23 20:43:13 +000040 MachineFunction *CurMF;
41 const TargetMachine *TM;
42 const TargetInstrInfo *TII;
43 MachineFrameInfo *MFI;
44 MachineRegisterInfo *MRI;
45 LiveIntervals *LIs;
Evan Cheng09e8ca82008-10-20 21:44:59 +000046
Evan Chengf5cd4f02008-10-23 20:43:13 +000047 // Barrier - Current barrier being processed.
48 MachineInstr *Barrier;
49
50 // BarrierMBB - Basic block where the barrier resides in.
51 MachineBasicBlock *BarrierMBB;
52
53 // Barrier - Current barrier index.
54 unsigned BarrierIdx;
55
56 // CurrLI - Current live interval being split.
57 LiveInterval *CurrLI;
58
59 // LIValNoSSMap - A map from live interval and val# pairs to spill slots.
60 // This records what live interval's val# has been split and what spill
61 // slot was used.
62 std::map<std::pair<unsigned, unsigned>, int> LIValNoSSMap;
63
Evan Cheng06587492008-10-24 02:05:00 +000064 // RestoreMIs - All the restores inserted due to live interval splitting.
65 SmallPtrSet<MachineInstr*, 8> RestoreMIs;
66
Evan Cheng09e8ca82008-10-20 21:44:59 +000067 public:
68 static char ID;
69 PreAllocSplitting() : MachineFunctionPass(&ID) {}
70
71 virtual bool runOnMachineFunction(MachineFunction &MF);
72
73 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
74 AU.addRequired<LiveIntervals>();
75 AU.addPreserved<LiveIntervals>();
Evan Cheng09e8ca82008-10-20 21:44:59 +000076 AU.addPreserved<RegisterCoalescer>();
77 if (StrongPHIElim)
78 AU.addPreservedID(StrongPHIEliminationID);
79 else
80 AU.addPreservedID(PHIEliminationID);
Evan Cheng09e8ca82008-10-20 21:44:59 +000081 MachineFunctionPass::getAnalysisUsage(AU);
82 }
83
84 virtual void releaseMemory() {
Evan Chengf5cd4f02008-10-23 20:43:13 +000085 LIValNoSSMap.clear();
Evan Cheng06587492008-10-24 02:05:00 +000086 RestoreMIs.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +000087 }
88
89 virtual const char *getPassName() const {
90 return "Pre-Register Allocaton Live Interval Splitting";
91 }
Evan Chengf5cd4f02008-10-23 20:43:13 +000092
93 /// print - Implement the dump method.
94 virtual void print(std::ostream &O, const Module* M = 0) const {
95 LIs->print(O, M);
96 }
97
98 void print(std::ostream *O, const Module* M = 0) const {
99 if (O) print(*O, M);
100 }
101
102 private:
103 MachineBasicBlock::iterator
104 findNextEmptySlot(MachineBasicBlock*, MachineInstr*,
105 unsigned&);
106
107 MachineBasicBlock::iterator
108 findSpillPoint(MachineBasicBlock*, MachineInstr*,
109 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
110
111 MachineBasicBlock::iterator
112 findRestorePoint(MachineBasicBlock*, MachineInstr*,
113 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
114
115 void RecordSplit(unsigned, unsigned, unsigned, int);
116
117 bool isAlreadySplit(unsigned, unsigned, int&);
118
119 void UpdateIntervalForSplit(VNInfo*, unsigned, unsigned);
120
121 bool ShrinkWrapToLastUse(MachineBasicBlock*,
Evan Cheng06587492008-10-24 02:05:00 +0000122 SmallVector<MachineOperand*, 4>&,
123 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000124
Evan Chengaaf510c2008-10-26 07:49:03 +0000125 void ShrinkWrapLiveInterval(VNInfo*, MachineBasicBlock*, MachineBasicBlock*,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000126 MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 8>&,
Evan Cheng06587492008-10-24 02:05:00 +0000127 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >&,
128 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >&,
129 SmallVector<MachineBasicBlock*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000130
131 bool SplitRegLiveInterval(LiveInterval*);
132
133 bool SplitRegLiveIntervals(const TargetRegisterClass **);
Evan Cheng09e8ca82008-10-20 21:44:59 +0000134 };
135} // end anonymous namespace
136
137char PreAllocSplitting::ID = 0;
138
139static RegisterPass<PreAllocSplitting>
140X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
141
142const PassInfo *const llvm::PreAllocSplittingID = &X;
143
Evan Chengf5cd4f02008-10-23 20:43:13 +0000144
145/// findNextEmptySlot - Find a gap after the given machine instruction in the
146/// instruction index map. If there isn't one, return end().
147MachineBasicBlock::iterator
148PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
149 unsigned &SpotIndex) {
150 MachineBasicBlock::iterator MII = MI;
151 if (++MII != MBB->end()) {
152 unsigned Index = LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
153 if (Index) {
154 SpotIndex = Index;
155 return MII;
156 }
157 }
158 return MBB->end();
159}
160
161/// findSpillPoint - Find a gap as far away from the given MI that's suitable
162/// for spilling the current live interval. The index must be before any
163/// defs and uses of the live interval register in the mbb. Return begin() if
164/// none is found.
165MachineBasicBlock::iterator
166PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
167 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
168 unsigned &SpillIndex) {
169 MachineBasicBlock::iterator Pt = MBB->begin();
170
171 // Go top down if RefsInMBB is empty.
172 if (RefsInMBB.empty()) {
173 MachineBasicBlock::iterator MII = MBB->begin();
174 MachineBasicBlock::iterator EndPt = MI;
175 do {
176 ++MII;
177 unsigned Index = LIs->getInstructionIndex(MII);
178 unsigned Gap = LIs->findGapBeforeInstr(Index);
179 if (Gap) {
180 Pt = MII;
181 SpillIndex = Gap;
182 break;
183 }
184 } while (MII != EndPt);
185 } else {
186 MachineBasicBlock::iterator MII = MI;
187 while (MII != MBB->begin() && !RefsInMBB.count(MII)) {
188 unsigned Index = LIs->getInstructionIndex(MII);
189 if (LIs->hasGapBeforeInstr(Index)) {
190 Pt = MII;
191 SpillIndex = LIs->findGapBeforeInstr(Index, true);
192 }
193 --MII;
194 }
195 }
196
197 return Pt;
198}
199
200/// findRestorePoint - Find a gap in the instruction index map that's suitable
201/// for restoring the current live interval value. The index must be before any
202/// uses of the live interval register in the mbb. Return end() if none is
203/// found.
204MachineBasicBlock::iterator
205PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
206 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
207 unsigned &RestoreIndex) {
208 MachineBasicBlock::iterator Pt = MBB->end();
209
210 // Go bottom up if RefsInMBB is empty.
211 if (RefsInMBB.empty()) {
212 MachineBasicBlock::iterator MII = MBB->end();
213 MachineBasicBlock::iterator EndPt = MI;
214 do {
215 --MII;
216 unsigned Index = LIs->getInstructionIndex(MII);
Evan Cheng56ab0de2008-10-24 18:46:44 +0000217 unsigned Gap = LIs->findGapBeforeInstr(Index);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000218 if (Gap) {
219 Pt = MII;
220 RestoreIndex = Gap;
221 break;
222 }
223 } while (MII != EndPt);
224 } else {
225 MachineBasicBlock::iterator MII = MI;
226 MII = ++MII;
227 while (MII != MBB->end()) {
228 unsigned Index = LIs->getInstructionIndex(MII);
229 unsigned Gap = LIs->findGapBeforeInstr(Index);
230 if (Gap) {
231 Pt = MII;
232 RestoreIndex = Gap;
233 }
234 if (RefsInMBB.count(MII))
235 break;
236 ++MII;
237 }
238 }
239
240 return Pt;
241}
242
243/// RecordSplit - Given a register live interval is split, remember the spill
244/// slot where the val#s are in.
245void PreAllocSplitting::RecordSplit(unsigned Reg, unsigned SpillIndex,
246 unsigned RestoreIndex, int SS) {
Evan Cheng06587492008-10-24 02:05:00 +0000247 const LiveRange *LR = NULL;
248 if (SpillIndex) {
249 LR = CurrLI->getLiveRangeContaining(LIs->getUseIndex(SpillIndex));
250 LIValNoSSMap.insert(std::make_pair(std::make_pair(CurrLI->reg,
251 LR->valno->id), SS));
252 }
253 LR = CurrLI->getLiveRangeContaining(LIs->getDefIndex(RestoreIndex));
254 LIValNoSSMap.insert(std::make_pair(std::make_pair(CurrLI->reg,
255 LR->valno->id), SS));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000256}
257
258/// isAlreadySplit - Return if a given val# of a register live interval is already
259/// split. Also return by reference the spill stock where the value is.
260bool PreAllocSplitting::isAlreadySplit(unsigned Reg, unsigned ValNoId, int &SS){
261 std::map<std::pair<unsigned, unsigned>, int>::iterator I =
262 LIValNoSSMap.find(std::make_pair(Reg, ValNoId));
263 if (I == LIValNoSSMap.end())
264 return false;
265 SS = I->second;
266 return true;
267}
268
269/// UpdateIntervalForSplit - Given the specified val# of the current live
270/// interval is being split, and the split and rejoin indices, update the live
271/// interval accordingly.
272void
273PreAllocSplitting::UpdateIntervalForSplit(VNInfo *ValNo, unsigned SplitIndex,
274 unsigned JoinIndex) {
275 SmallVector<std::pair<unsigned,unsigned>, 4> Before;
276 SmallVector<std::pair<unsigned,unsigned>, 4> After;
277 SmallVector<unsigned, 4> BeforeKills;
278 SmallVector<unsigned, 4> AfterKills;
279 SmallPtrSet<const LiveRange*, 4> Processed;
280
281 // First, let's figure out which parts of the live interval is now defined
282 // by the restore, which are defined by the original definition.
283 const LiveRange *LR = CurrLI->getLiveRangeContaining(JoinIndex);
284 After.push_back(std::make_pair(JoinIndex, LR->end));
Evan Cheng06587492008-10-24 02:05:00 +0000285 if (CurrLI->isKill(ValNo, LR->end))
286 AfterKills.push_back(LR->end);
287
Evan Chengf5cd4f02008-10-23 20:43:13 +0000288 assert(LR->contains(SplitIndex));
Evan Cheng06587492008-10-24 02:05:00 +0000289 if (SplitIndex > LR->start) {
290 Before.push_back(std::make_pair(LR->start, SplitIndex));
291 BeforeKills.push_back(SplitIndex);
292 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000293 Processed.insert(LR);
294
295 SmallVector<MachineBasicBlock*, 4> WorkList;
296 MachineBasicBlock *MBB = LIs->getMBBFromIndex(LR->end-1);
297 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
298 SE = MBB->succ_end(); SI != SE; ++SI)
299 WorkList.push_back(*SI);
300
301 while (!WorkList.empty()) {
302 MBB = WorkList.back();
303 WorkList.pop_back();
304 unsigned Idx = LIs->getMBBStartIdx(MBB);
305 LR = CurrLI->getLiveRangeContaining(Idx);
306 if (LR && LR->valno == ValNo && !Processed.count(LR)) {
307 After.push_back(std::make_pair(LR->start, LR->end));
308 if (CurrLI->isKill(ValNo, LR->end))
309 AfterKills.push_back(LR->end);
310 Idx = LIs->getMBBEndIdx(MBB);
311 if (LR->end > Idx) {
312 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
313 SE = MBB->succ_end(); SI != SE; ++SI)
314 WorkList.push_back(*SI);
315 if (LR->end > Idx+1) {
316 MBB = LIs->getMBBFromIndex(LR->end-1);
317 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
318 SE = MBB->succ_end(); SI != SE; ++SI)
319 WorkList.push_back(*SI);
320 }
321 }
322 Processed.insert(LR);
323 }
324 }
325
326 for (LiveInterval::iterator I = CurrLI->begin(), E = CurrLI->end();
327 I != E; ++I) {
328 LiveRange *LR = I;
329 if (LR->valno == ValNo && !Processed.count(LR)) {
330 Before.push_back(std::make_pair(LR->start, LR->end));
331 if (CurrLI->isKill(ValNo, LR->end))
332 BeforeKills.push_back(LR->end);
333 }
334 }
335
336 // Now create new val#s to represent the live ranges defined by the old def
337 // those defined by the restore.
338 unsigned AfterDef = ValNo->def;
339 MachineInstr *AfterCopy = ValNo->copy;
340 bool HasPHIKill = ValNo->hasPHIKill;
341 CurrLI->removeValNo(ValNo);
Evan Cheng06587492008-10-24 02:05:00 +0000342 VNInfo *BValNo = (Before.empty())
343 ? NULL
344 : CurrLI->getNextValue(AfterDef, AfterCopy, LIs->getVNInfoAllocator());
345 if (BValNo)
346 CurrLI->addKills(BValNo, BeforeKills);
347
348 VNInfo *AValNo = (After.empty())
349 ? NULL
350 : CurrLI->getNextValue(JoinIndex,0, LIs->getVNInfoAllocator());
351 if (AValNo) {
352 AValNo->hasPHIKill = HasPHIKill;
353 CurrLI->addKills(AValNo, AfterKills);
354 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000355
356 for (unsigned i = 0, e = Before.size(); i != e; ++i) {
357 unsigned Start = Before[i].first;
358 unsigned End = Before[i].second;
359 CurrLI->addRange(LiveRange(Start, End, BValNo));
360 }
361 for (unsigned i = 0, e = After.size(); i != e; ++i) {
362 unsigned Start = After[i].first;
363 unsigned End = After[i].second;
364 CurrLI->addRange(LiveRange(Start, End, AValNo));
365 }
366}
367
368/// ShrinkWrapToLastUse - There are uses of the current live interval in the
369/// given block, shrink wrap the live interval to the last use (i.e. remove
370/// from last use to the end of the mbb). In case mbb is the where the barrier
371/// is, remove from the last use to the barrier.
372bool
373PreAllocSplitting::ShrinkWrapToLastUse(MachineBasicBlock *MBB,
Evan Cheng06587492008-10-24 02:05:00 +0000374 SmallVector<MachineOperand*, 4> &Uses,
375 SmallPtrSet<MachineInstr*, 4> &UseMIs) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000376 MachineOperand *LastMO = 0;
377 MachineInstr *LastMI = 0;
378 if (MBB != BarrierMBB && Uses.size() == 1) {
379 // Single use, no need to traverse the block. We can't assume this for the
380 // barrier bb though since the use is probably below the barrier.
381 LastMO = Uses[0];
382 LastMI = LastMO->getParent();
383 } else {
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000384 MachineBasicBlock::iterator MEE = MBB->begin();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000385 MachineBasicBlock::iterator MII;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000386 if (MBB == BarrierMBB)
Evan Chengf5cd4f02008-10-23 20:43:13 +0000387 MII = Barrier;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000388 else
Evan Chengf5cd4f02008-10-23 20:43:13 +0000389 MII = MBB->end();
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000390 while (--MII != MEE) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000391 MachineInstr *UseMI = &*MII;
392 if (!UseMIs.count(UseMI))
393 continue;
394 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
395 MachineOperand &MO = UseMI->getOperand(i);
396 if (MO.isReg() && MO.getReg() == CurrLI->reg) {
397 LastMO = &MO;
398 break;
399 }
400 }
401 LastMI = UseMI;
402 break;
403 }
404 }
405
406 // Cut off live range from last use (or beginning of the mbb if there
407 // are no uses in it) to the end of the mbb.
408 unsigned RangeStart, RangeEnd = LIs->getMBBEndIdx(MBB)+1;
409 if (LastMI) {
410 RangeStart = LIs->getUseIndex(LIs->getInstructionIndex(LastMI))+1;
411 assert(!LastMO->isKill() && "Last use already terminates the interval?");
412 LastMO->setIsKill();
413 } else {
414 assert(MBB == BarrierMBB);
415 RangeStart = LIs->getMBBStartIdx(MBB);
416 }
417 if (MBB == BarrierMBB)
Evan Cheng06587492008-10-24 02:05:00 +0000418 RangeEnd = LIs->getUseIndex(BarrierIdx)+1;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000419 CurrLI->removeRange(RangeStart, RangeEnd);
420
421 // Return true if the last use becomes a new kill.
422 return LastMI;
423}
424
425/// ShrinkWrapLiveInterval - Recursively traverse the predecessor
426/// chain to find the new 'kills' and shrink wrap the live interval to the
427/// new kill indices.
428void
Evan Chengaaf510c2008-10-26 07:49:03 +0000429PreAllocSplitting::ShrinkWrapLiveInterval(VNInfo *ValNo, MachineBasicBlock *MBB,
430 MachineBasicBlock *SuccMBB, MachineBasicBlock *DefMBB,
Evan Cheng06587492008-10-24 02:05:00 +0000431 SmallPtrSet<MachineBasicBlock*, 8> &Visited,
432 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > &Uses,
433 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > &UseMIs,
434 SmallVector<MachineBasicBlock*, 4> &UseMBBs) {
Evan Chengaaf510c2008-10-26 07:49:03 +0000435 if (Visited.count(MBB))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000436 return;
437
Evan Chengaaf510c2008-10-26 07:49:03 +0000438 // If live interval is live in another successor path, then we can't process
439 // this block. But we may able to do so after all the successors have been
440 // processed.
441 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
442 SE = MBB->succ_end(); SI != SE; ++SI) {
443 MachineBasicBlock *SMBB = *SI;
444 if (SMBB == SuccMBB)
445 continue;
446 if (CurrLI->liveAt(LIs->getMBBStartIdx(SMBB)))
447 return;
448 }
449
450 Visited.insert(MBB);
451
Evan Cheng06587492008-10-24 02:05:00 +0000452 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
453 UMII = Uses.find(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000454 if (UMII != Uses.end()) {
455 // At least one use in this mbb, lets look for the kill.
Evan Cheng06587492008-10-24 02:05:00 +0000456 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
457 UMII2 = UseMIs.find(MBB);
458 if (ShrinkWrapToLastUse(MBB, UMII->second, UMII2->second))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000459 // Found a kill, shrink wrapping of this path ends here.
460 return;
Evan Cheng06587492008-10-24 02:05:00 +0000461 } else if (MBB == DefMBB) {
462 assert(LIValNoSSMap.find(std::make_pair(CurrLI->reg, ValNo->id)) !=
463 LIValNoSSMap.end() && "Why wasn't def spilled?");
464 // There are no uses after the def.
465 MachineInstr *DefMI = LIs->getInstructionFromIndex(ValNo->def);
466 assert(RestoreMIs.count(DefMI) && "Not defined by a join?");
467 if (UseMBBs.empty()) {
468 // The only use must be below barrier in the barrier block. It's safe to
469 // remove the def.
470 LIs->RemoveMachineInstrFromMaps(DefMI);
471 DefMI->eraseFromParent();
472 CurrLI->removeRange(ValNo->def, LIs->getMBBEndIdx(MBB)+1);
473 }
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000474 } else if (MBB == BarrierMBB) {
475 // Remove entire live range from start of mbb to barrier.
476 CurrLI->removeRange(LIs->getMBBStartIdx(MBB),
477 LIs->getUseIndex(BarrierIdx)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000478 } else {
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000479 // Remove entire live range of the mbb out of the live interval.
Evan Cheng06587492008-10-24 02:05:00 +0000480 CurrLI->removeRange(LIs->getMBBStartIdx(MBB), LIs->getMBBEndIdx(MBB)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000481 }
482
483 if (MBB == DefMBB)
484 // Reached the def mbb, stop traversing this path further.
485 return;
486
487 // Traverse the pathes up the predecessor chains further.
488 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
489 PE = MBB->pred_end(); PI != PE; ++PI) {
490 MachineBasicBlock *Pred = *PI;
491 if (Pred == MBB)
492 continue;
493 if (Pred == DefMBB && ValNo->hasPHIKill)
494 // Pred is the def bb and the def reaches other val#s, we must
495 // allow the value to be live out of the bb.
496 continue;
Evan Chengaaf510c2008-10-26 07:49:03 +0000497 ShrinkWrapLiveInterval(ValNo, Pred, MBB, DefMBB, Visited,
498 Uses, UseMIs, UseMBBs);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000499 }
500
501 return;
502}
503
504/// SplitRegLiveInterval - Split (spill and restore) the given live interval
505/// so it would not cross the barrier that's being processed. Shrink wrap
506/// (minimize) the live interval to the last uses.
507bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
508 CurrLI = LI;
509
510 // Find live range where current interval cross the barrier.
511 LiveInterval::iterator LR =
512 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
513 VNInfo *ValNo = LR->valno;
514
515 if (ValNo->def == ~1U) {
516 // Defined by a dead def? How can this be?
517 assert(0 && "Val# is defined by a dead def?");
518 abort();
519 }
520
Evan Cheng06587492008-10-24 02:05:00 +0000521 // FIXME: For now, if definition is rematerializable, do not split.
522 MachineInstr *DefMI = (ValNo->def != ~0U)
523 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
524 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
525 return false;
526
Evan Chengf5cd4f02008-10-23 20:43:13 +0000527 // Find all references in the barrier mbb.
528 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
529 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
530 E = MRI->reg_end(); I != E; ++I) {
531 MachineInstr *RefMI = &*I;
532 if (RefMI->getParent() == BarrierMBB)
533 RefsInMBB.insert(RefMI);
534 }
535
536 // Find a point to restore the value after the barrier.
537 unsigned RestoreIndex;
538 MachineBasicBlock::iterator RestorePt =
539 findRestorePoint(BarrierMBB, Barrier, RefsInMBB, RestoreIndex);
540 if (RestorePt == BarrierMBB->end())
541 return false;
542
543 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +0000544 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000545 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000546 unsigned SpillIndex = 0;
Evan Cheng06587492008-10-24 02:05:00 +0000547 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +0000548 int SS = -1;
Evan Cheng78dfef72008-10-25 00:52:41 +0000549 bool PrevSpilled = isAlreadySplit(CurrLI->reg, ValNo->id, SS);
550 if (ValNo->def == ~0U) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000551 // If it's defined by a phi, we must split just before the barrier.
552 MachineBasicBlock::iterator SpillPt =
553 findSpillPoint(BarrierMBB, Barrier, RefsInMBB, SpillIndex);
554 if (SpillPt == BarrierMBB->begin())
555 return false; // No gap to insert spill.
556 // Add spill.
Evan Cheng78dfef72008-10-25 00:52:41 +0000557 if (!PrevSpilled)
558 // If previously split, reuse the spill slot.
559 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
Evan Chengf5cd4f02008-10-23 20:43:13 +0000560 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
Evan Cheng06587492008-10-24 02:05:00 +0000561 SpillMI = prior(SpillPt);
562 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng78dfef72008-10-25 00:52:41 +0000563 } else if (!PrevSpilled) {
Evan Chengb3990d52008-10-27 23:21:01 +0000564 if (!DefMI)
565 // Def is dead. Do nothing.
566 return false;
Evan Cheng78dfef72008-10-25 00:52:41 +0000567 // If it's already split, just restore the value. There is no need to spill
568 // the def again.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000569 // Check if it's possible to insert a spill after the def MI.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000570 MachineBasicBlock::iterator SpillPt =
571 findNextEmptySlot(DefMBB, DefMI, SpillIndex);
572 if (SpillPt == DefMBB->end())
573 return false; // No gap to insert spill.
574 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
575
Evan Cheng78dfef72008-10-25 00:52:41 +0000576 // Add spill. The store instruction kills the register if def is before
577 // the barrier in the barrier block.
Evan Cheng06587492008-10-24 02:05:00 +0000578 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
579 DefMBB == BarrierMBB, SS, RC);
580 SpillMI = prior(SpillPt);
581 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000582 }
583
584 // Add restore.
585 // FIXME: Create live interval for stack slot.
586 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
587 MachineInstr *LoadMI = prior(RestorePt);
588 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
Evan Cheng06587492008-10-24 02:05:00 +0000589 RestoreMIs.insert(LoadMI);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000590
591 // If live interval is spilled in the same block as the barrier, just
592 // create a hole in the interval.
593 if (!DefMBB ||
Evan Cheng78dfef72008-10-25 00:52:41 +0000594 (SpillMI && SpillMI->getParent() == BarrierMBB)) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000595 UpdateIntervalForSplit(ValNo, LIs->getUseIndex(SpillIndex)+1,
596 LIs->getDefIndex(RestoreIndex));
597
598 // Record val# values are in the specific spill slot.
599 RecordSplit(CurrLI->reg, SpillIndex, RestoreIndex, SS);
600
601 ++NumSplit;
602 return true;
603 }
604
605 // Shrink wrap the live interval by walking up the CFG and find the
606 // new kills.
607 // Now let's find all the uses of the val#.
Evan Cheng06587492008-10-24 02:05:00 +0000608 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > Uses;
609 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > UseMIs;
610 SmallPtrSet<MachineBasicBlock*, 4> Seen;
611 SmallVector<MachineBasicBlock*, 4> UseMBBs;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000612 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(CurrLI->reg),
613 UE = MRI->use_end(); UI != UE; ++UI) {
614 MachineOperand &UseMO = UI.getOperand();
615 MachineInstr *UseMI = UseMO.getParent();
616 unsigned UseIdx = LIs->getInstructionIndex(UseMI);
617 LiveInterval::iterator ULR = CurrLI->FindLiveRangeContaining(UseIdx);
618 if (ULR->valno != ValNo)
619 continue;
620 MachineBasicBlock *UseMBB = UseMI->getParent();
Evan Cheng06587492008-10-24 02:05:00 +0000621 // Remember which other mbb's use this val#.
622 if (Seen.insert(UseMBB) && UseMBB != BarrierMBB)
623 UseMBBs.push_back(UseMBB);
624 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
625 UMII = Uses.find(UseMBB);
626 if (UMII != Uses.end()) {
627 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
628 UMII2 = UseMIs.find(UseMBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000629 UMII->second.push_back(&UseMO);
Evan Cheng06587492008-10-24 02:05:00 +0000630 UMII2->second.insert(UseMI);
631 } else {
632 SmallVector<MachineOperand*, 4> Ops;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000633 Ops.push_back(&UseMO);
Evan Cheng06587492008-10-24 02:05:00 +0000634 Uses.insert(std::make_pair(UseMBB, Ops));
635 SmallPtrSet<MachineInstr*, 4> MIs;
636 MIs.insert(UseMI);
637 UseMIs.insert(std::make_pair(UseMBB, MIs));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000638 }
639 }
640
641 // Walk up the predecessor chains.
642 SmallPtrSet<MachineBasicBlock*, 8> Visited;
Evan Chengaaf510c2008-10-26 07:49:03 +0000643 ShrinkWrapLiveInterval(ValNo, BarrierMBB, NULL, DefMBB, Visited,
Evan Cheng06587492008-10-24 02:05:00 +0000644 Uses, UseMIs, UseMBBs);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000645
646 // Remove live range from barrier to the restore. FIXME: Find a better
647 // point to re-start the live interval.
648 UpdateIntervalForSplit(ValNo, LIs->getUseIndex(BarrierIdx)+1,
649 LIs->getDefIndex(RestoreIndex));
650 // Record val# values are in the specific spill slot.
Evan Cheng06587492008-10-24 02:05:00 +0000651 RecordSplit(CurrLI->reg, SpillIndex, RestoreIndex, SS);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000652
653 ++NumSplit;
654 return true;
655}
656
657/// SplitRegLiveIntervals - Split all register live intervals that cross the
658/// barrier that's being processed.
659bool
660PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs) {
661 // First find all the virtual registers whose live intervals are intercepted
662 // by the current barrier.
663 SmallVector<LiveInterval*, 8> Intervals;
664 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng23066282008-10-27 07:14:50 +0000665 if (TII->IgnoreRegisterClassBarriers(*RC))
666 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000667 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
668 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
669 unsigned Reg = VRs[i];
670 if (!LIs->hasInterval(Reg))
671 continue;
672 LiveInterval *LI = &LIs->getInterval(Reg);
673 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
674 // Virtual register live interval is intercepted by the barrier. We
675 // should split and shrink wrap its interval if possible.
676 Intervals.push_back(LI);
677 }
678 }
679
680 // Process the affected live intervals.
681 bool Change = false;
682 while (!Intervals.empty()) {
683 LiveInterval *LI = Intervals.back();
684 Intervals.pop_back();
685 Change |= SplitRegLiveInterval(LI);
686 }
687
688 return Change;
689}
690
Evan Cheng09e8ca82008-10-20 21:44:59 +0000691bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000692 CurMF = &MF;
693 TM = &MF.getTarget();
694 TII = TM->getInstrInfo();
695 MFI = MF.getFrameInfo();
696 MRI = &MF.getRegInfo();
697 LIs = &getAnalysis<LiveIntervals>();
698
699 bool MadeChange = false;
700
701 // Make sure blocks are numbered in order.
702 MF.RenumberBlocks();
703
704 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
705 I != E; ++I) {
706 BarrierMBB = &*I;
707 for (MachineBasicBlock::reverse_iterator II = BarrierMBB->rbegin(),
708 EE = BarrierMBB->rend(); II != EE; ++II) {
709 Barrier = &*II;
710 const TargetRegisterClass **BarrierRCs =
711 Barrier->getDesc().getRegClassBarriers();
712 if (!BarrierRCs)
713 continue;
714 BarrierIdx = LIs->getInstructionIndex(Barrier);
715 MadeChange |= SplitRegLiveIntervals(BarrierRCs);
716 }
717 }
718
719 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +0000720}