blob: a45f7fcdfc2246a9275132d58e778d6cd2f5e1a9 [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 Chengae7fa5b2008-10-28 01:48:24 +000036static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden);
37
38STATISTIC(NumSplits, "Number of intervals split");
Evan Chengf5cd4f02008-10-23 20:43:13 +000039
Evan Cheng09e8ca82008-10-20 21:44:59 +000040namespace {
41 class VISIBILITY_HIDDEN PreAllocSplitting : public MachineFunctionPass {
Evan Chengf5cd4f02008-10-23 20:43:13 +000042 MachineFunction *CurMF;
43 const TargetMachine *TM;
44 const TargetInstrInfo *TII;
45 MachineFrameInfo *MFI;
46 MachineRegisterInfo *MRI;
47 LiveIntervals *LIs;
Evan Cheng09e8ca82008-10-20 21:44:59 +000048
Evan Chengf5cd4f02008-10-23 20:43:13 +000049 // Barrier - Current barrier being processed.
50 MachineInstr *Barrier;
51
52 // BarrierMBB - Basic block where the barrier resides in.
53 MachineBasicBlock *BarrierMBB;
54
55 // Barrier - Current barrier index.
56 unsigned BarrierIdx;
57
58 // CurrLI - Current live interval being split.
59 LiveInterval *CurrLI;
60
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
63 // slot was used.
64 std::map<std::pair<unsigned, unsigned>, int> LIValNoSSMap;
65
Evan Cheng06587492008-10-24 02:05:00 +000066 // RestoreMIs - All the restores inserted due to live interval splitting.
67 SmallPtrSet<MachineInstr*, 8> RestoreMIs;
68
Evan Cheng09e8ca82008-10-20 21:44:59 +000069 public:
70 static char ID;
71 PreAllocSplitting() : MachineFunctionPass(&ID) {}
72
73 virtual bool runOnMachineFunction(MachineFunction &MF);
74
75 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
76 AU.addRequired<LiveIntervals>();
77 AU.addPreserved<LiveIntervals>();
Evan Cheng09e8ca82008-10-20 21:44:59 +000078 AU.addPreserved<RegisterCoalescer>();
79 if (StrongPHIElim)
80 AU.addPreservedID(StrongPHIEliminationID);
81 else
82 AU.addPreservedID(PHIEliminationID);
Evan Cheng09e8ca82008-10-20 21:44:59 +000083 MachineFunctionPass::getAnalysisUsage(AU);
84 }
85
86 virtual void releaseMemory() {
Evan Chengf5cd4f02008-10-23 20:43:13 +000087 LIValNoSSMap.clear();
Evan Cheng06587492008-10-24 02:05:00 +000088 RestoreMIs.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +000089 }
90
91 virtual const char *getPassName() const {
92 return "Pre-Register Allocaton Live Interval Splitting";
93 }
Evan Chengf5cd4f02008-10-23 20:43:13 +000094
95 /// print - Implement the dump method.
96 virtual void print(std::ostream &O, const Module* M = 0) const {
97 LIs->print(O, M);
98 }
99
100 void print(std::ostream *O, const Module* M = 0) const {
101 if (O) print(*O, M);
102 }
103
104 private:
105 MachineBasicBlock::iterator
106 findNextEmptySlot(MachineBasicBlock*, MachineInstr*,
107 unsigned&);
108
109 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000110 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000111 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
112
113 MachineBasicBlock::iterator
Evan Chengf62ce372008-10-28 00:47:49 +0000114 findRestorePoint(MachineBasicBlock*, MachineInstr*, unsigned,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000115 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
116
117 void RecordSplit(unsigned, unsigned, unsigned, int);
118
119 bool isAlreadySplit(unsigned, unsigned, int&);
120
121 void UpdateIntervalForSplit(VNInfo*, unsigned, unsigned);
122
123 bool ShrinkWrapToLastUse(MachineBasicBlock*,
Evan Cheng06587492008-10-24 02:05:00 +0000124 SmallVector<MachineOperand*, 4>&,
125 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000126
Evan Chengaaf510c2008-10-26 07:49:03 +0000127 void ShrinkWrapLiveInterval(VNInfo*, MachineBasicBlock*, MachineBasicBlock*,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000128 MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 8>&,
Evan Cheng06587492008-10-24 02:05:00 +0000129 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >&,
130 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >&,
131 SmallVector<MachineBasicBlock*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000132
133 bool SplitRegLiveInterval(LiveInterval*);
134
135 bool SplitRegLiveIntervals(const TargetRegisterClass **);
Evan Cheng09e8ca82008-10-20 21:44:59 +0000136 };
137} // end anonymous namespace
138
139char PreAllocSplitting::ID = 0;
140
141static RegisterPass<PreAllocSplitting>
142X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
143
144const PassInfo *const llvm::PreAllocSplittingID = &X;
145
Evan Chengf5cd4f02008-10-23 20:43:13 +0000146
147/// findNextEmptySlot - Find a gap after the given machine instruction in the
148/// instruction index map. If there isn't one, return end().
149MachineBasicBlock::iterator
150PreAllocSplitting::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));
155 if (Index) {
156 SpotIndex = Index;
157 return MII;
158 }
159 }
160 return MBB->end();
161}
162
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
166/// none is found.
167MachineBasicBlock::iterator
168PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000169 MachineInstr *DefMI,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000170 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
171 unsigned &SpillIndex) {
172 MachineBasicBlock::iterator Pt = MBB->begin();
173
174 // Go top down if RefsInMBB is empty.
Evan Cheng1f08cc22008-10-28 05:28:21 +0000175 if (RefsInMBB.empty() && !DefMI) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000176 MachineBasicBlock::iterator MII = MBB->begin();
177 MachineBasicBlock::iterator EndPt = MI;
178 do {
179 ++MII;
180 unsigned Index = LIs->getInstructionIndex(MII);
181 unsigned Gap = LIs->findGapBeforeInstr(Index);
182 if (Gap) {
183 Pt = MII;
184 SpillIndex = Gap;
185 break;
186 }
187 } while (MII != EndPt);
188 } else {
189 MachineBasicBlock::iterator MII = MI;
Evan Cheng1f08cc22008-10-28 05:28:21 +0000190 MachineBasicBlock::iterator EndPt = DefMI
191 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
192 while (MII != EndPt && !RefsInMBB.count(MII)) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000193 unsigned Index = LIs->getInstructionIndex(MII);
194 if (LIs->hasGapBeforeInstr(Index)) {
195 Pt = MII;
196 SpillIndex = LIs->findGapBeforeInstr(Index, true);
197 }
198 --MII;
199 }
200 }
201
202 return Pt;
203}
204
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
208/// found.
209MachineBasicBlock::iterator
210PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Chengf62ce372008-10-28 00:47:49 +0000211 unsigned LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000212 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
213 unsigned &RestoreIndex) {
214 MachineBasicBlock::iterator Pt = MBB->end();
Evan Chengf62ce372008-10-28 00:47:49 +0000215 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000216
Evan Chengf62ce372008-10-28 00:47:49 +0000217 // 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) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000220 MachineBasicBlock::iterator MII = MBB->end();
221 MachineBasicBlock::iterator EndPt = MI;
222 do {
223 --MII;
224 unsigned Index = LIs->getInstructionIndex(MII);
Evan Cheng56ab0de2008-10-24 18:46:44 +0000225 unsigned Gap = LIs->findGapBeforeInstr(Index);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000226 if (Gap) {
227 Pt = MII;
228 RestoreIndex = Gap;
229 break;
230 }
231 } while (MII != EndPt);
232 } else {
233 MachineBasicBlock::iterator MII = MI;
234 MII = ++MII;
Evan Chengf62ce372008-10-28 00:47:49 +0000235 // FIXME: Limit the number of instructions to examine to reduce
236 // compile time?
Evan Chengf5cd4f02008-10-23 20:43:13 +0000237 while (MII != MBB->end()) {
238 unsigned Index = LIs->getInstructionIndex(MII);
Evan Chengf62ce372008-10-28 00:47:49 +0000239 if (Index > LastIdx)
240 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000241 unsigned Gap = LIs->findGapBeforeInstr(Index);
242 if (Gap) {
243 Pt = MII;
244 RestoreIndex = Gap;
245 }
246 if (RefsInMBB.count(MII))
247 break;
248 ++MII;
249 }
250 }
251
252 return Pt;
253}
254
255/// RecordSplit - Given a register live interval is split, remember the spill
256/// slot where the val#s are in.
257void PreAllocSplitting::RecordSplit(unsigned Reg, unsigned SpillIndex,
258 unsigned RestoreIndex, int SS) {
Evan Cheng06587492008-10-24 02:05:00 +0000259 const LiveRange *LR = NULL;
260 if (SpillIndex) {
261 LR = CurrLI->getLiveRangeContaining(LIs->getUseIndex(SpillIndex));
262 LIValNoSSMap.insert(std::make_pair(std::make_pair(CurrLI->reg,
263 LR->valno->id), SS));
264 }
265 LR = CurrLI->getLiveRangeContaining(LIs->getDefIndex(RestoreIndex));
266 LIValNoSSMap.insert(std::make_pair(std::make_pair(CurrLI->reg,
267 LR->valno->id), SS));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000268}
269
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.
272bool 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())
276 return false;
277 SS = I->second;
278 return true;
279}
280
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.
284void
285PreAllocSplitting::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;
292
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));
Evan Cheng06587492008-10-24 02:05:00 +0000297 if (CurrLI->isKill(ValNo, LR->end))
298 AfterKills.push_back(LR->end);
299
Evan Chengf5cd4f02008-10-23 20:43:13 +0000300 assert(LR->contains(SplitIndex));
Evan Cheng06587492008-10-24 02:05:00 +0000301 if (SplitIndex > LR->start) {
302 Before.push_back(std::make_pair(LR->start, SplitIndex));
303 BeforeKills.push_back(SplitIndex);
304 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000305 Processed.insert(LR);
306
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);
312
313 while (!WorkList.empty()) {
314 MBB = WorkList.back();
315 WorkList.pop_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);
323 if (LR->end > Idx) {
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);
332 }
333 }
334 Processed.insert(LR);
335 }
336 }
337
338 for (LiveInterval::iterator I = CurrLI->begin(), E = CurrLI->end();
339 I != E; ++I) {
340 LiveRange *LR = I;
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);
345 }
346 }
347
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);
Evan Cheng06587492008-10-24 02:05:00 +0000354 VNInfo *BValNo = (Before.empty())
355 ? NULL
356 : CurrLI->getNextValue(AfterDef, AfterCopy, LIs->getVNInfoAllocator());
357 if (BValNo)
358 CurrLI->addKills(BValNo, BeforeKills);
359
360 VNInfo *AValNo = (After.empty())
361 ? NULL
362 : CurrLI->getNextValue(JoinIndex,0, LIs->getVNInfoAllocator());
363 if (AValNo) {
364 AValNo->hasPHIKill = HasPHIKill;
365 CurrLI->addKills(AValNo, AfterKills);
366 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000367
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));
372 }
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));
377 }
378}
379
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.
384bool
385PreAllocSplitting::ShrinkWrapToLastUse(MachineBasicBlock *MBB,
Evan Cheng06587492008-10-24 02:05:00 +0000386 SmallVector<MachineOperand*, 4> &Uses,
387 SmallPtrSet<MachineInstr*, 4> &UseMIs) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000388 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.
393 LastMO = Uses[0];
394 LastMI = LastMO->getParent();
395 } else {
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000396 MachineBasicBlock::iterator MEE = MBB->begin();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000397 MachineBasicBlock::iterator MII;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000398 if (MBB == BarrierMBB)
Evan Chengf5cd4f02008-10-23 20:43:13 +0000399 MII = Barrier;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000400 else
Evan Chengf5cd4f02008-10-23 20:43:13 +0000401 MII = MBB->end();
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000402 while (--MII != MEE) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000403 MachineInstr *UseMI = &*MII;
404 if (!UseMIs.count(UseMI))
405 continue;
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) {
409 LastMO = &MO;
410 break;
411 }
412 }
413 LastMI = UseMI;
414 break;
415 }
416 }
417
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;
421 if (LastMI) {
422 RangeStart = LIs->getUseIndex(LIs->getInstructionIndex(LastMI))+1;
423 assert(!LastMO->isKill() && "Last use already terminates the interval?");
424 LastMO->setIsKill();
425 } else {
426 assert(MBB == BarrierMBB);
427 RangeStart = LIs->getMBBStartIdx(MBB);
428 }
429 if (MBB == BarrierMBB)
Evan Cheng06587492008-10-24 02:05:00 +0000430 RangeEnd = LIs->getUseIndex(BarrierIdx)+1;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000431 CurrLI->removeRange(RangeStart, RangeEnd);
432
433 // Return true if the last use becomes a new kill.
434 return LastMI;
435}
436
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.
440void
Evan Chengaaf510c2008-10-26 07:49:03 +0000441PreAllocSplitting::ShrinkWrapLiveInterval(VNInfo *ValNo, MachineBasicBlock *MBB,
442 MachineBasicBlock *SuccMBB, MachineBasicBlock *DefMBB,
Evan Cheng06587492008-10-24 02:05:00 +0000443 SmallPtrSet<MachineBasicBlock*, 8> &Visited,
444 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > &Uses,
445 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > &UseMIs,
446 SmallVector<MachineBasicBlock*, 4> &UseMBBs) {
Evan Chengaaf510c2008-10-26 07:49:03 +0000447 if (Visited.count(MBB))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000448 return;
449
Evan Chengaaf510c2008-10-26 07:49:03 +0000450 // 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
452 // processed.
Evan Chengf62ce372008-10-28 00:47:49 +0000453 if (MBB != BarrierMBB) {
454 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
455 SE = MBB->succ_end(); SI != SE; ++SI) {
456 MachineBasicBlock *SMBB = *SI;
457 if (SMBB == SuccMBB)
458 continue;
459 if (CurrLI->liveAt(LIs->getMBBStartIdx(SMBB)))
460 return;
461 }
Evan Chengaaf510c2008-10-26 07:49:03 +0000462 }
463
464 Visited.insert(MBB);
465
Evan Cheng06587492008-10-24 02:05:00 +0000466 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
467 UMII = Uses.find(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000468 if (UMII != Uses.end()) {
469 // At least one use in this mbb, lets look for the kill.
Evan Cheng06587492008-10-24 02:05:00 +0000470 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
471 UMII2 = UseMIs.find(MBB);
472 if (ShrinkWrapToLastUse(MBB, UMII->second, UMII2->second))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000473 // Found a kill, shrink wrapping of this path ends here.
474 return;
Evan Cheng06587492008-10-24 02:05:00 +0000475 } 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
483 // remove the def.
484 LIs->RemoveMachineInstrFromMaps(DefMI);
485 DefMI->eraseFromParent();
486 CurrLI->removeRange(ValNo->def, LIs->getMBBEndIdx(MBB)+1);
487 }
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000488 } 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);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000492 } else {
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000493 // Remove entire live range of the mbb out of the live interval.
Evan Cheng06587492008-10-24 02:05:00 +0000494 CurrLI->removeRange(LIs->getMBBStartIdx(MBB), LIs->getMBBEndIdx(MBB)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000495 }
496
497 if (MBB == DefMBB)
498 // Reached the def mbb, stop traversing this path further.
499 return;
500
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;
505 if (Pred == MBB)
506 continue;
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.
510 continue;
Evan Chengaaf510c2008-10-26 07:49:03 +0000511 ShrinkWrapLiveInterval(ValNo, Pred, MBB, DefMBB, Visited,
512 Uses, UseMIs, UseMBBs);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000513 }
514
515 return;
516}
517
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.
521bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
522 CurrLI = LI;
523
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;
528
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?");
532 abort();
533 }
534
Evan Cheng06587492008-10-24 02:05:00 +0000535 // 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))
539 return false;
540
Evan Chengf5cd4f02008-10-23 20:43:13 +0000541 // 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);
548 }
549
550 // Find a point to restore the value after the barrier.
551 unsigned RestoreIndex;
552 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +0000553 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000554 if (RestorePt == BarrierMBB->end())
555 return false;
556
557 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +0000558 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000559 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000560 unsigned SpillIndex = 0;
Evan Cheng06587492008-10-24 02:05:00 +0000561 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +0000562 int SS = -1;
Evan Cheng78dfef72008-10-25 00:52:41 +0000563 bool PrevSpilled = isAlreadySplit(CurrLI->reg, ValNo->id, SS);
564 if (ValNo->def == ~0U) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000565 // If it's defined by a phi, we must split just before the barrier.
566 MachineBasicBlock::iterator SpillPt =
Evan Cheng1f08cc22008-10-28 05:28:21 +0000567 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000568 if (SpillPt == BarrierMBB->begin())
569 return false; // No gap to insert spill.
570 // Add spill.
Evan Cheng78dfef72008-10-25 00:52:41 +0000571 if (!PrevSpilled)
572 // If previously split, reuse the spill slot.
573 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
Evan Chengf5cd4f02008-10-23 20:43:13 +0000574 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
Evan Cheng06587492008-10-24 02:05:00 +0000575 SpillMI = prior(SpillPt);
576 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng78dfef72008-10-25 00:52:41 +0000577 } else if (!PrevSpilled) {
Evan Chengb3990d52008-10-27 23:21:01 +0000578 if (!DefMI)
579 // Def is dead. Do nothing.
580 return false;
Evan Cheng78dfef72008-10-25 00:52:41 +0000581 // If it's already split, just restore the value. There is no need to spill
582 // the def again.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000583 // Check if it's possible to insert a spill after the def MI.
Evan Cheng1f08cc22008-10-28 05:28:21 +0000584 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.
590 } else {
591 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
592 if (SpillPt == DefMBB->end())
593 return false; // No gap to insert spill.
594 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000595 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
596
Evan Cheng78dfef72008-10-25 00:52:41 +0000597 // Add spill. The store instruction kills the register if def is before
598 // the barrier in the barrier block.
Evan Cheng06587492008-10-24 02:05:00 +0000599 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
600 DefMBB == BarrierMBB, SS, RC);
601 SpillMI = prior(SpillPt);
602 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000603 }
604
605 // Add restore.
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);
Evan Cheng06587492008-10-24 02:05:00 +0000610 RestoreMIs.insert(LoadMI);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000611
612 // If live interval is spilled in the same block as the barrier, just
613 // create a hole in the interval.
614 if (!DefMBB ||
Evan Cheng78dfef72008-10-25 00:52:41 +0000615 (SpillMI && SpillMI->getParent() == BarrierMBB)) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000616 UpdateIntervalForSplit(ValNo, LIs->getUseIndex(SpillIndex)+1,
617 LIs->getDefIndex(RestoreIndex));
618
619 // Record val# values are in the specific spill slot.
620 RecordSplit(CurrLI->reg, SpillIndex, RestoreIndex, SS);
621
Evan Chengae7fa5b2008-10-28 01:48:24 +0000622 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000623 return true;
624 }
625
626 // Shrink wrap the live interval by walking up the CFG and find the
627 // new kills.
628 // Now let's find all the uses of the val#.
Evan Cheng06587492008-10-24 02:05:00 +0000629 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > Uses;
630 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > UseMIs;
631 SmallPtrSet<MachineBasicBlock*, 4> Seen;
632 SmallVector<MachineBasicBlock*, 4> UseMBBs;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000633 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)
640 continue;
641 MachineBasicBlock *UseMBB = UseMI->getParent();
Evan Cheng06587492008-10-24 02:05:00 +0000642 // 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);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000650 UMII->second.push_back(&UseMO);
Evan Cheng06587492008-10-24 02:05:00 +0000651 UMII2->second.insert(UseMI);
652 } else {
653 SmallVector<MachineOperand*, 4> Ops;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000654 Ops.push_back(&UseMO);
Evan Cheng06587492008-10-24 02:05:00 +0000655 Uses.insert(std::make_pair(UseMBB, Ops));
656 SmallPtrSet<MachineInstr*, 4> MIs;
657 MIs.insert(UseMI);
658 UseMIs.insert(std::make_pair(UseMBB, MIs));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000659 }
660 }
661
662 // Walk up the predecessor chains.
663 SmallPtrSet<MachineBasicBlock*, 8> Visited;
Evan Chengaaf510c2008-10-26 07:49:03 +0000664 ShrinkWrapLiveInterval(ValNo, BarrierMBB, NULL, DefMBB, Visited,
Evan Cheng06587492008-10-24 02:05:00 +0000665 Uses, UseMIs, UseMBBs);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000666
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.
Evan Cheng06587492008-10-24 02:05:00 +0000672 RecordSplit(CurrLI->reg, SpillIndex, RestoreIndex, SS);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000673
Evan Chengae7fa5b2008-10-28 01:48:24 +0000674 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000675 return true;
676}
677
678/// SplitRegLiveIntervals - Split all register live intervals that cross the
679/// barrier that's being processed.
680bool
681PreAllocSplitting::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) {
Evan Cheng23066282008-10-27 07:14:50 +0000686 if (TII->IgnoreRegisterClassBarriers(*RC))
687 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000688 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))
692 continue;
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);
698 }
699 }
700
701 // Process the affected live intervals.
702 bool Change = false;
703 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +0000704 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
705 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000706 LiveInterval *LI = Intervals.back();
707 Intervals.pop_back();
708 Change |= SplitRegLiveInterval(LI);
709 }
710
711 return Change;
712}
713
Evan Cheng09e8ca82008-10-20 21:44:59 +0000714bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000715 CurMF = &MF;
716 TM = &MF.getTarget();
717 TII = TM->getInstrInfo();
718 MFI = MF.getFrameInfo();
719 MRI = &MF.getRegInfo();
720 LIs = &getAnalysis<LiveIntervals>();
721
722 bool MadeChange = false;
723
724 // Make sure blocks are numbered in order.
725 MF.RenumberBlocks();
726
727 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
728 I != E; ++I) {
729 BarrierMBB = &*I;
730 for (MachineBasicBlock::reverse_iterator II = BarrierMBB->rbegin(),
731 EE = BarrierMBB->rend(); II != EE; ++II) {
732 Barrier = &*II;
733 const TargetRegisterClass **BarrierRCs =
734 Barrier->getDesc().getRegClassBarriers();
735 if (!BarrierRCs)
736 continue;
737 BarrierIdx = LIs->getInstructionIndex(Barrier);
738 MadeChange |= SplitRegLiveIntervals(BarrierRCs);
739 }
740 }
741
742 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +0000743}