blob: 1c5222c38e7cbf08ab88008d90e662592747cb6f [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.
484 if (UseI == MBB->begin())
485 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
486 Uses, NewVNs, LiveOut, Phis,
487 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000488
Evan Cheng19a72582009-02-02 18:33:18 +0000489 MachineBasicBlock::iterator Walker = UseI;
490 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000491 bool found = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000492 while (Walker != MBB->begin()) {
493 if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000494 found = true;
495 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000496 }
497 --Walker;
498 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000499
500 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000501 if (!found) {
Evan Cheng19a72582009-02-02 18:33:18 +0000502 if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000503 found = true;
504 else
Evan Cheng19a72582009-02-02 18:33:18 +0000505 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
506 Uses, NewVNs, LiveOut, Phis,
507 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000508 }
509
Lang Hames233a60e2009-11-03 23:52:08 +0000510 SlotIndex UseIndex = LIs->getInstructionIndex(Walker);
511 UseIndex = UseIndex.getUseIndex();
512 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000513 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000514 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000515 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000516 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000517
518 // Now, recursively phi construct the VNInfo for the use we found,
519 // and then extend it to include the instruction we care about
Evan Cheng19a72582009-02-02 18:33:18 +0000520 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
521 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000522
Lang Hames74ab5ee2009-12-22 00:11:50 +0000523 LI->addRange(LiveRange(UseIndex, EndIndex, RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000524
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000525 // FIXME: Need to set kills properly for inter-block stuff.
Lang Hames86511252009-09-04 20:41:11 +0000526 if (RetVNI->isKill(UseIndex)) RetVNI->removeKill(UseIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000527 if (IsIntraBlock)
Lang Hames86511252009-09-04 20:41:11 +0000528 RetVNI->addKill(EndIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000529 } else if (ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000530 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
531 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000532
533 // This case is basically a merging of the two preceding case, with the
534 // special note that checking for defs must take precedence over checking
535 // for uses, because of two-address instructions.
536
Evan Cheng19a72582009-02-02 18:33:18 +0000537 if (UseI == MBB->begin())
538 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
539 NewVNs, LiveOut, Phis,
540 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000541
Evan Cheng19a72582009-02-02 18:33:18 +0000542 MachineBasicBlock::iterator Walker = UseI;
543 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000544 bool foundDef = false;
545 bool foundUse = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000546 while (Walker != MBB->begin()) {
547 if (BlockDefs.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000548 foundDef = true;
549 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000550 } else if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000551 foundUse = true;
552 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000553 }
554 --Walker;
555 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000556
557 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000558 if (!foundDef && !foundUse) {
Evan Cheng19a72582009-02-02 18:33:18 +0000559 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000560 foundDef = true;
Evan Cheng19a72582009-02-02 18:33:18 +0000561 else if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000562 foundUse = true;
563 else
Evan Cheng19a72582009-02-02 18:33:18 +0000564 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
565 Uses, NewVNs, LiveOut, Phis,
566 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000567 }
568
Lang Hames233a60e2009-11-03 23:52:08 +0000569 SlotIndex StartIndex = LIs->getInstructionIndex(Walker);
570 StartIndex = foundDef ? StartIndex.getDefIndex() : StartIndex.getUseIndex();
571 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000572 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000573 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000574 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000575 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000576
577 if (foundDef)
Evan Cheng19a72582009-02-02 18:33:18 +0000578 RetVNI = NewVNs[Walker];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000579 else
Evan Cheng19a72582009-02-02 18:33:18 +0000580 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
581 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000582
Lang Hames74ab5ee2009-12-22 00:11:50 +0000583 LI->addRange(LiveRange(StartIndex, EndIndex, RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000584
Lang Hames86511252009-09-04 20:41:11 +0000585 if (foundUse && RetVNI->isKill(StartIndex))
586 RetVNI->removeKill(StartIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000587 if (IsIntraBlock) {
Lang Hames86511252009-09-04 20:41:11 +0000588 RetVNI->addKill(EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000589 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000590 }
591
592 // Memoize results so we don't have to recompute them.
Evan Cheng19a72582009-02-02 18:33:18 +0000593 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000594 else {
Evan Cheng19a72582009-02-02 18:33:18 +0000595 if (!NewVNs.count(UseI))
596 NewVNs[UseI] = RetVNI;
597 Visited.insert(UseI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000598 }
599
Evan Cheng19a72582009-02-02 18:33:18 +0000600 return RetVNI;
601}
602
603/// PerformPHIConstructionFallBack - PerformPHIConstruction fall back path.
604///
605VNInfo*
606PreAllocSplitting::PerformPHIConstructionFallBack(MachineBasicBlock::iterator UseI,
607 MachineBasicBlock* MBB, LiveInterval* LI,
608 SmallPtrSet<MachineInstr*, 4>& Visited,
609 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
610 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
611 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
612 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
613 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
614 bool IsTopLevel, bool IsIntraBlock) {
615 // NOTE: Because this is the fallback case from other cases, we do NOT
616 // assume that we are not intrablock here.
617 if (Phis.count(MBB)) return Phis[MBB];
618
Lang Hames233a60e2009-11-03 23:52:08 +0000619 SlotIndex StartIndex = LIs->getMBBStartIdx(MBB);
Lang Hames857c4e02009-06-17 21:01:20 +0000620 VNInfo *RetVNI = Phis[MBB] =
Lang Hames233a60e2009-11-03 23:52:08 +0000621 LI->getNextValue(SlotIndex(), /*FIXME*/ 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000622 LIs->getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +0000623
Evan Cheng19a72582009-02-02 18:33:18 +0000624 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
625
626 // If there are no uses or defs between our starting point and the
627 // beginning of the block, then recursive perform phi construction
628 // on our predecessors.
629 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
630 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
631 PE = MBB->pred_end(); PI != PE; ++PI) {
632 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
633 Visited, Defs, Uses, NewVNs,
634 LiveOut, Phis, false, false);
635 if (Incoming != 0)
636 IncomingVNs[*PI] = Incoming;
637 }
638
Lang Hames857c4e02009-06-17 21:01:20 +0000639 if (MBB->pred_size() == 1 && !RetVNI->hasPHIKill()) {
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000640 VNInfo* OldVN = RetVNI;
641 VNInfo* NewVN = IncomingVNs.begin()->second;
642 VNInfo* MergedVN = LI->MergeValueNumberInto(OldVN, NewVN);
643 if (MergedVN == OldVN) std::swap(OldVN, NewVN);
644
645 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator LOI = LiveOut.begin(),
646 LOE = LiveOut.end(); LOI != LOE; ++LOI)
647 if (LOI->second == OldVN)
648 LOI->second = MergedVN;
649 for (DenseMap<MachineInstr*, VNInfo*>::iterator NVI = NewVNs.begin(),
650 NVE = NewVNs.end(); NVI != NVE; ++NVI)
651 if (NVI->second == OldVN)
652 NVI->second = MergedVN;
653 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator PI = Phis.begin(),
654 PE = Phis.end(); PI != PE; ++PI)
655 if (PI->second == OldVN)
656 PI->second = MergedVN;
657 RetVNI = MergedVN;
Evan Cheng19a72582009-02-02 18:33:18 +0000658 } else {
659 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
660 // VNInfo to represent the joined value.
661 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
662 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
Lang Hames857c4e02009-06-17 21:01:20 +0000663 I->second->setHasPHIKill(true);
Lang Hames74ab5ee2009-12-22 00:11:50 +0000664 SlotIndex KillIndex(LIs->getMBBEndIdx(I->first), true);
Lang Hames86511252009-09-04 20:41:11 +0000665 if (!I->second->isKill(KillIndex))
666 I->second->addKill(KillIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000667 }
668 }
669
Lang Hames233a60e2009-11-03 23:52:08 +0000670 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000671 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000672 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Evan Cheng19a72582009-02-02 18:33:18 +0000673 } else
674 EndIndex = LIs->getMBBEndIdx(MBB);
Lang Hames74ab5ee2009-12-22 00:11:50 +0000675 LI->addRange(LiveRange(StartIndex, EndIndex, RetVNI));
Evan Cheng19a72582009-02-02 18:33:18 +0000676 if (IsIntraBlock)
Lang Hames86511252009-09-04 20:41:11 +0000677 RetVNI->addKill(EndIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000678
679 // Memoize results so we don't have to recompute them.
680 if (!IsIntraBlock)
681 LiveOut[MBB] = RetVNI;
682 else {
683 if (!NewVNs.count(UseI))
684 NewVNs[UseI] = RetVNI;
685 Visited.insert(UseI);
686 }
687
688 return RetVNI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000689}
690
691/// ReconstructLiveInterval - Recompute a live interval from scratch.
692void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
693 BumpPtrAllocator& Alloc = LIs->getVNInfoAllocator();
694
695 // Clear the old ranges and valnos;
696 LI->clear();
697
698 // Cache the uses and defs of the register
699 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
700 RegMap Defs, Uses;
701
702 // Keep track of the new VNs we're creating.
703 DenseMap<MachineInstr*, VNInfo*> NewVNs;
704 SmallPtrSet<VNInfo*, 2> PhiVNs;
705
706 // Cache defs, and create a new VNInfo for each def.
707 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
708 DE = MRI->def_end(); DI != DE; ++DI) {
709 Defs[(*DI).getParent()].insert(&*DI);
710
Lang Hames233a60e2009-11-03 23:52:08 +0000711 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
712 DefIdx = DefIdx.getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000713
Lang Hames857c4e02009-06-17 21:01:20 +0000714 assert(DI->getOpcode() != TargetInstrInfo::PHI &&
Lang Hamesb3661582009-11-14 00:02:51 +0000715 "PHI instr in code during pre-alloc splitting.");
Lang Hames857c4e02009-06-17 21:01:20 +0000716 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, true, Alloc);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000717
718 // If the def is a move, set the copy field.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000719 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
720 if (TII->isMoveInstr(*DI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
721 if (DstReg == LI->reg)
Lang Hames52c1afc2009-08-10 23:43:28 +0000722 NewVN->setCopy(&*DI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000723
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000724 NewVNs[&*DI] = NewVN;
725 }
726
727 // Cache uses as a separate pass from actually processing them.
728 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
729 UE = MRI->use_end(); UI != UE; ++UI)
730 Uses[(*UI).getParent()].insert(&*UI);
731
732 // Now, actually process every use and use a phi construction algorithm
733 // to walk from it to its reaching definitions, building VNInfos along
734 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000735 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
736 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000737 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000738 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
739 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000740 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000741 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000742 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000743
744 // Add ranges for dead defs
745 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
746 DE = MRI->def_end(); DI != DE; ++DI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000747 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
748 DefIdx = DefIdx.getDefIndex();
Owen Andersond4f6fe52008-12-28 23:35:13 +0000749
750 if (LI->liveAt(DefIdx)) continue;
751
752 VNInfo* DeadVN = NewVNs[&*DI];
Lang Hames233a60e2009-11-03 23:52:08 +0000753 LI->addRange(LiveRange(DefIdx, DefIdx.getNextSlot(), DeadVN));
Lang Hames86511252009-09-04 20:41:11 +0000754 DeadVN->addKill(DefIdx);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000755 }
Evan Cheng35ca9202009-10-09 01:17:11 +0000756
757 // Update kill markers.
758 for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
759 VI != VE; ++VI) {
760 VNInfo* VNI = *VI;
761 for (unsigned i = 0, e = VNI->kills.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +0000762 SlotIndex KillIdx = VNI->kills[i];
763 if (KillIdx.isPHI())
Evan Cheng35ca9202009-10-09 01:17:11 +0000764 continue;
765 MachineInstr *KillMI = LIs->getInstructionFromIndex(KillIdx);
766 if (KillMI) {
767 MachineOperand *KillMO = KillMI->findRegisterUseOperand(CurrLI->reg);
768 if (KillMO)
769 // It could be a dead def.
770 KillMO->setIsKill();
771 }
772 }
773 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000774}
775
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000776/// RenumberValno - Split the given valno out into a new vreg, allowing it to
777/// be allocated to a different register. This function creates a new vreg,
778/// copies the valno and its live ranges over to the new vreg's interval,
779/// removes them from the old interval, and rewrites all uses and defs of
780/// the original reg to the new vreg within those ranges.
781void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000782 SmallVector<VNInfo*, 4> Stack;
783 SmallVector<VNInfo*, 4> VNsToCopy;
784 Stack.push_back(VN);
785
786 // Walk through and copy the valno we care about, and any other valnos
787 // that are two-address redefinitions of the one we care about. These
788 // will need to be rewritten as well. We also check for safety of the
789 // renumbering here, by making sure that none of the valno involved has
790 // phi kills.
791 while (!Stack.empty()) {
792 VNInfo* OldVN = Stack.back();
793 Stack.pop_back();
794
795 // Bail out if we ever encounter a valno that has a PHI kill. We can't
796 // renumber these.
Lang Hames857c4e02009-06-17 21:01:20 +0000797 if (OldVN->hasPHIKill()) return;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000798
799 VNsToCopy.push_back(OldVN);
800
801 // Locate two-address redefinitions
Lang Hamesffd13262009-07-09 03:57:02 +0000802 for (VNInfo::KillSet::iterator KI = OldVN->kills.begin(),
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000803 KE = OldVN->kills.end(); KI != KE; ++KI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000804 assert(!KI->isPHI() &&
Lang Hames86511252009-09-04 20:41:11 +0000805 "VN previously reported having no PHI kills.");
806 MachineInstr* MI = LIs->getInstructionFromIndex(*KI);
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000807 unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg);
808 if (DefIdx == ~0U) continue;
Bob Wilsond9df5012009-04-09 17:16:43 +0000809 if (MI->isRegTiedToUseOperand(DefIdx)) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000810 VNInfo* NextVN =
Lang Hames233a60e2009-11-03 23:52:08 +0000811 CurrLI->findDefinedVNInfoForRegInt(KI->getDefIndex());
Owen Andersonb4b84362009-01-26 21:57:31 +0000812 if (NextVN == OldVN) continue;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000813 Stack.push_back(NextVN);
814 }
815 }
816 }
817
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000818 // Create the new vreg
819 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
820
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000821 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000822 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000823
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000824 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
825 VNsToCopy.end(); OI != OE; ++OI) {
826 VNInfo* OldVN = *OI;
827
828 // Copy the valno over
Lang Hames857c4e02009-06-17 21:01:20 +0000829 VNInfo* NewVN = NewLI.createValueCopy(OldVN, LIs->getVNInfoAllocator());
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000830 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
831
832 // Remove the valno from the old interval
833 CurrLI->removeValNo(OldVN);
834 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000835
836 // Rewrite defs and uses. This is done in two stages to avoid invalidating
837 // the reg_iterator.
838 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
839
840 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
841 E = MRI->reg_end(); I != E; ++I) {
842 MachineOperand& MO = I.getOperand();
Lang Hames233a60e2009-11-03 23:52:08 +0000843 SlotIndex InstrIdx = LIs->getInstructionIndex(&*I);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000844
Lang Hames233a60e2009-11-03 23:52:08 +0000845 if ((MO.isUse() && NewLI.liveAt(InstrIdx.getUseIndex())) ||
846 (MO.isDef() && NewLI.liveAt(InstrIdx.getDefIndex())))
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000847 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
848 }
849
850 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
851 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
852 MachineInstr* Inst = I->first;
853 unsigned OpIdx = I->second;
854 MachineOperand& MO = Inst->getOperand(OpIdx);
855 MO.setReg(NewVReg);
856 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000857
Owen Anderson420dd372009-03-14 21:40:05 +0000858 // Grow the VirtRegMap, since we've created a new vreg.
859 VRM->grow();
860
Owen Anderson45e68552009-01-29 05:28:55 +0000861 // The renumbered vreg shares a stack slot with the old register.
862 if (IntervalSSMap.count(CurrLI->reg))
863 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
864
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000865 NumRenumbers++;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000866}
867
Evan Cheng37844532009-07-16 09:20:10 +0000868bool PreAllocSplitting::Rematerialize(unsigned VReg, VNInfo* ValNo,
Owen Anderson6002e992008-12-04 21:20:30 +0000869 MachineInstr* DefMI,
870 MachineBasicBlock::iterator RestorePt,
Owen Anderson6002e992008-12-04 21:20:30 +0000871 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
872 MachineBasicBlock& MBB = *RestorePt->getParent();
873
874 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
Lang Hames857c4e02009-06-17 21:01:20 +0000875 if (!ValNo->isDefAccurate() || DefMI->getParent() == BarrierMBB)
Lang Hamesb3661582009-11-14 00:02:51 +0000876 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB);
Owen Anderson6002e992008-12-04 21:20:30 +0000877 else
Chris Lattner7896c9f2009-12-03 00:50:42 +0000878 KillPt = llvm::next(MachineBasicBlock::iterator(DefMI));
Owen Anderson6002e992008-12-04 21:20:30 +0000879
880 if (KillPt == DefMI->getParent()->end())
881 return false;
882
Evan Chengd57cdd52009-11-14 02:55:43 +0000883 TII->reMaterialize(MBB, RestorePt, VReg, 0, DefMI, TRI);
Lang Hamesb3661582009-11-14 00:02:51 +0000884 SlotIndex RematIdx = LIs->InsertMachineInstrInMaps(prior(RestorePt));
Owen Anderson6002e992008-12-04 21:20:30 +0000885
Owen Andersonb4b84362009-01-26 21:57:31 +0000886 ReconstructLiveInterval(CurrLI);
Lang Hames233a60e2009-11-03 23:52:08 +0000887 RematIdx = RematIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +0000888 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000889
Owen Anderson75fa96b2008-11-19 04:28:29 +0000890 ++NumSplits;
891 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000892 return true;
893}
894
895MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
896 const TargetRegisterClass* RC,
897 MachineInstr* DefMI,
898 MachineInstr* Barrier,
899 MachineBasicBlock* MBB,
900 int& SS,
901 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000902 // Go top down if RefsInMBB is empty.
903 if (RefsInMBB.empty())
904 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000905
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000906 MachineBasicBlock::iterator FoldPt = Barrier;
907 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
908 !RefsInMBB.count(FoldPt))
909 --FoldPt;
910
911 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
912 if (OpIdx == -1)
913 return 0;
914
915 SmallVector<unsigned, 1> Ops;
916 Ops.push_back(OpIdx);
917
918 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
919 return 0;
920
921 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
922 if (I != IntervalSSMap.end()) {
923 SS = I->second;
924 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000925 SS = MFI->CreateSpillStackObject(RC->getSize(), RC->getAlignment());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000926 }
927
928 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
929 FoldPt, Ops, SS);
930
931 if (FMI) {
932 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
933 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
934 ++NumFolds;
935
936 IntervalSSMap[vreg] = SS;
Evan Chengc781a242009-05-03 18:32:42 +0000937 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000938 if (CurrSLI->hasAtLeastOneValue())
939 CurrSValNo = CurrSLI->getValNumInfo(0);
940 else
Lang Hames233a60e2009-11-03 23:52:08 +0000941 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000942 LSs->getVNInfoAllocator());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000943 }
944
945 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000946}
947
Owen Andersonc93023a2009-03-04 08:52:31 +0000948MachineInstr* PreAllocSplitting::FoldRestore(unsigned vreg,
949 const TargetRegisterClass* RC,
950 MachineInstr* Barrier,
951 MachineBasicBlock* MBB,
952 int SS,
953 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Andersona2bfb542009-03-05 08:23:20 +0000954 if ((int)RestoreFoldLimit != -1 && RestoreFoldLimit == (int)NumRestoreFolds)
Owen Anderson323c58d2009-03-05 07:19:18 +0000955 return 0;
956
Owen Andersonc93023a2009-03-04 08:52:31 +0000957 // Go top down if RefsInMBB is empty.
958 if (RefsInMBB.empty())
959 return 0;
960
961 // Can't fold a restore between a call stack setup and teardown.
962 MachineBasicBlock::iterator FoldPt = Barrier;
Owen Anderson323c58d2009-03-05 07:19:18 +0000963
964 // Advance from barrier to call frame teardown.
965 while (FoldPt != MBB->getFirstTerminator() &&
966 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
967 if (RefsInMBB.count(FoldPt))
968 return 0;
Owen Andersonc93023a2009-03-04 08:52:31 +0000969
Owen Anderson323c58d2009-03-05 07:19:18 +0000970 ++FoldPt;
971 }
972
973 if (FoldPt == MBB->getFirstTerminator())
974 return 0;
975 else
976 ++FoldPt;
977
978 // Now find the restore point.
979 while (FoldPt != MBB->getFirstTerminator() && !RefsInMBB.count(FoldPt)) {
Owen Andersonc93023a2009-03-04 08:52:31 +0000980 if (FoldPt->getOpcode() == TRI->getCallFrameSetupOpcode()) {
981 while (FoldPt != MBB->getFirstTerminator() &&
982 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
983 if (RefsInMBB.count(FoldPt))
984 return 0;
985
986 ++FoldPt;
987 }
988
Owen Anderson323c58d2009-03-05 07:19:18 +0000989 if (FoldPt == MBB->getFirstTerminator())
990 return 0;
991 }
992
993 ++FoldPt;
Owen Andersonc93023a2009-03-04 08:52:31 +0000994 }
995
996 if (FoldPt == MBB->getFirstTerminator())
997 return 0;
998
999 int OpIdx = FoldPt->findRegisterUseOperandIdx(vreg, true);
1000 if (OpIdx == -1)
1001 return 0;
1002
1003 SmallVector<unsigned, 1> Ops;
1004 Ops.push_back(OpIdx);
1005
1006 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
1007 return 0;
1008
1009 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
1010 FoldPt, Ops, SS);
1011
1012 if (FMI) {
1013 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
1014 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
Owen Anderson323c58d2009-03-05 07:19:18 +00001015 ++NumRestoreFolds;
Owen Andersonc93023a2009-03-04 08:52:31 +00001016 }
1017
1018 return FMI;
1019}
1020
Evan Chengf5cd4f02008-10-23 20:43:13 +00001021/// SplitRegLiveInterval - Split (spill and restore) the given live interval
1022/// so it would not cross the barrier that's being processed. Shrink wrap
1023/// (minimize) the live interval to the last uses.
1024bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
Lang Hames233a60e2009-11-03 23:52:08 +00001025 DEBUG(errs() << "Pre-alloc splitting " << LI->reg << " for " << *Barrier
1026 << " result: ");
1027
Evan Chengf5cd4f02008-10-23 20:43:13 +00001028 CurrLI = LI;
1029
1030 // Find live range where current interval cross the barrier.
1031 LiveInterval::iterator LR =
Lang Hames233a60e2009-11-03 23:52:08 +00001032 CurrLI->FindLiveRangeContaining(BarrierIdx.getUseIndex());
Evan Chengf5cd4f02008-10-23 20:43:13 +00001033 VNInfo *ValNo = LR->valno;
1034
Torok Edwinf3689232009-07-12 20:07:01 +00001035 assert(!ValNo->isUnused() && "Val# is defined by a dead def?");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001036
Lang Hames857c4e02009-06-17 21:01:20 +00001037 MachineInstr *DefMI = ValNo->isDefAccurate()
Evan Cheng06587492008-10-24 02:05:00 +00001038 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +00001039
Owen Andersond3be4622009-01-21 08:18:03 +00001040 // If this would create a new join point, do not split.
Lang Hames233a60e2009-11-03 23:52:08 +00001041 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent())) {
1042 DEBUG(errs() << "FAILED (would create a new join point).\n");
Owen Andersond3be4622009-01-21 08:18:03 +00001043 return false;
Lang Hames233a60e2009-11-03 23:52:08 +00001044 }
Owen Andersond3be4622009-01-21 08:18:03 +00001045
Evan Chengf5cd4f02008-10-23 20:43:13 +00001046 // Find all references in the barrier mbb.
1047 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
1048 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
1049 E = MRI->reg_end(); I != E; ++I) {
1050 MachineInstr *RefMI = &*I;
1051 if (RefMI->getParent() == BarrierMBB)
1052 RefsInMBB.insert(RefMI);
1053 }
1054
1055 // Find a point to restore the value after the barrier.
Evan Chengf5cd4f02008-10-23 20:43:13 +00001056 MachineBasicBlock::iterator RestorePt =
Lang Hamesb3661582009-11-14 00:02:51 +00001057 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001058 if (RestorePt == BarrierMBB->end()) {
1059 DEBUG(errs() << "FAILED (could not find a suitable restore point).\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001060 return false;
Lang Hames233a60e2009-11-03 23:52:08 +00001061 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001062
Owen Anderson75fa96b2008-11-19 04:28:29 +00001063 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
Lang Hamesb3661582009-11-14 00:02:51 +00001064 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt, RefsInMBB)) {
Lang Hames233a60e2009-11-03 23:52:08 +00001065 DEBUG(errs() << "success (remat).\n");
1066 return true;
1067 }
Owen Anderson75fa96b2008-11-19 04:28:29 +00001068
Evan Chengf5cd4f02008-10-23 20:43:13 +00001069 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001070 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001071 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Lang Hames233a60e2009-11-03 23:52:08 +00001072 SlotIndex SpillIndex;
Evan Cheng06587492008-10-24 02:05:00 +00001073 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001074 int SS = -1;
Lang Hames857c4e02009-06-17 21:01:20 +00001075 if (!ValNo->isDefAccurate()) {
1076 // If we don't know where the def is we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001077 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1078 BarrierMBB, SS, RefsInMBB))) {
1079 SpillIndex = LIs->getInstructionIndex(SpillMI);
1080 } else {
1081 MachineBasicBlock::iterator SpillPt =
Lang Hamesb3661582009-11-14 00:02:51 +00001082 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001083 if (SpillPt == BarrierMBB->begin()) {
1084 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001085 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001086 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001087 // Add spill.
1088
1089 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1090 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
1091 SpillMI = prior(SpillPt);
Lang Hamesb3661582009-11-14 00:02:51 +00001092 SpillIndex = LIs->InsertMachineInstrInMaps(SpillMI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001093 }
Evan Cheng54898932008-10-29 08:39:34 +00001094 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
Lang Hamesb3661582009-11-14 00:02:51 +00001095 LIs->getZeroIndex(), SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001096 // If it's already split, just restore the value. There is no need to spill
1097 // the def again.
Lang Hames233a60e2009-11-03 23:52:08 +00001098 if (!DefMI) {
1099 DEBUG(errs() << "FAILED (def is dead).\n");
Evan Chengd0e32c52008-10-29 05:06:14 +00001100 return false; // Def is dead. Do nothing.
Lang Hames233a60e2009-11-03 23:52:08 +00001101 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001102
1103 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
Evan Cheng35ca9202009-10-09 01:17:11 +00001104 BarrierMBB, SS, RefsInMBB))) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001105 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001106 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001107 // Check if it's possible to insert a spill after the def MI.
1108 MachineBasicBlock::iterator SpillPt;
1109 if (DefMBB == BarrierMBB) {
1110 // Add spill after the def and the last use before the barrier.
1111 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
Lang Hamesb3661582009-11-14 00:02:51 +00001112 RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001113 if (SpillPt == DefMBB->begin()) {
1114 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001115 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001116 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001117 } else {
Chris Lattner7896c9f2009-12-03 00:50:42 +00001118 SpillPt = llvm::next(MachineBasicBlock::iterator(DefMI));
Lang Hames233a60e2009-11-03 23:52:08 +00001119 if (SpillPt == DefMBB->end()) {
1120 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001121 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001122 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001123 }
Evan Cheng35ca9202009-10-09 01:17:11 +00001124 // Add spill.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001125 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng35ca9202009-10-09 01:17:11 +00001126 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg, false, SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001127 SpillMI = prior(SpillPt);
Lang Hamesb3661582009-11-14 00:02:51 +00001128 SpillIndex = LIs->InsertMachineInstrInMaps(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001129 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001130 }
1131
Evan Cheng54898932008-10-29 08:39:34 +00001132 // Remember def instruction index to spill index mapping.
1133 if (DefMI && SpillMI)
1134 Def2SpillMap[ValNo->def] = SpillIndex;
1135
Evan Chengf5cd4f02008-10-23 20:43:13 +00001136 // Add restore.
Owen Andersonc93023a2009-03-04 08:52:31 +00001137 bool FoldedRestore = false;
Lang Hamesb3661582009-11-14 00:02:51 +00001138 SlotIndex RestoreIndex;
Owen Andersonc93023a2009-03-04 08:52:31 +00001139 if (MachineInstr* LMI = FoldRestore(CurrLI->reg, RC, Barrier,
1140 BarrierMBB, SS, RefsInMBB)) {
1141 RestorePt = LMI;
Owen Anderson323c58d2009-03-05 07:19:18 +00001142 RestoreIndex = LIs->getInstructionIndex(RestorePt);
Owen Andersonc93023a2009-03-04 08:52:31 +00001143 FoldedRestore = true;
1144 } else {
1145 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
1146 MachineInstr *LoadMI = prior(RestorePt);
Lang Hamesb3661582009-11-14 00:02:51 +00001147 RestoreIndex = LIs->InsertMachineInstrInMaps(LoadMI);
Owen Andersonc93023a2009-03-04 08:52:31 +00001148 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001149
Evan Chengd0e32c52008-10-29 05:06:14 +00001150 // Update spill stack slot live interval.
Lang Hames233a60e2009-11-03 23:52:08 +00001151 UpdateSpillSlotInterval(ValNo, SpillIndex.getUseIndex().getNextSlot(),
1152 RestoreIndex.getDefIndex());
Evan Chengd0e32c52008-10-29 05:06:14 +00001153
Owen Andersonb4b84362009-01-26 21:57:31 +00001154 ReconstructLiveInterval(CurrLI);
Evan Cheng35ca9202009-10-09 01:17:11 +00001155
Owen Andersonc93023a2009-03-04 08:52:31 +00001156 if (!FoldedRestore) {
Lang Hames233a60e2009-11-03 23:52:08 +00001157 SlotIndex RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
1158 RestoreIdx = RestoreIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +00001159 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RestoreIdx));
Owen Andersonc93023a2009-03-04 08:52:31 +00001160 }
Owen Anderson7d211e22008-12-31 02:00:25 +00001161
Evan Chengae7fa5b2008-10-28 01:48:24 +00001162 ++NumSplits;
Lang Hames233a60e2009-11-03 23:52:08 +00001163 DEBUG(errs() << "success.\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001164 return true;
1165}
1166
1167/// SplitRegLiveIntervals - Split all register live intervals that cross the
1168/// barrier that's being processed.
1169bool
Owen Anderson956ec272009-01-23 00:23:32 +00001170PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1171 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001172 // First find all the virtual registers whose live intervals are intercepted
1173 // by the current barrier.
1174 SmallVector<LiveInterval*, 8> Intervals;
1175 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng4350eb82009-02-06 17:17:30 +00001176 // FIXME: If it's not safe to move any instruction that defines the barrier
1177 // register class, then it means there are some special dependencies which
1178 // codegen is not modelling. Ignore these barriers for now.
1179 if (!TII->isSafeToMoveRegClassDefs(*RC))
Evan Cheng23066282008-10-27 07:14:50 +00001180 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001181 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
1182 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1183 unsigned Reg = VRs[i];
1184 if (!LIs->hasInterval(Reg))
1185 continue;
1186 LiveInterval *LI = &LIs->getInterval(Reg);
1187 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1188 // Virtual register live interval is intercepted by the barrier. We
1189 // should split and shrink wrap its interval if possible.
1190 Intervals.push_back(LI);
1191 }
1192 }
1193
1194 // Process the affected live intervals.
1195 bool Change = false;
1196 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001197 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1198 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001199 LiveInterval *LI = Intervals.back();
1200 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001201 bool result = SplitRegLiveInterval(LI);
1202 if (result) Split.insert(LI);
1203 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001204 }
1205
1206 return Change;
1207}
1208
Owen Anderson45e68552009-01-29 05:28:55 +00001209unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001210 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001211 unsigned Reg, int FrameIndex,
1212 bool& FeedsTwoAddr) {
1213 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001214 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001215 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001216 int StoreFrameIndex;
1217 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001218 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
1219 NonSpills++;
1220
1221 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
Bob Wilsond9df5012009-04-09 17:16:43 +00001222 if (DefIdx != -1 && (*UI)->isRegTiedToUseOperand(DefIdx))
Owen Anderson45e68552009-01-29 05:28:55 +00001223 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001224 }
1225
Owen Anderson45e68552009-01-29 05:28:55 +00001226 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001227}
1228
Owen Anderson956ec272009-01-23 00:23:32 +00001229/// removeDeadSpills - After doing splitting, filter through all intervals we've
1230/// split, and see if any of the spills are unnecessary. If so, remove them.
1231bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1232 bool changed = false;
1233
Owen Anderson4bfc2092009-01-29 05:41:02 +00001234 // Walk over all of the live intervals that were touched by the splitter,
1235 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001236 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1237 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001238 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001239
Owen Anderson4bfc2092009-01-29 05:41:02 +00001240 // First, collect all the uses of the vreg, and sort them by their
1241 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001242 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1243 UE = MRI->use_end(); UI != UE; ++UI) {
Lang Hames233a60e2009-11-03 23:52:08 +00001244 SlotIndex index = LIs->getInstructionIndex(&*UI);
1245 index = index.getUseIndex();
Owen Anderson956ec272009-01-23 00:23:32 +00001246
1247 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001248 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001249 }
1250
Owen Anderson4bfc2092009-01-29 05:41:02 +00001251 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1252 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001253 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1254 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001255
1256 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1257 return changed;
1258
Owen Anderson956ec272009-01-23 00:23:32 +00001259 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001260
1261 // We don't currently try to handle definitions with PHI kills, because
1262 // it would involve processing more than one VNInfo at once.
Lang Hames857c4e02009-06-17 21:01:20 +00001263 if (CurrVN->hasPHIKill()) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001264
Owen Anderson4bfc2092009-01-29 05:41:02 +00001265 // We also don't try to handle the results of PHI joins, since there's
1266 // no defining instruction to analyze.
Lang Hames857c4e02009-06-17 21:01:20 +00001267 if (!CurrVN->isDefAccurate() || CurrVN->isUnused()) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001268
Owen Anderson4bfc2092009-01-29 05:41:02 +00001269 // We're only interested in eliminating cruft introduced by the splitter,
1270 // is of the form load-use or load-use-store. First, check that the
1271 // definition is a load, and remember what stack slot we loaded it from.
Lang Hames857c4e02009-06-17 21:01:20 +00001272 MachineInstr* DefMI = LIs->getInstructionFromIndex(CurrVN->def);
Owen Anderson956ec272009-01-23 00:23:32 +00001273 int FrameIndex;
1274 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1275
Owen Anderson4bfc2092009-01-29 05:41:02 +00001276 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001277 if (VNUseCount[CurrVN].size() == 0) {
1278 LIs->RemoveMachineInstrFromMaps(DefMI);
1279 (*LI)->removeValNo(CurrVN);
1280 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001281 VNUseCount.erase(CurrVN);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001282 NumDeadSpills++;
1283 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001284 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001285 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001286
Owen Anderson4bfc2092009-01-29 05:41:02 +00001287 // Second, get the number of non-store uses of the definition, as well as
1288 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001289 bool FeedsTwoAddr = false;
1290 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1291 (*LI)->reg, FrameIndex,
1292 FeedsTwoAddr);
1293
Owen Anderson4bfc2092009-01-29 05:41:02 +00001294 // If there's one non-store use and it doesn't feed a two-addr, then
1295 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001296 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001297 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001298 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1299 int StoreFrameIndex;
1300 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1301 while (UI != VNUseCount[CurrVN].end() &&
1302 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1303 ++UI;
1304 if (UI != VNUseCount[CurrVN].end())
1305 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1306 }
Owen Anderson45e68552009-01-29 05:28:55 +00001307 if (UI == VNUseCount[CurrVN].end()) continue;
1308
1309 MachineInstr* use = *UI;
1310
Owen Anderson4bfc2092009-01-29 05:41:02 +00001311 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001312 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1313 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001314 SmallVector<unsigned, 1> Ops;
1315 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001316 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1317
1318 MachineInstr* NewMI =
1319 TII->foldMemoryOperand(*use->getParent()->getParent(),
1320 use, Ops, FrameIndex);
1321
1322 if (!NewMI) continue;
1323
Owen Anderson4bfc2092009-01-29 05:41:02 +00001324 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001325 LIs->RemoveMachineInstrFromMaps(DefMI);
1326 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1327 (*LI)->removeValNo(CurrVN);
1328
1329 DefMI->eraseFromParent();
1330 MachineBasicBlock* MBB = use->getParent();
1331 NewMI = MBB->insert(MBB->erase(use), NewMI);
1332 VNUseCount[CurrVN].erase(use);
1333
Owen Anderson4bfc2092009-01-29 05:41:02 +00001334 // Remove deleted instructions. Note that we need to remove them from
1335 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001336 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1337 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1338 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001339 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001340 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1341 ++VNI)
1342 if (VNI->first != CurrVN)
1343 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001344 LIs->RemoveMachineInstrFromMaps(*II);
1345 (*II)->eraseFromParent();
1346 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001347
1348 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001349
1350 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1351 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1352 if (VI->second.erase(use))
1353 VI->second.insert(NewMI);
1354
1355 NumDeadSpills++;
1356 changed = true;
1357 continue;
1358 }
1359
Owen Anderson4bfc2092009-01-29 05:41:02 +00001360 // If there's more than one non-store instruction, we can't profitably
1361 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001362 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001363
Owen Anderson4bfc2092009-01-29 05:41:02 +00001364 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001365 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1366 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
Lang Hamesf035ce52009-11-12 01:24:08 +00001367 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001368 LIs->RemoveMachineInstrFromMaps(*UI);
1369 (*UI)->eraseFromParent();
1370 }
1371
Owen Andersonc0f3a032009-01-29 08:22:06 +00001372 VNUseCount.erase(CurrVN);
1373
Owen Anderson32ca8652009-01-24 10:07:43 +00001374 LIs->RemoveMachineInstrFromMaps(DefMI);
1375 (*LI)->removeValNo(CurrVN);
1376 DefMI->eraseFromParent();
1377 NumDeadSpills++;
1378 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001379 }
1380 }
1381
1382 return changed;
1383}
1384
Owen Andersonf1f75b12008-11-04 22:22:41 +00001385bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1386 MachineBasicBlock* DefMBB,
1387 MachineBasicBlock* BarrierMBB) {
1388 if (DefMBB == BarrierMBB)
1389 return false;
1390
Lang Hames857c4e02009-06-17 21:01:20 +00001391 if (LR->valno->hasPHIKill())
Owen Andersonf1f75b12008-11-04 22:22:41 +00001392 return false;
1393
Lang Hames233a60e2009-11-03 23:52:08 +00001394 SlotIndex MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
Owen Andersonf1f75b12008-11-04 22:22:41 +00001395 if (LR->end < MBBEnd)
1396 return false;
1397
1398 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1399 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1400 return true;
1401
1402 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1403 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1404 typedef std::pair<MachineBasicBlock*,
1405 MachineBasicBlock::succ_iterator> ItPair;
1406 SmallVector<ItPair, 4> Stack;
1407 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1408
1409 while (!Stack.empty()) {
1410 ItPair P = Stack.back();
1411 Stack.pop_back();
1412
1413 MachineBasicBlock* PredMBB = P.first;
1414 MachineBasicBlock::succ_iterator S = P.second;
1415
1416 if (S == PredMBB->succ_end())
1417 continue;
1418 else if (Visited.count(*S)) {
1419 Stack.push_back(std::make_pair(PredMBB, ++S));
1420 continue;
1421 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001422 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001423
1424 MachineBasicBlock* MBB = *S;
1425 Visited.insert(MBB);
1426
1427 if (MBB == BarrierMBB)
1428 return true;
1429
1430 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1431 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1432 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1433 while (MDTN) {
1434 if (MDTN == DefMDTN)
1435 return true;
1436 else if (MDTN == BarrierMDTN)
1437 break;
1438 MDTN = MDTN->getIDom();
1439 }
1440
1441 MBBEnd = LIs->getMBBEndIdx(MBB);
1442 if (LR->end > MBBEnd)
1443 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1444 }
1445
1446 return false;
1447}
1448
1449
Evan Cheng09e8ca82008-10-20 21:44:59 +00001450bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001451 CurrMF = &MF;
1452 TM = &MF.getTarget();
Owen Anderson3ef45492009-01-29 22:13:06 +00001453 TRI = TM->getRegisterInfo();
Evan Chengd0e32c52008-10-29 05:06:14 +00001454 TII = TM->getInstrInfo();
1455 MFI = MF.getFrameInfo();
1456 MRI = &MF.getRegInfo();
Lang Hames233a60e2009-11-03 23:52:08 +00001457 SIs = &getAnalysis<SlotIndexes>();
Evan Chengd0e32c52008-10-29 05:06:14 +00001458 LIs = &getAnalysis<LiveIntervals>();
1459 LSs = &getAnalysis<LiveStacks>();
Owen Anderson420dd372009-03-14 21:40:05 +00001460 VRM = &getAnalysis<VirtRegMap>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001461
1462 bool MadeChange = false;
1463
1464 // Make sure blocks are numbered in order.
1465 MF.RenumberBlocks();
1466
Evan Cheng54898932008-10-29 08:39:34 +00001467 MachineBasicBlock *Entry = MF.begin();
1468 SmallPtrSet<MachineBasicBlock*,16> Visited;
1469
Owen Anderson956ec272009-01-23 00:23:32 +00001470 SmallPtrSet<LiveInterval*, 8> Split;
1471
Evan Cheng54898932008-10-29 08:39:34 +00001472 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1473 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1474 DFI != E; ++DFI) {
1475 BarrierMBB = *DFI;
1476 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1477 E = BarrierMBB->end(); I != E; ++I) {
1478 Barrier = &*I;
1479 const TargetRegisterClass **BarrierRCs =
1480 Barrier->getDesc().getRegClassBarriers();
1481 if (!BarrierRCs)
1482 continue;
1483 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001484 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001485 }
1486 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001487
Owen Anderson956ec272009-01-23 00:23:32 +00001488 MadeChange |= removeDeadSpills(Split);
1489
Evan Chengf5cd4f02008-10-23 20:43:13 +00001490 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001491}