blob: 8784dc965cfc8d27346d42a274da1363df8c5c38 [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"
Lang Hamesa937f222009-12-14 06:49:42 +000019#include "llvm/CodeGen/CalcSpillWeights.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000020#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000021#include "llvm/CodeGen/LiveStackAnalysis.h"
Owen Andersonf1f75b12008-11-04 22:22:41 +000022#include "llvm/CodeGen/MachineDominators.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineLoopInfo.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/CodeGen/Passes.h"
28#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000029#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000030#include "llvm/Target/TargetMachine.h"
31#include "llvm/Target/TargetOptions.h"
32#include "llvm/Target/TargetRegisterInfo.h"
33#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000035#include "llvm/Support/ErrorHandling.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000036#include "llvm/ADT/DenseMap.h"
Evan Cheng54898932008-10-29 08:39:34 +000037#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000038#include "llvm/ADT/SmallPtrSet.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000039#include "llvm/ADT/Statistic.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000040using namespace llvm;
41
Evan Chengae7fa5b2008-10-28 01:48:24 +000042static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden);
Evan Cheng63040ce2009-11-09 06:49:22 +000043static cl::opt<int> DeadSplitLimit("dead-split-limit", cl::init(-1),
44 cl::Hidden);
45static cl::opt<int> RestoreFoldLimit("restore-fold-limit", cl::init(-1),
46 cl::Hidden);
Evan Chengae7fa5b2008-10-28 01:48:24 +000047
Owen Anderson45e68552009-01-29 05:28:55 +000048STATISTIC(NumSplits, "Number of intervals split");
Owen Anderson75fa96b2008-11-19 04:28:29 +000049STATISTIC(NumRemats, "Number of intervals split by rematerialization");
Owen Anderson7b9d67c2008-12-02 18:53:47 +000050STATISTIC(NumFolds, "Number of intervals split with spill folding");
Owen Anderson323c58d2009-03-05 07:19:18 +000051STATISTIC(NumRestoreFolds, "Number of intervals split with restore folding");
Owen Anderson2ebf63f2008-12-18 01:27:19 +000052STATISTIC(NumRenumbers, "Number of intervals renumbered into new registers");
Owen Anderson956ec272009-01-23 00:23:32 +000053STATISTIC(NumDeadSpills, "Number of dead spills removed");
Evan Chengf5cd4f02008-10-23 20:43:13 +000054
Evan Cheng09e8ca82008-10-20 21:44:59 +000055namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000056 class PreAllocSplitting : public MachineFunctionPass {
Evan Chengd0e32c52008-10-29 05:06:14 +000057 MachineFunction *CurrMF;
Evan Chengf5cd4f02008-10-23 20:43:13 +000058 const TargetMachine *TM;
59 const TargetInstrInfo *TII;
Owen Anderson3ef45492009-01-29 22:13:06 +000060 const TargetRegisterInfo* TRI;
Evan Chengf5cd4f02008-10-23 20:43:13 +000061 MachineFrameInfo *MFI;
62 MachineRegisterInfo *MRI;
Lang Hames233a60e2009-11-03 23:52:08 +000063 SlotIndexes *SIs;
Evan Chengf5cd4f02008-10-23 20:43:13 +000064 LiveIntervals *LIs;
Evan Chengd0e32c52008-10-29 05:06:14 +000065 LiveStacks *LSs;
Owen Anderson420dd372009-03-14 21:40:05 +000066 VirtRegMap *VRM;
Evan Cheng09e8ca82008-10-20 21:44:59 +000067
Evan Chengf5cd4f02008-10-23 20:43:13 +000068 // Barrier - Current barrier being processed.
69 MachineInstr *Barrier;
70
71 // BarrierMBB - Basic block where the barrier resides in.
72 MachineBasicBlock *BarrierMBB;
73
74 // Barrier - Current barrier index.
Lang Hames233a60e2009-11-03 23:52:08 +000075 SlotIndex BarrierIdx;
Evan Chengf5cd4f02008-10-23 20:43:13 +000076
77 // CurrLI - Current live interval being split.
78 LiveInterval *CurrLI;
79
Evan Chengd0e32c52008-10-29 05:06:14 +000080 // CurrSLI - Current stack slot live interval.
81 LiveInterval *CurrSLI;
82
83 // CurrSValNo - Current val# for the stack slot live interval.
84 VNInfo *CurrSValNo;
85
86 // IntervalSSMap - A map from live interval to spill slots.
87 DenseMap<unsigned, int> IntervalSSMap;
Evan Chengf5cd4f02008-10-23 20:43:13 +000088
Evan Cheng54898932008-10-29 08:39:34 +000089 // Def2SpillMap - A map from a def instruction index to spill index.
Lang Hames233a60e2009-11-03 23:52:08 +000090 DenseMap<SlotIndex, SlotIndex> Def2SpillMap;
Evan Cheng06587492008-10-24 02:05:00 +000091
Evan Cheng09e8ca82008-10-20 21:44:59 +000092 public:
93 static char ID;
Lang Hames233a60e2009-11-03 23:52:08 +000094 PreAllocSplitting()
95 : MachineFunctionPass(&ID) {}
Evan Cheng09e8ca82008-10-20 21:44:59 +000096
97 virtual bool runOnMachineFunction(MachineFunction &MF);
98
99 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +0000100 AU.setPreservesCFG();
Lang Hames233a60e2009-11-03 23:52:08 +0000101 AU.addRequired<SlotIndexes>();
102 AU.addPreserved<SlotIndexes>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000103 AU.addRequired<LiveIntervals>();
104 AU.addPreserved<LiveIntervals>();
Evan Chengd0e32c52008-10-29 05:06:14 +0000105 AU.addRequired<LiveStacks>();
106 AU.addPreserved<LiveStacks>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000107 AU.addPreserved<RegisterCoalescer>();
Lang Hamesa937f222009-12-14 06:49:42 +0000108 AU.addPreserved<CalculateSpillWeights>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000109 if (StrongPHIElim)
110 AU.addPreservedID(StrongPHIEliminationID);
111 else
112 AU.addPreservedID(PHIEliminationID);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000113 AU.addRequired<MachineDominatorTree>();
114 AU.addRequired<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000115 AU.addRequired<VirtRegMap>();
Owen Andersonf1f75b12008-11-04 22:22:41 +0000116 AU.addPreserved<MachineDominatorTree>();
117 AU.addPreserved<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000118 AU.addPreserved<VirtRegMap>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000119 MachineFunctionPass::getAnalysisUsage(AU);
120 }
121
122 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +0000123 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +0000124 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000125 }
126
127 virtual const char *getPassName() const {
128 return "Pre-Register Allocaton Live Interval Splitting";
129 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000130
131 /// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000132 virtual void print(raw_ostream &O, const Module* M = 0) const {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000133 LIs->print(O, M);
134 }
135
Evan Chengf5cd4f02008-10-23 20:43:13 +0000136
137 private:
Evan Chengf5cd4f02008-10-23 20:43:13 +0000138
139 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000140 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Lang Hamesb3661582009-11-14 00:02:51 +0000141 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000142
143 MachineBasicBlock::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000144 findRestorePoint(MachineBasicBlock*, MachineInstr*, SlotIndex,
Lang Hamesb3661582009-11-14 00:02:51 +0000145 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000146
Evan Chengd0e32c52008-10-29 05:06:14 +0000147 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000148
Lang Hames86511252009-09-04 20:41:11 +0000149 bool IsAvailableInStack(MachineBasicBlock*, unsigned,
Lang Hames233a60e2009-11-03 23:52:08 +0000150 SlotIndex, SlotIndex,
151 SlotIndex&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000152
Lang Hames233a60e2009-11-03 23:52:08 +0000153 void UpdateSpillSlotInterval(VNInfo*, SlotIndex, SlotIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000154
Evan Chengf5cd4f02008-10-23 20:43:13 +0000155 bool SplitRegLiveInterval(LiveInterval*);
156
Owen Anderson956ec272009-01-23 00:23:32 +0000157 bool SplitRegLiveIntervals(const TargetRegisterClass **,
158 SmallPtrSet<LiveInterval*, 8>&);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000159
160 bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB,
161 MachineBasicBlock* BarrierMBB);
Owen Anderson75fa96b2008-11-19 04:28:29 +0000162 bool Rematerialize(unsigned vreg, VNInfo* ValNo,
163 MachineInstr* DefMI,
164 MachineBasicBlock::iterator RestorePt,
Owen Anderson75fa96b2008-11-19 04:28:29 +0000165 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000166 MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC,
167 MachineInstr* DefMI,
168 MachineInstr* Barrier,
169 MachineBasicBlock* MBB,
170 int& SS,
171 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersonc93023a2009-03-04 08:52:31 +0000172 MachineInstr* FoldRestore(unsigned vreg,
173 const TargetRegisterClass* RC,
174 MachineInstr* Barrier,
175 MachineBasicBlock* MBB,
176 int SS,
177 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000178 void RenumberValno(VNInfo* VN);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000179 void ReconstructLiveInterval(LiveInterval* LI);
Owen Anderson956ec272009-01-23 00:23:32 +0000180 bool removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split);
Owen Anderson45e68552009-01-29 05:28:55 +0000181 unsigned getNumberOfNonSpills(SmallPtrSet<MachineInstr*, 4>& MIs,
182 unsigned Reg, int FrameIndex, bool& TwoAddr);
Evan Cheng19a72582009-02-02 18:33:18 +0000183 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator Use,
184 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000185 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000186 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
187 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
188 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000189 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
190 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000191 bool IsTopLevel, bool IsIntraBlock);
192 VNInfo* PerformPHIConstructionFallBack(MachineBasicBlock::iterator Use,
193 MachineBasicBlock* MBB, LiveInterval* LI,
194 SmallPtrSet<MachineInstr*, 4>& Visited,
195 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
196 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
197 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
198 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
199 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
200 bool IsTopLevel, bool IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000201};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000202} // end anonymous namespace
203
204char PreAllocSplitting::ID = 0;
205
206static RegisterPass<PreAllocSplitting>
207X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
208
209const PassInfo *const llvm::PreAllocSplittingID = &X;
210
Evan Chengf5cd4f02008-10-23 20:43:13 +0000211/// findSpillPoint - Find a gap as far away from the given MI that's suitable
212/// for spilling the current live interval. The index must be before any
213/// defs and uses of the live interval register in the mbb. Return begin() if
214/// none is found.
215MachineBasicBlock::iterator
216PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000217 MachineInstr *DefMI,
Lang Hamesb3661582009-11-14 00:02:51 +0000218 SmallPtrSet<MachineInstr*, 4> &RefsInMBB) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000219 MachineBasicBlock::iterator Pt = MBB->begin();
220
Owen Anderson696a1302009-03-31 08:27:09 +0000221 MachineBasicBlock::iterator MII = MI;
222 MachineBasicBlock::iterator EndPt = DefMI
223 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
Owen Anderson4cafbb52009-02-20 10:02:23 +0000224
Owen Anderson696a1302009-03-31 08:27:09 +0000225 while (MII != EndPt && !RefsInMBB.count(MII) &&
226 MII->getOpcode() != TRI->getCallFrameSetupOpcode())
227 --MII;
228 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson4cafbb52009-02-20 10:02:23 +0000229
Owen Anderson696a1302009-03-31 08:27:09 +0000230 while (MII != EndPt && !RefsInMBB.count(MII)) {
Owen Anderson696a1302009-03-31 08:27:09 +0000231 // We can't insert the spill between the barrier (a call), and its
232 // corresponding call frame setup.
233 if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
234 while (MII->getOpcode() != TRI->getCallFrameSetupOpcode()) {
Owen Anderson5734c942009-02-05 05:58:41 +0000235 --MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000236 if (MII == EndPt) {
237 return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000238 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000239 }
Owen Anderson696a1302009-03-31 08:27:09 +0000240 continue;
Lang Hamesb3661582009-11-14 00:02:51 +0000241 } else {
Owen Anderson696a1302009-03-31 08:27:09 +0000242 Pt = MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000243 }
Owen Anderson696a1302009-03-31 08:27:09 +0000244
245 if (RefsInMBB.count(MII))
246 return Pt;
247
248
249 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000250 }
251
252 return Pt;
253}
254
255/// findRestorePoint - Find a gap in the instruction index map that's suitable
256/// for restoring the current live interval value. The index must be before any
257/// uses of the live interval register in the mbb. Return end() if none is
258/// found.
259MachineBasicBlock::iterator
260PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000261 SlotIndex LastIdx,
Lang Hamesb3661582009-11-14 00:02:51 +0000262 SmallPtrSet<MachineInstr*, 4> &RefsInMBB) {
Evan Cheng54898932008-10-29 08:39:34 +0000263 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
264 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000265 MachineBasicBlock::iterator Pt = MBB->end();
Owen Anderson696a1302009-03-31 08:27:09 +0000266 MachineBasicBlock::iterator EndPt = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000267
Owen Anderson696a1302009-03-31 08:27:09 +0000268 // We start at the call, so walk forward until we find the call frame teardown
269 // since we can't insert restores before that. Bail if we encounter a use
270 // during this time.
271 MachineBasicBlock::iterator MII = MI;
272 if (MII == EndPt) return Pt;
273
274 while (MII != EndPt && !RefsInMBB.count(MII) &&
275 MII->getOpcode() != TRI->getCallFrameDestroyOpcode())
276 ++MII;
277 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
278 ++MII;
279
280 // FIXME: Limit the number of instructions to examine to reduce
281 // compile time?
282 while (MII != EndPt) {
Lang Hames233a60e2009-11-03 23:52:08 +0000283 SlotIndex Index = LIs->getInstructionIndex(MII);
Owen Anderson696a1302009-03-31 08:27:09 +0000284 if (Index > LastIdx)
285 break;
Owen Anderson3ef45492009-01-29 22:13:06 +0000286
Owen Anderson696a1302009-03-31 08:27:09 +0000287 // We can't insert a restore between the barrier (a call) and its
288 // corresponding call frame teardown.
289 if (MII->getOpcode() == TRI->getCallFrameSetupOpcode()) {
290 do {
291 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000292 ++MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000293 } while (MII->getOpcode() != TRI->getCallFrameDestroyOpcode());
Lang Hamesb3661582009-11-14 00:02:51 +0000294 } else {
Owen Anderson696a1302009-03-31 08:27:09 +0000295 Pt = MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000296 }
Owen Anderson696a1302009-03-31 08:27:09 +0000297
298 if (RefsInMBB.count(MII))
299 return Pt;
300
301 ++MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000302 }
303
304 return Pt;
305}
306
Evan Chengd0e32c52008-10-29 05:06:14 +0000307/// CreateSpillStackSlot - Create a stack slot for the live interval being
308/// split. If the live interval was previously split, just reuse the same
309/// slot.
310int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
311 const TargetRegisterClass *RC) {
312 int SS;
313 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
314 if (I != IntervalSSMap.end()) {
315 SS = I->second;
316 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000317 SS = MFI->CreateSpillStackObject(RC->getSize(), RC->getAlignment());
Evan Chengd0e32c52008-10-29 05:06:14 +0000318 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000319 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000320
321 // Create live interval for stack slot.
Evan Chengc781a242009-05-03 18:32:42 +0000322 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Evan Cheng54898932008-10-29 08:39:34 +0000323 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000324 CurrSValNo = CurrSLI->getValNumInfo(0);
325 else
Lang Hames233a60e2009-11-03 23:52:08 +0000326 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000327 LSs->getVNInfoAllocator());
Evan Chengd0e32c52008-10-29 05:06:14 +0000328 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000329}
330
Evan Chengd0e32c52008-10-29 05:06:14 +0000331/// IsAvailableInStack - Return true if register is available in a split stack
332/// slot at the specified index.
333bool
Evan Cheng54898932008-10-29 08:39:34 +0000334PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000335 unsigned Reg, SlotIndex DefIndex,
336 SlotIndex RestoreIndex,
337 SlotIndex &SpillIndex,
Evan Cheng54898932008-10-29 08:39:34 +0000338 int& SS) const {
339 if (!DefMBB)
340 return false;
341
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000342 DenseMap<unsigned, int>::const_iterator I = IntervalSSMap.find(Reg);
Evan Chengd0e32c52008-10-29 05:06:14 +0000343 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000344 return false;
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000345 DenseMap<SlotIndex, SlotIndex>::const_iterator
Lang Hames86511252009-09-04 20:41:11 +0000346 II = Def2SpillMap.find(DefIndex);
Evan Cheng54898932008-10-29 08:39:34 +0000347 if (II == Def2SpillMap.end())
348 return false;
349
350 // If last spill of def is in the same mbb as barrier mbb (where restore will
351 // be), make sure it's not below the intended restore index.
352 // FIXME: Undo the previous spill?
353 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
354 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
355 return false;
356
357 SS = I->second;
358 SpillIndex = II->second;
359 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000360}
361
Evan Chengd0e32c52008-10-29 05:06:14 +0000362/// UpdateSpillSlotInterval - Given the specified val# of the register live
363/// interval being split, and the spill and restore indicies, update the live
364/// interval of the spill stack slot.
365void
Lang Hames233a60e2009-11-03 23:52:08 +0000366PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, SlotIndex SpillIndex,
367 SlotIndex RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000368 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
369 "Expect restore in the barrier mbb");
370
371 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
372 if (MBB == BarrierMBB) {
373 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000374 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
375 CurrSLI->addRange(SLR);
376 return;
377 }
378
Evan Cheng54898932008-10-29 08:39:34 +0000379 SmallPtrSet<MachineBasicBlock*, 4> Processed;
Lang Hames233a60e2009-11-03 23:52:08 +0000380 SlotIndex EndIdx = LIs->getMBBEndIdx(MBB);
Lang Hames74ab5ee2009-12-22 00:11:50 +0000381 LiveRange SLR(SpillIndex, EndIdx, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000382 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000383 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000384
385 // Start from the spill mbb, figure out the extend of the spill slot's
386 // live interval.
387 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000388 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
389 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000390 // If live range extend beyond end of mbb, add successors to work list.
391 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
392 SE = MBB->succ_end(); SI != SE; ++SI)
393 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000394
395 while (!WorkList.empty()) {
396 MachineBasicBlock *MBB = WorkList.back();
397 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000398 if (Processed.count(MBB))
399 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000400 SlotIndex Idx = LIs->getMBBStartIdx(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000401 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000402 if (LR && LR->valno == ValNo) {
403 EndIdx = LIs->getMBBEndIdx(MBB);
404 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000405 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000406 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000407 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000408 } else if (LR->end > EndIdx) {
409 // Live range extends beyond end of mbb, process successors.
Lang Hames233a60e2009-11-03 23:52:08 +0000410 LiveRange SLR(Idx, EndIdx.getNextIndex(), CurrSValNo);
Evan Cheng54898932008-10-29 08:39:34 +0000411 CurrSLI->addRange(SLR);
412 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
413 SE = MBB->succ_end(); SI != SE; ++SI)
414 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000415 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000416 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000417 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000418 }
Evan Cheng54898932008-10-29 08:39:34 +0000419 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000420 }
421 }
422}
423
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000424/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
425/// construction algorithm to compute the ranges and valnos for an interval.
Evan Cheng19a72582009-02-02 18:33:18 +0000426VNInfo*
427PreAllocSplitting::PerformPHIConstruction(MachineBasicBlock::iterator UseI,
428 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000429 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000430 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
431 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
432 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000433 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
434 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000435 bool IsTopLevel, bool IsIntraBlock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000436 // Return memoized result if it's available.
Evan Cheng19a72582009-02-02 18:33:18 +0000437 if (IsTopLevel && Visited.count(UseI) && NewVNs.count(UseI))
438 return NewVNs[UseI];
439 else if (!IsTopLevel && IsIntraBlock && NewVNs.count(UseI))
440 return NewVNs[UseI];
441 else if (!IsIntraBlock && LiveOut.count(MBB))
Owen Anderson7d211e22008-12-31 02:00:25 +0000442 return LiveOut[MBB];
443
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000444 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000445 bool ContainsDefs = Defs.count(MBB);
446 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000447
Evan Cheng19a72582009-02-02 18:33:18 +0000448 VNInfo* RetVNI = 0;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000449
450 // Enumerate the cases of use/def contaning blocks.
451 if (!ContainsDefs && !ContainsUses) {
Evan Cheng19a72582009-02-02 18:33:18 +0000452 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
453 NewVNs, LiveOut, Phis,
454 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000455 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000456 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000457
458 // Search for the def in this block. If we don't find it before the
459 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000460 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000461 // always be an end() iterator.
Evan Cheng19a72582009-02-02 18:33:18 +0000462 assert(UseI == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000463
Evan Cheng19a72582009-02-02 18:33:18 +0000464 MachineBasicBlock::iterator Walker = UseI;
465 --Walker;
466 while (Walker != MBB->begin()) {
467 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000468 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000469 --Walker;
470 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000471
472 // Once we've found it, extend its VNInfo to our instruction.
Lang Hames233a60e2009-11-03 23:52:08 +0000473 SlotIndex DefIndex = LIs->getInstructionIndex(Walker);
474 DefIndex = DefIndex.getDefIndex();
475 SlotIndex EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000476
Evan Cheng19a72582009-02-02 18:33:18 +0000477 RetVNI = NewVNs[Walker];
Lang Hames74ab5ee2009-12-22 00:11:50 +0000478 LI->addRange(LiveRange(DefIndex, EndIndex, RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000479 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000480 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000481
482 // Search for the use in this block that precedes the instruction we care
Evan Cheng19a72582009-02-02 18:33:18 +0000483 // about, going to the fallback case if we don't find it.
Evan Cheng19a72582009-02-02 18:33:18 +0000484 MachineBasicBlock::iterator Walker = UseI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000485 bool found = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000486 while (Walker != MBB->begin()) {
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000487 --Walker;
Evan Cheng19a72582009-02-02 18:33:18 +0000488 if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000489 found = true;
490 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000491 }
Evan Cheng19a72582009-02-02 18:33:18 +0000492 }
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000493
494 if (!found)
495 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
496 Uses, NewVNs, LiveOut, Phis,
497 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000498
Lang Hames233a60e2009-11-03 23:52:08 +0000499 SlotIndex UseIndex = LIs->getInstructionIndex(Walker);
500 UseIndex = UseIndex.getUseIndex();
501 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000502 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000503 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000504 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000505 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000506
507 // Now, recursively phi construct the VNInfo for the use we found,
508 // and then extend it to include the instruction we care about
Evan Cheng19a72582009-02-02 18:33:18 +0000509 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
510 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000511
Lang Hames74ab5ee2009-12-22 00:11:50 +0000512 LI->addRange(LiveRange(UseIndex, EndIndex, RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000513
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000514 // FIXME: Need to set kills properly for inter-block stuff.
Evan Cheng19a72582009-02-02 18:33:18 +0000515 } else if (ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000516 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
517 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000518
519 // This case is basically a merging of the two preceding case, with the
520 // special note that checking for defs must take precedence over checking
521 // for uses, because of two-address instructions.
Evan Cheng19a72582009-02-02 18:33:18 +0000522 MachineBasicBlock::iterator Walker = UseI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000523 bool foundDef = false;
524 bool foundUse = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000525 while (Walker != MBB->begin()) {
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000526 --Walker;
Evan Cheng19a72582009-02-02 18:33:18 +0000527 if (BlockDefs.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000528 foundDef = true;
529 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000530 } else if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000531 foundUse = true;
532 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000533 }
Evan Cheng19a72582009-02-02 18:33:18 +0000534 }
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000535
536 if (!foundDef && !foundUse)
537 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
538 Uses, NewVNs, LiveOut, Phis,
539 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000540
Lang Hames233a60e2009-11-03 23:52:08 +0000541 SlotIndex StartIndex = LIs->getInstructionIndex(Walker);
542 StartIndex = foundDef ? StartIndex.getDefIndex() : StartIndex.getUseIndex();
543 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000544 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000545 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000546 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000547 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000548
549 if (foundDef)
Evan Cheng19a72582009-02-02 18:33:18 +0000550 RetVNI = NewVNs[Walker];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000551 else
Evan Cheng19a72582009-02-02 18:33:18 +0000552 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
553 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000554
Lang Hames74ab5ee2009-12-22 00:11:50 +0000555 LI->addRange(LiveRange(StartIndex, EndIndex, RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000556 }
557
558 // Memoize results so we don't have to recompute them.
Evan Cheng19a72582009-02-02 18:33:18 +0000559 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000560 else {
Evan Cheng19a72582009-02-02 18:33:18 +0000561 if (!NewVNs.count(UseI))
562 NewVNs[UseI] = RetVNI;
563 Visited.insert(UseI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000564 }
565
Evan Cheng19a72582009-02-02 18:33:18 +0000566 return RetVNI;
567}
568
569/// PerformPHIConstructionFallBack - PerformPHIConstruction fall back path.
570///
571VNInfo*
572PreAllocSplitting::PerformPHIConstructionFallBack(MachineBasicBlock::iterator UseI,
573 MachineBasicBlock* MBB, LiveInterval* LI,
574 SmallPtrSet<MachineInstr*, 4>& Visited,
575 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
576 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
577 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
578 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
579 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
580 bool IsTopLevel, bool IsIntraBlock) {
581 // NOTE: Because this is the fallback case from other cases, we do NOT
582 // assume that we are not intrablock here.
583 if (Phis.count(MBB)) return Phis[MBB];
584
Lang Hames233a60e2009-11-03 23:52:08 +0000585 SlotIndex StartIndex = LIs->getMBBStartIdx(MBB);
Lang Hames857c4e02009-06-17 21:01:20 +0000586 VNInfo *RetVNI = Phis[MBB] =
Lang Hames233a60e2009-11-03 23:52:08 +0000587 LI->getNextValue(SlotIndex(), /*FIXME*/ 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000588 LIs->getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +0000589
Evan Cheng19a72582009-02-02 18:33:18 +0000590 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
591
592 // If there are no uses or defs between our starting point and the
593 // beginning of the block, then recursive perform phi construction
594 // on our predecessors.
595 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
596 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
597 PE = MBB->pred_end(); PI != PE; ++PI) {
598 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
599 Visited, Defs, Uses, NewVNs,
600 LiveOut, Phis, false, false);
601 if (Incoming != 0)
602 IncomingVNs[*PI] = Incoming;
603 }
604
Lang Hames857c4e02009-06-17 21:01:20 +0000605 if (MBB->pred_size() == 1 && !RetVNI->hasPHIKill()) {
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000606 VNInfo* OldVN = RetVNI;
607 VNInfo* NewVN = IncomingVNs.begin()->second;
608 VNInfo* MergedVN = LI->MergeValueNumberInto(OldVN, NewVN);
609 if (MergedVN == OldVN) std::swap(OldVN, NewVN);
610
611 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator LOI = LiveOut.begin(),
612 LOE = LiveOut.end(); LOI != LOE; ++LOI)
613 if (LOI->second == OldVN)
614 LOI->second = MergedVN;
615 for (DenseMap<MachineInstr*, VNInfo*>::iterator NVI = NewVNs.begin(),
616 NVE = NewVNs.end(); NVI != NVE; ++NVI)
617 if (NVI->second == OldVN)
618 NVI->second = MergedVN;
619 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator PI = Phis.begin(),
620 PE = Phis.end(); PI != PE; ++PI)
621 if (PI->second == OldVN)
622 PI->second = MergedVN;
623 RetVNI = MergedVN;
Evan Cheng19a72582009-02-02 18:33:18 +0000624 } else {
625 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
626 // VNInfo to represent the joined value.
627 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
628 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
Lang Hames857c4e02009-06-17 21:01:20 +0000629 I->second->setHasPHIKill(true);
Evan Cheng19a72582009-02-02 18:33:18 +0000630 }
631 }
632
Lang Hames233a60e2009-11-03 23:52:08 +0000633 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000634 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000635 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Evan Cheng19a72582009-02-02 18:33:18 +0000636 } else
637 EndIndex = LIs->getMBBEndIdx(MBB);
Lang Hames74ab5ee2009-12-22 00:11:50 +0000638 LI->addRange(LiveRange(StartIndex, EndIndex, RetVNI));
Evan Cheng19a72582009-02-02 18:33:18 +0000639
640 // Memoize results so we don't have to recompute them.
641 if (!IsIntraBlock)
642 LiveOut[MBB] = RetVNI;
643 else {
644 if (!NewVNs.count(UseI))
645 NewVNs[UseI] = RetVNI;
646 Visited.insert(UseI);
647 }
648
649 return RetVNI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000650}
651
652/// ReconstructLiveInterval - Recompute a live interval from scratch.
653void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
Benjamin Kramer991de142010-03-30 20:16:45 +0000654 VNInfo::Allocator& Alloc = LIs->getVNInfoAllocator();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000655
656 // Clear the old ranges and valnos;
657 LI->clear();
658
659 // Cache the uses and defs of the register
660 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
661 RegMap Defs, Uses;
662
663 // Keep track of the new VNs we're creating.
664 DenseMap<MachineInstr*, VNInfo*> NewVNs;
665 SmallPtrSet<VNInfo*, 2> PhiVNs;
666
667 // Cache defs, and create a new VNInfo for each def.
668 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
669 DE = MRI->def_end(); DI != DE; ++DI) {
670 Defs[(*DI).getParent()].insert(&*DI);
671
Lang Hames233a60e2009-11-03 23:52:08 +0000672 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
673 DefIdx = DefIdx.getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000674
Chris Lattner518bb532010-02-09 19:54:29 +0000675 assert(!DI->isPHI() && "PHI instr in code during pre-alloc splitting.");
Lang Hames857c4e02009-06-17 21:01:20 +0000676 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, true, Alloc);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000677
678 // If the def is a move, set the copy field.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000679 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
680 if (TII->isMoveInstr(*DI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
681 if (DstReg == LI->reg)
Lang Hames52c1afc2009-08-10 23:43:28 +0000682 NewVN->setCopy(&*DI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000683
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000684 NewVNs[&*DI] = NewVN;
685 }
686
687 // Cache uses as a separate pass from actually processing them.
688 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
689 UE = MRI->use_end(); UI != UE; ++UI)
690 Uses[(*UI).getParent()].insert(&*UI);
691
692 // Now, actually process every use and use a phi construction algorithm
693 // to walk from it to its reaching definitions, building VNInfos along
694 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000695 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
696 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000697 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000698 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
699 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000700 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000701 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000702 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000703
704 // Add ranges for dead defs
705 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
706 DE = MRI->def_end(); DI != DE; ++DI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000707 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
708 DefIdx = DefIdx.getDefIndex();
Owen Andersond4f6fe52008-12-28 23:35:13 +0000709
710 if (LI->liveAt(DefIdx)) continue;
711
712 VNInfo* DeadVN = NewVNs[&*DI];
Lang Hames233a60e2009-11-03 23:52:08 +0000713 LI->addRange(LiveRange(DefIdx, DefIdx.getNextSlot(), DeadVN));
Evan Cheng35ca9202009-10-09 01:17:11 +0000714 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000715}
716
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000717/// RenumberValno - Split the given valno out into a new vreg, allowing it to
718/// be allocated to a different register. This function creates a new vreg,
719/// copies the valno and its live ranges over to the new vreg's interval,
720/// removes them from the old interval, and rewrites all uses and defs of
721/// the original reg to the new vreg within those ranges.
722void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000723 SmallVector<VNInfo*, 4> Stack;
724 SmallVector<VNInfo*, 4> VNsToCopy;
725 Stack.push_back(VN);
726
727 // Walk through and copy the valno we care about, and any other valnos
728 // that are two-address redefinitions of the one we care about. These
729 // will need to be rewritten as well. We also check for safety of the
730 // renumbering here, by making sure that none of the valno involved has
731 // phi kills.
732 while (!Stack.empty()) {
733 VNInfo* OldVN = Stack.back();
734 Stack.pop_back();
735
736 // Bail out if we ever encounter a valno that has a PHI kill. We can't
737 // renumber these.
Lang Hames857c4e02009-06-17 21:01:20 +0000738 if (OldVN->hasPHIKill()) return;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000739
740 VNsToCopy.push_back(OldVN);
741
742 // Locate two-address redefinitions
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000743 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(CurrLI->reg),
744 DE = MRI->def_end(); DI != DE; ++DI) {
745 if (!DI->isRegTiedToUseOperand(DI.getOperandNo())) continue;
746 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI).getDefIndex();
747 VNInfo* NextVN = CurrLI->findDefinedVNInfoForRegInt(DefIdx);
748 if (std::find(VNsToCopy.begin(), VNsToCopy.end(), NextVN) !=
749 VNsToCopy.end())
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000750 Stack.push_back(NextVN);
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000751 }
752 }
753
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000754 // Create the new vreg
755 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
756
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000757 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000758 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000759
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000760 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
761 VNsToCopy.end(); OI != OE; ++OI) {
762 VNInfo* OldVN = *OI;
763
764 // Copy the valno over
Lang Hames857c4e02009-06-17 21:01:20 +0000765 VNInfo* NewVN = NewLI.createValueCopy(OldVN, LIs->getVNInfoAllocator());
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000766 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
767
768 // Remove the valno from the old interval
769 CurrLI->removeValNo(OldVN);
770 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000771
772 // Rewrite defs and uses. This is done in two stages to avoid invalidating
773 // the reg_iterator.
774 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
775
776 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
777 E = MRI->reg_end(); I != E; ++I) {
778 MachineOperand& MO = I.getOperand();
Lang Hames233a60e2009-11-03 23:52:08 +0000779 SlotIndex InstrIdx = LIs->getInstructionIndex(&*I);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000780
Lang Hames233a60e2009-11-03 23:52:08 +0000781 if ((MO.isUse() && NewLI.liveAt(InstrIdx.getUseIndex())) ||
782 (MO.isDef() && NewLI.liveAt(InstrIdx.getDefIndex())))
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000783 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
784 }
785
786 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
787 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
788 MachineInstr* Inst = I->first;
789 unsigned OpIdx = I->second;
790 MachineOperand& MO = Inst->getOperand(OpIdx);
791 MO.setReg(NewVReg);
792 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000793
Owen Anderson420dd372009-03-14 21:40:05 +0000794 // Grow the VirtRegMap, since we've created a new vreg.
795 VRM->grow();
796
Owen Anderson45e68552009-01-29 05:28:55 +0000797 // The renumbered vreg shares a stack slot with the old register.
798 if (IntervalSSMap.count(CurrLI->reg))
799 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
800
Dan Gohmanfe601042010-06-22 15:08:57 +0000801 ++NumRenumbers;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000802}
803
Evan Cheng37844532009-07-16 09:20:10 +0000804bool PreAllocSplitting::Rematerialize(unsigned VReg, VNInfo* ValNo,
Owen Anderson6002e992008-12-04 21:20:30 +0000805 MachineInstr* DefMI,
806 MachineBasicBlock::iterator RestorePt,
Owen Anderson6002e992008-12-04 21:20:30 +0000807 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
808 MachineBasicBlock& MBB = *RestorePt->getParent();
809
810 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
Lang Hames857c4e02009-06-17 21:01:20 +0000811 if (!ValNo->isDefAccurate() || DefMI->getParent() == BarrierMBB)
Lang Hamesb3661582009-11-14 00:02:51 +0000812 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB);
Owen Anderson6002e992008-12-04 21:20:30 +0000813 else
Chris Lattner7896c9f2009-12-03 00:50:42 +0000814 KillPt = llvm::next(MachineBasicBlock::iterator(DefMI));
Owen Anderson6002e992008-12-04 21:20:30 +0000815
816 if (KillPt == DefMI->getParent()->end())
817 return false;
818
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000819 TII->reMaterialize(MBB, RestorePt, VReg, 0, DefMI, *TRI);
Lang Hamesb3661582009-11-14 00:02:51 +0000820 SlotIndex RematIdx = LIs->InsertMachineInstrInMaps(prior(RestorePt));
Owen Anderson6002e992008-12-04 21:20:30 +0000821
Owen Andersonb4b84362009-01-26 21:57:31 +0000822 ReconstructLiveInterval(CurrLI);
Lang Hames233a60e2009-11-03 23:52:08 +0000823 RematIdx = RematIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +0000824 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000825
Owen Anderson75fa96b2008-11-19 04:28:29 +0000826 ++NumSplits;
827 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000828 return true;
829}
830
831MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
832 const TargetRegisterClass* RC,
833 MachineInstr* DefMI,
834 MachineInstr* Barrier,
835 MachineBasicBlock* MBB,
836 int& SS,
837 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000838 // Go top down if RefsInMBB is empty.
839 if (RefsInMBB.empty())
840 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000841
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000842 MachineBasicBlock::iterator FoldPt = Barrier;
843 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
844 !RefsInMBB.count(FoldPt))
845 --FoldPt;
846
Evan Cheng1015ba72010-05-21 20:53:24 +0000847 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000848 if (OpIdx == -1)
849 return 0;
850
851 SmallVector<unsigned, 1> Ops;
852 Ops.push_back(OpIdx);
853
854 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
855 return 0;
856
857 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
858 if (I != IntervalSSMap.end()) {
859 SS = I->second;
860 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000861 SS = MFI->CreateSpillStackObject(RC->getSize(), RC->getAlignment());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000862 }
863
864 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
865 FoldPt, Ops, SS);
866
867 if (FMI) {
868 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
869 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
870 ++NumFolds;
871
872 IntervalSSMap[vreg] = SS;
Evan Chengc781a242009-05-03 18:32:42 +0000873 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000874 if (CurrSLI->hasAtLeastOneValue())
875 CurrSValNo = CurrSLI->getValNumInfo(0);
876 else
Lang Hames233a60e2009-11-03 23:52:08 +0000877 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000878 LSs->getVNInfoAllocator());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000879 }
880
881 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000882}
883
Owen Andersonc93023a2009-03-04 08:52:31 +0000884MachineInstr* PreAllocSplitting::FoldRestore(unsigned vreg,
885 const TargetRegisterClass* RC,
886 MachineInstr* Barrier,
887 MachineBasicBlock* MBB,
888 int SS,
889 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Andersona2bfb542009-03-05 08:23:20 +0000890 if ((int)RestoreFoldLimit != -1 && RestoreFoldLimit == (int)NumRestoreFolds)
Owen Anderson323c58d2009-03-05 07:19:18 +0000891 return 0;
892
Owen Andersonc93023a2009-03-04 08:52:31 +0000893 // Go top down if RefsInMBB is empty.
894 if (RefsInMBB.empty())
895 return 0;
896
897 // Can't fold a restore between a call stack setup and teardown.
898 MachineBasicBlock::iterator FoldPt = Barrier;
Owen Anderson323c58d2009-03-05 07:19:18 +0000899
900 // Advance from barrier to call frame teardown.
901 while (FoldPt != MBB->getFirstTerminator() &&
902 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
903 if (RefsInMBB.count(FoldPt))
904 return 0;
Owen Andersonc93023a2009-03-04 08:52:31 +0000905
Owen Anderson323c58d2009-03-05 07:19:18 +0000906 ++FoldPt;
907 }
908
909 if (FoldPt == MBB->getFirstTerminator())
910 return 0;
911 else
912 ++FoldPt;
913
914 // Now find the restore point.
915 while (FoldPt != MBB->getFirstTerminator() && !RefsInMBB.count(FoldPt)) {
Owen Andersonc93023a2009-03-04 08:52:31 +0000916 if (FoldPt->getOpcode() == TRI->getCallFrameSetupOpcode()) {
917 while (FoldPt != MBB->getFirstTerminator() &&
918 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
919 if (RefsInMBB.count(FoldPt))
920 return 0;
921
922 ++FoldPt;
923 }
924
Owen Anderson323c58d2009-03-05 07:19:18 +0000925 if (FoldPt == MBB->getFirstTerminator())
926 return 0;
927 }
928
929 ++FoldPt;
Owen Andersonc93023a2009-03-04 08:52:31 +0000930 }
931
932 if (FoldPt == MBB->getFirstTerminator())
933 return 0;
934
935 int OpIdx = FoldPt->findRegisterUseOperandIdx(vreg, true);
936 if (OpIdx == -1)
937 return 0;
938
939 SmallVector<unsigned, 1> Ops;
940 Ops.push_back(OpIdx);
941
942 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
943 return 0;
944
945 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
946 FoldPt, Ops, SS);
947
948 if (FMI) {
949 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
950 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
Owen Anderson323c58d2009-03-05 07:19:18 +0000951 ++NumRestoreFolds;
Owen Andersonc93023a2009-03-04 08:52:31 +0000952 }
953
954 return FMI;
955}
956
Evan Chengf5cd4f02008-10-23 20:43:13 +0000957/// SplitRegLiveInterval - Split (spill and restore) the given live interval
958/// so it would not cross the barrier that's being processed. Shrink wrap
959/// (minimize) the live interval to the last uses.
960bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
David Greene4175f582010-01-05 01:25:47 +0000961 DEBUG(dbgs() << "Pre-alloc splitting " << LI->reg << " for " << *Barrier
Lang Hames233a60e2009-11-03 23:52:08 +0000962 << " result: ");
963
Evan Chengf5cd4f02008-10-23 20:43:13 +0000964 CurrLI = LI;
965
966 // Find live range where current interval cross the barrier.
967 LiveInterval::iterator LR =
Lang Hames233a60e2009-11-03 23:52:08 +0000968 CurrLI->FindLiveRangeContaining(BarrierIdx.getUseIndex());
Evan Chengf5cd4f02008-10-23 20:43:13 +0000969 VNInfo *ValNo = LR->valno;
970
Torok Edwinf3689232009-07-12 20:07:01 +0000971 assert(!ValNo->isUnused() && "Val# is defined by a dead def?");
Evan Chengf5cd4f02008-10-23 20:43:13 +0000972
Lang Hames857c4e02009-06-17 21:01:20 +0000973 MachineInstr *DefMI = ValNo->isDefAccurate()
Evan Cheng06587492008-10-24 02:05:00 +0000974 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +0000975
Owen Andersond3be4622009-01-21 08:18:03 +0000976 // If this would create a new join point, do not split.
Lang Hames233a60e2009-11-03 23:52:08 +0000977 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent())) {
David Greene4175f582010-01-05 01:25:47 +0000978 DEBUG(dbgs() << "FAILED (would create a new join point).\n");
Owen Andersond3be4622009-01-21 08:18:03 +0000979 return false;
Lang Hames233a60e2009-11-03 23:52:08 +0000980 }
Owen Andersond3be4622009-01-21 08:18:03 +0000981
Evan Chengf5cd4f02008-10-23 20:43:13 +0000982 // Find all references in the barrier mbb.
983 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
984 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
985 E = MRI->reg_end(); I != E; ++I) {
986 MachineInstr *RefMI = &*I;
987 if (RefMI->getParent() == BarrierMBB)
988 RefsInMBB.insert(RefMI);
989 }
990
991 // Find a point to restore the value after the barrier.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000992 MachineBasicBlock::iterator RestorePt =
Lang Hamesb3661582009-11-14 00:02:51 +0000993 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +0000994 if (RestorePt == BarrierMBB->end()) {
David Greene4175f582010-01-05 01:25:47 +0000995 DEBUG(dbgs() << "FAILED (could not find a suitable restore point).\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +0000996 return false;
Lang Hames233a60e2009-11-03 23:52:08 +0000997 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000998
Owen Anderson75fa96b2008-11-19 04:28:29 +0000999 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
Lang Hamesb3661582009-11-14 00:02:51 +00001000 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt, RefsInMBB)) {
David Greene4175f582010-01-05 01:25:47 +00001001 DEBUG(dbgs() << "success (remat).\n");
Lang Hames233a60e2009-11-03 23:52:08 +00001002 return true;
1003 }
Owen Anderson75fa96b2008-11-19 04:28:29 +00001004
Evan Chengf5cd4f02008-10-23 20:43:13 +00001005 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001006 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001007 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Lang Hames233a60e2009-11-03 23:52:08 +00001008 SlotIndex SpillIndex;
Evan Cheng06587492008-10-24 02:05:00 +00001009 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001010 int SS = -1;
Lang Hames857c4e02009-06-17 21:01:20 +00001011 if (!ValNo->isDefAccurate()) {
1012 // If we don't know where the def is we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001013 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1014 BarrierMBB, SS, RefsInMBB))) {
1015 SpillIndex = LIs->getInstructionIndex(SpillMI);
1016 } else {
1017 MachineBasicBlock::iterator SpillPt =
Lang Hamesb3661582009-11-14 00:02:51 +00001018 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001019 if (SpillPt == BarrierMBB->begin()) {
David Greene4175f582010-01-05 01:25:47 +00001020 DEBUG(dbgs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001021 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001022 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001023 // Add spill.
1024
1025 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng746ad692010-05-06 19:06:44 +00001026 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC,
1027 TRI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001028 SpillMI = prior(SpillPt);
Lang Hamesb3661582009-11-14 00:02:51 +00001029 SpillIndex = LIs->InsertMachineInstrInMaps(SpillMI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001030 }
Evan Cheng54898932008-10-29 08:39:34 +00001031 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
Lang Hamesb3661582009-11-14 00:02:51 +00001032 LIs->getZeroIndex(), SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001033 // If it's already split, just restore the value. There is no need to spill
1034 // the def again.
Lang Hames233a60e2009-11-03 23:52:08 +00001035 if (!DefMI) {
David Greene4175f582010-01-05 01:25:47 +00001036 DEBUG(dbgs() << "FAILED (def is dead).\n");
Evan Chengd0e32c52008-10-29 05:06:14 +00001037 return false; // Def is dead. Do nothing.
Lang Hames233a60e2009-11-03 23:52:08 +00001038 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001039
1040 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
Evan Cheng35ca9202009-10-09 01:17:11 +00001041 BarrierMBB, SS, RefsInMBB))) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001042 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001043 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001044 // Check if it's possible to insert a spill after the def MI.
1045 MachineBasicBlock::iterator SpillPt;
1046 if (DefMBB == BarrierMBB) {
1047 // Add spill after the def and the last use before the barrier.
1048 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
Lang Hamesb3661582009-11-14 00:02:51 +00001049 RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001050 if (SpillPt == DefMBB->begin()) {
David Greene4175f582010-01-05 01:25:47 +00001051 DEBUG(dbgs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001052 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001053 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001054 } else {
Chris Lattner7896c9f2009-12-03 00:50:42 +00001055 SpillPt = llvm::next(MachineBasicBlock::iterator(DefMI));
Lang Hames233a60e2009-11-03 23:52:08 +00001056 if (SpillPt == DefMBB->end()) {
David Greene4175f582010-01-05 01:25:47 +00001057 DEBUG(dbgs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001058 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001059 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001060 }
Evan Cheng35ca9202009-10-09 01:17:11 +00001061 // Add spill.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001062 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng746ad692010-05-06 19:06:44 +00001063 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg, false, SS, RC,
1064 TRI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001065 SpillMI = prior(SpillPt);
Lang Hamesb3661582009-11-14 00:02:51 +00001066 SpillIndex = LIs->InsertMachineInstrInMaps(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001067 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001068 }
1069
Evan Cheng54898932008-10-29 08:39:34 +00001070 // Remember def instruction index to spill index mapping.
1071 if (DefMI && SpillMI)
1072 Def2SpillMap[ValNo->def] = SpillIndex;
1073
Evan Chengf5cd4f02008-10-23 20:43:13 +00001074 // Add restore.
Owen Andersonc93023a2009-03-04 08:52:31 +00001075 bool FoldedRestore = false;
Lang Hamesb3661582009-11-14 00:02:51 +00001076 SlotIndex RestoreIndex;
Owen Andersonc93023a2009-03-04 08:52:31 +00001077 if (MachineInstr* LMI = FoldRestore(CurrLI->reg, RC, Barrier,
1078 BarrierMBB, SS, RefsInMBB)) {
1079 RestorePt = LMI;
Owen Anderson323c58d2009-03-05 07:19:18 +00001080 RestoreIndex = LIs->getInstructionIndex(RestorePt);
Owen Andersonc93023a2009-03-04 08:52:31 +00001081 FoldedRestore = true;
1082 } else {
Evan Cheng746ad692010-05-06 19:06:44 +00001083 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC, TRI);
Owen Andersonc93023a2009-03-04 08:52:31 +00001084 MachineInstr *LoadMI = prior(RestorePt);
Lang Hamesb3661582009-11-14 00:02:51 +00001085 RestoreIndex = LIs->InsertMachineInstrInMaps(LoadMI);
Owen Andersonc93023a2009-03-04 08:52:31 +00001086 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001087
Evan Chengd0e32c52008-10-29 05:06:14 +00001088 // Update spill stack slot live interval.
Lang Hames233a60e2009-11-03 23:52:08 +00001089 UpdateSpillSlotInterval(ValNo, SpillIndex.getUseIndex().getNextSlot(),
1090 RestoreIndex.getDefIndex());
Evan Chengd0e32c52008-10-29 05:06:14 +00001091
Owen Andersonb4b84362009-01-26 21:57:31 +00001092 ReconstructLiveInterval(CurrLI);
Evan Cheng35ca9202009-10-09 01:17:11 +00001093
Owen Andersonc93023a2009-03-04 08:52:31 +00001094 if (!FoldedRestore) {
Lang Hames233a60e2009-11-03 23:52:08 +00001095 SlotIndex RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
1096 RestoreIdx = RestoreIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +00001097 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RestoreIdx));
Owen Andersonc93023a2009-03-04 08:52:31 +00001098 }
Owen Anderson7d211e22008-12-31 02:00:25 +00001099
Evan Chengae7fa5b2008-10-28 01:48:24 +00001100 ++NumSplits;
David Greene4175f582010-01-05 01:25:47 +00001101 DEBUG(dbgs() << "success.\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001102 return true;
1103}
1104
1105/// SplitRegLiveIntervals - Split all register live intervals that cross the
1106/// barrier that's being processed.
1107bool
Owen Anderson956ec272009-01-23 00:23:32 +00001108PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1109 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001110 // First find all the virtual registers whose live intervals are intercepted
1111 // by the current barrier.
1112 SmallVector<LiveInterval*, 8> Intervals;
1113 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng4350eb82009-02-06 17:17:30 +00001114 // FIXME: If it's not safe to move any instruction that defines the barrier
1115 // register class, then it means there are some special dependencies which
1116 // codegen is not modelling. Ignore these barriers for now.
1117 if (!TII->isSafeToMoveRegClassDefs(*RC))
Evan Cheng23066282008-10-27 07:14:50 +00001118 continue;
Chris Lattner65569b82010-05-21 17:47:50 +00001119 const std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
Evan Chengf5cd4f02008-10-23 20:43:13 +00001120 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1121 unsigned Reg = VRs[i];
1122 if (!LIs->hasInterval(Reg))
1123 continue;
1124 LiveInterval *LI = &LIs->getInterval(Reg);
1125 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1126 // Virtual register live interval is intercepted by the barrier. We
1127 // should split and shrink wrap its interval if possible.
1128 Intervals.push_back(LI);
1129 }
1130 }
1131
1132 // Process the affected live intervals.
1133 bool Change = false;
1134 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001135 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1136 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001137 LiveInterval *LI = Intervals.back();
1138 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001139 bool result = SplitRegLiveInterval(LI);
1140 if (result) Split.insert(LI);
1141 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001142 }
1143
1144 return Change;
1145}
1146
Owen Anderson45e68552009-01-29 05:28:55 +00001147unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001148 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001149 unsigned Reg, int FrameIndex,
1150 bool& FeedsTwoAddr) {
1151 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001152 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001153 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001154 int StoreFrameIndex;
1155 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001156 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
Dan Gohmanfe601042010-06-22 15:08:57 +00001157 ++NonSpills;
Owen Anderson45e68552009-01-29 05:28:55 +00001158
1159 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
Bob Wilsond9df5012009-04-09 17:16:43 +00001160 if (DefIdx != -1 && (*UI)->isRegTiedToUseOperand(DefIdx))
Owen Anderson45e68552009-01-29 05:28:55 +00001161 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001162 }
1163
Owen Anderson45e68552009-01-29 05:28:55 +00001164 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001165}
1166
Owen Anderson956ec272009-01-23 00:23:32 +00001167/// removeDeadSpills - After doing splitting, filter through all intervals we've
1168/// split, and see if any of the spills are unnecessary. If so, remove them.
1169bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1170 bool changed = false;
1171
Owen Anderson4bfc2092009-01-29 05:41:02 +00001172 // Walk over all of the live intervals that were touched by the splitter,
1173 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001174 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1175 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001176 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001177
Owen Anderson4bfc2092009-01-29 05:41:02 +00001178 // First, collect all the uses of the vreg, and sort them by their
1179 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001180 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1181 UE = MRI->use_end(); UI != UE; ++UI) {
Lang Hames233a60e2009-11-03 23:52:08 +00001182 SlotIndex index = LIs->getInstructionIndex(&*UI);
1183 index = index.getUseIndex();
Owen Anderson956ec272009-01-23 00:23:32 +00001184
1185 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001186 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001187 }
1188
Owen Anderson4bfc2092009-01-29 05:41:02 +00001189 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1190 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001191 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1192 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001193
1194 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1195 return changed;
1196
Owen Anderson956ec272009-01-23 00:23:32 +00001197 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001198
1199 // We don't currently try to handle definitions with PHI kills, because
1200 // it would involve processing more than one VNInfo at once.
Lang Hames857c4e02009-06-17 21:01:20 +00001201 if (CurrVN->hasPHIKill()) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001202
Owen Anderson4bfc2092009-01-29 05:41:02 +00001203 // We also don't try to handle the results of PHI joins, since there's
1204 // no defining instruction to analyze.
Lang Hames857c4e02009-06-17 21:01:20 +00001205 if (!CurrVN->isDefAccurate() || CurrVN->isUnused()) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001206
Owen Anderson4bfc2092009-01-29 05:41:02 +00001207 // We're only interested in eliminating cruft introduced by the splitter,
1208 // is of the form load-use or load-use-store. First, check that the
1209 // definition is a load, and remember what stack slot we loaded it from.
Lang Hames857c4e02009-06-17 21:01:20 +00001210 MachineInstr* DefMI = LIs->getInstructionFromIndex(CurrVN->def);
Owen Anderson956ec272009-01-23 00:23:32 +00001211 int FrameIndex;
1212 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1213
Owen Anderson4bfc2092009-01-29 05:41:02 +00001214 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001215 if (VNUseCount[CurrVN].size() == 0) {
1216 LIs->RemoveMachineInstrFromMaps(DefMI);
1217 (*LI)->removeValNo(CurrVN);
1218 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001219 VNUseCount.erase(CurrVN);
Dan Gohmanfe601042010-06-22 15:08:57 +00001220 ++NumDeadSpills;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001221 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001222 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001223 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001224
Owen Anderson4bfc2092009-01-29 05:41:02 +00001225 // Second, get the number of non-store uses of the definition, as well as
1226 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001227 bool FeedsTwoAddr = false;
1228 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1229 (*LI)->reg, FrameIndex,
1230 FeedsTwoAddr);
1231
Owen Anderson4bfc2092009-01-29 05:41:02 +00001232 // If there's one non-store use and it doesn't feed a two-addr, then
1233 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001234 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001235 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001236 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1237 int StoreFrameIndex;
1238 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1239 while (UI != VNUseCount[CurrVN].end() &&
1240 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1241 ++UI;
1242 if (UI != VNUseCount[CurrVN].end())
1243 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1244 }
Owen Anderson45e68552009-01-29 05:28:55 +00001245 if (UI == VNUseCount[CurrVN].end()) continue;
1246
1247 MachineInstr* use = *UI;
1248
Owen Anderson4bfc2092009-01-29 05:41:02 +00001249 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001250 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1251 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001252 SmallVector<unsigned, 1> Ops;
1253 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001254 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1255
1256 MachineInstr* NewMI =
1257 TII->foldMemoryOperand(*use->getParent()->getParent(),
1258 use, Ops, FrameIndex);
1259
1260 if (!NewMI) continue;
1261
Owen Anderson4bfc2092009-01-29 05:41:02 +00001262 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001263 LIs->RemoveMachineInstrFromMaps(DefMI);
1264 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1265 (*LI)->removeValNo(CurrVN);
1266
1267 DefMI->eraseFromParent();
1268 MachineBasicBlock* MBB = use->getParent();
1269 NewMI = MBB->insert(MBB->erase(use), NewMI);
1270 VNUseCount[CurrVN].erase(use);
1271
Owen Anderson4bfc2092009-01-29 05:41:02 +00001272 // Remove deleted instructions. Note that we need to remove them from
1273 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001274 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1275 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1276 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001277 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001278 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1279 ++VNI)
1280 if (VNI->first != CurrVN)
1281 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001282 LIs->RemoveMachineInstrFromMaps(*II);
1283 (*II)->eraseFromParent();
1284 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001285
1286 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001287
1288 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1289 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1290 if (VI->second.erase(use))
1291 VI->second.insert(NewMI);
1292
Dan Gohmanfe601042010-06-22 15:08:57 +00001293 ++NumDeadSpills;
Owen Anderson45e68552009-01-29 05:28:55 +00001294 changed = true;
1295 continue;
1296 }
1297
Owen Anderson4bfc2092009-01-29 05:41:02 +00001298 // If there's more than one non-store instruction, we can't profitably
1299 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001300 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001301
Owen Anderson4bfc2092009-01-29 05:41:02 +00001302 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001303 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1304 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
Lang Hamesf035ce52009-11-12 01:24:08 +00001305 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001306 LIs->RemoveMachineInstrFromMaps(*UI);
1307 (*UI)->eraseFromParent();
1308 }
1309
Owen Andersonc0f3a032009-01-29 08:22:06 +00001310 VNUseCount.erase(CurrVN);
1311
Owen Anderson32ca8652009-01-24 10:07:43 +00001312 LIs->RemoveMachineInstrFromMaps(DefMI);
1313 (*LI)->removeValNo(CurrVN);
1314 DefMI->eraseFromParent();
Dan Gohmanfe601042010-06-22 15:08:57 +00001315 ++NumDeadSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001316 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001317 }
1318 }
1319
1320 return changed;
1321}
1322
Owen Andersonf1f75b12008-11-04 22:22:41 +00001323bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1324 MachineBasicBlock* DefMBB,
1325 MachineBasicBlock* BarrierMBB) {
1326 if (DefMBB == BarrierMBB)
1327 return false;
1328
Lang Hames857c4e02009-06-17 21:01:20 +00001329 if (LR->valno->hasPHIKill())
Owen Andersonf1f75b12008-11-04 22:22:41 +00001330 return false;
1331
Lang Hames233a60e2009-11-03 23:52:08 +00001332 SlotIndex MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
Owen Andersonf1f75b12008-11-04 22:22:41 +00001333 if (LR->end < MBBEnd)
1334 return false;
1335
1336 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1337 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1338 return true;
1339
1340 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1341 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1342 typedef std::pair<MachineBasicBlock*,
1343 MachineBasicBlock::succ_iterator> ItPair;
1344 SmallVector<ItPair, 4> Stack;
1345 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1346
1347 while (!Stack.empty()) {
1348 ItPair P = Stack.back();
1349 Stack.pop_back();
1350
1351 MachineBasicBlock* PredMBB = P.first;
1352 MachineBasicBlock::succ_iterator S = P.second;
1353
1354 if (S == PredMBB->succ_end())
1355 continue;
1356 else if (Visited.count(*S)) {
1357 Stack.push_back(std::make_pair(PredMBB, ++S));
1358 continue;
1359 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001360 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001361
1362 MachineBasicBlock* MBB = *S;
1363 Visited.insert(MBB);
1364
1365 if (MBB == BarrierMBB)
1366 return true;
1367
1368 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1369 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1370 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1371 while (MDTN) {
1372 if (MDTN == DefMDTN)
1373 return true;
1374 else if (MDTN == BarrierMDTN)
1375 break;
1376 MDTN = MDTN->getIDom();
1377 }
1378
1379 MBBEnd = LIs->getMBBEndIdx(MBB);
1380 if (LR->end > MBBEnd)
1381 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1382 }
1383
1384 return false;
1385}
1386
1387
Evan Cheng09e8ca82008-10-20 21:44:59 +00001388bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001389 CurrMF = &MF;
1390 TM = &MF.getTarget();
Owen Anderson3ef45492009-01-29 22:13:06 +00001391 TRI = TM->getRegisterInfo();
Evan Chengd0e32c52008-10-29 05:06:14 +00001392 TII = TM->getInstrInfo();
1393 MFI = MF.getFrameInfo();
1394 MRI = &MF.getRegInfo();
Lang Hames233a60e2009-11-03 23:52:08 +00001395 SIs = &getAnalysis<SlotIndexes>();
Evan Chengd0e32c52008-10-29 05:06:14 +00001396 LIs = &getAnalysis<LiveIntervals>();
1397 LSs = &getAnalysis<LiveStacks>();
Owen Anderson420dd372009-03-14 21:40:05 +00001398 VRM = &getAnalysis<VirtRegMap>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001399
1400 bool MadeChange = false;
1401
1402 // Make sure blocks are numbered in order.
1403 MF.RenumberBlocks();
1404
Evan Cheng54898932008-10-29 08:39:34 +00001405 MachineBasicBlock *Entry = MF.begin();
1406 SmallPtrSet<MachineBasicBlock*,16> Visited;
1407
Owen Anderson956ec272009-01-23 00:23:32 +00001408 SmallPtrSet<LiveInterval*, 8> Split;
1409
Evan Cheng54898932008-10-29 08:39:34 +00001410 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1411 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1412 DFI != E; ++DFI) {
1413 BarrierMBB = *DFI;
1414 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1415 E = BarrierMBB->end(); I != E; ++I) {
1416 Barrier = &*I;
1417 const TargetRegisterClass **BarrierRCs =
1418 Barrier->getDesc().getRegClassBarriers();
1419 if (!BarrierRCs)
1420 continue;
1421 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001422 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001423 }
1424 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001425
Owen Anderson956ec272009-01-23 00:23:32 +00001426 MadeChange |= removeDeadSpills(Split);
1427
Evan Chengf5cd4f02008-10-23 20:43:13 +00001428 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001429}