blob: 2e20dc1f735121ecdadb7b653ed10a674ef5c600 [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);
Lang Hames96479942009-09-09 20:14:17 +0000495 DefIndex = LIs->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);
Lang Hames96479942009-09-09 20:14:17 +0000532 UseIndex = LIs->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);
Lang Hames96479942009-09-09 20:14:17 +0000536 EndIndex = LIs->getUseIndex(EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000537 } 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);
Lang Hames96479942009-09-09 20:14:17 +0000592 StartIndex = foundDef ? LIs->getDefIndex(StartIndex) :
593 LIs->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);
Lang Hames96479942009-09-09 20:14:17 +0000597 EndIndex = LIs->getUseIndex(EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000598 } 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);
Lang Hames96479942009-09-09 20:14:17 +0000697 EndIndex = LIs->getUseIndex(EndIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000698 } 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);
Lang Hames96479942009-09-09 20:14:17 +0000737 DefIdx = LIs->getDefIndex(DefIdx);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000738
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);
Lang Hames96479942009-09-09 20:14:17 +0000773 DefIdx = LIs->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 Hames96479942009-09-09 20:14:17 +0000818 CurrLI->findDefinedVNInfoForRegInt(LIs->getDefIndex(*KI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000819 if (NextVN == OldVN) continue;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000820 Stack.push_back(NextVN);
821 }
822 }
823 }
824
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000825 // Create the new vreg
826 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
827
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000828 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000829 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000830
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000831 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
832 VNsToCopy.end(); OI != OE; ++OI) {
833 VNInfo* OldVN = *OI;
834
835 // Copy the valno over
Lang Hames857c4e02009-06-17 21:01:20 +0000836 VNInfo* NewVN = NewLI.createValueCopy(OldVN, LIs->getVNInfoAllocator());
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000837 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
838
839 // Remove the valno from the old interval
840 CurrLI->removeValNo(OldVN);
841 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000842
843 // Rewrite defs and uses. This is done in two stages to avoid invalidating
844 // the reg_iterator.
845 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
846
847 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
848 E = MRI->reg_end(); I != E; ++I) {
849 MachineOperand& MO = I.getOperand();
Lang Hames86511252009-09-04 20:41:11 +0000850 MachineInstrIndex InstrIdx = LIs->getInstructionIndex(&*I);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000851
Lang Hames96479942009-09-09 20:14:17 +0000852 if ((MO.isUse() && NewLI.liveAt(LIs->getUseIndex(InstrIdx))) ||
853 (MO.isDef() && NewLI.liveAt(LIs->getDefIndex(InstrIdx))))
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000854 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
855 }
856
857 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
858 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
859 MachineInstr* Inst = I->first;
860 unsigned OpIdx = I->second;
861 MachineOperand& MO = Inst->getOperand(OpIdx);
862 MO.setReg(NewVReg);
863 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000864
Owen Anderson420dd372009-03-14 21:40:05 +0000865 // Grow the VirtRegMap, since we've created a new vreg.
866 VRM->grow();
867
Owen Anderson45e68552009-01-29 05:28:55 +0000868 // The renumbered vreg shares a stack slot with the old register.
869 if (IntervalSSMap.count(CurrLI->reg))
870 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
871
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000872 NumRenumbers++;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000873}
874
Evan Cheng37844532009-07-16 09:20:10 +0000875bool PreAllocSplitting::Rematerialize(unsigned VReg, VNInfo* ValNo,
Owen Anderson6002e992008-12-04 21:20:30 +0000876 MachineInstr* DefMI,
877 MachineBasicBlock::iterator RestorePt,
Lang Hames86511252009-09-04 20:41:11 +0000878 MachineInstrIndex RestoreIdx,
Owen Anderson6002e992008-12-04 21:20:30 +0000879 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
880 MachineBasicBlock& MBB = *RestorePt->getParent();
881
882 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
Lang Hames86511252009-09-04 20:41:11 +0000883 MachineInstrIndex KillIdx;
Lang Hames857c4e02009-06-17 21:01:20 +0000884 if (!ValNo->isDefAccurate() || DefMI->getParent() == BarrierMBB)
Owen Anderson6002e992008-12-04 21:20:30 +0000885 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx);
886 else
887 KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx);
888
889 if (KillPt == DefMI->getParent()->end())
890 return false;
891
Evan Cheng37844532009-07-16 09:20:10 +0000892 TII->reMaterialize(MBB, RestorePt, VReg, 0, DefMI);
Owen Anderson6002e992008-12-04 21:20:30 +0000893 LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx);
894
Owen Andersonb4b84362009-01-26 21:57:31 +0000895 ReconstructLiveInterval(CurrLI);
Lang Hames86511252009-09-04 20:41:11 +0000896 MachineInstrIndex RematIdx = LIs->getInstructionIndex(prior(RestorePt));
Lang Hames96479942009-09-09 20:14:17 +0000897 RematIdx = LIs->getDefIndex(RematIdx);
Lang Hames86511252009-09-04 20:41:11 +0000898 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000899
Owen Anderson75fa96b2008-11-19 04:28:29 +0000900 ++NumSplits;
901 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000902 return true;
903}
904
905MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
906 const TargetRegisterClass* RC,
907 MachineInstr* DefMI,
908 MachineInstr* Barrier,
909 MachineBasicBlock* MBB,
910 int& SS,
911 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
912 MachineBasicBlock::iterator Pt = MBB->begin();
913
914 // Go top down if RefsInMBB is empty.
915 if (RefsInMBB.empty())
916 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000917
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000918 MachineBasicBlock::iterator FoldPt = Barrier;
919 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
920 !RefsInMBB.count(FoldPt))
921 --FoldPt;
922
923 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
924 if (OpIdx == -1)
925 return 0;
926
927 SmallVector<unsigned, 1> Ops;
928 Ops.push_back(OpIdx);
929
930 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
931 return 0;
932
933 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
934 if (I != IntervalSSMap.end()) {
935 SS = I->second;
936 } else {
Evan Chengc781a242009-05-03 18:32:42 +0000937 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000938 }
939
940 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
941 FoldPt, Ops, SS);
942
943 if (FMI) {
944 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
945 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
946 ++NumFolds;
947
948 IntervalSSMap[vreg] = SS;
Evan Chengc781a242009-05-03 18:32:42 +0000949 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000950 if (CurrSLI->hasAtLeastOneValue())
951 CurrSValNo = CurrSLI->getValNumInfo(0);
952 else
Lang Hames86511252009-09-04 20:41:11 +0000953 CurrSValNo = CurrSLI->getNextValue(MachineInstrIndex(), 0, false,
954 LSs->getVNInfoAllocator());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000955 }
956
957 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000958}
959
Owen Andersonc93023a2009-03-04 08:52:31 +0000960MachineInstr* PreAllocSplitting::FoldRestore(unsigned vreg,
961 const TargetRegisterClass* RC,
962 MachineInstr* Barrier,
963 MachineBasicBlock* MBB,
964 int SS,
965 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Andersona2bfb542009-03-05 08:23:20 +0000966 if ((int)RestoreFoldLimit != -1 && RestoreFoldLimit == (int)NumRestoreFolds)
Owen Anderson323c58d2009-03-05 07:19:18 +0000967 return 0;
968
Owen Andersonc93023a2009-03-04 08:52:31 +0000969 // Go top down if RefsInMBB is empty.
970 if (RefsInMBB.empty())
971 return 0;
972
973 // Can't fold a restore between a call stack setup and teardown.
974 MachineBasicBlock::iterator FoldPt = Barrier;
Owen Anderson323c58d2009-03-05 07:19:18 +0000975
976 // Advance from barrier to call frame teardown.
977 while (FoldPt != MBB->getFirstTerminator() &&
978 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
979 if (RefsInMBB.count(FoldPt))
980 return 0;
Owen Andersonc93023a2009-03-04 08:52:31 +0000981
Owen Anderson323c58d2009-03-05 07:19:18 +0000982 ++FoldPt;
983 }
984
985 if (FoldPt == MBB->getFirstTerminator())
986 return 0;
987 else
988 ++FoldPt;
989
990 // Now find the restore point.
991 while (FoldPt != MBB->getFirstTerminator() && !RefsInMBB.count(FoldPt)) {
Owen Andersonc93023a2009-03-04 08:52:31 +0000992 if (FoldPt->getOpcode() == TRI->getCallFrameSetupOpcode()) {
993 while (FoldPt != MBB->getFirstTerminator() &&
994 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
995 if (RefsInMBB.count(FoldPt))
996 return 0;
997
998 ++FoldPt;
999 }
1000
Owen Anderson323c58d2009-03-05 07:19:18 +00001001 if (FoldPt == MBB->getFirstTerminator())
1002 return 0;
1003 }
1004
1005 ++FoldPt;
Owen Andersonc93023a2009-03-04 08:52:31 +00001006 }
1007
1008 if (FoldPt == MBB->getFirstTerminator())
1009 return 0;
1010
1011 int OpIdx = FoldPt->findRegisterUseOperandIdx(vreg, true);
1012 if (OpIdx == -1)
1013 return 0;
1014
1015 SmallVector<unsigned, 1> Ops;
1016 Ops.push_back(OpIdx);
1017
1018 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
1019 return 0;
1020
1021 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
1022 FoldPt, Ops, SS);
1023
1024 if (FMI) {
1025 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
1026 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
Owen Anderson323c58d2009-03-05 07:19:18 +00001027 ++NumRestoreFolds;
Owen Andersonc93023a2009-03-04 08:52:31 +00001028 }
1029
1030 return FMI;
1031}
1032
Evan Chengf5cd4f02008-10-23 20:43:13 +00001033/// SplitRegLiveInterval - Split (spill and restore) the given live interval
1034/// so it would not cross the barrier that's being processed. Shrink wrap
1035/// (minimize) the live interval to the last uses.
1036bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
1037 CurrLI = LI;
1038
1039 // Find live range where current interval cross the barrier.
1040 LiveInterval::iterator LR =
1041 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
1042 VNInfo *ValNo = LR->valno;
1043
Torok Edwinf3689232009-07-12 20:07:01 +00001044 assert(!ValNo->isUnused() && "Val# is defined by a dead def?");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001045
Lang Hames857c4e02009-06-17 21:01:20 +00001046 MachineInstr *DefMI = ValNo->isDefAccurate()
Evan Cheng06587492008-10-24 02:05:00 +00001047 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +00001048
Owen Andersond3be4622009-01-21 08:18:03 +00001049 // If this would create a new join point, do not split.
1050 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent()))
1051 return false;
1052
Evan Chengf5cd4f02008-10-23 20:43:13 +00001053 // Find all references in the barrier mbb.
1054 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
1055 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
1056 E = MRI->reg_end(); I != E; ++I) {
1057 MachineInstr *RefMI = &*I;
1058 if (RefMI->getParent() == BarrierMBB)
1059 RefsInMBB.insert(RefMI);
1060 }
1061
1062 // Find a point to restore the value after the barrier.
Lang Hames86511252009-09-04 20:41:11 +00001063 MachineInstrIndex RestoreIndex;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001064 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +00001065 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +00001066 if (RestorePt == BarrierMBB->end())
1067 return false;
1068
Owen Anderson75fa96b2008-11-19 04:28:29 +00001069 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
1070 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt,
1071 RestoreIndex, RefsInMBB))
1072 return true;
1073
Evan Chengf5cd4f02008-10-23 20:43:13 +00001074 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001075 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001076 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Lang Hames86511252009-09-04 20:41:11 +00001077 MachineInstrIndex SpillIndex;
Evan Cheng06587492008-10-24 02:05:00 +00001078 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001079 int SS = -1;
Lang Hames857c4e02009-06-17 21:01:20 +00001080 if (!ValNo->isDefAccurate()) {
1081 // If we don't know where the def is we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001082 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1083 BarrierMBB, SS, RefsInMBB))) {
1084 SpillIndex = LIs->getInstructionIndex(SpillMI);
1085 } else {
1086 MachineBasicBlock::iterator SpillPt =
1087 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
1088 if (SpillPt == BarrierMBB->begin())
1089 return false; // No gap to insert spill.
1090 // Add spill.
1091
1092 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1093 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
1094 SpillMI = prior(SpillPt);
1095 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
1096 }
Evan Cheng54898932008-10-29 08:39:34 +00001097 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
1098 RestoreIndex, SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001099 // If it's already split, just restore the value. There is no need to spill
1100 // the def again.
Evan Chengd0e32c52008-10-29 05:06:14 +00001101 if (!DefMI)
1102 return false; // Def is dead. Do nothing.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001103
1104 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
1105 BarrierMBB, SS, RefsInMBB))) {
1106 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001107 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001108 // Check if it's possible to insert a spill after the def MI.
1109 MachineBasicBlock::iterator SpillPt;
1110 if (DefMBB == BarrierMBB) {
1111 // Add spill after the def and the last use before the barrier.
1112 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
1113 RefsInMBB, SpillIndex);
1114 if (SpillPt == DefMBB->begin())
1115 return false; // No gap to insert spill.
1116 } else {
1117 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
1118 if (SpillPt == DefMBB->end())
1119 return false; // No gap to insert spill.
1120 }
1121 // Add spill. The store instruction kills the register if def is before
1122 // the barrier in the barrier block.
1123 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1124 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
1125 DefMBB == BarrierMBB, SS, RC);
1126 SpillMI = prior(SpillPt);
1127 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001128 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001129 }
1130
Evan Cheng54898932008-10-29 08:39:34 +00001131 // Remember def instruction index to spill index mapping.
1132 if (DefMI && SpillMI)
1133 Def2SpillMap[ValNo->def] = SpillIndex;
1134
Evan Chengf5cd4f02008-10-23 20:43:13 +00001135 // Add restore.
Owen Andersonc93023a2009-03-04 08:52:31 +00001136 bool FoldedRestore = false;
1137 if (MachineInstr* LMI = FoldRestore(CurrLI->reg, RC, Barrier,
1138 BarrierMBB, SS, RefsInMBB)) {
1139 RestorePt = LMI;
Owen Anderson323c58d2009-03-05 07:19:18 +00001140 RestoreIndex = LIs->getInstructionIndex(RestorePt);
Owen Andersonc93023a2009-03-04 08:52:31 +00001141 FoldedRestore = true;
1142 } else {
1143 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
1144 MachineInstr *LoadMI = prior(RestorePt);
1145 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
1146 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001147
Evan Chengd0e32c52008-10-29 05:06:14 +00001148 // Update spill stack slot live interval.
Lang Hames86511252009-09-04 20:41:11 +00001149 UpdateSpillSlotInterval(ValNo, LIs->getNextSlot(LIs->getUseIndex(SpillIndex)),
Evan Cheng54898932008-10-29 08:39:34 +00001150 LIs->getDefIndex(RestoreIndex));
Evan Chengd0e32c52008-10-29 05:06:14 +00001151
Owen Andersonb4b84362009-01-26 21:57:31 +00001152 ReconstructLiveInterval(CurrLI);
Owen Andersonc93023a2009-03-04 08:52:31 +00001153
1154 if (!FoldedRestore) {
Lang Hames86511252009-09-04 20:41:11 +00001155 MachineInstrIndex RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
Lang Hames96479942009-09-09 20:14:17 +00001156 RestoreIdx = LIs->getDefIndex(RestoreIdx);
Lang Hames86511252009-09-04 20:41:11 +00001157 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RestoreIdx));
Owen Andersonc93023a2009-03-04 08:52:31 +00001158 }
Owen Anderson7d211e22008-12-31 02:00:25 +00001159
Evan Chengae7fa5b2008-10-28 01:48:24 +00001160 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001161 return true;
1162}
1163
1164/// SplitRegLiveIntervals - Split all register live intervals that cross the
1165/// barrier that's being processed.
1166bool
Owen Anderson956ec272009-01-23 00:23:32 +00001167PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1168 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001169 // First find all the virtual registers whose live intervals are intercepted
1170 // by the current barrier.
1171 SmallVector<LiveInterval*, 8> Intervals;
1172 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng4350eb82009-02-06 17:17:30 +00001173 // FIXME: If it's not safe to move any instruction that defines the barrier
1174 // register class, then it means there are some special dependencies which
1175 // codegen is not modelling. Ignore these barriers for now.
1176 if (!TII->isSafeToMoveRegClassDefs(*RC))
Evan Cheng23066282008-10-27 07:14:50 +00001177 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001178 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
1179 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1180 unsigned Reg = VRs[i];
1181 if (!LIs->hasInterval(Reg))
1182 continue;
1183 LiveInterval *LI = &LIs->getInterval(Reg);
1184 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1185 // Virtual register live interval is intercepted by the barrier. We
1186 // should split and shrink wrap its interval if possible.
1187 Intervals.push_back(LI);
1188 }
1189 }
1190
1191 // Process the affected live intervals.
1192 bool Change = false;
1193 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001194 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1195 break;
Owen Andersone1762c92009-01-12 03:10:40 +00001196 else if (NumSplits == 4)
1197 Change |= Change;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001198 LiveInterval *LI = Intervals.back();
1199 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001200 bool result = SplitRegLiveInterval(LI);
1201 if (result) Split.insert(LI);
1202 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001203 }
1204
1205 return Change;
1206}
1207
Owen Anderson45e68552009-01-29 05:28:55 +00001208unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001209 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001210 unsigned Reg, int FrameIndex,
1211 bool& FeedsTwoAddr) {
1212 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001213 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001214 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001215 int StoreFrameIndex;
1216 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001217 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
1218 NonSpills++;
1219
1220 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
Bob Wilsond9df5012009-04-09 17:16:43 +00001221 if (DefIdx != -1 && (*UI)->isRegTiedToUseOperand(DefIdx))
Owen Anderson45e68552009-01-29 05:28:55 +00001222 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001223 }
1224
Owen Anderson45e68552009-01-29 05:28:55 +00001225 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001226}
1227
Owen Anderson956ec272009-01-23 00:23:32 +00001228/// removeDeadSpills - After doing splitting, filter through all intervals we've
1229/// split, and see if any of the spills are unnecessary. If so, remove them.
1230bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1231 bool changed = false;
1232
Owen Anderson4bfc2092009-01-29 05:41:02 +00001233 // Walk over all of the live intervals that were touched by the splitter,
1234 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001235 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1236 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001237 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001238
Owen Anderson4bfc2092009-01-29 05:41:02 +00001239 // First, collect all the uses of the vreg, and sort them by their
1240 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001241 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1242 UE = MRI->use_end(); UI != UE; ++UI) {
Lang Hames86511252009-09-04 20:41:11 +00001243 MachineInstrIndex index = LIs->getInstructionIndex(&*UI);
Lang Hames96479942009-09-09 20:14:17 +00001244 index = LIs->getUseIndex(index);
Owen Anderson956ec272009-01-23 00:23:32 +00001245
1246 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001247 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001248 }
1249
Owen Anderson4bfc2092009-01-29 05:41:02 +00001250 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1251 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001252 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1253 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001254
1255 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1256 return changed;
1257
Owen Anderson956ec272009-01-23 00:23:32 +00001258 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001259
1260 // We don't currently try to handle definitions with PHI kills, because
1261 // it would involve processing more than one VNInfo at once.
Lang Hames857c4e02009-06-17 21:01:20 +00001262 if (CurrVN->hasPHIKill()) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001263
Owen Anderson4bfc2092009-01-29 05:41:02 +00001264 // We also don't try to handle the results of PHI joins, since there's
1265 // no defining instruction to analyze.
Lang Hames857c4e02009-06-17 21:01:20 +00001266 if (!CurrVN->isDefAccurate() || CurrVN->isUnused()) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001267
Owen Anderson4bfc2092009-01-29 05:41:02 +00001268 // We're only interested in eliminating cruft introduced by the splitter,
1269 // is of the form load-use or load-use-store. First, check that the
1270 // definition is a load, and remember what stack slot we loaded it from.
Lang Hames857c4e02009-06-17 21:01:20 +00001271 MachineInstr* DefMI = LIs->getInstructionFromIndex(CurrVN->def);
Owen Anderson956ec272009-01-23 00:23:32 +00001272 int FrameIndex;
1273 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1274
Owen Anderson4bfc2092009-01-29 05:41:02 +00001275 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001276 if (VNUseCount[CurrVN].size() == 0) {
1277 LIs->RemoveMachineInstrFromMaps(DefMI);
1278 (*LI)->removeValNo(CurrVN);
1279 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001280 VNUseCount.erase(CurrVN);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001281 NumDeadSpills++;
1282 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001283 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001284 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001285
Owen Anderson4bfc2092009-01-29 05:41:02 +00001286 // Second, get the number of non-store uses of the definition, as well as
1287 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001288 bool FeedsTwoAddr = false;
1289 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1290 (*LI)->reg, FrameIndex,
1291 FeedsTwoAddr);
1292
Owen Anderson4bfc2092009-01-29 05:41:02 +00001293 // If there's one non-store use and it doesn't feed a two-addr, then
1294 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001295 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001296 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001297 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1298 int StoreFrameIndex;
1299 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1300 while (UI != VNUseCount[CurrVN].end() &&
1301 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1302 ++UI;
1303 if (UI != VNUseCount[CurrVN].end())
1304 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1305 }
Owen Anderson45e68552009-01-29 05:28:55 +00001306 if (UI == VNUseCount[CurrVN].end()) continue;
1307
1308 MachineInstr* use = *UI;
1309
Owen Anderson4bfc2092009-01-29 05:41:02 +00001310 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001311 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1312 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001313 SmallVector<unsigned, 1> Ops;
1314 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001315 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1316
1317 MachineInstr* NewMI =
1318 TII->foldMemoryOperand(*use->getParent()->getParent(),
1319 use, Ops, FrameIndex);
1320
1321 if (!NewMI) continue;
1322
Owen Anderson4bfc2092009-01-29 05:41:02 +00001323 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001324 LIs->RemoveMachineInstrFromMaps(DefMI);
1325 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1326 (*LI)->removeValNo(CurrVN);
1327
1328 DefMI->eraseFromParent();
1329 MachineBasicBlock* MBB = use->getParent();
1330 NewMI = MBB->insert(MBB->erase(use), NewMI);
1331 VNUseCount[CurrVN].erase(use);
1332
Owen Anderson4bfc2092009-01-29 05:41:02 +00001333 // Remove deleted instructions. Note that we need to remove them from
1334 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001335 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1336 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1337 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001338 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001339 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1340 ++VNI)
1341 if (VNI->first != CurrVN)
1342 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001343 LIs->RemoveMachineInstrFromMaps(*II);
1344 (*II)->eraseFromParent();
1345 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001346
1347 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001348
1349 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1350 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1351 if (VI->second.erase(use))
1352 VI->second.insert(NewMI);
1353
1354 NumDeadSpills++;
1355 changed = true;
1356 continue;
1357 }
1358
Owen Anderson4bfc2092009-01-29 05:41:02 +00001359 // If there's more than one non-store instruction, we can't profitably
1360 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001361 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001362
Owen Anderson4bfc2092009-01-29 05:41:02 +00001363 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001364 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1365 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
1366 UI != UI; ++UI) {
1367 LIs->RemoveMachineInstrFromMaps(*UI);
1368 (*UI)->eraseFromParent();
1369 }
1370
Owen Andersonc0f3a032009-01-29 08:22:06 +00001371 VNUseCount.erase(CurrVN);
1372
Owen Anderson32ca8652009-01-24 10:07:43 +00001373 LIs->RemoveMachineInstrFromMaps(DefMI);
1374 (*LI)->removeValNo(CurrVN);
1375 DefMI->eraseFromParent();
1376 NumDeadSpills++;
1377 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001378 }
1379 }
1380
1381 return changed;
1382}
1383
Owen Andersonf1f75b12008-11-04 22:22:41 +00001384bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1385 MachineBasicBlock* DefMBB,
1386 MachineBasicBlock* BarrierMBB) {
1387 if (DefMBB == BarrierMBB)
1388 return false;
1389
Lang Hames857c4e02009-06-17 21:01:20 +00001390 if (LR->valno->hasPHIKill())
Owen Andersonf1f75b12008-11-04 22:22:41 +00001391 return false;
1392
Lang Hames86511252009-09-04 20:41:11 +00001393 MachineInstrIndex MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
Owen Andersonf1f75b12008-11-04 22:22:41 +00001394 if (LR->end < MBBEnd)
1395 return false;
1396
1397 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1398 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1399 return true;
1400
1401 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1402 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1403 typedef std::pair<MachineBasicBlock*,
1404 MachineBasicBlock::succ_iterator> ItPair;
1405 SmallVector<ItPair, 4> Stack;
1406 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1407
1408 while (!Stack.empty()) {
1409 ItPair P = Stack.back();
1410 Stack.pop_back();
1411
1412 MachineBasicBlock* PredMBB = P.first;
1413 MachineBasicBlock::succ_iterator S = P.second;
1414
1415 if (S == PredMBB->succ_end())
1416 continue;
1417 else if (Visited.count(*S)) {
1418 Stack.push_back(std::make_pair(PredMBB, ++S));
1419 continue;
1420 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001421 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001422
1423 MachineBasicBlock* MBB = *S;
1424 Visited.insert(MBB);
1425
1426 if (MBB == BarrierMBB)
1427 return true;
1428
1429 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1430 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1431 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1432 while (MDTN) {
1433 if (MDTN == DefMDTN)
1434 return true;
1435 else if (MDTN == BarrierMDTN)
1436 break;
1437 MDTN = MDTN->getIDom();
1438 }
1439
1440 MBBEnd = LIs->getMBBEndIdx(MBB);
1441 if (LR->end > MBBEnd)
1442 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1443 }
1444
1445 return false;
1446}
1447
1448
Evan Cheng09e8ca82008-10-20 21:44:59 +00001449bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001450 CurrMF = &MF;
1451 TM = &MF.getTarget();
Owen Anderson3ef45492009-01-29 22:13:06 +00001452 TRI = TM->getRegisterInfo();
Evan Chengd0e32c52008-10-29 05:06:14 +00001453 TII = TM->getInstrInfo();
1454 MFI = MF.getFrameInfo();
1455 MRI = &MF.getRegInfo();
1456 LIs = &getAnalysis<LiveIntervals>();
1457 LSs = &getAnalysis<LiveStacks>();
Owen Anderson420dd372009-03-14 21:40:05 +00001458 VRM = &getAnalysis<VirtRegMap>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001459
1460 bool MadeChange = false;
1461
1462 // Make sure blocks are numbered in order.
1463 MF.RenumberBlocks();
1464
Evan Cheng54898932008-10-29 08:39:34 +00001465 MachineBasicBlock *Entry = MF.begin();
1466 SmallPtrSet<MachineBasicBlock*,16> Visited;
1467
Owen Anderson956ec272009-01-23 00:23:32 +00001468 SmallPtrSet<LiveInterval*, 8> Split;
1469
Evan Cheng54898932008-10-29 08:39:34 +00001470 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1471 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1472 DFI != E; ++DFI) {
1473 BarrierMBB = *DFI;
1474 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1475 E = BarrierMBB->end(); I != E; ++I) {
1476 Barrier = &*I;
1477 const TargetRegisterClass **BarrierRCs =
1478 Barrier->getDesc().getRegClassBarriers();
1479 if (!BarrierRCs)
1480 continue;
1481 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001482 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001483 }
1484 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001485
Owen Anderson956ec272009-01-23 00:23:32 +00001486 MadeChange |= removeDeadSpills(Split);
1487
Evan Chengf5cd4f02008-10-23 20:43:13 +00001488 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001489}