blob: 5a2948b852b1e59513d74e25853d151705faa407 [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"
Owen Andersonf1f75b12008-11-04 22:22:41 +000020#include "llvm/CodeGen/MachineDominators.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineLoopInfo.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/CodeGen/Passes.h"
26#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000027#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000028#include "llvm/Target/TargetMachine.h"
29#include "llvm/Target/TargetOptions.h"
30#include "llvm/Target/TargetRegisterInfo.h"
31#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000033#include "llvm/ADT/DenseMap.h"
Evan Cheng54898932008-10-29 08:39:34 +000034#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000035#include "llvm/ADT/SmallPtrSet.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000036#include "llvm/ADT/Statistic.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000037using namespace llvm;
38
Evan Chengae7fa5b2008-10-28 01:48:24 +000039static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden);
40
41STATISTIC(NumSplits, "Number of intervals split");
Owen Anderson75fa96b2008-11-19 04:28:29 +000042STATISTIC(NumRemats, "Number of intervals split by rematerialization");
Owen Anderson7b9d67c2008-12-02 18:53:47 +000043STATISTIC(NumFolds, "Number of intervals split with spill folding");
Owen Anderson2ebf63f2008-12-18 01:27:19 +000044STATISTIC(NumRenumbers, "Number of intervals renumbered into new registers");
Evan Chengf5cd4f02008-10-23 20:43:13 +000045
Evan Cheng09e8ca82008-10-20 21:44:59 +000046namespace {
47 class VISIBILITY_HIDDEN PreAllocSplitting : public MachineFunctionPass {
Evan Chengd0e32c52008-10-29 05:06:14 +000048 MachineFunction *CurrMF;
Evan Chengf5cd4f02008-10-23 20:43:13 +000049 const TargetMachine *TM;
50 const TargetInstrInfo *TII;
51 MachineFrameInfo *MFI;
52 MachineRegisterInfo *MRI;
53 LiveIntervals *LIs;
Evan Chengd0e32c52008-10-29 05:06:14 +000054 LiveStacks *LSs;
Evan Cheng09e8ca82008-10-20 21:44:59 +000055
Evan Chengf5cd4f02008-10-23 20:43:13 +000056 // Barrier - Current barrier being processed.
57 MachineInstr *Barrier;
58
59 // BarrierMBB - Basic block where the barrier resides in.
60 MachineBasicBlock *BarrierMBB;
61
62 // Barrier - Current barrier index.
63 unsigned BarrierIdx;
64
65 // CurrLI - Current live interval being split.
66 LiveInterval *CurrLI;
67
Evan Chengd0e32c52008-10-29 05:06:14 +000068 // CurrSLI - Current stack slot live interval.
69 LiveInterval *CurrSLI;
70
71 // CurrSValNo - Current val# for the stack slot live interval.
72 VNInfo *CurrSValNo;
73
74 // IntervalSSMap - A map from live interval to spill slots.
75 DenseMap<unsigned, int> IntervalSSMap;
Evan Chengf5cd4f02008-10-23 20:43:13 +000076
Evan Cheng54898932008-10-29 08:39:34 +000077 // Def2SpillMap - A map from a def instruction index to spill index.
78 DenseMap<unsigned, unsigned> Def2SpillMap;
Evan Cheng06587492008-10-24 02:05:00 +000079
Evan Cheng09e8ca82008-10-20 21:44:59 +000080 public:
81 static char ID;
82 PreAllocSplitting() : MachineFunctionPass(&ID) {}
83
84 virtual bool runOnMachineFunction(MachineFunction &MF);
85
86 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
87 AU.addRequired<LiveIntervals>();
88 AU.addPreserved<LiveIntervals>();
Evan Chengd0e32c52008-10-29 05:06:14 +000089 AU.addRequired<LiveStacks>();
90 AU.addPreserved<LiveStacks>();
Evan Cheng09e8ca82008-10-20 21:44:59 +000091 AU.addPreserved<RegisterCoalescer>();
92 if (StrongPHIElim)
93 AU.addPreservedID(StrongPHIEliminationID);
94 else
95 AU.addPreservedID(PHIEliminationID);
Owen Andersonf1f75b12008-11-04 22:22:41 +000096 AU.addRequired<MachineDominatorTree>();
97 AU.addRequired<MachineLoopInfo>();
98 AU.addPreserved<MachineDominatorTree>();
99 AU.addPreserved<MachineLoopInfo>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000100 MachineFunctionPass::getAnalysisUsage(AU);
101 }
102
103 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +0000104 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +0000105 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000106 }
107
108 virtual const char *getPassName() const {
109 return "Pre-Register Allocaton Live Interval Splitting";
110 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000111
112 /// print - Implement the dump method.
113 virtual void print(std::ostream &O, const Module* M = 0) const {
114 LIs->print(O, M);
115 }
116
117 void print(std::ostream *O, const Module* M = 0) const {
118 if (O) print(*O, M);
119 }
120
121 private:
122 MachineBasicBlock::iterator
123 findNextEmptySlot(MachineBasicBlock*, MachineInstr*,
124 unsigned&);
125
126 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000127 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000128 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
129
130 MachineBasicBlock::iterator
Evan Chengf62ce372008-10-28 00:47:49 +0000131 findRestorePoint(MachineBasicBlock*, MachineInstr*, unsigned,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000132 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
133
Evan Chengd0e32c52008-10-29 05:06:14 +0000134 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000135
Evan Cheng54898932008-10-29 08:39:34 +0000136 bool IsAvailableInStack(MachineBasicBlock*, unsigned, unsigned, unsigned,
137 unsigned&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000138
Evan Chengd0e32c52008-10-29 05:06:14 +0000139 void UpdateSpillSlotInterval(VNInfo*, unsigned, unsigned);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000140
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000141 VNInfo* UpdateRegisterInterval(VNInfo*, unsigned, unsigned);
Evan Chengd0e32c52008-10-29 05:06:14 +0000142
143 bool ShrinkWrapToLastUse(MachineBasicBlock*, VNInfo*,
Evan Cheng06587492008-10-24 02:05:00 +0000144 SmallVector<MachineOperand*, 4>&,
145 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000146
Evan Chengaaf510c2008-10-26 07:49:03 +0000147 void ShrinkWrapLiveInterval(VNInfo*, MachineBasicBlock*, MachineBasicBlock*,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000148 MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 8>&,
Evan Cheng06587492008-10-24 02:05:00 +0000149 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >&,
150 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >&,
151 SmallVector<MachineBasicBlock*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000152
153 bool SplitRegLiveInterval(LiveInterval*);
154
155 bool SplitRegLiveIntervals(const TargetRegisterClass **);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000156
Owen Anderson6002e992008-12-04 21:20:30 +0000157 void RepairLiveInterval(LiveInterval* CurrLI, VNInfo* ValNo,
158 MachineInstr* DefMI, unsigned RestoreIdx);
159
Owen Andersonf1f75b12008-11-04 22:22:41 +0000160 bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB,
161 MachineBasicBlock* BarrierMBB);
Owen Anderson75fa96b2008-11-19 04:28:29 +0000162 bool Rematerialize(unsigned vreg, VNInfo* ValNo,
163 MachineInstr* DefMI,
164 MachineBasicBlock::iterator RestorePt,
165 unsigned RestoreIdx,
166 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000167 MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC,
168 MachineInstr* DefMI,
169 MachineInstr* Barrier,
170 MachineBasicBlock* MBB,
171 int& SS,
172 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000173 void RenumberValno(VNInfo* VN);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000174 void ReconstructLiveInterval(LiveInterval* LI);
175 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator use,
Owen Anderson7d211e22008-12-31 02:00:25 +0000176 MachineBasicBlock* MBB,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000177 LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000178 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000179 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
180 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
181 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000182 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
183 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
184 bool toplevel, bool intrablock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000185};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000186} // end anonymous namespace
187
188char PreAllocSplitting::ID = 0;
189
190static RegisterPass<PreAllocSplitting>
191X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
192
193const PassInfo *const llvm::PreAllocSplittingID = &X;
194
Evan Chengf5cd4f02008-10-23 20:43:13 +0000195
196/// findNextEmptySlot - Find a gap after the given machine instruction in the
197/// instruction index map. If there isn't one, return end().
198MachineBasicBlock::iterator
199PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
200 unsigned &SpotIndex) {
201 MachineBasicBlock::iterator MII = MI;
202 if (++MII != MBB->end()) {
203 unsigned Index = LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
204 if (Index) {
205 SpotIndex = Index;
206 return MII;
207 }
208 }
209 return MBB->end();
210}
211
212/// findSpillPoint - Find a gap as far away from the given MI that's suitable
213/// for spilling the current live interval. The index must be before any
214/// defs and uses of the live interval register in the mbb. Return begin() if
215/// none is found.
216MachineBasicBlock::iterator
217PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000218 MachineInstr *DefMI,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000219 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
220 unsigned &SpillIndex) {
221 MachineBasicBlock::iterator Pt = MBB->begin();
222
223 // Go top down if RefsInMBB is empty.
Evan Cheng1f08cc22008-10-28 05:28:21 +0000224 if (RefsInMBB.empty() && !DefMI) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000225 MachineBasicBlock::iterator MII = MBB->begin();
226 MachineBasicBlock::iterator EndPt = MI;
227 do {
228 ++MII;
229 unsigned Index = LIs->getInstructionIndex(MII);
230 unsigned Gap = LIs->findGapBeforeInstr(Index);
231 if (Gap) {
232 Pt = MII;
233 SpillIndex = Gap;
234 break;
235 }
236 } while (MII != EndPt);
237 } else {
238 MachineBasicBlock::iterator MII = MI;
Evan Cheng1f08cc22008-10-28 05:28:21 +0000239 MachineBasicBlock::iterator EndPt = DefMI
240 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
241 while (MII != EndPt && !RefsInMBB.count(MII)) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000242 unsigned Index = LIs->getInstructionIndex(MII);
243 if (LIs->hasGapBeforeInstr(Index)) {
244 Pt = MII;
245 SpillIndex = LIs->findGapBeforeInstr(Index, true);
246 }
247 --MII;
248 }
249 }
250
251 return Pt;
252}
253
254/// findRestorePoint - Find a gap in the instruction index map that's suitable
255/// for restoring the current live interval value. The index must be before any
256/// uses of the live interval register in the mbb. Return end() if none is
257/// found.
258MachineBasicBlock::iterator
259PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Chengf62ce372008-10-28 00:47:49 +0000260 unsigned LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000261 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
262 unsigned &RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000263 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
264 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000265 MachineBasicBlock::iterator Pt = MBB->end();
Evan Chengf62ce372008-10-28 00:47:49 +0000266 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000267
Evan Chengf62ce372008-10-28 00:47:49 +0000268 // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond
269 // the last index in the live range.
270 if (RefsInMBB.empty() && LastIdx >= EndIdx) {
Owen Anderson711fd3d2008-11-13 21:53:14 +0000271 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000272 MachineBasicBlock::iterator EndPt = MI;
Evan Cheng54898932008-10-29 08:39:34 +0000273 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000274 do {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000275 unsigned Index = LIs->getInstructionIndex(MII);
Evan Cheng56ab0de2008-10-24 18:46:44 +0000276 unsigned Gap = LIs->findGapBeforeInstr(Index);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000277 if (Gap) {
278 Pt = MII;
279 RestoreIndex = Gap;
280 break;
281 }
Evan Cheng54898932008-10-29 08:39:34 +0000282 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000283 } while (MII != EndPt);
284 } else {
285 MachineBasicBlock::iterator MII = MI;
286 MII = ++MII;
Evan Chengf62ce372008-10-28 00:47:49 +0000287 // FIXME: Limit the number of instructions to examine to reduce
288 // compile time?
Evan Chengf5cd4f02008-10-23 20:43:13 +0000289 while (MII != MBB->end()) {
290 unsigned Index = LIs->getInstructionIndex(MII);
Evan Chengf62ce372008-10-28 00:47:49 +0000291 if (Index > LastIdx)
292 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000293 unsigned Gap = LIs->findGapBeforeInstr(Index);
294 if (Gap) {
295 Pt = MII;
296 RestoreIndex = Gap;
297 }
298 if (RefsInMBB.count(MII))
299 break;
300 ++MII;
301 }
302 }
303
304 return Pt;
305}
306
Evan Chengd0e32c52008-10-29 05:06:14 +0000307/// CreateSpillStackSlot - Create a stack slot for the live interval being
308/// split. If the live interval was previously split, just reuse the same
309/// slot.
310int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
311 const TargetRegisterClass *RC) {
312 int SS;
313 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
314 if (I != IntervalSSMap.end()) {
315 SS = I->second;
316 } else {
317 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
318 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000319 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000320
321 // Create live interval for stack slot.
322 CurrSLI = &LSs->getOrCreateInterval(SS);
Evan Cheng54898932008-10-29 08:39:34 +0000323 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000324 CurrSValNo = CurrSLI->getValNumInfo(0);
325 else
326 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
327 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000328}
329
Evan Chengd0e32c52008-10-29 05:06:14 +0000330/// IsAvailableInStack - Return true if register is available in a split stack
331/// slot at the specified index.
332bool
Evan Cheng54898932008-10-29 08:39:34 +0000333PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
334 unsigned Reg, unsigned DefIndex,
335 unsigned RestoreIndex, unsigned &SpillIndex,
336 int& SS) const {
337 if (!DefMBB)
338 return false;
339
Evan Chengd0e32c52008-10-29 05:06:14 +0000340 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
341 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000342 return false;
Evan Cheng54898932008-10-29 08:39:34 +0000343 DenseMap<unsigned, unsigned>::iterator II = Def2SpillMap.find(DefIndex);
344 if (II == Def2SpillMap.end())
345 return false;
346
347 // If last spill of def is in the same mbb as barrier mbb (where restore will
348 // be), make sure it's not below the intended restore index.
349 // FIXME: Undo the previous spill?
350 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
351 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
352 return false;
353
354 SS = I->second;
355 SpillIndex = II->second;
356 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000357}
358
Evan Chengd0e32c52008-10-29 05:06:14 +0000359/// UpdateSpillSlotInterval - Given the specified val# of the register live
360/// interval being split, and the spill and restore indicies, update the live
361/// interval of the spill stack slot.
362void
363PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, unsigned SpillIndex,
364 unsigned RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000365 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
366 "Expect restore in the barrier mbb");
367
368 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
369 if (MBB == BarrierMBB) {
370 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000371 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
372 CurrSLI->addRange(SLR);
373 return;
374 }
375
Evan Cheng54898932008-10-29 08:39:34 +0000376 SmallPtrSet<MachineBasicBlock*, 4> Processed;
377 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
378 LiveRange SLR(SpillIndex, EndIdx+1, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000379 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000380 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000381
382 // Start from the spill mbb, figure out the extend of the spill slot's
383 // live interval.
384 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000385 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
386 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000387 // If live range extend beyond end of mbb, add successors to work list.
388 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
389 SE = MBB->succ_end(); SI != SE; ++SI)
390 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000391
392 while (!WorkList.empty()) {
393 MachineBasicBlock *MBB = WorkList.back();
394 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000395 if (Processed.count(MBB))
396 continue;
Evan Chengd0e32c52008-10-29 05:06:14 +0000397 unsigned Idx = LIs->getMBBStartIdx(MBB);
398 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000399 if (LR && LR->valno == ValNo) {
400 EndIdx = LIs->getMBBEndIdx(MBB);
401 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000402 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000403 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000404 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000405 } else if (LR->end > EndIdx) {
406 // Live range extends beyond end of mbb, process successors.
407 LiveRange SLR(Idx, EndIdx+1, CurrSValNo);
408 CurrSLI->addRange(SLR);
409 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
410 SE = MBB->succ_end(); SI != SE; ++SI)
411 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000412 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000413 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000414 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000415 }
Evan Cheng54898932008-10-29 08:39:34 +0000416 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000417 }
418 }
419}
420
421/// UpdateRegisterInterval - Given the specified val# of the current live
422/// interval is being split, and the spill and restore indices, update the live
Evan Chengf5cd4f02008-10-23 20:43:13 +0000423/// interval accordingly.
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000424VNInfo*
Evan Chengd0e32c52008-10-29 05:06:14 +0000425PreAllocSplitting::UpdateRegisterInterval(VNInfo *ValNo, unsigned SpillIndex,
426 unsigned RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000427 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
428 "Expect restore in the barrier mbb");
429
Evan Chengf5cd4f02008-10-23 20:43:13 +0000430 SmallVector<std::pair<unsigned,unsigned>, 4> Before;
431 SmallVector<std::pair<unsigned,unsigned>, 4> After;
432 SmallVector<unsigned, 4> BeforeKills;
433 SmallVector<unsigned, 4> AfterKills;
434 SmallPtrSet<const LiveRange*, 4> Processed;
435
436 // First, let's figure out which parts of the live interval is now defined
437 // by the restore, which are defined by the original definition.
Evan Chengd0e32c52008-10-29 05:06:14 +0000438 const LiveRange *LR = CurrLI->getLiveRangeContaining(RestoreIndex);
439 After.push_back(std::make_pair(RestoreIndex, LR->end));
Evan Cheng06587492008-10-24 02:05:00 +0000440 if (CurrLI->isKill(ValNo, LR->end))
441 AfterKills.push_back(LR->end);
442
Evan Chengd0e32c52008-10-29 05:06:14 +0000443 assert(LR->contains(SpillIndex));
444 if (SpillIndex > LR->start) {
445 Before.push_back(std::make_pair(LR->start, SpillIndex));
446 BeforeKills.push_back(SpillIndex);
Evan Cheng06587492008-10-24 02:05:00 +0000447 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000448 Processed.insert(LR);
449
Evan Chengd0e32c52008-10-29 05:06:14 +0000450 // Start from the restore mbb, figure out what part of the live interval
451 // are defined by the restore.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000452 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000453 MachineBasicBlock *MBB = BarrierMBB;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000454 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
455 SE = MBB->succ_end(); SI != SE; ++SI)
456 WorkList.push_back(*SI);
457
Owen Anderson75c99c52008-12-07 05:33:18 +0000458 SmallPtrSet<MachineBasicBlock*, 4> ProcessedBlocks;
459 ProcessedBlocks.insert(MBB);
460
Evan Chengf5cd4f02008-10-23 20:43:13 +0000461 while (!WorkList.empty()) {
462 MBB = WorkList.back();
463 WorkList.pop_back();
464 unsigned Idx = LIs->getMBBStartIdx(MBB);
465 LR = CurrLI->getLiveRangeContaining(Idx);
466 if (LR && LR->valno == ValNo && !Processed.count(LR)) {
467 After.push_back(std::make_pair(LR->start, LR->end));
468 if (CurrLI->isKill(ValNo, LR->end))
469 AfterKills.push_back(LR->end);
470 Idx = LIs->getMBBEndIdx(MBB);
471 if (LR->end > Idx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000472 // Live range extend beyond at least one mbb. Let's see what other
473 // mbbs it reaches.
474 LIs->findReachableMBBs(LR->start, LR->end, WorkList);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000475 }
476 Processed.insert(LR);
477 }
Owen Anderson75c99c52008-12-07 05:33:18 +0000478
479 ProcessedBlocks.insert(MBB);
480 if (LR)
481 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
482 SE = MBB->succ_end(); SI != SE; ++SI)
483 if (!ProcessedBlocks.count(*SI))
484 WorkList.push_back(*SI);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000485 }
486
487 for (LiveInterval::iterator I = CurrLI->begin(), E = CurrLI->end();
488 I != E; ++I) {
489 LiveRange *LR = I;
490 if (LR->valno == ValNo && !Processed.count(LR)) {
491 Before.push_back(std::make_pair(LR->start, LR->end));
492 if (CurrLI->isKill(ValNo, LR->end))
493 BeforeKills.push_back(LR->end);
494 }
495 }
496
497 // Now create new val#s to represent the live ranges defined by the old def
498 // those defined by the restore.
499 unsigned AfterDef = ValNo->def;
500 MachineInstr *AfterCopy = ValNo->copy;
501 bool HasPHIKill = ValNo->hasPHIKill;
502 CurrLI->removeValNo(ValNo);
Evan Cheng06587492008-10-24 02:05:00 +0000503 VNInfo *BValNo = (Before.empty())
504 ? NULL
505 : CurrLI->getNextValue(AfterDef, AfterCopy, LIs->getVNInfoAllocator());
506 if (BValNo)
507 CurrLI->addKills(BValNo, BeforeKills);
508
509 VNInfo *AValNo = (After.empty())
510 ? NULL
Evan Chengd0e32c52008-10-29 05:06:14 +0000511 : CurrLI->getNextValue(RestoreIndex, 0, LIs->getVNInfoAllocator());
Evan Cheng06587492008-10-24 02:05:00 +0000512 if (AValNo) {
513 AValNo->hasPHIKill = HasPHIKill;
514 CurrLI->addKills(AValNo, AfterKills);
515 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000516
517 for (unsigned i = 0, e = Before.size(); i != e; ++i) {
518 unsigned Start = Before[i].first;
519 unsigned End = Before[i].second;
520 CurrLI->addRange(LiveRange(Start, End, BValNo));
521 }
522 for (unsigned i = 0, e = After.size(); i != e; ++i) {
523 unsigned Start = After[i].first;
524 unsigned End = After[i].second;
525 CurrLI->addRange(LiveRange(Start, End, AValNo));
526 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000527
528 return AValNo;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000529}
530
531/// ShrinkWrapToLastUse - There are uses of the current live interval in the
532/// given block, shrink wrap the live interval to the last use (i.e. remove
533/// from last use to the end of the mbb). In case mbb is the where the barrier
534/// is, remove from the last use to the barrier.
535bool
Evan Chengd0e32c52008-10-29 05:06:14 +0000536PreAllocSplitting::ShrinkWrapToLastUse(MachineBasicBlock *MBB, VNInfo *ValNo,
Evan Cheng06587492008-10-24 02:05:00 +0000537 SmallVector<MachineOperand*, 4> &Uses,
538 SmallPtrSet<MachineInstr*, 4> &UseMIs) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000539 MachineOperand *LastMO = 0;
540 MachineInstr *LastMI = 0;
541 if (MBB != BarrierMBB && Uses.size() == 1) {
542 // Single use, no need to traverse the block. We can't assume this for the
543 // barrier bb though since the use is probably below the barrier.
544 LastMO = Uses[0];
545 LastMI = LastMO->getParent();
546 } else {
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000547 MachineBasicBlock::iterator MEE = MBB->begin();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000548 MachineBasicBlock::iterator MII;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000549 if (MBB == BarrierMBB)
Evan Chengf5cd4f02008-10-23 20:43:13 +0000550 MII = Barrier;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000551 else
Evan Chengf5cd4f02008-10-23 20:43:13 +0000552 MII = MBB->end();
Evan Chengd0e32c52008-10-29 05:06:14 +0000553 while (MII != MEE) {
554 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000555 MachineInstr *UseMI = &*MII;
556 if (!UseMIs.count(UseMI))
557 continue;
558 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
559 MachineOperand &MO = UseMI->getOperand(i);
560 if (MO.isReg() && MO.getReg() == CurrLI->reg) {
561 LastMO = &MO;
562 break;
563 }
564 }
565 LastMI = UseMI;
566 break;
567 }
568 }
569
570 // Cut off live range from last use (or beginning of the mbb if there
571 // are no uses in it) to the end of the mbb.
572 unsigned RangeStart, RangeEnd = LIs->getMBBEndIdx(MBB)+1;
573 if (LastMI) {
574 RangeStart = LIs->getUseIndex(LIs->getInstructionIndex(LastMI))+1;
575 assert(!LastMO->isKill() && "Last use already terminates the interval?");
576 LastMO->setIsKill();
577 } else {
578 assert(MBB == BarrierMBB);
579 RangeStart = LIs->getMBBStartIdx(MBB);
580 }
581 if (MBB == BarrierMBB)
Evan Cheng06587492008-10-24 02:05:00 +0000582 RangeEnd = LIs->getUseIndex(BarrierIdx)+1;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000583 CurrLI->removeRange(RangeStart, RangeEnd);
Evan Chengd0e32c52008-10-29 05:06:14 +0000584 if (LastMI)
585 CurrLI->addKill(ValNo, RangeStart);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000586
587 // Return true if the last use becomes a new kill.
588 return LastMI;
589}
590
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000591/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
592/// construction algorithm to compute the ranges and valnos for an interval.
593VNInfo* PreAllocSplitting::PerformPHIConstruction(
594 MachineBasicBlock::iterator use,
Owen Anderson7d211e22008-12-31 02:00:25 +0000595 MachineBasicBlock* MBB,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000596 LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000597 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000598 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
599 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
600 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000601 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
602 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
603 bool toplevel, bool intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000604 // Return memoized result if it's available.
Owen Anderson200ee7f2009-01-06 07:53:32 +0000605 if (toplevel && Visited.count(use) && NewVNs.count(use))
606 return NewVNs[use];
607 else if (!toplevel && intrablock && NewVNs.count(use))
Owen Anderson7d211e22008-12-31 02:00:25 +0000608 return NewVNs[use];
609 else if (!intrablock && LiveOut.count(MBB))
610 return LiveOut[MBB];
611
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000612 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
613
614 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000615 bool ContainsDefs = Defs.count(MBB);
616 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000617
618 VNInfo* ret = 0;
619
620 // Enumerate the cases of use/def contaning blocks.
621 if (!ContainsDefs && !ContainsUses) {
622 Fallback:
623 // NOTE: Because this is the fallback case from other cases, we do NOT
Owen Anderson7d211e22008-12-31 02:00:25 +0000624 // assume that we are not intrablock here.
625 if (Phis.count(MBB)) return Phis[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000626
Owen Anderson7d211e22008-12-31 02:00:25 +0000627 unsigned StartIndex = LIs->getMBBStartIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000628
Owen Anderson7d211e22008-12-31 02:00:25 +0000629 if (MBB->pred_size() == 1) {
630 Phis[MBB] = ret = PerformPHIConstruction((*MBB->pred_begin())->end(),
Owen Anderson200ee7f2009-01-06 07:53:32 +0000631 *(MBB->pred_begin()), LI, Visited,
632 Defs, Uses, NewVNs, LiveOut, Phis,
Owen Anderson7d211e22008-12-31 02:00:25 +0000633 false, false);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000634 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000635 if (intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000636 EndIndex = LIs->getInstructionIndex(use);
637 EndIndex = LiveIntervals::getUseIndex(EndIndex);
638 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000639 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000640
Owen Anderson7d211e22008-12-31 02:00:25 +0000641 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000642 } else {
Owen Anderson7d211e22008-12-31 02:00:25 +0000643 Phis[MBB] = ret = LI->getNextValue(~0U, /*FIXME*/ 0,
644 LIs->getVNInfoAllocator());
645 if (!intrablock) LiveOut[MBB] = ret;
646
647 // If there are no uses or defs between our starting point and the
648 // beginning of the block, then recursive perform phi construction
649 // on our predecessors.
650 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
651 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
652 PE = MBB->pred_end(); PI != PE; ++PI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000653 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
654 Visited, Defs, Uses, NewVNs,
655 LiveOut, Phis, false, false);
Owen Anderson7d211e22008-12-31 02:00:25 +0000656 if (Incoming != 0)
657 IncomingVNs[*PI] = Incoming;
658 }
659
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000660 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
661 // VNInfo to represent the joined value.
662 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
663 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
664 I->second->hasPHIKill = true;
665 unsigned KillIndex = LIs->getMBBEndIdx(I->first);
666 LI->addKill(I->second, KillIndex);
667 }
668
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000669 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000670 if (intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000671 EndIndex = LIs->getInstructionIndex(use);
672 EndIndex = LiveIntervals::getUseIndex(EndIndex);
673 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000674 EndIndex = LIs->getMBBEndIdx(MBB);
675 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000676 }
677 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000678 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000679
680 // Search for the def in this block. If we don't find it before the
681 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000682 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000683 // always be an end() iterator.
Owen Anderson7d211e22008-12-31 02:00:25 +0000684 assert(use == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000685
686 MachineBasicBlock::iterator walker = use;
687 --walker;
Owen Anderson7d211e22008-12-31 02:00:25 +0000688 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000689 if (BlockDefs.count(walker)) {
690 break;
691 } else
692 --walker;
693
694 // Once we've found it, extend its VNInfo to our instruction.
695 unsigned DefIndex = LIs->getInstructionIndex(walker);
696 DefIndex = LiveIntervals::getDefIndex(DefIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000697 unsigned EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000698
699 ret = NewVNs[walker];
Owen Anderson7d211e22008-12-31 02:00:25 +0000700 LI->addRange(LiveRange(DefIndex, EndIndex+1, ret));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000701 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000702 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000703
704 // Search for the use in this block that precedes the instruction we care
705 // about, going to the fallback case if we don't find it.
706
Owen Anderson7d211e22008-12-31 02:00:25 +0000707 if (use == MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000708 goto Fallback;
709
710 MachineBasicBlock::iterator walker = use;
711 --walker;
712 bool found = false;
Owen Anderson7d211e22008-12-31 02:00:25 +0000713 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000714 if (BlockUses.count(walker)) {
715 found = true;
716 break;
717 } else
718 --walker;
719
720 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000721 if (!found) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000722 if (BlockUses.count(walker))
723 found = true;
724 else
725 goto Fallback;
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000726 }
727
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000728 unsigned UseIndex = LIs->getInstructionIndex(walker);
729 UseIndex = LiveIntervals::getUseIndex(UseIndex);
730 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000731 if (intrablock) {
732 EndIndex = LIs->getInstructionIndex(use);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000733 EndIndex = LiveIntervals::getUseIndex(EndIndex);
734 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000735 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000736
737 // Now, recursively phi construct the VNInfo for the use we found,
738 // and then extend it to include the instruction we care about
Owen Anderson200ee7f2009-01-06 07:53:32 +0000739 ret = PerformPHIConstruction(walker, MBB, LI, Visited, Defs, Uses,
Owen Anderson7d211e22008-12-31 02:00:25 +0000740 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000741
742 // FIXME: Need to set kills properly for inter-block stuff.
Owen Andersond4f6fe52008-12-28 23:35:13 +0000743 if (LI->isKill(ret, UseIndex)) LI->removeKill(ret, UseIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000744 if (intrablock)
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000745 LI->addKill(ret, EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000746
Owen Anderson7d211e22008-12-31 02:00:25 +0000747 LI->addRange(LiveRange(UseIndex, EndIndex+1, ret));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000748 } else if (ContainsDefs && ContainsUses){
Owen Anderson7d211e22008-12-31 02:00:25 +0000749 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
750 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000751
752 // This case is basically a merging of the two preceding case, with the
753 // special note that checking for defs must take precedence over checking
754 // for uses, because of two-address instructions.
755
Owen Anderson7d211e22008-12-31 02:00:25 +0000756 if (use == MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000757 goto Fallback;
758
759 MachineBasicBlock::iterator walker = use;
760 --walker;
761 bool foundDef = false;
762 bool foundUse = false;
Owen Anderson7d211e22008-12-31 02:00:25 +0000763 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000764 if (BlockDefs.count(walker)) {
765 foundDef = true;
766 break;
767 } else if (BlockUses.count(walker)) {
768 foundUse = true;
769 break;
770 } else
771 --walker;
772
773 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000774 if (!foundDef && !foundUse) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000775 if (BlockDefs.count(walker))
776 foundDef = true;
777 else if (BlockUses.count(walker))
778 foundUse = true;
779 else
780 goto Fallback;
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000781 }
782
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000783 unsigned StartIndex = LIs->getInstructionIndex(walker);
784 StartIndex = foundDef ? LiveIntervals::getDefIndex(StartIndex) :
785 LiveIntervals::getUseIndex(StartIndex);
786 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000787 if (intrablock) {
788 EndIndex = LIs->getInstructionIndex(use);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000789 EndIndex = LiveIntervals::getUseIndex(EndIndex);
790 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000791 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000792
793 if (foundDef)
794 ret = NewVNs[walker];
795 else
Owen Anderson200ee7f2009-01-06 07:53:32 +0000796 ret = PerformPHIConstruction(walker, MBB, LI, Visited, Defs, Uses,
Owen Anderson7d211e22008-12-31 02:00:25 +0000797 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000798
Owen Andersond4f6fe52008-12-28 23:35:13 +0000799 if (foundUse && LI->isKill(ret, StartIndex))
800 LI->removeKill(ret, StartIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000801 if (intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000802 LI->addKill(ret, EndIndex);
803 }
804
Owen Anderson7d211e22008-12-31 02:00:25 +0000805 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000806 }
807
808 // Memoize results so we don't have to recompute them.
Owen Anderson7d211e22008-12-31 02:00:25 +0000809 if (!intrablock) LiveOut[MBB] = ret;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000810 else {
811 NewVNs[use] = ret;
812 Visited.insert(use);
813 }
814
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000815 return ret;
816}
817
818/// ReconstructLiveInterval - Recompute a live interval from scratch.
819void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
820 BumpPtrAllocator& Alloc = LIs->getVNInfoAllocator();
821
822 // Clear the old ranges and valnos;
823 LI->clear();
824
825 // Cache the uses and defs of the register
826 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
827 RegMap Defs, Uses;
828
829 // Keep track of the new VNs we're creating.
830 DenseMap<MachineInstr*, VNInfo*> NewVNs;
831 SmallPtrSet<VNInfo*, 2> PhiVNs;
832
833 // Cache defs, and create a new VNInfo for each def.
834 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
835 DE = MRI->def_end(); DI != DE; ++DI) {
836 Defs[(*DI).getParent()].insert(&*DI);
837
838 unsigned DefIdx = LIs->getInstructionIndex(&*DI);
839 DefIdx = LiveIntervals::getDefIndex(DefIdx);
840
Owen Anderson200ee7f2009-01-06 07:53:32 +0000841 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, Alloc);
842
843 // If the def is a move, set the copy field.
844 unsigned source, dest;
845 if (TII->isMoveInstr(*DI, source, dest))
846 if (dest == LI->reg)
847 NewVN->copy = &*DI;
848
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000849 NewVNs[&*DI] = NewVN;
850 }
851
852 // Cache uses as a separate pass from actually processing them.
853 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
854 UE = MRI->use_end(); UI != UE; ++UI)
855 Uses[(*UI).getParent()].insert(&*UI);
856
857 // Now, actually process every use and use a phi construction algorithm
858 // to walk from it to its reaching definitions, building VNInfos along
859 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000860 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
861 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000862 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000863 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
864 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000865 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000866 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000867 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000868
869 // Add ranges for dead defs
870 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
871 DE = MRI->def_end(); DI != DE; ++DI) {
872 unsigned DefIdx = LIs->getInstructionIndex(&*DI);
873 DefIdx = LiveIntervals::getDefIndex(DefIdx);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000874
875 if (LI->liveAt(DefIdx)) continue;
876
877 VNInfo* DeadVN = NewVNs[&*DI];
Owen Anderson7d211e22008-12-31 02:00:25 +0000878 LI->addRange(LiveRange(DefIdx, DefIdx+1, DeadVN));
Owen Andersond4f6fe52008-12-28 23:35:13 +0000879 LI->addKill(DeadVN, DefIdx);
880 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000881}
882
Evan Chengf5cd4f02008-10-23 20:43:13 +0000883/// ShrinkWrapLiveInterval - Recursively traverse the predecessor
884/// chain to find the new 'kills' and shrink wrap the live interval to the
885/// new kill indices.
886void
Evan Chengaaf510c2008-10-26 07:49:03 +0000887PreAllocSplitting::ShrinkWrapLiveInterval(VNInfo *ValNo, MachineBasicBlock *MBB,
888 MachineBasicBlock *SuccMBB, MachineBasicBlock *DefMBB,
Evan Cheng06587492008-10-24 02:05:00 +0000889 SmallPtrSet<MachineBasicBlock*, 8> &Visited,
890 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > &Uses,
891 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > &UseMIs,
892 SmallVector<MachineBasicBlock*, 4> &UseMBBs) {
Evan Chengaaf510c2008-10-26 07:49:03 +0000893 if (Visited.count(MBB))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000894 return;
895
Evan Chengaaf510c2008-10-26 07:49:03 +0000896 // If live interval is live in another successor path, then we can't process
897 // this block. But we may able to do so after all the successors have been
898 // processed.
Evan Chengf62ce372008-10-28 00:47:49 +0000899 if (MBB != BarrierMBB) {
900 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
901 SE = MBB->succ_end(); SI != SE; ++SI) {
902 MachineBasicBlock *SMBB = *SI;
903 if (SMBB == SuccMBB)
904 continue;
905 if (CurrLI->liveAt(LIs->getMBBStartIdx(SMBB)))
906 return;
907 }
Evan Chengaaf510c2008-10-26 07:49:03 +0000908 }
909
910 Visited.insert(MBB);
911
Evan Cheng06587492008-10-24 02:05:00 +0000912 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
913 UMII = Uses.find(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000914 if (UMII != Uses.end()) {
915 // At least one use in this mbb, lets look for the kill.
Evan Cheng06587492008-10-24 02:05:00 +0000916 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
917 UMII2 = UseMIs.find(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000918 if (ShrinkWrapToLastUse(MBB, ValNo, UMII->second, UMII2->second))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000919 // Found a kill, shrink wrapping of this path ends here.
920 return;
Evan Cheng06587492008-10-24 02:05:00 +0000921 } else if (MBB == DefMBB) {
Evan Cheng06587492008-10-24 02:05:00 +0000922 // There are no uses after the def.
923 MachineInstr *DefMI = LIs->getInstructionFromIndex(ValNo->def);
Evan Cheng06587492008-10-24 02:05:00 +0000924 if (UseMBBs.empty()) {
925 // The only use must be below barrier in the barrier block. It's safe to
926 // remove the def.
927 LIs->RemoveMachineInstrFromMaps(DefMI);
928 DefMI->eraseFromParent();
929 CurrLI->removeRange(ValNo->def, LIs->getMBBEndIdx(MBB)+1);
930 }
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000931 } else if (MBB == BarrierMBB) {
932 // Remove entire live range from start of mbb to barrier.
933 CurrLI->removeRange(LIs->getMBBStartIdx(MBB),
934 LIs->getUseIndex(BarrierIdx)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000935 } else {
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000936 // Remove entire live range of the mbb out of the live interval.
Evan Cheng06587492008-10-24 02:05:00 +0000937 CurrLI->removeRange(LIs->getMBBStartIdx(MBB), LIs->getMBBEndIdx(MBB)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000938 }
939
940 if (MBB == DefMBB)
941 // Reached the def mbb, stop traversing this path further.
942 return;
943
944 // Traverse the pathes up the predecessor chains further.
945 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
946 PE = MBB->pred_end(); PI != PE; ++PI) {
947 MachineBasicBlock *Pred = *PI;
948 if (Pred == MBB)
949 continue;
950 if (Pred == DefMBB && ValNo->hasPHIKill)
951 // Pred is the def bb and the def reaches other val#s, we must
952 // allow the value to be live out of the bb.
953 continue;
Owen Anderson80fe8732008-11-11 22:11:27 +0000954 if (!CurrLI->liveAt(LIs->getMBBEndIdx(Pred)-1))
955 return;
Evan Chengaaf510c2008-10-26 07:49:03 +0000956 ShrinkWrapLiveInterval(ValNo, Pred, MBB, DefMBB, Visited,
957 Uses, UseMIs, UseMBBs);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000958 }
959
960 return;
961}
962
Owen Anderson75fa96b2008-11-19 04:28:29 +0000963
Owen Anderson6002e992008-12-04 21:20:30 +0000964void PreAllocSplitting::RepairLiveInterval(LiveInterval* CurrLI,
965 VNInfo* ValNo,
966 MachineInstr* DefMI,
967 unsigned RestoreIdx) {
Owen Anderson75fa96b2008-11-19 04:28:29 +0000968 // Shrink wrap the live interval by walking up the CFG and find the
969 // new kills.
970 // Now let's find all the uses of the val#.
971 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > Uses;
972 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > UseMIs;
973 SmallPtrSet<MachineBasicBlock*, 4> Seen;
974 SmallVector<MachineBasicBlock*, 4> UseMBBs;
975 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(CurrLI->reg),
976 UE = MRI->use_end(); UI != UE; ++UI) {
977 MachineOperand &UseMO = UI.getOperand();
978 MachineInstr *UseMI = UseMO.getParent();
979 unsigned UseIdx = LIs->getInstructionIndex(UseMI);
980 LiveInterval::iterator ULR = CurrLI->FindLiveRangeContaining(UseIdx);
981 if (ULR->valno != ValNo)
982 continue;
983 MachineBasicBlock *UseMBB = UseMI->getParent();
984 // Remember which other mbb's use this val#.
985 if (Seen.insert(UseMBB) && UseMBB != BarrierMBB)
986 UseMBBs.push_back(UseMBB);
987 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
988 UMII = Uses.find(UseMBB);
989 if (UMII != Uses.end()) {
990 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
991 UMII2 = UseMIs.find(UseMBB);
992 UMII->second.push_back(&UseMO);
993 UMII2->second.insert(UseMI);
994 } else {
995 SmallVector<MachineOperand*, 4> Ops;
996 Ops.push_back(&UseMO);
997 Uses.insert(std::make_pair(UseMBB, Ops));
998 SmallPtrSet<MachineInstr*, 4> MIs;
999 MIs.insert(UseMI);
1000 UseMIs.insert(std::make_pair(UseMBB, MIs));
1001 }
1002 }
1003
1004 // Walk up the predecessor chains.
1005 SmallPtrSet<MachineBasicBlock*, 8> Visited;
1006 ShrinkWrapLiveInterval(ValNo, BarrierMBB, NULL, DefMI->getParent(), Visited,
1007 Uses, UseMIs, UseMBBs);
1008
Owen Anderson75fa96b2008-11-19 04:28:29 +00001009 // Remove live range from barrier to the restore. FIXME: Find a better
1010 // point to re-start the live interval.
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001011 VNInfo* AfterValNo = UpdateRegisterInterval(ValNo,
1012 LIs->getUseIndex(BarrierIdx)+1,
1013 LIs->getDefIndex(RestoreIdx));
1014
1015 // Attempt to renumber the new valno into a new vreg.
1016 RenumberValno(AfterValNo);
Owen Anderson6002e992008-12-04 21:20:30 +00001017}
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001018
1019/// RenumberValno - Split the given valno out into a new vreg, allowing it to
1020/// be allocated to a different register. This function creates a new vreg,
1021/// copies the valno and its live ranges over to the new vreg's interval,
1022/// removes them from the old interval, and rewrites all uses and defs of
1023/// the original reg to the new vreg within those ranges.
1024void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001025 SmallVector<VNInfo*, 4> Stack;
1026 SmallVector<VNInfo*, 4> VNsToCopy;
1027 Stack.push_back(VN);
1028
1029 // Walk through and copy the valno we care about, and any other valnos
1030 // that are two-address redefinitions of the one we care about. These
1031 // will need to be rewritten as well. We also check for safety of the
1032 // renumbering here, by making sure that none of the valno involved has
1033 // phi kills.
1034 while (!Stack.empty()) {
1035 VNInfo* OldVN = Stack.back();
1036 Stack.pop_back();
1037
1038 // Bail out if we ever encounter a valno that has a PHI kill. We can't
1039 // renumber these.
1040 if (OldVN->hasPHIKill) return;
1041
1042 VNsToCopy.push_back(OldVN);
1043
1044 // Locate two-address redefinitions
1045 for (SmallVector<unsigned, 4>::iterator KI = OldVN->kills.begin(),
1046 KE = OldVN->kills.end(); KI != KE; ++KI) {
1047 MachineInstr* MI = LIs->getInstructionFromIndex(*KI);
1048 //if (!MI) continue;
1049 unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg);
1050 if (DefIdx == ~0U) continue;
1051 if (MI->isRegReDefinedByTwoAddr(DefIdx)) {
1052 VNInfo* NextVN =
1053 CurrLI->findDefinedVNInfo(LiveIntervals::getDefIndex(*KI));
1054 Stack.push_back(NextVN);
1055 }
1056 }
1057 }
1058
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001059 // Create the new vreg
1060 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
1061
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001062 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001063 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001064
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001065 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
1066 VNsToCopy.end(); OI != OE; ++OI) {
1067 VNInfo* OldVN = *OI;
1068
1069 // Copy the valno over
1070 VNInfo* NewVN = NewLI.getNextValue(OldVN->def, OldVN->copy,
1071 LIs->getVNInfoAllocator());
1072 NewLI.copyValNumInfo(NewVN, OldVN);
1073 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
1074
1075 // Remove the valno from the old interval
1076 CurrLI->removeValNo(OldVN);
1077 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001078
1079 // Rewrite defs and uses. This is done in two stages to avoid invalidating
1080 // the reg_iterator.
1081 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
1082
1083 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
1084 E = MRI->reg_end(); I != E; ++I) {
1085 MachineOperand& MO = I.getOperand();
1086 unsigned InstrIdx = LIs->getInstructionIndex(&*I);
1087
1088 if ((MO.isUse() && NewLI.liveAt(LiveIntervals::getUseIndex(InstrIdx))) ||
1089 (MO.isDef() && NewLI.liveAt(LiveIntervals::getDefIndex(InstrIdx))))
1090 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
1091 }
1092
1093 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
1094 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
1095 MachineInstr* Inst = I->first;
1096 unsigned OpIdx = I->second;
1097 MachineOperand& MO = Inst->getOperand(OpIdx);
1098 MO.setReg(NewVReg);
1099 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001100
1101 NumRenumbers++;
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001102}
1103
Owen Anderson6002e992008-12-04 21:20:30 +00001104bool PreAllocSplitting::Rematerialize(unsigned vreg, VNInfo* ValNo,
1105 MachineInstr* DefMI,
1106 MachineBasicBlock::iterator RestorePt,
1107 unsigned RestoreIdx,
1108 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
1109 MachineBasicBlock& MBB = *RestorePt->getParent();
1110
1111 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
1112 unsigned KillIdx = 0;
1113 if (ValNo->def == ~0U || DefMI->getParent() == BarrierMBB)
1114 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx);
1115 else
1116 KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx);
1117
1118 if (KillPt == DefMI->getParent()->end())
1119 return false;
1120
1121 TII->reMaterialize(MBB, RestorePt, vreg, DefMI);
1122 LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx);
1123
1124 if (KillPt->getParent() == BarrierMBB) {
1125 UpdateRegisterInterval(ValNo, LIs->getUseIndex(KillIdx)+1,
1126 LIs->getDefIndex(RestoreIdx));
1127
1128 ++NumSplits;
1129 ++NumRemats;
1130 return true;
1131 }
1132
1133 RepairLiveInterval(CurrLI, ValNo, DefMI, RestoreIdx);
Owen Anderson75fa96b2008-11-19 04:28:29 +00001134 ++NumSplits;
1135 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001136 return true;
1137}
1138
1139MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
1140 const TargetRegisterClass* RC,
1141 MachineInstr* DefMI,
1142 MachineInstr* Barrier,
1143 MachineBasicBlock* MBB,
1144 int& SS,
1145 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
1146 MachineBasicBlock::iterator Pt = MBB->begin();
1147
1148 // Go top down if RefsInMBB is empty.
1149 if (RefsInMBB.empty())
1150 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +00001151
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001152 MachineBasicBlock::iterator FoldPt = Barrier;
1153 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
1154 !RefsInMBB.count(FoldPt))
1155 --FoldPt;
1156
1157 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
1158 if (OpIdx == -1)
1159 return 0;
1160
1161 SmallVector<unsigned, 1> Ops;
1162 Ops.push_back(OpIdx);
1163
1164 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
1165 return 0;
1166
1167 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
1168 if (I != IntervalSSMap.end()) {
1169 SS = I->second;
1170 } else {
1171 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
1172
1173 }
1174
1175 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
1176 FoldPt, Ops, SS);
1177
1178 if (FMI) {
1179 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
1180 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
1181 ++NumFolds;
1182
1183 IntervalSSMap[vreg] = SS;
1184 CurrSLI = &LSs->getOrCreateInterval(SS);
1185 if (CurrSLI->hasAtLeastOneValue())
1186 CurrSValNo = CurrSLI->getValNumInfo(0);
1187 else
1188 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
1189 }
1190
1191 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +00001192}
1193
Evan Chengf5cd4f02008-10-23 20:43:13 +00001194/// SplitRegLiveInterval - Split (spill and restore) the given live interval
1195/// so it would not cross the barrier that's being processed. Shrink wrap
1196/// (minimize) the live interval to the last uses.
1197bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
1198 CurrLI = LI;
1199
1200 // Find live range where current interval cross the barrier.
1201 LiveInterval::iterator LR =
1202 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
1203 VNInfo *ValNo = LR->valno;
1204
1205 if (ValNo->def == ~1U) {
1206 // Defined by a dead def? How can this be?
1207 assert(0 && "Val# is defined by a dead def?");
1208 abort();
1209 }
1210
Evan Cheng06587492008-10-24 02:05:00 +00001211 MachineInstr *DefMI = (ValNo->def != ~0U)
1212 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +00001213
Owen Andersonb214c692008-11-05 00:32:13 +00001214 // If this would create a new join point, do not split.
1215 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent()))
1216 return false;
1217
Evan Chengf5cd4f02008-10-23 20:43:13 +00001218 // Find all references in the barrier mbb.
1219 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
1220 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
1221 E = MRI->reg_end(); I != E; ++I) {
1222 MachineInstr *RefMI = &*I;
1223 if (RefMI->getParent() == BarrierMBB)
1224 RefsInMBB.insert(RefMI);
1225 }
1226
1227 // Find a point to restore the value after the barrier.
1228 unsigned RestoreIndex;
1229 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +00001230 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +00001231 if (RestorePt == BarrierMBB->end())
1232 return false;
1233
Owen Anderson75fa96b2008-11-19 04:28:29 +00001234 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
1235 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt,
1236 RestoreIndex, RefsInMBB))
1237 return true;
1238
Evan Chengf5cd4f02008-10-23 20:43:13 +00001239 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001240 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001241 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Evan Chengf5cd4f02008-10-23 20:43:13 +00001242 unsigned SpillIndex = 0;
Evan Cheng06587492008-10-24 02:05:00 +00001243 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001244 int SS = -1;
Evan Cheng78dfef72008-10-25 00:52:41 +00001245 if (ValNo->def == ~0U) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001246 // If it's defined by a phi, we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001247 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1248 BarrierMBB, SS, RefsInMBB))) {
1249 SpillIndex = LIs->getInstructionIndex(SpillMI);
1250 } else {
1251 MachineBasicBlock::iterator SpillPt =
1252 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
1253 if (SpillPt == BarrierMBB->begin())
1254 return false; // No gap to insert spill.
1255 // Add spill.
1256
1257 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1258 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
1259 SpillMI = prior(SpillPt);
1260 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
1261 }
Evan Cheng54898932008-10-29 08:39:34 +00001262 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
1263 RestoreIndex, SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001264 // If it's already split, just restore the value. There is no need to spill
1265 // the def again.
Evan Chengd0e32c52008-10-29 05:06:14 +00001266 if (!DefMI)
1267 return false; // Def is dead. Do nothing.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001268
1269 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
1270 BarrierMBB, SS, RefsInMBB))) {
1271 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001272 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001273 // Check if it's possible to insert a spill after the def MI.
1274 MachineBasicBlock::iterator SpillPt;
1275 if (DefMBB == BarrierMBB) {
1276 // Add spill after the def and the last use before the barrier.
1277 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
1278 RefsInMBB, SpillIndex);
1279 if (SpillPt == DefMBB->begin())
1280 return false; // No gap to insert spill.
1281 } else {
1282 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
1283 if (SpillPt == DefMBB->end())
1284 return false; // No gap to insert spill.
1285 }
1286 // Add spill. The store instruction kills the register if def is before
1287 // the barrier in the barrier block.
1288 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1289 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
1290 DefMBB == BarrierMBB, SS, RC);
1291 SpillMI = prior(SpillPt);
1292 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001293 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001294 }
1295
Evan Cheng54898932008-10-29 08:39:34 +00001296 // Remember def instruction index to spill index mapping.
1297 if (DefMI && SpillMI)
1298 Def2SpillMap[ValNo->def] = SpillIndex;
1299
Evan Chengf5cd4f02008-10-23 20:43:13 +00001300 // Add restore.
Evan Chengf5cd4f02008-10-23 20:43:13 +00001301 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
1302 MachineInstr *LoadMI = prior(RestorePt);
1303 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
1304
1305 // If live interval is spilled in the same block as the barrier, just
1306 // create a hole in the interval.
1307 if (!DefMBB ||
Evan Cheng78dfef72008-10-25 00:52:41 +00001308 (SpillMI && SpillMI->getParent() == BarrierMBB)) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001309 // Update spill stack slot live interval.
1310 UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
1311 LIs->getDefIndex(RestoreIndex));
Evan Chengf5cd4f02008-10-23 20:43:13 +00001312
Evan Chengd0e32c52008-10-29 05:06:14 +00001313 UpdateRegisterInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
1314 LIs->getDefIndex(RestoreIndex));
Owen Anderson7d211e22008-12-31 02:00:25 +00001315
Evan Chengae7fa5b2008-10-28 01:48:24 +00001316 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001317 return true;
1318 }
1319
Evan Chengd0e32c52008-10-29 05:06:14 +00001320 // Update spill stack slot live interval.
Evan Cheng54898932008-10-29 08:39:34 +00001321 UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
1322 LIs->getDefIndex(RestoreIndex));
Evan Chengd0e32c52008-10-29 05:06:14 +00001323
Owen Anderson6002e992008-12-04 21:20:30 +00001324 RepairLiveInterval(CurrLI, ValNo, DefMI, RestoreIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +00001325
Evan Chengae7fa5b2008-10-28 01:48:24 +00001326 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001327 return true;
1328}
1329
1330/// SplitRegLiveIntervals - Split all register live intervals that cross the
1331/// barrier that's being processed.
1332bool
1333PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs) {
1334 // First find all the virtual registers whose live intervals are intercepted
1335 // by the current barrier.
1336 SmallVector<LiveInterval*, 8> Intervals;
1337 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng23066282008-10-27 07:14:50 +00001338 if (TII->IgnoreRegisterClassBarriers(*RC))
1339 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001340 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
1341 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1342 unsigned Reg = VRs[i];
1343 if (!LIs->hasInterval(Reg))
1344 continue;
1345 LiveInterval *LI = &LIs->getInterval(Reg);
1346 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1347 // Virtual register live interval is intercepted by the barrier. We
1348 // should split and shrink wrap its interval if possible.
1349 Intervals.push_back(LI);
1350 }
1351 }
1352
1353 // Process the affected live intervals.
1354 bool Change = false;
1355 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001356 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1357 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001358 LiveInterval *LI = Intervals.back();
1359 Intervals.pop_back();
1360 Change |= SplitRegLiveInterval(LI);
1361 }
1362
1363 return Change;
1364}
1365
Owen Andersonf1f75b12008-11-04 22:22:41 +00001366bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1367 MachineBasicBlock* DefMBB,
1368 MachineBasicBlock* BarrierMBB) {
1369 if (DefMBB == BarrierMBB)
1370 return false;
1371
1372 if (LR->valno->hasPHIKill)
1373 return false;
1374
1375 unsigned MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
1376 if (LR->end < MBBEnd)
1377 return false;
1378
1379 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1380 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1381 return true;
1382
1383 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1384 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1385 typedef std::pair<MachineBasicBlock*,
1386 MachineBasicBlock::succ_iterator> ItPair;
1387 SmallVector<ItPair, 4> Stack;
1388 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1389
1390 while (!Stack.empty()) {
1391 ItPair P = Stack.back();
1392 Stack.pop_back();
1393
1394 MachineBasicBlock* PredMBB = P.first;
1395 MachineBasicBlock::succ_iterator S = P.second;
1396
1397 if (S == PredMBB->succ_end())
1398 continue;
1399 else if (Visited.count(*S)) {
1400 Stack.push_back(std::make_pair(PredMBB, ++S));
1401 continue;
1402 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001403 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001404
1405 MachineBasicBlock* MBB = *S;
1406 Visited.insert(MBB);
1407
1408 if (MBB == BarrierMBB)
1409 return true;
1410
1411 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1412 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1413 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1414 while (MDTN) {
1415 if (MDTN == DefMDTN)
1416 return true;
1417 else if (MDTN == BarrierMDTN)
1418 break;
1419 MDTN = MDTN->getIDom();
1420 }
1421
1422 MBBEnd = LIs->getMBBEndIdx(MBB);
1423 if (LR->end > MBBEnd)
1424 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1425 }
1426
1427 return false;
1428}
1429
1430
Evan Cheng09e8ca82008-10-20 21:44:59 +00001431bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001432 CurrMF = &MF;
1433 TM = &MF.getTarget();
1434 TII = TM->getInstrInfo();
1435 MFI = MF.getFrameInfo();
1436 MRI = &MF.getRegInfo();
1437 LIs = &getAnalysis<LiveIntervals>();
1438 LSs = &getAnalysis<LiveStacks>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001439
1440 bool MadeChange = false;
1441
1442 // Make sure blocks are numbered in order.
1443 MF.RenumberBlocks();
1444
Evan Cheng54898932008-10-29 08:39:34 +00001445 MachineBasicBlock *Entry = MF.begin();
1446 SmallPtrSet<MachineBasicBlock*,16> Visited;
1447
1448 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1449 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1450 DFI != E; ++DFI) {
1451 BarrierMBB = *DFI;
1452 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1453 E = BarrierMBB->end(); I != E; ++I) {
1454 Barrier = &*I;
1455 const TargetRegisterClass **BarrierRCs =
1456 Barrier->getDesc().getRegClassBarriers();
1457 if (!BarrierRCs)
1458 continue;
1459 BarrierIdx = LIs->getInstructionIndex(Barrier);
1460 MadeChange |= SplitRegLiveIntervals(BarrierRCs);
1461 }
1462 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001463
1464 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001465}