blob: cce5ae817a20e61788ae061314262d44f07d3ddc [file] [log] [blame]
Evan Cheng09e8ca82008-10-20 21:44:59 +00001//===-- PreAllocSplitting.cpp - Pre-allocation Interval Spltting Pass. ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the machine instruction level pre-register allocation
11// live interval splitting pass. It finds live interval barriers, i.e.
12// instructions which will kill all physical registers in certain register
13// classes, and split all live intervals which cross the barrier.
14//
15//===----------------------------------------------------------------------===//
16
17#define DEBUG_TYPE "pre-alloc-split"
Owen Anderson420dd372009-03-14 21:40:05 +000018#include "VirtRegMap.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000020#include "llvm/CodeGen/LiveStackAnalysis.h"
Owen Andersonf1f75b12008-11-04 22:22:41 +000021#include "llvm/CodeGen/MachineDominators.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineLoopInfo.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000028#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000029#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetOptions.h"
31#include "llvm/Target/TargetRegisterInfo.h"
32#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000035#include "llvm/ADT/DenseMap.h"
Evan Cheng54898932008-10-29 08:39:34 +000036#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000037#include "llvm/ADT/SmallPtrSet.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000038#include "llvm/ADT/Statistic.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000039using namespace llvm;
40
Evan Chengae7fa5b2008-10-28 01:48:24 +000041static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden);
Owen Anderson45e68552009-01-29 05:28:55 +000042static cl::opt<int> DeadSplitLimit("dead-split-limit", cl::init(-1), cl::Hidden);
Owen Anderson323c58d2009-03-05 07:19:18 +000043static cl::opt<int> RestoreFoldLimit("restore-fold-limit", cl::init(-1), cl::Hidden);
Evan Chengae7fa5b2008-10-28 01:48:24 +000044
Owen Anderson45e68552009-01-29 05:28:55 +000045STATISTIC(NumSplits, "Number of intervals split");
Owen Anderson75fa96b2008-11-19 04:28:29 +000046STATISTIC(NumRemats, "Number of intervals split by rematerialization");
Owen Anderson7b9d67c2008-12-02 18:53:47 +000047STATISTIC(NumFolds, "Number of intervals split with spill folding");
Owen Anderson323c58d2009-03-05 07:19:18 +000048STATISTIC(NumRestoreFolds, "Number of intervals split with restore folding");
Owen Anderson2ebf63f2008-12-18 01:27:19 +000049STATISTIC(NumRenumbers, "Number of intervals renumbered into new registers");
Owen Anderson956ec272009-01-23 00:23:32 +000050STATISTIC(NumDeadSpills, "Number of dead spills removed");
Evan Chengf5cd4f02008-10-23 20:43:13 +000051
Evan Cheng09e8ca82008-10-20 21:44:59 +000052namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000053 class PreAllocSplitting : public MachineFunctionPass {
Evan Chengd0e32c52008-10-29 05:06:14 +000054 MachineFunction *CurrMF;
Evan Chengf5cd4f02008-10-23 20:43:13 +000055 const TargetMachine *TM;
56 const TargetInstrInfo *TII;
Owen Anderson3ef45492009-01-29 22:13:06 +000057 const TargetRegisterInfo* TRI;
Evan Chengf5cd4f02008-10-23 20:43:13 +000058 MachineFrameInfo *MFI;
59 MachineRegisterInfo *MRI;
Lang Hames233a60e2009-11-03 23:52:08 +000060 SlotIndexes *SIs;
Evan Chengf5cd4f02008-10-23 20:43:13 +000061 LiveIntervals *LIs;
Evan Chengd0e32c52008-10-29 05:06:14 +000062 LiveStacks *LSs;
Owen Anderson420dd372009-03-14 21:40:05 +000063 VirtRegMap *VRM;
Evan Cheng09e8ca82008-10-20 21:44:59 +000064
Evan Chengf5cd4f02008-10-23 20:43:13 +000065 // Barrier - Current barrier being processed.
66 MachineInstr *Barrier;
67
68 // BarrierMBB - Basic block where the barrier resides in.
69 MachineBasicBlock *BarrierMBB;
70
71 // Barrier - Current barrier index.
Lang Hames233a60e2009-11-03 23:52:08 +000072 SlotIndex BarrierIdx;
Evan Chengf5cd4f02008-10-23 20:43:13 +000073
74 // CurrLI - Current live interval being split.
75 LiveInterval *CurrLI;
76
Evan Chengd0e32c52008-10-29 05:06:14 +000077 // CurrSLI - Current stack slot live interval.
78 LiveInterval *CurrSLI;
79
80 // CurrSValNo - Current val# for the stack slot live interval.
81 VNInfo *CurrSValNo;
82
83 // IntervalSSMap - A map from live interval to spill slots.
84 DenseMap<unsigned, int> IntervalSSMap;
Evan Chengf5cd4f02008-10-23 20:43:13 +000085
Evan Cheng54898932008-10-29 08:39:34 +000086 // Def2SpillMap - A map from a def instruction index to spill index.
Lang Hames233a60e2009-11-03 23:52:08 +000087 DenseMap<SlotIndex, SlotIndex> Def2SpillMap;
Evan Cheng06587492008-10-24 02:05:00 +000088
Evan Cheng09e8ca82008-10-20 21:44:59 +000089 public:
90 static char ID;
Lang Hames233a60e2009-11-03 23:52:08 +000091 PreAllocSplitting()
92 : MachineFunctionPass(&ID) {}
Evan Cheng09e8ca82008-10-20 21:44:59 +000093
94 virtual bool runOnMachineFunction(MachineFunction &MF);
95
96 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000097 AU.setPreservesCFG();
Lang Hames233a60e2009-11-03 23:52:08 +000098 AU.addRequired<SlotIndexes>();
99 AU.addPreserved<SlotIndexes>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000100 AU.addRequired<LiveIntervals>();
101 AU.addPreserved<LiveIntervals>();
Evan Chengd0e32c52008-10-29 05:06:14 +0000102 AU.addRequired<LiveStacks>();
103 AU.addPreserved<LiveStacks>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000104 AU.addPreserved<RegisterCoalescer>();
105 if (StrongPHIElim)
106 AU.addPreservedID(StrongPHIEliminationID);
107 else
108 AU.addPreservedID(PHIEliminationID);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000109 AU.addRequired<MachineDominatorTree>();
110 AU.addRequired<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000111 AU.addRequired<VirtRegMap>();
Owen Andersonf1f75b12008-11-04 22:22:41 +0000112 AU.addPreserved<MachineDominatorTree>();
113 AU.addPreserved<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000114 AU.addPreserved<VirtRegMap>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000115 MachineFunctionPass::getAnalysisUsage(AU);
116 }
117
118 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +0000119 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +0000120 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000121 }
122
123 virtual const char *getPassName() const {
124 return "Pre-Register Allocaton Live Interval Splitting";
125 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000126
127 /// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000128 virtual void print(raw_ostream &O, const Module* M = 0) const {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000129 LIs->print(O, M);
130 }
131
Evan Chengf5cd4f02008-10-23 20:43:13 +0000132
133 private:
134 MachineBasicBlock::iterator
135 findNextEmptySlot(MachineBasicBlock*, MachineInstr*,
Lang Hames233a60e2009-11-03 23:52:08 +0000136 SlotIndex&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000137
138 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000139 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Lang Hames233a60e2009-11-03 23:52:08 +0000140 SmallPtrSet<MachineInstr*, 4>&, SlotIndex&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000141
142 MachineBasicBlock::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000143 findRestorePoint(MachineBasicBlock*, MachineInstr*, SlotIndex,
144 SmallPtrSet<MachineInstr*, 4>&, SlotIndex&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000145
Evan Chengd0e32c52008-10-29 05:06:14 +0000146 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000147
Lang Hames86511252009-09-04 20:41:11 +0000148 bool IsAvailableInStack(MachineBasicBlock*, unsigned,
Lang Hames233a60e2009-11-03 23:52:08 +0000149 SlotIndex, SlotIndex,
150 SlotIndex&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000151
Lang Hames233a60e2009-11-03 23:52:08 +0000152 void UpdateSpillSlotInterval(VNInfo*, SlotIndex, SlotIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000153
Evan Chengf5cd4f02008-10-23 20:43:13 +0000154 bool SplitRegLiveInterval(LiveInterval*);
155
Owen Anderson956ec272009-01-23 00:23:32 +0000156 bool SplitRegLiveIntervals(const TargetRegisterClass **,
157 SmallPtrSet<LiveInterval*, 8>&);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000158
159 bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB,
160 MachineBasicBlock* BarrierMBB);
Owen Anderson75fa96b2008-11-19 04:28:29 +0000161 bool Rematerialize(unsigned vreg, VNInfo* ValNo,
162 MachineInstr* DefMI,
163 MachineBasicBlock::iterator RestorePt,
Lang Hames233a60e2009-11-03 23:52:08 +0000164 SlotIndex RestoreIdx,
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
212/// findNextEmptySlot - Find a gap after the given machine instruction in the
213/// instruction index map. If there isn't one, return end().
214MachineBasicBlock::iterator
215PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000216 SlotIndex &SpotIndex) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000217 MachineBasicBlock::iterator MII = MI;
218 if (++MII != MBB->end()) {
Lang Hames233a60e2009-11-03 23:52:08 +0000219 SlotIndex Index =
Lang Hames86511252009-09-04 20:41:11 +0000220 LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
Lang Hames233a60e2009-11-03 23:52:08 +0000221 if (Index != SlotIndex()) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000222 SpotIndex = Index;
223 return MII;
224 }
225 }
226 return MBB->end();
227}
228
229/// findSpillPoint - Find a gap as far away from the given MI that's suitable
230/// for spilling the current live interval. The index must be before any
231/// defs and uses of the live interval register in the mbb. Return begin() if
232/// none is found.
233MachineBasicBlock::iterator
234PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000235 MachineInstr *DefMI,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000236 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000237 SlotIndex &SpillIndex) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000238 MachineBasicBlock::iterator Pt = MBB->begin();
239
Owen Anderson696a1302009-03-31 08:27:09 +0000240 MachineBasicBlock::iterator MII = MI;
241 MachineBasicBlock::iterator EndPt = DefMI
242 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
Owen Anderson4cafbb52009-02-20 10:02:23 +0000243
Owen Anderson696a1302009-03-31 08:27:09 +0000244 while (MII != EndPt && !RefsInMBB.count(MII) &&
245 MII->getOpcode() != TRI->getCallFrameSetupOpcode())
246 --MII;
247 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson4cafbb52009-02-20 10:02:23 +0000248
Owen Anderson696a1302009-03-31 08:27:09 +0000249 while (MII != EndPt && !RefsInMBB.count(MII)) {
Lang Hames233a60e2009-11-03 23:52:08 +0000250 SlotIndex Index = LIs->getInstructionIndex(MII);
Owen Anderson3ef45492009-01-29 22:13:06 +0000251
Owen Anderson696a1302009-03-31 08:27:09 +0000252 // We can't insert the spill between the barrier (a call), and its
253 // corresponding call frame setup.
254 if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
255 while (MII->getOpcode() != TRI->getCallFrameSetupOpcode()) {
Owen Anderson5734c942009-02-05 05:58:41 +0000256 --MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000257 if (MII == EndPt) {
258 return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000259 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000260 }
Owen Anderson696a1302009-03-31 08:27:09 +0000261 continue;
262 } else if (LIs->hasGapBeforeInstr(Index)) {
263 Pt = MII;
264 SpillIndex = LIs->findGapBeforeInstr(Index, true);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000265 }
Owen Anderson696a1302009-03-31 08:27:09 +0000266
267 if (RefsInMBB.count(MII))
268 return Pt;
269
270
271 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000272 }
273
274 return Pt;
275}
276
277/// findRestorePoint - Find a gap in the instruction index map that's suitable
278/// for restoring the current live interval value. The index must be before any
279/// uses of the live interval register in the mbb. Return end() if none is
280/// found.
281MachineBasicBlock::iterator
282PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000283 SlotIndex LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000284 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000285 SlotIndex &RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000286 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
287 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000288 MachineBasicBlock::iterator Pt = MBB->end();
Owen Anderson696a1302009-03-31 08:27:09 +0000289 MachineBasicBlock::iterator EndPt = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000290
Owen Anderson696a1302009-03-31 08:27:09 +0000291 // We start at the call, so walk forward until we find the call frame teardown
292 // since we can't insert restores before that. Bail if we encounter a use
293 // during this time.
294 MachineBasicBlock::iterator MII = MI;
295 if (MII == EndPt) return Pt;
296
297 while (MII != EndPt && !RefsInMBB.count(MII) &&
298 MII->getOpcode() != TRI->getCallFrameDestroyOpcode())
299 ++MII;
300 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
301 ++MII;
302
303 // FIXME: Limit the number of instructions to examine to reduce
304 // compile time?
305 while (MII != EndPt) {
Lang Hames233a60e2009-11-03 23:52:08 +0000306 SlotIndex Index = LIs->getInstructionIndex(MII);
Owen Anderson696a1302009-03-31 08:27:09 +0000307 if (Index > LastIdx)
308 break;
Lang Hames233a60e2009-11-03 23:52:08 +0000309 SlotIndex Gap = LIs->findGapBeforeInstr(Index);
Owen Anderson3ef45492009-01-29 22:13:06 +0000310
Owen Anderson696a1302009-03-31 08:27:09 +0000311 // We can't insert a restore between the barrier (a call) and its
312 // corresponding call frame teardown.
313 if (MII->getOpcode() == TRI->getCallFrameSetupOpcode()) {
314 do {
315 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000316 ++MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000317 } while (MII->getOpcode() != TRI->getCallFrameDestroyOpcode());
Lang Hames233a60e2009-11-03 23:52:08 +0000318 } else if (Gap != SlotIndex()) {
Owen Anderson696a1302009-03-31 08:27:09 +0000319 Pt = MII;
320 RestoreIndex = Gap;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000321 }
Owen Anderson696a1302009-03-31 08:27:09 +0000322
323 if (RefsInMBB.count(MII))
324 return Pt;
325
326 ++MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000327 }
328
329 return Pt;
330}
331
Evan Chengd0e32c52008-10-29 05:06:14 +0000332/// CreateSpillStackSlot - Create a stack slot for the live interval being
333/// split. If the live interval was previously split, just reuse the same
334/// slot.
335int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
336 const TargetRegisterClass *RC) {
337 int SS;
338 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
339 if (I != IntervalSSMap.end()) {
340 SS = I->second;
341 } else {
342 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
343 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000344 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000345
346 // Create live interval for stack slot.
Evan Chengc781a242009-05-03 18:32:42 +0000347 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Evan Cheng54898932008-10-29 08:39:34 +0000348 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000349 CurrSValNo = CurrSLI->getValNumInfo(0);
350 else
Lang Hames233a60e2009-11-03 23:52:08 +0000351 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000352 LSs->getVNInfoAllocator());
Evan Chengd0e32c52008-10-29 05:06:14 +0000353 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000354}
355
Evan Chengd0e32c52008-10-29 05:06:14 +0000356/// IsAvailableInStack - Return true if register is available in a split stack
357/// slot at the specified index.
358bool
Evan Cheng54898932008-10-29 08:39:34 +0000359PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000360 unsigned Reg, SlotIndex DefIndex,
361 SlotIndex RestoreIndex,
362 SlotIndex &SpillIndex,
Evan Cheng54898932008-10-29 08:39:34 +0000363 int& SS) const {
364 if (!DefMBB)
365 return false;
366
Evan Chengd0e32c52008-10-29 05:06:14 +0000367 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
368 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000369 return false;
Lang Hames233a60e2009-11-03 23:52:08 +0000370 DenseMap<SlotIndex, SlotIndex>::iterator
Lang Hames86511252009-09-04 20:41:11 +0000371 II = Def2SpillMap.find(DefIndex);
Evan Cheng54898932008-10-29 08:39:34 +0000372 if (II == Def2SpillMap.end())
373 return false;
374
375 // If last spill of def is in the same mbb as barrier mbb (where restore will
376 // be), make sure it's not below the intended restore index.
377 // FIXME: Undo the previous spill?
378 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
379 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
380 return false;
381
382 SS = I->second;
383 SpillIndex = II->second;
384 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000385}
386
Evan Chengd0e32c52008-10-29 05:06:14 +0000387/// UpdateSpillSlotInterval - Given the specified val# of the register live
388/// interval being split, and the spill and restore indicies, update the live
389/// interval of the spill stack slot.
390void
Lang Hames233a60e2009-11-03 23:52:08 +0000391PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, SlotIndex SpillIndex,
392 SlotIndex RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000393 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
394 "Expect restore in the barrier mbb");
395
396 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
397 if (MBB == BarrierMBB) {
398 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000399 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
400 CurrSLI->addRange(SLR);
401 return;
402 }
403
Evan Cheng54898932008-10-29 08:39:34 +0000404 SmallPtrSet<MachineBasicBlock*, 4> Processed;
Lang Hames233a60e2009-11-03 23:52:08 +0000405 SlotIndex EndIdx = LIs->getMBBEndIdx(MBB);
406 LiveRange SLR(SpillIndex, EndIdx.getNextSlot(), CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000407 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000408 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000409
410 // Start from the spill mbb, figure out the extend of the spill slot's
411 // live interval.
412 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000413 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
414 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000415 // If live range extend beyond end of mbb, add successors to work list.
416 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
417 SE = MBB->succ_end(); SI != SE; ++SI)
418 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000419
420 while (!WorkList.empty()) {
421 MachineBasicBlock *MBB = WorkList.back();
422 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000423 if (Processed.count(MBB))
424 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000425 SlotIndex Idx = LIs->getMBBStartIdx(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000426 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000427 if (LR && LR->valno == ValNo) {
428 EndIdx = LIs->getMBBEndIdx(MBB);
429 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000430 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000431 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000432 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000433 } else if (LR->end > EndIdx) {
434 // Live range extends beyond end of mbb, process successors.
Lang Hames233a60e2009-11-03 23:52:08 +0000435 LiveRange SLR(Idx, EndIdx.getNextIndex(), CurrSValNo);
Evan Cheng54898932008-10-29 08:39:34 +0000436 CurrSLI->addRange(SLR);
437 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
438 SE = MBB->succ_end(); SI != SE; ++SI)
439 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000440 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000441 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000442 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000443 }
Evan Cheng54898932008-10-29 08:39:34 +0000444 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000445 }
446 }
447}
448
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000449/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
450/// construction algorithm to compute the ranges and valnos for an interval.
Evan Cheng19a72582009-02-02 18:33:18 +0000451VNInfo*
452PreAllocSplitting::PerformPHIConstruction(MachineBasicBlock::iterator UseI,
453 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000454 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000455 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
456 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
457 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000458 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
459 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000460 bool IsTopLevel, bool IsIntraBlock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000461 // Return memoized result if it's available.
Evan Cheng19a72582009-02-02 18:33:18 +0000462 if (IsTopLevel && Visited.count(UseI) && NewVNs.count(UseI))
463 return NewVNs[UseI];
464 else if (!IsTopLevel && IsIntraBlock && NewVNs.count(UseI))
465 return NewVNs[UseI];
466 else if (!IsIntraBlock && LiveOut.count(MBB))
Owen Anderson7d211e22008-12-31 02:00:25 +0000467 return LiveOut[MBB];
468
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000469 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000470 bool ContainsDefs = Defs.count(MBB);
471 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000472
Evan Cheng19a72582009-02-02 18:33:18 +0000473 VNInfo* RetVNI = 0;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000474
475 // Enumerate the cases of use/def contaning blocks.
476 if (!ContainsDefs && !ContainsUses) {
Evan Cheng19a72582009-02-02 18:33:18 +0000477 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
478 NewVNs, LiveOut, Phis,
479 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000480 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000481 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000482
483 // Search for the def in this block. If we don't find it before the
484 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000485 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000486 // always be an end() iterator.
Evan Cheng19a72582009-02-02 18:33:18 +0000487 assert(UseI == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000488
Evan Cheng19a72582009-02-02 18:33:18 +0000489 MachineBasicBlock::iterator Walker = UseI;
490 --Walker;
491 while (Walker != MBB->begin()) {
492 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000493 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000494 --Walker;
495 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000496
497 // Once we've found it, extend its VNInfo to our instruction.
Lang Hames233a60e2009-11-03 23:52:08 +0000498 SlotIndex DefIndex = LIs->getInstructionIndex(Walker);
499 DefIndex = DefIndex.getDefIndex();
500 SlotIndex EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000501
Evan Cheng19a72582009-02-02 18:33:18 +0000502 RetVNI = NewVNs[Walker];
Lang Hames233a60e2009-11-03 23:52:08 +0000503 LI->addRange(LiveRange(DefIndex, EndIndex.getNextSlot(), RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000504 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000505 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000506
507 // Search for the use in this block that precedes the instruction we care
Evan Cheng19a72582009-02-02 18:33:18 +0000508 // about, going to the fallback case if we don't find it.
509 if (UseI == MBB->begin())
510 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
511 Uses, NewVNs, LiveOut, Phis,
512 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000513
Evan Cheng19a72582009-02-02 18:33:18 +0000514 MachineBasicBlock::iterator Walker = UseI;
515 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000516 bool found = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000517 while (Walker != MBB->begin()) {
518 if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000519 found = true;
520 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000521 }
522 --Walker;
523 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000524
525 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000526 if (!found) {
Evan Cheng19a72582009-02-02 18:33:18 +0000527 if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000528 found = true;
529 else
Evan Cheng19a72582009-02-02 18:33:18 +0000530 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
531 Uses, NewVNs, LiveOut, Phis,
532 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000533 }
534
Lang Hames233a60e2009-11-03 23:52:08 +0000535 SlotIndex UseIndex = LIs->getInstructionIndex(Walker);
536 UseIndex = UseIndex.getUseIndex();
537 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000538 if (IsIntraBlock) {
539 EndIndex = LIs->getInstructionIndex(UseI);
Lang Hames233a60e2009-11-03 23:52:08 +0000540 EndIndex = EndIndex.getUseIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000541 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000542 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000543
544 // Now, recursively phi construct the VNInfo for the use we found,
545 // and then extend it to include the instruction we care about
Evan Cheng19a72582009-02-02 18:33:18 +0000546 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
547 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000548
Lang Hames233a60e2009-11-03 23:52:08 +0000549 LI->addRange(LiveRange(UseIndex, EndIndex.getNextSlot(), RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000550
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000551 // FIXME: Need to set kills properly for inter-block stuff.
Lang Hames86511252009-09-04 20:41:11 +0000552 if (RetVNI->isKill(UseIndex)) RetVNI->removeKill(UseIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000553 if (IsIntraBlock)
Lang Hames86511252009-09-04 20:41:11 +0000554 RetVNI->addKill(EndIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000555 } else if (ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000556 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
557 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000558
559 // This case is basically a merging of the two preceding case, with the
560 // special note that checking for defs must take precedence over checking
561 // for uses, because of two-address instructions.
562
Evan Cheng19a72582009-02-02 18:33:18 +0000563 if (UseI == MBB->begin())
564 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
565 NewVNs, LiveOut, Phis,
566 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000567
Evan Cheng19a72582009-02-02 18:33:18 +0000568 MachineBasicBlock::iterator Walker = UseI;
569 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000570 bool foundDef = false;
571 bool foundUse = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000572 while (Walker != MBB->begin()) {
573 if (BlockDefs.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000574 foundDef = true;
575 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000576 } else if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000577 foundUse = true;
578 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000579 }
580 --Walker;
581 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000582
583 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000584 if (!foundDef && !foundUse) {
Evan Cheng19a72582009-02-02 18:33:18 +0000585 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000586 foundDef = true;
Evan Cheng19a72582009-02-02 18:33:18 +0000587 else if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000588 foundUse = true;
589 else
Evan Cheng19a72582009-02-02 18:33:18 +0000590 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
591 Uses, NewVNs, LiveOut, Phis,
592 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000593 }
594
Lang Hames233a60e2009-11-03 23:52:08 +0000595 SlotIndex StartIndex = LIs->getInstructionIndex(Walker);
596 StartIndex = foundDef ? StartIndex.getDefIndex() : StartIndex.getUseIndex();
597 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000598 if (IsIntraBlock) {
599 EndIndex = LIs->getInstructionIndex(UseI);
Lang Hames233a60e2009-11-03 23:52:08 +0000600 EndIndex = EndIndex.getUseIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000601 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000602 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000603
604 if (foundDef)
Evan Cheng19a72582009-02-02 18:33:18 +0000605 RetVNI = NewVNs[Walker];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000606 else
Evan Cheng19a72582009-02-02 18:33:18 +0000607 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
608 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000609
Lang Hames233a60e2009-11-03 23:52:08 +0000610 LI->addRange(LiveRange(StartIndex, EndIndex.getNextSlot(), RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000611
Lang Hames86511252009-09-04 20:41:11 +0000612 if (foundUse && RetVNI->isKill(StartIndex))
613 RetVNI->removeKill(StartIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000614 if (IsIntraBlock) {
Lang Hames86511252009-09-04 20:41:11 +0000615 RetVNI->addKill(EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000616 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000617 }
618
619 // Memoize results so we don't have to recompute them.
Evan Cheng19a72582009-02-02 18:33:18 +0000620 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000621 else {
Evan Cheng19a72582009-02-02 18:33:18 +0000622 if (!NewVNs.count(UseI))
623 NewVNs[UseI] = RetVNI;
624 Visited.insert(UseI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000625 }
626
Evan Cheng19a72582009-02-02 18:33:18 +0000627 return RetVNI;
628}
629
630/// PerformPHIConstructionFallBack - PerformPHIConstruction fall back path.
631///
632VNInfo*
633PreAllocSplitting::PerformPHIConstructionFallBack(MachineBasicBlock::iterator UseI,
634 MachineBasicBlock* MBB, LiveInterval* LI,
635 SmallPtrSet<MachineInstr*, 4>& Visited,
636 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
637 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
638 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
639 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
640 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
641 bool IsTopLevel, bool IsIntraBlock) {
642 // NOTE: Because this is the fallback case from other cases, we do NOT
643 // assume that we are not intrablock here.
644 if (Phis.count(MBB)) return Phis[MBB];
645
Lang Hames233a60e2009-11-03 23:52:08 +0000646 SlotIndex StartIndex = LIs->getMBBStartIdx(MBB);
Lang Hames857c4e02009-06-17 21:01:20 +0000647 VNInfo *RetVNI = Phis[MBB] =
Lang Hames233a60e2009-11-03 23:52:08 +0000648 LI->getNextValue(SlotIndex(), /*FIXME*/ 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000649 LIs->getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +0000650
Evan Cheng19a72582009-02-02 18:33:18 +0000651 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
652
653 // If there are no uses or defs between our starting point and the
654 // beginning of the block, then recursive perform phi construction
655 // on our predecessors.
656 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
657 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
658 PE = MBB->pred_end(); PI != PE; ++PI) {
659 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
660 Visited, Defs, Uses, NewVNs,
661 LiveOut, Phis, false, false);
662 if (Incoming != 0)
663 IncomingVNs[*PI] = Incoming;
664 }
665
Lang Hames857c4e02009-06-17 21:01:20 +0000666 if (MBB->pred_size() == 1 && !RetVNI->hasPHIKill()) {
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000667 VNInfo* OldVN = RetVNI;
668 VNInfo* NewVN = IncomingVNs.begin()->second;
669 VNInfo* MergedVN = LI->MergeValueNumberInto(OldVN, NewVN);
670 if (MergedVN == OldVN) std::swap(OldVN, NewVN);
671
672 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator LOI = LiveOut.begin(),
673 LOE = LiveOut.end(); LOI != LOE; ++LOI)
674 if (LOI->second == OldVN)
675 LOI->second = MergedVN;
676 for (DenseMap<MachineInstr*, VNInfo*>::iterator NVI = NewVNs.begin(),
677 NVE = NewVNs.end(); NVI != NVE; ++NVI)
678 if (NVI->second == OldVN)
679 NVI->second = MergedVN;
680 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator PI = Phis.begin(),
681 PE = Phis.end(); PI != PE; ++PI)
682 if (PI->second == OldVN)
683 PI->second = MergedVN;
684 RetVNI = MergedVN;
Evan Cheng19a72582009-02-02 18:33:18 +0000685 } else {
686 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
687 // VNInfo to represent the joined value.
688 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
689 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
Lang Hames857c4e02009-06-17 21:01:20 +0000690 I->second->setHasPHIKill(true);
Lang Hames233a60e2009-11-03 23:52:08 +0000691 SlotIndex KillIndex = LIs->getMBBEndIdx(I->first);
Lang Hames86511252009-09-04 20:41:11 +0000692 if (!I->second->isKill(KillIndex))
693 I->second->addKill(KillIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000694 }
695 }
696
Lang Hames233a60e2009-11-03 23:52:08 +0000697 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000698 if (IsIntraBlock) {
699 EndIndex = LIs->getInstructionIndex(UseI);
Lang Hames233a60e2009-11-03 23:52:08 +0000700 EndIndex = EndIndex.getUseIndex();
Evan Cheng19a72582009-02-02 18:33:18 +0000701 } else
702 EndIndex = LIs->getMBBEndIdx(MBB);
Lang Hames233a60e2009-11-03 23:52:08 +0000703 LI->addRange(LiveRange(StartIndex, EndIndex.getNextSlot(), RetVNI));
Evan Cheng19a72582009-02-02 18:33:18 +0000704 if (IsIntraBlock)
Lang Hames86511252009-09-04 20:41:11 +0000705 RetVNI->addKill(EndIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000706
707 // Memoize results so we don't have to recompute them.
708 if (!IsIntraBlock)
709 LiveOut[MBB] = RetVNI;
710 else {
711 if (!NewVNs.count(UseI))
712 NewVNs[UseI] = RetVNI;
713 Visited.insert(UseI);
714 }
715
716 return RetVNI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000717}
718
719/// ReconstructLiveInterval - Recompute a live interval from scratch.
720void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
721 BumpPtrAllocator& Alloc = LIs->getVNInfoAllocator();
722
723 // Clear the old ranges and valnos;
724 LI->clear();
725
726 // Cache the uses and defs of the register
727 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
728 RegMap Defs, Uses;
729
730 // Keep track of the new VNs we're creating.
731 DenseMap<MachineInstr*, VNInfo*> NewVNs;
732 SmallPtrSet<VNInfo*, 2> PhiVNs;
733
734 // Cache defs, and create a new VNInfo for each def.
735 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
736 DE = MRI->def_end(); DI != DE; ++DI) {
737 Defs[(*DI).getParent()].insert(&*DI);
738
Lang Hames233a60e2009-11-03 23:52:08 +0000739 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
740 DefIdx = DefIdx.getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000741
Lang Hames857c4e02009-06-17 21:01:20 +0000742 assert(DI->getOpcode() != TargetInstrInfo::PHI &&
743 "Following NewVN isPHIDef flag incorrect. Fix me!");
744 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, true, Alloc);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000745
746 // If the def is a move, set the copy field.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000747 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
748 if (TII->isMoveInstr(*DI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
749 if (DstReg == LI->reg)
Lang Hames52c1afc2009-08-10 23:43:28 +0000750 NewVN->setCopy(&*DI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000751
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000752 NewVNs[&*DI] = NewVN;
753 }
754
755 // Cache uses as a separate pass from actually processing them.
756 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
757 UE = MRI->use_end(); UI != UE; ++UI)
758 Uses[(*UI).getParent()].insert(&*UI);
759
760 // Now, actually process every use and use a phi construction algorithm
761 // to walk from it to its reaching definitions, building VNInfos along
762 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000763 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
764 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000765 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000766 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
767 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000768 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000769 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000770 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000771
772 // Add ranges for dead defs
773 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
774 DE = MRI->def_end(); DI != DE; ++DI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000775 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
776 DefIdx = DefIdx.getDefIndex();
Owen Andersond4f6fe52008-12-28 23:35:13 +0000777
778 if (LI->liveAt(DefIdx)) continue;
779
780 VNInfo* DeadVN = NewVNs[&*DI];
Lang Hames233a60e2009-11-03 23:52:08 +0000781 LI->addRange(LiveRange(DefIdx, DefIdx.getNextSlot(), DeadVN));
Lang Hames86511252009-09-04 20:41:11 +0000782 DeadVN->addKill(DefIdx);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000783 }
Evan Cheng35ca9202009-10-09 01:17:11 +0000784
785 // Update kill markers.
786 for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
787 VI != VE; ++VI) {
788 VNInfo* VNI = *VI;
789 for (unsigned i = 0, e = VNI->kills.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +0000790 SlotIndex KillIdx = VNI->kills[i];
791 if (KillIdx.isPHI())
Evan Cheng35ca9202009-10-09 01:17:11 +0000792 continue;
793 MachineInstr *KillMI = LIs->getInstructionFromIndex(KillIdx);
794 if (KillMI) {
795 MachineOperand *KillMO = KillMI->findRegisterUseOperand(CurrLI->reg);
796 if (KillMO)
797 // It could be a dead def.
798 KillMO->setIsKill();
799 }
800 }
801 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000802}
803
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000804/// RenumberValno - Split the given valno out into a new vreg, allowing it to
805/// be allocated to a different register. This function creates a new vreg,
806/// copies the valno and its live ranges over to the new vreg's interval,
807/// removes them from the old interval, and rewrites all uses and defs of
808/// the original reg to the new vreg within those ranges.
809void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000810 SmallVector<VNInfo*, 4> Stack;
811 SmallVector<VNInfo*, 4> VNsToCopy;
812 Stack.push_back(VN);
813
814 // Walk through and copy the valno we care about, and any other valnos
815 // that are two-address redefinitions of the one we care about. These
816 // will need to be rewritten as well. We also check for safety of the
817 // renumbering here, by making sure that none of the valno involved has
818 // phi kills.
819 while (!Stack.empty()) {
820 VNInfo* OldVN = Stack.back();
821 Stack.pop_back();
822
823 // Bail out if we ever encounter a valno that has a PHI kill. We can't
824 // renumber these.
Lang Hames857c4e02009-06-17 21:01:20 +0000825 if (OldVN->hasPHIKill()) return;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000826
827 VNsToCopy.push_back(OldVN);
828
829 // Locate two-address redefinitions
Lang Hamesffd13262009-07-09 03:57:02 +0000830 for (VNInfo::KillSet::iterator KI = OldVN->kills.begin(),
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000831 KE = OldVN->kills.end(); KI != KE; ++KI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000832 assert(!KI->isPHI() &&
Lang Hames86511252009-09-04 20:41:11 +0000833 "VN previously reported having no PHI kills.");
834 MachineInstr* MI = LIs->getInstructionFromIndex(*KI);
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000835 unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg);
836 if (DefIdx == ~0U) continue;
Bob Wilsond9df5012009-04-09 17:16:43 +0000837 if (MI->isRegTiedToUseOperand(DefIdx)) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000838 VNInfo* NextVN =
Lang Hames233a60e2009-11-03 23:52:08 +0000839 CurrLI->findDefinedVNInfoForRegInt(KI->getDefIndex());
Owen Andersonb4b84362009-01-26 21:57:31 +0000840 if (NextVN == OldVN) continue;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000841 Stack.push_back(NextVN);
842 }
843 }
844 }
845
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000846 // Create the new vreg
847 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
848
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000849 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000850 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000851
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000852 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
853 VNsToCopy.end(); OI != OE; ++OI) {
854 VNInfo* OldVN = *OI;
855
856 // Copy the valno over
Lang Hames857c4e02009-06-17 21:01:20 +0000857 VNInfo* NewVN = NewLI.createValueCopy(OldVN, LIs->getVNInfoAllocator());
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000858 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
859
860 // Remove the valno from the old interval
861 CurrLI->removeValNo(OldVN);
862 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000863
864 // Rewrite defs and uses. This is done in two stages to avoid invalidating
865 // the reg_iterator.
866 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
867
868 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
869 E = MRI->reg_end(); I != E; ++I) {
870 MachineOperand& MO = I.getOperand();
Lang Hames233a60e2009-11-03 23:52:08 +0000871 SlotIndex InstrIdx = LIs->getInstructionIndex(&*I);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000872
Lang Hames233a60e2009-11-03 23:52:08 +0000873 if ((MO.isUse() && NewLI.liveAt(InstrIdx.getUseIndex())) ||
874 (MO.isDef() && NewLI.liveAt(InstrIdx.getDefIndex())))
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000875 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
876 }
877
878 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
879 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
880 MachineInstr* Inst = I->first;
881 unsigned OpIdx = I->second;
882 MachineOperand& MO = Inst->getOperand(OpIdx);
883 MO.setReg(NewVReg);
884 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000885
Owen Anderson420dd372009-03-14 21:40:05 +0000886 // Grow the VirtRegMap, since we've created a new vreg.
887 VRM->grow();
888
Owen Anderson45e68552009-01-29 05:28:55 +0000889 // The renumbered vreg shares a stack slot with the old register.
890 if (IntervalSSMap.count(CurrLI->reg))
891 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
892
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000893 NumRenumbers++;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000894}
895
Evan Cheng37844532009-07-16 09:20:10 +0000896bool PreAllocSplitting::Rematerialize(unsigned VReg, VNInfo* ValNo,
Owen Anderson6002e992008-12-04 21:20:30 +0000897 MachineInstr* DefMI,
898 MachineBasicBlock::iterator RestorePt,
Lang Hames233a60e2009-11-03 23:52:08 +0000899 SlotIndex RestoreIdx,
Owen Anderson6002e992008-12-04 21:20:30 +0000900 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
901 MachineBasicBlock& MBB = *RestorePt->getParent();
902
903 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
Lang Hames233a60e2009-11-03 23:52:08 +0000904 SlotIndex KillIdx;
Lang Hames857c4e02009-06-17 21:01:20 +0000905 if (!ValNo->isDefAccurate() || DefMI->getParent() == BarrierMBB)
Owen Anderson6002e992008-12-04 21:20:30 +0000906 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx);
907 else
908 KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx);
909
910 if (KillPt == DefMI->getParent()->end())
911 return false;
912
Evan Cheng37844532009-07-16 09:20:10 +0000913 TII->reMaterialize(MBB, RestorePt, VReg, 0, DefMI);
Owen Anderson6002e992008-12-04 21:20:30 +0000914 LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx);
915
Owen Andersonb4b84362009-01-26 21:57:31 +0000916 ReconstructLiveInterval(CurrLI);
Lang Hames233a60e2009-11-03 23:52:08 +0000917 SlotIndex RematIdx = LIs->getInstructionIndex(prior(RestorePt));
918 RematIdx = RematIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +0000919 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000920
Owen Anderson75fa96b2008-11-19 04:28:29 +0000921 ++NumSplits;
922 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000923 return true;
924}
925
926MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
927 const TargetRegisterClass* RC,
928 MachineInstr* DefMI,
929 MachineInstr* Barrier,
930 MachineBasicBlock* MBB,
931 int& SS,
932 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
933 MachineBasicBlock::iterator Pt = MBB->begin();
934
935 // Go top down if RefsInMBB is empty.
936 if (RefsInMBB.empty())
937 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000938
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000939 MachineBasicBlock::iterator FoldPt = Barrier;
940 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
941 !RefsInMBB.count(FoldPt))
942 --FoldPt;
943
944 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
945 if (OpIdx == -1)
946 return 0;
947
948 SmallVector<unsigned, 1> Ops;
949 Ops.push_back(OpIdx);
950
951 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
952 return 0;
953
954 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
955 if (I != IntervalSSMap.end()) {
956 SS = I->second;
957 } else {
Evan Cheng491f54f2009-10-17 09:20:14 +0000958 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000959 }
960
961 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
962 FoldPt, Ops, SS);
963
964 if (FMI) {
965 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
966 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
967 ++NumFolds;
968
969 IntervalSSMap[vreg] = SS;
Evan Chengc781a242009-05-03 18:32:42 +0000970 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000971 if (CurrSLI->hasAtLeastOneValue())
972 CurrSValNo = CurrSLI->getValNumInfo(0);
973 else
Lang Hames233a60e2009-11-03 23:52:08 +0000974 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000975 LSs->getVNInfoAllocator());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000976 }
977
978 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000979}
980
Owen Andersonc93023a2009-03-04 08:52:31 +0000981MachineInstr* PreAllocSplitting::FoldRestore(unsigned vreg,
982 const TargetRegisterClass* RC,
983 MachineInstr* Barrier,
984 MachineBasicBlock* MBB,
985 int SS,
986 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Andersona2bfb542009-03-05 08:23:20 +0000987 if ((int)RestoreFoldLimit != -1 && RestoreFoldLimit == (int)NumRestoreFolds)
Owen Anderson323c58d2009-03-05 07:19:18 +0000988 return 0;
989
Owen Andersonc93023a2009-03-04 08:52:31 +0000990 // Go top down if RefsInMBB is empty.
991 if (RefsInMBB.empty())
992 return 0;
993
994 // Can't fold a restore between a call stack setup and teardown.
995 MachineBasicBlock::iterator FoldPt = Barrier;
Owen Anderson323c58d2009-03-05 07:19:18 +0000996
997 // Advance from barrier to call frame teardown.
998 while (FoldPt != MBB->getFirstTerminator() &&
999 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
1000 if (RefsInMBB.count(FoldPt))
1001 return 0;
Owen Andersonc93023a2009-03-04 08:52:31 +00001002
Owen Anderson323c58d2009-03-05 07:19:18 +00001003 ++FoldPt;
1004 }
1005
1006 if (FoldPt == MBB->getFirstTerminator())
1007 return 0;
1008 else
1009 ++FoldPt;
1010
1011 // Now find the restore point.
1012 while (FoldPt != MBB->getFirstTerminator() && !RefsInMBB.count(FoldPt)) {
Owen Andersonc93023a2009-03-04 08:52:31 +00001013 if (FoldPt->getOpcode() == TRI->getCallFrameSetupOpcode()) {
1014 while (FoldPt != MBB->getFirstTerminator() &&
1015 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
1016 if (RefsInMBB.count(FoldPt))
1017 return 0;
1018
1019 ++FoldPt;
1020 }
1021
Owen Anderson323c58d2009-03-05 07:19:18 +00001022 if (FoldPt == MBB->getFirstTerminator())
1023 return 0;
1024 }
1025
1026 ++FoldPt;
Owen Andersonc93023a2009-03-04 08:52:31 +00001027 }
1028
1029 if (FoldPt == MBB->getFirstTerminator())
1030 return 0;
1031
1032 int OpIdx = FoldPt->findRegisterUseOperandIdx(vreg, true);
1033 if (OpIdx == -1)
1034 return 0;
1035
1036 SmallVector<unsigned, 1> Ops;
1037 Ops.push_back(OpIdx);
1038
1039 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
1040 return 0;
1041
1042 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
1043 FoldPt, Ops, SS);
1044
1045 if (FMI) {
1046 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
1047 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
Owen Anderson323c58d2009-03-05 07:19:18 +00001048 ++NumRestoreFolds;
Owen Andersonc93023a2009-03-04 08:52:31 +00001049 }
1050
1051 return FMI;
1052}
1053
Evan Chengf5cd4f02008-10-23 20:43:13 +00001054/// SplitRegLiveInterval - Split (spill and restore) the given live interval
1055/// so it would not cross the barrier that's being processed. Shrink wrap
1056/// (minimize) the live interval to the last uses.
1057bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
Lang Hames233a60e2009-11-03 23:52:08 +00001058 DEBUG(errs() << "Pre-alloc splitting " << LI->reg << " for " << *Barrier
1059 << " result: ");
1060
Evan Chengf5cd4f02008-10-23 20:43:13 +00001061 CurrLI = LI;
1062
1063 // Find live range where current interval cross the barrier.
1064 LiveInterval::iterator LR =
Lang Hames233a60e2009-11-03 23:52:08 +00001065 CurrLI->FindLiveRangeContaining(BarrierIdx.getUseIndex());
Evan Chengf5cd4f02008-10-23 20:43:13 +00001066 VNInfo *ValNo = LR->valno;
1067
Torok Edwinf3689232009-07-12 20:07:01 +00001068 assert(!ValNo->isUnused() && "Val# is defined by a dead def?");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001069
Lang Hames857c4e02009-06-17 21:01:20 +00001070 MachineInstr *DefMI = ValNo->isDefAccurate()
Evan Cheng06587492008-10-24 02:05:00 +00001071 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +00001072
Owen Andersond3be4622009-01-21 08:18:03 +00001073 // If this would create a new join point, do not split.
Lang Hames233a60e2009-11-03 23:52:08 +00001074 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent())) {
1075 DEBUG(errs() << "FAILED (would create a new join point).\n");
Owen Andersond3be4622009-01-21 08:18:03 +00001076 return false;
Lang Hames233a60e2009-11-03 23:52:08 +00001077 }
Owen Andersond3be4622009-01-21 08:18:03 +00001078
Evan Chengf5cd4f02008-10-23 20:43:13 +00001079 // Find all references in the barrier mbb.
1080 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
1081 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
1082 E = MRI->reg_end(); I != E; ++I) {
1083 MachineInstr *RefMI = &*I;
1084 if (RefMI->getParent() == BarrierMBB)
1085 RefsInMBB.insert(RefMI);
1086 }
1087
1088 // Find a point to restore the value after the barrier.
Lang Hames233a60e2009-11-03 23:52:08 +00001089 SlotIndex RestoreIndex;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001090 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +00001091 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Lang Hames233a60e2009-11-03 23:52:08 +00001092 if (RestorePt == BarrierMBB->end()) {
1093 DEBUG(errs() << "FAILED (could not find a suitable restore point).\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001094 return false;
Lang Hames233a60e2009-11-03 23:52:08 +00001095 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001096
Owen Anderson75fa96b2008-11-19 04:28:29 +00001097 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
1098 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt,
Lang Hames233a60e2009-11-03 23:52:08 +00001099 RestoreIndex, RefsInMBB)) {
1100 DEBUG(errs() << "success (remat).\n");
1101 return true;
1102 }
Owen Anderson75fa96b2008-11-19 04:28:29 +00001103
Evan Chengf5cd4f02008-10-23 20:43:13 +00001104 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001105 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001106 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Lang Hames233a60e2009-11-03 23:52:08 +00001107 SlotIndex SpillIndex;
Evan Cheng06587492008-10-24 02:05:00 +00001108 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001109 int SS = -1;
Lang Hames857c4e02009-06-17 21:01:20 +00001110 if (!ValNo->isDefAccurate()) {
1111 // If we don't know where the def is we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001112 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1113 BarrierMBB, SS, RefsInMBB))) {
1114 SpillIndex = LIs->getInstructionIndex(SpillMI);
1115 } else {
1116 MachineBasicBlock::iterator SpillPt =
1117 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
Lang Hames233a60e2009-11-03 23:52:08 +00001118 if (SpillPt == BarrierMBB->begin()) {
1119 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001120 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001121 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001122 // Add spill.
1123
1124 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1125 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
1126 SpillMI = prior(SpillPt);
1127 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
1128 }
Evan Cheng54898932008-10-29 08:39:34 +00001129 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
1130 RestoreIndex, SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001131 // If it's already split, just restore the value. There is no need to spill
1132 // the def again.
Lang Hames233a60e2009-11-03 23:52:08 +00001133 if (!DefMI) {
1134 DEBUG(errs() << "FAILED (def is dead).\n");
Evan Chengd0e32c52008-10-29 05:06:14 +00001135 return false; // Def is dead. Do nothing.
Lang Hames233a60e2009-11-03 23:52:08 +00001136 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001137
1138 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
Evan Cheng35ca9202009-10-09 01:17:11 +00001139 BarrierMBB, SS, RefsInMBB))) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001140 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001141 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001142 // Check if it's possible to insert a spill after the def MI.
1143 MachineBasicBlock::iterator SpillPt;
1144 if (DefMBB == BarrierMBB) {
1145 // Add spill after the def and the last use before the barrier.
1146 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
1147 RefsInMBB, SpillIndex);
Lang Hames233a60e2009-11-03 23:52:08 +00001148 if (SpillPt == DefMBB->begin()) {
1149 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001150 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001151 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001152 } else {
1153 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
Lang Hames233a60e2009-11-03 23:52:08 +00001154 if (SpillPt == DefMBB->end()) {
1155 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001156 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001157 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001158 }
Evan Cheng35ca9202009-10-09 01:17:11 +00001159 // Add spill.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001160 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng35ca9202009-10-09 01:17:11 +00001161 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg, false, SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001162 SpillMI = prior(SpillPt);
1163 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001164 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001165 }
1166
Evan Cheng54898932008-10-29 08:39:34 +00001167 // Remember def instruction index to spill index mapping.
1168 if (DefMI && SpillMI)
1169 Def2SpillMap[ValNo->def] = SpillIndex;
1170
Evan Chengf5cd4f02008-10-23 20:43:13 +00001171 // Add restore.
Owen Andersonc93023a2009-03-04 08:52:31 +00001172 bool FoldedRestore = false;
1173 if (MachineInstr* LMI = FoldRestore(CurrLI->reg, RC, Barrier,
1174 BarrierMBB, SS, RefsInMBB)) {
1175 RestorePt = LMI;
Owen Anderson323c58d2009-03-05 07:19:18 +00001176 RestoreIndex = LIs->getInstructionIndex(RestorePt);
Owen Andersonc93023a2009-03-04 08:52:31 +00001177 FoldedRestore = true;
1178 } else {
1179 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
1180 MachineInstr *LoadMI = prior(RestorePt);
1181 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
1182 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001183
Evan Chengd0e32c52008-10-29 05:06:14 +00001184 // Update spill stack slot live interval.
Lang Hames233a60e2009-11-03 23:52:08 +00001185 UpdateSpillSlotInterval(ValNo, SpillIndex.getUseIndex().getNextSlot(),
1186 RestoreIndex.getDefIndex());
Evan Chengd0e32c52008-10-29 05:06:14 +00001187
Owen Andersonb4b84362009-01-26 21:57:31 +00001188 ReconstructLiveInterval(CurrLI);
Evan Cheng35ca9202009-10-09 01:17:11 +00001189
Owen Andersonc93023a2009-03-04 08:52:31 +00001190 if (!FoldedRestore) {
Lang Hames233a60e2009-11-03 23:52:08 +00001191 SlotIndex RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
1192 RestoreIdx = RestoreIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +00001193 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RestoreIdx));
Owen Andersonc93023a2009-03-04 08:52:31 +00001194 }
Owen Anderson7d211e22008-12-31 02:00:25 +00001195
Evan Chengae7fa5b2008-10-28 01:48:24 +00001196 ++NumSplits;
Lang Hames233a60e2009-11-03 23:52:08 +00001197 DEBUG(errs() << "success.\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001198 return true;
1199}
1200
1201/// SplitRegLiveIntervals - Split all register live intervals that cross the
1202/// barrier that's being processed.
1203bool
Owen Anderson956ec272009-01-23 00:23:32 +00001204PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1205 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001206 // First find all the virtual registers whose live intervals are intercepted
1207 // by the current barrier.
1208 SmallVector<LiveInterval*, 8> Intervals;
1209 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng4350eb82009-02-06 17:17:30 +00001210 // FIXME: If it's not safe to move any instruction that defines the barrier
1211 // register class, then it means there are some special dependencies which
1212 // codegen is not modelling. Ignore these barriers for now.
1213 if (!TII->isSafeToMoveRegClassDefs(*RC))
Evan Cheng23066282008-10-27 07:14:50 +00001214 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001215 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
1216 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1217 unsigned Reg = VRs[i];
1218 if (!LIs->hasInterval(Reg))
1219 continue;
1220 LiveInterval *LI = &LIs->getInterval(Reg);
1221 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1222 // Virtual register live interval is intercepted by the barrier. We
1223 // should split and shrink wrap its interval if possible.
1224 Intervals.push_back(LI);
1225 }
1226 }
1227
1228 // Process the affected live intervals.
1229 bool Change = false;
1230 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001231 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1232 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001233 LiveInterval *LI = Intervals.back();
1234 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001235 bool result = SplitRegLiveInterval(LI);
1236 if (result) Split.insert(LI);
1237 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001238 }
1239
1240 return Change;
1241}
1242
Owen Anderson45e68552009-01-29 05:28:55 +00001243unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001244 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001245 unsigned Reg, int FrameIndex,
1246 bool& FeedsTwoAddr) {
1247 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001248 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001249 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001250 int StoreFrameIndex;
1251 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001252 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
1253 NonSpills++;
1254
1255 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
Bob Wilsond9df5012009-04-09 17:16:43 +00001256 if (DefIdx != -1 && (*UI)->isRegTiedToUseOperand(DefIdx))
Owen Anderson45e68552009-01-29 05:28:55 +00001257 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001258 }
1259
Owen Anderson45e68552009-01-29 05:28:55 +00001260 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001261}
1262
Owen Anderson956ec272009-01-23 00:23:32 +00001263/// removeDeadSpills - After doing splitting, filter through all intervals we've
1264/// split, and see if any of the spills are unnecessary. If so, remove them.
1265bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1266 bool changed = false;
1267
Owen Anderson4bfc2092009-01-29 05:41:02 +00001268 // Walk over all of the live intervals that were touched by the splitter,
1269 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001270 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1271 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001272 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001273
Owen Anderson4bfc2092009-01-29 05:41:02 +00001274 // First, collect all the uses of the vreg, and sort them by their
1275 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001276 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1277 UE = MRI->use_end(); UI != UE; ++UI) {
Lang Hames233a60e2009-11-03 23:52:08 +00001278 SlotIndex index = LIs->getInstructionIndex(&*UI);
1279 index = index.getUseIndex();
Owen Anderson956ec272009-01-23 00:23:32 +00001280
1281 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001282 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001283 }
1284
Owen Anderson4bfc2092009-01-29 05:41:02 +00001285 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1286 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001287 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1288 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001289
1290 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1291 return changed;
1292
Owen Anderson956ec272009-01-23 00:23:32 +00001293 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001294
1295 // We don't currently try to handle definitions with PHI kills, because
1296 // it would involve processing more than one VNInfo at once.
Lang Hames857c4e02009-06-17 21:01:20 +00001297 if (CurrVN->hasPHIKill()) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001298
Owen Anderson4bfc2092009-01-29 05:41:02 +00001299 // We also don't try to handle the results of PHI joins, since there's
1300 // no defining instruction to analyze.
Lang Hames857c4e02009-06-17 21:01:20 +00001301 if (!CurrVN->isDefAccurate() || CurrVN->isUnused()) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001302
Owen Anderson4bfc2092009-01-29 05:41:02 +00001303 // We're only interested in eliminating cruft introduced by the splitter,
1304 // is of the form load-use or load-use-store. First, check that the
1305 // definition is a load, and remember what stack slot we loaded it from.
Lang Hames857c4e02009-06-17 21:01:20 +00001306 MachineInstr* DefMI = LIs->getInstructionFromIndex(CurrVN->def);
Owen Anderson956ec272009-01-23 00:23:32 +00001307 int FrameIndex;
1308 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1309
Owen Anderson4bfc2092009-01-29 05:41:02 +00001310 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001311 if (VNUseCount[CurrVN].size() == 0) {
1312 LIs->RemoveMachineInstrFromMaps(DefMI);
1313 (*LI)->removeValNo(CurrVN);
1314 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001315 VNUseCount.erase(CurrVN);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001316 NumDeadSpills++;
1317 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001318 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001319 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001320
Owen Anderson4bfc2092009-01-29 05:41:02 +00001321 // Second, get the number of non-store uses of the definition, as well as
1322 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001323 bool FeedsTwoAddr = false;
1324 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1325 (*LI)->reg, FrameIndex,
1326 FeedsTwoAddr);
1327
Owen Anderson4bfc2092009-01-29 05:41:02 +00001328 // If there's one non-store use and it doesn't feed a two-addr, then
1329 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001330 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001331 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001332 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1333 int StoreFrameIndex;
1334 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1335 while (UI != VNUseCount[CurrVN].end() &&
1336 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1337 ++UI;
1338 if (UI != VNUseCount[CurrVN].end())
1339 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1340 }
Owen Anderson45e68552009-01-29 05:28:55 +00001341 if (UI == VNUseCount[CurrVN].end()) continue;
1342
1343 MachineInstr* use = *UI;
1344
Owen Anderson4bfc2092009-01-29 05:41:02 +00001345 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001346 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1347 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001348 SmallVector<unsigned, 1> Ops;
1349 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001350 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1351
1352 MachineInstr* NewMI =
1353 TII->foldMemoryOperand(*use->getParent()->getParent(),
1354 use, Ops, FrameIndex);
1355
1356 if (!NewMI) continue;
1357
Owen Anderson4bfc2092009-01-29 05:41:02 +00001358 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001359 LIs->RemoveMachineInstrFromMaps(DefMI);
1360 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1361 (*LI)->removeValNo(CurrVN);
1362
1363 DefMI->eraseFromParent();
1364 MachineBasicBlock* MBB = use->getParent();
1365 NewMI = MBB->insert(MBB->erase(use), NewMI);
1366 VNUseCount[CurrVN].erase(use);
1367
Owen Anderson4bfc2092009-01-29 05:41:02 +00001368 // Remove deleted instructions. Note that we need to remove them from
1369 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001370 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1371 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1372 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001373 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001374 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1375 ++VNI)
1376 if (VNI->first != CurrVN)
1377 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001378 LIs->RemoveMachineInstrFromMaps(*II);
1379 (*II)->eraseFromParent();
1380 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001381
1382 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001383
1384 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1385 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1386 if (VI->second.erase(use))
1387 VI->second.insert(NewMI);
1388
1389 NumDeadSpills++;
1390 changed = true;
1391 continue;
1392 }
1393
Owen Anderson4bfc2092009-01-29 05:41:02 +00001394 // If there's more than one non-store instruction, we can't profitably
1395 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001396 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001397
Owen Anderson4bfc2092009-01-29 05:41:02 +00001398 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001399 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1400 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
1401 UI != UI; ++UI) {
1402 LIs->RemoveMachineInstrFromMaps(*UI);
1403 (*UI)->eraseFromParent();
1404 }
1405
Owen Andersonc0f3a032009-01-29 08:22:06 +00001406 VNUseCount.erase(CurrVN);
1407
Owen Anderson32ca8652009-01-24 10:07:43 +00001408 LIs->RemoveMachineInstrFromMaps(DefMI);
1409 (*LI)->removeValNo(CurrVN);
1410 DefMI->eraseFromParent();
1411 NumDeadSpills++;
1412 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001413 }
1414 }
1415
1416 return changed;
1417}
1418
Owen Andersonf1f75b12008-11-04 22:22:41 +00001419bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1420 MachineBasicBlock* DefMBB,
1421 MachineBasicBlock* BarrierMBB) {
1422 if (DefMBB == BarrierMBB)
1423 return false;
1424
Lang Hames857c4e02009-06-17 21:01:20 +00001425 if (LR->valno->hasPHIKill())
Owen Andersonf1f75b12008-11-04 22:22:41 +00001426 return false;
1427
Lang Hames233a60e2009-11-03 23:52:08 +00001428 SlotIndex MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
Owen Andersonf1f75b12008-11-04 22:22:41 +00001429 if (LR->end < MBBEnd)
1430 return false;
1431
1432 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1433 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1434 return true;
1435
1436 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1437 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1438 typedef std::pair<MachineBasicBlock*,
1439 MachineBasicBlock::succ_iterator> ItPair;
1440 SmallVector<ItPair, 4> Stack;
1441 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1442
1443 while (!Stack.empty()) {
1444 ItPair P = Stack.back();
1445 Stack.pop_back();
1446
1447 MachineBasicBlock* PredMBB = P.first;
1448 MachineBasicBlock::succ_iterator S = P.second;
1449
1450 if (S == PredMBB->succ_end())
1451 continue;
1452 else if (Visited.count(*S)) {
1453 Stack.push_back(std::make_pair(PredMBB, ++S));
1454 continue;
1455 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001456 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001457
1458 MachineBasicBlock* MBB = *S;
1459 Visited.insert(MBB);
1460
1461 if (MBB == BarrierMBB)
1462 return true;
1463
1464 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1465 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1466 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1467 while (MDTN) {
1468 if (MDTN == DefMDTN)
1469 return true;
1470 else if (MDTN == BarrierMDTN)
1471 break;
1472 MDTN = MDTN->getIDom();
1473 }
1474
1475 MBBEnd = LIs->getMBBEndIdx(MBB);
1476 if (LR->end > MBBEnd)
1477 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1478 }
1479
1480 return false;
1481}
1482
1483
Evan Cheng09e8ca82008-10-20 21:44:59 +00001484bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001485 CurrMF = &MF;
1486 TM = &MF.getTarget();
Owen Anderson3ef45492009-01-29 22:13:06 +00001487 TRI = TM->getRegisterInfo();
Evan Chengd0e32c52008-10-29 05:06:14 +00001488 TII = TM->getInstrInfo();
1489 MFI = MF.getFrameInfo();
1490 MRI = &MF.getRegInfo();
Lang Hames233a60e2009-11-03 23:52:08 +00001491 SIs = &getAnalysis<SlotIndexes>();
Evan Chengd0e32c52008-10-29 05:06:14 +00001492 LIs = &getAnalysis<LiveIntervals>();
1493 LSs = &getAnalysis<LiveStacks>();
Owen Anderson420dd372009-03-14 21:40:05 +00001494 VRM = &getAnalysis<VirtRegMap>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001495
1496 bool MadeChange = false;
1497
1498 // Make sure blocks are numbered in order.
1499 MF.RenumberBlocks();
1500
Evan Cheng54898932008-10-29 08:39:34 +00001501 MachineBasicBlock *Entry = MF.begin();
1502 SmallPtrSet<MachineBasicBlock*,16> Visited;
1503
Owen Anderson956ec272009-01-23 00:23:32 +00001504 SmallPtrSet<LiveInterval*, 8> Split;
1505
Evan Cheng54898932008-10-29 08:39:34 +00001506 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1507 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1508 DFI != E; ++DFI) {
1509 BarrierMBB = *DFI;
1510 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1511 E = BarrierMBB->end(); I != E; ++I) {
1512 Barrier = &*I;
1513 const TargetRegisterClass **BarrierRCs =
1514 Barrier->getDesc().getRegClassBarriers();
1515 if (!BarrierRCs)
1516 continue;
1517 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001518 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001519 }
1520 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001521
Owen Anderson956ec272009-01-23 00:23:32 +00001522 MadeChange |= removeDeadSpills(Split);
1523
Evan Chengf5cd4f02008-10-23 20:43:13 +00001524 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001525}