blob: 8f223b36007f5a2a97a5e0b6547d3a348b47bcc7 [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 Chengd0e32c52008-10-29 05:06:14 +000019#include "llvm/CodeGen/LiveStackAnalysis.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineLoopInfo.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000026#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000027#include "llvm/Target/TargetMachine.h"
28#include "llvm/Target/TargetOptions.h"
29#include "llvm/Target/TargetRegisterInfo.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Debug.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000032#include "llvm/ADT/DenseMap.h"
Evan Cheng54898932008-10-29 08:39:34 +000033#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000034#include "llvm/ADT/SmallPtrSet.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000035#include "llvm/ADT/Statistic.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000036using namespace llvm;
37
Evan Chengae7fa5b2008-10-28 01:48:24 +000038static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden);
39
40STATISTIC(NumSplits, "Number of intervals split");
Evan Chengf5cd4f02008-10-23 20:43:13 +000041
Evan Cheng09e8ca82008-10-20 21:44:59 +000042namespace {
43 class VISIBILITY_HIDDEN PreAllocSplitting : public MachineFunctionPass {
Evan Chengd0e32c52008-10-29 05:06:14 +000044 MachineFunction *CurrMF;
Evan Chengf5cd4f02008-10-23 20:43:13 +000045 const TargetMachine *TM;
46 const TargetInstrInfo *TII;
47 MachineFrameInfo *MFI;
48 MachineRegisterInfo *MRI;
49 LiveIntervals *LIs;
Evan Chengd0e32c52008-10-29 05:06:14 +000050 LiveStacks *LSs;
Evan Cheng09e8ca82008-10-20 21:44:59 +000051
Evan Chengf5cd4f02008-10-23 20:43:13 +000052 // Barrier - Current barrier being processed.
53 MachineInstr *Barrier;
54
55 // BarrierMBB - Basic block where the barrier resides in.
56 MachineBasicBlock *BarrierMBB;
57
58 // Barrier - Current barrier index.
59 unsigned BarrierIdx;
60
61 // CurrLI - Current live interval being split.
62 LiveInterval *CurrLI;
63
Evan Chengd0e32c52008-10-29 05:06:14 +000064 // CurrSLI - Current stack slot live interval.
65 LiveInterval *CurrSLI;
66
67 // CurrSValNo - Current val# for the stack slot live interval.
68 VNInfo *CurrSValNo;
69
70 // IntervalSSMap - A map from live interval to spill slots.
71 DenseMap<unsigned, int> IntervalSSMap;
Evan Chengf5cd4f02008-10-23 20:43:13 +000072
Evan Cheng54898932008-10-29 08:39:34 +000073 // Def2SpillMap - A map from a def instruction index to spill index.
74 DenseMap<unsigned, unsigned> Def2SpillMap;
Evan Cheng06587492008-10-24 02:05:00 +000075
Evan Cheng09e8ca82008-10-20 21:44:59 +000076 public:
77 static char ID;
78 PreAllocSplitting() : MachineFunctionPass(&ID) {}
79
80 virtual bool runOnMachineFunction(MachineFunction &MF);
81
82 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
83 AU.addRequired<LiveIntervals>();
84 AU.addPreserved<LiveIntervals>();
Evan Chengd0e32c52008-10-29 05:06:14 +000085 AU.addRequired<LiveStacks>();
86 AU.addPreserved<LiveStacks>();
Evan Cheng09e8ca82008-10-20 21:44:59 +000087 AU.addPreserved<RegisterCoalescer>();
88 if (StrongPHIElim)
89 AU.addPreservedID(StrongPHIEliminationID);
90 else
91 AU.addPreservedID(PHIEliminationID);
Evan Cheng09e8ca82008-10-20 21:44:59 +000092 MachineFunctionPass::getAnalysisUsage(AU);
93 }
94
95 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +000096 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +000097 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +000098 }
99
100 virtual const char *getPassName() const {
101 return "Pre-Register Allocaton Live Interval Splitting";
102 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000103
104 /// print - Implement the dump method.
105 virtual void print(std::ostream &O, const Module* M = 0) const {
106 LIs->print(O, M);
107 }
108
109 void print(std::ostream *O, const Module* M = 0) const {
110 if (O) print(*O, M);
111 }
112
113 private:
114 MachineBasicBlock::iterator
115 findNextEmptySlot(MachineBasicBlock*, MachineInstr*,
116 unsigned&);
117
118 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000119 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000120 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
121
122 MachineBasicBlock::iterator
Evan Chengf62ce372008-10-28 00:47:49 +0000123 findRestorePoint(MachineBasicBlock*, MachineInstr*, unsigned,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000124 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
125
Evan Chengd0e32c52008-10-29 05:06:14 +0000126 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000127
Evan Cheng54898932008-10-29 08:39:34 +0000128 bool IsAvailableInStack(MachineBasicBlock*, unsigned, unsigned, unsigned,
129 unsigned&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000130
Evan Chengd0e32c52008-10-29 05:06:14 +0000131 void UpdateSpillSlotInterval(VNInfo*, unsigned, unsigned);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000132
Evan Chengd0e32c52008-10-29 05:06:14 +0000133 void UpdateRegisterInterval(VNInfo*, unsigned, unsigned);
134
135 bool ShrinkWrapToLastUse(MachineBasicBlock*, VNInfo*,
Evan Cheng06587492008-10-24 02:05:00 +0000136 SmallVector<MachineOperand*, 4>&,
137 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000138
Evan Chengaaf510c2008-10-26 07:49:03 +0000139 void ShrinkWrapLiveInterval(VNInfo*, MachineBasicBlock*, MachineBasicBlock*,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000140 MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 8>&,
Evan Cheng06587492008-10-24 02:05:00 +0000141 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >&,
142 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >&,
143 SmallVector<MachineBasicBlock*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000144
145 bool SplitRegLiveInterval(LiveInterval*);
146
147 bool SplitRegLiveIntervals(const TargetRegisterClass **);
Evan Cheng09e8ca82008-10-20 21:44:59 +0000148 };
149} // end anonymous namespace
150
151char PreAllocSplitting::ID = 0;
152
153static RegisterPass<PreAllocSplitting>
154X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
155
156const PassInfo *const llvm::PreAllocSplittingID = &X;
157
Evan Chengf5cd4f02008-10-23 20:43:13 +0000158
159/// findNextEmptySlot - Find a gap after the given machine instruction in the
160/// instruction index map. If there isn't one, return end().
161MachineBasicBlock::iterator
162PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
163 unsigned &SpotIndex) {
164 MachineBasicBlock::iterator MII = MI;
165 if (++MII != MBB->end()) {
166 unsigned Index = LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
167 if (Index) {
168 SpotIndex = Index;
169 return MII;
170 }
171 }
172 return MBB->end();
173}
174
175/// findSpillPoint - Find a gap as far away from the given MI that's suitable
176/// for spilling the current live interval. The index must be before any
177/// defs and uses of the live interval register in the mbb. Return begin() if
178/// none is found.
179MachineBasicBlock::iterator
180PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000181 MachineInstr *DefMI,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000182 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
183 unsigned &SpillIndex) {
184 MachineBasicBlock::iterator Pt = MBB->begin();
185
186 // Go top down if RefsInMBB is empty.
Evan Cheng1f08cc22008-10-28 05:28:21 +0000187 if (RefsInMBB.empty() && !DefMI) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000188 MachineBasicBlock::iterator MII = MBB->begin();
189 MachineBasicBlock::iterator EndPt = MI;
190 do {
191 ++MII;
192 unsigned Index = LIs->getInstructionIndex(MII);
193 unsigned Gap = LIs->findGapBeforeInstr(Index);
194 if (Gap) {
195 Pt = MII;
196 SpillIndex = Gap;
197 break;
198 }
199 } while (MII != EndPt);
200 } else {
201 MachineBasicBlock::iterator MII = MI;
Evan Cheng1f08cc22008-10-28 05:28:21 +0000202 MachineBasicBlock::iterator EndPt = DefMI
203 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
204 while (MII != EndPt && !RefsInMBB.count(MII)) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000205 unsigned Index = LIs->getInstructionIndex(MII);
206 if (LIs->hasGapBeforeInstr(Index)) {
207 Pt = MII;
208 SpillIndex = LIs->findGapBeforeInstr(Index, true);
209 }
210 --MII;
211 }
212 }
213
214 return Pt;
215}
216
217/// findRestorePoint - Find a gap in the instruction index map that's suitable
218/// for restoring the current live interval value. The index must be before any
219/// uses of the live interval register in the mbb. Return end() if none is
220/// found.
221MachineBasicBlock::iterator
222PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Chengf62ce372008-10-28 00:47:49 +0000223 unsigned LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000224 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
225 unsigned &RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000226 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
227 // begin index accordingly.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000228 MachineBasicBlock::iterator Pt = MBB->end();
Evan Chengf62ce372008-10-28 00:47:49 +0000229 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000230
Evan Chengf62ce372008-10-28 00:47:49 +0000231 // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond
232 // the last index in the live range.
233 if (RefsInMBB.empty() && LastIdx >= EndIdx) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000234 MachineBasicBlock::iterator MII = MBB->end();
235 MachineBasicBlock::iterator EndPt = MI;
Evan Cheng54898932008-10-29 08:39:34 +0000236 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000237 do {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000238 unsigned Index = LIs->getInstructionIndex(MII);
Evan Cheng56ab0de2008-10-24 18:46:44 +0000239 unsigned Gap = LIs->findGapBeforeInstr(Index);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000240 if (Gap) {
241 Pt = MII;
242 RestoreIndex = Gap;
243 break;
244 }
Evan Cheng54898932008-10-29 08:39:34 +0000245 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000246 } while (MII != EndPt);
247 } else {
248 MachineBasicBlock::iterator MII = MI;
249 MII = ++MII;
Evan Chengf62ce372008-10-28 00:47:49 +0000250 // FIXME: Limit the number of instructions to examine to reduce
251 // compile time?
Evan Chengf5cd4f02008-10-23 20:43:13 +0000252 while (MII != MBB->end()) {
253 unsigned Index = LIs->getInstructionIndex(MII);
Evan Chengf62ce372008-10-28 00:47:49 +0000254 if (Index > LastIdx)
255 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000256 unsigned Gap = LIs->findGapBeforeInstr(Index);
257 if (Gap) {
258 Pt = MII;
259 RestoreIndex = Gap;
260 }
261 if (RefsInMBB.count(MII))
262 break;
263 ++MII;
264 }
265 }
266
267 return Pt;
268}
269
Evan Chengd0e32c52008-10-29 05:06:14 +0000270/// CreateSpillStackSlot - Create a stack slot for the live interval being
271/// split. If the live interval was previously split, just reuse the same
272/// slot.
273int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
274 const TargetRegisterClass *RC) {
275 int SS;
276 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
277 if (I != IntervalSSMap.end()) {
278 SS = I->second;
279 } else {
280 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
281 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000282 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000283
284 // Create live interval for stack slot.
285 CurrSLI = &LSs->getOrCreateInterval(SS);
Evan Cheng54898932008-10-29 08:39:34 +0000286 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000287 CurrSValNo = CurrSLI->getValNumInfo(0);
288 else
289 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
290 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000291}
292
Evan Chengd0e32c52008-10-29 05:06:14 +0000293/// IsAvailableInStack - Return true if register is available in a split stack
294/// slot at the specified index.
295bool
Evan Cheng54898932008-10-29 08:39:34 +0000296PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
297 unsigned Reg, unsigned DefIndex,
298 unsigned RestoreIndex, unsigned &SpillIndex,
299 int& SS) const {
300 if (!DefMBB)
301 return false;
302
Evan Chengd0e32c52008-10-29 05:06:14 +0000303 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
304 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000305 return false;
Evan Cheng54898932008-10-29 08:39:34 +0000306 DenseMap<unsigned, unsigned>::iterator II = Def2SpillMap.find(DefIndex);
307 if (II == Def2SpillMap.end())
308 return false;
309
310 // If last spill of def is in the same mbb as barrier mbb (where restore will
311 // be), make sure it's not below the intended restore index.
312 // FIXME: Undo the previous spill?
313 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
314 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
315 return false;
316
317 SS = I->second;
318 SpillIndex = II->second;
319 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000320}
321
Evan Chengd0e32c52008-10-29 05:06:14 +0000322/// UpdateSpillSlotInterval - Given the specified val# of the register live
323/// interval being split, and the spill and restore indicies, update the live
324/// interval of the spill stack slot.
325void
326PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, unsigned SpillIndex,
327 unsigned RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000328 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
329 "Expect restore in the barrier mbb");
330
331 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
332 if (MBB == BarrierMBB) {
333 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000334 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
335 CurrSLI->addRange(SLR);
336 return;
337 }
338
Evan Cheng54898932008-10-29 08:39:34 +0000339 SmallPtrSet<MachineBasicBlock*, 4> Processed;
340 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
341 LiveRange SLR(SpillIndex, EndIdx+1, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000342 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000343 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000344
345 // Start from the spill mbb, figure out the extend of the spill slot's
346 // live interval.
347 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000348 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
349 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000350 // If live range extend beyond end of mbb, add successors to work list.
351 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
352 SE = MBB->succ_end(); SI != SE; ++SI)
353 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000354
355 while (!WorkList.empty()) {
356 MachineBasicBlock *MBB = WorkList.back();
357 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000358 if (Processed.count(MBB))
359 continue;
Evan Chengd0e32c52008-10-29 05:06:14 +0000360 unsigned Idx = LIs->getMBBStartIdx(MBB);
361 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000362 if (LR && LR->valno == ValNo) {
363 EndIdx = LIs->getMBBEndIdx(MBB);
364 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000365 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000366 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000367 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000368 } else if (LR->end > EndIdx) {
369 // Live range extends beyond end of mbb, process successors.
370 LiveRange SLR(Idx, EndIdx+1, CurrSValNo);
371 CurrSLI->addRange(SLR);
372 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
373 SE = MBB->succ_end(); SI != SE; ++SI)
374 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000375 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000376 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000377 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000378 }
Evan Cheng54898932008-10-29 08:39:34 +0000379 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000380 }
381 }
382}
383
384/// UpdateRegisterInterval - Given the specified val# of the current live
385/// interval is being split, and the spill and restore indices, update the live
Evan Chengf5cd4f02008-10-23 20:43:13 +0000386/// interval accordingly.
387void
Evan Chengd0e32c52008-10-29 05:06:14 +0000388PreAllocSplitting::UpdateRegisterInterval(VNInfo *ValNo, unsigned SpillIndex,
389 unsigned RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000390 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
391 "Expect restore in the barrier mbb");
392
Evan Chengf5cd4f02008-10-23 20:43:13 +0000393 SmallVector<std::pair<unsigned,unsigned>, 4> Before;
394 SmallVector<std::pair<unsigned,unsigned>, 4> After;
395 SmallVector<unsigned, 4> BeforeKills;
396 SmallVector<unsigned, 4> AfterKills;
397 SmallPtrSet<const LiveRange*, 4> Processed;
398
399 // First, let's figure out which parts of the live interval is now defined
400 // by the restore, which are defined by the original definition.
Evan Chengd0e32c52008-10-29 05:06:14 +0000401 const LiveRange *LR = CurrLI->getLiveRangeContaining(RestoreIndex);
402 After.push_back(std::make_pair(RestoreIndex, LR->end));
Evan Cheng06587492008-10-24 02:05:00 +0000403 if (CurrLI->isKill(ValNo, LR->end))
404 AfterKills.push_back(LR->end);
405
Evan Chengd0e32c52008-10-29 05:06:14 +0000406 assert(LR->contains(SpillIndex));
407 if (SpillIndex > LR->start) {
408 Before.push_back(std::make_pair(LR->start, SpillIndex));
409 BeforeKills.push_back(SpillIndex);
Evan Cheng06587492008-10-24 02:05:00 +0000410 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000411 Processed.insert(LR);
412
Evan Chengd0e32c52008-10-29 05:06:14 +0000413 // Start from the restore mbb, figure out what part of the live interval
414 // are defined by the restore.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000415 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000416 MachineBasicBlock *MBB = BarrierMBB;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000417 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
418 SE = MBB->succ_end(); SI != SE; ++SI)
419 WorkList.push_back(*SI);
420
421 while (!WorkList.empty()) {
422 MBB = WorkList.back();
423 WorkList.pop_back();
424 unsigned Idx = LIs->getMBBStartIdx(MBB);
425 LR = CurrLI->getLiveRangeContaining(Idx);
426 if (LR && LR->valno == ValNo && !Processed.count(LR)) {
427 After.push_back(std::make_pair(LR->start, LR->end));
428 if (CurrLI->isKill(ValNo, LR->end))
429 AfterKills.push_back(LR->end);
430 Idx = LIs->getMBBEndIdx(MBB);
431 if (LR->end > Idx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000432 // Live range extend beyond at least one mbb. Let's see what other
433 // mbbs it reaches.
434 LIs->findReachableMBBs(LR->start, LR->end, WorkList);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000435 }
436 Processed.insert(LR);
437 }
438 }
439
440 for (LiveInterval::iterator I = CurrLI->begin(), E = CurrLI->end();
441 I != E; ++I) {
442 LiveRange *LR = I;
443 if (LR->valno == ValNo && !Processed.count(LR)) {
444 Before.push_back(std::make_pair(LR->start, LR->end));
445 if (CurrLI->isKill(ValNo, LR->end))
446 BeforeKills.push_back(LR->end);
447 }
448 }
449
450 // Now create new val#s to represent the live ranges defined by the old def
451 // those defined by the restore.
452 unsigned AfterDef = ValNo->def;
453 MachineInstr *AfterCopy = ValNo->copy;
454 bool HasPHIKill = ValNo->hasPHIKill;
455 CurrLI->removeValNo(ValNo);
Evan Cheng06587492008-10-24 02:05:00 +0000456 VNInfo *BValNo = (Before.empty())
457 ? NULL
458 : CurrLI->getNextValue(AfterDef, AfterCopy, LIs->getVNInfoAllocator());
459 if (BValNo)
460 CurrLI->addKills(BValNo, BeforeKills);
461
462 VNInfo *AValNo = (After.empty())
463 ? NULL
Evan Chengd0e32c52008-10-29 05:06:14 +0000464 : CurrLI->getNextValue(RestoreIndex, 0, LIs->getVNInfoAllocator());
Evan Cheng06587492008-10-24 02:05:00 +0000465 if (AValNo) {
466 AValNo->hasPHIKill = HasPHIKill;
467 CurrLI->addKills(AValNo, AfterKills);
468 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000469
470 for (unsigned i = 0, e = Before.size(); i != e; ++i) {
471 unsigned Start = Before[i].first;
472 unsigned End = Before[i].second;
473 CurrLI->addRange(LiveRange(Start, End, BValNo));
474 }
475 for (unsigned i = 0, e = After.size(); i != e; ++i) {
476 unsigned Start = After[i].first;
477 unsigned End = After[i].second;
478 CurrLI->addRange(LiveRange(Start, End, AValNo));
479 }
480}
481
482/// ShrinkWrapToLastUse - There are uses of the current live interval in the
483/// given block, shrink wrap the live interval to the last use (i.e. remove
484/// from last use to the end of the mbb). In case mbb is the where the barrier
485/// is, remove from the last use to the barrier.
486bool
Evan Chengd0e32c52008-10-29 05:06:14 +0000487PreAllocSplitting::ShrinkWrapToLastUse(MachineBasicBlock *MBB, VNInfo *ValNo,
Evan Cheng06587492008-10-24 02:05:00 +0000488 SmallVector<MachineOperand*, 4> &Uses,
489 SmallPtrSet<MachineInstr*, 4> &UseMIs) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000490 MachineOperand *LastMO = 0;
491 MachineInstr *LastMI = 0;
492 if (MBB != BarrierMBB && Uses.size() == 1) {
493 // Single use, no need to traverse the block. We can't assume this for the
494 // barrier bb though since the use is probably below the barrier.
495 LastMO = Uses[0];
496 LastMI = LastMO->getParent();
497 } else {
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000498 MachineBasicBlock::iterator MEE = MBB->begin();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000499 MachineBasicBlock::iterator MII;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000500 if (MBB == BarrierMBB)
Evan Chengf5cd4f02008-10-23 20:43:13 +0000501 MII = Barrier;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000502 else
Evan Chengf5cd4f02008-10-23 20:43:13 +0000503 MII = MBB->end();
Evan Chengd0e32c52008-10-29 05:06:14 +0000504 while (MII != MEE) {
505 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000506 MachineInstr *UseMI = &*MII;
507 if (!UseMIs.count(UseMI))
508 continue;
509 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
510 MachineOperand &MO = UseMI->getOperand(i);
511 if (MO.isReg() && MO.getReg() == CurrLI->reg) {
512 LastMO = &MO;
513 break;
514 }
515 }
516 LastMI = UseMI;
517 break;
518 }
519 }
520
521 // Cut off live range from last use (or beginning of the mbb if there
522 // are no uses in it) to the end of the mbb.
523 unsigned RangeStart, RangeEnd = LIs->getMBBEndIdx(MBB)+1;
524 if (LastMI) {
525 RangeStart = LIs->getUseIndex(LIs->getInstructionIndex(LastMI))+1;
526 assert(!LastMO->isKill() && "Last use already terminates the interval?");
527 LastMO->setIsKill();
528 } else {
529 assert(MBB == BarrierMBB);
530 RangeStart = LIs->getMBBStartIdx(MBB);
531 }
532 if (MBB == BarrierMBB)
Evan Cheng06587492008-10-24 02:05:00 +0000533 RangeEnd = LIs->getUseIndex(BarrierIdx)+1;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000534 CurrLI->removeRange(RangeStart, RangeEnd);
Evan Chengd0e32c52008-10-29 05:06:14 +0000535 if (LastMI)
536 CurrLI->addKill(ValNo, RangeStart);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000537
538 // Return true if the last use becomes a new kill.
539 return LastMI;
540}
541
542/// ShrinkWrapLiveInterval - Recursively traverse the predecessor
543/// chain to find the new 'kills' and shrink wrap the live interval to the
544/// new kill indices.
545void
Evan Chengaaf510c2008-10-26 07:49:03 +0000546PreAllocSplitting::ShrinkWrapLiveInterval(VNInfo *ValNo, MachineBasicBlock *MBB,
547 MachineBasicBlock *SuccMBB, MachineBasicBlock *DefMBB,
Evan Cheng06587492008-10-24 02:05:00 +0000548 SmallPtrSet<MachineBasicBlock*, 8> &Visited,
549 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > &Uses,
550 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > &UseMIs,
551 SmallVector<MachineBasicBlock*, 4> &UseMBBs) {
Evan Chengaaf510c2008-10-26 07:49:03 +0000552 if (Visited.count(MBB))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000553 return;
554
Evan Chengaaf510c2008-10-26 07:49:03 +0000555 // If live interval is live in another successor path, then we can't process
556 // this block. But we may able to do so after all the successors have been
557 // processed.
Evan Chengf62ce372008-10-28 00:47:49 +0000558 if (MBB != BarrierMBB) {
559 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
560 SE = MBB->succ_end(); SI != SE; ++SI) {
561 MachineBasicBlock *SMBB = *SI;
562 if (SMBB == SuccMBB)
563 continue;
564 if (CurrLI->liveAt(LIs->getMBBStartIdx(SMBB)))
565 return;
566 }
Evan Chengaaf510c2008-10-26 07:49:03 +0000567 }
568
569 Visited.insert(MBB);
570
Evan Cheng06587492008-10-24 02:05:00 +0000571 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
572 UMII = Uses.find(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000573 if (UMII != Uses.end()) {
574 // At least one use in this mbb, lets look for the kill.
Evan Cheng06587492008-10-24 02:05:00 +0000575 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
576 UMII2 = UseMIs.find(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000577 if (ShrinkWrapToLastUse(MBB, ValNo, UMII->second, UMII2->second))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000578 // Found a kill, shrink wrapping of this path ends here.
579 return;
Evan Cheng06587492008-10-24 02:05:00 +0000580 } else if (MBB == DefMBB) {
Evan Cheng06587492008-10-24 02:05:00 +0000581 // There are no uses after the def.
582 MachineInstr *DefMI = LIs->getInstructionFromIndex(ValNo->def);
Evan Cheng06587492008-10-24 02:05:00 +0000583 if (UseMBBs.empty()) {
584 // The only use must be below barrier in the barrier block. It's safe to
585 // remove the def.
586 LIs->RemoveMachineInstrFromMaps(DefMI);
587 DefMI->eraseFromParent();
588 CurrLI->removeRange(ValNo->def, LIs->getMBBEndIdx(MBB)+1);
589 }
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000590 } else if (MBB == BarrierMBB) {
591 // Remove entire live range from start of mbb to barrier.
592 CurrLI->removeRange(LIs->getMBBStartIdx(MBB),
593 LIs->getUseIndex(BarrierIdx)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000594 } else {
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000595 // Remove entire live range of the mbb out of the live interval.
Evan Cheng06587492008-10-24 02:05:00 +0000596 CurrLI->removeRange(LIs->getMBBStartIdx(MBB), LIs->getMBBEndIdx(MBB)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000597 }
598
599 if (MBB == DefMBB)
600 // Reached the def mbb, stop traversing this path further.
601 return;
602
603 // Traverse the pathes up the predecessor chains further.
604 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
605 PE = MBB->pred_end(); PI != PE; ++PI) {
606 MachineBasicBlock *Pred = *PI;
607 if (Pred == MBB)
608 continue;
609 if (Pred == DefMBB && ValNo->hasPHIKill)
610 // Pred is the def bb and the def reaches other val#s, we must
611 // allow the value to be live out of the bb.
612 continue;
Evan Chengaaf510c2008-10-26 07:49:03 +0000613 ShrinkWrapLiveInterval(ValNo, Pred, MBB, DefMBB, Visited,
614 Uses, UseMIs, UseMBBs);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000615 }
616
617 return;
618}
619
620/// SplitRegLiveInterval - Split (spill and restore) the given live interval
621/// so it would not cross the barrier that's being processed. Shrink wrap
622/// (minimize) the live interval to the last uses.
623bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
624 CurrLI = LI;
625
626 // Find live range where current interval cross the barrier.
627 LiveInterval::iterator LR =
628 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
629 VNInfo *ValNo = LR->valno;
630
631 if (ValNo->def == ~1U) {
632 // Defined by a dead def? How can this be?
633 assert(0 && "Val# is defined by a dead def?");
634 abort();
635 }
636
Evan Cheng06587492008-10-24 02:05:00 +0000637 // FIXME: For now, if definition is rematerializable, do not split.
638 MachineInstr *DefMI = (ValNo->def != ~0U)
639 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
640 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
641 return false;
642
Evan Chengf5cd4f02008-10-23 20:43:13 +0000643 // Find all references in the barrier mbb.
644 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
645 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
646 E = MRI->reg_end(); I != E; ++I) {
647 MachineInstr *RefMI = &*I;
648 if (RefMI->getParent() == BarrierMBB)
649 RefsInMBB.insert(RefMI);
650 }
651
652 // Find a point to restore the value after the barrier.
653 unsigned RestoreIndex;
654 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +0000655 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000656 if (RestorePt == BarrierMBB->end())
657 return false;
658
659 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +0000660 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000661 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000662 unsigned SpillIndex = 0;
Evan Cheng06587492008-10-24 02:05:00 +0000663 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +0000664 int SS = -1;
Evan Cheng78dfef72008-10-25 00:52:41 +0000665 if (ValNo->def == ~0U) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000666 // If it's defined by a phi, we must split just before the barrier.
667 MachineBasicBlock::iterator SpillPt =
Evan Cheng1f08cc22008-10-28 05:28:21 +0000668 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000669 if (SpillPt == BarrierMBB->begin())
670 return false; // No gap to insert spill.
671 // Add spill.
Evan Chengd0e32c52008-10-29 05:06:14 +0000672 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000673 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
Evan Cheng06587492008-10-24 02:05:00 +0000674 SpillMI = prior(SpillPt);
675 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng54898932008-10-29 08:39:34 +0000676 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
677 RestoreIndex, SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +0000678 // If it's already split, just restore the value. There is no need to spill
679 // the def again.
Evan Chengd0e32c52008-10-29 05:06:14 +0000680 if (!DefMI)
681 return false; // Def is dead. Do nothing.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000682 // Check if it's possible to insert a spill after the def MI.
Evan Cheng1f08cc22008-10-28 05:28:21 +0000683 MachineBasicBlock::iterator SpillPt;
684 if (DefMBB == BarrierMBB) {
685 // Add spill after the def and the last use before the barrier.
686 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI, RefsInMBB, SpillIndex);
687 if (SpillPt == DefMBB->begin())
688 return false; // No gap to insert spill.
689 } else {
690 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
691 if (SpillPt == DefMBB->end())
692 return false; // No gap to insert spill.
693 }
Evan Cheng78dfef72008-10-25 00:52:41 +0000694 // Add spill. The store instruction kills the register if def is before
695 // the barrier in the barrier block.
Evan Chengd0e32c52008-10-29 05:06:14 +0000696 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng06587492008-10-24 02:05:00 +0000697 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
698 DefMBB == BarrierMBB, SS, RC);
699 SpillMI = prior(SpillPt);
700 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000701 }
702
Evan Cheng54898932008-10-29 08:39:34 +0000703 // Remember def instruction index to spill index mapping.
704 if (DefMI && SpillMI)
705 Def2SpillMap[ValNo->def] = SpillIndex;
706
Evan Chengf5cd4f02008-10-23 20:43:13 +0000707 // Add restore.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000708 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
709 MachineInstr *LoadMI = prior(RestorePt);
710 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
711
712 // If live interval is spilled in the same block as the barrier, just
713 // create a hole in the interval.
714 if (!DefMBB ||
Evan Cheng78dfef72008-10-25 00:52:41 +0000715 (SpillMI && SpillMI->getParent() == BarrierMBB)) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000716 // Update spill stack slot live interval.
717 UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
718 LIs->getDefIndex(RestoreIndex));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000719
Evan Chengd0e32c52008-10-29 05:06:14 +0000720 UpdateRegisterInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
721 LIs->getDefIndex(RestoreIndex));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000722
Evan Chengae7fa5b2008-10-28 01:48:24 +0000723 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000724 return true;
725 }
726
Evan Chengd0e32c52008-10-29 05:06:14 +0000727 // Update spill stack slot live interval.
Evan Cheng54898932008-10-29 08:39:34 +0000728 UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
729 LIs->getDefIndex(RestoreIndex));
Evan Chengd0e32c52008-10-29 05:06:14 +0000730
Evan Chengf5cd4f02008-10-23 20:43:13 +0000731 // Shrink wrap the live interval by walking up the CFG and find the
732 // new kills.
733 // Now let's find all the uses of the val#.
Evan Cheng06587492008-10-24 02:05:00 +0000734 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > Uses;
735 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > UseMIs;
736 SmallPtrSet<MachineBasicBlock*, 4> Seen;
737 SmallVector<MachineBasicBlock*, 4> UseMBBs;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000738 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(CurrLI->reg),
739 UE = MRI->use_end(); UI != UE; ++UI) {
740 MachineOperand &UseMO = UI.getOperand();
741 MachineInstr *UseMI = UseMO.getParent();
742 unsigned UseIdx = LIs->getInstructionIndex(UseMI);
743 LiveInterval::iterator ULR = CurrLI->FindLiveRangeContaining(UseIdx);
744 if (ULR->valno != ValNo)
745 continue;
746 MachineBasicBlock *UseMBB = UseMI->getParent();
Evan Cheng06587492008-10-24 02:05:00 +0000747 // Remember which other mbb's use this val#.
748 if (Seen.insert(UseMBB) && UseMBB != BarrierMBB)
749 UseMBBs.push_back(UseMBB);
750 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
751 UMII = Uses.find(UseMBB);
752 if (UMII != Uses.end()) {
753 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
754 UMII2 = UseMIs.find(UseMBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000755 UMII->second.push_back(&UseMO);
Evan Cheng06587492008-10-24 02:05:00 +0000756 UMII2->second.insert(UseMI);
757 } else {
758 SmallVector<MachineOperand*, 4> Ops;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000759 Ops.push_back(&UseMO);
Evan Cheng06587492008-10-24 02:05:00 +0000760 Uses.insert(std::make_pair(UseMBB, Ops));
761 SmallPtrSet<MachineInstr*, 4> MIs;
762 MIs.insert(UseMI);
763 UseMIs.insert(std::make_pair(UseMBB, MIs));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000764 }
765 }
766
767 // Walk up the predecessor chains.
768 SmallPtrSet<MachineBasicBlock*, 8> Visited;
Evan Chengaaf510c2008-10-26 07:49:03 +0000769 ShrinkWrapLiveInterval(ValNo, BarrierMBB, NULL, DefMBB, Visited,
Evan Cheng06587492008-10-24 02:05:00 +0000770 Uses, UseMIs, UseMBBs);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000771
Evan Cheng36f3adf2008-10-31 16:41:59 +0000772 // FIXME: If ValNo->hasPHIKill is false, then renumber the val# by
773 // the restore.
774
Evan Chengf5cd4f02008-10-23 20:43:13 +0000775 // Remove live range from barrier to the restore. FIXME: Find a better
776 // point to re-start the live interval.
Evan Chengd0e32c52008-10-29 05:06:14 +0000777 UpdateRegisterInterval(ValNo, LIs->getUseIndex(BarrierIdx)+1,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000778 LIs->getDefIndex(RestoreIndex));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000779
Evan Chengae7fa5b2008-10-28 01:48:24 +0000780 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000781 return true;
782}
783
784/// SplitRegLiveIntervals - Split all register live intervals that cross the
785/// barrier that's being processed.
786bool
787PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs) {
788 // First find all the virtual registers whose live intervals are intercepted
789 // by the current barrier.
790 SmallVector<LiveInterval*, 8> Intervals;
791 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng23066282008-10-27 07:14:50 +0000792 if (TII->IgnoreRegisterClassBarriers(*RC))
793 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000794 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
795 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
796 unsigned Reg = VRs[i];
797 if (!LIs->hasInterval(Reg))
798 continue;
799 LiveInterval *LI = &LIs->getInterval(Reg);
800 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
801 // Virtual register live interval is intercepted by the barrier. We
802 // should split and shrink wrap its interval if possible.
803 Intervals.push_back(LI);
804 }
805 }
806
807 // Process the affected live intervals.
808 bool Change = false;
809 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +0000810 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
811 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000812 LiveInterval *LI = Intervals.back();
813 Intervals.pop_back();
814 Change |= SplitRegLiveInterval(LI);
815 }
816
817 return Change;
818}
819
Evan Cheng09e8ca82008-10-20 21:44:59 +0000820bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000821 CurrMF = &MF;
822 TM = &MF.getTarget();
823 TII = TM->getInstrInfo();
824 MFI = MF.getFrameInfo();
825 MRI = &MF.getRegInfo();
826 LIs = &getAnalysis<LiveIntervals>();
827 LSs = &getAnalysis<LiveStacks>();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000828
829 bool MadeChange = false;
830
831 // Make sure blocks are numbered in order.
832 MF.RenumberBlocks();
833
Evan Cheng54898932008-10-29 08:39:34 +0000834#if 0
835 // FIXME: Go top down.
836 MachineBasicBlock *Entry = MF.begin();
837 SmallPtrSet<MachineBasicBlock*,16> Visited;
838
839 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
840 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
841 DFI != E; ++DFI) {
842 BarrierMBB = *DFI;
843 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
844 E = BarrierMBB->end(); I != E; ++I) {
845 Barrier = &*I;
846 const TargetRegisterClass **BarrierRCs =
847 Barrier->getDesc().getRegClassBarriers();
848 if (!BarrierRCs)
849 continue;
850 BarrierIdx = LIs->getInstructionIndex(Barrier);
851 MadeChange |= SplitRegLiveIntervals(BarrierRCs);
852 }
853 }
854#else
Evan Chengf5cd4f02008-10-23 20:43:13 +0000855 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
856 I != E; ++I) {
857 BarrierMBB = &*I;
858 for (MachineBasicBlock::reverse_iterator II = BarrierMBB->rbegin(),
859 EE = BarrierMBB->rend(); II != EE; ++II) {
860 Barrier = &*II;
861 const TargetRegisterClass **BarrierRCs =
862 Barrier->getDesc().getRegClassBarriers();
863 if (!BarrierRCs)
864 continue;
865 BarrierIdx = LIs->getInstructionIndex(Barrier);
866 MadeChange |= SplitRegLiveIntervals(BarrierRCs);
867 }
868 }
Evan Cheng54898932008-10-29 08:39:34 +0000869#endif
Evan Chengf5cd4f02008-10-23 20:43:13 +0000870
871 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +0000872}