blob: f7c2e80c715d90297f86348f8a1707d81fd202fa [file] [log] [blame]
Evan Cheng09e8ca82008-10-20 21:44:59 +00001//===-- PreAllocSplitting.cpp - Pre-allocation Interval Spltting Pass. ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the machine instruction level pre-register allocation
11// live interval splitting pass. It finds live interval barriers, i.e.
12// instructions which will kill all physical registers in certain register
13// classes, and split all live intervals which cross the barrier.
14//
15//===----------------------------------------------------------------------===//
16
17#define DEBUG_TYPE "pre-alloc-split"
Owen Anderson420dd372009-03-14 21:40:05 +000018#include "VirtRegMap.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000020#include "llvm/CodeGen/LiveStackAnalysis.h"
Owen Andersonf1f75b12008-11-04 22:22:41 +000021#include "llvm/CodeGen/MachineDominators.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineLoopInfo.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000028#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000029#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetOptions.h"
31#include "llvm/Target/TargetRegisterInfo.h"
32#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000035#include "llvm/ADT/DenseMap.h"
Evan Cheng54898932008-10-29 08:39:34 +000036#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000037#include "llvm/ADT/SmallPtrSet.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000038#include "llvm/ADT/Statistic.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000039using namespace llvm;
40
Evan Chengae7fa5b2008-10-28 01:48:24 +000041static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden);
Evan Cheng63040ce2009-11-09 06:49:22 +000042static cl::opt<int> DeadSplitLimit("dead-split-limit", cl::init(-1),
43 cl::Hidden);
44static cl::opt<int> RestoreFoldLimit("restore-fold-limit", cl::init(-1),
45 cl::Hidden);
Evan Chengae7fa5b2008-10-28 01:48:24 +000046
Owen Anderson45e68552009-01-29 05:28:55 +000047STATISTIC(NumSplits, "Number of intervals split");
Owen Anderson75fa96b2008-11-19 04:28:29 +000048STATISTIC(NumRemats, "Number of intervals split by rematerialization");
Owen Anderson7b9d67c2008-12-02 18:53:47 +000049STATISTIC(NumFolds, "Number of intervals split with spill folding");
Owen Anderson323c58d2009-03-05 07:19:18 +000050STATISTIC(NumRestoreFolds, "Number of intervals split with restore folding");
Owen Anderson2ebf63f2008-12-18 01:27:19 +000051STATISTIC(NumRenumbers, "Number of intervals renumbered into new registers");
Owen Anderson956ec272009-01-23 00:23:32 +000052STATISTIC(NumDeadSpills, "Number of dead spills removed");
Evan Chengf5cd4f02008-10-23 20:43:13 +000053
Evan Cheng09e8ca82008-10-20 21:44:59 +000054namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000055 class PreAllocSplitting : public MachineFunctionPass {
Evan Chengd0e32c52008-10-29 05:06:14 +000056 MachineFunction *CurrMF;
Evan Chengf5cd4f02008-10-23 20:43:13 +000057 const TargetMachine *TM;
58 const TargetInstrInfo *TII;
Owen Anderson3ef45492009-01-29 22:13:06 +000059 const TargetRegisterInfo* TRI;
Evan Chengf5cd4f02008-10-23 20:43:13 +000060 MachineFrameInfo *MFI;
61 MachineRegisterInfo *MRI;
Lang Hames233a60e2009-11-03 23:52:08 +000062 SlotIndexes *SIs;
Evan Chengf5cd4f02008-10-23 20:43:13 +000063 LiveIntervals *LIs;
Evan Chengd0e32c52008-10-29 05:06:14 +000064 LiveStacks *LSs;
Owen Anderson420dd372009-03-14 21:40:05 +000065 VirtRegMap *VRM;
Evan Cheng09e8ca82008-10-20 21:44:59 +000066
Evan Chengf5cd4f02008-10-23 20:43:13 +000067 // Barrier - Current barrier being processed.
68 MachineInstr *Barrier;
69
70 // BarrierMBB - Basic block where the barrier resides in.
71 MachineBasicBlock *BarrierMBB;
72
73 // Barrier - Current barrier index.
Lang Hames233a60e2009-11-03 23:52:08 +000074 SlotIndex BarrierIdx;
Evan Chengf5cd4f02008-10-23 20:43:13 +000075
76 // CurrLI - Current live interval being split.
77 LiveInterval *CurrLI;
78
Evan Chengd0e32c52008-10-29 05:06:14 +000079 // CurrSLI - Current stack slot live interval.
80 LiveInterval *CurrSLI;
81
82 // CurrSValNo - Current val# for the stack slot live interval.
83 VNInfo *CurrSValNo;
84
85 // IntervalSSMap - A map from live interval to spill slots.
86 DenseMap<unsigned, int> IntervalSSMap;
Evan Chengf5cd4f02008-10-23 20:43:13 +000087
Evan Cheng54898932008-10-29 08:39:34 +000088 // Def2SpillMap - A map from a def instruction index to spill index.
Lang Hames233a60e2009-11-03 23:52:08 +000089 DenseMap<SlotIndex, SlotIndex> Def2SpillMap;
Evan Cheng06587492008-10-24 02:05:00 +000090
Evan Cheng09e8ca82008-10-20 21:44:59 +000091 public:
92 static char ID;
Lang Hames233a60e2009-11-03 23:52:08 +000093 PreAllocSplitting()
94 : MachineFunctionPass(&ID) {}
Evan Cheng09e8ca82008-10-20 21:44:59 +000095
96 virtual bool runOnMachineFunction(MachineFunction &MF);
97
98 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000099 AU.setPreservesCFG();
Lang Hames233a60e2009-11-03 23:52:08 +0000100 AU.addRequired<SlotIndexes>();
101 AU.addPreserved<SlotIndexes>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000102 AU.addRequired<LiveIntervals>();
103 AU.addPreserved<LiveIntervals>();
Evan Chengd0e32c52008-10-29 05:06:14 +0000104 AU.addRequired<LiveStacks>();
105 AU.addPreserved<LiveStacks>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000106 AU.addPreserved<RegisterCoalescer>();
107 if (StrongPHIElim)
108 AU.addPreservedID(StrongPHIEliminationID);
109 else
110 AU.addPreservedID(PHIEliminationID);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000111 AU.addRequired<MachineDominatorTree>();
112 AU.addRequired<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000113 AU.addRequired<VirtRegMap>();
Owen Andersonf1f75b12008-11-04 22:22:41 +0000114 AU.addPreserved<MachineDominatorTree>();
115 AU.addPreserved<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000116 AU.addPreserved<VirtRegMap>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000117 MachineFunctionPass::getAnalysisUsage(AU);
118 }
119
120 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +0000121 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +0000122 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000123 }
124
125 virtual const char *getPassName() const {
126 return "Pre-Register Allocaton Live Interval Splitting";
127 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000128
129 /// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000130 virtual void print(raw_ostream &O, const Module* M = 0) const {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000131 LIs->print(O, M);
132 }
133
Evan Chengf5cd4f02008-10-23 20:43:13 +0000134
135 private:
Evan Chengf5cd4f02008-10-23 20:43:13 +0000136
137 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000138 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Lang Hamesb3661582009-11-14 00:02:51 +0000139 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000140
141 MachineBasicBlock::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000142 findRestorePoint(MachineBasicBlock*, MachineInstr*, SlotIndex,
Lang Hamesb3661582009-11-14 00:02:51 +0000143 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000144
Evan Chengd0e32c52008-10-29 05:06:14 +0000145 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000146
Lang Hames86511252009-09-04 20:41:11 +0000147 bool IsAvailableInStack(MachineBasicBlock*, unsigned,
Lang Hames233a60e2009-11-03 23:52:08 +0000148 SlotIndex, SlotIndex,
149 SlotIndex&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000150
Lang Hames233a60e2009-11-03 23:52:08 +0000151 void UpdateSpillSlotInterval(VNInfo*, SlotIndex, SlotIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000152
Evan Chengf5cd4f02008-10-23 20:43:13 +0000153 bool SplitRegLiveInterval(LiveInterval*);
154
Owen Anderson956ec272009-01-23 00:23:32 +0000155 bool SplitRegLiveIntervals(const TargetRegisterClass **,
156 SmallPtrSet<LiveInterval*, 8>&);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000157
158 bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB,
159 MachineBasicBlock* BarrierMBB);
Owen Anderson75fa96b2008-11-19 04:28:29 +0000160 bool Rematerialize(unsigned vreg, VNInfo* ValNo,
161 MachineInstr* DefMI,
162 MachineBasicBlock::iterator RestorePt,
Owen Anderson75fa96b2008-11-19 04:28:29 +0000163 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000164 MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC,
165 MachineInstr* DefMI,
166 MachineInstr* Barrier,
167 MachineBasicBlock* MBB,
168 int& SS,
169 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersonc93023a2009-03-04 08:52:31 +0000170 MachineInstr* FoldRestore(unsigned vreg,
171 const TargetRegisterClass* RC,
172 MachineInstr* Barrier,
173 MachineBasicBlock* MBB,
174 int SS,
175 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000176 void RenumberValno(VNInfo* VN);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000177 void ReconstructLiveInterval(LiveInterval* LI);
Owen Anderson956ec272009-01-23 00:23:32 +0000178 bool removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split);
Owen Anderson45e68552009-01-29 05:28:55 +0000179 unsigned getNumberOfNonSpills(SmallPtrSet<MachineInstr*, 4>& MIs,
180 unsigned Reg, int FrameIndex, bool& TwoAddr);
Evan Cheng19a72582009-02-02 18:33:18 +0000181 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator Use,
182 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000183 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000184 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
185 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
186 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000187 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
188 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000189 bool IsTopLevel, bool IsIntraBlock);
190 VNInfo* PerformPHIConstructionFallBack(MachineBasicBlock::iterator Use,
191 MachineBasicBlock* MBB, LiveInterval* LI,
192 SmallPtrSet<MachineInstr*, 4>& Visited,
193 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
194 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
195 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
196 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
197 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
198 bool IsTopLevel, bool IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000199};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000200} // end anonymous namespace
201
202char PreAllocSplitting::ID = 0;
203
204static RegisterPass<PreAllocSplitting>
205X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
206
207const PassInfo *const llvm::PreAllocSplittingID = &X;
208
Evan Chengf5cd4f02008-10-23 20:43:13 +0000209/// findSpillPoint - Find a gap as far away from the given MI that's suitable
210/// for spilling the current live interval. The index must be before any
211/// defs and uses of the live interval register in the mbb. Return begin() if
212/// none is found.
213MachineBasicBlock::iterator
214PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000215 MachineInstr *DefMI,
Lang Hamesb3661582009-11-14 00:02:51 +0000216 SmallPtrSet<MachineInstr*, 4> &RefsInMBB) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000217 MachineBasicBlock::iterator Pt = MBB->begin();
218
Owen Anderson696a1302009-03-31 08:27:09 +0000219 MachineBasicBlock::iterator MII = MI;
220 MachineBasicBlock::iterator EndPt = DefMI
221 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
Owen Anderson4cafbb52009-02-20 10:02:23 +0000222
Owen Anderson696a1302009-03-31 08:27:09 +0000223 while (MII != EndPt && !RefsInMBB.count(MII) &&
224 MII->getOpcode() != TRI->getCallFrameSetupOpcode())
225 --MII;
226 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson4cafbb52009-02-20 10:02:23 +0000227
Owen Anderson696a1302009-03-31 08:27:09 +0000228 while (MII != EndPt && !RefsInMBB.count(MII)) {
Owen Anderson696a1302009-03-31 08:27:09 +0000229 // We can't insert the spill between the barrier (a call), and its
230 // corresponding call frame setup.
231 if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
232 while (MII->getOpcode() != TRI->getCallFrameSetupOpcode()) {
Owen Anderson5734c942009-02-05 05:58:41 +0000233 --MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000234 if (MII == EndPt) {
235 return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000236 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000237 }
Owen Anderson696a1302009-03-31 08:27:09 +0000238 continue;
Lang Hamesb3661582009-11-14 00:02:51 +0000239 } else {
Owen Anderson696a1302009-03-31 08:27:09 +0000240 Pt = MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000241 }
Owen Anderson696a1302009-03-31 08:27:09 +0000242
243 if (RefsInMBB.count(MII))
244 return Pt;
245
246
247 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000248 }
249
250 return Pt;
251}
252
253/// findRestorePoint - Find a gap in the instruction index map that's suitable
254/// for restoring the current live interval value. The index must be before any
255/// uses of the live interval register in the mbb. Return end() if none is
256/// found.
257MachineBasicBlock::iterator
258PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000259 SlotIndex LastIdx,
Lang Hamesb3661582009-11-14 00:02:51 +0000260 SmallPtrSet<MachineInstr*, 4> &RefsInMBB) {
Evan Cheng54898932008-10-29 08:39:34 +0000261 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
262 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000263 MachineBasicBlock::iterator Pt = MBB->end();
Owen Anderson696a1302009-03-31 08:27:09 +0000264 MachineBasicBlock::iterator EndPt = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000265
Owen Anderson696a1302009-03-31 08:27:09 +0000266 // We start at the call, so walk forward until we find the call frame teardown
267 // since we can't insert restores before that. Bail if we encounter a use
268 // during this time.
269 MachineBasicBlock::iterator MII = MI;
270 if (MII == EndPt) return Pt;
271
272 while (MII != EndPt && !RefsInMBB.count(MII) &&
273 MII->getOpcode() != TRI->getCallFrameDestroyOpcode())
274 ++MII;
275 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
276 ++MII;
277
278 // FIXME: Limit the number of instructions to examine to reduce
279 // compile time?
280 while (MII != EndPt) {
Lang Hames233a60e2009-11-03 23:52:08 +0000281 SlotIndex Index = LIs->getInstructionIndex(MII);
Owen Anderson696a1302009-03-31 08:27:09 +0000282 if (Index > LastIdx)
283 break;
Owen Anderson3ef45492009-01-29 22:13:06 +0000284
Owen Anderson696a1302009-03-31 08:27:09 +0000285 // We can't insert a restore between the barrier (a call) and its
286 // corresponding call frame teardown.
287 if (MII->getOpcode() == TRI->getCallFrameSetupOpcode()) {
288 do {
289 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000290 ++MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000291 } while (MII->getOpcode() != TRI->getCallFrameDestroyOpcode());
Lang Hamesb3661582009-11-14 00:02:51 +0000292 } else {
Owen Anderson696a1302009-03-31 08:27:09 +0000293 Pt = MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000294 }
Owen Anderson696a1302009-03-31 08:27:09 +0000295
296 if (RefsInMBB.count(MII))
297 return Pt;
298
299 ++MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000300 }
301
302 return Pt;
303}
304
Evan Chengd0e32c52008-10-29 05:06:14 +0000305/// CreateSpillStackSlot - Create a stack slot for the live interval being
306/// split. If the live interval was previously split, just reuse the same
307/// slot.
308int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
309 const TargetRegisterClass *RC) {
310 int SS;
311 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
312 if (I != IntervalSSMap.end()) {
313 SS = I->second;
314 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000315 SS = MFI->CreateSpillStackObject(RC->getSize(), RC->getAlignment());
Evan Chengd0e32c52008-10-29 05:06:14 +0000316 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000317 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000318
319 // Create live interval for stack slot.
Evan Chengc781a242009-05-03 18:32:42 +0000320 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Evan Cheng54898932008-10-29 08:39:34 +0000321 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000322 CurrSValNo = CurrSLI->getValNumInfo(0);
323 else
Lang Hames233a60e2009-11-03 23:52:08 +0000324 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000325 LSs->getVNInfoAllocator());
Evan Chengd0e32c52008-10-29 05:06:14 +0000326 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000327}
328
Evan Chengd0e32c52008-10-29 05:06:14 +0000329/// IsAvailableInStack - Return true if register is available in a split stack
330/// slot at the specified index.
331bool
Evan Cheng54898932008-10-29 08:39:34 +0000332PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000333 unsigned Reg, SlotIndex DefIndex,
334 SlotIndex RestoreIndex,
335 SlotIndex &SpillIndex,
Evan Cheng54898932008-10-29 08:39:34 +0000336 int& SS) const {
337 if (!DefMBB)
338 return false;
339
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000340 DenseMap<unsigned, int>::const_iterator I = IntervalSSMap.find(Reg);
Evan Chengd0e32c52008-10-29 05:06:14 +0000341 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000342 return false;
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000343 DenseMap<SlotIndex, SlotIndex>::const_iterator
Lang Hames86511252009-09-04 20:41:11 +0000344 II = Def2SpillMap.find(DefIndex);
Evan Cheng54898932008-10-29 08:39:34 +0000345 if (II == Def2SpillMap.end())
346 return false;
347
348 // If last spill of def is in the same mbb as barrier mbb (where restore will
349 // be), make sure it's not below the intended restore index.
350 // FIXME: Undo the previous spill?
351 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
352 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
353 return false;
354
355 SS = I->second;
356 SpillIndex = II->second;
357 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000358}
359
Evan Chengd0e32c52008-10-29 05:06:14 +0000360/// UpdateSpillSlotInterval - Given the specified val# of the register live
361/// interval being split, and the spill and restore indicies, update the live
362/// interval of the spill stack slot.
363void
Lang Hames233a60e2009-11-03 23:52:08 +0000364PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, SlotIndex SpillIndex,
365 SlotIndex RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000366 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
367 "Expect restore in the barrier mbb");
368
369 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
370 if (MBB == BarrierMBB) {
371 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000372 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
373 CurrSLI->addRange(SLR);
374 return;
375 }
376
Evan Cheng54898932008-10-29 08:39:34 +0000377 SmallPtrSet<MachineBasicBlock*, 4> Processed;
Lang Hames233a60e2009-11-03 23:52:08 +0000378 SlotIndex EndIdx = LIs->getMBBEndIdx(MBB);
379 LiveRange SLR(SpillIndex, EndIdx.getNextSlot(), CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000380 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000381 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000382
383 // Start from the spill mbb, figure out the extend of the spill slot's
384 // live interval.
385 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000386 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
387 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000388 // If live range extend beyond end of mbb, add successors to work list.
389 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
390 SE = MBB->succ_end(); SI != SE; ++SI)
391 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000392
393 while (!WorkList.empty()) {
394 MachineBasicBlock *MBB = WorkList.back();
395 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000396 if (Processed.count(MBB))
397 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000398 SlotIndex Idx = LIs->getMBBStartIdx(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000399 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000400 if (LR && LR->valno == ValNo) {
401 EndIdx = LIs->getMBBEndIdx(MBB);
402 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000403 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000404 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000405 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000406 } else if (LR->end > EndIdx) {
407 // Live range extends beyond end of mbb, process successors.
Lang Hames233a60e2009-11-03 23:52:08 +0000408 LiveRange SLR(Idx, EndIdx.getNextIndex(), CurrSValNo);
Evan Cheng54898932008-10-29 08:39:34 +0000409 CurrSLI->addRange(SLR);
410 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
411 SE = MBB->succ_end(); SI != SE; ++SI)
412 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000413 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000414 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000415 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000416 }
Evan Cheng54898932008-10-29 08:39:34 +0000417 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000418 }
419 }
420}
421
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000422/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
423/// construction algorithm to compute the ranges and valnos for an interval.
Evan Cheng19a72582009-02-02 18:33:18 +0000424VNInfo*
425PreAllocSplitting::PerformPHIConstruction(MachineBasicBlock::iterator UseI,
426 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000427 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000428 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
429 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
430 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000431 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
432 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000433 bool IsTopLevel, bool IsIntraBlock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000434 // Return memoized result if it's available.
Evan Cheng19a72582009-02-02 18:33:18 +0000435 if (IsTopLevel && Visited.count(UseI) && NewVNs.count(UseI))
436 return NewVNs[UseI];
437 else if (!IsTopLevel && IsIntraBlock && NewVNs.count(UseI))
438 return NewVNs[UseI];
439 else if (!IsIntraBlock && LiveOut.count(MBB))
Owen Anderson7d211e22008-12-31 02:00:25 +0000440 return LiveOut[MBB];
441
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000442 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000443 bool ContainsDefs = Defs.count(MBB);
444 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000445
Evan Cheng19a72582009-02-02 18:33:18 +0000446 VNInfo* RetVNI = 0;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000447
448 // Enumerate the cases of use/def contaning blocks.
449 if (!ContainsDefs && !ContainsUses) {
Evan Cheng19a72582009-02-02 18:33:18 +0000450 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
451 NewVNs, LiveOut, Phis,
452 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000453 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000454 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000455
456 // Search for the def in this block. If we don't find it before the
457 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000458 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000459 // always be an end() iterator.
Evan Cheng19a72582009-02-02 18:33:18 +0000460 assert(UseI == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000461
Evan Cheng19a72582009-02-02 18:33:18 +0000462 MachineBasicBlock::iterator Walker = UseI;
463 --Walker;
464 while (Walker != MBB->begin()) {
465 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000466 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000467 --Walker;
468 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000469
470 // Once we've found it, extend its VNInfo to our instruction.
Lang Hames233a60e2009-11-03 23:52:08 +0000471 SlotIndex DefIndex = LIs->getInstructionIndex(Walker);
472 DefIndex = DefIndex.getDefIndex();
473 SlotIndex EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000474
Evan Cheng19a72582009-02-02 18:33:18 +0000475 RetVNI = NewVNs[Walker];
Lang Hames233a60e2009-11-03 23:52:08 +0000476 LI->addRange(LiveRange(DefIndex, EndIndex.getNextSlot(), RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000477 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000478 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000479
480 // Search for the use in this block that precedes the instruction we care
Evan Cheng19a72582009-02-02 18:33:18 +0000481 // about, going to the fallback case if we don't find it.
482 if (UseI == MBB->begin())
483 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
484 Uses, NewVNs, LiveOut, Phis,
485 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000486
Evan Cheng19a72582009-02-02 18:33:18 +0000487 MachineBasicBlock::iterator Walker = UseI;
488 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000489 bool found = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000490 while (Walker != MBB->begin()) {
491 if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000492 found = true;
493 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000494 }
495 --Walker;
496 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000497
498 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000499 if (!found) {
Evan Cheng19a72582009-02-02 18:33:18 +0000500 if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000501 found = true;
502 else
Evan Cheng19a72582009-02-02 18:33:18 +0000503 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
504 Uses, NewVNs, LiveOut, Phis,
505 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000506 }
507
Lang Hames233a60e2009-11-03 23:52:08 +0000508 SlotIndex UseIndex = LIs->getInstructionIndex(Walker);
509 UseIndex = UseIndex.getUseIndex();
510 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000511 if (IsIntraBlock) {
512 EndIndex = LIs->getInstructionIndex(UseI);
Lang Hames233a60e2009-11-03 23:52:08 +0000513 EndIndex = EndIndex.getUseIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000514 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000515 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000516
517 // Now, recursively phi construct the VNInfo for the use we found,
518 // and then extend it to include the instruction we care about
Evan Cheng19a72582009-02-02 18:33:18 +0000519 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
520 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000521
Lang Hames233a60e2009-11-03 23:52:08 +0000522 LI->addRange(LiveRange(UseIndex, EndIndex.getNextSlot(), RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000523
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000524 // FIXME: Need to set kills properly for inter-block stuff.
Lang Hames86511252009-09-04 20:41:11 +0000525 if (RetVNI->isKill(UseIndex)) RetVNI->removeKill(UseIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000526 if (IsIntraBlock)
Lang Hames86511252009-09-04 20:41:11 +0000527 RetVNI->addKill(EndIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000528 } else if (ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000529 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
530 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000531
532 // This case is basically a merging of the two preceding case, with the
533 // special note that checking for defs must take precedence over checking
534 // for uses, because of two-address instructions.
535
Evan Cheng19a72582009-02-02 18:33:18 +0000536 if (UseI == MBB->begin())
537 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
538 NewVNs, LiveOut, Phis,
539 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000540
Evan Cheng19a72582009-02-02 18:33:18 +0000541 MachineBasicBlock::iterator Walker = UseI;
542 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000543 bool foundDef = false;
544 bool foundUse = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000545 while (Walker != MBB->begin()) {
546 if (BlockDefs.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000547 foundDef = true;
548 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000549 } else if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000550 foundUse = true;
551 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000552 }
553 --Walker;
554 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000555
556 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000557 if (!foundDef && !foundUse) {
Evan Cheng19a72582009-02-02 18:33:18 +0000558 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000559 foundDef = true;
Evan Cheng19a72582009-02-02 18:33:18 +0000560 else if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000561 foundUse = true;
562 else
Evan Cheng19a72582009-02-02 18:33:18 +0000563 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
564 Uses, NewVNs, LiveOut, Phis,
565 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000566 }
567
Lang Hames233a60e2009-11-03 23:52:08 +0000568 SlotIndex StartIndex = LIs->getInstructionIndex(Walker);
569 StartIndex = foundDef ? StartIndex.getDefIndex() : StartIndex.getUseIndex();
570 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000571 if (IsIntraBlock) {
572 EndIndex = LIs->getInstructionIndex(UseI);
Lang Hames233a60e2009-11-03 23:52:08 +0000573 EndIndex = EndIndex.getUseIndex();
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 Hames233a60e2009-11-03 23:52:08 +0000583 LI->addRange(LiveRange(StartIndex, EndIndex.getNextSlot(), 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 Hames233a60e2009-11-03 23:52:08 +0000664 SlotIndex KillIndex = LIs->getMBBEndIdx(I->first);
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) {
672 EndIndex = LIs->getInstructionIndex(UseI);
Lang Hames233a60e2009-11-03 23:52:08 +0000673 EndIndex = EndIndex.getUseIndex();
Evan Cheng19a72582009-02-02 18:33:18 +0000674 } else
675 EndIndex = LIs->getMBBEndIdx(MBB);
Lang Hames233a60e2009-11-03 23:52:08 +0000676 LI->addRange(LiveRange(StartIndex, EndIndex.getNextSlot(), RetVNI));
Evan Cheng19a72582009-02-02 18:33:18 +0000677 if (IsIntraBlock)
Lang Hames86511252009-09-04 20:41:11 +0000678 RetVNI->addKill(EndIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000679
680 // Memoize results so we don't have to recompute them.
681 if (!IsIntraBlock)
682 LiveOut[MBB] = RetVNI;
683 else {
684 if (!NewVNs.count(UseI))
685 NewVNs[UseI] = RetVNI;
686 Visited.insert(UseI);
687 }
688
689 return RetVNI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000690}
691
692/// ReconstructLiveInterval - Recompute a live interval from scratch.
693void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
694 BumpPtrAllocator& Alloc = LIs->getVNInfoAllocator();
695
696 // Clear the old ranges and valnos;
697 LI->clear();
698
699 // Cache the uses and defs of the register
700 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
701 RegMap Defs, Uses;
702
703 // Keep track of the new VNs we're creating.
704 DenseMap<MachineInstr*, VNInfo*> NewVNs;
705 SmallPtrSet<VNInfo*, 2> PhiVNs;
706
707 // Cache defs, and create a new VNInfo for each def.
708 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
709 DE = MRI->def_end(); DI != DE; ++DI) {
710 Defs[(*DI).getParent()].insert(&*DI);
711
Lang Hames233a60e2009-11-03 23:52:08 +0000712 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
713 DefIdx = DefIdx.getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000714
Lang Hames857c4e02009-06-17 21:01:20 +0000715 assert(DI->getOpcode() != TargetInstrInfo::PHI &&
Lang Hamesb3661582009-11-14 00:02:51 +0000716 "PHI instr in code during pre-alloc splitting.");
Lang Hames857c4e02009-06-17 21:01:20 +0000717 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, true, Alloc);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000718
719 // If the def is a move, set the copy field.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000720 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
721 if (TII->isMoveInstr(*DI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
722 if (DstReg == LI->reg)
Lang Hames52c1afc2009-08-10 23:43:28 +0000723 NewVN->setCopy(&*DI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000724
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000725 NewVNs[&*DI] = NewVN;
726 }
727
728 // Cache uses as a separate pass from actually processing them.
729 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
730 UE = MRI->use_end(); UI != UE; ++UI)
731 Uses[(*UI).getParent()].insert(&*UI);
732
733 // Now, actually process every use and use a phi construction algorithm
734 // to walk from it to its reaching definitions, building VNInfos along
735 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000736 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
737 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000738 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000739 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
740 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000741 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000742 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000743 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000744
745 // Add ranges for dead defs
746 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
747 DE = MRI->def_end(); DI != DE; ++DI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000748 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
749 DefIdx = DefIdx.getDefIndex();
Owen Andersond4f6fe52008-12-28 23:35:13 +0000750
751 if (LI->liveAt(DefIdx)) continue;
752
753 VNInfo* DeadVN = NewVNs[&*DI];
Lang Hames233a60e2009-11-03 23:52:08 +0000754 LI->addRange(LiveRange(DefIdx, DefIdx.getNextSlot(), DeadVN));
Lang Hames86511252009-09-04 20:41:11 +0000755 DeadVN->addKill(DefIdx);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000756 }
Evan Cheng35ca9202009-10-09 01:17:11 +0000757
758 // Update kill markers.
759 for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
760 VI != VE; ++VI) {
761 VNInfo* VNI = *VI;
762 for (unsigned i = 0, e = VNI->kills.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +0000763 SlotIndex KillIdx = VNI->kills[i];
764 if (KillIdx.isPHI())
Evan Cheng35ca9202009-10-09 01:17:11 +0000765 continue;
766 MachineInstr *KillMI = LIs->getInstructionFromIndex(KillIdx);
767 if (KillMI) {
768 MachineOperand *KillMO = KillMI->findRegisterUseOperand(CurrLI->reg);
769 if (KillMO)
770 // It could be a dead def.
771 KillMO->setIsKill();
772 }
773 }
774 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000775}
776
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000777/// RenumberValno - Split the given valno out into a new vreg, allowing it to
778/// be allocated to a different register. This function creates a new vreg,
779/// copies the valno and its live ranges over to the new vreg's interval,
780/// removes them from the old interval, and rewrites all uses and defs of
781/// the original reg to the new vreg within those ranges.
782void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000783 SmallVector<VNInfo*, 4> Stack;
784 SmallVector<VNInfo*, 4> VNsToCopy;
785 Stack.push_back(VN);
786
787 // Walk through and copy the valno we care about, and any other valnos
788 // that are two-address redefinitions of the one we care about. These
789 // will need to be rewritten as well. We also check for safety of the
790 // renumbering here, by making sure that none of the valno involved has
791 // phi kills.
792 while (!Stack.empty()) {
793 VNInfo* OldVN = Stack.back();
794 Stack.pop_back();
795
796 // Bail out if we ever encounter a valno that has a PHI kill. We can't
797 // renumber these.
Lang Hames857c4e02009-06-17 21:01:20 +0000798 if (OldVN->hasPHIKill()) return;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000799
800 VNsToCopy.push_back(OldVN);
801
802 // Locate two-address redefinitions
Lang Hamesffd13262009-07-09 03:57:02 +0000803 for (VNInfo::KillSet::iterator KI = OldVN->kills.begin(),
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000804 KE = OldVN->kills.end(); KI != KE; ++KI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000805 assert(!KI->isPHI() &&
Lang Hames86511252009-09-04 20:41:11 +0000806 "VN previously reported having no PHI kills.");
807 MachineInstr* MI = LIs->getInstructionFromIndex(*KI);
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000808 unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg);
809 if (DefIdx == ~0U) continue;
Bob Wilsond9df5012009-04-09 17:16:43 +0000810 if (MI->isRegTiedToUseOperand(DefIdx)) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000811 VNInfo* NextVN =
Lang Hames233a60e2009-11-03 23:52:08 +0000812 CurrLI->findDefinedVNInfoForRegInt(KI->getDefIndex());
Owen Andersonb4b84362009-01-26 21:57:31 +0000813 if (NextVN == OldVN) continue;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000814 Stack.push_back(NextVN);
815 }
816 }
817 }
818
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000819 // Create the new vreg
820 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
821
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000822 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000823 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000824
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000825 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
826 VNsToCopy.end(); OI != OE; ++OI) {
827 VNInfo* OldVN = *OI;
828
829 // Copy the valno over
Lang Hames857c4e02009-06-17 21:01:20 +0000830 VNInfo* NewVN = NewLI.createValueCopy(OldVN, LIs->getVNInfoAllocator());
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000831 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
832
833 // Remove the valno from the old interval
834 CurrLI->removeValNo(OldVN);
835 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000836
837 // Rewrite defs and uses. This is done in two stages to avoid invalidating
838 // the reg_iterator.
839 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
840
841 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
842 E = MRI->reg_end(); I != E; ++I) {
843 MachineOperand& MO = I.getOperand();
Lang Hames233a60e2009-11-03 23:52:08 +0000844 SlotIndex InstrIdx = LIs->getInstructionIndex(&*I);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000845
Lang Hames233a60e2009-11-03 23:52:08 +0000846 if ((MO.isUse() && NewLI.liveAt(InstrIdx.getUseIndex())) ||
847 (MO.isDef() && NewLI.liveAt(InstrIdx.getDefIndex())))
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000848 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
849 }
850
851 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
852 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
853 MachineInstr* Inst = I->first;
854 unsigned OpIdx = I->second;
855 MachineOperand& MO = Inst->getOperand(OpIdx);
856 MO.setReg(NewVReg);
857 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000858
Owen Anderson420dd372009-03-14 21:40:05 +0000859 // Grow the VirtRegMap, since we've created a new vreg.
860 VRM->grow();
861
Owen Anderson45e68552009-01-29 05:28:55 +0000862 // The renumbered vreg shares a stack slot with the old register.
863 if (IntervalSSMap.count(CurrLI->reg))
864 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
865
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000866 NumRenumbers++;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000867}
868
Evan Cheng37844532009-07-16 09:20:10 +0000869bool PreAllocSplitting::Rematerialize(unsigned VReg, VNInfo* ValNo,
Owen Anderson6002e992008-12-04 21:20:30 +0000870 MachineInstr* DefMI,
871 MachineBasicBlock::iterator RestorePt,
Owen Anderson6002e992008-12-04 21:20:30 +0000872 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
873 MachineBasicBlock& MBB = *RestorePt->getParent();
874
875 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
Lang Hames857c4e02009-06-17 21:01:20 +0000876 if (!ValNo->isDefAccurate() || DefMI->getParent() == BarrierMBB)
Lang Hamesb3661582009-11-14 00:02:51 +0000877 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB);
Owen Anderson6002e992008-12-04 21:20:30 +0000878 else
Lang Hamesb3661582009-11-14 00:02:51 +0000879 KillPt = next(MachineBasicBlock::iterator(DefMI));
Owen Anderson6002e992008-12-04 21:20:30 +0000880
881 if (KillPt == DefMI->getParent()->end())
882 return false;
883
Evan Cheng37844532009-07-16 09:20:10 +0000884 TII->reMaterialize(MBB, RestorePt, VReg, 0, DefMI);
Lang Hamesb3661582009-11-14 00:02:51 +0000885 SlotIndex RematIdx = LIs->InsertMachineInstrInMaps(prior(RestorePt));
Owen Anderson6002e992008-12-04 21:20:30 +0000886
Owen Andersonb4b84362009-01-26 21:57:31 +0000887 ReconstructLiveInterval(CurrLI);
Lang Hames233a60e2009-11-03 23:52:08 +0000888 RematIdx = RematIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +0000889 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000890
Owen Anderson75fa96b2008-11-19 04:28:29 +0000891 ++NumSplits;
892 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000893 return true;
894}
895
896MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
897 const TargetRegisterClass* RC,
898 MachineInstr* DefMI,
899 MachineInstr* Barrier,
900 MachineBasicBlock* MBB,
901 int& SS,
902 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
903 MachineBasicBlock::iterator Pt = MBB->begin();
904
905 // Go top down if RefsInMBB is empty.
906 if (RefsInMBB.empty())
907 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000908
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000909 MachineBasicBlock::iterator FoldPt = Barrier;
910 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
911 !RefsInMBB.count(FoldPt))
912 --FoldPt;
913
914 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
915 if (OpIdx == -1)
916 return 0;
917
918 SmallVector<unsigned, 1> Ops;
919 Ops.push_back(OpIdx);
920
921 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
922 return 0;
923
924 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
925 if (I != IntervalSSMap.end()) {
926 SS = I->second;
927 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000928 SS = MFI->CreateSpillStackObject(RC->getSize(), RC->getAlignment());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000929 }
930
931 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
932 FoldPt, Ops, SS);
933
934 if (FMI) {
935 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
936 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
937 ++NumFolds;
938
939 IntervalSSMap[vreg] = SS;
Evan Chengc781a242009-05-03 18:32:42 +0000940 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000941 if (CurrSLI->hasAtLeastOneValue())
942 CurrSValNo = CurrSLI->getValNumInfo(0);
943 else
Lang Hames233a60e2009-11-03 23:52:08 +0000944 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000945 LSs->getVNInfoAllocator());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000946 }
947
948 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000949}
950
Owen Andersonc93023a2009-03-04 08:52:31 +0000951MachineInstr* PreAllocSplitting::FoldRestore(unsigned vreg,
952 const TargetRegisterClass* RC,
953 MachineInstr* Barrier,
954 MachineBasicBlock* MBB,
955 int SS,
956 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Andersona2bfb542009-03-05 08:23:20 +0000957 if ((int)RestoreFoldLimit != -1 && RestoreFoldLimit == (int)NumRestoreFolds)
Owen Anderson323c58d2009-03-05 07:19:18 +0000958 return 0;
959
Owen Andersonc93023a2009-03-04 08:52:31 +0000960 // Go top down if RefsInMBB is empty.
961 if (RefsInMBB.empty())
962 return 0;
963
964 // Can't fold a restore between a call stack setup and teardown.
965 MachineBasicBlock::iterator FoldPt = Barrier;
Owen Anderson323c58d2009-03-05 07:19:18 +0000966
967 // Advance from barrier to call frame teardown.
968 while (FoldPt != MBB->getFirstTerminator() &&
969 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
970 if (RefsInMBB.count(FoldPt))
971 return 0;
Owen Andersonc93023a2009-03-04 08:52:31 +0000972
Owen Anderson323c58d2009-03-05 07:19:18 +0000973 ++FoldPt;
974 }
975
976 if (FoldPt == MBB->getFirstTerminator())
977 return 0;
978 else
979 ++FoldPt;
980
981 // Now find the restore point.
982 while (FoldPt != MBB->getFirstTerminator() && !RefsInMBB.count(FoldPt)) {
Owen Andersonc93023a2009-03-04 08:52:31 +0000983 if (FoldPt->getOpcode() == TRI->getCallFrameSetupOpcode()) {
984 while (FoldPt != MBB->getFirstTerminator() &&
985 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
986 if (RefsInMBB.count(FoldPt))
987 return 0;
988
989 ++FoldPt;
990 }
991
Owen Anderson323c58d2009-03-05 07:19:18 +0000992 if (FoldPt == MBB->getFirstTerminator())
993 return 0;
994 }
995
996 ++FoldPt;
Owen Andersonc93023a2009-03-04 08:52:31 +0000997 }
998
999 if (FoldPt == MBB->getFirstTerminator())
1000 return 0;
1001
1002 int OpIdx = FoldPt->findRegisterUseOperandIdx(vreg, true);
1003 if (OpIdx == -1)
1004 return 0;
1005
1006 SmallVector<unsigned, 1> Ops;
1007 Ops.push_back(OpIdx);
1008
1009 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
1010 return 0;
1011
1012 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
1013 FoldPt, Ops, SS);
1014
1015 if (FMI) {
1016 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
1017 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
Owen Anderson323c58d2009-03-05 07:19:18 +00001018 ++NumRestoreFolds;
Owen Andersonc93023a2009-03-04 08:52:31 +00001019 }
1020
1021 return FMI;
1022}
1023
Evan Chengf5cd4f02008-10-23 20:43:13 +00001024/// SplitRegLiveInterval - Split (spill and restore) the given live interval
1025/// so it would not cross the barrier that's being processed. Shrink wrap
1026/// (minimize) the live interval to the last uses.
1027bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
Lang Hames233a60e2009-11-03 23:52:08 +00001028 DEBUG(errs() << "Pre-alloc splitting " << LI->reg << " for " << *Barrier
1029 << " result: ");
1030
Evan Chengf5cd4f02008-10-23 20:43:13 +00001031 CurrLI = LI;
1032
1033 // Find live range where current interval cross the barrier.
1034 LiveInterval::iterator LR =
Lang Hames233a60e2009-11-03 23:52:08 +00001035 CurrLI->FindLiveRangeContaining(BarrierIdx.getUseIndex());
Evan Chengf5cd4f02008-10-23 20:43:13 +00001036 VNInfo *ValNo = LR->valno;
1037
Torok Edwinf3689232009-07-12 20:07:01 +00001038 assert(!ValNo->isUnused() && "Val# is defined by a dead def?");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001039
Lang Hames857c4e02009-06-17 21:01:20 +00001040 MachineInstr *DefMI = ValNo->isDefAccurate()
Evan Cheng06587492008-10-24 02:05:00 +00001041 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +00001042
Owen Andersond3be4622009-01-21 08:18:03 +00001043 // If this would create a new join point, do not split.
Lang Hames233a60e2009-11-03 23:52:08 +00001044 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent())) {
1045 DEBUG(errs() << "FAILED (would create a new join point).\n");
Owen Andersond3be4622009-01-21 08:18:03 +00001046 return false;
Lang Hames233a60e2009-11-03 23:52:08 +00001047 }
Owen Andersond3be4622009-01-21 08:18:03 +00001048
Evan Chengf5cd4f02008-10-23 20:43:13 +00001049 // Find all references in the barrier mbb.
1050 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
1051 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
1052 E = MRI->reg_end(); I != E; ++I) {
1053 MachineInstr *RefMI = &*I;
1054 if (RefMI->getParent() == BarrierMBB)
1055 RefsInMBB.insert(RefMI);
1056 }
1057
1058 // Find a point to restore the value after the barrier.
Evan Chengf5cd4f02008-10-23 20:43:13 +00001059 MachineBasicBlock::iterator RestorePt =
Lang Hamesb3661582009-11-14 00:02:51 +00001060 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001061 if (RestorePt == BarrierMBB->end()) {
1062 DEBUG(errs() << "FAILED (could not find a suitable restore point).\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001063 return false;
Lang Hames233a60e2009-11-03 23:52:08 +00001064 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001065
Owen Anderson75fa96b2008-11-19 04:28:29 +00001066 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
Lang Hamesb3661582009-11-14 00:02:51 +00001067 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt, RefsInMBB)) {
Lang Hames233a60e2009-11-03 23:52:08 +00001068 DEBUG(errs() << "success (remat).\n");
1069 return true;
1070 }
Owen Anderson75fa96b2008-11-19 04:28:29 +00001071
Evan Chengf5cd4f02008-10-23 20:43:13 +00001072 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001073 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001074 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Lang Hames233a60e2009-11-03 23:52:08 +00001075 SlotIndex SpillIndex;
Evan Cheng06587492008-10-24 02:05:00 +00001076 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001077 int SS = -1;
Lang Hames857c4e02009-06-17 21:01:20 +00001078 if (!ValNo->isDefAccurate()) {
1079 // If we don't know where the def is we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001080 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1081 BarrierMBB, SS, RefsInMBB))) {
1082 SpillIndex = LIs->getInstructionIndex(SpillMI);
1083 } else {
1084 MachineBasicBlock::iterator SpillPt =
Lang Hamesb3661582009-11-14 00:02:51 +00001085 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001086 if (SpillPt == BarrierMBB->begin()) {
1087 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001088 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001089 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001090 // Add spill.
1091
1092 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1093 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
1094 SpillMI = prior(SpillPt);
Lang Hamesb3661582009-11-14 00:02:51 +00001095 SpillIndex = LIs->InsertMachineInstrInMaps(SpillMI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001096 }
Evan Cheng54898932008-10-29 08:39:34 +00001097 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
Lang Hamesb3661582009-11-14 00:02:51 +00001098 LIs->getZeroIndex(), SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001099 // If it's already split, just restore the value. There is no need to spill
1100 // the def again.
Lang Hames233a60e2009-11-03 23:52:08 +00001101 if (!DefMI) {
1102 DEBUG(errs() << "FAILED (def is dead).\n");
Evan Chengd0e32c52008-10-29 05:06:14 +00001103 return false; // Def is dead. Do nothing.
Lang Hames233a60e2009-11-03 23:52:08 +00001104 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001105
1106 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
Evan Cheng35ca9202009-10-09 01:17:11 +00001107 BarrierMBB, SS, RefsInMBB))) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001108 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001109 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001110 // Check if it's possible to insert a spill after the def MI.
1111 MachineBasicBlock::iterator SpillPt;
1112 if (DefMBB == BarrierMBB) {
1113 // Add spill after the def and the last use before the barrier.
1114 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
Lang Hamesb3661582009-11-14 00:02:51 +00001115 RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001116 if (SpillPt == DefMBB->begin()) {
1117 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001118 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001119 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001120 } else {
Lang Hamesb3661582009-11-14 00:02:51 +00001121 SpillPt = next(MachineBasicBlock::iterator(DefMI));
Lang Hames233a60e2009-11-03 23:52:08 +00001122 if (SpillPt == DefMBB->end()) {
1123 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001124 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001125 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001126 }
Evan Cheng35ca9202009-10-09 01:17:11 +00001127 // Add spill.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001128 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng35ca9202009-10-09 01:17:11 +00001129 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg, false, SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001130 SpillMI = prior(SpillPt);
Lang Hamesb3661582009-11-14 00:02:51 +00001131 SpillIndex = LIs->InsertMachineInstrInMaps(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001132 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001133 }
1134
Evan Cheng54898932008-10-29 08:39:34 +00001135 // Remember def instruction index to spill index mapping.
1136 if (DefMI && SpillMI)
1137 Def2SpillMap[ValNo->def] = SpillIndex;
1138
Evan Chengf5cd4f02008-10-23 20:43:13 +00001139 // Add restore.
Owen Andersonc93023a2009-03-04 08:52:31 +00001140 bool FoldedRestore = false;
Lang Hamesb3661582009-11-14 00:02:51 +00001141 SlotIndex RestoreIndex;
Owen Andersonc93023a2009-03-04 08:52:31 +00001142 if (MachineInstr* LMI = FoldRestore(CurrLI->reg, RC, Barrier,
1143 BarrierMBB, SS, RefsInMBB)) {
1144 RestorePt = LMI;
Owen Anderson323c58d2009-03-05 07:19:18 +00001145 RestoreIndex = LIs->getInstructionIndex(RestorePt);
Owen Andersonc93023a2009-03-04 08:52:31 +00001146 FoldedRestore = true;
1147 } else {
1148 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
1149 MachineInstr *LoadMI = prior(RestorePt);
Lang Hamesb3661582009-11-14 00:02:51 +00001150 RestoreIndex = LIs->InsertMachineInstrInMaps(LoadMI);
Owen Andersonc93023a2009-03-04 08:52:31 +00001151 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001152
Evan Chengd0e32c52008-10-29 05:06:14 +00001153 // Update spill stack slot live interval.
Lang Hames233a60e2009-11-03 23:52:08 +00001154 UpdateSpillSlotInterval(ValNo, SpillIndex.getUseIndex().getNextSlot(),
1155 RestoreIndex.getDefIndex());
Evan Chengd0e32c52008-10-29 05:06:14 +00001156
Owen Andersonb4b84362009-01-26 21:57:31 +00001157 ReconstructLiveInterval(CurrLI);
Evan Cheng35ca9202009-10-09 01:17:11 +00001158
Owen Andersonc93023a2009-03-04 08:52:31 +00001159 if (!FoldedRestore) {
Lang Hames233a60e2009-11-03 23:52:08 +00001160 SlotIndex RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
1161 RestoreIdx = RestoreIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +00001162 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RestoreIdx));
Owen Andersonc93023a2009-03-04 08:52:31 +00001163 }
Owen Anderson7d211e22008-12-31 02:00:25 +00001164
Evan Chengae7fa5b2008-10-28 01:48:24 +00001165 ++NumSplits;
Lang Hames233a60e2009-11-03 23:52:08 +00001166 DEBUG(errs() << "success.\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001167 return true;
1168}
1169
1170/// SplitRegLiveIntervals - Split all register live intervals that cross the
1171/// barrier that's being processed.
1172bool
Owen Anderson956ec272009-01-23 00:23:32 +00001173PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1174 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001175 // First find all the virtual registers whose live intervals are intercepted
1176 // by the current barrier.
1177 SmallVector<LiveInterval*, 8> Intervals;
1178 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng4350eb82009-02-06 17:17:30 +00001179 // FIXME: If it's not safe to move any instruction that defines the barrier
1180 // register class, then it means there are some special dependencies which
1181 // codegen is not modelling. Ignore these barriers for now.
1182 if (!TII->isSafeToMoveRegClassDefs(*RC))
Evan Cheng23066282008-10-27 07:14:50 +00001183 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001184 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
1185 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1186 unsigned Reg = VRs[i];
1187 if (!LIs->hasInterval(Reg))
1188 continue;
1189 LiveInterval *LI = &LIs->getInterval(Reg);
1190 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1191 // Virtual register live interval is intercepted by the barrier. We
1192 // should split and shrink wrap its interval if possible.
1193 Intervals.push_back(LI);
1194 }
1195 }
1196
1197 // Process the affected live intervals.
1198 bool Change = false;
1199 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001200 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1201 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001202 LiveInterval *LI = Intervals.back();
1203 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001204 bool result = SplitRegLiveInterval(LI);
1205 if (result) Split.insert(LI);
1206 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001207 }
1208
1209 return Change;
1210}
1211
Owen Anderson45e68552009-01-29 05:28:55 +00001212unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001213 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001214 unsigned Reg, int FrameIndex,
1215 bool& FeedsTwoAddr) {
1216 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001217 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001218 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001219 int StoreFrameIndex;
1220 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001221 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
1222 NonSpills++;
1223
1224 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
Bob Wilsond9df5012009-04-09 17:16:43 +00001225 if (DefIdx != -1 && (*UI)->isRegTiedToUseOperand(DefIdx))
Owen Anderson45e68552009-01-29 05:28:55 +00001226 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001227 }
1228
Owen Anderson45e68552009-01-29 05:28:55 +00001229 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001230}
1231
Owen Anderson956ec272009-01-23 00:23:32 +00001232/// removeDeadSpills - After doing splitting, filter through all intervals we've
1233/// split, and see if any of the spills are unnecessary. If so, remove them.
1234bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1235 bool changed = false;
1236
Owen Anderson4bfc2092009-01-29 05:41:02 +00001237 // Walk over all of the live intervals that were touched by the splitter,
1238 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001239 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1240 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001241 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001242
Owen Anderson4bfc2092009-01-29 05:41:02 +00001243 // First, collect all the uses of the vreg, and sort them by their
1244 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001245 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1246 UE = MRI->use_end(); UI != UE; ++UI) {
Lang Hames233a60e2009-11-03 23:52:08 +00001247 SlotIndex index = LIs->getInstructionIndex(&*UI);
1248 index = index.getUseIndex();
Owen Anderson956ec272009-01-23 00:23:32 +00001249
1250 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001251 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001252 }
1253
Owen Anderson4bfc2092009-01-29 05:41:02 +00001254 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1255 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001256 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1257 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001258
1259 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1260 return changed;
1261
Owen Anderson956ec272009-01-23 00:23:32 +00001262 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001263
1264 // We don't currently try to handle definitions with PHI kills, because
1265 // it would involve processing more than one VNInfo at once.
Lang Hames857c4e02009-06-17 21:01:20 +00001266 if (CurrVN->hasPHIKill()) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001267
Owen Anderson4bfc2092009-01-29 05:41:02 +00001268 // We also don't try to handle the results of PHI joins, since there's
1269 // no defining instruction to analyze.
Lang Hames857c4e02009-06-17 21:01:20 +00001270 if (!CurrVN->isDefAccurate() || CurrVN->isUnused()) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001271
Owen Anderson4bfc2092009-01-29 05:41:02 +00001272 // We're only interested in eliminating cruft introduced by the splitter,
1273 // is of the form load-use or load-use-store. First, check that the
1274 // definition is a load, and remember what stack slot we loaded it from.
Lang Hames857c4e02009-06-17 21:01:20 +00001275 MachineInstr* DefMI = LIs->getInstructionFromIndex(CurrVN->def);
Owen Anderson956ec272009-01-23 00:23:32 +00001276 int FrameIndex;
1277 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1278
Owen Anderson4bfc2092009-01-29 05:41:02 +00001279 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001280 if (VNUseCount[CurrVN].size() == 0) {
1281 LIs->RemoveMachineInstrFromMaps(DefMI);
1282 (*LI)->removeValNo(CurrVN);
1283 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001284 VNUseCount.erase(CurrVN);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001285 NumDeadSpills++;
1286 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001287 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001288 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001289
Owen Anderson4bfc2092009-01-29 05:41:02 +00001290 // Second, get the number of non-store uses of the definition, as well as
1291 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001292 bool FeedsTwoAddr = false;
1293 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1294 (*LI)->reg, FrameIndex,
1295 FeedsTwoAddr);
1296
Owen Anderson4bfc2092009-01-29 05:41:02 +00001297 // If there's one non-store use and it doesn't feed a two-addr, then
1298 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001299 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001300 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001301 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1302 int StoreFrameIndex;
1303 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1304 while (UI != VNUseCount[CurrVN].end() &&
1305 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1306 ++UI;
1307 if (UI != VNUseCount[CurrVN].end())
1308 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1309 }
Owen Anderson45e68552009-01-29 05:28:55 +00001310 if (UI == VNUseCount[CurrVN].end()) continue;
1311
1312 MachineInstr* use = *UI;
1313
Owen Anderson4bfc2092009-01-29 05:41:02 +00001314 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001315 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1316 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001317 SmallVector<unsigned, 1> Ops;
1318 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001319 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1320
1321 MachineInstr* NewMI =
1322 TII->foldMemoryOperand(*use->getParent()->getParent(),
1323 use, Ops, FrameIndex);
1324
1325 if (!NewMI) continue;
1326
Owen Anderson4bfc2092009-01-29 05:41:02 +00001327 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001328 LIs->RemoveMachineInstrFromMaps(DefMI);
1329 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1330 (*LI)->removeValNo(CurrVN);
1331
1332 DefMI->eraseFromParent();
1333 MachineBasicBlock* MBB = use->getParent();
1334 NewMI = MBB->insert(MBB->erase(use), NewMI);
1335 VNUseCount[CurrVN].erase(use);
1336
Owen Anderson4bfc2092009-01-29 05:41:02 +00001337 // Remove deleted instructions. Note that we need to remove them from
1338 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001339 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1340 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1341 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001342 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001343 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1344 ++VNI)
1345 if (VNI->first != CurrVN)
1346 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001347 LIs->RemoveMachineInstrFromMaps(*II);
1348 (*II)->eraseFromParent();
1349 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001350
1351 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001352
1353 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1354 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1355 if (VI->second.erase(use))
1356 VI->second.insert(NewMI);
1357
1358 NumDeadSpills++;
1359 changed = true;
1360 continue;
1361 }
1362
Owen Anderson4bfc2092009-01-29 05:41:02 +00001363 // If there's more than one non-store instruction, we can't profitably
1364 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001365 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001366
Owen Anderson4bfc2092009-01-29 05:41:02 +00001367 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001368 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1369 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
Lang Hamesf035ce52009-11-12 01:24:08 +00001370 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001371 LIs->RemoveMachineInstrFromMaps(*UI);
1372 (*UI)->eraseFromParent();
1373 }
1374
Owen Andersonc0f3a032009-01-29 08:22:06 +00001375 VNUseCount.erase(CurrVN);
1376
Owen Anderson32ca8652009-01-24 10:07:43 +00001377 LIs->RemoveMachineInstrFromMaps(DefMI);
1378 (*LI)->removeValNo(CurrVN);
1379 DefMI->eraseFromParent();
1380 NumDeadSpills++;
1381 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001382 }
1383 }
1384
1385 return changed;
1386}
1387
Owen Andersonf1f75b12008-11-04 22:22:41 +00001388bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1389 MachineBasicBlock* DefMBB,
1390 MachineBasicBlock* BarrierMBB) {
1391 if (DefMBB == BarrierMBB)
1392 return false;
1393
Lang Hames857c4e02009-06-17 21:01:20 +00001394 if (LR->valno->hasPHIKill())
Owen Andersonf1f75b12008-11-04 22:22:41 +00001395 return false;
1396
Lang Hames233a60e2009-11-03 23:52:08 +00001397 SlotIndex MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
Owen Andersonf1f75b12008-11-04 22:22:41 +00001398 if (LR->end < MBBEnd)
1399 return false;
1400
1401 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1402 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1403 return true;
1404
1405 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1406 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1407 typedef std::pair<MachineBasicBlock*,
1408 MachineBasicBlock::succ_iterator> ItPair;
1409 SmallVector<ItPair, 4> Stack;
1410 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1411
1412 while (!Stack.empty()) {
1413 ItPair P = Stack.back();
1414 Stack.pop_back();
1415
1416 MachineBasicBlock* PredMBB = P.first;
1417 MachineBasicBlock::succ_iterator S = P.second;
1418
1419 if (S == PredMBB->succ_end())
1420 continue;
1421 else if (Visited.count(*S)) {
1422 Stack.push_back(std::make_pair(PredMBB, ++S));
1423 continue;
1424 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001425 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001426
1427 MachineBasicBlock* MBB = *S;
1428 Visited.insert(MBB);
1429
1430 if (MBB == BarrierMBB)
1431 return true;
1432
1433 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1434 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1435 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1436 while (MDTN) {
1437 if (MDTN == DefMDTN)
1438 return true;
1439 else if (MDTN == BarrierMDTN)
1440 break;
1441 MDTN = MDTN->getIDom();
1442 }
1443
1444 MBBEnd = LIs->getMBBEndIdx(MBB);
1445 if (LR->end > MBBEnd)
1446 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1447 }
1448
1449 return false;
1450}
1451
1452
Evan Cheng09e8ca82008-10-20 21:44:59 +00001453bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001454 CurrMF = &MF;
1455 TM = &MF.getTarget();
Owen Anderson3ef45492009-01-29 22:13:06 +00001456 TRI = TM->getRegisterInfo();
Evan Chengd0e32c52008-10-29 05:06:14 +00001457 TII = TM->getInstrInfo();
1458 MFI = MF.getFrameInfo();
1459 MRI = &MF.getRegInfo();
Lang Hames233a60e2009-11-03 23:52:08 +00001460 SIs = &getAnalysis<SlotIndexes>();
Evan Chengd0e32c52008-10-29 05:06:14 +00001461 LIs = &getAnalysis<LiveIntervals>();
1462 LSs = &getAnalysis<LiveStacks>();
Owen Anderson420dd372009-03-14 21:40:05 +00001463 VRM = &getAnalysis<VirtRegMap>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001464
1465 bool MadeChange = false;
1466
1467 // Make sure blocks are numbered in order.
1468 MF.RenumberBlocks();
1469
Evan Cheng54898932008-10-29 08:39:34 +00001470 MachineBasicBlock *Entry = MF.begin();
1471 SmallPtrSet<MachineBasicBlock*,16> Visited;
1472
Owen Anderson956ec272009-01-23 00:23:32 +00001473 SmallPtrSet<LiveInterval*, 8> Split;
1474
Evan Cheng54898932008-10-29 08:39:34 +00001475 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1476 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1477 DFI != E; ++DFI) {
1478 BarrierMBB = *DFI;
1479 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1480 E = BarrierMBB->end(); I != E; ++I) {
1481 Barrier = &*I;
1482 const TargetRegisterClass **BarrierRCs =
1483 Barrier->getDesc().getRegClassBarriers();
1484 if (!BarrierRCs)
1485 continue;
1486 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001487 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001488 }
1489 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001490
Owen Anderson956ec272009-01-23 00:23:32 +00001491 MadeChange |= removeDeadSpills(Split);
1492
Evan Chengf5cd4f02008-10-23 20:43:13 +00001493 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001494}