blob: 5c1c2230c854f5dc8ea3cad59e8d7115fc0acaf1 [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
Evan Chengf62ce372008-10-28 00:47:49 +0000112 findRestorePoint(MachineBasicBlock*, MachineInstr*, unsigned,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000113 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,
Evan Chengf62ce372008-10-28 00:47:49 +0000206 unsigned LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000207 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
208 unsigned &RestoreIndex) {
209 MachineBasicBlock::iterator Pt = MBB->end();
Evan Chengf62ce372008-10-28 00:47:49 +0000210 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000211
Evan Chengf62ce372008-10-28 00:47:49 +0000212 // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond
213 // the last index in the live range.
214 if (RefsInMBB.empty() && LastIdx >= EndIdx) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000215 MachineBasicBlock::iterator MII = MBB->end();
216 MachineBasicBlock::iterator EndPt = MI;
217 do {
218 --MII;
219 unsigned Index = LIs->getInstructionIndex(MII);
Evan Cheng56ab0de2008-10-24 18:46:44 +0000220 unsigned Gap = LIs->findGapBeforeInstr(Index);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000221 if (Gap) {
222 Pt = MII;
223 RestoreIndex = Gap;
224 break;
225 }
226 } while (MII != EndPt);
227 } else {
228 MachineBasicBlock::iterator MII = MI;
229 MII = ++MII;
Evan Chengf62ce372008-10-28 00:47:49 +0000230 // FIXME: Limit the number of instructions to examine to reduce
231 // compile time?
Evan Chengf5cd4f02008-10-23 20:43:13 +0000232 while (MII != MBB->end()) {
233 unsigned Index = LIs->getInstructionIndex(MII);
Evan Chengf62ce372008-10-28 00:47:49 +0000234 if (Index > LastIdx)
235 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000236 unsigned Gap = LIs->findGapBeforeInstr(Index);
237 if (Gap) {
238 Pt = MII;
239 RestoreIndex = Gap;
240 }
241 if (RefsInMBB.count(MII))
242 break;
243 ++MII;
244 }
245 }
246
247 return Pt;
248}
249
250/// RecordSplit - Given a register live interval is split, remember the spill
251/// slot where the val#s are in.
252void PreAllocSplitting::RecordSplit(unsigned Reg, unsigned SpillIndex,
253 unsigned RestoreIndex, int SS) {
Evan Cheng06587492008-10-24 02:05:00 +0000254 const LiveRange *LR = NULL;
255 if (SpillIndex) {
256 LR = CurrLI->getLiveRangeContaining(LIs->getUseIndex(SpillIndex));
257 LIValNoSSMap.insert(std::make_pair(std::make_pair(CurrLI->reg,
258 LR->valno->id), SS));
259 }
260 LR = CurrLI->getLiveRangeContaining(LIs->getDefIndex(RestoreIndex));
261 LIValNoSSMap.insert(std::make_pair(std::make_pair(CurrLI->reg,
262 LR->valno->id), SS));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000263}
264
265/// isAlreadySplit - Return if a given val# of a register live interval is already
266/// split. Also return by reference the spill stock where the value is.
267bool PreAllocSplitting::isAlreadySplit(unsigned Reg, unsigned ValNoId, int &SS){
268 std::map<std::pair<unsigned, unsigned>, int>::iterator I =
269 LIValNoSSMap.find(std::make_pair(Reg, ValNoId));
270 if (I == LIValNoSSMap.end())
271 return false;
272 SS = I->second;
273 return true;
274}
275
276/// UpdateIntervalForSplit - Given the specified val# of the current live
277/// interval is being split, and the split and rejoin indices, update the live
278/// interval accordingly.
279void
280PreAllocSplitting::UpdateIntervalForSplit(VNInfo *ValNo, unsigned SplitIndex,
281 unsigned JoinIndex) {
282 SmallVector<std::pair<unsigned,unsigned>, 4> Before;
283 SmallVector<std::pair<unsigned,unsigned>, 4> After;
284 SmallVector<unsigned, 4> BeforeKills;
285 SmallVector<unsigned, 4> AfterKills;
286 SmallPtrSet<const LiveRange*, 4> Processed;
287
288 // First, let's figure out which parts of the live interval is now defined
289 // by the restore, which are defined by the original definition.
290 const LiveRange *LR = CurrLI->getLiveRangeContaining(JoinIndex);
291 After.push_back(std::make_pair(JoinIndex, LR->end));
Evan Cheng06587492008-10-24 02:05:00 +0000292 if (CurrLI->isKill(ValNo, LR->end))
293 AfterKills.push_back(LR->end);
294
Evan Chengf5cd4f02008-10-23 20:43:13 +0000295 assert(LR->contains(SplitIndex));
Evan Cheng06587492008-10-24 02:05:00 +0000296 if (SplitIndex > LR->start) {
297 Before.push_back(std::make_pair(LR->start, SplitIndex));
298 BeforeKills.push_back(SplitIndex);
299 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000300 Processed.insert(LR);
301
302 SmallVector<MachineBasicBlock*, 4> WorkList;
303 MachineBasicBlock *MBB = LIs->getMBBFromIndex(LR->end-1);
304 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
305 SE = MBB->succ_end(); SI != SE; ++SI)
306 WorkList.push_back(*SI);
307
308 while (!WorkList.empty()) {
309 MBB = WorkList.back();
310 WorkList.pop_back();
311 unsigned Idx = LIs->getMBBStartIdx(MBB);
312 LR = CurrLI->getLiveRangeContaining(Idx);
313 if (LR && LR->valno == ValNo && !Processed.count(LR)) {
314 After.push_back(std::make_pair(LR->start, LR->end));
315 if (CurrLI->isKill(ValNo, LR->end))
316 AfterKills.push_back(LR->end);
317 Idx = LIs->getMBBEndIdx(MBB);
318 if (LR->end > Idx) {
319 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
320 SE = MBB->succ_end(); SI != SE; ++SI)
321 WorkList.push_back(*SI);
322 if (LR->end > Idx+1) {
323 MBB = LIs->getMBBFromIndex(LR->end-1);
324 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
325 SE = MBB->succ_end(); SI != SE; ++SI)
326 WorkList.push_back(*SI);
327 }
328 }
329 Processed.insert(LR);
330 }
331 }
332
333 for (LiveInterval::iterator I = CurrLI->begin(), E = CurrLI->end();
334 I != E; ++I) {
335 LiveRange *LR = I;
336 if (LR->valno == ValNo && !Processed.count(LR)) {
337 Before.push_back(std::make_pair(LR->start, LR->end));
338 if (CurrLI->isKill(ValNo, LR->end))
339 BeforeKills.push_back(LR->end);
340 }
341 }
342
343 // Now create new val#s to represent the live ranges defined by the old def
344 // those defined by the restore.
345 unsigned AfterDef = ValNo->def;
346 MachineInstr *AfterCopy = ValNo->copy;
347 bool HasPHIKill = ValNo->hasPHIKill;
348 CurrLI->removeValNo(ValNo);
Evan Cheng06587492008-10-24 02:05:00 +0000349 VNInfo *BValNo = (Before.empty())
350 ? NULL
351 : CurrLI->getNextValue(AfterDef, AfterCopy, LIs->getVNInfoAllocator());
352 if (BValNo)
353 CurrLI->addKills(BValNo, BeforeKills);
354
355 VNInfo *AValNo = (After.empty())
356 ? NULL
357 : CurrLI->getNextValue(JoinIndex,0, LIs->getVNInfoAllocator());
358 if (AValNo) {
359 AValNo->hasPHIKill = HasPHIKill;
360 CurrLI->addKills(AValNo, AfterKills);
361 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000362
363 for (unsigned i = 0, e = Before.size(); i != e; ++i) {
364 unsigned Start = Before[i].first;
365 unsigned End = Before[i].second;
366 CurrLI->addRange(LiveRange(Start, End, BValNo));
367 }
368 for (unsigned i = 0, e = After.size(); i != e; ++i) {
369 unsigned Start = After[i].first;
370 unsigned End = After[i].second;
371 CurrLI->addRange(LiveRange(Start, End, AValNo));
372 }
373}
374
375/// ShrinkWrapToLastUse - There are uses of the current live interval in the
376/// given block, shrink wrap the live interval to the last use (i.e. remove
377/// from last use to the end of the mbb). In case mbb is the where the barrier
378/// is, remove from the last use to the barrier.
379bool
380PreAllocSplitting::ShrinkWrapToLastUse(MachineBasicBlock *MBB,
Evan Cheng06587492008-10-24 02:05:00 +0000381 SmallVector<MachineOperand*, 4> &Uses,
382 SmallPtrSet<MachineInstr*, 4> &UseMIs) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000383 MachineOperand *LastMO = 0;
384 MachineInstr *LastMI = 0;
385 if (MBB != BarrierMBB && Uses.size() == 1) {
386 // Single use, no need to traverse the block. We can't assume this for the
387 // barrier bb though since the use is probably below the barrier.
388 LastMO = Uses[0];
389 LastMI = LastMO->getParent();
390 } else {
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000391 MachineBasicBlock::iterator MEE = MBB->begin();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000392 MachineBasicBlock::iterator MII;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000393 if (MBB == BarrierMBB)
Evan Chengf5cd4f02008-10-23 20:43:13 +0000394 MII = Barrier;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000395 else
Evan Chengf5cd4f02008-10-23 20:43:13 +0000396 MII = MBB->end();
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000397 while (--MII != MEE) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000398 MachineInstr *UseMI = &*MII;
399 if (!UseMIs.count(UseMI))
400 continue;
401 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
402 MachineOperand &MO = UseMI->getOperand(i);
403 if (MO.isReg() && MO.getReg() == CurrLI->reg) {
404 LastMO = &MO;
405 break;
406 }
407 }
408 LastMI = UseMI;
409 break;
410 }
411 }
412
413 // Cut off live range from last use (or beginning of the mbb if there
414 // are no uses in it) to the end of the mbb.
415 unsigned RangeStart, RangeEnd = LIs->getMBBEndIdx(MBB)+1;
416 if (LastMI) {
417 RangeStart = LIs->getUseIndex(LIs->getInstructionIndex(LastMI))+1;
418 assert(!LastMO->isKill() && "Last use already terminates the interval?");
419 LastMO->setIsKill();
420 } else {
421 assert(MBB == BarrierMBB);
422 RangeStart = LIs->getMBBStartIdx(MBB);
423 }
424 if (MBB == BarrierMBB)
Evan Cheng06587492008-10-24 02:05:00 +0000425 RangeEnd = LIs->getUseIndex(BarrierIdx)+1;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000426 CurrLI->removeRange(RangeStart, RangeEnd);
427
428 // Return true if the last use becomes a new kill.
429 return LastMI;
430}
431
432/// ShrinkWrapLiveInterval - Recursively traverse the predecessor
433/// chain to find the new 'kills' and shrink wrap the live interval to the
434/// new kill indices.
435void
Evan Chengaaf510c2008-10-26 07:49:03 +0000436PreAllocSplitting::ShrinkWrapLiveInterval(VNInfo *ValNo, MachineBasicBlock *MBB,
437 MachineBasicBlock *SuccMBB, MachineBasicBlock *DefMBB,
Evan Cheng06587492008-10-24 02:05:00 +0000438 SmallPtrSet<MachineBasicBlock*, 8> &Visited,
439 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > &Uses,
440 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > &UseMIs,
441 SmallVector<MachineBasicBlock*, 4> &UseMBBs) {
Evan Chengaaf510c2008-10-26 07:49:03 +0000442 if (Visited.count(MBB))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000443 return;
444
Evan Chengaaf510c2008-10-26 07:49:03 +0000445 // If live interval is live in another successor path, then we can't process
446 // this block. But we may able to do so after all the successors have been
447 // processed.
Evan Chengf62ce372008-10-28 00:47:49 +0000448 if (MBB != BarrierMBB) {
449 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
450 SE = MBB->succ_end(); SI != SE; ++SI) {
451 MachineBasicBlock *SMBB = *SI;
452 if (SMBB == SuccMBB)
453 continue;
454 if (CurrLI->liveAt(LIs->getMBBStartIdx(SMBB)))
455 return;
456 }
Evan Chengaaf510c2008-10-26 07:49:03 +0000457 }
458
459 Visited.insert(MBB);
460
Evan Cheng06587492008-10-24 02:05:00 +0000461 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
462 UMII = Uses.find(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000463 if (UMII != Uses.end()) {
464 // At least one use in this mbb, lets look for the kill.
Evan Cheng06587492008-10-24 02:05:00 +0000465 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
466 UMII2 = UseMIs.find(MBB);
467 if (ShrinkWrapToLastUse(MBB, UMII->second, UMII2->second))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000468 // Found a kill, shrink wrapping of this path ends here.
469 return;
Evan Cheng06587492008-10-24 02:05:00 +0000470 } else if (MBB == DefMBB) {
471 assert(LIValNoSSMap.find(std::make_pair(CurrLI->reg, ValNo->id)) !=
472 LIValNoSSMap.end() && "Why wasn't def spilled?");
473 // There are no uses after the def.
474 MachineInstr *DefMI = LIs->getInstructionFromIndex(ValNo->def);
475 assert(RestoreMIs.count(DefMI) && "Not defined by a join?");
476 if (UseMBBs.empty()) {
477 // The only use must be below barrier in the barrier block. It's safe to
478 // remove the def.
479 LIs->RemoveMachineInstrFromMaps(DefMI);
480 DefMI->eraseFromParent();
481 CurrLI->removeRange(ValNo->def, LIs->getMBBEndIdx(MBB)+1);
482 }
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000483 } else if (MBB == BarrierMBB) {
484 // Remove entire live range from start of mbb to barrier.
485 CurrLI->removeRange(LIs->getMBBStartIdx(MBB),
486 LIs->getUseIndex(BarrierIdx)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000487 } else {
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000488 // Remove entire live range of the mbb out of the live interval.
Evan Cheng06587492008-10-24 02:05:00 +0000489 CurrLI->removeRange(LIs->getMBBStartIdx(MBB), LIs->getMBBEndIdx(MBB)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000490 }
491
492 if (MBB == DefMBB)
493 // Reached the def mbb, stop traversing this path further.
494 return;
495
496 // Traverse the pathes up the predecessor chains further.
497 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
498 PE = MBB->pred_end(); PI != PE; ++PI) {
499 MachineBasicBlock *Pred = *PI;
500 if (Pred == MBB)
501 continue;
502 if (Pred == DefMBB && ValNo->hasPHIKill)
503 // Pred is the def bb and the def reaches other val#s, we must
504 // allow the value to be live out of the bb.
505 continue;
Evan Chengaaf510c2008-10-26 07:49:03 +0000506 ShrinkWrapLiveInterval(ValNo, Pred, MBB, DefMBB, Visited,
507 Uses, UseMIs, UseMBBs);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000508 }
509
510 return;
511}
512
513/// SplitRegLiveInterval - Split (spill and restore) the given live interval
514/// so it would not cross the barrier that's being processed. Shrink wrap
515/// (minimize) the live interval to the last uses.
516bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
517 CurrLI = LI;
518
519 // Find live range where current interval cross the barrier.
520 LiveInterval::iterator LR =
521 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
522 VNInfo *ValNo = LR->valno;
523
524 if (ValNo->def == ~1U) {
525 // Defined by a dead def? How can this be?
526 assert(0 && "Val# is defined by a dead def?");
527 abort();
528 }
529
Evan Cheng06587492008-10-24 02:05:00 +0000530 // FIXME: For now, if definition is rematerializable, do not split.
531 MachineInstr *DefMI = (ValNo->def != ~0U)
532 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
533 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
534 return false;
535
Evan Chengf5cd4f02008-10-23 20:43:13 +0000536 // Find all references in the barrier mbb.
537 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
538 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
539 E = MRI->reg_end(); I != E; ++I) {
540 MachineInstr *RefMI = &*I;
541 if (RefMI->getParent() == BarrierMBB)
542 RefsInMBB.insert(RefMI);
543 }
544
545 // Find a point to restore the value after the barrier.
546 unsigned RestoreIndex;
547 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +0000548 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000549 if (RestorePt == BarrierMBB->end())
550 return false;
551
552 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +0000553 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000554 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000555 unsigned SpillIndex = 0;
Evan Cheng06587492008-10-24 02:05:00 +0000556 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +0000557 int SS = -1;
Evan Cheng78dfef72008-10-25 00:52:41 +0000558 bool PrevSpilled = isAlreadySplit(CurrLI->reg, ValNo->id, SS);
559 if (ValNo->def == ~0U) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000560 // If it's defined by a phi, we must split just before the barrier.
561 MachineBasicBlock::iterator SpillPt =
562 findSpillPoint(BarrierMBB, Barrier, RefsInMBB, SpillIndex);
563 if (SpillPt == BarrierMBB->begin())
564 return false; // No gap to insert spill.
565 // Add spill.
Evan Cheng78dfef72008-10-25 00:52:41 +0000566 if (!PrevSpilled)
567 // If previously split, reuse the spill slot.
568 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
Evan Chengf5cd4f02008-10-23 20:43:13 +0000569 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
Evan Cheng06587492008-10-24 02:05:00 +0000570 SpillMI = prior(SpillPt);
571 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng78dfef72008-10-25 00:52:41 +0000572 } else if (!PrevSpilled) {
Evan Chengb3990d52008-10-27 23:21:01 +0000573 if (!DefMI)
574 // Def is dead. Do nothing.
575 return false;
Evan Cheng78dfef72008-10-25 00:52:41 +0000576 // If it's already split, just restore the value. There is no need to spill
577 // the def again.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000578 // Check if it's possible to insert a spill after the def MI.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000579 MachineBasicBlock::iterator SpillPt =
580 findNextEmptySlot(DefMBB, DefMI, SpillIndex);
581 if (SpillPt == DefMBB->end())
582 return false; // No gap to insert spill.
583 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
584
Evan Cheng78dfef72008-10-25 00:52:41 +0000585 // Add spill. The store instruction kills the register if def is before
586 // the barrier in the barrier block.
Evan Cheng06587492008-10-24 02:05:00 +0000587 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
588 DefMBB == BarrierMBB, SS, RC);
589 SpillMI = prior(SpillPt);
590 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000591 }
592
593 // Add restore.
594 // FIXME: Create live interval for stack slot.
595 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
596 MachineInstr *LoadMI = prior(RestorePt);
597 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
Evan Cheng06587492008-10-24 02:05:00 +0000598 RestoreMIs.insert(LoadMI);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000599
600 // If live interval is spilled in the same block as the barrier, just
601 // create a hole in the interval.
602 if (!DefMBB ||
Evan Cheng78dfef72008-10-25 00:52:41 +0000603 (SpillMI && SpillMI->getParent() == BarrierMBB)) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000604 UpdateIntervalForSplit(ValNo, LIs->getUseIndex(SpillIndex)+1,
605 LIs->getDefIndex(RestoreIndex));
606
607 // Record val# values are in the specific spill slot.
608 RecordSplit(CurrLI->reg, SpillIndex, RestoreIndex, SS);
609
610 ++NumSplit;
611 return true;
612 }
613
614 // Shrink wrap the live interval by walking up the CFG and find the
615 // new kills.
616 // Now let's find all the uses of the val#.
Evan Cheng06587492008-10-24 02:05:00 +0000617 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > Uses;
618 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > UseMIs;
619 SmallPtrSet<MachineBasicBlock*, 4> Seen;
620 SmallVector<MachineBasicBlock*, 4> UseMBBs;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000621 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(CurrLI->reg),
622 UE = MRI->use_end(); UI != UE; ++UI) {
623 MachineOperand &UseMO = UI.getOperand();
624 MachineInstr *UseMI = UseMO.getParent();
625 unsigned UseIdx = LIs->getInstructionIndex(UseMI);
626 LiveInterval::iterator ULR = CurrLI->FindLiveRangeContaining(UseIdx);
627 if (ULR->valno != ValNo)
628 continue;
629 MachineBasicBlock *UseMBB = UseMI->getParent();
Evan Cheng06587492008-10-24 02:05:00 +0000630 // Remember which other mbb's use this val#.
631 if (Seen.insert(UseMBB) && UseMBB != BarrierMBB)
632 UseMBBs.push_back(UseMBB);
633 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
634 UMII = Uses.find(UseMBB);
635 if (UMII != Uses.end()) {
636 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
637 UMII2 = UseMIs.find(UseMBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000638 UMII->second.push_back(&UseMO);
Evan Cheng06587492008-10-24 02:05:00 +0000639 UMII2->second.insert(UseMI);
640 } else {
641 SmallVector<MachineOperand*, 4> Ops;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000642 Ops.push_back(&UseMO);
Evan Cheng06587492008-10-24 02:05:00 +0000643 Uses.insert(std::make_pair(UseMBB, Ops));
644 SmallPtrSet<MachineInstr*, 4> MIs;
645 MIs.insert(UseMI);
646 UseMIs.insert(std::make_pair(UseMBB, MIs));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000647 }
648 }
649
650 // Walk up the predecessor chains.
651 SmallPtrSet<MachineBasicBlock*, 8> Visited;
Evan Chengaaf510c2008-10-26 07:49:03 +0000652 ShrinkWrapLiveInterval(ValNo, BarrierMBB, NULL, DefMBB, Visited,
Evan Cheng06587492008-10-24 02:05:00 +0000653 Uses, UseMIs, UseMBBs);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000654
655 // Remove live range from barrier to the restore. FIXME: Find a better
656 // point to re-start the live interval.
657 UpdateIntervalForSplit(ValNo, LIs->getUseIndex(BarrierIdx)+1,
658 LIs->getDefIndex(RestoreIndex));
659 // Record val# values are in the specific spill slot.
Evan Cheng06587492008-10-24 02:05:00 +0000660 RecordSplit(CurrLI->reg, SpillIndex, RestoreIndex, SS);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000661
662 ++NumSplit;
663 return true;
664}
665
666/// SplitRegLiveIntervals - Split all register live intervals that cross the
667/// barrier that's being processed.
668bool
669PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs) {
670 // First find all the virtual registers whose live intervals are intercepted
671 // by the current barrier.
672 SmallVector<LiveInterval*, 8> Intervals;
673 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng23066282008-10-27 07:14:50 +0000674 if (TII->IgnoreRegisterClassBarriers(*RC))
675 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000676 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
677 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
678 unsigned Reg = VRs[i];
679 if (!LIs->hasInterval(Reg))
680 continue;
681 LiveInterval *LI = &LIs->getInterval(Reg);
682 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
683 // Virtual register live interval is intercepted by the barrier. We
684 // should split and shrink wrap its interval if possible.
685 Intervals.push_back(LI);
686 }
687 }
688
689 // Process the affected live intervals.
690 bool Change = false;
691 while (!Intervals.empty()) {
692 LiveInterval *LI = Intervals.back();
693 Intervals.pop_back();
694 Change |= SplitRegLiveInterval(LI);
695 }
696
697 return Change;
698}
699
Evan Cheng09e8ca82008-10-20 21:44:59 +0000700bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000701 CurMF = &MF;
702 TM = &MF.getTarget();
703 TII = TM->getInstrInfo();
704 MFI = MF.getFrameInfo();
705 MRI = &MF.getRegInfo();
706 LIs = &getAnalysis<LiveIntervals>();
707
708 bool MadeChange = false;
709
710 // Make sure blocks are numbered in order.
711 MF.RenumberBlocks();
712
713 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
714 I != E; ++I) {
715 BarrierMBB = &*I;
716 for (MachineBasicBlock::reverse_iterator II = BarrierMBB->rbegin(),
717 EE = BarrierMBB->rend(); II != EE; ++II) {
718 Barrier = &*II;
719 const TargetRegisterClass **BarrierRCs =
720 Barrier->getDesc().getRegClassBarriers();
721 if (!BarrierRCs)
722 continue;
723 BarrierIdx = LIs->getInstructionIndex(Barrier);
724 MadeChange |= SplitRegLiveIntervals(BarrierRCs);
725 }
726 }
727
728 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +0000729}