blob: f2168b625e0a6c9badc42163a21d3ffecf7695be [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:
136 MachineBasicBlock::iterator
137 findNextEmptySlot(MachineBasicBlock*, MachineInstr*,
Lang Hames233a60e2009-11-03 23:52:08 +0000138 SlotIndex&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000139
140 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000141 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Lang Hames233a60e2009-11-03 23:52:08 +0000142 SmallPtrSet<MachineInstr*, 4>&, SlotIndex&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000143
144 MachineBasicBlock::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000145 findRestorePoint(MachineBasicBlock*, MachineInstr*, SlotIndex,
146 SmallPtrSet<MachineInstr*, 4>&, SlotIndex&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000147
Evan Chengd0e32c52008-10-29 05:06:14 +0000148 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000149
Lang Hames86511252009-09-04 20:41:11 +0000150 bool IsAvailableInStack(MachineBasicBlock*, unsigned,
Lang Hames233a60e2009-11-03 23:52:08 +0000151 SlotIndex, SlotIndex,
152 SlotIndex&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000153
Lang Hames233a60e2009-11-03 23:52:08 +0000154 void UpdateSpillSlotInterval(VNInfo*, SlotIndex, SlotIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000155
Evan Chengf5cd4f02008-10-23 20:43:13 +0000156 bool SplitRegLiveInterval(LiveInterval*);
157
Owen Anderson956ec272009-01-23 00:23:32 +0000158 bool SplitRegLiveIntervals(const TargetRegisterClass **,
159 SmallPtrSet<LiveInterval*, 8>&);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000160
161 bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB,
162 MachineBasicBlock* BarrierMBB);
Owen Anderson75fa96b2008-11-19 04:28:29 +0000163 bool Rematerialize(unsigned vreg, VNInfo* ValNo,
164 MachineInstr* DefMI,
165 MachineBasicBlock::iterator RestorePt,
Lang Hames233a60e2009-11-03 23:52:08 +0000166 SlotIndex RestoreIdx,
Owen Anderson75fa96b2008-11-19 04:28:29 +0000167 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000168 MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC,
169 MachineInstr* DefMI,
170 MachineInstr* Barrier,
171 MachineBasicBlock* MBB,
172 int& SS,
173 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersonc93023a2009-03-04 08:52:31 +0000174 MachineInstr* FoldRestore(unsigned vreg,
175 const TargetRegisterClass* RC,
176 MachineInstr* Barrier,
177 MachineBasicBlock* MBB,
178 int SS,
179 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000180 void RenumberValno(VNInfo* VN);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000181 void ReconstructLiveInterval(LiveInterval* LI);
Owen Anderson956ec272009-01-23 00:23:32 +0000182 bool removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split);
Owen Anderson45e68552009-01-29 05:28:55 +0000183 unsigned getNumberOfNonSpills(SmallPtrSet<MachineInstr*, 4>& MIs,
184 unsigned Reg, int FrameIndex, bool& TwoAddr);
Evan Cheng19a72582009-02-02 18:33:18 +0000185 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator Use,
186 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000187 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000188 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
189 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
190 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000191 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
192 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000193 bool IsTopLevel, bool IsIntraBlock);
194 VNInfo* PerformPHIConstructionFallBack(MachineBasicBlock::iterator Use,
195 MachineBasicBlock* MBB, LiveInterval* LI,
196 SmallPtrSet<MachineInstr*, 4>& Visited,
197 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
198 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
199 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
200 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
201 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
202 bool IsTopLevel, bool IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000203};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000204} // end anonymous namespace
205
206char PreAllocSplitting::ID = 0;
207
208static RegisterPass<PreAllocSplitting>
209X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
210
211const PassInfo *const llvm::PreAllocSplittingID = &X;
212
Evan Chengf5cd4f02008-10-23 20:43:13 +0000213
214/// findNextEmptySlot - Find a gap after the given machine instruction in the
215/// instruction index map. If there isn't one, return end().
216MachineBasicBlock::iterator
217PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000218 SlotIndex &SpotIndex) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000219 MachineBasicBlock::iterator MII = MI;
220 if (++MII != MBB->end()) {
Lang Hames233a60e2009-11-03 23:52:08 +0000221 SlotIndex Index =
Lang Hames86511252009-09-04 20:41:11 +0000222 LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
Lang Hames233a60e2009-11-03 23:52:08 +0000223 if (Index != SlotIndex()) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000224 SpotIndex = Index;
225 return MII;
226 }
227 }
228 return MBB->end();
229}
230
231/// findSpillPoint - Find a gap as far away from the given MI that's suitable
232/// for spilling the current live interval. The index must be before any
233/// defs and uses of the live interval register in the mbb. Return begin() if
234/// none is found.
235MachineBasicBlock::iterator
236PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000237 MachineInstr *DefMI,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000238 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000239 SlotIndex &SpillIndex) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000240 MachineBasicBlock::iterator Pt = MBB->begin();
241
Owen Anderson696a1302009-03-31 08:27:09 +0000242 MachineBasicBlock::iterator MII = MI;
243 MachineBasicBlock::iterator EndPt = DefMI
244 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
Owen Anderson4cafbb52009-02-20 10:02:23 +0000245
Owen Anderson696a1302009-03-31 08:27:09 +0000246 while (MII != EndPt && !RefsInMBB.count(MII) &&
247 MII->getOpcode() != TRI->getCallFrameSetupOpcode())
248 --MII;
249 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson4cafbb52009-02-20 10:02:23 +0000250
Owen Anderson696a1302009-03-31 08:27:09 +0000251 while (MII != EndPt && !RefsInMBB.count(MII)) {
Lang Hames233a60e2009-11-03 23:52:08 +0000252 SlotIndex Index = LIs->getInstructionIndex(MII);
Owen Anderson3ef45492009-01-29 22:13:06 +0000253
Owen Anderson696a1302009-03-31 08:27:09 +0000254 // We can't insert the spill between the barrier (a call), and its
255 // corresponding call frame setup.
256 if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
257 while (MII->getOpcode() != TRI->getCallFrameSetupOpcode()) {
Owen Anderson5734c942009-02-05 05:58:41 +0000258 --MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000259 if (MII == EndPt) {
260 return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000261 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000262 }
Owen Anderson696a1302009-03-31 08:27:09 +0000263 continue;
264 } else if (LIs->hasGapBeforeInstr(Index)) {
265 Pt = MII;
266 SpillIndex = LIs->findGapBeforeInstr(Index, true);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000267 }
Owen Anderson696a1302009-03-31 08:27:09 +0000268
269 if (RefsInMBB.count(MII))
270 return Pt;
271
272
273 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000274 }
275
276 return Pt;
277}
278
279/// findRestorePoint - Find a gap in the instruction index map that's suitable
280/// for restoring the current live interval value. The index must be before any
281/// uses of the live interval register in the mbb. Return end() if none is
282/// found.
283MachineBasicBlock::iterator
284PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000285 SlotIndex LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000286 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000287 SlotIndex &RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000288 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
289 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000290 MachineBasicBlock::iterator Pt = MBB->end();
Owen Anderson696a1302009-03-31 08:27:09 +0000291 MachineBasicBlock::iterator EndPt = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000292
Owen Anderson696a1302009-03-31 08:27:09 +0000293 // We start at the call, so walk forward until we find the call frame teardown
294 // since we can't insert restores before that. Bail if we encounter a use
295 // during this time.
296 MachineBasicBlock::iterator MII = MI;
297 if (MII == EndPt) return Pt;
298
299 while (MII != EndPt && !RefsInMBB.count(MII) &&
300 MII->getOpcode() != TRI->getCallFrameDestroyOpcode())
301 ++MII;
302 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
303 ++MII;
304
305 // FIXME: Limit the number of instructions to examine to reduce
306 // compile time?
307 while (MII != EndPt) {
Lang Hames233a60e2009-11-03 23:52:08 +0000308 SlotIndex Index = LIs->getInstructionIndex(MII);
Owen Anderson696a1302009-03-31 08:27:09 +0000309 if (Index > LastIdx)
310 break;
Lang Hames233a60e2009-11-03 23:52:08 +0000311 SlotIndex Gap = LIs->findGapBeforeInstr(Index);
Owen Anderson3ef45492009-01-29 22:13:06 +0000312
Owen Anderson696a1302009-03-31 08:27:09 +0000313 // We can't insert a restore between the barrier (a call) and its
314 // corresponding call frame teardown.
315 if (MII->getOpcode() == TRI->getCallFrameSetupOpcode()) {
316 do {
317 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000318 ++MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000319 } while (MII->getOpcode() != TRI->getCallFrameDestroyOpcode());
Lang Hames233a60e2009-11-03 23:52:08 +0000320 } else if (Gap != SlotIndex()) {
Owen Anderson696a1302009-03-31 08:27:09 +0000321 Pt = MII;
322 RestoreIndex = Gap;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000323 }
Owen Anderson696a1302009-03-31 08:27:09 +0000324
325 if (RefsInMBB.count(MII))
326 return Pt;
327
328 ++MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000329 }
330
331 return Pt;
332}
333
Evan Chengd0e32c52008-10-29 05:06:14 +0000334/// CreateSpillStackSlot - Create a stack slot for the live interval being
335/// split. If the live interval was previously split, just reuse the same
336/// slot.
337int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
338 const TargetRegisterClass *RC) {
339 int SS;
340 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
341 if (I != IntervalSSMap.end()) {
342 SS = I->second;
343 } else {
344 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
345 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000346 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000347
348 // Create live interval for stack slot.
Evan Chengc781a242009-05-03 18:32:42 +0000349 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Evan Cheng54898932008-10-29 08:39:34 +0000350 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000351 CurrSValNo = CurrSLI->getValNumInfo(0);
352 else
Lang Hames233a60e2009-11-03 23:52:08 +0000353 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000354 LSs->getVNInfoAllocator());
Evan Chengd0e32c52008-10-29 05:06:14 +0000355 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000356}
357
Evan Chengd0e32c52008-10-29 05:06:14 +0000358/// IsAvailableInStack - Return true if register is available in a split stack
359/// slot at the specified index.
360bool
Evan Cheng54898932008-10-29 08:39:34 +0000361PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000362 unsigned Reg, SlotIndex DefIndex,
363 SlotIndex RestoreIndex,
364 SlotIndex &SpillIndex,
Evan Cheng54898932008-10-29 08:39:34 +0000365 int& SS) const {
366 if (!DefMBB)
367 return false;
368
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000369 DenseMap<unsigned, int>::const_iterator I = IntervalSSMap.find(Reg);
Evan Chengd0e32c52008-10-29 05:06:14 +0000370 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000371 return false;
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000372 DenseMap<SlotIndex, SlotIndex>::const_iterator
Lang Hames86511252009-09-04 20:41:11 +0000373 II = Def2SpillMap.find(DefIndex);
Evan Cheng54898932008-10-29 08:39:34 +0000374 if (II == Def2SpillMap.end())
375 return false;
376
377 // If last spill of def is in the same mbb as barrier mbb (where restore will
378 // be), make sure it's not below the intended restore index.
379 // FIXME: Undo the previous spill?
380 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
381 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
382 return false;
383
384 SS = I->second;
385 SpillIndex = II->second;
386 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000387}
388
Evan Chengd0e32c52008-10-29 05:06:14 +0000389/// UpdateSpillSlotInterval - Given the specified val# of the register live
390/// interval being split, and the spill and restore indicies, update the live
391/// interval of the spill stack slot.
392void
Lang Hames233a60e2009-11-03 23:52:08 +0000393PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, SlotIndex SpillIndex,
394 SlotIndex RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000395 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
396 "Expect restore in the barrier mbb");
397
398 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
399 if (MBB == BarrierMBB) {
400 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000401 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
402 CurrSLI->addRange(SLR);
403 return;
404 }
405
Evan Cheng54898932008-10-29 08:39:34 +0000406 SmallPtrSet<MachineBasicBlock*, 4> Processed;
Lang Hames233a60e2009-11-03 23:52:08 +0000407 SlotIndex EndIdx = LIs->getMBBEndIdx(MBB);
408 LiveRange SLR(SpillIndex, EndIdx.getNextSlot(), CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000409 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000410 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000411
412 // Start from the spill mbb, figure out the extend of the spill slot's
413 // live interval.
414 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000415 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
416 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000417 // If live range extend beyond end of mbb, add successors to work list.
418 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
419 SE = MBB->succ_end(); SI != SE; ++SI)
420 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000421
422 while (!WorkList.empty()) {
423 MachineBasicBlock *MBB = WorkList.back();
424 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000425 if (Processed.count(MBB))
426 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000427 SlotIndex Idx = LIs->getMBBStartIdx(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000428 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000429 if (LR && LR->valno == ValNo) {
430 EndIdx = LIs->getMBBEndIdx(MBB);
431 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000432 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000433 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000434 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000435 } else if (LR->end > EndIdx) {
436 // Live range extends beyond end of mbb, process successors.
Lang Hames233a60e2009-11-03 23:52:08 +0000437 LiveRange SLR(Idx, EndIdx.getNextIndex(), CurrSValNo);
Evan Cheng54898932008-10-29 08:39:34 +0000438 CurrSLI->addRange(SLR);
439 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
440 SE = MBB->succ_end(); SI != SE; ++SI)
441 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000442 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000443 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000444 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000445 }
Evan Cheng54898932008-10-29 08:39:34 +0000446 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000447 }
448 }
449}
450
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000451/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
452/// construction algorithm to compute the ranges and valnos for an interval.
Evan Cheng19a72582009-02-02 18:33:18 +0000453VNInfo*
454PreAllocSplitting::PerformPHIConstruction(MachineBasicBlock::iterator UseI,
455 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000456 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000457 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
458 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
459 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000460 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
461 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000462 bool IsTopLevel, bool IsIntraBlock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000463 // Return memoized result if it's available.
Evan Cheng19a72582009-02-02 18:33:18 +0000464 if (IsTopLevel && Visited.count(UseI) && NewVNs.count(UseI))
465 return NewVNs[UseI];
466 else if (!IsTopLevel && IsIntraBlock && NewVNs.count(UseI))
467 return NewVNs[UseI];
468 else if (!IsIntraBlock && LiveOut.count(MBB))
Owen Anderson7d211e22008-12-31 02:00:25 +0000469 return LiveOut[MBB];
470
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000471 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000472 bool ContainsDefs = Defs.count(MBB);
473 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000474
Evan Cheng19a72582009-02-02 18:33:18 +0000475 VNInfo* RetVNI = 0;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000476
477 // Enumerate the cases of use/def contaning blocks.
478 if (!ContainsDefs && !ContainsUses) {
Evan Cheng19a72582009-02-02 18:33:18 +0000479 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
480 NewVNs, LiveOut, Phis,
481 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000482 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000483 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000484
485 // Search for the def in this block. If we don't find it before the
486 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000487 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000488 // always be an end() iterator.
Evan Cheng19a72582009-02-02 18:33:18 +0000489 assert(UseI == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000490
Evan Cheng19a72582009-02-02 18:33:18 +0000491 MachineBasicBlock::iterator Walker = UseI;
492 --Walker;
493 while (Walker != MBB->begin()) {
494 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000495 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000496 --Walker;
497 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000498
499 // Once we've found it, extend its VNInfo to our instruction.
Lang Hames233a60e2009-11-03 23:52:08 +0000500 SlotIndex DefIndex = LIs->getInstructionIndex(Walker);
501 DefIndex = DefIndex.getDefIndex();
502 SlotIndex EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000503
Evan Cheng19a72582009-02-02 18:33:18 +0000504 RetVNI = NewVNs[Walker];
Lang Hames233a60e2009-11-03 23:52:08 +0000505 LI->addRange(LiveRange(DefIndex, EndIndex.getNextSlot(), RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000506 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000507 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000508
509 // Search for the use in this block that precedes the instruction we care
Evan Cheng19a72582009-02-02 18:33:18 +0000510 // about, going to the fallback case if we don't find it.
511 if (UseI == MBB->begin())
512 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
513 Uses, NewVNs, LiveOut, Phis,
514 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000515
Evan Cheng19a72582009-02-02 18:33:18 +0000516 MachineBasicBlock::iterator Walker = UseI;
517 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000518 bool found = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000519 while (Walker != MBB->begin()) {
520 if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000521 found = true;
522 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000523 }
524 --Walker;
525 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000526
527 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000528 if (!found) {
Evan Cheng19a72582009-02-02 18:33:18 +0000529 if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000530 found = true;
531 else
Evan Cheng19a72582009-02-02 18:33:18 +0000532 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
533 Uses, NewVNs, LiveOut, Phis,
534 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000535 }
536
Lang Hames233a60e2009-11-03 23:52:08 +0000537 SlotIndex UseIndex = LIs->getInstructionIndex(Walker);
538 UseIndex = UseIndex.getUseIndex();
539 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000540 if (IsIntraBlock) {
541 EndIndex = LIs->getInstructionIndex(UseI);
Lang Hames233a60e2009-11-03 23:52:08 +0000542 EndIndex = EndIndex.getUseIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000543 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000544 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000545
546 // Now, recursively phi construct the VNInfo for the use we found,
547 // and then extend it to include the instruction we care about
Evan Cheng19a72582009-02-02 18:33:18 +0000548 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
549 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000550
Lang Hames233a60e2009-11-03 23:52:08 +0000551 LI->addRange(LiveRange(UseIndex, EndIndex.getNextSlot(), RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000552
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000553 // FIXME: Need to set kills properly for inter-block stuff.
Lang Hames86511252009-09-04 20:41:11 +0000554 if (RetVNI->isKill(UseIndex)) RetVNI->removeKill(UseIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000555 if (IsIntraBlock)
Lang Hames86511252009-09-04 20:41:11 +0000556 RetVNI->addKill(EndIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000557 } else if (ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000558 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
559 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000560
561 // This case is basically a merging of the two preceding case, with the
562 // special note that checking for defs must take precedence over checking
563 // for uses, because of two-address instructions.
564
Evan Cheng19a72582009-02-02 18:33:18 +0000565 if (UseI == MBB->begin())
566 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
567 NewVNs, LiveOut, Phis,
568 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000569
Evan Cheng19a72582009-02-02 18:33:18 +0000570 MachineBasicBlock::iterator Walker = UseI;
571 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000572 bool foundDef = false;
573 bool foundUse = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000574 while (Walker != MBB->begin()) {
575 if (BlockDefs.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000576 foundDef = true;
577 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000578 } else if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000579 foundUse = true;
580 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000581 }
582 --Walker;
583 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000584
585 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000586 if (!foundDef && !foundUse) {
Evan Cheng19a72582009-02-02 18:33:18 +0000587 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000588 foundDef = true;
Evan Cheng19a72582009-02-02 18:33:18 +0000589 else if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000590 foundUse = true;
591 else
Evan Cheng19a72582009-02-02 18:33:18 +0000592 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
593 Uses, NewVNs, LiveOut, Phis,
594 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000595 }
596
Lang Hames233a60e2009-11-03 23:52:08 +0000597 SlotIndex StartIndex = LIs->getInstructionIndex(Walker);
598 StartIndex = foundDef ? StartIndex.getDefIndex() : StartIndex.getUseIndex();
599 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000600 if (IsIntraBlock) {
601 EndIndex = LIs->getInstructionIndex(UseI);
Lang Hames233a60e2009-11-03 23:52:08 +0000602 EndIndex = EndIndex.getUseIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000603 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000604 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000605
606 if (foundDef)
Evan Cheng19a72582009-02-02 18:33:18 +0000607 RetVNI = NewVNs[Walker];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000608 else
Evan Cheng19a72582009-02-02 18:33:18 +0000609 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
610 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000611
Lang Hames233a60e2009-11-03 23:52:08 +0000612 LI->addRange(LiveRange(StartIndex, EndIndex.getNextSlot(), RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000613
Lang Hames86511252009-09-04 20:41:11 +0000614 if (foundUse && RetVNI->isKill(StartIndex))
615 RetVNI->removeKill(StartIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000616 if (IsIntraBlock) {
Lang Hames86511252009-09-04 20:41:11 +0000617 RetVNI->addKill(EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000618 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000619 }
620
621 // Memoize results so we don't have to recompute them.
Evan Cheng19a72582009-02-02 18:33:18 +0000622 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000623 else {
Evan Cheng19a72582009-02-02 18:33:18 +0000624 if (!NewVNs.count(UseI))
625 NewVNs[UseI] = RetVNI;
626 Visited.insert(UseI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000627 }
628
Evan Cheng19a72582009-02-02 18:33:18 +0000629 return RetVNI;
630}
631
632/// PerformPHIConstructionFallBack - PerformPHIConstruction fall back path.
633///
634VNInfo*
635PreAllocSplitting::PerformPHIConstructionFallBack(MachineBasicBlock::iterator UseI,
636 MachineBasicBlock* MBB, LiveInterval* LI,
637 SmallPtrSet<MachineInstr*, 4>& Visited,
638 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
639 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
640 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
641 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
642 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
643 bool IsTopLevel, bool IsIntraBlock) {
644 // NOTE: Because this is the fallback case from other cases, we do NOT
645 // assume that we are not intrablock here.
646 if (Phis.count(MBB)) return Phis[MBB];
647
Lang Hames233a60e2009-11-03 23:52:08 +0000648 SlotIndex StartIndex = LIs->getMBBStartIdx(MBB);
Lang Hames857c4e02009-06-17 21:01:20 +0000649 VNInfo *RetVNI = Phis[MBB] =
Lang Hames233a60e2009-11-03 23:52:08 +0000650 LI->getNextValue(SlotIndex(), /*FIXME*/ 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000651 LIs->getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +0000652
Evan Cheng19a72582009-02-02 18:33:18 +0000653 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
654
655 // If there are no uses or defs between our starting point and the
656 // beginning of the block, then recursive perform phi construction
657 // on our predecessors.
658 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
659 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
660 PE = MBB->pred_end(); PI != PE; ++PI) {
661 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
662 Visited, Defs, Uses, NewVNs,
663 LiveOut, Phis, false, false);
664 if (Incoming != 0)
665 IncomingVNs[*PI] = Incoming;
666 }
667
Lang Hames857c4e02009-06-17 21:01:20 +0000668 if (MBB->pred_size() == 1 && !RetVNI->hasPHIKill()) {
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000669 VNInfo* OldVN = RetVNI;
670 VNInfo* NewVN = IncomingVNs.begin()->second;
671 VNInfo* MergedVN = LI->MergeValueNumberInto(OldVN, NewVN);
672 if (MergedVN == OldVN) std::swap(OldVN, NewVN);
673
674 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator LOI = LiveOut.begin(),
675 LOE = LiveOut.end(); LOI != LOE; ++LOI)
676 if (LOI->second == OldVN)
677 LOI->second = MergedVN;
678 for (DenseMap<MachineInstr*, VNInfo*>::iterator NVI = NewVNs.begin(),
679 NVE = NewVNs.end(); NVI != NVE; ++NVI)
680 if (NVI->second == OldVN)
681 NVI->second = MergedVN;
682 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator PI = Phis.begin(),
683 PE = Phis.end(); PI != PE; ++PI)
684 if (PI->second == OldVN)
685 PI->second = MergedVN;
686 RetVNI = MergedVN;
Evan Cheng19a72582009-02-02 18:33:18 +0000687 } else {
688 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
689 // VNInfo to represent the joined value.
690 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
691 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
Lang Hames857c4e02009-06-17 21:01:20 +0000692 I->second->setHasPHIKill(true);
Lang Hames233a60e2009-11-03 23:52:08 +0000693 SlotIndex KillIndex = LIs->getMBBEndIdx(I->first);
Lang Hames86511252009-09-04 20:41:11 +0000694 if (!I->second->isKill(KillIndex))
695 I->second->addKill(KillIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000696 }
697 }
698
Lang Hames233a60e2009-11-03 23:52:08 +0000699 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000700 if (IsIntraBlock) {
701 EndIndex = LIs->getInstructionIndex(UseI);
Lang Hames233a60e2009-11-03 23:52:08 +0000702 EndIndex = EndIndex.getUseIndex();
Evan Cheng19a72582009-02-02 18:33:18 +0000703 } else
704 EndIndex = LIs->getMBBEndIdx(MBB);
Lang Hames233a60e2009-11-03 23:52:08 +0000705 LI->addRange(LiveRange(StartIndex, EndIndex.getNextSlot(), RetVNI));
Evan Cheng19a72582009-02-02 18:33:18 +0000706 if (IsIntraBlock)
Lang Hames86511252009-09-04 20:41:11 +0000707 RetVNI->addKill(EndIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000708
709 // Memoize results so we don't have to recompute them.
710 if (!IsIntraBlock)
711 LiveOut[MBB] = RetVNI;
712 else {
713 if (!NewVNs.count(UseI))
714 NewVNs[UseI] = RetVNI;
715 Visited.insert(UseI);
716 }
717
718 return RetVNI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000719}
720
721/// ReconstructLiveInterval - Recompute a live interval from scratch.
722void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
723 BumpPtrAllocator& Alloc = LIs->getVNInfoAllocator();
724
725 // Clear the old ranges and valnos;
726 LI->clear();
727
728 // Cache the uses and defs of the register
729 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
730 RegMap Defs, Uses;
731
732 // Keep track of the new VNs we're creating.
733 DenseMap<MachineInstr*, VNInfo*> NewVNs;
734 SmallPtrSet<VNInfo*, 2> PhiVNs;
735
736 // Cache defs, and create a new VNInfo for each def.
737 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
738 DE = MRI->def_end(); DI != DE; ++DI) {
739 Defs[(*DI).getParent()].insert(&*DI);
740
Lang Hames233a60e2009-11-03 23:52:08 +0000741 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
742 DefIdx = DefIdx.getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000743
Lang Hames857c4e02009-06-17 21:01:20 +0000744 assert(DI->getOpcode() != TargetInstrInfo::PHI &&
745 "Following NewVN isPHIDef flag incorrect. Fix me!");
746 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, true, Alloc);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000747
748 // If the def is a move, set the copy field.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000749 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
750 if (TII->isMoveInstr(*DI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
751 if (DstReg == LI->reg)
Lang Hames52c1afc2009-08-10 23:43:28 +0000752 NewVN->setCopy(&*DI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000753
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000754 NewVNs[&*DI] = NewVN;
755 }
756
757 // Cache uses as a separate pass from actually processing them.
758 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
759 UE = MRI->use_end(); UI != UE; ++UI)
760 Uses[(*UI).getParent()].insert(&*UI);
761
762 // Now, actually process every use and use a phi construction algorithm
763 // to walk from it to its reaching definitions, building VNInfos along
764 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000765 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
766 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000767 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000768 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
769 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000770 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000771 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000772 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000773
774 // Add ranges for dead defs
775 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
776 DE = MRI->def_end(); DI != DE; ++DI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000777 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
778 DefIdx = DefIdx.getDefIndex();
Owen Andersond4f6fe52008-12-28 23:35:13 +0000779
780 if (LI->liveAt(DefIdx)) continue;
781
782 VNInfo* DeadVN = NewVNs[&*DI];
Lang Hames233a60e2009-11-03 23:52:08 +0000783 LI->addRange(LiveRange(DefIdx, DefIdx.getNextSlot(), DeadVN));
Lang Hames86511252009-09-04 20:41:11 +0000784 DeadVN->addKill(DefIdx);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000785 }
Evan Cheng35ca9202009-10-09 01:17:11 +0000786
787 // Update kill markers.
788 for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
789 VI != VE; ++VI) {
790 VNInfo* VNI = *VI;
791 for (unsigned i = 0, e = VNI->kills.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +0000792 SlotIndex KillIdx = VNI->kills[i];
793 if (KillIdx.isPHI())
Evan Cheng35ca9202009-10-09 01:17:11 +0000794 continue;
795 MachineInstr *KillMI = LIs->getInstructionFromIndex(KillIdx);
796 if (KillMI) {
797 MachineOperand *KillMO = KillMI->findRegisterUseOperand(CurrLI->reg);
798 if (KillMO)
799 // It could be a dead def.
800 KillMO->setIsKill();
801 }
802 }
803 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000804}
805
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000806/// RenumberValno - Split the given valno out into a new vreg, allowing it to
807/// be allocated to a different register. This function creates a new vreg,
808/// copies the valno and its live ranges over to the new vreg's interval,
809/// removes them from the old interval, and rewrites all uses and defs of
810/// the original reg to the new vreg within those ranges.
811void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000812 SmallVector<VNInfo*, 4> Stack;
813 SmallVector<VNInfo*, 4> VNsToCopy;
814 Stack.push_back(VN);
815
816 // Walk through and copy the valno we care about, and any other valnos
817 // that are two-address redefinitions of the one we care about. These
818 // will need to be rewritten as well. We also check for safety of the
819 // renumbering here, by making sure that none of the valno involved has
820 // phi kills.
821 while (!Stack.empty()) {
822 VNInfo* OldVN = Stack.back();
823 Stack.pop_back();
824
825 // Bail out if we ever encounter a valno that has a PHI kill. We can't
826 // renumber these.
Lang Hames857c4e02009-06-17 21:01:20 +0000827 if (OldVN->hasPHIKill()) return;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000828
829 VNsToCopy.push_back(OldVN);
830
831 // Locate two-address redefinitions
Lang Hamesffd13262009-07-09 03:57:02 +0000832 for (VNInfo::KillSet::iterator KI = OldVN->kills.begin(),
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000833 KE = OldVN->kills.end(); KI != KE; ++KI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000834 assert(!KI->isPHI() &&
Lang Hames86511252009-09-04 20:41:11 +0000835 "VN previously reported having no PHI kills.");
836 MachineInstr* MI = LIs->getInstructionFromIndex(*KI);
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000837 unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg);
838 if (DefIdx == ~0U) continue;
Bob Wilsond9df5012009-04-09 17:16:43 +0000839 if (MI->isRegTiedToUseOperand(DefIdx)) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000840 VNInfo* NextVN =
Lang Hames233a60e2009-11-03 23:52:08 +0000841 CurrLI->findDefinedVNInfoForRegInt(KI->getDefIndex());
Owen Andersonb4b84362009-01-26 21:57:31 +0000842 if (NextVN == OldVN) continue;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000843 Stack.push_back(NextVN);
844 }
845 }
846 }
847
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000848 // Create the new vreg
849 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
850
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000851 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000852 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000853
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000854 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
855 VNsToCopy.end(); OI != OE; ++OI) {
856 VNInfo* OldVN = *OI;
857
858 // Copy the valno over
Lang Hames857c4e02009-06-17 21:01:20 +0000859 VNInfo* NewVN = NewLI.createValueCopy(OldVN, LIs->getVNInfoAllocator());
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000860 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
861
862 // Remove the valno from the old interval
863 CurrLI->removeValNo(OldVN);
864 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000865
866 // Rewrite defs and uses. This is done in two stages to avoid invalidating
867 // the reg_iterator.
868 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
869
870 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
871 E = MRI->reg_end(); I != E; ++I) {
872 MachineOperand& MO = I.getOperand();
Lang Hames233a60e2009-11-03 23:52:08 +0000873 SlotIndex InstrIdx = LIs->getInstructionIndex(&*I);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000874
Lang Hames233a60e2009-11-03 23:52:08 +0000875 if ((MO.isUse() && NewLI.liveAt(InstrIdx.getUseIndex())) ||
876 (MO.isDef() && NewLI.liveAt(InstrIdx.getDefIndex())))
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000877 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
878 }
879
880 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
881 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
882 MachineInstr* Inst = I->first;
883 unsigned OpIdx = I->second;
884 MachineOperand& MO = Inst->getOperand(OpIdx);
885 MO.setReg(NewVReg);
886 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000887
Owen Anderson420dd372009-03-14 21:40:05 +0000888 // Grow the VirtRegMap, since we've created a new vreg.
889 VRM->grow();
890
Owen Anderson45e68552009-01-29 05:28:55 +0000891 // The renumbered vreg shares a stack slot with the old register.
892 if (IntervalSSMap.count(CurrLI->reg))
893 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
894
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000895 NumRenumbers++;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000896}
897
Evan Cheng37844532009-07-16 09:20:10 +0000898bool PreAllocSplitting::Rematerialize(unsigned VReg, VNInfo* ValNo,
Owen Anderson6002e992008-12-04 21:20:30 +0000899 MachineInstr* DefMI,
900 MachineBasicBlock::iterator RestorePt,
Lang Hames233a60e2009-11-03 23:52:08 +0000901 SlotIndex RestoreIdx,
Owen Anderson6002e992008-12-04 21:20:30 +0000902 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
903 MachineBasicBlock& MBB = *RestorePt->getParent();
904
905 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
Lang Hames233a60e2009-11-03 23:52:08 +0000906 SlotIndex KillIdx;
Lang Hames857c4e02009-06-17 21:01:20 +0000907 if (!ValNo->isDefAccurate() || DefMI->getParent() == BarrierMBB)
Owen Anderson6002e992008-12-04 21:20:30 +0000908 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx);
909 else
910 KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx);
911
912 if (KillPt == DefMI->getParent()->end())
913 return false;
914
Evan Cheng37844532009-07-16 09:20:10 +0000915 TII->reMaterialize(MBB, RestorePt, VReg, 0, DefMI);
Owen Anderson6002e992008-12-04 21:20:30 +0000916 LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx);
917
Owen Andersonb4b84362009-01-26 21:57:31 +0000918 ReconstructLiveInterval(CurrLI);
Lang Hames233a60e2009-11-03 23:52:08 +0000919 SlotIndex RematIdx = LIs->getInstructionIndex(prior(RestorePt));
920 RematIdx = RematIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +0000921 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000922
Owen Anderson75fa96b2008-11-19 04:28:29 +0000923 ++NumSplits;
924 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000925 return true;
926}
927
928MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
929 const TargetRegisterClass* RC,
930 MachineInstr* DefMI,
931 MachineInstr* Barrier,
932 MachineBasicBlock* MBB,
933 int& SS,
934 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
935 MachineBasicBlock::iterator Pt = MBB->begin();
936
937 // Go top down if RefsInMBB is empty.
938 if (RefsInMBB.empty())
939 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000940
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000941 MachineBasicBlock::iterator FoldPt = Barrier;
942 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
943 !RefsInMBB.count(FoldPt))
944 --FoldPt;
945
946 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
947 if (OpIdx == -1)
948 return 0;
949
950 SmallVector<unsigned, 1> Ops;
951 Ops.push_back(OpIdx);
952
953 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
954 return 0;
955
956 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
957 if (I != IntervalSSMap.end()) {
958 SS = I->second;
959 } else {
Evan Cheng491f54f2009-10-17 09:20:14 +0000960 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000961 }
962
963 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
964 FoldPt, Ops, SS);
965
966 if (FMI) {
967 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
968 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
969 ++NumFolds;
970
971 IntervalSSMap[vreg] = SS;
Evan Chengc781a242009-05-03 18:32:42 +0000972 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000973 if (CurrSLI->hasAtLeastOneValue())
974 CurrSValNo = CurrSLI->getValNumInfo(0);
975 else
Lang Hames233a60e2009-11-03 23:52:08 +0000976 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000977 LSs->getVNInfoAllocator());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000978 }
979
980 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000981}
982
Owen Andersonc93023a2009-03-04 08:52:31 +0000983MachineInstr* PreAllocSplitting::FoldRestore(unsigned vreg,
984 const TargetRegisterClass* RC,
985 MachineInstr* Barrier,
986 MachineBasicBlock* MBB,
987 int SS,
988 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Andersona2bfb542009-03-05 08:23:20 +0000989 if ((int)RestoreFoldLimit != -1 && RestoreFoldLimit == (int)NumRestoreFolds)
Owen Anderson323c58d2009-03-05 07:19:18 +0000990 return 0;
991
Owen Andersonc93023a2009-03-04 08:52:31 +0000992 // Go top down if RefsInMBB is empty.
993 if (RefsInMBB.empty())
994 return 0;
995
996 // Can't fold a restore between a call stack setup and teardown.
997 MachineBasicBlock::iterator FoldPt = Barrier;
Owen Anderson323c58d2009-03-05 07:19:18 +0000998
999 // Advance from barrier to call frame teardown.
1000 while (FoldPt != MBB->getFirstTerminator() &&
1001 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
1002 if (RefsInMBB.count(FoldPt))
1003 return 0;
Owen Andersonc93023a2009-03-04 08:52:31 +00001004
Owen Anderson323c58d2009-03-05 07:19:18 +00001005 ++FoldPt;
1006 }
1007
1008 if (FoldPt == MBB->getFirstTerminator())
1009 return 0;
1010 else
1011 ++FoldPt;
1012
1013 // Now find the restore point.
1014 while (FoldPt != MBB->getFirstTerminator() && !RefsInMBB.count(FoldPt)) {
Owen Andersonc93023a2009-03-04 08:52:31 +00001015 if (FoldPt->getOpcode() == TRI->getCallFrameSetupOpcode()) {
1016 while (FoldPt != MBB->getFirstTerminator() &&
1017 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
1018 if (RefsInMBB.count(FoldPt))
1019 return 0;
1020
1021 ++FoldPt;
1022 }
1023
Owen Anderson323c58d2009-03-05 07:19:18 +00001024 if (FoldPt == MBB->getFirstTerminator())
1025 return 0;
1026 }
1027
1028 ++FoldPt;
Owen Andersonc93023a2009-03-04 08:52:31 +00001029 }
1030
1031 if (FoldPt == MBB->getFirstTerminator())
1032 return 0;
1033
1034 int OpIdx = FoldPt->findRegisterUseOperandIdx(vreg, true);
1035 if (OpIdx == -1)
1036 return 0;
1037
1038 SmallVector<unsigned, 1> Ops;
1039 Ops.push_back(OpIdx);
1040
1041 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
1042 return 0;
1043
1044 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
1045 FoldPt, Ops, SS);
1046
1047 if (FMI) {
1048 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
1049 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
Owen Anderson323c58d2009-03-05 07:19:18 +00001050 ++NumRestoreFolds;
Owen Andersonc93023a2009-03-04 08:52:31 +00001051 }
1052
1053 return FMI;
1054}
1055
Evan Chengf5cd4f02008-10-23 20:43:13 +00001056/// SplitRegLiveInterval - Split (spill and restore) the given live interval
1057/// so it would not cross the barrier that's being processed. Shrink wrap
1058/// (minimize) the live interval to the last uses.
1059bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
Lang Hames233a60e2009-11-03 23:52:08 +00001060 DEBUG(errs() << "Pre-alloc splitting " << LI->reg << " for " << *Barrier
1061 << " result: ");
1062
Evan Chengf5cd4f02008-10-23 20:43:13 +00001063 CurrLI = LI;
1064
1065 // Find live range where current interval cross the barrier.
1066 LiveInterval::iterator LR =
Lang Hames233a60e2009-11-03 23:52:08 +00001067 CurrLI->FindLiveRangeContaining(BarrierIdx.getUseIndex());
Evan Chengf5cd4f02008-10-23 20:43:13 +00001068 VNInfo *ValNo = LR->valno;
1069
Torok Edwinf3689232009-07-12 20:07:01 +00001070 assert(!ValNo->isUnused() && "Val# is defined by a dead def?");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001071
Lang Hames857c4e02009-06-17 21:01:20 +00001072 MachineInstr *DefMI = ValNo->isDefAccurate()
Evan Cheng06587492008-10-24 02:05:00 +00001073 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +00001074
Owen Andersond3be4622009-01-21 08:18:03 +00001075 // If this would create a new join point, do not split.
Lang Hames233a60e2009-11-03 23:52:08 +00001076 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent())) {
1077 DEBUG(errs() << "FAILED (would create a new join point).\n");
Owen Andersond3be4622009-01-21 08:18:03 +00001078 return false;
Lang Hames233a60e2009-11-03 23:52:08 +00001079 }
Owen Andersond3be4622009-01-21 08:18:03 +00001080
Evan Chengf5cd4f02008-10-23 20:43:13 +00001081 // Find all references in the barrier mbb.
1082 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
1083 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
1084 E = MRI->reg_end(); I != E; ++I) {
1085 MachineInstr *RefMI = &*I;
1086 if (RefMI->getParent() == BarrierMBB)
1087 RefsInMBB.insert(RefMI);
1088 }
1089
1090 // Find a point to restore the value after the barrier.
Lang Hames233a60e2009-11-03 23:52:08 +00001091 SlotIndex RestoreIndex;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001092 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +00001093 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Lang Hames233a60e2009-11-03 23:52:08 +00001094 if (RestorePt == BarrierMBB->end()) {
1095 DEBUG(errs() << "FAILED (could not find a suitable restore point).\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001096 return false;
Lang Hames233a60e2009-11-03 23:52:08 +00001097 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001098
Owen Anderson75fa96b2008-11-19 04:28:29 +00001099 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
1100 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt,
Lang Hames233a60e2009-11-03 23:52:08 +00001101 RestoreIndex, RefsInMBB)) {
1102 DEBUG(errs() << "success (remat).\n");
1103 return true;
1104 }
Owen Anderson75fa96b2008-11-19 04:28:29 +00001105
Evan Chengf5cd4f02008-10-23 20:43:13 +00001106 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001107 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001108 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Lang Hames233a60e2009-11-03 23:52:08 +00001109 SlotIndex SpillIndex;
Evan Cheng06587492008-10-24 02:05:00 +00001110 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001111 int SS = -1;
Lang Hames857c4e02009-06-17 21:01:20 +00001112 if (!ValNo->isDefAccurate()) {
1113 // If we don't know where the def is we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001114 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1115 BarrierMBB, SS, RefsInMBB))) {
1116 SpillIndex = LIs->getInstructionIndex(SpillMI);
1117 } else {
1118 MachineBasicBlock::iterator SpillPt =
1119 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
Lang Hames233a60e2009-11-03 23:52:08 +00001120 if (SpillPt == BarrierMBB->begin()) {
1121 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001122 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001123 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001124 // Add spill.
1125
1126 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1127 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
1128 SpillMI = prior(SpillPt);
1129 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
1130 }
Evan Cheng54898932008-10-29 08:39:34 +00001131 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
1132 RestoreIndex, SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001133 // If it's already split, just restore the value. There is no need to spill
1134 // the def again.
Lang Hames233a60e2009-11-03 23:52:08 +00001135 if (!DefMI) {
1136 DEBUG(errs() << "FAILED (def is dead).\n");
Evan Chengd0e32c52008-10-29 05:06:14 +00001137 return false; // Def is dead. Do nothing.
Lang Hames233a60e2009-11-03 23:52:08 +00001138 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001139
1140 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
Evan Cheng35ca9202009-10-09 01:17:11 +00001141 BarrierMBB, SS, RefsInMBB))) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001142 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001143 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001144 // Check if it's possible to insert a spill after the def MI.
1145 MachineBasicBlock::iterator SpillPt;
1146 if (DefMBB == BarrierMBB) {
1147 // Add spill after the def and the last use before the barrier.
1148 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
1149 RefsInMBB, SpillIndex);
Lang Hames233a60e2009-11-03 23:52:08 +00001150 if (SpillPt == DefMBB->begin()) {
1151 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001152 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001153 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001154 } else {
1155 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
Lang Hames233a60e2009-11-03 23:52:08 +00001156 if (SpillPt == DefMBB->end()) {
1157 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001158 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001159 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001160 }
Evan Cheng35ca9202009-10-09 01:17:11 +00001161 // Add spill.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001162 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng35ca9202009-10-09 01:17:11 +00001163 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg, false, SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001164 SpillMI = prior(SpillPt);
1165 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001166 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001167 }
1168
Evan Cheng54898932008-10-29 08:39:34 +00001169 // Remember def instruction index to spill index mapping.
1170 if (DefMI && SpillMI)
1171 Def2SpillMap[ValNo->def] = SpillIndex;
1172
Evan Chengf5cd4f02008-10-23 20:43:13 +00001173 // Add restore.
Owen Andersonc93023a2009-03-04 08:52:31 +00001174 bool FoldedRestore = false;
1175 if (MachineInstr* LMI = FoldRestore(CurrLI->reg, RC, Barrier,
1176 BarrierMBB, SS, RefsInMBB)) {
1177 RestorePt = LMI;
Owen Anderson323c58d2009-03-05 07:19:18 +00001178 RestoreIndex = LIs->getInstructionIndex(RestorePt);
Owen Andersonc93023a2009-03-04 08:52:31 +00001179 FoldedRestore = true;
1180 } else {
1181 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
1182 MachineInstr *LoadMI = prior(RestorePt);
1183 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
1184 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001185
Evan Chengd0e32c52008-10-29 05:06:14 +00001186 // Update spill stack slot live interval.
Lang Hames233a60e2009-11-03 23:52:08 +00001187 UpdateSpillSlotInterval(ValNo, SpillIndex.getUseIndex().getNextSlot(),
1188 RestoreIndex.getDefIndex());
Evan Chengd0e32c52008-10-29 05:06:14 +00001189
Owen Andersonb4b84362009-01-26 21:57:31 +00001190 ReconstructLiveInterval(CurrLI);
Evan Cheng35ca9202009-10-09 01:17:11 +00001191
Owen Andersonc93023a2009-03-04 08:52:31 +00001192 if (!FoldedRestore) {
Lang Hames233a60e2009-11-03 23:52:08 +00001193 SlotIndex RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
1194 RestoreIdx = RestoreIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +00001195 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RestoreIdx));
Owen Andersonc93023a2009-03-04 08:52:31 +00001196 }
Owen Anderson7d211e22008-12-31 02:00:25 +00001197
Evan Chengae7fa5b2008-10-28 01:48:24 +00001198 ++NumSplits;
Lang Hames233a60e2009-11-03 23:52:08 +00001199 DEBUG(errs() << "success.\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001200 return true;
1201}
1202
1203/// SplitRegLiveIntervals - Split all register live intervals that cross the
1204/// barrier that's being processed.
1205bool
Owen Anderson956ec272009-01-23 00:23:32 +00001206PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1207 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001208 // First find all the virtual registers whose live intervals are intercepted
1209 // by the current barrier.
1210 SmallVector<LiveInterval*, 8> Intervals;
1211 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng4350eb82009-02-06 17:17:30 +00001212 // FIXME: If it's not safe to move any instruction that defines the barrier
1213 // register class, then it means there are some special dependencies which
1214 // codegen is not modelling. Ignore these barriers for now.
1215 if (!TII->isSafeToMoveRegClassDefs(*RC))
Evan Cheng23066282008-10-27 07:14:50 +00001216 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001217 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
1218 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1219 unsigned Reg = VRs[i];
1220 if (!LIs->hasInterval(Reg))
1221 continue;
1222 LiveInterval *LI = &LIs->getInterval(Reg);
1223 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1224 // Virtual register live interval is intercepted by the barrier. We
1225 // should split and shrink wrap its interval if possible.
1226 Intervals.push_back(LI);
1227 }
1228 }
1229
1230 // Process the affected live intervals.
1231 bool Change = false;
1232 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001233 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1234 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001235 LiveInterval *LI = Intervals.back();
1236 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001237 bool result = SplitRegLiveInterval(LI);
1238 if (result) Split.insert(LI);
1239 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001240 }
1241
1242 return Change;
1243}
1244
Owen Anderson45e68552009-01-29 05:28:55 +00001245unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001246 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001247 unsigned Reg, int FrameIndex,
1248 bool& FeedsTwoAddr) {
1249 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001250 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001251 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001252 int StoreFrameIndex;
1253 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001254 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
1255 NonSpills++;
1256
1257 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
Bob Wilsond9df5012009-04-09 17:16:43 +00001258 if (DefIdx != -1 && (*UI)->isRegTiedToUseOperand(DefIdx))
Owen Anderson45e68552009-01-29 05:28:55 +00001259 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001260 }
1261
Owen Anderson45e68552009-01-29 05:28:55 +00001262 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001263}
1264
Owen Anderson956ec272009-01-23 00:23:32 +00001265/// removeDeadSpills - After doing splitting, filter through all intervals we've
1266/// split, and see if any of the spills are unnecessary. If so, remove them.
1267bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1268 bool changed = false;
1269
Owen Anderson4bfc2092009-01-29 05:41:02 +00001270 // Walk over all of the live intervals that were touched by the splitter,
1271 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001272 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1273 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001274 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001275
Owen Anderson4bfc2092009-01-29 05:41:02 +00001276 // First, collect all the uses of the vreg, and sort them by their
1277 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001278 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1279 UE = MRI->use_end(); UI != UE; ++UI) {
Lang Hames233a60e2009-11-03 23:52:08 +00001280 SlotIndex index = LIs->getInstructionIndex(&*UI);
1281 index = index.getUseIndex();
Owen Anderson956ec272009-01-23 00:23:32 +00001282
1283 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001284 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001285 }
1286
Owen Anderson4bfc2092009-01-29 05:41:02 +00001287 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1288 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001289 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1290 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001291
1292 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1293 return changed;
1294
Owen Anderson956ec272009-01-23 00:23:32 +00001295 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001296
1297 // We don't currently try to handle definitions with PHI kills, because
1298 // it would involve processing more than one VNInfo at once.
Lang Hames857c4e02009-06-17 21:01:20 +00001299 if (CurrVN->hasPHIKill()) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001300
Owen Anderson4bfc2092009-01-29 05:41:02 +00001301 // We also don't try to handle the results of PHI joins, since there's
1302 // no defining instruction to analyze.
Lang Hames857c4e02009-06-17 21:01:20 +00001303 if (!CurrVN->isDefAccurate() || CurrVN->isUnused()) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001304
Owen Anderson4bfc2092009-01-29 05:41:02 +00001305 // We're only interested in eliminating cruft introduced by the splitter,
1306 // is of the form load-use or load-use-store. First, check that the
1307 // definition is a load, and remember what stack slot we loaded it from.
Lang Hames857c4e02009-06-17 21:01:20 +00001308 MachineInstr* DefMI = LIs->getInstructionFromIndex(CurrVN->def);
Owen Anderson956ec272009-01-23 00:23:32 +00001309 int FrameIndex;
1310 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1311
Owen Anderson4bfc2092009-01-29 05:41:02 +00001312 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001313 if (VNUseCount[CurrVN].size() == 0) {
1314 LIs->RemoveMachineInstrFromMaps(DefMI);
1315 (*LI)->removeValNo(CurrVN);
1316 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001317 VNUseCount.erase(CurrVN);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001318 NumDeadSpills++;
1319 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001320 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001321 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001322
Owen Anderson4bfc2092009-01-29 05:41:02 +00001323 // Second, get the number of non-store uses of the definition, as well as
1324 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001325 bool FeedsTwoAddr = false;
1326 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1327 (*LI)->reg, FrameIndex,
1328 FeedsTwoAddr);
1329
Owen Anderson4bfc2092009-01-29 05:41:02 +00001330 // If there's one non-store use and it doesn't feed a two-addr, then
1331 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001332 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001333 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001334 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1335 int StoreFrameIndex;
1336 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1337 while (UI != VNUseCount[CurrVN].end() &&
1338 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1339 ++UI;
1340 if (UI != VNUseCount[CurrVN].end())
1341 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1342 }
Owen Anderson45e68552009-01-29 05:28:55 +00001343 if (UI == VNUseCount[CurrVN].end()) continue;
1344
1345 MachineInstr* use = *UI;
1346
Owen Anderson4bfc2092009-01-29 05:41:02 +00001347 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001348 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1349 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001350 SmallVector<unsigned, 1> Ops;
1351 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001352 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1353
1354 MachineInstr* NewMI =
1355 TII->foldMemoryOperand(*use->getParent()->getParent(),
1356 use, Ops, FrameIndex);
1357
1358 if (!NewMI) continue;
1359
Owen Anderson4bfc2092009-01-29 05:41:02 +00001360 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001361 LIs->RemoveMachineInstrFromMaps(DefMI);
1362 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1363 (*LI)->removeValNo(CurrVN);
1364
1365 DefMI->eraseFromParent();
1366 MachineBasicBlock* MBB = use->getParent();
1367 NewMI = MBB->insert(MBB->erase(use), NewMI);
1368 VNUseCount[CurrVN].erase(use);
1369
Owen Anderson4bfc2092009-01-29 05:41:02 +00001370 // Remove deleted instructions. Note that we need to remove them from
1371 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001372 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1373 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1374 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001375 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001376 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1377 ++VNI)
1378 if (VNI->first != CurrVN)
1379 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001380 LIs->RemoveMachineInstrFromMaps(*II);
1381 (*II)->eraseFromParent();
1382 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001383
1384 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001385
1386 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1387 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1388 if (VI->second.erase(use))
1389 VI->second.insert(NewMI);
1390
1391 NumDeadSpills++;
1392 changed = true;
1393 continue;
1394 }
1395
Owen Anderson4bfc2092009-01-29 05:41:02 +00001396 // If there's more than one non-store instruction, we can't profitably
1397 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001398 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001399
Owen Anderson4bfc2092009-01-29 05:41:02 +00001400 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001401 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1402 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
Lang Hamesf035ce52009-11-12 01:24:08 +00001403 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001404 LIs->RemoveMachineInstrFromMaps(*UI);
1405 (*UI)->eraseFromParent();
1406 }
1407
Owen Andersonc0f3a032009-01-29 08:22:06 +00001408 VNUseCount.erase(CurrVN);
1409
Owen Anderson32ca8652009-01-24 10:07:43 +00001410 LIs->RemoveMachineInstrFromMaps(DefMI);
1411 (*LI)->removeValNo(CurrVN);
1412 DefMI->eraseFromParent();
1413 NumDeadSpills++;
1414 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001415 }
1416 }
1417
1418 return changed;
1419}
1420
Owen Andersonf1f75b12008-11-04 22:22:41 +00001421bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1422 MachineBasicBlock* DefMBB,
1423 MachineBasicBlock* BarrierMBB) {
1424 if (DefMBB == BarrierMBB)
1425 return false;
1426
Lang Hames857c4e02009-06-17 21:01:20 +00001427 if (LR->valno->hasPHIKill())
Owen Andersonf1f75b12008-11-04 22:22:41 +00001428 return false;
1429
Lang Hames233a60e2009-11-03 23:52:08 +00001430 SlotIndex MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
Owen Andersonf1f75b12008-11-04 22:22:41 +00001431 if (LR->end < MBBEnd)
1432 return false;
1433
1434 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1435 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1436 return true;
1437
1438 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1439 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1440 typedef std::pair<MachineBasicBlock*,
1441 MachineBasicBlock::succ_iterator> ItPair;
1442 SmallVector<ItPair, 4> Stack;
1443 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1444
1445 while (!Stack.empty()) {
1446 ItPair P = Stack.back();
1447 Stack.pop_back();
1448
1449 MachineBasicBlock* PredMBB = P.first;
1450 MachineBasicBlock::succ_iterator S = P.second;
1451
1452 if (S == PredMBB->succ_end())
1453 continue;
1454 else if (Visited.count(*S)) {
1455 Stack.push_back(std::make_pair(PredMBB, ++S));
1456 continue;
1457 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001458 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001459
1460 MachineBasicBlock* MBB = *S;
1461 Visited.insert(MBB);
1462
1463 if (MBB == BarrierMBB)
1464 return true;
1465
1466 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1467 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1468 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1469 while (MDTN) {
1470 if (MDTN == DefMDTN)
1471 return true;
1472 else if (MDTN == BarrierMDTN)
1473 break;
1474 MDTN = MDTN->getIDom();
1475 }
1476
1477 MBBEnd = LIs->getMBBEndIdx(MBB);
1478 if (LR->end > MBBEnd)
1479 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1480 }
1481
1482 return false;
1483}
1484
1485
Evan Cheng09e8ca82008-10-20 21:44:59 +00001486bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001487 CurrMF = &MF;
1488 TM = &MF.getTarget();
Owen Anderson3ef45492009-01-29 22:13:06 +00001489 TRI = TM->getRegisterInfo();
Evan Chengd0e32c52008-10-29 05:06:14 +00001490 TII = TM->getInstrInfo();
1491 MFI = MF.getFrameInfo();
1492 MRI = &MF.getRegInfo();
Lang Hames233a60e2009-11-03 23:52:08 +00001493 SIs = &getAnalysis<SlotIndexes>();
Evan Chengd0e32c52008-10-29 05:06:14 +00001494 LIs = &getAnalysis<LiveIntervals>();
1495 LSs = &getAnalysis<LiveStacks>();
Owen Anderson420dd372009-03-14 21:40:05 +00001496 VRM = &getAnalysis<VirtRegMap>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001497
1498 bool MadeChange = false;
1499
1500 // Make sure blocks are numbered in order.
1501 MF.RenumberBlocks();
1502
Evan Cheng54898932008-10-29 08:39:34 +00001503 MachineBasicBlock *Entry = MF.begin();
1504 SmallPtrSet<MachineBasicBlock*,16> Visited;
1505
Owen Anderson956ec272009-01-23 00:23:32 +00001506 SmallPtrSet<LiveInterval*, 8> Split;
1507
Evan Cheng54898932008-10-29 08:39:34 +00001508 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1509 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1510 DFI != E; ++DFI) {
1511 BarrierMBB = *DFI;
1512 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1513 E = BarrierMBB->end(); I != E; ++I) {
1514 Barrier = &*I;
1515 const TargetRegisterClass **BarrierRCs =
1516 Barrier->getDesc().getRegClassBarriers();
1517 if (!BarrierRCs)
1518 continue;
1519 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001520 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001521 }
1522 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001523
Owen Anderson956ec272009-01-23 00:23:32 +00001524 MadeChange |= removeDeadSpills(Split);
1525
Evan Chengf5cd4f02008-10-23 20:43:13 +00001526 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001527}