blob: f07f0700945de4a72ba53d8b8f8871774f902d3a [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");
Owen Anderson956ec272009-01-23 00:23:32 +000045STATISTIC(NumDeadSpills, "Number of dead spills removed");
Evan Chengf5cd4f02008-10-23 20:43:13 +000046
Evan Cheng09e8ca82008-10-20 21:44:59 +000047namespace {
48 class VISIBILITY_HIDDEN PreAllocSplitting : public MachineFunctionPass {
Evan Chengd0e32c52008-10-29 05:06:14 +000049 MachineFunction *CurrMF;
Evan Chengf5cd4f02008-10-23 20:43:13 +000050 const TargetMachine *TM;
51 const TargetInstrInfo *TII;
52 MachineFrameInfo *MFI;
53 MachineRegisterInfo *MRI;
54 LiveIntervals *LIs;
Evan Chengd0e32c52008-10-29 05:06:14 +000055 LiveStacks *LSs;
Evan Cheng09e8ca82008-10-20 21:44:59 +000056
Evan Chengf5cd4f02008-10-23 20:43:13 +000057 // Barrier - Current barrier being processed.
58 MachineInstr *Barrier;
59
60 // BarrierMBB - Basic block where the barrier resides in.
61 MachineBasicBlock *BarrierMBB;
62
63 // Barrier - Current barrier index.
64 unsigned BarrierIdx;
65
66 // CurrLI - Current live interval being split.
67 LiveInterval *CurrLI;
68
Evan Chengd0e32c52008-10-29 05:06:14 +000069 // CurrSLI - Current stack slot live interval.
70 LiveInterval *CurrSLI;
71
72 // CurrSValNo - Current val# for the stack slot live interval.
73 VNInfo *CurrSValNo;
74
75 // IntervalSSMap - A map from live interval to spill slots.
76 DenseMap<unsigned, int> IntervalSSMap;
Evan Chengf5cd4f02008-10-23 20:43:13 +000077
Evan Cheng54898932008-10-29 08:39:34 +000078 // Def2SpillMap - A map from a def instruction index to spill index.
79 DenseMap<unsigned, unsigned> Def2SpillMap;
Evan Cheng06587492008-10-24 02:05:00 +000080
Evan Cheng09e8ca82008-10-20 21:44:59 +000081 public:
82 static char ID;
83 PreAllocSplitting() : MachineFunctionPass(&ID) {}
84
85 virtual bool runOnMachineFunction(MachineFunction &MF);
86
87 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
88 AU.addRequired<LiveIntervals>();
89 AU.addPreserved<LiveIntervals>();
Evan Chengd0e32c52008-10-29 05:06:14 +000090 AU.addRequired<LiveStacks>();
91 AU.addPreserved<LiveStacks>();
Evan Cheng09e8ca82008-10-20 21:44:59 +000092 AU.addPreserved<RegisterCoalescer>();
93 if (StrongPHIElim)
94 AU.addPreservedID(StrongPHIEliminationID);
95 else
96 AU.addPreservedID(PHIEliminationID);
Owen Andersonf1f75b12008-11-04 22:22:41 +000097 AU.addRequired<MachineDominatorTree>();
98 AU.addRequired<MachineLoopInfo>();
99 AU.addPreserved<MachineDominatorTree>();
100 AU.addPreserved<MachineLoopInfo>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000101 MachineFunctionPass::getAnalysisUsage(AU);
102 }
103
104 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +0000105 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +0000106 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000107 }
108
109 virtual const char *getPassName() const {
110 return "Pre-Register Allocaton Live Interval Splitting";
111 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000112
113 /// print - Implement the dump method.
114 virtual void print(std::ostream &O, const Module* M = 0) const {
115 LIs->print(O, M);
116 }
117
118 void print(std::ostream *O, const Module* M = 0) const {
119 if (O) print(*O, M);
120 }
121
122 private:
123 MachineBasicBlock::iterator
124 findNextEmptySlot(MachineBasicBlock*, MachineInstr*,
125 unsigned&);
126
127 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000128 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000129 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
130
131 MachineBasicBlock::iterator
Evan Chengf62ce372008-10-28 00:47:49 +0000132 findRestorePoint(MachineBasicBlock*, MachineInstr*, unsigned,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000133 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
134
Evan Chengd0e32c52008-10-29 05:06:14 +0000135 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000136
Evan Cheng54898932008-10-29 08:39:34 +0000137 bool IsAvailableInStack(MachineBasicBlock*, unsigned, unsigned, unsigned,
138 unsigned&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000139
Evan Chengd0e32c52008-10-29 05:06:14 +0000140 void UpdateSpillSlotInterval(VNInfo*, unsigned, unsigned);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000141
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000142 VNInfo* UpdateRegisterInterval(VNInfo*, unsigned, unsigned);
Evan Chengd0e32c52008-10-29 05:06:14 +0000143
144 bool ShrinkWrapToLastUse(MachineBasicBlock*, VNInfo*,
Evan Cheng06587492008-10-24 02:05:00 +0000145 SmallVector<MachineOperand*, 4>&,
146 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000147
Evan Chengaaf510c2008-10-26 07:49:03 +0000148 void ShrinkWrapLiveInterval(VNInfo*, MachineBasicBlock*, MachineBasicBlock*,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000149 MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 8>&,
Evan Cheng06587492008-10-24 02:05:00 +0000150 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >&,
151 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >&,
152 SmallVector<MachineBasicBlock*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000153
154 bool SplitRegLiveInterval(LiveInterval*);
155
Owen Anderson956ec272009-01-23 00:23:32 +0000156 bool SplitRegLiveIntervals(const TargetRegisterClass **,
157 SmallPtrSet<LiveInterval*, 8>&);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000158
Owen Anderson6002e992008-12-04 21:20:30 +0000159 void RepairLiveInterval(LiveInterval* CurrLI, VNInfo* ValNo,
160 MachineInstr* DefMI, unsigned RestoreIdx);
161
Owen Andersonf1f75b12008-11-04 22:22:41 +0000162 bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB,
163 MachineBasicBlock* BarrierMBB);
Owen Anderson75fa96b2008-11-19 04:28:29 +0000164 bool Rematerialize(unsigned vreg, VNInfo* ValNo,
165 MachineInstr* DefMI,
166 MachineBasicBlock::iterator RestorePt,
167 unsigned RestoreIdx,
168 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000169 MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC,
170 MachineInstr* DefMI,
171 MachineInstr* Barrier,
172 MachineBasicBlock* MBB,
173 int& SS,
174 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000175 void RenumberValno(VNInfo* VN);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000176 void ReconstructLiveInterval(LiveInterval* LI);
Owen Anderson956ec272009-01-23 00:23:32 +0000177 bool removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000178 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator use,
Owen Anderson7d211e22008-12-31 02:00:25 +0000179 MachineBasicBlock* MBB,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000180 LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000181 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000182 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
183 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
184 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000185 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
186 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
187 bool toplevel, bool intrablock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000188};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000189} // end anonymous namespace
190
191char PreAllocSplitting::ID = 0;
192
193static RegisterPass<PreAllocSplitting>
194X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
195
196const PassInfo *const llvm::PreAllocSplittingID = &X;
197
Evan Chengf5cd4f02008-10-23 20:43:13 +0000198
199/// findNextEmptySlot - Find a gap after the given machine instruction in the
200/// instruction index map. If there isn't one, return end().
201MachineBasicBlock::iterator
202PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
203 unsigned &SpotIndex) {
204 MachineBasicBlock::iterator MII = MI;
205 if (++MII != MBB->end()) {
206 unsigned Index = LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
207 if (Index) {
208 SpotIndex = Index;
209 return MII;
210 }
211 }
212 return MBB->end();
213}
214
215/// findSpillPoint - Find a gap as far away from the given MI that's suitable
216/// for spilling the current live interval. The index must be before any
217/// defs and uses of the live interval register in the mbb. Return begin() if
218/// none is found.
219MachineBasicBlock::iterator
220PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000221 MachineInstr *DefMI,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000222 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
223 unsigned &SpillIndex) {
224 MachineBasicBlock::iterator Pt = MBB->begin();
225
226 // Go top down if RefsInMBB is empty.
Evan Cheng1f08cc22008-10-28 05:28:21 +0000227 if (RefsInMBB.empty() && !DefMI) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000228 MachineBasicBlock::iterator MII = MBB->begin();
229 MachineBasicBlock::iterator EndPt = MI;
230 do {
231 ++MII;
232 unsigned Index = LIs->getInstructionIndex(MII);
233 unsigned Gap = LIs->findGapBeforeInstr(Index);
234 if (Gap) {
235 Pt = MII;
236 SpillIndex = Gap;
237 break;
238 }
239 } while (MII != EndPt);
240 } else {
241 MachineBasicBlock::iterator MII = MI;
Evan Cheng1f08cc22008-10-28 05:28:21 +0000242 MachineBasicBlock::iterator EndPt = DefMI
243 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
244 while (MII != EndPt && !RefsInMBB.count(MII)) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000245 unsigned Index = LIs->getInstructionIndex(MII);
246 if (LIs->hasGapBeforeInstr(Index)) {
247 Pt = MII;
248 SpillIndex = LIs->findGapBeforeInstr(Index, true);
249 }
250 --MII;
251 }
252 }
253
254 return Pt;
255}
256
257/// findRestorePoint - Find a gap in the instruction index map that's suitable
258/// for restoring the current live interval value. The index must be before any
259/// uses of the live interval register in the mbb. Return end() if none is
260/// found.
261MachineBasicBlock::iterator
262PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Chengf62ce372008-10-28 00:47:49 +0000263 unsigned LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000264 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
265 unsigned &RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000266 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
267 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000268 MachineBasicBlock::iterator Pt = MBB->end();
Evan Chengf62ce372008-10-28 00:47:49 +0000269 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000270
Evan Chengf62ce372008-10-28 00:47:49 +0000271 // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond
272 // the last index in the live range.
273 if (RefsInMBB.empty() && LastIdx >= EndIdx) {
Owen Anderson711fd3d2008-11-13 21:53:14 +0000274 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000275 MachineBasicBlock::iterator EndPt = MI;
Evan Cheng54898932008-10-29 08:39:34 +0000276 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000277 do {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000278 unsigned Index = LIs->getInstructionIndex(MII);
Evan Cheng56ab0de2008-10-24 18:46:44 +0000279 unsigned Gap = LIs->findGapBeforeInstr(Index);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000280 if (Gap) {
281 Pt = MII;
282 RestoreIndex = Gap;
283 break;
284 }
Evan Cheng54898932008-10-29 08:39:34 +0000285 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000286 } while (MII != EndPt);
287 } else {
288 MachineBasicBlock::iterator MII = MI;
289 MII = ++MII;
Evan Chengf62ce372008-10-28 00:47:49 +0000290 // FIXME: Limit the number of instructions to examine to reduce
291 // compile time?
Evan Chengf5cd4f02008-10-23 20:43:13 +0000292 while (MII != MBB->end()) {
293 unsigned Index = LIs->getInstructionIndex(MII);
Evan Chengf62ce372008-10-28 00:47:49 +0000294 if (Index > LastIdx)
295 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000296 unsigned Gap = LIs->findGapBeforeInstr(Index);
297 if (Gap) {
298 Pt = MII;
299 RestoreIndex = Gap;
300 }
301 if (RefsInMBB.count(MII))
302 break;
303 ++MII;
304 }
305 }
306
307 return Pt;
308}
309
Evan Chengd0e32c52008-10-29 05:06:14 +0000310/// CreateSpillStackSlot - Create a stack slot for the live interval being
311/// split. If the live interval was previously split, just reuse the same
312/// slot.
313int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
314 const TargetRegisterClass *RC) {
315 int SS;
316 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
317 if (I != IntervalSSMap.end()) {
318 SS = I->second;
319 } else {
320 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
321 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000322 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000323
324 // Create live interval for stack slot.
325 CurrSLI = &LSs->getOrCreateInterval(SS);
Evan Cheng54898932008-10-29 08:39:34 +0000326 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000327 CurrSValNo = CurrSLI->getValNumInfo(0);
328 else
329 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
330 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000331}
332
Evan Chengd0e32c52008-10-29 05:06:14 +0000333/// IsAvailableInStack - Return true if register is available in a split stack
334/// slot at the specified index.
335bool
Evan Cheng54898932008-10-29 08:39:34 +0000336PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
337 unsigned Reg, unsigned DefIndex,
338 unsigned RestoreIndex, unsigned &SpillIndex,
339 int& SS) const {
340 if (!DefMBB)
341 return false;
342
Evan Chengd0e32c52008-10-29 05:06:14 +0000343 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
344 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000345 return false;
Evan Cheng54898932008-10-29 08:39:34 +0000346 DenseMap<unsigned, unsigned>::iterator II = Def2SpillMap.find(DefIndex);
347 if (II == Def2SpillMap.end())
348 return false;
349
350 // If last spill of def is in the same mbb as barrier mbb (where restore will
351 // be), make sure it's not below the intended restore index.
352 // FIXME: Undo the previous spill?
353 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
354 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
355 return false;
356
357 SS = I->second;
358 SpillIndex = II->second;
359 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000360}
361
Evan Chengd0e32c52008-10-29 05:06:14 +0000362/// UpdateSpillSlotInterval - Given the specified val# of the register live
363/// interval being split, and the spill and restore indicies, update the live
364/// interval of the spill stack slot.
365void
366PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, unsigned SpillIndex,
367 unsigned RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000368 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
369 "Expect restore in the barrier mbb");
370
371 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
372 if (MBB == BarrierMBB) {
373 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000374 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
375 CurrSLI->addRange(SLR);
376 return;
377 }
378
Evan Cheng54898932008-10-29 08:39:34 +0000379 SmallPtrSet<MachineBasicBlock*, 4> Processed;
380 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
381 LiveRange SLR(SpillIndex, EndIdx+1, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000382 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000383 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000384
385 // Start from the spill mbb, figure out the extend of the spill slot's
386 // live interval.
387 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000388 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
389 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000390 // If live range extend beyond end of mbb, add successors to work list.
391 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
392 SE = MBB->succ_end(); SI != SE; ++SI)
393 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000394
395 while (!WorkList.empty()) {
396 MachineBasicBlock *MBB = WorkList.back();
397 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000398 if (Processed.count(MBB))
399 continue;
Evan Chengd0e32c52008-10-29 05:06:14 +0000400 unsigned Idx = LIs->getMBBStartIdx(MBB);
401 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000402 if (LR && LR->valno == ValNo) {
403 EndIdx = LIs->getMBBEndIdx(MBB);
404 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000405 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000406 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000407 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000408 } else if (LR->end > EndIdx) {
409 // Live range extends beyond end of mbb, process successors.
410 LiveRange SLR(Idx, EndIdx+1, CurrSValNo);
411 CurrSLI->addRange(SLR);
412 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
413 SE = MBB->succ_end(); SI != SE; ++SI)
414 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000415 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000416 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000417 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000418 }
Evan Cheng54898932008-10-29 08:39:34 +0000419 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000420 }
421 }
422}
423
424/// UpdateRegisterInterval - Given the specified val# of the current live
425/// interval is being split, and the spill and restore indices, update the live
Evan Chengf5cd4f02008-10-23 20:43:13 +0000426/// interval accordingly.
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000427VNInfo*
Evan Chengd0e32c52008-10-29 05:06:14 +0000428PreAllocSplitting::UpdateRegisterInterval(VNInfo *ValNo, unsigned SpillIndex,
429 unsigned RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000430 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
431 "Expect restore in the barrier mbb");
432
Evan Chengf5cd4f02008-10-23 20:43:13 +0000433 SmallVector<std::pair<unsigned,unsigned>, 4> Before;
434 SmallVector<std::pair<unsigned,unsigned>, 4> After;
435 SmallVector<unsigned, 4> BeforeKills;
436 SmallVector<unsigned, 4> AfterKills;
437 SmallPtrSet<const LiveRange*, 4> Processed;
438
439 // First, let's figure out which parts of the live interval is now defined
440 // by the restore, which are defined by the original definition.
Evan Chengd0e32c52008-10-29 05:06:14 +0000441 const LiveRange *LR = CurrLI->getLiveRangeContaining(RestoreIndex);
442 After.push_back(std::make_pair(RestoreIndex, LR->end));
Evan Cheng06587492008-10-24 02:05:00 +0000443 if (CurrLI->isKill(ValNo, LR->end))
444 AfterKills.push_back(LR->end);
445
Evan Chengd0e32c52008-10-29 05:06:14 +0000446 assert(LR->contains(SpillIndex));
447 if (SpillIndex > LR->start) {
448 Before.push_back(std::make_pair(LR->start, SpillIndex));
449 BeforeKills.push_back(SpillIndex);
Evan Cheng06587492008-10-24 02:05:00 +0000450 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000451 Processed.insert(LR);
452
Evan Chengd0e32c52008-10-29 05:06:14 +0000453 // Start from the restore mbb, figure out what part of the live interval
454 // are defined by the restore.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000455 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000456 MachineBasicBlock *MBB = BarrierMBB;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000457 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
458 SE = MBB->succ_end(); SI != SE; ++SI)
459 WorkList.push_back(*SI);
460
Owen Anderson75c99c52008-12-07 05:33:18 +0000461 SmallPtrSet<MachineBasicBlock*, 4> ProcessedBlocks;
462 ProcessedBlocks.insert(MBB);
463
Evan Chengf5cd4f02008-10-23 20:43:13 +0000464 while (!WorkList.empty()) {
465 MBB = WorkList.back();
466 WorkList.pop_back();
467 unsigned Idx = LIs->getMBBStartIdx(MBB);
468 LR = CurrLI->getLiveRangeContaining(Idx);
469 if (LR && LR->valno == ValNo && !Processed.count(LR)) {
470 After.push_back(std::make_pair(LR->start, LR->end));
471 if (CurrLI->isKill(ValNo, LR->end))
472 AfterKills.push_back(LR->end);
473 Idx = LIs->getMBBEndIdx(MBB);
474 if (LR->end > Idx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000475 // Live range extend beyond at least one mbb. Let's see what other
476 // mbbs it reaches.
477 LIs->findReachableMBBs(LR->start, LR->end, WorkList);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000478 }
479 Processed.insert(LR);
480 }
Owen Anderson75c99c52008-12-07 05:33:18 +0000481
482 ProcessedBlocks.insert(MBB);
483 if (LR)
484 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
485 SE = MBB->succ_end(); SI != SE; ++SI)
486 if (!ProcessedBlocks.count(*SI))
487 WorkList.push_back(*SI);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000488 }
489
490 for (LiveInterval::iterator I = CurrLI->begin(), E = CurrLI->end();
491 I != E; ++I) {
492 LiveRange *LR = I;
493 if (LR->valno == ValNo && !Processed.count(LR)) {
494 Before.push_back(std::make_pair(LR->start, LR->end));
495 if (CurrLI->isKill(ValNo, LR->end))
496 BeforeKills.push_back(LR->end);
497 }
498 }
499
500 // Now create new val#s to represent the live ranges defined by the old def
501 // those defined by the restore.
502 unsigned AfterDef = ValNo->def;
503 MachineInstr *AfterCopy = ValNo->copy;
504 bool HasPHIKill = ValNo->hasPHIKill;
505 CurrLI->removeValNo(ValNo);
Evan Cheng06587492008-10-24 02:05:00 +0000506 VNInfo *BValNo = (Before.empty())
507 ? NULL
508 : CurrLI->getNextValue(AfterDef, AfterCopy, LIs->getVNInfoAllocator());
509 if (BValNo)
510 CurrLI->addKills(BValNo, BeforeKills);
511
512 VNInfo *AValNo = (After.empty())
513 ? NULL
Evan Chengd0e32c52008-10-29 05:06:14 +0000514 : CurrLI->getNextValue(RestoreIndex, 0, LIs->getVNInfoAllocator());
Evan Cheng06587492008-10-24 02:05:00 +0000515 if (AValNo) {
516 AValNo->hasPHIKill = HasPHIKill;
517 CurrLI->addKills(AValNo, AfterKills);
518 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000519
520 for (unsigned i = 0, e = Before.size(); i != e; ++i) {
521 unsigned Start = Before[i].first;
522 unsigned End = Before[i].second;
523 CurrLI->addRange(LiveRange(Start, End, BValNo));
524 }
525 for (unsigned i = 0, e = After.size(); i != e; ++i) {
526 unsigned Start = After[i].first;
527 unsigned End = After[i].second;
528 CurrLI->addRange(LiveRange(Start, End, AValNo));
529 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000530
531 return AValNo;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000532}
533
534/// ShrinkWrapToLastUse - There are uses of the current live interval in the
535/// given block, shrink wrap the live interval to the last use (i.e. remove
536/// from last use to the end of the mbb). In case mbb is the where the barrier
537/// is, remove from the last use to the barrier.
538bool
Evan Chengd0e32c52008-10-29 05:06:14 +0000539PreAllocSplitting::ShrinkWrapToLastUse(MachineBasicBlock *MBB, VNInfo *ValNo,
Evan Cheng06587492008-10-24 02:05:00 +0000540 SmallVector<MachineOperand*, 4> &Uses,
541 SmallPtrSet<MachineInstr*, 4> &UseMIs) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000542 MachineOperand *LastMO = 0;
543 MachineInstr *LastMI = 0;
544 if (MBB != BarrierMBB && Uses.size() == 1) {
545 // Single use, no need to traverse the block. We can't assume this for the
546 // barrier bb though since the use is probably below the barrier.
547 LastMO = Uses[0];
548 LastMI = LastMO->getParent();
549 } else {
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000550 MachineBasicBlock::iterator MEE = MBB->begin();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000551 MachineBasicBlock::iterator MII;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000552 if (MBB == BarrierMBB)
Evan Chengf5cd4f02008-10-23 20:43:13 +0000553 MII = Barrier;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000554 else
Evan Chengf5cd4f02008-10-23 20:43:13 +0000555 MII = MBB->end();
Evan Chengd0e32c52008-10-29 05:06:14 +0000556 while (MII != MEE) {
557 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000558 MachineInstr *UseMI = &*MII;
559 if (!UseMIs.count(UseMI))
560 continue;
561 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
562 MachineOperand &MO = UseMI->getOperand(i);
563 if (MO.isReg() && MO.getReg() == CurrLI->reg) {
564 LastMO = &MO;
565 break;
566 }
567 }
568 LastMI = UseMI;
569 break;
570 }
571 }
572
573 // Cut off live range from last use (or beginning of the mbb if there
574 // are no uses in it) to the end of the mbb.
575 unsigned RangeStart, RangeEnd = LIs->getMBBEndIdx(MBB)+1;
576 if (LastMI) {
577 RangeStart = LIs->getUseIndex(LIs->getInstructionIndex(LastMI))+1;
578 assert(!LastMO->isKill() && "Last use already terminates the interval?");
579 LastMO->setIsKill();
580 } else {
581 assert(MBB == BarrierMBB);
582 RangeStart = LIs->getMBBStartIdx(MBB);
583 }
584 if (MBB == BarrierMBB)
Evan Cheng06587492008-10-24 02:05:00 +0000585 RangeEnd = LIs->getUseIndex(BarrierIdx)+1;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000586 CurrLI->removeRange(RangeStart, RangeEnd);
Evan Chengd0e32c52008-10-29 05:06:14 +0000587 if (LastMI)
588 CurrLI->addKill(ValNo, RangeStart);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000589
590 // Return true if the last use becomes a new kill.
591 return LastMI;
592}
593
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000594/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
595/// construction algorithm to compute the ranges and valnos for an interval.
596VNInfo* PreAllocSplitting::PerformPHIConstruction(
597 MachineBasicBlock::iterator use,
Owen Anderson7d211e22008-12-31 02:00:25 +0000598 MachineBasicBlock* MBB,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000599 LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000600 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000601 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
602 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
603 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000604 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
605 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
606 bool toplevel, bool intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000607 // Return memoized result if it's available.
Owen Anderson200ee7f2009-01-06 07:53:32 +0000608 if (toplevel && Visited.count(use) && NewVNs.count(use))
609 return NewVNs[use];
610 else if (!toplevel && intrablock && NewVNs.count(use))
Owen Anderson7d211e22008-12-31 02:00:25 +0000611 return NewVNs[use];
612 else if (!intrablock && LiveOut.count(MBB))
613 return LiveOut[MBB];
614
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000615 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
616
617 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000618 bool ContainsDefs = Defs.count(MBB);
619 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000620
621 VNInfo* ret = 0;
622
623 // Enumerate the cases of use/def contaning blocks.
624 if (!ContainsDefs && !ContainsUses) {
625 Fallback:
626 // NOTE: Because this is the fallback case from other cases, we do NOT
Owen Anderson7d211e22008-12-31 02:00:25 +0000627 // assume that we are not intrablock here.
628 if (Phis.count(MBB)) return Phis[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000629
Owen Anderson7d211e22008-12-31 02:00:25 +0000630 unsigned StartIndex = LIs->getMBBStartIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000631
Owen Anderson7d211e22008-12-31 02:00:25 +0000632 if (MBB->pred_size() == 1) {
633 Phis[MBB] = ret = PerformPHIConstruction((*MBB->pred_begin())->end(),
Owen Anderson200ee7f2009-01-06 07:53:32 +0000634 *(MBB->pred_begin()), LI, Visited,
635 Defs, Uses, NewVNs, LiveOut, Phis,
Owen Anderson7d211e22008-12-31 02:00:25 +0000636 false, false);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000637 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000638 if (intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000639 EndIndex = LIs->getInstructionIndex(use);
640 EndIndex = LiveIntervals::getUseIndex(EndIndex);
641 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000642 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000643
Owen Anderson7d211e22008-12-31 02:00:25 +0000644 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
Owen Andersone1762c92009-01-12 03:10:40 +0000645 if (intrablock)
646 LI->addKill(ret, EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000647 } else {
Owen Anderson7d211e22008-12-31 02:00:25 +0000648 Phis[MBB] = ret = LI->getNextValue(~0U, /*FIXME*/ 0,
649 LIs->getVNInfoAllocator());
650 if (!intrablock) LiveOut[MBB] = ret;
651
652 // If there are no uses or defs between our starting point and the
653 // beginning of the block, then recursive perform phi construction
654 // on our predecessors.
655 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
656 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
657 PE = MBB->pred_end(); PI != PE; ++PI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000658 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
659 Visited, Defs, Uses, NewVNs,
660 LiveOut, Phis, false, false);
Owen Anderson7d211e22008-12-31 02:00:25 +0000661 if (Incoming != 0)
662 IncomingVNs[*PI] = Incoming;
663 }
664
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000665 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
666 // VNInfo to represent the joined value.
667 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
668 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
669 I->second->hasPHIKill = true;
670 unsigned KillIndex = LIs->getMBBEndIdx(I->first);
671 LI->addKill(I->second, KillIndex);
672 }
673
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000674 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000675 if (intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000676 EndIndex = LIs->getInstructionIndex(use);
677 EndIndex = LiveIntervals::getUseIndex(EndIndex);
678 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000679 EndIndex = LIs->getMBBEndIdx(MBB);
680 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
Owen Andersone1762c92009-01-12 03:10:40 +0000681 if (intrablock)
682 LI->addKill(ret, EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000683 }
684 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000685 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000686
687 // Search for the def in this block. If we don't find it before the
688 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000689 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000690 // always be an end() iterator.
Owen Anderson7d211e22008-12-31 02:00:25 +0000691 assert(use == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000692
693 MachineBasicBlock::iterator walker = use;
694 --walker;
Owen Anderson7d211e22008-12-31 02:00:25 +0000695 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000696 if (BlockDefs.count(walker)) {
697 break;
698 } else
699 --walker;
700
701 // Once we've found it, extend its VNInfo to our instruction.
702 unsigned DefIndex = LIs->getInstructionIndex(walker);
703 DefIndex = LiveIntervals::getDefIndex(DefIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000704 unsigned EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000705
706 ret = NewVNs[walker];
Owen Anderson7d211e22008-12-31 02:00:25 +0000707 LI->addRange(LiveRange(DefIndex, EndIndex+1, ret));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000708 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000709 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000710
711 // Search for the use in this block that precedes the instruction we care
712 // about, going to the fallback case if we don't find it.
713
Owen Anderson7d211e22008-12-31 02:00:25 +0000714 if (use == MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000715 goto Fallback;
716
717 MachineBasicBlock::iterator walker = use;
718 --walker;
719 bool found = false;
Owen Anderson7d211e22008-12-31 02:00:25 +0000720 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000721 if (BlockUses.count(walker)) {
722 found = true;
723 break;
724 } else
725 --walker;
726
727 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000728 if (!found) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000729 if (BlockUses.count(walker))
730 found = true;
731 else
732 goto Fallback;
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000733 }
734
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000735 unsigned UseIndex = LIs->getInstructionIndex(walker);
736 UseIndex = LiveIntervals::getUseIndex(UseIndex);
737 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000738 if (intrablock) {
739 EndIndex = LIs->getInstructionIndex(use);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000740 EndIndex = LiveIntervals::getUseIndex(EndIndex);
741 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000742 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000743
744 // Now, recursively phi construct the VNInfo for the use we found,
745 // and then extend it to include the instruction we care about
Owen Anderson200ee7f2009-01-06 07:53:32 +0000746 ret = PerformPHIConstruction(walker, MBB, LI, Visited, Defs, Uses,
Owen Anderson7d211e22008-12-31 02:00:25 +0000747 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000748
749 // FIXME: Need to set kills properly for inter-block stuff.
Owen Andersond4f6fe52008-12-28 23:35:13 +0000750 if (LI->isKill(ret, UseIndex)) LI->removeKill(ret, UseIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000751 if (intrablock)
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000752 LI->addKill(ret, EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000753
Owen Anderson7d211e22008-12-31 02:00:25 +0000754 LI->addRange(LiveRange(UseIndex, EndIndex+1, ret));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000755 } else if (ContainsDefs && ContainsUses){
Owen Anderson7d211e22008-12-31 02:00:25 +0000756 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
757 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000758
759 // This case is basically a merging of the two preceding case, with the
760 // special note that checking for defs must take precedence over checking
761 // for uses, because of two-address instructions.
762
Owen Anderson7d211e22008-12-31 02:00:25 +0000763 if (use == MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000764 goto Fallback;
765
766 MachineBasicBlock::iterator walker = use;
767 --walker;
768 bool foundDef = false;
769 bool foundUse = false;
Owen Anderson7d211e22008-12-31 02:00:25 +0000770 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000771 if (BlockDefs.count(walker)) {
772 foundDef = true;
773 break;
774 } else if (BlockUses.count(walker)) {
775 foundUse = true;
776 break;
777 } else
778 --walker;
779
780 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000781 if (!foundDef && !foundUse) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000782 if (BlockDefs.count(walker))
783 foundDef = true;
784 else if (BlockUses.count(walker))
785 foundUse = true;
786 else
787 goto Fallback;
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000788 }
789
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000790 unsigned StartIndex = LIs->getInstructionIndex(walker);
791 StartIndex = foundDef ? LiveIntervals::getDefIndex(StartIndex) :
792 LiveIntervals::getUseIndex(StartIndex);
793 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000794 if (intrablock) {
795 EndIndex = LIs->getInstructionIndex(use);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000796 EndIndex = LiveIntervals::getUseIndex(EndIndex);
797 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000798 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000799
800 if (foundDef)
801 ret = NewVNs[walker];
802 else
Owen Anderson200ee7f2009-01-06 07:53:32 +0000803 ret = PerformPHIConstruction(walker, MBB, LI, Visited, Defs, Uses,
Owen Anderson7d211e22008-12-31 02:00:25 +0000804 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000805
Owen Andersond4f6fe52008-12-28 23:35:13 +0000806 if (foundUse && LI->isKill(ret, StartIndex))
807 LI->removeKill(ret, StartIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000808 if (intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000809 LI->addKill(ret, EndIndex);
810 }
811
Owen Anderson7d211e22008-12-31 02:00:25 +0000812 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000813 }
814
815 // Memoize results so we don't have to recompute them.
Owen Anderson7d211e22008-12-31 02:00:25 +0000816 if (!intrablock) LiveOut[MBB] = ret;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000817 else {
Owen Andersone1762c92009-01-12 03:10:40 +0000818 if (!NewVNs.count(use))
819 NewVNs[use] = ret;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000820 Visited.insert(use);
821 }
822
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000823 return ret;
824}
825
826/// ReconstructLiveInterval - Recompute a live interval from scratch.
827void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
828 BumpPtrAllocator& Alloc = LIs->getVNInfoAllocator();
829
830 // Clear the old ranges and valnos;
831 LI->clear();
832
833 // Cache the uses and defs of the register
834 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
835 RegMap Defs, Uses;
836
837 // Keep track of the new VNs we're creating.
838 DenseMap<MachineInstr*, VNInfo*> NewVNs;
839 SmallPtrSet<VNInfo*, 2> PhiVNs;
840
841 // Cache defs, and create a new VNInfo for each def.
842 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
843 DE = MRI->def_end(); DI != DE; ++DI) {
844 Defs[(*DI).getParent()].insert(&*DI);
845
846 unsigned DefIdx = LIs->getInstructionIndex(&*DI);
847 DefIdx = LiveIntervals::getDefIndex(DefIdx);
848
Owen Anderson200ee7f2009-01-06 07:53:32 +0000849 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, Alloc);
850
851 // If the def is a move, set the copy field.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000852 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
853 if (TII->isMoveInstr(*DI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
854 if (DstReg == LI->reg)
Owen Anderson200ee7f2009-01-06 07:53:32 +0000855 NewVN->copy = &*DI;
856
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000857 NewVNs[&*DI] = NewVN;
858 }
859
860 // Cache uses as a separate pass from actually processing them.
861 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
862 UE = MRI->use_end(); UI != UE; ++UI)
863 Uses[(*UI).getParent()].insert(&*UI);
864
865 // Now, actually process every use and use a phi construction algorithm
866 // to walk from it to its reaching definitions, building VNInfos along
867 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000868 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
869 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000870 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000871 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
872 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000873 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000874 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000875 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000876
877 // Add ranges for dead defs
878 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
879 DE = MRI->def_end(); DI != DE; ++DI) {
880 unsigned DefIdx = LIs->getInstructionIndex(&*DI);
881 DefIdx = LiveIntervals::getDefIndex(DefIdx);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000882
883 if (LI->liveAt(DefIdx)) continue;
884
885 VNInfo* DeadVN = NewVNs[&*DI];
Owen Anderson7d211e22008-12-31 02:00:25 +0000886 LI->addRange(LiveRange(DefIdx, DefIdx+1, DeadVN));
Owen Andersond4f6fe52008-12-28 23:35:13 +0000887 LI->addKill(DeadVN, DefIdx);
888 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000889}
890
Evan Chengf5cd4f02008-10-23 20:43:13 +0000891/// ShrinkWrapLiveInterval - Recursively traverse the predecessor
892/// chain to find the new 'kills' and shrink wrap the live interval to the
893/// new kill indices.
894void
Evan Chengaaf510c2008-10-26 07:49:03 +0000895PreAllocSplitting::ShrinkWrapLiveInterval(VNInfo *ValNo, MachineBasicBlock *MBB,
896 MachineBasicBlock *SuccMBB, MachineBasicBlock *DefMBB,
Evan Cheng06587492008-10-24 02:05:00 +0000897 SmallPtrSet<MachineBasicBlock*, 8> &Visited,
898 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > &Uses,
899 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > &UseMIs,
900 SmallVector<MachineBasicBlock*, 4> &UseMBBs) {
Evan Chengaaf510c2008-10-26 07:49:03 +0000901 if (Visited.count(MBB))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000902 return;
903
Evan Chengaaf510c2008-10-26 07:49:03 +0000904 // If live interval is live in another successor path, then we can't process
905 // this block. But we may able to do so after all the successors have been
906 // processed.
Evan Chengf62ce372008-10-28 00:47:49 +0000907 if (MBB != BarrierMBB) {
908 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
909 SE = MBB->succ_end(); SI != SE; ++SI) {
910 MachineBasicBlock *SMBB = *SI;
911 if (SMBB == SuccMBB)
912 continue;
913 if (CurrLI->liveAt(LIs->getMBBStartIdx(SMBB)))
914 return;
915 }
Evan Chengaaf510c2008-10-26 07:49:03 +0000916 }
917
918 Visited.insert(MBB);
919
Evan Cheng06587492008-10-24 02:05:00 +0000920 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
921 UMII = Uses.find(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000922 if (UMII != Uses.end()) {
923 // At least one use in this mbb, lets look for the kill.
Evan Cheng06587492008-10-24 02:05:00 +0000924 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
925 UMII2 = UseMIs.find(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000926 if (ShrinkWrapToLastUse(MBB, ValNo, UMII->second, UMII2->second))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000927 // Found a kill, shrink wrapping of this path ends here.
928 return;
Evan Cheng06587492008-10-24 02:05:00 +0000929 } else if (MBB == DefMBB) {
Evan Cheng06587492008-10-24 02:05:00 +0000930 // There are no uses after the def.
931 MachineInstr *DefMI = LIs->getInstructionFromIndex(ValNo->def);
Evan Cheng06587492008-10-24 02:05:00 +0000932 if (UseMBBs.empty()) {
933 // The only use must be below barrier in the barrier block. It's safe to
934 // remove the def.
935 LIs->RemoveMachineInstrFromMaps(DefMI);
936 DefMI->eraseFromParent();
937 CurrLI->removeRange(ValNo->def, LIs->getMBBEndIdx(MBB)+1);
938 }
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000939 } else if (MBB == BarrierMBB) {
940 // Remove entire live range from start of mbb to barrier.
941 CurrLI->removeRange(LIs->getMBBStartIdx(MBB),
942 LIs->getUseIndex(BarrierIdx)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000943 } else {
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000944 // Remove entire live range of the mbb out of the live interval.
Evan Cheng06587492008-10-24 02:05:00 +0000945 CurrLI->removeRange(LIs->getMBBStartIdx(MBB), LIs->getMBBEndIdx(MBB)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000946 }
947
948 if (MBB == DefMBB)
949 // Reached the def mbb, stop traversing this path further.
950 return;
951
952 // Traverse the pathes up the predecessor chains further.
953 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
954 PE = MBB->pred_end(); PI != PE; ++PI) {
955 MachineBasicBlock *Pred = *PI;
956 if (Pred == MBB)
957 continue;
958 if (Pred == DefMBB && ValNo->hasPHIKill)
959 // Pred is the def bb and the def reaches other val#s, we must
960 // allow the value to be live out of the bb.
961 continue;
Owen Anderson80fe8732008-11-11 22:11:27 +0000962 if (!CurrLI->liveAt(LIs->getMBBEndIdx(Pred)-1))
963 return;
Evan Chengaaf510c2008-10-26 07:49:03 +0000964 ShrinkWrapLiveInterval(ValNo, Pred, MBB, DefMBB, Visited,
965 Uses, UseMIs, UseMBBs);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000966 }
967
968 return;
969}
970
Owen Anderson75fa96b2008-11-19 04:28:29 +0000971
Owen Anderson6002e992008-12-04 21:20:30 +0000972void PreAllocSplitting::RepairLiveInterval(LiveInterval* CurrLI,
973 VNInfo* ValNo,
974 MachineInstr* DefMI,
975 unsigned RestoreIdx) {
Owen Anderson75fa96b2008-11-19 04:28:29 +0000976 // Shrink wrap the live interval by walking up the CFG and find the
977 // new kills.
978 // Now let's find all the uses of the val#.
979 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > Uses;
980 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > UseMIs;
981 SmallPtrSet<MachineBasicBlock*, 4> Seen;
982 SmallVector<MachineBasicBlock*, 4> UseMBBs;
983 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(CurrLI->reg),
984 UE = MRI->use_end(); UI != UE; ++UI) {
985 MachineOperand &UseMO = UI.getOperand();
986 MachineInstr *UseMI = UseMO.getParent();
987 unsigned UseIdx = LIs->getInstructionIndex(UseMI);
988 LiveInterval::iterator ULR = CurrLI->FindLiveRangeContaining(UseIdx);
989 if (ULR->valno != ValNo)
990 continue;
991 MachineBasicBlock *UseMBB = UseMI->getParent();
992 // Remember which other mbb's use this val#.
993 if (Seen.insert(UseMBB) && UseMBB != BarrierMBB)
994 UseMBBs.push_back(UseMBB);
995 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
996 UMII = Uses.find(UseMBB);
997 if (UMII != Uses.end()) {
998 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
999 UMII2 = UseMIs.find(UseMBB);
1000 UMII->second.push_back(&UseMO);
1001 UMII2->second.insert(UseMI);
1002 } else {
1003 SmallVector<MachineOperand*, 4> Ops;
1004 Ops.push_back(&UseMO);
1005 Uses.insert(std::make_pair(UseMBB, Ops));
1006 SmallPtrSet<MachineInstr*, 4> MIs;
1007 MIs.insert(UseMI);
1008 UseMIs.insert(std::make_pair(UseMBB, MIs));
1009 }
1010 }
1011
1012 // Walk up the predecessor chains.
1013 SmallPtrSet<MachineBasicBlock*, 8> Visited;
1014 ShrinkWrapLiveInterval(ValNo, BarrierMBB, NULL, DefMI->getParent(), Visited,
1015 Uses, UseMIs, UseMBBs);
1016
Owen Anderson75fa96b2008-11-19 04:28:29 +00001017 // Remove live range from barrier to the restore. FIXME: Find a better
1018 // point to re-start the live interval.
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001019 VNInfo* AfterValNo = UpdateRegisterInterval(ValNo,
1020 LIs->getUseIndex(BarrierIdx)+1,
1021 LIs->getDefIndex(RestoreIdx));
1022
1023 // Attempt to renumber the new valno into a new vreg.
1024 RenumberValno(AfterValNo);
Owen Anderson6002e992008-12-04 21:20:30 +00001025}
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001026
1027/// RenumberValno - Split the given valno out into a new vreg, allowing it to
1028/// be allocated to a different register. This function creates a new vreg,
1029/// copies the valno and its live ranges over to the new vreg's interval,
1030/// removes them from the old interval, and rewrites all uses and defs of
1031/// the original reg to the new vreg within those ranges.
1032void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001033 SmallVector<VNInfo*, 4> Stack;
1034 SmallVector<VNInfo*, 4> VNsToCopy;
1035 Stack.push_back(VN);
1036
1037 // Walk through and copy the valno we care about, and any other valnos
1038 // that are two-address redefinitions of the one we care about. These
1039 // will need to be rewritten as well. We also check for safety of the
1040 // renumbering here, by making sure that none of the valno involved has
1041 // phi kills.
1042 while (!Stack.empty()) {
1043 VNInfo* OldVN = Stack.back();
1044 Stack.pop_back();
1045
1046 // Bail out if we ever encounter a valno that has a PHI kill. We can't
1047 // renumber these.
1048 if (OldVN->hasPHIKill) return;
1049
1050 VNsToCopy.push_back(OldVN);
1051
1052 // Locate two-address redefinitions
1053 for (SmallVector<unsigned, 4>::iterator KI = OldVN->kills.begin(),
1054 KE = OldVN->kills.end(); KI != KE; ++KI) {
1055 MachineInstr* MI = LIs->getInstructionFromIndex(*KI);
1056 //if (!MI) continue;
1057 unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg);
1058 if (DefIdx == ~0U) continue;
1059 if (MI->isRegReDefinedByTwoAddr(DefIdx)) {
1060 VNInfo* NextVN =
1061 CurrLI->findDefinedVNInfo(LiveIntervals::getDefIndex(*KI));
1062 Stack.push_back(NextVN);
1063 }
1064 }
1065 }
1066
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001067 // Create the new vreg
1068 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
1069
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001070 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001071 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001072
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001073 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
1074 VNsToCopy.end(); OI != OE; ++OI) {
1075 VNInfo* OldVN = *OI;
1076
1077 // Copy the valno over
1078 VNInfo* NewVN = NewLI.getNextValue(OldVN->def, OldVN->copy,
1079 LIs->getVNInfoAllocator());
1080 NewLI.copyValNumInfo(NewVN, OldVN);
1081 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
1082
1083 // Remove the valno from the old interval
1084 CurrLI->removeValNo(OldVN);
1085 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001086
1087 // Rewrite defs and uses. This is done in two stages to avoid invalidating
1088 // the reg_iterator.
1089 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
1090
1091 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
1092 E = MRI->reg_end(); I != E; ++I) {
1093 MachineOperand& MO = I.getOperand();
1094 unsigned InstrIdx = LIs->getInstructionIndex(&*I);
1095
1096 if ((MO.isUse() && NewLI.liveAt(LiveIntervals::getUseIndex(InstrIdx))) ||
1097 (MO.isDef() && NewLI.liveAt(LiveIntervals::getDefIndex(InstrIdx))))
1098 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
1099 }
1100
1101 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
1102 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
1103 MachineInstr* Inst = I->first;
1104 unsigned OpIdx = I->second;
1105 MachineOperand& MO = Inst->getOperand(OpIdx);
1106 MO.setReg(NewVReg);
1107 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001108
1109 NumRenumbers++;
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001110}
1111
Owen Anderson6002e992008-12-04 21:20:30 +00001112bool PreAllocSplitting::Rematerialize(unsigned vreg, VNInfo* ValNo,
1113 MachineInstr* DefMI,
1114 MachineBasicBlock::iterator RestorePt,
1115 unsigned RestoreIdx,
1116 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
1117 MachineBasicBlock& MBB = *RestorePt->getParent();
1118
1119 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
1120 unsigned KillIdx = 0;
1121 if (ValNo->def == ~0U || DefMI->getParent() == BarrierMBB)
1122 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx);
1123 else
1124 KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx);
1125
1126 if (KillPt == DefMI->getParent()->end())
1127 return false;
1128
1129 TII->reMaterialize(MBB, RestorePt, vreg, DefMI);
1130 LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx);
1131
1132 if (KillPt->getParent() == BarrierMBB) {
Owen Anderson6cf7c392009-01-21 00:13:28 +00001133 VNInfo* After = UpdateRegisterInterval(ValNo, LIs->getUseIndex(KillIdx)+1,
Owen Anderson6002e992008-12-04 21:20:30 +00001134 LIs->getDefIndex(RestoreIdx));
Owen Andersone1762c92009-01-12 03:10:40 +00001135
Owen Anderson6cf7c392009-01-21 00:13:28 +00001136 RenumberValno(After);
1137
Owen Anderson6002e992008-12-04 21:20:30 +00001138 ++NumSplits;
1139 ++NumRemats;
1140 return true;
1141 }
1142
1143 RepairLiveInterval(CurrLI, ValNo, DefMI, RestoreIdx);
Owen Andersone1762c92009-01-12 03:10:40 +00001144
Owen Anderson75fa96b2008-11-19 04:28:29 +00001145 ++NumSplits;
1146 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001147 return true;
1148}
1149
1150MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
1151 const TargetRegisterClass* RC,
1152 MachineInstr* DefMI,
1153 MachineInstr* Barrier,
1154 MachineBasicBlock* MBB,
1155 int& SS,
1156 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
1157 MachineBasicBlock::iterator Pt = MBB->begin();
1158
1159 // Go top down if RefsInMBB is empty.
1160 if (RefsInMBB.empty())
1161 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +00001162
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001163 MachineBasicBlock::iterator FoldPt = Barrier;
1164 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
1165 !RefsInMBB.count(FoldPt))
1166 --FoldPt;
1167
1168 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
1169 if (OpIdx == -1)
1170 return 0;
1171
1172 SmallVector<unsigned, 1> Ops;
1173 Ops.push_back(OpIdx);
1174
1175 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
1176 return 0;
1177
1178 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
1179 if (I != IntervalSSMap.end()) {
1180 SS = I->second;
1181 } else {
1182 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
1183
1184 }
1185
1186 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
1187 FoldPt, Ops, SS);
1188
1189 if (FMI) {
1190 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
1191 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
1192 ++NumFolds;
1193
1194 IntervalSSMap[vreg] = SS;
1195 CurrSLI = &LSs->getOrCreateInterval(SS);
1196 if (CurrSLI->hasAtLeastOneValue())
1197 CurrSValNo = CurrSLI->getValNumInfo(0);
1198 else
1199 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
1200 }
1201
1202 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +00001203}
1204
Evan Chengf5cd4f02008-10-23 20:43:13 +00001205/// SplitRegLiveInterval - Split (spill and restore) the given live interval
1206/// so it would not cross the barrier that's being processed. Shrink wrap
1207/// (minimize) the live interval to the last uses.
1208bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
1209 CurrLI = LI;
1210
1211 // Find live range where current interval cross the barrier.
1212 LiveInterval::iterator LR =
1213 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
1214 VNInfo *ValNo = LR->valno;
1215
1216 if (ValNo->def == ~1U) {
1217 // Defined by a dead def? How can this be?
1218 assert(0 && "Val# is defined by a dead def?");
1219 abort();
1220 }
1221
Evan Cheng06587492008-10-24 02:05:00 +00001222 MachineInstr *DefMI = (ValNo->def != ~0U)
1223 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +00001224
Owen Andersond3be4622009-01-21 08:18:03 +00001225 // If this would create a new join point, do not split.
1226 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent()))
1227 return false;
1228
Evan Chengf5cd4f02008-10-23 20:43:13 +00001229 // Find all references in the barrier mbb.
1230 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
1231 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
1232 E = MRI->reg_end(); I != E; ++I) {
1233 MachineInstr *RefMI = &*I;
1234 if (RefMI->getParent() == BarrierMBB)
1235 RefsInMBB.insert(RefMI);
1236 }
1237
1238 // Find a point to restore the value after the barrier.
1239 unsigned RestoreIndex;
1240 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +00001241 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +00001242 if (RestorePt == BarrierMBB->end())
1243 return false;
1244
Owen Anderson75fa96b2008-11-19 04:28:29 +00001245 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
1246 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt,
1247 RestoreIndex, RefsInMBB))
1248 return true;
1249
Evan Chengf5cd4f02008-10-23 20:43:13 +00001250 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001251 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001252 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Evan Chengf5cd4f02008-10-23 20:43:13 +00001253 unsigned SpillIndex = 0;
Evan Cheng06587492008-10-24 02:05:00 +00001254 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001255 int SS = -1;
Evan Cheng78dfef72008-10-25 00:52:41 +00001256 if (ValNo->def == ~0U) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001257 // If it's defined by a phi, we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001258 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1259 BarrierMBB, SS, RefsInMBB))) {
1260 SpillIndex = LIs->getInstructionIndex(SpillMI);
1261 } else {
1262 MachineBasicBlock::iterator SpillPt =
1263 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
1264 if (SpillPt == BarrierMBB->begin())
1265 return false; // No gap to insert spill.
1266 // Add spill.
1267
1268 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1269 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
1270 SpillMI = prior(SpillPt);
1271 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
1272 }
Evan Cheng54898932008-10-29 08:39:34 +00001273 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
1274 RestoreIndex, SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001275 // If it's already split, just restore the value. There is no need to spill
1276 // the def again.
Evan Chengd0e32c52008-10-29 05:06:14 +00001277 if (!DefMI)
1278 return false; // Def is dead. Do nothing.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001279
1280 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
1281 BarrierMBB, SS, RefsInMBB))) {
1282 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001283 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001284 // Check if it's possible to insert a spill after the def MI.
1285 MachineBasicBlock::iterator SpillPt;
1286 if (DefMBB == BarrierMBB) {
1287 // Add spill after the def and the last use before the barrier.
1288 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
1289 RefsInMBB, SpillIndex);
1290 if (SpillPt == DefMBB->begin())
1291 return false; // No gap to insert spill.
1292 } else {
1293 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
1294 if (SpillPt == DefMBB->end())
1295 return false; // No gap to insert spill.
1296 }
1297 // Add spill. The store instruction kills the register if def is before
1298 // the barrier in the barrier block.
1299 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1300 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
1301 DefMBB == BarrierMBB, SS, RC);
1302 SpillMI = prior(SpillPt);
1303 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001304 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001305 }
1306
Evan Cheng54898932008-10-29 08:39:34 +00001307 // Remember def instruction index to spill index mapping.
1308 if (DefMI && SpillMI)
1309 Def2SpillMap[ValNo->def] = SpillIndex;
1310
Evan Chengf5cd4f02008-10-23 20:43:13 +00001311 // Add restore.
Evan Chengf5cd4f02008-10-23 20:43:13 +00001312 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
1313 MachineInstr *LoadMI = prior(RestorePt);
1314 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
1315
1316 // If live interval is spilled in the same block as the barrier, just
1317 // create a hole in the interval.
1318 if (!DefMBB ||
Evan Cheng78dfef72008-10-25 00:52:41 +00001319 (SpillMI && SpillMI->getParent() == BarrierMBB)) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001320 // Update spill stack slot live interval.
1321 UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
1322 LIs->getDefIndex(RestoreIndex));
Evan Chengf5cd4f02008-10-23 20:43:13 +00001323
Owen Anderson6cf7c392009-01-21 00:13:28 +00001324 VNInfo* After = UpdateRegisterInterval(ValNo,
1325 LIs->getUseIndex(SpillIndex)+1,
Evan Chengd0e32c52008-10-29 05:06:14 +00001326 LIs->getDefIndex(RestoreIndex));
Owen Anderson6cf7c392009-01-21 00:13:28 +00001327 RenumberValno(After);
1328
Evan Chengae7fa5b2008-10-28 01:48:24 +00001329 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001330 return true;
1331 }
1332
Evan Chengd0e32c52008-10-29 05:06:14 +00001333 // Update spill stack slot live interval.
Evan Cheng54898932008-10-29 08:39:34 +00001334 UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
1335 LIs->getDefIndex(RestoreIndex));
Evan Chengd0e32c52008-10-29 05:06:14 +00001336
Owen Anderson6002e992008-12-04 21:20:30 +00001337 RepairLiveInterval(CurrLI, ValNo, DefMI, RestoreIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +00001338
Evan Chengae7fa5b2008-10-28 01:48:24 +00001339 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001340 return true;
1341}
1342
1343/// SplitRegLiveIntervals - Split all register live intervals that cross the
1344/// barrier that's being processed.
1345bool
Owen Anderson956ec272009-01-23 00:23:32 +00001346PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1347 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001348 // First find all the virtual registers whose live intervals are intercepted
1349 // by the current barrier.
1350 SmallVector<LiveInterval*, 8> Intervals;
1351 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng23066282008-10-27 07:14:50 +00001352 if (TII->IgnoreRegisterClassBarriers(*RC))
1353 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001354 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
1355 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1356 unsigned Reg = VRs[i];
1357 if (!LIs->hasInterval(Reg))
1358 continue;
1359 LiveInterval *LI = &LIs->getInterval(Reg);
1360 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1361 // Virtual register live interval is intercepted by the barrier. We
1362 // should split and shrink wrap its interval if possible.
1363 Intervals.push_back(LI);
1364 }
1365 }
1366
1367 // Process the affected live intervals.
1368 bool Change = false;
1369 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001370 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1371 break;
Owen Andersone1762c92009-01-12 03:10:40 +00001372 else if (NumSplits == 4)
1373 Change |= Change;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001374 LiveInterval *LI = Intervals.back();
1375 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001376 bool result = SplitRegLiveInterval(LI);
1377 if (result) Split.insert(LI);
1378 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001379 }
1380
1381 return Change;
1382}
1383
Owen Anderson956ec272009-01-23 00:23:32 +00001384/// removeDeadSpills - After doing splitting, filter through all intervals we've
1385/// split, and see if any of the spills are unnecessary. If so, remove them.
1386bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1387 bool changed = false;
1388
1389 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1390 LE = split.end(); LI != LE; ++LI) {
1391 DenseMap<VNInfo*, unsigned > VNUseCount;
1392
1393 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1394 UE = MRI->use_end(); UI != UE; ++UI) {
1395 unsigned index = LIs->getInstructionIndex(&*UI);
1396 index = LiveIntervals::getUseIndex(index);
1397
1398 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
1399 VNUseCount[LR->valno]++;
1400 }
1401
1402 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1403 VE = (*LI)->vni_end(); VI != VE; ++VI) {
1404 VNInfo* CurrVN = *VI;
1405 if (CurrVN->hasPHIKill) continue;
1406 if (VNUseCount[CurrVN] > 0) continue;
1407
1408 unsigned DefIdx = CurrVN->def;
1409 if (DefIdx == ~0U || DefIdx == ~1U) continue;
1410
1411 MachineInstr* DefMI = LIs->getInstructionFromIndex(DefIdx);
1412 int FrameIndex;
1413 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1414
1415 LIs->RemoveMachineInstrFromMaps(DefMI);
1416 (*LI)->removeValNo(CurrVN);
1417 DefMI->eraseFromParent();
1418 NumDeadSpills++;
1419 changed = true;
1420 }
1421 }
1422
1423 return changed;
1424}
1425
Owen Andersonf1f75b12008-11-04 22:22:41 +00001426bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1427 MachineBasicBlock* DefMBB,
1428 MachineBasicBlock* BarrierMBB) {
1429 if (DefMBB == BarrierMBB)
1430 return false;
1431
1432 if (LR->valno->hasPHIKill)
1433 return false;
1434
1435 unsigned MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
1436 if (LR->end < MBBEnd)
1437 return false;
1438
1439 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1440 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1441 return true;
1442
1443 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1444 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1445 typedef std::pair<MachineBasicBlock*,
1446 MachineBasicBlock::succ_iterator> ItPair;
1447 SmallVector<ItPair, 4> Stack;
1448 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1449
1450 while (!Stack.empty()) {
1451 ItPair P = Stack.back();
1452 Stack.pop_back();
1453
1454 MachineBasicBlock* PredMBB = P.first;
1455 MachineBasicBlock::succ_iterator S = P.second;
1456
1457 if (S == PredMBB->succ_end())
1458 continue;
1459 else if (Visited.count(*S)) {
1460 Stack.push_back(std::make_pair(PredMBB, ++S));
1461 continue;
1462 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001463 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001464
1465 MachineBasicBlock* MBB = *S;
1466 Visited.insert(MBB);
1467
1468 if (MBB == BarrierMBB)
1469 return true;
1470
1471 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1472 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1473 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1474 while (MDTN) {
1475 if (MDTN == DefMDTN)
1476 return true;
1477 else if (MDTN == BarrierMDTN)
1478 break;
1479 MDTN = MDTN->getIDom();
1480 }
1481
1482 MBBEnd = LIs->getMBBEndIdx(MBB);
1483 if (LR->end > MBBEnd)
1484 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1485 }
1486
1487 return false;
1488}
1489
1490
Evan Cheng09e8ca82008-10-20 21:44:59 +00001491bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001492 CurrMF = &MF;
1493 TM = &MF.getTarget();
1494 TII = TM->getInstrInfo();
1495 MFI = MF.getFrameInfo();
1496 MRI = &MF.getRegInfo();
1497 LIs = &getAnalysis<LiveIntervals>();
1498 LSs = &getAnalysis<LiveStacks>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001499
1500 bool MadeChange = false;
1501
1502 // Make sure blocks are numbered in order.
1503 MF.RenumberBlocks();
1504
Evan Cheng54898932008-10-29 08:39:34 +00001505 MachineBasicBlock *Entry = MF.begin();
1506 SmallPtrSet<MachineBasicBlock*,16> Visited;
1507
Owen Anderson956ec272009-01-23 00:23:32 +00001508 SmallPtrSet<LiveInterval*, 8> Split;
1509
Evan Cheng54898932008-10-29 08:39:34 +00001510 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1511 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1512 DFI != E; ++DFI) {
1513 BarrierMBB = *DFI;
1514 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1515 E = BarrierMBB->end(); I != E; ++I) {
1516 Barrier = &*I;
1517 const TargetRegisterClass **BarrierRCs =
1518 Barrier->getDesc().getRegClassBarriers();
1519 if (!BarrierRCs)
1520 continue;
1521 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001522 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001523 }
1524 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001525
Owen Anderson956ec272009-01-23 00:23:32 +00001526 MadeChange |= removeDeadSpills(Split);
1527
Evan Chengf5cd4f02008-10-23 20:43:13 +00001528 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001529}