blob: 16a9e6d9b04173be1f79a515df016929cc8aac42 [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 Anderson32ca8652009-01-24 10:07:43 +0000178 unsigned getNumberOfSpills(SmallPtrSet<MachineInstr*, 4>& MIs,
179 unsigned Reg, int FrameIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000180 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator use,
Owen Anderson7d211e22008-12-31 02:00:25 +0000181 MachineBasicBlock* MBB,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000182 LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000183 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000184 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
185 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
186 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000187 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
188 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
189 bool toplevel, bool intrablock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000190};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000191} // end anonymous namespace
192
193char PreAllocSplitting::ID = 0;
194
195static RegisterPass<PreAllocSplitting>
196X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
197
198const PassInfo *const llvm::PreAllocSplittingID = &X;
199
Evan Chengf5cd4f02008-10-23 20:43:13 +0000200
201/// findNextEmptySlot - Find a gap after the given machine instruction in the
202/// instruction index map. If there isn't one, return end().
203MachineBasicBlock::iterator
204PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
205 unsigned &SpotIndex) {
206 MachineBasicBlock::iterator MII = MI;
207 if (++MII != MBB->end()) {
208 unsigned Index = LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
209 if (Index) {
210 SpotIndex = Index;
211 return MII;
212 }
213 }
214 return MBB->end();
215}
216
217/// findSpillPoint - Find a gap as far away from the given MI that's suitable
218/// for spilling the current live interval. The index must be before any
219/// defs and uses of the live interval register in the mbb. Return begin() if
220/// none is found.
221MachineBasicBlock::iterator
222PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000223 MachineInstr *DefMI,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000224 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
225 unsigned &SpillIndex) {
226 MachineBasicBlock::iterator Pt = MBB->begin();
227
228 // Go top down if RefsInMBB is empty.
Evan Cheng1f08cc22008-10-28 05:28:21 +0000229 if (RefsInMBB.empty() && !DefMI) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000230 MachineBasicBlock::iterator MII = MBB->begin();
231 MachineBasicBlock::iterator EndPt = MI;
232 do {
233 ++MII;
234 unsigned Index = LIs->getInstructionIndex(MII);
235 unsigned Gap = LIs->findGapBeforeInstr(Index);
236 if (Gap) {
237 Pt = MII;
238 SpillIndex = Gap;
239 break;
240 }
241 } while (MII != EndPt);
242 } else {
243 MachineBasicBlock::iterator MII = MI;
Evan Cheng1f08cc22008-10-28 05:28:21 +0000244 MachineBasicBlock::iterator EndPt = DefMI
245 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
246 while (MII != EndPt && !RefsInMBB.count(MII)) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000247 unsigned Index = LIs->getInstructionIndex(MII);
248 if (LIs->hasGapBeforeInstr(Index)) {
249 Pt = MII;
250 SpillIndex = LIs->findGapBeforeInstr(Index, true);
251 }
252 --MII;
253 }
254 }
255
256 return Pt;
257}
258
259/// findRestorePoint - Find a gap in the instruction index map that's suitable
260/// for restoring the current live interval value. The index must be before any
261/// uses of the live interval register in the mbb. Return end() if none is
262/// found.
263MachineBasicBlock::iterator
264PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Chengf62ce372008-10-28 00:47:49 +0000265 unsigned LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000266 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
267 unsigned &RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000268 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
269 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000270 MachineBasicBlock::iterator Pt = MBB->end();
Evan Chengf62ce372008-10-28 00:47:49 +0000271 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000272
Evan Chengf62ce372008-10-28 00:47:49 +0000273 // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond
274 // the last index in the live range.
275 if (RefsInMBB.empty() && LastIdx >= EndIdx) {
Owen Anderson711fd3d2008-11-13 21:53:14 +0000276 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000277 MachineBasicBlock::iterator EndPt = MI;
Evan Cheng54898932008-10-29 08:39:34 +0000278 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000279 do {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000280 unsigned Index = LIs->getInstructionIndex(MII);
Evan Cheng56ab0de2008-10-24 18:46:44 +0000281 unsigned Gap = LIs->findGapBeforeInstr(Index);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000282 if (Gap) {
283 Pt = MII;
284 RestoreIndex = Gap;
285 break;
286 }
Evan Cheng54898932008-10-29 08:39:34 +0000287 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000288 } while (MII != EndPt);
289 } else {
290 MachineBasicBlock::iterator MII = MI;
291 MII = ++MII;
Evan Chengf62ce372008-10-28 00:47:49 +0000292 // FIXME: Limit the number of instructions to examine to reduce
293 // compile time?
Evan Chengf5cd4f02008-10-23 20:43:13 +0000294 while (MII != MBB->end()) {
295 unsigned Index = LIs->getInstructionIndex(MII);
Evan Chengf62ce372008-10-28 00:47:49 +0000296 if (Index > LastIdx)
297 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000298 unsigned Gap = LIs->findGapBeforeInstr(Index);
299 if (Gap) {
300 Pt = MII;
301 RestoreIndex = Gap;
302 }
303 if (RefsInMBB.count(MII))
304 break;
305 ++MII;
306 }
307 }
308
309 return Pt;
310}
311
Evan Chengd0e32c52008-10-29 05:06:14 +0000312/// CreateSpillStackSlot - Create a stack slot for the live interval being
313/// split. If the live interval was previously split, just reuse the same
314/// slot.
315int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
316 const TargetRegisterClass *RC) {
317 int SS;
318 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
319 if (I != IntervalSSMap.end()) {
320 SS = I->second;
321 } else {
322 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
323 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000324 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000325
326 // Create live interval for stack slot.
327 CurrSLI = &LSs->getOrCreateInterval(SS);
Evan Cheng54898932008-10-29 08:39:34 +0000328 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000329 CurrSValNo = CurrSLI->getValNumInfo(0);
330 else
331 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
332 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000333}
334
Evan Chengd0e32c52008-10-29 05:06:14 +0000335/// IsAvailableInStack - Return true if register is available in a split stack
336/// slot at the specified index.
337bool
Evan Cheng54898932008-10-29 08:39:34 +0000338PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
339 unsigned Reg, unsigned DefIndex,
340 unsigned RestoreIndex, unsigned &SpillIndex,
341 int& SS) const {
342 if (!DefMBB)
343 return false;
344
Evan Chengd0e32c52008-10-29 05:06:14 +0000345 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
346 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000347 return false;
Evan Cheng54898932008-10-29 08:39:34 +0000348 DenseMap<unsigned, unsigned>::iterator II = Def2SpillMap.find(DefIndex);
349 if (II == Def2SpillMap.end())
350 return false;
351
352 // If last spill of def is in the same mbb as barrier mbb (where restore will
353 // be), make sure it's not below the intended restore index.
354 // FIXME: Undo the previous spill?
355 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
356 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
357 return false;
358
359 SS = I->second;
360 SpillIndex = II->second;
361 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000362}
363
Evan Chengd0e32c52008-10-29 05:06:14 +0000364/// UpdateSpillSlotInterval - Given the specified val# of the register live
365/// interval being split, and the spill and restore indicies, update the live
366/// interval of the spill stack slot.
367void
368PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, unsigned SpillIndex,
369 unsigned RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000370 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
371 "Expect restore in the barrier mbb");
372
373 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
374 if (MBB == BarrierMBB) {
375 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000376 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
377 CurrSLI->addRange(SLR);
378 return;
379 }
380
Evan Cheng54898932008-10-29 08:39:34 +0000381 SmallPtrSet<MachineBasicBlock*, 4> Processed;
382 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
383 LiveRange SLR(SpillIndex, EndIdx+1, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000384 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000385 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000386
387 // Start from the spill mbb, figure out the extend of the spill slot's
388 // live interval.
389 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000390 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
391 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000392 // If live range extend beyond end of mbb, add successors to work list.
393 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
394 SE = MBB->succ_end(); SI != SE; ++SI)
395 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000396
397 while (!WorkList.empty()) {
398 MachineBasicBlock *MBB = WorkList.back();
399 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000400 if (Processed.count(MBB))
401 continue;
Evan Chengd0e32c52008-10-29 05:06:14 +0000402 unsigned Idx = LIs->getMBBStartIdx(MBB);
403 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000404 if (LR && LR->valno == ValNo) {
405 EndIdx = LIs->getMBBEndIdx(MBB);
406 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000407 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000408 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000409 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000410 } else if (LR->end > EndIdx) {
411 // Live range extends beyond end of mbb, process successors.
412 LiveRange SLR(Idx, EndIdx+1, CurrSValNo);
413 CurrSLI->addRange(SLR);
414 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
415 SE = MBB->succ_end(); SI != SE; ++SI)
416 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000417 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000418 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000419 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000420 }
Evan Cheng54898932008-10-29 08:39:34 +0000421 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000422 }
423 }
424}
425
426/// UpdateRegisterInterval - Given the specified val# of the current live
427/// interval is being split, and the spill and restore indices, update the live
Evan Chengf5cd4f02008-10-23 20:43:13 +0000428/// interval accordingly.
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000429VNInfo*
Evan Chengd0e32c52008-10-29 05:06:14 +0000430PreAllocSplitting::UpdateRegisterInterval(VNInfo *ValNo, unsigned SpillIndex,
431 unsigned RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000432 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
433 "Expect restore in the barrier mbb");
434
Evan Chengf5cd4f02008-10-23 20:43:13 +0000435 SmallVector<std::pair<unsigned,unsigned>, 4> Before;
436 SmallVector<std::pair<unsigned,unsigned>, 4> After;
437 SmallVector<unsigned, 4> BeforeKills;
438 SmallVector<unsigned, 4> AfterKills;
439 SmallPtrSet<const LiveRange*, 4> Processed;
440
441 // First, let's figure out which parts of the live interval is now defined
442 // by the restore, which are defined by the original definition.
Evan Chengd0e32c52008-10-29 05:06:14 +0000443 const LiveRange *LR = CurrLI->getLiveRangeContaining(RestoreIndex);
444 After.push_back(std::make_pair(RestoreIndex, LR->end));
Evan Cheng06587492008-10-24 02:05:00 +0000445 if (CurrLI->isKill(ValNo, LR->end))
446 AfterKills.push_back(LR->end);
447
Evan Chengd0e32c52008-10-29 05:06:14 +0000448 assert(LR->contains(SpillIndex));
449 if (SpillIndex > LR->start) {
450 Before.push_back(std::make_pair(LR->start, SpillIndex));
451 BeforeKills.push_back(SpillIndex);
Evan Cheng06587492008-10-24 02:05:00 +0000452 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000453 Processed.insert(LR);
454
Evan Chengd0e32c52008-10-29 05:06:14 +0000455 // Start from the restore mbb, figure out what part of the live interval
456 // are defined by the restore.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000457 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000458 MachineBasicBlock *MBB = BarrierMBB;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000459 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
460 SE = MBB->succ_end(); SI != SE; ++SI)
461 WorkList.push_back(*SI);
462
Owen Anderson75c99c52008-12-07 05:33:18 +0000463 SmallPtrSet<MachineBasicBlock*, 4> ProcessedBlocks;
464 ProcessedBlocks.insert(MBB);
465
Evan Chengf5cd4f02008-10-23 20:43:13 +0000466 while (!WorkList.empty()) {
467 MBB = WorkList.back();
468 WorkList.pop_back();
469 unsigned Idx = LIs->getMBBStartIdx(MBB);
470 LR = CurrLI->getLiveRangeContaining(Idx);
471 if (LR && LR->valno == ValNo && !Processed.count(LR)) {
472 After.push_back(std::make_pair(LR->start, LR->end));
473 if (CurrLI->isKill(ValNo, LR->end))
474 AfterKills.push_back(LR->end);
475 Idx = LIs->getMBBEndIdx(MBB);
476 if (LR->end > Idx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000477 // Live range extend beyond at least one mbb. Let's see what other
478 // mbbs it reaches.
479 LIs->findReachableMBBs(LR->start, LR->end, WorkList);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000480 }
481 Processed.insert(LR);
482 }
Owen Anderson75c99c52008-12-07 05:33:18 +0000483
484 ProcessedBlocks.insert(MBB);
485 if (LR)
486 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
487 SE = MBB->succ_end(); SI != SE; ++SI)
488 if (!ProcessedBlocks.count(*SI))
489 WorkList.push_back(*SI);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000490 }
491
492 for (LiveInterval::iterator I = CurrLI->begin(), E = CurrLI->end();
493 I != E; ++I) {
494 LiveRange *LR = I;
495 if (LR->valno == ValNo && !Processed.count(LR)) {
496 Before.push_back(std::make_pair(LR->start, LR->end));
497 if (CurrLI->isKill(ValNo, LR->end))
498 BeforeKills.push_back(LR->end);
499 }
500 }
501
502 // Now create new val#s to represent the live ranges defined by the old def
503 // those defined by the restore.
504 unsigned AfterDef = ValNo->def;
505 MachineInstr *AfterCopy = ValNo->copy;
506 bool HasPHIKill = ValNo->hasPHIKill;
507 CurrLI->removeValNo(ValNo);
Evan Cheng06587492008-10-24 02:05:00 +0000508 VNInfo *BValNo = (Before.empty())
509 ? NULL
510 : CurrLI->getNextValue(AfterDef, AfterCopy, LIs->getVNInfoAllocator());
511 if (BValNo)
512 CurrLI->addKills(BValNo, BeforeKills);
513
514 VNInfo *AValNo = (After.empty())
515 ? NULL
Evan Chengd0e32c52008-10-29 05:06:14 +0000516 : CurrLI->getNextValue(RestoreIndex, 0, LIs->getVNInfoAllocator());
Evan Cheng06587492008-10-24 02:05:00 +0000517 if (AValNo) {
518 AValNo->hasPHIKill = HasPHIKill;
519 CurrLI->addKills(AValNo, AfterKills);
520 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000521
522 for (unsigned i = 0, e = Before.size(); i != e; ++i) {
523 unsigned Start = Before[i].first;
524 unsigned End = Before[i].second;
525 CurrLI->addRange(LiveRange(Start, End, BValNo));
526 }
527 for (unsigned i = 0, e = After.size(); i != e; ++i) {
528 unsigned Start = After[i].first;
529 unsigned End = After[i].second;
530 CurrLI->addRange(LiveRange(Start, End, AValNo));
531 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000532
533 return AValNo;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000534}
535
536/// ShrinkWrapToLastUse - There are uses of the current live interval in the
537/// given block, shrink wrap the live interval to the last use (i.e. remove
538/// from last use to the end of the mbb). In case mbb is the where the barrier
539/// is, remove from the last use to the barrier.
540bool
Evan Chengd0e32c52008-10-29 05:06:14 +0000541PreAllocSplitting::ShrinkWrapToLastUse(MachineBasicBlock *MBB, VNInfo *ValNo,
Evan Cheng06587492008-10-24 02:05:00 +0000542 SmallVector<MachineOperand*, 4> &Uses,
543 SmallPtrSet<MachineInstr*, 4> &UseMIs) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000544 MachineOperand *LastMO = 0;
545 MachineInstr *LastMI = 0;
546 if (MBB != BarrierMBB && Uses.size() == 1) {
547 // Single use, no need to traverse the block. We can't assume this for the
548 // barrier bb though since the use is probably below the barrier.
549 LastMO = Uses[0];
550 LastMI = LastMO->getParent();
551 } else {
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000552 MachineBasicBlock::iterator MEE = MBB->begin();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000553 MachineBasicBlock::iterator MII;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000554 if (MBB == BarrierMBB)
Evan Chengf5cd4f02008-10-23 20:43:13 +0000555 MII = Barrier;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000556 else
Evan Chengf5cd4f02008-10-23 20:43:13 +0000557 MII = MBB->end();
Evan Chengd0e32c52008-10-29 05:06:14 +0000558 while (MII != MEE) {
559 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000560 MachineInstr *UseMI = &*MII;
561 if (!UseMIs.count(UseMI))
562 continue;
563 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
564 MachineOperand &MO = UseMI->getOperand(i);
565 if (MO.isReg() && MO.getReg() == CurrLI->reg) {
566 LastMO = &MO;
567 break;
568 }
569 }
570 LastMI = UseMI;
571 break;
572 }
573 }
574
575 // Cut off live range from last use (or beginning of the mbb if there
576 // are no uses in it) to the end of the mbb.
577 unsigned RangeStart, RangeEnd = LIs->getMBBEndIdx(MBB)+1;
578 if (LastMI) {
579 RangeStart = LIs->getUseIndex(LIs->getInstructionIndex(LastMI))+1;
580 assert(!LastMO->isKill() && "Last use already terminates the interval?");
581 LastMO->setIsKill();
582 } else {
583 assert(MBB == BarrierMBB);
584 RangeStart = LIs->getMBBStartIdx(MBB);
585 }
586 if (MBB == BarrierMBB)
Evan Cheng06587492008-10-24 02:05:00 +0000587 RangeEnd = LIs->getUseIndex(BarrierIdx)+1;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000588 CurrLI->removeRange(RangeStart, RangeEnd);
Evan Chengd0e32c52008-10-29 05:06:14 +0000589 if (LastMI)
590 CurrLI->addKill(ValNo, RangeStart);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000591
592 // Return true if the last use becomes a new kill.
593 return LastMI;
594}
595
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000596/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
597/// construction algorithm to compute the ranges and valnos for an interval.
598VNInfo* PreAllocSplitting::PerformPHIConstruction(
599 MachineBasicBlock::iterator use,
Owen Anderson7d211e22008-12-31 02:00:25 +0000600 MachineBasicBlock* MBB,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000601 LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000602 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000603 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
604 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
605 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000606 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
607 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
608 bool toplevel, bool intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000609 // Return memoized result if it's available.
Owen Anderson200ee7f2009-01-06 07:53:32 +0000610 if (toplevel && Visited.count(use) && NewVNs.count(use))
611 return NewVNs[use];
612 else if (!toplevel && intrablock && NewVNs.count(use))
Owen Anderson7d211e22008-12-31 02:00:25 +0000613 return NewVNs[use];
614 else if (!intrablock && LiveOut.count(MBB))
615 return LiveOut[MBB];
616
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000617 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
618
619 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000620 bool ContainsDefs = Defs.count(MBB);
621 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000622
623 VNInfo* ret = 0;
624
625 // Enumerate the cases of use/def contaning blocks.
626 if (!ContainsDefs && !ContainsUses) {
627 Fallback:
628 // NOTE: Because this is the fallback case from other cases, we do NOT
Owen Anderson7d211e22008-12-31 02:00:25 +0000629 // assume that we are not intrablock here.
630 if (Phis.count(MBB)) return Phis[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000631
Owen Anderson7d211e22008-12-31 02:00:25 +0000632 unsigned StartIndex = LIs->getMBBStartIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000633
Owen Anderson7d211e22008-12-31 02:00:25 +0000634 if (MBB->pred_size() == 1) {
635 Phis[MBB] = ret = PerformPHIConstruction((*MBB->pred_begin())->end(),
Owen Anderson200ee7f2009-01-06 07:53:32 +0000636 *(MBB->pred_begin()), LI, Visited,
637 Defs, Uses, NewVNs, LiveOut, Phis,
Owen Anderson7d211e22008-12-31 02:00:25 +0000638 false, false);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000639 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000640 if (intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000641 EndIndex = LIs->getInstructionIndex(use);
642 EndIndex = LiveIntervals::getUseIndex(EndIndex);
643 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000644 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000645
Owen Anderson7d211e22008-12-31 02:00:25 +0000646 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
Owen Andersone1762c92009-01-12 03:10:40 +0000647 if (intrablock)
648 LI->addKill(ret, EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000649 } else {
Owen Anderson7d211e22008-12-31 02:00:25 +0000650 Phis[MBB] = ret = LI->getNextValue(~0U, /*FIXME*/ 0,
651 LIs->getVNInfoAllocator());
652 if (!intrablock) LiveOut[MBB] = ret;
653
654 // If there are no uses or defs between our starting point and the
655 // beginning of the block, then recursive perform phi construction
656 // on our predecessors.
657 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
658 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
659 PE = MBB->pred_end(); PI != PE; ++PI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000660 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
661 Visited, Defs, Uses, NewVNs,
662 LiveOut, Phis, false, false);
Owen Anderson7d211e22008-12-31 02:00:25 +0000663 if (Incoming != 0)
664 IncomingVNs[*PI] = Incoming;
665 }
666
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000667 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
668 // VNInfo to represent the joined value.
669 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
670 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
671 I->second->hasPHIKill = true;
672 unsigned KillIndex = LIs->getMBBEndIdx(I->first);
673 LI->addKill(I->second, KillIndex);
674 }
675
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000676 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000677 if (intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000678 EndIndex = LIs->getInstructionIndex(use);
679 EndIndex = LiveIntervals::getUseIndex(EndIndex);
680 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000681 EndIndex = LIs->getMBBEndIdx(MBB);
682 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
Owen Andersone1762c92009-01-12 03:10:40 +0000683 if (intrablock)
684 LI->addKill(ret, EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000685 }
686 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000687 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000688
689 // Search for the def in this block. If we don't find it before the
690 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000691 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000692 // always be an end() iterator.
Owen Anderson7d211e22008-12-31 02:00:25 +0000693 assert(use == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000694
695 MachineBasicBlock::iterator walker = use;
696 --walker;
Owen Anderson7d211e22008-12-31 02:00:25 +0000697 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000698 if (BlockDefs.count(walker)) {
699 break;
700 } else
701 --walker;
702
703 // Once we've found it, extend its VNInfo to our instruction.
704 unsigned DefIndex = LIs->getInstructionIndex(walker);
705 DefIndex = LiveIntervals::getDefIndex(DefIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000706 unsigned EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000707
708 ret = NewVNs[walker];
Owen Anderson7d211e22008-12-31 02:00:25 +0000709 LI->addRange(LiveRange(DefIndex, EndIndex+1, ret));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000710 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000711 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000712
713 // Search for the use in this block that precedes the instruction we care
714 // about, going to the fallback case if we don't find it.
715
Owen Anderson7d211e22008-12-31 02:00:25 +0000716 if (use == MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000717 goto Fallback;
718
719 MachineBasicBlock::iterator walker = use;
720 --walker;
721 bool found = false;
Owen Anderson7d211e22008-12-31 02:00:25 +0000722 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000723 if (BlockUses.count(walker)) {
724 found = true;
725 break;
726 } else
727 --walker;
728
729 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000730 if (!found) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000731 if (BlockUses.count(walker))
732 found = true;
733 else
734 goto Fallback;
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000735 }
736
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000737 unsigned UseIndex = LIs->getInstructionIndex(walker);
738 UseIndex = LiveIntervals::getUseIndex(UseIndex);
739 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000740 if (intrablock) {
741 EndIndex = LIs->getInstructionIndex(use);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000742 EndIndex = LiveIntervals::getUseIndex(EndIndex);
743 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000744 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000745
746 // Now, recursively phi construct the VNInfo for the use we found,
747 // and then extend it to include the instruction we care about
Owen Anderson200ee7f2009-01-06 07:53:32 +0000748 ret = PerformPHIConstruction(walker, MBB, LI, Visited, Defs, Uses,
Owen Anderson7d211e22008-12-31 02:00:25 +0000749 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000750
751 // FIXME: Need to set kills properly for inter-block stuff.
Owen Andersond4f6fe52008-12-28 23:35:13 +0000752 if (LI->isKill(ret, UseIndex)) LI->removeKill(ret, UseIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000753 if (intrablock)
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000754 LI->addKill(ret, EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000755
Owen Anderson7d211e22008-12-31 02:00:25 +0000756 LI->addRange(LiveRange(UseIndex, EndIndex+1, ret));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000757 } else if (ContainsDefs && ContainsUses){
Owen Anderson7d211e22008-12-31 02:00:25 +0000758 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
759 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000760
761 // This case is basically a merging of the two preceding case, with the
762 // special note that checking for defs must take precedence over checking
763 // for uses, because of two-address instructions.
764
Owen Anderson7d211e22008-12-31 02:00:25 +0000765 if (use == MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000766 goto Fallback;
767
768 MachineBasicBlock::iterator walker = use;
769 --walker;
770 bool foundDef = false;
771 bool foundUse = false;
Owen Anderson7d211e22008-12-31 02:00:25 +0000772 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000773 if (BlockDefs.count(walker)) {
774 foundDef = true;
775 break;
776 } else if (BlockUses.count(walker)) {
777 foundUse = true;
778 break;
779 } else
780 --walker;
781
782 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000783 if (!foundDef && !foundUse) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000784 if (BlockDefs.count(walker))
785 foundDef = true;
786 else if (BlockUses.count(walker))
787 foundUse = true;
788 else
789 goto Fallback;
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000790 }
791
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000792 unsigned StartIndex = LIs->getInstructionIndex(walker);
793 StartIndex = foundDef ? LiveIntervals::getDefIndex(StartIndex) :
794 LiveIntervals::getUseIndex(StartIndex);
795 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000796 if (intrablock) {
797 EndIndex = LIs->getInstructionIndex(use);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000798 EndIndex = LiveIntervals::getUseIndex(EndIndex);
799 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000800 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000801
802 if (foundDef)
803 ret = NewVNs[walker];
804 else
Owen Anderson200ee7f2009-01-06 07:53:32 +0000805 ret = PerformPHIConstruction(walker, MBB, LI, Visited, Defs, Uses,
Owen Anderson7d211e22008-12-31 02:00:25 +0000806 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000807
Owen Andersond4f6fe52008-12-28 23:35:13 +0000808 if (foundUse && LI->isKill(ret, StartIndex))
809 LI->removeKill(ret, StartIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000810 if (intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000811 LI->addKill(ret, EndIndex);
812 }
813
Owen Anderson7d211e22008-12-31 02:00:25 +0000814 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000815 }
816
817 // Memoize results so we don't have to recompute them.
Owen Anderson7d211e22008-12-31 02:00:25 +0000818 if (!intrablock) LiveOut[MBB] = ret;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000819 else {
Owen Andersone1762c92009-01-12 03:10:40 +0000820 if (!NewVNs.count(use))
821 NewVNs[use] = ret;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000822 Visited.insert(use);
823 }
824
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000825 return ret;
826}
827
828/// ReconstructLiveInterval - Recompute a live interval from scratch.
829void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
830 BumpPtrAllocator& Alloc = LIs->getVNInfoAllocator();
831
832 // Clear the old ranges and valnos;
833 LI->clear();
834
835 // Cache the uses and defs of the register
836 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
837 RegMap Defs, Uses;
838
839 // Keep track of the new VNs we're creating.
840 DenseMap<MachineInstr*, VNInfo*> NewVNs;
841 SmallPtrSet<VNInfo*, 2> PhiVNs;
842
843 // Cache defs, and create a new VNInfo for each def.
844 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
845 DE = MRI->def_end(); DI != DE; ++DI) {
846 Defs[(*DI).getParent()].insert(&*DI);
847
848 unsigned DefIdx = LIs->getInstructionIndex(&*DI);
849 DefIdx = LiveIntervals::getDefIndex(DefIdx);
850
Owen Anderson200ee7f2009-01-06 07:53:32 +0000851 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, Alloc);
852
853 // If the def is a move, set the copy field.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000854 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
855 if (TII->isMoveInstr(*DI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
856 if (DstReg == LI->reg)
Owen Anderson200ee7f2009-01-06 07:53:32 +0000857 NewVN->copy = &*DI;
858
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000859 NewVNs[&*DI] = NewVN;
860 }
861
862 // Cache uses as a separate pass from actually processing them.
863 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
864 UE = MRI->use_end(); UI != UE; ++UI)
865 Uses[(*UI).getParent()].insert(&*UI);
866
867 // Now, actually process every use and use a phi construction algorithm
868 // to walk from it to its reaching definitions, building VNInfos along
869 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000870 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
871 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000872 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000873 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
874 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000875 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000876 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000877 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000878
879 // Add ranges for dead defs
880 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
881 DE = MRI->def_end(); DI != DE; ++DI) {
882 unsigned DefIdx = LIs->getInstructionIndex(&*DI);
883 DefIdx = LiveIntervals::getDefIndex(DefIdx);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000884
885 if (LI->liveAt(DefIdx)) continue;
886
887 VNInfo* DeadVN = NewVNs[&*DI];
Owen Anderson7d211e22008-12-31 02:00:25 +0000888 LI->addRange(LiveRange(DefIdx, DefIdx+1, DeadVN));
Owen Andersond4f6fe52008-12-28 23:35:13 +0000889 LI->addKill(DeadVN, DefIdx);
890 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000891}
892
Evan Chengf5cd4f02008-10-23 20:43:13 +0000893/// ShrinkWrapLiveInterval - Recursively traverse the predecessor
894/// chain to find the new 'kills' and shrink wrap the live interval to the
895/// new kill indices.
896void
Evan Chengaaf510c2008-10-26 07:49:03 +0000897PreAllocSplitting::ShrinkWrapLiveInterval(VNInfo *ValNo, MachineBasicBlock *MBB,
898 MachineBasicBlock *SuccMBB, MachineBasicBlock *DefMBB,
Evan Cheng06587492008-10-24 02:05:00 +0000899 SmallPtrSet<MachineBasicBlock*, 8> &Visited,
900 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > &Uses,
901 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > &UseMIs,
902 SmallVector<MachineBasicBlock*, 4> &UseMBBs) {
Evan Chengaaf510c2008-10-26 07:49:03 +0000903 if (Visited.count(MBB))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000904 return;
905
Evan Chengaaf510c2008-10-26 07:49:03 +0000906 // If live interval is live in another successor path, then we can't process
907 // this block. But we may able to do so after all the successors have been
908 // processed.
Evan Chengf62ce372008-10-28 00:47:49 +0000909 if (MBB != BarrierMBB) {
910 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
911 SE = MBB->succ_end(); SI != SE; ++SI) {
912 MachineBasicBlock *SMBB = *SI;
913 if (SMBB == SuccMBB)
914 continue;
915 if (CurrLI->liveAt(LIs->getMBBStartIdx(SMBB)))
916 return;
917 }
Evan Chengaaf510c2008-10-26 07:49:03 +0000918 }
919
920 Visited.insert(MBB);
921
Evan Cheng06587492008-10-24 02:05:00 +0000922 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
923 UMII = Uses.find(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000924 if (UMII != Uses.end()) {
925 // At least one use in this mbb, lets look for the kill.
Evan Cheng06587492008-10-24 02:05:00 +0000926 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
927 UMII2 = UseMIs.find(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000928 if (ShrinkWrapToLastUse(MBB, ValNo, UMII->second, UMII2->second))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000929 // Found a kill, shrink wrapping of this path ends here.
930 return;
Evan Cheng06587492008-10-24 02:05:00 +0000931 } else if (MBB == DefMBB) {
Evan Cheng06587492008-10-24 02:05:00 +0000932 // There are no uses after the def.
933 MachineInstr *DefMI = LIs->getInstructionFromIndex(ValNo->def);
Evan Cheng06587492008-10-24 02:05:00 +0000934 if (UseMBBs.empty()) {
935 // The only use must be below barrier in the barrier block. It's safe to
936 // remove the def.
937 LIs->RemoveMachineInstrFromMaps(DefMI);
938 DefMI->eraseFromParent();
939 CurrLI->removeRange(ValNo->def, LIs->getMBBEndIdx(MBB)+1);
940 }
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000941 } else if (MBB == BarrierMBB) {
942 // Remove entire live range from start of mbb to barrier.
943 CurrLI->removeRange(LIs->getMBBStartIdx(MBB),
944 LIs->getUseIndex(BarrierIdx)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000945 } else {
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000946 // Remove entire live range of the mbb out of the live interval.
Evan Cheng06587492008-10-24 02:05:00 +0000947 CurrLI->removeRange(LIs->getMBBStartIdx(MBB), LIs->getMBBEndIdx(MBB)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000948 }
949
950 if (MBB == DefMBB)
951 // Reached the def mbb, stop traversing this path further.
952 return;
953
954 // Traverse the pathes up the predecessor chains further.
955 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
956 PE = MBB->pred_end(); PI != PE; ++PI) {
957 MachineBasicBlock *Pred = *PI;
958 if (Pred == MBB)
959 continue;
960 if (Pred == DefMBB && ValNo->hasPHIKill)
961 // Pred is the def bb and the def reaches other val#s, we must
962 // allow the value to be live out of the bb.
963 continue;
Owen Anderson80fe8732008-11-11 22:11:27 +0000964 if (!CurrLI->liveAt(LIs->getMBBEndIdx(Pred)-1))
965 return;
Evan Chengaaf510c2008-10-26 07:49:03 +0000966 ShrinkWrapLiveInterval(ValNo, Pred, MBB, DefMBB, Visited,
967 Uses, UseMIs, UseMBBs);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000968 }
969
970 return;
971}
972
Owen Anderson75fa96b2008-11-19 04:28:29 +0000973
Owen Anderson6002e992008-12-04 21:20:30 +0000974void PreAllocSplitting::RepairLiveInterval(LiveInterval* CurrLI,
975 VNInfo* ValNo,
976 MachineInstr* DefMI,
977 unsigned RestoreIdx) {
Owen Anderson75fa96b2008-11-19 04:28:29 +0000978 // Shrink wrap the live interval by walking up the CFG and find the
979 // new kills.
980 // Now let's find all the uses of the val#.
981 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > Uses;
982 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > UseMIs;
983 SmallPtrSet<MachineBasicBlock*, 4> Seen;
984 SmallVector<MachineBasicBlock*, 4> UseMBBs;
985 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(CurrLI->reg),
986 UE = MRI->use_end(); UI != UE; ++UI) {
987 MachineOperand &UseMO = UI.getOperand();
988 MachineInstr *UseMI = UseMO.getParent();
989 unsigned UseIdx = LIs->getInstructionIndex(UseMI);
990 LiveInterval::iterator ULR = CurrLI->FindLiveRangeContaining(UseIdx);
991 if (ULR->valno != ValNo)
992 continue;
993 MachineBasicBlock *UseMBB = UseMI->getParent();
994 // Remember which other mbb's use this val#.
995 if (Seen.insert(UseMBB) && UseMBB != BarrierMBB)
996 UseMBBs.push_back(UseMBB);
997 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
998 UMII = Uses.find(UseMBB);
999 if (UMII != Uses.end()) {
1000 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
1001 UMII2 = UseMIs.find(UseMBB);
1002 UMII->second.push_back(&UseMO);
1003 UMII2->second.insert(UseMI);
1004 } else {
1005 SmallVector<MachineOperand*, 4> Ops;
1006 Ops.push_back(&UseMO);
1007 Uses.insert(std::make_pair(UseMBB, Ops));
1008 SmallPtrSet<MachineInstr*, 4> MIs;
1009 MIs.insert(UseMI);
1010 UseMIs.insert(std::make_pair(UseMBB, MIs));
1011 }
1012 }
1013
1014 // Walk up the predecessor chains.
1015 SmallPtrSet<MachineBasicBlock*, 8> Visited;
1016 ShrinkWrapLiveInterval(ValNo, BarrierMBB, NULL, DefMI->getParent(), Visited,
1017 Uses, UseMIs, UseMBBs);
1018
Owen Anderson75fa96b2008-11-19 04:28:29 +00001019 // Remove live range from barrier to the restore. FIXME: Find a better
1020 // point to re-start the live interval.
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001021 VNInfo* AfterValNo = UpdateRegisterInterval(ValNo,
1022 LIs->getUseIndex(BarrierIdx)+1,
1023 LIs->getDefIndex(RestoreIdx));
1024
1025 // Attempt to renumber the new valno into a new vreg.
1026 RenumberValno(AfterValNo);
Owen Anderson6002e992008-12-04 21:20:30 +00001027}
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001028
1029/// RenumberValno - Split the given valno out into a new vreg, allowing it to
1030/// be allocated to a different register. This function creates a new vreg,
1031/// copies the valno and its live ranges over to the new vreg's interval,
1032/// removes them from the old interval, and rewrites all uses and defs of
1033/// the original reg to the new vreg within those ranges.
1034void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001035 SmallVector<VNInfo*, 4> Stack;
1036 SmallVector<VNInfo*, 4> VNsToCopy;
1037 Stack.push_back(VN);
1038
1039 // Walk through and copy the valno we care about, and any other valnos
1040 // that are two-address redefinitions of the one we care about. These
1041 // will need to be rewritten as well. We also check for safety of the
1042 // renumbering here, by making sure that none of the valno involved has
1043 // phi kills.
1044 while (!Stack.empty()) {
1045 VNInfo* OldVN = Stack.back();
1046 Stack.pop_back();
1047
1048 // Bail out if we ever encounter a valno that has a PHI kill. We can't
1049 // renumber these.
1050 if (OldVN->hasPHIKill) return;
1051
1052 VNsToCopy.push_back(OldVN);
1053
1054 // Locate two-address redefinitions
1055 for (SmallVector<unsigned, 4>::iterator KI = OldVN->kills.begin(),
1056 KE = OldVN->kills.end(); KI != KE; ++KI) {
1057 MachineInstr* MI = LIs->getInstructionFromIndex(*KI);
1058 //if (!MI) continue;
1059 unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg);
1060 if (DefIdx == ~0U) continue;
1061 if (MI->isRegReDefinedByTwoAddr(DefIdx)) {
1062 VNInfo* NextVN =
1063 CurrLI->findDefinedVNInfo(LiveIntervals::getDefIndex(*KI));
1064 Stack.push_back(NextVN);
1065 }
1066 }
1067 }
1068
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001069 // Create the new vreg
1070 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
1071
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001072 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001073 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001074
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001075 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
1076 VNsToCopy.end(); OI != OE; ++OI) {
1077 VNInfo* OldVN = *OI;
1078
1079 // Copy the valno over
1080 VNInfo* NewVN = NewLI.getNextValue(OldVN->def, OldVN->copy,
1081 LIs->getVNInfoAllocator());
1082 NewLI.copyValNumInfo(NewVN, OldVN);
1083 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
1084
1085 // Remove the valno from the old interval
1086 CurrLI->removeValNo(OldVN);
1087 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001088
1089 // Rewrite defs and uses. This is done in two stages to avoid invalidating
1090 // the reg_iterator.
1091 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
1092
1093 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
1094 E = MRI->reg_end(); I != E; ++I) {
1095 MachineOperand& MO = I.getOperand();
1096 unsigned InstrIdx = LIs->getInstructionIndex(&*I);
1097
1098 if ((MO.isUse() && NewLI.liveAt(LiveIntervals::getUseIndex(InstrIdx))) ||
1099 (MO.isDef() && NewLI.liveAt(LiveIntervals::getDefIndex(InstrIdx))))
1100 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
1101 }
1102
1103 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
1104 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
1105 MachineInstr* Inst = I->first;
1106 unsigned OpIdx = I->second;
1107 MachineOperand& MO = Inst->getOperand(OpIdx);
1108 MO.setReg(NewVReg);
1109 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +00001110
1111 NumRenumbers++;
Owen Andersond0b6a0d2008-12-16 21:35:08 +00001112}
1113
Owen Anderson6002e992008-12-04 21:20:30 +00001114bool PreAllocSplitting::Rematerialize(unsigned vreg, VNInfo* ValNo,
1115 MachineInstr* DefMI,
1116 MachineBasicBlock::iterator RestorePt,
1117 unsigned RestoreIdx,
1118 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
1119 MachineBasicBlock& MBB = *RestorePt->getParent();
1120
1121 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
1122 unsigned KillIdx = 0;
1123 if (ValNo->def == ~0U || DefMI->getParent() == BarrierMBB)
1124 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx);
1125 else
1126 KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx);
1127
1128 if (KillPt == DefMI->getParent()->end())
1129 return false;
1130
1131 TII->reMaterialize(MBB, RestorePt, vreg, DefMI);
1132 LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx);
1133
1134 if (KillPt->getParent() == BarrierMBB) {
Owen Anderson6cf7c392009-01-21 00:13:28 +00001135 VNInfo* After = UpdateRegisterInterval(ValNo, LIs->getUseIndex(KillIdx)+1,
Owen Anderson6002e992008-12-04 21:20:30 +00001136 LIs->getDefIndex(RestoreIdx));
Owen Andersone1762c92009-01-12 03:10:40 +00001137
Owen Anderson6cf7c392009-01-21 00:13:28 +00001138 RenumberValno(After);
1139
Owen Anderson6002e992008-12-04 21:20:30 +00001140 ++NumSplits;
1141 ++NumRemats;
1142 return true;
1143 }
1144
1145 RepairLiveInterval(CurrLI, ValNo, DefMI, RestoreIdx);
Owen Andersone1762c92009-01-12 03:10:40 +00001146
Owen Anderson75fa96b2008-11-19 04:28:29 +00001147 ++NumSplits;
1148 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001149 return true;
1150}
1151
1152MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
1153 const TargetRegisterClass* RC,
1154 MachineInstr* DefMI,
1155 MachineInstr* Barrier,
1156 MachineBasicBlock* MBB,
1157 int& SS,
1158 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
1159 MachineBasicBlock::iterator Pt = MBB->begin();
1160
1161 // Go top down if RefsInMBB is empty.
1162 if (RefsInMBB.empty())
1163 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +00001164
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001165 MachineBasicBlock::iterator FoldPt = Barrier;
1166 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
1167 !RefsInMBB.count(FoldPt))
1168 --FoldPt;
1169
1170 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
1171 if (OpIdx == -1)
1172 return 0;
1173
1174 SmallVector<unsigned, 1> Ops;
1175 Ops.push_back(OpIdx);
1176
1177 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
1178 return 0;
1179
1180 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
1181 if (I != IntervalSSMap.end()) {
1182 SS = I->second;
1183 } else {
1184 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
1185
1186 }
1187
1188 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
1189 FoldPt, Ops, SS);
1190
1191 if (FMI) {
1192 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
1193 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
1194 ++NumFolds;
1195
1196 IntervalSSMap[vreg] = SS;
1197 CurrSLI = &LSs->getOrCreateInterval(SS);
1198 if (CurrSLI->hasAtLeastOneValue())
1199 CurrSValNo = CurrSLI->getValNumInfo(0);
1200 else
1201 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
1202 }
1203
1204 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +00001205}
1206
Evan Chengf5cd4f02008-10-23 20:43:13 +00001207/// SplitRegLiveInterval - Split (spill and restore) the given live interval
1208/// so it would not cross the barrier that's being processed. Shrink wrap
1209/// (minimize) the live interval to the last uses.
1210bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
1211 CurrLI = LI;
1212
1213 // Find live range where current interval cross the barrier.
1214 LiveInterval::iterator LR =
1215 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
1216 VNInfo *ValNo = LR->valno;
1217
1218 if (ValNo->def == ~1U) {
1219 // Defined by a dead def? How can this be?
1220 assert(0 && "Val# is defined by a dead def?");
1221 abort();
1222 }
1223
Evan Cheng06587492008-10-24 02:05:00 +00001224 MachineInstr *DefMI = (ValNo->def != ~0U)
1225 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +00001226
Owen Andersond3be4622009-01-21 08:18:03 +00001227 // If this would create a new join point, do not split.
1228 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent()))
1229 return false;
1230
Evan Chengf5cd4f02008-10-23 20:43:13 +00001231 // Find all references in the barrier mbb.
1232 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
1233 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
1234 E = MRI->reg_end(); I != E; ++I) {
1235 MachineInstr *RefMI = &*I;
1236 if (RefMI->getParent() == BarrierMBB)
1237 RefsInMBB.insert(RefMI);
1238 }
1239
1240 // Find a point to restore the value after the barrier.
Evan Chengb964f332009-01-26 18:33:51 +00001241 unsigned RestoreIndex = 0;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001242 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +00001243 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +00001244 if (RestorePt == BarrierMBB->end())
1245 return false;
1246
Owen Anderson75fa96b2008-11-19 04:28:29 +00001247 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
1248 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt,
1249 RestoreIndex, RefsInMBB))
1250 return true;
1251
Evan Chengf5cd4f02008-10-23 20:43:13 +00001252 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001253 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001254 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Evan Chengf5cd4f02008-10-23 20:43:13 +00001255 unsigned SpillIndex = 0;
Evan Cheng06587492008-10-24 02:05:00 +00001256 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001257 int SS = -1;
Evan Cheng78dfef72008-10-25 00:52:41 +00001258 if (ValNo->def == ~0U) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001259 // If it's defined by a phi, we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001260 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1261 BarrierMBB, SS, RefsInMBB))) {
1262 SpillIndex = LIs->getInstructionIndex(SpillMI);
1263 } else {
1264 MachineBasicBlock::iterator SpillPt =
1265 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
1266 if (SpillPt == BarrierMBB->begin())
1267 return false; // No gap to insert spill.
1268 // Add spill.
1269
1270 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1271 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
1272 SpillMI = prior(SpillPt);
1273 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
1274 }
Evan Cheng54898932008-10-29 08:39:34 +00001275 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
1276 RestoreIndex, SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001277 // If it's already split, just restore the value. There is no need to spill
1278 // the def again.
Evan Chengd0e32c52008-10-29 05:06:14 +00001279 if (!DefMI)
1280 return false; // Def is dead. Do nothing.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001281
1282 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
1283 BarrierMBB, SS, RefsInMBB))) {
1284 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001285 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001286 // Check if it's possible to insert a spill after the def MI.
1287 MachineBasicBlock::iterator SpillPt;
1288 if (DefMBB == BarrierMBB) {
1289 // Add spill after the def and the last use before the barrier.
1290 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
1291 RefsInMBB, SpillIndex);
1292 if (SpillPt == DefMBB->begin())
1293 return false; // No gap to insert spill.
1294 } else {
1295 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
1296 if (SpillPt == DefMBB->end())
1297 return false; // No gap to insert spill.
1298 }
1299 // Add spill. The store instruction kills the register if def is before
1300 // the barrier in the barrier block.
1301 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1302 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
1303 DefMBB == BarrierMBB, SS, RC);
1304 SpillMI = prior(SpillPt);
1305 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001306 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001307 }
1308
Evan Cheng54898932008-10-29 08:39:34 +00001309 // Remember def instruction index to spill index mapping.
1310 if (DefMI && SpillMI)
1311 Def2SpillMap[ValNo->def] = SpillIndex;
1312
Evan Chengf5cd4f02008-10-23 20:43:13 +00001313 // Add restore.
Evan Chengf5cd4f02008-10-23 20:43:13 +00001314 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
1315 MachineInstr *LoadMI = prior(RestorePt);
1316 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
1317
1318 // If live interval is spilled in the same block as the barrier, just
1319 // create a hole in the interval.
1320 if (!DefMBB ||
Evan Cheng78dfef72008-10-25 00:52:41 +00001321 (SpillMI && SpillMI->getParent() == BarrierMBB)) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001322 // Update spill stack slot live interval.
1323 UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
1324 LIs->getDefIndex(RestoreIndex));
Evan Chengf5cd4f02008-10-23 20:43:13 +00001325
Owen Anderson6cf7c392009-01-21 00:13:28 +00001326 VNInfo* After = UpdateRegisterInterval(ValNo,
1327 LIs->getUseIndex(SpillIndex)+1,
Evan Chengd0e32c52008-10-29 05:06:14 +00001328 LIs->getDefIndex(RestoreIndex));
Owen Anderson6cf7c392009-01-21 00:13:28 +00001329 RenumberValno(After);
1330
Evan Chengae7fa5b2008-10-28 01:48:24 +00001331 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001332 return true;
1333 }
1334
Evan Chengd0e32c52008-10-29 05:06:14 +00001335 // Update spill stack slot live interval.
Evan Cheng54898932008-10-29 08:39:34 +00001336 UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
1337 LIs->getDefIndex(RestoreIndex));
Evan Chengd0e32c52008-10-29 05:06:14 +00001338
Owen Anderson6002e992008-12-04 21:20:30 +00001339 RepairLiveInterval(CurrLI, ValNo, DefMI, RestoreIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +00001340
Evan Chengae7fa5b2008-10-28 01:48:24 +00001341 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001342 return true;
1343}
1344
1345/// SplitRegLiveIntervals - Split all register live intervals that cross the
1346/// barrier that's being processed.
1347bool
Owen Anderson956ec272009-01-23 00:23:32 +00001348PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1349 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001350 // First find all the virtual registers whose live intervals are intercepted
1351 // by the current barrier.
1352 SmallVector<LiveInterval*, 8> Intervals;
1353 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng23066282008-10-27 07:14:50 +00001354 if (TII->IgnoreRegisterClassBarriers(*RC))
1355 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001356 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
1357 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1358 unsigned Reg = VRs[i];
1359 if (!LIs->hasInterval(Reg))
1360 continue;
1361 LiveInterval *LI = &LIs->getInterval(Reg);
1362 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1363 // Virtual register live interval is intercepted by the barrier. We
1364 // should split and shrink wrap its interval if possible.
1365 Intervals.push_back(LI);
1366 }
1367 }
1368
1369 // Process the affected live intervals.
1370 bool Change = false;
1371 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001372 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1373 break;
Owen Andersone1762c92009-01-12 03:10:40 +00001374 else if (NumSplits == 4)
1375 Change |= Change;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001376 LiveInterval *LI = Intervals.back();
1377 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001378 bool result = SplitRegLiveInterval(LI);
1379 if (result) Split.insert(LI);
1380 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001381 }
1382
1383 return Change;
1384}
1385
Owen Anderson32ca8652009-01-24 10:07:43 +00001386unsigned PreAllocSplitting::getNumberOfSpills(
1387 SmallPtrSet<MachineInstr*, 4>& MIs,
1388 unsigned Reg, int FrameIndex) {
1389 unsigned Spills = 0;
1390 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
1391 UI != UI; ++UI) {
1392 int StoreFrameIndex;
1393 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1394 if (StoreVReg == Reg && StoreFrameIndex == FrameIndex)
1395 Spills++;
1396 }
1397
1398 return Spills;
1399}
1400
Owen Anderson956ec272009-01-23 00:23:32 +00001401/// removeDeadSpills - After doing splitting, filter through all intervals we've
1402/// split, and see if any of the spills are unnecessary. If so, remove them.
1403bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1404 bool changed = false;
1405
1406 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1407 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001408 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001409
1410 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1411 UE = MRI->use_end(); UI != UE; ++UI) {
1412 unsigned index = LIs->getInstructionIndex(&*UI);
1413 index = LiveIntervals::getUseIndex(index);
1414
1415 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001416 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001417 }
1418
1419 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1420 VE = (*LI)->vni_end(); VI != VE; ++VI) {
1421 VNInfo* CurrVN = *VI;
1422 if (CurrVN->hasPHIKill) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001423
1424 unsigned DefIdx = CurrVN->def;
1425 if (DefIdx == ~0U || DefIdx == ~1U) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001426
Owen Anderson956ec272009-01-23 00:23:32 +00001427 MachineInstr* DefMI = LIs->getInstructionFromIndex(DefIdx);
1428 int FrameIndex;
1429 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1430
Owen Anderson9ce499a2009-01-23 03:28:53 +00001431 if (VNUseCount[CurrVN].size() == 0) {
1432 LIs->RemoveMachineInstrFromMaps(DefMI);
1433 (*LI)->removeValNo(CurrVN);
1434 DefMI->eraseFromParent();
1435 NumDeadSpills++;
1436 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001437 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001438 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001439
1440 unsigned SpillCount = getNumberOfSpills(VNUseCount[CurrVN],
1441 (*LI)->reg, FrameIndex);
1442 if (SpillCount != VNUseCount[CurrVN].size()) continue;
1443
1444 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1445 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
1446 UI != UI; ++UI) {
1447 LIs->RemoveMachineInstrFromMaps(*UI);
1448 (*UI)->eraseFromParent();
1449 }
1450
1451 LIs->RemoveMachineInstrFromMaps(DefMI);
1452 (*LI)->removeValNo(CurrVN);
1453 DefMI->eraseFromParent();
1454 NumDeadSpills++;
1455 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001456 }
1457 }
1458
1459 return changed;
1460}
1461
Owen Andersonf1f75b12008-11-04 22:22:41 +00001462bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1463 MachineBasicBlock* DefMBB,
1464 MachineBasicBlock* BarrierMBB) {
1465 if (DefMBB == BarrierMBB)
1466 return false;
1467
1468 if (LR->valno->hasPHIKill)
1469 return false;
1470
1471 unsigned MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
1472 if (LR->end < MBBEnd)
1473 return false;
1474
1475 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1476 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1477 return true;
1478
1479 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1480 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1481 typedef std::pair<MachineBasicBlock*,
1482 MachineBasicBlock::succ_iterator> ItPair;
1483 SmallVector<ItPair, 4> Stack;
1484 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1485
1486 while (!Stack.empty()) {
1487 ItPair P = Stack.back();
1488 Stack.pop_back();
1489
1490 MachineBasicBlock* PredMBB = P.first;
1491 MachineBasicBlock::succ_iterator S = P.second;
1492
1493 if (S == PredMBB->succ_end())
1494 continue;
1495 else if (Visited.count(*S)) {
1496 Stack.push_back(std::make_pair(PredMBB, ++S));
1497 continue;
1498 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001499 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001500
1501 MachineBasicBlock* MBB = *S;
1502 Visited.insert(MBB);
1503
1504 if (MBB == BarrierMBB)
1505 return true;
1506
1507 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1508 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1509 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1510 while (MDTN) {
1511 if (MDTN == DefMDTN)
1512 return true;
1513 else if (MDTN == BarrierMDTN)
1514 break;
1515 MDTN = MDTN->getIDom();
1516 }
1517
1518 MBBEnd = LIs->getMBBEndIdx(MBB);
1519 if (LR->end > MBBEnd)
1520 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1521 }
1522
1523 return false;
1524}
1525
1526
Evan Cheng09e8ca82008-10-20 21:44:59 +00001527bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001528 CurrMF = &MF;
1529 TM = &MF.getTarget();
1530 TII = TM->getInstrInfo();
1531 MFI = MF.getFrameInfo();
1532 MRI = &MF.getRegInfo();
1533 LIs = &getAnalysis<LiveIntervals>();
1534 LSs = &getAnalysis<LiveStacks>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001535
1536 bool MadeChange = false;
1537
1538 // Make sure blocks are numbered in order.
1539 MF.RenumberBlocks();
1540
Evan Cheng54898932008-10-29 08:39:34 +00001541 MachineBasicBlock *Entry = MF.begin();
1542 SmallPtrSet<MachineBasicBlock*,16> Visited;
1543
Owen Anderson956ec272009-01-23 00:23:32 +00001544 SmallPtrSet<LiveInterval*, 8> Split;
1545
Evan Cheng54898932008-10-29 08:39:34 +00001546 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1547 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1548 DFI != E; ++DFI) {
1549 BarrierMBB = *DFI;
1550 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1551 E = BarrierMBB->end(); I != E; ++I) {
1552 Barrier = &*I;
1553 const TargetRegisterClass **BarrierRCs =
1554 Barrier->getDesc().getRegClassBarriers();
1555 if (!BarrierRCs)
1556 continue;
1557 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001558 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001559 }
1560 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001561
Owen Anderson956ec272009-01-23 00:23:32 +00001562 MadeChange |= removeDeadSpills(Split);
1563
Evan Chengf5cd4f02008-10-23 20:43:13 +00001564 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001565}