blob: 6191bf9f30d48dcf287f3521152da2fb34587f8f [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"
Owen Anderson420dd372009-03-14 21:40:05 +000018#include "VirtRegMap.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000020#include "llvm/CodeGen/LiveStackAnalysis.h"
Owen Andersonf1f75b12008-11-04 22:22:41 +000021#include "llvm/CodeGen/MachineDominators.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineLoopInfo.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000028#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000029#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetOptions.h"
31#include "llvm/Target/TargetRegisterInfo.h"
32#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000035#include "llvm/ADT/DenseMap.h"
Evan Cheng54898932008-10-29 08:39:34 +000036#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000037#include "llvm/ADT/SmallPtrSet.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000038#include "llvm/ADT/Statistic.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000039using namespace llvm;
40
Evan Chengae7fa5b2008-10-28 01:48:24 +000041static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden);
Owen Anderson45e68552009-01-29 05:28:55 +000042static cl::opt<int> DeadSplitLimit("dead-split-limit", cl::init(-1), cl::Hidden);
Owen Anderson323c58d2009-03-05 07:19:18 +000043static cl::opt<int> RestoreFoldLimit("restore-fold-limit", cl::init(-1), cl::Hidden);
Evan Chengae7fa5b2008-10-28 01:48:24 +000044
Owen Anderson45e68552009-01-29 05:28:55 +000045STATISTIC(NumSplits, "Number of intervals split");
Owen Anderson75fa96b2008-11-19 04:28:29 +000046STATISTIC(NumRemats, "Number of intervals split by rematerialization");
Owen Anderson7b9d67c2008-12-02 18:53:47 +000047STATISTIC(NumFolds, "Number of intervals split with spill folding");
Owen Anderson323c58d2009-03-05 07:19:18 +000048STATISTIC(NumRestoreFolds, "Number of intervals split with restore folding");
Owen Anderson2ebf63f2008-12-18 01:27:19 +000049STATISTIC(NumRenumbers, "Number of intervals renumbered into new registers");
Owen Anderson956ec272009-01-23 00:23:32 +000050STATISTIC(NumDeadSpills, "Number of dead spills removed");
Evan Chengf5cd4f02008-10-23 20:43:13 +000051
Evan Cheng09e8ca82008-10-20 21:44:59 +000052namespace {
53 class VISIBILITY_HIDDEN PreAllocSplitting : public MachineFunctionPass {
Evan Chengd0e32c52008-10-29 05:06:14 +000054 MachineFunction *CurrMF;
Evan Chengf5cd4f02008-10-23 20:43:13 +000055 const TargetMachine *TM;
56 const TargetInstrInfo *TII;
Owen Anderson3ef45492009-01-29 22:13:06 +000057 const TargetRegisterInfo* TRI;
Evan Chengf5cd4f02008-10-23 20:43:13 +000058 MachineFrameInfo *MFI;
59 MachineRegisterInfo *MRI;
60 LiveIntervals *LIs;
Evan Chengd0e32c52008-10-29 05:06:14 +000061 LiveStacks *LSs;
Owen Anderson420dd372009-03-14 21:40:05 +000062 VirtRegMap *VRM;
Evan Cheng09e8ca82008-10-20 21:44:59 +000063
Evan Chengf5cd4f02008-10-23 20:43:13 +000064 // Barrier - Current barrier being processed.
65 MachineInstr *Barrier;
66
67 // BarrierMBB - Basic block where the barrier resides in.
68 MachineBasicBlock *BarrierMBB;
69
70 // Barrier - Current barrier index.
Lang Hames86511252009-09-04 20:41:11 +000071 MachineInstrIndex BarrierIdx;
Evan Chengf5cd4f02008-10-23 20:43:13 +000072
73 // CurrLI - Current live interval being split.
74 LiveInterval *CurrLI;
75
Evan Chengd0e32c52008-10-29 05:06:14 +000076 // CurrSLI - Current stack slot live interval.
77 LiveInterval *CurrSLI;
78
79 // CurrSValNo - Current val# for the stack slot live interval.
80 VNInfo *CurrSValNo;
81
82 // IntervalSSMap - A map from live interval to spill slots.
83 DenseMap<unsigned, int> IntervalSSMap;
Evan Chengf5cd4f02008-10-23 20:43:13 +000084
Evan Cheng54898932008-10-29 08:39:34 +000085 // Def2SpillMap - A map from a def instruction index to spill index.
Lang Hames86511252009-09-04 20:41:11 +000086 DenseMap<MachineInstrIndex, MachineInstrIndex> Def2SpillMap;
Evan Cheng06587492008-10-24 02:05:00 +000087
Evan Cheng09e8ca82008-10-20 21:44:59 +000088 public:
89 static char ID;
90 PreAllocSplitting() : MachineFunctionPass(&ID) {}
91
92 virtual bool runOnMachineFunction(MachineFunction &MF);
93
94 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000095 AU.setPreservesCFG();
Evan Cheng09e8ca82008-10-20 21:44:59 +000096 AU.addRequired<LiveIntervals>();
97 AU.addPreserved<LiveIntervals>();
Evan Chengd0e32c52008-10-29 05:06:14 +000098 AU.addRequired<LiveStacks>();
99 AU.addPreserved<LiveStacks>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000100 AU.addPreserved<RegisterCoalescer>();
101 if (StrongPHIElim)
102 AU.addPreservedID(StrongPHIEliminationID);
103 else
104 AU.addPreservedID(PHIEliminationID);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000105 AU.addRequired<MachineDominatorTree>();
106 AU.addRequired<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000107 AU.addRequired<VirtRegMap>();
Owen Andersonf1f75b12008-11-04 22:22:41 +0000108 AU.addPreserved<MachineDominatorTree>();
109 AU.addPreserved<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000110 AU.addPreserved<VirtRegMap>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000111 MachineFunctionPass::getAnalysisUsage(AU);
112 }
113
114 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +0000115 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +0000116 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000117 }
118
119 virtual const char *getPassName() const {
120 return "Pre-Register Allocaton Live Interval Splitting";
121 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000122
123 /// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000124 virtual void print(raw_ostream &O, const Module* M = 0) const {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000125 LIs->print(O, M);
126 }
127
Evan Chengf5cd4f02008-10-23 20:43:13 +0000128
129 private:
130 MachineBasicBlock::iterator
131 findNextEmptySlot(MachineBasicBlock*, MachineInstr*,
Lang Hames86511252009-09-04 20:41:11 +0000132 MachineInstrIndex&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000133
134 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000135 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Lang Hames86511252009-09-04 20:41:11 +0000136 SmallPtrSet<MachineInstr*, 4>&, MachineInstrIndex&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000137
138 MachineBasicBlock::iterator
Lang Hames86511252009-09-04 20:41:11 +0000139 findRestorePoint(MachineBasicBlock*, MachineInstr*, MachineInstrIndex,
140 SmallPtrSet<MachineInstr*, 4>&, MachineInstrIndex&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000141
Evan Chengd0e32c52008-10-29 05:06:14 +0000142 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000143
Lang Hames86511252009-09-04 20:41:11 +0000144 bool IsAvailableInStack(MachineBasicBlock*, unsigned,
145 MachineInstrIndex, MachineInstrIndex,
146 MachineInstrIndex&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000147
Lang Hames86511252009-09-04 20:41:11 +0000148 void UpdateSpillSlotInterval(VNInfo*, MachineInstrIndex, MachineInstrIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000149
Evan Chengf5cd4f02008-10-23 20:43:13 +0000150 bool SplitRegLiveInterval(LiveInterval*);
151
Owen Anderson956ec272009-01-23 00:23:32 +0000152 bool SplitRegLiveIntervals(const TargetRegisterClass **,
153 SmallPtrSet<LiveInterval*, 8>&);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000154
155 bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB,
156 MachineBasicBlock* BarrierMBB);
Owen Anderson75fa96b2008-11-19 04:28:29 +0000157 bool Rematerialize(unsigned vreg, VNInfo* ValNo,
158 MachineInstr* DefMI,
159 MachineBasicBlock::iterator RestorePt,
Lang Hames86511252009-09-04 20:41:11 +0000160 MachineInstrIndex RestoreIdx,
Owen Anderson75fa96b2008-11-19 04:28:29 +0000161 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000162 MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC,
163 MachineInstr* DefMI,
164 MachineInstr* Barrier,
165 MachineBasicBlock* MBB,
166 int& SS,
167 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersonc93023a2009-03-04 08:52:31 +0000168 MachineInstr* FoldRestore(unsigned vreg,
169 const TargetRegisterClass* RC,
170 MachineInstr* Barrier,
171 MachineBasicBlock* MBB,
172 int SS,
173 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000174 void RenumberValno(VNInfo* VN);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000175 void ReconstructLiveInterval(LiveInterval* LI);
Owen Anderson956ec272009-01-23 00:23:32 +0000176 bool removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split);
Owen Anderson45e68552009-01-29 05:28:55 +0000177 unsigned getNumberOfNonSpills(SmallPtrSet<MachineInstr*, 4>& MIs,
178 unsigned Reg, int FrameIndex, bool& TwoAddr);
Evan Cheng19a72582009-02-02 18:33:18 +0000179 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator Use,
180 MachineBasicBlock* MBB, 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,
Evan Cheng19a72582009-02-02 18:33:18 +0000187 bool IsTopLevel, bool IsIntraBlock);
188 VNInfo* PerformPHIConstructionFallBack(MachineBasicBlock::iterator Use,
189 MachineBasicBlock* MBB, LiveInterval* LI,
190 SmallPtrSet<MachineInstr*, 4>& Visited,
191 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
192 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
193 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
194 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
195 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
196 bool IsTopLevel, bool IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000197};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000198} // end anonymous namespace
199
200char PreAllocSplitting::ID = 0;
201
202static RegisterPass<PreAllocSplitting>
203X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
204
205const PassInfo *const llvm::PreAllocSplittingID = &X;
206
Evan Chengf5cd4f02008-10-23 20:43:13 +0000207
208/// findNextEmptySlot - Find a gap after the given machine instruction in the
209/// instruction index map. If there isn't one, return end().
210MachineBasicBlock::iterator
211PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
Lang Hames86511252009-09-04 20:41:11 +0000212 MachineInstrIndex &SpotIndex) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000213 MachineBasicBlock::iterator MII = MI;
214 if (++MII != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +0000215 MachineInstrIndex Index =
216 LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
217 if (Index != MachineInstrIndex()) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000218 SpotIndex = Index;
219 return MII;
220 }
221 }
222 return MBB->end();
223}
224
225/// findSpillPoint - Find a gap as far away from the given MI that's suitable
226/// for spilling the current live interval. The index must be before any
227/// defs and uses of the live interval register in the mbb. Return begin() if
228/// none is found.
229MachineBasicBlock::iterator
230PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000231 MachineInstr *DefMI,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000232 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
Lang Hames86511252009-09-04 20:41:11 +0000233 MachineInstrIndex &SpillIndex) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000234 MachineBasicBlock::iterator Pt = MBB->begin();
235
Owen Anderson696a1302009-03-31 08:27:09 +0000236 MachineBasicBlock::iterator MII = MI;
237 MachineBasicBlock::iterator EndPt = DefMI
238 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
Owen Anderson4cafbb52009-02-20 10:02:23 +0000239
Owen Anderson696a1302009-03-31 08:27:09 +0000240 while (MII != EndPt && !RefsInMBB.count(MII) &&
241 MII->getOpcode() != TRI->getCallFrameSetupOpcode())
242 --MII;
243 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson4cafbb52009-02-20 10:02:23 +0000244
Owen Anderson696a1302009-03-31 08:27:09 +0000245 while (MII != EndPt && !RefsInMBB.count(MII)) {
Lang Hames86511252009-09-04 20:41:11 +0000246 MachineInstrIndex Index = LIs->getInstructionIndex(MII);
Owen Anderson3ef45492009-01-29 22:13:06 +0000247
Owen Anderson696a1302009-03-31 08:27:09 +0000248 // We can't insert the spill between the barrier (a call), and its
249 // corresponding call frame setup.
250 if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
251 while (MII->getOpcode() != TRI->getCallFrameSetupOpcode()) {
Owen Anderson5734c942009-02-05 05:58:41 +0000252 --MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000253 if (MII == EndPt) {
254 return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000255 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000256 }
Owen Anderson696a1302009-03-31 08:27:09 +0000257 continue;
258 } else if (LIs->hasGapBeforeInstr(Index)) {
259 Pt = MII;
260 SpillIndex = LIs->findGapBeforeInstr(Index, true);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000261 }
Owen Anderson696a1302009-03-31 08:27:09 +0000262
263 if (RefsInMBB.count(MII))
264 return Pt;
265
266
267 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000268 }
269
270 return Pt;
271}
272
273/// findRestorePoint - Find a gap in the instruction index map that's suitable
274/// for restoring the current live interval value. The index must be before any
275/// uses of the live interval register in the mbb. Return end() if none is
276/// found.
277MachineBasicBlock::iterator
278PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Lang Hames86511252009-09-04 20:41:11 +0000279 MachineInstrIndex LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000280 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
Lang Hames86511252009-09-04 20:41:11 +0000281 MachineInstrIndex &RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000282 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
283 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000284 MachineBasicBlock::iterator Pt = MBB->end();
Owen Anderson696a1302009-03-31 08:27:09 +0000285 MachineBasicBlock::iterator EndPt = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000286
Owen Anderson696a1302009-03-31 08:27:09 +0000287 // We start at the call, so walk forward until we find the call frame teardown
288 // since we can't insert restores before that. Bail if we encounter a use
289 // during this time.
290 MachineBasicBlock::iterator MII = MI;
291 if (MII == EndPt) return Pt;
292
293 while (MII != EndPt && !RefsInMBB.count(MII) &&
294 MII->getOpcode() != TRI->getCallFrameDestroyOpcode())
295 ++MII;
296 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
297 ++MII;
298
299 // FIXME: Limit the number of instructions to examine to reduce
300 // compile time?
301 while (MII != EndPt) {
Lang Hames86511252009-09-04 20:41:11 +0000302 MachineInstrIndex Index = LIs->getInstructionIndex(MII);
Owen Anderson696a1302009-03-31 08:27:09 +0000303 if (Index > LastIdx)
304 break;
Lang Hames86511252009-09-04 20:41:11 +0000305 MachineInstrIndex Gap = LIs->findGapBeforeInstr(Index);
Owen Anderson3ef45492009-01-29 22:13:06 +0000306
Owen Anderson696a1302009-03-31 08:27:09 +0000307 // We can't insert a restore between the barrier (a call) and its
308 // corresponding call frame teardown.
309 if (MII->getOpcode() == TRI->getCallFrameSetupOpcode()) {
310 do {
311 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000312 ++MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000313 } while (MII->getOpcode() != TRI->getCallFrameDestroyOpcode());
Lang Hames86511252009-09-04 20:41:11 +0000314 } else if (Gap != MachineInstrIndex()) {
Owen Anderson696a1302009-03-31 08:27:09 +0000315 Pt = MII;
316 RestoreIndex = Gap;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000317 }
Owen Anderson696a1302009-03-31 08:27:09 +0000318
319 if (RefsInMBB.count(MII))
320 return Pt;
321
322 ++MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000323 }
324
325 return Pt;
326}
327
Evan Chengd0e32c52008-10-29 05:06:14 +0000328/// CreateSpillStackSlot - Create a stack slot for the live interval being
329/// split. If the live interval was previously split, just reuse the same
330/// slot.
331int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
332 const TargetRegisterClass *RC) {
333 int SS;
334 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
335 if (I != IntervalSSMap.end()) {
336 SS = I->second;
337 } else {
338 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
339 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000340 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000341
342 // Create live interval for stack slot.
Evan Chengc781a242009-05-03 18:32:42 +0000343 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Evan Cheng54898932008-10-29 08:39:34 +0000344 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000345 CurrSValNo = CurrSLI->getValNumInfo(0);
346 else
Lang Hames86511252009-09-04 20:41:11 +0000347 CurrSValNo = CurrSLI->getNextValue(MachineInstrIndex(), 0, false,
348 LSs->getVNInfoAllocator());
Evan Chengd0e32c52008-10-29 05:06:14 +0000349 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000350}
351
Evan Chengd0e32c52008-10-29 05:06:14 +0000352/// IsAvailableInStack - Return true if register is available in a split stack
353/// slot at the specified index.
354bool
Evan Cheng54898932008-10-29 08:39:34 +0000355PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
Lang Hames86511252009-09-04 20:41:11 +0000356 unsigned Reg, MachineInstrIndex DefIndex,
357 MachineInstrIndex RestoreIndex,
358 MachineInstrIndex &SpillIndex,
Evan Cheng54898932008-10-29 08:39:34 +0000359 int& SS) const {
360 if (!DefMBB)
361 return false;
362
Evan Chengd0e32c52008-10-29 05:06:14 +0000363 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
364 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000365 return false;
Lang Hames86511252009-09-04 20:41:11 +0000366 DenseMap<MachineInstrIndex, MachineInstrIndex>::iterator
367 II = Def2SpillMap.find(DefIndex);
Evan Cheng54898932008-10-29 08:39:34 +0000368 if (II == Def2SpillMap.end())
369 return false;
370
371 // If last spill of def is in the same mbb as barrier mbb (where restore will
372 // be), make sure it's not below the intended restore index.
373 // FIXME: Undo the previous spill?
374 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
375 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
376 return false;
377
378 SS = I->second;
379 SpillIndex = II->second;
380 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000381}
382
Evan Chengd0e32c52008-10-29 05:06:14 +0000383/// UpdateSpillSlotInterval - Given the specified val# of the register live
384/// interval being split, and the spill and restore indicies, update the live
385/// interval of the spill stack slot.
386void
Lang Hames86511252009-09-04 20:41:11 +0000387PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, MachineInstrIndex SpillIndex,
388 MachineInstrIndex RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000389 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
390 "Expect restore in the barrier mbb");
391
392 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
393 if (MBB == BarrierMBB) {
394 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000395 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
396 CurrSLI->addRange(SLR);
397 return;
398 }
399
Evan Cheng54898932008-10-29 08:39:34 +0000400 SmallPtrSet<MachineBasicBlock*, 4> Processed;
Lang Hames86511252009-09-04 20:41:11 +0000401 MachineInstrIndex EndIdx = LIs->getMBBEndIdx(MBB);
402 LiveRange SLR(SpillIndex, LIs->getNextSlot(EndIdx), CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000403 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000404 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000405
406 // Start from the spill mbb, figure out the extend of the spill slot's
407 // live interval.
408 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000409 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
410 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000411 // If live range extend beyond end of mbb, add successors to work list.
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
416 while (!WorkList.empty()) {
417 MachineBasicBlock *MBB = WorkList.back();
418 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000419 if (Processed.count(MBB))
420 continue;
Lang Hames86511252009-09-04 20:41:11 +0000421 MachineInstrIndex Idx = LIs->getMBBStartIdx(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000422 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000423 if (LR && LR->valno == ValNo) {
424 EndIdx = LIs->getMBBEndIdx(MBB);
425 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000426 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000427 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000428 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000429 } else if (LR->end > EndIdx) {
430 // Live range extends beyond end of mbb, process successors.
Lang Hames86511252009-09-04 20:41:11 +0000431 LiveRange SLR(Idx, LIs->getNextIndex(EndIdx), CurrSValNo);
Evan Cheng54898932008-10-29 08:39:34 +0000432 CurrSLI->addRange(SLR);
433 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
434 SE = MBB->succ_end(); SI != SE; ++SI)
435 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000436 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000437 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000438 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000439 }
Evan Cheng54898932008-10-29 08:39:34 +0000440 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000441 }
442 }
443}
444
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000445/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
446/// construction algorithm to compute the ranges and valnos for an interval.
Evan Cheng19a72582009-02-02 18:33:18 +0000447VNInfo*
448PreAllocSplitting::PerformPHIConstruction(MachineBasicBlock::iterator UseI,
449 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000450 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000451 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
452 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
453 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000454 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
455 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000456 bool IsTopLevel, bool IsIntraBlock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000457 // Return memoized result if it's available.
Evan Cheng19a72582009-02-02 18:33:18 +0000458 if (IsTopLevel && Visited.count(UseI) && NewVNs.count(UseI))
459 return NewVNs[UseI];
460 else if (!IsTopLevel && IsIntraBlock && NewVNs.count(UseI))
461 return NewVNs[UseI];
462 else if (!IsIntraBlock && LiveOut.count(MBB))
Owen Anderson7d211e22008-12-31 02:00:25 +0000463 return LiveOut[MBB];
464
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000465 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000466 bool ContainsDefs = Defs.count(MBB);
467 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000468
Evan Cheng19a72582009-02-02 18:33:18 +0000469 VNInfo* RetVNI = 0;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000470
471 // Enumerate the cases of use/def contaning blocks.
472 if (!ContainsDefs && !ContainsUses) {
Evan Cheng19a72582009-02-02 18:33:18 +0000473 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
474 NewVNs, LiveOut, Phis,
475 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000476 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000477 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000478
479 // Search for the def in this block. If we don't find it before the
480 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000481 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000482 // always be an end() iterator.
Evan Cheng19a72582009-02-02 18:33:18 +0000483 assert(UseI == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000484
Evan Cheng19a72582009-02-02 18:33:18 +0000485 MachineBasicBlock::iterator Walker = UseI;
486 --Walker;
487 while (Walker != MBB->begin()) {
488 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000489 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000490 --Walker;
491 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000492
493 // Once we've found it, extend its VNInfo to our instruction.
Lang Hames86511252009-09-04 20:41:11 +0000494 MachineInstrIndex DefIndex = LIs->getInstructionIndex(Walker);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000495 DefIndex = LiveIntervals::getDefIndex(DefIndex);
Lang Hames86511252009-09-04 20:41:11 +0000496 MachineInstrIndex EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000497
Evan Cheng19a72582009-02-02 18:33:18 +0000498 RetVNI = NewVNs[Walker];
Lang Hames86511252009-09-04 20:41:11 +0000499 LI->addRange(LiveRange(DefIndex, LIs->getNextSlot(EndIndex), RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000500 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000501 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000502
503 // Search for the use in this block that precedes the instruction we care
Evan Cheng19a72582009-02-02 18:33:18 +0000504 // about, going to the fallback case if we don't find it.
505 if (UseI == MBB->begin())
506 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
507 Uses, NewVNs, LiveOut, Phis,
508 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000509
Evan Cheng19a72582009-02-02 18:33:18 +0000510 MachineBasicBlock::iterator Walker = UseI;
511 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000512 bool found = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000513 while (Walker != MBB->begin()) {
514 if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000515 found = true;
516 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000517 }
518 --Walker;
519 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000520
521 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000522 if (!found) {
Evan Cheng19a72582009-02-02 18:33:18 +0000523 if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000524 found = true;
525 else
Evan Cheng19a72582009-02-02 18:33:18 +0000526 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
527 Uses, NewVNs, LiveOut, Phis,
528 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000529 }
530
Lang Hames86511252009-09-04 20:41:11 +0000531 MachineInstrIndex UseIndex = LIs->getInstructionIndex(Walker);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000532 UseIndex = LiveIntervals::getUseIndex(UseIndex);
Lang Hames86511252009-09-04 20:41:11 +0000533 MachineInstrIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000534 if (IsIntraBlock) {
535 EndIndex = LIs->getInstructionIndex(UseI);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000536 EndIndex = LiveIntervals::getUseIndex(EndIndex);
537 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000538 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000539
540 // Now, recursively phi construct the VNInfo for the use we found,
541 // and then extend it to include the instruction we care about
Evan Cheng19a72582009-02-02 18:33:18 +0000542 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
543 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000544
Lang Hames86511252009-09-04 20:41:11 +0000545 LI->addRange(LiveRange(UseIndex, LIs->getNextSlot(EndIndex), RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000546
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000547 // FIXME: Need to set kills properly for inter-block stuff.
Lang Hames86511252009-09-04 20:41:11 +0000548 if (RetVNI->isKill(UseIndex)) RetVNI->removeKill(UseIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000549 if (IsIntraBlock)
Lang Hames86511252009-09-04 20:41:11 +0000550 RetVNI->addKill(EndIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000551 } else if (ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000552 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
553 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000554
555 // This case is basically a merging of the two preceding case, with the
556 // special note that checking for defs must take precedence over checking
557 // for uses, because of two-address instructions.
558
Evan Cheng19a72582009-02-02 18:33:18 +0000559 if (UseI == MBB->begin())
560 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
561 NewVNs, LiveOut, Phis,
562 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000563
Evan Cheng19a72582009-02-02 18:33:18 +0000564 MachineBasicBlock::iterator Walker = UseI;
565 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000566 bool foundDef = false;
567 bool foundUse = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000568 while (Walker != MBB->begin()) {
569 if (BlockDefs.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000570 foundDef = true;
571 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000572 } else if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000573 foundUse = true;
574 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000575 }
576 --Walker;
577 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000578
579 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000580 if (!foundDef && !foundUse) {
Evan Cheng19a72582009-02-02 18:33:18 +0000581 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000582 foundDef = true;
Evan Cheng19a72582009-02-02 18:33:18 +0000583 else if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000584 foundUse = true;
585 else
Evan Cheng19a72582009-02-02 18:33:18 +0000586 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
587 Uses, NewVNs, LiveOut, Phis,
588 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000589 }
590
Lang Hames86511252009-09-04 20:41:11 +0000591 MachineInstrIndex StartIndex = LIs->getInstructionIndex(Walker);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000592 StartIndex = foundDef ? LiveIntervals::getDefIndex(StartIndex) :
593 LiveIntervals::getUseIndex(StartIndex);
Lang Hames86511252009-09-04 20:41:11 +0000594 MachineInstrIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000595 if (IsIntraBlock) {
596 EndIndex = LIs->getInstructionIndex(UseI);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000597 EndIndex = LiveIntervals::getUseIndex(EndIndex);
598 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000599 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000600
601 if (foundDef)
Evan Cheng19a72582009-02-02 18:33:18 +0000602 RetVNI = NewVNs[Walker];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000603 else
Evan Cheng19a72582009-02-02 18:33:18 +0000604 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
605 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000606
Lang Hames86511252009-09-04 20:41:11 +0000607 LI->addRange(LiveRange(StartIndex, LIs->getNextSlot(EndIndex), RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000608
Lang Hames86511252009-09-04 20:41:11 +0000609 if (foundUse && RetVNI->isKill(StartIndex))
610 RetVNI->removeKill(StartIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000611 if (IsIntraBlock) {
Lang Hames86511252009-09-04 20:41:11 +0000612 RetVNI->addKill(EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000613 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000614 }
615
616 // Memoize results so we don't have to recompute them.
Evan Cheng19a72582009-02-02 18:33:18 +0000617 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000618 else {
Evan Cheng19a72582009-02-02 18:33:18 +0000619 if (!NewVNs.count(UseI))
620 NewVNs[UseI] = RetVNI;
621 Visited.insert(UseI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000622 }
623
Evan Cheng19a72582009-02-02 18:33:18 +0000624 return RetVNI;
625}
626
627/// PerformPHIConstructionFallBack - PerformPHIConstruction fall back path.
628///
629VNInfo*
630PreAllocSplitting::PerformPHIConstructionFallBack(MachineBasicBlock::iterator UseI,
631 MachineBasicBlock* MBB, LiveInterval* LI,
632 SmallPtrSet<MachineInstr*, 4>& Visited,
633 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
634 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
635 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
636 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
637 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
638 bool IsTopLevel, bool IsIntraBlock) {
639 // NOTE: Because this is the fallback case from other cases, we do NOT
640 // assume that we are not intrablock here.
641 if (Phis.count(MBB)) return Phis[MBB];
642
Lang Hames86511252009-09-04 20:41:11 +0000643 MachineInstrIndex StartIndex = LIs->getMBBStartIdx(MBB);
Lang Hames857c4e02009-06-17 21:01:20 +0000644 VNInfo *RetVNI = Phis[MBB] =
Lang Hames86511252009-09-04 20:41:11 +0000645 LI->getNextValue(MachineInstrIndex(), /*FIXME*/ 0, false,
646 LIs->getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +0000647
Evan Cheng19a72582009-02-02 18:33:18 +0000648 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
649
650 // If there are no uses or defs between our starting point and the
651 // beginning of the block, then recursive perform phi construction
652 // on our predecessors.
653 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
654 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
655 PE = MBB->pred_end(); PI != PE; ++PI) {
656 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
657 Visited, Defs, Uses, NewVNs,
658 LiveOut, Phis, false, false);
659 if (Incoming != 0)
660 IncomingVNs[*PI] = Incoming;
661 }
662
Lang Hames857c4e02009-06-17 21:01:20 +0000663 if (MBB->pred_size() == 1 && !RetVNI->hasPHIKill()) {
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000664 VNInfo* OldVN = RetVNI;
665 VNInfo* NewVN = IncomingVNs.begin()->second;
666 VNInfo* MergedVN = LI->MergeValueNumberInto(OldVN, NewVN);
667 if (MergedVN == OldVN) std::swap(OldVN, NewVN);
668
669 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator LOI = LiveOut.begin(),
670 LOE = LiveOut.end(); LOI != LOE; ++LOI)
671 if (LOI->second == OldVN)
672 LOI->second = MergedVN;
673 for (DenseMap<MachineInstr*, VNInfo*>::iterator NVI = NewVNs.begin(),
674 NVE = NewVNs.end(); NVI != NVE; ++NVI)
675 if (NVI->second == OldVN)
676 NVI->second = MergedVN;
677 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator PI = Phis.begin(),
678 PE = Phis.end(); PI != PE; ++PI)
679 if (PI->second == OldVN)
680 PI->second = MergedVN;
681 RetVNI = MergedVN;
Evan Cheng19a72582009-02-02 18:33:18 +0000682 } else {
683 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
684 // VNInfo to represent the joined value.
685 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
686 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
Lang Hames857c4e02009-06-17 21:01:20 +0000687 I->second->setHasPHIKill(true);
Lang Hames86511252009-09-04 20:41:11 +0000688 MachineInstrIndex KillIndex = LIs->getMBBEndIdx(I->first);
689 if (!I->second->isKill(KillIndex))
690 I->second->addKill(KillIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000691 }
692 }
693
Lang Hames86511252009-09-04 20:41:11 +0000694 MachineInstrIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000695 if (IsIntraBlock) {
696 EndIndex = LIs->getInstructionIndex(UseI);
697 EndIndex = LiveIntervals::getUseIndex(EndIndex);
698 } else
699 EndIndex = LIs->getMBBEndIdx(MBB);
Lang Hames86511252009-09-04 20:41:11 +0000700 LI->addRange(LiveRange(StartIndex, LIs->getNextSlot(EndIndex), RetVNI));
Evan Cheng19a72582009-02-02 18:33:18 +0000701 if (IsIntraBlock)
Lang Hames86511252009-09-04 20:41:11 +0000702 RetVNI->addKill(EndIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000703
704 // Memoize results so we don't have to recompute them.
705 if (!IsIntraBlock)
706 LiveOut[MBB] = RetVNI;
707 else {
708 if (!NewVNs.count(UseI))
709 NewVNs[UseI] = RetVNI;
710 Visited.insert(UseI);
711 }
712
713 return RetVNI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000714}
715
716/// ReconstructLiveInterval - Recompute a live interval from scratch.
717void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
718 BumpPtrAllocator& Alloc = LIs->getVNInfoAllocator();
719
720 // Clear the old ranges and valnos;
721 LI->clear();
722
723 // Cache the uses and defs of the register
724 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
725 RegMap Defs, Uses;
726
727 // Keep track of the new VNs we're creating.
728 DenseMap<MachineInstr*, VNInfo*> NewVNs;
729 SmallPtrSet<VNInfo*, 2> PhiVNs;
730
731 // Cache defs, and create a new VNInfo for each def.
732 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
733 DE = MRI->def_end(); DI != DE; ++DI) {
734 Defs[(*DI).getParent()].insert(&*DI);
735
Lang Hames86511252009-09-04 20:41:11 +0000736 MachineInstrIndex DefIdx = LIs->getInstructionIndex(&*DI);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000737 DefIdx = LiveIntervals::getDefIndex(DefIdx);
738
Lang Hames857c4e02009-06-17 21:01:20 +0000739 assert(DI->getOpcode() != TargetInstrInfo::PHI &&
740 "Following NewVN isPHIDef flag incorrect. Fix me!");
741 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, true, Alloc);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000742
743 // If the def is a move, set the copy field.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000744 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
745 if (TII->isMoveInstr(*DI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
746 if (DstReg == LI->reg)
Lang Hames52c1afc2009-08-10 23:43:28 +0000747 NewVN->setCopy(&*DI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000748
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000749 NewVNs[&*DI] = NewVN;
750 }
751
752 // Cache uses as a separate pass from actually processing them.
753 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
754 UE = MRI->use_end(); UI != UE; ++UI)
755 Uses[(*UI).getParent()].insert(&*UI);
756
757 // Now, actually process every use and use a phi construction algorithm
758 // to walk from it to its reaching definitions, building VNInfos along
759 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000760 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
761 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000762 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000763 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
764 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000765 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000766 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000767 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000768
769 // Add ranges for dead defs
770 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
771 DE = MRI->def_end(); DI != DE; ++DI) {
Lang Hames86511252009-09-04 20:41:11 +0000772 MachineInstrIndex DefIdx = LIs->getInstructionIndex(&*DI);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000773 DefIdx = LiveIntervals::getDefIndex(DefIdx);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000774
775 if (LI->liveAt(DefIdx)) continue;
776
777 VNInfo* DeadVN = NewVNs[&*DI];
Lang Hames86511252009-09-04 20:41:11 +0000778 LI->addRange(LiveRange(DefIdx, LIs->getNextSlot(DefIdx), DeadVN));
779 DeadVN->addKill(DefIdx);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000780 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000781}
782
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000783/// RenumberValno - Split the given valno out into a new vreg, allowing it to
784/// be allocated to a different register. This function creates a new vreg,
785/// copies the valno and its live ranges over to the new vreg's interval,
786/// removes them from the old interval, and rewrites all uses and defs of
787/// the original reg to the new vreg within those ranges.
788void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000789 SmallVector<VNInfo*, 4> Stack;
790 SmallVector<VNInfo*, 4> VNsToCopy;
791 Stack.push_back(VN);
792
793 // Walk through and copy the valno we care about, and any other valnos
794 // that are two-address redefinitions of the one we care about. These
795 // will need to be rewritten as well. We also check for safety of the
796 // renumbering here, by making sure that none of the valno involved has
797 // phi kills.
798 while (!Stack.empty()) {
799 VNInfo* OldVN = Stack.back();
800 Stack.pop_back();
801
802 // Bail out if we ever encounter a valno that has a PHI kill. We can't
803 // renumber these.
Lang Hames857c4e02009-06-17 21:01:20 +0000804 if (OldVN->hasPHIKill()) return;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000805
806 VNsToCopy.push_back(OldVN);
807
808 // Locate two-address redefinitions
Lang Hamesffd13262009-07-09 03:57:02 +0000809 for (VNInfo::KillSet::iterator KI = OldVN->kills.begin(),
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000810 KE = OldVN->kills.end(); KI != KE; ++KI) {
Lang Hames86511252009-09-04 20:41:11 +0000811 assert(!KI->isPHIIndex() &&
812 "VN previously reported having no PHI kills.");
813 MachineInstr* MI = LIs->getInstructionFromIndex(*KI);
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000814 unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg);
815 if (DefIdx == ~0U) continue;
Bob Wilsond9df5012009-04-09 17:16:43 +0000816 if (MI->isRegTiedToUseOperand(DefIdx)) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000817 VNInfo* NextVN =
Lang Hames86511252009-09-04 20:41:11 +0000818 CurrLI->findDefinedVNInfoForRegInt(
819 LiveIntervals::getDefIndex(*KI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000820 if (NextVN == OldVN) continue;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000821 Stack.push_back(NextVN);
822 }
823 }
824 }
825
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000826 // Create the new vreg
827 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
828
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000829 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000830 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000831
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000832 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
833 VNsToCopy.end(); OI != OE; ++OI) {
834 VNInfo* OldVN = *OI;
835
836 // Copy the valno over
Lang Hames857c4e02009-06-17 21:01:20 +0000837 VNInfo* NewVN = NewLI.createValueCopy(OldVN, LIs->getVNInfoAllocator());
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000838 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
839
840 // Remove the valno from the old interval
841 CurrLI->removeValNo(OldVN);
842 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000843
844 // Rewrite defs and uses. This is done in two stages to avoid invalidating
845 // the reg_iterator.
846 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
847
848 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
849 E = MRI->reg_end(); I != E; ++I) {
850 MachineOperand& MO = I.getOperand();
Lang Hames86511252009-09-04 20:41:11 +0000851 MachineInstrIndex InstrIdx = LIs->getInstructionIndex(&*I);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000852
853 if ((MO.isUse() && NewLI.liveAt(LiveIntervals::getUseIndex(InstrIdx))) ||
854 (MO.isDef() && NewLI.liveAt(LiveIntervals::getDefIndex(InstrIdx))))
855 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
856 }
857
858 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
859 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
860 MachineInstr* Inst = I->first;
861 unsigned OpIdx = I->second;
862 MachineOperand& MO = Inst->getOperand(OpIdx);
863 MO.setReg(NewVReg);
864 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000865
Owen Anderson420dd372009-03-14 21:40:05 +0000866 // Grow the VirtRegMap, since we've created a new vreg.
867 VRM->grow();
868
Owen Anderson45e68552009-01-29 05:28:55 +0000869 // The renumbered vreg shares a stack slot with the old register.
870 if (IntervalSSMap.count(CurrLI->reg))
871 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
872
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000873 NumRenumbers++;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000874}
875
Evan Cheng37844532009-07-16 09:20:10 +0000876bool PreAllocSplitting::Rematerialize(unsigned VReg, VNInfo* ValNo,
Owen Anderson6002e992008-12-04 21:20:30 +0000877 MachineInstr* DefMI,
878 MachineBasicBlock::iterator RestorePt,
Lang Hames86511252009-09-04 20:41:11 +0000879 MachineInstrIndex RestoreIdx,
Owen Anderson6002e992008-12-04 21:20:30 +0000880 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
881 MachineBasicBlock& MBB = *RestorePt->getParent();
882
883 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
Lang Hames86511252009-09-04 20:41:11 +0000884 MachineInstrIndex KillIdx;
Lang Hames857c4e02009-06-17 21:01:20 +0000885 if (!ValNo->isDefAccurate() || DefMI->getParent() == BarrierMBB)
Owen Anderson6002e992008-12-04 21:20:30 +0000886 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx);
887 else
888 KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx);
889
890 if (KillPt == DefMI->getParent()->end())
891 return false;
892
Evan Cheng37844532009-07-16 09:20:10 +0000893 TII->reMaterialize(MBB, RestorePt, VReg, 0, DefMI);
Owen Anderson6002e992008-12-04 21:20:30 +0000894 LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx);
895
Owen Andersonb4b84362009-01-26 21:57:31 +0000896 ReconstructLiveInterval(CurrLI);
Lang Hames86511252009-09-04 20:41:11 +0000897 MachineInstrIndex RematIdx = LIs->getInstructionIndex(prior(RestorePt));
Owen Andersonb4b84362009-01-26 21:57:31 +0000898 RematIdx = LiveIntervals::getDefIndex(RematIdx);
Lang Hames86511252009-09-04 20:41:11 +0000899 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000900
Owen Anderson75fa96b2008-11-19 04:28:29 +0000901 ++NumSplits;
902 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000903 return true;
904}
905
906MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
907 const TargetRegisterClass* RC,
908 MachineInstr* DefMI,
909 MachineInstr* Barrier,
910 MachineBasicBlock* MBB,
911 int& SS,
912 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
913 MachineBasicBlock::iterator Pt = MBB->begin();
914
915 // Go top down if RefsInMBB is empty.
916 if (RefsInMBB.empty())
917 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000918
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000919 MachineBasicBlock::iterator FoldPt = Barrier;
920 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
921 !RefsInMBB.count(FoldPt))
922 --FoldPt;
923
924 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
925 if (OpIdx == -1)
926 return 0;
927
928 SmallVector<unsigned, 1> Ops;
929 Ops.push_back(OpIdx);
930
931 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
932 return 0;
933
934 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
935 if (I != IntervalSSMap.end()) {
936 SS = I->second;
937 } else {
Evan Chengc781a242009-05-03 18:32:42 +0000938 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000939 }
940
941 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
942 FoldPt, Ops, SS);
943
944 if (FMI) {
945 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
946 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
947 ++NumFolds;
948
949 IntervalSSMap[vreg] = SS;
Evan Chengc781a242009-05-03 18:32:42 +0000950 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000951 if (CurrSLI->hasAtLeastOneValue())
952 CurrSValNo = CurrSLI->getValNumInfo(0);
953 else
Lang Hames86511252009-09-04 20:41:11 +0000954 CurrSValNo = CurrSLI->getNextValue(MachineInstrIndex(), 0, false,
955 LSs->getVNInfoAllocator());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000956 }
957
958 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000959}
960
Owen Andersonc93023a2009-03-04 08:52:31 +0000961MachineInstr* PreAllocSplitting::FoldRestore(unsigned vreg,
962 const TargetRegisterClass* RC,
963 MachineInstr* Barrier,
964 MachineBasicBlock* MBB,
965 int SS,
966 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Andersona2bfb542009-03-05 08:23:20 +0000967 if ((int)RestoreFoldLimit != -1 && RestoreFoldLimit == (int)NumRestoreFolds)
Owen Anderson323c58d2009-03-05 07:19:18 +0000968 return 0;
969
Owen Andersonc93023a2009-03-04 08:52:31 +0000970 // Go top down if RefsInMBB is empty.
971 if (RefsInMBB.empty())
972 return 0;
973
974 // Can't fold a restore between a call stack setup and teardown.
975 MachineBasicBlock::iterator FoldPt = Barrier;
Owen Anderson323c58d2009-03-05 07:19:18 +0000976
977 // Advance from barrier to call frame teardown.
978 while (FoldPt != MBB->getFirstTerminator() &&
979 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
980 if (RefsInMBB.count(FoldPt))
981 return 0;
Owen Andersonc93023a2009-03-04 08:52:31 +0000982
Owen Anderson323c58d2009-03-05 07:19:18 +0000983 ++FoldPt;
984 }
985
986 if (FoldPt == MBB->getFirstTerminator())
987 return 0;
988 else
989 ++FoldPt;
990
991 // Now find the restore point.
992 while (FoldPt != MBB->getFirstTerminator() && !RefsInMBB.count(FoldPt)) {
Owen Andersonc93023a2009-03-04 08:52:31 +0000993 if (FoldPt->getOpcode() == TRI->getCallFrameSetupOpcode()) {
994 while (FoldPt != MBB->getFirstTerminator() &&
995 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
996 if (RefsInMBB.count(FoldPt))
997 return 0;
998
999 ++FoldPt;
1000 }
1001
Owen Anderson323c58d2009-03-05 07:19:18 +00001002 if (FoldPt == MBB->getFirstTerminator())
1003 return 0;
1004 }
1005
1006 ++FoldPt;
Owen Andersonc93023a2009-03-04 08:52:31 +00001007 }
1008
1009 if (FoldPt == MBB->getFirstTerminator())
1010 return 0;
1011
1012 int OpIdx = FoldPt->findRegisterUseOperandIdx(vreg, true);
1013 if (OpIdx == -1)
1014 return 0;
1015
1016 SmallVector<unsigned, 1> Ops;
1017 Ops.push_back(OpIdx);
1018
1019 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
1020 return 0;
1021
1022 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
1023 FoldPt, Ops, SS);
1024
1025 if (FMI) {
1026 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
1027 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
Owen Anderson323c58d2009-03-05 07:19:18 +00001028 ++NumRestoreFolds;
Owen Andersonc93023a2009-03-04 08:52:31 +00001029 }
1030
1031 return FMI;
1032}
1033
Evan Chengf5cd4f02008-10-23 20:43:13 +00001034/// SplitRegLiveInterval - Split (spill and restore) the given live interval
1035/// so it would not cross the barrier that's being processed. Shrink wrap
1036/// (minimize) the live interval to the last uses.
1037bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
1038 CurrLI = LI;
1039
1040 // Find live range where current interval cross the barrier.
1041 LiveInterval::iterator LR =
1042 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
1043 VNInfo *ValNo = LR->valno;
1044
Torok Edwinf3689232009-07-12 20:07:01 +00001045 assert(!ValNo->isUnused() && "Val# is defined by a dead def?");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001046
Lang Hames857c4e02009-06-17 21:01:20 +00001047 MachineInstr *DefMI = ValNo->isDefAccurate()
Evan Cheng06587492008-10-24 02:05:00 +00001048 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +00001049
Owen Andersond3be4622009-01-21 08:18:03 +00001050 // If this would create a new join point, do not split.
1051 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent()))
1052 return false;
1053
Evan Chengf5cd4f02008-10-23 20:43:13 +00001054 // Find all references in the barrier mbb.
1055 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
1056 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
1057 E = MRI->reg_end(); I != E; ++I) {
1058 MachineInstr *RefMI = &*I;
1059 if (RefMI->getParent() == BarrierMBB)
1060 RefsInMBB.insert(RefMI);
1061 }
1062
1063 // Find a point to restore the value after the barrier.
Lang Hames86511252009-09-04 20:41:11 +00001064 MachineInstrIndex RestoreIndex;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001065 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +00001066 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +00001067 if (RestorePt == BarrierMBB->end())
1068 return false;
1069
Owen Anderson75fa96b2008-11-19 04:28:29 +00001070 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
1071 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt,
1072 RestoreIndex, RefsInMBB))
1073 return true;
1074
Evan Chengf5cd4f02008-10-23 20:43:13 +00001075 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001076 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001077 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Lang Hames86511252009-09-04 20:41:11 +00001078 MachineInstrIndex SpillIndex;
Evan Cheng06587492008-10-24 02:05:00 +00001079 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001080 int SS = -1;
Lang Hames857c4e02009-06-17 21:01:20 +00001081 if (!ValNo->isDefAccurate()) {
1082 // If we don't know where the def is we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001083 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1084 BarrierMBB, SS, RefsInMBB))) {
1085 SpillIndex = LIs->getInstructionIndex(SpillMI);
1086 } else {
1087 MachineBasicBlock::iterator SpillPt =
1088 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
1089 if (SpillPt == BarrierMBB->begin())
1090 return false; // No gap to insert spill.
1091 // Add spill.
1092
1093 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1094 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
1095 SpillMI = prior(SpillPt);
1096 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
1097 }
Evan Cheng54898932008-10-29 08:39:34 +00001098 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
1099 RestoreIndex, SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001100 // If it's already split, just restore the value. There is no need to spill
1101 // the def again.
Evan Chengd0e32c52008-10-29 05:06:14 +00001102 if (!DefMI)
1103 return false; // Def is dead. Do nothing.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001104
1105 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
1106 BarrierMBB, SS, RefsInMBB))) {
1107 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001108 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001109 // Check if it's possible to insert a spill after the def MI.
1110 MachineBasicBlock::iterator SpillPt;
1111 if (DefMBB == BarrierMBB) {
1112 // Add spill after the def and the last use before the barrier.
1113 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
1114 RefsInMBB, SpillIndex);
1115 if (SpillPt == DefMBB->begin())
1116 return false; // No gap to insert spill.
1117 } else {
1118 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
1119 if (SpillPt == DefMBB->end())
1120 return false; // No gap to insert spill.
1121 }
1122 // Add spill. The store instruction kills the register if def is before
1123 // the barrier in the barrier block.
1124 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1125 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
1126 DefMBB == BarrierMBB, SS, RC);
1127 SpillMI = prior(SpillPt);
1128 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001129 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001130 }
1131
Evan Cheng54898932008-10-29 08:39:34 +00001132 // Remember def instruction index to spill index mapping.
1133 if (DefMI && SpillMI)
1134 Def2SpillMap[ValNo->def] = SpillIndex;
1135
Evan Chengf5cd4f02008-10-23 20:43:13 +00001136 // Add restore.
Owen Andersonc93023a2009-03-04 08:52:31 +00001137 bool FoldedRestore = false;
1138 if (MachineInstr* LMI = FoldRestore(CurrLI->reg, RC, Barrier,
1139 BarrierMBB, SS, RefsInMBB)) {
1140 RestorePt = LMI;
Owen Anderson323c58d2009-03-05 07:19:18 +00001141 RestoreIndex = LIs->getInstructionIndex(RestorePt);
Owen Andersonc93023a2009-03-04 08:52:31 +00001142 FoldedRestore = true;
1143 } else {
1144 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
1145 MachineInstr *LoadMI = prior(RestorePt);
1146 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
1147 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001148
Evan Chengd0e32c52008-10-29 05:06:14 +00001149 // Update spill stack slot live interval.
Lang Hames86511252009-09-04 20:41:11 +00001150 UpdateSpillSlotInterval(ValNo, LIs->getNextSlot(LIs->getUseIndex(SpillIndex)),
Evan Cheng54898932008-10-29 08:39:34 +00001151 LIs->getDefIndex(RestoreIndex));
Evan Chengd0e32c52008-10-29 05:06:14 +00001152
Owen Andersonb4b84362009-01-26 21:57:31 +00001153 ReconstructLiveInterval(CurrLI);
Owen Andersonc93023a2009-03-04 08:52:31 +00001154
1155 if (!FoldedRestore) {
Lang Hames86511252009-09-04 20:41:11 +00001156 MachineInstrIndex RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
Owen Andersonc93023a2009-03-04 08:52:31 +00001157 RestoreIdx = LiveIntervals::getDefIndex(RestoreIdx);
Lang Hames86511252009-09-04 20:41:11 +00001158 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RestoreIdx));
Owen Andersonc93023a2009-03-04 08:52:31 +00001159 }
Owen Anderson7d211e22008-12-31 02:00:25 +00001160
Evan Chengae7fa5b2008-10-28 01:48:24 +00001161 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001162 return true;
1163}
1164
1165/// SplitRegLiveIntervals - Split all register live intervals that cross the
1166/// barrier that's being processed.
1167bool
Owen Anderson956ec272009-01-23 00:23:32 +00001168PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1169 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001170 // First find all the virtual registers whose live intervals are intercepted
1171 // by the current barrier.
1172 SmallVector<LiveInterval*, 8> Intervals;
1173 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng4350eb82009-02-06 17:17:30 +00001174 // FIXME: If it's not safe to move any instruction that defines the barrier
1175 // register class, then it means there are some special dependencies which
1176 // codegen is not modelling. Ignore these barriers for now.
1177 if (!TII->isSafeToMoveRegClassDefs(*RC))
Evan Cheng23066282008-10-27 07:14:50 +00001178 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001179 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
1180 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1181 unsigned Reg = VRs[i];
1182 if (!LIs->hasInterval(Reg))
1183 continue;
1184 LiveInterval *LI = &LIs->getInterval(Reg);
1185 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1186 // Virtual register live interval is intercepted by the barrier. We
1187 // should split and shrink wrap its interval if possible.
1188 Intervals.push_back(LI);
1189 }
1190 }
1191
1192 // Process the affected live intervals.
1193 bool Change = false;
1194 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001195 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1196 break;
Owen Andersone1762c92009-01-12 03:10:40 +00001197 else if (NumSplits == 4)
1198 Change |= Change;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001199 LiveInterval *LI = Intervals.back();
1200 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001201 bool result = SplitRegLiveInterval(LI);
1202 if (result) Split.insert(LI);
1203 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001204 }
1205
1206 return Change;
1207}
1208
Owen Anderson45e68552009-01-29 05:28:55 +00001209unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001210 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001211 unsigned Reg, int FrameIndex,
1212 bool& FeedsTwoAddr) {
1213 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001214 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001215 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001216 int StoreFrameIndex;
1217 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001218 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
1219 NonSpills++;
1220
1221 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
Bob Wilsond9df5012009-04-09 17:16:43 +00001222 if (DefIdx != -1 && (*UI)->isRegTiedToUseOperand(DefIdx))
Owen Anderson45e68552009-01-29 05:28:55 +00001223 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001224 }
1225
Owen Anderson45e68552009-01-29 05:28:55 +00001226 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001227}
1228
Owen Anderson956ec272009-01-23 00:23:32 +00001229/// removeDeadSpills - After doing splitting, filter through all intervals we've
1230/// split, and see if any of the spills are unnecessary. If so, remove them.
1231bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1232 bool changed = false;
1233
Owen Anderson4bfc2092009-01-29 05:41:02 +00001234 // Walk over all of the live intervals that were touched by the splitter,
1235 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001236 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1237 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001238 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001239
Owen Anderson4bfc2092009-01-29 05:41:02 +00001240 // First, collect all the uses of the vreg, and sort them by their
1241 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001242 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1243 UE = MRI->use_end(); UI != UE; ++UI) {
Lang Hames86511252009-09-04 20:41:11 +00001244 MachineInstrIndex index = LIs->getInstructionIndex(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001245 index = LiveIntervals::getUseIndex(index);
1246
1247 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001248 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001249 }
1250
Owen Anderson4bfc2092009-01-29 05:41:02 +00001251 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1252 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001253 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1254 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001255
1256 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1257 return changed;
1258
Owen Anderson956ec272009-01-23 00:23:32 +00001259 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001260
1261 // We don't currently try to handle definitions with PHI kills, because
1262 // it would involve processing more than one VNInfo at once.
Lang Hames857c4e02009-06-17 21:01:20 +00001263 if (CurrVN->hasPHIKill()) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001264
Owen Anderson4bfc2092009-01-29 05:41:02 +00001265 // We also don't try to handle the results of PHI joins, since there's
1266 // no defining instruction to analyze.
Lang Hames857c4e02009-06-17 21:01:20 +00001267 if (!CurrVN->isDefAccurate() || CurrVN->isUnused()) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001268
Owen Anderson4bfc2092009-01-29 05:41:02 +00001269 // We're only interested in eliminating cruft introduced by the splitter,
1270 // is of the form load-use or load-use-store. First, check that the
1271 // definition is a load, and remember what stack slot we loaded it from.
Lang Hames857c4e02009-06-17 21:01:20 +00001272 MachineInstr* DefMI = LIs->getInstructionFromIndex(CurrVN->def);
Owen Anderson956ec272009-01-23 00:23:32 +00001273 int FrameIndex;
1274 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1275
Owen Anderson4bfc2092009-01-29 05:41:02 +00001276 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001277 if (VNUseCount[CurrVN].size() == 0) {
1278 LIs->RemoveMachineInstrFromMaps(DefMI);
1279 (*LI)->removeValNo(CurrVN);
1280 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001281 VNUseCount.erase(CurrVN);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001282 NumDeadSpills++;
1283 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001284 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001285 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001286
Owen Anderson4bfc2092009-01-29 05:41:02 +00001287 // Second, get the number of non-store uses of the definition, as well as
1288 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001289 bool FeedsTwoAddr = false;
1290 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1291 (*LI)->reg, FrameIndex,
1292 FeedsTwoAddr);
1293
Owen Anderson4bfc2092009-01-29 05:41:02 +00001294 // If there's one non-store use and it doesn't feed a two-addr, then
1295 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001296 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001297 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001298 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1299 int StoreFrameIndex;
1300 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1301 while (UI != VNUseCount[CurrVN].end() &&
1302 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1303 ++UI;
1304 if (UI != VNUseCount[CurrVN].end())
1305 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1306 }
Owen Anderson45e68552009-01-29 05:28:55 +00001307 if (UI == VNUseCount[CurrVN].end()) continue;
1308
1309 MachineInstr* use = *UI;
1310
Owen Anderson4bfc2092009-01-29 05:41:02 +00001311 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001312 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1313 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001314 SmallVector<unsigned, 1> Ops;
1315 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001316 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1317
1318 MachineInstr* NewMI =
1319 TII->foldMemoryOperand(*use->getParent()->getParent(),
1320 use, Ops, FrameIndex);
1321
1322 if (!NewMI) continue;
1323
Owen Anderson4bfc2092009-01-29 05:41:02 +00001324 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001325 LIs->RemoveMachineInstrFromMaps(DefMI);
1326 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1327 (*LI)->removeValNo(CurrVN);
1328
1329 DefMI->eraseFromParent();
1330 MachineBasicBlock* MBB = use->getParent();
1331 NewMI = MBB->insert(MBB->erase(use), NewMI);
1332 VNUseCount[CurrVN].erase(use);
1333
Owen Anderson4bfc2092009-01-29 05:41:02 +00001334 // Remove deleted instructions. Note that we need to remove them from
1335 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001336 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1337 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1338 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001339 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001340 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1341 ++VNI)
1342 if (VNI->first != CurrVN)
1343 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001344 LIs->RemoveMachineInstrFromMaps(*II);
1345 (*II)->eraseFromParent();
1346 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001347
1348 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001349
1350 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1351 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1352 if (VI->second.erase(use))
1353 VI->second.insert(NewMI);
1354
1355 NumDeadSpills++;
1356 changed = true;
1357 continue;
1358 }
1359
Owen Anderson4bfc2092009-01-29 05:41:02 +00001360 // If there's more than one non-store instruction, we can't profitably
1361 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001362 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001363
Owen Anderson4bfc2092009-01-29 05:41:02 +00001364 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001365 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1366 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
1367 UI != UI; ++UI) {
1368 LIs->RemoveMachineInstrFromMaps(*UI);
1369 (*UI)->eraseFromParent();
1370 }
1371
Owen Andersonc0f3a032009-01-29 08:22:06 +00001372 VNUseCount.erase(CurrVN);
1373
Owen Anderson32ca8652009-01-24 10:07:43 +00001374 LIs->RemoveMachineInstrFromMaps(DefMI);
1375 (*LI)->removeValNo(CurrVN);
1376 DefMI->eraseFromParent();
1377 NumDeadSpills++;
1378 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001379 }
1380 }
1381
1382 return changed;
1383}
1384
Owen Andersonf1f75b12008-11-04 22:22:41 +00001385bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1386 MachineBasicBlock* DefMBB,
1387 MachineBasicBlock* BarrierMBB) {
1388 if (DefMBB == BarrierMBB)
1389 return false;
1390
Lang Hames857c4e02009-06-17 21:01:20 +00001391 if (LR->valno->hasPHIKill())
Owen Andersonf1f75b12008-11-04 22:22:41 +00001392 return false;
1393
Lang Hames86511252009-09-04 20:41:11 +00001394 MachineInstrIndex MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
Owen Andersonf1f75b12008-11-04 22:22:41 +00001395 if (LR->end < MBBEnd)
1396 return false;
1397
1398 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1399 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1400 return true;
1401
1402 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1403 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1404 typedef std::pair<MachineBasicBlock*,
1405 MachineBasicBlock::succ_iterator> ItPair;
1406 SmallVector<ItPair, 4> Stack;
1407 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1408
1409 while (!Stack.empty()) {
1410 ItPair P = Stack.back();
1411 Stack.pop_back();
1412
1413 MachineBasicBlock* PredMBB = P.first;
1414 MachineBasicBlock::succ_iterator S = P.second;
1415
1416 if (S == PredMBB->succ_end())
1417 continue;
1418 else if (Visited.count(*S)) {
1419 Stack.push_back(std::make_pair(PredMBB, ++S));
1420 continue;
1421 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001422 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001423
1424 MachineBasicBlock* MBB = *S;
1425 Visited.insert(MBB);
1426
1427 if (MBB == BarrierMBB)
1428 return true;
1429
1430 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1431 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1432 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1433 while (MDTN) {
1434 if (MDTN == DefMDTN)
1435 return true;
1436 else if (MDTN == BarrierMDTN)
1437 break;
1438 MDTN = MDTN->getIDom();
1439 }
1440
1441 MBBEnd = LIs->getMBBEndIdx(MBB);
1442 if (LR->end > MBBEnd)
1443 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1444 }
1445
1446 return false;
1447}
1448
1449
Evan Cheng09e8ca82008-10-20 21:44:59 +00001450bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001451 CurrMF = &MF;
1452 TM = &MF.getTarget();
Owen Anderson3ef45492009-01-29 22:13:06 +00001453 TRI = TM->getRegisterInfo();
Evan Chengd0e32c52008-10-29 05:06:14 +00001454 TII = TM->getInstrInfo();
1455 MFI = MF.getFrameInfo();
1456 MRI = &MF.getRegInfo();
1457 LIs = &getAnalysis<LiveIntervals>();
1458 LSs = &getAnalysis<LiveStacks>();
Owen Anderson420dd372009-03-14 21:40:05 +00001459 VRM = &getAnalysis<VirtRegMap>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001460
1461 bool MadeChange = false;
1462
1463 // Make sure blocks are numbered in order.
1464 MF.RenumberBlocks();
1465
Evan Cheng54898932008-10-29 08:39:34 +00001466 MachineBasicBlock *Entry = MF.begin();
1467 SmallPtrSet<MachineBasicBlock*,16> Visited;
1468
Owen Anderson956ec272009-01-23 00:23:32 +00001469 SmallPtrSet<LiveInterval*, 8> Split;
1470
Evan Cheng54898932008-10-29 08:39:34 +00001471 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1472 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1473 DFI != E; ++DFI) {
1474 BarrierMBB = *DFI;
1475 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1476 E = BarrierMBB->end(); I != E; ++I) {
1477 Barrier = &*I;
1478 const TargetRegisterClass **BarrierRCs =
1479 Barrier->getDesc().getRegClassBarriers();
1480 if (!BarrierRCs)
1481 continue;
1482 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001483 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001484 }
1485 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001486
Owen Anderson956ec272009-01-23 00:23:32 +00001487 MadeChange |= removeDeadSpills(Split);
1488
Evan Chengf5cd4f02008-10-23 20:43:13 +00001489 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001490}